xref: /linux/drivers/iio/adc/ti-adc084s021.c (revision d2c9a99135da931377240942d44f3dea104cedb8)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2017 Axis Communications AB
4  *
5  * Driver for Texas Instruments' ADC084S021 ADC chip.
6  * Datasheets can be found here:
7  * https://www.ti.com/lit/ds/symlink/adc084s021.pdf
8  */
9 
10 #include <linux/err.h>
11 #include <linux/spi/spi.h>
12 #include <linux/module.h>
13 #include <linux/interrupt.h>
14 #include <linux/iio/iio.h>
15 #include <linux/iio/buffer.h>
16 #include <linux/iio/triggered_buffer.h>
17 #include <linux/iio/trigger_consumer.h>
18 #include <linux/regulator/consumer.h>
19 
20 #define ADC084S021_DRIVER_NAME "adc084s021"
21 
22 struct adc084s021 {
23 	struct spi_device *spi;
24 	struct spi_message message;
25 	struct spi_transfer spi_trans;
26 	struct regulator *reg;
27 	struct mutex lock;
28 	/* Buffer used to align data */
29 	struct {
30 		__be16 channels[4];
31 		aligned_s64 ts;
32 	} scan;
33 	/*
34 	 * DMA (thus cache coherency maintenance) may require the
35 	 * transfer buffers to live in their own cache line.
36 	 */
37 	u16 tx_buf[4] __aligned(IIO_DMA_MINALIGN);
38 	__be16 rx_buf[5]; /* First 16-bits are trash */
39 };
40 
41 #define ADC084S021_VOLTAGE_CHANNEL(num)                  \
42 	{                                                      \
43 		.type = IIO_VOLTAGE,                                 \
44 		.channel = (num),                                    \
45 		.indexed = 1,                                        \
46 		.scan_index = (num),                                 \
47 		.scan_type = {                                       \
48 			.sign = 'u',                                       \
49 			.realbits = 8,                                     \
50 			.storagebits = 16,                                 \
51 			.shift = 4,                                        \
52 			.endianness = IIO_BE,                              \
53 		},                                                   \
54 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),        \
55 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),\
56 	}
57 
58 static const struct iio_chan_spec adc084s021_channels[] = {
59 	ADC084S021_VOLTAGE_CHANNEL(0),
60 	ADC084S021_VOLTAGE_CHANNEL(1),
61 	ADC084S021_VOLTAGE_CHANNEL(2),
62 	ADC084S021_VOLTAGE_CHANNEL(3),
63 	IIO_CHAN_SOFT_TIMESTAMP(4),
64 };
65 
66 /**
67  * adc084s021_adc_conversion() - Read an ADC channel and return its value.
68  *
69  * @adc: The ADC SPI data.
70  * @data: Buffer for converted data.
71  */
adc084s021_adc_conversion(struct adc084s021 * adc,__be16 * data)72 static int adc084s021_adc_conversion(struct adc084s021 *adc, __be16 *data)
73 {
74 	int n_words = (adc->spi_trans.len >> 1) - 1; /* Discard first word */
75 	int ret, i = 0;
76 
77 	/* Do the transfer */
78 	ret = spi_sync(adc->spi, &adc->message);
79 	if (ret < 0)
80 		return ret;
81 
82 	for (; i < n_words; i++)
83 		*(data + i) = adc->rx_buf[i + 1];
84 
85 	return ret;
86 }
87 
adc084s021_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * channel,int * val,int * val2,long mask)88 static int adc084s021_read_raw(struct iio_dev *indio_dev,
89 			   struct iio_chan_spec const *channel, int *val,
90 			   int *val2, long mask)
91 {
92 	struct adc084s021 *adc = iio_priv(indio_dev);
93 	int ret;
94 	__be16 be_val;
95 
96 	switch (mask) {
97 	case IIO_CHAN_INFO_RAW:
98 		if (!iio_device_claim_direct(indio_dev))
99 			return -EBUSY;
100 
101 		ret = regulator_enable(adc->reg);
102 		if (ret) {
103 			iio_device_release_direct(indio_dev);
104 			return ret;
105 		}
106 
107 		adc->tx_buf[0] = channel->channel << 3;
108 		ret = adc084s021_adc_conversion(adc, &be_val);
109 		iio_device_release_direct(indio_dev);
110 		regulator_disable(adc->reg);
111 		if (ret < 0)
112 			return ret;
113 
114 		*val = be16_to_cpu(be_val);
115 		*val = (*val >> channel->scan_type.shift) & 0xff;
116 
117 		return IIO_VAL_INT;
118 	case IIO_CHAN_INFO_SCALE:
119 		ret = regulator_enable(adc->reg);
120 		if (ret)
121 			return ret;
122 
123 		ret = regulator_get_voltage(adc->reg);
124 		regulator_disable(adc->reg);
125 		if (ret < 0)
126 			return ret;
127 
128 		*val = ret / 1000;
129 
130 		return IIO_VAL_INT;
131 	default:
132 		return -EINVAL;
133 	}
134 }
135 
136 /**
137  * adc084s021_buffer_trigger_handler() - Read ADC channels and push to buffer.
138  *
139  * @irq: The interrupt number (not used).
140  * @pollfunc: Pointer to the poll func.
141  */
adc084s021_buffer_trigger_handler(int irq,void * pollfunc)142 static irqreturn_t adc084s021_buffer_trigger_handler(int irq, void *pollfunc)
143 {
144 	struct iio_poll_func *pf = pollfunc;
145 	struct iio_dev *indio_dev = pf->indio_dev;
146 	struct adc084s021 *adc = iio_priv(indio_dev);
147 
148 	mutex_lock(&adc->lock);
149 
150 	if (adc084s021_adc_conversion(adc, adc->scan.channels) < 0)
151 		dev_err(&adc->spi->dev, "Failed to read data\n");
152 
153 	iio_push_to_buffers_with_ts(indio_dev, &adc->scan, sizeof(adc->scan),
154 				    iio_get_time_ns(indio_dev));
155 	mutex_unlock(&adc->lock);
156 	iio_trigger_notify_done(indio_dev->trig);
157 
158 	return IRQ_HANDLED;
159 }
160 
adc084s021_buffer_preenable(struct iio_dev * indio_dev)161 static int adc084s021_buffer_preenable(struct iio_dev *indio_dev)
162 {
163 	struct adc084s021 *adc = iio_priv(indio_dev);
164 	int scan_index;
165 	int i = 0;
166 
167 	iio_for_each_active_channel(indio_dev, scan_index) {
168 		const struct iio_chan_spec *channel =
169 			&indio_dev->channels[scan_index];
170 		adc->tx_buf[i++] = channel->channel << 3;
171 	}
172 	adc->spi_trans.len = 2 + (i * sizeof(__be16)); /* Trash + channels */
173 
174 	return regulator_enable(adc->reg);
175 }
176 
adc084s021_buffer_postdisable(struct iio_dev * indio_dev)177 static int adc084s021_buffer_postdisable(struct iio_dev *indio_dev)
178 {
179 	struct adc084s021 *adc = iio_priv(indio_dev);
180 
181 	adc->spi_trans.len = 4; /* Trash + single channel */
182 
183 	return regulator_disable(adc->reg);
184 }
185 
186 static const struct iio_info adc084s021_info = {
187 	.read_raw = adc084s021_read_raw,
188 };
189 
190 static const struct iio_buffer_setup_ops adc084s021_buffer_setup_ops = {
191 	.preenable = adc084s021_buffer_preenable,
192 	.postdisable = adc084s021_buffer_postdisable,
193 };
194 
adc084s021_probe(struct spi_device * spi)195 static int adc084s021_probe(struct spi_device *spi)
196 {
197 	struct iio_dev *indio_dev;
198 	struct adc084s021 *adc;
199 	int ret;
200 
201 	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc));
202 	if (!indio_dev)
203 		return -ENOMEM;
204 
205 	adc = iio_priv(indio_dev);
206 	adc->spi = spi;
207 
208 	/* Initiate the Industrial I/O device */
209 	indio_dev->name = spi_get_device_id(spi)->name;
210 	indio_dev->modes = INDIO_DIRECT_MODE;
211 	indio_dev->info = &adc084s021_info;
212 	indio_dev->channels = adc084s021_channels;
213 	indio_dev->num_channels = ARRAY_SIZE(adc084s021_channels);
214 
215 	/* Create SPI transfer for channel reads */
216 	adc->spi_trans.tx_buf = adc->tx_buf;
217 	adc->spi_trans.rx_buf = adc->rx_buf;
218 	adc->spi_trans.len = 4; /* Trash + single channel */
219 	spi_message_init_with_transfers(&adc->message, &adc->spi_trans, 1);
220 
221 	adc->reg = devm_regulator_get(&spi->dev, "vref");
222 	if (IS_ERR(adc->reg))
223 		return PTR_ERR(adc->reg);
224 
225 	mutex_init(&adc->lock);
226 
227 	/* Setup triggered buffer with pollfunction */
228 	ret = devm_iio_triggered_buffer_setup(&spi->dev, indio_dev, NULL,
229 					    adc084s021_buffer_trigger_handler,
230 					    &adc084s021_buffer_setup_ops);
231 	if (ret) {
232 		dev_err(&spi->dev, "Failed to setup triggered buffer\n");
233 		return ret;
234 	}
235 
236 	return devm_iio_device_register(&spi->dev, indio_dev);
237 }
238 
239 static const struct of_device_id adc084s021_of_match[] = {
240 	{ .compatible = "ti,adc084s021", },
241 	{ }
242 };
243 MODULE_DEVICE_TABLE(of, adc084s021_of_match);
244 
245 static const struct spi_device_id adc084s021_id[] = {
246 	{ ADC084S021_DRIVER_NAME, 0 },
247 	{ }
248 };
249 MODULE_DEVICE_TABLE(spi, adc084s021_id);
250 
251 static struct spi_driver adc084s021_driver = {
252 	.driver = {
253 		.name = ADC084S021_DRIVER_NAME,
254 		.of_match_table = adc084s021_of_match,
255 	},
256 	.probe = adc084s021_probe,
257 	.id_table = adc084s021_id,
258 };
259 module_spi_driver(adc084s021_driver);
260 
261 MODULE_AUTHOR("Mårten Lindahl <martenli@axis.com>");
262 MODULE_DESCRIPTION("Texas Instruments ADC084S021");
263 MODULE_LICENSE("GPL v2");
264 MODULE_VERSION("1.0");
265