drm/panel-edp: Add BOE NV140FHM-NZ panel entry
[drm/drm-misc.git] / drivers / iio / adc / qcom-spmi-vadc.c
blob00a7f098202543891d7ae6c0ab2904b78b96b8fb
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2012-2016, The Linux Foundation. All rights reserved.
4 */
6 #include <linux/bitops.h>
7 #include <linux/completion.h>
8 #include <linux/delay.h>
9 #include <linux/err.h>
10 #include <linux/iio/adc/qcom-vadc-common.h>
11 #include <linux/iio/iio.h>
12 #include <linux/interrupt.h>
13 #include <linux/kernel.h>
14 #include <linux/math64.h>
15 #include <linux/module.h>
16 #include <linux/mod_devicetable.h>
17 #include <linux/platform_device.h>
18 #include <linux/property.h>
19 #include <linux/regmap.h>
20 #include <linux/slab.h>
21 #include <linux/log2.h>
23 #include <dt-bindings/iio/qcom,spmi-vadc.h>
25 /* VADC register and bit definitions */
26 #define VADC_REVISION2 0x1
27 #define VADC_REVISION2_SUPPORTED_VADC 1
29 #define VADC_PERPH_TYPE 0x4
30 #define VADC_PERPH_TYPE_ADC 8
32 #define VADC_PERPH_SUBTYPE 0x5
33 #define VADC_PERPH_SUBTYPE_VADC 1
35 #define VADC_STATUS1 0x8
36 #define VADC_STATUS1_OP_MODE 4
37 #define VADC_STATUS1_REQ_STS BIT(1)
38 #define VADC_STATUS1_EOC BIT(0)
39 #define VADC_STATUS1_REQ_STS_EOC_MASK 0x3
41 #define VADC_MODE_CTL 0x40
42 #define VADC_OP_MODE_SHIFT 3
43 #define VADC_OP_MODE_NORMAL 0
44 #define VADC_AMUX_TRIM_EN BIT(1)
45 #define VADC_ADC_TRIM_EN BIT(0)
47 #define VADC_EN_CTL1 0x46
48 #define VADC_EN_CTL1_SET BIT(7)
50 #define VADC_ADC_CH_SEL_CTL 0x48
52 #define VADC_ADC_DIG_PARAM 0x50
53 #define VADC_ADC_DIG_DEC_RATIO_SEL_SHIFT 2
55 #define VADC_HW_SETTLE_DELAY 0x51
57 #define VADC_CONV_REQ 0x52
58 #define VADC_CONV_REQ_SET BIT(7)
60 #define VADC_FAST_AVG_CTL 0x5a
61 #define VADC_FAST_AVG_EN 0x5b
62 #define VADC_FAST_AVG_EN_SET BIT(7)
64 #define VADC_ACCESS 0xd0
65 #define VADC_ACCESS_DATA 0xa5
67 #define VADC_PERH_RESET_CTL3 0xda
68 #define VADC_FOLLOW_WARM_RB BIT(2)
70 #define VADC_DATA 0x60 /* 16 bits */
72 #define VADC_CHAN_MIN VADC_USBIN
73 #define VADC_CHAN_MAX VADC_LR_MUX3_BUF_PU1_PU2_XO_THERM
75 /**
76 * struct vadc_channel_prop - VADC channel property.
77 * @channel: channel number, refer to the channel list.
78 * @calibration: calibration type.
79 * @decimation: sampling rate supported for the channel.
80 * @prescale: channel scaling performed on the input signal.
81 * @hw_settle_time: the time between AMUX being configured and the
82 * start of conversion.
83 * @avg_samples: ability to provide single result from the ADC
84 * that is an average of multiple measurements.
85 * @scale_fn_type: Represents the scaling function to convert voltage
86 * physical units desired by the client for the channel.
87 * @channel_name: Channel name used in device tree.
89 struct vadc_channel_prop {
90 unsigned int channel;
91 enum vadc_calibration calibration;
92 unsigned int decimation;
93 unsigned int prescale;
94 unsigned int hw_settle_time;
95 unsigned int avg_samples;
96 enum vadc_scale_fn_type scale_fn_type;
97 const char *channel_name;
101 * struct vadc_priv - VADC private structure.
102 * @regmap: pointer to struct regmap.
103 * @dev: pointer to struct device.
104 * @base: base address for the ADC peripheral.
105 * @nchannels: number of VADC channels.
106 * @chan_props: array of VADC channel properties.
107 * @iio_chans: array of IIO channels specification.
108 * @are_ref_measured: are reference points measured.
109 * @poll_eoc: use polling instead of interrupt.
110 * @complete: VADC result notification after interrupt is received.
111 * @graph: store parameters for calibration.
112 * @lock: ADC lock for access to the peripheral.
114 struct vadc_priv {
115 struct regmap *regmap;
116 struct device *dev;
117 u16 base;
118 unsigned int nchannels;
119 struct vadc_channel_prop *chan_props;
120 struct iio_chan_spec *iio_chans;
121 bool are_ref_measured;
122 bool poll_eoc;
123 struct completion complete;
124 struct vadc_linear_graph graph[2];
125 struct mutex lock;
128 static const struct u32_fract vadc_prescale_ratios[] = {
129 { .numerator = 1, .denominator = 1 },
130 { .numerator = 1, .denominator = 3 },
131 { .numerator = 1, .denominator = 4 },
132 { .numerator = 1, .denominator = 6 },
133 { .numerator = 1, .denominator = 20 },
134 { .numerator = 1, .denominator = 8 },
135 { .numerator = 10, .denominator = 81 },
136 { .numerator = 1, .denominator = 10 },
139 static int vadc_read(struct vadc_priv *vadc, u16 offset, u8 *data)
141 return regmap_bulk_read(vadc->regmap, vadc->base + offset, data, 1);
144 static int vadc_write(struct vadc_priv *vadc, u16 offset, u8 data)
146 return regmap_write(vadc->regmap, vadc->base + offset, data);
149 static int vadc_reset(struct vadc_priv *vadc)
151 u8 data;
152 int ret;
154 ret = vadc_write(vadc, VADC_ACCESS, VADC_ACCESS_DATA);
155 if (ret)
156 return ret;
158 ret = vadc_read(vadc, VADC_PERH_RESET_CTL3, &data);
159 if (ret)
160 return ret;
162 ret = vadc_write(vadc, VADC_ACCESS, VADC_ACCESS_DATA);
163 if (ret)
164 return ret;
166 data |= VADC_FOLLOW_WARM_RB;
168 return vadc_write(vadc, VADC_PERH_RESET_CTL3, data);
171 static int vadc_set_state(struct vadc_priv *vadc, bool state)
173 return vadc_write(vadc, VADC_EN_CTL1, state ? VADC_EN_CTL1_SET : 0);
176 static void vadc_show_status(struct vadc_priv *vadc)
178 u8 mode, sta1, chan, dig, en, req;
179 int ret;
181 ret = vadc_read(vadc, VADC_MODE_CTL, &mode);
182 if (ret)
183 return;
185 ret = vadc_read(vadc, VADC_ADC_DIG_PARAM, &dig);
186 if (ret)
187 return;
189 ret = vadc_read(vadc, VADC_ADC_CH_SEL_CTL, &chan);
190 if (ret)
191 return;
193 ret = vadc_read(vadc, VADC_CONV_REQ, &req);
194 if (ret)
195 return;
197 ret = vadc_read(vadc, VADC_STATUS1, &sta1);
198 if (ret)
199 return;
201 ret = vadc_read(vadc, VADC_EN_CTL1, &en);
202 if (ret)
203 return;
205 dev_err(vadc->dev,
206 "mode:%02x en:%02x chan:%02x dig:%02x req:%02x sta1:%02x\n",
207 mode, en, chan, dig, req, sta1);
210 static int vadc_configure(struct vadc_priv *vadc,
211 struct vadc_channel_prop *prop)
213 u8 decimation, mode_ctrl;
214 int ret;
216 /* Mode selection */
217 mode_ctrl = (VADC_OP_MODE_NORMAL << VADC_OP_MODE_SHIFT) |
218 VADC_ADC_TRIM_EN | VADC_AMUX_TRIM_EN;
219 ret = vadc_write(vadc, VADC_MODE_CTL, mode_ctrl);
220 if (ret)
221 return ret;
223 /* Channel selection */
224 ret = vadc_write(vadc, VADC_ADC_CH_SEL_CTL, prop->channel);
225 if (ret)
226 return ret;
228 /* Digital parameter setup */
229 decimation = prop->decimation << VADC_ADC_DIG_DEC_RATIO_SEL_SHIFT;
230 ret = vadc_write(vadc, VADC_ADC_DIG_PARAM, decimation);
231 if (ret)
232 return ret;
234 /* HW settle time delay */
235 ret = vadc_write(vadc, VADC_HW_SETTLE_DELAY, prop->hw_settle_time);
236 if (ret)
237 return ret;
239 ret = vadc_write(vadc, VADC_FAST_AVG_CTL, prop->avg_samples);
240 if (ret)
241 return ret;
243 if (prop->avg_samples)
244 ret = vadc_write(vadc, VADC_FAST_AVG_EN, VADC_FAST_AVG_EN_SET);
245 else
246 ret = vadc_write(vadc, VADC_FAST_AVG_EN, 0);
248 return ret;
251 static int vadc_poll_wait_eoc(struct vadc_priv *vadc, unsigned int interval_us)
253 unsigned int count, retry;
254 u8 sta1;
255 int ret;
257 retry = interval_us / VADC_CONV_TIME_MIN_US;
259 for (count = 0; count < retry; count++) {
260 ret = vadc_read(vadc, VADC_STATUS1, &sta1);
261 if (ret)
262 return ret;
264 sta1 &= VADC_STATUS1_REQ_STS_EOC_MASK;
265 if (sta1 == VADC_STATUS1_EOC)
266 return 0;
268 usleep_range(VADC_CONV_TIME_MIN_US, VADC_CONV_TIME_MAX_US);
271 vadc_show_status(vadc);
273 return -ETIMEDOUT;
276 static int vadc_read_result(struct vadc_priv *vadc, u16 *data)
278 int ret;
280 ret = regmap_bulk_read(vadc->regmap, vadc->base + VADC_DATA, data, 2);
281 if (ret)
282 return ret;
284 *data = clamp_t(u16, *data, VADC_MIN_ADC_CODE, VADC_MAX_ADC_CODE);
286 return 0;
289 static struct vadc_channel_prop *vadc_get_channel(struct vadc_priv *vadc,
290 unsigned int num)
292 unsigned int i;
294 for (i = 0; i < vadc->nchannels; i++)
295 if (vadc->chan_props[i].channel == num)
296 return &vadc->chan_props[i];
298 dev_dbg(vadc->dev, "no such channel %02x\n", num);
300 return NULL;
303 static int vadc_do_conversion(struct vadc_priv *vadc,
304 struct vadc_channel_prop *prop, u16 *data)
306 unsigned int timeout;
307 int ret;
309 mutex_lock(&vadc->lock);
311 ret = vadc_configure(vadc, prop);
312 if (ret)
313 goto unlock;
315 if (!vadc->poll_eoc)
316 reinit_completion(&vadc->complete);
318 ret = vadc_set_state(vadc, true);
319 if (ret)
320 goto unlock;
322 ret = vadc_write(vadc, VADC_CONV_REQ, VADC_CONV_REQ_SET);
323 if (ret)
324 goto err_disable;
326 timeout = BIT(prop->avg_samples) * VADC_CONV_TIME_MIN_US * 2;
328 if (vadc->poll_eoc) {
329 ret = vadc_poll_wait_eoc(vadc, timeout);
330 } else {
331 ret = wait_for_completion_timeout(&vadc->complete, timeout);
332 if (!ret) {
333 ret = -ETIMEDOUT;
334 goto err_disable;
337 /* Double check conversion status */
338 ret = vadc_poll_wait_eoc(vadc, VADC_CONV_TIME_MIN_US);
339 if (ret)
340 goto err_disable;
343 ret = vadc_read_result(vadc, data);
345 err_disable:
346 vadc_set_state(vadc, false);
347 if (ret)
348 dev_err(vadc->dev, "conversion failed\n");
349 unlock:
350 mutex_unlock(&vadc->lock);
351 return ret;
354 static int vadc_measure_ref_points(struct vadc_priv *vadc)
356 struct vadc_channel_prop *prop;
357 u16 read_1, read_2;
358 int ret;
360 vadc->graph[VADC_CALIB_RATIOMETRIC].dx = VADC_RATIOMETRIC_RANGE;
361 vadc->graph[VADC_CALIB_ABSOLUTE].dx = VADC_ABSOLUTE_RANGE_UV;
363 prop = vadc_get_channel(vadc, VADC_REF_1250MV);
364 ret = vadc_do_conversion(vadc, prop, &read_1);
365 if (ret)
366 goto err;
368 /* Try with buffered 625mV channel first */
369 prop = vadc_get_channel(vadc, VADC_SPARE1);
370 if (!prop)
371 prop = vadc_get_channel(vadc, VADC_REF_625MV);
373 ret = vadc_do_conversion(vadc, prop, &read_2);
374 if (ret)
375 goto err;
377 if (read_1 == read_2) {
378 ret = -EINVAL;
379 goto err;
382 vadc->graph[VADC_CALIB_ABSOLUTE].dy = read_1 - read_2;
383 vadc->graph[VADC_CALIB_ABSOLUTE].gnd = read_2;
385 /* Ratiometric calibration */
386 prop = vadc_get_channel(vadc, VADC_VDD_VADC);
387 ret = vadc_do_conversion(vadc, prop, &read_1);
388 if (ret)
389 goto err;
391 prop = vadc_get_channel(vadc, VADC_GND_REF);
392 ret = vadc_do_conversion(vadc, prop, &read_2);
393 if (ret)
394 goto err;
396 if (read_1 == read_2) {
397 ret = -EINVAL;
398 goto err;
401 vadc->graph[VADC_CALIB_RATIOMETRIC].dy = read_1 - read_2;
402 vadc->graph[VADC_CALIB_RATIOMETRIC].gnd = read_2;
403 err:
404 if (ret)
405 dev_err(vadc->dev, "measure reference points failed\n");
407 return ret;
410 static int vadc_prescaling_from_dt(u32 numerator, u32 denominator)
412 unsigned int pre;
414 for (pre = 0; pre < ARRAY_SIZE(vadc_prescale_ratios); pre++)
415 if (vadc_prescale_ratios[pre].numerator == numerator &&
416 vadc_prescale_ratios[pre].denominator == denominator)
417 break;
419 if (pre == ARRAY_SIZE(vadc_prescale_ratios))
420 return -EINVAL;
422 return pre;
425 static int vadc_hw_settle_time_from_dt(u32 value)
427 if ((value <= 1000 && value % 100) || (value > 1000 && value % 2000))
428 return -EINVAL;
430 if (value <= 1000)
431 value /= 100;
432 else
433 value = value / 2000 + 10;
435 return value;
438 static int vadc_avg_samples_from_dt(u32 value)
440 if (!is_power_of_2(value) || value > VADC_AVG_SAMPLES_MAX)
441 return -EINVAL;
443 return __ffs64(value);
446 static int vadc_read_raw(struct iio_dev *indio_dev,
447 struct iio_chan_spec const *chan, int *val, int *val2,
448 long mask)
450 struct vadc_priv *vadc = iio_priv(indio_dev);
451 struct vadc_channel_prop *prop;
452 u16 adc_code;
453 int ret;
455 switch (mask) {
456 case IIO_CHAN_INFO_PROCESSED:
457 prop = &vadc->chan_props[chan->address];
458 ret = vadc_do_conversion(vadc, prop, &adc_code);
459 if (ret)
460 break;
462 ret = qcom_vadc_scale(prop->scale_fn_type,
463 &vadc->graph[prop->calibration],
464 &vadc_prescale_ratios[prop->prescale],
465 (prop->calibration == VADC_CALIB_ABSOLUTE),
466 adc_code, val);
467 if (ret)
468 break;
470 return IIO_VAL_INT;
471 case IIO_CHAN_INFO_RAW:
472 prop = &vadc->chan_props[chan->address];
473 ret = vadc_do_conversion(vadc, prop, &adc_code);
474 if (ret)
475 break;
477 *val = (int)adc_code;
478 return IIO_VAL_INT;
479 default:
480 ret = -EINVAL;
481 break;
484 return ret;
487 static int vadc_fwnode_xlate(struct iio_dev *indio_dev,
488 const struct fwnode_reference_args *iiospec)
490 struct vadc_priv *vadc = iio_priv(indio_dev);
491 unsigned int i;
493 for (i = 0; i < vadc->nchannels; i++)
494 if (vadc->iio_chans[i].channel == iiospec->args[0])
495 return i;
497 return -EINVAL;
500 static int vadc_read_label(struct iio_dev *indio_dev,
501 struct iio_chan_spec const *chan, char *label)
503 struct vadc_priv *vadc = iio_priv(indio_dev);
504 const char *name = vadc->chan_props[chan->address].channel_name;
506 return sysfs_emit(label, "%s\n", name);
509 static const struct iio_info vadc_info = {
510 .read_raw = vadc_read_raw,
511 .read_label = vadc_read_label,
512 .fwnode_xlate = vadc_fwnode_xlate,
515 struct vadc_channels {
516 const char *datasheet_name;
517 unsigned int prescale_index;
518 enum iio_chan_type type;
519 long info_mask;
520 enum vadc_scale_fn_type scale_fn_type;
523 #define VADC_CHAN(_dname, _type, _mask, _pre, _scale) \
524 [VADC_##_dname] = { \
525 .datasheet_name = __stringify(_dname), \
526 .prescale_index = _pre, \
527 .type = _type, \
528 .info_mask = _mask, \
529 .scale_fn_type = _scale \
530 }, \
532 #define VADC_NO_CHAN(_dname, _type, _mask, _pre) \
533 [VADC_##_dname] = { \
534 .datasheet_name = __stringify(_dname), \
535 .prescale_index = _pre, \
536 .type = _type, \
537 .info_mask = _mask \
540 #define VADC_CHAN_TEMP(_dname, _pre, _scale) \
541 VADC_CHAN(_dname, IIO_TEMP, \
542 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_PROCESSED), \
543 _pre, _scale) \
545 #define VADC_CHAN_VOLT(_dname, _pre, _scale) \
546 VADC_CHAN(_dname, IIO_VOLTAGE, \
547 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_PROCESSED),\
548 _pre, _scale) \
550 #define VADC_CHAN_NO_SCALE(_dname, _pre) \
551 VADC_NO_CHAN(_dname, IIO_VOLTAGE, \
552 BIT(IIO_CHAN_INFO_RAW), \
553 _pre) \
556 * The array represents all possible ADC channels found in the supported PMICs.
557 * Every index in the array is equal to the channel number per datasheet. The
558 * gaps in the array should be treated as reserved channels.
560 static const struct vadc_channels vadc_chans[] = {
561 VADC_CHAN_VOLT(USBIN, 4, SCALE_DEFAULT)
562 VADC_CHAN_VOLT(DCIN, 4, SCALE_DEFAULT)
563 VADC_CHAN_NO_SCALE(VCHG_SNS, 3)
564 VADC_CHAN_NO_SCALE(SPARE1_03, 1)
565 VADC_CHAN_NO_SCALE(USB_ID_MV, 1)
566 VADC_CHAN_VOLT(VCOIN, 1, SCALE_DEFAULT)
567 VADC_CHAN_NO_SCALE(VBAT_SNS, 1)
568 VADC_CHAN_VOLT(VSYS, 1, SCALE_DEFAULT)
569 VADC_CHAN_TEMP(DIE_TEMP, 0, SCALE_PMIC_THERM)
570 VADC_CHAN_VOLT(REF_625MV, 0, SCALE_DEFAULT)
571 VADC_CHAN_VOLT(REF_1250MV, 0, SCALE_DEFAULT)
572 VADC_CHAN_NO_SCALE(CHG_TEMP, 0)
573 VADC_CHAN_NO_SCALE(SPARE1, 0)
574 VADC_CHAN_TEMP(SPARE2, 0, SCALE_PMI_CHG_TEMP)
575 VADC_CHAN_VOLT(GND_REF, 0, SCALE_DEFAULT)
576 VADC_CHAN_VOLT(VDD_VADC, 0, SCALE_DEFAULT)
578 VADC_CHAN_NO_SCALE(P_MUX1_1_1, 0)
579 VADC_CHAN_NO_SCALE(P_MUX2_1_1, 0)
580 VADC_CHAN_NO_SCALE(P_MUX3_1_1, 0)
581 VADC_CHAN_NO_SCALE(P_MUX4_1_1, 0)
582 VADC_CHAN_NO_SCALE(P_MUX5_1_1, 0)
583 VADC_CHAN_NO_SCALE(P_MUX6_1_1, 0)
584 VADC_CHAN_NO_SCALE(P_MUX7_1_1, 0)
585 VADC_CHAN_NO_SCALE(P_MUX8_1_1, 0)
586 VADC_CHAN_NO_SCALE(P_MUX9_1_1, 0)
587 VADC_CHAN_NO_SCALE(P_MUX10_1_1, 0)
588 VADC_CHAN_NO_SCALE(P_MUX11_1_1, 0)
589 VADC_CHAN_NO_SCALE(P_MUX12_1_1, 0)
590 VADC_CHAN_NO_SCALE(P_MUX13_1_1, 0)
591 VADC_CHAN_NO_SCALE(P_MUX14_1_1, 0)
592 VADC_CHAN_NO_SCALE(P_MUX15_1_1, 0)
593 VADC_CHAN_NO_SCALE(P_MUX16_1_1, 0)
595 VADC_CHAN_NO_SCALE(P_MUX1_1_3, 1)
596 VADC_CHAN_NO_SCALE(P_MUX2_1_3, 1)
597 VADC_CHAN_NO_SCALE(P_MUX3_1_3, 1)
598 VADC_CHAN_NO_SCALE(P_MUX4_1_3, 1)
599 VADC_CHAN_NO_SCALE(P_MUX5_1_3, 1)
600 VADC_CHAN_NO_SCALE(P_MUX6_1_3, 1)
601 VADC_CHAN_NO_SCALE(P_MUX7_1_3, 1)
602 VADC_CHAN_NO_SCALE(P_MUX8_1_3, 1)
603 VADC_CHAN_NO_SCALE(P_MUX9_1_3, 1)
604 VADC_CHAN_NO_SCALE(P_MUX10_1_3, 1)
605 VADC_CHAN_NO_SCALE(P_MUX11_1_3, 1)
606 VADC_CHAN_NO_SCALE(P_MUX12_1_3, 1)
607 VADC_CHAN_NO_SCALE(P_MUX13_1_3, 1)
608 VADC_CHAN_NO_SCALE(P_MUX14_1_3, 1)
609 VADC_CHAN_NO_SCALE(P_MUX15_1_3, 1)
610 VADC_CHAN_NO_SCALE(P_MUX16_1_3, 1)
612 VADC_CHAN_NO_SCALE(LR_MUX1_BAT_THERM, 0)
613 VADC_CHAN_VOLT(LR_MUX2_BAT_ID, 0, SCALE_DEFAULT)
614 VADC_CHAN_NO_SCALE(LR_MUX3_XO_THERM, 0)
615 VADC_CHAN_NO_SCALE(LR_MUX4_AMUX_THM1, 0)
616 VADC_CHAN_NO_SCALE(LR_MUX5_AMUX_THM2, 0)
617 VADC_CHAN_NO_SCALE(LR_MUX6_AMUX_THM3, 0)
618 VADC_CHAN_NO_SCALE(LR_MUX7_HW_ID, 0)
619 VADC_CHAN_NO_SCALE(LR_MUX8_AMUX_THM4, 0)
620 VADC_CHAN_NO_SCALE(LR_MUX9_AMUX_THM5, 0)
621 VADC_CHAN_NO_SCALE(LR_MUX10_USB_ID, 0)
622 VADC_CHAN_NO_SCALE(AMUX_PU1, 0)
623 VADC_CHAN_NO_SCALE(AMUX_PU2, 0)
624 VADC_CHAN_NO_SCALE(LR_MUX3_BUF_XO_THERM, 0)
626 VADC_CHAN_NO_SCALE(LR_MUX1_PU1_BAT_THERM, 0)
627 VADC_CHAN_NO_SCALE(LR_MUX2_PU1_BAT_ID, 0)
628 VADC_CHAN_NO_SCALE(LR_MUX3_PU1_XO_THERM, 0)
629 VADC_CHAN_TEMP(LR_MUX4_PU1_AMUX_THM1, 0, SCALE_THERM_100K_PULLUP)
630 VADC_CHAN_TEMP(LR_MUX5_PU1_AMUX_THM2, 0, SCALE_THERM_100K_PULLUP)
631 VADC_CHAN_TEMP(LR_MUX6_PU1_AMUX_THM3, 0, SCALE_THERM_100K_PULLUP)
632 VADC_CHAN_NO_SCALE(LR_MUX7_PU1_AMUX_HW_ID, 0)
633 VADC_CHAN_TEMP(LR_MUX8_PU1_AMUX_THM4, 0, SCALE_THERM_100K_PULLUP)
634 VADC_CHAN_TEMP(LR_MUX9_PU1_AMUX_THM5, 0, SCALE_THERM_100K_PULLUP)
635 VADC_CHAN_NO_SCALE(LR_MUX10_PU1_AMUX_USB_ID, 0)
636 VADC_CHAN_TEMP(LR_MUX3_BUF_PU1_XO_THERM, 0, SCALE_XOTHERM)
638 VADC_CHAN_NO_SCALE(LR_MUX1_PU2_BAT_THERM, 0)
639 VADC_CHAN_NO_SCALE(LR_MUX2_PU2_BAT_ID, 0)
640 VADC_CHAN_NO_SCALE(LR_MUX3_PU2_XO_THERM, 0)
641 VADC_CHAN_NO_SCALE(LR_MUX4_PU2_AMUX_THM1, 0)
642 VADC_CHAN_NO_SCALE(LR_MUX5_PU2_AMUX_THM2, 0)
643 VADC_CHAN_NO_SCALE(LR_MUX6_PU2_AMUX_THM3, 0)
644 VADC_CHAN_NO_SCALE(LR_MUX7_PU2_AMUX_HW_ID, 0)
645 VADC_CHAN_NO_SCALE(LR_MUX8_PU2_AMUX_THM4, 0)
646 VADC_CHAN_NO_SCALE(LR_MUX9_PU2_AMUX_THM5, 0)
647 VADC_CHAN_NO_SCALE(LR_MUX10_PU2_AMUX_USB_ID, 0)
648 VADC_CHAN_NO_SCALE(LR_MUX3_BUF_PU2_XO_THERM, 0)
650 VADC_CHAN_NO_SCALE(LR_MUX1_PU1_PU2_BAT_THERM, 0)
651 VADC_CHAN_NO_SCALE(LR_MUX2_PU1_PU2_BAT_ID, 0)
652 VADC_CHAN_NO_SCALE(LR_MUX3_PU1_PU2_XO_THERM, 0)
653 VADC_CHAN_NO_SCALE(LR_MUX4_PU1_PU2_AMUX_THM1, 0)
654 VADC_CHAN_NO_SCALE(LR_MUX5_PU1_PU2_AMUX_THM2, 0)
655 VADC_CHAN_NO_SCALE(LR_MUX6_PU1_PU2_AMUX_THM3, 0)
656 VADC_CHAN_NO_SCALE(LR_MUX7_PU1_PU2_AMUX_HW_ID, 0)
657 VADC_CHAN_NO_SCALE(LR_MUX8_PU1_PU2_AMUX_THM4, 0)
658 VADC_CHAN_NO_SCALE(LR_MUX9_PU1_PU2_AMUX_THM5, 0)
659 VADC_CHAN_NO_SCALE(LR_MUX10_PU1_PU2_AMUX_USB_ID, 0)
660 VADC_CHAN_NO_SCALE(LR_MUX3_BUF_PU1_PU2_XO_THERM, 0)
663 static int vadc_get_fw_channel_data(struct device *dev,
664 struct vadc_channel_prop *prop,
665 struct fwnode_handle *fwnode)
667 const char *name = fwnode_get_name(fwnode), *label;
668 u32 chan, value, varr[2];
669 int ret;
671 ret = fwnode_property_read_u32(fwnode, "reg", &chan);
672 if (ret) {
673 dev_err(dev, "invalid channel number %s\n", name);
674 return ret;
677 if (chan > VADC_CHAN_MAX || chan < VADC_CHAN_MIN) {
678 dev_err(dev, "%s invalid channel number %d\n", name, chan);
679 return -EINVAL;
682 ret = fwnode_property_read_string(fwnode, "label", &label);
683 if (ret)
684 label = vadc_chans[chan].datasheet_name;
685 prop->channel_name = label;
687 /* the channel has DT description */
688 prop->channel = chan;
690 ret = fwnode_property_read_u32(fwnode, "qcom,decimation", &value);
691 if (!ret) {
692 ret = qcom_vadc_decimation_from_dt(value);
693 if (ret < 0) {
694 dev_err(dev, "%02x invalid decimation %d\n",
695 chan, value);
696 return ret;
698 prop->decimation = ret;
699 } else {
700 prop->decimation = VADC_DEF_DECIMATION;
703 ret = fwnode_property_read_u32_array(fwnode, "qcom,pre-scaling", varr, 2);
704 if (!ret) {
705 ret = vadc_prescaling_from_dt(varr[0], varr[1]);
706 if (ret < 0) {
707 dev_err(dev, "%02x invalid pre-scaling <%d %d>\n",
708 chan, varr[0], varr[1]);
709 return ret;
711 prop->prescale = ret;
712 } else {
713 prop->prescale = vadc_chans[prop->channel].prescale_index;
716 ret = fwnode_property_read_u32(fwnode, "qcom,hw-settle-time", &value);
717 if (!ret) {
718 ret = vadc_hw_settle_time_from_dt(value);
719 if (ret < 0) {
720 dev_err(dev, "%02x invalid hw-settle-time %d us\n",
721 chan, value);
722 return ret;
724 prop->hw_settle_time = ret;
725 } else {
726 prop->hw_settle_time = VADC_DEF_HW_SETTLE_TIME;
729 ret = fwnode_property_read_u32(fwnode, "qcom,avg-samples", &value);
730 if (!ret) {
731 ret = vadc_avg_samples_from_dt(value);
732 if (ret < 0) {
733 dev_err(dev, "%02x invalid avg-samples %d\n",
734 chan, value);
735 return ret;
737 prop->avg_samples = ret;
738 } else {
739 prop->avg_samples = VADC_DEF_AVG_SAMPLES;
742 if (fwnode_property_read_bool(fwnode, "qcom,ratiometric"))
743 prop->calibration = VADC_CALIB_RATIOMETRIC;
744 else
745 prop->calibration = VADC_CALIB_ABSOLUTE;
747 dev_dbg(dev, "%02x name %s\n", chan, name);
749 return 0;
752 static int vadc_get_fw_data(struct vadc_priv *vadc)
754 const struct vadc_channels *vadc_chan;
755 struct iio_chan_spec *iio_chan;
756 struct vadc_channel_prop prop;
757 unsigned int index = 0;
758 int ret;
760 vadc->nchannels = device_get_child_node_count(vadc->dev);
761 if (!vadc->nchannels)
762 return -EINVAL;
764 vadc->iio_chans = devm_kcalloc(vadc->dev, vadc->nchannels,
765 sizeof(*vadc->iio_chans), GFP_KERNEL);
766 if (!vadc->iio_chans)
767 return -ENOMEM;
769 vadc->chan_props = devm_kcalloc(vadc->dev, vadc->nchannels,
770 sizeof(*vadc->chan_props), GFP_KERNEL);
771 if (!vadc->chan_props)
772 return -ENOMEM;
774 iio_chan = vadc->iio_chans;
776 device_for_each_child_node_scoped(vadc->dev, child) {
777 ret = vadc_get_fw_channel_data(vadc->dev, &prop, child);
778 if (ret)
779 return ret;
781 prop.scale_fn_type = vadc_chans[prop.channel].scale_fn_type;
782 vadc->chan_props[index] = prop;
784 vadc_chan = &vadc_chans[prop.channel];
786 iio_chan->channel = prop.channel;
787 iio_chan->datasheet_name = vadc_chan->datasheet_name;
788 iio_chan->info_mask_separate = vadc_chan->info_mask;
789 iio_chan->type = vadc_chan->type;
790 iio_chan->indexed = 1;
791 iio_chan->address = index++;
793 iio_chan++;
796 /* These channels are mandatory, they are used as reference points */
797 if (!vadc_get_channel(vadc, VADC_REF_1250MV)) {
798 dev_err(vadc->dev, "Please define 1.25V channel\n");
799 return -ENODEV;
802 if (!vadc_get_channel(vadc, VADC_REF_625MV)) {
803 dev_err(vadc->dev, "Please define 0.625V channel\n");
804 return -ENODEV;
807 if (!vadc_get_channel(vadc, VADC_VDD_VADC)) {
808 dev_err(vadc->dev, "Please define VDD channel\n");
809 return -ENODEV;
812 if (!vadc_get_channel(vadc, VADC_GND_REF)) {
813 dev_err(vadc->dev, "Please define GND channel\n");
814 return -ENODEV;
817 return 0;
820 static irqreturn_t vadc_isr(int irq, void *dev_id)
822 struct vadc_priv *vadc = dev_id;
824 complete(&vadc->complete);
826 return IRQ_HANDLED;
829 static int vadc_check_revision(struct vadc_priv *vadc)
831 u8 val;
832 int ret;
834 ret = vadc_read(vadc, VADC_PERPH_TYPE, &val);
835 if (ret)
836 return ret;
838 if (val < VADC_PERPH_TYPE_ADC) {
839 dev_err(vadc->dev, "%d is not ADC\n", val);
840 return -ENODEV;
843 ret = vadc_read(vadc, VADC_PERPH_SUBTYPE, &val);
844 if (ret)
845 return ret;
847 if (val < VADC_PERPH_SUBTYPE_VADC) {
848 dev_err(vadc->dev, "%d is not VADC\n", val);
849 return -ENODEV;
852 ret = vadc_read(vadc, VADC_REVISION2, &val);
853 if (ret)
854 return ret;
856 if (val < VADC_REVISION2_SUPPORTED_VADC) {
857 dev_err(vadc->dev, "revision %d not supported\n", val);
858 return -ENODEV;
861 return 0;
864 static int vadc_probe(struct platform_device *pdev)
866 struct device *dev = &pdev->dev;
867 struct iio_dev *indio_dev;
868 struct vadc_priv *vadc;
869 struct regmap *regmap;
870 int ret, irq_eoc;
871 u32 reg;
873 regmap = dev_get_regmap(dev->parent, NULL);
874 if (!regmap)
875 return -ENODEV;
877 ret = device_property_read_u32(dev, "reg", &reg);
878 if (ret < 0)
879 return ret;
881 indio_dev = devm_iio_device_alloc(dev, sizeof(*vadc));
882 if (!indio_dev)
883 return -ENOMEM;
885 vadc = iio_priv(indio_dev);
886 vadc->regmap = regmap;
887 vadc->dev = dev;
888 vadc->base = reg;
889 vadc->are_ref_measured = false;
890 init_completion(&vadc->complete);
891 mutex_init(&vadc->lock);
893 ret = vadc_check_revision(vadc);
894 if (ret)
895 return ret;
897 ret = vadc_get_fw_data(vadc);
898 if (ret)
899 return ret;
901 irq_eoc = platform_get_irq(pdev, 0);
902 if (irq_eoc < 0) {
903 if (irq_eoc == -EPROBE_DEFER || irq_eoc == -EINVAL)
904 return irq_eoc;
905 vadc->poll_eoc = true;
906 } else {
907 ret = devm_request_irq(dev, irq_eoc, vadc_isr, 0,
908 "spmi-vadc", vadc);
909 if (ret)
910 return ret;
913 ret = vadc_reset(vadc);
914 if (ret) {
915 dev_err(dev, "reset failed\n");
916 return ret;
919 ret = vadc_measure_ref_points(vadc);
920 if (ret)
921 return ret;
923 indio_dev->name = pdev->name;
924 indio_dev->modes = INDIO_DIRECT_MODE;
925 indio_dev->info = &vadc_info;
926 indio_dev->channels = vadc->iio_chans;
927 indio_dev->num_channels = vadc->nchannels;
929 return devm_iio_device_register(dev, indio_dev);
932 static const struct of_device_id vadc_match_table[] = {
933 { .compatible = "qcom,spmi-vadc" },
936 MODULE_DEVICE_TABLE(of, vadc_match_table);
938 static struct platform_driver vadc_driver = {
939 .driver = {
940 .name = "qcom-spmi-vadc",
941 .of_match_table = vadc_match_table,
943 .probe = vadc_probe,
945 module_platform_driver(vadc_driver);
947 MODULE_ALIAS("platform:qcom-spmi-vadc");
948 MODULE_DESCRIPTION("Qualcomm SPMI PMIC voltage ADC driver");
949 MODULE_LICENSE("GPL v2");
950 MODULE_AUTHOR("Stanimir Varbanov <svarbanov@mm-sol.com>");
951 MODULE_AUTHOR("Ivan T. Ivanov <iivanov@mm-sol.com>");