]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/iio/adc/ti-adc0832.c
Merge tag 'clk-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/clk/linux
[karo-tx-linux.git] / drivers / iio / adc / ti-adc0832.c
1 /*
2  * ADC0831/ADC0832/ADC0834/ADC0838 8-bit ADC driver
3  *
4  * Copyright (c) 2016 Akinobu Mita <akinobu.mita@gmail.com>
5  *
6  * This file is subject to the terms and conditions of version 2 of
7  * the GNU General Public License.  See the file COPYING in the main
8  * directory of this archive for more details.
9  *
10  * Datasheet: http://www.ti.com/lit/ds/symlink/adc0832-n.pdf
11  */
12
13 #include <linux/module.h>
14 #include <linux/spi/spi.h>
15 #include <linux/iio/iio.h>
16 #include <linux/regulator/consumer.h>
17 #include <linux/iio/buffer.h>
18 #include <linux/iio/trigger.h>
19 #include <linux/iio/triggered_buffer.h>
20 #include <linux/iio/trigger_consumer.h>
21
22 enum {
23         adc0831,
24         adc0832,
25         adc0834,
26         adc0838,
27 };
28
29 struct adc0832 {
30         struct spi_device *spi;
31         struct regulator *reg;
32         struct mutex lock;
33         u8 mux_bits;
34
35         u8 tx_buf[2] ____cacheline_aligned;
36         u8 rx_buf[2];
37 };
38
39 #define ADC0832_VOLTAGE_CHANNEL(chan)                                   \
40         {                                                               \
41                 .type = IIO_VOLTAGE,                                    \
42                 .indexed = 1,                                           \
43                 .channel = chan,                                        \
44                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),           \
45                 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),   \
46                 .scan_index = chan,                                     \
47                 .scan_type = {                                          \
48                         .sign = 'u',                                    \
49                         .realbits = 8,                                  \
50                         .storagebits = 8,                               \
51                 },                                                      \
52         }
53
54 #define ADC0832_VOLTAGE_CHANNEL_DIFF(chan1, chan2, si)                  \
55         {                                                               \
56                 .type = IIO_VOLTAGE,                                    \
57                 .indexed = 1,                                           \
58                 .channel = (chan1),                                     \
59                 .channel2 = (chan2),                                    \
60                 .differential = 1,                                      \
61                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),           \
62                 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),   \
63                 .scan_index = si,                                       \
64                 .scan_type = {                                          \
65                         .sign = 'u',                                    \
66                         .realbits = 8,                                  \
67                         .storagebits = 8,                               \
68                 },                                                      \
69         }
70
71 static const struct iio_chan_spec adc0831_channels[] = {
72         ADC0832_VOLTAGE_CHANNEL_DIFF(0, 1, 0),
73         IIO_CHAN_SOFT_TIMESTAMP(1),
74 };
75
76 static const struct iio_chan_spec adc0832_channels[] = {
77         ADC0832_VOLTAGE_CHANNEL(0),
78         ADC0832_VOLTAGE_CHANNEL(1),
79         ADC0832_VOLTAGE_CHANNEL_DIFF(0, 1, 2),
80         ADC0832_VOLTAGE_CHANNEL_DIFF(1, 0, 3),
81         IIO_CHAN_SOFT_TIMESTAMP(4),
82 };
83
84 static const struct iio_chan_spec adc0834_channels[] = {
85         ADC0832_VOLTAGE_CHANNEL(0),
86         ADC0832_VOLTAGE_CHANNEL(1),
87         ADC0832_VOLTAGE_CHANNEL(2),
88         ADC0832_VOLTAGE_CHANNEL(3),
89         ADC0832_VOLTAGE_CHANNEL_DIFF(0, 1, 4),
90         ADC0832_VOLTAGE_CHANNEL_DIFF(1, 0, 5),
91         ADC0832_VOLTAGE_CHANNEL_DIFF(2, 3, 6),
92         ADC0832_VOLTAGE_CHANNEL_DIFF(3, 2, 7),
93         IIO_CHAN_SOFT_TIMESTAMP(8),
94 };
95
96 static const struct iio_chan_spec adc0838_channels[] = {
97         ADC0832_VOLTAGE_CHANNEL(0),
98         ADC0832_VOLTAGE_CHANNEL(1),
99         ADC0832_VOLTAGE_CHANNEL(2),
100         ADC0832_VOLTAGE_CHANNEL(3),
101         ADC0832_VOLTAGE_CHANNEL(4),
102         ADC0832_VOLTAGE_CHANNEL(5),
103         ADC0832_VOLTAGE_CHANNEL(6),
104         ADC0832_VOLTAGE_CHANNEL(7),
105         ADC0832_VOLTAGE_CHANNEL_DIFF(0, 1, 8),
106         ADC0832_VOLTAGE_CHANNEL_DIFF(1, 0, 9),
107         ADC0832_VOLTAGE_CHANNEL_DIFF(2, 3, 10),
108         ADC0832_VOLTAGE_CHANNEL_DIFF(3, 2, 11),
109         ADC0832_VOLTAGE_CHANNEL_DIFF(4, 5, 12),
110         ADC0832_VOLTAGE_CHANNEL_DIFF(5, 4, 13),
111         ADC0832_VOLTAGE_CHANNEL_DIFF(6, 7, 14),
112         ADC0832_VOLTAGE_CHANNEL_DIFF(7, 6, 15),
113         IIO_CHAN_SOFT_TIMESTAMP(16),
114 };
115
116 static int adc0831_adc_conversion(struct adc0832 *adc)
117 {
118         struct spi_device *spi = adc->spi;
119         int ret;
120
121         ret = spi_read(spi, &adc->rx_buf, 2);
122         if (ret)
123                 return ret;
124
125         /*
126          * Skip TRI-STATE and a leading zero
127          */
128         return (adc->rx_buf[0] << 2 & 0xff) | (adc->rx_buf[1] >> 6);
129 }
130
131 static int adc0832_adc_conversion(struct adc0832 *adc, int channel,
132                                 bool differential)
133 {
134         struct spi_device *spi = adc->spi;
135         struct spi_transfer xfer = {
136                 .tx_buf = adc->tx_buf,
137                 .rx_buf = adc->rx_buf,
138                 .len = 2,
139         };
140         int ret;
141
142         if (!adc->mux_bits)
143                 return adc0831_adc_conversion(adc);
144
145         /* start bit */
146         adc->tx_buf[0] = 1 << (adc->mux_bits + 1);
147         /* single-ended or differential */
148         adc->tx_buf[0] |= differential ? 0 : (1 << adc->mux_bits);
149         /* odd / sign */
150         adc->tx_buf[0] |= (channel % 2) << (adc->mux_bits - 1);
151         /* select */
152         if (adc->mux_bits > 1)
153                 adc->tx_buf[0] |= channel / 2;
154
155         /* align Data output BIT7 (MSB) to 8-bit boundary */
156         adc->tx_buf[0] <<= 1;
157
158         ret = spi_sync_transfer(spi, &xfer, 1);
159         if (ret)
160                 return ret;
161
162         return adc->rx_buf[1];
163 }
164
165 static int adc0832_read_raw(struct iio_dev *iio,
166                         struct iio_chan_spec const *channel, int *value,
167                         int *shift, long mask)
168 {
169         struct adc0832 *adc = iio_priv(iio);
170
171         switch (mask) {
172         case IIO_CHAN_INFO_RAW:
173                 mutex_lock(&adc->lock);
174                 *value = adc0832_adc_conversion(adc, channel->channel,
175                                                 channel->differential);
176                 mutex_unlock(&adc->lock);
177                 if (*value < 0)
178                         return *value;
179
180                 return IIO_VAL_INT;
181         case IIO_CHAN_INFO_SCALE:
182                 *value = regulator_get_voltage(adc->reg);
183                 if (*value < 0)
184                         return *value;
185
186                 /* convert regulator output voltage to mV */
187                 *value /= 1000;
188                 *shift = 8;
189
190                 return IIO_VAL_FRACTIONAL_LOG2;
191         }
192
193         return -EINVAL;
194 }
195
196 static const struct iio_info adc0832_info = {
197         .read_raw = adc0832_read_raw,
198         .driver_module = THIS_MODULE,
199 };
200
201 static irqreturn_t adc0832_trigger_handler(int irq, void *p)
202 {
203         struct iio_poll_func *pf = p;
204         struct iio_dev *indio_dev = pf->indio_dev;
205         struct adc0832 *adc = iio_priv(indio_dev);
206         u8 data[24] = { }; /* 16x 1 byte ADC data + 8 bytes timestamp */
207         int scan_index;
208         int i = 0;
209
210         mutex_lock(&adc->lock);
211
212         for_each_set_bit(scan_index, indio_dev->active_scan_mask,
213                          indio_dev->masklength) {
214                 const struct iio_chan_spec *scan_chan =
215                                 &indio_dev->channels[scan_index];
216                 int ret = adc0832_adc_conversion(adc, scan_chan->channel,
217                                                  scan_chan->differential);
218                 if (ret < 0) {
219                         dev_warn(&adc->spi->dev,
220                                  "failed to get conversion data\n");
221                         goto out;
222                 }
223
224                 data[i] = ret;
225                 i++;
226         }
227         iio_push_to_buffers_with_timestamp(indio_dev, data,
228                                            iio_get_time_ns(indio_dev));
229 out:
230         mutex_unlock(&adc->lock);
231
232         iio_trigger_notify_done(indio_dev->trig);
233
234         return IRQ_HANDLED;
235 }
236
237 static int adc0832_probe(struct spi_device *spi)
238 {
239         struct iio_dev *indio_dev;
240         struct adc0832 *adc;
241         int ret;
242
243         indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc));
244         if (!indio_dev)
245                 return -ENOMEM;
246
247         adc = iio_priv(indio_dev);
248         adc->spi = spi;
249         mutex_init(&adc->lock);
250
251         indio_dev->name = spi_get_device_id(spi)->name;
252         indio_dev->dev.parent = &spi->dev;
253         indio_dev->dev.of_node = spi->dev.of_node;
254         indio_dev->info = &adc0832_info;
255         indio_dev->modes = INDIO_DIRECT_MODE;
256
257         switch (spi_get_device_id(spi)->driver_data) {
258         case adc0831:
259                 adc->mux_bits = 0;
260                 indio_dev->channels = adc0831_channels;
261                 indio_dev->num_channels = ARRAY_SIZE(adc0831_channels);
262                 break;
263         case adc0832:
264                 adc->mux_bits = 1;
265                 indio_dev->channels = adc0832_channels;
266                 indio_dev->num_channels = ARRAY_SIZE(adc0832_channels);
267                 break;
268         case adc0834:
269                 adc->mux_bits = 2;
270                 indio_dev->channels = adc0834_channels;
271                 indio_dev->num_channels = ARRAY_SIZE(adc0834_channels);
272                 break;
273         case adc0838:
274                 adc->mux_bits = 3;
275                 indio_dev->channels = adc0838_channels;
276                 indio_dev->num_channels = ARRAY_SIZE(adc0838_channels);
277                 break;
278         default:
279                 return -EINVAL;
280         }
281
282         adc->reg = devm_regulator_get(&spi->dev, "vref");
283         if (IS_ERR(adc->reg))
284                 return PTR_ERR(adc->reg);
285
286         ret = regulator_enable(adc->reg);
287         if (ret)
288                 return ret;
289
290         spi_set_drvdata(spi, indio_dev);
291
292         ret = iio_triggered_buffer_setup(indio_dev, NULL,
293                                          adc0832_trigger_handler, NULL);
294         if (ret)
295                 goto err_reg_disable;
296
297         ret = iio_device_register(indio_dev);
298         if (ret)
299                 goto err_buffer_cleanup;
300
301         return 0;
302 err_buffer_cleanup:
303         iio_triggered_buffer_cleanup(indio_dev);
304 err_reg_disable:
305         regulator_disable(adc->reg);
306
307         return ret;
308 }
309
310 static int adc0832_remove(struct spi_device *spi)
311 {
312         struct iio_dev *indio_dev = spi_get_drvdata(spi);
313         struct adc0832 *adc = iio_priv(indio_dev);
314
315         iio_device_unregister(indio_dev);
316         iio_triggered_buffer_cleanup(indio_dev);
317         regulator_disable(adc->reg);
318
319         return 0;
320 }
321
322 #ifdef CONFIG_OF
323
324 static const struct of_device_id adc0832_dt_ids[] = {
325         { .compatible = "ti,adc0831", },
326         { .compatible = "ti,adc0832", },
327         { .compatible = "ti,adc0834", },
328         { .compatible = "ti,adc0838", },
329         {}
330 };
331 MODULE_DEVICE_TABLE(of, adc0832_dt_ids);
332
333 #endif
334
335 static const struct spi_device_id adc0832_id[] = {
336         { "adc0831", adc0831 },
337         { "adc0832", adc0832 },
338         { "adc0834", adc0834 },
339         { "adc0838", adc0838 },
340         {}
341 };
342 MODULE_DEVICE_TABLE(spi, adc0832_id);
343
344 static struct spi_driver adc0832_driver = {
345         .driver = {
346                 .name = "adc0832",
347                 .of_match_table = of_match_ptr(adc0832_dt_ids),
348         },
349         .probe = adc0832_probe,
350         .remove = adc0832_remove,
351         .id_table = adc0832_id,
352 };
353 module_spi_driver(adc0832_driver);
354
355 MODULE_AUTHOR("Akinobu Mita <akinobu.mita@gmail.com>");
356 MODULE_DESCRIPTION("ADC0831/ADC0832/ADC0834/ADC0838 driver");
357 MODULE_LICENSE("GPL v2");