xref: /openbmc/linux/drivers/iio/adc/ti-tlc4541.c (revision 6d99a79c)
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 };
161 
162 static int tlc4541_probe(struct spi_device *spi)
163 {
164 	struct tlc4541_state *st;
165 	struct iio_dev *indio_dev;
166 	const struct tlc4541_chip_info *info;
167 	int ret;
168 	int8_t device_init = 0;
169 
170 	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
171 	if (indio_dev == NULL)
172 		return -ENOMEM;
173 
174 	st = iio_priv(indio_dev);
175 
176 	spi_set_drvdata(spi, indio_dev);
177 
178 	st->spi = spi;
179 
180 	info = &tlc4541_chip_info[spi_get_device_id(spi)->driver_data];
181 
182 	indio_dev->name = spi_get_device_id(spi)->name;
183 	indio_dev->dev.parent = &spi->dev;
184 	indio_dev->modes = INDIO_DIRECT_MODE;
185 	indio_dev->channels = info->channels;
186 	indio_dev->num_channels = info->num_channels;
187 	indio_dev->info = &tlc4541_info;
188 
189 	/* perform reset */
190 	spi_write(spi, &device_init, 1);
191 
192 	/* Setup default message */
193 	st->scan_single_xfer[0].rx_buf = &st->rx_buf[0];
194 	st->scan_single_xfer[0].len = 3;
195 	st->scan_single_xfer[1].delay_usecs = 3;
196 	st->scan_single_xfer[2].rx_buf = &st->rx_buf[0];
197 	st->scan_single_xfer[2].len = 2;
198 
199 	spi_message_init_with_transfers(&st->scan_single_msg,
200 					st->scan_single_xfer, 3);
201 
202 	st->reg = devm_regulator_get(&spi->dev, "vref");
203 	if (IS_ERR(st->reg))
204 		return PTR_ERR(st->reg);
205 
206 	ret = regulator_enable(st->reg);
207 	if (ret)
208 		return ret;
209 
210 	ret = iio_triggered_buffer_setup(indio_dev, NULL,
211 			&tlc4541_trigger_handler, NULL);
212 	if (ret)
213 		goto error_disable_reg;
214 
215 	ret = iio_device_register(indio_dev);
216 	if (ret)
217 		goto error_cleanup_buffer;
218 
219 	return 0;
220 
221 error_cleanup_buffer:
222 	iio_triggered_buffer_cleanup(indio_dev);
223 error_disable_reg:
224 	regulator_disable(st->reg);
225 
226 	return ret;
227 }
228 
229 static int tlc4541_remove(struct spi_device *spi)
230 {
231 	struct iio_dev *indio_dev = spi_get_drvdata(spi);
232 	struct tlc4541_state *st = iio_priv(indio_dev);
233 
234 	iio_device_unregister(indio_dev);
235 	iio_triggered_buffer_cleanup(indio_dev);
236 	regulator_disable(st->reg);
237 
238 	return 0;
239 }
240 
241 #ifdef CONFIG_OF
242 static const struct of_device_id tlc4541_dt_ids[] = {
243 	{ .compatible = "ti,tlc3541", },
244 	{ .compatible = "ti,tlc4541", },
245 	{}
246 };
247 MODULE_DEVICE_TABLE(of, tlc4541_dt_ids);
248 #endif
249 
250 static const struct spi_device_id tlc4541_id[] = {
251 	{"tlc3541", TLC3541},
252 	{"tlc4541", TLC4541},
253 	{}
254 };
255 MODULE_DEVICE_TABLE(spi, tlc4541_id);
256 
257 static struct spi_driver tlc4541_driver = {
258 	.driver = {
259 		.name   = "tlc4541",
260 		.of_match_table = of_match_ptr(tlc4541_dt_ids),
261 	},
262 	.probe          = tlc4541_probe,
263 	.remove         = tlc4541_remove,
264 	.id_table       = tlc4541_id,
265 };
266 module_spi_driver(tlc4541_driver);
267 
268 MODULE_AUTHOR("Phil Reid <preid@electromag.com.au>");
269 MODULE_DESCRIPTION("Texas Instruments TLC4541 ADC");
270 MODULE_LICENSE("GPL v2");
271