GNU Linux-libre 4.19.286-gnu1
[releases.git] / drivers / iio / adc / ti-adc084s021.c
1 /**
2  * Copyright (C) 2017 Axis Communications AB
3  *
4  * Driver for Texas Instruments' ADC084S021 ADC chip.
5  * Datasheets can be found here:
6  * http://www.ti.com/lit/ds/symlink/adc084s021.pdf
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 #include <linux/err.h>
14 #include <linux/spi/spi.h>
15 #include <linux/module.h>
16 #include <linux/interrupt.h>
17 #include <linux/iio/iio.h>
18 #include <linux/iio/buffer.h>
19 #include <linux/iio/triggered_buffer.h>
20 #include <linux/iio/trigger_consumer.h>
21 #include <linux/regulator/consumer.h>
22
23 #define ADC084S021_DRIVER_NAME "adc084s021"
24
25 struct adc084s021 {
26         struct spi_device *spi;
27         struct spi_message message;
28         struct spi_transfer spi_trans;
29         struct regulator *reg;
30         struct mutex lock;
31         /* Buffer used to align data */
32         struct {
33                 __be16 channels[4];
34                 s64 ts __aligned(8);
35         } scan;
36         /*
37          * DMA (thus cache coherency maintenance) requires the
38          * transfer buffers to live in their own cache line.
39          */
40         u16 tx_buf[4] ____cacheline_aligned;
41         __be16 rx_buf[5]; /* First 16-bits are trash */
42 };
43
44 #define ADC084S021_VOLTAGE_CHANNEL(num)                  \
45         {                                                      \
46                 .type = IIO_VOLTAGE,                                 \
47                 .channel = (num),                                    \
48                 .indexed = 1,                                        \
49                 .scan_index = (num),                                 \
50                 .scan_type = {                                       \
51                         .sign = 'u',                                       \
52                         .realbits = 8,                                     \
53                         .storagebits = 16,                                 \
54                         .shift = 4,                                        \
55                         .endianness = IIO_BE,                              \
56                 },                                                   \
57                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),        \
58                 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),\
59         }
60
61 static const struct iio_chan_spec adc084s021_channels[] = {
62         ADC084S021_VOLTAGE_CHANNEL(0),
63         ADC084S021_VOLTAGE_CHANNEL(1),
64         ADC084S021_VOLTAGE_CHANNEL(2),
65         ADC084S021_VOLTAGE_CHANNEL(3),
66         IIO_CHAN_SOFT_TIMESTAMP(4),
67 };
68
69 /**
70  * Read an ADC channel and return its value.
71  *
72  * @adc: The ADC SPI data.
73  * @data: Buffer for converted data.
74  */
75 static int adc084s021_adc_conversion(struct adc084s021 *adc, void *data)
76 {
77         int n_words = (adc->spi_trans.len >> 1) - 1; /* Discard first word */
78         int ret, i = 0;
79         u16 *p = data;
80
81         /* Do the transfer */
82         ret = spi_sync(adc->spi, &adc->message);
83         if (ret < 0)
84                 return ret;
85
86         for (; i < n_words; i++)
87                 *(p + i) = adc->rx_buf[i + 1];
88
89         return ret;
90 }
91
92 static int adc084s021_read_raw(struct iio_dev *indio_dev,
93                            struct iio_chan_spec const *channel, int *val,
94                            int *val2, long mask)
95 {
96         struct adc084s021 *adc = iio_priv(indio_dev);
97         int ret;
98
99         switch (mask) {
100         case IIO_CHAN_INFO_RAW:
101                 ret = iio_device_claim_direct_mode(indio_dev);
102                 if (ret < 0)
103                         return ret;
104
105                 ret = regulator_enable(adc->reg);
106                 if (ret) {
107                         iio_device_release_direct_mode(indio_dev);
108                         return ret;
109                 }
110
111                 adc->tx_buf[0] = channel->channel << 3;
112                 ret = adc084s021_adc_conversion(adc, val);
113                 iio_device_release_direct_mode(indio_dev);
114                 regulator_disable(adc->reg);
115                 if (ret < 0)
116                         return ret;
117
118                 *val = be16_to_cpu(*val);
119                 *val = (*val >> channel->scan_type.shift) & 0xff;
120
121                 return IIO_VAL_INT;
122         case IIO_CHAN_INFO_SCALE:
123                 ret = regulator_enable(adc->reg);
124                 if (ret)
125                         return ret;
126
127                 ret = regulator_get_voltage(adc->reg);
128                 regulator_disable(adc->reg);
129                 if (ret < 0)
130                         return ret;
131
132                 *val = ret / 1000;
133
134                 return IIO_VAL_INT;
135         default:
136                 return -EINVAL;
137         }
138 }
139
140 /**
141  * Read enabled ADC channels and push data to the buffer.
142  *
143  * @irq: The interrupt number (not used).
144  * @pollfunc: Pointer to the poll func.
145  */
146 static irqreturn_t adc084s021_buffer_trigger_handler(int irq, void *pollfunc)
147 {
148         struct iio_poll_func *pf = pollfunc;
149         struct iio_dev *indio_dev = pf->indio_dev;
150         struct adc084s021 *adc = iio_priv(indio_dev);
151
152         mutex_lock(&adc->lock);
153
154         if (adc084s021_adc_conversion(adc, adc->scan.channels) < 0)
155                 dev_err(&adc->spi->dev, "Failed to read data\n");
156
157         iio_push_to_buffers_with_timestamp(indio_dev, &adc->scan,
158                                            iio_get_time_ns(indio_dev));
159         mutex_unlock(&adc->lock);
160         iio_trigger_notify_done(indio_dev->trig);
161
162         return IRQ_HANDLED;
163 }
164
165 static int adc084s021_buffer_preenable(struct iio_dev *indio_dev)
166 {
167         struct adc084s021 *adc = iio_priv(indio_dev);
168         int scan_index;
169         int i = 0;
170
171         for_each_set_bit(scan_index, indio_dev->active_scan_mask,
172                          indio_dev->masklength) {
173                 const struct iio_chan_spec *channel =
174                         &indio_dev->channels[scan_index];
175                 adc->tx_buf[i++] = channel->channel << 3;
176         }
177         adc->spi_trans.len = 2 + (i * sizeof(__be16)); /* Trash + channels */
178
179         return regulator_enable(adc->reg);
180 }
181
182 static int adc084s021_buffer_postdisable(struct iio_dev *indio_dev)
183 {
184         struct adc084s021 *adc = iio_priv(indio_dev);
185
186         adc->spi_trans.len = 4; /* Trash + single channel */
187
188         return regulator_disable(adc->reg);
189 }
190
191 static const struct iio_info adc084s021_info = {
192         .read_raw = adc084s021_read_raw,
193 };
194
195 static const struct iio_buffer_setup_ops adc084s021_buffer_setup_ops = {
196         .preenable = adc084s021_buffer_preenable,
197         .postenable = iio_triggered_buffer_postenable,
198         .predisable = iio_triggered_buffer_predisable,
199         .postdisable = adc084s021_buffer_postdisable,
200 };
201
202 static int adc084s021_probe(struct spi_device *spi)
203 {
204         struct iio_dev *indio_dev;
205         struct adc084s021 *adc;
206         int ret;
207
208         indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc));
209         if (!indio_dev) {
210                 dev_err(&spi->dev, "Failed to allocate IIO device\n");
211                 return -ENOMEM;
212         }
213
214         adc = iio_priv(indio_dev);
215         adc->spi = spi;
216
217         /* Connect the SPI device and the iio dev */
218         spi_set_drvdata(spi, indio_dev);
219
220         /* Initiate the Industrial I/O device */
221         indio_dev->dev.parent = &spi->dev;
222         indio_dev->dev.of_node = spi->dev.of_node;
223         indio_dev->name = spi_get_device_id(spi)->name;
224         indio_dev->modes = INDIO_DIRECT_MODE;
225         indio_dev->info = &adc084s021_info;
226         indio_dev->channels = adc084s021_channels;
227         indio_dev->num_channels = ARRAY_SIZE(adc084s021_channels);
228
229         /* Create SPI transfer for channel reads */
230         adc->spi_trans.tx_buf = adc->tx_buf;
231         adc->spi_trans.rx_buf = adc->rx_buf;
232         adc->spi_trans.len = 4; /* Trash + single channel */
233         spi_message_init_with_transfers(&adc->message, &adc->spi_trans, 1);
234
235         adc->reg = devm_regulator_get(&spi->dev, "vref");
236         if (IS_ERR(adc->reg))
237                 return PTR_ERR(adc->reg);
238
239         mutex_init(&adc->lock);
240
241         /* Setup triggered buffer with pollfunction */
242         ret = devm_iio_triggered_buffer_setup(&spi->dev, indio_dev, NULL,
243                                             adc084s021_buffer_trigger_handler,
244                                             &adc084s021_buffer_setup_ops);
245         if (ret) {
246                 dev_err(&spi->dev, "Failed to setup triggered buffer\n");
247                 return ret;
248         }
249
250         return devm_iio_device_register(&spi->dev, indio_dev);
251 }
252
253 static const struct of_device_id adc084s021_of_match[] = {
254         { .compatible = "ti,adc084s021", },
255         {},
256 };
257 MODULE_DEVICE_TABLE(of, adc084s021_of_match);
258
259 static const struct spi_device_id adc084s021_id[] = {
260         { ADC084S021_DRIVER_NAME, 0},
261         {}
262 };
263 MODULE_DEVICE_TABLE(spi, adc084s021_id);
264
265 static struct spi_driver adc084s021_driver = {
266         .driver = {
267                 .name = ADC084S021_DRIVER_NAME,
268                 .of_match_table = of_match_ptr(adc084s021_of_match),
269         },
270         .probe = adc084s021_probe,
271         .id_table = adc084s021_id,
272 };
273 module_spi_driver(adc084s021_driver);
274
275 MODULE_AUTHOR("MÃ¥rten Lindahl <martenli@axis.com>");
276 MODULE_DESCRIPTION("Texas Instruments ADC084S021");
277 MODULE_LICENSE("GPL v2");
278 MODULE_VERSION("1.0");