1 // SPDX-License-Identifier: GPL-2.0-only
3 * ADC12130/ADC12132/ADC12138 12-bit plus sign ADC driver
5 * Copyright (c) 2016 Akinobu Mita <akinobu.mita@gmail.com>
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 iio_for_each_active_channel(indio_dev
, scan_index
) {
348 const struct iio_chan_spec
*scan_chan
=
349 &indio_dev
->channels
[scan_index
];
351 reinit_completion(&adc
->complete
);
353 ret
= adc12138_start_and_read_conv(adc
, scan_chan
,
354 i
? &adc
->data
[i
- 1] : &trash
);
356 dev_warn(&adc
->spi
->dev
,
357 "failed to start conversion\n");
361 ret
= adc12138_wait_eoc(adc
, msecs_to_jiffies(100));
363 dev_warn(&adc
->spi
->dev
, "wait eoc timeout\n");
371 ret
= adc12138_read_conv_data(adc
, &adc
->data
[i
- 1]);
373 dev_warn(&adc
->spi
->dev
,
374 "failed to get conversion data\n");
379 iio_push_to_buffers_with_timestamp(indio_dev
, adc
->data
,
380 iio_get_time_ns(indio_dev
));
382 mutex_unlock(&adc
->lock
);
384 iio_trigger_notify_done(indio_dev
->trig
);
389 static irqreturn_t
adc12138_eoc_handler(int irq
, void *p
)
391 struct iio_dev
*indio_dev
= p
;
392 struct adc12138
*adc
= iio_priv(indio_dev
);
394 complete(&adc
->complete
);
399 static int adc12138_probe(struct spi_device
*spi
)
401 struct iio_dev
*indio_dev
;
402 struct adc12138
*adc
;
405 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*adc
));
409 adc
= iio_priv(indio_dev
);
411 adc
->id
= spi_get_device_id(spi
)->driver_data
;
412 mutex_init(&adc
->lock
);
413 init_completion(&adc
->complete
);
415 indio_dev
->name
= spi_get_device_id(spi
)->name
;
416 indio_dev
->info
= &adc12138_info
;
417 indio_dev
->modes
= INDIO_DIRECT_MODE
;
422 indio_dev
->channels
= adc12132_channels
;
423 indio_dev
->num_channels
= ARRAY_SIZE(adc12132_channels
);
426 indio_dev
->channels
= adc12138_channels
;
427 indio_dev
->num_channels
= ARRAY_SIZE(adc12138_channels
);
433 ret
= device_property_read_u32(&spi
->dev
, "ti,acquisition-time",
434 &adc
->acquisition_time
);
436 adc
->acquisition_time
= 10;
438 adc
->cclk
= devm_clk_get(&spi
->dev
, NULL
);
439 if (IS_ERR(adc
->cclk
))
440 return PTR_ERR(adc
->cclk
);
442 adc
->vref_p
= devm_regulator_get(&spi
->dev
, "vref-p");
443 if (IS_ERR(adc
->vref_p
))
444 return PTR_ERR(adc
->vref_p
);
446 adc
->vref_n
= devm_regulator_get_optional(&spi
->dev
, "vref-n");
447 if (IS_ERR(adc
->vref_n
)) {
449 * Assume vref_n is 0V if an optional regulator is not
450 * specified, otherwise return the error code.
452 ret
= PTR_ERR(adc
->vref_n
);
457 ret
= devm_request_irq(&spi
->dev
, spi
->irq
, adc12138_eoc_handler
,
458 IRQF_TRIGGER_RISING
, indio_dev
->name
, indio_dev
);
462 ret
= clk_prepare_enable(adc
->cclk
);
466 ret
= regulator_enable(adc
->vref_p
);
468 goto err_clk_disable
;
470 if (!IS_ERR(adc
->vref_n
)) {
471 ret
= regulator_enable(adc
->vref_n
);
473 goto err_vref_p_disable
;
476 ret
= adc12138_init(adc
);
478 goto err_vref_n_disable
;
480 spi_set_drvdata(spi
, indio_dev
);
482 ret
= iio_triggered_buffer_setup(indio_dev
, NULL
,
483 adc12138_trigger_handler
, NULL
);
485 goto err_vref_n_disable
;
487 ret
= iio_device_register(indio_dev
);
489 goto err_buffer_cleanup
;
493 iio_triggered_buffer_cleanup(indio_dev
);
495 if (!IS_ERR(adc
->vref_n
))
496 regulator_disable(adc
->vref_n
);
498 regulator_disable(adc
->vref_p
);
500 clk_disable_unprepare(adc
->cclk
);
505 static void adc12138_remove(struct spi_device
*spi
)
507 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
508 struct adc12138
*adc
= iio_priv(indio_dev
);
510 iio_device_unregister(indio_dev
);
511 iio_triggered_buffer_cleanup(indio_dev
);
512 if (!IS_ERR(adc
->vref_n
))
513 regulator_disable(adc
->vref_n
);
514 regulator_disable(adc
->vref_p
);
515 clk_disable_unprepare(adc
->cclk
);
518 static const struct of_device_id adc12138_dt_ids
[] = {
519 { .compatible
= "ti,adc12130", },
520 { .compatible
= "ti,adc12132", },
521 { .compatible
= "ti,adc12138", },
524 MODULE_DEVICE_TABLE(of
, adc12138_dt_ids
);
526 static const struct spi_device_id adc12138_id
[] = {
527 { "adc12130", adc12130
},
528 { "adc12132", adc12132
},
529 { "adc12138", adc12138
},
532 MODULE_DEVICE_TABLE(spi
, adc12138_id
);
534 static struct spi_driver adc12138_driver
= {
537 .of_match_table
= adc12138_dt_ids
,
539 .probe
= adc12138_probe
,
540 .remove
= adc12138_remove
,
541 .id_table
= adc12138_id
,
543 module_spi_driver(adc12138_driver
);
545 MODULE_AUTHOR("Akinobu Mita <akinobu.mita@gmail.com>");
546 MODULE_DESCRIPTION("ADC12130/ADC12132/ADC12138 driver");
547 MODULE_LICENSE("GPL v2");