1 // SPDX-License-Identifier: GPL-2.0-only
3 * Analog Devices Generic AXI ADC IP core
4 * Link: https://wiki.analog.com/resources/fpga/docs/axi_adc_ip
6 * Copyright 2012-2020 Analog Devices Inc.
9 #include <linux/bitfield.h>
10 #include <linux/cleanup.h>
11 #include <linux/clk.h>
12 #include <linux/err.h>
14 #include <linux/delay.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
18 #include <linux/platform_device.h>
19 #include <linux/property.h>
20 #include <linux/regmap.h>
21 #include <linux/slab.h>
23 #include <linux/fpga/adi-axi-common.h>
25 #include <linux/iio/backend.h>
26 #include <linux/iio/buffer-dmaengine.h>
27 #include <linux/iio/buffer.h>
28 #include <linux/iio/iio.h>
31 * Register definitions:
32 * https://wiki.analog.com/resources/fpga/docs/axi_adc_ip#register_map
37 #define ADI_AXI_REG_RSTN 0x0040
38 #define ADI_AXI_REG_RSTN_CE_N BIT(2)
39 #define ADI_AXI_REG_RSTN_MMCM_RSTN BIT(1)
40 #define ADI_AXI_REG_RSTN_RSTN BIT(0)
42 #define ADI_AXI_ADC_REG_CTRL 0x0044
43 #define ADI_AXI_ADC_CTRL_DDR_EDGESEL_MASK BIT(1)
45 #define ADI_AXI_ADC_REG_DRP_STATUS 0x0074
46 #define ADI_AXI_ADC_DRP_LOCKED BIT(17)
48 /* ADC Channel controls */
50 #define ADI_AXI_REG_CHAN_CTRL(c) (0x0400 + (c) * 0x40)
51 #define ADI_AXI_REG_CHAN_CTRL_LB_OWR BIT(11)
52 #define ADI_AXI_REG_CHAN_CTRL_PN_SEL_OWR BIT(10)
53 #define ADI_AXI_REG_CHAN_CTRL_IQCOR_EN BIT(9)
54 #define ADI_AXI_REG_CHAN_CTRL_DCFILT_EN BIT(8)
55 #define ADI_AXI_REG_CHAN_CTRL_FMT_MASK GENMASK(6, 4)
56 #define ADI_AXI_REG_CHAN_CTRL_FMT_SIGNEXT BIT(6)
57 #define ADI_AXI_REG_CHAN_CTRL_FMT_TYPE BIT(5)
58 #define ADI_AXI_REG_CHAN_CTRL_FMT_EN BIT(4)
59 #define ADI_AXI_REG_CHAN_CTRL_PN_TYPE_OWR BIT(1)
60 #define ADI_AXI_REG_CHAN_CTRL_ENABLE BIT(0)
62 #define ADI_AXI_ADC_REG_CHAN_STATUS(c) (0x0404 + (c) * 0x40)
63 #define ADI_AXI_ADC_CHAN_STAT_PN_MASK GENMASK(2, 1)
65 #define ADI_AXI_ADC_CHAN_STAT_PN_OOS BIT(1)
66 /* spurious out of sync */
67 #define ADI_AXI_ADC_CHAN_STAT_PN_ERR BIT(2)
69 #define ADI_AXI_ADC_REG_CHAN_CTRL_3(c) (0x0418 + (c) * 0x40)
70 #define ADI_AXI_ADC_CHAN_PN_SEL_MASK GENMASK(19, 16)
73 #define ADI_AXI_ADC_REG_DELAY(l) (0x0800 + (l) * 0x4)
74 #define AXI_ADC_DELAY_CTRL_MASK GENMASK(4, 0)
76 #define ADI_AXI_ADC_MAX_IO_NUM_LANES 15
78 #define ADI_AXI_REG_CHAN_CTRL_DEFAULTS \
79 (ADI_AXI_REG_CHAN_CTRL_FMT_SIGNEXT | \
80 ADI_AXI_REG_CHAN_CTRL_FMT_EN | \
81 ADI_AXI_REG_CHAN_CTRL_ENABLE)
83 struct adi_axi_adc_state
{
84 struct regmap
*regmap
;
86 /* lock to protect multiple accesses to the device registers */
90 static int axi_adc_enable(struct iio_backend
*back
)
92 struct adi_axi_adc_state
*st
= iio_backend_get_priv(back
);
96 guard(mutex
)(&st
->lock
);
97 ret
= regmap_set_bits(st
->regmap
, ADI_AXI_REG_RSTN
,
98 ADI_AXI_REG_RSTN_MMCM_RSTN
);
103 * Make sure the DRP (Dynamic Reconfiguration Port) is locked. Not all
104 * designs really use it but if they don't we still get the lock bit
105 * set. So let's do it all the time so the code is generic.
107 ret
= regmap_read_poll_timeout(st
->regmap
, ADI_AXI_ADC_REG_DRP_STATUS
,
108 __val
, __val
& ADI_AXI_ADC_DRP_LOCKED
,
113 return regmap_set_bits(st
->regmap
, ADI_AXI_REG_RSTN
,
114 ADI_AXI_REG_RSTN_RSTN
| ADI_AXI_REG_RSTN_MMCM_RSTN
);
117 static void axi_adc_disable(struct iio_backend
*back
)
119 struct adi_axi_adc_state
*st
= iio_backend_get_priv(back
);
121 guard(mutex
)(&st
->lock
);
122 regmap_write(st
->regmap
, ADI_AXI_REG_RSTN
, 0);
125 static int axi_adc_data_format_set(struct iio_backend
*back
, unsigned int chan
,
126 const struct iio_backend_data_fmt
*data
)
128 struct adi_axi_adc_state
*st
= iio_backend_get_priv(back
);
132 return regmap_clear_bits(st
->regmap
,
133 ADI_AXI_REG_CHAN_CTRL(chan
),
134 ADI_AXI_REG_CHAN_CTRL_FMT_EN
);
136 val
= FIELD_PREP(ADI_AXI_REG_CHAN_CTRL_FMT_EN
, true);
137 if (data
->sign_extend
)
138 val
|= FIELD_PREP(ADI_AXI_REG_CHAN_CTRL_FMT_SIGNEXT
, true);
139 if (data
->type
== IIO_BACKEND_OFFSET_BINARY
)
140 val
|= FIELD_PREP(ADI_AXI_REG_CHAN_CTRL_FMT_TYPE
, true);
142 return regmap_update_bits(st
->regmap
, ADI_AXI_REG_CHAN_CTRL(chan
),
143 ADI_AXI_REG_CHAN_CTRL_FMT_MASK
, val
);
146 static int axi_adc_data_sample_trigger(struct iio_backend
*back
,
147 enum iio_backend_sample_trigger trigger
)
149 struct adi_axi_adc_state
*st
= iio_backend_get_priv(back
);
152 case IIO_BACKEND_SAMPLE_TRIGGER_EDGE_RISING
:
153 return regmap_clear_bits(st
->regmap
, ADI_AXI_ADC_REG_CTRL
,
154 ADI_AXI_ADC_CTRL_DDR_EDGESEL_MASK
);
155 case IIO_BACKEND_SAMPLE_TRIGGER_EDGE_FALLING
:
156 return regmap_set_bits(st
->regmap
, ADI_AXI_ADC_REG_CTRL
,
157 ADI_AXI_ADC_CTRL_DDR_EDGESEL_MASK
);
163 static int axi_adc_iodelays_set(struct iio_backend
*back
, unsigned int lane
,
166 struct adi_axi_adc_state
*st
= iio_backend_get_priv(back
);
170 if (tap
> FIELD_MAX(AXI_ADC_DELAY_CTRL_MASK
))
172 if (lane
> ADI_AXI_ADC_MAX_IO_NUM_LANES
)
175 guard(mutex
)(&st
->lock
);
176 ret
= regmap_write(st
->regmap
, ADI_AXI_ADC_REG_DELAY(lane
), tap
);
180 * If readback is ~0, that means there are issues with the
183 ret
= regmap_read(st
->regmap
, ADI_AXI_ADC_REG_DELAY(lane
), &val
);
192 static int axi_adc_test_pattern_set(struct iio_backend
*back
,
194 enum iio_backend_test_pattern pattern
)
196 struct adi_axi_adc_state
*st
= iio_backend_get_priv(back
);
199 case IIO_BACKEND_NO_TEST_PATTERN
:
202 case IIO_BACKEND_ADI_PRBS_9A
:
203 return regmap_update_bits(st
->regmap
, ADI_AXI_ADC_REG_CHAN_CTRL_3(chan
),
204 ADI_AXI_ADC_CHAN_PN_SEL_MASK
,
205 FIELD_PREP(ADI_AXI_ADC_CHAN_PN_SEL_MASK
, 0));
206 case IIO_BACKEND_ADI_PRBS_23A
:
207 return regmap_update_bits(st
->regmap
, ADI_AXI_ADC_REG_CHAN_CTRL_3(chan
),
208 ADI_AXI_ADC_CHAN_PN_SEL_MASK
,
209 FIELD_PREP(ADI_AXI_ADC_CHAN_PN_SEL_MASK
, 1));
215 static int axi_adc_read_chan_status(struct adi_axi_adc_state
*st
, unsigned int chan
,
216 unsigned int *status
)
220 guard(mutex
)(&st
->lock
);
221 /* reset test bits by setting them */
222 ret
= regmap_write(st
->regmap
, ADI_AXI_ADC_REG_CHAN_STATUS(chan
),
223 ADI_AXI_ADC_CHAN_STAT_PN_MASK
);
227 /* let's give enough time to validate or erroring the incoming pattern */
230 return regmap_read(st
->regmap
, ADI_AXI_ADC_REG_CHAN_STATUS(chan
),
234 static int axi_adc_chan_status(struct iio_backend
*back
, unsigned int chan
,
237 struct adi_axi_adc_state
*st
= iio_backend_get_priv(back
);
241 ret
= axi_adc_read_chan_status(st
, chan
, &val
);
245 if (ADI_AXI_ADC_CHAN_STAT_PN_MASK
& val
)
253 static int axi_adc_debugfs_print_chan_status(struct iio_backend
*back
,
254 unsigned int chan
, char *buf
,
257 struct adi_axi_adc_state
*st
= iio_backend_get_priv(back
);
261 ret
= axi_adc_read_chan_status(st
, chan
, &val
);
266 * PN_ERR is cleared in case out of sync is set. Hence, no point in
267 * checking both bits.
269 if (val
& ADI_AXI_ADC_CHAN_STAT_PN_OOS
)
270 return scnprintf(buf
, len
, "CH%u: Out of Sync.\n", chan
);
271 if (val
& ADI_AXI_ADC_CHAN_STAT_PN_ERR
)
272 return scnprintf(buf
, len
, "CH%u: Spurious Out of Sync.\n", chan
);
274 return scnprintf(buf
, len
, "CH%u: OK.\n", chan
);
277 static int axi_adc_chan_enable(struct iio_backend
*back
, unsigned int chan
)
279 struct adi_axi_adc_state
*st
= iio_backend_get_priv(back
);
281 return regmap_set_bits(st
->regmap
, ADI_AXI_REG_CHAN_CTRL(chan
),
282 ADI_AXI_REG_CHAN_CTRL_ENABLE
);
285 static int axi_adc_chan_disable(struct iio_backend
*back
, unsigned int chan
)
287 struct adi_axi_adc_state
*st
= iio_backend_get_priv(back
);
289 return regmap_clear_bits(st
->regmap
, ADI_AXI_REG_CHAN_CTRL(chan
),
290 ADI_AXI_REG_CHAN_CTRL_ENABLE
);
293 static struct iio_buffer
*axi_adc_request_buffer(struct iio_backend
*back
,
294 struct iio_dev
*indio_dev
)
296 struct adi_axi_adc_state
*st
= iio_backend_get_priv(back
);
297 const char *dma_name
;
299 if (device_property_read_string(st
->dev
, "dma-names", &dma_name
))
302 return iio_dmaengine_buffer_setup(st
->dev
, indio_dev
, dma_name
);
305 static void axi_adc_free_buffer(struct iio_backend
*back
,
306 struct iio_buffer
*buffer
)
308 iio_dmaengine_buffer_free(buffer
);
311 static int axi_adc_reg_access(struct iio_backend
*back
, unsigned int reg
,
312 unsigned int writeval
, unsigned int *readval
)
314 struct adi_axi_adc_state
*st
= iio_backend_get_priv(back
);
317 return regmap_read(st
->regmap
, reg
, readval
);
319 return regmap_write(st
->regmap
, reg
, writeval
);
322 static const struct regmap_config axi_adc_regmap_config
= {
328 static const struct iio_backend_ops adi_axi_adc_ops
= {
329 .enable
= axi_adc_enable
,
330 .disable
= axi_adc_disable
,
331 .data_format_set
= axi_adc_data_format_set
,
332 .chan_enable
= axi_adc_chan_enable
,
333 .chan_disable
= axi_adc_chan_disable
,
334 .request_buffer
= axi_adc_request_buffer
,
335 .free_buffer
= axi_adc_free_buffer
,
336 .data_sample_trigger
= axi_adc_data_sample_trigger
,
337 .iodelay_set
= axi_adc_iodelays_set
,
338 .test_pattern_set
= axi_adc_test_pattern_set
,
339 .chan_status
= axi_adc_chan_status
,
340 .debugfs_reg_access
= iio_backend_debugfs_ptr(axi_adc_reg_access
),
341 .debugfs_print_chan_status
= iio_backend_debugfs_ptr(axi_adc_debugfs_print_chan_status
),
344 static const struct iio_backend_info adi_axi_adc_generic
= {
346 .ops
= &adi_axi_adc_ops
,
349 static int adi_axi_adc_probe(struct platform_device
*pdev
)
351 const unsigned int *expected_ver
;
352 struct adi_axi_adc_state
*st
;
358 st
= devm_kzalloc(&pdev
->dev
, sizeof(*st
), GFP_KERNEL
);
362 base
= devm_platform_ioremap_resource(pdev
, 0);
364 return PTR_ERR(base
);
366 st
->dev
= &pdev
->dev
;
367 st
->regmap
= devm_regmap_init_mmio(&pdev
->dev
, base
,
368 &axi_adc_regmap_config
);
369 if (IS_ERR(st
->regmap
))
370 return dev_err_probe(&pdev
->dev
, PTR_ERR(st
->regmap
),
371 "failed to init register map\n");
373 expected_ver
= device_get_match_data(&pdev
->dev
);
377 clk
= devm_clk_get_enabled(&pdev
->dev
, NULL
);
379 return dev_err_probe(&pdev
->dev
, PTR_ERR(clk
),
380 "failed to get clock\n");
383 * Force disable the core. Up to the frontend to enable us. And we can
384 * still read/write registers...
386 ret
= regmap_write(st
->regmap
, ADI_AXI_REG_RSTN
, 0);
390 ret
= regmap_read(st
->regmap
, ADI_AXI_REG_VERSION
, &ver
);
394 if (ADI_AXI_PCORE_VER_MAJOR(ver
) != ADI_AXI_PCORE_VER_MAJOR(*expected_ver
)) {
396 "Major version mismatch. Expected %d.%.2d.%c, Reported %d.%.2d.%c\n",
397 ADI_AXI_PCORE_VER_MAJOR(*expected_ver
),
398 ADI_AXI_PCORE_VER_MINOR(*expected_ver
),
399 ADI_AXI_PCORE_VER_PATCH(*expected_ver
),
400 ADI_AXI_PCORE_VER_MAJOR(ver
),
401 ADI_AXI_PCORE_VER_MINOR(ver
),
402 ADI_AXI_PCORE_VER_PATCH(ver
));
406 ret
= devm_iio_backend_register(&pdev
->dev
, &adi_axi_adc_generic
, st
);
408 return dev_err_probe(&pdev
->dev
, ret
,
409 "failed to register iio backend\n");
411 dev_info(&pdev
->dev
, "AXI ADC IP core (%d.%.2d.%c) probed\n",
412 ADI_AXI_PCORE_VER_MAJOR(ver
),
413 ADI_AXI_PCORE_VER_MINOR(ver
),
414 ADI_AXI_PCORE_VER_PATCH(ver
));
419 static unsigned int adi_axi_adc_10_0_a_info
= ADI_AXI_PCORE_VER(10, 0, 'a');
421 /* Match table for of_platform binding */
422 static const struct of_device_id adi_axi_adc_of_match
[] = {
423 { .compatible
= "adi,axi-adc-10.0.a", .data
= &adi_axi_adc_10_0_a_info
},
424 { /* end of list */ }
426 MODULE_DEVICE_TABLE(of
, adi_axi_adc_of_match
);
428 static struct platform_driver adi_axi_adc_driver
= {
430 .name
= KBUILD_MODNAME
,
431 .of_match_table
= adi_axi_adc_of_match
,
433 .probe
= adi_axi_adc_probe
,
435 module_platform_driver(adi_axi_adc_driver
);
437 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
438 MODULE_DESCRIPTION("Analog Devices Generic AXI ADC IP core driver");
439 MODULE_LICENSE("GPL v2");
440 MODULE_IMPORT_NS(IIO_DMAENGINE_BUFFER
);
441 MODULE_IMPORT_NS(IIO_BACKEND
);