xref: /openbmc/linux/drivers/gpu/drm/mediatek/mtk_dsi.c (revision 5ee9cd065836e5934710ca35653bce7905add20b)
11802d0beSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
22e54c14eSCK Hu /*
32e54c14eSCK Hu  * Copyright (c) 2015 MediaTek Inc.
42e54c14eSCK Hu  */
52e54c14eSCK Hu 
62e54c14eSCK Hu #include <linux/clk.h>
72e54c14eSCK Hu #include <linux/component.h>
8f752413eSDan Carpenter #include <linux/iopoll.h>
9dd5080a5Sshaoming chen #include <linux/irq.h>
102e54c14eSCK Hu #include <linux/of.h>
112e54c14eSCK Hu #include <linux/of_platform.h>
122e54c14eSCK Hu #include <linux/phy/phy.h>
132e54c14eSCK Hu #include <linux/platform_device.h>
14605c8375SEnric Balletbo i Serra #include <linux/reset.h>
159aef5867SSam Ravnborg 
1621898816Sshaoming chen #include <video/mipi_display.h>
172e54c14eSCK Hu #include <video/videomode.h>
182e54c14eSCK Hu 
199aef5867SSam Ravnborg #include <drm/drm_atomic_helper.h>
20ee68c743SBoris Brezillon #include <drm/drm_bridge.h>
21a9d9fea7SEnric Balletbo i Serra #include <drm/drm_bridge_connector.h>
229aef5867SSam Ravnborg #include <drm/drm_mipi_dsi.h>
239aef5867SSam Ravnborg #include <drm/drm_of.h>
249aef5867SSam Ravnborg #include <drm/drm_panel.h>
259aef5867SSam Ravnborg #include <drm/drm_print.h>
269aef5867SSam Ravnborg #include <drm/drm_probe_helper.h>
27b534c4f5SThomas Zimmermann #include <drm/drm_simple_kms_helper.h>
289aef5867SSam Ravnborg 
291d33f13aSCK Hu #include "mtk_disp_drv.h"
302e54c14eSCK Hu #include "mtk_drm_ddp_comp.h"
31807e2f3fSMiles Chen #include "mtk_drm_drv.h"
322e54c14eSCK Hu 
332e54c14eSCK Hu #define DSI_START		0x00
342e54c14eSCK Hu 
35dd5080a5Sshaoming chen #define DSI_INTEN		0x08
36dd5080a5Sshaoming chen 
37dd5080a5Sshaoming chen #define DSI_INTSTA		0x0c
38dd5080a5Sshaoming chen #define LPRX_RD_RDY_INT_FLAG		BIT(0)
39dd5080a5Sshaoming chen #define CMD_DONE_INT_FLAG		BIT(1)
40dd5080a5Sshaoming chen #define TE_RDY_INT_FLAG			BIT(2)
41dd5080a5Sshaoming chen #define VM_DONE_INT_FLAG		BIT(3)
42dd5080a5Sshaoming chen #define EXT_TE_RDY_INT_FLAG		BIT(4)
43dd5080a5Sshaoming chen #define DSI_BUSY			BIT(31)
44dd5080a5Sshaoming chen 
452e54c14eSCK Hu #define DSI_CON_CTRL		0x10
462e54c14eSCK Hu #define DSI_RESET			BIT(0)
472e54c14eSCK Hu #define DSI_EN				BIT(1)
4875374fc2SJitao Shi #define DPHY_RESET			BIT(2)
492e54c14eSCK Hu 
502e54c14eSCK Hu #define DSI_MODE_CTRL		0x14
512e54c14eSCK Hu #define MODE				(3)
522e54c14eSCK Hu #define CMD_MODE			0
532e54c14eSCK Hu #define SYNC_PULSE_MODE			1
542e54c14eSCK Hu #define SYNC_EVENT_MODE			2
552e54c14eSCK Hu #define BURST_MODE			3
562e54c14eSCK Hu #define FRM_MODE			BIT(16)
572e54c14eSCK Hu #define MIX_MODE			BIT(17)
582e54c14eSCK Hu 
592e54c14eSCK Hu #define DSI_TXRX_CTRL		0x18
6080a5cfd6Syt.shen@mediatek.com #define VC_NUM				BIT(1)
612e54c14eSCK Hu #define LANE_NUM			(0xf << 2)
622e54c14eSCK Hu #define DIS_EOT				BIT(6)
632e54c14eSCK Hu #define NULL_EN				BIT(7)
642e54c14eSCK Hu #define TE_FREERUN			BIT(8)
652e54c14eSCK Hu #define EXT_TE_EN			BIT(9)
662e54c14eSCK Hu #define EXT_TE_EDGE			BIT(10)
672e54c14eSCK Hu #define MAX_RTN_SIZE			(0xf << 12)
682e54c14eSCK Hu #define HSTX_CKLP_EN			BIT(16)
692e54c14eSCK Hu 
702e54c14eSCK Hu #define DSI_PSCTRL		0x1c
712e54c14eSCK Hu #define DSI_PS_WC			0x3fff
722e54c14eSCK Hu #define DSI_PS_SEL			(3 << 16)
732e54c14eSCK Hu #define PACKED_PS_16BIT_RGB565		(0 << 16)
74*87a5a916SAngeloGioacchino Del Regno #define PACKED_PS_18BIT_RGB666		(1 << 16)
75*87a5a916SAngeloGioacchino Del Regno #define LOOSELY_PS_24BIT_RGB666		(2 << 16)
762e54c14eSCK Hu #define PACKED_PS_24BIT_RGB888		(3 << 16)
772e54c14eSCK Hu 
782e54c14eSCK Hu #define DSI_VSA_NL		0x20
792e54c14eSCK Hu #define DSI_VBP_NL		0x24
802e54c14eSCK Hu #define DSI_VFP_NL		0x28
812e54c14eSCK Hu #define DSI_VACT_NL		0x2C
827bf54afeSJitao Shi #define DSI_SIZE_CON		0x38
832e54c14eSCK Hu #define DSI_HSA_WC		0x50
842e54c14eSCK Hu #define DSI_HBP_WC		0x54
852e54c14eSCK Hu #define DSI_HFP_WC		0x58
862e54c14eSCK Hu 
8721898816Sshaoming chen #define DSI_CMDQ_SIZE		0x60
8821898816Sshaoming chen #define CMDQ_SIZE			0x3f
8921898816Sshaoming chen 
902e54c14eSCK Hu #define DSI_HSTX_CKL_WC		0x64
912e54c14eSCK Hu 
9221898816Sshaoming chen #define DSI_RX_DATA0		0x74
9321898816Sshaoming chen #define DSI_RX_DATA1		0x78
9421898816Sshaoming chen #define DSI_RX_DATA2		0x7c
9521898816Sshaoming chen #define DSI_RX_DATA3		0x80
9621898816Sshaoming chen 
97dd5080a5Sshaoming chen #define DSI_RACK		0x84
98dd5080a5Sshaoming chen #define RACK				BIT(0)
99dd5080a5Sshaoming chen 
1002e54c14eSCK Hu #define DSI_PHY_LCCON		0x104
1012e54c14eSCK Hu #define LC_HS_TX_EN			BIT(0)
1022e54c14eSCK Hu #define LC_ULPM_EN			BIT(1)
1032e54c14eSCK Hu #define LC_WAKEUP_EN			BIT(2)
1042e54c14eSCK Hu 
1052e54c14eSCK Hu #define DSI_PHY_LD0CON		0x108
1062e54c14eSCK Hu #define LD0_HS_TX_EN			BIT(0)
1072e54c14eSCK Hu #define LD0_ULPM_EN			BIT(1)
1082e54c14eSCK Hu #define LD0_WAKEUP_EN			BIT(2)
1092e54c14eSCK Hu 
1102e54c14eSCK Hu #define DSI_PHY_TIMECON0	0x110
1112e54c14eSCK Hu #define LPX				(0xff << 0)
112f6c87239SJitao Shi #define HS_PREP				(0xff << 8)
1132e54c14eSCK Hu #define HS_ZERO				(0xff << 16)
1142e54c14eSCK Hu #define HS_TRAIL			(0xff << 24)
1152e54c14eSCK Hu 
1162e54c14eSCK Hu #define DSI_PHY_TIMECON1	0x114
1172e54c14eSCK Hu #define TA_GO				(0xff << 0)
1182e54c14eSCK Hu #define TA_SURE				(0xff << 8)
1192e54c14eSCK Hu #define TA_GET				(0xff << 16)
1202e54c14eSCK Hu #define DA_HS_EXIT			(0xff << 24)
1212e54c14eSCK Hu 
1222e54c14eSCK Hu #define DSI_PHY_TIMECON2	0x118
1232e54c14eSCK Hu #define CONT_DET			(0xff << 0)
1242e54c14eSCK Hu #define CLK_ZERO			(0xff << 16)
1252e54c14eSCK Hu #define CLK_TRAIL			(0xff << 24)
1262e54c14eSCK Hu 
1272e54c14eSCK Hu #define DSI_PHY_TIMECON3	0x11c
128f6c87239SJitao Shi #define CLK_HS_PREP			(0xff << 0)
1292e54c14eSCK Hu #define CLK_HS_POST			(0xff << 8)
1302e54c14eSCK Hu #define CLK_HS_EXIT			(0xff << 16)
1312e54c14eSCK Hu 
1320707632bSyt.shen@mediatek.com #define DSI_VM_CMD_CON		0x130
1330707632bSyt.shen@mediatek.com #define VM_CMD_EN			BIT(0)
1340707632bSyt.shen@mediatek.com #define TS_VFP_EN			BIT(5)
1350707632bSyt.shen@mediatek.com 
1363c6bd94dSJitao Shi #define DSI_SHADOW_DEBUG	0x190U
1373c6bd94dSJitao Shi #define FORCE_COMMIT			BIT(0)
1383c6bd94dSJitao Shi #define BYPASS_SHADOW			BIT(1)
1393c6bd94dSJitao Shi 
14021898816Sshaoming chen #define CONFIG				(0xff << 0)
14121898816Sshaoming chen #define SHORT_PACKET			0
14221898816Sshaoming chen #define LONG_PACKET			2
14321898816Sshaoming chen #define BTA				BIT(2)
14421898816Sshaoming chen #define DATA_ID				(0xff << 8)
14521898816Sshaoming chen #define DATA_0				(0xff << 16)
14621898816Sshaoming chen #define DATA_1				(0xff << 24)
14721898816Sshaoming chen 
1482e54c14eSCK Hu #define NS_TO_CYCLE(n, c)    ((n) / (c) + (((n) % (c)) ? 1 : 0))
1492e54c14eSCK Hu 
15021898816Sshaoming chen #define MTK_DSI_HOST_IS_READ(type) \
15121898816Sshaoming chen 	((type == MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM) || \
15221898816Sshaoming chen 	(type == MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM) || \
15321898816Sshaoming chen 	(type == MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM) || \
15421898816Sshaoming chen 	(type == MIPI_DSI_DCS_READ))
15521898816Sshaoming chen 
1567a5bc4e2SJitao Shi struct mtk_phy_timing {
1577a5bc4e2SJitao Shi 	u32 lpx;
1587a5bc4e2SJitao Shi 	u32 da_hs_prepare;
1597a5bc4e2SJitao Shi 	u32 da_hs_zero;
1607a5bc4e2SJitao Shi 	u32 da_hs_trail;
1617a5bc4e2SJitao Shi 
1627a5bc4e2SJitao Shi 	u32 ta_go;
1637a5bc4e2SJitao Shi 	u32 ta_sure;
1647a5bc4e2SJitao Shi 	u32 ta_get;
1657a5bc4e2SJitao Shi 	u32 da_hs_exit;
1667a5bc4e2SJitao Shi 
1677a5bc4e2SJitao Shi 	u32 clk_hs_zero;
1687a5bc4e2SJitao Shi 	u32 clk_hs_trail;
1697a5bc4e2SJitao Shi 
1707a5bc4e2SJitao Shi 	u32 clk_hs_prepare;
1717a5bc4e2SJitao Shi 	u32 clk_hs_post;
1727a5bc4e2SJitao Shi 	u32 clk_hs_exit;
1737a5bc4e2SJitao Shi };
1747a5bc4e2SJitao Shi 
1752e54c14eSCK Hu struct phy;
1762e54c14eSCK Hu 
177bb6bc298SJitao Shi struct mtk_dsi_driver_data {
178bb6bc298SJitao Shi 	const u32 reg_cmdq_off;
1793c6bd94dSJitao Shi 	bool has_shadow_ctl;
1807bf54afeSJitao Shi 	bool has_size_ctl;
181bb6bc298SJitao Shi };
182bb6bc298SJitao Shi 
1832e54c14eSCK Hu struct mtk_dsi {
1842e54c14eSCK Hu 	struct device *dev;
1852e54c14eSCK Hu 	struct mipi_dsi_host host;
1862e54c14eSCK Hu 	struct drm_encoder encoder;
18771e780f1SEnric Balletbo i Serra 	struct drm_bridge bridge;
18868a9e11bSEnric Balletbo i Serra 	struct drm_bridge *next_bridge;
189a9d9fea7SEnric Balletbo i Serra 	struct drm_connector *connector;
1902e54c14eSCK Hu 	struct phy *phy;
1912e54c14eSCK Hu 
1922e54c14eSCK Hu 	void __iomem *regs;
1932e54c14eSCK Hu 
1942e54c14eSCK Hu 	struct clk *engine_clk;
1952e54c14eSCK Hu 	struct clk *digital_clk;
1962e54c14eSCK Hu 	struct clk *hs_clk;
1972e54c14eSCK Hu 
1982e54c14eSCK Hu 	u32 data_rate;
1992e54c14eSCK Hu 
2002e54c14eSCK Hu 	unsigned long mode_flags;
2012e54c14eSCK Hu 	enum mipi_dsi_pixel_format format;
2022e54c14eSCK Hu 	unsigned int lanes;
2032e54c14eSCK Hu 	struct videomode vm;
2047a5bc4e2SJitao Shi 	struct mtk_phy_timing phy_timing;
2052e54c14eSCK Hu 	int refcount;
2062e54c14eSCK Hu 	bool enabled;
20739e8d062SJitao Shi 	bool lanes_ready;
208dd5080a5Sshaoming chen 	u32 irq_data;
209dd5080a5Sshaoming chen 	wait_queue_head_t irq_wait_queue;
210bb6bc298SJitao Shi 	const struct mtk_dsi_driver_data *driver_data;
2112e54c14eSCK Hu };
2122e54c14eSCK Hu 
bridge_to_dsi(struct drm_bridge * b)21371e780f1SEnric Balletbo i Serra static inline struct mtk_dsi *bridge_to_dsi(struct drm_bridge *b)
2142e54c14eSCK Hu {
21571e780f1SEnric Balletbo i Serra 	return container_of(b, struct mtk_dsi, bridge);
2162e54c14eSCK Hu }
2172e54c14eSCK Hu 
host_to_dsi(struct mipi_dsi_host * h)2182e54c14eSCK Hu static inline struct mtk_dsi *host_to_dsi(struct mipi_dsi_host *h)
2192e54c14eSCK Hu {
2202e54c14eSCK Hu 	return container_of(h, struct mtk_dsi, host);
2212e54c14eSCK Hu }
2222e54c14eSCK Hu 
mtk_dsi_mask(struct mtk_dsi * dsi,u32 offset,u32 mask,u32 data)2232e54c14eSCK Hu static void mtk_dsi_mask(struct mtk_dsi *dsi, u32 offset, u32 mask, u32 data)
2242e54c14eSCK Hu {
2252e54c14eSCK Hu 	u32 temp = readl(dsi->regs + offset);
2262e54c14eSCK Hu 
2272e54c14eSCK Hu 	writel((temp & ~mask) | (data & mask), dsi->regs + offset);
2282e54c14eSCK Hu }
2292e54c14eSCK Hu 
mtk_dsi_phy_timconfig(struct mtk_dsi * dsi)23080a5cfd6Syt.shen@mediatek.com static void mtk_dsi_phy_timconfig(struct mtk_dsi *dsi)
2312e54c14eSCK Hu {
2322e54c14eSCK Hu 	u32 timcon0, timcon1, timcon2, timcon3;
233e18e0f6bSJitao Shi 	u32 data_rate_mhz = DIV_ROUND_UP(dsi->data_rate, 1000000);
2347a5bc4e2SJitao Shi 	struct mtk_phy_timing *timing = &dsi->phy_timing;
2352e54c14eSCK Hu 
236e18e0f6bSJitao Shi 	timing->lpx = (60 * data_rate_mhz / (8 * 1000)) + 1;
237e18e0f6bSJitao Shi 	timing->da_hs_prepare = (80 * data_rate_mhz + 4 * 1000) / 8000;
238e18e0f6bSJitao Shi 	timing->da_hs_zero = (170 * data_rate_mhz + 10 * 1000) / 8000 + 1 -
239e18e0f6bSJitao Shi 			     timing->da_hs_prepare;
240e18e0f6bSJitao Shi 	timing->da_hs_trail = timing->da_hs_prepare + 1;
2412e54c14eSCK Hu 
242e18e0f6bSJitao Shi 	timing->ta_go = 4 * timing->lpx - 2;
243e18e0f6bSJitao Shi 	timing->ta_sure = timing->lpx + 2;
244e18e0f6bSJitao Shi 	timing->ta_get = 4 * timing->lpx;
245e18e0f6bSJitao Shi 	timing->da_hs_exit = 2 * timing->lpx + 1;
2467a5bc4e2SJitao Shi 
247e18e0f6bSJitao Shi 	timing->clk_hs_prepare = 70 * data_rate_mhz / (8 * 1000);
248e18e0f6bSJitao Shi 	timing->clk_hs_post = timing->clk_hs_prepare + 8;
249e18e0f6bSJitao Shi 	timing->clk_hs_trail = timing->clk_hs_prepare;
250e18e0f6bSJitao Shi 	timing->clk_hs_zero = timing->clk_hs_trail * 4;
251e18e0f6bSJitao Shi 	timing->clk_hs_exit = 2 * timing->clk_hs_trail;
2527a5bc4e2SJitao Shi 
2537a5bc4e2SJitao Shi 	timcon0 = timing->lpx | timing->da_hs_prepare << 8 |
2547a5bc4e2SJitao Shi 		  timing->da_hs_zero << 16 | timing->da_hs_trail << 24;
2557a5bc4e2SJitao Shi 	timcon1 = timing->ta_go | timing->ta_sure << 8 |
2567a5bc4e2SJitao Shi 		  timing->ta_get << 16 | timing->da_hs_exit << 24;
2577a5bc4e2SJitao Shi 	timcon2 = 1 << 8 | timing->clk_hs_zero << 16 |
2587a5bc4e2SJitao Shi 		  timing->clk_hs_trail << 24;
2597a5bc4e2SJitao Shi 	timcon3 = timing->clk_hs_prepare | timing->clk_hs_post << 8 |
2607a5bc4e2SJitao Shi 		  timing->clk_hs_exit << 16;
2612e54c14eSCK Hu 
2622e54c14eSCK Hu 	writel(timcon0, dsi->regs + DSI_PHY_TIMECON0);
2632e54c14eSCK Hu 	writel(timcon1, dsi->regs + DSI_PHY_TIMECON1);
2642e54c14eSCK Hu 	writel(timcon2, dsi->regs + DSI_PHY_TIMECON2);
2652e54c14eSCK Hu 	writel(timcon3, dsi->regs + DSI_PHY_TIMECON3);
2662e54c14eSCK Hu }
2672e54c14eSCK Hu 
mtk_dsi_enable(struct mtk_dsi * dsi)2682e54c14eSCK Hu static void mtk_dsi_enable(struct mtk_dsi *dsi)
2692e54c14eSCK Hu {
2702e54c14eSCK Hu 	mtk_dsi_mask(dsi, DSI_CON_CTRL, DSI_EN, DSI_EN);
2712e54c14eSCK Hu }
2722e54c14eSCK Hu 
mtk_dsi_disable(struct mtk_dsi * dsi)2732e54c14eSCK Hu static void mtk_dsi_disable(struct mtk_dsi *dsi)
2742e54c14eSCK Hu {
2752e54c14eSCK Hu 	mtk_dsi_mask(dsi, DSI_CON_CTRL, DSI_EN, 0);
2762e54c14eSCK Hu }
2772e54c14eSCK Hu 
mtk_dsi_reset_engine(struct mtk_dsi * dsi)27880a5cfd6Syt.shen@mediatek.com static void mtk_dsi_reset_engine(struct mtk_dsi *dsi)
2792e54c14eSCK Hu {
2802e54c14eSCK Hu 	mtk_dsi_mask(dsi, DSI_CON_CTRL, DSI_RESET, DSI_RESET);
2812e54c14eSCK Hu 	mtk_dsi_mask(dsi, DSI_CON_CTRL, DSI_RESET, 0);
2822e54c14eSCK Hu }
2832e54c14eSCK Hu 
mtk_dsi_reset_dphy(struct mtk_dsi * dsi)28475374fc2SJitao Shi static void mtk_dsi_reset_dphy(struct mtk_dsi *dsi)
28575374fc2SJitao Shi {
28675374fc2SJitao Shi 	mtk_dsi_mask(dsi, DSI_CON_CTRL, DPHY_RESET, DPHY_RESET);
28775374fc2SJitao Shi 	mtk_dsi_mask(dsi, DSI_CON_CTRL, DPHY_RESET, 0);
28875374fc2SJitao Shi }
28975374fc2SJitao Shi 
mtk_dsi_clk_ulp_mode_enter(struct mtk_dsi * dsi)29080a5cfd6Syt.shen@mediatek.com static void mtk_dsi_clk_ulp_mode_enter(struct mtk_dsi *dsi)
2912e54c14eSCK Hu {
2922e54c14eSCK Hu 	mtk_dsi_mask(dsi, DSI_PHY_LCCON, LC_HS_TX_EN, 0);
2932e54c14eSCK Hu 	mtk_dsi_mask(dsi, DSI_PHY_LCCON, LC_ULPM_EN, 0);
2942e54c14eSCK Hu }
2952e54c14eSCK Hu 
mtk_dsi_clk_ulp_mode_leave(struct mtk_dsi * dsi)29680a5cfd6Syt.shen@mediatek.com static void mtk_dsi_clk_ulp_mode_leave(struct mtk_dsi *dsi)
2972e54c14eSCK Hu {
2982e54c14eSCK Hu 	mtk_dsi_mask(dsi, DSI_PHY_LCCON, LC_ULPM_EN, 0);
2992e54c14eSCK Hu 	mtk_dsi_mask(dsi, DSI_PHY_LCCON, LC_WAKEUP_EN, LC_WAKEUP_EN);
3002e54c14eSCK Hu 	mtk_dsi_mask(dsi, DSI_PHY_LCCON, LC_WAKEUP_EN, 0);
3012e54c14eSCK Hu }
3022e54c14eSCK Hu 
mtk_dsi_lane0_ulp_mode_enter(struct mtk_dsi * dsi)30380a5cfd6Syt.shen@mediatek.com static void mtk_dsi_lane0_ulp_mode_enter(struct mtk_dsi *dsi)
3042e54c14eSCK Hu {
3052e54c14eSCK Hu 	mtk_dsi_mask(dsi, DSI_PHY_LD0CON, LD0_HS_TX_EN, 0);
3062e54c14eSCK Hu 	mtk_dsi_mask(dsi, DSI_PHY_LD0CON, LD0_ULPM_EN, 0);
3072e54c14eSCK Hu }
3082e54c14eSCK Hu 
mtk_dsi_lane0_ulp_mode_leave(struct mtk_dsi * dsi)30980a5cfd6Syt.shen@mediatek.com static void mtk_dsi_lane0_ulp_mode_leave(struct mtk_dsi *dsi)
3102e54c14eSCK Hu {
3112e54c14eSCK Hu 	mtk_dsi_mask(dsi, DSI_PHY_LD0CON, LD0_ULPM_EN, 0);
3122e54c14eSCK Hu 	mtk_dsi_mask(dsi, DSI_PHY_LD0CON, LD0_WAKEUP_EN, LD0_WAKEUP_EN);
3132e54c14eSCK Hu 	mtk_dsi_mask(dsi, DSI_PHY_LD0CON, LD0_WAKEUP_EN, 0);
3142e54c14eSCK Hu }
3152e54c14eSCK Hu 
mtk_dsi_clk_hs_state(struct mtk_dsi * dsi)31680a5cfd6Syt.shen@mediatek.com static bool mtk_dsi_clk_hs_state(struct mtk_dsi *dsi)
3172e54c14eSCK Hu {
318e9052927SBernard Zhao 	return readl(dsi->regs + DSI_PHY_LCCON) & LC_HS_TX_EN;
3192e54c14eSCK Hu }
3202e54c14eSCK Hu 
mtk_dsi_clk_hs_mode(struct mtk_dsi * dsi,bool enter)32180a5cfd6Syt.shen@mediatek.com static void mtk_dsi_clk_hs_mode(struct mtk_dsi *dsi, bool enter)
3222e54c14eSCK Hu {
32380a5cfd6Syt.shen@mediatek.com 	if (enter && !mtk_dsi_clk_hs_state(dsi))
3242e54c14eSCK Hu 		mtk_dsi_mask(dsi, DSI_PHY_LCCON, LC_HS_TX_EN, LC_HS_TX_EN);
32580a5cfd6Syt.shen@mediatek.com 	else if (!enter && mtk_dsi_clk_hs_state(dsi))
3262e54c14eSCK Hu 		mtk_dsi_mask(dsi, DSI_PHY_LCCON, LC_HS_TX_EN, 0);
3272e54c14eSCK Hu }
3282e54c14eSCK Hu 
mtk_dsi_set_mode(struct mtk_dsi * dsi)32980a5cfd6Syt.shen@mediatek.com static void mtk_dsi_set_mode(struct mtk_dsi *dsi)
3302e54c14eSCK Hu {
3312e54c14eSCK Hu 	u32 vid_mode = CMD_MODE;
3322e54c14eSCK Hu 
3332e54c14eSCK Hu 	if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO) {
3340707632bSyt.shen@mediatek.com 		if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_BURST)
3352e54c14eSCK Hu 			vid_mode = BURST_MODE;
3360707632bSyt.shen@mediatek.com 		else if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
3370707632bSyt.shen@mediatek.com 			vid_mode = SYNC_PULSE_MODE;
3380707632bSyt.shen@mediatek.com 		else
3390707632bSyt.shen@mediatek.com 			vid_mode = SYNC_EVENT_MODE;
3402e54c14eSCK Hu 	}
3412e54c14eSCK Hu 
3422e54c14eSCK Hu 	writel(vid_mode, dsi->regs + DSI_MODE_CTRL);
3432e54c14eSCK Hu }
3442e54c14eSCK Hu 
mtk_dsi_set_vm_cmd(struct mtk_dsi * dsi)3450707632bSyt.shen@mediatek.com static void mtk_dsi_set_vm_cmd(struct mtk_dsi *dsi)
3460707632bSyt.shen@mediatek.com {
3470707632bSyt.shen@mediatek.com 	mtk_dsi_mask(dsi, DSI_VM_CMD_CON, VM_CMD_EN, VM_CMD_EN);
3480707632bSyt.shen@mediatek.com 	mtk_dsi_mask(dsi, DSI_VM_CMD_CON, TS_VFP_EN, TS_VFP_EN);
3490707632bSyt.shen@mediatek.com }
3500707632bSyt.shen@mediatek.com 
mtk_dsi_ps_control_vact(struct mtk_dsi * dsi)35180a5cfd6Syt.shen@mediatek.com static void mtk_dsi_ps_control_vact(struct mtk_dsi *dsi)
3522e54c14eSCK Hu {
3532e54c14eSCK Hu 	struct videomode *vm = &dsi->vm;
3542e54c14eSCK Hu 	u32 dsi_buf_bpp, ps_wc;
3552e54c14eSCK Hu 	u32 ps_bpp_mode;
3562e54c14eSCK Hu 
3572e54c14eSCK Hu 	if (dsi->format == MIPI_DSI_FMT_RGB565)
3582e54c14eSCK Hu 		dsi_buf_bpp = 2;
3592e54c14eSCK Hu 	else
3602e54c14eSCK Hu 		dsi_buf_bpp = 3;
3612e54c14eSCK Hu 
3622e54c14eSCK Hu 	ps_wc = vm->hactive * dsi_buf_bpp;
3632e54c14eSCK Hu 	ps_bpp_mode = ps_wc;
3642e54c14eSCK Hu 
3652e54c14eSCK Hu 	switch (dsi->format) {
3662e54c14eSCK Hu 	case MIPI_DSI_FMT_RGB888:
3672e54c14eSCK Hu 		ps_bpp_mode |= PACKED_PS_24BIT_RGB888;
3682e54c14eSCK Hu 		break;
3692e54c14eSCK Hu 	case MIPI_DSI_FMT_RGB666:
370*87a5a916SAngeloGioacchino Del Regno 		ps_bpp_mode |= LOOSELY_PS_24BIT_RGB666;
3712e54c14eSCK Hu 		break;
3722e54c14eSCK Hu 	case MIPI_DSI_FMT_RGB666_PACKED:
373*87a5a916SAngeloGioacchino Del Regno 		ps_bpp_mode |= PACKED_PS_18BIT_RGB666;
3742e54c14eSCK Hu 		break;
3752e54c14eSCK Hu 	case MIPI_DSI_FMT_RGB565:
3762e54c14eSCK Hu 		ps_bpp_mode |= PACKED_PS_16BIT_RGB565;
3772e54c14eSCK Hu 		break;
3782e54c14eSCK Hu 	}
3792e54c14eSCK Hu 
3802e54c14eSCK Hu 	writel(vm->vactive, dsi->regs + DSI_VACT_NL);
3812e54c14eSCK Hu 	writel(ps_bpp_mode, dsi->regs + DSI_PSCTRL);
3822e54c14eSCK Hu 	writel(ps_wc, dsi->regs + DSI_HSTX_CKL_WC);
3832e54c14eSCK Hu }
3842e54c14eSCK Hu 
mtk_dsi_rxtx_control(struct mtk_dsi * dsi)38580a5cfd6Syt.shen@mediatek.com static void mtk_dsi_rxtx_control(struct mtk_dsi *dsi)
3862e54c14eSCK Hu {
3872e54c14eSCK Hu 	u32 tmp_reg;
3882e54c14eSCK Hu 
3892e54c14eSCK Hu 	switch (dsi->lanes) {
3902e54c14eSCK Hu 	case 1:
3912e54c14eSCK Hu 		tmp_reg = 1 << 2;
3922e54c14eSCK Hu 		break;
3932e54c14eSCK Hu 	case 2:
3942e54c14eSCK Hu 		tmp_reg = 3 << 2;
3952e54c14eSCK Hu 		break;
3962e54c14eSCK Hu 	case 3:
3972e54c14eSCK Hu 		tmp_reg = 7 << 2;
3982e54c14eSCK Hu 		break;
3992e54c14eSCK Hu 	case 4:
4002e54c14eSCK Hu 		tmp_reg = 0xf << 2;
4012e54c14eSCK Hu 		break;
4022e54c14eSCK Hu 	default:
4032e54c14eSCK Hu 		tmp_reg = 0xf << 2;
4042e54c14eSCK Hu 		break;
4052e54c14eSCK Hu 	}
4062e54c14eSCK Hu 
407c87d1c4bSJitao Shi 	if (dsi->mode_flags & MIPI_DSI_CLOCK_NON_CONTINUOUS)
408c87d1c4bSJitao Shi 		tmp_reg |= HSTX_CKLP_EN;
409c87d1c4bSJitao Shi 
4108f3ea1ecSAngeloGioacchino Del Regno 	if (dsi->mode_flags & MIPI_DSI_MODE_NO_EOT_PACKET)
411c87d1c4bSJitao Shi 		tmp_reg |= DIS_EOT;
4122d52bfbaSyt.shen@mediatek.com 
4132e54c14eSCK Hu 	writel(tmp_reg, dsi->regs + DSI_TXRX_CTRL);
4142e54c14eSCK Hu }
4152e54c14eSCK Hu 
mtk_dsi_ps_control(struct mtk_dsi * dsi)41680a5cfd6Syt.shen@mediatek.com static void mtk_dsi_ps_control(struct mtk_dsi *dsi)
4172e54c14eSCK Hu {
41880a5cfd6Syt.shen@mediatek.com 	u32 dsi_tmp_buf_bpp;
4192e54c14eSCK Hu 	u32 tmp_reg;
4202e54c14eSCK Hu 
4212e54c14eSCK Hu 	switch (dsi->format) {
4222e54c14eSCK Hu 	case MIPI_DSI_FMT_RGB888:
4232e54c14eSCK Hu 		tmp_reg = PACKED_PS_24BIT_RGB888;
4242e54c14eSCK Hu 		dsi_tmp_buf_bpp = 3;
4252e54c14eSCK Hu 		break;
4262e54c14eSCK Hu 	case MIPI_DSI_FMT_RGB666:
427*87a5a916SAngeloGioacchino Del Regno 		tmp_reg = LOOSELY_PS_24BIT_RGB666;
4282e54c14eSCK Hu 		dsi_tmp_buf_bpp = 3;
4292e54c14eSCK Hu 		break;
4302e54c14eSCK Hu 	case MIPI_DSI_FMT_RGB666_PACKED:
4312e54c14eSCK Hu 		tmp_reg = PACKED_PS_18BIT_RGB666;
4322e54c14eSCK Hu 		dsi_tmp_buf_bpp = 3;
4332e54c14eSCK Hu 		break;
4342e54c14eSCK Hu 	case MIPI_DSI_FMT_RGB565:
4352e54c14eSCK Hu 		tmp_reg = PACKED_PS_16BIT_RGB565;
4362e54c14eSCK Hu 		dsi_tmp_buf_bpp = 2;
4372e54c14eSCK Hu 		break;
4382e54c14eSCK Hu 	default:
4392e54c14eSCK Hu 		tmp_reg = PACKED_PS_24BIT_RGB888;
4402e54c14eSCK Hu 		dsi_tmp_buf_bpp = 3;
4412e54c14eSCK Hu 		break;
4422e54c14eSCK Hu 	}
4432e54c14eSCK Hu 
4442e54c14eSCK Hu 	tmp_reg += dsi->vm.hactive * dsi_tmp_buf_bpp & DSI_PS_WC;
4452e54c14eSCK Hu 	writel(tmp_reg, dsi->regs + DSI_PSCTRL);
4462e54c14eSCK Hu }
4472e54c14eSCK Hu 
mtk_dsi_config_vdo_timing(struct mtk_dsi * dsi)44880a5cfd6Syt.shen@mediatek.com static void mtk_dsi_config_vdo_timing(struct mtk_dsi *dsi)
4492e54c14eSCK Hu {
45080a5cfd6Syt.shen@mediatek.com 	u32 horizontal_sync_active_byte;
45180a5cfd6Syt.shen@mediatek.com 	u32 horizontal_backporch_byte;
45280a5cfd6Syt.shen@mediatek.com 	u32 horizontal_frontporch_byte;
453487778f8SCK Hu 	u32 horizontal_front_back_byte;
454487778f8SCK Hu 	u32 data_phy_cycles_byte;
4557a5bc4e2SJitao Shi 	u32 dsi_tmp_buf_bpp, data_phy_cycles;
456487778f8SCK Hu 	u32 delta;
4577a5bc4e2SJitao Shi 	struct mtk_phy_timing *timing = &dsi->phy_timing;
4582e54c14eSCK Hu 
4592e54c14eSCK Hu 	struct videomode *vm = &dsi->vm;
4602e54c14eSCK Hu 
4612e54c14eSCK Hu 	if (dsi->format == MIPI_DSI_FMT_RGB565)
4622e54c14eSCK Hu 		dsi_tmp_buf_bpp = 2;
4632e54c14eSCK Hu 	else
4642e54c14eSCK Hu 		dsi_tmp_buf_bpp = 3;
4652e54c14eSCK Hu 
4662e54c14eSCK Hu 	writel(vm->vsync_len, dsi->regs + DSI_VSA_NL);
4672e54c14eSCK Hu 	writel(vm->vback_porch, dsi->regs + DSI_VBP_NL);
4682e54c14eSCK Hu 	writel(vm->vfront_porch, dsi->regs + DSI_VFP_NL);
4692e54c14eSCK Hu 	writel(vm->vactive, dsi->regs + DSI_VACT_NL);
4702e54c14eSCK Hu 
4717bf54afeSJitao Shi 	if (dsi->driver_data->has_size_ctl)
4727bf54afeSJitao Shi 		writel(vm->vactive << 16 | vm->hactive,
4737bf54afeSJitao Shi 		       dsi->regs + DSI_SIZE_CON);
4747bf54afeSJitao Shi 
4752e54c14eSCK Hu 	horizontal_sync_active_byte = (vm->hsync_len * dsi_tmp_buf_bpp - 10);
4762e54c14eSCK Hu 
4772e54c14eSCK Hu 	if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
478487778f8SCK Hu 		horizontal_backporch_byte = vm->hback_porch * dsi_tmp_buf_bpp - 10;
4792e54c14eSCK Hu 	else
48035bf948fSJitao Shi 		horizontal_backporch_byte = (vm->hback_porch + vm->hsync_len) *
481487778f8SCK Hu 					    dsi_tmp_buf_bpp - 10;
4822e54c14eSCK Hu 
4837a5bc4e2SJitao Shi 	data_phy_cycles = timing->lpx + timing->da_hs_prepare +
484487778f8SCK Hu 			  timing->da_hs_zero + timing->da_hs_exit + 3;
4857a5bc4e2SJitao Shi 
486487778f8SCK Hu 	delta = dsi->mode_flags & MIPI_DSI_MODE_VIDEO_BURST ? 18 : 12;
4878f3ea1ecSAngeloGioacchino Del Regno 	delta += dsi->mode_flags & MIPI_DSI_MODE_NO_EOT_PACKET ? 0 : 2;
488e18e0f6bSJitao Shi 
489487778f8SCK Hu 	horizontal_frontporch_byte = vm->hfront_porch * dsi_tmp_buf_bpp;
490487778f8SCK Hu 	horizontal_front_back_byte = horizontal_frontporch_byte + horizontal_backporch_byte;
491487778f8SCK Hu 	data_phy_cycles_byte = data_phy_cycles * dsi->lanes + delta;
492487778f8SCK Hu 
493487778f8SCK Hu 	if (horizontal_front_back_byte > data_phy_cycles_byte) {
494487778f8SCK Hu 		horizontal_frontporch_byte -= data_phy_cycles_byte *
495487778f8SCK Hu 					      horizontal_frontporch_byte /
496487778f8SCK Hu 					      horizontal_front_back_byte;
497487778f8SCK Hu 
498487778f8SCK Hu 		horizontal_backporch_byte -= data_phy_cycles_byte *
499487778f8SCK Hu 					     horizontal_backporch_byte /
500487778f8SCK Hu 					     horizontal_front_back_byte;
5017a5bc4e2SJitao Shi 	} else {
502487778f8SCK Hu 		DRM_WARN("HFP + HBP less than d-phy, FPS will under 60Hz\n");
5037a5bc4e2SJitao Shi 	}
5042e54c14eSCK Hu 
5057d8d0b4dSRex-BC Chen 	if ((dsi->mode_flags & MIPI_DSI_HS_PKT_END_ALIGNED) &&
5067d8d0b4dSRex-BC Chen 	    (dsi->lanes == 4)) {
5077d8d0b4dSRex-BC Chen 		horizontal_sync_active_byte =
5087d8d0b4dSRex-BC Chen 			roundup(horizontal_sync_active_byte, dsi->lanes) - 2;
5097d8d0b4dSRex-BC Chen 		horizontal_frontporch_byte =
5107d8d0b4dSRex-BC Chen 			roundup(horizontal_frontporch_byte, dsi->lanes) - 2;
5117d8d0b4dSRex-BC Chen 		horizontal_backporch_byte =
5127d8d0b4dSRex-BC Chen 			roundup(horizontal_backporch_byte, dsi->lanes) - 2;
5137d8d0b4dSRex-BC Chen 		horizontal_backporch_byte -=
5147d8d0b4dSRex-BC Chen 			(vm->hactive * dsi_tmp_buf_bpp + 2) % dsi->lanes;
5157d8d0b4dSRex-BC Chen 	}
5167d8d0b4dSRex-BC Chen 
5172e54c14eSCK Hu 	writel(horizontal_sync_active_byte, dsi->regs + DSI_HSA_WC);
5182e54c14eSCK Hu 	writel(horizontal_backporch_byte, dsi->regs + DSI_HBP_WC);
5192e54c14eSCK Hu 	writel(horizontal_frontporch_byte, dsi->regs + DSI_HFP_WC);
5202e54c14eSCK Hu 
52180a5cfd6Syt.shen@mediatek.com 	mtk_dsi_ps_control(dsi);
5222e54c14eSCK Hu }
5232e54c14eSCK Hu 
mtk_dsi_start(struct mtk_dsi * dsi)5242e54c14eSCK Hu static void mtk_dsi_start(struct mtk_dsi *dsi)
5252e54c14eSCK Hu {
5262e54c14eSCK Hu 	writel(0, dsi->regs + DSI_START);
5272e54c14eSCK Hu 	writel(1, dsi->regs + DSI_START);
5282e54c14eSCK Hu }
5292e54c14eSCK Hu 
mtk_dsi_stop(struct mtk_dsi * dsi)5300707632bSyt.shen@mediatek.com static void mtk_dsi_stop(struct mtk_dsi *dsi)
5310707632bSyt.shen@mediatek.com {
5320707632bSyt.shen@mediatek.com 	writel(0, dsi->regs + DSI_START);
5330707632bSyt.shen@mediatek.com }
5340707632bSyt.shen@mediatek.com 
mtk_dsi_set_cmd_mode(struct mtk_dsi * dsi)5350707632bSyt.shen@mediatek.com static void mtk_dsi_set_cmd_mode(struct mtk_dsi *dsi)
5360707632bSyt.shen@mediatek.com {
5370707632bSyt.shen@mediatek.com 	writel(CMD_MODE, dsi->regs + DSI_MODE_CTRL);
5380707632bSyt.shen@mediatek.com }
5390707632bSyt.shen@mediatek.com 
mtk_dsi_set_interrupt_enable(struct mtk_dsi * dsi)540dd5080a5Sshaoming chen static void mtk_dsi_set_interrupt_enable(struct mtk_dsi *dsi)
541dd5080a5Sshaoming chen {
542dd5080a5Sshaoming chen 	u32 inten = LPRX_RD_RDY_INT_FLAG | CMD_DONE_INT_FLAG | VM_DONE_INT_FLAG;
543dd5080a5Sshaoming chen 
544dd5080a5Sshaoming chen 	writel(inten, dsi->regs + DSI_INTEN);
545dd5080a5Sshaoming chen }
546dd5080a5Sshaoming chen 
mtk_dsi_irq_data_set(struct mtk_dsi * dsi,u32 irq_bit)547dd5080a5Sshaoming chen static void mtk_dsi_irq_data_set(struct mtk_dsi *dsi, u32 irq_bit)
548dd5080a5Sshaoming chen {
549dd5080a5Sshaoming chen 	dsi->irq_data |= irq_bit;
550dd5080a5Sshaoming chen }
551dd5080a5Sshaoming chen 
mtk_dsi_irq_data_clear(struct mtk_dsi * dsi,u32 irq_bit)55221898816Sshaoming chen static void mtk_dsi_irq_data_clear(struct mtk_dsi *dsi, u32 irq_bit)
553dd5080a5Sshaoming chen {
554dd5080a5Sshaoming chen 	dsi->irq_data &= ~irq_bit;
555dd5080a5Sshaoming chen }
556dd5080a5Sshaoming chen 
mtk_dsi_wait_for_irq_done(struct mtk_dsi * dsi,u32 irq_flag,unsigned int timeout)55721898816Sshaoming chen static s32 mtk_dsi_wait_for_irq_done(struct mtk_dsi *dsi, u32 irq_flag,
558dd5080a5Sshaoming chen 				     unsigned int timeout)
559dd5080a5Sshaoming chen {
560dd5080a5Sshaoming chen 	s32 ret = 0;
561dd5080a5Sshaoming chen 	unsigned long jiffies = msecs_to_jiffies(timeout);
562dd5080a5Sshaoming chen 
563dd5080a5Sshaoming chen 	ret = wait_event_interruptible_timeout(dsi->irq_wait_queue,
564dd5080a5Sshaoming chen 					       dsi->irq_data & irq_flag,
565dd5080a5Sshaoming chen 					       jiffies);
566dd5080a5Sshaoming chen 	if (ret == 0) {
567dd5080a5Sshaoming chen 		DRM_WARN("Wait DSI IRQ(0x%08x) Timeout\n", irq_flag);
568dd5080a5Sshaoming chen 
569dd5080a5Sshaoming chen 		mtk_dsi_enable(dsi);
570dd5080a5Sshaoming chen 		mtk_dsi_reset_engine(dsi);
571dd5080a5Sshaoming chen 	}
572dd5080a5Sshaoming chen 
573dd5080a5Sshaoming chen 	return ret;
574dd5080a5Sshaoming chen }
575dd5080a5Sshaoming chen 
mtk_dsi_irq(int irq,void * dev_id)576dd5080a5Sshaoming chen static irqreturn_t mtk_dsi_irq(int irq, void *dev_id)
577dd5080a5Sshaoming chen {
578dd5080a5Sshaoming chen 	struct mtk_dsi *dsi = dev_id;
579dd5080a5Sshaoming chen 	u32 status, tmp;
580dd5080a5Sshaoming chen 	u32 flag = LPRX_RD_RDY_INT_FLAG | CMD_DONE_INT_FLAG | VM_DONE_INT_FLAG;
581dd5080a5Sshaoming chen 
582dd5080a5Sshaoming chen 	status = readl(dsi->regs + DSI_INTSTA) & flag;
583dd5080a5Sshaoming chen 
584dd5080a5Sshaoming chen 	if (status) {
585dd5080a5Sshaoming chen 		do {
586dd5080a5Sshaoming chen 			mtk_dsi_mask(dsi, DSI_RACK, RACK, RACK);
587dd5080a5Sshaoming chen 			tmp = readl(dsi->regs + DSI_INTSTA);
588dd5080a5Sshaoming chen 		} while (tmp & DSI_BUSY);
589dd5080a5Sshaoming chen 
590dd5080a5Sshaoming chen 		mtk_dsi_mask(dsi, DSI_INTSTA, status, 0);
591dd5080a5Sshaoming chen 		mtk_dsi_irq_data_set(dsi, status);
592dd5080a5Sshaoming chen 		wake_up_interruptible(&dsi->irq_wait_queue);
593dd5080a5Sshaoming chen 	}
594dd5080a5Sshaoming chen 
595dd5080a5Sshaoming chen 	return IRQ_HANDLED;
596dd5080a5Sshaoming chen }
597dd5080a5Sshaoming chen 
mtk_dsi_switch_to_cmd_mode(struct mtk_dsi * dsi,u8 irq_flag,u32 t)5980707632bSyt.shen@mediatek.com static s32 mtk_dsi_switch_to_cmd_mode(struct mtk_dsi *dsi, u8 irq_flag, u32 t)
5990707632bSyt.shen@mediatek.com {
6000707632bSyt.shen@mediatek.com 	mtk_dsi_irq_data_clear(dsi, irq_flag);
6010707632bSyt.shen@mediatek.com 	mtk_dsi_set_cmd_mode(dsi);
6020707632bSyt.shen@mediatek.com 
6030707632bSyt.shen@mediatek.com 	if (!mtk_dsi_wait_for_irq_done(dsi, irq_flag, t)) {
6040707632bSyt.shen@mediatek.com 		DRM_ERROR("failed to switch cmd mode\n");
6050707632bSyt.shen@mediatek.com 		return -ETIME;
6060707632bSyt.shen@mediatek.com 	} else {
6070707632bSyt.shen@mediatek.com 		return 0;
6080707632bSyt.shen@mediatek.com 	}
6090707632bSyt.shen@mediatek.com }
6100707632bSyt.shen@mediatek.com 
mtk_dsi_poweron(struct mtk_dsi * dsi)6110707632bSyt.shen@mediatek.com static int mtk_dsi_poweron(struct mtk_dsi *dsi)
6120707632bSyt.shen@mediatek.com {
613b3218e74SJitao Shi 	struct device *dev = dsi->host.dev;
6140707632bSyt.shen@mediatek.com 	int ret;
6157a5bc4e2SJitao Shi 	u32 bit_per_pixel;
6160707632bSyt.shen@mediatek.com 
6170707632bSyt.shen@mediatek.com 	if (++dsi->refcount != 1)
6180707632bSyt.shen@mediatek.com 		return 0;
6190707632bSyt.shen@mediatek.com 
6200707632bSyt.shen@mediatek.com 	switch (dsi->format) {
6210707632bSyt.shen@mediatek.com 	case MIPI_DSI_FMT_RGB565:
6220707632bSyt.shen@mediatek.com 		bit_per_pixel = 16;
6230707632bSyt.shen@mediatek.com 		break;
6240707632bSyt.shen@mediatek.com 	case MIPI_DSI_FMT_RGB666_PACKED:
6250707632bSyt.shen@mediatek.com 		bit_per_pixel = 18;
6260707632bSyt.shen@mediatek.com 		break;
6270707632bSyt.shen@mediatek.com 	case MIPI_DSI_FMT_RGB666:
6280707632bSyt.shen@mediatek.com 	case MIPI_DSI_FMT_RGB888:
6290707632bSyt.shen@mediatek.com 	default:
6300707632bSyt.shen@mediatek.com 		bit_per_pixel = 24;
6310707632bSyt.shen@mediatek.com 		break;
6320707632bSyt.shen@mediatek.com 	}
6330707632bSyt.shen@mediatek.com 
6347a5bc4e2SJitao Shi 	dsi->data_rate = DIV_ROUND_UP_ULL(dsi->vm.pixelclock * bit_per_pixel,
6357a5bc4e2SJitao Shi 					  dsi->lanes);
6360707632bSyt.shen@mediatek.com 
6370707632bSyt.shen@mediatek.com 	ret = clk_set_rate(dsi->hs_clk, dsi->data_rate);
6380707632bSyt.shen@mediatek.com 	if (ret < 0) {
6390707632bSyt.shen@mediatek.com 		dev_err(dev, "Failed to set data rate: %d\n", ret);
6400707632bSyt.shen@mediatek.com 		goto err_refcount;
6410707632bSyt.shen@mediatek.com 	}
6420707632bSyt.shen@mediatek.com 
6430707632bSyt.shen@mediatek.com 	phy_power_on(dsi->phy);
6440707632bSyt.shen@mediatek.com 
6450707632bSyt.shen@mediatek.com 	ret = clk_prepare_enable(dsi->engine_clk);
6460707632bSyt.shen@mediatek.com 	if (ret < 0) {
6470707632bSyt.shen@mediatek.com 		dev_err(dev, "Failed to enable engine clock: %d\n", ret);
6480707632bSyt.shen@mediatek.com 		goto err_phy_power_off;
6490707632bSyt.shen@mediatek.com 	}
6500707632bSyt.shen@mediatek.com 
6510707632bSyt.shen@mediatek.com 	ret = clk_prepare_enable(dsi->digital_clk);
6520707632bSyt.shen@mediatek.com 	if (ret < 0) {
6530707632bSyt.shen@mediatek.com 		dev_err(dev, "Failed to enable digital clock: %d\n", ret);
6540707632bSyt.shen@mediatek.com 		goto err_disable_engine_clk;
6550707632bSyt.shen@mediatek.com 	}
6560707632bSyt.shen@mediatek.com 
6570707632bSyt.shen@mediatek.com 	mtk_dsi_enable(dsi);
6583c6bd94dSJitao Shi 
6593c6bd94dSJitao Shi 	if (dsi->driver_data->has_shadow_ctl)
6603c6bd94dSJitao Shi 		writel(FORCE_COMMIT | BYPASS_SHADOW,
6613c6bd94dSJitao Shi 		       dsi->regs + DSI_SHADOW_DEBUG);
6623c6bd94dSJitao Shi 
6630707632bSyt.shen@mediatek.com 	mtk_dsi_reset_engine(dsi);
6640707632bSyt.shen@mediatek.com 	mtk_dsi_phy_timconfig(dsi);
6650707632bSyt.shen@mediatek.com 
6660707632bSyt.shen@mediatek.com 	mtk_dsi_ps_control_vact(dsi);
6670707632bSyt.shen@mediatek.com 	mtk_dsi_set_vm_cmd(dsi);
6680707632bSyt.shen@mediatek.com 	mtk_dsi_config_vdo_timing(dsi);
6690707632bSyt.shen@mediatek.com 	mtk_dsi_set_interrupt_enable(dsi);
6700707632bSyt.shen@mediatek.com 
6710707632bSyt.shen@mediatek.com 	return 0;
6720707632bSyt.shen@mediatek.com err_disable_engine_clk:
6730707632bSyt.shen@mediatek.com 	clk_disable_unprepare(dsi->engine_clk);
6740707632bSyt.shen@mediatek.com err_phy_power_off:
6750707632bSyt.shen@mediatek.com 	phy_power_off(dsi->phy);
6760707632bSyt.shen@mediatek.com err_refcount:
6770707632bSyt.shen@mediatek.com 	dsi->refcount--;
6780707632bSyt.shen@mediatek.com 	return ret;
6790707632bSyt.shen@mediatek.com }
6800707632bSyt.shen@mediatek.com 
mtk_dsi_poweroff(struct mtk_dsi * dsi)6812e54c14eSCK Hu static void mtk_dsi_poweroff(struct mtk_dsi *dsi)
6822e54c14eSCK Hu {
6832e54c14eSCK Hu 	if (WARN_ON(dsi->refcount == 0))
6842e54c14eSCK Hu 		return;
6852e54c14eSCK Hu 
6862e54c14eSCK Hu 	if (--dsi->refcount != 0)
6872e54c14eSCK Hu 		return;
6882e54c14eSCK Hu 
68990144dd8SNícolas F. R. A. Prado 	/*
69090144dd8SNícolas F. R. A. Prado 	 * mtk_dsi_stop() and mtk_dsi_start() is asymmetric, since
69190144dd8SNícolas F. R. A. Prado 	 * mtk_dsi_stop() should be called after mtk_drm_crtc_atomic_disable(),
69290144dd8SNícolas F. R. A. Prado 	 * which needs irq for vblank, and mtk_dsi_stop() will disable irq.
69390144dd8SNícolas F. R. A. Prado 	 * mtk_dsi_start() needs to be called in mtk_output_dsi_enable(),
69490144dd8SNícolas F. R. A. Prado 	 * after dsi is fully set.
69590144dd8SNícolas F. R. A. Prado 	 */
69690144dd8SNícolas F. R. A. Prado 	mtk_dsi_stop(dsi);
69790144dd8SNícolas F. R. A. Prado 
69890144dd8SNícolas F. R. A. Prado 	mtk_dsi_switch_to_cmd_mode(dsi, VM_DONE_INT_FLAG, 500);
6990707632bSyt.shen@mediatek.com 	mtk_dsi_reset_engine(dsi);
70080a5cfd6Syt.shen@mediatek.com 	mtk_dsi_lane0_ulp_mode_enter(dsi);
70180a5cfd6Syt.shen@mediatek.com 	mtk_dsi_clk_ulp_mode_enter(dsi);
702fa5d0a02SXinlei Lee 	/* set the lane number as 0 to pull down mipi */
703fa5d0a02SXinlei Lee 	writel(0, dsi->regs + DSI_TXRX_CTRL);
7042e54c14eSCK Hu 
7052e54c14eSCK Hu 	mtk_dsi_disable(dsi);
7062e54c14eSCK Hu 
7072e54c14eSCK Hu 	clk_disable_unprepare(dsi->engine_clk);
7082e54c14eSCK Hu 	clk_disable_unprepare(dsi->digital_clk);
7092e54c14eSCK Hu 
7102e54c14eSCK Hu 	phy_power_off(dsi->phy);
71139e8d062SJitao Shi 
71239e8d062SJitao Shi 	dsi->lanes_ready = false;
71339e8d062SJitao Shi }
71439e8d062SJitao Shi 
mtk_dsi_lane_ready(struct mtk_dsi * dsi)71539e8d062SJitao Shi static void mtk_dsi_lane_ready(struct mtk_dsi *dsi)
71639e8d062SJitao Shi {
71739e8d062SJitao Shi 	if (!dsi->lanes_ready) {
71839e8d062SJitao Shi 		dsi->lanes_ready = true;
71939e8d062SJitao Shi 		mtk_dsi_rxtx_control(dsi);
72039e8d062SJitao Shi 		usleep_range(30, 100);
72139e8d062SJitao Shi 		mtk_dsi_reset_dphy(dsi);
72239e8d062SJitao Shi 		mtk_dsi_clk_ulp_mode_leave(dsi);
72339e8d062SJitao Shi 		mtk_dsi_lane0_ulp_mode_leave(dsi);
72439e8d062SJitao Shi 		mtk_dsi_clk_hs_mode(dsi, 0);
72591aeaed2SXinlei Lee 		usleep_range(1000, 3000);
72639e8d062SJitao Shi 		/* The reaction time after pulling up the mipi signal for dsi_rx */
72739e8d062SJitao Shi 	}
7282e54c14eSCK Hu }
7292e54c14eSCK Hu 
mtk_output_dsi_enable(struct mtk_dsi * dsi)7302e54c14eSCK Hu static void mtk_output_dsi_enable(struct mtk_dsi *dsi)
7312e54c14eSCK Hu {
7322e54c14eSCK Hu 	if (dsi->enabled)
7332e54c14eSCK Hu 		return;
7342e54c14eSCK Hu 
73539e8d062SJitao Shi 	mtk_dsi_lane_ready(dsi);
73680a5cfd6Syt.shen@mediatek.com 	mtk_dsi_set_mode(dsi);
73780a5cfd6Syt.shen@mediatek.com 	mtk_dsi_clk_hs_mode(dsi, 1);
7382e54c14eSCK Hu 
7392e54c14eSCK Hu 	mtk_dsi_start(dsi);
7402e54c14eSCK Hu 
7412e54c14eSCK Hu 	dsi->enabled = true;
7422e54c14eSCK Hu }
7432e54c14eSCK Hu 
mtk_output_dsi_disable(struct mtk_dsi * dsi)7442e54c14eSCK Hu static void mtk_output_dsi_disable(struct mtk_dsi *dsi)
7452e54c14eSCK Hu {
7462e54c14eSCK Hu 	if (!dsi->enabled)
7472e54c14eSCK Hu 		return;
7482e54c14eSCK Hu 
7492e54c14eSCK Hu 	dsi->enabled = false;
7502e54c14eSCK Hu }
7512e54c14eSCK Hu 
mtk_dsi_bridge_attach(struct drm_bridge * bridge,enum drm_bridge_attach_flags flags)75271e780f1SEnric Balletbo i Serra static int mtk_dsi_bridge_attach(struct drm_bridge *bridge,
75371e780f1SEnric Balletbo i Serra 				 enum drm_bridge_attach_flags flags)
7542e54c14eSCK Hu {
75571e780f1SEnric Balletbo i Serra 	struct mtk_dsi *dsi = bridge_to_dsi(bridge);
75671e780f1SEnric Balletbo i Serra 
7572dd8075dSEnric Balletbo i Serra 	/* Attach the panel or bridge to the dsi bridge */
7582dd8075dSEnric Balletbo i Serra 	return drm_bridge_attach(bridge->encoder, dsi->next_bridge,
7592dd8075dSEnric Balletbo i Serra 				 &dsi->bridge, flags);
76071e780f1SEnric Balletbo i Serra }
76171e780f1SEnric Balletbo i Serra 
mtk_dsi_bridge_mode_set(struct drm_bridge * bridge,const struct drm_display_mode * mode,const struct drm_display_mode * adjusted)76271e780f1SEnric Balletbo i Serra static void mtk_dsi_bridge_mode_set(struct drm_bridge *bridge,
76371e780f1SEnric Balletbo i Serra 				    const struct drm_display_mode *mode,
76471e780f1SEnric Balletbo i Serra 				    const struct drm_display_mode *adjusted)
76571e780f1SEnric Balletbo i Serra {
76671e780f1SEnric Balletbo i Serra 	struct mtk_dsi *dsi = bridge_to_dsi(bridge);
7672e54c14eSCK Hu 
76872ac6969SSatendra Singh Thakur 	drm_display_mode_to_videomode(adjusted, &dsi->vm);
7692e54c14eSCK Hu }
7702e54c14eSCK Hu 
mtk_dsi_bridge_atomic_disable(struct drm_bridge * bridge,struct drm_bridge_state * old_bridge_state)7717f6335c6SXinlei Lee static void mtk_dsi_bridge_atomic_disable(struct drm_bridge *bridge,
7727f6335c6SXinlei Lee 					  struct drm_bridge_state *old_bridge_state)
7732e54c14eSCK Hu {
77471e780f1SEnric Balletbo i Serra 	struct mtk_dsi *dsi = bridge_to_dsi(bridge);
7752e54c14eSCK Hu 
7762e54c14eSCK Hu 	mtk_output_dsi_disable(dsi);
7772e54c14eSCK Hu }
7782e54c14eSCK Hu 
mtk_dsi_bridge_atomic_enable(struct drm_bridge * bridge,struct drm_bridge_state * old_bridge_state)7797f6335c6SXinlei Lee static void mtk_dsi_bridge_atomic_enable(struct drm_bridge *bridge,
7807f6335c6SXinlei Lee 					 struct drm_bridge_state *old_bridge_state)
7812e54c14eSCK Hu {
78271e780f1SEnric Balletbo i Serra 	struct mtk_dsi *dsi = bridge_to_dsi(bridge);
7832e54c14eSCK Hu 
784cde7e2e3SJitao Shi 	if (dsi->refcount == 0)
785cde7e2e3SJitao Shi 		return;
786cde7e2e3SJitao Shi 
7872e54c14eSCK Hu 	mtk_output_dsi_enable(dsi);
7882e54c14eSCK Hu }
7892e54c14eSCK Hu 
mtk_dsi_bridge_atomic_pre_enable(struct drm_bridge * bridge,struct drm_bridge_state * old_bridge_state)790cde7e2e3SJitao Shi static void mtk_dsi_bridge_atomic_pre_enable(struct drm_bridge *bridge,
791cde7e2e3SJitao Shi 					     struct drm_bridge_state *old_bridge_state)
792cde7e2e3SJitao Shi {
793cde7e2e3SJitao Shi 	struct mtk_dsi *dsi = bridge_to_dsi(bridge);
794cde7e2e3SJitao Shi 	int ret;
795cde7e2e3SJitao Shi 
796cde7e2e3SJitao Shi 	ret = mtk_dsi_poweron(dsi);
797cde7e2e3SJitao Shi 	if (ret < 0)
798cde7e2e3SJitao Shi 		DRM_ERROR("failed to power on dsi\n");
799cde7e2e3SJitao Shi }
800cde7e2e3SJitao Shi 
mtk_dsi_bridge_atomic_post_disable(struct drm_bridge * bridge,struct drm_bridge_state * old_bridge_state)801cde7e2e3SJitao Shi static void mtk_dsi_bridge_atomic_post_disable(struct drm_bridge *bridge,
802cde7e2e3SJitao Shi 					       struct drm_bridge_state *old_bridge_state)
803cde7e2e3SJitao Shi {
804cde7e2e3SJitao Shi 	struct mtk_dsi *dsi = bridge_to_dsi(bridge);
805cde7e2e3SJitao Shi 
806cde7e2e3SJitao Shi 	mtk_dsi_poweroff(dsi);
807cde7e2e3SJitao Shi }
808cde7e2e3SJitao Shi 
80971e780f1SEnric Balletbo i Serra static const struct drm_bridge_funcs mtk_dsi_bridge_funcs = {
81071e780f1SEnric Balletbo i Serra 	.attach = mtk_dsi_bridge_attach,
811eeda05b5SAngeloGioacchino Del Regno 	.atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
8127f6335c6SXinlei Lee 	.atomic_disable = mtk_dsi_bridge_atomic_disable,
813eeda05b5SAngeloGioacchino Del Regno 	.atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
8147f6335c6SXinlei Lee 	.atomic_enable = mtk_dsi_bridge_atomic_enable,
815cde7e2e3SJitao Shi 	.atomic_pre_enable = mtk_dsi_bridge_atomic_pre_enable,
816cde7e2e3SJitao Shi 	.atomic_post_disable = mtk_dsi_bridge_atomic_post_disable,
817eeda05b5SAngeloGioacchino Del Regno 	.atomic_reset = drm_atomic_helper_bridge_reset,
81871e780f1SEnric Balletbo i Serra 	.mode_set = mtk_dsi_bridge_mode_set,
8192e54c14eSCK Hu };
8202e54c14eSCK Hu 
mtk_dsi_ddp_start(struct device * dev)8211d33f13aSCK Hu void mtk_dsi_ddp_start(struct device *dev)
8222e54c14eSCK Hu {
8234d510659SCK Hu 	struct mtk_dsi *dsi = dev_get_drvdata(dev);
8242e54c14eSCK Hu 
8252e54c14eSCK Hu 	mtk_dsi_poweron(dsi);
8262e54c14eSCK Hu }
8272e54c14eSCK Hu 
mtk_dsi_ddp_stop(struct device * dev)8281d33f13aSCK Hu void mtk_dsi_ddp_stop(struct device *dev)
8292e54c14eSCK Hu {
8304d510659SCK Hu 	struct mtk_dsi *dsi = dev_get_drvdata(dev);
8312e54c14eSCK Hu 
8322e54c14eSCK Hu 	mtk_dsi_poweroff(dsi);
8332e54c14eSCK Hu }
8342e54c14eSCK Hu 
mtk_dsi_encoder_init(struct drm_device * drm,struct mtk_dsi * dsi)835647474b8SAngeloGioacchino Del Regno static int mtk_dsi_encoder_init(struct drm_device *drm, struct mtk_dsi *dsi)
836647474b8SAngeloGioacchino Del Regno {
837647474b8SAngeloGioacchino Del Regno 	int ret;
838647474b8SAngeloGioacchino Del Regno 
839647474b8SAngeloGioacchino Del Regno 	ret = drm_simple_encoder_init(drm, &dsi->encoder,
840647474b8SAngeloGioacchino Del Regno 				      DRM_MODE_ENCODER_DSI);
841647474b8SAngeloGioacchino Del Regno 	if (ret) {
842647474b8SAngeloGioacchino Del Regno 		DRM_ERROR("Failed to encoder init to drm\n");
843647474b8SAngeloGioacchino Del Regno 		return ret;
844647474b8SAngeloGioacchino Del Regno 	}
845647474b8SAngeloGioacchino Del Regno 
846647474b8SAngeloGioacchino Del Regno 	dsi->encoder.possible_crtcs = mtk_drm_find_possible_crtc_by_comp(drm, dsi->host.dev);
847647474b8SAngeloGioacchino Del Regno 
848647474b8SAngeloGioacchino Del Regno 	ret = drm_bridge_attach(&dsi->encoder, &dsi->bridge, NULL,
849647474b8SAngeloGioacchino Del Regno 				DRM_BRIDGE_ATTACH_NO_CONNECTOR);
850647474b8SAngeloGioacchino Del Regno 	if (ret)
851647474b8SAngeloGioacchino Del Regno 		goto err_cleanup_encoder;
852647474b8SAngeloGioacchino Del Regno 
853647474b8SAngeloGioacchino Del Regno 	dsi->connector = drm_bridge_connector_init(drm, &dsi->encoder);
854647474b8SAngeloGioacchino Del Regno 	if (IS_ERR(dsi->connector)) {
855647474b8SAngeloGioacchino Del Regno 		DRM_ERROR("Unable to create bridge connector\n");
856647474b8SAngeloGioacchino Del Regno 		ret = PTR_ERR(dsi->connector);
857647474b8SAngeloGioacchino Del Regno 		goto err_cleanup_encoder;
858647474b8SAngeloGioacchino Del Regno 	}
859647474b8SAngeloGioacchino Del Regno 	drm_connector_attach_encoder(dsi->connector, &dsi->encoder);
860647474b8SAngeloGioacchino Del Regno 
861647474b8SAngeloGioacchino Del Regno 	return 0;
862647474b8SAngeloGioacchino Del Regno 
863647474b8SAngeloGioacchino Del Regno err_cleanup_encoder:
864647474b8SAngeloGioacchino Del Regno 	drm_encoder_cleanup(&dsi->encoder);
865647474b8SAngeloGioacchino Del Regno 	return ret;
866647474b8SAngeloGioacchino Del Regno }
867647474b8SAngeloGioacchino Del Regno 
mtk_dsi_bind(struct device * dev,struct device * master,void * data)868647474b8SAngeloGioacchino Del Regno static int mtk_dsi_bind(struct device *dev, struct device *master, void *data)
869647474b8SAngeloGioacchino Del Regno {
870647474b8SAngeloGioacchino Del Regno 	int ret;
871647474b8SAngeloGioacchino Del Regno 	struct drm_device *drm = data;
872647474b8SAngeloGioacchino Del Regno 	struct mtk_dsi *dsi = dev_get_drvdata(dev);
873647474b8SAngeloGioacchino Del Regno 
874647474b8SAngeloGioacchino Del Regno 	ret = mtk_dsi_encoder_init(drm, dsi);
875647474b8SAngeloGioacchino Del Regno 	if (ret)
876647474b8SAngeloGioacchino Del Regno 		return ret;
877647474b8SAngeloGioacchino Del Regno 
878647474b8SAngeloGioacchino Del Regno 	return device_reset_optional(dev);
879647474b8SAngeloGioacchino Del Regno }
880647474b8SAngeloGioacchino Del Regno 
mtk_dsi_unbind(struct device * dev,struct device * master,void * data)881647474b8SAngeloGioacchino Del Regno static void mtk_dsi_unbind(struct device *dev, struct device *master,
882647474b8SAngeloGioacchino Del Regno 			   void *data)
883647474b8SAngeloGioacchino Del Regno {
884647474b8SAngeloGioacchino Del Regno 	struct mtk_dsi *dsi = dev_get_drvdata(dev);
885647474b8SAngeloGioacchino Del Regno 
886647474b8SAngeloGioacchino Del Regno 	drm_encoder_cleanup(&dsi->encoder);
887647474b8SAngeloGioacchino Del Regno }
888647474b8SAngeloGioacchino Del Regno 
889647474b8SAngeloGioacchino Del Regno static const struct component_ops mtk_dsi_component_ops = {
890647474b8SAngeloGioacchino Del Regno 	.bind = mtk_dsi_bind,
891647474b8SAngeloGioacchino Del Regno 	.unbind = mtk_dsi_unbind,
892647474b8SAngeloGioacchino Del Regno };
893647474b8SAngeloGioacchino Del Regno 
mtk_dsi_host_attach(struct mipi_dsi_host * host,struct mipi_dsi_device * device)8942e54c14eSCK Hu static int mtk_dsi_host_attach(struct mipi_dsi_host *host,
8952e54c14eSCK Hu 			       struct mipi_dsi_device *device)
8962e54c14eSCK Hu {
8972e54c14eSCK Hu 	struct mtk_dsi *dsi = host_to_dsi(host);
898647474b8SAngeloGioacchino Del Regno 	struct device *dev = host->dev;
899647474b8SAngeloGioacchino Del Regno 	int ret;
9002e54c14eSCK Hu 
9012e54c14eSCK Hu 	dsi->lanes = device->lanes;
9022e54c14eSCK Hu 	dsi->format = device->format;
9032e54c14eSCK Hu 	dsi->mode_flags = device->mode_flags;
904647474b8SAngeloGioacchino Del Regno 	dsi->next_bridge = devm_drm_of_get_bridge(dev, dev->of_node, 0, 0);
905647474b8SAngeloGioacchino Del Regno 	if (IS_ERR(dsi->next_bridge))
906647474b8SAngeloGioacchino Del Regno 		return PTR_ERR(dsi->next_bridge);
9072e54c14eSCK Hu 
908647474b8SAngeloGioacchino Del Regno 	drm_bridge_add(&dsi->bridge);
909647474b8SAngeloGioacchino Del Regno 
910647474b8SAngeloGioacchino Del Regno 	ret = component_add(host->dev, &mtk_dsi_component_ops);
911647474b8SAngeloGioacchino Del Regno 	if (ret) {
912647474b8SAngeloGioacchino Del Regno 		DRM_ERROR("failed to add dsi_host component: %d\n", ret);
913647474b8SAngeloGioacchino Del Regno 		drm_bridge_remove(&dsi->bridge);
914647474b8SAngeloGioacchino Del Regno 		return ret;
915647474b8SAngeloGioacchino Del Regno 	}
916647474b8SAngeloGioacchino Del Regno 
917647474b8SAngeloGioacchino Del Regno 	return 0;
918647474b8SAngeloGioacchino Del Regno }
919647474b8SAngeloGioacchino Del Regno 
mtk_dsi_host_detach(struct mipi_dsi_host * host,struct mipi_dsi_device * device)920647474b8SAngeloGioacchino Del Regno static int mtk_dsi_host_detach(struct mipi_dsi_host *host,
921647474b8SAngeloGioacchino Del Regno 			       struct mipi_dsi_device *device)
922647474b8SAngeloGioacchino Del Regno {
923647474b8SAngeloGioacchino Del Regno 	struct mtk_dsi *dsi = host_to_dsi(host);
924647474b8SAngeloGioacchino Del Regno 
925647474b8SAngeloGioacchino Del Regno 	component_del(host->dev, &mtk_dsi_component_ops);
926647474b8SAngeloGioacchino Del Regno 	drm_bridge_remove(&dsi->bridge);
9272e54c14eSCK Hu 	return 0;
9282e54c14eSCK Hu }
9292e54c14eSCK Hu 
mtk_dsi_wait_for_idle(struct mtk_dsi * dsi)93021898816Sshaoming chen static void mtk_dsi_wait_for_idle(struct mtk_dsi *dsi)
93121898816Sshaoming chen {
932f752413eSDan Carpenter 	int ret;
933f752413eSDan Carpenter 	u32 val;
93421898816Sshaoming chen 
935f752413eSDan Carpenter 	ret = readl_poll_timeout(dsi->regs + DSI_INTSTA, val, !(val & DSI_BUSY),
936f752413eSDan Carpenter 				 4, 2000000);
937f752413eSDan Carpenter 	if (ret) {
93821898816Sshaoming chen 		DRM_WARN("polling dsi wait not busy timeout!\n");
93921898816Sshaoming chen 
94021898816Sshaoming chen 		mtk_dsi_enable(dsi);
94121898816Sshaoming chen 		mtk_dsi_reset_engine(dsi);
94221898816Sshaoming chen 	}
94321898816Sshaoming chen }
94421898816Sshaoming chen 
mtk_dsi_recv_cnt(u8 type,u8 * read_data)94521898816Sshaoming chen static u32 mtk_dsi_recv_cnt(u8 type, u8 *read_data)
94621898816Sshaoming chen {
94721898816Sshaoming chen 	switch (type) {
94821898816Sshaoming chen 	case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_1BYTE:
94921898816Sshaoming chen 	case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_1BYTE:
95021898816Sshaoming chen 		return 1;
95121898816Sshaoming chen 	case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_2BYTE:
95221898816Sshaoming chen 	case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_2BYTE:
95321898816Sshaoming chen 		return 2;
95421898816Sshaoming chen 	case MIPI_DSI_RX_GENERIC_LONG_READ_RESPONSE:
95521898816Sshaoming chen 	case MIPI_DSI_RX_DCS_LONG_READ_RESPONSE:
95621898816Sshaoming chen 		return read_data[1] + read_data[2] * 16;
95721898816Sshaoming chen 	case MIPI_DSI_RX_ACKNOWLEDGE_AND_ERROR_REPORT:
95821898816Sshaoming chen 		DRM_INFO("type is 0x02, try again\n");
95921898816Sshaoming chen 		break;
96021898816Sshaoming chen 	default:
961afd89636SColin Ian King 		DRM_INFO("type(0x%x) not recognized\n", type);
96221898816Sshaoming chen 		break;
96321898816Sshaoming chen 	}
96421898816Sshaoming chen 
96521898816Sshaoming chen 	return 0;
96621898816Sshaoming chen }
96721898816Sshaoming chen 
mtk_dsi_cmdq(struct mtk_dsi * dsi,const struct mipi_dsi_msg * msg)96821898816Sshaoming chen static void mtk_dsi_cmdq(struct mtk_dsi *dsi, const struct mipi_dsi_msg *msg)
96921898816Sshaoming chen {
97021898816Sshaoming chen 	const char *tx_buf = msg->tx_buf;
97121898816Sshaoming chen 	u8 config, cmdq_size, cmdq_off, type = msg->type;
97221898816Sshaoming chen 	u32 reg_val, cmdq_mask, i;
973bb6bc298SJitao Shi 	u32 reg_cmdq_off = dsi->driver_data->reg_cmdq_off;
97421898816Sshaoming chen 
97521898816Sshaoming chen 	if (MTK_DSI_HOST_IS_READ(type))
97621898816Sshaoming chen 		config = BTA;
97721898816Sshaoming chen 	else
97821898816Sshaoming chen 		config = (msg->tx_len > 2) ? LONG_PACKET : SHORT_PACKET;
97921898816Sshaoming chen 
98021898816Sshaoming chen 	if (msg->tx_len > 2) {
98121898816Sshaoming chen 		cmdq_size = 1 + (msg->tx_len + 3) / 4;
98221898816Sshaoming chen 		cmdq_off = 4;
98321898816Sshaoming chen 		cmdq_mask = CONFIG | DATA_ID | DATA_0 | DATA_1;
98421898816Sshaoming chen 		reg_val = (msg->tx_len << 16) | (type << 8) | config;
98521898816Sshaoming chen 	} else {
98621898816Sshaoming chen 		cmdq_size = 1;
98721898816Sshaoming chen 		cmdq_off = 2;
98821898816Sshaoming chen 		cmdq_mask = CONFIG | DATA_ID;
98921898816Sshaoming chen 		reg_val = (type << 8) | config;
99021898816Sshaoming chen 	}
99121898816Sshaoming chen 
99221898816Sshaoming chen 	for (i = 0; i < msg->tx_len; i++)
99389d0e3f8SJitao Shi 		mtk_dsi_mask(dsi, (reg_cmdq_off + cmdq_off + i) & (~0x3U),
99489d0e3f8SJitao Shi 			     (0xffUL << (((i + cmdq_off) & 3U) * 8U)),
99589d0e3f8SJitao Shi 			     tx_buf[i] << (((i + cmdq_off) & 3U) * 8U));
99621898816Sshaoming chen 
997bb6bc298SJitao Shi 	mtk_dsi_mask(dsi, reg_cmdq_off, cmdq_mask, reg_val);
99821898816Sshaoming chen 	mtk_dsi_mask(dsi, DSI_CMDQ_SIZE, CMDQ_SIZE, cmdq_size);
99921898816Sshaoming chen }
100021898816Sshaoming chen 
mtk_dsi_host_send_cmd(struct mtk_dsi * dsi,const struct mipi_dsi_msg * msg,u8 flag)100121898816Sshaoming chen static ssize_t mtk_dsi_host_send_cmd(struct mtk_dsi *dsi,
100221898816Sshaoming chen 				     const struct mipi_dsi_msg *msg, u8 flag)
100321898816Sshaoming chen {
100421898816Sshaoming chen 	mtk_dsi_wait_for_idle(dsi);
100521898816Sshaoming chen 	mtk_dsi_irq_data_clear(dsi, flag);
100621898816Sshaoming chen 	mtk_dsi_cmdq(dsi, msg);
100721898816Sshaoming chen 	mtk_dsi_start(dsi);
100821898816Sshaoming chen 
100921898816Sshaoming chen 	if (!mtk_dsi_wait_for_irq_done(dsi, flag, 2000))
101021898816Sshaoming chen 		return -ETIME;
101121898816Sshaoming chen 	else
101221898816Sshaoming chen 		return 0;
101321898816Sshaoming chen }
101421898816Sshaoming chen 
mtk_dsi_host_transfer(struct mipi_dsi_host * host,const struct mipi_dsi_msg * msg)101521898816Sshaoming chen static ssize_t mtk_dsi_host_transfer(struct mipi_dsi_host *host,
101621898816Sshaoming chen 				     const struct mipi_dsi_msg *msg)
101721898816Sshaoming chen {
101821898816Sshaoming chen 	struct mtk_dsi *dsi = host_to_dsi(host);
101921898816Sshaoming chen 	u32 recv_cnt, i;
102021898816Sshaoming chen 	u8 read_data[16];
102121898816Sshaoming chen 	void *src_addr;
102221898816Sshaoming chen 	u8 irq_flag = CMD_DONE_INT_FLAG;
102381cc7e51SJulien STEPHAN 	u32 dsi_mode;
102481cc7e51SJulien STEPHAN 	int ret;
102521898816Sshaoming chen 
102681cc7e51SJulien STEPHAN 	dsi_mode = readl(dsi->regs + DSI_MODE_CTRL);
102781cc7e51SJulien STEPHAN 	if (dsi_mode & MODE) {
102881cc7e51SJulien STEPHAN 		mtk_dsi_stop(dsi);
102981cc7e51SJulien STEPHAN 		ret = mtk_dsi_switch_to_cmd_mode(dsi, VM_DONE_INT_FLAG, 500);
103081cc7e51SJulien STEPHAN 		if (ret)
103181cc7e51SJulien STEPHAN 			goto restore_dsi_mode;
103221898816Sshaoming chen 	}
103321898816Sshaoming chen 
103421898816Sshaoming chen 	if (MTK_DSI_HOST_IS_READ(msg->type))
103521898816Sshaoming chen 		irq_flag |= LPRX_RD_RDY_INT_FLAG;
103621898816Sshaoming chen 
103739e8d062SJitao Shi 	mtk_dsi_lane_ready(dsi);
103839e8d062SJitao Shi 
103981cc7e51SJulien STEPHAN 	ret = mtk_dsi_host_send_cmd(dsi, msg, irq_flag);
104081cc7e51SJulien STEPHAN 	if (ret)
104181cc7e51SJulien STEPHAN 		goto restore_dsi_mode;
104221898816Sshaoming chen 
104381cc7e51SJulien STEPHAN 	if (!MTK_DSI_HOST_IS_READ(msg->type)) {
104481cc7e51SJulien STEPHAN 		recv_cnt = 0;
104581cc7e51SJulien STEPHAN 		goto restore_dsi_mode;
104681cc7e51SJulien STEPHAN 	}
104721898816Sshaoming chen 
104821898816Sshaoming chen 	if (!msg->rx_buf) {
104921898816Sshaoming chen 		DRM_ERROR("dsi receive buffer size may be NULL\n");
105081cc7e51SJulien STEPHAN 		ret = -EINVAL;
105181cc7e51SJulien STEPHAN 		goto restore_dsi_mode;
105221898816Sshaoming chen 	}
105321898816Sshaoming chen 
105421898816Sshaoming chen 	for (i = 0; i < 16; i++)
105521898816Sshaoming chen 		*(read_data + i) = readb(dsi->regs + DSI_RX_DATA0 + i);
105621898816Sshaoming chen 
105721898816Sshaoming chen 	recv_cnt = mtk_dsi_recv_cnt(read_data[0], read_data);
105821898816Sshaoming chen 
105921898816Sshaoming chen 	if (recv_cnt > 2)
106021898816Sshaoming chen 		src_addr = &read_data[4];
106121898816Sshaoming chen 	else
106221898816Sshaoming chen 		src_addr = &read_data[1];
106321898816Sshaoming chen 
106421898816Sshaoming chen 	if (recv_cnt > 10)
106521898816Sshaoming chen 		recv_cnt = 10;
106621898816Sshaoming chen 
106721898816Sshaoming chen 	if (recv_cnt > msg->rx_len)
106821898816Sshaoming chen 		recv_cnt = msg->rx_len;
106921898816Sshaoming chen 
107021898816Sshaoming chen 	if (recv_cnt)
107121898816Sshaoming chen 		memcpy(msg->rx_buf, src_addr, recv_cnt);
107221898816Sshaoming chen 
107321898816Sshaoming chen 	DRM_INFO("dsi get %d byte data from the panel address(0x%x)\n",
107421898816Sshaoming chen 		 recv_cnt, *((u8 *)(msg->tx_buf)));
107521898816Sshaoming chen 
107681cc7e51SJulien STEPHAN restore_dsi_mode:
107781cc7e51SJulien STEPHAN 	if (dsi_mode & MODE) {
107881cc7e51SJulien STEPHAN 		mtk_dsi_set_mode(dsi);
107981cc7e51SJulien STEPHAN 		mtk_dsi_start(dsi);
108081cc7e51SJulien STEPHAN 	}
108181cc7e51SJulien STEPHAN 
108281cc7e51SJulien STEPHAN 	return ret < 0 ? ret : recv_cnt;
108321898816Sshaoming chen }
108421898816Sshaoming chen 
10852e54c14eSCK Hu static const struct mipi_dsi_host_ops mtk_dsi_ops = {
10862e54c14eSCK Hu 	.attach = mtk_dsi_host_attach,
1087647474b8SAngeloGioacchino Del Regno 	.detach = mtk_dsi_host_detach,
108821898816Sshaoming chen 	.transfer = mtk_dsi_host_transfer,
10892e54c14eSCK Hu };
10902e54c14eSCK Hu 
mtk_dsi_probe(struct platform_device * pdev)10912e54c14eSCK Hu static int mtk_dsi_probe(struct platform_device *pdev)
10922e54c14eSCK Hu {
10932e54c14eSCK Hu 	struct mtk_dsi *dsi;
10942e54c14eSCK Hu 	struct device *dev = &pdev->dev;
10952e54c14eSCK Hu 	struct resource *regs;
1096dd5080a5Sshaoming chen 	int irq_num;
10972e54c14eSCK Hu 	int ret;
10982e54c14eSCK Hu 
10992e54c14eSCK Hu 	dsi = devm_kzalloc(dev, sizeof(*dsi), GFP_KERNEL);
11002e54c14eSCK Hu 	if (!dsi)
11012e54c14eSCK Hu 		return -ENOMEM;
11022e54c14eSCK Hu 
11032e54c14eSCK Hu 	dsi->host.ops = &mtk_dsi_ops;
11042e54c14eSCK Hu 	dsi->host.dev = dev;
1105b3218e74SJitao Shi 	ret = mipi_dsi_host_register(&dsi->host);
1106b3218e74SJitao Shi 	if (ret < 0) {
1107b3218e74SJitao Shi 		dev_err(dev, "failed to register DSI host: %d\n", ret);
1108b3218e74SJitao Shi 		return ret;
1109b3218e74SJitao Shi 	}
11102e54c14eSCK Hu 
1111bb6bc298SJitao Shi 	dsi->driver_data = of_device_get_match_data(dev);
11122e54c14eSCK Hu 
11132e54c14eSCK Hu 	dsi->engine_clk = devm_clk_get(dev, "engine");
11142e54c14eSCK Hu 	if (IS_ERR(dsi->engine_clk)) {
11152e54c14eSCK Hu 		ret = PTR_ERR(dsi->engine_clk);
1116af19d645SMatthias Brugger 
1117af19d645SMatthias Brugger 		if (ret != -EPROBE_DEFER)
11182e54c14eSCK Hu 			dev_err(dev, "Failed to get engine clock: %d\n", ret);
1119b3218e74SJitao Shi 		goto err_unregister_host;
11202e54c14eSCK Hu 	}
11212e54c14eSCK Hu 
11222e54c14eSCK Hu 	dsi->digital_clk = devm_clk_get(dev, "digital");
11232e54c14eSCK Hu 	if (IS_ERR(dsi->digital_clk)) {
11242e54c14eSCK Hu 		ret = PTR_ERR(dsi->digital_clk);
1125af19d645SMatthias Brugger 
1126af19d645SMatthias Brugger 		if (ret != -EPROBE_DEFER)
11272e54c14eSCK Hu 			dev_err(dev, "Failed to get digital clock: %d\n", ret);
1128b3218e74SJitao Shi 		goto err_unregister_host;
11292e54c14eSCK Hu 	}
11302e54c14eSCK Hu 
11312e54c14eSCK Hu 	dsi->hs_clk = devm_clk_get(dev, "hs");
11322e54c14eSCK Hu 	if (IS_ERR(dsi->hs_clk)) {
11332e54c14eSCK Hu 		ret = PTR_ERR(dsi->hs_clk);
11342e54c14eSCK Hu 		dev_err(dev, "Failed to get hs clock: %d\n", ret);
1135b3218e74SJitao Shi 		goto err_unregister_host;
11362e54c14eSCK Hu 	}
11372e54c14eSCK Hu 
11382e54c14eSCK Hu 	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
11392e54c14eSCK Hu 	dsi->regs = devm_ioremap_resource(dev, regs);
11402e54c14eSCK Hu 	if (IS_ERR(dsi->regs)) {
11412e54c14eSCK Hu 		ret = PTR_ERR(dsi->regs);
11422e54c14eSCK Hu 		dev_err(dev, "Failed to ioremap memory: %d\n", ret);
1143b3218e74SJitao Shi 		goto err_unregister_host;
11442e54c14eSCK Hu 	}
11452e54c14eSCK Hu 
11462e54c14eSCK Hu 	dsi->phy = devm_phy_get(dev, "dphy");
11472e54c14eSCK Hu 	if (IS_ERR(dsi->phy)) {
11482e54c14eSCK Hu 		ret = PTR_ERR(dsi->phy);
11492e54c14eSCK Hu 		dev_err(dev, "Failed to get MIPI-DPHY: %d\n", ret);
1150b3218e74SJitao Shi 		goto err_unregister_host;
11512e54c14eSCK Hu 	}
11522e54c14eSCK Hu 
1153dd5080a5Sshaoming chen 	irq_num = platform_get_irq(pdev, 0);
1154dd5080a5Sshaoming chen 	if (irq_num < 0) {
1155b3218e74SJitao Shi 		ret = irq_num;
1156b3218e74SJitao Shi 		goto err_unregister_host;
1157dd5080a5Sshaoming chen 	}
1158dd5080a5Sshaoming chen 
1159dd5080a5Sshaoming chen 	ret = devm_request_irq(&pdev->dev, irq_num, mtk_dsi_irq,
1160435884b0SJitao Shi 			       IRQF_TRIGGER_NONE, dev_name(&pdev->dev), dsi);
1161dd5080a5Sshaoming chen 	if (ret) {
1162dd5080a5Sshaoming chen 		dev_err(&pdev->dev, "failed to request mediatek dsi irq\n");
1163b3218e74SJitao Shi 		goto err_unregister_host;
1164dd5080a5Sshaoming chen 	}
1165dd5080a5Sshaoming chen 
1166dd5080a5Sshaoming chen 	init_waitqueue_head(&dsi->irq_wait_queue);
1167dd5080a5Sshaoming chen 
11682e54c14eSCK Hu 	platform_set_drvdata(pdev, dsi);
11692e54c14eSCK Hu 
117071e780f1SEnric Balletbo i Serra 	dsi->bridge.funcs = &mtk_dsi_bridge_funcs;
117171e780f1SEnric Balletbo i Serra 	dsi->bridge.of_node = dev->of_node;
117271e780f1SEnric Balletbo i Serra 	dsi->bridge.type = DRM_MODE_CONNECTOR_DSI;
117371e780f1SEnric Balletbo i Serra 
1174b3218e74SJitao Shi 	return 0;
1175b3218e74SJitao Shi 
1176b3218e74SJitao Shi err_unregister_host:
1177b3218e74SJitao Shi 	mipi_dsi_host_unregister(&dsi->host);
1178b3218e74SJitao Shi 	return ret;
11792e54c14eSCK Hu }
11802e54c14eSCK Hu 
mtk_dsi_remove(struct platform_device * pdev)1181b3af12a0SUwe Kleine-König static void mtk_dsi_remove(struct platform_device *pdev)
11822e54c14eSCK Hu {
11832e54c14eSCK Hu 	struct mtk_dsi *dsi = platform_get_drvdata(pdev);
11842e54c14eSCK Hu 
11852e54c14eSCK Hu 	mtk_output_dsi_disable(dsi);
1186b3218e74SJitao Shi 	mipi_dsi_host_unregister(&dsi->host);
11872e54c14eSCK Hu }
11882e54c14eSCK Hu 
1189bb6bc298SJitao Shi static const struct mtk_dsi_driver_data mt8173_dsi_driver_data = {
1190bb6bc298SJitao Shi 	.reg_cmdq_off = 0x200,
1191bb6bc298SJitao Shi };
1192bb6bc298SJitao Shi 
1193bb6bc298SJitao Shi static const struct mtk_dsi_driver_data mt2701_dsi_driver_data = {
1194bb6bc298SJitao Shi 	.reg_cmdq_off = 0x180,
1195bb6bc298SJitao Shi };
1196bb6bc298SJitao Shi 
1197e249e3e8SJitao Shi static const struct mtk_dsi_driver_data mt8183_dsi_driver_data = {
1198e249e3e8SJitao Shi 	.reg_cmdq_off = 0x200,
1199e249e3e8SJitao Shi 	.has_shadow_ctl = true,
1200e249e3e8SJitao Shi 	.has_size_ctl = true,
1201e249e3e8SJitao Shi };
1202e249e3e8SJitao Shi 
120303d7adc4SXinlei Lee static const struct mtk_dsi_driver_data mt8186_dsi_driver_data = {
120403d7adc4SXinlei Lee 	.reg_cmdq_off = 0xd00,
120503d7adc4SXinlei Lee 	.has_shadow_ctl = true,
120603d7adc4SXinlei Lee 	.has_size_ctl = true,
120703d7adc4SXinlei Lee };
120803d7adc4SXinlei Lee 
12092e54c14eSCK Hu static const struct of_device_id mtk_dsi_of_match[] = {
1210bb6bc298SJitao Shi 	{ .compatible = "mediatek,mt2701-dsi",
1211bb6bc298SJitao Shi 	  .data = &mt2701_dsi_driver_data },
1212bb6bc298SJitao Shi 	{ .compatible = "mediatek,mt8173-dsi",
1213bb6bc298SJitao Shi 	  .data = &mt8173_dsi_driver_data },
1214e249e3e8SJitao Shi 	{ .compatible = "mediatek,mt8183-dsi",
1215e249e3e8SJitao Shi 	  .data = &mt8183_dsi_driver_data },
121603d7adc4SXinlei Lee 	{ .compatible = "mediatek,mt8186-dsi",
121703d7adc4SXinlei Lee 	  .data = &mt8186_dsi_driver_data },
12182e54c14eSCK Hu 	{ },
12192e54c14eSCK Hu };
1220fdcbe17cSBoris Brezillon MODULE_DEVICE_TABLE(of, mtk_dsi_of_match);
12212e54c14eSCK Hu 
12222e54c14eSCK Hu struct platform_driver mtk_dsi_driver = {
12232e54c14eSCK Hu 	.probe = mtk_dsi_probe,
1224b3af12a0SUwe Kleine-König 	.remove_new = mtk_dsi_remove,
12252e54c14eSCK Hu 	.driver = {
12262e54c14eSCK Hu 		.name = "mtk-dsi",
12272e54c14eSCK Hu 		.of_match_table = mtk_dsi_of_match,
12282e54c14eSCK Hu 	},
12292e54c14eSCK Hu };
1230