2 * ADC12130/ADC12132/ADC12138 12-bit plus sign ADC driver
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)
41 struct spi_device *spi;
43 /* conversion clock */
45 /* positive analog voltage reference */
46 struct regulator *vref_p;
47 /* negative analog voltage reference */
48 struct regulator *vref_n;
50 struct completion complete;
51 /* The number of cclk periods for the S/H's acquisition time */
52 unsigned int acquisition_time;
54 u8 tx_buf[2] ____cacheline_aligned;
58 #define ADC12138_VOLTAGE_CHANNEL(chan) \
60 .type = IIO_VOLTAGE, \
63 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
64 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) \
65 | BIT(IIO_CHAN_INFO_OFFSET), \
72 .endianness = IIO_BE, \
76 #define ADC12138_VOLTAGE_CHANNEL_DIFF(chan1, chan2, si) \
78 .type = IIO_VOLTAGE, \
81 .channel2 = (chan2), \
83 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
84 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) \
85 | BIT(IIO_CHAN_INFO_OFFSET), \
92 .endianness = IIO_BE, \
96 static const struct iio_chan_spec adc12132_channels[] = {
97 ADC12138_VOLTAGE_CHANNEL(0),
98 ADC12138_VOLTAGE_CHANNEL(1),
99 ADC12138_VOLTAGE_CHANNEL_DIFF(0, 1, 2),
100 ADC12138_VOLTAGE_CHANNEL_DIFF(1, 0, 3),
101 IIO_CHAN_SOFT_TIMESTAMP(4),
104 static const struct iio_chan_spec adc12138_channels[] = {
105 ADC12138_VOLTAGE_CHANNEL(0),
106 ADC12138_VOLTAGE_CHANNEL(1),
107 ADC12138_VOLTAGE_CHANNEL(2),
108 ADC12138_VOLTAGE_CHANNEL(3),
109 ADC12138_VOLTAGE_CHANNEL(4),
110 ADC12138_VOLTAGE_CHANNEL(5),
111 ADC12138_VOLTAGE_CHANNEL(6),
112 ADC12138_VOLTAGE_CHANNEL(7),
113 ADC12138_VOLTAGE_CHANNEL_DIFF(0, 1, 8),
114 ADC12138_VOLTAGE_CHANNEL_DIFF(1, 0, 9),
115 ADC12138_VOLTAGE_CHANNEL_DIFF(2, 3, 10),
116 ADC12138_VOLTAGE_CHANNEL_DIFF(3, 2, 11),
117 ADC12138_VOLTAGE_CHANNEL_DIFF(4, 5, 12),
118 ADC12138_VOLTAGE_CHANNEL_DIFF(5, 4, 13),
119 ADC12138_VOLTAGE_CHANNEL_DIFF(6, 7, 14),
120 ADC12138_VOLTAGE_CHANNEL_DIFF(7, 6, 15),
121 IIO_CHAN_SOFT_TIMESTAMP(16),
124 static int adc12138_mode_programming(struct adc12138 *adc, u8 mode,
125 void *rx_buf, int len)
127 struct spi_transfer xfer = {
128 .tx_buf = adc->tx_buf,
129 .rx_buf = adc->rx_buf,
134 /* Skip unused bits for ADC12130 and ADC12132 */
135 if (adc->id != adc12138)
136 mode = (mode & 0xc0) | ((mode & 0x0f) << 2);
138 adc->tx_buf[0] = mode;
140 ret = spi_sync_transfer(adc->spi, &xfer, 1);
144 memcpy(rx_buf, adc->rx_buf, len);
149 static int adc12138_read_status(struct adc12138 *adc)
154 ret = adc12138_mode_programming(adc, ADC12138_MODE_READ_STATUS,
159 return (rx_buf[0] << 1) | (rx_buf[1] >> 7);
162 static int __adc12138_start_conv(struct adc12138 *adc,
163 struct iio_chan_spec const *channel,
167 static const u8 ch_to_mux[] = { 0, 4, 1, 5, 2, 6, 3, 7 };
168 u8 mode = (ch_to_mux[channel->channel] << 4) |
169 (channel->differential ? 0 : 0x80);
171 return adc12138_mode_programming(adc, mode, data, len);
174 static int adc12138_start_conv(struct adc12138 *adc,
175 struct iio_chan_spec const *channel)
179 return __adc12138_start_conv(adc, channel, &trash, 1);
182 static int adc12138_start_and_read_conv(struct adc12138 *adc,
183 struct iio_chan_spec const *channel,
186 return __adc12138_start_conv(adc, channel, data, 2);
189 static int adc12138_read_conv_data(struct adc12138 *adc, __be16 *value)
191 /* Issue a read status instruction and read previous conversion data */
192 return adc12138_mode_programming(adc, ADC12138_MODE_READ_STATUS,
193 value, sizeof(*value));
196 static int adc12138_wait_eoc(struct adc12138 *adc, unsigned long timeout)
198 if (!wait_for_completion_timeout(&adc->complete, timeout))
204 static int adc12138_adc_conversion(struct adc12138 *adc,
205 struct iio_chan_spec const *channel,
210 reinit_completion(&adc->complete);
212 ret = adc12138_start_conv(adc, channel);
216 ret = adc12138_wait_eoc(adc, msecs_to_jiffies(100));
220 return adc12138_read_conv_data(adc, value);
223 static int adc12138_read_raw(struct iio_dev *iio,
224 struct iio_chan_spec const *channel, int *value,
225 int *shift, long mask)
227 struct adc12138 *adc = iio_priv(iio);
232 case IIO_CHAN_INFO_RAW:
233 mutex_lock(&adc->lock);
234 ret = adc12138_adc_conversion(adc, channel, &data);
235 mutex_unlock(&adc->lock);
239 *value = sign_extend32(be16_to_cpu(data) >> 3, 12);
242 case IIO_CHAN_INFO_SCALE:
243 ret = regulator_get_voltage(adc->vref_p);
248 if (!IS_ERR(adc->vref_n)) {
249 ret = regulator_get_voltage(adc->vref_n);
255 /* convert regulator output voltage to mV */
257 *shift = channel->scan_type.realbits - 1;
259 return IIO_VAL_FRACTIONAL_LOG2;
260 case IIO_CHAN_INFO_OFFSET:
261 if (!IS_ERR(adc->vref_n)) {
262 *value = regulator_get_voltage(adc->vref_n);
269 /* convert regulator output voltage to mV */
278 static const struct iio_info adc12138_info = {
279 .read_raw = adc12138_read_raw,
282 static int adc12138_init(struct adc12138 *adc)
289 reinit_completion(&adc->complete);
291 ret = adc12138_mode_programming(adc, ADC12138_MODE_AUTO_CAL, &trash, 1);
295 /* data output at this time has no significance */
296 status = adc12138_read_status(adc);
300 adc12138_wait_eoc(adc, msecs_to_jiffies(100));
302 status = adc12138_read_status(adc);
303 if (status & ADC12138_STATUS_CAL) {
304 dev_warn(&adc->spi->dev,
305 "Auto Cal sequence is still in progress: %#x\n",
310 switch (adc->acquisition_time) {
312 mode = ADC12138_MODE_ACQUISITION_TIME_6;
315 mode = ADC12138_MODE_ACQUISITION_TIME_10;
318 mode = ADC12138_MODE_ACQUISITION_TIME_18;
321 mode = ADC12138_MODE_ACQUISITION_TIME_34;
327 return adc12138_mode_programming(adc, mode, &trash, 1);
330 static irqreturn_t adc12138_trigger_handler(int irq, void *p)
332 struct iio_poll_func *pf = p;
333 struct iio_dev *indio_dev = pf->indio_dev;
334 struct adc12138 *adc = iio_priv(indio_dev);
335 __be16 data[20] = { }; /* 16x 2 bytes ADC data + 8 bytes timestamp */
341 mutex_lock(&adc->lock);
343 for_each_set_bit(scan_index, indio_dev->active_scan_mask,
344 indio_dev->masklength) {
345 const struct iio_chan_spec *scan_chan =
346 &indio_dev->channels[scan_index];
348 reinit_completion(&adc->complete);
350 ret = adc12138_start_and_read_conv(adc, scan_chan,
351 i ? &data[i - 1] : &trash);
353 dev_warn(&adc->spi->dev,
354 "failed to start conversion\n");
358 ret = adc12138_wait_eoc(adc, msecs_to_jiffies(100));
360 dev_warn(&adc->spi->dev, "wait eoc timeout\n");
368 ret = adc12138_read_conv_data(adc, &data[i - 1]);
370 dev_warn(&adc->spi->dev,
371 "failed to get conversion data\n");
376 iio_push_to_buffers_with_timestamp(indio_dev, data,
377 iio_get_time_ns(indio_dev));
379 mutex_unlock(&adc->lock);
381 iio_trigger_notify_done(indio_dev->trig);
386 static irqreturn_t adc12138_eoc_handler(int irq, void *p)
388 struct iio_dev *indio_dev = p;
389 struct adc12138 *adc = iio_priv(indio_dev);
391 complete(&adc->complete);
396 static int adc12138_probe(struct spi_device *spi)
398 struct iio_dev *indio_dev;
399 struct adc12138 *adc;
402 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc));
406 adc = iio_priv(indio_dev);
408 adc->id = spi_get_device_id(spi)->driver_data;
409 mutex_init(&adc->lock);
410 init_completion(&adc->complete);
412 indio_dev->name = spi_get_device_id(spi)->name;
413 indio_dev->dev.parent = &spi->dev;
414 indio_dev->info = &adc12138_info;
415 indio_dev->modes = INDIO_DIRECT_MODE;
420 indio_dev->channels = adc12132_channels;
421 indio_dev->num_channels = ARRAY_SIZE(adc12132_channels);
424 indio_dev->channels = adc12138_channels;
425 indio_dev->num_channels = ARRAY_SIZE(adc12138_channels);
431 ret = of_property_read_u32(spi->dev.of_node, "ti,acquisition-time",
432 &adc->acquisition_time);
434 adc->acquisition_time = 10;
436 adc->cclk = devm_clk_get(&spi->dev, NULL);
437 if (IS_ERR(adc->cclk))
438 return PTR_ERR(adc->cclk);
440 adc->vref_p = devm_regulator_get(&spi->dev, "vref-p");
441 if (IS_ERR(adc->vref_p))
442 return PTR_ERR(adc->vref_p);
444 adc->vref_n = devm_regulator_get_optional(&spi->dev, "vref-n");
445 if (IS_ERR(adc->vref_n)) {
447 * Assume vref_n is 0V if an optional regulator is not
448 * specified, otherwise return the error code.
450 ret = PTR_ERR(adc->vref_n);
455 ret = devm_request_irq(&spi->dev, spi->irq, adc12138_eoc_handler,
456 IRQF_TRIGGER_RISING, indio_dev->name, indio_dev);
460 ret = clk_prepare_enable(adc->cclk);
464 ret = regulator_enable(adc->vref_p);
466 goto err_clk_disable;
468 if (!IS_ERR(adc->vref_n)) {
469 ret = regulator_enable(adc->vref_n);
471 goto err_vref_p_disable;
474 ret = adc12138_init(adc);
476 goto err_vref_n_disable;
478 spi_set_drvdata(spi, indio_dev);
480 ret = iio_triggered_buffer_setup(indio_dev, NULL,
481 adc12138_trigger_handler, NULL);
483 goto err_vref_n_disable;
485 ret = iio_device_register(indio_dev);
487 goto err_buffer_cleanup;
491 iio_triggered_buffer_cleanup(indio_dev);
493 if (!IS_ERR(adc->vref_n))
494 regulator_disable(adc->vref_n);
496 regulator_disable(adc->vref_p);
498 clk_disable_unprepare(adc->cclk);
503 static int adc12138_remove(struct spi_device *spi)
505 struct iio_dev *indio_dev = spi_get_drvdata(spi);
506 struct adc12138 *adc = iio_priv(indio_dev);
508 iio_device_unregister(indio_dev);
509 iio_triggered_buffer_cleanup(indio_dev);
510 if (!IS_ERR(adc->vref_n))
511 regulator_disable(adc->vref_n);
512 regulator_disable(adc->vref_p);
513 clk_disable_unprepare(adc->cclk);
520 static const struct of_device_id adc12138_dt_ids[] = {
521 { .compatible = "ti,adc12130", },
522 { .compatible = "ti,adc12132", },
523 { .compatible = "ti,adc12138", },
526 MODULE_DEVICE_TABLE(of, adc12138_dt_ids);
530 static const struct spi_device_id adc12138_id[] = {
531 { "adc12130", adc12130 },
532 { "adc12132", adc12132 },
533 { "adc12138", adc12138 },
536 MODULE_DEVICE_TABLE(spi, adc12138_id);
538 static struct spi_driver adc12138_driver = {
541 .of_match_table = of_match_ptr(adc12138_dt_ids),
543 .probe = adc12138_probe,
544 .remove = adc12138_remove,
545 .id_table = adc12138_id,
547 module_spi_driver(adc12138_driver);
550 MODULE_DESCRIPTION("ADC12130/ADC12132/ADC12138 driver");
551 MODULE_LICENSE("GPL v2");