sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / drivers / iio / adc / ti-adc161s626.c
blob4836a0d7aef57fd98bf0bb9408ed9a4f49f0a45f
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 .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;
184 int ret;
186 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*data));
187 if (!indio_dev)
188 return -ENOMEM;
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);
198 data->spi = spi;
200 switch (spi_get_device_id(spi)->driver_data) {
201 case TI_ADC141S626:
202 indio_dev->channels = ti_adc141s626_channels;
203 indio_dev->num_channels = ARRAY_SIZE(ti_adc141s626_channels);
204 data->shift = 0;
205 data->read_size = 2;
206 break;
207 case TI_ADC161S626:
208 indio_dev->channels = ti_adc161s626_channels;
209 indio_dev->num_channels = ARRAY_SIZE(ti_adc161s626_channels);
210 data->shift = 6;
211 data->read_size = 3;
212 break;
215 data->ref = devm_regulator_get(&spi->dev, "vdda");
216 if (!IS_ERR(data->ref)) {
217 ret = regulator_enable(data->ref);
218 if (ret < 0)
219 return ret;
222 ret = iio_triggered_buffer_setup(indio_dev, NULL,
223 ti_adc_trigger_handler, NULL);
224 if (ret)
225 goto error_regulator_disable;
227 ret = iio_device_register(indio_dev);
228 if (ret)
229 goto error_unreg_buffer;
231 return 0;
233 error_unreg_buffer:
234 iio_triggered_buffer_cleanup(indio_dev);
236 error_regulator_disable:
237 regulator_disable(data->ref);
239 return ret;
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);
251 return 0;
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 = {
269 .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");