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)
75 * struct twl6030_chnl_calib - channel calibration
76 * @gain: slope coefficient for ideal curve
77 * @gain_error: gain error
78 * @offset_error: offset of the real curve
80 struct twl6030_chnl_calib {
87 * struct twl6030_ideal_code - GPADC calibration parameters
88 * GPADC is calibrated in two points: close to the beginning and
89 * to the and of the measurable input range
91 * @channel: channel number
92 * @code1: ideal code for the input at the beginning
93 * @code2: ideal code for at the end of the range
94 * @volt1: voltage input at the beginning(low voltage)
95 * @volt2: voltage input at the end(high voltage)
97 struct twl6030_ideal_code {
105 struct twl6030_gpadc_data;
108 * struct twl6030_gpadc_platform_data - platform specific data
109 * @nchannels: number of GPADC channels
110 * @iio_channels: iio channels
111 * @twl6030_ideal: pointer to calibration parameters
112 * @start_conversion: pointer to ADC start conversion function
113 * @channel_to_reg pointer to ADC function to convert channel to
114 * register address for reading conversion result
115 * @calibrate: pointer to calibration function
117 struct twl6030_gpadc_platform_data {
119 const struct iio_chan_spec *iio_channels;
120 const struct twl6030_ideal_code *ideal;
121 int (*start_conversion)(int channel);
122 u8 (*channel_to_reg)(int channel);
123 int (*calibrate)(struct twl6030_gpadc_data *gpadc);
127 * struct twl6030_gpadc_data - GPADC data
128 * @dev: device pointer
129 * @lock: mutual exclusion lock for the structure
130 * @irq_complete: completion to signal end of conversion
131 * @twl6030_cal_tbl: pointer to calibration data for each
132 * channel with gain error and offset
133 * @pdata: pointer to device specific data
135 struct twl6030_gpadc_data {
138 struct completion irq_complete;
139 struct twl6030_chnl_calib *twl6030_cal_tbl;
140 const struct twl6030_gpadc_platform_data *pdata;
144 * channels 11, 12, 13, 15 and 16 have no calibration data
145 * calibration offset is same for channels 1, 3, 4, 5
147 * The data is taken from GPADC_TRIM registers description.
148 * GPADC_TRIM registers keep difference between the code measured
149 * at volt1 and volt2 input voltages and corresponding code1 and code2
151 static const struct twl6030_ideal_code
152 twl6030_ideal[TWL6030_GPADC_USED_CHANNELS] = {
153 [0] = { /* ch 0, external, battery type, resistor value */
160 [1] = { /* ch 1, external, battery temperature, NTC resistor value */
167 [2] = { /* ch 2, external, audio accessory/general purpose */
174 [3] = { /* ch 3, external, general purpose */
181 [4] = { /* ch 4, external, temperature measurement/general purpose */
188 [5] = { /* ch 5, external, general purpose */
195 [6] = { /* ch 6, external, general purpose */
202 [7] = { /* ch 7, internal, main battery */
209 [8] = { /* ch 8, internal, backup battery */
216 [9] = { /* ch 9, internal, external charger input */
223 [10] = { /* ch 10, internal, VBUS */
230 [11] = { /* ch 11, internal, VBUS charging current */
233 /* ch 12, internal, Die temperature */
234 /* ch 13, internal, Die temperature */
235 [12] = { /* ch 14, internal, USB ID line */
244 static const struct twl6030_ideal_code
245 twl6032_ideal[TWL6032_GPADC_USED_CHANNELS] = {
246 [0] = { /* ch 0, external, battery type, resistor value */
253 [1] = { /* ch 1, external, battery temperature, NTC resistor value */
260 [2] = { /* ch 2, external, audio accessory/general purpose */
267 [3] = { /* ch 3, external, temperature with external diode/general
275 [4] = { /* ch 4, external, temperature measurement/general purpose */
282 [5] = { /* ch 5, external, general purpose */
289 [6] = { /* ch 6, external, general purpose */
296 [7] = { /* ch7, internal, system supply */
303 [8] = { /* ch8, internal, backup battery */
310 [9] = { /* ch 9, internal, external charger input */
317 [10] = { /* ch10, internal, VBUS */
324 [11] = { /* ch 11, internal, VBUS DC-DC output current */
331 /* ch 12, internal, Die temperature */
332 /* ch 13, internal, Die temperature */
333 [12] = { /* ch 14, internal, USB ID line */
340 /* ch 15, internal, test network */
341 /* ch 16, internal, test network */
342 [13] = { /* ch 17, internal, battery charging current */
345 [14] = { /* ch 18, internal, battery voltage */
354 static inline int twl6030_gpadc_write(u8 reg, u8 val)
356 return twl_i2c_write_u8(TWL6030_MODULE_GPADC, val, reg);
359 static inline int twl6030_gpadc_read(u8 reg, u8 *val)
362 return twl_i2c_read(TWL6030_MODULE_GPADC, val, reg, 2);
365 static int twl6030_gpadc_enable_irq(u8 mask)
369 ret = twl6030_interrupt_unmask(mask, REG_INT_MSK_LINE_B);
373 ret = twl6030_interrupt_unmask(mask, REG_INT_MSK_STS_B);
378 static void twl6030_gpadc_disable_irq(u8 mask)
380 twl6030_interrupt_mask(mask, REG_INT_MSK_LINE_B);
381 twl6030_interrupt_mask(mask, REG_INT_MSK_STS_B);
384 static irqreturn_t twl6030_gpadc_irq_handler(int irq, void *indio_dev)
386 struct twl6030_gpadc_data *gpadc = iio_priv(indio_dev);
388 complete(&gpadc->irq_complete);
393 static int twl6030_start_conversion(int channel)
395 return twl6030_gpadc_write(TWL6030_GPADC_CTRL_P1,
396 TWL6030_GPADC_CTRL_P1_SP1);
399 static int twl6032_start_conversion(int channel)
403 ret = twl6030_gpadc_write(TWL6032_GPADC_GPSELECT_ISB, channel);
407 return twl6030_gpadc_write(TWL6032_GPADC_CTRL_P1,
408 TWL6030_GPADC_CTRL_P1_SP1);
411 static u8 twl6030_channel_to_reg(int channel)
413 return TWL6030_GPADC_GPCH0_LSB + 2 * channel;
416 static u8 twl6032_channel_to_reg(int channel)
419 * for any prior chosen channel, when the conversion is ready
420 * the result is avalable in GPCH0_LSB, GPCH0_MSB.
423 return TWL6032_GPADC_GPCH0_LSB;
426 static int twl6030_gpadc_lookup(const struct twl6030_ideal_code *ideal,
427 int channel, int size)
431 for (i = 0; i < size; i++)
432 if (ideal[i].channel == channel)
438 static int twl6030_channel_calibrated(const struct twl6030_gpadc_platform_data
441 const struct twl6030_ideal_code *ideal = pdata->ideal;
444 i = twl6030_gpadc_lookup(ideal, channel, pdata->nchannels);
445 /* not calibrated channels have 0 in all structure members */
446 return pdata->ideal[i].code2;
449 static int twl6030_gpadc_make_correction(struct twl6030_gpadc_data *gpadc,
450 int channel, int raw_code)
452 const struct twl6030_ideal_code *ideal = gpadc->pdata->ideal;
456 i = twl6030_gpadc_lookup(ideal, channel, gpadc->pdata->nchannels);
457 corrected_code = ((raw_code * 1000) -
458 gpadc->twl6030_cal_tbl[i].offset_error) /
459 gpadc->twl6030_cal_tbl[i].gain_error;
461 return corrected_code;
464 static int twl6030_gpadc_get_raw(struct twl6030_gpadc_data *gpadc,
465 int channel, int *res)
467 u8 reg = gpadc->pdata->channel_to_reg(channel);
472 ret = twl6030_gpadc_read(reg, (u8 *)&val);
474 dev_dbg(gpadc->dev, "unable to read register 0x%X\n", reg);
478 raw_code = le16_to_cpu(val);
479 dev_dbg(gpadc->dev, "GPADC raw code: %d", raw_code);
481 if (twl6030_channel_calibrated(gpadc->pdata, channel))
482 *res = twl6030_gpadc_make_correction(gpadc, channel, raw_code);
489 static int twl6030_gpadc_get_processed(struct twl6030_gpadc_data *gpadc,
490 int channel, int *val)
492 const struct twl6030_ideal_code *ideal = gpadc->pdata->ideal;
498 ret = twl6030_gpadc_get_raw(gpadc, channel, &corrected_code);
502 i = twl6030_gpadc_lookup(ideal, channel, gpadc->pdata->nchannels);
503 channel_value = corrected_code *
504 gpadc->twl6030_cal_tbl[i].gain;
506 /* Shift back into mV range */
507 channel_value /= 1000;
509 dev_dbg(gpadc->dev, "GPADC corrected code: %d", corrected_code);
510 dev_dbg(gpadc->dev, "GPADC value: %d", channel_value);
512 *val = channel_value;
517 static int twl6030_gpadc_read_raw(struct iio_dev *indio_dev,
518 const struct iio_chan_spec *chan,
519 int *val, int *val2, long mask)
521 struct twl6030_gpadc_data *gpadc = iio_priv(indio_dev);
525 mutex_lock(&gpadc->lock);
527 ret = gpadc->pdata->start_conversion(chan->channel);
529 dev_err(gpadc->dev, "failed to start conversion\n");
532 /* wait for conversion to complete */
533 timeout = wait_for_completion_interruptible_timeout(
534 &gpadc->irq_complete, msecs_to_jiffies(5000));
538 } else if (timeout < 0) {
544 case IIO_CHAN_INFO_RAW:
545 ret = twl6030_gpadc_get_raw(gpadc, chan->channel, val);
546 ret = ret ? -EIO : IIO_VAL_INT;
549 case IIO_CHAN_INFO_PROCESSED:
550 ret = twl6030_gpadc_get_processed(gpadc, chan->channel, val);
551 ret = ret ? -EIO : IIO_VAL_INT;
558 mutex_unlock(&gpadc->lock);
564 * The GPADC channels are calibrated using a two point calibration method.
565 * The channels measured with two known values: volt1 and volt2, and
566 * ideal corresponding output codes are known: code1, code2.
567 * The difference(d1, d2) between ideal and measured codes stored in trim
569 * The goal is to find offset and gain of the real curve for each calibrated
571 * gain: k = 1 + ((d2 - d1) / (x2 - x1))
572 * offset: b = d1 + (k - 1) * x1
574 static void twl6030_calibrate_channel(struct twl6030_gpadc_data *gpadc,
575 int channel, int d1, int d2)
577 int b, k, gain, x1, x2, i;
578 const struct twl6030_ideal_code *ideal = gpadc->pdata->ideal;
580 i = twl6030_gpadc_lookup(ideal, channel, gpadc->pdata->nchannels);
583 gain = ((ideal[i].volt2 - ideal[i].volt1) * 1000) /
584 (ideal[i].code2 - ideal[i].code1);
589 /* k - real curve gain */
590 k = 1000 + (((d2 - d1) * 1000) / (x2 - x1));
592 /* b - offset of the real curve gain */
593 b = (d1 * 1000) - (k - 1000) * x1;
595 gpadc->twl6030_cal_tbl[i].gain = gain;
596 gpadc->twl6030_cal_tbl[i].gain_error = k;
597 gpadc->twl6030_cal_tbl[i].offset_error = b;
599 dev_dbg(gpadc->dev, "GPADC d1 for Chn: %d = %d\n", channel, d1);
600 dev_dbg(gpadc->dev, "GPADC d2 for Chn: %d = %d\n", channel, d2);
601 dev_dbg(gpadc->dev, "GPADC x1 for Chn: %d = %d\n", channel, x1);
602 dev_dbg(gpadc->dev, "GPADC x2 for Chn: %d = %d\n", channel, x2);
603 dev_dbg(gpadc->dev, "GPADC Gain for Chn: %d = %d\n", channel, gain);
604 dev_dbg(gpadc->dev, "GPADC k for Chn: %d = %d\n", channel, k);
605 dev_dbg(gpadc->dev, "GPADC b for Chn: %d = %d\n", channel, b);
608 static inline int twl6030_gpadc_get_trim_offset(s8 d)
612 * bit 0 - sign, bit 7 - reserved, 6..1 - trim value
613 * though, the documentation states that trim value
614 * is absolute value, the correct conversion results are
615 * obtained if the value is interpreted as 2's complement.
617 __u32 temp = ((d & 0x7f) >> 1) | ((d & 1) << 6);
619 return sign_extend32(temp, 6);
622 static int twl6030_calibration(struct twl6030_gpadc_data *gpadc)
626 u8 trim_regs[TWL6030_GPADC_NUM_TRIM_REGS];
630 * for calibration two measurements have been performed at
631 * factory, for some channels, during the production test and
632 * have been stored in registers. This two stored values are
633 * used to correct the measurements. The values represent
634 * offsets for the given input from the output on ideal curve.
636 ret = twl_i2c_read(TWL6030_MODULE_ID2, trim_regs,
637 TWL6030_GPADC_TRIM1, TWL6030_GPADC_NUM_TRIM_REGS);
639 dev_err(gpadc->dev, "calibration failed\n");
643 for (chn = 0; chn < TWL6030_GPADC_MAX_CHANNELS; chn++) {
686 d1 = twl6030_gpadc_get_trim_offset(d1);
687 d2 = twl6030_gpadc_get_trim_offset(d2);
689 twl6030_calibrate_channel(gpadc, chn, d1, d2);
695 static int twl6032_get_trim_value(u8 *trim_regs, unsigned int reg0,
696 unsigned int reg1, unsigned int mask0, unsigned int mask1,
701 val = (trim_regs[reg0] & mask0) << shift0;
702 val |= (trim_regs[reg1] & mask1) >> 1;
703 if (trim_regs[reg1] & 0x01)
709 static int twl6032_calibration(struct twl6030_gpadc_data *gpadc)
711 int chn, d1 = 0, d2 = 0, temp;
712 u8 trim_regs[TWL6030_GPADC_NUM_TRIM_REGS];
715 ret = twl_i2c_read(TWL6030_MODULE_ID2, trim_regs,
716 TWL6030_GPADC_TRIM1, TWL6030_GPADC_NUM_TRIM_REGS);
718 dev_err(gpadc->dev, "calibration failed\n");
723 * Loop to calculate the value needed for returning voltages from
726 * gain is calculated to 3 decimal places fixed point.
728 for (chn = 0; chn < TWL6032_GPADC_MAX_CHANNELS; chn++) {
740 d1 = twl6032_get_trim_value(trim_regs, 2, 0, 0x1f,
742 d2 = twl6032_get_trim_value(trim_regs, 3, 1, 0x3f,
746 temp = twl6032_get_trim_value(trim_regs, 2, 0, 0x1f,
748 d1 = temp + twl6032_get_trim_value(trim_regs, 7, 6,
751 temp = twl6032_get_trim_value(trim_regs, 3, 1, 0x3F,
753 d2 = temp + twl6032_get_trim_value(trim_regs, 9, 7,
757 temp = twl6032_get_trim_value(trim_regs, 2, 0, 0x1f,
759 d1 = temp + twl6032_get_trim_value(trim_regs, 13, 11,
762 temp = twl6032_get_trim_value(trim_regs, 3, 1, 0x3f,
764 d2 = temp + twl6032_get_trim_value(trim_regs, 15, 13,
768 d1 = twl6032_get_trim_value(trim_regs, 10, 8, 0x0f,
770 d2 = twl6032_get_trim_value(trim_regs, 14, 12, 0x0f,
775 temp = twl6032_get_trim_value(trim_regs, 2, 0, 0x1f,
778 d1 = (trim_regs[4] & 0x7E) >> 1;
779 if (trim_regs[4] & 0x01)
783 temp = twl6032_get_trim_value(trim_regs, 3, 1, 0x3f,
786 d2 = (trim_regs[5] & 0xFE) >> 1;
787 if (trim_regs[5] & 0x01)
793 /* No data for other channels */
797 twl6030_calibrate_channel(gpadc, chn, d1, d2);
803 #define TWL6030_GPADC_CHAN(chn, _type, chan_info) { \
806 .info_mask_separate = BIT(chan_info), \
810 static const struct iio_chan_spec twl6030_gpadc_iio_channels[] = {
811 TWL6030_GPADC_CHAN(0, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
812 TWL6030_GPADC_CHAN(1, IIO_TEMP, IIO_CHAN_INFO_RAW),
813 TWL6030_GPADC_CHAN(2, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
814 TWL6030_GPADC_CHAN(3, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
815 TWL6030_GPADC_CHAN(4, IIO_TEMP, IIO_CHAN_INFO_RAW),
816 TWL6030_GPADC_CHAN(5, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
817 TWL6030_GPADC_CHAN(6, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
818 TWL6030_GPADC_CHAN(7, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
819 TWL6030_GPADC_CHAN(8, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
820 TWL6030_GPADC_CHAN(9, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
821 TWL6030_GPADC_CHAN(10, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
822 TWL6030_GPADC_CHAN(11, IIO_VOLTAGE, IIO_CHAN_INFO_RAW),
823 TWL6030_GPADC_CHAN(14, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
826 static const struct iio_chan_spec twl6032_gpadc_iio_channels[] = {
827 TWL6030_GPADC_CHAN(0, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
828 TWL6030_GPADC_CHAN(1, IIO_TEMP, IIO_CHAN_INFO_RAW),
829 TWL6030_GPADC_CHAN(2, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
830 TWL6030_GPADC_CHAN(3, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
831 TWL6030_GPADC_CHAN(4, IIO_TEMP, IIO_CHAN_INFO_RAW),
832 TWL6030_GPADC_CHAN(5, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
833 TWL6030_GPADC_CHAN(6, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
834 TWL6030_GPADC_CHAN(7, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
835 TWL6030_GPADC_CHAN(8, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
836 TWL6030_GPADC_CHAN(9, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
837 TWL6030_GPADC_CHAN(10, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
838 TWL6030_GPADC_CHAN(11, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
839 TWL6030_GPADC_CHAN(14, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
840 TWL6030_GPADC_CHAN(17, IIO_VOLTAGE, IIO_CHAN_INFO_RAW),
841 TWL6030_GPADC_CHAN(18, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
844 static const struct iio_info twl6030_gpadc_iio_info = {
845 .read_raw = &twl6030_gpadc_read_raw,
848 static const struct twl6030_gpadc_platform_data twl6030_pdata = {
849 .iio_channels = twl6030_gpadc_iio_channels,
850 .nchannels = TWL6030_GPADC_USED_CHANNELS,
851 .ideal = twl6030_ideal,
852 .start_conversion = twl6030_start_conversion,
853 .channel_to_reg = twl6030_channel_to_reg,
854 .calibrate = twl6030_calibration,
857 static const struct twl6030_gpadc_platform_data twl6032_pdata = {
858 .iio_channels = twl6032_gpadc_iio_channels,
859 .nchannels = TWL6032_GPADC_USED_CHANNELS,
860 .ideal = twl6032_ideal,
861 .start_conversion = twl6032_start_conversion,
862 .channel_to_reg = twl6032_channel_to_reg,
863 .calibrate = twl6032_calibration,
866 static const struct of_device_id of_twl6030_match_tbl[] = {
868 .compatible = "ti,twl6030-gpadc",
869 .data = &twl6030_pdata,
872 .compatible = "ti,twl6032-gpadc",
873 .data = &twl6032_pdata,
877 MODULE_DEVICE_TABLE(of, of_twl6030_match_tbl);
879 static int twl6030_gpadc_probe(struct platform_device *pdev)
881 struct device *dev = &pdev->dev;
882 struct twl6030_gpadc_data *gpadc;
883 const struct twl6030_gpadc_platform_data *pdata;
884 const struct of_device_id *match;
885 struct iio_dev *indio_dev;
889 match = of_match_device(of_twl6030_match_tbl, dev);
895 indio_dev = devm_iio_device_alloc(dev, sizeof(*gpadc));
899 gpadc = iio_priv(indio_dev);
901 gpadc->twl6030_cal_tbl = devm_kzalloc(dev,
902 sizeof(*gpadc->twl6030_cal_tbl) *
903 pdata->nchannels, GFP_KERNEL);
904 if (!gpadc->twl6030_cal_tbl)
908 gpadc->pdata = pdata;
910 platform_set_drvdata(pdev, indio_dev);
911 mutex_init(&gpadc->lock);
912 init_completion(&gpadc->irq_complete);
914 ret = pdata->calibrate(gpadc);
916 dev_err(&pdev->dev, "failed to read calibration registers\n");
920 irq = platform_get_irq(pdev, 0);
922 dev_err(&pdev->dev, "failed to get irq\n");
926 ret = devm_request_threaded_irq(dev, irq, NULL,
927 twl6030_gpadc_irq_handler,
928 IRQF_ONESHOT, "twl6030_gpadc", indio_dev);
930 ret = twl6030_gpadc_enable_irq(TWL6030_GPADC_RT_SW1_EOC_MASK);
932 dev_err(&pdev->dev, "failed to enable GPADC interrupt\n");
936 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, TWL6030_GPADCS,
937 TWL6030_REG_TOGGLE1);
939 dev_err(&pdev->dev, "failed to enable GPADC module\n");
943 indio_dev->name = DRIVER_NAME;
944 indio_dev->dev.parent = dev;
945 indio_dev->info = &twl6030_gpadc_iio_info;
946 indio_dev->modes = INDIO_DIRECT_MODE;
947 indio_dev->channels = pdata->iio_channels;
948 indio_dev->num_channels = pdata->nchannels;
950 return iio_device_register(indio_dev);
953 static int twl6030_gpadc_remove(struct platform_device *pdev)
955 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
957 twl6030_gpadc_disable_irq(TWL6030_GPADC_RT_SW1_EOC_MASK);
958 iio_device_unregister(indio_dev);
963 #ifdef CONFIG_PM_SLEEP
964 static int twl6030_gpadc_suspend(struct device *pdev)
968 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, TWL6030_GPADCR,
969 TWL6030_REG_TOGGLE1);
971 dev_err(pdev, "error resetting GPADC (%d)!\n", ret);
976 static int twl6030_gpadc_resume(struct device *pdev)
980 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, TWL6030_GPADCS,
981 TWL6030_REG_TOGGLE1);
983 dev_err(pdev, "error setting GPADC (%d)!\n", ret);
989 static SIMPLE_DEV_PM_OPS(twl6030_gpadc_pm_ops, twl6030_gpadc_suspend,
990 twl6030_gpadc_resume);
992 static struct platform_driver twl6030_gpadc_driver = {
993 .probe = twl6030_gpadc_probe,
994 .remove = twl6030_gpadc_remove,
997 .pm = &twl6030_gpadc_pm_ops,
998 .of_match_table = of_twl6030_match_tbl,
1002 module_platform_driver(twl6030_gpadc_driver);
1004 MODULE_ALIAS("platform:" DRIVER_NAME);
1005 MODULE_AUTHOR("Balaji T K <balajitk@ti.com>");
1006 MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
1007 MODULE_AUTHOR("Oleksandr Kozaruk <oleksandr.kozaruk@ti.com");
1008 MODULE_DESCRIPTION("twl6030 ADC driver");
1009 MODULE_LICENSE("GPL");