2 * ti-adc161s626.c - Texas Instruments ADC161S626 1-channel differential ADC
4 * ADC Devices Supported:
5 * adc141s626 - 14-bit ADC
6 * adc161s626 - 16-bit ADC
8 * Copyright (C) 2016 Matt Ranostay <mranostay@gmail.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/err.h>
24 #include <linux/spi/spi.h>
25 #include <linux/iio/iio.h>
26 #include <linux/iio/trigger.h>
27 #include <linux/iio/buffer.h>
28 #include <linux/iio/trigger_consumer.h>
29 #include <linux/iio/triggered_buffer.h>
30 #include <linux/regulator/consumer.h>
32 #define TI_ADC_DRV_NAME "ti-adc161s626"
39 static const struct iio_chan_spec ti_adc141s626_channels
[] = {
43 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
44 BIT(IIO_CHAN_INFO_SCALE
) |
45 BIT(IIO_CHAN_INFO_OFFSET
),
53 IIO_CHAN_SOFT_TIMESTAMP(1),
56 static const struct iio_chan_spec ti_adc161s626_channels
[] = {
60 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
61 BIT(IIO_CHAN_INFO_SCALE
) |
62 BIT(IIO_CHAN_INFO_OFFSET
),
70 IIO_CHAN_SOFT_TIMESTAMP(1),
74 struct iio_dev
*indio_dev
;
75 struct spi_device
*spi
;
76 struct regulator
*ref
;
81 u8 buffer
[16] ____cacheline_aligned
;
84 static int ti_adc_read_measurement(struct ti_adc_data
*data
,
85 struct iio_chan_spec
const *chan
, int *val
)
89 switch (data
->read_size
) {
93 ret
= spi_read(data
->spi
, (void *) &buf
, 2);
97 *val
= be16_to_cpu(buf
);
103 ret
= spi_read(data
->spi
, (void *) &buf
, 3);
107 *val
= be32_to_cpu(buf
) >> 8;
114 *val
= sign_extend32(*val
>> data
->shift
, chan
->scan_type
.realbits
- 1);
119 static irqreturn_t
ti_adc_trigger_handler(int irq
, void *private)
121 struct iio_poll_func
*pf
= private;
122 struct iio_dev
*indio_dev
= pf
->indio_dev
;
123 struct ti_adc_data
*data
= iio_priv(indio_dev
);
126 ret
= ti_adc_read_measurement(data
, &indio_dev
->channels
[0],
127 (int *) &data
->buffer
);
129 iio_push_to_buffers_with_timestamp(indio_dev
,
131 iio_get_time_ns(indio_dev
));
133 iio_trigger_notify_done(indio_dev
->trig
);
138 static int ti_adc_read_raw(struct iio_dev
*indio_dev
,
139 struct iio_chan_spec
const *chan
,
140 int *val
, int *val2
, long mask
)
142 struct ti_adc_data
*data
= iio_priv(indio_dev
);
146 case IIO_CHAN_INFO_RAW
:
147 ret
= iio_device_claim_direct_mode(indio_dev
);
151 ret
= ti_adc_read_measurement(data
, chan
, val
);
152 iio_device_release_direct_mode(indio_dev
);
158 case IIO_CHAN_INFO_SCALE
:
159 ret
= regulator_get_voltage(data
->ref
);
164 *val2
= chan
->scan_type
.realbits
;
166 return IIO_VAL_FRACTIONAL_LOG2
;
167 case IIO_CHAN_INFO_OFFSET
:
168 *val
= 1 << (chan
->scan_type
.realbits
- 1);
175 static const struct iio_info ti_adc_info
= {
176 .driver_module
= THIS_MODULE
,
177 .read_raw
= ti_adc_read_raw
,
180 static int ti_adc_probe(struct spi_device
*spi
)
182 struct iio_dev
*indio_dev
;
183 struct ti_adc_data
*data
;
186 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*data
));
190 indio_dev
->info
= &ti_adc_info
;
191 indio_dev
->dev
.parent
= &spi
->dev
;
192 indio_dev
->dev
.of_node
= spi
->dev
.of_node
;
193 indio_dev
->name
= TI_ADC_DRV_NAME
;
194 indio_dev
->modes
= INDIO_DIRECT_MODE
;
195 spi_set_drvdata(spi
, indio_dev
);
197 data
= iio_priv(indio_dev
);
200 switch (spi_get_device_id(spi
)->driver_data
) {
202 indio_dev
->channels
= ti_adc141s626_channels
;
203 indio_dev
->num_channels
= ARRAY_SIZE(ti_adc141s626_channels
);
208 indio_dev
->channels
= ti_adc161s626_channels
;
209 indio_dev
->num_channels
= ARRAY_SIZE(ti_adc161s626_channels
);
215 data
->ref
= devm_regulator_get(&spi
->dev
, "vdda");
216 if (!IS_ERR(data
->ref
)) {
217 ret
= regulator_enable(data
->ref
);
222 ret
= iio_triggered_buffer_setup(indio_dev
, NULL
,
223 ti_adc_trigger_handler
, NULL
);
225 goto error_regulator_disable
;
227 ret
= iio_device_register(indio_dev
);
229 goto error_unreg_buffer
;
234 iio_triggered_buffer_cleanup(indio_dev
);
236 error_regulator_disable
:
237 regulator_disable(data
->ref
);
242 static int ti_adc_remove(struct spi_device
*spi
)
244 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
245 struct ti_adc_data
*data
= iio_priv(indio_dev
);
247 iio_device_unregister(indio_dev
);
248 iio_triggered_buffer_cleanup(indio_dev
);
249 regulator_disable(data
->ref
);
254 static const struct of_device_id ti_adc_dt_ids
[] = {
255 { .compatible
= "ti,adc141s626", },
256 { .compatible
= "ti,adc161s626", },
259 MODULE_DEVICE_TABLE(of
, ti_adc_dt_ids
);
261 static const struct spi_device_id ti_adc_id
[] = {
262 {"adc141s626", TI_ADC141S626
},
263 {"adc161s626", TI_ADC161S626
},
266 MODULE_DEVICE_TABLE(spi
, ti_adc_id
);
268 static struct spi_driver ti_adc_driver
= {
270 .name
= TI_ADC_DRV_NAME
,
271 .of_match_table
= of_match_ptr(ti_adc_dt_ids
),
273 .probe
= ti_adc_probe
,
274 .remove
= ti_adc_remove
,
275 .id_table
= ti_adc_id
,
277 module_spi_driver(ti_adc_driver
);
279 MODULE_AUTHOR("Matt Ranostay <mranostay@gmail.com>");
280 MODULE_DESCRIPTION("Texas Instruments ADC1x1S 1-channel differential ADC");
281 MODULE_LICENSE("GPL");