2 * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
3 * Author: Chris Zhong <zyw@rock-chips.com>
5 * This software is licensed under the terms of the GNU General Public
6 * License version 2, as published by the Free Software Foundation, and
7 * may be copied, distributed, and modified under those terms.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <linux/clk.h>
16 #include <linux/device.h>
17 #include <linux/delay.h>
19 #include <linux/iopoll.h>
20 #include <linux/reset.h>
22 #include "cdn-dp-core.h"
23 #include "cdn-dp-reg.h"
25 #define CDN_DP_SPDIF_CLK 200000000
26 #define FW_ALIVE_TIMEOUT_US 1000000
27 #define MAILBOX_RETRY_US 1000
28 #define MAILBOX_TIMEOUT_US 5000000
29 #define LINK_TRAINING_RETRY_MS 20
30 #define LINK_TRAINING_TIMEOUT_MS 500
32 void cdn_dp_set_fw_clk(struct cdn_dp_device
*dp
, unsigned long clk
)
34 writel(clk
/ 1000000, dp
->regs
+ SW_CLK_H
);
37 void cdn_dp_clock_reset(struct cdn_dp_device
*dp
)
41 val
= DPTX_FRMR_DATA_CLK_RSTN_EN
|
42 DPTX_FRMR_DATA_CLK_EN
|
43 DPTX_PHY_DATA_RSTN_EN
|
44 DPTX_PHY_DATA_CLK_EN
|
45 DPTX_PHY_CHAR_RSTN_EN
|
46 DPTX_PHY_CHAR_CLK_EN
|
47 SOURCE_AUX_SYS_CLK_RSTN_EN
|
48 SOURCE_AUX_SYS_CLK_EN
|
49 DPTX_SYS_CLK_RSTN_EN
|
51 CFG_DPTX_VIF_CLK_RSTN_EN
|
53 writel(val
, dp
->regs
+ SOURCE_DPTX_CAR
);
55 val
= SOURCE_PHY_RSTN_EN
| SOURCE_PHY_CLK_EN
;
56 writel(val
, dp
->regs
+ SOURCE_PHY_CAR
);
58 val
= SOURCE_PKT_SYS_RSTN_EN
|
59 SOURCE_PKT_SYS_CLK_EN
|
60 SOURCE_PKT_DATA_RSTN_EN
|
61 SOURCE_PKT_DATA_CLK_EN
;
62 writel(val
, dp
->regs
+ SOURCE_PKT_CAR
);
64 val
= SPDIF_CDR_CLK_RSTN_EN
|
66 SOURCE_AIF_SYS_RSTN_EN
|
67 SOURCE_AIF_SYS_CLK_EN
|
68 SOURCE_AIF_CLK_RSTN_EN
|
70 writel(val
, dp
->regs
+ SOURCE_AIF_CAR
);
72 val
= SOURCE_CIPHER_SYSTEM_CLK_RSTN_EN
|
73 SOURCE_CIPHER_SYS_CLK_EN
|
74 SOURCE_CIPHER_CHAR_CLK_RSTN_EN
|
75 SOURCE_CIPHER_CHAR_CLK_EN
;
76 writel(val
, dp
->regs
+ SOURCE_CIPHER_CAR
);
78 val
= SOURCE_CRYPTO_SYS_CLK_RSTN_EN
|
79 SOURCE_CRYPTO_SYS_CLK_EN
;
80 writel(val
, dp
->regs
+ SOURCE_CRYPTO_CAR
);
82 /* enable Mailbox and PIF interrupt */
83 writel(0, dp
->regs
+ APB_INT_MASK
);
86 static int cdn_dp_mailbox_read(struct cdn_dp_device
*dp
)
90 ret
= readx_poll_timeout(readl
, dp
->regs
+ MAILBOX_EMPTY_ADDR
,
91 val
, !val
, MAILBOX_RETRY_US
,
96 return readl(dp
->regs
+ MAILBOX0_RD_DATA
) & 0xff;
99 static int cdp_dp_mailbox_write(struct cdn_dp_device
*dp
, u8 val
)
103 ret
= readx_poll_timeout(readl
, dp
->regs
+ MAILBOX_FULL_ADDR
,
104 full
, !full
, MAILBOX_RETRY_US
,
109 writel(val
, dp
->regs
+ MAILBOX0_WR_DATA
);
114 static int cdn_dp_mailbox_validate_receive(struct cdn_dp_device
*dp
,
115 u8 module_id
, u8 opcode
,
122 /* read the header of the message */
123 for (i
= 0; i
< 4; i
++) {
124 ret
= cdn_dp_mailbox_read(dp
);
131 mbox_size
= (header
[2] << 8) | header
[3];
133 if (opcode
!= header
[0] || module_id
!= header
[1] ||
134 req_size
!= mbox_size
) {
136 * If the message in mailbox is not what we want, we need to
137 * clear the mailbox by reading its contents.
139 for (i
= 0; i
< mbox_size
; i
++)
140 if (cdn_dp_mailbox_read(dp
) < 0)
149 static int cdn_dp_mailbox_read_receive(struct cdn_dp_device
*dp
,
150 u8
*buff
, u8 buff_size
)
155 for (i
= 0; i
< buff_size
; i
++) {
156 ret
= cdn_dp_mailbox_read(dp
);
166 static int cdn_dp_mailbox_send(struct cdn_dp_device
*dp
, u8 module_id
,
167 u8 opcode
, u16 size
, u8
*message
)
173 header
[1] = module_id
;
174 header
[2] = (size
>> 8) & 0xff;
175 header
[3] = size
& 0xff;
177 for (i
= 0; i
< 4; i
++) {
178 ret
= cdp_dp_mailbox_write(dp
, header
[i
]);
183 for (i
= 0; i
< size
; i
++) {
184 ret
= cdp_dp_mailbox_write(dp
, message
[i
]);
192 static int cdn_dp_reg_write(struct cdn_dp_device
*dp
, u16 addr
, u32 val
)
196 msg
[0] = (addr
>> 8) & 0xff;
197 msg
[1] = addr
& 0xff;
198 msg
[2] = (val
>> 24) & 0xff;
199 msg
[3] = (val
>> 16) & 0xff;
200 msg
[4] = (val
>> 8) & 0xff;
202 return cdn_dp_mailbox_send(dp
, MB_MODULE_ID_DP_TX
, DPTX_WRITE_REGISTER
,
206 static int cdn_dp_reg_write_bit(struct cdn_dp_device
*dp
, u16 addr
,
207 u8 start_bit
, u8 bits_no
, u32 val
)
211 field
[0] = (addr
>> 8) & 0xff;
212 field
[1] = addr
& 0xff;
213 field
[2] = start_bit
;
215 field
[4] = (val
>> 24) & 0xff;
216 field
[5] = (val
>> 16) & 0xff;
217 field
[6] = (val
>> 8) & 0xff;
218 field
[7] = val
& 0xff;
220 return cdn_dp_mailbox_send(dp
, MB_MODULE_ID_DP_TX
, DPTX_WRITE_FIELD
,
221 sizeof(field
), field
);
224 int cdn_dp_dpcd_read(struct cdn_dp_device
*dp
, u32 addr
, u8
*data
, u16 len
)
229 msg
[0] = (len
>> 8) & 0xff;
231 msg
[2] = (addr
>> 16) & 0xff;
232 msg
[3] = (addr
>> 8) & 0xff;
233 msg
[4] = addr
& 0xff;
234 ret
= cdn_dp_mailbox_send(dp
, MB_MODULE_ID_DP_TX
, DPTX_READ_DPCD
,
239 ret
= cdn_dp_mailbox_validate_receive(dp
, MB_MODULE_ID_DP_TX
,
245 ret
= cdn_dp_mailbox_read_receive(dp
, reg
, sizeof(reg
));
249 ret
= cdn_dp_mailbox_read_receive(dp
, data
, len
);
255 int cdn_dp_dpcd_write(struct cdn_dp_device
*dp
, u32 addr
, u8 value
)
262 msg
[2] = (addr
>> 16) & 0xff;
263 msg
[3] = (addr
>> 8) & 0xff;
264 msg
[4] = addr
& 0xff;
266 ret
= cdn_dp_mailbox_send(dp
, MB_MODULE_ID_DP_TX
, DPTX_WRITE_DPCD
,
271 ret
= cdn_dp_mailbox_validate_receive(dp
, MB_MODULE_ID_DP_TX
,
272 DPTX_WRITE_DPCD
, sizeof(reg
));
276 ret
= cdn_dp_mailbox_read_receive(dp
, reg
, sizeof(reg
));
280 if (addr
!= (reg
[2] << 16 | reg
[3] << 8 | reg
[4]))
285 DRM_DEV_ERROR(dp
->dev
, "dpcd write failed: %d\n", ret
);
289 int cdn_dp_load_firmware(struct cdn_dp_device
*dp
, const u32
*i_mem
,
290 u32 i_size
, const u32
*d_mem
, u32 d_size
)
295 /* reset ucpu before load firmware*/
296 writel(APB_IRAM_PATH
| APB_DRAM_PATH
| APB_XT_RESET
,
297 dp
->regs
+ APB_CTRL
);
299 for (i
= 0; i
< i_size
; i
+= 4)
300 writel(*i_mem
++, dp
->regs
+ ADDR_IMEM
+ i
);
302 for (i
= 0; i
< d_size
; i
+= 4)
303 writel(*d_mem
++, dp
->regs
+ ADDR_DMEM
+ i
);
306 writel(0, dp
->regs
+ APB_CTRL
);
308 /* check the keep alive register to make sure fw working */
309 ret
= readx_poll_timeout(readl
, dp
->regs
+ KEEP_ALIVE
,
310 reg
, reg
, 2000, FW_ALIVE_TIMEOUT_US
);
312 DRM_DEV_ERROR(dp
->dev
, "failed to loaded the FW reg = %x\n",
317 reg
= readl(dp
->regs
+ VER_L
) & 0xff;
318 dp
->fw_version
= reg
;
319 reg
= readl(dp
->regs
+ VER_H
) & 0xff;
320 dp
->fw_version
|= reg
<< 8;
321 reg
= readl(dp
->regs
+ VER_LIB_L_ADDR
) & 0xff;
322 dp
->fw_version
|= reg
<< 16;
323 reg
= readl(dp
->regs
+ VER_LIB_H_ADDR
) & 0xff;
324 dp
->fw_version
|= reg
<< 24;
326 DRM_DEV_DEBUG(dp
->dev
, "firmware version: %x\n", dp
->fw_version
);
331 int cdn_dp_set_firmware_active(struct cdn_dp_device
*dp
, bool enable
)
336 msg
[0] = GENERAL_MAIN_CONTROL
;
337 msg
[1] = MB_MODULE_ID_GENERAL
;
340 msg
[4] = enable
? FW_ACTIVE
: FW_STANDBY
;
342 for (i
= 0; i
< sizeof(msg
); i
++) {
343 ret
= cdp_dp_mailbox_write(dp
, msg
[i
]);
345 goto err_set_firmware_active
;
348 /* read the firmware state */
349 for (i
= 0; i
< sizeof(msg
); i
++) {
350 ret
= cdn_dp_mailbox_read(dp
);
352 goto err_set_firmware_active
;
359 err_set_firmware_active
:
361 DRM_DEV_ERROR(dp
->dev
, "set firmware active failed\n");
365 int cdn_dp_set_host_cap(struct cdn_dp_device
*dp
, u8 lanes
, bool flip
)
370 msg
[0] = CDN_DP_MAX_LINK_RATE
;
371 msg
[1] = lanes
| SCRAMBLER_EN
;
372 msg
[2] = VOLTAGE_LEVEL_2
;
373 msg
[3] = PRE_EMPHASIS_LEVEL_3
;
374 msg
[4] = PTS1
| PTS2
| PTS3
| PTS4
;
375 msg
[5] = FAST_LT_NOT_SUPPORT
;
376 msg
[6] = flip
? LANE_MAPPING_FLIPPED
: LANE_MAPPING_NORMAL
;
379 ret
= cdn_dp_mailbox_send(dp
, MB_MODULE_ID_DP_TX
,
380 DPTX_SET_HOST_CAPABILITIES
,
383 goto err_set_host_cap
;
385 ret
= cdn_dp_reg_write(dp
, DP_AUX_SWAP_INVERSION_CONTROL
,
390 DRM_DEV_ERROR(dp
->dev
, "set host cap failed: %d\n", ret
);
394 int cdn_dp_event_config(struct cdn_dp_device
*dp
)
399 memset(msg
, 0, sizeof(msg
));
401 msg
[0] = DPTX_EVENT_ENABLE_HPD
| DPTX_EVENT_ENABLE_TRAINING
;
403 ret
= cdn_dp_mailbox_send(dp
, MB_MODULE_ID_DP_TX
, DPTX_ENABLE_EVENT
,
406 DRM_DEV_ERROR(dp
->dev
, "set event config failed: %d\n", ret
);
411 u32
cdn_dp_get_event(struct cdn_dp_device
*dp
)
413 return readl(dp
->regs
+ SW_EVENTS0
);
416 int cdn_dp_get_hpd_status(struct cdn_dp_device
*dp
)
421 ret
= cdn_dp_mailbox_send(dp
, MB_MODULE_ID_DP_TX
, DPTX_HPD_STATE
,
426 ret
= cdn_dp_mailbox_validate_receive(dp
, MB_MODULE_ID_DP_TX
,
427 DPTX_HPD_STATE
, sizeof(status
));
431 ret
= cdn_dp_mailbox_read_receive(dp
, &status
, sizeof(status
));
438 DRM_DEV_ERROR(dp
->dev
, "get hpd status failed: %d\n", ret
);
442 int cdn_dp_get_edid_block(void *data
, u8
*edid
,
443 unsigned int block
, size_t length
)
445 struct cdn_dp_device
*dp
= data
;
446 u8 msg
[2], reg
[2], i
;
449 for (i
= 0; i
< 4; i
++) {
453 ret
= cdn_dp_mailbox_send(dp
, MB_MODULE_ID_DP_TX
, DPTX_GET_EDID
,
458 ret
= cdn_dp_mailbox_validate_receive(dp
, MB_MODULE_ID_DP_TX
,
460 sizeof(reg
) + length
);
464 ret
= cdn_dp_mailbox_read_receive(dp
, reg
, sizeof(reg
));
468 ret
= cdn_dp_mailbox_read_receive(dp
, edid
, length
);
472 if (reg
[0] == length
&& reg
[1] == block
/ 2)
477 DRM_DEV_ERROR(dp
->dev
, "get block[%d] edid failed: %d\n", block
,
483 static int cdn_dp_training_start(struct cdn_dp_device
*dp
)
485 unsigned long timeout
;
489 msg
= LINK_TRAINING_RUN
;
492 ret
= cdn_dp_mailbox_send(dp
, MB_MODULE_ID_DP_TX
, DPTX_TRAINING_CONTROL
,
495 goto err_training_start
;
497 timeout
= jiffies
+ msecs_to_jiffies(LINK_TRAINING_TIMEOUT_MS
);
498 while (time_before(jiffies
, timeout
)) {
499 msleep(LINK_TRAINING_RETRY_MS
);
500 ret
= cdn_dp_mailbox_send(dp
, MB_MODULE_ID_DP_TX
,
501 DPTX_READ_EVENT
, 0, NULL
);
503 goto err_training_start
;
505 ret
= cdn_dp_mailbox_validate_receive(dp
, MB_MODULE_ID_DP_TX
,
509 goto err_training_start
;
511 ret
= cdn_dp_mailbox_read_receive(dp
, event
, sizeof(event
));
513 goto err_training_start
;
515 if (event
[1] & EQ_PHASE_FINISHED
)
522 DRM_DEV_ERROR(dp
->dev
, "training failed: %d\n", ret
);
526 static int cdn_dp_get_training_status(struct cdn_dp_device
*dp
)
531 ret
= cdn_dp_mailbox_send(dp
, MB_MODULE_ID_DP_TX
, DPTX_READ_LINK_STAT
,
534 goto err_get_training_status
;
536 ret
= cdn_dp_mailbox_validate_receive(dp
, MB_MODULE_ID_DP_TX
,
540 goto err_get_training_status
;
542 ret
= cdn_dp_mailbox_read_receive(dp
, status
, sizeof(status
));
544 goto err_get_training_status
;
546 dp
->link
.rate
= status
[0];
547 dp
->link
.num_lanes
= status
[1];
549 err_get_training_status
:
551 DRM_DEV_ERROR(dp
->dev
, "get training status failed: %d\n", ret
);
555 int cdn_dp_train_link(struct cdn_dp_device
*dp
)
559 ret
= cdn_dp_training_start(dp
);
561 DRM_DEV_ERROR(dp
->dev
, "Failed to start training %d\n", ret
);
565 ret
= cdn_dp_get_training_status(dp
);
567 DRM_DEV_ERROR(dp
->dev
, "Failed to get training stat %d\n", ret
);
571 DRM_DEV_DEBUG_KMS(dp
->dev
, "rate:0x%x, lanes:%d\n", dp
->link
.rate
,
576 int cdn_dp_set_video_status(struct cdn_dp_device
*dp
, int active
)
583 ret
= cdn_dp_mailbox_send(dp
, MB_MODULE_ID_DP_TX
, DPTX_SET_VIDEO
,
586 DRM_DEV_ERROR(dp
->dev
, "set video status failed: %d\n", ret
);
591 static int cdn_dp_get_msa_misc(struct video_info
*video
,
592 struct drm_display_mode
*mode
)
597 switch (video
->color_fmt
) {
602 /* set YUV default color space conversion to BT601 */
604 val
[0] = 6 + BT_601
* 8;
607 val
[0] = 5 + BT_601
* 8;
614 switch (video
->color_depth
) {
632 msa_misc
= 2 * val
[0] + 32 * val
[1] +
633 ((video
->color_fmt
== Y_ONLY
) ? (1 << 14) : 0);
638 int cdn_dp_config_video(struct cdn_dp_device
*dp
)
640 struct video_info
*video
= &dp
->video_info
;
641 struct drm_display_mode
*mode
= &dp
->mode
;
643 u32 val
, link_rate
, rem
;
644 u8 bit_per_pix
, tu_size_reg
= TU_SIZE
;
647 bit_per_pix
= (video
->color_fmt
== YCBCR_4_2_2
) ?
648 (video
->color_depth
* 2) : (video
->color_depth
* 3);
650 link_rate
= drm_dp_bw_code_to_link_rate(dp
->link
.rate
) / 1000;
652 ret
= cdn_dp_reg_write(dp
, BND_HSYNC2VSYNC
, VIF_BYPASS_INTERLACE
);
654 goto err_config_video
;
656 ret
= cdn_dp_reg_write(dp
, HSYNC2VSYNC_POL_CTRL
, 0);
658 goto err_config_video
;
661 * get a best tu_size and valid symbol:
662 * 1. chose Lclk freq(162Mhz, 270Mhz, 540Mhz), set TU to 32
663 * 2. calculate VS(valid symbol) = TU * Pclk * Bpp / (Lclk * Lanes)
664 * 3. if VS > *.85 or VS < *.1 or VS < 2 or TU < VS + 4, then set
665 * TU += 2 and repeat 2nd step.
669 symbol
= tu_size_reg
* mode
->clock
* bit_per_pix
;
670 do_div(symbol
, dp
->link
.num_lanes
* link_rate
* 8);
671 rem
= do_div(symbol
, 1000);
672 if (tu_size_reg
> 64) {
674 DRM_DEV_ERROR(dp
->dev
,
675 "tu error, clk:%d, lanes:%d, rate:%d\n",
676 mode
->clock
, dp
->link
.num_lanes
,
678 goto err_config_video
;
680 } while ((symbol
<= 1) || (tu_size_reg
- symbol
< 4) ||
681 (rem
> 850) || (rem
< 100));
683 val
= symbol
+ (tu_size_reg
<< 8);
684 val
|= TU_CNT_RST_EN
;
685 ret
= cdn_dp_reg_write(dp
, DP_FRAMER_TU
, val
);
687 goto err_config_video
;
689 /* set the FIFO Buffer size */
690 val
= div_u64(mode
->clock
* (symbol
+ 1), 1000) + link_rate
;
691 val
/= (dp
->link
.num_lanes
* link_rate
);
692 val
= div_u64(8 * (symbol
+ 1), bit_per_pix
) - val
;
694 ret
= cdn_dp_reg_write(dp
, DP_VC_TABLE(15), val
);
696 switch (video
->color_depth
) {
714 val
+= video
->color_fmt
<< 8;
715 ret
= cdn_dp_reg_write(dp
, DP_FRAMER_PXL_REPR
, val
);
717 goto err_config_video
;
719 val
= video
->h_sync_polarity
? DP_FRAMER_SP_HSP
: 0;
720 val
|= video
->v_sync_polarity
? DP_FRAMER_SP_VSP
: 0;
721 ret
= cdn_dp_reg_write(dp
, DP_FRAMER_SP
, val
);
723 goto err_config_video
;
725 val
= (mode
->hsync_start
- mode
->hdisplay
) << 16;
726 val
|= mode
->htotal
- mode
->hsync_end
;
727 ret
= cdn_dp_reg_write(dp
, DP_FRONT_BACK_PORCH
, val
);
729 goto err_config_video
;
731 val
= mode
->hdisplay
* bit_per_pix
/ 8;
732 ret
= cdn_dp_reg_write(dp
, DP_BYTE_COUNT
, val
);
734 goto err_config_video
;
736 val
= mode
->htotal
| ((mode
->htotal
- mode
->hsync_start
) << 16);
737 ret
= cdn_dp_reg_write(dp
, MSA_HORIZONTAL_0
, val
);
739 goto err_config_video
;
741 val
= mode
->hsync_end
- mode
->hsync_start
;
742 val
|= (mode
->hdisplay
<< 16) | (video
->h_sync_polarity
<< 15);
743 ret
= cdn_dp_reg_write(dp
, MSA_HORIZONTAL_1
, val
);
745 goto err_config_video
;
748 val
|= (mode
->vtotal
- mode
->vsync_start
) << 16;
749 ret
= cdn_dp_reg_write(dp
, MSA_VERTICAL_0
, val
);
751 goto err_config_video
;
753 val
= mode
->vsync_end
- mode
->vsync_start
;
754 val
|= (mode
->vdisplay
<< 16) | (video
->v_sync_polarity
<< 15);
755 ret
= cdn_dp_reg_write(dp
, MSA_VERTICAL_1
, val
);
757 goto err_config_video
;
759 val
= cdn_dp_get_msa_misc(video
, mode
);
760 ret
= cdn_dp_reg_write(dp
, MSA_MISC
, val
);
762 goto err_config_video
;
764 ret
= cdn_dp_reg_write(dp
, STREAM_CONFIG
, 1);
766 goto err_config_video
;
768 val
= mode
->hsync_end
- mode
->hsync_start
;
769 val
|= mode
->hdisplay
<< 16;
770 ret
= cdn_dp_reg_write(dp
, DP_HORIZONTAL
, val
);
772 goto err_config_video
;
774 val
= mode
->vdisplay
;
775 val
|= (mode
->vtotal
- mode
->vsync_start
) << 16;
776 ret
= cdn_dp_reg_write(dp
, DP_VERTICAL_0
, val
);
778 goto err_config_video
;
781 ret
= cdn_dp_reg_write(dp
, DP_VERTICAL_1
, val
);
783 goto err_config_video
;
785 ret
= cdn_dp_reg_write_bit(dp
, DP_VB_ID
, 2, 1, 0);
789 DRM_DEV_ERROR(dp
->dev
, "config video failed: %d\n", ret
);
793 int cdn_dp_audio_stop(struct cdn_dp_device
*dp
, struct audio_info
*audio
)
797 ret
= cdn_dp_reg_write(dp
, AUDIO_PACK_CONTROL
, 0);
799 DRM_DEV_ERROR(dp
->dev
, "audio stop failed: %d\n", ret
);
803 writel(0, dp
->regs
+ SPDIF_CTRL_ADDR
);
805 /* clearn the audio config and reset */
806 writel(0, dp
->regs
+ AUDIO_SRC_CNTL
);
807 writel(0, dp
->regs
+ AUDIO_SRC_CNFG
);
808 writel(AUDIO_SW_RST
, dp
->regs
+ AUDIO_SRC_CNTL
);
809 writel(0, dp
->regs
+ AUDIO_SRC_CNTL
);
811 /* reset smpl2pckt component */
812 writel(0, dp
->regs
+ SMPL2PKT_CNTL
);
813 writel(AUDIO_SW_RST
, dp
->regs
+ SMPL2PKT_CNTL
);
814 writel(0, dp
->regs
+ SMPL2PKT_CNTL
);
817 writel(AUDIO_SW_RST
, dp
->regs
+ FIFO_CNTL
);
818 writel(0, dp
->regs
+ FIFO_CNTL
);
820 if (audio
->format
== AFMT_SPDIF
)
821 clk_disable_unprepare(dp
->spdif_clk
);
826 int cdn_dp_audio_mute(struct cdn_dp_device
*dp
, bool enable
)
830 ret
= cdn_dp_reg_write_bit(dp
, DP_VB_ID
, 4, 1, enable
);
832 DRM_DEV_ERROR(dp
->dev
, "audio mute failed: %d\n", ret
);
837 static void cdn_dp_audio_config_i2s(struct cdn_dp_device
*dp
,
838 struct audio_info
*audio
)
840 int sub_pckt_num
= 1, i2s_port_en_val
= 0xf, i
;
843 if (audio
->channels
== 2) {
844 if (dp
->link
.num_lanes
== 1)
850 } else if (audio
->channels
== 4) {
854 writel(0x0, dp
->regs
+ SPDIF_CTRL_ADDR
);
856 writel(SYNC_WR_TO_CH_ZERO
, dp
->regs
+ FIFO_CNTL
);
858 val
= MAX_NUM_CH(audio
->channels
);
859 val
|= NUM_OF_I2S_PORTS(audio
->channels
);
860 val
|= AUDIO_TYPE_LPCM
;
861 val
|= CFG_SUB_PCKT_NUM(sub_pckt_num
);
862 writel(val
, dp
->regs
+ SMPL2PKT_CNFG
);
864 if (audio
->sample_width
== 16)
866 else if (audio
->sample_width
== 24)
871 val
|= AUDIO_CH_NUM(audio
->channels
);
872 val
|= I2S_DEC_PORT_EN(i2s_port_en_val
);
873 val
|= TRANS_SMPL_WIDTH_32
;
874 writel(val
, dp
->regs
+ AUDIO_SRC_CNFG
);
876 for (i
= 0; i
< (audio
->channels
+ 1) / 2; i
++) {
877 if (audio
->sample_width
== 16)
878 val
= (0x02 << 8) | (0x02 << 20);
879 else if (audio
->sample_width
== 24)
880 val
= (0x0b << 8) | (0x0b << 20);
882 val
|= ((2 * i
) << 4) | ((2 * i
+ 1) << 16);
883 writel(val
, dp
->regs
+ STTS_BIT_CH(i
));
886 switch (audio
->sample_rate
) {
888 val
= SAMPLING_FREQ(3) |
889 ORIGINAL_SAMP_FREQ(0xc);
892 val
= SAMPLING_FREQ(0) |
893 ORIGINAL_SAMP_FREQ(0xf);
896 val
= SAMPLING_FREQ(2) |
897 ORIGINAL_SAMP_FREQ(0xd);
900 val
= SAMPLING_FREQ(8) |
901 ORIGINAL_SAMP_FREQ(0x7);
904 val
= SAMPLING_FREQ(0xa) |
905 ORIGINAL_SAMP_FREQ(5);
908 val
= SAMPLING_FREQ(0xc) |
909 ORIGINAL_SAMP_FREQ(3);
912 val
= SAMPLING_FREQ(0xe) |
913 ORIGINAL_SAMP_FREQ(1);
917 writel(val
, dp
->regs
+ COM_CH_STTS_BITS
);
919 writel(SMPL2PKT_EN
, dp
->regs
+ SMPL2PKT_CNTL
);
920 writel(I2S_DEC_START
, dp
->regs
+ AUDIO_SRC_CNTL
);
923 static void cdn_dp_audio_config_spdif(struct cdn_dp_device
*dp
)
927 writel(SYNC_WR_TO_CH_ZERO
, dp
->regs
+ FIFO_CNTL
);
929 val
= MAX_NUM_CH(2) | AUDIO_TYPE_LPCM
| CFG_SUB_PCKT_NUM(4);
930 writel(val
, dp
->regs
+ SMPL2PKT_CNFG
);
931 writel(SMPL2PKT_EN
, dp
->regs
+ SMPL2PKT_CNTL
);
933 val
= SPDIF_ENABLE
| SPDIF_AVG_SEL
| SPDIF_JITTER_BYPASS
;
934 writel(val
, dp
->regs
+ SPDIF_CTRL_ADDR
);
936 clk_prepare_enable(dp
->spdif_clk
);
937 clk_set_rate(dp
->spdif_clk
, CDN_DP_SPDIF_CLK
);
940 int cdn_dp_audio_config(struct cdn_dp_device
*dp
, struct audio_info
*audio
)
944 /* reset the spdif clk before config */
945 if (audio
->format
== AFMT_SPDIF
) {
946 reset_control_assert(dp
->spdif_rst
);
947 reset_control_deassert(dp
->spdif_rst
);
950 ret
= cdn_dp_reg_write(dp
, CM_LANE_CTRL
, LANE_REF_CYC
);
952 goto err_audio_config
;
954 ret
= cdn_dp_reg_write(dp
, CM_CTRL
, 0);
956 goto err_audio_config
;
958 if (audio
->format
== AFMT_I2S
)
959 cdn_dp_audio_config_i2s(dp
, audio
);
960 else if (audio
->format
== AFMT_SPDIF
)
961 cdn_dp_audio_config_spdif(dp
);
963 ret
= cdn_dp_reg_write(dp
, AUDIO_PACK_CONTROL
, AUDIO_PACK_EN
);
967 DRM_DEV_ERROR(dp
->dev
, "audio config failed: %d\n", ret
);