1 // SPDX-License-Identifier: GPL-2.0-only
3 * ADC12130/ADC12132/ADC12138 12-bit plus sign ADC driver
7 * Datasheet: http://www.ti.com/lit/ds/symlink/adc12138.pdf
10 #include <linux/module.h>
11 #include <linux/interrupt.h>
12 #include <linux/completion.h>
13 #include <linux/clk.h>
14 #include <linux/property.h>
15 #include <linux/spi/spi.h>
16 #include <linux/iio/iio.h>
17 #include <linux/iio/buffer.h>
18 #include <linux/iio/trigger.h>
19 #include <linux/iio/triggered_buffer.h>
20 #include <linux/iio/trigger_consumer.h>
21 #include <linux/regulator/consumer.h>
23 #define ADC12138_MODE_AUTO_CAL 0x08
24 #define ADC12138_MODE_READ_STATUS 0x0c
25 #define ADC12138_MODE_ACQUISITION_TIME_6 0x0e
26 #define ADC12138_MODE_ACQUISITION_TIME_10 0x4e
27 #define ADC12138_MODE_ACQUISITION_TIME_18 0x8e
28 #define ADC12138_MODE_ACQUISITION_TIME_34 0xce
30 #define ADC12138_STATUS_CAL BIT(6)
39 struct spi_device *spi;
41 /* conversion clock */
43 /* positive analog voltage reference */
44 struct regulator *vref_p;
45 /* negative analog voltage reference */
46 struct regulator *vref_n;
48 struct completion complete;
49 /* The number of cclk periods for the S/H's acquisition time */
50 unsigned int acquisition_time;
52 * Maximum size needed: 16x 2 bytes ADC data + 8 bytes timestamp.
53 * Less may be need if not all channels are enabled, as long as
54 * the 8 byte alignment of the timestamp is maintained.
56 __be16 data[20] __aligned(8);
58 u8 tx_buf[2] __aligned(IIO_DMA_MINALIGN);
62 #define ADC12138_VOLTAGE_CHANNEL(chan) \
64 .type = IIO_VOLTAGE, \
67 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
68 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) \
69 | BIT(IIO_CHAN_INFO_OFFSET), \
76 .endianness = IIO_BE, \
80 #define ADC12138_VOLTAGE_CHANNEL_DIFF(chan1, chan2, si) \
82 .type = IIO_VOLTAGE, \
85 .channel2 = (chan2), \
87 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
88 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) \
89 | BIT(IIO_CHAN_INFO_OFFSET), \
96 .endianness = IIO_BE, \
100 static const struct iio_chan_spec adc12132_channels[] = {
101 ADC12138_VOLTAGE_CHANNEL(0),
102 ADC12138_VOLTAGE_CHANNEL(1),
103 ADC12138_VOLTAGE_CHANNEL_DIFF(0, 1, 2),
104 ADC12138_VOLTAGE_CHANNEL_DIFF(1, 0, 3),
105 IIO_CHAN_SOFT_TIMESTAMP(4),
108 static const struct iio_chan_spec adc12138_channels[] = {
109 ADC12138_VOLTAGE_CHANNEL(0),
110 ADC12138_VOLTAGE_CHANNEL(1),
111 ADC12138_VOLTAGE_CHANNEL(2),
112 ADC12138_VOLTAGE_CHANNEL(3),
113 ADC12138_VOLTAGE_CHANNEL(4),
114 ADC12138_VOLTAGE_CHANNEL(5),
115 ADC12138_VOLTAGE_CHANNEL(6),
116 ADC12138_VOLTAGE_CHANNEL(7),
117 ADC12138_VOLTAGE_CHANNEL_DIFF(0, 1, 8),
118 ADC12138_VOLTAGE_CHANNEL_DIFF(1, 0, 9),
119 ADC12138_VOLTAGE_CHANNEL_DIFF(2, 3, 10),
120 ADC12138_VOLTAGE_CHANNEL_DIFF(3, 2, 11),
121 ADC12138_VOLTAGE_CHANNEL_DIFF(4, 5, 12),
122 ADC12138_VOLTAGE_CHANNEL_DIFF(5, 4, 13),
123 ADC12138_VOLTAGE_CHANNEL_DIFF(6, 7, 14),
124 ADC12138_VOLTAGE_CHANNEL_DIFF(7, 6, 15),
125 IIO_CHAN_SOFT_TIMESTAMP(16),
128 static int adc12138_mode_programming(struct adc12138 *adc, u8 mode,
129 void *rx_buf, int len)
131 struct spi_transfer xfer = {
132 .tx_buf = adc->tx_buf,
133 .rx_buf = adc->rx_buf,
138 /* Skip unused bits for ADC12130 and ADC12132 */
139 if (adc->id != adc12138)
140 mode = (mode & 0xc0) | ((mode & 0x0f) << 2);
142 adc->tx_buf[0] = mode;
144 ret = spi_sync_transfer(adc->spi, &xfer, 1);
148 memcpy(rx_buf, adc->rx_buf, len);
153 static int adc12138_read_status(struct adc12138 *adc)
158 ret = adc12138_mode_programming(adc, ADC12138_MODE_READ_STATUS,
163 return (rx_buf[0] << 1) | (rx_buf[1] >> 7);
166 static int __adc12138_start_conv(struct adc12138 *adc,
167 struct iio_chan_spec const *channel,
171 static const u8 ch_to_mux[] = { 0, 4, 1, 5, 2, 6, 3, 7 };
172 u8 mode = (ch_to_mux[channel->channel] << 4) |
173 (channel->differential ? 0 : 0x80);
175 return adc12138_mode_programming(adc, mode, data, len);
178 static int adc12138_start_conv(struct adc12138 *adc,
179 struct iio_chan_spec const *channel)
183 return __adc12138_start_conv(adc, channel, &trash, 1);
186 static int adc12138_start_and_read_conv(struct adc12138 *adc,
187 struct iio_chan_spec const *channel,
190 return __adc12138_start_conv(adc, channel, data, 2);
193 static int adc12138_read_conv_data(struct adc12138 *adc, __be16 *value)
195 /* Issue a read status instruction and read previous conversion data */
196 return adc12138_mode_programming(adc, ADC12138_MODE_READ_STATUS,
197 value, sizeof(*value));
200 static int adc12138_wait_eoc(struct adc12138 *adc, unsigned long timeout)
202 if (!wait_for_completion_timeout(&adc->complete, timeout))
208 static int adc12138_adc_conversion(struct adc12138 *adc,
209 struct iio_chan_spec const *channel,
214 reinit_completion(&adc->complete);
216 ret = adc12138_start_conv(adc, channel);
220 ret = adc12138_wait_eoc(adc, msecs_to_jiffies(100));
224 return adc12138_read_conv_data(adc, value);
227 static int adc12138_read_raw(struct iio_dev *iio,
228 struct iio_chan_spec const *channel, int *value,
229 int *shift, long mask)
231 struct adc12138 *adc = iio_priv(iio);
236 case IIO_CHAN_INFO_RAW:
237 mutex_lock(&adc->lock);
238 ret = adc12138_adc_conversion(adc, channel, &data);
239 mutex_unlock(&adc->lock);
243 *value = sign_extend32(be16_to_cpu(data) >> channel->scan_type.shift,
244 channel->scan_type.realbits - 1);
247 case IIO_CHAN_INFO_SCALE:
248 ret = regulator_get_voltage(adc->vref_p);
253 if (!IS_ERR(adc->vref_n)) {
254 ret = regulator_get_voltage(adc->vref_n);
260 /* convert regulator output voltage to mV */
262 *shift = channel->scan_type.realbits - 1;
264 return IIO_VAL_FRACTIONAL_LOG2;
265 case IIO_CHAN_INFO_OFFSET:
266 if (!IS_ERR(adc->vref_n)) {
267 *value = regulator_get_voltage(adc->vref_n);
274 /* convert regulator output voltage to mV */
283 static const struct iio_info adc12138_info = {
284 .read_raw = adc12138_read_raw,
287 static int adc12138_init(struct adc12138 *adc)
294 reinit_completion(&adc->complete);
296 ret = adc12138_mode_programming(adc, ADC12138_MODE_AUTO_CAL, &trash, 1);
300 /* data output at this time has no significance */
301 status = adc12138_read_status(adc);
305 adc12138_wait_eoc(adc, msecs_to_jiffies(100));
307 status = adc12138_read_status(adc);
308 if (status & ADC12138_STATUS_CAL) {
309 dev_warn(&adc->spi->dev,
310 "Auto Cal sequence is still in progress: %#x\n",
315 switch (adc->acquisition_time) {
317 mode = ADC12138_MODE_ACQUISITION_TIME_6;
320 mode = ADC12138_MODE_ACQUISITION_TIME_10;
323 mode = ADC12138_MODE_ACQUISITION_TIME_18;
326 mode = ADC12138_MODE_ACQUISITION_TIME_34;
332 return adc12138_mode_programming(adc, mode, &trash, 1);
335 static irqreturn_t adc12138_trigger_handler(int irq, void *p)
337 struct iio_poll_func *pf = p;
338 struct iio_dev *indio_dev = pf->indio_dev;
339 struct adc12138 *adc = iio_priv(indio_dev);
345 mutex_lock(&adc->lock);
347 for_each_set_bit(scan_index, indio_dev->active_scan_mask,
348 indio_dev->masklength) {
349 const struct iio_chan_spec *scan_chan =
350 &indio_dev->channels[scan_index];
352 reinit_completion(&adc->complete);
354 ret = adc12138_start_and_read_conv(adc, scan_chan,
355 i ? &adc->data[i - 1] : &trash);
357 dev_warn(&adc->spi->dev,
358 "failed to start conversion\n");
362 ret = adc12138_wait_eoc(adc, msecs_to_jiffies(100));
364 dev_warn(&adc->spi->dev, "wait eoc timeout\n");
372 ret = adc12138_read_conv_data(adc, &adc->data[i - 1]);
374 dev_warn(&adc->spi->dev,
375 "failed to get conversion data\n");
380 iio_push_to_buffers_with_timestamp(indio_dev, adc->data,
381 iio_get_time_ns(indio_dev));
383 mutex_unlock(&adc->lock);
385 iio_trigger_notify_done(indio_dev->trig);
390 static irqreturn_t adc12138_eoc_handler(int irq, void *p)
392 struct iio_dev *indio_dev = p;
393 struct adc12138 *adc = iio_priv(indio_dev);
395 complete(&adc->complete);
400 static int adc12138_probe(struct spi_device *spi)
402 struct iio_dev *indio_dev;
403 struct adc12138 *adc;
406 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc));
410 adc = iio_priv(indio_dev);
412 adc->id = spi_get_device_id(spi)->driver_data;
413 mutex_init(&adc->lock);
414 init_completion(&adc->complete);
416 indio_dev->name = spi_get_device_id(spi)->name;
417 indio_dev->info = &adc12138_info;
418 indio_dev->modes = INDIO_DIRECT_MODE;
423 indio_dev->channels = adc12132_channels;
424 indio_dev->num_channels = ARRAY_SIZE(adc12132_channels);
427 indio_dev->channels = adc12138_channels;
428 indio_dev->num_channels = ARRAY_SIZE(adc12138_channels);
434 ret = device_property_read_u32(&spi->dev, "ti,acquisition-time",
435 &adc->acquisition_time);
437 adc->acquisition_time = 10;
439 adc->cclk = devm_clk_get(&spi->dev, NULL);
440 if (IS_ERR(adc->cclk))
441 return PTR_ERR(adc->cclk);
443 adc->vref_p = devm_regulator_get(&spi->dev, "vref-p");
444 if (IS_ERR(adc->vref_p))
445 return PTR_ERR(adc->vref_p);
447 adc->vref_n = devm_regulator_get_optional(&spi->dev, "vref-n");
448 if (IS_ERR(adc->vref_n)) {
450 * Assume vref_n is 0V if an optional regulator is not
451 * specified, otherwise return the error code.
453 ret = PTR_ERR(adc->vref_n);
458 ret = devm_request_irq(&spi->dev, spi->irq, adc12138_eoc_handler,
459 IRQF_TRIGGER_RISING, indio_dev->name, indio_dev);
463 ret = clk_prepare_enable(adc->cclk);
467 ret = regulator_enable(adc->vref_p);
469 goto err_clk_disable;
471 if (!IS_ERR(adc->vref_n)) {
472 ret = regulator_enable(adc->vref_n);
474 goto err_vref_p_disable;
477 ret = adc12138_init(adc);
479 goto err_vref_n_disable;
481 spi_set_drvdata(spi, indio_dev);
483 ret = iio_triggered_buffer_setup(indio_dev, NULL,
484 adc12138_trigger_handler, NULL);
486 goto err_vref_n_disable;
488 ret = iio_device_register(indio_dev);
490 goto err_buffer_cleanup;
494 iio_triggered_buffer_cleanup(indio_dev);
496 if (!IS_ERR(adc->vref_n))
497 regulator_disable(adc->vref_n);
499 regulator_disable(adc->vref_p);
501 clk_disable_unprepare(adc->cclk);
506 static void adc12138_remove(struct spi_device *spi)
508 struct iio_dev *indio_dev = spi_get_drvdata(spi);
509 struct adc12138 *adc = iio_priv(indio_dev);
511 iio_device_unregister(indio_dev);
512 iio_triggered_buffer_cleanup(indio_dev);
513 if (!IS_ERR(adc->vref_n))
514 regulator_disable(adc->vref_n);
515 regulator_disable(adc->vref_p);
516 clk_disable_unprepare(adc->cclk);
519 static const struct of_device_id adc12138_dt_ids[] = {
520 { .compatible = "ti,adc12130", },
521 { .compatible = "ti,adc12132", },
522 { .compatible = "ti,adc12138", },
525 MODULE_DEVICE_TABLE(of, adc12138_dt_ids);
527 static const struct spi_device_id adc12138_id[] = {
528 { "adc12130", adc12130 },
529 { "adc12132", adc12132 },
530 { "adc12138", adc12138 },
533 MODULE_DEVICE_TABLE(spi, adc12138_id);
535 static struct spi_driver adc12138_driver = {
538 .of_match_table = adc12138_dt_ids,
540 .probe = adc12138_probe,
541 .remove = adc12138_remove,
542 .id_table = adc12138_id,
544 module_spi_driver(adc12138_driver);
547 MODULE_DESCRIPTION("ADC12130/ADC12132/ADC12138 driver");
548 MODULE_LICENSE("GPL v2");