1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * AD7606 SPI ADC driver
4 *
5 * Copyright 2011 Analog Devices Inc.
6 */
7
8 #include <linux/err.h>
9 #include <linux/module.h>
10 #include <linux/spi/spi.h>
11 #include <linux/types.h>
12
13 #include <linux/iio/iio.h>
14 #include "ad7606.h"
15
16 #define MAX_SPI_FREQ_HZ 23500000 /* VDRIVE above 4.75 V */
17
18 static const struct iio_chan_spec ad7616_sw_channels[] = {
19 IIO_CHAN_SOFT_TIMESTAMP(16),
20 AD7616_CHANNEL(0),
21 AD7616_CHANNEL(1),
22 AD7616_CHANNEL(2),
23 AD7616_CHANNEL(3),
24 AD7616_CHANNEL(4),
25 AD7616_CHANNEL(5),
26 AD7616_CHANNEL(6),
27 AD7616_CHANNEL(7),
28 AD7616_CHANNEL(8),
29 AD7616_CHANNEL(9),
30 AD7616_CHANNEL(10),
31 AD7616_CHANNEL(11),
32 AD7616_CHANNEL(12),
33 AD7616_CHANNEL(13),
34 AD7616_CHANNEL(14),
35 AD7616_CHANNEL(15),
36 };
37
38 static const struct iio_chan_spec ad7606b_sw_channels[] = {
39 IIO_CHAN_SOFT_TIMESTAMP(8),
40 AD7606_SW_CHANNEL(0, 16),
41 AD7606_SW_CHANNEL(1, 16),
42 AD7606_SW_CHANNEL(2, 16),
43 AD7606_SW_CHANNEL(3, 16),
44 AD7606_SW_CHANNEL(4, 16),
45 AD7606_SW_CHANNEL(5, 16),
46 AD7606_SW_CHANNEL(6, 16),
47 AD7606_SW_CHANNEL(7, 16),
48 };
49
50 static const struct iio_chan_spec ad7606c_18_sw_channels[] = {
51 IIO_CHAN_SOFT_TIMESTAMP(8),
52 AD7606_SW_CHANNEL(0, 18),
53 AD7606_SW_CHANNEL(1, 18),
54 AD7606_SW_CHANNEL(2, 18),
55 AD7606_SW_CHANNEL(3, 18),
56 AD7606_SW_CHANNEL(4, 18),
57 AD7606_SW_CHANNEL(5, 18),
58 AD7606_SW_CHANNEL(6, 18),
59 AD7606_SW_CHANNEL(7, 18),
60 };
61
ad7616_spi_rd_wr_cmd(int addr,char isWriteOp)62 static u16 ad7616_spi_rd_wr_cmd(int addr, char isWriteOp)
63 {
64 /*
65 * The address of register consist of one w/r bit
66 * 6 bits of address followed by one reserved bit.
67 */
68 return ((addr & 0x7F) << 1) | ((isWriteOp & 0x1) << 7);
69 }
70
ad7606B_spi_rd_wr_cmd(int addr,char is_write_op)71 static u16 ad7606B_spi_rd_wr_cmd(int addr, char is_write_op)
72 {
73 /*
74 * The address of register consists of one bit which
75 * specifies a read command placed in bit 6, followed by
76 * 6 bits of address.
77 */
78 return (addr & 0x3F) | (((~is_write_op) & 0x1) << 6);
79 }
80
ad7606_spi_read_block(struct device * dev,int count,void * buf)81 static int ad7606_spi_read_block(struct device *dev,
82 int count, void *buf)
83 {
84 struct spi_device *spi = to_spi_device(dev);
85 int i, ret;
86 unsigned short *data = buf;
87 __be16 *bdata = buf;
88
89 ret = spi_read(spi, buf, count * 2);
90 if (ret < 0) {
91 dev_err(&spi->dev, "SPI read error\n");
92 return ret;
93 }
94
95 for (i = 0; i < count; i++)
96 data[i] = be16_to_cpu(bdata[i]);
97
98 return 0;
99 }
100
ad7606_spi_read_block14to16(struct device * dev,int count,void * buf)101 static int ad7606_spi_read_block14to16(struct device *dev,
102 int count, void *buf)
103 {
104 struct spi_device *spi = to_spi_device(dev);
105 struct spi_transfer xfer = {
106 .bits_per_word = 14,
107 .len = count * sizeof(u16),
108 .rx_buf = buf,
109 };
110
111 return spi_sync_transfer(spi, &xfer, 1);
112 }
113
ad7606_spi_read_block18to32(struct device * dev,int count,void * buf)114 static int ad7606_spi_read_block18to32(struct device *dev,
115 int count, void *buf)
116 {
117 struct spi_device *spi = to_spi_device(dev);
118 struct spi_transfer xfer = {
119 .bits_per_word = 18,
120 .len = count * sizeof(u32),
121 .rx_buf = buf,
122 };
123
124 return spi_sync_transfer(spi, &xfer, 1);
125 }
126
ad7606_spi_reg_read(struct ad7606_state * st,unsigned int addr)127 static int ad7606_spi_reg_read(struct ad7606_state *st, unsigned int addr)
128 {
129 struct spi_device *spi = to_spi_device(st->dev);
130 struct spi_transfer t[] = {
131 {
132 .tx_buf = &st->d16[0],
133 .len = 2,
134 .cs_change = 1,
135 }, {
136 .rx_buf = &st->d16[1],
137 .len = 2,
138 },
139 };
140 int ret;
141
142 st->d16[0] = cpu_to_be16(st->bops->rd_wr_cmd(addr, 0) << 8);
143
144 ret = spi_sync_transfer(spi, t, ARRAY_SIZE(t));
145 if (ret < 0)
146 return ret;
147
148 return be16_to_cpu(st->d16[1]);
149 }
150
ad7606_spi_reg_write(struct ad7606_state * st,unsigned int addr,unsigned int val)151 static int ad7606_spi_reg_write(struct ad7606_state *st,
152 unsigned int addr,
153 unsigned int val)
154 {
155 struct spi_device *spi = to_spi_device(st->dev);
156
157 st->d16[0] = cpu_to_be16((st->bops->rd_wr_cmd(addr, 1) << 8) |
158 (val & 0x1FF));
159
160 return spi_write(spi, &st->d16[0], sizeof(st->d16[0]));
161 }
162
ad7616_sw_mode_config(struct iio_dev * indio_dev)163 static int ad7616_sw_mode_config(struct iio_dev *indio_dev)
164 {
165 /*
166 * Scale can be configured individually for each channel
167 * in software mode.
168 */
169 indio_dev->channels = ad7616_sw_channels;
170
171 return 0;
172 }
173
ad7606B_sw_mode_config(struct iio_dev * indio_dev)174 static int ad7606B_sw_mode_config(struct iio_dev *indio_dev)
175 {
176 struct ad7606_state *st = iio_priv(indio_dev);
177
178 /* Configure device spi to output on a single channel */
179 st->bops->reg_write(st,
180 AD7606_CONFIGURATION_REGISTER,
181 AD7606_SINGLE_DOUT);
182
183 /*
184 * Scale can be configured individually for each channel
185 * in software mode.
186 */
187 indio_dev->channels = ad7606b_sw_channels;
188
189 return 0;
190 }
191
ad7606c_18_sw_mode_config(struct iio_dev * indio_dev)192 static int ad7606c_18_sw_mode_config(struct iio_dev *indio_dev)
193 {
194 int ret;
195
196 ret = ad7606B_sw_mode_config(indio_dev);
197 if (ret)
198 return ret;
199
200 indio_dev->channels = ad7606c_18_sw_channels;
201
202 return 0;
203 }
204
205 static const struct ad7606_bus_ops ad7606_spi_bops = {
206 .read_block = ad7606_spi_read_block,
207 };
208
209 static const struct ad7606_bus_ops ad7607_spi_bops = {
210 .read_block = ad7606_spi_read_block14to16,
211 };
212
213 static const struct ad7606_bus_ops ad7608_spi_bops = {
214 .read_block = ad7606_spi_read_block18to32,
215 };
216
217 static const struct ad7606_bus_ops ad7616_spi_bops = {
218 .read_block = ad7606_spi_read_block,
219 .reg_read = ad7606_spi_reg_read,
220 .reg_write = ad7606_spi_reg_write,
221 .rd_wr_cmd = ad7616_spi_rd_wr_cmd,
222 .sw_mode_config = ad7616_sw_mode_config,
223 };
224
225 static const struct ad7606_bus_ops ad7606b_spi_bops = {
226 .read_block = ad7606_spi_read_block,
227 .reg_read = ad7606_spi_reg_read,
228 .reg_write = ad7606_spi_reg_write,
229 .rd_wr_cmd = ad7606B_spi_rd_wr_cmd,
230 .sw_mode_config = ad7606B_sw_mode_config,
231 };
232
233 static const struct ad7606_bus_ops ad7606c_18_spi_bops = {
234 .read_block = ad7606_spi_read_block18to32,
235 .reg_read = ad7606_spi_reg_read,
236 .reg_write = ad7606_spi_reg_write,
237 .rd_wr_cmd = ad7606B_spi_rd_wr_cmd,
238 .sw_mode_config = ad7606c_18_sw_mode_config,
239 };
240
241 static const struct ad7606_bus_info ad7605_4_bus_info = {
242 .chip_info = &ad7605_4_info,
243 .bops = &ad7606_spi_bops,
244 };
245
246 static const struct ad7606_bus_info ad7606_8_bus_info = {
247 .chip_info = &ad7606_8_info,
248 .bops = &ad7606_spi_bops,
249 };
250
251 static const struct ad7606_bus_info ad7606_6_bus_info = {
252 .chip_info = &ad7606_6_info,
253 .bops = &ad7606_spi_bops,
254 };
255
256 static const struct ad7606_bus_info ad7606_4_bus_info = {
257 .chip_info = &ad7606_4_info,
258 .bops = &ad7606_spi_bops,
259 };
260
261 static const struct ad7606_bus_info ad7606b_bus_info = {
262 .chip_info = &ad7606b_info,
263 .bops = &ad7606b_spi_bops,
264 };
265
266 static const struct ad7606_bus_info ad7606c_16_bus_info = {
267 .chip_info = &ad7606c_16_info,
268 .bops = &ad7606b_spi_bops,
269 };
270
271 static const struct ad7606_bus_info ad7606c_18_bus_info = {
272 .chip_info = &ad7606c_18_info,
273 .bops = &ad7606c_18_spi_bops,
274 };
275
276 static const struct ad7606_bus_info ad7607_bus_info = {
277 .chip_info = &ad7607_info,
278 .bops = &ad7607_spi_bops,
279 };
280
281 static const struct ad7606_bus_info ad7608_bus_info = {
282 .chip_info = &ad7608_info,
283 .bops = &ad7608_spi_bops,
284 };
285
286 static const struct ad7606_bus_info ad7609_bus_info = {
287 .chip_info = &ad7609_info,
288 .bops = &ad7608_spi_bops,
289 };
290
291 static const struct ad7606_bus_info ad7616_bus_info = {
292 .chip_info = &ad7616_info,
293 .bops = &ad7616_spi_bops,
294 };
295
ad7606_spi_probe(struct spi_device * spi)296 static int ad7606_spi_probe(struct spi_device *spi)
297 {
298 const struct ad7606_bus_info *bus_info = spi_get_device_match_data(spi);
299
300 return ad7606_probe(&spi->dev, spi->irq, NULL,
301 bus_info->chip_info, bus_info->bops);
302 }
303
304 static const struct spi_device_id ad7606_id_table[] = {
305 { "ad7605-4", (kernel_ulong_t)&ad7605_4_bus_info },
306 { "ad7606-4", (kernel_ulong_t)&ad7606_4_bus_info },
307 { "ad7606-6", (kernel_ulong_t)&ad7606_6_bus_info },
308 { "ad7606-8", (kernel_ulong_t)&ad7606_8_bus_info },
309 { "ad7606b", (kernel_ulong_t)&ad7606b_bus_info },
310 { "ad7606c-16", (kernel_ulong_t)&ad7606c_16_bus_info },
311 { "ad7606c-18", (kernel_ulong_t)&ad7606c_18_bus_info },
312 { "ad7607", (kernel_ulong_t)&ad7607_bus_info },
313 { "ad7608", (kernel_ulong_t)&ad7608_bus_info },
314 { "ad7609", (kernel_ulong_t)&ad7609_bus_info },
315 { "ad7616", (kernel_ulong_t)&ad7616_bus_info },
316 { }
317 };
318 MODULE_DEVICE_TABLE(spi, ad7606_id_table);
319
320 static const struct of_device_id ad7606_of_match[] = {
321 { .compatible = "adi,ad7605-4", .data = &ad7605_4_bus_info },
322 { .compatible = "adi,ad7606-4", .data = &ad7606_4_bus_info },
323 { .compatible = "adi,ad7606-6", .data = &ad7606_6_bus_info },
324 { .compatible = "adi,ad7606-8", .data = &ad7606_8_bus_info },
325 { .compatible = "adi,ad7606b", .data = &ad7606b_bus_info },
326 { .compatible = "adi,ad7606c-16", .data = &ad7606c_16_bus_info },
327 { .compatible = "adi,ad7606c-18", .data = &ad7606c_18_bus_info },
328 { .compatible = "adi,ad7607", .data = &ad7607_bus_info },
329 { .compatible = "adi,ad7608", .data = &ad7608_bus_info },
330 { .compatible = "adi,ad7609", .data = &ad7609_bus_info },
331 { .compatible = "adi,ad7616", .data = &ad7616_bus_info },
332 { }
333 };
334 MODULE_DEVICE_TABLE(of, ad7606_of_match);
335
336 static struct spi_driver ad7606_driver = {
337 .driver = {
338 .name = "ad7606",
339 .of_match_table = ad7606_of_match,
340 .pm = AD7606_PM_OPS,
341 },
342 .probe = ad7606_spi_probe,
343 .id_table = ad7606_id_table,
344 };
345 module_spi_driver(ad7606_driver);
346
347 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
348 MODULE_DESCRIPTION("Analog Devices AD7606 ADC");
349 MODULE_LICENSE("GPL v2");
350 MODULE_IMPORT_NS("IIO_AD7606");
351