xref: /openbmc/linux/drivers/gpu/drm/bridge/tc358767.c (revision ecc23d0a422a3118fcf6e4f0a46e17a6c2047b02)
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.
6c1de02bbSMarek Vasut  * All modes are supported -- DPI->(e)DP / DSI->DPI / DSI->(e)DP .
77caff0fcSAndrey Gusakov  *
87caff0fcSAndrey Gusakov  * Copyright (C) 2016 CogentEmbedded Inc
97caff0fcSAndrey Gusakov  * Author: Andrey Gusakov <andrey.gusakov@cogentembedded.com>
107caff0fcSAndrey Gusakov  *
117caff0fcSAndrey Gusakov  * Copyright (C) 2016 Pengutronix, Philipp Zabel <p.zabel@pengutronix.de>
127caff0fcSAndrey Gusakov  *
132f51be09SAndrey Gusakov  * Copyright (C) 2016 Zodiac Inflight Innovations
142f51be09SAndrey Gusakov  *
157caff0fcSAndrey Gusakov  * Initially based on: drivers/gpu/drm/i2c/tda998x_drv.c
167caff0fcSAndrey Gusakov  *
177caff0fcSAndrey Gusakov  * Copyright (C) 2012 Texas Instruments
187caff0fcSAndrey Gusakov  * Author: Rob Clark <robdclark@gmail.com>
197caff0fcSAndrey Gusakov  */
207caff0fcSAndrey Gusakov 
213f072c30SAndrey Smirnov #include <linux/bitfield.h>
227caff0fcSAndrey Gusakov #include <linux/clk.h>
237caff0fcSAndrey Gusakov #include <linux/device.h>
247caff0fcSAndrey Gusakov #include <linux/gpio/consumer.h>
257caff0fcSAndrey Gusakov #include <linux/i2c.h>
267caff0fcSAndrey Gusakov #include <linux/kernel.h>
2772bd9ea3SVille Syrjälä #include <linux/media-bus-format.h>
287caff0fcSAndrey Gusakov #include <linux/module.h>
297caff0fcSAndrey Gusakov #include <linux/regmap.h>
307caff0fcSAndrey Gusakov #include <linux/slab.h>
317caff0fcSAndrey Gusakov 
32da68386dSThomas Zimmermann #include <drm/display/drm_dp_helper.h>
337caff0fcSAndrey Gusakov #include <drm/drm_atomic_helper.h>
34ee68c743SBoris Brezillon #include <drm/drm_bridge.h>
357caff0fcSAndrey Gusakov #include <drm/drm_edid.h>
36bbfd3190SMarek Vasut #include <drm/drm_mipi_dsi.h>
377caff0fcSAndrey Gusakov #include <drm/drm_of.h>
387caff0fcSAndrey Gusakov #include <drm/drm_panel.h>
39a25b988fSLaurent Pinchart #include <drm/drm_print.h>
40fcd70cd3SDaniel Vetter #include <drm/drm_probe_helper.h>
417caff0fcSAndrey Gusakov 
427caff0fcSAndrey Gusakov /* Registers */
437caff0fcSAndrey Gusakov 
44bbfd3190SMarek Vasut /* PPI layer registers */
45bbfd3190SMarek Vasut #define PPI_STARTPPI		0x0104 /* START control bit */
46bbfd3190SMarek Vasut #define PPI_LPTXTIMECNT		0x0114 /* LPTX timing signal */
47bbfd3190SMarek Vasut #define LPX_PERIOD			3
48bbfd3190SMarek Vasut #define PPI_LANEENABLE		0x0134
49bbfd3190SMarek Vasut #define PPI_TX_RX_TA		0x013c
50bbfd3190SMarek Vasut #define TTA_GET				0x40000
51bbfd3190SMarek Vasut #define TTA_SURE			6
52bbfd3190SMarek Vasut #define PPI_D0S_ATMR		0x0144
53bbfd3190SMarek Vasut #define PPI_D1S_ATMR		0x0148
54bbfd3190SMarek Vasut #define PPI_D0S_CLRSIPOCOUNT	0x0164 /* Assertion timer for Lane 0 */
55bbfd3190SMarek Vasut #define PPI_D1S_CLRSIPOCOUNT	0x0168 /* Assertion timer for Lane 1 */
56bbfd3190SMarek Vasut #define PPI_D2S_CLRSIPOCOUNT	0x016c /* Assertion timer for Lane 2 */
57bbfd3190SMarek Vasut #define PPI_D3S_CLRSIPOCOUNT	0x0170 /* Assertion timer for Lane 3 */
58bbfd3190SMarek Vasut #define PPI_START_FUNCTION		BIT(0)
59bbfd3190SMarek Vasut 
60bbfd3190SMarek Vasut /* DSI layer registers */
61bbfd3190SMarek Vasut #define DSI_STARTDSI		0x0204 /* START control bit of DSI-TX */
62bbfd3190SMarek Vasut #define DSI_LANEENABLE		0x0210 /* Enables each lane */
63bbfd3190SMarek Vasut #define DSI_RX_START			BIT(0)
64bbfd3190SMarek Vasut 
65bbfd3190SMarek Vasut /* Lane enable PPI and DSI register bits */
66bbfd3190SMarek Vasut #define LANEENABLE_CLEN		BIT(0)
67bbfd3190SMarek Vasut #define LANEENABLE_L0EN		BIT(1)
68bbfd3190SMarek Vasut #define LANEENABLE_L1EN		BIT(2)
69bbfd3190SMarek Vasut #define LANEENABLE_L2EN		BIT(1)
70bbfd3190SMarek Vasut #define LANEENABLE_L3EN		BIT(2)
71bbfd3190SMarek Vasut 
72bbfd3190SMarek Vasut /* Display Parallel Input Interface */
737caff0fcSAndrey Gusakov #define DPIPXLFMT		0x0440
747caff0fcSAndrey Gusakov #define VS_POL_ACTIVE_LOW		(1 << 10)
757caff0fcSAndrey Gusakov #define HS_POL_ACTIVE_LOW		(1 << 9)
767caff0fcSAndrey Gusakov #define DE_POL_ACTIVE_HIGH		(0 << 8)
777caff0fcSAndrey Gusakov #define SUB_CFG_TYPE_CONFIG1		(0 << 2) /* LSB aligned */
787caff0fcSAndrey Gusakov #define SUB_CFG_TYPE_CONFIG2		(1 << 2) /* Loosely Packed */
797caff0fcSAndrey Gusakov #define SUB_CFG_TYPE_CONFIG3		(2 << 2) /* LSB aligned 8-bit */
807caff0fcSAndrey Gusakov #define DPI_BPP_RGB888			(0 << 0)
817caff0fcSAndrey Gusakov #define DPI_BPP_RGB666			(1 << 0)
827caff0fcSAndrey Gusakov #define DPI_BPP_RGB565			(2 << 0)
837caff0fcSAndrey Gusakov 
84bbfd3190SMarek Vasut /* Display Parallel Output Interface */
85bbfd3190SMarek Vasut #define POCTRL			0x0448
86bbfd3190SMarek Vasut #define POCTRL_S2P			BIT(7)
87bbfd3190SMarek Vasut #define POCTRL_PCLK_POL			BIT(3)
88bbfd3190SMarek Vasut #define POCTRL_VS_POL			BIT(2)
89bbfd3190SMarek Vasut #define POCTRL_HS_POL			BIT(1)
90bbfd3190SMarek Vasut #define POCTRL_DE_POL			BIT(0)
91bbfd3190SMarek Vasut 
927caff0fcSAndrey Gusakov /* Video Path */
937caff0fcSAndrey Gusakov #define VPCTRL0			0x0450
943f072c30SAndrey Smirnov #define VSDELAY			GENMASK(31, 20)
957caff0fcSAndrey Gusakov #define OPXLFMT_RGB666			(0 << 8)
967caff0fcSAndrey Gusakov #define OPXLFMT_RGB888			(1 << 8)
977caff0fcSAndrey Gusakov #define FRMSYNC_DISABLED		(0 << 4) /* Video Timing Gen Disabled */
987caff0fcSAndrey Gusakov #define FRMSYNC_ENABLED			(1 << 4) /* Video Timing Gen Enabled */
997caff0fcSAndrey Gusakov #define MSF_DISABLED			(0 << 0) /* Magic Square FRC disabled */
1007caff0fcSAndrey Gusakov #define MSF_ENABLED			(1 << 0) /* Magic Square FRC enabled */
1017caff0fcSAndrey Gusakov #define HTIM01			0x0454
1023f072c30SAndrey Smirnov #define HPW			GENMASK(8, 0)
1033f072c30SAndrey Smirnov #define HBPR			GENMASK(24, 16)
1047caff0fcSAndrey Gusakov #define HTIM02			0x0458
1053f072c30SAndrey Smirnov #define HDISPR			GENMASK(10, 0)
1063f072c30SAndrey Smirnov #define HFPR			GENMASK(24, 16)
1077caff0fcSAndrey Gusakov #define VTIM01			0x045c
1083f072c30SAndrey Smirnov #define VSPR			GENMASK(7, 0)
1093f072c30SAndrey Smirnov #define VBPR			GENMASK(23, 16)
1107caff0fcSAndrey Gusakov #define VTIM02			0x0460
1113f072c30SAndrey Smirnov #define VFPR			GENMASK(23, 16)
1123f072c30SAndrey Smirnov #define VDISPR			GENMASK(10, 0)
1137caff0fcSAndrey Gusakov #define VFUEN0			0x0464
1147caff0fcSAndrey Gusakov #define VFUEN				BIT(0)   /* Video Frame Timing Upload */
1157caff0fcSAndrey Gusakov 
1167caff0fcSAndrey Gusakov /* System */
1177caff0fcSAndrey Gusakov #define TC_IDREG		0x0500
118f25ee501STomi Valkeinen #define SYSSTAT			0x0508
1197caff0fcSAndrey Gusakov #define SYSCTRL			0x0510
1207caff0fcSAndrey Gusakov #define DP0_AUDSRC_NO_INPUT		(0 << 3)
1217caff0fcSAndrey Gusakov #define DP0_AUDSRC_I2S_RX		(1 << 3)
1227caff0fcSAndrey Gusakov #define DP0_VIDSRC_NO_INPUT		(0 << 0)
1237caff0fcSAndrey Gusakov #define DP0_VIDSRC_DSI_RX		(1 << 0)
1247caff0fcSAndrey Gusakov #define DP0_VIDSRC_DPI_RX		(2 << 0)
1257caff0fcSAndrey Gusakov #define DP0_VIDSRC_COLOR_BAR		(3 << 0)
12652c2197aSLucas Stach #define SYSRSTENB		0x050c
12752c2197aSLucas Stach #define ENBI2C				(1 << 0)
12852c2197aSLucas Stach #define ENBLCD0				(1 << 2)
12952c2197aSLucas Stach #define ENBBM				(1 << 3)
13052c2197aSLucas Stach #define ENBDSIRX			(1 << 4)
13152c2197aSLucas Stach #define ENBREG				(1 << 5)
13252c2197aSLucas Stach #define ENBHDCP				(1 << 8)
133af9526f2STomi Valkeinen #define GPIOM			0x0540
134f25ee501STomi Valkeinen #define GPIOC			0x0544
135f25ee501STomi Valkeinen #define GPIOO			0x0548
136af9526f2STomi Valkeinen #define GPIOI			0x054c
137af9526f2STomi Valkeinen #define INTCTL_G		0x0560
138af9526f2STomi Valkeinen #define INTSTS_G		0x0564
139f25ee501STomi Valkeinen 
140f25ee501STomi Valkeinen #define INT_SYSERR		BIT(16)
141f25ee501STomi Valkeinen #define INT_GPIO_H(x)		(1 << (x == 0 ? 2 : 10))
142f25ee501STomi Valkeinen #define INT_GPIO_LC(x)		(1 << (x == 0 ? 3 : 11))
143f25ee501STomi Valkeinen 
144af9526f2STomi Valkeinen #define INT_GP0_LCNT		0x0584
145af9526f2STomi Valkeinen #define INT_GP1_LCNT		0x0588
1467caff0fcSAndrey Gusakov 
1477caff0fcSAndrey Gusakov /* Control */
1487caff0fcSAndrey Gusakov #define DP0CTL			0x0600
1497caff0fcSAndrey Gusakov #define VID_MN_GEN			BIT(6)   /* Auto-generate M/N values */
1507caff0fcSAndrey Gusakov #define EF_EN				BIT(5)   /* Enable Enhanced Framing */
1517caff0fcSAndrey Gusakov #define VID_EN				BIT(1)   /* Video transmission enable */
1527caff0fcSAndrey Gusakov #define DP_EN				BIT(0)   /* Enable DPTX function */
1537caff0fcSAndrey Gusakov 
1547caff0fcSAndrey Gusakov /* Clocks */
1557caff0fcSAndrey Gusakov #define DP0_VIDMNGEN0		0x0610
1567caff0fcSAndrey Gusakov #define DP0_VIDMNGEN1		0x0614
1577caff0fcSAndrey Gusakov #define DP0_VMNGENSTATUS	0x0618
1587caff0fcSAndrey Gusakov 
1597caff0fcSAndrey Gusakov /* Main Channel */
1607caff0fcSAndrey Gusakov #define DP0_SECSAMPLE		0x0640
1617caff0fcSAndrey Gusakov #define DP0_VIDSYNCDELAY	0x0644
1623f072c30SAndrey Smirnov #define VID_SYNC_DLY		GENMASK(15, 0)
1633f072c30SAndrey Smirnov #define THRESH_DLY		GENMASK(31, 16)
1643f072c30SAndrey Smirnov 
1657caff0fcSAndrey Gusakov #define DP0_TOTALVAL		0x0648
1663f072c30SAndrey Smirnov #define H_TOTAL			GENMASK(15, 0)
1673f072c30SAndrey Smirnov #define V_TOTAL			GENMASK(31, 16)
1687caff0fcSAndrey Gusakov #define DP0_STARTVAL		0x064c
1693f072c30SAndrey Smirnov #define H_START			GENMASK(15, 0)
1703f072c30SAndrey Smirnov #define V_START			GENMASK(31, 16)
1717caff0fcSAndrey Gusakov #define DP0_ACTIVEVAL		0x0650
1723f072c30SAndrey Smirnov #define H_ACT			GENMASK(15, 0)
1733f072c30SAndrey Smirnov #define V_ACT			GENMASK(31, 16)
1743f072c30SAndrey Smirnov 
1757caff0fcSAndrey Gusakov #define DP0_SYNCVAL		0x0654
1763f072c30SAndrey Smirnov #define VS_WIDTH		GENMASK(30, 16)
1773f072c30SAndrey Smirnov #define HS_WIDTH		GENMASK(14, 0)
1787923e09cSTomi Valkeinen #define SYNCVAL_HS_POL_ACTIVE_LOW	(1 << 15)
1797923e09cSTomi Valkeinen #define SYNCVAL_VS_POL_ACTIVE_LOW	(1 << 31)
1807caff0fcSAndrey Gusakov #define DP0_MISC		0x0658
181f3b8adbeSAndrey Gusakov #define TU_SIZE_RECOMMENDED		(63) /* LSCLK cycles per TU */
1823f072c30SAndrey Smirnov #define MAX_TU_SYMBOL		GENMASK(28, 23)
1833f072c30SAndrey Smirnov #define TU_SIZE			GENMASK(21, 16)
1847caff0fcSAndrey Gusakov #define BPC_6				(0 << 5)
1857caff0fcSAndrey Gusakov #define BPC_8				(1 << 5)
1867caff0fcSAndrey Gusakov 
1877caff0fcSAndrey Gusakov /* AUX channel */
1887caff0fcSAndrey Gusakov #define DP0_AUXCFG0		0x0660
189fdb29b73SAndrey Smirnov #define DP0_AUXCFG0_BSIZE	GENMASK(11, 8)
190fdb29b73SAndrey Smirnov #define DP0_AUXCFG0_ADDR_ONLY	BIT(4)
1917caff0fcSAndrey Gusakov #define DP0_AUXCFG1		0x0664
1927caff0fcSAndrey Gusakov #define AUX_RX_FILTER_EN		BIT(16)
1937caff0fcSAndrey Gusakov 
1947caff0fcSAndrey Gusakov #define DP0_AUXADDR		0x0668
1957caff0fcSAndrey Gusakov #define DP0_AUXWDATA(i)		(0x066c + (i) * 4)
1967caff0fcSAndrey Gusakov #define DP0_AUXRDATA(i)		(0x067c + (i) * 4)
1977caff0fcSAndrey Gusakov #define DP0_AUXSTATUS		0x068c
19812dfe7c4SAndrey Smirnov #define AUX_BYTES		GENMASK(15, 8)
19912dfe7c4SAndrey Smirnov #define AUX_STATUS		GENMASK(7, 4)
2007caff0fcSAndrey Gusakov #define AUX_TIMEOUT		BIT(1)
2017caff0fcSAndrey Gusakov #define AUX_BUSY		BIT(0)
2027caff0fcSAndrey Gusakov #define DP0_AUXI2CADR		0x0698
2037caff0fcSAndrey Gusakov 
2047caff0fcSAndrey Gusakov /* Link Training */
2057caff0fcSAndrey Gusakov #define DP0_SRCCTRL		0x06a0
2067caff0fcSAndrey Gusakov #define DP0_SRCCTRL_SCRMBLDIS		BIT(13)
2077caff0fcSAndrey Gusakov #define DP0_SRCCTRL_EN810B		BIT(12)
2087caff0fcSAndrey Gusakov #define DP0_SRCCTRL_NOTP		(0 << 8)
2097caff0fcSAndrey Gusakov #define DP0_SRCCTRL_TP1			(1 << 8)
2107caff0fcSAndrey Gusakov #define DP0_SRCCTRL_TP2			(2 << 8)
2117caff0fcSAndrey Gusakov #define DP0_SRCCTRL_LANESKEW		BIT(7)
2127caff0fcSAndrey Gusakov #define DP0_SRCCTRL_SSCG		BIT(3)
2137caff0fcSAndrey Gusakov #define DP0_SRCCTRL_LANES_1		(0 << 2)
2147caff0fcSAndrey Gusakov #define DP0_SRCCTRL_LANES_2		(1 << 2)
2157caff0fcSAndrey Gusakov #define DP0_SRCCTRL_BW27		(1 << 1)
2167caff0fcSAndrey Gusakov #define DP0_SRCCTRL_BW162		(0 << 1)
2177caff0fcSAndrey Gusakov #define DP0_SRCCTRL_AUTOCORRECT		BIT(0)
2187caff0fcSAndrey Gusakov #define DP0_LTSTAT		0x06d0
2197caff0fcSAndrey Gusakov #define LT_LOOPDONE			BIT(13)
2207caff0fcSAndrey Gusakov #define LT_STATUS_MASK			(0x1f << 8)
2217caff0fcSAndrey Gusakov #define LT_CHANNEL1_EQ_BITS		(DP_CHANNEL_EQ_BITS << 4)
2227caff0fcSAndrey Gusakov #define LT_INTERLANE_ALIGN_DONE		BIT(3)
2237caff0fcSAndrey Gusakov #define LT_CHANNEL0_EQ_BITS		(DP_CHANNEL_EQ_BITS)
2247caff0fcSAndrey Gusakov #define DP0_SNKLTCHGREQ		0x06d4
2257caff0fcSAndrey Gusakov #define DP0_LTLOOPCTRL		0x06d8
2267caff0fcSAndrey Gusakov #define DP0_SNKLTCTRL		0x06e4
2277caff0fcSAndrey Gusakov 
228adf41098STomi Valkeinen #define DP1_SRCCTRL		0x07a0
229adf41098STomi Valkeinen 
2307caff0fcSAndrey Gusakov /* PHY */
2317caff0fcSAndrey Gusakov #define DP_PHY_CTRL		0x0800
2327caff0fcSAndrey Gusakov #define DP_PHY_RST			BIT(28)  /* DP PHY Global Soft Reset */
2337caff0fcSAndrey Gusakov #define BGREN				BIT(25)  /* AUX PHY BGR Enable */
2347caff0fcSAndrey Gusakov #define PWR_SW_EN			BIT(24)  /* PHY Power Switch Enable */
2357caff0fcSAndrey Gusakov #define PHY_M1_RST			BIT(12)  /* Reset PHY1 Main Channel */
2367caff0fcSAndrey Gusakov #define PHY_RDY				BIT(16)  /* PHY Main Channels Ready */
2377caff0fcSAndrey Gusakov #define PHY_M0_RST			BIT(8)   /* Reset PHY0 Main Channel */
238adf41098STomi Valkeinen #define PHY_2LANE			BIT(2)   /* PHY Enable 2 lanes */
2397caff0fcSAndrey Gusakov #define PHY_A0_EN			BIT(1)   /* PHY Aux Channel0 Enable */
2407caff0fcSAndrey Gusakov #define PHY_M0_EN			BIT(0)   /* PHY Main Channel0 Enable */
2417caff0fcSAndrey Gusakov 
2427caff0fcSAndrey Gusakov /* PLL */
2437caff0fcSAndrey Gusakov #define DP0_PLLCTRL		0x0900
2447caff0fcSAndrey Gusakov #define DP1_PLLCTRL		0x0904	/* not defined in DS */
2457caff0fcSAndrey Gusakov #define PXL_PLLCTRL		0x0908
2467caff0fcSAndrey Gusakov #define PLLUPDATE			BIT(2)
2477caff0fcSAndrey Gusakov #define PLLBYP				BIT(1)
2487caff0fcSAndrey Gusakov #define PLLEN				BIT(0)
2497caff0fcSAndrey Gusakov #define PXL_PLLPARAM		0x0914
2507caff0fcSAndrey Gusakov #define IN_SEL_REFCLK			(0 << 14)
2517caff0fcSAndrey Gusakov #define SYS_PLLPARAM		0x0918
2527caff0fcSAndrey Gusakov #define REF_FREQ_38M4			(0 << 8) /* 38.4 MHz */
2537caff0fcSAndrey Gusakov #define REF_FREQ_19M2			(1 << 8) /* 19.2 MHz */
2547caff0fcSAndrey Gusakov #define REF_FREQ_26M			(2 << 8) /* 26 MHz */
2557caff0fcSAndrey Gusakov #define REF_FREQ_13M			(3 << 8) /* 13 MHz */
2567caff0fcSAndrey Gusakov #define SYSCLK_SEL_LSCLK		(0 << 4)
2577caff0fcSAndrey Gusakov #define LSCLK_DIV_1			(0 << 0)
2587caff0fcSAndrey Gusakov #define LSCLK_DIV_2			(1 << 0)
2597caff0fcSAndrey Gusakov 
2607caff0fcSAndrey Gusakov /* Test & Debug */
2617caff0fcSAndrey Gusakov #define TSTCTL			0x0a00
2623f072c30SAndrey Smirnov #define COLOR_R			GENMASK(31, 24)
2633f072c30SAndrey Smirnov #define COLOR_G			GENMASK(23, 16)
2643f072c30SAndrey Smirnov #define COLOR_B			GENMASK(15, 8)
2653f072c30SAndrey Smirnov #define ENI2CFILTER		BIT(4)
2663f072c30SAndrey Smirnov #define COLOR_BAR_MODE		GENMASK(1, 0)
2673f072c30SAndrey Smirnov #define COLOR_BAR_MODE_BARS	2
2687caff0fcSAndrey Gusakov #define PLL_DBG			0x0a04
2697caff0fcSAndrey Gusakov 
2707caff0fcSAndrey Gusakov static bool tc_test_pattern;
2717caff0fcSAndrey Gusakov module_param_named(test, tc_test_pattern, bool, 0644);
2727caff0fcSAndrey Gusakov 
2737caff0fcSAndrey Gusakov struct tc_edp_link {
274e7dc8d40SThierry Reding 	u8			dpcd[DP_RECEIVER_CAP_SIZE];
275e7dc8d40SThierry Reding 	unsigned int		rate;
276e7dc8d40SThierry Reding 	u8			num_lanes;
2777caff0fcSAndrey Gusakov 	u8			assr;
278e5607637STomi Valkeinen 	bool			scrambler_dis;
279e5607637STomi Valkeinen 	bool			spread;
2807caff0fcSAndrey Gusakov };
2817caff0fcSAndrey Gusakov 
2827caff0fcSAndrey Gusakov struct tc_data {
2837caff0fcSAndrey Gusakov 	struct device		*dev;
2847caff0fcSAndrey Gusakov 	struct regmap		*regmap;
2857caff0fcSAndrey Gusakov 	struct drm_dp_aux	aux;
2867caff0fcSAndrey Gusakov 
2877caff0fcSAndrey Gusakov 	struct drm_bridge	bridge;
288de5e6c02SSam Ravnborg 	struct drm_bridge	*panel_bridge;
2897caff0fcSAndrey Gusakov 	struct drm_connector	connector;
2907caff0fcSAndrey Gusakov 
291bbfd3190SMarek Vasut 	struct mipi_dsi_device	*dsi;
292bbfd3190SMarek Vasut 
2937caff0fcSAndrey Gusakov 	/* link settings */
2947caff0fcSAndrey Gusakov 	struct tc_edp_link	link;
2957caff0fcSAndrey Gusakov 
2967caff0fcSAndrey Gusakov 	/* current mode */
29746648a3cSTomi Valkeinen 	struct drm_display_mode	mode;
2987caff0fcSAndrey Gusakov 
2997caff0fcSAndrey Gusakov 	u32			rev;
3007caff0fcSAndrey Gusakov 	u8			assr;
3017caff0fcSAndrey Gusakov 
3027caff0fcSAndrey Gusakov 	struct gpio_desc	*sd_gpio;
3037caff0fcSAndrey Gusakov 	struct gpio_desc	*reset_gpio;
3047caff0fcSAndrey Gusakov 	struct clk		*refclk;
305f25ee501STomi Valkeinen 
306f25ee501STomi Valkeinen 	/* do we have IRQ */
307f25ee501STomi Valkeinen 	bool			have_irq;
308f25ee501STomi Valkeinen 
3093080c21aSMarek Vasut 	/* Input connector type, DSI and not DPI. */
3103080c21aSMarek Vasut 	bool			input_connector_dsi;
3113080c21aSMarek Vasut 
312f25ee501STomi Valkeinen 	/* HPD pin number (0 or 1) or -ENODEV */
313f25ee501STomi Valkeinen 	int			hpd_pin;
3147caff0fcSAndrey Gusakov };
3157caff0fcSAndrey Gusakov 
aux_to_tc(struct drm_dp_aux * a)3167caff0fcSAndrey Gusakov static inline struct tc_data *aux_to_tc(struct drm_dp_aux *a)
3177caff0fcSAndrey Gusakov {
3187caff0fcSAndrey Gusakov 	return container_of(a, struct tc_data, aux);
3197caff0fcSAndrey Gusakov }
3207caff0fcSAndrey Gusakov 
bridge_to_tc(struct drm_bridge * b)3217caff0fcSAndrey Gusakov static inline struct tc_data *bridge_to_tc(struct drm_bridge *b)
3227caff0fcSAndrey Gusakov {
3237caff0fcSAndrey Gusakov 	return container_of(b, struct tc_data, bridge);
3247caff0fcSAndrey Gusakov }
3257caff0fcSAndrey Gusakov 
connector_to_tc(struct drm_connector * c)3267caff0fcSAndrey Gusakov static inline struct tc_data *connector_to_tc(struct drm_connector *c)
3277caff0fcSAndrey Gusakov {
3287caff0fcSAndrey Gusakov 	return container_of(c, struct tc_data, connector);
3297caff0fcSAndrey Gusakov }
3307caff0fcSAndrey Gusakov 
tc_poll_timeout(struct tc_data * tc,unsigned int addr,unsigned int cond_mask,unsigned int cond_value,unsigned long sleep_us,u64 timeout_us)33193a10569SAndrey Smirnov static inline int tc_poll_timeout(struct tc_data *tc, unsigned int addr,
3327caff0fcSAndrey Gusakov 				  unsigned int cond_mask,
3337caff0fcSAndrey Gusakov 				  unsigned int cond_value,
3347caff0fcSAndrey Gusakov 				  unsigned long sleep_us, u64 timeout_us)
3357caff0fcSAndrey Gusakov {
3367caff0fcSAndrey Gusakov 	unsigned int val;
3377caff0fcSAndrey Gusakov 
33893a10569SAndrey Smirnov 	return regmap_read_poll_timeout(tc->regmap, addr, val,
33993a10569SAndrey Smirnov 					(val & cond_mask) == cond_value,
34093a10569SAndrey Smirnov 					sleep_us, timeout_us);
3417caff0fcSAndrey Gusakov }
3427caff0fcSAndrey Gusakov 
tc_aux_wait_busy(struct tc_data * tc)34372648926SAndrey Smirnov static int tc_aux_wait_busy(struct tc_data *tc)
3447caff0fcSAndrey Gusakov {
3458a6483acSTomi Valkeinen 	return tc_poll_timeout(tc, DP0_AUXSTATUS, AUX_BUSY, 0, 100, 100000);
3467caff0fcSAndrey Gusakov }
3477caff0fcSAndrey Gusakov 
tc_aux_write_data(struct tc_data * tc,const void * data,size_t size)348792a081aSAndrey Smirnov static int tc_aux_write_data(struct tc_data *tc, const void *data,
349792a081aSAndrey Smirnov 			     size_t size)
350792a081aSAndrey Smirnov {
351792a081aSAndrey Smirnov 	u32 auxwdata[DP_AUX_MAX_PAYLOAD_BYTES / sizeof(u32)] = { 0 };
352792a081aSAndrey Smirnov 	int ret, count = ALIGN(size, sizeof(u32));
353792a081aSAndrey Smirnov 
354792a081aSAndrey Smirnov 	memcpy(auxwdata, data, size);
355792a081aSAndrey Smirnov 
356792a081aSAndrey Smirnov 	ret = regmap_raw_write(tc->regmap, DP0_AUXWDATA(0), auxwdata, count);
357792a081aSAndrey Smirnov 	if (ret)
358792a081aSAndrey Smirnov 		return ret;
359792a081aSAndrey Smirnov 
360792a081aSAndrey Smirnov 	return size;
361792a081aSAndrey Smirnov }
362792a081aSAndrey Smirnov 
tc_aux_read_data(struct tc_data * tc,void * data,size_t size)36353b166dcSAndrey Smirnov static int tc_aux_read_data(struct tc_data *tc, void *data, size_t size)
36453b166dcSAndrey Smirnov {
36553b166dcSAndrey Smirnov 	u32 auxrdata[DP_AUX_MAX_PAYLOAD_BYTES / sizeof(u32)];
36653b166dcSAndrey Smirnov 	int ret, count = ALIGN(size, sizeof(u32));
36753b166dcSAndrey Smirnov 
36853b166dcSAndrey Smirnov 	ret = regmap_raw_read(tc->regmap, DP0_AUXRDATA(0), auxrdata, count);
36953b166dcSAndrey Smirnov 	if (ret)
37053b166dcSAndrey Smirnov 		return ret;
37153b166dcSAndrey Smirnov 
37253b166dcSAndrey Smirnov 	memcpy(data, auxrdata, size);
37353b166dcSAndrey Smirnov 
37453b166dcSAndrey Smirnov 	return size;
37553b166dcSAndrey Smirnov }
37653b166dcSAndrey Smirnov 
tc_auxcfg0(struct drm_dp_aux_msg * msg,size_t size)377fdb29b73SAndrey Smirnov static u32 tc_auxcfg0(struct drm_dp_aux_msg *msg, size_t size)
378fdb29b73SAndrey Smirnov {
379fdb29b73SAndrey Smirnov 	u32 auxcfg0 = msg->request;
380fdb29b73SAndrey Smirnov 
381fdb29b73SAndrey Smirnov 	if (size)
382fdb29b73SAndrey Smirnov 		auxcfg0 |= FIELD_PREP(DP0_AUXCFG0_BSIZE, size - 1);
383fdb29b73SAndrey Smirnov 	else
384fdb29b73SAndrey Smirnov 		auxcfg0 |= DP0_AUXCFG0_ADDR_ONLY;
385fdb29b73SAndrey Smirnov 
386fdb29b73SAndrey Smirnov 	return auxcfg0;
387fdb29b73SAndrey Smirnov }
388fdb29b73SAndrey Smirnov 
tc_aux_transfer(struct drm_dp_aux * aux,struct drm_dp_aux_msg * msg)3897caff0fcSAndrey Gusakov static ssize_t tc_aux_transfer(struct drm_dp_aux *aux,
3907caff0fcSAndrey Gusakov 			       struct drm_dp_aux_msg *msg)
3917caff0fcSAndrey Gusakov {
3927caff0fcSAndrey Gusakov 	struct tc_data *tc = aux_to_tc(aux);
393e0655feaSAndrey Smirnov 	size_t size = min_t(size_t, DP_AUX_MAX_PAYLOAD_BYTES - 1, msg->size);
3947caff0fcSAndrey Gusakov 	u8 request = msg->request & ~DP_AUX_I2C_MOT;
39512dfe7c4SAndrey Smirnov 	u32 auxstatus;
3967caff0fcSAndrey Gusakov 	int ret;
3977caff0fcSAndrey Gusakov 
39872648926SAndrey Smirnov 	ret = tc_aux_wait_busy(tc);
3997caff0fcSAndrey Gusakov 	if (ret)
4006d0c3831SAndrey Smirnov 		return ret;
4017caff0fcSAndrey Gusakov 
402792a081aSAndrey Smirnov 	switch (request) {
403792a081aSAndrey Smirnov 	case DP_AUX_NATIVE_READ:
404792a081aSAndrey Smirnov 	case DP_AUX_I2C_READ:
405792a081aSAndrey Smirnov 		break;
406792a081aSAndrey Smirnov 	case DP_AUX_NATIVE_WRITE:
407792a081aSAndrey Smirnov 	case DP_AUX_I2C_WRITE:
408fdb29b73SAndrey Smirnov 		if (size) {
409792a081aSAndrey Smirnov 			ret = tc_aux_write_data(tc, msg->buffer, size);
410792a081aSAndrey Smirnov 			if (ret < 0)
4116d0c3831SAndrey Smirnov 				return ret;
412fdb29b73SAndrey Smirnov 		}
413792a081aSAndrey Smirnov 		break;
414792a081aSAndrey Smirnov 	default:
4157caff0fcSAndrey Gusakov 		return -EINVAL;
4167caff0fcSAndrey Gusakov 	}
4177caff0fcSAndrey Gusakov 
4187caff0fcSAndrey Gusakov 	/* Store address */
4196d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_AUXADDR, msg->address);
4206d0c3831SAndrey Smirnov 	if (ret)
4216d0c3831SAndrey Smirnov 		return ret;
4227caff0fcSAndrey Gusakov 	/* Start transfer */
423fdb29b73SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_AUXCFG0, tc_auxcfg0(msg, size));
4246d0c3831SAndrey Smirnov 	if (ret)
4256d0c3831SAndrey Smirnov 		return ret;
4267caff0fcSAndrey Gusakov 
42772648926SAndrey Smirnov 	ret = tc_aux_wait_busy(tc);
4287caff0fcSAndrey Gusakov 	if (ret)
4296d0c3831SAndrey Smirnov 		return ret;
4307caff0fcSAndrey Gusakov 
43112dfe7c4SAndrey Smirnov 	ret = regmap_read(tc->regmap, DP0_AUXSTATUS, &auxstatus);
4327caff0fcSAndrey Gusakov 	if (ret)
4336d0c3831SAndrey Smirnov 		return ret;
4347caff0fcSAndrey Gusakov 
43512dfe7c4SAndrey Smirnov 	if (auxstatus & AUX_TIMEOUT)
43612dfe7c4SAndrey Smirnov 		return -ETIMEDOUT;
437fdb29b73SAndrey Smirnov 	/*
438fdb29b73SAndrey Smirnov 	 * For some reason address-only DP_AUX_I2C_WRITE (MOT), still
439fdb29b73SAndrey Smirnov 	 * reports 1 byte transferred in its status. To deal we that
440fdb29b73SAndrey Smirnov 	 * we ignore aux_bytes field if we know that this was an
441fdb29b73SAndrey Smirnov 	 * address-only transfer
442fdb29b73SAndrey Smirnov 	 */
443fdb29b73SAndrey Smirnov 	if (size)
44412dfe7c4SAndrey Smirnov 		size = FIELD_GET(AUX_BYTES, auxstatus);
44512dfe7c4SAndrey Smirnov 	msg->reply = FIELD_GET(AUX_STATUS, auxstatus);
44612dfe7c4SAndrey Smirnov 
44753b166dcSAndrey Smirnov 	switch (request) {
44853b166dcSAndrey Smirnov 	case DP_AUX_NATIVE_READ:
44953b166dcSAndrey Smirnov 	case DP_AUX_I2C_READ:
450fdb29b73SAndrey Smirnov 		if (size)
45153b166dcSAndrey Smirnov 			return tc_aux_read_data(tc, msg->buffer, size);
452fdb29b73SAndrey Smirnov 		break;
4537caff0fcSAndrey Gusakov 	}
4547caff0fcSAndrey Gusakov 
4557caff0fcSAndrey Gusakov 	return size;
4567caff0fcSAndrey Gusakov }
4577caff0fcSAndrey Gusakov 
4587caff0fcSAndrey Gusakov static const char * const training_pattern1_errors[] = {
4597caff0fcSAndrey Gusakov 	"No errors",
4607caff0fcSAndrey Gusakov 	"Aux write error",
4617caff0fcSAndrey Gusakov 	"Aux read error",
4627caff0fcSAndrey Gusakov 	"Max voltage reached error",
4637caff0fcSAndrey Gusakov 	"Loop counter expired error",
4647caff0fcSAndrey Gusakov 	"res", "res", "res"
4657caff0fcSAndrey Gusakov };
4667caff0fcSAndrey Gusakov 
4677caff0fcSAndrey Gusakov static const char * const training_pattern2_errors[] = {
4687caff0fcSAndrey Gusakov 	"No errors",
4697caff0fcSAndrey Gusakov 	"Aux write error",
4707caff0fcSAndrey Gusakov 	"Aux read error",
4717caff0fcSAndrey Gusakov 	"Clock recovery failed error",
4727caff0fcSAndrey Gusakov 	"Loop counter expired error",
4737caff0fcSAndrey Gusakov 	"res", "res", "res"
4747caff0fcSAndrey Gusakov };
4757caff0fcSAndrey Gusakov 
tc_srcctrl(struct tc_data * tc)4767caff0fcSAndrey Gusakov static u32 tc_srcctrl(struct tc_data *tc)
4777caff0fcSAndrey Gusakov {
4787caff0fcSAndrey Gusakov 	/*
4797caff0fcSAndrey Gusakov 	 * No training pattern, skew lane 1 data by two LSCLK cycles with
4807caff0fcSAndrey Gusakov 	 * respect to lane 0 data, AutoCorrect Mode = 0
4817caff0fcSAndrey Gusakov 	 */
4824b30bf41STomi Valkeinen 	u32 reg = DP0_SRCCTRL_NOTP | DP0_SRCCTRL_LANESKEW | DP0_SRCCTRL_EN810B;
4837caff0fcSAndrey Gusakov 
4847caff0fcSAndrey Gusakov 	if (tc->link.scrambler_dis)
4857caff0fcSAndrey Gusakov 		reg |= DP0_SRCCTRL_SCRMBLDIS;	/* Scrambler Disabled */
4867caff0fcSAndrey Gusakov 	if (tc->link.spread)
4877caff0fcSAndrey Gusakov 		reg |= DP0_SRCCTRL_SSCG;	/* Spread Spectrum Enable */
488e7dc8d40SThierry Reding 	if (tc->link.num_lanes == 2)
4897caff0fcSAndrey Gusakov 		reg |= DP0_SRCCTRL_LANES_2;	/* Two Main Channel Lanes */
490e7dc8d40SThierry Reding 	if (tc->link.rate != 162000)
4917caff0fcSAndrey Gusakov 		reg |= DP0_SRCCTRL_BW27;	/* 2.7 Gbps link */
4927caff0fcSAndrey Gusakov 	return reg;
4937caff0fcSAndrey Gusakov }
4947caff0fcSAndrey Gusakov 
tc_pllupdate(struct tc_data * tc,unsigned int pllctrl)495134fb306SAndrey Smirnov static int tc_pllupdate(struct tc_data *tc, unsigned int pllctrl)
4967caff0fcSAndrey Gusakov {
497134fb306SAndrey Smirnov 	int ret;
498134fb306SAndrey Smirnov 
499134fb306SAndrey Smirnov 	ret = regmap_write(tc->regmap, pllctrl, PLLUPDATE | PLLEN);
500134fb306SAndrey Smirnov 	if (ret)
501134fb306SAndrey Smirnov 		return ret;
502134fb306SAndrey Smirnov 
50363fbe9dbSDavid Jander 	/* Wait for PLL to lock: up to 7.5 ms, depending on refclk */
50463fbe9dbSDavid Jander 	usleep_range(15000, 20000);
505134fb306SAndrey Smirnov 
506134fb306SAndrey Smirnov 	return 0;
5077caff0fcSAndrey Gusakov }
5087caff0fcSAndrey Gusakov 
tc_pxl_pll_en(struct tc_data * tc,u32 refclk,u32 pixelclock)5097caff0fcSAndrey Gusakov static int tc_pxl_pll_en(struct tc_data *tc, u32 refclk, u32 pixelclock)
5107caff0fcSAndrey Gusakov {
5117caff0fcSAndrey Gusakov 	int ret;
5127caff0fcSAndrey Gusakov 	int i_pre, best_pre = 1;
5137caff0fcSAndrey Gusakov 	int i_post, best_post = 1;
5147caff0fcSAndrey Gusakov 	int div, best_div = 1;
5157caff0fcSAndrey Gusakov 	int mul, best_mul = 1;
5167caff0fcSAndrey Gusakov 	int delta, best_delta;
5177caff0fcSAndrey Gusakov 	int ext_div[] = {1, 2, 3, 5, 7};
518bbfd3190SMarek Vasut 	int clk_min, clk_max;
5197caff0fcSAndrey Gusakov 	int best_pixelclock = 0;
5207caff0fcSAndrey Gusakov 	int vco_hi = 0;
5216d0c3831SAndrey Smirnov 	u32 pxl_pllparam;
5227caff0fcSAndrey Gusakov 
523bbfd3190SMarek Vasut 	/*
524bbfd3190SMarek Vasut 	 * refclk * mul / (ext_pre_div * pre_div) should be in range:
525bbfd3190SMarek Vasut 	 * - DPI ..... 0 to 100 MHz
526bbfd3190SMarek Vasut 	 * - (e)DP ... 150 to 650 MHz
527bbfd3190SMarek Vasut 	 */
528bbfd3190SMarek Vasut 	if (tc->bridge.type == DRM_MODE_CONNECTOR_DPI) {
529bbfd3190SMarek Vasut 		clk_min = 0;
530bbfd3190SMarek Vasut 		clk_max = 100000000;
531bbfd3190SMarek Vasut 	} else {
532bbfd3190SMarek Vasut 		clk_min = 150000000;
533bbfd3190SMarek Vasut 		clk_max = 650000000;
534bbfd3190SMarek Vasut 	}
535bbfd3190SMarek Vasut 
5367caff0fcSAndrey Gusakov 	dev_dbg(tc->dev, "PLL: requested %d pixelclock, ref %d\n", pixelclock,
5377caff0fcSAndrey Gusakov 		refclk);
5387caff0fcSAndrey Gusakov 	best_delta = pixelclock;
5397caff0fcSAndrey Gusakov 	/* Loop over all possible ext_divs, skipping invalid configurations */
5407caff0fcSAndrey Gusakov 	for (i_pre = 0; i_pre < ARRAY_SIZE(ext_div); i_pre++) {
5417caff0fcSAndrey Gusakov 		/*
5427caff0fcSAndrey Gusakov 		 * refclk / ext_pre_div should be in the 1 to 200 MHz range.
5437caff0fcSAndrey Gusakov 		 * We don't allow any refclk > 200 MHz, only check lower bounds.
5447caff0fcSAndrey Gusakov 		 */
5457caff0fcSAndrey Gusakov 		if (refclk / ext_div[i_pre] < 1000000)
5467caff0fcSAndrey Gusakov 			continue;
5477caff0fcSAndrey Gusakov 		for (i_post = 0; i_post < ARRAY_SIZE(ext_div); i_post++) {
5487caff0fcSAndrey Gusakov 			for (div = 1; div <= 16; div++) {
5497caff0fcSAndrey Gusakov 				u32 clk;
5507caff0fcSAndrey Gusakov 				u64 tmp;
5517caff0fcSAndrey Gusakov 
5527caff0fcSAndrey Gusakov 				tmp = pixelclock * ext_div[i_pre] *
5537caff0fcSAndrey Gusakov 				      ext_div[i_post] * div;
5547caff0fcSAndrey Gusakov 				do_div(tmp, refclk);
5557caff0fcSAndrey Gusakov 				mul = tmp;
5567caff0fcSAndrey Gusakov 
5577caff0fcSAndrey Gusakov 				/* Check limits */
5587caff0fcSAndrey Gusakov 				if ((mul < 1) || (mul > 128))
5597caff0fcSAndrey Gusakov 					continue;
5607caff0fcSAndrey Gusakov 
5617caff0fcSAndrey Gusakov 				clk = (refclk / ext_div[i_pre] / div) * mul;
562bbfd3190SMarek Vasut 				if ((clk > clk_max) || (clk < clk_min))
5637caff0fcSAndrey Gusakov 					continue;
5647caff0fcSAndrey Gusakov 
5657caff0fcSAndrey Gusakov 				clk = clk / ext_div[i_post];
5667caff0fcSAndrey Gusakov 				delta = clk - pixelclock;
5677caff0fcSAndrey Gusakov 
5687caff0fcSAndrey Gusakov 				if (abs(delta) < abs(best_delta)) {
5697caff0fcSAndrey Gusakov 					best_pre = i_pre;
5707caff0fcSAndrey Gusakov 					best_post = i_post;
5717caff0fcSAndrey Gusakov 					best_div = div;
5727caff0fcSAndrey Gusakov 					best_mul = mul;
5737caff0fcSAndrey Gusakov 					best_delta = delta;
5747caff0fcSAndrey Gusakov 					best_pixelclock = clk;
5757caff0fcSAndrey Gusakov 				}
5767caff0fcSAndrey Gusakov 			}
5777caff0fcSAndrey Gusakov 		}
5787caff0fcSAndrey Gusakov 	}
5797caff0fcSAndrey Gusakov 	if (best_pixelclock == 0) {
5807caff0fcSAndrey Gusakov 		dev_err(tc->dev, "Failed to calc clock for %d pixelclock\n",
5817caff0fcSAndrey Gusakov 			pixelclock);
5827caff0fcSAndrey Gusakov 		return -EINVAL;
5837caff0fcSAndrey Gusakov 	}
5847caff0fcSAndrey Gusakov 
5857caff0fcSAndrey Gusakov 	dev_dbg(tc->dev, "PLL: got %d, delta %d\n", best_pixelclock,
5867caff0fcSAndrey Gusakov 		best_delta);
5877caff0fcSAndrey Gusakov 	dev_dbg(tc->dev, "PLL: %d / %d / %d * %d / %d\n", refclk,
5887caff0fcSAndrey Gusakov 		ext_div[best_pre], best_div, best_mul, ext_div[best_post]);
5897caff0fcSAndrey Gusakov 
5907caff0fcSAndrey Gusakov 	/* if VCO >= 300 MHz */
5917caff0fcSAndrey Gusakov 	if (refclk / ext_div[best_pre] / best_div * best_mul >= 300000000)
5927caff0fcSAndrey Gusakov 		vco_hi = 1;
5937caff0fcSAndrey Gusakov 	/* see DS */
5947caff0fcSAndrey Gusakov 	if (best_div == 16)
5957caff0fcSAndrey Gusakov 		best_div = 0;
5967caff0fcSAndrey Gusakov 	if (best_mul == 128)
5977caff0fcSAndrey Gusakov 		best_mul = 0;
5987caff0fcSAndrey Gusakov 
5997caff0fcSAndrey Gusakov 	/* Power up PLL and switch to bypass */
6006d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, PXL_PLLCTRL, PLLBYP | PLLEN);
6016d0c3831SAndrey Smirnov 	if (ret)
6026d0c3831SAndrey Smirnov 		return ret;
6037caff0fcSAndrey Gusakov 
6046d0c3831SAndrey Smirnov 	pxl_pllparam  = vco_hi << 24; /* For PLL VCO >= 300 MHz = 1 */
6056d0c3831SAndrey Smirnov 	pxl_pllparam |= ext_div[best_pre] << 20; /* External Pre-divider */
6066d0c3831SAndrey Smirnov 	pxl_pllparam |= ext_div[best_post] << 16; /* External Post-divider */
6076d0c3831SAndrey Smirnov 	pxl_pllparam |= IN_SEL_REFCLK; /* Use RefClk as PLL input */
6086d0c3831SAndrey Smirnov 	pxl_pllparam |= best_div << 8; /* Divider for PLL RefClk */
6096d0c3831SAndrey Smirnov 	pxl_pllparam |= best_mul; /* Multiplier for PLL */
6106d0c3831SAndrey Smirnov 
6116d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, PXL_PLLPARAM, pxl_pllparam);
6126d0c3831SAndrey Smirnov 	if (ret)
6136d0c3831SAndrey Smirnov 		return ret;
6147caff0fcSAndrey Gusakov 
6157caff0fcSAndrey Gusakov 	/* Force PLL parameter update and disable bypass */
616134fb306SAndrey Smirnov 	return tc_pllupdate(tc, PXL_PLLCTRL);
6177caff0fcSAndrey Gusakov }
6187caff0fcSAndrey Gusakov 
tc_pxl_pll_dis(struct tc_data * tc)6197caff0fcSAndrey Gusakov static int tc_pxl_pll_dis(struct tc_data *tc)
6207caff0fcSAndrey Gusakov {
6217caff0fcSAndrey Gusakov 	/* Enable PLL bypass, power down PLL */
6227caff0fcSAndrey Gusakov 	return regmap_write(tc->regmap, PXL_PLLCTRL, PLLBYP);
6237caff0fcSAndrey Gusakov }
6247caff0fcSAndrey Gusakov 
tc_stream_clock_calc(struct tc_data * tc)6257caff0fcSAndrey Gusakov static int tc_stream_clock_calc(struct tc_data *tc)
6267caff0fcSAndrey Gusakov {
6277caff0fcSAndrey Gusakov 	/*
6287caff0fcSAndrey Gusakov 	 * If the Stream clock and Link Symbol clock are
6297caff0fcSAndrey Gusakov 	 * asynchronous with each other, the value of M changes over
6307caff0fcSAndrey Gusakov 	 * time. This way of generating link clock and stream
6317caff0fcSAndrey Gusakov 	 * clock is called Asynchronous Clock mode. The value M
6327caff0fcSAndrey Gusakov 	 * must change while the value N stays constant. The
6337caff0fcSAndrey Gusakov 	 * value of N in this Asynchronous Clock mode must be set
6347caff0fcSAndrey Gusakov 	 * to 2^15 or 32,768.
6357caff0fcSAndrey Gusakov 	 *
6367caff0fcSAndrey Gusakov 	 * LSCLK = 1/10 of high speed link clock
6377caff0fcSAndrey Gusakov 	 *
6387caff0fcSAndrey Gusakov 	 * f_STRMCLK = M/N * f_LSCLK
6397caff0fcSAndrey Gusakov 	 * M/N = f_STRMCLK / f_LSCLK
6407caff0fcSAndrey Gusakov 	 *
6417caff0fcSAndrey Gusakov 	 */
6426d0c3831SAndrey Smirnov 	return regmap_write(tc->regmap, DP0_VIDMNGEN1, 32768);
6437caff0fcSAndrey Gusakov }
6447caff0fcSAndrey Gusakov 
tc_set_syspllparam(struct tc_data * tc)645c49f60dfSAndrey Smirnov static int tc_set_syspllparam(struct tc_data *tc)
6467caff0fcSAndrey Gusakov {
6477caff0fcSAndrey Gusakov 	unsigned long rate;
648c49f60dfSAndrey Smirnov 	u32 pllparam = SYSCLK_SEL_LSCLK | LSCLK_DIV_2;
6497caff0fcSAndrey Gusakov 
6507caff0fcSAndrey Gusakov 	rate = clk_get_rate(tc->refclk);
6517caff0fcSAndrey Gusakov 	switch (rate) {
6527caff0fcSAndrey Gusakov 	case 38400000:
653c49f60dfSAndrey Smirnov 		pllparam |= REF_FREQ_38M4;
6547caff0fcSAndrey Gusakov 		break;
6557caff0fcSAndrey Gusakov 	case 26000000:
656c49f60dfSAndrey Smirnov 		pllparam |= REF_FREQ_26M;
6577caff0fcSAndrey Gusakov 		break;
6587caff0fcSAndrey Gusakov 	case 19200000:
659c49f60dfSAndrey Smirnov 		pllparam |= REF_FREQ_19M2;
6607caff0fcSAndrey Gusakov 		break;
6617caff0fcSAndrey Gusakov 	case 13000000:
662c49f60dfSAndrey Smirnov 		pllparam |= REF_FREQ_13M;
6637caff0fcSAndrey Gusakov 		break;
6647caff0fcSAndrey Gusakov 	default:
6657caff0fcSAndrey Gusakov 		dev_err(tc->dev, "Invalid refclk rate: %lu Hz\n", rate);
6667caff0fcSAndrey Gusakov 		return -EINVAL;
6677caff0fcSAndrey Gusakov 	}
6687caff0fcSAndrey Gusakov 
669c49f60dfSAndrey Smirnov 	return regmap_write(tc->regmap, SYS_PLLPARAM, pllparam);
670c49f60dfSAndrey Smirnov }
671c49f60dfSAndrey Smirnov 
tc_aux_link_setup(struct tc_data * tc)672c49f60dfSAndrey Smirnov static int tc_aux_link_setup(struct tc_data *tc)
673c49f60dfSAndrey Smirnov {
674c49f60dfSAndrey Smirnov 	int ret;
675c49f60dfSAndrey Smirnov 	u32 dp0_auxcfg1;
676c49f60dfSAndrey Smirnov 
6777caff0fcSAndrey Gusakov 	/* Setup DP-PHY / PLL */
678c49f60dfSAndrey Smirnov 	ret = tc_set_syspllparam(tc);
6796d0c3831SAndrey Smirnov 	if (ret)
6806d0c3831SAndrey Smirnov 		goto err;
6817caff0fcSAndrey Gusakov 
6826d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP_PHY_CTRL,
6836d0c3831SAndrey Smirnov 			   BGREN | PWR_SW_EN | PHY_A0_EN);
6846d0c3831SAndrey Smirnov 	if (ret)
6856d0c3831SAndrey Smirnov 		goto err;
6867caff0fcSAndrey Gusakov 	/*
6877caff0fcSAndrey Gusakov 	 * Initially PLLs are in bypass. Force PLL parameter update,
6887caff0fcSAndrey Gusakov 	 * disable PLL bypass, enable PLL
6897caff0fcSAndrey Gusakov 	 */
690134fb306SAndrey Smirnov 	ret = tc_pllupdate(tc, DP0_PLLCTRL);
6916d0c3831SAndrey Smirnov 	if (ret)
6926d0c3831SAndrey Smirnov 		goto err;
6937caff0fcSAndrey Gusakov 
694134fb306SAndrey Smirnov 	ret = tc_pllupdate(tc, DP1_PLLCTRL);
6956d0c3831SAndrey Smirnov 	if (ret)
6966d0c3831SAndrey Smirnov 		goto err;
6977caff0fcSAndrey Gusakov 
6988a6483acSTomi Valkeinen 	ret = tc_poll_timeout(tc, DP_PHY_CTRL, PHY_RDY, PHY_RDY, 100, 100000);
6997caff0fcSAndrey Gusakov 	if (ret == -ETIMEDOUT) {
7007caff0fcSAndrey Gusakov 		dev_err(tc->dev, "Timeout waiting for PHY to become ready");
7017caff0fcSAndrey Gusakov 		return ret;
702ca342386STomi Valkeinen 	} else if (ret) {
7037caff0fcSAndrey Gusakov 		goto err;
704ca342386STomi Valkeinen 	}
7057caff0fcSAndrey Gusakov 
7067caff0fcSAndrey Gusakov 	/* Setup AUX link */
7076d0c3831SAndrey Smirnov 	dp0_auxcfg1  = AUX_RX_FILTER_EN;
7086d0c3831SAndrey Smirnov 	dp0_auxcfg1 |= 0x06 << 8; /* Aux Bit Period Calculator Threshold */
7096d0c3831SAndrey Smirnov 	dp0_auxcfg1 |= 0x3f << 0; /* Aux Response Timeout Timer */
7106d0c3831SAndrey Smirnov 
7116d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_AUXCFG1, dp0_auxcfg1);
7126d0c3831SAndrey Smirnov 	if (ret)
7136d0c3831SAndrey Smirnov 		goto err;
7147caff0fcSAndrey Gusakov 
715824c7bb4SMarek Vasut 	/* Register DP AUX channel */
716824c7bb4SMarek Vasut 	tc->aux.name = "TC358767 AUX i2c adapter";
717824c7bb4SMarek Vasut 	tc->aux.dev = tc->dev;
718824c7bb4SMarek Vasut 	tc->aux.transfer = tc_aux_transfer;
719824c7bb4SMarek Vasut 	drm_dp_aux_init(&tc->aux);
720824c7bb4SMarek Vasut 
7217caff0fcSAndrey Gusakov 	return 0;
7227caff0fcSAndrey Gusakov err:
7237caff0fcSAndrey Gusakov 	dev_err(tc->dev, "tc_aux_link_setup failed: %d\n", ret);
7247caff0fcSAndrey Gusakov 	return ret;
7257caff0fcSAndrey Gusakov }
7267caff0fcSAndrey Gusakov 
tc_get_display_props(struct tc_data * tc)7277caff0fcSAndrey Gusakov static int tc_get_display_props(struct tc_data *tc)
7287caff0fcSAndrey Gusakov {
729e7dc8d40SThierry Reding 	u8 revision, num_lanes;
730e7dc8d40SThierry Reding 	unsigned int rate;
7317caff0fcSAndrey Gusakov 	int ret;
732d174db07SAndrey Smirnov 	u8 reg;
7337caff0fcSAndrey Gusakov 
7347caff0fcSAndrey Gusakov 	/* Read DP Rx Link Capability */
735e7dc8d40SThierry Reding 	ret = drm_dp_dpcd_read(&tc->aux, DP_DPCD_REV, tc->link.dpcd,
736e7dc8d40SThierry Reding 			       DP_RECEIVER_CAP_SIZE);
7377caff0fcSAndrey Gusakov 	if (ret < 0)
7387caff0fcSAndrey Gusakov 		goto err_dpcd_read;
739e7dc8d40SThierry Reding 
740e7dc8d40SThierry Reding 	revision = tc->link.dpcd[DP_DPCD_REV];
741e7dc8d40SThierry Reding 	rate = drm_dp_max_link_rate(tc->link.dpcd);
742e7dc8d40SThierry Reding 	num_lanes = drm_dp_max_lane_count(tc->link.dpcd);
743e7dc8d40SThierry Reding 
744e7dc8d40SThierry Reding 	if (rate != 162000 && rate != 270000) {
745cffd2b16SAndrey Gusakov 		dev_dbg(tc->dev, "Falling to 2.7 Gbps rate\n");
746e7dc8d40SThierry Reding 		rate = 270000;
747cffd2b16SAndrey Gusakov 	}
748cffd2b16SAndrey Gusakov 
749e7dc8d40SThierry Reding 	tc->link.rate = rate;
750e7dc8d40SThierry Reding 
751e7dc8d40SThierry Reding 	if (num_lanes > 2) {
752cffd2b16SAndrey Gusakov 		dev_dbg(tc->dev, "Falling to 2 lanes\n");
753e7dc8d40SThierry Reding 		num_lanes = 2;
754cffd2b16SAndrey Gusakov 	}
7557caff0fcSAndrey Gusakov 
756e7dc8d40SThierry Reding 	tc->link.num_lanes = num_lanes;
757e7dc8d40SThierry Reding 
758d174db07SAndrey Smirnov 	ret = drm_dp_dpcd_readb(&tc->aux, DP_MAX_DOWNSPREAD, &reg);
7597caff0fcSAndrey Gusakov 	if (ret < 0)
7607caff0fcSAndrey Gusakov 		goto err_dpcd_read;
761d174db07SAndrey Smirnov 	tc->link.spread = reg & DP_MAX_DOWNSPREAD_0_5;
7627caff0fcSAndrey Gusakov 
763d174db07SAndrey Smirnov 	ret = drm_dp_dpcd_readb(&tc->aux, DP_MAIN_LINK_CHANNEL_CODING, &reg);
7647caff0fcSAndrey Gusakov 	if (ret < 0)
7657caff0fcSAndrey Gusakov 		goto err_dpcd_read;
7664b30bf41STomi Valkeinen 
767e5607637STomi Valkeinen 	tc->link.scrambler_dis = false;
7687caff0fcSAndrey Gusakov 	/* read assr */
769d174db07SAndrey Smirnov 	ret = drm_dp_dpcd_readb(&tc->aux, DP_EDP_CONFIGURATION_SET, &reg);
7707caff0fcSAndrey Gusakov 	if (ret < 0)
7717caff0fcSAndrey Gusakov 		goto err_dpcd_read;
772d174db07SAndrey Smirnov 	tc->link.assr = reg & DP_ALTERNATE_SCRAMBLER_RESET_ENABLE;
7737caff0fcSAndrey Gusakov 
7747caff0fcSAndrey Gusakov 	dev_dbg(tc->dev, "DPCD rev: %d.%d, rate: %s, lanes: %d, framing: %s\n",
775e7dc8d40SThierry Reding 		revision >> 4, revision & 0x0f,
776e7dc8d40SThierry Reding 		(tc->link.rate == 162000) ? "1.62Gbps" : "2.7Gbps",
777e7dc8d40SThierry Reding 		tc->link.num_lanes,
778e7dc8d40SThierry Reding 		drm_dp_enhanced_frame_cap(tc->link.dpcd) ?
77998bca69bSThierry Reding 		"enhanced" : "default");
780e5607637STomi Valkeinen 	dev_dbg(tc->dev, "Downspread: %s, scrambler: %s\n",
781e5607637STomi Valkeinen 		tc->link.spread ? "0.5%" : "0.0%",
782e5607637STomi Valkeinen 		tc->link.scrambler_dis ? "disabled" : "enabled");
7837caff0fcSAndrey Gusakov 	dev_dbg(tc->dev, "Display ASSR: %d, TC358767 ASSR: %d\n",
7847caff0fcSAndrey Gusakov 		tc->link.assr, tc->assr);
7857caff0fcSAndrey Gusakov 
7867caff0fcSAndrey Gusakov 	return 0;
7877caff0fcSAndrey Gusakov 
7887caff0fcSAndrey Gusakov err_dpcd_read:
7897caff0fcSAndrey Gusakov 	dev_err(tc->dev, "failed to read DPCD: %d\n", ret);
7907caff0fcSAndrey Gusakov 	return ret;
7917caff0fcSAndrey Gusakov }
7927caff0fcSAndrey Gusakov 
tc_set_common_video_mode(struct tc_data * tc,const struct drm_display_mode * mode)793aebe58a7SMarek Vasut static int tc_set_common_video_mode(struct tc_data *tc,
79463f8f3baSLaurent Pinchart 				    const struct drm_display_mode *mode)
7957caff0fcSAndrey Gusakov {
7967caff0fcSAndrey Gusakov 	int left_margin = mode->htotal - mode->hsync_end;
7977caff0fcSAndrey Gusakov 	int right_margin = mode->hsync_start - mode->hdisplay;
7987caff0fcSAndrey Gusakov 	int hsync_len = mode->hsync_end - mode->hsync_start;
7997caff0fcSAndrey Gusakov 	int upper_margin = mode->vtotal - mode->vsync_end;
8007caff0fcSAndrey Gusakov 	int lower_margin = mode->vsync_start - mode->vdisplay;
8017caff0fcSAndrey Gusakov 	int vsync_len = mode->vsync_end - mode->vsync_start;
802aebe58a7SMarek Vasut 	int ret;
80366d1c3b9SAndrey Gusakov 
8047caff0fcSAndrey Gusakov 	dev_dbg(tc->dev, "set mode %dx%d\n",
8057caff0fcSAndrey Gusakov 		mode->hdisplay, mode->vdisplay);
8067caff0fcSAndrey Gusakov 	dev_dbg(tc->dev, "H margin %d,%d sync %d\n",
8077caff0fcSAndrey Gusakov 		left_margin, right_margin, hsync_len);
8087caff0fcSAndrey Gusakov 	dev_dbg(tc->dev, "V margin %d,%d sync %d\n",
8097caff0fcSAndrey Gusakov 		upper_margin, lower_margin, vsync_len);
8107caff0fcSAndrey Gusakov 	dev_dbg(tc->dev, "total: %dx%d\n", mode->htotal, mode->vtotal);
8117caff0fcSAndrey Gusakov 
8127caff0fcSAndrey Gusakov 
81366d1c3b9SAndrey Gusakov 	/*
81466d1c3b9SAndrey Gusakov 	 * LCD Ctl Frame Size
81566d1c3b9SAndrey Gusakov 	 * datasheet is not clear of vsdelay in case of DPI
81666d1c3b9SAndrey Gusakov 	 * assume we do not need any delay when DPI is a source of
81766d1c3b9SAndrey Gusakov 	 * sync signals
81866d1c3b9SAndrey Gusakov 	 */
8196d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, VPCTRL0,
82085a241cbSDavid Jander 			   FIELD_PREP(VSDELAY, right_margin + 10) |
8217caff0fcSAndrey Gusakov 			   OPXLFMT_RGB888 | FRMSYNC_DISABLED | MSF_DISABLED);
8226d0c3831SAndrey Smirnov 	if (ret)
8236d0c3831SAndrey Smirnov 		return ret;
8246d0c3831SAndrey Smirnov 
8256d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, HTIM01,
8263f072c30SAndrey Smirnov 			   FIELD_PREP(HBPR, ALIGN(left_margin, 2)) |
8273f072c30SAndrey Smirnov 			   FIELD_PREP(HPW, ALIGN(hsync_len, 2)));
8286d0c3831SAndrey Smirnov 	if (ret)
8296d0c3831SAndrey Smirnov 		return ret;
8306d0c3831SAndrey Smirnov 
8316d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, HTIM02,
8323f072c30SAndrey Smirnov 			   FIELD_PREP(HDISPR, ALIGN(mode->hdisplay, 2)) |
8333f072c30SAndrey Smirnov 			   FIELD_PREP(HFPR, ALIGN(right_margin, 2)));
8346d0c3831SAndrey Smirnov 	if (ret)
8356d0c3831SAndrey Smirnov 		return ret;
8366d0c3831SAndrey Smirnov 
8376d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, VTIM01,
8383f072c30SAndrey Smirnov 			   FIELD_PREP(VBPR, upper_margin) |
8393f072c30SAndrey Smirnov 			   FIELD_PREP(VSPR, vsync_len));
8406d0c3831SAndrey Smirnov 	if (ret)
8416d0c3831SAndrey Smirnov 		return ret;
8426d0c3831SAndrey Smirnov 
8436d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, VTIM02,
8443f072c30SAndrey Smirnov 			   FIELD_PREP(VFPR, lower_margin) |
8453f072c30SAndrey Smirnov 			   FIELD_PREP(VDISPR, mode->vdisplay));
8466d0c3831SAndrey Smirnov 	if (ret)
8476d0c3831SAndrey Smirnov 		return ret;
8486d0c3831SAndrey Smirnov 
8496d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, VFUEN0, VFUEN); /* update settings */
8506d0c3831SAndrey Smirnov 	if (ret)
8516d0c3831SAndrey Smirnov 		return ret;
8527caff0fcSAndrey Gusakov 
8537caff0fcSAndrey Gusakov 	/* Test pattern settings */
8546d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, TSTCTL,
8553f072c30SAndrey Smirnov 			   FIELD_PREP(COLOR_R, 120) |
8563f072c30SAndrey Smirnov 			   FIELD_PREP(COLOR_G, 20) |
8573f072c30SAndrey Smirnov 			   FIELD_PREP(COLOR_B, 99) |
8583f072c30SAndrey Smirnov 			   ENI2CFILTER |
8593f072c30SAndrey Smirnov 			   FIELD_PREP(COLOR_BAR_MODE, COLOR_BAR_MODE_BARS));
860aebe58a7SMarek Vasut 
8616d0c3831SAndrey Smirnov 	return ret;
862aebe58a7SMarek Vasut }
863aebe58a7SMarek Vasut 
tc_set_dpi_video_mode(struct tc_data * tc,const struct drm_display_mode * mode)864bbfd3190SMarek Vasut static int tc_set_dpi_video_mode(struct tc_data *tc,
865bbfd3190SMarek Vasut 				 const struct drm_display_mode *mode)
866bbfd3190SMarek Vasut {
867bbfd3190SMarek Vasut 	u32 value = POCTRL_S2P;
868bbfd3190SMarek Vasut 
869bbfd3190SMarek Vasut 	if (tc->mode.flags & DRM_MODE_FLAG_NHSYNC)
870bbfd3190SMarek Vasut 		value |= POCTRL_HS_POL;
871bbfd3190SMarek Vasut 
872bbfd3190SMarek Vasut 	if (tc->mode.flags & DRM_MODE_FLAG_NVSYNC)
873bbfd3190SMarek Vasut 		value |= POCTRL_VS_POL;
874bbfd3190SMarek Vasut 
875bbfd3190SMarek Vasut 	return regmap_write(tc->regmap, POCTRL, value);
876bbfd3190SMarek Vasut }
877bbfd3190SMarek Vasut 
tc_set_edp_video_mode(struct tc_data * tc,const struct drm_display_mode * mode)878aebe58a7SMarek Vasut static int tc_set_edp_video_mode(struct tc_data *tc,
879aebe58a7SMarek Vasut 				 const struct drm_display_mode *mode)
880aebe58a7SMarek Vasut {
881aebe58a7SMarek Vasut 	int ret;
882aebe58a7SMarek Vasut 	int vid_sync_dly;
883aebe58a7SMarek Vasut 	int max_tu_symbol;
884aebe58a7SMarek Vasut 
885aebe58a7SMarek Vasut 	int left_margin = mode->htotal - mode->hsync_end;
886aebe58a7SMarek Vasut 	int hsync_len = mode->hsync_end - mode->hsync_start;
887aebe58a7SMarek Vasut 	int upper_margin = mode->vtotal - mode->vsync_end;
888aebe58a7SMarek Vasut 	int vsync_len = mode->vsync_end - mode->vsync_start;
889aebe58a7SMarek Vasut 	u32 dp0_syncval;
890aebe58a7SMarek Vasut 	u32 bits_per_pixel = 24;
891aebe58a7SMarek Vasut 	u32 in_bw, out_bw;
8925fa9e161SLucas Stach 	u32 dpipxlfmt;
893aebe58a7SMarek Vasut 
894aebe58a7SMarek Vasut 	/*
895aebe58a7SMarek Vasut 	 * Recommended maximum number of symbols transferred in a transfer unit:
896aebe58a7SMarek Vasut 	 * DIV_ROUND_UP((input active video bandwidth in bytes) * tu_size,
897aebe58a7SMarek Vasut 	 *              (output active video bandwidth in bytes))
898aebe58a7SMarek Vasut 	 * Must be less than tu_size.
899aebe58a7SMarek Vasut 	 */
900aebe58a7SMarek Vasut 
901aebe58a7SMarek Vasut 	in_bw = mode->clock * bits_per_pixel / 8;
902aebe58a7SMarek Vasut 	out_bw = tc->link.num_lanes * tc->link.rate;
903aebe58a7SMarek Vasut 	max_tu_symbol = DIV_ROUND_UP(in_bw * TU_SIZE_RECOMMENDED, out_bw);
9047caff0fcSAndrey Gusakov 
9057caff0fcSAndrey Gusakov 	/* DP Main Stream Attributes */
9067caff0fcSAndrey Gusakov 	vid_sync_dly = hsync_len + left_margin + mode->hdisplay;
9076d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_VIDSYNCDELAY,
9083f072c30SAndrey Smirnov 		 FIELD_PREP(THRESH_DLY, max_tu_symbol) |
9093f072c30SAndrey Smirnov 		 FIELD_PREP(VID_SYNC_DLY, vid_sync_dly));
9107caff0fcSAndrey Gusakov 
9116d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_TOTALVAL,
9123f072c30SAndrey Smirnov 			   FIELD_PREP(H_TOTAL, mode->htotal) |
9133f072c30SAndrey Smirnov 			   FIELD_PREP(V_TOTAL, mode->vtotal));
9146d0c3831SAndrey Smirnov 	if (ret)
9156d0c3831SAndrey Smirnov 		return ret;
9167caff0fcSAndrey Gusakov 
9176d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_STARTVAL,
9183f072c30SAndrey Smirnov 			   FIELD_PREP(H_START, left_margin + hsync_len) |
9193f072c30SAndrey Smirnov 			   FIELD_PREP(V_START, upper_margin + vsync_len));
9206d0c3831SAndrey Smirnov 	if (ret)
9216d0c3831SAndrey Smirnov 		return ret;
9227caff0fcSAndrey Gusakov 
9236d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_ACTIVEVAL,
9243f072c30SAndrey Smirnov 			   FIELD_PREP(V_ACT, mode->vdisplay) |
9253f072c30SAndrey Smirnov 			   FIELD_PREP(H_ACT, mode->hdisplay));
9266d0c3831SAndrey Smirnov 	if (ret)
9276d0c3831SAndrey Smirnov 		return ret;
9287caff0fcSAndrey Gusakov 
9293f072c30SAndrey Smirnov 	dp0_syncval = FIELD_PREP(VS_WIDTH, vsync_len) |
9303f072c30SAndrey Smirnov 		      FIELD_PREP(HS_WIDTH, hsync_len);
9317caff0fcSAndrey Gusakov 
9323f072c30SAndrey Smirnov 	if (mode->flags & DRM_MODE_FLAG_NVSYNC)
9333f072c30SAndrey Smirnov 		dp0_syncval |= SYNCVAL_VS_POL_ACTIVE_LOW;
9347caff0fcSAndrey Gusakov 
9353f072c30SAndrey Smirnov 	if (mode->flags & DRM_MODE_FLAG_NHSYNC)
9363f072c30SAndrey Smirnov 		dp0_syncval |= SYNCVAL_HS_POL_ACTIVE_LOW;
9373f072c30SAndrey Smirnov 
9386d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_SYNCVAL, dp0_syncval);
9396d0c3831SAndrey Smirnov 	if (ret)
9406d0c3831SAndrey Smirnov 		return ret;
9413f072c30SAndrey Smirnov 
9425fa9e161SLucas Stach 	dpipxlfmt = DE_POL_ACTIVE_HIGH | SUB_CFG_TYPE_CONFIG1 | DPI_BPP_RGB888;
9435fa9e161SLucas Stach 
9445fa9e161SLucas Stach 	if (mode->flags & DRM_MODE_FLAG_NVSYNC)
9455fa9e161SLucas Stach 		dpipxlfmt |= VS_POL_ACTIVE_LOW;
9465fa9e161SLucas Stach 
9475fa9e161SLucas Stach 	if (mode->flags & DRM_MODE_FLAG_NHSYNC)
9485fa9e161SLucas Stach 		dpipxlfmt |= HS_POL_ACTIVE_LOW;
9495fa9e161SLucas Stach 
9505fa9e161SLucas Stach 	ret = regmap_write(tc->regmap, DPIPXLFMT, dpipxlfmt);
9516d0c3831SAndrey Smirnov 	if (ret)
9526d0c3831SAndrey Smirnov 		return ret;
9533f072c30SAndrey Smirnov 
9546d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_MISC,
9553f072c30SAndrey Smirnov 			   FIELD_PREP(MAX_TU_SYMBOL, max_tu_symbol) |
9563f072c30SAndrey Smirnov 			   FIELD_PREP(TU_SIZE, TU_SIZE_RECOMMENDED) |
957f3b8adbeSAndrey Gusakov 			   BPC_8);
9586d0c3831SAndrey Smirnov 	return ret;
9597caff0fcSAndrey Gusakov }
9607caff0fcSAndrey Gusakov 
tc_wait_link_training(struct tc_data * tc)961f9538357STomi Valkeinen static int tc_wait_link_training(struct tc_data *tc)
9627caff0fcSAndrey Gusakov {
9637caff0fcSAndrey Gusakov 	u32 value;
9647caff0fcSAndrey Gusakov 	int ret;
9657caff0fcSAndrey Gusakov 
966aa92213fSAndrey Smirnov 	ret = tc_poll_timeout(tc, DP0_LTSTAT, LT_LOOPDONE,
9678a6483acSTomi Valkeinen 			      LT_LOOPDONE, 500, 100000);
968aa92213fSAndrey Smirnov 	if (ret) {
969f9538357STomi Valkeinen 		dev_err(tc->dev, "Link training timeout waiting for LT_LOOPDONE!\n");
970aa92213fSAndrey Smirnov 		return ret;
9717caff0fcSAndrey Gusakov 	}
9727caff0fcSAndrey Gusakov 
9736d0c3831SAndrey Smirnov 	ret = regmap_read(tc->regmap, DP0_LTSTAT, &value);
9746d0c3831SAndrey Smirnov 	if (ret)
9756d0c3831SAndrey Smirnov 		return ret;
976f9538357STomi Valkeinen 
977aa92213fSAndrey Smirnov 	return (value >> 8) & 0x7;
9787caff0fcSAndrey Gusakov }
9797caff0fcSAndrey Gusakov 
tc_main_link_enable(struct tc_data * tc)980cb3263b2STomi Valkeinen static int tc_main_link_enable(struct tc_data *tc)
9817caff0fcSAndrey Gusakov {
9827caff0fcSAndrey Gusakov 	struct drm_dp_aux *aux = &tc->aux;
9837caff0fcSAndrey Gusakov 	struct device *dev = tc->dev;
9847caff0fcSAndrey Gusakov 	u32 dp_phy_ctrl;
9857caff0fcSAndrey Gusakov 	u32 value;
9867caff0fcSAndrey Gusakov 	int ret;
98732d36219SAndrey Smirnov 	u8 tmp[DP_LINK_STATUS_SIZE];
9887caff0fcSAndrey Gusakov 
989cb3263b2STomi Valkeinen 	dev_dbg(tc->dev, "link enable\n");
990cb3263b2STomi Valkeinen 
9916d0c3831SAndrey Smirnov 	ret = regmap_read(tc->regmap, DP0CTL, &value);
9926d0c3831SAndrey Smirnov 	if (ret)
9936d0c3831SAndrey Smirnov 		return ret;
99467bca92fSTomi Valkeinen 
9956d0c3831SAndrey Smirnov 	if (WARN_ON(value & DP_EN)) {
9966d0c3831SAndrey Smirnov 		ret = regmap_write(tc->regmap, DP0CTL, 0);
9976d0c3831SAndrey Smirnov 		if (ret)
9986d0c3831SAndrey Smirnov 			return ret;
9996d0c3831SAndrey Smirnov 	}
10006d0c3831SAndrey Smirnov 
10016d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_SRCCTRL, tc_srcctrl(tc));
10026d0c3831SAndrey Smirnov 	if (ret)
10036d0c3831SAndrey Smirnov 		return ret;
10049a63bd6fSTomi Valkeinen 	/* SSCG and BW27 on DP1 must be set to the same as on DP0 */
10056d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP1_SRCCTRL,
10069a63bd6fSTomi Valkeinen 		 (tc->link.spread ? DP0_SRCCTRL_SSCG : 0) |
1007e7dc8d40SThierry Reding 		 ((tc->link.rate != 162000) ? DP0_SRCCTRL_BW27 : 0));
10086d0c3831SAndrey Smirnov 	if (ret)
10096d0c3831SAndrey Smirnov 		return ret;
10107caff0fcSAndrey Gusakov 
1011c49f60dfSAndrey Smirnov 	ret = tc_set_syspllparam(tc);
10126d0c3831SAndrey Smirnov 	if (ret)
10136d0c3831SAndrey Smirnov 		return ret;
1014adf41098STomi Valkeinen 
10157caff0fcSAndrey Gusakov 	/* Setup Main Link */
10164d9d54a7STomi Valkeinen 	dp_phy_ctrl = BGREN | PWR_SW_EN | PHY_A0_EN | PHY_M0_EN;
1017e7dc8d40SThierry Reding 	if (tc->link.num_lanes == 2)
10184d9d54a7STomi Valkeinen 		dp_phy_ctrl |= PHY_2LANE;
10196d0c3831SAndrey Smirnov 
10206d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP_PHY_CTRL, dp_phy_ctrl);
10216d0c3831SAndrey Smirnov 	if (ret)
10226d0c3831SAndrey Smirnov 		return ret;
10237caff0fcSAndrey Gusakov 
10247caff0fcSAndrey Gusakov 	/* PLL setup */
1025134fb306SAndrey Smirnov 	ret = tc_pllupdate(tc, DP0_PLLCTRL);
10266d0c3831SAndrey Smirnov 	if (ret)
10276d0c3831SAndrey Smirnov 		return ret;
10287caff0fcSAndrey Gusakov 
1029134fb306SAndrey Smirnov 	ret = tc_pllupdate(tc, DP1_PLLCTRL);
10306d0c3831SAndrey Smirnov 	if (ret)
10316d0c3831SAndrey Smirnov 		return ret;
10327caff0fcSAndrey Gusakov 
10337caff0fcSAndrey Gusakov 	/* Reset/Enable Main Links */
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 	usleep_range(100, 200);
10377caff0fcSAndrey Gusakov 	dp_phy_ctrl &= ~(DP_PHY_RST | PHY_M1_RST | PHY_M0_RST);
10386d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP_PHY_CTRL, dp_phy_ctrl);
10397caff0fcSAndrey Gusakov 
10408a6483acSTomi Valkeinen 	ret = tc_poll_timeout(tc, DP_PHY_CTRL, PHY_RDY, PHY_RDY, 500, 100000);
1041ebcce4e6SAndrey Smirnov 	if (ret) {
10427caff0fcSAndrey Gusakov 		dev_err(dev, "timeout waiting for phy become ready");
1043ebcce4e6SAndrey Smirnov 		return ret;
10447caff0fcSAndrey Gusakov 	}
10457caff0fcSAndrey Gusakov 
10467caff0fcSAndrey Gusakov 	/* Set misc: 8 bits per color */
10477caff0fcSAndrey Gusakov 	ret = regmap_update_bits(tc->regmap, DP0_MISC, BPC_8, BPC_8);
10487caff0fcSAndrey Gusakov 	if (ret)
10496d0c3831SAndrey Smirnov 		return ret;
10507caff0fcSAndrey Gusakov 
10517caff0fcSAndrey Gusakov 	/*
10527caff0fcSAndrey Gusakov 	 * ASSR mode
10537caff0fcSAndrey Gusakov 	 * on TC358767 side ASSR configured through strap pin
10547caff0fcSAndrey Gusakov 	 * seems there is no way to change this setting from SW
10557caff0fcSAndrey Gusakov 	 *
10567caff0fcSAndrey Gusakov 	 * check is tc configured for same mode
10577caff0fcSAndrey Gusakov 	 */
10587caff0fcSAndrey Gusakov 	if (tc->assr != tc->link.assr) {
10597caff0fcSAndrey Gusakov 		dev_dbg(dev, "Trying to set display to ASSR: %d\n",
10607caff0fcSAndrey Gusakov 			tc->assr);
10617caff0fcSAndrey Gusakov 		/* try to set ASSR on display side */
10627caff0fcSAndrey Gusakov 		tmp[0] = tc->assr;
10637caff0fcSAndrey Gusakov 		ret = drm_dp_dpcd_writeb(aux, DP_EDP_CONFIGURATION_SET, tmp[0]);
10647caff0fcSAndrey Gusakov 		if (ret < 0)
10657caff0fcSAndrey Gusakov 			goto err_dpcd_read;
10667caff0fcSAndrey Gusakov 		/* read back */
10677caff0fcSAndrey Gusakov 		ret = drm_dp_dpcd_readb(aux, DP_EDP_CONFIGURATION_SET, tmp);
10687caff0fcSAndrey Gusakov 		if (ret < 0)
10697caff0fcSAndrey Gusakov 			goto err_dpcd_read;
10707caff0fcSAndrey Gusakov 
10717caff0fcSAndrey Gusakov 		if (tmp[0] != tc->assr) {
107287291e5dSLucas Stach 			dev_dbg(dev, "Failed to switch display ASSR to %d, falling back to unscrambled mode\n",
10737caff0fcSAndrey Gusakov 				tc->assr);
10747caff0fcSAndrey Gusakov 			/* trying with disabled scrambler */
1075e5607637STomi Valkeinen 			tc->link.scrambler_dis = true;
10767caff0fcSAndrey Gusakov 		}
10777caff0fcSAndrey Gusakov 	}
10787caff0fcSAndrey Gusakov 
10797caff0fcSAndrey Gusakov 	/* Setup Link & DPRx Config for Training */
1080e7dc8d40SThierry Reding 	tmp[0] = drm_dp_link_rate_to_bw_code(tc->link.rate);
1081e7dc8d40SThierry Reding 	tmp[1] = tc->link.num_lanes;
1082e7dc8d40SThierry Reding 
1083e7dc8d40SThierry Reding 	if (drm_dp_enhanced_frame_cap(tc->link.dpcd))
1084e7dc8d40SThierry Reding 		tmp[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
1085e7dc8d40SThierry Reding 
1086e7dc8d40SThierry Reding 	ret = drm_dp_dpcd_write(aux, DP_LINK_BW_SET, tmp, 2);
10877caff0fcSAndrey Gusakov 	if (ret < 0)
10887caff0fcSAndrey Gusakov 		goto err_dpcd_write;
10897caff0fcSAndrey Gusakov 
10907caff0fcSAndrey Gusakov 	/* DOWNSPREAD_CTRL */
10917caff0fcSAndrey Gusakov 	tmp[0] = tc->link.spread ? DP_SPREAD_AMP_0_5 : 0x00;
10927caff0fcSAndrey Gusakov 	/* MAIN_LINK_CHANNEL_CODING_SET */
10934b30bf41STomi Valkeinen 	tmp[1] =  DP_SET_ANSI_8B10B;
10947caff0fcSAndrey Gusakov 	ret = drm_dp_dpcd_write(aux, DP_DOWNSPREAD_CTRL, tmp, 2);
10957caff0fcSAndrey Gusakov 	if (ret < 0)
10967caff0fcSAndrey Gusakov 		goto err_dpcd_write;
10977caff0fcSAndrey Gusakov 
1098c28d1484STomi Valkeinen 	/* Reset voltage-swing & pre-emphasis */
1099c28d1484STomi Valkeinen 	tmp[0] = tmp[1] = DP_TRAIN_VOLTAGE_SWING_LEVEL_0 |
1100c28d1484STomi Valkeinen 			  DP_TRAIN_PRE_EMPH_LEVEL_0;
1101c28d1484STomi Valkeinen 	ret = drm_dp_dpcd_write(aux, DP_TRAINING_LANE0_SET, tmp, 2);
1102c28d1484STomi Valkeinen 	if (ret < 0)
1103c28d1484STomi Valkeinen 		goto err_dpcd_write;
1104c28d1484STomi Valkeinen 
1105f9538357STomi Valkeinen 	/* Clock-Recovery */
1106f9538357STomi Valkeinen 
1107f9538357STomi Valkeinen 	/* Set DPCD 0x102 for Training Pattern 1 */
11086d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_SNKLTCTRL,
11096d0c3831SAndrey Smirnov 			   DP_LINK_SCRAMBLING_DISABLE |
1110f9538357STomi Valkeinen 			   DP_TRAINING_PATTERN_1);
11116d0c3831SAndrey Smirnov 	if (ret)
11126d0c3831SAndrey Smirnov 		return ret;
1113f9538357STomi Valkeinen 
11146d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_LTLOOPCTRL,
1115f9538357STomi Valkeinen 			   (15 << 28) |	/* Defer Iteration Count */
1116f9538357STomi Valkeinen 			   (15 << 24) |	/* Loop Iteration Count */
1117f9538357STomi Valkeinen 			   (0xd << 0));	/* Loop Timer Delay */
11186d0c3831SAndrey Smirnov 	if (ret)
11196d0c3831SAndrey Smirnov 		return ret;
1120f9538357STomi Valkeinen 
11216d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_SRCCTRL,
11226d0c3831SAndrey Smirnov 			   tc_srcctrl(tc) | DP0_SRCCTRL_SCRMBLDIS |
11236d0c3831SAndrey Smirnov 			   DP0_SRCCTRL_AUTOCORRECT |
11246d0c3831SAndrey Smirnov 			   DP0_SRCCTRL_TP1);
11256d0c3831SAndrey Smirnov 	if (ret)
11266d0c3831SAndrey Smirnov 		return ret;
1127f9538357STomi Valkeinen 
1128f9538357STomi Valkeinen 	/* Enable DP0 to start Link Training */
11296d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0CTL,
1130e7dc8d40SThierry Reding 			   (drm_dp_enhanced_frame_cap(tc->link.dpcd) ?
1131e7dc8d40SThierry Reding 				EF_EN : 0) | DP_EN);
11326d0c3831SAndrey Smirnov 	if (ret)
11336d0c3831SAndrey Smirnov 		return ret;
1134f9538357STomi Valkeinen 
1135f9538357STomi Valkeinen 	/* wait */
11366d0c3831SAndrey Smirnov 
1137f9538357STomi Valkeinen 	ret = tc_wait_link_training(tc);
1138f9538357STomi Valkeinen 	if (ret < 0)
11396d0c3831SAndrey Smirnov 		return ret;
11407caff0fcSAndrey Gusakov 
1141f9538357STomi Valkeinen 	if (ret) {
1142f9538357STomi Valkeinen 		dev_err(tc->dev, "Link training phase 1 failed: %s\n",
1143f9538357STomi Valkeinen 			training_pattern1_errors[ret]);
11446d0c3831SAndrey Smirnov 		return -ENODEV;
1145f9538357STomi Valkeinen 	}
1146f9538357STomi Valkeinen 
1147f9538357STomi Valkeinen 	/* Channel Equalization */
1148f9538357STomi Valkeinen 
1149f9538357STomi Valkeinen 	/* Set DPCD 0x102 for Training Pattern 2 */
11506d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_SNKLTCTRL,
11516d0c3831SAndrey Smirnov 			   DP_LINK_SCRAMBLING_DISABLE |
1152f9538357STomi Valkeinen 			   DP_TRAINING_PATTERN_2);
11536d0c3831SAndrey Smirnov 	if (ret)
11546d0c3831SAndrey Smirnov 		return ret;
1155f9538357STomi Valkeinen 
11566d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_SRCCTRL,
11576d0c3831SAndrey Smirnov 			   tc_srcctrl(tc) | DP0_SRCCTRL_SCRMBLDIS |
11586d0c3831SAndrey Smirnov 			   DP0_SRCCTRL_AUTOCORRECT |
11596d0c3831SAndrey Smirnov 			   DP0_SRCCTRL_TP2);
11606d0c3831SAndrey Smirnov 	if (ret)
11616d0c3831SAndrey Smirnov 		return ret;
1162f9538357STomi Valkeinen 
1163f9538357STomi Valkeinen 	/* wait */
1164f9538357STomi Valkeinen 	ret = tc_wait_link_training(tc);
1165f9538357STomi Valkeinen 	if (ret < 0)
11666d0c3831SAndrey Smirnov 		return ret;
1167f9538357STomi Valkeinen 
1168f9538357STomi Valkeinen 	if (ret) {
1169f9538357STomi Valkeinen 		dev_err(tc->dev, "Link training phase 2 failed: %s\n",
1170f9538357STomi Valkeinen 			training_pattern2_errors[ret]);
11716d0c3831SAndrey Smirnov 		return -ENODEV;
1172f9538357STomi Valkeinen 	}
11737caff0fcSAndrey Gusakov 
11740776a269STomi Valkeinen 	/*
11750776a269STomi Valkeinen 	 * Toshiba's documentation suggests to first clear DPCD 0x102, then
11760776a269STomi Valkeinen 	 * clear the training pattern bit in DP0_SRCCTRL. Testing shows
11770776a269STomi Valkeinen 	 * that the link sometimes drops if those steps are done in that order,
11780776a269STomi Valkeinen 	 * but if the steps are done in reverse order, the link stays up.
11790776a269STomi Valkeinen 	 *
11800776a269STomi Valkeinen 	 * So we do the steps differently than documented here.
11810776a269STomi Valkeinen 	 */
11820776a269STomi Valkeinen 
11830776a269STomi Valkeinen 	/* Clear Training Pattern, set AutoCorrect Mode = 1 */
11846d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_SRCCTRL, tc_srcctrl(tc) |
11856d0c3831SAndrey Smirnov 			   DP0_SRCCTRL_AUTOCORRECT);
11866d0c3831SAndrey Smirnov 	if (ret)
11876d0c3831SAndrey Smirnov 		return ret;
11880776a269STomi Valkeinen 
11897caff0fcSAndrey Gusakov 	/* Clear DPCD 0x102 */
11907caff0fcSAndrey Gusakov 	/* Note: Can Not use DP0_SNKLTCTRL (0x06E4) short cut */
11917caff0fcSAndrey Gusakov 	tmp[0] = tc->link.scrambler_dis ? DP_LINK_SCRAMBLING_DISABLE : 0x00;
11927caff0fcSAndrey Gusakov 	ret = drm_dp_dpcd_writeb(aux, DP_TRAINING_PATTERN_SET, tmp[0]);
11937caff0fcSAndrey Gusakov 	if (ret < 0)
11947caff0fcSAndrey Gusakov 		goto err_dpcd_write;
11957caff0fcSAndrey Gusakov 
11960bf25146STomi Valkeinen 	/* Check link status */
11970bf25146STomi Valkeinen 	ret = drm_dp_dpcd_read_link_status(aux, tmp);
11987caff0fcSAndrey Gusakov 	if (ret < 0)
11997caff0fcSAndrey Gusakov 		goto err_dpcd_read;
12007caff0fcSAndrey Gusakov 
12010bf25146STomi Valkeinen 	ret = 0;
12027caff0fcSAndrey Gusakov 
12030bf25146STomi Valkeinen 	value = tmp[0] & DP_CHANNEL_EQ_BITS;
12040bf25146STomi Valkeinen 
12050bf25146STomi Valkeinen 	if (value != DP_CHANNEL_EQ_BITS) {
12060bf25146STomi Valkeinen 		dev_err(tc->dev, "Lane 0 failed: %x\n", value);
12070bf25146STomi Valkeinen 		ret = -ENODEV;
12080bf25146STomi Valkeinen 	}
12090bf25146STomi Valkeinen 
1210e7dc8d40SThierry Reding 	if (tc->link.num_lanes == 2) {
12110bf25146STomi Valkeinen 		value = (tmp[0] >> 4) & DP_CHANNEL_EQ_BITS;
12120bf25146STomi Valkeinen 
12130bf25146STomi Valkeinen 		if (value != DP_CHANNEL_EQ_BITS) {
12140bf25146STomi Valkeinen 			dev_err(tc->dev, "Lane 1 failed: %x\n", value);
12150bf25146STomi Valkeinen 			ret = -ENODEV;
12160bf25146STomi Valkeinen 		}
12170bf25146STomi Valkeinen 
12180bf25146STomi Valkeinen 		if (!(tmp[2] & DP_INTERLANE_ALIGN_DONE)) {
12190bf25146STomi Valkeinen 			dev_err(tc->dev, "Interlane align failed\n");
12200bf25146STomi Valkeinen 			ret = -ENODEV;
12210bf25146STomi Valkeinen 		}
12220bf25146STomi Valkeinen 	}
12230bf25146STomi Valkeinen 
12240bf25146STomi Valkeinen 	if (ret) {
12250bf25146STomi Valkeinen 		dev_err(dev, "0x0202 LANE0_1_STATUS:            0x%02x\n", tmp[0]);
12260bf25146STomi Valkeinen 		dev_err(dev, "0x0203 LANE2_3_STATUS             0x%02x\n", tmp[1]);
12270bf25146STomi Valkeinen 		dev_err(dev, "0x0204 LANE_ALIGN_STATUS_UPDATED: 0x%02x\n", tmp[2]);
12280bf25146STomi Valkeinen 		dev_err(dev, "0x0205 SINK_STATUS:               0x%02x\n", tmp[3]);
12290bf25146STomi Valkeinen 		dev_err(dev, "0x0206 ADJUST_REQUEST_LANE0_1:    0x%02x\n", tmp[4]);
12300bf25146STomi Valkeinen 		dev_err(dev, "0x0207 ADJUST_REQUEST_LANE2_3:    0x%02x\n", tmp[5]);
12316d0c3831SAndrey Smirnov 		return ret;
12327caff0fcSAndrey Gusakov 	}
12337caff0fcSAndrey Gusakov 
12347caff0fcSAndrey Gusakov 	return 0;
12357caff0fcSAndrey Gusakov err_dpcd_read:
12367caff0fcSAndrey Gusakov 	dev_err(tc->dev, "Failed to read DPCD: %d\n", ret);
12377caff0fcSAndrey Gusakov 	return ret;
12387caff0fcSAndrey Gusakov err_dpcd_write:
12397caff0fcSAndrey Gusakov 	dev_err(tc->dev, "Failed to write DPCD: %d\n", ret);
12407caff0fcSAndrey Gusakov 	return ret;
12417caff0fcSAndrey Gusakov }
12427caff0fcSAndrey Gusakov 
tc_main_link_disable(struct tc_data * tc)1243cb3263b2STomi Valkeinen static int tc_main_link_disable(struct tc_data *tc)
1244cb3263b2STomi Valkeinen {
1245cb3263b2STomi Valkeinen 	int ret;
1246cb3263b2STomi Valkeinen 
1247cb3263b2STomi Valkeinen 	dev_dbg(tc->dev, "link disable\n");
1248cb3263b2STomi Valkeinen 
12496d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_SRCCTRL, 0);
12506d0c3831SAndrey Smirnov 	if (ret)
1251cb3263b2STomi Valkeinen 		return ret;
12526d0c3831SAndrey Smirnov 
12538759464dSLucas Stach 	ret = regmap_write(tc->regmap, DP0CTL, 0);
12548759464dSLucas Stach 	if (ret)
12558759464dSLucas Stach 		return ret;
12568759464dSLucas Stach 
12578759464dSLucas Stach 	return regmap_update_bits(tc->regmap, DP_PHY_CTRL,
12588759464dSLucas Stach 				  PHY_M0_RST | PHY_M1_RST | PHY_M0_EN,
12598759464dSLucas Stach 				  PHY_M0_RST | PHY_M1_RST);
1260cb3263b2STomi Valkeinen }
1261cb3263b2STomi Valkeinen 
tc_dsi_rx_enable(struct tc_data * tc)1262d7fd32ecSMarek Vasut static int tc_dsi_rx_enable(struct tc_data *tc)
1263d7fd32ecSMarek Vasut {
1264d7fd32ecSMarek Vasut 	u32 value;
1265d7fd32ecSMarek Vasut 	int ret;
1266d7fd32ecSMarek Vasut 
126701338bb8SMarek Vasut 	regmap_write(tc->regmap, PPI_D0S_CLRSIPOCOUNT, 25);
126801338bb8SMarek Vasut 	regmap_write(tc->regmap, PPI_D1S_CLRSIPOCOUNT, 25);
126901338bb8SMarek Vasut 	regmap_write(tc->regmap, PPI_D2S_CLRSIPOCOUNT, 25);
127001338bb8SMarek Vasut 	regmap_write(tc->regmap, PPI_D3S_CLRSIPOCOUNT, 25);
1271d7fd32ecSMarek Vasut 	regmap_write(tc->regmap, PPI_D0S_ATMR, 0);
1272d7fd32ecSMarek Vasut 	regmap_write(tc->regmap, PPI_D1S_ATMR, 0);
1273d7fd32ecSMarek Vasut 	regmap_write(tc->regmap, PPI_TX_RX_TA, TTA_GET | TTA_SURE);
1274d7fd32ecSMarek Vasut 	regmap_write(tc->regmap, PPI_LPTXTIMECNT, LPX_PERIOD);
1275d7fd32ecSMarek Vasut 
12765bdaaf4fSMarek Vasut 	value = ((LANEENABLE_L0EN << tc->dsi->lanes) - LANEENABLE_L0EN) |
1277d7fd32ecSMarek Vasut 		LANEENABLE_CLEN;
1278d7fd32ecSMarek Vasut 	regmap_write(tc->regmap, PPI_LANEENABLE, value);
1279d7fd32ecSMarek Vasut 	regmap_write(tc->regmap, DSI_LANEENABLE, value);
1280d7fd32ecSMarek Vasut 
1281d7fd32ecSMarek Vasut 	/* Set input interface */
1282d7fd32ecSMarek Vasut 	value = DP0_AUDSRC_NO_INPUT;
1283d7fd32ecSMarek Vasut 	if (tc_test_pattern)
1284d7fd32ecSMarek Vasut 		value |= DP0_VIDSRC_COLOR_BAR;
1285d7fd32ecSMarek Vasut 	else
1286d7fd32ecSMarek Vasut 		value |= DP0_VIDSRC_DSI_RX;
1287d7fd32ecSMarek Vasut 	ret = regmap_write(tc->regmap, SYSCTRL, value);
1288d7fd32ecSMarek Vasut 	if (ret)
1289d7fd32ecSMarek Vasut 		return ret;
1290d7fd32ecSMarek Vasut 
1291d7fd32ecSMarek Vasut 	usleep_range(120, 150);
1292d7fd32ecSMarek Vasut 
1293d7fd32ecSMarek Vasut 	regmap_write(tc->regmap, PPI_STARTPPI, PPI_START_FUNCTION);
1294d7fd32ecSMarek Vasut 	regmap_write(tc->regmap, DSI_STARTDSI, DSI_RX_START);
1295d7fd32ecSMarek Vasut 
1296d7fd32ecSMarek Vasut 	return 0;
1297d7fd32ecSMarek Vasut }
1298d7fd32ecSMarek Vasut 
tc_dpi_rx_enable(struct tc_data * tc)1299d7fd32ecSMarek Vasut static int tc_dpi_rx_enable(struct tc_data *tc)
1300d7fd32ecSMarek Vasut {
1301d7fd32ecSMarek Vasut 	u32 value;
1302d7fd32ecSMarek Vasut 
1303d7fd32ecSMarek Vasut 	/* Set input interface */
1304d7fd32ecSMarek Vasut 	value = DP0_AUDSRC_NO_INPUT;
1305d7fd32ecSMarek Vasut 	if (tc_test_pattern)
1306d7fd32ecSMarek Vasut 		value |= DP0_VIDSRC_COLOR_BAR;
1307d7fd32ecSMarek Vasut 	else
1308d7fd32ecSMarek Vasut 		value |= DP0_VIDSRC_DPI_RX;
1309d7fd32ecSMarek Vasut 	return regmap_write(tc->regmap, SYSCTRL, value);
1310d7fd32ecSMarek Vasut }
1311d7fd32ecSMarek Vasut 
tc_dpi_stream_enable(struct tc_data * tc)1312bbfd3190SMarek Vasut static int tc_dpi_stream_enable(struct tc_data *tc)
1313bbfd3190SMarek Vasut {
1314bbfd3190SMarek Vasut 	int ret;
1315bbfd3190SMarek Vasut 
1316bbfd3190SMarek Vasut 	dev_dbg(tc->dev, "enable video stream\n");
1317bbfd3190SMarek Vasut 
1318bbfd3190SMarek Vasut 	/* Setup PLL */
1319bbfd3190SMarek Vasut 	ret = tc_set_syspllparam(tc);
1320bbfd3190SMarek Vasut 	if (ret)
1321bbfd3190SMarek Vasut 		return ret;
1322bbfd3190SMarek Vasut 
1323bbfd3190SMarek Vasut 	/*
1324bbfd3190SMarek Vasut 	 * Initially PLLs are in bypass. Force PLL parameter update,
1325bbfd3190SMarek Vasut 	 * disable PLL bypass, enable PLL
1326bbfd3190SMarek Vasut 	 */
1327bbfd3190SMarek Vasut 	ret = tc_pllupdate(tc, DP0_PLLCTRL);
1328bbfd3190SMarek Vasut 	if (ret)
1329bbfd3190SMarek Vasut 		return ret;
1330bbfd3190SMarek Vasut 
1331bbfd3190SMarek Vasut 	ret = tc_pllupdate(tc, DP1_PLLCTRL);
1332bbfd3190SMarek Vasut 	if (ret)
1333bbfd3190SMarek Vasut 		return ret;
1334bbfd3190SMarek Vasut 
1335bbfd3190SMarek Vasut 	/* Pixel PLL must always be enabled for DPI mode */
1336bbfd3190SMarek Vasut 	ret = tc_pxl_pll_en(tc, clk_get_rate(tc->refclk),
1337bbfd3190SMarek Vasut 			    1000 * tc->mode.clock);
1338bbfd3190SMarek Vasut 	if (ret)
1339bbfd3190SMarek Vasut 		return ret;
1340bbfd3190SMarek Vasut 
1341bbfd3190SMarek Vasut 	ret = tc_set_common_video_mode(tc, &tc->mode);
1342bbfd3190SMarek Vasut 	if (ret)
1343bbfd3190SMarek Vasut 		return ret;
1344bbfd3190SMarek Vasut 
1345bbfd3190SMarek Vasut 	ret = tc_set_dpi_video_mode(tc, &tc->mode);
1346bbfd3190SMarek Vasut 	if (ret)
1347bbfd3190SMarek Vasut 		return ret;
1348bbfd3190SMarek Vasut 
1349d7fd32ecSMarek Vasut 	return tc_dsi_rx_enable(tc);
1350bbfd3190SMarek Vasut }
1351bbfd3190SMarek Vasut 
tc_dpi_stream_disable(struct tc_data * tc)1352bbfd3190SMarek Vasut static int tc_dpi_stream_disable(struct tc_data *tc)
1353bbfd3190SMarek Vasut {
1354bbfd3190SMarek Vasut 	dev_dbg(tc->dev, "disable video stream\n");
1355bbfd3190SMarek Vasut 
1356bbfd3190SMarek Vasut 	tc_pxl_pll_dis(tc);
1357bbfd3190SMarek Vasut 
1358bbfd3190SMarek Vasut 	return 0;
1359bbfd3190SMarek Vasut }
1360bbfd3190SMarek Vasut 
tc_edp_stream_enable(struct tc_data * tc)1361a219062bSMarek Vasut static int tc_edp_stream_enable(struct tc_data *tc)
13627caff0fcSAndrey Gusakov {
13637caff0fcSAndrey Gusakov 	int ret;
13647caff0fcSAndrey Gusakov 	u32 value;
13657caff0fcSAndrey Gusakov 
136680d57245STomi Valkeinen 	dev_dbg(tc->dev, "enable video stream\n");
13677caff0fcSAndrey Gusakov 
13683080c21aSMarek Vasut 	/*
13693080c21aSMarek Vasut 	 * Pixel PLL must be enabled for DSI input mode and test pattern.
13703080c21aSMarek Vasut 	 *
13713080c21aSMarek Vasut 	 * Per TC9595XBG datasheet Revision 0.1 2018-12-27 Figure 4.18
13723080c21aSMarek Vasut 	 * "Clock Mode Selection and Clock Sources", either Pixel PLL
13733080c21aSMarek Vasut 	 * or DPI_PCLK supplies StrmClk. DPI_PCLK is only available in
13743080c21aSMarek Vasut 	 * case valid Pixel Clock are supplied to the chip DPI input.
13753080c21aSMarek Vasut 	 * In case built-in test pattern is desired OR DSI input mode
13763080c21aSMarek Vasut 	 * is used, DPI_PCLK is not available and thus Pixel PLL must
13773080c21aSMarek Vasut 	 * be used instead.
13783080c21aSMarek Vasut 	 */
13793080c21aSMarek Vasut 	if (tc->input_connector_dsi || tc_test_pattern) {
1380bb248368STomi Valkeinen 		ret = tc_pxl_pll_en(tc, clk_get_rate(tc->refclk),
138146648a3cSTomi Valkeinen 				    1000 * tc->mode.clock);
1382bb248368STomi Valkeinen 		if (ret)
13836d0c3831SAndrey Smirnov 			return ret;
1384bb248368STomi Valkeinen 	}
1385bb248368STomi Valkeinen 
1386aebe58a7SMarek Vasut 	ret = tc_set_common_video_mode(tc, &tc->mode);
1387aebe58a7SMarek Vasut 	if (ret)
1388aebe58a7SMarek Vasut 		return ret;
1389aebe58a7SMarek Vasut 
1390aebe58a7SMarek Vasut 	ret = tc_set_edp_video_mode(tc, &tc->mode);
13915761a259STomi Valkeinen 	if (ret)
139280d57245STomi Valkeinen 		return ret;
13935761a259STomi Valkeinen 
13945761a259STomi Valkeinen 	/* Set M/N */
13955761a259STomi Valkeinen 	ret = tc_stream_clock_calc(tc);
13965761a259STomi Valkeinen 	if (ret)
139780d57245STomi Valkeinen 		return ret;
13985761a259STomi Valkeinen 
13997caff0fcSAndrey Gusakov 	value = VID_MN_GEN | DP_EN;
1400e7dc8d40SThierry Reding 	if (drm_dp_enhanced_frame_cap(tc->link.dpcd))
14017caff0fcSAndrey Gusakov 		value |= EF_EN;
14026d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0CTL, value);
14036d0c3831SAndrey Smirnov 	if (ret)
14046d0c3831SAndrey Smirnov 		return ret;
14057caff0fcSAndrey Gusakov 	/*
14067caff0fcSAndrey Gusakov 	 * VID_EN assertion should be delayed by at least N * LSCLK
14077caff0fcSAndrey Gusakov 	 * cycles from the time VID_MN_GEN is enabled in order to
14087caff0fcSAndrey Gusakov 	 * generate stable values for VID_M. LSCLK is 270 MHz or
14097caff0fcSAndrey Gusakov 	 * 162 MHz, VID_N is set to 32768 in  tc_stream_clock_calc(),
14107caff0fcSAndrey Gusakov 	 * so a delay of at least 203 us should suffice.
14117caff0fcSAndrey Gusakov 	 */
14127caff0fcSAndrey Gusakov 	usleep_range(500, 1000);
14137caff0fcSAndrey Gusakov 	value |= VID_EN;
14146d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0CTL, value);
14156d0c3831SAndrey Smirnov 	if (ret)
14166d0c3831SAndrey Smirnov 		return ret;
141780d57245STomi Valkeinen 
1418d7fd32ecSMarek Vasut 	/* Set input interface */
14193080c21aSMarek Vasut 	if (tc->input_connector_dsi)
14203080c21aSMarek Vasut 		return tc_dsi_rx_enable(tc);
14213080c21aSMarek Vasut 	else
1422d7fd32ecSMarek Vasut 		return tc_dpi_rx_enable(tc);
14237caff0fcSAndrey Gusakov }
14247caff0fcSAndrey Gusakov 
tc_edp_stream_disable(struct tc_data * tc)1425a219062bSMarek Vasut static int tc_edp_stream_disable(struct tc_data *tc)
142680d57245STomi Valkeinen {
142780d57245STomi Valkeinen 	int ret;
142880d57245STomi Valkeinen 
142980d57245STomi Valkeinen 	dev_dbg(tc->dev, "disable video stream\n");
143080d57245STomi Valkeinen 
14316d0c3831SAndrey Smirnov 	ret = regmap_update_bits(tc->regmap, DP0CTL, VID_EN, 0);
14326d0c3831SAndrey Smirnov 	if (ret)
14336d0c3831SAndrey Smirnov 		return ret;
143480d57245STomi Valkeinen 
1435bb248368STomi Valkeinen 	tc_pxl_pll_dis(tc);
1436bb248368STomi Valkeinen 
14377caff0fcSAndrey Gusakov 	return 0;
14387caff0fcSAndrey Gusakov }
14397caff0fcSAndrey Gusakov 
1440f5be6239SMarek Vasut static void
tc_dpi_bridge_atomic_enable(struct drm_bridge * bridge,struct drm_bridge_state * old_bridge_state)1441bbfd3190SMarek Vasut tc_dpi_bridge_atomic_enable(struct drm_bridge *bridge,
1442bbfd3190SMarek Vasut 			    struct drm_bridge_state *old_bridge_state)
1443bbfd3190SMarek Vasut 
1444bbfd3190SMarek Vasut {
1445bbfd3190SMarek Vasut 	struct tc_data *tc = bridge_to_tc(bridge);
1446bbfd3190SMarek Vasut 	int ret;
1447bbfd3190SMarek Vasut 
1448bbfd3190SMarek Vasut 	ret = tc_dpi_stream_enable(tc);
1449bbfd3190SMarek Vasut 	if (ret < 0) {
1450bbfd3190SMarek Vasut 		dev_err(tc->dev, "main link stream start error: %d\n", ret);
1451bbfd3190SMarek Vasut 		tc_main_link_disable(tc);
1452bbfd3190SMarek Vasut 		return;
1453bbfd3190SMarek Vasut 	}
1454bbfd3190SMarek Vasut }
1455bbfd3190SMarek Vasut 
1456bbfd3190SMarek Vasut static void
tc_dpi_bridge_atomic_disable(struct drm_bridge * bridge,struct drm_bridge_state * old_bridge_state)1457bbfd3190SMarek Vasut tc_dpi_bridge_atomic_disable(struct drm_bridge *bridge,
1458bbfd3190SMarek Vasut 			     struct drm_bridge_state *old_bridge_state)
1459bbfd3190SMarek Vasut {
1460bbfd3190SMarek Vasut 	struct tc_data *tc = bridge_to_tc(bridge);
1461bbfd3190SMarek Vasut 	int ret;
1462bbfd3190SMarek Vasut 
1463bbfd3190SMarek Vasut 	ret = tc_dpi_stream_disable(tc);
1464bbfd3190SMarek Vasut 	if (ret < 0)
1465bbfd3190SMarek Vasut 		dev_err(tc->dev, "main link stream stop error: %d\n", ret);
1466bbfd3190SMarek Vasut }
1467bbfd3190SMarek Vasut 
1468bbfd3190SMarek Vasut static void
tc_edp_bridge_atomic_enable(struct drm_bridge * bridge,struct drm_bridge_state * old_bridge_state)1469f5be6239SMarek Vasut tc_edp_bridge_atomic_enable(struct drm_bridge *bridge,
1470f5be6239SMarek Vasut 			    struct drm_bridge_state *old_bridge_state)
14717caff0fcSAndrey Gusakov {
14727caff0fcSAndrey Gusakov 	struct tc_data *tc = bridge_to_tc(bridge);
14737caff0fcSAndrey Gusakov 	int ret;
14747caff0fcSAndrey Gusakov 
1475f25ee501STomi Valkeinen 	ret = tc_get_display_props(tc);
1476f25ee501STomi Valkeinen 	if (ret < 0) {
1477f25ee501STomi Valkeinen 		dev_err(tc->dev, "failed to read display props: %d\n", ret);
1478f25ee501STomi Valkeinen 		return;
1479f25ee501STomi Valkeinen 	}
1480f25ee501STomi Valkeinen 
1481cb3263b2STomi Valkeinen 	ret = tc_main_link_enable(tc);
14827caff0fcSAndrey Gusakov 	if (ret < 0) {
1483cb3263b2STomi Valkeinen 		dev_err(tc->dev, "main link enable error: %d\n", ret);
14847caff0fcSAndrey Gusakov 		return;
14857caff0fcSAndrey Gusakov 	}
14867caff0fcSAndrey Gusakov 
1487a219062bSMarek Vasut 	ret = tc_edp_stream_enable(tc);
14887caff0fcSAndrey Gusakov 	if (ret < 0) {
14897caff0fcSAndrey Gusakov 		dev_err(tc->dev, "main link stream start error: %d\n", ret);
1490cb3263b2STomi Valkeinen 		tc_main_link_disable(tc);
14917caff0fcSAndrey Gusakov 		return;
14927caff0fcSAndrey Gusakov 	}
14937caff0fcSAndrey Gusakov }
14947caff0fcSAndrey Gusakov 
1495f5be6239SMarek Vasut static void
tc_edp_bridge_atomic_disable(struct drm_bridge * bridge,struct drm_bridge_state * old_bridge_state)1496f5be6239SMarek Vasut tc_edp_bridge_atomic_disable(struct drm_bridge *bridge,
1497f5be6239SMarek Vasut 			     struct drm_bridge_state *old_bridge_state)
14987caff0fcSAndrey Gusakov {
14997caff0fcSAndrey Gusakov 	struct tc_data *tc = bridge_to_tc(bridge);
15007caff0fcSAndrey Gusakov 	int ret;
15017caff0fcSAndrey Gusakov 
1502a219062bSMarek Vasut 	ret = tc_edp_stream_disable(tc);
15037caff0fcSAndrey Gusakov 	if (ret < 0)
15047caff0fcSAndrey Gusakov 		dev_err(tc->dev, "main link stream stop error: %d\n", ret);
1505cb3263b2STomi Valkeinen 
1506cb3263b2STomi Valkeinen 	ret = tc_main_link_disable(tc);
1507cb3263b2STomi Valkeinen 	if (ret < 0)
1508cb3263b2STomi Valkeinen 		dev_err(tc->dev, "main link disable error: %d\n", ret);
15097caff0fcSAndrey Gusakov }
15107caff0fcSAndrey Gusakov 
tc_dpi_atomic_check(struct drm_bridge * bridge,struct drm_bridge_state * bridge_state,struct drm_crtc_state * crtc_state,struct drm_connector_state * conn_state)1511bbfd3190SMarek Vasut static int tc_dpi_atomic_check(struct drm_bridge *bridge,
1512bbfd3190SMarek Vasut 			       struct drm_bridge_state *bridge_state,
1513bbfd3190SMarek Vasut 			       struct drm_crtc_state *crtc_state,
1514bbfd3190SMarek Vasut 			       struct drm_connector_state *conn_state)
1515bbfd3190SMarek Vasut {
1516bbfd3190SMarek Vasut 	/* DSI->DPI interface clock limitation: upto 100 MHz */
15175fa9e161SLucas Stach 	if (crtc_state->adjusted_mode.clock > 100000)
15185fa9e161SLucas Stach 		return -EINVAL;
15195fa9e161SLucas Stach 
15205fa9e161SLucas Stach 	return 0;
1521bbfd3190SMarek Vasut }
1522bbfd3190SMarek Vasut 
tc_edp_atomic_check(struct drm_bridge * bridge,struct drm_bridge_state * bridge_state,struct drm_crtc_state * crtc_state,struct drm_connector_state * conn_state)152365fdbb71SMarek Vasut static int tc_edp_atomic_check(struct drm_bridge *bridge,
152465fdbb71SMarek Vasut 			       struct drm_bridge_state *bridge_state,
152565fdbb71SMarek Vasut 			       struct drm_crtc_state *crtc_state,
152665fdbb71SMarek Vasut 			       struct drm_connector_state *conn_state)
152765fdbb71SMarek Vasut {
152865fdbb71SMarek Vasut 	/* DPI->(e)DP interface clock limitation: upto 154 MHz */
15295fa9e161SLucas Stach 	if (crtc_state->adjusted_mode.clock > 154000)
15305fa9e161SLucas Stach 		return -EINVAL;
15315fa9e161SLucas Stach 
15325fa9e161SLucas Stach 	return 0;
153365fdbb71SMarek Vasut }
153465fdbb71SMarek Vasut 
1535a219062bSMarek Vasut static enum drm_mode_status
tc_dpi_mode_valid(struct drm_bridge * bridge,const struct drm_display_info * info,const struct drm_display_mode * mode)1536bbfd3190SMarek Vasut tc_dpi_mode_valid(struct drm_bridge *bridge,
1537bbfd3190SMarek Vasut 		  const struct drm_display_info *info,
1538bbfd3190SMarek Vasut 		  const struct drm_display_mode *mode)
1539bbfd3190SMarek Vasut {
1540bbfd3190SMarek Vasut 	/* DPI interface clock limitation: upto 100 MHz */
1541bbfd3190SMarek Vasut 	if (mode->clock > 100000)
1542bbfd3190SMarek Vasut 		return MODE_CLOCK_HIGH;
1543bbfd3190SMarek Vasut 
1544bbfd3190SMarek Vasut 	return MODE_OK;
1545bbfd3190SMarek Vasut }
1546bbfd3190SMarek Vasut 
1547bbfd3190SMarek Vasut static enum drm_mode_status
tc_edp_mode_valid(struct drm_bridge * bridge,const struct drm_display_info * info,const struct drm_display_mode * mode)1548a219062bSMarek Vasut tc_edp_mode_valid(struct drm_bridge *bridge,
154912c683e1SLaurent Pinchart 		  const struct drm_display_info *info,
15504647a64fSTomi Valkeinen 		  const struct drm_display_mode *mode)
15517caff0fcSAndrey Gusakov {
15524647a64fSTomi Valkeinen 	struct tc_data *tc = bridge_to_tc(bridge);
155351b9e62eSTomi Valkeinen 	u32 req, avail;
155451b9e62eSTomi Valkeinen 	u32 bits_per_pixel = 24;
155551b9e62eSTomi Valkeinen 
155699fc8e96SAndrey Gusakov 	/* DPI interface clock limitation: upto 154 MHz */
155799fc8e96SAndrey Gusakov 	if (mode->clock > 154000)
155899fc8e96SAndrey Gusakov 		return MODE_CLOCK_HIGH;
155999fc8e96SAndrey Gusakov 
156051b9e62eSTomi Valkeinen 	req = mode->clock * bits_per_pixel / 8;
1561e7dc8d40SThierry Reding 	avail = tc->link.num_lanes * tc->link.rate;
156251b9e62eSTomi Valkeinen 
156351b9e62eSTomi Valkeinen 	if (req > avail)
156451b9e62eSTomi Valkeinen 		return MODE_BAD;
156551b9e62eSTomi Valkeinen 
15667caff0fcSAndrey Gusakov 	return MODE_OK;
15677caff0fcSAndrey Gusakov }
15687caff0fcSAndrey Gusakov 
tc_bridge_mode_set(struct drm_bridge * bridge,const struct drm_display_mode * mode,const struct drm_display_mode * adj)15697caff0fcSAndrey Gusakov static void tc_bridge_mode_set(struct drm_bridge *bridge,
157063f8f3baSLaurent Pinchart 			       const struct drm_display_mode *mode,
157163f8f3baSLaurent Pinchart 			       const struct drm_display_mode *adj)
15727caff0fcSAndrey Gusakov {
15737caff0fcSAndrey Gusakov 	struct tc_data *tc = bridge_to_tc(bridge);
15747caff0fcSAndrey Gusakov 
1575d008bc33SVille Syrjälä 	drm_mode_copy(&tc->mode, mode);
15767caff0fcSAndrey Gusakov }
15777caff0fcSAndrey Gusakov 
tc_get_edid(struct drm_bridge * bridge,struct drm_connector * connector)1578731f4badSSam Ravnborg static struct edid *tc_get_edid(struct drm_bridge *bridge,
1579731f4badSSam Ravnborg 				struct drm_connector *connector)
1580731f4badSSam Ravnborg {
1581731f4badSSam Ravnborg 	struct tc_data *tc = bridge_to_tc(bridge);
1582*5981e6e1STomi Valkeinen 	int ret;
1583*5981e6e1STomi Valkeinen 
1584*5981e6e1STomi Valkeinen 	ret = tc_get_display_props(tc);
1585*5981e6e1STomi Valkeinen 	if (ret < 0) {
1586*5981e6e1STomi Valkeinen 		dev_err(tc->dev, "failed to read display props: %d\n", ret);
1587*5981e6e1STomi Valkeinen 		return 0;
1588*5981e6e1STomi Valkeinen 	}
1589731f4badSSam Ravnborg 
1590731f4badSSam Ravnborg 	return drm_get_edid(connector, &tc->aux.ddc);
1591731f4badSSam Ravnborg }
1592731f4badSSam Ravnborg 
tc_connector_get_modes(struct drm_connector * connector)15937caff0fcSAndrey Gusakov static int tc_connector_get_modes(struct drm_connector *connector)
15947caff0fcSAndrey Gusakov {
15957caff0fcSAndrey Gusakov 	struct tc_data *tc = connector_to_tc(connector);
1596731f4badSSam Ravnborg 	int num_modes;
15977caff0fcSAndrey Gusakov 	struct edid *edid;
159832315730STomi Valkeinen 	int ret;
159932315730STomi Valkeinen 
160032315730STomi Valkeinen 	ret = tc_get_display_props(tc);
160132315730STomi Valkeinen 	if (ret < 0) {
160232315730STomi Valkeinen 		dev_err(tc->dev, "failed to read display props: %d\n", ret);
160332315730STomi Valkeinen 		return 0;
160432315730STomi Valkeinen 	}
16057caff0fcSAndrey Gusakov 
1606de5e6c02SSam Ravnborg 	if (tc->panel_bridge) {
1607de5e6c02SSam Ravnborg 		num_modes = drm_bridge_get_modes(tc->panel_bridge, connector);
1608731f4badSSam Ravnborg 		if (num_modes > 0)
1609731f4badSSam Ravnborg 			return num_modes;
1610de5e6c02SSam Ravnborg 	}
16117caff0fcSAndrey Gusakov 
1612731f4badSSam Ravnborg 	edid = tc_get_edid(&tc->bridge, connector);
1613731f4badSSam Ravnborg 	num_modes = drm_add_edid_modes(connector, edid);
1614731f4badSSam Ravnborg 	kfree(edid);
16157caff0fcSAndrey Gusakov 
1616731f4badSSam Ravnborg 	return num_modes;
16177caff0fcSAndrey Gusakov }
16187caff0fcSAndrey Gusakov 
16197caff0fcSAndrey Gusakov static const struct drm_connector_helper_funcs tc_connector_helper_funcs = {
16207caff0fcSAndrey Gusakov 	.get_modes = tc_connector_get_modes,
16217caff0fcSAndrey Gusakov };
16227caff0fcSAndrey Gusakov 
tc_bridge_detect(struct drm_bridge * bridge)1623136d73a8SSam Ravnborg static enum drm_connector_status tc_bridge_detect(struct drm_bridge *bridge)
1624f25ee501STomi Valkeinen {
1625136d73a8SSam Ravnborg 	struct tc_data *tc = bridge_to_tc(bridge);
1626f25ee501STomi Valkeinen 	bool conn;
1627f25ee501STomi Valkeinen 	u32 val;
1628f25ee501STomi Valkeinen 	int ret;
1629f25ee501STomi Valkeinen 
16306d0c3831SAndrey Smirnov 	ret = regmap_read(tc->regmap, GPIOI, &val);
16316d0c3831SAndrey Smirnov 	if (ret)
16326d0c3831SAndrey Smirnov 		return connector_status_unknown;
1633f25ee501STomi Valkeinen 
1634f25ee501STomi Valkeinen 	conn = val & BIT(tc->hpd_pin);
1635f25ee501STomi Valkeinen 
1636f25ee501STomi Valkeinen 	if (conn)
1637f25ee501STomi Valkeinen 		return connector_status_connected;
1638f25ee501STomi Valkeinen 	else
1639f25ee501STomi Valkeinen 		return connector_status_disconnected;
1640f25ee501STomi Valkeinen }
1641f25ee501STomi Valkeinen 
1642136d73a8SSam Ravnborg static enum drm_connector_status
tc_connector_detect(struct drm_connector * connector,bool force)1643136d73a8SSam Ravnborg tc_connector_detect(struct drm_connector *connector, bool force)
1644136d73a8SSam Ravnborg {
1645136d73a8SSam Ravnborg 	struct tc_data *tc = connector_to_tc(connector);
1646136d73a8SSam Ravnborg 
1647136d73a8SSam Ravnborg 	if (tc->hpd_pin >= 0)
1648136d73a8SSam Ravnborg 		return tc_bridge_detect(&tc->bridge);
1649136d73a8SSam Ravnborg 
1650de5e6c02SSam Ravnborg 	if (tc->panel_bridge)
1651136d73a8SSam Ravnborg 		return connector_status_connected;
1652136d73a8SSam Ravnborg 	else
1653136d73a8SSam Ravnborg 		return connector_status_unknown;
1654136d73a8SSam Ravnborg }
1655136d73a8SSam Ravnborg 
16567caff0fcSAndrey Gusakov static const struct drm_connector_funcs tc_connector_funcs = {
1657f25ee501STomi Valkeinen 	.detect = tc_connector_detect,
16587caff0fcSAndrey Gusakov 	.fill_modes = drm_helper_probe_single_connector_modes,
1659fdd8326aSMarek Vasut 	.destroy = drm_connector_cleanup,
16607caff0fcSAndrey Gusakov 	.reset = drm_atomic_helper_connector_reset,
16617caff0fcSAndrey Gusakov 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
16627caff0fcSAndrey Gusakov 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
16637caff0fcSAndrey Gusakov };
16647caff0fcSAndrey Gusakov 
tc_dpi_bridge_attach(struct drm_bridge * bridge,enum drm_bridge_attach_flags flags)1665bbfd3190SMarek Vasut static int tc_dpi_bridge_attach(struct drm_bridge *bridge,
1666bbfd3190SMarek Vasut 				enum drm_bridge_attach_flags flags)
1667bbfd3190SMarek Vasut {
1668bbfd3190SMarek Vasut 	struct tc_data *tc = bridge_to_tc(bridge);
1669bbfd3190SMarek Vasut 
1670bbfd3190SMarek Vasut 	if (!tc->panel_bridge)
1671bbfd3190SMarek Vasut 		return 0;
1672bbfd3190SMarek Vasut 
1673bbfd3190SMarek Vasut 	return drm_bridge_attach(tc->bridge.encoder, tc->panel_bridge,
1674bbfd3190SMarek Vasut 				 &tc->bridge, flags);
1675bbfd3190SMarek Vasut }
1676bbfd3190SMarek Vasut 
tc_edp_bridge_attach(struct drm_bridge * bridge,enum drm_bridge_attach_flags flags)1677a219062bSMarek Vasut static int tc_edp_bridge_attach(struct drm_bridge *bridge,
1678a25b988fSLaurent Pinchart 				enum drm_bridge_attach_flags flags)
16797caff0fcSAndrey Gusakov {
16807caff0fcSAndrey Gusakov 	u32 bus_format = MEDIA_BUS_FMT_RGB888_1X24;
16817caff0fcSAndrey Gusakov 	struct tc_data *tc = bridge_to_tc(bridge);
16827caff0fcSAndrey Gusakov 	struct drm_device *drm = bridge->dev;
16837caff0fcSAndrey Gusakov 	int ret;
16847caff0fcSAndrey Gusakov 
1685de5e6c02SSam Ravnborg 	if (tc->panel_bridge) {
1686de5e6c02SSam Ravnborg 		/* If a connector is required then this driver shall create it */
1687de5e6c02SSam Ravnborg 		ret = drm_bridge_attach(tc->bridge.encoder, tc->panel_bridge,
1688de5e6c02SSam Ravnborg 					&tc->bridge, flags | DRM_BRIDGE_ATTACH_NO_CONNECTOR);
1689de5e6c02SSam Ravnborg 		if (ret)
1690de5e6c02SSam Ravnborg 			return ret;
1691a25b988fSLaurent Pinchart 	}
1692a25b988fSLaurent Pinchart 
1693de5e6c02SSam Ravnborg 	if (flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)
1694de5e6c02SSam Ravnborg 		return 0;
1695de5e6c02SSam Ravnborg 
16966cba3fe4SLyude Paul 	tc->aux.drm_dev = drm;
169785ddbe2cSLyude Paul 	ret = drm_dp_aux_register(&tc->aux);
169885ddbe2cSLyude Paul 	if (ret < 0)
169985ddbe2cSLyude Paul 		return ret;
170085ddbe2cSLyude Paul 
1701f25ee501STomi Valkeinen 	/* Create DP/eDP connector */
17027caff0fcSAndrey Gusakov 	drm_connector_helper_add(&tc->connector, &tc_connector_helper_funcs);
1703de5e6c02SSam Ravnborg 	ret = drm_connector_init(drm, &tc->connector, &tc_connector_funcs, tc->bridge.type);
17047caff0fcSAndrey Gusakov 	if (ret)
170585ddbe2cSLyude Paul 		goto aux_unregister;
17067caff0fcSAndrey Gusakov 
1707f25ee501STomi Valkeinen 	/* Don't poll if don't have HPD connected */
1708f25ee501STomi Valkeinen 	if (tc->hpd_pin >= 0) {
1709f25ee501STomi Valkeinen 		if (tc->have_irq)
1710f25ee501STomi Valkeinen 			tc->connector.polled = DRM_CONNECTOR_POLL_HPD;
1711f25ee501STomi Valkeinen 		else
1712f25ee501STomi Valkeinen 			tc->connector.polled = DRM_CONNECTOR_POLL_CONNECT |
1713f25ee501STomi Valkeinen 					       DRM_CONNECTOR_POLL_DISCONNECT;
1714f25ee501STomi Valkeinen 	}
1715f25ee501STomi Valkeinen 
17167caff0fcSAndrey Gusakov 	drm_display_info_set_bus_formats(&tc->connector.display_info,
17177caff0fcSAndrey Gusakov 					 &bus_format, 1);
17184842379cSTomi Valkeinen 	tc->connector.display_info.bus_flags =
17194842379cSTomi Valkeinen 		DRM_BUS_FLAG_DE_HIGH |
172088bc4178SLaurent Pinchart 		DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE |
172188bc4178SLaurent Pinchart 		DRM_BUS_FLAG_SYNC_DRIVE_NEGEDGE;
1722cde4c44dSDaniel Vetter 	drm_connector_attach_encoder(&tc->connector, tc->bridge.encoder);
17237caff0fcSAndrey Gusakov 
17247caff0fcSAndrey Gusakov 	return 0;
172585ddbe2cSLyude Paul aux_unregister:
172685ddbe2cSLyude Paul 	drm_dp_aux_unregister(&tc->aux);
172785ddbe2cSLyude Paul 	return ret;
172885ddbe2cSLyude Paul }
172985ddbe2cSLyude Paul 
tc_edp_bridge_detach(struct drm_bridge * bridge)1730a219062bSMarek Vasut static void tc_edp_bridge_detach(struct drm_bridge *bridge)
173185ddbe2cSLyude Paul {
173285ddbe2cSLyude Paul 	drm_dp_aux_unregister(&bridge_to_tc(bridge)->aux);
17337caff0fcSAndrey Gusakov }
17347caff0fcSAndrey Gusakov 
1735bbfd3190SMarek Vasut #define MAX_INPUT_SEL_FORMATS	1
1736bbfd3190SMarek Vasut 
1737bbfd3190SMarek Vasut static u32 *
tc_dpi_atomic_get_input_bus_fmts(struct drm_bridge * bridge,struct drm_bridge_state * bridge_state,struct drm_crtc_state * crtc_state,struct drm_connector_state * conn_state,u32 output_fmt,unsigned int * num_input_fmts)1738bbfd3190SMarek Vasut tc_dpi_atomic_get_input_bus_fmts(struct drm_bridge *bridge,
1739bbfd3190SMarek Vasut 				 struct drm_bridge_state *bridge_state,
1740bbfd3190SMarek Vasut 				 struct drm_crtc_state *crtc_state,
1741bbfd3190SMarek Vasut 				 struct drm_connector_state *conn_state,
1742bbfd3190SMarek Vasut 				 u32 output_fmt,
1743bbfd3190SMarek Vasut 				 unsigned int *num_input_fmts)
1744bbfd3190SMarek Vasut {
1745bbfd3190SMarek Vasut 	u32 *input_fmts;
1746bbfd3190SMarek Vasut 
1747bbfd3190SMarek Vasut 	*num_input_fmts = 0;
1748bbfd3190SMarek Vasut 
1749bbfd3190SMarek Vasut 	input_fmts = kcalloc(MAX_INPUT_SEL_FORMATS, sizeof(*input_fmts),
1750bbfd3190SMarek Vasut 			     GFP_KERNEL);
1751bbfd3190SMarek Vasut 	if (!input_fmts)
1752bbfd3190SMarek Vasut 		return NULL;
1753bbfd3190SMarek Vasut 
1754bbfd3190SMarek Vasut 	/* This is the DSI-end bus format */
1755bbfd3190SMarek Vasut 	input_fmts[0] = MEDIA_BUS_FMT_RGB888_1X24;
1756bbfd3190SMarek Vasut 	*num_input_fmts = 1;
1757bbfd3190SMarek Vasut 
1758bbfd3190SMarek Vasut 	return input_fmts;
1759bbfd3190SMarek Vasut }
1760bbfd3190SMarek Vasut 
1761bbfd3190SMarek Vasut static const struct drm_bridge_funcs tc_dpi_bridge_funcs = {
1762bbfd3190SMarek Vasut 	.attach = tc_dpi_bridge_attach,
1763bbfd3190SMarek Vasut 	.mode_valid = tc_dpi_mode_valid,
1764bbfd3190SMarek Vasut 	.mode_set = tc_bridge_mode_set,
1765bbfd3190SMarek Vasut 	.atomic_check = tc_dpi_atomic_check,
1766bbfd3190SMarek Vasut 	.atomic_enable = tc_dpi_bridge_atomic_enable,
1767bbfd3190SMarek Vasut 	.atomic_disable = tc_dpi_bridge_atomic_disable,
1768bbfd3190SMarek Vasut 	.atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
1769bbfd3190SMarek Vasut 	.atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
1770bbfd3190SMarek Vasut 	.atomic_reset = drm_atomic_helper_bridge_reset,
1771bbfd3190SMarek Vasut 	.atomic_get_input_bus_fmts = tc_dpi_atomic_get_input_bus_fmts,
1772bbfd3190SMarek Vasut };
1773bbfd3190SMarek Vasut 
1774a219062bSMarek Vasut static const struct drm_bridge_funcs tc_edp_bridge_funcs = {
1775a219062bSMarek Vasut 	.attach = tc_edp_bridge_attach,
1776a219062bSMarek Vasut 	.detach = tc_edp_bridge_detach,
1777a219062bSMarek Vasut 	.mode_valid = tc_edp_mode_valid,
17787caff0fcSAndrey Gusakov 	.mode_set = tc_bridge_mode_set,
177965fdbb71SMarek Vasut 	.atomic_check = tc_edp_atomic_check,
1780f5be6239SMarek Vasut 	.atomic_enable = tc_edp_bridge_atomic_enable,
1781f5be6239SMarek Vasut 	.atomic_disable = tc_edp_bridge_atomic_disable,
1782136d73a8SSam Ravnborg 	.detect = tc_bridge_detect,
1783731f4badSSam Ravnborg 	.get_edid = tc_get_edid,
1784f5be6239SMarek Vasut 	.atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
1785f5be6239SMarek Vasut 	.atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
1786f5be6239SMarek Vasut 	.atomic_reset = drm_atomic_helper_bridge_reset,
17877caff0fcSAndrey Gusakov };
17887caff0fcSAndrey Gusakov 
tc_readable_reg(struct device * dev,unsigned int reg)17897caff0fcSAndrey Gusakov static bool tc_readable_reg(struct device *dev, unsigned int reg)
17907caff0fcSAndrey Gusakov {
1791abd686b8SAlexander Stein 	switch (reg) {
1792abd686b8SAlexander Stein 	/* DSI D-PHY Layer */
1793abd686b8SAlexander Stein 	case 0x004:
1794abd686b8SAlexander Stein 	case 0x020:
1795abd686b8SAlexander Stein 	case 0x024:
1796abd686b8SAlexander Stein 	case 0x028:
1797abd686b8SAlexander Stein 	case 0x02c:
1798abd686b8SAlexander Stein 	case 0x030:
1799abd686b8SAlexander Stein 	case 0x038:
1800abd686b8SAlexander Stein 	case 0x040:
1801abd686b8SAlexander Stein 	case 0x044:
1802abd686b8SAlexander Stein 	case 0x048:
1803abd686b8SAlexander Stein 	case 0x04c:
1804abd686b8SAlexander Stein 	case 0x050:
1805abd686b8SAlexander Stein 	case 0x054:
1806abd686b8SAlexander Stein 	/* DSI PPI Layer */
1807abd686b8SAlexander Stein 	case PPI_STARTPPI:
1808abd686b8SAlexander Stein 	case 0x108:
1809abd686b8SAlexander Stein 	case 0x110:
1810abd686b8SAlexander Stein 	case PPI_LPTXTIMECNT:
1811abd686b8SAlexander Stein 	case PPI_LANEENABLE:
1812abd686b8SAlexander Stein 	case PPI_TX_RX_TA:
1813abd686b8SAlexander Stein 	case 0x140:
1814abd686b8SAlexander Stein 	case PPI_D0S_ATMR:
1815abd686b8SAlexander Stein 	case PPI_D1S_ATMR:
1816abd686b8SAlexander Stein 	case 0x14c:
1817abd686b8SAlexander Stein 	case 0x150:
1818abd686b8SAlexander Stein 	case PPI_D0S_CLRSIPOCOUNT:
1819abd686b8SAlexander Stein 	case PPI_D1S_CLRSIPOCOUNT:
1820abd686b8SAlexander Stein 	case PPI_D2S_CLRSIPOCOUNT:
1821abd686b8SAlexander Stein 	case PPI_D3S_CLRSIPOCOUNT:
1822abd686b8SAlexander Stein 	case 0x180:
1823abd686b8SAlexander Stein 	case 0x184:
1824abd686b8SAlexander Stein 	case 0x188:
1825abd686b8SAlexander Stein 	case 0x18c:
1826abd686b8SAlexander Stein 	case 0x190:
1827abd686b8SAlexander Stein 	case 0x1a0:
1828abd686b8SAlexander Stein 	case 0x1a4:
1829abd686b8SAlexander Stein 	case 0x1a8:
1830abd686b8SAlexander Stein 	case 0x1ac:
1831abd686b8SAlexander Stein 	case 0x1b0:
1832abd686b8SAlexander Stein 	case 0x1c0:
1833abd686b8SAlexander Stein 	case 0x1c4:
1834abd686b8SAlexander Stein 	case 0x1c8:
1835abd686b8SAlexander Stein 	case 0x1cc:
1836abd686b8SAlexander Stein 	case 0x1d0:
1837abd686b8SAlexander Stein 	case 0x1e0:
1838abd686b8SAlexander Stein 	case 0x1e4:
1839abd686b8SAlexander Stein 	case 0x1f0:
1840abd686b8SAlexander Stein 	case 0x1f4:
1841abd686b8SAlexander Stein 	/* DSI Protocol Layer */
1842abd686b8SAlexander Stein 	case DSI_STARTDSI:
1843abd686b8SAlexander Stein 	case 0x208:
1844abd686b8SAlexander Stein 	case DSI_LANEENABLE:
1845abd686b8SAlexander Stein 	case 0x214:
1846abd686b8SAlexander Stein 	case 0x218:
1847abd686b8SAlexander Stein 	case 0x220:
1848abd686b8SAlexander Stein 	case 0x224:
1849abd686b8SAlexander Stein 	case 0x228:
1850abd686b8SAlexander Stein 	case 0x230:
1851abd686b8SAlexander Stein 	/* DSI General */
1852abd686b8SAlexander Stein 	case 0x300:
1853abd686b8SAlexander Stein 	/* DSI Application Layer */
1854abd686b8SAlexander Stein 	case 0x400:
1855abd686b8SAlexander Stein 	case 0x404:
1856abd686b8SAlexander Stein 	/* DPI */
1857abd686b8SAlexander Stein 	case DPIPXLFMT:
1858abd686b8SAlexander Stein 	/* Parallel Output */
1859abd686b8SAlexander Stein 	case POCTRL:
1860abd686b8SAlexander Stein 	/* Video Path0 Configuration */
1861abd686b8SAlexander Stein 	case VPCTRL0:
1862abd686b8SAlexander Stein 	case HTIM01:
1863abd686b8SAlexander Stein 	case HTIM02:
1864abd686b8SAlexander Stein 	case VTIM01:
1865abd686b8SAlexander Stein 	case VTIM02:
1866abd686b8SAlexander Stein 	case VFUEN0:
1867abd686b8SAlexander Stein 	/* System */
1868abd686b8SAlexander Stein 	case TC_IDREG:
1869abd686b8SAlexander Stein 	case 0x504:
1870abd686b8SAlexander Stein 	case SYSSTAT:
1871abd686b8SAlexander Stein 	case SYSRSTENB:
1872abd686b8SAlexander Stein 	case SYSCTRL:
1873abd686b8SAlexander Stein 	/* I2C */
1874abd686b8SAlexander Stein 	case 0x520:
1875abd686b8SAlexander Stein 	/* GPIO */
1876abd686b8SAlexander Stein 	case GPIOM:
1877abd686b8SAlexander Stein 	case GPIOC:
1878abd686b8SAlexander Stein 	case GPIOO:
1879abd686b8SAlexander Stein 	case GPIOI:
1880abd686b8SAlexander Stein 	/* Interrupt */
1881abd686b8SAlexander Stein 	case INTCTL_G:
1882abd686b8SAlexander Stein 	case INTSTS_G:
1883abd686b8SAlexander Stein 	case 0x570:
1884abd686b8SAlexander Stein 	case 0x574:
1885abd686b8SAlexander Stein 	case INT_GP0_LCNT:
1886abd686b8SAlexander Stein 	case INT_GP1_LCNT:
1887abd686b8SAlexander Stein 	/* DisplayPort Control */
1888abd686b8SAlexander Stein 	case DP0CTL:
1889abd686b8SAlexander Stein 	/* DisplayPort Clock */
1890abd686b8SAlexander Stein 	case DP0_VIDMNGEN0:
1891abd686b8SAlexander Stein 	case DP0_VIDMNGEN1:
1892abd686b8SAlexander Stein 	case DP0_VMNGENSTATUS:
1893abd686b8SAlexander Stein 	case 0x628:
1894abd686b8SAlexander Stein 	case 0x62c:
1895abd686b8SAlexander Stein 	case 0x630:
1896abd686b8SAlexander Stein 	/* DisplayPort Main Channel */
1897abd686b8SAlexander Stein 	case DP0_SECSAMPLE:
1898abd686b8SAlexander Stein 	case DP0_VIDSYNCDELAY:
1899abd686b8SAlexander Stein 	case DP0_TOTALVAL:
1900abd686b8SAlexander Stein 	case DP0_STARTVAL:
1901abd686b8SAlexander Stein 	case DP0_ACTIVEVAL:
1902abd686b8SAlexander Stein 	case DP0_SYNCVAL:
1903abd686b8SAlexander Stein 	case DP0_MISC:
1904abd686b8SAlexander Stein 	/* DisplayPort Aux Channel */
1905abd686b8SAlexander Stein 	case DP0_AUXCFG0:
1906abd686b8SAlexander Stein 	case DP0_AUXCFG1:
1907abd686b8SAlexander Stein 	case DP0_AUXADDR:
1908abd686b8SAlexander Stein 	case 0x66c:
1909abd686b8SAlexander Stein 	case 0x670:
1910abd686b8SAlexander Stein 	case 0x674:
1911abd686b8SAlexander Stein 	case 0x678:
1912abd686b8SAlexander Stein 	case 0x67c:
1913abd686b8SAlexander Stein 	case 0x680:
1914abd686b8SAlexander Stein 	case 0x684:
1915abd686b8SAlexander Stein 	case 0x688:
1916abd686b8SAlexander Stein 	case DP0_AUXSTATUS:
1917abd686b8SAlexander Stein 	case DP0_AUXI2CADR:
1918abd686b8SAlexander Stein 	/* DisplayPort Link Training */
1919abd686b8SAlexander Stein 	case DP0_SRCCTRL:
1920abd686b8SAlexander Stein 	case DP0_LTSTAT:
1921abd686b8SAlexander Stein 	case DP0_SNKLTCHGREQ:
1922abd686b8SAlexander Stein 	case DP0_LTLOOPCTRL:
1923abd686b8SAlexander Stein 	case DP0_SNKLTCTRL:
1924abd686b8SAlexander Stein 	case 0x6e8:
1925abd686b8SAlexander Stein 	case 0x6ec:
1926abd686b8SAlexander Stein 	case 0x6f0:
1927abd686b8SAlexander Stein 	case 0x6f4:
1928abd686b8SAlexander Stein 	/* DisplayPort Audio */
1929abd686b8SAlexander Stein 	case 0x700:
1930abd686b8SAlexander Stein 	case 0x704:
1931abd686b8SAlexander Stein 	case 0x708:
1932abd686b8SAlexander Stein 	case 0x70c:
1933abd686b8SAlexander Stein 	case 0x710:
1934abd686b8SAlexander Stein 	case 0x714:
1935abd686b8SAlexander Stein 	case 0x718:
1936abd686b8SAlexander Stein 	case 0x71c:
1937abd686b8SAlexander Stein 	case 0x720:
1938abd686b8SAlexander Stein 	/* DisplayPort Source Control */
1939abd686b8SAlexander Stein 	case DP1_SRCCTRL:
1940abd686b8SAlexander Stein 	/* DisplayPort PHY */
1941abd686b8SAlexander Stein 	case DP_PHY_CTRL:
1942abd686b8SAlexander Stein 	case 0x810:
1943abd686b8SAlexander Stein 	case 0x814:
1944abd686b8SAlexander Stein 	case 0x820:
1945abd686b8SAlexander Stein 	case 0x840:
1946abd686b8SAlexander Stein 	/* I2S */
1947abd686b8SAlexander Stein 	case 0x880:
1948abd686b8SAlexander Stein 	case 0x888:
1949abd686b8SAlexander Stein 	case 0x88c:
1950abd686b8SAlexander Stein 	case 0x890:
1951abd686b8SAlexander Stein 	case 0x894:
1952abd686b8SAlexander Stein 	case 0x898:
1953abd686b8SAlexander Stein 	case 0x89c:
1954abd686b8SAlexander Stein 	case 0x8a0:
1955abd686b8SAlexander Stein 	case 0x8a4:
1956abd686b8SAlexander Stein 	case 0x8a8:
1957abd686b8SAlexander Stein 	case 0x8ac:
1958abd686b8SAlexander Stein 	case 0x8b0:
1959abd686b8SAlexander Stein 	case 0x8b4:
1960abd686b8SAlexander Stein 	/* PLL */
1961abd686b8SAlexander Stein 	case DP0_PLLCTRL:
1962abd686b8SAlexander Stein 	case DP1_PLLCTRL:
1963abd686b8SAlexander Stein 	case PXL_PLLCTRL:
1964abd686b8SAlexander Stein 	case PXL_PLLPARAM:
1965abd686b8SAlexander Stein 	case SYS_PLLPARAM:
1966abd686b8SAlexander Stein 	/* HDCP */
1967abd686b8SAlexander Stein 	case 0x980:
1968abd686b8SAlexander Stein 	case 0x984:
1969abd686b8SAlexander Stein 	case 0x988:
1970abd686b8SAlexander Stein 	case 0x98c:
1971abd686b8SAlexander Stein 	case 0x990:
1972abd686b8SAlexander Stein 	case 0x994:
1973abd686b8SAlexander Stein 	case 0x998:
1974abd686b8SAlexander Stein 	case 0x99c:
1975abd686b8SAlexander Stein 	case 0x9a0:
1976abd686b8SAlexander Stein 	case 0x9a4:
1977abd686b8SAlexander Stein 	case 0x9a8:
1978abd686b8SAlexander Stein 	case 0x9ac:
1979abd686b8SAlexander Stein 	/* Debug */
1980abd686b8SAlexander Stein 	case TSTCTL:
1981abd686b8SAlexander Stein 	case PLL_DBG:
1982abd686b8SAlexander Stein 		return true;
1983abd686b8SAlexander Stein 	}
1984abd686b8SAlexander Stein 	return false;
19857caff0fcSAndrey Gusakov }
19867caff0fcSAndrey Gusakov 
19877caff0fcSAndrey Gusakov static const struct regmap_range tc_volatile_ranges[] = {
19887caff0fcSAndrey Gusakov 	regmap_reg_range(DP0_AUXWDATA(0), DP0_AUXSTATUS),
19897caff0fcSAndrey Gusakov 	regmap_reg_range(DP0_LTSTAT, DP0_SNKLTCHGREQ),
19907caff0fcSAndrey Gusakov 	regmap_reg_range(DP_PHY_CTRL, DP_PHY_CTRL),
19917caff0fcSAndrey Gusakov 	regmap_reg_range(DP0_PLLCTRL, PXL_PLLCTRL),
19927caff0fcSAndrey Gusakov 	regmap_reg_range(VFUEN0, VFUEN0),
1993af9526f2STomi Valkeinen 	regmap_reg_range(INTSTS_G, INTSTS_G),
1994af9526f2STomi Valkeinen 	regmap_reg_range(GPIOI, GPIOI),
19957caff0fcSAndrey Gusakov };
19967caff0fcSAndrey Gusakov 
19977caff0fcSAndrey Gusakov static const struct regmap_access_table tc_volatile_table = {
19987caff0fcSAndrey Gusakov 	.yes_ranges = tc_volatile_ranges,
19997caff0fcSAndrey Gusakov 	.n_yes_ranges = ARRAY_SIZE(tc_volatile_ranges),
20007caff0fcSAndrey Gusakov };
20017caff0fcSAndrey Gusakov 
tc_writeable_reg(struct device * dev,unsigned int reg)20027caff0fcSAndrey Gusakov static bool tc_writeable_reg(struct device *dev, unsigned int reg)
20037caff0fcSAndrey Gusakov {
20047caff0fcSAndrey Gusakov 	return (reg != TC_IDREG) &&
20057caff0fcSAndrey Gusakov 	       (reg != DP0_LTSTAT) &&
20067caff0fcSAndrey Gusakov 	       (reg != DP0_SNKLTCHGREQ);
20077caff0fcSAndrey Gusakov }
20087caff0fcSAndrey Gusakov 
20097caff0fcSAndrey Gusakov static const struct regmap_config tc_regmap_config = {
20107caff0fcSAndrey Gusakov 	.name = "tc358767",
20117caff0fcSAndrey Gusakov 	.reg_bits = 16,
20127caff0fcSAndrey Gusakov 	.val_bits = 32,
20137caff0fcSAndrey Gusakov 	.reg_stride = 4,
20147caff0fcSAndrey Gusakov 	.max_register = PLL_DBG,
20157caff0fcSAndrey Gusakov 	.cache_type = REGCACHE_RBTREE,
20167caff0fcSAndrey Gusakov 	.readable_reg = tc_readable_reg,
20177caff0fcSAndrey Gusakov 	.volatile_table = &tc_volatile_table,
20187caff0fcSAndrey Gusakov 	.writeable_reg = tc_writeable_reg,
20197caff0fcSAndrey Gusakov 	.reg_format_endian = REGMAP_ENDIAN_BIG,
20207caff0fcSAndrey Gusakov 	.val_format_endian = REGMAP_ENDIAN_LITTLE,
20217caff0fcSAndrey Gusakov };
20227caff0fcSAndrey Gusakov 
tc_irq_handler(int irq,void * arg)2023f25ee501STomi Valkeinen static irqreturn_t tc_irq_handler(int irq, void *arg)
2024f25ee501STomi Valkeinen {
2025f25ee501STomi Valkeinen 	struct tc_data *tc = arg;
2026f25ee501STomi Valkeinen 	u32 val;
2027f25ee501STomi Valkeinen 	int r;
2028f25ee501STomi Valkeinen 
2029f25ee501STomi Valkeinen 	r = regmap_read(tc->regmap, INTSTS_G, &val);
2030f25ee501STomi Valkeinen 	if (r)
2031f25ee501STomi Valkeinen 		return IRQ_NONE;
2032f25ee501STomi Valkeinen 
2033f25ee501STomi Valkeinen 	if (!val)
2034f25ee501STomi Valkeinen 		return IRQ_NONE;
2035f25ee501STomi Valkeinen 
2036f25ee501STomi Valkeinen 	if (val & INT_SYSERR) {
2037f25ee501STomi Valkeinen 		u32 stat = 0;
2038f25ee501STomi Valkeinen 
2039f25ee501STomi Valkeinen 		regmap_read(tc->regmap, SYSSTAT, &stat);
2040f25ee501STomi Valkeinen 
2041f25ee501STomi Valkeinen 		dev_err(tc->dev, "syserr %x\n", stat);
2042f25ee501STomi Valkeinen 	}
2043f25ee501STomi Valkeinen 
20449d567126SMarek Vasut 	if (tc->hpd_pin >= 0 && tc->bridge.dev && tc->aux.drm_dev) {
2045f25ee501STomi Valkeinen 		/*
2046f25ee501STomi Valkeinen 		 * H is triggered when the GPIO goes high.
2047f25ee501STomi Valkeinen 		 *
2048f25ee501STomi Valkeinen 		 * LC is triggered when the GPIO goes low and stays low for
2049f25ee501STomi Valkeinen 		 * the duration of LCNT
2050f25ee501STomi Valkeinen 		 */
2051f25ee501STomi Valkeinen 		bool h = val & INT_GPIO_H(tc->hpd_pin);
2052f25ee501STomi Valkeinen 		bool lc = val & INT_GPIO_LC(tc->hpd_pin);
2053f25ee501STomi Valkeinen 
2054f25ee501STomi Valkeinen 		dev_dbg(tc->dev, "GPIO%d: %s %s\n", tc->hpd_pin,
2055f25ee501STomi Valkeinen 			h ? "H" : "", lc ? "LC" : "");
2056f25ee501STomi Valkeinen 
2057f25ee501STomi Valkeinen 		if (h || lc)
2058f25ee501STomi Valkeinen 			drm_kms_helper_hotplug_event(tc->bridge.dev);
2059f25ee501STomi Valkeinen 	}
2060f25ee501STomi Valkeinen 
2061f25ee501STomi Valkeinen 	regmap_write(tc->regmap, INTSTS_G, val);
2062f25ee501STomi Valkeinen 
2063f25ee501STomi Valkeinen 	return IRQ_HANDLED;
2064f25ee501STomi Valkeinen }
2065f25ee501STomi Valkeinen 
tc_mipi_dsi_host_attach(struct tc_data * tc)2066bbfd3190SMarek Vasut static int tc_mipi_dsi_host_attach(struct tc_data *tc)
2067bbfd3190SMarek Vasut {
2068bbfd3190SMarek Vasut 	struct device *dev = tc->dev;
2069bbfd3190SMarek Vasut 	struct device_node *host_node;
2070bbfd3190SMarek Vasut 	struct device_node *endpoint;
2071bbfd3190SMarek Vasut 	struct mipi_dsi_device *dsi;
2072bbfd3190SMarek Vasut 	struct mipi_dsi_host *host;
2073bbfd3190SMarek Vasut 	const struct mipi_dsi_device_info info = {
2074bbfd3190SMarek Vasut 		.type = "tc358767",
2075bbfd3190SMarek Vasut 		.channel = 0,
2076bbfd3190SMarek Vasut 		.node = NULL,
2077bbfd3190SMarek Vasut 	};
2078bbfd3190SMarek Vasut 	int dsi_lanes, ret;
2079bbfd3190SMarek Vasut 
2080bbfd3190SMarek Vasut 	endpoint = of_graph_get_endpoint_by_regs(dev->of_node, 0, -1);
2081d8609fd1SMarek Vasut 	dsi_lanes = drm_of_get_data_lanes_count(endpoint, 1, 4);
2082bbfd3190SMarek Vasut 	host_node = of_graph_get_remote_port_parent(endpoint);
2083bbfd3190SMarek Vasut 	host = of_find_mipi_dsi_host_by_node(host_node);
2084bbfd3190SMarek Vasut 	of_node_put(host_node);
2085bbfd3190SMarek Vasut 	of_node_put(endpoint);
2086bbfd3190SMarek Vasut 
2087bbfd3190SMarek Vasut 	if (!host)
2088bbfd3190SMarek Vasut 		return -EPROBE_DEFER;
2089bbfd3190SMarek Vasut 
2090d8609fd1SMarek Vasut 	if (dsi_lanes < 0)
2091d8609fd1SMarek Vasut 		return dsi_lanes;
2092d8609fd1SMarek Vasut 
2093f47d6140SAlexander Stein 	dsi = devm_mipi_dsi_device_register_full(dev, host, &info);
2094bbfd3190SMarek Vasut 	if (IS_ERR(dsi))
2095bbfd3190SMarek Vasut 		return dev_err_probe(dev, PTR_ERR(dsi),
2096bbfd3190SMarek Vasut 				     "failed to create dsi device\n");
2097bbfd3190SMarek Vasut 
2098bbfd3190SMarek Vasut 	tc->dsi = dsi;
20995bdaaf4fSMarek Vasut 	dsi->lanes = dsi_lanes;
2100bbfd3190SMarek Vasut 	dsi->format = MIPI_DSI_FMT_RGB888;
21012792aed1SMarek Vasut 	dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_BURST |
21022792aed1SMarek Vasut 			  MIPI_DSI_MODE_LPM | MIPI_DSI_CLOCK_NON_CONTINUOUS;
2103bbfd3190SMarek Vasut 
2104f47d6140SAlexander Stein 	ret = devm_mipi_dsi_attach(dev, dsi);
2105bbfd3190SMarek Vasut 	if (ret < 0) {
2106bbfd3190SMarek Vasut 		dev_err(dev, "failed to attach dsi to host: %d\n", ret);
2107bbfd3190SMarek Vasut 		return ret;
2108bbfd3190SMarek Vasut 	}
2109bbfd3190SMarek Vasut 
2110bbfd3190SMarek Vasut 	return 0;
2111bbfd3190SMarek Vasut }
2112bbfd3190SMarek Vasut 
tc_probe_dpi_bridge_endpoint(struct tc_data * tc)2113bbfd3190SMarek Vasut static int tc_probe_dpi_bridge_endpoint(struct tc_data *tc)
2114bbfd3190SMarek Vasut {
2115bbfd3190SMarek Vasut 	struct device *dev = tc->dev;
2116d688f6b5SMarek Vasut 	struct drm_bridge *bridge;
2117bbfd3190SMarek Vasut 	struct drm_panel *panel;
2118bbfd3190SMarek Vasut 	int ret;
2119bbfd3190SMarek Vasut 
2120bbfd3190SMarek Vasut 	/* port@1 is the DPI input/output port */
2121d688f6b5SMarek Vasut 	ret = drm_of_find_panel_or_bridge(dev->of_node, 1, 0, &panel, &bridge);
2122bbfd3190SMarek Vasut 	if (ret && ret != -ENODEV)
2123bbfd3190SMarek Vasut 		return ret;
2124bbfd3190SMarek Vasut 
2125bbfd3190SMarek Vasut 	if (panel) {
2126d688f6b5SMarek Vasut 		bridge = devm_drm_panel_bridge_add(dev, panel);
2127d688f6b5SMarek Vasut 		if (IS_ERR(bridge))
2128d688f6b5SMarek Vasut 			return PTR_ERR(bridge);
2129d688f6b5SMarek Vasut 	}
2130bbfd3190SMarek Vasut 
2131d688f6b5SMarek Vasut 	if (bridge) {
2132d688f6b5SMarek Vasut 		tc->panel_bridge = bridge;
2133bbfd3190SMarek Vasut 		tc->bridge.type = DRM_MODE_CONNECTOR_DPI;
2134bbfd3190SMarek Vasut 		tc->bridge.funcs = &tc_dpi_bridge_funcs;
2135bbfd3190SMarek Vasut 
2136bbfd3190SMarek Vasut 		return 0;
2137bbfd3190SMarek Vasut 	}
2138bbfd3190SMarek Vasut 
2139bbfd3190SMarek Vasut 	return ret;
2140bbfd3190SMarek Vasut }
2141bbfd3190SMarek Vasut 
tc_probe_edp_bridge_endpoint(struct tc_data * tc)21428478095aSMarek Vasut static int tc_probe_edp_bridge_endpoint(struct tc_data *tc)
21437caff0fcSAndrey Gusakov {
21448478095aSMarek Vasut 	struct device *dev = tc->dev;
2145de5e6c02SSam Ravnborg 	struct drm_panel *panel;
21467caff0fcSAndrey Gusakov 	int ret;
21477caff0fcSAndrey Gusakov 
21487caff0fcSAndrey Gusakov 	/* port@2 is the output port */
2149de5e6c02SSam Ravnborg 	ret = drm_of_find_panel_or_bridge(dev->of_node, 2, 0, &panel, NULL);
2150d630213fSLucas Stach 	if (ret && ret != -ENODEV)
2151ebc94461SRob Herring 		return ret;
21527caff0fcSAndrey Gusakov 
2153de5e6c02SSam Ravnborg 	if (panel) {
2154de5e6c02SSam Ravnborg 		struct drm_bridge *panel_bridge;
2155de5e6c02SSam Ravnborg 
2156de5e6c02SSam Ravnborg 		panel_bridge = devm_drm_panel_bridge_add(dev, panel);
2157de5e6c02SSam Ravnborg 		if (IS_ERR(panel_bridge))
2158de5e6c02SSam Ravnborg 			return PTR_ERR(panel_bridge);
2159de5e6c02SSam Ravnborg 
2160de5e6c02SSam Ravnborg 		tc->panel_bridge = panel_bridge;
2161de5e6c02SSam Ravnborg 		tc->bridge.type = DRM_MODE_CONNECTOR_eDP;
2162de5e6c02SSam Ravnborg 	} else {
2163de5e6c02SSam Ravnborg 		tc->bridge.type = DRM_MODE_CONNECTOR_DisplayPort;
2164de5e6c02SSam Ravnborg 	}
2165de5e6c02SSam Ravnborg 
2166dd1fd5abSMarek Vasut 	tc->bridge.funcs = &tc_edp_bridge_funcs;
2167dd1fd5abSMarek Vasut 	if (tc->hpd_pin >= 0)
2168dd1fd5abSMarek Vasut 		tc->bridge.ops |= DRM_BRIDGE_OP_DETECT;
2169dd1fd5abSMarek Vasut 	tc->bridge.ops |= DRM_BRIDGE_OP_EDID;
2170dd1fd5abSMarek Vasut 
21719030a9e5SMarek Vasut 	return 0;
21728478095aSMarek Vasut }
21738478095aSMarek Vasut 
tc_probe_bridge_endpoint(struct tc_data * tc)217471f7d9c0SMarek Vasut static int tc_probe_bridge_endpoint(struct tc_data *tc)
217571f7d9c0SMarek Vasut {
217671f7d9c0SMarek Vasut 	struct device *dev = tc->dev;
217771f7d9c0SMarek Vasut 	struct of_endpoint endpoint;
217871f7d9c0SMarek Vasut 	struct device_node *node = NULL;
217971f7d9c0SMarek Vasut 	const u8 mode_dpi_to_edp = BIT(1) | BIT(2);
21801bb533b6SMarek Vasut 	const u8 mode_dpi_to_dp = BIT(1);
218171f7d9c0SMarek Vasut 	const u8 mode_dsi_to_edp = BIT(0) | BIT(2);
21821bb533b6SMarek Vasut 	const u8 mode_dsi_to_dp = BIT(0);
218371f7d9c0SMarek Vasut 	const u8 mode_dsi_to_dpi = BIT(0) | BIT(1);
218471f7d9c0SMarek Vasut 	u8 mode = 0;
218571f7d9c0SMarek Vasut 
218671f7d9c0SMarek Vasut 	/*
218771f7d9c0SMarek Vasut 	 * Determine bridge configuration.
218871f7d9c0SMarek Vasut 	 *
218971f7d9c0SMarek Vasut 	 * Port allocation:
219071f7d9c0SMarek Vasut 	 * port@0 - DSI input
219171f7d9c0SMarek Vasut 	 * port@1 - DPI input/output
219271f7d9c0SMarek Vasut 	 * port@2 - eDP output
219371f7d9c0SMarek Vasut 	 *
219471f7d9c0SMarek Vasut 	 * Possible connections:
219571f7d9c0SMarek Vasut 	 * DPI -> port@1 -> port@2 -> eDP :: [port@0 is not connected]
219671f7d9c0SMarek Vasut 	 * DSI -> port@0 -> port@2 -> eDP :: [port@1 is not connected]
219771f7d9c0SMarek Vasut 	 * DSI -> port@0 -> port@1 -> DPI :: [port@2 is not connected]
219871f7d9c0SMarek Vasut 	 */
219971f7d9c0SMarek Vasut 
220071f7d9c0SMarek Vasut 	for_each_endpoint_of_node(dev->of_node, node) {
220171f7d9c0SMarek Vasut 		of_graph_parse_endpoint(node, &endpoint);
220214e7157aSLiang He 		if (endpoint.port > 2) {
220314e7157aSLiang He 			of_node_put(node);
220471f7d9c0SMarek Vasut 			return -EINVAL;
220514e7157aSLiang He 		}
220671f7d9c0SMarek Vasut 		mode |= BIT(endpoint.port);
220771f7d9c0SMarek Vasut 	}
220871f7d9c0SMarek Vasut 
22093080c21aSMarek Vasut 	if (mode == mode_dpi_to_edp || mode == mode_dpi_to_dp) {
22103080c21aSMarek Vasut 		tc->input_connector_dsi = false;
221171f7d9c0SMarek Vasut 		return tc_probe_edp_bridge_endpoint(tc);
22123080c21aSMarek Vasut 	} else if (mode == mode_dsi_to_dpi) {
22133080c21aSMarek Vasut 		tc->input_connector_dsi = true;
2214bbfd3190SMarek Vasut 		return tc_probe_dpi_bridge_endpoint(tc);
22153080c21aSMarek Vasut 	} else if (mode == mode_dsi_to_edp || mode == mode_dsi_to_dp) {
22163080c21aSMarek Vasut 		tc->input_connector_dsi = true;
22173080c21aSMarek Vasut 		return tc_probe_edp_bridge_endpoint(tc);
22183080c21aSMarek Vasut 	}
22193080c21aSMarek Vasut 
222071f7d9c0SMarek Vasut 	dev_warn(dev, "Invalid mode (0x%x) is not supported!\n", mode);
222171f7d9c0SMarek Vasut 
222271f7d9c0SMarek Vasut 	return -EINVAL;
222371f7d9c0SMarek Vasut }
222471f7d9c0SMarek Vasut 
tc_probe(struct i2c_client * client)222539fffc9dSUwe Kleine-König static int tc_probe(struct i2c_client *client)
22268478095aSMarek Vasut {
22278478095aSMarek Vasut 	struct device *dev = &client->dev;
22288478095aSMarek Vasut 	struct tc_data *tc;
22298478095aSMarek Vasut 	int ret;
22308478095aSMarek Vasut 
22318478095aSMarek Vasut 	tc = devm_kzalloc(dev, sizeof(*tc), GFP_KERNEL);
22328478095aSMarek Vasut 	if (!tc)
22338478095aSMarek Vasut 		return -ENOMEM;
22348478095aSMarek Vasut 
22358478095aSMarek Vasut 	tc->dev = dev;
22368478095aSMarek Vasut 
223771f7d9c0SMarek Vasut 	ret = tc_probe_bridge_endpoint(tc);
22388478095aSMarek Vasut 	if (ret)
22398478095aSMarek Vasut 		return ret;
22408478095aSMarek Vasut 
224170d3c92dSChristophe JAILLET 	tc->refclk = devm_clk_get_enabled(dev, "ref");
224270d3c92dSChristophe JAILLET 	if (IS_ERR(tc->refclk))
224370d3c92dSChristophe JAILLET 		return dev_err_probe(dev, PTR_ERR(tc->refclk),
224470d3c92dSChristophe JAILLET 				     "Failed to get and enable the ref clk\n");
22450b4c48f3SMarek Vasut 
22460b4c48f3SMarek Vasut 	/* tRSTW = 100 cycles , at 13 MHz that is ~7.69 us */
22470b4c48f3SMarek Vasut 	usleep_range(10, 15);
22480b4c48f3SMarek Vasut 
22497caff0fcSAndrey Gusakov 	/* Shut down GPIO is optional */
22507caff0fcSAndrey Gusakov 	tc->sd_gpio = devm_gpiod_get_optional(dev, "shutdown", GPIOD_OUT_HIGH);
22517caff0fcSAndrey Gusakov 	if (IS_ERR(tc->sd_gpio))
22527caff0fcSAndrey Gusakov 		return PTR_ERR(tc->sd_gpio);
22537caff0fcSAndrey Gusakov 
22547caff0fcSAndrey Gusakov 	if (tc->sd_gpio) {
22557caff0fcSAndrey Gusakov 		gpiod_set_value_cansleep(tc->sd_gpio, 0);
22567caff0fcSAndrey Gusakov 		usleep_range(5000, 10000);
22577caff0fcSAndrey Gusakov 	}
22587caff0fcSAndrey Gusakov 
22597caff0fcSAndrey Gusakov 	/* Reset GPIO is optional */
22607caff0fcSAndrey Gusakov 	tc->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
22617caff0fcSAndrey Gusakov 	if (IS_ERR(tc->reset_gpio))
22627caff0fcSAndrey Gusakov 		return PTR_ERR(tc->reset_gpio);
22637caff0fcSAndrey Gusakov 
22647caff0fcSAndrey Gusakov 	if (tc->reset_gpio) {
22657caff0fcSAndrey Gusakov 		gpiod_set_value_cansleep(tc->reset_gpio, 1);
22667caff0fcSAndrey Gusakov 		usleep_range(5000, 10000);
22677caff0fcSAndrey Gusakov 	}
22687caff0fcSAndrey Gusakov 
22697caff0fcSAndrey Gusakov 	tc->regmap = devm_regmap_init_i2c(client, &tc_regmap_config);
22707caff0fcSAndrey Gusakov 	if (IS_ERR(tc->regmap)) {
22717caff0fcSAndrey Gusakov 		ret = PTR_ERR(tc->regmap);
22727caff0fcSAndrey Gusakov 		dev_err(dev, "Failed to initialize regmap: %d\n", ret);
22737caff0fcSAndrey Gusakov 		return ret;
22747caff0fcSAndrey Gusakov 	}
22757caff0fcSAndrey Gusakov 
2276f25ee501STomi Valkeinen 	ret = of_property_read_u32(dev->of_node, "toshiba,hpd-pin",
2277f25ee501STomi Valkeinen 				   &tc->hpd_pin);
2278f25ee501STomi Valkeinen 	if (ret) {
2279f25ee501STomi Valkeinen 		tc->hpd_pin = -ENODEV;
2280f25ee501STomi Valkeinen 	} else {
2281f25ee501STomi Valkeinen 		if (tc->hpd_pin < 0 || tc->hpd_pin > 1) {
2282f25ee501STomi Valkeinen 			dev_err(dev, "failed to parse HPD number\n");
22833258bc1fSTomi Valkeinen 			return -EINVAL;
2284f25ee501STomi Valkeinen 		}
2285f25ee501STomi Valkeinen 	}
2286f25ee501STomi Valkeinen 
2287f25ee501STomi Valkeinen 	if (client->irq > 0) {
2288f25ee501STomi Valkeinen 		/* enable SysErr */
2289f25ee501STomi Valkeinen 		regmap_write(tc->regmap, INTCTL_G, INT_SYSERR);
2290f25ee501STomi Valkeinen 
2291f25ee501STomi Valkeinen 		ret = devm_request_threaded_irq(dev, client->irq,
2292f25ee501STomi Valkeinen 						NULL, tc_irq_handler,
2293f25ee501STomi Valkeinen 						IRQF_ONESHOT,
2294f25ee501STomi Valkeinen 						"tc358767-irq", tc);
2295f25ee501STomi Valkeinen 		if (ret) {
2296f25ee501STomi Valkeinen 			dev_err(dev, "failed to register dp interrupt\n");
2297f25ee501STomi Valkeinen 			return ret;
2298f25ee501STomi Valkeinen 		}
2299f25ee501STomi Valkeinen 
2300f25ee501STomi Valkeinen 		tc->have_irq = true;
2301f25ee501STomi Valkeinen 	}
2302f25ee501STomi Valkeinen 
23037caff0fcSAndrey Gusakov 	ret = regmap_read(tc->regmap, TC_IDREG, &tc->rev);
23047caff0fcSAndrey Gusakov 	if (ret) {
23057caff0fcSAndrey Gusakov 		dev_err(tc->dev, "can not read device ID: %d\n", ret);
23067caff0fcSAndrey Gusakov 		return ret;
23077caff0fcSAndrey Gusakov 	}
23087caff0fcSAndrey Gusakov 
23097caff0fcSAndrey Gusakov 	if ((tc->rev != 0x6601) && (tc->rev != 0x6603)) {
23107caff0fcSAndrey Gusakov 		dev_err(tc->dev, "invalid device ID: 0x%08x\n", tc->rev);
23117caff0fcSAndrey Gusakov 		return -EINVAL;
23127caff0fcSAndrey Gusakov 	}
23137caff0fcSAndrey Gusakov 
23147caff0fcSAndrey Gusakov 	tc->assr = (tc->rev == 0x6601); /* Enable ASSR for eDP panels */
23157caff0fcSAndrey Gusakov 
231652c2197aSLucas Stach 	if (!tc->reset_gpio) {
231752c2197aSLucas Stach 		/*
231852c2197aSLucas Stach 		 * If the reset pin isn't present, do a software reset. It isn't
231952c2197aSLucas Stach 		 * as thorough as the hardware reset, as we can't reset the I2C
232052c2197aSLucas Stach 		 * communication block for obvious reasons, but it's getting the
232152c2197aSLucas Stach 		 * chip into a defined state.
232252c2197aSLucas Stach 		 */
232352c2197aSLucas Stach 		regmap_update_bits(tc->regmap, SYSRSTENB,
232452c2197aSLucas Stach 				ENBLCD0 | ENBBM | ENBDSIRX | ENBREG | ENBHDCP,
232552c2197aSLucas Stach 				0);
232652c2197aSLucas Stach 		regmap_update_bits(tc->regmap, SYSRSTENB,
232752c2197aSLucas Stach 				ENBLCD0 | ENBBM | ENBDSIRX | ENBREG | ENBHDCP,
232852c2197aSLucas Stach 				ENBLCD0 | ENBBM | ENBDSIRX | ENBREG | ENBHDCP);
232952c2197aSLucas Stach 		usleep_range(5000, 10000);
233052c2197aSLucas Stach 	}
233152c2197aSLucas Stach 
2332f25ee501STomi Valkeinen 	if (tc->hpd_pin >= 0) {
2333f25ee501STomi Valkeinen 		u32 lcnt_reg = tc->hpd_pin == 0 ? INT_GP0_LCNT : INT_GP1_LCNT;
2334f25ee501STomi Valkeinen 		u32 h_lc = INT_GPIO_H(tc->hpd_pin) | INT_GPIO_LC(tc->hpd_pin);
2335f25ee501STomi Valkeinen 
2336f25ee501STomi Valkeinen 		/* Set LCNT to 2ms */
2337f25ee501STomi Valkeinen 		regmap_write(tc->regmap, lcnt_reg,
2338f25ee501STomi Valkeinen 			     clk_get_rate(tc->refclk) * 2 / 1000);
2339f25ee501STomi Valkeinen 		/* We need the "alternate" mode for HPD */
2340f25ee501STomi Valkeinen 		regmap_write(tc->regmap, GPIOM, BIT(tc->hpd_pin));
2341f25ee501STomi Valkeinen 
2342f25ee501STomi Valkeinen 		if (tc->have_irq) {
2343f25ee501STomi Valkeinen 			/* enable H & LC */
2344f25ee501STomi Valkeinen 			regmap_update_bits(tc->regmap, INTCTL_G, h_lc, h_lc);
2345f25ee501STomi Valkeinen 		}
2346f25ee501STomi Valkeinen 	}
2347f25ee501STomi Valkeinen 
2348bbfd3190SMarek Vasut 	if (tc->bridge.type != DRM_MODE_CONNECTOR_DPI) { /* (e)DP output */
23497caff0fcSAndrey Gusakov 		ret = tc_aux_link_setup(tc);
23507caff0fcSAndrey Gusakov 		if (ret)
23517caff0fcSAndrey Gusakov 			return ret;
2352bbfd3190SMarek Vasut 	}
23537caff0fcSAndrey Gusakov 
23547caff0fcSAndrey Gusakov 	tc->bridge.of_node = dev->of_node;
2355dc01732eSInki Dae 	drm_bridge_add(&tc->bridge);
23567caff0fcSAndrey Gusakov 
23577caff0fcSAndrey Gusakov 	i2c_set_clientdata(client, tc);
23587caff0fcSAndrey Gusakov 
23593080c21aSMarek Vasut 	if (tc->input_connector_dsi) {			/* DSI input */
2360bbfd3190SMarek Vasut 		ret = tc_mipi_dsi_host_attach(tc);
2361bbfd3190SMarek Vasut 		if (ret) {
2362bbfd3190SMarek Vasut 			drm_bridge_remove(&tc->bridge);
2363bbfd3190SMarek Vasut 			return ret;
2364bbfd3190SMarek Vasut 		}
2365bbfd3190SMarek Vasut 	}
2366bbfd3190SMarek Vasut 
23677caff0fcSAndrey Gusakov 	return 0;
23687caff0fcSAndrey Gusakov }
23697caff0fcSAndrey Gusakov 
tc_remove(struct i2c_client * client)2370ed5c2f5fSUwe Kleine-König static void tc_remove(struct i2c_client *client)
23717caff0fcSAndrey Gusakov {
23727caff0fcSAndrey Gusakov 	struct tc_data *tc = i2c_get_clientdata(client);
23737caff0fcSAndrey Gusakov 
23747caff0fcSAndrey Gusakov 	drm_bridge_remove(&tc->bridge);
23757caff0fcSAndrey Gusakov }
23767caff0fcSAndrey Gusakov 
23777caff0fcSAndrey Gusakov static const struct i2c_device_id tc358767_i2c_ids[] = {
23787caff0fcSAndrey Gusakov 	{ "tc358767", 0 },
23797caff0fcSAndrey Gusakov 	{ }
23807caff0fcSAndrey Gusakov };
23817caff0fcSAndrey Gusakov MODULE_DEVICE_TABLE(i2c, tc358767_i2c_ids);
23827caff0fcSAndrey Gusakov 
23837caff0fcSAndrey Gusakov static const struct of_device_id tc358767_of_ids[] = {
23847caff0fcSAndrey Gusakov 	{ .compatible = "toshiba,tc358767", },
23857caff0fcSAndrey Gusakov 	{ }
23867caff0fcSAndrey Gusakov };
23877caff0fcSAndrey Gusakov MODULE_DEVICE_TABLE(of, tc358767_of_ids);
23887caff0fcSAndrey Gusakov 
23897caff0fcSAndrey Gusakov static struct i2c_driver tc358767_driver = {
23907caff0fcSAndrey Gusakov 	.driver = {
23917caff0fcSAndrey Gusakov 		.name = "tc358767",
23927caff0fcSAndrey Gusakov 		.of_match_table = tc358767_of_ids,
23937caff0fcSAndrey Gusakov 	},
23947caff0fcSAndrey Gusakov 	.id_table = tc358767_i2c_ids,
2395332af828SUwe Kleine-König 	.probe = tc_probe,
23967caff0fcSAndrey Gusakov 	.remove	= tc_remove,
23977caff0fcSAndrey Gusakov };
23987caff0fcSAndrey Gusakov module_i2c_driver(tc358767_driver);
23997caff0fcSAndrey Gusakov 
24007caff0fcSAndrey Gusakov MODULE_AUTHOR("Andrey Gusakov <andrey.gusakov@cogentembedded.com>");
24017caff0fcSAndrey Gusakov MODULE_DESCRIPTION("tc358767 eDP encoder driver");
24027caff0fcSAndrey Gusakov MODULE_LICENSE("GPL");
2403