1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2015 MediaTek Inc.
7 #include <linux/component.h>
8 #include <linux/iopoll.h>
11 #include <linux/of_platform.h>
12 #include <linux/phy/phy.h>
13 #include <linux/platform_device.h>
15 #include <video/mipi_display.h>
16 #include <video/videomode.h>
18 #include <drm/drm_atomic_helper.h>
19 #include <drm/drm_bridge.h>
20 #include <drm/drm_mipi_dsi.h>
21 #include <drm/drm_of.h>
22 #include <drm/drm_panel.h>
23 #include <drm/drm_print.h>
24 #include <drm/drm_probe_helper.h>
26 #include "mtk_drm_ddp_comp.h"
28 #define DSI_START 0x00
30 #define DSI_INTEN 0x08
32 #define DSI_INTSTA 0x0c
33 #define LPRX_RD_RDY_INT_FLAG BIT(0)
34 #define CMD_DONE_INT_FLAG BIT(1)
35 #define TE_RDY_INT_FLAG BIT(2)
36 #define VM_DONE_INT_FLAG BIT(3)
37 #define EXT_TE_RDY_INT_FLAG BIT(4)
38 #define DSI_BUSY BIT(31)
40 #define DSI_CON_CTRL 0x10
41 #define DSI_RESET BIT(0)
43 #define DPHY_RESET BIT(2)
45 #define DSI_MODE_CTRL 0x14
48 #define SYNC_PULSE_MODE 1
49 #define SYNC_EVENT_MODE 2
51 #define FRM_MODE BIT(16)
52 #define MIX_MODE BIT(17)
54 #define DSI_TXRX_CTRL 0x18
56 #define LANE_NUM (0xf << 2)
57 #define DIS_EOT BIT(6)
58 #define NULL_EN BIT(7)
59 #define TE_FREERUN BIT(8)
60 #define EXT_TE_EN BIT(9)
61 #define EXT_TE_EDGE BIT(10)
62 #define MAX_RTN_SIZE (0xf << 12)
63 #define HSTX_CKLP_EN BIT(16)
65 #define DSI_PSCTRL 0x1c
66 #define DSI_PS_WC 0x3fff
67 #define DSI_PS_SEL (3 << 16)
68 #define PACKED_PS_16BIT_RGB565 (0 << 16)
69 #define LOOSELY_PS_18BIT_RGB666 (1 << 16)
70 #define PACKED_PS_18BIT_RGB666 (2 << 16)
71 #define PACKED_PS_24BIT_RGB888 (3 << 16)
73 #define DSI_VSA_NL 0x20
74 #define DSI_VBP_NL 0x24
75 #define DSI_VFP_NL 0x28
76 #define DSI_VACT_NL 0x2C
77 #define DSI_SIZE_CON 0x38
78 #define DSI_HSA_WC 0x50
79 #define DSI_HBP_WC 0x54
80 #define DSI_HFP_WC 0x58
82 #define DSI_CMDQ_SIZE 0x60
83 #define CMDQ_SIZE 0x3f
85 #define DSI_HSTX_CKL_WC 0x64
87 #define DSI_RX_DATA0 0x74
88 #define DSI_RX_DATA1 0x78
89 #define DSI_RX_DATA2 0x7c
90 #define DSI_RX_DATA3 0x80
95 #define DSI_PHY_LCCON 0x104
96 #define LC_HS_TX_EN BIT(0)
97 #define LC_ULPM_EN BIT(1)
98 #define LC_WAKEUP_EN BIT(2)
100 #define DSI_PHY_LD0CON 0x108
101 #define LD0_HS_TX_EN BIT(0)
102 #define LD0_ULPM_EN BIT(1)
103 #define LD0_WAKEUP_EN BIT(2)
105 #define DSI_PHY_TIMECON0 0x110
106 #define LPX (0xff << 0)
107 #define HS_PREP (0xff << 8)
108 #define HS_ZERO (0xff << 16)
109 #define HS_TRAIL (0xff << 24)
111 #define DSI_PHY_TIMECON1 0x114
112 #define TA_GO (0xff << 0)
113 #define TA_SURE (0xff << 8)
114 #define TA_GET (0xff << 16)
115 #define DA_HS_EXIT (0xff << 24)
117 #define DSI_PHY_TIMECON2 0x118
118 #define CONT_DET (0xff << 0)
119 #define CLK_ZERO (0xff << 16)
120 #define CLK_TRAIL (0xff << 24)
122 #define DSI_PHY_TIMECON3 0x11c
123 #define CLK_HS_PREP (0xff << 0)
124 #define CLK_HS_POST (0xff << 8)
125 #define CLK_HS_EXIT (0xff << 16)
127 #define DSI_VM_CMD_CON 0x130
128 #define VM_CMD_EN BIT(0)
129 #define TS_VFP_EN BIT(5)
131 #define DSI_SHADOW_DEBUG 0x190U
132 #define FORCE_COMMIT BIT(0)
133 #define BYPASS_SHADOW BIT(1)
135 #define CONFIG (0xff << 0)
136 #define SHORT_PACKET 0
137 #define LONG_PACKET 2
139 #define DATA_ID (0xff << 8)
140 #define DATA_0 (0xff << 16)
141 #define DATA_1 (0xff << 24)
143 #define NS_TO_CYCLE(n, c) ((n) / (c) + (((n) % (c)) ? 1 : 0))
145 #define MTK_DSI_HOST_IS_READ(type) \
146 ((type == MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM) || \
147 (type == MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM) || \
148 (type == MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM) || \
149 (type == MIPI_DSI_DCS_READ))
151 struct mtk_phy_timing {
172 struct mtk_dsi_driver_data {
173 const u32 reg_cmdq_off;
179 struct mtk_ddp_comp ddp_comp;
181 struct mipi_dsi_host host;
182 struct drm_encoder encoder;
183 struct drm_connector conn;
184 struct drm_panel *panel;
185 struct drm_bridge *bridge;
190 struct clk *engine_clk;
191 struct clk *digital_clk;
196 unsigned long mode_flags;
197 enum mipi_dsi_pixel_format format;
200 struct mtk_phy_timing phy_timing;
204 wait_queue_head_t irq_wait_queue;
205 const struct mtk_dsi_driver_data *driver_data;
208 static inline struct mtk_dsi *encoder_to_dsi(struct drm_encoder *e)
210 return container_of(e, struct mtk_dsi, encoder);
213 static inline struct mtk_dsi *connector_to_dsi(struct drm_connector *c)
215 return container_of(c, struct mtk_dsi, conn);
218 static inline struct mtk_dsi *host_to_dsi(struct mipi_dsi_host *h)
220 return container_of(h, struct mtk_dsi, host);
223 static void mtk_dsi_mask(struct mtk_dsi *dsi, u32 offset, u32 mask, u32 data)
225 u32 temp = readl(dsi->regs + offset);
227 writel((temp & ~mask) | (data & mask), dsi->regs + offset);
230 static void mtk_dsi_phy_timconfig(struct mtk_dsi *dsi)
232 u32 timcon0, timcon1, timcon2, timcon3;
234 struct mtk_phy_timing *timing = &dsi->phy_timing;
236 ui = DIV_ROUND_UP(1000000000, dsi->data_rate);
237 cycle_time = div_u64(8000000000ULL, dsi->data_rate);
239 timing->lpx = NS_TO_CYCLE(60, cycle_time);
240 timing->da_hs_prepare = NS_TO_CYCLE(50 + 5 * ui, cycle_time);
241 timing->da_hs_zero = NS_TO_CYCLE(110 + 6 * ui, cycle_time);
242 timing->da_hs_trail = NS_TO_CYCLE(77 + 4 * ui, cycle_time);
244 timing->ta_go = 4 * timing->lpx;
245 timing->ta_sure = 3 * timing->lpx / 2;
246 timing->ta_get = 5 * timing->lpx;
247 timing->da_hs_exit = 2 * timing->lpx;
249 timing->clk_hs_zero = NS_TO_CYCLE(336, cycle_time);
250 timing->clk_hs_trail = NS_TO_CYCLE(100, cycle_time) + 10;
252 timing->clk_hs_prepare = NS_TO_CYCLE(64, cycle_time);
253 timing->clk_hs_post = NS_TO_CYCLE(80 + 52 * ui, cycle_time);
254 timing->clk_hs_exit = 2 * timing->lpx;
256 timcon0 = timing->lpx | timing->da_hs_prepare << 8 |
257 timing->da_hs_zero << 16 | timing->da_hs_trail << 24;
258 timcon1 = timing->ta_go | timing->ta_sure << 8 |
259 timing->ta_get << 16 | timing->da_hs_exit << 24;
260 timcon2 = 1 << 8 | timing->clk_hs_zero << 16 |
261 timing->clk_hs_trail << 24;
262 timcon3 = timing->clk_hs_prepare | timing->clk_hs_post << 8 |
263 timing->clk_hs_exit << 16;
265 writel(timcon0, dsi->regs + DSI_PHY_TIMECON0);
266 writel(timcon1, dsi->regs + DSI_PHY_TIMECON1);
267 writel(timcon2, dsi->regs + DSI_PHY_TIMECON2);
268 writel(timcon3, dsi->regs + DSI_PHY_TIMECON3);
271 static void mtk_dsi_enable(struct mtk_dsi *dsi)
273 mtk_dsi_mask(dsi, DSI_CON_CTRL, DSI_EN, DSI_EN);
276 static void mtk_dsi_disable(struct mtk_dsi *dsi)
278 mtk_dsi_mask(dsi, DSI_CON_CTRL, DSI_EN, 0);
281 static void mtk_dsi_reset_engine(struct mtk_dsi *dsi)
283 mtk_dsi_mask(dsi, DSI_CON_CTRL, DSI_RESET, DSI_RESET);
284 mtk_dsi_mask(dsi, DSI_CON_CTRL, DSI_RESET, 0);
287 static void mtk_dsi_reset_dphy(struct mtk_dsi *dsi)
289 mtk_dsi_mask(dsi, DSI_CON_CTRL, DPHY_RESET, DPHY_RESET);
290 mtk_dsi_mask(dsi, DSI_CON_CTRL, DPHY_RESET, 0);
293 static void mtk_dsi_clk_ulp_mode_enter(struct mtk_dsi *dsi)
295 mtk_dsi_mask(dsi, DSI_PHY_LCCON, LC_HS_TX_EN, 0);
296 mtk_dsi_mask(dsi, DSI_PHY_LCCON, LC_ULPM_EN, 0);
299 static void mtk_dsi_clk_ulp_mode_leave(struct mtk_dsi *dsi)
301 mtk_dsi_mask(dsi, DSI_PHY_LCCON, LC_ULPM_EN, 0);
302 mtk_dsi_mask(dsi, DSI_PHY_LCCON, LC_WAKEUP_EN, LC_WAKEUP_EN);
303 mtk_dsi_mask(dsi, DSI_PHY_LCCON, LC_WAKEUP_EN, 0);
306 static void mtk_dsi_lane0_ulp_mode_enter(struct mtk_dsi *dsi)
308 mtk_dsi_mask(dsi, DSI_PHY_LD0CON, LD0_HS_TX_EN, 0);
309 mtk_dsi_mask(dsi, DSI_PHY_LD0CON, LD0_ULPM_EN, 0);
312 static void mtk_dsi_lane0_ulp_mode_leave(struct mtk_dsi *dsi)
314 mtk_dsi_mask(dsi, DSI_PHY_LD0CON, LD0_ULPM_EN, 0);
315 mtk_dsi_mask(dsi, DSI_PHY_LD0CON, LD0_WAKEUP_EN, LD0_WAKEUP_EN);
316 mtk_dsi_mask(dsi, DSI_PHY_LD0CON, LD0_WAKEUP_EN, 0);
319 static bool mtk_dsi_clk_hs_state(struct mtk_dsi *dsi)
323 tmp_reg1 = readl(dsi->regs + DSI_PHY_LCCON);
324 return ((tmp_reg1 & LC_HS_TX_EN) == 1) ? true : false;
327 static void mtk_dsi_clk_hs_mode(struct mtk_dsi *dsi, bool enter)
329 if (enter && !mtk_dsi_clk_hs_state(dsi))
330 mtk_dsi_mask(dsi, DSI_PHY_LCCON, LC_HS_TX_EN, LC_HS_TX_EN);
331 else if (!enter && mtk_dsi_clk_hs_state(dsi))
332 mtk_dsi_mask(dsi, DSI_PHY_LCCON, LC_HS_TX_EN, 0);
335 static void mtk_dsi_set_mode(struct mtk_dsi *dsi)
337 u32 vid_mode = CMD_MODE;
339 if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO) {
340 if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_BURST)
341 vid_mode = BURST_MODE;
342 else if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
343 vid_mode = SYNC_PULSE_MODE;
345 vid_mode = SYNC_EVENT_MODE;
348 writel(vid_mode, dsi->regs + DSI_MODE_CTRL);
351 static void mtk_dsi_set_vm_cmd(struct mtk_dsi *dsi)
353 mtk_dsi_mask(dsi, DSI_VM_CMD_CON, VM_CMD_EN, VM_CMD_EN);
354 mtk_dsi_mask(dsi, DSI_VM_CMD_CON, TS_VFP_EN, TS_VFP_EN);
357 static void mtk_dsi_ps_control_vact(struct mtk_dsi *dsi)
359 struct videomode *vm = &dsi->vm;
360 u32 dsi_buf_bpp, ps_wc;
363 if (dsi->format == MIPI_DSI_FMT_RGB565)
368 ps_wc = vm->hactive * dsi_buf_bpp;
371 switch (dsi->format) {
372 case MIPI_DSI_FMT_RGB888:
373 ps_bpp_mode |= PACKED_PS_24BIT_RGB888;
375 case MIPI_DSI_FMT_RGB666:
376 ps_bpp_mode |= PACKED_PS_18BIT_RGB666;
378 case MIPI_DSI_FMT_RGB666_PACKED:
379 ps_bpp_mode |= LOOSELY_PS_18BIT_RGB666;
381 case MIPI_DSI_FMT_RGB565:
382 ps_bpp_mode |= PACKED_PS_16BIT_RGB565;
386 writel(vm->vactive, dsi->regs + DSI_VACT_NL);
387 writel(ps_bpp_mode, dsi->regs + DSI_PSCTRL);
388 writel(ps_wc, dsi->regs + DSI_HSTX_CKL_WC);
391 static void mtk_dsi_rxtx_control(struct mtk_dsi *dsi)
395 switch (dsi->lanes) {
413 tmp_reg |= (dsi->mode_flags & MIPI_DSI_CLOCK_NON_CONTINUOUS) << 6;
414 tmp_reg |= (dsi->mode_flags & MIPI_DSI_MODE_EOT_PACKET) >> 3;
416 writel(tmp_reg, dsi->regs + DSI_TXRX_CTRL);
419 static void mtk_dsi_ps_control(struct mtk_dsi *dsi)
424 switch (dsi->format) {
425 case MIPI_DSI_FMT_RGB888:
426 tmp_reg = PACKED_PS_24BIT_RGB888;
429 case MIPI_DSI_FMT_RGB666:
430 tmp_reg = LOOSELY_PS_18BIT_RGB666;
433 case MIPI_DSI_FMT_RGB666_PACKED:
434 tmp_reg = PACKED_PS_18BIT_RGB666;
437 case MIPI_DSI_FMT_RGB565:
438 tmp_reg = PACKED_PS_16BIT_RGB565;
442 tmp_reg = PACKED_PS_24BIT_RGB888;
447 tmp_reg += dsi->vm.hactive * dsi_tmp_buf_bpp & DSI_PS_WC;
448 writel(tmp_reg, dsi->regs + DSI_PSCTRL);
451 static void mtk_dsi_config_vdo_timing(struct mtk_dsi *dsi)
453 u32 horizontal_sync_active_byte;
454 u32 horizontal_backporch_byte;
455 u32 horizontal_frontporch_byte;
456 u32 dsi_tmp_buf_bpp, data_phy_cycles;
457 struct mtk_phy_timing *timing = &dsi->phy_timing;
459 struct videomode *vm = &dsi->vm;
461 if (dsi->format == MIPI_DSI_FMT_RGB565)
466 writel(vm->vsync_len, dsi->regs + DSI_VSA_NL);
467 writel(vm->vback_porch, dsi->regs + DSI_VBP_NL);
468 writel(vm->vfront_porch, dsi->regs + DSI_VFP_NL);
469 writel(vm->vactive, dsi->regs + DSI_VACT_NL);
471 if (dsi->driver_data->has_size_ctl)
472 writel(vm->vactive << 16 | vm->hactive,
473 dsi->regs + DSI_SIZE_CON);
475 horizontal_sync_active_byte = (vm->hsync_len * dsi_tmp_buf_bpp - 10);
477 if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
478 horizontal_backporch_byte =
479 (vm->hback_porch * dsi_tmp_buf_bpp - 10);
481 horizontal_backporch_byte = ((vm->hback_porch + vm->hsync_len) *
482 dsi_tmp_buf_bpp - 10);
484 data_phy_cycles = timing->lpx + timing->da_hs_prepare +
485 timing->da_hs_zero + timing->da_hs_exit + 2;
487 if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_BURST) {
488 if (vm->hfront_porch * dsi_tmp_buf_bpp >
489 data_phy_cycles * dsi->lanes + 18) {
490 horizontal_frontporch_byte = vm->hfront_porch *
495 DRM_WARN("HFP less than d-phy, FPS will under 60Hz\n");
496 horizontal_frontporch_byte = vm->hfront_porch *
500 if (vm->hfront_porch * dsi_tmp_buf_bpp >
501 data_phy_cycles * dsi->lanes + 12) {
502 horizontal_frontporch_byte = vm->hfront_porch *
507 DRM_WARN("HFP less than d-phy, FPS will under 60Hz\n");
508 horizontal_frontporch_byte = vm->hfront_porch *
513 writel(horizontal_sync_active_byte, dsi->regs + DSI_HSA_WC);
514 writel(horizontal_backporch_byte, dsi->regs + DSI_HBP_WC);
515 writel(horizontal_frontporch_byte, dsi->regs + DSI_HFP_WC);
517 mtk_dsi_ps_control(dsi);
520 static void mtk_dsi_start(struct mtk_dsi *dsi)
522 writel(0, dsi->regs + DSI_START);
523 writel(1, dsi->regs + DSI_START);
526 static void mtk_dsi_stop(struct mtk_dsi *dsi)
528 writel(0, dsi->regs + DSI_START);
531 static void mtk_dsi_set_cmd_mode(struct mtk_dsi *dsi)
533 writel(CMD_MODE, dsi->regs + DSI_MODE_CTRL);
536 static void mtk_dsi_set_interrupt_enable(struct mtk_dsi *dsi)
538 u32 inten = LPRX_RD_RDY_INT_FLAG | CMD_DONE_INT_FLAG | VM_DONE_INT_FLAG;
540 writel(inten, dsi->regs + DSI_INTEN);
543 static void mtk_dsi_irq_data_set(struct mtk_dsi *dsi, u32 irq_bit)
545 dsi->irq_data |= irq_bit;
548 static void mtk_dsi_irq_data_clear(struct mtk_dsi *dsi, u32 irq_bit)
550 dsi->irq_data &= ~irq_bit;
553 static s32 mtk_dsi_wait_for_irq_done(struct mtk_dsi *dsi, u32 irq_flag,
554 unsigned int timeout)
557 unsigned long jiffies = msecs_to_jiffies(timeout);
559 ret = wait_event_interruptible_timeout(dsi->irq_wait_queue,
560 dsi->irq_data & irq_flag,
563 DRM_WARN("Wait DSI IRQ(0x%08x) Timeout\n", irq_flag);
566 mtk_dsi_reset_engine(dsi);
572 static irqreturn_t mtk_dsi_irq(int irq, void *dev_id)
574 struct mtk_dsi *dsi = dev_id;
576 u32 flag = LPRX_RD_RDY_INT_FLAG | CMD_DONE_INT_FLAG | VM_DONE_INT_FLAG;
578 status = readl(dsi->regs + DSI_INTSTA) & flag;
582 mtk_dsi_mask(dsi, DSI_RACK, RACK, RACK);
583 tmp = readl(dsi->regs + DSI_INTSTA);
584 } while (tmp & DSI_BUSY);
586 mtk_dsi_mask(dsi, DSI_INTSTA, status, 0);
587 mtk_dsi_irq_data_set(dsi, status);
588 wake_up_interruptible(&dsi->irq_wait_queue);
594 static s32 mtk_dsi_switch_to_cmd_mode(struct mtk_dsi *dsi, u8 irq_flag, u32 t)
596 mtk_dsi_irq_data_clear(dsi, irq_flag);
597 mtk_dsi_set_cmd_mode(dsi);
599 if (!mtk_dsi_wait_for_irq_done(dsi, irq_flag, t)) {
600 DRM_ERROR("failed to switch cmd mode\n");
607 static int mtk_dsi_poweron(struct mtk_dsi *dsi)
609 struct device *dev = dsi->host.dev;
613 if (++dsi->refcount != 1)
616 switch (dsi->format) {
617 case MIPI_DSI_FMT_RGB565:
620 case MIPI_DSI_FMT_RGB666_PACKED:
623 case MIPI_DSI_FMT_RGB666:
624 case MIPI_DSI_FMT_RGB888:
630 dsi->data_rate = DIV_ROUND_UP_ULL(dsi->vm.pixelclock * bit_per_pixel,
633 ret = clk_set_rate(dsi->hs_clk, dsi->data_rate);
635 dev_err(dev, "Failed to set data rate: %d\n", ret);
639 phy_power_on(dsi->phy);
641 ret = clk_prepare_enable(dsi->engine_clk);
643 dev_err(dev, "Failed to enable engine clock: %d\n", ret);
644 goto err_phy_power_off;
647 ret = clk_prepare_enable(dsi->digital_clk);
649 dev_err(dev, "Failed to enable digital clock: %d\n", ret);
650 goto err_disable_engine_clk;
655 if (dsi->driver_data->has_shadow_ctl)
656 writel(FORCE_COMMIT | BYPASS_SHADOW,
657 dsi->regs + DSI_SHADOW_DEBUG);
659 mtk_dsi_reset_engine(dsi);
660 mtk_dsi_phy_timconfig(dsi);
662 mtk_dsi_rxtx_control(dsi);
663 usleep_range(30, 100);
664 mtk_dsi_reset_dphy(dsi);
665 mtk_dsi_ps_control_vact(dsi);
666 mtk_dsi_set_vm_cmd(dsi);
667 mtk_dsi_config_vdo_timing(dsi);
668 mtk_dsi_set_interrupt_enable(dsi);
670 mtk_dsi_clk_ulp_mode_leave(dsi);
671 mtk_dsi_lane0_ulp_mode_leave(dsi);
672 mtk_dsi_clk_hs_mode(dsi, 0);
675 if (drm_panel_prepare(dsi->panel)) {
676 DRM_ERROR("failed to prepare the panel\n");
677 goto err_disable_digital_clk;
682 err_disable_digital_clk:
683 clk_disable_unprepare(dsi->digital_clk);
684 err_disable_engine_clk:
685 clk_disable_unprepare(dsi->engine_clk);
687 phy_power_off(dsi->phy);
693 static void mtk_dsi_poweroff(struct mtk_dsi *dsi)
695 if (WARN_ON(dsi->refcount == 0))
698 if (--dsi->refcount != 0)
702 * mtk_dsi_stop() and mtk_dsi_start() is asymmetric, since
703 * mtk_dsi_stop() should be called after mtk_drm_crtc_atomic_disable(),
704 * which needs irq for vblank, and mtk_dsi_stop() will disable irq.
705 * mtk_dsi_start() needs to be called in mtk_output_dsi_enable(),
706 * after dsi is fully set.
710 if (!mtk_dsi_switch_to_cmd_mode(dsi, VM_DONE_INT_FLAG, 500)) {
712 if (drm_panel_unprepare(dsi->panel)) {
713 DRM_ERROR("failed to unprepare the panel\n");
719 mtk_dsi_reset_engine(dsi);
720 mtk_dsi_lane0_ulp_mode_enter(dsi);
721 mtk_dsi_clk_ulp_mode_enter(dsi);
723 mtk_dsi_disable(dsi);
725 clk_disable_unprepare(dsi->engine_clk);
726 clk_disable_unprepare(dsi->digital_clk);
728 phy_power_off(dsi->phy);
731 static void mtk_output_dsi_enable(struct mtk_dsi *dsi)
738 ret = mtk_dsi_poweron(dsi);
740 DRM_ERROR("failed to power on dsi\n");
744 mtk_dsi_set_mode(dsi);
745 mtk_dsi_clk_hs_mode(dsi, 1);
750 if (drm_panel_enable(dsi->panel)) {
751 DRM_ERROR("failed to enable the panel\n");
752 goto err_dsi_power_off;
761 mtk_dsi_poweroff(dsi);
764 static void mtk_output_dsi_disable(struct mtk_dsi *dsi)
770 if (drm_panel_disable(dsi->panel)) {
771 DRM_ERROR("failed to disable the panel\n");
776 mtk_dsi_poweroff(dsi);
778 dsi->enabled = false;
781 static void mtk_dsi_encoder_destroy(struct drm_encoder *encoder)
783 drm_encoder_cleanup(encoder);
786 static const struct drm_encoder_funcs mtk_dsi_encoder_funcs = {
787 .destroy = mtk_dsi_encoder_destroy,
790 static bool mtk_dsi_encoder_mode_fixup(struct drm_encoder *encoder,
791 const struct drm_display_mode *mode,
792 struct drm_display_mode *adjusted_mode)
797 static void mtk_dsi_encoder_mode_set(struct drm_encoder *encoder,
798 struct drm_display_mode *mode,
799 struct drm_display_mode *adjusted)
801 struct mtk_dsi *dsi = encoder_to_dsi(encoder);
803 drm_display_mode_to_videomode(adjusted, &dsi->vm);
806 static void mtk_dsi_encoder_disable(struct drm_encoder *encoder)
808 struct mtk_dsi *dsi = encoder_to_dsi(encoder);
810 mtk_output_dsi_disable(dsi);
813 static void mtk_dsi_encoder_enable(struct drm_encoder *encoder)
815 struct mtk_dsi *dsi = encoder_to_dsi(encoder);
817 mtk_output_dsi_enable(dsi);
820 static int mtk_dsi_connector_get_modes(struct drm_connector *connector)
822 struct mtk_dsi *dsi = connector_to_dsi(connector);
824 return drm_panel_get_modes(dsi->panel, connector);
827 static const struct drm_encoder_helper_funcs mtk_dsi_encoder_helper_funcs = {
828 .mode_fixup = mtk_dsi_encoder_mode_fixup,
829 .mode_set = mtk_dsi_encoder_mode_set,
830 .disable = mtk_dsi_encoder_disable,
831 .enable = mtk_dsi_encoder_enable,
834 static const struct drm_connector_funcs mtk_dsi_connector_funcs = {
835 .fill_modes = drm_helper_probe_single_connector_modes,
836 .destroy = drm_connector_cleanup,
837 .reset = drm_atomic_helper_connector_reset,
838 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
839 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
842 static const struct drm_connector_helper_funcs
843 mtk_dsi_connector_helper_funcs = {
844 .get_modes = mtk_dsi_connector_get_modes,
847 static int mtk_dsi_create_connector(struct drm_device *drm, struct mtk_dsi *dsi)
851 ret = drm_connector_init(drm, &dsi->conn, &mtk_dsi_connector_funcs,
852 DRM_MODE_CONNECTOR_DSI);
854 DRM_ERROR("Failed to connector init to drm\n");
858 drm_connector_helper_add(&dsi->conn, &mtk_dsi_connector_helper_funcs);
860 dsi->conn.dpms = DRM_MODE_DPMS_OFF;
861 drm_connector_attach_encoder(&dsi->conn, &dsi->encoder);
864 ret = drm_panel_attach(dsi->panel, &dsi->conn);
866 DRM_ERROR("Failed to attach panel to drm\n");
867 goto err_connector_cleanup;
873 err_connector_cleanup:
874 drm_connector_cleanup(&dsi->conn);
878 static int mtk_dsi_create_conn_enc(struct drm_device *drm, struct mtk_dsi *dsi)
882 ret = drm_encoder_init(drm, &dsi->encoder, &mtk_dsi_encoder_funcs,
883 DRM_MODE_ENCODER_DSI, NULL);
885 DRM_ERROR("Failed to encoder init to drm\n");
888 drm_encoder_helper_add(&dsi->encoder, &mtk_dsi_encoder_helper_funcs);
891 * Currently display data paths are statically assigned to a crtc each.
892 * crtc 0 is OVL0 -> COLOR0 -> AAL -> OD -> RDMA0 -> UFOE -> DSI0
894 dsi->encoder.possible_crtcs = 1;
896 /* If there's a bridge, attach to it and let it create the connector */
898 ret = drm_bridge_attach(&dsi->encoder, dsi->bridge, NULL);
900 DRM_ERROR("Failed to attach bridge to drm\n");
901 goto err_encoder_cleanup;
904 /* Otherwise create our own connector and attach to a panel */
905 ret = mtk_dsi_create_connector(drm, dsi);
907 goto err_encoder_cleanup;
913 drm_encoder_cleanup(&dsi->encoder);
917 static void mtk_dsi_destroy_conn_enc(struct mtk_dsi *dsi)
919 drm_encoder_cleanup(&dsi->encoder);
920 /* Skip connector cleanup if creation was delegated to the bridge */
922 drm_connector_cleanup(&dsi->conn);
924 drm_panel_detach(dsi->panel);
927 static void mtk_dsi_ddp_start(struct mtk_ddp_comp *comp)
929 struct mtk_dsi *dsi = container_of(comp, struct mtk_dsi, ddp_comp);
931 mtk_dsi_poweron(dsi);
934 static void mtk_dsi_ddp_stop(struct mtk_ddp_comp *comp)
936 struct mtk_dsi *dsi = container_of(comp, struct mtk_dsi, ddp_comp);
938 mtk_dsi_poweroff(dsi);
941 static const struct mtk_ddp_comp_funcs mtk_dsi_funcs = {
942 .start = mtk_dsi_ddp_start,
943 .stop = mtk_dsi_ddp_stop,
946 static int mtk_dsi_host_attach(struct mipi_dsi_host *host,
947 struct mipi_dsi_device *device)
949 struct mtk_dsi *dsi = host_to_dsi(host);
951 dsi->lanes = device->lanes;
952 dsi->format = device->format;
953 dsi->mode_flags = device->mode_flags;
956 drm_helper_hpd_irq_event(dsi->conn.dev);
961 static int mtk_dsi_host_detach(struct mipi_dsi_host *host,
962 struct mipi_dsi_device *device)
964 struct mtk_dsi *dsi = host_to_dsi(host);
967 drm_helper_hpd_irq_event(dsi->conn.dev);
972 static void mtk_dsi_wait_for_idle(struct mtk_dsi *dsi)
977 ret = readl_poll_timeout(dsi->regs + DSI_INTSTA, val, !(val & DSI_BUSY),
980 DRM_WARN("polling dsi wait not busy timeout!\n");
983 mtk_dsi_reset_engine(dsi);
987 static u32 mtk_dsi_recv_cnt(u8 type, u8 *read_data)
990 case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_1BYTE:
991 case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_1BYTE:
993 case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_2BYTE:
994 case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_2BYTE:
996 case MIPI_DSI_RX_GENERIC_LONG_READ_RESPONSE:
997 case MIPI_DSI_RX_DCS_LONG_READ_RESPONSE:
998 return read_data[1] + read_data[2] * 16;
999 case MIPI_DSI_RX_ACKNOWLEDGE_AND_ERROR_REPORT:
1000 DRM_INFO("type is 0x02, try again\n");
1003 DRM_INFO("type(0x%x) not recognized\n", type);
1010 static void mtk_dsi_cmdq(struct mtk_dsi *dsi, const struct mipi_dsi_msg *msg)
1012 const char *tx_buf = msg->tx_buf;
1013 u8 config, cmdq_size, cmdq_off, type = msg->type;
1014 u32 reg_val, cmdq_mask, i;
1015 u32 reg_cmdq_off = dsi->driver_data->reg_cmdq_off;
1017 if (MTK_DSI_HOST_IS_READ(type))
1020 config = (msg->tx_len > 2) ? LONG_PACKET : SHORT_PACKET;
1022 if (msg->tx_len > 2) {
1023 cmdq_size = 1 + (msg->tx_len + 3) / 4;
1025 cmdq_mask = CONFIG | DATA_ID | DATA_0 | DATA_1;
1026 reg_val = (msg->tx_len << 16) | (type << 8) | config;
1030 cmdq_mask = CONFIG | DATA_ID;
1031 reg_val = (type << 8) | config;
1034 for (i = 0; i < msg->tx_len; i++)
1035 mtk_dsi_mask(dsi, (reg_cmdq_off + cmdq_off + i) & (~0x3U),
1036 (0xffUL << (((i + cmdq_off) & 3U) * 8U)),
1037 tx_buf[i] << (((i + cmdq_off) & 3U) * 8U));
1039 mtk_dsi_mask(dsi, reg_cmdq_off, cmdq_mask, reg_val);
1040 mtk_dsi_mask(dsi, DSI_CMDQ_SIZE, CMDQ_SIZE, cmdq_size);
1043 static ssize_t mtk_dsi_host_send_cmd(struct mtk_dsi *dsi,
1044 const struct mipi_dsi_msg *msg, u8 flag)
1046 mtk_dsi_wait_for_idle(dsi);
1047 mtk_dsi_irq_data_clear(dsi, flag);
1048 mtk_dsi_cmdq(dsi, msg);
1051 if (!mtk_dsi_wait_for_irq_done(dsi, flag, 2000))
1057 static ssize_t mtk_dsi_host_transfer(struct mipi_dsi_host *host,
1058 const struct mipi_dsi_msg *msg)
1060 struct mtk_dsi *dsi = host_to_dsi(host);
1064 u8 irq_flag = CMD_DONE_INT_FLAG;
1066 if (readl(dsi->regs + DSI_MODE_CTRL) & MODE) {
1067 DRM_ERROR("dsi engine is not command mode\n");
1071 if (MTK_DSI_HOST_IS_READ(msg->type))
1072 irq_flag |= LPRX_RD_RDY_INT_FLAG;
1074 if (mtk_dsi_host_send_cmd(dsi, msg, irq_flag) < 0)
1077 if (!MTK_DSI_HOST_IS_READ(msg->type))
1081 DRM_ERROR("dsi receive buffer size may be NULL\n");
1085 for (i = 0; i < 16; i++)
1086 *(read_data + i) = readb(dsi->regs + DSI_RX_DATA0 + i);
1088 recv_cnt = mtk_dsi_recv_cnt(read_data[0], read_data);
1091 src_addr = &read_data[4];
1093 src_addr = &read_data[1];
1098 if (recv_cnt > msg->rx_len)
1099 recv_cnt = msg->rx_len;
1102 memcpy(msg->rx_buf, src_addr, recv_cnt);
1104 DRM_INFO("dsi get %d byte data from the panel address(0x%x)\n",
1105 recv_cnt, *((u8 *)(msg->tx_buf)));
1110 static const struct mipi_dsi_host_ops mtk_dsi_ops = {
1111 .attach = mtk_dsi_host_attach,
1112 .detach = mtk_dsi_host_detach,
1113 .transfer = mtk_dsi_host_transfer,
1116 static int mtk_dsi_bind(struct device *dev, struct device *master, void *data)
1119 struct drm_device *drm = data;
1120 struct mtk_dsi *dsi = dev_get_drvdata(dev);
1122 ret = mtk_ddp_comp_register(drm, &dsi->ddp_comp);
1124 dev_err(dev, "Failed to register component %pOF: %d\n",
1129 ret = mtk_dsi_create_conn_enc(drm, dsi);
1131 DRM_ERROR("Encoder create failed with %d\n", ret);
1132 goto err_unregister;
1138 mtk_ddp_comp_unregister(drm, &dsi->ddp_comp);
1142 static void mtk_dsi_unbind(struct device *dev, struct device *master,
1145 struct drm_device *drm = data;
1146 struct mtk_dsi *dsi = dev_get_drvdata(dev);
1148 mtk_dsi_destroy_conn_enc(dsi);
1149 mtk_ddp_comp_unregister(drm, &dsi->ddp_comp);
1152 static const struct component_ops mtk_dsi_component_ops = {
1153 .bind = mtk_dsi_bind,
1154 .unbind = mtk_dsi_unbind,
1157 static int mtk_dsi_probe(struct platform_device *pdev)
1159 struct mtk_dsi *dsi;
1160 struct device *dev = &pdev->dev;
1161 struct resource *regs;
1166 dsi = devm_kzalloc(dev, sizeof(*dsi), GFP_KERNEL);
1170 dsi->host.ops = &mtk_dsi_ops;
1171 dsi->host.dev = dev;
1172 ret = mipi_dsi_host_register(&dsi->host);
1174 dev_err(dev, "failed to register DSI host: %d\n", ret);
1178 ret = drm_of_find_panel_or_bridge(dev->of_node, 0, 0,
1179 &dsi->panel, &dsi->bridge);
1181 goto err_unregister_host;
1183 dsi->driver_data = of_device_get_match_data(dev);
1185 dsi->engine_clk = devm_clk_get(dev, "engine");
1186 if (IS_ERR(dsi->engine_clk)) {
1187 ret = PTR_ERR(dsi->engine_clk);
1188 dev_err(dev, "Failed to get engine clock: %d\n", ret);
1189 goto err_unregister_host;
1192 dsi->digital_clk = devm_clk_get(dev, "digital");
1193 if (IS_ERR(dsi->digital_clk)) {
1194 ret = PTR_ERR(dsi->digital_clk);
1195 dev_err(dev, "Failed to get digital clock: %d\n", ret);
1196 goto err_unregister_host;
1199 dsi->hs_clk = devm_clk_get(dev, "hs");
1200 if (IS_ERR(dsi->hs_clk)) {
1201 ret = PTR_ERR(dsi->hs_clk);
1202 dev_err(dev, "Failed to get hs clock: %d\n", ret);
1203 goto err_unregister_host;
1206 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1207 dsi->regs = devm_ioremap_resource(dev, regs);
1208 if (IS_ERR(dsi->regs)) {
1209 ret = PTR_ERR(dsi->regs);
1210 dev_err(dev, "Failed to ioremap memory: %d\n", ret);
1211 goto err_unregister_host;
1214 dsi->phy = devm_phy_get(dev, "dphy");
1215 if (IS_ERR(dsi->phy)) {
1216 ret = PTR_ERR(dsi->phy);
1217 dev_err(dev, "Failed to get MIPI-DPHY: %d\n", ret);
1218 goto err_unregister_host;
1221 comp_id = mtk_ddp_comp_get_id(dev->of_node, MTK_DSI);
1223 dev_err(dev, "Failed to identify by alias: %d\n", comp_id);
1225 goto err_unregister_host;
1228 ret = mtk_ddp_comp_init(dev, dev->of_node, &dsi->ddp_comp, comp_id,
1231 dev_err(dev, "Failed to initialize component: %d\n", ret);
1232 goto err_unregister_host;
1235 irq_num = platform_get_irq(pdev, 0);
1237 dev_err(&pdev->dev, "failed to get dsi irq_num: %d\n", irq_num);
1239 goto err_unregister_host;
1242 irq_set_status_flags(irq_num, IRQ_TYPE_LEVEL_LOW);
1243 ret = devm_request_irq(&pdev->dev, irq_num, mtk_dsi_irq,
1244 IRQF_TRIGGER_LOW, dev_name(&pdev->dev), dsi);
1246 dev_err(&pdev->dev, "failed to request mediatek dsi irq\n");
1247 goto err_unregister_host;
1250 init_waitqueue_head(&dsi->irq_wait_queue);
1252 platform_set_drvdata(pdev, dsi);
1254 ret = component_add(&pdev->dev, &mtk_dsi_component_ops);
1256 dev_err(&pdev->dev, "failed to add component: %d\n", ret);
1257 goto err_unregister_host;
1262 err_unregister_host:
1263 mipi_dsi_host_unregister(&dsi->host);
1267 static int mtk_dsi_remove(struct platform_device *pdev)
1269 struct mtk_dsi *dsi = platform_get_drvdata(pdev);
1271 mtk_output_dsi_disable(dsi);
1272 component_del(&pdev->dev, &mtk_dsi_component_ops);
1273 mipi_dsi_host_unregister(&dsi->host);
1278 static const struct mtk_dsi_driver_data mt8173_dsi_driver_data = {
1279 .reg_cmdq_off = 0x200,
1282 static const struct mtk_dsi_driver_data mt2701_dsi_driver_data = {
1283 .reg_cmdq_off = 0x180,
1286 static const struct mtk_dsi_driver_data mt8183_dsi_driver_data = {
1287 .reg_cmdq_off = 0x200,
1288 .has_shadow_ctl = true,
1289 .has_size_ctl = true,
1292 static const struct of_device_id mtk_dsi_of_match[] = {
1293 { .compatible = "mediatek,mt2701-dsi",
1294 .data = &mt2701_dsi_driver_data },
1295 { .compatible = "mediatek,mt8173-dsi",
1296 .data = &mt8173_dsi_driver_data },
1297 { .compatible = "mediatek,mt8183-dsi",
1298 .data = &mt8183_dsi_driver_data },
1302 struct platform_driver mtk_dsi_driver = {
1303 .probe = mtk_dsi_probe,
1304 .remove = mtk_dsi_remove,
1307 .of_match_table = mtk_dsi_of_match,