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/iio.h>
11 #include <linux/interrupt.h>
12 #include <linux/kernel.h>
13 #include <linux/math64.h>
14 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/regmap.h>
18 #include <linux/slab.h>
19 #include <linux/log2.h>
21 #include <dt-bindings/iio/qcom,spmi-vadc.h>
23 #include "qcom-vadc-common.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.
88 struct vadc_channel_prop
{
90 enum vadc_calibration calibration
;
91 unsigned int decimation
;
92 unsigned int prescale
;
93 unsigned int hw_settle_time
;
94 unsigned int avg_samples
;
95 enum vadc_scale_fn_type scale_fn_type
;
99 * struct vadc_priv - VADC private structure.
100 * @regmap: pointer to struct regmap.
101 * @dev: pointer to struct device.
102 * @base: base address for the ADC peripheral.
103 * @nchannels: number of VADC channels.
104 * @chan_props: array of VADC channel properties.
105 * @iio_chans: array of IIO channels specification.
106 * @are_ref_measured: are reference points measured.
107 * @poll_eoc: use polling instead of interrupt.
108 * @complete: VADC result notification after interrupt is received.
109 * @graph: store parameters for calibration.
110 * @lock: ADC lock for access to the peripheral.
113 struct regmap
*regmap
;
116 unsigned int nchannels
;
117 struct vadc_channel_prop
*chan_props
;
118 struct iio_chan_spec
*iio_chans
;
119 bool are_ref_measured
;
121 struct completion complete
;
122 struct vadc_linear_graph graph
[2];
126 static const struct vadc_prescale_ratio vadc_prescale_ratios
[] = {
127 {.num
= 1, .den
= 1},
128 {.num
= 1, .den
= 3},
129 {.num
= 1, .den
= 4},
130 {.num
= 1, .den
= 6},
131 {.num
= 1, .den
= 20},
132 {.num
= 1, .den
= 8},
133 {.num
= 10, .den
= 81},
134 {.num
= 1, .den
= 10}
137 static int vadc_read(struct vadc_priv
*vadc
, u16 offset
, u8
*data
)
139 return regmap_bulk_read(vadc
->regmap
, vadc
->base
+ offset
, data
, 1);
142 static int vadc_write(struct vadc_priv
*vadc
, u16 offset
, u8 data
)
144 return regmap_write(vadc
->regmap
, vadc
->base
+ offset
, data
);
147 static int vadc_reset(struct vadc_priv
*vadc
)
152 ret
= vadc_write(vadc
, VADC_ACCESS
, VADC_ACCESS_DATA
);
156 ret
= vadc_read(vadc
, VADC_PERH_RESET_CTL3
, &data
);
160 ret
= vadc_write(vadc
, VADC_ACCESS
, VADC_ACCESS_DATA
);
164 data
|= VADC_FOLLOW_WARM_RB
;
166 return vadc_write(vadc
, VADC_PERH_RESET_CTL3
, data
);
169 static int vadc_set_state(struct vadc_priv
*vadc
, bool state
)
171 return vadc_write(vadc
, VADC_EN_CTL1
, state
? VADC_EN_CTL1_SET
: 0);
174 static void vadc_show_status(struct vadc_priv
*vadc
)
176 u8 mode
, sta1
, chan
, dig
, en
, req
;
179 ret
= vadc_read(vadc
, VADC_MODE_CTL
, &mode
);
183 ret
= vadc_read(vadc
, VADC_ADC_DIG_PARAM
, &dig
);
187 ret
= vadc_read(vadc
, VADC_ADC_CH_SEL_CTL
, &chan
);
191 ret
= vadc_read(vadc
, VADC_CONV_REQ
, &req
);
195 ret
= vadc_read(vadc
, VADC_STATUS1
, &sta1
);
199 ret
= vadc_read(vadc
, VADC_EN_CTL1
, &en
);
204 "mode:%02x en:%02x chan:%02x dig:%02x req:%02x sta1:%02x\n",
205 mode
, en
, chan
, dig
, req
, sta1
);
208 static int vadc_configure(struct vadc_priv
*vadc
,
209 struct vadc_channel_prop
*prop
)
211 u8 decimation
, mode_ctrl
;
215 mode_ctrl
= (VADC_OP_MODE_NORMAL
<< VADC_OP_MODE_SHIFT
) |
216 VADC_ADC_TRIM_EN
| VADC_AMUX_TRIM_EN
;
217 ret
= vadc_write(vadc
, VADC_MODE_CTL
, mode_ctrl
);
221 /* Channel selection */
222 ret
= vadc_write(vadc
, VADC_ADC_CH_SEL_CTL
, prop
->channel
);
226 /* Digital parameter setup */
227 decimation
= prop
->decimation
<< VADC_ADC_DIG_DEC_RATIO_SEL_SHIFT
;
228 ret
= vadc_write(vadc
, VADC_ADC_DIG_PARAM
, decimation
);
232 /* HW settle time delay */
233 ret
= vadc_write(vadc
, VADC_HW_SETTLE_DELAY
, prop
->hw_settle_time
);
237 ret
= vadc_write(vadc
, VADC_FAST_AVG_CTL
, prop
->avg_samples
);
241 if (prop
->avg_samples
)
242 ret
= vadc_write(vadc
, VADC_FAST_AVG_EN
, VADC_FAST_AVG_EN_SET
);
244 ret
= vadc_write(vadc
, VADC_FAST_AVG_EN
, 0);
249 static int vadc_poll_wait_eoc(struct vadc_priv
*vadc
, unsigned int interval_us
)
251 unsigned int count
, retry
;
255 retry
= interval_us
/ VADC_CONV_TIME_MIN_US
;
257 for (count
= 0; count
< retry
; count
++) {
258 ret
= vadc_read(vadc
, VADC_STATUS1
, &sta1
);
262 sta1
&= VADC_STATUS1_REQ_STS_EOC_MASK
;
263 if (sta1
== VADC_STATUS1_EOC
)
266 usleep_range(VADC_CONV_TIME_MIN_US
, VADC_CONV_TIME_MAX_US
);
269 vadc_show_status(vadc
);
274 static int vadc_read_result(struct vadc_priv
*vadc
, u16
*data
)
278 ret
= regmap_bulk_read(vadc
->regmap
, vadc
->base
+ VADC_DATA
, data
, 2);
282 *data
= clamp_t(u16
, *data
, VADC_MIN_ADC_CODE
, VADC_MAX_ADC_CODE
);
287 static struct vadc_channel_prop
*vadc_get_channel(struct vadc_priv
*vadc
,
292 for (i
= 0; i
< vadc
->nchannels
; i
++)
293 if (vadc
->chan_props
[i
].channel
== num
)
294 return &vadc
->chan_props
[i
];
296 dev_dbg(vadc
->dev
, "no such channel %02x\n", num
);
301 static int vadc_do_conversion(struct vadc_priv
*vadc
,
302 struct vadc_channel_prop
*prop
, u16
*data
)
304 unsigned int timeout
;
307 mutex_lock(&vadc
->lock
);
309 ret
= vadc_configure(vadc
, prop
);
314 reinit_completion(&vadc
->complete
);
316 ret
= vadc_set_state(vadc
, true);
320 ret
= vadc_write(vadc
, VADC_CONV_REQ
, VADC_CONV_REQ_SET
);
324 timeout
= BIT(prop
->avg_samples
) * VADC_CONV_TIME_MIN_US
* 2;
326 if (vadc
->poll_eoc
) {
327 ret
= vadc_poll_wait_eoc(vadc
, timeout
);
329 ret
= wait_for_completion_timeout(&vadc
->complete
, timeout
);
335 /* Double check conversion status */
336 ret
= vadc_poll_wait_eoc(vadc
, VADC_CONV_TIME_MIN_US
);
341 ret
= vadc_read_result(vadc
, data
);
344 vadc_set_state(vadc
, false);
346 dev_err(vadc
->dev
, "conversion failed\n");
348 mutex_unlock(&vadc
->lock
);
352 static int vadc_measure_ref_points(struct vadc_priv
*vadc
)
354 struct vadc_channel_prop
*prop
;
358 vadc
->graph
[VADC_CALIB_RATIOMETRIC
].dx
= VADC_RATIOMETRIC_RANGE
;
359 vadc
->graph
[VADC_CALIB_ABSOLUTE
].dx
= VADC_ABSOLUTE_RANGE_UV
;
361 prop
= vadc_get_channel(vadc
, VADC_REF_1250MV
);
362 ret
= vadc_do_conversion(vadc
, prop
, &read_1
);
366 /* Try with buffered 625mV channel first */
367 prop
= vadc_get_channel(vadc
, VADC_SPARE1
);
369 prop
= vadc_get_channel(vadc
, VADC_REF_625MV
);
371 ret
= vadc_do_conversion(vadc
, prop
, &read_2
);
375 if (read_1
== read_2
) {
380 vadc
->graph
[VADC_CALIB_ABSOLUTE
].dy
= read_1
- read_2
;
381 vadc
->graph
[VADC_CALIB_ABSOLUTE
].gnd
= read_2
;
383 /* Ratiometric calibration */
384 prop
= vadc_get_channel(vadc
, VADC_VDD_VADC
);
385 ret
= vadc_do_conversion(vadc
, prop
, &read_1
);
389 prop
= vadc_get_channel(vadc
, VADC_GND_REF
);
390 ret
= vadc_do_conversion(vadc
, prop
, &read_2
);
394 if (read_1
== read_2
) {
399 vadc
->graph
[VADC_CALIB_RATIOMETRIC
].dy
= read_1
- read_2
;
400 vadc
->graph
[VADC_CALIB_RATIOMETRIC
].gnd
= read_2
;
403 dev_err(vadc
->dev
, "measure reference points failed\n");
408 static int vadc_prescaling_from_dt(u32 num
, u32 den
)
412 for (pre
= 0; pre
< ARRAY_SIZE(vadc_prescale_ratios
); pre
++)
413 if (vadc_prescale_ratios
[pre
].num
== num
&&
414 vadc_prescale_ratios
[pre
].den
== den
)
417 if (pre
== ARRAY_SIZE(vadc_prescale_ratios
))
423 static int vadc_hw_settle_time_from_dt(u32 value
)
425 if ((value
<= 1000 && value
% 100) || (value
> 1000 && value
% 2000))
431 value
= value
/ 2000 + 10;
436 static int vadc_avg_samples_from_dt(u32 value
)
438 if (!is_power_of_2(value
) || value
> VADC_AVG_SAMPLES_MAX
)
441 return __ffs64(value
);
444 static int vadc_read_raw(struct iio_dev
*indio_dev
,
445 struct iio_chan_spec
const *chan
, int *val
, int *val2
,
448 struct vadc_priv
*vadc
= iio_priv(indio_dev
);
449 struct vadc_channel_prop
*prop
;
454 case IIO_CHAN_INFO_PROCESSED
:
455 prop
= &vadc
->chan_props
[chan
->address
];
456 ret
= vadc_do_conversion(vadc
, prop
, &adc_code
);
460 ret
= qcom_vadc_scale(prop
->scale_fn_type
,
461 &vadc
->graph
[prop
->calibration
],
462 &vadc_prescale_ratios
[prop
->prescale
],
463 (prop
->calibration
== VADC_CALIB_ABSOLUTE
),
469 case IIO_CHAN_INFO_RAW
:
470 prop
= &vadc
->chan_props
[chan
->address
];
471 ret
= vadc_do_conversion(vadc
, prop
, &adc_code
);
475 *val
= (int)adc_code
;
485 static int vadc_of_xlate(struct iio_dev
*indio_dev
,
486 const struct of_phandle_args
*iiospec
)
488 struct vadc_priv
*vadc
= iio_priv(indio_dev
);
491 for (i
= 0; i
< vadc
->nchannels
; i
++)
492 if (vadc
->iio_chans
[i
].channel
== iiospec
->args
[0])
498 static const struct iio_info vadc_info
= {
499 .read_raw
= vadc_read_raw
,
500 .of_xlate
= vadc_of_xlate
,
503 struct vadc_channels
{
504 const char *datasheet_name
;
505 unsigned int prescale_index
;
506 enum iio_chan_type type
;
508 enum vadc_scale_fn_type scale_fn_type
;
511 #define VADC_CHAN(_dname, _type, _mask, _pre, _scale) \
512 [VADC_##_dname] = { \
513 .datasheet_name = __stringify(_dname), \
514 .prescale_index = _pre, \
516 .info_mask = _mask, \
517 .scale_fn_type = _scale \
520 #define VADC_NO_CHAN(_dname, _type, _mask, _pre) \
521 [VADC_##_dname] = { \
522 .datasheet_name = __stringify(_dname), \
523 .prescale_index = _pre, \
528 #define VADC_CHAN_TEMP(_dname, _pre, _scale) \
529 VADC_CHAN(_dname, IIO_TEMP, \
530 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_PROCESSED), \
533 #define VADC_CHAN_VOLT(_dname, _pre, _scale) \
534 VADC_CHAN(_dname, IIO_VOLTAGE, \
535 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_PROCESSED),\
538 #define VADC_CHAN_NO_SCALE(_dname, _pre) \
539 VADC_NO_CHAN(_dname, IIO_VOLTAGE, \
540 BIT(IIO_CHAN_INFO_RAW), \
544 * The array represents all possible ADC channels found in the supported PMICs.
545 * Every index in the array is equal to the channel number per datasheet. The
546 * gaps in the array should be treated as reserved channels.
548 static const struct vadc_channels vadc_chans
[] = {
549 VADC_CHAN_VOLT(USBIN
, 4, SCALE_DEFAULT
)
550 VADC_CHAN_VOLT(DCIN
, 4, SCALE_DEFAULT
)
551 VADC_CHAN_NO_SCALE(VCHG_SNS
, 3)
552 VADC_CHAN_NO_SCALE(SPARE1_03
, 1)
553 VADC_CHAN_NO_SCALE(USB_ID_MV
, 1)
554 VADC_CHAN_VOLT(VCOIN
, 1, SCALE_DEFAULT
)
555 VADC_CHAN_NO_SCALE(VBAT_SNS
, 1)
556 VADC_CHAN_VOLT(VSYS
, 1, SCALE_DEFAULT
)
557 VADC_CHAN_TEMP(DIE_TEMP
, 0, SCALE_PMIC_THERM
)
558 VADC_CHAN_VOLT(REF_625MV
, 0, SCALE_DEFAULT
)
559 VADC_CHAN_VOLT(REF_1250MV
, 0, SCALE_DEFAULT
)
560 VADC_CHAN_NO_SCALE(CHG_TEMP
, 0)
561 VADC_CHAN_NO_SCALE(SPARE1
, 0)
562 VADC_CHAN_TEMP(SPARE2
, 0, SCALE_PMI_CHG_TEMP
)
563 VADC_CHAN_VOLT(GND_REF
, 0, SCALE_DEFAULT
)
564 VADC_CHAN_VOLT(VDD_VADC
, 0, SCALE_DEFAULT
)
566 VADC_CHAN_NO_SCALE(P_MUX1_1_1
, 0)
567 VADC_CHAN_NO_SCALE(P_MUX2_1_1
, 0)
568 VADC_CHAN_NO_SCALE(P_MUX3_1_1
, 0)
569 VADC_CHAN_NO_SCALE(P_MUX4_1_1
, 0)
570 VADC_CHAN_NO_SCALE(P_MUX5_1_1
, 0)
571 VADC_CHAN_NO_SCALE(P_MUX6_1_1
, 0)
572 VADC_CHAN_NO_SCALE(P_MUX7_1_1
, 0)
573 VADC_CHAN_NO_SCALE(P_MUX8_1_1
, 0)
574 VADC_CHAN_NO_SCALE(P_MUX9_1_1
, 0)
575 VADC_CHAN_NO_SCALE(P_MUX10_1_1
, 0)
576 VADC_CHAN_NO_SCALE(P_MUX11_1_1
, 0)
577 VADC_CHAN_NO_SCALE(P_MUX12_1_1
, 0)
578 VADC_CHAN_NO_SCALE(P_MUX13_1_1
, 0)
579 VADC_CHAN_NO_SCALE(P_MUX14_1_1
, 0)
580 VADC_CHAN_NO_SCALE(P_MUX15_1_1
, 0)
581 VADC_CHAN_NO_SCALE(P_MUX16_1_1
, 0)
583 VADC_CHAN_NO_SCALE(P_MUX1_1_3
, 1)
584 VADC_CHAN_NO_SCALE(P_MUX2_1_3
, 1)
585 VADC_CHAN_NO_SCALE(P_MUX3_1_3
, 1)
586 VADC_CHAN_NO_SCALE(P_MUX4_1_3
, 1)
587 VADC_CHAN_NO_SCALE(P_MUX5_1_3
, 1)
588 VADC_CHAN_NO_SCALE(P_MUX6_1_3
, 1)
589 VADC_CHAN_NO_SCALE(P_MUX7_1_3
, 1)
590 VADC_CHAN_NO_SCALE(P_MUX8_1_3
, 1)
591 VADC_CHAN_NO_SCALE(P_MUX9_1_3
, 1)
592 VADC_CHAN_NO_SCALE(P_MUX10_1_3
, 1)
593 VADC_CHAN_NO_SCALE(P_MUX11_1_3
, 1)
594 VADC_CHAN_NO_SCALE(P_MUX12_1_3
, 1)
595 VADC_CHAN_NO_SCALE(P_MUX13_1_3
, 1)
596 VADC_CHAN_NO_SCALE(P_MUX14_1_3
, 1)
597 VADC_CHAN_NO_SCALE(P_MUX15_1_3
, 1)
598 VADC_CHAN_NO_SCALE(P_MUX16_1_3
, 1)
600 VADC_CHAN_NO_SCALE(LR_MUX1_BAT_THERM
, 0)
601 VADC_CHAN_NO_SCALE(LR_MUX2_BAT_ID
, 0)
602 VADC_CHAN_NO_SCALE(LR_MUX3_XO_THERM
, 0)
603 VADC_CHAN_NO_SCALE(LR_MUX4_AMUX_THM1
, 0)
604 VADC_CHAN_NO_SCALE(LR_MUX5_AMUX_THM2
, 0)
605 VADC_CHAN_NO_SCALE(LR_MUX6_AMUX_THM3
, 0)
606 VADC_CHAN_NO_SCALE(LR_MUX7_HW_ID
, 0)
607 VADC_CHAN_NO_SCALE(LR_MUX8_AMUX_THM4
, 0)
608 VADC_CHAN_NO_SCALE(LR_MUX9_AMUX_THM5
, 0)
609 VADC_CHAN_NO_SCALE(LR_MUX10_USB_ID
, 0)
610 VADC_CHAN_NO_SCALE(AMUX_PU1
, 0)
611 VADC_CHAN_NO_SCALE(AMUX_PU2
, 0)
612 VADC_CHAN_NO_SCALE(LR_MUX3_BUF_XO_THERM
, 0)
614 VADC_CHAN_NO_SCALE(LR_MUX1_PU1_BAT_THERM
, 0)
615 VADC_CHAN_NO_SCALE(LR_MUX2_PU1_BAT_ID
, 0)
616 VADC_CHAN_NO_SCALE(LR_MUX3_PU1_XO_THERM
, 0)
617 VADC_CHAN_TEMP(LR_MUX4_PU1_AMUX_THM1
, 0, SCALE_THERM_100K_PULLUP
)
618 VADC_CHAN_TEMP(LR_MUX5_PU1_AMUX_THM2
, 0, SCALE_THERM_100K_PULLUP
)
619 VADC_CHAN_TEMP(LR_MUX6_PU1_AMUX_THM3
, 0, SCALE_THERM_100K_PULLUP
)
620 VADC_CHAN_NO_SCALE(LR_MUX7_PU1_AMUX_HW_ID
, 0)
621 VADC_CHAN_TEMP(LR_MUX8_PU1_AMUX_THM4
, 0, SCALE_THERM_100K_PULLUP
)
622 VADC_CHAN_TEMP(LR_MUX9_PU1_AMUX_THM5
, 0, SCALE_THERM_100K_PULLUP
)
623 VADC_CHAN_NO_SCALE(LR_MUX10_PU1_AMUX_USB_ID
, 0)
624 VADC_CHAN_TEMP(LR_MUX3_BUF_PU1_XO_THERM
, 0, SCALE_XOTHERM
)
626 VADC_CHAN_NO_SCALE(LR_MUX1_PU2_BAT_THERM
, 0)
627 VADC_CHAN_NO_SCALE(LR_MUX2_PU2_BAT_ID
, 0)
628 VADC_CHAN_NO_SCALE(LR_MUX3_PU2_XO_THERM
, 0)
629 VADC_CHAN_NO_SCALE(LR_MUX4_PU2_AMUX_THM1
, 0)
630 VADC_CHAN_NO_SCALE(LR_MUX5_PU2_AMUX_THM2
, 0)
631 VADC_CHAN_NO_SCALE(LR_MUX6_PU2_AMUX_THM3
, 0)
632 VADC_CHAN_NO_SCALE(LR_MUX7_PU2_AMUX_HW_ID
, 0)
633 VADC_CHAN_NO_SCALE(LR_MUX8_PU2_AMUX_THM4
, 0)
634 VADC_CHAN_NO_SCALE(LR_MUX9_PU2_AMUX_THM5
, 0)
635 VADC_CHAN_NO_SCALE(LR_MUX10_PU2_AMUX_USB_ID
, 0)
636 VADC_CHAN_NO_SCALE(LR_MUX3_BUF_PU2_XO_THERM
, 0)
638 VADC_CHAN_NO_SCALE(LR_MUX1_PU1_PU2_BAT_THERM
, 0)
639 VADC_CHAN_NO_SCALE(LR_MUX2_PU1_PU2_BAT_ID
, 0)
640 VADC_CHAN_NO_SCALE(LR_MUX3_PU1_PU2_XO_THERM
, 0)
641 VADC_CHAN_NO_SCALE(LR_MUX4_PU1_PU2_AMUX_THM1
, 0)
642 VADC_CHAN_NO_SCALE(LR_MUX5_PU1_PU2_AMUX_THM2
, 0)
643 VADC_CHAN_NO_SCALE(LR_MUX6_PU1_PU2_AMUX_THM3
, 0)
644 VADC_CHAN_NO_SCALE(LR_MUX7_PU1_PU2_AMUX_HW_ID
, 0)
645 VADC_CHAN_NO_SCALE(LR_MUX8_PU1_PU2_AMUX_THM4
, 0)
646 VADC_CHAN_NO_SCALE(LR_MUX9_PU1_PU2_AMUX_THM5
, 0)
647 VADC_CHAN_NO_SCALE(LR_MUX10_PU1_PU2_AMUX_USB_ID
, 0)
648 VADC_CHAN_NO_SCALE(LR_MUX3_BUF_PU1_PU2_XO_THERM
, 0)
651 static int vadc_get_dt_channel_data(struct device
*dev
,
652 struct vadc_channel_prop
*prop
,
653 struct device_node
*node
)
655 const char *name
= node
->name
;
656 u32 chan
, value
, varr
[2];
659 ret
= of_property_read_u32(node
, "reg", &chan
);
661 dev_err(dev
, "invalid channel number %s\n", name
);
665 if (chan
> VADC_CHAN_MAX
|| chan
< VADC_CHAN_MIN
) {
666 dev_err(dev
, "%s invalid channel number %d\n", name
, chan
);
670 /* the channel has DT description */
671 prop
->channel
= chan
;
673 ret
= of_property_read_u32(node
, "qcom,decimation", &value
);
675 ret
= qcom_vadc_decimation_from_dt(value
);
677 dev_err(dev
, "%02x invalid decimation %d\n",
681 prop
->decimation
= ret
;
683 prop
->decimation
= VADC_DEF_DECIMATION
;
686 ret
= of_property_read_u32_array(node
, "qcom,pre-scaling", varr
, 2);
688 ret
= vadc_prescaling_from_dt(varr
[0], varr
[1]);
690 dev_err(dev
, "%02x invalid pre-scaling <%d %d>\n",
691 chan
, varr
[0], varr
[1]);
694 prop
->prescale
= ret
;
696 prop
->prescale
= vadc_chans
[prop
->channel
].prescale_index
;
699 ret
= of_property_read_u32(node
, "qcom,hw-settle-time", &value
);
701 ret
= vadc_hw_settle_time_from_dt(value
);
703 dev_err(dev
, "%02x invalid hw-settle-time %d us\n",
707 prop
->hw_settle_time
= ret
;
709 prop
->hw_settle_time
= VADC_DEF_HW_SETTLE_TIME
;
712 ret
= of_property_read_u32(node
, "qcom,avg-samples", &value
);
714 ret
= vadc_avg_samples_from_dt(value
);
716 dev_err(dev
, "%02x invalid avg-samples %d\n",
720 prop
->avg_samples
= ret
;
722 prop
->avg_samples
= VADC_DEF_AVG_SAMPLES
;
725 if (of_property_read_bool(node
, "qcom,ratiometric"))
726 prop
->calibration
= VADC_CALIB_RATIOMETRIC
;
728 prop
->calibration
= VADC_CALIB_ABSOLUTE
;
730 dev_dbg(dev
, "%02x name %s\n", chan
, name
);
735 static int vadc_get_dt_data(struct vadc_priv
*vadc
, struct device_node
*node
)
737 const struct vadc_channels
*vadc_chan
;
738 struct iio_chan_spec
*iio_chan
;
739 struct vadc_channel_prop prop
;
740 struct device_node
*child
;
741 unsigned int index
= 0;
744 vadc
->nchannels
= of_get_available_child_count(node
);
745 if (!vadc
->nchannels
)
748 vadc
->iio_chans
= devm_kcalloc(vadc
->dev
, vadc
->nchannels
,
749 sizeof(*vadc
->iio_chans
), GFP_KERNEL
);
750 if (!vadc
->iio_chans
)
753 vadc
->chan_props
= devm_kcalloc(vadc
->dev
, vadc
->nchannels
,
754 sizeof(*vadc
->chan_props
), GFP_KERNEL
);
755 if (!vadc
->chan_props
)
758 iio_chan
= vadc
->iio_chans
;
760 for_each_available_child_of_node(node
, child
) {
761 ret
= vadc_get_dt_channel_data(vadc
->dev
, &prop
, child
);
767 prop
.scale_fn_type
= vadc_chans
[prop
.channel
].scale_fn_type
;
768 vadc
->chan_props
[index
] = prop
;
770 vadc_chan
= &vadc_chans
[prop
.channel
];
772 iio_chan
->channel
= prop
.channel
;
773 iio_chan
->datasheet_name
= vadc_chan
->datasheet_name
;
774 iio_chan
->info_mask_separate
= vadc_chan
->info_mask
;
775 iio_chan
->type
= vadc_chan
->type
;
776 iio_chan
->indexed
= 1;
777 iio_chan
->address
= index
++;
782 /* These channels are mandatory, they are used as reference points */
783 if (!vadc_get_channel(vadc
, VADC_REF_1250MV
)) {
784 dev_err(vadc
->dev
, "Please define 1.25V channel\n");
788 if (!vadc_get_channel(vadc
, VADC_REF_625MV
)) {
789 dev_err(vadc
->dev
, "Please define 0.625V channel\n");
793 if (!vadc_get_channel(vadc
, VADC_VDD_VADC
)) {
794 dev_err(vadc
->dev
, "Please define VDD channel\n");
798 if (!vadc_get_channel(vadc
, VADC_GND_REF
)) {
799 dev_err(vadc
->dev
, "Please define GND channel\n");
806 static irqreturn_t
vadc_isr(int irq
, void *dev_id
)
808 struct vadc_priv
*vadc
= dev_id
;
810 complete(&vadc
->complete
);
815 static int vadc_check_revision(struct vadc_priv
*vadc
)
820 ret
= vadc_read(vadc
, VADC_PERPH_TYPE
, &val
);
824 if (val
< VADC_PERPH_TYPE_ADC
) {
825 dev_err(vadc
->dev
, "%d is not ADC\n", val
);
829 ret
= vadc_read(vadc
, VADC_PERPH_SUBTYPE
, &val
);
833 if (val
< VADC_PERPH_SUBTYPE_VADC
) {
834 dev_err(vadc
->dev
, "%d is not VADC\n", val
);
838 ret
= vadc_read(vadc
, VADC_REVISION2
, &val
);
842 if (val
< VADC_REVISION2_SUPPORTED_VADC
) {
843 dev_err(vadc
->dev
, "revision %d not supported\n", val
);
850 static int vadc_probe(struct platform_device
*pdev
)
852 struct device_node
*node
= pdev
->dev
.of_node
;
853 struct device
*dev
= &pdev
->dev
;
854 struct iio_dev
*indio_dev
;
855 struct vadc_priv
*vadc
;
856 struct regmap
*regmap
;
860 regmap
= dev_get_regmap(dev
->parent
, NULL
);
864 ret
= of_property_read_u32(node
, "reg", ®
);
868 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*vadc
));
872 vadc
= iio_priv(indio_dev
);
873 vadc
->regmap
= regmap
;
876 vadc
->are_ref_measured
= false;
877 init_completion(&vadc
->complete
);
878 mutex_init(&vadc
->lock
);
880 ret
= vadc_check_revision(vadc
);
884 ret
= vadc_get_dt_data(vadc
, node
);
888 irq_eoc
= platform_get_irq(pdev
, 0);
890 if (irq_eoc
== -EPROBE_DEFER
|| irq_eoc
== -EINVAL
)
892 vadc
->poll_eoc
= true;
894 ret
= devm_request_irq(dev
, irq_eoc
, vadc_isr
, 0,
900 ret
= vadc_reset(vadc
);
902 dev_err(dev
, "reset failed\n");
906 ret
= vadc_measure_ref_points(vadc
);
910 indio_dev
->name
= pdev
->name
;
911 indio_dev
->modes
= INDIO_DIRECT_MODE
;
912 indio_dev
->info
= &vadc_info
;
913 indio_dev
->channels
= vadc
->iio_chans
;
914 indio_dev
->num_channels
= vadc
->nchannels
;
916 return devm_iio_device_register(dev
, indio_dev
);
919 static const struct of_device_id vadc_match_table
[] = {
920 { .compatible
= "qcom,spmi-vadc" },
923 MODULE_DEVICE_TABLE(of
, vadc_match_table
);
925 static struct platform_driver vadc_driver
= {
927 .name
= "qcom-spmi-vadc",
928 .of_match_table
= vadc_match_table
,
932 module_platform_driver(vadc_driver
);
934 MODULE_ALIAS("platform:qcom-spmi-vadc");
935 MODULE_DESCRIPTION("Qualcomm SPMI PMIC voltage ADC driver");
936 MODULE_LICENSE("GPL v2");
937 MODULE_AUTHOR("Stanimir Varbanov <svarbanov@mm-sol.com>");
938 MODULE_AUTHOR("Ivan T. Ivanov <iivanov@mm-sol.com>");