1 // SPDX-License-Identifier: GPL-2.0+
3 * ti-adc161s626.c - Texas Instruments ADC161S626 1-channel differential ADC
5 * ADC Devices Supported:
6 * adc141s626 - 14-bit ADC
7 * adc161s626 - 16-bit ADC
9 * Copyright (C) 2016-2018
10 * Author: Matt Ranostay <matt.ranostay@konsulko.com>
13 #include <linux/module.h>
14 #include <linux/mod_devicetable.h>
15 #include <linux/init.h>
16 #include <linux/err.h>
17 #include <linux/spi/spi.h>
18 #include <linux/iio/iio.h>
19 #include <linux/iio/trigger.h>
20 #include <linux/iio/buffer.h>
21 #include <linux/iio/trigger_consumer.h>
22 #include <linux/iio/triggered_buffer.h>
23 #include <linux/regulator/consumer.h>
25 #define TI_ADC_DRV_NAME "ti-adc161s626"
32 static const struct iio_chan_spec ti_adc141s626_channels
[] = {
36 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
37 BIT(IIO_CHAN_INFO_SCALE
) |
38 BIT(IIO_CHAN_INFO_OFFSET
),
46 IIO_CHAN_SOFT_TIMESTAMP(1),
49 static const struct iio_chan_spec ti_adc161s626_channels
[] = {
53 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
54 BIT(IIO_CHAN_INFO_SCALE
) |
55 BIT(IIO_CHAN_INFO_OFFSET
),
63 IIO_CHAN_SOFT_TIMESTAMP(1),
67 struct iio_dev
*indio_dev
;
68 struct spi_device
*spi
;
69 struct regulator
*ref
;
74 u8 buffer
[16] __aligned(IIO_DMA_MINALIGN
);
77 static int ti_adc_read_measurement(struct ti_adc_data
*data
,
78 struct iio_chan_spec
const *chan
, int *val
)
82 switch (data
->read_size
) {
86 ret
= spi_read(data
->spi
, (void *) &buf
, 2);
90 *val
= be16_to_cpu(buf
);
96 ret
= spi_read(data
->spi
, (void *) &buf
, 3);
100 *val
= be32_to_cpu(buf
) >> 8;
107 *val
= sign_extend32(*val
>> data
->shift
, chan
->scan_type
.realbits
- 1);
112 static irqreturn_t
ti_adc_trigger_handler(int irq
, void *private)
114 struct iio_poll_func
*pf
= private;
115 struct iio_dev
*indio_dev
= pf
->indio_dev
;
116 struct ti_adc_data
*data
= iio_priv(indio_dev
);
119 ret
= ti_adc_read_measurement(data
, &indio_dev
->channels
[0],
120 (int *) &data
->buffer
);
122 iio_push_to_buffers_with_timestamp(indio_dev
,
124 iio_get_time_ns(indio_dev
));
126 iio_trigger_notify_done(indio_dev
->trig
);
131 static int ti_adc_read_raw(struct iio_dev
*indio_dev
,
132 struct iio_chan_spec
const *chan
,
133 int *val
, int *val2
, long mask
)
135 struct ti_adc_data
*data
= iio_priv(indio_dev
);
139 case IIO_CHAN_INFO_RAW
:
140 iio_device_claim_direct_scoped(return -EBUSY
, indio_dev
) {
141 ret
= ti_adc_read_measurement(data
, chan
, val
);
147 case IIO_CHAN_INFO_SCALE
:
148 ret
= regulator_get_voltage(data
->ref
);
153 *val2
= chan
->scan_type
.realbits
;
155 return IIO_VAL_FRACTIONAL_LOG2
;
156 case IIO_CHAN_INFO_OFFSET
:
157 *val
= 1 << (chan
->scan_type
.realbits
- 1);
164 static const struct iio_info ti_adc_info
= {
165 .read_raw
= ti_adc_read_raw
,
168 static void ti_adc_reg_disable(void *reg
)
170 regulator_disable(reg
);
173 static int ti_adc_probe(struct spi_device
*spi
)
175 struct iio_dev
*indio_dev
;
176 struct ti_adc_data
*data
;
179 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*data
));
183 indio_dev
->info
= &ti_adc_info
;
184 indio_dev
->name
= TI_ADC_DRV_NAME
;
185 indio_dev
->modes
= INDIO_DIRECT_MODE
;
187 data
= iio_priv(indio_dev
);
190 switch (spi_get_device_id(spi
)->driver_data
) {
192 indio_dev
->channels
= ti_adc141s626_channels
;
193 indio_dev
->num_channels
= ARRAY_SIZE(ti_adc141s626_channels
);
198 indio_dev
->channels
= ti_adc161s626_channels
;
199 indio_dev
->num_channels
= ARRAY_SIZE(ti_adc161s626_channels
);
205 data
->ref
= devm_regulator_get(&spi
->dev
, "vdda");
206 if (IS_ERR(data
->ref
))
207 return PTR_ERR(data
->ref
);
209 ret
= regulator_enable(data
->ref
);
213 ret
= devm_add_action_or_reset(&spi
->dev
, ti_adc_reg_disable
,
218 ret
= devm_iio_triggered_buffer_setup(&spi
->dev
, indio_dev
, NULL
,
219 ti_adc_trigger_handler
, NULL
);
223 return devm_iio_device_register(&spi
->dev
, indio_dev
);
226 static const struct of_device_id ti_adc_dt_ids
[] = {
227 { .compatible
= "ti,adc141s626", },
228 { .compatible
= "ti,adc161s626", },
231 MODULE_DEVICE_TABLE(of
, ti_adc_dt_ids
);
233 static const struct spi_device_id ti_adc_id
[] = {
234 { "adc141s626", TI_ADC141S626
},
235 { "adc161s626", TI_ADC161S626
},
238 MODULE_DEVICE_TABLE(spi
, ti_adc_id
);
240 static struct spi_driver ti_adc_driver
= {
242 .name
= TI_ADC_DRV_NAME
,
243 .of_match_table
= ti_adc_dt_ids
,
245 .probe
= ti_adc_probe
,
246 .id_table
= ti_adc_id
,
248 module_spi_driver(ti_adc_driver
);
250 MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
251 MODULE_DESCRIPTION("Texas Instruments ADC1x1S 1-channel differential ADC");
252 MODULE_LICENSE("GPL");