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/mutex.h>
22 #include <linux/module.h>
24 #include <linux/of_device.h>
25 #include <linux/platform_device.h>
26 #include <linux/regmap.h>
27 #include <linux/slab.h>
29 /* IADC register and bit definition */
30 #define IADC_REVISION2 0x1
31 #define IADC_REVISION2_SUPPORTED_IADC 1
33 #define IADC_PERPH_TYPE 0x4
34 #define IADC_PERPH_TYPE_ADC 8
36 #define IADC_PERPH_SUBTYPE 0x5
37 #define IADC_PERPH_SUBTYPE_IADC 3
39 #define IADC_STATUS1 0x8
40 #define IADC_STATUS1_OP_MODE 4
41 #define IADC_STATUS1_REQ_STS BIT(1)
42 #define IADC_STATUS1_EOC BIT(0)
43 #define IADC_STATUS1_REQ_STS_EOC_MASK 0x3
45 #define IADC_MODE_CTL 0x40
46 #define IADC_OP_MODE_SHIFT 3
47 #define IADC_OP_MODE_NORMAL 0
48 #define IADC_TRIM_EN BIT(0)
50 #define IADC_EN_CTL1 0x46
51 #define IADC_EN_CTL1_SET BIT(7)
53 #define IADC_CH_SEL_CTL 0x48
55 #define IADC_DIG_PARAM 0x50
56 #define IADC_DIG_DEC_RATIO_SEL_SHIFT 2
58 #define IADC_HW_SETTLE_DELAY 0x51
60 #define IADC_CONV_REQ 0x52
61 #define IADC_CONV_REQ_SET BIT(7)
63 #define IADC_FAST_AVG_CTL 0x5a
64 #define IADC_FAST_AVG_EN 0x5b
65 #define IADC_FAST_AVG_EN_SET BIT(7)
67 #define IADC_PERH_RESET_CTL3 0xda
68 #define IADC_FOLLOW_WARM_RB BIT(2)
70 #define IADC_DATA 0x60 /* 16 bits */
72 #define IADC_SEC_ACCESS 0xd0
73 #define IADC_SEC_ACCESS_DATA 0xa5
75 #define IADC_NOMINAL_RSENSE 0xf4
76 #define IADC_NOMINAL_RSENSE_SIGN_MASK BIT(7)
78 #define IADC_REF_GAIN_MICRO_VOLTS 17857
80 #define IADC_INT_RSENSE_DEVIATION 15625 /* nano Ohms per bit */
82 #define IADC_INT_RSENSE_IDEAL_VALUE 10000 /* micro Ohms */
83 #define IADC_INT_RSENSE_DEFAULT_VALUE 7800 /* micro Ohms */
84 #define IADC_INT_RSENSE_DEFAULT_GF 9000 /* micro Ohms */
85 #define IADC_INT_RSENSE_DEFAULT_SMIC 9700 /* micro Ohms */
87 #define IADC_CONV_TIME_MIN_US 2000
88 #define IADC_CONV_TIME_MAX_US 2100
90 #define IADC_DEF_PRESCALING 0 /* 1:1 */
91 #define IADC_DEF_DECIMATION 0 /* 512 */
92 #define IADC_DEF_HW_SETTLE_TIME 0 /* 0 us */
93 #define IADC_DEF_AVG_SAMPLES 0 /* 1 sample */
95 /* IADC channel list */
96 #define IADC_INT_RSENSE 0
97 #define IADC_EXT_RSENSE 1
98 #define IADC_GAIN_17P857MV 3
99 #define IADC_EXT_OFFSET_CSP_CSN 5
100 #define IADC_INT_OFFSET_CSP2_CSN2 6
103 * struct iadc_chip - IADC Current ADC device structure.
104 * @regmap: regmap for register read/write.
105 * @dev: This device pointer.
106 * @base: base offset for the ADC peripheral.
107 * @rsense: Values of the internal and external sense resister in micro Ohms.
108 * @poll_eoc: Poll for end of conversion instead of waiting for IRQ.
109 * @offset: Raw offset values for the internal and external channels.
110 * @gain: Raw gain of the channels.
111 * @lock: ADC lock for access to the peripheral.
112 * @complete: ADC notification after end of conversion interrupt is received.
115 struct regmap *regmap;
123 struct completion complete;
126 static int iadc_read(struct iadc_chip *iadc, u16 offset, u8 *data)
131 ret = regmap_read(iadc->regmap, iadc->base + offset, &val);
139 static int iadc_write(struct iadc_chip *iadc, u16 offset, u8 data)
141 return regmap_write(iadc->regmap, iadc->base + offset, data);
144 static int iadc_reset(struct iadc_chip *iadc)
149 ret = iadc_write(iadc, IADC_SEC_ACCESS, IADC_SEC_ACCESS_DATA);
153 ret = iadc_read(iadc, IADC_PERH_RESET_CTL3, &data);
157 ret = iadc_write(iadc, IADC_SEC_ACCESS, IADC_SEC_ACCESS_DATA);
161 data |= IADC_FOLLOW_WARM_RB;
163 return iadc_write(iadc, IADC_PERH_RESET_CTL3, data);
166 static int iadc_set_state(struct iadc_chip *iadc, bool state)
168 return iadc_write(iadc, IADC_EN_CTL1, state ? IADC_EN_CTL1_SET : 0);
171 static void iadc_status_show(struct iadc_chip *iadc)
173 u8 mode, sta1, chan, dig, en, req;
176 ret = iadc_read(iadc, IADC_MODE_CTL, &mode);
180 ret = iadc_read(iadc, IADC_DIG_PARAM, &dig);
184 ret = iadc_read(iadc, IADC_CH_SEL_CTL, &chan);
188 ret = iadc_read(iadc, IADC_CONV_REQ, &req);
192 ret = iadc_read(iadc, IADC_STATUS1, &sta1);
196 ret = iadc_read(iadc, IADC_EN_CTL1, &en);
201 "mode:%02x en:%02x chan:%02x dig:%02x req:%02x sta1:%02x\n",
202 mode, en, chan, dig, req, sta1);
205 static int iadc_configure(struct iadc_chip *iadc, int channel)
211 mode = (IADC_OP_MODE_NORMAL << IADC_OP_MODE_SHIFT) | IADC_TRIM_EN;
212 ret = iadc_write(iadc, IADC_MODE_CTL, mode);
216 /* Channel selection */
217 ret = iadc_write(iadc, IADC_CH_SEL_CTL, channel);
221 /* Digital parameter setup */
222 decim = IADC_DEF_DECIMATION << IADC_DIG_DEC_RATIO_SEL_SHIFT;
223 ret = iadc_write(iadc, IADC_DIG_PARAM, decim);
227 /* HW settle time delay */
228 ret = iadc_write(iadc, IADC_HW_SETTLE_DELAY, IADC_DEF_HW_SETTLE_TIME);
232 ret = iadc_write(iadc, IADC_FAST_AVG_CTL, IADC_DEF_AVG_SAMPLES);
236 if (IADC_DEF_AVG_SAMPLES)
237 ret = iadc_write(iadc, IADC_FAST_AVG_EN, IADC_FAST_AVG_EN_SET);
239 ret = iadc_write(iadc, IADC_FAST_AVG_EN, 0);
245 reinit_completion(&iadc->complete);
247 ret = iadc_set_state(iadc, true);
251 /* Request conversion */
252 return iadc_write(iadc, IADC_CONV_REQ, IADC_CONV_REQ_SET);
255 static int iadc_poll_wait_eoc(struct iadc_chip *iadc, unsigned int interval_us)
257 unsigned int count, retry;
261 retry = interval_us / IADC_CONV_TIME_MIN_US;
263 for (count = 0; count < retry; count++) {
264 ret = iadc_read(iadc, IADC_STATUS1, &sta1);
268 sta1 &= IADC_STATUS1_REQ_STS_EOC_MASK;
269 if (sta1 == IADC_STATUS1_EOC)
272 usleep_range(IADC_CONV_TIME_MIN_US, IADC_CONV_TIME_MAX_US);
275 iadc_status_show(iadc);
280 static int iadc_read_result(struct iadc_chip *iadc, u16 *data)
282 return regmap_bulk_read(iadc->regmap, iadc->base + IADC_DATA, data, 2);
285 static int iadc_do_conversion(struct iadc_chip *iadc, int chan, u16 *data)
290 ret = iadc_configure(iadc, chan);
294 wait = BIT(IADC_DEF_AVG_SAMPLES) * IADC_CONV_TIME_MIN_US * 2;
296 if (iadc->poll_eoc) {
297 ret = iadc_poll_wait_eoc(iadc, wait);
299 ret = wait_for_completion_timeout(&iadc->complete,
300 usecs_to_jiffies(wait));
304 /* double check conversion status */
305 ret = iadc_poll_wait_eoc(iadc, IADC_CONV_TIME_MIN_US);
309 ret = iadc_read_result(iadc, data);
311 iadc_set_state(iadc, false);
313 dev_err(iadc->dev, "conversion failed\n");
318 static int iadc_read_raw(struct iio_dev *indio_dev,
319 struct iio_chan_spec const *chan,
320 int *val, int *val2, long mask)
322 struct iadc_chip *iadc = iio_priv(indio_dev);
323 s32 isense_ua, vsense_uv;
324 u16 adc_raw, vsense_raw;
328 case IIO_CHAN_INFO_RAW:
329 mutex_lock(&iadc->lock);
330 ret = iadc_do_conversion(iadc, chan->channel, &adc_raw);
331 mutex_unlock(&iadc->lock);
335 vsense_raw = adc_raw - iadc->offset[chan->channel];
337 vsense_uv = vsense_raw * IADC_REF_GAIN_MICRO_VOLTS;
338 vsense_uv /= (s32)iadc->gain - iadc->offset[chan->channel];
340 isense_ua = vsense_uv / iadc->rsense[chan->channel];
342 dev_dbg(iadc->dev, "off %d gain %d adc %d %duV I %duA\n",
343 iadc->offset[chan->channel], iadc->gain,
344 adc_raw, vsense_uv, isense_ua);
348 case IIO_CHAN_INFO_SCALE:
351 return IIO_VAL_INT_PLUS_MICRO;
357 static const struct iio_info iadc_info = {
358 .read_raw = iadc_read_raw,
359 .driver_module = THIS_MODULE,
362 static irqreturn_t iadc_isr(int irq, void *dev_id)
364 struct iadc_chip *iadc = dev_id;
366 complete(&iadc->complete);
371 static int iadc_update_offset(struct iadc_chip *iadc)
375 ret = iadc_do_conversion(iadc, IADC_GAIN_17P857MV, &iadc->gain);
379 ret = iadc_do_conversion(iadc, IADC_INT_OFFSET_CSP2_CSN2,
380 &iadc->offset[IADC_INT_RSENSE]);
384 if (iadc->gain == iadc->offset[IADC_INT_RSENSE]) {
385 dev_err(iadc->dev, "error: internal offset == gain %d\n",
390 ret = iadc_do_conversion(iadc, IADC_EXT_OFFSET_CSP_CSN,
391 &iadc->offset[IADC_EXT_RSENSE]);
395 if (iadc->gain == iadc->offset[IADC_EXT_RSENSE]) {
396 dev_err(iadc->dev, "error: external offset == gain %d\n",
404 static int iadc_version_check(struct iadc_chip *iadc)
409 ret = iadc_read(iadc, IADC_PERPH_TYPE, &val);
413 if (val < IADC_PERPH_TYPE_ADC) {
414 dev_err(iadc->dev, "%d is not ADC\n", val);
418 ret = iadc_read(iadc, IADC_PERPH_SUBTYPE, &val);
422 if (val < IADC_PERPH_SUBTYPE_IADC) {
423 dev_err(iadc->dev, "%d is not IADC\n", val);
427 ret = iadc_read(iadc, IADC_REVISION2, &val);
431 if (val < IADC_REVISION2_SUPPORTED_IADC) {
432 dev_err(iadc->dev, "revision %d not supported\n", val);
439 static int iadc_rsense_read(struct iadc_chip *iadc, struct device_node *node)
441 int ret, sign, int_sense;
444 ret = of_property_read_u32(node, "qcom,external-resistor-micro-ohms",
445 &iadc->rsense[IADC_EXT_RSENSE]);
447 iadc->rsense[IADC_EXT_RSENSE] = IADC_INT_RSENSE_IDEAL_VALUE;
449 if (!iadc->rsense[IADC_EXT_RSENSE]) {
450 dev_err(iadc->dev, "external resistor can't be zero Ohms");
454 ret = iadc_read(iadc, IADC_NOMINAL_RSENSE, &deviation);
459 * Deviation value stored is an offset from 10 mili Ohms, bit 7 is
460 * the sign, the remaining bits have an LSB of 15625 nano Ohms.
462 sign = (deviation & IADC_NOMINAL_RSENSE_SIGN_MASK) ? -1 : 1;
464 deviation &= ~IADC_NOMINAL_RSENSE_SIGN_MASK;
466 /* Scale it to nono Ohms */
467 int_sense = IADC_INT_RSENSE_IDEAL_VALUE * 1000;
468 int_sense += sign * deviation * IADC_INT_RSENSE_DEVIATION;
469 int_sense /= 1000; /* micro Ohms */
471 iadc->rsense[IADC_INT_RSENSE] = int_sense;
475 static const struct iio_chan_spec iadc_channels[] = {
478 .datasheet_name = "INTERNAL_RSENSE",
480 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
481 BIT(IIO_CHAN_INFO_SCALE),
486 .datasheet_name = "EXTERNAL_RSENSE",
488 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
489 BIT(IIO_CHAN_INFO_SCALE),
494 static int iadc_probe(struct platform_device *pdev)
496 struct device_node *node = pdev->dev.of_node;
497 struct device *dev = &pdev->dev;
498 struct iio_dev *indio_dev;
499 struct iadc_chip *iadc;
503 indio_dev = devm_iio_device_alloc(dev, sizeof(*iadc));
507 iadc = iio_priv(indio_dev);
510 iadc->regmap = dev_get_regmap(dev->parent, NULL);
514 init_completion(&iadc->complete);
515 mutex_init(&iadc->lock);
517 ret = of_property_read_u32(node, "reg", &res);
523 ret = iadc_version_check(iadc);
527 ret = iadc_rsense_read(iadc, node);
531 dev_dbg(iadc->dev, "sense resistors %d and %d micro Ohm\n",
532 iadc->rsense[IADC_INT_RSENSE],
533 iadc->rsense[IADC_EXT_RSENSE]);
535 irq_eoc = platform_get_irq(pdev, 0);
536 if (irq_eoc == -EPROBE_DEFER)
540 iadc->poll_eoc = true;
542 ret = iadc_reset(iadc);
544 dev_err(dev, "reset failed\n");
548 if (!iadc->poll_eoc) {
549 ret = devm_request_irq(dev, irq_eoc, iadc_isr, 0,
552 enable_irq_wake(irq_eoc);
556 device_init_wakeup(iadc->dev, 1);
559 ret = iadc_update_offset(iadc);
561 dev_err(dev, "failed offset calibration\n");
565 indio_dev->dev.parent = dev;
566 indio_dev->dev.of_node = node;
567 indio_dev->name = pdev->name;
568 indio_dev->modes = INDIO_DIRECT_MODE;
569 indio_dev->info = &iadc_info;
570 indio_dev->channels = iadc_channels;
571 indio_dev->num_channels = ARRAY_SIZE(iadc_channels);
573 return devm_iio_device_register(dev, indio_dev);
576 static const struct of_device_id iadc_match_table[] = {
577 { .compatible = "qcom,spmi-iadc" },
581 MODULE_DEVICE_TABLE(of, iadc_match_table);
583 static struct platform_driver iadc_driver = {
585 .name = "qcom-spmi-iadc",
586 .of_match_table = iadc_match_table,
591 module_platform_driver(iadc_driver);
593 MODULE_ALIAS("platform:qcom-spmi-iadc");
594 MODULE_DESCRIPTION("Qualcomm SPMI PMIC current ADC driver");
595 MODULE_LICENSE("GPL v2");
596 MODULE_AUTHOR("Ivan T. Ivanov <iivanov@mm-sol.com>");