1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2012-2014, 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/mutex.h>
14 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/regmap.h>
18 #include <linux/slab.h>
20 /* IADC register and bit definition */
21 #define IADC_REVISION2 0x1
22 #define IADC_REVISION2_SUPPORTED_IADC 1
24 #define IADC_PERPH_TYPE 0x4
25 #define IADC_PERPH_TYPE_ADC 8
27 #define IADC_PERPH_SUBTYPE 0x5
28 #define IADC_PERPH_SUBTYPE_IADC 3
30 #define IADC_STATUS1 0x8
31 #define IADC_STATUS1_OP_MODE 4
32 #define IADC_STATUS1_REQ_STS BIT(1)
33 #define IADC_STATUS1_EOC BIT(0)
34 #define IADC_STATUS1_REQ_STS_EOC_MASK 0x3
36 #define IADC_MODE_CTL 0x40
37 #define IADC_OP_MODE_SHIFT 3
38 #define IADC_OP_MODE_NORMAL 0
39 #define IADC_TRIM_EN BIT(0)
41 #define IADC_EN_CTL1 0x46
42 #define IADC_EN_CTL1_SET BIT(7)
44 #define IADC_CH_SEL_CTL 0x48
46 #define IADC_DIG_PARAM 0x50
47 #define IADC_DIG_DEC_RATIO_SEL_SHIFT 2
49 #define IADC_HW_SETTLE_DELAY 0x51
51 #define IADC_CONV_REQ 0x52
52 #define IADC_CONV_REQ_SET BIT(7)
54 #define IADC_FAST_AVG_CTL 0x5a
55 #define IADC_FAST_AVG_EN 0x5b
56 #define IADC_FAST_AVG_EN_SET BIT(7)
58 #define IADC_PERH_RESET_CTL3 0xda
59 #define IADC_FOLLOW_WARM_RB BIT(2)
61 #define IADC_DATA 0x60 /* 16 bits */
63 #define IADC_SEC_ACCESS 0xd0
64 #define IADC_SEC_ACCESS_DATA 0xa5
66 #define IADC_NOMINAL_RSENSE 0xf4
67 #define IADC_NOMINAL_RSENSE_SIGN_MASK BIT(7)
69 #define IADC_REF_GAIN_MICRO_VOLTS 17857
71 #define IADC_INT_RSENSE_DEVIATION 15625 /* nano Ohms per bit */
73 #define IADC_INT_RSENSE_IDEAL_VALUE 10000 /* micro Ohms */
74 #define IADC_INT_RSENSE_DEFAULT_VALUE 7800 /* micro Ohms */
75 #define IADC_INT_RSENSE_DEFAULT_GF 9000 /* micro Ohms */
76 #define IADC_INT_RSENSE_DEFAULT_SMIC 9700 /* micro Ohms */
78 #define IADC_CONV_TIME_MIN_US 2000
79 #define IADC_CONV_TIME_MAX_US 2100
81 #define IADC_DEF_PRESCALING 0 /* 1:1 */
82 #define IADC_DEF_DECIMATION 0 /* 512 */
83 #define IADC_DEF_HW_SETTLE_TIME 0 /* 0 us */
84 #define IADC_DEF_AVG_SAMPLES 0 /* 1 sample */
86 /* IADC channel list */
87 #define IADC_INT_RSENSE 0
88 #define IADC_EXT_RSENSE 1
89 #define IADC_GAIN_17P857MV 3
90 #define IADC_EXT_OFFSET_CSP_CSN 5
91 #define IADC_INT_OFFSET_CSP2_CSN2 6
94 * struct iadc_chip - IADC Current ADC device structure.
95 * @regmap: regmap for register read/write.
96 * @dev: This device pointer.
97 * @base: base offset for the ADC peripheral.
98 * @rsense: Values of the internal and external sense resister in micro Ohms.
99 * @poll_eoc: Poll for end of conversion instead of waiting for IRQ.
100 * @offset: Raw offset values for the internal and external channels.
101 * @gain: Raw gain of the channels.
102 * @lock: ADC lock for access to the peripheral.
103 * @complete: ADC notification after end of conversion interrupt is received.
106 struct regmap *regmap;
114 struct completion complete;
117 static int iadc_read(struct iadc_chip *iadc, u16 offset, u8 *data)
122 ret = regmap_read(iadc->regmap, iadc->base + offset, &val);
130 static int iadc_write(struct iadc_chip *iadc, u16 offset, u8 data)
132 return regmap_write(iadc->regmap, iadc->base + offset, data);
135 static int iadc_reset(struct iadc_chip *iadc)
140 ret = iadc_write(iadc, IADC_SEC_ACCESS, IADC_SEC_ACCESS_DATA);
144 ret = iadc_read(iadc, IADC_PERH_RESET_CTL3, &data);
148 ret = iadc_write(iadc, IADC_SEC_ACCESS, IADC_SEC_ACCESS_DATA);
152 data |= IADC_FOLLOW_WARM_RB;
154 return iadc_write(iadc, IADC_PERH_RESET_CTL3, data);
157 static int iadc_set_state(struct iadc_chip *iadc, bool state)
159 return iadc_write(iadc, IADC_EN_CTL1, state ? IADC_EN_CTL1_SET : 0);
162 static void iadc_status_show(struct iadc_chip *iadc)
164 u8 mode, sta1, chan, dig, en, req;
167 ret = iadc_read(iadc, IADC_MODE_CTL, &mode);
171 ret = iadc_read(iadc, IADC_DIG_PARAM, &dig);
175 ret = iadc_read(iadc, IADC_CH_SEL_CTL, &chan);
179 ret = iadc_read(iadc, IADC_CONV_REQ, &req);
183 ret = iadc_read(iadc, IADC_STATUS1, &sta1);
187 ret = iadc_read(iadc, IADC_EN_CTL1, &en);
192 "mode:%02x en:%02x chan:%02x dig:%02x req:%02x sta1:%02x\n",
193 mode, en, chan, dig, req, sta1);
196 static int iadc_configure(struct iadc_chip *iadc, int channel)
202 mode = (IADC_OP_MODE_NORMAL << IADC_OP_MODE_SHIFT) | IADC_TRIM_EN;
203 ret = iadc_write(iadc, IADC_MODE_CTL, mode);
207 /* Channel selection */
208 ret = iadc_write(iadc, IADC_CH_SEL_CTL, channel);
212 /* Digital parameter setup */
213 decim = IADC_DEF_DECIMATION << IADC_DIG_DEC_RATIO_SEL_SHIFT;
214 ret = iadc_write(iadc, IADC_DIG_PARAM, decim);
218 /* HW settle time delay */
219 ret = iadc_write(iadc, IADC_HW_SETTLE_DELAY, IADC_DEF_HW_SETTLE_TIME);
223 ret = iadc_write(iadc, IADC_FAST_AVG_CTL, IADC_DEF_AVG_SAMPLES);
227 if (IADC_DEF_AVG_SAMPLES)
228 ret = iadc_write(iadc, IADC_FAST_AVG_EN, IADC_FAST_AVG_EN_SET);
230 ret = iadc_write(iadc, IADC_FAST_AVG_EN, 0);
236 reinit_completion(&iadc->complete);
238 ret = iadc_set_state(iadc, true);
242 /* Request conversion */
243 return iadc_write(iadc, IADC_CONV_REQ, IADC_CONV_REQ_SET);
246 static int iadc_poll_wait_eoc(struct iadc_chip *iadc, unsigned int interval_us)
248 unsigned int count, retry;
252 retry = interval_us / IADC_CONV_TIME_MIN_US;
254 for (count = 0; count < retry; count++) {
255 ret = iadc_read(iadc, IADC_STATUS1, &sta1);
259 sta1 &= IADC_STATUS1_REQ_STS_EOC_MASK;
260 if (sta1 == IADC_STATUS1_EOC)
263 usleep_range(IADC_CONV_TIME_MIN_US, IADC_CONV_TIME_MAX_US);
266 iadc_status_show(iadc);
271 static int iadc_read_result(struct iadc_chip *iadc, u16 *data)
273 return regmap_bulk_read(iadc->regmap, iadc->base + IADC_DATA, data, 2);
276 static int iadc_do_conversion(struct iadc_chip *iadc, int chan, u16 *data)
281 ret = iadc_configure(iadc, chan);
285 wait = BIT(IADC_DEF_AVG_SAMPLES) * IADC_CONV_TIME_MIN_US * 2;
287 if (iadc->poll_eoc) {
288 ret = iadc_poll_wait_eoc(iadc, wait);
290 ret = wait_for_completion_timeout(&iadc->complete,
291 usecs_to_jiffies(wait));
295 /* double check conversion status */
296 ret = iadc_poll_wait_eoc(iadc, IADC_CONV_TIME_MIN_US);
300 ret = iadc_read_result(iadc, data);
302 iadc_set_state(iadc, false);
304 dev_err(iadc->dev, "conversion failed\n");
309 static int iadc_read_raw(struct iio_dev *indio_dev,
310 struct iio_chan_spec const *chan,
311 int *val, int *val2, long mask)
313 struct iadc_chip *iadc = iio_priv(indio_dev);
314 s32 isense_ua, vsense_uv;
315 u16 adc_raw, vsense_raw;
319 case IIO_CHAN_INFO_RAW:
320 mutex_lock(&iadc->lock);
321 ret = iadc_do_conversion(iadc, chan->channel, &adc_raw);
322 mutex_unlock(&iadc->lock);
326 vsense_raw = adc_raw - iadc->offset[chan->channel];
328 vsense_uv = vsense_raw * IADC_REF_GAIN_MICRO_VOLTS;
329 vsense_uv /= (s32)iadc->gain - iadc->offset[chan->channel];
331 isense_ua = vsense_uv / iadc->rsense[chan->channel];
333 dev_dbg(iadc->dev, "off %d gain %d adc %d %duV I %duA\n",
334 iadc->offset[chan->channel], iadc->gain,
335 adc_raw, vsense_uv, isense_ua);
339 case IIO_CHAN_INFO_SCALE:
342 return IIO_VAL_INT_PLUS_MICRO;
348 static const struct iio_info iadc_info = {
349 .read_raw = iadc_read_raw,
352 static irqreturn_t iadc_isr(int irq, void *dev_id)
354 struct iadc_chip *iadc = dev_id;
356 complete(&iadc->complete);
361 static int iadc_update_offset(struct iadc_chip *iadc)
365 ret = iadc_do_conversion(iadc, IADC_GAIN_17P857MV, &iadc->gain);
369 ret = iadc_do_conversion(iadc, IADC_INT_OFFSET_CSP2_CSN2,
370 &iadc->offset[IADC_INT_RSENSE]);
374 if (iadc->gain == iadc->offset[IADC_INT_RSENSE]) {
375 dev_err(iadc->dev, "error: internal offset == gain %d\n",
380 ret = iadc_do_conversion(iadc, IADC_EXT_OFFSET_CSP_CSN,
381 &iadc->offset[IADC_EXT_RSENSE]);
385 if (iadc->gain == iadc->offset[IADC_EXT_RSENSE]) {
386 dev_err(iadc->dev, "error: external offset == gain %d\n",
394 static int iadc_version_check(struct iadc_chip *iadc)
399 ret = iadc_read(iadc, IADC_PERPH_TYPE, &val);
403 if (val < IADC_PERPH_TYPE_ADC) {
404 dev_err(iadc->dev, "%d is not ADC\n", val);
408 ret = iadc_read(iadc, IADC_PERPH_SUBTYPE, &val);
412 if (val < IADC_PERPH_SUBTYPE_IADC) {
413 dev_err(iadc->dev, "%d is not IADC\n", val);
417 ret = iadc_read(iadc, IADC_REVISION2, &val);
421 if (val < IADC_REVISION2_SUPPORTED_IADC) {
422 dev_err(iadc->dev, "revision %d not supported\n", val);
429 static int iadc_rsense_read(struct iadc_chip *iadc, struct device_node *node)
431 int ret, sign, int_sense;
434 ret = of_property_read_u32(node, "qcom,external-resistor-micro-ohms",
435 &iadc->rsense[IADC_EXT_RSENSE]);
437 iadc->rsense[IADC_EXT_RSENSE] = IADC_INT_RSENSE_IDEAL_VALUE;
439 if (!iadc->rsense[IADC_EXT_RSENSE]) {
440 dev_err(iadc->dev, "external resistor can't be zero Ohms");
444 ret = iadc_read(iadc, IADC_NOMINAL_RSENSE, &deviation);
449 * Deviation value stored is an offset from 10 mili Ohms, bit 7 is
450 * the sign, the remaining bits have an LSB of 15625 nano Ohms.
452 sign = (deviation & IADC_NOMINAL_RSENSE_SIGN_MASK) ? -1 : 1;
454 deviation &= ~IADC_NOMINAL_RSENSE_SIGN_MASK;
456 /* Scale it to nono Ohms */
457 int_sense = IADC_INT_RSENSE_IDEAL_VALUE * 1000;
458 int_sense += sign * deviation * IADC_INT_RSENSE_DEVIATION;
459 int_sense /= 1000; /* micro Ohms */
461 iadc->rsense[IADC_INT_RSENSE] = int_sense;
465 static const struct iio_chan_spec iadc_channels[] = {
468 .datasheet_name = "INTERNAL_RSENSE",
470 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
471 BIT(IIO_CHAN_INFO_SCALE),
476 .datasheet_name = "EXTERNAL_RSENSE",
478 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
479 BIT(IIO_CHAN_INFO_SCALE),
484 static int iadc_probe(struct platform_device *pdev)
486 struct device_node *node = pdev->dev.of_node;
487 struct device *dev = &pdev->dev;
488 struct iio_dev *indio_dev;
489 struct iadc_chip *iadc;
493 indio_dev = devm_iio_device_alloc(dev, sizeof(*iadc));
497 iadc = iio_priv(indio_dev);
500 iadc->regmap = dev_get_regmap(dev->parent, NULL);
504 init_completion(&iadc->complete);
505 mutex_init(&iadc->lock);
507 ret = of_property_read_u32(node, "reg", &res);
513 ret = iadc_version_check(iadc);
517 ret = iadc_rsense_read(iadc, node);
521 dev_dbg(iadc->dev, "sense resistors %d and %d micro Ohm\n",
522 iadc->rsense[IADC_INT_RSENSE],
523 iadc->rsense[IADC_EXT_RSENSE]);
525 irq_eoc = platform_get_irq(pdev, 0);
526 if (irq_eoc == -EPROBE_DEFER)
530 iadc->poll_eoc = true;
532 ret = iadc_reset(iadc);
534 dev_err(dev, "reset failed\n");
538 if (!iadc->poll_eoc) {
539 ret = devm_request_irq(dev, irq_eoc, iadc_isr, 0,
542 enable_irq_wake(irq_eoc);
546 device_init_wakeup(iadc->dev, 1);
549 ret = iadc_update_offset(iadc);
551 dev_err(dev, "failed offset calibration\n");
555 indio_dev->name = pdev->name;
556 indio_dev->modes = INDIO_DIRECT_MODE;
557 indio_dev->info = &iadc_info;
558 indio_dev->channels = iadc_channels;
559 indio_dev->num_channels = ARRAY_SIZE(iadc_channels);
561 return devm_iio_device_register(dev, indio_dev);
564 static const struct of_device_id iadc_match_table[] = {
565 { .compatible = "qcom,spmi-iadc" },
569 MODULE_DEVICE_TABLE(of, iadc_match_table);
571 static struct platform_driver iadc_driver = {
573 .name = "qcom-spmi-iadc",
574 .of_match_table = iadc_match_table,
579 module_platform_driver(iadc_driver);
581 MODULE_ALIAS("platform:qcom-spmi-iadc");
582 MODULE_DESCRIPTION("Qualcomm SPMI PMIC current ADC driver");
583 MODULE_LICENSE("GPL v2");