xref: /openbmc/linux/drivers/iio/adc/qcom-spmi-vadc.c (revision 726bd223)
1 /*
2  * Copyright (c) 2012-2016, The Linux Foundation. All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 and
6  * only version 2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13 
14 #include <linux/bitops.h>
15 #include <linux/completion.h>
16 #include <linux/delay.h>
17 #include <linux/err.h>
18 #include <linux/iio/iio.h>
19 #include <linux/interrupt.h>
20 #include <linux/kernel.h>
21 #include <linux/math64.h>
22 #include <linux/module.h>
23 #include <linux/of.h>
24 #include <linux/platform_device.h>
25 #include <linux/regmap.h>
26 #include <linux/slab.h>
27 #include <linux/log2.h>
28 
29 #include <dt-bindings/iio/qcom,spmi-vadc.h>
30 
31 /* VADC register and bit definitions */
32 #define VADC_REVISION2				0x1
33 #define VADC_REVISION2_SUPPORTED_VADC		1
34 
35 #define VADC_PERPH_TYPE				0x4
36 #define VADC_PERPH_TYPE_ADC			8
37 
38 #define VADC_PERPH_SUBTYPE			0x5
39 #define VADC_PERPH_SUBTYPE_VADC			1
40 
41 #define VADC_STATUS1				0x8
42 #define VADC_STATUS1_OP_MODE			4
43 #define VADC_STATUS1_REQ_STS			BIT(1)
44 #define VADC_STATUS1_EOC			BIT(0)
45 #define VADC_STATUS1_REQ_STS_EOC_MASK		0x3
46 
47 #define VADC_MODE_CTL				0x40
48 #define VADC_OP_MODE_SHIFT			3
49 #define VADC_OP_MODE_NORMAL			0
50 #define VADC_AMUX_TRIM_EN			BIT(1)
51 #define VADC_ADC_TRIM_EN			BIT(0)
52 
53 #define VADC_EN_CTL1				0x46
54 #define VADC_EN_CTL1_SET			BIT(7)
55 
56 #define VADC_ADC_CH_SEL_CTL			0x48
57 
58 #define VADC_ADC_DIG_PARAM			0x50
59 #define VADC_ADC_DIG_DEC_RATIO_SEL_SHIFT	2
60 
61 #define VADC_HW_SETTLE_DELAY			0x51
62 
63 #define VADC_CONV_REQ				0x52
64 #define VADC_CONV_REQ_SET			BIT(7)
65 
66 #define VADC_FAST_AVG_CTL			0x5a
67 #define VADC_FAST_AVG_EN			0x5b
68 #define VADC_FAST_AVG_EN_SET			BIT(7)
69 
70 #define VADC_ACCESS				0xd0
71 #define VADC_ACCESS_DATA			0xa5
72 
73 #define VADC_PERH_RESET_CTL3			0xda
74 #define VADC_FOLLOW_WARM_RB			BIT(2)
75 
76 #define VADC_DATA				0x60	/* 16 bits */
77 
78 #define VADC_CONV_TIME_MIN_US			2000
79 #define VADC_CONV_TIME_MAX_US			2100
80 
81 /* Min ADC code represents 0V */
82 #define VADC_MIN_ADC_CODE			0x6000
83 /* Max ADC code represents full-scale range of 1.8V */
84 #define VADC_MAX_ADC_CODE			0xa800
85 
86 #define VADC_ABSOLUTE_RANGE_UV			625000
87 #define VADC_RATIOMETRIC_RANGE			1800
88 
89 #define VADC_DEF_PRESCALING			0 /* 1:1 */
90 #define VADC_DEF_DECIMATION			0 /* 512 */
91 #define VADC_DEF_HW_SETTLE_TIME			0 /* 0 us */
92 #define VADC_DEF_AVG_SAMPLES			0 /* 1 sample */
93 #define VADC_DEF_CALIB_TYPE			VADC_CALIB_ABSOLUTE
94 
95 #define VADC_DECIMATION_MIN			512
96 #define VADC_DECIMATION_MAX			4096
97 
98 #define VADC_HW_SETTLE_DELAY_MAX		10000
99 #define VADC_AVG_SAMPLES_MAX			512
100 
101 #define KELVINMIL_CELSIUSMIL			273150
102 
103 #define PMI_CHG_SCALE_1				-138890
104 #define PMI_CHG_SCALE_2				391750000000LL
105 
106 #define VADC_CHAN_MIN			VADC_USBIN
107 #define VADC_CHAN_MAX			VADC_LR_MUX3_BUF_PU1_PU2_XO_THERM
108 
109 /**
110  * struct vadc_map_pt - Map the graph representation for ADC channel
111  * @x: Represent the ADC digitized code.
112  * @y: Represent the physical data which can be temperature, voltage,
113  *     resistance.
114  */
115 struct vadc_map_pt {
116 	s32 x;
117 	s32 y;
118 };
119 
120 /*
121  * VADC_CALIB_ABSOLUTE: uses the 625mV and 1.25V as reference channels.
122  * VADC_CALIB_RATIOMETRIC: uses the reference voltage (1.8V) and GND for
123  * calibration.
124  */
125 enum vadc_calibration {
126 	VADC_CALIB_ABSOLUTE = 0,
127 	VADC_CALIB_RATIOMETRIC
128 };
129 
130 /**
131  * struct vadc_linear_graph - Represent ADC characteristics.
132  * @dy: numerator slope to calculate the gain.
133  * @dx: denominator slope to calculate the gain.
134  * @gnd: A/D word of the ground reference used for the channel.
135  *
136  * Each ADC device has different offset and gain parameters which are
137  * computed to calibrate the device.
138  */
139 struct vadc_linear_graph {
140 	s32 dy;
141 	s32 dx;
142 	s32 gnd;
143 };
144 
145 /**
146  * struct vadc_prescale_ratio - Represent scaling ratio for ADC input.
147  * @num: the inverse numerator of the gain applied to the input channel.
148  * @den: the inverse denominator of the gain applied to the input channel.
149  */
150 struct vadc_prescale_ratio {
151 	u32 num;
152 	u32 den;
153 };
154 
155 /**
156  * struct vadc_channel_prop - VADC channel property.
157  * @channel: channel number, refer to the channel list.
158  * @calibration: calibration type.
159  * @decimation: sampling rate supported for the channel.
160  * @prescale: channel scaling performed on the input signal.
161  * @hw_settle_time: the time between AMUX being configured and the
162  *	start of conversion.
163  * @avg_samples: ability to provide single result from the ADC
164  *	that is an average of multiple measurements.
165  * @scale_fn: Represents the scaling function to convert voltage
166  *	physical units desired by the client for the channel.
167  *	Referenced from enum vadc_scale_fn_type.
168  */
169 struct vadc_channel_prop {
170 	unsigned int channel;
171 	enum vadc_calibration calibration;
172 	unsigned int decimation;
173 	unsigned int prescale;
174 	unsigned int hw_settle_time;
175 	unsigned int avg_samples;
176 	unsigned int scale_fn;
177 };
178 
179 /**
180  * struct vadc_priv - VADC private structure.
181  * @regmap: pointer to struct regmap.
182  * @dev: pointer to struct device.
183  * @base: base address for the ADC peripheral.
184  * @nchannels: number of VADC channels.
185  * @chan_props: array of VADC channel properties.
186  * @iio_chans: array of IIO channels specification.
187  * @are_ref_measured: are reference points measured.
188  * @poll_eoc: use polling instead of interrupt.
189  * @complete: VADC result notification after interrupt is received.
190  * @graph: store parameters for calibration.
191  * @lock: ADC lock for access to the peripheral.
192  */
193 struct vadc_priv {
194 	struct regmap		 *regmap;
195 	struct device		 *dev;
196 	u16			 base;
197 	unsigned int		 nchannels;
198 	struct vadc_channel_prop *chan_props;
199 	struct iio_chan_spec	 *iio_chans;
200 	bool			 are_ref_measured;
201 	bool			 poll_eoc;
202 	struct completion	 complete;
203 	struct vadc_linear_graph graph[2];
204 	struct mutex		 lock;
205 };
206 
207 /**
208  * struct vadc_scale_fn - Scaling function prototype
209  * @scale: Function pointer to one of the scaling functions
210  *	which takes the adc properties, channel properties,
211  *	and returns the physical result.
212  */
213 struct vadc_scale_fn {
214 	int (*scale)(struct vadc_priv *, const struct vadc_channel_prop *,
215 		     u16, int *);
216 };
217 
218 /**
219  * enum vadc_scale_fn_type - Scaling function to convert ADC code to
220  *				physical scaled units for the channel.
221  * SCALE_DEFAULT: Default scaling to convert raw adc code to voltage (uV).
222  * SCALE_THERM_100K_PULLUP: Returns temperature in millidegC.
223  *				 Uses a mapping table with 100K pullup.
224  * SCALE_PMIC_THERM: Returns result in milli degree's Centigrade.
225  * SCALE_XOTHERM: Returns XO thermistor voltage in millidegC.
226  * SCALE_PMI_CHG_TEMP: Conversion for PMI CHG temp
227  */
228 enum vadc_scale_fn_type {
229 	SCALE_DEFAULT = 0,
230 	SCALE_THERM_100K_PULLUP,
231 	SCALE_PMIC_THERM,
232 	SCALE_XOTHERM,
233 	SCALE_PMI_CHG_TEMP,
234 };
235 
236 static const struct vadc_prescale_ratio vadc_prescale_ratios[] = {
237 	{.num =  1, .den =  1},
238 	{.num =  1, .den =  3},
239 	{.num =  1, .den =  4},
240 	{.num =  1, .den =  6},
241 	{.num =  1, .den = 20},
242 	{.num =  1, .den =  8},
243 	{.num = 10, .den = 81},
244 	{.num =  1, .den = 10}
245 };
246 
247 /* Voltage to temperature */
248 static const struct vadc_map_pt adcmap_100k_104ef_104fb[] = {
249 	{1758,	-40},
250 	{1742,	-35},
251 	{1719,	-30},
252 	{1691,	-25},
253 	{1654,	-20},
254 	{1608,	-15},
255 	{1551,	-10},
256 	{1483,	-5},
257 	{1404,	0},
258 	{1315,	5},
259 	{1218,	10},
260 	{1114,	15},
261 	{1007,	20},
262 	{900,	25},
263 	{795,	30},
264 	{696,	35},
265 	{605,	40},
266 	{522,	45},
267 	{448,	50},
268 	{383,	55},
269 	{327,	60},
270 	{278,	65},
271 	{237,	70},
272 	{202,	75},
273 	{172,	80},
274 	{146,	85},
275 	{125,	90},
276 	{107,	95},
277 	{92,	100},
278 	{79,	105},
279 	{68,	110},
280 	{59,	115},
281 	{51,	120},
282 	{44,	125}
283 };
284 
285 static int vadc_read(struct vadc_priv *vadc, u16 offset, u8 *data)
286 {
287 	return regmap_bulk_read(vadc->regmap, vadc->base + offset, data, 1);
288 }
289 
290 static int vadc_write(struct vadc_priv *vadc, u16 offset, u8 data)
291 {
292 	return regmap_write(vadc->regmap, vadc->base + offset, data);
293 }
294 
295 static int vadc_reset(struct vadc_priv *vadc)
296 {
297 	u8 data;
298 	int ret;
299 
300 	ret = vadc_write(vadc, VADC_ACCESS, VADC_ACCESS_DATA);
301 	if (ret)
302 		return ret;
303 
304 	ret = vadc_read(vadc, VADC_PERH_RESET_CTL3, &data);
305 	if (ret)
306 		return ret;
307 
308 	ret = vadc_write(vadc, VADC_ACCESS, VADC_ACCESS_DATA);
309 	if (ret)
310 		return ret;
311 
312 	data |= VADC_FOLLOW_WARM_RB;
313 
314 	return vadc_write(vadc, VADC_PERH_RESET_CTL3, data);
315 }
316 
317 static int vadc_set_state(struct vadc_priv *vadc, bool state)
318 {
319 	return vadc_write(vadc, VADC_EN_CTL1, state ? VADC_EN_CTL1_SET : 0);
320 }
321 
322 static void vadc_show_status(struct vadc_priv *vadc)
323 {
324 	u8 mode, sta1, chan, dig, en, req;
325 	int ret;
326 
327 	ret = vadc_read(vadc, VADC_MODE_CTL, &mode);
328 	if (ret)
329 		return;
330 
331 	ret = vadc_read(vadc, VADC_ADC_DIG_PARAM, &dig);
332 	if (ret)
333 		return;
334 
335 	ret = vadc_read(vadc, VADC_ADC_CH_SEL_CTL, &chan);
336 	if (ret)
337 		return;
338 
339 	ret = vadc_read(vadc, VADC_CONV_REQ, &req);
340 	if (ret)
341 		return;
342 
343 	ret = vadc_read(vadc, VADC_STATUS1, &sta1);
344 	if (ret)
345 		return;
346 
347 	ret = vadc_read(vadc, VADC_EN_CTL1, &en);
348 	if (ret)
349 		return;
350 
351 	dev_err(vadc->dev,
352 		"mode:%02x en:%02x chan:%02x dig:%02x req:%02x sta1:%02x\n",
353 		mode, en, chan, dig, req, sta1);
354 }
355 
356 static int vadc_configure(struct vadc_priv *vadc,
357 			  struct vadc_channel_prop *prop)
358 {
359 	u8 decimation, mode_ctrl;
360 	int ret;
361 
362 	/* Mode selection */
363 	mode_ctrl = (VADC_OP_MODE_NORMAL << VADC_OP_MODE_SHIFT) |
364 		     VADC_ADC_TRIM_EN | VADC_AMUX_TRIM_EN;
365 	ret = vadc_write(vadc, VADC_MODE_CTL, mode_ctrl);
366 	if (ret)
367 		return ret;
368 
369 	/* Channel selection */
370 	ret = vadc_write(vadc, VADC_ADC_CH_SEL_CTL, prop->channel);
371 	if (ret)
372 		return ret;
373 
374 	/* Digital parameter setup */
375 	decimation = prop->decimation << VADC_ADC_DIG_DEC_RATIO_SEL_SHIFT;
376 	ret = vadc_write(vadc, VADC_ADC_DIG_PARAM, decimation);
377 	if (ret)
378 		return ret;
379 
380 	/* HW settle time delay */
381 	ret = vadc_write(vadc, VADC_HW_SETTLE_DELAY, prop->hw_settle_time);
382 	if (ret)
383 		return ret;
384 
385 	ret = vadc_write(vadc, VADC_FAST_AVG_CTL, prop->avg_samples);
386 	if (ret)
387 		return ret;
388 
389 	if (prop->avg_samples)
390 		ret = vadc_write(vadc, VADC_FAST_AVG_EN, VADC_FAST_AVG_EN_SET);
391 	else
392 		ret = vadc_write(vadc, VADC_FAST_AVG_EN, 0);
393 
394 	return ret;
395 }
396 
397 static int vadc_poll_wait_eoc(struct vadc_priv *vadc, unsigned int interval_us)
398 {
399 	unsigned int count, retry;
400 	u8 sta1;
401 	int ret;
402 
403 	retry = interval_us / VADC_CONV_TIME_MIN_US;
404 
405 	for (count = 0; count < retry; count++) {
406 		ret = vadc_read(vadc, VADC_STATUS1, &sta1);
407 		if (ret)
408 			return ret;
409 
410 		sta1 &= VADC_STATUS1_REQ_STS_EOC_MASK;
411 		if (sta1 == VADC_STATUS1_EOC)
412 			return 0;
413 
414 		usleep_range(VADC_CONV_TIME_MIN_US, VADC_CONV_TIME_MAX_US);
415 	}
416 
417 	vadc_show_status(vadc);
418 
419 	return -ETIMEDOUT;
420 }
421 
422 static int vadc_read_result(struct vadc_priv *vadc, u16 *data)
423 {
424 	int ret;
425 
426 	ret = regmap_bulk_read(vadc->regmap, vadc->base + VADC_DATA, data, 2);
427 	if (ret)
428 		return ret;
429 
430 	*data = clamp_t(u16, *data, VADC_MIN_ADC_CODE, VADC_MAX_ADC_CODE);
431 
432 	return 0;
433 }
434 
435 static struct vadc_channel_prop *vadc_get_channel(struct vadc_priv *vadc,
436 						  unsigned int num)
437 {
438 	unsigned int i;
439 
440 	for (i = 0; i < vadc->nchannels; i++)
441 		if (vadc->chan_props[i].channel == num)
442 			return &vadc->chan_props[i];
443 
444 	dev_dbg(vadc->dev, "no such channel %02x\n", num);
445 
446 	return NULL;
447 }
448 
449 static int vadc_do_conversion(struct vadc_priv *vadc,
450 			      struct vadc_channel_prop *prop, u16 *data)
451 {
452 	unsigned int timeout;
453 	int ret;
454 
455 	mutex_lock(&vadc->lock);
456 
457 	ret = vadc_configure(vadc, prop);
458 	if (ret)
459 		goto unlock;
460 
461 	if (!vadc->poll_eoc)
462 		reinit_completion(&vadc->complete);
463 
464 	ret = vadc_set_state(vadc, true);
465 	if (ret)
466 		goto unlock;
467 
468 	ret = vadc_write(vadc, VADC_CONV_REQ, VADC_CONV_REQ_SET);
469 	if (ret)
470 		goto err_disable;
471 
472 	timeout = BIT(prop->avg_samples) * VADC_CONV_TIME_MIN_US * 2;
473 
474 	if (vadc->poll_eoc) {
475 		ret = vadc_poll_wait_eoc(vadc, timeout);
476 	} else {
477 		ret = wait_for_completion_timeout(&vadc->complete, timeout);
478 		if (!ret) {
479 			ret = -ETIMEDOUT;
480 			goto err_disable;
481 		}
482 
483 		/* Double check conversion status */
484 		ret = vadc_poll_wait_eoc(vadc, VADC_CONV_TIME_MIN_US);
485 		if (ret)
486 			goto err_disable;
487 	}
488 
489 	ret = vadc_read_result(vadc, data);
490 
491 err_disable:
492 	vadc_set_state(vadc, false);
493 	if (ret)
494 		dev_err(vadc->dev, "conversion failed\n");
495 unlock:
496 	mutex_unlock(&vadc->lock);
497 	return ret;
498 }
499 
500 static int vadc_measure_ref_points(struct vadc_priv *vadc)
501 {
502 	struct vadc_channel_prop *prop;
503 	u16 read_1, read_2;
504 	int ret;
505 
506 	vadc->graph[VADC_CALIB_RATIOMETRIC].dx = VADC_RATIOMETRIC_RANGE;
507 	vadc->graph[VADC_CALIB_ABSOLUTE].dx = VADC_ABSOLUTE_RANGE_UV;
508 
509 	prop = vadc_get_channel(vadc, VADC_REF_1250MV);
510 	ret = vadc_do_conversion(vadc, prop, &read_1);
511 	if (ret)
512 		goto err;
513 
514 	/* Try with buffered 625mV channel first */
515 	prop = vadc_get_channel(vadc, VADC_SPARE1);
516 	if (!prop)
517 		prop = vadc_get_channel(vadc, VADC_REF_625MV);
518 
519 	ret = vadc_do_conversion(vadc, prop, &read_2);
520 	if (ret)
521 		goto err;
522 
523 	if (read_1 == read_2) {
524 		ret = -EINVAL;
525 		goto err;
526 	}
527 
528 	vadc->graph[VADC_CALIB_ABSOLUTE].dy = read_1 - read_2;
529 	vadc->graph[VADC_CALIB_ABSOLUTE].gnd = read_2;
530 
531 	/* Ratiometric calibration */
532 	prop = vadc_get_channel(vadc, VADC_VDD_VADC);
533 	ret = vadc_do_conversion(vadc, prop, &read_1);
534 	if (ret)
535 		goto err;
536 
537 	prop = vadc_get_channel(vadc, VADC_GND_REF);
538 	ret = vadc_do_conversion(vadc, prop, &read_2);
539 	if (ret)
540 		goto err;
541 
542 	if (read_1 == read_2) {
543 		ret = -EINVAL;
544 		goto err;
545 	}
546 
547 	vadc->graph[VADC_CALIB_RATIOMETRIC].dy = read_1 - read_2;
548 	vadc->graph[VADC_CALIB_RATIOMETRIC].gnd = read_2;
549 err:
550 	if (ret)
551 		dev_err(vadc->dev, "measure reference points failed\n");
552 
553 	return ret;
554 }
555 
556 static int vadc_map_voltage_temp(const struct vadc_map_pt *pts,
557 				 u32 tablesize, s32 input, s64 *output)
558 {
559 	bool descending = 1;
560 	u32 i = 0;
561 
562 	if (!pts)
563 		return -EINVAL;
564 
565 	/* Check if table is descending or ascending */
566 	if (tablesize > 1) {
567 		if (pts[0].x < pts[1].x)
568 			descending = 0;
569 	}
570 
571 	while (i < tablesize) {
572 		if ((descending) && (pts[i].x < input)) {
573 			/* table entry is less than measured*/
574 			 /* value and table is descending, stop */
575 			break;
576 		} else if ((!descending) &&
577 				(pts[i].x > input)) {
578 			/* table entry is greater than measured*/
579 			/*value and table is ascending, stop */
580 			break;
581 		}
582 		i++;
583 	}
584 
585 	if (i == 0) {
586 		*output = pts[0].y;
587 	} else if (i == tablesize) {
588 		*output = pts[tablesize - 1].y;
589 	} else {
590 		/* result is between search_index and search_index-1 */
591 		/* interpolate linearly */
592 		*output = (((s32)((pts[i].y - pts[i - 1].y) *
593 			(input - pts[i - 1].x)) /
594 			(pts[i].x - pts[i - 1].x)) +
595 			pts[i - 1].y);
596 	}
597 
598 	return 0;
599 }
600 
601 static void vadc_scale_calib(struct vadc_priv *vadc, u16 adc_code,
602 			     const struct vadc_channel_prop *prop,
603 			     s64 *scale_voltage)
604 {
605 	*scale_voltage = (adc_code -
606 		vadc->graph[prop->calibration].gnd);
607 	*scale_voltage *= vadc->graph[prop->calibration].dx;
608 	*scale_voltage = div64_s64(*scale_voltage,
609 		vadc->graph[prop->calibration].dy);
610 	if (prop->calibration == VADC_CALIB_ABSOLUTE)
611 		*scale_voltage +=
612 		vadc->graph[prop->calibration].dx;
613 
614 	if (*scale_voltage < 0)
615 		*scale_voltage = 0;
616 }
617 
618 static int vadc_scale_volt(struct vadc_priv *vadc,
619 			   const struct vadc_channel_prop *prop, u16 adc_code,
620 			   int *result_uv)
621 {
622 	const struct vadc_prescale_ratio *prescale;
623 	s64 voltage = 0, result = 0;
624 
625 	vadc_scale_calib(vadc, adc_code, prop, &voltage);
626 
627 	prescale = &vadc_prescale_ratios[prop->prescale];
628 	voltage = voltage * prescale->den;
629 	result = div64_s64(voltage, prescale->num);
630 	*result_uv = result;
631 
632 	return 0;
633 }
634 
635 static int vadc_scale_therm(struct vadc_priv *vadc,
636 			    const struct vadc_channel_prop *prop, u16 adc_code,
637 			    int *result_mdec)
638 {
639 	s64 voltage = 0, result = 0;
640 
641 	vadc_scale_calib(vadc, adc_code, prop, &voltage);
642 
643 	if (prop->calibration == VADC_CALIB_ABSOLUTE)
644 		voltage = div64_s64(voltage, 1000);
645 
646 	vadc_map_voltage_temp(adcmap_100k_104ef_104fb,
647 			      ARRAY_SIZE(adcmap_100k_104ef_104fb),
648 			      voltage, &result);
649 	result *= 1000;
650 	*result_mdec = result;
651 
652 	return 0;
653 }
654 
655 static int vadc_scale_die_temp(struct vadc_priv *vadc,
656 			       const struct vadc_channel_prop *prop,
657 			       u16 adc_code, int *result_mdec)
658 {
659 	const struct vadc_prescale_ratio *prescale;
660 	s64 voltage = 0;
661 	u64 temp; /* Temporary variable for do_div */
662 
663 	vadc_scale_calib(vadc, adc_code, prop, &voltage);
664 
665 	if (voltage > 0) {
666 		prescale = &vadc_prescale_ratios[prop->prescale];
667 		temp = voltage * prescale->den;
668 		do_div(temp, prescale->num * 2);
669 		voltage = temp;
670 	} else {
671 		voltage = 0;
672 	}
673 
674 	voltage -= KELVINMIL_CELSIUSMIL;
675 	*result_mdec = voltage;
676 
677 	return 0;
678 }
679 
680 static int vadc_scale_chg_temp(struct vadc_priv *vadc,
681 			       const struct vadc_channel_prop *prop,
682 			       u16 adc_code, int *result_mdec)
683 {
684 	const struct vadc_prescale_ratio *prescale;
685 	s64 voltage = 0, result = 0;
686 
687 	vadc_scale_calib(vadc, adc_code, prop, &voltage);
688 
689 	prescale = &vadc_prescale_ratios[prop->prescale];
690 	voltage = voltage * prescale->den;
691 	voltage = div64_s64(voltage, prescale->num);
692 	voltage = ((PMI_CHG_SCALE_1) * (voltage * 2));
693 	voltage = (voltage + PMI_CHG_SCALE_2);
694 	result =  div64_s64(voltage, 1000000);
695 	*result_mdec = result;
696 
697 	return 0;
698 }
699 
700 static int vadc_decimation_from_dt(u32 value)
701 {
702 	if (!is_power_of_2(value) || value < VADC_DECIMATION_MIN ||
703 	    value > VADC_DECIMATION_MAX)
704 		return -EINVAL;
705 
706 	return __ffs64(value / VADC_DECIMATION_MIN);
707 }
708 
709 static int vadc_prescaling_from_dt(u32 num, u32 den)
710 {
711 	unsigned int pre;
712 
713 	for (pre = 0; pre < ARRAY_SIZE(vadc_prescale_ratios); pre++)
714 		if (vadc_prescale_ratios[pre].num == num &&
715 		    vadc_prescale_ratios[pre].den == den)
716 			break;
717 
718 	if (pre == ARRAY_SIZE(vadc_prescale_ratios))
719 		return -EINVAL;
720 
721 	return pre;
722 }
723 
724 static int vadc_hw_settle_time_from_dt(u32 value)
725 {
726 	if ((value <= 1000 && value % 100) || (value > 1000 && value % 2000))
727 		return -EINVAL;
728 
729 	if (value <= 1000)
730 		value /= 100;
731 	else
732 		value = value / 2000 + 10;
733 
734 	return value;
735 }
736 
737 static int vadc_avg_samples_from_dt(u32 value)
738 {
739 	if (!is_power_of_2(value) || value > VADC_AVG_SAMPLES_MAX)
740 		return -EINVAL;
741 
742 	return __ffs64(value);
743 }
744 
745 static struct vadc_scale_fn scale_fn[] = {
746 	[SCALE_DEFAULT] = {vadc_scale_volt},
747 	[SCALE_THERM_100K_PULLUP] = {vadc_scale_therm},
748 	[SCALE_PMIC_THERM] = {vadc_scale_die_temp},
749 	[SCALE_XOTHERM] = {vadc_scale_therm},
750 	[SCALE_PMI_CHG_TEMP] = {vadc_scale_chg_temp},
751 };
752 
753 static int vadc_read_raw(struct iio_dev *indio_dev,
754 			 struct iio_chan_spec const *chan, int *val, int *val2,
755 			 long mask)
756 {
757 	struct vadc_priv *vadc = iio_priv(indio_dev);
758 	struct vadc_channel_prop *prop;
759 	u16 adc_code;
760 	int ret;
761 
762 	switch (mask) {
763 	case IIO_CHAN_INFO_PROCESSED:
764 		prop = &vadc->chan_props[chan->address];
765 		ret = vadc_do_conversion(vadc, prop, &adc_code);
766 		if (ret)
767 			break;
768 
769 		scale_fn[prop->scale_fn].scale(vadc, prop, adc_code, val);
770 
771 		return IIO_VAL_INT;
772 	case IIO_CHAN_INFO_RAW:
773 		prop = &vadc->chan_props[chan->address];
774 		ret = vadc_do_conversion(vadc, prop, &adc_code);
775 		if (ret)
776 			break;
777 
778 		*val = (int)adc_code;
779 		return IIO_VAL_INT;
780 	default:
781 		ret = -EINVAL;
782 		break;
783 	}
784 
785 	return ret;
786 }
787 
788 static int vadc_of_xlate(struct iio_dev *indio_dev,
789 			 const struct of_phandle_args *iiospec)
790 {
791 	struct vadc_priv *vadc = iio_priv(indio_dev);
792 	unsigned int i;
793 
794 	for (i = 0; i < vadc->nchannels; i++)
795 		if (vadc->iio_chans[i].channel == iiospec->args[0])
796 			return i;
797 
798 	return -EINVAL;
799 }
800 
801 static const struct iio_info vadc_info = {
802 	.read_raw = vadc_read_raw,
803 	.of_xlate = vadc_of_xlate,
804 	.driver_module = THIS_MODULE,
805 };
806 
807 struct vadc_channels {
808 	const char *datasheet_name;
809 	unsigned int prescale_index;
810 	enum iio_chan_type type;
811 	long info_mask;
812 	unsigned int scale_fn;
813 };
814 
815 #define VADC_CHAN(_dname, _type, _mask, _pre, _scale)			\
816 	[VADC_##_dname] = {						\
817 		.datasheet_name = __stringify(_dname),			\
818 		.prescale_index = _pre,					\
819 		.type = _type,						\
820 		.info_mask = _mask,					\
821 		.scale_fn = _scale					\
822 	},								\
823 
824 #define VADC_NO_CHAN(_dname, _type, _mask, _pre)			\
825 	[VADC_##_dname] = {						\
826 		.datasheet_name = __stringify(_dname),			\
827 		.prescale_index = _pre,					\
828 		.type = _type,						\
829 		.info_mask = _mask					\
830 	},
831 
832 #define VADC_CHAN_TEMP(_dname, _pre, _scale)				\
833 	VADC_CHAN(_dname, IIO_TEMP,					\
834 		BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_PROCESSED),	\
835 		_pre, _scale)						\
836 
837 #define VADC_CHAN_VOLT(_dname, _pre, _scale)				\
838 	VADC_CHAN(_dname, IIO_VOLTAGE,					\
839 		  BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_PROCESSED),\
840 		  _pre, _scale)						\
841 
842 #define VADC_CHAN_NO_SCALE(_dname, _pre)				\
843 	VADC_NO_CHAN(_dname, IIO_VOLTAGE,				\
844 		  BIT(IIO_CHAN_INFO_RAW),				\
845 		  _pre)							\
846 
847 /*
848  * The array represents all possible ADC channels found in the supported PMICs.
849  * Every index in the array is equal to the channel number per datasheet. The
850  * gaps in the array should be treated as reserved channels.
851  */
852 static const struct vadc_channels vadc_chans[] = {
853 	VADC_CHAN_VOLT(USBIN, 4, SCALE_DEFAULT)
854 	VADC_CHAN_VOLT(DCIN, 4, SCALE_DEFAULT)
855 	VADC_CHAN_NO_SCALE(VCHG_SNS, 3)
856 	VADC_CHAN_NO_SCALE(SPARE1_03, 1)
857 	VADC_CHAN_NO_SCALE(USB_ID_MV, 1)
858 	VADC_CHAN_VOLT(VCOIN, 1, SCALE_DEFAULT)
859 	VADC_CHAN_NO_SCALE(VBAT_SNS, 1)
860 	VADC_CHAN_VOLT(VSYS, 1, SCALE_DEFAULT)
861 	VADC_CHAN_TEMP(DIE_TEMP, 0, SCALE_PMIC_THERM)
862 	VADC_CHAN_VOLT(REF_625MV, 0, SCALE_DEFAULT)
863 	VADC_CHAN_VOLT(REF_1250MV, 0, SCALE_DEFAULT)
864 	VADC_CHAN_NO_SCALE(CHG_TEMP, 0)
865 	VADC_CHAN_NO_SCALE(SPARE1, 0)
866 	VADC_CHAN_TEMP(SPARE2, 0, SCALE_PMI_CHG_TEMP)
867 	VADC_CHAN_VOLT(GND_REF, 0, SCALE_DEFAULT)
868 	VADC_CHAN_VOLT(VDD_VADC, 0, SCALE_DEFAULT)
869 
870 	VADC_CHAN_NO_SCALE(P_MUX1_1_1, 0)
871 	VADC_CHAN_NO_SCALE(P_MUX2_1_1, 0)
872 	VADC_CHAN_NO_SCALE(P_MUX3_1_1, 0)
873 	VADC_CHAN_NO_SCALE(P_MUX4_1_1, 0)
874 	VADC_CHAN_NO_SCALE(P_MUX5_1_1, 0)
875 	VADC_CHAN_NO_SCALE(P_MUX6_1_1, 0)
876 	VADC_CHAN_NO_SCALE(P_MUX7_1_1, 0)
877 	VADC_CHAN_NO_SCALE(P_MUX8_1_1, 0)
878 	VADC_CHAN_NO_SCALE(P_MUX9_1_1, 0)
879 	VADC_CHAN_NO_SCALE(P_MUX10_1_1, 0)
880 	VADC_CHAN_NO_SCALE(P_MUX11_1_1, 0)
881 	VADC_CHAN_NO_SCALE(P_MUX12_1_1, 0)
882 	VADC_CHAN_NO_SCALE(P_MUX13_1_1, 0)
883 	VADC_CHAN_NO_SCALE(P_MUX14_1_1, 0)
884 	VADC_CHAN_NO_SCALE(P_MUX15_1_1, 0)
885 	VADC_CHAN_NO_SCALE(P_MUX16_1_1, 0)
886 
887 	VADC_CHAN_NO_SCALE(P_MUX1_1_3, 1)
888 	VADC_CHAN_NO_SCALE(P_MUX2_1_3, 1)
889 	VADC_CHAN_NO_SCALE(P_MUX3_1_3, 1)
890 	VADC_CHAN_NO_SCALE(P_MUX4_1_3, 1)
891 	VADC_CHAN_NO_SCALE(P_MUX5_1_3, 1)
892 	VADC_CHAN_NO_SCALE(P_MUX6_1_3, 1)
893 	VADC_CHAN_NO_SCALE(P_MUX7_1_3, 1)
894 	VADC_CHAN_NO_SCALE(P_MUX8_1_3, 1)
895 	VADC_CHAN_NO_SCALE(P_MUX9_1_3, 1)
896 	VADC_CHAN_NO_SCALE(P_MUX10_1_3, 1)
897 	VADC_CHAN_NO_SCALE(P_MUX11_1_3, 1)
898 	VADC_CHAN_NO_SCALE(P_MUX12_1_3, 1)
899 	VADC_CHAN_NO_SCALE(P_MUX13_1_3, 1)
900 	VADC_CHAN_NO_SCALE(P_MUX14_1_3, 1)
901 	VADC_CHAN_NO_SCALE(P_MUX15_1_3, 1)
902 	VADC_CHAN_NO_SCALE(P_MUX16_1_3, 1)
903 
904 	VADC_CHAN_NO_SCALE(LR_MUX1_BAT_THERM, 0)
905 	VADC_CHAN_NO_SCALE(LR_MUX2_BAT_ID, 0)
906 	VADC_CHAN_NO_SCALE(LR_MUX3_XO_THERM, 0)
907 	VADC_CHAN_NO_SCALE(LR_MUX4_AMUX_THM1, 0)
908 	VADC_CHAN_NO_SCALE(LR_MUX5_AMUX_THM2, 0)
909 	VADC_CHAN_NO_SCALE(LR_MUX6_AMUX_THM3, 0)
910 	VADC_CHAN_NO_SCALE(LR_MUX7_HW_ID, 0)
911 	VADC_CHAN_NO_SCALE(LR_MUX8_AMUX_THM4, 0)
912 	VADC_CHAN_NO_SCALE(LR_MUX9_AMUX_THM5, 0)
913 	VADC_CHAN_NO_SCALE(LR_MUX10_USB_ID, 0)
914 	VADC_CHAN_NO_SCALE(AMUX_PU1, 0)
915 	VADC_CHAN_NO_SCALE(AMUX_PU2, 0)
916 	VADC_CHAN_NO_SCALE(LR_MUX3_BUF_XO_THERM, 0)
917 
918 	VADC_CHAN_NO_SCALE(LR_MUX1_PU1_BAT_THERM, 0)
919 	VADC_CHAN_NO_SCALE(LR_MUX2_PU1_BAT_ID, 0)
920 	VADC_CHAN_NO_SCALE(LR_MUX3_PU1_XO_THERM, 0)
921 	VADC_CHAN_TEMP(LR_MUX4_PU1_AMUX_THM1, 0, SCALE_THERM_100K_PULLUP)
922 	VADC_CHAN_TEMP(LR_MUX5_PU1_AMUX_THM2, 0, SCALE_THERM_100K_PULLUP)
923 	VADC_CHAN_TEMP(LR_MUX6_PU1_AMUX_THM3, 0, SCALE_THERM_100K_PULLUP)
924 	VADC_CHAN_NO_SCALE(LR_MUX7_PU1_AMUX_HW_ID, 0)
925 	VADC_CHAN_TEMP(LR_MUX8_PU1_AMUX_THM4, 0, SCALE_THERM_100K_PULLUP)
926 	VADC_CHAN_TEMP(LR_MUX9_PU1_AMUX_THM5, 0, SCALE_THERM_100K_PULLUP)
927 	VADC_CHAN_NO_SCALE(LR_MUX10_PU1_AMUX_USB_ID, 0)
928 	VADC_CHAN_TEMP(LR_MUX3_BUF_PU1_XO_THERM, 0, SCALE_XOTHERM)
929 
930 	VADC_CHAN_NO_SCALE(LR_MUX1_PU2_BAT_THERM, 0)
931 	VADC_CHAN_NO_SCALE(LR_MUX2_PU2_BAT_ID, 0)
932 	VADC_CHAN_NO_SCALE(LR_MUX3_PU2_XO_THERM, 0)
933 	VADC_CHAN_NO_SCALE(LR_MUX4_PU2_AMUX_THM1, 0)
934 	VADC_CHAN_NO_SCALE(LR_MUX5_PU2_AMUX_THM2, 0)
935 	VADC_CHAN_NO_SCALE(LR_MUX6_PU2_AMUX_THM3, 0)
936 	VADC_CHAN_NO_SCALE(LR_MUX7_PU2_AMUX_HW_ID, 0)
937 	VADC_CHAN_NO_SCALE(LR_MUX8_PU2_AMUX_THM4, 0)
938 	VADC_CHAN_NO_SCALE(LR_MUX9_PU2_AMUX_THM5, 0)
939 	VADC_CHAN_NO_SCALE(LR_MUX10_PU2_AMUX_USB_ID, 0)
940 	VADC_CHAN_NO_SCALE(LR_MUX3_BUF_PU2_XO_THERM, 0)
941 
942 	VADC_CHAN_NO_SCALE(LR_MUX1_PU1_PU2_BAT_THERM, 0)
943 	VADC_CHAN_NO_SCALE(LR_MUX2_PU1_PU2_BAT_ID, 0)
944 	VADC_CHAN_NO_SCALE(LR_MUX3_PU1_PU2_XO_THERM, 0)
945 	VADC_CHAN_NO_SCALE(LR_MUX4_PU1_PU2_AMUX_THM1, 0)
946 	VADC_CHAN_NO_SCALE(LR_MUX5_PU1_PU2_AMUX_THM2, 0)
947 	VADC_CHAN_NO_SCALE(LR_MUX6_PU1_PU2_AMUX_THM3, 0)
948 	VADC_CHAN_NO_SCALE(LR_MUX7_PU1_PU2_AMUX_HW_ID, 0)
949 	VADC_CHAN_NO_SCALE(LR_MUX8_PU1_PU2_AMUX_THM4, 0)
950 	VADC_CHAN_NO_SCALE(LR_MUX9_PU1_PU2_AMUX_THM5, 0)
951 	VADC_CHAN_NO_SCALE(LR_MUX10_PU1_PU2_AMUX_USB_ID, 0)
952 	VADC_CHAN_NO_SCALE(LR_MUX3_BUF_PU1_PU2_XO_THERM, 0)
953 };
954 
955 static int vadc_get_dt_channel_data(struct device *dev,
956 				    struct vadc_channel_prop *prop,
957 				    struct device_node *node)
958 {
959 	const char *name = node->name;
960 	u32 chan, value, varr[2];
961 	int ret;
962 
963 	ret = of_property_read_u32(node, "reg", &chan);
964 	if (ret) {
965 		dev_err(dev, "invalid channel number %s\n", name);
966 		return ret;
967 	}
968 
969 	if (chan > VADC_CHAN_MAX || chan < VADC_CHAN_MIN) {
970 		dev_err(dev, "%s invalid channel number %d\n", name, chan);
971 		return -EINVAL;
972 	}
973 
974 	/* the channel has DT description */
975 	prop->channel = chan;
976 
977 	ret = of_property_read_u32(node, "qcom,decimation", &value);
978 	if (!ret) {
979 		ret = vadc_decimation_from_dt(value);
980 		if (ret < 0) {
981 			dev_err(dev, "%02x invalid decimation %d\n",
982 				chan, value);
983 			return ret;
984 		}
985 		prop->decimation = ret;
986 	} else {
987 		prop->decimation = VADC_DEF_DECIMATION;
988 	}
989 
990 	ret = of_property_read_u32_array(node, "qcom,pre-scaling", varr, 2);
991 	if (!ret) {
992 		ret = vadc_prescaling_from_dt(varr[0], varr[1]);
993 		if (ret < 0) {
994 			dev_err(dev, "%02x invalid pre-scaling <%d %d>\n",
995 				chan, varr[0], varr[1]);
996 			return ret;
997 		}
998 		prop->prescale = ret;
999 	} else {
1000 		prop->prescale = vadc_chans[prop->channel].prescale_index;
1001 	}
1002 
1003 	ret = of_property_read_u32(node, "qcom,hw-settle-time", &value);
1004 	if (!ret) {
1005 		ret = vadc_hw_settle_time_from_dt(value);
1006 		if (ret < 0) {
1007 			dev_err(dev, "%02x invalid hw-settle-time %d us\n",
1008 				chan, value);
1009 			return ret;
1010 		}
1011 		prop->hw_settle_time = ret;
1012 	} else {
1013 		prop->hw_settle_time = VADC_DEF_HW_SETTLE_TIME;
1014 	}
1015 
1016 	ret = of_property_read_u32(node, "qcom,avg-samples", &value);
1017 	if (!ret) {
1018 		ret = vadc_avg_samples_from_dt(value);
1019 		if (ret < 0) {
1020 			dev_err(dev, "%02x invalid avg-samples %d\n",
1021 				chan, value);
1022 			return ret;
1023 		}
1024 		prop->avg_samples = ret;
1025 	} else {
1026 		prop->avg_samples = VADC_DEF_AVG_SAMPLES;
1027 	}
1028 
1029 	if (of_property_read_bool(node, "qcom,ratiometric"))
1030 		prop->calibration = VADC_CALIB_RATIOMETRIC;
1031 	else
1032 		prop->calibration = VADC_CALIB_ABSOLUTE;
1033 
1034 	dev_dbg(dev, "%02x name %s\n", chan, name);
1035 
1036 	return 0;
1037 }
1038 
1039 static int vadc_get_dt_data(struct vadc_priv *vadc, struct device_node *node)
1040 {
1041 	const struct vadc_channels *vadc_chan;
1042 	struct iio_chan_spec *iio_chan;
1043 	struct vadc_channel_prop prop;
1044 	struct device_node *child;
1045 	unsigned int index = 0;
1046 	int ret;
1047 
1048 	vadc->nchannels = of_get_available_child_count(node);
1049 	if (!vadc->nchannels)
1050 		return -EINVAL;
1051 
1052 	vadc->iio_chans = devm_kcalloc(vadc->dev, vadc->nchannels,
1053 				       sizeof(*vadc->iio_chans), GFP_KERNEL);
1054 	if (!vadc->iio_chans)
1055 		return -ENOMEM;
1056 
1057 	vadc->chan_props = devm_kcalloc(vadc->dev, vadc->nchannels,
1058 					sizeof(*vadc->chan_props), GFP_KERNEL);
1059 	if (!vadc->chan_props)
1060 		return -ENOMEM;
1061 
1062 	iio_chan = vadc->iio_chans;
1063 
1064 	for_each_available_child_of_node(node, child) {
1065 		ret = vadc_get_dt_channel_data(vadc->dev, &prop, child);
1066 		if (ret) {
1067 			of_node_put(child);
1068 			return ret;
1069 		}
1070 
1071 		prop.scale_fn = vadc_chans[prop.channel].scale_fn;
1072 		vadc->chan_props[index] = prop;
1073 
1074 		vadc_chan = &vadc_chans[prop.channel];
1075 
1076 		iio_chan->channel = prop.channel;
1077 		iio_chan->datasheet_name = vadc_chan->datasheet_name;
1078 		iio_chan->info_mask_separate = vadc_chan->info_mask;
1079 		iio_chan->type = vadc_chan->type;
1080 		iio_chan->indexed = 1;
1081 		iio_chan->address = index++;
1082 
1083 		iio_chan++;
1084 	}
1085 
1086 	/* These channels are mandatory, they are used as reference points */
1087 	if (!vadc_get_channel(vadc, VADC_REF_1250MV)) {
1088 		dev_err(vadc->dev, "Please define 1.25V channel\n");
1089 		return -ENODEV;
1090 	}
1091 
1092 	if (!vadc_get_channel(vadc, VADC_REF_625MV)) {
1093 		dev_err(vadc->dev, "Please define 0.625V channel\n");
1094 		return -ENODEV;
1095 	}
1096 
1097 	if (!vadc_get_channel(vadc, VADC_VDD_VADC)) {
1098 		dev_err(vadc->dev, "Please define VDD channel\n");
1099 		return -ENODEV;
1100 	}
1101 
1102 	if (!vadc_get_channel(vadc, VADC_GND_REF)) {
1103 		dev_err(vadc->dev, "Please define GND channel\n");
1104 		return -ENODEV;
1105 	}
1106 
1107 	return 0;
1108 }
1109 
1110 static irqreturn_t vadc_isr(int irq, void *dev_id)
1111 {
1112 	struct vadc_priv *vadc = dev_id;
1113 
1114 	complete(&vadc->complete);
1115 
1116 	return IRQ_HANDLED;
1117 }
1118 
1119 static int vadc_check_revision(struct vadc_priv *vadc)
1120 {
1121 	u8 val;
1122 	int ret;
1123 
1124 	ret = vadc_read(vadc, VADC_PERPH_TYPE, &val);
1125 	if (ret)
1126 		return ret;
1127 
1128 	if (val < VADC_PERPH_TYPE_ADC) {
1129 		dev_err(vadc->dev, "%d is not ADC\n", val);
1130 		return -ENODEV;
1131 	}
1132 
1133 	ret = vadc_read(vadc, VADC_PERPH_SUBTYPE, &val);
1134 	if (ret)
1135 		return ret;
1136 
1137 	if (val < VADC_PERPH_SUBTYPE_VADC) {
1138 		dev_err(vadc->dev, "%d is not VADC\n", val);
1139 		return -ENODEV;
1140 	}
1141 
1142 	ret = vadc_read(vadc, VADC_REVISION2, &val);
1143 	if (ret)
1144 		return ret;
1145 
1146 	if (val < VADC_REVISION2_SUPPORTED_VADC) {
1147 		dev_err(vadc->dev, "revision %d not supported\n", val);
1148 		return -ENODEV;
1149 	}
1150 
1151 	return 0;
1152 }
1153 
1154 static int vadc_probe(struct platform_device *pdev)
1155 {
1156 	struct device_node *node = pdev->dev.of_node;
1157 	struct device *dev = &pdev->dev;
1158 	struct iio_dev *indio_dev;
1159 	struct vadc_priv *vadc;
1160 	struct regmap *regmap;
1161 	int ret, irq_eoc;
1162 	u32 reg;
1163 
1164 	regmap = dev_get_regmap(dev->parent, NULL);
1165 	if (!regmap)
1166 		return -ENODEV;
1167 
1168 	ret = of_property_read_u32(node, "reg", &reg);
1169 	if (ret < 0)
1170 		return ret;
1171 
1172 	indio_dev = devm_iio_device_alloc(dev, sizeof(*vadc));
1173 	if (!indio_dev)
1174 		return -ENOMEM;
1175 
1176 	vadc = iio_priv(indio_dev);
1177 	vadc->regmap = regmap;
1178 	vadc->dev = dev;
1179 	vadc->base = reg;
1180 	vadc->are_ref_measured = false;
1181 	init_completion(&vadc->complete);
1182 	mutex_init(&vadc->lock);
1183 
1184 	ret = vadc_check_revision(vadc);
1185 	if (ret)
1186 		return ret;
1187 
1188 	ret = vadc_get_dt_data(vadc, node);
1189 	if (ret)
1190 		return ret;
1191 
1192 	irq_eoc = platform_get_irq(pdev, 0);
1193 	if (irq_eoc < 0) {
1194 		if (irq_eoc == -EPROBE_DEFER || irq_eoc == -EINVAL)
1195 			return irq_eoc;
1196 		vadc->poll_eoc = true;
1197 	} else {
1198 		ret = devm_request_irq(dev, irq_eoc, vadc_isr, 0,
1199 				       "spmi-vadc", vadc);
1200 		if (ret)
1201 			return ret;
1202 	}
1203 
1204 	ret = vadc_reset(vadc);
1205 	if (ret) {
1206 		dev_err(dev, "reset failed\n");
1207 		return ret;
1208 	}
1209 
1210 	ret = vadc_measure_ref_points(vadc);
1211 	if (ret)
1212 		return ret;
1213 
1214 	indio_dev->dev.parent = dev;
1215 	indio_dev->dev.of_node = node;
1216 	indio_dev->name = pdev->name;
1217 	indio_dev->modes = INDIO_DIRECT_MODE;
1218 	indio_dev->info = &vadc_info;
1219 	indio_dev->channels = vadc->iio_chans;
1220 	indio_dev->num_channels = vadc->nchannels;
1221 
1222 	return devm_iio_device_register(dev, indio_dev);
1223 }
1224 
1225 static const struct of_device_id vadc_match_table[] = {
1226 	{ .compatible = "qcom,spmi-vadc" },
1227 	{ }
1228 };
1229 MODULE_DEVICE_TABLE(of, vadc_match_table);
1230 
1231 static struct platform_driver vadc_driver = {
1232 	.driver = {
1233 		   .name = "qcom-spmi-vadc",
1234 		   .of_match_table = vadc_match_table,
1235 	},
1236 	.probe = vadc_probe,
1237 };
1238 module_platform_driver(vadc_driver);
1239 
1240 MODULE_ALIAS("platform:qcom-spmi-vadc");
1241 MODULE_DESCRIPTION("Qualcomm SPMI PMIC voltage ADC driver");
1242 MODULE_LICENSE("GPL v2");
1243 MODULE_AUTHOR("Stanimir Varbanov <svarbanov@mm-sol.com>");
1244 MODULE_AUTHOR("Ivan T. Ivanov <iivanov@mm-sol.com>");
1245