xtensa: support DMA buffers in high memory
[cris-mirror.git] / drivers / iio / adc / ti-adc161s626.c
blob10fa7677ac4b98ec32589fda1adb763207785f30
1 /*
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"
34 enum {
35 TI_ADC141S626,
36 TI_ADC161S626,
39 static const struct iio_chan_spec ti_adc141s626_channels[] = {
41 .type = IIO_VOLTAGE,
42 .channel = 0,
43 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
44 BIT(IIO_CHAN_INFO_SCALE) |
45 BIT(IIO_CHAN_INFO_OFFSET),
46 .scan_index = 0,
47 .scan_type = {
48 .sign = 's',
49 .realbits = 14,
50 .storagebits = 16,
53 IIO_CHAN_SOFT_TIMESTAMP(1),
56 static const struct iio_chan_spec ti_adc161s626_channels[] = {
58 .type = IIO_VOLTAGE,
59 .channel = 0,
60 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
61 BIT(IIO_CHAN_INFO_SCALE) |
62 BIT(IIO_CHAN_INFO_OFFSET),
63 .scan_index = 0,
64 .scan_type = {
65 .sign = 's',
66 .realbits = 16,
67 .storagebits = 16,
70 IIO_CHAN_SOFT_TIMESTAMP(1),
73 struct ti_adc_data {
74 struct iio_dev *indio_dev;
75 struct spi_device *spi;
76 struct regulator *ref;
78 u8 read_size;
79 u8 shift;
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)
87 int ret;
89 switch (data->read_size) {
90 case 2: {
91 __be16 buf;
93 ret = spi_read(data->spi, (void *) &buf, 2);
94 if (ret)
95 return ret;
97 *val = be16_to_cpu(buf);
98 break;
100 case 3: {
101 __be32 buf;
103 ret = spi_read(data->spi, (void *) &buf, 3);
104 if (ret)
105 return ret;
107 *val = be32_to_cpu(buf) >> 8;
108 break;
110 default:
111 return -EINVAL;
114 *val = sign_extend32(*val >> data->shift, chan->scan_type.realbits - 1);
116 return 0;
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);
124 int ret;
126 ret = ti_adc_read_measurement(data, &indio_dev->channels[0],
127 (int *) &data->buffer);
128 if (!ret)
129 iio_push_to_buffers_with_timestamp(indio_dev,
130 data->buffer,
131 iio_get_time_ns(indio_dev));
133 iio_trigger_notify_done(indio_dev->trig);
135 return IRQ_HANDLED;
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);
143 int ret;
145 switch (mask) {
146 case IIO_CHAN_INFO_RAW:
147 ret = iio_device_claim_direct_mode(indio_dev);
148 if (ret)
149 return ret;
151 ret = ti_adc_read_measurement(data, chan, val);
152 iio_device_release_direct_mode(indio_dev);
154 if (ret)
155 return ret;
157 return IIO_VAL_INT;
158 case IIO_CHAN_INFO_SCALE:
159 ret = regulator_get_voltage(data->ref);
160 if (ret < 0)
161 return ret;
163 *val = ret / 1000;
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);
169 return IIO_VAL_INT;
172 return 0;
175 static const struct iio_info ti_adc_info = {
176 .read_raw = ti_adc_read_raw,
179 static int ti_adc_probe(struct spi_device *spi)
181 struct iio_dev *indio_dev;
182 struct ti_adc_data *data;
183 int ret;
185 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*data));
186 if (!indio_dev)
187 return -ENOMEM;
189 indio_dev->info = &ti_adc_info;
190 indio_dev->dev.parent = &spi->dev;
191 indio_dev->dev.of_node = spi->dev.of_node;
192 indio_dev->name = TI_ADC_DRV_NAME;
193 indio_dev->modes = INDIO_DIRECT_MODE;
194 spi_set_drvdata(spi, indio_dev);
196 data = iio_priv(indio_dev);
197 data->spi = spi;
199 switch (spi_get_device_id(spi)->driver_data) {
200 case TI_ADC141S626:
201 indio_dev->channels = ti_adc141s626_channels;
202 indio_dev->num_channels = ARRAY_SIZE(ti_adc141s626_channels);
203 data->shift = 0;
204 data->read_size = 2;
205 break;
206 case TI_ADC161S626:
207 indio_dev->channels = ti_adc161s626_channels;
208 indio_dev->num_channels = ARRAY_SIZE(ti_adc161s626_channels);
209 data->shift = 6;
210 data->read_size = 3;
211 break;
214 data->ref = devm_regulator_get(&spi->dev, "vdda");
215 if (!IS_ERR(data->ref)) {
216 ret = regulator_enable(data->ref);
217 if (ret < 0)
218 return ret;
221 ret = iio_triggered_buffer_setup(indio_dev, NULL,
222 ti_adc_trigger_handler, NULL);
223 if (ret)
224 goto error_regulator_disable;
226 ret = iio_device_register(indio_dev);
227 if (ret)
228 goto error_unreg_buffer;
230 return 0;
232 error_unreg_buffer:
233 iio_triggered_buffer_cleanup(indio_dev);
235 error_regulator_disable:
236 regulator_disable(data->ref);
238 return ret;
241 static int ti_adc_remove(struct spi_device *spi)
243 struct iio_dev *indio_dev = spi_get_drvdata(spi);
244 struct ti_adc_data *data = iio_priv(indio_dev);
246 iio_device_unregister(indio_dev);
247 iio_triggered_buffer_cleanup(indio_dev);
248 regulator_disable(data->ref);
250 return 0;
253 static const struct of_device_id ti_adc_dt_ids[] = {
254 { .compatible = "ti,adc141s626", },
255 { .compatible = "ti,adc161s626", },
258 MODULE_DEVICE_TABLE(of, ti_adc_dt_ids);
260 static const struct spi_device_id ti_adc_id[] = {
261 {"adc141s626", TI_ADC141S626},
262 {"adc161s626", TI_ADC161S626},
265 MODULE_DEVICE_TABLE(spi, ti_adc_id);
267 static struct spi_driver ti_adc_driver = {
268 .driver = {
269 .name = TI_ADC_DRV_NAME,
270 .of_match_table = of_match_ptr(ti_adc_dt_ids),
272 .probe = ti_adc_probe,
273 .remove = ti_adc_remove,
274 .id_table = ti_adc_id,
276 module_spi_driver(ti_adc_driver);
278 MODULE_AUTHOR("Matt Ranostay <mranostay@gmail.com>");
279 MODULE_DESCRIPTION("Texas Instruments ADC1x1S 1-channel differential ADC");
280 MODULE_LICENSE("GPL");