4 * Copyright (c) 2014 Dialog Semiconductor
6 * Author: Adam Thomson <Adam.Thomson.Opensource@diasemi.com>
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.
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>
28 enum 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_
,
63 enum 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
,
79 struct da9150
*da9150
;
83 struct completion complete
;
87 static irqreturn_t
da9150_gpadc_irq(int irq
, void *data
)
90 struct da9150_gpadc
*gpadc
= data
;
92 complete(&gpadc
->complete
);
97 static int da9150_gpadc_read_adc(struct da9150_gpadc
*gpadc
, int hw_chan
)
102 mutex_lock(&gpadc
->lock
);
104 /* Set channel & enable measurement */
105 da9150_reg_write(gpadc
->da9150
, DA9150_GPADC_MAN
,
106 (DA9150_GPADC_EN_MASK
|
107 hw_chan
<< DA9150_GPADC_MUX_SHIFT
));
109 /* Consume left-over completion from a previous timeout */
110 try_wait_for_completion(&gpadc
->complete
);
112 /* Check for actual completion */
113 wait_for_completion_timeout(&gpadc
->complete
, msecs_to_jiffies(5));
115 /* Read result and status from device */
116 da9150_bulk_read(gpadc
->da9150
, DA9150_GPADC_RES_A
, 2, result_regs
);
118 mutex_unlock(&gpadc
->lock
);
120 /* Check to make sure device really has completed reading */
121 if (result_regs
[1] & DA9150_GPADC_RUN_MASK
) {
122 dev_err(gpadc
->dev
, "Timeout on channel %d of GPADC\n",
128 result
= (result_regs
[1] & DA9150_GPADC_RES_L_MASK
) >>
129 DA9150_GPADC_RES_L_SHIFT
;
131 result
|= result_regs
[0] << DA9150_GPADC_RES_L_BITS
;
136 static inline int da9150_gpadc_gpio_6v_voltage_now(int raw_val
)
139 return (6 * ((raw_val
* 1000) + 500)) / 1024;
142 static inline int da9150_gpadc_ibus_current_avg(int raw_val
)
145 return (4 * ((raw_val
* 1000) + 500)) / 2048;
148 static inline int da9150_gpadc_vbus_21v_voltage_now(int raw_val
)
151 return (21 * ((raw_val
* 1000) + 500)) / 1024;
154 static inline int da9150_gpadc_vsys_6v_voltage_now(int raw_val
)
157 return (3 * ((raw_val
* 1000) + 500)) / 512;
160 static int da9150_gpadc_read_processed(struct da9150_gpadc
*gpadc
, int channel
,
161 int hw_chan
, int *val
)
165 raw_val
= da9150_gpadc_read_adc(gpadc
, hw_chan
);
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
);
176 case DA9150_GPADC_CHAN_IBUS
:
177 *val
= da9150_gpadc_ibus_current_avg(raw_val
);
179 case DA9150_GPADC_CHAN_VBUS
:
180 *val
= da9150_gpadc_vbus_21v_voltage_now(raw_val
);
182 case DA9150_GPADC_CHAN_VSYS
:
183 *val
= da9150_gpadc_vsys_6v_voltage_now(raw_val
);
186 /* No processing for other channels so return raw value */
194 static int da9150_gpadc_read_scale(int channel
, int *val
, int *val2
)
197 case DA9150_GPADC_CHAN_VBAT
:
200 return IIO_VAL_FRACTIONAL
;
201 case DA9150_GPADC_CHAN_TJUNC_CORE
:
202 case DA9150_GPADC_CHAN_TJUNC_OVP
:
205 return IIO_VAL_FRACTIONAL
;
211 static int da9150_gpadc_read_offset(int channel
, int *val
)
214 case DA9150_GPADC_CHAN_VBAT
:
215 *val
= 1500000 / 2932;
217 case DA9150_GPADC_CHAN_TJUNC_CORE
:
218 case DA9150_GPADC_CHAN_TJUNC_OVP
:
226 static int da9150_gpadc_read_raw(struct iio_dev
*indio_dev
,
227 struct iio_chan_spec
const *chan
,
228 int *val
, int *val2
, long mask
)
230 struct da9150_gpadc
*gpadc
= iio_priv(indio_dev
);
232 if ((chan
->channel
< DA9150_GPADC_CHAN_GPIOA
) ||
233 (chan
->channel
> DA9150_GPADC_CHAN_TJUNC_OVP
))
237 case IIO_CHAN_INFO_RAW
:
238 case IIO_CHAN_INFO_PROCESSED
:
239 return da9150_gpadc_read_processed(gpadc
, chan
->channel
,
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
);
250 static const struct iio_info da9150_gpadc_info
= {
251 .read_raw
= &da9150_gpadc_read_raw
,
254 #define DA9150_GPADC_CHANNEL(_id, _hw_id, _type, chan_info, \
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, \
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)
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), \
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)
280 /* Supported channels */
281 static 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
,
293 DA9150_GPADC_CHANNEL_SCALED(TJUNC_OVP
, TJUNC_OVP
, IIO_TEMP
,
297 /* Default maps used by da9150-charger */
298 static struct iio_map da9150_gpadc_default_maps
[] = {
300 .consumer_dev_name
= "da9150-charger",
301 .consumer_channel
= "CHAN_IBUS",
302 .adc_channel_label
= "IBUS",
305 .consumer_dev_name
= "da9150-charger",
306 .consumer_channel
= "CHAN_VBUS",
307 .adc_channel_label
= "VBUS",
310 .consumer_dev_name
= "da9150-charger",
311 .consumer_channel
= "CHAN_TJUNC",
312 .adc_channel_label
= "TJUNC_CORE",
315 .consumer_dev_name
= "da9150-charger",
316 .consumer_channel
= "CHAN_VBAT",
317 .adc_channel_label
= "VBAT",
322 static int da9150_gpadc_probe(struct platform_device
*pdev
)
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
;
330 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*gpadc
));
332 dev_err(&pdev
->dev
, "Failed to allocate IIO device\n");
335 gpadc
= iio_priv(indio_dev
);
337 platform_set_drvdata(pdev
, indio_dev
);
338 gpadc
->da9150
= da9150
;
340 mutex_init(&gpadc
->lock
);
341 init_completion(&gpadc
->complete
);
343 irq
= platform_get_irq_byname(pdev
, "GPADC");
345 dev_err(dev
, "Failed to get IRQ: %d\n", irq
);
349 ret
= devm_request_threaded_irq(dev
, irq
, NULL
, da9150_gpadc_irq
,
350 IRQF_ONESHOT
, "GPADC", gpadc
);
352 dev_err(dev
, "Failed to request IRQ %d: %d\n", irq
, ret
);
356 ret
= iio_map_array_register(indio_dev
, da9150_gpadc_default_maps
);
358 dev_err(dev
, "Failed to register IIO maps: %d\n", ret
);
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
);
370 ret
= iio_device_register(indio_dev
);
372 dev_err(dev
, "Failed to register IIO device: %d\n", ret
);
379 iio_map_array_unregister(indio_dev
);
384 static int da9150_gpadc_remove(struct platform_device
*pdev
)
386 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
388 iio_device_unregister(indio_dev
);
389 iio_map_array_unregister(indio_dev
);
394 static struct platform_driver da9150_gpadc_driver
= {
396 .name
= "da9150-gpadc",
398 .probe
= da9150_gpadc_probe
,
399 .remove
= da9150_gpadc_remove
,
402 module_platform_driver(da9150_gpadc_driver
);
404 MODULE_DESCRIPTION("GPADC Driver for DA9150");
405 MODULE_AUTHOR("Adam Thomson <Adam.Thomson.Opensource@diasemi.com>");
406 MODULE_LICENSE("GPL");