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