1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <linux/kernel.h>
15#include <linux/slab.h>
16#include <linux/module.h>
17#include <linux/platform_device.h>
18#include <linux/interrupt.h>
19#include <linux/mutex.h>
20#include <linux/completion.h>
21#include <linux/iio/iio.h>
22#include <linux/iio/machine.h>
23#include <linux/iio/driver.h>
24#include <linux/mfd/da9150/core.h>
25#include <linux/mfd/da9150/registers.h>
26
27
28enum da9150_gpadc_hw_channel {
29 DA9150_GPADC_HW_CHAN_GPIOA_2V = 0,
30 DA9150_GPADC_HW_CHAN_GPIOA_2V_,
31 DA9150_GPADC_HW_CHAN_GPIOB_2V,
32 DA9150_GPADC_HW_CHAN_GPIOB_2V_,
33 DA9150_GPADC_HW_CHAN_GPIOC_2V,
34 DA9150_GPADC_HW_CHAN_GPIOC_2V_,
35 DA9150_GPADC_HW_CHAN_GPIOD_2V,
36 DA9150_GPADC_HW_CHAN_GPIOD_2V_,
37 DA9150_GPADC_HW_CHAN_IBUS_SENSE,
38 DA9150_GPADC_HW_CHAN_IBUS_SENSE_,
39 DA9150_GPADC_HW_CHAN_VBUS_DIV,
40 DA9150_GPADC_HW_CHAN_VBUS_DIV_,
41 DA9150_GPADC_HW_CHAN_ID,
42 DA9150_GPADC_HW_CHAN_ID_,
43 DA9150_GPADC_HW_CHAN_VSYS,
44 DA9150_GPADC_HW_CHAN_VSYS_,
45 DA9150_GPADC_HW_CHAN_GPIOA_6V,
46 DA9150_GPADC_HW_CHAN_GPIOA_6V_,
47 DA9150_GPADC_HW_CHAN_GPIOB_6V,
48 DA9150_GPADC_HW_CHAN_GPIOB_6V_,
49 DA9150_GPADC_HW_CHAN_GPIOC_6V,
50 DA9150_GPADC_HW_CHAN_GPIOC_6V_,
51 DA9150_GPADC_HW_CHAN_GPIOD_6V,
52 DA9150_GPADC_HW_CHAN_GPIOD_6V_,
53 DA9150_GPADC_HW_CHAN_VBAT,
54 DA9150_GPADC_HW_CHAN_VBAT_,
55 DA9150_GPADC_HW_CHAN_TBAT,
56 DA9150_GPADC_HW_CHAN_TBAT_,
57 DA9150_GPADC_HW_CHAN_TJUNC_CORE,
58 DA9150_GPADC_HW_CHAN_TJUNC_CORE_,
59 DA9150_GPADC_HW_CHAN_TJUNC_OVP,
60 DA9150_GPADC_HW_CHAN_TJUNC_OVP_,
61};
62
63enum da9150_gpadc_channel {
64 DA9150_GPADC_CHAN_GPIOA = 0,
65 DA9150_GPADC_CHAN_GPIOB,
66 DA9150_GPADC_CHAN_GPIOC,
67 DA9150_GPADC_CHAN_GPIOD,
68 DA9150_GPADC_CHAN_IBUS,
69 DA9150_GPADC_CHAN_VBUS,
70 DA9150_GPADC_CHAN_VSYS,
71 DA9150_GPADC_CHAN_VBAT,
72 DA9150_GPADC_CHAN_TBAT,
73 DA9150_GPADC_CHAN_TJUNC_CORE,
74 DA9150_GPADC_CHAN_TJUNC_OVP,
75};
76
77
78struct da9150_gpadc {
79 struct da9150 *da9150;
80 struct device *dev;
81
82 struct mutex lock;
83 struct completion complete;
84};
85
86
87static irqreturn_t da9150_gpadc_irq(int irq, void *data)
88{
89
90 struct da9150_gpadc *gpadc = data;
91
92 complete(&gpadc->complete);
93
94 return IRQ_HANDLED;
95}
96
97static int da9150_gpadc_read_adc(struct da9150_gpadc *gpadc, int hw_chan)
98{
99 u8 result_regs[2];
100 int result;
101
102 mutex_lock(&gpadc->lock);
103
104
105 da9150_reg_write(gpadc->da9150, DA9150_GPADC_MAN,
106 (DA9150_GPADC_EN_MASK |
107 hw_chan << DA9150_GPADC_MUX_SHIFT));
108
109
110 try_wait_for_completion(&gpadc->complete);
111
112
113 wait_for_completion_timeout(&gpadc->complete, msecs_to_jiffies(5));
114
115
116 da9150_bulk_read(gpadc->da9150, DA9150_GPADC_RES_A, 2, result_regs);
117
118 mutex_unlock(&gpadc->lock);
119
120
121 if (result_regs[1] & DA9150_GPADC_RUN_MASK) {
122 dev_err(gpadc->dev, "Timeout on channel %d of GPADC\n",
123 hw_chan);
124 return -ETIMEDOUT;
125 }
126
127
128 result = (result_regs[1] & DA9150_GPADC_RES_L_MASK) >>
129 DA9150_GPADC_RES_L_SHIFT;
130
131 result |= result_regs[0] << DA9150_GPADC_RES_L_BITS;
132
133 return result;
134}
135
136static inline int da9150_gpadc_gpio_6v_voltage_now(int raw_val)
137{
138
139 return (6 * ((raw_val * 1000) + 500)) / 1024;
140}
141
142static inline int da9150_gpadc_ibus_current_avg(int raw_val)
143{
144
145 return (4 * ((raw_val * 1000) + 500)) / 2048;
146}
147
148static inline int da9150_gpadc_vbus_21v_voltage_now(int raw_val)
149{
150
151 return (21 * ((raw_val * 1000) + 500)) / 1024;
152}
153
154static inline int da9150_gpadc_vsys_6v_voltage_now(int raw_val)
155{
156
157 return (3 * ((raw_val * 1000) + 500)) / 512;
158}
159
160static int da9150_gpadc_read_processed(struct da9150_gpadc *gpadc, int channel,
161 int hw_chan, int *val)
162{
163 int raw_val;
164
165 raw_val = da9150_gpadc_read_adc(gpadc, hw_chan);
166 if (raw_val < 0)
167 return raw_val;
168
169 switch (channel) {
170 case DA9150_GPADC_CHAN_GPIOA:
171 case DA9150_GPADC_CHAN_GPIOB:
172 case DA9150_GPADC_CHAN_GPIOC:
173 case DA9150_GPADC_CHAN_GPIOD:
174 *val = da9150_gpadc_gpio_6v_voltage_now(raw_val);
175 break;
176 case DA9150_GPADC_CHAN_IBUS:
177 *val = da9150_gpadc_ibus_current_avg(raw_val);
178 break;
179 case DA9150_GPADC_CHAN_VBUS:
180 *val = da9150_gpadc_vbus_21v_voltage_now(raw_val);
181 break;
182 case DA9150_GPADC_CHAN_VSYS:
183 *val = da9150_gpadc_vsys_6v_voltage_now(raw_val);
184 break;
185 default:
186
187 *val = raw_val;
188 break;
189 }
190
191 return IIO_VAL_INT;
192}
193
194static int da9150_gpadc_read_scale(int channel, int *val, int *val2)
195{
196 switch (channel) {
197 case DA9150_GPADC_CHAN_VBAT:
198 *val = 2932;
199 *val2 = 1000;
200 return IIO_VAL_FRACTIONAL;
201 case DA9150_GPADC_CHAN_TJUNC_CORE:
202 case DA9150_GPADC_CHAN_TJUNC_OVP:
203 *val = 1000000;
204 *val2 = 4420;
205 return IIO_VAL_FRACTIONAL;
206 default:
207 return -EINVAL;
208 }
209}
210
211static int da9150_gpadc_read_offset(int channel, int *val)
212{
213 switch (channel) {
214 case DA9150_GPADC_CHAN_VBAT:
215 *val = 1500000 / 2932;
216 return IIO_VAL_INT;
217 case DA9150_GPADC_CHAN_TJUNC_CORE:
218 case DA9150_GPADC_CHAN_TJUNC_OVP:
219 *val = -144;
220 return IIO_VAL_INT;
221 default:
222 return -EINVAL;
223 }
224}
225
226static int da9150_gpadc_read_raw(struct iio_dev *indio_dev,
227 struct iio_chan_spec const *chan,
228 int *val, int *val2, long mask)
229{
230 struct da9150_gpadc *gpadc = iio_priv(indio_dev);
231
232 if ((chan->channel < DA9150_GPADC_CHAN_GPIOA) ||
233 (chan->channel > DA9150_GPADC_CHAN_TJUNC_OVP))
234 return -EINVAL;
235
236 switch (mask) {
237 case IIO_CHAN_INFO_RAW:
238 case IIO_CHAN_INFO_PROCESSED:
239 return da9150_gpadc_read_processed(gpadc, chan->channel,
240 chan->address, val);
241 case IIO_CHAN_INFO_SCALE:
242 return da9150_gpadc_read_scale(chan->channel, val, val2);
243 case IIO_CHAN_INFO_OFFSET:
244 return da9150_gpadc_read_offset(chan->channel, val);
245 default:
246 return -EINVAL;
247 }
248}
249
250static const struct iio_info da9150_gpadc_info = {
251 .read_raw = &da9150_gpadc_read_raw,
252};
253
254#define DA9150_GPADC_CHANNEL(_id, _hw_id, _type, chan_info, \
255 _ext_name) { \
256 .type = _type, \
257 .indexed = 1, \
258 .channel = DA9150_GPADC_CHAN_##_id, \
259 .address = DA9150_GPADC_HW_CHAN_##_hw_id, \
260 .info_mask_separate = chan_info, \
261 .extend_name = _ext_name, \
262 .datasheet_name = #_id, \
263}
264
265#define DA9150_GPADC_CHANNEL_RAW(_id, _hw_id, _type, _ext_name) \
266 DA9150_GPADC_CHANNEL(_id, _hw_id, _type, \
267 BIT(IIO_CHAN_INFO_RAW), _ext_name)
268
269#define DA9150_GPADC_CHANNEL_SCALED(_id, _hw_id, _type, _ext_name) \
270 DA9150_GPADC_CHANNEL(_id, _hw_id, _type, \
271 BIT(IIO_CHAN_INFO_RAW) | \
272 BIT(IIO_CHAN_INFO_SCALE) | \
273 BIT(IIO_CHAN_INFO_OFFSET), \
274 _ext_name)
275
276#define DA9150_GPADC_CHANNEL_PROCESSED(_id, _hw_id, _type, _ext_name) \
277 DA9150_GPADC_CHANNEL(_id, _hw_id, _type, \
278 BIT(IIO_CHAN_INFO_PROCESSED), _ext_name)
279
280
281static const struct iio_chan_spec da9150_gpadc_channels[] = {
282 DA9150_GPADC_CHANNEL_PROCESSED(GPIOA, GPIOA_6V, IIO_VOLTAGE, NULL),
283 DA9150_GPADC_CHANNEL_PROCESSED(GPIOB, GPIOB_6V, IIO_VOLTAGE, NULL),
284 DA9150_GPADC_CHANNEL_PROCESSED(GPIOC, GPIOC_6V, IIO_VOLTAGE, NULL),
285 DA9150_GPADC_CHANNEL_PROCESSED(GPIOD, GPIOD_6V, IIO_VOLTAGE, NULL),
286 DA9150_GPADC_CHANNEL_PROCESSED(IBUS, IBUS_SENSE, IIO_CURRENT, "ibus"),
287 DA9150_GPADC_CHANNEL_PROCESSED(VBUS, VBUS_DIV_, IIO_VOLTAGE, "vbus"),
288 DA9150_GPADC_CHANNEL_PROCESSED(VSYS, VSYS, IIO_VOLTAGE, "vsys"),
289 DA9150_GPADC_CHANNEL_SCALED(VBAT, VBAT, IIO_VOLTAGE, "vbat"),
290 DA9150_GPADC_CHANNEL_RAW(TBAT, TBAT, IIO_VOLTAGE, "tbat"),
291 DA9150_GPADC_CHANNEL_SCALED(TJUNC_CORE, TJUNC_CORE, IIO_TEMP,
292 "tjunc_core"),
293 DA9150_GPADC_CHANNEL_SCALED(TJUNC_OVP, TJUNC_OVP, IIO_TEMP,
294 "tjunc_ovp"),
295};
296
297
298static struct iio_map da9150_gpadc_default_maps[] = {
299 {
300 .consumer_dev_name = "da9150-charger",
301 .consumer_channel = "CHAN_IBUS",
302 .adc_channel_label = "IBUS",
303 },
304 {
305 .consumer_dev_name = "da9150-charger",
306 .consumer_channel = "CHAN_VBUS",
307 .adc_channel_label = "VBUS",
308 },
309 {
310 .consumer_dev_name = "da9150-charger",
311 .consumer_channel = "CHAN_TJUNC",
312 .adc_channel_label = "TJUNC_CORE",
313 },
314 {
315 .consumer_dev_name = "da9150-charger",
316 .consumer_channel = "CHAN_VBAT",
317 .adc_channel_label = "VBAT",
318 },
319 {},
320};
321
322static int da9150_gpadc_probe(struct platform_device *pdev)
323{
324 struct device *dev = &pdev->dev;
325 struct da9150 *da9150 = dev_get_drvdata(dev->parent);
326 struct da9150_gpadc *gpadc;
327 struct iio_dev *indio_dev;
328 int irq, ret;
329
330 indio_dev = devm_iio_device_alloc(dev, sizeof(*gpadc));
331 if (!indio_dev) {
332 dev_err(&pdev->dev, "Failed to allocate IIO device\n");
333 return -ENOMEM;
334 }
335 gpadc = iio_priv(indio_dev);
336
337 platform_set_drvdata(pdev, indio_dev);
338 gpadc->da9150 = da9150;
339 gpadc->dev = dev;
340 mutex_init(&gpadc->lock);
341 init_completion(&gpadc->complete);
342
343 irq = platform_get_irq_byname(pdev, "GPADC");
344 if (irq < 0) {
345 dev_err(dev, "Failed to get IRQ: %d\n", irq);
346 return irq;
347 }
348
349 ret = devm_request_threaded_irq(dev, irq, NULL, da9150_gpadc_irq,
350 IRQF_ONESHOT, "GPADC", gpadc);
351 if (ret) {
352 dev_err(dev, "Failed to request IRQ %d: %d\n", irq, ret);
353 return ret;
354 }
355
356 ret = iio_map_array_register(indio_dev, da9150_gpadc_default_maps);
357 if (ret) {
358 dev_err(dev, "Failed to register IIO maps: %d\n", ret);
359 return ret;
360 }
361
362 indio_dev->name = dev_name(dev);
363 indio_dev->dev.parent = dev;
364 indio_dev->dev.of_node = pdev->dev.of_node;
365 indio_dev->info = &da9150_gpadc_info;
366 indio_dev->modes = INDIO_DIRECT_MODE;
367 indio_dev->channels = da9150_gpadc_channels;
368 indio_dev->num_channels = ARRAY_SIZE(da9150_gpadc_channels);
369
370 ret = iio_device_register(indio_dev);
371 if (ret) {
372 dev_err(dev, "Failed to register IIO device: %d\n", ret);
373 goto iio_map_unreg;
374 }
375
376 return 0;
377
378iio_map_unreg:
379 iio_map_array_unregister(indio_dev);
380
381 return ret;
382}
383
384static int da9150_gpadc_remove(struct platform_device *pdev)
385{
386 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
387
388 iio_device_unregister(indio_dev);
389 iio_map_array_unregister(indio_dev);
390
391 return 0;
392}
393
394static struct platform_driver da9150_gpadc_driver = {
395 .driver = {
396 .name = "da9150-gpadc",
397 },
398 .probe = da9150_gpadc_probe,
399 .remove = da9150_gpadc_remove,
400};
401
402module_platform_driver(da9150_gpadc_driver);
403
404MODULE_DESCRIPTION("GPADC Driver for DA9150");
405MODULE_AUTHOR("Adam Thomson <Adam.Thomson.Opensource@diasemi.com>");
406MODULE_LICENSE("GPL");
407