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)
61 * struct twl6030_chnl_calib - channel calibration
62 * @gain: slope coefficient for ideal curve
63 * @gain_error: gain error
64 * @offset_error: offset of the real curve
66 struct twl6030_chnl_calib
{
73 * struct twl6030_ideal_code - GPADC calibration parameters
74 * GPADC is calibrated in two points: close to the beginning and
75 * to the and of the measurable input range
77 * @channel: channel number
78 * @code1: ideal code for the input at the beginning
79 * @code2: ideal code for at the end of the range
80 * @volt1: voltage input at the beginning(low voltage)
81 * @volt2: voltage input at the end(high voltage)
83 struct twl6030_ideal_code
{
91 struct twl6030_gpadc_data
;
94 * struct twl6030_gpadc_platform_data - platform specific data
95 * @nchannels: number of GPADC channels
96 * @iio_channels: iio channels
97 * @ideal: pointer to calibration parameters
98 * @start_conversion: pointer to ADC start conversion function
99 * @channel_to_reg: pointer to ADC function to convert channel to
100 * register address for reading conversion result
101 * @calibrate: pointer to calibration function
103 struct twl6030_gpadc_platform_data
{
105 const struct iio_chan_spec
*iio_channels
;
106 const struct twl6030_ideal_code
*ideal
;
107 int (*start_conversion
)(int channel
);
108 u8 (*channel_to_reg
)(int channel
);
109 int (*calibrate
)(struct twl6030_gpadc_data
*gpadc
);
113 * struct twl6030_gpadc_data - GPADC data
114 * @dev: device pointer
115 * @lock: mutual exclusion lock for the structure
116 * @irq_complete: completion to signal end of conversion
117 * @twl6030_cal_tbl: pointer to calibration data for each
118 * channel with gain error and offset
119 * @pdata: pointer to device specific data
121 struct twl6030_gpadc_data
{
124 struct completion irq_complete
;
125 struct twl6030_chnl_calib
*twl6030_cal_tbl
;
126 const struct twl6030_gpadc_platform_data
*pdata
;
130 * channels 11, 12, 13, 15 and 16 have no calibration data
131 * calibration offset is same for channels 1, 3, 4, 5
133 * The data is taken from GPADC_TRIM registers description.
134 * GPADC_TRIM registers keep difference between the code measured
135 * at volt1 and volt2 input voltages and corresponding code1 and code2
137 static const struct twl6030_ideal_code
138 twl6030_ideal
[TWL6030_GPADC_USED_CHANNELS
] = {
139 [0] = { /* ch 0, external, battery type, resistor value */
146 [1] = { /* ch 1, external, battery temperature, NTC resistor value */
153 [2] = { /* ch 2, external, audio accessory/general purpose */
160 [3] = { /* ch 3, external, general purpose */
167 [4] = { /* ch 4, external, temperature measurement/general purpose */
174 [5] = { /* ch 5, external, general purpose */
181 [6] = { /* ch 6, external, general purpose */
188 [7] = { /* ch 7, internal, main battery */
195 [8] = { /* ch 8, internal, backup battery */
202 [9] = { /* ch 9, internal, external charger input */
209 [10] = { /* ch 10, internal, VBUS */
216 [11] = { /* ch 11, internal, VBUS charging current */
219 /* ch 12, internal, Die temperature */
220 /* ch 13, internal, Die temperature */
221 [12] = { /* ch 14, internal, USB ID line */
230 static const struct twl6030_ideal_code
231 twl6032_ideal
[TWL6032_GPADC_USED_CHANNELS
] = {
232 [0] = { /* ch 0, external, battery type, resistor value */
239 [1] = { /* ch 1, external, battery temperature, NTC resistor value */
246 [2] = { /* ch 2, external, audio accessory/general purpose */
253 [3] = { /* ch 3, external, temperature with external diode/general
261 [4] = { /* ch 4, external, temperature measurement/general purpose */
268 [5] = { /* ch 5, external, general purpose */
275 [6] = { /* ch 6, external, general purpose */
282 [7] = { /* ch7, internal, system supply */
289 [8] = { /* ch8, internal, backup battery */
296 [9] = { /* ch 9, internal, external charger input */
303 [10] = { /* ch10, internal, VBUS */
310 [11] = { /* ch 11, internal, VBUS DC-DC output current */
317 /* ch 12, internal, Die temperature */
318 /* ch 13, internal, Die temperature */
319 [12] = { /* ch 14, internal, USB ID line */
326 /* ch 15, internal, test network */
327 /* ch 16, internal, test network */
328 [13] = { /* ch 17, internal, battery charging current */
331 [14] = { /* ch 18, internal, battery voltage */
340 static inline int twl6030_gpadc_write(u8 reg
, u8 val
)
342 return twl_i2c_write_u8(TWL6030_MODULE_GPADC
, val
, reg
);
345 static inline int twl6030_gpadc_read(u8 reg
, u8
*val
)
348 return twl_i2c_read(TWL6030_MODULE_GPADC
, val
, reg
, 2);
351 static int twl6030_gpadc_enable_irq(u8 mask
)
355 ret
= twl6030_interrupt_unmask(mask
, REG_INT_MSK_LINE_B
);
359 ret
= twl6030_interrupt_unmask(mask
, REG_INT_MSK_STS_B
);
364 static void twl6030_gpadc_disable_irq(u8 mask
)
366 twl6030_interrupt_mask(mask
, REG_INT_MSK_LINE_B
);
367 twl6030_interrupt_mask(mask
, REG_INT_MSK_STS_B
);
370 static irqreturn_t
twl6030_gpadc_irq_handler(int irq
, void *indio_dev
)
372 struct twl6030_gpadc_data
*gpadc
= iio_priv(indio_dev
);
374 complete(&gpadc
->irq_complete
);
379 static int twl6030_start_conversion(int channel
)
381 return twl6030_gpadc_write(TWL6030_GPADC_CTRL_P1
,
382 TWL6030_GPADC_CTRL_P1_SP1
);
385 static int twl6032_start_conversion(int channel
)
389 ret
= twl6030_gpadc_write(TWL6032_GPADC_GPSELECT_ISB
, channel
);
393 return twl6030_gpadc_write(TWL6032_GPADC_CTRL_P1
,
394 TWL6030_GPADC_CTRL_P1_SP1
);
397 static u8
twl6030_channel_to_reg(int channel
)
399 return TWL6030_GPADC_GPCH0_LSB
+ 2 * channel
;
402 static u8
twl6032_channel_to_reg(int channel
)
405 * for any prior chosen channel, when the conversion is ready
406 * the result is avalable in GPCH0_LSB, GPCH0_MSB.
409 return TWL6032_GPADC_GPCH0_LSB
;
412 static int twl6030_gpadc_lookup(const struct twl6030_ideal_code
*ideal
,
413 int channel
, int size
)
417 for (i
= 0; i
< size
; i
++)
418 if (ideal
[i
].channel
== channel
)
424 static int twl6030_channel_calibrated(const struct twl6030_gpadc_platform_data
427 const struct twl6030_ideal_code
*ideal
= pdata
->ideal
;
430 i
= twl6030_gpadc_lookup(ideal
, channel
, pdata
->nchannels
);
431 /* not calibrated channels have 0 in all structure members */
432 return pdata
->ideal
[i
].code2
;
435 static int twl6030_gpadc_make_correction(struct twl6030_gpadc_data
*gpadc
,
436 int channel
, int raw_code
)
438 const struct twl6030_ideal_code
*ideal
= gpadc
->pdata
->ideal
;
442 i
= twl6030_gpadc_lookup(ideal
, channel
, gpadc
->pdata
->nchannels
);
443 corrected_code
= ((raw_code
* 1000) -
444 gpadc
->twl6030_cal_tbl
[i
].offset_error
) /
445 gpadc
->twl6030_cal_tbl
[i
].gain_error
;
447 return corrected_code
;
450 static int twl6030_gpadc_get_raw(struct twl6030_gpadc_data
*gpadc
,
451 int channel
, int *res
)
453 u8 reg
= gpadc
->pdata
->channel_to_reg(channel
);
458 ret
= twl6030_gpadc_read(reg
, (u8
*)&val
);
460 dev_dbg(gpadc
->dev
, "unable to read register 0x%X\n", reg
);
464 raw_code
= le16_to_cpu(val
);
465 dev_dbg(gpadc
->dev
, "GPADC raw code: %d", raw_code
);
467 if (twl6030_channel_calibrated(gpadc
->pdata
, channel
))
468 *res
= twl6030_gpadc_make_correction(gpadc
, channel
, raw_code
);
475 static int twl6030_gpadc_get_processed(struct twl6030_gpadc_data
*gpadc
,
476 int channel
, int *val
)
478 const struct twl6030_ideal_code
*ideal
= gpadc
->pdata
->ideal
;
484 ret
= twl6030_gpadc_get_raw(gpadc
, channel
, &corrected_code
);
488 i
= twl6030_gpadc_lookup(ideal
, channel
, gpadc
->pdata
->nchannels
);
489 channel_value
= corrected_code
*
490 gpadc
->twl6030_cal_tbl
[i
].gain
;
492 /* Shift back into mV range */
493 channel_value
/= 1000;
495 dev_dbg(gpadc
->dev
, "GPADC corrected code: %d", corrected_code
);
496 dev_dbg(gpadc
->dev
, "GPADC value: %d", channel_value
);
498 *val
= channel_value
;
503 static int twl6030_gpadc_read_raw(struct iio_dev
*indio_dev
,
504 const struct iio_chan_spec
*chan
,
505 int *val
, int *val2
, long mask
)
507 struct twl6030_gpadc_data
*gpadc
= iio_priv(indio_dev
);
511 mutex_lock(&gpadc
->lock
);
513 ret
= gpadc
->pdata
->start_conversion(chan
->channel
);
515 dev_err(gpadc
->dev
, "failed to start conversion\n");
518 /* wait for conversion to complete */
519 timeout
= wait_for_completion_interruptible_timeout(
520 &gpadc
->irq_complete
, msecs_to_jiffies(5000));
524 } else if (timeout
< 0) {
530 case IIO_CHAN_INFO_RAW
:
531 ret
= twl6030_gpadc_get_raw(gpadc
, chan
->channel
, val
);
532 ret
= ret
? -EIO
: IIO_VAL_INT
;
535 case IIO_CHAN_INFO_PROCESSED
:
536 ret
= twl6030_gpadc_get_processed(gpadc
, chan
->channel
, val
);
537 ret
= ret
? -EIO
: IIO_VAL_INT
;
544 mutex_unlock(&gpadc
->lock
);
550 * The GPADC channels are calibrated using a two point calibration method.
551 * The channels measured with two known values: volt1 and volt2, and
552 * ideal corresponding output codes are known: code1, code2.
553 * The difference(d1, d2) between ideal and measured codes stored in trim
555 * The goal is to find offset and gain of the real curve for each calibrated
557 * gain: k = 1 + ((d2 - d1) / (x2 - x1))
558 * offset: b = d1 + (k - 1) * x1
560 static void twl6030_calibrate_channel(struct twl6030_gpadc_data
*gpadc
,
561 int channel
, int d1
, int d2
)
563 int b
, k
, gain
, x1
, x2
, i
;
564 const struct twl6030_ideal_code
*ideal
= gpadc
->pdata
->ideal
;
566 i
= twl6030_gpadc_lookup(ideal
, channel
, gpadc
->pdata
->nchannels
);
569 gain
= ((ideal
[i
].volt2
- ideal
[i
].volt1
) * 1000) /
570 (ideal
[i
].code2
- ideal
[i
].code1
);
575 /* k - real curve gain */
576 k
= 1000 + (((d2
- d1
) * 1000) / (x2
- x1
));
578 /* b - offset of the real curve gain */
579 b
= (d1
* 1000) - (k
- 1000) * x1
;
581 gpadc
->twl6030_cal_tbl
[i
].gain
= gain
;
582 gpadc
->twl6030_cal_tbl
[i
].gain_error
= k
;
583 gpadc
->twl6030_cal_tbl
[i
].offset_error
= b
;
585 dev_dbg(gpadc
->dev
, "GPADC d1 for Chn: %d = %d\n", channel
, d1
);
586 dev_dbg(gpadc
->dev
, "GPADC d2 for Chn: %d = %d\n", channel
, d2
);
587 dev_dbg(gpadc
->dev
, "GPADC x1 for Chn: %d = %d\n", channel
, x1
);
588 dev_dbg(gpadc
->dev
, "GPADC x2 for Chn: %d = %d\n", channel
, x2
);
589 dev_dbg(gpadc
->dev
, "GPADC Gain for Chn: %d = %d\n", channel
, gain
);
590 dev_dbg(gpadc
->dev
, "GPADC k for Chn: %d = %d\n", channel
, k
);
591 dev_dbg(gpadc
->dev
, "GPADC b for Chn: %d = %d\n", channel
, b
);
594 static inline int twl6030_gpadc_get_trim_offset(s8 d
)
598 * bit 0 - sign, bit 7 - reserved, 6..1 - trim value
599 * though, the documentation states that trim value
600 * is absolute value, the correct conversion results are
601 * obtained if the value is interpreted as 2's complement.
603 __u32 temp
= ((d
& 0x7f) >> 1) | ((d
& 1) << 6);
605 return sign_extend32(temp
, 6);
608 static int twl6030_calibration(struct twl6030_gpadc_data
*gpadc
)
612 u8 trim_regs
[TWL6030_GPADC_NUM_TRIM_REGS
];
616 * for calibration two measurements have been performed at
617 * factory, for some channels, during the production test and
618 * have been stored in registers. This two stored values are
619 * used to correct the measurements. The values represent
620 * offsets for the given input from the output on ideal curve.
622 ret
= twl_i2c_read(TWL6030_MODULE_ID2
, trim_regs
,
623 TWL6030_GPADC_TRIM1
, TWL6030_GPADC_NUM_TRIM_REGS
);
625 dev_err(gpadc
->dev
, "calibration failed\n");
629 for (chn
= 0; chn
< TWL6030_GPADC_MAX_CHANNELS
; chn
++) {
672 d1
= twl6030_gpadc_get_trim_offset(d1
);
673 d2
= twl6030_gpadc_get_trim_offset(d2
);
675 twl6030_calibrate_channel(gpadc
, chn
, d1
, d2
);
681 static int twl6032_get_trim_value(u8
*trim_regs
, unsigned int reg0
,
682 unsigned int reg1
, unsigned int mask0
, unsigned int mask1
,
687 val
= (trim_regs
[reg0
] & mask0
) << shift0
;
688 val
|= (trim_regs
[reg1
] & mask1
) >> 1;
689 if (trim_regs
[reg1
] & 0x01)
695 static int twl6032_calibration(struct twl6030_gpadc_data
*gpadc
)
697 int chn
, d1
= 0, d2
= 0, temp
;
698 u8 trim_regs
[TWL6030_GPADC_NUM_TRIM_REGS
];
701 ret
= twl_i2c_read(TWL6030_MODULE_ID2
, trim_regs
,
702 TWL6030_GPADC_TRIM1
, TWL6030_GPADC_NUM_TRIM_REGS
);
704 dev_err(gpadc
->dev
, "calibration failed\n");
709 * Loop to calculate the value needed for returning voltages from
712 * gain is calculated to 3 decimal places fixed point.
714 for (chn
= 0; chn
< TWL6032_GPADC_MAX_CHANNELS
; chn
++) {
726 d1
= twl6032_get_trim_value(trim_regs
, 2, 0, 0x1f,
728 d2
= twl6032_get_trim_value(trim_regs
, 3, 1, 0x3f,
732 temp
= twl6032_get_trim_value(trim_regs
, 2, 0, 0x1f,
734 d1
= temp
+ twl6032_get_trim_value(trim_regs
, 7, 6,
737 temp
= twl6032_get_trim_value(trim_regs
, 3, 1, 0x3F,
739 d2
= temp
+ twl6032_get_trim_value(trim_regs
, 9, 7,
743 temp
= twl6032_get_trim_value(trim_regs
, 2, 0, 0x1f,
745 d1
= temp
+ twl6032_get_trim_value(trim_regs
, 13, 11,
748 temp
= twl6032_get_trim_value(trim_regs
, 3, 1, 0x3f,
750 d2
= temp
+ twl6032_get_trim_value(trim_regs
, 15, 13,
754 d1
= twl6032_get_trim_value(trim_regs
, 10, 8, 0x0f,
756 d2
= twl6032_get_trim_value(trim_regs
, 14, 12, 0x0f,
761 temp
= twl6032_get_trim_value(trim_regs
, 2, 0, 0x1f,
764 d1
= (trim_regs
[4] & 0x7E) >> 1;
765 if (trim_regs
[4] & 0x01)
769 temp
= twl6032_get_trim_value(trim_regs
, 3, 1, 0x3f,
772 d2
= (trim_regs
[5] & 0xFE) >> 1;
773 if (trim_regs
[5] & 0x01)
779 /* No data for other channels */
783 twl6030_calibrate_channel(gpadc
, chn
, d1
, d2
);
789 #define TWL6030_GPADC_CHAN(chn, _type, chan_info) { \
792 .info_mask_separate = BIT(chan_info), \
796 static const struct iio_chan_spec twl6030_gpadc_iio_channels
[] = {
797 TWL6030_GPADC_CHAN(0, IIO_VOLTAGE
, IIO_CHAN_INFO_PROCESSED
),
798 TWL6030_GPADC_CHAN(1, IIO_TEMP
, IIO_CHAN_INFO_RAW
),
799 TWL6030_GPADC_CHAN(2, IIO_VOLTAGE
, IIO_CHAN_INFO_PROCESSED
),
800 TWL6030_GPADC_CHAN(3, IIO_VOLTAGE
, IIO_CHAN_INFO_PROCESSED
),
801 TWL6030_GPADC_CHAN(4, IIO_TEMP
, IIO_CHAN_INFO_RAW
),
802 TWL6030_GPADC_CHAN(5, IIO_VOLTAGE
, IIO_CHAN_INFO_PROCESSED
),
803 TWL6030_GPADC_CHAN(6, IIO_VOLTAGE
, IIO_CHAN_INFO_PROCESSED
),
804 TWL6030_GPADC_CHAN(7, IIO_VOLTAGE
, IIO_CHAN_INFO_PROCESSED
),
805 TWL6030_GPADC_CHAN(8, IIO_VOLTAGE
, IIO_CHAN_INFO_PROCESSED
),
806 TWL6030_GPADC_CHAN(9, IIO_VOLTAGE
, IIO_CHAN_INFO_PROCESSED
),
807 TWL6030_GPADC_CHAN(10, IIO_VOLTAGE
, IIO_CHAN_INFO_PROCESSED
),
808 TWL6030_GPADC_CHAN(11, IIO_VOLTAGE
, IIO_CHAN_INFO_RAW
),
809 TWL6030_GPADC_CHAN(14, IIO_VOLTAGE
, IIO_CHAN_INFO_PROCESSED
),
812 static const struct iio_chan_spec twl6032_gpadc_iio_channels
[] = {
813 TWL6030_GPADC_CHAN(0, IIO_VOLTAGE
, IIO_CHAN_INFO_PROCESSED
),
814 TWL6030_GPADC_CHAN(1, IIO_TEMP
, IIO_CHAN_INFO_RAW
),
815 TWL6030_GPADC_CHAN(2, IIO_VOLTAGE
, IIO_CHAN_INFO_PROCESSED
),
816 TWL6030_GPADC_CHAN(3, IIO_VOLTAGE
, IIO_CHAN_INFO_PROCESSED
),
817 TWL6030_GPADC_CHAN(4, IIO_TEMP
, IIO_CHAN_INFO_RAW
),
818 TWL6030_GPADC_CHAN(5, IIO_VOLTAGE
, IIO_CHAN_INFO_PROCESSED
),
819 TWL6030_GPADC_CHAN(6, IIO_VOLTAGE
, IIO_CHAN_INFO_PROCESSED
),
820 TWL6030_GPADC_CHAN(7, IIO_VOLTAGE
, IIO_CHAN_INFO_PROCESSED
),
821 TWL6030_GPADC_CHAN(8, IIO_VOLTAGE
, IIO_CHAN_INFO_PROCESSED
),
822 TWL6030_GPADC_CHAN(9, IIO_VOLTAGE
, IIO_CHAN_INFO_PROCESSED
),
823 TWL6030_GPADC_CHAN(10, IIO_VOLTAGE
, IIO_CHAN_INFO_PROCESSED
),
824 TWL6030_GPADC_CHAN(11, IIO_VOLTAGE
, IIO_CHAN_INFO_PROCESSED
),
825 TWL6030_GPADC_CHAN(14, IIO_VOLTAGE
, IIO_CHAN_INFO_PROCESSED
),
826 TWL6030_GPADC_CHAN(17, IIO_VOLTAGE
, IIO_CHAN_INFO_RAW
),
827 TWL6030_GPADC_CHAN(18, IIO_VOLTAGE
, IIO_CHAN_INFO_PROCESSED
),
830 static const struct iio_info twl6030_gpadc_iio_info
= {
831 .read_raw
= &twl6030_gpadc_read_raw
,
834 static const struct twl6030_gpadc_platform_data twl6030_pdata
= {
835 .iio_channels
= twl6030_gpadc_iio_channels
,
836 .nchannels
= TWL6030_GPADC_USED_CHANNELS
,
837 .ideal
= twl6030_ideal
,
838 .start_conversion
= twl6030_start_conversion
,
839 .channel_to_reg
= twl6030_channel_to_reg
,
840 .calibrate
= twl6030_calibration
,
843 static const struct twl6030_gpadc_platform_data twl6032_pdata
= {
844 .iio_channels
= twl6032_gpadc_iio_channels
,
845 .nchannels
= TWL6032_GPADC_USED_CHANNELS
,
846 .ideal
= twl6032_ideal
,
847 .start_conversion
= twl6032_start_conversion
,
848 .channel_to_reg
= twl6032_channel_to_reg
,
849 .calibrate
= twl6032_calibration
,
852 static const struct of_device_id of_twl6030_match_tbl
[] = {
854 .compatible
= "ti,twl6030-gpadc",
855 .data
= &twl6030_pdata
,
858 .compatible
= "ti,twl6032-gpadc",
859 .data
= &twl6032_pdata
,
863 MODULE_DEVICE_TABLE(of
, of_twl6030_match_tbl
);
865 static int twl6030_gpadc_probe(struct platform_device
*pdev
)
867 struct device
*dev
= &pdev
->dev
;
868 struct twl6030_gpadc_data
*gpadc
;
869 const struct twl6030_gpadc_platform_data
*pdata
;
870 const struct of_device_id
*match
;
871 struct iio_dev
*indio_dev
;
875 match
= of_match_device(of_twl6030_match_tbl
, dev
);
881 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*gpadc
));
885 gpadc
= iio_priv(indio_dev
);
887 gpadc
->twl6030_cal_tbl
= devm_kcalloc(dev
,
889 sizeof(*gpadc
->twl6030_cal_tbl
),
891 if (!gpadc
->twl6030_cal_tbl
)
895 gpadc
->pdata
= pdata
;
897 platform_set_drvdata(pdev
, indio_dev
);
898 mutex_init(&gpadc
->lock
);
899 init_completion(&gpadc
->irq_complete
);
901 ret
= pdata
->calibrate(gpadc
);
903 dev_err(&pdev
->dev
, "failed to read calibration registers\n");
907 irq
= platform_get_irq(pdev
, 0);
911 ret
= devm_request_threaded_irq(dev
, irq
, NULL
,
912 twl6030_gpadc_irq_handler
,
913 IRQF_ONESHOT
, "twl6030_gpadc", indio_dev
);
915 ret
= twl6030_gpadc_enable_irq(TWL6030_GPADC_RT_SW1_EOC_MASK
);
917 dev_err(&pdev
->dev
, "failed to enable GPADC interrupt\n");
921 ret
= twl_i2c_write_u8(TWL6030_MODULE_ID1
, TWL6030_GPADCS
,
922 TWL6030_REG_TOGGLE1
);
924 dev_err(&pdev
->dev
, "failed to enable GPADC module\n");
928 indio_dev
->name
= DRIVER_NAME
;
929 indio_dev
->info
= &twl6030_gpadc_iio_info
;
930 indio_dev
->modes
= INDIO_DIRECT_MODE
;
931 indio_dev
->channels
= pdata
->iio_channels
;
932 indio_dev
->num_channels
= pdata
->nchannels
;
934 return iio_device_register(indio_dev
);
937 static int twl6030_gpadc_remove(struct platform_device
*pdev
)
939 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
941 twl6030_gpadc_disable_irq(TWL6030_GPADC_RT_SW1_EOC_MASK
);
942 iio_device_unregister(indio_dev
);
947 #ifdef CONFIG_PM_SLEEP
948 static int twl6030_gpadc_suspend(struct device
*pdev
)
952 ret
= twl_i2c_write_u8(TWL6030_MODULE_ID1
, TWL6030_GPADCR
,
953 TWL6030_REG_TOGGLE1
);
955 dev_err(pdev
, "error resetting GPADC (%d)!\n", ret
);
960 static int twl6030_gpadc_resume(struct device
*pdev
)
964 ret
= twl_i2c_write_u8(TWL6030_MODULE_ID1
, TWL6030_GPADCS
,
965 TWL6030_REG_TOGGLE1
);
967 dev_err(pdev
, "error setting GPADC (%d)!\n", ret
);
973 static SIMPLE_DEV_PM_OPS(twl6030_gpadc_pm_ops
, twl6030_gpadc_suspend
,
974 twl6030_gpadc_resume
);
976 static struct platform_driver twl6030_gpadc_driver
= {
977 .probe
= twl6030_gpadc_probe
,
978 .remove
= twl6030_gpadc_remove
,
981 .pm
= &twl6030_gpadc_pm_ops
,
982 .of_match_table
= of_twl6030_match_tbl
,
986 module_platform_driver(twl6030_gpadc_driver
);
988 MODULE_ALIAS("platform:" DRIVER_NAME
);
989 MODULE_AUTHOR("Balaji T K <balajitk@ti.com>");
990 MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
991 MODULE_AUTHOR("Oleksandr Kozaruk <oleksandr.kozaruk@ti.com");
992 MODULE_DESCRIPTION("twl6030 ADC driver");
993 MODULE_LICENSE("GPL");