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>
31 #define TI_ADC_DRV_NAME "ti-adc161s626"
38 static const struct iio_chan_spec ti_adc141s626_channels
[] = {
42 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
50 IIO_CHAN_SOFT_TIMESTAMP(1),
53 static const struct iio_chan_spec ti_adc161s626_channels
[] = {
57 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
65 IIO_CHAN_SOFT_TIMESTAMP(1),
69 struct iio_dev
*indio_dev
;
70 struct spi_device
*spi
;
74 u8 buffer
[16] ____cacheline_aligned
;
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
);
138 if (mask
!= IIO_CHAN_INFO_RAW
)
141 ret
= iio_device_claim_direct_mode(indio_dev
);
145 ret
= ti_adc_read_measurement(data
, chan
, val
);
146 iio_device_release_direct_mode(indio_dev
);
154 static const struct iio_info ti_adc_info
= {
155 .driver_module
= THIS_MODULE
,
156 .read_raw
= ti_adc_read_raw
,
159 static int ti_adc_probe(struct spi_device
*spi
)
161 struct iio_dev
*indio_dev
;
162 struct ti_adc_data
*data
;
165 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*data
));
169 indio_dev
->info
= &ti_adc_info
;
170 indio_dev
->dev
.parent
= &spi
->dev
;
171 indio_dev
->dev
.of_node
= spi
->dev
.of_node
;
172 indio_dev
->name
= TI_ADC_DRV_NAME
;
173 indio_dev
->modes
= INDIO_DIRECT_MODE
;
174 spi_set_drvdata(spi
, indio_dev
);
176 data
= iio_priv(indio_dev
);
179 switch (spi_get_device_id(spi
)->driver_data
) {
181 indio_dev
->channels
= ti_adc141s626_channels
;
182 indio_dev
->num_channels
= ARRAY_SIZE(ti_adc141s626_channels
);
187 indio_dev
->channels
= ti_adc161s626_channels
;
188 indio_dev
->num_channels
= ARRAY_SIZE(ti_adc161s626_channels
);
194 ret
= iio_triggered_buffer_setup(indio_dev
, NULL
,
195 ti_adc_trigger_handler
, NULL
);
199 ret
= iio_device_register(indio_dev
);
201 goto error_unreg_buffer
;
206 iio_triggered_buffer_cleanup(indio_dev
);
211 static int ti_adc_remove(struct spi_device
*spi
)
213 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
215 iio_device_unregister(indio_dev
);
216 iio_triggered_buffer_cleanup(indio_dev
);
221 static const struct of_device_id ti_adc_dt_ids
[] = {
222 { .compatible
= "ti,adc141s626", },
223 { .compatible
= "ti,adc161s626", },
226 MODULE_DEVICE_TABLE(of
, ti_adc_dt_ids
);
228 static const struct spi_device_id ti_adc_id
[] = {
229 {"adc141s626", TI_ADC141S626
},
230 {"adc161s626", TI_ADC161S626
},
233 MODULE_DEVICE_TABLE(spi
, ti_adc_id
);
235 static struct spi_driver ti_adc_driver
= {
237 .name
= TI_ADC_DRV_NAME
,
238 .of_match_table
= of_match_ptr(ti_adc_dt_ids
),
240 .probe
= ti_adc_probe
,
241 .remove
= ti_adc_remove
,
242 .id_table
= ti_adc_id
,
244 module_spi_driver(ti_adc_driver
);
246 MODULE_AUTHOR("Matt Ranostay <mranostay@gmail.com>");
247 MODULE_DESCRIPTION("Texas Instruments ADC1x1S 1-channel differential ADC");
248 MODULE_LICENSE("GPL");