Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (c) 2015, The Linux Foundation. All rights reserved.
0004  */
0005 
0006 #include <linux/clk.h>
0007 #include <linux/delay.h>
0008 #include <linux/dma-mapping.h>
0009 #include <linux/err.h>
0010 #include <linux/gpio/consumer.h>
0011 #include <linux/interrupt.h>
0012 #include <linux/mfd/syscon.h>
0013 #include <linux/of_device.h>
0014 #include <linux/of_graph.h>
0015 #include <linux/of_irq.h>
0016 #include <linux/pinctrl/consumer.h>
0017 #include <linux/pm_opp.h>
0018 #include <linux/regmap.h>
0019 #include <linux/regulator/consumer.h>
0020 #include <linux/spinlock.h>
0021 
0022 #include <video/mipi_display.h>
0023 
0024 #include <drm/drm_of.h>
0025 
0026 #include "dsi.h"
0027 #include "dsi.xml.h"
0028 #include "sfpb.xml.h"
0029 #include "dsi_cfg.h"
0030 #include "msm_kms.h"
0031 #include "msm_gem.h"
0032 #include "phy/dsi_phy.h"
0033 
0034 #define DSI_RESET_TOGGLE_DELAY_MS 20
0035 
0036 static int dsi_populate_dsc_params(struct msm_display_dsc_config *dsc);
0037 
0038 static int dsi_get_version(const void __iomem *base, u32 *major, u32 *minor)
0039 {
0040     u32 ver;
0041 
0042     if (!major || !minor)
0043         return -EINVAL;
0044 
0045     /*
0046      * From DSI6G(v3), addition of a 6G_HW_VERSION register at offset 0
0047      * makes all other registers 4-byte shifted down.
0048      *
0049      * In order to identify between DSI6G(v3) and beyond, and DSIv2 and
0050      * older, we read the DSI_VERSION register without any shift(offset
0051      * 0x1f0). In the case of DSIv2, this hast to be a non-zero value. In
0052      * the case of DSI6G, this has to be zero (the offset points to a
0053      * scratch register which we never touch)
0054      */
0055 
0056     ver = msm_readl(base + REG_DSI_VERSION);
0057     if (ver) {
0058         /* older dsi host, there is no register shift */
0059         ver = FIELD(ver, DSI_VERSION_MAJOR);
0060         if (ver <= MSM_DSI_VER_MAJOR_V2) {
0061             /* old versions */
0062             *major = ver;
0063             *minor = 0;
0064             return 0;
0065         } else {
0066             return -EINVAL;
0067         }
0068     } else {
0069         /*
0070          * newer host, offset 0 has 6G_HW_VERSION, the rest of the
0071          * registers are shifted down, read DSI_VERSION again with
0072          * the shifted offset
0073          */
0074         ver = msm_readl(base + DSI_6G_REG_SHIFT + REG_DSI_VERSION);
0075         ver = FIELD(ver, DSI_VERSION_MAJOR);
0076         if (ver == MSM_DSI_VER_MAJOR_6G) {
0077             /* 6G version */
0078             *major = ver;
0079             *minor = msm_readl(base + REG_DSI_6G_HW_VERSION);
0080             return 0;
0081         } else {
0082             return -EINVAL;
0083         }
0084     }
0085 }
0086 
0087 #define DSI_ERR_STATE_ACK           0x0000
0088 #define DSI_ERR_STATE_TIMEOUT           0x0001
0089 #define DSI_ERR_STATE_DLN0_PHY          0x0002
0090 #define DSI_ERR_STATE_FIFO          0x0004
0091 #define DSI_ERR_STATE_MDP_FIFO_UNDERFLOW    0x0008
0092 #define DSI_ERR_STATE_INTERLEAVE_OP_CONTENTION  0x0010
0093 #define DSI_ERR_STATE_PLL_UNLOCKED      0x0020
0094 
0095 #define DSI_CLK_CTRL_ENABLE_CLKS    \
0096         (DSI_CLK_CTRL_AHBS_HCLK_ON | DSI_CLK_CTRL_AHBM_SCLK_ON | \
0097         DSI_CLK_CTRL_PCLK_ON | DSI_CLK_CTRL_DSICLK_ON | \
0098         DSI_CLK_CTRL_BYTECLK_ON | DSI_CLK_CTRL_ESCCLK_ON | \
0099         DSI_CLK_CTRL_FORCE_ON_DYN_AHBM_HCLK)
0100 
0101 struct msm_dsi_host {
0102     struct mipi_dsi_host base;
0103 
0104     struct platform_device *pdev;
0105     struct drm_device *dev;
0106 
0107     int id;
0108 
0109     void __iomem *ctrl_base;
0110     phys_addr_t ctrl_size;
0111     struct regulator_bulk_data supplies[DSI_DEV_REGULATOR_MAX];
0112 
0113     int num_bus_clks;
0114     struct clk_bulk_data bus_clks[DSI_BUS_CLK_MAX];
0115 
0116     struct clk *byte_clk;
0117     struct clk *esc_clk;
0118     struct clk *pixel_clk;
0119     struct clk *byte_clk_src;
0120     struct clk *pixel_clk_src;
0121     struct clk *byte_intf_clk;
0122 
0123     unsigned long byte_clk_rate;
0124     unsigned long pixel_clk_rate;
0125     unsigned long esc_clk_rate;
0126 
0127     /* DSI v2 specific clocks */
0128     struct clk *src_clk;
0129     struct clk *esc_clk_src;
0130     struct clk *dsi_clk_src;
0131 
0132     unsigned long src_clk_rate;
0133 
0134     struct gpio_desc *disp_en_gpio;
0135     struct gpio_desc *te_gpio;
0136 
0137     const struct msm_dsi_cfg_handler *cfg_hnd;
0138 
0139     struct completion dma_comp;
0140     struct completion video_comp;
0141     struct mutex dev_mutex;
0142     struct mutex cmd_mutex;
0143     spinlock_t intr_lock; /* Protect interrupt ctrl register */
0144 
0145     u32 err_work_state;
0146     struct work_struct err_work;
0147     struct work_struct hpd_work;
0148     struct workqueue_struct *workqueue;
0149 
0150     /* DSI 6G TX buffer*/
0151     struct drm_gem_object *tx_gem_obj;
0152 
0153     /* DSI v2 TX buffer */
0154     void *tx_buf;
0155     dma_addr_t tx_buf_paddr;
0156 
0157     int tx_size;
0158 
0159     u8 *rx_buf;
0160 
0161     struct regmap *sfpb;
0162 
0163     struct drm_display_mode *mode;
0164     struct msm_display_dsc_config *dsc;
0165 
0166     /* connected device info */
0167     struct device_node *device_node;
0168     unsigned int channel;
0169     unsigned int lanes;
0170     enum mipi_dsi_pixel_format format;
0171     unsigned long mode_flags;
0172 
0173     /* lane data parsed via DT */
0174     int dlane_swap;
0175     int num_data_lanes;
0176 
0177     /* from phy DT */
0178     bool cphy_mode;
0179 
0180     u32 dma_cmd_ctrl_restore;
0181 
0182     bool registered;
0183     bool power_on;
0184     bool enabled;
0185     int irq;
0186 };
0187 
0188 static u32 dsi_get_bpp(const enum mipi_dsi_pixel_format fmt)
0189 {
0190     switch (fmt) {
0191     case MIPI_DSI_FMT_RGB565:       return 16;
0192     case MIPI_DSI_FMT_RGB666_PACKED:    return 18;
0193     case MIPI_DSI_FMT_RGB666:
0194     case MIPI_DSI_FMT_RGB888:
0195     default:                return 24;
0196     }
0197 }
0198 
0199 static inline u32 dsi_read(struct msm_dsi_host *msm_host, u32 reg)
0200 {
0201     return msm_readl(msm_host->ctrl_base + reg);
0202 }
0203 static inline void dsi_write(struct msm_dsi_host *msm_host, u32 reg, u32 data)
0204 {
0205     msm_writel(data, msm_host->ctrl_base + reg);
0206 }
0207 
0208 static int dsi_host_regulator_enable(struct msm_dsi_host *msm_host);
0209 static void dsi_host_regulator_disable(struct msm_dsi_host *msm_host);
0210 
0211 static const struct msm_dsi_cfg_handler *dsi_get_config(
0212                         struct msm_dsi_host *msm_host)
0213 {
0214     const struct msm_dsi_cfg_handler *cfg_hnd = NULL;
0215     struct device *dev = &msm_host->pdev->dev;
0216     struct clk *ahb_clk;
0217     int ret;
0218     u32 major = 0, minor = 0;
0219 
0220     cfg_hnd = device_get_match_data(dev);
0221     if (cfg_hnd)
0222         return cfg_hnd;
0223 
0224     ahb_clk = msm_clk_get(msm_host->pdev, "iface");
0225     if (IS_ERR(ahb_clk)) {
0226         pr_err("%s: cannot get interface clock\n", __func__);
0227         goto exit;
0228     }
0229 
0230     pm_runtime_get_sync(dev);
0231 
0232     ret = clk_prepare_enable(ahb_clk);
0233     if (ret) {
0234         pr_err("%s: unable to enable ahb_clk\n", __func__);
0235         goto runtime_put;
0236     }
0237 
0238     ret = dsi_get_version(msm_host->ctrl_base, &major, &minor);
0239     if (ret) {
0240         pr_err("%s: Invalid version\n", __func__);
0241         goto disable_clks;
0242     }
0243 
0244     cfg_hnd = msm_dsi_cfg_get(major, minor);
0245 
0246     DBG("%s: Version %x:%x\n", __func__, major, minor);
0247 
0248 disable_clks:
0249     clk_disable_unprepare(ahb_clk);
0250 runtime_put:
0251     pm_runtime_put_sync(dev);
0252 exit:
0253     return cfg_hnd;
0254 }
0255 
0256 static inline struct msm_dsi_host *to_msm_dsi_host(struct mipi_dsi_host *host)
0257 {
0258     return container_of(host, struct msm_dsi_host, base);
0259 }
0260 
0261 static void dsi_host_regulator_disable(struct msm_dsi_host *msm_host)
0262 {
0263     struct regulator_bulk_data *s = msm_host->supplies;
0264     const struct dsi_reg_entry *regs = msm_host->cfg_hnd->cfg->reg_cfg.regs;
0265     int num = msm_host->cfg_hnd->cfg->reg_cfg.num;
0266     int i;
0267 
0268     DBG("");
0269     for (i = num - 1; i >= 0; i--)
0270         if (regs[i].disable_load >= 0)
0271             regulator_set_load(s[i].consumer,
0272                        regs[i].disable_load);
0273 
0274     regulator_bulk_disable(num, s);
0275 }
0276 
0277 static int dsi_host_regulator_enable(struct msm_dsi_host *msm_host)
0278 {
0279     struct regulator_bulk_data *s = msm_host->supplies;
0280     const struct dsi_reg_entry *regs = msm_host->cfg_hnd->cfg->reg_cfg.regs;
0281     int num = msm_host->cfg_hnd->cfg->reg_cfg.num;
0282     int ret, i;
0283 
0284     DBG("");
0285     for (i = 0; i < num; i++) {
0286         if (regs[i].enable_load >= 0) {
0287             ret = regulator_set_load(s[i].consumer,
0288                          regs[i].enable_load);
0289             if (ret < 0) {
0290                 pr_err("regulator %d set op mode failed, %d\n",
0291                     i, ret);
0292                 goto fail;
0293             }
0294         }
0295     }
0296 
0297     ret = regulator_bulk_enable(num, s);
0298     if (ret < 0) {
0299         pr_err("regulator enable failed, %d\n", ret);
0300         goto fail;
0301     }
0302 
0303     return 0;
0304 
0305 fail:
0306     for (i--; i >= 0; i--)
0307         regulator_set_load(s[i].consumer, regs[i].disable_load);
0308     return ret;
0309 }
0310 
0311 static int dsi_regulator_init(struct msm_dsi_host *msm_host)
0312 {
0313     struct regulator_bulk_data *s = msm_host->supplies;
0314     const struct dsi_reg_entry *regs = msm_host->cfg_hnd->cfg->reg_cfg.regs;
0315     int num = msm_host->cfg_hnd->cfg->reg_cfg.num;
0316     int i, ret;
0317 
0318     for (i = 0; i < num; i++)
0319         s[i].supply = regs[i].name;
0320 
0321     ret = devm_regulator_bulk_get(&msm_host->pdev->dev, num, s);
0322     if (ret < 0) {
0323         pr_err("%s: failed to init regulator, ret=%d\n",
0324                         __func__, ret);
0325         return ret;
0326     }
0327 
0328     return 0;
0329 }
0330 
0331 int dsi_clk_init_v2(struct msm_dsi_host *msm_host)
0332 {
0333     struct platform_device *pdev = msm_host->pdev;
0334     int ret = 0;
0335 
0336     msm_host->src_clk = msm_clk_get(pdev, "src");
0337 
0338     if (IS_ERR(msm_host->src_clk)) {
0339         ret = PTR_ERR(msm_host->src_clk);
0340         pr_err("%s: can't find src clock. ret=%d\n",
0341             __func__, ret);
0342         msm_host->src_clk = NULL;
0343         return ret;
0344     }
0345 
0346     msm_host->esc_clk_src = clk_get_parent(msm_host->esc_clk);
0347     if (!msm_host->esc_clk_src) {
0348         ret = -ENODEV;
0349         pr_err("%s: can't get esc clock parent. ret=%d\n",
0350             __func__, ret);
0351         return ret;
0352     }
0353 
0354     msm_host->dsi_clk_src = clk_get_parent(msm_host->src_clk);
0355     if (!msm_host->dsi_clk_src) {
0356         ret = -ENODEV;
0357         pr_err("%s: can't get src clock parent. ret=%d\n",
0358             __func__, ret);
0359     }
0360 
0361     return ret;
0362 }
0363 
0364 int dsi_clk_init_6g_v2(struct msm_dsi_host *msm_host)
0365 {
0366     struct platform_device *pdev = msm_host->pdev;
0367     int ret = 0;
0368 
0369     msm_host->byte_intf_clk = msm_clk_get(pdev, "byte_intf");
0370     if (IS_ERR(msm_host->byte_intf_clk)) {
0371         ret = PTR_ERR(msm_host->byte_intf_clk);
0372         pr_err("%s: can't find byte_intf clock. ret=%d\n",
0373             __func__, ret);
0374     }
0375 
0376     return ret;
0377 }
0378 
0379 static int dsi_clk_init(struct msm_dsi_host *msm_host)
0380 {
0381     struct platform_device *pdev = msm_host->pdev;
0382     const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
0383     const struct msm_dsi_config *cfg = cfg_hnd->cfg;
0384     int i, ret = 0;
0385 
0386     /* get bus clocks */
0387     for (i = 0; i < cfg->num_bus_clks; i++)
0388         msm_host->bus_clks[i].id = cfg->bus_clk_names[i];
0389     msm_host->num_bus_clks = cfg->num_bus_clks;
0390 
0391     ret = devm_clk_bulk_get(&pdev->dev, msm_host->num_bus_clks, msm_host->bus_clks);
0392     if (ret < 0) {
0393         dev_err(&pdev->dev, "Unable to get clocks, ret = %d\n", ret);
0394         goto exit;
0395     }
0396 
0397     /* get link and source clocks */
0398     msm_host->byte_clk = msm_clk_get(pdev, "byte");
0399     if (IS_ERR(msm_host->byte_clk)) {
0400         ret = PTR_ERR(msm_host->byte_clk);
0401         pr_err("%s: can't find dsi_byte clock. ret=%d\n",
0402             __func__, ret);
0403         msm_host->byte_clk = NULL;
0404         goto exit;
0405     }
0406 
0407     msm_host->pixel_clk = msm_clk_get(pdev, "pixel");
0408     if (IS_ERR(msm_host->pixel_clk)) {
0409         ret = PTR_ERR(msm_host->pixel_clk);
0410         pr_err("%s: can't find dsi_pixel clock. ret=%d\n",
0411             __func__, ret);
0412         msm_host->pixel_clk = NULL;
0413         goto exit;
0414     }
0415 
0416     msm_host->esc_clk = msm_clk_get(pdev, "core");
0417     if (IS_ERR(msm_host->esc_clk)) {
0418         ret = PTR_ERR(msm_host->esc_clk);
0419         pr_err("%s: can't find dsi_esc clock. ret=%d\n",
0420             __func__, ret);
0421         msm_host->esc_clk = NULL;
0422         goto exit;
0423     }
0424 
0425     msm_host->byte_clk_src = clk_get_parent(msm_host->byte_clk);
0426     if (IS_ERR(msm_host->byte_clk_src)) {
0427         ret = PTR_ERR(msm_host->byte_clk_src);
0428         pr_err("%s: can't find byte_clk clock. ret=%d\n", __func__, ret);
0429         goto exit;
0430     }
0431 
0432     msm_host->pixel_clk_src = clk_get_parent(msm_host->pixel_clk);
0433     if (IS_ERR(msm_host->pixel_clk_src)) {
0434         ret = PTR_ERR(msm_host->pixel_clk_src);
0435         pr_err("%s: can't find pixel_clk clock. ret=%d\n", __func__, ret);
0436         goto exit;
0437     }
0438 
0439     if (cfg_hnd->ops->clk_init_ver)
0440         ret = cfg_hnd->ops->clk_init_ver(msm_host);
0441 exit:
0442     return ret;
0443 }
0444 
0445 int msm_dsi_runtime_suspend(struct device *dev)
0446 {
0447     struct platform_device *pdev = to_platform_device(dev);
0448     struct msm_dsi *msm_dsi = platform_get_drvdata(pdev);
0449     struct mipi_dsi_host *host = msm_dsi->host;
0450     struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
0451 
0452     if (!msm_host->cfg_hnd)
0453         return 0;
0454 
0455     clk_bulk_disable_unprepare(msm_host->num_bus_clks, msm_host->bus_clks);
0456 
0457     return 0;
0458 }
0459 
0460 int msm_dsi_runtime_resume(struct device *dev)
0461 {
0462     struct platform_device *pdev = to_platform_device(dev);
0463     struct msm_dsi *msm_dsi = platform_get_drvdata(pdev);
0464     struct mipi_dsi_host *host = msm_dsi->host;
0465     struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
0466 
0467     if (!msm_host->cfg_hnd)
0468         return 0;
0469 
0470     return clk_bulk_prepare_enable(msm_host->num_bus_clks, msm_host->bus_clks);
0471 }
0472 
0473 int dsi_link_clk_set_rate_6g(struct msm_dsi_host *msm_host)
0474 {
0475     unsigned long byte_intf_rate;
0476     int ret;
0477 
0478     DBG("Set clk rates: pclk=%d, byteclk=%lu",
0479         msm_host->mode->clock, msm_host->byte_clk_rate);
0480 
0481     ret = dev_pm_opp_set_rate(&msm_host->pdev->dev,
0482                   msm_host->byte_clk_rate);
0483     if (ret) {
0484         pr_err("%s: dev_pm_opp_set_rate failed %d\n", __func__, ret);
0485         return ret;
0486     }
0487 
0488     ret = clk_set_rate(msm_host->pixel_clk, msm_host->pixel_clk_rate);
0489     if (ret) {
0490         pr_err("%s: Failed to set rate pixel clk, %d\n", __func__, ret);
0491         return ret;
0492     }
0493 
0494     if (msm_host->byte_intf_clk) {
0495         /* For CPHY, byte_intf_clk is same as byte_clk */
0496         if (msm_host->cphy_mode)
0497             byte_intf_rate = msm_host->byte_clk_rate;
0498         else
0499             byte_intf_rate = msm_host->byte_clk_rate / 2;
0500 
0501         ret = clk_set_rate(msm_host->byte_intf_clk, byte_intf_rate);
0502         if (ret) {
0503             pr_err("%s: Failed to set rate byte intf clk, %d\n",
0504                    __func__, ret);
0505             return ret;
0506         }
0507     }
0508 
0509     return 0;
0510 }
0511 
0512 
0513 int dsi_link_clk_enable_6g(struct msm_dsi_host *msm_host)
0514 {
0515     int ret;
0516 
0517     ret = clk_prepare_enable(msm_host->esc_clk);
0518     if (ret) {
0519         pr_err("%s: Failed to enable dsi esc clk\n", __func__);
0520         goto error;
0521     }
0522 
0523     ret = clk_prepare_enable(msm_host->byte_clk);
0524     if (ret) {
0525         pr_err("%s: Failed to enable dsi byte clk\n", __func__);
0526         goto byte_clk_err;
0527     }
0528 
0529     ret = clk_prepare_enable(msm_host->pixel_clk);
0530     if (ret) {
0531         pr_err("%s: Failed to enable dsi pixel clk\n", __func__);
0532         goto pixel_clk_err;
0533     }
0534 
0535     ret = clk_prepare_enable(msm_host->byte_intf_clk);
0536     if (ret) {
0537         pr_err("%s: Failed to enable byte intf clk\n",
0538                __func__);
0539         goto byte_intf_clk_err;
0540     }
0541 
0542     return 0;
0543 
0544 byte_intf_clk_err:
0545     clk_disable_unprepare(msm_host->pixel_clk);
0546 pixel_clk_err:
0547     clk_disable_unprepare(msm_host->byte_clk);
0548 byte_clk_err:
0549     clk_disable_unprepare(msm_host->esc_clk);
0550 error:
0551     return ret;
0552 }
0553 
0554 int dsi_link_clk_set_rate_v2(struct msm_dsi_host *msm_host)
0555 {
0556     int ret;
0557 
0558     DBG("Set clk rates: pclk=%d, byteclk=%lu, esc_clk=%lu, dsi_src_clk=%lu",
0559         msm_host->mode->clock, msm_host->byte_clk_rate,
0560         msm_host->esc_clk_rate, msm_host->src_clk_rate);
0561 
0562     ret = clk_set_rate(msm_host->byte_clk, msm_host->byte_clk_rate);
0563     if (ret) {
0564         pr_err("%s: Failed to set rate byte clk, %d\n", __func__, ret);
0565         return ret;
0566     }
0567 
0568     ret = clk_set_rate(msm_host->esc_clk, msm_host->esc_clk_rate);
0569     if (ret) {
0570         pr_err("%s: Failed to set rate esc clk, %d\n", __func__, ret);
0571         return ret;
0572     }
0573 
0574     ret = clk_set_rate(msm_host->src_clk, msm_host->src_clk_rate);
0575     if (ret) {
0576         pr_err("%s: Failed to set rate src clk, %d\n", __func__, ret);
0577         return ret;
0578     }
0579 
0580     ret = clk_set_rate(msm_host->pixel_clk, msm_host->pixel_clk_rate);
0581     if (ret) {
0582         pr_err("%s: Failed to set rate pixel clk, %d\n", __func__, ret);
0583         return ret;
0584     }
0585 
0586     return 0;
0587 }
0588 
0589 int dsi_link_clk_enable_v2(struct msm_dsi_host *msm_host)
0590 {
0591     int ret;
0592 
0593     ret = clk_prepare_enable(msm_host->byte_clk);
0594     if (ret) {
0595         pr_err("%s: Failed to enable dsi byte clk\n", __func__);
0596         goto error;
0597     }
0598 
0599     ret = clk_prepare_enable(msm_host->esc_clk);
0600     if (ret) {
0601         pr_err("%s: Failed to enable dsi esc clk\n", __func__);
0602         goto esc_clk_err;
0603     }
0604 
0605     ret = clk_prepare_enable(msm_host->src_clk);
0606     if (ret) {
0607         pr_err("%s: Failed to enable dsi src clk\n", __func__);
0608         goto src_clk_err;
0609     }
0610 
0611     ret = clk_prepare_enable(msm_host->pixel_clk);
0612     if (ret) {
0613         pr_err("%s: Failed to enable dsi pixel clk\n", __func__);
0614         goto pixel_clk_err;
0615     }
0616 
0617     return 0;
0618 
0619 pixel_clk_err:
0620     clk_disable_unprepare(msm_host->src_clk);
0621 src_clk_err:
0622     clk_disable_unprepare(msm_host->esc_clk);
0623 esc_clk_err:
0624     clk_disable_unprepare(msm_host->byte_clk);
0625 error:
0626     return ret;
0627 }
0628 
0629 void dsi_link_clk_disable_6g(struct msm_dsi_host *msm_host)
0630 {
0631     /* Drop the performance state vote */
0632     dev_pm_opp_set_rate(&msm_host->pdev->dev, 0);
0633     clk_disable_unprepare(msm_host->esc_clk);
0634     clk_disable_unprepare(msm_host->pixel_clk);
0635     clk_disable_unprepare(msm_host->byte_intf_clk);
0636     clk_disable_unprepare(msm_host->byte_clk);
0637 }
0638 
0639 void dsi_link_clk_disable_v2(struct msm_dsi_host *msm_host)
0640 {
0641     clk_disable_unprepare(msm_host->pixel_clk);
0642     clk_disable_unprepare(msm_host->src_clk);
0643     clk_disable_unprepare(msm_host->esc_clk);
0644     clk_disable_unprepare(msm_host->byte_clk);
0645 }
0646 
0647 static unsigned long dsi_get_pclk_rate(struct msm_dsi_host *msm_host, bool is_bonded_dsi)
0648 {
0649     struct drm_display_mode *mode = msm_host->mode;
0650     unsigned long pclk_rate;
0651 
0652     pclk_rate = mode->clock * 1000;
0653 
0654     /*
0655      * For bonded DSI mode, the current DRM mode has the complete width of the
0656      * panel. Since, the complete panel is driven by two DSI controllers,
0657      * the clock rates have to be split between the two dsi controllers.
0658      * Adjust the byte and pixel clock rates for each dsi host accordingly.
0659      */
0660     if (is_bonded_dsi)
0661         pclk_rate /= 2;
0662 
0663     return pclk_rate;
0664 }
0665 
0666 static void dsi_calc_pclk(struct msm_dsi_host *msm_host, bool is_bonded_dsi)
0667 {
0668     u8 lanes = msm_host->lanes;
0669     u32 bpp = dsi_get_bpp(msm_host->format);
0670     unsigned long pclk_rate = dsi_get_pclk_rate(msm_host, is_bonded_dsi);
0671     u64 pclk_bpp = (u64)pclk_rate * bpp;
0672 
0673     if (lanes == 0) {
0674         pr_err("%s: forcing mdss_dsi lanes to 1\n", __func__);
0675         lanes = 1;
0676     }
0677 
0678     /* CPHY "byte_clk" is in units of 16 bits */
0679     if (msm_host->cphy_mode)
0680         do_div(pclk_bpp, (16 * lanes));
0681     else
0682         do_div(pclk_bpp, (8 * lanes));
0683 
0684     msm_host->pixel_clk_rate = pclk_rate;
0685     msm_host->byte_clk_rate = pclk_bpp;
0686 
0687     DBG("pclk=%lu, bclk=%lu", msm_host->pixel_clk_rate,
0688                 msm_host->byte_clk_rate);
0689 
0690 }
0691 
0692 int dsi_calc_clk_rate_6g(struct msm_dsi_host *msm_host, bool is_bonded_dsi)
0693 {
0694     if (!msm_host->mode) {
0695         pr_err("%s: mode not set\n", __func__);
0696         return -EINVAL;
0697     }
0698 
0699     dsi_calc_pclk(msm_host, is_bonded_dsi);
0700     msm_host->esc_clk_rate = clk_get_rate(msm_host->esc_clk);
0701     return 0;
0702 }
0703 
0704 int dsi_calc_clk_rate_v2(struct msm_dsi_host *msm_host, bool is_bonded_dsi)
0705 {
0706     u32 bpp = dsi_get_bpp(msm_host->format);
0707     u64 pclk_bpp;
0708     unsigned int esc_mhz, esc_div;
0709     unsigned long byte_mhz;
0710 
0711     dsi_calc_pclk(msm_host, is_bonded_dsi);
0712 
0713     pclk_bpp = (u64)dsi_get_pclk_rate(msm_host, is_bonded_dsi) * bpp;
0714     do_div(pclk_bpp, 8);
0715     msm_host->src_clk_rate = pclk_bpp;
0716 
0717     /*
0718      * esc clock is byte clock followed by a 4 bit divider,
0719      * we need to find an escape clock frequency within the
0720      * mipi DSI spec range within the maximum divider limit
0721      * We iterate here between an escape clock frequencey
0722      * between 20 Mhz to 5 Mhz and pick up the first one
0723      * that can be supported by our divider
0724      */
0725 
0726     byte_mhz = msm_host->byte_clk_rate / 1000000;
0727 
0728     for (esc_mhz = 20; esc_mhz >= 5; esc_mhz--) {
0729         esc_div = DIV_ROUND_UP(byte_mhz, esc_mhz);
0730 
0731         /*
0732          * TODO: Ideally, we shouldn't know what sort of divider
0733          * is available in mmss_cc, we're just assuming that
0734          * it'll always be a 4 bit divider. Need to come up with
0735          * a better way here.
0736          */
0737         if (esc_div >= 1 && esc_div <= 16)
0738             break;
0739     }
0740 
0741     if (esc_mhz < 5)
0742         return -EINVAL;
0743 
0744     msm_host->esc_clk_rate = msm_host->byte_clk_rate / esc_div;
0745 
0746     DBG("esc=%lu, src=%lu", msm_host->esc_clk_rate,
0747         msm_host->src_clk_rate);
0748 
0749     return 0;
0750 }
0751 
0752 static void dsi_intr_ctrl(struct msm_dsi_host *msm_host, u32 mask, int enable)
0753 {
0754     u32 intr;
0755     unsigned long flags;
0756 
0757     spin_lock_irqsave(&msm_host->intr_lock, flags);
0758     intr = dsi_read(msm_host, REG_DSI_INTR_CTRL);
0759 
0760     if (enable)
0761         intr |= mask;
0762     else
0763         intr &= ~mask;
0764 
0765     DBG("intr=%x enable=%d", intr, enable);
0766 
0767     dsi_write(msm_host, REG_DSI_INTR_CTRL, intr);
0768     spin_unlock_irqrestore(&msm_host->intr_lock, flags);
0769 }
0770 
0771 static inline enum dsi_traffic_mode dsi_get_traffic_mode(const u32 mode_flags)
0772 {
0773     if (mode_flags & MIPI_DSI_MODE_VIDEO_BURST)
0774         return BURST_MODE;
0775     else if (mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
0776         return NON_BURST_SYNCH_PULSE;
0777 
0778     return NON_BURST_SYNCH_EVENT;
0779 }
0780 
0781 static inline enum dsi_vid_dst_format dsi_get_vid_fmt(
0782                 const enum mipi_dsi_pixel_format mipi_fmt)
0783 {
0784     switch (mipi_fmt) {
0785     case MIPI_DSI_FMT_RGB888:   return VID_DST_FORMAT_RGB888;
0786     case MIPI_DSI_FMT_RGB666:   return VID_DST_FORMAT_RGB666_LOOSE;
0787     case MIPI_DSI_FMT_RGB666_PACKED:    return VID_DST_FORMAT_RGB666;
0788     case MIPI_DSI_FMT_RGB565:   return VID_DST_FORMAT_RGB565;
0789     default:            return VID_DST_FORMAT_RGB888;
0790     }
0791 }
0792 
0793 static inline enum dsi_cmd_dst_format dsi_get_cmd_fmt(
0794                 const enum mipi_dsi_pixel_format mipi_fmt)
0795 {
0796     switch (mipi_fmt) {
0797     case MIPI_DSI_FMT_RGB888:   return CMD_DST_FORMAT_RGB888;
0798     case MIPI_DSI_FMT_RGB666_PACKED:
0799     case MIPI_DSI_FMT_RGB666:   return CMD_DST_FORMAT_RGB666;
0800     case MIPI_DSI_FMT_RGB565:   return CMD_DST_FORMAT_RGB565;
0801     default:            return CMD_DST_FORMAT_RGB888;
0802     }
0803 }
0804 
0805 static void dsi_ctrl_config(struct msm_dsi_host *msm_host, bool enable,
0806             struct msm_dsi_phy_shared_timings *phy_shared_timings, struct msm_dsi_phy *phy)
0807 {
0808     u32 flags = msm_host->mode_flags;
0809     enum mipi_dsi_pixel_format mipi_fmt = msm_host->format;
0810     const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
0811     u32 data = 0, lane_ctrl = 0;
0812 
0813     if (!enable) {
0814         dsi_write(msm_host, REG_DSI_CTRL, 0);
0815         return;
0816     }
0817 
0818     if (flags & MIPI_DSI_MODE_VIDEO) {
0819         if (flags & MIPI_DSI_MODE_VIDEO_HSE)
0820             data |= DSI_VID_CFG0_PULSE_MODE_HSA_HE;
0821         if (flags & MIPI_DSI_MODE_VIDEO_NO_HFP)
0822             data |= DSI_VID_CFG0_HFP_POWER_STOP;
0823         if (flags & MIPI_DSI_MODE_VIDEO_NO_HBP)
0824             data |= DSI_VID_CFG0_HBP_POWER_STOP;
0825         if (flags & MIPI_DSI_MODE_VIDEO_NO_HSA)
0826             data |= DSI_VID_CFG0_HSA_POWER_STOP;
0827         /* Always set low power stop mode for BLLP
0828          * to let command engine send packets
0829          */
0830         data |= DSI_VID_CFG0_EOF_BLLP_POWER_STOP |
0831             DSI_VID_CFG0_BLLP_POWER_STOP;
0832         data |= DSI_VID_CFG0_TRAFFIC_MODE(dsi_get_traffic_mode(flags));
0833         data |= DSI_VID_CFG0_DST_FORMAT(dsi_get_vid_fmt(mipi_fmt));
0834         data |= DSI_VID_CFG0_VIRT_CHANNEL(msm_host->channel);
0835         dsi_write(msm_host, REG_DSI_VID_CFG0, data);
0836 
0837         /* Do not swap RGB colors */
0838         data = DSI_VID_CFG1_RGB_SWAP(SWAP_RGB);
0839         dsi_write(msm_host, REG_DSI_VID_CFG1, 0);
0840     } else {
0841         /* Do not swap RGB colors */
0842         data = DSI_CMD_CFG0_RGB_SWAP(SWAP_RGB);
0843         data |= DSI_CMD_CFG0_DST_FORMAT(dsi_get_cmd_fmt(mipi_fmt));
0844         dsi_write(msm_host, REG_DSI_CMD_CFG0, data);
0845 
0846         data = DSI_CMD_CFG1_WR_MEM_START(MIPI_DCS_WRITE_MEMORY_START) |
0847             DSI_CMD_CFG1_WR_MEM_CONTINUE(
0848                     MIPI_DCS_WRITE_MEMORY_CONTINUE);
0849         /* Always insert DCS command */
0850         data |= DSI_CMD_CFG1_INSERT_DCS_COMMAND;
0851         dsi_write(msm_host, REG_DSI_CMD_CFG1, data);
0852     }
0853 
0854     dsi_write(msm_host, REG_DSI_CMD_DMA_CTRL,
0855             DSI_CMD_DMA_CTRL_FROM_FRAME_BUFFER |
0856             DSI_CMD_DMA_CTRL_LOW_POWER);
0857 
0858     data = 0;
0859     /* Always assume dedicated TE pin */
0860     data |= DSI_TRIG_CTRL_TE;
0861     data |= DSI_TRIG_CTRL_MDP_TRIGGER(TRIGGER_NONE);
0862     data |= DSI_TRIG_CTRL_DMA_TRIGGER(TRIGGER_SW);
0863     data |= DSI_TRIG_CTRL_STREAM(msm_host->channel);
0864     if ((cfg_hnd->major == MSM_DSI_VER_MAJOR_6G) &&
0865         (cfg_hnd->minor >= MSM_DSI_6G_VER_MINOR_V1_2))
0866         data |= DSI_TRIG_CTRL_BLOCK_DMA_WITHIN_FRAME;
0867     dsi_write(msm_host, REG_DSI_TRIG_CTRL, data);
0868 
0869     data = DSI_CLKOUT_TIMING_CTRL_T_CLK_POST(phy_shared_timings->clk_post) |
0870         DSI_CLKOUT_TIMING_CTRL_T_CLK_PRE(phy_shared_timings->clk_pre);
0871     dsi_write(msm_host, REG_DSI_CLKOUT_TIMING_CTRL, data);
0872 
0873     if ((cfg_hnd->major == MSM_DSI_VER_MAJOR_6G) &&
0874         (cfg_hnd->minor > MSM_DSI_6G_VER_MINOR_V1_0) &&
0875         phy_shared_timings->clk_pre_inc_by_2)
0876         dsi_write(msm_host, REG_DSI_T_CLK_PRE_EXTEND,
0877               DSI_T_CLK_PRE_EXTEND_INC_BY_2_BYTECLK);
0878 
0879     data = 0;
0880     if (!(flags & MIPI_DSI_MODE_NO_EOT_PACKET))
0881         data |= DSI_EOT_PACKET_CTRL_TX_EOT_APPEND;
0882     dsi_write(msm_host, REG_DSI_EOT_PACKET_CTRL, data);
0883 
0884     /* allow only ack-err-status to generate interrupt */
0885     dsi_write(msm_host, REG_DSI_ERR_INT_MASK0, 0x13ff3fe0);
0886 
0887     dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_ERROR, 1);
0888 
0889     dsi_write(msm_host, REG_DSI_CLK_CTRL, DSI_CLK_CTRL_ENABLE_CLKS);
0890 
0891     data = DSI_CTRL_CLK_EN;
0892 
0893     DBG("lane number=%d", msm_host->lanes);
0894     data |= ((DSI_CTRL_LANE0 << msm_host->lanes) - DSI_CTRL_LANE0);
0895 
0896     dsi_write(msm_host, REG_DSI_LANE_SWAP_CTRL,
0897           DSI_LANE_SWAP_CTRL_DLN_SWAP_SEL(msm_host->dlane_swap));
0898 
0899     if (!(flags & MIPI_DSI_CLOCK_NON_CONTINUOUS)) {
0900         lane_ctrl = dsi_read(msm_host, REG_DSI_LANE_CTRL);
0901 
0902         if (msm_dsi_phy_set_continuous_clock(phy, enable))
0903             lane_ctrl &= ~DSI_LANE_CTRL_HS_REQ_SEL_PHY;
0904 
0905         dsi_write(msm_host, REG_DSI_LANE_CTRL,
0906             lane_ctrl | DSI_LANE_CTRL_CLKLN_HS_FORCE_REQUEST);
0907     }
0908 
0909     data |= DSI_CTRL_ENABLE;
0910 
0911     dsi_write(msm_host, REG_DSI_CTRL, data);
0912 
0913     if (msm_host->cphy_mode)
0914         dsi_write(msm_host, REG_DSI_CPHY_MODE_CTRL, BIT(0));
0915 }
0916 
0917 static void dsi_update_dsc_timing(struct msm_dsi_host *msm_host, bool is_cmd_mode, u32 hdisplay)
0918 {
0919     struct msm_display_dsc_config *dsc = msm_host->dsc;
0920     u32 reg, intf_width, reg_ctrl, reg_ctrl2;
0921     u32 slice_per_intf, total_bytes_per_intf;
0922     u32 pkt_per_line;
0923     u32 bytes_in_slice;
0924     u32 eol_byte_num;
0925 
0926     /* first calculate dsc parameters and then program
0927      * compress mode registers
0928      */
0929     intf_width = hdisplay;
0930     slice_per_intf = DIV_ROUND_UP(intf_width, dsc->drm->slice_width);
0931 
0932     /* If slice_per_pkt is greater than slice_per_intf
0933      * then default to 1. This can happen during partial
0934      * update.
0935      */
0936     if (slice_per_intf > dsc->drm->slice_count)
0937         dsc->drm->slice_count = 1;
0938 
0939     slice_per_intf = DIV_ROUND_UP(hdisplay, dsc->drm->slice_width);
0940     bytes_in_slice = DIV_ROUND_UP(dsc->drm->slice_width * dsc->drm->bits_per_pixel, 8);
0941 
0942     dsc->drm->slice_chunk_size = bytes_in_slice;
0943 
0944     total_bytes_per_intf = bytes_in_slice * slice_per_intf;
0945 
0946     eol_byte_num = total_bytes_per_intf % 3;
0947     pkt_per_line = slice_per_intf / dsc->drm->slice_count;
0948 
0949     if (is_cmd_mode) /* packet data type */
0950         reg = DSI_COMMAND_COMPRESSION_MODE_CTRL_STREAM0_DATATYPE(MIPI_DSI_DCS_LONG_WRITE);
0951     else
0952         reg = DSI_VIDEO_COMPRESSION_MODE_CTRL_DATATYPE(MIPI_DSI_COMPRESSED_PIXEL_STREAM);
0953 
0954     /* DSI_VIDEO_COMPRESSION_MODE & DSI_COMMAND_COMPRESSION_MODE
0955      * registers have similar offsets, so for below common code use
0956      * DSI_VIDEO_COMPRESSION_MODE_XXXX for setting bits
0957      */
0958     reg |= DSI_VIDEO_COMPRESSION_MODE_CTRL_PKT_PER_LINE(pkt_per_line >> 1);
0959     reg |= DSI_VIDEO_COMPRESSION_MODE_CTRL_EOL_BYTE_NUM(eol_byte_num);
0960     reg |= DSI_VIDEO_COMPRESSION_MODE_CTRL_EN;
0961 
0962     if (is_cmd_mode) {
0963         reg_ctrl = dsi_read(msm_host, REG_DSI_COMMAND_COMPRESSION_MODE_CTRL);
0964         reg_ctrl2 = dsi_read(msm_host, REG_DSI_COMMAND_COMPRESSION_MODE_CTRL2);
0965 
0966         reg_ctrl &= ~0xffff;
0967         reg_ctrl |= reg;
0968 
0969         reg_ctrl2 &= ~DSI_COMMAND_COMPRESSION_MODE_CTRL2_STREAM0_SLICE_WIDTH__MASK;
0970         reg_ctrl2 |= DSI_COMMAND_COMPRESSION_MODE_CTRL2_STREAM0_SLICE_WIDTH(bytes_in_slice);
0971 
0972         dsi_write(msm_host, REG_DSI_COMMAND_COMPRESSION_MODE_CTRL, reg_ctrl);
0973         dsi_write(msm_host, REG_DSI_COMMAND_COMPRESSION_MODE_CTRL2, reg_ctrl2);
0974     } else {
0975         dsi_write(msm_host, REG_DSI_VIDEO_COMPRESSION_MODE_CTRL, reg);
0976     }
0977 }
0978 
0979 static void dsi_timing_setup(struct msm_dsi_host *msm_host, bool is_bonded_dsi)
0980 {
0981     struct drm_display_mode *mode = msm_host->mode;
0982     u32 hs_start = 0, vs_start = 0; /* take sync start as 0 */
0983     u32 h_total = mode->htotal;
0984     u32 v_total = mode->vtotal;
0985     u32 hs_end = mode->hsync_end - mode->hsync_start;
0986     u32 vs_end = mode->vsync_end - mode->vsync_start;
0987     u32 ha_start = h_total - mode->hsync_start;
0988     u32 ha_end = ha_start + mode->hdisplay;
0989     u32 va_start = v_total - mode->vsync_start;
0990     u32 va_end = va_start + mode->vdisplay;
0991     u32 hdisplay = mode->hdisplay;
0992     u32 wc;
0993 
0994     DBG("");
0995 
0996     /*
0997      * For bonded DSI mode, the current DRM mode has
0998      * the complete width of the panel. Since, the complete
0999      * panel is driven by two DSI controllers, the horizontal
1000      * timings have to be split between the two dsi controllers.
1001      * Adjust the DSI host timing values accordingly.
1002      */
1003     if (is_bonded_dsi) {
1004         h_total /= 2;
1005         hs_end /= 2;
1006         ha_start /= 2;
1007         ha_end /= 2;
1008         hdisplay /= 2;
1009     }
1010 
1011     if (msm_host->dsc) {
1012         struct msm_display_dsc_config *dsc = msm_host->dsc;
1013 
1014         /* update dsc params with timing params */
1015         if (!dsc || !mode->hdisplay || !mode->vdisplay) {
1016             pr_err("DSI: invalid input: pic_width: %d pic_height: %d\n",
1017                    mode->hdisplay, mode->vdisplay);
1018             return;
1019         }
1020 
1021         dsc->drm->pic_width = mode->hdisplay;
1022         dsc->drm->pic_height = mode->vdisplay;
1023         DBG("Mode %dx%d\n", dsc->drm->pic_width, dsc->drm->pic_height);
1024 
1025         /* we do the calculations for dsc parameters here so that
1026          * panel can use these parameters
1027          */
1028         dsi_populate_dsc_params(dsc);
1029 
1030         /* Divide the display by 3 but keep back/font porch and
1031          * pulse width same
1032          */
1033         h_total -= hdisplay;
1034         hdisplay /= 3;
1035         h_total += hdisplay;
1036         ha_end = ha_start + hdisplay;
1037     }
1038 
1039     if (msm_host->mode_flags & MIPI_DSI_MODE_VIDEO) {
1040         if (msm_host->dsc)
1041             dsi_update_dsc_timing(msm_host, false, mode->hdisplay);
1042 
1043         dsi_write(msm_host, REG_DSI_ACTIVE_H,
1044             DSI_ACTIVE_H_START(ha_start) |
1045             DSI_ACTIVE_H_END(ha_end));
1046         dsi_write(msm_host, REG_DSI_ACTIVE_V,
1047             DSI_ACTIVE_V_START(va_start) |
1048             DSI_ACTIVE_V_END(va_end));
1049         dsi_write(msm_host, REG_DSI_TOTAL,
1050             DSI_TOTAL_H_TOTAL(h_total - 1) |
1051             DSI_TOTAL_V_TOTAL(v_total - 1));
1052 
1053         dsi_write(msm_host, REG_DSI_ACTIVE_HSYNC,
1054             DSI_ACTIVE_HSYNC_START(hs_start) |
1055             DSI_ACTIVE_HSYNC_END(hs_end));
1056         dsi_write(msm_host, REG_DSI_ACTIVE_VSYNC_HPOS, 0);
1057         dsi_write(msm_host, REG_DSI_ACTIVE_VSYNC_VPOS,
1058             DSI_ACTIVE_VSYNC_VPOS_START(vs_start) |
1059             DSI_ACTIVE_VSYNC_VPOS_END(vs_end));
1060     } else {        /* command mode */
1061         if (msm_host->dsc)
1062             dsi_update_dsc_timing(msm_host, true, mode->hdisplay);
1063 
1064         /* image data and 1 byte write_memory_start cmd */
1065         if (!msm_host->dsc)
1066             wc = hdisplay * dsi_get_bpp(msm_host->format) / 8 + 1;
1067         else
1068             wc = mode->hdisplay / 2 + 1;
1069 
1070         dsi_write(msm_host, REG_DSI_CMD_MDP_STREAM0_CTRL,
1071             DSI_CMD_MDP_STREAM0_CTRL_WORD_COUNT(wc) |
1072             DSI_CMD_MDP_STREAM0_CTRL_VIRTUAL_CHANNEL(
1073                     msm_host->channel) |
1074             DSI_CMD_MDP_STREAM0_CTRL_DATA_TYPE(
1075                     MIPI_DSI_DCS_LONG_WRITE));
1076 
1077         dsi_write(msm_host, REG_DSI_CMD_MDP_STREAM0_TOTAL,
1078             DSI_CMD_MDP_STREAM0_TOTAL_H_TOTAL(hdisplay) |
1079             DSI_CMD_MDP_STREAM0_TOTAL_V_TOTAL(mode->vdisplay));
1080     }
1081 }
1082 
1083 static void dsi_sw_reset(struct msm_dsi_host *msm_host)
1084 {
1085     u32 ctrl;
1086 
1087     ctrl = dsi_read(msm_host, REG_DSI_CTRL);
1088 
1089     if (ctrl & DSI_CTRL_ENABLE) {
1090         dsi_write(msm_host, REG_DSI_CTRL, ctrl & ~DSI_CTRL_ENABLE);
1091         /*
1092          * dsi controller need to be disabled before
1093          * clocks turned on
1094          */
1095         wmb();
1096     }
1097 
1098     dsi_write(msm_host, REG_DSI_CLK_CTRL, DSI_CLK_CTRL_ENABLE_CLKS);
1099     wmb(); /* clocks need to be enabled before reset */
1100 
1101     /* dsi controller can only be reset while clocks are running */
1102     dsi_write(msm_host, REG_DSI_RESET, 1);
1103     msleep(DSI_RESET_TOGGLE_DELAY_MS); /* make sure reset happen */
1104     dsi_write(msm_host, REG_DSI_RESET, 0);
1105     wmb(); /* controller out of reset */
1106 
1107     if (ctrl & DSI_CTRL_ENABLE) {
1108         dsi_write(msm_host, REG_DSI_CTRL, ctrl);
1109         wmb();  /* make sure dsi controller enabled again */
1110     }
1111 }
1112 
1113 static void dsi_op_mode_config(struct msm_dsi_host *msm_host,
1114                     bool video_mode, bool enable)
1115 {
1116     u32 dsi_ctrl;
1117 
1118     dsi_ctrl = dsi_read(msm_host, REG_DSI_CTRL);
1119 
1120     if (!enable) {
1121         dsi_ctrl &= ~(DSI_CTRL_ENABLE | DSI_CTRL_VID_MODE_EN |
1122                 DSI_CTRL_CMD_MODE_EN);
1123         dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_CMD_MDP_DONE |
1124                     DSI_IRQ_MASK_VIDEO_DONE, 0);
1125     } else {
1126         if (video_mode) {
1127             dsi_ctrl |= DSI_CTRL_VID_MODE_EN;
1128         } else {        /* command mode */
1129             dsi_ctrl |= DSI_CTRL_CMD_MODE_EN;
1130             dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_CMD_MDP_DONE, 1);
1131         }
1132         dsi_ctrl |= DSI_CTRL_ENABLE;
1133     }
1134 
1135     dsi_write(msm_host, REG_DSI_CTRL, dsi_ctrl);
1136 }
1137 
1138 static void dsi_set_tx_power_mode(int mode, struct msm_dsi_host *msm_host)
1139 {
1140     u32 data;
1141 
1142     data = dsi_read(msm_host, REG_DSI_CMD_DMA_CTRL);
1143 
1144     if (mode == 0)
1145         data &= ~DSI_CMD_DMA_CTRL_LOW_POWER;
1146     else
1147         data |= DSI_CMD_DMA_CTRL_LOW_POWER;
1148 
1149     dsi_write(msm_host, REG_DSI_CMD_DMA_CTRL, data);
1150 }
1151 
1152 static void dsi_wait4video_done(struct msm_dsi_host *msm_host)
1153 {
1154     u32 ret = 0;
1155     struct device *dev = &msm_host->pdev->dev;
1156 
1157     dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_VIDEO_DONE, 1);
1158 
1159     reinit_completion(&msm_host->video_comp);
1160 
1161     ret = wait_for_completion_timeout(&msm_host->video_comp,
1162             msecs_to_jiffies(70));
1163 
1164     if (ret == 0)
1165         DRM_DEV_ERROR(dev, "wait for video done timed out\n");
1166 
1167     dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_VIDEO_DONE, 0);
1168 }
1169 
1170 static void dsi_wait4video_eng_busy(struct msm_dsi_host *msm_host)
1171 {
1172     if (!(msm_host->mode_flags & MIPI_DSI_MODE_VIDEO))
1173         return;
1174 
1175     if (msm_host->power_on && msm_host->enabled) {
1176         dsi_wait4video_done(msm_host);
1177         /* delay 4 ms to skip BLLP */
1178         usleep_range(2000, 4000);
1179     }
1180 }
1181 
1182 int dsi_tx_buf_alloc_6g(struct msm_dsi_host *msm_host, int size)
1183 {
1184     struct drm_device *dev = msm_host->dev;
1185     struct msm_drm_private *priv = dev->dev_private;
1186     uint64_t iova;
1187     u8 *data;
1188 
1189     data = msm_gem_kernel_new(dev, size, MSM_BO_WC,
1190                     priv->kms->aspace,
1191                     &msm_host->tx_gem_obj, &iova);
1192 
1193     if (IS_ERR(data)) {
1194         msm_host->tx_gem_obj = NULL;
1195         return PTR_ERR(data);
1196     }
1197 
1198     msm_gem_object_set_name(msm_host->tx_gem_obj, "tx_gem");
1199 
1200     msm_host->tx_size = msm_host->tx_gem_obj->size;
1201 
1202     return 0;
1203 }
1204 
1205 int dsi_tx_buf_alloc_v2(struct msm_dsi_host *msm_host, int size)
1206 {
1207     struct drm_device *dev = msm_host->dev;
1208 
1209     msm_host->tx_buf = dma_alloc_coherent(dev->dev, size,
1210                     &msm_host->tx_buf_paddr, GFP_KERNEL);
1211     if (!msm_host->tx_buf)
1212         return -ENOMEM;
1213 
1214     msm_host->tx_size = size;
1215 
1216     return 0;
1217 }
1218 
1219 static void dsi_tx_buf_free(struct msm_dsi_host *msm_host)
1220 {
1221     struct drm_device *dev = msm_host->dev;
1222     struct msm_drm_private *priv;
1223 
1224     /*
1225      * This is possible if we're tearing down before we've had a chance to
1226      * fully initialize. A very real possibility if our probe is deferred,
1227      * in which case we'll hit msm_dsi_host_destroy() without having run
1228      * through the dsi_tx_buf_alloc().
1229      */
1230     if (!dev)
1231         return;
1232 
1233     priv = dev->dev_private;
1234     if (msm_host->tx_gem_obj) {
1235         msm_gem_unpin_iova(msm_host->tx_gem_obj, priv->kms->aspace);
1236         drm_gem_object_put(msm_host->tx_gem_obj);
1237         msm_host->tx_gem_obj = NULL;
1238     }
1239 
1240     if (msm_host->tx_buf)
1241         dma_free_coherent(dev->dev, msm_host->tx_size, msm_host->tx_buf,
1242             msm_host->tx_buf_paddr);
1243 }
1244 
1245 void *dsi_tx_buf_get_6g(struct msm_dsi_host *msm_host)
1246 {
1247     return msm_gem_get_vaddr(msm_host->tx_gem_obj);
1248 }
1249 
1250 void *dsi_tx_buf_get_v2(struct msm_dsi_host *msm_host)
1251 {
1252     return msm_host->tx_buf;
1253 }
1254 
1255 void dsi_tx_buf_put_6g(struct msm_dsi_host *msm_host)
1256 {
1257     msm_gem_put_vaddr(msm_host->tx_gem_obj);
1258 }
1259 
1260 /*
1261  * prepare cmd buffer to be txed
1262  */
1263 static int dsi_cmd_dma_add(struct msm_dsi_host *msm_host,
1264                const struct mipi_dsi_msg *msg)
1265 {
1266     const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
1267     struct mipi_dsi_packet packet;
1268     int len;
1269     int ret;
1270     u8 *data;
1271 
1272     ret = mipi_dsi_create_packet(&packet, msg);
1273     if (ret) {
1274         pr_err("%s: create packet failed, %d\n", __func__, ret);
1275         return ret;
1276     }
1277     len = (packet.size + 3) & (~0x3);
1278 
1279     if (len > msm_host->tx_size) {
1280         pr_err("%s: packet size is too big\n", __func__);
1281         return -EINVAL;
1282     }
1283 
1284     data = cfg_hnd->ops->tx_buf_get(msm_host);
1285     if (IS_ERR(data)) {
1286         ret = PTR_ERR(data);
1287         pr_err("%s: get vaddr failed, %d\n", __func__, ret);
1288         return ret;
1289     }
1290 
1291     /* MSM specific command format in memory */
1292     data[0] = packet.header[1];
1293     data[1] = packet.header[2];
1294     data[2] = packet.header[0];
1295     data[3] = BIT(7); /* Last packet */
1296     if (mipi_dsi_packet_format_is_long(msg->type))
1297         data[3] |= BIT(6);
1298     if (msg->rx_buf && msg->rx_len)
1299         data[3] |= BIT(5);
1300 
1301     /* Long packet */
1302     if (packet.payload && packet.payload_length)
1303         memcpy(data + 4, packet.payload, packet.payload_length);
1304 
1305     /* Append 0xff to the end */
1306     if (packet.size < len)
1307         memset(data + packet.size, 0xff, len - packet.size);
1308 
1309     if (cfg_hnd->ops->tx_buf_put)
1310         cfg_hnd->ops->tx_buf_put(msm_host);
1311 
1312     return len;
1313 }
1314 
1315 /*
1316  * dsi_short_read1_resp: 1 parameter
1317  */
1318 static int dsi_short_read1_resp(u8 *buf, const struct mipi_dsi_msg *msg)
1319 {
1320     u8 *data = msg->rx_buf;
1321     if (data && (msg->rx_len >= 1)) {
1322         *data = buf[1]; /* strip out dcs type */
1323         return 1;
1324     } else {
1325         pr_err("%s: read data does not match with rx_buf len %zu\n",
1326             __func__, msg->rx_len);
1327         return -EINVAL;
1328     }
1329 }
1330 
1331 /*
1332  * dsi_short_read2_resp: 2 parameter
1333  */
1334 static int dsi_short_read2_resp(u8 *buf, const struct mipi_dsi_msg *msg)
1335 {
1336     u8 *data = msg->rx_buf;
1337     if (data && (msg->rx_len >= 2)) {
1338         data[0] = buf[1]; /* strip out dcs type */
1339         data[1] = buf[2];
1340         return 2;
1341     } else {
1342         pr_err("%s: read data does not match with rx_buf len %zu\n",
1343             __func__, msg->rx_len);
1344         return -EINVAL;
1345     }
1346 }
1347 
1348 static int dsi_long_read_resp(u8 *buf, const struct mipi_dsi_msg *msg)
1349 {
1350     /* strip out 4 byte dcs header */
1351     if (msg->rx_buf && msg->rx_len)
1352         memcpy(msg->rx_buf, buf + 4, msg->rx_len);
1353 
1354     return msg->rx_len;
1355 }
1356 
1357 int dsi_dma_base_get_6g(struct msm_dsi_host *msm_host, uint64_t *dma_base)
1358 {
1359     struct drm_device *dev = msm_host->dev;
1360     struct msm_drm_private *priv = dev->dev_private;
1361 
1362     if (!dma_base)
1363         return -EINVAL;
1364 
1365     return msm_gem_get_and_pin_iova(msm_host->tx_gem_obj,
1366                 priv->kms->aspace, dma_base);
1367 }
1368 
1369 int dsi_dma_base_get_v2(struct msm_dsi_host *msm_host, uint64_t *dma_base)
1370 {
1371     if (!dma_base)
1372         return -EINVAL;
1373 
1374     *dma_base = msm_host->tx_buf_paddr;
1375     return 0;
1376 }
1377 
1378 static int dsi_cmd_dma_tx(struct msm_dsi_host *msm_host, int len)
1379 {
1380     const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
1381     int ret;
1382     uint64_t dma_base;
1383     bool triggered;
1384 
1385     ret = cfg_hnd->ops->dma_base_get(msm_host, &dma_base);
1386     if (ret) {
1387         pr_err("%s: failed to get iova: %d\n", __func__, ret);
1388         return ret;
1389     }
1390 
1391     reinit_completion(&msm_host->dma_comp);
1392 
1393     dsi_wait4video_eng_busy(msm_host);
1394 
1395     triggered = msm_dsi_manager_cmd_xfer_trigger(
1396                         msm_host->id, dma_base, len);
1397     if (triggered) {
1398         ret = wait_for_completion_timeout(&msm_host->dma_comp,
1399                     msecs_to_jiffies(200));
1400         DBG("ret=%d", ret);
1401         if (ret == 0)
1402             ret = -ETIMEDOUT;
1403         else
1404             ret = len;
1405     } else
1406         ret = len;
1407 
1408     return ret;
1409 }
1410 
1411 static int dsi_cmd_dma_rx(struct msm_dsi_host *msm_host,
1412             u8 *buf, int rx_byte, int pkt_size)
1413 {
1414     u32 *temp, data;
1415     int i, j = 0, cnt;
1416     u32 read_cnt;
1417     u8 reg[16];
1418     int repeated_bytes = 0;
1419     int buf_offset = buf - msm_host->rx_buf;
1420 
1421     temp = (u32 *)reg;
1422     cnt = (rx_byte + 3) >> 2;
1423     if (cnt > 4)
1424         cnt = 4; /* 4 x 32 bits registers only */
1425 
1426     if (rx_byte == 4)
1427         read_cnt = 4;
1428     else
1429         read_cnt = pkt_size + 6;
1430 
1431     /*
1432      * In case of multiple reads from the panel, after the first read, there
1433      * is possibility that there are some bytes in the payload repeating in
1434      * the RDBK_DATA registers. Since we read all the parameters from the
1435      * panel right from the first byte for every pass. We need to skip the
1436      * repeating bytes and then append the new parameters to the rx buffer.
1437      */
1438     if (read_cnt > 16) {
1439         int bytes_shifted;
1440         /* Any data more than 16 bytes will be shifted out.
1441          * The temp read buffer should already contain these bytes.
1442          * The remaining bytes in read buffer are the repeated bytes.
1443          */
1444         bytes_shifted = read_cnt - 16;
1445         repeated_bytes = buf_offset - bytes_shifted;
1446     }
1447 
1448     for (i = cnt - 1; i >= 0; i--) {
1449         data = dsi_read(msm_host, REG_DSI_RDBK_DATA(i));
1450         *temp++ = ntohl(data); /* to host byte order */
1451         DBG("data = 0x%x and ntohl(data) = 0x%x", data, ntohl(data));
1452     }
1453 
1454     for (i = repeated_bytes; i < 16; i++)
1455         buf[j++] = reg[i];
1456 
1457     return j;
1458 }
1459 
1460 static int dsi_cmds2buf_tx(struct msm_dsi_host *msm_host,
1461                 const struct mipi_dsi_msg *msg)
1462 {
1463     int len, ret;
1464     int bllp_len = msm_host->mode->hdisplay *
1465             dsi_get_bpp(msm_host->format) / 8;
1466 
1467     len = dsi_cmd_dma_add(msm_host, msg);
1468     if (len < 0) {
1469         pr_err("%s: failed to add cmd type = 0x%x\n",
1470             __func__,  msg->type);
1471         return len;
1472     }
1473 
1474     /* for video mode, do not send cmds more than
1475     * one pixel line, since it only transmit it
1476     * during BLLP.
1477     */
1478     /* TODO: if the command is sent in LP mode, the bit rate is only
1479      * half of esc clk rate. In this case, if the video is already
1480      * actively streaming, we need to check more carefully if the
1481      * command can be fit into one BLLP.
1482      */
1483     if ((msm_host->mode_flags & MIPI_DSI_MODE_VIDEO) && (len > bllp_len)) {
1484         pr_err("%s: cmd cannot fit into BLLP period, len=%d\n",
1485             __func__, len);
1486         return -EINVAL;
1487     }
1488 
1489     ret = dsi_cmd_dma_tx(msm_host, len);
1490     if (ret < 0) {
1491         pr_err("%s: cmd dma tx failed, type=0x%x, data0=0x%x, len=%d, ret=%d\n",
1492             __func__, msg->type, (*(u8 *)(msg->tx_buf)), len, ret);
1493         return ret;
1494     } else if (ret < len) {
1495         pr_err("%s: cmd dma tx failed, type=0x%x, data0=0x%x, ret=%d len=%d\n",
1496             __func__, msg->type, (*(u8 *)(msg->tx_buf)), ret, len);
1497         return -EIO;
1498     }
1499 
1500     return len;
1501 }
1502 
1503 static void dsi_hpd_worker(struct work_struct *work)
1504 {
1505     struct msm_dsi_host *msm_host =
1506         container_of(work, struct msm_dsi_host, hpd_work);
1507 
1508     drm_helper_hpd_irq_event(msm_host->dev);
1509 }
1510 
1511 static void dsi_err_worker(struct work_struct *work)
1512 {
1513     struct msm_dsi_host *msm_host =
1514         container_of(work, struct msm_dsi_host, err_work);
1515     u32 status = msm_host->err_work_state;
1516 
1517     pr_err_ratelimited("%s: status=%x\n", __func__, status);
1518     if (status & DSI_ERR_STATE_MDP_FIFO_UNDERFLOW)
1519         dsi_sw_reset(msm_host);
1520 
1521     /* It is safe to clear here because error irq is disabled. */
1522     msm_host->err_work_state = 0;
1523 
1524     /* enable dsi error interrupt */
1525     dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_ERROR, 1);
1526 }
1527 
1528 static void dsi_ack_err_status(struct msm_dsi_host *msm_host)
1529 {
1530     u32 status;
1531 
1532     status = dsi_read(msm_host, REG_DSI_ACK_ERR_STATUS);
1533 
1534     if (status) {
1535         dsi_write(msm_host, REG_DSI_ACK_ERR_STATUS, status);
1536         /* Writing of an extra 0 needed to clear error bits */
1537         dsi_write(msm_host, REG_DSI_ACK_ERR_STATUS, 0);
1538         msm_host->err_work_state |= DSI_ERR_STATE_ACK;
1539     }
1540 }
1541 
1542 static void dsi_timeout_status(struct msm_dsi_host *msm_host)
1543 {
1544     u32 status;
1545 
1546     status = dsi_read(msm_host, REG_DSI_TIMEOUT_STATUS);
1547 
1548     if (status) {
1549         dsi_write(msm_host, REG_DSI_TIMEOUT_STATUS, status);
1550         msm_host->err_work_state |= DSI_ERR_STATE_TIMEOUT;
1551     }
1552 }
1553 
1554 static void dsi_dln0_phy_err(struct msm_dsi_host *msm_host)
1555 {
1556     u32 status;
1557 
1558     status = dsi_read(msm_host, REG_DSI_DLN0_PHY_ERR);
1559 
1560     if (status & (DSI_DLN0_PHY_ERR_DLN0_ERR_ESC |
1561             DSI_DLN0_PHY_ERR_DLN0_ERR_SYNC_ESC |
1562             DSI_DLN0_PHY_ERR_DLN0_ERR_CONTROL |
1563             DSI_DLN0_PHY_ERR_DLN0_ERR_CONTENTION_LP0 |
1564             DSI_DLN0_PHY_ERR_DLN0_ERR_CONTENTION_LP1)) {
1565         dsi_write(msm_host, REG_DSI_DLN0_PHY_ERR, status);
1566         msm_host->err_work_state |= DSI_ERR_STATE_DLN0_PHY;
1567     }
1568 }
1569 
1570 static void dsi_fifo_status(struct msm_dsi_host *msm_host)
1571 {
1572     u32 status;
1573 
1574     status = dsi_read(msm_host, REG_DSI_FIFO_STATUS);
1575 
1576     /* fifo underflow, overflow */
1577     if (status) {
1578         dsi_write(msm_host, REG_DSI_FIFO_STATUS, status);
1579         msm_host->err_work_state |= DSI_ERR_STATE_FIFO;
1580         if (status & DSI_FIFO_STATUS_CMD_MDP_FIFO_UNDERFLOW)
1581             msm_host->err_work_state |=
1582                     DSI_ERR_STATE_MDP_FIFO_UNDERFLOW;
1583     }
1584 }
1585 
1586 static void dsi_status(struct msm_dsi_host *msm_host)
1587 {
1588     u32 status;
1589 
1590     status = dsi_read(msm_host, REG_DSI_STATUS0);
1591 
1592     if (status & DSI_STATUS0_INTERLEAVE_OP_CONTENTION) {
1593         dsi_write(msm_host, REG_DSI_STATUS0, status);
1594         msm_host->err_work_state |=
1595             DSI_ERR_STATE_INTERLEAVE_OP_CONTENTION;
1596     }
1597 }
1598 
1599 static void dsi_clk_status(struct msm_dsi_host *msm_host)
1600 {
1601     u32 status;
1602 
1603     status = dsi_read(msm_host, REG_DSI_CLK_STATUS);
1604 
1605     if (status & DSI_CLK_STATUS_PLL_UNLOCKED) {
1606         dsi_write(msm_host, REG_DSI_CLK_STATUS, status);
1607         msm_host->err_work_state |= DSI_ERR_STATE_PLL_UNLOCKED;
1608     }
1609 }
1610 
1611 static void dsi_error(struct msm_dsi_host *msm_host)
1612 {
1613     /* disable dsi error interrupt */
1614     dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_ERROR, 0);
1615 
1616     dsi_clk_status(msm_host);
1617     dsi_fifo_status(msm_host);
1618     dsi_ack_err_status(msm_host);
1619     dsi_timeout_status(msm_host);
1620     dsi_status(msm_host);
1621     dsi_dln0_phy_err(msm_host);
1622 
1623     queue_work(msm_host->workqueue, &msm_host->err_work);
1624 }
1625 
1626 static irqreturn_t dsi_host_irq(int irq, void *ptr)
1627 {
1628     struct msm_dsi_host *msm_host = ptr;
1629     u32 isr;
1630     unsigned long flags;
1631 
1632     if (!msm_host->ctrl_base)
1633         return IRQ_HANDLED;
1634 
1635     spin_lock_irqsave(&msm_host->intr_lock, flags);
1636     isr = dsi_read(msm_host, REG_DSI_INTR_CTRL);
1637     dsi_write(msm_host, REG_DSI_INTR_CTRL, isr);
1638     spin_unlock_irqrestore(&msm_host->intr_lock, flags);
1639 
1640     DBG("isr=0x%x, id=%d", isr, msm_host->id);
1641 
1642     if (isr & DSI_IRQ_ERROR)
1643         dsi_error(msm_host);
1644 
1645     if (isr & DSI_IRQ_VIDEO_DONE)
1646         complete(&msm_host->video_comp);
1647 
1648     if (isr & DSI_IRQ_CMD_DMA_DONE)
1649         complete(&msm_host->dma_comp);
1650 
1651     return IRQ_HANDLED;
1652 }
1653 
1654 static int dsi_host_init_panel_gpios(struct msm_dsi_host *msm_host,
1655             struct device *panel_device)
1656 {
1657     msm_host->disp_en_gpio = devm_gpiod_get_optional(panel_device,
1658                              "disp-enable",
1659                              GPIOD_OUT_LOW);
1660     if (IS_ERR(msm_host->disp_en_gpio)) {
1661         DBG("cannot get disp-enable-gpios %ld",
1662                 PTR_ERR(msm_host->disp_en_gpio));
1663         return PTR_ERR(msm_host->disp_en_gpio);
1664     }
1665 
1666     msm_host->te_gpio = devm_gpiod_get_optional(panel_device, "disp-te",
1667                                 GPIOD_IN);
1668     if (IS_ERR(msm_host->te_gpio)) {
1669         DBG("cannot get disp-te-gpios %ld", PTR_ERR(msm_host->te_gpio));
1670         return PTR_ERR(msm_host->te_gpio);
1671     }
1672 
1673     return 0;
1674 }
1675 
1676 static int dsi_host_attach(struct mipi_dsi_host *host,
1677                     struct mipi_dsi_device *dsi)
1678 {
1679     struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1680     int ret;
1681 
1682     if (dsi->lanes > msm_host->num_data_lanes)
1683         return -EINVAL;
1684 
1685     msm_host->channel = dsi->channel;
1686     msm_host->lanes = dsi->lanes;
1687     msm_host->format = dsi->format;
1688     msm_host->mode_flags = dsi->mode_flags;
1689 
1690     /* Some gpios defined in panel DT need to be controlled by host */
1691     ret = dsi_host_init_panel_gpios(msm_host, &dsi->dev);
1692     if (ret)
1693         return ret;
1694 
1695     ret = dsi_dev_attach(msm_host->pdev);
1696     if (ret)
1697         return ret;
1698 
1699     DBG("id=%d", msm_host->id);
1700     if (msm_host->dev)
1701         queue_work(msm_host->workqueue, &msm_host->hpd_work);
1702 
1703     return 0;
1704 }
1705 
1706 static int dsi_host_detach(struct mipi_dsi_host *host,
1707                     struct mipi_dsi_device *dsi)
1708 {
1709     struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1710 
1711     dsi_dev_detach(msm_host->pdev);
1712 
1713     msm_host->device_node = NULL;
1714 
1715     DBG("id=%d", msm_host->id);
1716     if (msm_host->dev)
1717         queue_work(msm_host->workqueue, &msm_host->hpd_work);
1718 
1719     return 0;
1720 }
1721 
1722 static ssize_t dsi_host_transfer(struct mipi_dsi_host *host,
1723                     const struct mipi_dsi_msg *msg)
1724 {
1725     struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1726     int ret;
1727 
1728     if (!msg || !msm_host->power_on)
1729         return -EINVAL;
1730 
1731     mutex_lock(&msm_host->cmd_mutex);
1732     ret = msm_dsi_manager_cmd_xfer(msm_host->id, msg);
1733     mutex_unlock(&msm_host->cmd_mutex);
1734 
1735     return ret;
1736 }
1737 
1738 static const struct mipi_dsi_host_ops dsi_host_ops = {
1739     .attach = dsi_host_attach,
1740     .detach = dsi_host_detach,
1741     .transfer = dsi_host_transfer,
1742 };
1743 
1744 /*
1745  * List of supported physical to logical lane mappings.
1746  * For example, the 2nd entry represents the following mapping:
1747  *
1748  * "3012": Logic 3->Phys 0; Logic 0->Phys 1; Logic 1->Phys 2; Logic 2->Phys 3;
1749  */
1750 static const int supported_data_lane_swaps[][4] = {
1751     { 0, 1, 2, 3 },
1752     { 3, 0, 1, 2 },
1753     { 2, 3, 0, 1 },
1754     { 1, 2, 3, 0 },
1755     { 0, 3, 2, 1 },
1756     { 1, 0, 3, 2 },
1757     { 2, 1, 0, 3 },
1758     { 3, 2, 1, 0 },
1759 };
1760 
1761 static int dsi_host_parse_lane_data(struct msm_dsi_host *msm_host,
1762                     struct device_node *ep)
1763 {
1764     struct device *dev = &msm_host->pdev->dev;
1765     struct property *prop;
1766     u32 lane_map[4];
1767     int ret, i, len, num_lanes;
1768 
1769     prop = of_find_property(ep, "data-lanes", &len);
1770     if (!prop) {
1771         DRM_DEV_DEBUG(dev,
1772             "failed to find data lane mapping, using default\n");
1773         /* Set the number of date lanes to 4 by default. */
1774         msm_host->num_data_lanes = 4;
1775         return 0;
1776     }
1777 
1778     num_lanes = drm_of_get_data_lanes_count(ep, 1, 4);
1779     if (num_lanes < 0) {
1780         DRM_DEV_ERROR(dev, "bad number of data lanes\n");
1781         return num_lanes;
1782     }
1783 
1784     msm_host->num_data_lanes = num_lanes;
1785 
1786     ret = of_property_read_u32_array(ep, "data-lanes", lane_map,
1787                      num_lanes);
1788     if (ret) {
1789         DRM_DEV_ERROR(dev, "failed to read lane data\n");
1790         return ret;
1791     }
1792 
1793     /*
1794      * compare DT specified physical-logical lane mappings with the ones
1795      * supported by hardware
1796      */
1797     for (i = 0; i < ARRAY_SIZE(supported_data_lane_swaps); i++) {
1798         const int *swap = supported_data_lane_swaps[i];
1799         int j;
1800 
1801         /*
1802          * the data-lanes array we get from DT has a logical->physical
1803          * mapping. The "data lane swap" register field represents
1804          * supported configurations in a physical->logical mapping.
1805          * Translate the DT mapping to what we understand and find a
1806          * configuration that works.
1807          */
1808         for (j = 0; j < num_lanes; j++) {
1809             if (lane_map[j] < 0 || lane_map[j] > 3)
1810                 DRM_DEV_ERROR(dev, "bad physical lane entry %u\n",
1811                     lane_map[j]);
1812 
1813             if (swap[lane_map[j]] != j)
1814                 break;
1815         }
1816 
1817         if (j == num_lanes) {
1818             msm_host->dlane_swap = i;
1819             return 0;
1820         }
1821     }
1822 
1823     return -EINVAL;
1824 }
1825 
1826 static u32 dsi_dsc_rc_buf_thresh[DSC_NUM_BUF_RANGES - 1] = {
1827     0x0e, 0x1c, 0x2a, 0x38, 0x46, 0x54, 0x62,
1828     0x69, 0x70, 0x77, 0x79, 0x7b, 0x7d, 0x7e
1829 };
1830 
1831 /* only 8bpc, 8bpp added */
1832 static char min_qp[DSC_NUM_BUF_RANGES] = {
1833     0, 0, 1, 1, 3, 3, 3, 3, 3, 3, 5, 5, 5, 7, 13
1834 };
1835 
1836 static char max_qp[DSC_NUM_BUF_RANGES] = {
1837     4, 4, 5, 6, 7, 7, 7, 8, 9, 10, 11, 12, 13, 13, 15
1838 };
1839 
1840 static char bpg_offset[DSC_NUM_BUF_RANGES] = {
1841     2, 0, 0, -2, -4, -6, -8, -8, -8, -10, -10, -12, -12, -12, -12
1842 };
1843 
1844 static int dsi_populate_dsc_params(struct msm_display_dsc_config *dsc)
1845 {
1846     int mux_words_size;
1847     int groups_per_line, groups_total;
1848     int min_rate_buffer_size;
1849     int hrd_delay;
1850     int pre_num_extra_mux_bits, num_extra_mux_bits;
1851     int slice_bits;
1852     int target_bpp_x16;
1853     int data;
1854     int final_value, final_scale;
1855     int i;
1856 
1857     dsc->drm->rc_model_size = 8192;
1858     dsc->drm->first_line_bpg_offset = 12;
1859     dsc->drm->rc_edge_factor = 6;
1860     dsc->drm->rc_tgt_offset_high = 3;
1861     dsc->drm->rc_tgt_offset_low = 3;
1862     dsc->drm->simple_422 = 0;
1863     dsc->drm->convert_rgb = 1;
1864     dsc->drm->vbr_enable = 0;
1865 
1866     /* handle only bpp = bpc = 8 */
1867     for (i = 0; i < DSC_NUM_BUF_RANGES - 1 ; i++)
1868         dsc->drm->rc_buf_thresh[i] = dsi_dsc_rc_buf_thresh[i];
1869 
1870     for (i = 0; i < DSC_NUM_BUF_RANGES; i++) {
1871         dsc->drm->rc_range_params[i].range_min_qp = min_qp[i];
1872         dsc->drm->rc_range_params[i].range_max_qp = max_qp[i];
1873         dsc->drm->rc_range_params[i].range_bpg_offset = bpg_offset[i];
1874     }
1875 
1876     dsc->drm->initial_offset = 6144; /* Not bpp 12 */
1877     if (dsc->drm->bits_per_pixel != 8)
1878         dsc->drm->initial_offset = 2048;    /* bpp = 12 */
1879 
1880     mux_words_size = 48;        /* bpc == 8/10 */
1881     if (dsc->drm->bits_per_component == 12)
1882         mux_words_size = 64;
1883 
1884     dsc->drm->initial_xmit_delay = 512;
1885     dsc->drm->initial_scale_value = 32;
1886     dsc->drm->first_line_bpg_offset = 12;
1887     dsc->drm->line_buf_depth = dsc->drm->bits_per_component + 1;
1888 
1889     /* bpc 8 */
1890     dsc->drm->flatness_min_qp = 3;
1891     dsc->drm->flatness_max_qp = 12;
1892     dsc->drm->rc_quant_incr_limit0 = 11;
1893     dsc->drm->rc_quant_incr_limit1 = 11;
1894     dsc->drm->mux_word_size = DSC_MUX_WORD_SIZE_8_10_BPC;
1895 
1896     /* FIXME: need to call drm_dsc_compute_rc_parameters() so that rest of
1897      * params are calculated
1898      */
1899     groups_per_line = DIV_ROUND_UP(dsc->drm->slice_width, 3);
1900     dsc->drm->slice_chunk_size = dsc->drm->slice_width * dsc->drm->bits_per_pixel / 8;
1901     if ((dsc->drm->slice_width * dsc->drm->bits_per_pixel) % 8)
1902         dsc->drm->slice_chunk_size++;
1903 
1904     /* rbs-min */
1905     min_rate_buffer_size =  dsc->drm->rc_model_size - dsc->drm->initial_offset +
1906                 dsc->drm->initial_xmit_delay * dsc->drm->bits_per_pixel +
1907                 groups_per_line * dsc->drm->first_line_bpg_offset;
1908 
1909     hrd_delay = DIV_ROUND_UP(min_rate_buffer_size, dsc->drm->bits_per_pixel);
1910 
1911     dsc->drm->initial_dec_delay = hrd_delay - dsc->drm->initial_xmit_delay;
1912 
1913     dsc->drm->initial_scale_value = 8 * dsc->drm->rc_model_size /
1914                        (dsc->drm->rc_model_size - dsc->drm->initial_offset);
1915 
1916     slice_bits = 8 * dsc->drm->slice_chunk_size * dsc->drm->slice_height;
1917 
1918     groups_total = groups_per_line * dsc->drm->slice_height;
1919 
1920     data = dsc->drm->first_line_bpg_offset * 2048;
1921 
1922     dsc->drm->nfl_bpg_offset = DIV_ROUND_UP(data, (dsc->drm->slice_height - 1));
1923 
1924     pre_num_extra_mux_bits = 3 * (mux_words_size + (4 * dsc->drm->bits_per_component + 4) - 2);
1925 
1926     num_extra_mux_bits = pre_num_extra_mux_bits - (mux_words_size -
1927                  ((slice_bits - pre_num_extra_mux_bits) % mux_words_size));
1928 
1929     data = 2048 * (dsc->drm->rc_model_size - dsc->drm->initial_offset + num_extra_mux_bits);
1930     dsc->drm->slice_bpg_offset = DIV_ROUND_UP(data, groups_total);
1931 
1932     /* bpp * 16 + 0.5 */
1933     data = dsc->drm->bits_per_pixel * 16;
1934     data *= 2;
1935     data++;
1936     data /= 2;
1937     target_bpp_x16 = data;
1938 
1939     data = (dsc->drm->initial_xmit_delay * target_bpp_x16) / 16;
1940     final_value =  dsc->drm->rc_model_size - data + num_extra_mux_bits;
1941     dsc->drm->final_offset = final_value;
1942 
1943     final_scale = 8 * dsc->drm->rc_model_size / (dsc->drm->rc_model_size - final_value);
1944 
1945     data = (final_scale - 9) * (dsc->drm->nfl_bpg_offset + dsc->drm->slice_bpg_offset);
1946     dsc->drm->scale_increment_interval = (2048 * dsc->drm->final_offset) / data;
1947 
1948     dsc->drm->scale_decrement_interval = groups_per_line / (dsc->drm->initial_scale_value - 8);
1949 
1950     return 0;
1951 }
1952 
1953 static int dsi_host_parse_dt(struct msm_dsi_host *msm_host)
1954 {
1955     struct device *dev = &msm_host->pdev->dev;
1956     struct device_node *np = dev->of_node;
1957     struct device_node *endpoint, *device_node;
1958     int ret = 0;
1959 
1960     /*
1961      * Get the endpoint of the output port of the DSI host. In our case,
1962      * this is mapped to port number with reg = 1. Don't return an error if
1963      * the remote endpoint isn't defined. It's possible that there is
1964      * nothing connected to the dsi output.
1965      */
1966     endpoint = of_graph_get_endpoint_by_regs(np, 1, -1);
1967     if (!endpoint) {
1968         DRM_DEV_DEBUG(dev, "%s: no endpoint\n", __func__);
1969         return 0;
1970     }
1971 
1972     ret = dsi_host_parse_lane_data(msm_host, endpoint);
1973     if (ret) {
1974         DRM_DEV_ERROR(dev, "%s: invalid lane configuration %d\n",
1975             __func__, ret);
1976         ret = -EINVAL;
1977         goto err;
1978     }
1979 
1980     /* Get panel node from the output port's endpoint data */
1981     device_node = of_graph_get_remote_node(np, 1, 0);
1982     if (!device_node) {
1983         DRM_DEV_DEBUG(dev, "%s: no valid device\n", __func__);
1984         ret = -ENODEV;
1985         goto err;
1986     }
1987 
1988     msm_host->device_node = device_node;
1989 
1990     if (of_property_read_bool(np, "syscon-sfpb")) {
1991         msm_host->sfpb = syscon_regmap_lookup_by_phandle(np,
1992                     "syscon-sfpb");
1993         if (IS_ERR(msm_host->sfpb)) {
1994             DRM_DEV_ERROR(dev, "%s: failed to get sfpb regmap\n",
1995                 __func__);
1996             ret = PTR_ERR(msm_host->sfpb);
1997         }
1998     }
1999 
2000     of_node_put(device_node);
2001 
2002 err:
2003     of_node_put(endpoint);
2004 
2005     return ret;
2006 }
2007 
2008 static int dsi_host_get_id(struct msm_dsi_host *msm_host)
2009 {
2010     struct platform_device *pdev = msm_host->pdev;
2011     const struct msm_dsi_config *cfg = msm_host->cfg_hnd->cfg;
2012     struct resource *res;
2013     int i;
2014 
2015     res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dsi_ctrl");
2016     if (!res)
2017         return -EINVAL;
2018 
2019     for (i = 0; i < cfg->num_dsi; i++) {
2020         if (cfg->io_start[i] == res->start)
2021             return i;
2022     }
2023 
2024     return -EINVAL;
2025 }
2026 
2027 int msm_dsi_host_init(struct msm_dsi *msm_dsi)
2028 {
2029     struct msm_dsi_host *msm_host = NULL;
2030     struct platform_device *pdev = msm_dsi->pdev;
2031     int ret;
2032 
2033     msm_host = devm_kzalloc(&pdev->dev, sizeof(*msm_host), GFP_KERNEL);
2034     if (!msm_host) {
2035         ret = -ENOMEM;
2036         goto fail;
2037     }
2038 
2039     msm_host->pdev = pdev;
2040     msm_dsi->host = &msm_host->base;
2041 
2042     ret = dsi_host_parse_dt(msm_host);
2043     if (ret) {
2044         pr_err("%s: failed to parse dt\n", __func__);
2045         goto fail;
2046     }
2047 
2048     msm_host->ctrl_base = msm_ioremap_size(pdev, "dsi_ctrl", &msm_host->ctrl_size);
2049     if (IS_ERR(msm_host->ctrl_base)) {
2050         pr_err("%s: unable to map Dsi ctrl base\n", __func__);
2051         ret = PTR_ERR(msm_host->ctrl_base);
2052         goto fail;
2053     }
2054 
2055     pm_runtime_enable(&pdev->dev);
2056 
2057     msm_host->cfg_hnd = dsi_get_config(msm_host);
2058     if (!msm_host->cfg_hnd) {
2059         ret = -EINVAL;
2060         pr_err("%s: get config failed\n", __func__);
2061         goto fail;
2062     }
2063 
2064     msm_host->id = dsi_host_get_id(msm_host);
2065     if (msm_host->id < 0) {
2066         ret = msm_host->id;
2067         pr_err("%s: unable to identify DSI host index\n", __func__);
2068         goto fail;
2069     }
2070 
2071     /* fixup base address by io offset */
2072     msm_host->ctrl_base += msm_host->cfg_hnd->cfg->io_offset;
2073 
2074     ret = dsi_regulator_init(msm_host);
2075     if (ret) {
2076         pr_err("%s: regulator init failed\n", __func__);
2077         goto fail;
2078     }
2079 
2080     ret = dsi_clk_init(msm_host);
2081     if (ret) {
2082         pr_err("%s: unable to initialize dsi clks\n", __func__);
2083         goto fail;
2084     }
2085 
2086     msm_host->rx_buf = devm_kzalloc(&pdev->dev, SZ_4K, GFP_KERNEL);
2087     if (!msm_host->rx_buf) {
2088         ret = -ENOMEM;
2089         pr_err("%s: alloc rx temp buf failed\n", __func__);
2090         goto fail;
2091     }
2092 
2093     ret = devm_pm_opp_set_clkname(&pdev->dev, "byte");
2094     if (ret)
2095         return ret;
2096     /* OPP table is optional */
2097     ret = devm_pm_opp_of_add_table(&pdev->dev);
2098     if (ret && ret != -ENODEV) {
2099         dev_err(&pdev->dev, "invalid OPP table in device tree\n");
2100         return ret;
2101     }
2102 
2103     msm_host->irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
2104     if (msm_host->irq < 0) {
2105         ret = msm_host->irq;
2106         dev_err(&pdev->dev, "failed to get irq: %d\n", ret);
2107         return ret;
2108     }
2109 
2110     /* do not autoenable, will be enabled later */
2111     ret = devm_request_irq(&pdev->dev, msm_host->irq, dsi_host_irq,
2112             IRQF_TRIGGER_HIGH | IRQF_NO_AUTOEN,
2113             "dsi_isr", msm_host);
2114     if (ret < 0) {
2115         dev_err(&pdev->dev, "failed to request IRQ%u: %d\n",
2116                 msm_host->irq, ret);
2117         return ret;
2118     }
2119 
2120     init_completion(&msm_host->dma_comp);
2121     init_completion(&msm_host->video_comp);
2122     mutex_init(&msm_host->dev_mutex);
2123     mutex_init(&msm_host->cmd_mutex);
2124     spin_lock_init(&msm_host->intr_lock);
2125 
2126     /* setup workqueue */
2127     msm_host->workqueue = alloc_ordered_workqueue("dsi_drm_work", 0);
2128     INIT_WORK(&msm_host->err_work, dsi_err_worker);
2129     INIT_WORK(&msm_host->hpd_work, dsi_hpd_worker);
2130 
2131     msm_dsi->id = msm_host->id;
2132 
2133     DBG("Dsi Host %d initialized", msm_host->id);
2134     return 0;
2135 
2136 fail:
2137     return ret;
2138 }
2139 
2140 void msm_dsi_host_destroy(struct mipi_dsi_host *host)
2141 {
2142     struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2143 
2144     DBG("");
2145     dsi_tx_buf_free(msm_host);
2146     if (msm_host->workqueue) {
2147         destroy_workqueue(msm_host->workqueue);
2148         msm_host->workqueue = NULL;
2149     }
2150 
2151     mutex_destroy(&msm_host->cmd_mutex);
2152     mutex_destroy(&msm_host->dev_mutex);
2153 
2154     pm_runtime_disable(&msm_host->pdev->dev);
2155 }
2156 
2157 int msm_dsi_host_modeset_init(struct mipi_dsi_host *host,
2158                     struct drm_device *dev)
2159 {
2160     struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2161     const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
2162     struct drm_panel *panel;
2163     int ret;
2164 
2165     msm_host->dev = dev;
2166     panel = msm_dsi_host_get_panel(&msm_host->base);
2167 
2168     if (!IS_ERR(panel) && panel->dsc) {
2169         struct msm_display_dsc_config *dsc = msm_host->dsc;
2170 
2171         if (!dsc) {
2172             dsc = devm_kzalloc(&msm_host->pdev->dev, sizeof(*dsc), GFP_KERNEL);
2173             if (!dsc)
2174                 return -ENOMEM;
2175             dsc->drm = panel->dsc;
2176             msm_host->dsc = dsc;
2177         }
2178     }
2179 
2180     ret = cfg_hnd->ops->tx_buf_alloc(msm_host, SZ_4K);
2181     if (ret) {
2182         pr_err("%s: alloc tx gem obj failed, %d\n", __func__, ret);
2183         return ret;
2184     }
2185 
2186     return 0;
2187 }
2188 
2189 int msm_dsi_host_register(struct mipi_dsi_host *host)
2190 {
2191     struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2192     int ret;
2193 
2194     /* Register mipi dsi host */
2195     if (!msm_host->registered) {
2196         host->dev = &msm_host->pdev->dev;
2197         host->ops = &dsi_host_ops;
2198         ret = mipi_dsi_host_register(host);
2199         if (ret)
2200             return ret;
2201 
2202         msm_host->registered = true;
2203     }
2204 
2205     return 0;
2206 }
2207 
2208 void msm_dsi_host_unregister(struct mipi_dsi_host *host)
2209 {
2210     struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2211 
2212     if (msm_host->registered) {
2213         mipi_dsi_host_unregister(host);
2214         host->dev = NULL;
2215         host->ops = NULL;
2216         msm_host->registered = false;
2217     }
2218 }
2219 
2220 int msm_dsi_host_xfer_prepare(struct mipi_dsi_host *host,
2221                 const struct mipi_dsi_msg *msg)
2222 {
2223     struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2224     const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
2225 
2226     /* TODO: make sure dsi_cmd_mdp is idle.
2227      * Since DSI6G v1.2.0, we can set DSI_TRIG_CTRL.BLOCK_DMA_WITHIN_FRAME
2228      * to ask H/W to wait until cmd mdp is idle. S/W wait is not needed.
2229      * How to handle the old versions? Wait for mdp cmd done?
2230      */
2231 
2232     /*
2233      * mdss interrupt is generated in mdp core clock domain
2234      * mdp clock need to be enabled to receive dsi interrupt
2235      */
2236     pm_runtime_get_sync(&msm_host->pdev->dev);
2237     cfg_hnd->ops->link_clk_set_rate(msm_host);
2238     cfg_hnd->ops->link_clk_enable(msm_host);
2239 
2240     /* TODO: vote for bus bandwidth */
2241 
2242     if (!(msg->flags & MIPI_DSI_MSG_USE_LPM))
2243         dsi_set_tx_power_mode(0, msm_host);
2244 
2245     msm_host->dma_cmd_ctrl_restore = dsi_read(msm_host, REG_DSI_CTRL);
2246     dsi_write(msm_host, REG_DSI_CTRL,
2247         msm_host->dma_cmd_ctrl_restore |
2248         DSI_CTRL_CMD_MODE_EN |
2249         DSI_CTRL_ENABLE);
2250     dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_CMD_DMA_DONE, 1);
2251 
2252     return 0;
2253 }
2254 
2255 void msm_dsi_host_xfer_restore(struct mipi_dsi_host *host,
2256                 const struct mipi_dsi_msg *msg)
2257 {
2258     struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2259     const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
2260 
2261     dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_CMD_DMA_DONE, 0);
2262     dsi_write(msm_host, REG_DSI_CTRL, msm_host->dma_cmd_ctrl_restore);
2263 
2264     if (!(msg->flags & MIPI_DSI_MSG_USE_LPM))
2265         dsi_set_tx_power_mode(1, msm_host);
2266 
2267     /* TODO: unvote for bus bandwidth */
2268 
2269     cfg_hnd->ops->link_clk_disable(msm_host);
2270     pm_runtime_put(&msm_host->pdev->dev);
2271 }
2272 
2273 int msm_dsi_host_cmd_tx(struct mipi_dsi_host *host,
2274                 const struct mipi_dsi_msg *msg)
2275 {
2276     struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2277 
2278     return dsi_cmds2buf_tx(msm_host, msg);
2279 }
2280 
2281 int msm_dsi_host_cmd_rx(struct mipi_dsi_host *host,
2282                 const struct mipi_dsi_msg *msg)
2283 {
2284     struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2285     const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
2286     int data_byte, rx_byte, dlen, end;
2287     int short_response, diff, pkt_size, ret = 0;
2288     char cmd;
2289     int rlen = msg->rx_len;
2290     u8 *buf;
2291 
2292     if (rlen <= 2) {
2293         short_response = 1;
2294         pkt_size = rlen;
2295         rx_byte = 4;
2296     } else {
2297         short_response = 0;
2298         data_byte = 10; /* first read */
2299         if (rlen < data_byte)
2300             pkt_size = rlen;
2301         else
2302             pkt_size = data_byte;
2303         rx_byte = data_byte + 6; /* 4 header + 2 crc */
2304     }
2305 
2306     buf = msm_host->rx_buf;
2307     end = 0;
2308     while (!end) {
2309         u8 tx[2] = {pkt_size & 0xff, pkt_size >> 8};
2310         struct mipi_dsi_msg max_pkt_size_msg = {
2311             .channel = msg->channel,
2312             .type = MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE,
2313             .tx_len = 2,
2314             .tx_buf = tx,
2315         };
2316 
2317         DBG("rlen=%d pkt_size=%d rx_byte=%d",
2318             rlen, pkt_size, rx_byte);
2319 
2320         ret = dsi_cmds2buf_tx(msm_host, &max_pkt_size_msg);
2321         if (ret < 2) {
2322             pr_err("%s: Set max pkt size failed, %d\n",
2323                 __func__, ret);
2324             return -EINVAL;
2325         }
2326 
2327         if ((cfg_hnd->major == MSM_DSI_VER_MAJOR_6G) &&
2328             (cfg_hnd->minor >= MSM_DSI_6G_VER_MINOR_V1_1)) {
2329             /* Clear the RDBK_DATA registers */
2330             dsi_write(msm_host, REG_DSI_RDBK_DATA_CTRL,
2331                     DSI_RDBK_DATA_CTRL_CLR);
2332             wmb(); /* make sure the RDBK registers are cleared */
2333             dsi_write(msm_host, REG_DSI_RDBK_DATA_CTRL, 0);
2334             wmb(); /* release cleared status before transfer */
2335         }
2336 
2337         ret = dsi_cmds2buf_tx(msm_host, msg);
2338         if (ret < 0) {
2339             pr_err("%s: Read cmd Tx failed, %d\n", __func__, ret);
2340             return ret;
2341         } else if (ret < msg->tx_len) {
2342             pr_err("%s: Read cmd Tx failed, too short: %d\n", __func__, ret);
2343             return -ECOMM;
2344         }
2345 
2346         /*
2347          * once cmd_dma_done interrupt received,
2348          * return data from client is ready and stored
2349          * at RDBK_DATA register already
2350          * since rx fifo is 16 bytes, dcs header is kept at first loop,
2351          * after that dcs header lost during shift into registers
2352          */
2353         dlen = dsi_cmd_dma_rx(msm_host, buf, rx_byte, pkt_size);
2354 
2355         if (dlen <= 0)
2356             return 0;
2357 
2358         if (short_response)
2359             break;
2360 
2361         if (rlen <= data_byte) {
2362             diff = data_byte - rlen;
2363             end = 1;
2364         } else {
2365             diff = 0;
2366             rlen -= data_byte;
2367         }
2368 
2369         if (!end) {
2370             dlen -= 2; /* 2 crc */
2371             dlen -= diff;
2372             buf += dlen;    /* next start position */
2373             data_byte = 14; /* NOT first read */
2374             if (rlen < data_byte)
2375                 pkt_size += rlen;
2376             else
2377                 pkt_size += data_byte;
2378             DBG("buf=%p dlen=%d diff=%d", buf, dlen, diff);
2379         }
2380     }
2381 
2382     /*
2383      * For single Long read, if the requested rlen < 10,
2384      * we need to shift the start position of rx
2385      * data buffer to skip the bytes which are not
2386      * updated.
2387      */
2388     if (pkt_size < 10 && !short_response)
2389         buf = msm_host->rx_buf + (10 - rlen);
2390     else
2391         buf = msm_host->rx_buf;
2392 
2393     cmd = buf[0];
2394     switch (cmd) {
2395     case MIPI_DSI_RX_ACKNOWLEDGE_AND_ERROR_REPORT:
2396         pr_err("%s: rx ACK_ERR_PACLAGE\n", __func__);
2397         ret = 0;
2398         break;
2399     case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_1BYTE:
2400     case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_1BYTE:
2401         ret = dsi_short_read1_resp(buf, msg);
2402         break;
2403     case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_2BYTE:
2404     case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_2BYTE:
2405         ret = dsi_short_read2_resp(buf, msg);
2406         break;
2407     case MIPI_DSI_RX_GENERIC_LONG_READ_RESPONSE:
2408     case MIPI_DSI_RX_DCS_LONG_READ_RESPONSE:
2409         ret = dsi_long_read_resp(buf, msg);
2410         break;
2411     default:
2412         pr_warn("%s:Invalid response cmd\n", __func__);
2413         ret = 0;
2414     }
2415 
2416     return ret;
2417 }
2418 
2419 void msm_dsi_host_cmd_xfer_commit(struct mipi_dsi_host *host, u32 dma_base,
2420                   u32 len)
2421 {
2422     struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2423 
2424     dsi_write(msm_host, REG_DSI_DMA_BASE, dma_base);
2425     dsi_write(msm_host, REG_DSI_DMA_LEN, len);
2426     dsi_write(msm_host, REG_DSI_TRIG_DMA, 1);
2427 
2428     /* Make sure trigger happens */
2429     wmb();
2430 }
2431 
2432 void msm_dsi_host_set_phy_mode(struct mipi_dsi_host *host,
2433     struct msm_dsi_phy *src_phy)
2434 {
2435     struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2436 
2437     msm_host->cphy_mode = src_phy->cphy_mode;
2438 }
2439 
2440 void msm_dsi_host_reset_phy(struct mipi_dsi_host *host)
2441 {
2442     struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2443 
2444     DBG("");
2445     dsi_write(msm_host, REG_DSI_PHY_RESET, DSI_PHY_RESET_RESET);
2446     /* Make sure fully reset */
2447     wmb();
2448     udelay(1000);
2449     dsi_write(msm_host, REG_DSI_PHY_RESET, 0);
2450     udelay(100);
2451 }
2452 
2453 void msm_dsi_host_get_phy_clk_req(struct mipi_dsi_host *host,
2454             struct msm_dsi_phy_clk_request *clk_req,
2455             bool is_bonded_dsi)
2456 {
2457     struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2458     const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
2459     int ret;
2460 
2461     ret = cfg_hnd->ops->calc_clk_rate(msm_host, is_bonded_dsi);
2462     if (ret) {
2463         pr_err("%s: unable to calc clk rate, %d\n", __func__, ret);
2464         return;
2465     }
2466 
2467     /* CPHY transmits 16 bits over 7 clock cycles
2468      * "byte_clk" is in units of 16-bits (see dsi_calc_pclk),
2469      * so multiply by 7 to get the "bitclk rate"
2470      */
2471     if (msm_host->cphy_mode)
2472         clk_req->bitclk_rate = msm_host->byte_clk_rate * 7;
2473     else
2474         clk_req->bitclk_rate = msm_host->byte_clk_rate * 8;
2475     clk_req->escclk_rate = msm_host->esc_clk_rate;
2476 }
2477 
2478 void msm_dsi_host_enable_irq(struct mipi_dsi_host *host)
2479 {
2480     struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2481 
2482     enable_irq(msm_host->irq);
2483 }
2484 
2485 void msm_dsi_host_disable_irq(struct mipi_dsi_host *host)
2486 {
2487     struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2488 
2489     disable_irq(msm_host->irq);
2490 }
2491 
2492 int msm_dsi_host_enable(struct mipi_dsi_host *host)
2493 {
2494     struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2495 
2496     dsi_op_mode_config(msm_host,
2497         !!(msm_host->mode_flags & MIPI_DSI_MODE_VIDEO), true);
2498 
2499     /* TODO: clock should be turned off for command mode,
2500      * and only turned on before MDP START.
2501      * This part of code should be enabled once mdp driver support it.
2502      */
2503     /* if (msm_panel->mode == MSM_DSI_CMD_MODE) {
2504      *  dsi_link_clk_disable(msm_host);
2505      *  pm_runtime_put(&msm_host->pdev->dev);
2506      * }
2507      */
2508     msm_host->enabled = true;
2509     return 0;
2510 }
2511 
2512 int msm_dsi_host_disable(struct mipi_dsi_host *host)
2513 {
2514     struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2515 
2516     msm_host->enabled = false;
2517     dsi_op_mode_config(msm_host,
2518         !!(msm_host->mode_flags & MIPI_DSI_MODE_VIDEO), false);
2519 
2520     /* Since we have disabled INTF, the video engine won't stop so that
2521      * the cmd engine will be blocked.
2522      * Reset to disable video engine so that we can send off cmd.
2523      */
2524     dsi_sw_reset(msm_host);
2525 
2526     return 0;
2527 }
2528 
2529 static void msm_dsi_sfpb_config(struct msm_dsi_host *msm_host, bool enable)
2530 {
2531     enum sfpb_ahb_arb_master_port_en en;
2532 
2533     if (!msm_host->sfpb)
2534         return;
2535 
2536     en = enable ? SFPB_MASTER_PORT_ENABLE : SFPB_MASTER_PORT_DISABLE;
2537 
2538     regmap_update_bits(msm_host->sfpb, REG_SFPB_GPREG,
2539             SFPB_GPREG_MASTER_PORT_EN__MASK,
2540             SFPB_GPREG_MASTER_PORT_EN(en));
2541 }
2542 
2543 int msm_dsi_host_power_on(struct mipi_dsi_host *host,
2544             struct msm_dsi_phy_shared_timings *phy_shared_timings,
2545             bool is_bonded_dsi, struct msm_dsi_phy *phy)
2546 {
2547     struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2548     const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
2549     int ret = 0;
2550 
2551     mutex_lock(&msm_host->dev_mutex);
2552     if (msm_host->power_on) {
2553         DBG("dsi host already on");
2554         goto unlock_ret;
2555     }
2556 
2557     msm_dsi_sfpb_config(msm_host, true);
2558 
2559     ret = dsi_host_regulator_enable(msm_host);
2560     if (ret) {
2561         pr_err("%s:Failed to enable vregs.ret=%d\n",
2562             __func__, ret);
2563         goto unlock_ret;
2564     }
2565 
2566     pm_runtime_get_sync(&msm_host->pdev->dev);
2567     ret = cfg_hnd->ops->link_clk_set_rate(msm_host);
2568     if (!ret)
2569         ret = cfg_hnd->ops->link_clk_enable(msm_host);
2570     if (ret) {
2571         pr_err("%s: failed to enable link clocks. ret=%d\n",
2572                __func__, ret);
2573         goto fail_disable_reg;
2574     }
2575 
2576     ret = pinctrl_pm_select_default_state(&msm_host->pdev->dev);
2577     if (ret) {
2578         pr_err("%s: failed to set pinctrl default state, %d\n",
2579             __func__, ret);
2580         goto fail_disable_clk;
2581     }
2582 
2583     dsi_timing_setup(msm_host, is_bonded_dsi);
2584     dsi_sw_reset(msm_host);
2585     dsi_ctrl_config(msm_host, true, phy_shared_timings, phy);
2586 
2587     if (msm_host->disp_en_gpio)
2588         gpiod_set_value(msm_host->disp_en_gpio, 1);
2589 
2590     msm_host->power_on = true;
2591     mutex_unlock(&msm_host->dev_mutex);
2592 
2593     return 0;
2594 
2595 fail_disable_clk:
2596     cfg_hnd->ops->link_clk_disable(msm_host);
2597     pm_runtime_put(&msm_host->pdev->dev);
2598 fail_disable_reg:
2599     dsi_host_regulator_disable(msm_host);
2600 unlock_ret:
2601     mutex_unlock(&msm_host->dev_mutex);
2602     return ret;
2603 }
2604 
2605 int msm_dsi_host_power_off(struct mipi_dsi_host *host)
2606 {
2607     struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2608     const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
2609 
2610     mutex_lock(&msm_host->dev_mutex);
2611     if (!msm_host->power_on) {
2612         DBG("dsi host already off");
2613         goto unlock_ret;
2614     }
2615 
2616     dsi_ctrl_config(msm_host, false, NULL, NULL);
2617 
2618     if (msm_host->disp_en_gpio)
2619         gpiod_set_value(msm_host->disp_en_gpio, 0);
2620 
2621     pinctrl_pm_select_sleep_state(&msm_host->pdev->dev);
2622 
2623     cfg_hnd->ops->link_clk_disable(msm_host);
2624     pm_runtime_put(&msm_host->pdev->dev);
2625 
2626     dsi_host_regulator_disable(msm_host);
2627 
2628     msm_dsi_sfpb_config(msm_host, false);
2629 
2630     DBG("-");
2631 
2632     msm_host->power_on = false;
2633 
2634 unlock_ret:
2635     mutex_unlock(&msm_host->dev_mutex);
2636     return 0;
2637 }
2638 
2639 int msm_dsi_host_set_display_mode(struct mipi_dsi_host *host,
2640                   const struct drm_display_mode *mode)
2641 {
2642     struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2643 
2644     if (msm_host->mode) {
2645         drm_mode_destroy(msm_host->dev, msm_host->mode);
2646         msm_host->mode = NULL;
2647     }
2648 
2649     msm_host->mode = drm_mode_duplicate(msm_host->dev, mode);
2650     if (!msm_host->mode) {
2651         pr_err("%s: cannot duplicate mode\n", __func__);
2652         return -ENOMEM;
2653     }
2654 
2655     return 0;
2656 }
2657 
2658 enum drm_mode_status msm_dsi_host_check_dsc(struct mipi_dsi_host *host,
2659                         const struct drm_display_mode *mode)
2660 {
2661     struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2662     struct msm_display_dsc_config *dsc = msm_host->dsc;
2663     int pic_width = mode->hdisplay;
2664     int pic_height = mode->vdisplay;
2665 
2666     if (!msm_host->dsc)
2667         return MODE_OK;
2668 
2669     if (pic_width % dsc->drm->slice_width) {
2670         pr_err("DSI: pic_width %d has to be multiple of slice %d\n",
2671                pic_width, dsc->drm->slice_width);
2672         return MODE_H_ILLEGAL;
2673     }
2674 
2675     if (pic_height % dsc->drm->slice_height) {
2676         pr_err("DSI: pic_height %d has to be multiple of slice %d\n",
2677                pic_height, dsc->drm->slice_height);
2678         return MODE_V_ILLEGAL;
2679     }
2680 
2681     return MODE_OK;
2682 }
2683 
2684 struct drm_panel *msm_dsi_host_get_panel(struct mipi_dsi_host *host)
2685 {
2686     return of_drm_find_panel(to_msm_dsi_host(host)->device_node);
2687 }
2688 
2689 unsigned long msm_dsi_host_get_mode_flags(struct mipi_dsi_host *host)
2690 {
2691     return to_msm_dsi_host(host)->mode_flags;
2692 }
2693 
2694 struct drm_bridge *msm_dsi_host_get_bridge(struct mipi_dsi_host *host)
2695 {
2696     struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2697 
2698     return of_drm_find_bridge(msm_host->device_node);
2699 }
2700 
2701 void msm_dsi_host_snapshot(struct msm_disp_state *disp_state, struct mipi_dsi_host *host)
2702 {
2703     struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2704 
2705     pm_runtime_get_sync(&msm_host->pdev->dev);
2706 
2707     msm_disp_snapshot_add_block(disp_state, msm_host->ctrl_size,
2708             msm_host->ctrl_base, "dsi%d_ctrl", msm_host->id);
2709 
2710     pm_runtime_put_sync(&msm_host->pdev->dev);
2711 }
2712 
2713 static void msm_dsi_host_video_test_pattern_setup(struct msm_dsi_host *msm_host)
2714 {
2715     u32 reg;
2716 
2717     reg = dsi_read(msm_host, REG_DSI_TEST_PATTERN_GEN_CTRL);
2718 
2719     dsi_write(msm_host, REG_DSI_TEST_PATTERN_GEN_VIDEO_INIT_VAL, 0xff);
2720     /* draw checkered rectangle pattern */
2721     dsi_write(msm_host, REG_DSI_TPG_MAIN_CONTROL,
2722             DSI_TPG_MAIN_CONTROL_CHECKERED_RECTANGLE_PATTERN);
2723     /* use 24-bit RGB test pttern */
2724     dsi_write(msm_host, REG_DSI_TPG_VIDEO_CONFIG,
2725             DSI_TPG_VIDEO_CONFIG_BPP(VIDEO_CONFIG_24BPP) |
2726             DSI_TPG_VIDEO_CONFIG_RGB);
2727 
2728     reg |= DSI_TEST_PATTERN_GEN_CTRL_VIDEO_PATTERN_SEL(VID_MDSS_GENERAL_PATTERN);
2729     dsi_write(msm_host, REG_DSI_TEST_PATTERN_GEN_CTRL, reg);
2730 
2731     DBG("Video test pattern setup done\n");
2732 }
2733 
2734 static void msm_dsi_host_cmd_test_pattern_setup(struct msm_dsi_host *msm_host)
2735 {
2736     u32 reg;
2737 
2738     reg = dsi_read(msm_host, REG_DSI_TEST_PATTERN_GEN_CTRL);
2739 
2740     /* initial value for test pattern */
2741     dsi_write(msm_host, REG_DSI_TEST_PATTERN_GEN_CMD_MDP_INIT_VAL0, 0xff);
2742 
2743     reg |= DSI_TEST_PATTERN_GEN_CTRL_CMD_MDP_STREAM0_PATTERN_SEL(CMD_MDP_MDSS_GENERAL_PATTERN);
2744 
2745     dsi_write(msm_host, REG_DSI_TEST_PATTERN_GEN_CTRL, reg);
2746     /* draw checkered rectangle pattern */
2747     dsi_write(msm_host, REG_DSI_TPG_MAIN_CONTROL2,
2748             DSI_TPG_MAIN_CONTROL2_CMD_MDP0_CHECKERED_RECTANGLE_PATTERN);
2749 
2750     DBG("Cmd test pattern setup done\n");
2751 }
2752 
2753 void msm_dsi_host_test_pattern_en(struct mipi_dsi_host *host)
2754 {
2755     struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2756     bool is_video_mode = !!(msm_host->mode_flags & MIPI_DSI_MODE_VIDEO);
2757     u32 reg;
2758 
2759     if (is_video_mode)
2760         msm_dsi_host_video_test_pattern_setup(msm_host);
2761     else
2762         msm_dsi_host_cmd_test_pattern_setup(msm_host);
2763 
2764     reg = dsi_read(msm_host, REG_DSI_TEST_PATTERN_GEN_CTRL);
2765     /* enable the test pattern generator */
2766     dsi_write(msm_host, REG_DSI_TEST_PATTERN_GEN_CTRL, (reg | DSI_TEST_PATTERN_GEN_CTRL_EN));
2767 
2768     /* for command mode need to trigger one frame from tpg */
2769     if (!is_video_mode)
2770         dsi_write(msm_host, REG_DSI_TEST_PATTERN_GEN_CMD_STREAM0_TRIGGER,
2771                 DSI_TEST_PATTERN_GEN_CMD_STREAM0_TRIGGER_SW_TRIGGER);
2772 }
2773 
2774 struct msm_display_dsc_config *msm_dsi_host_get_dsc_config(struct mipi_dsi_host *host)
2775 {
2776     struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2777 
2778     return msm_host->dsc;
2779 }