xref: /openbmc/linux/drivers/iio/adc/rn5t618-adc.c (revision e79b548b)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * ADC driver for the RICOH RN5T618 power management chip family
4  *
5  * Copyright (C) 2019 Andreas Kemnade
6  */
7 
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>
22 
23 #define RN5T618_ADC_CONVERSION_TIMEOUT   (msecs_to_jiffies(500))
24 #define RN5T618_REFERENCE_VOLT 2500
25 
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
33  * in ADCCNT1
34  */
35 #define RN5T618_ADCCNT3_AUTO BIT(5)
36 #define RN5T618_ADCEND_IRQ BIT(0)
37 
38 struct rn5t618_adc_data {
39 	struct device *dev;
40 	struct rn5t618 *rn5t618;
41 	struct completion conv_completion;
42 	int irq;
43 };
44 
45 enum rn5t618_channels {
46 	LIMMON = 0,
47 	VBAT,
48 	VADP,
49 	VUSB,
50 	VSYS,
51 	VTHM,
52 	AIN1,
53 	AIN0
54 };
55 
56 static const struct u16_fract rn5t618_ratios[8] = {
57 	[LIMMON] = {50, 32}, /* measured across 20mOhm, amplified by 32 */
58 	[VBAT] = {2, 1},
59 	[VADP] = {3, 1},
60 	[VUSB] = {3, 1},
61 	[VSYS] = {3, 1},
62 	[VTHM] = {1, 1},
63 	[AIN1] = {1, 1},
64 	[AIN0] = {1, 1},
65 };
66 
67 static int rn5t618_read_adc_reg(struct rn5t618 *rn5t618, int reg, u16 *val)
68 {
69 	u8 data[2];
70 	int ret;
71 
72 	ret = regmap_bulk_read(rn5t618->regmap, reg, data, sizeof(data));
73 	if (ret < 0)
74 		return ret;
75 
76 	*val = (data[0] << 4) | (data[1] & 0xF);
77 
78 	return 0;
79 }
80 
81 static irqreturn_t rn5t618_adc_irq(int irq, void *data)
82 {
83 	struct rn5t618_adc_data *adc = data;
84 	unsigned int r = 0;
85 	int ret;
86 
87 	/* clear low & high threshold irqs */
88 	regmap_write(adc->rn5t618->regmap, RN5T618_IR_ADC1, 0);
89 	regmap_write(adc->rn5t618->regmap, RN5T618_IR_ADC2, 0);
90 
91 	ret = regmap_read(adc->rn5t618->regmap, RN5T618_IR_ADC3, &r);
92 	if (ret < 0)
93 		dev_err(adc->dev, "failed to read IRQ status: %d\n", ret);
94 
95 	regmap_write(adc->rn5t618->regmap, RN5T618_IR_ADC3, 0);
96 
97 	if (r & RN5T618_ADCEND_IRQ)
98 		complete(&adc->conv_completion);
99 
100 	return IRQ_HANDLED;
101 }
102 
103 static int rn5t618_adc_read(struct iio_dev *iio_dev,
104 			    const struct iio_chan_spec *chan,
105 			    int *val, int *val2, long mask)
106 {
107 	struct rn5t618_adc_data *adc = iio_priv(iio_dev);
108 	u16 raw;
109 	int ret;
110 
111 	if (mask == IIO_CHAN_INFO_SCALE) {
112 		*val = RN5T618_REFERENCE_VOLT *
113 		       rn5t618_ratios[chan->channel].numerator;
114 		*val2 = rn5t618_ratios[chan->channel].denominator * 4095;
115 
116 		return IIO_VAL_FRACTIONAL;
117 	}
118 
119 	/* select channel */
120 	ret = regmap_update_bits(adc->rn5t618->regmap, RN5T618_ADCCNT3,
121 				 RN5T618_ADCCNT3_CHANNEL_MASK,
122 				 chan->channel);
123 	if (ret < 0)
124 		return ret;
125 
126 	ret = regmap_write(adc->rn5t618->regmap, RN5T618_EN_ADCIR3,
127 			   RN5T618_ADCEND_IRQ);
128 	if (ret < 0)
129 		return ret;
130 
131 	ret = regmap_update_bits(adc->rn5t618->regmap, RN5T618_ADCCNT3,
132 				 RN5T618_ADCCNT3_AVG,
133 				 mask == IIO_CHAN_INFO_AVERAGE_RAW ?
134 				 RN5T618_ADCCNT3_AVG : 0);
135 	if (ret < 0)
136 		return ret;
137 
138 	init_completion(&adc->conv_completion);
139 	/* single conversion */
140 	ret = regmap_update_bits(adc->rn5t618->regmap, RN5T618_ADCCNT3,
141 				 RN5T618_ADCCNT3_GODONE,
142 				 RN5T618_ADCCNT3_GODONE);
143 	if (ret < 0)
144 		return ret;
145 
146 	ret = wait_for_completion_timeout(&adc->conv_completion,
147 					  RN5T618_ADC_CONVERSION_TIMEOUT);
148 	if (ret == 0) {
149 		dev_warn(adc->dev, "timeout waiting for adc result\n");
150 		return -ETIMEDOUT;
151 	}
152 
153 	ret = rn5t618_read_adc_reg(adc->rn5t618,
154 				   RN5T618_ILIMDATAH + 2 * chan->channel,
155 				   &raw);
156 	if (ret < 0)
157 		return ret;
158 
159 	*val = raw;
160 
161 	return IIO_VAL_INT;
162 }
163 
164 static const struct iio_info rn5t618_adc_iio_info = {
165 	.read_raw = &rn5t618_adc_read,
166 };
167 
168 #define RN5T618_ADC_CHANNEL(_channel, _type, _name) { \
169 	.type = _type, \
170 	.channel = _channel, \
171 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
172 			      BIT(IIO_CHAN_INFO_AVERAGE_RAW) | \
173 			      BIT(IIO_CHAN_INFO_SCALE), \
174 	.datasheet_name = _name, \
175 	.indexed = 1. \
176 }
177 
178 static const struct iio_chan_spec rn5t618_adc_iio_channels[] = {
179 	RN5T618_ADC_CHANNEL(LIMMON, IIO_CURRENT, "LIMMON"),
180 	RN5T618_ADC_CHANNEL(VBAT, IIO_VOLTAGE, "VBAT"),
181 	RN5T618_ADC_CHANNEL(VADP, IIO_VOLTAGE, "VADP"),
182 	RN5T618_ADC_CHANNEL(VUSB, IIO_VOLTAGE, "VUSB"),
183 	RN5T618_ADC_CHANNEL(VSYS, IIO_VOLTAGE, "VSYS"),
184 	RN5T618_ADC_CHANNEL(VTHM, IIO_VOLTAGE, "VTHM"),
185 	RN5T618_ADC_CHANNEL(AIN1, IIO_VOLTAGE, "AIN1"),
186 	RN5T618_ADC_CHANNEL(AIN0, IIO_VOLTAGE, "AIN0")
187 };
188 
189 static struct iio_map rn5t618_maps[] = {
190 	IIO_MAP("VADP", "rn5t618-power", "vadp"),
191 	IIO_MAP("VUSB", "rn5t618-power", "vusb"),
192 	{ /* sentinel */ }
193 };
194 
195 static int rn5t618_adc_probe(struct platform_device *pdev)
196 {
197 	int ret;
198 	struct iio_dev *iio_dev;
199 	struct rn5t618_adc_data *adc;
200 	struct rn5t618 *rn5t618 = dev_get_drvdata(pdev->dev.parent);
201 
202 	iio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*adc));
203 	if (!iio_dev) {
204 		dev_err(&pdev->dev, "failed allocating iio device\n");
205 		return -ENOMEM;
206 	}
207 
208 	adc = iio_priv(iio_dev);
209 	adc->dev = &pdev->dev;
210 	adc->rn5t618 = rn5t618;
211 
212 	if (rn5t618->irq_data)
213 		adc->irq = regmap_irq_get_virq(rn5t618->irq_data,
214 					       RN5T618_IRQ_ADC);
215 
216 	if (adc->irq <= 0) {
217 		dev_err(&pdev->dev, "get virq failed\n");
218 		return -EINVAL;
219 	}
220 
221 	init_completion(&adc->conv_completion);
222 
223 	iio_dev->name = dev_name(&pdev->dev);
224 	iio_dev->info = &rn5t618_adc_iio_info;
225 	iio_dev->modes = INDIO_DIRECT_MODE;
226 	iio_dev->channels = rn5t618_adc_iio_channels;
227 	iio_dev->num_channels = ARRAY_SIZE(rn5t618_adc_iio_channels);
228 
229 	/* stop any auto-conversion */
230 	ret = regmap_write(rn5t618->regmap, RN5T618_ADCCNT3, 0);
231 	if (ret < 0)
232 		return ret;
233 
234 	platform_set_drvdata(pdev, iio_dev);
235 
236 	ret = devm_request_threaded_irq(adc->dev, adc->irq, NULL,
237 					rn5t618_adc_irq,
238 					IRQF_ONESHOT, dev_name(adc->dev),
239 					adc);
240 	if (ret < 0) {
241 		dev_err(adc->dev, "request irq %d failed: %d\n", adc->irq, ret);
242 		return ret;
243 	}
244 
245 	ret = devm_iio_map_array_register(adc->dev, iio_dev, rn5t618_maps);
246 	if (ret < 0)
247 		return ret;
248 
249 	return devm_iio_device_register(adc->dev, iio_dev);
250 }
251 
252 static struct platform_driver rn5t618_adc_driver = {
253 	.driver = {
254 		.name   = "rn5t618-adc",
255 	},
256 	.probe = rn5t618_adc_probe,
257 };
258 
259 module_platform_driver(rn5t618_adc_driver);
260 MODULE_ALIAS("platform:rn5t618-adc");
261 MODULE_DESCRIPTION("RICOH RN5T618 ADC driver");
262 MODULE_LICENSE("GPL");
263