Linux 4.9.243
[linux/fpc-iii.git] / drivers / iio / adc / ti-adc12138.c
blob1410a41782cc9a10d56a7bf57c1e8f0c92bc5bd2
1 /*
2 * ADC12130/ADC12132/ADC12138 12-bit plus sign ADC driver
4 * Copyright (c) 2016 Akinobu Mita <akinobu.mita@gmail.com>
6 * This file is subject to the terms and conditions of version 2 of
7 * the GNU General Public License. See the file COPYING in the main
8 * directory of this archive for more details.
10 * Datasheet: http://www.ti.com/lit/ds/symlink/adc12138.pdf
13 #include <linux/module.h>
14 #include <linux/interrupt.h>
15 #include <linux/completion.h>
16 #include <linux/clk.h>
17 #include <linux/spi/spi.h>
18 #include <linux/iio/iio.h>
19 #include <linux/iio/buffer.h>
20 #include <linux/iio/trigger.h>
21 #include <linux/iio/triggered_buffer.h>
22 #include <linux/iio/trigger_consumer.h>
23 #include <linux/regulator/consumer.h>
25 #define ADC12138_MODE_AUTO_CAL 0x08
26 #define ADC12138_MODE_READ_STATUS 0x0c
27 #define ADC12138_MODE_ACQUISITION_TIME_6 0x0e
28 #define ADC12138_MODE_ACQUISITION_TIME_10 0x4e
29 #define ADC12138_MODE_ACQUISITION_TIME_18 0x8e
30 #define ADC12138_MODE_ACQUISITION_TIME_34 0xce
32 #define ADC12138_STATUS_CAL BIT(6)
34 enum {
35 adc12130,
36 adc12132,
37 adc12138,
40 struct adc12138 {
41 struct spi_device *spi;
42 unsigned int id;
43 /* conversion clock */
44 struct clk *cclk;
45 /* positive analog voltage reference */
46 struct regulator *vref_p;
47 /* negative analog voltage reference */
48 struct regulator *vref_n;
49 struct mutex lock;
50 struct completion complete;
51 /* The number of cclk periods for the S/H's acquisition time */
52 unsigned int acquisition_time;
54 * Maximum size needed: 16x 2 bytes ADC data + 8 bytes timestamp.
55 * Less may be need if not all channels are enabled, as long as
56 * the 8 byte alignment of the timestamp is maintained.
58 __be16 data[20] __aligned(8);
60 u8 tx_buf[2] ____cacheline_aligned;
61 u8 rx_buf[2];
64 #define ADC12138_VOLTAGE_CHANNEL(chan) \
65 { \
66 .type = IIO_VOLTAGE, \
67 .indexed = 1, \
68 .channel = chan, \
69 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
70 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) \
71 | BIT(IIO_CHAN_INFO_OFFSET), \
72 .scan_index = chan, \
73 .scan_type = { \
74 .sign = 's', \
75 .realbits = 13, \
76 .storagebits = 16, \
77 .shift = 3, \
78 .endianness = IIO_BE, \
79 }, \
82 #define ADC12138_VOLTAGE_CHANNEL_DIFF(chan1, chan2, si) \
83 { \
84 .type = IIO_VOLTAGE, \
85 .indexed = 1, \
86 .channel = (chan1), \
87 .channel2 = (chan2), \
88 .differential = 1, \
89 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
90 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) \
91 | BIT(IIO_CHAN_INFO_OFFSET), \
92 .scan_index = si, \
93 .scan_type = { \
94 .sign = 's', \
95 .realbits = 13, \
96 .storagebits = 16, \
97 .shift = 3, \
98 .endianness = IIO_BE, \
99 }, \
102 static const struct iio_chan_spec adc12132_channels[] = {
103 ADC12138_VOLTAGE_CHANNEL(0),
104 ADC12138_VOLTAGE_CHANNEL(1),
105 ADC12138_VOLTAGE_CHANNEL_DIFF(0, 1, 2),
106 ADC12138_VOLTAGE_CHANNEL_DIFF(1, 0, 3),
107 IIO_CHAN_SOFT_TIMESTAMP(4),
110 static const struct iio_chan_spec adc12138_channels[] = {
111 ADC12138_VOLTAGE_CHANNEL(0),
112 ADC12138_VOLTAGE_CHANNEL(1),
113 ADC12138_VOLTAGE_CHANNEL(2),
114 ADC12138_VOLTAGE_CHANNEL(3),
115 ADC12138_VOLTAGE_CHANNEL(4),
116 ADC12138_VOLTAGE_CHANNEL(5),
117 ADC12138_VOLTAGE_CHANNEL(6),
118 ADC12138_VOLTAGE_CHANNEL(7),
119 ADC12138_VOLTAGE_CHANNEL_DIFF(0, 1, 8),
120 ADC12138_VOLTAGE_CHANNEL_DIFF(1, 0, 9),
121 ADC12138_VOLTAGE_CHANNEL_DIFF(2, 3, 10),
122 ADC12138_VOLTAGE_CHANNEL_DIFF(3, 2, 11),
123 ADC12138_VOLTAGE_CHANNEL_DIFF(4, 5, 12),
124 ADC12138_VOLTAGE_CHANNEL_DIFF(5, 4, 13),
125 ADC12138_VOLTAGE_CHANNEL_DIFF(6, 7, 14),
126 ADC12138_VOLTAGE_CHANNEL_DIFF(7, 6, 15),
127 IIO_CHAN_SOFT_TIMESTAMP(16),
130 static int adc12138_mode_programming(struct adc12138 *adc, u8 mode,
131 void *rx_buf, int len)
133 struct spi_transfer xfer = {
134 .tx_buf = adc->tx_buf,
135 .rx_buf = adc->rx_buf,
136 .len = len,
138 int ret;
140 /* Skip unused bits for ADC12130 and ADC12132 */
141 if (adc->id != adc12138)
142 mode = (mode & 0xc0) | ((mode & 0x0f) << 2);
144 adc->tx_buf[0] = mode;
146 ret = spi_sync_transfer(adc->spi, &xfer, 1);
147 if (ret)
148 return ret;
150 memcpy(rx_buf, adc->rx_buf, len);
152 return 0;
155 static int adc12138_read_status(struct adc12138 *adc)
157 u8 rx_buf[2];
158 int ret;
160 ret = adc12138_mode_programming(adc, ADC12138_MODE_READ_STATUS,
161 rx_buf, 2);
162 if (ret)
163 return ret;
165 return (rx_buf[0] << 1) | (rx_buf[1] >> 7);
168 static int __adc12138_start_conv(struct adc12138 *adc,
169 struct iio_chan_spec const *channel,
170 void *data, int len)
173 const u8 ch_to_mux[] = { 0, 4, 1, 5, 2, 6, 3, 7 };
174 u8 mode = (ch_to_mux[channel->channel] << 4) |
175 (channel->differential ? 0 : 0x80);
177 return adc12138_mode_programming(adc, mode, data, len);
180 static int adc12138_start_conv(struct adc12138 *adc,
181 struct iio_chan_spec const *channel)
183 u8 trash;
185 return __adc12138_start_conv(adc, channel, &trash, 1);
188 static int adc12138_start_and_read_conv(struct adc12138 *adc,
189 struct iio_chan_spec const *channel,
190 __be16 *data)
192 return __adc12138_start_conv(adc, channel, data, 2);
195 static int adc12138_read_conv_data(struct adc12138 *adc, __be16 *value)
197 /* Issue a read status instruction and read previous conversion data */
198 return adc12138_mode_programming(adc, ADC12138_MODE_READ_STATUS,
199 value, sizeof(*value));
202 static int adc12138_wait_eoc(struct adc12138 *adc, unsigned long timeout)
204 if (!wait_for_completion_timeout(&adc->complete, timeout))
205 return -ETIMEDOUT;
207 return 0;
210 static int adc12138_adc_conversion(struct adc12138 *adc,
211 struct iio_chan_spec const *channel,
212 __be16 *value)
214 int ret;
216 reinit_completion(&adc->complete);
218 ret = adc12138_start_conv(adc, channel);
219 if (ret)
220 return ret;
222 ret = adc12138_wait_eoc(adc, msecs_to_jiffies(100));
223 if (ret)
224 return ret;
226 return adc12138_read_conv_data(adc, value);
229 static int adc12138_read_raw(struct iio_dev *iio,
230 struct iio_chan_spec const *channel, int *value,
231 int *shift, long mask)
233 struct adc12138 *adc = iio_priv(iio);
234 int ret;
235 __be16 data;
237 switch (mask) {
238 case IIO_CHAN_INFO_RAW:
239 mutex_lock(&adc->lock);
240 ret = adc12138_adc_conversion(adc, channel, &data);
241 mutex_unlock(&adc->lock);
242 if (ret)
243 return ret;
245 *value = sign_extend32(be16_to_cpu(data) >> 3, 12);
247 return IIO_VAL_INT;
248 case IIO_CHAN_INFO_SCALE:
249 ret = regulator_get_voltage(adc->vref_p);
250 if (ret < 0)
251 return ret;
252 *value = ret;
254 if (!IS_ERR(adc->vref_n)) {
255 ret = regulator_get_voltage(adc->vref_n);
256 if (ret < 0)
257 return ret;
258 *value -= ret;
261 /* convert regulator output voltage to mV */
262 *value /= 1000;
263 *shift = channel->scan_type.realbits - 1;
265 return IIO_VAL_FRACTIONAL_LOG2;
266 case IIO_CHAN_INFO_OFFSET:
267 if (!IS_ERR(adc->vref_n)) {
268 *value = regulator_get_voltage(adc->vref_n);
269 if (*value < 0)
270 return *value;
271 } else {
272 *value = 0;
275 /* convert regulator output voltage to mV */
276 *value /= 1000;
278 return IIO_VAL_INT;
281 return -EINVAL;
284 static const struct iio_info adc12138_info = {
285 .read_raw = adc12138_read_raw,
286 .driver_module = THIS_MODULE,
289 static int adc12138_init(struct adc12138 *adc)
291 int ret;
292 int status;
293 u8 mode;
294 u8 trash;
296 reinit_completion(&adc->complete);
298 ret = adc12138_mode_programming(adc, ADC12138_MODE_AUTO_CAL, &trash, 1);
299 if (ret)
300 return ret;
302 /* data output at this time has no significance */
303 status = adc12138_read_status(adc);
304 if (status < 0)
305 return status;
307 adc12138_wait_eoc(adc, msecs_to_jiffies(100));
309 status = adc12138_read_status(adc);
310 if (status & ADC12138_STATUS_CAL) {
311 dev_warn(&adc->spi->dev,
312 "Auto Cal sequence is still in progress: %#x\n",
313 status);
314 return -EIO;
317 switch (adc->acquisition_time) {
318 case 6:
319 mode = ADC12138_MODE_ACQUISITION_TIME_6;
320 break;
321 case 10:
322 mode = ADC12138_MODE_ACQUISITION_TIME_10;
323 break;
324 case 18:
325 mode = ADC12138_MODE_ACQUISITION_TIME_18;
326 break;
327 case 34:
328 mode = ADC12138_MODE_ACQUISITION_TIME_34;
329 break;
330 default:
331 return -EINVAL;
334 return adc12138_mode_programming(adc, mode, &trash, 1);
337 static irqreturn_t adc12138_trigger_handler(int irq, void *p)
339 struct iio_poll_func *pf = p;
340 struct iio_dev *indio_dev = pf->indio_dev;
341 struct adc12138 *adc = iio_priv(indio_dev);
342 __be16 trash;
343 int ret;
344 int scan_index;
345 int i = 0;
347 mutex_lock(&adc->lock);
349 for_each_set_bit(scan_index, indio_dev->active_scan_mask,
350 indio_dev->masklength) {
351 const struct iio_chan_spec *scan_chan =
352 &indio_dev->channels[scan_index];
354 reinit_completion(&adc->complete);
356 ret = adc12138_start_and_read_conv(adc, scan_chan,
357 i ? &adc->data[i - 1] : &trash);
358 if (ret) {
359 dev_warn(&adc->spi->dev,
360 "failed to start conversion\n");
361 goto out;
364 ret = adc12138_wait_eoc(adc, msecs_to_jiffies(100));
365 if (ret) {
366 dev_warn(&adc->spi->dev, "wait eoc timeout\n");
367 goto out;
370 i++;
373 if (i) {
374 ret = adc12138_read_conv_data(adc, &adc->data[i - 1]);
375 if (ret) {
376 dev_warn(&adc->spi->dev,
377 "failed to get conversion data\n");
378 goto out;
382 iio_push_to_buffers_with_timestamp(indio_dev, adc->data,
383 iio_get_time_ns(indio_dev));
384 out:
385 mutex_unlock(&adc->lock);
387 iio_trigger_notify_done(indio_dev->trig);
389 return IRQ_HANDLED;
392 static irqreturn_t adc12138_eoc_handler(int irq, void *p)
394 struct iio_dev *indio_dev = p;
395 struct adc12138 *adc = iio_priv(indio_dev);
397 complete(&adc->complete);
399 return IRQ_HANDLED;
402 static int adc12138_probe(struct spi_device *spi)
404 struct iio_dev *indio_dev;
405 struct adc12138 *adc;
406 int ret;
408 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc));
409 if (!indio_dev)
410 return -ENOMEM;
412 adc = iio_priv(indio_dev);
413 adc->spi = spi;
414 adc->id = spi_get_device_id(spi)->driver_data;
415 mutex_init(&adc->lock);
416 init_completion(&adc->complete);
418 indio_dev->name = spi_get_device_id(spi)->name;
419 indio_dev->dev.parent = &spi->dev;
420 indio_dev->info = &adc12138_info;
421 indio_dev->modes = INDIO_DIRECT_MODE;
423 switch (adc->id) {
424 case adc12130:
425 case adc12132:
426 indio_dev->channels = adc12132_channels;
427 indio_dev->num_channels = ARRAY_SIZE(adc12132_channels);
428 break;
429 case adc12138:
430 indio_dev->channels = adc12138_channels;
431 indio_dev->num_channels = ARRAY_SIZE(adc12138_channels);
432 break;
433 default:
434 return -EINVAL;
437 ret = of_property_read_u32(spi->dev.of_node, "ti,acquisition-time",
438 &adc->acquisition_time);
439 if (ret)
440 adc->acquisition_time = 10;
442 adc->cclk = devm_clk_get(&spi->dev, NULL);
443 if (IS_ERR(adc->cclk))
444 return PTR_ERR(adc->cclk);
446 adc->vref_p = devm_regulator_get(&spi->dev, "vref-p");
447 if (IS_ERR(adc->vref_p))
448 return PTR_ERR(adc->vref_p);
450 adc->vref_n = devm_regulator_get_optional(&spi->dev, "vref-n");
451 if (IS_ERR(adc->vref_n)) {
453 * Assume vref_n is 0V if an optional regulator is not
454 * specified, otherwise return the error code.
456 ret = PTR_ERR(adc->vref_n);
457 if (ret != -ENODEV)
458 return ret;
461 ret = devm_request_irq(&spi->dev, spi->irq, adc12138_eoc_handler,
462 IRQF_TRIGGER_RISING, indio_dev->name, indio_dev);
463 if (ret)
464 return ret;
466 ret = clk_prepare_enable(adc->cclk);
467 if (ret)
468 return ret;
470 ret = regulator_enable(adc->vref_p);
471 if (ret)
472 goto err_clk_disable;
474 if (!IS_ERR(adc->vref_n)) {
475 ret = regulator_enable(adc->vref_n);
476 if (ret)
477 goto err_vref_p_disable;
480 ret = adc12138_init(adc);
481 if (ret)
482 goto err_vref_n_disable;
484 spi_set_drvdata(spi, indio_dev);
486 ret = iio_triggered_buffer_setup(indio_dev, NULL,
487 adc12138_trigger_handler, NULL);
488 if (ret)
489 goto err_vref_n_disable;
491 ret = iio_device_register(indio_dev);
492 if (ret)
493 goto err_buffer_cleanup;
495 return 0;
496 err_buffer_cleanup:
497 iio_triggered_buffer_cleanup(indio_dev);
498 err_vref_n_disable:
499 if (!IS_ERR(adc->vref_n))
500 regulator_disable(adc->vref_n);
501 err_vref_p_disable:
502 regulator_disable(adc->vref_p);
503 err_clk_disable:
504 clk_disable_unprepare(adc->cclk);
506 return ret;
509 static int adc12138_remove(struct spi_device *spi)
511 struct iio_dev *indio_dev = spi_get_drvdata(spi);
512 struct adc12138 *adc = iio_priv(indio_dev);
514 iio_device_unregister(indio_dev);
515 iio_triggered_buffer_cleanup(indio_dev);
516 if (!IS_ERR(adc->vref_n))
517 regulator_disable(adc->vref_n);
518 regulator_disable(adc->vref_p);
519 clk_disable_unprepare(adc->cclk);
521 return 0;
524 #ifdef CONFIG_OF
526 static const struct of_device_id adc12138_dt_ids[] = {
527 { .compatible = "ti,adc12130", },
528 { .compatible = "ti,adc12132", },
529 { .compatible = "ti,adc12138", },
532 MODULE_DEVICE_TABLE(of, adc12138_dt_ids);
534 #endif
536 static const struct spi_device_id adc12138_id[] = {
537 { "adc12130", adc12130 },
538 { "adc12132", adc12132 },
539 { "adc12138", adc12138 },
542 MODULE_DEVICE_TABLE(spi, adc12138_id);
544 static struct spi_driver adc12138_driver = {
545 .driver = {
546 .name = "adc12138",
547 .of_match_table = of_match_ptr(adc12138_dt_ids),
549 .probe = adc12138_probe,
550 .remove = adc12138_remove,
551 .id_table = adc12138_id,
553 module_spi_driver(adc12138_driver);
555 MODULE_AUTHOR("Akinobu Mita <akinobu.mita@gmail.com>");
556 MODULE_DESCRIPTION("ADC12130/ADC12132/ADC12138 driver");
557 MODULE_LICENSE("GPL v2");