1 // SPDX-License-Identifier: GPL-2.0-only
3 * TWL6030 GPADC module driver
5 * Copyright (C) 2009-2013 Texas Instruments Inc.
6 * Nishant Kamat <nskamat@ti.com>
7 * Balaji T K <balajitk@ti.com>
8 * Graeme Gregory <gg@slimlogic.co.uk>
9 * Girish S Ghongdemath <girishsg@ti.com>
10 * Ambresh K <ambresh@ti.com>
11 * Oleksandr Kozaruk <oleksandr.kozaruk@ti.com
13 * Based on twl4030-madc.c
14 * Copyright (C) 2008 Nokia Corporation
15 * Mikko Ylinen <mikko.k.ylinen@nokia.com>
17 #include <linux/interrupt.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/of_platform.h>
22 #include <linux/mfd/twl.h>
23 #include <linux/iio/iio.h>
24 #include <linux/iio/sysfs.h>
26 #define DRIVER_NAME "twl6030_gpadc"
29 * twl6030 per TRM has 17 channels, and twl6032 has 19 channels
30 * 2 test network channels are not used,
31 * 2 die temperature channels are not used either, as it is not
32 * defined how to convert ADC value to temperature
34 #define TWL6030_GPADC_USED_CHANNELS 13
35 #define TWL6030_GPADC_MAX_CHANNELS 15
36 #define TWL6032_GPADC_USED_CHANNELS 15
37 #define TWL6032_GPADC_MAX_CHANNELS 19
38 #define TWL6030_GPADC_NUM_TRIM_REGS 16
40 #define TWL6030_GPADC_CTRL_P1 0x05
42 #define TWL6032_GPADC_GPSELECT_ISB 0x07
43 #define TWL6032_GPADC_CTRL_P1 0x08
45 #define TWL6032_GPADC_GPCH0_LSB 0x0d
46 #define TWL6032_GPADC_GPCH0_MSB 0x0e
48 #define TWL6030_GPADC_CTRL_P1_SP1 BIT(3)
50 #define TWL6030_GPADC_GPCH0_LSB (0x29)
52 #define TWL6030_GPADC_RT_SW1_EOC_MASK BIT(5)
54 #define TWL6030_GPADC_TRIM1 0xCD
56 #define TWL6030_REG_TOGGLE1 0x90
57 #define TWL6030_GPADCS BIT(1)
58 #define TWL6030_GPADCR BIT(0)
60 #define USB_VBUS_CTRL_SET 0x04
61 #define USB_ID_CTRL_SET 0x06
63 #define TWL6030_MISC1 0xE4
64 #define VBUS_MEAS 0x01
68 #define VBAT_MEAS 0x02
73 * struct twl6030_chnl_calib - channel calibration
74 * @gain: slope coefficient for ideal curve
75 * @gain_error: gain error
76 * @offset_error: offset of the real curve
78 struct twl6030_chnl_calib {
85 * struct twl6030_ideal_code - GPADC calibration parameters
86 * GPADC is calibrated in two points: close to the beginning and
87 * to the and of the measurable input range
89 * @channel: channel number
90 * @code1: ideal code for the input at the beginning
91 * @code2: ideal code for at the end of the range
92 * @volt1: voltage input at the beginning(low voltage)
93 * @volt2: voltage input at the end(high voltage)
95 struct twl6030_ideal_code {
103 struct twl6030_gpadc_data;
106 * struct twl6030_gpadc_platform_data - platform specific data
107 * @nchannels: number of GPADC channels
108 * @iio_channels: iio channels
109 * @ideal: pointer to calibration parameters
110 * @start_conversion: pointer to ADC start conversion function
111 * @channel_to_reg: pointer to ADC function to convert channel to
112 * register address for reading conversion result
113 * @calibrate: pointer to calibration function
115 struct twl6030_gpadc_platform_data {
117 const struct iio_chan_spec *iio_channels;
118 const struct twl6030_ideal_code *ideal;
119 int (*start_conversion)(int channel);
120 u8 (*channel_to_reg)(int channel);
121 int (*calibrate)(struct twl6030_gpadc_data *gpadc);
125 * struct twl6030_gpadc_data - GPADC data
126 * @dev: device pointer
127 * @lock: mutual exclusion lock for the structure
128 * @irq_complete: completion to signal end of conversion
129 * @twl6030_cal_tbl: pointer to calibration data for each
130 * channel with gain error and offset
131 * @pdata: pointer to device specific data
133 struct twl6030_gpadc_data {
136 struct completion irq_complete;
137 struct twl6030_chnl_calib *twl6030_cal_tbl;
138 const struct twl6030_gpadc_platform_data *pdata;
142 * channels 11, 12, 13, 15 and 16 have no calibration data
143 * calibration offset is same for channels 1, 3, 4, 5
145 * The data is taken from GPADC_TRIM registers description.
146 * GPADC_TRIM registers keep difference between the code measured
147 * at volt1 and volt2 input voltages and corresponding code1 and code2
149 static const struct twl6030_ideal_code
150 twl6030_ideal[TWL6030_GPADC_USED_CHANNELS] = {
151 [0] = { /* ch 0, external, battery type, resistor value */
158 [1] = { /* ch 1, external, battery temperature, NTC resistor value */
165 [2] = { /* ch 2, external, audio accessory/general purpose */
172 [3] = { /* ch 3, external, general purpose */
179 [4] = { /* ch 4, external, temperature measurement/general purpose */
186 [5] = { /* ch 5, external, general purpose */
193 [6] = { /* ch 6, external, general purpose */
200 [7] = { /* ch 7, internal, main battery */
207 [8] = { /* ch 8, internal, backup battery */
214 [9] = { /* ch 9, internal, external charger input */
221 [10] = { /* ch 10, internal, VBUS */
228 [11] = { /* ch 11, internal, VBUS charging current */
231 /* ch 12, internal, Die temperature */
232 /* ch 13, internal, Die temperature */
233 [12] = { /* ch 14, internal, USB ID line */
242 static const struct twl6030_ideal_code
243 twl6032_ideal[TWL6032_GPADC_USED_CHANNELS] = {
244 [0] = { /* ch 0, external, battery type, resistor value */
251 [1] = { /* ch 1, external, battery temperature, NTC resistor value */
258 [2] = { /* ch 2, external, audio accessory/general purpose */
265 [3] = { /* ch 3, external, temperature with external diode/general
273 [4] = { /* ch 4, external, temperature measurement/general purpose */
280 [5] = { /* ch 5, external, general purpose */
287 [6] = { /* ch 6, external, general purpose */
294 [7] = { /* ch7, internal, system supply */
301 [8] = { /* ch8, internal, backup battery */
308 [9] = { /* ch 9, internal, external charger input */
315 [10] = { /* ch10, internal, VBUS */
322 [11] = { /* ch 11, internal, VBUS DC-DC output current */
329 /* ch 12, internal, Die temperature */
330 /* ch 13, internal, Die temperature */
331 [12] = { /* ch 14, internal, USB ID line */
338 /* ch 15, internal, test network */
339 /* ch 16, internal, test network */
340 [13] = { /* ch 17, internal, battery charging current */
343 [14] = { /* ch 18, internal, battery voltage */
352 static inline int twl6030_gpadc_write(u8 reg, u8 val)
354 return twl_i2c_write_u8(TWL6030_MODULE_GPADC, val, reg);
357 static inline int twl6030_gpadc_read(u8 reg, u8 *val)
360 return twl_i2c_read(TWL6030_MODULE_GPADC, val, reg, 2);
363 static int twl6030_gpadc_enable_irq(u8 mask)
367 ret = twl6030_interrupt_unmask(mask, REG_INT_MSK_LINE_B);
371 ret = twl6030_interrupt_unmask(mask, REG_INT_MSK_STS_B);
376 static void twl6030_gpadc_disable_irq(u8 mask)
378 twl6030_interrupt_mask(mask, REG_INT_MSK_LINE_B);
379 twl6030_interrupt_mask(mask, REG_INT_MSK_STS_B);
382 static irqreturn_t twl6030_gpadc_irq_handler(int irq, void *indio_dev)
384 struct twl6030_gpadc_data *gpadc = iio_priv(indio_dev);
386 complete(&gpadc->irq_complete);
391 static int twl6030_start_conversion(int channel)
393 return twl6030_gpadc_write(TWL6030_GPADC_CTRL_P1,
394 TWL6030_GPADC_CTRL_P1_SP1);
397 static int twl6032_start_conversion(int channel)
401 ret = twl6030_gpadc_write(TWL6032_GPADC_GPSELECT_ISB, channel);
405 return twl6030_gpadc_write(TWL6032_GPADC_CTRL_P1,
406 TWL6030_GPADC_CTRL_P1_SP1);
409 static u8 twl6030_channel_to_reg(int channel)
411 return TWL6030_GPADC_GPCH0_LSB + 2 * channel;
414 static u8 twl6032_channel_to_reg(int channel)
417 * for any prior chosen channel, when the conversion is ready
418 * the result is avalable in GPCH0_LSB, GPCH0_MSB.
421 return TWL6032_GPADC_GPCH0_LSB;
424 static int twl6030_gpadc_lookup(const struct twl6030_ideal_code *ideal,
425 int channel, int size)
429 for (i = 0; i < size; i++)
430 if (ideal[i].channel == channel)
436 static int twl6030_channel_calibrated(const struct twl6030_gpadc_platform_data
439 const struct twl6030_ideal_code *ideal = pdata->ideal;
442 i = twl6030_gpadc_lookup(ideal, channel, pdata->nchannels);
443 /* not calibrated channels have 0 in all structure members */
444 return pdata->ideal[i].code2;
447 static int twl6030_gpadc_make_correction(struct twl6030_gpadc_data *gpadc,
448 int channel, int raw_code)
450 const struct twl6030_ideal_code *ideal = gpadc->pdata->ideal;
454 i = twl6030_gpadc_lookup(ideal, channel, gpadc->pdata->nchannels);
455 corrected_code = ((raw_code * 1000) -
456 gpadc->twl6030_cal_tbl[i].offset_error) /
457 gpadc->twl6030_cal_tbl[i].gain_error;
459 return corrected_code;
462 static int twl6030_gpadc_get_raw(struct twl6030_gpadc_data *gpadc,
463 int channel, int *res)
465 u8 reg = gpadc->pdata->channel_to_reg(channel);
470 ret = twl6030_gpadc_read(reg, (u8 *)&val);
472 dev_dbg(gpadc->dev, "unable to read register 0x%X\n", reg);
476 raw_code = le16_to_cpu(val);
477 dev_dbg(gpadc->dev, "GPADC raw code: %d", raw_code);
479 if (twl6030_channel_calibrated(gpadc->pdata, channel))
480 *res = twl6030_gpadc_make_correction(gpadc, channel, raw_code);
487 static int twl6030_gpadc_get_processed(struct twl6030_gpadc_data *gpadc,
488 int channel, int *val)
490 const struct twl6030_ideal_code *ideal = gpadc->pdata->ideal;
496 ret = twl6030_gpadc_get_raw(gpadc, channel, &corrected_code);
500 i = twl6030_gpadc_lookup(ideal, channel, gpadc->pdata->nchannels);
501 channel_value = corrected_code *
502 gpadc->twl6030_cal_tbl[i].gain;
504 /* Shift back into mV range */
505 channel_value /= 1000;
507 dev_dbg(gpadc->dev, "GPADC corrected code: %d", corrected_code);
508 dev_dbg(gpadc->dev, "GPADC value: %d", channel_value);
510 *val = channel_value;
515 static int twl6030_gpadc_read_raw(struct iio_dev *indio_dev,
516 const struct iio_chan_spec *chan,
517 int *val, int *val2, long mask)
519 struct twl6030_gpadc_data *gpadc = iio_priv(indio_dev);
523 mutex_lock(&gpadc->lock);
525 ret = gpadc->pdata->start_conversion(chan->channel);
527 dev_err(gpadc->dev, "failed to start conversion\n");
530 /* wait for conversion to complete */
531 timeout = wait_for_completion_interruptible_timeout(
532 &gpadc->irq_complete, msecs_to_jiffies(5000));
536 } else if (timeout < 0) {
542 case IIO_CHAN_INFO_RAW:
543 ret = twl6030_gpadc_get_raw(gpadc, chan->channel, val);
544 ret = ret ? -EIO : IIO_VAL_INT;
547 case IIO_CHAN_INFO_PROCESSED:
548 ret = twl6030_gpadc_get_processed(gpadc, chan->channel, val);
549 ret = ret ? -EIO : IIO_VAL_INT;
556 mutex_unlock(&gpadc->lock);
562 * The GPADC channels are calibrated using a two point calibration method.
563 * The channels measured with two known values: volt1 and volt2, and
564 * ideal corresponding output codes are known: code1, code2.
565 * The difference(d1, d2) between ideal and measured codes stored in trim
567 * The goal is to find offset and gain of the real curve for each calibrated
569 * gain: k = 1 + ((d2 - d1) / (x2 - x1))
570 * offset: b = d1 + (k - 1) * x1
572 static void twl6030_calibrate_channel(struct twl6030_gpadc_data *gpadc,
573 int channel, int d1, int d2)
575 int b, k, gain, x1, x2, i;
576 const struct twl6030_ideal_code *ideal = gpadc->pdata->ideal;
578 i = twl6030_gpadc_lookup(ideal, channel, gpadc->pdata->nchannels);
581 gain = ((ideal[i].volt2 - ideal[i].volt1) * 1000) /
582 (ideal[i].code2 - ideal[i].code1);
587 /* k - real curve gain */
588 k = 1000 + (((d2 - d1) * 1000) / (x2 - x1));
590 /* b - offset of the real curve gain */
591 b = (d1 * 1000) - (k - 1000) * x1;
593 gpadc->twl6030_cal_tbl[i].gain = gain;
594 gpadc->twl6030_cal_tbl[i].gain_error = k;
595 gpadc->twl6030_cal_tbl[i].offset_error = b;
597 dev_dbg(gpadc->dev, "GPADC d1 for Chn: %d = %d\n", channel, d1);
598 dev_dbg(gpadc->dev, "GPADC d2 for Chn: %d = %d\n", channel, d2);
599 dev_dbg(gpadc->dev, "GPADC x1 for Chn: %d = %d\n", channel, x1);
600 dev_dbg(gpadc->dev, "GPADC x2 for Chn: %d = %d\n", channel, x2);
601 dev_dbg(gpadc->dev, "GPADC Gain for Chn: %d = %d\n", channel, gain);
602 dev_dbg(gpadc->dev, "GPADC k for Chn: %d = %d\n", channel, k);
603 dev_dbg(gpadc->dev, "GPADC b for Chn: %d = %d\n", channel, b);
606 static inline int twl6030_gpadc_get_trim_offset(s8 d)
610 * bit 0 - sign, bit 7 - reserved, 6..1 - trim value
611 * though, the documentation states that trim value
612 * is absolute value, the correct conversion results are
613 * obtained if the value is interpreted as 2's complement.
615 __u32 temp = ((d & 0x7f) >> 1) | ((d & 1) << 6);
617 return sign_extend32(temp, 6);
620 static int twl6030_calibration(struct twl6030_gpadc_data *gpadc)
624 u8 trim_regs[TWL6030_GPADC_NUM_TRIM_REGS];
628 * for calibration two measurements have been performed at
629 * factory, for some channels, during the production test and
630 * have been stored in registers. This two stored values are
631 * used to correct the measurements. The values represent
632 * offsets for the given input from the output on ideal curve.
634 ret = twl_i2c_read(TWL6030_MODULE_ID2, trim_regs,
635 TWL6030_GPADC_TRIM1, TWL6030_GPADC_NUM_TRIM_REGS);
637 dev_err(gpadc->dev, "calibration failed\n");
641 for (chn = 0; chn < TWL6030_GPADC_MAX_CHANNELS; chn++) {
684 d1 = twl6030_gpadc_get_trim_offset(d1);
685 d2 = twl6030_gpadc_get_trim_offset(d2);
687 twl6030_calibrate_channel(gpadc, chn, d1, d2);
693 static int twl6032_get_trim_value(u8 *trim_regs, unsigned int reg0,
694 unsigned int reg1, unsigned int mask0, unsigned int mask1,
699 val = (trim_regs[reg0] & mask0) << shift0;
700 val |= (trim_regs[reg1] & mask1) >> 1;
701 if (trim_regs[reg1] & 0x01)
707 static int twl6032_calibration(struct twl6030_gpadc_data *gpadc)
709 int chn, d1 = 0, d2 = 0, temp;
710 u8 trim_regs[TWL6030_GPADC_NUM_TRIM_REGS];
713 ret = twl_i2c_read(TWL6030_MODULE_ID2, trim_regs,
714 TWL6030_GPADC_TRIM1, TWL6030_GPADC_NUM_TRIM_REGS);
716 dev_err(gpadc->dev, "calibration failed\n");
721 * Loop to calculate the value needed for returning voltages from
724 * gain is calculated to 3 decimal places fixed point.
726 for (chn = 0; chn < TWL6032_GPADC_MAX_CHANNELS; chn++) {
738 d1 = twl6032_get_trim_value(trim_regs, 2, 0, 0x1f,
740 d2 = twl6032_get_trim_value(trim_regs, 3, 1, 0x3f,
744 temp = twl6032_get_trim_value(trim_regs, 2, 0, 0x1f,
746 d1 = temp + twl6032_get_trim_value(trim_regs, 7, 6,
749 temp = twl6032_get_trim_value(trim_regs, 3, 1, 0x3F,
751 d2 = temp + twl6032_get_trim_value(trim_regs, 9, 7,
755 temp = twl6032_get_trim_value(trim_regs, 2, 0, 0x1f,
757 d1 = temp + twl6032_get_trim_value(trim_regs, 13, 11,
760 temp = twl6032_get_trim_value(trim_regs, 3, 1, 0x3f,
762 d2 = temp + twl6032_get_trim_value(trim_regs, 15, 13,
766 d1 = twl6032_get_trim_value(trim_regs, 10, 8, 0x0f,
768 d2 = twl6032_get_trim_value(trim_regs, 14, 12, 0x0f,
773 temp = twl6032_get_trim_value(trim_regs, 2, 0, 0x1f,
776 d1 = (trim_regs[4] & 0x7E) >> 1;
777 if (trim_regs[4] & 0x01)
781 temp = twl6032_get_trim_value(trim_regs, 3, 1, 0x3f,
784 d2 = (trim_regs[5] & 0xFE) >> 1;
785 if (trim_regs[5] & 0x01)
791 /* No data for other channels */
795 twl6030_calibrate_channel(gpadc, chn, d1, d2);
801 #define TWL6030_GPADC_CHAN(chn, _type, chan_info) { \
804 .info_mask_separate = BIT(chan_info), \
808 static const struct iio_chan_spec twl6030_gpadc_iio_channels[] = {
809 TWL6030_GPADC_CHAN(0, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
810 TWL6030_GPADC_CHAN(1, IIO_TEMP, IIO_CHAN_INFO_RAW),
811 TWL6030_GPADC_CHAN(2, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
812 TWL6030_GPADC_CHAN(3, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
813 TWL6030_GPADC_CHAN(4, IIO_TEMP, IIO_CHAN_INFO_RAW),
814 TWL6030_GPADC_CHAN(5, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
815 TWL6030_GPADC_CHAN(6, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
816 TWL6030_GPADC_CHAN(7, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
817 TWL6030_GPADC_CHAN(8, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
818 TWL6030_GPADC_CHAN(9, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
819 TWL6030_GPADC_CHAN(10, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
820 TWL6030_GPADC_CHAN(11, IIO_VOLTAGE, IIO_CHAN_INFO_RAW),
821 TWL6030_GPADC_CHAN(14, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
824 static const struct iio_chan_spec twl6032_gpadc_iio_channels[] = {
825 TWL6030_GPADC_CHAN(0, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
826 TWL6030_GPADC_CHAN(1, IIO_TEMP, IIO_CHAN_INFO_RAW),
827 TWL6030_GPADC_CHAN(2, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
828 TWL6030_GPADC_CHAN(3, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
829 TWL6030_GPADC_CHAN(4, IIO_TEMP, IIO_CHAN_INFO_RAW),
830 TWL6030_GPADC_CHAN(5, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
831 TWL6030_GPADC_CHAN(6, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
832 TWL6030_GPADC_CHAN(7, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
833 TWL6030_GPADC_CHAN(8, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
834 TWL6030_GPADC_CHAN(9, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
835 TWL6030_GPADC_CHAN(10, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
836 TWL6030_GPADC_CHAN(11, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
837 TWL6030_GPADC_CHAN(14, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
838 TWL6030_GPADC_CHAN(17, IIO_VOLTAGE, IIO_CHAN_INFO_RAW),
839 TWL6030_GPADC_CHAN(18, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
842 static const struct iio_info twl6030_gpadc_iio_info = {
843 .read_raw = &twl6030_gpadc_read_raw,
846 static const struct twl6030_gpadc_platform_data twl6030_pdata = {
847 .iio_channels = twl6030_gpadc_iio_channels,
848 .nchannels = TWL6030_GPADC_USED_CHANNELS,
849 .ideal = twl6030_ideal,
850 .start_conversion = twl6030_start_conversion,
851 .channel_to_reg = twl6030_channel_to_reg,
852 .calibrate = twl6030_calibration,
855 static const struct twl6030_gpadc_platform_data twl6032_pdata = {
856 .iio_channels = twl6032_gpadc_iio_channels,
857 .nchannels = TWL6032_GPADC_USED_CHANNELS,
858 .ideal = twl6032_ideal,
859 .start_conversion = twl6032_start_conversion,
860 .channel_to_reg = twl6032_channel_to_reg,
861 .calibrate = twl6032_calibration,
864 static const struct of_device_id of_twl6030_match_tbl[] = {
866 .compatible = "ti,twl6030-gpadc",
867 .data = &twl6030_pdata,
870 .compatible = "ti,twl6032-gpadc",
871 .data = &twl6032_pdata,
875 MODULE_DEVICE_TABLE(of, of_twl6030_match_tbl);
877 static int twl6030_gpadc_probe(struct platform_device *pdev)
879 struct device *dev = &pdev->dev;
880 struct twl6030_gpadc_data *gpadc;
881 const struct twl6030_gpadc_platform_data *pdata;
882 const struct of_device_id *match;
883 struct iio_dev *indio_dev;
887 match = of_match_device(of_twl6030_match_tbl, dev);
893 indio_dev = devm_iio_device_alloc(dev, sizeof(*gpadc));
897 gpadc = iio_priv(indio_dev);
899 gpadc->twl6030_cal_tbl = devm_kcalloc(dev,
901 sizeof(*gpadc->twl6030_cal_tbl),
903 if (!gpadc->twl6030_cal_tbl)
907 gpadc->pdata = pdata;
909 platform_set_drvdata(pdev, indio_dev);
910 mutex_init(&gpadc->lock);
911 init_completion(&gpadc->irq_complete);
913 ret = pdata->calibrate(gpadc);
915 dev_err(dev, "failed to read calibration registers\n");
919 irq = platform_get_irq(pdev, 0);
923 ret = devm_request_threaded_irq(dev, irq, NULL,
924 twl6030_gpadc_irq_handler,
925 IRQF_ONESHOT, "twl6030_gpadc", indio_dev);
929 ret = twl6030_gpadc_enable_irq(TWL6030_GPADC_RT_SW1_EOC_MASK);
931 dev_err(dev, "failed to enable GPADC interrupt\n");
935 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, TWL6030_GPADCS,
936 TWL6030_REG_TOGGLE1);
938 dev_err(dev, "failed to enable GPADC module\n");
942 ret = twl_i2c_write_u8(TWL_MODULE_USB, VBUS_MEAS, USB_VBUS_CTRL_SET);
944 dev_err(dev, "failed to wire up inputs\n");
948 ret = twl_i2c_write_u8(TWL_MODULE_USB, ID_MEAS, USB_ID_CTRL_SET);
950 dev_err(dev, "failed to wire up inputs\n");
954 ret = twl_i2c_write_u8(TWL6030_MODULE_ID0,
955 VBAT_MEAS | BB_MEAS | VAC_MEAS,
958 dev_err(dev, "failed to wire up inputs\n");
962 indio_dev->name = DRIVER_NAME;
963 indio_dev->info = &twl6030_gpadc_iio_info;
964 indio_dev->modes = INDIO_DIRECT_MODE;
965 indio_dev->channels = pdata->iio_channels;
966 indio_dev->num_channels = pdata->nchannels;
968 return iio_device_register(indio_dev);
971 static int twl6030_gpadc_remove(struct platform_device *pdev)
973 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
975 twl6030_gpadc_disable_irq(TWL6030_GPADC_RT_SW1_EOC_MASK);
976 iio_device_unregister(indio_dev);
981 static int twl6030_gpadc_suspend(struct device *pdev)
985 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, TWL6030_GPADCR,
986 TWL6030_REG_TOGGLE1);
988 dev_err(pdev, "error resetting GPADC (%d)!\n", ret);
993 static int twl6030_gpadc_resume(struct device *pdev)
997 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, TWL6030_GPADCS,
998 TWL6030_REG_TOGGLE1);
1000 dev_err(pdev, "error setting GPADC (%d)!\n", ret);
1005 static DEFINE_SIMPLE_DEV_PM_OPS(twl6030_gpadc_pm_ops, twl6030_gpadc_suspend,
1006 twl6030_gpadc_resume);
1008 static struct platform_driver twl6030_gpadc_driver = {
1009 .probe = twl6030_gpadc_probe,
1010 .remove = twl6030_gpadc_remove,
1012 .name = DRIVER_NAME,
1013 .pm = pm_sleep_ptr(&twl6030_gpadc_pm_ops),
1014 .of_match_table = of_twl6030_match_tbl,
1018 module_platform_driver(twl6030_gpadc_driver);
1020 MODULE_ALIAS("platform:" DRIVER_NAME);
1021 MODULE_AUTHOR("Balaji T K <balajitk@ti.com>");
1022 MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
1023 MODULE_AUTHOR("Oleksandr Kozaruk <oleksandr.kozaruk@ti.com");
1024 MODULE_DESCRIPTION("twl6030 ADC driver");
1025 MODULE_LICENSE("GPL");