xref: /openbmc/linux/drivers/gpu/drm/bridge/tc358767.c (revision e0655feaec62d5139b6b13a7b1bbb1ab8f1c2d83)
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>
297caff0fcSAndrey Gusakov #include <drm/drm_dp_helper.h>
307caff0fcSAndrey Gusakov #include <drm/drm_edid.h>
317caff0fcSAndrey Gusakov #include <drm/drm_of.h>
327caff0fcSAndrey Gusakov #include <drm/drm_panel.h>
33fcd70cd3SDaniel Vetter #include <drm/drm_probe_helper.h>
347caff0fcSAndrey Gusakov 
357caff0fcSAndrey Gusakov /* Registers */
367caff0fcSAndrey Gusakov 
377caff0fcSAndrey Gusakov /* Display Parallel Interface */
387caff0fcSAndrey Gusakov #define DPIPXLFMT		0x0440
397caff0fcSAndrey Gusakov #define VS_POL_ACTIVE_LOW		(1 << 10)
407caff0fcSAndrey Gusakov #define HS_POL_ACTIVE_LOW		(1 << 9)
417caff0fcSAndrey Gusakov #define DE_POL_ACTIVE_HIGH		(0 << 8)
427caff0fcSAndrey Gusakov #define SUB_CFG_TYPE_CONFIG1		(0 << 2) /* LSB aligned */
437caff0fcSAndrey Gusakov #define SUB_CFG_TYPE_CONFIG2		(1 << 2) /* Loosely Packed */
447caff0fcSAndrey Gusakov #define SUB_CFG_TYPE_CONFIG3		(2 << 2) /* LSB aligned 8-bit */
457caff0fcSAndrey Gusakov #define DPI_BPP_RGB888			(0 << 0)
467caff0fcSAndrey Gusakov #define DPI_BPP_RGB666			(1 << 0)
477caff0fcSAndrey Gusakov #define DPI_BPP_RGB565			(2 << 0)
487caff0fcSAndrey Gusakov 
497caff0fcSAndrey Gusakov /* Video Path */
507caff0fcSAndrey Gusakov #define VPCTRL0			0x0450
513f072c30SAndrey Smirnov #define VSDELAY			GENMASK(31, 20)
527caff0fcSAndrey Gusakov #define OPXLFMT_RGB666			(0 << 8)
537caff0fcSAndrey Gusakov #define OPXLFMT_RGB888			(1 << 8)
547caff0fcSAndrey Gusakov #define FRMSYNC_DISABLED		(0 << 4) /* Video Timing Gen Disabled */
557caff0fcSAndrey Gusakov #define FRMSYNC_ENABLED			(1 << 4) /* Video Timing Gen Enabled */
567caff0fcSAndrey Gusakov #define MSF_DISABLED			(0 << 0) /* Magic Square FRC disabled */
577caff0fcSAndrey Gusakov #define MSF_ENABLED			(1 << 0) /* Magic Square FRC enabled */
587caff0fcSAndrey Gusakov #define HTIM01			0x0454
593f072c30SAndrey Smirnov #define HPW			GENMASK(8, 0)
603f072c30SAndrey Smirnov #define HBPR			GENMASK(24, 16)
617caff0fcSAndrey Gusakov #define HTIM02			0x0458
623f072c30SAndrey Smirnov #define HDISPR			GENMASK(10, 0)
633f072c30SAndrey Smirnov #define HFPR			GENMASK(24, 16)
647caff0fcSAndrey Gusakov #define VTIM01			0x045c
653f072c30SAndrey Smirnov #define VSPR			GENMASK(7, 0)
663f072c30SAndrey Smirnov #define VBPR			GENMASK(23, 16)
677caff0fcSAndrey Gusakov #define VTIM02			0x0460
683f072c30SAndrey Smirnov #define VFPR			GENMASK(23, 16)
693f072c30SAndrey Smirnov #define VDISPR			GENMASK(10, 0)
707caff0fcSAndrey Gusakov #define VFUEN0			0x0464
717caff0fcSAndrey Gusakov #define VFUEN				BIT(0)   /* Video Frame Timing Upload */
727caff0fcSAndrey Gusakov 
737caff0fcSAndrey Gusakov /* System */
747caff0fcSAndrey Gusakov #define TC_IDREG		0x0500
75f25ee501STomi Valkeinen #define SYSSTAT			0x0508
767caff0fcSAndrey Gusakov #define SYSCTRL			0x0510
777caff0fcSAndrey Gusakov #define DP0_AUDSRC_NO_INPUT		(0 << 3)
787caff0fcSAndrey Gusakov #define DP0_AUDSRC_I2S_RX		(1 << 3)
797caff0fcSAndrey Gusakov #define DP0_VIDSRC_NO_INPUT		(0 << 0)
807caff0fcSAndrey Gusakov #define DP0_VIDSRC_DSI_RX		(1 << 0)
817caff0fcSAndrey Gusakov #define DP0_VIDSRC_DPI_RX		(2 << 0)
827caff0fcSAndrey Gusakov #define DP0_VIDSRC_COLOR_BAR		(3 << 0)
83af9526f2STomi Valkeinen #define GPIOM			0x0540
84f25ee501STomi Valkeinen #define GPIOC			0x0544
85f25ee501STomi Valkeinen #define GPIOO			0x0548
86af9526f2STomi Valkeinen #define GPIOI			0x054c
87af9526f2STomi Valkeinen #define INTCTL_G		0x0560
88af9526f2STomi Valkeinen #define INTSTS_G		0x0564
89f25ee501STomi Valkeinen 
90f25ee501STomi Valkeinen #define INT_SYSERR		BIT(16)
91f25ee501STomi Valkeinen #define INT_GPIO_H(x)		(1 << (x == 0 ? 2 : 10))
92f25ee501STomi Valkeinen #define INT_GPIO_LC(x)		(1 << (x == 0 ? 3 : 11))
93f25ee501STomi Valkeinen 
94af9526f2STomi Valkeinen #define INT_GP0_LCNT		0x0584
95af9526f2STomi Valkeinen #define INT_GP1_LCNT		0x0588
967caff0fcSAndrey Gusakov 
977caff0fcSAndrey Gusakov /* Control */
987caff0fcSAndrey Gusakov #define DP0CTL			0x0600
997caff0fcSAndrey Gusakov #define VID_MN_GEN			BIT(6)   /* Auto-generate M/N values */
1007caff0fcSAndrey Gusakov #define EF_EN				BIT(5)   /* Enable Enhanced Framing */
1017caff0fcSAndrey Gusakov #define VID_EN				BIT(1)   /* Video transmission enable */
1027caff0fcSAndrey Gusakov #define DP_EN				BIT(0)   /* Enable DPTX function */
1037caff0fcSAndrey Gusakov 
1047caff0fcSAndrey Gusakov /* Clocks */
1057caff0fcSAndrey Gusakov #define DP0_VIDMNGEN0		0x0610
1067caff0fcSAndrey Gusakov #define DP0_VIDMNGEN1		0x0614
1077caff0fcSAndrey Gusakov #define DP0_VMNGENSTATUS	0x0618
1087caff0fcSAndrey Gusakov 
1097caff0fcSAndrey Gusakov /* Main Channel */
1107caff0fcSAndrey Gusakov #define DP0_SECSAMPLE		0x0640
1117caff0fcSAndrey Gusakov #define DP0_VIDSYNCDELAY	0x0644
1123f072c30SAndrey Smirnov #define VID_SYNC_DLY		GENMASK(15, 0)
1133f072c30SAndrey Smirnov #define THRESH_DLY		GENMASK(31, 16)
1143f072c30SAndrey Smirnov 
1157caff0fcSAndrey Gusakov #define DP0_TOTALVAL		0x0648
1163f072c30SAndrey Smirnov #define H_TOTAL			GENMASK(15, 0)
1173f072c30SAndrey Smirnov #define V_TOTAL			GENMASK(31, 16)
1187caff0fcSAndrey Gusakov #define DP0_STARTVAL		0x064c
1193f072c30SAndrey Smirnov #define H_START			GENMASK(15, 0)
1203f072c30SAndrey Smirnov #define V_START			GENMASK(31, 16)
1217caff0fcSAndrey Gusakov #define DP0_ACTIVEVAL		0x0650
1223f072c30SAndrey Smirnov #define H_ACT			GENMASK(15, 0)
1233f072c30SAndrey Smirnov #define V_ACT			GENMASK(31, 16)
1243f072c30SAndrey Smirnov 
1257caff0fcSAndrey Gusakov #define DP0_SYNCVAL		0x0654
1263f072c30SAndrey Smirnov #define VS_WIDTH		GENMASK(30, 16)
1273f072c30SAndrey Smirnov #define HS_WIDTH		GENMASK(14, 0)
1287923e09cSTomi Valkeinen #define SYNCVAL_HS_POL_ACTIVE_LOW	(1 << 15)
1297923e09cSTomi Valkeinen #define SYNCVAL_VS_POL_ACTIVE_LOW	(1 << 31)
1307caff0fcSAndrey Gusakov #define DP0_MISC		0x0658
131f3b8adbeSAndrey Gusakov #define TU_SIZE_RECOMMENDED		(63) /* LSCLK cycles per TU */
1323f072c30SAndrey Smirnov #define MAX_TU_SYMBOL		GENMASK(28, 23)
1333f072c30SAndrey Smirnov #define TU_SIZE			GENMASK(21, 16)
1347caff0fcSAndrey Gusakov #define BPC_6				(0 << 5)
1357caff0fcSAndrey Gusakov #define BPC_8				(1 << 5)
1367caff0fcSAndrey Gusakov 
1377caff0fcSAndrey Gusakov /* AUX channel */
1387caff0fcSAndrey Gusakov #define DP0_AUXCFG0		0x0660
1397caff0fcSAndrey Gusakov #define DP0_AUXCFG1		0x0664
1407caff0fcSAndrey Gusakov #define AUX_RX_FILTER_EN		BIT(16)
1417caff0fcSAndrey Gusakov 
1427caff0fcSAndrey Gusakov #define DP0_AUXADDR		0x0668
1437caff0fcSAndrey Gusakov #define DP0_AUXWDATA(i)		(0x066c + (i) * 4)
1447caff0fcSAndrey Gusakov #define DP0_AUXRDATA(i)		(0x067c + (i) * 4)
1457caff0fcSAndrey Gusakov #define DP0_AUXSTATUS		0x068c
1467caff0fcSAndrey Gusakov #define AUX_STATUS_MASK			0xf0
1477caff0fcSAndrey Gusakov #define AUX_STATUS_SHIFT		4
1487caff0fcSAndrey Gusakov #define AUX_TIMEOUT			BIT(1)
1497caff0fcSAndrey Gusakov #define AUX_BUSY			BIT(0)
1507caff0fcSAndrey Gusakov #define DP0_AUXI2CADR		0x0698
1517caff0fcSAndrey Gusakov 
1527caff0fcSAndrey Gusakov /* Link Training */
1537caff0fcSAndrey Gusakov #define DP0_SRCCTRL		0x06a0
1547caff0fcSAndrey Gusakov #define DP0_SRCCTRL_SCRMBLDIS		BIT(13)
1557caff0fcSAndrey Gusakov #define DP0_SRCCTRL_EN810B		BIT(12)
1567caff0fcSAndrey Gusakov #define DP0_SRCCTRL_NOTP		(0 << 8)
1577caff0fcSAndrey Gusakov #define DP0_SRCCTRL_TP1			(1 << 8)
1587caff0fcSAndrey Gusakov #define DP0_SRCCTRL_TP2			(2 << 8)
1597caff0fcSAndrey Gusakov #define DP0_SRCCTRL_LANESKEW		BIT(7)
1607caff0fcSAndrey Gusakov #define DP0_SRCCTRL_SSCG		BIT(3)
1617caff0fcSAndrey Gusakov #define DP0_SRCCTRL_LANES_1		(0 << 2)
1627caff0fcSAndrey Gusakov #define DP0_SRCCTRL_LANES_2		(1 << 2)
1637caff0fcSAndrey Gusakov #define DP0_SRCCTRL_BW27		(1 << 1)
1647caff0fcSAndrey Gusakov #define DP0_SRCCTRL_BW162		(0 << 1)
1657caff0fcSAndrey Gusakov #define DP0_SRCCTRL_AUTOCORRECT		BIT(0)
1667caff0fcSAndrey Gusakov #define DP0_LTSTAT		0x06d0
1677caff0fcSAndrey Gusakov #define LT_LOOPDONE			BIT(13)
1687caff0fcSAndrey Gusakov #define LT_STATUS_MASK			(0x1f << 8)
1697caff0fcSAndrey Gusakov #define LT_CHANNEL1_EQ_BITS		(DP_CHANNEL_EQ_BITS << 4)
1707caff0fcSAndrey Gusakov #define LT_INTERLANE_ALIGN_DONE		BIT(3)
1717caff0fcSAndrey Gusakov #define LT_CHANNEL0_EQ_BITS		(DP_CHANNEL_EQ_BITS)
1727caff0fcSAndrey Gusakov #define DP0_SNKLTCHGREQ		0x06d4
1737caff0fcSAndrey Gusakov #define DP0_LTLOOPCTRL		0x06d8
1747caff0fcSAndrey Gusakov #define DP0_SNKLTCTRL		0x06e4
1757caff0fcSAndrey Gusakov 
176adf41098STomi Valkeinen #define DP1_SRCCTRL		0x07a0
177adf41098STomi Valkeinen 
1787caff0fcSAndrey Gusakov /* PHY */
1797caff0fcSAndrey Gusakov #define DP_PHY_CTRL		0x0800
1807caff0fcSAndrey Gusakov #define DP_PHY_RST			BIT(28)  /* DP PHY Global Soft Reset */
1817caff0fcSAndrey Gusakov #define BGREN				BIT(25)  /* AUX PHY BGR Enable */
1827caff0fcSAndrey Gusakov #define PWR_SW_EN			BIT(24)  /* PHY Power Switch Enable */
1837caff0fcSAndrey Gusakov #define PHY_M1_RST			BIT(12)  /* Reset PHY1 Main Channel */
1847caff0fcSAndrey Gusakov #define PHY_RDY				BIT(16)  /* PHY Main Channels Ready */
1857caff0fcSAndrey Gusakov #define PHY_M0_RST			BIT(8)   /* Reset PHY0 Main Channel */
186adf41098STomi Valkeinen #define PHY_2LANE			BIT(2)   /* PHY Enable 2 lanes */
1877caff0fcSAndrey Gusakov #define PHY_A0_EN			BIT(1)   /* PHY Aux Channel0 Enable */
1887caff0fcSAndrey Gusakov #define PHY_M0_EN			BIT(0)   /* PHY Main Channel0 Enable */
1897caff0fcSAndrey Gusakov 
1907caff0fcSAndrey Gusakov /* PLL */
1917caff0fcSAndrey Gusakov #define DP0_PLLCTRL		0x0900
1927caff0fcSAndrey Gusakov #define DP1_PLLCTRL		0x0904	/* not defined in DS */
1937caff0fcSAndrey Gusakov #define PXL_PLLCTRL		0x0908
1947caff0fcSAndrey Gusakov #define PLLUPDATE			BIT(2)
1957caff0fcSAndrey Gusakov #define PLLBYP				BIT(1)
1967caff0fcSAndrey Gusakov #define PLLEN				BIT(0)
1977caff0fcSAndrey Gusakov #define PXL_PLLPARAM		0x0914
1987caff0fcSAndrey Gusakov #define IN_SEL_REFCLK			(0 << 14)
1997caff0fcSAndrey Gusakov #define SYS_PLLPARAM		0x0918
2007caff0fcSAndrey Gusakov #define REF_FREQ_38M4			(0 << 8) /* 38.4 MHz */
2017caff0fcSAndrey Gusakov #define REF_FREQ_19M2			(1 << 8) /* 19.2 MHz */
2027caff0fcSAndrey Gusakov #define REF_FREQ_26M			(2 << 8) /* 26 MHz */
2037caff0fcSAndrey Gusakov #define REF_FREQ_13M			(3 << 8) /* 13 MHz */
2047caff0fcSAndrey Gusakov #define SYSCLK_SEL_LSCLK		(0 << 4)
2057caff0fcSAndrey Gusakov #define LSCLK_DIV_1			(0 << 0)
2067caff0fcSAndrey Gusakov #define LSCLK_DIV_2			(1 << 0)
2077caff0fcSAndrey Gusakov 
2087caff0fcSAndrey Gusakov /* Test & Debug */
2097caff0fcSAndrey Gusakov #define TSTCTL			0x0a00
2103f072c30SAndrey Smirnov #define COLOR_R			GENMASK(31, 24)
2113f072c30SAndrey Smirnov #define COLOR_G			GENMASK(23, 16)
2123f072c30SAndrey Smirnov #define COLOR_B			GENMASK(15, 8)
2133f072c30SAndrey Smirnov #define ENI2CFILTER		BIT(4)
2143f072c30SAndrey Smirnov #define COLOR_BAR_MODE		GENMASK(1, 0)
2153f072c30SAndrey Smirnov #define COLOR_BAR_MODE_BARS	2
2167caff0fcSAndrey Gusakov #define PLL_DBG			0x0a04
2177caff0fcSAndrey Gusakov 
2187caff0fcSAndrey Gusakov static bool tc_test_pattern;
2197caff0fcSAndrey Gusakov module_param_named(test, tc_test_pattern, bool, 0644);
2207caff0fcSAndrey Gusakov 
2217caff0fcSAndrey Gusakov struct tc_edp_link {
2227caff0fcSAndrey Gusakov 	struct drm_dp_link	base;
2237caff0fcSAndrey Gusakov 	u8			assr;
224e5607637STomi Valkeinen 	bool			scrambler_dis;
225e5607637STomi Valkeinen 	bool			spread;
2267caff0fcSAndrey Gusakov };
2277caff0fcSAndrey Gusakov 
2287caff0fcSAndrey Gusakov struct tc_data {
2297caff0fcSAndrey Gusakov 	struct device		*dev;
2307caff0fcSAndrey Gusakov 	struct regmap		*regmap;
2317caff0fcSAndrey Gusakov 	struct drm_dp_aux	aux;
2327caff0fcSAndrey Gusakov 
2337caff0fcSAndrey Gusakov 	struct drm_bridge	bridge;
2347caff0fcSAndrey Gusakov 	struct drm_connector	connector;
2357caff0fcSAndrey Gusakov 	struct drm_panel	*panel;
2367caff0fcSAndrey Gusakov 
2377caff0fcSAndrey Gusakov 	/* link settings */
2387caff0fcSAndrey Gusakov 	struct tc_edp_link	link;
2397caff0fcSAndrey Gusakov 
2407caff0fcSAndrey Gusakov 	/* display edid */
2417caff0fcSAndrey Gusakov 	struct edid		*edid;
2427caff0fcSAndrey Gusakov 	/* current mode */
24346648a3cSTomi Valkeinen 	struct drm_display_mode	mode;
2447caff0fcSAndrey Gusakov 
2457caff0fcSAndrey Gusakov 	u32			rev;
2467caff0fcSAndrey Gusakov 	u8			assr;
2477caff0fcSAndrey Gusakov 
2487caff0fcSAndrey Gusakov 	struct gpio_desc	*sd_gpio;
2497caff0fcSAndrey Gusakov 	struct gpio_desc	*reset_gpio;
2507caff0fcSAndrey Gusakov 	struct clk		*refclk;
251f25ee501STomi Valkeinen 
252f25ee501STomi Valkeinen 	/* do we have IRQ */
253f25ee501STomi Valkeinen 	bool			have_irq;
254f25ee501STomi Valkeinen 
255f25ee501STomi Valkeinen 	/* HPD pin number (0 or 1) or -ENODEV */
256f25ee501STomi Valkeinen 	int			hpd_pin;
2577caff0fcSAndrey Gusakov };
2587caff0fcSAndrey Gusakov 
2597caff0fcSAndrey Gusakov static inline struct tc_data *aux_to_tc(struct drm_dp_aux *a)
2607caff0fcSAndrey Gusakov {
2617caff0fcSAndrey Gusakov 	return container_of(a, struct tc_data, aux);
2627caff0fcSAndrey Gusakov }
2637caff0fcSAndrey Gusakov 
2647caff0fcSAndrey Gusakov static inline struct tc_data *bridge_to_tc(struct drm_bridge *b)
2657caff0fcSAndrey Gusakov {
2667caff0fcSAndrey Gusakov 	return container_of(b, struct tc_data, bridge);
2677caff0fcSAndrey Gusakov }
2687caff0fcSAndrey Gusakov 
2697caff0fcSAndrey Gusakov static inline struct tc_data *connector_to_tc(struct drm_connector *c)
2707caff0fcSAndrey Gusakov {
2717caff0fcSAndrey Gusakov 	return container_of(c, struct tc_data, connector);
2727caff0fcSAndrey Gusakov }
2737caff0fcSAndrey Gusakov 
27493a10569SAndrey Smirnov static inline int tc_poll_timeout(struct tc_data *tc, unsigned int addr,
2757caff0fcSAndrey Gusakov 				  unsigned int cond_mask,
2767caff0fcSAndrey Gusakov 				  unsigned int cond_value,
2777caff0fcSAndrey Gusakov 				  unsigned long sleep_us, u64 timeout_us)
2787caff0fcSAndrey Gusakov {
2797caff0fcSAndrey Gusakov 	unsigned int val;
2807caff0fcSAndrey Gusakov 
28193a10569SAndrey Smirnov 	return regmap_read_poll_timeout(tc->regmap, addr, val,
28293a10569SAndrey Smirnov 					(val & cond_mask) == cond_value,
28393a10569SAndrey Smirnov 					sleep_us, timeout_us);
2847caff0fcSAndrey Gusakov }
2857caff0fcSAndrey Gusakov 
2867caff0fcSAndrey Gusakov static int tc_aux_wait_busy(struct tc_data *tc, unsigned int timeout_ms)
2877caff0fcSAndrey Gusakov {
28893a10569SAndrey Smirnov 	return tc_poll_timeout(tc, DP0_AUXSTATUS, AUX_BUSY, 0,
2897caff0fcSAndrey Gusakov 			       1000, 1000 * timeout_ms);
2907caff0fcSAndrey Gusakov }
2917caff0fcSAndrey Gusakov 
2927caff0fcSAndrey Gusakov static int tc_aux_get_status(struct tc_data *tc, u8 *reply)
2937caff0fcSAndrey Gusakov {
2947caff0fcSAndrey Gusakov 	int ret;
2957caff0fcSAndrey Gusakov 	u32 value;
2967caff0fcSAndrey Gusakov 
2977caff0fcSAndrey Gusakov 	ret = regmap_read(tc->regmap, DP0_AUXSTATUS, &value);
2987caff0fcSAndrey Gusakov 	if (ret < 0)
2997caff0fcSAndrey Gusakov 		return ret;
300bfb6e014STomi Valkeinen 
3017caff0fcSAndrey Gusakov 	if (value & AUX_BUSY) {
302bfb6e014STomi Valkeinen 		dev_err(tc->dev, "aux busy!\n");
3037caff0fcSAndrey Gusakov 		return -EBUSY;
3047caff0fcSAndrey Gusakov 	}
3057caff0fcSAndrey Gusakov 
306bfb6e014STomi Valkeinen 	if (value & AUX_TIMEOUT) {
307bfb6e014STomi Valkeinen 		dev_err(tc->dev, "aux access timeout!\n");
308bfb6e014STomi Valkeinen 		return -ETIMEDOUT;
309bfb6e014STomi Valkeinen 	}
310bfb6e014STomi Valkeinen 
3117caff0fcSAndrey Gusakov 	*reply = (value & AUX_STATUS_MASK) >> AUX_STATUS_SHIFT;
3127caff0fcSAndrey Gusakov 	return 0;
3137caff0fcSAndrey Gusakov }
3147caff0fcSAndrey Gusakov 
315792a081aSAndrey Smirnov static int tc_aux_write_data(struct tc_data *tc, const void *data,
316792a081aSAndrey Smirnov 			     size_t size)
317792a081aSAndrey Smirnov {
318792a081aSAndrey Smirnov 	u32 auxwdata[DP_AUX_MAX_PAYLOAD_BYTES / sizeof(u32)] = { 0 };
319792a081aSAndrey Smirnov 	int ret, count = ALIGN(size, sizeof(u32));
320792a081aSAndrey Smirnov 
321792a081aSAndrey Smirnov 	memcpy(auxwdata, data, size);
322792a081aSAndrey Smirnov 
323792a081aSAndrey Smirnov 	ret = regmap_raw_write(tc->regmap, DP0_AUXWDATA(0), auxwdata, count);
324792a081aSAndrey Smirnov 	if (ret)
325792a081aSAndrey Smirnov 		return ret;
326792a081aSAndrey Smirnov 
327792a081aSAndrey Smirnov 	return size;
328792a081aSAndrey Smirnov }
329792a081aSAndrey Smirnov 
33053b166dcSAndrey Smirnov static int tc_aux_read_data(struct tc_data *tc, void *data, size_t size)
33153b166dcSAndrey Smirnov {
33253b166dcSAndrey Smirnov 	u32 auxrdata[DP_AUX_MAX_PAYLOAD_BYTES / sizeof(u32)];
33353b166dcSAndrey Smirnov 	int ret, count = ALIGN(size, sizeof(u32));
33453b166dcSAndrey Smirnov 
33553b166dcSAndrey Smirnov 	ret = regmap_raw_read(tc->regmap, DP0_AUXRDATA(0), auxrdata, count);
33653b166dcSAndrey Smirnov 	if (ret)
33753b166dcSAndrey Smirnov 		return ret;
33853b166dcSAndrey Smirnov 
33953b166dcSAndrey Smirnov 	memcpy(data, auxrdata, size);
34053b166dcSAndrey Smirnov 
34153b166dcSAndrey Smirnov 	return size;
34253b166dcSAndrey Smirnov }
34353b166dcSAndrey Smirnov 
3447caff0fcSAndrey Gusakov static ssize_t tc_aux_transfer(struct drm_dp_aux *aux,
3457caff0fcSAndrey Gusakov 			       struct drm_dp_aux_msg *msg)
3467caff0fcSAndrey Gusakov {
3477caff0fcSAndrey Gusakov 	struct tc_data *tc = aux_to_tc(aux);
348*e0655feaSAndrey Smirnov 	size_t size = min_t(size_t, DP_AUX_MAX_PAYLOAD_BYTES - 1, msg->size);
3497caff0fcSAndrey Gusakov 	u8 request = msg->request & ~DP_AUX_I2C_MOT;
3507caff0fcSAndrey Gusakov 	int ret;
3517caff0fcSAndrey Gusakov 
3527caff0fcSAndrey Gusakov 	if (size == 0)
3537caff0fcSAndrey Gusakov 		return 0;
3547caff0fcSAndrey Gusakov 
3557caff0fcSAndrey Gusakov 	ret = tc_aux_wait_busy(tc, 100);
3567caff0fcSAndrey Gusakov 	if (ret)
3576d0c3831SAndrey Smirnov 		return ret;
3587caff0fcSAndrey Gusakov 
359792a081aSAndrey Smirnov 	switch (request) {
360792a081aSAndrey Smirnov 	case DP_AUX_NATIVE_READ:
361792a081aSAndrey Smirnov 	case DP_AUX_I2C_READ:
362792a081aSAndrey Smirnov 		break;
363792a081aSAndrey Smirnov 	case DP_AUX_NATIVE_WRITE:
364792a081aSAndrey Smirnov 	case DP_AUX_I2C_WRITE:
365792a081aSAndrey Smirnov 		ret = tc_aux_write_data(tc, msg->buffer, size);
366792a081aSAndrey Smirnov 		if (ret < 0)
3676d0c3831SAndrey Smirnov 			return ret;
368792a081aSAndrey Smirnov 		break;
369792a081aSAndrey Smirnov 	default:
3707caff0fcSAndrey Gusakov 		return -EINVAL;
3717caff0fcSAndrey Gusakov 	}
3727caff0fcSAndrey Gusakov 
3737caff0fcSAndrey Gusakov 	/* Store address */
3746d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_AUXADDR, msg->address);
3756d0c3831SAndrey Smirnov 	if (ret)
3766d0c3831SAndrey Smirnov 		return ret;
3777caff0fcSAndrey Gusakov 	/* Start transfer */
3786d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_AUXCFG0,
3796d0c3831SAndrey Smirnov 			   ((size - 1) << 8) | request);
3806d0c3831SAndrey Smirnov 	if (ret)
3816d0c3831SAndrey Smirnov 		return ret;
3827caff0fcSAndrey Gusakov 
3837caff0fcSAndrey Gusakov 	ret = tc_aux_wait_busy(tc, 100);
3847caff0fcSAndrey Gusakov 	if (ret)
3856d0c3831SAndrey Smirnov 		return ret;
3867caff0fcSAndrey Gusakov 
3877caff0fcSAndrey Gusakov 	ret = tc_aux_get_status(tc, &msg->reply);
3887caff0fcSAndrey Gusakov 	if (ret)
3896d0c3831SAndrey Smirnov 		return ret;
3907caff0fcSAndrey Gusakov 
39153b166dcSAndrey Smirnov 	switch (request) {
39253b166dcSAndrey Smirnov 	case DP_AUX_NATIVE_READ:
39353b166dcSAndrey Smirnov 	case DP_AUX_I2C_READ:
39453b166dcSAndrey Smirnov 		return tc_aux_read_data(tc, msg->buffer, size);
3957caff0fcSAndrey Gusakov 	}
3967caff0fcSAndrey Gusakov 
3977caff0fcSAndrey Gusakov 	return size;
3987caff0fcSAndrey Gusakov }
3997caff0fcSAndrey Gusakov 
4007caff0fcSAndrey Gusakov static const char * const training_pattern1_errors[] = {
4017caff0fcSAndrey Gusakov 	"No errors",
4027caff0fcSAndrey Gusakov 	"Aux write error",
4037caff0fcSAndrey Gusakov 	"Aux read error",
4047caff0fcSAndrey Gusakov 	"Max voltage reached error",
4057caff0fcSAndrey Gusakov 	"Loop counter expired error",
4067caff0fcSAndrey Gusakov 	"res", "res", "res"
4077caff0fcSAndrey Gusakov };
4087caff0fcSAndrey Gusakov 
4097caff0fcSAndrey Gusakov static const char * const training_pattern2_errors[] = {
4107caff0fcSAndrey Gusakov 	"No errors",
4117caff0fcSAndrey Gusakov 	"Aux write error",
4127caff0fcSAndrey Gusakov 	"Aux read error",
4137caff0fcSAndrey Gusakov 	"Clock recovery failed error",
4147caff0fcSAndrey Gusakov 	"Loop counter expired error",
4157caff0fcSAndrey Gusakov 	"res", "res", "res"
4167caff0fcSAndrey Gusakov };
4177caff0fcSAndrey Gusakov 
4187caff0fcSAndrey Gusakov static u32 tc_srcctrl(struct tc_data *tc)
4197caff0fcSAndrey Gusakov {
4207caff0fcSAndrey Gusakov 	/*
4217caff0fcSAndrey Gusakov 	 * No training pattern, skew lane 1 data by two LSCLK cycles with
4227caff0fcSAndrey Gusakov 	 * respect to lane 0 data, AutoCorrect Mode = 0
4237caff0fcSAndrey Gusakov 	 */
4244b30bf41STomi Valkeinen 	u32 reg = DP0_SRCCTRL_NOTP | DP0_SRCCTRL_LANESKEW | DP0_SRCCTRL_EN810B;
4257caff0fcSAndrey Gusakov 
4267caff0fcSAndrey Gusakov 	if (tc->link.scrambler_dis)
4277caff0fcSAndrey Gusakov 		reg |= DP0_SRCCTRL_SCRMBLDIS;	/* Scrambler Disabled */
4287caff0fcSAndrey Gusakov 	if (tc->link.spread)
4297caff0fcSAndrey Gusakov 		reg |= DP0_SRCCTRL_SSCG;	/* Spread Spectrum Enable */
4307caff0fcSAndrey Gusakov 	if (tc->link.base.num_lanes == 2)
4317caff0fcSAndrey Gusakov 		reg |= DP0_SRCCTRL_LANES_2;	/* Two Main Channel Lanes */
4327caff0fcSAndrey Gusakov 	if (tc->link.base.rate != 162000)
4337caff0fcSAndrey Gusakov 		reg |= DP0_SRCCTRL_BW27;	/* 2.7 Gbps link */
4347caff0fcSAndrey Gusakov 	return reg;
4357caff0fcSAndrey Gusakov }
4367caff0fcSAndrey Gusakov 
4377caff0fcSAndrey Gusakov static void tc_wait_pll_lock(struct tc_data *tc)
4387caff0fcSAndrey Gusakov {
4397caff0fcSAndrey Gusakov 	/* Wait for PLL to lock: up to 2.09 ms, depending on refclk */
4407caff0fcSAndrey Gusakov 	usleep_range(3000, 6000);
4417caff0fcSAndrey Gusakov }
4427caff0fcSAndrey Gusakov 
4437caff0fcSAndrey Gusakov static int tc_pxl_pll_en(struct tc_data *tc, u32 refclk, u32 pixelclock)
4447caff0fcSAndrey Gusakov {
4457caff0fcSAndrey Gusakov 	int ret;
4467caff0fcSAndrey Gusakov 	int i_pre, best_pre = 1;
4477caff0fcSAndrey Gusakov 	int i_post, best_post = 1;
4487caff0fcSAndrey Gusakov 	int div, best_div = 1;
4497caff0fcSAndrey Gusakov 	int mul, best_mul = 1;
4507caff0fcSAndrey Gusakov 	int delta, best_delta;
4517caff0fcSAndrey Gusakov 	int ext_div[] = {1, 2, 3, 5, 7};
4527caff0fcSAndrey Gusakov 	int best_pixelclock = 0;
4537caff0fcSAndrey Gusakov 	int vco_hi = 0;
4546d0c3831SAndrey Smirnov 	u32 pxl_pllparam;
4557caff0fcSAndrey Gusakov 
4567caff0fcSAndrey Gusakov 	dev_dbg(tc->dev, "PLL: requested %d pixelclock, ref %d\n", pixelclock,
4577caff0fcSAndrey Gusakov 		refclk);
4587caff0fcSAndrey Gusakov 	best_delta = pixelclock;
4597caff0fcSAndrey Gusakov 	/* Loop over all possible ext_divs, skipping invalid configurations */
4607caff0fcSAndrey Gusakov 	for (i_pre = 0; i_pre < ARRAY_SIZE(ext_div); i_pre++) {
4617caff0fcSAndrey Gusakov 		/*
4627caff0fcSAndrey Gusakov 		 * refclk / ext_pre_div should be in the 1 to 200 MHz range.
4637caff0fcSAndrey Gusakov 		 * We don't allow any refclk > 200 MHz, only check lower bounds.
4647caff0fcSAndrey Gusakov 		 */
4657caff0fcSAndrey Gusakov 		if (refclk / ext_div[i_pre] < 1000000)
4667caff0fcSAndrey Gusakov 			continue;
4677caff0fcSAndrey Gusakov 		for (i_post = 0; i_post < ARRAY_SIZE(ext_div); i_post++) {
4687caff0fcSAndrey Gusakov 			for (div = 1; div <= 16; div++) {
4697caff0fcSAndrey Gusakov 				u32 clk;
4707caff0fcSAndrey Gusakov 				u64 tmp;
4717caff0fcSAndrey Gusakov 
4727caff0fcSAndrey Gusakov 				tmp = pixelclock * ext_div[i_pre] *
4737caff0fcSAndrey Gusakov 				      ext_div[i_post] * div;
4747caff0fcSAndrey Gusakov 				do_div(tmp, refclk);
4757caff0fcSAndrey Gusakov 				mul = tmp;
4767caff0fcSAndrey Gusakov 
4777caff0fcSAndrey Gusakov 				/* Check limits */
4787caff0fcSAndrey Gusakov 				if ((mul < 1) || (mul > 128))
4797caff0fcSAndrey Gusakov 					continue;
4807caff0fcSAndrey Gusakov 
4817caff0fcSAndrey Gusakov 				clk = (refclk / ext_div[i_pre] / div) * mul;
4827caff0fcSAndrey Gusakov 				/*
4837caff0fcSAndrey Gusakov 				 * refclk * mul / (ext_pre_div * pre_div)
4847caff0fcSAndrey Gusakov 				 * should be in the 150 to 650 MHz range
4857caff0fcSAndrey Gusakov 				 */
4867caff0fcSAndrey Gusakov 				if ((clk > 650000000) || (clk < 150000000))
4877caff0fcSAndrey Gusakov 					continue;
4887caff0fcSAndrey Gusakov 
4897caff0fcSAndrey Gusakov 				clk = clk / ext_div[i_post];
4907caff0fcSAndrey Gusakov 				delta = clk - pixelclock;
4917caff0fcSAndrey Gusakov 
4927caff0fcSAndrey Gusakov 				if (abs(delta) < abs(best_delta)) {
4937caff0fcSAndrey Gusakov 					best_pre = i_pre;
4947caff0fcSAndrey Gusakov 					best_post = i_post;
4957caff0fcSAndrey Gusakov 					best_div = div;
4967caff0fcSAndrey Gusakov 					best_mul = mul;
4977caff0fcSAndrey Gusakov 					best_delta = delta;
4987caff0fcSAndrey Gusakov 					best_pixelclock = clk;
4997caff0fcSAndrey Gusakov 				}
5007caff0fcSAndrey Gusakov 			}
5017caff0fcSAndrey Gusakov 		}
5027caff0fcSAndrey Gusakov 	}
5037caff0fcSAndrey Gusakov 	if (best_pixelclock == 0) {
5047caff0fcSAndrey Gusakov 		dev_err(tc->dev, "Failed to calc clock for %d pixelclock\n",
5057caff0fcSAndrey Gusakov 			pixelclock);
5067caff0fcSAndrey Gusakov 		return -EINVAL;
5077caff0fcSAndrey Gusakov 	}
5087caff0fcSAndrey Gusakov 
5097caff0fcSAndrey Gusakov 	dev_dbg(tc->dev, "PLL: got %d, delta %d\n", best_pixelclock,
5107caff0fcSAndrey Gusakov 		best_delta);
5117caff0fcSAndrey Gusakov 	dev_dbg(tc->dev, "PLL: %d / %d / %d * %d / %d\n", refclk,
5127caff0fcSAndrey Gusakov 		ext_div[best_pre], best_div, best_mul, ext_div[best_post]);
5137caff0fcSAndrey Gusakov 
5147caff0fcSAndrey Gusakov 	/* if VCO >= 300 MHz */
5157caff0fcSAndrey Gusakov 	if (refclk / ext_div[best_pre] / best_div * best_mul >= 300000000)
5167caff0fcSAndrey Gusakov 		vco_hi = 1;
5177caff0fcSAndrey Gusakov 	/* see DS */
5187caff0fcSAndrey Gusakov 	if (best_div == 16)
5197caff0fcSAndrey Gusakov 		best_div = 0;
5207caff0fcSAndrey Gusakov 	if (best_mul == 128)
5217caff0fcSAndrey Gusakov 		best_mul = 0;
5227caff0fcSAndrey Gusakov 
5237caff0fcSAndrey Gusakov 	/* Power up PLL and switch to bypass */
5246d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, PXL_PLLCTRL, PLLBYP | PLLEN);
5256d0c3831SAndrey Smirnov 	if (ret)
5266d0c3831SAndrey Smirnov 		return ret;
5277caff0fcSAndrey Gusakov 
5286d0c3831SAndrey Smirnov 	pxl_pllparam  = vco_hi << 24; /* For PLL VCO >= 300 MHz = 1 */
5296d0c3831SAndrey Smirnov 	pxl_pllparam |= ext_div[best_pre] << 20; /* External Pre-divider */
5306d0c3831SAndrey Smirnov 	pxl_pllparam |= ext_div[best_post] << 16; /* External Post-divider */
5316d0c3831SAndrey Smirnov 	pxl_pllparam |= IN_SEL_REFCLK; /* Use RefClk as PLL input */
5326d0c3831SAndrey Smirnov 	pxl_pllparam |= best_div << 8; /* Divider for PLL RefClk */
5336d0c3831SAndrey Smirnov 	pxl_pllparam |= best_mul; /* Multiplier for PLL */
5346d0c3831SAndrey Smirnov 
5356d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, PXL_PLLPARAM, pxl_pllparam);
5366d0c3831SAndrey Smirnov 	if (ret)
5376d0c3831SAndrey Smirnov 		return ret;
5387caff0fcSAndrey Gusakov 
5397caff0fcSAndrey Gusakov 	/* Force PLL parameter update and disable bypass */
5406d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, PXL_PLLCTRL, PLLUPDATE | PLLEN);
5416d0c3831SAndrey Smirnov 	if (ret)
5426d0c3831SAndrey Smirnov 		return ret;
5437caff0fcSAndrey Gusakov 
5447caff0fcSAndrey Gusakov 	tc_wait_pll_lock(tc);
5457caff0fcSAndrey Gusakov 
5467caff0fcSAndrey Gusakov 	return 0;
5477caff0fcSAndrey Gusakov }
5487caff0fcSAndrey Gusakov 
5497caff0fcSAndrey Gusakov static int tc_pxl_pll_dis(struct tc_data *tc)
5507caff0fcSAndrey Gusakov {
5517caff0fcSAndrey Gusakov 	/* Enable PLL bypass, power down PLL */
5527caff0fcSAndrey Gusakov 	return regmap_write(tc->regmap, PXL_PLLCTRL, PLLBYP);
5537caff0fcSAndrey Gusakov }
5547caff0fcSAndrey Gusakov 
5557caff0fcSAndrey Gusakov static int tc_stream_clock_calc(struct tc_data *tc)
5567caff0fcSAndrey Gusakov {
5577caff0fcSAndrey Gusakov 	/*
5587caff0fcSAndrey Gusakov 	 * If the Stream clock and Link Symbol clock are
5597caff0fcSAndrey Gusakov 	 * asynchronous with each other, the value of M changes over
5607caff0fcSAndrey Gusakov 	 * time. This way of generating link clock and stream
5617caff0fcSAndrey Gusakov 	 * clock is called Asynchronous Clock mode. The value M
5627caff0fcSAndrey Gusakov 	 * must change while the value N stays constant. The
5637caff0fcSAndrey Gusakov 	 * value of N in this Asynchronous Clock mode must be set
5647caff0fcSAndrey Gusakov 	 * to 2^15 or 32,768.
5657caff0fcSAndrey Gusakov 	 *
5667caff0fcSAndrey Gusakov 	 * LSCLK = 1/10 of high speed link clock
5677caff0fcSAndrey Gusakov 	 *
5687caff0fcSAndrey Gusakov 	 * f_STRMCLK = M/N * f_LSCLK
5697caff0fcSAndrey Gusakov 	 * M/N = f_STRMCLK / f_LSCLK
5707caff0fcSAndrey Gusakov 	 *
5717caff0fcSAndrey Gusakov 	 */
5726d0c3831SAndrey Smirnov 	return regmap_write(tc->regmap, DP0_VIDMNGEN1, 32768);
5737caff0fcSAndrey Gusakov }
5747caff0fcSAndrey Gusakov 
5757caff0fcSAndrey Gusakov static int tc_aux_link_setup(struct tc_data *tc)
5767caff0fcSAndrey Gusakov {
5777caff0fcSAndrey Gusakov 	unsigned long rate;
5786d0c3831SAndrey Smirnov 	u32 dp0_auxcfg1;
5797caff0fcSAndrey Gusakov 	u32 value;
5807caff0fcSAndrey Gusakov 	int ret;
5817caff0fcSAndrey Gusakov 
5827caff0fcSAndrey Gusakov 	rate = clk_get_rate(tc->refclk);
5837caff0fcSAndrey Gusakov 	switch (rate) {
5847caff0fcSAndrey Gusakov 	case 38400000:
5857caff0fcSAndrey Gusakov 		value = REF_FREQ_38M4;
5867caff0fcSAndrey Gusakov 		break;
5877caff0fcSAndrey Gusakov 	case 26000000:
5887caff0fcSAndrey Gusakov 		value = REF_FREQ_26M;
5897caff0fcSAndrey Gusakov 		break;
5907caff0fcSAndrey Gusakov 	case 19200000:
5917caff0fcSAndrey Gusakov 		value = REF_FREQ_19M2;
5927caff0fcSAndrey Gusakov 		break;
5937caff0fcSAndrey Gusakov 	case 13000000:
5947caff0fcSAndrey Gusakov 		value = REF_FREQ_13M;
5957caff0fcSAndrey Gusakov 		break;
5967caff0fcSAndrey Gusakov 	default:
5977caff0fcSAndrey Gusakov 		dev_err(tc->dev, "Invalid refclk rate: %lu Hz\n", rate);
5987caff0fcSAndrey Gusakov 		return -EINVAL;
5997caff0fcSAndrey Gusakov 	}
6007caff0fcSAndrey Gusakov 
6017caff0fcSAndrey Gusakov 	/* Setup DP-PHY / PLL */
6027caff0fcSAndrey Gusakov 	value |= SYSCLK_SEL_LSCLK | LSCLK_DIV_2;
6036d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, SYS_PLLPARAM, value);
6046d0c3831SAndrey Smirnov 	if (ret)
6056d0c3831SAndrey Smirnov 		goto err;
6067caff0fcSAndrey Gusakov 
6076d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP_PHY_CTRL,
6086d0c3831SAndrey Smirnov 			   BGREN | PWR_SW_EN | PHY_A0_EN);
6096d0c3831SAndrey Smirnov 	if (ret)
6106d0c3831SAndrey Smirnov 		goto err;
6117caff0fcSAndrey Gusakov 	/*
6127caff0fcSAndrey Gusakov 	 * Initially PLLs are in bypass. Force PLL parameter update,
6137caff0fcSAndrey Gusakov 	 * disable PLL bypass, enable PLL
6147caff0fcSAndrey Gusakov 	 */
6156d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_PLLCTRL, PLLUPDATE | PLLEN);
6166d0c3831SAndrey Smirnov 	if (ret)
6176d0c3831SAndrey Smirnov 		goto err;
6187caff0fcSAndrey Gusakov 	tc_wait_pll_lock(tc);
6197caff0fcSAndrey Gusakov 
6206d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP1_PLLCTRL, PLLUPDATE | PLLEN);
6216d0c3831SAndrey Smirnov 	if (ret)
6226d0c3831SAndrey Smirnov 		goto err;
6237caff0fcSAndrey Gusakov 	tc_wait_pll_lock(tc);
6247caff0fcSAndrey Gusakov 
62593a10569SAndrey Smirnov 	ret = tc_poll_timeout(tc, DP_PHY_CTRL, PHY_RDY, PHY_RDY, 1, 1000);
6267caff0fcSAndrey Gusakov 	if (ret == -ETIMEDOUT) {
6277caff0fcSAndrey Gusakov 		dev_err(tc->dev, "Timeout waiting for PHY to become ready");
6287caff0fcSAndrey Gusakov 		return ret;
629ca342386STomi Valkeinen 	} else if (ret) {
6307caff0fcSAndrey Gusakov 		goto err;
631ca342386STomi Valkeinen 	}
6327caff0fcSAndrey Gusakov 
6337caff0fcSAndrey Gusakov 	/* Setup AUX link */
6346d0c3831SAndrey Smirnov 	dp0_auxcfg1  = AUX_RX_FILTER_EN;
6356d0c3831SAndrey Smirnov 	dp0_auxcfg1 |= 0x06 << 8; /* Aux Bit Period Calculator Threshold */
6366d0c3831SAndrey Smirnov 	dp0_auxcfg1 |= 0x3f << 0; /* Aux Response Timeout Timer */
6376d0c3831SAndrey Smirnov 
6386d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_AUXCFG1, dp0_auxcfg1);
6396d0c3831SAndrey Smirnov 	if (ret)
6406d0c3831SAndrey Smirnov 		goto err;
6417caff0fcSAndrey Gusakov 
6427caff0fcSAndrey Gusakov 	return 0;
6437caff0fcSAndrey Gusakov err:
6447caff0fcSAndrey Gusakov 	dev_err(tc->dev, "tc_aux_link_setup failed: %d\n", ret);
6457caff0fcSAndrey Gusakov 	return ret;
6467caff0fcSAndrey Gusakov }
6477caff0fcSAndrey Gusakov 
6487caff0fcSAndrey Gusakov static int tc_get_display_props(struct tc_data *tc)
6497caff0fcSAndrey Gusakov {
6507caff0fcSAndrey Gusakov 	int ret;
6517caff0fcSAndrey Gusakov 	/* temp buffer */
6527caff0fcSAndrey Gusakov 	u8 tmp[8];
6537caff0fcSAndrey Gusakov 
6547caff0fcSAndrey Gusakov 	/* Read DP Rx Link Capability */
6557caff0fcSAndrey Gusakov 	ret = drm_dp_link_probe(&tc->aux, &tc->link.base);
6567caff0fcSAndrey Gusakov 	if (ret < 0)
6577caff0fcSAndrey Gusakov 		goto err_dpcd_read;
658cffd2b16SAndrey Gusakov 	if (tc->link.base.rate != 162000 && tc->link.base.rate != 270000) {
659cffd2b16SAndrey Gusakov 		dev_dbg(tc->dev, "Falling to 2.7 Gbps rate\n");
660cffd2b16SAndrey Gusakov 		tc->link.base.rate = 270000;
661cffd2b16SAndrey Gusakov 	}
662cffd2b16SAndrey Gusakov 
663cffd2b16SAndrey Gusakov 	if (tc->link.base.num_lanes > 2) {
664cffd2b16SAndrey Gusakov 		dev_dbg(tc->dev, "Falling to 2 lanes\n");
665cffd2b16SAndrey Gusakov 		tc->link.base.num_lanes = 2;
666cffd2b16SAndrey Gusakov 	}
6677caff0fcSAndrey Gusakov 
6687caff0fcSAndrey Gusakov 	ret = drm_dp_dpcd_readb(&tc->aux, DP_MAX_DOWNSPREAD, tmp);
6697caff0fcSAndrey Gusakov 	if (ret < 0)
6707caff0fcSAndrey Gusakov 		goto err_dpcd_read;
671e5607637STomi Valkeinen 	tc->link.spread = tmp[0] & DP_MAX_DOWNSPREAD_0_5;
6727caff0fcSAndrey Gusakov 
6737caff0fcSAndrey Gusakov 	ret = drm_dp_dpcd_readb(&tc->aux, DP_MAIN_LINK_CHANNEL_CODING, tmp);
6747caff0fcSAndrey Gusakov 	if (ret < 0)
6757caff0fcSAndrey Gusakov 		goto err_dpcd_read;
6764b30bf41STomi Valkeinen 
677e5607637STomi Valkeinen 	tc->link.scrambler_dis = false;
6787caff0fcSAndrey Gusakov 	/* read assr */
6797caff0fcSAndrey Gusakov 	ret = drm_dp_dpcd_readb(&tc->aux, DP_EDP_CONFIGURATION_SET, tmp);
6807caff0fcSAndrey Gusakov 	if (ret < 0)
6817caff0fcSAndrey Gusakov 		goto err_dpcd_read;
6827caff0fcSAndrey Gusakov 	tc->link.assr = tmp[0] & DP_ALTERNATE_SCRAMBLER_RESET_ENABLE;
6837caff0fcSAndrey Gusakov 
6847caff0fcSAndrey Gusakov 	dev_dbg(tc->dev, "DPCD rev: %d.%d, rate: %s, lanes: %d, framing: %s\n",
6857caff0fcSAndrey Gusakov 		tc->link.base.revision >> 4, tc->link.base.revision & 0x0f,
6867caff0fcSAndrey Gusakov 		(tc->link.base.rate == 162000) ? "1.62Gbps" : "2.7Gbps",
6877caff0fcSAndrey Gusakov 		tc->link.base.num_lanes,
6887caff0fcSAndrey Gusakov 		(tc->link.base.capabilities & DP_LINK_CAP_ENHANCED_FRAMING) ?
6897caff0fcSAndrey Gusakov 		"enhanced" : "non-enhanced");
690e5607637STomi Valkeinen 	dev_dbg(tc->dev, "Downspread: %s, scrambler: %s\n",
691e5607637STomi Valkeinen 		tc->link.spread ? "0.5%" : "0.0%",
692e5607637STomi Valkeinen 		tc->link.scrambler_dis ? "disabled" : "enabled");
6937caff0fcSAndrey Gusakov 	dev_dbg(tc->dev, "Display ASSR: %d, TC358767 ASSR: %d\n",
6947caff0fcSAndrey Gusakov 		tc->link.assr, tc->assr);
6957caff0fcSAndrey Gusakov 
6967caff0fcSAndrey Gusakov 	return 0;
6977caff0fcSAndrey Gusakov 
6987caff0fcSAndrey Gusakov err_dpcd_read:
6997caff0fcSAndrey Gusakov 	dev_err(tc->dev, "failed to read DPCD: %d\n", ret);
7007caff0fcSAndrey Gusakov 	return ret;
7017caff0fcSAndrey Gusakov }
7027caff0fcSAndrey Gusakov 
70363f8f3baSLaurent Pinchart static int tc_set_video_mode(struct tc_data *tc,
70463f8f3baSLaurent Pinchart 			     const struct drm_display_mode *mode)
7057caff0fcSAndrey Gusakov {
7067caff0fcSAndrey Gusakov 	int ret;
7077caff0fcSAndrey Gusakov 	int vid_sync_dly;
7087caff0fcSAndrey Gusakov 	int max_tu_symbol;
7097caff0fcSAndrey Gusakov 
7107caff0fcSAndrey Gusakov 	int left_margin = mode->htotal - mode->hsync_end;
7117caff0fcSAndrey Gusakov 	int right_margin = mode->hsync_start - mode->hdisplay;
7127caff0fcSAndrey Gusakov 	int hsync_len = mode->hsync_end - mode->hsync_start;
7137caff0fcSAndrey Gusakov 	int upper_margin = mode->vtotal - mode->vsync_end;
7147caff0fcSAndrey Gusakov 	int lower_margin = mode->vsync_start - mode->vdisplay;
7157caff0fcSAndrey Gusakov 	int vsync_len = mode->vsync_end - mode->vsync_start;
7163f072c30SAndrey Smirnov 	u32 dp0_syncval;
7177caff0fcSAndrey Gusakov 
71866d1c3b9SAndrey Gusakov 	/*
71966d1c3b9SAndrey Gusakov 	 * Recommended maximum number of symbols transferred in a transfer unit:
72066d1c3b9SAndrey Gusakov 	 * DIV_ROUND_UP((input active video bandwidth in bytes) * tu_size,
72166d1c3b9SAndrey Gusakov 	 *              (output active video bandwidth in bytes))
72266d1c3b9SAndrey Gusakov 	 * Must be less than tu_size.
72366d1c3b9SAndrey Gusakov 	 */
72466d1c3b9SAndrey Gusakov 	max_tu_symbol = TU_SIZE_RECOMMENDED - 1;
72566d1c3b9SAndrey Gusakov 
7267caff0fcSAndrey Gusakov 	dev_dbg(tc->dev, "set mode %dx%d\n",
7277caff0fcSAndrey Gusakov 		mode->hdisplay, mode->vdisplay);
7287caff0fcSAndrey Gusakov 	dev_dbg(tc->dev, "H margin %d,%d sync %d\n",
7297caff0fcSAndrey Gusakov 		left_margin, right_margin, hsync_len);
7307caff0fcSAndrey Gusakov 	dev_dbg(tc->dev, "V margin %d,%d sync %d\n",
7317caff0fcSAndrey Gusakov 		upper_margin, lower_margin, vsync_len);
7327caff0fcSAndrey Gusakov 	dev_dbg(tc->dev, "total: %dx%d\n", mode->htotal, mode->vtotal);
7337caff0fcSAndrey Gusakov 
7347caff0fcSAndrey Gusakov 
73566d1c3b9SAndrey Gusakov 	/*
73666d1c3b9SAndrey Gusakov 	 * LCD Ctl Frame Size
73766d1c3b9SAndrey Gusakov 	 * datasheet is not clear of vsdelay in case of DPI
73866d1c3b9SAndrey Gusakov 	 * assume we do not need any delay when DPI is a source of
73966d1c3b9SAndrey Gusakov 	 * sync signals
74066d1c3b9SAndrey Gusakov 	 */
7416d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, VPCTRL0,
7423f072c30SAndrey Smirnov 			   FIELD_PREP(VSDELAY, 0) |
7437caff0fcSAndrey Gusakov 			   OPXLFMT_RGB888 | FRMSYNC_DISABLED | MSF_DISABLED);
7446d0c3831SAndrey Smirnov 	if (ret)
7456d0c3831SAndrey Smirnov 		return ret;
7466d0c3831SAndrey Smirnov 
7476d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, HTIM01,
7483f072c30SAndrey Smirnov 			   FIELD_PREP(HBPR, ALIGN(left_margin, 2)) |
7493f072c30SAndrey Smirnov 			   FIELD_PREP(HPW, ALIGN(hsync_len, 2)));
7506d0c3831SAndrey Smirnov 	if (ret)
7516d0c3831SAndrey Smirnov 		return ret;
7526d0c3831SAndrey Smirnov 
7536d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, HTIM02,
7543f072c30SAndrey Smirnov 			   FIELD_PREP(HDISPR, ALIGN(mode->hdisplay, 2)) |
7553f072c30SAndrey Smirnov 			   FIELD_PREP(HFPR, ALIGN(right_margin, 2)));
7566d0c3831SAndrey Smirnov 	if (ret)
7576d0c3831SAndrey Smirnov 		return ret;
7586d0c3831SAndrey Smirnov 
7596d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, VTIM01,
7603f072c30SAndrey Smirnov 			   FIELD_PREP(VBPR, upper_margin) |
7613f072c30SAndrey Smirnov 			   FIELD_PREP(VSPR, vsync_len));
7626d0c3831SAndrey Smirnov 	if (ret)
7636d0c3831SAndrey Smirnov 		return ret;
7646d0c3831SAndrey Smirnov 
7656d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, VTIM02,
7663f072c30SAndrey Smirnov 			   FIELD_PREP(VFPR, lower_margin) |
7673f072c30SAndrey Smirnov 			   FIELD_PREP(VDISPR, mode->vdisplay));
7686d0c3831SAndrey Smirnov 	if (ret)
7696d0c3831SAndrey Smirnov 		return ret;
7706d0c3831SAndrey Smirnov 
7716d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, VFUEN0, VFUEN); /* update settings */
7726d0c3831SAndrey Smirnov 	if (ret)
7736d0c3831SAndrey Smirnov 		return ret;
7747caff0fcSAndrey Gusakov 
7757caff0fcSAndrey Gusakov 	/* Test pattern settings */
7766d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, TSTCTL,
7773f072c30SAndrey Smirnov 			   FIELD_PREP(COLOR_R, 120) |
7783f072c30SAndrey Smirnov 			   FIELD_PREP(COLOR_G, 20) |
7793f072c30SAndrey Smirnov 			   FIELD_PREP(COLOR_B, 99) |
7803f072c30SAndrey Smirnov 			   ENI2CFILTER |
7813f072c30SAndrey Smirnov 			   FIELD_PREP(COLOR_BAR_MODE, COLOR_BAR_MODE_BARS));
7826d0c3831SAndrey Smirnov 	if (ret)
7836d0c3831SAndrey Smirnov 		return ret;
7847caff0fcSAndrey Gusakov 
7857caff0fcSAndrey Gusakov 	/* DP Main Stream Attributes */
7867caff0fcSAndrey Gusakov 	vid_sync_dly = hsync_len + left_margin + mode->hdisplay;
7876d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_VIDSYNCDELAY,
7883f072c30SAndrey Smirnov 		 FIELD_PREP(THRESH_DLY, max_tu_symbol) |
7893f072c30SAndrey Smirnov 		 FIELD_PREP(VID_SYNC_DLY, vid_sync_dly));
7907caff0fcSAndrey Gusakov 
7916d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_TOTALVAL,
7923f072c30SAndrey Smirnov 			   FIELD_PREP(H_TOTAL, mode->htotal) |
7933f072c30SAndrey Smirnov 			   FIELD_PREP(V_TOTAL, mode->vtotal));
7946d0c3831SAndrey Smirnov 	if (ret)
7956d0c3831SAndrey Smirnov 		return ret;
7967caff0fcSAndrey Gusakov 
7976d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_STARTVAL,
7983f072c30SAndrey Smirnov 			   FIELD_PREP(H_START, left_margin + hsync_len) |
7993f072c30SAndrey Smirnov 			   FIELD_PREP(V_START, upper_margin + vsync_len));
8006d0c3831SAndrey Smirnov 	if (ret)
8016d0c3831SAndrey Smirnov 		return ret;
8027caff0fcSAndrey Gusakov 
8036d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_ACTIVEVAL,
8043f072c30SAndrey Smirnov 			   FIELD_PREP(V_ACT, mode->vdisplay) |
8053f072c30SAndrey Smirnov 			   FIELD_PREP(H_ACT, mode->hdisplay));
8066d0c3831SAndrey Smirnov 	if (ret)
8076d0c3831SAndrey Smirnov 		return ret;
8087caff0fcSAndrey Gusakov 
8093f072c30SAndrey Smirnov 	dp0_syncval = FIELD_PREP(VS_WIDTH, vsync_len) |
8103f072c30SAndrey Smirnov 		      FIELD_PREP(HS_WIDTH, hsync_len);
8117caff0fcSAndrey Gusakov 
8123f072c30SAndrey Smirnov 	if (mode->flags & DRM_MODE_FLAG_NVSYNC)
8133f072c30SAndrey Smirnov 		dp0_syncval |= SYNCVAL_VS_POL_ACTIVE_LOW;
8147caff0fcSAndrey Gusakov 
8153f072c30SAndrey Smirnov 	if (mode->flags & DRM_MODE_FLAG_NHSYNC)
8163f072c30SAndrey Smirnov 		dp0_syncval |= SYNCVAL_HS_POL_ACTIVE_LOW;
8173f072c30SAndrey Smirnov 
8186d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_SYNCVAL, dp0_syncval);
8196d0c3831SAndrey Smirnov 	if (ret)
8206d0c3831SAndrey Smirnov 		return ret;
8213f072c30SAndrey Smirnov 
8226d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DPIPXLFMT,
8233f072c30SAndrey Smirnov 			   VS_POL_ACTIVE_LOW | HS_POL_ACTIVE_LOW |
8243f072c30SAndrey Smirnov 			   DE_POL_ACTIVE_HIGH | SUB_CFG_TYPE_CONFIG1 |
8253f072c30SAndrey Smirnov 			   DPI_BPP_RGB888);
8266d0c3831SAndrey Smirnov 	if (ret)
8276d0c3831SAndrey Smirnov 		return ret;
8283f072c30SAndrey Smirnov 
8296d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_MISC,
8303f072c30SAndrey Smirnov 			   FIELD_PREP(MAX_TU_SYMBOL, max_tu_symbol) |
8313f072c30SAndrey Smirnov 			   FIELD_PREP(TU_SIZE, TU_SIZE_RECOMMENDED) |
832f3b8adbeSAndrey Gusakov 			   BPC_8);
8336d0c3831SAndrey Smirnov 	if (ret)
8346d0c3831SAndrey Smirnov 		return ret;
8357caff0fcSAndrey Gusakov 
8367caff0fcSAndrey Gusakov 	return 0;
8377caff0fcSAndrey Gusakov }
8387caff0fcSAndrey Gusakov 
839f9538357STomi Valkeinen static int tc_wait_link_training(struct tc_data *tc)
8407caff0fcSAndrey Gusakov {
8417caff0fcSAndrey Gusakov 	u32 value;
8427caff0fcSAndrey Gusakov 	int ret;
8437caff0fcSAndrey Gusakov 
844aa92213fSAndrey Smirnov 	ret = tc_poll_timeout(tc, DP0_LTSTAT, LT_LOOPDONE,
845aa92213fSAndrey Smirnov 			      LT_LOOPDONE, 1, 1000);
846aa92213fSAndrey Smirnov 	if (ret) {
847f9538357STomi Valkeinen 		dev_err(tc->dev, "Link training timeout waiting for LT_LOOPDONE!\n");
848aa92213fSAndrey Smirnov 		return ret;
8497caff0fcSAndrey Gusakov 	}
8507caff0fcSAndrey Gusakov 
8516d0c3831SAndrey Smirnov 	ret = regmap_read(tc->regmap, DP0_LTSTAT, &value);
8526d0c3831SAndrey Smirnov 	if (ret)
8536d0c3831SAndrey Smirnov 		return ret;
854f9538357STomi Valkeinen 
855aa92213fSAndrey Smirnov 	return (value >> 8) & 0x7;
8567caff0fcSAndrey Gusakov }
8577caff0fcSAndrey Gusakov 
858cb3263b2STomi Valkeinen static int tc_main_link_enable(struct tc_data *tc)
8597caff0fcSAndrey Gusakov {
8607caff0fcSAndrey Gusakov 	struct drm_dp_aux *aux = &tc->aux;
8617caff0fcSAndrey Gusakov 	struct device *dev = tc->dev;
8627caff0fcSAndrey Gusakov 	unsigned int rate;
8637caff0fcSAndrey Gusakov 	u32 dp_phy_ctrl;
8647caff0fcSAndrey Gusakov 	u32 value;
8657caff0fcSAndrey Gusakov 	int ret;
8667caff0fcSAndrey Gusakov 	u8 tmp[8];
8677caff0fcSAndrey Gusakov 
868cb3263b2STomi Valkeinen 	dev_dbg(tc->dev, "link enable\n");
869cb3263b2STomi Valkeinen 
8706d0c3831SAndrey Smirnov 	ret = regmap_read(tc->regmap, DP0CTL, &value);
8716d0c3831SAndrey Smirnov 	if (ret)
8726d0c3831SAndrey Smirnov 		return ret;
87367bca92fSTomi Valkeinen 
8746d0c3831SAndrey Smirnov 	if (WARN_ON(value & DP_EN)) {
8756d0c3831SAndrey Smirnov 		ret = regmap_write(tc->regmap, DP0CTL, 0);
8766d0c3831SAndrey Smirnov 		if (ret)
8776d0c3831SAndrey Smirnov 			return ret;
8786d0c3831SAndrey Smirnov 	}
8796d0c3831SAndrey Smirnov 
8806d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_SRCCTRL, tc_srcctrl(tc));
8816d0c3831SAndrey Smirnov 	if (ret)
8826d0c3831SAndrey Smirnov 		return ret;
8839a63bd6fSTomi Valkeinen 	/* SSCG and BW27 on DP1 must be set to the same as on DP0 */
8846d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP1_SRCCTRL,
8859a63bd6fSTomi Valkeinen 		 (tc->link.spread ? DP0_SRCCTRL_SSCG : 0) |
8869a63bd6fSTomi Valkeinen 		 ((tc->link.base.rate != 162000) ? DP0_SRCCTRL_BW27 : 0));
8876d0c3831SAndrey Smirnov 	if (ret)
8886d0c3831SAndrey Smirnov 		return ret;
8897caff0fcSAndrey Gusakov 
8907caff0fcSAndrey Gusakov 	rate = clk_get_rate(tc->refclk);
8917caff0fcSAndrey Gusakov 	switch (rate) {
8927caff0fcSAndrey Gusakov 	case 38400000:
8937caff0fcSAndrey Gusakov 		value = REF_FREQ_38M4;
8947caff0fcSAndrey Gusakov 		break;
8957caff0fcSAndrey Gusakov 	case 26000000:
8967caff0fcSAndrey Gusakov 		value = REF_FREQ_26M;
8977caff0fcSAndrey Gusakov 		break;
8987caff0fcSAndrey Gusakov 	case 19200000:
8997caff0fcSAndrey Gusakov 		value = REF_FREQ_19M2;
9007caff0fcSAndrey Gusakov 		break;
9017caff0fcSAndrey Gusakov 	case 13000000:
9027caff0fcSAndrey Gusakov 		value = REF_FREQ_13M;
9037caff0fcSAndrey Gusakov 		break;
9047caff0fcSAndrey Gusakov 	default:
9057caff0fcSAndrey Gusakov 		return -EINVAL;
9067caff0fcSAndrey Gusakov 	}
9077caff0fcSAndrey Gusakov 	value |= SYSCLK_SEL_LSCLK | LSCLK_DIV_2;
9086d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, SYS_PLLPARAM, value);
9096d0c3831SAndrey Smirnov 	if (ret)
9106d0c3831SAndrey Smirnov 		return ret;
911adf41098STomi Valkeinen 
9127caff0fcSAndrey Gusakov 	/* Setup Main Link */
9134d9d54a7STomi Valkeinen 	dp_phy_ctrl = BGREN | PWR_SW_EN | PHY_A0_EN | PHY_M0_EN;
9144d9d54a7STomi Valkeinen 	if (tc->link.base.num_lanes == 2)
9154d9d54a7STomi Valkeinen 		dp_phy_ctrl |= PHY_2LANE;
9166d0c3831SAndrey Smirnov 
9176d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP_PHY_CTRL, dp_phy_ctrl);
9186d0c3831SAndrey Smirnov 	if (ret)
9196d0c3831SAndrey Smirnov 		return ret;
9207caff0fcSAndrey Gusakov 
9217caff0fcSAndrey Gusakov 	/* PLL setup */
9226d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_PLLCTRL, PLLUPDATE | PLLEN);
9236d0c3831SAndrey Smirnov 	if (ret)
9246d0c3831SAndrey Smirnov 		return ret;
9257caff0fcSAndrey Gusakov 	tc_wait_pll_lock(tc);
9267caff0fcSAndrey Gusakov 
9276d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP1_PLLCTRL, PLLUPDATE | PLLEN);
9286d0c3831SAndrey Smirnov 	if (ret)
9296d0c3831SAndrey Smirnov 		return ret;
9307caff0fcSAndrey Gusakov 	tc_wait_pll_lock(tc);
9317caff0fcSAndrey Gusakov 
9327caff0fcSAndrey Gusakov 	/* Reset/Enable Main Links */
9337caff0fcSAndrey Gusakov 	dp_phy_ctrl |= DP_PHY_RST | PHY_M1_RST | PHY_M0_RST;
9346d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP_PHY_CTRL, dp_phy_ctrl);
9357caff0fcSAndrey Gusakov 	usleep_range(100, 200);
9367caff0fcSAndrey Gusakov 	dp_phy_ctrl &= ~(DP_PHY_RST | PHY_M1_RST | PHY_M0_RST);
9376d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP_PHY_CTRL, dp_phy_ctrl);
9387caff0fcSAndrey Gusakov 
939ebcce4e6SAndrey Smirnov 	ret = tc_poll_timeout(tc, DP_PHY_CTRL, PHY_RDY, PHY_RDY, 1, 1000);
940ebcce4e6SAndrey Smirnov 	if (ret) {
9417caff0fcSAndrey Gusakov 		dev_err(dev, "timeout waiting for phy become ready");
942ebcce4e6SAndrey Smirnov 		return ret;
9437caff0fcSAndrey Gusakov 	}
9447caff0fcSAndrey Gusakov 
9457caff0fcSAndrey Gusakov 	/* Set misc: 8 bits per color */
9467caff0fcSAndrey Gusakov 	ret = regmap_update_bits(tc->regmap, DP0_MISC, BPC_8, BPC_8);
9477caff0fcSAndrey Gusakov 	if (ret)
9486d0c3831SAndrey Smirnov 		return ret;
9497caff0fcSAndrey Gusakov 
9507caff0fcSAndrey Gusakov 	/*
9517caff0fcSAndrey Gusakov 	 * ASSR mode
9527caff0fcSAndrey Gusakov 	 * on TC358767 side ASSR configured through strap pin
9537caff0fcSAndrey Gusakov 	 * seems there is no way to change this setting from SW
9547caff0fcSAndrey Gusakov 	 *
9557caff0fcSAndrey Gusakov 	 * check is tc configured for same mode
9567caff0fcSAndrey Gusakov 	 */
9577caff0fcSAndrey Gusakov 	if (tc->assr != tc->link.assr) {
9587caff0fcSAndrey Gusakov 		dev_dbg(dev, "Trying to set display to ASSR: %d\n",
9597caff0fcSAndrey Gusakov 			tc->assr);
9607caff0fcSAndrey Gusakov 		/* try to set ASSR on display side */
9617caff0fcSAndrey Gusakov 		tmp[0] = tc->assr;
9627caff0fcSAndrey Gusakov 		ret = drm_dp_dpcd_writeb(aux, DP_EDP_CONFIGURATION_SET, tmp[0]);
9637caff0fcSAndrey Gusakov 		if (ret < 0)
9647caff0fcSAndrey Gusakov 			goto err_dpcd_read;
9657caff0fcSAndrey Gusakov 		/* read back */
9667caff0fcSAndrey Gusakov 		ret = drm_dp_dpcd_readb(aux, DP_EDP_CONFIGURATION_SET, tmp);
9677caff0fcSAndrey Gusakov 		if (ret < 0)
9687caff0fcSAndrey Gusakov 			goto err_dpcd_read;
9697caff0fcSAndrey Gusakov 
9707caff0fcSAndrey Gusakov 		if (tmp[0] != tc->assr) {
97187291e5dSLucas Stach 			dev_dbg(dev, "Failed to switch display ASSR to %d, falling back to unscrambled mode\n",
9727caff0fcSAndrey Gusakov 				tc->assr);
9737caff0fcSAndrey Gusakov 			/* trying with disabled scrambler */
974e5607637STomi Valkeinen 			tc->link.scrambler_dis = true;
9757caff0fcSAndrey Gusakov 		}
9767caff0fcSAndrey Gusakov 	}
9777caff0fcSAndrey Gusakov 
9787caff0fcSAndrey Gusakov 	/* Setup Link & DPRx Config for Training */
9797caff0fcSAndrey Gusakov 	ret = drm_dp_link_configure(aux, &tc->link.base);
9807caff0fcSAndrey Gusakov 	if (ret < 0)
9817caff0fcSAndrey Gusakov 		goto err_dpcd_write;
9827caff0fcSAndrey Gusakov 
9837caff0fcSAndrey Gusakov 	/* DOWNSPREAD_CTRL */
9847caff0fcSAndrey Gusakov 	tmp[0] = tc->link.spread ? DP_SPREAD_AMP_0_5 : 0x00;
9857caff0fcSAndrey Gusakov 	/* MAIN_LINK_CHANNEL_CODING_SET */
9864b30bf41STomi Valkeinen 	tmp[1] =  DP_SET_ANSI_8B10B;
9877caff0fcSAndrey Gusakov 	ret = drm_dp_dpcd_write(aux, DP_DOWNSPREAD_CTRL, tmp, 2);
9887caff0fcSAndrey Gusakov 	if (ret < 0)
9897caff0fcSAndrey Gusakov 		goto err_dpcd_write;
9907caff0fcSAndrey Gusakov 
991c28d1484STomi Valkeinen 	/* Reset voltage-swing & pre-emphasis */
992c28d1484STomi Valkeinen 	tmp[0] = tmp[1] = DP_TRAIN_VOLTAGE_SWING_LEVEL_0 |
993c28d1484STomi Valkeinen 			  DP_TRAIN_PRE_EMPH_LEVEL_0;
994c28d1484STomi Valkeinen 	ret = drm_dp_dpcd_write(aux, DP_TRAINING_LANE0_SET, tmp, 2);
995c28d1484STomi Valkeinen 	if (ret < 0)
996c28d1484STomi Valkeinen 		goto err_dpcd_write;
997c28d1484STomi Valkeinen 
998f9538357STomi Valkeinen 	/* Clock-Recovery */
999f9538357STomi Valkeinen 
1000f9538357STomi Valkeinen 	/* Set DPCD 0x102 for Training Pattern 1 */
10016d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_SNKLTCTRL,
10026d0c3831SAndrey Smirnov 			   DP_LINK_SCRAMBLING_DISABLE |
1003f9538357STomi Valkeinen 			   DP_TRAINING_PATTERN_1);
10046d0c3831SAndrey Smirnov 	if (ret)
10056d0c3831SAndrey Smirnov 		return ret;
1006f9538357STomi Valkeinen 
10076d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_LTLOOPCTRL,
1008f9538357STomi Valkeinen 			   (15 << 28) |	/* Defer Iteration Count */
1009f9538357STomi Valkeinen 			   (15 << 24) |	/* Loop Iteration Count */
1010f9538357STomi Valkeinen 			   (0xd << 0));	/* Loop Timer Delay */
10116d0c3831SAndrey Smirnov 	if (ret)
10126d0c3831SAndrey Smirnov 		return ret;
1013f9538357STomi Valkeinen 
10146d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_SRCCTRL,
10156d0c3831SAndrey Smirnov 			   tc_srcctrl(tc) | DP0_SRCCTRL_SCRMBLDIS |
10166d0c3831SAndrey Smirnov 			   DP0_SRCCTRL_AUTOCORRECT |
10176d0c3831SAndrey Smirnov 			   DP0_SRCCTRL_TP1);
10186d0c3831SAndrey Smirnov 	if (ret)
10196d0c3831SAndrey Smirnov 		return ret;
1020f9538357STomi Valkeinen 
1021f9538357STomi Valkeinen 	/* Enable DP0 to start Link Training */
10226d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0CTL,
10236d0c3831SAndrey Smirnov 			   ((tc->link.base.capabilities &
10246d0c3831SAndrey Smirnov 			     DP_LINK_CAP_ENHANCED_FRAMING) ? EF_EN : 0) |
1025f9538357STomi Valkeinen 			   DP_EN);
10266d0c3831SAndrey Smirnov 	if (ret)
10276d0c3831SAndrey Smirnov 		return ret;
1028f9538357STomi Valkeinen 
1029f9538357STomi Valkeinen 	/* wait */
10306d0c3831SAndrey Smirnov 
1031f9538357STomi Valkeinen 	ret = tc_wait_link_training(tc);
1032f9538357STomi Valkeinen 	if (ret < 0)
10336d0c3831SAndrey Smirnov 		return ret;
10347caff0fcSAndrey Gusakov 
1035f9538357STomi Valkeinen 	if (ret) {
1036f9538357STomi Valkeinen 		dev_err(tc->dev, "Link training phase 1 failed: %s\n",
1037f9538357STomi Valkeinen 			training_pattern1_errors[ret]);
10386d0c3831SAndrey Smirnov 		return -ENODEV;
1039f9538357STomi Valkeinen 	}
1040f9538357STomi Valkeinen 
1041f9538357STomi Valkeinen 	/* Channel Equalization */
1042f9538357STomi Valkeinen 
1043f9538357STomi Valkeinen 	/* Set DPCD 0x102 for Training Pattern 2 */
10446d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_SNKLTCTRL,
10456d0c3831SAndrey Smirnov 			   DP_LINK_SCRAMBLING_DISABLE |
1046f9538357STomi Valkeinen 			   DP_TRAINING_PATTERN_2);
10476d0c3831SAndrey Smirnov 	if (ret)
10486d0c3831SAndrey Smirnov 		return ret;
1049f9538357STomi Valkeinen 
10506d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_SRCCTRL,
10516d0c3831SAndrey Smirnov 			   tc_srcctrl(tc) | DP0_SRCCTRL_SCRMBLDIS |
10526d0c3831SAndrey Smirnov 			   DP0_SRCCTRL_AUTOCORRECT |
10536d0c3831SAndrey Smirnov 			   DP0_SRCCTRL_TP2);
10546d0c3831SAndrey Smirnov 	if (ret)
10556d0c3831SAndrey Smirnov 		return ret;
1056f9538357STomi Valkeinen 
1057f9538357STomi Valkeinen 	/* wait */
1058f9538357STomi Valkeinen 	ret = tc_wait_link_training(tc);
1059f9538357STomi Valkeinen 	if (ret < 0)
10606d0c3831SAndrey Smirnov 		return ret;
1061f9538357STomi Valkeinen 
1062f9538357STomi Valkeinen 	if (ret) {
1063f9538357STomi Valkeinen 		dev_err(tc->dev, "Link training phase 2 failed: %s\n",
1064f9538357STomi Valkeinen 			training_pattern2_errors[ret]);
10656d0c3831SAndrey Smirnov 		return -ENODEV;
1066f9538357STomi Valkeinen 	}
10677caff0fcSAndrey Gusakov 
10680776a269STomi Valkeinen 	/*
10690776a269STomi Valkeinen 	 * Toshiba's documentation suggests to first clear DPCD 0x102, then
10700776a269STomi Valkeinen 	 * clear the training pattern bit in DP0_SRCCTRL. Testing shows
10710776a269STomi Valkeinen 	 * that the link sometimes drops if those steps are done in that order,
10720776a269STomi Valkeinen 	 * but if the steps are done in reverse order, the link stays up.
10730776a269STomi Valkeinen 	 *
10740776a269STomi Valkeinen 	 * So we do the steps differently than documented here.
10750776a269STomi Valkeinen 	 */
10760776a269STomi Valkeinen 
10770776a269STomi Valkeinen 	/* Clear Training Pattern, set AutoCorrect Mode = 1 */
10786d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_SRCCTRL, tc_srcctrl(tc) |
10796d0c3831SAndrey Smirnov 			   DP0_SRCCTRL_AUTOCORRECT);
10806d0c3831SAndrey Smirnov 	if (ret)
10816d0c3831SAndrey Smirnov 		return ret;
10820776a269STomi Valkeinen 
10837caff0fcSAndrey Gusakov 	/* Clear DPCD 0x102 */
10847caff0fcSAndrey Gusakov 	/* Note: Can Not use DP0_SNKLTCTRL (0x06E4) short cut */
10857caff0fcSAndrey Gusakov 	tmp[0] = tc->link.scrambler_dis ? DP_LINK_SCRAMBLING_DISABLE : 0x00;
10867caff0fcSAndrey Gusakov 	ret = drm_dp_dpcd_writeb(aux, DP_TRAINING_PATTERN_SET, tmp[0]);
10877caff0fcSAndrey Gusakov 	if (ret < 0)
10887caff0fcSAndrey Gusakov 		goto err_dpcd_write;
10897caff0fcSAndrey Gusakov 
10900bf25146STomi Valkeinen 	/* Check link status */
10910bf25146STomi Valkeinen 	ret = drm_dp_dpcd_read_link_status(aux, tmp);
10927caff0fcSAndrey Gusakov 	if (ret < 0)
10937caff0fcSAndrey Gusakov 		goto err_dpcd_read;
10947caff0fcSAndrey Gusakov 
10950bf25146STomi Valkeinen 	ret = 0;
10967caff0fcSAndrey Gusakov 
10970bf25146STomi Valkeinen 	value = tmp[0] & DP_CHANNEL_EQ_BITS;
10980bf25146STomi Valkeinen 
10990bf25146STomi Valkeinen 	if (value != DP_CHANNEL_EQ_BITS) {
11000bf25146STomi Valkeinen 		dev_err(tc->dev, "Lane 0 failed: %x\n", value);
11010bf25146STomi Valkeinen 		ret = -ENODEV;
11020bf25146STomi Valkeinen 	}
11030bf25146STomi Valkeinen 
11040bf25146STomi Valkeinen 	if (tc->link.base.num_lanes == 2) {
11050bf25146STomi Valkeinen 		value = (tmp[0] >> 4) & DP_CHANNEL_EQ_BITS;
11060bf25146STomi Valkeinen 
11070bf25146STomi Valkeinen 		if (value != DP_CHANNEL_EQ_BITS) {
11080bf25146STomi Valkeinen 			dev_err(tc->dev, "Lane 1 failed: %x\n", value);
11090bf25146STomi Valkeinen 			ret = -ENODEV;
11100bf25146STomi Valkeinen 		}
11110bf25146STomi Valkeinen 
11120bf25146STomi Valkeinen 		if (!(tmp[2] & DP_INTERLANE_ALIGN_DONE)) {
11130bf25146STomi Valkeinen 			dev_err(tc->dev, "Interlane align failed\n");
11140bf25146STomi Valkeinen 			ret = -ENODEV;
11150bf25146STomi Valkeinen 		}
11160bf25146STomi Valkeinen 	}
11170bf25146STomi Valkeinen 
11180bf25146STomi Valkeinen 	if (ret) {
11190bf25146STomi Valkeinen 		dev_err(dev, "0x0202 LANE0_1_STATUS:            0x%02x\n", tmp[0]);
11200bf25146STomi Valkeinen 		dev_err(dev, "0x0203 LANE2_3_STATUS             0x%02x\n", tmp[1]);
11210bf25146STomi Valkeinen 		dev_err(dev, "0x0204 LANE_ALIGN_STATUS_UPDATED: 0x%02x\n", tmp[2]);
11220bf25146STomi Valkeinen 		dev_err(dev, "0x0205 SINK_STATUS:               0x%02x\n", tmp[3]);
11230bf25146STomi Valkeinen 		dev_err(dev, "0x0206 ADJUST_REQUEST_LANE0_1:    0x%02x\n", tmp[4]);
11240bf25146STomi Valkeinen 		dev_err(dev, "0x0207 ADJUST_REQUEST_LANE2_3:    0x%02x\n", tmp[5]);
11256d0c3831SAndrey Smirnov 		return ret;
11267caff0fcSAndrey Gusakov 	}
11277caff0fcSAndrey Gusakov 
11287caff0fcSAndrey Gusakov 	return 0;
11297caff0fcSAndrey Gusakov err_dpcd_read:
11307caff0fcSAndrey Gusakov 	dev_err(tc->dev, "Failed to read DPCD: %d\n", ret);
11317caff0fcSAndrey Gusakov 	return ret;
11327caff0fcSAndrey Gusakov err_dpcd_write:
11337caff0fcSAndrey Gusakov 	dev_err(tc->dev, "Failed to write DPCD: %d\n", ret);
11347caff0fcSAndrey Gusakov 	return ret;
11357caff0fcSAndrey Gusakov }
11367caff0fcSAndrey Gusakov 
1137cb3263b2STomi Valkeinen static int tc_main_link_disable(struct tc_data *tc)
1138cb3263b2STomi Valkeinen {
1139cb3263b2STomi Valkeinen 	int ret;
1140cb3263b2STomi Valkeinen 
1141cb3263b2STomi Valkeinen 	dev_dbg(tc->dev, "link disable\n");
1142cb3263b2STomi Valkeinen 
11436d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0_SRCCTRL, 0);
11446d0c3831SAndrey Smirnov 	if (ret)
1145cb3263b2STomi Valkeinen 		return ret;
11466d0c3831SAndrey Smirnov 
11476d0c3831SAndrey Smirnov 	return regmap_write(tc->regmap, DP0CTL, 0);
1148cb3263b2STomi Valkeinen }
1149cb3263b2STomi Valkeinen 
115080d57245STomi Valkeinen static int tc_stream_enable(struct tc_data *tc)
11517caff0fcSAndrey Gusakov {
11527caff0fcSAndrey Gusakov 	int ret;
11537caff0fcSAndrey Gusakov 	u32 value;
11547caff0fcSAndrey Gusakov 
115580d57245STomi Valkeinen 	dev_dbg(tc->dev, "enable video stream\n");
11567caff0fcSAndrey Gusakov 
1157bb248368STomi Valkeinen 	/* PXL PLL setup */
1158bb248368STomi Valkeinen 	if (tc_test_pattern) {
1159bb248368STomi Valkeinen 		ret = tc_pxl_pll_en(tc, clk_get_rate(tc->refclk),
116046648a3cSTomi Valkeinen 				    1000 * tc->mode.clock);
1161bb248368STomi Valkeinen 		if (ret)
11626d0c3831SAndrey Smirnov 			return ret;
1163bb248368STomi Valkeinen 	}
1164bb248368STomi Valkeinen 
116546648a3cSTomi Valkeinen 	ret = tc_set_video_mode(tc, &tc->mode);
11665761a259STomi Valkeinen 	if (ret)
116780d57245STomi Valkeinen 		return ret;
11685761a259STomi Valkeinen 
11695761a259STomi Valkeinen 	/* Set M/N */
11705761a259STomi Valkeinen 	ret = tc_stream_clock_calc(tc);
11715761a259STomi Valkeinen 	if (ret)
117280d57245STomi Valkeinen 		return ret;
11735761a259STomi Valkeinen 
11747caff0fcSAndrey Gusakov 	value = VID_MN_GEN | DP_EN;
11757caff0fcSAndrey Gusakov 	if (tc->link.base.capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
11767caff0fcSAndrey Gusakov 		value |= EF_EN;
11776d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0CTL, value);
11786d0c3831SAndrey Smirnov 	if (ret)
11796d0c3831SAndrey Smirnov 		return ret;
11807caff0fcSAndrey Gusakov 	/*
11817caff0fcSAndrey Gusakov 	 * VID_EN assertion should be delayed by at least N * LSCLK
11827caff0fcSAndrey Gusakov 	 * cycles from the time VID_MN_GEN is enabled in order to
11837caff0fcSAndrey Gusakov 	 * generate stable values for VID_M. LSCLK is 270 MHz or
11847caff0fcSAndrey Gusakov 	 * 162 MHz, VID_N is set to 32768 in  tc_stream_clock_calc(),
11857caff0fcSAndrey Gusakov 	 * so a delay of at least 203 us should suffice.
11867caff0fcSAndrey Gusakov 	 */
11877caff0fcSAndrey Gusakov 	usleep_range(500, 1000);
11887caff0fcSAndrey Gusakov 	value |= VID_EN;
11896d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, DP0CTL, value);
11906d0c3831SAndrey Smirnov 	if (ret)
11916d0c3831SAndrey Smirnov 		return ret;
11927caff0fcSAndrey Gusakov 	/* Set input interface */
11937caff0fcSAndrey Gusakov 	value = DP0_AUDSRC_NO_INPUT;
11947caff0fcSAndrey Gusakov 	if (tc_test_pattern)
11957caff0fcSAndrey Gusakov 		value |= DP0_VIDSRC_COLOR_BAR;
11967caff0fcSAndrey Gusakov 	else
11977caff0fcSAndrey Gusakov 		value |= DP0_VIDSRC_DPI_RX;
11986d0c3831SAndrey Smirnov 	ret = regmap_write(tc->regmap, SYSCTRL, value);
11996d0c3831SAndrey Smirnov 	if (ret)
12006d0c3831SAndrey Smirnov 		return ret;
120180d57245STomi Valkeinen 
120280d57245STomi Valkeinen 	return 0;
12037caff0fcSAndrey Gusakov }
12047caff0fcSAndrey Gusakov 
120580d57245STomi Valkeinen static int tc_stream_disable(struct tc_data *tc)
120680d57245STomi Valkeinen {
120780d57245STomi Valkeinen 	int ret;
120880d57245STomi Valkeinen 
120980d57245STomi Valkeinen 	dev_dbg(tc->dev, "disable video stream\n");
121080d57245STomi Valkeinen 
12116d0c3831SAndrey Smirnov 	ret = regmap_update_bits(tc->regmap, DP0CTL, VID_EN, 0);
12126d0c3831SAndrey Smirnov 	if (ret)
12136d0c3831SAndrey Smirnov 		return ret;
121480d57245STomi Valkeinen 
1215bb248368STomi Valkeinen 	tc_pxl_pll_dis(tc);
1216bb248368STomi Valkeinen 
12177caff0fcSAndrey Gusakov 	return 0;
12187caff0fcSAndrey Gusakov }
12197caff0fcSAndrey Gusakov 
12207caff0fcSAndrey Gusakov static void tc_bridge_pre_enable(struct drm_bridge *bridge)
12217caff0fcSAndrey Gusakov {
12227caff0fcSAndrey Gusakov 	struct tc_data *tc = bridge_to_tc(bridge);
12237caff0fcSAndrey Gusakov 
12247caff0fcSAndrey Gusakov 	drm_panel_prepare(tc->panel);
12257caff0fcSAndrey Gusakov }
12267caff0fcSAndrey Gusakov 
12277caff0fcSAndrey Gusakov static void tc_bridge_enable(struct drm_bridge *bridge)
12287caff0fcSAndrey Gusakov {
12297caff0fcSAndrey Gusakov 	struct tc_data *tc = bridge_to_tc(bridge);
12307caff0fcSAndrey Gusakov 	int ret;
12317caff0fcSAndrey Gusakov 
1232f25ee501STomi Valkeinen 	ret = tc_get_display_props(tc);
1233f25ee501STomi Valkeinen 	if (ret < 0) {
1234f25ee501STomi Valkeinen 		dev_err(tc->dev, "failed to read display props: %d\n", ret);
1235f25ee501STomi Valkeinen 		return;
1236f25ee501STomi Valkeinen 	}
1237f25ee501STomi Valkeinen 
1238cb3263b2STomi Valkeinen 	ret = tc_main_link_enable(tc);
12397caff0fcSAndrey Gusakov 	if (ret < 0) {
1240cb3263b2STomi Valkeinen 		dev_err(tc->dev, "main link enable error: %d\n", ret);
12417caff0fcSAndrey Gusakov 		return;
12427caff0fcSAndrey Gusakov 	}
12437caff0fcSAndrey Gusakov 
124480d57245STomi Valkeinen 	ret = tc_stream_enable(tc);
12457caff0fcSAndrey Gusakov 	if (ret < 0) {
12467caff0fcSAndrey Gusakov 		dev_err(tc->dev, "main link stream start error: %d\n", ret);
1247cb3263b2STomi Valkeinen 		tc_main_link_disable(tc);
12487caff0fcSAndrey Gusakov 		return;
12497caff0fcSAndrey Gusakov 	}
12507caff0fcSAndrey Gusakov 
12517caff0fcSAndrey Gusakov 	drm_panel_enable(tc->panel);
12527caff0fcSAndrey Gusakov }
12537caff0fcSAndrey Gusakov 
12547caff0fcSAndrey Gusakov static void tc_bridge_disable(struct drm_bridge *bridge)
12557caff0fcSAndrey Gusakov {
12567caff0fcSAndrey Gusakov 	struct tc_data *tc = bridge_to_tc(bridge);
12577caff0fcSAndrey Gusakov 	int ret;
12587caff0fcSAndrey Gusakov 
12597caff0fcSAndrey Gusakov 	drm_panel_disable(tc->panel);
12607caff0fcSAndrey Gusakov 
126180d57245STomi Valkeinen 	ret = tc_stream_disable(tc);
12627caff0fcSAndrey Gusakov 	if (ret < 0)
12637caff0fcSAndrey Gusakov 		dev_err(tc->dev, "main link stream stop error: %d\n", ret);
1264cb3263b2STomi Valkeinen 
1265cb3263b2STomi Valkeinen 	ret = tc_main_link_disable(tc);
1266cb3263b2STomi Valkeinen 	if (ret < 0)
1267cb3263b2STomi Valkeinen 		dev_err(tc->dev, "main link disable error: %d\n", ret);
12687caff0fcSAndrey Gusakov }
12697caff0fcSAndrey Gusakov 
12707caff0fcSAndrey Gusakov static void tc_bridge_post_disable(struct drm_bridge *bridge)
12717caff0fcSAndrey Gusakov {
12727caff0fcSAndrey Gusakov 	struct tc_data *tc = bridge_to_tc(bridge);
12737caff0fcSAndrey Gusakov 
12747caff0fcSAndrey Gusakov 	drm_panel_unprepare(tc->panel);
12757caff0fcSAndrey Gusakov }
12767caff0fcSAndrey Gusakov 
12777caff0fcSAndrey Gusakov static bool tc_bridge_mode_fixup(struct drm_bridge *bridge,
12787caff0fcSAndrey Gusakov 				 const struct drm_display_mode *mode,
12797caff0fcSAndrey Gusakov 				 struct drm_display_mode *adj)
12807caff0fcSAndrey Gusakov {
12817caff0fcSAndrey Gusakov 	/* Fixup sync polarities, both hsync and vsync are active low */
12827caff0fcSAndrey Gusakov 	adj->flags = mode->flags;
12837caff0fcSAndrey Gusakov 	adj->flags |= (DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC);
12847caff0fcSAndrey Gusakov 	adj->flags &= ~(DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC);
12857caff0fcSAndrey Gusakov 
12867caff0fcSAndrey Gusakov 	return true;
12877caff0fcSAndrey Gusakov }
12887caff0fcSAndrey Gusakov 
12894647a64fSTomi Valkeinen static enum drm_mode_status tc_mode_valid(struct drm_bridge *bridge,
12904647a64fSTomi Valkeinen 					  const struct drm_display_mode *mode)
12917caff0fcSAndrey Gusakov {
12924647a64fSTomi Valkeinen 	struct tc_data *tc = bridge_to_tc(bridge);
129351b9e62eSTomi Valkeinen 	u32 req, avail;
129451b9e62eSTomi Valkeinen 	u32 bits_per_pixel = 24;
129551b9e62eSTomi Valkeinen 
129699fc8e96SAndrey Gusakov 	/* DPI interface clock limitation: upto 154 MHz */
129799fc8e96SAndrey Gusakov 	if (mode->clock > 154000)
129899fc8e96SAndrey Gusakov 		return MODE_CLOCK_HIGH;
129999fc8e96SAndrey Gusakov 
130051b9e62eSTomi Valkeinen 	req = mode->clock * bits_per_pixel / 8;
130151b9e62eSTomi Valkeinen 	avail = tc->link.base.num_lanes * tc->link.base.rate;
130251b9e62eSTomi Valkeinen 
130351b9e62eSTomi Valkeinen 	if (req > avail)
130451b9e62eSTomi Valkeinen 		return MODE_BAD;
130551b9e62eSTomi Valkeinen 
13067caff0fcSAndrey Gusakov 	return MODE_OK;
13077caff0fcSAndrey Gusakov }
13087caff0fcSAndrey Gusakov 
13097caff0fcSAndrey Gusakov static void tc_bridge_mode_set(struct drm_bridge *bridge,
131063f8f3baSLaurent Pinchart 			       const struct drm_display_mode *mode,
131163f8f3baSLaurent Pinchart 			       const struct drm_display_mode *adj)
13127caff0fcSAndrey Gusakov {
13137caff0fcSAndrey Gusakov 	struct tc_data *tc = bridge_to_tc(bridge);
13147caff0fcSAndrey Gusakov 
131546648a3cSTomi Valkeinen 	tc->mode = *mode;
13167caff0fcSAndrey Gusakov }
13177caff0fcSAndrey Gusakov 
13187caff0fcSAndrey Gusakov static int tc_connector_get_modes(struct drm_connector *connector)
13197caff0fcSAndrey Gusakov {
13207caff0fcSAndrey Gusakov 	struct tc_data *tc = connector_to_tc(connector);
13217caff0fcSAndrey Gusakov 	struct edid *edid;
13227caff0fcSAndrey Gusakov 	unsigned int count;
132332315730STomi Valkeinen 	int ret;
132432315730STomi Valkeinen 
132532315730STomi Valkeinen 	ret = tc_get_display_props(tc);
132632315730STomi Valkeinen 	if (ret < 0) {
132732315730STomi Valkeinen 		dev_err(tc->dev, "failed to read display props: %d\n", ret);
132832315730STomi Valkeinen 		return 0;
132932315730STomi Valkeinen 	}
13307caff0fcSAndrey Gusakov 
13317caff0fcSAndrey Gusakov 	if (tc->panel && tc->panel->funcs && tc->panel->funcs->get_modes) {
13327caff0fcSAndrey Gusakov 		count = tc->panel->funcs->get_modes(tc->panel);
13337caff0fcSAndrey Gusakov 		if (count > 0)
13347caff0fcSAndrey Gusakov 			return count;
13357caff0fcSAndrey Gusakov 	}
13367caff0fcSAndrey Gusakov 
13377caff0fcSAndrey Gusakov 	edid = drm_get_edid(connector, &tc->aux.ddc);
13387caff0fcSAndrey Gusakov 
13397caff0fcSAndrey Gusakov 	kfree(tc->edid);
13407caff0fcSAndrey Gusakov 	tc->edid = edid;
13417caff0fcSAndrey Gusakov 	if (!edid)
13427caff0fcSAndrey Gusakov 		return 0;
13437caff0fcSAndrey Gusakov 
1344c555f023SDaniel Vetter 	drm_connector_update_edid_property(connector, edid);
13457caff0fcSAndrey Gusakov 	count = drm_add_edid_modes(connector, edid);
13467caff0fcSAndrey Gusakov 
13477caff0fcSAndrey Gusakov 	return count;
13487caff0fcSAndrey Gusakov }
13497caff0fcSAndrey Gusakov 
13507caff0fcSAndrey Gusakov static const struct drm_connector_helper_funcs tc_connector_helper_funcs = {
13517caff0fcSAndrey Gusakov 	.get_modes = tc_connector_get_modes,
13527caff0fcSAndrey Gusakov };
13537caff0fcSAndrey Gusakov 
1354f25ee501STomi Valkeinen static enum drm_connector_status tc_connector_detect(struct drm_connector *connector,
1355f25ee501STomi Valkeinen 						     bool force)
1356f25ee501STomi Valkeinen {
1357f25ee501STomi Valkeinen 	struct tc_data *tc = connector_to_tc(connector);
1358f25ee501STomi Valkeinen 	bool conn;
1359f25ee501STomi Valkeinen 	u32 val;
1360f25ee501STomi Valkeinen 	int ret;
1361f25ee501STomi Valkeinen 
1362f25ee501STomi Valkeinen 	if (tc->hpd_pin < 0) {
1363f25ee501STomi Valkeinen 		if (tc->panel)
1364f25ee501STomi Valkeinen 			return connector_status_connected;
1365f25ee501STomi Valkeinen 		else
1366f25ee501STomi Valkeinen 			return connector_status_unknown;
1367f25ee501STomi Valkeinen 	}
1368f25ee501STomi Valkeinen 
13696d0c3831SAndrey Smirnov 	ret = regmap_read(tc->regmap, GPIOI, &val);
13706d0c3831SAndrey Smirnov 	if (ret)
13716d0c3831SAndrey Smirnov 		return connector_status_unknown;
1372f25ee501STomi Valkeinen 
1373f25ee501STomi Valkeinen 	conn = val & BIT(tc->hpd_pin);
1374f25ee501STomi Valkeinen 
1375f25ee501STomi Valkeinen 	if (conn)
1376f25ee501STomi Valkeinen 		return connector_status_connected;
1377f25ee501STomi Valkeinen 	else
1378f25ee501STomi Valkeinen 		return connector_status_disconnected;
1379f25ee501STomi Valkeinen }
1380f25ee501STomi Valkeinen 
13817caff0fcSAndrey Gusakov static const struct drm_connector_funcs tc_connector_funcs = {
1382f25ee501STomi Valkeinen 	.detect = tc_connector_detect,
13837caff0fcSAndrey Gusakov 	.fill_modes = drm_helper_probe_single_connector_modes,
1384fdd8326aSMarek Vasut 	.destroy = drm_connector_cleanup,
13857caff0fcSAndrey Gusakov 	.reset = drm_atomic_helper_connector_reset,
13867caff0fcSAndrey Gusakov 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
13877caff0fcSAndrey Gusakov 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
13887caff0fcSAndrey Gusakov };
13897caff0fcSAndrey Gusakov 
13907caff0fcSAndrey Gusakov static int tc_bridge_attach(struct drm_bridge *bridge)
13917caff0fcSAndrey Gusakov {
13927caff0fcSAndrey Gusakov 	u32 bus_format = MEDIA_BUS_FMT_RGB888_1X24;
13937caff0fcSAndrey Gusakov 	struct tc_data *tc = bridge_to_tc(bridge);
13947caff0fcSAndrey Gusakov 	struct drm_device *drm = bridge->dev;
13957caff0fcSAndrey Gusakov 	int ret;
13967caff0fcSAndrey Gusakov 
1397f25ee501STomi Valkeinen 	/* Create DP/eDP connector */
13987caff0fcSAndrey Gusakov 	drm_connector_helper_add(&tc->connector, &tc_connector_helper_funcs);
13997caff0fcSAndrey Gusakov 	ret = drm_connector_init(drm, &tc->connector, &tc_connector_funcs,
1400f8c15790STomi Valkeinen 				 tc->panel ? DRM_MODE_CONNECTOR_eDP :
1401f8c15790STomi Valkeinen 				 DRM_MODE_CONNECTOR_DisplayPort);
14027caff0fcSAndrey Gusakov 	if (ret)
14037caff0fcSAndrey Gusakov 		return ret;
14047caff0fcSAndrey Gusakov 
1405f25ee501STomi Valkeinen 	/* Don't poll if don't have HPD connected */
1406f25ee501STomi Valkeinen 	if (tc->hpd_pin >= 0) {
1407f25ee501STomi Valkeinen 		if (tc->have_irq)
1408f25ee501STomi Valkeinen 			tc->connector.polled = DRM_CONNECTOR_POLL_HPD;
1409f25ee501STomi Valkeinen 		else
1410f25ee501STomi Valkeinen 			tc->connector.polled = DRM_CONNECTOR_POLL_CONNECT |
1411f25ee501STomi Valkeinen 					       DRM_CONNECTOR_POLL_DISCONNECT;
1412f25ee501STomi Valkeinen 	}
1413f25ee501STomi Valkeinen 
14147caff0fcSAndrey Gusakov 	if (tc->panel)
14157caff0fcSAndrey Gusakov 		drm_panel_attach(tc->panel, &tc->connector);
14167caff0fcSAndrey Gusakov 
14177caff0fcSAndrey Gusakov 	drm_display_info_set_bus_formats(&tc->connector.display_info,
14187caff0fcSAndrey Gusakov 					 &bus_format, 1);
14194842379cSTomi Valkeinen 	tc->connector.display_info.bus_flags =
14204842379cSTomi Valkeinen 		DRM_BUS_FLAG_DE_HIGH |
142188bc4178SLaurent Pinchart 		DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE |
142288bc4178SLaurent Pinchart 		DRM_BUS_FLAG_SYNC_DRIVE_NEGEDGE;
1423cde4c44dSDaniel Vetter 	drm_connector_attach_encoder(&tc->connector, tc->bridge.encoder);
14247caff0fcSAndrey Gusakov 
14257caff0fcSAndrey Gusakov 	return 0;
14267caff0fcSAndrey Gusakov }
14277caff0fcSAndrey Gusakov 
14287caff0fcSAndrey Gusakov static const struct drm_bridge_funcs tc_bridge_funcs = {
14297caff0fcSAndrey Gusakov 	.attach = tc_bridge_attach,
14304647a64fSTomi Valkeinen 	.mode_valid = tc_mode_valid,
14317caff0fcSAndrey Gusakov 	.mode_set = tc_bridge_mode_set,
14327caff0fcSAndrey Gusakov 	.pre_enable = tc_bridge_pre_enable,
14337caff0fcSAndrey Gusakov 	.enable = tc_bridge_enable,
14347caff0fcSAndrey Gusakov 	.disable = tc_bridge_disable,
14357caff0fcSAndrey Gusakov 	.post_disable = tc_bridge_post_disable,
14367caff0fcSAndrey Gusakov 	.mode_fixup = tc_bridge_mode_fixup,
14377caff0fcSAndrey Gusakov };
14387caff0fcSAndrey Gusakov 
14397caff0fcSAndrey Gusakov static bool tc_readable_reg(struct device *dev, unsigned int reg)
14407caff0fcSAndrey Gusakov {
14417caff0fcSAndrey Gusakov 	return reg != SYSCTRL;
14427caff0fcSAndrey Gusakov }
14437caff0fcSAndrey Gusakov 
14447caff0fcSAndrey Gusakov static const struct regmap_range tc_volatile_ranges[] = {
14457caff0fcSAndrey Gusakov 	regmap_reg_range(DP0_AUXWDATA(0), DP0_AUXSTATUS),
14467caff0fcSAndrey Gusakov 	regmap_reg_range(DP0_LTSTAT, DP0_SNKLTCHGREQ),
14477caff0fcSAndrey Gusakov 	regmap_reg_range(DP_PHY_CTRL, DP_PHY_CTRL),
14487caff0fcSAndrey Gusakov 	regmap_reg_range(DP0_PLLCTRL, PXL_PLLCTRL),
14497caff0fcSAndrey Gusakov 	regmap_reg_range(VFUEN0, VFUEN0),
1450af9526f2STomi Valkeinen 	regmap_reg_range(INTSTS_G, INTSTS_G),
1451af9526f2STomi Valkeinen 	regmap_reg_range(GPIOI, GPIOI),
14527caff0fcSAndrey Gusakov };
14537caff0fcSAndrey Gusakov 
14547caff0fcSAndrey Gusakov static const struct regmap_access_table tc_volatile_table = {
14557caff0fcSAndrey Gusakov 	.yes_ranges = tc_volatile_ranges,
14567caff0fcSAndrey Gusakov 	.n_yes_ranges = ARRAY_SIZE(tc_volatile_ranges),
14577caff0fcSAndrey Gusakov };
14587caff0fcSAndrey Gusakov 
14597caff0fcSAndrey Gusakov static bool tc_writeable_reg(struct device *dev, unsigned int reg)
14607caff0fcSAndrey Gusakov {
14617caff0fcSAndrey Gusakov 	return (reg != TC_IDREG) &&
14627caff0fcSAndrey Gusakov 	       (reg != DP0_LTSTAT) &&
14637caff0fcSAndrey Gusakov 	       (reg != DP0_SNKLTCHGREQ);
14647caff0fcSAndrey Gusakov }
14657caff0fcSAndrey Gusakov 
14667caff0fcSAndrey Gusakov static const struct regmap_config tc_regmap_config = {
14677caff0fcSAndrey Gusakov 	.name = "tc358767",
14687caff0fcSAndrey Gusakov 	.reg_bits = 16,
14697caff0fcSAndrey Gusakov 	.val_bits = 32,
14707caff0fcSAndrey Gusakov 	.reg_stride = 4,
14717caff0fcSAndrey Gusakov 	.max_register = PLL_DBG,
14727caff0fcSAndrey Gusakov 	.cache_type = REGCACHE_RBTREE,
14737caff0fcSAndrey Gusakov 	.readable_reg = tc_readable_reg,
14747caff0fcSAndrey Gusakov 	.volatile_table = &tc_volatile_table,
14757caff0fcSAndrey Gusakov 	.writeable_reg = tc_writeable_reg,
14767caff0fcSAndrey Gusakov 	.reg_format_endian = REGMAP_ENDIAN_BIG,
14777caff0fcSAndrey Gusakov 	.val_format_endian = REGMAP_ENDIAN_LITTLE,
14787caff0fcSAndrey Gusakov };
14797caff0fcSAndrey Gusakov 
1480f25ee501STomi Valkeinen static irqreturn_t tc_irq_handler(int irq, void *arg)
1481f25ee501STomi Valkeinen {
1482f25ee501STomi Valkeinen 	struct tc_data *tc = arg;
1483f25ee501STomi Valkeinen 	u32 val;
1484f25ee501STomi Valkeinen 	int r;
1485f25ee501STomi Valkeinen 
1486f25ee501STomi Valkeinen 	r = regmap_read(tc->regmap, INTSTS_G, &val);
1487f25ee501STomi Valkeinen 	if (r)
1488f25ee501STomi Valkeinen 		return IRQ_NONE;
1489f25ee501STomi Valkeinen 
1490f25ee501STomi Valkeinen 	if (!val)
1491f25ee501STomi Valkeinen 		return IRQ_NONE;
1492f25ee501STomi Valkeinen 
1493f25ee501STomi Valkeinen 	if (val & INT_SYSERR) {
1494f25ee501STomi Valkeinen 		u32 stat = 0;
1495f25ee501STomi Valkeinen 
1496f25ee501STomi Valkeinen 		regmap_read(tc->regmap, SYSSTAT, &stat);
1497f25ee501STomi Valkeinen 
1498f25ee501STomi Valkeinen 		dev_err(tc->dev, "syserr %x\n", stat);
1499f25ee501STomi Valkeinen 	}
1500f25ee501STomi Valkeinen 
1501f25ee501STomi Valkeinen 	if (tc->hpd_pin >= 0 && tc->bridge.dev) {
1502f25ee501STomi Valkeinen 		/*
1503f25ee501STomi Valkeinen 		 * H is triggered when the GPIO goes high.
1504f25ee501STomi Valkeinen 		 *
1505f25ee501STomi Valkeinen 		 * LC is triggered when the GPIO goes low and stays low for
1506f25ee501STomi Valkeinen 		 * the duration of LCNT
1507f25ee501STomi Valkeinen 		 */
1508f25ee501STomi Valkeinen 		bool h = val & INT_GPIO_H(tc->hpd_pin);
1509f25ee501STomi Valkeinen 		bool lc = val & INT_GPIO_LC(tc->hpd_pin);
1510f25ee501STomi Valkeinen 
1511f25ee501STomi Valkeinen 		dev_dbg(tc->dev, "GPIO%d: %s %s\n", tc->hpd_pin,
1512f25ee501STomi Valkeinen 			h ? "H" : "", lc ? "LC" : "");
1513f25ee501STomi Valkeinen 
1514f25ee501STomi Valkeinen 		if (h || lc)
1515f25ee501STomi Valkeinen 			drm_kms_helper_hotplug_event(tc->bridge.dev);
1516f25ee501STomi Valkeinen 	}
1517f25ee501STomi Valkeinen 
1518f25ee501STomi Valkeinen 	regmap_write(tc->regmap, INTSTS_G, val);
1519f25ee501STomi Valkeinen 
1520f25ee501STomi Valkeinen 	return IRQ_HANDLED;
1521f25ee501STomi Valkeinen }
1522f25ee501STomi Valkeinen 
15237caff0fcSAndrey Gusakov static int tc_probe(struct i2c_client *client, const struct i2c_device_id *id)
15247caff0fcSAndrey Gusakov {
15257caff0fcSAndrey Gusakov 	struct device *dev = &client->dev;
15267caff0fcSAndrey Gusakov 	struct tc_data *tc;
15277caff0fcSAndrey Gusakov 	int ret;
15287caff0fcSAndrey Gusakov 
15297caff0fcSAndrey Gusakov 	tc = devm_kzalloc(dev, sizeof(*tc), GFP_KERNEL);
15307caff0fcSAndrey Gusakov 	if (!tc)
15317caff0fcSAndrey Gusakov 		return -ENOMEM;
15327caff0fcSAndrey Gusakov 
15337caff0fcSAndrey Gusakov 	tc->dev = dev;
15347caff0fcSAndrey Gusakov 
15357caff0fcSAndrey Gusakov 	/* port@2 is the output port */
1536ebc94461SRob Herring 	ret = drm_of_find_panel_or_bridge(dev->of_node, 2, 0, &tc->panel, NULL);
1537d630213fSLucas Stach 	if (ret && ret != -ENODEV)
1538ebc94461SRob Herring 		return ret;
15397caff0fcSAndrey Gusakov 
15407caff0fcSAndrey Gusakov 	/* Shut down GPIO is optional */
15417caff0fcSAndrey Gusakov 	tc->sd_gpio = devm_gpiod_get_optional(dev, "shutdown", GPIOD_OUT_HIGH);
15427caff0fcSAndrey Gusakov 	if (IS_ERR(tc->sd_gpio))
15437caff0fcSAndrey Gusakov 		return PTR_ERR(tc->sd_gpio);
15447caff0fcSAndrey Gusakov 
15457caff0fcSAndrey Gusakov 	if (tc->sd_gpio) {
15467caff0fcSAndrey Gusakov 		gpiod_set_value_cansleep(tc->sd_gpio, 0);
15477caff0fcSAndrey Gusakov 		usleep_range(5000, 10000);
15487caff0fcSAndrey Gusakov 	}
15497caff0fcSAndrey Gusakov 
15507caff0fcSAndrey Gusakov 	/* Reset GPIO is optional */
15517caff0fcSAndrey Gusakov 	tc->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
15527caff0fcSAndrey Gusakov 	if (IS_ERR(tc->reset_gpio))
15537caff0fcSAndrey Gusakov 		return PTR_ERR(tc->reset_gpio);
15547caff0fcSAndrey Gusakov 
15557caff0fcSAndrey Gusakov 	if (tc->reset_gpio) {
15567caff0fcSAndrey Gusakov 		gpiod_set_value_cansleep(tc->reset_gpio, 1);
15577caff0fcSAndrey Gusakov 		usleep_range(5000, 10000);
15587caff0fcSAndrey Gusakov 	}
15597caff0fcSAndrey Gusakov 
15607caff0fcSAndrey Gusakov 	tc->refclk = devm_clk_get(dev, "ref");
15617caff0fcSAndrey Gusakov 	if (IS_ERR(tc->refclk)) {
15627caff0fcSAndrey Gusakov 		ret = PTR_ERR(tc->refclk);
15637caff0fcSAndrey Gusakov 		dev_err(dev, "Failed to get refclk: %d\n", ret);
15647caff0fcSAndrey Gusakov 		return ret;
15657caff0fcSAndrey Gusakov 	}
15667caff0fcSAndrey Gusakov 
15677caff0fcSAndrey Gusakov 	tc->regmap = devm_regmap_init_i2c(client, &tc_regmap_config);
15687caff0fcSAndrey Gusakov 	if (IS_ERR(tc->regmap)) {
15697caff0fcSAndrey Gusakov 		ret = PTR_ERR(tc->regmap);
15707caff0fcSAndrey Gusakov 		dev_err(dev, "Failed to initialize regmap: %d\n", ret);
15717caff0fcSAndrey Gusakov 		return ret;
15727caff0fcSAndrey Gusakov 	}
15737caff0fcSAndrey Gusakov 
1574f25ee501STomi Valkeinen 	ret = of_property_read_u32(dev->of_node, "toshiba,hpd-pin",
1575f25ee501STomi Valkeinen 				   &tc->hpd_pin);
1576f25ee501STomi Valkeinen 	if (ret) {
1577f25ee501STomi Valkeinen 		tc->hpd_pin = -ENODEV;
1578f25ee501STomi Valkeinen 	} else {
1579f25ee501STomi Valkeinen 		if (tc->hpd_pin < 0 || tc->hpd_pin > 1) {
1580f25ee501STomi Valkeinen 			dev_err(dev, "failed to parse HPD number\n");
1581f25ee501STomi Valkeinen 			return ret;
1582f25ee501STomi Valkeinen 		}
1583f25ee501STomi Valkeinen 	}
1584f25ee501STomi Valkeinen 
1585f25ee501STomi Valkeinen 	if (client->irq > 0) {
1586f25ee501STomi Valkeinen 		/* enable SysErr */
1587f25ee501STomi Valkeinen 		regmap_write(tc->regmap, INTCTL_G, INT_SYSERR);
1588f25ee501STomi Valkeinen 
1589f25ee501STomi Valkeinen 		ret = devm_request_threaded_irq(dev, client->irq,
1590f25ee501STomi Valkeinen 						NULL, tc_irq_handler,
1591f25ee501STomi Valkeinen 						IRQF_ONESHOT,
1592f25ee501STomi Valkeinen 						"tc358767-irq", tc);
1593f25ee501STomi Valkeinen 		if (ret) {
1594f25ee501STomi Valkeinen 			dev_err(dev, "failed to register dp interrupt\n");
1595f25ee501STomi Valkeinen 			return ret;
1596f25ee501STomi Valkeinen 		}
1597f25ee501STomi Valkeinen 
1598f25ee501STomi Valkeinen 		tc->have_irq = true;
1599f25ee501STomi Valkeinen 	}
1600f25ee501STomi Valkeinen 
16017caff0fcSAndrey Gusakov 	ret = regmap_read(tc->regmap, TC_IDREG, &tc->rev);
16027caff0fcSAndrey Gusakov 	if (ret) {
16037caff0fcSAndrey Gusakov 		dev_err(tc->dev, "can not read device ID: %d\n", ret);
16047caff0fcSAndrey Gusakov 		return ret;
16057caff0fcSAndrey Gusakov 	}
16067caff0fcSAndrey Gusakov 
16077caff0fcSAndrey Gusakov 	if ((tc->rev != 0x6601) && (tc->rev != 0x6603)) {
16087caff0fcSAndrey Gusakov 		dev_err(tc->dev, "invalid device ID: 0x%08x\n", tc->rev);
16097caff0fcSAndrey Gusakov 		return -EINVAL;
16107caff0fcSAndrey Gusakov 	}
16117caff0fcSAndrey Gusakov 
16127caff0fcSAndrey Gusakov 	tc->assr = (tc->rev == 0x6601); /* Enable ASSR for eDP panels */
16137caff0fcSAndrey Gusakov 
1614f25ee501STomi Valkeinen 	if (tc->hpd_pin >= 0) {
1615f25ee501STomi Valkeinen 		u32 lcnt_reg = tc->hpd_pin == 0 ? INT_GP0_LCNT : INT_GP1_LCNT;
1616f25ee501STomi Valkeinen 		u32 h_lc = INT_GPIO_H(tc->hpd_pin) | INT_GPIO_LC(tc->hpd_pin);
1617f25ee501STomi Valkeinen 
1618f25ee501STomi Valkeinen 		/* Set LCNT to 2ms */
1619f25ee501STomi Valkeinen 		regmap_write(tc->regmap, lcnt_reg,
1620f25ee501STomi Valkeinen 			     clk_get_rate(tc->refclk) * 2 / 1000);
1621f25ee501STomi Valkeinen 		/* We need the "alternate" mode for HPD */
1622f25ee501STomi Valkeinen 		regmap_write(tc->regmap, GPIOM, BIT(tc->hpd_pin));
1623f25ee501STomi Valkeinen 
1624f25ee501STomi Valkeinen 		if (tc->have_irq) {
1625f25ee501STomi Valkeinen 			/* enable H & LC */
1626f25ee501STomi Valkeinen 			regmap_update_bits(tc->regmap, INTCTL_G, h_lc, h_lc);
1627f25ee501STomi Valkeinen 		}
1628f25ee501STomi Valkeinen 	}
1629f25ee501STomi Valkeinen 
16307caff0fcSAndrey Gusakov 	ret = tc_aux_link_setup(tc);
16317caff0fcSAndrey Gusakov 	if (ret)
16327caff0fcSAndrey Gusakov 		return ret;
16337caff0fcSAndrey Gusakov 
16347caff0fcSAndrey Gusakov 	/* Register DP AUX channel */
16357caff0fcSAndrey Gusakov 	tc->aux.name = "TC358767 AUX i2c adapter";
16367caff0fcSAndrey Gusakov 	tc->aux.dev = tc->dev;
16377caff0fcSAndrey Gusakov 	tc->aux.transfer = tc_aux_transfer;
16387caff0fcSAndrey Gusakov 	ret = drm_dp_aux_register(&tc->aux);
16397caff0fcSAndrey Gusakov 	if (ret)
16407caff0fcSAndrey Gusakov 		return ret;
16417caff0fcSAndrey Gusakov 
16427caff0fcSAndrey Gusakov 	tc->bridge.funcs = &tc_bridge_funcs;
16437caff0fcSAndrey Gusakov 	tc->bridge.of_node = dev->of_node;
1644dc01732eSInki Dae 	drm_bridge_add(&tc->bridge);
16457caff0fcSAndrey Gusakov 
16467caff0fcSAndrey Gusakov 	i2c_set_clientdata(client, tc);
16477caff0fcSAndrey Gusakov 
16487caff0fcSAndrey Gusakov 	return 0;
16497caff0fcSAndrey Gusakov }
16507caff0fcSAndrey Gusakov 
16517caff0fcSAndrey Gusakov static int tc_remove(struct i2c_client *client)
16527caff0fcSAndrey Gusakov {
16537caff0fcSAndrey Gusakov 	struct tc_data *tc = i2c_get_clientdata(client);
16547caff0fcSAndrey Gusakov 
16557caff0fcSAndrey Gusakov 	drm_bridge_remove(&tc->bridge);
16567caff0fcSAndrey Gusakov 	drm_dp_aux_unregister(&tc->aux);
16577caff0fcSAndrey Gusakov 
16587caff0fcSAndrey Gusakov 	return 0;
16597caff0fcSAndrey Gusakov }
16607caff0fcSAndrey Gusakov 
16617caff0fcSAndrey Gusakov static const struct i2c_device_id tc358767_i2c_ids[] = {
16627caff0fcSAndrey Gusakov 	{ "tc358767", 0 },
16637caff0fcSAndrey Gusakov 	{ }
16647caff0fcSAndrey Gusakov };
16657caff0fcSAndrey Gusakov MODULE_DEVICE_TABLE(i2c, tc358767_i2c_ids);
16667caff0fcSAndrey Gusakov 
16677caff0fcSAndrey Gusakov static const struct of_device_id tc358767_of_ids[] = {
16687caff0fcSAndrey Gusakov 	{ .compatible = "toshiba,tc358767", },
16697caff0fcSAndrey Gusakov 	{ }
16707caff0fcSAndrey Gusakov };
16717caff0fcSAndrey Gusakov MODULE_DEVICE_TABLE(of, tc358767_of_ids);
16727caff0fcSAndrey Gusakov 
16737caff0fcSAndrey Gusakov static struct i2c_driver tc358767_driver = {
16747caff0fcSAndrey Gusakov 	.driver = {
16757caff0fcSAndrey Gusakov 		.name = "tc358767",
16767caff0fcSAndrey Gusakov 		.of_match_table = tc358767_of_ids,
16777caff0fcSAndrey Gusakov 	},
16787caff0fcSAndrey Gusakov 	.id_table = tc358767_i2c_ids,
16797caff0fcSAndrey Gusakov 	.probe = tc_probe,
16807caff0fcSAndrey Gusakov 	.remove	= tc_remove,
16817caff0fcSAndrey Gusakov };
16827caff0fcSAndrey Gusakov module_i2c_driver(tc358767_driver);
16837caff0fcSAndrey Gusakov 
16847caff0fcSAndrey Gusakov MODULE_AUTHOR("Andrey Gusakov <andrey.gusakov@cogentembedded.com>");
16857caff0fcSAndrey Gusakov MODULE_DESCRIPTION("tc358767 eDP encoder driver");
16867caff0fcSAndrey Gusakov MODULE_LICENSE("GPL");
1687