1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
4 * Author: Chris Zhong <zyw@rock-chips.com>
8 #include <linux/device.h>
9 #include <linux/delay.h>
11 #include <linux/iopoll.h>
12 #include <linux/reset.h>
14 #include "cdn-dp-core.h"
15 #include "cdn-dp-reg.h"
17 #define CDN_DP_SPDIF_CLK 200000000
18 #define FW_ALIVE_TIMEOUT_US 1000000
19 #define MAILBOX_RETRY_US 1000
20 #define MAILBOX_TIMEOUT_US 5000000
21 #define LINK_TRAINING_RETRY_MS 20
22 #define LINK_TRAINING_TIMEOUT_MS 500
24 void cdn_dp_set_fw_clk(struct cdn_dp_device
*dp
, unsigned long clk
)
26 writel(clk
/ 1000000, dp
->regs
+ SW_CLK_H
);
29 void cdn_dp_clock_reset(struct cdn_dp_device
*dp
)
33 val
= DPTX_FRMR_DATA_CLK_RSTN_EN
|
34 DPTX_FRMR_DATA_CLK_EN
|
35 DPTX_PHY_DATA_RSTN_EN
|
36 DPTX_PHY_DATA_CLK_EN
|
37 DPTX_PHY_CHAR_RSTN_EN
|
38 DPTX_PHY_CHAR_CLK_EN
|
39 SOURCE_AUX_SYS_CLK_RSTN_EN
|
40 SOURCE_AUX_SYS_CLK_EN
|
41 DPTX_SYS_CLK_RSTN_EN
|
43 CFG_DPTX_VIF_CLK_RSTN_EN
|
45 writel(val
, dp
->regs
+ SOURCE_DPTX_CAR
);
47 val
= SOURCE_PHY_RSTN_EN
| SOURCE_PHY_CLK_EN
;
48 writel(val
, dp
->regs
+ SOURCE_PHY_CAR
);
50 val
= SOURCE_PKT_SYS_RSTN_EN
|
51 SOURCE_PKT_SYS_CLK_EN
|
52 SOURCE_PKT_DATA_RSTN_EN
|
53 SOURCE_PKT_DATA_CLK_EN
;
54 writel(val
, dp
->regs
+ SOURCE_PKT_CAR
);
56 val
= SPDIF_CDR_CLK_RSTN_EN
|
58 SOURCE_AIF_SYS_RSTN_EN
|
59 SOURCE_AIF_SYS_CLK_EN
|
60 SOURCE_AIF_CLK_RSTN_EN
|
62 writel(val
, dp
->regs
+ SOURCE_AIF_CAR
);
64 val
= SOURCE_CIPHER_SYSTEM_CLK_RSTN_EN
|
65 SOURCE_CIPHER_SYS_CLK_EN
|
66 SOURCE_CIPHER_CHAR_CLK_RSTN_EN
|
67 SOURCE_CIPHER_CHAR_CLK_EN
;
68 writel(val
, dp
->regs
+ SOURCE_CIPHER_CAR
);
70 val
= SOURCE_CRYPTO_SYS_CLK_RSTN_EN
|
71 SOURCE_CRYPTO_SYS_CLK_EN
;
72 writel(val
, dp
->regs
+ SOURCE_CRYPTO_CAR
);
74 /* enable Mailbox and PIF interrupt */
75 writel(0, dp
->regs
+ APB_INT_MASK
);
78 static int cdn_dp_mailbox_read(struct cdn_dp_device
*dp
)
82 ret
= readx_poll_timeout(readl
, dp
->regs
+ MAILBOX_EMPTY_ADDR
,
83 val
, !val
, MAILBOX_RETRY_US
,
88 return readl(dp
->regs
+ MAILBOX0_RD_DATA
) & 0xff;
91 static int cdp_dp_mailbox_write(struct cdn_dp_device
*dp
, u8 val
)
95 ret
= readx_poll_timeout(readl
, dp
->regs
+ MAILBOX_FULL_ADDR
,
96 full
, !full
, MAILBOX_RETRY_US
,
101 writel(val
, dp
->regs
+ MAILBOX0_WR_DATA
);
106 static int cdn_dp_mailbox_validate_receive(struct cdn_dp_device
*dp
,
107 u8 module_id
, u8 opcode
,
114 /* read the header of the message */
115 for (i
= 0; i
< 4; i
++) {
116 ret
= cdn_dp_mailbox_read(dp
);
123 mbox_size
= (header
[2] << 8) | header
[3];
125 if (opcode
!= header
[0] || module_id
!= header
[1] ||
126 req_size
!= mbox_size
) {
128 * If the message in mailbox is not what we want, we need to
129 * clear the mailbox by reading its contents.
131 for (i
= 0; i
< mbox_size
; i
++)
132 if (cdn_dp_mailbox_read(dp
) < 0)
141 static int cdn_dp_mailbox_read_receive(struct cdn_dp_device
*dp
,
142 u8
*buff
, u16 buff_size
)
147 for (i
= 0; i
< buff_size
; i
++) {
148 ret
= cdn_dp_mailbox_read(dp
);
158 static int cdn_dp_mailbox_send(struct cdn_dp_device
*dp
, u8 module_id
,
159 u8 opcode
, u16 size
, u8
*message
)
165 header
[1] = module_id
;
166 header
[2] = (size
>> 8) & 0xff;
167 header
[3] = size
& 0xff;
169 for (i
= 0; i
< 4; i
++) {
170 ret
= cdp_dp_mailbox_write(dp
, header
[i
]);
175 for (i
= 0; i
< size
; i
++) {
176 ret
= cdp_dp_mailbox_write(dp
, message
[i
]);
184 static int cdn_dp_reg_write(struct cdn_dp_device
*dp
, u16 addr
, u32 val
)
188 msg
[0] = (addr
>> 8) & 0xff;
189 msg
[1] = addr
& 0xff;
190 msg
[2] = (val
>> 24) & 0xff;
191 msg
[3] = (val
>> 16) & 0xff;
192 msg
[4] = (val
>> 8) & 0xff;
194 return cdn_dp_mailbox_send(dp
, MB_MODULE_ID_DP_TX
, DPTX_WRITE_REGISTER
,
198 static int cdn_dp_reg_write_bit(struct cdn_dp_device
*dp
, u16 addr
,
199 u8 start_bit
, u8 bits_no
, u32 val
)
203 field
[0] = (addr
>> 8) & 0xff;
204 field
[1] = addr
& 0xff;
205 field
[2] = start_bit
;
207 field
[4] = (val
>> 24) & 0xff;
208 field
[5] = (val
>> 16) & 0xff;
209 field
[6] = (val
>> 8) & 0xff;
210 field
[7] = val
& 0xff;
212 return cdn_dp_mailbox_send(dp
, MB_MODULE_ID_DP_TX
, DPTX_WRITE_FIELD
,
213 sizeof(field
), field
);
216 int cdn_dp_dpcd_read(struct cdn_dp_device
*dp
, u32 addr
, u8
*data
, u16 len
)
221 msg
[0] = (len
>> 8) & 0xff;
223 msg
[2] = (addr
>> 16) & 0xff;
224 msg
[3] = (addr
>> 8) & 0xff;
225 msg
[4] = addr
& 0xff;
226 ret
= cdn_dp_mailbox_send(dp
, MB_MODULE_ID_DP_TX
, DPTX_READ_DPCD
,
231 ret
= cdn_dp_mailbox_validate_receive(dp
, MB_MODULE_ID_DP_TX
,
237 ret
= cdn_dp_mailbox_read_receive(dp
, reg
, sizeof(reg
));
241 ret
= cdn_dp_mailbox_read_receive(dp
, data
, len
);
247 int cdn_dp_dpcd_write(struct cdn_dp_device
*dp
, u32 addr
, u8 value
)
254 msg
[2] = (addr
>> 16) & 0xff;
255 msg
[3] = (addr
>> 8) & 0xff;
256 msg
[4] = addr
& 0xff;
258 ret
= cdn_dp_mailbox_send(dp
, MB_MODULE_ID_DP_TX
, DPTX_WRITE_DPCD
,
263 ret
= cdn_dp_mailbox_validate_receive(dp
, MB_MODULE_ID_DP_TX
,
264 DPTX_WRITE_DPCD
, sizeof(reg
));
268 ret
= cdn_dp_mailbox_read_receive(dp
, reg
, sizeof(reg
));
272 if (addr
!= (reg
[2] << 16 | reg
[3] << 8 | reg
[4]))
277 DRM_DEV_ERROR(dp
->dev
, "dpcd write failed: %d\n", ret
);
281 int cdn_dp_load_firmware(struct cdn_dp_device
*dp
, const u32
*i_mem
,
282 u32 i_size
, const u32
*d_mem
, u32 d_size
)
287 /* reset ucpu before load firmware*/
288 writel(APB_IRAM_PATH
| APB_DRAM_PATH
| APB_XT_RESET
,
289 dp
->regs
+ APB_CTRL
);
291 for (i
= 0; i
< i_size
; i
+= 4)
292 writel(*i_mem
++, dp
->regs
+ ADDR_IMEM
+ i
);
294 for (i
= 0; i
< d_size
; i
+= 4)
295 writel(*d_mem
++, dp
->regs
+ ADDR_DMEM
+ i
);
298 writel(0, dp
->regs
+ APB_CTRL
);
300 /* check the keep alive register to make sure fw working */
301 ret
= readx_poll_timeout(readl
, dp
->regs
+ KEEP_ALIVE
,
302 reg
, reg
, 2000, FW_ALIVE_TIMEOUT_US
);
304 DRM_DEV_ERROR(dp
->dev
, "failed to loaded the FW reg = %x\n",
309 reg
= readl(dp
->regs
+ VER_L
) & 0xff;
310 dp
->fw_version
= reg
;
311 reg
= readl(dp
->regs
+ VER_H
) & 0xff;
312 dp
->fw_version
|= reg
<< 8;
313 reg
= readl(dp
->regs
+ VER_LIB_L_ADDR
) & 0xff;
314 dp
->fw_version
|= reg
<< 16;
315 reg
= readl(dp
->regs
+ VER_LIB_H_ADDR
) & 0xff;
316 dp
->fw_version
|= reg
<< 24;
318 DRM_DEV_DEBUG(dp
->dev
, "firmware version: %x\n", dp
->fw_version
);
323 int cdn_dp_set_firmware_active(struct cdn_dp_device
*dp
, bool enable
)
328 msg
[0] = GENERAL_MAIN_CONTROL
;
329 msg
[1] = MB_MODULE_ID_GENERAL
;
332 msg
[4] = enable
? FW_ACTIVE
: FW_STANDBY
;
334 for (i
= 0; i
< sizeof(msg
); i
++) {
335 ret
= cdp_dp_mailbox_write(dp
, msg
[i
]);
337 goto err_set_firmware_active
;
340 /* read the firmware state */
341 for (i
= 0; i
< sizeof(msg
); i
++) {
342 ret
= cdn_dp_mailbox_read(dp
);
344 goto err_set_firmware_active
;
351 err_set_firmware_active
:
353 DRM_DEV_ERROR(dp
->dev
, "set firmware active failed\n");
357 int cdn_dp_set_host_cap(struct cdn_dp_device
*dp
, u8 lanes
, bool flip
)
362 msg
[0] = CDN_DP_MAX_LINK_RATE
;
363 msg
[1] = lanes
| SCRAMBLER_EN
;
364 msg
[2] = VOLTAGE_LEVEL_2
;
365 msg
[3] = PRE_EMPHASIS_LEVEL_3
;
366 msg
[4] = PTS1
| PTS2
| PTS3
| PTS4
;
367 msg
[5] = FAST_LT_NOT_SUPPORT
;
368 msg
[6] = flip
? LANE_MAPPING_FLIPPED
: LANE_MAPPING_NORMAL
;
371 ret
= cdn_dp_mailbox_send(dp
, MB_MODULE_ID_DP_TX
,
372 DPTX_SET_HOST_CAPABILITIES
,
375 goto err_set_host_cap
;
377 ret
= cdn_dp_reg_write(dp
, DP_AUX_SWAP_INVERSION_CONTROL
,
382 DRM_DEV_ERROR(dp
->dev
, "set host cap failed: %d\n", ret
);
386 int cdn_dp_event_config(struct cdn_dp_device
*dp
)
391 memset(msg
, 0, sizeof(msg
));
393 msg
[0] = DPTX_EVENT_ENABLE_HPD
| DPTX_EVENT_ENABLE_TRAINING
;
395 ret
= cdn_dp_mailbox_send(dp
, MB_MODULE_ID_DP_TX
, DPTX_ENABLE_EVENT
,
398 DRM_DEV_ERROR(dp
->dev
, "set event config failed: %d\n", ret
);
403 u32
cdn_dp_get_event(struct cdn_dp_device
*dp
)
405 return readl(dp
->regs
+ SW_EVENTS0
);
408 int cdn_dp_get_hpd_status(struct cdn_dp_device
*dp
)
413 ret
= cdn_dp_mailbox_send(dp
, MB_MODULE_ID_DP_TX
, DPTX_HPD_STATE
,
418 ret
= cdn_dp_mailbox_validate_receive(dp
, MB_MODULE_ID_DP_TX
,
419 DPTX_HPD_STATE
, sizeof(status
));
423 ret
= cdn_dp_mailbox_read_receive(dp
, &status
, sizeof(status
));
430 DRM_DEV_ERROR(dp
->dev
, "get hpd status failed: %d\n", ret
);
434 int cdn_dp_get_edid_block(void *data
, u8
*edid
,
435 unsigned int block
, size_t length
)
437 struct cdn_dp_device
*dp
= data
;
438 u8 msg
[2], reg
[2], i
;
441 for (i
= 0; i
< 4; i
++) {
445 ret
= cdn_dp_mailbox_send(dp
, MB_MODULE_ID_DP_TX
, DPTX_GET_EDID
,
450 ret
= cdn_dp_mailbox_validate_receive(dp
, MB_MODULE_ID_DP_TX
,
452 sizeof(reg
) + length
);
456 ret
= cdn_dp_mailbox_read_receive(dp
, reg
, sizeof(reg
));
460 ret
= cdn_dp_mailbox_read_receive(dp
, edid
, length
);
464 if (reg
[0] == length
&& reg
[1] == block
/ 2)
469 DRM_DEV_ERROR(dp
->dev
, "get block[%d] edid failed: %d\n", block
,
475 static int cdn_dp_training_start(struct cdn_dp_device
*dp
)
477 unsigned long timeout
;
481 msg
= LINK_TRAINING_RUN
;
484 ret
= cdn_dp_mailbox_send(dp
, MB_MODULE_ID_DP_TX
, DPTX_TRAINING_CONTROL
,
487 goto err_training_start
;
489 timeout
= jiffies
+ msecs_to_jiffies(LINK_TRAINING_TIMEOUT_MS
);
490 while (time_before(jiffies
, timeout
)) {
491 msleep(LINK_TRAINING_RETRY_MS
);
492 ret
= cdn_dp_mailbox_send(dp
, MB_MODULE_ID_DP_TX
,
493 DPTX_READ_EVENT
, 0, NULL
);
495 goto err_training_start
;
497 ret
= cdn_dp_mailbox_validate_receive(dp
, MB_MODULE_ID_DP_TX
,
501 goto err_training_start
;
503 ret
= cdn_dp_mailbox_read_receive(dp
, event
, sizeof(event
));
505 goto err_training_start
;
507 if (event
[1] & EQ_PHASE_FINISHED
)
514 DRM_DEV_ERROR(dp
->dev
, "training failed: %d\n", ret
);
518 static int cdn_dp_get_training_status(struct cdn_dp_device
*dp
)
523 ret
= cdn_dp_mailbox_send(dp
, MB_MODULE_ID_DP_TX
, DPTX_READ_LINK_STAT
,
526 goto err_get_training_status
;
528 ret
= cdn_dp_mailbox_validate_receive(dp
, MB_MODULE_ID_DP_TX
,
532 goto err_get_training_status
;
534 ret
= cdn_dp_mailbox_read_receive(dp
, status
, sizeof(status
));
536 goto err_get_training_status
;
538 dp
->max_rate
= drm_dp_bw_code_to_link_rate(status
[0]);
539 dp
->max_lanes
= status
[1];
541 err_get_training_status
:
543 DRM_DEV_ERROR(dp
->dev
, "get training status failed: %d\n", ret
);
547 int cdn_dp_train_link(struct cdn_dp_device
*dp
)
551 ret
= cdn_dp_training_start(dp
);
553 DRM_DEV_ERROR(dp
->dev
, "Failed to start training %d\n", ret
);
557 ret
= cdn_dp_get_training_status(dp
);
559 DRM_DEV_ERROR(dp
->dev
, "Failed to get training stat %d\n", ret
);
563 DRM_DEV_DEBUG_KMS(dp
->dev
, "rate:0x%x, lanes:%d\n", dp
->max_rate
,
568 int cdn_dp_set_video_status(struct cdn_dp_device
*dp
, int active
)
575 ret
= cdn_dp_mailbox_send(dp
, MB_MODULE_ID_DP_TX
, DPTX_SET_VIDEO
,
578 DRM_DEV_ERROR(dp
->dev
, "set video status failed: %d\n", ret
);
583 static int cdn_dp_get_msa_misc(struct video_info
*video
,
584 struct drm_display_mode
*mode
)
589 switch (video
->color_fmt
) {
594 /* set YUV default color space conversion to BT601 */
596 val
[0] = 6 + BT_601
* 8;
599 val
[0] = 5 + BT_601
* 8;
606 switch (video
->color_depth
) {
624 msa_misc
= 2 * val
[0] + 32 * val
[1] +
625 ((video
->color_fmt
== Y_ONLY
) ? (1 << 14) : 0);
630 int cdn_dp_config_video(struct cdn_dp_device
*dp
)
632 struct video_info
*video
= &dp
->video_info
;
633 struct drm_display_mode
*mode
= &dp
->mode
;
635 u32 val
, link_rate
, rem
;
636 u8 bit_per_pix
, tu_size_reg
= TU_SIZE
;
639 bit_per_pix
= (video
->color_fmt
== YCBCR_4_2_2
) ?
640 (video
->color_depth
* 2) : (video
->color_depth
* 3);
642 link_rate
= dp
->max_rate
/ 1000;
644 ret
= cdn_dp_reg_write(dp
, BND_HSYNC2VSYNC
, VIF_BYPASS_INTERLACE
);
646 goto err_config_video
;
648 ret
= cdn_dp_reg_write(dp
, HSYNC2VSYNC_POL_CTRL
, 0);
650 goto err_config_video
;
653 * get a best tu_size and valid symbol:
654 * 1. chose Lclk freq(162Mhz, 270Mhz, 540Mhz), set TU to 32
655 * 2. calculate VS(valid symbol) = TU * Pclk * Bpp / (Lclk * Lanes)
656 * 3. if VS > *.85 or VS < *.1 or VS < 2 or TU < VS + 4, then set
657 * TU += 2 and repeat 2nd step.
661 symbol
= tu_size_reg
* mode
->clock
* bit_per_pix
;
662 do_div(symbol
, dp
->max_lanes
* link_rate
* 8);
663 rem
= do_div(symbol
, 1000);
664 if (tu_size_reg
> 64) {
666 DRM_DEV_ERROR(dp
->dev
,
667 "tu error, clk:%d, lanes:%d, rate:%d\n",
668 mode
->clock
, dp
->max_lanes
, link_rate
);
669 goto err_config_video
;
671 } while ((symbol
<= 1) || (tu_size_reg
- symbol
< 4) ||
672 (rem
> 850) || (rem
< 100));
674 val
= symbol
+ (tu_size_reg
<< 8);
675 val
|= TU_CNT_RST_EN
;
676 ret
= cdn_dp_reg_write(dp
, DP_FRAMER_TU
, val
);
678 goto err_config_video
;
680 /* set the FIFO Buffer size */
681 val
= div_u64(mode
->clock
* (symbol
+ 1), 1000) + link_rate
;
682 val
/= (dp
->max_lanes
* link_rate
);
683 val
= div_u64(8 * (symbol
+ 1), bit_per_pix
) - val
;
685 ret
= cdn_dp_reg_write(dp
, DP_VC_TABLE(15), val
);
687 switch (video
->color_depth
) {
705 val
+= video
->color_fmt
<< 8;
706 ret
= cdn_dp_reg_write(dp
, DP_FRAMER_PXL_REPR
, val
);
708 goto err_config_video
;
710 val
= video
->h_sync_polarity
? DP_FRAMER_SP_HSP
: 0;
711 val
|= video
->v_sync_polarity
? DP_FRAMER_SP_VSP
: 0;
712 ret
= cdn_dp_reg_write(dp
, DP_FRAMER_SP
, val
);
714 goto err_config_video
;
716 val
= (mode
->hsync_start
- mode
->hdisplay
) << 16;
717 val
|= mode
->htotal
- mode
->hsync_end
;
718 ret
= cdn_dp_reg_write(dp
, DP_FRONT_BACK_PORCH
, val
);
720 goto err_config_video
;
722 val
= mode
->hdisplay
* bit_per_pix
/ 8;
723 ret
= cdn_dp_reg_write(dp
, DP_BYTE_COUNT
, val
);
725 goto err_config_video
;
727 val
= mode
->htotal
| ((mode
->htotal
- mode
->hsync_start
) << 16);
728 ret
= cdn_dp_reg_write(dp
, MSA_HORIZONTAL_0
, val
);
730 goto err_config_video
;
732 val
= mode
->hsync_end
- mode
->hsync_start
;
733 val
|= (mode
->hdisplay
<< 16) | (video
->h_sync_polarity
<< 15);
734 ret
= cdn_dp_reg_write(dp
, MSA_HORIZONTAL_1
, val
);
736 goto err_config_video
;
739 val
|= (mode
->vtotal
- mode
->vsync_start
) << 16;
740 ret
= cdn_dp_reg_write(dp
, MSA_VERTICAL_0
, val
);
742 goto err_config_video
;
744 val
= mode
->vsync_end
- mode
->vsync_start
;
745 val
|= (mode
->vdisplay
<< 16) | (video
->v_sync_polarity
<< 15);
746 ret
= cdn_dp_reg_write(dp
, MSA_VERTICAL_1
, val
);
748 goto err_config_video
;
750 val
= cdn_dp_get_msa_misc(video
, mode
);
751 ret
= cdn_dp_reg_write(dp
, MSA_MISC
, val
);
753 goto err_config_video
;
755 ret
= cdn_dp_reg_write(dp
, STREAM_CONFIG
, 1);
757 goto err_config_video
;
759 val
= mode
->hsync_end
- mode
->hsync_start
;
760 val
|= mode
->hdisplay
<< 16;
761 ret
= cdn_dp_reg_write(dp
, DP_HORIZONTAL
, val
);
763 goto err_config_video
;
765 val
= mode
->vdisplay
;
766 val
|= (mode
->vtotal
- mode
->vsync_start
) << 16;
767 ret
= cdn_dp_reg_write(dp
, DP_VERTICAL_0
, val
);
769 goto err_config_video
;
772 ret
= cdn_dp_reg_write(dp
, DP_VERTICAL_1
, val
);
774 goto err_config_video
;
776 ret
= cdn_dp_reg_write_bit(dp
, DP_VB_ID
, 2, 1, 0);
780 DRM_DEV_ERROR(dp
->dev
, "config video failed: %d\n", ret
);
784 int cdn_dp_audio_stop(struct cdn_dp_device
*dp
, struct audio_info
*audio
)
788 ret
= cdn_dp_reg_write(dp
, AUDIO_PACK_CONTROL
, 0);
790 DRM_DEV_ERROR(dp
->dev
, "audio stop failed: %d\n", ret
);
794 writel(0, dp
->regs
+ SPDIF_CTRL_ADDR
);
796 /* clearn the audio config and reset */
797 writel(0, dp
->regs
+ AUDIO_SRC_CNTL
);
798 writel(0, dp
->regs
+ AUDIO_SRC_CNFG
);
799 writel(AUDIO_SW_RST
, dp
->regs
+ AUDIO_SRC_CNTL
);
800 writel(0, dp
->regs
+ AUDIO_SRC_CNTL
);
802 /* reset smpl2pckt component */
803 writel(0, dp
->regs
+ SMPL2PKT_CNTL
);
804 writel(AUDIO_SW_RST
, dp
->regs
+ SMPL2PKT_CNTL
);
805 writel(0, dp
->regs
+ SMPL2PKT_CNTL
);
808 writel(AUDIO_SW_RST
, dp
->regs
+ FIFO_CNTL
);
809 writel(0, dp
->regs
+ FIFO_CNTL
);
811 if (audio
->format
== AFMT_SPDIF
)
812 clk_disable_unprepare(dp
->spdif_clk
);
817 int cdn_dp_audio_mute(struct cdn_dp_device
*dp
, bool enable
)
821 ret
= cdn_dp_reg_write_bit(dp
, DP_VB_ID
, 4, 1, enable
);
823 DRM_DEV_ERROR(dp
->dev
, "audio mute failed: %d\n", ret
);
828 static void cdn_dp_audio_config_i2s(struct cdn_dp_device
*dp
,
829 struct audio_info
*audio
)
831 int sub_pckt_num
= 1, i2s_port_en_val
= 0xf, i
;
834 if (audio
->channels
== 2) {
835 if (dp
->max_lanes
== 1)
841 } else if (audio
->channels
== 4) {
845 writel(0x0, dp
->regs
+ SPDIF_CTRL_ADDR
);
847 writel(SYNC_WR_TO_CH_ZERO
, dp
->regs
+ FIFO_CNTL
);
849 val
= MAX_NUM_CH(audio
->channels
);
850 val
|= NUM_OF_I2S_PORTS(audio
->channels
);
851 val
|= AUDIO_TYPE_LPCM
;
852 val
|= CFG_SUB_PCKT_NUM(sub_pckt_num
);
853 writel(val
, dp
->regs
+ SMPL2PKT_CNFG
);
855 if (audio
->sample_width
== 16)
857 else if (audio
->sample_width
== 24)
862 val
|= AUDIO_CH_NUM(audio
->channels
);
863 val
|= I2S_DEC_PORT_EN(i2s_port_en_val
);
864 val
|= TRANS_SMPL_WIDTH_32
;
865 writel(val
, dp
->regs
+ AUDIO_SRC_CNFG
);
867 for (i
= 0; i
< (audio
->channels
+ 1) / 2; i
++) {
868 if (audio
->sample_width
== 16)
869 val
= (0x02 << 8) | (0x02 << 20);
870 else if (audio
->sample_width
== 24)
871 val
= (0x0b << 8) | (0x0b << 20);
873 val
|= ((2 * i
) << 4) | ((2 * i
+ 1) << 16);
874 writel(val
, dp
->regs
+ STTS_BIT_CH(i
));
877 switch (audio
->sample_rate
) {
879 val
= SAMPLING_FREQ(3) |
880 ORIGINAL_SAMP_FREQ(0xc);
883 val
= SAMPLING_FREQ(0) |
884 ORIGINAL_SAMP_FREQ(0xf);
887 val
= SAMPLING_FREQ(2) |
888 ORIGINAL_SAMP_FREQ(0xd);
891 val
= SAMPLING_FREQ(8) |
892 ORIGINAL_SAMP_FREQ(0x7);
895 val
= SAMPLING_FREQ(0xa) |
896 ORIGINAL_SAMP_FREQ(5);
899 val
= SAMPLING_FREQ(0xc) |
900 ORIGINAL_SAMP_FREQ(3);
903 val
= SAMPLING_FREQ(0xe) |
904 ORIGINAL_SAMP_FREQ(1);
908 writel(val
, dp
->regs
+ COM_CH_STTS_BITS
);
910 writel(SMPL2PKT_EN
, dp
->regs
+ SMPL2PKT_CNTL
);
911 writel(I2S_DEC_START
, dp
->regs
+ AUDIO_SRC_CNTL
);
914 static void cdn_dp_audio_config_spdif(struct cdn_dp_device
*dp
)
918 writel(SYNC_WR_TO_CH_ZERO
, dp
->regs
+ FIFO_CNTL
);
920 val
= MAX_NUM_CH(2) | AUDIO_TYPE_LPCM
| CFG_SUB_PCKT_NUM(4);
921 writel(val
, dp
->regs
+ SMPL2PKT_CNFG
);
922 writel(SMPL2PKT_EN
, dp
->regs
+ SMPL2PKT_CNTL
);
924 val
= SPDIF_ENABLE
| SPDIF_AVG_SEL
| SPDIF_JITTER_BYPASS
;
925 writel(val
, dp
->regs
+ SPDIF_CTRL_ADDR
);
927 clk_prepare_enable(dp
->spdif_clk
);
928 clk_set_rate(dp
->spdif_clk
, CDN_DP_SPDIF_CLK
);
931 int cdn_dp_audio_config(struct cdn_dp_device
*dp
, struct audio_info
*audio
)
935 /* reset the spdif clk before config */
936 if (audio
->format
== AFMT_SPDIF
) {
937 reset_control_assert(dp
->spdif_rst
);
938 reset_control_deassert(dp
->spdif_rst
);
941 ret
= cdn_dp_reg_write(dp
, CM_LANE_CTRL
, LANE_REF_CYC
);
943 goto err_audio_config
;
945 ret
= cdn_dp_reg_write(dp
, CM_CTRL
, 0);
947 goto err_audio_config
;
949 if (audio
->format
== AFMT_I2S
)
950 cdn_dp_audio_config_i2s(dp
, audio
);
951 else if (audio
->format
== AFMT_SPDIF
)
952 cdn_dp_audio_config_spdif(dp
);
954 ret
= cdn_dp_reg_write(dp
, AUDIO_PACK_CONTROL
, AUDIO_PACK_EN
);
958 DRM_DEV_ERROR(dp
->dev
, "audio config failed: %d\n", ret
);