1 // SPDX-License-Identifier: GPL-2.0-only
3 * Analog Devices Generic AXI DAC IP core
4 * Link: https://wiki.analog.com/resources/fpga/docs/axi_dac_ip
6 * Copyright 2016-2024 Analog Devices Inc.
8 #include <linux/bitfield.h>
9 #include <linux/bits.h>
10 #include <linux/cleanup.h>
11 #include <linux/clk.h>
12 #include <linux/device.h>
13 #include <linux/err.h>
14 #include <linux/limits.h>
15 #include <linux/kstrtox.h>
16 #include <linux/math.h>
17 #include <linux/math64.h>
18 #include <linux/module.h>
19 #include <linux/mod_devicetable.h>
20 #include <linux/mutex.h>
21 #include <linux/platform_device.h>
22 #include <linux/property.h>
23 #include <linux/regmap.h>
24 #include <linux/units.h>
26 #include <linux/fpga/adi-axi-common.h>
27 #include <linux/iio/backend.h>
28 #include <linux/iio/buffer-dmaengine.h>
29 #include <linux/iio/buffer.h>
30 #include <linux/iio/iio.h>
33 * Register definitions:
34 * https://wiki.analog.com/resources/fpga/docs/axi_dac_ip#register_map
38 #define AXI_DAC_REG_CONFIG 0x0c
39 #define AXI_DDS_DISABLE BIT(6)
42 #define AXI_DAC_REG_RSTN 0x0040
43 #define AXI_DAC_RSTN_CE_N BIT(2)
44 #define AXI_DAC_RSTN_MMCM_RSTN BIT(1)
45 #define AXI_DAC_RSTN_RSTN BIT(0)
46 #define AXI_DAC_REG_CNTRL_1 0x0044
47 #define AXI_DAC_SYNC BIT(0)
48 #define AXI_DAC_REG_CNTRL_2 0x0048
49 #define ADI_DAC_R1_MODE BIT(4)
50 #define AXI_DAC_DRP_STATUS 0x0074
51 #define AXI_DAC_DRP_LOCKED BIT(17)
52 /* DAC Channel controls */
53 #define AXI_DAC_REG_CHAN_CNTRL_1(c) (0x0400 + (c) * 0x40)
54 #define AXI_DAC_REG_CHAN_CNTRL_3(c) (0x0408 + (c) * 0x40)
55 #define AXI_DAC_SCALE_SIGN BIT(15)
56 #define AXI_DAC_SCALE_INT BIT(14)
57 #define AXI_DAC_SCALE GENMASK(14, 0)
58 #define AXI_DAC_REG_CHAN_CNTRL_2(c) (0x0404 + (c) * 0x40)
59 #define AXI_DAC_REG_CHAN_CNTRL_4(c) (0x040c + (c) * 0x40)
60 #define AXI_DAC_PHASE GENMASK(31, 16)
61 #define AXI_DAC_FREQUENCY GENMASK(15, 0)
62 #define AXI_DAC_REG_CHAN_CNTRL_7(c) (0x0418 + (c) * 0x40)
63 #define AXI_DAC_DATA_SEL GENMASK(3, 0)
65 /* 360 degrees in rad */
66 #define AXI_DAC_2_PI_MEGA 6283190
68 AXI_DAC_DATA_INTERNAL_TONE
,
72 struct axi_dac_state
{
73 struct regmap
*regmap
;
76 * lock to protect multiple accesses to the device registers and global
85 static int axi_dac_enable(struct iio_backend
*back
)
87 struct axi_dac_state
*st
= iio_backend_get_priv(back
);
91 guard(mutex
)(&st
->lock
);
92 ret
= regmap_set_bits(st
->regmap
, AXI_DAC_REG_RSTN
,
93 AXI_DAC_RSTN_MMCM_RSTN
);
97 * Make sure the DRP (Dynamic Reconfiguration Port) is locked. Not all
98 * designs really use it but if they don't we still get the lock bit
99 * set. So let's do it all the time so the code is generic.
101 ret
= regmap_read_poll_timeout(st
->regmap
, AXI_DAC_DRP_STATUS
, __val
,
102 __val
& AXI_DAC_DRP_LOCKED
, 100, 1000);
106 return regmap_set_bits(st
->regmap
, AXI_DAC_REG_RSTN
,
107 AXI_DAC_RSTN_RSTN
| AXI_DAC_RSTN_MMCM_RSTN
);
110 static void axi_dac_disable(struct iio_backend
*back
)
112 struct axi_dac_state
*st
= iio_backend_get_priv(back
);
114 guard(mutex
)(&st
->lock
);
115 regmap_write(st
->regmap
, AXI_DAC_REG_RSTN
, 0);
118 static struct iio_buffer
*axi_dac_request_buffer(struct iio_backend
*back
,
119 struct iio_dev
*indio_dev
)
121 struct axi_dac_state
*st
= iio_backend_get_priv(back
);
122 const char *dma_name
;
124 if (device_property_read_string(st
->dev
, "dma-names", &dma_name
))
127 return iio_dmaengine_buffer_setup_ext(st
->dev
, indio_dev
, dma_name
,
128 IIO_BUFFER_DIRECTION_OUT
);
131 static void axi_dac_free_buffer(struct iio_backend
*back
,
132 struct iio_buffer
*buffer
)
134 iio_dmaengine_buffer_free(buffer
);
140 AXI_DAC_SCALE_TONE_1
,
141 AXI_DAC_SCALE_TONE_2
,
142 AXI_DAC_PHASE_TONE_1
,
143 AXI_DAC_PHASE_TONE_2
,
146 static int __axi_dac_frequency_get(struct axi_dac_state
*st
, unsigned int chan
,
147 unsigned int tone_2
, unsigned int *freq
)
153 dev_err(st
->dev
, "Sampling rate is 0...\n");
158 reg
= AXI_DAC_REG_CHAN_CNTRL_4(chan
);
160 reg
= AXI_DAC_REG_CHAN_CNTRL_2(chan
);
162 ret
= regmap_read(st
->regmap
, reg
, &raw
);
166 raw
= FIELD_GET(AXI_DAC_FREQUENCY
, raw
);
167 *freq
= DIV_ROUND_CLOSEST_ULL(raw
* st
->dac_clk
, BIT(16));
172 static int axi_dac_frequency_get(struct axi_dac_state
*st
,
173 const struct iio_chan_spec
*chan
, char *buf
,
179 scoped_guard(mutex
, &st
->lock
) {
180 ret
= __axi_dac_frequency_get(st
, chan
->channel
, tone_2
, &freq
);
185 return sysfs_emit(buf
, "%u\n", freq
);
188 static int axi_dac_scale_get(struct axi_dac_state
*st
,
189 const struct iio_chan_spec
*chan
, char *buf
,
192 unsigned int scale
, sign
;
197 reg
= AXI_DAC_REG_CHAN_CNTRL_3(chan
->channel
);
199 reg
= AXI_DAC_REG_CHAN_CNTRL_1(chan
->channel
);
201 ret
= regmap_read(st
->regmap
, reg
, &raw
);
205 sign
= FIELD_GET(AXI_DAC_SCALE_SIGN
, raw
);
206 raw
= FIELD_GET(AXI_DAC_SCALE
, raw
);
207 scale
= DIV_ROUND_CLOSEST_ULL((u64
)raw
* MEGA
, AXI_DAC_SCALE_INT
);
209 vals
[0] = scale
/ MEGA
;
210 vals
[1] = scale
% MEGA
;
218 return iio_format_value(buf
, IIO_VAL_INT_PLUS_MICRO
, ARRAY_SIZE(vals
),
222 static int axi_dac_phase_get(struct axi_dac_state
*st
,
223 const struct iio_chan_spec
*chan
, char *buf
,
230 reg
= AXI_DAC_REG_CHAN_CNTRL_4(chan
->channel
);
232 reg
= AXI_DAC_REG_CHAN_CNTRL_2(chan
->channel
);
234 ret
= regmap_read(st
->regmap
, reg
, &raw
);
238 raw
= FIELD_GET(AXI_DAC_PHASE
, raw
);
239 phase
= DIV_ROUND_CLOSEST_ULL((u64
)raw
* AXI_DAC_2_PI_MEGA
, U16_MAX
);
241 vals
[0] = phase
/ MEGA
;
242 vals
[1] = phase
% MEGA
;
244 return iio_format_value(buf
, IIO_VAL_INT_PLUS_MICRO
, ARRAY_SIZE(vals
),
248 static int __axi_dac_frequency_set(struct axi_dac_state
*st
, unsigned int chan
,
249 u64 sample_rate
, unsigned int freq
,
256 if (!sample_rate
|| freq
> sample_rate
/ 2) {
257 dev_err(st
->dev
, "Invalid frequency(%u) dac_clk(%llu)\n",
263 reg
= AXI_DAC_REG_CHAN_CNTRL_4(chan
);
265 reg
= AXI_DAC_REG_CHAN_CNTRL_2(chan
);
267 raw
= DIV64_U64_ROUND_CLOSEST((u64
)freq
* BIT(16), sample_rate
);
269 ret
= regmap_update_bits(st
->regmap
, reg
, AXI_DAC_FREQUENCY
, raw
);
273 /* synchronize channels */
274 return regmap_set_bits(st
->regmap
, AXI_DAC_REG_CNTRL_1
, AXI_DAC_SYNC
);
277 static int axi_dac_frequency_set(struct axi_dac_state
*st
,
278 const struct iio_chan_spec
*chan
,
279 const char *buf
, size_t len
, unsigned int tone_2
)
284 ret
= kstrtou32(buf
, 10, &freq
);
288 guard(mutex
)(&st
->lock
);
289 ret
= __axi_dac_frequency_set(st
, chan
->channel
, st
->dac_clk
, freq
,
297 static int axi_dac_scale_set(struct axi_dac_state
*st
,
298 const struct iio_chan_spec
*chan
,
299 const char *buf
, size_t len
, unsigned int tone_2
)
301 int integer
, frac
, scale
;
305 ret
= iio_str_to_fixpoint(buf
, 100000, &integer
, &frac
);
309 scale
= integer
* MEGA
+ frac
;
310 if (scale
<= -2 * (int)MEGA
|| scale
>= 2 * (int)MEGA
)
313 /* format is 1.1.14 (sign, integer and fractional bits) */
315 raw
= FIELD_PREP(AXI_DAC_SCALE_SIGN
, 1);
319 raw
|= div_u64((u64
)scale
* AXI_DAC_SCALE_INT
, MEGA
);
322 reg
= AXI_DAC_REG_CHAN_CNTRL_3(chan
->channel
);
324 reg
= AXI_DAC_REG_CHAN_CNTRL_1(chan
->channel
);
326 guard(mutex
)(&st
->lock
);
327 ret
= regmap_write(st
->regmap
, reg
, raw
);
331 /* synchronize channels */
332 ret
= regmap_set_bits(st
->regmap
, AXI_DAC_REG_CNTRL_1
, AXI_DAC_SYNC
);
339 static int axi_dac_phase_set(struct axi_dac_state
*st
,
340 const struct iio_chan_spec
*chan
,
341 const char *buf
, size_t len
, unsigned int tone_2
)
343 int integer
, frac
, phase
;
347 ret
= iio_str_to_fixpoint(buf
, 100000, &integer
, &frac
);
351 phase
= integer
* MEGA
+ frac
;
352 if (phase
< 0 || phase
> AXI_DAC_2_PI_MEGA
)
355 raw
= DIV_ROUND_CLOSEST_ULL((u64
)phase
* U16_MAX
, AXI_DAC_2_PI_MEGA
);
358 reg
= AXI_DAC_REG_CHAN_CNTRL_4(chan
->channel
);
360 reg
= AXI_DAC_REG_CHAN_CNTRL_2(chan
->channel
);
362 guard(mutex
)(&st
->lock
);
363 ret
= regmap_update_bits(st
->regmap
, reg
, AXI_DAC_PHASE
,
364 FIELD_PREP(AXI_DAC_PHASE
, raw
));
368 /* synchronize channels */
369 ret
= regmap_set_bits(st
->regmap
, AXI_DAC_REG_CNTRL_1
, AXI_DAC_SYNC
);
376 static int axi_dac_ext_info_set(struct iio_backend
*back
, uintptr_t private,
377 const struct iio_chan_spec
*chan
,
378 const char *buf
, size_t len
)
380 struct axi_dac_state
*st
= iio_backend_get_priv(back
);
383 case AXI_DAC_FREQ_TONE_1
:
384 case AXI_DAC_FREQ_TONE_2
:
385 return axi_dac_frequency_set(st
, chan
, buf
, len
,
386 private == AXI_DAC_FREQ_TONE_2
);
387 case AXI_DAC_SCALE_TONE_1
:
388 case AXI_DAC_SCALE_TONE_2
:
389 return axi_dac_scale_set(st
, chan
, buf
, len
,
390 private == AXI_DAC_SCALE_TONE_2
);
391 case AXI_DAC_PHASE_TONE_1
:
392 case AXI_DAC_PHASE_TONE_2
:
393 return axi_dac_phase_set(st
, chan
, buf
, len
,
394 private == AXI_DAC_PHASE_TONE_2
);
400 static int axi_dac_ext_info_get(struct iio_backend
*back
, uintptr_t private,
401 const struct iio_chan_spec
*chan
, char *buf
)
403 struct axi_dac_state
*st
= iio_backend_get_priv(back
);
406 case AXI_DAC_FREQ_TONE_1
:
407 case AXI_DAC_FREQ_TONE_2
:
408 return axi_dac_frequency_get(st
, chan
, buf
,
409 private - AXI_DAC_FREQ_TONE_1
);
410 case AXI_DAC_SCALE_TONE_1
:
411 case AXI_DAC_SCALE_TONE_2
:
412 return axi_dac_scale_get(st
, chan
, buf
,
413 private - AXI_DAC_SCALE_TONE_1
);
414 case AXI_DAC_PHASE_TONE_1
:
415 case AXI_DAC_PHASE_TONE_2
:
416 return axi_dac_phase_get(st
, chan
, buf
,
417 private - AXI_DAC_PHASE_TONE_1
);
423 static const struct iio_chan_spec_ext_info axi_dac_ext_info
[] = {
424 IIO_BACKEND_EX_INFO("frequency0", IIO_SEPARATE
, AXI_DAC_FREQ_TONE_1
),
425 IIO_BACKEND_EX_INFO("frequency1", IIO_SEPARATE
, AXI_DAC_FREQ_TONE_2
),
426 IIO_BACKEND_EX_INFO("scale0", IIO_SEPARATE
, AXI_DAC_SCALE_TONE_1
),
427 IIO_BACKEND_EX_INFO("scale1", IIO_SEPARATE
, AXI_DAC_SCALE_TONE_2
),
428 IIO_BACKEND_EX_INFO("phase0", IIO_SEPARATE
, AXI_DAC_PHASE_TONE_1
),
429 IIO_BACKEND_EX_INFO("phase1", IIO_SEPARATE
, AXI_DAC_PHASE_TONE_2
),
433 static int axi_dac_extend_chan(struct iio_backend
*back
,
434 struct iio_chan_spec
*chan
)
436 struct axi_dac_state
*st
= iio_backend_get_priv(back
);
438 if (chan
->type
!= IIO_ALTVOLTAGE
)
440 if (st
->reg_config
& AXI_DDS_DISABLE
)
441 /* nothing to extend */
444 chan
->ext_info
= axi_dac_ext_info
;
449 static int axi_dac_data_source_set(struct iio_backend
*back
, unsigned int chan
,
450 enum iio_backend_data_source data
)
452 struct axi_dac_state
*st
= iio_backend_get_priv(back
);
455 case IIO_BACKEND_INTERNAL_CONTINUOUS_WAVE
:
456 return regmap_update_bits(st
->regmap
,
457 AXI_DAC_REG_CHAN_CNTRL_7(chan
),
459 AXI_DAC_DATA_INTERNAL_TONE
);
460 case IIO_BACKEND_EXTERNAL
:
461 return regmap_update_bits(st
->regmap
,
462 AXI_DAC_REG_CHAN_CNTRL_7(chan
),
463 AXI_DAC_DATA_SEL
, AXI_DAC_DATA_DMA
);
469 static int axi_dac_set_sample_rate(struct iio_backend
*back
, unsigned int chan
,
472 struct axi_dac_state
*st
= iio_backend_get_priv(back
);
478 if (st
->reg_config
& AXI_DDS_DISABLE
)
479 /* sample_rate has no meaning if DDS is disabled */
482 guard(mutex
)(&st
->lock
);
484 * If dac_clk is 0 then this must be the first time we're being notified
485 * about the interface sample rate. Hence, just update our internal
486 * variable and bail... If it's not 0, then we get the current DDS
487 * frequency (for the old rate) and update the registers for the new
491 st
->dac_clk
= sample_rate
;
495 for (tone
= 0; tone
<= AXI_DAC_FREQ_TONE_2
; tone
++) {
496 ret
= __axi_dac_frequency_get(st
, chan
, tone
, &freq
);
500 ret
= __axi_dac_frequency_set(st
, chan
, sample_rate
, tone
, freq
);
505 st
->dac_clk
= sample_rate
;
510 static int axi_dac_reg_access(struct iio_backend
*back
, unsigned int reg
,
511 unsigned int writeval
, unsigned int *readval
)
513 struct axi_dac_state
*st
= iio_backend_get_priv(back
);
516 return regmap_read(st
->regmap
, reg
, readval
);
518 return regmap_write(st
->regmap
, reg
, writeval
);
521 static const struct iio_backend_ops axi_dac_generic_ops
= {
522 .enable
= axi_dac_enable
,
523 .disable
= axi_dac_disable
,
524 .request_buffer
= axi_dac_request_buffer
,
525 .free_buffer
= axi_dac_free_buffer
,
526 .extend_chan_spec
= axi_dac_extend_chan
,
527 .ext_info_set
= axi_dac_ext_info_set
,
528 .ext_info_get
= axi_dac_ext_info_get
,
529 .data_source_set
= axi_dac_data_source_set
,
530 .set_sample_rate
= axi_dac_set_sample_rate
,
531 .debugfs_reg_access
= iio_backend_debugfs_ptr(axi_dac_reg_access
),
534 static const struct iio_backend_info axi_dac_generic
= {
536 .ops
= &axi_dac_generic_ops
,
539 static const struct regmap_config axi_dac_regmap_config
= {
543 .max_register
= 0x0800,
546 static int axi_dac_probe(struct platform_device
*pdev
)
548 const unsigned int *expected_ver
;
549 struct axi_dac_state
*st
;
555 st
= devm_kzalloc(&pdev
->dev
, sizeof(*st
), GFP_KERNEL
);
559 expected_ver
= device_get_match_data(&pdev
->dev
);
563 clk
= devm_clk_get_enabled(&pdev
->dev
, NULL
);
565 return dev_err_probe(&pdev
->dev
, PTR_ERR(clk
),
566 "failed to get clock\n");
568 base
= devm_platform_ioremap_resource(pdev
, 0);
570 return PTR_ERR(base
);
572 st
->dev
= &pdev
->dev
;
573 st
->regmap
= devm_regmap_init_mmio(&pdev
->dev
, base
,
574 &axi_dac_regmap_config
);
575 if (IS_ERR(st
->regmap
))
576 return dev_err_probe(&pdev
->dev
, PTR_ERR(st
->regmap
),
577 "failed to init register map\n");
580 * Force disable the core. Up to the frontend to enable us. And we can
581 * still read/write registers...
583 ret
= regmap_write(st
->regmap
, AXI_DAC_REG_RSTN
, 0);
587 ret
= regmap_read(st
->regmap
, ADI_AXI_REG_VERSION
, &ver
);
591 if (ADI_AXI_PCORE_VER_MAJOR(ver
) != ADI_AXI_PCORE_VER_MAJOR(*expected_ver
)) {
593 "Major version mismatch. Expected %d.%.2d.%c, Reported %d.%.2d.%c\n",
594 ADI_AXI_PCORE_VER_MAJOR(*expected_ver
),
595 ADI_AXI_PCORE_VER_MINOR(*expected_ver
),
596 ADI_AXI_PCORE_VER_PATCH(*expected_ver
),
597 ADI_AXI_PCORE_VER_MAJOR(ver
),
598 ADI_AXI_PCORE_VER_MINOR(ver
),
599 ADI_AXI_PCORE_VER_PATCH(ver
));
603 /* Let's get the core read only configuration */
604 ret
= regmap_read(st
->regmap
, AXI_DAC_REG_CONFIG
, &st
->reg_config
);
609 * In some designs, setting the R1_MODE bit to 0 (which is the default
610 * value) causes all channels of the frontend to be routed to the same
611 * DMA (so they are sampled together). This is for things like
612 * Multiple-Input and Multiple-Output (MIMO). As most of the times we
613 * want independent channels let's override the core's default value and
614 * set the R1_MODE bit.
616 ret
= regmap_set_bits(st
->regmap
, AXI_DAC_REG_CNTRL_2
, ADI_DAC_R1_MODE
);
620 mutex_init(&st
->lock
);
621 ret
= devm_iio_backend_register(&pdev
->dev
, &axi_dac_generic
, st
);
623 return dev_err_probe(&pdev
->dev
, ret
,
624 "failed to register iio backend\n");
626 dev_info(&pdev
->dev
, "AXI DAC IP core (%d.%.2d.%c) probed\n",
627 ADI_AXI_PCORE_VER_MAJOR(ver
),
628 ADI_AXI_PCORE_VER_MINOR(ver
),
629 ADI_AXI_PCORE_VER_PATCH(ver
));
634 static unsigned int axi_dac_9_1_b_info
= ADI_AXI_PCORE_VER(9, 1, 'b');
636 static const struct of_device_id axi_dac_of_match
[] = {
637 { .compatible
= "adi,axi-dac-9.1.b", .data
= &axi_dac_9_1_b_info
},
640 MODULE_DEVICE_TABLE(of
, axi_dac_of_match
);
642 static struct platform_driver axi_dac_driver
= {
644 .name
= "adi-axi-dac",
645 .of_match_table
= axi_dac_of_match
,
647 .probe
= axi_dac_probe
,
649 module_platform_driver(axi_dac_driver
);
651 MODULE_AUTHOR("Nuno Sa <nuno.sa@analog.com>");
652 MODULE_DESCRIPTION("Analog Devices Generic AXI DAC IP core driver");
653 MODULE_LICENSE("GPL");
654 MODULE_IMPORT_NS(IIO_DMAENGINE_BUFFER
);
655 MODULE_IMPORT_NS(IIO_BACKEND
);