Merge tag 'regmap-fix-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / drivers / iio / adc / twl6030-gpadc.c
blobc6416ad795ca48ae2a24cd02c8ecd5e8d4fb74e3
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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)
60 /**
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 {
67 s32 gain;
68 s32 gain_error;
69 s32 offset_error;
72 /**
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 {
84 int channel;
85 u16 code1;
86 u16 code2;
87 u16 volt1;
88 u16 volt2;
91 struct twl6030_gpadc_data;
93 /**
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 {
104 const int nchannels;
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 {
122 struct device *dev;
123 struct mutex lock;
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 */
140 .channel = 0,
141 .code1 = 116,
142 .code2 = 745,
143 .volt1 = 141,
144 .volt2 = 910,
146 [1] = { /* ch 1, external, battery temperature, NTC resistor value */
147 .channel = 1,
148 .code1 = 82,
149 .code2 = 900,
150 .volt1 = 100,
151 .volt2 = 1100,
153 [2] = { /* ch 2, external, audio accessory/general purpose */
154 .channel = 2,
155 .code1 = 55,
156 .code2 = 818,
157 .volt1 = 101,
158 .volt2 = 1499,
160 [3] = { /* ch 3, external, general purpose */
161 .channel = 3,
162 .code1 = 82,
163 .code2 = 900,
164 .volt1 = 100,
165 .volt2 = 1100,
167 [4] = { /* ch 4, external, temperature measurement/general purpose */
168 .channel = 4,
169 .code1 = 82,
170 .code2 = 900,
171 .volt1 = 100,
172 .volt2 = 1100,
174 [5] = { /* ch 5, external, general purpose */
175 .channel = 5,
176 .code1 = 82,
177 .code2 = 900,
178 .volt1 = 100,
179 .volt2 = 1100,
181 [6] = { /* ch 6, external, general purpose */
182 .channel = 6,
183 .code1 = 82,
184 .code2 = 900,
185 .volt1 = 100,
186 .volt2 = 1100,
188 [7] = { /* ch 7, internal, main battery */
189 .channel = 7,
190 .code1 = 614,
191 .code2 = 941,
192 .volt1 = 3001,
193 .volt2 = 4599,
195 [8] = { /* ch 8, internal, backup battery */
196 .channel = 8,
197 .code1 = 82,
198 .code2 = 688,
199 .volt1 = 501,
200 .volt2 = 4203,
202 [9] = { /* ch 9, internal, external charger input */
203 .channel = 9,
204 .code1 = 182,
205 .code2 = 818,
206 .volt1 = 2001,
207 .volt2 = 8996,
209 [10] = { /* ch 10, internal, VBUS */
210 .channel = 10,
211 .code1 = 149,
212 .code2 = 818,
213 .volt1 = 1001,
214 .volt2 = 5497,
216 [11] = { /* ch 11, internal, VBUS charging current */
217 .channel = 11,
219 /* ch 12, internal, Die temperature */
220 /* ch 13, internal, Die temperature */
221 [12] = { /* ch 14, internal, USB ID line */
222 .channel = 14,
223 .code1 = 48,
224 .code2 = 714,
225 .volt1 = 323,
226 .volt2 = 4800,
230 static const struct twl6030_ideal_code
231 twl6032_ideal[TWL6032_GPADC_USED_CHANNELS] = {
232 [0] = { /* ch 0, external, battery type, resistor value */
233 .channel = 0,
234 .code1 = 1441,
235 .code2 = 3276,
236 .volt1 = 440,
237 .volt2 = 1000,
239 [1] = { /* ch 1, external, battery temperature, NTC resistor value */
240 .channel = 1,
241 .code1 = 1441,
242 .code2 = 3276,
243 .volt1 = 440,
244 .volt2 = 1000,
246 [2] = { /* ch 2, external, audio accessory/general purpose */
247 .channel = 2,
248 .code1 = 1441,
249 .code2 = 3276,
250 .volt1 = 660,
251 .volt2 = 1500,
253 [3] = { /* ch 3, external, temperature with external diode/general
254 purpose */
255 .channel = 3,
256 .code1 = 1441,
257 .code2 = 3276,
258 .volt1 = 440,
259 .volt2 = 1000,
261 [4] = { /* ch 4, external, temperature measurement/general purpose */
262 .channel = 4,
263 .code1 = 1441,
264 .code2 = 3276,
265 .volt1 = 440,
266 .volt2 = 1000,
268 [5] = { /* ch 5, external, general purpose */
269 .channel = 5,
270 .code1 = 1441,
271 .code2 = 3276,
272 .volt1 = 440,
273 .volt2 = 1000,
275 [6] = { /* ch 6, external, general purpose */
276 .channel = 6,
277 .code1 = 1441,
278 .code2 = 3276,
279 .volt1 = 440,
280 .volt2 = 1000,
282 [7] = { /* ch7, internal, system supply */
283 .channel = 7,
284 .code1 = 1441,
285 .code2 = 3276,
286 .volt1 = 2200,
287 .volt2 = 5000,
289 [8] = { /* ch8, internal, backup battery */
290 .channel = 8,
291 .code1 = 1441,
292 .code2 = 3276,
293 .volt1 = 2200,
294 .volt2 = 5000,
296 [9] = { /* ch 9, internal, external charger input */
297 .channel = 9,
298 .code1 = 1441,
299 .code2 = 3276,
300 .volt1 = 3960,
301 .volt2 = 9000,
303 [10] = { /* ch10, internal, VBUS */
304 .channel = 10,
305 .code1 = 150,
306 .code2 = 751,
307 .volt1 = 1000,
308 .volt2 = 5000,
310 [11] = { /* ch 11, internal, VBUS DC-DC output current */
311 .channel = 11,
312 .code1 = 1441,
313 .code2 = 3276,
314 .volt1 = 660,
315 .volt2 = 1500,
317 /* ch 12, internal, Die temperature */
318 /* ch 13, internal, Die temperature */
319 [12] = { /* ch 14, internal, USB ID line */
320 .channel = 14,
321 .code1 = 1441,
322 .code2 = 3276,
323 .volt1 = 2420,
324 .volt2 = 5500,
326 /* ch 15, internal, test network */
327 /* ch 16, internal, test network */
328 [13] = { /* ch 17, internal, battery charging current */
329 .channel = 17,
331 [14] = { /* ch 18, internal, battery voltage */
332 .channel = 18,
333 .code1 = 1441,
334 .code2 = 3276,
335 .volt1 = 2200,
336 .volt2 = 5000,
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)
353 int ret;
355 ret = twl6030_interrupt_unmask(mask, REG_INT_MSK_LINE_B);
356 if (ret < 0)
357 return ret;
359 ret = twl6030_interrupt_unmask(mask, REG_INT_MSK_STS_B);
361 return ret;
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);
376 return IRQ_HANDLED;
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)
387 int ret;
389 ret = twl6030_gpadc_write(TWL6032_GPADC_GPSELECT_ISB, channel);
390 if (ret)
391 return ret;
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)
415 int i;
417 for (i = 0; i < size; i++)
418 if (ideal[i].channel == channel)
419 break;
421 return i;
424 static int twl6030_channel_calibrated(const struct twl6030_gpadc_platform_data
425 *pdata, int channel)
427 const struct twl6030_ideal_code *ideal = pdata->ideal;
428 int i;
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;
439 int corrected_code;
440 int i;
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);
454 __le16 val;
455 int raw_code;
456 int ret;
458 ret = twl6030_gpadc_read(reg, (u8 *)&val);
459 if (ret) {
460 dev_dbg(gpadc->dev, "unable to read register 0x%X\n", reg);
461 return ret;
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);
469 else
470 *res = raw_code;
472 return ret;
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;
479 int corrected_code;
480 int channel_value;
481 int i;
482 int ret;
484 ret = twl6030_gpadc_get_raw(gpadc, channel, &corrected_code);
485 if (ret)
486 return ret;
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;
500 return ret;
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);
508 int ret;
509 long timeout;
511 mutex_lock(&gpadc->lock);
513 ret = gpadc->pdata->start_conversion(chan->channel);
514 if (ret) {
515 dev_err(gpadc->dev, "failed to start conversion\n");
516 goto err;
518 /* wait for conversion to complete */
519 timeout = wait_for_completion_interruptible_timeout(
520 &gpadc->irq_complete, msecs_to_jiffies(5000));
521 if (timeout == 0) {
522 ret = -ETIMEDOUT;
523 goto err;
524 } else if (timeout < 0) {
525 ret = -EINTR;
526 goto err;
529 switch (mask) {
530 case IIO_CHAN_INFO_RAW:
531 ret = twl6030_gpadc_get_raw(gpadc, chan->channel, val);
532 ret = ret ? -EIO : IIO_VAL_INT;
533 break;
535 case IIO_CHAN_INFO_PROCESSED:
536 ret = twl6030_gpadc_get_processed(gpadc, chan->channel, val);
537 ret = ret ? -EIO : IIO_VAL_INT;
538 break;
540 default:
541 break;
543 err:
544 mutex_unlock(&gpadc->lock);
546 return ret;
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
554 * registers.
555 * The goal is to find offset and gain of the real curve for each calibrated
556 * channel.
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);
568 /* Gain */
569 gain = ((ideal[i].volt2 - ideal[i].volt1) * 1000) /
570 (ideal[i].code2 - ideal[i].code1);
572 x1 = ideal[i].code1;
573 x2 = ideal[i].code2;
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)
597 * XXX NOTE!
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)
610 int ret;
611 int chn;
612 u8 trim_regs[TWL6030_GPADC_NUM_TRIM_REGS];
613 s8 d1, d2;
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);
624 if (ret < 0) {
625 dev_err(gpadc->dev, "calibration failed\n");
626 return ret;
629 for (chn = 0; chn < TWL6030_GPADC_MAX_CHANNELS; chn++) {
631 switch (chn) {
632 case 0:
633 d1 = trim_regs[0];
634 d2 = trim_regs[1];
635 break;
636 case 1:
637 case 3:
638 case 4:
639 case 5:
640 case 6:
641 d1 = trim_regs[4];
642 d2 = trim_regs[5];
643 break;
644 case 2:
645 d1 = trim_regs[12];
646 d2 = trim_regs[13];
647 break;
648 case 7:
649 d1 = trim_regs[6];
650 d2 = trim_regs[7];
651 break;
652 case 8:
653 d1 = trim_regs[2];
654 d2 = trim_regs[3];
655 break;
656 case 9:
657 d1 = trim_regs[8];
658 d2 = trim_regs[9];
659 break;
660 case 10:
661 d1 = trim_regs[10];
662 d2 = trim_regs[11];
663 break;
664 case 14:
665 d1 = trim_regs[14];
666 d2 = trim_regs[15];
667 break;
668 default:
669 continue;
672 d1 = twl6030_gpadc_get_trim_offset(d1);
673 d2 = twl6030_gpadc_get_trim_offset(d2);
675 twl6030_calibrate_channel(gpadc, chn, d1, d2);
678 return 0;
681 static int twl6032_get_trim_value(u8 *trim_regs, unsigned int reg0,
682 unsigned int reg1, unsigned int mask0, unsigned int mask1,
683 unsigned int shift0)
685 int val;
687 val = (trim_regs[reg0] & mask0) << shift0;
688 val |= (trim_regs[reg1] & mask1) >> 1;
689 if (trim_regs[reg1] & 0x01)
690 val = -val;
692 return val;
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];
699 int ret;
701 ret = twl_i2c_read(TWL6030_MODULE_ID2, trim_regs,
702 TWL6030_GPADC_TRIM1, TWL6030_GPADC_NUM_TRIM_REGS);
703 if (ret < 0) {
704 dev_err(gpadc->dev, "calibration failed\n");
705 return ret;
709 * Loop to calculate the value needed for returning voltages from
710 * GPADC not values.
712 * gain is calculated to 3 decimal places fixed point.
714 for (chn = 0; chn < TWL6032_GPADC_MAX_CHANNELS; chn++) {
716 switch (chn) {
717 case 0:
718 case 1:
719 case 2:
720 case 3:
721 case 4:
722 case 5:
723 case 6:
724 case 11:
725 case 14:
726 d1 = twl6032_get_trim_value(trim_regs, 2, 0, 0x1f,
727 0x06, 2);
728 d2 = twl6032_get_trim_value(trim_regs, 3, 1, 0x3f,
729 0x06, 2);
730 break;
731 case 8:
732 temp = twl6032_get_trim_value(trim_regs, 2, 0, 0x1f,
733 0x06, 2);
734 d1 = temp + twl6032_get_trim_value(trim_regs, 7, 6,
735 0x18, 0x1E, 1);
737 temp = twl6032_get_trim_value(trim_regs, 3, 1, 0x3F,
738 0x06, 2);
739 d2 = temp + twl6032_get_trim_value(trim_regs, 9, 7,
740 0x1F, 0x06, 2);
741 break;
742 case 9:
743 temp = twl6032_get_trim_value(trim_regs, 2, 0, 0x1f,
744 0x06, 2);
745 d1 = temp + twl6032_get_trim_value(trim_regs, 13, 11,
746 0x18, 0x1E, 1);
748 temp = twl6032_get_trim_value(trim_regs, 3, 1, 0x3f,
749 0x06, 2);
750 d2 = temp + twl6032_get_trim_value(trim_regs, 15, 13,
751 0x1F, 0x06, 1);
752 break;
753 case 10:
754 d1 = twl6032_get_trim_value(trim_regs, 10, 8, 0x0f,
755 0x0E, 3);
756 d2 = twl6032_get_trim_value(trim_regs, 14, 12, 0x0f,
757 0x0E, 3);
758 break;
759 case 7:
760 case 18:
761 temp = twl6032_get_trim_value(trim_regs, 2, 0, 0x1f,
762 0x06, 2);
764 d1 = (trim_regs[4] & 0x7E) >> 1;
765 if (trim_regs[4] & 0x01)
766 d1 = -d1;
767 d1 += temp;
769 temp = twl6032_get_trim_value(trim_regs, 3, 1, 0x3f,
770 0x06, 2);
772 d2 = (trim_regs[5] & 0xFE) >> 1;
773 if (trim_regs[5] & 0x01)
774 d2 = -d2;
776 d2 += temp;
777 break;
778 default:
779 /* No data for other channels */
780 continue;
783 twl6030_calibrate_channel(gpadc, chn, d1, d2);
786 return 0;
789 #define TWL6030_GPADC_CHAN(chn, _type, chan_info) { \
790 .type = _type, \
791 .channel = chn, \
792 .info_mask_separate = BIT(chan_info), \
793 .indexed = 1, \
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,
861 { /* end */ }
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;
872 int irq;
873 int ret;
875 match = of_match_device(of_twl6030_match_tbl, dev);
876 if (!match)
877 return -EINVAL;
879 pdata = match->data;
881 indio_dev = devm_iio_device_alloc(dev, sizeof(*gpadc));
882 if (!indio_dev)
883 return -ENOMEM;
885 gpadc = iio_priv(indio_dev);
887 gpadc->twl6030_cal_tbl = devm_kcalloc(dev,
888 pdata->nchannels,
889 sizeof(*gpadc->twl6030_cal_tbl),
890 GFP_KERNEL);
891 if (!gpadc->twl6030_cal_tbl)
892 return -ENOMEM;
894 gpadc->dev = dev;
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);
902 if (ret < 0) {
903 dev_err(&pdev->dev, "failed to read calibration registers\n");
904 return ret;
907 irq = platform_get_irq(pdev, 0);
908 if (irq < 0)
909 return irq;
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);
916 if (ret < 0) {
917 dev_err(&pdev->dev, "failed to enable GPADC interrupt\n");
918 return ret;
921 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, TWL6030_GPADCS,
922 TWL6030_REG_TOGGLE1);
923 if (ret < 0) {
924 dev_err(&pdev->dev, "failed to enable GPADC module\n");
925 return ret;
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);
944 return 0;
947 #ifdef CONFIG_PM_SLEEP
948 static int twl6030_gpadc_suspend(struct device *pdev)
950 int ret;
952 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, TWL6030_GPADCR,
953 TWL6030_REG_TOGGLE1);
954 if (ret)
955 dev_err(pdev, "error resetting GPADC (%d)!\n", ret);
957 return 0;
960 static int twl6030_gpadc_resume(struct device *pdev)
962 int ret;
964 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, TWL6030_GPADCS,
965 TWL6030_REG_TOGGLE1);
966 if (ret)
967 dev_err(pdev, "error setting GPADC (%d)!\n", ret);
969 return 0;
971 #endif
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,
979 .driver = {
980 .name = DRIVER_NAME,
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");