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/module.h>
23 #include <linux/platform_device.h>
24 #include <linux/regmap.h>
25 #include <linux/slab.h>
26 #include <linux/log2.h>
28 #include <dt-bindings/iio/qcom,spmi-vadc.h>
30 /* VADC register and bit definitions */
31 #define VADC_REVISION2 0x1
32 #define VADC_REVISION2_SUPPORTED_VADC 1
34 #define VADC_PERPH_TYPE 0x4
35 #define VADC_PERPH_TYPE_ADC 8
37 #define VADC_PERPH_SUBTYPE 0x5
38 #define VADC_PERPH_SUBTYPE_VADC 1
40 #define VADC_STATUS1 0x8
41 #define VADC_STATUS1_OP_MODE 4
42 #define VADC_STATUS1_REQ_STS BIT(1)
43 #define VADC_STATUS1_EOC BIT(0)
44 #define VADC_STATUS1_REQ_STS_EOC_MASK 0x3
46 #define VADC_MODE_CTL 0x40
47 #define VADC_OP_MODE_SHIFT 3
48 #define VADC_OP_MODE_NORMAL 0
49 #define VADC_AMUX_TRIM_EN BIT(1)
50 #define VADC_ADC_TRIM_EN BIT(0)
52 #define VADC_EN_CTL1 0x46
53 #define VADC_EN_CTL1_SET BIT(7)
55 #define VADC_ADC_CH_SEL_CTL 0x48
57 #define VADC_ADC_DIG_PARAM 0x50
58 #define VADC_ADC_DIG_DEC_RATIO_SEL_SHIFT 2
60 #define VADC_HW_SETTLE_DELAY 0x51
62 #define VADC_CONV_REQ 0x52
63 #define VADC_CONV_REQ_SET BIT(7)
65 #define VADC_FAST_AVG_CTL 0x5a
66 #define VADC_FAST_AVG_EN 0x5b
67 #define VADC_FAST_AVG_EN_SET BIT(7)
69 #define VADC_ACCESS 0xd0
70 #define VADC_ACCESS_DATA 0xa5
72 #define VADC_PERH_RESET_CTL3 0xda
73 #define VADC_FOLLOW_WARM_RB BIT(2)
75 #define VADC_DATA 0x60 /* 16 bits */
77 #define VADC_CONV_TIME_MIN_US 2000
78 #define VADC_CONV_TIME_MAX_US 2100
80 /* Min ADC code represents 0V */
81 #define VADC_MIN_ADC_CODE 0x6000
82 /* Max ADC code represents full-scale range of 1.8V */
83 #define VADC_MAX_ADC_CODE 0xa800
85 #define VADC_ABSOLUTE_RANGE_UV 625000
86 #define VADC_RATIOMETRIC_RANGE_UV 1800000
88 #define VADC_DEF_PRESCALING 0 /* 1:1 */
89 #define VADC_DEF_DECIMATION 0 /* 512 */
90 #define VADC_DEF_HW_SETTLE_TIME 0 /* 0 us */
91 #define VADC_DEF_AVG_SAMPLES 0 /* 1 sample */
92 #define VADC_DEF_CALIB_TYPE VADC_CALIB_ABSOLUTE
94 #define VADC_DECIMATION_MIN 512
95 #define VADC_DECIMATION_MAX 4096
97 #define VADC_HW_SETTLE_DELAY_MAX 10000
98 #define VADC_AVG_SAMPLES_MAX 512
100 #define KELVINMIL_CELSIUSMIL 273150
102 #define VADC_CHAN_MIN VADC_USBIN
103 #define VADC_CHAN_MAX VADC_LR_MUX3_BUF_PU1_PU2_XO_THERM
106 * VADC_CALIB_ABSOLUTE: uses the 625mV and 1.25V as reference channels.
107 * VADC_CALIB_RATIOMETRIC: uses the reference voltage (1.8V) and GND for
110 enum vadc_calibration
{
111 VADC_CALIB_ABSOLUTE
= 0,
112 VADC_CALIB_RATIOMETRIC
116 * struct vadc_linear_graph - Represent ADC characteristics.
117 * @dy: numerator slope to calculate the gain.
118 * @dx: denominator slope to calculate the gain.
119 * @gnd: A/D word of the ground reference used for the channel.
121 * Each ADC device has different offset and gain parameters which are
122 * computed to calibrate the device.
124 struct vadc_linear_graph
{
131 * struct vadc_prescale_ratio - Represent scaling ratio for ADC input.
132 * @num: the inverse numerator of the gain applied to the input channel.
133 * @den: the inverse denominator of the gain applied to the input channel.
135 struct vadc_prescale_ratio
{
141 * struct vadc_channel_prop - VADC channel property.
142 * @channel: channel number, refer to the channel list.
143 * @calibration: calibration type.
144 * @decimation: sampling rate supported for the channel.
145 * @prescale: channel scaling performed on the input signal.
146 * @hw_settle_time: the time between AMUX being configured and the
147 * start of conversion.
148 * @avg_samples: ability to provide single result from the ADC
149 * that is an average of multiple measurements.
151 struct vadc_channel_prop
{
152 unsigned int channel
;
153 enum vadc_calibration calibration
;
154 unsigned int decimation
;
155 unsigned int prescale
;
156 unsigned int hw_settle_time
;
157 unsigned int avg_samples
;
161 * struct vadc_priv - VADC private structure.
162 * @regmap: pointer to struct regmap.
163 * @dev: pointer to struct device.
164 * @base: base address for the ADC peripheral.
165 * @nchannels: number of VADC channels.
166 * @chan_props: array of VADC channel properties.
167 * @iio_chans: array of IIO channels specification.
168 * @are_ref_measured: are reference points measured.
169 * @poll_eoc: use polling instead of interrupt.
170 * @complete: VADC result notification after interrupt is received.
171 * @graph: store parameters for calibration.
172 * @lock: ADC lock for access to the peripheral.
175 struct regmap
*regmap
;
178 unsigned int nchannels
;
179 struct vadc_channel_prop
*chan_props
;
180 struct iio_chan_spec
*iio_chans
;
181 bool are_ref_measured
;
183 struct completion complete
;
184 struct vadc_linear_graph graph
[2];
188 static const struct vadc_prescale_ratio vadc_prescale_ratios
[] = {
189 {.num
= 1, .den
= 1},
190 {.num
= 1, .den
= 3},
191 {.num
= 1, .den
= 4},
192 {.num
= 1, .den
= 6},
193 {.num
= 1, .den
= 20},
194 {.num
= 1, .den
= 8},
195 {.num
= 10, .den
= 81},
196 {.num
= 1, .den
= 10}
199 static int vadc_read(struct vadc_priv
*vadc
, u16 offset
, u8
*data
)
201 return regmap_bulk_read(vadc
->regmap
, vadc
->base
+ offset
, data
, 1);
204 static int vadc_write(struct vadc_priv
*vadc
, u16 offset
, u8 data
)
206 return regmap_write(vadc
->regmap
, vadc
->base
+ offset
, data
);
209 static int vadc_reset(struct vadc_priv
*vadc
)
214 ret
= vadc_write(vadc
, VADC_ACCESS
, VADC_ACCESS_DATA
);
218 ret
= vadc_read(vadc
, VADC_PERH_RESET_CTL3
, &data
);
222 ret
= vadc_write(vadc
, VADC_ACCESS
, VADC_ACCESS_DATA
);
226 data
|= VADC_FOLLOW_WARM_RB
;
228 return vadc_write(vadc
, VADC_PERH_RESET_CTL3
, data
);
231 static int vadc_set_state(struct vadc_priv
*vadc
, bool state
)
233 return vadc_write(vadc
, VADC_EN_CTL1
, state
? VADC_EN_CTL1_SET
: 0);
236 static void vadc_show_status(struct vadc_priv
*vadc
)
238 u8 mode
, sta1
, chan
, dig
, en
, req
;
241 ret
= vadc_read(vadc
, VADC_MODE_CTL
, &mode
);
245 ret
= vadc_read(vadc
, VADC_ADC_DIG_PARAM
, &dig
);
249 ret
= vadc_read(vadc
, VADC_ADC_CH_SEL_CTL
, &chan
);
253 ret
= vadc_read(vadc
, VADC_CONV_REQ
, &req
);
257 ret
= vadc_read(vadc
, VADC_STATUS1
, &sta1
);
261 ret
= vadc_read(vadc
, VADC_EN_CTL1
, &en
);
266 "mode:%02x en:%02x chan:%02x dig:%02x req:%02x sta1:%02x\n",
267 mode
, en
, chan
, dig
, req
, sta1
);
270 static int vadc_configure(struct vadc_priv
*vadc
,
271 struct vadc_channel_prop
*prop
)
273 u8 decimation
, mode_ctrl
;
277 mode_ctrl
= (VADC_OP_MODE_NORMAL
<< VADC_OP_MODE_SHIFT
) |
278 VADC_ADC_TRIM_EN
| VADC_AMUX_TRIM_EN
;
279 ret
= vadc_write(vadc
, VADC_MODE_CTL
, mode_ctrl
);
283 /* Channel selection */
284 ret
= vadc_write(vadc
, VADC_ADC_CH_SEL_CTL
, prop
->channel
);
288 /* Digital parameter setup */
289 decimation
= prop
->decimation
<< VADC_ADC_DIG_DEC_RATIO_SEL_SHIFT
;
290 ret
= vadc_write(vadc
, VADC_ADC_DIG_PARAM
, decimation
);
294 /* HW settle time delay */
295 ret
= vadc_write(vadc
, VADC_HW_SETTLE_DELAY
, prop
->hw_settle_time
);
299 ret
= vadc_write(vadc
, VADC_FAST_AVG_CTL
, prop
->avg_samples
);
303 if (prop
->avg_samples
)
304 ret
= vadc_write(vadc
, VADC_FAST_AVG_EN
, VADC_FAST_AVG_EN_SET
);
306 ret
= vadc_write(vadc
, VADC_FAST_AVG_EN
, 0);
311 static int vadc_poll_wait_eoc(struct vadc_priv
*vadc
, unsigned int interval_us
)
313 unsigned int count
, retry
;
317 retry
= interval_us
/ VADC_CONV_TIME_MIN_US
;
319 for (count
= 0; count
< retry
; count
++) {
320 ret
= vadc_read(vadc
, VADC_STATUS1
, &sta1
);
324 sta1
&= VADC_STATUS1_REQ_STS_EOC_MASK
;
325 if (sta1
== VADC_STATUS1_EOC
)
328 usleep_range(VADC_CONV_TIME_MIN_US
, VADC_CONV_TIME_MAX_US
);
331 vadc_show_status(vadc
);
336 static int vadc_read_result(struct vadc_priv
*vadc
, u16
*data
)
340 ret
= regmap_bulk_read(vadc
->regmap
, vadc
->base
+ VADC_DATA
, data
, 2);
344 *data
= clamp_t(u16
, *data
, VADC_MIN_ADC_CODE
, VADC_MAX_ADC_CODE
);
349 static struct vadc_channel_prop
*vadc_get_channel(struct vadc_priv
*vadc
,
354 for (i
= 0; i
< vadc
->nchannels
; i
++)
355 if (vadc
->chan_props
[i
].channel
== num
)
356 return &vadc
->chan_props
[i
];
358 dev_dbg(vadc
->dev
, "no such channel %02x\n", num
);
363 static int vadc_do_conversion(struct vadc_priv
*vadc
,
364 struct vadc_channel_prop
*prop
, u16
*data
)
366 unsigned int timeout
;
369 mutex_lock(&vadc
->lock
);
371 ret
= vadc_configure(vadc
, prop
);
376 reinit_completion(&vadc
->complete
);
378 ret
= vadc_set_state(vadc
, true);
382 ret
= vadc_write(vadc
, VADC_CONV_REQ
, VADC_CONV_REQ_SET
);
386 timeout
= BIT(prop
->avg_samples
) * VADC_CONV_TIME_MIN_US
* 2;
388 if (vadc
->poll_eoc
) {
389 ret
= vadc_poll_wait_eoc(vadc
, timeout
);
391 ret
= wait_for_completion_timeout(&vadc
->complete
, timeout
);
397 /* Double check conversion status */
398 ret
= vadc_poll_wait_eoc(vadc
, VADC_CONV_TIME_MIN_US
);
403 ret
= vadc_read_result(vadc
, data
);
406 vadc_set_state(vadc
, false);
408 dev_err(vadc
->dev
, "conversion failed\n");
410 mutex_unlock(&vadc
->lock
);
414 static int vadc_measure_ref_points(struct vadc_priv
*vadc
)
416 struct vadc_channel_prop
*prop
;
420 vadc
->graph
[VADC_CALIB_RATIOMETRIC
].dx
= VADC_RATIOMETRIC_RANGE_UV
;
421 vadc
->graph
[VADC_CALIB_ABSOLUTE
].dx
= VADC_ABSOLUTE_RANGE_UV
;
423 prop
= vadc_get_channel(vadc
, VADC_REF_1250MV
);
424 ret
= vadc_do_conversion(vadc
, prop
, &read_1
);
428 /* Try with buffered 625mV channel first */
429 prop
= vadc_get_channel(vadc
, VADC_SPARE1
);
431 prop
= vadc_get_channel(vadc
, VADC_REF_625MV
);
433 ret
= vadc_do_conversion(vadc
, prop
, &read_2
);
437 if (read_1
== read_2
) {
442 vadc
->graph
[VADC_CALIB_ABSOLUTE
].dy
= read_1
- read_2
;
443 vadc
->graph
[VADC_CALIB_ABSOLUTE
].gnd
= read_2
;
445 /* Ratiometric calibration */
446 prop
= vadc_get_channel(vadc
, VADC_VDD_VADC
);
447 ret
= vadc_do_conversion(vadc
, prop
, &read_1
);
451 prop
= vadc_get_channel(vadc
, VADC_GND_REF
);
452 ret
= vadc_do_conversion(vadc
, prop
, &read_2
);
456 if (read_1
== read_2
) {
461 vadc
->graph
[VADC_CALIB_RATIOMETRIC
].dy
= read_1
- read_2
;
462 vadc
->graph
[VADC_CALIB_RATIOMETRIC
].gnd
= read_2
;
465 dev_err(vadc
->dev
, "measure reference points failed\n");
470 static s32
vadc_calibrate(struct vadc_priv
*vadc
,
471 const struct vadc_channel_prop
*prop
, u16 adc_code
)
473 const struct vadc_prescale_ratio
*prescale
;
476 voltage
= adc_code
- vadc
->graph
[prop
->calibration
].gnd
;
477 voltage
*= vadc
->graph
[prop
->calibration
].dx
;
478 voltage
= voltage
/ vadc
->graph
[prop
->calibration
].dy
;
480 if (prop
->calibration
== VADC_CALIB_ABSOLUTE
)
481 voltage
+= vadc
->graph
[prop
->calibration
].dx
;
486 prescale
= &vadc_prescale_ratios
[prop
->prescale
];
488 voltage
= voltage
* prescale
->den
;
490 return voltage
/ prescale
->num
;
493 static int vadc_decimation_from_dt(u32 value
)
495 if (!is_power_of_2(value
) || value
< VADC_DECIMATION_MIN
||
496 value
> VADC_DECIMATION_MAX
)
499 return __ffs64(value
/ VADC_DECIMATION_MIN
);
502 static int vadc_prescaling_from_dt(u32 num
, u32 den
)
506 for (pre
= 0; pre
< ARRAY_SIZE(vadc_prescale_ratios
); pre
++)
507 if (vadc_prescale_ratios
[pre
].num
== num
&&
508 vadc_prescale_ratios
[pre
].den
== den
)
511 if (pre
== ARRAY_SIZE(vadc_prescale_ratios
))
517 static int vadc_hw_settle_time_from_dt(u32 value
)
519 if ((value
<= 1000 && value
% 100) || (value
> 1000 && value
% 2000))
525 value
= value
/ 2000 + 10;
530 static int vadc_avg_samples_from_dt(u32 value
)
532 if (!is_power_of_2(value
) || value
> VADC_AVG_SAMPLES_MAX
)
535 return __ffs64(value
);
538 static int vadc_read_raw(struct iio_dev
*indio_dev
,
539 struct iio_chan_spec
const *chan
, int *val
, int *val2
,
542 struct vadc_priv
*vadc
= iio_priv(indio_dev
);
543 struct vadc_channel_prop
*prop
;
548 case IIO_CHAN_INFO_PROCESSED
:
549 prop
= &vadc
->chan_props
[chan
->address
];
550 ret
= vadc_do_conversion(vadc
, prop
, &adc_code
);
554 *val
= vadc_calibrate(vadc
, prop
, adc_code
);
556 /* 2mV/K, return milli Celsius */
558 *val
-= KELVINMIL_CELSIUSMIL
;
560 case IIO_CHAN_INFO_RAW
:
561 prop
= &vadc
->chan_props
[chan
->address
];
562 ret
= vadc_do_conversion(vadc
, prop
, &adc_code
);
566 *val
= vadc_calibrate(vadc
, prop
, adc_code
);
568 case IIO_CHAN_INFO_SCALE
:
571 return IIO_VAL_INT_PLUS_MICRO
;
580 static int vadc_of_xlate(struct iio_dev
*indio_dev
,
581 const struct of_phandle_args
*iiospec
)
583 struct vadc_priv
*vadc
= iio_priv(indio_dev
);
586 for (i
= 0; i
< vadc
->nchannels
; i
++)
587 if (vadc
->iio_chans
[i
].channel
== iiospec
->args
[0])
593 static const struct iio_info vadc_info
= {
594 .read_raw
= vadc_read_raw
,
595 .of_xlate
= vadc_of_xlate
,
596 .driver_module
= THIS_MODULE
,
599 struct vadc_channels
{
600 const char *datasheet_name
;
601 unsigned int prescale_index
;
602 enum iio_chan_type type
;
606 #define VADC_CHAN(_dname, _type, _mask, _pre) \
607 [VADC_##_dname] = { \
608 .datasheet_name = __stringify(_dname), \
609 .prescale_index = _pre, \
614 #define VADC_CHAN_TEMP(_dname, _pre) \
615 VADC_CHAN(_dname, IIO_TEMP, BIT(IIO_CHAN_INFO_PROCESSED), _pre) \
617 #define VADC_CHAN_VOLT(_dname, _pre) \
618 VADC_CHAN(_dname, IIO_VOLTAGE, \
619 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), \
623 * The array represents all possible ADC channels found in the supported PMICs.
624 * Every index in the array is equal to the channel number per datasheet. The
625 * gaps in the array should be treated as reserved channels.
627 static const struct vadc_channels vadc_chans
[] = {
628 VADC_CHAN_VOLT(USBIN
, 4)
629 VADC_CHAN_VOLT(DCIN
, 4)
630 VADC_CHAN_VOLT(VCHG_SNS
, 3)
631 VADC_CHAN_VOLT(SPARE1_03
, 1)
632 VADC_CHAN_VOLT(USB_ID_MV
, 1)
633 VADC_CHAN_VOLT(VCOIN
, 1)
634 VADC_CHAN_VOLT(VBAT_SNS
, 1)
635 VADC_CHAN_VOLT(VSYS
, 1)
636 VADC_CHAN_TEMP(DIE_TEMP
, 0)
637 VADC_CHAN_VOLT(REF_625MV
, 0)
638 VADC_CHAN_VOLT(REF_1250MV
, 0)
639 VADC_CHAN_VOLT(CHG_TEMP
, 0)
640 VADC_CHAN_VOLT(SPARE1
, 0)
641 VADC_CHAN_VOLT(SPARE2
, 0)
642 VADC_CHAN_VOLT(GND_REF
, 0)
643 VADC_CHAN_VOLT(VDD_VADC
, 0)
645 VADC_CHAN_VOLT(P_MUX1_1_1
, 0)
646 VADC_CHAN_VOLT(P_MUX2_1_1
, 0)
647 VADC_CHAN_VOLT(P_MUX3_1_1
, 0)
648 VADC_CHAN_VOLT(P_MUX4_1_1
, 0)
649 VADC_CHAN_VOLT(P_MUX5_1_1
, 0)
650 VADC_CHAN_VOLT(P_MUX6_1_1
, 0)
651 VADC_CHAN_VOLT(P_MUX7_1_1
, 0)
652 VADC_CHAN_VOLT(P_MUX8_1_1
, 0)
653 VADC_CHAN_VOLT(P_MUX9_1_1
, 0)
654 VADC_CHAN_VOLT(P_MUX10_1_1
, 0)
655 VADC_CHAN_VOLT(P_MUX11_1_1
, 0)
656 VADC_CHAN_VOLT(P_MUX12_1_1
, 0)
657 VADC_CHAN_VOLT(P_MUX13_1_1
, 0)
658 VADC_CHAN_VOLT(P_MUX14_1_1
, 0)
659 VADC_CHAN_VOLT(P_MUX15_1_1
, 0)
660 VADC_CHAN_VOLT(P_MUX16_1_1
, 0)
662 VADC_CHAN_VOLT(P_MUX1_1_3
, 1)
663 VADC_CHAN_VOLT(P_MUX2_1_3
, 1)
664 VADC_CHAN_VOLT(P_MUX3_1_3
, 1)
665 VADC_CHAN_VOLT(P_MUX4_1_3
, 1)
666 VADC_CHAN_VOLT(P_MUX5_1_3
, 1)
667 VADC_CHAN_VOLT(P_MUX6_1_3
, 1)
668 VADC_CHAN_VOLT(P_MUX7_1_3
, 1)
669 VADC_CHAN_VOLT(P_MUX8_1_3
, 1)
670 VADC_CHAN_VOLT(P_MUX9_1_3
, 1)
671 VADC_CHAN_VOLT(P_MUX10_1_3
, 1)
672 VADC_CHAN_VOLT(P_MUX11_1_3
, 1)
673 VADC_CHAN_VOLT(P_MUX12_1_3
, 1)
674 VADC_CHAN_VOLT(P_MUX13_1_3
, 1)
675 VADC_CHAN_VOLT(P_MUX14_1_3
, 1)
676 VADC_CHAN_VOLT(P_MUX15_1_3
, 1)
677 VADC_CHAN_VOLT(P_MUX16_1_3
, 1)
679 VADC_CHAN_VOLT(LR_MUX1_BAT_THERM
, 0)
680 VADC_CHAN_VOLT(LR_MUX2_BAT_ID
, 0)
681 VADC_CHAN_VOLT(LR_MUX3_XO_THERM
, 0)
682 VADC_CHAN_VOLT(LR_MUX4_AMUX_THM1
, 0)
683 VADC_CHAN_VOLT(LR_MUX5_AMUX_THM2
, 0)
684 VADC_CHAN_VOLT(LR_MUX6_AMUX_THM3
, 0)
685 VADC_CHAN_VOLT(LR_MUX7_HW_ID
, 0)
686 VADC_CHAN_VOLT(LR_MUX8_AMUX_THM4
, 0)
687 VADC_CHAN_VOLT(LR_MUX9_AMUX_THM5
, 0)
688 VADC_CHAN_VOLT(LR_MUX10_USB_ID
, 0)
689 VADC_CHAN_VOLT(AMUX_PU1
, 0)
690 VADC_CHAN_VOLT(AMUX_PU2
, 0)
691 VADC_CHAN_VOLT(LR_MUX3_BUF_XO_THERM
, 0)
693 VADC_CHAN_VOLT(LR_MUX1_PU1_BAT_THERM
, 0)
694 VADC_CHAN_VOLT(LR_MUX2_PU1_BAT_ID
, 0)
695 VADC_CHAN_VOLT(LR_MUX3_PU1_XO_THERM
, 0)
696 VADC_CHAN_VOLT(LR_MUX4_PU1_AMUX_THM1
, 0)
697 VADC_CHAN_VOLT(LR_MUX5_PU1_AMUX_THM2
, 0)
698 VADC_CHAN_VOLT(LR_MUX6_PU1_AMUX_THM3
, 0)
699 VADC_CHAN_VOLT(LR_MUX7_PU1_AMUX_HW_ID
, 0)
700 VADC_CHAN_VOLT(LR_MUX8_PU1_AMUX_THM4
, 0)
701 VADC_CHAN_VOLT(LR_MUX9_PU1_AMUX_THM5
, 0)
702 VADC_CHAN_VOLT(LR_MUX10_PU1_AMUX_USB_ID
, 0)
703 VADC_CHAN_VOLT(LR_MUX3_BUF_PU1_XO_THERM
, 0)
705 VADC_CHAN_VOLT(LR_MUX1_PU2_BAT_THERM
, 0)
706 VADC_CHAN_VOLT(LR_MUX2_PU2_BAT_ID
, 0)
707 VADC_CHAN_VOLT(LR_MUX3_PU2_XO_THERM
, 0)
708 VADC_CHAN_VOLT(LR_MUX4_PU2_AMUX_THM1
, 0)
709 VADC_CHAN_VOLT(LR_MUX5_PU2_AMUX_THM2
, 0)
710 VADC_CHAN_VOLT(LR_MUX6_PU2_AMUX_THM3
, 0)
711 VADC_CHAN_VOLT(LR_MUX7_PU2_AMUX_HW_ID
, 0)
712 VADC_CHAN_VOLT(LR_MUX8_PU2_AMUX_THM4
, 0)
713 VADC_CHAN_VOLT(LR_MUX9_PU2_AMUX_THM5
, 0)
714 VADC_CHAN_VOLT(LR_MUX10_PU2_AMUX_USB_ID
, 0)
715 VADC_CHAN_VOLT(LR_MUX3_BUF_PU2_XO_THERM
, 0)
717 VADC_CHAN_VOLT(LR_MUX1_PU1_PU2_BAT_THERM
, 0)
718 VADC_CHAN_VOLT(LR_MUX2_PU1_PU2_BAT_ID
, 0)
719 VADC_CHAN_VOLT(LR_MUX3_PU1_PU2_XO_THERM
, 0)
720 VADC_CHAN_VOLT(LR_MUX4_PU1_PU2_AMUX_THM1
, 0)
721 VADC_CHAN_VOLT(LR_MUX5_PU1_PU2_AMUX_THM2
, 0)
722 VADC_CHAN_VOLT(LR_MUX6_PU1_PU2_AMUX_THM3
, 0)
723 VADC_CHAN_VOLT(LR_MUX7_PU1_PU2_AMUX_HW_ID
, 0)
724 VADC_CHAN_VOLT(LR_MUX8_PU1_PU2_AMUX_THM4
, 0)
725 VADC_CHAN_VOLT(LR_MUX9_PU1_PU2_AMUX_THM5
, 0)
726 VADC_CHAN_VOLT(LR_MUX10_PU1_PU2_AMUX_USB_ID
, 0)
727 VADC_CHAN_VOLT(LR_MUX3_BUF_PU1_PU2_XO_THERM
, 0)
730 static int vadc_get_dt_channel_data(struct device
*dev
,
731 struct vadc_channel_prop
*prop
,
732 struct device_node
*node
)
734 const char *name
= node
->name
;
735 u32 chan
, value
, varr
[2];
738 ret
= of_property_read_u32(node
, "reg", &chan
);
740 dev_err(dev
, "invalid channel number %s\n", name
);
744 if (chan
> VADC_CHAN_MAX
|| chan
< VADC_CHAN_MIN
) {
745 dev_err(dev
, "%s invalid channel number %d\n", name
, chan
);
749 /* the channel has DT description */
750 prop
->channel
= chan
;
752 ret
= of_property_read_u32(node
, "qcom,decimation", &value
);
754 ret
= vadc_decimation_from_dt(value
);
756 dev_err(dev
, "%02x invalid decimation %d\n",
760 prop
->decimation
= ret
;
762 prop
->decimation
= VADC_DEF_DECIMATION
;
765 ret
= of_property_read_u32_array(node
, "qcom,pre-scaling", varr
, 2);
767 ret
= vadc_prescaling_from_dt(varr
[0], varr
[1]);
769 dev_err(dev
, "%02x invalid pre-scaling <%d %d>\n",
770 chan
, varr
[0], varr
[1]);
773 prop
->prescale
= ret
;
775 prop
->prescale
= vadc_chans
[prop
->channel
].prescale_index
;
778 ret
= of_property_read_u32(node
, "qcom,hw-settle-time", &value
);
780 ret
= vadc_hw_settle_time_from_dt(value
);
782 dev_err(dev
, "%02x invalid hw-settle-time %d us\n",
786 prop
->hw_settle_time
= ret
;
788 prop
->hw_settle_time
= VADC_DEF_HW_SETTLE_TIME
;
791 ret
= of_property_read_u32(node
, "qcom,avg-samples", &value
);
793 ret
= vadc_avg_samples_from_dt(value
);
795 dev_err(dev
, "%02x invalid avg-samples %d\n",
799 prop
->avg_samples
= ret
;
801 prop
->avg_samples
= VADC_DEF_AVG_SAMPLES
;
804 if (of_property_read_bool(node
, "qcom,ratiometric"))
805 prop
->calibration
= VADC_CALIB_RATIOMETRIC
;
807 prop
->calibration
= VADC_CALIB_ABSOLUTE
;
809 dev_dbg(dev
, "%02x name %s\n", chan
, name
);
814 static int vadc_get_dt_data(struct vadc_priv
*vadc
, struct device_node
*node
)
816 const struct vadc_channels
*vadc_chan
;
817 struct iio_chan_spec
*iio_chan
;
818 struct vadc_channel_prop prop
;
819 struct device_node
*child
;
820 unsigned int index
= 0;
823 vadc
->nchannels
= of_get_available_child_count(node
);
824 if (!vadc
->nchannels
)
827 vadc
->iio_chans
= devm_kcalloc(vadc
->dev
, vadc
->nchannels
,
828 sizeof(*vadc
->iio_chans
), GFP_KERNEL
);
829 if (!vadc
->iio_chans
)
832 vadc
->chan_props
= devm_kcalloc(vadc
->dev
, vadc
->nchannels
,
833 sizeof(*vadc
->chan_props
), GFP_KERNEL
);
834 if (!vadc
->chan_props
)
837 iio_chan
= vadc
->iio_chans
;
839 for_each_available_child_of_node(node
, child
) {
840 ret
= vadc_get_dt_channel_data(vadc
->dev
, &prop
, child
);
844 vadc
->chan_props
[index
] = prop
;
846 vadc_chan
= &vadc_chans
[prop
.channel
];
848 iio_chan
->channel
= prop
.channel
;
849 iio_chan
->datasheet_name
= vadc_chan
->datasheet_name
;
850 iio_chan
->info_mask_separate
= vadc_chan
->info_mask
;
851 iio_chan
->type
= vadc_chan
->type
;
852 iio_chan
->indexed
= 1;
853 iio_chan
->address
= index
++;
858 /* These channels are mandatory, they are used as reference points */
859 if (!vadc_get_channel(vadc
, VADC_REF_1250MV
)) {
860 dev_err(vadc
->dev
, "Please define 1.25V channel\n");
864 if (!vadc_get_channel(vadc
, VADC_REF_625MV
)) {
865 dev_err(vadc
->dev
, "Please define 0.625V channel\n");
869 if (!vadc_get_channel(vadc
, VADC_VDD_VADC
)) {
870 dev_err(vadc
->dev
, "Please define VDD channel\n");
874 if (!vadc_get_channel(vadc
, VADC_GND_REF
)) {
875 dev_err(vadc
->dev
, "Please define GND channel\n");
882 static irqreturn_t
vadc_isr(int irq
, void *dev_id
)
884 struct vadc_priv
*vadc
= dev_id
;
886 complete(&vadc
->complete
);
891 static int vadc_check_revision(struct vadc_priv
*vadc
)
896 ret
= vadc_read(vadc
, VADC_PERPH_TYPE
, &val
);
900 if (val
< VADC_PERPH_TYPE_ADC
) {
901 dev_err(vadc
->dev
, "%d is not ADC\n", val
);
905 ret
= vadc_read(vadc
, VADC_PERPH_SUBTYPE
, &val
);
909 if (val
< VADC_PERPH_SUBTYPE_VADC
) {
910 dev_err(vadc
->dev
, "%d is not VADC\n", val
);
914 ret
= vadc_read(vadc
, VADC_REVISION2
, &val
);
918 if (val
< VADC_REVISION2_SUPPORTED_VADC
) {
919 dev_err(vadc
->dev
, "revision %d not supported\n", val
);
926 static int vadc_probe(struct platform_device
*pdev
)
928 struct device_node
*node
= pdev
->dev
.of_node
;
929 struct device
*dev
= &pdev
->dev
;
930 struct iio_dev
*indio_dev
;
931 struct vadc_priv
*vadc
;
932 struct regmap
*regmap
;
936 regmap
= dev_get_regmap(dev
->parent
, NULL
);
940 ret
= of_property_read_u32(node
, "reg", ®
);
944 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*vadc
));
948 vadc
= iio_priv(indio_dev
);
949 vadc
->regmap
= regmap
;
952 vadc
->are_ref_measured
= false;
953 init_completion(&vadc
->complete
);
954 mutex_init(&vadc
->lock
);
956 ret
= vadc_check_revision(vadc
);
960 ret
= vadc_get_dt_data(vadc
, node
);
964 irq_eoc
= platform_get_irq(pdev
, 0);
966 if (irq_eoc
== -EPROBE_DEFER
|| irq_eoc
== -EINVAL
)
968 vadc
->poll_eoc
= true;
970 ret
= devm_request_irq(dev
, irq_eoc
, vadc_isr
, 0,
976 ret
= vadc_reset(vadc
);
978 dev_err(dev
, "reset failed\n");
982 ret
= vadc_measure_ref_points(vadc
);
986 indio_dev
->dev
.parent
= dev
;
987 indio_dev
->dev
.of_node
= node
;
988 indio_dev
->name
= pdev
->name
;
989 indio_dev
->modes
= INDIO_DIRECT_MODE
;
990 indio_dev
->info
= &vadc_info
;
991 indio_dev
->channels
= vadc
->iio_chans
;
992 indio_dev
->num_channels
= vadc
->nchannels
;
994 return devm_iio_device_register(dev
, indio_dev
);
997 static const struct of_device_id vadc_match_table
[] = {
998 { .compatible
= "qcom,spmi-vadc" },
1001 MODULE_DEVICE_TABLE(of
, vadc_match_table
);
1003 static struct platform_driver vadc_driver
= {
1005 .name
= "qcom-spmi-vadc",
1006 .of_match_table
= vadc_match_table
,
1008 .probe
= vadc_probe
,
1010 module_platform_driver(vadc_driver
);
1012 MODULE_ALIAS("platform:qcom-spmi-vadc");
1013 MODULE_DESCRIPTION("Qualcomm SPMI PMIC voltage ADC driver");
1014 MODULE_LICENSE("GPL v2");
1015 MODULE_AUTHOR("Stanimir Varbanov <svarbanov@mm-sol.com>");
1016 MODULE_AUTHOR("Ivan T. Ivanov <iivanov@mm-sol.com>");