xref: /openbmc/linux/drivers/gpu/drm/tegra/sor.c (revision 8044449556338fb27b1a03f6b1dbbdbc59e4ebfa)
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);
97a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT0);
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);
103a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS0);
1046b6b6042SThierry Reding 
105a9a9e4fdSThierry Reding 	value = SOR_LANE_POSTCURSOR_LANE3(0x00) |
106a9a9e4fdSThierry Reding 		SOR_LANE_POSTCURSOR_LANE2(0x00) |
107a9a9e4fdSThierry Reding 		SOR_LANE_POSTCURSOR_LANE1(0x00) |
108a9a9e4fdSThierry Reding 		SOR_LANE_POSTCURSOR_LANE0(0x00);
109a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_POSTCURSOR0);
1106b6b6042SThierry Reding 
1116b6b6042SThierry Reding 	/* disable LVDS mode */
1126b6b6042SThierry Reding 	tegra_sor_writel(sor, 0, SOR_LVDS);
1136b6b6042SThierry Reding 
114a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
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? */
118a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
1196b6b6042SThierry Reding 
120a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
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;
123a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
1246b6b6042SThierry Reding 
1256b6b6042SThierry Reding 	usleep_range(10, 100);
1266b6b6042SThierry Reding 
127a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
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);
130a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
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 
151a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_SPARE0);
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;
155a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_SPARE0);
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 {
192a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
193a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 1, SOR_SUPER_STATE0);
194a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
1956b6b6042SThierry Reding }
1966b6b6042SThierry Reding 
1976b6b6042SThierry Reding static void tegra_sor_update(struct tegra_sor *sor)
1986b6b6042SThierry Reding {
199a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE0);
200a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 1, SOR_STATE0);
201a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE0);
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 */
238a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
2396b6b6042SThierry Reding 	value |= SOR_SUPER_STATE_HEAD_MODE_AWAKE;
2406b6b6042SThierry Reding 	value |= SOR_SUPER_STATE_MODE_NORMAL;
241a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
2426b6b6042SThierry Reding 	tegra_sor_super_update(sor);
2436b6b6042SThierry Reding 
2446b6b6042SThierry Reding 	/* attach */
245a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
2466b6b6042SThierry Reding 	value |= SOR_SUPER_STATE_ATTACHED;
247a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
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,
388*80444495SThierry Reding 				 const 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 */
484a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
4856fad8f66SThierry Reding 	value &= ~SOR_SUPER_STATE_MODE_NORMAL;
486a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
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 */
501a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
5026fad8f66SThierry Reding 	value &= ~SOR_SUPER_STATE_HEAD_MODE_MASK;
503a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
5046fad8f66SThierry Reding 	tegra_sor_super_update(sor);
5056fad8f66SThierry Reding 
5066fad8f66SThierry Reding 	/* detach */
507a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
5086fad8f66SThierry Reding 	value &= ~SOR_SUPER_STATE_ATTACHED;
509a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
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 
555a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
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);
558a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
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 
578a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
579a9a9e4fdSThierry Reding 	value |= SOR_PLL2_PORT_POWERDOWN;
580a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
5816fad8f66SThierry Reding 
5826fad8f66SThierry Reding 	usleep_range(20, 100);
5836fad8f66SThierry Reding 
584a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL0);
585a9a9e4fdSThierry Reding 	value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
586a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL0);
5876fad8f66SThierry Reding 
588a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
589a9a9e4fdSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD;
590a9a9e4fdSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
591a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
5926fad8f66SThierry Reding 
5936fad8f66SThierry Reding 	usleep_range(20, 100);
5946fad8f66SThierry Reding 
5956fad8f66SThierry Reding 	return 0;
5966fad8f66SThierry Reding }
5976fad8f66SThierry Reding 
5986fad8f66SThierry Reding static int tegra_sor_crc_open(struct inode *inode, struct file *file)
5996fad8f66SThierry Reding {
6006fad8f66SThierry Reding 	file->private_data = inode->i_private;
6016fad8f66SThierry Reding 
6026fad8f66SThierry Reding 	return 0;
6036fad8f66SThierry Reding }
6046fad8f66SThierry Reding 
6056fad8f66SThierry Reding static int tegra_sor_crc_release(struct inode *inode, struct file *file)
6066fad8f66SThierry Reding {
6076fad8f66SThierry Reding 	return 0;
6086fad8f66SThierry Reding }
6096fad8f66SThierry Reding 
6106fad8f66SThierry Reding static int tegra_sor_crc_wait(struct tegra_sor *sor, unsigned long timeout)
6116fad8f66SThierry Reding {
6126fad8f66SThierry Reding 	u32 value;
6136fad8f66SThierry Reding 
6146fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
6156fad8f66SThierry Reding 
6166fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
617a9a9e4fdSThierry Reding 		value = tegra_sor_readl(sor, SOR_CRCA);
618a9a9e4fdSThierry Reding 		if (value & SOR_CRCA_VALID)
6196fad8f66SThierry Reding 			return 0;
6206fad8f66SThierry Reding 
6216fad8f66SThierry Reding 		usleep_range(100, 200);
6226fad8f66SThierry Reding 	}
6236fad8f66SThierry Reding 
6246fad8f66SThierry Reding 	return -ETIMEDOUT;
6256fad8f66SThierry Reding }
6266fad8f66SThierry Reding 
6276fad8f66SThierry Reding static ssize_t tegra_sor_crc_read(struct file *file, char __user *buffer,
6286fad8f66SThierry Reding 				  size_t size, loff_t *ppos)
6296fad8f66SThierry Reding {
6306fad8f66SThierry Reding 	struct tegra_sor *sor = file->private_data;
6316fad8f66SThierry Reding 	ssize_t num, err;
6326fad8f66SThierry Reding 	char buf[10];
6336fad8f66SThierry Reding 	u32 value;
6346fad8f66SThierry Reding 
6356fad8f66SThierry Reding 	mutex_lock(&sor->lock);
6366fad8f66SThierry Reding 
6376fad8f66SThierry Reding 	if (!sor->enabled) {
6386fad8f66SThierry Reding 		err = -EAGAIN;
6396fad8f66SThierry Reding 		goto unlock;
6406fad8f66SThierry Reding 	}
6416fad8f66SThierry Reding 
642a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
6436fad8f66SThierry Reding 	value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
644a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
6456fad8f66SThierry Reding 
6466fad8f66SThierry Reding 	value = tegra_sor_readl(sor, SOR_CRC_CNTRL);
6476fad8f66SThierry Reding 	value |= SOR_CRC_CNTRL_ENABLE;
6486fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_CRC_CNTRL);
6496fad8f66SThierry Reding 
6506fad8f66SThierry Reding 	value = tegra_sor_readl(sor, SOR_TEST);
6516fad8f66SThierry Reding 	value &= ~SOR_TEST_CRC_POST_SERIALIZE;
6526fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_TEST);
6536fad8f66SThierry Reding 
6546fad8f66SThierry Reding 	err = tegra_sor_crc_wait(sor, 100);
6556fad8f66SThierry Reding 	if (err < 0)
6566fad8f66SThierry Reding 		goto unlock;
6576fad8f66SThierry Reding 
658a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, SOR_CRCA_RESET, SOR_CRCA);
659a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_CRCB);
6606fad8f66SThierry Reding 
6616fad8f66SThierry Reding 	num = scnprintf(buf, sizeof(buf), "%08x\n", value);
6626fad8f66SThierry Reding 
6636fad8f66SThierry Reding 	err = simple_read_from_buffer(buffer, size, ppos, buf, num);
6646fad8f66SThierry Reding 
6656fad8f66SThierry Reding unlock:
6666fad8f66SThierry Reding 	mutex_unlock(&sor->lock);
6676fad8f66SThierry Reding 	return err;
6686fad8f66SThierry Reding }
6696fad8f66SThierry Reding 
6706fad8f66SThierry Reding static const struct file_operations tegra_sor_crc_fops = {
6716fad8f66SThierry Reding 	.owner = THIS_MODULE,
6726fad8f66SThierry Reding 	.open = tegra_sor_crc_open,
6736fad8f66SThierry Reding 	.read = tegra_sor_crc_read,
6746fad8f66SThierry Reding 	.release = tegra_sor_crc_release,
6756fad8f66SThierry Reding };
6766fad8f66SThierry Reding 
677dab16336SThierry Reding static int tegra_sor_show_regs(struct seq_file *s, void *data)
678dab16336SThierry Reding {
679dab16336SThierry Reding 	struct drm_info_node *node = s->private;
680dab16336SThierry Reding 	struct tegra_sor *sor = node->info_ent->data;
681dab16336SThierry Reding 
682dab16336SThierry Reding #define DUMP_REG(name)						\
683dab16336SThierry Reding 	seq_printf(s, "%-38s %#05x %08x\n", #name, name,	\
684dab16336SThierry Reding 		   tegra_sor_readl(sor, name))
685dab16336SThierry Reding 
686dab16336SThierry Reding 	DUMP_REG(SOR_CTXSW);
687a9a9e4fdSThierry Reding 	DUMP_REG(SOR_SUPER_STATE0);
688a9a9e4fdSThierry Reding 	DUMP_REG(SOR_SUPER_STATE1);
689a9a9e4fdSThierry Reding 	DUMP_REG(SOR_STATE0);
690a9a9e4fdSThierry Reding 	DUMP_REG(SOR_STATE1);
691a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE0(0));
692a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE0(1));
693a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE1(0));
694a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE1(1));
695a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE2(0));
696a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE2(1));
697a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE3(0));
698a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE3(1));
699a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE4(0));
700a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE4(1));
701a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE5(0));
702a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE5(1));
703dab16336SThierry Reding 	DUMP_REG(SOR_CRC_CNTRL);
704dab16336SThierry Reding 	DUMP_REG(SOR_DP_DEBUG_MVID);
705dab16336SThierry Reding 	DUMP_REG(SOR_CLK_CNTRL);
706dab16336SThierry Reding 	DUMP_REG(SOR_CAP);
707dab16336SThierry Reding 	DUMP_REG(SOR_PWR);
708dab16336SThierry Reding 	DUMP_REG(SOR_TEST);
709a9a9e4fdSThierry Reding 	DUMP_REG(SOR_PLL0);
710a9a9e4fdSThierry Reding 	DUMP_REG(SOR_PLL1);
711a9a9e4fdSThierry Reding 	DUMP_REG(SOR_PLL2);
712a9a9e4fdSThierry Reding 	DUMP_REG(SOR_PLL3);
713dab16336SThierry Reding 	DUMP_REG(SOR_CSTM);
714dab16336SThierry Reding 	DUMP_REG(SOR_LVDS);
715a9a9e4fdSThierry Reding 	DUMP_REG(SOR_CRCA);
716a9a9e4fdSThierry Reding 	DUMP_REG(SOR_CRCB);
717dab16336SThierry Reding 	DUMP_REG(SOR_BLANK);
718dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_CTL);
719dab16336SThierry Reding 	DUMP_REG(SOR_LANE_SEQ_CTL);
720dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(0));
721dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(1));
722dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(2));
723dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(3));
724dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(4));
725dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(5));
726dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(6));
727dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(7));
728dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(8));
729dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(9));
730dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(10));
731dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(11));
732dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(12));
733dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(13));
734dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(14));
735dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(15));
736dab16336SThierry Reding 	DUMP_REG(SOR_PWM_DIV);
737dab16336SThierry Reding 	DUMP_REG(SOR_PWM_CTL);
738a9a9e4fdSThierry Reding 	DUMP_REG(SOR_VCRC_A0);
739a9a9e4fdSThierry Reding 	DUMP_REG(SOR_VCRC_A1);
740a9a9e4fdSThierry Reding 	DUMP_REG(SOR_VCRC_B0);
741a9a9e4fdSThierry Reding 	DUMP_REG(SOR_VCRC_B1);
742a9a9e4fdSThierry Reding 	DUMP_REG(SOR_CCRC_A0);
743a9a9e4fdSThierry Reding 	DUMP_REG(SOR_CCRC_A1);
744a9a9e4fdSThierry Reding 	DUMP_REG(SOR_CCRC_B0);
745a9a9e4fdSThierry Reding 	DUMP_REG(SOR_CCRC_B1);
746a9a9e4fdSThierry Reding 	DUMP_REG(SOR_EDATA_A0);
747a9a9e4fdSThierry Reding 	DUMP_REG(SOR_EDATA_A1);
748a9a9e4fdSThierry Reding 	DUMP_REG(SOR_EDATA_B0);
749a9a9e4fdSThierry Reding 	DUMP_REG(SOR_EDATA_B1);
750a9a9e4fdSThierry Reding 	DUMP_REG(SOR_COUNT_A0);
751a9a9e4fdSThierry Reding 	DUMP_REG(SOR_COUNT_A1);
752a9a9e4fdSThierry Reding 	DUMP_REG(SOR_COUNT_B0);
753a9a9e4fdSThierry Reding 	DUMP_REG(SOR_COUNT_B1);
754a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DEBUG_A0);
755a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DEBUG_A1);
756a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DEBUG_B0);
757a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DEBUG_B1);
758dab16336SThierry Reding 	DUMP_REG(SOR_TRIG);
759dab16336SThierry Reding 	DUMP_REG(SOR_MSCHECK);
760dab16336SThierry Reding 	DUMP_REG(SOR_XBAR_CTRL);
761dab16336SThierry Reding 	DUMP_REG(SOR_XBAR_POL);
762a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_LINKCTL0);
763a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_LINKCTL1);
764a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE_DRIVE_CURRENT0);
765a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE_DRIVE_CURRENT1);
766a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE4_DRIVE_CURRENT0);
767a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE4_DRIVE_CURRENT1);
768a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE_PREEMPHASIS0);
769a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE_PREEMPHASIS1);
770a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE4_PREEMPHASIS0);
771a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE4_PREEMPHASIS1);
772a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE_POSTCURSOR0);
773a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE_POSTCURSOR1);
774a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_CONFIG0);
775a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_CONFIG1);
776a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_MN0);
777a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_MN1);
778a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_PADCTL0);
779a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_PADCTL1);
780a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_DEBUG0);
781a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_DEBUG1);
782a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_SPARE0);
783a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_SPARE1);
784dab16336SThierry Reding 	DUMP_REG(SOR_DP_AUDIO_CTRL);
785dab16336SThierry Reding 	DUMP_REG(SOR_DP_AUDIO_HBLANK_SYMBOLS);
786dab16336SThierry Reding 	DUMP_REG(SOR_DP_AUDIO_VBLANK_SYMBOLS);
787dab16336SThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_HEADER);
788a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK0);
789a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK1);
790a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK2);
791a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK3);
792a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK4);
793a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK5);
794a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK6);
795dab16336SThierry Reding 	DUMP_REG(SOR_DP_TPG);
796dab16336SThierry Reding 	DUMP_REG(SOR_DP_TPG_CONFIG);
797a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_LQ_CSTM0);
798a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_LQ_CSTM1);
799a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_LQ_CSTM2);
800dab16336SThierry Reding 
801dab16336SThierry Reding #undef DUMP_REG
802dab16336SThierry Reding 
803dab16336SThierry Reding 	return 0;
804dab16336SThierry Reding }
805dab16336SThierry Reding 
806dab16336SThierry Reding static const struct drm_info_list debugfs_files[] = {
807dab16336SThierry Reding 	{ "regs", tegra_sor_show_regs, 0, NULL },
808dab16336SThierry Reding };
809dab16336SThierry Reding 
8106fad8f66SThierry Reding static int tegra_sor_debugfs_init(struct tegra_sor *sor,
8116fad8f66SThierry Reding 				  struct drm_minor *minor)
8126fad8f66SThierry Reding {
8136fad8f66SThierry Reding 	struct dentry *entry;
814dab16336SThierry Reding 	unsigned int i;
8156fad8f66SThierry Reding 	int err = 0;
8166fad8f66SThierry Reding 
8176fad8f66SThierry Reding 	sor->debugfs = debugfs_create_dir("sor", minor->debugfs_root);
8186fad8f66SThierry Reding 	if (!sor->debugfs)
8196fad8f66SThierry Reding 		return -ENOMEM;
8206fad8f66SThierry Reding 
821dab16336SThierry Reding 	sor->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
822dab16336SThierry Reding 				     GFP_KERNEL);
823dab16336SThierry Reding 	if (!sor->debugfs_files) {
8246fad8f66SThierry Reding 		err = -ENOMEM;
8256fad8f66SThierry Reding 		goto remove;
8266fad8f66SThierry Reding 	}
8276fad8f66SThierry Reding 
828dab16336SThierry Reding 	for (i = 0; i < ARRAY_SIZE(debugfs_files); i++)
829dab16336SThierry Reding 		sor->debugfs_files[i].data = sor;
830dab16336SThierry Reding 
831dab16336SThierry Reding 	err = drm_debugfs_create_files(sor->debugfs_files,
832dab16336SThierry Reding 				       ARRAY_SIZE(debugfs_files),
833dab16336SThierry Reding 				       sor->debugfs, minor);
834dab16336SThierry Reding 	if (err < 0)
835dab16336SThierry Reding 		goto free;
836dab16336SThierry Reding 
837dab16336SThierry Reding 	entry = debugfs_create_file("crc", 0644, sor->debugfs, sor,
838dab16336SThierry Reding 				    &tegra_sor_crc_fops);
839dab16336SThierry Reding 	if (!entry) {
840dab16336SThierry Reding 		err = -ENOMEM;
841dab16336SThierry Reding 		goto free;
842dab16336SThierry Reding 	}
843dab16336SThierry Reding 
8443ff1f22cSThierry Reding 	sor->minor = minor;
8453ff1f22cSThierry Reding 
8466fad8f66SThierry Reding 	return err;
8476fad8f66SThierry Reding 
848dab16336SThierry Reding free:
849dab16336SThierry Reding 	kfree(sor->debugfs_files);
850dab16336SThierry Reding 	sor->debugfs_files = NULL;
8516fad8f66SThierry Reding remove:
852dab16336SThierry Reding 	debugfs_remove_recursive(sor->debugfs);
8536fad8f66SThierry Reding 	sor->debugfs = NULL;
8546fad8f66SThierry Reding 	return err;
8556fad8f66SThierry Reding }
8566fad8f66SThierry Reding 
8574009c224SThierry Reding static void tegra_sor_debugfs_exit(struct tegra_sor *sor)
8586fad8f66SThierry Reding {
859dab16336SThierry Reding 	drm_debugfs_remove_files(sor->debugfs_files, ARRAY_SIZE(debugfs_files),
860dab16336SThierry Reding 				 sor->minor);
861dab16336SThierry Reding 	sor->minor = NULL;
862dab16336SThierry Reding 
863dab16336SThierry Reding 	kfree(sor->debugfs_files);
864066d30f8SThierry Reding 	sor->debugfs_files = NULL;
865dab16336SThierry Reding 
866dab16336SThierry Reding 	debugfs_remove_recursive(sor->debugfs);
867066d30f8SThierry Reding 	sor->debugfs = NULL;
8686fad8f66SThierry Reding }
8696fad8f66SThierry Reding 
8706fad8f66SThierry Reding static void tegra_sor_connector_dpms(struct drm_connector *connector, int mode)
8716fad8f66SThierry Reding {
8726fad8f66SThierry Reding }
8736fad8f66SThierry Reding 
8746fad8f66SThierry Reding static enum drm_connector_status
8756fad8f66SThierry Reding tegra_sor_connector_detect(struct drm_connector *connector, bool force)
8766fad8f66SThierry Reding {
8776fad8f66SThierry Reding 	struct tegra_output *output = connector_to_output(connector);
8786fad8f66SThierry Reding 	struct tegra_sor *sor = to_sor(output);
8796fad8f66SThierry Reding 
8806fad8f66SThierry Reding 	if (sor->dpaux)
8816fad8f66SThierry Reding 		return tegra_dpaux_detect(sor->dpaux);
8826fad8f66SThierry Reding 
8836fad8f66SThierry Reding 	return connector_status_unknown;
8846fad8f66SThierry Reding }
8856fad8f66SThierry Reding 
8866fad8f66SThierry Reding static const struct drm_connector_funcs tegra_sor_connector_funcs = {
8876fad8f66SThierry Reding 	.dpms = tegra_sor_connector_dpms,
8889d44189fSThierry Reding 	.reset = drm_atomic_helper_connector_reset,
8896fad8f66SThierry Reding 	.detect = tegra_sor_connector_detect,
8906fad8f66SThierry Reding 	.fill_modes = drm_helper_probe_single_connector_modes,
8916fad8f66SThierry Reding 	.destroy = tegra_output_connector_destroy,
8929d44189fSThierry Reding 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
8934aa3df71SThierry Reding 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
8946fad8f66SThierry Reding };
8956fad8f66SThierry Reding 
8966fad8f66SThierry Reding static int tegra_sor_connector_get_modes(struct drm_connector *connector)
8976fad8f66SThierry Reding {
8986fad8f66SThierry Reding 	struct tegra_output *output = connector_to_output(connector);
8996fad8f66SThierry Reding 	struct tegra_sor *sor = to_sor(output);
9006fad8f66SThierry Reding 	int err;
9016fad8f66SThierry Reding 
9026fad8f66SThierry Reding 	if (sor->dpaux)
9036fad8f66SThierry Reding 		tegra_dpaux_enable(sor->dpaux);
9046fad8f66SThierry Reding 
9056fad8f66SThierry Reding 	err = tegra_output_connector_get_modes(connector);
9066fad8f66SThierry Reding 
9076fad8f66SThierry Reding 	if (sor->dpaux)
9086fad8f66SThierry Reding 		tegra_dpaux_disable(sor->dpaux);
9096fad8f66SThierry Reding 
9106fad8f66SThierry Reding 	return err;
9116fad8f66SThierry Reding }
9126fad8f66SThierry Reding 
9136fad8f66SThierry Reding static enum drm_mode_status
9146fad8f66SThierry Reding tegra_sor_connector_mode_valid(struct drm_connector *connector,
9156fad8f66SThierry Reding 			       struct drm_display_mode *mode)
9166fad8f66SThierry Reding {
9176fad8f66SThierry Reding 	return MODE_OK;
9186fad8f66SThierry Reding }
9196fad8f66SThierry Reding 
9206fad8f66SThierry Reding static const struct drm_connector_helper_funcs tegra_sor_connector_helper_funcs = {
9216fad8f66SThierry Reding 	.get_modes = tegra_sor_connector_get_modes,
9226fad8f66SThierry Reding 	.mode_valid = tegra_sor_connector_mode_valid,
9236fad8f66SThierry Reding 	.best_encoder = tegra_output_connector_best_encoder,
9246fad8f66SThierry Reding };
9256fad8f66SThierry Reding 
9266fad8f66SThierry Reding static const struct drm_encoder_funcs tegra_sor_encoder_funcs = {
9276fad8f66SThierry Reding 	.destroy = tegra_output_encoder_destroy,
9286fad8f66SThierry Reding };
9296fad8f66SThierry Reding 
9306fad8f66SThierry Reding static void tegra_sor_encoder_dpms(struct drm_encoder *encoder, int mode)
9316fad8f66SThierry Reding {
9326fad8f66SThierry Reding }
9336fad8f66SThierry Reding 
9346fad8f66SThierry Reding static void tegra_sor_encoder_prepare(struct drm_encoder *encoder)
9356fad8f66SThierry Reding {
9366fad8f66SThierry Reding }
9376fad8f66SThierry Reding 
9386fad8f66SThierry Reding static void tegra_sor_encoder_commit(struct drm_encoder *encoder)
9396fad8f66SThierry Reding {
9406fad8f66SThierry Reding }
9416fad8f66SThierry Reding 
9426fad8f66SThierry Reding static void tegra_sor_encoder_mode_set(struct drm_encoder *encoder,
9436fad8f66SThierry Reding 				       struct drm_display_mode *mode,
9446fad8f66SThierry Reding 				       struct drm_display_mode *adjusted)
9456fad8f66SThierry Reding {
9466fad8f66SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
9476fad8f66SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
9486b6b6042SThierry Reding 	unsigned int vbe, vse, hbe, hse, vbs, hbs, i;
9496b6b6042SThierry Reding 	struct tegra_sor *sor = to_sor(output);
95034fa183bSThierry Reding 	struct tegra_sor_config config;
95134fa183bSThierry Reding 	struct drm_dp_link link;
95234fa183bSThierry Reding 	struct drm_dp_aux *aux;
95386f5c52dSThierry Reding 	int err = 0;
95428fe2076SThierry Reding 	u32 value;
95586f5c52dSThierry Reding 
95686f5c52dSThierry Reding 	mutex_lock(&sor->lock);
9576b6b6042SThierry Reding 
9586b6b6042SThierry Reding 	if (sor->enabled)
95986f5c52dSThierry Reding 		goto unlock;
9606b6b6042SThierry Reding 
9616b6b6042SThierry Reding 	err = clk_prepare_enable(sor->clk);
9626b6b6042SThierry Reding 	if (err < 0)
96386f5c52dSThierry Reding 		goto unlock;
9646b6b6042SThierry Reding 
9656b6b6042SThierry Reding 	reset_control_deassert(sor->rst);
9666b6b6042SThierry Reding 
9676fad8f66SThierry Reding 	if (output->panel)
9686fad8f66SThierry Reding 		drm_panel_prepare(output->panel);
9696fad8f66SThierry Reding 
97034fa183bSThierry Reding 	/* FIXME: properly convert to struct drm_dp_aux */
97134fa183bSThierry Reding 	aux = (struct drm_dp_aux *)sor->dpaux;
97234fa183bSThierry Reding 
9736b6b6042SThierry Reding 	if (sor->dpaux) {
9746b6b6042SThierry Reding 		err = tegra_dpaux_enable(sor->dpaux);
9756b6b6042SThierry Reding 		if (err < 0)
9766b6b6042SThierry Reding 			dev_err(sor->dev, "failed to enable DP: %d\n", err);
97734fa183bSThierry Reding 
97834fa183bSThierry Reding 		err = drm_dp_link_probe(aux, &link);
97934fa183bSThierry Reding 		if (err < 0) {
98034fa183bSThierry Reding 			dev_err(sor->dev, "failed to probe eDP link: %d\n",
98134fa183bSThierry Reding 				err);
9822263c460SDan Carpenter 			goto unlock;
98334fa183bSThierry Reding 		}
9846b6b6042SThierry Reding 	}
9856b6b6042SThierry Reding 
9866b6b6042SThierry Reding 	err = clk_set_parent(sor->clk, sor->clk_safe);
9876b6b6042SThierry Reding 	if (err < 0)
9886b6b6042SThierry Reding 		dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
9896b6b6042SThierry Reding 
99034fa183bSThierry Reding 	memset(&config, 0, sizeof(config));
991054b1bd1SStéphane Marchesin 	config.bits_per_pixel = output->connector.display_info.bpc * 3;
99234fa183bSThierry Reding 
99334fa183bSThierry Reding 	err = tegra_sor_calc_config(sor, mode, &config, &link);
99434fa183bSThierry Reding 	if (err < 0)
99534fa183bSThierry Reding 		dev_err(sor->dev, "failed to compute link configuration: %d\n",
99634fa183bSThierry Reding 			err);
99734fa183bSThierry Reding 
9986b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
9996b6b6042SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
10006b6b6042SThierry Reding 	value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
10016b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
10026b6b6042SThierry Reding 
1003a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1004a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
1005a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
10066b6b6042SThierry Reding 	usleep_range(20, 100);
10076b6b6042SThierry Reding 
1008a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL3);
1009a9a9e4fdSThierry Reding 	value |= SOR_PLL3_PLL_VDD_MODE_3V3;
1010a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL3);
10116b6b6042SThierry Reding 
1012a9a9e4fdSThierry Reding 	value = SOR_PLL0_ICHPMP(0xf) | SOR_PLL0_VCOCAP_RST |
1013a9a9e4fdSThierry Reding 		SOR_PLL0_PLLREG_LEVEL_V45 | SOR_PLL0_RESISTOR_EXT;
1014a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL0);
10156b6b6042SThierry Reding 
1016a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1017a9a9e4fdSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD;
1018a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1019a9a9e4fdSThierry Reding 	value |= SOR_PLL2_LVDS_ENABLE;
1020a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
10216b6b6042SThierry Reding 
1022a9a9e4fdSThierry Reding 	value = SOR_PLL1_TERM_COMPOUT | SOR_PLL1_TMDS_TERM;
1023a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL1);
10246b6b6042SThierry Reding 
10256b6b6042SThierry Reding 	while (true) {
1026a9a9e4fdSThierry Reding 		value = tegra_sor_readl(sor, SOR_PLL2);
1027a9a9e4fdSThierry Reding 		if ((value & SOR_PLL2_SEQ_PLLCAPPD_ENFORCE) == 0)
10286b6b6042SThierry Reding 			break;
10296b6b6042SThierry Reding 
10306b6b6042SThierry Reding 		usleep_range(250, 1000);
10316b6b6042SThierry Reding 	}
10326b6b6042SThierry Reding 
1033a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1034a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
1035a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_PORT_POWERDOWN;
1036a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
10376b6b6042SThierry Reding 
10386b6b6042SThierry Reding 	/*
10396b6b6042SThierry Reding 	 * power up
10406b6b6042SThierry Reding 	 */
10416b6b6042SThierry Reding 
10426b6b6042SThierry Reding 	/* set safe link bandwidth (1.62 Gbps) */
10436b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
10446b6b6042SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
10456b6b6042SThierry Reding 	value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G1_62;
10466b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
10476b6b6042SThierry Reding 
10486b6b6042SThierry Reding 	/* step 1 */
1049a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1050a9a9e4fdSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE | SOR_PLL2_PORT_POWERDOWN |
1051a9a9e4fdSThierry Reding 		 SOR_PLL2_BANDGAP_POWERDOWN;
1052a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
10536b6b6042SThierry Reding 
1054a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL0);
1055a9a9e4fdSThierry Reding 	value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
1056a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL0);
10576b6b6042SThierry Reding 
1058a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
10596b6b6042SThierry Reding 	value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
1060a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
10616b6b6042SThierry Reding 
10626b6b6042SThierry Reding 	/* step 2 */
10636b6b6042SThierry Reding 	err = tegra_io_rail_power_on(TEGRA_IO_RAIL_LVDS);
10646b6b6042SThierry Reding 	if (err < 0) {
10656b6b6042SThierry Reding 		dev_err(sor->dev, "failed to power on I/O rail: %d\n", err);
106686f5c52dSThierry Reding 		goto unlock;
10676b6b6042SThierry Reding 	}
10686b6b6042SThierry Reding 
10696b6b6042SThierry Reding 	usleep_range(5, 100);
10706b6b6042SThierry Reding 
10716b6b6042SThierry Reding 	/* step 3 */
1072a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1073a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
1074a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
10756b6b6042SThierry Reding 
10766b6b6042SThierry Reding 	usleep_range(20, 100);
10776b6b6042SThierry Reding 
10786b6b6042SThierry Reding 	/* step 4 */
1079a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL0);
1080a9a9e4fdSThierry Reding 	value &= ~SOR_PLL0_VCOPD;
1081a9a9e4fdSThierry Reding 	value &= ~SOR_PLL0_PWR;
1082a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL0);
10836b6b6042SThierry Reding 
1084a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1085a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1086a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
10876b6b6042SThierry Reding 
10886b6b6042SThierry Reding 	usleep_range(200, 1000);
10896b6b6042SThierry Reding 
10906b6b6042SThierry Reding 	/* step 5 */
1091a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1092a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_PORT_POWERDOWN;
1093a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
10946b6b6042SThierry Reding 
10956b6b6042SThierry Reding 	/* switch to DP clock */
10966b6b6042SThierry Reding 	err = clk_set_parent(sor->clk, sor->clk_dp);
10976b6b6042SThierry Reding 	if (err < 0)
10986b6b6042SThierry Reding 		dev_err(sor->dev, "failed to set DP parent clock: %d\n", err);
10996b6b6042SThierry Reding 
1100899451b7SThierry Reding 	/* power DP lanes */
1101a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
1102899451b7SThierry Reding 
1103899451b7SThierry Reding 	if (link.num_lanes <= 2)
1104899451b7SThierry Reding 		value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2);
1105899451b7SThierry Reding 	else
1106899451b7SThierry Reding 		value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2;
1107899451b7SThierry Reding 
1108899451b7SThierry Reding 	if (link.num_lanes <= 1)
1109899451b7SThierry Reding 		value &= ~SOR_DP_PADCTL_PD_TXD_1;
1110899451b7SThierry Reding 	else
1111899451b7SThierry Reding 		value |= SOR_DP_PADCTL_PD_TXD_1;
1112899451b7SThierry Reding 
1113899451b7SThierry Reding 	if (link.num_lanes == 0)
1114899451b7SThierry Reding 		value &= ~SOR_DP_PADCTL_PD_TXD_0;
1115899451b7SThierry Reding 	else
1116899451b7SThierry Reding 		value |= SOR_DP_PADCTL_PD_TXD_0;
1117899451b7SThierry Reding 
1118a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
11196b6b6042SThierry Reding 
1120a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
11216b6b6042SThierry Reding 	value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
11220c90a184SThierry Reding 	value |= SOR_DP_LINKCTL_LANE_COUNT(link.num_lanes);
1123a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
11246b6b6042SThierry Reding 
11256b6b6042SThierry Reding 	/* start lane sequencer */
11266b6b6042SThierry Reding 	value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
11276b6b6042SThierry Reding 		SOR_LANE_SEQ_CTL_POWER_STATE_UP;
11286b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
11296b6b6042SThierry Reding 
11306b6b6042SThierry Reding 	while (true) {
11316b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
11326b6b6042SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
11336b6b6042SThierry Reding 			break;
11346b6b6042SThierry Reding 
11356b6b6042SThierry Reding 		usleep_range(250, 1000);
11366b6b6042SThierry Reding 	}
11376b6b6042SThierry Reding 
1138a4263fedSThierry Reding 	/* set link bandwidth */
11396b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
11406b6b6042SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
1141a4263fedSThierry Reding 	value |= drm_dp_link_rate_to_bw_code(link.rate) << 2;
11426b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
11436b6b6042SThierry Reding 
11446b6b6042SThierry Reding 	/* set linkctl */
1145a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
11466b6b6042SThierry Reding 	value |= SOR_DP_LINKCTL_ENABLE;
11476b6b6042SThierry Reding 
11486b6b6042SThierry Reding 	value &= ~SOR_DP_LINKCTL_TU_SIZE_MASK;
114934fa183bSThierry Reding 	value |= SOR_DP_LINKCTL_TU_SIZE(config.tu_size);
11506b6b6042SThierry Reding 
11516b6b6042SThierry Reding 	value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
1152a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
11536b6b6042SThierry Reding 
11546b6b6042SThierry Reding 	for (i = 0, value = 0; i < 4; i++) {
11556b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
11566b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_GALIOS |
11576b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_NONE;
11586b6b6042SThierry Reding 		value = (value << 8) | lane;
11596b6b6042SThierry Reding 	}
11606b6b6042SThierry Reding 
11616b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
11626b6b6042SThierry Reding 
1163a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_CONFIG0);
11646b6b6042SThierry Reding 	value &= ~SOR_DP_CONFIG_WATERMARK_MASK;
116534fa183bSThierry Reding 	value |= SOR_DP_CONFIG_WATERMARK(config.watermark);
11666b6b6042SThierry Reding 
11676b6b6042SThierry Reding 	value &= ~SOR_DP_CONFIG_ACTIVE_SYM_COUNT_MASK;
116834fa183bSThierry Reding 	value |= SOR_DP_CONFIG_ACTIVE_SYM_COUNT(config.active_count);
11696b6b6042SThierry Reding 
11706b6b6042SThierry Reding 	value &= ~SOR_DP_CONFIG_ACTIVE_SYM_FRAC_MASK;
117134fa183bSThierry Reding 	value |= SOR_DP_CONFIG_ACTIVE_SYM_FRAC(config.active_frac);
11726b6b6042SThierry Reding 
117334fa183bSThierry Reding 	if (config.active_polarity)
117434fa183bSThierry Reding 		value |= SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
117534fa183bSThierry Reding 	else
117634fa183bSThierry Reding 		value &= ~SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
11776b6b6042SThierry Reding 
11786b6b6042SThierry Reding 	value |= SOR_DP_CONFIG_ACTIVE_SYM_ENABLE;
11791f64ae7cSThierry Reding 	value |= SOR_DP_CONFIG_DISPARITY_NEGATIVE;
1180a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_CONFIG0);
11816b6b6042SThierry Reding 
11826b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_AUDIO_HBLANK_SYMBOLS);
11836b6b6042SThierry Reding 	value &= ~SOR_DP_AUDIO_HBLANK_SYMBOLS_MASK;
11847890b576SThierry Reding 	value |= config.hblank_symbols & 0xffff;
11856b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_AUDIO_HBLANK_SYMBOLS);
11866b6b6042SThierry Reding 
11876b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_AUDIO_VBLANK_SYMBOLS);
11886b6b6042SThierry Reding 	value &= ~SOR_DP_AUDIO_VBLANK_SYMBOLS_MASK;
11897890b576SThierry Reding 	value |= config.vblank_symbols & 0xffff;
11906b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_AUDIO_VBLANK_SYMBOLS);
11916b6b6042SThierry Reding 
11926b6b6042SThierry Reding 	/* enable pad calibration logic */
1193a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
11946b6b6042SThierry Reding 	value |= SOR_DP_PADCTL_PAD_CAL_PD;
1195a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
11966b6b6042SThierry Reding 
11976b6b6042SThierry Reding 	if (sor->dpaux) {
11986b6b6042SThierry Reding 		u8 rate, lanes;
11996b6b6042SThierry Reding 
12006b6b6042SThierry Reding 		err = drm_dp_link_probe(aux, &link);
12016b6b6042SThierry Reding 		if (err < 0) {
12026b6b6042SThierry Reding 			dev_err(sor->dev, "failed to probe eDP link: %d\n",
12036b6b6042SThierry Reding 				err);
120486f5c52dSThierry Reding 			goto unlock;
12056b6b6042SThierry Reding 		}
12066b6b6042SThierry Reding 
12076b6b6042SThierry Reding 		err = drm_dp_link_power_up(aux, &link);
12086b6b6042SThierry Reding 		if (err < 0) {
12096b6b6042SThierry Reding 			dev_err(sor->dev, "failed to power up eDP link: %d\n",
12106b6b6042SThierry Reding 				err);
121186f5c52dSThierry Reding 			goto unlock;
12126b6b6042SThierry Reding 		}
12136b6b6042SThierry Reding 
12146b6b6042SThierry Reding 		err = drm_dp_link_configure(aux, &link);
12156b6b6042SThierry Reding 		if (err < 0) {
12166b6b6042SThierry Reding 			dev_err(sor->dev, "failed to configure eDP link: %d\n",
12176b6b6042SThierry Reding 				err);
121886f5c52dSThierry Reding 			goto unlock;
12196b6b6042SThierry Reding 		}
12206b6b6042SThierry Reding 
12216b6b6042SThierry Reding 		rate = drm_dp_link_rate_to_bw_code(link.rate);
12226b6b6042SThierry Reding 		lanes = link.num_lanes;
12236b6b6042SThierry Reding 
12246b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
12256b6b6042SThierry Reding 		value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
12266b6b6042SThierry Reding 		value |= SOR_CLK_CNTRL_DP_LINK_SPEED(rate);
12276b6b6042SThierry Reding 		tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
12286b6b6042SThierry Reding 
1229a9a9e4fdSThierry Reding 		value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
12306b6b6042SThierry Reding 		value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
12316b6b6042SThierry Reding 		value |= SOR_DP_LINKCTL_LANE_COUNT(lanes);
12326b6b6042SThierry Reding 
12336b6b6042SThierry Reding 		if (link.capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
12346b6b6042SThierry Reding 			value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
12356b6b6042SThierry Reding 
1236a9a9e4fdSThierry Reding 		tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
12376b6b6042SThierry Reding 
12386b6b6042SThierry Reding 		/* disable training pattern generator */
12396b6b6042SThierry Reding 
12406b6b6042SThierry Reding 		for (i = 0; i < link.num_lanes; i++) {
12416b6b6042SThierry Reding 			unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
12426b6b6042SThierry Reding 					     SOR_DP_TPG_SCRAMBLER_GALIOS |
12436b6b6042SThierry Reding 					     SOR_DP_TPG_PATTERN_NONE;
12446b6b6042SThierry Reding 			value = (value << 8) | lane;
12456b6b6042SThierry Reding 		}
12466b6b6042SThierry Reding 
12476b6b6042SThierry Reding 		tegra_sor_writel(sor, value, SOR_DP_TPG);
12486b6b6042SThierry Reding 
12496b6b6042SThierry Reding 		err = tegra_sor_dp_train_fast(sor, &link);
12506b6b6042SThierry Reding 		if (err < 0) {
12516b6b6042SThierry Reding 			dev_err(sor->dev, "DP fast link training failed: %d\n",
12526b6b6042SThierry Reding 				err);
125386f5c52dSThierry Reding 			goto unlock;
12546b6b6042SThierry Reding 		}
12556b6b6042SThierry Reding 
12566b6b6042SThierry Reding 		dev_dbg(sor->dev, "fast link training succeeded\n");
12576b6b6042SThierry Reding 	}
12586b6b6042SThierry Reding 
12596b6b6042SThierry Reding 	err = tegra_sor_power_up(sor, 250);
12606b6b6042SThierry Reding 	if (err < 0) {
12616b6b6042SThierry Reding 		dev_err(sor->dev, "failed to power up SOR: %d\n", err);
126286f5c52dSThierry Reding 		goto unlock;
12636b6b6042SThierry Reding 	}
12646b6b6042SThierry Reding 
12656b6b6042SThierry Reding 	/*
12666b6b6042SThierry Reding 	 * configure panel (24bpp, vsync-, hsync-, DP-A protocol, complete
12676b6b6042SThierry Reding 	 * raster, associate with display controller)
12686b6b6042SThierry Reding 	 */
12693f4f3b5fSThierry Reding 	value = SOR_STATE_ASY_PROTOCOL_DP_A |
12706b6b6042SThierry Reding 		SOR_STATE_ASY_CRC_MODE_COMPLETE |
12716b6b6042SThierry Reding 		SOR_STATE_ASY_OWNER(dc->pipe + 1);
127234fa183bSThierry Reding 
12733f4f3b5fSThierry Reding 	if (mode->flags & DRM_MODE_FLAG_PHSYNC)
12743f4f3b5fSThierry Reding 		value &= ~SOR_STATE_ASY_HSYNCPOL;
12753f4f3b5fSThierry Reding 
12763f4f3b5fSThierry Reding 	if (mode->flags & DRM_MODE_FLAG_NHSYNC)
12773f4f3b5fSThierry Reding 		value |= SOR_STATE_ASY_HSYNCPOL;
12783f4f3b5fSThierry Reding 
12793f4f3b5fSThierry Reding 	if (mode->flags & DRM_MODE_FLAG_PVSYNC)
12803f4f3b5fSThierry Reding 		value &= ~SOR_STATE_ASY_VSYNCPOL;
12813f4f3b5fSThierry Reding 
12823f4f3b5fSThierry Reding 	if (mode->flags & DRM_MODE_FLAG_NVSYNC)
12833f4f3b5fSThierry Reding 		value |= SOR_STATE_ASY_VSYNCPOL;
12843f4f3b5fSThierry Reding 
128534fa183bSThierry Reding 	switch (config.bits_per_pixel) {
128634fa183bSThierry Reding 	case 24:
128734fa183bSThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
128834fa183bSThierry Reding 		break;
128934fa183bSThierry Reding 
129034fa183bSThierry Reding 	case 18:
129134fa183bSThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_18_444;
129234fa183bSThierry Reding 		break;
129334fa183bSThierry Reding 
129434fa183bSThierry Reding 	default:
129534fa183bSThierry Reding 		BUG();
129634fa183bSThierry Reding 		break;
129734fa183bSThierry Reding 	}
129834fa183bSThierry Reding 
1299a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
13006b6b6042SThierry Reding 
13016b6b6042SThierry Reding 	/*
13026b6b6042SThierry Reding 	 * TODO: The video timing programming below doesn't seem to match the
13036b6b6042SThierry Reding 	 * register definitions.
13046b6b6042SThierry Reding 	 */
13056b6b6042SThierry Reding 
13066b6b6042SThierry Reding 	value = ((mode->vtotal & 0x7fff) << 16) | (mode->htotal & 0x7fff);
1307a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_HEAD_STATE1(0));
13086b6b6042SThierry Reding 
13096b6b6042SThierry Reding 	vse = mode->vsync_end - mode->vsync_start - 1;
13106b6b6042SThierry Reding 	hse = mode->hsync_end - mode->hsync_start - 1;
13116b6b6042SThierry Reding 
13126b6b6042SThierry Reding 	value = ((vse & 0x7fff) << 16) | (hse & 0x7fff);
1313a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_HEAD_STATE2(0));
13146b6b6042SThierry Reding 
13156b6b6042SThierry Reding 	vbe = vse + (mode->vsync_start - mode->vdisplay);
13166b6b6042SThierry Reding 	hbe = hse + (mode->hsync_start - mode->hdisplay);
13176b6b6042SThierry Reding 
13186b6b6042SThierry Reding 	value = ((vbe & 0x7fff) << 16) | (hbe & 0x7fff);
1319a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_HEAD_STATE3(0));
13206b6b6042SThierry Reding 
13216b6b6042SThierry Reding 	vbs = vbe + mode->vdisplay;
13226b6b6042SThierry Reding 	hbs = hbe + mode->hdisplay;
13236b6b6042SThierry Reding 
13246b6b6042SThierry Reding 	value = ((vbs & 0x7fff) << 16) | (hbs & 0x7fff);
1325a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_HEAD_STATE4(0));
13266b6b6042SThierry Reding 
13276b6b6042SThierry Reding 	/* CSTM (LVDS, link A/B, upper) */
1328143b1df2SStéphane Marchesin 	value = SOR_CSTM_LVDS | SOR_CSTM_LINK_ACT_A | SOR_CSTM_LINK_ACT_B |
13296b6b6042SThierry Reding 		SOR_CSTM_UPPER;
13306b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CSTM);
13316b6b6042SThierry Reding 
13326b6b6042SThierry Reding 	/* PWM setup */
13336b6b6042SThierry Reding 	err = tegra_sor_setup_pwm(sor, 250);
13346b6b6042SThierry Reding 	if (err < 0) {
13356b6b6042SThierry Reding 		dev_err(sor->dev, "failed to setup PWM: %d\n", err);
133686f5c52dSThierry Reding 		goto unlock;
13376b6b6042SThierry Reding 	}
13386b6b6042SThierry Reding 
1339666cb873SThierry Reding 	tegra_sor_update(sor);
1340666cb873SThierry Reding 
13416b6b6042SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
13426b6b6042SThierry Reding 	value |= SOR_ENABLE;
13436b6b6042SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
13446b6b6042SThierry Reding 
1345666cb873SThierry Reding 	tegra_dc_commit(dc);
13466b6b6042SThierry Reding 
13476b6b6042SThierry Reding 	err = tegra_sor_attach(sor);
13486b6b6042SThierry Reding 	if (err < 0) {
13496b6b6042SThierry Reding 		dev_err(sor->dev, "failed to attach SOR: %d\n", err);
135086f5c52dSThierry Reding 		goto unlock;
13516b6b6042SThierry Reding 	}
13526b6b6042SThierry Reding 
13536b6b6042SThierry Reding 	err = tegra_sor_wakeup(sor);
13546b6b6042SThierry Reding 	if (err < 0) {
13556b6b6042SThierry Reding 		dev_err(sor->dev, "failed to enable DC: %d\n", err);
135686f5c52dSThierry Reding 		goto unlock;
13576b6b6042SThierry Reding 	}
13586b6b6042SThierry Reding 
13596fad8f66SThierry Reding 	if (output->panel)
13606fad8f66SThierry Reding 		drm_panel_enable(output->panel);
13616fad8f66SThierry Reding 
13626b6b6042SThierry Reding 	sor->enabled = true;
13636b6b6042SThierry Reding 
136486f5c52dSThierry Reding unlock:
136586f5c52dSThierry Reding 	mutex_unlock(&sor->lock);
13666b6b6042SThierry Reding }
13676b6b6042SThierry Reding 
13686fad8f66SThierry Reding static void tegra_sor_encoder_disable(struct drm_encoder *encoder)
13696b6b6042SThierry Reding {
13706fad8f66SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
13716fad8f66SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
13726b6b6042SThierry Reding 	struct tegra_sor *sor = to_sor(output);
13736fad8f66SThierry Reding 	u32 value;
13746fad8f66SThierry Reding 	int err;
137586f5c52dSThierry Reding 
137686f5c52dSThierry Reding 	mutex_lock(&sor->lock);
13776b6b6042SThierry Reding 
13786b6b6042SThierry Reding 	if (!sor->enabled)
137986f5c52dSThierry Reding 		goto unlock;
13806b6b6042SThierry Reding 
13816fad8f66SThierry Reding 	if (output->panel)
13826fad8f66SThierry Reding 		drm_panel_disable(output->panel);
13836fad8f66SThierry Reding 
13846b6b6042SThierry Reding 	err = tegra_sor_detach(sor);
13856b6b6042SThierry Reding 	if (err < 0) {
13866b6b6042SThierry Reding 		dev_err(sor->dev, "failed to detach SOR: %d\n", err);
138786f5c52dSThierry Reding 		goto unlock;
13886b6b6042SThierry Reding 	}
13896b6b6042SThierry Reding 
1390a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE1);
13916b6b6042SThierry Reding 	tegra_sor_update(sor);
13926b6b6042SThierry Reding 
13936b6b6042SThierry Reding 	/*
13946b6b6042SThierry Reding 	 * The following accesses registers of the display controller, so make
13956b6b6042SThierry Reding 	 * sure it's only executed when the output is attached to one.
13966b6b6042SThierry Reding 	 */
13976b6b6042SThierry Reding 	if (dc) {
13986b6b6042SThierry Reding 		value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
13996b6b6042SThierry Reding 		value &= ~SOR_ENABLE;
14006b6b6042SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
14016b6b6042SThierry Reding 
140262b9e063SThierry Reding 		tegra_dc_commit(dc);
14036b6b6042SThierry Reding 	}
14046b6b6042SThierry Reding 
14056b6b6042SThierry Reding 	err = tegra_sor_power_down(sor);
14066b6b6042SThierry Reding 	if (err < 0) {
14076b6b6042SThierry Reding 		dev_err(sor->dev, "failed to power down SOR: %d\n", err);
140886f5c52dSThierry Reding 		goto unlock;
14096b6b6042SThierry Reding 	}
14106b6b6042SThierry Reding 
14116b6b6042SThierry Reding 	if (sor->dpaux) {
14126b6b6042SThierry Reding 		err = tegra_dpaux_disable(sor->dpaux);
14136b6b6042SThierry Reding 		if (err < 0) {
14146b6b6042SThierry Reding 			dev_err(sor->dev, "failed to disable DP: %d\n", err);
141586f5c52dSThierry Reding 			goto unlock;
14166b6b6042SThierry Reding 		}
14176b6b6042SThierry Reding 	}
14186b6b6042SThierry Reding 
14196b6b6042SThierry Reding 	err = tegra_io_rail_power_off(TEGRA_IO_RAIL_LVDS);
14206b6b6042SThierry Reding 	if (err < 0) {
14216b6b6042SThierry Reding 		dev_err(sor->dev, "failed to power off I/O rail: %d\n", err);
142286f5c52dSThierry Reding 		goto unlock;
14236b6b6042SThierry Reding 	}
14246b6b6042SThierry Reding 
14256fad8f66SThierry Reding 	if (output->panel)
14266fad8f66SThierry Reding 		drm_panel_unprepare(output->panel);
14276fad8f66SThierry Reding 
14286b6b6042SThierry Reding 	clk_disable_unprepare(sor->clk);
14296fad8f66SThierry Reding 	reset_control_assert(sor->rst);
14306b6b6042SThierry Reding 
14316b6b6042SThierry Reding 	sor->enabled = false;
14326b6b6042SThierry Reding 
143386f5c52dSThierry Reding unlock:
143486f5c52dSThierry Reding 	mutex_unlock(&sor->lock);
14356b6b6042SThierry Reding }
14366b6b6042SThierry Reding 
143782f1511cSThierry Reding static int
143882f1511cSThierry Reding tegra_sor_encoder_atomic_check(struct drm_encoder *encoder,
143982f1511cSThierry Reding 			       struct drm_crtc_state *crtc_state,
144082f1511cSThierry Reding 			       struct drm_connector_state *conn_state)
144182f1511cSThierry Reding {
144282f1511cSThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
144382f1511cSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(conn_state->crtc);
144482f1511cSThierry Reding 	unsigned long pclk = crtc_state->mode.clock * 1000;
144582f1511cSThierry Reding 	struct tegra_sor *sor = to_sor(output);
144682f1511cSThierry Reding 	int err;
144782f1511cSThierry Reding 
144882f1511cSThierry Reding 	err = tegra_dc_state_setup_clock(dc, crtc_state, sor->clk_parent,
144982f1511cSThierry Reding 					 pclk, 0);
145082f1511cSThierry Reding 	if (err < 0) {
145182f1511cSThierry Reding 		dev_err(output->dev, "failed to setup CRTC state: %d\n", err);
145282f1511cSThierry Reding 		return err;
145382f1511cSThierry Reding 	}
145482f1511cSThierry Reding 
145582f1511cSThierry Reding 	return 0;
145682f1511cSThierry Reding }
145782f1511cSThierry Reding 
14586fad8f66SThierry Reding static const struct drm_encoder_helper_funcs tegra_sor_encoder_helper_funcs = {
14596fad8f66SThierry Reding 	.dpms = tegra_sor_encoder_dpms,
14606fad8f66SThierry Reding 	.prepare = tegra_sor_encoder_prepare,
14616fad8f66SThierry Reding 	.commit = tegra_sor_encoder_commit,
14626fad8f66SThierry Reding 	.mode_set = tegra_sor_encoder_mode_set,
14636fad8f66SThierry Reding 	.disable = tegra_sor_encoder_disable,
146482f1511cSThierry Reding 	.atomic_check = tegra_sor_encoder_atomic_check,
14656b6b6042SThierry Reding };
14666b6b6042SThierry Reding 
14676b6b6042SThierry Reding static int tegra_sor_init(struct host1x_client *client)
14686b6b6042SThierry Reding {
14699910f5c4SThierry Reding 	struct drm_device *drm = dev_get_drvdata(client->parent);
14706b6b6042SThierry Reding 	struct tegra_sor *sor = host1x_client_to_sor(client);
14716b6b6042SThierry Reding 	int err;
14726b6b6042SThierry Reding 
14736b6b6042SThierry Reding 	if (!sor->dpaux)
14746b6b6042SThierry Reding 		return -ENODEV;
14756b6b6042SThierry Reding 
14766b6b6042SThierry Reding 	sor->output.dev = sor->dev;
14776b6b6042SThierry Reding 
14786fad8f66SThierry Reding 	drm_connector_init(drm, &sor->output.connector,
14796fad8f66SThierry Reding 			   &tegra_sor_connector_funcs,
14806fad8f66SThierry Reding 			   DRM_MODE_CONNECTOR_eDP);
14816fad8f66SThierry Reding 	drm_connector_helper_add(&sor->output.connector,
14826fad8f66SThierry Reding 				 &tegra_sor_connector_helper_funcs);
14836fad8f66SThierry Reding 	sor->output.connector.dpms = DRM_MODE_DPMS_OFF;
14846fad8f66SThierry Reding 
14856fad8f66SThierry Reding 	drm_encoder_init(drm, &sor->output.encoder, &tegra_sor_encoder_funcs,
14866fad8f66SThierry Reding 			 DRM_MODE_ENCODER_TMDS);
14876fad8f66SThierry Reding 	drm_encoder_helper_add(&sor->output.encoder,
14886fad8f66SThierry Reding 			       &tegra_sor_encoder_helper_funcs);
14896fad8f66SThierry Reding 
14906fad8f66SThierry Reding 	drm_mode_connector_attach_encoder(&sor->output.connector,
14916fad8f66SThierry Reding 					  &sor->output.encoder);
14926fad8f66SThierry Reding 	drm_connector_register(&sor->output.connector);
14936fad8f66SThierry Reding 
1494ea130b24SThierry Reding 	err = tegra_output_init(drm, &sor->output);
1495ea130b24SThierry Reding 	if (err < 0) {
1496ea130b24SThierry Reding 		dev_err(client->dev, "failed to initialize output: %d\n", err);
1497ea130b24SThierry Reding 		return err;
1498ea130b24SThierry Reding 	}
14996fad8f66SThierry Reding 
1500ea130b24SThierry Reding 	sor->output.encoder.possible_crtcs = 0x3;
15016b6b6042SThierry Reding 
1502a82752e1SThierry Reding 	if (IS_ENABLED(CONFIG_DEBUG_FS)) {
15031b0c7b48SThierry Reding 		err = tegra_sor_debugfs_init(sor, drm->primary);
1504a82752e1SThierry Reding 		if (err < 0)
1505a82752e1SThierry Reding 			dev_err(sor->dev, "debugfs setup failed: %d\n", err);
1506a82752e1SThierry Reding 	}
1507a82752e1SThierry Reding 
15086b6b6042SThierry Reding 	if (sor->dpaux) {
15096b6b6042SThierry Reding 		err = tegra_dpaux_attach(sor->dpaux, &sor->output);
15106b6b6042SThierry Reding 		if (err < 0) {
15116b6b6042SThierry Reding 			dev_err(sor->dev, "failed to attach DP: %d\n", err);
15126b6b6042SThierry Reding 			return err;
15136b6b6042SThierry Reding 		}
15146b6b6042SThierry Reding 	}
15156b6b6042SThierry Reding 
1516535a65dbSTomeu Vizoso 	/*
1517535a65dbSTomeu Vizoso 	 * XXX: Remove this reset once proper hand-over from firmware to
1518535a65dbSTomeu Vizoso 	 * kernel is possible.
1519535a65dbSTomeu Vizoso 	 */
1520535a65dbSTomeu Vizoso 	err = reset_control_assert(sor->rst);
1521535a65dbSTomeu Vizoso 	if (err < 0) {
1522535a65dbSTomeu Vizoso 		dev_err(sor->dev, "failed to assert SOR reset: %d\n", err);
1523535a65dbSTomeu Vizoso 		return err;
1524535a65dbSTomeu Vizoso 	}
1525535a65dbSTomeu Vizoso 
15266fad8f66SThierry Reding 	err = clk_prepare_enable(sor->clk);
15276fad8f66SThierry Reding 	if (err < 0) {
15286fad8f66SThierry Reding 		dev_err(sor->dev, "failed to enable clock: %d\n", err);
15296fad8f66SThierry Reding 		return err;
15306fad8f66SThierry Reding 	}
15316fad8f66SThierry Reding 
1532535a65dbSTomeu Vizoso 	usleep_range(1000, 3000);
1533535a65dbSTomeu Vizoso 
1534535a65dbSTomeu Vizoso 	err = reset_control_deassert(sor->rst);
1535535a65dbSTomeu Vizoso 	if (err < 0) {
1536535a65dbSTomeu Vizoso 		dev_err(sor->dev, "failed to deassert SOR reset: %d\n", err);
1537535a65dbSTomeu Vizoso 		return err;
1538535a65dbSTomeu Vizoso 	}
1539535a65dbSTomeu Vizoso 
15406fad8f66SThierry Reding 	err = clk_prepare_enable(sor->clk_safe);
15416fad8f66SThierry Reding 	if (err < 0)
15426fad8f66SThierry Reding 		return err;
15436fad8f66SThierry Reding 
15446fad8f66SThierry Reding 	err = clk_prepare_enable(sor->clk_dp);
15456fad8f66SThierry Reding 	if (err < 0)
15466fad8f66SThierry Reding 		return err;
15476fad8f66SThierry Reding 
15486b6b6042SThierry Reding 	return 0;
15496b6b6042SThierry Reding }
15506b6b6042SThierry Reding 
15516b6b6042SThierry Reding static int tegra_sor_exit(struct host1x_client *client)
15526b6b6042SThierry Reding {
15536b6b6042SThierry Reding 	struct tegra_sor *sor = host1x_client_to_sor(client);
15546b6b6042SThierry Reding 	int err;
15556b6b6042SThierry Reding 
1556328ec69eSThierry Reding 	tegra_output_exit(&sor->output);
1557328ec69eSThierry Reding 
15586b6b6042SThierry Reding 	if (sor->dpaux) {
15596b6b6042SThierry Reding 		err = tegra_dpaux_detach(sor->dpaux);
15606b6b6042SThierry Reding 		if (err < 0) {
15616b6b6042SThierry Reding 			dev_err(sor->dev, "failed to detach DP: %d\n", err);
15626b6b6042SThierry Reding 			return err;
15636b6b6042SThierry Reding 		}
15646b6b6042SThierry Reding 	}
15656b6b6042SThierry Reding 
15666fad8f66SThierry Reding 	clk_disable_unprepare(sor->clk_safe);
15676fad8f66SThierry Reding 	clk_disable_unprepare(sor->clk_dp);
15686fad8f66SThierry Reding 	clk_disable_unprepare(sor->clk);
15696fad8f66SThierry Reding 
15704009c224SThierry Reding 	if (IS_ENABLED(CONFIG_DEBUG_FS))
15714009c224SThierry Reding 		tegra_sor_debugfs_exit(sor);
1572a82752e1SThierry Reding 
15736b6b6042SThierry Reding 	return 0;
15746b6b6042SThierry Reding }
15756b6b6042SThierry Reding 
15766b6b6042SThierry Reding static const struct host1x_client_ops sor_client_ops = {
15776b6b6042SThierry Reding 	.init = tegra_sor_init,
15786b6b6042SThierry Reding 	.exit = tegra_sor_exit,
15796b6b6042SThierry Reding };
15806b6b6042SThierry Reding 
15816b6b6042SThierry Reding static int tegra_sor_probe(struct platform_device *pdev)
15826b6b6042SThierry Reding {
15836b6b6042SThierry Reding 	struct device_node *np;
15846b6b6042SThierry Reding 	struct tegra_sor *sor;
15856b6b6042SThierry Reding 	struct resource *regs;
15866b6b6042SThierry Reding 	int err;
15876b6b6042SThierry Reding 
15886b6b6042SThierry Reding 	sor = devm_kzalloc(&pdev->dev, sizeof(*sor), GFP_KERNEL);
15896b6b6042SThierry Reding 	if (!sor)
15906b6b6042SThierry Reding 		return -ENOMEM;
15916b6b6042SThierry Reding 
15926b6b6042SThierry Reding 	sor->output.dev = sor->dev = &pdev->dev;
15936b6b6042SThierry Reding 
15946b6b6042SThierry Reding 	np = of_parse_phandle(pdev->dev.of_node, "nvidia,dpaux", 0);
15956b6b6042SThierry Reding 	if (np) {
15966b6b6042SThierry Reding 		sor->dpaux = tegra_dpaux_find_by_of_node(np);
15976b6b6042SThierry Reding 		of_node_put(np);
15986b6b6042SThierry Reding 
15996b6b6042SThierry Reding 		if (!sor->dpaux)
16006b6b6042SThierry Reding 			return -EPROBE_DEFER;
16016b6b6042SThierry Reding 	}
16026b6b6042SThierry Reding 
16036b6b6042SThierry Reding 	err = tegra_output_probe(&sor->output);
16044dbdc740SThierry Reding 	if (err < 0) {
16054dbdc740SThierry Reding 		dev_err(&pdev->dev, "failed to probe output: %d\n", err);
16066b6b6042SThierry Reding 		return err;
16074dbdc740SThierry Reding 	}
16086b6b6042SThierry Reding 
16096b6b6042SThierry Reding 	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
16106b6b6042SThierry Reding 	sor->regs = devm_ioremap_resource(&pdev->dev, regs);
16116b6b6042SThierry Reding 	if (IS_ERR(sor->regs))
16126b6b6042SThierry Reding 		return PTR_ERR(sor->regs);
16136b6b6042SThierry Reding 
16146b6b6042SThierry Reding 	sor->rst = devm_reset_control_get(&pdev->dev, "sor");
16154dbdc740SThierry Reding 	if (IS_ERR(sor->rst)) {
16164dbdc740SThierry Reding 		dev_err(&pdev->dev, "failed to get reset control: %ld\n",
16174dbdc740SThierry Reding 			PTR_ERR(sor->rst));
16186b6b6042SThierry Reding 		return PTR_ERR(sor->rst);
16194dbdc740SThierry Reding 	}
16206b6b6042SThierry Reding 
16216b6b6042SThierry Reding 	sor->clk = devm_clk_get(&pdev->dev, NULL);
16224dbdc740SThierry Reding 	if (IS_ERR(sor->clk)) {
16234dbdc740SThierry Reding 		dev_err(&pdev->dev, "failed to get module clock: %ld\n",
16244dbdc740SThierry Reding 			PTR_ERR(sor->clk));
16256b6b6042SThierry Reding 		return PTR_ERR(sor->clk);
16264dbdc740SThierry Reding 	}
16276b6b6042SThierry Reding 
16286b6b6042SThierry Reding 	sor->clk_parent = devm_clk_get(&pdev->dev, "parent");
16294dbdc740SThierry Reding 	if (IS_ERR(sor->clk_parent)) {
16304dbdc740SThierry Reding 		dev_err(&pdev->dev, "failed to get parent clock: %ld\n",
16314dbdc740SThierry Reding 			PTR_ERR(sor->clk_parent));
16326b6b6042SThierry Reding 		return PTR_ERR(sor->clk_parent);
16334dbdc740SThierry Reding 	}
16346b6b6042SThierry Reding 
16356b6b6042SThierry Reding 	sor->clk_safe = devm_clk_get(&pdev->dev, "safe");
16364dbdc740SThierry Reding 	if (IS_ERR(sor->clk_safe)) {
16374dbdc740SThierry Reding 		dev_err(&pdev->dev, "failed to get safe clock: %ld\n",
16384dbdc740SThierry Reding 			PTR_ERR(sor->clk_safe));
16396b6b6042SThierry Reding 		return PTR_ERR(sor->clk_safe);
16404dbdc740SThierry Reding 	}
16416b6b6042SThierry Reding 
16426b6b6042SThierry Reding 	sor->clk_dp = devm_clk_get(&pdev->dev, "dp");
16434dbdc740SThierry Reding 	if (IS_ERR(sor->clk_dp)) {
16444dbdc740SThierry Reding 		dev_err(&pdev->dev, "failed to get DP clock: %ld\n",
16454dbdc740SThierry Reding 			PTR_ERR(sor->clk_dp));
16466b6b6042SThierry Reding 		return PTR_ERR(sor->clk_dp);
16474dbdc740SThierry Reding 	}
16486b6b6042SThierry Reding 
16496b6b6042SThierry Reding 	INIT_LIST_HEAD(&sor->client.list);
16506b6b6042SThierry Reding 	sor->client.ops = &sor_client_ops;
16516b6b6042SThierry Reding 	sor->client.dev = &pdev->dev;
16526b6b6042SThierry Reding 
165386f5c52dSThierry Reding 	mutex_init(&sor->lock);
165486f5c52dSThierry Reding 
16556b6b6042SThierry Reding 	err = host1x_client_register(&sor->client);
16566b6b6042SThierry Reding 	if (err < 0) {
16576b6b6042SThierry Reding 		dev_err(&pdev->dev, "failed to register host1x client: %d\n",
16586b6b6042SThierry Reding 			err);
16596b6b6042SThierry Reding 		return err;
16606b6b6042SThierry Reding 	}
16616b6b6042SThierry Reding 
16626b6b6042SThierry Reding 	platform_set_drvdata(pdev, sor);
16636b6b6042SThierry Reding 
16646b6b6042SThierry Reding 	return 0;
16656b6b6042SThierry Reding }
16666b6b6042SThierry Reding 
16676b6b6042SThierry Reding static int tegra_sor_remove(struct platform_device *pdev)
16686b6b6042SThierry Reding {
16696b6b6042SThierry Reding 	struct tegra_sor *sor = platform_get_drvdata(pdev);
16706b6b6042SThierry Reding 	int err;
16716b6b6042SThierry Reding 
16726b6b6042SThierry Reding 	err = host1x_client_unregister(&sor->client);
16736b6b6042SThierry Reding 	if (err < 0) {
16746b6b6042SThierry Reding 		dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
16756b6b6042SThierry Reding 			err);
16766b6b6042SThierry Reding 		return err;
16776b6b6042SThierry Reding 	}
16786b6b6042SThierry Reding 
1679328ec69eSThierry Reding 	tegra_output_remove(&sor->output);
16806b6b6042SThierry Reding 
16816b6b6042SThierry Reding 	return 0;
16826b6b6042SThierry Reding }
16836b6b6042SThierry Reding 
16846b6b6042SThierry Reding static const struct of_device_id tegra_sor_of_match[] = {
16856b6b6042SThierry Reding 	{ .compatible = "nvidia,tegra124-sor", },
16866b6b6042SThierry Reding 	{ },
16876b6b6042SThierry Reding };
1688ef70728cSStephen Warren MODULE_DEVICE_TABLE(of, tegra_sor_of_match);
16896b6b6042SThierry Reding 
16906b6b6042SThierry Reding struct platform_driver tegra_sor_driver = {
16916b6b6042SThierry Reding 	.driver = {
16926b6b6042SThierry Reding 		.name = "tegra-sor",
16936b6b6042SThierry Reding 		.of_match_table = tegra_sor_of_match,
16946b6b6042SThierry Reding 	},
16956b6b6042SThierry Reding 	.probe = tegra_sor_probe,
16966b6b6042SThierry Reding 	.remove = tegra_sor_remove,
16976b6b6042SThierry Reding };
1698