2 * TWL6030 GPADC module driver
4 * Copyright (C) 2009-2013 Texas Instruments Inc.
5 * Nishant Kamat <nskamat@ti.com>
6 * Balaji T K <balajitk@ti.com>
7 * Graeme Gregory <gg@slimlogic.co.uk>
8 * Girish S Ghongdemath <girishsg@ti.com>
9 * Ambresh K <ambresh@ti.com>
10 * Oleksandr Kozaruk <oleksandr.kozaruk@ti.com
12 * Based on twl4030-madc.c
13 * Copyright (C) 2008 Nokia Corporation
14 * Mikko Ylinen <mikko.k.ylinen@nokia.com>
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * version 2 as published by the Free Software Foundation.
20 * This program is distributed in the hope that it will be useful, but
21 * WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
31 #include <linux/interrupt.h>
32 #include <linux/kernel.h>
33 #include <linux/module.h>
34 #include <linux/platform_device.h>
35 #include <linux/of_platform.h>
36 #include <linux/mfd/twl.h>
37 #include <linux/iio/iio.h>
38 #include <linux/iio/sysfs.h>
40 #define DRIVER_NAME "twl6030_gpadc"
43 * twl6030 per TRM has 17 channels, and twl6032 has 19 channels
44 * 2 test network channels are not used,
45 * 2 die temperature channels are not used either, as it is not
46 * defined how to convert ADC value to temperature
48 #define TWL6030_GPADC_USED_CHANNELS 13
49 #define TWL6030_GPADC_MAX_CHANNELS 15
50 #define TWL6032_GPADC_USED_CHANNELS 15
51 #define TWL6032_GPADC_MAX_CHANNELS 19
52 #define TWL6030_GPADC_NUM_TRIM_REGS 16
54 #define TWL6030_GPADC_CTRL_P1 0x05
56 #define TWL6032_GPADC_GPSELECT_ISB 0x07
57 #define TWL6032_GPADC_CTRL_P1 0x08
59 #define TWL6032_GPADC_GPCH0_LSB 0x0d
60 #define TWL6032_GPADC_GPCH0_MSB 0x0e
62 #define TWL6030_GPADC_CTRL_P1_SP1 BIT(3)
64 #define TWL6030_GPADC_GPCH0_LSB (0x29)
66 #define TWL6030_GPADC_RT_SW1_EOC_MASK BIT(5)
68 #define TWL6030_GPADC_TRIM1 0xCD
70 #define TWL6030_REG_TOGGLE1 0x90
71 #define TWL6030_GPADCS BIT(1)
72 #define TWL6030_GPADCR BIT(0)
74 #define USB_VBUS_CTRL_SET 0x04
75 #define USB_ID_CTRL_SET 0x06
77 #define TWL6030_MISC1 0xE4
78 #define VBUS_MEAS 0x01
82 #define VBAT_MEAS 0x02
87 * struct twl6030_chnl_calib - channel calibration
88 * @gain: slope coefficient for ideal curve
89 * @gain_error: gain error
90 * @offset_error: offset of the real curve
92 struct twl6030_chnl_calib {
99 * struct twl6030_ideal_code - GPADC calibration parameters
100 * GPADC is calibrated in two points: close to the beginning and
101 * to the and of the measurable input range
103 * @channel: channel number
104 * @code1: ideal code for the input at the beginning
105 * @code2: ideal code for at the end of the range
106 * @volt1: voltage input at the beginning(low voltage)
107 * @volt2: voltage input at the end(high voltage)
109 struct twl6030_ideal_code {
117 struct twl6030_gpadc_data;
120 * struct twl6030_gpadc_platform_data - platform specific data
121 * @nchannels: number of GPADC channels
122 * @iio_channels: iio channels
123 * @twl6030_ideal: pointer to calibration parameters
124 * @start_conversion: pointer to ADC start conversion function
125 * @channel_to_reg pointer to ADC function to convert channel to
126 * register address for reading conversion result
127 * @calibrate: pointer to calibration function
129 struct twl6030_gpadc_platform_data {
131 const struct iio_chan_spec *iio_channels;
132 const struct twl6030_ideal_code *ideal;
133 int (*start_conversion)(int channel);
134 u8 (*channel_to_reg)(int channel);
135 int (*calibrate)(struct twl6030_gpadc_data *gpadc);
139 * struct twl6030_gpadc_data - GPADC data
140 * @dev: device pointer
141 * @lock: mutual exclusion lock for the structure
142 * @irq_complete: completion to signal end of conversion
143 * @twl6030_cal_tbl: pointer to calibration data for each
144 * channel with gain error and offset
145 * @pdata: pointer to device specific data
147 struct twl6030_gpadc_data {
150 struct completion irq_complete;
151 struct twl6030_chnl_calib *twl6030_cal_tbl;
152 const struct twl6030_gpadc_platform_data *pdata;
156 * channels 11, 12, 13, 15 and 16 have no calibration data
157 * calibration offset is same for channels 1, 3, 4, 5
159 * The data is taken from GPADC_TRIM registers description.
160 * GPADC_TRIM registers keep difference between the code measured
161 * at volt1 and volt2 input voltages and corresponding code1 and code2
163 static const struct twl6030_ideal_code
164 twl6030_ideal[TWL6030_GPADC_USED_CHANNELS] = {
165 [0] = { /* ch 0, external, battery type, resistor value */
172 [1] = { /* ch 1, external, battery temperature, NTC resistor value */
179 [2] = { /* ch 2, external, audio accessory/general purpose */
186 [3] = { /* ch 3, external, general purpose */
193 [4] = { /* ch 4, external, temperature measurement/general purpose */
200 [5] = { /* ch 5, external, general purpose */
207 [6] = { /* ch 6, external, general purpose */
214 [7] = { /* ch 7, internal, main battery */
221 [8] = { /* ch 8, internal, backup battery */
228 [9] = { /* ch 9, internal, external charger input */
235 [10] = { /* ch 10, internal, VBUS */
242 [11] = { /* ch 11, internal, VBUS charging current */
245 /* ch 12, internal, Die temperature */
246 /* ch 13, internal, Die temperature */
247 [12] = { /* ch 14, internal, USB ID line */
256 static const struct twl6030_ideal_code
257 twl6032_ideal[TWL6032_GPADC_USED_CHANNELS] = {
258 [0] = { /* ch 0, external, battery type, resistor value */
265 [1] = { /* ch 1, external, battery temperature, NTC resistor value */
272 [2] = { /* ch 2, external, audio accessory/general purpose */
279 [3] = { /* ch 3, external, temperature with external diode/general
287 [4] = { /* ch 4, external, temperature measurement/general purpose */
294 [5] = { /* ch 5, external, general purpose */
301 [6] = { /* ch 6, external, general purpose */
308 [7] = { /* ch7, internal, system supply */
315 [8] = { /* ch8, internal, backup battery */
322 [9] = { /* ch 9, internal, external charger input */
329 [10] = { /* ch10, internal, VBUS */
336 [11] = { /* ch 11, internal, VBUS DC-DC output current */
343 /* ch 12, internal, Die temperature */
344 /* ch 13, internal, Die temperature */
345 [12] = { /* ch 14, internal, USB ID line */
352 /* ch 15, internal, test network */
353 /* ch 16, internal, test network */
354 [13] = { /* ch 17, internal, battery charging current */
357 [14] = { /* ch 18, internal, battery voltage */
366 static inline int twl6030_gpadc_write(u8 reg, u8 val)
368 return twl_i2c_write_u8(TWL6030_MODULE_GPADC, val, reg);
371 static inline int twl6030_gpadc_read(u8 reg, u8 *val)
374 return twl_i2c_read(TWL6030_MODULE_GPADC, val, reg, 2);
377 static int twl6030_gpadc_enable_irq(u8 mask)
381 ret = twl6030_interrupt_unmask(mask, REG_INT_MSK_LINE_B);
385 ret = twl6030_interrupt_unmask(mask, REG_INT_MSK_STS_B);
390 static void twl6030_gpadc_disable_irq(u8 mask)
392 twl6030_interrupt_mask(mask, REG_INT_MSK_LINE_B);
393 twl6030_interrupt_mask(mask, REG_INT_MSK_STS_B);
396 static irqreturn_t twl6030_gpadc_irq_handler(int irq, void *indio_dev)
398 struct twl6030_gpadc_data *gpadc = iio_priv(indio_dev);
400 complete(&gpadc->irq_complete);
405 static int twl6030_start_conversion(int channel)
407 return twl6030_gpadc_write(TWL6030_GPADC_CTRL_P1,
408 TWL6030_GPADC_CTRL_P1_SP1);
411 static int twl6032_start_conversion(int channel)
415 ret = twl6030_gpadc_write(TWL6032_GPADC_GPSELECT_ISB, channel);
419 return twl6030_gpadc_write(TWL6032_GPADC_CTRL_P1,
420 TWL6030_GPADC_CTRL_P1_SP1);
423 static u8 twl6030_channel_to_reg(int channel)
425 return TWL6030_GPADC_GPCH0_LSB + 2 * channel;
428 static u8 twl6032_channel_to_reg(int channel)
431 * for any prior chosen channel, when the conversion is ready
432 * the result is avalable in GPCH0_LSB, GPCH0_MSB.
435 return TWL6032_GPADC_GPCH0_LSB;
438 static int twl6030_gpadc_lookup(const struct twl6030_ideal_code *ideal,
439 int channel, int size)
443 for (i = 0; i < size; i++)
444 if (ideal[i].channel == channel)
450 static int twl6030_channel_calibrated(const struct twl6030_gpadc_platform_data
453 const struct twl6030_ideal_code *ideal = pdata->ideal;
456 i = twl6030_gpadc_lookup(ideal, channel, pdata->nchannels);
457 /* not calibrated channels have 0 in all structure members */
458 return pdata->ideal[i].code2;
461 static int twl6030_gpadc_make_correction(struct twl6030_gpadc_data *gpadc,
462 int channel, int raw_code)
464 const struct twl6030_ideal_code *ideal = gpadc->pdata->ideal;
468 i = twl6030_gpadc_lookup(ideal, channel, gpadc->pdata->nchannels);
469 corrected_code = ((raw_code * 1000) -
470 gpadc->twl6030_cal_tbl[i].offset_error) /
471 gpadc->twl6030_cal_tbl[i].gain_error;
473 return corrected_code;
476 static int twl6030_gpadc_get_raw(struct twl6030_gpadc_data *gpadc,
477 int channel, int *res)
479 u8 reg = gpadc->pdata->channel_to_reg(channel);
484 ret = twl6030_gpadc_read(reg, (u8 *)&val);
486 dev_dbg(gpadc->dev, "unable to read register 0x%X\n", reg);
490 raw_code = le16_to_cpu(val);
491 dev_dbg(gpadc->dev, "GPADC raw code: %d", raw_code);
493 if (twl6030_channel_calibrated(gpadc->pdata, channel))
494 *res = twl6030_gpadc_make_correction(gpadc, channel, raw_code);
501 static int twl6030_gpadc_get_processed(struct twl6030_gpadc_data *gpadc,
502 int channel, int *val)
504 const struct twl6030_ideal_code *ideal = gpadc->pdata->ideal;
510 ret = twl6030_gpadc_get_raw(gpadc, channel, &corrected_code);
514 i = twl6030_gpadc_lookup(ideal, channel, gpadc->pdata->nchannels);
515 channel_value = corrected_code *
516 gpadc->twl6030_cal_tbl[i].gain;
518 /* Shift back into mV range */
519 channel_value /= 1000;
521 dev_dbg(gpadc->dev, "GPADC corrected code: %d", corrected_code);
522 dev_dbg(gpadc->dev, "GPADC value: %d", channel_value);
524 *val = channel_value;
529 static int twl6030_gpadc_read_raw(struct iio_dev *indio_dev,
530 const struct iio_chan_spec *chan,
531 int *val, int *val2, long mask)
533 struct twl6030_gpadc_data *gpadc = iio_priv(indio_dev);
537 mutex_lock(&gpadc->lock);
539 ret = gpadc->pdata->start_conversion(chan->channel);
541 dev_err(gpadc->dev, "failed to start conversion\n");
544 /* wait for conversion to complete */
545 timeout = wait_for_completion_interruptible_timeout(
546 &gpadc->irq_complete, msecs_to_jiffies(5000));
550 } else if (timeout < 0) {
556 case IIO_CHAN_INFO_RAW:
557 ret = twl6030_gpadc_get_raw(gpadc, chan->channel, val);
558 ret = ret ? -EIO : IIO_VAL_INT;
561 case IIO_CHAN_INFO_PROCESSED:
562 ret = twl6030_gpadc_get_processed(gpadc, chan->channel, val);
563 ret = ret ? -EIO : IIO_VAL_INT;
570 mutex_unlock(&gpadc->lock);
576 * The GPADC channels are calibrated using a two point calibration method.
577 * The channels measured with two known values: volt1 and volt2, and
578 * ideal corresponding output codes are known: code1, code2.
579 * The difference(d1, d2) between ideal and measured codes stored in trim
581 * The goal is to find offset and gain of the real curve for each calibrated
583 * gain: k = 1 + ((d2 - d1) / (x2 - x1))
584 * offset: b = d1 + (k - 1) * x1
586 static void twl6030_calibrate_channel(struct twl6030_gpadc_data *gpadc,
587 int channel, int d1, int d2)
589 int b, k, gain, x1, x2, i;
590 const struct twl6030_ideal_code *ideal = gpadc->pdata->ideal;
592 i = twl6030_gpadc_lookup(ideal, channel, gpadc->pdata->nchannels);
595 gain = ((ideal[i].volt2 - ideal[i].volt1) * 1000) /
596 (ideal[i].code2 - ideal[i].code1);
601 /* k - real curve gain */
602 k = 1000 + (((d2 - d1) * 1000) / (x2 - x1));
604 /* b - offset of the real curve gain */
605 b = (d1 * 1000) - (k - 1000) * x1;
607 gpadc->twl6030_cal_tbl[i].gain = gain;
608 gpadc->twl6030_cal_tbl[i].gain_error = k;
609 gpadc->twl6030_cal_tbl[i].offset_error = b;
611 dev_dbg(gpadc->dev, "GPADC d1 for Chn: %d = %d\n", channel, d1);
612 dev_dbg(gpadc->dev, "GPADC d2 for Chn: %d = %d\n", channel, d2);
613 dev_dbg(gpadc->dev, "GPADC x1 for Chn: %d = %d\n", channel, x1);
614 dev_dbg(gpadc->dev, "GPADC x2 for Chn: %d = %d\n", channel, x2);
615 dev_dbg(gpadc->dev, "GPADC Gain for Chn: %d = %d\n", channel, gain);
616 dev_dbg(gpadc->dev, "GPADC k for Chn: %d = %d\n", channel, k);
617 dev_dbg(gpadc->dev, "GPADC b for Chn: %d = %d\n", channel, b);
620 static inline int twl6030_gpadc_get_trim_offset(s8 d)
624 * bit 0 - sign, bit 7 - reserved, 6..1 - trim value
625 * though, the documentation states that trim value
626 * is absolute value, the correct conversion results are
627 * obtained if the value is interpreted as 2's complement.
629 __u32 temp = ((d & 0x7f) >> 1) | ((d & 1) << 6);
631 return sign_extend32(temp, 6);
634 static int twl6030_calibration(struct twl6030_gpadc_data *gpadc)
638 u8 trim_regs[TWL6030_GPADC_NUM_TRIM_REGS];
642 * for calibration two measurements have been performed at
643 * factory, for some channels, during the production test and
644 * have been stored in registers. This two stored values are
645 * used to correct the measurements. The values represent
646 * offsets for the given input from the output on ideal curve.
648 ret = twl_i2c_read(TWL6030_MODULE_ID2, trim_regs,
649 TWL6030_GPADC_TRIM1, TWL6030_GPADC_NUM_TRIM_REGS);
651 dev_err(gpadc->dev, "calibration failed\n");
655 for (chn = 0; chn < TWL6030_GPADC_MAX_CHANNELS; chn++) {
698 d1 = twl6030_gpadc_get_trim_offset(d1);
699 d2 = twl6030_gpadc_get_trim_offset(d2);
701 twl6030_calibrate_channel(gpadc, chn, d1, d2);
707 static int twl6032_get_trim_value(u8 *trim_regs, unsigned int reg0,
708 unsigned int reg1, unsigned int mask0, unsigned int mask1,
713 val = (trim_regs[reg0] & mask0) << shift0;
714 val |= (trim_regs[reg1] & mask1) >> 1;
715 if (trim_regs[reg1] & 0x01)
721 static int twl6032_calibration(struct twl6030_gpadc_data *gpadc)
723 int chn, d1 = 0, d2 = 0, temp;
724 u8 trim_regs[TWL6030_GPADC_NUM_TRIM_REGS];
727 ret = twl_i2c_read(TWL6030_MODULE_ID2, trim_regs,
728 TWL6030_GPADC_TRIM1, TWL6030_GPADC_NUM_TRIM_REGS);
730 dev_err(gpadc->dev, "calibration failed\n");
735 * Loop to calculate the value needed for returning voltages from
738 * gain is calculated to 3 decimal places fixed point.
740 for (chn = 0; chn < TWL6032_GPADC_MAX_CHANNELS; chn++) {
752 d1 = twl6032_get_trim_value(trim_regs, 2, 0, 0x1f,
754 d2 = twl6032_get_trim_value(trim_regs, 3, 1, 0x3f,
758 temp = twl6032_get_trim_value(trim_regs, 2, 0, 0x1f,
760 d1 = temp + twl6032_get_trim_value(trim_regs, 7, 6,
763 temp = twl6032_get_trim_value(trim_regs, 3, 1, 0x3F,
765 d2 = temp + twl6032_get_trim_value(trim_regs, 9, 7,
769 temp = twl6032_get_trim_value(trim_regs, 2, 0, 0x1f,
771 d1 = temp + twl6032_get_trim_value(trim_regs, 13, 11,
774 temp = twl6032_get_trim_value(trim_regs, 3, 1, 0x3f,
776 d2 = temp + twl6032_get_trim_value(trim_regs, 15, 13,
780 d1 = twl6032_get_trim_value(trim_regs, 10, 8, 0x0f,
782 d2 = twl6032_get_trim_value(trim_regs, 14, 12, 0x0f,
787 temp = twl6032_get_trim_value(trim_regs, 2, 0, 0x1f,
790 d1 = (trim_regs[4] & 0x7E) >> 1;
791 if (trim_regs[4] & 0x01)
795 temp = twl6032_get_trim_value(trim_regs, 3, 1, 0x3f,
798 d2 = (trim_regs[5] & 0xFE) >> 1;
799 if (trim_regs[5] & 0x01)
805 /* No data for other channels */
809 twl6030_calibrate_channel(gpadc, chn, d1, d2);
815 #define TWL6030_GPADC_CHAN(chn, _type, chan_info) { \
818 .info_mask_separate = BIT(chan_info), \
822 static const struct iio_chan_spec twl6030_gpadc_iio_channels[] = {
823 TWL6030_GPADC_CHAN(0, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
824 TWL6030_GPADC_CHAN(1, IIO_TEMP, IIO_CHAN_INFO_RAW),
825 TWL6030_GPADC_CHAN(2, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
826 TWL6030_GPADC_CHAN(3, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
827 TWL6030_GPADC_CHAN(4, IIO_TEMP, IIO_CHAN_INFO_RAW),
828 TWL6030_GPADC_CHAN(5, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
829 TWL6030_GPADC_CHAN(6, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
830 TWL6030_GPADC_CHAN(7, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
831 TWL6030_GPADC_CHAN(8, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
832 TWL6030_GPADC_CHAN(9, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
833 TWL6030_GPADC_CHAN(10, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
834 TWL6030_GPADC_CHAN(11, IIO_VOLTAGE, IIO_CHAN_INFO_RAW),
835 TWL6030_GPADC_CHAN(14, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
838 static const struct iio_chan_spec twl6032_gpadc_iio_channels[] = {
839 TWL6030_GPADC_CHAN(0, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
840 TWL6030_GPADC_CHAN(1, IIO_TEMP, IIO_CHAN_INFO_RAW),
841 TWL6030_GPADC_CHAN(2, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
842 TWL6030_GPADC_CHAN(3, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
843 TWL6030_GPADC_CHAN(4, IIO_TEMP, IIO_CHAN_INFO_RAW),
844 TWL6030_GPADC_CHAN(5, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
845 TWL6030_GPADC_CHAN(6, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
846 TWL6030_GPADC_CHAN(7, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
847 TWL6030_GPADC_CHAN(8, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
848 TWL6030_GPADC_CHAN(9, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
849 TWL6030_GPADC_CHAN(10, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
850 TWL6030_GPADC_CHAN(11, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
851 TWL6030_GPADC_CHAN(14, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
852 TWL6030_GPADC_CHAN(17, IIO_VOLTAGE, IIO_CHAN_INFO_RAW),
853 TWL6030_GPADC_CHAN(18, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
856 static const struct iio_info twl6030_gpadc_iio_info = {
857 .read_raw = &twl6030_gpadc_read_raw,
858 .driver_module = THIS_MODULE,
861 static const struct twl6030_gpadc_platform_data twl6030_pdata = {
862 .iio_channels = twl6030_gpadc_iio_channels,
863 .nchannels = TWL6030_GPADC_USED_CHANNELS,
864 .ideal = twl6030_ideal,
865 .start_conversion = twl6030_start_conversion,
866 .channel_to_reg = twl6030_channel_to_reg,
867 .calibrate = twl6030_calibration,
870 static const struct twl6030_gpadc_platform_data twl6032_pdata = {
871 .iio_channels = twl6032_gpadc_iio_channels,
872 .nchannels = TWL6032_GPADC_USED_CHANNELS,
873 .ideal = twl6032_ideal,
874 .start_conversion = twl6032_start_conversion,
875 .channel_to_reg = twl6032_channel_to_reg,
876 .calibrate = twl6032_calibration,
879 static const struct of_device_id of_twl6030_match_tbl[] = {
881 .compatible = "ti,twl6030-gpadc",
882 .data = &twl6030_pdata,
885 .compatible = "ti,twl6032-gpadc",
886 .data = &twl6032_pdata,
890 MODULE_DEVICE_TABLE(of, of_twl6030_match_tbl);
892 static int twl6030_gpadc_probe(struct platform_device *pdev)
894 struct device *dev = &pdev->dev;
895 struct twl6030_gpadc_data *gpadc;
896 const struct twl6030_gpadc_platform_data *pdata;
897 const struct of_device_id *match;
898 struct iio_dev *indio_dev;
902 match = of_match_device(of_twl6030_match_tbl, dev);
908 indio_dev = devm_iio_device_alloc(dev, sizeof(*gpadc));
912 gpadc = iio_priv(indio_dev);
914 gpadc->twl6030_cal_tbl = devm_kzalloc(dev,
915 sizeof(*gpadc->twl6030_cal_tbl) *
916 pdata->nchannels, GFP_KERNEL);
917 if (!gpadc->twl6030_cal_tbl)
921 gpadc->pdata = pdata;
923 platform_set_drvdata(pdev, indio_dev);
924 mutex_init(&gpadc->lock);
925 init_completion(&gpadc->irq_complete);
927 ret = pdata->calibrate(gpadc);
929 dev_err(&pdev->dev, "failed to read calibration registers\n");
933 irq = platform_get_irq(pdev, 0);
935 dev_err(&pdev->dev, "failed to get irq\n");
939 ret = devm_request_threaded_irq(dev, irq, NULL,
940 twl6030_gpadc_irq_handler,
941 IRQF_ONESHOT, "twl6030_gpadc", indio_dev);
945 ret = twl6030_gpadc_enable_irq(TWL6030_GPADC_RT_SW1_EOC_MASK);
947 dev_err(&pdev->dev, "failed to enable GPADC interrupt\n");
951 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, TWL6030_GPADCS,
952 TWL6030_REG_TOGGLE1);
954 dev_err(&pdev->dev, "failed to enable GPADC module\n");
958 ret = twl_i2c_write_u8(TWL_MODULE_USB, VBUS_MEAS, USB_VBUS_CTRL_SET);
960 dev_err(dev, "failed to wire up inputs\n");
964 ret = twl_i2c_write_u8(TWL_MODULE_USB, ID_MEAS, USB_ID_CTRL_SET);
966 dev_err(dev, "failed to wire up inputs\n");
970 ret = twl_i2c_write_u8(TWL6030_MODULE_ID0,
971 VBAT_MEAS | BB_MEAS | BB_MEAS,
974 dev_err(dev, "failed to wire up inputs\n");
978 indio_dev->name = DRIVER_NAME;
979 indio_dev->dev.parent = dev;
980 indio_dev->info = &twl6030_gpadc_iio_info;
981 indio_dev->modes = INDIO_DIRECT_MODE;
982 indio_dev->channels = pdata->iio_channels;
983 indio_dev->num_channels = pdata->nchannels;
985 return iio_device_register(indio_dev);
988 static int twl6030_gpadc_remove(struct platform_device *pdev)
990 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
992 twl6030_gpadc_disable_irq(TWL6030_GPADC_RT_SW1_EOC_MASK);
993 iio_device_unregister(indio_dev);
998 #ifdef CONFIG_PM_SLEEP
999 static int twl6030_gpadc_suspend(struct device *pdev)
1003 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, TWL6030_GPADCR,
1004 TWL6030_REG_TOGGLE1);
1006 dev_err(pdev, "error resetting GPADC (%d)!\n", ret);
1011 static int twl6030_gpadc_resume(struct device *pdev)
1015 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, TWL6030_GPADCS,
1016 TWL6030_REG_TOGGLE1);
1018 dev_err(pdev, "error setting GPADC (%d)!\n", ret);
1024 static SIMPLE_DEV_PM_OPS(twl6030_gpadc_pm_ops, twl6030_gpadc_suspend,
1025 twl6030_gpadc_resume);
1027 static struct platform_driver twl6030_gpadc_driver = {
1028 .probe = twl6030_gpadc_probe,
1029 .remove = twl6030_gpadc_remove,
1031 .name = DRIVER_NAME,
1032 .pm = &twl6030_gpadc_pm_ops,
1033 .of_match_table = of_twl6030_match_tbl,
1037 module_platform_driver(twl6030_gpadc_driver);
1039 MODULE_ALIAS("platform:" DRIVER_NAME);
1040 MODULE_AUTHOR("Balaji T K <balajitk@ti.com>");
1041 MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
1042 MODULE_AUTHOR("Oleksandr Kozaruk <oleksandr.kozaruk@ti.com");
1043 MODULE_DESCRIPTION("twl6030 ADC driver");
1044 MODULE_LICENSE("GPL");