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/mod_devicetable.h>
20 #include <linux/module.h>
21 #include <linux/platform_device.h>
22 #include <linux/property.h>
23 #include <linux/mfd/twl.h>
24 #include <linux/iio/iio.h>
25 #include <linux/iio/sysfs.h>
27 #define DRIVER_NAME "twl6030_gpadc"
30 * twl6030 per TRM has 17 channels, and twl6032 has 19 channels
31 * 2 test network channels are not used,
32 * 2 die temperature channels are not used either, as it is not
33 * defined how to convert ADC value to temperature
35 #define TWL6030_GPADC_USED_CHANNELS 13
36 #define TWL6030_GPADC_MAX_CHANNELS 15
37 #define TWL6032_GPADC_USED_CHANNELS 15
38 #define TWL6032_GPADC_MAX_CHANNELS 19
39 #define TWL6030_GPADC_NUM_TRIM_REGS 16
41 #define TWL6030_GPADC_CTRL_P1 0x05
43 #define TWL6032_GPADC_GPSELECT_ISB 0x07
44 #define TWL6032_GPADC_CTRL_P1 0x08
46 #define TWL6032_GPADC_GPCH0_LSB 0x0d
47 #define TWL6032_GPADC_GPCH0_MSB 0x0e
49 #define TWL6030_GPADC_CTRL_P1_SP1 BIT(3)
51 #define TWL6030_GPADC_GPCH0_LSB (0x29)
53 #define TWL6030_GPADC_RT_SW1_EOC_MASK BIT(5)
55 #define TWL6030_GPADC_TRIM1 0xCD
57 #define TWL6030_REG_TOGGLE1 0x90
58 #define TWL6030_GPADCS BIT(1)
59 #define TWL6030_GPADCR BIT(0)
61 #define USB_VBUS_CTRL_SET 0x04
62 #define USB_ID_CTRL_SET 0x06
64 #define TWL6030_MISC1 0xE4
65 #define VBUS_MEAS 0x01
69 #define VBAT_MEAS 0x02
74 * struct twl6030_chnl_calib - channel calibration
75 * @gain: slope coefficient for ideal curve
76 * @gain_error: gain error
77 * @offset_error: offset of the real curve
79 struct twl6030_chnl_calib {
86 * struct twl6030_ideal_code - GPADC calibration parameters
87 * GPADC is calibrated in two points: close to the beginning and
88 * to the and of the measurable input range
90 * @channel: channel number
91 * @code1: ideal code for the input at the beginning
92 * @code2: ideal code for at the end of the range
93 * @volt1: voltage input at the beginning(low voltage)
94 * @volt2: voltage input at the end(high voltage)
96 struct twl6030_ideal_code {
104 struct twl6030_gpadc_data;
107 * struct twl6030_gpadc_platform_data - platform specific data
108 * @nchannels: number of GPADC channels
109 * @iio_channels: iio channels
110 * @ideal: pointer to calibration parameters
111 * @start_conversion: pointer to ADC start conversion function
112 * @channel_to_reg: pointer to ADC function to convert channel to
113 * register address for reading conversion result
114 * @calibrate: pointer to calibration function
116 struct twl6030_gpadc_platform_data {
118 const struct iio_chan_spec *iio_channels;
119 const struct twl6030_ideal_code *ideal;
120 int (*start_conversion)(int channel);
121 u8 (*channel_to_reg)(int channel);
122 int (*calibrate)(struct twl6030_gpadc_data *gpadc);
126 * struct twl6030_gpadc_data - GPADC data
127 * @dev: device pointer
128 * @lock: mutual exclusion lock for the structure
129 * @irq_complete: completion to signal end of conversion
130 * @twl6030_cal_tbl: pointer to calibration data for each
131 * channel with gain error and offset
132 * @pdata: pointer to device specific data
134 struct twl6030_gpadc_data {
137 struct completion irq_complete;
138 struct twl6030_chnl_calib *twl6030_cal_tbl;
139 const struct twl6030_gpadc_platform_data *pdata;
143 * channels 11, 12, 13, 15 and 16 have no calibration data
144 * calibration offset is same for channels 1, 3, 4, 5
146 * The data is taken from GPADC_TRIM registers description.
147 * GPADC_TRIM registers keep difference between the code measured
148 * at volt1 and volt2 input voltages and corresponding code1 and code2
150 static const struct twl6030_ideal_code
151 twl6030_ideal[TWL6030_GPADC_USED_CHANNELS] = {
152 [0] = { /* ch 0, external, battery type, resistor value */
159 [1] = { /* ch 1, external, battery temperature, NTC resistor value */
166 [2] = { /* ch 2, external, audio accessory/general purpose */
173 [3] = { /* ch 3, external, general purpose */
180 [4] = { /* ch 4, external, temperature measurement/general purpose */
187 [5] = { /* ch 5, external, general purpose */
194 [6] = { /* ch 6, external, general purpose */
201 [7] = { /* ch 7, internal, main battery */
208 [8] = { /* ch 8, internal, backup battery */
215 [9] = { /* ch 9, internal, external charger input */
222 [10] = { /* ch 10, internal, VBUS */
229 [11] = { /* ch 11, internal, VBUS charging current */
232 /* ch 12, internal, Die temperature */
233 /* ch 13, internal, Die temperature */
234 [12] = { /* ch 14, internal, USB ID line */
243 static const struct twl6030_ideal_code
244 twl6032_ideal[TWL6032_GPADC_USED_CHANNELS] = {
245 [0] = { /* ch 0, external, battery type, resistor value */
252 [1] = { /* ch 1, external, battery temperature, NTC resistor value */
259 [2] = { /* ch 2, external, audio accessory/general purpose */
266 [3] = { /* ch 3, external, temperature with external diode/general
274 [4] = { /* ch 4, external, temperature measurement/general purpose */
281 [5] = { /* ch 5, external, general purpose */
288 [6] = { /* ch 6, external, general purpose */
295 [7] = { /* ch7, internal, system supply */
302 [8] = { /* ch8, internal, backup battery */
309 [9] = { /* ch 9, internal, external charger input */
316 [10] = { /* ch10, internal, VBUS */
323 [11] = { /* ch 11, internal, VBUS DC-DC output current */
330 /* ch 12, internal, Die temperature */
331 /* ch 13, internal, Die temperature */
332 [12] = { /* ch 14, internal, USB ID line */
339 /* ch 15, internal, test network */
340 /* ch 16, internal, test network */
341 [13] = { /* ch 17, internal, battery charging current */
344 [14] = { /* ch 18, internal, battery voltage */
353 static inline int twl6030_gpadc_write(u8 reg, u8 val)
355 return twl_i2c_write_u8(TWL6030_MODULE_GPADC, val, reg);
358 static inline int twl6030_gpadc_read(u8 reg, u8 *val)
361 return twl_i2c_read(TWL6030_MODULE_GPADC, val, reg, 2);
364 static int twl6030_gpadc_enable_irq(u8 mask)
368 ret = twl6030_interrupt_unmask(mask, REG_INT_MSK_LINE_B);
372 ret = twl6030_interrupt_unmask(mask, REG_INT_MSK_STS_B);
377 static void twl6030_gpadc_disable_irq(u8 mask)
379 twl6030_interrupt_mask(mask, REG_INT_MSK_LINE_B);
380 twl6030_interrupt_mask(mask, REG_INT_MSK_STS_B);
383 static irqreturn_t twl6030_gpadc_irq_handler(int irq, void *indio_dev)
385 struct twl6030_gpadc_data *gpadc = iio_priv(indio_dev);
387 complete(&gpadc->irq_complete);
392 static int twl6030_start_conversion(int channel)
394 return twl6030_gpadc_write(TWL6030_GPADC_CTRL_P1,
395 TWL6030_GPADC_CTRL_P1_SP1);
398 static int twl6032_start_conversion(int channel)
402 ret = twl6030_gpadc_write(TWL6032_GPADC_GPSELECT_ISB, channel);
406 return twl6030_gpadc_write(TWL6032_GPADC_CTRL_P1,
407 TWL6030_GPADC_CTRL_P1_SP1);
410 static u8 twl6030_channel_to_reg(int channel)
412 return TWL6030_GPADC_GPCH0_LSB + 2 * channel;
415 static u8 twl6032_channel_to_reg(int channel)
418 * for any prior chosen channel, when the conversion is ready
419 * the result is avalable in GPCH0_LSB, GPCH0_MSB.
422 return TWL6032_GPADC_GPCH0_LSB;
425 static int twl6030_gpadc_lookup(const struct twl6030_ideal_code *ideal,
426 int channel, int size)
430 for (i = 0; i < size; i++)
431 if (ideal[i].channel == channel)
437 static int twl6030_channel_calibrated(const struct twl6030_gpadc_platform_data
440 const struct twl6030_ideal_code *ideal = pdata->ideal;
443 i = twl6030_gpadc_lookup(ideal, channel, pdata->nchannels);
444 /* not calibrated channels have 0 in all structure members */
445 return pdata->ideal[i].code2;
448 static int twl6030_gpadc_make_correction(struct twl6030_gpadc_data *gpadc,
449 int channel, int raw_code)
451 const struct twl6030_ideal_code *ideal = gpadc->pdata->ideal;
455 i = twl6030_gpadc_lookup(ideal, channel, gpadc->pdata->nchannels);
456 corrected_code = ((raw_code * 1000) -
457 gpadc->twl6030_cal_tbl[i].offset_error) /
458 gpadc->twl6030_cal_tbl[i].gain_error;
460 return corrected_code;
463 static int twl6030_gpadc_get_raw(struct twl6030_gpadc_data *gpadc,
464 int channel, int *res)
466 u8 reg = gpadc->pdata->channel_to_reg(channel);
471 ret = twl6030_gpadc_read(reg, (u8 *)&val);
473 dev_dbg(gpadc->dev, "unable to read register 0x%X\n", reg);
477 raw_code = le16_to_cpu(val);
478 dev_dbg(gpadc->dev, "GPADC raw code: %d", raw_code);
480 if (twl6030_channel_calibrated(gpadc->pdata, channel))
481 *res = twl6030_gpadc_make_correction(gpadc, channel, raw_code);
488 static int twl6030_gpadc_get_processed(struct twl6030_gpadc_data *gpadc,
489 int channel, int *val)
491 const struct twl6030_ideal_code *ideal = gpadc->pdata->ideal;
497 ret = twl6030_gpadc_get_raw(gpadc, channel, &corrected_code);
501 i = twl6030_gpadc_lookup(ideal, channel, gpadc->pdata->nchannels);
502 channel_value = corrected_code *
503 gpadc->twl6030_cal_tbl[i].gain;
505 /* Shift back into mV range */
506 channel_value /= 1000;
508 dev_dbg(gpadc->dev, "GPADC corrected code: %d", corrected_code);
509 dev_dbg(gpadc->dev, "GPADC value: %d", channel_value);
511 *val = channel_value;
516 static int twl6030_gpadc_read_raw(struct iio_dev *indio_dev,
517 const struct iio_chan_spec *chan,
518 int *val, int *val2, long mask)
520 struct twl6030_gpadc_data *gpadc = iio_priv(indio_dev);
524 mutex_lock(&gpadc->lock);
526 ret = gpadc->pdata->start_conversion(chan->channel);
528 dev_err(gpadc->dev, "failed to start conversion\n");
531 /* wait for conversion to complete */
532 timeout = wait_for_completion_interruptible_timeout(
533 &gpadc->irq_complete, msecs_to_jiffies(5000));
537 } else if (timeout < 0) {
543 case IIO_CHAN_INFO_RAW:
544 ret = twl6030_gpadc_get_raw(gpadc, chan->channel, val);
545 ret = ret ? -EIO : IIO_VAL_INT;
548 case IIO_CHAN_INFO_PROCESSED:
549 ret = twl6030_gpadc_get_processed(gpadc, chan->channel, val);
550 ret = ret ? -EIO : IIO_VAL_INT;
557 mutex_unlock(&gpadc->lock);
563 * The GPADC channels are calibrated using a two point calibration method.
564 * The channels measured with two known values: volt1 and volt2, and
565 * ideal corresponding output codes are known: code1, code2.
566 * The difference(d1, d2) between ideal and measured codes stored in trim
568 * The goal is to find offset and gain of the real curve for each calibrated
570 * gain: k = 1 + ((d2 - d1) / (x2 - x1))
571 * offset: b = d1 + (k - 1) * x1
573 static void twl6030_calibrate_channel(struct twl6030_gpadc_data *gpadc,
574 int channel, int d1, int d2)
576 int b, k, gain, x1, x2, i;
577 const struct twl6030_ideal_code *ideal = gpadc->pdata->ideal;
579 i = twl6030_gpadc_lookup(ideal, channel, gpadc->pdata->nchannels);
582 gain = ((ideal[i].volt2 - ideal[i].volt1) * 1000) /
583 (ideal[i].code2 - ideal[i].code1);
588 /* k - real curve gain */
589 k = 1000 + (((d2 - d1) * 1000) / (x2 - x1));
591 /* b - offset of the real curve gain */
592 b = (d1 * 1000) - (k - 1000) * x1;
594 gpadc->twl6030_cal_tbl[i].gain = gain;
595 gpadc->twl6030_cal_tbl[i].gain_error = k;
596 gpadc->twl6030_cal_tbl[i].offset_error = b;
598 dev_dbg(gpadc->dev, "GPADC d1 for Chn: %d = %d\n", channel, d1);
599 dev_dbg(gpadc->dev, "GPADC d2 for Chn: %d = %d\n", channel, d2);
600 dev_dbg(gpadc->dev, "GPADC x1 for Chn: %d = %d\n", channel, x1);
601 dev_dbg(gpadc->dev, "GPADC x2 for Chn: %d = %d\n", channel, x2);
602 dev_dbg(gpadc->dev, "GPADC Gain for Chn: %d = %d\n", channel, gain);
603 dev_dbg(gpadc->dev, "GPADC k for Chn: %d = %d\n", channel, k);
604 dev_dbg(gpadc->dev, "GPADC b for Chn: %d = %d\n", channel, b);
607 static inline int twl6030_gpadc_get_trim_offset(s8 d)
611 * bit 0 - sign, bit 7 - reserved, 6..1 - trim value
612 * though, the documentation states that trim value
613 * is absolute value, the correct conversion results are
614 * obtained if the value is interpreted as 2's complement.
616 __u32 temp = ((d & 0x7f) >> 1) | ((d & 1) << 6);
618 return sign_extend32(temp, 6);
621 static int twl6030_calibration(struct twl6030_gpadc_data *gpadc)
625 u8 trim_regs[TWL6030_GPADC_NUM_TRIM_REGS];
629 * for calibration two measurements have been performed at
630 * factory, for some channels, during the production test and
631 * have been stored in registers. This two stored values are
632 * used to correct the measurements. The values represent
633 * offsets for the given input from the output on ideal curve.
635 ret = twl_i2c_read(TWL6030_MODULE_ID2, trim_regs,
636 TWL6030_GPADC_TRIM1, TWL6030_GPADC_NUM_TRIM_REGS);
638 dev_err(gpadc->dev, "calibration failed\n");
642 for (chn = 0; chn < TWL6030_GPADC_MAX_CHANNELS; chn++) {
685 d1 = twl6030_gpadc_get_trim_offset(d1);
686 d2 = twl6030_gpadc_get_trim_offset(d2);
688 twl6030_calibrate_channel(gpadc, chn, d1, d2);
694 static int twl6032_get_trim_value(u8 *trim_regs, unsigned int reg0,
695 unsigned int reg1, unsigned int mask0, unsigned int mask1,
700 val = (trim_regs[reg0] & mask0) << shift0;
701 val |= (trim_regs[reg1] & mask1) >> 1;
702 if (trim_regs[reg1] & 0x01)
708 static int twl6032_calibration(struct twl6030_gpadc_data *gpadc)
710 int chn, d1 = 0, d2 = 0, temp;
711 u8 trim_regs[TWL6030_GPADC_NUM_TRIM_REGS];
714 ret = twl_i2c_read(TWL6030_MODULE_ID2, trim_regs,
715 TWL6030_GPADC_TRIM1, TWL6030_GPADC_NUM_TRIM_REGS);
717 dev_err(gpadc->dev, "calibration failed\n");
722 * Loop to calculate the value needed for returning voltages from
725 * gain is calculated to 3 decimal places fixed point.
727 for (chn = 0; chn < TWL6032_GPADC_MAX_CHANNELS; chn++) {
739 d1 = twl6032_get_trim_value(trim_regs, 2, 0, 0x1f,
741 d2 = twl6032_get_trim_value(trim_regs, 3, 1, 0x3f,
745 temp = twl6032_get_trim_value(trim_regs, 2, 0, 0x1f,
747 d1 = temp + twl6032_get_trim_value(trim_regs, 7, 6,
750 temp = twl6032_get_trim_value(trim_regs, 3, 1, 0x3F,
752 d2 = temp + twl6032_get_trim_value(trim_regs, 9, 7,
756 temp = twl6032_get_trim_value(trim_regs, 2, 0, 0x1f,
758 d1 = temp + twl6032_get_trim_value(trim_regs, 13, 11,
761 temp = twl6032_get_trim_value(trim_regs, 3, 1, 0x3f,
763 d2 = temp + twl6032_get_trim_value(trim_regs, 15, 13,
767 d1 = twl6032_get_trim_value(trim_regs, 10, 8, 0x0f,
769 d2 = twl6032_get_trim_value(trim_regs, 14, 12, 0x0f,
774 temp = twl6032_get_trim_value(trim_regs, 2, 0, 0x1f,
777 d1 = (trim_regs[4] & 0x7E) >> 1;
778 if (trim_regs[4] & 0x01)
782 temp = twl6032_get_trim_value(trim_regs, 3, 1, 0x3f,
785 d2 = (trim_regs[5] & 0xFE) >> 1;
786 if (trim_regs[5] & 0x01)
792 /* No data for other channels */
796 twl6030_calibrate_channel(gpadc, chn, d1, d2);
802 #define TWL6030_GPADC_CHAN(chn, _type, chan_info) { \
805 .info_mask_separate = BIT(chan_info), \
809 static const struct iio_chan_spec twl6030_gpadc_iio_channels[] = {
810 TWL6030_GPADC_CHAN(0, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
811 TWL6030_GPADC_CHAN(1, IIO_TEMP, IIO_CHAN_INFO_RAW),
812 TWL6030_GPADC_CHAN(2, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
813 TWL6030_GPADC_CHAN(3, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
814 TWL6030_GPADC_CHAN(4, IIO_TEMP, IIO_CHAN_INFO_RAW),
815 TWL6030_GPADC_CHAN(5, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
816 TWL6030_GPADC_CHAN(6, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
817 TWL6030_GPADC_CHAN(7, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
818 TWL6030_GPADC_CHAN(8, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
819 TWL6030_GPADC_CHAN(9, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
820 TWL6030_GPADC_CHAN(10, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
821 TWL6030_GPADC_CHAN(11, IIO_VOLTAGE, IIO_CHAN_INFO_RAW),
822 TWL6030_GPADC_CHAN(14, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
825 static const struct iio_chan_spec twl6032_gpadc_iio_channels[] = {
826 TWL6030_GPADC_CHAN(0, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
827 TWL6030_GPADC_CHAN(1, IIO_TEMP, IIO_CHAN_INFO_RAW),
828 TWL6030_GPADC_CHAN(2, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
829 TWL6030_GPADC_CHAN(3, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
830 TWL6030_GPADC_CHAN(4, IIO_TEMP, IIO_CHAN_INFO_RAW),
831 TWL6030_GPADC_CHAN(5, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
832 TWL6030_GPADC_CHAN(6, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
833 TWL6030_GPADC_CHAN(7, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
834 TWL6030_GPADC_CHAN(8, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
835 TWL6030_GPADC_CHAN(9, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
836 TWL6030_GPADC_CHAN(10, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
837 TWL6030_GPADC_CHAN(11, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
838 TWL6030_GPADC_CHAN(14, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
839 TWL6030_GPADC_CHAN(17, IIO_VOLTAGE, IIO_CHAN_INFO_RAW),
840 TWL6030_GPADC_CHAN(18, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
843 static const struct iio_info twl6030_gpadc_iio_info = {
844 .read_raw = &twl6030_gpadc_read_raw,
847 static const struct twl6030_gpadc_platform_data twl6030_pdata = {
848 .iio_channels = twl6030_gpadc_iio_channels,
849 .nchannels = TWL6030_GPADC_USED_CHANNELS,
850 .ideal = twl6030_ideal,
851 .start_conversion = twl6030_start_conversion,
852 .channel_to_reg = twl6030_channel_to_reg,
853 .calibrate = twl6030_calibration,
856 static const struct twl6030_gpadc_platform_data twl6032_pdata = {
857 .iio_channels = twl6032_gpadc_iio_channels,
858 .nchannels = TWL6032_GPADC_USED_CHANNELS,
859 .ideal = twl6032_ideal,
860 .start_conversion = twl6032_start_conversion,
861 .channel_to_reg = twl6032_channel_to_reg,
862 .calibrate = twl6032_calibration,
865 static const struct of_device_id of_twl6030_match_tbl[] = {
867 .compatible = "ti,twl6030-gpadc",
868 .data = &twl6030_pdata,
871 .compatible = "ti,twl6032-gpadc",
872 .data = &twl6032_pdata,
876 MODULE_DEVICE_TABLE(of, of_twl6030_match_tbl);
878 static int twl6030_gpadc_probe(struct platform_device *pdev)
880 struct device *dev = &pdev->dev;
881 struct twl6030_gpadc_data *gpadc;
882 const struct twl6030_gpadc_platform_data *pdata;
883 struct iio_dev *indio_dev;
887 pdata = device_get_match_data(&pdev->dev);
891 indio_dev = devm_iio_device_alloc(dev, sizeof(*gpadc));
895 gpadc = iio_priv(indio_dev);
897 gpadc->twl6030_cal_tbl = devm_kcalloc(dev,
899 sizeof(*gpadc->twl6030_cal_tbl),
901 if (!gpadc->twl6030_cal_tbl)
905 gpadc->pdata = pdata;
907 platform_set_drvdata(pdev, indio_dev);
908 mutex_init(&gpadc->lock);
909 init_completion(&gpadc->irq_complete);
911 ret = pdata->calibrate(gpadc);
913 dev_err(dev, "failed to read calibration registers\n");
917 irq = platform_get_irq(pdev, 0);
921 ret = devm_request_threaded_irq(dev, irq, NULL,
922 twl6030_gpadc_irq_handler,
923 IRQF_ONESHOT, "twl6030_gpadc", indio_dev);
927 ret = twl6030_gpadc_enable_irq(TWL6030_GPADC_RT_SW1_EOC_MASK);
929 dev_err(dev, "failed to enable GPADC interrupt\n");
933 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, TWL6030_GPADCS,
934 TWL6030_REG_TOGGLE1);
936 dev_err(dev, "failed to enable GPADC module\n");
940 ret = twl_i2c_write_u8(TWL_MODULE_USB, VBUS_MEAS, USB_VBUS_CTRL_SET);
942 dev_err(dev, "failed to wire up inputs\n");
946 ret = twl_i2c_write_u8(TWL_MODULE_USB, ID_MEAS, USB_ID_CTRL_SET);
948 dev_err(dev, "failed to wire up inputs\n");
952 ret = twl_i2c_write_u8(TWL6030_MODULE_ID0,
953 VBAT_MEAS | BB_MEAS | VAC_MEAS,
956 dev_err(dev, "failed to wire up inputs\n");
960 indio_dev->name = DRIVER_NAME;
961 indio_dev->info = &twl6030_gpadc_iio_info;
962 indio_dev->modes = INDIO_DIRECT_MODE;
963 indio_dev->channels = pdata->iio_channels;
964 indio_dev->num_channels = pdata->nchannels;
966 return iio_device_register(indio_dev);
969 static void twl6030_gpadc_remove(struct platform_device *pdev)
971 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
973 twl6030_gpadc_disable_irq(TWL6030_GPADC_RT_SW1_EOC_MASK);
974 iio_device_unregister(indio_dev);
977 static int twl6030_gpadc_suspend(struct device *pdev)
981 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, TWL6030_GPADCR,
982 TWL6030_REG_TOGGLE1);
984 dev_err(pdev, "error resetting GPADC (%d)!\n", ret);
989 static int twl6030_gpadc_resume(struct device *pdev)
993 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, TWL6030_GPADCS,
994 TWL6030_REG_TOGGLE1);
996 dev_err(pdev, "error setting GPADC (%d)!\n", ret);
1001 static DEFINE_SIMPLE_DEV_PM_OPS(twl6030_gpadc_pm_ops, twl6030_gpadc_suspend,
1002 twl6030_gpadc_resume);
1004 static struct platform_driver twl6030_gpadc_driver = {
1005 .probe = twl6030_gpadc_probe,
1006 .remove_new = twl6030_gpadc_remove,
1008 .name = DRIVER_NAME,
1009 .pm = pm_sleep_ptr(&twl6030_gpadc_pm_ops),
1010 .of_match_table = of_twl6030_match_tbl,
1014 module_platform_driver(twl6030_gpadc_driver);
1016 MODULE_ALIAS("platform:" DRIVER_NAME);
1017 MODULE_AUTHOR("Balaji T K <balajitk@ti.com>");
1018 MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
1019 MODULE_AUTHOR("Oleksandr Kozaruk <oleksandr.kozaruk@ti.com");
1020 MODULE_DESCRIPTION("twl6030 ADC driver");
1021 MODULE_LICENSE("GPL");