Linux-libre 4.14.68-gnu
[librecmc/linux-libre.git] / drivers / iio / adc / ti-tlc4541.c
1 /*
2  * TI tlc4541 ADC Driver
3  *
4  * Copyright (C) 2017 Phil Reid
5  *
6  * Datasheets can be found here:
7  * http://www.ti.com/lit/gpn/tlc3541
8  * http://www.ti.com/lit/gpn/tlc4541
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  *
14  * The tlc4541 requires 24 clock cycles to start a transfer.
15  * Conversion then takes 2.94us to complete before data is ready
16  * Data is returned MSB first.
17  */
18
19 #include <linux/delay.h>
20 #include <linux/device.h>
21 #include <linux/err.h>
22 #include <linux/interrupt.h>
23 #include <linux/iio/iio.h>
24 #include <linux/iio/sysfs.h>
25 #include <linux/iio/buffer.h>
26 #include <linux/iio/trigger_consumer.h>
27 #include <linux/iio/triggered_buffer.h>
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/regulator/consumer.h>
31 #include <linux/slab.h>
32 #include <linux/spi/spi.h>
33 #include <linux/sysfs.h>
34
35 struct tlc4541_state {
36         struct spi_device               *spi;
37         struct regulator                *reg;
38         struct spi_transfer             scan_single_xfer[3];
39         struct spi_message              scan_single_msg;
40
41         /*
42          * DMA (thus cache coherency maintenance) requires the
43          * transfer buffers to live in their own cache lines.
44          * 2 bytes data + 6 bytes padding + 8 bytes timestamp when
45          * call iio_push_to_buffers_with_timestamp.
46          */
47         __be16                          rx_buf[8] ____cacheline_aligned;
48 };
49
50 struct tlc4541_chip_info {
51         const struct iio_chan_spec *channels;
52         unsigned int num_channels;
53 };
54
55 enum tlc4541_id {
56         TLC3541,
57         TLC4541,
58 };
59
60 #define TLC4541_V_CHAN(bits, bitshift) {                              \
61                 .type = IIO_VOLTAGE,                                  \
62                 .info_mask_separate       = BIT(IIO_CHAN_INFO_RAW),   \
63                 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
64                 .scan_type = {                                        \
65                         .sign = 'u',                                  \
66                         .realbits = (bits),                           \
67                         .storagebits = 16,                            \
68                         .shift = (bitshift),                          \
69                         .endianness = IIO_BE,                         \
70                 },                                                    \
71         }
72
73 #define DECLARE_TLC4541_CHANNELS(name, bits, bitshift) \
74 const struct iio_chan_spec name ## _channels[] = { \
75         TLC4541_V_CHAN(bits, bitshift), \
76         IIO_CHAN_SOFT_TIMESTAMP(1), \
77 }
78
79 static DECLARE_TLC4541_CHANNELS(tlc3541, 14, 2);
80 static DECLARE_TLC4541_CHANNELS(tlc4541, 16, 0);
81
82 static const struct tlc4541_chip_info tlc4541_chip_info[] = {
83         [TLC3541] = {
84                 .channels = tlc3541_channels,
85                 .num_channels = ARRAY_SIZE(tlc3541_channels),
86         },
87         [TLC4541] = {
88                 .channels = tlc4541_channels,
89                 .num_channels = ARRAY_SIZE(tlc4541_channels),
90         },
91 };
92
93 static irqreturn_t tlc4541_trigger_handler(int irq, void *p)
94 {
95         struct iio_poll_func *pf = p;
96         struct iio_dev *indio_dev = pf->indio_dev;
97         struct tlc4541_state *st = iio_priv(indio_dev);
98         int ret;
99
100         ret = spi_sync(st->spi, &st->scan_single_msg);
101         if (ret < 0)
102                 goto done;
103
104         iio_push_to_buffers_with_timestamp(indio_dev, st->rx_buf,
105                                            iio_get_time_ns(indio_dev));
106
107 done:
108         iio_trigger_notify_done(indio_dev->trig);
109         return IRQ_HANDLED;
110 }
111
112 static int tlc4541_get_range(struct tlc4541_state *st)
113 {
114         int vref;
115
116         vref = regulator_get_voltage(st->reg);
117         if (vref < 0)
118                 return vref;
119
120         vref /= 1000;
121
122         return vref;
123 }
124
125 static int tlc4541_read_raw(struct iio_dev *indio_dev,
126                             struct iio_chan_spec const *chan,
127                             int *val,
128                             int *val2,
129                             long m)
130 {
131         int ret = 0;
132         struct tlc4541_state *st = iio_priv(indio_dev);
133
134         switch (m) {
135         case IIO_CHAN_INFO_RAW:
136                 ret = iio_device_claim_direct_mode(indio_dev);
137                 if (ret)
138                         return ret;
139                 ret = spi_sync(st->spi, &st->scan_single_msg);
140                 iio_device_release_direct_mode(indio_dev);
141                 if (ret < 0)
142                         return ret;
143                 *val = be16_to_cpu(st->rx_buf[0]);
144                 *val = *val >> chan->scan_type.shift;
145                 *val &= GENMASK(chan->scan_type.realbits - 1, 0);
146                 return IIO_VAL_INT;
147         case IIO_CHAN_INFO_SCALE:
148                 ret = tlc4541_get_range(st);
149                 if (ret < 0)
150                         return ret;
151                 *val = ret;
152                 *val2 = chan->scan_type.realbits;
153                 return IIO_VAL_FRACTIONAL_LOG2;
154         }
155         return -EINVAL;
156 }
157
158 static const struct iio_info tlc4541_info = {
159         .read_raw = &tlc4541_read_raw,
160         .driver_module = THIS_MODULE,
161 };
162
163 static int tlc4541_probe(struct spi_device *spi)
164 {
165         struct tlc4541_state *st;
166         struct iio_dev *indio_dev;
167         const struct tlc4541_chip_info *info;
168         int ret;
169         int8_t device_init = 0;
170
171         indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
172         if (indio_dev == NULL)
173                 return -ENOMEM;
174
175         st = iio_priv(indio_dev);
176
177         spi_set_drvdata(spi, indio_dev);
178
179         st->spi = spi;
180
181         info = &tlc4541_chip_info[spi_get_device_id(spi)->driver_data];
182
183         indio_dev->name = spi_get_device_id(spi)->name;
184         indio_dev->dev.parent = &spi->dev;
185         indio_dev->modes = INDIO_DIRECT_MODE;
186         indio_dev->channels = info->channels;
187         indio_dev->num_channels = info->num_channels;
188         indio_dev->info = &tlc4541_info;
189
190         /* perform reset */
191         spi_write(spi, &device_init, 1);
192
193         /* Setup default message */
194         st->scan_single_xfer[0].rx_buf = &st->rx_buf[0];
195         st->scan_single_xfer[0].len = 3;
196         st->scan_single_xfer[1].delay_usecs = 3;
197         st->scan_single_xfer[2].rx_buf = &st->rx_buf[0];
198         st->scan_single_xfer[2].len = 2;
199
200         spi_message_init_with_transfers(&st->scan_single_msg,
201                                         st->scan_single_xfer, 3);
202
203         st->reg = devm_regulator_get(&spi->dev, "vref");
204         if (IS_ERR(st->reg))
205                 return PTR_ERR(st->reg);
206
207         ret = regulator_enable(st->reg);
208         if (ret)
209                 return ret;
210
211         ret = iio_triggered_buffer_setup(indio_dev, NULL,
212                         &tlc4541_trigger_handler, NULL);
213         if (ret)
214                 goto error_disable_reg;
215
216         ret = iio_device_register(indio_dev);
217         if (ret)
218                 goto error_cleanup_buffer;
219
220         return 0;
221
222 error_cleanup_buffer:
223         iio_triggered_buffer_cleanup(indio_dev);
224 error_disable_reg:
225         regulator_disable(st->reg);
226
227         return ret;
228 }
229
230 static int tlc4541_remove(struct spi_device *spi)
231 {
232         struct iio_dev *indio_dev = spi_get_drvdata(spi);
233         struct tlc4541_state *st = iio_priv(indio_dev);
234
235         iio_device_unregister(indio_dev);
236         iio_triggered_buffer_cleanup(indio_dev);
237         regulator_disable(st->reg);
238
239         return 0;
240 }
241
242 #ifdef CONFIG_OF
243 static const struct of_device_id tlc4541_dt_ids[] = {
244         { .compatible = "ti,tlc3541", },
245         { .compatible = "ti,tlc4541", },
246         {}
247 };
248 MODULE_DEVICE_TABLE(of, tlc4541_dt_ids);
249 #endif
250
251 static const struct spi_device_id tlc4541_id[] = {
252         {"tlc3541", TLC3541},
253         {"tlc4541", TLC4541},
254         {}
255 };
256 MODULE_DEVICE_TABLE(spi, tlc4541_id);
257
258 static struct spi_driver tlc4541_driver = {
259         .driver = {
260                 .name   = "tlc4541",
261                 .of_match_table = of_match_ptr(tlc4541_dt_ids),
262         },
263         .probe          = tlc4541_probe,
264         .remove         = tlc4541_remove,
265         .id_table       = tlc4541_id,
266 };
267 module_spi_driver(tlc4541_driver);
268
269 MODULE_AUTHOR("Phil Reid <preid@electromag.com.au>");
270 MODULE_DESCRIPTION("Texas Instruments TLC4541 ADC");
271 MODULE_LICENSE("GPL v2");