1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2018, 2020, 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/log2.h>
14 #include <linux/math64.h>
15 #include <linux/module.h>
17 #include <linux/platform_device.h>
18 #include <linux/regmap.h>
19 #include <linux/slab.h>
21 #include <dt-bindings/iio/qcom,spmi-vadc.h>
22 #include "qcom-vadc-common.h"
24 #define ADC5_USR_REVISION1 0x0
25 #define ADC5_USR_STATUS1 0x8
26 #define ADC5_USR_STATUS1_CONV_FAULT BIT(7)
27 #define ADC5_USR_STATUS1_REQ_STS BIT(1)
28 #define ADC5_USR_STATUS1_EOC BIT(0)
29 #define ADC5_USR_STATUS1_REQ_STS_EOC_MASK 0x3
31 #define ADC5_USR_STATUS2 0x9
32 #define ADC5_USR_STATUS2_CONV_SEQ_MASK 0x70
33 #define ADC5_USR_STATUS2_CONV_SEQ_MASK_SHIFT 0x5
35 #define ADC5_USR_IBAT_MEAS 0xf
36 #define ADC5_USR_IBAT_MEAS_SUPPORTED BIT(0)
38 #define ADC5_USR_DIG_PARAM 0x42
39 #define ADC5_USR_DIG_PARAM_CAL_VAL BIT(6)
40 #define ADC5_USR_DIG_PARAM_CAL_VAL_SHIFT 6
41 #define ADC5_USR_DIG_PARAM_CAL_SEL 0x30
42 #define ADC5_USR_DIG_PARAM_CAL_SEL_SHIFT 4
43 #define ADC5_USR_DIG_PARAM_DEC_RATIO_SEL 0xc
44 #define ADC5_USR_DIG_PARAM_DEC_RATIO_SEL_SHIFT 2
46 #define ADC5_USR_FAST_AVG_CTL 0x43
47 #define ADC5_USR_FAST_AVG_CTL_EN BIT(7)
48 #define ADC5_USR_FAST_AVG_CTL_SAMPLES_MASK 0x7
50 #define ADC5_USR_CH_SEL_CTL 0x44
52 #define ADC5_USR_DELAY_CTL 0x45
53 #define ADC5_USR_HW_SETTLE_DELAY_MASK 0xf
55 #define ADC5_USR_EN_CTL1 0x46
56 #define ADC5_USR_EN_CTL1_ADC_EN BIT(7)
58 #define ADC5_USR_CONV_REQ 0x47
59 #define ADC5_USR_CONV_REQ_REQ BIT(7)
61 #define ADC5_USR_DATA0 0x50
63 #define ADC5_USR_DATA1 0x51
65 #define ADC5_USR_IBAT_DATA0 0x52
67 #define ADC5_USR_IBAT_DATA1 0x53
69 #define ADC_CHANNEL_OFFSET 0x8
70 #define ADC_CHANNEL_MASK GENMASK(7, 0)
73 * Conversion time varies based on the decimation, clock rate, fast average
74 * samples and measurements queued across different VADC peripherals.
75 * Set the timeout to a max of 100ms.
77 #define ADC5_CONV_TIME_MIN_US 263
78 #define ADC5_CONV_TIME_MAX_US 264
79 #define ADC5_CONV_TIME_RETRY 400
80 #define ADC5_CONV_TIMEOUT msecs_to_jiffies(100)
82 /* Digital version >= 5.3 supports hw_settle_2 */
83 #define ADC5_HW_SETTLE_DIFF_MINOR 3
84 #define ADC5_HW_SETTLE_DIFF_MAJOR 5
87 #define ADC_APP_SID 0x40
88 #define ADC_APP_SID_MASK GENMASK(3, 0)
89 #define ADC7_CONV_TIMEOUT msecs_to_jiffies(10)
91 enum adc5_cal_method
{
103 * struct adc5_channel_prop - ADC channel property.
104 * @channel: channel number, refer to the channel list.
105 * @cal_method: calibration method.
106 * @cal_val: calibration value
107 * @decimation: sampling rate supported for the channel.
108 * @sid: slave id of PMIC owning the channel, for PMIC7.
109 * @prescale: channel scaling performed on the input signal.
110 * @hw_settle_time: the time between AMUX being configured and the
111 * start of conversion.
112 * @avg_samples: ability to provide single result from the ADC
113 * that is an average of multiple measurements.
114 * @scale_fn_type: Represents the scaling function to convert voltage
115 * physical units desired by the client for the channel.
116 * @datasheet_name: Channel name used in device tree.
118 struct adc5_channel_prop
{
119 unsigned int channel
;
120 enum adc5_cal_method cal_method
;
121 enum adc5_cal_val cal_val
;
122 unsigned int decimation
;
124 unsigned int prescale
;
125 unsigned int hw_settle_time
;
126 unsigned int avg_samples
;
127 enum vadc_scale_fn_type scale_fn_type
;
128 const char *datasheet_name
;
132 * struct adc5_chip - ADC private structure.
133 * @regmap: SPMI ADC5 peripheral register map field.
134 * @dev: SPMI ADC5 device.
135 * @base: base address for the ADC peripheral.
136 * @nchannels: number of ADC channels.
137 * @chan_props: array of ADC channel properties.
138 * @iio_chans: array of IIO channels specification.
139 * @poll_eoc: use polling instead of interrupt.
140 * @complete: ADC result notification after interrupt is received.
141 * @lock: ADC lock for access to the peripheral.
142 * @data: software configuration data.
145 struct regmap
*regmap
;
148 unsigned int nchannels
;
149 struct adc5_channel_prop
*chan_props
;
150 struct iio_chan_spec
*iio_chans
;
152 struct completion complete
;
154 const struct adc5_data
*data
;
157 static const struct vadc_prescale_ratio adc5_prescale_ratios
[] = {
158 {.num
= 1, .den
= 1},
159 {.num
= 1, .den
= 3},
160 {.num
= 1, .den
= 4},
161 {.num
= 1, .den
= 6},
162 {.num
= 1, .den
= 20},
163 {.num
= 1, .den
= 8},
164 {.num
= 10, .den
= 81},
165 {.num
= 1, .den
= 10},
166 {.num
= 1, .den
= 16}
169 static int adc5_read(struct adc5_chip
*adc
, u16 offset
, u8
*data
, int len
)
171 return regmap_bulk_read(adc
->regmap
, adc
->base
+ offset
, data
, len
);
174 static int adc5_write(struct adc5_chip
*adc
, u16 offset
, u8
*data
, int len
)
176 return regmap_bulk_write(adc
->regmap
, adc
->base
+ offset
, data
, len
);
179 static int adc5_masked_write(struct adc5_chip
*adc
, u16 offset
, u8 mask
, u8 val
)
181 return regmap_update_bits(adc
->regmap
, adc
->base
+ offset
, mask
, val
);
184 static int adc5_prescaling_from_dt(u32 num
, u32 den
)
188 for (pre
= 0; pre
< ARRAY_SIZE(adc5_prescale_ratios
); pre
++)
189 if (adc5_prescale_ratios
[pre
].num
== num
&&
190 adc5_prescale_ratios
[pre
].den
== den
)
193 if (pre
== ARRAY_SIZE(adc5_prescale_ratios
))
199 static int adc5_hw_settle_time_from_dt(u32 value
,
200 const unsigned int *hw_settle
)
204 for (i
= 0; i
< VADC_HW_SETTLE_SAMPLES_MAX
; i
++) {
205 if (value
== hw_settle
[i
])
212 static int adc5_avg_samples_from_dt(u32 value
)
214 if (!is_power_of_2(value
) || value
> ADC5_AVG_SAMPLES_MAX
)
220 static int adc5_decimation_from_dt(u32 value
,
221 const unsigned int *decimation
)
225 for (i
= 0; i
< ADC5_DECIMATION_SAMPLES_MAX
; i
++) {
226 if (value
== decimation
[i
])
233 static int adc5_read_voltage_data(struct adc5_chip
*adc
, u16
*data
)
236 u8 rslt_lsb
, rslt_msb
;
238 ret
= adc5_read(adc
, ADC5_USR_DATA0
, &rslt_lsb
, sizeof(rslt_lsb
));
242 ret
= adc5_read(adc
, ADC5_USR_DATA1
, &rslt_msb
, sizeof(rslt_lsb
));
246 *data
= (rslt_msb
<< 8) | rslt_lsb
;
248 if (*data
== ADC5_USR_DATA_CHECK
) {
249 dev_err(adc
->dev
, "Invalid data:0x%x\n", *data
);
253 dev_dbg(adc
->dev
, "voltage raw code:0x%x\n", *data
);
258 static int adc5_poll_wait_eoc(struct adc5_chip
*adc
)
260 unsigned int count
, retry
= ADC5_CONV_TIME_RETRY
;
264 for (count
= 0; count
< retry
; count
++) {
265 ret
= adc5_read(adc
, ADC5_USR_STATUS1
, &status1
,
270 status1
&= ADC5_USR_STATUS1_REQ_STS_EOC_MASK
;
271 if (status1
== ADC5_USR_STATUS1_EOC
)
274 usleep_range(ADC5_CONV_TIME_MIN_US
, ADC5_CONV_TIME_MAX_US
);
280 static void adc5_update_dig_param(struct adc5_chip
*adc
,
281 struct adc5_channel_prop
*prop
, u8
*data
)
283 /* Update calibration value */
284 *data
&= ~ADC5_USR_DIG_PARAM_CAL_VAL
;
285 *data
|= (prop
->cal_val
<< ADC5_USR_DIG_PARAM_CAL_VAL_SHIFT
);
287 /* Update calibration select */
288 *data
&= ~ADC5_USR_DIG_PARAM_CAL_SEL
;
289 *data
|= (prop
->cal_method
<< ADC5_USR_DIG_PARAM_CAL_SEL_SHIFT
);
291 /* Update decimation ratio select */
292 *data
&= ~ADC5_USR_DIG_PARAM_DEC_RATIO_SEL
;
293 *data
|= (prop
->decimation
<< ADC5_USR_DIG_PARAM_DEC_RATIO_SEL_SHIFT
);
296 static int adc5_configure(struct adc5_chip
*adc
,
297 struct adc5_channel_prop
*prop
)
302 /* Read registers 0x42 through 0x46 */
303 ret
= adc5_read(adc
, ADC5_USR_DIG_PARAM
, buf
, sizeof(buf
));
307 /* Digital param selection */
308 adc5_update_dig_param(adc
, prop
, &buf
[0]);
310 /* Update fast average sample value */
311 buf
[1] &= (u8
) ~ADC5_USR_FAST_AVG_CTL_SAMPLES_MASK
;
312 buf
[1] |= prop
->avg_samples
;
314 /* Select ADC channel */
315 buf
[2] = prop
->channel
;
317 /* Select HW settle delay for channel */
318 buf
[3] &= (u8
) ~ADC5_USR_HW_SETTLE_DELAY_MASK
;
319 buf
[3] |= prop
->hw_settle_time
;
321 /* Select ADC enable */
322 buf
[4] |= ADC5_USR_EN_CTL1_ADC_EN
;
324 /* Select CONV request */
325 buf
[5] |= ADC5_USR_CONV_REQ_REQ
;
328 reinit_completion(&adc
->complete
);
330 return adc5_write(adc
, ADC5_USR_DIG_PARAM
, buf
, sizeof(buf
));
333 static int adc7_configure(struct adc5_chip
*adc
,
334 struct adc5_channel_prop
*prop
)
337 u8 conv_req
= 0, buf
[4];
339 ret
= adc5_masked_write(adc
, ADC_APP_SID
, ADC_APP_SID_MASK
, prop
->sid
);
343 ret
= adc5_read(adc
, ADC5_USR_DIG_PARAM
, buf
, sizeof(buf
));
347 /* Digital param selection */
348 adc5_update_dig_param(adc
, prop
, &buf
[0]);
350 /* Update fast average sample value */
351 buf
[1] &= ~ADC5_USR_FAST_AVG_CTL_SAMPLES_MASK
;
352 buf
[1] |= prop
->avg_samples
;
354 /* Select ADC channel */
355 buf
[2] = prop
->channel
;
357 /* Select HW settle delay for channel */
358 buf
[3] &= ~ADC5_USR_HW_SETTLE_DELAY_MASK
;
359 buf
[3] |= prop
->hw_settle_time
;
361 /* Select CONV request */
362 conv_req
= ADC5_USR_CONV_REQ_REQ
;
365 reinit_completion(&adc
->complete
);
367 ret
= adc5_write(adc
, ADC5_USR_DIG_PARAM
, buf
, sizeof(buf
));
371 return adc5_write(adc
, ADC5_USR_CONV_REQ
, &conv_req
, 1);
374 static int adc5_do_conversion(struct adc5_chip
*adc
,
375 struct adc5_channel_prop
*prop
,
376 struct iio_chan_spec
const *chan
,
377 u16
*data_volt
, u16
*data_cur
)
381 mutex_lock(&adc
->lock
);
383 ret
= adc5_configure(adc
, prop
);
385 dev_err(adc
->dev
, "ADC configure failed with %d\n", ret
);
390 ret
= adc5_poll_wait_eoc(adc
);
392 dev_err(adc
->dev
, "EOC bit not set\n");
396 ret
= wait_for_completion_timeout(&adc
->complete
,
399 dev_dbg(adc
->dev
, "Did not get completion timeout.\n");
400 ret
= adc5_poll_wait_eoc(adc
);
402 dev_err(adc
->dev
, "EOC bit not set\n");
408 ret
= adc5_read_voltage_data(adc
, data_volt
);
410 mutex_unlock(&adc
->lock
);
415 static int adc7_do_conversion(struct adc5_chip
*adc
,
416 struct adc5_channel_prop
*prop
,
417 struct iio_chan_spec
const *chan
,
418 u16
*data_volt
, u16
*data_cur
)
423 mutex_lock(&adc
->lock
);
425 ret
= adc7_configure(adc
, prop
);
427 dev_err(adc
->dev
, "ADC configure failed with %d\n", ret
);
431 /* No support for polling mode at present */
432 wait_for_completion_timeout(&adc
->complete
, ADC7_CONV_TIMEOUT
);
434 ret
= adc5_read(adc
, ADC5_USR_STATUS1
, &status
, 1);
438 if (status
& ADC5_USR_STATUS1_CONV_FAULT
) {
439 dev_err(adc
->dev
, "Unexpected conversion fault\n");
444 ret
= adc5_read_voltage_data(adc
, data_volt
);
447 mutex_unlock(&adc
->lock
);
452 typedef int (*adc_do_conversion
)(struct adc5_chip
*adc
,
453 struct adc5_channel_prop
*prop
,
454 struct iio_chan_spec
const *chan
,
455 u16
*data_volt
, u16
*data_cur
);
457 static irqreturn_t
adc5_isr(int irq
, void *dev_id
)
459 struct adc5_chip
*adc
= dev_id
;
461 complete(&adc
->complete
);
466 static int adc5_of_xlate(struct iio_dev
*indio_dev
,
467 const struct of_phandle_args
*iiospec
)
469 struct adc5_chip
*adc
= iio_priv(indio_dev
);
472 for (i
= 0; i
< adc
->nchannels
; i
++)
473 if (adc
->chan_props
[i
].channel
== iiospec
->args
[0])
479 static int adc7_of_xlate(struct iio_dev
*indio_dev
,
480 const struct of_phandle_args
*iiospec
)
482 struct adc5_chip
*adc
= iio_priv(indio_dev
);
485 for (i
= 0; i
< adc
->nchannels
; i
++) {
486 v_channel
= (adc
->chan_props
[i
].sid
<< ADC_CHANNEL_OFFSET
) |
487 adc
->chan_props
[i
].channel
;
488 if (v_channel
== iiospec
->args
[0])
495 static int adc_read_raw_common(struct iio_dev
*indio_dev
,
496 struct iio_chan_spec
const *chan
, int *val
, int *val2
,
497 long mask
, adc_do_conversion do_conv
)
499 struct adc5_chip
*adc
= iio_priv(indio_dev
);
500 struct adc5_channel_prop
*prop
;
501 u16 adc_code_volt
, adc_code_cur
;
504 prop
= &adc
->chan_props
[chan
->address
];
507 case IIO_CHAN_INFO_PROCESSED
:
508 ret
= do_conv(adc
, prop
, chan
,
509 &adc_code_volt
, &adc_code_cur
);
513 ret
= qcom_adc5_hw_scale(prop
->scale_fn_type
,
514 &adc5_prescale_ratios
[prop
->prescale
],
526 static int adc5_read_raw(struct iio_dev
*indio_dev
,
527 struct iio_chan_spec
const *chan
, int *val
, int *val2
,
530 return adc_read_raw_common(indio_dev
, chan
, val
, val2
,
531 mask
, adc5_do_conversion
);
534 static int adc7_read_raw(struct iio_dev
*indio_dev
,
535 struct iio_chan_spec
const *chan
, int *val
, int *val2
,
538 return adc_read_raw_common(indio_dev
, chan
, val
, val2
,
539 mask
, adc7_do_conversion
);
542 static const struct iio_info adc5_info
= {
543 .read_raw
= adc5_read_raw
,
544 .of_xlate
= adc5_of_xlate
,
547 static const struct iio_info adc7_info
= {
548 .read_raw
= adc7_read_raw
,
549 .of_xlate
= adc7_of_xlate
,
552 struct adc5_channels
{
553 const char *datasheet_name
;
554 unsigned int prescale_index
;
555 enum iio_chan_type type
;
557 enum vadc_scale_fn_type scale_fn_type
;
560 /* In these definitions, _pre refers to an index into adc5_prescale_ratios. */
561 #define ADC5_CHAN(_dname, _type, _mask, _pre, _scale) \
563 .datasheet_name = _dname, \
564 .prescale_index = _pre, \
566 .info_mask = _mask, \
567 .scale_fn_type = _scale, \
570 #define ADC5_CHAN_TEMP(_dname, _pre, _scale) \
571 ADC5_CHAN(_dname, IIO_TEMP, \
572 BIT(IIO_CHAN_INFO_PROCESSED), \
575 #define ADC5_CHAN_VOLT(_dname, _pre, _scale) \
576 ADC5_CHAN(_dname, IIO_VOLTAGE, \
577 BIT(IIO_CHAN_INFO_PROCESSED), \
580 static const struct adc5_channels adc5_chans_pmic[ADC5_MAX_CHANNEL] = {
581 [ADC5_REF_GND
] = ADC5_CHAN_VOLT("ref_gnd", 0,
582 SCALE_HW_CALIB_DEFAULT
)
583 [ADC5_1P25VREF
] = ADC5_CHAN_VOLT("vref_1p25", 0,
584 SCALE_HW_CALIB_DEFAULT
)
585 [ADC5_VPH_PWR
] = ADC5_CHAN_VOLT("vph_pwr", 1,
586 SCALE_HW_CALIB_DEFAULT
)
587 [ADC5_VBAT_SNS
] = ADC5_CHAN_VOLT("vbat_sns", 1,
588 SCALE_HW_CALIB_DEFAULT
)
589 [ADC5_DIE_TEMP
] = ADC5_CHAN_TEMP("die_temp", 0,
590 SCALE_HW_CALIB_PMIC_THERM
)
591 [ADC5_USB_IN_I
] = ADC5_CHAN_VOLT("usb_in_i_uv", 0,
592 SCALE_HW_CALIB_DEFAULT
)
593 [ADC5_USB_IN_V_16
] = ADC5_CHAN_VOLT("usb_in_v_div_16", 8,
594 SCALE_HW_CALIB_DEFAULT
)
595 [ADC5_CHG_TEMP
] = ADC5_CHAN_TEMP("chg_temp", 0,
596 SCALE_HW_CALIB_PM5_CHG_TEMP
)
597 /* Charger prescales SBUx and MID_CHG to fit within 1.8V upper unit */
598 [ADC5_SBUx
] = ADC5_CHAN_VOLT("chg_sbux", 1,
599 SCALE_HW_CALIB_DEFAULT
)
600 [ADC5_MID_CHG_DIV6
] = ADC5_CHAN_VOLT("chg_mid_chg", 3,
601 SCALE_HW_CALIB_DEFAULT
)
602 [ADC5_XO_THERM_100K_PU
] = ADC5_CHAN_TEMP("xo_therm", 0,
603 SCALE_HW_CALIB_XOTHERM
)
604 [ADC5_AMUX_THM1_100K_PU
] = ADC5_CHAN_TEMP("amux_thm1_100k_pu", 0,
605 SCALE_HW_CALIB_THERM_100K_PULLUP
)
606 [ADC5_AMUX_THM2_100K_PU
] = ADC5_CHAN_TEMP("amux_thm2_100k_pu", 0,
607 SCALE_HW_CALIB_THERM_100K_PULLUP
)
608 [ADC5_AMUX_THM3_100K_PU
] = ADC5_CHAN_TEMP("amux_thm3_100k_pu", 0,
609 SCALE_HW_CALIB_THERM_100K_PULLUP
)
610 [ADC5_AMUX_THM2
] = ADC5_CHAN_TEMP("amux_thm2", 0,
611 SCALE_HW_CALIB_PM5_SMB_TEMP
)
614 static const struct adc5_channels adc7_chans_pmic
[ADC5_MAX_CHANNEL
] = {
615 [ADC7_REF_GND
] = ADC5_CHAN_VOLT("ref_gnd", 0,
616 SCALE_HW_CALIB_DEFAULT
)
617 [ADC7_1P25VREF
] = ADC5_CHAN_VOLT("vref_1p25", 0,
618 SCALE_HW_CALIB_DEFAULT
)
619 [ADC7_VPH_PWR
] = ADC5_CHAN_VOLT("vph_pwr", 1,
620 SCALE_HW_CALIB_DEFAULT
)
621 [ADC7_VBAT_SNS
] = ADC5_CHAN_VOLT("vbat_sns", 3,
622 SCALE_HW_CALIB_DEFAULT
)
623 [ADC7_DIE_TEMP
] = ADC5_CHAN_TEMP("die_temp", 0,
624 SCALE_HW_CALIB_PMIC_THERM_PM7
)
625 [ADC7_AMUX_THM1_100K_PU
] = ADC5_CHAN_TEMP("amux_thm1_pu2", 0,
626 SCALE_HW_CALIB_THERM_100K_PU_PM7
)
627 [ADC7_AMUX_THM2_100K_PU
] = ADC5_CHAN_TEMP("amux_thm2_pu2", 0,
628 SCALE_HW_CALIB_THERM_100K_PU_PM7
)
629 [ADC7_AMUX_THM3_100K_PU
] = ADC5_CHAN_TEMP("amux_thm3_pu2", 0,
630 SCALE_HW_CALIB_THERM_100K_PU_PM7
)
631 [ADC7_AMUX_THM4_100K_PU
] = ADC5_CHAN_TEMP("amux_thm4_pu2", 0,
632 SCALE_HW_CALIB_THERM_100K_PU_PM7
)
633 [ADC7_AMUX_THM5_100K_PU
] = ADC5_CHAN_TEMP("amux_thm5_pu2", 0,
634 SCALE_HW_CALIB_THERM_100K_PU_PM7
)
635 [ADC7_AMUX_THM6_100K_PU
] = ADC5_CHAN_TEMP("amux_thm6_pu2", 0,
636 SCALE_HW_CALIB_THERM_100K_PU_PM7
)
637 [ADC7_GPIO1_100K_PU
] = ADC5_CHAN_TEMP("gpio1_pu2", 0,
638 SCALE_HW_CALIB_THERM_100K_PU_PM7
)
639 [ADC7_GPIO2_100K_PU
] = ADC5_CHAN_TEMP("gpio2_pu2", 0,
640 SCALE_HW_CALIB_THERM_100K_PU_PM7
)
641 [ADC7_GPIO3_100K_PU
] = ADC5_CHAN_TEMP("gpio3_pu2", 0,
642 SCALE_HW_CALIB_THERM_100K_PU_PM7
)
643 [ADC7_GPIO4_100K_PU
] = ADC5_CHAN_TEMP("gpio4_pu2", 0,
644 SCALE_HW_CALIB_THERM_100K_PU_PM7
)
647 static const struct adc5_channels adc5_chans_rev2
[ADC5_MAX_CHANNEL
] = {
648 [ADC5_REF_GND
] = ADC5_CHAN_VOLT("ref_gnd", 0,
649 SCALE_HW_CALIB_DEFAULT
)
650 [ADC5_1P25VREF
] = ADC5_CHAN_VOLT("vref_1p25", 0,
651 SCALE_HW_CALIB_DEFAULT
)
652 [ADC5_VPH_PWR
] = ADC5_CHAN_VOLT("vph_pwr", 1,
653 SCALE_HW_CALIB_DEFAULT
)
654 [ADC5_VBAT_SNS
] = ADC5_CHAN_VOLT("vbat_sns", 1,
655 SCALE_HW_CALIB_DEFAULT
)
656 [ADC5_VCOIN
] = ADC5_CHAN_VOLT("vcoin", 1,
657 SCALE_HW_CALIB_DEFAULT
)
658 [ADC5_DIE_TEMP
] = ADC5_CHAN_TEMP("die_temp", 0,
659 SCALE_HW_CALIB_PMIC_THERM
)
660 [ADC5_AMUX_THM1_100K_PU
] = ADC5_CHAN_TEMP("amux_thm1_100k_pu", 0,
661 SCALE_HW_CALIB_THERM_100K_PULLUP
)
662 [ADC5_AMUX_THM2_100K_PU
] = ADC5_CHAN_TEMP("amux_thm2_100k_pu", 0,
663 SCALE_HW_CALIB_THERM_100K_PULLUP
)
664 [ADC5_AMUX_THM3_100K_PU
] = ADC5_CHAN_TEMP("amux_thm3_100k_pu", 0,
665 SCALE_HW_CALIB_THERM_100K_PULLUP
)
666 [ADC5_AMUX_THM4_100K_PU
] = ADC5_CHAN_TEMP("amux_thm4_100k_pu", 0,
667 SCALE_HW_CALIB_THERM_100K_PULLUP
)
668 [ADC5_AMUX_THM5_100K_PU
] = ADC5_CHAN_TEMP("amux_thm5_100k_pu", 0,
669 SCALE_HW_CALIB_THERM_100K_PULLUP
)
670 [ADC5_XO_THERM_100K_PU
] = ADC5_CHAN_TEMP("xo_therm_100k_pu", 0,
671 SCALE_HW_CALIB_THERM_100K_PULLUP
)
674 static int adc5_get_dt_channel_data(struct adc5_chip
*adc
,
675 struct adc5_channel_prop
*prop
,
676 struct device_node
*node
,
677 const struct adc5_data
*data
)
679 const char *name
= node
->name
, *channel_name
;
680 u32 chan
, value
, varr
[2];
683 struct device
*dev
= adc
->dev
;
685 ret
= of_property_read_u32(node
, "reg", &chan
);
687 dev_err(dev
, "invalid channel number %s\n", name
);
691 /* Value read from "reg" is virtual channel number */
693 /* virtual channel number = sid << 8 | channel number */
695 if (adc
->data
->info
== &adc7_info
) {
696 sid
= chan
>> ADC_CHANNEL_OFFSET
;
697 chan
= chan
& ADC_CHANNEL_MASK
;
700 if (chan
> ADC5_PARALLEL_ISENSE_VBAT_IDATA
||
701 !data
->adc_chans
[chan
].datasheet_name
) {
702 dev_err(dev
, "%s invalid channel number %d\n", name
, chan
);
706 /* the channel has DT description */
707 prop
->channel
= chan
;
710 channel_name
= of_get_property(node
,
711 "label", NULL
) ? : node
->name
;
713 dev_err(dev
, "Invalid channel name\n");
716 prop
->datasheet_name
= channel_name
;
718 ret
= of_property_read_u32(node
, "qcom,decimation", &value
);
720 ret
= adc5_decimation_from_dt(value
, data
->decimation
);
722 dev_err(dev
, "%02x invalid decimation %d\n",
726 prop
->decimation
= ret
;
728 prop
->decimation
= ADC5_DECIMATION_DEFAULT
;
731 ret
= of_property_read_u32_array(node
, "qcom,pre-scaling", varr
, 2);
733 ret
= adc5_prescaling_from_dt(varr
[0], varr
[1]);
735 dev_err(dev
, "%02x invalid pre-scaling <%d %d>\n",
736 chan
, varr
[0], varr
[1]);
739 prop
->prescale
= ret
;
742 adc
->data
->adc_chans
[prop
->channel
].prescale_index
;
745 ret
= of_property_read_u32(node
, "qcom,hw-settle-time", &value
);
749 ret
= adc5_read(adc
, ADC5_USR_REVISION1
, dig_version
,
750 sizeof(dig_version
));
752 dev_err(dev
, "Invalid dig version read %d\n", ret
);
756 dev_dbg(dev
, "dig_ver:minor:%d, major:%d\n", dig_version
[0],
758 /* Digital controller >= 5.3 have hw_settle_2 option */
759 if ((dig_version
[0] >= ADC5_HW_SETTLE_DIFF_MINOR
&&
760 dig_version
[1] >= ADC5_HW_SETTLE_DIFF_MAJOR
) ||
761 adc
->data
->info
== &adc7_info
)
762 ret
= adc5_hw_settle_time_from_dt(value
,
765 ret
= adc5_hw_settle_time_from_dt(value
,
769 dev_err(dev
, "%02x invalid hw-settle-time %d us\n",
773 prop
->hw_settle_time
= ret
;
775 prop
->hw_settle_time
= VADC_DEF_HW_SETTLE_TIME
;
778 ret
= of_property_read_u32(node
, "qcom,avg-samples", &value
);
780 ret
= adc5_avg_samples_from_dt(value
);
782 dev_err(dev
, "%02x invalid avg-samples %d\n",
786 prop
->avg_samples
= ret
;
788 prop
->avg_samples
= VADC_DEF_AVG_SAMPLES
;
791 if (of_property_read_bool(node
, "qcom,ratiometric"))
792 prop
->cal_method
= ADC5_RATIOMETRIC_CAL
;
794 prop
->cal_method
= ADC5_ABSOLUTE_CAL
;
797 * Default to using timer calibration. Using a fresh calibration value
798 * for every conversion will increase the overall time for a request.
800 prop
->cal_val
= ADC5_TIMER_CAL
;
802 dev_dbg(dev
, "%02x name %s\n", chan
, name
);
807 static const struct adc5_data adc5_data_pmic
= {
808 .full_scale_code_volt
= 0x70e4,
809 .full_scale_code_cur
= 0x2710,
810 .adc_chans
= adc5_chans_pmic
,
812 .decimation
= (unsigned int [ADC5_DECIMATION_SAMPLES_MAX
])
814 .hw_settle_1
= (unsigned int [VADC_HW_SETTLE_SAMPLES_MAX
])
815 {15, 100, 200, 300, 400, 500, 600, 700,
816 800, 900, 1, 2, 4, 6, 8, 10},
817 .hw_settle_2
= (unsigned int [VADC_HW_SETTLE_SAMPLES_MAX
])
818 {15, 100, 200, 300, 400, 500, 600, 700,
819 1, 2, 4, 8, 16, 32, 64, 128},
822 static const struct adc5_data adc7_data_pmic
= {
823 .full_scale_code_volt
= 0x70e4,
824 .adc_chans
= adc7_chans_pmic
,
826 .decimation
= (unsigned int [ADC5_DECIMATION_SAMPLES_MAX
])
828 .hw_settle_2
= (unsigned int [VADC_HW_SETTLE_SAMPLES_MAX
])
829 {15, 100, 200, 300, 400, 500, 600, 700,
830 1000, 2000, 4000, 8000, 16000, 32000,
834 static const struct adc5_data adc5_data_pmic_rev2
= {
835 .full_scale_code_volt
= 0x4000,
836 .full_scale_code_cur
= 0x1800,
837 .adc_chans
= adc5_chans_rev2
,
839 .decimation
= (unsigned int [ADC5_DECIMATION_SAMPLES_MAX
])
841 .hw_settle_1
= (unsigned int [VADC_HW_SETTLE_SAMPLES_MAX
])
842 {0, 100, 200, 300, 400, 500, 600, 700,
843 800, 900, 1, 2, 4, 6, 8, 10},
844 .hw_settle_2
= (unsigned int [VADC_HW_SETTLE_SAMPLES_MAX
])
845 {15, 100, 200, 300, 400, 500, 600, 700,
846 1, 2, 4, 8, 16, 32, 64, 128},
849 static const struct of_device_id adc5_match_table
[] = {
851 .compatible
= "qcom,spmi-adc5",
852 .data
= &adc5_data_pmic
,
855 .compatible
= "qcom,spmi-adc7",
856 .data
= &adc7_data_pmic
,
859 .compatible
= "qcom,spmi-adc-rev2",
860 .data
= &adc5_data_pmic_rev2
,
864 MODULE_DEVICE_TABLE(of
, adc5_match_table
);
866 static int adc5_get_dt_data(struct adc5_chip
*adc
, struct device_node
*node
)
868 const struct adc5_channels
*adc_chan
;
869 struct iio_chan_spec
*iio_chan
;
870 struct adc5_channel_prop prop
, *chan_props
;
871 struct device_node
*child
;
872 unsigned int index
= 0;
873 const struct of_device_id
*id
;
874 const struct adc5_data
*data
;
877 adc
->nchannels
= of_get_available_child_count(node
);
881 adc
->iio_chans
= devm_kcalloc(adc
->dev
, adc
->nchannels
,
882 sizeof(*adc
->iio_chans
), GFP_KERNEL
);
886 adc
->chan_props
= devm_kcalloc(adc
->dev
, adc
->nchannels
,
887 sizeof(*adc
->chan_props
), GFP_KERNEL
);
888 if (!adc
->chan_props
)
891 chan_props
= adc
->chan_props
;
892 iio_chan
= adc
->iio_chans
;
893 id
= of_match_node(adc5_match_table
, node
);
897 data
= &adc5_data_pmic
;
900 for_each_available_child_of_node(node
, child
) {
901 ret
= adc5_get_dt_channel_data(adc
, &prop
, child
, data
);
908 data
->adc_chans
[prop
.channel
].scale_fn_type
;
910 adc_chan
= &data
->adc_chans
[prop
.channel
];
912 iio_chan
->channel
= prop
.channel
;
913 iio_chan
->datasheet_name
= prop
.datasheet_name
;
914 iio_chan
->extend_name
= prop
.datasheet_name
;
915 iio_chan
->info_mask_separate
= adc_chan
->info_mask
;
916 iio_chan
->type
= adc_chan
->type
;
917 iio_chan
->address
= index
;
926 static int adc5_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 adc5_chip
*adc
;
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(*adc
));
948 adc
= iio_priv(indio_dev
);
949 adc
->regmap
= regmap
;
953 init_completion(&adc
->complete
);
954 mutex_init(&adc
->lock
);
956 ret
= adc5_get_dt_data(adc
, node
);
958 dev_err(dev
, "adc get dt data failed\n");
962 irq_eoc
= platform_get_irq(pdev
, 0);
964 if (irq_eoc
== -EPROBE_DEFER
|| irq_eoc
== -EINVAL
)
966 adc
->poll_eoc
= true;
968 ret
= devm_request_irq(dev
, irq_eoc
, adc5_isr
, 0,
974 indio_dev
->name
= pdev
->name
;
975 indio_dev
->modes
= INDIO_DIRECT_MODE
;
976 indio_dev
->info
= adc
->data
->info
;
977 indio_dev
->channels
= adc
->iio_chans
;
978 indio_dev
->num_channels
= adc
->nchannels
;
980 return devm_iio_device_register(dev
, indio_dev
);
983 static struct platform_driver adc5_driver
= {
985 .name
= "qcom-spmi-adc5",
986 .of_match_table
= adc5_match_table
,
990 module_platform_driver(adc5_driver
);
992 MODULE_ALIAS("platform:qcom-spmi-adc5");
993 MODULE_DESCRIPTION("Qualcomm Technologies Inc. PMIC5 ADC driver");
994 MODULE_LICENSE("GPL v2");