xref: /linux/drivers/iio/adc/ti-adc161s626.c (revision f3a8b6645dc2e60d11f20c1c23afd964ff4e55ae)
1 /*
2  * ti-adc161s626.c - Texas Instruments ADC161S626 1-channel differential ADC
3  *
4  * ADC Devices Supported:
5  *  adc141s626 - 14-bit ADC
6  *  adc161s626 - 16-bit ADC
7  *
8  * Copyright (C) 2016 Matt Ranostay <mranostay@gmail.com>
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 as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  */
20 
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/err.h>
24 #include <linux/spi/spi.h>
25 #include <linux/iio/iio.h>
26 #include <linux/iio/trigger.h>
27 #include <linux/iio/buffer.h>
28 #include <linux/iio/trigger_consumer.h>
29 #include <linux/iio/triggered_buffer.h>
30 
31 #define TI_ADC_DRV_NAME	"ti-adc161s626"
32 
33 enum {
34 	TI_ADC141S626,
35 	TI_ADC161S626,
36 };
37 
38 static const struct iio_chan_spec ti_adc141s626_channels[] = {
39 	{
40 		.type = IIO_VOLTAGE,
41 		.channel = 0,
42 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
43 		.scan_index = 0,
44 		.scan_type = {
45 			.sign = 's',
46 			.realbits = 14,
47 			.storagebits = 16,
48 		},
49 	},
50 	IIO_CHAN_SOFT_TIMESTAMP(1),
51 };
52 
53 static const struct iio_chan_spec ti_adc161s626_channels[] = {
54 	{
55 		.type = IIO_VOLTAGE,
56 		.channel = 0,
57 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
58 		.scan_index = 0,
59 		.scan_type = {
60 			.sign = 's',
61 			.realbits = 16,
62 			.storagebits = 16,
63 		},
64 	},
65 	IIO_CHAN_SOFT_TIMESTAMP(1),
66 };
67 
68 struct ti_adc_data {
69 	struct iio_dev *indio_dev;
70 	struct spi_device *spi;
71 	u8 read_size;
72 	u8 shift;
73 
74 	u8 buffer[16] ____cacheline_aligned;
75 };
76 
77 static int ti_adc_read_measurement(struct ti_adc_data *data,
78 				   struct iio_chan_spec const *chan, int *val)
79 {
80 	int ret;
81 
82 	switch (data->read_size) {
83 	case 2: {
84 		__be16 buf;
85 
86 		ret = spi_read(data->spi, (void *) &buf, 2);
87 		if (ret)
88 			return ret;
89 
90 		*val = be16_to_cpu(buf);
91 		break;
92 	}
93 	case 3: {
94 		__be32 buf;
95 
96 		ret = spi_read(data->spi, (void *) &buf, 3);
97 		if (ret)
98 			return ret;
99 
100 		*val = be32_to_cpu(buf) >> 8;
101 		break;
102 	}
103 	default:
104 		return -EINVAL;
105 	}
106 
107 	*val = sign_extend32(*val >> data->shift, chan->scan_type.realbits - 1);
108 
109 	return 0;
110 }
111 
112 static irqreturn_t ti_adc_trigger_handler(int irq, void *private)
113 {
114 	struct iio_poll_func *pf = private;
115 	struct iio_dev *indio_dev = pf->indio_dev;
116 	struct ti_adc_data *data = iio_priv(indio_dev);
117 	int ret;
118 
119 	ret = ti_adc_read_measurement(data, &indio_dev->channels[0],
120 				     (int *) &data->buffer);
121 	if (!ret)
122 		iio_push_to_buffers_with_timestamp(indio_dev,
123 					data->buffer,
124 					iio_get_time_ns(indio_dev));
125 
126 	iio_trigger_notify_done(indio_dev->trig);
127 
128 	return IRQ_HANDLED;
129 }
130 
131 static int ti_adc_read_raw(struct iio_dev *indio_dev,
132 			   struct iio_chan_spec const *chan,
133 			   int *val, int *val2, long mask)
134 {
135 	struct ti_adc_data *data = iio_priv(indio_dev);
136 	int ret;
137 
138 	if (mask != IIO_CHAN_INFO_RAW)
139 		return -EINVAL;
140 
141 	ret = iio_device_claim_direct_mode(indio_dev);
142 	if (ret)
143 		return ret;
144 
145 	ret = ti_adc_read_measurement(data, chan, val);
146 	iio_device_release_direct_mode(indio_dev);
147 
148 	if (!ret)
149 		return IIO_VAL_INT;
150 
151 	return 0;
152 }
153 
154 static const struct iio_info ti_adc_info = {
155 	.driver_module = THIS_MODULE,
156 	.read_raw = ti_adc_read_raw,
157 };
158 
159 static int ti_adc_probe(struct spi_device *spi)
160 {
161 	struct iio_dev *indio_dev;
162 	struct ti_adc_data *data;
163 	int ret;
164 
165 	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*data));
166 	if (!indio_dev)
167 		return -ENOMEM;
168 
169 	indio_dev->info = &ti_adc_info;
170 	indio_dev->dev.parent = &spi->dev;
171 	indio_dev->dev.of_node = spi->dev.of_node;
172 	indio_dev->name = TI_ADC_DRV_NAME;
173 	indio_dev->modes = INDIO_DIRECT_MODE;
174 	spi_set_drvdata(spi, indio_dev);
175 
176 	data = iio_priv(indio_dev);
177 	data->spi = spi;
178 
179 	switch (spi_get_device_id(spi)->driver_data) {
180 	case TI_ADC141S626:
181 		indio_dev->channels = ti_adc141s626_channels;
182 		indio_dev->num_channels = ARRAY_SIZE(ti_adc141s626_channels);
183 		data->shift = 0;
184 		data->read_size = 2;
185 		break;
186 	case TI_ADC161S626:
187 		indio_dev->channels = ti_adc161s626_channels;
188 		indio_dev->num_channels = ARRAY_SIZE(ti_adc161s626_channels);
189 		data->shift = 6;
190 		data->read_size = 3;
191 		break;
192 	}
193 
194 	ret = iio_triggered_buffer_setup(indio_dev, NULL,
195 					 ti_adc_trigger_handler, NULL);
196 	if (ret)
197 		return ret;
198 
199 	ret = iio_device_register(indio_dev);
200 	if (ret)
201 		goto error_unreg_buffer;
202 
203 	return 0;
204 
205 error_unreg_buffer:
206 	iio_triggered_buffer_cleanup(indio_dev);
207 
208 	return ret;
209 }
210 
211 static int ti_adc_remove(struct spi_device *spi)
212 {
213 	struct iio_dev *indio_dev = spi_get_drvdata(spi);
214 
215 	iio_device_unregister(indio_dev);
216 	iio_triggered_buffer_cleanup(indio_dev);
217 
218 	return 0;
219 }
220 
221 static const struct of_device_id ti_adc_dt_ids[] = {
222 	{ .compatible = "ti,adc141s626", },
223 	{ .compatible = "ti,adc161s626", },
224 	{}
225 };
226 MODULE_DEVICE_TABLE(of, ti_adc_dt_ids);
227 
228 static const struct spi_device_id ti_adc_id[] = {
229 	{"adc141s626", TI_ADC141S626},
230 	{"adc161s626", TI_ADC161S626},
231 	{},
232 };
233 MODULE_DEVICE_TABLE(spi, ti_adc_id);
234 
235 static struct spi_driver ti_adc_driver = {
236 	.driver = {
237 		.name	= TI_ADC_DRV_NAME,
238 		.of_match_table = of_match_ptr(ti_adc_dt_ids),
239 	},
240 	.probe		= ti_adc_probe,
241 	.remove		= ti_adc_remove,
242 	.id_table	= ti_adc_id,
243 };
244 module_spi_driver(ti_adc_driver);
245 
246 MODULE_AUTHOR("Matt Ranostay <mranostay@gmail.com>");
247 MODULE_DESCRIPTION("Texas Instruments ADC1x1S 1-channel differential ADC");
248 MODULE_LICENSE("GPL");
249