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/init.h>
32 #include <linux/interrupt.h>
33 #include <linux/kernel.h>
34 #include <linux/module.h>
35 #include <linux/platform_device.h>
36 #include <linux/of_platform.h>
37 #include <linux/i2c/twl.h>
38 #include <linux/iio/iio.h>
39 #include <linux/iio/sysfs.h>
41 #define DRIVER_NAME "twl6030_gpadc"
44 * twl6030 per TRM has 17 channels, and twl6032 has 19 channels
45 * 2 test network channels are not used,
46 * 2 die temperature channels are not used either, as it is not
47 * defined how to convert ADC value to temperature
49 #define TWL6030_GPADC_USED_CHANNELS 13
50 #define TWL6030_GPADC_MAX_CHANNELS 15
51 #define TWL6032_GPADC_USED_CHANNELS 15
52 #define TWL6032_GPADC_MAX_CHANNELS 19
53 #define TWL6030_GPADC_NUM_TRIM_REGS 16
55 #define TWL6030_GPADC_CTRL_P1 0x05
57 #define TWL6032_GPADC_GPSELECT_ISB 0x07
58 #define TWL6032_GPADC_CTRL_P1 0x08
60 #define TWL6032_GPADC_GPCH0_LSB 0x0d
61 #define TWL6032_GPADC_GPCH0_MSB 0x0e
63 #define TWL6030_GPADC_CTRL_P1_SP1 BIT(3)
65 #define TWL6030_GPADC_GPCH0_LSB (0x29)
67 #define TWL6030_GPADC_RT_SW1_EOC_MASK BIT(5)
69 #define TWL6030_GPADC_TRIM1 0xCD
71 #define TWL6030_REG_TOGGLE1 0x90
72 #define TWL6030_GPADCS BIT(1)
73 #define TWL6030_GPADCR BIT(0)
76 * struct twl6030_chnl_calib - channel calibration
77 * @gain: slope coefficient for ideal curve
78 * @gain_error: gain error
79 * @offset_error: offset of the real curve
81 struct twl6030_chnl_calib
{
88 * struct twl6030_ideal_code - GPADC calibration parameters
89 * GPADC is calibrated in two points: close to the beginning and
90 * to the and of the measurable input range
92 * @channel: channel number
93 * @code1: ideal code for the input at the beginning
94 * @code2: ideal code for at the end of the range
95 * @volt1: voltage input at the beginning(low voltage)
96 * @volt2: voltage input at the end(high voltage)
98 struct twl6030_ideal_code
{
106 struct twl6030_gpadc_data
;
109 * struct twl6030_gpadc_platform_data - platform specific data
110 * @nchannels: number of GPADC channels
111 * @iio_channels: iio channels
112 * @twl6030_ideal: pointer to calibration parameters
113 * @start_conversion: pointer to ADC start conversion function
114 * @channel_to_reg pointer to ADC function to convert channel to
115 * register address for reading conversion result
116 * @calibrate: pointer to calibration function
118 struct twl6030_gpadc_platform_data
{
120 const struct iio_chan_spec
*iio_channels
;
121 const struct twl6030_ideal_code
*ideal
;
122 int (*start_conversion
)(int channel
);
123 u8 (*channel_to_reg
)(int channel
);
124 int (*calibrate
)(struct twl6030_gpadc_data
*gpadc
);
128 * struct twl6030_gpadc_data - GPADC data
129 * @dev: device pointer
130 * @lock: mutual exclusion lock for the structure
131 * @irq_complete: completion to signal end of conversion
132 * @twl6030_cal_tbl: pointer to calibration data for each
133 * channel with gain error and offset
134 * @pdata: pointer to device specific data
136 struct twl6030_gpadc_data
{
139 struct completion irq_complete
;
140 struct twl6030_chnl_calib
*twl6030_cal_tbl
;
141 const struct twl6030_gpadc_platform_data
*pdata
;
145 * channels 11, 12, 13, 15 and 16 have no calibration data
146 * calibration offset is same for channels 1, 3, 4, 5
148 * The data is taken from GPADC_TRIM registers description.
149 * GPADC_TRIM registers keep difference between the code measured
150 * at volt1 and volt2 input voltages and corresponding code1 and code2
152 static const struct twl6030_ideal_code
153 twl6030_ideal
[TWL6030_GPADC_USED_CHANNELS
] = {
154 [0] = { /* ch 0, external, battery type, resistor value */
161 [1] = { /* ch 1, external, battery temperature, NTC resistor value */
168 [2] = { /* ch 2, external, audio accessory/general purpose */
175 [3] = { /* ch 3, external, general purpose */
182 [4] = { /* ch 4, external, temperature measurement/general purpose */
189 [5] = { /* ch 5, external, general purpose */
196 [6] = { /* ch 6, external, general purpose */
203 [7] = { /* ch 7, internal, main battery */
210 [8] = { /* ch 8, internal, backup battery */
217 [9] = { /* ch 9, internal, external charger input */
224 [10] = { /* ch 10, internal, VBUS */
231 [11] = { /* ch 11, internal, VBUS charging current */
234 /* ch 12, internal, Die temperature */
235 /* ch 13, internal, Die temperature */
236 [12] = { /* ch 14, internal, USB ID line */
245 static const struct twl6030_ideal_code
246 twl6032_ideal
[TWL6032_GPADC_USED_CHANNELS
] = {
247 [0] = { /* ch 0, external, battery type, resistor value */
254 [1] = { /* ch 1, external, battery temperature, NTC resistor value */
261 [2] = { /* ch 2, external, audio accessory/general purpose */
268 [3] = { /* ch 3, external, temperature with external diode/general
276 [4] = { /* ch 4, external, temperature measurement/general purpose */
283 [5] = { /* ch 5, external, general purpose */
290 [6] = { /* ch 6, external, general purpose */
297 [7] = { /* ch7, internal, system supply */
304 [8] = { /* ch8, internal, backup battery */
311 [9] = { /* ch 9, internal, external charger input */
318 [10] = { /* ch10, internal, VBUS */
325 [11] = { /* ch 11, internal, VBUS DC-DC output current */
332 /* ch 12, internal, Die temperature */
333 /* ch 13, internal, Die temperature */
334 [12] = { /* ch 14, internal, USB ID line */
341 /* ch 15, internal, test network */
342 /* ch 16, internal, test network */
343 [13] = { /* ch 17, internal, battery charging current */
346 [14] = { /* ch 18, internal, battery voltage */
355 static inline int twl6030_gpadc_write(u8 reg
, u8 val
)
357 return twl_i2c_write_u8(TWL6030_MODULE_GPADC
, val
, reg
);
360 static inline int twl6030_gpadc_read(u8 reg
, u8
*val
)
363 return twl_i2c_read(TWL6030_MODULE_GPADC
, val
, reg
, 2);
366 static int twl6030_gpadc_enable_irq(u8 mask
)
370 ret
= twl6030_interrupt_unmask(mask
, REG_INT_MSK_LINE_B
);
374 ret
= twl6030_interrupt_unmask(mask
, REG_INT_MSK_STS_B
);
379 static void twl6030_gpadc_disable_irq(u8 mask
)
381 twl6030_interrupt_mask(mask
, REG_INT_MSK_LINE_B
);
382 twl6030_interrupt_mask(mask
, REG_INT_MSK_STS_B
);
385 static irqreturn_t
twl6030_gpadc_irq_handler(int irq
, void *indio_dev
)
387 struct twl6030_gpadc_data
*gpadc
= iio_priv(indio_dev
);
389 complete(&gpadc
->irq_complete
);
394 static int twl6030_start_conversion(int channel
)
396 return twl6030_gpadc_write(TWL6030_GPADC_CTRL_P1
,
397 TWL6030_GPADC_CTRL_P1_SP1
);
400 static int twl6032_start_conversion(int channel
)
404 ret
= twl6030_gpadc_write(TWL6032_GPADC_GPSELECT_ISB
, channel
);
408 return twl6030_gpadc_write(TWL6032_GPADC_CTRL_P1
,
409 TWL6030_GPADC_CTRL_P1_SP1
);
412 static u8
twl6030_channel_to_reg(int channel
)
414 return TWL6030_GPADC_GPCH0_LSB
+ 2 * channel
;
417 static u8
twl6032_channel_to_reg(int channel
)
420 * for any prior chosen channel, when the conversion is ready
421 * the result is avalable in GPCH0_LSB, GPCH0_MSB.
424 return TWL6032_GPADC_GPCH0_LSB
;
427 static int twl6030_gpadc_lookup(const struct twl6030_ideal_code
*ideal
,
428 int channel
, int size
)
432 for (i
= 0; i
< size
; i
++)
433 if (ideal
[i
].channel
== channel
)
439 static int twl6030_channel_calibrated(const struct twl6030_gpadc_platform_data
442 const struct twl6030_ideal_code
*ideal
= pdata
->ideal
;
445 i
= twl6030_gpadc_lookup(ideal
, channel
, pdata
->nchannels
);
446 /* not calibrated channels have 0 in all structure members */
447 return pdata
->ideal
[i
].code2
;
450 static int twl6030_gpadc_make_correction(struct twl6030_gpadc_data
*gpadc
,
451 int channel
, int raw_code
)
453 const struct twl6030_ideal_code
*ideal
= gpadc
->pdata
->ideal
;
457 i
= twl6030_gpadc_lookup(ideal
, channel
, gpadc
->pdata
->nchannels
);
458 corrected_code
= ((raw_code
* 1000) -
459 gpadc
->twl6030_cal_tbl
[i
].offset_error
) /
460 gpadc
->twl6030_cal_tbl
[i
].gain_error
;
462 return corrected_code
;
465 static int twl6030_gpadc_get_raw(struct twl6030_gpadc_data
*gpadc
,
466 int channel
, int *res
)
468 u8 reg
= gpadc
->pdata
->channel_to_reg(channel
);
473 ret
= twl6030_gpadc_read(reg
, (u8
*)&val
);
475 dev_dbg(gpadc
->dev
, "unable to read register 0x%X\n", reg
);
479 raw_code
= le16_to_cpu(val
);
480 dev_dbg(gpadc
->dev
, "GPADC raw code: %d", raw_code
);
482 if (twl6030_channel_calibrated(gpadc
->pdata
, channel
))
483 *res
= twl6030_gpadc_make_correction(gpadc
, channel
, raw_code
);
490 static int twl6030_gpadc_get_processed(struct twl6030_gpadc_data
*gpadc
,
491 int channel
, int *val
)
493 const struct twl6030_ideal_code
*ideal
= gpadc
->pdata
->ideal
;
499 ret
= twl6030_gpadc_get_raw(gpadc
, channel
, &corrected_code
);
503 i
= twl6030_gpadc_lookup(ideal
, channel
, gpadc
->pdata
->nchannels
);
504 channel_value
= corrected_code
*
505 gpadc
->twl6030_cal_tbl
[i
].gain
;
507 /* Shift back into mV range */
508 channel_value
/= 1000;
510 dev_dbg(gpadc
->dev
, "GPADC corrected code: %d", corrected_code
);
511 dev_dbg(gpadc
->dev
, "GPADC value: %d", channel_value
);
513 *val
= channel_value
;
518 static int twl6030_gpadc_read_raw(struct iio_dev
*indio_dev
,
519 const struct iio_chan_spec
*chan
,
520 int *val
, int *val2
, long mask
)
522 struct twl6030_gpadc_data
*gpadc
= iio_priv(indio_dev
);
526 mutex_lock(&gpadc
->lock
);
528 ret
= gpadc
->pdata
->start_conversion(chan
->channel
);
530 dev_err(gpadc
->dev
, "failed to start conversion\n");
533 /* wait for conversion to complete */
534 timeout
= wait_for_completion_interruptible_timeout(
535 &gpadc
->irq_complete
, msecs_to_jiffies(5000));
539 } else if (timeout
< 0) {
545 case IIO_CHAN_INFO_RAW
:
546 ret
= twl6030_gpadc_get_raw(gpadc
, chan
->channel
, val
);
547 ret
= ret
? -EIO
: IIO_VAL_INT
;
550 case IIO_CHAN_INFO_PROCESSED
:
551 ret
= twl6030_gpadc_get_processed(gpadc
, chan
->channel
, val
);
552 ret
= ret
? -EIO
: IIO_VAL_INT
;
559 mutex_unlock(&gpadc
->lock
);
565 * The GPADC channels are calibrated using a two point calibration method.
566 * The channels measured with two known values: volt1 and volt2, and
567 * ideal corresponding output codes are known: code1, code2.
568 * The difference(d1, d2) between ideal and measured codes stored in trim
570 * The goal is to find offset and gain of the real curve for each calibrated
572 * gain: k = 1 + ((d2 - d1) / (x2 - x1))
573 * offset: b = d1 + (k - 1) * x1
575 static void twl6030_calibrate_channel(struct twl6030_gpadc_data
*gpadc
,
576 int channel
, int d1
, int d2
)
578 int b
, k
, gain
, x1
, x2
, i
;
579 const struct twl6030_ideal_code
*ideal
= gpadc
->pdata
->ideal
;
581 i
= twl6030_gpadc_lookup(ideal
, channel
, gpadc
->pdata
->nchannels
);
584 gain
= ((ideal
[i
].volt2
- ideal
[i
].volt1
) * 1000) /
585 (ideal
[i
].code2
- ideal
[i
].code1
);
590 /* k - real curve gain */
591 k
= 1000 + (((d2
- d1
) * 1000) / (x2
- x1
));
593 /* b - offset of the real curve gain */
594 b
= (d1
* 1000) - (k
- 1000) * x1
;
596 gpadc
->twl6030_cal_tbl
[i
].gain
= gain
;
597 gpadc
->twl6030_cal_tbl
[i
].gain_error
= k
;
598 gpadc
->twl6030_cal_tbl
[i
].offset_error
= b
;
600 dev_dbg(gpadc
->dev
, "GPADC d1 for Chn: %d = %d\n", channel
, d1
);
601 dev_dbg(gpadc
->dev
, "GPADC d2 for Chn: %d = %d\n", channel
, d2
);
602 dev_dbg(gpadc
->dev
, "GPADC x1 for Chn: %d = %d\n", channel
, x1
);
603 dev_dbg(gpadc
->dev
, "GPADC x2 for Chn: %d = %d\n", channel
, x2
);
604 dev_dbg(gpadc
->dev
, "GPADC Gain for Chn: %d = %d\n", channel
, gain
);
605 dev_dbg(gpadc
->dev
, "GPADC k for Chn: %d = %d\n", channel
, k
);
606 dev_dbg(gpadc
->dev
, "GPADC b for Chn: %d = %d\n", channel
, b
);
609 static inline int twl6030_gpadc_get_trim_offset(s8 d
)
613 * bit 0 - sign, bit 7 - reserved, 6..1 - trim value
614 * though, the documentation states that trim value
615 * is absolute value, the correct conversion results are
616 * obtained if the value is interpreted as 2's complement.
618 __u32 temp
= ((d
& 0x7f) >> 1) | ((d
& 1) << 6);
620 return sign_extend32(temp
, 6);
623 static int twl6030_calibration(struct twl6030_gpadc_data
*gpadc
)
627 u8 trim_regs
[TWL6030_GPADC_NUM_TRIM_REGS
];
631 * for calibration two measurements have been performed at
632 * factory, for some channels, during the production test and
633 * have been stored in registers. This two stored values are
634 * used to correct the measurements. The values represent
635 * offsets for the given input from the output on ideal curve.
637 ret
= twl_i2c_read(TWL6030_MODULE_ID2
, trim_regs
,
638 TWL6030_GPADC_TRIM1
, TWL6030_GPADC_NUM_TRIM_REGS
);
640 dev_err(gpadc
->dev
, "calibration failed\n");
644 for (chn
= 0; chn
< TWL6030_GPADC_MAX_CHANNELS
; chn
++) {
687 d1
= twl6030_gpadc_get_trim_offset(d1
);
688 d2
= twl6030_gpadc_get_trim_offset(d2
);
690 twl6030_calibrate_channel(gpadc
, chn
, d1
, d2
);
696 static int twl6032_get_trim_value(u8
*trim_regs
, unsigned int reg0
,
697 unsigned int reg1
, unsigned int mask0
, unsigned int mask1
,
702 val
= (trim_regs
[reg0
] & mask0
) << shift0
;
703 val
|= (trim_regs
[reg1
] & mask1
) >> 1;
704 if (trim_regs
[reg1
] & 0x01)
710 static int twl6032_calibration(struct twl6030_gpadc_data
*gpadc
)
712 int chn
, d1
= 0, d2
= 0, temp
;
713 u8 trim_regs
[TWL6030_GPADC_NUM_TRIM_REGS
];
716 ret
= twl_i2c_read(TWL6030_MODULE_ID2
, trim_regs
,
717 TWL6030_GPADC_TRIM1
, TWL6030_GPADC_NUM_TRIM_REGS
);
719 dev_err(gpadc
->dev
, "calibration failed\n");
724 * Loop to calculate the value needed for returning voltages from
727 * gain is calculated to 3 decimal places fixed point.
729 for (chn
= 0; chn
< TWL6032_GPADC_MAX_CHANNELS
; chn
++) {
741 d1
= twl6032_get_trim_value(trim_regs
, 2, 0, 0x1f,
743 d2
= twl6032_get_trim_value(trim_regs
, 3, 1, 0x3f,
747 temp
= twl6032_get_trim_value(trim_regs
, 2, 0, 0x1f,
749 d1
= temp
+ twl6032_get_trim_value(trim_regs
, 7, 6,
752 temp
= twl6032_get_trim_value(trim_regs
, 3, 1, 0x3F,
754 d2
= temp
+ twl6032_get_trim_value(trim_regs
, 9, 7,
758 temp
= twl6032_get_trim_value(trim_regs
, 2, 0, 0x1f,
760 d1
= temp
+ twl6032_get_trim_value(trim_regs
, 13, 11,
763 temp
= twl6032_get_trim_value(trim_regs
, 3, 1, 0x3f,
765 d2
= temp
+ twl6032_get_trim_value(trim_regs
, 15, 13,
769 d1
= twl6032_get_trim_value(trim_regs
, 10, 8, 0x0f,
771 d2
= twl6032_get_trim_value(trim_regs
, 14, 12, 0x0f,
776 temp
= twl6032_get_trim_value(trim_regs
, 2, 0, 0x1f,
779 d1
= (trim_regs
[4] & 0x7E) >> 1;
780 if (trim_regs
[4] & 0x01)
784 temp
= twl6032_get_trim_value(trim_regs
, 3, 1, 0x3f,
787 d2
= (trim_regs
[5] & 0xFE) >> 1;
788 if (trim_regs
[5] & 0x01)
794 /* No data for other channels */
798 twl6030_calibrate_channel(gpadc
, chn
, d1
, d2
);
804 #define TWL6030_GPADC_CHAN(chn, _type, chan_info) { \
807 .info_mask_separate = BIT(chan_info), \
811 static const struct iio_chan_spec twl6030_gpadc_iio_channels
[] = {
812 TWL6030_GPADC_CHAN(0, IIO_VOLTAGE
, IIO_CHAN_INFO_PROCESSED
),
813 TWL6030_GPADC_CHAN(1, IIO_TEMP
, IIO_CHAN_INFO_RAW
),
814 TWL6030_GPADC_CHAN(2, IIO_VOLTAGE
, IIO_CHAN_INFO_PROCESSED
),
815 TWL6030_GPADC_CHAN(3, IIO_VOLTAGE
, IIO_CHAN_INFO_PROCESSED
),
816 TWL6030_GPADC_CHAN(4, IIO_TEMP
, IIO_CHAN_INFO_RAW
),
817 TWL6030_GPADC_CHAN(5, IIO_VOLTAGE
, IIO_CHAN_INFO_PROCESSED
),
818 TWL6030_GPADC_CHAN(6, IIO_VOLTAGE
, IIO_CHAN_INFO_PROCESSED
),
819 TWL6030_GPADC_CHAN(7, IIO_VOLTAGE
, IIO_CHAN_INFO_PROCESSED
),
820 TWL6030_GPADC_CHAN(8, IIO_VOLTAGE
, IIO_CHAN_INFO_PROCESSED
),
821 TWL6030_GPADC_CHAN(9, IIO_VOLTAGE
, IIO_CHAN_INFO_PROCESSED
),
822 TWL6030_GPADC_CHAN(10, IIO_VOLTAGE
, IIO_CHAN_INFO_PROCESSED
),
823 TWL6030_GPADC_CHAN(11, IIO_VOLTAGE
, IIO_CHAN_INFO_RAW
),
824 TWL6030_GPADC_CHAN(14, IIO_VOLTAGE
, IIO_CHAN_INFO_PROCESSED
),
827 static const struct iio_chan_spec twl6032_gpadc_iio_channels
[] = {
828 TWL6030_GPADC_CHAN(0, IIO_VOLTAGE
, IIO_CHAN_INFO_PROCESSED
),
829 TWL6030_GPADC_CHAN(1, IIO_TEMP
, IIO_CHAN_INFO_RAW
),
830 TWL6030_GPADC_CHAN(2, IIO_VOLTAGE
, IIO_CHAN_INFO_PROCESSED
),
831 TWL6030_GPADC_CHAN(3, IIO_VOLTAGE
, IIO_CHAN_INFO_PROCESSED
),
832 TWL6030_GPADC_CHAN(4, IIO_TEMP
, IIO_CHAN_INFO_RAW
),
833 TWL6030_GPADC_CHAN(5, IIO_VOLTAGE
, IIO_CHAN_INFO_PROCESSED
),
834 TWL6030_GPADC_CHAN(6, IIO_VOLTAGE
, IIO_CHAN_INFO_PROCESSED
),
835 TWL6030_GPADC_CHAN(7, IIO_VOLTAGE
, IIO_CHAN_INFO_PROCESSED
),
836 TWL6030_GPADC_CHAN(8, IIO_VOLTAGE
, IIO_CHAN_INFO_PROCESSED
),
837 TWL6030_GPADC_CHAN(9, IIO_VOLTAGE
, IIO_CHAN_INFO_PROCESSED
),
838 TWL6030_GPADC_CHAN(10, IIO_VOLTAGE
, IIO_CHAN_INFO_PROCESSED
),
839 TWL6030_GPADC_CHAN(11, IIO_VOLTAGE
, IIO_CHAN_INFO_PROCESSED
),
840 TWL6030_GPADC_CHAN(14, IIO_VOLTAGE
, IIO_CHAN_INFO_PROCESSED
),
841 TWL6030_GPADC_CHAN(17, IIO_VOLTAGE
, IIO_CHAN_INFO_RAW
),
842 TWL6030_GPADC_CHAN(18, IIO_VOLTAGE
, IIO_CHAN_INFO_PROCESSED
),
845 static const struct iio_info twl6030_gpadc_iio_info
= {
846 .read_raw
= &twl6030_gpadc_read_raw
,
847 .driver_module
= THIS_MODULE
,
850 static const struct twl6030_gpadc_platform_data twl6030_pdata
= {
851 .iio_channels
= twl6030_gpadc_iio_channels
,
852 .nchannels
= TWL6030_GPADC_USED_CHANNELS
,
853 .ideal
= twl6030_ideal
,
854 .start_conversion
= twl6030_start_conversion
,
855 .channel_to_reg
= twl6030_channel_to_reg
,
856 .calibrate
= twl6030_calibration
,
859 static const struct twl6030_gpadc_platform_data twl6032_pdata
= {
860 .iio_channels
= twl6032_gpadc_iio_channels
,
861 .nchannels
= TWL6032_GPADC_USED_CHANNELS
,
862 .ideal
= twl6032_ideal
,
863 .start_conversion
= twl6032_start_conversion
,
864 .channel_to_reg
= twl6032_channel_to_reg
,
865 .calibrate
= twl6032_calibration
,
868 static const struct of_device_id of_twl6030_match_tbl
[] = {
870 .compatible
= "ti,twl6030-gpadc",
871 .data
= &twl6030_pdata
,
874 .compatible
= "ti,twl6032-gpadc",
875 .data
= &twl6032_pdata
,
880 static int twl6030_gpadc_probe(struct platform_device
*pdev
)
882 struct device
*dev
= &pdev
->dev
;
883 struct twl6030_gpadc_data
*gpadc
;
884 const struct twl6030_gpadc_platform_data
*pdata
;
885 const struct of_device_id
*match
;
886 struct iio_dev
*indio_dev
;
890 match
= of_match_device(of_twl6030_match_tbl
, dev
);
896 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*gpadc
));
900 gpadc
= iio_priv(indio_dev
);
902 gpadc
->twl6030_cal_tbl
= devm_kzalloc(dev
,
903 sizeof(*gpadc
->twl6030_cal_tbl
) *
904 pdata
->nchannels
, GFP_KERNEL
);
905 if (!gpadc
->twl6030_cal_tbl
)
909 gpadc
->pdata
= pdata
;
911 platform_set_drvdata(pdev
, indio_dev
);
912 mutex_init(&gpadc
->lock
);
913 init_completion(&gpadc
->irq_complete
);
915 ret
= pdata
->calibrate(gpadc
);
917 dev_err(&pdev
->dev
, "failed to read calibration registers\n");
921 irq
= platform_get_irq(pdev
, 0);
923 dev_err(&pdev
->dev
, "failed to get irq\n");
927 ret
= devm_request_threaded_irq(dev
, irq
, NULL
,
928 twl6030_gpadc_irq_handler
,
929 IRQF_ONESHOT
, "twl6030_gpadc", indio_dev
);
931 ret
= twl6030_gpadc_enable_irq(TWL6030_GPADC_RT_SW1_EOC_MASK
);
933 dev_err(&pdev
->dev
, "failed to enable GPADC interrupt\n");
937 ret
= twl_i2c_write_u8(TWL6030_MODULE_ID1
, TWL6030_GPADCS
,
938 TWL6030_REG_TOGGLE1
);
940 dev_err(&pdev
->dev
, "failed to enable GPADC module\n");
944 indio_dev
->name
= DRIVER_NAME
;
945 indio_dev
->dev
.parent
= dev
;
946 indio_dev
->info
= &twl6030_gpadc_iio_info
;
947 indio_dev
->modes
= INDIO_DIRECT_MODE
;
948 indio_dev
->channels
= pdata
->iio_channels
;
949 indio_dev
->num_channels
= pdata
->nchannels
;
951 return iio_device_register(indio_dev
);
954 static int twl6030_gpadc_remove(struct platform_device
*pdev
)
956 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
958 twl6030_gpadc_disable_irq(TWL6030_GPADC_RT_SW1_EOC_MASK
);
959 iio_device_unregister(indio_dev
);
964 #ifdef CONFIG_PM_SLEEP
965 static int twl6030_gpadc_suspend(struct device
*pdev
)
969 ret
= twl_i2c_write_u8(TWL6030_MODULE_ID1
, TWL6030_GPADCR
,
970 TWL6030_REG_TOGGLE1
);
972 dev_err(pdev
, "error resetting GPADC (%d)!\n", ret
);
977 static int twl6030_gpadc_resume(struct device
*pdev
)
981 ret
= twl_i2c_write_u8(TWL6030_MODULE_ID1
, TWL6030_GPADCS
,
982 TWL6030_REG_TOGGLE1
);
984 dev_err(pdev
, "error setting GPADC (%d)!\n", ret
);
990 static SIMPLE_DEV_PM_OPS(twl6030_gpadc_pm_ops
, twl6030_gpadc_suspend
,
991 twl6030_gpadc_resume
);
993 static struct platform_driver twl6030_gpadc_driver
= {
994 .probe
= twl6030_gpadc_probe
,
995 .remove
= twl6030_gpadc_remove
,
998 .owner
= THIS_MODULE
,
999 .pm
= &twl6030_gpadc_pm_ops
,
1000 .of_match_table
= of_twl6030_match_tbl
,
1004 module_platform_driver(twl6030_gpadc_driver
);
1006 MODULE_ALIAS("platform: " DRIVER_NAME
);
1007 MODULE_AUTHOR("Balaji T K <balajitk@ti.com>");
1008 MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
1009 MODULE_AUTHOR("Oleksandr Kozaruk <oleksandr.kozaruk@ti.com");
1010 MODULE_DESCRIPTION("twl6030 ADC driver");
1011 MODULE_LICENSE("GPL");