1 // SPDX-License-Identifier: GPL-2.0+
3 * ADC driver for the RICOH RN5T618 power management chip family
5 * Copyright (C) 2019 Andreas Kemnade
8 #include <linux/kernel.h>
9 #include <linux/device.h>
10 #include <linux/errno.h>
11 #include <linux/interrupt.h>
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/mfd/rn5t618.h>
15 #include <linux/platform_device.h>
16 #include <linux/completion.h>
17 #include <linux/regmap.h>
18 #include <linux/iio/iio.h>
19 #include <linux/iio/driver.h>
20 #include <linux/iio/machine.h>
21 #include <linux/slab.h>
23 #define RN5T618_ADC_CONVERSION_TIMEOUT (msecs_to_jiffies(500))
24 #define RN5T618_REFERENCE_VOLT 2500
26 /* mask for selecting channels for single conversion */
27 #define RN5T618_ADCCNT3_CHANNEL_MASK 0x7
28 /* average 4-time conversion mode */
29 #define RN5T618_ADCCNT3_AVG BIT(3)
30 /* set for starting a single conversion, gets cleared by hw when done */
31 #define RN5T618_ADCCNT3_GODONE BIT(4)
32 /* automatic conversion, period is in ADCCNT2, selected channels are
35 #define RN5T618_ADCCNT3_AUTO BIT(5)
36 #define RN5T618_ADCEND_IRQ BIT(0)
38 struct rn5t618_adc_data {
40 struct rn5t618 *rn5t618;
41 struct completion conv_completion;
45 struct rn5t618_channel_ratios {
50 enum rn5t618_channels {
61 static const struct rn5t618_channel_ratios rn5t618_ratios[8] = {
62 [LIMMON] = {50, 32}, /* measured across 20mOhm, amplified by 32 */
72 static int rn5t618_read_adc_reg(struct rn5t618 *rn5t618, int reg, u16 *val)
77 ret = regmap_bulk_read(rn5t618->regmap, reg, data, sizeof(data));
81 *val = (data[0] << 4) | (data[1] & 0xF);
86 static irqreturn_t rn5t618_adc_irq(int irq, void *data)
88 struct rn5t618_adc_data *adc = data;
92 /* clear low & high threshold irqs */
93 regmap_write(adc->rn5t618->regmap, RN5T618_IR_ADC1, 0);
94 regmap_write(adc->rn5t618->regmap, RN5T618_IR_ADC2, 0);
96 ret = regmap_read(adc->rn5t618->regmap, RN5T618_IR_ADC3, &r);
98 dev_err(adc->dev, "failed to read IRQ status: %d\n", ret);
100 regmap_write(adc->rn5t618->regmap, RN5T618_IR_ADC3, 0);
102 if (r & RN5T618_ADCEND_IRQ)
103 complete(&adc->conv_completion);
108 static int rn5t618_adc_read(struct iio_dev *iio_dev,
109 const struct iio_chan_spec *chan,
110 int *val, int *val2, long mask)
112 struct rn5t618_adc_data *adc = iio_priv(iio_dev);
116 if (mask == IIO_CHAN_INFO_SCALE) {
117 *val = RN5T618_REFERENCE_VOLT *
118 rn5t618_ratios[chan->channel].numerator;
119 *val2 = rn5t618_ratios[chan->channel].denominator * 4095;
121 return IIO_VAL_FRACTIONAL;
125 ret = regmap_update_bits(adc->rn5t618->regmap, RN5T618_ADCCNT3,
126 RN5T618_ADCCNT3_CHANNEL_MASK,
131 ret = regmap_write(adc->rn5t618->regmap, RN5T618_EN_ADCIR3,
136 ret = regmap_update_bits(adc->rn5t618->regmap, RN5T618_ADCCNT3,
138 mask == IIO_CHAN_INFO_AVERAGE_RAW ?
139 RN5T618_ADCCNT3_AVG : 0);
143 init_completion(&adc->conv_completion);
144 /* single conversion */
145 ret = regmap_update_bits(adc->rn5t618->regmap, RN5T618_ADCCNT3,
146 RN5T618_ADCCNT3_GODONE,
147 RN5T618_ADCCNT3_GODONE);
151 ret = wait_for_completion_timeout(&adc->conv_completion,
152 RN5T618_ADC_CONVERSION_TIMEOUT);
154 dev_warn(adc->dev, "timeout waiting for adc result\n");
158 ret = rn5t618_read_adc_reg(adc->rn5t618,
159 RN5T618_ILIMDATAH + 2 * chan->channel,
169 static const struct iio_info rn5t618_adc_iio_info = {
170 .read_raw = &rn5t618_adc_read,
173 #define RN5T618_ADC_CHANNEL(_channel, _type, _name) { \
175 .channel = _channel, \
176 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
177 BIT(IIO_CHAN_INFO_AVERAGE_RAW) | \
178 BIT(IIO_CHAN_INFO_SCALE), \
179 .datasheet_name = _name, \
183 static const struct iio_chan_spec rn5t618_adc_iio_channels[] = {
184 RN5T618_ADC_CHANNEL(LIMMON, IIO_CURRENT, "LIMMON"),
185 RN5T618_ADC_CHANNEL(VBAT, IIO_VOLTAGE, "VBAT"),
186 RN5T618_ADC_CHANNEL(VADP, IIO_VOLTAGE, "VADP"),
187 RN5T618_ADC_CHANNEL(VUSB, IIO_VOLTAGE, "VUSB"),
188 RN5T618_ADC_CHANNEL(VSYS, IIO_VOLTAGE, "VSYS"),
189 RN5T618_ADC_CHANNEL(VTHM, IIO_VOLTAGE, "VTHM"),
190 RN5T618_ADC_CHANNEL(AIN1, IIO_VOLTAGE, "AIN1"),
191 RN5T618_ADC_CHANNEL(AIN0, IIO_VOLTAGE, "AIN0")
194 static struct iio_map rn5t618_maps[] = {
195 IIO_MAP("VADP", "rn5t618-power", "vadp"),
196 IIO_MAP("VUSB", "rn5t618-power", "vusb"),
200 static int rn5t618_adc_probe(struct platform_device *pdev)
203 struct iio_dev *iio_dev;
204 struct rn5t618_adc_data *adc;
205 struct rn5t618 *rn5t618 = dev_get_drvdata(pdev->dev.parent);
207 iio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*adc));
209 dev_err(&pdev->dev, "failed allocating iio device\n");
213 adc = iio_priv(iio_dev);
214 adc->dev = &pdev->dev;
215 adc->rn5t618 = rn5t618;
217 if (rn5t618->irq_data)
218 adc->irq = regmap_irq_get_virq(rn5t618->irq_data,
222 dev_err(&pdev->dev, "get virq failed\n");
226 init_completion(&adc->conv_completion);
228 iio_dev->name = dev_name(&pdev->dev);
229 iio_dev->info = &rn5t618_adc_iio_info;
230 iio_dev->modes = INDIO_DIRECT_MODE;
231 iio_dev->channels = rn5t618_adc_iio_channels;
232 iio_dev->num_channels = ARRAY_SIZE(rn5t618_adc_iio_channels);
234 /* stop any auto-conversion */
235 ret = regmap_write(rn5t618->regmap, RN5T618_ADCCNT3, 0);
239 platform_set_drvdata(pdev, iio_dev);
241 ret = devm_request_threaded_irq(adc->dev, adc->irq, NULL,
243 IRQF_ONESHOT, dev_name(adc->dev),
246 dev_err(adc->dev, "request irq %d failed: %d\n", adc->irq, ret);
250 ret = devm_iio_map_array_register(adc->dev, iio_dev, rn5t618_maps);
254 return devm_iio_device_register(adc->dev, iio_dev);
257 static struct platform_driver rn5t618_adc_driver = {
259 .name = "rn5t618-adc",
261 .probe = rn5t618_adc_probe,
264 module_platform_driver(rn5t618_adc_driver);
265 MODULE_ALIAS("platform:rn5t618-adc");
266 MODULE_DESCRIPTION("RICOH RN5T618 ADC driver");
267 MODULE_LICENSE("GPL");