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