xref: /openbmc/linux/drivers/gpu/drm/bridge/tc358767.c (revision a219062b9a87fd0a1591266413e62596d9bd8316)
1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
27caff0fcSAndrey Gusakov /*
37caff0fcSAndrey Gusakov  * tc358767 eDP bridge driver
47caff0fcSAndrey Gusakov  *
57caff0fcSAndrey Gusakov  * Copyright (C) 2016 CogentEmbedded Inc
67caff0fcSAndrey Gusakov  * Author: Andrey Gusakov <andrey.gusakov@cogentembedded.com>
77caff0fcSAndrey Gusakov  *
87caff0fcSAndrey Gusakov  * Copyright (C) 2016 Pengutronix, Philipp Zabel <p.zabel@pengutronix.de>
97caff0fcSAndrey Gusakov  *
102f51be09SAndrey Gusakov  * Copyright (C) 2016 Zodiac Inflight Innovations
112f51be09SAndrey Gusakov  *
127caff0fcSAndrey Gusakov  * Initially based on: drivers/gpu/drm/i2c/tda998x_drv.c
137caff0fcSAndrey Gusakov  *
147caff0fcSAndrey Gusakov  * Copyright (C) 2012 Texas Instruments
157caff0fcSAndrey Gusakov  * Author: Rob Clark <robdclark@gmail.com>
167caff0fcSAndrey Gusakov  */
177caff0fcSAndrey Gusakov 
183f072c30SAndrey Smirnov #include <linux/bitfield.h>
197caff0fcSAndrey Gusakov #include <linux/clk.h>
207caff0fcSAndrey Gusakov #include <linux/device.h>
217caff0fcSAndrey Gusakov #include <linux/gpio/consumer.h>
227caff0fcSAndrey Gusakov #include <linux/i2c.h>
237caff0fcSAndrey Gusakov #include <linux/kernel.h>
247caff0fcSAndrey Gusakov #include <linux/module.h>
257caff0fcSAndrey Gusakov #include <linux/regmap.h>
267caff0fcSAndrey Gusakov #include <linux/slab.h>
277caff0fcSAndrey Gusakov 
287caff0fcSAndrey Gusakov #include <drm/drm_atomic_helper.h>
29ee68c743SBoris Brezillon #include <drm/drm_bridge.h>
305b529e8dSThomas Zimmermann #include <drm/dp/drm_dp_helper.h>
317caff0fcSAndrey Gusakov #include <drm/drm_edid.h>
327caff0fcSAndrey Gusakov #include <drm/drm_of.h>
337caff0fcSAndrey Gusakov #include <drm/drm_panel.h>
34a25b988fSLaurent Pinchart #include <drm/drm_print.h>
35fcd70cd3SDaniel Vetter #include <drm/drm_probe_helper.h>
367caff0fcSAndrey Gusakov 
377caff0fcSAndrey Gusakov /* Registers */
387caff0fcSAndrey Gusakov 
397caff0fcSAndrey Gusakov /* Display Parallel Interface */
407caff0fcSAndrey Gusakov #define DPIPXLFMT		0x0440
417caff0fcSAndrey Gusakov #define VS_POL_ACTIVE_LOW		(1 << 10)
427caff0fcSAndrey Gusakov #define HS_POL_ACTIVE_LOW		(1 << 9)
437caff0fcSAndrey Gusakov #define DE_POL_ACTIVE_HIGH		(0 << 8)
447caff0fcSAndrey Gusakov #define SUB_CFG_TYPE_CONFIG1		(0 << 2) /* LSB aligned */
457caff0fcSAndrey Gusakov #define SUB_CFG_TYPE_CONFIG2		(1 << 2) /* Loosely Packed */
467caff0fcSAndrey Gusakov #define SUB_CFG_TYPE_CONFIG3		(2 << 2) /* LSB aligned 8-bit */
477caff0fcSAndrey Gusakov #define DPI_BPP_RGB888			(0 << 0)
487caff0fcSAndrey Gusakov #define DPI_BPP_RGB666			(1 << 0)
497caff0fcSAndrey Gusakov #define DPI_BPP_RGB565			(2 << 0)
507caff0fcSAndrey Gusakov 
517caff0fcSAndrey Gusakov /* Video Path */
527caff0fcSAndrey Gusakov #define VPCTRL0			0x0450
533f072c30SAndrey Smirnov #define VSDELAY			GENMASK(31, 20)
547caff0fcSAndrey Gusakov #define OPXLFMT_RGB666			(0 << 8)
557caff0fcSAndrey Gusakov #define OPXLFMT_RGB888			(1 << 8)
567caff0fcSAndrey Gusakov #define FRMSYNC_DISABLED		(0 << 4) /* Video Timing Gen Disabled */
577caff0fcSAndrey Gusakov #define FRMSYNC_ENABLED			(1 << 4) /* Video Timing Gen Enabled */
587caff0fcSAndrey Gusakov #define MSF_DISABLED			(0 << 0) /* Magic Square FRC disabled */
597caff0fcSAndrey Gusakov #define MSF_ENABLED			(1 << 0) /* Magic Square FRC enabled */
607caff0fcSAndrey Gusakov #define HTIM01			0x0454
613f072c30SAndrey Smirnov #define HPW			GENMASK(8, 0)
623f072c30SAndrey Smirnov #define HBPR			GENMASK(24, 16)
637caff0fcSAndrey Gusakov #define HTIM02			0x0458
643f072c30SAndrey Smirnov #define HDISPR			GENMASK(10, 0)
653f072c30SAndrey Smirnov #define HFPR			GENMASK(24, 16)
667caff0fcSAndrey Gusakov #define VTIM01			0x045c
673f072c30SAndrey Smirnov #define VSPR			GENMASK(7, 0)
683f072c30SAndrey Smirnov #define VBPR			GENMASK(23, 16)
697caff0fcSAndrey Gusakov #define VTIM02			0x0460
703f072c30SAndrey Smirnov #define VFPR			GENMASK(23, 16)
713f072c30SAndrey Smirnov #define VDISPR			GENMASK(10, 0)
727caff0fcSAndrey Gusakov #define VFUEN0			0x0464
737caff0fcSAndrey Gusakov #define VFUEN				BIT(0)   /* Video Frame Timing Upload */
747caff0fcSAndrey Gusakov 
757caff0fcSAndrey Gusakov /* System */
767caff0fcSAndrey Gusakov #define TC_IDREG		0x0500
77f25ee501STomi Valkeinen #define SYSSTAT			0x0508
787caff0fcSAndrey Gusakov #define SYSCTRL			0x0510
797caff0fcSAndrey Gusakov #define DP0_AUDSRC_NO_INPUT		(0 << 3)
807caff0fcSAndrey Gusakov #define DP0_AUDSRC_I2S_RX		(1 << 3)
817caff0fcSAndrey Gusakov #define DP0_VIDSRC_NO_INPUT		(0 << 0)
827caff0fcSAndrey Gusakov #define DP0_VIDSRC_DSI_RX		(1 << 0)
837caff0fcSAndrey Gusakov #define DP0_VIDSRC_DPI_RX		(2 << 0)
847caff0fcSAndrey Gusakov #define DP0_VIDSRC_COLOR_BAR		(3 << 0)
8552c2197aSLucas Stach #define SYSRSTENB		0x050c
8652c2197aSLucas Stach #define ENBI2C				(1 << 0)
8752c2197aSLucas Stach #define ENBLCD0				(1 << 2)
8852c2197aSLucas Stach #define ENBBM				(1 << 3)
8952c2197aSLucas Stach #define ENBDSIRX			(1 << 4)
9052c2197aSLucas Stach #define ENBREG				(1 << 5)
9152c2197aSLucas Stach #define ENBHDCP				(1 << 8)
92af9526f2STomi Valkeinen #define GPIOM			0x0540
93f25ee501STomi Valkeinen #define GPIOC			0x0544
94f25ee501STomi Valkeinen #define GPIOO			0x0548
95af9526f2STomi Valkeinen #define GPIOI			0x054c
96af9526f2STomi Valkeinen #define INTCTL_G		0x0560
97af9526f2STomi Valkeinen #define INTSTS_G		0x0564
98f25ee501STomi Valkeinen 
99f25ee501STomi Valkeinen #define INT_SYSERR		BIT(16)
100f25ee501STomi Valkeinen #define INT_GPIO_H(x)		(1 << (x == 0 ? 2 : 10))
101f25ee501STomi Valkeinen #define INT_GPIO_LC(x)		(1 << (x == 0 ? 3 : 11))
102f25ee501STomi Valkeinen 
103af9526f2STomi Valkeinen #define INT_GP0_LCNT		0x0584
104af9526f2STomi Valkeinen #define INT_GP1_LCNT		0x0588
1057caff0fcSAndrey Gusakov 
1067caff0fcSAndrey Gusakov /* Control */
1077caff0fcSAndrey Gusakov #define DP0CTL			0x0600
1087caff0fcSAndrey Gusakov #define VID_MN_GEN			BIT(6)   /* Auto-generate M/N values */
1097caff0fcSAndrey Gusakov #define EF_EN				BIT(5)   /* Enable Enhanced Framing */
1107caff0fcSAndrey Gusakov #define VID_EN				BIT(1)   /* Video transmission enable */
1117caff0fcSAndrey Gusakov #define DP_EN				BIT(0)   /* Enable DPTX function */
1127caff0fcSAndrey Gusakov 
1137caff0fcSAndrey Gusakov /* Clocks */
1147caff0fcSAndrey Gusakov #define DP0_VIDMNGEN0		0x0610
1157caff0fcSAndrey Gusakov #define DP0_VIDMNGEN1		0x0614
1167caff0fcSAndrey Gusakov #define DP0_VMNGENSTATUS	0x0618
1177caff0fcSAndrey Gusakov 
1187caff0fcSAndrey Gusakov /* Main Channel */
1197caff0fcSAndrey Gusakov #define DP0_SECSAMPLE		0x0640
1207caff0fcSAndrey Gusakov #define DP0_VIDSYNCDELAY	0x0644
1213f072c30SAndrey Smirnov #define VID_SYNC_DLY		GENMASK(15, 0)
1223f072c30SAndrey Smirnov #define THRESH_DLY		GENMASK(31, 16)
1233f072c30SAndrey Smirnov 
1247caff0fcSAndrey Gusakov #define DP0_TOTALVAL		0x0648
1253f072c30SAndrey Smirnov #define H_TOTAL			GENMASK(15, 0)
1263f072c30SAndrey Smirnov #define V_TOTAL			GENMASK(31, 16)
1277caff0fcSAndrey Gusakov #define DP0_STARTVAL		0x064c
1283f072c30SAndrey Smirnov #define H_START			GENMASK(15, 0)
1293f072c30SAndrey Smirnov #define V_START			GENMASK(31, 16)
1307caff0fcSAndrey Gusakov #define DP0_ACTIVEVAL		0x0650
1313f072c30SAndrey Smirnov #define H_ACT			GENMASK(15, 0)
1323f072c30SAndrey Smirnov #define V_ACT			GENMASK(31, 16)
1333f072c30SAndrey Smirnov 
1347caff0fcSAndrey Gusakov #define DP0_SYNCVAL		0x0654
1353f072c30SAndrey Smirnov #define VS_WIDTH		GENMASK(30, 16)
1363f072c30SAndrey Smirnov #define HS_WIDTH		GENMASK(14, 0)
1377923e09cSTomi Valkeinen #define SYNCVAL_HS_POL_ACTIVE_LOW	(1 << 15)
1387923e09cSTomi Valkeinen #define SYNCVAL_VS_POL_ACTIVE_LOW	(1 << 31)
1397caff0fcSAndrey Gusakov #define DP0_MISC		0x0658
140f3b8adbeSAndrey Gusakov #define TU_SIZE_RECOMMENDED		(63) /* LSCLK cycles per TU */
1413f072c30SAndrey Smirnov #define MAX_TU_SYMBOL		GENMASK(28, 23)
1423f072c30SAndrey Smirnov #define TU_SIZE			GENMASK(21, 16)
1437caff0fcSAndrey Gusakov #define BPC_6				(0 << 5)
1447caff0fcSAndrey Gusakov #define BPC_8				(1 << 5)
1457caff0fcSAndrey Gusakov 
1467caff0fcSAndrey Gusakov /* AUX channel */
1477caff0fcSAndrey Gusakov #define DP0_AUXCFG0		0x0660
148fdb29b73SAndrey Smirnov #define DP0_AUXCFG0_BSIZE	GENMASK(11, 8)
149fdb29b73SAndrey Smirnov #define DP0_AUXCFG0_ADDR_ONLY	BIT(4)
1507caff0fcSAndrey Gusakov #define DP0_AUXCFG1		0x0664
1517caff0fcSAndrey Gusakov #define AUX_RX_FILTER_EN		BIT(16)
1527caff0fcSAndrey Gusakov 
1537caff0fcSAndrey Gusakov #define DP0_AUXADDR		0x0668
1547caff0fcSAndrey Gusakov #define DP0_AUXWDATA(i)		(0x066c + (i) * 4)
1557caff0fcSAndrey Gusakov #define DP0_AUXRDATA(i)		(0x067c + (i) * 4)
1567caff0fcSAndrey Gusakov #define DP0_AUXSTATUS		0x068c
15712dfe7c4SAndrey Smirnov #define AUX_BYTES		GENMASK(15, 8)
15812dfe7c4SAndrey Smirnov #define AUX_STATUS		GENMASK(7, 4)
1597caff0fcSAndrey Gusakov #define AUX_TIMEOUT		BIT(1)
1607caff0fcSAndrey Gusakov #define AUX_BUSY		BIT(0)
1617caff0fcSAndrey Gusakov #define DP0_AUXI2CADR		0x0698
1627caff0fcSAndrey Gusakov 
1637caff0fcSAndrey Gusakov /* Link Training */
1647caff0fcSAndrey Gusakov #define DP0_SRCCTRL		0x06a0
1657caff0fcSAndrey Gusakov #define DP0_SRCCTRL_SCRMBLDIS		BIT(13)
1667caff0fcSAndrey Gusakov #define DP0_SRCCTRL_EN810B		BIT(12)
1677caff0fcSAndrey Gusakov #define DP0_SRCCTRL_NOTP		(0 << 8)
1687caff0fcSAndrey Gusakov #define DP0_SRCCTRL_TP1			(1 << 8)
1697caff0fcSAndrey Gusakov #define DP0_SRCCTRL_TP2			(2 << 8)
1707caff0fcSAndrey Gusakov #define DP0_SRCCTRL_LANESKEW		BIT(7)
1717caff0fcSAndrey Gusakov #define DP0_SRCCTRL_SSCG		BIT(3)
1727caff0fcSAndrey Gusakov #define DP0_SRCCTRL_LANES_1		(0 << 2)
1737caff0fcSAndrey Gusakov #define DP0_SRCCTRL_LANES_2		(1 << 2)
1747caff0fcSAndrey Gusakov #define DP0_SRCCTRL_BW27		(1 << 1)
1757caff0fcSAndrey Gusakov #define DP0_SRCCTRL_BW162		(0 << 1)
1767caff0fcSAndrey Gusakov #define DP0_SRCCTRL_AUTOCORRECT		BIT(0)
1777caff0fcSAndrey Gusakov #define DP0_LTSTAT		0x06d0
1787caff0fcSAndrey Gusakov #define LT_LOOPDONE			BIT(13)
1797caff0fcSAndrey Gusakov #define LT_STATUS_MASK			(0x1f << 8)
1807caff0fcSAndrey Gusakov #define LT_CHANNEL1_EQ_BITS		(DP_CHANNEL_EQ_BITS << 4)
1817caff0fcSAndrey Gusakov #define LT_INTERLANE_ALIGN_DONE		BIT(3)
1827caff0fcSAndrey Gusakov #define LT_CHANNEL0_EQ_BITS		(DP_CHANNEL_EQ_BITS)
1837caff0fcSAndrey Gusakov #define DP0_SNKLTCHGREQ		0x06d4
1847caff0fcSAndrey Gusakov #define DP0_LTLOOPCTRL		0x06d8
1857caff0fcSAndrey Gusakov #define DP0_SNKLTCTRL		0x06e4
1867caff0fcSAndrey Gusakov 
187adf41098STomi Valkeinen #define DP1_SRCCTRL		0x07a0
188adf41098STomi Valkeinen 
1897caff0fcSAndrey Gusakov /* PHY */
1907caff0fcSAndrey Gusakov #define DP_PHY_CTRL		0x0800
1917caff0fcSAndrey Gusakov #define DP_PHY_RST			BIT(28)  /* DP PHY Global Soft Reset */
1927caff0fcSAndrey Gusakov #define BGREN				BIT(25)  /* AUX PHY BGR Enable */
1937caff0fcSAndrey Gusakov #define PWR_SW_EN			BIT(24)  /* PHY Power Switch Enable */
1947caff0fcSAndrey Gusakov #define PHY_M1_RST			BIT(12)  /* Reset PHY1 Main Channel */
1957caff0fcSAndrey Gusakov #define PHY_RDY				BIT(16)  /* PHY Main Channels Ready */
1967caff0fcSAndrey Gusakov #define PHY_M0_RST			BIT(8)   /* Reset PHY0 Main Channel */
197adf41098STomi Valkeinen #define PHY_2LANE			BIT(2)   /* PHY Enable 2 lanes */
1987caff0fcSAndrey Gusakov #define PHY_A0_EN			BIT(1)   /* PHY Aux Channel0 Enable */
1997caff0fcSAndrey Gusakov #define PHY_M0_EN			BIT(0)   /* PHY Main Channel0 Enable */
2007caff0fcSAndrey Gusakov 
2017caff0fcSAndrey Gusakov /* PLL */
2027caff0fcSAndrey Gusakov #define DP0_PLLCTRL		0x0900
2037caff0fcSAndrey Gusakov #define DP1_PLLCTRL		0x0904	/* not defined in DS */
2047caff0fcSAndrey Gusakov #define PXL_PLLCTRL		0x0908
2057caff0fcSAndrey Gusakov #define PLLUPDATE			BIT(2)
2067caff0fcSAndrey Gusakov #define PLLBYP				BIT(1)
2077caff0fcSAndrey Gusakov #define PLLEN				BIT(0)
2087caff0fcSAndrey Gusakov #define PXL_PLLPARAM		0x0914
2097caff0fcSAndrey Gusakov #define IN_SEL_REFCLK			(0 << 14)
2107caff0fcSAndrey Gusakov #define SYS_PLLPARAM		0x0918
2117caff0fcSAndrey Gusakov #define REF_FREQ_38M4			(0 << 8) /* 38.4 MHz */
2127caff0fcSAndrey Gusakov #define REF_FREQ_19M2			(1 << 8) /* 19.2 MHz */
2137caff0fcSAndrey Gusakov #define REF_FREQ_26M			(2 << 8) /* 26 MHz */
2147caff0fcSAndrey Gusakov #define REF_FREQ_13M			(3 << 8) /* 13 MHz */
2157caff0fcSAndrey Gusakov #define SYSCLK_SEL_LSCLK		(0 << 4)
2167caff0fcSAndrey Gusakov #define LSCLK_DIV_1			(0 << 0)
2177caff0fcSAndrey Gusakov #define LSCLK_DIV_2			(1 << 0)
2187caff0fcSAndrey Gusakov 
2197caff0fcSAndrey Gusakov /* Test & Debug */
2207caff0fcSAndrey Gusakov #define TSTCTL			0x0a00
2213f072c30SAndrey Smirnov #define COLOR_R			GENMASK(31, 24)
2223f072c30SAndrey Smirnov #define COLOR_G			GENMASK(23, 16)
2233f072c30SAndrey Smirnov #define COLOR_B			GENMASK(15, 8)
2243f072c30SAndrey Smirnov #define ENI2CFILTER		BIT(4)
2253f072c30SAndrey Smirnov #define COLOR_BAR_MODE		GENMASK(1, 0)
2263f072c30SAndrey Smirnov #define COLOR_BAR_MODE_BARS	2
2277caff0fcSAndrey Gusakov #define PLL_DBG			0x0a04
2287caff0fcSAndrey Gusakov 
2297caff0fcSAndrey Gusakov static bool tc_test_pattern;
2307caff0fcSAndrey Gusakov module_param_named(test, tc_test_pattern, bool, 0644);
2317caff0fcSAndrey Gusakov 
2327caff0fcSAndrey Gusakov struct tc_edp_link {
233e7dc8d40SThierry Reding 	u8			dpcd[DP_RECEIVER_CAP_SIZE];
234e7dc8d40SThierry Reding 	unsigned int		rate;
235e7dc8d40SThierry Reding 	u8			num_lanes;
2367caff0fcSAndrey Gusakov 	u8			assr;
237e5607637STomi Valkeinen 	bool			scrambler_dis;
238e5607637STomi Valkeinen 	bool			spread;
2397caff0fcSAndrey Gusakov };
2407caff0fcSAndrey Gusakov 
2417caff0fcSAndrey Gusakov struct tc_data {
2427caff0fcSAndrey Gusakov 	struct device		*dev;
2437caff0fcSAndrey Gusakov 	struct regmap		*regmap;
2447caff0fcSAndrey Gusakov 	struct drm_dp_aux	aux;
2457caff0fcSAndrey Gusakov 
2467caff0fcSAndrey Gusakov 	struct drm_bridge	bridge;
247de5e6c02SSam Ravnborg 	struct drm_bridge	*panel_bridge;
2487caff0fcSAndrey Gusakov 	struct drm_connector	connector;
2497caff0fcSAndrey Gusakov 
2507caff0fcSAndrey Gusakov 	/* link settings */
2517caff0fcSAndrey Gusakov 	struct tc_edp_link	link;
2527caff0fcSAndrey Gusakov 
2537caff0fcSAndrey Gusakov 	/* current mode */
25446648a3cSTomi Valkeinen 	struct drm_display_mode	mode;
2557caff0fcSAndrey Gusakov 
2567caff0fcSAndrey Gusakov 	u32			rev;
2577caff0fcSAndrey Gusakov 	u8			assr;
2587caff0fcSAndrey Gusakov 
2597caff0fcSAndrey Gusakov 	struct gpio_desc	*sd_gpio;
2607caff0fcSAndrey Gusakov 	struct gpio_desc	*reset_gpio;
2617caff0fcSAndrey Gusakov 	struct clk		*refclk;
262f25ee501STomi Valkeinen 
263f25ee501STomi Valkeinen 	/* do we have IRQ */
264f25ee501STomi Valkeinen 	bool			have_irq;
265f25ee501STomi Valkeinen 
266f25ee501STomi Valkeinen 	/* HPD pin number (0 or 1) or -ENODEV */
267f25ee501STomi Valkeinen 	int			hpd_pin;
2687caff0fcSAndrey Gusakov };
2697caff0fcSAndrey Gusakov 
2707caff0fcSAndrey Gusakov static inline struct tc_data *aux_to_tc(struct drm_dp_aux *a)
2717caff0fcSAndrey Gusakov {
2727caff0fcSAndrey Gusakov 	return container_of(a, struct tc_data, aux);
2737caff0fcSAndrey Gusakov }
2747caff0fcSAndrey Gusakov 
2757caff0fcSAndrey Gusakov static inline struct tc_data *bridge_to_tc(struct drm_bridge *b)
2767caff0fcSAndrey Gusakov {
2777caff0fcSAndrey Gusakov 	return container_of(b, struct tc_data, bridge);
2787caff0fcSAndrey Gusakov }
2797caff0fcSAndrey Gusakov 
2807caff0fcSAndrey Gusakov static inline struct tc_data *connector_to_tc(struct drm_connector *c)
2817caff0fcSAndrey Gusakov {
2827caff0fcSAndrey Gusakov 	return container_of(c, struct tc_data, connector);
2837caff0fcSAndrey Gusakov }
2847caff0fcSAndrey Gusakov 
28593a10569SAndrey Smirnov static inline int tc_poll_timeout(struct tc_data *tc, unsigned int addr,
2867caff0fcSAndrey Gusakov 				  unsigned int cond_mask,
2877caff0fcSAndrey Gusakov 				  unsigned int cond_value,
2887caff0fcSAndrey Gusakov 				  unsigned long sleep_us, u64 timeout_us)
2897caff0fcSAndrey Gusakov {
2907caff0fcSAndrey Gusakov 	unsigned int val;
2917caff0fcSAndrey Gusakov 
29293a10569SAndrey Smirnov 	return regmap_read_poll_timeout(tc->regmap, addr, val,
29393a10569SAndrey Smirnov 					(val & cond_mask) == cond_value,
29493a10569SAndrey Smirnov 					sleep_us, timeout_us);
2957caff0fcSAndrey Gusakov }
2967caff0fcSAndrey Gusakov 
29772648926SAndrey Smirnov static int tc_aux_wait_busy(struct tc_data *tc)
2987caff0fcSAndrey Gusakov {
2998a6483acSTomi Valkeinen 	return tc_poll_timeout(tc, DP0_AUXSTATUS, AUX_BUSY, 0, 100, 100000);
3007caff0fcSAndrey Gusakov }
3017caff0fcSAndrey Gusakov 
302792a081aSAndrey Smirnov static int tc_aux_write_data(struct tc_data *tc, const void *data,
303792a081aSAndrey Smirnov 			     size_t size)
304792a081aSAndrey Smirnov {
305792a081aSAndrey Smirnov 	u32 auxwdata[DP_AUX_MAX_PAYLOAD_BYTES / sizeof(u32)] = { 0 };
306792a081aSAndrey Smirnov 	int ret, count = ALIGN(size, sizeof(u32));
307792a081aSAndrey Smirnov 
308792a081aSAndrey Smirnov 	memcpy(auxwdata, data, size);
309792a081aSAndrey Smirnov 
310792a081aSAndrey Smirnov 	ret = regmap_raw_write(tc->regmap, DP0_AUXWDATA(0), auxwdata, count);
311792a081aSAndrey Smirnov 	if (ret)
312792a081aSAndrey Smirnov 		return ret;
313792a081aSAndrey Smirnov 
314792a081aSAndrey Smirnov 	return size;
315792a081aSAndrey Smirnov }
316792a081aSAndrey Smirnov 
31753b166dcSAndrey Smirnov static int tc_aux_read_data(struct tc_data *tc, void *data, size_t size)
31853b166dcSAndrey Smirnov {
31953b166dcSAndrey Smirnov 	u32 auxrdata[DP_AUX_MAX_PAYLOAD_BYTES / sizeof(u32)];
32053b166dcSAndrey Smirnov 	int ret, count = ALIGN(size, sizeof(u32));
32153b166dcSAndrey Smirnov 
32253b166dcSAndrey Smirnov 	ret = regmap_raw_read(tc->regmap, DP0_AUXRDATA(0), auxrdata, count);
32353b166dcSAndrey Smirnov 	if (ret)
32453b166dcSAndrey Smirnov 		return ret;
32553b166dcSAndrey Smirnov 
32653b166dcSAndrey Smirnov 	memcpy(data, auxrdata, size);
32753b166dcSAndrey Smirnov 
32853b166dcSAndrey Smirnov 	return size;
32953b166dcSAndrey Smirnov }
33053b166dcSAndrey Smirnov 
331fdb29b73SAndrey Smirnov static u32 tc_auxcfg0(struct drm_dp_aux_msg *msg, size_t size)
332fdb29b73SAndrey Smirnov {
333fdb29b73SAndrey Smirnov 	u32 auxcfg0 = msg->request;
334fdb29b73SAndrey Smirnov 
335fdb29b73SAndrey Smirnov 	if (size)
336fdb29b73SAndrey Smirnov 		auxcfg0 |= FIELD_PREP(DP0_AUXCFG0_BSIZE, size - 1);
337fdb29b73SAndrey Smirnov 	else
338fdb29b73SAndrey Smirnov 		auxcfg0 |= DP0_AUXCFG0_ADDR_ONLY;
339fdb29b73SAndrey Smirnov 
340fdb29b73SAndrey Smirnov 	return auxcfg0;
341fdb29b73SAndrey Smirnov }
342fdb29b73SAndrey Smirnov 
3437caff0fcSAndrey Gusakov static ssize_t tc_aux_transfer(struct drm_dp_aux *aux,
3447caff0fcSAndrey Gusakov 			       struct drm_dp_aux_msg *msg)
3457caff0fcSAndrey Gusakov {
3467caff0fcSAndrey Gusakov 	struct tc_data *tc = aux_to_tc(aux);
347e0655feaSAndrey Smirnov 	size_t size = min_t(size_t, DP_AUX_MAX_PAYLOAD_BYTES - 1, msg->size);
3487caff0fcSAndrey Gusakov 	u8 request = msg->request & ~DP_AUX_I2C_MOT;
34912dfe7c4SAndrey Smirnov 	u32 auxstatus;
3507caff0fcSAndrey Gusakov 	int ret;
3517caff0fcSAndrey Gusakov 
35272648926SAndrey Smirnov 	ret = tc_aux_wait_busy(tc);
3537caff0fcSAndrey Gusakov 	if (ret)
3546d0c3831SAndrey Smirnov 		return ret;
3557caff0fcSAndrey Gusakov 
356792a081aSAndrey Smirnov 	switch (request) {
357792a081aSAndrey Smirnov 	case DP_AUX_NATIVE_READ:
358792a081aSAndrey Smirnov 	case DP_AUX_I2C_READ:
359792a081aSAndrey Smirnov 		break;
360792a081aSAndrey Smirnov 	case DP_AUX_NATIVE_WRITE:
361792a081aSAndrey Smirnov 	case DP_AUX_I2C_WRITE:
362fdb29b73SAndrey Smirnov 		if (size) {
363792a081aSAndrey Smirnov 			ret = tc_aux_write_data(tc, msg->buffer, size);
364792a081aSAndrey Smirnov 			if (ret < 0)
3656d0c3831SAndrey Smirnov 				return ret;
366fdb29b73SAndrey Smirnov 		}
367792a081aSAndrey Smirnov 		break;
368792a081aSAndrey Smirnov 	default:
3697caff0fcSAndrey Gusakov 		return -EINVAL;
3707caff0fcSAndrey Gusakov 	}
3717caff0fcSAndrey Gusakov 
3727caff0fcSAndrey Gusakov 	/* Store address */
3736d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_AUXADDR, msg->address);
3746d0c3831SAndrey Smirnov 	if (ret)
3756d0c3831SAndrey Smirnov 		return ret;
3767caff0fcSAndrey Gusakov 	/* Start transfer */
377fdb29b73SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_AUXCFG0, tc_auxcfg0(msg, size));
3786d0c3831SAndrey Smirnov 	if (ret)
3796d0c3831SAndrey Smirnov 		return ret;
3807caff0fcSAndrey Gusakov 
38172648926SAndrey Smirnov 	ret = tc_aux_wait_busy(tc);
3827caff0fcSAndrey Gusakov 	if (ret)
3836d0c3831SAndrey Smirnov 		return ret;
3847caff0fcSAndrey Gusakov 
38512dfe7c4SAndrey Smirnov 	ret = regmap_read(tc->regmap, DP0_AUXSTATUS, &auxstatus);
3867caff0fcSAndrey Gusakov 	if (ret)
3876d0c3831SAndrey Smirnov 		return ret;
3887caff0fcSAndrey Gusakov 
38912dfe7c4SAndrey Smirnov 	if (auxstatus & AUX_TIMEOUT)
39012dfe7c4SAndrey Smirnov 		return -ETIMEDOUT;
391fdb29b73SAndrey Smirnov 	/*
392fdb29b73SAndrey Smirnov 	 * For some reason address-only DP_AUX_I2C_WRITE (MOT), still
393fdb29b73SAndrey Smirnov 	 * reports 1 byte transferred in its status. To deal we that
394fdb29b73SAndrey Smirnov 	 * we ignore aux_bytes field if we know that this was an
395fdb29b73SAndrey Smirnov 	 * address-only transfer
396fdb29b73SAndrey Smirnov 	 */
397fdb29b73SAndrey Smirnov 	if (size)
39812dfe7c4SAndrey Smirnov 		size = FIELD_GET(AUX_BYTES, auxstatus);
39912dfe7c4SAndrey Smirnov 	msg->reply = FIELD_GET(AUX_STATUS, auxstatus);
40012dfe7c4SAndrey Smirnov 
40153b166dcSAndrey Smirnov 	switch (request) {
40253b166dcSAndrey Smirnov 	case DP_AUX_NATIVE_READ:
40353b166dcSAndrey Smirnov 	case DP_AUX_I2C_READ:
404fdb29b73SAndrey Smirnov 		if (size)
40553b166dcSAndrey Smirnov 			return tc_aux_read_data(tc, msg->buffer, size);
406fdb29b73SAndrey Smirnov 		break;
4077caff0fcSAndrey Gusakov 	}
4087caff0fcSAndrey Gusakov 
4097caff0fcSAndrey Gusakov 	return size;
4107caff0fcSAndrey Gusakov }
4117caff0fcSAndrey Gusakov 
4127caff0fcSAndrey Gusakov static const char * const training_pattern1_errors[] = {
4137caff0fcSAndrey Gusakov 	"No errors",
4147caff0fcSAndrey Gusakov 	"Aux write error",
4157caff0fcSAndrey Gusakov 	"Aux read error",
4167caff0fcSAndrey Gusakov 	"Max voltage reached error",
4177caff0fcSAndrey Gusakov 	"Loop counter expired error",
4187caff0fcSAndrey Gusakov 	"res", "res", "res"
4197caff0fcSAndrey Gusakov };
4207caff0fcSAndrey Gusakov 
4217caff0fcSAndrey Gusakov static const char * const training_pattern2_errors[] = {
4227caff0fcSAndrey Gusakov 	"No errors",
4237caff0fcSAndrey Gusakov 	"Aux write error",
4247caff0fcSAndrey Gusakov 	"Aux read error",
4257caff0fcSAndrey Gusakov 	"Clock recovery failed error",
4267caff0fcSAndrey Gusakov 	"Loop counter expired error",
4277caff0fcSAndrey Gusakov 	"res", "res", "res"
4287caff0fcSAndrey Gusakov };
4297caff0fcSAndrey Gusakov 
4307caff0fcSAndrey Gusakov static u32 tc_srcctrl(struct tc_data *tc)
4317caff0fcSAndrey Gusakov {
4327caff0fcSAndrey Gusakov 	/*
4337caff0fcSAndrey Gusakov 	 * No training pattern, skew lane 1 data by two LSCLK cycles with
4347caff0fcSAndrey Gusakov 	 * respect to lane 0 data, AutoCorrect Mode = 0
4357caff0fcSAndrey Gusakov 	 */
4364b30bf41STomi Valkeinen 	u32 reg = DP0_SRCCTRL_NOTP | DP0_SRCCTRL_LANESKEW | DP0_SRCCTRL_EN810B;
4377caff0fcSAndrey Gusakov 
4387caff0fcSAndrey Gusakov 	if (tc->link.scrambler_dis)
4397caff0fcSAndrey Gusakov 		reg |= DP0_SRCCTRL_SCRMBLDIS;	/* Scrambler Disabled */
4407caff0fcSAndrey Gusakov 	if (tc->link.spread)
4417caff0fcSAndrey Gusakov 		reg |= DP0_SRCCTRL_SSCG;	/* Spread Spectrum Enable */
442e7dc8d40SThierry Reding 	if (tc->link.num_lanes == 2)
4437caff0fcSAndrey Gusakov 		reg |= DP0_SRCCTRL_LANES_2;	/* Two Main Channel Lanes */
444e7dc8d40SThierry Reding 	if (tc->link.rate != 162000)
4457caff0fcSAndrey Gusakov 		reg |= DP0_SRCCTRL_BW27;	/* 2.7 Gbps link */
4467caff0fcSAndrey Gusakov 	return reg;
4477caff0fcSAndrey Gusakov }
4487caff0fcSAndrey Gusakov 
449134fb306SAndrey Smirnov static int tc_pllupdate(struct tc_data *tc, unsigned int pllctrl)
4507caff0fcSAndrey Gusakov {
451134fb306SAndrey Smirnov 	int ret;
452134fb306SAndrey Smirnov 
453134fb306SAndrey Smirnov 	ret = regmap_write(tc->regmap, pllctrl, PLLUPDATE | PLLEN);
454134fb306SAndrey Smirnov 	if (ret)
455134fb306SAndrey Smirnov 		return ret;
456134fb306SAndrey Smirnov 
4577caff0fcSAndrey Gusakov 	/* Wait for PLL to lock: up to 2.09 ms, depending on refclk */
4587caff0fcSAndrey Gusakov 	usleep_range(3000, 6000);
459134fb306SAndrey Smirnov 
460134fb306SAndrey Smirnov 	return 0;
4617caff0fcSAndrey Gusakov }
4627caff0fcSAndrey Gusakov 
4637caff0fcSAndrey Gusakov static int tc_pxl_pll_en(struct tc_data *tc, u32 refclk, u32 pixelclock)
4647caff0fcSAndrey Gusakov {
4657caff0fcSAndrey Gusakov 	int ret;
4667caff0fcSAndrey Gusakov 	int i_pre, best_pre = 1;
4677caff0fcSAndrey Gusakov 	int i_post, best_post = 1;
4687caff0fcSAndrey Gusakov 	int div, best_div = 1;
4697caff0fcSAndrey Gusakov 	int mul, best_mul = 1;
4707caff0fcSAndrey Gusakov 	int delta, best_delta;
4717caff0fcSAndrey Gusakov 	int ext_div[] = {1, 2, 3, 5, 7};
4727caff0fcSAndrey Gusakov 	int best_pixelclock = 0;
4737caff0fcSAndrey Gusakov 	int vco_hi = 0;
4746d0c3831SAndrey Smirnov 	u32 pxl_pllparam;
4757caff0fcSAndrey Gusakov 
4767caff0fcSAndrey Gusakov 	dev_dbg(tc->dev, "PLL: requested %d pixelclock, ref %d\n", pixelclock,
4777caff0fcSAndrey Gusakov 		refclk);
4787caff0fcSAndrey Gusakov 	best_delta = pixelclock;
4797caff0fcSAndrey Gusakov 	/* Loop over all possible ext_divs, skipping invalid configurations */
4807caff0fcSAndrey Gusakov 	for (i_pre = 0; i_pre < ARRAY_SIZE(ext_div); i_pre++) {
4817caff0fcSAndrey Gusakov 		/*
4827caff0fcSAndrey Gusakov 		 * refclk / ext_pre_div should be in the 1 to 200 MHz range.
4837caff0fcSAndrey Gusakov 		 * We don't allow any refclk > 200 MHz, only check lower bounds.
4847caff0fcSAndrey Gusakov 		 */
4857caff0fcSAndrey Gusakov 		if (refclk / ext_div[i_pre] < 1000000)
4867caff0fcSAndrey Gusakov 			continue;
4877caff0fcSAndrey Gusakov 		for (i_post = 0; i_post < ARRAY_SIZE(ext_div); i_post++) {
4887caff0fcSAndrey Gusakov 			for (div = 1; div <= 16; div++) {
4897caff0fcSAndrey Gusakov 				u32 clk;
4907caff0fcSAndrey Gusakov 				u64 tmp;
4917caff0fcSAndrey Gusakov 
4927caff0fcSAndrey Gusakov 				tmp = pixelclock * ext_div[i_pre] *
4937caff0fcSAndrey Gusakov 				      ext_div[i_post] * div;
4947caff0fcSAndrey Gusakov 				do_div(tmp, refclk);
4957caff0fcSAndrey Gusakov 				mul = tmp;
4967caff0fcSAndrey Gusakov 
4977caff0fcSAndrey Gusakov 				/* Check limits */
4987caff0fcSAndrey Gusakov 				if ((mul < 1) || (mul > 128))
4997caff0fcSAndrey Gusakov 					continue;
5007caff0fcSAndrey Gusakov 
5017caff0fcSAndrey Gusakov 				clk = (refclk / ext_div[i_pre] / div) * mul;
5027caff0fcSAndrey Gusakov 				/*
5037caff0fcSAndrey Gusakov 				 * refclk * mul / (ext_pre_div * pre_div)
5047caff0fcSAndrey Gusakov 				 * should be in the 150 to 650 MHz range
5057caff0fcSAndrey Gusakov 				 */
5067caff0fcSAndrey Gusakov 				if ((clk > 650000000) || (clk < 150000000))
5077caff0fcSAndrey Gusakov 					continue;
5087caff0fcSAndrey Gusakov 
5097caff0fcSAndrey Gusakov 				clk = clk / ext_div[i_post];
5107caff0fcSAndrey Gusakov 				delta = clk - pixelclock;
5117caff0fcSAndrey Gusakov 
5127caff0fcSAndrey Gusakov 				if (abs(delta) < abs(best_delta)) {
5137caff0fcSAndrey Gusakov 					best_pre = i_pre;
5147caff0fcSAndrey Gusakov 					best_post = i_post;
5157caff0fcSAndrey Gusakov 					best_div = div;
5167caff0fcSAndrey Gusakov 					best_mul = mul;
5177caff0fcSAndrey Gusakov 					best_delta = delta;
5187caff0fcSAndrey Gusakov 					best_pixelclock = clk;
5197caff0fcSAndrey Gusakov 				}
5207caff0fcSAndrey Gusakov 			}
5217caff0fcSAndrey Gusakov 		}
5227caff0fcSAndrey Gusakov 	}
5237caff0fcSAndrey Gusakov 	if (best_pixelclock == 0) {
5247caff0fcSAndrey Gusakov 		dev_err(tc->dev, "Failed to calc clock for %d pixelclock\n",
5257caff0fcSAndrey Gusakov 			pixelclock);
5267caff0fcSAndrey Gusakov 		return -EINVAL;
5277caff0fcSAndrey Gusakov 	}
5287caff0fcSAndrey Gusakov 
5297caff0fcSAndrey Gusakov 	dev_dbg(tc->dev, "PLL: got %d, delta %d\n", best_pixelclock,
5307caff0fcSAndrey Gusakov 		best_delta);
5317caff0fcSAndrey Gusakov 	dev_dbg(tc->dev, "PLL: %d / %d / %d * %d / %d\n", refclk,
5327caff0fcSAndrey Gusakov 		ext_div[best_pre], best_div, best_mul, ext_div[best_post]);
5337caff0fcSAndrey Gusakov 
5347caff0fcSAndrey Gusakov 	/* if VCO >= 300 MHz */
5357caff0fcSAndrey Gusakov 	if (refclk / ext_div[best_pre] / best_div * best_mul >= 300000000)
5367caff0fcSAndrey Gusakov 		vco_hi = 1;
5377caff0fcSAndrey Gusakov 	/* see DS */
5387caff0fcSAndrey Gusakov 	if (best_div == 16)
5397caff0fcSAndrey Gusakov 		best_div = 0;
5407caff0fcSAndrey Gusakov 	if (best_mul == 128)
5417caff0fcSAndrey Gusakov 		best_mul = 0;
5427caff0fcSAndrey Gusakov 
5437caff0fcSAndrey Gusakov 	/* Power up PLL and switch to bypass */
5446d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, PXL_PLLCTRL, PLLBYP | PLLEN);
5456d0c3831SAndrey Smirnov 	if (ret)
5466d0c3831SAndrey Smirnov 		return ret;
5477caff0fcSAndrey Gusakov 
5486d0c3831SAndrey Smirnov 	pxl_pllparam  = vco_hi << 24; /* For PLL VCO >= 300 MHz = 1 */
5496d0c3831SAndrey Smirnov 	pxl_pllparam |= ext_div[best_pre] << 20; /* External Pre-divider */
5506d0c3831SAndrey Smirnov 	pxl_pllparam |= ext_div[best_post] << 16; /* External Post-divider */
5516d0c3831SAndrey Smirnov 	pxl_pllparam |= IN_SEL_REFCLK; /* Use RefClk as PLL input */
5526d0c3831SAndrey Smirnov 	pxl_pllparam |= best_div << 8; /* Divider for PLL RefClk */
5536d0c3831SAndrey Smirnov 	pxl_pllparam |= best_mul; /* Multiplier for PLL */
5546d0c3831SAndrey Smirnov 
5556d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, PXL_PLLPARAM, pxl_pllparam);
5566d0c3831SAndrey Smirnov 	if (ret)
5576d0c3831SAndrey Smirnov 		return ret;
5587caff0fcSAndrey Gusakov 
5597caff0fcSAndrey Gusakov 	/* Force PLL parameter update and disable bypass */
560134fb306SAndrey Smirnov 	return tc_pllupdate(tc, PXL_PLLCTRL);
5617caff0fcSAndrey Gusakov }
5627caff0fcSAndrey Gusakov 
5637caff0fcSAndrey Gusakov static int tc_pxl_pll_dis(struct tc_data *tc)
5647caff0fcSAndrey Gusakov {
5657caff0fcSAndrey Gusakov 	/* Enable PLL bypass, power down PLL */
5667caff0fcSAndrey Gusakov 	return regmap_write(tc->regmap, PXL_PLLCTRL, PLLBYP);
5677caff0fcSAndrey Gusakov }
5687caff0fcSAndrey Gusakov 
5697caff0fcSAndrey Gusakov static int tc_stream_clock_calc(struct tc_data *tc)
5707caff0fcSAndrey Gusakov {
5717caff0fcSAndrey Gusakov 	/*
5727caff0fcSAndrey Gusakov 	 * If the Stream clock and Link Symbol clock are
5737caff0fcSAndrey Gusakov 	 * asynchronous with each other, the value of M changes over
5747caff0fcSAndrey Gusakov 	 * time. This way of generating link clock and stream
5757caff0fcSAndrey Gusakov 	 * clock is called Asynchronous Clock mode. The value M
5767caff0fcSAndrey Gusakov 	 * must change while the value N stays constant. The
5777caff0fcSAndrey Gusakov 	 * value of N in this Asynchronous Clock mode must be set
5787caff0fcSAndrey Gusakov 	 * to 2^15 or 32,768.
5797caff0fcSAndrey Gusakov 	 *
5807caff0fcSAndrey Gusakov 	 * LSCLK = 1/10 of high speed link clock
5817caff0fcSAndrey Gusakov 	 *
5827caff0fcSAndrey Gusakov 	 * f_STRMCLK = M/N * f_LSCLK
5837caff0fcSAndrey Gusakov 	 * M/N = f_STRMCLK / f_LSCLK
5847caff0fcSAndrey Gusakov 	 *
5857caff0fcSAndrey Gusakov 	 */
5866d0c3831SAndrey Smirnov 	return regmap_write(tc->regmap, DP0_VIDMNGEN1, 32768);
5877caff0fcSAndrey Gusakov }
5887caff0fcSAndrey Gusakov 
589c49f60dfSAndrey Smirnov static int tc_set_syspllparam(struct tc_data *tc)
5907caff0fcSAndrey Gusakov {
5917caff0fcSAndrey Gusakov 	unsigned long rate;
592c49f60dfSAndrey Smirnov 	u32 pllparam = SYSCLK_SEL_LSCLK | LSCLK_DIV_2;
5937caff0fcSAndrey Gusakov 
5947caff0fcSAndrey Gusakov 	rate = clk_get_rate(tc->refclk);
5957caff0fcSAndrey Gusakov 	switch (rate) {
5967caff0fcSAndrey Gusakov 	case 38400000:
597c49f60dfSAndrey Smirnov 		pllparam |= REF_FREQ_38M4;
5987caff0fcSAndrey Gusakov 		break;
5997caff0fcSAndrey Gusakov 	case 26000000:
600c49f60dfSAndrey Smirnov 		pllparam |= REF_FREQ_26M;
6017caff0fcSAndrey Gusakov 		break;
6027caff0fcSAndrey Gusakov 	case 19200000:
603c49f60dfSAndrey Smirnov 		pllparam |= REF_FREQ_19M2;
6047caff0fcSAndrey Gusakov 		break;
6057caff0fcSAndrey Gusakov 	case 13000000:
606c49f60dfSAndrey Smirnov 		pllparam |= REF_FREQ_13M;
6077caff0fcSAndrey Gusakov 		break;
6087caff0fcSAndrey Gusakov 	default:
6097caff0fcSAndrey Gusakov 		dev_err(tc->dev, "Invalid refclk rate: %lu Hz\n", rate);
6107caff0fcSAndrey Gusakov 		return -EINVAL;
6117caff0fcSAndrey Gusakov 	}
6127caff0fcSAndrey Gusakov 
613c49f60dfSAndrey Smirnov 	return regmap_write(tc->regmap, SYS_PLLPARAM, pllparam);
614c49f60dfSAndrey Smirnov }
615c49f60dfSAndrey Smirnov 
616c49f60dfSAndrey Smirnov static int tc_aux_link_setup(struct tc_data *tc)
617c49f60dfSAndrey Smirnov {
618c49f60dfSAndrey Smirnov 	int ret;
619c49f60dfSAndrey Smirnov 	u32 dp0_auxcfg1;
620c49f60dfSAndrey Smirnov 
6217caff0fcSAndrey Gusakov 	/* Setup DP-PHY / PLL */
622c49f60dfSAndrey Smirnov 	ret = tc_set_syspllparam(tc);
6236d0c3831SAndrey Smirnov 	if (ret)
6246d0c3831SAndrey Smirnov 		goto err;
6257caff0fcSAndrey Gusakov 
6266d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP_PHY_CTRL,
6276d0c3831SAndrey Smirnov 			   BGREN | PWR_SW_EN | PHY_A0_EN);
6286d0c3831SAndrey Smirnov 	if (ret)
6296d0c3831SAndrey Smirnov 		goto err;
6307caff0fcSAndrey Gusakov 	/*
6317caff0fcSAndrey Gusakov 	 * Initially PLLs are in bypass. Force PLL parameter update,
6327caff0fcSAndrey Gusakov 	 * disable PLL bypass, enable PLL
6337caff0fcSAndrey Gusakov 	 */
634134fb306SAndrey Smirnov 	ret = tc_pllupdate(tc, DP0_PLLCTRL);
6356d0c3831SAndrey Smirnov 	if (ret)
6366d0c3831SAndrey Smirnov 		goto err;
6377caff0fcSAndrey Gusakov 
638134fb306SAndrey Smirnov 	ret = tc_pllupdate(tc, DP1_PLLCTRL);
6396d0c3831SAndrey Smirnov 	if (ret)
6406d0c3831SAndrey Smirnov 		goto err;
6417caff0fcSAndrey Gusakov 
6428a6483acSTomi Valkeinen 	ret = tc_poll_timeout(tc, DP_PHY_CTRL, PHY_RDY, PHY_RDY, 100, 100000);
6437caff0fcSAndrey Gusakov 	if (ret == -ETIMEDOUT) {
6447caff0fcSAndrey Gusakov 		dev_err(tc->dev, "Timeout waiting for PHY to become ready");
6457caff0fcSAndrey Gusakov 		return ret;
646ca342386STomi Valkeinen 	} else if (ret) {
6477caff0fcSAndrey Gusakov 		goto err;
648ca342386STomi Valkeinen 	}
6497caff0fcSAndrey Gusakov 
6507caff0fcSAndrey Gusakov 	/* Setup AUX link */
6516d0c3831SAndrey Smirnov 	dp0_auxcfg1  = AUX_RX_FILTER_EN;
6526d0c3831SAndrey Smirnov 	dp0_auxcfg1 |= 0x06 << 8; /* Aux Bit Period Calculator Threshold */
6536d0c3831SAndrey Smirnov 	dp0_auxcfg1 |= 0x3f << 0; /* Aux Response Timeout Timer */
6546d0c3831SAndrey Smirnov 
6556d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_AUXCFG1, dp0_auxcfg1);
6566d0c3831SAndrey Smirnov 	if (ret)
6576d0c3831SAndrey Smirnov 		goto err;
6587caff0fcSAndrey Gusakov 
6597caff0fcSAndrey Gusakov 	return 0;
6607caff0fcSAndrey Gusakov err:
6617caff0fcSAndrey Gusakov 	dev_err(tc->dev, "tc_aux_link_setup failed: %d\n", ret);
6627caff0fcSAndrey Gusakov 	return ret;
6637caff0fcSAndrey Gusakov }
6647caff0fcSAndrey Gusakov 
6657caff0fcSAndrey Gusakov static int tc_get_display_props(struct tc_data *tc)
6667caff0fcSAndrey Gusakov {
667e7dc8d40SThierry Reding 	u8 revision, num_lanes;
668e7dc8d40SThierry Reding 	unsigned int rate;
6697caff0fcSAndrey Gusakov 	int ret;
670d174db07SAndrey Smirnov 	u8 reg;
6717caff0fcSAndrey Gusakov 
6727caff0fcSAndrey Gusakov 	/* Read DP Rx Link Capability */
673e7dc8d40SThierry Reding 	ret = drm_dp_dpcd_read(&tc->aux, DP_DPCD_REV, tc->link.dpcd,
674e7dc8d40SThierry Reding 			       DP_RECEIVER_CAP_SIZE);
6757caff0fcSAndrey Gusakov 	if (ret < 0)
6767caff0fcSAndrey Gusakov 		goto err_dpcd_read;
677e7dc8d40SThierry Reding 
678e7dc8d40SThierry Reding 	revision = tc->link.dpcd[DP_DPCD_REV];
679e7dc8d40SThierry Reding 	rate = drm_dp_max_link_rate(tc->link.dpcd);
680e7dc8d40SThierry Reding 	num_lanes = drm_dp_max_lane_count(tc->link.dpcd);
681e7dc8d40SThierry Reding 
682e7dc8d40SThierry Reding 	if (rate != 162000 && rate != 270000) {
683cffd2b16SAndrey Gusakov 		dev_dbg(tc->dev, "Falling to 2.7 Gbps rate\n");
684e7dc8d40SThierry Reding 		rate = 270000;
685cffd2b16SAndrey Gusakov 	}
686cffd2b16SAndrey Gusakov 
687e7dc8d40SThierry Reding 	tc->link.rate = rate;
688e7dc8d40SThierry Reding 
689e7dc8d40SThierry Reding 	if (num_lanes > 2) {
690cffd2b16SAndrey Gusakov 		dev_dbg(tc->dev, "Falling to 2 lanes\n");
691e7dc8d40SThierry Reding 		num_lanes = 2;
692cffd2b16SAndrey Gusakov 	}
6937caff0fcSAndrey Gusakov 
694e7dc8d40SThierry Reding 	tc->link.num_lanes = num_lanes;
695e7dc8d40SThierry Reding 
696d174db07SAndrey Smirnov 	ret = drm_dp_dpcd_readb(&tc->aux, DP_MAX_DOWNSPREAD, &reg);
6977caff0fcSAndrey Gusakov 	if (ret < 0)
6987caff0fcSAndrey Gusakov 		goto err_dpcd_read;
699d174db07SAndrey Smirnov 	tc->link.spread = reg & DP_MAX_DOWNSPREAD_0_5;
7007caff0fcSAndrey Gusakov 
701d174db07SAndrey Smirnov 	ret = drm_dp_dpcd_readb(&tc->aux, DP_MAIN_LINK_CHANNEL_CODING, &reg);
7027caff0fcSAndrey Gusakov 	if (ret < 0)
7037caff0fcSAndrey Gusakov 		goto err_dpcd_read;
7044b30bf41STomi Valkeinen 
705e5607637STomi Valkeinen 	tc->link.scrambler_dis = false;
7067caff0fcSAndrey Gusakov 	/* read assr */
707d174db07SAndrey Smirnov 	ret = drm_dp_dpcd_readb(&tc->aux, DP_EDP_CONFIGURATION_SET, &reg);
7087caff0fcSAndrey Gusakov 	if (ret < 0)
7097caff0fcSAndrey Gusakov 		goto err_dpcd_read;
710d174db07SAndrey Smirnov 	tc->link.assr = reg & DP_ALTERNATE_SCRAMBLER_RESET_ENABLE;
7117caff0fcSAndrey Gusakov 
7127caff0fcSAndrey Gusakov 	dev_dbg(tc->dev, "DPCD rev: %d.%d, rate: %s, lanes: %d, framing: %s\n",
713e7dc8d40SThierry Reding 		revision >> 4, revision & 0x0f,
714e7dc8d40SThierry Reding 		(tc->link.rate == 162000) ? "1.62Gbps" : "2.7Gbps",
715e7dc8d40SThierry Reding 		tc->link.num_lanes,
716e7dc8d40SThierry Reding 		drm_dp_enhanced_frame_cap(tc->link.dpcd) ?
71798bca69bSThierry Reding 		"enhanced" : "default");
718e5607637STomi Valkeinen 	dev_dbg(tc->dev, "Downspread: %s, scrambler: %s\n",
719e5607637STomi Valkeinen 		tc->link.spread ? "0.5%" : "0.0%",
720e5607637STomi Valkeinen 		tc->link.scrambler_dis ? "disabled" : "enabled");
7217caff0fcSAndrey Gusakov 	dev_dbg(tc->dev, "Display ASSR: %d, TC358767 ASSR: %d\n",
7227caff0fcSAndrey Gusakov 		tc->link.assr, tc->assr);
7237caff0fcSAndrey Gusakov 
7247caff0fcSAndrey Gusakov 	return 0;
7257caff0fcSAndrey Gusakov 
7267caff0fcSAndrey Gusakov err_dpcd_read:
7277caff0fcSAndrey Gusakov 	dev_err(tc->dev, "failed to read DPCD: %d\n", ret);
7287caff0fcSAndrey Gusakov 	return ret;
7297caff0fcSAndrey Gusakov }
7307caff0fcSAndrey Gusakov 
73163f8f3baSLaurent Pinchart static int tc_set_video_mode(struct tc_data *tc,
73263f8f3baSLaurent Pinchart 			     const struct drm_display_mode *mode)
7337caff0fcSAndrey Gusakov {
7347caff0fcSAndrey Gusakov 	int ret;
7357caff0fcSAndrey Gusakov 	int vid_sync_dly;
7367caff0fcSAndrey Gusakov 	int max_tu_symbol;
7377caff0fcSAndrey Gusakov 
7387caff0fcSAndrey Gusakov 	int left_margin = mode->htotal - mode->hsync_end;
7397caff0fcSAndrey Gusakov 	int right_margin = mode->hsync_start - mode->hdisplay;
7407caff0fcSAndrey Gusakov 	int hsync_len = mode->hsync_end - mode->hsync_start;
7417caff0fcSAndrey Gusakov 	int upper_margin = mode->vtotal - mode->vsync_end;
7427caff0fcSAndrey Gusakov 	int lower_margin = mode->vsync_start - mode->vdisplay;
7437caff0fcSAndrey Gusakov 	int vsync_len = mode->vsync_end - mode->vsync_start;
7443f072c30SAndrey Smirnov 	u32 dp0_syncval;
745fd70c775STomi Valkeinen 	u32 bits_per_pixel = 24;
746fd70c775STomi Valkeinen 	u32 in_bw, out_bw;
7477caff0fcSAndrey Gusakov 
74866d1c3b9SAndrey Gusakov 	/*
74966d1c3b9SAndrey Gusakov 	 * Recommended maximum number of symbols transferred in a transfer unit:
75066d1c3b9SAndrey Gusakov 	 * DIV_ROUND_UP((input active video bandwidth in bytes) * tu_size,
75166d1c3b9SAndrey Gusakov 	 *              (output active video bandwidth in bytes))
75266d1c3b9SAndrey Gusakov 	 * Must be less than tu_size.
75366d1c3b9SAndrey Gusakov 	 */
754fd70c775STomi Valkeinen 
755fd70c775STomi Valkeinen 	in_bw = mode->clock * bits_per_pixel / 8;
756e7dc8d40SThierry Reding 	out_bw = tc->link.num_lanes * tc->link.rate;
757fd70c775STomi Valkeinen 	max_tu_symbol = DIV_ROUND_UP(in_bw * TU_SIZE_RECOMMENDED, out_bw);
75866d1c3b9SAndrey Gusakov 
7597caff0fcSAndrey Gusakov 	dev_dbg(tc->dev, "set mode %dx%d\n",
7607caff0fcSAndrey Gusakov 		mode->hdisplay, mode->vdisplay);
7617caff0fcSAndrey Gusakov 	dev_dbg(tc->dev, "H margin %d,%d sync %d\n",
7627caff0fcSAndrey Gusakov 		left_margin, right_margin, hsync_len);
7637caff0fcSAndrey Gusakov 	dev_dbg(tc->dev, "V margin %d,%d sync %d\n",
7647caff0fcSAndrey Gusakov 		upper_margin, lower_margin, vsync_len);
7657caff0fcSAndrey Gusakov 	dev_dbg(tc->dev, "total: %dx%d\n", mode->htotal, mode->vtotal);
7667caff0fcSAndrey Gusakov 
7677caff0fcSAndrey Gusakov 
76866d1c3b9SAndrey Gusakov 	/*
76966d1c3b9SAndrey Gusakov 	 * LCD Ctl Frame Size
77066d1c3b9SAndrey Gusakov 	 * datasheet is not clear of vsdelay in case of DPI
77166d1c3b9SAndrey Gusakov 	 * assume we do not need any delay when DPI is a source of
77266d1c3b9SAndrey Gusakov 	 * sync signals
77366d1c3b9SAndrey Gusakov 	 */
7746d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, VPCTRL0,
7753f072c30SAndrey Smirnov 			   FIELD_PREP(VSDELAY, 0) |
7767caff0fcSAndrey Gusakov 			   OPXLFMT_RGB888 | FRMSYNC_DISABLED | MSF_DISABLED);
7776d0c3831SAndrey Smirnov 	if (ret)
7786d0c3831SAndrey Smirnov 		return ret;
7796d0c3831SAndrey Smirnov 
7806d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, HTIM01,
7813f072c30SAndrey Smirnov 			   FIELD_PREP(HBPR, ALIGN(left_margin, 2)) |
7823f072c30SAndrey Smirnov 			   FIELD_PREP(HPW, ALIGN(hsync_len, 2)));
7836d0c3831SAndrey Smirnov 	if (ret)
7846d0c3831SAndrey Smirnov 		return ret;
7856d0c3831SAndrey Smirnov 
7866d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, HTIM02,
7873f072c30SAndrey Smirnov 			   FIELD_PREP(HDISPR, ALIGN(mode->hdisplay, 2)) |
7883f072c30SAndrey Smirnov 			   FIELD_PREP(HFPR, ALIGN(right_margin, 2)));
7896d0c3831SAndrey Smirnov 	if (ret)
7906d0c3831SAndrey Smirnov 		return ret;
7916d0c3831SAndrey Smirnov 
7926d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, VTIM01,
7933f072c30SAndrey Smirnov 			   FIELD_PREP(VBPR, upper_margin) |
7943f072c30SAndrey Smirnov 			   FIELD_PREP(VSPR, vsync_len));
7956d0c3831SAndrey Smirnov 	if (ret)
7966d0c3831SAndrey Smirnov 		return ret;
7976d0c3831SAndrey Smirnov 
7986d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, VTIM02,
7993f072c30SAndrey Smirnov 			   FIELD_PREP(VFPR, lower_margin) |
8003f072c30SAndrey Smirnov 			   FIELD_PREP(VDISPR, mode->vdisplay));
8016d0c3831SAndrey Smirnov 	if (ret)
8026d0c3831SAndrey Smirnov 		return ret;
8036d0c3831SAndrey Smirnov 
8046d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, VFUEN0, VFUEN); /* update settings */
8056d0c3831SAndrey Smirnov 	if (ret)
8066d0c3831SAndrey Smirnov 		return ret;
8077caff0fcSAndrey Gusakov 
8087caff0fcSAndrey Gusakov 	/* Test pattern settings */
8096d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, TSTCTL,
8103f072c30SAndrey Smirnov 			   FIELD_PREP(COLOR_R, 120) |
8113f072c30SAndrey Smirnov 			   FIELD_PREP(COLOR_G, 20) |
8123f072c30SAndrey Smirnov 			   FIELD_PREP(COLOR_B, 99) |
8133f072c30SAndrey Smirnov 			   ENI2CFILTER |
8143f072c30SAndrey Smirnov 			   FIELD_PREP(COLOR_BAR_MODE, COLOR_BAR_MODE_BARS));
8156d0c3831SAndrey Smirnov 	if (ret)
8166d0c3831SAndrey Smirnov 		return ret;
8177caff0fcSAndrey Gusakov 
8187caff0fcSAndrey Gusakov 	/* DP Main Stream Attributes */
8197caff0fcSAndrey Gusakov 	vid_sync_dly = hsync_len + left_margin + mode->hdisplay;
8206d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_VIDSYNCDELAY,
8213f072c30SAndrey Smirnov 		 FIELD_PREP(THRESH_DLY, max_tu_symbol) |
8223f072c30SAndrey Smirnov 		 FIELD_PREP(VID_SYNC_DLY, vid_sync_dly));
8237caff0fcSAndrey Gusakov 
8246d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_TOTALVAL,
8253f072c30SAndrey Smirnov 			   FIELD_PREP(H_TOTAL, mode->htotal) |
8263f072c30SAndrey Smirnov 			   FIELD_PREP(V_TOTAL, mode->vtotal));
8276d0c3831SAndrey Smirnov 	if (ret)
8286d0c3831SAndrey Smirnov 		return ret;
8297caff0fcSAndrey Gusakov 
8306d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_STARTVAL,
8313f072c30SAndrey Smirnov 			   FIELD_PREP(H_START, left_margin + hsync_len) |
8323f072c30SAndrey Smirnov 			   FIELD_PREP(V_START, upper_margin + vsync_len));
8336d0c3831SAndrey Smirnov 	if (ret)
8346d0c3831SAndrey Smirnov 		return ret;
8357caff0fcSAndrey Gusakov 
8366d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_ACTIVEVAL,
8373f072c30SAndrey Smirnov 			   FIELD_PREP(V_ACT, mode->vdisplay) |
8383f072c30SAndrey Smirnov 			   FIELD_PREP(H_ACT, mode->hdisplay));
8396d0c3831SAndrey Smirnov 	if (ret)
8406d0c3831SAndrey Smirnov 		return ret;
8417caff0fcSAndrey Gusakov 
8423f072c30SAndrey Smirnov 	dp0_syncval = FIELD_PREP(VS_WIDTH, vsync_len) |
8433f072c30SAndrey Smirnov 		      FIELD_PREP(HS_WIDTH, hsync_len);
8447caff0fcSAndrey Gusakov 
8453f072c30SAndrey Smirnov 	if (mode->flags & DRM_MODE_FLAG_NVSYNC)
8463f072c30SAndrey Smirnov 		dp0_syncval |= SYNCVAL_VS_POL_ACTIVE_LOW;
8477caff0fcSAndrey Gusakov 
8483f072c30SAndrey Smirnov 	if (mode->flags & DRM_MODE_FLAG_NHSYNC)
8493f072c30SAndrey Smirnov 		dp0_syncval |= SYNCVAL_HS_POL_ACTIVE_LOW;
8503f072c30SAndrey Smirnov 
8516d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_SYNCVAL, dp0_syncval);
8526d0c3831SAndrey Smirnov 	if (ret)
8536d0c3831SAndrey Smirnov 		return ret;
8543f072c30SAndrey Smirnov 
8556d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DPIPXLFMT,
8563f072c30SAndrey Smirnov 			   VS_POL_ACTIVE_LOW | HS_POL_ACTIVE_LOW |
8573f072c30SAndrey Smirnov 			   DE_POL_ACTIVE_HIGH | SUB_CFG_TYPE_CONFIG1 |
8583f072c30SAndrey Smirnov 			   DPI_BPP_RGB888);
8596d0c3831SAndrey Smirnov 	if (ret)
8606d0c3831SAndrey Smirnov 		return ret;
8613f072c30SAndrey Smirnov 
8626d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_MISC,
8633f072c30SAndrey Smirnov 			   FIELD_PREP(MAX_TU_SYMBOL, max_tu_symbol) |
8643f072c30SAndrey Smirnov 			   FIELD_PREP(TU_SIZE, TU_SIZE_RECOMMENDED) |
865f3b8adbeSAndrey Gusakov 			   BPC_8);
8666d0c3831SAndrey Smirnov 	if (ret)
8676d0c3831SAndrey Smirnov 		return ret;
8687caff0fcSAndrey Gusakov 
8697caff0fcSAndrey Gusakov 	return 0;
8707caff0fcSAndrey Gusakov }
8717caff0fcSAndrey Gusakov 
872f9538357STomi Valkeinen static int tc_wait_link_training(struct tc_data *tc)
8737caff0fcSAndrey Gusakov {
8747caff0fcSAndrey Gusakov 	u32 value;
8757caff0fcSAndrey Gusakov 	int ret;
8767caff0fcSAndrey Gusakov 
877aa92213fSAndrey Smirnov 	ret = tc_poll_timeout(tc, DP0_LTSTAT, LT_LOOPDONE,
8788a6483acSTomi Valkeinen 			      LT_LOOPDONE, 500, 100000);
879aa92213fSAndrey Smirnov 	if (ret) {
880f9538357STomi Valkeinen 		dev_err(tc->dev, "Link training timeout waiting for LT_LOOPDONE!\n");
881aa92213fSAndrey Smirnov 		return ret;
8827caff0fcSAndrey Gusakov 	}
8837caff0fcSAndrey Gusakov 
8846d0c3831SAndrey Smirnov 	ret = regmap_read(tc->regmap, DP0_LTSTAT, &value);
8856d0c3831SAndrey Smirnov 	if (ret)
8866d0c3831SAndrey Smirnov 		return ret;
887f9538357STomi Valkeinen 
888aa92213fSAndrey Smirnov 	return (value >> 8) & 0x7;
8897caff0fcSAndrey Gusakov }
8907caff0fcSAndrey Gusakov 
891cb3263b2STomi Valkeinen static int tc_main_link_enable(struct tc_data *tc)
8927caff0fcSAndrey Gusakov {
8937caff0fcSAndrey Gusakov 	struct drm_dp_aux *aux = &tc->aux;
8947caff0fcSAndrey Gusakov 	struct device *dev = tc->dev;
8957caff0fcSAndrey Gusakov 	u32 dp_phy_ctrl;
8967caff0fcSAndrey Gusakov 	u32 value;
8977caff0fcSAndrey Gusakov 	int ret;
89832d36219SAndrey Smirnov 	u8 tmp[DP_LINK_STATUS_SIZE];
8997caff0fcSAndrey Gusakov 
900cb3263b2STomi Valkeinen 	dev_dbg(tc->dev, "link enable\n");
901cb3263b2STomi Valkeinen 
9026d0c3831SAndrey Smirnov 	ret = regmap_read(tc->regmap, DP0CTL, &value);
9036d0c3831SAndrey Smirnov 	if (ret)
9046d0c3831SAndrey Smirnov 		return ret;
90567bca92fSTomi Valkeinen 
9066d0c3831SAndrey Smirnov 	if (WARN_ON(value & DP_EN)) {
9076d0c3831SAndrey Smirnov 		ret = regmap_write(tc->regmap, DP0CTL, 0);
9086d0c3831SAndrey Smirnov 		if (ret)
9096d0c3831SAndrey Smirnov 			return ret;
9106d0c3831SAndrey Smirnov 	}
9116d0c3831SAndrey Smirnov 
9126d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_SRCCTRL, tc_srcctrl(tc));
9136d0c3831SAndrey Smirnov 	if (ret)
9146d0c3831SAndrey Smirnov 		return ret;
9159a63bd6fSTomi Valkeinen 	/* SSCG and BW27 on DP1 must be set to the same as on DP0 */
9166d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP1_SRCCTRL,
9179a63bd6fSTomi Valkeinen 		 (tc->link.spread ? DP0_SRCCTRL_SSCG : 0) |
918e7dc8d40SThierry Reding 		 ((tc->link.rate != 162000) ? DP0_SRCCTRL_BW27 : 0));
9196d0c3831SAndrey Smirnov 	if (ret)
9206d0c3831SAndrey Smirnov 		return ret;
9217caff0fcSAndrey Gusakov 
922c49f60dfSAndrey Smirnov 	ret = tc_set_syspllparam(tc);
9236d0c3831SAndrey Smirnov 	if (ret)
9246d0c3831SAndrey Smirnov 		return ret;
925adf41098STomi Valkeinen 
9267caff0fcSAndrey Gusakov 	/* Setup Main Link */
9274d9d54a7STomi Valkeinen 	dp_phy_ctrl = BGREN | PWR_SW_EN | PHY_A0_EN | PHY_M0_EN;
928e7dc8d40SThierry Reding 	if (tc->link.num_lanes == 2)
9294d9d54a7STomi Valkeinen 		dp_phy_ctrl |= PHY_2LANE;
9306d0c3831SAndrey Smirnov 
9316d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP_PHY_CTRL, dp_phy_ctrl);
9326d0c3831SAndrey Smirnov 	if (ret)
9336d0c3831SAndrey Smirnov 		return ret;
9347caff0fcSAndrey Gusakov 
9357caff0fcSAndrey Gusakov 	/* PLL setup */
936134fb306SAndrey Smirnov 	ret = tc_pllupdate(tc, DP0_PLLCTRL);
9376d0c3831SAndrey Smirnov 	if (ret)
9386d0c3831SAndrey Smirnov 		return ret;
9397caff0fcSAndrey Gusakov 
940134fb306SAndrey Smirnov 	ret = tc_pllupdate(tc, DP1_PLLCTRL);
9416d0c3831SAndrey Smirnov 	if (ret)
9426d0c3831SAndrey Smirnov 		return ret;
9437caff0fcSAndrey Gusakov 
9447caff0fcSAndrey Gusakov 	/* Reset/Enable Main Links */
9457caff0fcSAndrey Gusakov 	dp_phy_ctrl |= DP_PHY_RST | PHY_M1_RST | PHY_M0_RST;
9466d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP_PHY_CTRL, dp_phy_ctrl);
9477caff0fcSAndrey Gusakov 	usleep_range(100, 200);
9487caff0fcSAndrey Gusakov 	dp_phy_ctrl &= ~(DP_PHY_RST | PHY_M1_RST | PHY_M0_RST);
9496d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP_PHY_CTRL, dp_phy_ctrl);
9507caff0fcSAndrey Gusakov 
9518a6483acSTomi Valkeinen 	ret = tc_poll_timeout(tc, DP_PHY_CTRL, PHY_RDY, PHY_RDY, 500, 100000);
952ebcce4e6SAndrey Smirnov 	if (ret) {
9537caff0fcSAndrey Gusakov 		dev_err(dev, "timeout waiting for phy become ready");
954ebcce4e6SAndrey Smirnov 		return ret;
9557caff0fcSAndrey Gusakov 	}
9567caff0fcSAndrey Gusakov 
9577caff0fcSAndrey Gusakov 	/* Set misc: 8 bits per color */
9587caff0fcSAndrey Gusakov 	ret = regmap_update_bits(tc->regmap, DP0_MISC, BPC_8, BPC_8);
9597caff0fcSAndrey Gusakov 	if (ret)
9606d0c3831SAndrey Smirnov 		return ret;
9617caff0fcSAndrey Gusakov 
9627caff0fcSAndrey Gusakov 	/*
9637caff0fcSAndrey Gusakov 	 * ASSR mode
9647caff0fcSAndrey Gusakov 	 * on TC358767 side ASSR configured through strap pin
9657caff0fcSAndrey Gusakov 	 * seems there is no way to change this setting from SW
9667caff0fcSAndrey Gusakov 	 *
9677caff0fcSAndrey Gusakov 	 * check is tc configured for same mode
9687caff0fcSAndrey Gusakov 	 */
9697caff0fcSAndrey Gusakov 	if (tc->assr != tc->link.assr) {
9707caff0fcSAndrey Gusakov 		dev_dbg(dev, "Trying to set display to ASSR: %d\n",
9717caff0fcSAndrey Gusakov 			tc->assr);
9727caff0fcSAndrey Gusakov 		/* try to set ASSR on display side */
9737caff0fcSAndrey Gusakov 		tmp[0] = tc->assr;
9747caff0fcSAndrey Gusakov 		ret = drm_dp_dpcd_writeb(aux, DP_EDP_CONFIGURATION_SET, tmp[0]);
9757caff0fcSAndrey Gusakov 		if (ret < 0)
9767caff0fcSAndrey Gusakov 			goto err_dpcd_read;
9777caff0fcSAndrey Gusakov 		/* read back */
9787caff0fcSAndrey Gusakov 		ret = drm_dp_dpcd_readb(aux, DP_EDP_CONFIGURATION_SET, tmp);
9797caff0fcSAndrey Gusakov 		if (ret < 0)
9807caff0fcSAndrey Gusakov 			goto err_dpcd_read;
9817caff0fcSAndrey Gusakov 
9827caff0fcSAndrey Gusakov 		if (tmp[0] != tc->assr) {
98387291e5dSLucas Stach 			dev_dbg(dev, "Failed to switch display ASSR to %d, falling back to unscrambled mode\n",
9847caff0fcSAndrey Gusakov 				tc->assr);
9857caff0fcSAndrey Gusakov 			/* trying with disabled scrambler */
986e5607637STomi Valkeinen 			tc->link.scrambler_dis = true;
9877caff0fcSAndrey Gusakov 		}
9887caff0fcSAndrey Gusakov 	}
9897caff0fcSAndrey Gusakov 
9907caff0fcSAndrey Gusakov 	/* Setup Link & DPRx Config for Training */
991e7dc8d40SThierry Reding 	tmp[0] = drm_dp_link_rate_to_bw_code(tc->link.rate);
992e7dc8d40SThierry Reding 	tmp[1] = tc->link.num_lanes;
993e7dc8d40SThierry Reding 
994e7dc8d40SThierry Reding 	if (drm_dp_enhanced_frame_cap(tc->link.dpcd))
995e7dc8d40SThierry Reding 		tmp[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
996e7dc8d40SThierry Reding 
997e7dc8d40SThierry Reding 	ret = drm_dp_dpcd_write(aux, DP_LINK_BW_SET, tmp, 2);
9987caff0fcSAndrey Gusakov 	if (ret < 0)
9997caff0fcSAndrey Gusakov 		goto err_dpcd_write;
10007caff0fcSAndrey Gusakov 
10017caff0fcSAndrey Gusakov 	/* DOWNSPREAD_CTRL */
10027caff0fcSAndrey Gusakov 	tmp[0] = tc->link.spread ? DP_SPREAD_AMP_0_5 : 0x00;
10037caff0fcSAndrey Gusakov 	/* MAIN_LINK_CHANNEL_CODING_SET */
10044b30bf41STomi Valkeinen 	tmp[1] =  DP_SET_ANSI_8B10B;
10057caff0fcSAndrey Gusakov 	ret = drm_dp_dpcd_write(aux, DP_DOWNSPREAD_CTRL, tmp, 2);
10067caff0fcSAndrey Gusakov 	if (ret < 0)
10077caff0fcSAndrey Gusakov 		goto err_dpcd_write;
10087caff0fcSAndrey Gusakov 
1009c28d1484STomi Valkeinen 	/* Reset voltage-swing & pre-emphasis */
1010c28d1484STomi Valkeinen 	tmp[0] = tmp[1] = DP_TRAIN_VOLTAGE_SWING_LEVEL_0 |
1011c28d1484STomi Valkeinen 			  DP_TRAIN_PRE_EMPH_LEVEL_0;
1012c28d1484STomi Valkeinen 	ret = drm_dp_dpcd_write(aux, DP_TRAINING_LANE0_SET, tmp, 2);
1013c28d1484STomi Valkeinen 	if (ret < 0)
1014c28d1484STomi Valkeinen 		goto err_dpcd_write;
1015c28d1484STomi Valkeinen 
1016f9538357STomi Valkeinen 	/* Clock-Recovery */
1017f9538357STomi Valkeinen 
1018f9538357STomi Valkeinen 	/* Set DPCD 0x102 for Training Pattern 1 */
10196d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_SNKLTCTRL,
10206d0c3831SAndrey Smirnov 			   DP_LINK_SCRAMBLING_DISABLE |
1021f9538357STomi Valkeinen 			   DP_TRAINING_PATTERN_1);
10226d0c3831SAndrey Smirnov 	if (ret)
10236d0c3831SAndrey Smirnov 		return ret;
1024f9538357STomi Valkeinen 
10256d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_LTLOOPCTRL,
1026f9538357STomi Valkeinen 			   (15 << 28) |	/* Defer Iteration Count */
1027f9538357STomi Valkeinen 			   (15 << 24) |	/* Loop Iteration Count */
1028f9538357STomi Valkeinen 			   (0xd << 0));	/* Loop Timer Delay */
10296d0c3831SAndrey Smirnov 	if (ret)
10306d0c3831SAndrey Smirnov 		return ret;
1031f9538357STomi Valkeinen 
10326d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_SRCCTRL,
10336d0c3831SAndrey Smirnov 			   tc_srcctrl(tc) | DP0_SRCCTRL_SCRMBLDIS |
10346d0c3831SAndrey Smirnov 			   DP0_SRCCTRL_AUTOCORRECT |
10356d0c3831SAndrey Smirnov 			   DP0_SRCCTRL_TP1);
10366d0c3831SAndrey Smirnov 	if (ret)
10376d0c3831SAndrey Smirnov 		return ret;
1038f9538357STomi Valkeinen 
1039f9538357STomi Valkeinen 	/* Enable DP0 to start Link Training */
10406d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0CTL,
1041e7dc8d40SThierry Reding 			   (drm_dp_enhanced_frame_cap(tc->link.dpcd) ?
1042e7dc8d40SThierry Reding 				EF_EN : 0) | DP_EN);
10436d0c3831SAndrey Smirnov 	if (ret)
10446d0c3831SAndrey Smirnov 		return ret;
1045f9538357STomi Valkeinen 
1046f9538357STomi Valkeinen 	/* wait */
10476d0c3831SAndrey Smirnov 
1048f9538357STomi Valkeinen 	ret = tc_wait_link_training(tc);
1049f9538357STomi Valkeinen 	if (ret < 0)
10506d0c3831SAndrey Smirnov 		return ret;
10517caff0fcSAndrey Gusakov 
1052f9538357STomi Valkeinen 	if (ret) {
1053f9538357STomi Valkeinen 		dev_err(tc->dev, "Link training phase 1 failed: %s\n",
1054f9538357STomi Valkeinen 			training_pattern1_errors[ret]);
10556d0c3831SAndrey Smirnov 		return -ENODEV;
1056f9538357STomi Valkeinen 	}
1057f9538357STomi Valkeinen 
1058f9538357STomi Valkeinen 	/* Channel Equalization */
1059f9538357STomi Valkeinen 
1060f9538357STomi Valkeinen 	/* Set DPCD 0x102 for Training Pattern 2 */
10616d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_SNKLTCTRL,
10626d0c3831SAndrey Smirnov 			   DP_LINK_SCRAMBLING_DISABLE |
1063f9538357STomi Valkeinen 			   DP_TRAINING_PATTERN_2);
10646d0c3831SAndrey Smirnov 	if (ret)
10656d0c3831SAndrey Smirnov 		return ret;
1066f9538357STomi Valkeinen 
10676d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_SRCCTRL,
10686d0c3831SAndrey Smirnov 			   tc_srcctrl(tc) | DP0_SRCCTRL_SCRMBLDIS |
10696d0c3831SAndrey Smirnov 			   DP0_SRCCTRL_AUTOCORRECT |
10706d0c3831SAndrey Smirnov 			   DP0_SRCCTRL_TP2);
10716d0c3831SAndrey Smirnov 	if (ret)
10726d0c3831SAndrey Smirnov 		return ret;
1073f9538357STomi Valkeinen 
1074f9538357STomi Valkeinen 	/* wait */
1075f9538357STomi Valkeinen 	ret = tc_wait_link_training(tc);
1076f9538357STomi Valkeinen 	if (ret < 0)
10776d0c3831SAndrey Smirnov 		return ret;
1078f9538357STomi Valkeinen 
1079f9538357STomi Valkeinen 	if (ret) {
1080f9538357STomi Valkeinen 		dev_err(tc->dev, "Link training phase 2 failed: %s\n",
1081f9538357STomi Valkeinen 			training_pattern2_errors[ret]);
10826d0c3831SAndrey Smirnov 		return -ENODEV;
1083f9538357STomi Valkeinen 	}
10847caff0fcSAndrey Gusakov 
10850776a269STomi Valkeinen 	/*
10860776a269STomi Valkeinen 	 * Toshiba's documentation suggests to first clear DPCD 0x102, then
10870776a269STomi Valkeinen 	 * clear the training pattern bit in DP0_SRCCTRL. Testing shows
10880776a269STomi Valkeinen 	 * that the link sometimes drops if those steps are done in that order,
10890776a269STomi Valkeinen 	 * but if the steps are done in reverse order, the link stays up.
10900776a269STomi Valkeinen 	 *
10910776a269STomi Valkeinen 	 * So we do the steps differently than documented here.
10920776a269STomi Valkeinen 	 */
10930776a269STomi Valkeinen 
10940776a269STomi Valkeinen 	/* Clear Training Pattern, set AutoCorrect Mode = 1 */
10956d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_SRCCTRL, tc_srcctrl(tc) |
10966d0c3831SAndrey Smirnov 			   DP0_SRCCTRL_AUTOCORRECT);
10976d0c3831SAndrey Smirnov 	if (ret)
10986d0c3831SAndrey Smirnov 		return ret;
10990776a269STomi Valkeinen 
11007caff0fcSAndrey Gusakov 	/* Clear DPCD 0x102 */
11017caff0fcSAndrey Gusakov 	/* Note: Can Not use DP0_SNKLTCTRL (0x06E4) short cut */
11027caff0fcSAndrey Gusakov 	tmp[0] = tc->link.scrambler_dis ? DP_LINK_SCRAMBLING_DISABLE : 0x00;
11037caff0fcSAndrey Gusakov 	ret = drm_dp_dpcd_writeb(aux, DP_TRAINING_PATTERN_SET, tmp[0]);
11047caff0fcSAndrey Gusakov 	if (ret < 0)
11057caff0fcSAndrey Gusakov 		goto err_dpcd_write;
11067caff0fcSAndrey Gusakov 
11070bf25146STomi Valkeinen 	/* Check link status */
11080bf25146STomi Valkeinen 	ret = drm_dp_dpcd_read_link_status(aux, tmp);
11097caff0fcSAndrey Gusakov 	if (ret < 0)
11107caff0fcSAndrey Gusakov 		goto err_dpcd_read;
11117caff0fcSAndrey Gusakov 
11120bf25146STomi Valkeinen 	ret = 0;
11137caff0fcSAndrey Gusakov 
11140bf25146STomi Valkeinen 	value = tmp[0] & DP_CHANNEL_EQ_BITS;
11150bf25146STomi Valkeinen 
11160bf25146STomi Valkeinen 	if (value != DP_CHANNEL_EQ_BITS) {
11170bf25146STomi Valkeinen 		dev_err(tc->dev, "Lane 0 failed: %x\n", value);
11180bf25146STomi Valkeinen 		ret = -ENODEV;
11190bf25146STomi Valkeinen 	}
11200bf25146STomi Valkeinen 
1121e7dc8d40SThierry Reding 	if (tc->link.num_lanes == 2) {
11220bf25146STomi Valkeinen 		value = (tmp[0] >> 4) & DP_CHANNEL_EQ_BITS;
11230bf25146STomi Valkeinen 
11240bf25146STomi Valkeinen 		if (value != DP_CHANNEL_EQ_BITS) {
11250bf25146STomi Valkeinen 			dev_err(tc->dev, "Lane 1 failed: %x\n", value);
11260bf25146STomi Valkeinen 			ret = -ENODEV;
11270bf25146STomi Valkeinen 		}
11280bf25146STomi Valkeinen 
11290bf25146STomi Valkeinen 		if (!(tmp[2] & DP_INTERLANE_ALIGN_DONE)) {
11300bf25146STomi Valkeinen 			dev_err(tc->dev, "Interlane align failed\n");
11310bf25146STomi Valkeinen 			ret = -ENODEV;
11320bf25146STomi Valkeinen 		}
11330bf25146STomi Valkeinen 	}
11340bf25146STomi Valkeinen 
11350bf25146STomi Valkeinen 	if (ret) {
11360bf25146STomi Valkeinen 		dev_err(dev, "0x0202 LANE0_1_STATUS:            0x%02x\n", tmp[0]);
11370bf25146STomi Valkeinen 		dev_err(dev, "0x0203 LANE2_3_STATUS             0x%02x\n", tmp[1]);
11380bf25146STomi Valkeinen 		dev_err(dev, "0x0204 LANE_ALIGN_STATUS_UPDATED: 0x%02x\n", tmp[2]);
11390bf25146STomi Valkeinen 		dev_err(dev, "0x0205 SINK_STATUS:               0x%02x\n", tmp[3]);
11400bf25146STomi Valkeinen 		dev_err(dev, "0x0206 ADJUST_REQUEST_LANE0_1:    0x%02x\n", tmp[4]);
11410bf25146STomi Valkeinen 		dev_err(dev, "0x0207 ADJUST_REQUEST_LANE2_3:    0x%02x\n", tmp[5]);
11426d0c3831SAndrey Smirnov 		return ret;
11437caff0fcSAndrey Gusakov 	}
11447caff0fcSAndrey Gusakov 
11457caff0fcSAndrey Gusakov 	return 0;
11467caff0fcSAndrey Gusakov err_dpcd_read:
11477caff0fcSAndrey Gusakov 	dev_err(tc->dev, "Failed to read DPCD: %d\n", ret);
11487caff0fcSAndrey Gusakov 	return ret;
11497caff0fcSAndrey Gusakov err_dpcd_write:
11507caff0fcSAndrey Gusakov 	dev_err(tc->dev, "Failed to write DPCD: %d\n", ret);
11517caff0fcSAndrey Gusakov 	return ret;
11527caff0fcSAndrey Gusakov }
11537caff0fcSAndrey Gusakov 
1154cb3263b2STomi Valkeinen static int tc_main_link_disable(struct tc_data *tc)
1155cb3263b2STomi Valkeinen {
1156cb3263b2STomi Valkeinen 	int ret;
1157cb3263b2STomi Valkeinen 
1158cb3263b2STomi Valkeinen 	dev_dbg(tc->dev, "link disable\n");
1159cb3263b2STomi Valkeinen 
11606d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_SRCCTRL, 0);
11616d0c3831SAndrey Smirnov 	if (ret)
1162cb3263b2STomi Valkeinen 		return ret;
11636d0c3831SAndrey Smirnov 
11646d0c3831SAndrey Smirnov 	return regmap_write(tc->regmap, DP0CTL, 0);
1165cb3263b2STomi Valkeinen }
1166cb3263b2STomi Valkeinen 
1167*a219062bSMarek Vasut static int tc_edp_stream_enable(struct tc_data *tc)
11687caff0fcSAndrey Gusakov {
11697caff0fcSAndrey Gusakov 	int ret;
11707caff0fcSAndrey Gusakov 	u32 value;
11717caff0fcSAndrey Gusakov 
117280d57245STomi Valkeinen 	dev_dbg(tc->dev, "enable video stream\n");
11737caff0fcSAndrey Gusakov 
1174bb248368STomi Valkeinen 	/* PXL PLL setup */
1175bb248368STomi Valkeinen 	if (tc_test_pattern) {
1176bb248368STomi Valkeinen 		ret = tc_pxl_pll_en(tc, clk_get_rate(tc->refclk),
117746648a3cSTomi Valkeinen 				    1000 * tc->mode.clock);
1178bb248368STomi Valkeinen 		if (ret)
11796d0c3831SAndrey Smirnov 			return ret;
1180bb248368STomi Valkeinen 	}
1181bb248368STomi Valkeinen 
118246648a3cSTomi Valkeinen 	ret = tc_set_video_mode(tc, &tc->mode);
11835761a259STomi Valkeinen 	if (ret)
118480d57245STomi Valkeinen 		return ret;
11855761a259STomi Valkeinen 
11865761a259STomi Valkeinen 	/* Set M/N */
11875761a259STomi Valkeinen 	ret = tc_stream_clock_calc(tc);
11885761a259STomi Valkeinen 	if (ret)
118980d57245STomi Valkeinen 		return ret;
11905761a259STomi Valkeinen 
11917caff0fcSAndrey Gusakov 	value = VID_MN_GEN | DP_EN;
1192e7dc8d40SThierry Reding 	if (drm_dp_enhanced_frame_cap(tc->link.dpcd))
11937caff0fcSAndrey Gusakov 		value |= EF_EN;
11946d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0CTL, value);
11956d0c3831SAndrey Smirnov 	if (ret)
11966d0c3831SAndrey Smirnov 		return ret;
11977caff0fcSAndrey Gusakov 	/*
11987caff0fcSAndrey Gusakov 	 * VID_EN assertion should be delayed by at least N * LSCLK
11997caff0fcSAndrey Gusakov 	 * cycles from the time VID_MN_GEN is enabled in order to
12007caff0fcSAndrey Gusakov 	 * generate stable values for VID_M. LSCLK is 270 MHz or
12017caff0fcSAndrey Gusakov 	 * 162 MHz, VID_N is set to 32768 in  tc_stream_clock_calc(),
12027caff0fcSAndrey Gusakov 	 * so a delay of at least 203 us should suffice.
12037caff0fcSAndrey Gusakov 	 */
12047caff0fcSAndrey Gusakov 	usleep_range(500, 1000);
12057caff0fcSAndrey Gusakov 	value |= VID_EN;
12066d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0CTL, value);
12076d0c3831SAndrey Smirnov 	if (ret)
12086d0c3831SAndrey Smirnov 		return ret;
12097caff0fcSAndrey Gusakov 	/* Set input interface */
12107caff0fcSAndrey Gusakov 	value = DP0_AUDSRC_NO_INPUT;
12117caff0fcSAndrey Gusakov 	if (tc_test_pattern)
12127caff0fcSAndrey Gusakov 		value |= DP0_VIDSRC_COLOR_BAR;
12137caff0fcSAndrey Gusakov 	else
12147caff0fcSAndrey Gusakov 		value |= DP0_VIDSRC_DPI_RX;
12156d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, SYSCTRL, value);
12166d0c3831SAndrey Smirnov 	if (ret)
12176d0c3831SAndrey Smirnov 		return ret;
121880d57245STomi Valkeinen 
121980d57245STomi Valkeinen 	return 0;
12207caff0fcSAndrey Gusakov }
12217caff0fcSAndrey Gusakov 
1222*a219062bSMarek Vasut static int tc_edp_stream_disable(struct tc_data *tc)
122380d57245STomi Valkeinen {
122480d57245STomi Valkeinen 	int ret;
122580d57245STomi Valkeinen 
122680d57245STomi Valkeinen 	dev_dbg(tc->dev, "disable video stream\n");
122780d57245STomi Valkeinen 
12286d0c3831SAndrey Smirnov 	ret = regmap_update_bits(tc->regmap, DP0CTL, VID_EN, 0);
12296d0c3831SAndrey Smirnov 	if (ret)
12306d0c3831SAndrey Smirnov 		return ret;
123180d57245STomi Valkeinen 
1232bb248368STomi Valkeinen 	tc_pxl_pll_dis(tc);
1233bb248368STomi Valkeinen 
12347caff0fcSAndrey Gusakov 	return 0;
12357caff0fcSAndrey Gusakov }
12367caff0fcSAndrey Gusakov 
1237*a219062bSMarek Vasut static void tc_edp_bridge_enable(struct drm_bridge *bridge)
12387caff0fcSAndrey Gusakov {
12397caff0fcSAndrey Gusakov 	struct tc_data *tc = bridge_to_tc(bridge);
12407caff0fcSAndrey Gusakov 	int ret;
12417caff0fcSAndrey Gusakov 
1242f25ee501STomi Valkeinen 	ret = tc_get_display_props(tc);
1243f25ee501STomi Valkeinen 	if (ret < 0) {
1244f25ee501STomi Valkeinen 		dev_err(tc->dev, "failed to read display props: %d\n", ret);
1245f25ee501STomi Valkeinen 		return;
1246f25ee501STomi Valkeinen 	}
1247f25ee501STomi Valkeinen 
1248cb3263b2STomi Valkeinen 	ret = tc_main_link_enable(tc);
12497caff0fcSAndrey Gusakov 	if (ret < 0) {
1250cb3263b2STomi Valkeinen 		dev_err(tc->dev, "main link enable error: %d\n", ret);
12517caff0fcSAndrey Gusakov 		return;
12527caff0fcSAndrey Gusakov 	}
12537caff0fcSAndrey Gusakov 
1254*a219062bSMarek Vasut 	ret = tc_edp_stream_enable(tc);
12557caff0fcSAndrey Gusakov 	if (ret < 0) {
12567caff0fcSAndrey Gusakov 		dev_err(tc->dev, "main link stream start error: %d\n", ret);
1257cb3263b2STomi Valkeinen 		tc_main_link_disable(tc);
12587caff0fcSAndrey Gusakov 		return;
12597caff0fcSAndrey Gusakov 	}
12607caff0fcSAndrey Gusakov }
12617caff0fcSAndrey Gusakov 
1262*a219062bSMarek Vasut static void tc_edp_bridge_disable(struct drm_bridge *bridge)
12637caff0fcSAndrey Gusakov {
12647caff0fcSAndrey Gusakov 	struct tc_data *tc = bridge_to_tc(bridge);
12657caff0fcSAndrey Gusakov 	int ret;
12667caff0fcSAndrey Gusakov 
1267*a219062bSMarek Vasut 	ret = tc_edp_stream_disable(tc);
12687caff0fcSAndrey Gusakov 	if (ret < 0)
12697caff0fcSAndrey Gusakov 		dev_err(tc->dev, "main link stream stop error: %d\n", ret);
1270cb3263b2STomi Valkeinen 
1271cb3263b2STomi Valkeinen 	ret = tc_main_link_disable(tc);
1272cb3263b2STomi Valkeinen 	if (ret < 0)
1273cb3263b2STomi Valkeinen 		dev_err(tc->dev, "main link disable error: %d\n", ret);
12747caff0fcSAndrey Gusakov }
12757caff0fcSAndrey Gusakov 
12767caff0fcSAndrey Gusakov static bool tc_bridge_mode_fixup(struct drm_bridge *bridge,
12777caff0fcSAndrey Gusakov 				 const struct drm_display_mode *mode,
12787caff0fcSAndrey Gusakov 				 struct drm_display_mode *adj)
12797caff0fcSAndrey Gusakov {
12807caff0fcSAndrey Gusakov 	/* Fixup sync polarities, both hsync and vsync are active low */
12817caff0fcSAndrey Gusakov 	adj->flags = mode->flags;
12827caff0fcSAndrey Gusakov 	adj->flags |= (DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC);
12837caff0fcSAndrey Gusakov 	adj->flags &= ~(DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC);
12847caff0fcSAndrey Gusakov 
12857caff0fcSAndrey Gusakov 	return true;
12867caff0fcSAndrey Gusakov }
12877caff0fcSAndrey Gusakov 
1288*a219062bSMarek Vasut static enum drm_mode_status
1289*a219062bSMarek Vasut tc_edp_mode_valid(struct drm_bridge *bridge,
129012c683e1SLaurent Pinchart 		  const struct drm_display_info *info,
12914647a64fSTomi Valkeinen 		  const struct drm_display_mode *mode)
12927caff0fcSAndrey Gusakov {
12934647a64fSTomi Valkeinen 	struct tc_data *tc = bridge_to_tc(bridge);
129451b9e62eSTomi Valkeinen 	u32 req, avail;
129551b9e62eSTomi Valkeinen 	u32 bits_per_pixel = 24;
129651b9e62eSTomi Valkeinen 
129799fc8e96SAndrey Gusakov 	/* DPI interface clock limitation: upto 154 MHz */
129899fc8e96SAndrey Gusakov 	if (mode->clock > 154000)
129999fc8e96SAndrey Gusakov 		return MODE_CLOCK_HIGH;
130099fc8e96SAndrey Gusakov 
130151b9e62eSTomi Valkeinen 	req = mode->clock * bits_per_pixel / 8;
1302e7dc8d40SThierry Reding 	avail = tc->link.num_lanes * tc->link.rate;
130351b9e62eSTomi Valkeinen 
130451b9e62eSTomi Valkeinen 	if (req > avail)
130551b9e62eSTomi Valkeinen 		return MODE_BAD;
130651b9e62eSTomi Valkeinen 
13077caff0fcSAndrey Gusakov 	return MODE_OK;
13087caff0fcSAndrey Gusakov }
13097caff0fcSAndrey Gusakov 
13107caff0fcSAndrey Gusakov static void tc_bridge_mode_set(struct drm_bridge *bridge,
131163f8f3baSLaurent Pinchart 			       const struct drm_display_mode *mode,
131263f8f3baSLaurent Pinchart 			       const struct drm_display_mode *adj)
13137caff0fcSAndrey Gusakov {
13147caff0fcSAndrey Gusakov 	struct tc_data *tc = bridge_to_tc(bridge);
13157caff0fcSAndrey Gusakov 
1316d008bc33SVille Syrjälä 	drm_mode_copy(&tc->mode, mode);
13177caff0fcSAndrey Gusakov }
13187caff0fcSAndrey Gusakov 
1319731f4badSSam Ravnborg static struct edid *tc_get_edid(struct drm_bridge *bridge,
1320731f4badSSam Ravnborg 				struct drm_connector *connector)
1321731f4badSSam Ravnborg {
1322731f4badSSam Ravnborg 	struct tc_data *tc = bridge_to_tc(bridge);
1323731f4badSSam Ravnborg 
1324731f4badSSam Ravnborg 	return drm_get_edid(connector, &tc->aux.ddc);
1325731f4badSSam Ravnborg }
1326731f4badSSam Ravnborg 
13277caff0fcSAndrey Gusakov static int tc_connector_get_modes(struct drm_connector *connector)
13287caff0fcSAndrey Gusakov {
13297caff0fcSAndrey Gusakov 	struct tc_data *tc = connector_to_tc(connector);
1330731f4badSSam Ravnborg 	int num_modes;
13317caff0fcSAndrey Gusakov 	struct edid *edid;
133232315730STomi Valkeinen 	int ret;
133332315730STomi Valkeinen 
133432315730STomi Valkeinen 	ret = tc_get_display_props(tc);
133532315730STomi Valkeinen 	if (ret < 0) {
133632315730STomi Valkeinen 		dev_err(tc->dev, "failed to read display props: %d\n", ret);
133732315730STomi Valkeinen 		return 0;
133832315730STomi Valkeinen 	}
13397caff0fcSAndrey Gusakov 
1340de5e6c02SSam Ravnborg 	if (tc->panel_bridge) {
1341de5e6c02SSam Ravnborg 		num_modes = drm_bridge_get_modes(tc->panel_bridge, connector);
1342731f4badSSam Ravnborg 		if (num_modes > 0)
1343731f4badSSam Ravnborg 			return num_modes;
1344de5e6c02SSam Ravnborg 	}
13457caff0fcSAndrey Gusakov 
1346731f4badSSam Ravnborg 	edid = tc_get_edid(&tc->bridge, connector);
1347731f4badSSam Ravnborg 	num_modes = drm_add_edid_modes(connector, edid);
1348731f4badSSam Ravnborg 	kfree(edid);
13497caff0fcSAndrey Gusakov 
1350731f4badSSam Ravnborg 	return num_modes;
13517caff0fcSAndrey Gusakov }
13527caff0fcSAndrey Gusakov 
13537caff0fcSAndrey Gusakov static const struct drm_connector_helper_funcs tc_connector_helper_funcs = {
13547caff0fcSAndrey Gusakov 	.get_modes = tc_connector_get_modes,
13557caff0fcSAndrey Gusakov };
13567caff0fcSAndrey Gusakov 
1357136d73a8SSam Ravnborg static enum drm_connector_status tc_bridge_detect(struct drm_bridge *bridge)
1358f25ee501STomi Valkeinen {
1359136d73a8SSam Ravnborg 	struct tc_data *tc = bridge_to_tc(bridge);
1360f25ee501STomi Valkeinen 	bool conn;
1361f25ee501STomi Valkeinen 	u32 val;
1362f25ee501STomi Valkeinen 	int ret;
1363f25ee501STomi Valkeinen 
13646d0c3831SAndrey Smirnov 	ret = regmap_read(tc->regmap, GPIOI, &val);
13656d0c3831SAndrey Smirnov 	if (ret)
13666d0c3831SAndrey Smirnov 		return connector_status_unknown;
1367f25ee501STomi Valkeinen 
1368f25ee501STomi Valkeinen 	conn = val & BIT(tc->hpd_pin);
1369f25ee501STomi Valkeinen 
1370f25ee501STomi Valkeinen 	if (conn)
1371f25ee501STomi Valkeinen 		return connector_status_connected;
1372f25ee501STomi Valkeinen 	else
1373f25ee501STomi Valkeinen 		return connector_status_disconnected;
1374f25ee501STomi Valkeinen }
1375f25ee501STomi Valkeinen 
1376136d73a8SSam Ravnborg static enum drm_connector_status
1377136d73a8SSam Ravnborg tc_connector_detect(struct drm_connector *connector, bool force)
1378136d73a8SSam Ravnborg {
1379136d73a8SSam Ravnborg 	struct tc_data *tc = connector_to_tc(connector);
1380136d73a8SSam Ravnborg 
1381136d73a8SSam Ravnborg 	if (tc->hpd_pin >= 0)
1382136d73a8SSam Ravnborg 		return tc_bridge_detect(&tc->bridge);
1383136d73a8SSam Ravnborg 
1384de5e6c02SSam Ravnborg 	if (tc->panel_bridge)
1385136d73a8SSam Ravnborg 		return connector_status_connected;
1386136d73a8SSam Ravnborg 	else
1387136d73a8SSam Ravnborg 		return connector_status_unknown;
1388136d73a8SSam Ravnborg }
1389136d73a8SSam Ravnborg 
13907caff0fcSAndrey Gusakov static const struct drm_connector_funcs tc_connector_funcs = {
1391f25ee501STomi Valkeinen 	.detect = tc_connector_detect,
13927caff0fcSAndrey Gusakov 	.fill_modes = drm_helper_probe_single_connector_modes,
1393fdd8326aSMarek Vasut 	.destroy = drm_connector_cleanup,
13947caff0fcSAndrey Gusakov 	.reset = drm_atomic_helper_connector_reset,
13957caff0fcSAndrey Gusakov 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
13967caff0fcSAndrey Gusakov 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
13977caff0fcSAndrey Gusakov };
13987caff0fcSAndrey Gusakov 
1399*a219062bSMarek Vasut static int tc_edp_bridge_attach(struct drm_bridge *bridge,
1400a25b988fSLaurent Pinchart 				enum drm_bridge_attach_flags flags)
14017caff0fcSAndrey Gusakov {
14027caff0fcSAndrey Gusakov 	u32 bus_format = MEDIA_BUS_FMT_RGB888_1X24;
14037caff0fcSAndrey Gusakov 	struct tc_data *tc = bridge_to_tc(bridge);
14047caff0fcSAndrey Gusakov 	struct drm_device *drm = bridge->dev;
14057caff0fcSAndrey Gusakov 	int ret;
14067caff0fcSAndrey Gusakov 
1407de5e6c02SSam Ravnborg 	if (tc->panel_bridge) {
1408de5e6c02SSam Ravnborg 		/* If a connector is required then this driver shall create it */
1409de5e6c02SSam Ravnborg 		ret = drm_bridge_attach(tc->bridge.encoder, tc->panel_bridge,
1410de5e6c02SSam Ravnborg 					&tc->bridge, flags | DRM_BRIDGE_ATTACH_NO_CONNECTOR);
1411de5e6c02SSam Ravnborg 		if (ret)
1412de5e6c02SSam Ravnborg 			return ret;
1413a25b988fSLaurent Pinchart 	}
1414a25b988fSLaurent Pinchart 
1415de5e6c02SSam Ravnborg 	if (flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)
1416de5e6c02SSam Ravnborg 		return 0;
1417de5e6c02SSam Ravnborg 
14186cba3fe4SLyude Paul 	tc->aux.drm_dev = drm;
141985ddbe2cSLyude Paul 	ret = drm_dp_aux_register(&tc->aux);
142085ddbe2cSLyude Paul 	if (ret < 0)
142185ddbe2cSLyude Paul 		return ret;
142285ddbe2cSLyude Paul 
1423f25ee501STomi Valkeinen 	/* Create DP/eDP connector */
14247caff0fcSAndrey Gusakov 	drm_connector_helper_add(&tc->connector, &tc_connector_helper_funcs);
1425de5e6c02SSam Ravnborg 	ret = drm_connector_init(drm, &tc->connector, &tc_connector_funcs, tc->bridge.type);
14267caff0fcSAndrey Gusakov 	if (ret)
142785ddbe2cSLyude Paul 		goto aux_unregister;
14287caff0fcSAndrey Gusakov 
1429f25ee501STomi Valkeinen 	/* Don't poll if don't have HPD connected */
1430f25ee501STomi Valkeinen 	if (tc->hpd_pin >= 0) {
1431f25ee501STomi Valkeinen 		if (tc->have_irq)
1432f25ee501STomi Valkeinen 			tc->connector.polled = DRM_CONNECTOR_POLL_HPD;
1433f25ee501STomi Valkeinen 		else
1434f25ee501STomi Valkeinen 			tc->connector.polled = DRM_CONNECTOR_POLL_CONNECT |
1435f25ee501STomi Valkeinen 					       DRM_CONNECTOR_POLL_DISCONNECT;
1436f25ee501STomi Valkeinen 	}
1437f25ee501STomi Valkeinen 
14387caff0fcSAndrey Gusakov 	drm_display_info_set_bus_formats(&tc->connector.display_info,
14397caff0fcSAndrey Gusakov 					 &bus_format, 1);
14404842379cSTomi Valkeinen 	tc->connector.display_info.bus_flags =
14414842379cSTomi Valkeinen 		DRM_BUS_FLAG_DE_HIGH |
144288bc4178SLaurent Pinchart 		DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE |
144388bc4178SLaurent Pinchart 		DRM_BUS_FLAG_SYNC_DRIVE_NEGEDGE;
1444cde4c44dSDaniel Vetter 	drm_connector_attach_encoder(&tc->connector, tc->bridge.encoder);
14457caff0fcSAndrey Gusakov 
14467caff0fcSAndrey Gusakov 	return 0;
144785ddbe2cSLyude Paul aux_unregister:
144885ddbe2cSLyude Paul 	drm_dp_aux_unregister(&tc->aux);
144985ddbe2cSLyude Paul 	return ret;
145085ddbe2cSLyude Paul }
145185ddbe2cSLyude Paul 
1452*a219062bSMarek Vasut static void tc_edp_bridge_detach(struct drm_bridge *bridge)
145385ddbe2cSLyude Paul {
145485ddbe2cSLyude Paul 	drm_dp_aux_unregister(&bridge_to_tc(bridge)->aux);
14557caff0fcSAndrey Gusakov }
14567caff0fcSAndrey Gusakov 
1457*a219062bSMarek Vasut static const struct drm_bridge_funcs tc_edp_bridge_funcs = {
1458*a219062bSMarek Vasut 	.attach = tc_edp_bridge_attach,
1459*a219062bSMarek Vasut 	.detach = tc_edp_bridge_detach,
1460*a219062bSMarek Vasut 	.mode_valid = tc_edp_mode_valid,
14617caff0fcSAndrey Gusakov 	.mode_set = tc_bridge_mode_set,
1462*a219062bSMarek Vasut 	.enable = tc_edp_bridge_enable,
1463*a219062bSMarek Vasut 	.disable = tc_edp_bridge_disable,
14647caff0fcSAndrey Gusakov 	.mode_fixup = tc_bridge_mode_fixup,
1465136d73a8SSam Ravnborg 	.detect = tc_bridge_detect,
1466731f4badSSam Ravnborg 	.get_edid = tc_get_edid,
14677caff0fcSAndrey Gusakov };
14687caff0fcSAndrey Gusakov 
14697caff0fcSAndrey Gusakov static bool tc_readable_reg(struct device *dev, unsigned int reg)
14707caff0fcSAndrey Gusakov {
14717caff0fcSAndrey Gusakov 	return reg != SYSCTRL;
14727caff0fcSAndrey Gusakov }
14737caff0fcSAndrey Gusakov 
14747caff0fcSAndrey Gusakov static const struct regmap_range tc_volatile_ranges[] = {
14757caff0fcSAndrey Gusakov 	regmap_reg_range(DP0_AUXWDATA(0), DP0_AUXSTATUS),
14767caff0fcSAndrey Gusakov 	regmap_reg_range(DP0_LTSTAT, DP0_SNKLTCHGREQ),
14777caff0fcSAndrey Gusakov 	regmap_reg_range(DP_PHY_CTRL, DP_PHY_CTRL),
14787caff0fcSAndrey Gusakov 	regmap_reg_range(DP0_PLLCTRL, PXL_PLLCTRL),
14797caff0fcSAndrey Gusakov 	regmap_reg_range(VFUEN0, VFUEN0),
1480af9526f2STomi Valkeinen 	regmap_reg_range(INTSTS_G, INTSTS_G),
1481af9526f2STomi Valkeinen 	regmap_reg_range(GPIOI, GPIOI),
14827caff0fcSAndrey Gusakov };
14837caff0fcSAndrey Gusakov 
14847caff0fcSAndrey Gusakov static const struct regmap_access_table tc_volatile_table = {
14857caff0fcSAndrey Gusakov 	.yes_ranges = tc_volatile_ranges,
14867caff0fcSAndrey Gusakov 	.n_yes_ranges = ARRAY_SIZE(tc_volatile_ranges),
14877caff0fcSAndrey Gusakov };
14887caff0fcSAndrey Gusakov 
14897caff0fcSAndrey Gusakov static bool tc_writeable_reg(struct device *dev, unsigned int reg)
14907caff0fcSAndrey Gusakov {
14917caff0fcSAndrey Gusakov 	return (reg != TC_IDREG) &&
14927caff0fcSAndrey Gusakov 	       (reg != DP0_LTSTAT) &&
14937caff0fcSAndrey Gusakov 	       (reg != DP0_SNKLTCHGREQ);
14947caff0fcSAndrey Gusakov }
14957caff0fcSAndrey Gusakov 
14967caff0fcSAndrey Gusakov static const struct regmap_config tc_regmap_config = {
14977caff0fcSAndrey Gusakov 	.name = "tc358767",
14987caff0fcSAndrey Gusakov 	.reg_bits = 16,
14997caff0fcSAndrey Gusakov 	.val_bits = 32,
15007caff0fcSAndrey Gusakov 	.reg_stride = 4,
15017caff0fcSAndrey Gusakov 	.max_register = PLL_DBG,
15027caff0fcSAndrey Gusakov 	.cache_type = REGCACHE_RBTREE,
15037caff0fcSAndrey Gusakov 	.readable_reg = tc_readable_reg,
15047caff0fcSAndrey Gusakov 	.volatile_table = &tc_volatile_table,
15057caff0fcSAndrey Gusakov 	.writeable_reg = tc_writeable_reg,
15067caff0fcSAndrey Gusakov 	.reg_format_endian = REGMAP_ENDIAN_BIG,
15077caff0fcSAndrey Gusakov 	.val_format_endian = REGMAP_ENDIAN_LITTLE,
15087caff0fcSAndrey Gusakov };
15097caff0fcSAndrey Gusakov 
1510f25ee501STomi Valkeinen static irqreturn_t tc_irq_handler(int irq, void *arg)
1511f25ee501STomi Valkeinen {
1512f25ee501STomi Valkeinen 	struct tc_data *tc = arg;
1513f25ee501STomi Valkeinen 	u32 val;
1514f25ee501STomi Valkeinen 	int r;
1515f25ee501STomi Valkeinen 
1516f25ee501STomi Valkeinen 	r = regmap_read(tc->regmap, INTSTS_G, &val);
1517f25ee501STomi Valkeinen 	if (r)
1518f25ee501STomi Valkeinen 		return IRQ_NONE;
1519f25ee501STomi Valkeinen 
1520f25ee501STomi Valkeinen 	if (!val)
1521f25ee501STomi Valkeinen 		return IRQ_NONE;
1522f25ee501STomi Valkeinen 
1523f25ee501STomi Valkeinen 	if (val & INT_SYSERR) {
1524f25ee501STomi Valkeinen 		u32 stat = 0;
1525f25ee501STomi Valkeinen 
1526f25ee501STomi Valkeinen 		regmap_read(tc->regmap, SYSSTAT, &stat);
1527f25ee501STomi Valkeinen 
1528f25ee501STomi Valkeinen 		dev_err(tc->dev, "syserr %x\n", stat);
1529f25ee501STomi Valkeinen 	}
1530f25ee501STomi Valkeinen 
1531f25ee501STomi Valkeinen 	if (tc->hpd_pin >= 0 && tc->bridge.dev) {
1532f25ee501STomi Valkeinen 		/*
1533f25ee501STomi Valkeinen 		 * H is triggered when the GPIO goes high.
1534f25ee501STomi Valkeinen 		 *
1535f25ee501STomi Valkeinen 		 * LC is triggered when the GPIO goes low and stays low for
1536f25ee501STomi Valkeinen 		 * the duration of LCNT
1537f25ee501STomi Valkeinen 		 */
1538f25ee501STomi Valkeinen 		bool h = val & INT_GPIO_H(tc->hpd_pin);
1539f25ee501STomi Valkeinen 		bool lc = val & INT_GPIO_LC(tc->hpd_pin);
1540f25ee501STomi Valkeinen 
1541f25ee501STomi Valkeinen 		dev_dbg(tc->dev, "GPIO%d: %s %s\n", tc->hpd_pin,
1542f25ee501STomi Valkeinen 			h ? "H" : "", lc ? "LC" : "");
1543f25ee501STomi Valkeinen 
1544f25ee501STomi Valkeinen 		if (h || lc)
1545f25ee501STomi Valkeinen 			drm_kms_helper_hotplug_event(tc->bridge.dev);
1546f25ee501STomi Valkeinen 	}
1547f25ee501STomi Valkeinen 
1548f25ee501STomi Valkeinen 	regmap_write(tc->regmap, INTSTS_G, val);
1549f25ee501STomi Valkeinen 
1550f25ee501STomi Valkeinen 	return IRQ_HANDLED;
1551f25ee501STomi Valkeinen }
1552f25ee501STomi Valkeinen 
15537caff0fcSAndrey Gusakov static int tc_probe(struct i2c_client *client, const struct i2c_device_id *id)
15547caff0fcSAndrey Gusakov {
15557caff0fcSAndrey Gusakov 	struct device *dev = &client->dev;
1556de5e6c02SSam Ravnborg 	struct drm_panel *panel;
15577caff0fcSAndrey Gusakov 	struct tc_data *tc;
15587caff0fcSAndrey Gusakov 	int ret;
15597caff0fcSAndrey Gusakov 
15607caff0fcSAndrey Gusakov 	tc = devm_kzalloc(dev, sizeof(*tc), GFP_KERNEL);
15617caff0fcSAndrey Gusakov 	if (!tc)
15627caff0fcSAndrey Gusakov 		return -ENOMEM;
15637caff0fcSAndrey Gusakov 
15647caff0fcSAndrey Gusakov 	tc->dev = dev;
15657caff0fcSAndrey Gusakov 
15667caff0fcSAndrey Gusakov 	/* port@2 is the output port */
1567de5e6c02SSam Ravnborg 	ret = drm_of_find_panel_or_bridge(dev->of_node, 2, 0, &panel, NULL);
1568d630213fSLucas Stach 	if (ret && ret != -ENODEV)
1569ebc94461SRob Herring 		return ret;
15707caff0fcSAndrey Gusakov 
1571de5e6c02SSam Ravnborg 	if (panel) {
1572de5e6c02SSam Ravnborg 		struct drm_bridge *panel_bridge;
1573de5e6c02SSam Ravnborg 
1574de5e6c02SSam Ravnborg 		panel_bridge = devm_drm_panel_bridge_add(dev, panel);
1575de5e6c02SSam Ravnborg 		if (IS_ERR(panel_bridge))
1576de5e6c02SSam Ravnborg 			return PTR_ERR(panel_bridge);
1577de5e6c02SSam Ravnborg 
1578de5e6c02SSam Ravnborg 		tc->panel_bridge = panel_bridge;
1579de5e6c02SSam Ravnborg 		tc->bridge.type = DRM_MODE_CONNECTOR_eDP;
1580de5e6c02SSam Ravnborg 	} else {
1581de5e6c02SSam Ravnborg 		tc->bridge.type = DRM_MODE_CONNECTOR_DisplayPort;
1582de5e6c02SSam Ravnborg 	}
1583de5e6c02SSam Ravnborg 
15847caff0fcSAndrey Gusakov 	/* Shut down GPIO is optional */
15857caff0fcSAndrey Gusakov 	tc->sd_gpio = devm_gpiod_get_optional(dev, "shutdown", GPIOD_OUT_HIGH);
15867caff0fcSAndrey Gusakov 	if (IS_ERR(tc->sd_gpio))
15877caff0fcSAndrey Gusakov 		return PTR_ERR(tc->sd_gpio);
15887caff0fcSAndrey Gusakov 
15897caff0fcSAndrey Gusakov 	if (tc->sd_gpio) {
15907caff0fcSAndrey Gusakov 		gpiod_set_value_cansleep(tc->sd_gpio, 0);
15917caff0fcSAndrey Gusakov 		usleep_range(5000, 10000);
15927caff0fcSAndrey Gusakov 	}
15937caff0fcSAndrey Gusakov 
15947caff0fcSAndrey Gusakov 	/* Reset GPIO is optional */
15957caff0fcSAndrey Gusakov 	tc->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
15967caff0fcSAndrey Gusakov 	if (IS_ERR(tc->reset_gpio))
15977caff0fcSAndrey Gusakov 		return PTR_ERR(tc->reset_gpio);
15987caff0fcSAndrey Gusakov 
15997caff0fcSAndrey Gusakov 	if (tc->reset_gpio) {
16007caff0fcSAndrey Gusakov 		gpiod_set_value_cansleep(tc->reset_gpio, 1);
16017caff0fcSAndrey Gusakov 		usleep_range(5000, 10000);
16027caff0fcSAndrey Gusakov 	}
16037caff0fcSAndrey Gusakov 
16047caff0fcSAndrey Gusakov 	tc->refclk = devm_clk_get(dev, "ref");
16057caff0fcSAndrey Gusakov 	if (IS_ERR(tc->refclk)) {
16067caff0fcSAndrey Gusakov 		ret = PTR_ERR(tc->refclk);
16077caff0fcSAndrey Gusakov 		dev_err(dev, "Failed to get refclk: %d\n", ret);
16087caff0fcSAndrey Gusakov 		return ret;
16097caff0fcSAndrey Gusakov 	}
16107caff0fcSAndrey Gusakov 
16117caff0fcSAndrey Gusakov 	tc->regmap = devm_regmap_init_i2c(client, &tc_regmap_config);
16127caff0fcSAndrey Gusakov 	if (IS_ERR(tc->regmap)) {
16137caff0fcSAndrey Gusakov 		ret = PTR_ERR(tc->regmap);
16147caff0fcSAndrey Gusakov 		dev_err(dev, "Failed to initialize regmap: %d\n", ret);
16157caff0fcSAndrey Gusakov 		return ret;
16167caff0fcSAndrey Gusakov 	}
16177caff0fcSAndrey Gusakov 
1618f25ee501STomi Valkeinen 	ret = of_property_read_u32(dev->of_node, "toshiba,hpd-pin",
1619f25ee501STomi Valkeinen 				   &tc->hpd_pin);
1620f25ee501STomi Valkeinen 	if (ret) {
1621f25ee501STomi Valkeinen 		tc->hpd_pin = -ENODEV;
1622f25ee501STomi Valkeinen 	} else {
1623f25ee501STomi Valkeinen 		if (tc->hpd_pin < 0 || tc->hpd_pin > 1) {
1624f25ee501STomi Valkeinen 			dev_err(dev, "failed to parse HPD number\n");
1625f25ee501STomi Valkeinen 			return ret;
1626f25ee501STomi Valkeinen 		}
1627f25ee501STomi Valkeinen 	}
1628f25ee501STomi Valkeinen 
1629f25ee501STomi Valkeinen 	if (client->irq > 0) {
1630f25ee501STomi Valkeinen 		/* enable SysErr */
1631f25ee501STomi Valkeinen 		regmap_write(tc->regmap, INTCTL_G, INT_SYSERR);
1632f25ee501STomi Valkeinen 
1633f25ee501STomi Valkeinen 		ret = devm_request_threaded_irq(dev, client->irq,
1634f25ee501STomi Valkeinen 						NULL, tc_irq_handler,
1635f25ee501STomi Valkeinen 						IRQF_ONESHOT,
1636f25ee501STomi Valkeinen 						"tc358767-irq", tc);
1637f25ee501STomi Valkeinen 		if (ret) {
1638f25ee501STomi Valkeinen 			dev_err(dev, "failed to register dp interrupt\n");
1639f25ee501STomi Valkeinen 			return ret;
1640f25ee501STomi Valkeinen 		}
1641f25ee501STomi Valkeinen 
1642f25ee501STomi Valkeinen 		tc->have_irq = true;
1643f25ee501STomi Valkeinen 	}
1644f25ee501STomi Valkeinen 
16457caff0fcSAndrey Gusakov 	ret = regmap_read(tc->regmap, TC_IDREG, &tc->rev);
16467caff0fcSAndrey Gusakov 	if (ret) {
16477caff0fcSAndrey Gusakov 		dev_err(tc->dev, "can not read device ID: %d\n", ret);
16487caff0fcSAndrey Gusakov 		return ret;
16497caff0fcSAndrey Gusakov 	}
16507caff0fcSAndrey Gusakov 
16517caff0fcSAndrey Gusakov 	if ((tc->rev != 0x6601) && (tc->rev != 0x6603)) {
16527caff0fcSAndrey Gusakov 		dev_err(tc->dev, "invalid device ID: 0x%08x\n", tc->rev);
16537caff0fcSAndrey Gusakov 		return -EINVAL;
16547caff0fcSAndrey Gusakov 	}
16557caff0fcSAndrey Gusakov 
16567caff0fcSAndrey Gusakov 	tc->assr = (tc->rev == 0x6601); /* Enable ASSR for eDP panels */
16577caff0fcSAndrey Gusakov 
165852c2197aSLucas Stach 	if (!tc->reset_gpio) {
165952c2197aSLucas Stach 		/*
166052c2197aSLucas Stach 		 * If the reset pin isn't present, do a software reset. It isn't
166152c2197aSLucas Stach 		 * as thorough as the hardware reset, as we can't reset the I2C
166252c2197aSLucas Stach 		 * communication block for obvious reasons, but it's getting the
166352c2197aSLucas Stach 		 * chip into a defined state.
166452c2197aSLucas Stach 		 */
166552c2197aSLucas Stach 		regmap_update_bits(tc->regmap, SYSRSTENB,
166652c2197aSLucas Stach 				ENBLCD0 | ENBBM | ENBDSIRX | ENBREG | ENBHDCP,
166752c2197aSLucas Stach 				0);
166852c2197aSLucas Stach 		regmap_update_bits(tc->regmap, SYSRSTENB,
166952c2197aSLucas Stach 				ENBLCD0 | ENBBM | ENBDSIRX | ENBREG | ENBHDCP,
167052c2197aSLucas Stach 				ENBLCD0 | ENBBM | ENBDSIRX | ENBREG | ENBHDCP);
167152c2197aSLucas Stach 		usleep_range(5000, 10000);
167252c2197aSLucas Stach 	}
167352c2197aSLucas Stach 
1674f25ee501STomi Valkeinen 	if (tc->hpd_pin >= 0) {
1675f25ee501STomi Valkeinen 		u32 lcnt_reg = tc->hpd_pin == 0 ? INT_GP0_LCNT : INT_GP1_LCNT;
1676f25ee501STomi Valkeinen 		u32 h_lc = INT_GPIO_H(tc->hpd_pin) | INT_GPIO_LC(tc->hpd_pin);
1677f25ee501STomi Valkeinen 
1678f25ee501STomi Valkeinen 		/* Set LCNT to 2ms */
1679f25ee501STomi Valkeinen 		regmap_write(tc->regmap, lcnt_reg,
1680f25ee501STomi Valkeinen 			     clk_get_rate(tc->refclk) * 2 / 1000);
1681f25ee501STomi Valkeinen 		/* We need the "alternate" mode for HPD */
1682f25ee501STomi Valkeinen 		regmap_write(tc->regmap, GPIOM, BIT(tc->hpd_pin));
1683f25ee501STomi Valkeinen 
1684f25ee501STomi Valkeinen 		if (tc->have_irq) {
1685f25ee501STomi Valkeinen 			/* enable H & LC */
1686f25ee501STomi Valkeinen 			regmap_update_bits(tc->regmap, INTCTL_G, h_lc, h_lc);
1687f25ee501STomi Valkeinen 		}
1688f25ee501STomi Valkeinen 	}
1689f25ee501STomi Valkeinen 
16907caff0fcSAndrey Gusakov 	ret = tc_aux_link_setup(tc);
16917caff0fcSAndrey Gusakov 	if (ret)
16927caff0fcSAndrey Gusakov 		return ret;
16937caff0fcSAndrey Gusakov 
16947caff0fcSAndrey Gusakov 	/* Register DP AUX channel */
16957caff0fcSAndrey Gusakov 	tc->aux.name = "TC358767 AUX i2c adapter";
16967caff0fcSAndrey Gusakov 	tc->aux.dev = tc->dev;
16977caff0fcSAndrey Gusakov 	tc->aux.transfer = tc_aux_transfer;
169885ddbe2cSLyude Paul 	drm_dp_aux_init(&tc->aux);
16997caff0fcSAndrey Gusakov 
1700*a219062bSMarek Vasut 	tc->bridge.funcs = &tc_edp_bridge_funcs;
1701136d73a8SSam Ravnborg 	if (tc->hpd_pin >= 0)
1702136d73a8SSam Ravnborg 		tc->bridge.ops |= DRM_BRIDGE_OP_DETECT;
1703731f4badSSam Ravnborg 	tc->bridge.ops |= DRM_BRIDGE_OP_EDID;
1704136d73a8SSam Ravnborg 
17057caff0fcSAndrey Gusakov 	tc->bridge.of_node = dev->of_node;
1706dc01732eSInki Dae 	drm_bridge_add(&tc->bridge);
17077caff0fcSAndrey Gusakov 
17087caff0fcSAndrey Gusakov 	i2c_set_clientdata(client, tc);
17097caff0fcSAndrey Gusakov 
17107caff0fcSAndrey Gusakov 	return 0;
17117caff0fcSAndrey Gusakov }
17127caff0fcSAndrey Gusakov 
17137caff0fcSAndrey Gusakov static int tc_remove(struct i2c_client *client)
17147caff0fcSAndrey Gusakov {
17157caff0fcSAndrey Gusakov 	struct tc_data *tc = i2c_get_clientdata(client);
17167caff0fcSAndrey Gusakov 
17177caff0fcSAndrey Gusakov 	drm_bridge_remove(&tc->bridge);
17187caff0fcSAndrey Gusakov 
17197caff0fcSAndrey Gusakov 	return 0;
17207caff0fcSAndrey Gusakov }
17217caff0fcSAndrey Gusakov 
17227caff0fcSAndrey Gusakov static const struct i2c_device_id tc358767_i2c_ids[] = {
17237caff0fcSAndrey Gusakov 	{ "tc358767", 0 },
17247caff0fcSAndrey Gusakov 	{ }
17257caff0fcSAndrey Gusakov };
17267caff0fcSAndrey Gusakov MODULE_DEVICE_TABLE(i2c, tc358767_i2c_ids);
17277caff0fcSAndrey Gusakov 
17287caff0fcSAndrey Gusakov static const struct of_device_id tc358767_of_ids[] = {
17297caff0fcSAndrey Gusakov 	{ .compatible = "toshiba,tc358767", },
17307caff0fcSAndrey Gusakov 	{ }
17317caff0fcSAndrey Gusakov };
17327caff0fcSAndrey Gusakov MODULE_DEVICE_TABLE(of, tc358767_of_ids);
17337caff0fcSAndrey Gusakov 
17347caff0fcSAndrey Gusakov static struct i2c_driver tc358767_driver = {
17357caff0fcSAndrey Gusakov 	.driver = {
17367caff0fcSAndrey Gusakov 		.name = "tc358767",
17377caff0fcSAndrey Gusakov 		.of_match_table = tc358767_of_ids,
17387caff0fcSAndrey Gusakov 	},
17397caff0fcSAndrey Gusakov 	.id_table = tc358767_i2c_ids,
17407caff0fcSAndrey Gusakov 	.probe = tc_probe,
17417caff0fcSAndrey Gusakov 	.remove	= tc_remove,
17427caff0fcSAndrey Gusakov };
17437caff0fcSAndrey Gusakov module_i2c_driver(tc358767_driver);
17447caff0fcSAndrey Gusakov 
17457caff0fcSAndrey Gusakov MODULE_AUTHOR("Andrey Gusakov <andrey.gusakov@cogentembedded.com>");
17467caff0fcSAndrey Gusakov MODULE_DESCRIPTION("tc358767 eDP encoder driver");
17477caff0fcSAndrey Gusakov MODULE_LICENSE("GPL");
1748