xref: /openbmc/linux/drivers/gpu/drm/bridge/tc358767.c (revision 3080c21a043abc83e56da9bb5942eb3f4b92c90e)
1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
27caff0fcSAndrey Gusakov /*
3bbfd3190SMarek Vasut  * TC358767/TC358867/TC9595 DSI/DPI-to-DPI/(e)DP bridge driver
4bbfd3190SMarek Vasut  *
5bbfd3190SMarek Vasut  * The TC358767/TC358867/TC9595 can operate in multiple modes.
6bbfd3190SMarek Vasut  * The following modes are supported:
7bbfd3190SMarek Vasut  *   DPI->(e)DP -- supported
8bbfd3190SMarek Vasut  *   DSI->DPI .... supported
9bbfd3190SMarek Vasut  *   DSI->(e)DP .. NOT supported
107caff0fcSAndrey Gusakov  *
117caff0fcSAndrey Gusakov  * Copyright (C) 2016 CogentEmbedded Inc
127caff0fcSAndrey Gusakov  * Author: Andrey Gusakov <andrey.gusakov@cogentembedded.com>
137caff0fcSAndrey Gusakov  *
147caff0fcSAndrey Gusakov  * Copyright (C) 2016 Pengutronix, Philipp Zabel <p.zabel@pengutronix.de>
157caff0fcSAndrey Gusakov  *
162f51be09SAndrey Gusakov  * Copyright (C) 2016 Zodiac Inflight Innovations
172f51be09SAndrey Gusakov  *
187caff0fcSAndrey Gusakov  * Initially based on: drivers/gpu/drm/i2c/tda998x_drv.c
197caff0fcSAndrey Gusakov  *
207caff0fcSAndrey Gusakov  * Copyright (C) 2012 Texas Instruments
217caff0fcSAndrey Gusakov  * Author: Rob Clark <robdclark@gmail.com>
227caff0fcSAndrey Gusakov  */
237caff0fcSAndrey Gusakov 
243f072c30SAndrey Smirnov #include <linux/bitfield.h>
257caff0fcSAndrey Gusakov #include <linux/clk.h>
267caff0fcSAndrey Gusakov #include <linux/device.h>
277caff0fcSAndrey Gusakov #include <linux/gpio/consumer.h>
287caff0fcSAndrey Gusakov #include <linux/i2c.h>
297caff0fcSAndrey Gusakov #include <linux/kernel.h>
307caff0fcSAndrey Gusakov #include <linux/module.h>
317caff0fcSAndrey Gusakov #include <linux/regmap.h>
327caff0fcSAndrey Gusakov #include <linux/slab.h>
337caff0fcSAndrey Gusakov 
34da68386dSThomas Zimmermann #include <drm/display/drm_dp_helper.h>
357caff0fcSAndrey Gusakov #include <drm/drm_atomic_helper.h>
36ee68c743SBoris Brezillon #include <drm/drm_bridge.h>
377caff0fcSAndrey Gusakov #include <drm/drm_edid.h>
38bbfd3190SMarek Vasut #include <drm/drm_mipi_dsi.h>
397caff0fcSAndrey Gusakov #include <drm/drm_of.h>
407caff0fcSAndrey Gusakov #include <drm/drm_panel.h>
41a25b988fSLaurent Pinchart #include <drm/drm_print.h>
42fcd70cd3SDaniel Vetter #include <drm/drm_probe_helper.h>
437caff0fcSAndrey Gusakov 
447caff0fcSAndrey Gusakov /* Registers */
457caff0fcSAndrey Gusakov 
46bbfd3190SMarek Vasut /* PPI layer registers */
47bbfd3190SMarek Vasut #define PPI_STARTPPI		0x0104 /* START control bit */
48bbfd3190SMarek Vasut #define PPI_LPTXTIMECNT		0x0114 /* LPTX timing signal */
49bbfd3190SMarek Vasut #define LPX_PERIOD			3
50bbfd3190SMarek Vasut #define PPI_LANEENABLE		0x0134
51bbfd3190SMarek Vasut #define PPI_TX_RX_TA		0x013c
52bbfd3190SMarek Vasut #define TTA_GET				0x40000
53bbfd3190SMarek Vasut #define TTA_SURE			6
54bbfd3190SMarek Vasut #define PPI_D0S_ATMR		0x0144
55bbfd3190SMarek Vasut #define PPI_D1S_ATMR		0x0148
56bbfd3190SMarek Vasut #define PPI_D0S_CLRSIPOCOUNT	0x0164 /* Assertion timer for Lane 0 */
57bbfd3190SMarek Vasut #define PPI_D1S_CLRSIPOCOUNT	0x0168 /* Assertion timer for Lane 1 */
58bbfd3190SMarek Vasut #define PPI_D2S_CLRSIPOCOUNT	0x016c /* Assertion timer for Lane 2 */
59bbfd3190SMarek Vasut #define PPI_D3S_CLRSIPOCOUNT	0x0170 /* Assertion timer for Lane 3 */
60bbfd3190SMarek Vasut #define PPI_START_FUNCTION		BIT(0)
61bbfd3190SMarek Vasut 
62bbfd3190SMarek Vasut /* DSI layer registers */
63bbfd3190SMarek Vasut #define DSI_STARTDSI		0x0204 /* START control bit of DSI-TX */
64bbfd3190SMarek Vasut #define DSI_LANEENABLE		0x0210 /* Enables each lane */
65bbfd3190SMarek Vasut #define DSI_RX_START			BIT(0)
66bbfd3190SMarek Vasut 
67bbfd3190SMarek Vasut /* Lane enable PPI and DSI register bits */
68bbfd3190SMarek Vasut #define LANEENABLE_CLEN		BIT(0)
69bbfd3190SMarek Vasut #define LANEENABLE_L0EN		BIT(1)
70bbfd3190SMarek Vasut #define LANEENABLE_L1EN		BIT(2)
71bbfd3190SMarek Vasut #define LANEENABLE_L2EN		BIT(1)
72bbfd3190SMarek Vasut #define LANEENABLE_L3EN		BIT(2)
73bbfd3190SMarek Vasut 
74bbfd3190SMarek Vasut /* Display Parallel Input Interface */
757caff0fcSAndrey Gusakov #define DPIPXLFMT		0x0440
767caff0fcSAndrey Gusakov #define VS_POL_ACTIVE_LOW		(1 << 10)
777caff0fcSAndrey Gusakov #define HS_POL_ACTIVE_LOW		(1 << 9)
787caff0fcSAndrey Gusakov #define DE_POL_ACTIVE_HIGH		(0 << 8)
797caff0fcSAndrey Gusakov #define SUB_CFG_TYPE_CONFIG1		(0 << 2) /* LSB aligned */
807caff0fcSAndrey Gusakov #define SUB_CFG_TYPE_CONFIG2		(1 << 2) /* Loosely Packed */
817caff0fcSAndrey Gusakov #define SUB_CFG_TYPE_CONFIG3		(2 << 2) /* LSB aligned 8-bit */
827caff0fcSAndrey Gusakov #define DPI_BPP_RGB888			(0 << 0)
837caff0fcSAndrey Gusakov #define DPI_BPP_RGB666			(1 << 0)
847caff0fcSAndrey Gusakov #define DPI_BPP_RGB565			(2 << 0)
857caff0fcSAndrey Gusakov 
86bbfd3190SMarek Vasut /* Display Parallel Output Interface */
87bbfd3190SMarek Vasut #define POCTRL			0x0448
88bbfd3190SMarek Vasut #define POCTRL_S2P			BIT(7)
89bbfd3190SMarek Vasut #define POCTRL_PCLK_POL			BIT(3)
90bbfd3190SMarek Vasut #define POCTRL_VS_POL			BIT(2)
91bbfd3190SMarek Vasut #define POCTRL_HS_POL			BIT(1)
92bbfd3190SMarek Vasut #define POCTRL_DE_POL			BIT(0)
93bbfd3190SMarek Vasut 
947caff0fcSAndrey Gusakov /* Video Path */
957caff0fcSAndrey Gusakov #define VPCTRL0			0x0450
963f072c30SAndrey Smirnov #define VSDELAY			GENMASK(31, 20)
977caff0fcSAndrey Gusakov #define OPXLFMT_RGB666			(0 << 8)
987caff0fcSAndrey Gusakov #define OPXLFMT_RGB888			(1 << 8)
997caff0fcSAndrey Gusakov #define FRMSYNC_DISABLED		(0 << 4) /* Video Timing Gen Disabled */
1007caff0fcSAndrey Gusakov #define FRMSYNC_ENABLED			(1 << 4) /* Video Timing Gen Enabled */
1017caff0fcSAndrey Gusakov #define MSF_DISABLED			(0 << 0) /* Magic Square FRC disabled */
1027caff0fcSAndrey Gusakov #define MSF_ENABLED			(1 << 0) /* Magic Square FRC enabled */
1037caff0fcSAndrey Gusakov #define HTIM01			0x0454
1043f072c30SAndrey Smirnov #define HPW			GENMASK(8, 0)
1053f072c30SAndrey Smirnov #define HBPR			GENMASK(24, 16)
1067caff0fcSAndrey Gusakov #define HTIM02			0x0458
1073f072c30SAndrey Smirnov #define HDISPR			GENMASK(10, 0)
1083f072c30SAndrey Smirnov #define HFPR			GENMASK(24, 16)
1097caff0fcSAndrey Gusakov #define VTIM01			0x045c
1103f072c30SAndrey Smirnov #define VSPR			GENMASK(7, 0)
1113f072c30SAndrey Smirnov #define VBPR			GENMASK(23, 16)
1127caff0fcSAndrey Gusakov #define VTIM02			0x0460
1133f072c30SAndrey Smirnov #define VFPR			GENMASK(23, 16)
1143f072c30SAndrey Smirnov #define VDISPR			GENMASK(10, 0)
1157caff0fcSAndrey Gusakov #define VFUEN0			0x0464
1167caff0fcSAndrey Gusakov #define VFUEN				BIT(0)   /* Video Frame Timing Upload */
1177caff0fcSAndrey Gusakov 
1187caff0fcSAndrey Gusakov /* System */
1197caff0fcSAndrey Gusakov #define TC_IDREG		0x0500
120f25ee501STomi Valkeinen #define SYSSTAT			0x0508
1217caff0fcSAndrey Gusakov #define SYSCTRL			0x0510
1227caff0fcSAndrey Gusakov #define DP0_AUDSRC_NO_INPUT		(0 << 3)
1237caff0fcSAndrey Gusakov #define DP0_AUDSRC_I2S_RX		(1 << 3)
1247caff0fcSAndrey Gusakov #define DP0_VIDSRC_NO_INPUT		(0 << 0)
1257caff0fcSAndrey Gusakov #define DP0_VIDSRC_DSI_RX		(1 << 0)
1267caff0fcSAndrey Gusakov #define DP0_VIDSRC_DPI_RX		(2 << 0)
1277caff0fcSAndrey Gusakov #define DP0_VIDSRC_COLOR_BAR		(3 << 0)
12852c2197aSLucas Stach #define SYSRSTENB		0x050c
12952c2197aSLucas Stach #define ENBI2C				(1 << 0)
13052c2197aSLucas Stach #define ENBLCD0				(1 << 2)
13152c2197aSLucas Stach #define ENBBM				(1 << 3)
13252c2197aSLucas Stach #define ENBDSIRX			(1 << 4)
13352c2197aSLucas Stach #define ENBREG				(1 << 5)
13452c2197aSLucas Stach #define ENBHDCP				(1 << 8)
135af9526f2STomi Valkeinen #define GPIOM			0x0540
136f25ee501STomi Valkeinen #define GPIOC			0x0544
137f25ee501STomi Valkeinen #define GPIOO			0x0548
138af9526f2STomi Valkeinen #define GPIOI			0x054c
139af9526f2STomi Valkeinen #define INTCTL_G		0x0560
140af9526f2STomi Valkeinen #define INTSTS_G		0x0564
141f25ee501STomi Valkeinen 
142f25ee501STomi Valkeinen #define INT_SYSERR		BIT(16)
143f25ee501STomi Valkeinen #define INT_GPIO_H(x)		(1 << (x == 0 ? 2 : 10))
144f25ee501STomi Valkeinen #define INT_GPIO_LC(x)		(1 << (x == 0 ? 3 : 11))
145f25ee501STomi Valkeinen 
146af9526f2STomi Valkeinen #define INT_GP0_LCNT		0x0584
147af9526f2STomi Valkeinen #define INT_GP1_LCNT		0x0588
1487caff0fcSAndrey Gusakov 
1497caff0fcSAndrey Gusakov /* Control */
1507caff0fcSAndrey Gusakov #define DP0CTL			0x0600
1517caff0fcSAndrey Gusakov #define VID_MN_GEN			BIT(6)   /* Auto-generate M/N values */
1527caff0fcSAndrey Gusakov #define EF_EN				BIT(5)   /* Enable Enhanced Framing */
1537caff0fcSAndrey Gusakov #define VID_EN				BIT(1)   /* Video transmission enable */
1547caff0fcSAndrey Gusakov #define DP_EN				BIT(0)   /* Enable DPTX function */
1557caff0fcSAndrey Gusakov 
1567caff0fcSAndrey Gusakov /* Clocks */
1577caff0fcSAndrey Gusakov #define DP0_VIDMNGEN0		0x0610
1587caff0fcSAndrey Gusakov #define DP0_VIDMNGEN1		0x0614
1597caff0fcSAndrey Gusakov #define DP0_VMNGENSTATUS	0x0618
1607caff0fcSAndrey Gusakov 
1617caff0fcSAndrey Gusakov /* Main Channel */
1627caff0fcSAndrey Gusakov #define DP0_SECSAMPLE		0x0640
1637caff0fcSAndrey Gusakov #define DP0_VIDSYNCDELAY	0x0644
1643f072c30SAndrey Smirnov #define VID_SYNC_DLY		GENMASK(15, 0)
1653f072c30SAndrey Smirnov #define THRESH_DLY		GENMASK(31, 16)
1663f072c30SAndrey Smirnov 
1677caff0fcSAndrey Gusakov #define DP0_TOTALVAL		0x0648
1683f072c30SAndrey Smirnov #define H_TOTAL			GENMASK(15, 0)
1693f072c30SAndrey Smirnov #define V_TOTAL			GENMASK(31, 16)
1707caff0fcSAndrey Gusakov #define DP0_STARTVAL		0x064c
1713f072c30SAndrey Smirnov #define H_START			GENMASK(15, 0)
1723f072c30SAndrey Smirnov #define V_START			GENMASK(31, 16)
1737caff0fcSAndrey Gusakov #define DP0_ACTIVEVAL		0x0650
1743f072c30SAndrey Smirnov #define H_ACT			GENMASK(15, 0)
1753f072c30SAndrey Smirnov #define V_ACT			GENMASK(31, 16)
1763f072c30SAndrey Smirnov 
1777caff0fcSAndrey Gusakov #define DP0_SYNCVAL		0x0654
1783f072c30SAndrey Smirnov #define VS_WIDTH		GENMASK(30, 16)
1793f072c30SAndrey Smirnov #define HS_WIDTH		GENMASK(14, 0)
1807923e09cSTomi Valkeinen #define SYNCVAL_HS_POL_ACTIVE_LOW	(1 << 15)
1817923e09cSTomi Valkeinen #define SYNCVAL_VS_POL_ACTIVE_LOW	(1 << 31)
1827caff0fcSAndrey Gusakov #define DP0_MISC		0x0658
183f3b8adbeSAndrey Gusakov #define TU_SIZE_RECOMMENDED		(63) /* LSCLK cycles per TU */
1843f072c30SAndrey Smirnov #define MAX_TU_SYMBOL		GENMASK(28, 23)
1853f072c30SAndrey Smirnov #define TU_SIZE			GENMASK(21, 16)
1867caff0fcSAndrey Gusakov #define BPC_6				(0 << 5)
1877caff0fcSAndrey Gusakov #define BPC_8				(1 << 5)
1887caff0fcSAndrey Gusakov 
1897caff0fcSAndrey Gusakov /* AUX channel */
1907caff0fcSAndrey Gusakov #define DP0_AUXCFG0		0x0660
191fdb29b73SAndrey Smirnov #define DP0_AUXCFG0_BSIZE	GENMASK(11, 8)
192fdb29b73SAndrey Smirnov #define DP0_AUXCFG0_ADDR_ONLY	BIT(4)
1937caff0fcSAndrey Gusakov #define DP0_AUXCFG1		0x0664
1947caff0fcSAndrey Gusakov #define AUX_RX_FILTER_EN		BIT(16)
1957caff0fcSAndrey Gusakov 
1967caff0fcSAndrey Gusakov #define DP0_AUXADDR		0x0668
1977caff0fcSAndrey Gusakov #define DP0_AUXWDATA(i)		(0x066c + (i) * 4)
1987caff0fcSAndrey Gusakov #define DP0_AUXRDATA(i)		(0x067c + (i) * 4)
1997caff0fcSAndrey Gusakov #define DP0_AUXSTATUS		0x068c
20012dfe7c4SAndrey Smirnov #define AUX_BYTES		GENMASK(15, 8)
20112dfe7c4SAndrey Smirnov #define AUX_STATUS		GENMASK(7, 4)
2027caff0fcSAndrey Gusakov #define AUX_TIMEOUT		BIT(1)
2037caff0fcSAndrey Gusakov #define AUX_BUSY		BIT(0)
2047caff0fcSAndrey Gusakov #define DP0_AUXI2CADR		0x0698
2057caff0fcSAndrey Gusakov 
2067caff0fcSAndrey Gusakov /* Link Training */
2077caff0fcSAndrey Gusakov #define DP0_SRCCTRL		0x06a0
2087caff0fcSAndrey Gusakov #define DP0_SRCCTRL_SCRMBLDIS		BIT(13)
2097caff0fcSAndrey Gusakov #define DP0_SRCCTRL_EN810B		BIT(12)
2107caff0fcSAndrey Gusakov #define DP0_SRCCTRL_NOTP		(0 << 8)
2117caff0fcSAndrey Gusakov #define DP0_SRCCTRL_TP1			(1 << 8)
2127caff0fcSAndrey Gusakov #define DP0_SRCCTRL_TP2			(2 << 8)
2137caff0fcSAndrey Gusakov #define DP0_SRCCTRL_LANESKEW		BIT(7)
2147caff0fcSAndrey Gusakov #define DP0_SRCCTRL_SSCG		BIT(3)
2157caff0fcSAndrey Gusakov #define DP0_SRCCTRL_LANES_1		(0 << 2)
2167caff0fcSAndrey Gusakov #define DP0_SRCCTRL_LANES_2		(1 << 2)
2177caff0fcSAndrey Gusakov #define DP0_SRCCTRL_BW27		(1 << 1)
2187caff0fcSAndrey Gusakov #define DP0_SRCCTRL_BW162		(0 << 1)
2197caff0fcSAndrey Gusakov #define DP0_SRCCTRL_AUTOCORRECT		BIT(0)
2207caff0fcSAndrey Gusakov #define DP0_LTSTAT		0x06d0
2217caff0fcSAndrey Gusakov #define LT_LOOPDONE			BIT(13)
2227caff0fcSAndrey Gusakov #define LT_STATUS_MASK			(0x1f << 8)
2237caff0fcSAndrey Gusakov #define LT_CHANNEL1_EQ_BITS		(DP_CHANNEL_EQ_BITS << 4)
2247caff0fcSAndrey Gusakov #define LT_INTERLANE_ALIGN_DONE		BIT(3)
2257caff0fcSAndrey Gusakov #define LT_CHANNEL0_EQ_BITS		(DP_CHANNEL_EQ_BITS)
2267caff0fcSAndrey Gusakov #define DP0_SNKLTCHGREQ		0x06d4
2277caff0fcSAndrey Gusakov #define DP0_LTLOOPCTRL		0x06d8
2287caff0fcSAndrey Gusakov #define DP0_SNKLTCTRL		0x06e4
2297caff0fcSAndrey Gusakov 
230adf41098STomi Valkeinen #define DP1_SRCCTRL		0x07a0
231adf41098STomi Valkeinen 
2327caff0fcSAndrey Gusakov /* PHY */
2337caff0fcSAndrey Gusakov #define DP_PHY_CTRL		0x0800
2347caff0fcSAndrey Gusakov #define DP_PHY_RST			BIT(28)  /* DP PHY Global Soft Reset */
2357caff0fcSAndrey Gusakov #define BGREN				BIT(25)  /* AUX PHY BGR Enable */
2367caff0fcSAndrey Gusakov #define PWR_SW_EN			BIT(24)  /* PHY Power Switch Enable */
2377caff0fcSAndrey Gusakov #define PHY_M1_RST			BIT(12)  /* Reset PHY1 Main Channel */
2387caff0fcSAndrey Gusakov #define PHY_RDY				BIT(16)  /* PHY Main Channels Ready */
2397caff0fcSAndrey Gusakov #define PHY_M0_RST			BIT(8)   /* Reset PHY0 Main Channel */
240adf41098STomi Valkeinen #define PHY_2LANE			BIT(2)   /* PHY Enable 2 lanes */
2417caff0fcSAndrey Gusakov #define PHY_A0_EN			BIT(1)   /* PHY Aux Channel0 Enable */
2427caff0fcSAndrey Gusakov #define PHY_M0_EN			BIT(0)   /* PHY Main Channel0 Enable */
2437caff0fcSAndrey Gusakov 
2447caff0fcSAndrey Gusakov /* PLL */
2457caff0fcSAndrey Gusakov #define DP0_PLLCTRL		0x0900
2467caff0fcSAndrey Gusakov #define DP1_PLLCTRL		0x0904	/* not defined in DS */
2477caff0fcSAndrey Gusakov #define PXL_PLLCTRL		0x0908
2487caff0fcSAndrey Gusakov #define PLLUPDATE			BIT(2)
2497caff0fcSAndrey Gusakov #define PLLBYP				BIT(1)
2507caff0fcSAndrey Gusakov #define PLLEN				BIT(0)
2517caff0fcSAndrey Gusakov #define PXL_PLLPARAM		0x0914
2527caff0fcSAndrey Gusakov #define IN_SEL_REFCLK			(0 << 14)
2537caff0fcSAndrey Gusakov #define SYS_PLLPARAM		0x0918
2547caff0fcSAndrey Gusakov #define REF_FREQ_38M4			(0 << 8) /* 38.4 MHz */
2557caff0fcSAndrey Gusakov #define REF_FREQ_19M2			(1 << 8) /* 19.2 MHz */
2567caff0fcSAndrey Gusakov #define REF_FREQ_26M			(2 << 8) /* 26 MHz */
2577caff0fcSAndrey Gusakov #define REF_FREQ_13M			(3 << 8) /* 13 MHz */
2587caff0fcSAndrey Gusakov #define SYSCLK_SEL_LSCLK		(0 << 4)
2597caff0fcSAndrey Gusakov #define LSCLK_DIV_1			(0 << 0)
2607caff0fcSAndrey Gusakov #define LSCLK_DIV_2			(1 << 0)
2617caff0fcSAndrey Gusakov 
2627caff0fcSAndrey Gusakov /* Test & Debug */
2637caff0fcSAndrey Gusakov #define TSTCTL			0x0a00
2643f072c30SAndrey Smirnov #define COLOR_R			GENMASK(31, 24)
2653f072c30SAndrey Smirnov #define COLOR_G			GENMASK(23, 16)
2663f072c30SAndrey Smirnov #define COLOR_B			GENMASK(15, 8)
2673f072c30SAndrey Smirnov #define ENI2CFILTER		BIT(4)
2683f072c30SAndrey Smirnov #define COLOR_BAR_MODE		GENMASK(1, 0)
2693f072c30SAndrey Smirnov #define COLOR_BAR_MODE_BARS	2
2707caff0fcSAndrey Gusakov #define PLL_DBG			0x0a04
2717caff0fcSAndrey Gusakov 
2727caff0fcSAndrey Gusakov static bool tc_test_pattern;
2737caff0fcSAndrey Gusakov module_param_named(test, tc_test_pattern, bool, 0644);
2747caff0fcSAndrey Gusakov 
2757caff0fcSAndrey Gusakov struct tc_edp_link {
276e7dc8d40SThierry Reding 	u8			dpcd[DP_RECEIVER_CAP_SIZE];
277e7dc8d40SThierry Reding 	unsigned int		rate;
278e7dc8d40SThierry Reding 	u8			num_lanes;
2797caff0fcSAndrey Gusakov 	u8			assr;
280e5607637STomi Valkeinen 	bool			scrambler_dis;
281e5607637STomi Valkeinen 	bool			spread;
2827caff0fcSAndrey Gusakov };
2837caff0fcSAndrey Gusakov 
2847caff0fcSAndrey Gusakov struct tc_data {
2857caff0fcSAndrey Gusakov 	struct device		*dev;
2867caff0fcSAndrey Gusakov 	struct regmap		*regmap;
2877caff0fcSAndrey Gusakov 	struct drm_dp_aux	aux;
2887caff0fcSAndrey Gusakov 
2897caff0fcSAndrey Gusakov 	struct drm_bridge	bridge;
290de5e6c02SSam Ravnborg 	struct drm_bridge	*panel_bridge;
2917caff0fcSAndrey Gusakov 	struct drm_connector	connector;
2927caff0fcSAndrey Gusakov 
293bbfd3190SMarek Vasut 	struct mipi_dsi_device	*dsi;
294bbfd3190SMarek Vasut 	u8			dsi_lanes;
295bbfd3190SMarek Vasut 
2967caff0fcSAndrey Gusakov 	/* link settings */
2977caff0fcSAndrey Gusakov 	struct tc_edp_link	link;
2987caff0fcSAndrey Gusakov 
2997caff0fcSAndrey Gusakov 	/* current mode */
30046648a3cSTomi Valkeinen 	struct drm_display_mode	mode;
3017caff0fcSAndrey Gusakov 
3027caff0fcSAndrey Gusakov 	u32			rev;
3037caff0fcSAndrey Gusakov 	u8			assr;
3047caff0fcSAndrey Gusakov 
3057caff0fcSAndrey Gusakov 	struct gpio_desc	*sd_gpio;
3067caff0fcSAndrey Gusakov 	struct gpio_desc	*reset_gpio;
3077caff0fcSAndrey Gusakov 	struct clk		*refclk;
308f25ee501STomi Valkeinen 
309f25ee501STomi Valkeinen 	/* do we have IRQ */
310f25ee501STomi Valkeinen 	bool			have_irq;
311f25ee501STomi Valkeinen 
312*3080c21aSMarek Vasut 	/* Input connector type, DSI and not DPI. */
313*3080c21aSMarek Vasut 	bool			input_connector_dsi;
314*3080c21aSMarek Vasut 
315f25ee501STomi Valkeinen 	/* HPD pin number (0 or 1) or -ENODEV */
316f25ee501STomi Valkeinen 	int			hpd_pin;
3177caff0fcSAndrey Gusakov };
3187caff0fcSAndrey Gusakov 
3197caff0fcSAndrey Gusakov static inline struct tc_data *aux_to_tc(struct drm_dp_aux *a)
3207caff0fcSAndrey Gusakov {
3217caff0fcSAndrey Gusakov 	return container_of(a, struct tc_data, aux);
3227caff0fcSAndrey Gusakov }
3237caff0fcSAndrey Gusakov 
3247caff0fcSAndrey Gusakov static inline struct tc_data *bridge_to_tc(struct drm_bridge *b)
3257caff0fcSAndrey Gusakov {
3267caff0fcSAndrey Gusakov 	return container_of(b, struct tc_data, bridge);
3277caff0fcSAndrey Gusakov }
3287caff0fcSAndrey Gusakov 
3297caff0fcSAndrey Gusakov static inline struct tc_data *connector_to_tc(struct drm_connector *c)
3307caff0fcSAndrey Gusakov {
3317caff0fcSAndrey Gusakov 	return container_of(c, struct tc_data, connector);
3327caff0fcSAndrey Gusakov }
3337caff0fcSAndrey Gusakov 
33493a10569SAndrey Smirnov static inline int tc_poll_timeout(struct tc_data *tc, unsigned int addr,
3357caff0fcSAndrey Gusakov 				  unsigned int cond_mask,
3367caff0fcSAndrey Gusakov 				  unsigned int cond_value,
3377caff0fcSAndrey Gusakov 				  unsigned long sleep_us, u64 timeout_us)
3387caff0fcSAndrey Gusakov {
3397caff0fcSAndrey Gusakov 	unsigned int val;
3407caff0fcSAndrey Gusakov 
34193a10569SAndrey Smirnov 	return regmap_read_poll_timeout(tc->regmap, addr, val,
34293a10569SAndrey Smirnov 					(val & cond_mask) == cond_value,
34393a10569SAndrey Smirnov 					sleep_us, timeout_us);
3447caff0fcSAndrey Gusakov }
3457caff0fcSAndrey Gusakov 
34672648926SAndrey Smirnov static int tc_aux_wait_busy(struct tc_data *tc)
3477caff0fcSAndrey Gusakov {
3488a6483acSTomi Valkeinen 	return tc_poll_timeout(tc, DP0_AUXSTATUS, AUX_BUSY, 0, 100, 100000);
3497caff0fcSAndrey Gusakov }
3507caff0fcSAndrey Gusakov 
351792a081aSAndrey Smirnov static int tc_aux_write_data(struct tc_data *tc, const void *data,
352792a081aSAndrey Smirnov 			     size_t size)
353792a081aSAndrey Smirnov {
354792a081aSAndrey Smirnov 	u32 auxwdata[DP_AUX_MAX_PAYLOAD_BYTES / sizeof(u32)] = { 0 };
355792a081aSAndrey Smirnov 	int ret, count = ALIGN(size, sizeof(u32));
356792a081aSAndrey Smirnov 
357792a081aSAndrey Smirnov 	memcpy(auxwdata, data, size);
358792a081aSAndrey Smirnov 
359792a081aSAndrey Smirnov 	ret = regmap_raw_write(tc->regmap, DP0_AUXWDATA(0), auxwdata, count);
360792a081aSAndrey Smirnov 	if (ret)
361792a081aSAndrey Smirnov 		return ret;
362792a081aSAndrey Smirnov 
363792a081aSAndrey Smirnov 	return size;
364792a081aSAndrey Smirnov }
365792a081aSAndrey Smirnov 
36653b166dcSAndrey Smirnov static int tc_aux_read_data(struct tc_data *tc, void *data, size_t size)
36753b166dcSAndrey Smirnov {
36853b166dcSAndrey Smirnov 	u32 auxrdata[DP_AUX_MAX_PAYLOAD_BYTES / sizeof(u32)];
36953b166dcSAndrey Smirnov 	int ret, count = ALIGN(size, sizeof(u32));
37053b166dcSAndrey Smirnov 
37153b166dcSAndrey Smirnov 	ret = regmap_raw_read(tc->regmap, DP0_AUXRDATA(0), auxrdata, count);
37253b166dcSAndrey Smirnov 	if (ret)
37353b166dcSAndrey Smirnov 		return ret;
37453b166dcSAndrey Smirnov 
37553b166dcSAndrey Smirnov 	memcpy(data, auxrdata, size);
37653b166dcSAndrey Smirnov 
37753b166dcSAndrey Smirnov 	return size;
37853b166dcSAndrey Smirnov }
37953b166dcSAndrey Smirnov 
380fdb29b73SAndrey Smirnov static u32 tc_auxcfg0(struct drm_dp_aux_msg *msg, size_t size)
381fdb29b73SAndrey Smirnov {
382fdb29b73SAndrey Smirnov 	u32 auxcfg0 = msg->request;
383fdb29b73SAndrey Smirnov 
384fdb29b73SAndrey Smirnov 	if (size)
385fdb29b73SAndrey Smirnov 		auxcfg0 |= FIELD_PREP(DP0_AUXCFG0_BSIZE, size - 1);
386fdb29b73SAndrey Smirnov 	else
387fdb29b73SAndrey Smirnov 		auxcfg0 |= DP0_AUXCFG0_ADDR_ONLY;
388fdb29b73SAndrey Smirnov 
389fdb29b73SAndrey Smirnov 	return auxcfg0;
390fdb29b73SAndrey Smirnov }
391fdb29b73SAndrey Smirnov 
3927caff0fcSAndrey Gusakov static ssize_t tc_aux_transfer(struct drm_dp_aux *aux,
3937caff0fcSAndrey Gusakov 			       struct drm_dp_aux_msg *msg)
3947caff0fcSAndrey Gusakov {
3957caff0fcSAndrey Gusakov 	struct tc_data *tc = aux_to_tc(aux);
396e0655feaSAndrey Smirnov 	size_t size = min_t(size_t, DP_AUX_MAX_PAYLOAD_BYTES - 1, msg->size);
3977caff0fcSAndrey Gusakov 	u8 request = msg->request & ~DP_AUX_I2C_MOT;
39812dfe7c4SAndrey Smirnov 	u32 auxstatus;
3997caff0fcSAndrey Gusakov 	int ret;
4007caff0fcSAndrey Gusakov 
40172648926SAndrey Smirnov 	ret = tc_aux_wait_busy(tc);
4027caff0fcSAndrey Gusakov 	if (ret)
4036d0c3831SAndrey Smirnov 		return ret;
4047caff0fcSAndrey Gusakov 
405792a081aSAndrey Smirnov 	switch (request) {
406792a081aSAndrey Smirnov 	case DP_AUX_NATIVE_READ:
407792a081aSAndrey Smirnov 	case DP_AUX_I2C_READ:
408792a081aSAndrey Smirnov 		break;
409792a081aSAndrey Smirnov 	case DP_AUX_NATIVE_WRITE:
410792a081aSAndrey Smirnov 	case DP_AUX_I2C_WRITE:
411fdb29b73SAndrey Smirnov 		if (size) {
412792a081aSAndrey Smirnov 			ret = tc_aux_write_data(tc, msg->buffer, size);
413792a081aSAndrey Smirnov 			if (ret < 0)
4146d0c3831SAndrey Smirnov 				return ret;
415fdb29b73SAndrey Smirnov 		}
416792a081aSAndrey Smirnov 		break;
417792a081aSAndrey Smirnov 	default:
4187caff0fcSAndrey Gusakov 		return -EINVAL;
4197caff0fcSAndrey Gusakov 	}
4207caff0fcSAndrey Gusakov 
4217caff0fcSAndrey Gusakov 	/* Store address */
4226d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_AUXADDR, msg->address);
4236d0c3831SAndrey Smirnov 	if (ret)
4246d0c3831SAndrey Smirnov 		return ret;
4257caff0fcSAndrey Gusakov 	/* Start transfer */
426fdb29b73SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_AUXCFG0, tc_auxcfg0(msg, size));
4276d0c3831SAndrey Smirnov 	if (ret)
4286d0c3831SAndrey Smirnov 		return ret;
4297caff0fcSAndrey Gusakov 
43072648926SAndrey Smirnov 	ret = tc_aux_wait_busy(tc);
4317caff0fcSAndrey Gusakov 	if (ret)
4326d0c3831SAndrey Smirnov 		return ret;
4337caff0fcSAndrey Gusakov 
43412dfe7c4SAndrey Smirnov 	ret = regmap_read(tc->regmap, DP0_AUXSTATUS, &auxstatus);
4357caff0fcSAndrey Gusakov 	if (ret)
4366d0c3831SAndrey Smirnov 		return ret;
4377caff0fcSAndrey Gusakov 
43812dfe7c4SAndrey Smirnov 	if (auxstatus & AUX_TIMEOUT)
43912dfe7c4SAndrey Smirnov 		return -ETIMEDOUT;
440fdb29b73SAndrey Smirnov 	/*
441fdb29b73SAndrey Smirnov 	 * For some reason address-only DP_AUX_I2C_WRITE (MOT), still
442fdb29b73SAndrey Smirnov 	 * reports 1 byte transferred in its status. To deal we that
443fdb29b73SAndrey Smirnov 	 * we ignore aux_bytes field if we know that this was an
444fdb29b73SAndrey Smirnov 	 * address-only transfer
445fdb29b73SAndrey Smirnov 	 */
446fdb29b73SAndrey Smirnov 	if (size)
44712dfe7c4SAndrey Smirnov 		size = FIELD_GET(AUX_BYTES, auxstatus);
44812dfe7c4SAndrey Smirnov 	msg->reply = FIELD_GET(AUX_STATUS, auxstatus);
44912dfe7c4SAndrey Smirnov 
45053b166dcSAndrey Smirnov 	switch (request) {
45153b166dcSAndrey Smirnov 	case DP_AUX_NATIVE_READ:
45253b166dcSAndrey Smirnov 	case DP_AUX_I2C_READ:
453fdb29b73SAndrey Smirnov 		if (size)
45453b166dcSAndrey Smirnov 			return tc_aux_read_data(tc, msg->buffer, size);
455fdb29b73SAndrey Smirnov 		break;
4567caff0fcSAndrey Gusakov 	}
4577caff0fcSAndrey Gusakov 
4587caff0fcSAndrey Gusakov 	return size;
4597caff0fcSAndrey Gusakov }
4607caff0fcSAndrey Gusakov 
4617caff0fcSAndrey Gusakov static const char * const training_pattern1_errors[] = {
4627caff0fcSAndrey Gusakov 	"No errors",
4637caff0fcSAndrey Gusakov 	"Aux write error",
4647caff0fcSAndrey Gusakov 	"Aux read error",
4657caff0fcSAndrey Gusakov 	"Max voltage reached error",
4667caff0fcSAndrey Gusakov 	"Loop counter expired error",
4677caff0fcSAndrey Gusakov 	"res", "res", "res"
4687caff0fcSAndrey Gusakov };
4697caff0fcSAndrey Gusakov 
4707caff0fcSAndrey Gusakov static const char * const training_pattern2_errors[] = {
4717caff0fcSAndrey Gusakov 	"No errors",
4727caff0fcSAndrey Gusakov 	"Aux write error",
4737caff0fcSAndrey Gusakov 	"Aux read error",
4747caff0fcSAndrey Gusakov 	"Clock recovery failed error",
4757caff0fcSAndrey Gusakov 	"Loop counter expired error",
4767caff0fcSAndrey Gusakov 	"res", "res", "res"
4777caff0fcSAndrey Gusakov };
4787caff0fcSAndrey Gusakov 
4797caff0fcSAndrey Gusakov static u32 tc_srcctrl(struct tc_data *tc)
4807caff0fcSAndrey Gusakov {
4817caff0fcSAndrey Gusakov 	/*
4827caff0fcSAndrey Gusakov 	 * No training pattern, skew lane 1 data by two LSCLK cycles with
4837caff0fcSAndrey Gusakov 	 * respect to lane 0 data, AutoCorrect Mode = 0
4847caff0fcSAndrey Gusakov 	 */
4854b30bf41STomi Valkeinen 	u32 reg = DP0_SRCCTRL_NOTP | DP0_SRCCTRL_LANESKEW | DP0_SRCCTRL_EN810B;
4867caff0fcSAndrey Gusakov 
4877caff0fcSAndrey Gusakov 	if (tc->link.scrambler_dis)
4887caff0fcSAndrey Gusakov 		reg |= DP0_SRCCTRL_SCRMBLDIS;	/* Scrambler Disabled */
4897caff0fcSAndrey Gusakov 	if (tc->link.spread)
4907caff0fcSAndrey Gusakov 		reg |= DP0_SRCCTRL_SSCG;	/* Spread Spectrum Enable */
491e7dc8d40SThierry Reding 	if (tc->link.num_lanes == 2)
4927caff0fcSAndrey Gusakov 		reg |= DP0_SRCCTRL_LANES_2;	/* Two Main Channel Lanes */
493e7dc8d40SThierry Reding 	if (tc->link.rate != 162000)
4947caff0fcSAndrey Gusakov 		reg |= DP0_SRCCTRL_BW27;	/* 2.7 Gbps link */
4957caff0fcSAndrey Gusakov 	return reg;
4967caff0fcSAndrey Gusakov }
4977caff0fcSAndrey Gusakov 
498134fb306SAndrey Smirnov static int tc_pllupdate(struct tc_data *tc, unsigned int pllctrl)
4997caff0fcSAndrey Gusakov {
500134fb306SAndrey Smirnov 	int ret;
501134fb306SAndrey Smirnov 
502134fb306SAndrey Smirnov 	ret = regmap_write(tc->regmap, pllctrl, PLLUPDATE | PLLEN);
503134fb306SAndrey Smirnov 	if (ret)
504134fb306SAndrey Smirnov 		return ret;
505134fb306SAndrey Smirnov 
5067caff0fcSAndrey Gusakov 	/* Wait for PLL to lock: up to 2.09 ms, depending on refclk */
5077caff0fcSAndrey Gusakov 	usleep_range(3000, 6000);
508134fb306SAndrey Smirnov 
509134fb306SAndrey Smirnov 	return 0;
5107caff0fcSAndrey Gusakov }
5117caff0fcSAndrey Gusakov 
5127caff0fcSAndrey Gusakov static int tc_pxl_pll_en(struct tc_data *tc, u32 refclk, u32 pixelclock)
5137caff0fcSAndrey Gusakov {
5147caff0fcSAndrey Gusakov 	int ret;
5157caff0fcSAndrey Gusakov 	int i_pre, best_pre = 1;
5167caff0fcSAndrey Gusakov 	int i_post, best_post = 1;
5177caff0fcSAndrey Gusakov 	int div, best_div = 1;
5187caff0fcSAndrey Gusakov 	int mul, best_mul = 1;
5197caff0fcSAndrey Gusakov 	int delta, best_delta;
5207caff0fcSAndrey Gusakov 	int ext_div[] = {1, 2, 3, 5, 7};
521bbfd3190SMarek Vasut 	int clk_min, clk_max;
5227caff0fcSAndrey Gusakov 	int best_pixelclock = 0;
5237caff0fcSAndrey Gusakov 	int vco_hi = 0;
5246d0c3831SAndrey Smirnov 	u32 pxl_pllparam;
5257caff0fcSAndrey Gusakov 
526bbfd3190SMarek Vasut 	/*
527bbfd3190SMarek Vasut 	 * refclk * mul / (ext_pre_div * pre_div) should be in range:
528bbfd3190SMarek Vasut 	 * - DPI ..... 0 to 100 MHz
529bbfd3190SMarek Vasut 	 * - (e)DP ... 150 to 650 MHz
530bbfd3190SMarek Vasut 	 */
531bbfd3190SMarek Vasut 	if (tc->bridge.type == DRM_MODE_CONNECTOR_DPI) {
532bbfd3190SMarek Vasut 		clk_min = 0;
533bbfd3190SMarek Vasut 		clk_max = 100000000;
534bbfd3190SMarek Vasut 	} else {
535bbfd3190SMarek Vasut 		clk_min = 150000000;
536bbfd3190SMarek Vasut 		clk_max = 650000000;
537bbfd3190SMarek Vasut 	}
538bbfd3190SMarek Vasut 
5397caff0fcSAndrey Gusakov 	dev_dbg(tc->dev, "PLL: requested %d pixelclock, ref %d\n", pixelclock,
5407caff0fcSAndrey Gusakov 		refclk);
5417caff0fcSAndrey Gusakov 	best_delta = pixelclock;
5427caff0fcSAndrey Gusakov 	/* Loop over all possible ext_divs, skipping invalid configurations */
5437caff0fcSAndrey Gusakov 	for (i_pre = 0; i_pre < ARRAY_SIZE(ext_div); i_pre++) {
5447caff0fcSAndrey Gusakov 		/*
5457caff0fcSAndrey Gusakov 		 * refclk / ext_pre_div should be in the 1 to 200 MHz range.
5467caff0fcSAndrey Gusakov 		 * We don't allow any refclk > 200 MHz, only check lower bounds.
5477caff0fcSAndrey Gusakov 		 */
5487caff0fcSAndrey Gusakov 		if (refclk / ext_div[i_pre] < 1000000)
5497caff0fcSAndrey Gusakov 			continue;
5507caff0fcSAndrey Gusakov 		for (i_post = 0; i_post < ARRAY_SIZE(ext_div); i_post++) {
5517caff0fcSAndrey Gusakov 			for (div = 1; div <= 16; div++) {
5527caff0fcSAndrey Gusakov 				u32 clk;
5537caff0fcSAndrey Gusakov 				u64 tmp;
5547caff0fcSAndrey Gusakov 
5557caff0fcSAndrey Gusakov 				tmp = pixelclock * ext_div[i_pre] *
5567caff0fcSAndrey Gusakov 				      ext_div[i_post] * div;
5577caff0fcSAndrey Gusakov 				do_div(tmp, refclk);
5587caff0fcSAndrey Gusakov 				mul = tmp;
5597caff0fcSAndrey Gusakov 
5607caff0fcSAndrey Gusakov 				/* Check limits */
5617caff0fcSAndrey Gusakov 				if ((mul < 1) || (mul > 128))
5627caff0fcSAndrey Gusakov 					continue;
5637caff0fcSAndrey Gusakov 
5647caff0fcSAndrey Gusakov 				clk = (refclk / ext_div[i_pre] / div) * mul;
565bbfd3190SMarek Vasut 				if ((clk > clk_max) || (clk < clk_min))
5667caff0fcSAndrey Gusakov 					continue;
5677caff0fcSAndrey Gusakov 
5687caff0fcSAndrey Gusakov 				clk = clk / ext_div[i_post];
5697caff0fcSAndrey Gusakov 				delta = clk - pixelclock;
5707caff0fcSAndrey Gusakov 
5717caff0fcSAndrey Gusakov 				if (abs(delta) < abs(best_delta)) {
5727caff0fcSAndrey Gusakov 					best_pre = i_pre;
5737caff0fcSAndrey Gusakov 					best_post = i_post;
5747caff0fcSAndrey Gusakov 					best_div = div;
5757caff0fcSAndrey Gusakov 					best_mul = mul;
5767caff0fcSAndrey Gusakov 					best_delta = delta;
5777caff0fcSAndrey Gusakov 					best_pixelclock = clk;
5787caff0fcSAndrey Gusakov 				}
5797caff0fcSAndrey Gusakov 			}
5807caff0fcSAndrey Gusakov 		}
5817caff0fcSAndrey Gusakov 	}
5827caff0fcSAndrey Gusakov 	if (best_pixelclock == 0) {
5837caff0fcSAndrey Gusakov 		dev_err(tc->dev, "Failed to calc clock for %d pixelclock\n",
5847caff0fcSAndrey Gusakov 			pixelclock);
5857caff0fcSAndrey Gusakov 		return -EINVAL;
5867caff0fcSAndrey Gusakov 	}
5877caff0fcSAndrey Gusakov 
5887caff0fcSAndrey Gusakov 	dev_dbg(tc->dev, "PLL: got %d, delta %d\n", best_pixelclock,
5897caff0fcSAndrey Gusakov 		best_delta);
5907caff0fcSAndrey Gusakov 	dev_dbg(tc->dev, "PLL: %d / %d / %d * %d / %d\n", refclk,
5917caff0fcSAndrey Gusakov 		ext_div[best_pre], best_div, best_mul, ext_div[best_post]);
5927caff0fcSAndrey Gusakov 
5937caff0fcSAndrey Gusakov 	/* if VCO >= 300 MHz */
5947caff0fcSAndrey Gusakov 	if (refclk / ext_div[best_pre] / best_div * best_mul >= 300000000)
5957caff0fcSAndrey Gusakov 		vco_hi = 1;
5967caff0fcSAndrey Gusakov 	/* see DS */
5977caff0fcSAndrey Gusakov 	if (best_div == 16)
5987caff0fcSAndrey Gusakov 		best_div = 0;
5997caff0fcSAndrey Gusakov 	if (best_mul == 128)
6007caff0fcSAndrey Gusakov 		best_mul = 0;
6017caff0fcSAndrey Gusakov 
6027caff0fcSAndrey Gusakov 	/* Power up PLL and switch to bypass */
6036d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, PXL_PLLCTRL, PLLBYP | PLLEN);
6046d0c3831SAndrey Smirnov 	if (ret)
6056d0c3831SAndrey Smirnov 		return ret;
6067caff0fcSAndrey Gusakov 
6076d0c3831SAndrey Smirnov 	pxl_pllparam  = vco_hi << 24; /* For PLL VCO >= 300 MHz = 1 */
6086d0c3831SAndrey Smirnov 	pxl_pllparam |= ext_div[best_pre] << 20; /* External Pre-divider */
6096d0c3831SAndrey Smirnov 	pxl_pllparam |= ext_div[best_post] << 16; /* External Post-divider */
6106d0c3831SAndrey Smirnov 	pxl_pllparam |= IN_SEL_REFCLK; /* Use RefClk as PLL input */
6116d0c3831SAndrey Smirnov 	pxl_pllparam |= best_div << 8; /* Divider for PLL RefClk */
6126d0c3831SAndrey Smirnov 	pxl_pllparam |= best_mul; /* Multiplier for PLL */
6136d0c3831SAndrey Smirnov 
6146d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, PXL_PLLPARAM, pxl_pllparam);
6156d0c3831SAndrey Smirnov 	if (ret)
6166d0c3831SAndrey Smirnov 		return ret;
6177caff0fcSAndrey Gusakov 
6187caff0fcSAndrey Gusakov 	/* Force PLL parameter update and disable bypass */
619134fb306SAndrey Smirnov 	return tc_pllupdate(tc, PXL_PLLCTRL);
6207caff0fcSAndrey Gusakov }
6217caff0fcSAndrey Gusakov 
6227caff0fcSAndrey Gusakov static int tc_pxl_pll_dis(struct tc_data *tc)
6237caff0fcSAndrey Gusakov {
6247caff0fcSAndrey Gusakov 	/* Enable PLL bypass, power down PLL */
6257caff0fcSAndrey Gusakov 	return regmap_write(tc->regmap, PXL_PLLCTRL, PLLBYP);
6267caff0fcSAndrey Gusakov }
6277caff0fcSAndrey Gusakov 
6287caff0fcSAndrey Gusakov static int tc_stream_clock_calc(struct tc_data *tc)
6297caff0fcSAndrey Gusakov {
6307caff0fcSAndrey Gusakov 	/*
6317caff0fcSAndrey Gusakov 	 * If the Stream clock and Link Symbol clock are
6327caff0fcSAndrey Gusakov 	 * asynchronous with each other, the value of M changes over
6337caff0fcSAndrey Gusakov 	 * time. This way of generating link clock and stream
6347caff0fcSAndrey Gusakov 	 * clock is called Asynchronous Clock mode. The value M
6357caff0fcSAndrey Gusakov 	 * must change while the value N stays constant. The
6367caff0fcSAndrey Gusakov 	 * value of N in this Asynchronous Clock mode must be set
6377caff0fcSAndrey Gusakov 	 * to 2^15 or 32,768.
6387caff0fcSAndrey Gusakov 	 *
6397caff0fcSAndrey Gusakov 	 * LSCLK = 1/10 of high speed link clock
6407caff0fcSAndrey Gusakov 	 *
6417caff0fcSAndrey Gusakov 	 * f_STRMCLK = M/N * f_LSCLK
6427caff0fcSAndrey Gusakov 	 * M/N = f_STRMCLK / f_LSCLK
6437caff0fcSAndrey Gusakov 	 *
6447caff0fcSAndrey Gusakov 	 */
6456d0c3831SAndrey Smirnov 	return regmap_write(tc->regmap, DP0_VIDMNGEN1, 32768);
6467caff0fcSAndrey Gusakov }
6477caff0fcSAndrey Gusakov 
648c49f60dfSAndrey Smirnov static int tc_set_syspllparam(struct tc_data *tc)
6497caff0fcSAndrey Gusakov {
6507caff0fcSAndrey Gusakov 	unsigned long rate;
651c49f60dfSAndrey Smirnov 	u32 pllparam = SYSCLK_SEL_LSCLK | LSCLK_DIV_2;
6527caff0fcSAndrey Gusakov 
6537caff0fcSAndrey Gusakov 	rate = clk_get_rate(tc->refclk);
6547caff0fcSAndrey Gusakov 	switch (rate) {
6557caff0fcSAndrey Gusakov 	case 38400000:
656c49f60dfSAndrey Smirnov 		pllparam |= REF_FREQ_38M4;
6577caff0fcSAndrey Gusakov 		break;
6587caff0fcSAndrey Gusakov 	case 26000000:
659c49f60dfSAndrey Smirnov 		pllparam |= REF_FREQ_26M;
6607caff0fcSAndrey Gusakov 		break;
6617caff0fcSAndrey Gusakov 	case 19200000:
662c49f60dfSAndrey Smirnov 		pllparam |= REF_FREQ_19M2;
6637caff0fcSAndrey Gusakov 		break;
6647caff0fcSAndrey Gusakov 	case 13000000:
665c49f60dfSAndrey Smirnov 		pllparam |= REF_FREQ_13M;
6667caff0fcSAndrey Gusakov 		break;
6677caff0fcSAndrey Gusakov 	default:
6687caff0fcSAndrey Gusakov 		dev_err(tc->dev, "Invalid refclk rate: %lu Hz\n", rate);
6697caff0fcSAndrey Gusakov 		return -EINVAL;
6707caff0fcSAndrey Gusakov 	}
6717caff0fcSAndrey Gusakov 
672c49f60dfSAndrey Smirnov 	return regmap_write(tc->regmap, SYS_PLLPARAM, pllparam);
673c49f60dfSAndrey Smirnov }
674c49f60dfSAndrey Smirnov 
675c49f60dfSAndrey Smirnov static int tc_aux_link_setup(struct tc_data *tc)
676c49f60dfSAndrey Smirnov {
677c49f60dfSAndrey Smirnov 	int ret;
678c49f60dfSAndrey Smirnov 	u32 dp0_auxcfg1;
679c49f60dfSAndrey Smirnov 
6807caff0fcSAndrey Gusakov 	/* Setup DP-PHY / PLL */
681c49f60dfSAndrey Smirnov 	ret = tc_set_syspllparam(tc);
6826d0c3831SAndrey Smirnov 	if (ret)
6836d0c3831SAndrey Smirnov 		goto err;
6847caff0fcSAndrey Gusakov 
6856d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP_PHY_CTRL,
6866d0c3831SAndrey Smirnov 			   BGREN | PWR_SW_EN | PHY_A0_EN);
6876d0c3831SAndrey Smirnov 	if (ret)
6886d0c3831SAndrey Smirnov 		goto err;
6897caff0fcSAndrey Gusakov 	/*
6907caff0fcSAndrey Gusakov 	 * Initially PLLs are in bypass. Force PLL parameter update,
6917caff0fcSAndrey Gusakov 	 * disable PLL bypass, enable PLL
6927caff0fcSAndrey Gusakov 	 */
693134fb306SAndrey Smirnov 	ret = tc_pllupdate(tc, DP0_PLLCTRL);
6946d0c3831SAndrey Smirnov 	if (ret)
6956d0c3831SAndrey Smirnov 		goto err;
6967caff0fcSAndrey Gusakov 
697134fb306SAndrey Smirnov 	ret = tc_pllupdate(tc, DP1_PLLCTRL);
6986d0c3831SAndrey Smirnov 	if (ret)
6996d0c3831SAndrey Smirnov 		goto err;
7007caff0fcSAndrey Gusakov 
7018a6483acSTomi Valkeinen 	ret = tc_poll_timeout(tc, DP_PHY_CTRL, PHY_RDY, PHY_RDY, 100, 100000);
7027caff0fcSAndrey Gusakov 	if (ret == -ETIMEDOUT) {
7037caff0fcSAndrey Gusakov 		dev_err(tc->dev, "Timeout waiting for PHY to become ready");
7047caff0fcSAndrey Gusakov 		return ret;
705ca342386STomi Valkeinen 	} else if (ret) {
7067caff0fcSAndrey Gusakov 		goto err;
707ca342386STomi Valkeinen 	}
7087caff0fcSAndrey Gusakov 
7097caff0fcSAndrey Gusakov 	/* Setup AUX link */
7106d0c3831SAndrey Smirnov 	dp0_auxcfg1  = AUX_RX_FILTER_EN;
7116d0c3831SAndrey Smirnov 	dp0_auxcfg1 |= 0x06 << 8; /* Aux Bit Period Calculator Threshold */
7126d0c3831SAndrey Smirnov 	dp0_auxcfg1 |= 0x3f << 0; /* Aux Response Timeout Timer */
7136d0c3831SAndrey Smirnov 
7146d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_AUXCFG1, dp0_auxcfg1);
7156d0c3831SAndrey Smirnov 	if (ret)
7166d0c3831SAndrey Smirnov 		goto err;
7177caff0fcSAndrey Gusakov 
718824c7bb4SMarek Vasut 	/* Register DP AUX channel */
719824c7bb4SMarek Vasut 	tc->aux.name = "TC358767 AUX i2c adapter";
720824c7bb4SMarek Vasut 	tc->aux.dev = tc->dev;
721824c7bb4SMarek Vasut 	tc->aux.transfer = tc_aux_transfer;
722824c7bb4SMarek Vasut 	drm_dp_aux_init(&tc->aux);
723824c7bb4SMarek Vasut 
7247caff0fcSAndrey Gusakov 	return 0;
7257caff0fcSAndrey Gusakov err:
7267caff0fcSAndrey Gusakov 	dev_err(tc->dev, "tc_aux_link_setup failed: %d\n", ret);
7277caff0fcSAndrey Gusakov 	return ret;
7287caff0fcSAndrey Gusakov }
7297caff0fcSAndrey Gusakov 
7307caff0fcSAndrey Gusakov static int tc_get_display_props(struct tc_data *tc)
7317caff0fcSAndrey Gusakov {
732e7dc8d40SThierry Reding 	u8 revision, num_lanes;
733e7dc8d40SThierry Reding 	unsigned int rate;
7347caff0fcSAndrey Gusakov 	int ret;
735d174db07SAndrey Smirnov 	u8 reg;
7367caff0fcSAndrey Gusakov 
7377caff0fcSAndrey Gusakov 	/* Read DP Rx Link Capability */
738e7dc8d40SThierry Reding 	ret = drm_dp_dpcd_read(&tc->aux, DP_DPCD_REV, tc->link.dpcd,
739e7dc8d40SThierry Reding 			       DP_RECEIVER_CAP_SIZE);
7407caff0fcSAndrey Gusakov 	if (ret < 0)
7417caff0fcSAndrey Gusakov 		goto err_dpcd_read;
742e7dc8d40SThierry Reding 
743e7dc8d40SThierry Reding 	revision = tc->link.dpcd[DP_DPCD_REV];
744e7dc8d40SThierry Reding 	rate = drm_dp_max_link_rate(tc->link.dpcd);
745e7dc8d40SThierry Reding 	num_lanes = drm_dp_max_lane_count(tc->link.dpcd);
746e7dc8d40SThierry Reding 
747e7dc8d40SThierry Reding 	if (rate != 162000 && rate != 270000) {
748cffd2b16SAndrey Gusakov 		dev_dbg(tc->dev, "Falling to 2.7 Gbps rate\n");
749e7dc8d40SThierry Reding 		rate = 270000;
750cffd2b16SAndrey Gusakov 	}
751cffd2b16SAndrey Gusakov 
752e7dc8d40SThierry Reding 	tc->link.rate = rate;
753e7dc8d40SThierry Reding 
754e7dc8d40SThierry Reding 	if (num_lanes > 2) {
755cffd2b16SAndrey Gusakov 		dev_dbg(tc->dev, "Falling to 2 lanes\n");
756e7dc8d40SThierry Reding 		num_lanes = 2;
757cffd2b16SAndrey Gusakov 	}
7587caff0fcSAndrey Gusakov 
759e7dc8d40SThierry Reding 	tc->link.num_lanes = num_lanes;
760e7dc8d40SThierry Reding 
761d174db07SAndrey Smirnov 	ret = drm_dp_dpcd_readb(&tc->aux, DP_MAX_DOWNSPREAD, &reg);
7627caff0fcSAndrey Gusakov 	if (ret < 0)
7637caff0fcSAndrey Gusakov 		goto err_dpcd_read;
764d174db07SAndrey Smirnov 	tc->link.spread = reg & DP_MAX_DOWNSPREAD_0_5;
7657caff0fcSAndrey Gusakov 
766d174db07SAndrey Smirnov 	ret = drm_dp_dpcd_readb(&tc->aux, DP_MAIN_LINK_CHANNEL_CODING, &reg);
7677caff0fcSAndrey Gusakov 	if (ret < 0)
7687caff0fcSAndrey Gusakov 		goto err_dpcd_read;
7694b30bf41STomi Valkeinen 
770e5607637STomi Valkeinen 	tc->link.scrambler_dis = false;
7717caff0fcSAndrey Gusakov 	/* read assr */
772d174db07SAndrey Smirnov 	ret = drm_dp_dpcd_readb(&tc->aux, DP_EDP_CONFIGURATION_SET, &reg);
7737caff0fcSAndrey Gusakov 	if (ret < 0)
7747caff0fcSAndrey Gusakov 		goto err_dpcd_read;
775d174db07SAndrey Smirnov 	tc->link.assr = reg & DP_ALTERNATE_SCRAMBLER_RESET_ENABLE;
7767caff0fcSAndrey Gusakov 
7777caff0fcSAndrey Gusakov 	dev_dbg(tc->dev, "DPCD rev: %d.%d, rate: %s, lanes: %d, framing: %s\n",
778e7dc8d40SThierry Reding 		revision >> 4, revision & 0x0f,
779e7dc8d40SThierry Reding 		(tc->link.rate == 162000) ? "1.62Gbps" : "2.7Gbps",
780e7dc8d40SThierry Reding 		tc->link.num_lanes,
781e7dc8d40SThierry Reding 		drm_dp_enhanced_frame_cap(tc->link.dpcd) ?
78298bca69bSThierry Reding 		"enhanced" : "default");
783e5607637STomi Valkeinen 	dev_dbg(tc->dev, "Downspread: %s, scrambler: %s\n",
784e5607637STomi Valkeinen 		tc->link.spread ? "0.5%" : "0.0%",
785e5607637STomi Valkeinen 		tc->link.scrambler_dis ? "disabled" : "enabled");
7867caff0fcSAndrey Gusakov 	dev_dbg(tc->dev, "Display ASSR: %d, TC358767 ASSR: %d\n",
7877caff0fcSAndrey Gusakov 		tc->link.assr, tc->assr);
7887caff0fcSAndrey Gusakov 
7897caff0fcSAndrey Gusakov 	return 0;
7907caff0fcSAndrey Gusakov 
7917caff0fcSAndrey Gusakov err_dpcd_read:
7927caff0fcSAndrey Gusakov 	dev_err(tc->dev, "failed to read DPCD: %d\n", ret);
7937caff0fcSAndrey Gusakov 	return ret;
7947caff0fcSAndrey Gusakov }
7957caff0fcSAndrey Gusakov 
796aebe58a7SMarek Vasut static int tc_set_common_video_mode(struct tc_data *tc,
79763f8f3baSLaurent Pinchart 				    const struct drm_display_mode *mode)
7987caff0fcSAndrey Gusakov {
7997caff0fcSAndrey Gusakov 	int left_margin = mode->htotal - mode->hsync_end;
8007caff0fcSAndrey Gusakov 	int right_margin = mode->hsync_start - mode->hdisplay;
8017caff0fcSAndrey Gusakov 	int hsync_len = mode->hsync_end - mode->hsync_start;
8027caff0fcSAndrey Gusakov 	int upper_margin = mode->vtotal - mode->vsync_end;
8037caff0fcSAndrey Gusakov 	int lower_margin = mode->vsync_start - mode->vdisplay;
8047caff0fcSAndrey Gusakov 	int vsync_len = mode->vsync_end - mode->vsync_start;
805aebe58a7SMarek Vasut 	int ret;
80666d1c3b9SAndrey Gusakov 
8077caff0fcSAndrey Gusakov 	dev_dbg(tc->dev, "set mode %dx%d\n",
8087caff0fcSAndrey Gusakov 		mode->hdisplay, mode->vdisplay);
8097caff0fcSAndrey Gusakov 	dev_dbg(tc->dev, "H margin %d,%d sync %d\n",
8107caff0fcSAndrey Gusakov 		left_margin, right_margin, hsync_len);
8117caff0fcSAndrey Gusakov 	dev_dbg(tc->dev, "V margin %d,%d sync %d\n",
8127caff0fcSAndrey Gusakov 		upper_margin, lower_margin, vsync_len);
8137caff0fcSAndrey Gusakov 	dev_dbg(tc->dev, "total: %dx%d\n", mode->htotal, mode->vtotal);
8147caff0fcSAndrey Gusakov 
8157caff0fcSAndrey Gusakov 
81666d1c3b9SAndrey Gusakov 	/*
81766d1c3b9SAndrey Gusakov 	 * LCD Ctl Frame Size
81866d1c3b9SAndrey Gusakov 	 * datasheet is not clear of vsdelay in case of DPI
81966d1c3b9SAndrey Gusakov 	 * assume we do not need any delay when DPI is a source of
82066d1c3b9SAndrey Gusakov 	 * sync signals
82166d1c3b9SAndrey Gusakov 	 */
8226d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, VPCTRL0,
8233f072c30SAndrey Smirnov 			   FIELD_PREP(VSDELAY, 0) |
8247caff0fcSAndrey Gusakov 			   OPXLFMT_RGB888 | FRMSYNC_DISABLED | MSF_DISABLED);
8256d0c3831SAndrey Smirnov 	if (ret)
8266d0c3831SAndrey Smirnov 		return ret;
8276d0c3831SAndrey Smirnov 
8286d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, HTIM01,
8293f072c30SAndrey Smirnov 			   FIELD_PREP(HBPR, ALIGN(left_margin, 2)) |
8303f072c30SAndrey Smirnov 			   FIELD_PREP(HPW, ALIGN(hsync_len, 2)));
8316d0c3831SAndrey Smirnov 	if (ret)
8326d0c3831SAndrey Smirnov 		return ret;
8336d0c3831SAndrey Smirnov 
8346d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, HTIM02,
8353f072c30SAndrey Smirnov 			   FIELD_PREP(HDISPR, ALIGN(mode->hdisplay, 2)) |
8363f072c30SAndrey Smirnov 			   FIELD_PREP(HFPR, ALIGN(right_margin, 2)));
8376d0c3831SAndrey Smirnov 	if (ret)
8386d0c3831SAndrey Smirnov 		return ret;
8396d0c3831SAndrey Smirnov 
8406d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, VTIM01,
8413f072c30SAndrey Smirnov 			   FIELD_PREP(VBPR, upper_margin) |
8423f072c30SAndrey Smirnov 			   FIELD_PREP(VSPR, vsync_len));
8436d0c3831SAndrey Smirnov 	if (ret)
8446d0c3831SAndrey Smirnov 		return ret;
8456d0c3831SAndrey Smirnov 
8466d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, VTIM02,
8473f072c30SAndrey Smirnov 			   FIELD_PREP(VFPR, lower_margin) |
8483f072c30SAndrey Smirnov 			   FIELD_PREP(VDISPR, mode->vdisplay));
8496d0c3831SAndrey Smirnov 	if (ret)
8506d0c3831SAndrey Smirnov 		return ret;
8516d0c3831SAndrey Smirnov 
8526d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, VFUEN0, VFUEN); /* update settings */
8536d0c3831SAndrey Smirnov 	if (ret)
8546d0c3831SAndrey Smirnov 		return ret;
8557caff0fcSAndrey Gusakov 
8567caff0fcSAndrey Gusakov 	/* Test pattern settings */
8576d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, TSTCTL,
8583f072c30SAndrey Smirnov 			   FIELD_PREP(COLOR_R, 120) |
8593f072c30SAndrey Smirnov 			   FIELD_PREP(COLOR_G, 20) |
8603f072c30SAndrey Smirnov 			   FIELD_PREP(COLOR_B, 99) |
8613f072c30SAndrey Smirnov 			   ENI2CFILTER |
8623f072c30SAndrey Smirnov 			   FIELD_PREP(COLOR_BAR_MODE, COLOR_BAR_MODE_BARS));
863aebe58a7SMarek Vasut 
8646d0c3831SAndrey Smirnov 	return ret;
865aebe58a7SMarek Vasut }
866aebe58a7SMarek Vasut 
867bbfd3190SMarek Vasut static int tc_set_dpi_video_mode(struct tc_data *tc,
868bbfd3190SMarek Vasut 				 const struct drm_display_mode *mode)
869bbfd3190SMarek Vasut {
870bbfd3190SMarek Vasut 	u32 value = POCTRL_S2P;
871bbfd3190SMarek Vasut 
872bbfd3190SMarek Vasut 	if (tc->mode.flags & DRM_MODE_FLAG_NHSYNC)
873bbfd3190SMarek Vasut 		value |= POCTRL_HS_POL;
874bbfd3190SMarek Vasut 
875bbfd3190SMarek Vasut 	if (tc->mode.flags & DRM_MODE_FLAG_NVSYNC)
876bbfd3190SMarek Vasut 		value |= POCTRL_VS_POL;
877bbfd3190SMarek Vasut 
878bbfd3190SMarek Vasut 	return regmap_write(tc->regmap, POCTRL, value);
879bbfd3190SMarek Vasut }
880bbfd3190SMarek Vasut 
881aebe58a7SMarek Vasut static int tc_set_edp_video_mode(struct tc_data *tc,
882aebe58a7SMarek Vasut 				 const struct drm_display_mode *mode)
883aebe58a7SMarek Vasut {
884aebe58a7SMarek Vasut 	int ret;
885aebe58a7SMarek Vasut 	int vid_sync_dly;
886aebe58a7SMarek Vasut 	int max_tu_symbol;
887aebe58a7SMarek Vasut 
888aebe58a7SMarek Vasut 	int left_margin = mode->htotal - mode->hsync_end;
889aebe58a7SMarek Vasut 	int hsync_len = mode->hsync_end - mode->hsync_start;
890aebe58a7SMarek Vasut 	int upper_margin = mode->vtotal - mode->vsync_end;
891aebe58a7SMarek Vasut 	int vsync_len = mode->vsync_end - mode->vsync_start;
892aebe58a7SMarek Vasut 	u32 dp0_syncval;
893aebe58a7SMarek Vasut 	u32 bits_per_pixel = 24;
894aebe58a7SMarek Vasut 	u32 in_bw, out_bw;
895aebe58a7SMarek Vasut 
896aebe58a7SMarek Vasut 	/*
897aebe58a7SMarek Vasut 	 * Recommended maximum number of symbols transferred in a transfer unit:
898aebe58a7SMarek Vasut 	 * DIV_ROUND_UP((input active video bandwidth in bytes) * tu_size,
899aebe58a7SMarek Vasut 	 *              (output active video bandwidth in bytes))
900aebe58a7SMarek Vasut 	 * Must be less than tu_size.
901aebe58a7SMarek Vasut 	 */
902aebe58a7SMarek Vasut 
903aebe58a7SMarek Vasut 	in_bw = mode->clock * bits_per_pixel / 8;
904aebe58a7SMarek Vasut 	out_bw = tc->link.num_lanes * tc->link.rate;
905aebe58a7SMarek Vasut 	max_tu_symbol = DIV_ROUND_UP(in_bw * TU_SIZE_RECOMMENDED, out_bw);
9067caff0fcSAndrey Gusakov 
9077caff0fcSAndrey Gusakov 	/* DP Main Stream Attributes */
9087caff0fcSAndrey Gusakov 	vid_sync_dly = hsync_len + left_margin + mode->hdisplay;
9096d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_VIDSYNCDELAY,
9103f072c30SAndrey Smirnov 		 FIELD_PREP(THRESH_DLY, max_tu_symbol) |
9113f072c30SAndrey Smirnov 		 FIELD_PREP(VID_SYNC_DLY, vid_sync_dly));
9127caff0fcSAndrey Gusakov 
9136d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_TOTALVAL,
9143f072c30SAndrey Smirnov 			   FIELD_PREP(H_TOTAL, mode->htotal) |
9153f072c30SAndrey Smirnov 			   FIELD_PREP(V_TOTAL, mode->vtotal));
9166d0c3831SAndrey Smirnov 	if (ret)
9176d0c3831SAndrey Smirnov 		return ret;
9187caff0fcSAndrey Gusakov 
9196d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_STARTVAL,
9203f072c30SAndrey Smirnov 			   FIELD_PREP(H_START, left_margin + hsync_len) |
9213f072c30SAndrey Smirnov 			   FIELD_PREP(V_START, upper_margin + vsync_len));
9226d0c3831SAndrey Smirnov 	if (ret)
9236d0c3831SAndrey Smirnov 		return ret;
9247caff0fcSAndrey Gusakov 
9256d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_ACTIVEVAL,
9263f072c30SAndrey Smirnov 			   FIELD_PREP(V_ACT, mode->vdisplay) |
9273f072c30SAndrey Smirnov 			   FIELD_PREP(H_ACT, mode->hdisplay));
9286d0c3831SAndrey Smirnov 	if (ret)
9296d0c3831SAndrey Smirnov 		return ret;
9307caff0fcSAndrey Gusakov 
9313f072c30SAndrey Smirnov 	dp0_syncval = FIELD_PREP(VS_WIDTH, vsync_len) |
9323f072c30SAndrey Smirnov 		      FIELD_PREP(HS_WIDTH, hsync_len);
9337caff0fcSAndrey Gusakov 
9343f072c30SAndrey Smirnov 	if (mode->flags & DRM_MODE_FLAG_NVSYNC)
9353f072c30SAndrey Smirnov 		dp0_syncval |= SYNCVAL_VS_POL_ACTIVE_LOW;
9367caff0fcSAndrey Gusakov 
9373f072c30SAndrey Smirnov 	if (mode->flags & DRM_MODE_FLAG_NHSYNC)
9383f072c30SAndrey Smirnov 		dp0_syncval |= SYNCVAL_HS_POL_ACTIVE_LOW;
9393f072c30SAndrey Smirnov 
9406d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_SYNCVAL, dp0_syncval);
9416d0c3831SAndrey Smirnov 	if (ret)
9426d0c3831SAndrey Smirnov 		return ret;
9433f072c30SAndrey Smirnov 
9446d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DPIPXLFMT,
9453f072c30SAndrey Smirnov 			   VS_POL_ACTIVE_LOW | HS_POL_ACTIVE_LOW |
9463f072c30SAndrey Smirnov 			   DE_POL_ACTIVE_HIGH | SUB_CFG_TYPE_CONFIG1 |
9473f072c30SAndrey Smirnov 			   DPI_BPP_RGB888);
9486d0c3831SAndrey Smirnov 	if (ret)
9496d0c3831SAndrey Smirnov 		return ret;
9503f072c30SAndrey Smirnov 
9516d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_MISC,
9523f072c30SAndrey Smirnov 			   FIELD_PREP(MAX_TU_SYMBOL, max_tu_symbol) |
9533f072c30SAndrey Smirnov 			   FIELD_PREP(TU_SIZE, TU_SIZE_RECOMMENDED) |
954f3b8adbeSAndrey Gusakov 			   BPC_8);
9556d0c3831SAndrey Smirnov 	return ret;
9567caff0fcSAndrey Gusakov }
9577caff0fcSAndrey Gusakov 
958f9538357STomi Valkeinen static int tc_wait_link_training(struct tc_data *tc)
9597caff0fcSAndrey Gusakov {
9607caff0fcSAndrey Gusakov 	u32 value;
9617caff0fcSAndrey Gusakov 	int ret;
9627caff0fcSAndrey Gusakov 
963aa92213fSAndrey Smirnov 	ret = tc_poll_timeout(tc, DP0_LTSTAT, LT_LOOPDONE,
9648a6483acSTomi Valkeinen 			      LT_LOOPDONE, 500, 100000);
965aa92213fSAndrey Smirnov 	if (ret) {
966f9538357STomi Valkeinen 		dev_err(tc->dev, "Link training timeout waiting for LT_LOOPDONE!\n");
967aa92213fSAndrey Smirnov 		return ret;
9687caff0fcSAndrey Gusakov 	}
9697caff0fcSAndrey Gusakov 
9706d0c3831SAndrey Smirnov 	ret = regmap_read(tc->regmap, DP0_LTSTAT, &value);
9716d0c3831SAndrey Smirnov 	if (ret)
9726d0c3831SAndrey Smirnov 		return ret;
973f9538357STomi Valkeinen 
974aa92213fSAndrey Smirnov 	return (value >> 8) & 0x7;
9757caff0fcSAndrey Gusakov }
9767caff0fcSAndrey Gusakov 
977cb3263b2STomi Valkeinen static int tc_main_link_enable(struct tc_data *tc)
9787caff0fcSAndrey Gusakov {
9797caff0fcSAndrey Gusakov 	struct drm_dp_aux *aux = &tc->aux;
9807caff0fcSAndrey Gusakov 	struct device *dev = tc->dev;
9817caff0fcSAndrey Gusakov 	u32 dp_phy_ctrl;
9827caff0fcSAndrey Gusakov 	u32 value;
9837caff0fcSAndrey Gusakov 	int ret;
98432d36219SAndrey Smirnov 	u8 tmp[DP_LINK_STATUS_SIZE];
9857caff0fcSAndrey Gusakov 
986cb3263b2STomi Valkeinen 	dev_dbg(tc->dev, "link enable\n");
987cb3263b2STomi Valkeinen 
9886d0c3831SAndrey Smirnov 	ret = regmap_read(tc->regmap, DP0CTL, &value);
9896d0c3831SAndrey Smirnov 	if (ret)
9906d0c3831SAndrey Smirnov 		return ret;
99167bca92fSTomi Valkeinen 
9926d0c3831SAndrey Smirnov 	if (WARN_ON(value & DP_EN)) {
9936d0c3831SAndrey Smirnov 		ret = regmap_write(tc->regmap, DP0CTL, 0);
9946d0c3831SAndrey Smirnov 		if (ret)
9956d0c3831SAndrey Smirnov 			return ret;
9966d0c3831SAndrey Smirnov 	}
9976d0c3831SAndrey Smirnov 
9986d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_SRCCTRL, tc_srcctrl(tc));
9996d0c3831SAndrey Smirnov 	if (ret)
10006d0c3831SAndrey Smirnov 		return ret;
10019a63bd6fSTomi Valkeinen 	/* SSCG and BW27 on DP1 must be set to the same as on DP0 */
10026d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP1_SRCCTRL,
10039a63bd6fSTomi Valkeinen 		 (tc->link.spread ? DP0_SRCCTRL_SSCG : 0) |
1004e7dc8d40SThierry Reding 		 ((tc->link.rate != 162000) ? DP0_SRCCTRL_BW27 : 0));
10056d0c3831SAndrey Smirnov 	if (ret)
10066d0c3831SAndrey Smirnov 		return ret;
10077caff0fcSAndrey Gusakov 
1008c49f60dfSAndrey Smirnov 	ret = tc_set_syspllparam(tc);
10096d0c3831SAndrey Smirnov 	if (ret)
10106d0c3831SAndrey Smirnov 		return ret;
1011adf41098STomi Valkeinen 
10127caff0fcSAndrey Gusakov 	/* Setup Main Link */
10134d9d54a7STomi Valkeinen 	dp_phy_ctrl = BGREN | PWR_SW_EN | PHY_A0_EN | PHY_M0_EN;
1014e7dc8d40SThierry Reding 	if (tc->link.num_lanes == 2)
10154d9d54a7STomi Valkeinen 		dp_phy_ctrl |= PHY_2LANE;
10166d0c3831SAndrey Smirnov 
10176d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP_PHY_CTRL, dp_phy_ctrl);
10186d0c3831SAndrey Smirnov 	if (ret)
10196d0c3831SAndrey Smirnov 		return ret;
10207caff0fcSAndrey Gusakov 
10217caff0fcSAndrey Gusakov 	/* PLL setup */
1022134fb306SAndrey Smirnov 	ret = tc_pllupdate(tc, DP0_PLLCTRL);
10236d0c3831SAndrey Smirnov 	if (ret)
10246d0c3831SAndrey Smirnov 		return ret;
10257caff0fcSAndrey Gusakov 
1026134fb306SAndrey Smirnov 	ret = tc_pllupdate(tc, DP1_PLLCTRL);
10276d0c3831SAndrey Smirnov 	if (ret)
10286d0c3831SAndrey Smirnov 		return ret;
10297caff0fcSAndrey Gusakov 
10307caff0fcSAndrey Gusakov 	/* Reset/Enable Main Links */
10317caff0fcSAndrey Gusakov 	dp_phy_ctrl |= DP_PHY_RST | PHY_M1_RST | PHY_M0_RST;
10326d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP_PHY_CTRL, dp_phy_ctrl);
10337caff0fcSAndrey Gusakov 	usleep_range(100, 200);
10347caff0fcSAndrey Gusakov 	dp_phy_ctrl &= ~(DP_PHY_RST | PHY_M1_RST | PHY_M0_RST);
10356d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP_PHY_CTRL, dp_phy_ctrl);
10367caff0fcSAndrey Gusakov 
10378a6483acSTomi Valkeinen 	ret = tc_poll_timeout(tc, DP_PHY_CTRL, PHY_RDY, PHY_RDY, 500, 100000);
1038ebcce4e6SAndrey Smirnov 	if (ret) {
10397caff0fcSAndrey Gusakov 		dev_err(dev, "timeout waiting for phy become ready");
1040ebcce4e6SAndrey Smirnov 		return ret;
10417caff0fcSAndrey Gusakov 	}
10427caff0fcSAndrey Gusakov 
10437caff0fcSAndrey Gusakov 	/* Set misc: 8 bits per color */
10447caff0fcSAndrey Gusakov 	ret = regmap_update_bits(tc->regmap, DP0_MISC, BPC_8, BPC_8);
10457caff0fcSAndrey Gusakov 	if (ret)
10466d0c3831SAndrey Smirnov 		return ret;
10477caff0fcSAndrey Gusakov 
10487caff0fcSAndrey Gusakov 	/*
10497caff0fcSAndrey Gusakov 	 * ASSR mode
10507caff0fcSAndrey Gusakov 	 * on TC358767 side ASSR configured through strap pin
10517caff0fcSAndrey Gusakov 	 * seems there is no way to change this setting from SW
10527caff0fcSAndrey Gusakov 	 *
10537caff0fcSAndrey Gusakov 	 * check is tc configured for same mode
10547caff0fcSAndrey Gusakov 	 */
10557caff0fcSAndrey Gusakov 	if (tc->assr != tc->link.assr) {
10567caff0fcSAndrey Gusakov 		dev_dbg(dev, "Trying to set display to ASSR: %d\n",
10577caff0fcSAndrey Gusakov 			tc->assr);
10587caff0fcSAndrey Gusakov 		/* try to set ASSR on display side */
10597caff0fcSAndrey Gusakov 		tmp[0] = tc->assr;
10607caff0fcSAndrey Gusakov 		ret = drm_dp_dpcd_writeb(aux, DP_EDP_CONFIGURATION_SET, tmp[0]);
10617caff0fcSAndrey Gusakov 		if (ret < 0)
10627caff0fcSAndrey Gusakov 			goto err_dpcd_read;
10637caff0fcSAndrey Gusakov 		/* read back */
10647caff0fcSAndrey Gusakov 		ret = drm_dp_dpcd_readb(aux, DP_EDP_CONFIGURATION_SET, tmp);
10657caff0fcSAndrey Gusakov 		if (ret < 0)
10667caff0fcSAndrey Gusakov 			goto err_dpcd_read;
10677caff0fcSAndrey Gusakov 
10687caff0fcSAndrey Gusakov 		if (tmp[0] != tc->assr) {
106987291e5dSLucas Stach 			dev_dbg(dev, "Failed to switch display ASSR to %d, falling back to unscrambled mode\n",
10707caff0fcSAndrey Gusakov 				tc->assr);
10717caff0fcSAndrey Gusakov 			/* trying with disabled scrambler */
1072e5607637STomi Valkeinen 			tc->link.scrambler_dis = true;
10737caff0fcSAndrey Gusakov 		}
10747caff0fcSAndrey Gusakov 	}
10757caff0fcSAndrey Gusakov 
10767caff0fcSAndrey Gusakov 	/* Setup Link & DPRx Config for Training */
1077e7dc8d40SThierry Reding 	tmp[0] = drm_dp_link_rate_to_bw_code(tc->link.rate);
1078e7dc8d40SThierry Reding 	tmp[1] = tc->link.num_lanes;
1079e7dc8d40SThierry Reding 
1080e7dc8d40SThierry Reding 	if (drm_dp_enhanced_frame_cap(tc->link.dpcd))
1081e7dc8d40SThierry Reding 		tmp[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
1082e7dc8d40SThierry Reding 
1083e7dc8d40SThierry Reding 	ret = drm_dp_dpcd_write(aux, DP_LINK_BW_SET, tmp, 2);
10847caff0fcSAndrey Gusakov 	if (ret < 0)
10857caff0fcSAndrey Gusakov 		goto err_dpcd_write;
10867caff0fcSAndrey Gusakov 
10877caff0fcSAndrey Gusakov 	/* DOWNSPREAD_CTRL */
10887caff0fcSAndrey Gusakov 	tmp[0] = tc->link.spread ? DP_SPREAD_AMP_0_5 : 0x00;
10897caff0fcSAndrey Gusakov 	/* MAIN_LINK_CHANNEL_CODING_SET */
10904b30bf41STomi Valkeinen 	tmp[1] =  DP_SET_ANSI_8B10B;
10917caff0fcSAndrey Gusakov 	ret = drm_dp_dpcd_write(aux, DP_DOWNSPREAD_CTRL, tmp, 2);
10927caff0fcSAndrey Gusakov 	if (ret < 0)
10937caff0fcSAndrey Gusakov 		goto err_dpcd_write;
10947caff0fcSAndrey Gusakov 
1095c28d1484STomi Valkeinen 	/* Reset voltage-swing & pre-emphasis */
1096c28d1484STomi Valkeinen 	tmp[0] = tmp[1] = DP_TRAIN_VOLTAGE_SWING_LEVEL_0 |
1097c28d1484STomi Valkeinen 			  DP_TRAIN_PRE_EMPH_LEVEL_0;
1098c28d1484STomi Valkeinen 	ret = drm_dp_dpcd_write(aux, DP_TRAINING_LANE0_SET, tmp, 2);
1099c28d1484STomi Valkeinen 	if (ret < 0)
1100c28d1484STomi Valkeinen 		goto err_dpcd_write;
1101c28d1484STomi Valkeinen 
1102f9538357STomi Valkeinen 	/* Clock-Recovery */
1103f9538357STomi Valkeinen 
1104f9538357STomi Valkeinen 	/* Set DPCD 0x102 for Training Pattern 1 */
11056d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_SNKLTCTRL,
11066d0c3831SAndrey Smirnov 			   DP_LINK_SCRAMBLING_DISABLE |
1107f9538357STomi Valkeinen 			   DP_TRAINING_PATTERN_1);
11086d0c3831SAndrey Smirnov 	if (ret)
11096d0c3831SAndrey Smirnov 		return ret;
1110f9538357STomi Valkeinen 
11116d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_LTLOOPCTRL,
1112f9538357STomi Valkeinen 			   (15 << 28) |	/* Defer Iteration Count */
1113f9538357STomi Valkeinen 			   (15 << 24) |	/* Loop Iteration Count */
1114f9538357STomi Valkeinen 			   (0xd << 0));	/* Loop Timer Delay */
11156d0c3831SAndrey Smirnov 	if (ret)
11166d0c3831SAndrey Smirnov 		return ret;
1117f9538357STomi Valkeinen 
11186d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_SRCCTRL,
11196d0c3831SAndrey Smirnov 			   tc_srcctrl(tc) | DP0_SRCCTRL_SCRMBLDIS |
11206d0c3831SAndrey Smirnov 			   DP0_SRCCTRL_AUTOCORRECT |
11216d0c3831SAndrey Smirnov 			   DP0_SRCCTRL_TP1);
11226d0c3831SAndrey Smirnov 	if (ret)
11236d0c3831SAndrey Smirnov 		return ret;
1124f9538357STomi Valkeinen 
1125f9538357STomi Valkeinen 	/* Enable DP0 to start Link Training */
11266d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0CTL,
1127e7dc8d40SThierry Reding 			   (drm_dp_enhanced_frame_cap(tc->link.dpcd) ?
1128e7dc8d40SThierry Reding 				EF_EN : 0) | DP_EN);
11296d0c3831SAndrey Smirnov 	if (ret)
11306d0c3831SAndrey Smirnov 		return ret;
1131f9538357STomi Valkeinen 
1132f9538357STomi Valkeinen 	/* wait */
11336d0c3831SAndrey Smirnov 
1134f9538357STomi Valkeinen 	ret = tc_wait_link_training(tc);
1135f9538357STomi Valkeinen 	if (ret < 0)
11366d0c3831SAndrey Smirnov 		return ret;
11377caff0fcSAndrey Gusakov 
1138f9538357STomi Valkeinen 	if (ret) {
1139f9538357STomi Valkeinen 		dev_err(tc->dev, "Link training phase 1 failed: %s\n",
1140f9538357STomi Valkeinen 			training_pattern1_errors[ret]);
11416d0c3831SAndrey Smirnov 		return -ENODEV;
1142f9538357STomi Valkeinen 	}
1143f9538357STomi Valkeinen 
1144f9538357STomi Valkeinen 	/* Channel Equalization */
1145f9538357STomi Valkeinen 
1146f9538357STomi Valkeinen 	/* Set DPCD 0x102 for Training Pattern 2 */
11476d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_SNKLTCTRL,
11486d0c3831SAndrey Smirnov 			   DP_LINK_SCRAMBLING_DISABLE |
1149f9538357STomi Valkeinen 			   DP_TRAINING_PATTERN_2);
11506d0c3831SAndrey Smirnov 	if (ret)
11516d0c3831SAndrey Smirnov 		return ret;
1152f9538357STomi Valkeinen 
11536d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_SRCCTRL,
11546d0c3831SAndrey Smirnov 			   tc_srcctrl(tc) | DP0_SRCCTRL_SCRMBLDIS |
11556d0c3831SAndrey Smirnov 			   DP0_SRCCTRL_AUTOCORRECT |
11566d0c3831SAndrey Smirnov 			   DP0_SRCCTRL_TP2);
11576d0c3831SAndrey Smirnov 	if (ret)
11586d0c3831SAndrey Smirnov 		return ret;
1159f9538357STomi Valkeinen 
1160f9538357STomi Valkeinen 	/* wait */
1161f9538357STomi Valkeinen 	ret = tc_wait_link_training(tc);
1162f9538357STomi Valkeinen 	if (ret < 0)
11636d0c3831SAndrey Smirnov 		return ret;
1164f9538357STomi Valkeinen 
1165f9538357STomi Valkeinen 	if (ret) {
1166f9538357STomi Valkeinen 		dev_err(tc->dev, "Link training phase 2 failed: %s\n",
1167f9538357STomi Valkeinen 			training_pattern2_errors[ret]);
11686d0c3831SAndrey Smirnov 		return -ENODEV;
1169f9538357STomi Valkeinen 	}
11707caff0fcSAndrey Gusakov 
11710776a269STomi Valkeinen 	/*
11720776a269STomi Valkeinen 	 * Toshiba's documentation suggests to first clear DPCD 0x102, then
11730776a269STomi Valkeinen 	 * clear the training pattern bit in DP0_SRCCTRL. Testing shows
11740776a269STomi Valkeinen 	 * that the link sometimes drops if those steps are done in that order,
11750776a269STomi Valkeinen 	 * but if the steps are done in reverse order, the link stays up.
11760776a269STomi Valkeinen 	 *
11770776a269STomi Valkeinen 	 * So we do the steps differently than documented here.
11780776a269STomi Valkeinen 	 */
11790776a269STomi Valkeinen 
11800776a269STomi Valkeinen 	/* Clear Training Pattern, set AutoCorrect Mode = 1 */
11816d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_SRCCTRL, tc_srcctrl(tc) |
11826d0c3831SAndrey Smirnov 			   DP0_SRCCTRL_AUTOCORRECT);
11836d0c3831SAndrey Smirnov 	if (ret)
11846d0c3831SAndrey Smirnov 		return ret;
11850776a269STomi Valkeinen 
11867caff0fcSAndrey Gusakov 	/* Clear DPCD 0x102 */
11877caff0fcSAndrey Gusakov 	/* Note: Can Not use DP0_SNKLTCTRL (0x06E4) short cut */
11887caff0fcSAndrey Gusakov 	tmp[0] = tc->link.scrambler_dis ? DP_LINK_SCRAMBLING_DISABLE : 0x00;
11897caff0fcSAndrey Gusakov 	ret = drm_dp_dpcd_writeb(aux, DP_TRAINING_PATTERN_SET, tmp[0]);
11907caff0fcSAndrey Gusakov 	if (ret < 0)
11917caff0fcSAndrey Gusakov 		goto err_dpcd_write;
11927caff0fcSAndrey Gusakov 
11930bf25146STomi Valkeinen 	/* Check link status */
11940bf25146STomi Valkeinen 	ret = drm_dp_dpcd_read_link_status(aux, tmp);
11957caff0fcSAndrey Gusakov 	if (ret < 0)
11967caff0fcSAndrey Gusakov 		goto err_dpcd_read;
11977caff0fcSAndrey Gusakov 
11980bf25146STomi Valkeinen 	ret = 0;
11997caff0fcSAndrey Gusakov 
12000bf25146STomi Valkeinen 	value = tmp[0] & DP_CHANNEL_EQ_BITS;
12010bf25146STomi Valkeinen 
12020bf25146STomi Valkeinen 	if (value != DP_CHANNEL_EQ_BITS) {
12030bf25146STomi Valkeinen 		dev_err(tc->dev, "Lane 0 failed: %x\n", value);
12040bf25146STomi Valkeinen 		ret = -ENODEV;
12050bf25146STomi Valkeinen 	}
12060bf25146STomi Valkeinen 
1207e7dc8d40SThierry Reding 	if (tc->link.num_lanes == 2) {
12080bf25146STomi Valkeinen 		value = (tmp[0] >> 4) & DP_CHANNEL_EQ_BITS;
12090bf25146STomi Valkeinen 
12100bf25146STomi Valkeinen 		if (value != DP_CHANNEL_EQ_BITS) {
12110bf25146STomi Valkeinen 			dev_err(tc->dev, "Lane 1 failed: %x\n", value);
12120bf25146STomi Valkeinen 			ret = -ENODEV;
12130bf25146STomi Valkeinen 		}
12140bf25146STomi Valkeinen 
12150bf25146STomi Valkeinen 		if (!(tmp[2] & DP_INTERLANE_ALIGN_DONE)) {
12160bf25146STomi Valkeinen 			dev_err(tc->dev, "Interlane align failed\n");
12170bf25146STomi Valkeinen 			ret = -ENODEV;
12180bf25146STomi Valkeinen 		}
12190bf25146STomi Valkeinen 	}
12200bf25146STomi Valkeinen 
12210bf25146STomi Valkeinen 	if (ret) {
12220bf25146STomi Valkeinen 		dev_err(dev, "0x0202 LANE0_1_STATUS:            0x%02x\n", tmp[0]);
12230bf25146STomi Valkeinen 		dev_err(dev, "0x0203 LANE2_3_STATUS             0x%02x\n", tmp[1]);
12240bf25146STomi Valkeinen 		dev_err(dev, "0x0204 LANE_ALIGN_STATUS_UPDATED: 0x%02x\n", tmp[2]);
12250bf25146STomi Valkeinen 		dev_err(dev, "0x0205 SINK_STATUS:               0x%02x\n", tmp[3]);
12260bf25146STomi Valkeinen 		dev_err(dev, "0x0206 ADJUST_REQUEST_LANE0_1:    0x%02x\n", tmp[4]);
12270bf25146STomi Valkeinen 		dev_err(dev, "0x0207 ADJUST_REQUEST_LANE2_3:    0x%02x\n", tmp[5]);
12286d0c3831SAndrey Smirnov 		return ret;
12297caff0fcSAndrey Gusakov 	}
12307caff0fcSAndrey Gusakov 
12317caff0fcSAndrey Gusakov 	return 0;
12327caff0fcSAndrey Gusakov err_dpcd_read:
12337caff0fcSAndrey Gusakov 	dev_err(tc->dev, "Failed to read DPCD: %d\n", ret);
12347caff0fcSAndrey Gusakov 	return ret;
12357caff0fcSAndrey Gusakov err_dpcd_write:
12367caff0fcSAndrey Gusakov 	dev_err(tc->dev, "Failed to write DPCD: %d\n", ret);
12377caff0fcSAndrey Gusakov 	return ret;
12387caff0fcSAndrey Gusakov }
12397caff0fcSAndrey Gusakov 
1240cb3263b2STomi Valkeinen static int tc_main_link_disable(struct tc_data *tc)
1241cb3263b2STomi Valkeinen {
1242cb3263b2STomi Valkeinen 	int ret;
1243cb3263b2STomi Valkeinen 
1244cb3263b2STomi Valkeinen 	dev_dbg(tc->dev, "link disable\n");
1245cb3263b2STomi Valkeinen 
12466d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_SRCCTRL, 0);
12476d0c3831SAndrey Smirnov 	if (ret)
1248cb3263b2STomi Valkeinen 		return ret;
12496d0c3831SAndrey Smirnov 
12506d0c3831SAndrey Smirnov 	return regmap_write(tc->regmap, DP0CTL, 0);
1251cb3263b2STomi Valkeinen }
1252cb3263b2STomi Valkeinen 
1253d7fd32ecSMarek Vasut static int tc_dsi_rx_enable(struct tc_data *tc)
1254d7fd32ecSMarek Vasut {
1255d7fd32ecSMarek Vasut 	u32 value;
1256d7fd32ecSMarek Vasut 	int ret;
1257d7fd32ecSMarek Vasut 
1258d7fd32ecSMarek Vasut 	regmap_write(tc->regmap, PPI_D0S_CLRSIPOCOUNT, 3);
1259d7fd32ecSMarek Vasut 	regmap_write(tc->regmap, PPI_D1S_CLRSIPOCOUNT, 3);
1260d7fd32ecSMarek Vasut 	regmap_write(tc->regmap, PPI_D2S_CLRSIPOCOUNT, 3);
1261d7fd32ecSMarek Vasut 	regmap_write(tc->regmap, PPI_D3S_CLRSIPOCOUNT, 3);
1262d7fd32ecSMarek Vasut 	regmap_write(tc->regmap, PPI_D0S_ATMR, 0);
1263d7fd32ecSMarek Vasut 	regmap_write(tc->regmap, PPI_D1S_ATMR, 0);
1264d7fd32ecSMarek Vasut 	regmap_write(tc->regmap, PPI_TX_RX_TA, TTA_GET | TTA_SURE);
1265d7fd32ecSMarek Vasut 	regmap_write(tc->regmap, PPI_LPTXTIMECNT, LPX_PERIOD);
1266d7fd32ecSMarek Vasut 
1267d7fd32ecSMarek Vasut 	value = ((LANEENABLE_L0EN << tc->dsi_lanes) - LANEENABLE_L0EN) |
1268d7fd32ecSMarek Vasut 		LANEENABLE_CLEN;
1269d7fd32ecSMarek Vasut 	regmap_write(tc->regmap, PPI_LANEENABLE, value);
1270d7fd32ecSMarek Vasut 	regmap_write(tc->regmap, DSI_LANEENABLE, value);
1271d7fd32ecSMarek Vasut 
1272d7fd32ecSMarek Vasut 	/* Set input interface */
1273d7fd32ecSMarek Vasut 	value = DP0_AUDSRC_NO_INPUT;
1274d7fd32ecSMarek Vasut 	if (tc_test_pattern)
1275d7fd32ecSMarek Vasut 		value |= DP0_VIDSRC_COLOR_BAR;
1276d7fd32ecSMarek Vasut 	else
1277d7fd32ecSMarek Vasut 		value |= DP0_VIDSRC_DSI_RX;
1278d7fd32ecSMarek Vasut 	ret = regmap_write(tc->regmap, SYSCTRL, value);
1279d7fd32ecSMarek Vasut 	if (ret)
1280d7fd32ecSMarek Vasut 		return ret;
1281d7fd32ecSMarek Vasut 
1282d7fd32ecSMarek Vasut 	usleep_range(120, 150);
1283d7fd32ecSMarek Vasut 
1284d7fd32ecSMarek Vasut 	regmap_write(tc->regmap, PPI_STARTPPI, PPI_START_FUNCTION);
1285d7fd32ecSMarek Vasut 	regmap_write(tc->regmap, DSI_STARTDSI, DSI_RX_START);
1286d7fd32ecSMarek Vasut 
1287d7fd32ecSMarek Vasut 	return 0;
1288d7fd32ecSMarek Vasut }
1289d7fd32ecSMarek Vasut 
1290d7fd32ecSMarek Vasut static int tc_dpi_rx_enable(struct tc_data *tc)
1291d7fd32ecSMarek Vasut {
1292d7fd32ecSMarek Vasut 	u32 value;
1293d7fd32ecSMarek Vasut 
1294d7fd32ecSMarek Vasut 	/* Set input interface */
1295d7fd32ecSMarek Vasut 	value = DP0_AUDSRC_NO_INPUT;
1296d7fd32ecSMarek Vasut 	if (tc_test_pattern)
1297d7fd32ecSMarek Vasut 		value |= DP0_VIDSRC_COLOR_BAR;
1298d7fd32ecSMarek Vasut 	else
1299d7fd32ecSMarek Vasut 		value |= DP0_VIDSRC_DPI_RX;
1300d7fd32ecSMarek Vasut 	return regmap_write(tc->regmap, SYSCTRL, value);
1301d7fd32ecSMarek Vasut }
1302d7fd32ecSMarek Vasut 
1303bbfd3190SMarek Vasut static int tc_dpi_stream_enable(struct tc_data *tc)
1304bbfd3190SMarek Vasut {
1305bbfd3190SMarek Vasut 	int ret;
1306bbfd3190SMarek Vasut 
1307bbfd3190SMarek Vasut 	dev_dbg(tc->dev, "enable video stream\n");
1308bbfd3190SMarek Vasut 
1309bbfd3190SMarek Vasut 	/* Setup PLL */
1310bbfd3190SMarek Vasut 	ret = tc_set_syspllparam(tc);
1311bbfd3190SMarek Vasut 	if (ret)
1312bbfd3190SMarek Vasut 		return ret;
1313bbfd3190SMarek Vasut 
1314bbfd3190SMarek Vasut 	/*
1315bbfd3190SMarek Vasut 	 * Initially PLLs are in bypass. Force PLL parameter update,
1316bbfd3190SMarek Vasut 	 * disable PLL bypass, enable PLL
1317bbfd3190SMarek Vasut 	 */
1318bbfd3190SMarek Vasut 	ret = tc_pllupdate(tc, DP0_PLLCTRL);
1319bbfd3190SMarek Vasut 	if (ret)
1320bbfd3190SMarek Vasut 		return ret;
1321bbfd3190SMarek Vasut 
1322bbfd3190SMarek Vasut 	ret = tc_pllupdate(tc, DP1_PLLCTRL);
1323bbfd3190SMarek Vasut 	if (ret)
1324bbfd3190SMarek Vasut 		return ret;
1325bbfd3190SMarek Vasut 
1326bbfd3190SMarek Vasut 	/* Pixel PLL must always be enabled for DPI mode */
1327bbfd3190SMarek Vasut 	ret = tc_pxl_pll_en(tc, clk_get_rate(tc->refclk),
1328bbfd3190SMarek Vasut 			    1000 * tc->mode.clock);
1329bbfd3190SMarek Vasut 	if (ret)
1330bbfd3190SMarek Vasut 		return ret;
1331bbfd3190SMarek Vasut 
1332bbfd3190SMarek Vasut 	ret = tc_set_common_video_mode(tc, &tc->mode);
1333bbfd3190SMarek Vasut 	if (ret)
1334bbfd3190SMarek Vasut 		return ret;
1335bbfd3190SMarek Vasut 
1336bbfd3190SMarek Vasut 	ret = tc_set_dpi_video_mode(tc, &tc->mode);
1337bbfd3190SMarek Vasut 	if (ret)
1338bbfd3190SMarek Vasut 		return ret;
1339bbfd3190SMarek Vasut 
1340d7fd32ecSMarek Vasut 	return tc_dsi_rx_enable(tc);
1341bbfd3190SMarek Vasut }
1342bbfd3190SMarek Vasut 
1343bbfd3190SMarek Vasut static int tc_dpi_stream_disable(struct tc_data *tc)
1344bbfd3190SMarek Vasut {
1345bbfd3190SMarek Vasut 	dev_dbg(tc->dev, "disable video stream\n");
1346bbfd3190SMarek Vasut 
1347bbfd3190SMarek Vasut 	tc_pxl_pll_dis(tc);
1348bbfd3190SMarek Vasut 
1349bbfd3190SMarek Vasut 	return 0;
1350bbfd3190SMarek Vasut }
1351bbfd3190SMarek Vasut 
1352a219062bSMarek Vasut static int tc_edp_stream_enable(struct tc_data *tc)
13537caff0fcSAndrey Gusakov {
13547caff0fcSAndrey Gusakov 	int ret;
13557caff0fcSAndrey Gusakov 	u32 value;
13567caff0fcSAndrey Gusakov 
135780d57245STomi Valkeinen 	dev_dbg(tc->dev, "enable video stream\n");
13587caff0fcSAndrey Gusakov 
1359*3080c21aSMarek Vasut 	/*
1360*3080c21aSMarek Vasut 	 * Pixel PLL must be enabled for DSI input mode and test pattern.
1361*3080c21aSMarek Vasut 	 *
1362*3080c21aSMarek Vasut 	 * Per TC9595XBG datasheet Revision 0.1 2018-12-27 Figure 4.18
1363*3080c21aSMarek Vasut 	 * "Clock Mode Selection and Clock Sources", either Pixel PLL
1364*3080c21aSMarek Vasut 	 * or DPI_PCLK supplies StrmClk. DPI_PCLK is only available in
1365*3080c21aSMarek Vasut 	 * case valid Pixel Clock are supplied to the chip DPI input.
1366*3080c21aSMarek Vasut 	 * In case built-in test pattern is desired OR DSI input mode
1367*3080c21aSMarek Vasut 	 * is used, DPI_PCLK is not available and thus Pixel PLL must
1368*3080c21aSMarek Vasut 	 * be used instead.
1369*3080c21aSMarek Vasut 	 */
1370*3080c21aSMarek Vasut 	if (tc->input_connector_dsi || tc_test_pattern) {
1371bb248368STomi Valkeinen 		ret = tc_pxl_pll_en(tc, clk_get_rate(tc->refclk),
137246648a3cSTomi Valkeinen 				    1000 * tc->mode.clock);
1373bb248368STomi Valkeinen 		if (ret)
13746d0c3831SAndrey Smirnov 			return ret;
1375bb248368STomi Valkeinen 	}
1376bb248368STomi Valkeinen 
1377aebe58a7SMarek Vasut 	ret = tc_set_common_video_mode(tc, &tc->mode);
1378aebe58a7SMarek Vasut 	if (ret)
1379aebe58a7SMarek Vasut 		return ret;
1380aebe58a7SMarek Vasut 
1381aebe58a7SMarek Vasut 	ret = tc_set_edp_video_mode(tc, &tc->mode);
13825761a259STomi Valkeinen 	if (ret)
138380d57245STomi Valkeinen 		return ret;
13845761a259STomi Valkeinen 
13855761a259STomi Valkeinen 	/* Set M/N */
13865761a259STomi Valkeinen 	ret = tc_stream_clock_calc(tc);
13875761a259STomi Valkeinen 	if (ret)
138880d57245STomi Valkeinen 		return ret;
13895761a259STomi Valkeinen 
13907caff0fcSAndrey Gusakov 	value = VID_MN_GEN | DP_EN;
1391e7dc8d40SThierry Reding 	if (drm_dp_enhanced_frame_cap(tc->link.dpcd))
13927caff0fcSAndrey Gusakov 		value |= EF_EN;
13936d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0CTL, value);
13946d0c3831SAndrey Smirnov 	if (ret)
13956d0c3831SAndrey Smirnov 		return ret;
13967caff0fcSAndrey Gusakov 	/*
13977caff0fcSAndrey Gusakov 	 * VID_EN assertion should be delayed by at least N * LSCLK
13987caff0fcSAndrey Gusakov 	 * cycles from the time VID_MN_GEN is enabled in order to
13997caff0fcSAndrey Gusakov 	 * generate stable values for VID_M. LSCLK is 270 MHz or
14007caff0fcSAndrey Gusakov 	 * 162 MHz, VID_N is set to 32768 in  tc_stream_clock_calc(),
14017caff0fcSAndrey Gusakov 	 * so a delay of at least 203 us should suffice.
14027caff0fcSAndrey Gusakov 	 */
14037caff0fcSAndrey Gusakov 	usleep_range(500, 1000);
14047caff0fcSAndrey Gusakov 	value |= VID_EN;
14056d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0CTL, value);
14066d0c3831SAndrey Smirnov 	if (ret)
14076d0c3831SAndrey Smirnov 		return ret;
140880d57245STomi Valkeinen 
1409d7fd32ecSMarek Vasut 	/* Set input interface */
1410*3080c21aSMarek Vasut 	if (tc->input_connector_dsi)
1411*3080c21aSMarek Vasut 		return tc_dsi_rx_enable(tc);
1412*3080c21aSMarek Vasut 	else
1413d7fd32ecSMarek Vasut 		return tc_dpi_rx_enable(tc);
14147caff0fcSAndrey Gusakov }
14157caff0fcSAndrey Gusakov 
1416a219062bSMarek Vasut static int tc_edp_stream_disable(struct tc_data *tc)
141780d57245STomi Valkeinen {
141880d57245STomi Valkeinen 	int ret;
141980d57245STomi Valkeinen 
142080d57245STomi Valkeinen 	dev_dbg(tc->dev, "disable video stream\n");
142180d57245STomi Valkeinen 
14226d0c3831SAndrey Smirnov 	ret = regmap_update_bits(tc->regmap, DP0CTL, VID_EN, 0);
14236d0c3831SAndrey Smirnov 	if (ret)
14246d0c3831SAndrey Smirnov 		return ret;
142580d57245STomi Valkeinen 
1426bb248368STomi Valkeinen 	tc_pxl_pll_dis(tc);
1427bb248368STomi Valkeinen 
14287caff0fcSAndrey Gusakov 	return 0;
14297caff0fcSAndrey Gusakov }
14307caff0fcSAndrey Gusakov 
1431f5be6239SMarek Vasut static void
1432bbfd3190SMarek Vasut tc_dpi_bridge_atomic_enable(struct drm_bridge *bridge,
1433bbfd3190SMarek Vasut 			    struct drm_bridge_state *old_bridge_state)
1434bbfd3190SMarek Vasut 
1435bbfd3190SMarek Vasut {
1436bbfd3190SMarek Vasut 	struct tc_data *tc = bridge_to_tc(bridge);
1437bbfd3190SMarek Vasut 	int ret;
1438bbfd3190SMarek Vasut 
1439bbfd3190SMarek Vasut 	ret = tc_dpi_stream_enable(tc);
1440bbfd3190SMarek Vasut 	if (ret < 0) {
1441bbfd3190SMarek Vasut 		dev_err(tc->dev, "main link stream start error: %d\n", ret);
1442bbfd3190SMarek Vasut 		tc_main_link_disable(tc);
1443bbfd3190SMarek Vasut 		return;
1444bbfd3190SMarek Vasut 	}
1445bbfd3190SMarek Vasut }
1446bbfd3190SMarek Vasut 
1447bbfd3190SMarek Vasut static void
1448bbfd3190SMarek Vasut tc_dpi_bridge_atomic_disable(struct drm_bridge *bridge,
1449bbfd3190SMarek Vasut 			     struct drm_bridge_state *old_bridge_state)
1450bbfd3190SMarek Vasut {
1451bbfd3190SMarek Vasut 	struct tc_data *tc = bridge_to_tc(bridge);
1452bbfd3190SMarek Vasut 	int ret;
1453bbfd3190SMarek Vasut 
1454bbfd3190SMarek Vasut 	ret = tc_dpi_stream_disable(tc);
1455bbfd3190SMarek Vasut 	if (ret < 0)
1456bbfd3190SMarek Vasut 		dev_err(tc->dev, "main link stream stop error: %d\n", ret);
1457bbfd3190SMarek Vasut }
1458bbfd3190SMarek Vasut 
1459bbfd3190SMarek Vasut static void
1460f5be6239SMarek Vasut tc_edp_bridge_atomic_enable(struct drm_bridge *bridge,
1461f5be6239SMarek Vasut 			    struct drm_bridge_state *old_bridge_state)
14627caff0fcSAndrey Gusakov {
14637caff0fcSAndrey Gusakov 	struct tc_data *tc = bridge_to_tc(bridge);
14647caff0fcSAndrey Gusakov 	int ret;
14657caff0fcSAndrey Gusakov 
1466f25ee501STomi Valkeinen 	ret = tc_get_display_props(tc);
1467f25ee501STomi Valkeinen 	if (ret < 0) {
1468f25ee501STomi Valkeinen 		dev_err(tc->dev, "failed to read display props: %d\n", ret);
1469f25ee501STomi Valkeinen 		return;
1470f25ee501STomi Valkeinen 	}
1471f25ee501STomi Valkeinen 
1472cb3263b2STomi Valkeinen 	ret = tc_main_link_enable(tc);
14737caff0fcSAndrey Gusakov 	if (ret < 0) {
1474cb3263b2STomi Valkeinen 		dev_err(tc->dev, "main link enable error: %d\n", ret);
14757caff0fcSAndrey Gusakov 		return;
14767caff0fcSAndrey Gusakov 	}
14777caff0fcSAndrey Gusakov 
1478a219062bSMarek Vasut 	ret = tc_edp_stream_enable(tc);
14797caff0fcSAndrey Gusakov 	if (ret < 0) {
14807caff0fcSAndrey Gusakov 		dev_err(tc->dev, "main link stream start error: %d\n", ret);
1481cb3263b2STomi Valkeinen 		tc_main_link_disable(tc);
14827caff0fcSAndrey Gusakov 		return;
14837caff0fcSAndrey Gusakov 	}
14847caff0fcSAndrey Gusakov }
14857caff0fcSAndrey Gusakov 
1486f5be6239SMarek Vasut static void
1487f5be6239SMarek Vasut tc_edp_bridge_atomic_disable(struct drm_bridge *bridge,
1488f5be6239SMarek Vasut 			     struct drm_bridge_state *old_bridge_state)
14897caff0fcSAndrey Gusakov {
14907caff0fcSAndrey Gusakov 	struct tc_data *tc = bridge_to_tc(bridge);
14917caff0fcSAndrey Gusakov 	int ret;
14927caff0fcSAndrey Gusakov 
1493a219062bSMarek Vasut 	ret = tc_edp_stream_disable(tc);
14947caff0fcSAndrey Gusakov 	if (ret < 0)
14957caff0fcSAndrey Gusakov 		dev_err(tc->dev, "main link stream stop error: %d\n", ret);
1496cb3263b2STomi Valkeinen 
1497cb3263b2STomi Valkeinen 	ret = tc_main_link_disable(tc);
1498cb3263b2STomi Valkeinen 	if (ret < 0)
1499cb3263b2STomi Valkeinen 		dev_err(tc->dev, "main link disable error: %d\n", ret);
15007caff0fcSAndrey Gusakov }
15017caff0fcSAndrey Gusakov 
15027caff0fcSAndrey Gusakov static bool tc_bridge_mode_fixup(struct drm_bridge *bridge,
15037caff0fcSAndrey Gusakov 				 const struct drm_display_mode *mode,
15047caff0fcSAndrey Gusakov 				 struct drm_display_mode *adj)
15057caff0fcSAndrey Gusakov {
15067caff0fcSAndrey Gusakov 	/* Fixup sync polarities, both hsync and vsync are active low */
15077caff0fcSAndrey Gusakov 	adj->flags = mode->flags;
15087caff0fcSAndrey Gusakov 	adj->flags |= (DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC);
15097caff0fcSAndrey Gusakov 	adj->flags &= ~(DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC);
15107caff0fcSAndrey Gusakov 
15117caff0fcSAndrey Gusakov 	return true;
15127caff0fcSAndrey Gusakov }
15137caff0fcSAndrey Gusakov 
151465fdbb71SMarek Vasut static int tc_common_atomic_check(struct drm_bridge *bridge,
151565fdbb71SMarek Vasut 				  struct drm_bridge_state *bridge_state,
151665fdbb71SMarek Vasut 				  struct drm_crtc_state *crtc_state,
151765fdbb71SMarek Vasut 				  struct drm_connector_state *conn_state,
151865fdbb71SMarek Vasut 				  const unsigned int max_khz)
151965fdbb71SMarek Vasut {
152065fdbb71SMarek Vasut 	tc_bridge_mode_fixup(bridge, &crtc_state->mode,
152165fdbb71SMarek Vasut 			     &crtc_state->adjusted_mode);
152265fdbb71SMarek Vasut 
152365fdbb71SMarek Vasut 	if (crtc_state->adjusted_mode.clock > max_khz)
152465fdbb71SMarek Vasut 		return -EINVAL;
152565fdbb71SMarek Vasut 
152665fdbb71SMarek Vasut 	return 0;
152765fdbb71SMarek Vasut }
152865fdbb71SMarek Vasut 
1529bbfd3190SMarek Vasut static int tc_dpi_atomic_check(struct drm_bridge *bridge,
1530bbfd3190SMarek Vasut 			       struct drm_bridge_state *bridge_state,
1531bbfd3190SMarek Vasut 			       struct drm_crtc_state *crtc_state,
1532bbfd3190SMarek Vasut 			       struct drm_connector_state *conn_state)
1533bbfd3190SMarek Vasut {
1534bbfd3190SMarek Vasut 	/* DSI->DPI interface clock limitation: upto 100 MHz */
1535bbfd3190SMarek Vasut 	return tc_common_atomic_check(bridge, bridge_state, crtc_state,
1536bbfd3190SMarek Vasut 				      conn_state, 100000);
1537bbfd3190SMarek Vasut }
1538bbfd3190SMarek Vasut 
153965fdbb71SMarek Vasut static int tc_edp_atomic_check(struct drm_bridge *bridge,
154065fdbb71SMarek Vasut 			       struct drm_bridge_state *bridge_state,
154165fdbb71SMarek Vasut 			       struct drm_crtc_state *crtc_state,
154265fdbb71SMarek Vasut 			       struct drm_connector_state *conn_state)
154365fdbb71SMarek Vasut {
154465fdbb71SMarek Vasut 	/* DPI->(e)DP interface clock limitation: upto 154 MHz */
154565fdbb71SMarek Vasut 	return tc_common_atomic_check(bridge, bridge_state, crtc_state,
154665fdbb71SMarek Vasut 				      conn_state, 154000);
154765fdbb71SMarek Vasut }
154865fdbb71SMarek Vasut 
1549a219062bSMarek Vasut static enum drm_mode_status
1550bbfd3190SMarek Vasut tc_dpi_mode_valid(struct drm_bridge *bridge,
1551bbfd3190SMarek Vasut 		  const struct drm_display_info *info,
1552bbfd3190SMarek Vasut 		  const struct drm_display_mode *mode)
1553bbfd3190SMarek Vasut {
1554bbfd3190SMarek Vasut 	/* DPI interface clock limitation: upto 100 MHz */
1555bbfd3190SMarek Vasut 	if (mode->clock > 100000)
1556bbfd3190SMarek Vasut 		return MODE_CLOCK_HIGH;
1557bbfd3190SMarek Vasut 
1558bbfd3190SMarek Vasut 	return MODE_OK;
1559bbfd3190SMarek Vasut }
1560bbfd3190SMarek Vasut 
1561bbfd3190SMarek Vasut static enum drm_mode_status
1562a219062bSMarek Vasut tc_edp_mode_valid(struct drm_bridge *bridge,
156312c683e1SLaurent Pinchart 		  const struct drm_display_info *info,
15644647a64fSTomi Valkeinen 		  const struct drm_display_mode *mode)
15657caff0fcSAndrey Gusakov {
15664647a64fSTomi Valkeinen 	struct tc_data *tc = bridge_to_tc(bridge);
156751b9e62eSTomi Valkeinen 	u32 req, avail;
156851b9e62eSTomi Valkeinen 	u32 bits_per_pixel = 24;
156951b9e62eSTomi Valkeinen 
157099fc8e96SAndrey Gusakov 	/* DPI interface clock limitation: upto 154 MHz */
157199fc8e96SAndrey Gusakov 	if (mode->clock > 154000)
157299fc8e96SAndrey Gusakov 		return MODE_CLOCK_HIGH;
157399fc8e96SAndrey Gusakov 
157451b9e62eSTomi Valkeinen 	req = mode->clock * bits_per_pixel / 8;
1575e7dc8d40SThierry Reding 	avail = tc->link.num_lanes * tc->link.rate;
157651b9e62eSTomi Valkeinen 
157751b9e62eSTomi Valkeinen 	if (req > avail)
157851b9e62eSTomi Valkeinen 		return MODE_BAD;
157951b9e62eSTomi Valkeinen 
15807caff0fcSAndrey Gusakov 	return MODE_OK;
15817caff0fcSAndrey Gusakov }
15827caff0fcSAndrey Gusakov 
15837caff0fcSAndrey Gusakov static void tc_bridge_mode_set(struct drm_bridge *bridge,
158463f8f3baSLaurent Pinchart 			       const struct drm_display_mode *mode,
158563f8f3baSLaurent Pinchart 			       const struct drm_display_mode *adj)
15867caff0fcSAndrey Gusakov {
15877caff0fcSAndrey Gusakov 	struct tc_data *tc = bridge_to_tc(bridge);
15887caff0fcSAndrey Gusakov 
1589d008bc33SVille Syrjälä 	drm_mode_copy(&tc->mode, mode);
15907caff0fcSAndrey Gusakov }
15917caff0fcSAndrey Gusakov 
1592731f4badSSam Ravnborg static struct edid *tc_get_edid(struct drm_bridge *bridge,
1593731f4badSSam Ravnborg 				struct drm_connector *connector)
1594731f4badSSam Ravnborg {
1595731f4badSSam Ravnborg 	struct tc_data *tc = bridge_to_tc(bridge);
1596731f4badSSam Ravnborg 
1597731f4badSSam Ravnborg 	return drm_get_edid(connector, &tc->aux.ddc);
1598731f4badSSam Ravnborg }
1599731f4badSSam Ravnborg 
16007caff0fcSAndrey Gusakov static int tc_connector_get_modes(struct drm_connector *connector)
16017caff0fcSAndrey Gusakov {
16027caff0fcSAndrey Gusakov 	struct tc_data *tc = connector_to_tc(connector);
1603731f4badSSam Ravnborg 	int num_modes;
16047caff0fcSAndrey Gusakov 	struct edid *edid;
160532315730STomi Valkeinen 	int ret;
160632315730STomi Valkeinen 
160732315730STomi Valkeinen 	ret = tc_get_display_props(tc);
160832315730STomi Valkeinen 	if (ret < 0) {
160932315730STomi Valkeinen 		dev_err(tc->dev, "failed to read display props: %d\n", ret);
161032315730STomi Valkeinen 		return 0;
161132315730STomi Valkeinen 	}
16127caff0fcSAndrey Gusakov 
1613de5e6c02SSam Ravnborg 	if (tc->panel_bridge) {
1614de5e6c02SSam Ravnborg 		num_modes = drm_bridge_get_modes(tc->panel_bridge, connector);
1615731f4badSSam Ravnborg 		if (num_modes > 0)
1616731f4badSSam Ravnborg 			return num_modes;
1617de5e6c02SSam Ravnborg 	}
16187caff0fcSAndrey Gusakov 
1619731f4badSSam Ravnborg 	edid = tc_get_edid(&tc->bridge, connector);
1620731f4badSSam Ravnborg 	num_modes = drm_add_edid_modes(connector, edid);
1621731f4badSSam Ravnborg 	kfree(edid);
16227caff0fcSAndrey Gusakov 
1623731f4badSSam Ravnborg 	return num_modes;
16247caff0fcSAndrey Gusakov }
16257caff0fcSAndrey Gusakov 
16267caff0fcSAndrey Gusakov static const struct drm_connector_helper_funcs tc_connector_helper_funcs = {
16277caff0fcSAndrey Gusakov 	.get_modes = tc_connector_get_modes,
16287caff0fcSAndrey Gusakov };
16297caff0fcSAndrey Gusakov 
1630136d73a8SSam Ravnborg static enum drm_connector_status tc_bridge_detect(struct drm_bridge *bridge)
1631f25ee501STomi Valkeinen {
1632136d73a8SSam Ravnborg 	struct tc_data *tc = bridge_to_tc(bridge);
1633f25ee501STomi Valkeinen 	bool conn;
1634f25ee501STomi Valkeinen 	u32 val;
1635f25ee501STomi Valkeinen 	int ret;
1636f25ee501STomi Valkeinen 
16376d0c3831SAndrey Smirnov 	ret = regmap_read(tc->regmap, GPIOI, &val);
16386d0c3831SAndrey Smirnov 	if (ret)
16396d0c3831SAndrey Smirnov 		return connector_status_unknown;
1640f25ee501STomi Valkeinen 
1641f25ee501STomi Valkeinen 	conn = val & BIT(tc->hpd_pin);
1642f25ee501STomi Valkeinen 
1643f25ee501STomi Valkeinen 	if (conn)
1644f25ee501STomi Valkeinen 		return connector_status_connected;
1645f25ee501STomi Valkeinen 	else
1646f25ee501STomi Valkeinen 		return connector_status_disconnected;
1647f25ee501STomi Valkeinen }
1648f25ee501STomi Valkeinen 
1649136d73a8SSam Ravnborg static enum drm_connector_status
1650136d73a8SSam Ravnborg tc_connector_detect(struct drm_connector *connector, bool force)
1651136d73a8SSam Ravnborg {
1652136d73a8SSam Ravnborg 	struct tc_data *tc = connector_to_tc(connector);
1653136d73a8SSam Ravnborg 
1654136d73a8SSam Ravnborg 	if (tc->hpd_pin >= 0)
1655136d73a8SSam Ravnborg 		return tc_bridge_detect(&tc->bridge);
1656136d73a8SSam Ravnborg 
1657de5e6c02SSam Ravnborg 	if (tc->panel_bridge)
1658136d73a8SSam Ravnborg 		return connector_status_connected;
1659136d73a8SSam Ravnborg 	else
1660136d73a8SSam Ravnborg 		return connector_status_unknown;
1661136d73a8SSam Ravnborg }
1662136d73a8SSam Ravnborg 
16637caff0fcSAndrey Gusakov static const struct drm_connector_funcs tc_connector_funcs = {
1664f25ee501STomi Valkeinen 	.detect = tc_connector_detect,
16657caff0fcSAndrey Gusakov 	.fill_modes = drm_helper_probe_single_connector_modes,
1666fdd8326aSMarek Vasut 	.destroy = drm_connector_cleanup,
16677caff0fcSAndrey Gusakov 	.reset = drm_atomic_helper_connector_reset,
16687caff0fcSAndrey Gusakov 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
16697caff0fcSAndrey Gusakov 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
16707caff0fcSAndrey Gusakov };
16717caff0fcSAndrey Gusakov 
1672bbfd3190SMarek Vasut static int tc_dpi_bridge_attach(struct drm_bridge *bridge,
1673bbfd3190SMarek Vasut 				enum drm_bridge_attach_flags flags)
1674bbfd3190SMarek Vasut {
1675bbfd3190SMarek Vasut 	struct tc_data *tc = bridge_to_tc(bridge);
1676bbfd3190SMarek Vasut 
1677bbfd3190SMarek Vasut 	if (!tc->panel_bridge)
1678bbfd3190SMarek Vasut 		return 0;
1679bbfd3190SMarek Vasut 
1680bbfd3190SMarek Vasut 	return drm_bridge_attach(tc->bridge.encoder, tc->panel_bridge,
1681bbfd3190SMarek Vasut 				 &tc->bridge, flags);
1682bbfd3190SMarek Vasut }
1683bbfd3190SMarek Vasut 
1684a219062bSMarek Vasut static int tc_edp_bridge_attach(struct drm_bridge *bridge,
1685a25b988fSLaurent Pinchart 				enum drm_bridge_attach_flags flags)
16867caff0fcSAndrey Gusakov {
16877caff0fcSAndrey Gusakov 	u32 bus_format = MEDIA_BUS_FMT_RGB888_1X24;
16887caff0fcSAndrey Gusakov 	struct tc_data *tc = bridge_to_tc(bridge);
16897caff0fcSAndrey Gusakov 	struct drm_device *drm = bridge->dev;
16907caff0fcSAndrey Gusakov 	int ret;
16917caff0fcSAndrey Gusakov 
1692de5e6c02SSam Ravnborg 	if (tc->panel_bridge) {
1693de5e6c02SSam Ravnborg 		/* If a connector is required then this driver shall create it */
1694de5e6c02SSam Ravnborg 		ret = drm_bridge_attach(tc->bridge.encoder, tc->panel_bridge,
1695de5e6c02SSam Ravnborg 					&tc->bridge, flags | DRM_BRIDGE_ATTACH_NO_CONNECTOR);
1696de5e6c02SSam Ravnborg 		if (ret)
1697de5e6c02SSam Ravnborg 			return ret;
1698a25b988fSLaurent Pinchart 	}
1699a25b988fSLaurent Pinchart 
1700de5e6c02SSam Ravnborg 	if (flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)
1701de5e6c02SSam Ravnborg 		return 0;
1702de5e6c02SSam Ravnborg 
17036cba3fe4SLyude Paul 	tc->aux.drm_dev = drm;
170485ddbe2cSLyude Paul 	ret = drm_dp_aux_register(&tc->aux);
170585ddbe2cSLyude Paul 	if (ret < 0)
170685ddbe2cSLyude Paul 		return ret;
170785ddbe2cSLyude Paul 
1708f25ee501STomi Valkeinen 	/* Create DP/eDP connector */
17097caff0fcSAndrey Gusakov 	drm_connector_helper_add(&tc->connector, &tc_connector_helper_funcs);
1710de5e6c02SSam Ravnborg 	ret = drm_connector_init(drm, &tc->connector, &tc_connector_funcs, tc->bridge.type);
17117caff0fcSAndrey Gusakov 	if (ret)
171285ddbe2cSLyude Paul 		goto aux_unregister;
17137caff0fcSAndrey Gusakov 
1714f25ee501STomi Valkeinen 	/* Don't poll if don't have HPD connected */
1715f25ee501STomi Valkeinen 	if (tc->hpd_pin >= 0) {
1716f25ee501STomi Valkeinen 		if (tc->have_irq)
1717f25ee501STomi Valkeinen 			tc->connector.polled = DRM_CONNECTOR_POLL_HPD;
1718f25ee501STomi Valkeinen 		else
1719f25ee501STomi Valkeinen 			tc->connector.polled = DRM_CONNECTOR_POLL_CONNECT |
1720f25ee501STomi Valkeinen 					       DRM_CONNECTOR_POLL_DISCONNECT;
1721f25ee501STomi Valkeinen 	}
1722f25ee501STomi Valkeinen 
17237caff0fcSAndrey Gusakov 	drm_display_info_set_bus_formats(&tc->connector.display_info,
17247caff0fcSAndrey Gusakov 					 &bus_format, 1);
17254842379cSTomi Valkeinen 	tc->connector.display_info.bus_flags =
17264842379cSTomi Valkeinen 		DRM_BUS_FLAG_DE_HIGH |
172788bc4178SLaurent Pinchart 		DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE |
172888bc4178SLaurent Pinchart 		DRM_BUS_FLAG_SYNC_DRIVE_NEGEDGE;
1729cde4c44dSDaniel Vetter 	drm_connector_attach_encoder(&tc->connector, tc->bridge.encoder);
17307caff0fcSAndrey Gusakov 
17317caff0fcSAndrey Gusakov 	return 0;
173285ddbe2cSLyude Paul aux_unregister:
173385ddbe2cSLyude Paul 	drm_dp_aux_unregister(&tc->aux);
173485ddbe2cSLyude Paul 	return ret;
173585ddbe2cSLyude Paul }
173685ddbe2cSLyude Paul 
1737a219062bSMarek Vasut static void tc_edp_bridge_detach(struct drm_bridge *bridge)
173885ddbe2cSLyude Paul {
173985ddbe2cSLyude Paul 	drm_dp_aux_unregister(&bridge_to_tc(bridge)->aux);
17407caff0fcSAndrey Gusakov }
17417caff0fcSAndrey Gusakov 
1742bbfd3190SMarek Vasut #define MAX_INPUT_SEL_FORMATS	1
1743bbfd3190SMarek Vasut 
1744bbfd3190SMarek Vasut static u32 *
1745bbfd3190SMarek Vasut tc_dpi_atomic_get_input_bus_fmts(struct drm_bridge *bridge,
1746bbfd3190SMarek Vasut 				 struct drm_bridge_state *bridge_state,
1747bbfd3190SMarek Vasut 				 struct drm_crtc_state *crtc_state,
1748bbfd3190SMarek Vasut 				 struct drm_connector_state *conn_state,
1749bbfd3190SMarek Vasut 				 u32 output_fmt,
1750bbfd3190SMarek Vasut 				 unsigned int *num_input_fmts)
1751bbfd3190SMarek Vasut {
1752bbfd3190SMarek Vasut 	u32 *input_fmts;
1753bbfd3190SMarek Vasut 
1754bbfd3190SMarek Vasut 	*num_input_fmts = 0;
1755bbfd3190SMarek Vasut 
1756bbfd3190SMarek Vasut 	input_fmts = kcalloc(MAX_INPUT_SEL_FORMATS, sizeof(*input_fmts),
1757bbfd3190SMarek Vasut 			     GFP_KERNEL);
1758bbfd3190SMarek Vasut 	if (!input_fmts)
1759bbfd3190SMarek Vasut 		return NULL;
1760bbfd3190SMarek Vasut 
1761bbfd3190SMarek Vasut 	/* This is the DSI-end bus format */
1762bbfd3190SMarek Vasut 	input_fmts[0] = MEDIA_BUS_FMT_RGB888_1X24;
1763bbfd3190SMarek Vasut 	*num_input_fmts = 1;
1764bbfd3190SMarek Vasut 
1765bbfd3190SMarek Vasut 	return input_fmts;
1766bbfd3190SMarek Vasut }
1767bbfd3190SMarek Vasut 
1768bbfd3190SMarek Vasut static const struct drm_bridge_funcs tc_dpi_bridge_funcs = {
1769bbfd3190SMarek Vasut 	.attach = tc_dpi_bridge_attach,
1770bbfd3190SMarek Vasut 	.mode_valid = tc_dpi_mode_valid,
1771bbfd3190SMarek Vasut 	.mode_set = tc_bridge_mode_set,
1772bbfd3190SMarek Vasut 	.atomic_check = tc_dpi_atomic_check,
1773bbfd3190SMarek Vasut 	.atomic_enable = tc_dpi_bridge_atomic_enable,
1774bbfd3190SMarek Vasut 	.atomic_disable = tc_dpi_bridge_atomic_disable,
1775bbfd3190SMarek Vasut 	.atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
1776bbfd3190SMarek Vasut 	.atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
1777bbfd3190SMarek Vasut 	.atomic_reset = drm_atomic_helper_bridge_reset,
1778bbfd3190SMarek Vasut 	.atomic_get_input_bus_fmts = tc_dpi_atomic_get_input_bus_fmts,
1779bbfd3190SMarek Vasut };
1780bbfd3190SMarek Vasut 
1781a219062bSMarek Vasut static const struct drm_bridge_funcs tc_edp_bridge_funcs = {
1782a219062bSMarek Vasut 	.attach = tc_edp_bridge_attach,
1783a219062bSMarek Vasut 	.detach = tc_edp_bridge_detach,
1784a219062bSMarek Vasut 	.mode_valid = tc_edp_mode_valid,
17857caff0fcSAndrey Gusakov 	.mode_set = tc_bridge_mode_set,
178665fdbb71SMarek Vasut 	.atomic_check = tc_edp_atomic_check,
1787f5be6239SMarek Vasut 	.atomic_enable = tc_edp_bridge_atomic_enable,
1788f5be6239SMarek Vasut 	.atomic_disable = tc_edp_bridge_atomic_disable,
17897caff0fcSAndrey Gusakov 	.mode_fixup = tc_bridge_mode_fixup,
1790136d73a8SSam Ravnborg 	.detect = tc_bridge_detect,
1791731f4badSSam Ravnborg 	.get_edid = tc_get_edid,
1792f5be6239SMarek Vasut 	.atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
1793f5be6239SMarek Vasut 	.atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
1794f5be6239SMarek Vasut 	.atomic_reset = drm_atomic_helper_bridge_reset,
17957caff0fcSAndrey Gusakov };
17967caff0fcSAndrey Gusakov 
17977caff0fcSAndrey Gusakov static bool tc_readable_reg(struct device *dev, unsigned int reg)
17987caff0fcSAndrey Gusakov {
17997caff0fcSAndrey Gusakov 	return reg != SYSCTRL;
18007caff0fcSAndrey Gusakov }
18017caff0fcSAndrey Gusakov 
18027caff0fcSAndrey Gusakov static const struct regmap_range tc_volatile_ranges[] = {
18037caff0fcSAndrey Gusakov 	regmap_reg_range(DP0_AUXWDATA(0), DP0_AUXSTATUS),
18047caff0fcSAndrey Gusakov 	regmap_reg_range(DP0_LTSTAT, DP0_SNKLTCHGREQ),
18057caff0fcSAndrey Gusakov 	regmap_reg_range(DP_PHY_CTRL, DP_PHY_CTRL),
18067caff0fcSAndrey Gusakov 	regmap_reg_range(DP0_PLLCTRL, PXL_PLLCTRL),
18077caff0fcSAndrey Gusakov 	regmap_reg_range(VFUEN0, VFUEN0),
1808af9526f2STomi Valkeinen 	regmap_reg_range(INTSTS_G, INTSTS_G),
1809af9526f2STomi Valkeinen 	regmap_reg_range(GPIOI, GPIOI),
18107caff0fcSAndrey Gusakov };
18117caff0fcSAndrey Gusakov 
18127caff0fcSAndrey Gusakov static const struct regmap_access_table tc_volatile_table = {
18137caff0fcSAndrey Gusakov 	.yes_ranges = tc_volatile_ranges,
18147caff0fcSAndrey Gusakov 	.n_yes_ranges = ARRAY_SIZE(tc_volatile_ranges),
18157caff0fcSAndrey Gusakov };
18167caff0fcSAndrey Gusakov 
18177caff0fcSAndrey Gusakov static bool tc_writeable_reg(struct device *dev, unsigned int reg)
18187caff0fcSAndrey Gusakov {
18197caff0fcSAndrey Gusakov 	return (reg != TC_IDREG) &&
18207caff0fcSAndrey Gusakov 	       (reg != DP0_LTSTAT) &&
18217caff0fcSAndrey Gusakov 	       (reg != DP0_SNKLTCHGREQ);
18227caff0fcSAndrey Gusakov }
18237caff0fcSAndrey Gusakov 
18247caff0fcSAndrey Gusakov static const struct regmap_config tc_regmap_config = {
18257caff0fcSAndrey Gusakov 	.name = "tc358767",
18267caff0fcSAndrey Gusakov 	.reg_bits = 16,
18277caff0fcSAndrey Gusakov 	.val_bits = 32,
18287caff0fcSAndrey Gusakov 	.reg_stride = 4,
18297caff0fcSAndrey Gusakov 	.max_register = PLL_DBG,
18307caff0fcSAndrey Gusakov 	.cache_type = REGCACHE_RBTREE,
18317caff0fcSAndrey Gusakov 	.readable_reg = tc_readable_reg,
18327caff0fcSAndrey Gusakov 	.volatile_table = &tc_volatile_table,
18337caff0fcSAndrey Gusakov 	.writeable_reg = tc_writeable_reg,
18347caff0fcSAndrey Gusakov 	.reg_format_endian = REGMAP_ENDIAN_BIG,
18357caff0fcSAndrey Gusakov 	.val_format_endian = REGMAP_ENDIAN_LITTLE,
18367caff0fcSAndrey Gusakov };
18377caff0fcSAndrey Gusakov 
1838f25ee501STomi Valkeinen static irqreturn_t tc_irq_handler(int irq, void *arg)
1839f25ee501STomi Valkeinen {
1840f25ee501STomi Valkeinen 	struct tc_data *tc = arg;
1841f25ee501STomi Valkeinen 	u32 val;
1842f25ee501STomi Valkeinen 	int r;
1843f25ee501STomi Valkeinen 
1844f25ee501STomi Valkeinen 	r = regmap_read(tc->regmap, INTSTS_G, &val);
1845f25ee501STomi Valkeinen 	if (r)
1846f25ee501STomi Valkeinen 		return IRQ_NONE;
1847f25ee501STomi Valkeinen 
1848f25ee501STomi Valkeinen 	if (!val)
1849f25ee501STomi Valkeinen 		return IRQ_NONE;
1850f25ee501STomi Valkeinen 
1851f25ee501STomi Valkeinen 	if (val & INT_SYSERR) {
1852f25ee501STomi Valkeinen 		u32 stat = 0;
1853f25ee501STomi Valkeinen 
1854f25ee501STomi Valkeinen 		regmap_read(tc->regmap, SYSSTAT, &stat);
1855f25ee501STomi Valkeinen 
1856f25ee501STomi Valkeinen 		dev_err(tc->dev, "syserr %x\n", stat);
1857f25ee501STomi Valkeinen 	}
1858f25ee501STomi Valkeinen 
1859f25ee501STomi Valkeinen 	if (tc->hpd_pin >= 0 && tc->bridge.dev) {
1860f25ee501STomi Valkeinen 		/*
1861f25ee501STomi Valkeinen 		 * H is triggered when the GPIO goes high.
1862f25ee501STomi Valkeinen 		 *
1863f25ee501STomi Valkeinen 		 * LC is triggered when the GPIO goes low and stays low for
1864f25ee501STomi Valkeinen 		 * the duration of LCNT
1865f25ee501STomi Valkeinen 		 */
1866f25ee501STomi Valkeinen 		bool h = val & INT_GPIO_H(tc->hpd_pin);
1867f25ee501STomi Valkeinen 		bool lc = val & INT_GPIO_LC(tc->hpd_pin);
1868f25ee501STomi Valkeinen 
1869f25ee501STomi Valkeinen 		dev_dbg(tc->dev, "GPIO%d: %s %s\n", tc->hpd_pin,
1870f25ee501STomi Valkeinen 			h ? "H" : "", lc ? "LC" : "");
1871f25ee501STomi Valkeinen 
1872f25ee501STomi Valkeinen 		if (h || lc)
1873f25ee501STomi Valkeinen 			drm_kms_helper_hotplug_event(tc->bridge.dev);
1874f25ee501STomi Valkeinen 	}
1875f25ee501STomi Valkeinen 
1876f25ee501STomi Valkeinen 	regmap_write(tc->regmap, INTSTS_G, val);
1877f25ee501STomi Valkeinen 
1878f25ee501STomi Valkeinen 	return IRQ_HANDLED;
1879f25ee501STomi Valkeinen }
1880f25ee501STomi Valkeinen 
1881bbfd3190SMarek Vasut static int tc_mipi_dsi_host_attach(struct tc_data *tc)
1882bbfd3190SMarek Vasut {
1883bbfd3190SMarek Vasut 	struct device *dev = tc->dev;
1884bbfd3190SMarek Vasut 	struct device_node *host_node;
1885bbfd3190SMarek Vasut 	struct device_node *endpoint;
1886bbfd3190SMarek Vasut 	struct mipi_dsi_device *dsi;
1887bbfd3190SMarek Vasut 	struct mipi_dsi_host *host;
1888bbfd3190SMarek Vasut 	const struct mipi_dsi_device_info info = {
1889bbfd3190SMarek Vasut 		.type = "tc358767",
1890bbfd3190SMarek Vasut 		.channel = 0,
1891bbfd3190SMarek Vasut 		.node = NULL,
1892bbfd3190SMarek Vasut 	};
1893bbfd3190SMarek Vasut 	int dsi_lanes, ret;
1894bbfd3190SMarek Vasut 
1895bbfd3190SMarek Vasut 	endpoint = of_graph_get_endpoint_by_regs(dev->of_node, 0, -1);
1896bbfd3190SMarek Vasut 	dsi_lanes = of_property_count_u32_elems(endpoint, "data-lanes");
1897bbfd3190SMarek Vasut 	host_node = of_graph_get_remote_port_parent(endpoint);
1898bbfd3190SMarek Vasut 	host = of_find_mipi_dsi_host_by_node(host_node);
1899bbfd3190SMarek Vasut 	of_node_put(host_node);
1900bbfd3190SMarek Vasut 	of_node_put(endpoint);
1901bbfd3190SMarek Vasut 
1902bbfd3190SMarek Vasut 	if (dsi_lanes < 0 || dsi_lanes > 4)
1903bbfd3190SMarek Vasut 		return -EINVAL;
1904bbfd3190SMarek Vasut 
1905bbfd3190SMarek Vasut 	if (!host)
1906bbfd3190SMarek Vasut 		return -EPROBE_DEFER;
1907bbfd3190SMarek Vasut 
1908bbfd3190SMarek Vasut 	dsi = mipi_dsi_device_register_full(host, &info);
1909bbfd3190SMarek Vasut 	if (IS_ERR(dsi))
1910bbfd3190SMarek Vasut 		return dev_err_probe(dev, PTR_ERR(dsi),
1911bbfd3190SMarek Vasut 				     "failed to create dsi device\n");
1912bbfd3190SMarek Vasut 
1913bbfd3190SMarek Vasut 	tc->dsi = dsi;
1914bbfd3190SMarek Vasut 
1915bbfd3190SMarek Vasut 	tc->dsi_lanes = dsi_lanes;
1916bbfd3190SMarek Vasut 	dsi->lanes = tc->dsi_lanes;
1917bbfd3190SMarek Vasut 	dsi->format = MIPI_DSI_FMT_RGB888;
1918bbfd3190SMarek Vasut 	dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_SYNC_PULSE;
1919bbfd3190SMarek Vasut 
1920bbfd3190SMarek Vasut 	ret = mipi_dsi_attach(dsi);
1921bbfd3190SMarek Vasut 	if (ret < 0) {
1922bbfd3190SMarek Vasut 		dev_err(dev, "failed to attach dsi to host: %d\n", ret);
1923bbfd3190SMarek Vasut 		return ret;
1924bbfd3190SMarek Vasut 	}
1925bbfd3190SMarek Vasut 
1926bbfd3190SMarek Vasut 	return 0;
1927bbfd3190SMarek Vasut }
1928bbfd3190SMarek Vasut 
1929bbfd3190SMarek Vasut static int tc_probe_dpi_bridge_endpoint(struct tc_data *tc)
1930bbfd3190SMarek Vasut {
1931bbfd3190SMarek Vasut 	struct device *dev = tc->dev;
1932bbfd3190SMarek Vasut 	struct drm_panel *panel;
1933bbfd3190SMarek Vasut 	int ret;
1934bbfd3190SMarek Vasut 
1935bbfd3190SMarek Vasut 	/* port@1 is the DPI input/output port */
1936bbfd3190SMarek Vasut 	ret = drm_of_find_panel_or_bridge(dev->of_node, 1, 0, &panel, NULL);
1937bbfd3190SMarek Vasut 	if (ret && ret != -ENODEV)
1938bbfd3190SMarek Vasut 		return ret;
1939bbfd3190SMarek Vasut 
1940bbfd3190SMarek Vasut 	if (panel) {
1941bbfd3190SMarek Vasut 		struct drm_bridge *panel_bridge;
1942bbfd3190SMarek Vasut 
1943bbfd3190SMarek Vasut 		panel_bridge = devm_drm_panel_bridge_add(dev, panel);
1944bbfd3190SMarek Vasut 		if (IS_ERR(panel_bridge))
1945bbfd3190SMarek Vasut 			return PTR_ERR(panel_bridge);
1946bbfd3190SMarek Vasut 
1947bbfd3190SMarek Vasut 		tc->panel_bridge = panel_bridge;
1948bbfd3190SMarek Vasut 		tc->bridge.type = DRM_MODE_CONNECTOR_DPI;
1949bbfd3190SMarek Vasut 		tc->bridge.funcs = &tc_dpi_bridge_funcs;
1950bbfd3190SMarek Vasut 
1951bbfd3190SMarek Vasut 		return 0;
1952bbfd3190SMarek Vasut 	}
1953bbfd3190SMarek Vasut 
1954bbfd3190SMarek Vasut 	return ret;
1955bbfd3190SMarek Vasut }
1956bbfd3190SMarek Vasut 
19578478095aSMarek Vasut static int tc_probe_edp_bridge_endpoint(struct tc_data *tc)
19587caff0fcSAndrey Gusakov {
19598478095aSMarek Vasut 	struct device *dev = tc->dev;
1960de5e6c02SSam Ravnborg 	struct drm_panel *panel;
19617caff0fcSAndrey Gusakov 	int ret;
19627caff0fcSAndrey Gusakov 
19637caff0fcSAndrey Gusakov 	/* port@2 is the output port */
1964de5e6c02SSam Ravnborg 	ret = drm_of_find_panel_or_bridge(dev->of_node, 2, 0, &panel, NULL);
1965d630213fSLucas Stach 	if (ret && ret != -ENODEV)
1966ebc94461SRob Herring 		return ret;
19677caff0fcSAndrey Gusakov 
1968de5e6c02SSam Ravnborg 	if (panel) {
1969de5e6c02SSam Ravnborg 		struct drm_bridge *panel_bridge;
1970de5e6c02SSam Ravnborg 
1971de5e6c02SSam Ravnborg 		panel_bridge = devm_drm_panel_bridge_add(dev, panel);
1972de5e6c02SSam Ravnborg 		if (IS_ERR(panel_bridge))
1973de5e6c02SSam Ravnborg 			return PTR_ERR(panel_bridge);
1974de5e6c02SSam Ravnborg 
1975de5e6c02SSam Ravnborg 		tc->panel_bridge = panel_bridge;
1976de5e6c02SSam Ravnborg 		tc->bridge.type = DRM_MODE_CONNECTOR_eDP;
1977de5e6c02SSam Ravnborg 	} else {
1978de5e6c02SSam Ravnborg 		tc->bridge.type = DRM_MODE_CONNECTOR_DisplayPort;
1979de5e6c02SSam Ravnborg 	}
1980de5e6c02SSam Ravnborg 
1981dd1fd5abSMarek Vasut 	tc->bridge.funcs = &tc_edp_bridge_funcs;
1982dd1fd5abSMarek Vasut 	if (tc->hpd_pin >= 0)
1983dd1fd5abSMarek Vasut 		tc->bridge.ops |= DRM_BRIDGE_OP_DETECT;
1984dd1fd5abSMarek Vasut 	tc->bridge.ops |= DRM_BRIDGE_OP_EDID;
1985dd1fd5abSMarek Vasut 
19869030a9e5SMarek Vasut 	return 0;
19878478095aSMarek Vasut }
19888478095aSMarek Vasut 
198971f7d9c0SMarek Vasut static int tc_probe_bridge_endpoint(struct tc_data *tc)
199071f7d9c0SMarek Vasut {
199171f7d9c0SMarek Vasut 	struct device *dev = tc->dev;
199271f7d9c0SMarek Vasut 	struct of_endpoint endpoint;
199371f7d9c0SMarek Vasut 	struct device_node *node = NULL;
199471f7d9c0SMarek Vasut 	const u8 mode_dpi_to_edp = BIT(1) | BIT(2);
19951bb533b6SMarek Vasut 	const u8 mode_dpi_to_dp = BIT(1);
199671f7d9c0SMarek Vasut 	const u8 mode_dsi_to_edp = BIT(0) | BIT(2);
19971bb533b6SMarek Vasut 	const u8 mode_dsi_to_dp = BIT(0);
199871f7d9c0SMarek Vasut 	const u8 mode_dsi_to_dpi = BIT(0) | BIT(1);
199971f7d9c0SMarek Vasut 	u8 mode = 0;
200071f7d9c0SMarek Vasut 
200171f7d9c0SMarek Vasut 	/*
200271f7d9c0SMarek Vasut 	 * Determine bridge configuration.
200371f7d9c0SMarek Vasut 	 *
200471f7d9c0SMarek Vasut 	 * Port allocation:
200571f7d9c0SMarek Vasut 	 * port@0 - DSI input
200671f7d9c0SMarek Vasut 	 * port@1 - DPI input/output
200771f7d9c0SMarek Vasut 	 * port@2 - eDP output
200871f7d9c0SMarek Vasut 	 *
200971f7d9c0SMarek Vasut 	 * Possible connections:
201071f7d9c0SMarek Vasut 	 * DPI -> port@1 -> port@2 -> eDP :: [port@0 is not connected]
201171f7d9c0SMarek Vasut 	 * DSI -> port@0 -> port@2 -> eDP :: [port@1 is not connected]
201271f7d9c0SMarek Vasut 	 * DSI -> port@0 -> port@1 -> DPI :: [port@2 is not connected]
201371f7d9c0SMarek Vasut 	 */
201471f7d9c0SMarek Vasut 
201571f7d9c0SMarek Vasut 	for_each_endpoint_of_node(dev->of_node, node) {
201671f7d9c0SMarek Vasut 		of_graph_parse_endpoint(node, &endpoint);
201771f7d9c0SMarek Vasut 		if (endpoint.port > 2)
201871f7d9c0SMarek Vasut 			return -EINVAL;
201971f7d9c0SMarek Vasut 
202071f7d9c0SMarek Vasut 		mode |= BIT(endpoint.port);
202171f7d9c0SMarek Vasut 	}
202271f7d9c0SMarek Vasut 
2023*3080c21aSMarek Vasut 	if (mode == mode_dpi_to_edp || mode == mode_dpi_to_dp) {
2024*3080c21aSMarek Vasut 		tc->input_connector_dsi = false;
202571f7d9c0SMarek Vasut 		return tc_probe_edp_bridge_endpoint(tc);
2026*3080c21aSMarek Vasut 	} else if (mode == mode_dsi_to_dpi) {
2027*3080c21aSMarek Vasut 		tc->input_connector_dsi = true;
2028bbfd3190SMarek Vasut 		return tc_probe_dpi_bridge_endpoint(tc);
2029*3080c21aSMarek Vasut 	} else if (mode == mode_dsi_to_edp || mode == mode_dsi_to_dp) {
2030*3080c21aSMarek Vasut 		tc->input_connector_dsi = true;
2031*3080c21aSMarek Vasut 		return tc_probe_edp_bridge_endpoint(tc);
2032*3080c21aSMarek Vasut 	}
2033*3080c21aSMarek Vasut 
203471f7d9c0SMarek Vasut 	dev_warn(dev, "Invalid mode (0x%x) is not supported!\n", mode);
203571f7d9c0SMarek Vasut 
203671f7d9c0SMarek Vasut 	return -EINVAL;
203771f7d9c0SMarek Vasut }
203871f7d9c0SMarek Vasut 
20398478095aSMarek Vasut static int tc_probe(struct i2c_client *client, const struct i2c_device_id *id)
20408478095aSMarek Vasut {
20418478095aSMarek Vasut 	struct device *dev = &client->dev;
20428478095aSMarek Vasut 	struct tc_data *tc;
20438478095aSMarek Vasut 	int ret;
20448478095aSMarek Vasut 
20458478095aSMarek Vasut 	tc = devm_kzalloc(dev, sizeof(*tc), GFP_KERNEL);
20468478095aSMarek Vasut 	if (!tc)
20478478095aSMarek Vasut 		return -ENOMEM;
20488478095aSMarek Vasut 
20498478095aSMarek Vasut 	tc->dev = dev;
20508478095aSMarek Vasut 
205171f7d9c0SMarek Vasut 	ret = tc_probe_bridge_endpoint(tc);
20528478095aSMarek Vasut 	if (ret)
20538478095aSMarek Vasut 		return ret;
20548478095aSMarek Vasut 
20557caff0fcSAndrey Gusakov 	/* Shut down GPIO is optional */
20567caff0fcSAndrey Gusakov 	tc->sd_gpio = devm_gpiod_get_optional(dev, "shutdown", GPIOD_OUT_HIGH);
20577caff0fcSAndrey Gusakov 	if (IS_ERR(tc->sd_gpio))
20587caff0fcSAndrey Gusakov 		return PTR_ERR(tc->sd_gpio);
20597caff0fcSAndrey Gusakov 
20607caff0fcSAndrey Gusakov 	if (tc->sd_gpio) {
20617caff0fcSAndrey Gusakov 		gpiod_set_value_cansleep(tc->sd_gpio, 0);
20627caff0fcSAndrey Gusakov 		usleep_range(5000, 10000);
20637caff0fcSAndrey Gusakov 	}
20647caff0fcSAndrey Gusakov 
20657caff0fcSAndrey Gusakov 	/* Reset GPIO is optional */
20667caff0fcSAndrey Gusakov 	tc->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
20677caff0fcSAndrey Gusakov 	if (IS_ERR(tc->reset_gpio))
20687caff0fcSAndrey Gusakov 		return PTR_ERR(tc->reset_gpio);
20697caff0fcSAndrey Gusakov 
20707caff0fcSAndrey Gusakov 	if (tc->reset_gpio) {
20717caff0fcSAndrey Gusakov 		gpiod_set_value_cansleep(tc->reset_gpio, 1);
20727caff0fcSAndrey Gusakov 		usleep_range(5000, 10000);
20737caff0fcSAndrey Gusakov 	}
20747caff0fcSAndrey Gusakov 
20757caff0fcSAndrey Gusakov 	tc->refclk = devm_clk_get(dev, "ref");
20767caff0fcSAndrey Gusakov 	if (IS_ERR(tc->refclk)) {
20777caff0fcSAndrey Gusakov 		ret = PTR_ERR(tc->refclk);
20787caff0fcSAndrey Gusakov 		dev_err(dev, "Failed to get refclk: %d\n", ret);
20797caff0fcSAndrey Gusakov 		return ret;
20807caff0fcSAndrey Gusakov 	}
20817caff0fcSAndrey Gusakov 
20827caff0fcSAndrey Gusakov 	tc->regmap = devm_regmap_init_i2c(client, &tc_regmap_config);
20837caff0fcSAndrey Gusakov 	if (IS_ERR(tc->regmap)) {
20847caff0fcSAndrey Gusakov 		ret = PTR_ERR(tc->regmap);
20857caff0fcSAndrey Gusakov 		dev_err(dev, "Failed to initialize regmap: %d\n", ret);
20867caff0fcSAndrey Gusakov 		return ret;
20877caff0fcSAndrey Gusakov 	}
20887caff0fcSAndrey Gusakov 
2089f25ee501STomi Valkeinen 	ret = of_property_read_u32(dev->of_node, "toshiba,hpd-pin",
2090f25ee501STomi Valkeinen 				   &tc->hpd_pin);
2091f25ee501STomi Valkeinen 	if (ret) {
2092f25ee501STomi Valkeinen 		tc->hpd_pin = -ENODEV;
2093f25ee501STomi Valkeinen 	} else {
2094f25ee501STomi Valkeinen 		if (tc->hpd_pin < 0 || tc->hpd_pin > 1) {
2095f25ee501STomi Valkeinen 			dev_err(dev, "failed to parse HPD number\n");
2096f25ee501STomi Valkeinen 			return ret;
2097f25ee501STomi Valkeinen 		}
2098f25ee501STomi Valkeinen 	}
2099f25ee501STomi Valkeinen 
2100f25ee501STomi Valkeinen 	if (client->irq > 0) {
2101f25ee501STomi Valkeinen 		/* enable SysErr */
2102f25ee501STomi Valkeinen 		regmap_write(tc->regmap, INTCTL_G, INT_SYSERR);
2103f25ee501STomi Valkeinen 
2104f25ee501STomi Valkeinen 		ret = devm_request_threaded_irq(dev, client->irq,
2105f25ee501STomi Valkeinen 						NULL, tc_irq_handler,
2106f25ee501STomi Valkeinen 						IRQF_ONESHOT,
2107f25ee501STomi Valkeinen 						"tc358767-irq", tc);
2108f25ee501STomi Valkeinen 		if (ret) {
2109f25ee501STomi Valkeinen 			dev_err(dev, "failed to register dp interrupt\n");
2110f25ee501STomi Valkeinen 			return ret;
2111f25ee501STomi Valkeinen 		}
2112f25ee501STomi Valkeinen 
2113f25ee501STomi Valkeinen 		tc->have_irq = true;
2114f25ee501STomi Valkeinen 	}
2115f25ee501STomi Valkeinen 
21167caff0fcSAndrey Gusakov 	ret = regmap_read(tc->regmap, TC_IDREG, &tc->rev);
21177caff0fcSAndrey Gusakov 	if (ret) {
21187caff0fcSAndrey Gusakov 		dev_err(tc->dev, "can not read device ID: %d\n", ret);
21197caff0fcSAndrey Gusakov 		return ret;
21207caff0fcSAndrey Gusakov 	}
21217caff0fcSAndrey Gusakov 
21227caff0fcSAndrey Gusakov 	if ((tc->rev != 0x6601) && (tc->rev != 0x6603)) {
21237caff0fcSAndrey Gusakov 		dev_err(tc->dev, "invalid device ID: 0x%08x\n", tc->rev);
21247caff0fcSAndrey Gusakov 		return -EINVAL;
21257caff0fcSAndrey Gusakov 	}
21267caff0fcSAndrey Gusakov 
21277caff0fcSAndrey Gusakov 	tc->assr = (tc->rev == 0x6601); /* Enable ASSR for eDP panels */
21287caff0fcSAndrey Gusakov 
212952c2197aSLucas Stach 	if (!tc->reset_gpio) {
213052c2197aSLucas Stach 		/*
213152c2197aSLucas Stach 		 * If the reset pin isn't present, do a software reset. It isn't
213252c2197aSLucas Stach 		 * as thorough as the hardware reset, as we can't reset the I2C
213352c2197aSLucas Stach 		 * communication block for obvious reasons, but it's getting the
213452c2197aSLucas Stach 		 * chip into a defined state.
213552c2197aSLucas Stach 		 */
213652c2197aSLucas Stach 		regmap_update_bits(tc->regmap, SYSRSTENB,
213752c2197aSLucas Stach 				ENBLCD0 | ENBBM | ENBDSIRX | ENBREG | ENBHDCP,
213852c2197aSLucas Stach 				0);
213952c2197aSLucas Stach 		regmap_update_bits(tc->regmap, SYSRSTENB,
214052c2197aSLucas Stach 				ENBLCD0 | ENBBM | ENBDSIRX | ENBREG | ENBHDCP,
214152c2197aSLucas Stach 				ENBLCD0 | ENBBM | ENBDSIRX | ENBREG | ENBHDCP);
214252c2197aSLucas Stach 		usleep_range(5000, 10000);
214352c2197aSLucas Stach 	}
214452c2197aSLucas Stach 
2145f25ee501STomi Valkeinen 	if (tc->hpd_pin >= 0) {
2146f25ee501STomi Valkeinen 		u32 lcnt_reg = tc->hpd_pin == 0 ? INT_GP0_LCNT : INT_GP1_LCNT;
2147f25ee501STomi Valkeinen 		u32 h_lc = INT_GPIO_H(tc->hpd_pin) | INT_GPIO_LC(tc->hpd_pin);
2148f25ee501STomi Valkeinen 
2149f25ee501STomi Valkeinen 		/* Set LCNT to 2ms */
2150f25ee501STomi Valkeinen 		regmap_write(tc->regmap, lcnt_reg,
2151f25ee501STomi Valkeinen 			     clk_get_rate(tc->refclk) * 2 / 1000);
2152f25ee501STomi Valkeinen 		/* We need the "alternate" mode for HPD */
2153f25ee501STomi Valkeinen 		regmap_write(tc->regmap, GPIOM, BIT(tc->hpd_pin));
2154f25ee501STomi Valkeinen 
2155f25ee501STomi Valkeinen 		if (tc->have_irq) {
2156f25ee501STomi Valkeinen 			/* enable H & LC */
2157f25ee501STomi Valkeinen 			regmap_update_bits(tc->regmap, INTCTL_G, h_lc, h_lc);
2158f25ee501STomi Valkeinen 		}
2159f25ee501STomi Valkeinen 	}
2160f25ee501STomi Valkeinen 
2161bbfd3190SMarek Vasut 	if (tc->bridge.type != DRM_MODE_CONNECTOR_DPI) { /* (e)DP output */
21627caff0fcSAndrey Gusakov 		ret = tc_aux_link_setup(tc);
21637caff0fcSAndrey Gusakov 		if (ret)
21647caff0fcSAndrey Gusakov 			return ret;
2165bbfd3190SMarek Vasut 	}
21667caff0fcSAndrey Gusakov 
21677caff0fcSAndrey Gusakov 	tc->bridge.of_node = dev->of_node;
2168dc01732eSInki Dae 	drm_bridge_add(&tc->bridge);
21697caff0fcSAndrey Gusakov 
21707caff0fcSAndrey Gusakov 	i2c_set_clientdata(client, tc);
21717caff0fcSAndrey Gusakov 
2172*3080c21aSMarek Vasut 	if (tc->input_connector_dsi) {			/* DSI input */
2173bbfd3190SMarek Vasut 		ret = tc_mipi_dsi_host_attach(tc);
2174bbfd3190SMarek Vasut 		if (ret) {
2175bbfd3190SMarek Vasut 			drm_bridge_remove(&tc->bridge);
2176bbfd3190SMarek Vasut 			return ret;
2177bbfd3190SMarek Vasut 		}
2178bbfd3190SMarek Vasut 	}
2179bbfd3190SMarek Vasut 
21807caff0fcSAndrey Gusakov 	return 0;
21817caff0fcSAndrey Gusakov }
21827caff0fcSAndrey Gusakov 
21837caff0fcSAndrey Gusakov static int tc_remove(struct i2c_client *client)
21847caff0fcSAndrey Gusakov {
21857caff0fcSAndrey Gusakov 	struct tc_data *tc = i2c_get_clientdata(client);
21867caff0fcSAndrey Gusakov 
21877caff0fcSAndrey Gusakov 	drm_bridge_remove(&tc->bridge);
21887caff0fcSAndrey Gusakov 
21897caff0fcSAndrey Gusakov 	return 0;
21907caff0fcSAndrey Gusakov }
21917caff0fcSAndrey Gusakov 
21927caff0fcSAndrey Gusakov static const struct i2c_device_id tc358767_i2c_ids[] = {
21937caff0fcSAndrey Gusakov 	{ "tc358767", 0 },
21947caff0fcSAndrey Gusakov 	{ }
21957caff0fcSAndrey Gusakov };
21967caff0fcSAndrey Gusakov MODULE_DEVICE_TABLE(i2c, tc358767_i2c_ids);
21977caff0fcSAndrey Gusakov 
21987caff0fcSAndrey Gusakov static const struct of_device_id tc358767_of_ids[] = {
21997caff0fcSAndrey Gusakov 	{ .compatible = "toshiba,tc358767", },
22007caff0fcSAndrey Gusakov 	{ }
22017caff0fcSAndrey Gusakov };
22027caff0fcSAndrey Gusakov MODULE_DEVICE_TABLE(of, tc358767_of_ids);
22037caff0fcSAndrey Gusakov 
22047caff0fcSAndrey Gusakov static struct i2c_driver tc358767_driver = {
22057caff0fcSAndrey Gusakov 	.driver = {
22067caff0fcSAndrey Gusakov 		.name = "tc358767",
22077caff0fcSAndrey Gusakov 		.of_match_table = tc358767_of_ids,
22087caff0fcSAndrey Gusakov 	},
22097caff0fcSAndrey Gusakov 	.id_table = tc358767_i2c_ids,
22107caff0fcSAndrey Gusakov 	.probe = tc_probe,
22117caff0fcSAndrey Gusakov 	.remove	= tc_remove,
22127caff0fcSAndrey Gusakov };
22137caff0fcSAndrey Gusakov module_i2c_driver(tc358767_driver);
22147caff0fcSAndrey Gusakov 
22157caff0fcSAndrey Gusakov MODULE_AUTHOR("Andrey Gusakov <andrey.gusakov@cogentembedded.com>");
22167caff0fcSAndrey Gusakov MODULE_DESCRIPTION("tc358767 eDP encoder driver");
22177caff0fcSAndrey Gusakov MODULE_LICENSE("GPL");
2218