1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2012-2016, The Linux Foundation. All rights reserved.
6 #include <linux/bitops.h>
7 #include <linux/completion.h>
8 #include <linux/delay.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
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
{
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.
115 struct regmap
*regmap
;
118 unsigned int nchannels
;
119 struct vadc_channel_prop
*chan_props
;
120 struct iio_chan_spec
*iio_chans
;
121 bool are_ref_measured
;
123 struct completion complete
;
124 struct vadc_linear_graph graph
[2];
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
)
154 ret
= vadc_write(vadc
, VADC_ACCESS
, VADC_ACCESS_DATA
);
158 ret
= vadc_read(vadc
, VADC_PERH_RESET_CTL3
, &data
);
162 ret
= vadc_write(vadc
, VADC_ACCESS
, VADC_ACCESS_DATA
);
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
;
181 ret
= vadc_read(vadc
, VADC_MODE_CTL
, &mode
);
185 ret
= vadc_read(vadc
, VADC_ADC_DIG_PARAM
, &dig
);
189 ret
= vadc_read(vadc
, VADC_ADC_CH_SEL_CTL
, &chan
);
193 ret
= vadc_read(vadc
, VADC_CONV_REQ
, &req
);
197 ret
= vadc_read(vadc
, VADC_STATUS1
, &sta1
);
201 ret
= vadc_read(vadc
, VADC_EN_CTL1
, &en
);
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
;
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
);
223 /* Channel selection */
224 ret
= vadc_write(vadc
, VADC_ADC_CH_SEL_CTL
, prop
->channel
);
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
);
234 /* HW settle time delay */
235 ret
= vadc_write(vadc
, VADC_HW_SETTLE_DELAY
, prop
->hw_settle_time
);
239 ret
= vadc_write(vadc
, VADC_FAST_AVG_CTL
, prop
->avg_samples
);
243 if (prop
->avg_samples
)
244 ret
= vadc_write(vadc
, VADC_FAST_AVG_EN
, VADC_FAST_AVG_EN_SET
);
246 ret
= vadc_write(vadc
, VADC_FAST_AVG_EN
, 0);
251 static int vadc_poll_wait_eoc(struct vadc_priv
*vadc
, unsigned int interval_us
)
253 unsigned int count
, retry
;
257 retry
= interval_us
/ VADC_CONV_TIME_MIN_US
;
259 for (count
= 0; count
< retry
; count
++) {
260 ret
= vadc_read(vadc
, VADC_STATUS1
, &sta1
);
264 sta1
&= VADC_STATUS1_REQ_STS_EOC_MASK
;
265 if (sta1
== VADC_STATUS1_EOC
)
268 usleep_range(VADC_CONV_TIME_MIN_US
, VADC_CONV_TIME_MAX_US
);
271 vadc_show_status(vadc
);
276 static int vadc_read_result(struct vadc_priv
*vadc
, u16
*data
)
280 ret
= regmap_bulk_read(vadc
->regmap
, vadc
->base
+ VADC_DATA
, data
, 2);
284 *data
= clamp_t(u16
, *data
, VADC_MIN_ADC_CODE
, VADC_MAX_ADC_CODE
);
289 static struct vadc_channel_prop
*vadc_get_channel(struct vadc_priv
*vadc
,
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
);
303 static int vadc_do_conversion(struct vadc_priv
*vadc
,
304 struct vadc_channel_prop
*prop
, u16
*data
)
306 unsigned int timeout
;
309 mutex_lock(&vadc
->lock
);
311 ret
= vadc_configure(vadc
, prop
);
316 reinit_completion(&vadc
->complete
);
318 ret
= vadc_set_state(vadc
, true);
322 ret
= vadc_write(vadc
, VADC_CONV_REQ
, VADC_CONV_REQ_SET
);
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
);
331 ret
= wait_for_completion_timeout(&vadc
->complete
, timeout
);
337 /* Double check conversion status */
338 ret
= vadc_poll_wait_eoc(vadc
, VADC_CONV_TIME_MIN_US
);
343 ret
= vadc_read_result(vadc
, data
);
346 vadc_set_state(vadc
, false);
348 dev_err(vadc
->dev
, "conversion failed\n");
350 mutex_unlock(&vadc
->lock
);
354 static int vadc_measure_ref_points(struct vadc_priv
*vadc
)
356 struct vadc_channel_prop
*prop
;
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
);
368 /* Try with buffered 625mV channel first */
369 prop
= vadc_get_channel(vadc
, VADC_SPARE1
);
371 prop
= vadc_get_channel(vadc
, VADC_REF_625MV
);
373 ret
= vadc_do_conversion(vadc
, prop
, &read_2
);
377 if (read_1
== read_2
) {
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
);
391 prop
= vadc_get_channel(vadc
, VADC_GND_REF
);
392 ret
= vadc_do_conversion(vadc
, prop
, &read_2
);
396 if (read_1
== read_2
) {
401 vadc
->graph
[VADC_CALIB_RATIOMETRIC
].dy
= read_1
- read_2
;
402 vadc
->graph
[VADC_CALIB_RATIOMETRIC
].gnd
= read_2
;
405 dev_err(vadc
->dev
, "measure reference points failed\n");
410 static int vadc_prescaling_from_dt(u32 numerator
, u32 denominator
)
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
)
419 if (pre
== ARRAY_SIZE(vadc_prescale_ratios
))
425 static int vadc_hw_settle_time_from_dt(u32 value
)
427 if ((value
<= 1000 && value
% 100) || (value
> 1000 && value
% 2000))
433 value
= value
/ 2000 + 10;
438 static int vadc_avg_samples_from_dt(u32 value
)
440 if (!is_power_of_2(value
) || value
> VADC_AVG_SAMPLES_MAX
)
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
,
450 struct vadc_priv
*vadc
= iio_priv(indio_dev
);
451 struct vadc_channel_prop
*prop
;
456 case IIO_CHAN_INFO_PROCESSED
:
457 prop
= &vadc
->chan_props
[chan
->address
];
458 ret
= vadc_do_conversion(vadc
, prop
, &adc_code
);
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
),
471 case IIO_CHAN_INFO_RAW
:
472 prop
= &vadc
->chan_props
[chan
->address
];
473 ret
= vadc_do_conversion(vadc
, prop
, &adc_code
);
477 *val
= (int)adc_code
;
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
);
493 for (i
= 0; i
< vadc
->nchannels
; i
++)
494 if (vadc
->iio_chans
[i
].channel
== iiospec
->args
[0])
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
;
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, \
528 .info_mask = _mask, \
529 .scale_fn_type = _scale \
532 #define VADC_NO_CHAN(_dname, _type, _mask, _pre) \
533 [VADC_##_dname] = { \
534 .datasheet_name = __stringify(_dname), \
535 .prescale_index = _pre, \
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), \
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),\
550 #define VADC_CHAN_NO_SCALE(_dname, _pre) \
551 VADC_NO_CHAN(_dname, IIO_VOLTAGE, \
552 BIT(IIO_CHAN_INFO_RAW), \
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];
671 ret
= fwnode_property_read_u32(fwnode
, "reg", &chan
);
673 dev_err(dev
, "invalid channel number %s\n", name
);
677 if (chan
> VADC_CHAN_MAX
|| chan
< VADC_CHAN_MIN
) {
678 dev_err(dev
, "%s invalid channel number %d\n", name
, chan
);
682 ret
= fwnode_property_read_string(fwnode
, "label", &label
);
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
);
692 ret
= qcom_vadc_decimation_from_dt(value
);
694 dev_err(dev
, "%02x invalid decimation %d\n",
698 prop
->decimation
= ret
;
700 prop
->decimation
= VADC_DEF_DECIMATION
;
703 ret
= fwnode_property_read_u32_array(fwnode
, "qcom,pre-scaling", varr
, 2);
705 ret
= vadc_prescaling_from_dt(varr
[0], varr
[1]);
707 dev_err(dev
, "%02x invalid pre-scaling <%d %d>\n",
708 chan
, varr
[0], varr
[1]);
711 prop
->prescale
= ret
;
713 prop
->prescale
= vadc_chans
[prop
->channel
].prescale_index
;
716 ret
= fwnode_property_read_u32(fwnode
, "qcom,hw-settle-time", &value
);
718 ret
= vadc_hw_settle_time_from_dt(value
);
720 dev_err(dev
, "%02x invalid hw-settle-time %d us\n",
724 prop
->hw_settle_time
= ret
;
726 prop
->hw_settle_time
= VADC_DEF_HW_SETTLE_TIME
;
729 ret
= fwnode_property_read_u32(fwnode
, "qcom,avg-samples", &value
);
731 ret
= vadc_avg_samples_from_dt(value
);
733 dev_err(dev
, "%02x invalid avg-samples %d\n",
737 prop
->avg_samples
= ret
;
739 prop
->avg_samples
= VADC_DEF_AVG_SAMPLES
;
742 if (fwnode_property_read_bool(fwnode
, "qcom,ratiometric"))
743 prop
->calibration
= VADC_CALIB_RATIOMETRIC
;
745 prop
->calibration
= VADC_CALIB_ABSOLUTE
;
747 dev_dbg(dev
, "%02x name %s\n", chan
, name
);
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;
760 vadc
->nchannels
= device_get_child_node_count(vadc
->dev
);
761 if (!vadc
->nchannels
)
764 vadc
->iio_chans
= devm_kcalloc(vadc
->dev
, vadc
->nchannels
,
765 sizeof(*vadc
->iio_chans
), GFP_KERNEL
);
766 if (!vadc
->iio_chans
)
769 vadc
->chan_props
= devm_kcalloc(vadc
->dev
, vadc
->nchannels
,
770 sizeof(*vadc
->chan_props
), GFP_KERNEL
);
771 if (!vadc
->chan_props
)
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
);
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
++;
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");
802 if (!vadc_get_channel(vadc
, VADC_REF_625MV
)) {
803 dev_err(vadc
->dev
, "Please define 0.625V channel\n");
807 if (!vadc_get_channel(vadc
, VADC_VDD_VADC
)) {
808 dev_err(vadc
->dev
, "Please define VDD channel\n");
812 if (!vadc_get_channel(vadc
, VADC_GND_REF
)) {
813 dev_err(vadc
->dev
, "Please define GND channel\n");
820 static irqreturn_t
vadc_isr(int irq
, void *dev_id
)
822 struct vadc_priv
*vadc
= dev_id
;
824 complete(&vadc
->complete
);
829 static int vadc_check_revision(struct vadc_priv
*vadc
)
834 ret
= vadc_read(vadc
, VADC_PERPH_TYPE
, &val
);
838 if (val
< VADC_PERPH_TYPE_ADC
) {
839 dev_err(vadc
->dev
, "%d is not ADC\n", val
);
843 ret
= vadc_read(vadc
, VADC_PERPH_SUBTYPE
, &val
);
847 if (val
< VADC_PERPH_SUBTYPE_VADC
) {
848 dev_err(vadc
->dev
, "%d is not VADC\n", val
);
852 ret
= vadc_read(vadc
, VADC_REVISION2
, &val
);
856 if (val
< VADC_REVISION2_SUPPORTED_VADC
) {
857 dev_err(vadc
->dev
, "revision %d not supported\n", val
);
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
;
873 regmap
= dev_get_regmap(dev
->parent
, NULL
);
877 ret
= device_property_read_u32(dev
, "reg", ®
);
881 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*vadc
));
885 vadc
= iio_priv(indio_dev
);
886 vadc
->regmap
= regmap
;
889 vadc
->are_ref_measured
= false;
890 init_completion(&vadc
->complete
);
891 mutex_init(&vadc
->lock
);
893 ret
= vadc_check_revision(vadc
);
897 ret
= vadc_get_fw_data(vadc
);
901 irq_eoc
= platform_get_irq(pdev
, 0);
903 if (irq_eoc
== -EPROBE_DEFER
|| irq_eoc
== -EINVAL
)
905 vadc
->poll_eoc
= true;
907 ret
= devm_request_irq(dev
, irq_eoc
, vadc_isr
, 0,
913 ret
= vadc_reset(vadc
);
915 dev_err(dev
, "reset failed\n");
919 ret
= vadc_measure_ref_points(vadc
);
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
= {
940 .name
= "qcom-spmi-vadc",
941 .of_match_table
= vadc_match_table
,
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>");