1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2016-2017, 2019, The Linux Foundation. All rights reserved.
4 * Copyright (c) 2022 Linaro Limited.
5 * Author: Caleb Connolly <caleb.connolly@linaro.org>
7 * This driver is for the Round Robin ADC found in the pmi8998 and pm660 PMICs.
10 #include <linux/bitfield.h>
11 #include <linux/delay.h>
12 #include <linux/kernel.h>
13 #include <linux/math64.h>
14 #include <linux/module.h>
15 #include <linux/mod_devicetable.h>
16 #include <linux/platform_device.h>
17 #include <linux/property.h>
18 #include <linux/regmap.h>
19 #include <linux/spmi.h>
20 #include <linux/types.h>
21 #include <linux/units.h>
23 #include <linux/unaligned.h>
25 #include <linux/iio/iio.h>
26 #include <linux/iio/types.h>
28 #include <soc/qcom/qcom-spmi-pmic.h>
30 #define DRIVER_NAME "qcom-spmi-rradc"
32 #define RR_ADC_EN_CTL 0x46
33 #define RR_ADC_SKIN_TEMP_LSB 0x50
34 #define RR_ADC_SKIN_TEMP_MSB 0x51
35 #define RR_ADC_CTL 0x52
36 #define RR_ADC_CTL_CONTINUOUS_SEL BIT(3)
37 #define RR_ADC_LOG 0x53
38 #define RR_ADC_LOG_CLR_CTRL BIT(0)
40 #define RR_ADC_FAKE_BATT_LOW_LSB 0x58
41 #define RR_ADC_FAKE_BATT_LOW_MSB 0x59
42 #define RR_ADC_FAKE_BATT_HIGH_LSB 0x5A
43 #define RR_ADC_FAKE_BATT_HIGH_MSB 0x5B
45 #define RR_ADC_BATT_ID_CTRL 0x60
46 #define RR_ADC_BATT_ID_CTRL_CHANNEL_CONV BIT(0)
47 #define RR_ADC_BATT_ID_TRIGGER 0x61
48 #define RR_ADC_BATT_ID_STS 0x62
49 #define RR_ADC_BATT_ID_CFG 0x63
50 #define BATT_ID_SETTLE_MASK GENMASK(7, 5)
51 #define RR_ADC_BATT_ID_5_LSB 0x66
52 #define RR_ADC_BATT_ID_5_MSB 0x67
53 #define RR_ADC_BATT_ID_15_LSB 0x68
54 #define RR_ADC_BATT_ID_15_MSB 0x69
55 #define RR_ADC_BATT_ID_150_LSB 0x6A
56 #define RR_ADC_BATT_ID_150_MSB 0x6B
58 #define RR_ADC_BATT_THERM_CTRL 0x70
59 #define RR_ADC_BATT_THERM_TRIGGER 0x71
60 #define RR_ADC_BATT_THERM_STS 0x72
61 #define RR_ADC_BATT_THERM_CFG 0x73
62 #define RR_ADC_BATT_THERM_LSB 0x74
63 #define RR_ADC_BATT_THERM_MSB 0x75
64 #define RR_ADC_BATT_THERM_FREQ 0x76
66 #define RR_ADC_AUX_THERM_CTRL 0x80
67 #define RR_ADC_AUX_THERM_TRIGGER 0x81
68 #define RR_ADC_AUX_THERM_STS 0x82
69 #define RR_ADC_AUX_THERM_CFG 0x83
70 #define RR_ADC_AUX_THERM_LSB 0x84
71 #define RR_ADC_AUX_THERM_MSB 0x85
73 #define RR_ADC_SKIN_HOT 0x86
74 #define RR_ADC_SKIN_TOO_HOT 0x87
76 #define RR_ADC_AUX_THERM_C1 0x88
77 #define RR_ADC_AUX_THERM_C2 0x89
78 #define RR_ADC_AUX_THERM_C3 0x8A
79 #define RR_ADC_AUX_THERM_HALF_RANGE 0x8B
81 #define RR_ADC_USB_IN_V_CTRL 0x90
82 #define RR_ADC_USB_IN_V_TRIGGER 0x91
83 #define RR_ADC_USB_IN_V_STS 0x92
84 #define RR_ADC_USB_IN_V_LSB 0x94
85 #define RR_ADC_USB_IN_V_MSB 0x95
86 #define RR_ADC_USB_IN_I_CTRL 0x98
87 #define RR_ADC_USB_IN_I_TRIGGER 0x99
88 #define RR_ADC_USB_IN_I_STS 0x9A
89 #define RR_ADC_USB_IN_I_LSB 0x9C
90 #define RR_ADC_USB_IN_I_MSB 0x9D
92 #define RR_ADC_DC_IN_V_CTRL 0xA0
93 #define RR_ADC_DC_IN_V_TRIGGER 0xA1
94 #define RR_ADC_DC_IN_V_STS 0xA2
95 #define RR_ADC_DC_IN_V_LSB 0xA4
96 #define RR_ADC_DC_IN_V_MSB 0xA5
97 #define RR_ADC_DC_IN_I_CTRL 0xA8
98 #define RR_ADC_DC_IN_I_TRIGGER 0xA9
99 #define RR_ADC_DC_IN_I_STS 0xAA
100 #define RR_ADC_DC_IN_I_LSB 0xAC
101 #define RR_ADC_DC_IN_I_MSB 0xAD
103 #define RR_ADC_PMI_DIE_TEMP_CTRL 0xB0
104 #define RR_ADC_PMI_DIE_TEMP_TRIGGER 0xB1
105 #define RR_ADC_PMI_DIE_TEMP_STS 0xB2
106 #define RR_ADC_PMI_DIE_TEMP_CFG 0xB3
107 #define RR_ADC_PMI_DIE_TEMP_LSB 0xB4
108 #define RR_ADC_PMI_DIE_TEMP_MSB 0xB5
110 #define RR_ADC_CHARGER_TEMP_CTRL 0xB8
111 #define RR_ADC_CHARGER_TEMP_TRIGGER 0xB9
112 #define RR_ADC_CHARGER_TEMP_STS 0xBA
113 #define RR_ADC_CHARGER_TEMP_CFG 0xBB
114 #define RR_ADC_CHARGER_TEMP_LSB 0xBC
115 #define RR_ADC_CHARGER_TEMP_MSB 0xBD
116 #define RR_ADC_CHARGER_HOT 0xBE
117 #define RR_ADC_CHARGER_TOO_HOT 0xBF
119 #define RR_ADC_GPIO_CTRL 0xC0
120 #define RR_ADC_GPIO_TRIGGER 0xC1
121 #define RR_ADC_GPIO_STS 0xC2
122 #define RR_ADC_GPIO_LSB 0xC4
123 #define RR_ADC_GPIO_MSB 0xC5
125 #define RR_ADC_ATEST_CTRL 0xC8
126 #define RR_ADC_ATEST_TRIGGER 0xC9
127 #define RR_ADC_ATEST_STS 0xCA
128 #define RR_ADC_ATEST_LSB 0xCC
129 #define RR_ADC_ATEST_MSB 0xCD
130 #define RR_ADC_SEC_ACCESS 0xD0
132 #define RR_ADC_PERPH_RESET_CTL2 0xD9
133 #define RR_ADC_PERPH_RESET_CTL3 0xDA
134 #define RR_ADC_PERPH_RESET_CTL4 0xDB
135 #define RR_ADC_INT_TEST1 0xE0
136 #define RR_ADC_INT_TEST_VAL 0xE1
138 #define RR_ADC_TM_TRIGGER_CTRLS 0xE2
139 #define RR_ADC_TM_ADC_CTRLS 0xE3
140 #define RR_ADC_TM_CNL_CTRL 0xE4
141 #define RR_ADC_TM_BATT_ID_CTRL 0xE5
142 #define RR_ADC_TM_THERM_CTRL 0xE6
143 #define RR_ADC_TM_CONV_STS 0xE7
144 #define RR_ADC_TM_ADC_READ_LSB 0xE8
145 #define RR_ADC_TM_ADC_READ_MSB 0xE9
146 #define RR_ADC_TM_ATEST_MUX_1 0xEA
147 #define RR_ADC_TM_ATEST_MUX_2 0xEB
148 #define RR_ADC_TM_REFERENCES 0xED
149 #define RR_ADC_TM_MISC_CTL 0xEE
150 #define RR_ADC_TM_RR_CTRL 0xEF
152 #define RR_ADC_TRIGGER_EVERY_CYCLE BIT(7)
153 #define RR_ADC_TRIGGER_CTL BIT(0)
155 #define RR_ADC_BATT_ID_RANGE 820
157 #define RR_ADC_BITS 10
158 #define RR_ADC_CHAN_MSB (1 << RR_ADC_BITS)
159 #define RR_ADC_FS_VOLTAGE_MV 2500
161 /* BATT_THERM 0.25K/LSB */
162 #define RR_ADC_BATT_THERM_LSB_K 4
164 #define RR_ADC_TEMP_FS_VOLTAGE_NUM 5000000
165 #define RR_ADC_TEMP_FS_VOLTAGE_DEN 3
166 #define RR_ADC_DIE_TEMP_OFFSET 601400
167 #define RR_ADC_DIE_TEMP_SLOPE 2
168 #define RR_ADC_DIE_TEMP_OFFSET_MILLI_DEGC 25000
170 #define RR_ADC_CHG_TEMP_GF_OFFSET_UV 1303168
171 #define RR_ADC_CHG_TEMP_GF_SLOPE_UV_PER_C 3784
172 #define RR_ADC_CHG_TEMP_SMIC_OFFSET_UV 1338433
173 #define RR_ADC_CHG_TEMP_SMIC_SLOPE_UV_PER_C 3655
174 #define RR_ADC_CHG_TEMP_660_GF_OFFSET_UV 1309001
175 #define RR_ADC_CHG_TEMP_660_GF_SLOPE_UV_PER_C 3403
176 #define RR_ADC_CHG_TEMP_660_SMIC_OFFSET_UV 1295898
177 #define RR_ADC_CHG_TEMP_660_SMIC_SLOPE_UV_PER_C 3596
178 #define RR_ADC_CHG_TEMP_660_MGNA_OFFSET_UV 1314779
179 #define RR_ADC_CHG_TEMP_660_MGNA_SLOPE_UV_PER_C 3496
180 #define RR_ADC_CHG_TEMP_OFFSET_MILLI_DEGC 25000
181 #define RR_ADC_CHG_THRESHOLD_SCALE 4
183 #define RR_ADC_VOLT_INPUT_FACTOR 8
184 #define RR_ADC_CURR_INPUT_FACTOR 2000
185 #define RR_ADC_CURR_USBIN_INPUT_FACTOR_MIL 1886
186 #define RR_ADC_CURR_USBIN_660_FACTOR_MIL 9
187 #define RR_ADC_CURR_USBIN_660_UV_VAL 579500
189 #define RR_ADC_GPIO_FS_RANGE 5000
190 #define RR_ADC_COHERENT_CHECK_RETRY 5
191 #define RR_ADC_CHAN_MAX_CONTINUOUS_BUFFER_LEN 16
193 #define RR_ADC_STS_CHANNEL_READING_MASK GENMASK(1, 0)
194 #define RR_ADC_STS_CHANNEL_STS BIT(1)
196 #define RR_ADC_TP_REV_VERSION1 21
197 #define RR_ADC_TP_REV_VERSION2 29
198 #define RR_ADC_TP_REV_VERSION3 32
200 #define RRADC_BATT_ID_DELAY_MAX 8
202 enum rradc_channel_id
{
219 * struct rradc_channel - rradc channel data
220 * @label: channel label
221 * @lsb: Channel least significant byte
222 * @status: Channel status address
223 * @size: number of bytes to read
224 * @trigger_addr: Trigger address, trigger is only used on some channels
225 * @trigger_mask: Trigger mask
226 * @scale_fn: Post process callback for channels which can't be exposed
229 struct rradc_channel
{
236 int (*scale_fn
)(struct rradc_chip
*chip
, u16 adc_code
, int *result
);
241 const struct qcom_spmi_pmic
*pmic
;
243 * Lock held while doing channel conversion
244 * involving multiple register read/writes
246 struct mutex conversion_lock
;
247 struct regmap
*regmap
;
253 static const int batt_id_delays
[] = { 0, 1, 4, 12, 20, 40, 60, 80 };
254 static const struct rradc_channel rradc_chans
[RR_ADC_CHAN_MAX
];
255 static const struct iio_chan_spec rradc_iio_chans
[RR_ADC_CHAN_MAX
];
257 static int rradc_read(struct rradc_chip
*chip
, u16 addr
, __le16
*buf
, int len
)
259 int ret
, retry_cnt
= 0;
260 __le16 data_check
[RR_ADC_CHAN_MAX_CONTINUOUS_BUFFER_LEN
/ 2];
262 if (len
> RR_ADC_CHAN_MAX_CONTINUOUS_BUFFER_LEN
) {
264 "Can't read more than %d bytes, but asked to read %d bytes.\n",
265 RR_ADC_CHAN_MAX_CONTINUOUS_BUFFER_LEN
, len
);
269 while (retry_cnt
< RR_ADC_COHERENT_CHECK_RETRY
) {
270 ret
= regmap_bulk_read(chip
->regmap
, chip
->base
+ addr
, buf
,
273 dev_err(chip
->dev
, "rr_adc reg 0x%x failed :%d\n", addr
,
278 ret
= regmap_bulk_read(chip
->regmap
, chip
->base
+ addr
,
281 dev_err(chip
->dev
, "rr_adc reg 0x%x failed :%d\n", addr
,
286 if (memcmp(buf
, data_check
, len
) != 0) {
289 "coherent read error, retry_cnt:%d\n",
297 if (retry_cnt
== RR_ADC_COHERENT_CHECK_RETRY
)
298 dev_err(chip
->dev
, "Retry exceeded for coherency check\n");
303 static int rradc_get_fab_coeff(struct rradc_chip
*chip
, int64_t *offset
,
306 if (chip
->pmic
->subtype
== PM660_SUBTYPE
) {
307 switch (chip
->pmic
->fab_id
) {
308 case PM660_FAB_ID_GF
:
309 *offset
= RR_ADC_CHG_TEMP_660_GF_OFFSET_UV
;
310 *slope
= RR_ADC_CHG_TEMP_660_GF_SLOPE_UV_PER_C
;
312 case PM660_FAB_ID_TSMC
:
313 *offset
= RR_ADC_CHG_TEMP_660_SMIC_OFFSET_UV
;
314 *slope
= RR_ADC_CHG_TEMP_660_SMIC_SLOPE_UV_PER_C
;
317 *offset
= RR_ADC_CHG_TEMP_660_MGNA_OFFSET_UV
;
318 *slope
= RR_ADC_CHG_TEMP_660_MGNA_SLOPE_UV_PER_C
;
320 } else if (chip
->pmic
->subtype
== PMI8998_SUBTYPE
) {
321 switch (chip
->pmic
->fab_id
) {
322 case PMI8998_FAB_ID_GF
:
323 *offset
= RR_ADC_CHG_TEMP_GF_OFFSET_UV
;
324 *slope
= RR_ADC_CHG_TEMP_GF_SLOPE_UV_PER_C
;
326 case PMI8998_FAB_ID_SMIC
:
327 *offset
= RR_ADC_CHG_TEMP_SMIC_OFFSET_UV
;
328 *slope
= RR_ADC_CHG_TEMP_SMIC_SLOPE_UV_PER_C
;
339 * These functions explicitly cast int64_t to int.
340 * They will never overflow, as the values are small enough.
342 static int rradc_post_process_batt_id(struct rradc_chip
*chip
, u16 adc_code
,
345 uint32_t current_value
;
348 current_value
= chip
->batt_id_data
;
349 r_id
= ((int64_t)adc_code
* RR_ADC_FS_VOLTAGE_MV
);
350 r_id
= div64_s64(r_id
, (RR_ADC_CHAN_MSB
* current_value
));
351 *result_ohms
= (int)(r_id
* MILLI
);
356 static int rradc_enable_continuous_mode(struct rradc_chip
*chip
)
360 /* Clear channel log */
361 ret
= regmap_set_bits(chip
->regmap
, chip
->base
+ RR_ADC_LOG
,
362 RR_ADC_LOG_CLR_CTRL
);
364 dev_err(chip
->dev
, "log ctrl update to clear failed:%d\n", ret
);
368 ret
= regmap_clear_bits(chip
->regmap
, chip
->base
+ RR_ADC_LOG
,
369 RR_ADC_LOG_CLR_CTRL
);
371 dev_err(chip
->dev
, "log ctrl update to not clear failed:%d\n",
376 /* Switch to continuous mode */
377 ret
= regmap_set_bits(chip
->regmap
, chip
->base
+ RR_ADC_CTL
,
378 RR_ADC_CTL_CONTINUOUS_SEL
);
380 dev_err(chip
->dev
, "Update to continuous mode failed:%d\n",
386 static int rradc_disable_continuous_mode(struct rradc_chip
*chip
)
390 /* Switch to non continuous mode */
391 ret
= regmap_clear_bits(chip
->regmap
, chip
->base
+ RR_ADC_CTL
,
392 RR_ADC_CTL_CONTINUOUS_SEL
);
394 dev_err(chip
->dev
, "Update to non-continuous mode failed:%d\n",
400 static bool rradc_is_ready(struct rradc_chip
*chip
,
401 enum rradc_channel_id chan_address
)
403 const struct rradc_channel
*chan
= &rradc_chans
[chan_address
];
405 unsigned int status
, mask
;
407 /* BATT_ID STS bit does not get set initially */
408 switch (chan_address
) {
410 mask
= RR_ADC_STS_CHANNEL_STS
;
413 mask
= RR_ADC_STS_CHANNEL_READING_MASK
;
417 ret
= regmap_read(chip
->regmap
, chip
->base
+ chan
->status
, &status
);
418 if (ret
< 0 || !(status
& mask
))
424 static int rradc_read_status_in_cont_mode(struct rradc_chip
*chip
,
425 enum rradc_channel_id chan_address
)
427 const struct rradc_channel
*chan
= &rradc_chans
[chan_address
];
428 const struct iio_chan_spec
*iio_chan
= &rradc_iio_chans
[chan_address
];
431 if (chan
->trigger_mask
== 0) {
432 dev_err(chip
->dev
, "Channel doesn't have a trigger mask\n");
436 ret
= regmap_set_bits(chip
->regmap
, chip
->base
+ chan
->trigger_addr
,
440 "Failed to apply trigger for channel '%s' ret=%d\n",
441 iio_chan
->extend_name
, ret
);
445 ret
= rradc_enable_continuous_mode(chip
);
447 dev_err(chip
->dev
, "Failed to switch to continuous mode\n");
448 goto disable_trigger
;
452 * The wait/sleep values were found through trial and error,
453 * this is mostly for the battery ID channel which takes some
456 for (i
= 0; i
< 5; i
++) {
457 if (rradc_is_ready(chip
, chan_address
))
459 usleep_range(50000, 50000 + 500);
463 dev_err(chip
->dev
, "Channel '%s' is not ready\n",
464 iio_chan
->extend_name
);
468 rradc_disable_continuous_mode(chip
);
471 regmap_clear_bits(chip
->regmap
, chip
->base
+ chan
->trigger_addr
,
477 static int rradc_prepare_batt_id_conversion(struct rradc_chip
*chip
,
478 enum rradc_channel_id chan_address
,
483 ret
= regmap_set_bits(chip
->regmap
, chip
->base
+ RR_ADC_BATT_ID_CTRL
,
484 RR_ADC_BATT_ID_CTRL_CHANNEL_CONV
);
486 dev_err(chip
->dev
, "Enabling BATT ID channel failed:%d\n", ret
);
490 ret
= regmap_set_bits(chip
->regmap
,
491 chip
->base
+ RR_ADC_BATT_ID_TRIGGER
,
494 dev_err(chip
->dev
, "BATT_ID trigger set failed:%d\n", ret
);
495 goto out_disable_batt_id
;
498 ret
= rradc_read_status_in_cont_mode(chip
, chan_address
);
500 /* Reset registers back to default values */
501 regmap_clear_bits(chip
->regmap
, chip
->base
+ RR_ADC_BATT_ID_TRIGGER
,
505 regmap_clear_bits(chip
->regmap
, chip
->base
+ RR_ADC_BATT_ID_CTRL
,
506 RR_ADC_BATT_ID_CTRL_CHANNEL_CONV
);
511 static int rradc_do_conversion(struct rradc_chip
*chip
,
512 enum rradc_channel_id chan_address
, u16
*data
)
514 const struct rradc_channel
*chan
= &rradc_chans
[chan_address
];
515 const struct iio_chan_spec
*iio_chan
= &rradc_iio_chans
[chan_address
];
519 mutex_lock(&chip
->conversion_lock
);
521 switch (chan_address
) {
523 ret
= rradc_prepare_batt_id_conversion(chip
, chan_address
, data
);
525 dev_err(chip
->dev
, "Battery ID conversion failed:%d\n",
532 case RR_ADC_DIE_TEMP
:
533 ret
= rradc_read_status_in_cont_mode(chip
, chan_address
);
536 "Error reading in continuous mode:%d\n", ret
);
541 if (!rradc_is_ready(chip
, chan_address
)) {
543 * Usually this means the channel isn't attached, for example
544 * the in_voltage_usbin_v_input channel will not be ready if
545 * no USB cable is attached
547 dev_dbg(chip
->dev
, "channel '%s' is not ready\n",
548 iio_chan
->extend_name
);
555 ret
= rradc_read(chip
, chan
->lsb
, buf
, chan
->size
);
557 dev_err(chip
->dev
, "read data failed\n");
562 * For the battery ID we read the register for every ID ADC and then
563 * see which one is actually connected.
565 if (chan_address
== RR_ADC_BATT_ID
) {
566 u16 batt_id_150
= le16_to_cpu(buf
[2]);
567 u16 batt_id_15
= le16_to_cpu(buf
[1]);
568 u16 batt_id_5
= le16_to_cpu(buf
[0]);
570 if (!batt_id_150
&& !batt_id_15
&& !batt_id_5
) {
572 "Invalid batt_id values with all zeros\n");
577 if (batt_id_150
<= RR_ADC_BATT_ID_RANGE
) {
579 chip
->batt_id_data
= 150;
580 } else if (batt_id_15
<= RR_ADC_BATT_ID_RANGE
) {
582 chip
->batt_id_data
= 15;
585 chip
->batt_id_data
= 5;
589 * All of the other channels are either 1 or 2 bytes.
590 * We can rely on the second byte being 0 for 1-byte channels.
592 *data
= le16_to_cpu(buf
[0]);
596 mutex_unlock(&chip
->conversion_lock
);
601 static int rradc_read_scale(struct rradc_chip
*chip
, int chan_address
, int *val
,
604 int64_t fab_offset
, fab_slope
;
607 ret
= rradc_get_fab_coeff(chip
, &fab_offset
, &fab_slope
);
609 dev_err(chip
->dev
, "Unable to get fab id coefficients\n");
613 switch (chan_address
) {
614 case RR_ADC_SKIN_TEMP
:
616 *val2
= RR_ADC_BATT_THERM_LSB_K
;
617 return IIO_VAL_FRACTIONAL
;
619 *val
= RR_ADC_CURR_USBIN_INPUT_FACTOR_MIL
*
620 RR_ADC_FS_VOLTAGE_MV
;
621 *val2
= RR_ADC_CHAN_MSB
;
622 return IIO_VAL_FRACTIONAL
;
624 *val
= RR_ADC_CURR_INPUT_FACTOR
* RR_ADC_FS_VOLTAGE_MV
;
625 *val2
= RR_ADC_CHAN_MSB
;
626 return IIO_VAL_FRACTIONAL
;
629 *val
= RR_ADC_VOLT_INPUT_FACTOR
* RR_ADC_FS_VOLTAGE_MV
* MILLI
;
630 *val2
= RR_ADC_CHAN_MSB
;
631 return IIO_VAL_FRACTIONAL
;
633 *val
= RR_ADC_GPIO_FS_RANGE
;
634 *val2
= RR_ADC_CHAN_MSB
;
635 return IIO_VAL_FRACTIONAL
;
636 case RR_ADC_CHG_TEMP
:
638 * We divide val2 by MILLI instead of multiplying val
639 * to avoid an integer overflow.
641 *val
= -RR_ADC_TEMP_FS_VOLTAGE_NUM
;
642 *val2
= div64_s64(RR_ADC_TEMP_FS_VOLTAGE_DEN
* RR_ADC_CHAN_MSB
*
646 return IIO_VAL_FRACTIONAL
;
647 case RR_ADC_DIE_TEMP
:
648 *val
= RR_ADC_TEMP_FS_VOLTAGE_NUM
;
649 *val2
= RR_ADC_TEMP_FS_VOLTAGE_DEN
* RR_ADC_CHAN_MSB
*
650 RR_ADC_DIE_TEMP_SLOPE
;
652 return IIO_VAL_FRACTIONAL
;
658 static int rradc_read_offset(struct rradc_chip
*chip
, int chan_address
, int *val
)
660 int64_t fab_offset
, fab_slope
;
661 int64_t offset1
, offset2
;
664 switch (chan_address
) {
665 case RR_ADC_SKIN_TEMP
:
667 * Offset from kelvin to degC, divided by the
668 * scale factor (250). We lose some precision here.
669 * 273150 / 250 = 1092.6
671 *val
= div64_s64(ABSOLUTE_ZERO_MILLICELSIUS
,
672 (MILLI
/ RR_ADC_BATT_THERM_LSB_K
));
674 case RR_ADC_CHG_TEMP
:
675 ret
= rradc_get_fab_coeff(chip
, &fab_offset
, &fab_slope
);
678 "Unable to get fab id coefficients\n");
681 offset1
= -(fab_offset
* RR_ADC_TEMP_FS_VOLTAGE_DEN
*
683 offset1
+= (int64_t)RR_ADC_TEMP_FS_VOLTAGE_NUM
/ 2ULL;
684 offset1
= div64_s64(offset1
,
685 (int64_t)(RR_ADC_TEMP_FS_VOLTAGE_NUM
));
687 offset2
= (int64_t)RR_ADC_CHG_TEMP_OFFSET_MILLI_DEGC
*
688 RR_ADC_TEMP_FS_VOLTAGE_DEN
* RR_ADC_CHAN_MSB
*
690 offset2
+= ((int64_t)MILLI
* RR_ADC_TEMP_FS_VOLTAGE_NUM
) / 2;
692 offset2
, ((int64_t)MILLI
* RR_ADC_TEMP_FS_VOLTAGE_NUM
));
695 * The -1 is to compensate for lost precision.
696 * It should actually be -0.7906976744186046.
697 * This works out to every value being off
698 * by about +0.091 degrees C after applying offset and scale.
700 *val
= (int)(offset1
- offset2
- 1);
702 case RR_ADC_DIE_TEMP
:
703 offset1
= -RR_ADC_DIE_TEMP_OFFSET
*
704 (int64_t)RR_ADC_TEMP_FS_VOLTAGE_DEN
*
705 (int64_t)RR_ADC_CHAN_MSB
;
706 offset1
= div64_s64(offset1
, RR_ADC_TEMP_FS_VOLTAGE_NUM
);
708 offset2
= -(int64_t)RR_ADC_CHG_TEMP_OFFSET_MILLI_DEGC
*
709 RR_ADC_TEMP_FS_VOLTAGE_DEN
* RR_ADC_CHAN_MSB
*
710 RR_ADC_DIE_TEMP_SLOPE
;
711 offset2
= div64_s64(offset2
,
712 ((int64_t)RR_ADC_TEMP_FS_VOLTAGE_NUM
));
715 * The result is -339, it should be -338.69789, this results
716 * in the calculated die temp being off by
717 * -0.004 - -0.0175 degrees C
719 *val
= (int)(offset1
- offset2
);
727 static int rradc_read_raw(struct iio_dev
*indio_dev
,
728 struct iio_chan_spec
const *chan_spec
, int *val
,
729 int *val2
, long mask
)
731 struct rradc_chip
*chip
= iio_priv(indio_dev
);
732 const struct rradc_channel
*chan
;
736 if (chan_spec
->address
>= RR_ADC_CHAN_MAX
) {
737 dev_err(chip
->dev
, "Invalid channel index:%lu\n",
743 case IIO_CHAN_INFO_SCALE
:
744 return rradc_read_scale(chip
, chan_spec
->address
, val
, val2
);
745 case IIO_CHAN_INFO_OFFSET
:
746 return rradc_read_offset(chip
, chan_spec
->address
, val
);
747 case IIO_CHAN_INFO_RAW
:
748 ret
= rradc_do_conversion(chip
, chan_spec
->address
, &adc_code
);
754 case IIO_CHAN_INFO_PROCESSED
:
755 chan
= &rradc_chans
[chan_spec
->address
];
758 ret
= rradc_do_conversion(chip
, chan_spec
->address
, &adc_code
);
762 *val
= chan
->scale_fn(chip
, adc_code
, val
);
769 static int rradc_read_label(struct iio_dev
*indio_dev
,
770 struct iio_chan_spec
const *chan
, char *label
)
772 return snprintf(label
, PAGE_SIZE
, "%s\n",
773 rradc_chans
[chan
->address
].label
);
776 static const struct iio_info rradc_info
= {
777 .read_raw
= rradc_read_raw
,
778 .read_label
= rradc_read_label
,
781 static const struct rradc_channel rradc_chans
[RR_ADC_CHAN_MAX
] = {
784 .scale_fn
= rradc_post_process_batt_id
,
785 .lsb
= RR_ADC_BATT_ID_5_LSB
,
786 .status
= RR_ADC_BATT_ID_STS
,
788 .trigger_addr
= RR_ADC_BATT_ID_TRIGGER
,
789 .trigger_mask
= BIT(0),
792 .lsb
= RR_ADC_BATT_THERM_LSB
,
793 .status
= RR_ADC_BATT_THERM_STS
,
795 .trigger_addr
= RR_ADC_BATT_THERM_TRIGGER
,
797 .label
= "pmi8998_skin",
798 .lsb
= RR_ADC_SKIN_TEMP_LSB
,
799 .status
= RR_ADC_AUX_THERM_STS
,
801 .trigger_addr
= RR_ADC_AUX_THERM_TRIGGER
,
804 .lsb
= RR_ADC_USB_IN_I_LSB
,
805 .status
= RR_ADC_USB_IN_I_STS
,
807 .trigger_addr
= RR_ADC_USB_IN_I_TRIGGER
,
810 .lsb
= RR_ADC_USB_IN_V_LSB
,
811 .status
= RR_ADC_USB_IN_V_STS
,
813 .trigger_addr
= RR_ADC_USB_IN_V_TRIGGER
,
814 .trigger_mask
= BIT(7),
817 .lsb
= RR_ADC_DC_IN_I_LSB
,
818 .status
= RR_ADC_DC_IN_I_STS
,
820 .trigger_addr
= RR_ADC_DC_IN_I_TRIGGER
,
823 .lsb
= RR_ADC_DC_IN_V_LSB
,
824 .status
= RR_ADC_DC_IN_V_STS
,
826 .trigger_addr
= RR_ADC_DC_IN_V_TRIGGER
,
828 .label
= "pmi8998_die",
829 .lsb
= RR_ADC_PMI_DIE_TEMP_LSB
,
830 .status
= RR_ADC_PMI_DIE_TEMP_STS
,
832 .trigger_addr
= RR_ADC_PMI_DIE_TEMP_TRIGGER
,
833 .trigger_mask
= RR_ADC_TRIGGER_EVERY_CYCLE
,
836 .lsb
= RR_ADC_CHARGER_TEMP_LSB
,
837 .status
= RR_ADC_CHARGER_TEMP_STS
,
839 .trigger_addr
= RR_ADC_CHARGER_TEMP_TRIGGER
,
842 .lsb
= RR_ADC_GPIO_LSB
,
843 .status
= RR_ADC_GPIO_STS
,
845 .trigger_addr
= RR_ADC_GPIO_TRIGGER
,
849 static const struct iio_chan_spec rradc_iio_chans
[RR_ADC_CHAN_MAX
] = {
851 .type
= IIO_RESISTANCE
,
852 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
853 .address
= RR_ADC_BATT_ID
,
858 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
859 .address
= RR_ADC_BATT_THERM
,
864 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
865 BIT(IIO_CHAN_INFO_SCALE
) |
866 BIT(IIO_CHAN_INFO_OFFSET
),
867 .address
= RR_ADC_SKIN_TEMP
,
872 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
873 BIT(IIO_CHAN_INFO_SCALE
),
874 .address
= RR_ADC_USBIN_I
,
879 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
880 BIT(IIO_CHAN_INFO_SCALE
),
881 .address
= RR_ADC_USBIN_V
,
886 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
887 BIT(IIO_CHAN_INFO_SCALE
),
888 .address
= RR_ADC_DCIN_I
,
893 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
894 BIT(IIO_CHAN_INFO_SCALE
),
895 .address
= RR_ADC_DCIN_V
,
900 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
901 BIT(IIO_CHAN_INFO_SCALE
) |
902 BIT(IIO_CHAN_INFO_OFFSET
),
903 .address
= RR_ADC_DIE_TEMP
,
908 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
909 BIT(IIO_CHAN_INFO_OFFSET
) |
910 BIT(IIO_CHAN_INFO_SCALE
),
911 .address
= RR_ADC_CHG_TEMP
,
916 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
) |
917 BIT(IIO_CHAN_INFO_SCALE
),
918 .address
= RR_ADC_GPIO
,
924 static int rradc_probe(struct platform_device
*pdev
)
926 struct device
*dev
= &pdev
->dev
;
927 struct iio_dev
*indio_dev
;
928 struct rradc_chip
*chip
;
929 int ret
, i
, batt_id_delay
;
931 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*chip
));
935 chip
= iio_priv(indio_dev
);
936 chip
->regmap
= dev_get_regmap(pdev
->dev
.parent
, NULL
);
938 dev_err(dev
, "Couldn't get parent's regmap\n");
943 mutex_init(&chip
->conversion_lock
);
945 ret
= device_property_read_u32(dev
, "reg", &chip
->base
);
947 dev_err(chip
->dev
, "Couldn't find reg address, ret = %d\n",
953 ret
= device_property_read_u32(dev
, "qcom,batt-id-delay-ms",
956 for (i
= 0; i
< RRADC_BATT_ID_DELAY_MAX
; i
++) {
957 if (batt_id_delay
== batt_id_delays
[i
])
960 if (i
== RRADC_BATT_ID_DELAY_MAX
)
964 if (batt_id_delay
>= 0) {
965 batt_id_delay
= FIELD_PREP(BATT_ID_SETTLE_MASK
, batt_id_delay
);
966 ret
= regmap_set_bits(chip
->regmap
,
967 chip
->base
+ RR_ADC_BATT_ID_CFG
,
971 "BATT_ID settling time config failed:%d\n",
976 /* Get the PMIC revision, we need it to handle some varying coefficients */
977 chip
->pmic
= qcom_pmic_get(chip
->dev
);
978 if (IS_ERR(chip
->pmic
)) {
979 dev_err(chip
->dev
, "Unable to get reference to PMIC device\n");
980 return PTR_ERR(chip
->pmic
);
983 switch (chip
->pmic
->subtype
) {
984 case PMI8998_SUBTYPE
:
985 indio_dev
->name
= "pmi8998-rradc";
988 indio_dev
->name
= "pm660-rradc";
991 indio_dev
->name
= DRIVER_NAME
;
994 indio_dev
->modes
= INDIO_DIRECT_MODE
;
995 indio_dev
->info
= &rradc_info
;
996 indio_dev
->channels
= rradc_iio_chans
;
997 indio_dev
->num_channels
= ARRAY_SIZE(rradc_iio_chans
);
999 return devm_iio_device_register(dev
, indio_dev
);
1002 static const struct of_device_id rradc_match_table
[] = {
1003 { .compatible
= "qcom,pm660-rradc" },
1004 { .compatible
= "qcom,pmi8998-rradc" },
1007 MODULE_DEVICE_TABLE(of
, rradc_match_table
);
1009 static struct platform_driver rradc_driver
= {
1011 .name
= DRIVER_NAME
,
1012 .of_match_table
= rradc_match_table
,
1014 .probe
= rradc_probe
,
1016 module_platform_driver(rradc_driver
);
1018 MODULE_DESCRIPTION("QCOM SPMI PMIC RR ADC driver");
1019 MODULE_AUTHOR("Caleb Connolly <caleb.connolly@linaro.org>");
1020 MODULE_LICENSE("GPL");