1 // SPDX-License-Identifier: GPL-2.0-only
3 * Analog Devices AD9467 SPI ADC driver
5 * Copyright 2012-2020 Analog Devices Inc.
7 #include <linux/cleanup.h>
8 #include <linux/module.h>
9 #include <linux/mutex.h>
10 #include <linux/device.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/spi/spi.h>
14 #include <linux/err.h>
15 #include <linux/delay.h>
16 #include <linux/gpio/consumer.h>
20 #include <linux/iio/iio.h>
21 #include <linux/iio/sysfs.h>
23 #include <linux/clk.h>
25 #include <linux/iio/adc/adi-axi-adc.h>
28 * ADI High-Speed ADC common spi interface registers
29 * See Application-Note AN-877:
30 * https://www.analog.com/media/en/technical-documentation/application-notes/AN-877.pdf
33 #define AN877_ADC_REG_CHIP_PORT_CONF 0x00
34 #define AN877_ADC_REG_CHIP_ID 0x01
35 #define AN877_ADC_REG_CHIP_GRADE 0x02
36 #define AN877_ADC_REG_CHAN_INDEX 0x05
37 #define AN877_ADC_REG_TRANSFER 0xFF
38 #define AN877_ADC_REG_MODES 0x08
39 #define AN877_ADC_REG_TEST_IO 0x0D
40 #define AN877_ADC_REG_ADC_INPUT 0x0F
41 #define AN877_ADC_REG_OFFSET 0x10
42 #define AN877_ADC_REG_OUTPUT_MODE 0x14
43 #define AN877_ADC_REG_OUTPUT_ADJUST 0x15
44 #define AN877_ADC_REG_OUTPUT_PHASE 0x16
45 #define AN877_ADC_REG_OUTPUT_DELAY 0x17
46 #define AN877_ADC_REG_VREF 0x18
47 #define AN877_ADC_REG_ANALOG_INPUT 0x2C
49 /* AN877_ADC_REG_TEST_IO */
50 #define AN877_ADC_TESTMODE_OFF 0x0
51 #define AN877_ADC_TESTMODE_MIDSCALE_SHORT 0x1
52 #define AN877_ADC_TESTMODE_POS_FULLSCALE 0x2
53 #define AN877_ADC_TESTMODE_NEG_FULLSCALE 0x3
54 #define AN877_ADC_TESTMODE_ALT_CHECKERBOARD 0x4
55 #define AN877_ADC_TESTMODE_PN23_SEQ 0x5
56 #define AN877_ADC_TESTMODE_PN9_SEQ 0x6
57 #define AN877_ADC_TESTMODE_ONE_ZERO_TOGGLE 0x7
58 #define AN877_ADC_TESTMODE_USER 0x8
59 #define AN877_ADC_TESTMODE_BIT_TOGGLE 0x9
60 #define AN877_ADC_TESTMODE_SYNC 0xA
61 #define AN877_ADC_TESTMODE_ONE_BIT_HIGH 0xB
62 #define AN877_ADC_TESTMODE_MIXED_BIT_FREQUENCY 0xC
63 #define AN877_ADC_TESTMODE_RAMP 0xF
65 /* AN877_ADC_REG_TRANSFER */
66 #define AN877_ADC_TRANSFER_SYNC 0x1
68 /* AN877_ADC_REG_OUTPUT_MODE */
69 #define AN877_ADC_OUTPUT_MODE_OFFSET_BINARY 0x0
70 #define AN877_ADC_OUTPUT_MODE_TWOS_COMPLEMENT 0x1
71 #define AN877_ADC_OUTPUT_MODE_GRAY_CODE 0x2
73 /* AN877_ADC_REG_OUTPUT_PHASE */
74 #define AN877_ADC_OUTPUT_EVEN_ODD_MODE_EN 0x20
75 #define AN877_ADC_INVERT_DCO_CLK 0x80
77 /* AN877_ADC_REG_OUTPUT_DELAY */
78 #define AN877_ADC_DCO_DELAY_ENABLE 0x80
81 * Analog Devices AD9265 16-Bit, 125/105/80 MSPS ADC
84 #define CHIPID_AD9265 0x64
85 #define AD9265_DEF_OUTPUT_MODE 0x40
86 #define AD9265_REG_VREF_MASK 0xC0
89 * Analog Devices AD9434 12-Bit, 370/500 MSPS ADC
92 #define CHIPID_AD9434 0x6A
93 #define AD9434_DEF_OUTPUT_MODE 0x00
94 #define AD9434_REG_VREF_MASK 0xC0
97 * Analog Devices AD9467 16-Bit, 200/250 MSPS ADC
100 #define CHIPID_AD9467 0x50
101 #define AD9467_DEF_OUTPUT_MODE 0x08
102 #define AD9467_REG_VREF_MASK 0x0F
110 struct ad9467_chip_info {
111 struct adi_axi_adc_chip_info axi_adc_info;
112 unsigned int default_output_mode;
113 unsigned int vref_mask;
116 #define to_ad9467_chip_info(_info) \
117 container_of(_info, struct ad9467_chip_info, axi_adc_info)
119 struct ad9467_state {
120 struct spi_device *spi;
122 unsigned int output_mode;
123 unsigned int (*scales)[2];
125 struct gpio_desc *pwrdown_gpio;
126 /* ensure consistent state obtained on multiple related accesses */
130 static int ad9467_spi_read(struct spi_device *spi, unsigned int reg)
132 unsigned char tbuf[2], rbuf[1];
135 tbuf[0] = 0x80 | (reg >> 8);
136 tbuf[1] = reg & 0xFF;
138 ret = spi_write_then_read(spi,
139 tbuf, ARRAY_SIZE(tbuf),
140 rbuf, ARRAY_SIZE(rbuf));
148 static int ad9467_spi_write(struct spi_device *spi, unsigned int reg,
151 unsigned char buf[3];
157 return spi_write(spi, buf, ARRAY_SIZE(buf));
160 static int ad9467_reg_access(struct adi_axi_adc_conv *conv, unsigned int reg,
161 unsigned int writeval, unsigned int *readval)
163 struct ad9467_state *st = adi_axi_adc_conv_priv(conv);
164 struct spi_device *spi = st->spi;
167 if (readval == NULL) {
168 guard(mutex)(&st->lock);
169 ret = ad9467_spi_write(spi, reg, writeval);
172 return ad9467_spi_write(spi, AN877_ADC_REG_TRANSFER,
173 AN877_ADC_TRANSFER_SYNC);
176 ret = ad9467_spi_read(spi, reg);
184 static const unsigned int ad9265_scale_table[][2] = {
185 {1250, 0x00}, {1500, 0x40}, {1750, 0x80}, {2000, 0xC0},
188 static const unsigned int ad9434_scale_table[][2] = {
189 {1600, 0x1C}, {1580, 0x1D}, {1550, 0x1E}, {1520, 0x1F}, {1500, 0x00},
190 {1470, 0x01}, {1440, 0x02}, {1420, 0x03}, {1390, 0x04}, {1360, 0x05},
191 {1340, 0x06}, {1310, 0x07}, {1280, 0x08}, {1260, 0x09}, {1230, 0x0A},
192 {1200, 0x0B}, {1180, 0x0C},
195 static const unsigned int ad9467_scale_table[][2] = {
196 {2000, 0}, {2100, 6}, {2200, 7},
197 {2300, 8}, {2400, 9}, {2500, 10},
200 static void __ad9467_get_scale(struct adi_axi_adc_conv *conv, int index,
201 unsigned int *val, unsigned int *val2)
203 const struct adi_axi_adc_chip_info *info = conv->chip_info;
204 const struct iio_chan_spec *chan = &info->channels[0];
207 tmp = (info->scale_table[index][0] * 1000000ULL) >>
208 chan->scan_type.realbits;
209 *val = tmp / 1000000;
210 *val2 = tmp % 1000000;
213 #define AD9467_CHAN(_chan, _si, _bits, _sign) \
215 .type = IIO_VOLTAGE, \
218 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
219 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
220 .info_mask_shared_by_type_available = BIT(IIO_CHAN_INFO_SCALE), \
229 static const struct iio_chan_spec ad9434_channels[] = {
230 AD9467_CHAN(0, 0, 12, 'S'),
233 static const struct iio_chan_spec ad9467_channels[] = {
234 AD9467_CHAN(0, 0, 16, 'S'),
237 static const struct ad9467_chip_info ad9467_chip_tbl[] = {
241 .max_rate = 125000000UL,
242 .scale_table = ad9265_scale_table,
243 .num_scales = ARRAY_SIZE(ad9265_scale_table),
244 .channels = ad9467_channels,
245 .num_channels = ARRAY_SIZE(ad9467_channels),
247 .default_output_mode = AD9265_DEF_OUTPUT_MODE,
248 .vref_mask = AD9265_REG_VREF_MASK,
253 .max_rate = 500000000UL,
254 .scale_table = ad9434_scale_table,
255 .num_scales = ARRAY_SIZE(ad9434_scale_table),
256 .channels = ad9434_channels,
257 .num_channels = ARRAY_SIZE(ad9434_channels),
259 .default_output_mode = AD9434_DEF_OUTPUT_MODE,
260 .vref_mask = AD9434_REG_VREF_MASK,
265 .max_rate = 250000000UL,
266 .scale_table = ad9467_scale_table,
267 .num_scales = ARRAY_SIZE(ad9467_scale_table),
268 .channels = ad9467_channels,
269 .num_channels = ARRAY_SIZE(ad9467_channels),
271 .default_output_mode = AD9467_DEF_OUTPUT_MODE,
272 .vref_mask = AD9467_REG_VREF_MASK,
276 static int ad9467_get_scale(struct adi_axi_adc_conv *conv, int *val, int *val2)
278 const struct adi_axi_adc_chip_info *info = conv->chip_info;
279 const struct ad9467_chip_info *info1 = to_ad9467_chip_info(info);
280 struct ad9467_state *st = adi_axi_adc_conv_priv(conv);
281 unsigned int i, vref_val;
284 ret = ad9467_spi_read(st->spi, AN877_ADC_REG_VREF);
288 vref_val = ret & info1->vref_mask;
290 for (i = 0; i < info->num_scales; i++) {
291 if (vref_val == info->scale_table[i][1])
295 if (i == info->num_scales)
298 __ad9467_get_scale(conv, i, val, val2);
300 return IIO_VAL_INT_PLUS_MICRO;
303 static int ad9467_set_scale(struct adi_axi_adc_conv *conv, int val, int val2)
305 const struct adi_axi_adc_chip_info *info = conv->chip_info;
306 struct ad9467_state *st = adi_axi_adc_conv_priv(conv);
307 unsigned int scale_val[2];
314 for (i = 0; i < info->num_scales; i++) {
315 __ad9467_get_scale(conv, i, &scale_val[0], &scale_val[1]);
316 if (scale_val[0] != val || scale_val[1] != val2)
319 guard(mutex)(&st->lock);
320 ret = ad9467_spi_write(st->spi, AN877_ADC_REG_VREF,
321 info->scale_table[i][1]);
325 return ad9467_spi_write(st->spi, AN877_ADC_REG_TRANSFER,
326 AN877_ADC_TRANSFER_SYNC);
332 static int ad9467_read_raw(struct adi_axi_adc_conv *conv,
333 struct iio_chan_spec const *chan,
334 int *val, int *val2, long m)
336 struct ad9467_state *st = adi_axi_adc_conv_priv(conv);
339 case IIO_CHAN_INFO_SCALE:
340 return ad9467_get_scale(conv, val, val2);
341 case IIO_CHAN_INFO_SAMP_FREQ:
342 *val = clk_get_rate(st->clk);
350 static int ad9467_write_raw(struct adi_axi_adc_conv *conv,
351 struct iio_chan_spec const *chan,
352 int val, int val2, long mask)
354 const struct adi_axi_adc_chip_info *info = conv->chip_info;
355 struct ad9467_state *st = adi_axi_adc_conv_priv(conv);
359 case IIO_CHAN_INFO_SCALE:
360 return ad9467_set_scale(conv, val, val2);
361 case IIO_CHAN_INFO_SAMP_FREQ:
362 r_clk = clk_round_rate(st->clk, val);
363 if (r_clk < 0 || r_clk > info->max_rate) {
364 dev_warn(&st->spi->dev,
365 "Error setting ADC sample rate %ld", r_clk);
369 return clk_set_rate(st->clk, r_clk);
375 static int ad9467_read_avail(struct adi_axi_adc_conv *conv,
376 struct iio_chan_spec const *chan,
377 const int **vals, int *type, int *length,
380 const struct adi_axi_adc_chip_info *info = conv->chip_info;
381 struct ad9467_state *st = adi_axi_adc_conv_priv(conv);
384 case IIO_CHAN_INFO_SCALE:
385 *vals = (const int *)st->scales;
386 *type = IIO_VAL_INT_PLUS_MICRO;
387 /* Values are stored in a 2D matrix */
388 *length = info->num_scales * 2;
389 return IIO_AVAIL_LIST;
395 static int ad9467_outputmode_set(struct spi_device *spi, unsigned int mode)
399 ret = ad9467_spi_write(spi, AN877_ADC_REG_OUTPUT_MODE, mode);
403 return ad9467_spi_write(spi, AN877_ADC_REG_TRANSFER,
404 AN877_ADC_TRANSFER_SYNC);
407 static int ad9467_scale_fill(struct adi_axi_adc_conv *conv)
409 const struct adi_axi_adc_chip_info *info = conv->chip_info;
410 struct ad9467_state *st = adi_axi_adc_conv_priv(conv);
411 unsigned int i, val1, val2;
413 st->scales = devm_kmalloc_array(&st->spi->dev, info->num_scales,
414 sizeof(*st->scales), GFP_KERNEL);
418 for (i = 0; i < info->num_scales; i++) {
419 __ad9467_get_scale(conv, i, &val1, &val2);
420 st->scales[i][0] = val1;
421 st->scales[i][1] = val2;
427 static int ad9467_preenable_setup(struct adi_axi_adc_conv *conv)
429 struct ad9467_state *st = adi_axi_adc_conv_priv(conv);
431 return ad9467_outputmode_set(st->spi, st->output_mode);
434 static int ad9467_reset(struct device *dev)
436 struct gpio_desc *gpio;
438 gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
439 if (IS_ERR_OR_NULL(gpio))
440 return PTR_ERR_OR_ZERO(gpio);
443 gpiod_set_value_cansleep(gpio, 0);
444 fsleep(10 * USEC_PER_MSEC);
449 static int ad9467_probe(struct spi_device *spi)
451 const struct ad9467_chip_info *info;
452 struct adi_axi_adc_conv *conv;
453 struct ad9467_state *st;
457 info = of_device_get_match_data(&spi->dev);
459 info = (void *)spi_get_device_id(spi)->driver_data;
463 conv = devm_adi_axi_adc_conv_register(&spi->dev, sizeof(*st));
465 return PTR_ERR(conv);
467 st = adi_axi_adc_conv_priv(conv);
470 st->clk = devm_clk_get_enabled(&spi->dev, "adc-clk");
472 return PTR_ERR(st->clk);
474 st->pwrdown_gpio = devm_gpiod_get_optional(&spi->dev, "powerdown",
476 if (IS_ERR(st->pwrdown_gpio))
477 return PTR_ERR(st->pwrdown_gpio);
479 ret = ad9467_reset(&spi->dev);
483 conv->chip_info = &info->axi_adc_info;
485 ret = ad9467_scale_fill(conv);
489 id = ad9467_spi_read(spi, AN877_ADC_REG_CHIP_ID);
490 if (id != conv->chip_info->id) {
491 dev_err(&spi->dev, "Mismatch CHIP_ID, got 0x%X, expected 0x%X\n",
492 id, conv->chip_info->id);
496 conv->reg_access = ad9467_reg_access;
497 conv->write_raw = ad9467_write_raw;
498 conv->read_raw = ad9467_read_raw;
499 conv->read_avail = ad9467_read_avail;
500 conv->preenable_setup = ad9467_preenable_setup;
502 st->output_mode = info->default_output_mode |
503 AN877_ADC_OUTPUT_MODE_TWOS_COMPLEMENT;
508 static const struct of_device_id ad9467_of_match[] = {
509 { .compatible = "adi,ad9265", .data = &ad9467_chip_tbl[ID_AD9265], },
510 { .compatible = "adi,ad9434", .data = &ad9467_chip_tbl[ID_AD9434], },
511 { .compatible = "adi,ad9467", .data = &ad9467_chip_tbl[ID_AD9467], },
514 MODULE_DEVICE_TABLE(of, ad9467_of_match);
516 static const struct spi_device_id ad9467_ids[] = {
517 { "ad9265", (kernel_ulong_t)&ad9467_chip_tbl[ID_AD9265] },
518 { "ad9434", (kernel_ulong_t)&ad9467_chip_tbl[ID_AD9434] },
519 { "ad9467", (kernel_ulong_t)&ad9467_chip_tbl[ID_AD9467] },
522 MODULE_DEVICE_TABLE(spi, ad9467_ids);
524 static struct spi_driver ad9467_driver = {
527 .of_match_table = ad9467_of_match,
529 .probe = ad9467_probe,
530 .id_table = ad9467_ids,
532 module_spi_driver(ad9467_driver);
534 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
535 MODULE_DESCRIPTION("Analog Devices AD9467 ADC driver");
536 MODULE_LICENSE("GPL v2");
537 MODULE_IMPORT_NS(IIO_ADI_AXI);