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