1/*
2 * HWMON Driver for Dialog DA9055
3 *
4 * Copyright(c) 2012 Dialog Semiconductor Ltd.
5 *
6 * Author: David Dajun Chen <dchen@diasemi.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
12 *
13 */
14
15#include <linux/delay.h>
16#include <linux/err.h>
17#include <linux/hwmon.h>
18#include <linux/hwmon-sysfs.h>
19#include <linux/init.h>
20#include <linux/kernel.h>
21#include <linux/module.h>
22#include <linux/platform_device.h>
23#include <linux/completion.h>
24
25#include <linux/mfd/da9055/core.h>
26#include <linux/mfd/da9055/reg.h>
27
28#define DA9055_ADCIN_DIV 102
29#define DA9055_VSYS_DIV 85
30
31#define DA9055_ADC_VSYS 0
32#define DA9055_ADC_ADCIN1 1
33#define DA9055_ADC_ADCIN2 2
34#define DA9055_ADC_ADCIN3 3
35#define DA9055_ADC_TJUNC 4
36
37struct da9055_hwmon {
38 struct da9055 *da9055;
39 struct mutex hwmon_lock;
40 struct mutex irq_lock;
41 struct completion done;
42};
43
44static const char * const input_names[] = {
45 [DA9055_ADC_VSYS] = "VSYS",
46 [DA9055_ADC_ADCIN1] = "ADC IN1",
47 [DA9055_ADC_ADCIN2] = "ADC IN2",
48 [DA9055_ADC_ADCIN3] = "ADC IN3",
49 [DA9055_ADC_TJUNC] = "CHIP TEMP",
50};
51
52static const u8 chan_mux[DA9055_ADC_TJUNC + 1] = {
53 [DA9055_ADC_VSYS] = DA9055_ADC_MUX_VSYS,
54 [DA9055_ADC_ADCIN1] = DA9055_ADC_MUX_ADCIN1,
55 [DA9055_ADC_ADCIN2] = DA9055_ADC_MUX_ADCIN2,
56 [DA9055_ADC_ADCIN3] = DA9055_ADC_MUX_ADCIN3,
57 [DA9055_ADC_TJUNC] = DA9055_ADC_MUX_T_SENSE,
58};
59
60static int da9055_adc_manual_read(struct da9055_hwmon *hwmon,
61 unsigned char channel)
62{
63 int ret;
64 unsigned short calc_data;
65 unsigned short data;
66 unsigned char mux_sel;
67 struct da9055 *da9055 = hwmon->da9055;
68
69 if (channel > DA9055_ADC_TJUNC)
70 return -EINVAL;
71
72 mutex_lock(&hwmon->irq_lock);
73
74 /* Selects desired MUX for manual conversion */
75 mux_sel = chan_mux[channel] | DA9055_ADC_MAN_CONV;
76
77 ret = da9055_reg_write(da9055, DA9055_REG_ADC_MAN, mux_sel);
78 if (ret < 0)
79 goto err;
80
81 /* Wait for an interrupt */
82 if (!wait_for_completion_timeout(&hwmon->done,
83 msecs_to_jiffies(500))) {
84 dev_err(da9055->dev,
85 "timeout waiting for ADC conversion interrupt\n");
86 ret = -ETIMEDOUT;
87 goto err;
88 }
89
90 ret = da9055_reg_read(da9055, DA9055_REG_ADC_RES_H);
91 if (ret < 0)
92 goto err;
93
94 calc_data = (unsigned short)ret;
95 data = calc_data << 2;
96
97 ret = da9055_reg_read(da9055, DA9055_REG_ADC_RES_L);
98 if (ret < 0)
99 goto err;
100
101 calc_data = (unsigned short)(ret & DA9055_ADC_LSB_MASK);
102 data |= calc_data;
103
104 ret = data;
105
106err:
107 mutex_unlock(&hwmon->irq_lock);
108 return ret;
109}
110
111static irqreturn_t da9055_auxadc_irq(int irq, void *irq_data)
112{
113 struct da9055_hwmon *hwmon = irq_data;
114
115 complete(&hwmon->done);
116
117 return IRQ_HANDLED;
118}
119
120/* Conversion function for VSYS and ADCINx */
121static inline int volt_reg_to_mv(int value, int channel)
122{
123 if (channel == DA9055_ADC_VSYS)
124 return DIV_ROUND_CLOSEST(value * 1000, DA9055_VSYS_DIV) + 2500;
125 else
126 return DIV_ROUND_CLOSEST(value * 1000, DA9055_ADCIN_DIV);
127}
128
129static int da9055_enable_auto_mode(struct da9055 *da9055, int channel)
130{
131
132 return da9055_reg_update(da9055, DA9055_REG_ADC_CONT, 1 << channel,
133 1 << channel);
134
135}
136
137static int da9055_disable_auto_mode(struct da9055 *da9055, int channel)
138{
139
140 return da9055_reg_update(da9055, DA9055_REG_ADC_CONT, 1 << channel, 0);
141}
142
143static ssize_t da9055_auto_ch_show(struct device *dev,
144 struct device_attribute *devattr,
145 char *buf)
146{
147 struct da9055_hwmon *hwmon = dev_get_drvdata(dev);
148 int ret, adc;
149 int channel = to_sensor_dev_attr(devattr)->index;
150
151 mutex_lock(&hwmon->hwmon_lock);
152
153 ret = da9055_enable_auto_mode(hwmon->da9055, channel);
154 if (ret < 0)
155 goto hwmon_err;
156
157 usleep_range(10000, 10500);
158
159 adc = da9055_reg_read(hwmon->da9055, DA9055_REG_VSYS_RES + channel);
160 if (adc < 0) {
161 ret = adc;
162 goto hwmon_err_release;
163 }
164
165 ret = da9055_disable_auto_mode(hwmon->da9055, channel);
166 if (ret < 0)
167 goto hwmon_err;
168
169 mutex_unlock(&hwmon->hwmon_lock);
170
171 return sprintf(buf, "%d\n", volt_reg_to_mv(adc, channel));
172
173hwmon_err_release:
174 da9055_disable_auto_mode(hwmon->da9055, channel);
175hwmon_err:
176 mutex_unlock(&hwmon->hwmon_lock);
177 return ret;
178}
179
180static ssize_t da9055_tjunc_show(struct device *dev,
181 struct device_attribute *devattr, char *buf)
182{
183 struct da9055_hwmon *hwmon = dev_get_drvdata(dev);
184 int tjunc;
185 int toffset;
186
187 tjunc = da9055_adc_manual_read(hwmon, DA9055_ADC_TJUNC);
188 if (tjunc < 0)
189 return tjunc;
190
191 toffset = da9055_reg_read(hwmon->da9055, DA9055_REG_T_OFFSET);
192 if (toffset < 0)
193 return toffset;
194
195 /*
196 * Degrees celsius = -0.4084 * (ADC_RES - T_OFFSET) + 307.6332
197 * T_OFFSET is a trim value used to improve accuracy of the result
198 */
199 return sprintf(buf, "%d\n", DIV_ROUND_CLOSEST(-4084 * (tjunc - toffset)
200 + 3076332, 10000));
201}
202
203static ssize_t label_show(struct device *dev,
204 struct device_attribute *devattr, char *buf)
205{
206 return sprintf(buf, "%s\n",
207 input_names[to_sensor_dev_attr(devattr)->index]);
208}
209
210static SENSOR_DEVICE_ATTR_RO(in0_input, da9055_auto_ch, DA9055_ADC_VSYS);
211static SENSOR_DEVICE_ATTR_RO(in0_label, label, DA9055_ADC_VSYS);
212static SENSOR_DEVICE_ATTR_RO(in1_input, da9055_auto_ch, DA9055_ADC_ADCIN1);
213static SENSOR_DEVICE_ATTR_RO(in1_label, label, DA9055_ADC_ADCIN1);
214static SENSOR_DEVICE_ATTR_RO(in2_input, da9055_auto_ch, DA9055_ADC_ADCIN2);
215static SENSOR_DEVICE_ATTR_RO(in2_label, label, DA9055_ADC_ADCIN2);
216static SENSOR_DEVICE_ATTR_RO(in3_input, da9055_auto_ch, DA9055_ADC_ADCIN3);
217static SENSOR_DEVICE_ATTR_RO(in3_label, label, DA9055_ADC_ADCIN3);
218
219static SENSOR_DEVICE_ATTR_RO(temp1_input, da9055_tjunc, DA9055_ADC_TJUNC);
220static SENSOR_DEVICE_ATTR_RO(temp1_label, label, DA9055_ADC_TJUNC);
221
222static struct attribute *da9055_attrs[] = {
223 &sensor_dev_attr_in0_input.dev_attr.attr,
224 &sensor_dev_attr_in0_label.dev_attr.attr,
225 &sensor_dev_attr_in1_input.dev_attr.attr,
226 &sensor_dev_attr_in1_label.dev_attr.attr,
227 &sensor_dev_attr_in2_input.dev_attr.attr,
228 &sensor_dev_attr_in2_label.dev_attr.attr,
229 &sensor_dev_attr_in3_input.dev_attr.attr,
230 &sensor_dev_attr_in3_label.dev_attr.attr,
231
232 &sensor_dev_attr_temp1_input.dev_attr.attr,
233 &sensor_dev_attr_temp1_label.dev_attr.attr,
234 NULL
235};
236
237ATTRIBUTE_GROUPS(da9055);
238
239static int da9055_hwmon_probe(struct platform_device *pdev)
240{
241 struct device *dev = &pdev->dev;
242 struct da9055_hwmon *hwmon;
243 struct device *hwmon_dev;
244 int hwmon_irq, ret;
245
246 hwmon = devm_kzalloc(dev, sizeof(struct da9055_hwmon), GFP_KERNEL);
247 if (!hwmon)
248 return -ENOMEM;
249
250 mutex_init(&hwmon->hwmon_lock);
251 mutex_init(&hwmon->irq_lock);
252
253 init_completion(&hwmon->done);
254 hwmon->da9055 = dev_get_drvdata(pdev->dev.parent);
255
256 hwmon_irq = platform_get_irq_byname(pdev, "HWMON");
257 if (hwmon_irq < 0)
258 return hwmon_irq;
259
260 ret = devm_request_threaded_irq(&pdev->dev, hwmon_irq,
261 NULL, da9055_auxadc_irq,
262 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
263 "adc-irq", hwmon);
264 if (ret != 0) {
265 dev_err(hwmon->da9055->dev, "DA9055 ADC IRQ failed ret=%d\n",
266 ret);
267 return ret;
268 }
269
270 hwmon_dev = devm_hwmon_device_register_with_groups(dev, "da9055",
271 hwmon,
272 da9055_groups);
273 return PTR_ERR_OR_ZERO(hwmon_dev);
274}
275
276static struct platform_driver da9055_hwmon_driver = {
277 .probe = da9055_hwmon_probe,
278 .driver = {
279 .name = "da9055-hwmon",
280 },
281};
282
283module_platform_driver(da9055_hwmon_driver);
284
285MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
286MODULE_DESCRIPTION("DA9055 HWMON driver");
287MODULE_LICENSE("GPL");
288MODULE_ALIAS("platform:da9055-hwmon");
289