xref: /openbmc/linux/drivers/iio/adc/ti-adc0832.c (revision dfc53baa)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * ADC0831/ADC0832/ADC0834/ADC0838 8-bit ADC driver
4  *
5  * Copyright (c) 2016 Akinobu Mita <akinobu.mita@gmail.com>
6  *
7  * Datasheet: https://www.ti.com/lit/ds/symlink/adc0832-n.pdf
8  */
9 
10 #include <linux/module.h>
11 #include <linux/mod_devicetable.h>
12 #include <linux/spi/spi.h>
13 #include <linux/iio/iio.h>
14 #include <linux/regulator/consumer.h>
15 #include <linux/iio/buffer.h>
16 #include <linux/iio/trigger.h>
17 #include <linux/iio/triggered_buffer.h>
18 #include <linux/iio/trigger_consumer.h>
19 
20 enum {
21 	adc0831,
22 	adc0832,
23 	adc0834,
24 	adc0838,
25 };
26 
27 struct adc0832 {
28 	struct spi_device *spi;
29 	struct regulator *reg;
30 	struct mutex lock;
31 	u8 mux_bits;
32 
33 	u8 tx_buf[2] ____cacheline_aligned;
34 	u8 rx_buf[2];
35 };
36 
37 #define ADC0832_VOLTAGE_CHANNEL(chan)					\
38 	{								\
39 		.type = IIO_VOLTAGE,					\
40 		.indexed = 1,						\
41 		.channel = chan,					\
42 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
43 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),	\
44 		.scan_index = chan,					\
45 		.scan_type = {						\
46 			.sign = 'u',					\
47 			.realbits = 8,					\
48 			.storagebits = 8,				\
49 		},							\
50 	}
51 
52 #define ADC0832_VOLTAGE_CHANNEL_DIFF(chan1, chan2, si)			\
53 	{								\
54 		.type = IIO_VOLTAGE,					\
55 		.indexed = 1,						\
56 		.channel = (chan1),					\
57 		.channel2 = (chan2),					\
58 		.differential = 1,					\
59 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
60 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),	\
61 		.scan_index = si,					\
62 		.scan_type = {						\
63 			.sign = 'u',					\
64 			.realbits = 8,					\
65 			.storagebits = 8,				\
66 		},							\
67 	}
68 
69 static const struct iio_chan_spec adc0831_channels[] = {
70 	ADC0832_VOLTAGE_CHANNEL_DIFF(0, 1, 0),
71 	IIO_CHAN_SOFT_TIMESTAMP(1),
72 };
73 
74 static const struct iio_chan_spec adc0832_channels[] = {
75 	ADC0832_VOLTAGE_CHANNEL(0),
76 	ADC0832_VOLTAGE_CHANNEL(1),
77 	ADC0832_VOLTAGE_CHANNEL_DIFF(0, 1, 2),
78 	ADC0832_VOLTAGE_CHANNEL_DIFF(1, 0, 3),
79 	IIO_CHAN_SOFT_TIMESTAMP(4),
80 };
81 
82 static const struct iio_chan_spec adc0834_channels[] = {
83 	ADC0832_VOLTAGE_CHANNEL(0),
84 	ADC0832_VOLTAGE_CHANNEL(1),
85 	ADC0832_VOLTAGE_CHANNEL(2),
86 	ADC0832_VOLTAGE_CHANNEL(3),
87 	ADC0832_VOLTAGE_CHANNEL_DIFF(0, 1, 4),
88 	ADC0832_VOLTAGE_CHANNEL_DIFF(1, 0, 5),
89 	ADC0832_VOLTAGE_CHANNEL_DIFF(2, 3, 6),
90 	ADC0832_VOLTAGE_CHANNEL_DIFF(3, 2, 7),
91 	IIO_CHAN_SOFT_TIMESTAMP(8),
92 };
93 
94 static const struct iio_chan_spec adc0838_channels[] = {
95 	ADC0832_VOLTAGE_CHANNEL(0),
96 	ADC0832_VOLTAGE_CHANNEL(1),
97 	ADC0832_VOLTAGE_CHANNEL(2),
98 	ADC0832_VOLTAGE_CHANNEL(3),
99 	ADC0832_VOLTAGE_CHANNEL(4),
100 	ADC0832_VOLTAGE_CHANNEL(5),
101 	ADC0832_VOLTAGE_CHANNEL(6),
102 	ADC0832_VOLTAGE_CHANNEL(7),
103 	ADC0832_VOLTAGE_CHANNEL_DIFF(0, 1, 8),
104 	ADC0832_VOLTAGE_CHANNEL_DIFF(1, 0, 9),
105 	ADC0832_VOLTAGE_CHANNEL_DIFF(2, 3, 10),
106 	ADC0832_VOLTAGE_CHANNEL_DIFF(3, 2, 11),
107 	ADC0832_VOLTAGE_CHANNEL_DIFF(4, 5, 12),
108 	ADC0832_VOLTAGE_CHANNEL_DIFF(5, 4, 13),
109 	ADC0832_VOLTAGE_CHANNEL_DIFF(6, 7, 14),
110 	ADC0832_VOLTAGE_CHANNEL_DIFF(7, 6, 15),
111 	IIO_CHAN_SOFT_TIMESTAMP(16),
112 };
113 
114 static int adc0831_adc_conversion(struct adc0832 *adc)
115 {
116 	struct spi_device *spi = adc->spi;
117 	int ret;
118 
119 	ret = spi_read(spi, &adc->rx_buf, 2);
120 	if (ret)
121 		return ret;
122 
123 	/*
124 	 * Skip TRI-STATE and a leading zero
125 	 */
126 	return (adc->rx_buf[0] << 2 & 0xff) | (adc->rx_buf[1] >> 6);
127 }
128 
129 static int adc0832_adc_conversion(struct adc0832 *adc, int channel,
130 				bool differential)
131 {
132 	struct spi_device *spi = adc->spi;
133 	struct spi_transfer xfer = {
134 		.tx_buf = adc->tx_buf,
135 		.rx_buf = adc->rx_buf,
136 		.len = 2,
137 	};
138 	int ret;
139 
140 	if (!adc->mux_bits)
141 		return adc0831_adc_conversion(adc);
142 
143 	/* start bit */
144 	adc->tx_buf[0] = 1 << (adc->mux_bits + 1);
145 	/* single-ended or differential */
146 	adc->tx_buf[0] |= differential ? 0 : (1 << adc->mux_bits);
147 	/* odd / sign */
148 	adc->tx_buf[0] |= (channel % 2) << (adc->mux_bits - 1);
149 	/* select */
150 	if (adc->mux_bits > 1)
151 		adc->tx_buf[0] |= channel / 2;
152 
153 	/* align Data output BIT7 (MSB) to 8-bit boundary */
154 	adc->tx_buf[0] <<= 1;
155 
156 	ret = spi_sync_transfer(spi, &xfer, 1);
157 	if (ret)
158 		return ret;
159 
160 	return adc->rx_buf[1];
161 }
162 
163 static int adc0832_read_raw(struct iio_dev *iio,
164 			struct iio_chan_spec const *channel, int *value,
165 			int *shift, long mask)
166 {
167 	struct adc0832 *adc = iio_priv(iio);
168 
169 	switch (mask) {
170 	case IIO_CHAN_INFO_RAW:
171 		mutex_lock(&adc->lock);
172 		*value = adc0832_adc_conversion(adc, channel->channel,
173 						channel->differential);
174 		mutex_unlock(&adc->lock);
175 		if (*value < 0)
176 			return *value;
177 
178 		return IIO_VAL_INT;
179 	case IIO_CHAN_INFO_SCALE:
180 		*value = regulator_get_voltage(adc->reg);
181 		if (*value < 0)
182 			return *value;
183 
184 		/* convert regulator output voltage to mV */
185 		*value /= 1000;
186 		*shift = 8;
187 
188 		return IIO_VAL_FRACTIONAL_LOG2;
189 	}
190 
191 	return -EINVAL;
192 }
193 
194 static const struct iio_info adc0832_info = {
195 	.read_raw = adc0832_read_raw,
196 };
197 
198 static irqreturn_t adc0832_trigger_handler(int irq, void *p)
199 {
200 	struct iio_poll_func *pf = p;
201 	struct iio_dev *indio_dev = pf->indio_dev;
202 	struct adc0832 *adc = iio_priv(indio_dev);
203 	u8 data[24] = { }; /* 16x 1 byte ADC data + 8 bytes timestamp */
204 	int scan_index;
205 	int i = 0;
206 
207 	mutex_lock(&adc->lock);
208 
209 	for_each_set_bit(scan_index, indio_dev->active_scan_mask,
210 			 indio_dev->masklength) {
211 		const struct iio_chan_spec *scan_chan =
212 				&indio_dev->channels[scan_index];
213 		int ret = adc0832_adc_conversion(adc, scan_chan->channel,
214 						 scan_chan->differential);
215 		if (ret < 0) {
216 			dev_warn(&adc->spi->dev,
217 				 "failed to get conversion data\n");
218 			goto out;
219 		}
220 
221 		data[i] = ret;
222 		i++;
223 	}
224 	iio_push_to_buffers_with_timestamp(indio_dev, data,
225 					   iio_get_time_ns(indio_dev));
226 out:
227 	mutex_unlock(&adc->lock);
228 
229 	iio_trigger_notify_done(indio_dev->trig);
230 
231 	return IRQ_HANDLED;
232 }
233 
234 static int adc0832_probe(struct spi_device *spi)
235 {
236 	struct iio_dev *indio_dev;
237 	struct adc0832 *adc;
238 	int ret;
239 
240 	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc));
241 	if (!indio_dev)
242 		return -ENOMEM;
243 
244 	adc = iio_priv(indio_dev);
245 	adc->spi = spi;
246 	mutex_init(&adc->lock);
247 
248 	indio_dev->name = spi_get_device_id(spi)->name;
249 	indio_dev->info = &adc0832_info;
250 	indio_dev->modes = INDIO_DIRECT_MODE;
251 
252 	switch (spi_get_device_id(spi)->driver_data) {
253 	case adc0831:
254 		adc->mux_bits = 0;
255 		indio_dev->channels = adc0831_channels;
256 		indio_dev->num_channels = ARRAY_SIZE(adc0831_channels);
257 		break;
258 	case adc0832:
259 		adc->mux_bits = 1;
260 		indio_dev->channels = adc0832_channels;
261 		indio_dev->num_channels = ARRAY_SIZE(adc0832_channels);
262 		break;
263 	case adc0834:
264 		adc->mux_bits = 2;
265 		indio_dev->channels = adc0834_channels;
266 		indio_dev->num_channels = ARRAY_SIZE(adc0834_channels);
267 		break;
268 	case adc0838:
269 		adc->mux_bits = 3;
270 		indio_dev->channels = adc0838_channels;
271 		indio_dev->num_channels = ARRAY_SIZE(adc0838_channels);
272 		break;
273 	default:
274 		return -EINVAL;
275 	}
276 
277 	adc->reg = devm_regulator_get(&spi->dev, "vref");
278 	if (IS_ERR(adc->reg))
279 		return PTR_ERR(adc->reg);
280 
281 	ret = regulator_enable(adc->reg);
282 	if (ret)
283 		return ret;
284 
285 	spi_set_drvdata(spi, indio_dev);
286 
287 	ret = iio_triggered_buffer_setup(indio_dev, NULL,
288 					 adc0832_trigger_handler, NULL);
289 	if (ret)
290 		goto err_reg_disable;
291 
292 	ret = iio_device_register(indio_dev);
293 	if (ret)
294 		goto err_buffer_cleanup;
295 
296 	return 0;
297 err_buffer_cleanup:
298 	iio_triggered_buffer_cleanup(indio_dev);
299 err_reg_disable:
300 	regulator_disable(adc->reg);
301 
302 	return ret;
303 }
304 
305 static int adc0832_remove(struct spi_device *spi)
306 {
307 	struct iio_dev *indio_dev = spi_get_drvdata(spi);
308 	struct adc0832 *adc = iio_priv(indio_dev);
309 
310 	iio_device_unregister(indio_dev);
311 	iio_triggered_buffer_cleanup(indio_dev);
312 	regulator_disable(adc->reg);
313 
314 	return 0;
315 }
316 
317 static const struct of_device_id adc0832_dt_ids[] = {
318 	{ .compatible = "ti,adc0831", },
319 	{ .compatible = "ti,adc0832", },
320 	{ .compatible = "ti,adc0834", },
321 	{ .compatible = "ti,adc0838", },
322 	{}
323 };
324 MODULE_DEVICE_TABLE(of, adc0832_dt_ids);
325 
326 static const struct spi_device_id adc0832_id[] = {
327 	{ "adc0831", adc0831 },
328 	{ "adc0832", adc0832 },
329 	{ "adc0834", adc0834 },
330 	{ "adc0838", adc0838 },
331 	{}
332 };
333 MODULE_DEVICE_TABLE(spi, adc0832_id);
334 
335 static struct spi_driver adc0832_driver = {
336 	.driver = {
337 		.name = "adc0832",
338 		.of_match_table = adc0832_dt_ids,
339 	},
340 	.probe = adc0832_probe,
341 	.remove = adc0832_remove,
342 	.id_table = adc0832_id,
343 };
344 module_spi_driver(adc0832_driver);
345 
346 MODULE_AUTHOR("Akinobu Mita <akinobu.mita@gmail.com>");
347 MODULE_DESCRIPTION("ADC0831/ADC0832/ADC0834/ADC0838 driver");
348 MODULE_LICENSE("GPL v2");
349