• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * http://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 		s64 ts __aligned(8);
32 	} scan;
33 	/*
34 	 * DMA (thus cache coherency maintenance) requires the
35 	 * transfer buffers to live in their own cache line.
36 	 */
37 	u16 tx_buf[4] ____cacheline_aligned;
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  * 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,void * data)72 static int adc084s021_adc_conversion(struct adc084s021 *adc, void *data)
73 {
74 	int n_words = (adc->spi_trans.len >> 1) - 1; /* Discard first word */
75 	int ret, i = 0;
76 	u16 *p = data;
77 
78 	/* Do the transfer */
79 	ret = spi_sync(adc->spi, &adc->message);
80 	if (ret < 0)
81 		return ret;
82 
83 	for (; i < n_words; i++)
84 		*(p + i) = adc->rx_buf[i + 1];
85 
86 	return ret;
87 }
88 
adc084s021_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * channel,int * val,int * val2,long mask)89 static int adc084s021_read_raw(struct iio_dev *indio_dev,
90 			   struct iio_chan_spec const *channel, int *val,
91 			   int *val2, long mask)
92 {
93 	struct adc084s021 *adc = iio_priv(indio_dev);
94 	int ret;
95 
96 	switch (mask) {
97 	case IIO_CHAN_INFO_RAW:
98 		ret = iio_device_claim_direct_mode(indio_dev);
99 		if (ret < 0)
100 			return ret;
101 
102 		ret = regulator_enable(adc->reg);
103 		if (ret) {
104 			iio_device_release_direct_mode(indio_dev);
105 			return ret;
106 		}
107 
108 		adc->tx_buf[0] = channel->channel << 3;
109 		ret = adc084s021_adc_conversion(adc, val);
110 		iio_device_release_direct_mode(indio_dev);
111 		regulator_disable(adc->reg);
112 		if (ret < 0)
113 			return ret;
114 
115 		*val = be16_to_cpu(*val);
116 		*val = (*val >> channel->scan_type.shift) & 0xff;
117 
118 		return IIO_VAL_INT;
119 	case IIO_CHAN_INFO_SCALE:
120 		ret = regulator_enable(adc->reg);
121 		if (ret)
122 			return ret;
123 
124 		ret = regulator_get_voltage(adc->reg);
125 		regulator_disable(adc->reg);
126 		if (ret < 0)
127 			return ret;
128 
129 		*val = ret / 1000;
130 
131 		return IIO_VAL_INT;
132 	default:
133 		return -EINVAL;
134 	}
135 }
136 
137 /**
138  * Read enabled ADC channels and push data to the buffer.
139  *
140  * @irq: The interrupt number (not used).
141  * @pollfunc: Pointer to the poll func.
142  */
adc084s021_buffer_trigger_handler(int irq,void * pollfunc)143 static irqreturn_t adc084s021_buffer_trigger_handler(int irq, void *pollfunc)
144 {
145 	struct iio_poll_func *pf = pollfunc;
146 	struct iio_dev *indio_dev = pf->indio_dev;
147 	struct adc084s021 *adc = iio_priv(indio_dev);
148 
149 	mutex_lock(&adc->lock);
150 
151 	if (adc084s021_adc_conversion(adc, adc->scan.channels) < 0)
152 		dev_err(&adc->spi->dev, "Failed to read data\n");
153 
154 	iio_push_to_buffers_with_timestamp(indio_dev, &adc->scan,
155 					   iio_get_time_ns(indio_dev));
156 	mutex_unlock(&adc->lock);
157 	iio_trigger_notify_done(indio_dev->trig);
158 
159 	return IRQ_HANDLED;
160 }
161 
adc084s021_buffer_preenable(struct iio_dev * indio_dev)162 static int adc084s021_buffer_preenable(struct iio_dev *indio_dev)
163 {
164 	struct adc084s021 *adc = iio_priv(indio_dev);
165 	int scan_index;
166 	int i = 0;
167 
168 	for_each_set_bit(scan_index, indio_dev->active_scan_mask,
169 			 indio_dev->masklength) {
170 		const struct iio_chan_spec *channel =
171 			&indio_dev->channels[scan_index];
172 		adc->tx_buf[i++] = channel->channel << 3;
173 	}
174 	adc->spi_trans.len = 2 + (i * sizeof(__be16)); /* Trash + channels */
175 
176 	return regulator_enable(adc->reg);
177 }
178 
adc084s021_buffer_postdisable(struct iio_dev * indio_dev)179 static int adc084s021_buffer_postdisable(struct iio_dev *indio_dev)
180 {
181 	struct adc084s021 *adc = iio_priv(indio_dev);
182 
183 	adc->spi_trans.len = 4; /* Trash + single channel */
184 
185 	return regulator_disable(adc->reg);
186 }
187 
188 static const struct iio_info adc084s021_info = {
189 	.read_raw = adc084s021_read_raw,
190 };
191 
192 static const struct iio_buffer_setup_ops adc084s021_buffer_setup_ops = {
193 	.preenable = adc084s021_buffer_preenable,
194 	.postenable = iio_triggered_buffer_postenable,
195 	.predisable = iio_triggered_buffer_predisable,
196 	.postdisable = adc084s021_buffer_postdisable,
197 };
198 
adc084s021_probe(struct spi_device * spi)199 static int adc084s021_probe(struct spi_device *spi)
200 {
201 	struct iio_dev *indio_dev;
202 	struct adc084s021 *adc;
203 	int ret;
204 
205 	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc));
206 	if (!indio_dev) {
207 		dev_err(&spi->dev, "Failed to allocate IIO device\n");
208 		return -ENOMEM;
209 	}
210 
211 	adc = iio_priv(indio_dev);
212 	adc->spi = spi;
213 
214 	/* Connect the SPI device and the iio dev */
215 	spi_set_drvdata(spi, indio_dev);
216 
217 	/* Initiate the Industrial I/O device */
218 	indio_dev->dev.parent = &spi->dev;
219 	indio_dev->dev.of_node = spi->dev.of_node;
220 	indio_dev->name = spi_get_device_id(spi)->name;
221 	indio_dev->modes = INDIO_DIRECT_MODE;
222 	indio_dev->info = &adc084s021_info;
223 	indio_dev->channels = adc084s021_channels;
224 	indio_dev->num_channels = ARRAY_SIZE(adc084s021_channels);
225 
226 	/* Create SPI transfer for channel reads */
227 	adc->spi_trans.tx_buf = adc->tx_buf;
228 	adc->spi_trans.rx_buf = adc->rx_buf;
229 	adc->spi_trans.len = 4; /* Trash + single channel */
230 	spi_message_init_with_transfers(&adc->message, &adc->spi_trans, 1);
231 
232 	adc->reg = devm_regulator_get(&spi->dev, "vref");
233 	if (IS_ERR(adc->reg))
234 		return PTR_ERR(adc->reg);
235 
236 	mutex_init(&adc->lock);
237 
238 	/* Setup triggered buffer with pollfunction */
239 	ret = devm_iio_triggered_buffer_setup(&spi->dev, indio_dev, NULL,
240 					    adc084s021_buffer_trigger_handler,
241 					    &adc084s021_buffer_setup_ops);
242 	if (ret) {
243 		dev_err(&spi->dev, "Failed to setup triggered buffer\n");
244 		return ret;
245 	}
246 
247 	return devm_iio_device_register(&spi->dev, indio_dev);
248 }
249 
250 static const struct of_device_id adc084s021_of_match[] = {
251 	{ .compatible = "ti,adc084s021", },
252 	{},
253 };
254 MODULE_DEVICE_TABLE(of, adc084s021_of_match);
255 
256 static const struct spi_device_id adc084s021_id[] = {
257 	{ ADC084S021_DRIVER_NAME, 0},
258 	{}
259 };
260 MODULE_DEVICE_TABLE(spi, adc084s021_id);
261 
262 static struct spi_driver adc084s021_driver = {
263 	.driver = {
264 		.name = ADC084S021_DRIVER_NAME,
265 		.of_match_table = of_match_ptr(adc084s021_of_match),
266 	},
267 	.probe = adc084s021_probe,
268 	.id_table = adc084s021_id,
269 };
270 module_spi_driver(adc084s021_driver);
271 
272 MODULE_AUTHOR("Mårten Lindahl <martenli@axis.com>");
273 MODULE_DESCRIPTION("Texas Instruments ADC084S021");
274 MODULE_LICENSE("GPL v2");
275 MODULE_VERSION("1.0");
276