File Index
Files and directories
Sign in
FILE

ti-tlc4541.c

File preview with syntax highlighting.

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