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