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>
32 #include "ad3552r-hs.h"
35 * Register definitions:
36 * https://wiki.analog.com/resources/fpga/docs/axi_dac_ip#register_map
40 #define AXI_DAC_CONFIG_REG 0x0c
41 #define AXI_DAC_CONFIG_DDS_DISABLE BIT(6)
44 #define AXI_DAC_RSTN_REG 0x0040
45 #define AXI_DAC_RSTN_CE_N BIT(2)
46 #define AXI_DAC_RSTN_MMCM_RSTN BIT(1)
47 #define AXI_DAC_RSTN_RSTN BIT(0)
48 #define AXI_DAC_CNTRL_1_REG 0x0044
49 #define AXI_DAC_CNTRL_1_SYNC BIT(0)
50 #define AXI_DAC_CNTRL_2_REG 0x0048
51 #define AXI_DAC_CNTRL_2_SDR_DDR_N BIT(16)
52 #define AXI_DAC_CNTRL_2_SYMB_8B BIT(14)
53 #define ADI_DAC_CNTRL_2_R1_MODE BIT(5)
54 #define AXI_DAC_CNTRL_2_UNSIGNED_DATA BIT(4)
55 #define AXI_DAC_STATUS_1_REG 0x0054
56 #define AXI_DAC_STATUS_2_REG 0x0058
57 #define AXI_DAC_DRP_STATUS_REG 0x0074
58 #define AXI_DAC_DRP_STATUS_DRP_LOCKED BIT(17)
59 #define AXI_DAC_CUSTOM_RD_REG 0x0080
60 #define AXI_DAC_CUSTOM_WR_REG 0x0084
61 #define AXI_DAC_CUSTOM_WR_DATA_8 GENMASK(23, 16)
62 #define AXI_DAC_CUSTOM_WR_DATA_16 GENMASK(23, 8)
63 #define AXI_DAC_UI_STATUS_REG 0x0088
64 #define AXI_DAC_UI_STATUS_IF_BUSY BIT(4)
65 #define AXI_DAC_CUSTOM_CTRL_REG 0x008C
66 #define AXI_DAC_CUSTOM_CTRL_ADDRESS GENMASK(31, 24)
67 #define AXI_DAC_CUSTOM_CTRL_SYNCED_TRANSFER BIT(2)
68 #define AXI_DAC_CUSTOM_CTRL_STREAM BIT(1)
69 #define AXI_DAC_CUSTOM_CTRL_TRANSFER_DATA BIT(0)
71 #define AXI_DAC_CUSTOM_CTRL_STREAM_ENABLE (AXI_DAC_CUSTOM_CTRL_TRANSFER_DATA | \
72 AXI_DAC_CUSTOM_CTRL_STREAM)
74 /* DAC Channel controls */
75 #define AXI_DAC_CHAN_CNTRL_1_REG(c) (0x0400 + (c) * 0x40)
76 #define AXI_DAC_CHAN_CNTRL_3_REG(c) (0x0408 + (c) * 0x40)
77 #define AXI_DAC_CHAN_CNTRL_3_SCALE_SIGN BIT(15)
78 #define AXI_DAC_CHAN_CNTRL_3_SCALE_INT BIT(14)
79 #define AXI_DAC_CHAN_CNTRL_3_SCALE GENMASK(14, 0)
80 #define AXI_DAC_CHAN_CNTRL_2_REG(c) (0x0404 + (c) * 0x40)
81 #define AXI_DAC_CHAN_CNTRL_2_PHASE GENMASK(31, 16)
82 #define AXI_DAC_CHAN_CNTRL_2_FREQUENCY GENMASK(15, 0)
83 #define AXI_DAC_CHAN_CNTRL_4_REG(c) (0x040c + (c) * 0x40)
84 #define AXI_DAC_CHAN_CNTRL_7_REG(c) (0x0418 + (c) * 0x40)
85 #define AXI_DAC_CHAN_CNTRL_7_DATA_SEL GENMASK(3, 0)
87 #define AXI_DAC_RD_ADDR(x) (BIT(7) | (x))
89 /* 360 degrees in rad */
90 #define AXI_DAC_2_PI_MEGA 6283190
93 AXI_DAC_DATA_INTERNAL_TONE
,
95 AXI_DAC_DATA_INTERNAL_RAMP_16BIT
= 11,
100 const struct iio_backend_info
*backend_info
;
102 bool has_child_nodes
;
105 struct axi_dac_state
{
106 struct regmap
*regmap
;
109 * lock to protect multiple accesses to the device registers and global
113 const struct axi_dac_info
*info
;
120 static int axi_dac_enable(struct iio_backend
*back
)
122 struct axi_dac_state
*st
= iio_backend_get_priv(back
);
126 guard(mutex
)(&st
->lock
);
127 ret
= regmap_set_bits(st
->regmap
, AXI_DAC_RSTN_REG
,
128 AXI_DAC_RSTN_MMCM_RSTN
);
132 * Make sure the DRP (Dynamic Reconfiguration Port) is locked. Not all
133 * designs really use it but if they don't we still get the lock bit
134 * set. So let's do it all the time so the code is generic.
136 ret
= regmap_read_poll_timeout(st
->regmap
, AXI_DAC_DRP_STATUS_REG
,
138 __val
& AXI_DAC_DRP_STATUS_DRP_LOCKED
,
143 return regmap_set_bits(st
->regmap
, AXI_DAC_RSTN_REG
,
144 AXI_DAC_RSTN_RSTN
| AXI_DAC_RSTN_MMCM_RSTN
);
147 static void axi_dac_disable(struct iio_backend
*back
)
149 struct axi_dac_state
*st
= iio_backend_get_priv(back
);
151 guard(mutex
)(&st
->lock
);
152 regmap_write(st
->regmap
, AXI_DAC_RSTN_REG
, 0);
155 static struct iio_buffer
*axi_dac_request_buffer(struct iio_backend
*back
,
156 struct iio_dev
*indio_dev
)
158 struct axi_dac_state
*st
= iio_backend_get_priv(back
);
159 const char *dma_name
;
161 if (device_property_read_string(st
->dev
, "dma-names", &dma_name
))
164 return iio_dmaengine_buffer_setup_ext(st
->dev
, indio_dev
, dma_name
,
165 IIO_BUFFER_DIRECTION_OUT
);
168 static void axi_dac_free_buffer(struct iio_backend
*back
,
169 struct iio_buffer
*buffer
)
171 iio_dmaengine_buffer_free(buffer
);
177 AXI_DAC_SCALE_TONE_1
,
178 AXI_DAC_SCALE_TONE_2
,
179 AXI_DAC_PHASE_TONE_1
,
180 AXI_DAC_PHASE_TONE_2
,
183 static int __axi_dac_frequency_get(struct axi_dac_state
*st
, unsigned int chan
,
184 unsigned int tone_2
, unsigned int *freq
)
190 dev_err(st
->dev
, "Sampling rate is 0...\n");
195 reg
= AXI_DAC_CHAN_CNTRL_4_REG(chan
);
197 reg
= AXI_DAC_CHAN_CNTRL_2_REG(chan
);
199 ret
= regmap_read(st
->regmap
, reg
, &raw
);
203 raw
= FIELD_GET(AXI_DAC_CHAN_CNTRL_2_FREQUENCY
, raw
);
204 *freq
= DIV_ROUND_CLOSEST_ULL(raw
* st
->dac_clk
, BIT(16));
209 static int axi_dac_frequency_get(struct axi_dac_state
*st
,
210 const struct iio_chan_spec
*chan
, char *buf
,
216 scoped_guard(mutex
, &st
->lock
) {
217 ret
= __axi_dac_frequency_get(st
, chan
->channel
, tone_2
, &freq
);
222 return sysfs_emit(buf
, "%u\n", freq
);
225 static int axi_dac_scale_get(struct axi_dac_state
*st
,
226 const struct iio_chan_spec
*chan
, char *buf
,
229 unsigned int scale
, sign
;
234 reg
= AXI_DAC_CHAN_CNTRL_3_REG(chan
->channel
);
236 reg
= AXI_DAC_CHAN_CNTRL_1_REG(chan
->channel
);
238 ret
= regmap_read(st
->regmap
, reg
, &raw
);
242 sign
= FIELD_GET(AXI_DAC_CHAN_CNTRL_3_SCALE_SIGN
, raw
);
243 raw
= FIELD_GET(AXI_DAC_CHAN_CNTRL_3_SCALE
, raw
);
244 scale
= DIV_ROUND_CLOSEST_ULL((u64
)raw
* MEGA
,
245 AXI_DAC_CHAN_CNTRL_3_SCALE_INT
);
247 vals
[0] = scale
/ MEGA
;
248 vals
[1] = scale
% MEGA
;
256 return iio_format_value(buf
, IIO_VAL_INT_PLUS_MICRO
, ARRAY_SIZE(vals
),
260 static int axi_dac_phase_get(struct axi_dac_state
*st
,
261 const struct iio_chan_spec
*chan
, char *buf
,
268 reg
= AXI_DAC_CHAN_CNTRL_4_REG(chan
->channel
);
270 reg
= AXI_DAC_CHAN_CNTRL_2_REG(chan
->channel
);
272 ret
= regmap_read(st
->regmap
, reg
, &raw
);
276 raw
= FIELD_GET(AXI_DAC_CHAN_CNTRL_2_PHASE
, raw
);
277 phase
= DIV_ROUND_CLOSEST_ULL((u64
)raw
* AXI_DAC_2_PI_MEGA
, U16_MAX
);
279 vals
[0] = phase
/ MEGA
;
280 vals
[1] = phase
% MEGA
;
282 return iio_format_value(buf
, IIO_VAL_INT_PLUS_MICRO
, ARRAY_SIZE(vals
),
286 static int __axi_dac_frequency_set(struct axi_dac_state
*st
, unsigned int chan
,
287 u64 sample_rate
, unsigned int freq
,
294 if (!sample_rate
|| freq
> sample_rate
/ 2) {
295 dev_err(st
->dev
, "Invalid frequency(%u) dac_clk(%llu)\n",
301 reg
= AXI_DAC_CHAN_CNTRL_4_REG(chan
);
303 reg
= AXI_DAC_CHAN_CNTRL_2_REG(chan
);
305 raw
= DIV64_U64_ROUND_CLOSEST((u64
)freq
* BIT(16), sample_rate
);
307 ret
= regmap_update_bits(st
->regmap
, reg
,
308 AXI_DAC_CHAN_CNTRL_2_FREQUENCY
, raw
);
312 /* synchronize channels */
313 return regmap_set_bits(st
->regmap
, AXI_DAC_CNTRL_1_REG
,
314 AXI_DAC_CNTRL_1_SYNC
);
317 static int axi_dac_frequency_set(struct axi_dac_state
*st
,
318 const struct iio_chan_spec
*chan
,
319 const char *buf
, size_t len
, unsigned int tone_2
)
324 ret
= kstrtou32(buf
, 10, &freq
);
328 guard(mutex
)(&st
->lock
);
329 ret
= __axi_dac_frequency_set(st
, chan
->channel
, st
->dac_clk
, freq
,
337 static int axi_dac_scale_set(struct axi_dac_state
*st
,
338 const struct iio_chan_spec
*chan
,
339 const char *buf
, size_t len
, unsigned int tone_2
)
341 int integer
, frac
, scale
;
345 ret
= iio_str_to_fixpoint(buf
, 100000, &integer
, &frac
);
349 scale
= integer
* MEGA
+ frac
;
350 if (scale
<= -2 * (int)MEGA
|| scale
>= 2 * (int)MEGA
)
353 /* format is 1.1.14 (sign, integer and fractional bits) */
355 raw
= FIELD_PREP(AXI_DAC_CHAN_CNTRL_3_SCALE_SIGN
, 1);
359 raw
|= div_u64((u64
)scale
* AXI_DAC_CHAN_CNTRL_3_SCALE_INT
, MEGA
);
362 reg
= AXI_DAC_CHAN_CNTRL_3_REG(chan
->channel
);
364 reg
= AXI_DAC_CHAN_CNTRL_1_REG(chan
->channel
);
366 guard(mutex
)(&st
->lock
);
367 ret
= regmap_write(st
->regmap
, reg
, raw
);
371 /* synchronize channels */
372 ret
= regmap_set_bits(st
->regmap
, AXI_DAC_CNTRL_1_REG
,
373 AXI_DAC_CNTRL_1_SYNC
);
380 static int axi_dac_phase_set(struct axi_dac_state
*st
,
381 const struct iio_chan_spec
*chan
,
382 const char *buf
, size_t len
, unsigned int tone_2
)
384 int integer
, frac
, phase
;
388 ret
= iio_str_to_fixpoint(buf
, 100000, &integer
, &frac
);
392 phase
= integer
* MEGA
+ frac
;
393 if (phase
< 0 || phase
> AXI_DAC_2_PI_MEGA
)
396 raw
= DIV_ROUND_CLOSEST_ULL((u64
)phase
* U16_MAX
, AXI_DAC_2_PI_MEGA
);
399 reg
= AXI_DAC_CHAN_CNTRL_4_REG(chan
->channel
);
401 reg
= AXI_DAC_CHAN_CNTRL_2_REG(chan
->channel
);
403 guard(mutex
)(&st
->lock
);
404 ret
= regmap_update_bits(st
->regmap
, reg
, AXI_DAC_CHAN_CNTRL_2_PHASE
,
405 FIELD_PREP(AXI_DAC_CHAN_CNTRL_2_PHASE
, raw
));
409 /* synchronize channels */
410 ret
= regmap_set_bits(st
->regmap
, AXI_DAC_CNTRL_1_REG
,
411 AXI_DAC_CNTRL_1_SYNC
);
418 static int axi_dac_ext_info_set(struct iio_backend
*back
, uintptr_t private,
419 const struct iio_chan_spec
*chan
,
420 const char *buf
, size_t len
)
422 struct axi_dac_state
*st
= iio_backend_get_priv(back
);
425 case AXI_DAC_FREQ_TONE_1
:
426 case AXI_DAC_FREQ_TONE_2
:
427 return axi_dac_frequency_set(st
, chan
, buf
, len
,
428 private == AXI_DAC_FREQ_TONE_2
);
429 case AXI_DAC_SCALE_TONE_1
:
430 case AXI_DAC_SCALE_TONE_2
:
431 return axi_dac_scale_set(st
, chan
, buf
, len
,
432 private == AXI_DAC_SCALE_TONE_2
);
433 case AXI_DAC_PHASE_TONE_1
:
434 case AXI_DAC_PHASE_TONE_2
:
435 return axi_dac_phase_set(st
, chan
, buf
, len
,
436 private == AXI_DAC_PHASE_TONE_2
);
442 static int axi_dac_ext_info_get(struct iio_backend
*back
, uintptr_t private,
443 const struct iio_chan_spec
*chan
, char *buf
)
445 struct axi_dac_state
*st
= iio_backend_get_priv(back
);
448 case AXI_DAC_FREQ_TONE_1
:
449 case AXI_DAC_FREQ_TONE_2
:
450 return axi_dac_frequency_get(st
, chan
, buf
,
451 private - AXI_DAC_FREQ_TONE_1
);
452 case AXI_DAC_SCALE_TONE_1
:
453 case AXI_DAC_SCALE_TONE_2
:
454 return axi_dac_scale_get(st
, chan
, buf
,
455 private - AXI_DAC_SCALE_TONE_1
);
456 case AXI_DAC_PHASE_TONE_1
:
457 case AXI_DAC_PHASE_TONE_2
:
458 return axi_dac_phase_get(st
, chan
, buf
,
459 private - AXI_DAC_PHASE_TONE_1
);
465 static const struct iio_chan_spec_ext_info axi_dac_ext_info
[] = {
466 IIO_BACKEND_EX_INFO("frequency0", IIO_SEPARATE
, AXI_DAC_FREQ_TONE_1
),
467 IIO_BACKEND_EX_INFO("frequency1", IIO_SEPARATE
, AXI_DAC_FREQ_TONE_2
),
468 IIO_BACKEND_EX_INFO("scale0", IIO_SEPARATE
, AXI_DAC_SCALE_TONE_1
),
469 IIO_BACKEND_EX_INFO("scale1", IIO_SEPARATE
, AXI_DAC_SCALE_TONE_2
),
470 IIO_BACKEND_EX_INFO("phase0", IIO_SEPARATE
, AXI_DAC_PHASE_TONE_1
),
471 IIO_BACKEND_EX_INFO("phase1", IIO_SEPARATE
, AXI_DAC_PHASE_TONE_2
),
475 static int axi_dac_extend_chan(struct iio_backend
*back
,
476 struct iio_chan_spec
*chan
)
478 struct axi_dac_state
*st
= iio_backend_get_priv(back
);
480 if (chan
->type
!= IIO_ALTVOLTAGE
)
482 if (st
->reg_config
& AXI_DAC_CONFIG_DDS_DISABLE
)
483 /* nothing to extend */
486 chan
->ext_info
= axi_dac_ext_info
;
491 static int axi_dac_data_source_set(struct iio_backend
*back
, unsigned int chan
,
492 enum iio_backend_data_source data
)
494 struct axi_dac_state
*st
= iio_backend_get_priv(back
);
497 case IIO_BACKEND_INTERNAL_CONTINUOUS_WAVE
:
498 return regmap_update_bits(st
->regmap
,
499 AXI_DAC_CHAN_CNTRL_7_REG(chan
),
500 AXI_DAC_CHAN_CNTRL_7_DATA_SEL
,
501 AXI_DAC_DATA_INTERNAL_TONE
);
502 case IIO_BACKEND_EXTERNAL
:
503 return regmap_update_bits(st
->regmap
,
504 AXI_DAC_CHAN_CNTRL_7_REG(chan
),
505 AXI_DAC_CHAN_CNTRL_7_DATA_SEL
,
507 case IIO_BACKEND_INTERNAL_RAMP_16BIT
:
508 return regmap_update_bits(st
->regmap
,
509 AXI_DAC_CHAN_CNTRL_7_REG(chan
),
510 AXI_DAC_CHAN_CNTRL_7_DATA_SEL
,
511 AXI_DAC_DATA_INTERNAL_RAMP_16BIT
);
517 static int axi_dac_set_sample_rate(struct iio_backend
*back
, unsigned int chan
,
520 struct axi_dac_state
*st
= iio_backend_get_priv(back
);
526 if (st
->reg_config
& AXI_DAC_CONFIG_DDS_DISABLE
)
527 /* sample_rate has no meaning if DDS is disabled */
530 guard(mutex
)(&st
->lock
);
532 * If dac_clk is 0 then this must be the first time we're being notified
533 * about the interface sample rate. Hence, just update our internal
534 * variable and bail... If it's not 0, then we get the current DDS
535 * frequency (for the old rate) and update the registers for the new
539 st
->dac_clk
= sample_rate
;
543 for (tone
= 0; tone
<= AXI_DAC_FREQ_TONE_2
; tone
++) {
544 ret
= __axi_dac_frequency_get(st
, chan
, tone
, &freq
);
548 ret
= __axi_dac_frequency_set(st
, chan
, sample_rate
, tone
, freq
);
553 st
->dac_clk
= sample_rate
;
558 static int axi_dac_reg_access(struct iio_backend
*back
, unsigned int reg
,
559 unsigned int writeval
, unsigned int *readval
)
561 struct axi_dac_state
*st
= iio_backend_get_priv(back
);
564 return regmap_read(st
->regmap
, reg
, readval
);
566 return regmap_write(st
->regmap
, reg
, writeval
);
569 static int axi_dac_ddr_enable(struct iio_backend
*back
)
571 struct axi_dac_state
*st
= iio_backend_get_priv(back
);
573 return regmap_clear_bits(st
->regmap
, AXI_DAC_CNTRL_2_REG
,
574 AXI_DAC_CNTRL_2_SDR_DDR_N
);
577 static int axi_dac_ddr_disable(struct iio_backend
*back
)
579 struct axi_dac_state
*st
= iio_backend_get_priv(back
);
581 return regmap_set_bits(st
->regmap
, AXI_DAC_CNTRL_2_REG
,
582 AXI_DAC_CNTRL_2_SDR_DDR_N
);
585 static int axi_dac_data_stream_enable(struct iio_backend
*back
)
587 struct axi_dac_state
*st
= iio_backend_get_priv(back
);
589 return regmap_set_bits(st
->regmap
, AXI_DAC_CUSTOM_CTRL_REG
,
590 AXI_DAC_CUSTOM_CTRL_STREAM_ENABLE
);
593 static int axi_dac_data_stream_disable(struct iio_backend
*back
)
595 struct axi_dac_state
*st
= iio_backend_get_priv(back
);
597 return regmap_clear_bits(st
->regmap
, AXI_DAC_CUSTOM_CTRL_REG
,
598 AXI_DAC_CUSTOM_CTRL_STREAM_ENABLE
);
601 static int axi_dac_data_transfer_addr(struct iio_backend
*back
, u32 address
)
603 struct axi_dac_state
*st
= iio_backend_get_priv(back
);
605 if (address
> FIELD_MAX(AXI_DAC_CUSTOM_CTRL_ADDRESS
))
609 * Sample register address, when the DAC is configured, or stream
610 * start address when the FSM is in stream state.
612 return regmap_update_bits(st
->regmap
, AXI_DAC_CUSTOM_CTRL_REG
,
613 AXI_DAC_CUSTOM_CTRL_ADDRESS
,
614 FIELD_PREP(AXI_DAC_CUSTOM_CTRL_ADDRESS
,
618 static int axi_dac_data_format_set(struct iio_backend
*back
, unsigned int ch
,
619 const struct iio_backend_data_fmt
*data
)
621 struct axi_dac_state
*st
= iio_backend_get_priv(back
);
623 switch (data
->type
) {
624 case IIO_BACKEND_DATA_UNSIGNED
:
625 return regmap_clear_bits(st
->regmap
, AXI_DAC_CNTRL_2_REG
,
626 AXI_DAC_CNTRL_2_UNSIGNED_DATA
);
632 static int __axi_dac_bus_reg_write(struct iio_backend
*back
, u32 reg
,
633 u32 val
, size_t data_size
)
635 struct axi_dac_state
*st
= iio_backend_get_priv(back
);
640 * Both AXI_DAC_CNTRL_2_REG and AXI_DAC_CUSTOM_WR_REG need to know
641 * the data size. So keeping data size control here only,
642 * since data size is mandatory for the current transfer.
643 * DDR state handled separately by specific backend calls,
644 * generally all raw register writes are SDR.
646 if (data_size
== sizeof(u16
))
647 ival
= FIELD_PREP(AXI_DAC_CUSTOM_WR_DATA_16
, val
);
649 ival
= FIELD_PREP(AXI_DAC_CUSTOM_WR_DATA_8
, val
);
651 ret
= regmap_write(st
->regmap
, AXI_DAC_CUSTOM_WR_REG
, ival
);
655 if (data_size
== sizeof(u8
))
656 ret
= regmap_set_bits(st
->regmap
, AXI_DAC_CNTRL_2_REG
,
657 AXI_DAC_CNTRL_2_SYMB_8B
);
659 ret
= regmap_clear_bits(st
->regmap
, AXI_DAC_CNTRL_2_REG
,
660 AXI_DAC_CNTRL_2_SYMB_8B
);
664 ret
= regmap_update_bits(st
->regmap
, AXI_DAC_CUSTOM_CTRL_REG
,
665 AXI_DAC_CUSTOM_CTRL_ADDRESS
,
666 FIELD_PREP(AXI_DAC_CUSTOM_CTRL_ADDRESS
, reg
));
670 ret
= regmap_update_bits(st
->regmap
, AXI_DAC_CUSTOM_CTRL_REG
,
671 AXI_DAC_CUSTOM_CTRL_TRANSFER_DATA
,
672 AXI_DAC_CUSTOM_CTRL_TRANSFER_DATA
);
676 ret
= regmap_read_poll_timeout(st
->regmap
,
677 AXI_DAC_UI_STATUS_REG
, ival
,
678 FIELD_GET(AXI_DAC_UI_STATUS_IF_BUSY
, ival
) == 0,
680 if (ret
== -ETIMEDOUT
)
681 dev_err(st
->dev
, "AXI read timeout\n");
683 /* Cleaning always AXI_DAC_CUSTOM_CTRL_TRANSFER_DATA */
684 return regmap_clear_bits(st
->regmap
, AXI_DAC_CUSTOM_CTRL_REG
,
685 AXI_DAC_CUSTOM_CTRL_TRANSFER_DATA
);
688 static int axi_dac_bus_reg_write(struct iio_backend
*back
, u32 reg
,
689 u32 val
, size_t data_size
)
691 struct axi_dac_state
*st
= iio_backend_get_priv(back
);
693 guard(mutex
)(&st
->lock
);
694 return __axi_dac_bus_reg_write(back
, reg
, val
, data_size
);
697 static int axi_dac_bus_reg_read(struct iio_backend
*back
, u32 reg
, u32
*val
,
700 struct axi_dac_state
*st
= iio_backend_get_priv(back
);
703 guard(mutex
)(&st
->lock
);
706 * SPI, we write with read flag, then we read just at the AXI
707 * io address space to get data read.
709 ret
= __axi_dac_bus_reg_write(back
, AXI_DAC_RD_ADDR(reg
), 0,
714 return regmap_read(st
->regmap
, AXI_DAC_CUSTOM_RD_REG
, val
);
717 static void axi_dac_child_remove(void *data
)
719 platform_device_unregister(data
);
722 static int axi_dac_create_platform_device(struct axi_dac_state
*st
,
723 struct fwnode_handle
*child
)
725 struct ad3552r_hs_platform_data pdata
= {
726 .bus_reg_read
= axi_dac_bus_reg_read
,
727 .bus_reg_write
= axi_dac_bus_reg_write
,
728 .bus_sample_data_clock_hz
= st
->dac_clk_rate
,
730 struct platform_device_info pi
= {
732 .name
= fwnode_get_name(child
),
733 .id
= PLATFORM_DEVID_AUTO
,
736 .size_data
= sizeof(pdata
),
738 struct platform_device
*pdev
;
740 pdev
= platform_device_register_full(&pi
);
742 return PTR_ERR(pdev
);
744 return devm_add_action_or_reset(st
->dev
, axi_dac_child_remove
, pdev
);
747 static const struct iio_backend_ops axi_dac_generic_ops
= {
748 .enable
= axi_dac_enable
,
749 .disable
= axi_dac_disable
,
750 .request_buffer
= axi_dac_request_buffer
,
751 .free_buffer
= axi_dac_free_buffer
,
752 .extend_chan_spec
= axi_dac_extend_chan
,
753 .ext_info_set
= axi_dac_ext_info_set
,
754 .ext_info_get
= axi_dac_ext_info_get
,
755 .data_source_set
= axi_dac_data_source_set
,
756 .set_sample_rate
= axi_dac_set_sample_rate
,
757 .debugfs_reg_access
= iio_backend_debugfs_ptr(axi_dac_reg_access
),
760 static const struct iio_backend_ops axi_ad3552r_ops
= {
761 .enable
= axi_dac_enable
,
762 .disable
= axi_dac_disable
,
763 .request_buffer
= axi_dac_request_buffer
,
764 .free_buffer
= axi_dac_free_buffer
,
765 .data_source_set
= axi_dac_data_source_set
,
766 .ddr_enable
= axi_dac_ddr_enable
,
767 .ddr_disable
= axi_dac_ddr_disable
,
768 .data_stream_enable
= axi_dac_data_stream_enable
,
769 .data_stream_disable
= axi_dac_data_stream_disable
,
770 .data_format_set
= axi_dac_data_format_set
,
771 .data_transfer_addr
= axi_dac_data_transfer_addr
,
774 static const struct iio_backend_info axi_dac_generic
= {
776 .ops
= &axi_dac_generic_ops
,
779 static const struct iio_backend_info axi_ad3552r
= {
780 .name
= "axi-ad3552r",
781 .ops
= &axi_ad3552r_ops
,
784 static const struct regmap_config axi_dac_regmap_config
= {
788 .max_register
= 0x0800,
791 static int axi_dac_probe(struct platform_device
*pdev
)
793 struct axi_dac_state
*st
;
799 st
= devm_kzalloc(&pdev
->dev
, sizeof(*st
), GFP_KERNEL
);
803 st
->info
= device_get_match_data(&pdev
->dev
);
806 clk
= devm_clk_get_enabled(&pdev
->dev
, "s_axi_aclk");
808 /* Backward compat., old fdt versions without clock-names. */
809 clk
= devm_clk_get_enabled(&pdev
->dev
, NULL
);
811 return dev_err_probe(&pdev
->dev
, PTR_ERR(clk
),
812 "failed to get clock\n");
815 if (st
->info
->has_dac_clk
) {
818 dac_clk
= devm_clk_get_enabled(&pdev
->dev
, "dac_clk");
820 return dev_err_probe(&pdev
->dev
, PTR_ERR(dac_clk
),
821 "failed to get dac_clk clock\n");
823 /* We only care about the streaming mode rate */
824 st
->dac_clk_rate
= clk_get_rate(dac_clk
) / 2;
827 base
= devm_platform_ioremap_resource(pdev
, 0);
829 return PTR_ERR(base
);
831 st
->dev
= &pdev
->dev
;
832 st
->regmap
= devm_regmap_init_mmio(&pdev
->dev
, base
,
833 &axi_dac_regmap_config
);
834 if (IS_ERR(st
->regmap
))
835 return dev_err_probe(&pdev
->dev
, PTR_ERR(st
->regmap
),
836 "failed to init register map\n");
839 * Force disable the core. Up to the frontend to enable us. And we can
840 * still read/write registers...
842 ret
= regmap_write(st
->regmap
, AXI_DAC_RSTN_REG
, 0);
846 ret
= regmap_read(st
->regmap
, ADI_AXI_REG_VERSION
, &ver
);
850 if (ADI_AXI_PCORE_VER_MAJOR(ver
) !=
851 ADI_AXI_PCORE_VER_MAJOR(st
->info
->version
)) {
853 "Major version mismatch. Expected %d.%.2d.%c, Reported %d.%.2d.%c\n",
854 ADI_AXI_PCORE_VER_MAJOR(st
->info
->version
),
855 ADI_AXI_PCORE_VER_MINOR(st
->info
->version
),
856 ADI_AXI_PCORE_VER_PATCH(st
->info
->version
),
857 ADI_AXI_PCORE_VER_MAJOR(ver
),
858 ADI_AXI_PCORE_VER_MINOR(ver
),
859 ADI_AXI_PCORE_VER_PATCH(ver
));
863 /* Let's get the core read only configuration */
864 ret
= regmap_read(st
->regmap
, AXI_DAC_CONFIG_REG
, &st
->reg_config
);
869 * In some designs, setting the R1_MODE bit to 0 (which is the default
870 * value) causes all channels of the frontend to be routed to the same
871 * DMA (so they are sampled together). This is for things like
872 * Multiple-Input and Multiple-Output (MIMO). As most of the times we
873 * want independent channels let's override the core's default value and
874 * set the R1_MODE bit.
876 ret
= regmap_set_bits(st
->regmap
, AXI_DAC_CNTRL_2_REG
,
877 ADI_DAC_CNTRL_2_R1_MODE
);
881 mutex_init(&st
->lock
);
883 ret
= devm_iio_backend_register(&pdev
->dev
, st
->info
->backend_info
, st
);
885 return dev_err_probe(&pdev
->dev
, ret
,
886 "failed to register iio backend\n");
888 device_for_each_child_node_scoped(&pdev
->dev
, child
) {
891 if (!st
->info
->has_child_nodes
)
892 return dev_err_probe(&pdev
->dev
, -EINVAL
,
893 "invalid fdt axi-dac compatible.");
895 /* Processing only reg 0 node */
896 ret
= fwnode_property_read_u32(child
, "reg", &val
);
898 return dev_err_probe(&pdev
->dev
, ret
,
899 "invalid reg property.");
901 return dev_err_probe(&pdev
->dev
, -EINVAL
,
902 "invalid node address.");
904 ret
= axi_dac_create_platform_device(st
, child
);
906 return dev_err_probe(&pdev
->dev
, -EINVAL
,
907 "cannot create device.");
910 dev_info(&pdev
->dev
, "AXI DAC IP core (%d.%.2d.%c) probed\n",
911 ADI_AXI_PCORE_VER_MAJOR(ver
),
912 ADI_AXI_PCORE_VER_MINOR(ver
),
913 ADI_AXI_PCORE_VER_PATCH(ver
));
918 static const struct axi_dac_info dac_generic
= {
919 .version
= ADI_AXI_PCORE_VER(9, 1, 'b'),
920 .backend_info
= &axi_dac_generic
,
923 static const struct axi_dac_info dac_ad3552r
= {
924 .version
= ADI_AXI_PCORE_VER(9, 1, 'b'),
925 .backend_info
= &axi_ad3552r
,
927 .has_child_nodes
= true,
930 static const struct of_device_id axi_dac_of_match
[] = {
931 { .compatible
= "adi,axi-dac-9.1.b", .data
= &dac_generic
},
932 { .compatible
= "adi,axi-ad3552r", .data
= &dac_ad3552r
},
935 MODULE_DEVICE_TABLE(of
, axi_dac_of_match
);
937 static struct platform_driver axi_dac_driver
= {
939 .name
= "adi-axi-dac",
940 .of_match_table
= axi_dac_of_match
,
942 .probe
= axi_dac_probe
,
944 module_platform_driver(axi_dac_driver
);
946 MODULE_AUTHOR("Nuno Sa <nuno.sa@analog.com>");
947 MODULE_DESCRIPTION("Analog Devices Generic AXI DAC IP core driver");
948 MODULE_LICENSE("GPL");
949 MODULE_IMPORT_NS("IIO_DMAENGINE_BUFFER");
950 MODULE_IMPORT_NS("IIO_BACKEND");