xref: /openbmc/linux/drivers/gpu/drm/bridge/tc358767.c (revision 3231573065ad4f4ecc5c9147b24f29f846dc0c2f)
17caff0fcSAndrey Gusakov /*
27caff0fcSAndrey Gusakov  * tc358767 eDP bridge driver
37caff0fcSAndrey Gusakov  *
47caff0fcSAndrey Gusakov  * Copyright (C) 2016 CogentEmbedded Inc
57caff0fcSAndrey Gusakov  * Author: Andrey Gusakov <andrey.gusakov@cogentembedded.com>
67caff0fcSAndrey Gusakov  *
77caff0fcSAndrey Gusakov  * Copyright (C) 2016 Pengutronix, Philipp Zabel <p.zabel@pengutronix.de>
87caff0fcSAndrey Gusakov  *
92f51be09SAndrey Gusakov  * Copyright (C) 2016 Zodiac Inflight Innovations
102f51be09SAndrey Gusakov  *
117caff0fcSAndrey Gusakov  * Initially based on: drivers/gpu/drm/i2c/tda998x_drv.c
127caff0fcSAndrey Gusakov  *
137caff0fcSAndrey Gusakov  * Copyright (C) 2012 Texas Instruments
147caff0fcSAndrey Gusakov  * Author: Rob Clark <robdclark@gmail.com>
157caff0fcSAndrey Gusakov  *
167caff0fcSAndrey Gusakov  * This program is free software; you can redistribute it and/or modify
177caff0fcSAndrey Gusakov  * it under the terms of the GNU General Public License as published by
187caff0fcSAndrey Gusakov  * the Free Software Foundation; either version 2 of the License, or
197caff0fcSAndrey Gusakov  * (at your option) any later version.
207caff0fcSAndrey Gusakov  *
217caff0fcSAndrey Gusakov  * This program is distributed in the hope that it will be useful,
227caff0fcSAndrey Gusakov  * but WITHOUT ANY WARRANTY; without even the implied warranty of
237caff0fcSAndrey Gusakov  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
247caff0fcSAndrey Gusakov  * GNU General Public License for more details.
257caff0fcSAndrey Gusakov  */
267caff0fcSAndrey Gusakov 
277caff0fcSAndrey Gusakov #include <linux/clk.h>
287caff0fcSAndrey Gusakov #include <linux/device.h>
297caff0fcSAndrey Gusakov #include <linux/gpio/consumer.h>
307caff0fcSAndrey Gusakov #include <linux/i2c.h>
317caff0fcSAndrey Gusakov #include <linux/kernel.h>
327caff0fcSAndrey Gusakov #include <linux/module.h>
337caff0fcSAndrey Gusakov #include <linux/regmap.h>
347caff0fcSAndrey Gusakov #include <linux/slab.h>
357caff0fcSAndrey Gusakov 
367caff0fcSAndrey Gusakov #include <drm/drm_atomic_helper.h>
377caff0fcSAndrey Gusakov #include <drm/drm_dp_helper.h>
387caff0fcSAndrey Gusakov #include <drm/drm_edid.h>
397caff0fcSAndrey Gusakov #include <drm/drm_of.h>
407caff0fcSAndrey Gusakov #include <drm/drm_panel.h>
41fcd70cd3SDaniel Vetter #include <drm/drm_probe_helper.h>
427caff0fcSAndrey Gusakov 
437caff0fcSAndrey Gusakov /* Registers */
447caff0fcSAndrey Gusakov 
457caff0fcSAndrey Gusakov /* Display Parallel Interface */
467caff0fcSAndrey Gusakov #define DPIPXLFMT		0x0440
477caff0fcSAndrey Gusakov #define VS_POL_ACTIVE_LOW		(1 << 10)
487caff0fcSAndrey Gusakov #define HS_POL_ACTIVE_LOW		(1 << 9)
497caff0fcSAndrey Gusakov #define DE_POL_ACTIVE_HIGH		(0 << 8)
507caff0fcSAndrey Gusakov #define SUB_CFG_TYPE_CONFIG1		(0 << 2) /* LSB aligned */
517caff0fcSAndrey Gusakov #define SUB_CFG_TYPE_CONFIG2		(1 << 2) /* Loosely Packed */
527caff0fcSAndrey Gusakov #define SUB_CFG_TYPE_CONFIG3		(2 << 2) /* LSB aligned 8-bit */
537caff0fcSAndrey Gusakov #define DPI_BPP_RGB888			(0 << 0)
547caff0fcSAndrey Gusakov #define DPI_BPP_RGB666			(1 << 0)
557caff0fcSAndrey Gusakov #define DPI_BPP_RGB565			(2 << 0)
567caff0fcSAndrey Gusakov 
577caff0fcSAndrey Gusakov /* Video Path */
587caff0fcSAndrey Gusakov #define VPCTRL0			0x0450
597caff0fcSAndrey Gusakov #define OPXLFMT_RGB666			(0 << 8)
607caff0fcSAndrey Gusakov #define OPXLFMT_RGB888			(1 << 8)
617caff0fcSAndrey Gusakov #define FRMSYNC_DISABLED		(0 << 4) /* Video Timing Gen Disabled */
627caff0fcSAndrey Gusakov #define FRMSYNC_ENABLED			(1 << 4) /* Video Timing Gen Enabled */
637caff0fcSAndrey Gusakov #define MSF_DISABLED			(0 << 0) /* Magic Square FRC disabled */
647caff0fcSAndrey Gusakov #define MSF_ENABLED			(1 << 0) /* Magic Square FRC enabled */
657caff0fcSAndrey Gusakov #define HTIM01			0x0454
667caff0fcSAndrey Gusakov #define HTIM02			0x0458
677caff0fcSAndrey Gusakov #define VTIM01			0x045c
687caff0fcSAndrey Gusakov #define VTIM02			0x0460
697caff0fcSAndrey Gusakov #define VFUEN0			0x0464
707caff0fcSAndrey Gusakov #define VFUEN				BIT(0)   /* Video Frame Timing Upload */
717caff0fcSAndrey Gusakov 
727caff0fcSAndrey Gusakov /* System */
737caff0fcSAndrey Gusakov #define TC_IDREG		0x0500
747caff0fcSAndrey Gusakov #define SYSCTRL			0x0510
757caff0fcSAndrey Gusakov #define DP0_AUDSRC_NO_INPUT		(0 << 3)
767caff0fcSAndrey Gusakov #define DP0_AUDSRC_I2S_RX		(1 << 3)
777caff0fcSAndrey Gusakov #define DP0_VIDSRC_NO_INPUT		(0 << 0)
787caff0fcSAndrey Gusakov #define DP0_VIDSRC_DSI_RX		(1 << 0)
797caff0fcSAndrey Gusakov #define DP0_VIDSRC_DPI_RX		(2 << 0)
807caff0fcSAndrey Gusakov #define DP0_VIDSRC_COLOR_BAR		(3 << 0)
817caff0fcSAndrey Gusakov 
827caff0fcSAndrey Gusakov /* Control */
837caff0fcSAndrey Gusakov #define DP0CTL			0x0600
847caff0fcSAndrey Gusakov #define VID_MN_GEN			BIT(6)   /* Auto-generate M/N values */
857caff0fcSAndrey Gusakov #define EF_EN				BIT(5)   /* Enable Enhanced Framing */
867caff0fcSAndrey Gusakov #define VID_EN				BIT(1)   /* Video transmission enable */
877caff0fcSAndrey Gusakov #define DP_EN				BIT(0)   /* Enable DPTX function */
887caff0fcSAndrey Gusakov 
897caff0fcSAndrey Gusakov /* Clocks */
907caff0fcSAndrey Gusakov #define DP0_VIDMNGEN0		0x0610
917caff0fcSAndrey Gusakov #define DP0_VIDMNGEN1		0x0614
927caff0fcSAndrey Gusakov #define DP0_VMNGENSTATUS	0x0618
937caff0fcSAndrey Gusakov 
947caff0fcSAndrey Gusakov /* Main Channel */
957caff0fcSAndrey Gusakov #define DP0_SECSAMPLE		0x0640
967caff0fcSAndrey Gusakov #define DP0_VIDSYNCDELAY	0x0644
977caff0fcSAndrey Gusakov #define DP0_TOTALVAL		0x0648
987caff0fcSAndrey Gusakov #define DP0_STARTVAL		0x064c
997caff0fcSAndrey Gusakov #define DP0_ACTIVEVAL		0x0650
1007caff0fcSAndrey Gusakov #define DP0_SYNCVAL		0x0654
1017923e09cSTomi Valkeinen #define SYNCVAL_HS_POL_ACTIVE_LOW	(1 << 15)
1027923e09cSTomi Valkeinen #define SYNCVAL_VS_POL_ACTIVE_LOW	(1 << 31)
1037caff0fcSAndrey Gusakov #define DP0_MISC		0x0658
104f3b8adbeSAndrey Gusakov #define TU_SIZE_RECOMMENDED		(63) /* LSCLK cycles per TU */
1057caff0fcSAndrey Gusakov #define BPC_6				(0 << 5)
1067caff0fcSAndrey Gusakov #define BPC_8				(1 << 5)
1077caff0fcSAndrey Gusakov 
1087caff0fcSAndrey Gusakov /* AUX channel */
1097caff0fcSAndrey Gusakov #define DP0_AUXCFG0		0x0660
1107caff0fcSAndrey Gusakov #define DP0_AUXCFG1		0x0664
1117caff0fcSAndrey Gusakov #define AUX_RX_FILTER_EN		BIT(16)
1127caff0fcSAndrey Gusakov 
1137caff0fcSAndrey Gusakov #define DP0_AUXADDR		0x0668
1147caff0fcSAndrey Gusakov #define DP0_AUXWDATA(i)		(0x066c + (i) * 4)
1157caff0fcSAndrey Gusakov #define DP0_AUXRDATA(i)		(0x067c + (i) * 4)
1167caff0fcSAndrey Gusakov #define DP0_AUXSTATUS		0x068c
1177caff0fcSAndrey Gusakov #define AUX_STATUS_MASK			0xf0
1187caff0fcSAndrey Gusakov #define AUX_STATUS_SHIFT		4
1197caff0fcSAndrey Gusakov #define AUX_TIMEOUT			BIT(1)
1207caff0fcSAndrey Gusakov #define AUX_BUSY			BIT(0)
1217caff0fcSAndrey Gusakov #define DP0_AUXI2CADR		0x0698
1227caff0fcSAndrey Gusakov 
1237caff0fcSAndrey Gusakov /* Link Training */
1247caff0fcSAndrey Gusakov #define DP0_SRCCTRL		0x06a0
1257caff0fcSAndrey Gusakov #define DP0_SRCCTRL_SCRMBLDIS		BIT(13)
1267caff0fcSAndrey Gusakov #define DP0_SRCCTRL_EN810B		BIT(12)
1277caff0fcSAndrey Gusakov #define DP0_SRCCTRL_NOTP		(0 << 8)
1287caff0fcSAndrey Gusakov #define DP0_SRCCTRL_TP1			(1 << 8)
1297caff0fcSAndrey Gusakov #define DP0_SRCCTRL_TP2			(2 << 8)
1307caff0fcSAndrey Gusakov #define DP0_SRCCTRL_LANESKEW		BIT(7)
1317caff0fcSAndrey Gusakov #define DP0_SRCCTRL_SSCG		BIT(3)
1327caff0fcSAndrey Gusakov #define DP0_SRCCTRL_LANES_1		(0 << 2)
1337caff0fcSAndrey Gusakov #define DP0_SRCCTRL_LANES_2		(1 << 2)
1347caff0fcSAndrey Gusakov #define DP0_SRCCTRL_BW27		(1 << 1)
1357caff0fcSAndrey Gusakov #define DP0_SRCCTRL_BW162		(0 << 1)
1367caff0fcSAndrey Gusakov #define DP0_SRCCTRL_AUTOCORRECT		BIT(0)
1377caff0fcSAndrey Gusakov #define DP0_LTSTAT		0x06d0
1387caff0fcSAndrey Gusakov #define LT_LOOPDONE			BIT(13)
1397caff0fcSAndrey Gusakov #define LT_STATUS_MASK			(0x1f << 8)
1407caff0fcSAndrey Gusakov #define LT_CHANNEL1_EQ_BITS		(DP_CHANNEL_EQ_BITS << 4)
1417caff0fcSAndrey Gusakov #define LT_INTERLANE_ALIGN_DONE		BIT(3)
1427caff0fcSAndrey Gusakov #define LT_CHANNEL0_EQ_BITS		(DP_CHANNEL_EQ_BITS)
1437caff0fcSAndrey Gusakov #define DP0_SNKLTCHGREQ		0x06d4
1447caff0fcSAndrey Gusakov #define DP0_LTLOOPCTRL		0x06d8
1457caff0fcSAndrey Gusakov #define DP0_SNKLTCTRL		0x06e4
1467caff0fcSAndrey Gusakov 
147adf41098STomi Valkeinen #define DP1_SRCCTRL		0x07a0
148adf41098STomi Valkeinen 
1497caff0fcSAndrey Gusakov /* PHY */
1507caff0fcSAndrey Gusakov #define DP_PHY_CTRL		0x0800
1517caff0fcSAndrey Gusakov #define DP_PHY_RST			BIT(28)  /* DP PHY Global Soft Reset */
1527caff0fcSAndrey Gusakov #define BGREN				BIT(25)  /* AUX PHY BGR Enable */
1537caff0fcSAndrey Gusakov #define PWR_SW_EN			BIT(24)  /* PHY Power Switch Enable */
1547caff0fcSAndrey Gusakov #define PHY_M1_RST			BIT(12)  /* Reset PHY1 Main Channel */
1557caff0fcSAndrey Gusakov #define PHY_RDY				BIT(16)  /* PHY Main Channels Ready */
1567caff0fcSAndrey Gusakov #define PHY_M0_RST			BIT(8)   /* Reset PHY0 Main Channel */
157adf41098STomi Valkeinen #define PHY_2LANE			BIT(2)   /* PHY Enable 2 lanes */
1587caff0fcSAndrey Gusakov #define PHY_A0_EN			BIT(1)   /* PHY Aux Channel0 Enable */
1597caff0fcSAndrey Gusakov #define PHY_M0_EN			BIT(0)   /* PHY Main Channel0 Enable */
1607caff0fcSAndrey Gusakov 
1617caff0fcSAndrey Gusakov /* PLL */
1627caff0fcSAndrey Gusakov #define DP0_PLLCTRL		0x0900
1637caff0fcSAndrey Gusakov #define DP1_PLLCTRL		0x0904	/* not defined in DS */
1647caff0fcSAndrey Gusakov #define PXL_PLLCTRL		0x0908
1657caff0fcSAndrey Gusakov #define PLLUPDATE			BIT(2)
1667caff0fcSAndrey Gusakov #define PLLBYP				BIT(1)
1677caff0fcSAndrey Gusakov #define PLLEN				BIT(0)
1687caff0fcSAndrey Gusakov #define PXL_PLLPARAM		0x0914
1697caff0fcSAndrey Gusakov #define IN_SEL_REFCLK			(0 << 14)
1707caff0fcSAndrey Gusakov #define SYS_PLLPARAM		0x0918
1717caff0fcSAndrey Gusakov #define REF_FREQ_38M4			(0 << 8) /* 38.4 MHz */
1727caff0fcSAndrey Gusakov #define REF_FREQ_19M2			(1 << 8) /* 19.2 MHz */
1737caff0fcSAndrey Gusakov #define REF_FREQ_26M			(2 << 8) /* 26 MHz */
1747caff0fcSAndrey Gusakov #define REF_FREQ_13M			(3 << 8) /* 13 MHz */
1757caff0fcSAndrey Gusakov #define SYSCLK_SEL_LSCLK		(0 << 4)
1767caff0fcSAndrey Gusakov #define LSCLK_DIV_1			(0 << 0)
1777caff0fcSAndrey Gusakov #define LSCLK_DIV_2			(1 << 0)
1787caff0fcSAndrey Gusakov 
1797caff0fcSAndrey Gusakov /* Test & Debug */
1807caff0fcSAndrey Gusakov #define TSTCTL			0x0a00
1817caff0fcSAndrey Gusakov #define PLL_DBG			0x0a04
1827caff0fcSAndrey Gusakov 
1837caff0fcSAndrey Gusakov static bool tc_test_pattern;
1847caff0fcSAndrey Gusakov module_param_named(test, tc_test_pattern, bool, 0644);
1857caff0fcSAndrey Gusakov 
1867caff0fcSAndrey Gusakov struct tc_edp_link {
1877caff0fcSAndrey Gusakov 	struct drm_dp_link	base;
1887caff0fcSAndrey Gusakov 	u8			assr;
189e5607637STomi Valkeinen 	bool			scrambler_dis;
190e5607637STomi Valkeinen 	bool			spread;
1917caff0fcSAndrey Gusakov };
1927caff0fcSAndrey Gusakov 
1937caff0fcSAndrey Gusakov struct tc_data {
1947caff0fcSAndrey Gusakov 	struct device		*dev;
1957caff0fcSAndrey Gusakov 	struct regmap		*regmap;
1967caff0fcSAndrey Gusakov 	struct drm_dp_aux	aux;
1977caff0fcSAndrey Gusakov 
1987caff0fcSAndrey Gusakov 	struct drm_bridge	bridge;
1997caff0fcSAndrey Gusakov 	struct drm_connector	connector;
2007caff0fcSAndrey Gusakov 	struct drm_panel	*panel;
2017caff0fcSAndrey Gusakov 
2027caff0fcSAndrey Gusakov 	/* link settings */
2037caff0fcSAndrey Gusakov 	struct tc_edp_link	link;
2047caff0fcSAndrey Gusakov 
2057caff0fcSAndrey Gusakov 	/* display edid */
2067caff0fcSAndrey Gusakov 	struct edid		*edid;
2077caff0fcSAndrey Gusakov 	/* current mode */
20846648a3cSTomi Valkeinen 	struct drm_display_mode	mode;
2097caff0fcSAndrey Gusakov 
2107caff0fcSAndrey Gusakov 	u32			rev;
2117caff0fcSAndrey Gusakov 	u8			assr;
2127caff0fcSAndrey Gusakov 
2137caff0fcSAndrey Gusakov 	struct gpio_desc	*sd_gpio;
2147caff0fcSAndrey Gusakov 	struct gpio_desc	*reset_gpio;
2157caff0fcSAndrey Gusakov 	struct clk		*refclk;
2167caff0fcSAndrey Gusakov };
2177caff0fcSAndrey Gusakov 
2187caff0fcSAndrey Gusakov static inline struct tc_data *aux_to_tc(struct drm_dp_aux *a)
2197caff0fcSAndrey Gusakov {
2207caff0fcSAndrey Gusakov 	return container_of(a, struct tc_data, aux);
2217caff0fcSAndrey Gusakov }
2227caff0fcSAndrey Gusakov 
2237caff0fcSAndrey Gusakov static inline struct tc_data *bridge_to_tc(struct drm_bridge *b)
2247caff0fcSAndrey Gusakov {
2257caff0fcSAndrey Gusakov 	return container_of(b, struct tc_data, bridge);
2267caff0fcSAndrey Gusakov }
2277caff0fcSAndrey Gusakov 
2287caff0fcSAndrey Gusakov static inline struct tc_data *connector_to_tc(struct drm_connector *c)
2297caff0fcSAndrey Gusakov {
2307caff0fcSAndrey Gusakov 	return container_of(c, struct tc_data, connector);
2317caff0fcSAndrey Gusakov }
2327caff0fcSAndrey Gusakov 
2337caff0fcSAndrey Gusakov /* Simple macros to avoid repeated error checks */
2347caff0fcSAndrey Gusakov #define tc_write(reg, var)					\
2357caff0fcSAndrey Gusakov 	do {							\
2367caff0fcSAndrey Gusakov 		ret = regmap_write(tc->regmap, reg, var);	\
2377caff0fcSAndrey Gusakov 		if (ret)					\
2387caff0fcSAndrey Gusakov 			goto err;				\
2397caff0fcSAndrey Gusakov 	} while (0)
2407caff0fcSAndrey Gusakov #define tc_read(reg, var)					\
2417caff0fcSAndrey Gusakov 	do {							\
2427caff0fcSAndrey Gusakov 		ret = regmap_read(tc->regmap, reg, var);	\
2437caff0fcSAndrey Gusakov 		if (ret)					\
2447caff0fcSAndrey Gusakov 			goto err;				\
2457caff0fcSAndrey Gusakov 	} while (0)
2467caff0fcSAndrey Gusakov 
2477caff0fcSAndrey Gusakov static inline int tc_poll_timeout(struct regmap *map, unsigned int addr,
2487caff0fcSAndrey Gusakov 				  unsigned int cond_mask,
2497caff0fcSAndrey Gusakov 				  unsigned int cond_value,
2507caff0fcSAndrey Gusakov 				  unsigned long sleep_us, u64 timeout_us)
2517caff0fcSAndrey Gusakov {
2527caff0fcSAndrey Gusakov 	ktime_t timeout = ktime_add_us(ktime_get(), timeout_us);
2537caff0fcSAndrey Gusakov 	unsigned int val;
2547caff0fcSAndrey Gusakov 	int ret;
2557caff0fcSAndrey Gusakov 
2567caff0fcSAndrey Gusakov 	for (;;) {
2577caff0fcSAndrey Gusakov 		ret = regmap_read(map, addr, &val);
2587caff0fcSAndrey Gusakov 		if (ret)
2597caff0fcSAndrey Gusakov 			break;
2607caff0fcSAndrey Gusakov 		if ((val & cond_mask) == cond_value)
2617caff0fcSAndrey Gusakov 			break;
2627caff0fcSAndrey Gusakov 		if (timeout_us && ktime_compare(ktime_get(), timeout) > 0) {
2637caff0fcSAndrey Gusakov 			ret = regmap_read(map, addr, &val);
2647caff0fcSAndrey Gusakov 			break;
2657caff0fcSAndrey Gusakov 		}
2667caff0fcSAndrey Gusakov 		if (sleep_us)
2677caff0fcSAndrey Gusakov 			usleep_range((sleep_us >> 2) + 1, sleep_us);
2687caff0fcSAndrey Gusakov 	}
2697caff0fcSAndrey Gusakov 	return ret ?: (((val & cond_mask) == cond_value) ? 0 : -ETIMEDOUT);
2707caff0fcSAndrey Gusakov }
2717caff0fcSAndrey Gusakov 
2727caff0fcSAndrey Gusakov static int tc_aux_wait_busy(struct tc_data *tc, unsigned int timeout_ms)
2737caff0fcSAndrey Gusakov {
2747caff0fcSAndrey Gusakov 	return tc_poll_timeout(tc->regmap, DP0_AUXSTATUS, AUX_BUSY, 0,
2757caff0fcSAndrey Gusakov 			       1000, 1000 * timeout_ms);
2767caff0fcSAndrey Gusakov }
2777caff0fcSAndrey Gusakov 
2787caff0fcSAndrey Gusakov static int tc_aux_get_status(struct tc_data *tc, u8 *reply)
2797caff0fcSAndrey Gusakov {
2807caff0fcSAndrey Gusakov 	int ret;
2817caff0fcSAndrey Gusakov 	u32 value;
2827caff0fcSAndrey Gusakov 
2837caff0fcSAndrey Gusakov 	ret = regmap_read(tc->regmap, DP0_AUXSTATUS, &value);
2847caff0fcSAndrey Gusakov 	if (ret < 0)
2857caff0fcSAndrey Gusakov 		return ret;
286bfb6e014STomi Valkeinen 
2877caff0fcSAndrey Gusakov 	if (value & AUX_BUSY) {
288bfb6e014STomi Valkeinen 		dev_err(tc->dev, "aux busy!\n");
2897caff0fcSAndrey Gusakov 		return -EBUSY;
2907caff0fcSAndrey Gusakov 	}
2917caff0fcSAndrey Gusakov 
292bfb6e014STomi Valkeinen 	if (value & AUX_TIMEOUT) {
293bfb6e014STomi Valkeinen 		dev_err(tc->dev, "aux access timeout!\n");
294bfb6e014STomi Valkeinen 		return -ETIMEDOUT;
295bfb6e014STomi Valkeinen 	}
296bfb6e014STomi Valkeinen 
2977caff0fcSAndrey Gusakov 	*reply = (value & AUX_STATUS_MASK) >> AUX_STATUS_SHIFT;
2987caff0fcSAndrey Gusakov 	return 0;
2997caff0fcSAndrey Gusakov }
3007caff0fcSAndrey Gusakov 
3017caff0fcSAndrey Gusakov static ssize_t tc_aux_transfer(struct drm_dp_aux *aux,
3027caff0fcSAndrey Gusakov 			       struct drm_dp_aux_msg *msg)
3037caff0fcSAndrey Gusakov {
3047caff0fcSAndrey Gusakov 	struct tc_data *tc = aux_to_tc(aux);
3057caff0fcSAndrey Gusakov 	size_t size = min_t(size_t, 8, msg->size);
3067caff0fcSAndrey Gusakov 	u8 request = msg->request & ~DP_AUX_I2C_MOT;
3077caff0fcSAndrey Gusakov 	u8 *buf = msg->buffer;
3087caff0fcSAndrey Gusakov 	u32 tmp = 0;
3097caff0fcSAndrey Gusakov 	int i = 0;
3107caff0fcSAndrey Gusakov 	int ret;
3117caff0fcSAndrey Gusakov 
3127caff0fcSAndrey Gusakov 	if (size == 0)
3137caff0fcSAndrey Gusakov 		return 0;
3147caff0fcSAndrey Gusakov 
3157caff0fcSAndrey Gusakov 	ret = tc_aux_wait_busy(tc, 100);
3167caff0fcSAndrey Gusakov 	if (ret)
3177caff0fcSAndrey Gusakov 		goto err;
3187caff0fcSAndrey Gusakov 
3197caff0fcSAndrey Gusakov 	if (request == DP_AUX_I2C_WRITE || request == DP_AUX_NATIVE_WRITE) {
3207caff0fcSAndrey Gusakov 		/* Store data */
3217caff0fcSAndrey Gusakov 		while (i < size) {
3227caff0fcSAndrey Gusakov 			if (request == DP_AUX_NATIVE_WRITE)
3237caff0fcSAndrey Gusakov 				tmp = tmp | (buf[i] << (8 * (i & 0x3)));
3247caff0fcSAndrey Gusakov 			else
3257caff0fcSAndrey Gusakov 				tmp = (tmp << 8) | buf[i];
3267caff0fcSAndrey Gusakov 			i++;
3277caff0fcSAndrey Gusakov 			if (((i % 4) == 0) || (i == size)) {
3289217c1abSAndrey Gusakov 				tc_write(DP0_AUXWDATA((i - 1) >> 2), tmp);
3297caff0fcSAndrey Gusakov 				tmp = 0;
3307caff0fcSAndrey Gusakov 			}
3317caff0fcSAndrey Gusakov 		}
3327caff0fcSAndrey Gusakov 	} else if (request != DP_AUX_I2C_READ &&
3337caff0fcSAndrey Gusakov 		   request != DP_AUX_NATIVE_READ) {
3347caff0fcSAndrey Gusakov 		return -EINVAL;
3357caff0fcSAndrey Gusakov 	}
3367caff0fcSAndrey Gusakov 
3377caff0fcSAndrey Gusakov 	/* Store address */
3387caff0fcSAndrey Gusakov 	tc_write(DP0_AUXADDR, msg->address);
3397caff0fcSAndrey Gusakov 	/* Start transfer */
3407caff0fcSAndrey Gusakov 	tc_write(DP0_AUXCFG0, ((size - 1) << 8) | request);
3417caff0fcSAndrey Gusakov 
3427caff0fcSAndrey Gusakov 	ret = tc_aux_wait_busy(tc, 100);
3437caff0fcSAndrey Gusakov 	if (ret)
3447caff0fcSAndrey Gusakov 		goto err;
3457caff0fcSAndrey Gusakov 
3467caff0fcSAndrey Gusakov 	ret = tc_aux_get_status(tc, &msg->reply);
3477caff0fcSAndrey Gusakov 	if (ret)
3487caff0fcSAndrey Gusakov 		goto err;
3497caff0fcSAndrey Gusakov 
3507caff0fcSAndrey Gusakov 	if (request == DP_AUX_I2C_READ || request == DP_AUX_NATIVE_READ) {
3517caff0fcSAndrey Gusakov 		/* Read data */
3527caff0fcSAndrey Gusakov 		while (i < size) {
3537caff0fcSAndrey Gusakov 			if ((i % 4) == 0)
3547caff0fcSAndrey Gusakov 				tc_read(DP0_AUXRDATA(i >> 2), &tmp);
3557caff0fcSAndrey Gusakov 			buf[i] = tmp & 0xff;
3567caff0fcSAndrey Gusakov 			tmp = tmp >> 8;
3577caff0fcSAndrey Gusakov 			i++;
3587caff0fcSAndrey Gusakov 		}
3597caff0fcSAndrey Gusakov 	}
3607caff0fcSAndrey Gusakov 
3617caff0fcSAndrey Gusakov 	return size;
3627caff0fcSAndrey Gusakov err:
3637caff0fcSAndrey Gusakov 	return ret;
3647caff0fcSAndrey Gusakov }
3657caff0fcSAndrey Gusakov 
3667caff0fcSAndrey Gusakov static const char * const training_pattern1_errors[] = {
3677caff0fcSAndrey Gusakov 	"No errors",
3687caff0fcSAndrey Gusakov 	"Aux write error",
3697caff0fcSAndrey Gusakov 	"Aux read error",
3707caff0fcSAndrey Gusakov 	"Max voltage reached error",
3717caff0fcSAndrey Gusakov 	"Loop counter expired error",
3727caff0fcSAndrey Gusakov 	"res", "res", "res"
3737caff0fcSAndrey Gusakov };
3747caff0fcSAndrey Gusakov 
3757caff0fcSAndrey Gusakov static const char * const training_pattern2_errors[] = {
3767caff0fcSAndrey Gusakov 	"No errors",
3777caff0fcSAndrey Gusakov 	"Aux write error",
3787caff0fcSAndrey Gusakov 	"Aux read error",
3797caff0fcSAndrey Gusakov 	"Clock recovery failed error",
3807caff0fcSAndrey Gusakov 	"Loop counter expired error",
3817caff0fcSAndrey Gusakov 	"res", "res", "res"
3827caff0fcSAndrey Gusakov };
3837caff0fcSAndrey Gusakov 
3847caff0fcSAndrey Gusakov static u32 tc_srcctrl(struct tc_data *tc)
3857caff0fcSAndrey Gusakov {
3867caff0fcSAndrey Gusakov 	/*
3877caff0fcSAndrey Gusakov 	 * No training pattern, skew lane 1 data by two LSCLK cycles with
3887caff0fcSAndrey Gusakov 	 * respect to lane 0 data, AutoCorrect Mode = 0
3897caff0fcSAndrey Gusakov 	 */
3904b30bf41STomi Valkeinen 	u32 reg = DP0_SRCCTRL_NOTP | DP0_SRCCTRL_LANESKEW | DP0_SRCCTRL_EN810B;
3917caff0fcSAndrey Gusakov 
3927caff0fcSAndrey Gusakov 	if (tc->link.scrambler_dis)
3937caff0fcSAndrey Gusakov 		reg |= DP0_SRCCTRL_SCRMBLDIS;	/* Scrambler Disabled */
3947caff0fcSAndrey Gusakov 	if (tc->link.spread)
3957caff0fcSAndrey Gusakov 		reg |= DP0_SRCCTRL_SSCG;	/* Spread Spectrum Enable */
3967caff0fcSAndrey Gusakov 	if (tc->link.base.num_lanes == 2)
3977caff0fcSAndrey Gusakov 		reg |= DP0_SRCCTRL_LANES_2;	/* Two Main Channel Lanes */
3987caff0fcSAndrey Gusakov 	if (tc->link.base.rate != 162000)
3997caff0fcSAndrey Gusakov 		reg |= DP0_SRCCTRL_BW27;	/* 2.7 Gbps link */
4007caff0fcSAndrey Gusakov 	return reg;
4017caff0fcSAndrey Gusakov }
4027caff0fcSAndrey Gusakov 
4037caff0fcSAndrey Gusakov static void tc_wait_pll_lock(struct tc_data *tc)
4047caff0fcSAndrey Gusakov {
4057caff0fcSAndrey Gusakov 	/* Wait for PLL to lock: up to 2.09 ms, depending on refclk */
4067caff0fcSAndrey Gusakov 	usleep_range(3000, 6000);
4077caff0fcSAndrey Gusakov }
4087caff0fcSAndrey Gusakov 
4097caff0fcSAndrey Gusakov static int tc_pxl_pll_en(struct tc_data *tc, u32 refclk, u32 pixelclock)
4107caff0fcSAndrey Gusakov {
4117caff0fcSAndrey Gusakov 	int ret;
4127caff0fcSAndrey Gusakov 	int i_pre, best_pre = 1;
4137caff0fcSAndrey Gusakov 	int i_post, best_post = 1;
4147caff0fcSAndrey Gusakov 	int div, best_div = 1;
4157caff0fcSAndrey Gusakov 	int mul, best_mul = 1;
4167caff0fcSAndrey Gusakov 	int delta, best_delta;
4177caff0fcSAndrey Gusakov 	int ext_div[] = {1, 2, 3, 5, 7};
4187caff0fcSAndrey Gusakov 	int best_pixelclock = 0;
4197caff0fcSAndrey Gusakov 	int vco_hi = 0;
4207caff0fcSAndrey Gusakov 
4217caff0fcSAndrey Gusakov 	dev_dbg(tc->dev, "PLL: requested %d pixelclock, ref %d\n", pixelclock,
4227caff0fcSAndrey Gusakov 		refclk);
4237caff0fcSAndrey Gusakov 	best_delta = pixelclock;
4247caff0fcSAndrey Gusakov 	/* Loop over all possible ext_divs, skipping invalid configurations */
4257caff0fcSAndrey Gusakov 	for (i_pre = 0; i_pre < ARRAY_SIZE(ext_div); i_pre++) {
4267caff0fcSAndrey Gusakov 		/*
4277caff0fcSAndrey Gusakov 		 * refclk / ext_pre_div should be in the 1 to 200 MHz range.
4287caff0fcSAndrey Gusakov 		 * We don't allow any refclk > 200 MHz, only check lower bounds.
4297caff0fcSAndrey Gusakov 		 */
4307caff0fcSAndrey Gusakov 		if (refclk / ext_div[i_pre] < 1000000)
4317caff0fcSAndrey Gusakov 			continue;
4327caff0fcSAndrey Gusakov 		for (i_post = 0; i_post < ARRAY_SIZE(ext_div); i_post++) {
4337caff0fcSAndrey Gusakov 			for (div = 1; div <= 16; div++) {
4347caff0fcSAndrey Gusakov 				u32 clk;
4357caff0fcSAndrey Gusakov 				u64 tmp;
4367caff0fcSAndrey Gusakov 
4377caff0fcSAndrey Gusakov 				tmp = pixelclock * ext_div[i_pre] *
4387caff0fcSAndrey Gusakov 				      ext_div[i_post] * div;
4397caff0fcSAndrey Gusakov 				do_div(tmp, refclk);
4407caff0fcSAndrey Gusakov 				mul = tmp;
4417caff0fcSAndrey Gusakov 
4427caff0fcSAndrey Gusakov 				/* Check limits */
4437caff0fcSAndrey Gusakov 				if ((mul < 1) || (mul > 128))
4447caff0fcSAndrey Gusakov 					continue;
4457caff0fcSAndrey Gusakov 
4467caff0fcSAndrey Gusakov 				clk = (refclk / ext_div[i_pre] / div) * mul;
4477caff0fcSAndrey Gusakov 				/*
4487caff0fcSAndrey Gusakov 				 * refclk * mul / (ext_pre_div * pre_div)
4497caff0fcSAndrey Gusakov 				 * should be in the 150 to 650 MHz range
4507caff0fcSAndrey Gusakov 				 */
4517caff0fcSAndrey Gusakov 				if ((clk > 650000000) || (clk < 150000000))
4527caff0fcSAndrey Gusakov 					continue;
4537caff0fcSAndrey Gusakov 
4547caff0fcSAndrey Gusakov 				clk = clk / ext_div[i_post];
4557caff0fcSAndrey Gusakov 				delta = clk - pixelclock;
4567caff0fcSAndrey Gusakov 
4577caff0fcSAndrey Gusakov 				if (abs(delta) < abs(best_delta)) {
4587caff0fcSAndrey Gusakov 					best_pre = i_pre;
4597caff0fcSAndrey Gusakov 					best_post = i_post;
4607caff0fcSAndrey Gusakov 					best_div = div;
4617caff0fcSAndrey Gusakov 					best_mul = mul;
4627caff0fcSAndrey Gusakov 					best_delta = delta;
4637caff0fcSAndrey Gusakov 					best_pixelclock = clk;
4647caff0fcSAndrey Gusakov 				}
4657caff0fcSAndrey Gusakov 			}
4667caff0fcSAndrey Gusakov 		}
4677caff0fcSAndrey Gusakov 	}
4687caff0fcSAndrey Gusakov 	if (best_pixelclock == 0) {
4697caff0fcSAndrey Gusakov 		dev_err(tc->dev, "Failed to calc clock for %d pixelclock\n",
4707caff0fcSAndrey Gusakov 			pixelclock);
4717caff0fcSAndrey Gusakov 		return -EINVAL;
4727caff0fcSAndrey Gusakov 	}
4737caff0fcSAndrey Gusakov 
4747caff0fcSAndrey Gusakov 	dev_dbg(tc->dev, "PLL: got %d, delta %d\n", best_pixelclock,
4757caff0fcSAndrey Gusakov 		best_delta);
4767caff0fcSAndrey Gusakov 	dev_dbg(tc->dev, "PLL: %d / %d / %d * %d / %d\n", refclk,
4777caff0fcSAndrey Gusakov 		ext_div[best_pre], best_div, best_mul, ext_div[best_post]);
4787caff0fcSAndrey Gusakov 
4797caff0fcSAndrey Gusakov 	/* if VCO >= 300 MHz */
4807caff0fcSAndrey Gusakov 	if (refclk / ext_div[best_pre] / best_div * best_mul >= 300000000)
4817caff0fcSAndrey Gusakov 		vco_hi = 1;
4827caff0fcSAndrey Gusakov 	/* see DS */
4837caff0fcSAndrey Gusakov 	if (best_div == 16)
4847caff0fcSAndrey Gusakov 		best_div = 0;
4857caff0fcSAndrey Gusakov 	if (best_mul == 128)
4867caff0fcSAndrey Gusakov 		best_mul = 0;
4877caff0fcSAndrey Gusakov 
4887caff0fcSAndrey Gusakov 	/* Power up PLL and switch to bypass */
4897caff0fcSAndrey Gusakov 	tc_write(PXL_PLLCTRL, PLLBYP | PLLEN);
4907caff0fcSAndrey Gusakov 
4917caff0fcSAndrey Gusakov 	tc_write(PXL_PLLPARAM,
4927caff0fcSAndrey Gusakov 		 (vco_hi << 24) |		/* For PLL VCO >= 300 MHz = 1 */
4937caff0fcSAndrey Gusakov 		 (ext_div[best_pre] << 20) |	/* External Pre-divider */
4947caff0fcSAndrey Gusakov 		 (ext_div[best_post] << 16) |	/* External Post-divider */
4957caff0fcSAndrey Gusakov 		 IN_SEL_REFCLK |		/* Use RefClk as PLL input */
4967caff0fcSAndrey Gusakov 		 (best_div << 8) |		/* Divider for PLL RefClk */
4977caff0fcSAndrey Gusakov 		 (best_mul << 0));		/* Multiplier for PLL */
4987caff0fcSAndrey Gusakov 
4997caff0fcSAndrey Gusakov 	/* Force PLL parameter update and disable bypass */
5007caff0fcSAndrey Gusakov 	tc_write(PXL_PLLCTRL, PLLUPDATE | PLLEN);
5017caff0fcSAndrey Gusakov 
5027caff0fcSAndrey Gusakov 	tc_wait_pll_lock(tc);
5037caff0fcSAndrey Gusakov 
5047caff0fcSAndrey Gusakov 	return 0;
5057caff0fcSAndrey Gusakov err:
5067caff0fcSAndrey Gusakov 	return ret;
5077caff0fcSAndrey Gusakov }
5087caff0fcSAndrey Gusakov 
5097caff0fcSAndrey Gusakov static int tc_pxl_pll_dis(struct tc_data *tc)
5107caff0fcSAndrey Gusakov {
5117caff0fcSAndrey Gusakov 	/* Enable PLL bypass, power down PLL */
5127caff0fcSAndrey Gusakov 	return regmap_write(tc->regmap, PXL_PLLCTRL, PLLBYP);
5137caff0fcSAndrey Gusakov }
5147caff0fcSAndrey Gusakov 
5157caff0fcSAndrey Gusakov static int tc_stream_clock_calc(struct tc_data *tc)
5167caff0fcSAndrey Gusakov {
5177caff0fcSAndrey Gusakov 	int ret;
5187caff0fcSAndrey Gusakov 	/*
5197caff0fcSAndrey Gusakov 	 * If the Stream clock and Link Symbol clock are
5207caff0fcSAndrey Gusakov 	 * asynchronous with each other, the value of M changes over
5217caff0fcSAndrey Gusakov 	 * time. This way of generating link clock and stream
5227caff0fcSAndrey Gusakov 	 * clock is called Asynchronous Clock mode. The value M
5237caff0fcSAndrey Gusakov 	 * must change while the value N stays constant. The
5247caff0fcSAndrey Gusakov 	 * value of N in this Asynchronous Clock mode must be set
5257caff0fcSAndrey Gusakov 	 * to 2^15 or 32,768.
5267caff0fcSAndrey Gusakov 	 *
5277caff0fcSAndrey Gusakov 	 * LSCLK = 1/10 of high speed link clock
5287caff0fcSAndrey Gusakov 	 *
5297caff0fcSAndrey Gusakov 	 * f_STRMCLK = M/N * f_LSCLK
5307caff0fcSAndrey Gusakov 	 * M/N = f_STRMCLK / f_LSCLK
5317caff0fcSAndrey Gusakov 	 *
5327caff0fcSAndrey Gusakov 	 */
5337caff0fcSAndrey Gusakov 	tc_write(DP0_VIDMNGEN1, 32768);
5347caff0fcSAndrey Gusakov 
5357caff0fcSAndrey Gusakov 	return 0;
5367caff0fcSAndrey Gusakov err:
5377caff0fcSAndrey Gusakov 	return ret;
5387caff0fcSAndrey Gusakov }
5397caff0fcSAndrey Gusakov 
5407caff0fcSAndrey Gusakov static int tc_aux_link_setup(struct tc_data *tc)
5417caff0fcSAndrey Gusakov {
5427caff0fcSAndrey Gusakov 	unsigned long rate;
5437caff0fcSAndrey Gusakov 	u32 value;
5447caff0fcSAndrey Gusakov 	int ret;
5457caff0fcSAndrey Gusakov 
5467caff0fcSAndrey Gusakov 	rate = clk_get_rate(tc->refclk);
5477caff0fcSAndrey Gusakov 	switch (rate) {
5487caff0fcSAndrey Gusakov 	case 38400000:
5497caff0fcSAndrey Gusakov 		value = REF_FREQ_38M4;
5507caff0fcSAndrey Gusakov 		break;
5517caff0fcSAndrey Gusakov 	case 26000000:
5527caff0fcSAndrey Gusakov 		value = REF_FREQ_26M;
5537caff0fcSAndrey Gusakov 		break;
5547caff0fcSAndrey Gusakov 	case 19200000:
5557caff0fcSAndrey Gusakov 		value = REF_FREQ_19M2;
5567caff0fcSAndrey Gusakov 		break;
5577caff0fcSAndrey Gusakov 	case 13000000:
5587caff0fcSAndrey Gusakov 		value = REF_FREQ_13M;
5597caff0fcSAndrey Gusakov 		break;
5607caff0fcSAndrey Gusakov 	default:
5617caff0fcSAndrey Gusakov 		dev_err(tc->dev, "Invalid refclk rate: %lu Hz\n", rate);
5627caff0fcSAndrey Gusakov 		return -EINVAL;
5637caff0fcSAndrey Gusakov 	}
5647caff0fcSAndrey Gusakov 
5657caff0fcSAndrey Gusakov 	/* Setup DP-PHY / PLL */
5667caff0fcSAndrey Gusakov 	value |= SYSCLK_SEL_LSCLK | LSCLK_DIV_2;
5677caff0fcSAndrey Gusakov 	tc_write(SYS_PLLPARAM, value);
5687caff0fcSAndrey Gusakov 
569ca342386STomi Valkeinen 	tc_write(DP_PHY_CTRL, BGREN | PWR_SW_EN | PHY_A0_EN);
5707caff0fcSAndrey Gusakov 
5717caff0fcSAndrey Gusakov 	/*
5727caff0fcSAndrey Gusakov 	 * Initially PLLs are in bypass. Force PLL parameter update,
5737caff0fcSAndrey Gusakov 	 * disable PLL bypass, enable PLL
5747caff0fcSAndrey Gusakov 	 */
5757caff0fcSAndrey Gusakov 	tc_write(DP0_PLLCTRL, PLLUPDATE | PLLEN);
5767caff0fcSAndrey Gusakov 	tc_wait_pll_lock(tc);
5777caff0fcSAndrey Gusakov 
5787caff0fcSAndrey Gusakov 	tc_write(DP1_PLLCTRL, PLLUPDATE | PLLEN);
5797caff0fcSAndrey Gusakov 	tc_wait_pll_lock(tc);
5807caff0fcSAndrey Gusakov 
5817caff0fcSAndrey Gusakov 	ret = tc_poll_timeout(tc->regmap, DP_PHY_CTRL, PHY_RDY, PHY_RDY, 1,
5827caff0fcSAndrey Gusakov 			      1000);
5837caff0fcSAndrey Gusakov 	if (ret == -ETIMEDOUT) {
5847caff0fcSAndrey Gusakov 		dev_err(tc->dev, "Timeout waiting for PHY to become ready");
5857caff0fcSAndrey Gusakov 		return ret;
586ca342386STomi Valkeinen 	} else if (ret) {
5877caff0fcSAndrey Gusakov 		goto err;
588ca342386STomi Valkeinen 	}
5897caff0fcSAndrey Gusakov 
5907caff0fcSAndrey Gusakov 	/* Setup AUX link */
5917caff0fcSAndrey Gusakov 	tc_write(DP0_AUXCFG1, AUX_RX_FILTER_EN |
5927caff0fcSAndrey Gusakov 		 (0x06 << 8) |	/* Aux Bit Period Calculator Threshold */
5937caff0fcSAndrey Gusakov 		 (0x3f << 0));	/* Aux Response Timeout Timer */
5947caff0fcSAndrey Gusakov 
5957caff0fcSAndrey Gusakov 	return 0;
5967caff0fcSAndrey Gusakov err:
5977caff0fcSAndrey Gusakov 	dev_err(tc->dev, "tc_aux_link_setup failed: %d\n", ret);
5987caff0fcSAndrey Gusakov 	return ret;
5997caff0fcSAndrey Gusakov }
6007caff0fcSAndrey Gusakov 
6017caff0fcSAndrey Gusakov static int tc_get_display_props(struct tc_data *tc)
6027caff0fcSAndrey Gusakov {
6037caff0fcSAndrey Gusakov 	int ret;
6047caff0fcSAndrey Gusakov 	/* temp buffer */
6057caff0fcSAndrey Gusakov 	u8 tmp[8];
6067caff0fcSAndrey Gusakov 
6077caff0fcSAndrey Gusakov 	/* Read DP Rx Link Capability */
6087caff0fcSAndrey Gusakov 	ret = drm_dp_link_probe(&tc->aux, &tc->link.base);
6097caff0fcSAndrey Gusakov 	if (ret < 0)
6107caff0fcSAndrey Gusakov 		goto err_dpcd_read;
611cffd2b16SAndrey Gusakov 	if (tc->link.base.rate != 162000 && tc->link.base.rate != 270000) {
612cffd2b16SAndrey Gusakov 		dev_dbg(tc->dev, "Falling to 2.7 Gbps rate\n");
613cffd2b16SAndrey Gusakov 		tc->link.base.rate = 270000;
614cffd2b16SAndrey Gusakov 	}
615cffd2b16SAndrey Gusakov 
616cffd2b16SAndrey Gusakov 	if (tc->link.base.num_lanes > 2) {
617cffd2b16SAndrey Gusakov 		dev_dbg(tc->dev, "Falling to 2 lanes\n");
618cffd2b16SAndrey Gusakov 		tc->link.base.num_lanes = 2;
619cffd2b16SAndrey Gusakov 	}
6207caff0fcSAndrey Gusakov 
6217caff0fcSAndrey Gusakov 	ret = drm_dp_dpcd_readb(&tc->aux, DP_MAX_DOWNSPREAD, tmp);
6227caff0fcSAndrey Gusakov 	if (ret < 0)
6237caff0fcSAndrey Gusakov 		goto err_dpcd_read;
624e5607637STomi Valkeinen 	tc->link.spread = tmp[0] & DP_MAX_DOWNSPREAD_0_5;
6257caff0fcSAndrey Gusakov 
6267caff0fcSAndrey Gusakov 	ret = drm_dp_dpcd_readb(&tc->aux, DP_MAIN_LINK_CHANNEL_CODING, tmp);
6277caff0fcSAndrey Gusakov 	if (ret < 0)
6287caff0fcSAndrey Gusakov 		goto err_dpcd_read;
6294b30bf41STomi Valkeinen 
630e5607637STomi Valkeinen 	tc->link.scrambler_dis = false;
6317caff0fcSAndrey Gusakov 	/* read assr */
6327caff0fcSAndrey Gusakov 	ret = drm_dp_dpcd_readb(&tc->aux, DP_EDP_CONFIGURATION_SET, tmp);
6337caff0fcSAndrey Gusakov 	if (ret < 0)
6347caff0fcSAndrey Gusakov 		goto err_dpcd_read;
6357caff0fcSAndrey Gusakov 	tc->link.assr = tmp[0] & DP_ALTERNATE_SCRAMBLER_RESET_ENABLE;
6367caff0fcSAndrey Gusakov 
6377caff0fcSAndrey Gusakov 	dev_dbg(tc->dev, "DPCD rev: %d.%d, rate: %s, lanes: %d, framing: %s\n",
6387caff0fcSAndrey Gusakov 		tc->link.base.revision >> 4, tc->link.base.revision & 0x0f,
6397caff0fcSAndrey Gusakov 		(tc->link.base.rate == 162000) ? "1.62Gbps" : "2.7Gbps",
6407caff0fcSAndrey Gusakov 		tc->link.base.num_lanes,
6417caff0fcSAndrey Gusakov 		(tc->link.base.capabilities & DP_LINK_CAP_ENHANCED_FRAMING) ?
6427caff0fcSAndrey Gusakov 		"enhanced" : "non-enhanced");
643e5607637STomi Valkeinen 	dev_dbg(tc->dev, "Downspread: %s, scrambler: %s\n",
644e5607637STomi Valkeinen 		tc->link.spread ? "0.5%" : "0.0%",
645e5607637STomi Valkeinen 		tc->link.scrambler_dis ? "disabled" : "enabled");
6467caff0fcSAndrey Gusakov 	dev_dbg(tc->dev, "Display ASSR: %d, TC358767 ASSR: %d\n",
6477caff0fcSAndrey Gusakov 		tc->link.assr, tc->assr);
6487caff0fcSAndrey Gusakov 
6497caff0fcSAndrey Gusakov 	return 0;
6507caff0fcSAndrey Gusakov 
6517caff0fcSAndrey Gusakov err_dpcd_read:
6527caff0fcSAndrey Gusakov 	dev_err(tc->dev, "failed to read DPCD: %d\n", ret);
6537caff0fcSAndrey Gusakov 	return ret;
6547caff0fcSAndrey Gusakov }
6557caff0fcSAndrey Gusakov 
65663f8f3baSLaurent Pinchart static int tc_set_video_mode(struct tc_data *tc,
65763f8f3baSLaurent Pinchart 			     const struct drm_display_mode *mode)
6587caff0fcSAndrey Gusakov {
6597caff0fcSAndrey Gusakov 	int ret;
6607caff0fcSAndrey Gusakov 	int vid_sync_dly;
6617caff0fcSAndrey Gusakov 	int max_tu_symbol;
6627caff0fcSAndrey Gusakov 
6637caff0fcSAndrey Gusakov 	int left_margin = mode->htotal - mode->hsync_end;
6647caff0fcSAndrey Gusakov 	int right_margin = mode->hsync_start - mode->hdisplay;
6657caff0fcSAndrey Gusakov 	int hsync_len = mode->hsync_end - mode->hsync_start;
6667caff0fcSAndrey Gusakov 	int upper_margin = mode->vtotal - mode->vsync_end;
6677caff0fcSAndrey Gusakov 	int lower_margin = mode->vsync_start - mode->vdisplay;
6687caff0fcSAndrey Gusakov 	int vsync_len = mode->vsync_end - mode->vsync_start;
6697caff0fcSAndrey Gusakov 
67066d1c3b9SAndrey Gusakov 	/*
67166d1c3b9SAndrey Gusakov 	 * Recommended maximum number of symbols transferred in a transfer unit:
67266d1c3b9SAndrey Gusakov 	 * DIV_ROUND_UP((input active video bandwidth in bytes) * tu_size,
67366d1c3b9SAndrey Gusakov 	 *              (output active video bandwidth in bytes))
67466d1c3b9SAndrey Gusakov 	 * Must be less than tu_size.
67566d1c3b9SAndrey Gusakov 	 */
67666d1c3b9SAndrey Gusakov 	max_tu_symbol = TU_SIZE_RECOMMENDED - 1;
67766d1c3b9SAndrey Gusakov 
6787caff0fcSAndrey Gusakov 	dev_dbg(tc->dev, "set mode %dx%d\n",
6797caff0fcSAndrey Gusakov 		mode->hdisplay, mode->vdisplay);
6807caff0fcSAndrey Gusakov 	dev_dbg(tc->dev, "H margin %d,%d sync %d\n",
6817caff0fcSAndrey Gusakov 		left_margin, right_margin, hsync_len);
6827caff0fcSAndrey Gusakov 	dev_dbg(tc->dev, "V margin %d,%d sync %d\n",
6837caff0fcSAndrey Gusakov 		upper_margin, lower_margin, vsync_len);
6847caff0fcSAndrey Gusakov 	dev_dbg(tc->dev, "total: %dx%d\n", mode->htotal, mode->vtotal);
6857caff0fcSAndrey Gusakov 
6867caff0fcSAndrey Gusakov 
68766d1c3b9SAndrey Gusakov 	/*
68866d1c3b9SAndrey Gusakov 	 * LCD Ctl Frame Size
68966d1c3b9SAndrey Gusakov 	 * datasheet is not clear of vsdelay in case of DPI
69066d1c3b9SAndrey Gusakov 	 * assume we do not need any delay when DPI is a source of
69166d1c3b9SAndrey Gusakov 	 * sync signals
69266d1c3b9SAndrey Gusakov 	 */
69366d1c3b9SAndrey Gusakov 	tc_write(VPCTRL0, (0 << 20) /* VSDELAY */ |
6947caff0fcSAndrey Gusakov 		 OPXLFMT_RGB888 | FRMSYNC_DISABLED | MSF_DISABLED);
69566d1c3b9SAndrey Gusakov 	tc_write(HTIM01, (ALIGN(left_margin, 2) << 16) | /* H back porch */
69666d1c3b9SAndrey Gusakov 			 (ALIGN(hsync_len, 2) << 0));	 /* Hsync */
69766d1c3b9SAndrey Gusakov 	tc_write(HTIM02, (ALIGN(right_margin, 2) << 16) |  /* H front porch */
69866d1c3b9SAndrey Gusakov 			 (ALIGN(mode->hdisplay, 2) << 0)); /* width */
6997caff0fcSAndrey Gusakov 	tc_write(VTIM01, (upper_margin << 16) |		/* V back porch */
7007caff0fcSAndrey Gusakov 			 (vsync_len << 0));		/* Vsync */
7017caff0fcSAndrey Gusakov 	tc_write(VTIM02, (lower_margin << 16) |		/* V front porch */
7027caff0fcSAndrey Gusakov 			 (mode->vdisplay << 0));	/* height */
7037caff0fcSAndrey Gusakov 	tc_write(VFUEN0, VFUEN);		/* update settings */
7047caff0fcSAndrey Gusakov 
7057caff0fcSAndrey Gusakov 	/* Test pattern settings */
7067caff0fcSAndrey Gusakov 	tc_write(TSTCTL,
7077caff0fcSAndrey Gusakov 		 (120 << 24) |	/* Red Color component value */
7087caff0fcSAndrey Gusakov 		 (20 << 16) |	/* Green Color component value */
7097caff0fcSAndrey Gusakov 		 (99 << 8) |	/* Blue Color component value */
7107caff0fcSAndrey Gusakov 		 (1 << 4) |	/* Enable I2C Filter */
7117caff0fcSAndrey Gusakov 		 (2 << 0) |	/* Color bar Mode */
7127caff0fcSAndrey Gusakov 		 0);
7137caff0fcSAndrey Gusakov 
7147caff0fcSAndrey Gusakov 	/* DP Main Stream Attributes */
7157caff0fcSAndrey Gusakov 	vid_sync_dly = hsync_len + left_margin + mode->hdisplay;
7167caff0fcSAndrey Gusakov 	tc_write(DP0_VIDSYNCDELAY,
71766d1c3b9SAndrey Gusakov 		 (max_tu_symbol << 16) |	/* thresh_dly */
7187caff0fcSAndrey Gusakov 		 (vid_sync_dly << 0));
7197caff0fcSAndrey Gusakov 
7207caff0fcSAndrey Gusakov 	tc_write(DP0_TOTALVAL, (mode->vtotal << 16) | (mode->htotal));
7217caff0fcSAndrey Gusakov 
7227caff0fcSAndrey Gusakov 	tc_write(DP0_STARTVAL,
7237caff0fcSAndrey Gusakov 		 ((upper_margin + vsync_len) << 16) |
7247caff0fcSAndrey Gusakov 		 ((left_margin + hsync_len) << 0));
7257caff0fcSAndrey Gusakov 
7267caff0fcSAndrey Gusakov 	tc_write(DP0_ACTIVEVAL, (mode->vdisplay << 16) | (mode->hdisplay));
7277caff0fcSAndrey Gusakov 
7287923e09cSTomi Valkeinen 	tc_write(DP0_SYNCVAL, (vsync_len << 16) | (hsync_len << 0) |
7297923e09cSTomi Valkeinen 		 ((mode->flags & DRM_MODE_FLAG_NHSYNC) ? SYNCVAL_HS_POL_ACTIVE_LOW : 0) |
7307923e09cSTomi Valkeinen 		 ((mode->flags & DRM_MODE_FLAG_NVSYNC) ? SYNCVAL_VS_POL_ACTIVE_LOW : 0));
7317caff0fcSAndrey Gusakov 
7327caff0fcSAndrey Gusakov 	tc_write(DPIPXLFMT, VS_POL_ACTIVE_LOW | HS_POL_ACTIVE_LOW |
7337caff0fcSAndrey Gusakov 		 DE_POL_ACTIVE_HIGH | SUB_CFG_TYPE_CONFIG1 | DPI_BPP_RGB888);
7347caff0fcSAndrey Gusakov 
735f3b8adbeSAndrey Gusakov 	tc_write(DP0_MISC, (max_tu_symbol << 23) | (TU_SIZE_RECOMMENDED << 16) |
736f3b8adbeSAndrey Gusakov 			   BPC_8);
7377caff0fcSAndrey Gusakov 
7387caff0fcSAndrey Gusakov 	return 0;
7397caff0fcSAndrey Gusakov err:
7407caff0fcSAndrey Gusakov 	return ret;
7417caff0fcSAndrey Gusakov }
7427caff0fcSAndrey Gusakov 
743f9538357STomi Valkeinen static int tc_wait_link_training(struct tc_data *tc)
7447caff0fcSAndrey Gusakov {
745f9538357STomi Valkeinen 	u32 timeout = 1000;
7467caff0fcSAndrey Gusakov 	u32 value;
7477caff0fcSAndrey Gusakov 	int ret;
7487caff0fcSAndrey Gusakov 
7497caff0fcSAndrey Gusakov 	do {
7507caff0fcSAndrey Gusakov 		udelay(1);
751f9538357STomi Valkeinen 		tc_read(DP0_LTSTAT, &value);
7527caff0fcSAndrey Gusakov 	} while ((!(value & LT_LOOPDONE)) && (--timeout));
753f9538357STomi Valkeinen 
7547caff0fcSAndrey Gusakov 	if (timeout == 0) {
755f9538357STomi Valkeinen 		dev_err(tc->dev, "Link training timeout waiting for LT_LOOPDONE!\n");
756f9538357STomi Valkeinen 		return -ETIMEDOUT;
7577caff0fcSAndrey Gusakov 	}
7587caff0fcSAndrey Gusakov 
759f9538357STomi Valkeinen 	return (value >> 8) & 0x7;
760f9538357STomi Valkeinen 
7617caff0fcSAndrey Gusakov err:
7627caff0fcSAndrey Gusakov 	return ret;
7637caff0fcSAndrey Gusakov }
7647caff0fcSAndrey Gusakov 
765cb3263b2STomi Valkeinen static int tc_main_link_enable(struct tc_data *tc)
7667caff0fcSAndrey Gusakov {
7677caff0fcSAndrey Gusakov 	struct drm_dp_aux *aux = &tc->aux;
7687caff0fcSAndrey Gusakov 	struct device *dev = tc->dev;
7697caff0fcSAndrey Gusakov 	unsigned int rate;
7707caff0fcSAndrey Gusakov 	u32 dp_phy_ctrl;
7717caff0fcSAndrey Gusakov 	int timeout;
7727caff0fcSAndrey Gusakov 	u32 value;
7737caff0fcSAndrey Gusakov 	int ret;
7747caff0fcSAndrey Gusakov 	u8 tmp[8];
7757caff0fcSAndrey Gusakov 
776cb3263b2STomi Valkeinen 	dev_dbg(tc->dev, "link enable\n");
777cb3263b2STomi Valkeinen 
77867bca92fSTomi Valkeinen 	tc_read(DP0CTL, &value);
77967bca92fSTomi Valkeinen 	if (WARN_ON(value & DP_EN))
78067bca92fSTomi Valkeinen 		tc_write(DP0CTL, 0);
78167bca92fSTomi Valkeinen 
7829a63bd6fSTomi Valkeinen 	tc_write(DP0_SRCCTRL, tc_srcctrl(tc));
7839a63bd6fSTomi Valkeinen 	/* SSCG and BW27 on DP1 must be set to the same as on DP0 */
7849a63bd6fSTomi Valkeinen 	tc_write(DP1_SRCCTRL,
7859a63bd6fSTomi Valkeinen 		 (tc->link.spread ? DP0_SRCCTRL_SSCG : 0) |
7869a63bd6fSTomi Valkeinen 		 ((tc->link.base.rate != 162000) ? DP0_SRCCTRL_BW27 : 0));
7877caff0fcSAndrey Gusakov 
7887caff0fcSAndrey Gusakov 	rate = clk_get_rate(tc->refclk);
7897caff0fcSAndrey Gusakov 	switch (rate) {
7907caff0fcSAndrey Gusakov 	case 38400000:
7917caff0fcSAndrey Gusakov 		value = REF_FREQ_38M4;
7927caff0fcSAndrey Gusakov 		break;
7937caff0fcSAndrey Gusakov 	case 26000000:
7947caff0fcSAndrey Gusakov 		value = REF_FREQ_26M;
7957caff0fcSAndrey Gusakov 		break;
7967caff0fcSAndrey Gusakov 	case 19200000:
7977caff0fcSAndrey Gusakov 		value = REF_FREQ_19M2;
7987caff0fcSAndrey Gusakov 		break;
7997caff0fcSAndrey Gusakov 	case 13000000:
8007caff0fcSAndrey Gusakov 		value = REF_FREQ_13M;
8017caff0fcSAndrey Gusakov 		break;
8027caff0fcSAndrey Gusakov 	default:
8037caff0fcSAndrey Gusakov 		return -EINVAL;
8047caff0fcSAndrey Gusakov 	}
8057caff0fcSAndrey Gusakov 	value |= SYSCLK_SEL_LSCLK | LSCLK_DIV_2;
8067caff0fcSAndrey Gusakov 	tc_write(SYS_PLLPARAM, value);
807adf41098STomi Valkeinen 
8087caff0fcSAndrey Gusakov 	/* Setup Main Link */
8094d9d54a7STomi Valkeinen 	dp_phy_ctrl = BGREN | PWR_SW_EN | PHY_A0_EN | PHY_M0_EN;
8104d9d54a7STomi Valkeinen 	if (tc->link.base.num_lanes == 2)
8114d9d54a7STomi Valkeinen 		dp_phy_ctrl |= PHY_2LANE;
8127caff0fcSAndrey Gusakov 	tc_write(DP_PHY_CTRL, dp_phy_ctrl);
8137caff0fcSAndrey Gusakov 
8147caff0fcSAndrey Gusakov 	/* PLL setup */
8157caff0fcSAndrey Gusakov 	tc_write(DP0_PLLCTRL, PLLUPDATE | PLLEN);
8167caff0fcSAndrey Gusakov 	tc_wait_pll_lock(tc);
8177caff0fcSAndrey Gusakov 
8187caff0fcSAndrey Gusakov 	tc_write(DP1_PLLCTRL, PLLUPDATE | PLLEN);
8197caff0fcSAndrey Gusakov 	tc_wait_pll_lock(tc);
8207caff0fcSAndrey Gusakov 
8217caff0fcSAndrey Gusakov 	/* Reset/Enable Main Links */
8227caff0fcSAndrey Gusakov 	dp_phy_ctrl |= DP_PHY_RST | PHY_M1_RST | PHY_M0_RST;
8237caff0fcSAndrey Gusakov 	tc_write(DP_PHY_CTRL, dp_phy_ctrl);
8247caff0fcSAndrey Gusakov 	usleep_range(100, 200);
8257caff0fcSAndrey Gusakov 	dp_phy_ctrl &= ~(DP_PHY_RST | PHY_M1_RST | PHY_M0_RST);
8267caff0fcSAndrey Gusakov 	tc_write(DP_PHY_CTRL, dp_phy_ctrl);
8277caff0fcSAndrey Gusakov 
8287caff0fcSAndrey Gusakov 	timeout = 1000;
8297caff0fcSAndrey Gusakov 	do {
8307caff0fcSAndrey Gusakov 		tc_read(DP_PHY_CTRL, &value);
8317caff0fcSAndrey Gusakov 		udelay(1);
8327caff0fcSAndrey Gusakov 	} while ((!(value & PHY_RDY)) && (--timeout));
8337caff0fcSAndrey Gusakov 
8347caff0fcSAndrey Gusakov 	if (timeout == 0) {
8357caff0fcSAndrey Gusakov 		dev_err(dev, "timeout waiting for phy become ready");
8367caff0fcSAndrey Gusakov 		return -ETIMEDOUT;
8377caff0fcSAndrey Gusakov 	}
8387caff0fcSAndrey Gusakov 
8397caff0fcSAndrey Gusakov 	/* Set misc: 8 bits per color */
8407caff0fcSAndrey Gusakov 	ret = regmap_update_bits(tc->regmap, DP0_MISC, BPC_8, BPC_8);
8417caff0fcSAndrey Gusakov 	if (ret)
8427caff0fcSAndrey Gusakov 		goto err;
8437caff0fcSAndrey Gusakov 
8447caff0fcSAndrey Gusakov 	/*
8457caff0fcSAndrey Gusakov 	 * ASSR mode
8467caff0fcSAndrey Gusakov 	 * on TC358767 side ASSR configured through strap pin
8477caff0fcSAndrey Gusakov 	 * seems there is no way to change this setting from SW
8487caff0fcSAndrey Gusakov 	 *
8497caff0fcSAndrey Gusakov 	 * check is tc configured for same mode
8507caff0fcSAndrey Gusakov 	 */
8517caff0fcSAndrey Gusakov 	if (tc->assr != tc->link.assr) {
8527caff0fcSAndrey Gusakov 		dev_dbg(dev, "Trying to set display to ASSR: %d\n",
8537caff0fcSAndrey Gusakov 			tc->assr);
8547caff0fcSAndrey Gusakov 		/* try to set ASSR on display side */
8557caff0fcSAndrey Gusakov 		tmp[0] = tc->assr;
8567caff0fcSAndrey Gusakov 		ret = drm_dp_dpcd_writeb(aux, DP_EDP_CONFIGURATION_SET, tmp[0]);
8577caff0fcSAndrey Gusakov 		if (ret < 0)
8587caff0fcSAndrey Gusakov 			goto err_dpcd_read;
8597caff0fcSAndrey Gusakov 		/* read back */
8607caff0fcSAndrey Gusakov 		ret = drm_dp_dpcd_readb(aux, DP_EDP_CONFIGURATION_SET, tmp);
8617caff0fcSAndrey Gusakov 		if (ret < 0)
8627caff0fcSAndrey Gusakov 			goto err_dpcd_read;
8637caff0fcSAndrey Gusakov 
8647caff0fcSAndrey Gusakov 		if (tmp[0] != tc->assr) {
86587291e5dSLucas Stach 			dev_dbg(dev, "Failed to switch display ASSR to %d, falling back to unscrambled mode\n",
8667caff0fcSAndrey Gusakov 				tc->assr);
8677caff0fcSAndrey Gusakov 			/* trying with disabled scrambler */
868e5607637STomi Valkeinen 			tc->link.scrambler_dis = true;
8697caff0fcSAndrey Gusakov 		}
8707caff0fcSAndrey Gusakov 	}
8717caff0fcSAndrey Gusakov 
8727caff0fcSAndrey Gusakov 	/* Setup Link & DPRx Config for Training */
8737caff0fcSAndrey Gusakov 	ret = drm_dp_link_configure(aux, &tc->link.base);
8747caff0fcSAndrey Gusakov 	if (ret < 0)
8757caff0fcSAndrey Gusakov 		goto err_dpcd_write;
8767caff0fcSAndrey Gusakov 
8777caff0fcSAndrey Gusakov 	/* DOWNSPREAD_CTRL */
8787caff0fcSAndrey Gusakov 	tmp[0] = tc->link.spread ? DP_SPREAD_AMP_0_5 : 0x00;
8797caff0fcSAndrey Gusakov 	/* MAIN_LINK_CHANNEL_CODING_SET */
8804b30bf41STomi Valkeinen 	tmp[1] =  DP_SET_ANSI_8B10B;
8817caff0fcSAndrey Gusakov 	ret = drm_dp_dpcd_write(aux, DP_DOWNSPREAD_CTRL, tmp, 2);
8827caff0fcSAndrey Gusakov 	if (ret < 0)
8837caff0fcSAndrey Gusakov 		goto err_dpcd_write;
8847caff0fcSAndrey Gusakov 
885c28d1484STomi Valkeinen 	/* Reset voltage-swing & pre-emphasis */
886c28d1484STomi Valkeinen 	tmp[0] = tmp[1] = DP_TRAIN_VOLTAGE_SWING_LEVEL_0 |
887c28d1484STomi Valkeinen 			  DP_TRAIN_PRE_EMPH_LEVEL_0;
888c28d1484STomi Valkeinen 	ret = drm_dp_dpcd_write(aux, DP_TRAINING_LANE0_SET, tmp, 2);
889c28d1484STomi Valkeinen 	if (ret < 0)
890c28d1484STomi Valkeinen 		goto err_dpcd_write;
891c28d1484STomi Valkeinen 
892f9538357STomi Valkeinen 	/* Clock-Recovery */
893f9538357STomi Valkeinen 
894f9538357STomi Valkeinen 	/* Set DPCD 0x102 for Training Pattern 1 */
895f9538357STomi Valkeinen 	tc_write(DP0_SNKLTCTRL, DP_LINK_SCRAMBLING_DISABLE |
896f9538357STomi Valkeinen 		 DP_TRAINING_PATTERN_1);
897f9538357STomi Valkeinen 
898f9538357STomi Valkeinen 	tc_write(DP0_LTLOOPCTRL,
899f9538357STomi Valkeinen 		 (15 << 28) |	/* Defer Iteration Count */
900f9538357STomi Valkeinen 		 (15 << 24) |	/* Loop Iteration Count */
901f9538357STomi Valkeinen 		 (0xd << 0));	/* Loop Timer Delay */
902f9538357STomi Valkeinen 
903f9538357STomi Valkeinen 	tc_write(DP0_SRCCTRL, tc_srcctrl(tc) | DP0_SRCCTRL_SCRMBLDIS |
904f9538357STomi Valkeinen 		 DP0_SRCCTRL_AUTOCORRECT | DP0_SRCCTRL_TP1);
905f9538357STomi Valkeinen 
906f9538357STomi Valkeinen 	/* Enable DP0 to start Link Training */
907f9538357STomi Valkeinen 	tc_write(DP0CTL,
908f9538357STomi Valkeinen 		 ((tc->link.base.capabilities & DP_LINK_CAP_ENHANCED_FRAMING) ? EF_EN : 0) |
909f9538357STomi Valkeinen 		 DP_EN);
910f9538357STomi Valkeinen 
911f9538357STomi Valkeinen 	/* wait */
912f9538357STomi Valkeinen 	ret = tc_wait_link_training(tc);
913f9538357STomi Valkeinen 	if (ret < 0)
9147caff0fcSAndrey Gusakov 		goto err;
9157caff0fcSAndrey Gusakov 
916f9538357STomi Valkeinen 	if (ret) {
917f9538357STomi Valkeinen 		dev_err(tc->dev, "Link training phase 1 failed: %s\n",
918f9538357STomi Valkeinen 			training_pattern1_errors[ret]);
919f9538357STomi Valkeinen 		ret = -ENODEV;
9207caff0fcSAndrey Gusakov 		goto err;
921f9538357STomi Valkeinen 	}
922f9538357STomi Valkeinen 
923f9538357STomi Valkeinen 	/* Channel Equalization */
924f9538357STomi Valkeinen 
925f9538357STomi Valkeinen 	/* Set DPCD 0x102 for Training Pattern 2 */
926f9538357STomi Valkeinen 	tc_write(DP0_SNKLTCTRL, DP_LINK_SCRAMBLING_DISABLE |
927f9538357STomi Valkeinen 		 DP_TRAINING_PATTERN_2);
928f9538357STomi Valkeinen 
929f9538357STomi Valkeinen 	tc_write(DP0_SRCCTRL, tc_srcctrl(tc) | DP0_SRCCTRL_SCRMBLDIS |
930f9538357STomi Valkeinen 		 DP0_SRCCTRL_AUTOCORRECT | DP0_SRCCTRL_TP2);
931f9538357STomi Valkeinen 
932f9538357STomi Valkeinen 	/* wait */
933f9538357STomi Valkeinen 	ret = tc_wait_link_training(tc);
934f9538357STomi Valkeinen 	if (ret < 0)
935f9538357STomi Valkeinen 		goto err;
936f9538357STomi Valkeinen 
937f9538357STomi Valkeinen 	if (ret) {
938f9538357STomi Valkeinen 		dev_err(tc->dev, "Link training phase 2 failed: %s\n",
939f9538357STomi Valkeinen 			training_pattern2_errors[ret]);
940f9538357STomi Valkeinen 		ret = -ENODEV;
941f9538357STomi Valkeinen 		goto err;
942f9538357STomi Valkeinen 	}
9437caff0fcSAndrey Gusakov 
9440776a269STomi Valkeinen 	/*
9450776a269STomi Valkeinen 	 * Toshiba's documentation suggests to first clear DPCD 0x102, then
9460776a269STomi Valkeinen 	 * clear the training pattern bit in DP0_SRCCTRL. Testing shows
9470776a269STomi Valkeinen 	 * that the link sometimes drops if those steps are done in that order,
9480776a269STomi Valkeinen 	 * but if the steps are done in reverse order, the link stays up.
9490776a269STomi Valkeinen 	 *
9500776a269STomi Valkeinen 	 * So we do the steps differently than documented here.
9510776a269STomi Valkeinen 	 */
9520776a269STomi Valkeinen 
9530776a269STomi Valkeinen 	/* Clear Training Pattern, set AutoCorrect Mode = 1 */
9540776a269STomi Valkeinen 	tc_write(DP0_SRCCTRL, tc_srcctrl(tc) | DP0_SRCCTRL_AUTOCORRECT);
9550776a269STomi Valkeinen 
9567caff0fcSAndrey Gusakov 	/* Clear DPCD 0x102 */
9577caff0fcSAndrey Gusakov 	/* Note: Can Not use DP0_SNKLTCTRL (0x06E4) short cut */
9587caff0fcSAndrey Gusakov 	tmp[0] = tc->link.scrambler_dis ? DP_LINK_SCRAMBLING_DISABLE : 0x00;
9597caff0fcSAndrey Gusakov 	ret = drm_dp_dpcd_writeb(aux, DP_TRAINING_PATTERN_SET, tmp[0]);
9607caff0fcSAndrey Gusakov 	if (ret < 0)
9617caff0fcSAndrey Gusakov 		goto err_dpcd_write;
9627caff0fcSAndrey Gusakov 
9630bf25146STomi Valkeinen 	/* Check link status */
9640bf25146STomi Valkeinen 	ret = drm_dp_dpcd_read_link_status(aux, tmp);
9657caff0fcSAndrey Gusakov 	if (ret < 0)
9667caff0fcSAndrey Gusakov 		goto err_dpcd_read;
9677caff0fcSAndrey Gusakov 
9680bf25146STomi Valkeinen 	ret = 0;
9697caff0fcSAndrey Gusakov 
9700bf25146STomi Valkeinen 	value = tmp[0] & DP_CHANNEL_EQ_BITS;
9710bf25146STomi Valkeinen 
9720bf25146STomi Valkeinen 	if (value != DP_CHANNEL_EQ_BITS) {
9730bf25146STomi Valkeinen 		dev_err(tc->dev, "Lane 0 failed: %x\n", value);
9740bf25146STomi Valkeinen 		ret = -ENODEV;
9750bf25146STomi Valkeinen 	}
9760bf25146STomi Valkeinen 
9770bf25146STomi Valkeinen 	if (tc->link.base.num_lanes == 2) {
9780bf25146STomi Valkeinen 		value = (tmp[0] >> 4) & DP_CHANNEL_EQ_BITS;
9790bf25146STomi Valkeinen 
9800bf25146STomi Valkeinen 		if (value != DP_CHANNEL_EQ_BITS) {
9810bf25146STomi Valkeinen 			dev_err(tc->dev, "Lane 1 failed: %x\n", value);
9820bf25146STomi Valkeinen 			ret = -ENODEV;
9830bf25146STomi Valkeinen 		}
9840bf25146STomi Valkeinen 
9850bf25146STomi Valkeinen 		if (!(tmp[2] & DP_INTERLANE_ALIGN_DONE)) {
9860bf25146STomi Valkeinen 			dev_err(tc->dev, "Interlane align failed\n");
9870bf25146STomi Valkeinen 			ret = -ENODEV;
9880bf25146STomi Valkeinen 		}
9890bf25146STomi Valkeinen 	}
9900bf25146STomi Valkeinen 
9910bf25146STomi Valkeinen 	if (ret) {
9920bf25146STomi Valkeinen 		dev_err(dev, "0x0202 LANE0_1_STATUS:            0x%02x\n", tmp[0]);
9930bf25146STomi Valkeinen 		dev_err(dev, "0x0203 LANE2_3_STATUS             0x%02x\n", tmp[1]);
9940bf25146STomi Valkeinen 		dev_err(dev, "0x0204 LANE_ALIGN_STATUS_UPDATED: 0x%02x\n", tmp[2]);
9950bf25146STomi Valkeinen 		dev_err(dev, "0x0205 SINK_STATUS:               0x%02x\n", tmp[3]);
9960bf25146STomi Valkeinen 		dev_err(dev, "0x0206 ADJUST_REQUEST_LANE0_1:    0x%02x\n", tmp[4]);
9970bf25146STomi Valkeinen 		dev_err(dev, "0x0207 ADJUST_REQUEST_LANE2_3:    0x%02x\n", tmp[5]);
9980bf25146STomi Valkeinen 		goto err;
9997caff0fcSAndrey Gusakov 	}
10007caff0fcSAndrey Gusakov 
10017caff0fcSAndrey Gusakov 	return 0;
10027caff0fcSAndrey Gusakov err_dpcd_read:
10037caff0fcSAndrey Gusakov 	dev_err(tc->dev, "Failed to read DPCD: %d\n", ret);
10047caff0fcSAndrey Gusakov 	return ret;
10057caff0fcSAndrey Gusakov err_dpcd_write:
10067caff0fcSAndrey Gusakov 	dev_err(tc->dev, "Failed to write DPCD: %d\n", ret);
10077caff0fcSAndrey Gusakov err:
10087caff0fcSAndrey Gusakov 	return ret;
10097caff0fcSAndrey Gusakov }
10107caff0fcSAndrey Gusakov 
1011cb3263b2STomi Valkeinen static int tc_main_link_disable(struct tc_data *tc)
1012cb3263b2STomi Valkeinen {
1013cb3263b2STomi Valkeinen 	int ret;
1014cb3263b2STomi Valkeinen 
1015cb3263b2STomi Valkeinen 	dev_dbg(tc->dev, "link disable\n");
1016cb3263b2STomi Valkeinen 
1017cb3263b2STomi Valkeinen 	tc_write(DP0_SRCCTRL, 0);
1018cb3263b2STomi Valkeinen 	tc_write(DP0CTL, 0);
1019cb3263b2STomi Valkeinen 
1020cb3263b2STomi Valkeinen 	return 0;
1021cb3263b2STomi Valkeinen err:
1022cb3263b2STomi Valkeinen 	return ret;
1023cb3263b2STomi Valkeinen }
1024cb3263b2STomi Valkeinen 
102580d57245STomi Valkeinen static int tc_stream_enable(struct tc_data *tc)
10267caff0fcSAndrey Gusakov {
10277caff0fcSAndrey Gusakov 	int ret;
10287caff0fcSAndrey Gusakov 	u32 value;
10297caff0fcSAndrey Gusakov 
103080d57245STomi Valkeinen 	dev_dbg(tc->dev, "enable video stream\n");
10317caff0fcSAndrey Gusakov 
1032bb248368STomi Valkeinen 	/* PXL PLL setup */
1033bb248368STomi Valkeinen 	if (tc_test_pattern) {
1034bb248368STomi Valkeinen 		ret = tc_pxl_pll_en(tc, clk_get_rate(tc->refclk),
103546648a3cSTomi Valkeinen 				    1000 * tc->mode.clock);
1036bb248368STomi Valkeinen 		if (ret)
1037bb248368STomi Valkeinen 			goto err;
1038bb248368STomi Valkeinen 	}
1039bb248368STomi Valkeinen 
104046648a3cSTomi Valkeinen 	ret = tc_set_video_mode(tc, &tc->mode);
10415761a259STomi Valkeinen 	if (ret)
104280d57245STomi Valkeinen 		return ret;
10435761a259STomi Valkeinen 
10445761a259STomi Valkeinen 	/* Set M/N */
10455761a259STomi Valkeinen 	ret = tc_stream_clock_calc(tc);
10465761a259STomi Valkeinen 	if (ret)
104780d57245STomi Valkeinen 		return ret;
10485761a259STomi Valkeinen 
10497caff0fcSAndrey Gusakov 	value = VID_MN_GEN | DP_EN;
10507caff0fcSAndrey Gusakov 	if (tc->link.base.capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
10517caff0fcSAndrey Gusakov 		value |= EF_EN;
10527caff0fcSAndrey Gusakov 	tc_write(DP0CTL, value);
10537caff0fcSAndrey Gusakov 	/*
10547caff0fcSAndrey Gusakov 	 * VID_EN assertion should be delayed by at least N * LSCLK
10557caff0fcSAndrey Gusakov 	 * cycles from the time VID_MN_GEN is enabled in order to
10567caff0fcSAndrey Gusakov 	 * generate stable values for VID_M. LSCLK is 270 MHz or
10577caff0fcSAndrey Gusakov 	 * 162 MHz, VID_N is set to 32768 in  tc_stream_clock_calc(),
10587caff0fcSAndrey Gusakov 	 * so a delay of at least 203 us should suffice.
10597caff0fcSAndrey Gusakov 	 */
10607caff0fcSAndrey Gusakov 	usleep_range(500, 1000);
10617caff0fcSAndrey Gusakov 	value |= VID_EN;
10627caff0fcSAndrey Gusakov 	tc_write(DP0CTL, value);
10637caff0fcSAndrey Gusakov 	/* Set input interface */
10647caff0fcSAndrey Gusakov 	value = DP0_AUDSRC_NO_INPUT;
10657caff0fcSAndrey Gusakov 	if (tc_test_pattern)
10667caff0fcSAndrey Gusakov 		value |= DP0_VIDSRC_COLOR_BAR;
10677caff0fcSAndrey Gusakov 	else
10687caff0fcSAndrey Gusakov 		value |= DP0_VIDSRC_DPI_RX;
10697caff0fcSAndrey Gusakov 	tc_write(SYSCTRL, value);
107080d57245STomi Valkeinen 
107180d57245STomi Valkeinen 	return 0;
107280d57245STomi Valkeinen err:
107380d57245STomi Valkeinen 	return ret;
10747caff0fcSAndrey Gusakov }
10757caff0fcSAndrey Gusakov 
107680d57245STomi Valkeinen static int tc_stream_disable(struct tc_data *tc)
107780d57245STomi Valkeinen {
107880d57245STomi Valkeinen 	int ret;
10791c928267STomi Valkeinen 	u32 val;
108080d57245STomi Valkeinen 
108180d57245STomi Valkeinen 	dev_dbg(tc->dev, "disable video stream\n");
108280d57245STomi Valkeinen 
10831c928267STomi Valkeinen 	tc_read(DP0CTL, &val);
10841c928267STomi Valkeinen 	val &= ~VID_EN;
10851c928267STomi Valkeinen 	tc_write(DP0CTL, val);
108680d57245STomi Valkeinen 
1087bb248368STomi Valkeinen 	tc_pxl_pll_dis(tc);
1088bb248368STomi Valkeinen 
10897caff0fcSAndrey Gusakov 	return 0;
10907caff0fcSAndrey Gusakov err:
10917caff0fcSAndrey Gusakov 	return ret;
10927caff0fcSAndrey Gusakov }
10937caff0fcSAndrey Gusakov 
10947caff0fcSAndrey Gusakov static void tc_bridge_pre_enable(struct drm_bridge *bridge)
10957caff0fcSAndrey Gusakov {
10967caff0fcSAndrey Gusakov 	struct tc_data *tc = bridge_to_tc(bridge);
10977caff0fcSAndrey Gusakov 
10987caff0fcSAndrey Gusakov 	drm_panel_prepare(tc->panel);
10997caff0fcSAndrey Gusakov }
11007caff0fcSAndrey Gusakov 
11017caff0fcSAndrey Gusakov static void tc_bridge_enable(struct drm_bridge *bridge)
11027caff0fcSAndrey Gusakov {
11037caff0fcSAndrey Gusakov 	struct tc_data *tc = bridge_to_tc(bridge);
11047caff0fcSAndrey Gusakov 	int ret;
11057caff0fcSAndrey Gusakov 
1106cb3263b2STomi Valkeinen 	ret = tc_main_link_enable(tc);
11077caff0fcSAndrey Gusakov 	if (ret < 0) {
1108cb3263b2STomi Valkeinen 		dev_err(tc->dev, "main link enable error: %d\n", ret);
11097caff0fcSAndrey Gusakov 		return;
11107caff0fcSAndrey Gusakov 	}
11117caff0fcSAndrey Gusakov 
111280d57245STomi Valkeinen 	ret = tc_stream_enable(tc);
11137caff0fcSAndrey Gusakov 	if (ret < 0) {
11147caff0fcSAndrey Gusakov 		dev_err(tc->dev, "main link stream start error: %d\n", ret);
1115cb3263b2STomi Valkeinen 		tc_main_link_disable(tc);
11167caff0fcSAndrey Gusakov 		return;
11177caff0fcSAndrey Gusakov 	}
11187caff0fcSAndrey Gusakov 
11197caff0fcSAndrey Gusakov 	drm_panel_enable(tc->panel);
11207caff0fcSAndrey Gusakov }
11217caff0fcSAndrey Gusakov 
11227caff0fcSAndrey Gusakov static void tc_bridge_disable(struct drm_bridge *bridge)
11237caff0fcSAndrey Gusakov {
11247caff0fcSAndrey Gusakov 	struct tc_data *tc = bridge_to_tc(bridge);
11257caff0fcSAndrey Gusakov 	int ret;
11267caff0fcSAndrey Gusakov 
11277caff0fcSAndrey Gusakov 	drm_panel_disable(tc->panel);
11287caff0fcSAndrey Gusakov 
112980d57245STomi Valkeinen 	ret = tc_stream_disable(tc);
11307caff0fcSAndrey Gusakov 	if (ret < 0)
11317caff0fcSAndrey Gusakov 		dev_err(tc->dev, "main link stream stop error: %d\n", ret);
1132cb3263b2STomi Valkeinen 
1133cb3263b2STomi Valkeinen 	ret = tc_main_link_disable(tc);
1134cb3263b2STomi Valkeinen 	if (ret < 0)
1135cb3263b2STomi Valkeinen 		dev_err(tc->dev, "main link disable error: %d\n", ret);
11367caff0fcSAndrey Gusakov }
11377caff0fcSAndrey Gusakov 
11387caff0fcSAndrey Gusakov static void tc_bridge_post_disable(struct drm_bridge *bridge)
11397caff0fcSAndrey Gusakov {
11407caff0fcSAndrey Gusakov 	struct tc_data *tc = bridge_to_tc(bridge);
11417caff0fcSAndrey Gusakov 
11427caff0fcSAndrey Gusakov 	drm_panel_unprepare(tc->panel);
11437caff0fcSAndrey Gusakov }
11447caff0fcSAndrey Gusakov 
11457caff0fcSAndrey Gusakov static bool tc_bridge_mode_fixup(struct drm_bridge *bridge,
11467caff0fcSAndrey Gusakov 				 const struct drm_display_mode *mode,
11477caff0fcSAndrey Gusakov 				 struct drm_display_mode *adj)
11487caff0fcSAndrey Gusakov {
11497caff0fcSAndrey Gusakov 	/* Fixup sync polarities, both hsync and vsync are active low */
11507caff0fcSAndrey Gusakov 	adj->flags = mode->flags;
11517caff0fcSAndrey Gusakov 	adj->flags |= (DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC);
11527caff0fcSAndrey Gusakov 	adj->flags &= ~(DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC);
11537caff0fcSAndrey Gusakov 
11547caff0fcSAndrey Gusakov 	return true;
11557caff0fcSAndrey Gusakov }
11567caff0fcSAndrey Gusakov 
11574647a64fSTomi Valkeinen static enum drm_mode_status tc_mode_valid(struct drm_bridge *bridge,
11584647a64fSTomi Valkeinen 					  const struct drm_display_mode *mode)
11597caff0fcSAndrey Gusakov {
11604647a64fSTomi Valkeinen 	struct tc_data *tc = bridge_to_tc(bridge);
116151b9e62eSTomi Valkeinen 	u32 req, avail;
116251b9e62eSTomi Valkeinen 	u32 bits_per_pixel = 24;
116351b9e62eSTomi Valkeinen 
116499fc8e96SAndrey Gusakov 	/* DPI interface clock limitation: upto 154 MHz */
116599fc8e96SAndrey Gusakov 	if (mode->clock > 154000)
116699fc8e96SAndrey Gusakov 		return MODE_CLOCK_HIGH;
116799fc8e96SAndrey Gusakov 
116851b9e62eSTomi Valkeinen 	req = mode->clock * bits_per_pixel / 8;
116951b9e62eSTomi Valkeinen 	avail = tc->link.base.num_lanes * tc->link.base.rate;
117051b9e62eSTomi Valkeinen 
117151b9e62eSTomi Valkeinen 	if (req > avail)
117251b9e62eSTomi Valkeinen 		return MODE_BAD;
117351b9e62eSTomi Valkeinen 
11747caff0fcSAndrey Gusakov 	return MODE_OK;
11757caff0fcSAndrey Gusakov }
11767caff0fcSAndrey Gusakov 
11777caff0fcSAndrey Gusakov static void tc_bridge_mode_set(struct drm_bridge *bridge,
117863f8f3baSLaurent Pinchart 			       const struct drm_display_mode *mode,
117963f8f3baSLaurent Pinchart 			       const struct drm_display_mode *adj)
11807caff0fcSAndrey Gusakov {
11817caff0fcSAndrey Gusakov 	struct tc_data *tc = bridge_to_tc(bridge);
11827caff0fcSAndrey Gusakov 
118346648a3cSTomi Valkeinen 	tc->mode = *mode;
11847caff0fcSAndrey Gusakov }
11857caff0fcSAndrey Gusakov 
11867caff0fcSAndrey Gusakov static int tc_connector_get_modes(struct drm_connector *connector)
11877caff0fcSAndrey Gusakov {
11887caff0fcSAndrey Gusakov 	struct tc_data *tc = connector_to_tc(connector);
11897caff0fcSAndrey Gusakov 	struct edid *edid;
11907caff0fcSAndrey Gusakov 	unsigned int count;
1191*32315730STomi Valkeinen 	int ret;
1192*32315730STomi Valkeinen 
1193*32315730STomi Valkeinen 	ret = tc_get_display_props(tc);
1194*32315730STomi Valkeinen 	if (ret < 0) {
1195*32315730STomi Valkeinen 		dev_err(tc->dev, "failed to read display props: %d\n", ret);
1196*32315730STomi Valkeinen 		return 0;
1197*32315730STomi Valkeinen 	}
11987caff0fcSAndrey Gusakov 
11997caff0fcSAndrey Gusakov 	if (tc->panel && tc->panel->funcs && tc->panel->funcs->get_modes) {
12007caff0fcSAndrey Gusakov 		count = tc->panel->funcs->get_modes(tc->panel);
12017caff0fcSAndrey Gusakov 		if (count > 0)
12027caff0fcSAndrey Gusakov 			return count;
12037caff0fcSAndrey Gusakov 	}
12047caff0fcSAndrey Gusakov 
12057caff0fcSAndrey Gusakov 	edid = drm_get_edid(connector, &tc->aux.ddc);
12067caff0fcSAndrey Gusakov 
12077caff0fcSAndrey Gusakov 	kfree(tc->edid);
12087caff0fcSAndrey Gusakov 	tc->edid = edid;
12097caff0fcSAndrey Gusakov 	if (!edid)
12107caff0fcSAndrey Gusakov 		return 0;
12117caff0fcSAndrey Gusakov 
1212c555f023SDaniel Vetter 	drm_connector_update_edid_property(connector, edid);
12137caff0fcSAndrey Gusakov 	count = drm_add_edid_modes(connector, edid);
12147caff0fcSAndrey Gusakov 
12157caff0fcSAndrey Gusakov 	return count;
12167caff0fcSAndrey Gusakov }
12177caff0fcSAndrey Gusakov 
12187caff0fcSAndrey Gusakov static void tc_connector_set_polling(struct tc_data *tc,
12197caff0fcSAndrey Gusakov 				     struct drm_connector *connector)
12207caff0fcSAndrey Gusakov {
12217caff0fcSAndrey Gusakov 	/* TODO: add support for HPD */
12227caff0fcSAndrey Gusakov 	connector->polled = DRM_CONNECTOR_POLL_CONNECT |
12237caff0fcSAndrey Gusakov 			    DRM_CONNECTOR_POLL_DISCONNECT;
12247caff0fcSAndrey Gusakov }
12257caff0fcSAndrey Gusakov 
12267caff0fcSAndrey Gusakov static const struct drm_connector_helper_funcs tc_connector_helper_funcs = {
12277caff0fcSAndrey Gusakov 	.get_modes = tc_connector_get_modes,
12287caff0fcSAndrey Gusakov };
12297caff0fcSAndrey Gusakov 
12307caff0fcSAndrey Gusakov static const struct drm_connector_funcs tc_connector_funcs = {
12317caff0fcSAndrey Gusakov 	.fill_modes = drm_helper_probe_single_connector_modes,
1232fdd8326aSMarek Vasut 	.destroy = drm_connector_cleanup,
12337caff0fcSAndrey Gusakov 	.reset = drm_atomic_helper_connector_reset,
12347caff0fcSAndrey Gusakov 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
12357caff0fcSAndrey Gusakov 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
12367caff0fcSAndrey Gusakov };
12377caff0fcSAndrey Gusakov 
12387caff0fcSAndrey Gusakov static int tc_bridge_attach(struct drm_bridge *bridge)
12397caff0fcSAndrey Gusakov {
12407caff0fcSAndrey Gusakov 	u32 bus_format = MEDIA_BUS_FMT_RGB888_1X24;
12417caff0fcSAndrey Gusakov 	struct tc_data *tc = bridge_to_tc(bridge);
12427caff0fcSAndrey Gusakov 	struct drm_device *drm = bridge->dev;
12437caff0fcSAndrey Gusakov 	int ret;
12447caff0fcSAndrey Gusakov 
12457caff0fcSAndrey Gusakov 	/* Create eDP connector */
12467caff0fcSAndrey Gusakov 	drm_connector_helper_add(&tc->connector, &tc_connector_helper_funcs);
12477caff0fcSAndrey Gusakov 	ret = drm_connector_init(drm, &tc->connector, &tc_connector_funcs,
1248f8c15790STomi Valkeinen 				 tc->panel ? DRM_MODE_CONNECTOR_eDP :
1249f8c15790STomi Valkeinen 				 DRM_MODE_CONNECTOR_DisplayPort);
12507caff0fcSAndrey Gusakov 	if (ret)
12517caff0fcSAndrey Gusakov 		return ret;
12527caff0fcSAndrey Gusakov 
12537caff0fcSAndrey Gusakov 	if (tc->panel)
12547caff0fcSAndrey Gusakov 		drm_panel_attach(tc->panel, &tc->connector);
12557caff0fcSAndrey Gusakov 
12567caff0fcSAndrey Gusakov 	drm_display_info_set_bus_formats(&tc->connector.display_info,
12577caff0fcSAndrey Gusakov 					 &bus_format, 1);
12584842379cSTomi Valkeinen 	tc->connector.display_info.bus_flags =
12594842379cSTomi Valkeinen 		DRM_BUS_FLAG_DE_HIGH |
126088bc4178SLaurent Pinchart 		DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE |
126188bc4178SLaurent Pinchart 		DRM_BUS_FLAG_SYNC_DRIVE_NEGEDGE;
1262cde4c44dSDaniel Vetter 	drm_connector_attach_encoder(&tc->connector, tc->bridge.encoder);
12637caff0fcSAndrey Gusakov 
12647caff0fcSAndrey Gusakov 	return 0;
12657caff0fcSAndrey Gusakov }
12667caff0fcSAndrey Gusakov 
12677caff0fcSAndrey Gusakov static const struct drm_bridge_funcs tc_bridge_funcs = {
12687caff0fcSAndrey Gusakov 	.attach = tc_bridge_attach,
12694647a64fSTomi Valkeinen 	.mode_valid = tc_mode_valid,
12707caff0fcSAndrey Gusakov 	.mode_set = tc_bridge_mode_set,
12717caff0fcSAndrey Gusakov 	.pre_enable = tc_bridge_pre_enable,
12727caff0fcSAndrey Gusakov 	.enable = tc_bridge_enable,
12737caff0fcSAndrey Gusakov 	.disable = tc_bridge_disable,
12747caff0fcSAndrey Gusakov 	.post_disable = tc_bridge_post_disable,
12757caff0fcSAndrey Gusakov 	.mode_fixup = tc_bridge_mode_fixup,
12767caff0fcSAndrey Gusakov };
12777caff0fcSAndrey Gusakov 
12787caff0fcSAndrey Gusakov static bool tc_readable_reg(struct device *dev, unsigned int reg)
12797caff0fcSAndrey Gusakov {
12807caff0fcSAndrey Gusakov 	return reg != SYSCTRL;
12817caff0fcSAndrey Gusakov }
12827caff0fcSAndrey Gusakov 
12837caff0fcSAndrey Gusakov static const struct regmap_range tc_volatile_ranges[] = {
12847caff0fcSAndrey Gusakov 	regmap_reg_range(DP0_AUXWDATA(0), DP0_AUXSTATUS),
12857caff0fcSAndrey Gusakov 	regmap_reg_range(DP0_LTSTAT, DP0_SNKLTCHGREQ),
12867caff0fcSAndrey Gusakov 	regmap_reg_range(DP_PHY_CTRL, DP_PHY_CTRL),
12877caff0fcSAndrey Gusakov 	regmap_reg_range(DP0_PLLCTRL, PXL_PLLCTRL),
12887caff0fcSAndrey Gusakov 	regmap_reg_range(VFUEN0, VFUEN0),
12897caff0fcSAndrey Gusakov };
12907caff0fcSAndrey Gusakov 
12917caff0fcSAndrey Gusakov static const struct regmap_access_table tc_volatile_table = {
12927caff0fcSAndrey Gusakov 	.yes_ranges = tc_volatile_ranges,
12937caff0fcSAndrey Gusakov 	.n_yes_ranges = ARRAY_SIZE(tc_volatile_ranges),
12947caff0fcSAndrey Gusakov };
12957caff0fcSAndrey Gusakov 
12967caff0fcSAndrey Gusakov static bool tc_writeable_reg(struct device *dev, unsigned int reg)
12977caff0fcSAndrey Gusakov {
12987caff0fcSAndrey Gusakov 	return (reg != TC_IDREG) &&
12997caff0fcSAndrey Gusakov 	       (reg != DP0_LTSTAT) &&
13007caff0fcSAndrey Gusakov 	       (reg != DP0_SNKLTCHGREQ);
13017caff0fcSAndrey Gusakov }
13027caff0fcSAndrey Gusakov 
13037caff0fcSAndrey Gusakov static const struct regmap_config tc_regmap_config = {
13047caff0fcSAndrey Gusakov 	.name = "tc358767",
13057caff0fcSAndrey Gusakov 	.reg_bits = 16,
13067caff0fcSAndrey Gusakov 	.val_bits = 32,
13077caff0fcSAndrey Gusakov 	.reg_stride = 4,
13087caff0fcSAndrey Gusakov 	.max_register = PLL_DBG,
13097caff0fcSAndrey Gusakov 	.cache_type = REGCACHE_RBTREE,
13107caff0fcSAndrey Gusakov 	.readable_reg = tc_readable_reg,
13117caff0fcSAndrey Gusakov 	.volatile_table = &tc_volatile_table,
13127caff0fcSAndrey Gusakov 	.writeable_reg = tc_writeable_reg,
13137caff0fcSAndrey Gusakov 	.reg_format_endian = REGMAP_ENDIAN_BIG,
13147caff0fcSAndrey Gusakov 	.val_format_endian = REGMAP_ENDIAN_LITTLE,
13157caff0fcSAndrey Gusakov };
13167caff0fcSAndrey Gusakov 
13177caff0fcSAndrey Gusakov static int tc_probe(struct i2c_client *client, const struct i2c_device_id *id)
13187caff0fcSAndrey Gusakov {
13197caff0fcSAndrey Gusakov 	struct device *dev = &client->dev;
13207caff0fcSAndrey Gusakov 	struct tc_data *tc;
13217caff0fcSAndrey Gusakov 	int ret;
13227caff0fcSAndrey Gusakov 
13237caff0fcSAndrey Gusakov 	tc = devm_kzalloc(dev, sizeof(*tc), GFP_KERNEL);
13247caff0fcSAndrey Gusakov 	if (!tc)
13257caff0fcSAndrey Gusakov 		return -ENOMEM;
13267caff0fcSAndrey Gusakov 
13277caff0fcSAndrey Gusakov 	tc->dev = dev;
13287caff0fcSAndrey Gusakov 
13297caff0fcSAndrey Gusakov 	/* port@2 is the output port */
1330ebc94461SRob Herring 	ret = drm_of_find_panel_or_bridge(dev->of_node, 2, 0, &tc->panel, NULL);
1331d630213fSLucas Stach 	if (ret && ret != -ENODEV)
1332ebc94461SRob Herring 		return ret;
13337caff0fcSAndrey Gusakov 
13347caff0fcSAndrey Gusakov 	/* Shut down GPIO is optional */
13357caff0fcSAndrey Gusakov 	tc->sd_gpio = devm_gpiod_get_optional(dev, "shutdown", GPIOD_OUT_HIGH);
13367caff0fcSAndrey Gusakov 	if (IS_ERR(tc->sd_gpio))
13377caff0fcSAndrey Gusakov 		return PTR_ERR(tc->sd_gpio);
13387caff0fcSAndrey Gusakov 
13397caff0fcSAndrey Gusakov 	if (tc->sd_gpio) {
13407caff0fcSAndrey Gusakov 		gpiod_set_value_cansleep(tc->sd_gpio, 0);
13417caff0fcSAndrey Gusakov 		usleep_range(5000, 10000);
13427caff0fcSAndrey Gusakov 	}
13437caff0fcSAndrey Gusakov 
13447caff0fcSAndrey Gusakov 	/* Reset GPIO is optional */
13457caff0fcSAndrey Gusakov 	tc->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
13467caff0fcSAndrey Gusakov 	if (IS_ERR(tc->reset_gpio))
13477caff0fcSAndrey Gusakov 		return PTR_ERR(tc->reset_gpio);
13487caff0fcSAndrey Gusakov 
13497caff0fcSAndrey Gusakov 	if (tc->reset_gpio) {
13507caff0fcSAndrey Gusakov 		gpiod_set_value_cansleep(tc->reset_gpio, 1);
13517caff0fcSAndrey Gusakov 		usleep_range(5000, 10000);
13527caff0fcSAndrey Gusakov 	}
13537caff0fcSAndrey Gusakov 
13547caff0fcSAndrey Gusakov 	tc->refclk = devm_clk_get(dev, "ref");
13557caff0fcSAndrey Gusakov 	if (IS_ERR(tc->refclk)) {
13567caff0fcSAndrey Gusakov 		ret = PTR_ERR(tc->refclk);
13577caff0fcSAndrey Gusakov 		dev_err(dev, "Failed to get refclk: %d\n", ret);
13587caff0fcSAndrey Gusakov 		return ret;
13597caff0fcSAndrey Gusakov 	}
13607caff0fcSAndrey Gusakov 
13617caff0fcSAndrey Gusakov 	tc->regmap = devm_regmap_init_i2c(client, &tc_regmap_config);
13627caff0fcSAndrey Gusakov 	if (IS_ERR(tc->regmap)) {
13637caff0fcSAndrey Gusakov 		ret = PTR_ERR(tc->regmap);
13647caff0fcSAndrey Gusakov 		dev_err(dev, "Failed to initialize regmap: %d\n", ret);
13657caff0fcSAndrey Gusakov 		return ret;
13667caff0fcSAndrey Gusakov 	}
13677caff0fcSAndrey Gusakov 
13687caff0fcSAndrey Gusakov 	ret = regmap_read(tc->regmap, TC_IDREG, &tc->rev);
13697caff0fcSAndrey Gusakov 	if (ret) {
13707caff0fcSAndrey Gusakov 		dev_err(tc->dev, "can not read device ID: %d\n", ret);
13717caff0fcSAndrey Gusakov 		return ret;
13727caff0fcSAndrey Gusakov 	}
13737caff0fcSAndrey Gusakov 
13747caff0fcSAndrey Gusakov 	if ((tc->rev != 0x6601) && (tc->rev != 0x6603)) {
13757caff0fcSAndrey Gusakov 		dev_err(tc->dev, "invalid device ID: 0x%08x\n", tc->rev);
13767caff0fcSAndrey Gusakov 		return -EINVAL;
13777caff0fcSAndrey Gusakov 	}
13787caff0fcSAndrey Gusakov 
13797caff0fcSAndrey Gusakov 	tc->assr = (tc->rev == 0x6601); /* Enable ASSR for eDP panels */
13807caff0fcSAndrey Gusakov 
13817caff0fcSAndrey Gusakov 	ret = tc_aux_link_setup(tc);
13827caff0fcSAndrey Gusakov 	if (ret)
13837caff0fcSAndrey Gusakov 		return ret;
13847caff0fcSAndrey Gusakov 
13857caff0fcSAndrey Gusakov 	/* Register DP AUX channel */
13867caff0fcSAndrey Gusakov 	tc->aux.name = "TC358767 AUX i2c adapter";
13877caff0fcSAndrey Gusakov 	tc->aux.dev = tc->dev;
13887caff0fcSAndrey Gusakov 	tc->aux.transfer = tc_aux_transfer;
13897caff0fcSAndrey Gusakov 	ret = drm_dp_aux_register(&tc->aux);
13907caff0fcSAndrey Gusakov 	if (ret)
13917caff0fcSAndrey Gusakov 		return ret;
13927caff0fcSAndrey Gusakov 
13937caff0fcSAndrey Gusakov 	ret = tc_get_display_props(tc);
13947caff0fcSAndrey Gusakov 	if (ret)
13957caff0fcSAndrey Gusakov 		goto err_unregister_aux;
13967caff0fcSAndrey Gusakov 
13977caff0fcSAndrey Gusakov 	tc_connector_set_polling(tc, &tc->connector);
13987caff0fcSAndrey Gusakov 
13997caff0fcSAndrey Gusakov 	tc->bridge.funcs = &tc_bridge_funcs;
14007caff0fcSAndrey Gusakov 	tc->bridge.of_node = dev->of_node;
1401dc01732eSInki Dae 	drm_bridge_add(&tc->bridge);
14027caff0fcSAndrey Gusakov 
14037caff0fcSAndrey Gusakov 	i2c_set_clientdata(client, tc);
14047caff0fcSAndrey Gusakov 
14057caff0fcSAndrey Gusakov 	return 0;
14067caff0fcSAndrey Gusakov err_unregister_aux:
14077caff0fcSAndrey Gusakov 	drm_dp_aux_unregister(&tc->aux);
14087caff0fcSAndrey Gusakov 	return ret;
14097caff0fcSAndrey Gusakov }
14107caff0fcSAndrey Gusakov 
14117caff0fcSAndrey Gusakov static int tc_remove(struct i2c_client *client)
14127caff0fcSAndrey Gusakov {
14137caff0fcSAndrey Gusakov 	struct tc_data *tc = i2c_get_clientdata(client);
14147caff0fcSAndrey Gusakov 
14157caff0fcSAndrey Gusakov 	drm_bridge_remove(&tc->bridge);
14167caff0fcSAndrey Gusakov 	drm_dp_aux_unregister(&tc->aux);
14177caff0fcSAndrey Gusakov 
14187caff0fcSAndrey Gusakov 	return 0;
14197caff0fcSAndrey Gusakov }
14207caff0fcSAndrey Gusakov 
14217caff0fcSAndrey Gusakov static const struct i2c_device_id tc358767_i2c_ids[] = {
14227caff0fcSAndrey Gusakov 	{ "tc358767", 0 },
14237caff0fcSAndrey Gusakov 	{ }
14247caff0fcSAndrey Gusakov };
14257caff0fcSAndrey Gusakov MODULE_DEVICE_TABLE(i2c, tc358767_i2c_ids);
14267caff0fcSAndrey Gusakov 
14277caff0fcSAndrey Gusakov static const struct of_device_id tc358767_of_ids[] = {
14287caff0fcSAndrey Gusakov 	{ .compatible = "toshiba,tc358767", },
14297caff0fcSAndrey Gusakov 	{ }
14307caff0fcSAndrey Gusakov };
14317caff0fcSAndrey Gusakov MODULE_DEVICE_TABLE(of, tc358767_of_ids);
14327caff0fcSAndrey Gusakov 
14337caff0fcSAndrey Gusakov static struct i2c_driver tc358767_driver = {
14347caff0fcSAndrey Gusakov 	.driver = {
14357caff0fcSAndrey Gusakov 		.name = "tc358767",
14367caff0fcSAndrey Gusakov 		.of_match_table = tc358767_of_ids,
14377caff0fcSAndrey Gusakov 	},
14387caff0fcSAndrey Gusakov 	.id_table = tc358767_i2c_ids,
14397caff0fcSAndrey Gusakov 	.probe = tc_probe,
14407caff0fcSAndrey Gusakov 	.remove	= tc_remove,
14417caff0fcSAndrey Gusakov };
14427caff0fcSAndrey Gusakov module_i2c_driver(tc358767_driver);
14437caff0fcSAndrey Gusakov 
14447caff0fcSAndrey Gusakov MODULE_AUTHOR("Andrey Gusakov <andrey.gusakov@cogentembedded.com>");
14457caff0fcSAndrey Gusakov MODULE_DESCRIPTION("tc358767 eDP encoder driver");
14467caff0fcSAndrey Gusakov MODULE_LICENSE("GPL");
1447