Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * Copyright (c) 2016, Fuzhou Rockchip Electronics Co., Ltd
0004  * Copyright (C) STMicroelectronics SA 2017
0005  *
0006  * Modified by Philippe Cornu <philippe.cornu@st.com>
0007  * This generic Synopsys DesignWare MIPI DSI host driver is based on the
0008  * Rockchip version from rockchip/dw-mipi-dsi.c with phy & bridge APIs.
0009  */
0010 
0011 #include <linux/clk.h>
0012 #include <linux/component.h>
0013 #include <linux/debugfs.h>
0014 #include <linux/iopoll.h>
0015 #include <linux/module.h>
0016 #include <linux/of_device.h>
0017 #include <linux/pm_runtime.h>
0018 #include <linux/reset.h>
0019 
0020 #include <video/mipi_display.h>
0021 
0022 #include <drm/bridge/dw_mipi_dsi.h>
0023 #include <drm/drm_atomic_helper.h>
0024 #include <drm/drm_bridge.h>
0025 #include <drm/drm_crtc.h>
0026 #include <drm/drm_mipi_dsi.h>
0027 #include <drm/drm_modes.h>
0028 #include <drm/drm_of.h>
0029 #include <drm/drm_print.h>
0030 
0031 #define HWVER_131           0x31333100  /* IP version 1.31 */
0032 
0033 #define DSI_VERSION         0x00
0034 #define VERSION             GENMASK(31, 8)
0035 
0036 #define DSI_PWR_UP          0x04
0037 #define RESET               0
0038 #define POWERUP             BIT(0)
0039 
0040 #define DSI_CLKMGR_CFG          0x08
0041 #define TO_CLK_DIVISION(div)        (((div) & 0xff) << 8)
0042 #define TX_ESC_CLK_DIVISION(div)    ((div) & 0xff)
0043 
0044 #define DSI_DPI_VCID            0x0c
0045 #define DPI_VCID(vcid)          ((vcid) & 0x3)
0046 
0047 #define DSI_DPI_COLOR_CODING        0x10
0048 #define LOOSELY18_EN            BIT(8)
0049 #define DPI_COLOR_CODING_16BIT_1    0x0
0050 #define DPI_COLOR_CODING_16BIT_2    0x1
0051 #define DPI_COLOR_CODING_16BIT_3    0x2
0052 #define DPI_COLOR_CODING_18BIT_1    0x3
0053 #define DPI_COLOR_CODING_18BIT_2    0x4
0054 #define DPI_COLOR_CODING_24BIT      0x5
0055 
0056 #define DSI_DPI_CFG_POL         0x14
0057 #define COLORM_ACTIVE_LOW       BIT(4)
0058 #define SHUTD_ACTIVE_LOW        BIT(3)
0059 #define HSYNC_ACTIVE_LOW        BIT(2)
0060 #define VSYNC_ACTIVE_LOW        BIT(1)
0061 #define DATAEN_ACTIVE_LOW       BIT(0)
0062 
0063 #define DSI_DPI_LP_CMD_TIM      0x18
0064 #define OUTVACT_LPCMD_TIME(p)       (((p) & 0xff) << 16)
0065 #define INVACT_LPCMD_TIME(p)        ((p) & 0xff)
0066 
0067 #define DSI_DBI_VCID            0x1c
0068 #define DSI_DBI_CFG         0x20
0069 #define DSI_DBI_PARTITIONING_EN     0x24
0070 #define DSI_DBI_CMDSIZE         0x28
0071 
0072 #define DSI_PCKHDL_CFG          0x2c
0073 #define CRC_RX_EN           BIT(4)
0074 #define ECC_RX_EN           BIT(3)
0075 #define BTA_EN              BIT(2)
0076 #define EOTP_RX_EN          BIT(1)
0077 #define EOTP_TX_EN          BIT(0)
0078 
0079 #define DSI_GEN_VCID            0x30
0080 
0081 #define DSI_MODE_CFG            0x34
0082 #define ENABLE_VIDEO_MODE       0
0083 #define ENABLE_CMD_MODE         BIT(0)
0084 
0085 #define DSI_VID_MODE_CFG        0x38
0086 #define ENABLE_LOW_POWER        (0x3f << 8)
0087 #define ENABLE_LOW_POWER_MASK       (0x3f << 8)
0088 #define VID_MODE_TYPE_NON_BURST_SYNC_PULSES 0x0
0089 #define VID_MODE_TYPE_NON_BURST_SYNC_EVENTS 0x1
0090 #define VID_MODE_TYPE_BURST         0x2
0091 #define VID_MODE_TYPE_MASK          0x3
0092 #define ENABLE_LOW_POWER_CMD        BIT(15)
0093 #define VID_MODE_VPG_ENABLE     BIT(16)
0094 #define VID_MODE_VPG_MODE       BIT(20)
0095 #define VID_MODE_VPG_HORIZONTAL     BIT(24)
0096 
0097 #define DSI_VID_PKT_SIZE        0x3c
0098 #define VID_PKT_SIZE(p)         ((p) & 0x3fff)
0099 
0100 #define DSI_VID_NUM_CHUNKS      0x40
0101 #define VID_NUM_CHUNKS(c)       ((c) & 0x1fff)
0102 
0103 #define DSI_VID_NULL_SIZE       0x44
0104 #define VID_NULL_SIZE(b)        ((b) & 0x1fff)
0105 
0106 #define DSI_VID_HSA_TIME        0x48
0107 #define DSI_VID_HBP_TIME        0x4c
0108 #define DSI_VID_HLINE_TIME      0x50
0109 #define DSI_VID_VSA_LINES       0x54
0110 #define DSI_VID_VBP_LINES       0x58
0111 #define DSI_VID_VFP_LINES       0x5c
0112 #define DSI_VID_VACTIVE_LINES       0x60
0113 #define DSI_EDPI_CMD_SIZE       0x64
0114 
0115 #define DSI_CMD_MODE_CFG        0x68
0116 #define MAX_RD_PKT_SIZE_LP      BIT(24)
0117 #define DCS_LW_TX_LP            BIT(19)
0118 #define DCS_SR_0P_TX_LP         BIT(18)
0119 #define DCS_SW_1P_TX_LP         BIT(17)
0120 #define DCS_SW_0P_TX_LP         BIT(16)
0121 #define GEN_LW_TX_LP            BIT(14)
0122 #define GEN_SR_2P_TX_LP         BIT(13)
0123 #define GEN_SR_1P_TX_LP         BIT(12)
0124 #define GEN_SR_0P_TX_LP         BIT(11)
0125 #define GEN_SW_2P_TX_LP         BIT(10)
0126 #define GEN_SW_1P_TX_LP         BIT(9)
0127 #define GEN_SW_0P_TX_LP         BIT(8)
0128 #define ACK_RQST_EN         BIT(1)
0129 #define TEAR_FX_EN          BIT(0)
0130 
0131 #define CMD_MODE_ALL_LP         (MAX_RD_PKT_SIZE_LP | \
0132                      DCS_LW_TX_LP | \
0133                      DCS_SR_0P_TX_LP | \
0134                      DCS_SW_1P_TX_LP | \
0135                      DCS_SW_0P_TX_LP | \
0136                      GEN_LW_TX_LP | \
0137                      GEN_SR_2P_TX_LP | \
0138                      GEN_SR_1P_TX_LP | \
0139                      GEN_SR_0P_TX_LP | \
0140                      GEN_SW_2P_TX_LP | \
0141                      GEN_SW_1P_TX_LP | \
0142                      GEN_SW_0P_TX_LP)
0143 
0144 #define DSI_GEN_HDR         0x6c
0145 #define DSI_GEN_PLD_DATA        0x70
0146 
0147 #define DSI_CMD_PKT_STATUS      0x74
0148 #define GEN_RD_CMD_BUSY         BIT(6)
0149 #define GEN_PLD_R_FULL          BIT(5)
0150 #define GEN_PLD_R_EMPTY         BIT(4)
0151 #define GEN_PLD_W_FULL          BIT(3)
0152 #define GEN_PLD_W_EMPTY         BIT(2)
0153 #define GEN_CMD_FULL            BIT(1)
0154 #define GEN_CMD_EMPTY           BIT(0)
0155 
0156 #define DSI_TO_CNT_CFG          0x78
0157 #define HSTX_TO_CNT(p)          (((p) & 0xffff) << 16)
0158 #define LPRX_TO_CNT(p)          ((p) & 0xffff)
0159 
0160 #define DSI_HS_RD_TO_CNT        0x7c
0161 #define DSI_LP_RD_TO_CNT        0x80
0162 #define DSI_HS_WR_TO_CNT        0x84
0163 #define DSI_LP_WR_TO_CNT        0x88
0164 #define DSI_BTA_TO_CNT          0x8c
0165 
0166 #define DSI_LPCLK_CTRL          0x94
0167 #define AUTO_CLKLANE_CTRL       BIT(1)
0168 #define PHY_TXREQUESTCLKHS      BIT(0)
0169 
0170 #define DSI_PHY_TMR_LPCLK_CFG       0x98
0171 #define PHY_CLKHS2LP_TIME(lbcc)     (((lbcc) & 0x3ff) << 16)
0172 #define PHY_CLKLP2HS_TIME(lbcc)     ((lbcc) & 0x3ff)
0173 
0174 #define DSI_PHY_TMR_CFG         0x9c
0175 #define PHY_HS2LP_TIME(lbcc)        (((lbcc) & 0xff) << 24)
0176 #define PHY_LP2HS_TIME(lbcc)        (((lbcc) & 0xff) << 16)
0177 #define MAX_RD_TIME(lbcc)       ((lbcc) & 0x7fff)
0178 #define PHY_HS2LP_TIME_V131(lbcc)   (((lbcc) & 0x3ff) << 16)
0179 #define PHY_LP2HS_TIME_V131(lbcc)   ((lbcc) & 0x3ff)
0180 
0181 #define DSI_PHY_RSTZ            0xa0
0182 #define PHY_DISFORCEPLL         0
0183 #define PHY_ENFORCEPLL          BIT(3)
0184 #define PHY_DISABLECLK          0
0185 #define PHY_ENABLECLK           BIT(2)
0186 #define PHY_RSTZ            0
0187 #define PHY_UNRSTZ          BIT(1)
0188 #define PHY_SHUTDOWNZ           0
0189 #define PHY_UNSHUTDOWNZ         BIT(0)
0190 
0191 #define DSI_PHY_IF_CFG          0xa4
0192 #define PHY_STOP_WAIT_TIME(cycle)   (((cycle) & 0xff) << 8)
0193 #define N_LANES(n)          (((n) - 1) & 0x3)
0194 
0195 #define DSI_PHY_ULPS_CTRL       0xa8
0196 #define DSI_PHY_TX_TRIGGERS     0xac
0197 
0198 #define DSI_PHY_STATUS          0xb0
0199 #define PHY_STOP_STATE_CLK_LANE     BIT(2)
0200 #define PHY_LOCK            BIT(0)
0201 
0202 #define DSI_PHY_TST_CTRL0       0xb4
0203 #define PHY_TESTCLK         BIT(1)
0204 #define PHY_UNTESTCLK           0
0205 #define PHY_TESTCLR         BIT(0)
0206 #define PHY_UNTESTCLR           0
0207 
0208 #define DSI_PHY_TST_CTRL1       0xb8
0209 #define PHY_TESTEN          BIT(16)
0210 #define PHY_UNTESTEN            0
0211 #define PHY_TESTDOUT(n)         (((n) & 0xff) << 8)
0212 #define PHY_TESTDIN(n)          ((n) & 0xff)
0213 
0214 #define DSI_INT_ST0         0xbc
0215 #define DSI_INT_ST1         0xc0
0216 #define DSI_INT_MSK0            0xc4
0217 #define DSI_INT_MSK1            0xc8
0218 
0219 #define DSI_PHY_TMR_RD_CFG      0xf4
0220 #define MAX_RD_TIME_V131(lbcc)      ((lbcc) & 0x7fff)
0221 
0222 #define PHY_STATUS_TIMEOUT_US       10000
0223 #define CMD_PKT_STATUS_TIMEOUT_US   20000
0224 
0225 #ifdef CONFIG_DEBUG_FS
0226 #define VPG_DEFS(name, dsi) \
0227     ((void __force *)&((*dsi).vpg_defs.name))
0228 
0229 #define REGISTER(name, mask, dsi) \
0230     { #name, VPG_DEFS(name, dsi), mask, dsi }
0231 
0232 struct debugfs_entries {
0233     const char              *name;
0234     bool                    *reg;
0235     u32                 mask;
0236     struct dw_mipi_dsi          *dsi;
0237 };
0238 #endif /* CONFIG_DEBUG_FS */
0239 
0240 struct dw_mipi_dsi {
0241     struct drm_bridge bridge;
0242     struct mipi_dsi_host dsi_host;
0243     struct drm_bridge *panel_bridge;
0244     struct device *dev;
0245     void __iomem *base;
0246 
0247     struct clk *pclk;
0248 
0249     unsigned int lane_mbps; /* per lane */
0250     u32 channel;
0251     u32 lanes;
0252     u32 format;
0253     unsigned long mode_flags;
0254 
0255 #ifdef CONFIG_DEBUG_FS
0256     struct dentry *debugfs;
0257     struct debugfs_entries *debugfs_vpg;
0258     struct {
0259         bool vpg;
0260         bool vpg_horizontal;
0261         bool vpg_ber_pattern;
0262     } vpg_defs;
0263 #endif /* CONFIG_DEBUG_FS */
0264 
0265     struct dw_mipi_dsi *master; /* dual-dsi master ptr */
0266     struct dw_mipi_dsi *slave; /* dual-dsi slave ptr */
0267 
0268     const struct dw_mipi_dsi_plat_data *plat_data;
0269 };
0270 
0271 /*
0272  * Check if either a link to a master or slave is present
0273  */
0274 static inline bool dw_mipi_is_dual_mode(struct dw_mipi_dsi *dsi)
0275 {
0276     return dsi->slave || dsi->master;
0277 }
0278 
0279 /*
0280  * The controller should generate 2 frames before
0281  * preparing the peripheral.
0282  */
0283 static void dw_mipi_dsi_wait_for_two_frames(const struct drm_display_mode *mode)
0284 {
0285     int refresh, two_frames;
0286 
0287     refresh = drm_mode_vrefresh(mode);
0288     two_frames = DIV_ROUND_UP(MSEC_PER_SEC, refresh) * 2;
0289     msleep(two_frames);
0290 }
0291 
0292 static inline struct dw_mipi_dsi *host_to_dsi(struct mipi_dsi_host *host)
0293 {
0294     return container_of(host, struct dw_mipi_dsi, dsi_host);
0295 }
0296 
0297 static inline struct dw_mipi_dsi *bridge_to_dsi(struct drm_bridge *bridge)
0298 {
0299     return container_of(bridge, struct dw_mipi_dsi, bridge);
0300 }
0301 
0302 static inline void dsi_write(struct dw_mipi_dsi *dsi, u32 reg, u32 val)
0303 {
0304     writel(val, dsi->base + reg);
0305 }
0306 
0307 static inline u32 dsi_read(struct dw_mipi_dsi *dsi, u32 reg)
0308 {
0309     return readl(dsi->base + reg);
0310 }
0311 
0312 static int dw_mipi_dsi_host_attach(struct mipi_dsi_host *host,
0313                    struct mipi_dsi_device *device)
0314 {
0315     struct dw_mipi_dsi *dsi = host_to_dsi(host);
0316     const struct dw_mipi_dsi_plat_data *pdata = dsi->plat_data;
0317     struct drm_bridge *bridge;
0318     int ret;
0319 
0320     if (device->lanes > dsi->plat_data->max_data_lanes) {
0321         dev_err(dsi->dev, "the number of data lanes(%u) is too many\n",
0322             device->lanes);
0323         return -EINVAL;
0324     }
0325 
0326     dsi->lanes = device->lanes;
0327     dsi->channel = device->channel;
0328     dsi->format = device->format;
0329     dsi->mode_flags = device->mode_flags;
0330 
0331     bridge = devm_drm_of_get_bridge(dsi->dev, dsi->dev->of_node, 1, 0);
0332     if (IS_ERR(bridge))
0333         return PTR_ERR(bridge);
0334 
0335     dsi->panel_bridge = bridge;
0336 
0337     drm_bridge_add(&dsi->bridge);
0338 
0339     if (pdata->host_ops && pdata->host_ops->attach) {
0340         ret = pdata->host_ops->attach(pdata->priv_data, device);
0341         if (ret < 0)
0342             return ret;
0343     }
0344 
0345     return 0;
0346 }
0347 
0348 static int dw_mipi_dsi_host_detach(struct mipi_dsi_host *host,
0349                    struct mipi_dsi_device *device)
0350 {
0351     struct dw_mipi_dsi *dsi = host_to_dsi(host);
0352     const struct dw_mipi_dsi_plat_data *pdata = dsi->plat_data;
0353     int ret;
0354 
0355     if (pdata->host_ops && pdata->host_ops->detach) {
0356         ret = pdata->host_ops->detach(pdata->priv_data, device);
0357         if (ret < 0)
0358             return ret;
0359     }
0360 
0361     drm_of_panel_bridge_remove(host->dev->of_node, 1, 0);
0362 
0363     drm_bridge_remove(&dsi->bridge);
0364 
0365     return 0;
0366 }
0367 
0368 static void dw_mipi_message_config(struct dw_mipi_dsi *dsi,
0369                    const struct mipi_dsi_msg *msg)
0370 {
0371     bool lpm = msg->flags & MIPI_DSI_MSG_USE_LPM;
0372     u32 val = 0;
0373 
0374     /*
0375      * TODO dw drv improvements
0376      * largest packet sizes during hfp or during vsa/vpb/vfp
0377      * should be computed according to byte lane, lane number and only
0378      * if sending lp cmds in high speed is enable (PHY_TXREQUESTCLKHS)
0379      */
0380     dsi_write(dsi, DSI_DPI_LP_CMD_TIM, OUTVACT_LPCMD_TIME(16)
0381           | INVACT_LPCMD_TIME(4));
0382 
0383     if (msg->flags & MIPI_DSI_MSG_REQ_ACK)
0384         val |= ACK_RQST_EN;
0385     if (lpm)
0386         val |= CMD_MODE_ALL_LP;
0387 
0388     dsi_write(dsi, DSI_CMD_MODE_CFG, val);
0389 
0390     val = dsi_read(dsi, DSI_VID_MODE_CFG);
0391     if (lpm)
0392         val |= ENABLE_LOW_POWER_CMD;
0393     else
0394         val &= ~ENABLE_LOW_POWER_CMD;
0395     dsi_write(dsi, DSI_VID_MODE_CFG, val);
0396 }
0397 
0398 static int dw_mipi_dsi_gen_pkt_hdr_write(struct dw_mipi_dsi *dsi, u32 hdr_val)
0399 {
0400     int ret;
0401     u32 val, mask;
0402 
0403     ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
0404                  val, !(val & GEN_CMD_FULL), 1000,
0405                  CMD_PKT_STATUS_TIMEOUT_US);
0406     if (ret) {
0407         dev_err(dsi->dev, "failed to get available command FIFO\n");
0408         return ret;
0409     }
0410 
0411     dsi_write(dsi, DSI_GEN_HDR, hdr_val);
0412 
0413     mask = GEN_CMD_EMPTY | GEN_PLD_W_EMPTY;
0414     ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
0415                  val, (val & mask) == mask,
0416                  1000, CMD_PKT_STATUS_TIMEOUT_US);
0417     if (ret) {
0418         dev_err(dsi->dev, "failed to write command FIFO\n");
0419         return ret;
0420     }
0421 
0422     return 0;
0423 }
0424 
0425 static int dw_mipi_dsi_write(struct dw_mipi_dsi *dsi,
0426                  const struct mipi_dsi_packet *packet)
0427 {
0428     const u8 *tx_buf = packet->payload;
0429     int len = packet->payload_length, pld_data_bytes = sizeof(u32), ret;
0430     __le32 word;
0431     u32 val;
0432 
0433     while (len) {
0434         if (len < pld_data_bytes) {
0435             word = 0;
0436             memcpy(&word, tx_buf, len);
0437             dsi_write(dsi, DSI_GEN_PLD_DATA, le32_to_cpu(word));
0438             len = 0;
0439         } else {
0440             memcpy(&word, tx_buf, pld_data_bytes);
0441             dsi_write(dsi, DSI_GEN_PLD_DATA, le32_to_cpu(word));
0442             tx_buf += pld_data_bytes;
0443             len -= pld_data_bytes;
0444         }
0445 
0446         ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
0447                      val, !(val & GEN_PLD_W_FULL), 1000,
0448                      CMD_PKT_STATUS_TIMEOUT_US);
0449         if (ret) {
0450             dev_err(dsi->dev,
0451                 "failed to get available write payload FIFO\n");
0452             return ret;
0453         }
0454     }
0455 
0456     word = 0;
0457     memcpy(&word, packet->header, sizeof(packet->header));
0458     return dw_mipi_dsi_gen_pkt_hdr_write(dsi, le32_to_cpu(word));
0459 }
0460 
0461 static int dw_mipi_dsi_read(struct dw_mipi_dsi *dsi,
0462                 const struct mipi_dsi_msg *msg)
0463 {
0464     int i, j, ret, len = msg->rx_len;
0465     u8 *buf = msg->rx_buf;
0466     u32 val;
0467 
0468     /* Wait end of the read operation */
0469     ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
0470                  val, !(val & GEN_RD_CMD_BUSY),
0471                  1000, CMD_PKT_STATUS_TIMEOUT_US);
0472     if (ret) {
0473         dev_err(dsi->dev, "Timeout during read operation\n");
0474         return ret;
0475     }
0476 
0477     for (i = 0; i < len; i += 4) {
0478         /* Read fifo must not be empty before all bytes are read */
0479         ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
0480                      val, !(val & GEN_PLD_R_EMPTY),
0481                      1000, CMD_PKT_STATUS_TIMEOUT_US);
0482         if (ret) {
0483             dev_err(dsi->dev, "Read payload FIFO is empty\n");
0484             return ret;
0485         }
0486 
0487         val = dsi_read(dsi, DSI_GEN_PLD_DATA);
0488         for (j = 0; j < 4 && j + i < len; j++)
0489             buf[i + j] = val >> (8 * j);
0490     }
0491 
0492     return ret;
0493 }
0494 
0495 static ssize_t dw_mipi_dsi_host_transfer(struct mipi_dsi_host *host,
0496                      const struct mipi_dsi_msg *msg)
0497 {
0498     struct dw_mipi_dsi *dsi = host_to_dsi(host);
0499     struct mipi_dsi_packet packet;
0500     int ret, nb_bytes;
0501 
0502     ret = mipi_dsi_create_packet(&packet, msg);
0503     if (ret) {
0504         dev_err(dsi->dev, "failed to create packet: %d\n", ret);
0505         return ret;
0506     }
0507 
0508     dw_mipi_message_config(dsi, msg);
0509     if (dsi->slave)
0510         dw_mipi_message_config(dsi->slave, msg);
0511 
0512     ret = dw_mipi_dsi_write(dsi, &packet);
0513     if (ret)
0514         return ret;
0515     if (dsi->slave) {
0516         ret = dw_mipi_dsi_write(dsi->slave, &packet);
0517         if (ret)
0518             return ret;
0519     }
0520 
0521     if (msg->rx_buf && msg->rx_len) {
0522         ret = dw_mipi_dsi_read(dsi, msg);
0523         if (ret)
0524             return ret;
0525         nb_bytes = msg->rx_len;
0526     } else {
0527         nb_bytes = packet.size;
0528     }
0529 
0530     return nb_bytes;
0531 }
0532 
0533 static const struct mipi_dsi_host_ops dw_mipi_dsi_host_ops = {
0534     .attach = dw_mipi_dsi_host_attach,
0535     .detach = dw_mipi_dsi_host_detach,
0536     .transfer = dw_mipi_dsi_host_transfer,
0537 };
0538 
0539 static void dw_mipi_dsi_video_mode_config(struct dw_mipi_dsi *dsi)
0540 {
0541     u32 val;
0542 
0543     /*
0544      * TODO dw drv improvements
0545      * enabling low power is panel-dependent, we should use the
0546      * panel configuration here...
0547      */
0548     val = ENABLE_LOW_POWER;
0549 
0550     if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_BURST)
0551         val |= VID_MODE_TYPE_BURST;
0552     else if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
0553         val |= VID_MODE_TYPE_NON_BURST_SYNC_PULSES;
0554     else
0555         val |= VID_MODE_TYPE_NON_BURST_SYNC_EVENTS;
0556 
0557 #ifdef CONFIG_DEBUG_FS
0558     if (dsi->vpg_defs.vpg) {
0559         val |= VID_MODE_VPG_ENABLE;
0560         val |= dsi->vpg_defs.vpg_horizontal ?
0561                VID_MODE_VPG_HORIZONTAL : 0;
0562         val |= dsi->vpg_defs.vpg_ber_pattern ? VID_MODE_VPG_MODE : 0;
0563     }
0564 #endif /* CONFIG_DEBUG_FS */
0565 
0566     dsi_write(dsi, DSI_VID_MODE_CFG, val);
0567 }
0568 
0569 static void dw_mipi_dsi_set_mode(struct dw_mipi_dsi *dsi,
0570                  unsigned long mode_flags)
0571 {
0572     u32 val;
0573 
0574     dsi_write(dsi, DSI_PWR_UP, RESET);
0575 
0576     if (mode_flags & MIPI_DSI_MODE_VIDEO) {
0577         dsi_write(dsi, DSI_MODE_CFG, ENABLE_VIDEO_MODE);
0578         dw_mipi_dsi_video_mode_config(dsi);
0579     } else {
0580         dsi_write(dsi, DSI_MODE_CFG, ENABLE_CMD_MODE);
0581     }
0582 
0583     val = PHY_TXREQUESTCLKHS;
0584     if (dsi->mode_flags & MIPI_DSI_CLOCK_NON_CONTINUOUS)
0585         val |= AUTO_CLKLANE_CTRL;
0586     dsi_write(dsi, DSI_LPCLK_CTRL, val);
0587 
0588     dsi_write(dsi, DSI_PWR_UP, POWERUP);
0589 }
0590 
0591 static void dw_mipi_dsi_disable(struct dw_mipi_dsi *dsi)
0592 {
0593     dsi_write(dsi, DSI_PWR_UP, RESET);
0594     dsi_write(dsi, DSI_PHY_RSTZ, PHY_RSTZ);
0595 }
0596 
0597 static void dw_mipi_dsi_init(struct dw_mipi_dsi *dsi)
0598 {
0599     const struct dw_mipi_dsi_phy_ops *phy_ops = dsi->plat_data->phy_ops;
0600     unsigned int esc_rate; /* in MHz */
0601     u32 esc_clk_division;
0602     int ret;
0603 
0604     /*
0605      * The maximum permitted escape clock is 20MHz and it is derived from
0606      * lanebyteclk, which is running at "lane_mbps / 8".
0607      */
0608     if (phy_ops->get_esc_clk_rate) {
0609         ret = phy_ops->get_esc_clk_rate(dsi->plat_data->priv_data,
0610                         &esc_rate);
0611         if (ret)
0612             DRM_DEBUG_DRIVER("Phy get_esc_clk_rate() failed\n");
0613     } else
0614         esc_rate = 20; /* Default to 20MHz */
0615 
0616     /*
0617      * We want :
0618      *     (lane_mbps >> 3) / esc_clk_division < X
0619      * which is:
0620      *     (lane_mbps >> 3) / X > esc_clk_division
0621      */
0622     esc_clk_division = (dsi->lane_mbps >> 3) / esc_rate + 1;
0623 
0624     dsi_write(dsi, DSI_PWR_UP, RESET);
0625 
0626     /*
0627      * TODO dw drv improvements
0628      * timeout clock division should be computed with the
0629      * high speed transmission counter timeout and byte lane...
0630      */
0631     dsi_write(dsi, DSI_CLKMGR_CFG, TO_CLK_DIVISION(10) |
0632           TX_ESC_CLK_DIVISION(esc_clk_division));
0633 }
0634 
0635 static void dw_mipi_dsi_dpi_config(struct dw_mipi_dsi *dsi,
0636                    const struct drm_display_mode *mode)
0637 {
0638     u32 val = 0, color = 0;
0639 
0640     switch (dsi->format) {
0641     case MIPI_DSI_FMT_RGB888:
0642         color = DPI_COLOR_CODING_24BIT;
0643         break;
0644     case MIPI_DSI_FMT_RGB666:
0645         color = DPI_COLOR_CODING_18BIT_2 | LOOSELY18_EN;
0646         break;
0647     case MIPI_DSI_FMT_RGB666_PACKED:
0648         color = DPI_COLOR_CODING_18BIT_1;
0649         break;
0650     case MIPI_DSI_FMT_RGB565:
0651         color = DPI_COLOR_CODING_16BIT_1;
0652         break;
0653     }
0654 
0655     if (mode->flags & DRM_MODE_FLAG_NVSYNC)
0656         val |= VSYNC_ACTIVE_LOW;
0657     if (mode->flags & DRM_MODE_FLAG_NHSYNC)
0658         val |= HSYNC_ACTIVE_LOW;
0659 
0660     dsi_write(dsi, DSI_DPI_VCID, DPI_VCID(dsi->channel));
0661     dsi_write(dsi, DSI_DPI_COLOR_CODING, color);
0662     dsi_write(dsi, DSI_DPI_CFG_POL, val);
0663 }
0664 
0665 static void dw_mipi_dsi_packet_handler_config(struct dw_mipi_dsi *dsi)
0666 {
0667     dsi_write(dsi, DSI_PCKHDL_CFG, CRC_RX_EN | ECC_RX_EN | BTA_EN);
0668 }
0669 
0670 static void dw_mipi_dsi_video_packet_config(struct dw_mipi_dsi *dsi,
0671                         const struct drm_display_mode *mode)
0672 {
0673     /*
0674      * TODO dw drv improvements
0675      * only burst mode is supported here. For non-burst video modes,
0676      * we should compute DSI_VID_PKT_SIZE, DSI_VCCR.NUMC &
0677      * DSI_VNPCR.NPSIZE... especially because this driver supports
0678      * non-burst video modes, see dw_mipi_dsi_video_mode_config()...
0679      */
0680 
0681     dsi_write(dsi, DSI_VID_PKT_SIZE,
0682                dw_mipi_is_dual_mode(dsi) ?
0683                 VID_PKT_SIZE(mode->hdisplay / 2) :
0684                 VID_PKT_SIZE(mode->hdisplay));
0685 }
0686 
0687 static void dw_mipi_dsi_command_mode_config(struct dw_mipi_dsi *dsi)
0688 {
0689     /*
0690      * TODO dw drv improvements
0691      * compute high speed transmission counter timeout according
0692      * to the timeout clock division (TO_CLK_DIVISION) and byte lane...
0693      */
0694     dsi_write(dsi, DSI_TO_CNT_CFG, HSTX_TO_CNT(1000) | LPRX_TO_CNT(1000));
0695     /*
0696      * TODO dw drv improvements
0697      * the Bus-Turn-Around Timeout Counter should be computed
0698      * according to byte lane...
0699      */
0700     dsi_write(dsi, DSI_BTA_TO_CNT, 0xd00);
0701     dsi_write(dsi, DSI_MODE_CFG, ENABLE_CMD_MODE);
0702 }
0703 
0704 /* Get lane byte clock cycles. */
0705 static u32 dw_mipi_dsi_get_hcomponent_lbcc(struct dw_mipi_dsi *dsi,
0706                        const struct drm_display_mode *mode,
0707                        u32 hcomponent)
0708 {
0709     u32 frac, lbcc;
0710 
0711     lbcc = hcomponent * dsi->lane_mbps * MSEC_PER_SEC / 8;
0712 
0713     frac = lbcc % mode->clock;
0714     lbcc = lbcc / mode->clock;
0715     if (frac)
0716         lbcc++;
0717 
0718     return lbcc;
0719 }
0720 
0721 static void dw_mipi_dsi_line_timer_config(struct dw_mipi_dsi *dsi,
0722                       const struct drm_display_mode *mode)
0723 {
0724     u32 htotal, hsa, hbp, lbcc;
0725 
0726     htotal = mode->htotal;
0727     hsa = mode->hsync_end - mode->hsync_start;
0728     hbp = mode->htotal - mode->hsync_end;
0729 
0730     /*
0731      * TODO dw drv improvements
0732      * computations below may be improved...
0733      */
0734     lbcc = dw_mipi_dsi_get_hcomponent_lbcc(dsi, mode, htotal);
0735     dsi_write(dsi, DSI_VID_HLINE_TIME, lbcc);
0736 
0737     lbcc = dw_mipi_dsi_get_hcomponent_lbcc(dsi, mode, hsa);
0738     dsi_write(dsi, DSI_VID_HSA_TIME, lbcc);
0739 
0740     lbcc = dw_mipi_dsi_get_hcomponent_lbcc(dsi, mode, hbp);
0741     dsi_write(dsi, DSI_VID_HBP_TIME, lbcc);
0742 }
0743 
0744 static void dw_mipi_dsi_vertical_timing_config(struct dw_mipi_dsi *dsi,
0745                     const struct drm_display_mode *mode)
0746 {
0747     u32 vactive, vsa, vfp, vbp;
0748 
0749     vactive = mode->vdisplay;
0750     vsa = mode->vsync_end - mode->vsync_start;
0751     vfp = mode->vsync_start - mode->vdisplay;
0752     vbp = mode->vtotal - mode->vsync_end;
0753 
0754     dsi_write(dsi, DSI_VID_VACTIVE_LINES, vactive);
0755     dsi_write(dsi, DSI_VID_VSA_LINES, vsa);
0756     dsi_write(dsi, DSI_VID_VFP_LINES, vfp);
0757     dsi_write(dsi, DSI_VID_VBP_LINES, vbp);
0758 }
0759 
0760 static void dw_mipi_dsi_dphy_timing_config(struct dw_mipi_dsi *dsi)
0761 {
0762     const struct dw_mipi_dsi_phy_ops *phy_ops = dsi->plat_data->phy_ops;
0763     struct dw_mipi_dsi_dphy_timing timing;
0764     u32 hw_version;
0765     int ret;
0766 
0767     ret = phy_ops->get_timing(dsi->plat_data->priv_data,
0768                   dsi->lane_mbps, &timing);
0769     if (ret)
0770         DRM_DEV_ERROR(dsi->dev, "Retrieving phy timings failed\n");
0771 
0772     /*
0773      * TODO dw drv improvements
0774      * data & clock lane timers should be computed according to panel
0775      * blankings and to the automatic clock lane control mode...
0776      * note: DSI_PHY_TMR_CFG.MAX_RD_TIME should be in line with
0777      * DSI_CMD_MODE_CFG.MAX_RD_PKT_SIZE_LP (see CMD_MODE_ALL_LP)
0778      */
0779 
0780     hw_version = dsi_read(dsi, DSI_VERSION) & VERSION;
0781 
0782     if (hw_version >= HWVER_131) {
0783         dsi_write(dsi, DSI_PHY_TMR_CFG,
0784               PHY_HS2LP_TIME_V131(timing.data_hs2lp) |
0785               PHY_LP2HS_TIME_V131(timing.data_lp2hs));
0786         dsi_write(dsi, DSI_PHY_TMR_RD_CFG, MAX_RD_TIME_V131(10000));
0787     } else {
0788         dsi_write(dsi, DSI_PHY_TMR_CFG,
0789               PHY_HS2LP_TIME(timing.data_hs2lp) |
0790               PHY_LP2HS_TIME(timing.data_lp2hs) |
0791               MAX_RD_TIME(10000));
0792     }
0793 
0794     dsi_write(dsi, DSI_PHY_TMR_LPCLK_CFG,
0795           PHY_CLKHS2LP_TIME(timing.clk_hs2lp) |
0796           PHY_CLKLP2HS_TIME(timing.clk_lp2hs));
0797 }
0798 
0799 static void dw_mipi_dsi_dphy_interface_config(struct dw_mipi_dsi *dsi)
0800 {
0801     /*
0802      * TODO dw drv improvements
0803      * stop wait time should be the maximum between host dsi
0804      * and panel stop wait times
0805      */
0806     dsi_write(dsi, DSI_PHY_IF_CFG, PHY_STOP_WAIT_TIME(0x20) |
0807           N_LANES(dsi->lanes));
0808 }
0809 
0810 static void dw_mipi_dsi_dphy_init(struct dw_mipi_dsi *dsi)
0811 {
0812     /* Clear PHY state */
0813     dsi_write(dsi, DSI_PHY_RSTZ, PHY_DISFORCEPLL | PHY_DISABLECLK
0814           | PHY_RSTZ | PHY_SHUTDOWNZ);
0815     dsi_write(dsi, DSI_PHY_TST_CTRL0, PHY_UNTESTCLR);
0816     dsi_write(dsi, DSI_PHY_TST_CTRL0, PHY_TESTCLR);
0817     dsi_write(dsi, DSI_PHY_TST_CTRL0, PHY_UNTESTCLR);
0818 }
0819 
0820 static void dw_mipi_dsi_dphy_enable(struct dw_mipi_dsi *dsi)
0821 {
0822     u32 val;
0823     int ret;
0824 
0825     dsi_write(dsi, DSI_PHY_RSTZ, PHY_ENFORCEPLL | PHY_ENABLECLK |
0826           PHY_UNRSTZ | PHY_UNSHUTDOWNZ);
0827 
0828     ret = readl_poll_timeout(dsi->base + DSI_PHY_STATUS, val,
0829                  val & PHY_LOCK, 1000, PHY_STATUS_TIMEOUT_US);
0830     if (ret)
0831         DRM_DEBUG_DRIVER("failed to wait phy lock state\n");
0832 
0833     ret = readl_poll_timeout(dsi->base + DSI_PHY_STATUS,
0834                  val, val & PHY_STOP_STATE_CLK_LANE, 1000,
0835                  PHY_STATUS_TIMEOUT_US);
0836     if (ret)
0837         DRM_DEBUG_DRIVER("failed to wait phy clk lane stop state\n");
0838 }
0839 
0840 static void dw_mipi_dsi_clear_err(struct dw_mipi_dsi *dsi)
0841 {
0842     dsi_read(dsi, DSI_INT_ST0);
0843     dsi_read(dsi, DSI_INT_ST1);
0844     dsi_write(dsi, DSI_INT_MSK0, 0);
0845     dsi_write(dsi, DSI_INT_MSK1, 0);
0846 }
0847 
0848 static void dw_mipi_dsi_bridge_post_atomic_disable(struct drm_bridge *bridge,
0849                            struct drm_bridge_state *old_bridge_state)
0850 {
0851     struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
0852     const struct dw_mipi_dsi_phy_ops *phy_ops = dsi->plat_data->phy_ops;
0853 
0854     /*
0855      * Switch to command mode before panel-bridge post_disable &
0856      * panel unprepare.
0857      * Note: panel-bridge disable & panel disable has been called
0858      * before by the drm framework.
0859      */
0860     dw_mipi_dsi_set_mode(dsi, 0);
0861 
0862     /*
0863      * TODO Only way found to call panel-bridge post_disable &
0864      * panel unprepare before the dsi "final" disable...
0865      * This needs to be fixed in the drm_bridge framework and the API
0866      * needs to be updated to manage our own call chains...
0867      */
0868     if (dsi->panel_bridge->funcs->post_disable)
0869         dsi->panel_bridge->funcs->post_disable(dsi->panel_bridge);
0870 
0871     if (phy_ops->power_off)
0872         phy_ops->power_off(dsi->plat_data->priv_data);
0873 
0874     if (dsi->slave) {
0875         dw_mipi_dsi_disable(dsi->slave);
0876         clk_disable_unprepare(dsi->slave->pclk);
0877         pm_runtime_put(dsi->slave->dev);
0878     }
0879     dw_mipi_dsi_disable(dsi);
0880 
0881     clk_disable_unprepare(dsi->pclk);
0882     pm_runtime_put(dsi->dev);
0883 }
0884 
0885 static unsigned int dw_mipi_dsi_get_lanes(struct dw_mipi_dsi *dsi)
0886 {
0887     /* this instance is the slave, so add the master's lanes */
0888     if (dsi->master)
0889         return dsi->master->lanes + dsi->lanes;
0890 
0891     /* this instance is the master, so add the slave's lanes */
0892     if (dsi->slave)
0893         return dsi->lanes + dsi->slave->lanes;
0894 
0895     /* single-dsi, so no other instance to consider */
0896     return dsi->lanes;
0897 }
0898 
0899 static void dw_mipi_dsi_mode_set(struct dw_mipi_dsi *dsi,
0900                  const struct drm_display_mode *adjusted_mode)
0901 {
0902     const struct dw_mipi_dsi_phy_ops *phy_ops = dsi->plat_data->phy_ops;
0903     void *priv_data = dsi->plat_data->priv_data;
0904     int ret;
0905     u32 lanes = dw_mipi_dsi_get_lanes(dsi);
0906 
0907     clk_prepare_enable(dsi->pclk);
0908 
0909     ret = phy_ops->get_lane_mbps(priv_data, adjusted_mode, dsi->mode_flags,
0910                      lanes, dsi->format, &dsi->lane_mbps);
0911     if (ret)
0912         DRM_DEBUG_DRIVER("Phy get_lane_mbps() failed\n");
0913 
0914     pm_runtime_get_sync(dsi->dev);
0915     dw_mipi_dsi_init(dsi);
0916     dw_mipi_dsi_dpi_config(dsi, adjusted_mode);
0917     dw_mipi_dsi_packet_handler_config(dsi);
0918     dw_mipi_dsi_video_mode_config(dsi);
0919     dw_mipi_dsi_video_packet_config(dsi, adjusted_mode);
0920     dw_mipi_dsi_command_mode_config(dsi);
0921     dw_mipi_dsi_line_timer_config(dsi, adjusted_mode);
0922     dw_mipi_dsi_vertical_timing_config(dsi, adjusted_mode);
0923 
0924     dw_mipi_dsi_dphy_init(dsi);
0925     dw_mipi_dsi_dphy_timing_config(dsi);
0926     dw_mipi_dsi_dphy_interface_config(dsi);
0927 
0928     dw_mipi_dsi_clear_err(dsi);
0929 
0930     ret = phy_ops->init(priv_data);
0931     if (ret)
0932         DRM_DEBUG_DRIVER("Phy init() failed\n");
0933 
0934     dw_mipi_dsi_dphy_enable(dsi);
0935 
0936     dw_mipi_dsi_wait_for_two_frames(adjusted_mode);
0937 
0938     /* Switch to cmd mode for panel-bridge pre_enable & panel prepare */
0939     dw_mipi_dsi_set_mode(dsi, 0);
0940 
0941     if (phy_ops->power_on)
0942         phy_ops->power_on(dsi->plat_data->priv_data);
0943 }
0944 
0945 static void dw_mipi_dsi_bridge_mode_set(struct drm_bridge *bridge,
0946                     const struct drm_display_mode *mode,
0947                     const struct drm_display_mode *adjusted_mode)
0948 {
0949     struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
0950 
0951     dw_mipi_dsi_mode_set(dsi, adjusted_mode);
0952     if (dsi->slave)
0953         dw_mipi_dsi_mode_set(dsi->slave, adjusted_mode);
0954 }
0955 
0956 static void dw_mipi_dsi_bridge_atomic_enable(struct drm_bridge *bridge,
0957                          struct drm_bridge_state *old_bridge_state)
0958 {
0959     struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
0960 
0961     /* Switch to video mode for panel-bridge enable & panel enable */
0962     dw_mipi_dsi_set_mode(dsi, MIPI_DSI_MODE_VIDEO);
0963     if (dsi->slave)
0964         dw_mipi_dsi_set_mode(dsi->slave, MIPI_DSI_MODE_VIDEO);
0965 }
0966 
0967 static enum drm_mode_status
0968 dw_mipi_dsi_bridge_mode_valid(struct drm_bridge *bridge,
0969                   const struct drm_display_info *info,
0970                   const struct drm_display_mode *mode)
0971 {
0972     struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
0973     const struct dw_mipi_dsi_plat_data *pdata = dsi->plat_data;
0974     enum drm_mode_status mode_status = MODE_OK;
0975 
0976     if (pdata->mode_valid)
0977         mode_status = pdata->mode_valid(pdata->priv_data, mode,
0978                         dsi->mode_flags,
0979                         dw_mipi_dsi_get_lanes(dsi),
0980                         dsi->format);
0981 
0982     return mode_status;
0983 }
0984 
0985 static int dw_mipi_dsi_bridge_attach(struct drm_bridge *bridge,
0986                      enum drm_bridge_attach_flags flags)
0987 {
0988     struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
0989 
0990     if (!bridge->encoder) {
0991         DRM_ERROR("Parent encoder object not found\n");
0992         return -ENODEV;
0993     }
0994 
0995     /* Set the encoder type as caller does not know it */
0996     bridge->encoder->encoder_type = DRM_MODE_ENCODER_DSI;
0997 
0998     /* Attach the panel-bridge to the dsi bridge */
0999     return drm_bridge_attach(bridge->encoder, dsi->panel_bridge, bridge,
1000                  flags);
1001 }
1002 
1003 static const struct drm_bridge_funcs dw_mipi_dsi_bridge_funcs = {
1004     .atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
1005     .atomic_destroy_state   = drm_atomic_helper_bridge_destroy_state,
1006     .atomic_reset       = drm_atomic_helper_bridge_reset,
1007     .atomic_enable      = dw_mipi_dsi_bridge_atomic_enable,
1008     .atomic_post_disable    = dw_mipi_dsi_bridge_post_atomic_disable,
1009     .mode_set       = dw_mipi_dsi_bridge_mode_set,
1010     .mode_valid     = dw_mipi_dsi_bridge_mode_valid,
1011     .attach         = dw_mipi_dsi_bridge_attach,
1012 };
1013 
1014 #ifdef CONFIG_DEBUG_FS
1015 
1016 static int dw_mipi_dsi_debugfs_write(void *data, u64 val)
1017 {
1018     struct debugfs_entries *vpg = data;
1019     struct dw_mipi_dsi *dsi;
1020     u32 mode_cfg;
1021 
1022     if (!vpg)
1023         return -ENODEV;
1024 
1025     dsi = vpg->dsi;
1026 
1027     *vpg->reg = (bool)val;
1028 
1029     mode_cfg = dsi_read(dsi, DSI_VID_MODE_CFG);
1030 
1031     if (*vpg->reg)
1032         mode_cfg |= vpg->mask;
1033     else
1034         mode_cfg &= ~vpg->mask;
1035 
1036     dsi_write(dsi, DSI_VID_MODE_CFG, mode_cfg);
1037 
1038     return 0;
1039 }
1040 
1041 static int dw_mipi_dsi_debugfs_show(void *data, u64 *val)
1042 {
1043     struct debugfs_entries *vpg = data;
1044 
1045     if (!vpg)
1046         return -ENODEV;
1047 
1048     *val = *vpg->reg;
1049 
1050     return 0;
1051 }
1052 
1053 DEFINE_DEBUGFS_ATTRIBUTE(fops_x32, dw_mipi_dsi_debugfs_show,
1054              dw_mipi_dsi_debugfs_write, "%llu\n");
1055 
1056 static void debugfs_create_files(void *data)
1057 {
1058     struct dw_mipi_dsi *dsi = data;
1059     struct debugfs_entries debugfs[] = {
1060         REGISTER(vpg, VID_MODE_VPG_ENABLE, dsi),
1061         REGISTER(vpg_horizontal, VID_MODE_VPG_HORIZONTAL, dsi),
1062         REGISTER(vpg_ber_pattern, VID_MODE_VPG_MODE, dsi),
1063     };
1064     int i;
1065 
1066     dsi->debugfs_vpg = kmemdup(debugfs, sizeof(debugfs), GFP_KERNEL);
1067     if (!dsi->debugfs_vpg)
1068         return;
1069 
1070     for (i = 0; i < ARRAY_SIZE(debugfs); i++)
1071         debugfs_create_file(dsi->debugfs_vpg[i].name, 0644,
1072                     dsi->debugfs, &dsi->debugfs_vpg[i],
1073                     &fops_x32);
1074 }
1075 
1076 static void dw_mipi_dsi_debugfs_init(struct dw_mipi_dsi *dsi)
1077 {
1078     dsi->debugfs = debugfs_create_dir(dev_name(dsi->dev), NULL);
1079     if (IS_ERR(dsi->debugfs)) {
1080         dev_err(dsi->dev, "failed to create debugfs root\n");
1081         return;
1082     }
1083 
1084     debugfs_create_files(dsi);
1085 }
1086 
1087 static void dw_mipi_dsi_debugfs_remove(struct dw_mipi_dsi *dsi)
1088 {
1089     debugfs_remove_recursive(dsi->debugfs);
1090     kfree(dsi->debugfs_vpg);
1091 }
1092 
1093 #else
1094 
1095 static void dw_mipi_dsi_debugfs_init(struct dw_mipi_dsi *dsi) { }
1096 static void dw_mipi_dsi_debugfs_remove(struct dw_mipi_dsi *dsi) { }
1097 
1098 #endif /* CONFIG_DEBUG_FS */
1099 
1100 static struct dw_mipi_dsi *
1101 __dw_mipi_dsi_probe(struct platform_device *pdev,
1102             const struct dw_mipi_dsi_plat_data *plat_data)
1103 {
1104     struct device *dev = &pdev->dev;
1105     struct reset_control *apb_rst;
1106     struct dw_mipi_dsi *dsi;
1107     int ret;
1108 
1109     dsi = devm_kzalloc(dev, sizeof(*dsi), GFP_KERNEL);
1110     if (!dsi)
1111         return ERR_PTR(-ENOMEM);
1112 
1113     dsi->dev = dev;
1114     dsi->plat_data = plat_data;
1115 
1116     if (!plat_data->phy_ops->init || !plat_data->phy_ops->get_lane_mbps ||
1117         !plat_data->phy_ops->get_timing) {
1118         DRM_ERROR("Phy not properly configured\n");
1119         return ERR_PTR(-ENODEV);
1120     }
1121 
1122     if (!plat_data->base) {
1123         dsi->base = devm_platform_ioremap_resource(pdev, 0);
1124         if (IS_ERR(dsi->base))
1125             return ERR_PTR(-ENODEV);
1126 
1127     } else {
1128         dsi->base = plat_data->base;
1129     }
1130 
1131     dsi->pclk = devm_clk_get(dev, "pclk");
1132     if (IS_ERR(dsi->pclk)) {
1133         ret = PTR_ERR(dsi->pclk);
1134         dev_err(dev, "Unable to get pclk: %d\n", ret);
1135         return ERR_PTR(ret);
1136     }
1137 
1138     /*
1139      * Note that the reset was not defined in the initial device tree, so
1140      * we have to be prepared for it not being found.
1141      */
1142     apb_rst = devm_reset_control_get_optional_exclusive(dev, "apb");
1143     if (IS_ERR(apb_rst)) {
1144         ret = PTR_ERR(apb_rst);
1145 
1146         if (ret != -EPROBE_DEFER)
1147             dev_err(dev, "Unable to get reset control: %d\n", ret);
1148 
1149         return ERR_PTR(ret);
1150     }
1151 
1152     if (apb_rst) {
1153         ret = clk_prepare_enable(dsi->pclk);
1154         if (ret) {
1155             dev_err(dev, "%s: Failed to enable pclk\n", __func__);
1156             return ERR_PTR(ret);
1157         }
1158 
1159         reset_control_assert(apb_rst);
1160         usleep_range(10, 20);
1161         reset_control_deassert(apb_rst);
1162 
1163         clk_disable_unprepare(dsi->pclk);
1164     }
1165 
1166     dw_mipi_dsi_debugfs_init(dsi);
1167     pm_runtime_enable(dev);
1168 
1169     dsi->dsi_host.ops = &dw_mipi_dsi_host_ops;
1170     dsi->dsi_host.dev = dev;
1171     ret = mipi_dsi_host_register(&dsi->dsi_host);
1172     if (ret) {
1173         dev_err(dev, "Failed to register MIPI host: %d\n", ret);
1174         pm_runtime_disable(dev);
1175         dw_mipi_dsi_debugfs_remove(dsi);
1176         return ERR_PTR(ret);
1177     }
1178 
1179     dsi->bridge.driver_private = dsi;
1180     dsi->bridge.funcs = &dw_mipi_dsi_bridge_funcs;
1181 #ifdef CONFIG_OF
1182     dsi->bridge.of_node = pdev->dev.of_node;
1183 #endif
1184 
1185     return dsi;
1186 }
1187 
1188 static void __dw_mipi_dsi_remove(struct dw_mipi_dsi *dsi)
1189 {
1190     mipi_dsi_host_unregister(&dsi->dsi_host);
1191 
1192     pm_runtime_disable(dsi->dev);
1193     dw_mipi_dsi_debugfs_remove(dsi);
1194 }
1195 
1196 void dw_mipi_dsi_set_slave(struct dw_mipi_dsi *dsi, struct dw_mipi_dsi *slave)
1197 {
1198     /* introduce controllers to each other */
1199     dsi->slave = slave;
1200     dsi->slave->master = dsi;
1201 
1202     /* migrate settings for already attached displays */
1203     dsi->slave->lanes = dsi->lanes;
1204     dsi->slave->channel = dsi->channel;
1205     dsi->slave->format = dsi->format;
1206     dsi->slave->mode_flags = dsi->mode_flags;
1207 }
1208 EXPORT_SYMBOL_GPL(dw_mipi_dsi_set_slave);
1209 
1210 /*
1211  * Probe/remove API, used from platforms based on the DRM bridge API.
1212  */
1213 struct dw_mipi_dsi *
1214 dw_mipi_dsi_probe(struct platform_device *pdev,
1215           const struct dw_mipi_dsi_plat_data *plat_data)
1216 {
1217     return __dw_mipi_dsi_probe(pdev, plat_data);
1218 }
1219 EXPORT_SYMBOL_GPL(dw_mipi_dsi_probe);
1220 
1221 void dw_mipi_dsi_remove(struct dw_mipi_dsi *dsi)
1222 {
1223     __dw_mipi_dsi_remove(dsi);
1224 }
1225 EXPORT_SYMBOL_GPL(dw_mipi_dsi_remove);
1226 
1227 /*
1228  * Bind/unbind API, used from platforms based on the component framework.
1229  */
1230 int dw_mipi_dsi_bind(struct dw_mipi_dsi *dsi, struct drm_encoder *encoder)
1231 {
1232     return drm_bridge_attach(encoder, &dsi->bridge, NULL, 0);
1233 }
1234 EXPORT_SYMBOL_GPL(dw_mipi_dsi_bind);
1235 
1236 void dw_mipi_dsi_unbind(struct dw_mipi_dsi *dsi)
1237 {
1238 }
1239 EXPORT_SYMBOL_GPL(dw_mipi_dsi_unbind);
1240 
1241 MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
1242 MODULE_AUTHOR("Philippe Cornu <philippe.cornu@st.com>");
1243 MODULE_DESCRIPTION("DW MIPI DSI host controller driver");
1244 MODULE_LICENSE("GPL");
1245 MODULE_ALIAS("platform:dw-mipi-dsi");