xref: /openbmc/linux/drivers/iio/adc/twl6030-gpadc.c (revision 745b7908)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * TWL6030 GPADC module driver
4  *
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
12  *
13  * Based on twl4030-madc.c
14  * Copyright (C) 2008 Nokia Corporation
15  * Mikko Ylinen <mikko.k.ylinen@nokia.com>
16  */
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>
25 
26 #define DRIVER_NAME		"twl6030_gpadc"
27 
28 /*
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
33  */
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
39 
40 #define TWL6030_GPADC_CTRL_P1			0x05
41 
42 #define TWL6032_GPADC_GPSELECT_ISB		0x07
43 #define TWL6032_GPADC_CTRL_P1			0x08
44 
45 #define TWL6032_GPADC_GPCH0_LSB			0x0d
46 #define TWL6032_GPADC_GPCH0_MSB			0x0e
47 
48 #define TWL6030_GPADC_CTRL_P1_SP1		BIT(3)
49 
50 #define TWL6030_GPADC_GPCH0_LSB			(0x29)
51 
52 #define TWL6030_GPADC_RT_SW1_EOC_MASK		BIT(5)
53 
54 #define TWL6030_GPADC_TRIM1			0xCD
55 
56 #define TWL6030_REG_TOGGLE1			0x90
57 #define TWL6030_GPADCS				BIT(1)
58 #define TWL6030_GPADCR				BIT(0)
59 
60 #define USB_VBUS_CTRL_SET			0x04
61 #define USB_ID_CTRL_SET				0x06
62 
63 #define TWL6030_MISC1				0xE4
64 #define VBUS_MEAS				0x01
65 #define ID_MEAS					0x01
66 
67 #define VAC_MEAS                0x04
68 #define VBAT_MEAS               0x02
69 #define BB_MEAS                 0x01
70 
71 
72 /**
73  * struct twl6030_chnl_calib - channel calibration
74  * @gain:		slope coefficient for ideal curve
75  * @gain_error:		gain error
76  * @offset_error:	offset of the real curve
77  */
78 struct twl6030_chnl_calib {
79 	s32 gain;
80 	s32 gain_error;
81 	s32 offset_error;
82 };
83 
84 /**
85  * struct twl6030_ideal_code - GPADC calibration parameters
86  * GPADC is calibrated in two points: close to the beginning and
87  * to the and of the measurable input range
88  *
89  * @channel:	channel number
90  * @code1:	ideal code for the input at the beginning
91  * @code2:	ideal code for at the end of the range
92  * @volt1:	voltage input at the beginning(low voltage)
93  * @volt2:	voltage input at the end(high voltage)
94  */
95 struct twl6030_ideal_code {
96 	int channel;
97 	u16 code1;
98 	u16 code2;
99 	u16 volt1;
100 	u16 volt2;
101 };
102 
103 struct twl6030_gpadc_data;
104 
105 /**
106  * struct twl6030_gpadc_platform_data - platform specific data
107  * @nchannels:		number of GPADC channels
108  * @iio_channels:	iio channels
109  * @ideal:		pointer to calibration parameters
110  * @start_conversion:	pointer to ADC start conversion function
111  * @channel_to_reg:	pointer to ADC function to convert channel to
112  *			register address for reading conversion result
113  * @calibrate:		pointer to calibration function
114  */
115 struct twl6030_gpadc_platform_data {
116 	const int nchannels;
117 	const struct iio_chan_spec *iio_channels;
118 	const struct twl6030_ideal_code *ideal;
119 	int (*start_conversion)(int channel);
120 	u8 (*channel_to_reg)(int channel);
121 	int (*calibrate)(struct twl6030_gpadc_data *gpadc);
122 };
123 
124 /**
125  * struct twl6030_gpadc_data - GPADC data
126  * @dev:		device pointer
127  * @lock:		mutual exclusion lock for the structure
128  * @irq_complete:	completion to signal end of conversion
129  * @twl6030_cal_tbl:	pointer to calibration data for each
130  *			channel with gain error and offset
131  * @pdata:		pointer to device specific data
132  */
133 struct twl6030_gpadc_data {
134 	struct device	*dev;
135 	struct mutex	lock;
136 	struct completion	irq_complete;
137 	struct twl6030_chnl_calib	*twl6030_cal_tbl;
138 	const struct twl6030_gpadc_platform_data *pdata;
139 };
140 
141 /*
142  * channels 11, 12, 13, 15 and 16 have no calibration data
143  * calibration offset is same for channels 1, 3, 4, 5
144  *
145  * The data is taken from GPADC_TRIM registers description.
146  * GPADC_TRIM registers keep difference between the code measured
147  * at volt1 and volt2 input voltages and corresponding code1 and code2
148  */
149 static const struct twl6030_ideal_code
150 	twl6030_ideal[TWL6030_GPADC_USED_CHANNELS] = {
151 	[0] = { /* ch 0, external, battery type, resistor value */
152 		.channel = 0,
153 		.code1 = 116,
154 		.code2 = 745,
155 		.volt1 = 141,
156 		.volt2 = 910,
157 	},
158 	[1] = { /* ch 1, external, battery temperature, NTC resistor value */
159 		.channel = 1,
160 		.code1 = 82,
161 		.code2 = 900,
162 		.volt1 = 100,
163 		.volt2 = 1100,
164 	},
165 	[2] = { /* ch 2, external, audio accessory/general purpose */
166 		.channel = 2,
167 		.code1 = 55,
168 		.code2 = 818,
169 		.volt1 = 101,
170 		.volt2 = 1499,
171 	},
172 	[3] = { /* ch 3, external, general purpose */
173 		.channel = 3,
174 		.code1 = 82,
175 		.code2 = 900,
176 		.volt1 = 100,
177 		.volt2 = 1100,
178 	},
179 	[4] = { /* ch 4, external, temperature measurement/general purpose */
180 		.channel = 4,
181 		.code1 = 82,
182 		.code2 = 900,
183 		.volt1 = 100,
184 		.volt2 = 1100,
185 	},
186 	[5] = { /* ch 5, external, general purpose */
187 		.channel = 5,
188 		.code1 = 82,
189 		.code2 = 900,
190 		.volt1 = 100,
191 		.volt2 = 1100,
192 	},
193 	[6] = { /* ch 6, external, general purpose */
194 		.channel = 6,
195 		.code1 = 82,
196 		.code2 = 900,
197 		.volt1 = 100,
198 		.volt2 = 1100,
199 	},
200 	[7] = { /* ch 7, internal, main battery */
201 		.channel = 7,
202 		.code1 = 614,
203 		.code2 = 941,
204 		.volt1 = 3001,
205 		.volt2 = 4599,
206 	},
207 	[8] = { /* ch 8, internal, backup battery */
208 		.channel = 8,
209 		.code1 = 82,
210 		.code2 = 688,
211 		.volt1 = 501,
212 		.volt2 = 4203,
213 	},
214 	[9] = { /* ch 9, internal, external charger input */
215 		.channel = 9,
216 		.code1 = 182,
217 		.code2 = 818,
218 		.volt1 = 2001,
219 		.volt2 = 8996,
220 	},
221 	[10] = { /* ch 10, internal, VBUS */
222 		.channel = 10,
223 		.code1 = 149,
224 		.code2 = 818,
225 		.volt1 = 1001,
226 		.volt2 = 5497,
227 	},
228 	[11] = { /* ch 11, internal, VBUS charging current */
229 		.channel = 11,
230 	},
231 		/* ch 12, internal, Die temperature */
232 		/* ch 13, internal, Die temperature */
233 	[12] = { /* ch 14, internal, USB ID line */
234 		.channel = 14,
235 		.code1 = 48,
236 		.code2 = 714,
237 		.volt1 = 323,
238 		.volt2 = 4800,
239 	},
240 };
241 
242 static const struct twl6030_ideal_code
243 			twl6032_ideal[TWL6032_GPADC_USED_CHANNELS] = {
244 	[0] = { /* ch 0, external, battery type, resistor value */
245 		.channel = 0,
246 		.code1 = 1441,
247 		.code2 = 3276,
248 		.volt1 = 440,
249 		.volt2 = 1000,
250 	},
251 	[1] = { /* ch 1, external, battery temperature, NTC resistor value */
252 		.channel = 1,
253 		.code1 = 1441,
254 		.code2 = 3276,
255 		.volt1 = 440,
256 		.volt2 = 1000,
257 	},
258 	[2] = { /* ch 2, external, audio accessory/general purpose */
259 		.channel = 2,
260 		.code1 = 1441,
261 		.code2 = 3276,
262 		.volt1 = 660,
263 		.volt2 = 1500,
264 	},
265 	[3] = { /* ch 3, external, temperature with external diode/general
266 								purpose */
267 		.channel = 3,
268 		.code1 = 1441,
269 		.code2 = 3276,
270 		.volt1 = 440,
271 		.volt2 = 1000,
272 	},
273 	[4] = { /* ch 4, external, temperature measurement/general purpose */
274 		.channel = 4,
275 		.code1 = 1441,
276 		.code2 = 3276,
277 		.volt1 = 440,
278 		.volt2 = 1000,
279 	},
280 	[5] = { /* ch 5, external, general purpose */
281 		.channel = 5,
282 		.code1 = 1441,
283 		.code2 = 3276,
284 		.volt1 = 440,
285 		.volt2 = 1000,
286 	},
287 	[6] = { /* ch 6, external, general purpose */
288 		.channel = 6,
289 		.code1 = 1441,
290 		.code2 = 3276,
291 		.volt1 = 440,
292 		.volt2 = 1000,
293 	},
294 	[7] = { /* ch7, internal, system supply */
295 		.channel = 7,
296 		.code1 = 1441,
297 		.code2 = 3276,
298 		.volt1 = 2200,
299 		.volt2 = 5000,
300 	},
301 	[8] = { /* ch8, internal, backup battery */
302 		.channel = 8,
303 		.code1 = 1441,
304 		.code2 = 3276,
305 		.volt1 = 2200,
306 		.volt2 = 5000,
307 	},
308 	[9] = { /* ch 9, internal, external charger input */
309 		.channel = 9,
310 		.code1 = 1441,
311 		.code2 = 3276,
312 		.volt1 = 3960,
313 		.volt2 = 9000,
314 	},
315 	[10] = { /* ch10, internal, VBUS */
316 		.channel = 10,
317 		.code1 = 150,
318 		.code2 = 751,
319 		.volt1 = 1000,
320 		.volt2 = 5000,
321 	},
322 	[11] = { /* ch 11, internal, VBUS DC-DC output current */
323 		.channel = 11,
324 		.code1 = 1441,
325 		.code2 = 3276,
326 		.volt1 = 660,
327 		.volt2 = 1500,
328 	},
329 		/* ch 12, internal, Die temperature */
330 		/* ch 13, internal, Die temperature */
331 	[12] = { /* ch 14, internal, USB ID line */
332 		.channel = 14,
333 		.code1 = 1441,
334 		.code2 = 3276,
335 		.volt1 = 2420,
336 		.volt2 = 5500,
337 	},
338 		/* ch 15, internal, test network */
339 		/* ch 16, internal, test network */
340 	[13] = { /* ch 17, internal, battery charging current */
341 		.channel = 17,
342 	},
343 	[14] = { /* ch 18, internal, battery voltage */
344 		.channel = 18,
345 		.code1 = 1441,
346 		.code2 = 3276,
347 		.volt1 = 2200,
348 		.volt2 = 5000,
349 	},
350 };
351 
352 static inline int twl6030_gpadc_write(u8 reg, u8 val)
353 {
354 	return twl_i2c_write_u8(TWL6030_MODULE_GPADC, val, reg);
355 }
356 
357 static inline int twl6030_gpadc_read(u8 reg, u8 *val)
358 {
359 
360 	return twl_i2c_read(TWL6030_MODULE_GPADC, val, reg, 2);
361 }
362 
363 static int twl6030_gpadc_enable_irq(u8 mask)
364 {
365 	int ret;
366 
367 	ret = twl6030_interrupt_unmask(mask, REG_INT_MSK_LINE_B);
368 	if (ret < 0)
369 		return ret;
370 
371 	ret = twl6030_interrupt_unmask(mask, REG_INT_MSK_STS_B);
372 
373 	return ret;
374 }
375 
376 static void twl6030_gpadc_disable_irq(u8 mask)
377 {
378 	twl6030_interrupt_mask(mask, REG_INT_MSK_LINE_B);
379 	twl6030_interrupt_mask(mask, REG_INT_MSK_STS_B);
380 }
381 
382 static irqreturn_t twl6030_gpadc_irq_handler(int irq, void *indio_dev)
383 {
384 	struct twl6030_gpadc_data *gpadc = iio_priv(indio_dev);
385 
386 	complete(&gpadc->irq_complete);
387 
388 	return IRQ_HANDLED;
389 }
390 
391 static int twl6030_start_conversion(int channel)
392 {
393 	return twl6030_gpadc_write(TWL6030_GPADC_CTRL_P1,
394 					TWL6030_GPADC_CTRL_P1_SP1);
395 }
396 
397 static int twl6032_start_conversion(int channel)
398 {
399 	int ret;
400 
401 	ret = twl6030_gpadc_write(TWL6032_GPADC_GPSELECT_ISB, channel);
402 	if (ret)
403 		return ret;
404 
405 	return twl6030_gpadc_write(TWL6032_GPADC_CTRL_P1,
406 						TWL6030_GPADC_CTRL_P1_SP1);
407 }
408 
409 static u8 twl6030_channel_to_reg(int channel)
410 {
411 	return TWL6030_GPADC_GPCH0_LSB + 2 * channel;
412 }
413 
414 static u8 twl6032_channel_to_reg(int channel)
415 {
416 	/*
417 	 * for any prior chosen channel, when the conversion is ready
418 	 * the result is avalable in GPCH0_LSB, GPCH0_MSB.
419 	 */
420 
421 	return TWL6032_GPADC_GPCH0_LSB;
422 }
423 
424 static int twl6030_gpadc_lookup(const struct twl6030_ideal_code *ideal,
425 		int channel, int size)
426 {
427 	int i;
428 
429 	for (i = 0; i < size; i++)
430 		if (ideal[i].channel == channel)
431 			break;
432 
433 	return i;
434 }
435 
436 static int twl6030_channel_calibrated(const struct twl6030_gpadc_platform_data
437 		*pdata, int channel)
438 {
439 	const struct twl6030_ideal_code *ideal = pdata->ideal;
440 	int i;
441 
442 	i = twl6030_gpadc_lookup(ideal, channel, pdata->nchannels);
443 	/* not calibrated channels have 0 in all structure members */
444 	return pdata->ideal[i].code2;
445 }
446 
447 static int twl6030_gpadc_make_correction(struct twl6030_gpadc_data *gpadc,
448 		int channel, int raw_code)
449 {
450 	const struct twl6030_ideal_code *ideal = gpadc->pdata->ideal;
451 	int corrected_code;
452 	int i;
453 
454 	i = twl6030_gpadc_lookup(ideal, channel, gpadc->pdata->nchannels);
455 	corrected_code = ((raw_code * 1000) -
456 		gpadc->twl6030_cal_tbl[i].offset_error) /
457 		gpadc->twl6030_cal_tbl[i].gain_error;
458 
459 	return corrected_code;
460 }
461 
462 static int twl6030_gpadc_get_raw(struct twl6030_gpadc_data *gpadc,
463 		int channel, int *res)
464 {
465 	u8 reg = gpadc->pdata->channel_to_reg(channel);
466 	__le16 val;
467 	int raw_code;
468 	int ret;
469 
470 	ret = twl6030_gpadc_read(reg, (u8 *)&val);
471 	if (ret) {
472 		dev_dbg(gpadc->dev, "unable to read register 0x%X\n", reg);
473 		return ret;
474 	}
475 
476 	raw_code = le16_to_cpu(val);
477 	dev_dbg(gpadc->dev, "GPADC raw code: %d", raw_code);
478 
479 	if (twl6030_channel_calibrated(gpadc->pdata, channel))
480 		*res = twl6030_gpadc_make_correction(gpadc, channel, raw_code);
481 	else
482 		*res = raw_code;
483 
484 	return ret;
485 }
486 
487 static int twl6030_gpadc_get_processed(struct twl6030_gpadc_data *gpadc,
488 		int channel, int *val)
489 {
490 	const struct twl6030_ideal_code *ideal = gpadc->pdata->ideal;
491 	int corrected_code;
492 	int channel_value;
493 	int i;
494 	int ret;
495 
496 	ret = twl6030_gpadc_get_raw(gpadc, channel, &corrected_code);
497 	if (ret)
498 		return ret;
499 
500 	i = twl6030_gpadc_lookup(ideal, channel, gpadc->pdata->nchannels);
501 	channel_value = corrected_code *
502 			gpadc->twl6030_cal_tbl[i].gain;
503 
504 	/* Shift back into mV range */
505 	channel_value /= 1000;
506 
507 	dev_dbg(gpadc->dev, "GPADC corrected code: %d", corrected_code);
508 	dev_dbg(gpadc->dev, "GPADC value: %d", channel_value);
509 
510 	*val = channel_value;
511 
512 	return ret;
513 }
514 
515 static int twl6030_gpadc_read_raw(struct iio_dev *indio_dev,
516 			     const struct iio_chan_spec *chan,
517 			     int *val, int *val2, long mask)
518 {
519 	struct twl6030_gpadc_data *gpadc = iio_priv(indio_dev);
520 	int ret;
521 	long timeout;
522 
523 	mutex_lock(&gpadc->lock);
524 
525 	ret = gpadc->pdata->start_conversion(chan->channel);
526 	if (ret) {
527 		dev_err(gpadc->dev, "failed to start conversion\n");
528 		goto err;
529 	}
530 	/* wait for conversion to complete */
531 	timeout = wait_for_completion_interruptible_timeout(
532 				&gpadc->irq_complete, msecs_to_jiffies(5000));
533 	if (timeout == 0) {
534 		ret = -ETIMEDOUT;
535 		goto err;
536 	} else if (timeout < 0) {
537 		ret = -EINTR;
538 		goto err;
539 	}
540 
541 	switch (mask) {
542 	case IIO_CHAN_INFO_RAW:
543 		ret = twl6030_gpadc_get_raw(gpadc, chan->channel, val);
544 		ret = ret ? -EIO : IIO_VAL_INT;
545 		break;
546 
547 	case IIO_CHAN_INFO_PROCESSED:
548 		ret = twl6030_gpadc_get_processed(gpadc, chan->channel, val);
549 		ret = ret ? -EIO : IIO_VAL_INT;
550 		break;
551 
552 	default:
553 		break;
554 	}
555 err:
556 	mutex_unlock(&gpadc->lock);
557 
558 	return ret;
559 }
560 
561 /*
562  * The GPADC channels are calibrated using a two point calibration method.
563  * The channels measured with two known values: volt1 and volt2, and
564  * ideal corresponding output codes are known: code1, code2.
565  * The difference(d1, d2) between ideal and measured codes stored in trim
566  * registers.
567  * The goal is to find offset and gain of the real curve for each calibrated
568  * channel.
569  * gain: k = 1 + ((d2 - d1) / (x2 - x1))
570  * offset: b = d1 + (k - 1) * x1
571  */
572 static void twl6030_calibrate_channel(struct twl6030_gpadc_data *gpadc,
573 		int channel, int d1, int d2)
574 {
575 	int b, k, gain, x1, x2, i;
576 	const struct twl6030_ideal_code *ideal = gpadc->pdata->ideal;
577 
578 	i = twl6030_gpadc_lookup(ideal, channel, gpadc->pdata->nchannels);
579 
580 	/* Gain */
581 	gain = ((ideal[i].volt2 - ideal[i].volt1) * 1000) /
582 		(ideal[i].code2 - ideal[i].code1);
583 
584 	x1 = ideal[i].code1;
585 	x2 = ideal[i].code2;
586 
587 	/* k - real curve gain */
588 	k = 1000 + (((d2 - d1) * 1000) / (x2 - x1));
589 
590 	/* b - offset of the real curve gain */
591 	b = (d1 * 1000) - (k - 1000) * x1;
592 
593 	gpadc->twl6030_cal_tbl[i].gain = gain;
594 	gpadc->twl6030_cal_tbl[i].gain_error = k;
595 	gpadc->twl6030_cal_tbl[i].offset_error = b;
596 
597 	dev_dbg(gpadc->dev, "GPADC d1   for Chn: %d = %d\n", channel, d1);
598 	dev_dbg(gpadc->dev, "GPADC d2   for Chn: %d = %d\n", channel, d2);
599 	dev_dbg(gpadc->dev, "GPADC x1   for Chn: %d = %d\n", channel, x1);
600 	dev_dbg(gpadc->dev, "GPADC x2   for Chn: %d = %d\n", channel, x2);
601 	dev_dbg(gpadc->dev, "GPADC Gain for Chn: %d = %d\n", channel, gain);
602 	dev_dbg(gpadc->dev, "GPADC k    for Chn: %d = %d\n", channel, k);
603 	dev_dbg(gpadc->dev, "GPADC b    for Chn: %d = %d\n", channel, b);
604 }
605 
606 static inline int twl6030_gpadc_get_trim_offset(s8 d)
607 {
608 	/*
609 	 * XXX NOTE!
610 	 * bit 0 - sign, bit 7 - reserved, 6..1 - trim value
611 	 * though, the documentation states that trim value
612 	 * is absolute value, the correct conversion results are
613 	 * obtained if the value is interpreted as 2's complement.
614 	 */
615 	__u32 temp = ((d & 0x7f) >> 1) | ((d & 1) << 6);
616 
617 	return sign_extend32(temp, 6);
618 }
619 
620 static int twl6030_calibration(struct twl6030_gpadc_data *gpadc)
621 {
622 	int ret;
623 	int chn;
624 	u8 trim_regs[TWL6030_GPADC_NUM_TRIM_REGS];
625 	s8 d1, d2;
626 
627 	/*
628 	 * for calibration two measurements have been performed at
629 	 * factory, for some channels, during the production test and
630 	 * have been stored in registers. This two stored values are
631 	 * used to correct the measurements. The values represent
632 	 * offsets for the given input from the output on ideal curve.
633 	 */
634 	ret = twl_i2c_read(TWL6030_MODULE_ID2, trim_regs,
635 			TWL6030_GPADC_TRIM1, TWL6030_GPADC_NUM_TRIM_REGS);
636 	if (ret < 0) {
637 		dev_err(gpadc->dev, "calibration failed\n");
638 		return ret;
639 	}
640 
641 	for (chn = 0; chn < TWL6030_GPADC_MAX_CHANNELS; chn++) {
642 
643 		switch (chn) {
644 		case 0:
645 			d1 = trim_regs[0];
646 			d2 = trim_regs[1];
647 			break;
648 		case 1:
649 		case 3:
650 		case 4:
651 		case 5:
652 		case 6:
653 			d1 = trim_regs[4];
654 			d2 = trim_regs[5];
655 			break;
656 		case 2:
657 			d1 = trim_regs[12];
658 			d2 = trim_regs[13];
659 			break;
660 		case 7:
661 			d1 = trim_regs[6];
662 			d2 = trim_regs[7];
663 			break;
664 		case 8:
665 			d1 = trim_regs[2];
666 			d2 = trim_regs[3];
667 			break;
668 		case 9:
669 			d1 = trim_regs[8];
670 			d2 = trim_regs[9];
671 			break;
672 		case 10:
673 			d1 = trim_regs[10];
674 			d2 = trim_regs[11];
675 			break;
676 		case 14:
677 			d1 = trim_regs[14];
678 			d2 = trim_regs[15];
679 			break;
680 		default:
681 			continue;
682 		}
683 
684 		d1 = twl6030_gpadc_get_trim_offset(d1);
685 		d2 = twl6030_gpadc_get_trim_offset(d2);
686 
687 		twl6030_calibrate_channel(gpadc, chn, d1, d2);
688 	}
689 
690 	return 0;
691 }
692 
693 static int twl6032_get_trim_value(u8 *trim_regs, unsigned int reg0,
694 		unsigned int reg1, unsigned int mask0, unsigned int mask1,
695 		unsigned int shift0)
696 {
697 	int val;
698 
699 	val = (trim_regs[reg0] & mask0) << shift0;
700 	val |= (trim_regs[reg1] & mask1) >> 1;
701 	if (trim_regs[reg1] & 0x01)
702 		val = -val;
703 
704 	return val;
705 }
706 
707 static int twl6032_calibration(struct twl6030_gpadc_data *gpadc)
708 {
709 	int chn, d1 = 0, d2 = 0, temp;
710 	u8 trim_regs[TWL6030_GPADC_NUM_TRIM_REGS];
711 	int ret;
712 
713 	ret = twl_i2c_read(TWL6030_MODULE_ID2, trim_regs,
714 			TWL6030_GPADC_TRIM1, TWL6030_GPADC_NUM_TRIM_REGS);
715 	if (ret < 0) {
716 		dev_err(gpadc->dev, "calibration failed\n");
717 		return ret;
718 	}
719 
720 	/*
721 	 * Loop to calculate the value needed for returning voltages from
722 	 * GPADC not values.
723 	 *
724 	 * gain is calculated to 3 decimal places fixed point.
725 	 */
726 	for (chn = 0; chn < TWL6032_GPADC_MAX_CHANNELS; chn++) {
727 
728 		switch (chn) {
729 		case 0:
730 		case 1:
731 		case 2:
732 		case 3:
733 		case 4:
734 		case 5:
735 		case 6:
736 		case 11:
737 		case 14:
738 			d1 = twl6032_get_trim_value(trim_regs, 2, 0, 0x1f,
739 								0x06, 2);
740 			d2 = twl6032_get_trim_value(trim_regs, 3, 1, 0x3f,
741 								0x06, 2);
742 			break;
743 		case 8:
744 			temp = twl6032_get_trim_value(trim_regs, 2, 0, 0x1f,
745 								0x06, 2);
746 			d1 = temp + twl6032_get_trim_value(trim_regs, 7, 6,
747 								0x18, 0x1E, 1);
748 
749 			temp = twl6032_get_trim_value(trim_regs, 3, 1, 0x3F,
750 								0x06, 2);
751 			d2 = temp + twl6032_get_trim_value(trim_regs, 9, 7,
752 								0x1F, 0x06, 2);
753 			break;
754 		case 9:
755 			temp = twl6032_get_trim_value(trim_regs, 2, 0, 0x1f,
756 								0x06, 2);
757 			d1 = temp + twl6032_get_trim_value(trim_regs, 13, 11,
758 								0x18, 0x1E, 1);
759 
760 			temp = twl6032_get_trim_value(trim_regs, 3, 1, 0x3f,
761 								0x06, 2);
762 			d2 = temp + twl6032_get_trim_value(trim_regs, 15, 13,
763 								0x1F, 0x06, 1);
764 			break;
765 		case 10:
766 			d1 = twl6032_get_trim_value(trim_regs, 10, 8, 0x0f,
767 								0x0E, 3);
768 			d2 = twl6032_get_trim_value(trim_regs, 14, 12, 0x0f,
769 								0x0E, 3);
770 			break;
771 		case 7:
772 		case 18:
773 			temp = twl6032_get_trim_value(trim_regs, 2, 0, 0x1f,
774 								0x06, 2);
775 
776 			d1 = (trim_regs[4] & 0x7E) >> 1;
777 			if (trim_regs[4] & 0x01)
778 				d1 = -d1;
779 			d1 += temp;
780 
781 			temp = twl6032_get_trim_value(trim_regs, 3, 1, 0x3f,
782 								0x06, 2);
783 
784 			d2 = (trim_regs[5] & 0xFE) >> 1;
785 			if (trim_regs[5] & 0x01)
786 				d2 = -d2;
787 
788 			d2 += temp;
789 			break;
790 		default:
791 			/* No data for other channels */
792 			continue;
793 		}
794 
795 		twl6030_calibrate_channel(gpadc, chn, d1, d2);
796 	}
797 
798 	return 0;
799 }
800 
801 #define TWL6030_GPADC_CHAN(chn, _type, chan_info) {	\
802 	.type = _type,					\
803 	.channel = chn,					\
804 	.info_mask_separate = BIT(chan_info),		\
805 	.indexed = 1,					\
806 }
807 
808 static const struct iio_chan_spec twl6030_gpadc_iio_channels[] = {
809 	TWL6030_GPADC_CHAN(0, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
810 	TWL6030_GPADC_CHAN(1, IIO_TEMP, IIO_CHAN_INFO_RAW),
811 	TWL6030_GPADC_CHAN(2, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
812 	TWL6030_GPADC_CHAN(3, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
813 	TWL6030_GPADC_CHAN(4, IIO_TEMP, IIO_CHAN_INFO_RAW),
814 	TWL6030_GPADC_CHAN(5, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
815 	TWL6030_GPADC_CHAN(6, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
816 	TWL6030_GPADC_CHAN(7, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
817 	TWL6030_GPADC_CHAN(8, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
818 	TWL6030_GPADC_CHAN(9, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
819 	TWL6030_GPADC_CHAN(10, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
820 	TWL6030_GPADC_CHAN(11, IIO_VOLTAGE, IIO_CHAN_INFO_RAW),
821 	TWL6030_GPADC_CHAN(14, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
822 };
823 
824 static const struct iio_chan_spec twl6032_gpadc_iio_channels[] = {
825 	TWL6030_GPADC_CHAN(0, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
826 	TWL6030_GPADC_CHAN(1, IIO_TEMP, IIO_CHAN_INFO_RAW),
827 	TWL6030_GPADC_CHAN(2, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
828 	TWL6030_GPADC_CHAN(3, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
829 	TWL6030_GPADC_CHAN(4, IIO_TEMP, IIO_CHAN_INFO_RAW),
830 	TWL6030_GPADC_CHAN(5, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
831 	TWL6030_GPADC_CHAN(6, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
832 	TWL6030_GPADC_CHAN(7, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
833 	TWL6030_GPADC_CHAN(8, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
834 	TWL6030_GPADC_CHAN(9, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
835 	TWL6030_GPADC_CHAN(10, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
836 	TWL6030_GPADC_CHAN(11, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
837 	TWL6030_GPADC_CHAN(14, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
838 	TWL6030_GPADC_CHAN(17, IIO_VOLTAGE, IIO_CHAN_INFO_RAW),
839 	TWL6030_GPADC_CHAN(18, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
840 };
841 
842 static const struct iio_info twl6030_gpadc_iio_info = {
843 	.read_raw = &twl6030_gpadc_read_raw,
844 };
845 
846 static const struct twl6030_gpadc_platform_data twl6030_pdata = {
847 	.iio_channels = twl6030_gpadc_iio_channels,
848 	.nchannels = TWL6030_GPADC_USED_CHANNELS,
849 	.ideal = twl6030_ideal,
850 	.start_conversion = twl6030_start_conversion,
851 	.channel_to_reg = twl6030_channel_to_reg,
852 	.calibrate = twl6030_calibration,
853 };
854 
855 static const struct twl6030_gpadc_platform_data twl6032_pdata = {
856 	.iio_channels = twl6032_gpadc_iio_channels,
857 	.nchannels = TWL6032_GPADC_USED_CHANNELS,
858 	.ideal = twl6032_ideal,
859 	.start_conversion = twl6032_start_conversion,
860 	.channel_to_reg = twl6032_channel_to_reg,
861 	.calibrate = twl6032_calibration,
862 };
863 
864 static const struct of_device_id of_twl6030_match_tbl[] = {
865 	{
866 		.compatible = "ti,twl6030-gpadc",
867 		.data = &twl6030_pdata,
868 	},
869 	{
870 		.compatible = "ti,twl6032-gpadc",
871 		.data = &twl6032_pdata,
872 	},
873 	{ /* end */ }
874 };
875 MODULE_DEVICE_TABLE(of, of_twl6030_match_tbl);
876 
877 static int twl6030_gpadc_probe(struct platform_device *pdev)
878 {
879 	struct device *dev = &pdev->dev;
880 	struct twl6030_gpadc_data *gpadc;
881 	const struct twl6030_gpadc_platform_data *pdata;
882 	const struct of_device_id *match;
883 	struct iio_dev *indio_dev;
884 	int irq;
885 	int ret;
886 
887 	match = of_match_device(of_twl6030_match_tbl, dev);
888 	if (!match)
889 		return -EINVAL;
890 
891 	pdata = match->data;
892 
893 	indio_dev = devm_iio_device_alloc(dev, sizeof(*gpadc));
894 	if (!indio_dev)
895 		return -ENOMEM;
896 
897 	gpadc = iio_priv(indio_dev);
898 
899 	gpadc->twl6030_cal_tbl = devm_kcalloc(dev,
900 					pdata->nchannels,
901 					sizeof(*gpadc->twl6030_cal_tbl),
902 					GFP_KERNEL);
903 	if (!gpadc->twl6030_cal_tbl)
904 		return -ENOMEM;
905 
906 	gpadc->dev = dev;
907 	gpadc->pdata = pdata;
908 
909 	platform_set_drvdata(pdev, indio_dev);
910 	mutex_init(&gpadc->lock);
911 	init_completion(&gpadc->irq_complete);
912 
913 	ret = pdata->calibrate(gpadc);
914 	if (ret < 0) {
915 		dev_err(dev, "failed to read calibration registers\n");
916 		return ret;
917 	}
918 
919 	irq = platform_get_irq(pdev, 0);
920 	if (irq < 0)
921 		return irq;
922 
923 	ret = devm_request_threaded_irq(dev, irq, NULL,
924 				twl6030_gpadc_irq_handler,
925 				IRQF_ONESHOT, "twl6030_gpadc", indio_dev);
926 	if (ret)
927 		return ret;
928 
929 	ret = twl6030_gpadc_enable_irq(TWL6030_GPADC_RT_SW1_EOC_MASK);
930 	if (ret < 0) {
931 		dev_err(dev, "failed to enable GPADC interrupt\n");
932 		return ret;
933 	}
934 
935 	ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, TWL6030_GPADCS,
936 					TWL6030_REG_TOGGLE1);
937 	if (ret < 0) {
938 		dev_err(dev, "failed to enable GPADC module\n");
939 		return ret;
940 	}
941 
942 	ret = twl_i2c_write_u8(TWL_MODULE_USB, VBUS_MEAS, USB_VBUS_CTRL_SET);
943 	if (ret < 0) {
944 		dev_err(dev, "failed to wire up inputs\n");
945 		return ret;
946 	}
947 
948 	ret = twl_i2c_write_u8(TWL_MODULE_USB, ID_MEAS, USB_ID_CTRL_SET);
949 	if (ret < 0) {
950 		dev_err(dev, "failed to wire up inputs\n");
951 		return ret;
952 	}
953 
954 	ret = twl_i2c_write_u8(TWL6030_MODULE_ID0,
955 				VBAT_MEAS | BB_MEAS | VAC_MEAS,
956 				TWL6030_MISC1);
957 	if (ret < 0) {
958 		dev_err(dev, "failed to wire up inputs\n");
959 		return ret;
960 	}
961 
962 	indio_dev->name = DRIVER_NAME;
963 	indio_dev->info = &twl6030_gpadc_iio_info;
964 	indio_dev->modes = INDIO_DIRECT_MODE;
965 	indio_dev->channels = pdata->iio_channels;
966 	indio_dev->num_channels = pdata->nchannels;
967 
968 	return iio_device_register(indio_dev);
969 }
970 
971 static int twl6030_gpadc_remove(struct platform_device *pdev)
972 {
973 	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
974 
975 	twl6030_gpadc_disable_irq(TWL6030_GPADC_RT_SW1_EOC_MASK);
976 	iio_device_unregister(indio_dev);
977 
978 	return 0;
979 }
980 
981 static int twl6030_gpadc_suspend(struct device *pdev)
982 {
983 	int ret;
984 
985 	ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, TWL6030_GPADCR,
986 				TWL6030_REG_TOGGLE1);
987 	if (ret)
988 		dev_err(pdev, "error resetting GPADC (%d)!\n", ret);
989 
990 	return 0;
991 };
992 
993 static int twl6030_gpadc_resume(struct device *pdev)
994 {
995 	int ret;
996 
997 	ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, TWL6030_GPADCS,
998 				TWL6030_REG_TOGGLE1);
999 	if (ret)
1000 		dev_err(pdev, "error setting GPADC (%d)!\n", ret);
1001 
1002 	return 0;
1003 };
1004 
1005 static DEFINE_SIMPLE_DEV_PM_OPS(twl6030_gpadc_pm_ops, twl6030_gpadc_suspend,
1006 				twl6030_gpadc_resume);
1007 
1008 static struct platform_driver twl6030_gpadc_driver = {
1009 	.probe		= twl6030_gpadc_probe,
1010 	.remove		= twl6030_gpadc_remove,
1011 	.driver		= {
1012 		.name	= DRIVER_NAME,
1013 		.pm	= pm_sleep_ptr(&twl6030_gpadc_pm_ops),
1014 		.of_match_table = of_twl6030_match_tbl,
1015 	},
1016 };
1017 
1018 module_platform_driver(twl6030_gpadc_driver);
1019 
1020 MODULE_ALIAS("platform:" DRIVER_NAME);
1021 MODULE_AUTHOR("Balaji T K <balajitk@ti.com>");
1022 MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
1023 MODULE_AUTHOR("Oleksandr Kozaruk <oleksandr.kozaruk@ti.com");
1024 MODULE_DESCRIPTION("twl6030 ADC driver");
1025 MODULE_LICENSE("GPL");
1026