xref: /openbmc/linux/drivers/gpu/drm/tegra/sor.c (revision 535a65db484ff4e4654a037f2ea7e1ff21431b77)
16b6b6042SThierry Reding /*
26b6b6042SThierry Reding  * Copyright (C) 2013 NVIDIA Corporation
36b6b6042SThierry Reding  *
46b6b6042SThierry Reding  * This program is free software; you can redistribute it and/or modify
56b6b6042SThierry Reding  * it under the terms of the GNU General Public License version 2 as
66b6b6042SThierry Reding  * published by the Free Software Foundation.
76b6b6042SThierry Reding  */
86b6b6042SThierry Reding 
96b6b6042SThierry Reding #include <linux/clk.h>
10a82752e1SThierry Reding #include <linux/debugfs.h>
116fad8f66SThierry Reding #include <linux/gpio.h>
126b6b6042SThierry Reding #include <linux/io.h>
136b6b6042SThierry Reding #include <linux/platform_device.h>
146b6b6042SThierry Reding #include <linux/reset.h>
15306a7f91SThierry Reding 
167232398aSThierry Reding #include <soc/tegra/pmc.h>
176b6b6042SThierry Reding 
184aa3df71SThierry Reding #include <drm/drm_atomic_helper.h>
196b6b6042SThierry Reding #include <drm/drm_dp_helper.h>
206fad8f66SThierry Reding #include <drm/drm_panel.h>
216b6b6042SThierry Reding 
226b6b6042SThierry Reding #include "dc.h"
236b6b6042SThierry Reding #include "drm.h"
246b6b6042SThierry Reding #include "sor.h"
256b6b6042SThierry Reding 
266b6b6042SThierry Reding struct tegra_sor {
276b6b6042SThierry Reding 	struct host1x_client client;
286b6b6042SThierry Reding 	struct tegra_output output;
296b6b6042SThierry Reding 	struct device *dev;
306b6b6042SThierry Reding 
316b6b6042SThierry Reding 	void __iomem *regs;
326b6b6042SThierry Reding 
336b6b6042SThierry Reding 	struct reset_control *rst;
346b6b6042SThierry Reding 	struct clk *clk_parent;
356b6b6042SThierry Reding 	struct clk *clk_safe;
366b6b6042SThierry Reding 	struct clk *clk_dp;
376b6b6042SThierry Reding 	struct clk *clk;
386b6b6042SThierry Reding 
396b6b6042SThierry Reding 	struct tegra_dpaux *dpaux;
406b6b6042SThierry Reding 
4186f5c52dSThierry Reding 	struct mutex lock;
426b6b6042SThierry Reding 	bool enabled;
43a82752e1SThierry Reding 
44dab16336SThierry Reding 	struct drm_info_list *debugfs_files;
45dab16336SThierry Reding 	struct drm_minor *minor;
46a82752e1SThierry Reding 	struct dentry *debugfs;
476b6b6042SThierry Reding };
486b6b6042SThierry Reding 
4934fa183bSThierry Reding struct tegra_sor_config {
5034fa183bSThierry Reding 	u32 bits_per_pixel;
5134fa183bSThierry Reding 
5234fa183bSThierry Reding 	u32 active_polarity;
5334fa183bSThierry Reding 	u32 active_count;
5434fa183bSThierry Reding 	u32 tu_size;
5534fa183bSThierry Reding 	u32 active_frac;
5634fa183bSThierry Reding 	u32 watermark;
577890b576SThierry Reding 
587890b576SThierry Reding 	u32 hblank_symbols;
597890b576SThierry Reding 	u32 vblank_symbols;
6034fa183bSThierry Reding };
6134fa183bSThierry Reding 
626b6b6042SThierry Reding static inline struct tegra_sor *
636b6b6042SThierry Reding host1x_client_to_sor(struct host1x_client *client)
646b6b6042SThierry Reding {
656b6b6042SThierry Reding 	return container_of(client, struct tegra_sor, client);
666b6b6042SThierry Reding }
676b6b6042SThierry Reding 
686b6b6042SThierry Reding static inline struct tegra_sor *to_sor(struct tegra_output *output)
696b6b6042SThierry Reding {
706b6b6042SThierry Reding 	return container_of(output, struct tegra_sor, output);
716b6b6042SThierry Reding }
726b6b6042SThierry Reding 
7328fe2076SThierry Reding static inline u32 tegra_sor_readl(struct tegra_sor *sor, unsigned long offset)
746b6b6042SThierry Reding {
756b6b6042SThierry Reding 	return readl(sor->regs + (offset << 2));
766b6b6042SThierry Reding }
776b6b6042SThierry Reding 
7828fe2076SThierry Reding static inline void tegra_sor_writel(struct tegra_sor *sor, u32 value,
796b6b6042SThierry Reding 				    unsigned long offset)
806b6b6042SThierry Reding {
816b6b6042SThierry Reding 	writel(value, sor->regs + (offset << 2));
826b6b6042SThierry Reding }
836b6b6042SThierry Reding 
846b6b6042SThierry Reding static int tegra_sor_dp_train_fast(struct tegra_sor *sor,
856b6b6042SThierry Reding 				   struct drm_dp_link *link)
866b6b6042SThierry Reding {
876b6b6042SThierry Reding 	unsigned int i;
886b6b6042SThierry Reding 	u8 pattern;
8928fe2076SThierry Reding 	u32 value;
906b6b6042SThierry Reding 	int err;
916b6b6042SThierry Reding 
926b6b6042SThierry Reding 	/* setup lane parameters */
936b6b6042SThierry Reding 	value = SOR_LANE_DRIVE_CURRENT_LANE3(0x40) |
946b6b6042SThierry Reding 		SOR_LANE_DRIVE_CURRENT_LANE2(0x40) |
956b6b6042SThierry Reding 		SOR_LANE_DRIVE_CURRENT_LANE1(0x40) |
966b6b6042SThierry Reding 		SOR_LANE_DRIVE_CURRENT_LANE0(0x40);
976b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT_0);
986b6b6042SThierry Reding 
996b6b6042SThierry Reding 	value = SOR_LANE_PREEMPHASIS_LANE3(0x0f) |
1006b6b6042SThierry Reding 		SOR_LANE_PREEMPHASIS_LANE2(0x0f) |
1016b6b6042SThierry Reding 		SOR_LANE_PREEMPHASIS_LANE1(0x0f) |
1026b6b6042SThierry Reding 		SOR_LANE_PREEMPHASIS_LANE0(0x0f);
1036b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS_0);
1046b6b6042SThierry Reding 
1056b6b6042SThierry Reding 	value = SOR_LANE_POST_CURSOR_LANE3(0x00) |
1066b6b6042SThierry Reding 		SOR_LANE_POST_CURSOR_LANE2(0x00) |
1076b6b6042SThierry Reding 		SOR_LANE_POST_CURSOR_LANE1(0x00) |
1086b6b6042SThierry Reding 		SOR_LANE_POST_CURSOR_LANE0(0x00);
1096b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_POST_CURSOR_0);
1106b6b6042SThierry Reding 
1116b6b6042SThierry Reding 	/* disable LVDS mode */
1126b6b6042SThierry Reding 	tegra_sor_writel(sor, 0, SOR_LVDS);
1136b6b6042SThierry Reding 
1146b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
1156b6b6042SThierry Reding 	value |= SOR_DP_PADCTL_TX_PU_ENABLE;
1166b6b6042SThierry Reding 	value &= ~SOR_DP_PADCTL_TX_PU_MASK;
1176b6b6042SThierry Reding 	value |= SOR_DP_PADCTL_TX_PU(2); /* XXX: don't hardcode? */
1186b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
1196b6b6042SThierry Reding 
1206b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
1216b6b6042SThierry Reding 	value |= SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
1226b6b6042SThierry Reding 		 SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0;
1236b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
1246b6b6042SThierry Reding 
1256b6b6042SThierry Reding 	usleep_range(10, 100);
1266b6b6042SThierry Reding 
1276b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
1286b6b6042SThierry Reding 	value &= ~(SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
1296b6b6042SThierry Reding 		   SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0);
1306b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
1316b6b6042SThierry Reding 
1326b6b6042SThierry Reding 	err = tegra_dpaux_prepare(sor->dpaux, DP_SET_ANSI_8B10B);
1336b6b6042SThierry Reding 	if (err < 0)
1346b6b6042SThierry Reding 		return err;
1356b6b6042SThierry Reding 
1366b6b6042SThierry Reding 	for (i = 0, value = 0; i < link->num_lanes; i++) {
1376b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
1386b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_NONE |
1396b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_TRAIN1;
1406b6b6042SThierry Reding 		value = (value << 8) | lane;
1416b6b6042SThierry Reding 	}
1426b6b6042SThierry Reding 
1436b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
1446b6b6042SThierry Reding 
1456b6b6042SThierry Reding 	pattern = DP_TRAINING_PATTERN_1;
1466b6b6042SThierry Reding 
1476b6b6042SThierry Reding 	err = tegra_dpaux_train(sor->dpaux, link, pattern);
1486b6b6042SThierry Reding 	if (err < 0)
1496b6b6042SThierry Reding 		return err;
1506b6b6042SThierry Reding 
1516b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_SPARE_0);
1526b6b6042SThierry Reding 	value |= SOR_DP_SPARE_SEQ_ENABLE;
1536b6b6042SThierry Reding 	value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
1546b6b6042SThierry Reding 	value |= SOR_DP_SPARE_MACRO_SOR_CLK;
1556b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_SPARE_0);
1566b6b6042SThierry Reding 
1576b6b6042SThierry Reding 	for (i = 0, value = 0; i < link->num_lanes; i++) {
1586b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
1596b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_NONE |
1606b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_TRAIN2;
1616b6b6042SThierry Reding 		value = (value << 8) | lane;
1626b6b6042SThierry Reding 	}
1636b6b6042SThierry Reding 
1646b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
1656b6b6042SThierry Reding 
1666b6b6042SThierry Reding 	pattern = DP_LINK_SCRAMBLING_DISABLE | DP_TRAINING_PATTERN_2;
1676b6b6042SThierry Reding 
1686b6b6042SThierry Reding 	err = tegra_dpaux_train(sor->dpaux, link, pattern);
1696b6b6042SThierry Reding 	if (err < 0)
1706b6b6042SThierry Reding 		return err;
1716b6b6042SThierry Reding 
1726b6b6042SThierry Reding 	for (i = 0, value = 0; i < link->num_lanes; i++) {
1736b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
1746b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_GALIOS |
1756b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_NONE;
1766b6b6042SThierry Reding 		value = (value << 8) | lane;
1776b6b6042SThierry Reding 	}
1786b6b6042SThierry Reding 
1796b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
1806b6b6042SThierry Reding 
1816b6b6042SThierry Reding 	pattern = DP_TRAINING_PATTERN_DISABLE;
1826b6b6042SThierry Reding 
1836b6b6042SThierry Reding 	err = tegra_dpaux_train(sor->dpaux, link, pattern);
1846b6b6042SThierry Reding 	if (err < 0)
1856b6b6042SThierry Reding 		return err;
1866b6b6042SThierry Reding 
1876b6b6042SThierry Reding 	return 0;
1886b6b6042SThierry Reding }
1896b6b6042SThierry Reding 
1906b6b6042SThierry Reding static void tegra_sor_super_update(struct tegra_sor *sor)
1916b6b6042SThierry Reding {
1926b6b6042SThierry Reding 	tegra_sor_writel(sor, 0, SOR_SUPER_STATE_0);
1936b6b6042SThierry Reding 	tegra_sor_writel(sor, 1, SOR_SUPER_STATE_0);
1946b6b6042SThierry Reding 	tegra_sor_writel(sor, 0, SOR_SUPER_STATE_0);
1956b6b6042SThierry Reding }
1966b6b6042SThierry Reding 
1976b6b6042SThierry Reding static void tegra_sor_update(struct tegra_sor *sor)
1986b6b6042SThierry Reding {
1996b6b6042SThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE_0);
2006b6b6042SThierry Reding 	tegra_sor_writel(sor, 1, SOR_STATE_0);
2016b6b6042SThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE_0);
2026b6b6042SThierry Reding }
2036b6b6042SThierry Reding 
2046b6b6042SThierry Reding static int tegra_sor_setup_pwm(struct tegra_sor *sor, unsigned long timeout)
2056b6b6042SThierry Reding {
20628fe2076SThierry Reding 	u32 value;
2076b6b6042SThierry Reding 
2086b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWM_DIV);
2096b6b6042SThierry Reding 	value &= ~SOR_PWM_DIV_MASK;
2106b6b6042SThierry Reding 	value |= 0x400; /* period */
2116b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWM_DIV);
2126b6b6042SThierry Reding 
2136b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWM_CTL);
2146b6b6042SThierry Reding 	value &= ~SOR_PWM_CTL_DUTY_CYCLE_MASK;
2156b6b6042SThierry Reding 	value |= 0x400; /* duty cycle */
2166b6b6042SThierry Reding 	value &= ~SOR_PWM_CTL_CLK_SEL; /* clock source: PCLK */
2176b6b6042SThierry Reding 	value |= SOR_PWM_CTL_TRIGGER;
2186b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWM_CTL);
2196b6b6042SThierry Reding 
2206b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
2216b6b6042SThierry Reding 
2226b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
2236b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWM_CTL);
2246b6b6042SThierry Reding 		if ((value & SOR_PWM_CTL_TRIGGER) == 0)
2256b6b6042SThierry Reding 			return 0;
2266b6b6042SThierry Reding 
2276b6b6042SThierry Reding 		usleep_range(25, 100);
2286b6b6042SThierry Reding 	}
2296b6b6042SThierry Reding 
2306b6b6042SThierry Reding 	return -ETIMEDOUT;
2316b6b6042SThierry Reding }
2326b6b6042SThierry Reding 
2336b6b6042SThierry Reding static int tegra_sor_attach(struct tegra_sor *sor)
2346b6b6042SThierry Reding {
2356b6b6042SThierry Reding 	unsigned long value, timeout;
2366b6b6042SThierry Reding 
2376b6b6042SThierry Reding 	/* wake up in normal mode */
2386b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE_1);
2396b6b6042SThierry Reding 	value |= SOR_SUPER_STATE_HEAD_MODE_AWAKE;
2406b6b6042SThierry Reding 	value |= SOR_SUPER_STATE_MODE_NORMAL;
2416b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE_1);
2426b6b6042SThierry Reding 	tegra_sor_super_update(sor);
2436b6b6042SThierry Reding 
2446b6b6042SThierry Reding 	/* attach */
2456b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE_1);
2466b6b6042SThierry Reding 	value |= SOR_SUPER_STATE_ATTACHED;
2476b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE_1);
2486b6b6042SThierry Reding 	tegra_sor_super_update(sor);
2496b6b6042SThierry Reding 
2506b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
2516b6b6042SThierry Reding 
2526b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
2536b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_TEST);
2546b6b6042SThierry Reding 		if ((value & SOR_TEST_ATTACHED) != 0)
2556b6b6042SThierry Reding 			return 0;
2566b6b6042SThierry Reding 
2576b6b6042SThierry Reding 		usleep_range(25, 100);
2586b6b6042SThierry Reding 	}
2596b6b6042SThierry Reding 
2606b6b6042SThierry Reding 	return -ETIMEDOUT;
2616b6b6042SThierry Reding }
2626b6b6042SThierry Reding 
2636b6b6042SThierry Reding static int tegra_sor_wakeup(struct tegra_sor *sor)
2646b6b6042SThierry Reding {
2656b6b6042SThierry Reding 	unsigned long value, timeout;
2666b6b6042SThierry Reding 
2676b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
2686b6b6042SThierry Reding 
2696b6b6042SThierry Reding 	/* wait for head to wake up */
2706b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
2716b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_TEST);
2726b6b6042SThierry Reding 		value &= SOR_TEST_HEAD_MODE_MASK;
2736b6b6042SThierry Reding 
2746b6b6042SThierry Reding 		if (value == SOR_TEST_HEAD_MODE_AWAKE)
2756b6b6042SThierry Reding 			return 0;
2766b6b6042SThierry Reding 
2776b6b6042SThierry Reding 		usleep_range(25, 100);
2786b6b6042SThierry Reding 	}
2796b6b6042SThierry Reding 
2806b6b6042SThierry Reding 	return -ETIMEDOUT;
2816b6b6042SThierry Reding }
2826b6b6042SThierry Reding 
2836b6b6042SThierry Reding static int tegra_sor_power_up(struct tegra_sor *sor, unsigned long timeout)
2846b6b6042SThierry Reding {
28528fe2076SThierry Reding 	u32 value;
2866b6b6042SThierry Reding 
2876b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWR);
2886b6b6042SThierry Reding 	value |= SOR_PWR_TRIGGER | SOR_PWR_NORMAL_STATE_PU;
2896b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWR);
2906b6b6042SThierry Reding 
2916b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
2926b6b6042SThierry Reding 
2936b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
2946b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWR);
2956b6b6042SThierry Reding 		if ((value & SOR_PWR_TRIGGER) == 0)
2966b6b6042SThierry Reding 			return 0;
2976b6b6042SThierry Reding 
2986b6b6042SThierry Reding 		usleep_range(25, 100);
2996b6b6042SThierry Reding 	}
3006b6b6042SThierry Reding 
3016b6b6042SThierry Reding 	return -ETIMEDOUT;
3026b6b6042SThierry Reding }
3036b6b6042SThierry Reding 
30434fa183bSThierry Reding struct tegra_sor_params {
30534fa183bSThierry Reding 	/* number of link clocks per line */
30634fa183bSThierry Reding 	unsigned int num_clocks;
30734fa183bSThierry Reding 	/* ratio between input and output */
30834fa183bSThierry Reding 	u64 ratio;
30934fa183bSThierry Reding 	/* precision factor */
31034fa183bSThierry Reding 	u64 precision;
31134fa183bSThierry Reding 
31234fa183bSThierry Reding 	unsigned int active_polarity;
31334fa183bSThierry Reding 	unsigned int active_count;
31434fa183bSThierry Reding 	unsigned int active_frac;
31534fa183bSThierry Reding 	unsigned int tu_size;
31634fa183bSThierry Reding 	unsigned int error;
31734fa183bSThierry Reding };
31834fa183bSThierry Reding 
31934fa183bSThierry Reding static int tegra_sor_compute_params(struct tegra_sor *sor,
32034fa183bSThierry Reding 				    struct tegra_sor_params *params,
32134fa183bSThierry Reding 				    unsigned int tu_size)
32234fa183bSThierry Reding {
32334fa183bSThierry Reding 	u64 active_sym, active_count, frac, approx;
32434fa183bSThierry Reding 	u32 active_polarity, active_frac = 0;
32534fa183bSThierry Reding 	const u64 f = params->precision;
32634fa183bSThierry Reding 	s64 error;
32734fa183bSThierry Reding 
32834fa183bSThierry Reding 	active_sym = params->ratio * tu_size;
32934fa183bSThierry Reding 	active_count = div_u64(active_sym, f) * f;
33034fa183bSThierry Reding 	frac = active_sym - active_count;
33134fa183bSThierry Reding 
33234fa183bSThierry Reding 	/* fraction < 0.5 */
33334fa183bSThierry Reding 	if (frac >= (f / 2)) {
33434fa183bSThierry Reding 		active_polarity = 1;
33534fa183bSThierry Reding 		frac = f - frac;
33634fa183bSThierry Reding 	} else {
33734fa183bSThierry Reding 		active_polarity = 0;
33834fa183bSThierry Reding 	}
33934fa183bSThierry Reding 
34034fa183bSThierry Reding 	if (frac != 0) {
34134fa183bSThierry Reding 		frac = div_u64(f * f,  frac); /* 1/fraction */
34234fa183bSThierry Reding 		if (frac <= (15 * f)) {
34334fa183bSThierry Reding 			active_frac = div_u64(frac, f);
34434fa183bSThierry Reding 
34534fa183bSThierry Reding 			/* round up */
34634fa183bSThierry Reding 			if (active_polarity)
34734fa183bSThierry Reding 				active_frac++;
34834fa183bSThierry Reding 		} else {
34934fa183bSThierry Reding 			active_frac = active_polarity ? 1 : 15;
35034fa183bSThierry Reding 		}
35134fa183bSThierry Reding 	}
35234fa183bSThierry Reding 
35334fa183bSThierry Reding 	if (active_frac == 1)
35434fa183bSThierry Reding 		active_polarity = 0;
35534fa183bSThierry Reding 
35634fa183bSThierry Reding 	if (active_polarity == 1) {
35734fa183bSThierry Reding 		if (active_frac) {
35834fa183bSThierry Reding 			approx = active_count + (active_frac * (f - 1)) * f;
35934fa183bSThierry Reding 			approx = div_u64(approx, active_frac * f);
36034fa183bSThierry Reding 		} else {
36134fa183bSThierry Reding 			approx = active_count + f;
36234fa183bSThierry Reding 		}
36334fa183bSThierry Reding 	} else {
36434fa183bSThierry Reding 		if (active_frac)
36534fa183bSThierry Reding 			approx = active_count + div_u64(f, active_frac);
36634fa183bSThierry Reding 		else
36734fa183bSThierry Reding 			approx = active_count;
36834fa183bSThierry Reding 	}
36934fa183bSThierry Reding 
37034fa183bSThierry Reding 	error = div_s64(active_sym - approx, tu_size);
37134fa183bSThierry Reding 	error *= params->num_clocks;
37234fa183bSThierry Reding 
37334fa183bSThierry Reding 	if (error <= 0 && abs64(error) < params->error) {
37434fa183bSThierry Reding 		params->active_count = div_u64(active_count, f);
37534fa183bSThierry Reding 		params->active_polarity = active_polarity;
37634fa183bSThierry Reding 		params->active_frac = active_frac;
37734fa183bSThierry Reding 		params->error = abs64(error);
37834fa183bSThierry Reding 		params->tu_size = tu_size;
37934fa183bSThierry Reding 
38034fa183bSThierry Reding 		if (error == 0)
38134fa183bSThierry Reding 			return true;
38234fa183bSThierry Reding 	}
38334fa183bSThierry Reding 
38434fa183bSThierry Reding 	return false;
38534fa183bSThierry Reding }
38634fa183bSThierry Reding 
38734fa183bSThierry Reding static int tegra_sor_calc_config(struct tegra_sor *sor,
38834fa183bSThierry Reding 				 struct drm_display_mode *mode,
38934fa183bSThierry Reding 				 struct tegra_sor_config *config,
39034fa183bSThierry Reding 				 struct drm_dp_link *link)
39134fa183bSThierry Reding {
39234fa183bSThierry Reding 	const u64 f = 100000, link_rate = link->rate * 1000;
39334fa183bSThierry Reding 	const u64 pclk = mode->clock * 1000;
3947890b576SThierry Reding 	u64 input, output, watermark, num;
39534fa183bSThierry Reding 	struct tegra_sor_params params;
39634fa183bSThierry Reding 	u32 num_syms_per_line;
39734fa183bSThierry Reding 	unsigned int i;
39834fa183bSThierry Reding 
39934fa183bSThierry Reding 	if (!link_rate || !link->num_lanes || !pclk || !config->bits_per_pixel)
40034fa183bSThierry Reding 		return -EINVAL;
40134fa183bSThierry Reding 
40234fa183bSThierry Reding 	output = link_rate * 8 * link->num_lanes;
40334fa183bSThierry Reding 	input = pclk * config->bits_per_pixel;
40434fa183bSThierry Reding 
40534fa183bSThierry Reding 	if (input >= output)
40634fa183bSThierry Reding 		return -ERANGE;
40734fa183bSThierry Reding 
40834fa183bSThierry Reding 	memset(&params, 0, sizeof(params));
40934fa183bSThierry Reding 	params.ratio = div64_u64(input * f, output);
41034fa183bSThierry Reding 	params.num_clocks = div_u64(link_rate * mode->hdisplay, pclk);
41134fa183bSThierry Reding 	params.precision = f;
41234fa183bSThierry Reding 	params.error = 64 * f;
41334fa183bSThierry Reding 	params.tu_size = 64;
41434fa183bSThierry Reding 
41534fa183bSThierry Reding 	for (i = params.tu_size; i >= 32; i--)
41634fa183bSThierry Reding 		if (tegra_sor_compute_params(sor, &params, i))
41734fa183bSThierry Reding 			break;
41834fa183bSThierry Reding 
41934fa183bSThierry Reding 	if (params.active_frac == 0) {
42034fa183bSThierry Reding 		config->active_polarity = 0;
42134fa183bSThierry Reding 		config->active_count = params.active_count;
42234fa183bSThierry Reding 
42334fa183bSThierry Reding 		if (!params.active_polarity)
42434fa183bSThierry Reding 			config->active_count--;
42534fa183bSThierry Reding 
42634fa183bSThierry Reding 		config->tu_size = params.tu_size;
42734fa183bSThierry Reding 		config->active_frac = 1;
42834fa183bSThierry Reding 	} else {
42934fa183bSThierry Reding 		config->active_polarity = params.active_polarity;
43034fa183bSThierry Reding 		config->active_count = params.active_count;
43134fa183bSThierry Reding 		config->active_frac = params.active_frac;
43234fa183bSThierry Reding 		config->tu_size = params.tu_size;
43334fa183bSThierry Reding 	}
43434fa183bSThierry Reding 
43534fa183bSThierry Reding 	dev_dbg(sor->dev,
43634fa183bSThierry Reding 		"polarity: %d active count: %d tu size: %d active frac: %d\n",
43734fa183bSThierry Reding 		config->active_polarity, config->active_count,
43834fa183bSThierry Reding 		config->tu_size, config->active_frac);
43934fa183bSThierry Reding 
44034fa183bSThierry Reding 	watermark = params.ratio * config->tu_size * (f - params.ratio);
44134fa183bSThierry Reding 	watermark = div_u64(watermark, f);
44234fa183bSThierry Reding 
44334fa183bSThierry Reding 	watermark = div_u64(watermark + params.error, f);
44434fa183bSThierry Reding 	config->watermark = watermark + (config->bits_per_pixel / 8) + 2;
44534fa183bSThierry Reding 	num_syms_per_line = (mode->hdisplay * config->bits_per_pixel) *
44634fa183bSThierry Reding 			    (link->num_lanes * 8);
44734fa183bSThierry Reding 
44834fa183bSThierry Reding 	if (config->watermark > 30) {
44934fa183bSThierry Reding 		config->watermark = 30;
45034fa183bSThierry Reding 		dev_err(sor->dev,
45134fa183bSThierry Reding 			"unable to compute TU size, forcing watermark to %u\n",
45234fa183bSThierry Reding 			config->watermark);
45334fa183bSThierry Reding 	} else if (config->watermark > num_syms_per_line) {
45434fa183bSThierry Reding 		config->watermark = num_syms_per_line;
45534fa183bSThierry Reding 		dev_err(sor->dev, "watermark too high, forcing to %u\n",
45634fa183bSThierry Reding 			config->watermark);
45734fa183bSThierry Reding 	}
45834fa183bSThierry Reding 
4597890b576SThierry Reding 	/* compute the number of symbols per horizontal blanking interval */
4607890b576SThierry Reding 	num = ((mode->htotal - mode->hdisplay) - 7) * link_rate;
4617890b576SThierry Reding 	config->hblank_symbols = div_u64(num, pclk);
4627890b576SThierry Reding 
4637890b576SThierry Reding 	if (link->capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
4647890b576SThierry Reding 		config->hblank_symbols -= 3;
4657890b576SThierry Reding 
4667890b576SThierry Reding 	config->hblank_symbols -= 12 / link->num_lanes;
4677890b576SThierry Reding 
4687890b576SThierry Reding 	/* compute the number of symbols per vertical blanking interval */
4697890b576SThierry Reding 	num = (mode->hdisplay - 25) * link_rate;
4707890b576SThierry Reding 	config->vblank_symbols = div_u64(num, pclk);
4717890b576SThierry Reding 	config->vblank_symbols -= 36 / link->num_lanes + 4;
4727890b576SThierry Reding 
4737890b576SThierry Reding 	dev_dbg(sor->dev, "blank symbols: H:%u V:%u\n", config->hblank_symbols,
4747890b576SThierry Reding 		config->vblank_symbols);
4757890b576SThierry Reding 
47634fa183bSThierry Reding 	return 0;
47734fa183bSThierry Reding }
47834fa183bSThierry Reding 
4796fad8f66SThierry Reding static int tegra_sor_detach(struct tegra_sor *sor)
4806b6b6042SThierry Reding {
4816fad8f66SThierry Reding 	unsigned long value, timeout;
4826fad8f66SThierry Reding 
4836fad8f66SThierry Reding 	/* switch to safe mode */
4846fad8f66SThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE_1);
4856fad8f66SThierry Reding 	value &= ~SOR_SUPER_STATE_MODE_NORMAL;
4866fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE_1);
4876fad8f66SThierry Reding 	tegra_sor_super_update(sor);
4886fad8f66SThierry Reding 
4896fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
4906fad8f66SThierry Reding 
4916fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
4926fad8f66SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWR);
4936fad8f66SThierry Reding 		if (value & SOR_PWR_MODE_SAFE)
4946fad8f66SThierry Reding 			break;
4956fad8f66SThierry Reding 	}
4966fad8f66SThierry Reding 
4976fad8f66SThierry Reding 	if ((value & SOR_PWR_MODE_SAFE) == 0)
4986fad8f66SThierry Reding 		return -ETIMEDOUT;
4996fad8f66SThierry Reding 
5006fad8f66SThierry Reding 	/* go to sleep */
5016fad8f66SThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE_1);
5026fad8f66SThierry Reding 	value &= ~SOR_SUPER_STATE_HEAD_MODE_MASK;
5036fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE_1);
5046fad8f66SThierry Reding 	tegra_sor_super_update(sor);
5056fad8f66SThierry Reding 
5066fad8f66SThierry Reding 	/* detach */
5076fad8f66SThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE_1);
5086fad8f66SThierry Reding 	value &= ~SOR_SUPER_STATE_ATTACHED;
5096fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE_1);
5106fad8f66SThierry Reding 	tegra_sor_super_update(sor);
5116fad8f66SThierry Reding 
5126fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
5136fad8f66SThierry Reding 
5146fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
5156fad8f66SThierry Reding 		value = tegra_sor_readl(sor, SOR_TEST);
5166fad8f66SThierry Reding 		if ((value & SOR_TEST_ATTACHED) == 0)
5176fad8f66SThierry Reding 			break;
5186fad8f66SThierry Reding 
5196fad8f66SThierry Reding 		usleep_range(25, 100);
5206fad8f66SThierry Reding 	}
5216fad8f66SThierry Reding 
5226fad8f66SThierry Reding 	if ((value & SOR_TEST_ATTACHED) != 0)
5236fad8f66SThierry Reding 		return -ETIMEDOUT;
5246fad8f66SThierry Reding 
5256fad8f66SThierry Reding 	return 0;
5266fad8f66SThierry Reding }
5276fad8f66SThierry Reding 
5286fad8f66SThierry Reding static int tegra_sor_power_down(struct tegra_sor *sor)
5296fad8f66SThierry Reding {
5306fad8f66SThierry Reding 	unsigned long value, timeout;
5316fad8f66SThierry Reding 	int err;
5326fad8f66SThierry Reding 
5336fad8f66SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWR);
5346fad8f66SThierry Reding 	value &= ~SOR_PWR_NORMAL_STATE_PU;
5356fad8f66SThierry Reding 	value |= SOR_PWR_TRIGGER;
5366fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWR);
5376fad8f66SThierry Reding 
5386fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
5396fad8f66SThierry Reding 
5406fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
5416fad8f66SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWR);
5426fad8f66SThierry Reding 		if ((value & SOR_PWR_TRIGGER) == 0)
5436fad8f66SThierry Reding 			return 0;
5446fad8f66SThierry Reding 
5456fad8f66SThierry Reding 		usleep_range(25, 100);
5466fad8f66SThierry Reding 	}
5476fad8f66SThierry Reding 
5486fad8f66SThierry Reding 	if ((value & SOR_PWR_TRIGGER) != 0)
5496fad8f66SThierry Reding 		return -ETIMEDOUT;
5506fad8f66SThierry Reding 
5516fad8f66SThierry Reding 	err = clk_set_parent(sor->clk, sor->clk_safe);
5526fad8f66SThierry Reding 	if (err < 0)
5536fad8f66SThierry Reding 		dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
5546fad8f66SThierry Reding 
5556fad8f66SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
5566fad8f66SThierry Reding 	value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
5576fad8f66SThierry Reding 		   SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2);
5586fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
5596fad8f66SThierry Reding 
5606fad8f66SThierry Reding 	/* stop lane sequencer */
5616fad8f66SThierry Reding 	value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_UP |
5626fad8f66SThierry Reding 		SOR_LANE_SEQ_CTL_POWER_STATE_DOWN;
5636fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
5646fad8f66SThierry Reding 
5656fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
5666fad8f66SThierry Reding 
5676fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
5686fad8f66SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
5696fad8f66SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
5706fad8f66SThierry Reding 			break;
5716fad8f66SThierry Reding 
5726fad8f66SThierry Reding 		usleep_range(25, 100);
5736fad8f66SThierry Reding 	}
5746fad8f66SThierry Reding 
5756fad8f66SThierry Reding 	if ((value & SOR_LANE_SEQ_CTL_TRIGGER) != 0)
5766fad8f66SThierry Reding 		return -ETIMEDOUT;
5776fad8f66SThierry Reding 
5786fad8f66SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL_2);
5796fad8f66SThierry Reding 	value |= SOR_PLL_2_PORT_POWERDOWN;
5806fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL_2);
5816fad8f66SThierry Reding 
5826fad8f66SThierry Reding 	usleep_range(20, 100);
5836fad8f66SThierry Reding 
5846fad8f66SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL_0);
5856fad8f66SThierry Reding 	value |= SOR_PLL_0_POWER_OFF;
5866fad8f66SThierry Reding 	value |= SOR_PLL_0_VCOPD;
5876fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL_0);
5886fad8f66SThierry Reding 
5896fad8f66SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL_2);
5906fad8f66SThierry Reding 	value |= SOR_PLL_2_SEQ_PLLCAPPD;
5916fad8f66SThierry Reding 	value |= SOR_PLL_2_SEQ_PLLCAPPD_ENFORCE;
5926fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL_2);
5936fad8f66SThierry Reding 
5946fad8f66SThierry Reding 	usleep_range(20, 100);
5956fad8f66SThierry Reding 
5966fad8f66SThierry Reding 	return 0;
5976fad8f66SThierry Reding }
5986fad8f66SThierry Reding 
5996fad8f66SThierry Reding static int tegra_sor_crc_open(struct inode *inode, struct file *file)
6006fad8f66SThierry Reding {
6016fad8f66SThierry Reding 	file->private_data = inode->i_private;
6026fad8f66SThierry Reding 
6036fad8f66SThierry Reding 	return 0;
6046fad8f66SThierry Reding }
6056fad8f66SThierry Reding 
6066fad8f66SThierry Reding static int tegra_sor_crc_release(struct inode *inode, struct file *file)
6076fad8f66SThierry Reding {
6086fad8f66SThierry Reding 	return 0;
6096fad8f66SThierry Reding }
6106fad8f66SThierry Reding 
6116fad8f66SThierry Reding static int tegra_sor_crc_wait(struct tegra_sor *sor, unsigned long timeout)
6126fad8f66SThierry Reding {
6136fad8f66SThierry Reding 	u32 value;
6146fad8f66SThierry Reding 
6156fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
6166fad8f66SThierry Reding 
6176fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
6186fad8f66SThierry Reding 		value = tegra_sor_readl(sor, SOR_CRC_A);
6196fad8f66SThierry Reding 		if (value & SOR_CRC_A_VALID)
6206fad8f66SThierry Reding 			return 0;
6216fad8f66SThierry Reding 
6226fad8f66SThierry Reding 		usleep_range(100, 200);
6236fad8f66SThierry Reding 	}
6246fad8f66SThierry Reding 
6256fad8f66SThierry Reding 	return -ETIMEDOUT;
6266fad8f66SThierry Reding }
6276fad8f66SThierry Reding 
6286fad8f66SThierry Reding static ssize_t tegra_sor_crc_read(struct file *file, char __user *buffer,
6296fad8f66SThierry Reding 				  size_t size, loff_t *ppos)
6306fad8f66SThierry Reding {
6316fad8f66SThierry Reding 	struct tegra_sor *sor = file->private_data;
6326fad8f66SThierry Reding 	ssize_t num, err;
6336fad8f66SThierry Reding 	char buf[10];
6346fad8f66SThierry Reding 	u32 value;
6356fad8f66SThierry Reding 
6366fad8f66SThierry Reding 	mutex_lock(&sor->lock);
6376fad8f66SThierry Reding 
6386fad8f66SThierry Reding 	if (!sor->enabled) {
6396fad8f66SThierry Reding 		err = -EAGAIN;
6406fad8f66SThierry Reding 		goto unlock;
6416fad8f66SThierry Reding 	}
6426fad8f66SThierry Reding 
6436fad8f66SThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE_1);
6446fad8f66SThierry Reding 	value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
6456fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE_1);
6466fad8f66SThierry Reding 
6476fad8f66SThierry Reding 	value = tegra_sor_readl(sor, SOR_CRC_CNTRL);
6486fad8f66SThierry Reding 	value |= SOR_CRC_CNTRL_ENABLE;
6496fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_CRC_CNTRL);
6506fad8f66SThierry Reding 
6516fad8f66SThierry Reding 	value = tegra_sor_readl(sor, SOR_TEST);
6526fad8f66SThierry Reding 	value &= ~SOR_TEST_CRC_POST_SERIALIZE;
6536fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_TEST);
6546fad8f66SThierry Reding 
6556fad8f66SThierry Reding 	err = tegra_sor_crc_wait(sor, 100);
6566fad8f66SThierry Reding 	if (err < 0)
6576fad8f66SThierry Reding 		goto unlock;
6586fad8f66SThierry Reding 
6596fad8f66SThierry Reding 	tegra_sor_writel(sor, SOR_CRC_A_RESET, SOR_CRC_A);
6606fad8f66SThierry Reding 	value = tegra_sor_readl(sor, SOR_CRC_B);
6616fad8f66SThierry Reding 
6626fad8f66SThierry Reding 	num = scnprintf(buf, sizeof(buf), "%08x\n", value);
6636fad8f66SThierry Reding 
6646fad8f66SThierry Reding 	err = simple_read_from_buffer(buffer, size, ppos, buf, num);
6656fad8f66SThierry Reding 
6666fad8f66SThierry Reding unlock:
6676fad8f66SThierry Reding 	mutex_unlock(&sor->lock);
6686fad8f66SThierry Reding 	return err;
6696fad8f66SThierry Reding }
6706fad8f66SThierry Reding 
6716fad8f66SThierry Reding static const struct file_operations tegra_sor_crc_fops = {
6726fad8f66SThierry Reding 	.owner = THIS_MODULE,
6736fad8f66SThierry Reding 	.open = tegra_sor_crc_open,
6746fad8f66SThierry Reding 	.read = tegra_sor_crc_read,
6756fad8f66SThierry Reding 	.release = tegra_sor_crc_release,
6766fad8f66SThierry Reding };
6776fad8f66SThierry Reding 
678dab16336SThierry Reding static int tegra_sor_show_regs(struct seq_file *s, void *data)
679dab16336SThierry Reding {
680dab16336SThierry Reding 	struct drm_info_node *node = s->private;
681dab16336SThierry Reding 	struct tegra_sor *sor = node->info_ent->data;
682dab16336SThierry Reding 
683dab16336SThierry Reding #define DUMP_REG(name)						\
684dab16336SThierry Reding 	seq_printf(s, "%-38s %#05x %08x\n", #name, name,	\
685dab16336SThierry Reding 		   tegra_sor_readl(sor, name))
686dab16336SThierry Reding 
687dab16336SThierry Reding 	DUMP_REG(SOR_CTXSW);
688dab16336SThierry Reding 	DUMP_REG(SOR_SUPER_STATE_0);
689dab16336SThierry Reding 	DUMP_REG(SOR_SUPER_STATE_1);
690dab16336SThierry Reding 	DUMP_REG(SOR_STATE_0);
691dab16336SThierry Reding 	DUMP_REG(SOR_STATE_1);
692dab16336SThierry Reding 	DUMP_REG(SOR_HEAD_STATE_0(0));
693dab16336SThierry Reding 	DUMP_REG(SOR_HEAD_STATE_0(1));
694dab16336SThierry Reding 	DUMP_REG(SOR_HEAD_STATE_1(0));
695dab16336SThierry Reding 	DUMP_REG(SOR_HEAD_STATE_1(1));
696dab16336SThierry Reding 	DUMP_REG(SOR_HEAD_STATE_2(0));
697dab16336SThierry Reding 	DUMP_REG(SOR_HEAD_STATE_2(1));
698dab16336SThierry Reding 	DUMP_REG(SOR_HEAD_STATE_3(0));
699dab16336SThierry Reding 	DUMP_REG(SOR_HEAD_STATE_3(1));
700dab16336SThierry Reding 	DUMP_REG(SOR_HEAD_STATE_4(0));
701dab16336SThierry Reding 	DUMP_REG(SOR_HEAD_STATE_4(1));
702dab16336SThierry Reding 	DUMP_REG(SOR_HEAD_STATE_5(0));
703dab16336SThierry Reding 	DUMP_REG(SOR_HEAD_STATE_5(1));
704dab16336SThierry Reding 	DUMP_REG(SOR_CRC_CNTRL);
705dab16336SThierry Reding 	DUMP_REG(SOR_DP_DEBUG_MVID);
706dab16336SThierry Reding 	DUMP_REG(SOR_CLK_CNTRL);
707dab16336SThierry Reding 	DUMP_REG(SOR_CAP);
708dab16336SThierry Reding 	DUMP_REG(SOR_PWR);
709dab16336SThierry Reding 	DUMP_REG(SOR_TEST);
710dab16336SThierry Reding 	DUMP_REG(SOR_PLL_0);
711dab16336SThierry Reding 	DUMP_REG(SOR_PLL_1);
712dab16336SThierry Reding 	DUMP_REG(SOR_PLL_2);
713dab16336SThierry Reding 	DUMP_REG(SOR_PLL_3);
714dab16336SThierry Reding 	DUMP_REG(SOR_CSTM);
715dab16336SThierry Reding 	DUMP_REG(SOR_LVDS);
716dab16336SThierry Reding 	DUMP_REG(SOR_CRC_A);
717dab16336SThierry Reding 	DUMP_REG(SOR_CRC_B);
718dab16336SThierry Reding 	DUMP_REG(SOR_BLANK);
719dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_CTL);
720dab16336SThierry Reding 	DUMP_REG(SOR_LANE_SEQ_CTL);
721dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(0));
722dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(1));
723dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(2));
724dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(3));
725dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(4));
726dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(5));
727dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(6));
728dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(7));
729dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(8));
730dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(9));
731dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(10));
732dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(11));
733dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(12));
734dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(13));
735dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(14));
736dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(15));
737dab16336SThierry Reding 	DUMP_REG(SOR_PWM_DIV);
738dab16336SThierry Reding 	DUMP_REG(SOR_PWM_CTL);
739dab16336SThierry Reding 	DUMP_REG(SOR_VCRC_A_0);
740dab16336SThierry Reding 	DUMP_REG(SOR_VCRC_A_1);
741dab16336SThierry Reding 	DUMP_REG(SOR_VCRC_B_0);
742dab16336SThierry Reding 	DUMP_REG(SOR_VCRC_B_1);
743dab16336SThierry Reding 	DUMP_REG(SOR_CCRC_A_0);
744dab16336SThierry Reding 	DUMP_REG(SOR_CCRC_A_1);
745dab16336SThierry Reding 	DUMP_REG(SOR_CCRC_B_0);
746dab16336SThierry Reding 	DUMP_REG(SOR_CCRC_B_1);
747dab16336SThierry Reding 	DUMP_REG(SOR_EDATA_A_0);
748dab16336SThierry Reding 	DUMP_REG(SOR_EDATA_A_1);
749dab16336SThierry Reding 	DUMP_REG(SOR_EDATA_B_0);
750dab16336SThierry Reding 	DUMP_REG(SOR_EDATA_B_1);
751dab16336SThierry Reding 	DUMP_REG(SOR_COUNT_A_0);
752dab16336SThierry Reding 	DUMP_REG(SOR_COUNT_A_1);
753dab16336SThierry Reding 	DUMP_REG(SOR_COUNT_B_0);
754dab16336SThierry Reding 	DUMP_REG(SOR_COUNT_B_1);
755dab16336SThierry Reding 	DUMP_REG(SOR_DEBUG_A_0);
756dab16336SThierry Reding 	DUMP_REG(SOR_DEBUG_A_1);
757dab16336SThierry Reding 	DUMP_REG(SOR_DEBUG_B_0);
758dab16336SThierry Reding 	DUMP_REG(SOR_DEBUG_B_1);
759dab16336SThierry Reding 	DUMP_REG(SOR_TRIG);
760dab16336SThierry Reding 	DUMP_REG(SOR_MSCHECK);
761dab16336SThierry Reding 	DUMP_REG(SOR_XBAR_CTRL);
762dab16336SThierry Reding 	DUMP_REG(SOR_XBAR_POL);
763dab16336SThierry Reding 	DUMP_REG(SOR_DP_LINKCTL_0);
764dab16336SThierry Reding 	DUMP_REG(SOR_DP_LINKCTL_1);
765dab16336SThierry Reding 	DUMP_REG(SOR_LANE_DRIVE_CURRENT_0);
766dab16336SThierry Reding 	DUMP_REG(SOR_LANE_DRIVE_CURRENT_1);
767dab16336SThierry Reding 	DUMP_REG(SOR_LANE4_DRIVE_CURRENT_0);
768dab16336SThierry Reding 	DUMP_REG(SOR_LANE4_DRIVE_CURRENT_1);
769dab16336SThierry Reding 	DUMP_REG(SOR_LANE_PREEMPHASIS_0);
770dab16336SThierry Reding 	DUMP_REG(SOR_LANE_PREEMPHASIS_1);
771dab16336SThierry Reding 	DUMP_REG(SOR_LANE4_PREEMPHASIS_0);
772dab16336SThierry Reding 	DUMP_REG(SOR_LANE4_PREEMPHASIS_1);
773dab16336SThierry Reding 	DUMP_REG(SOR_LANE_POST_CURSOR_0);
774dab16336SThierry Reding 	DUMP_REG(SOR_LANE_POST_CURSOR_1);
775dab16336SThierry Reding 	DUMP_REG(SOR_DP_CONFIG_0);
776dab16336SThierry Reding 	DUMP_REG(SOR_DP_CONFIG_1);
777dab16336SThierry Reding 	DUMP_REG(SOR_DP_MN_0);
778dab16336SThierry Reding 	DUMP_REG(SOR_DP_MN_1);
779dab16336SThierry Reding 	DUMP_REG(SOR_DP_PADCTL_0);
780dab16336SThierry Reding 	DUMP_REG(SOR_DP_PADCTL_1);
781dab16336SThierry Reding 	DUMP_REG(SOR_DP_DEBUG_0);
782dab16336SThierry Reding 	DUMP_REG(SOR_DP_DEBUG_1);
783dab16336SThierry Reding 	DUMP_REG(SOR_DP_SPARE_0);
784dab16336SThierry Reding 	DUMP_REG(SOR_DP_SPARE_1);
785dab16336SThierry Reding 	DUMP_REG(SOR_DP_AUDIO_CTRL);
786dab16336SThierry Reding 	DUMP_REG(SOR_DP_AUDIO_HBLANK_SYMBOLS);
787dab16336SThierry Reding 	DUMP_REG(SOR_DP_AUDIO_VBLANK_SYMBOLS);
788dab16336SThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_HEADER);
789dab16336SThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK_0);
790dab16336SThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK_1);
791dab16336SThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK_2);
792dab16336SThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK_3);
793dab16336SThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK_4);
794dab16336SThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK_5);
795dab16336SThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK_6);
796dab16336SThierry Reding 	DUMP_REG(SOR_DP_TPG);
797dab16336SThierry Reding 	DUMP_REG(SOR_DP_TPG_CONFIG);
798dab16336SThierry Reding 	DUMP_REG(SOR_DP_LQ_CSTM_0);
799dab16336SThierry Reding 	DUMP_REG(SOR_DP_LQ_CSTM_1);
800dab16336SThierry Reding 	DUMP_REG(SOR_DP_LQ_CSTM_2);
801dab16336SThierry Reding 
802dab16336SThierry Reding #undef DUMP_REG
803dab16336SThierry Reding 
804dab16336SThierry Reding 	return 0;
805dab16336SThierry Reding }
806dab16336SThierry Reding 
807dab16336SThierry Reding static const struct drm_info_list debugfs_files[] = {
808dab16336SThierry Reding 	{ "regs", tegra_sor_show_regs, 0, NULL },
809dab16336SThierry Reding };
810dab16336SThierry Reding 
8116fad8f66SThierry Reding static int tegra_sor_debugfs_init(struct tegra_sor *sor,
8126fad8f66SThierry Reding 				  struct drm_minor *minor)
8136fad8f66SThierry Reding {
8146fad8f66SThierry Reding 	struct dentry *entry;
815dab16336SThierry Reding 	unsigned int i;
8166fad8f66SThierry Reding 	int err = 0;
8176fad8f66SThierry Reding 
8186fad8f66SThierry Reding 	sor->debugfs = debugfs_create_dir("sor", minor->debugfs_root);
8196fad8f66SThierry Reding 	if (!sor->debugfs)
8206fad8f66SThierry Reding 		return -ENOMEM;
8216fad8f66SThierry Reding 
822dab16336SThierry Reding 	sor->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
823dab16336SThierry Reding 				     GFP_KERNEL);
824dab16336SThierry Reding 	if (!sor->debugfs_files) {
8256fad8f66SThierry Reding 		err = -ENOMEM;
8266fad8f66SThierry Reding 		goto remove;
8276fad8f66SThierry Reding 	}
8286fad8f66SThierry Reding 
829dab16336SThierry Reding 	for (i = 0; i < ARRAY_SIZE(debugfs_files); i++)
830dab16336SThierry Reding 		sor->debugfs_files[i].data = sor;
831dab16336SThierry Reding 
832dab16336SThierry Reding 	err = drm_debugfs_create_files(sor->debugfs_files,
833dab16336SThierry Reding 				       ARRAY_SIZE(debugfs_files),
834dab16336SThierry Reding 				       sor->debugfs, minor);
835dab16336SThierry Reding 	if (err < 0)
836dab16336SThierry Reding 		goto free;
837dab16336SThierry Reding 
838dab16336SThierry Reding 	entry = debugfs_create_file("crc", 0644, sor->debugfs, sor,
839dab16336SThierry Reding 				    &tegra_sor_crc_fops);
840dab16336SThierry Reding 	if (!entry) {
841dab16336SThierry Reding 		err = -ENOMEM;
842dab16336SThierry Reding 		goto free;
843dab16336SThierry Reding 	}
844dab16336SThierry Reding 
8456fad8f66SThierry Reding 	return err;
8466fad8f66SThierry Reding 
847dab16336SThierry Reding free:
848dab16336SThierry Reding 	kfree(sor->debugfs_files);
849dab16336SThierry Reding 	sor->debugfs_files = NULL;
8506fad8f66SThierry Reding remove:
851dab16336SThierry Reding 	debugfs_remove_recursive(sor->debugfs);
8526fad8f66SThierry Reding 	sor->debugfs = NULL;
8536fad8f66SThierry Reding 	return err;
8546fad8f66SThierry Reding }
8556fad8f66SThierry Reding 
8564009c224SThierry Reding static void tegra_sor_debugfs_exit(struct tegra_sor *sor)
8576fad8f66SThierry Reding {
858dab16336SThierry Reding 	drm_debugfs_remove_files(sor->debugfs_files, ARRAY_SIZE(debugfs_files),
859dab16336SThierry Reding 				 sor->minor);
860dab16336SThierry Reding 	sor->minor = NULL;
861dab16336SThierry Reding 
862dab16336SThierry Reding 	kfree(sor->debugfs_files);
8636fad8f66SThierry Reding 	sor->debugfs = NULL;
864dab16336SThierry Reding 
865dab16336SThierry Reding 	debugfs_remove_recursive(sor->debugfs);
866dab16336SThierry Reding 	sor->debugfs_files = NULL;
8676fad8f66SThierry Reding }
8686fad8f66SThierry Reding 
8696fad8f66SThierry Reding static void tegra_sor_connector_dpms(struct drm_connector *connector, int mode)
8706fad8f66SThierry Reding {
8716fad8f66SThierry Reding }
8726fad8f66SThierry Reding 
8736fad8f66SThierry Reding static enum drm_connector_status
8746fad8f66SThierry Reding tegra_sor_connector_detect(struct drm_connector *connector, bool force)
8756fad8f66SThierry Reding {
8766fad8f66SThierry Reding 	struct tegra_output *output = connector_to_output(connector);
8776fad8f66SThierry Reding 	struct tegra_sor *sor = to_sor(output);
8786fad8f66SThierry Reding 
8796fad8f66SThierry Reding 	if (sor->dpaux)
8806fad8f66SThierry Reding 		return tegra_dpaux_detect(sor->dpaux);
8816fad8f66SThierry Reding 
8826fad8f66SThierry Reding 	return connector_status_unknown;
8836fad8f66SThierry Reding }
8846fad8f66SThierry Reding 
8856fad8f66SThierry Reding static const struct drm_connector_funcs tegra_sor_connector_funcs = {
8866fad8f66SThierry Reding 	.dpms = tegra_sor_connector_dpms,
8879d44189fSThierry Reding 	.reset = drm_atomic_helper_connector_reset,
8886fad8f66SThierry Reding 	.detect = tegra_sor_connector_detect,
8896fad8f66SThierry Reding 	.fill_modes = drm_helper_probe_single_connector_modes,
8906fad8f66SThierry Reding 	.destroy = tegra_output_connector_destroy,
8919d44189fSThierry Reding 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
8924aa3df71SThierry Reding 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
8936fad8f66SThierry Reding };
8946fad8f66SThierry Reding 
8956fad8f66SThierry Reding static int tegra_sor_connector_get_modes(struct drm_connector *connector)
8966fad8f66SThierry Reding {
8976fad8f66SThierry Reding 	struct tegra_output *output = connector_to_output(connector);
8986fad8f66SThierry Reding 	struct tegra_sor *sor = to_sor(output);
8996fad8f66SThierry Reding 	int err;
9006fad8f66SThierry Reding 
9016fad8f66SThierry Reding 	if (sor->dpaux)
9026fad8f66SThierry Reding 		tegra_dpaux_enable(sor->dpaux);
9036fad8f66SThierry Reding 
9046fad8f66SThierry Reding 	err = tegra_output_connector_get_modes(connector);
9056fad8f66SThierry Reding 
9066fad8f66SThierry Reding 	if (sor->dpaux)
9076fad8f66SThierry Reding 		tegra_dpaux_disable(sor->dpaux);
9086fad8f66SThierry Reding 
9096fad8f66SThierry Reding 	return err;
9106fad8f66SThierry Reding }
9116fad8f66SThierry Reding 
9126fad8f66SThierry Reding static enum drm_mode_status
9136fad8f66SThierry Reding tegra_sor_connector_mode_valid(struct drm_connector *connector,
9146fad8f66SThierry Reding 			       struct drm_display_mode *mode)
9156fad8f66SThierry Reding {
9166fad8f66SThierry Reding 	return MODE_OK;
9176fad8f66SThierry Reding }
9186fad8f66SThierry Reding 
9196fad8f66SThierry Reding static const struct drm_connector_helper_funcs tegra_sor_connector_helper_funcs = {
9206fad8f66SThierry Reding 	.get_modes = tegra_sor_connector_get_modes,
9216fad8f66SThierry Reding 	.mode_valid = tegra_sor_connector_mode_valid,
9226fad8f66SThierry Reding 	.best_encoder = tegra_output_connector_best_encoder,
9236fad8f66SThierry Reding };
9246fad8f66SThierry Reding 
9256fad8f66SThierry Reding static const struct drm_encoder_funcs tegra_sor_encoder_funcs = {
9266fad8f66SThierry Reding 	.destroy = tegra_output_encoder_destroy,
9276fad8f66SThierry Reding };
9286fad8f66SThierry Reding 
9296fad8f66SThierry Reding static void tegra_sor_encoder_dpms(struct drm_encoder *encoder, int mode)
9306fad8f66SThierry Reding {
9316fad8f66SThierry Reding }
9326fad8f66SThierry Reding 
9336fad8f66SThierry Reding static void tegra_sor_encoder_prepare(struct drm_encoder *encoder)
9346fad8f66SThierry Reding {
9356fad8f66SThierry Reding }
9366fad8f66SThierry Reding 
9376fad8f66SThierry Reding static void tegra_sor_encoder_commit(struct drm_encoder *encoder)
9386fad8f66SThierry Reding {
9396fad8f66SThierry Reding }
9406fad8f66SThierry Reding 
9416fad8f66SThierry Reding static void tegra_sor_encoder_mode_set(struct drm_encoder *encoder,
9426fad8f66SThierry Reding 				       struct drm_display_mode *mode,
9436fad8f66SThierry Reding 				       struct drm_display_mode *adjusted)
9446fad8f66SThierry Reding {
9456fad8f66SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
9466fad8f66SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
9476b6b6042SThierry Reding 	unsigned int vbe, vse, hbe, hse, vbs, hbs, i;
9486b6b6042SThierry Reding 	struct tegra_sor *sor = to_sor(output);
94934fa183bSThierry Reding 	struct tegra_sor_config config;
95034fa183bSThierry Reding 	struct drm_dp_link link;
95134fa183bSThierry Reding 	struct drm_dp_aux *aux;
95286f5c52dSThierry Reding 	int err = 0;
95328fe2076SThierry Reding 	u32 value;
95486f5c52dSThierry Reding 
95586f5c52dSThierry Reding 	mutex_lock(&sor->lock);
9566b6b6042SThierry Reding 
9576b6b6042SThierry Reding 	if (sor->enabled)
95886f5c52dSThierry Reding 		goto unlock;
9596b6b6042SThierry Reding 
9606b6b6042SThierry Reding 	err = clk_prepare_enable(sor->clk);
9616b6b6042SThierry Reding 	if (err < 0)
96286f5c52dSThierry Reding 		goto unlock;
9636b6b6042SThierry Reding 
9646b6b6042SThierry Reding 	reset_control_deassert(sor->rst);
9656b6b6042SThierry Reding 
9666fad8f66SThierry Reding 	if (output->panel)
9676fad8f66SThierry Reding 		drm_panel_prepare(output->panel);
9686fad8f66SThierry Reding 
96934fa183bSThierry Reding 	/* FIXME: properly convert to struct drm_dp_aux */
97034fa183bSThierry Reding 	aux = (struct drm_dp_aux *)sor->dpaux;
97134fa183bSThierry Reding 
9726b6b6042SThierry Reding 	if (sor->dpaux) {
9736b6b6042SThierry Reding 		err = tegra_dpaux_enable(sor->dpaux);
9746b6b6042SThierry Reding 		if (err < 0)
9756b6b6042SThierry Reding 			dev_err(sor->dev, "failed to enable DP: %d\n", err);
97634fa183bSThierry Reding 
97734fa183bSThierry Reding 		err = drm_dp_link_probe(aux, &link);
97834fa183bSThierry Reding 		if (err < 0) {
97934fa183bSThierry Reding 			dev_err(sor->dev, "failed to probe eDP link: %d\n",
98034fa183bSThierry Reding 				err);
9812263c460SDan Carpenter 			goto unlock;
98234fa183bSThierry Reding 		}
9836b6b6042SThierry Reding 	}
9846b6b6042SThierry Reding 
9856b6b6042SThierry Reding 	err = clk_set_parent(sor->clk, sor->clk_safe);
9866b6b6042SThierry Reding 	if (err < 0)
9876b6b6042SThierry Reding 		dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
9886b6b6042SThierry Reding 
98934fa183bSThierry Reding 	memset(&config, 0, sizeof(config));
990054b1bd1SStéphane Marchesin 	config.bits_per_pixel = output->connector.display_info.bpc * 3;
99134fa183bSThierry Reding 
99234fa183bSThierry Reding 	err = tegra_sor_calc_config(sor, mode, &config, &link);
99334fa183bSThierry Reding 	if (err < 0)
99434fa183bSThierry Reding 		dev_err(sor->dev, "failed to compute link configuration: %d\n",
99534fa183bSThierry Reding 			err);
99634fa183bSThierry Reding 
9976b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
9986b6b6042SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
9996b6b6042SThierry Reding 	value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
10006b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
10016b6b6042SThierry Reding 
10026b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL_2);
10036b6b6042SThierry Reding 	value &= ~SOR_PLL_2_BANDGAP_POWERDOWN;
10046b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL_2);
10056b6b6042SThierry Reding 	usleep_range(20, 100);
10066b6b6042SThierry Reding 
10076b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL_3);
10086b6b6042SThierry Reding 	value |= SOR_PLL_3_PLL_VDD_MODE_V3_3;
10096b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL_3);
10106b6b6042SThierry Reding 
10116b6b6042SThierry Reding 	value = SOR_PLL_0_ICHPMP(0xf) | SOR_PLL_0_VCOCAP_RST |
10126b6b6042SThierry Reding 		SOR_PLL_0_PLLREG_LEVEL_V45 | SOR_PLL_0_RESISTOR_EXT;
10136b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL_0);
10146b6b6042SThierry Reding 
10156b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL_2);
10166b6b6042SThierry Reding 	value |= SOR_PLL_2_SEQ_PLLCAPPD;
10176b6b6042SThierry Reding 	value &= ~SOR_PLL_2_SEQ_PLLCAPPD_ENFORCE;
10186b6b6042SThierry Reding 	value |= SOR_PLL_2_LVDS_ENABLE;
10196b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL_2);
10206b6b6042SThierry Reding 
10216b6b6042SThierry Reding 	value = SOR_PLL_1_TERM_COMPOUT | SOR_PLL_1_TMDS_TERM;
10226b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL_1);
10236b6b6042SThierry Reding 
10246b6b6042SThierry Reding 	while (true) {
10256b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_PLL_2);
10266b6b6042SThierry Reding 		if ((value & SOR_PLL_2_SEQ_PLLCAPPD_ENFORCE) == 0)
10276b6b6042SThierry Reding 			break;
10286b6b6042SThierry Reding 
10296b6b6042SThierry Reding 		usleep_range(250, 1000);
10306b6b6042SThierry Reding 	}
10316b6b6042SThierry Reding 
10326b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL_2);
10336b6b6042SThierry Reding 	value &= ~SOR_PLL_2_POWERDOWN_OVERRIDE;
10346b6b6042SThierry Reding 	value &= ~SOR_PLL_2_PORT_POWERDOWN;
10356b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL_2);
10366b6b6042SThierry Reding 
10376b6b6042SThierry Reding 	/*
10386b6b6042SThierry Reding 	 * power up
10396b6b6042SThierry Reding 	 */
10406b6b6042SThierry Reding 
10416b6b6042SThierry Reding 	/* set safe link bandwidth (1.62 Gbps) */
10426b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
10436b6b6042SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
10446b6b6042SThierry Reding 	value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G1_62;
10456b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
10466b6b6042SThierry Reding 
10476b6b6042SThierry Reding 	/* step 1 */
10486b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL_2);
10496b6b6042SThierry Reding 	value |= SOR_PLL_2_SEQ_PLLCAPPD_ENFORCE | SOR_PLL_2_PORT_POWERDOWN |
10506b6b6042SThierry Reding 		 SOR_PLL_2_BANDGAP_POWERDOWN;
10516b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL_2);
10526b6b6042SThierry Reding 
10536b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL_0);
10546b6b6042SThierry Reding 	value |= SOR_PLL_0_VCOPD | SOR_PLL_0_POWER_OFF;
10556b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL_0);
10566b6b6042SThierry Reding 
10576b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
10586b6b6042SThierry Reding 	value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
10596b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
10606b6b6042SThierry Reding 
10616b6b6042SThierry Reding 	/* step 2 */
10626b6b6042SThierry Reding 	err = tegra_io_rail_power_on(TEGRA_IO_RAIL_LVDS);
10636b6b6042SThierry Reding 	if (err < 0) {
10646b6b6042SThierry Reding 		dev_err(sor->dev, "failed to power on I/O rail: %d\n", err);
106586f5c52dSThierry Reding 		goto unlock;
10666b6b6042SThierry Reding 	}
10676b6b6042SThierry Reding 
10686b6b6042SThierry Reding 	usleep_range(5, 100);
10696b6b6042SThierry Reding 
10706b6b6042SThierry Reding 	/* step 3 */
10716b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL_2);
10726b6b6042SThierry Reding 	value &= ~SOR_PLL_2_BANDGAP_POWERDOWN;
10736b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL_2);
10746b6b6042SThierry Reding 
10756b6b6042SThierry Reding 	usleep_range(20, 100);
10766b6b6042SThierry Reding 
10776b6b6042SThierry Reding 	/* step 4 */
10786b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL_0);
10796b6b6042SThierry Reding 	value &= ~SOR_PLL_0_POWER_OFF;
10806b6b6042SThierry Reding 	value &= ~SOR_PLL_0_VCOPD;
10816b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL_0);
10826b6b6042SThierry Reding 
10836b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL_2);
10846b6b6042SThierry Reding 	value &= ~SOR_PLL_2_SEQ_PLLCAPPD_ENFORCE;
10856b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL_2);
10866b6b6042SThierry Reding 
10876b6b6042SThierry Reding 	usleep_range(200, 1000);
10886b6b6042SThierry Reding 
10896b6b6042SThierry Reding 	/* step 5 */
10906b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL_2);
10916b6b6042SThierry Reding 	value &= ~SOR_PLL_2_PORT_POWERDOWN;
10926b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL_2);
10936b6b6042SThierry Reding 
10946b6b6042SThierry Reding 	/* switch to DP clock */
10956b6b6042SThierry Reding 	err = clk_set_parent(sor->clk, sor->clk_dp);
10966b6b6042SThierry Reding 	if (err < 0)
10976b6b6042SThierry Reding 		dev_err(sor->dev, "failed to set DP parent clock: %d\n", err);
10986b6b6042SThierry Reding 
1099899451b7SThierry Reding 	/* power DP lanes */
11006b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
1101899451b7SThierry Reding 
1102899451b7SThierry Reding 	if (link.num_lanes <= 2)
1103899451b7SThierry Reding 		value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2);
1104899451b7SThierry Reding 	else
1105899451b7SThierry Reding 		value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2;
1106899451b7SThierry Reding 
1107899451b7SThierry Reding 	if (link.num_lanes <= 1)
1108899451b7SThierry Reding 		value &= ~SOR_DP_PADCTL_PD_TXD_1;
1109899451b7SThierry Reding 	else
1110899451b7SThierry Reding 		value |= SOR_DP_PADCTL_PD_TXD_1;
1111899451b7SThierry Reding 
1112899451b7SThierry Reding 	if (link.num_lanes == 0)
1113899451b7SThierry Reding 		value &= ~SOR_DP_PADCTL_PD_TXD_0;
1114899451b7SThierry Reding 	else
1115899451b7SThierry Reding 		value |= SOR_DP_PADCTL_PD_TXD_0;
1116899451b7SThierry Reding 
11176b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
11186b6b6042SThierry Reding 
11196b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL_0);
11206b6b6042SThierry Reding 	value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
11210c90a184SThierry Reding 	value |= SOR_DP_LINKCTL_LANE_COUNT(link.num_lanes);
11226b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL_0);
11236b6b6042SThierry Reding 
11246b6b6042SThierry Reding 	/* start lane sequencer */
11256b6b6042SThierry Reding 	value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
11266b6b6042SThierry Reding 		SOR_LANE_SEQ_CTL_POWER_STATE_UP;
11276b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
11286b6b6042SThierry Reding 
11296b6b6042SThierry Reding 	while (true) {
11306b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
11316b6b6042SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
11326b6b6042SThierry Reding 			break;
11336b6b6042SThierry Reding 
11346b6b6042SThierry Reding 		usleep_range(250, 1000);
11356b6b6042SThierry Reding 	}
11366b6b6042SThierry Reding 
1137a4263fedSThierry Reding 	/* set link bandwidth */
11386b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
11396b6b6042SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
1140a4263fedSThierry Reding 	value |= drm_dp_link_rate_to_bw_code(link.rate) << 2;
11416b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
11426b6b6042SThierry Reding 
11436b6b6042SThierry Reding 	/* set linkctl */
11446b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL_0);
11456b6b6042SThierry Reding 	value |= SOR_DP_LINKCTL_ENABLE;
11466b6b6042SThierry Reding 
11476b6b6042SThierry Reding 	value &= ~SOR_DP_LINKCTL_TU_SIZE_MASK;
114834fa183bSThierry Reding 	value |= SOR_DP_LINKCTL_TU_SIZE(config.tu_size);
11496b6b6042SThierry Reding 
11506b6b6042SThierry Reding 	value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
11516b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL_0);
11526b6b6042SThierry Reding 
11536b6b6042SThierry Reding 	for (i = 0, value = 0; i < 4; i++) {
11546b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
11556b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_GALIOS |
11566b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_NONE;
11576b6b6042SThierry Reding 		value = (value << 8) | lane;
11586b6b6042SThierry Reding 	}
11596b6b6042SThierry Reding 
11606b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
11616b6b6042SThierry Reding 
11626b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_CONFIG_0);
11636b6b6042SThierry Reding 	value &= ~SOR_DP_CONFIG_WATERMARK_MASK;
116434fa183bSThierry Reding 	value |= SOR_DP_CONFIG_WATERMARK(config.watermark);
11656b6b6042SThierry Reding 
11666b6b6042SThierry Reding 	value &= ~SOR_DP_CONFIG_ACTIVE_SYM_COUNT_MASK;
116734fa183bSThierry Reding 	value |= SOR_DP_CONFIG_ACTIVE_SYM_COUNT(config.active_count);
11686b6b6042SThierry Reding 
11696b6b6042SThierry Reding 	value &= ~SOR_DP_CONFIG_ACTIVE_SYM_FRAC_MASK;
117034fa183bSThierry Reding 	value |= SOR_DP_CONFIG_ACTIVE_SYM_FRAC(config.active_frac);
11716b6b6042SThierry Reding 
117234fa183bSThierry Reding 	if (config.active_polarity)
117334fa183bSThierry Reding 		value |= SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
117434fa183bSThierry Reding 	else
117534fa183bSThierry Reding 		value &= ~SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
11766b6b6042SThierry Reding 
11776b6b6042SThierry Reding 	value |= SOR_DP_CONFIG_ACTIVE_SYM_ENABLE;
11781f64ae7cSThierry Reding 	value |= SOR_DP_CONFIG_DISPARITY_NEGATIVE;
11796b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_CONFIG_0);
11806b6b6042SThierry Reding 
11816b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_AUDIO_HBLANK_SYMBOLS);
11826b6b6042SThierry Reding 	value &= ~SOR_DP_AUDIO_HBLANK_SYMBOLS_MASK;
11837890b576SThierry Reding 	value |= config.hblank_symbols & 0xffff;
11846b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_AUDIO_HBLANK_SYMBOLS);
11856b6b6042SThierry Reding 
11866b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_AUDIO_VBLANK_SYMBOLS);
11876b6b6042SThierry Reding 	value &= ~SOR_DP_AUDIO_VBLANK_SYMBOLS_MASK;
11887890b576SThierry Reding 	value |= config.vblank_symbols & 0xffff;
11896b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_AUDIO_VBLANK_SYMBOLS);
11906b6b6042SThierry Reding 
11916b6b6042SThierry Reding 	/* enable pad calibration logic */
11926b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
11936b6b6042SThierry Reding 	value |= SOR_DP_PADCTL_PAD_CAL_PD;
11946b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
11956b6b6042SThierry Reding 
11966b6b6042SThierry Reding 	if (sor->dpaux) {
11976b6b6042SThierry Reding 		u8 rate, lanes;
11986b6b6042SThierry Reding 
11996b6b6042SThierry Reding 		err = drm_dp_link_probe(aux, &link);
12006b6b6042SThierry Reding 		if (err < 0) {
12016b6b6042SThierry Reding 			dev_err(sor->dev, "failed to probe eDP link: %d\n",
12026b6b6042SThierry Reding 				err);
120386f5c52dSThierry Reding 			goto unlock;
12046b6b6042SThierry Reding 		}
12056b6b6042SThierry Reding 
12066b6b6042SThierry Reding 		err = drm_dp_link_power_up(aux, &link);
12076b6b6042SThierry Reding 		if (err < 0) {
12086b6b6042SThierry Reding 			dev_err(sor->dev, "failed to power up eDP link: %d\n",
12096b6b6042SThierry Reding 				err);
121086f5c52dSThierry Reding 			goto unlock;
12116b6b6042SThierry Reding 		}
12126b6b6042SThierry Reding 
12136b6b6042SThierry Reding 		err = drm_dp_link_configure(aux, &link);
12146b6b6042SThierry Reding 		if (err < 0) {
12156b6b6042SThierry Reding 			dev_err(sor->dev, "failed to configure eDP link: %d\n",
12166b6b6042SThierry Reding 				err);
121786f5c52dSThierry Reding 			goto unlock;
12186b6b6042SThierry Reding 		}
12196b6b6042SThierry Reding 
12206b6b6042SThierry Reding 		rate = drm_dp_link_rate_to_bw_code(link.rate);
12216b6b6042SThierry Reding 		lanes = link.num_lanes;
12226b6b6042SThierry Reding 
12236b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
12246b6b6042SThierry Reding 		value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
12256b6b6042SThierry Reding 		value |= SOR_CLK_CNTRL_DP_LINK_SPEED(rate);
12266b6b6042SThierry Reding 		tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
12276b6b6042SThierry Reding 
12286b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_DP_LINKCTL_0);
12296b6b6042SThierry Reding 		value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
12306b6b6042SThierry Reding 		value |= SOR_DP_LINKCTL_LANE_COUNT(lanes);
12316b6b6042SThierry Reding 
12326b6b6042SThierry Reding 		if (link.capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
12336b6b6042SThierry Reding 			value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
12346b6b6042SThierry Reding 
12356b6b6042SThierry Reding 		tegra_sor_writel(sor, value, SOR_DP_LINKCTL_0);
12366b6b6042SThierry Reding 
12376b6b6042SThierry Reding 		/* disable training pattern generator */
12386b6b6042SThierry Reding 
12396b6b6042SThierry Reding 		for (i = 0; i < link.num_lanes; i++) {
12406b6b6042SThierry Reding 			unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
12416b6b6042SThierry Reding 					     SOR_DP_TPG_SCRAMBLER_GALIOS |
12426b6b6042SThierry Reding 					     SOR_DP_TPG_PATTERN_NONE;
12436b6b6042SThierry Reding 			value = (value << 8) | lane;
12446b6b6042SThierry Reding 		}
12456b6b6042SThierry Reding 
12466b6b6042SThierry Reding 		tegra_sor_writel(sor, value, SOR_DP_TPG);
12476b6b6042SThierry Reding 
12486b6b6042SThierry Reding 		err = tegra_sor_dp_train_fast(sor, &link);
12496b6b6042SThierry Reding 		if (err < 0) {
12506b6b6042SThierry Reding 			dev_err(sor->dev, "DP fast link training failed: %d\n",
12516b6b6042SThierry Reding 				err);
125286f5c52dSThierry Reding 			goto unlock;
12536b6b6042SThierry Reding 		}
12546b6b6042SThierry Reding 
12556b6b6042SThierry Reding 		dev_dbg(sor->dev, "fast link training succeeded\n");
12566b6b6042SThierry Reding 	}
12576b6b6042SThierry Reding 
12586b6b6042SThierry Reding 	err = tegra_sor_power_up(sor, 250);
12596b6b6042SThierry Reding 	if (err < 0) {
12606b6b6042SThierry Reding 		dev_err(sor->dev, "failed to power up SOR: %d\n", err);
126186f5c52dSThierry Reding 		goto unlock;
12626b6b6042SThierry Reding 	}
12636b6b6042SThierry Reding 
12646b6b6042SThierry Reding 	/*
12656b6b6042SThierry Reding 	 * configure panel (24bpp, vsync-, hsync-, DP-A protocol, complete
12666b6b6042SThierry Reding 	 * raster, associate with display controller)
12676b6b6042SThierry Reding 	 */
12683f4f3b5fSThierry Reding 	value = SOR_STATE_ASY_PROTOCOL_DP_A |
12696b6b6042SThierry Reding 		SOR_STATE_ASY_CRC_MODE_COMPLETE |
12706b6b6042SThierry Reding 		SOR_STATE_ASY_OWNER(dc->pipe + 1);
127134fa183bSThierry Reding 
12723f4f3b5fSThierry Reding 	if (mode->flags & DRM_MODE_FLAG_PHSYNC)
12733f4f3b5fSThierry Reding 		value &= ~SOR_STATE_ASY_HSYNCPOL;
12743f4f3b5fSThierry Reding 
12753f4f3b5fSThierry Reding 	if (mode->flags & DRM_MODE_FLAG_NHSYNC)
12763f4f3b5fSThierry Reding 		value |= SOR_STATE_ASY_HSYNCPOL;
12773f4f3b5fSThierry Reding 
12783f4f3b5fSThierry Reding 	if (mode->flags & DRM_MODE_FLAG_PVSYNC)
12793f4f3b5fSThierry Reding 		value &= ~SOR_STATE_ASY_VSYNCPOL;
12803f4f3b5fSThierry Reding 
12813f4f3b5fSThierry Reding 	if (mode->flags & DRM_MODE_FLAG_NVSYNC)
12823f4f3b5fSThierry Reding 		value |= SOR_STATE_ASY_VSYNCPOL;
12833f4f3b5fSThierry Reding 
128434fa183bSThierry Reding 	switch (config.bits_per_pixel) {
128534fa183bSThierry Reding 	case 24:
128634fa183bSThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
128734fa183bSThierry Reding 		break;
128834fa183bSThierry Reding 
128934fa183bSThierry Reding 	case 18:
129034fa183bSThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_18_444;
129134fa183bSThierry Reding 		break;
129234fa183bSThierry Reding 
129334fa183bSThierry Reding 	default:
129434fa183bSThierry Reding 		BUG();
129534fa183bSThierry Reding 		break;
129634fa183bSThierry Reding 	}
129734fa183bSThierry Reding 
12986b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE_1);
12996b6b6042SThierry Reding 
13006b6b6042SThierry Reding 	/*
13016b6b6042SThierry Reding 	 * TODO: The video timing programming below doesn't seem to match the
13026b6b6042SThierry Reding 	 * register definitions.
13036b6b6042SThierry Reding 	 */
13046b6b6042SThierry Reding 
13056b6b6042SThierry Reding 	value = ((mode->vtotal & 0x7fff) << 16) | (mode->htotal & 0x7fff);
13066b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_HEAD_STATE_1(0));
13076b6b6042SThierry Reding 
13086b6b6042SThierry Reding 	vse = mode->vsync_end - mode->vsync_start - 1;
13096b6b6042SThierry Reding 	hse = mode->hsync_end - mode->hsync_start - 1;
13106b6b6042SThierry Reding 
13116b6b6042SThierry Reding 	value = ((vse & 0x7fff) << 16) | (hse & 0x7fff);
13126b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_HEAD_STATE_2(0));
13136b6b6042SThierry Reding 
13146b6b6042SThierry Reding 	vbe = vse + (mode->vsync_start - mode->vdisplay);
13156b6b6042SThierry Reding 	hbe = hse + (mode->hsync_start - mode->hdisplay);
13166b6b6042SThierry Reding 
13176b6b6042SThierry Reding 	value = ((vbe & 0x7fff) << 16) | (hbe & 0x7fff);
13186b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_HEAD_STATE_3(0));
13196b6b6042SThierry Reding 
13206b6b6042SThierry Reding 	vbs = vbe + mode->vdisplay;
13216b6b6042SThierry Reding 	hbs = hbe + mode->hdisplay;
13226b6b6042SThierry Reding 
13236b6b6042SThierry Reding 	value = ((vbs & 0x7fff) << 16) | (hbs & 0x7fff);
13246b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_HEAD_STATE_4(0));
13256b6b6042SThierry Reding 
13266b6b6042SThierry Reding 	/* CSTM (LVDS, link A/B, upper) */
1327143b1df2SStéphane Marchesin 	value = SOR_CSTM_LVDS | SOR_CSTM_LINK_ACT_A | SOR_CSTM_LINK_ACT_B |
13286b6b6042SThierry Reding 		SOR_CSTM_UPPER;
13296b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CSTM);
13306b6b6042SThierry Reding 
13316b6b6042SThierry Reding 	/* PWM setup */
13326b6b6042SThierry Reding 	err = tegra_sor_setup_pwm(sor, 250);
13336b6b6042SThierry Reding 	if (err < 0) {
13346b6b6042SThierry Reding 		dev_err(sor->dev, "failed to setup PWM: %d\n", err);
133586f5c52dSThierry Reding 		goto unlock;
13366b6b6042SThierry Reding 	}
13376b6b6042SThierry Reding 
1338666cb873SThierry Reding 	tegra_sor_update(sor);
1339666cb873SThierry Reding 
13406b6b6042SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
13416b6b6042SThierry Reding 	value |= SOR_ENABLE;
13426b6b6042SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
13436b6b6042SThierry Reding 
1344666cb873SThierry Reding 	tegra_dc_commit(dc);
13456b6b6042SThierry Reding 
13466b6b6042SThierry Reding 	err = tegra_sor_attach(sor);
13476b6b6042SThierry Reding 	if (err < 0) {
13486b6b6042SThierry Reding 		dev_err(sor->dev, "failed to attach SOR: %d\n", err);
134986f5c52dSThierry Reding 		goto unlock;
13506b6b6042SThierry Reding 	}
13516b6b6042SThierry Reding 
13526b6b6042SThierry Reding 	err = tegra_sor_wakeup(sor);
13536b6b6042SThierry Reding 	if (err < 0) {
13546b6b6042SThierry Reding 		dev_err(sor->dev, "failed to enable DC: %d\n", err);
135586f5c52dSThierry Reding 		goto unlock;
13566b6b6042SThierry Reding 	}
13576b6b6042SThierry Reding 
13586fad8f66SThierry Reding 	if (output->panel)
13596fad8f66SThierry Reding 		drm_panel_enable(output->panel);
13606fad8f66SThierry Reding 
13616b6b6042SThierry Reding 	sor->enabled = true;
13626b6b6042SThierry Reding 
136386f5c52dSThierry Reding unlock:
136486f5c52dSThierry Reding 	mutex_unlock(&sor->lock);
13656b6b6042SThierry Reding }
13666b6b6042SThierry Reding 
13676fad8f66SThierry Reding static void tegra_sor_encoder_disable(struct drm_encoder *encoder)
13686b6b6042SThierry Reding {
13696fad8f66SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
13706fad8f66SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
13716b6b6042SThierry Reding 	struct tegra_sor *sor = to_sor(output);
13726fad8f66SThierry Reding 	u32 value;
13736fad8f66SThierry Reding 	int err;
137486f5c52dSThierry Reding 
137586f5c52dSThierry Reding 	mutex_lock(&sor->lock);
13766b6b6042SThierry Reding 
13776b6b6042SThierry Reding 	if (!sor->enabled)
137886f5c52dSThierry Reding 		goto unlock;
13796b6b6042SThierry Reding 
13806fad8f66SThierry Reding 	if (output->panel)
13816fad8f66SThierry Reding 		drm_panel_disable(output->panel);
13826fad8f66SThierry Reding 
13836b6b6042SThierry Reding 	err = tegra_sor_detach(sor);
13846b6b6042SThierry Reding 	if (err < 0) {
13856b6b6042SThierry Reding 		dev_err(sor->dev, "failed to detach SOR: %d\n", err);
138686f5c52dSThierry Reding 		goto unlock;
13876b6b6042SThierry Reding 	}
13886b6b6042SThierry Reding 
13896b6b6042SThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE_1);
13906b6b6042SThierry Reding 	tegra_sor_update(sor);
13916b6b6042SThierry Reding 
13926b6b6042SThierry Reding 	/*
13936b6b6042SThierry Reding 	 * The following accesses registers of the display controller, so make
13946b6b6042SThierry Reding 	 * sure it's only executed when the output is attached to one.
13956b6b6042SThierry Reding 	 */
13966b6b6042SThierry Reding 	if (dc) {
13976b6b6042SThierry Reding 		value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
13986b6b6042SThierry Reding 		value &= ~SOR_ENABLE;
13996b6b6042SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
14006b6b6042SThierry Reding 
140162b9e063SThierry Reding 		tegra_dc_commit(dc);
14026b6b6042SThierry Reding 	}
14036b6b6042SThierry Reding 
14046b6b6042SThierry Reding 	err = tegra_sor_power_down(sor);
14056b6b6042SThierry Reding 	if (err < 0) {
14066b6b6042SThierry Reding 		dev_err(sor->dev, "failed to power down SOR: %d\n", err);
140786f5c52dSThierry Reding 		goto unlock;
14086b6b6042SThierry Reding 	}
14096b6b6042SThierry Reding 
14106b6b6042SThierry Reding 	if (sor->dpaux) {
14116b6b6042SThierry Reding 		err = tegra_dpaux_disable(sor->dpaux);
14126b6b6042SThierry Reding 		if (err < 0) {
14136b6b6042SThierry Reding 			dev_err(sor->dev, "failed to disable DP: %d\n", err);
141486f5c52dSThierry Reding 			goto unlock;
14156b6b6042SThierry Reding 		}
14166b6b6042SThierry Reding 	}
14176b6b6042SThierry Reding 
14186b6b6042SThierry Reding 	err = tegra_io_rail_power_off(TEGRA_IO_RAIL_LVDS);
14196b6b6042SThierry Reding 	if (err < 0) {
14206b6b6042SThierry Reding 		dev_err(sor->dev, "failed to power off I/O rail: %d\n", err);
142186f5c52dSThierry Reding 		goto unlock;
14226b6b6042SThierry Reding 	}
14236b6b6042SThierry Reding 
14246fad8f66SThierry Reding 	if (output->panel)
14256fad8f66SThierry Reding 		drm_panel_unprepare(output->panel);
14266fad8f66SThierry Reding 
14276b6b6042SThierry Reding 	clk_disable_unprepare(sor->clk);
14286fad8f66SThierry Reding 	reset_control_assert(sor->rst);
14296b6b6042SThierry Reding 
14306b6b6042SThierry Reding 	sor->enabled = false;
14316b6b6042SThierry Reding 
143286f5c52dSThierry Reding unlock:
143386f5c52dSThierry Reding 	mutex_unlock(&sor->lock);
14346b6b6042SThierry Reding }
14356b6b6042SThierry Reding 
143682f1511cSThierry Reding static int
143782f1511cSThierry Reding tegra_sor_encoder_atomic_check(struct drm_encoder *encoder,
143882f1511cSThierry Reding 			       struct drm_crtc_state *crtc_state,
143982f1511cSThierry Reding 			       struct drm_connector_state *conn_state)
144082f1511cSThierry Reding {
144182f1511cSThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
144282f1511cSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(conn_state->crtc);
144382f1511cSThierry Reding 	unsigned long pclk = crtc_state->mode.clock * 1000;
144482f1511cSThierry Reding 	struct tegra_sor *sor = to_sor(output);
144582f1511cSThierry Reding 	int err;
144682f1511cSThierry Reding 
144782f1511cSThierry Reding 	err = tegra_dc_state_setup_clock(dc, crtc_state, sor->clk_parent,
144882f1511cSThierry Reding 					 pclk, 0);
144982f1511cSThierry Reding 	if (err < 0) {
145082f1511cSThierry Reding 		dev_err(output->dev, "failed to setup CRTC state: %d\n", err);
145182f1511cSThierry Reding 		return err;
145282f1511cSThierry Reding 	}
145382f1511cSThierry Reding 
145482f1511cSThierry Reding 	return 0;
145582f1511cSThierry Reding }
145682f1511cSThierry Reding 
14576fad8f66SThierry Reding static const struct drm_encoder_helper_funcs tegra_sor_encoder_helper_funcs = {
14586fad8f66SThierry Reding 	.dpms = tegra_sor_encoder_dpms,
14596fad8f66SThierry Reding 	.prepare = tegra_sor_encoder_prepare,
14606fad8f66SThierry Reding 	.commit = tegra_sor_encoder_commit,
14616fad8f66SThierry Reding 	.mode_set = tegra_sor_encoder_mode_set,
14626fad8f66SThierry Reding 	.disable = tegra_sor_encoder_disable,
146382f1511cSThierry Reding 	.atomic_check = tegra_sor_encoder_atomic_check,
14646b6b6042SThierry Reding };
14656b6b6042SThierry Reding 
14666b6b6042SThierry Reding static int tegra_sor_init(struct host1x_client *client)
14676b6b6042SThierry Reding {
14689910f5c4SThierry Reding 	struct drm_device *drm = dev_get_drvdata(client->parent);
14696b6b6042SThierry Reding 	struct tegra_sor *sor = host1x_client_to_sor(client);
14706b6b6042SThierry Reding 	int err;
14716b6b6042SThierry Reding 
14726b6b6042SThierry Reding 	if (!sor->dpaux)
14736b6b6042SThierry Reding 		return -ENODEV;
14746b6b6042SThierry Reding 
14756b6b6042SThierry Reding 	sor->output.dev = sor->dev;
14766b6b6042SThierry Reding 
14776fad8f66SThierry Reding 	drm_connector_init(drm, &sor->output.connector,
14786fad8f66SThierry Reding 			   &tegra_sor_connector_funcs,
14796fad8f66SThierry Reding 			   DRM_MODE_CONNECTOR_eDP);
14806fad8f66SThierry Reding 	drm_connector_helper_add(&sor->output.connector,
14816fad8f66SThierry Reding 				 &tegra_sor_connector_helper_funcs);
14826fad8f66SThierry Reding 	sor->output.connector.dpms = DRM_MODE_DPMS_OFF;
14836fad8f66SThierry Reding 
14846fad8f66SThierry Reding 	drm_encoder_init(drm, &sor->output.encoder, &tegra_sor_encoder_funcs,
14856fad8f66SThierry Reding 			 DRM_MODE_ENCODER_TMDS);
14866fad8f66SThierry Reding 	drm_encoder_helper_add(&sor->output.encoder,
14876fad8f66SThierry Reding 			       &tegra_sor_encoder_helper_funcs);
14886fad8f66SThierry Reding 
14896fad8f66SThierry Reding 	drm_mode_connector_attach_encoder(&sor->output.connector,
14906fad8f66SThierry Reding 					  &sor->output.encoder);
14916fad8f66SThierry Reding 	drm_connector_register(&sor->output.connector);
14926fad8f66SThierry Reding 
1493ea130b24SThierry Reding 	err = tegra_output_init(drm, &sor->output);
1494ea130b24SThierry Reding 	if (err < 0) {
1495ea130b24SThierry Reding 		dev_err(client->dev, "failed to initialize output: %d\n", err);
1496ea130b24SThierry Reding 		return err;
1497ea130b24SThierry Reding 	}
14986fad8f66SThierry Reding 
1499ea130b24SThierry Reding 	sor->output.encoder.possible_crtcs = 0x3;
15006b6b6042SThierry Reding 
1501a82752e1SThierry Reding 	if (IS_ENABLED(CONFIG_DEBUG_FS)) {
15021b0c7b48SThierry Reding 		err = tegra_sor_debugfs_init(sor, drm->primary);
1503a82752e1SThierry Reding 		if (err < 0)
1504a82752e1SThierry Reding 			dev_err(sor->dev, "debugfs setup failed: %d\n", err);
1505a82752e1SThierry Reding 	}
1506a82752e1SThierry Reding 
15076b6b6042SThierry Reding 	if (sor->dpaux) {
15086b6b6042SThierry Reding 		err = tegra_dpaux_attach(sor->dpaux, &sor->output);
15096b6b6042SThierry Reding 		if (err < 0) {
15106b6b6042SThierry Reding 			dev_err(sor->dev, "failed to attach DP: %d\n", err);
15116b6b6042SThierry Reding 			return err;
15126b6b6042SThierry Reding 		}
15136b6b6042SThierry Reding 	}
15146b6b6042SThierry Reding 
1515*535a65dbSTomeu Vizoso 	/*
1516*535a65dbSTomeu Vizoso 	 * XXX: Remove this reset once proper hand-over from firmware to
1517*535a65dbSTomeu Vizoso 	 * kernel is possible.
1518*535a65dbSTomeu Vizoso 	 */
1519*535a65dbSTomeu Vizoso 	err = reset_control_assert(sor->rst);
1520*535a65dbSTomeu Vizoso 	if (err < 0) {
1521*535a65dbSTomeu Vizoso 		dev_err(sor->dev, "failed to assert SOR reset: %d\n", err);
1522*535a65dbSTomeu Vizoso 		return err;
1523*535a65dbSTomeu Vizoso 	}
1524*535a65dbSTomeu Vizoso 
15256fad8f66SThierry Reding 	err = clk_prepare_enable(sor->clk);
15266fad8f66SThierry Reding 	if (err < 0) {
15276fad8f66SThierry Reding 		dev_err(sor->dev, "failed to enable clock: %d\n", err);
15286fad8f66SThierry Reding 		return err;
15296fad8f66SThierry Reding 	}
15306fad8f66SThierry Reding 
1531*535a65dbSTomeu Vizoso 	usleep_range(1000, 3000);
1532*535a65dbSTomeu Vizoso 
1533*535a65dbSTomeu Vizoso 	err = reset_control_deassert(sor->rst);
1534*535a65dbSTomeu Vizoso 	if (err < 0) {
1535*535a65dbSTomeu Vizoso 		dev_err(sor->dev, "failed to deassert SOR reset: %d\n", err);
1536*535a65dbSTomeu Vizoso 		return err;
1537*535a65dbSTomeu Vizoso 	}
1538*535a65dbSTomeu Vizoso 
15396fad8f66SThierry Reding 	err = clk_prepare_enable(sor->clk_safe);
15406fad8f66SThierry Reding 	if (err < 0)
15416fad8f66SThierry Reding 		return err;
15426fad8f66SThierry Reding 
15436fad8f66SThierry Reding 	err = clk_prepare_enable(sor->clk_dp);
15446fad8f66SThierry Reding 	if (err < 0)
15456fad8f66SThierry Reding 		return err;
15466fad8f66SThierry Reding 
15476b6b6042SThierry Reding 	return 0;
15486b6b6042SThierry Reding }
15496b6b6042SThierry Reding 
15506b6b6042SThierry Reding static int tegra_sor_exit(struct host1x_client *client)
15516b6b6042SThierry Reding {
15526b6b6042SThierry Reding 	struct tegra_sor *sor = host1x_client_to_sor(client);
15536b6b6042SThierry Reding 	int err;
15546b6b6042SThierry Reding 
1555328ec69eSThierry Reding 	tegra_output_exit(&sor->output);
1556328ec69eSThierry Reding 
15576b6b6042SThierry Reding 	if (sor->dpaux) {
15586b6b6042SThierry Reding 		err = tegra_dpaux_detach(sor->dpaux);
15596b6b6042SThierry Reding 		if (err < 0) {
15606b6b6042SThierry Reding 			dev_err(sor->dev, "failed to detach DP: %d\n", err);
15616b6b6042SThierry Reding 			return err;
15626b6b6042SThierry Reding 		}
15636b6b6042SThierry Reding 	}
15646b6b6042SThierry Reding 
15656fad8f66SThierry Reding 	clk_disable_unprepare(sor->clk_safe);
15666fad8f66SThierry Reding 	clk_disable_unprepare(sor->clk_dp);
15676fad8f66SThierry Reding 	clk_disable_unprepare(sor->clk);
15686fad8f66SThierry Reding 
15694009c224SThierry Reding 	if (IS_ENABLED(CONFIG_DEBUG_FS))
15704009c224SThierry Reding 		tegra_sor_debugfs_exit(sor);
1571a82752e1SThierry Reding 
15726b6b6042SThierry Reding 	return 0;
15736b6b6042SThierry Reding }
15746b6b6042SThierry Reding 
15756b6b6042SThierry Reding static const struct host1x_client_ops sor_client_ops = {
15766b6b6042SThierry Reding 	.init = tegra_sor_init,
15776b6b6042SThierry Reding 	.exit = tegra_sor_exit,
15786b6b6042SThierry Reding };
15796b6b6042SThierry Reding 
15806b6b6042SThierry Reding static int tegra_sor_probe(struct platform_device *pdev)
15816b6b6042SThierry Reding {
15826b6b6042SThierry Reding 	struct device_node *np;
15836b6b6042SThierry Reding 	struct tegra_sor *sor;
15846b6b6042SThierry Reding 	struct resource *regs;
15856b6b6042SThierry Reding 	int err;
15866b6b6042SThierry Reding 
15876b6b6042SThierry Reding 	sor = devm_kzalloc(&pdev->dev, sizeof(*sor), GFP_KERNEL);
15886b6b6042SThierry Reding 	if (!sor)
15896b6b6042SThierry Reding 		return -ENOMEM;
15906b6b6042SThierry Reding 
15916b6b6042SThierry Reding 	sor->output.dev = sor->dev = &pdev->dev;
15926b6b6042SThierry Reding 
15936b6b6042SThierry Reding 	np = of_parse_phandle(pdev->dev.of_node, "nvidia,dpaux", 0);
15946b6b6042SThierry Reding 	if (np) {
15956b6b6042SThierry Reding 		sor->dpaux = tegra_dpaux_find_by_of_node(np);
15966b6b6042SThierry Reding 		of_node_put(np);
15976b6b6042SThierry Reding 
15986b6b6042SThierry Reding 		if (!sor->dpaux)
15996b6b6042SThierry Reding 			return -EPROBE_DEFER;
16006b6b6042SThierry Reding 	}
16016b6b6042SThierry Reding 
16026b6b6042SThierry Reding 	err = tegra_output_probe(&sor->output);
16036b6b6042SThierry Reding 	if (err < 0)
16046b6b6042SThierry Reding 		return err;
16056b6b6042SThierry Reding 
16066b6b6042SThierry Reding 	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
16076b6b6042SThierry Reding 	sor->regs = devm_ioremap_resource(&pdev->dev, regs);
16086b6b6042SThierry Reding 	if (IS_ERR(sor->regs))
16096b6b6042SThierry Reding 		return PTR_ERR(sor->regs);
16106b6b6042SThierry Reding 
16116b6b6042SThierry Reding 	sor->rst = devm_reset_control_get(&pdev->dev, "sor");
16126b6b6042SThierry Reding 	if (IS_ERR(sor->rst))
16136b6b6042SThierry Reding 		return PTR_ERR(sor->rst);
16146b6b6042SThierry Reding 
16156b6b6042SThierry Reding 	sor->clk = devm_clk_get(&pdev->dev, NULL);
16166b6b6042SThierry Reding 	if (IS_ERR(sor->clk))
16176b6b6042SThierry Reding 		return PTR_ERR(sor->clk);
16186b6b6042SThierry Reding 
16196b6b6042SThierry Reding 	sor->clk_parent = devm_clk_get(&pdev->dev, "parent");
16206b6b6042SThierry Reding 	if (IS_ERR(sor->clk_parent))
16216b6b6042SThierry Reding 		return PTR_ERR(sor->clk_parent);
16226b6b6042SThierry Reding 
16236b6b6042SThierry Reding 	sor->clk_safe = devm_clk_get(&pdev->dev, "safe");
16246b6b6042SThierry Reding 	if (IS_ERR(sor->clk_safe))
16256b6b6042SThierry Reding 		return PTR_ERR(sor->clk_safe);
16266b6b6042SThierry Reding 
16276b6b6042SThierry Reding 	sor->clk_dp = devm_clk_get(&pdev->dev, "dp");
16286b6b6042SThierry Reding 	if (IS_ERR(sor->clk_dp))
16296b6b6042SThierry Reding 		return PTR_ERR(sor->clk_dp);
16306b6b6042SThierry Reding 
16316b6b6042SThierry Reding 	INIT_LIST_HEAD(&sor->client.list);
16326b6b6042SThierry Reding 	sor->client.ops = &sor_client_ops;
16336b6b6042SThierry Reding 	sor->client.dev = &pdev->dev;
16346b6b6042SThierry Reding 
163586f5c52dSThierry Reding 	mutex_init(&sor->lock);
163686f5c52dSThierry Reding 
16376b6b6042SThierry Reding 	err = host1x_client_register(&sor->client);
16386b6b6042SThierry Reding 	if (err < 0) {
16396b6b6042SThierry Reding 		dev_err(&pdev->dev, "failed to register host1x client: %d\n",
16406b6b6042SThierry Reding 			err);
16416b6b6042SThierry Reding 		return err;
16426b6b6042SThierry Reding 	}
16436b6b6042SThierry Reding 
16446b6b6042SThierry Reding 	platform_set_drvdata(pdev, sor);
16456b6b6042SThierry Reding 
16466b6b6042SThierry Reding 	return 0;
16476b6b6042SThierry Reding }
16486b6b6042SThierry Reding 
16496b6b6042SThierry Reding static int tegra_sor_remove(struct platform_device *pdev)
16506b6b6042SThierry Reding {
16516b6b6042SThierry Reding 	struct tegra_sor *sor = platform_get_drvdata(pdev);
16526b6b6042SThierry Reding 	int err;
16536b6b6042SThierry Reding 
16546b6b6042SThierry Reding 	err = host1x_client_unregister(&sor->client);
16556b6b6042SThierry Reding 	if (err < 0) {
16566b6b6042SThierry Reding 		dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
16576b6b6042SThierry Reding 			err);
16586b6b6042SThierry Reding 		return err;
16596b6b6042SThierry Reding 	}
16606b6b6042SThierry Reding 
1661328ec69eSThierry Reding 	tegra_output_remove(&sor->output);
16626b6b6042SThierry Reding 
16636b6b6042SThierry Reding 	return 0;
16646b6b6042SThierry Reding }
16656b6b6042SThierry Reding 
16666b6b6042SThierry Reding static const struct of_device_id tegra_sor_of_match[] = {
16676b6b6042SThierry Reding 	{ .compatible = "nvidia,tegra124-sor", },
16686b6b6042SThierry Reding 	{ },
16696b6b6042SThierry Reding };
1670ef70728cSStephen Warren MODULE_DEVICE_TABLE(of, tegra_sor_of_match);
16716b6b6042SThierry Reding 
16726b6b6042SThierry Reding struct platform_driver tegra_sor_driver = {
16736b6b6042SThierry Reding 	.driver = {
16746b6b6042SThierry Reding 		.name = "tegra-sor",
16756b6b6042SThierry Reding 		.of_match_table = tegra_sor_of_match,
16766b6b6042SThierry Reding 	},
16776b6b6042SThierry Reding 	.probe = tegra_sor_probe,
16786b6b6042SThierry Reding 	.remove = tegra_sor_remove,
16796b6b6042SThierry Reding };
1680