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>
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 1800
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 PMI_CHG_SCALE_1 -138890
104 #define PMI_CHG_SCALE_2 391750000000LL
106 #define VADC_CHAN_MIN VADC_USBIN
107 #define VADC_CHAN_MAX VADC_LR_MUX3_BUF_PU1_PU2_XO_THERM
110 * struct vadc_map_pt - Map the graph representation for ADC channel
111 * @x: Represent the ADC digitized code.
112 * @y: Represent the physical data which can be temperature, voltage,
121 * VADC_CALIB_ABSOLUTE: uses the 625mV and 1.25V as reference channels.
122 * VADC_CALIB_RATIOMETRIC: uses the reference voltage (1.8V) and GND for
125 enum vadc_calibration {
126 VADC_CALIB_ABSOLUTE = 0,
127 VADC_CALIB_RATIOMETRIC
131 * struct vadc_linear_graph - Represent ADC characteristics.
132 * @dy: numerator slope to calculate the gain.
133 * @dx: denominator slope to calculate the gain.
134 * @gnd: A/D word of the ground reference used for the channel.
136 * Each ADC device has different offset and gain parameters which are
137 * computed to calibrate the device.
139 struct vadc_linear_graph {
146 * struct vadc_prescale_ratio - Represent scaling ratio for ADC input.
147 * @num: the inverse numerator of the gain applied to the input channel.
148 * @den: the inverse denominator of the gain applied to the input channel.
150 struct vadc_prescale_ratio {
156 * struct vadc_channel_prop - VADC channel property.
157 * @channel: channel number, refer to the channel list.
158 * @calibration: calibration type.
159 * @decimation: sampling rate supported for the channel.
160 * @prescale: channel scaling performed on the input signal.
161 * @hw_settle_time: the time between AMUX being configured and the
162 * start of conversion.
163 * @avg_samples: ability to provide single result from the ADC
164 * that is an average of multiple measurements.
165 * @scale_fn: Represents the scaling function to convert voltage
166 * physical units desired by the client for the channel.
167 * Referenced from enum vadc_scale_fn_type.
169 struct vadc_channel_prop {
170 unsigned int channel;
171 enum vadc_calibration calibration;
172 unsigned int decimation;
173 unsigned int prescale;
174 unsigned int hw_settle_time;
175 unsigned int avg_samples;
176 unsigned int scale_fn;
180 * struct vadc_priv - VADC private structure.
181 * @regmap: pointer to struct regmap.
182 * @dev: pointer to struct device.
183 * @base: base address for the ADC peripheral.
184 * @nchannels: number of VADC channels.
185 * @chan_props: array of VADC channel properties.
186 * @iio_chans: array of IIO channels specification.
187 * @are_ref_measured: are reference points measured.
188 * @poll_eoc: use polling instead of interrupt.
189 * @complete: VADC result notification after interrupt is received.
190 * @graph: store parameters for calibration.
191 * @lock: ADC lock for access to the peripheral.
194 struct regmap *regmap;
197 unsigned int nchannels;
198 struct vadc_channel_prop *chan_props;
199 struct iio_chan_spec *iio_chans;
200 bool are_ref_measured;
202 struct completion complete;
203 struct vadc_linear_graph graph[2];
208 * struct vadc_scale_fn - Scaling function prototype
209 * @scale: Function pointer to one of the scaling functions
210 * which takes the adc properties, channel properties,
211 * and returns the physical result.
213 struct vadc_scale_fn {
214 int (*scale)(struct vadc_priv *, const struct vadc_channel_prop *,
219 * enum vadc_scale_fn_type - Scaling function to convert ADC code to
220 * physical scaled units for the channel.
221 * SCALE_DEFAULT: Default scaling to convert raw adc code to voltage (uV).
222 * SCALE_THERM_100K_PULLUP: Returns temperature in millidegC.
223 * Uses a mapping table with 100K pullup.
224 * SCALE_PMIC_THERM: Returns result in milli degree's Centigrade.
225 * SCALE_XOTHERM: Returns XO thermistor voltage in millidegC.
226 * SCALE_PMI_CHG_TEMP: Conversion for PMI CHG temp
228 enum vadc_scale_fn_type {
230 SCALE_THERM_100K_PULLUP,
236 static const struct vadc_prescale_ratio vadc_prescale_ratios[] = {
237 {.num = 1, .den = 1},
238 {.num = 1, .den = 3},
239 {.num = 1, .den = 4},
240 {.num = 1, .den = 6},
241 {.num = 1, .den = 20},
242 {.num = 1, .den = 8},
243 {.num = 10, .den = 81},
244 {.num = 1, .den = 10}
247 /* Voltage to temperature */
248 static const struct vadc_map_pt adcmap_100k_104ef_104fb[] = {
285 static int vadc_read(struct vadc_priv *vadc, u16 offset, u8 *data)
287 return regmap_bulk_read(vadc->regmap, vadc->base + offset, data, 1);
290 static int vadc_write(struct vadc_priv *vadc, u16 offset, u8 data)
292 return regmap_write(vadc->regmap, vadc->base + offset, data);
295 static int vadc_reset(struct vadc_priv *vadc)
300 ret = vadc_write(vadc, VADC_ACCESS, VADC_ACCESS_DATA);
304 ret = vadc_read(vadc, VADC_PERH_RESET_CTL3, &data);
308 ret = vadc_write(vadc, VADC_ACCESS, VADC_ACCESS_DATA);
312 data |= VADC_FOLLOW_WARM_RB;
314 return vadc_write(vadc, VADC_PERH_RESET_CTL3, data);
317 static int vadc_set_state(struct vadc_priv *vadc, bool state)
319 return vadc_write(vadc, VADC_EN_CTL1, state ? VADC_EN_CTL1_SET : 0);
322 static void vadc_show_status(struct vadc_priv *vadc)
324 u8 mode, sta1, chan, dig, en, req;
327 ret = vadc_read(vadc, VADC_MODE_CTL, &mode);
331 ret = vadc_read(vadc, VADC_ADC_DIG_PARAM, &dig);
335 ret = vadc_read(vadc, VADC_ADC_CH_SEL_CTL, &chan);
339 ret = vadc_read(vadc, VADC_CONV_REQ, &req);
343 ret = vadc_read(vadc, VADC_STATUS1, &sta1);
347 ret = vadc_read(vadc, VADC_EN_CTL1, &en);
352 "mode:%02x en:%02x chan:%02x dig:%02x req:%02x sta1:%02x\n",
353 mode, en, chan, dig, req, sta1);
356 static int vadc_configure(struct vadc_priv *vadc,
357 struct vadc_channel_prop *prop)
359 u8 decimation, mode_ctrl;
363 mode_ctrl = (VADC_OP_MODE_NORMAL << VADC_OP_MODE_SHIFT) |
364 VADC_ADC_TRIM_EN | VADC_AMUX_TRIM_EN;
365 ret = vadc_write(vadc, VADC_MODE_CTL, mode_ctrl);
369 /* Channel selection */
370 ret = vadc_write(vadc, VADC_ADC_CH_SEL_CTL, prop->channel);
374 /* Digital parameter setup */
375 decimation = prop->decimation << VADC_ADC_DIG_DEC_RATIO_SEL_SHIFT;
376 ret = vadc_write(vadc, VADC_ADC_DIG_PARAM, decimation);
380 /* HW settle time delay */
381 ret = vadc_write(vadc, VADC_HW_SETTLE_DELAY, prop->hw_settle_time);
385 ret = vadc_write(vadc, VADC_FAST_AVG_CTL, prop->avg_samples);
389 if (prop->avg_samples)
390 ret = vadc_write(vadc, VADC_FAST_AVG_EN, VADC_FAST_AVG_EN_SET);
392 ret = vadc_write(vadc, VADC_FAST_AVG_EN, 0);
397 static int vadc_poll_wait_eoc(struct vadc_priv *vadc, unsigned int interval_us)
399 unsigned int count, retry;
403 retry = interval_us / VADC_CONV_TIME_MIN_US;
405 for (count = 0; count < retry; count++) {
406 ret = vadc_read(vadc, VADC_STATUS1, &sta1);
410 sta1 &= VADC_STATUS1_REQ_STS_EOC_MASK;
411 if (sta1 == VADC_STATUS1_EOC)
414 usleep_range(VADC_CONV_TIME_MIN_US, VADC_CONV_TIME_MAX_US);
417 vadc_show_status(vadc);
422 static int vadc_read_result(struct vadc_priv *vadc, u16 *data)
426 ret = regmap_bulk_read(vadc->regmap, vadc->base + VADC_DATA, data, 2);
430 *data = clamp_t(u16, *data, VADC_MIN_ADC_CODE, VADC_MAX_ADC_CODE);
435 static struct vadc_channel_prop *vadc_get_channel(struct vadc_priv *vadc,
440 for (i = 0; i < vadc->nchannels; i++)
441 if (vadc->chan_props[i].channel == num)
442 return &vadc->chan_props[i];
444 dev_dbg(vadc->dev, "no such channel %02x\n", num);
449 static int vadc_do_conversion(struct vadc_priv *vadc,
450 struct vadc_channel_prop *prop, u16 *data)
452 unsigned int timeout;
455 mutex_lock(&vadc->lock);
457 ret = vadc_configure(vadc, prop);
462 reinit_completion(&vadc->complete);
464 ret = vadc_set_state(vadc, true);
468 ret = vadc_write(vadc, VADC_CONV_REQ, VADC_CONV_REQ_SET);
472 timeout = BIT(prop->avg_samples) * VADC_CONV_TIME_MIN_US * 2;
474 if (vadc->poll_eoc) {
475 ret = vadc_poll_wait_eoc(vadc, timeout);
477 ret = wait_for_completion_timeout(&vadc->complete, timeout);
483 /* Double check conversion status */
484 ret = vadc_poll_wait_eoc(vadc, VADC_CONV_TIME_MIN_US);
489 ret = vadc_read_result(vadc, data);
492 vadc_set_state(vadc, false);
494 dev_err(vadc->dev, "conversion failed\n");
496 mutex_unlock(&vadc->lock);
500 static int vadc_measure_ref_points(struct vadc_priv *vadc)
502 struct vadc_channel_prop *prop;
506 vadc->graph[VADC_CALIB_RATIOMETRIC].dx = VADC_RATIOMETRIC_RANGE;
507 vadc->graph[VADC_CALIB_ABSOLUTE].dx = VADC_ABSOLUTE_RANGE_UV;
509 prop = vadc_get_channel(vadc, VADC_REF_1250MV);
510 ret = vadc_do_conversion(vadc, prop, &read_1);
514 /* Try with buffered 625mV channel first */
515 prop = vadc_get_channel(vadc, VADC_SPARE1);
517 prop = vadc_get_channel(vadc, VADC_REF_625MV);
519 ret = vadc_do_conversion(vadc, prop, &read_2);
523 if (read_1 == read_2) {
528 vadc->graph[VADC_CALIB_ABSOLUTE].dy = read_1 - read_2;
529 vadc->graph[VADC_CALIB_ABSOLUTE].gnd = read_2;
531 /* Ratiometric calibration */
532 prop = vadc_get_channel(vadc, VADC_VDD_VADC);
533 ret = vadc_do_conversion(vadc, prop, &read_1);
537 prop = vadc_get_channel(vadc, VADC_GND_REF);
538 ret = vadc_do_conversion(vadc, prop, &read_2);
542 if (read_1 == read_2) {
547 vadc->graph[VADC_CALIB_RATIOMETRIC].dy = read_1 - read_2;
548 vadc->graph[VADC_CALIB_RATIOMETRIC].gnd = read_2;
551 dev_err(vadc->dev, "measure reference points failed\n");
556 static int vadc_map_voltage_temp(const struct vadc_map_pt *pts,
557 u32 tablesize, s32 input, s64 *output)
565 /* Check if table is descending or ascending */
567 if (pts[0].x < pts[1].x)
571 while (i < tablesize) {
572 if ((descending) && (pts[i].x < input)) {
573 /* table entry is less than measured*/
574 /* value and table is descending, stop */
576 } else if ((!descending) &&
577 (pts[i].x > input)) {
578 /* table entry is greater than measured*/
579 /*value and table is ascending, stop */
587 } else if (i == tablesize) {
588 *output = pts[tablesize - 1].y;
590 /* result is between search_index and search_index-1 */
591 /* interpolate linearly */
592 *output = (((s32)((pts[i].y - pts[i - 1].y) *
593 (input - pts[i - 1].x)) /
594 (pts[i].x - pts[i - 1].x)) +
601 static void vadc_scale_calib(struct vadc_priv *vadc, u16 adc_code,
602 const struct vadc_channel_prop *prop,
605 *scale_voltage = (adc_code -
606 vadc->graph[prop->calibration].gnd);
607 *scale_voltage *= vadc->graph[prop->calibration].dx;
608 *scale_voltage = div64_s64(*scale_voltage,
609 vadc->graph[prop->calibration].dy);
610 if (prop->calibration == VADC_CALIB_ABSOLUTE)
612 vadc->graph[prop->calibration].dx;
614 if (*scale_voltage < 0)
618 static int vadc_scale_volt(struct vadc_priv *vadc,
619 const struct vadc_channel_prop *prop, u16 adc_code,
622 const struct vadc_prescale_ratio *prescale;
623 s64 voltage = 0, result = 0;
625 vadc_scale_calib(vadc, adc_code, prop, &voltage);
627 prescale = &vadc_prescale_ratios[prop->prescale];
628 voltage = voltage * prescale->den;
629 result = div64_s64(voltage, prescale->num);
635 static int vadc_scale_therm(struct vadc_priv *vadc,
636 const struct vadc_channel_prop *prop, u16 adc_code,
639 s64 voltage = 0, result = 0;
641 vadc_scale_calib(vadc, adc_code, prop, &voltage);
643 if (prop->calibration == VADC_CALIB_ABSOLUTE)
644 voltage = div64_s64(voltage, 1000);
646 vadc_map_voltage_temp(adcmap_100k_104ef_104fb,
647 ARRAY_SIZE(adcmap_100k_104ef_104fb),
650 *result_mdec = result;
655 static int vadc_scale_die_temp(struct vadc_priv *vadc,
656 const struct vadc_channel_prop *prop,
657 u16 adc_code, int *result_mdec)
659 const struct vadc_prescale_ratio *prescale;
661 u64 temp; /* Temporary variable for do_div */
663 vadc_scale_calib(vadc, adc_code, prop, &voltage);
666 prescale = &vadc_prescale_ratios[prop->prescale];
667 temp = voltage * prescale->den;
668 do_div(temp, prescale->num * 2);
674 voltage -= KELVINMIL_CELSIUSMIL;
675 *result_mdec = voltage;
680 static int vadc_scale_chg_temp(struct vadc_priv *vadc,
681 const struct vadc_channel_prop *prop,
682 u16 adc_code, int *result_mdec)
684 const struct vadc_prescale_ratio *prescale;
685 s64 voltage = 0, result = 0;
687 vadc_scale_calib(vadc, adc_code, prop, &voltage);
689 prescale = &vadc_prescale_ratios[prop->prescale];
690 voltage = voltage * prescale->den;
691 voltage = div64_s64(voltage, prescale->num);
692 voltage = ((PMI_CHG_SCALE_1) * (voltage * 2));
693 voltage = (voltage + PMI_CHG_SCALE_2);
694 result = div64_s64(voltage, 1000000);
695 *result_mdec = result;
700 static int vadc_decimation_from_dt(u32 value)
702 if (!is_power_of_2(value) || value < VADC_DECIMATION_MIN ||
703 value > VADC_DECIMATION_MAX)
706 return __ffs64(value / VADC_DECIMATION_MIN);
709 static int vadc_prescaling_from_dt(u32 num, u32 den)
713 for (pre = 0; pre < ARRAY_SIZE(vadc_prescale_ratios); pre++)
714 if (vadc_prescale_ratios[pre].num == num &&
715 vadc_prescale_ratios[pre].den == den)
718 if (pre == ARRAY_SIZE(vadc_prescale_ratios))
724 static int vadc_hw_settle_time_from_dt(u32 value)
726 if ((value <= 1000 && value % 100) || (value > 1000 && value % 2000))
732 value = value / 2000 + 10;
737 static int vadc_avg_samples_from_dt(u32 value)
739 if (!is_power_of_2(value) || value > VADC_AVG_SAMPLES_MAX)
742 return __ffs64(value);
745 static struct vadc_scale_fn scale_fn[] = {
746 [SCALE_DEFAULT] = {vadc_scale_volt},
747 [SCALE_THERM_100K_PULLUP] = {vadc_scale_therm},
748 [SCALE_PMIC_THERM] = {vadc_scale_die_temp},
749 [SCALE_XOTHERM] = {vadc_scale_therm},
750 [SCALE_PMI_CHG_TEMP] = {vadc_scale_chg_temp},
753 static int vadc_read_raw(struct iio_dev *indio_dev,
754 struct iio_chan_spec const *chan, int *val, int *val2,
757 struct vadc_priv *vadc = iio_priv(indio_dev);
758 struct vadc_channel_prop *prop;
763 case IIO_CHAN_INFO_PROCESSED:
764 prop = &vadc->chan_props[chan->address];
765 ret = vadc_do_conversion(vadc, prop, &adc_code);
769 scale_fn[prop->scale_fn].scale(vadc, prop, adc_code, val);
772 case IIO_CHAN_INFO_RAW:
773 prop = &vadc->chan_props[chan->address];
774 ret = vadc_do_conversion(vadc, prop, &adc_code);
778 *val = (int)adc_code;
788 static int vadc_of_xlate(struct iio_dev *indio_dev,
789 const struct of_phandle_args *iiospec)
791 struct vadc_priv *vadc = iio_priv(indio_dev);
794 for (i = 0; i < vadc->nchannels; i++)
795 if (vadc->iio_chans[i].channel == iiospec->args[0])
801 static const struct iio_info vadc_info = {
802 .read_raw = vadc_read_raw,
803 .of_xlate = vadc_of_xlate,
804 .driver_module = THIS_MODULE,
807 struct vadc_channels {
808 const char *datasheet_name;
809 unsigned int prescale_index;
810 enum iio_chan_type type;
812 unsigned int scale_fn;
815 #define VADC_CHAN(_dname, _type, _mask, _pre, _scale) \
816 [VADC_##_dname] = { \
817 .datasheet_name = __stringify(_dname), \
818 .prescale_index = _pre, \
820 .info_mask = _mask, \
824 #define VADC_NO_CHAN(_dname, _type, _mask, _pre) \
825 [VADC_##_dname] = { \
826 .datasheet_name = __stringify(_dname), \
827 .prescale_index = _pre, \
832 #define VADC_CHAN_TEMP(_dname, _pre, _scale) \
833 VADC_CHAN(_dname, IIO_TEMP, \
834 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_PROCESSED), \
837 #define VADC_CHAN_VOLT(_dname, _pre, _scale) \
838 VADC_CHAN(_dname, IIO_VOLTAGE, \
839 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_PROCESSED),\
842 #define VADC_CHAN_NO_SCALE(_dname, _pre) \
843 VADC_NO_CHAN(_dname, IIO_VOLTAGE, \
844 BIT(IIO_CHAN_INFO_RAW), \
848 * The array represents all possible ADC channels found in the supported PMICs.
849 * Every index in the array is equal to the channel number per datasheet. The
850 * gaps in the array should be treated as reserved channels.
852 static const struct vadc_channels vadc_chans[] = {
853 VADC_CHAN_VOLT(USBIN, 4, SCALE_DEFAULT)
854 VADC_CHAN_VOLT(DCIN, 4, SCALE_DEFAULT)
855 VADC_CHAN_NO_SCALE(VCHG_SNS, 3)
856 VADC_CHAN_NO_SCALE(SPARE1_03, 1)
857 VADC_CHAN_NO_SCALE(USB_ID_MV, 1)
858 VADC_CHAN_VOLT(VCOIN, 1, SCALE_DEFAULT)
859 VADC_CHAN_NO_SCALE(VBAT_SNS, 1)
860 VADC_CHAN_VOLT(VSYS, 1, SCALE_DEFAULT)
861 VADC_CHAN_TEMP(DIE_TEMP, 0, SCALE_PMIC_THERM)
862 VADC_CHAN_VOLT(REF_625MV, 0, SCALE_DEFAULT)
863 VADC_CHAN_VOLT(REF_1250MV, 0, SCALE_DEFAULT)
864 VADC_CHAN_NO_SCALE(CHG_TEMP, 0)
865 VADC_CHAN_NO_SCALE(SPARE1, 0)
866 VADC_CHAN_TEMP(SPARE2, 0, SCALE_PMI_CHG_TEMP)
867 VADC_CHAN_VOLT(GND_REF, 0, SCALE_DEFAULT)
868 VADC_CHAN_VOLT(VDD_VADC, 0, SCALE_DEFAULT)
870 VADC_CHAN_NO_SCALE(P_MUX1_1_1, 0)
871 VADC_CHAN_NO_SCALE(P_MUX2_1_1, 0)
872 VADC_CHAN_NO_SCALE(P_MUX3_1_1, 0)
873 VADC_CHAN_NO_SCALE(P_MUX4_1_1, 0)
874 VADC_CHAN_NO_SCALE(P_MUX5_1_1, 0)
875 VADC_CHAN_NO_SCALE(P_MUX6_1_1, 0)
876 VADC_CHAN_NO_SCALE(P_MUX7_1_1, 0)
877 VADC_CHAN_NO_SCALE(P_MUX8_1_1, 0)
878 VADC_CHAN_NO_SCALE(P_MUX9_1_1, 0)
879 VADC_CHAN_NO_SCALE(P_MUX10_1_1, 0)
880 VADC_CHAN_NO_SCALE(P_MUX11_1_1, 0)
881 VADC_CHAN_NO_SCALE(P_MUX12_1_1, 0)
882 VADC_CHAN_NO_SCALE(P_MUX13_1_1, 0)
883 VADC_CHAN_NO_SCALE(P_MUX14_1_1, 0)
884 VADC_CHAN_NO_SCALE(P_MUX15_1_1, 0)
885 VADC_CHAN_NO_SCALE(P_MUX16_1_1, 0)
887 VADC_CHAN_NO_SCALE(P_MUX1_1_3, 1)
888 VADC_CHAN_NO_SCALE(P_MUX2_1_3, 1)
889 VADC_CHAN_NO_SCALE(P_MUX3_1_3, 1)
890 VADC_CHAN_NO_SCALE(P_MUX4_1_3, 1)
891 VADC_CHAN_NO_SCALE(P_MUX5_1_3, 1)
892 VADC_CHAN_NO_SCALE(P_MUX6_1_3, 1)
893 VADC_CHAN_NO_SCALE(P_MUX7_1_3, 1)
894 VADC_CHAN_NO_SCALE(P_MUX8_1_3, 1)
895 VADC_CHAN_NO_SCALE(P_MUX9_1_3, 1)
896 VADC_CHAN_NO_SCALE(P_MUX10_1_3, 1)
897 VADC_CHAN_NO_SCALE(P_MUX11_1_3, 1)
898 VADC_CHAN_NO_SCALE(P_MUX12_1_3, 1)
899 VADC_CHAN_NO_SCALE(P_MUX13_1_3, 1)
900 VADC_CHAN_NO_SCALE(P_MUX14_1_3, 1)
901 VADC_CHAN_NO_SCALE(P_MUX15_1_3, 1)
902 VADC_CHAN_NO_SCALE(P_MUX16_1_3, 1)
904 VADC_CHAN_NO_SCALE(LR_MUX1_BAT_THERM, 0)
905 VADC_CHAN_NO_SCALE(LR_MUX2_BAT_ID, 0)
906 VADC_CHAN_NO_SCALE(LR_MUX3_XO_THERM, 0)
907 VADC_CHAN_NO_SCALE(LR_MUX4_AMUX_THM1, 0)
908 VADC_CHAN_NO_SCALE(LR_MUX5_AMUX_THM2, 0)
909 VADC_CHAN_NO_SCALE(LR_MUX6_AMUX_THM3, 0)
910 VADC_CHAN_NO_SCALE(LR_MUX7_HW_ID, 0)
911 VADC_CHAN_NO_SCALE(LR_MUX8_AMUX_THM4, 0)
912 VADC_CHAN_NO_SCALE(LR_MUX9_AMUX_THM5, 0)
913 VADC_CHAN_NO_SCALE(LR_MUX10_USB_ID, 0)
914 VADC_CHAN_NO_SCALE(AMUX_PU1, 0)
915 VADC_CHAN_NO_SCALE(AMUX_PU2, 0)
916 VADC_CHAN_NO_SCALE(LR_MUX3_BUF_XO_THERM, 0)
918 VADC_CHAN_NO_SCALE(LR_MUX1_PU1_BAT_THERM, 0)
919 VADC_CHAN_NO_SCALE(LR_MUX2_PU1_BAT_ID, 0)
920 VADC_CHAN_NO_SCALE(LR_MUX3_PU1_XO_THERM, 0)
921 VADC_CHAN_TEMP(LR_MUX4_PU1_AMUX_THM1, 0, SCALE_THERM_100K_PULLUP)
922 VADC_CHAN_TEMP(LR_MUX5_PU1_AMUX_THM2, 0, SCALE_THERM_100K_PULLUP)
923 VADC_CHAN_TEMP(LR_MUX6_PU1_AMUX_THM3, 0, SCALE_THERM_100K_PULLUP)
924 VADC_CHAN_NO_SCALE(LR_MUX7_PU1_AMUX_HW_ID, 0)
925 VADC_CHAN_TEMP(LR_MUX8_PU1_AMUX_THM4, 0, SCALE_THERM_100K_PULLUP)
926 VADC_CHAN_TEMP(LR_MUX9_PU1_AMUX_THM5, 0, SCALE_THERM_100K_PULLUP)
927 VADC_CHAN_NO_SCALE(LR_MUX10_PU1_AMUX_USB_ID, 0)
928 VADC_CHAN_TEMP(LR_MUX3_BUF_PU1_XO_THERM, 0, SCALE_XOTHERM)
930 VADC_CHAN_NO_SCALE(LR_MUX1_PU2_BAT_THERM, 0)
931 VADC_CHAN_NO_SCALE(LR_MUX2_PU2_BAT_ID, 0)
932 VADC_CHAN_NO_SCALE(LR_MUX3_PU2_XO_THERM, 0)
933 VADC_CHAN_NO_SCALE(LR_MUX4_PU2_AMUX_THM1, 0)
934 VADC_CHAN_NO_SCALE(LR_MUX5_PU2_AMUX_THM2, 0)
935 VADC_CHAN_NO_SCALE(LR_MUX6_PU2_AMUX_THM3, 0)
936 VADC_CHAN_NO_SCALE(LR_MUX7_PU2_AMUX_HW_ID, 0)
937 VADC_CHAN_NO_SCALE(LR_MUX8_PU2_AMUX_THM4, 0)
938 VADC_CHAN_NO_SCALE(LR_MUX9_PU2_AMUX_THM5, 0)
939 VADC_CHAN_NO_SCALE(LR_MUX10_PU2_AMUX_USB_ID, 0)
940 VADC_CHAN_NO_SCALE(LR_MUX3_BUF_PU2_XO_THERM, 0)
942 VADC_CHAN_NO_SCALE(LR_MUX1_PU1_PU2_BAT_THERM, 0)
943 VADC_CHAN_NO_SCALE(LR_MUX2_PU1_PU2_BAT_ID, 0)
944 VADC_CHAN_NO_SCALE(LR_MUX3_PU1_PU2_XO_THERM, 0)
945 VADC_CHAN_NO_SCALE(LR_MUX4_PU1_PU2_AMUX_THM1, 0)
946 VADC_CHAN_NO_SCALE(LR_MUX5_PU1_PU2_AMUX_THM2, 0)
947 VADC_CHAN_NO_SCALE(LR_MUX6_PU1_PU2_AMUX_THM3, 0)
948 VADC_CHAN_NO_SCALE(LR_MUX7_PU1_PU2_AMUX_HW_ID, 0)
949 VADC_CHAN_NO_SCALE(LR_MUX8_PU1_PU2_AMUX_THM4, 0)
950 VADC_CHAN_NO_SCALE(LR_MUX9_PU1_PU2_AMUX_THM5, 0)
951 VADC_CHAN_NO_SCALE(LR_MUX10_PU1_PU2_AMUX_USB_ID, 0)
952 VADC_CHAN_NO_SCALE(LR_MUX3_BUF_PU1_PU2_XO_THERM, 0)
955 static int vadc_get_dt_channel_data(struct device *dev,
956 struct vadc_channel_prop *prop,
957 struct device_node *node)
959 const char *name = node->name;
960 u32 chan, value, varr[2];
963 ret = of_property_read_u32(node, "reg", &chan);
965 dev_err(dev, "invalid channel number %s\n", name);
969 if (chan > VADC_CHAN_MAX || chan < VADC_CHAN_MIN) {
970 dev_err(dev, "%s invalid channel number %d\n", name, chan);
974 /* the channel has DT description */
975 prop->channel = chan;
977 ret = of_property_read_u32(node, "qcom,decimation", &value);
979 ret = vadc_decimation_from_dt(value);
981 dev_err(dev, "%02x invalid decimation %d\n",
985 prop->decimation = ret;
987 prop->decimation = VADC_DEF_DECIMATION;
990 ret = of_property_read_u32_array(node, "qcom,pre-scaling", varr, 2);
992 ret = vadc_prescaling_from_dt(varr[0], varr[1]);
994 dev_err(dev, "%02x invalid pre-scaling <%d %d>\n",
995 chan, varr[0], varr[1]);
998 prop->prescale = ret;
1000 prop->prescale = vadc_chans[prop->channel].prescale_index;
1003 ret = of_property_read_u32(node, "qcom,hw-settle-time", &value);
1005 ret = vadc_hw_settle_time_from_dt(value);
1007 dev_err(dev, "%02x invalid hw-settle-time %d us\n",
1011 prop->hw_settle_time = ret;
1013 prop->hw_settle_time = VADC_DEF_HW_SETTLE_TIME;
1016 ret = of_property_read_u32(node, "qcom,avg-samples", &value);
1018 ret = vadc_avg_samples_from_dt(value);
1020 dev_err(dev, "%02x invalid avg-samples %d\n",
1024 prop->avg_samples = ret;
1026 prop->avg_samples = VADC_DEF_AVG_SAMPLES;
1029 if (of_property_read_bool(node, "qcom,ratiometric"))
1030 prop->calibration = VADC_CALIB_RATIOMETRIC;
1032 prop->calibration = VADC_CALIB_ABSOLUTE;
1034 dev_dbg(dev, "%02x name %s\n", chan, name);
1039 static int vadc_get_dt_data(struct vadc_priv *vadc, struct device_node *node)
1041 const struct vadc_channels *vadc_chan;
1042 struct iio_chan_spec *iio_chan;
1043 struct vadc_channel_prop prop;
1044 struct device_node *child;
1045 unsigned int index = 0;
1048 vadc->nchannels = of_get_available_child_count(node);
1049 if (!vadc->nchannels)
1052 vadc->iio_chans = devm_kcalloc(vadc->dev, vadc->nchannels,
1053 sizeof(*vadc->iio_chans), GFP_KERNEL);
1054 if (!vadc->iio_chans)
1057 vadc->chan_props = devm_kcalloc(vadc->dev, vadc->nchannels,
1058 sizeof(*vadc->chan_props), GFP_KERNEL);
1059 if (!vadc->chan_props)
1062 iio_chan = vadc->iio_chans;
1064 for_each_available_child_of_node(node, child) {
1065 ret = vadc_get_dt_channel_data(vadc->dev, &prop, child);
1071 prop.scale_fn = vadc_chans[prop.channel].scale_fn;
1072 vadc->chan_props[index] = prop;
1074 vadc_chan = &vadc_chans[prop.channel];
1076 iio_chan->channel = prop.channel;
1077 iio_chan->datasheet_name = vadc_chan->datasheet_name;
1078 iio_chan->info_mask_separate = vadc_chan->info_mask;
1079 iio_chan->type = vadc_chan->type;
1080 iio_chan->indexed = 1;
1081 iio_chan->address = index++;
1086 /* These channels are mandatory, they are used as reference points */
1087 if (!vadc_get_channel(vadc, VADC_REF_1250MV)) {
1088 dev_err(vadc->dev, "Please define 1.25V channel\n");
1092 if (!vadc_get_channel(vadc, VADC_REF_625MV)) {
1093 dev_err(vadc->dev, "Please define 0.625V channel\n");
1097 if (!vadc_get_channel(vadc, VADC_VDD_VADC)) {
1098 dev_err(vadc->dev, "Please define VDD channel\n");
1102 if (!vadc_get_channel(vadc, VADC_GND_REF)) {
1103 dev_err(vadc->dev, "Please define GND channel\n");
1110 static irqreturn_t vadc_isr(int irq, void *dev_id)
1112 struct vadc_priv *vadc = dev_id;
1114 complete(&vadc->complete);
1119 static int vadc_check_revision(struct vadc_priv *vadc)
1124 ret = vadc_read(vadc, VADC_PERPH_TYPE, &val);
1128 if (val < VADC_PERPH_TYPE_ADC) {
1129 dev_err(vadc->dev, "%d is not ADC\n", val);
1133 ret = vadc_read(vadc, VADC_PERPH_SUBTYPE, &val);
1137 if (val < VADC_PERPH_SUBTYPE_VADC) {
1138 dev_err(vadc->dev, "%d is not VADC\n", val);
1142 ret = vadc_read(vadc, VADC_REVISION2, &val);
1146 if (val < VADC_REVISION2_SUPPORTED_VADC) {
1147 dev_err(vadc->dev, "revision %d not supported\n", val);
1154 static int vadc_probe(struct platform_device *pdev)
1156 struct device_node *node = pdev->dev.of_node;
1157 struct device *dev = &pdev->dev;
1158 struct iio_dev *indio_dev;
1159 struct vadc_priv *vadc;
1160 struct regmap *regmap;
1164 regmap = dev_get_regmap(dev->parent, NULL);
1168 ret = of_property_read_u32(node, "reg", ®);
1172 indio_dev = devm_iio_device_alloc(dev, sizeof(*vadc));
1176 vadc = iio_priv(indio_dev);
1177 vadc->regmap = regmap;
1180 vadc->are_ref_measured = false;
1181 init_completion(&vadc->complete);
1182 mutex_init(&vadc->lock);
1184 ret = vadc_check_revision(vadc);
1188 ret = vadc_get_dt_data(vadc, node);
1192 irq_eoc = platform_get_irq(pdev, 0);
1194 if (irq_eoc == -EPROBE_DEFER || irq_eoc == -EINVAL)
1196 vadc->poll_eoc = true;
1198 ret = devm_request_irq(dev, irq_eoc, vadc_isr, 0,
1204 ret = vadc_reset(vadc);
1206 dev_err(dev, "reset failed\n");
1210 ret = vadc_measure_ref_points(vadc);
1214 indio_dev->dev.parent = dev;
1215 indio_dev->dev.of_node = node;
1216 indio_dev->name = pdev->name;
1217 indio_dev->modes = INDIO_DIRECT_MODE;
1218 indio_dev->info = &vadc_info;
1219 indio_dev->channels = vadc->iio_chans;
1220 indio_dev->num_channels = vadc->nchannels;
1222 return devm_iio_device_register(dev, indio_dev);
1225 static const struct of_device_id vadc_match_table[] = {
1226 { .compatible = "qcom,spmi-vadc" },
1229 MODULE_DEVICE_TABLE(of, vadc_match_table);
1231 static struct platform_driver vadc_driver = {
1233 .name = "qcom-spmi-vadc",
1234 .of_match_table = vadc_match_table,
1236 .probe = vadc_probe,
1238 module_platform_driver(vadc_driver);
1240 MODULE_ALIAS("platform:qcom-spmi-vadc");
1241 MODULE_DESCRIPTION("Qualcomm SPMI PMIC voltage ADC driver");
1242 MODULE_LICENSE("GPL v2");