Linux 4.19.133
[linux/fpc-iii.git] / drivers / iio / adc / qcom-spmi-vadc.c
blob3680e0d474120e9cd83afb1ac448b72ea8f8929b
1 /*
2 * Copyright (c) 2012-2016, The Linux Foundation. All rights reserved.
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.
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.
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>
29 #include <dt-bindings/iio/qcom,spmi-vadc.h>
31 #include "qcom-vadc-common.h"
33 /* VADC register and bit definitions */
34 #define VADC_REVISION2 0x1
35 #define VADC_REVISION2_SUPPORTED_VADC 1
37 #define VADC_PERPH_TYPE 0x4
38 #define VADC_PERPH_TYPE_ADC 8
40 #define VADC_PERPH_SUBTYPE 0x5
41 #define VADC_PERPH_SUBTYPE_VADC 1
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
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)
55 #define VADC_EN_CTL1 0x46
56 #define VADC_EN_CTL1_SET BIT(7)
58 #define VADC_ADC_CH_SEL_CTL 0x48
60 #define VADC_ADC_DIG_PARAM 0x50
61 #define VADC_ADC_DIG_DEC_RATIO_SEL_SHIFT 2
63 #define VADC_HW_SETTLE_DELAY 0x51
65 #define VADC_CONV_REQ 0x52
66 #define VADC_CONV_REQ_SET BIT(7)
68 #define VADC_FAST_AVG_CTL 0x5a
69 #define VADC_FAST_AVG_EN 0x5b
70 #define VADC_FAST_AVG_EN_SET BIT(7)
72 #define VADC_ACCESS 0xd0
73 #define VADC_ACCESS_DATA 0xa5
75 #define VADC_PERH_RESET_CTL3 0xda
76 #define VADC_FOLLOW_WARM_RB BIT(2)
78 #define VADC_DATA 0x60 /* 16 bits */
80 #define VADC_CHAN_MIN VADC_USBIN
81 #define VADC_CHAN_MAX VADC_LR_MUX3_BUF_PU1_PU2_XO_THERM
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.
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;
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.
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;
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}
145 static int vadc_read(struct vadc_priv *vadc, u16 offset, u8 *data)
147 return regmap_bulk_read(vadc->regmap, vadc->base + offset, data, 1);
150 static int vadc_write(struct vadc_priv *vadc, u16 offset, u8 data)
152 return regmap_write(vadc->regmap, vadc->base + offset, data);
155 static int vadc_reset(struct vadc_priv *vadc)
157 u8 data;
158 int ret;
160 ret = vadc_write(vadc, VADC_ACCESS, VADC_ACCESS_DATA);
161 if (ret)
162 return ret;
164 ret = vadc_read(vadc, VADC_PERH_RESET_CTL3, &data);
165 if (ret)
166 return ret;
168 ret = vadc_write(vadc, VADC_ACCESS, VADC_ACCESS_DATA);
169 if (ret)
170 return ret;
172 data |= VADC_FOLLOW_WARM_RB;
174 return vadc_write(vadc, VADC_PERH_RESET_CTL3, data);
177 static int vadc_set_state(struct vadc_priv *vadc, bool state)
179 return vadc_write(vadc, VADC_EN_CTL1, state ? VADC_EN_CTL1_SET : 0);
182 static void vadc_show_status(struct vadc_priv *vadc)
184 u8 mode, sta1, chan, dig, en, req;
185 int ret;
187 ret = vadc_read(vadc, VADC_MODE_CTL, &mode);
188 if (ret)
189 return;
191 ret = vadc_read(vadc, VADC_ADC_DIG_PARAM, &dig);
192 if (ret)
193 return;
195 ret = vadc_read(vadc, VADC_ADC_CH_SEL_CTL, &chan);
196 if (ret)
197 return;
199 ret = vadc_read(vadc, VADC_CONV_REQ, &req);
200 if (ret)
201 return;
203 ret = vadc_read(vadc, VADC_STATUS1, &sta1);
204 if (ret)
205 return;
207 ret = vadc_read(vadc, VADC_EN_CTL1, &en);
208 if (ret)
209 return;
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);
216 static int vadc_configure(struct vadc_priv *vadc,
217 struct vadc_channel_prop *prop)
219 u8 decimation, mode_ctrl;
220 int ret;
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;
229 /* Channel selection */
230 ret = vadc_write(vadc, VADC_ADC_CH_SEL_CTL, prop->channel);
231 if (ret)
232 return ret;
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;
240 /* HW settle time delay */
241 ret = vadc_write(vadc, VADC_HW_SETTLE_DELAY, prop->hw_settle_time);
242 if (ret)
243 return ret;
245 ret = vadc_write(vadc, VADC_FAST_AVG_CTL, prop->avg_samples);
246 if (ret)
247 return ret;
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);
254 return ret;
257 static int vadc_poll_wait_eoc(struct vadc_priv *vadc, unsigned int interval_us)
259 unsigned int count, retry;
260 u8 sta1;
261 int ret;
263 retry = interval_us / VADC_CONV_TIME_MIN_US;
265 for (count = 0; count < retry; count++) {
266 ret = vadc_read(vadc, VADC_STATUS1, &sta1);
267 if (ret)
268 return ret;
270 sta1 &= VADC_STATUS1_REQ_STS_EOC_MASK;
271 if (sta1 == VADC_STATUS1_EOC)
272 return 0;
274 usleep_range(VADC_CONV_TIME_MIN_US, VADC_CONV_TIME_MAX_US);
277 vadc_show_status(vadc);
279 return -ETIMEDOUT;
282 static int vadc_read_result(struct vadc_priv *vadc, u16 *data)
284 int ret;
286 ret = regmap_bulk_read(vadc->regmap, vadc->base + VADC_DATA, data, 2);
287 if (ret)
288 return ret;
290 *data = clamp_t(u16, *data, VADC_MIN_ADC_CODE, VADC_MAX_ADC_CODE);
292 return 0;
295 static struct vadc_channel_prop *vadc_get_channel(struct vadc_priv *vadc,
296 unsigned int num)
298 unsigned int i;
300 for (i = 0; i < vadc->nchannels; i++)
301 if (vadc->chan_props[i].channel == num)
302 return &vadc->chan_props[i];
304 dev_dbg(vadc->dev, "no such channel %02x\n", num);
306 return NULL;
309 static int vadc_do_conversion(struct vadc_priv *vadc,
310 struct vadc_channel_prop *prop, u16 *data)
312 unsigned int timeout;
313 int ret;
315 mutex_lock(&vadc->lock);
317 ret = vadc_configure(vadc, prop);
318 if (ret)
319 goto unlock;
321 if (!vadc->poll_eoc)
322 reinit_completion(&vadc->complete);
324 ret = vadc_set_state(vadc, true);
325 if (ret)
326 goto unlock;
328 ret = vadc_write(vadc, VADC_CONV_REQ, VADC_CONV_REQ_SET);
329 if (ret)
330 goto err_disable;
332 timeout = BIT(prop->avg_samples) * VADC_CONV_TIME_MIN_US * 2;
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;
343 /* Double check conversion status */
344 ret = vadc_poll_wait_eoc(vadc, VADC_CONV_TIME_MIN_US);
345 if (ret)
346 goto err_disable;
349 ret = vadc_read_result(vadc, data);
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;
360 static int vadc_measure_ref_points(struct vadc_priv *vadc)
362 struct vadc_channel_prop *prop;
363 u16 read_1, read_2;
364 int ret;
366 vadc->graph[VADC_CALIB_RATIOMETRIC].dx = VADC_RATIOMETRIC_RANGE;
367 vadc->graph[VADC_CALIB_ABSOLUTE].dx = VADC_ABSOLUTE_RANGE_UV;
369 prop = vadc_get_channel(vadc, VADC_REF_1250MV);
370 ret = vadc_do_conversion(vadc, prop, &read_1);
371 if (ret)
372 goto err;
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);
379 ret = vadc_do_conversion(vadc, prop, &read_2);
380 if (ret)
381 goto err;
383 if (read_1 == read_2) {
384 ret = -EINVAL;
385 goto err;
388 vadc->graph[VADC_CALIB_ABSOLUTE].dy = read_1 - read_2;
389 vadc->graph[VADC_CALIB_ABSOLUTE].gnd = read_2;
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;
397 prop = vadc_get_channel(vadc, VADC_GND_REF);
398 ret = vadc_do_conversion(vadc, prop, &read_2);
399 if (ret)
400 goto err;
402 if (read_1 == read_2) {
403 ret = -EINVAL;
404 goto err;
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");
413 return ret;
416 static int vadc_prescaling_from_dt(u32 num, u32 den)
418 unsigned int pre;
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;
425 if (pre == ARRAY_SIZE(vadc_prescale_ratios))
426 return -EINVAL;
428 return pre;
431 static int vadc_hw_settle_time_from_dt(u32 value)
433 if ((value <= 1000 && value % 100) || (value > 1000 && value % 2000))
434 return -EINVAL;
436 if (value <= 1000)
437 value /= 100;
438 else
439 value = value / 2000 + 10;
441 return value;
444 static int vadc_avg_samples_from_dt(u32 value)
446 if (!is_power_of_2(value) || value > VADC_AVG_SAMPLES_MAX)
447 return -EINVAL;
449 return __ffs64(value);
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)
456 struct vadc_priv *vadc = iio_priv(indio_dev);
457 struct vadc_channel_prop *prop;
458 u16 adc_code;
459 int ret;
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;
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;
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;
483 *val = (int)adc_code;
484 return IIO_VAL_INT;
485 default:
486 ret = -EINVAL;
487 break;
490 return ret;
493 static int vadc_of_xlate(struct iio_dev *indio_dev,
494 const struct of_phandle_args *iiospec)
496 struct vadc_priv *vadc = iio_priv(indio_dev);
497 unsigned int i;
499 for (i = 0; i < vadc->nchannels; i++)
500 if (vadc->iio_chans[i].channel == iiospec->args[0])
501 return i;
503 return -EINVAL;
506 static const struct iio_info vadc_info = {
507 .read_raw = vadc_read_raw,
508 .of_xlate = vadc_of_xlate,
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;
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 }, \
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 \
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) \
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) \
546 #define VADC_CHAN_NO_SCALE(_dname, _pre) \
547 VADC_NO_CHAN(_dname, IIO_VOLTAGE, \
548 BIT(IIO_CHAN_INFO_RAW), \
549 _pre) \
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.
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)
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)
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)
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)
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)
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)
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)
659 static int vadc_get_dt_channel_data(struct device *dev,
660 struct vadc_channel_prop *prop,
661 struct device_node *node)
663 const char *name = node->name;
664 u32 chan, value, varr[2];
665 int ret;
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;
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;
678 /* the channel has DT description */
679 prop->channel = chan;
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;
689 prop->decimation = ret;
690 } else {
691 prop->decimation = VADC_DEF_DECIMATION;
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;
702 prop->prescale = ret;
703 } else {
704 prop->prescale = vadc_chans[prop->channel].prescale_index;
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;
715 prop->hw_settle_time = ret;
716 } else {
717 prop->hw_settle_time = VADC_DEF_HW_SETTLE_TIME;
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;
728 prop->avg_samples = ret;
729 } else {
730 prop->avg_samples = VADC_DEF_AVG_SAMPLES;
733 if (of_property_read_bool(node, "qcom,ratiometric"))
734 prop->calibration = VADC_CALIB_RATIOMETRIC;
735 else
736 prop->calibration = VADC_CALIB_ABSOLUTE;
738 dev_dbg(dev, "%02x name %s\n", chan, name);
740 return 0;
743 static int vadc_get_dt_data(struct vadc_priv *vadc, struct device_node *node)
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;
752 vadc->nchannels = of_get_available_child_count(node);
753 if (!vadc->nchannels)
754 return -EINVAL;
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;
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;
766 iio_chan = vadc->iio_chans;
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;
775 prop.scale_fn_type = vadc_chans[prop.channel].scale_fn_type;
776 vadc->chan_props[index] = prop;
778 vadc_chan = &vadc_chans[prop.channel];
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++;
787 iio_chan++;
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;
796 if (!vadc_get_channel(vadc, VADC_REF_625MV)) {
797 dev_err(vadc->dev, "Please define 0.625V channel\n");
798 return -ENODEV;
801 if (!vadc_get_channel(vadc, VADC_VDD_VADC)) {
802 dev_err(vadc->dev, "Please define VDD channel\n");
803 return -ENODEV;
806 if (!vadc_get_channel(vadc, VADC_GND_REF)) {
807 dev_err(vadc->dev, "Please define GND channel\n");
808 return -ENODEV;
811 return 0;
814 static irqreturn_t vadc_isr(int irq, void *dev_id)
816 struct vadc_priv *vadc = dev_id;
818 complete(&vadc->complete);
820 return IRQ_HANDLED;
823 static int vadc_check_revision(struct vadc_priv *vadc)
825 u8 val;
826 int ret;
828 ret = vadc_read(vadc, VADC_PERPH_TYPE, &val);
829 if (ret)
830 return ret;
832 if (val < VADC_PERPH_TYPE_ADC) {
833 dev_err(vadc->dev, "%d is not ADC\n", val);
834 return -ENODEV;
837 ret = vadc_read(vadc, VADC_PERPH_SUBTYPE, &val);
838 if (ret)
839 return ret;
841 if (val < VADC_PERPH_SUBTYPE_VADC) {
842 dev_err(vadc->dev, "%d is not VADC\n", val);
843 return -ENODEV;
846 ret = vadc_read(vadc, VADC_REVISION2, &val);
847 if (ret)
848 return ret;
850 if (val < VADC_REVISION2_SUPPORTED_VADC) {
851 dev_err(vadc->dev, "revision %d not supported\n", val);
852 return -ENODEV;
855 return 0;
858 static int vadc_probe(struct platform_device *pdev)
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;
868 regmap = dev_get_regmap(dev->parent, NULL);
869 if (!regmap)
870 return -ENODEV;
872 ret = of_property_read_u32(node, "reg", &reg);
873 if (ret < 0)
874 return ret;
876 indio_dev = devm_iio_device_alloc(dev, sizeof(*vadc));
877 if (!indio_dev)
878 return -ENOMEM;
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);
888 ret = vadc_check_revision(vadc);
889 if (ret)
890 return ret;
892 ret = vadc_get_dt_data(vadc, node);
893 if (ret)
894 return ret;
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;
908 ret = vadc_reset(vadc);
909 if (ret) {
910 dev_err(dev, "reset failed\n");
911 return ret;
914 ret = vadc_measure_ref_points(vadc);
915 if (ret)
916 return ret;
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;
926 return devm_iio_device_register(dev, indio_dev);
929 static const struct of_device_id vadc_match_table[] = {
930 { .compatible = "qcom,spmi-vadc" },
933 MODULE_DEVICE_TABLE(of, vadc_match_table);
935 static struct platform_driver vadc_driver = {
936 .driver = {
937 .name = "qcom-spmi-vadc",
938 .of_match_table = vadc_match_table,
940 .probe = vadc_probe,
942 module_platform_driver(vadc_driver);
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>");