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