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 #include "qcom-vadc-common.h"
33 /* VADC register and bit definitions */
34 #define VADC_REVISION2 0x1
35 #define VADC_REVISION2_SUPPORTED_VADC 1
37 #define VADC_PERPH_TYPE 0x4
38 #define VADC_PERPH_TYPE_ADC 8
40 #define VADC_PERPH_SUBTYPE 0x5
41 #define VADC_PERPH_SUBTYPE_VADC 1
43 #define VADC_STATUS1 0x8
44 #define VADC_STATUS1_OP_MODE 4
45 #define VADC_STATUS1_REQ_STS BIT(1)
46 #define VADC_STATUS1_EOC BIT(0)
47 #define VADC_STATUS1_REQ_STS_EOC_MASK 0x3
49 #define VADC_MODE_CTL 0x40
50 #define VADC_OP_MODE_SHIFT 3
51 #define VADC_OP_MODE_NORMAL 0
52 #define VADC_AMUX_TRIM_EN BIT(1)
53 #define VADC_ADC_TRIM_EN BIT(0)
55 #define VADC_EN_CTL1 0x46
56 #define VADC_EN_CTL1_SET BIT(7)
58 #define VADC_ADC_CH_SEL_CTL 0x48
60 #define VADC_ADC_DIG_PARAM 0x50
61 #define VADC_ADC_DIG_DEC_RATIO_SEL_SHIFT 2
63 #define VADC_HW_SETTLE_DELAY 0x51
65 #define VADC_CONV_REQ 0x52
66 #define VADC_CONV_REQ_SET BIT(7)
68 #define VADC_FAST_AVG_CTL 0x5a
69 #define VADC_FAST_AVG_EN 0x5b
70 #define VADC_FAST_AVG_EN_SET BIT(7)
72 #define VADC_ACCESS 0xd0
73 #define VADC_ACCESS_DATA 0xa5
75 #define VADC_PERH_RESET_CTL3 0xda
76 #define VADC_FOLLOW_WARM_RB BIT(2)
78 #define VADC_DATA 0x60 /* 16 bits */
80 #define VADC_CHAN_MIN VADC_USBIN
81 #define VADC_CHAN_MAX VADC_LR_MUX3_BUF_PU1_PU2_XO_THERM
84 * struct vadc_channel_prop - VADC channel property.
85 * @channel: channel number, refer to the channel list.
86 * @calibration: calibration type.
87 * @decimation: sampling rate supported for the channel.
88 * @prescale: channel scaling performed on the input signal.
89 * @hw_settle_time: the time between AMUX being configured and the
90 * start of conversion.
91 * @avg_samples: ability to provide single result from the ADC
92 * that is an average of multiple measurements.
93 * @scale_fn_type: Represents the scaling function to convert voltage
94 * physical units desired by the client for the channel.
96 struct vadc_channel_prop
{
98 enum vadc_calibration calibration
;
99 unsigned int decimation
;
100 unsigned int prescale
;
101 unsigned int hw_settle_time
;
102 unsigned int avg_samples
;
103 enum vadc_scale_fn_type scale_fn_type
;
107 * struct vadc_priv - VADC private structure.
108 * @regmap: pointer to struct regmap.
109 * @dev: pointer to struct device.
110 * @base: base address for the ADC peripheral.
111 * @nchannels: number of VADC channels.
112 * @chan_props: array of VADC channel properties.
113 * @iio_chans: array of IIO channels specification.
114 * @are_ref_measured: are reference points measured.
115 * @poll_eoc: use polling instead of interrupt.
116 * @complete: VADC result notification after interrupt is received.
117 * @graph: store parameters for calibration.
118 * @lock: ADC lock for access to the peripheral.
121 struct regmap
*regmap
;
124 unsigned int nchannels
;
125 struct vadc_channel_prop
*chan_props
;
126 struct iio_chan_spec
*iio_chans
;
127 bool are_ref_measured
;
129 struct completion complete
;
130 struct vadc_linear_graph graph
[2];
134 static const struct vadc_prescale_ratio vadc_prescale_ratios
[] = {
135 {.num
= 1, .den
= 1},
136 {.num
= 1, .den
= 3},
137 {.num
= 1, .den
= 4},
138 {.num
= 1, .den
= 6},
139 {.num
= 1, .den
= 20},
140 {.num
= 1, .den
= 8},
141 {.num
= 10, .den
= 81},
142 {.num
= 1, .den
= 10}
145 static int vadc_read(struct vadc_priv
*vadc
, u16 offset
, u8
*data
)
147 return regmap_bulk_read(vadc
->regmap
, vadc
->base
+ offset
, data
, 1);
150 static int vadc_write(struct vadc_priv
*vadc
, u16 offset
, u8 data
)
152 return regmap_write(vadc
->regmap
, vadc
->base
+ offset
, data
);
155 static int vadc_reset(struct vadc_priv
*vadc
)
160 ret
= vadc_write(vadc
, VADC_ACCESS
, VADC_ACCESS_DATA
);
164 ret
= vadc_read(vadc
, VADC_PERH_RESET_CTL3
, &data
);
168 ret
= vadc_write(vadc
, VADC_ACCESS
, VADC_ACCESS_DATA
);
172 data
|= VADC_FOLLOW_WARM_RB
;
174 return vadc_write(vadc
, VADC_PERH_RESET_CTL3
, data
);
177 static int vadc_set_state(struct vadc_priv
*vadc
, bool state
)
179 return vadc_write(vadc
, VADC_EN_CTL1
, state
? VADC_EN_CTL1_SET
: 0);
182 static void vadc_show_status(struct vadc_priv
*vadc
)
184 u8 mode
, sta1
, chan
, dig
, en
, req
;
187 ret
= vadc_read(vadc
, VADC_MODE_CTL
, &mode
);
191 ret
= vadc_read(vadc
, VADC_ADC_DIG_PARAM
, &dig
);
195 ret
= vadc_read(vadc
, VADC_ADC_CH_SEL_CTL
, &chan
);
199 ret
= vadc_read(vadc
, VADC_CONV_REQ
, &req
);
203 ret
= vadc_read(vadc
, VADC_STATUS1
, &sta1
);
207 ret
= vadc_read(vadc
, VADC_EN_CTL1
, &en
);
212 "mode:%02x en:%02x chan:%02x dig:%02x req:%02x sta1:%02x\n",
213 mode
, en
, chan
, dig
, req
, sta1
);
216 static int vadc_configure(struct vadc_priv
*vadc
,
217 struct vadc_channel_prop
*prop
)
219 u8 decimation
, mode_ctrl
;
223 mode_ctrl
= (VADC_OP_MODE_NORMAL
<< VADC_OP_MODE_SHIFT
) |
224 VADC_ADC_TRIM_EN
| VADC_AMUX_TRIM_EN
;
225 ret
= vadc_write(vadc
, VADC_MODE_CTL
, mode_ctrl
);
229 /* Channel selection */
230 ret
= vadc_write(vadc
, VADC_ADC_CH_SEL_CTL
, prop
->channel
);
234 /* Digital parameter setup */
235 decimation
= prop
->decimation
<< VADC_ADC_DIG_DEC_RATIO_SEL_SHIFT
;
236 ret
= vadc_write(vadc
, VADC_ADC_DIG_PARAM
, decimation
);
240 /* HW settle time delay */
241 ret
= vadc_write(vadc
, VADC_HW_SETTLE_DELAY
, prop
->hw_settle_time
);
245 ret
= vadc_write(vadc
, VADC_FAST_AVG_CTL
, prop
->avg_samples
);
249 if (prop
->avg_samples
)
250 ret
= vadc_write(vadc
, VADC_FAST_AVG_EN
, VADC_FAST_AVG_EN_SET
);
252 ret
= vadc_write(vadc
, VADC_FAST_AVG_EN
, 0);
257 static int vadc_poll_wait_eoc(struct vadc_priv
*vadc
, unsigned int interval_us
)
259 unsigned int count
, retry
;
263 retry
= interval_us
/ VADC_CONV_TIME_MIN_US
;
265 for (count
= 0; count
< retry
; count
++) {
266 ret
= vadc_read(vadc
, VADC_STATUS1
, &sta1
);
270 sta1
&= VADC_STATUS1_REQ_STS_EOC_MASK
;
271 if (sta1
== VADC_STATUS1_EOC
)
274 usleep_range(VADC_CONV_TIME_MIN_US
, VADC_CONV_TIME_MAX_US
);
277 vadc_show_status(vadc
);
282 static int vadc_read_result(struct vadc_priv
*vadc
, u16
*data
)
286 ret
= regmap_bulk_read(vadc
->regmap
, vadc
->base
+ VADC_DATA
, data
, 2);
290 *data
= clamp_t(u16
, *data
, VADC_MIN_ADC_CODE
, VADC_MAX_ADC_CODE
);
295 static struct vadc_channel_prop
*vadc_get_channel(struct vadc_priv
*vadc
,
300 for (i
= 0; i
< vadc
->nchannels
; i
++)
301 if (vadc
->chan_props
[i
].channel
== num
)
302 return &vadc
->chan_props
[i
];
304 dev_dbg(vadc
->dev
, "no such channel %02x\n", num
);
309 static int vadc_do_conversion(struct vadc_priv
*vadc
,
310 struct vadc_channel_prop
*prop
, u16
*data
)
312 unsigned int timeout
;
315 mutex_lock(&vadc
->lock
);
317 ret
= vadc_configure(vadc
, prop
);
322 reinit_completion(&vadc
->complete
);
324 ret
= vadc_set_state(vadc
, true);
328 ret
= vadc_write(vadc
, VADC_CONV_REQ
, VADC_CONV_REQ_SET
);
332 timeout
= BIT(prop
->avg_samples
) * VADC_CONV_TIME_MIN_US
* 2;
334 if (vadc
->poll_eoc
) {
335 ret
= vadc_poll_wait_eoc(vadc
, timeout
);
337 ret
= wait_for_completion_timeout(&vadc
->complete
, timeout
);
343 /* Double check conversion status */
344 ret
= vadc_poll_wait_eoc(vadc
, VADC_CONV_TIME_MIN_US
);
349 ret
= vadc_read_result(vadc
, data
);
352 vadc_set_state(vadc
, false);
354 dev_err(vadc
->dev
, "conversion failed\n");
356 mutex_unlock(&vadc
->lock
);
360 static int vadc_measure_ref_points(struct vadc_priv
*vadc
)
362 struct vadc_channel_prop
*prop
;
366 vadc
->graph
[VADC_CALIB_RATIOMETRIC
].dx
= VADC_RATIOMETRIC_RANGE
;
367 vadc
->graph
[VADC_CALIB_ABSOLUTE
].dx
= VADC_ABSOLUTE_RANGE_UV
;
369 prop
= vadc_get_channel(vadc
, VADC_REF_1250MV
);
370 ret
= vadc_do_conversion(vadc
, prop
, &read_1
);
374 /* Try with buffered 625mV channel first */
375 prop
= vadc_get_channel(vadc
, VADC_SPARE1
);
377 prop
= vadc_get_channel(vadc
, VADC_REF_625MV
);
379 ret
= vadc_do_conversion(vadc
, prop
, &read_2
);
383 if (read_1
== read_2
) {
388 vadc
->graph
[VADC_CALIB_ABSOLUTE
].dy
= read_1
- read_2
;
389 vadc
->graph
[VADC_CALIB_ABSOLUTE
].gnd
= read_2
;
391 /* Ratiometric calibration */
392 prop
= vadc_get_channel(vadc
, VADC_VDD_VADC
);
393 ret
= vadc_do_conversion(vadc
, prop
, &read_1
);
397 prop
= vadc_get_channel(vadc
, VADC_GND_REF
);
398 ret
= vadc_do_conversion(vadc
, prop
, &read_2
);
402 if (read_1
== read_2
) {
407 vadc
->graph
[VADC_CALIB_RATIOMETRIC
].dy
= read_1
- read_2
;
408 vadc
->graph
[VADC_CALIB_RATIOMETRIC
].gnd
= read_2
;
411 dev_err(vadc
->dev
, "measure reference points failed\n");
416 static int vadc_prescaling_from_dt(u32 num
, u32 den
)
420 for (pre
= 0; pre
< ARRAY_SIZE(vadc_prescale_ratios
); pre
++)
421 if (vadc_prescale_ratios
[pre
].num
== num
&&
422 vadc_prescale_ratios
[pre
].den
== den
)
425 if (pre
== ARRAY_SIZE(vadc_prescale_ratios
))
431 static int vadc_hw_settle_time_from_dt(u32 value
)
433 if ((value
<= 1000 && value
% 100) || (value
> 1000 && value
% 2000))
439 value
= value
/ 2000 + 10;
444 static int vadc_avg_samples_from_dt(u32 value
)
446 if (!is_power_of_2(value
) || value
> VADC_AVG_SAMPLES_MAX
)
449 return __ffs64(value
);
452 static int vadc_read_raw(struct iio_dev
*indio_dev
,
453 struct iio_chan_spec
const *chan
, int *val
, int *val2
,
456 struct vadc_priv
*vadc
= iio_priv(indio_dev
);
457 struct vadc_channel_prop
*prop
;
462 case IIO_CHAN_INFO_PROCESSED
:
463 prop
= &vadc
->chan_props
[chan
->address
];
464 ret
= vadc_do_conversion(vadc
, prop
, &adc_code
);
468 ret
= qcom_vadc_scale(prop
->scale_fn_type
,
469 &vadc
->graph
[prop
->calibration
],
470 &vadc_prescale_ratios
[prop
->prescale
],
471 (prop
->calibration
== VADC_CALIB_ABSOLUTE
),
477 case IIO_CHAN_INFO_RAW
:
478 prop
= &vadc
->chan_props
[chan
->address
];
479 ret
= vadc_do_conversion(vadc
, prop
, &adc_code
);
483 *val
= (int)adc_code
;
493 static int vadc_of_xlate(struct iio_dev
*indio_dev
,
494 const struct of_phandle_args
*iiospec
)
496 struct vadc_priv
*vadc
= iio_priv(indio_dev
);
499 for (i
= 0; i
< vadc
->nchannels
; i
++)
500 if (vadc
->iio_chans
[i
].channel
== iiospec
->args
[0])
506 static const struct iio_info vadc_info
= {
507 .read_raw
= vadc_read_raw
,
508 .of_xlate
= vadc_of_xlate
,
511 struct vadc_channels
{
512 const char *datasheet_name
;
513 unsigned int prescale_index
;
514 enum iio_chan_type type
;
516 enum vadc_scale_fn_type scale_fn_type
;
519 #define VADC_CHAN(_dname, _type, _mask, _pre, _scale) \
520 [VADC_##_dname] = { \
521 .datasheet_name = __stringify(_dname), \
522 .prescale_index = _pre, \
524 .info_mask = _mask, \
525 .scale_fn_type = _scale \
528 #define VADC_NO_CHAN(_dname, _type, _mask, _pre) \
529 [VADC_##_dname] = { \
530 .datasheet_name = __stringify(_dname), \
531 .prescale_index = _pre, \
536 #define VADC_CHAN_TEMP(_dname, _pre, _scale) \
537 VADC_CHAN(_dname, IIO_TEMP, \
538 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_PROCESSED), \
541 #define VADC_CHAN_VOLT(_dname, _pre, _scale) \
542 VADC_CHAN(_dname, IIO_VOLTAGE, \
543 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_PROCESSED),\
546 #define VADC_CHAN_NO_SCALE(_dname, _pre) \
547 VADC_NO_CHAN(_dname, IIO_VOLTAGE, \
548 BIT(IIO_CHAN_INFO_RAW), \
552 * The array represents all possible ADC channels found in the supported PMICs.
553 * Every index in the array is equal to the channel number per datasheet. The
554 * gaps in the array should be treated as reserved channels.
556 static const struct vadc_channels vadc_chans
[] = {
557 VADC_CHAN_VOLT(USBIN
, 4, SCALE_DEFAULT
)
558 VADC_CHAN_VOLT(DCIN
, 4, SCALE_DEFAULT
)
559 VADC_CHAN_NO_SCALE(VCHG_SNS
, 3)
560 VADC_CHAN_NO_SCALE(SPARE1_03
, 1)
561 VADC_CHAN_NO_SCALE(USB_ID_MV
, 1)
562 VADC_CHAN_VOLT(VCOIN
, 1, SCALE_DEFAULT
)
563 VADC_CHAN_NO_SCALE(VBAT_SNS
, 1)
564 VADC_CHAN_VOLT(VSYS
, 1, SCALE_DEFAULT
)
565 VADC_CHAN_TEMP(DIE_TEMP
, 0, SCALE_PMIC_THERM
)
566 VADC_CHAN_VOLT(REF_625MV
, 0, SCALE_DEFAULT
)
567 VADC_CHAN_VOLT(REF_1250MV
, 0, SCALE_DEFAULT
)
568 VADC_CHAN_NO_SCALE(CHG_TEMP
, 0)
569 VADC_CHAN_NO_SCALE(SPARE1
, 0)
570 VADC_CHAN_TEMP(SPARE2
, 0, SCALE_PMI_CHG_TEMP
)
571 VADC_CHAN_VOLT(GND_REF
, 0, SCALE_DEFAULT
)
572 VADC_CHAN_VOLT(VDD_VADC
, 0, SCALE_DEFAULT
)
574 VADC_CHAN_NO_SCALE(P_MUX1_1_1
, 0)
575 VADC_CHAN_NO_SCALE(P_MUX2_1_1
, 0)
576 VADC_CHAN_NO_SCALE(P_MUX3_1_1
, 0)
577 VADC_CHAN_NO_SCALE(P_MUX4_1_1
, 0)
578 VADC_CHAN_NO_SCALE(P_MUX5_1_1
, 0)
579 VADC_CHAN_NO_SCALE(P_MUX6_1_1
, 0)
580 VADC_CHAN_NO_SCALE(P_MUX7_1_1
, 0)
581 VADC_CHAN_NO_SCALE(P_MUX8_1_1
, 0)
582 VADC_CHAN_NO_SCALE(P_MUX9_1_1
, 0)
583 VADC_CHAN_NO_SCALE(P_MUX10_1_1
, 0)
584 VADC_CHAN_NO_SCALE(P_MUX11_1_1
, 0)
585 VADC_CHAN_NO_SCALE(P_MUX12_1_1
, 0)
586 VADC_CHAN_NO_SCALE(P_MUX13_1_1
, 0)
587 VADC_CHAN_NO_SCALE(P_MUX14_1_1
, 0)
588 VADC_CHAN_NO_SCALE(P_MUX15_1_1
, 0)
589 VADC_CHAN_NO_SCALE(P_MUX16_1_1
, 0)
591 VADC_CHAN_NO_SCALE(P_MUX1_1_3
, 1)
592 VADC_CHAN_NO_SCALE(P_MUX2_1_3
, 1)
593 VADC_CHAN_NO_SCALE(P_MUX3_1_3
, 1)
594 VADC_CHAN_NO_SCALE(P_MUX4_1_3
, 1)
595 VADC_CHAN_NO_SCALE(P_MUX5_1_3
, 1)
596 VADC_CHAN_NO_SCALE(P_MUX6_1_3
, 1)
597 VADC_CHAN_NO_SCALE(P_MUX7_1_3
, 1)
598 VADC_CHAN_NO_SCALE(P_MUX8_1_3
, 1)
599 VADC_CHAN_NO_SCALE(P_MUX9_1_3
, 1)
600 VADC_CHAN_NO_SCALE(P_MUX10_1_3
, 1)
601 VADC_CHAN_NO_SCALE(P_MUX11_1_3
, 1)
602 VADC_CHAN_NO_SCALE(P_MUX12_1_3
, 1)
603 VADC_CHAN_NO_SCALE(P_MUX13_1_3
, 1)
604 VADC_CHAN_NO_SCALE(P_MUX14_1_3
, 1)
605 VADC_CHAN_NO_SCALE(P_MUX15_1_3
, 1)
606 VADC_CHAN_NO_SCALE(P_MUX16_1_3
, 1)
608 VADC_CHAN_NO_SCALE(LR_MUX1_BAT_THERM
, 0)
609 VADC_CHAN_NO_SCALE(LR_MUX2_BAT_ID
, 0)
610 VADC_CHAN_NO_SCALE(LR_MUX3_XO_THERM
, 0)
611 VADC_CHAN_NO_SCALE(LR_MUX4_AMUX_THM1
, 0)
612 VADC_CHAN_NO_SCALE(LR_MUX5_AMUX_THM2
, 0)
613 VADC_CHAN_NO_SCALE(LR_MUX6_AMUX_THM3
, 0)
614 VADC_CHAN_NO_SCALE(LR_MUX7_HW_ID
, 0)
615 VADC_CHAN_NO_SCALE(LR_MUX8_AMUX_THM4
, 0)
616 VADC_CHAN_NO_SCALE(LR_MUX9_AMUX_THM5
, 0)
617 VADC_CHAN_NO_SCALE(LR_MUX10_USB_ID
, 0)
618 VADC_CHAN_NO_SCALE(AMUX_PU1
, 0)
619 VADC_CHAN_NO_SCALE(AMUX_PU2
, 0)
620 VADC_CHAN_NO_SCALE(LR_MUX3_BUF_XO_THERM
, 0)
622 VADC_CHAN_NO_SCALE(LR_MUX1_PU1_BAT_THERM
, 0)
623 VADC_CHAN_NO_SCALE(LR_MUX2_PU1_BAT_ID
, 0)
624 VADC_CHAN_NO_SCALE(LR_MUX3_PU1_XO_THERM
, 0)
625 VADC_CHAN_TEMP(LR_MUX4_PU1_AMUX_THM1
, 0, SCALE_THERM_100K_PULLUP
)
626 VADC_CHAN_TEMP(LR_MUX5_PU1_AMUX_THM2
, 0, SCALE_THERM_100K_PULLUP
)
627 VADC_CHAN_TEMP(LR_MUX6_PU1_AMUX_THM3
, 0, SCALE_THERM_100K_PULLUP
)
628 VADC_CHAN_NO_SCALE(LR_MUX7_PU1_AMUX_HW_ID
, 0)
629 VADC_CHAN_TEMP(LR_MUX8_PU1_AMUX_THM4
, 0, SCALE_THERM_100K_PULLUP
)
630 VADC_CHAN_TEMP(LR_MUX9_PU1_AMUX_THM5
, 0, SCALE_THERM_100K_PULLUP
)
631 VADC_CHAN_NO_SCALE(LR_MUX10_PU1_AMUX_USB_ID
, 0)
632 VADC_CHAN_TEMP(LR_MUX3_BUF_PU1_XO_THERM
, 0, SCALE_XOTHERM
)
634 VADC_CHAN_NO_SCALE(LR_MUX1_PU2_BAT_THERM
, 0)
635 VADC_CHAN_NO_SCALE(LR_MUX2_PU2_BAT_ID
, 0)
636 VADC_CHAN_NO_SCALE(LR_MUX3_PU2_XO_THERM
, 0)
637 VADC_CHAN_NO_SCALE(LR_MUX4_PU2_AMUX_THM1
, 0)
638 VADC_CHAN_NO_SCALE(LR_MUX5_PU2_AMUX_THM2
, 0)
639 VADC_CHAN_NO_SCALE(LR_MUX6_PU2_AMUX_THM3
, 0)
640 VADC_CHAN_NO_SCALE(LR_MUX7_PU2_AMUX_HW_ID
, 0)
641 VADC_CHAN_NO_SCALE(LR_MUX8_PU2_AMUX_THM4
, 0)
642 VADC_CHAN_NO_SCALE(LR_MUX9_PU2_AMUX_THM5
, 0)
643 VADC_CHAN_NO_SCALE(LR_MUX10_PU2_AMUX_USB_ID
, 0)
644 VADC_CHAN_NO_SCALE(LR_MUX3_BUF_PU2_XO_THERM
, 0)
646 VADC_CHAN_NO_SCALE(LR_MUX1_PU1_PU2_BAT_THERM
, 0)
647 VADC_CHAN_NO_SCALE(LR_MUX2_PU1_PU2_BAT_ID
, 0)
648 VADC_CHAN_NO_SCALE(LR_MUX3_PU1_PU2_XO_THERM
, 0)
649 VADC_CHAN_NO_SCALE(LR_MUX4_PU1_PU2_AMUX_THM1
, 0)
650 VADC_CHAN_NO_SCALE(LR_MUX5_PU1_PU2_AMUX_THM2
, 0)
651 VADC_CHAN_NO_SCALE(LR_MUX6_PU1_PU2_AMUX_THM3
, 0)
652 VADC_CHAN_NO_SCALE(LR_MUX7_PU1_PU2_AMUX_HW_ID
, 0)
653 VADC_CHAN_NO_SCALE(LR_MUX8_PU1_PU2_AMUX_THM4
, 0)
654 VADC_CHAN_NO_SCALE(LR_MUX9_PU1_PU2_AMUX_THM5
, 0)
655 VADC_CHAN_NO_SCALE(LR_MUX10_PU1_PU2_AMUX_USB_ID
, 0)
656 VADC_CHAN_NO_SCALE(LR_MUX3_BUF_PU1_PU2_XO_THERM
, 0)
659 static int vadc_get_dt_channel_data(struct device
*dev
,
660 struct vadc_channel_prop
*prop
,
661 struct device_node
*node
)
663 const char *name
= node
->name
;
664 u32 chan
, value
, varr
[2];
667 ret
= of_property_read_u32(node
, "reg", &chan
);
669 dev_err(dev
, "invalid channel number %s\n", name
);
673 if (chan
> VADC_CHAN_MAX
|| chan
< VADC_CHAN_MIN
) {
674 dev_err(dev
, "%s invalid channel number %d\n", name
, chan
);
678 /* the channel has DT description */
679 prop
->channel
= chan
;
681 ret
= of_property_read_u32(node
, "qcom,decimation", &value
);
683 ret
= qcom_vadc_decimation_from_dt(value
);
685 dev_err(dev
, "%02x invalid decimation %d\n",
689 prop
->decimation
= ret
;
691 prop
->decimation
= VADC_DEF_DECIMATION
;
694 ret
= of_property_read_u32_array(node
, "qcom,pre-scaling", varr
, 2);
696 ret
= vadc_prescaling_from_dt(varr
[0], varr
[1]);
698 dev_err(dev
, "%02x invalid pre-scaling <%d %d>\n",
699 chan
, varr
[0], varr
[1]);
702 prop
->prescale
= ret
;
704 prop
->prescale
= vadc_chans
[prop
->channel
].prescale_index
;
707 ret
= of_property_read_u32(node
, "qcom,hw-settle-time", &value
);
709 ret
= vadc_hw_settle_time_from_dt(value
);
711 dev_err(dev
, "%02x invalid hw-settle-time %d us\n",
715 prop
->hw_settle_time
= ret
;
717 prop
->hw_settle_time
= VADC_DEF_HW_SETTLE_TIME
;
720 ret
= of_property_read_u32(node
, "qcom,avg-samples", &value
);
722 ret
= vadc_avg_samples_from_dt(value
);
724 dev_err(dev
, "%02x invalid avg-samples %d\n",
728 prop
->avg_samples
= ret
;
730 prop
->avg_samples
= VADC_DEF_AVG_SAMPLES
;
733 if (of_property_read_bool(node
, "qcom,ratiometric"))
734 prop
->calibration
= VADC_CALIB_RATIOMETRIC
;
736 prop
->calibration
= VADC_CALIB_ABSOLUTE
;
738 dev_dbg(dev
, "%02x name %s\n", chan
, name
);
743 static int vadc_get_dt_data(struct vadc_priv
*vadc
, struct device_node
*node
)
745 const struct vadc_channels
*vadc_chan
;
746 struct iio_chan_spec
*iio_chan
;
747 struct vadc_channel_prop prop
;
748 struct device_node
*child
;
749 unsigned int index
= 0;
752 vadc
->nchannels
= of_get_available_child_count(node
);
753 if (!vadc
->nchannels
)
756 vadc
->iio_chans
= devm_kcalloc(vadc
->dev
, vadc
->nchannels
,
757 sizeof(*vadc
->iio_chans
), GFP_KERNEL
);
758 if (!vadc
->iio_chans
)
761 vadc
->chan_props
= devm_kcalloc(vadc
->dev
, vadc
->nchannels
,
762 sizeof(*vadc
->chan_props
), GFP_KERNEL
);
763 if (!vadc
->chan_props
)
766 iio_chan
= vadc
->iio_chans
;
768 for_each_available_child_of_node(node
, child
) {
769 ret
= vadc_get_dt_channel_data(vadc
->dev
, &prop
, child
);
775 prop
.scale_fn_type
= vadc_chans
[prop
.channel
].scale_fn_type
;
776 vadc
->chan_props
[index
] = prop
;
778 vadc_chan
= &vadc_chans
[prop
.channel
];
780 iio_chan
->channel
= prop
.channel
;
781 iio_chan
->datasheet_name
= vadc_chan
->datasheet_name
;
782 iio_chan
->info_mask_separate
= vadc_chan
->info_mask
;
783 iio_chan
->type
= vadc_chan
->type
;
784 iio_chan
->indexed
= 1;
785 iio_chan
->address
= index
++;
790 /* These channels are mandatory, they are used as reference points */
791 if (!vadc_get_channel(vadc
, VADC_REF_1250MV
)) {
792 dev_err(vadc
->dev
, "Please define 1.25V channel\n");
796 if (!vadc_get_channel(vadc
, VADC_REF_625MV
)) {
797 dev_err(vadc
->dev
, "Please define 0.625V channel\n");
801 if (!vadc_get_channel(vadc
, VADC_VDD_VADC
)) {
802 dev_err(vadc
->dev
, "Please define VDD channel\n");
806 if (!vadc_get_channel(vadc
, VADC_GND_REF
)) {
807 dev_err(vadc
->dev
, "Please define GND channel\n");
814 static irqreturn_t
vadc_isr(int irq
, void *dev_id
)
816 struct vadc_priv
*vadc
= dev_id
;
818 complete(&vadc
->complete
);
823 static int vadc_check_revision(struct vadc_priv
*vadc
)
828 ret
= vadc_read(vadc
, VADC_PERPH_TYPE
, &val
);
832 if (val
< VADC_PERPH_TYPE_ADC
) {
833 dev_err(vadc
->dev
, "%d is not ADC\n", val
);
837 ret
= vadc_read(vadc
, VADC_PERPH_SUBTYPE
, &val
);
841 if (val
< VADC_PERPH_SUBTYPE_VADC
) {
842 dev_err(vadc
->dev
, "%d is not VADC\n", val
);
846 ret
= vadc_read(vadc
, VADC_REVISION2
, &val
);
850 if (val
< VADC_REVISION2_SUPPORTED_VADC
) {
851 dev_err(vadc
->dev
, "revision %d not supported\n", val
);
858 static int vadc_probe(struct platform_device
*pdev
)
860 struct device_node
*node
= pdev
->dev
.of_node
;
861 struct device
*dev
= &pdev
->dev
;
862 struct iio_dev
*indio_dev
;
863 struct vadc_priv
*vadc
;
864 struct regmap
*regmap
;
868 regmap
= dev_get_regmap(dev
->parent
, NULL
);
872 ret
= of_property_read_u32(node
, "reg", ®
);
876 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*vadc
));
880 vadc
= iio_priv(indio_dev
);
881 vadc
->regmap
= regmap
;
884 vadc
->are_ref_measured
= false;
885 init_completion(&vadc
->complete
);
886 mutex_init(&vadc
->lock
);
888 ret
= vadc_check_revision(vadc
);
892 ret
= vadc_get_dt_data(vadc
, node
);
896 irq_eoc
= platform_get_irq(pdev
, 0);
898 if (irq_eoc
== -EPROBE_DEFER
|| irq_eoc
== -EINVAL
)
900 vadc
->poll_eoc
= true;
902 ret
= devm_request_irq(dev
, irq_eoc
, vadc_isr
, 0,
908 ret
= vadc_reset(vadc
);
910 dev_err(dev
, "reset failed\n");
914 ret
= vadc_measure_ref_points(vadc
);
918 indio_dev
->dev
.parent
= dev
;
919 indio_dev
->dev
.of_node
= node
;
920 indio_dev
->name
= pdev
->name
;
921 indio_dev
->modes
= INDIO_DIRECT_MODE
;
922 indio_dev
->info
= &vadc_info
;
923 indio_dev
->channels
= vadc
->iio_chans
;
924 indio_dev
->num_channels
= vadc
->nchannels
;
926 return devm_iio_device_register(dev
, indio_dev
);
929 static const struct of_device_id vadc_match_table
[] = {
930 { .compatible
= "qcom,spmi-vadc" },
933 MODULE_DEVICE_TABLE(of
, vadc_match_table
);
935 static struct platform_driver vadc_driver
= {
937 .name
= "qcom-spmi-vadc",
938 .of_match_table
= vadc_match_table
,
942 module_platform_driver(vadc_driver
);
944 MODULE_ALIAS("platform:qcom-spmi-vadc");
945 MODULE_DESCRIPTION("Qualcomm SPMI PMIC voltage ADC driver");
946 MODULE_LICENSE("GPL v2");
947 MODULE_AUTHOR("Stanimir Varbanov <svarbanov@mm-sol.com>");
948 MODULE_AUTHOR("Ivan T. Ivanov <iivanov@mm-sol.com>");