Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (c) 2015 MediaTek Inc.
0004  */
0005 
0006 #include <linux/clk.h>
0007 #include <linux/component.h>
0008 #include <linux/iopoll.h>
0009 #include <linux/irq.h>
0010 #include <linux/of.h>
0011 #include <linux/of_platform.h>
0012 #include <linux/phy/phy.h>
0013 #include <linux/platform_device.h>
0014 #include <linux/reset.h>
0015 
0016 #include <video/mipi_display.h>
0017 #include <video/videomode.h>
0018 
0019 #include <drm/drm_atomic_helper.h>
0020 #include <drm/drm_bridge.h>
0021 #include <drm/drm_bridge_connector.h>
0022 #include <drm/drm_mipi_dsi.h>
0023 #include <drm/drm_of.h>
0024 #include <drm/drm_panel.h>
0025 #include <drm/drm_print.h>
0026 #include <drm/drm_probe_helper.h>
0027 #include <drm/drm_simple_kms_helper.h>
0028 
0029 #include "mtk_disp_drv.h"
0030 #include "mtk_drm_ddp_comp.h"
0031 
0032 #define DSI_START       0x00
0033 
0034 #define DSI_INTEN       0x08
0035 
0036 #define DSI_INTSTA      0x0c
0037 #define LPRX_RD_RDY_INT_FLAG        BIT(0)
0038 #define CMD_DONE_INT_FLAG       BIT(1)
0039 #define TE_RDY_INT_FLAG         BIT(2)
0040 #define VM_DONE_INT_FLAG        BIT(3)
0041 #define EXT_TE_RDY_INT_FLAG     BIT(4)
0042 #define DSI_BUSY            BIT(31)
0043 
0044 #define DSI_CON_CTRL        0x10
0045 #define DSI_RESET           BIT(0)
0046 #define DSI_EN              BIT(1)
0047 #define DPHY_RESET          BIT(2)
0048 
0049 #define DSI_MODE_CTRL       0x14
0050 #define MODE                (3)
0051 #define CMD_MODE            0
0052 #define SYNC_PULSE_MODE         1
0053 #define SYNC_EVENT_MODE         2
0054 #define BURST_MODE          3
0055 #define FRM_MODE            BIT(16)
0056 #define MIX_MODE            BIT(17)
0057 
0058 #define DSI_TXRX_CTRL       0x18
0059 #define VC_NUM              BIT(1)
0060 #define LANE_NUM            (0xf << 2)
0061 #define DIS_EOT             BIT(6)
0062 #define NULL_EN             BIT(7)
0063 #define TE_FREERUN          BIT(8)
0064 #define EXT_TE_EN           BIT(9)
0065 #define EXT_TE_EDGE         BIT(10)
0066 #define MAX_RTN_SIZE            (0xf << 12)
0067 #define HSTX_CKLP_EN            BIT(16)
0068 
0069 #define DSI_PSCTRL      0x1c
0070 #define DSI_PS_WC           0x3fff
0071 #define DSI_PS_SEL          (3 << 16)
0072 #define PACKED_PS_16BIT_RGB565      (0 << 16)
0073 #define LOOSELY_PS_18BIT_RGB666     (1 << 16)
0074 #define PACKED_PS_18BIT_RGB666      (2 << 16)
0075 #define PACKED_PS_24BIT_RGB888      (3 << 16)
0076 
0077 #define DSI_VSA_NL      0x20
0078 #define DSI_VBP_NL      0x24
0079 #define DSI_VFP_NL      0x28
0080 #define DSI_VACT_NL     0x2C
0081 #define DSI_SIZE_CON        0x38
0082 #define DSI_HSA_WC      0x50
0083 #define DSI_HBP_WC      0x54
0084 #define DSI_HFP_WC      0x58
0085 
0086 #define DSI_CMDQ_SIZE       0x60
0087 #define CMDQ_SIZE           0x3f
0088 
0089 #define DSI_HSTX_CKL_WC     0x64
0090 
0091 #define DSI_RX_DATA0        0x74
0092 #define DSI_RX_DATA1        0x78
0093 #define DSI_RX_DATA2        0x7c
0094 #define DSI_RX_DATA3        0x80
0095 
0096 #define DSI_RACK        0x84
0097 #define RACK                BIT(0)
0098 
0099 #define DSI_PHY_LCCON       0x104
0100 #define LC_HS_TX_EN         BIT(0)
0101 #define LC_ULPM_EN          BIT(1)
0102 #define LC_WAKEUP_EN            BIT(2)
0103 
0104 #define DSI_PHY_LD0CON      0x108
0105 #define LD0_HS_TX_EN            BIT(0)
0106 #define LD0_ULPM_EN         BIT(1)
0107 #define LD0_WAKEUP_EN           BIT(2)
0108 
0109 #define DSI_PHY_TIMECON0    0x110
0110 #define LPX             (0xff << 0)
0111 #define HS_PREP             (0xff << 8)
0112 #define HS_ZERO             (0xff << 16)
0113 #define HS_TRAIL            (0xff << 24)
0114 
0115 #define DSI_PHY_TIMECON1    0x114
0116 #define TA_GO               (0xff << 0)
0117 #define TA_SURE             (0xff << 8)
0118 #define TA_GET              (0xff << 16)
0119 #define DA_HS_EXIT          (0xff << 24)
0120 
0121 #define DSI_PHY_TIMECON2    0x118
0122 #define CONT_DET            (0xff << 0)
0123 #define CLK_ZERO            (0xff << 16)
0124 #define CLK_TRAIL           (0xff << 24)
0125 
0126 #define DSI_PHY_TIMECON3    0x11c
0127 #define CLK_HS_PREP         (0xff << 0)
0128 #define CLK_HS_POST         (0xff << 8)
0129 #define CLK_HS_EXIT         (0xff << 16)
0130 
0131 #define DSI_VM_CMD_CON      0x130
0132 #define VM_CMD_EN           BIT(0)
0133 #define TS_VFP_EN           BIT(5)
0134 
0135 #define DSI_SHADOW_DEBUG    0x190U
0136 #define FORCE_COMMIT            BIT(0)
0137 #define BYPASS_SHADOW           BIT(1)
0138 
0139 #define CONFIG              (0xff << 0)
0140 #define SHORT_PACKET            0
0141 #define LONG_PACKET         2
0142 #define BTA             BIT(2)
0143 #define DATA_ID             (0xff << 8)
0144 #define DATA_0              (0xff << 16)
0145 #define DATA_1              (0xff << 24)
0146 
0147 #define NS_TO_CYCLE(n, c)    ((n) / (c) + (((n) % (c)) ? 1 : 0))
0148 
0149 #define MTK_DSI_HOST_IS_READ(type) \
0150     ((type == MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM) || \
0151     (type == MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM) || \
0152     (type == MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM) || \
0153     (type == MIPI_DSI_DCS_READ))
0154 
0155 struct mtk_phy_timing {
0156     u32 lpx;
0157     u32 da_hs_prepare;
0158     u32 da_hs_zero;
0159     u32 da_hs_trail;
0160 
0161     u32 ta_go;
0162     u32 ta_sure;
0163     u32 ta_get;
0164     u32 da_hs_exit;
0165 
0166     u32 clk_hs_zero;
0167     u32 clk_hs_trail;
0168 
0169     u32 clk_hs_prepare;
0170     u32 clk_hs_post;
0171     u32 clk_hs_exit;
0172 };
0173 
0174 struct phy;
0175 
0176 struct mtk_dsi_driver_data {
0177     const u32 reg_cmdq_off;
0178     bool has_shadow_ctl;
0179     bool has_size_ctl;
0180 };
0181 
0182 struct mtk_dsi {
0183     struct device *dev;
0184     struct mipi_dsi_host host;
0185     struct drm_encoder encoder;
0186     struct drm_bridge bridge;
0187     struct drm_bridge *next_bridge;
0188     struct drm_connector *connector;
0189     struct phy *phy;
0190 
0191     void __iomem *regs;
0192 
0193     struct clk *engine_clk;
0194     struct clk *digital_clk;
0195     struct clk *hs_clk;
0196 
0197     u32 data_rate;
0198 
0199     unsigned long mode_flags;
0200     enum mipi_dsi_pixel_format format;
0201     unsigned int lanes;
0202     struct videomode vm;
0203     struct mtk_phy_timing phy_timing;
0204     int refcount;
0205     bool enabled;
0206     bool lanes_ready;
0207     u32 irq_data;
0208     wait_queue_head_t irq_wait_queue;
0209     const struct mtk_dsi_driver_data *driver_data;
0210 };
0211 
0212 static inline struct mtk_dsi *bridge_to_dsi(struct drm_bridge *b)
0213 {
0214     return container_of(b, struct mtk_dsi, bridge);
0215 }
0216 
0217 static inline struct mtk_dsi *host_to_dsi(struct mipi_dsi_host *h)
0218 {
0219     return container_of(h, struct mtk_dsi, host);
0220 }
0221 
0222 static void mtk_dsi_mask(struct mtk_dsi *dsi, u32 offset, u32 mask, u32 data)
0223 {
0224     u32 temp = readl(dsi->regs + offset);
0225 
0226     writel((temp & ~mask) | (data & mask), dsi->regs + offset);
0227 }
0228 
0229 static void mtk_dsi_phy_timconfig(struct mtk_dsi *dsi)
0230 {
0231     u32 timcon0, timcon1, timcon2, timcon3;
0232     u32 data_rate_mhz = DIV_ROUND_UP(dsi->data_rate, 1000000);
0233     struct mtk_phy_timing *timing = &dsi->phy_timing;
0234 
0235     timing->lpx = (60 * data_rate_mhz / (8 * 1000)) + 1;
0236     timing->da_hs_prepare = (80 * data_rate_mhz + 4 * 1000) / 8000;
0237     timing->da_hs_zero = (170 * data_rate_mhz + 10 * 1000) / 8000 + 1 -
0238                  timing->da_hs_prepare;
0239     timing->da_hs_trail = timing->da_hs_prepare + 1;
0240 
0241     timing->ta_go = 4 * timing->lpx - 2;
0242     timing->ta_sure = timing->lpx + 2;
0243     timing->ta_get = 4 * timing->lpx;
0244     timing->da_hs_exit = 2 * timing->lpx + 1;
0245 
0246     timing->clk_hs_prepare = 70 * data_rate_mhz / (8 * 1000);
0247     timing->clk_hs_post = timing->clk_hs_prepare + 8;
0248     timing->clk_hs_trail = timing->clk_hs_prepare;
0249     timing->clk_hs_zero = timing->clk_hs_trail * 4;
0250     timing->clk_hs_exit = 2 * timing->clk_hs_trail;
0251 
0252     timcon0 = timing->lpx | timing->da_hs_prepare << 8 |
0253           timing->da_hs_zero << 16 | timing->da_hs_trail << 24;
0254     timcon1 = timing->ta_go | timing->ta_sure << 8 |
0255           timing->ta_get << 16 | timing->da_hs_exit << 24;
0256     timcon2 = 1 << 8 | timing->clk_hs_zero << 16 |
0257           timing->clk_hs_trail << 24;
0258     timcon3 = timing->clk_hs_prepare | timing->clk_hs_post << 8 |
0259           timing->clk_hs_exit << 16;
0260 
0261     writel(timcon0, dsi->regs + DSI_PHY_TIMECON0);
0262     writel(timcon1, dsi->regs + DSI_PHY_TIMECON1);
0263     writel(timcon2, dsi->regs + DSI_PHY_TIMECON2);
0264     writel(timcon3, dsi->regs + DSI_PHY_TIMECON3);
0265 }
0266 
0267 static void mtk_dsi_enable(struct mtk_dsi *dsi)
0268 {
0269     mtk_dsi_mask(dsi, DSI_CON_CTRL, DSI_EN, DSI_EN);
0270 }
0271 
0272 static void mtk_dsi_disable(struct mtk_dsi *dsi)
0273 {
0274     mtk_dsi_mask(dsi, DSI_CON_CTRL, DSI_EN, 0);
0275 }
0276 
0277 static void mtk_dsi_reset_engine(struct mtk_dsi *dsi)
0278 {
0279     mtk_dsi_mask(dsi, DSI_CON_CTRL, DSI_RESET, DSI_RESET);
0280     mtk_dsi_mask(dsi, DSI_CON_CTRL, DSI_RESET, 0);
0281 }
0282 
0283 static void mtk_dsi_reset_dphy(struct mtk_dsi *dsi)
0284 {
0285     mtk_dsi_mask(dsi, DSI_CON_CTRL, DPHY_RESET, DPHY_RESET);
0286     mtk_dsi_mask(dsi, DSI_CON_CTRL, DPHY_RESET, 0);
0287 }
0288 
0289 static void mtk_dsi_clk_ulp_mode_enter(struct mtk_dsi *dsi)
0290 {
0291     mtk_dsi_mask(dsi, DSI_PHY_LCCON, LC_HS_TX_EN, 0);
0292     mtk_dsi_mask(dsi, DSI_PHY_LCCON, LC_ULPM_EN, 0);
0293 }
0294 
0295 static void mtk_dsi_clk_ulp_mode_leave(struct mtk_dsi *dsi)
0296 {
0297     mtk_dsi_mask(dsi, DSI_PHY_LCCON, LC_ULPM_EN, 0);
0298     mtk_dsi_mask(dsi, DSI_PHY_LCCON, LC_WAKEUP_EN, LC_WAKEUP_EN);
0299     mtk_dsi_mask(dsi, DSI_PHY_LCCON, LC_WAKEUP_EN, 0);
0300 }
0301 
0302 static void mtk_dsi_lane0_ulp_mode_enter(struct mtk_dsi *dsi)
0303 {
0304     mtk_dsi_mask(dsi, DSI_PHY_LD0CON, LD0_HS_TX_EN, 0);
0305     mtk_dsi_mask(dsi, DSI_PHY_LD0CON, LD0_ULPM_EN, 0);
0306 }
0307 
0308 static void mtk_dsi_lane0_ulp_mode_leave(struct mtk_dsi *dsi)
0309 {
0310     mtk_dsi_mask(dsi, DSI_PHY_LD0CON, LD0_ULPM_EN, 0);
0311     mtk_dsi_mask(dsi, DSI_PHY_LD0CON, LD0_WAKEUP_EN, LD0_WAKEUP_EN);
0312     mtk_dsi_mask(dsi, DSI_PHY_LD0CON, LD0_WAKEUP_EN, 0);
0313 }
0314 
0315 static bool mtk_dsi_clk_hs_state(struct mtk_dsi *dsi)
0316 {
0317     return readl(dsi->regs + DSI_PHY_LCCON) & LC_HS_TX_EN;
0318 }
0319 
0320 static void mtk_dsi_clk_hs_mode(struct mtk_dsi *dsi, bool enter)
0321 {
0322     if (enter && !mtk_dsi_clk_hs_state(dsi))
0323         mtk_dsi_mask(dsi, DSI_PHY_LCCON, LC_HS_TX_EN, LC_HS_TX_EN);
0324     else if (!enter && mtk_dsi_clk_hs_state(dsi))
0325         mtk_dsi_mask(dsi, DSI_PHY_LCCON, LC_HS_TX_EN, 0);
0326 }
0327 
0328 static void mtk_dsi_set_mode(struct mtk_dsi *dsi)
0329 {
0330     u32 vid_mode = CMD_MODE;
0331 
0332     if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO) {
0333         if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_BURST)
0334             vid_mode = BURST_MODE;
0335         else if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
0336             vid_mode = SYNC_PULSE_MODE;
0337         else
0338             vid_mode = SYNC_EVENT_MODE;
0339     }
0340 
0341     writel(vid_mode, dsi->regs + DSI_MODE_CTRL);
0342 }
0343 
0344 static void mtk_dsi_set_vm_cmd(struct mtk_dsi *dsi)
0345 {
0346     mtk_dsi_mask(dsi, DSI_VM_CMD_CON, VM_CMD_EN, VM_CMD_EN);
0347     mtk_dsi_mask(dsi, DSI_VM_CMD_CON, TS_VFP_EN, TS_VFP_EN);
0348 }
0349 
0350 static void mtk_dsi_ps_control_vact(struct mtk_dsi *dsi)
0351 {
0352     struct videomode *vm = &dsi->vm;
0353     u32 dsi_buf_bpp, ps_wc;
0354     u32 ps_bpp_mode;
0355 
0356     if (dsi->format == MIPI_DSI_FMT_RGB565)
0357         dsi_buf_bpp = 2;
0358     else
0359         dsi_buf_bpp = 3;
0360 
0361     ps_wc = vm->hactive * dsi_buf_bpp;
0362     ps_bpp_mode = ps_wc;
0363 
0364     switch (dsi->format) {
0365     case MIPI_DSI_FMT_RGB888:
0366         ps_bpp_mode |= PACKED_PS_24BIT_RGB888;
0367         break;
0368     case MIPI_DSI_FMT_RGB666:
0369         ps_bpp_mode |= PACKED_PS_18BIT_RGB666;
0370         break;
0371     case MIPI_DSI_FMT_RGB666_PACKED:
0372         ps_bpp_mode |= LOOSELY_PS_18BIT_RGB666;
0373         break;
0374     case MIPI_DSI_FMT_RGB565:
0375         ps_bpp_mode |= PACKED_PS_16BIT_RGB565;
0376         break;
0377     }
0378 
0379     writel(vm->vactive, dsi->regs + DSI_VACT_NL);
0380     writel(ps_bpp_mode, dsi->regs + DSI_PSCTRL);
0381     writel(ps_wc, dsi->regs + DSI_HSTX_CKL_WC);
0382 }
0383 
0384 static void mtk_dsi_rxtx_control(struct mtk_dsi *dsi)
0385 {
0386     u32 tmp_reg;
0387 
0388     switch (dsi->lanes) {
0389     case 1:
0390         tmp_reg = 1 << 2;
0391         break;
0392     case 2:
0393         tmp_reg = 3 << 2;
0394         break;
0395     case 3:
0396         tmp_reg = 7 << 2;
0397         break;
0398     case 4:
0399         tmp_reg = 0xf << 2;
0400         break;
0401     default:
0402         tmp_reg = 0xf << 2;
0403         break;
0404     }
0405 
0406     if (dsi->mode_flags & MIPI_DSI_CLOCK_NON_CONTINUOUS)
0407         tmp_reg |= HSTX_CKLP_EN;
0408 
0409     if (!(dsi->mode_flags & MIPI_DSI_MODE_NO_EOT_PACKET))
0410         tmp_reg |= DIS_EOT;
0411 
0412     writel(tmp_reg, dsi->regs + DSI_TXRX_CTRL);
0413 }
0414 
0415 static void mtk_dsi_ps_control(struct mtk_dsi *dsi)
0416 {
0417     u32 dsi_tmp_buf_bpp;
0418     u32 tmp_reg;
0419 
0420     switch (dsi->format) {
0421     case MIPI_DSI_FMT_RGB888:
0422         tmp_reg = PACKED_PS_24BIT_RGB888;
0423         dsi_tmp_buf_bpp = 3;
0424         break;
0425     case MIPI_DSI_FMT_RGB666:
0426         tmp_reg = LOOSELY_PS_18BIT_RGB666;
0427         dsi_tmp_buf_bpp = 3;
0428         break;
0429     case MIPI_DSI_FMT_RGB666_PACKED:
0430         tmp_reg = PACKED_PS_18BIT_RGB666;
0431         dsi_tmp_buf_bpp = 3;
0432         break;
0433     case MIPI_DSI_FMT_RGB565:
0434         tmp_reg = PACKED_PS_16BIT_RGB565;
0435         dsi_tmp_buf_bpp = 2;
0436         break;
0437     default:
0438         tmp_reg = PACKED_PS_24BIT_RGB888;
0439         dsi_tmp_buf_bpp = 3;
0440         break;
0441     }
0442 
0443     tmp_reg += dsi->vm.hactive * dsi_tmp_buf_bpp & DSI_PS_WC;
0444     writel(tmp_reg, dsi->regs + DSI_PSCTRL);
0445 }
0446 
0447 static void mtk_dsi_config_vdo_timing(struct mtk_dsi *dsi)
0448 {
0449     u32 horizontal_sync_active_byte;
0450     u32 horizontal_backporch_byte;
0451     u32 horizontal_frontporch_byte;
0452     u32 horizontal_front_back_byte;
0453     u32 data_phy_cycles_byte;
0454     u32 dsi_tmp_buf_bpp, data_phy_cycles;
0455     u32 delta;
0456     struct mtk_phy_timing *timing = &dsi->phy_timing;
0457 
0458     struct videomode *vm = &dsi->vm;
0459 
0460     if (dsi->format == MIPI_DSI_FMT_RGB565)
0461         dsi_tmp_buf_bpp = 2;
0462     else
0463         dsi_tmp_buf_bpp = 3;
0464 
0465     writel(vm->vsync_len, dsi->regs + DSI_VSA_NL);
0466     writel(vm->vback_porch, dsi->regs + DSI_VBP_NL);
0467     writel(vm->vfront_porch, dsi->regs + DSI_VFP_NL);
0468     writel(vm->vactive, dsi->regs + DSI_VACT_NL);
0469 
0470     if (dsi->driver_data->has_size_ctl)
0471         writel(vm->vactive << 16 | vm->hactive,
0472                dsi->regs + DSI_SIZE_CON);
0473 
0474     horizontal_sync_active_byte = (vm->hsync_len * dsi_tmp_buf_bpp - 10);
0475 
0476     if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
0477         horizontal_backporch_byte = vm->hback_porch * dsi_tmp_buf_bpp - 10;
0478     else
0479         horizontal_backporch_byte = (vm->hback_porch + vm->hsync_len) *
0480                         dsi_tmp_buf_bpp - 10;
0481 
0482     data_phy_cycles = timing->lpx + timing->da_hs_prepare +
0483               timing->da_hs_zero + timing->da_hs_exit + 3;
0484 
0485     delta = dsi->mode_flags & MIPI_DSI_MODE_VIDEO_BURST ? 18 : 12;
0486     delta += dsi->mode_flags & MIPI_DSI_MODE_NO_EOT_PACKET ? 2 : 0;
0487 
0488     horizontal_frontporch_byte = vm->hfront_porch * dsi_tmp_buf_bpp;
0489     horizontal_front_back_byte = horizontal_frontporch_byte + horizontal_backporch_byte;
0490     data_phy_cycles_byte = data_phy_cycles * dsi->lanes + delta;
0491 
0492     if (horizontal_front_back_byte > data_phy_cycles_byte) {
0493         horizontal_frontporch_byte -= data_phy_cycles_byte *
0494                           horizontal_frontporch_byte /
0495                           horizontal_front_back_byte;
0496 
0497         horizontal_backporch_byte -= data_phy_cycles_byte *
0498                          horizontal_backporch_byte /
0499                          horizontal_front_back_byte;
0500     } else {
0501         DRM_WARN("HFP + HBP less than d-phy, FPS will under 60Hz\n");
0502     }
0503 
0504     if ((dsi->mode_flags & MIPI_DSI_HS_PKT_END_ALIGNED) &&
0505         (dsi->lanes == 4)) {
0506         horizontal_sync_active_byte =
0507             roundup(horizontal_sync_active_byte, dsi->lanes) - 2;
0508         horizontal_frontporch_byte =
0509             roundup(horizontal_frontporch_byte, dsi->lanes) - 2;
0510         horizontal_backporch_byte =
0511             roundup(horizontal_backporch_byte, dsi->lanes) - 2;
0512         horizontal_backporch_byte -=
0513             (vm->hactive * dsi_tmp_buf_bpp + 2) % dsi->lanes;
0514     }
0515 
0516     writel(horizontal_sync_active_byte, dsi->regs + DSI_HSA_WC);
0517     writel(horizontal_backporch_byte, dsi->regs + DSI_HBP_WC);
0518     writel(horizontal_frontporch_byte, dsi->regs + DSI_HFP_WC);
0519 
0520     mtk_dsi_ps_control(dsi);
0521 }
0522 
0523 static void mtk_dsi_start(struct mtk_dsi *dsi)
0524 {
0525     writel(0, dsi->regs + DSI_START);
0526     writel(1, dsi->regs + DSI_START);
0527 }
0528 
0529 static void mtk_dsi_stop(struct mtk_dsi *dsi)
0530 {
0531     writel(0, dsi->regs + DSI_START);
0532 }
0533 
0534 static void mtk_dsi_set_cmd_mode(struct mtk_dsi *dsi)
0535 {
0536     writel(CMD_MODE, dsi->regs + DSI_MODE_CTRL);
0537 }
0538 
0539 static void mtk_dsi_set_interrupt_enable(struct mtk_dsi *dsi)
0540 {
0541     u32 inten = LPRX_RD_RDY_INT_FLAG | CMD_DONE_INT_FLAG | VM_DONE_INT_FLAG;
0542 
0543     writel(inten, dsi->regs + DSI_INTEN);
0544 }
0545 
0546 static void mtk_dsi_irq_data_set(struct mtk_dsi *dsi, u32 irq_bit)
0547 {
0548     dsi->irq_data |= irq_bit;
0549 }
0550 
0551 static void mtk_dsi_irq_data_clear(struct mtk_dsi *dsi, u32 irq_bit)
0552 {
0553     dsi->irq_data &= ~irq_bit;
0554 }
0555 
0556 static s32 mtk_dsi_wait_for_irq_done(struct mtk_dsi *dsi, u32 irq_flag,
0557                      unsigned int timeout)
0558 {
0559     s32 ret = 0;
0560     unsigned long jiffies = msecs_to_jiffies(timeout);
0561 
0562     ret = wait_event_interruptible_timeout(dsi->irq_wait_queue,
0563                            dsi->irq_data & irq_flag,
0564                            jiffies);
0565     if (ret == 0) {
0566         DRM_WARN("Wait DSI IRQ(0x%08x) Timeout\n", irq_flag);
0567 
0568         mtk_dsi_enable(dsi);
0569         mtk_dsi_reset_engine(dsi);
0570     }
0571 
0572     return ret;
0573 }
0574 
0575 static irqreturn_t mtk_dsi_irq(int irq, void *dev_id)
0576 {
0577     struct mtk_dsi *dsi = dev_id;
0578     u32 status, tmp;
0579     u32 flag = LPRX_RD_RDY_INT_FLAG | CMD_DONE_INT_FLAG | VM_DONE_INT_FLAG;
0580 
0581     status = readl(dsi->regs + DSI_INTSTA) & flag;
0582 
0583     if (status) {
0584         do {
0585             mtk_dsi_mask(dsi, DSI_RACK, RACK, RACK);
0586             tmp = readl(dsi->regs + DSI_INTSTA);
0587         } while (tmp & DSI_BUSY);
0588 
0589         mtk_dsi_mask(dsi, DSI_INTSTA, status, 0);
0590         mtk_dsi_irq_data_set(dsi, status);
0591         wake_up_interruptible(&dsi->irq_wait_queue);
0592     }
0593 
0594     return IRQ_HANDLED;
0595 }
0596 
0597 static s32 mtk_dsi_switch_to_cmd_mode(struct mtk_dsi *dsi, u8 irq_flag, u32 t)
0598 {
0599     mtk_dsi_irq_data_clear(dsi, irq_flag);
0600     mtk_dsi_set_cmd_mode(dsi);
0601 
0602     if (!mtk_dsi_wait_for_irq_done(dsi, irq_flag, t)) {
0603         DRM_ERROR("failed to switch cmd mode\n");
0604         return -ETIME;
0605     } else {
0606         return 0;
0607     }
0608 }
0609 
0610 static int mtk_dsi_poweron(struct mtk_dsi *dsi)
0611 {
0612     struct device *dev = dsi->host.dev;
0613     int ret;
0614     u32 bit_per_pixel;
0615 
0616     if (++dsi->refcount != 1)
0617         return 0;
0618 
0619     switch (dsi->format) {
0620     case MIPI_DSI_FMT_RGB565:
0621         bit_per_pixel = 16;
0622         break;
0623     case MIPI_DSI_FMT_RGB666_PACKED:
0624         bit_per_pixel = 18;
0625         break;
0626     case MIPI_DSI_FMT_RGB666:
0627     case MIPI_DSI_FMT_RGB888:
0628     default:
0629         bit_per_pixel = 24;
0630         break;
0631     }
0632 
0633     dsi->data_rate = DIV_ROUND_UP_ULL(dsi->vm.pixelclock * bit_per_pixel,
0634                       dsi->lanes);
0635 
0636     ret = clk_set_rate(dsi->hs_clk, dsi->data_rate);
0637     if (ret < 0) {
0638         dev_err(dev, "Failed to set data rate: %d\n", ret);
0639         goto err_refcount;
0640     }
0641 
0642     phy_power_on(dsi->phy);
0643 
0644     ret = clk_prepare_enable(dsi->engine_clk);
0645     if (ret < 0) {
0646         dev_err(dev, "Failed to enable engine clock: %d\n", ret);
0647         goto err_phy_power_off;
0648     }
0649 
0650     ret = clk_prepare_enable(dsi->digital_clk);
0651     if (ret < 0) {
0652         dev_err(dev, "Failed to enable digital clock: %d\n", ret);
0653         goto err_disable_engine_clk;
0654     }
0655 
0656     mtk_dsi_enable(dsi);
0657 
0658     if (dsi->driver_data->has_shadow_ctl)
0659         writel(FORCE_COMMIT | BYPASS_SHADOW,
0660                dsi->regs + DSI_SHADOW_DEBUG);
0661 
0662     mtk_dsi_reset_engine(dsi);
0663     mtk_dsi_phy_timconfig(dsi);
0664 
0665     mtk_dsi_ps_control_vact(dsi);
0666     mtk_dsi_set_vm_cmd(dsi);
0667     mtk_dsi_config_vdo_timing(dsi);
0668     mtk_dsi_set_interrupt_enable(dsi);
0669 
0670     return 0;
0671 err_disable_engine_clk:
0672     clk_disable_unprepare(dsi->engine_clk);
0673 err_phy_power_off:
0674     phy_power_off(dsi->phy);
0675 err_refcount:
0676     dsi->refcount--;
0677     return ret;
0678 }
0679 
0680 static void mtk_dsi_poweroff(struct mtk_dsi *dsi)
0681 {
0682     if (WARN_ON(dsi->refcount == 0))
0683         return;
0684 
0685     if (--dsi->refcount != 0)
0686         return;
0687 
0688     /*
0689      * mtk_dsi_stop() and mtk_dsi_start() is asymmetric, since
0690      * mtk_dsi_stop() should be called after mtk_drm_crtc_atomic_disable(),
0691      * which needs irq for vblank, and mtk_dsi_stop() will disable irq.
0692      * mtk_dsi_start() needs to be called in mtk_output_dsi_enable(),
0693      * after dsi is fully set.
0694      */
0695     mtk_dsi_stop(dsi);
0696 
0697     mtk_dsi_switch_to_cmd_mode(dsi, VM_DONE_INT_FLAG, 500);
0698     mtk_dsi_reset_engine(dsi);
0699     mtk_dsi_lane0_ulp_mode_enter(dsi);
0700     mtk_dsi_clk_ulp_mode_enter(dsi);
0701     /* set the lane number as 0 to pull down mipi */
0702     writel(0, dsi->regs + DSI_TXRX_CTRL);
0703 
0704     mtk_dsi_disable(dsi);
0705 
0706     clk_disable_unprepare(dsi->engine_clk);
0707     clk_disable_unprepare(dsi->digital_clk);
0708 
0709     phy_power_off(dsi->phy);
0710 
0711     dsi->lanes_ready = false;
0712 }
0713 
0714 static void mtk_dsi_lane_ready(struct mtk_dsi *dsi)
0715 {
0716     if (!dsi->lanes_ready) {
0717         dsi->lanes_ready = true;
0718         mtk_dsi_rxtx_control(dsi);
0719         usleep_range(30, 100);
0720         mtk_dsi_reset_dphy(dsi);
0721         mtk_dsi_clk_ulp_mode_leave(dsi);
0722         mtk_dsi_lane0_ulp_mode_leave(dsi);
0723         mtk_dsi_clk_hs_mode(dsi, 0);
0724         msleep(20);
0725         /* The reaction time after pulling up the mipi signal for dsi_rx */
0726     }
0727 }
0728 
0729 static void mtk_output_dsi_enable(struct mtk_dsi *dsi)
0730 {
0731     if (dsi->enabled)
0732         return;
0733 
0734     mtk_dsi_lane_ready(dsi);
0735     mtk_dsi_set_mode(dsi);
0736     mtk_dsi_clk_hs_mode(dsi, 1);
0737 
0738     mtk_dsi_start(dsi);
0739 
0740     dsi->enabled = true;
0741 }
0742 
0743 static void mtk_output_dsi_disable(struct mtk_dsi *dsi)
0744 {
0745     if (!dsi->enabled)
0746         return;
0747 
0748     dsi->enabled = false;
0749 }
0750 
0751 static int mtk_dsi_bridge_attach(struct drm_bridge *bridge,
0752                  enum drm_bridge_attach_flags flags)
0753 {
0754     struct mtk_dsi *dsi = bridge_to_dsi(bridge);
0755 
0756     /* Attach the panel or bridge to the dsi bridge */
0757     return drm_bridge_attach(bridge->encoder, dsi->next_bridge,
0758                  &dsi->bridge, flags);
0759 }
0760 
0761 static void mtk_dsi_bridge_mode_set(struct drm_bridge *bridge,
0762                     const struct drm_display_mode *mode,
0763                     const struct drm_display_mode *adjusted)
0764 {
0765     struct mtk_dsi *dsi = bridge_to_dsi(bridge);
0766 
0767     drm_display_mode_to_videomode(adjusted, &dsi->vm);
0768 }
0769 
0770 static void mtk_dsi_bridge_atomic_disable(struct drm_bridge *bridge,
0771                       struct drm_bridge_state *old_bridge_state)
0772 {
0773     struct mtk_dsi *dsi = bridge_to_dsi(bridge);
0774 
0775     mtk_output_dsi_disable(dsi);
0776 }
0777 
0778 static void mtk_dsi_bridge_atomic_enable(struct drm_bridge *bridge,
0779                      struct drm_bridge_state *old_bridge_state)
0780 {
0781     struct mtk_dsi *dsi = bridge_to_dsi(bridge);
0782 
0783     if (dsi->refcount == 0)
0784         return;
0785 
0786     mtk_output_dsi_enable(dsi);
0787 }
0788 
0789 static void mtk_dsi_bridge_atomic_pre_enable(struct drm_bridge *bridge,
0790                          struct drm_bridge_state *old_bridge_state)
0791 {
0792     struct mtk_dsi *dsi = bridge_to_dsi(bridge);
0793     int ret;
0794 
0795     ret = mtk_dsi_poweron(dsi);
0796     if (ret < 0)
0797         DRM_ERROR("failed to power on dsi\n");
0798 }
0799 
0800 static void mtk_dsi_bridge_atomic_post_disable(struct drm_bridge *bridge,
0801                            struct drm_bridge_state *old_bridge_state)
0802 {
0803     struct mtk_dsi *dsi = bridge_to_dsi(bridge);
0804 
0805     mtk_dsi_poweroff(dsi);
0806 }
0807 
0808 static const struct drm_bridge_funcs mtk_dsi_bridge_funcs = {
0809     .attach = mtk_dsi_bridge_attach,
0810     .atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
0811     .atomic_disable = mtk_dsi_bridge_atomic_disable,
0812     .atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
0813     .atomic_enable = mtk_dsi_bridge_atomic_enable,
0814     .atomic_pre_enable = mtk_dsi_bridge_atomic_pre_enable,
0815     .atomic_post_disable = mtk_dsi_bridge_atomic_post_disable,
0816     .atomic_reset = drm_atomic_helper_bridge_reset,
0817     .mode_set = mtk_dsi_bridge_mode_set,
0818 };
0819 
0820 void mtk_dsi_ddp_start(struct device *dev)
0821 {
0822     struct mtk_dsi *dsi = dev_get_drvdata(dev);
0823 
0824     mtk_dsi_poweron(dsi);
0825 }
0826 
0827 void mtk_dsi_ddp_stop(struct device *dev)
0828 {
0829     struct mtk_dsi *dsi = dev_get_drvdata(dev);
0830 
0831     mtk_dsi_poweroff(dsi);
0832 }
0833 
0834 static int mtk_dsi_encoder_init(struct drm_device *drm, struct mtk_dsi *dsi)
0835 {
0836     int ret;
0837 
0838     ret = drm_simple_encoder_init(drm, &dsi->encoder,
0839                       DRM_MODE_ENCODER_DSI);
0840     if (ret) {
0841         DRM_ERROR("Failed to encoder init to drm\n");
0842         return ret;
0843     }
0844 
0845     dsi->encoder.possible_crtcs = mtk_drm_find_possible_crtc_by_comp(drm, dsi->host.dev);
0846 
0847     ret = drm_bridge_attach(&dsi->encoder, &dsi->bridge, NULL,
0848                 DRM_BRIDGE_ATTACH_NO_CONNECTOR);
0849     if (ret)
0850         goto err_cleanup_encoder;
0851 
0852     dsi->connector = drm_bridge_connector_init(drm, &dsi->encoder);
0853     if (IS_ERR(dsi->connector)) {
0854         DRM_ERROR("Unable to create bridge connector\n");
0855         ret = PTR_ERR(dsi->connector);
0856         goto err_cleanup_encoder;
0857     }
0858     drm_connector_attach_encoder(dsi->connector, &dsi->encoder);
0859 
0860     return 0;
0861 
0862 err_cleanup_encoder:
0863     drm_encoder_cleanup(&dsi->encoder);
0864     return ret;
0865 }
0866 
0867 static int mtk_dsi_bind(struct device *dev, struct device *master, void *data)
0868 {
0869     int ret;
0870     struct drm_device *drm = data;
0871     struct mtk_dsi *dsi = dev_get_drvdata(dev);
0872 
0873     ret = mtk_dsi_encoder_init(drm, dsi);
0874     if (ret)
0875         return ret;
0876 
0877     return device_reset_optional(dev);
0878 }
0879 
0880 static void mtk_dsi_unbind(struct device *dev, struct device *master,
0881                void *data)
0882 {
0883     struct mtk_dsi *dsi = dev_get_drvdata(dev);
0884 
0885     drm_encoder_cleanup(&dsi->encoder);
0886 }
0887 
0888 static const struct component_ops mtk_dsi_component_ops = {
0889     .bind = mtk_dsi_bind,
0890     .unbind = mtk_dsi_unbind,
0891 };
0892 
0893 static int mtk_dsi_host_attach(struct mipi_dsi_host *host,
0894                    struct mipi_dsi_device *device)
0895 {
0896     struct mtk_dsi *dsi = host_to_dsi(host);
0897     struct device *dev = host->dev;
0898     int ret;
0899 
0900     dsi->lanes = device->lanes;
0901     dsi->format = device->format;
0902     dsi->mode_flags = device->mode_flags;
0903     dsi->next_bridge = devm_drm_of_get_bridge(dev, dev->of_node, 0, 0);
0904     if (IS_ERR(dsi->next_bridge))
0905         return PTR_ERR(dsi->next_bridge);
0906 
0907     drm_bridge_add(&dsi->bridge);
0908 
0909     ret = component_add(host->dev, &mtk_dsi_component_ops);
0910     if (ret) {
0911         DRM_ERROR("failed to add dsi_host component: %d\n", ret);
0912         drm_bridge_remove(&dsi->bridge);
0913         return ret;
0914     }
0915 
0916     return 0;
0917 }
0918 
0919 static int mtk_dsi_host_detach(struct mipi_dsi_host *host,
0920                    struct mipi_dsi_device *device)
0921 {
0922     struct mtk_dsi *dsi = host_to_dsi(host);
0923 
0924     component_del(host->dev, &mtk_dsi_component_ops);
0925     drm_bridge_remove(&dsi->bridge);
0926     return 0;
0927 }
0928 
0929 static void mtk_dsi_wait_for_idle(struct mtk_dsi *dsi)
0930 {
0931     int ret;
0932     u32 val;
0933 
0934     ret = readl_poll_timeout(dsi->regs + DSI_INTSTA, val, !(val & DSI_BUSY),
0935                  4, 2000000);
0936     if (ret) {
0937         DRM_WARN("polling dsi wait not busy timeout!\n");
0938 
0939         mtk_dsi_enable(dsi);
0940         mtk_dsi_reset_engine(dsi);
0941     }
0942 }
0943 
0944 static u32 mtk_dsi_recv_cnt(u8 type, u8 *read_data)
0945 {
0946     switch (type) {
0947     case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_1BYTE:
0948     case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_1BYTE:
0949         return 1;
0950     case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_2BYTE:
0951     case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_2BYTE:
0952         return 2;
0953     case MIPI_DSI_RX_GENERIC_LONG_READ_RESPONSE:
0954     case MIPI_DSI_RX_DCS_LONG_READ_RESPONSE:
0955         return read_data[1] + read_data[2] * 16;
0956     case MIPI_DSI_RX_ACKNOWLEDGE_AND_ERROR_REPORT:
0957         DRM_INFO("type is 0x02, try again\n");
0958         break;
0959     default:
0960         DRM_INFO("type(0x%x) not recognized\n", type);
0961         break;
0962     }
0963 
0964     return 0;
0965 }
0966 
0967 static void mtk_dsi_cmdq(struct mtk_dsi *dsi, const struct mipi_dsi_msg *msg)
0968 {
0969     const char *tx_buf = msg->tx_buf;
0970     u8 config, cmdq_size, cmdq_off, type = msg->type;
0971     u32 reg_val, cmdq_mask, i;
0972     u32 reg_cmdq_off = dsi->driver_data->reg_cmdq_off;
0973 
0974     if (MTK_DSI_HOST_IS_READ(type))
0975         config = BTA;
0976     else
0977         config = (msg->tx_len > 2) ? LONG_PACKET : SHORT_PACKET;
0978 
0979     if (msg->tx_len > 2) {
0980         cmdq_size = 1 + (msg->tx_len + 3) / 4;
0981         cmdq_off = 4;
0982         cmdq_mask = CONFIG | DATA_ID | DATA_0 | DATA_1;
0983         reg_val = (msg->tx_len << 16) | (type << 8) | config;
0984     } else {
0985         cmdq_size = 1;
0986         cmdq_off = 2;
0987         cmdq_mask = CONFIG | DATA_ID;
0988         reg_val = (type << 8) | config;
0989     }
0990 
0991     for (i = 0; i < msg->tx_len; i++)
0992         mtk_dsi_mask(dsi, (reg_cmdq_off + cmdq_off + i) & (~0x3U),
0993                  (0xffUL << (((i + cmdq_off) & 3U) * 8U)),
0994                  tx_buf[i] << (((i + cmdq_off) & 3U) * 8U));
0995 
0996     mtk_dsi_mask(dsi, reg_cmdq_off, cmdq_mask, reg_val);
0997     mtk_dsi_mask(dsi, DSI_CMDQ_SIZE, CMDQ_SIZE, cmdq_size);
0998 }
0999 
1000 static ssize_t mtk_dsi_host_send_cmd(struct mtk_dsi *dsi,
1001                      const struct mipi_dsi_msg *msg, u8 flag)
1002 {
1003     mtk_dsi_wait_for_idle(dsi);
1004     mtk_dsi_irq_data_clear(dsi, flag);
1005     mtk_dsi_cmdq(dsi, msg);
1006     mtk_dsi_start(dsi);
1007 
1008     if (!mtk_dsi_wait_for_irq_done(dsi, flag, 2000))
1009         return -ETIME;
1010     else
1011         return 0;
1012 }
1013 
1014 static ssize_t mtk_dsi_host_transfer(struct mipi_dsi_host *host,
1015                      const struct mipi_dsi_msg *msg)
1016 {
1017     struct mtk_dsi *dsi = host_to_dsi(host);
1018     u32 recv_cnt, i;
1019     u8 read_data[16];
1020     void *src_addr;
1021     u8 irq_flag = CMD_DONE_INT_FLAG;
1022     u32 dsi_mode;
1023     int ret;
1024 
1025     dsi_mode = readl(dsi->regs + DSI_MODE_CTRL);
1026     if (dsi_mode & MODE) {
1027         mtk_dsi_stop(dsi);
1028         ret = mtk_dsi_switch_to_cmd_mode(dsi, VM_DONE_INT_FLAG, 500);
1029         if (ret)
1030             goto restore_dsi_mode;
1031     }
1032 
1033     if (MTK_DSI_HOST_IS_READ(msg->type))
1034         irq_flag |= LPRX_RD_RDY_INT_FLAG;
1035 
1036     mtk_dsi_lane_ready(dsi);
1037 
1038     ret = mtk_dsi_host_send_cmd(dsi, msg, irq_flag);
1039     if (ret)
1040         goto restore_dsi_mode;
1041 
1042     if (!MTK_DSI_HOST_IS_READ(msg->type)) {
1043         recv_cnt = 0;
1044         goto restore_dsi_mode;
1045     }
1046 
1047     if (!msg->rx_buf) {
1048         DRM_ERROR("dsi receive buffer size may be NULL\n");
1049         ret = -EINVAL;
1050         goto restore_dsi_mode;
1051     }
1052 
1053     for (i = 0; i < 16; i++)
1054         *(read_data + i) = readb(dsi->regs + DSI_RX_DATA0 + i);
1055 
1056     recv_cnt = mtk_dsi_recv_cnt(read_data[0], read_data);
1057 
1058     if (recv_cnt > 2)
1059         src_addr = &read_data[4];
1060     else
1061         src_addr = &read_data[1];
1062 
1063     if (recv_cnt > 10)
1064         recv_cnt = 10;
1065 
1066     if (recv_cnt > msg->rx_len)
1067         recv_cnt = msg->rx_len;
1068 
1069     if (recv_cnt)
1070         memcpy(msg->rx_buf, src_addr, recv_cnt);
1071 
1072     DRM_INFO("dsi get %d byte data from the panel address(0x%x)\n",
1073          recv_cnt, *((u8 *)(msg->tx_buf)));
1074 
1075 restore_dsi_mode:
1076     if (dsi_mode & MODE) {
1077         mtk_dsi_set_mode(dsi);
1078         mtk_dsi_start(dsi);
1079     }
1080 
1081     return ret < 0 ? ret : recv_cnt;
1082 }
1083 
1084 static const struct mipi_dsi_host_ops mtk_dsi_ops = {
1085     .attach = mtk_dsi_host_attach,
1086     .detach = mtk_dsi_host_detach,
1087     .transfer = mtk_dsi_host_transfer,
1088 };
1089 
1090 static int mtk_dsi_probe(struct platform_device *pdev)
1091 {
1092     struct mtk_dsi *dsi;
1093     struct device *dev = &pdev->dev;
1094     struct resource *regs;
1095     int irq_num;
1096     int ret;
1097 
1098     dsi = devm_kzalloc(dev, sizeof(*dsi), GFP_KERNEL);
1099     if (!dsi)
1100         return -ENOMEM;
1101 
1102     dsi->host.ops = &mtk_dsi_ops;
1103     dsi->host.dev = dev;
1104     ret = mipi_dsi_host_register(&dsi->host);
1105     if (ret < 0) {
1106         dev_err(dev, "failed to register DSI host: %d\n", ret);
1107         return ret;
1108     }
1109 
1110     dsi->driver_data = of_device_get_match_data(dev);
1111 
1112     dsi->engine_clk = devm_clk_get(dev, "engine");
1113     if (IS_ERR(dsi->engine_clk)) {
1114         ret = PTR_ERR(dsi->engine_clk);
1115 
1116         if (ret != -EPROBE_DEFER)
1117             dev_err(dev, "Failed to get engine clock: %d\n", ret);
1118         goto err_unregister_host;
1119     }
1120 
1121     dsi->digital_clk = devm_clk_get(dev, "digital");
1122     if (IS_ERR(dsi->digital_clk)) {
1123         ret = PTR_ERR(dsi->digital_clk);
1124 
1125         if (ret != -EPROBE_DEFER)
1126             dev_err(dev, "Failed to get digital clock: %d\n", ret);
1127         goto err_unregister_host;
1128     }
1129 
1130     dsi->hs_clk = devm_clk_get(dev, "hs");
1131     if (IS_ERR(dsi->hs_clk)) {
1132         ret = PTR_ERR(dsi->hs_clk);
1133         dev_err(dev, "Failed to get hs clock: %d\n", ret);
1134         goto err_unregister_host;
1135     }
1136 
1137     regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1138     dsi->regs = devm_ioremap_resource(dev, regs);
1139     if (IS_ERR(dsi->regs)) {
1140         ret = PTR_ERR(dsi->regs);
1141         dev_err(dev, "Failed to ioremap memory: %d\n", ret);
1142         goto err_unregister_host;
1143     }
1144 
1145     dsi->phy = devm_phy_get(dev, "dphy");
1146     if (IS_ERR(dsi->phy)) {
1147         ret = PTR_ERR(dsi->phy);
1148         dev_err(dev, "Failed to get MIPI-DPHY: %d\n", ret);
1149         goto err_unregister_host;
1150     }
1151 
1152     irq_num = platform_get_irq(pdev, 0);
1153     if (irq_num < 0) {
1154         ret = irq_num;
1155         goto err_unregister_host;
1156     }
1157 
1158     ret = devm_request_irq(&pdev->dev, irq_num, mtk_dsi_irq,
1159                    IRQF_TRIGGER_NONE, dev_name(&pdev->dev), dsi);
1160     if (ret) {
1161         dev_err(&pdev->dev, "failed to request mediatek dsi irq\n");
1162         goto err_unregister_host;
1163     }
1164 
1165     init_waitqueue_head(&dsi->irq_wait_queue);
1166 
1167     platform_set_drvdata(pdev, dsi);
1168 
1169     dsi->bridge.funcs = &mtk_dsi_bridge_funcs;
1170     dsi->bridge.of_node = dev->of_node;
1171     dsi->bridge.type = DRM_MODE_CONNECTOR_DSI;
1172 
1173     return 0;
1174 
1175 err_unregister_host:
1176     mipi_dsi_host_unregister(&dsi->host);
1177     return ret;
1178 }
1179 
1180 static int mtk_dsi_remove(struct platform_device *pdev)
1181 {
1182     struct mtk_dsi *dsi = platform_get_drvdata(pdev);
1183 
1184     mtk_output_dsi_disable(dsi);
1185     mipi_dsi_host_unregister(&dsi->host);
1186 
1187     return 0;
1188 }
1189 
1190 static const struct mtk_dsi_driver_data mt8173_dsi_driver_data = {
1191     .reg_cmdq_off = 0x200,
1192 };
1193 
1194 static const struct mtk_dsi_driver_data mt2701_dsi_driver_data = {
1195     .reg_cmdq_off = 0x180,
1196 };
1197 
1198 static const struct mtk_dsi_driver_data mt8183_dsi_driver_data = {
1199     .reg_cmdq_off = 0x200,
1200     .has_shadow_ctl = true,
1201     .has_size_ctl = true,
1202 };
1203 
1204 static const struct mtk_dsi_driver_data mt8186_dsi_driver_data = {
1205     .reg_cmdq_off = 0xd00,
1206     .has_shadow_ctl = true,
1207     .has_size_ctl = true,
1208 };
1209 
1210 static const struct of_device_id mtk_dsi_of_match[] = {
1211     { .compatible = "mediatek,mt2701-dsi",
1212       .data = &mt2701_dsi_driver_data },
1213     { .compatible = "mediatek,mt8173-dsi",
1214       .data = &mt8173_dsi_driver_data },
1215     { .compatible = "mediatek,mt8183-dsi",
1216       .data = &mt8183_dsi_driver_data },
1217     { .compatible = "mediatek,mt8186-dsi",
1218       .data = &mt8186_dsi_driver_data },
1219     { },
1220 };
1221 MODULE_DEVICE_TABLE(of, mtk_dsi_of_match);
1222 
1223 struct platform_driver mtk_dsi_driver = {
1224     .probe = mtk_dsi_probe,
1225     .remove = mtk_dsi_remove,
1226     .driver = {
1227         .name = "mtk-dsi",
1228         .of_match_table = mtk_dsi_of_match,
1229     },
1230 };