xref: /openbmc/linux/drivers/gpu/drm/tegra/sor.c (revision a9a9e4fd7c923707a11b1b386cc31156d474039c)
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);
97*a9a9e4fdSThierry 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);
103*a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS0);
1046b6b6042SThierry Reding 
105*a9a9e4fdSThierry Reding 	value = SOR_LANE_POSTCURSOR_LANE3(0x00) |
106*a9a9e4fdSThierry Reding 		SOR_LANE_POSTCURSOR_LANE2(0x00) |
107*a9a9e4fdSThierry Reding 		SOR_LANE_POSTCURSOR_LANE1(0x00) |
108*a9a9e4fdSThierry Reding 		SOR_LANE_POSTCURSOR_LANE0(0x00);
109*a9a9e4fdSThierry 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 
114*a9a9e4fdSThierry 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? */
118*a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
1196b6b6042SThierry Reding 
120*a9a9e4fdSThierry 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;
123*a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
1246b6b6042SThierry Reding 
1256b6b6042SThierry Reding 	usleep_range(10, 100);
1266b6b6042SThierry Reding 
127*a9a9e4fdSThierry 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);
130*a9a9e4fdSThierry 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 
151*a9a9e4fdSThierry 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;
155*a9a9e4fdSThierry 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 {
192*a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
193*a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 1, SOR_SUPER_STATE0);
194*a9a9e4fdSThierry 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 {
199*a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE0);
200*a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 1, SOR_STATE0);
201*a9a9e4fdSThierry 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 */
238*a9a9e4fdSThierry 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;
241*a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
2426b6b6042SThierry Reding 	tegra_sor_super_update(sor);
2436b6b6042SThierry Reding 
2446b6b6042SThierry Reding 	/* attach */
245*a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
2466b6b6042SThierry Reding 	value |= SOR_SUPER_STATE_ATTACHED;
247*a9a9e4fdSThierry 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,
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 */
484*a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
4856fad8f66SThierry Reding 	value &= ~SOR_SUPER_STATE_MODE_NORMAL;
486*a9a9e4fdSThierry 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 */
501*a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
5026fad8f66SThierry Reding 	value &= ~SOR_SUPER_STATE_HEAD_MODE_MASK;
503*a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
5046fad8f66SThierry Reding 	tegra_sor_super_update(sor);
5056fad8f66SThierry Reding 
5066fad8f66SThierry Reding 	/* detach */
507*a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
5086fad8f66SThierry Reding 	value &= ~SOR_SUPER_STATE_ATTACHED;
509*a9a9e4fdSThierry 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 
555*a9a9e4fdSThierry 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);
558*a9a9e4fdSThierry 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 
578*a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
579*a9a9e4fdSThierry Reding 	value |= SOR_PLL2_PORT_POWERDOWN;
580*a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
5816fad8f66SThierry Reding 
5826fad8f66SThierry Reding 	usleep_range(20, 100);
5836fad8f66SThierry Reding 
584*a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL0);
585*a9a9e4fdSThierry Reding 	value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
586*a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL0);
5876fad8f66SThierry Reding 
588*a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
589*a9a9e4fdSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD;
590*a9a9e4fdSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
591*a9a9e4fdSThierry 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)) {
617*a9a9e4fdSThierry Reding 		value = tegra_sor_readl(sor, SOR_CRCA);
618*a9a9e4fdSThierry 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 
642*a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
6436fad8f66SThierry Reding 	value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
644*a9a9e4fdSThierry 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 
658*a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, SOR_CRCA_RESET, SOR_CRCA);
659*a9a9e4fdSThierry 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);
687*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_SUPER_STATE0);
688*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_SUPER_STATE1);
689*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_STATE0);
690*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_STATE1);
691*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE0(0));
692*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE0(1));
693*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE1(0));
694*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE1(1));
695*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE2(0));
696*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE2(1));
697*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE3(0));
698*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE3(1));
699*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE4(0));
700*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE4(1));
701*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE5(0));
702*a9a9e4fdSThierry 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);
709*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_PLL0);
710*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_PLL1);
711*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_PLL2);
712*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_PLL3);
713dab16336SThierry Reding 	DUMP_REG(SOR_CSTM);
714dab16336SThierry Reding 	DUMP_REG(SOR_LVDS);
715*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_CRCA);
716*a9a9e4fdSThierry 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);
738*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_VCRC_A0);
739*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_VCRC_A1);
740*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_VCRC_B0);
741*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_VCRC_B1);
742*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_CCRC_A0);
743*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_CCRC_A1);
744*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_CCRC_B0);
745*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_CCRC_B1);
746*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_EDATA_A0);
747*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_EDATA_A1);
748*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_EDATA_B0);
749*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_EDATA_B1);
750*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_COUNT_A0);
751*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_COUNT_A1);
752*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_COUNT_B0);
753*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_COUNT_B1);
754*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DEBUG_A0);
755*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DEBUG_A1);
756*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DEBUG_B0);
757*a9a9e4fdSThierry 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);
762*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_LINKCTL0);
763*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_LINKCTL1);
764*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE_DRIVE_CURRENT0);
765*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE_DRIVE_CURRENT1);
766*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE4_DRIVE_CURRENT0);
767*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE4_DRIVE_CURRENT1);
768*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE_PREEMPHASIS0);
769*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE_PREEMPHASIS1);
770*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE4_PREEMPHASIS0);
771*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE4_PREEMPHASIS1);
772*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE_POSTCURSOR0);
773*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE_POSTCURSOR1);
774*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_CONFIG0);
775*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_CONFIG1);
776*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_MN0);
777*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_MN1);
778*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_PADCTL0);
779*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_PADCTL1);
780*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_DEBUG0);
781*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_DEBUG1);
782*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_SPARE0);
783*a9a9e4fdSThierry 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);
788*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK0);
789*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK1);
790*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK2);
791*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK3);
792*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK4);
793*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK5);
794*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK6);
795dab16336SThierry Reding 	DUMP_REG(SOR_DP_TPG);
796dab16336SThierry Reding 	DUMP_REG(SOR_DP_TPG_CONFIG);
797*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_LQ_CSTM0);
798*a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_LQ_CSTM1);
799*a9a9e4fdSThierry 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 
8446fad8f66SThierry Reding 	return err;
8456fad8f66SThierry Reding 
846dab16336SThierry Reding free:
847dab16336SThierry Reding 	kfree(sor->debugfs_files);
848dab16336SThierry Reding 	sor->debugfs_files = NULL;
8496fad8f66SThierry Reding remove:
850dab16336SThierry Reding 	debugfs_remove_recursive(sor->debugfs);
8516fad8f66SThierry Reding 	sor->debugfs = NULL;
8526fad8f66SThierry Reding 	return err;
8536fad8f66SThierry Reding }
8546fad8f66SThierry Reding 
8554009c224SThierry Reding static void tegra_sor_debugfs_exit(struct tegra_sor *sor)
8566fad8f66SThierry Reding {
857dab16336SThierry Reding 	drm_debugfs_remove_files(sor->debugfs_files, ARRAY_SIZE(debugfs_files),
858dab16336SThierry Reding 				 sor->minor);
859dab16336SThierry Reding 	sor->minor = NULL;
860dab16336SThierry Reding 
861dab16336SThierry Reding 	kfree(sor->debugfs_files);
8626fad8f66SThierry Reding 	sor->debugfs = NULL;
863dab16336SThierry Reding 
864dab16336SThierry Reding 	debugfs_remove_recursive(sor->debugfs);
865dab16336SThierry Reding 	sor->debugfs_files = NULL;
8666fad8f66SThierry Reding }
8676fad8f66SThierry Reding 
8686fad8f66SThierry Reding static void tegra_sor_connector_dpms(struct drm_connector *connector, int mode)
8696fad8f66SThierry Reding {
8706fad8f66SThierry Reding }
8716fad8f66SThierry Reding 
8726fad8f66SThierry Reding static enum drm_connector_status
8736fad8f66SThierry Reding tegra_sor_connector_detect(struct drm_connector *connector, bool force)
8746fad8f66SThierry Reding {
8756fad8f66SThierry Reding 	struct tegra_output *output = connector_to_output(connector);
8766fad8f66SThierry Reding 	struct tegra_sor *sor = to_sor(output);
8776fad8f66SThierry Reding 
8786fad8f66SThierry Reding 	if (sor->dpaux)
8796fad8f66SThierry Reding 		return tegra_dpaux_detect(sor->dpaux);
8806fad8f66SThierry Reding 
8816fad8f66SThierry Reding 	return connector_status_unknown;
8826fad8f66SThierry Reding }
8836fad8f66SThierry Reding 
8846fad8f66SThierry Reding static const struct drm_connector_funcs tegra_sor_connector_funcs = {
8856fad8f66SThierry Reding 	.dpms = tegra_sor_connector_dpms,
8869d44189fSThierry Reding 	.reset = drm_atomic_helper_connector_reset,
8876fad8f66SThierry Reding 	.detect = tegra_sor_connector_detect,
8886fad8f66SThierry Reding 	.fill_modes = drm_helper_probe_single_connector_modes,
8896fad8f66SThierry Reding 	.destroy = tegra_output_connector_destroy,
8909d44189fSThierry Reding 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
8914aa3df71SThierry Reding 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
8926fad8f66SThierry Reding };
8936fad8f66SThierry Reding 
8946fad8f66SThierry Reding static int tegra_sor_connector_get_modes(struct drm_connector *connector)
8956fad8f66SThierry Reding {
8966fad8f66SThierry Reding 	struct tegra_output *output = connector_to_output(connector);
8976fad8f66SThierry Reding 	struct tegra_sor *sor = to_sor(output);
8986fad8f66SThierry Reding 	int err;
8996fad8f66SThierry Reding 
9006fad8f66SThierry Reding 	if (sor->dpaux)
9016fad8f66SThierry Reding 		tegra_dpaux_enable(sor->dpaux);
9026fad8f66SThierry Reding 
9036fad8f66SThierry Reding 	err = tegra_output_connector_get_modes(connector);
9046fad8f66SThierry Reding 
9056fad8f66SThierry Reding 	if (sor->dpaux)
9066fad8f66SThierry Reding 		tegra_dpaux_disable(sor->dpaux);
9076fad8f66SThierry Reding 
9086fad8f66SThierry Reding 	return err;
9096fad8f66SThierry Reding }
9106fad8f66SThierry Reding 
9116fad8f66SThierry Reding static enum drm_mode_status
9126fad8f66SThierry Reding tegra_sor_connector_mode_valid(struct drm_connector *connector,
9136fad8f66SThierry Reding 			       struct drm_display_mode *mode)
9146fad8f66SThierry Reding {
9156fad8f66SThierry Reding 	return MODE_OK;
9166fad8f66SThierry Reding }
9176fad8f66SThierry Reding 
9186fad8f66SThierry Reding static const struct drm_connector_helper_funcs tegra_sor_connector_helper_funcs = {
9196fad8f66SThierry Reding 	.get_modes = tegra_sor_connector_get_modes,
9206fad8f66SThierry Reding 	.mode_valid = tegra_sor_connector_mode_valid,
9216fad8f66SThierry Reding 	.best_encoder = tegra_output_connector_best_encoder,
9226fad8f66SThierry Reding };
9236fad8f66SThierry Reding 
9246fad8f66SThierry Reding static const struct drm_encoder_funcs tegra_sor_encoder_funcs = {
9256fad8f66SThierry Reding 	.destroy = tegra_output_encoder_destroy,
9266fad8f66SThierry Reding };
9276fad8f66SThierry Reding 
9286fad8f66SThierry Reding static void tegra_sor_encoder_dpms(struct drm_encoder *encoder, int mode)
9296fad8f66SThierry Reding {
9306fad8f66SThierry Reding }
9316fad8f66SThierry Reding 
9326fad8f66SThierry Reding static void tegra_sor_encoder_prepare(struct drm_encoder *encoder)
9336fad8f66SThierry Reding {
9346fad8f66SThierry Reding }
9356fad8f66SThierry Reding 
9366fad8f66SThierry Reding static void tegra_sor_encoder_commit(struct drm_encoder *encoder)
9376fad8f66SThierry Reding {
9386fad8f66SThierry Reding }
9396fad8f66SThierry Reding 
9406fad8f66SThierry Reding static void tegra_sor_encoder_mode_set(struct drm_encoder *encoder,
9416fad8f66SThierry Reding 				       struct drm_display_mode *mode,
9426fad8f66SThierry Reding 				       struct drm_display_mode *adjusted)
9436fad8f66SThierry Reding {
9446fad8f66SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
9456fad8f66SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
9466b6b6042SThierry Reding 	unsigned int vbe, vse, hbe, hse, vbs, hbs, i;
9476b6b6042SThierry Reding 	struct tegra_sor *sor = to_sor(output);
94834fa183bSThierry Reding 	struct tegra_sor_config config;
94934fa183bSThierry Reding 	struct drm_dp_link link;
95034fa183bSThierry Reding 	struct drm_dp_aux *aux;
95186f5c52dSThierry Reding 	int err = 0;
95228fe2076SThierry Reding 	u32 value;
95386f5c52dSThierry Reding 
95486f5c52dSThierry Reding 	mutex_lock(&sor->lock);
9556b6b6042SThierry Reding 
9566b6b6042SThierry Reding 	if (sor->enabled)
95786f5c52dSThierry Reding 		goto unlock;
9586b6b6042SThierry Reding 
9596b6b6042SThierry Reding 	err = clk_prepare_enable(sor->clk);
9606b6b6042SThierry Reding 	if (err < 0)
96186f5c52dSThierry Reding 		goto unlock;
9626b6b6042SThierry Reding 
9636b6b6042SThierry Reding 	reset_control_deassert(sor->rst);
9646b6b6042SThierry Reding 
9656fad8f66SThierry Reding 	if (output->panel)
9666fad8f66SThierry Reding 		drm_panel_prepare(output->panel);
9676fad8f66SThierry Reding 
96834fa183bSThierry Reding 	/* FIXME: properly convert to struct drm_dp_aux */
96934fa183bSThierry Reding 	aux = (struct drm_dp_aux *)sor->dpaux;
97034fa183bSThierry Reding 
9716b6b6042SThierry Reding 	if (sor->dpaux) {
9726b6b6042SThierry Reding 		err = tegra_dpaux_enable(sor->dpaux);
9736b6b6042SThierry Reding 		if (err < 0)
9746b6b6042SThierry Reding 			dev_err(sor->dev, "failed to enable DP: %d\n", err);
97534fa183bSThierry Reding 
97634fa183bSThierry Reding 		err = drm_dp_link_probe(aux, &link);
97734fa183bSThierry Reding 		if (err < 0) {
97834fa183bSThierry Reding 			dev_err(sor->dev, "failed to probe eDP link: %d\n",
97934fa183bSThierry Reding 				err);
9802263c460SDan Carpenter 			goto unlock;
98134fa183bSThierry Reding 		}
9826b6b6042SThierry Reding 	}
9836b6b6042SThierry Reding 
9846b6b6042SThierry Reding 	err = clk_set_parent(sor->clk, sor->clk_safe);
9856b6b6042SThierry Reding 	if (err < 0)
9866b6b6042SThierry Reding 		dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
9876b6b6042SThierry Reding 
98834fa183bSThierry Reding 	memset(&config, 0, sizeof(config));
989054b1bd1SStéphane Marchesin 	config.bits_per_pixel = output->connector.display_info.bpc * 3;
99034fa183bSThierry Reding 
99134fa183bSThierry Reding 	err = tegra_sor_calc_config(sor, mode, &config, &link);
99234fa183bSThierry Reding 	if (err < 0)
99334fa183bSThierry Reding 		dev_err(sor->dev, "failed to compute link configuration: %d\n",
99434fa183bSThierry Reding 			err);
99534fa183bSThierry Reding 
9966b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
9976b6b6042SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
9986b6b6042SThierry Reding 	value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
9996b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
10006b6b6042SThierry Reding 
1001*a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1002*a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
1003*a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
10046b6b6042SThierry Reding 	usleep_range(20, 100);
10056b6b6042SThierry Reding 
1006*a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL3);
1007*a9a9e4fdSThierry Reding 	value |= SOR_PLL3_PLL_VDD_MODE_3V3;
1008*a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL3);
10096b6b6042SThierry Reding 
1010*a9a9e4fdSThierry Reding 	value = SOR_PLL0_ICHPMP(0xf) | SOR_PLL0_VCOCAP_RST |
1011*a9a9e4fdSThierry Reding 		SOR_PLL0_PLLREG_LEVEL_V45 | SOR_PLL0_RESISTOR_EXT;
1012*a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL0);
10136b6b6042SThierry Reding 
1014*a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1015*a9a9e4fdSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD;
1016*a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1017*a9a9e4fdSThierry Reding 	value |= SOR_PLL2_LVDS_ENABLE;
1018*a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
10196b6b6042SThierry Reding 
1020*a9a9e4fdSThierry Reding 	value = SOR_PLL1_TERM_COMPOUT | SOR_PLL1_TMDS_TERM;
1021*a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL1);
10226b6b6042SThierry Reding 
10236b6b6042SThierry Reding 	while (true) {
1024*a9a9e4fdSThierry Reding 		value = tegra_sor_readl(sor, SOR_PLL2);
1025*a9a9e4fdSThierry Reding 		if ((value & SOR_PLL2_SEQ_PLLCAPPD_ENFORCE) == 0)
10266b6b6042SThierry Reding 			break;
10276b6b6042SThierry Reding 
10286b6b6042SThierry Reding 		usleep_range(250, 1000);
10296b6b6042SThierry Reding 	}
10306b6b6042SThierry Reding 
1031*a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1032*a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
1033*a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_PORT_POWERDOWN;
1034*a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
10356b6b6042SThierry Reding 
10366b6b6042SThierry Reding 	/*
10376b6b6042SThierry Reding 	 * power up
10386b6b6042SThierry Reding 	 */
10396b6b6042SThierry Reding 
10406b6b6042SThierry Reding 	/* set safe link bandwidth (1.62 Gbps) */
10416b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
10426b6b6042SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
10436b6b6042SThierry Reding 	value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G1_62;
10446b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
10456b6b6042SThierry Reding 
10466b6b6042SThierry Reding 	/* step 1 */
1047*a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1048*a9a9e4fdSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE | SOR_PLL2_PORT_POWERDOWN |
1049*a9a9e4fdSThierry Reding 		 SOR_PLL2_BANDGAP_POWERDOWN;
1050*a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
10516b6b6042SThierry Reding 
1052*a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL0);
1053*a9a9e4fdSThierry Reding 	value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
1054*a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL0);
10556b6b6042SThierry Reding 
1056*a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
10576b6b6042SThierry Reding 	value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
1058*a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
10596b6b6042SThierry Reding 
10606b6b6042SThierry Reding 	/* step 2 */
10616b6b6042SThierry Reding 	err = tegra_io_rail_power_on(TEGRA_IO_RAIL_LVDS);
10626b6b6042SThierry Reding 	if (err < 0) {
10636b6b6042SThierry Reding 		dev_err(sor->dev, "failed to power on I/O rail: %d\n", err);
106486f5c52dSThierry Reding 		goto unlock;
10656b6b6042SThierry Reding 	}
10666b6b6042SThierry Reding 
10676b6b6042SThierry Reding 	usleep_range(5, 100);
10686b6b6042SThierry Reding 
10696b6b6042SThierry Reding 	/* step 3 */
1070*a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1071*a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
1072*a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
10736b6b6042SThierry Reding 
10746b6b6042SThierry Reding 	usleep_range(20, 100);
10756b6b6042SThierry Reding 
10766b6b6042SThierry Reding 	/* step 4 */
1077*a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL0);
1078*a9a9e4fdSThierry Reding 	value &= ~SOR_PLL0_VCOPD;
1079*a9a9e4fdSThierry Reding 	value &= ~SOR_PLL0_PWR;
1080*a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL0);
10816b6b6042SThierry Reding 
1082*a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1083*a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1084*a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
10856b6b6042SThierry Reding 
10866b6b6042SThierry Reding 	usleep_range(200, 1000);
10876b6b6042SThierry Reding 
10886b6b6042SThierry Reding 	/* step 5 */
1089*a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1090*a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_PORT_POWERDOWN;
1091*a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
10926b6b6042SThierry Reding 
10936b6b6042SThierry Reding 	/* switch to DP clock */
10946b6b6042SThierry Reding 	err = clk_set_parent(sor->clk, sor->clk_dp);
10956b6b6042SThierry Reding 	if (err < 0)
10966b6b6042SThierry Reding 		dev_err(sor->dev, "failed to set DP parent clock: %d\n", err);
10976b6b6042SThierry Reding 
1098899451b7SThierry Reding 	/* power DP lanes */
1099*a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
1100899451b7SThierry Reding 
1101899451b7SThierry Reding 	if (link.num_lanes <= 2)
1102899451b7SThierry Reding 		value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2);
1103899451b7SThierry Reding 	else
1104899451b7SThierry Reding 		value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2;
1105899451b7SThierry Reding 
1106899451b7SThierry Reding 	if (link.num_lanes <= 1)
1107899451b7SThierry Reding 		value &= ~SOR_DP_PADCTL_PD_TXD_1;
1108899451b7SThierry Reding 	else
1109899451b7SThierry Reding 		value |= SOR_DP_PADCTL_PD_TXD_1;
1110899451b7SThierry Reding 
1111899451b7SThierry Reding 	if (link.num_lanes == 0)
1112899451b7SThierry Reding 		value &= ~SOR_DP_PADCTL_PD_TXD_0;
1113899451b7SThierry Reding 	else
1114899451b7SThierry Reding 		value |= SOR_DP_PADCTL_PD_TXD_0;
1115899451b7SThierry Reding 
1116*a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
11176b6b6042SThierry Reding 
1118*a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
11196b6b6042SThierry Reding 	value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
11200c90a184SThierry Reding 	value |= SOR_DP_LINKCTL_LANE_COUNT(link.num_lanes);
1121*a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
11226b6b6042SThierry Reding 
11236b6b6042SThierry Reding 	/* start lane sequencer */
11246b6b6042SThierry Reding 	value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
11256b6b6042SThierry Reding 		SOR_LANE_SEQ_CTL_POWER_STATE_UP;
11266b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
11276b6b6042SThierry Reding 
11286b6b6042SThierry Reding 	while (true) {
11296b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
11306b6b6042SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
11316b6b6042SThierry Reding 			break;
11326b6b6042SThierry Reding 
11336b6b6042SThierry Reding 		usleep_range(250, 1000);
11346b6b6042SThierry Reding 	}
11356b6b6042SThierry Reding 
1136a4263fedSThierry Reding 	/* set link bandwidth */
11376b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
11386b6b6042SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
1139a4263fedSThierry Reding 	value |= drm_dp_link_rate_to_bw_code(link.rate) << 2;
11406b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
11416b6b6042SThierry Reding 
11426b6b6042SThierry Reding 	/* set linkctl */
1143*a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
11446b6b6042SThierry Reding 	value |= SOR_DP_LINKCTL_ENABLE;
11456b6b6042SThierry Reding 
11466b6b6042SThierry Reding 	value &= ~SOR_DP_LINKCTL_TU_SIZE_MASK;
114734fa183bSThierry Reding 	value |= SOR_DP_LINKCTL_TU_SIZE(config.tu_size);
11486b6b6042SThierry Reding 
11496b6b6042SThierry Reding 	value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
1150*a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
11516b6b6042SThierry Reding 
11526b6b6042SThierry Reding 	for (i = 0, value = 0; i < 4; i++) {
11536b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
11546b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_GALIOS |
11556b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_NONE;
11566b6b6042SThierry Reding 		value = (value << 8) | lane;
11576b6b6042SThierry Reding 	}
11586b6b6042SThierry Reding 
11596b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
11606b6b6042SThierry Reding 
1161*a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_CONFIG0);
11626b6b6042SThierry Reding 	value &= ~SOR_DP_CONFIG_WATERMARK_MASK;
116334fa183bSThierry Reding 	value |= SOR_DP_CONFIG_WATERMARK(config.watermark);
11646b6b6042SThierry Reding 
11656b6b6042SThierry Reding 	value &= ~SOR_DP_CONFIG_ACTIVE_SYM_COUNT_MASK;
116634fa183bSThierry Reding 	value |= SOR_DP_CONFIG_ACTIVE_SYM_COUNT(config.active_count);
11676b6b6042SThierry Reding 
11686b6b6042SThierry Reding 	value &= ~SOR_DP_CONFIG_ACTIVE_SYM_FRAC_MASK;
116934fa183bSThierry Reding 	value |= SOR_DP_CONFIG_ACTIVE_SYM_FRAC(config.active_frac);
11706b6b6042SThierry Reding 
117134fa183bSThierry Reding 	if (config.active_polarity)
117234fa183bSThierry Reding 		value |= SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
117334fa183bSThierry Reding 	else
117434fa183bSThierry Reding 		value &= ~SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
11756b6b6042SThierry Reding 
11766b6b6042SThierry Reding 	value |= SOR_DP_CONFIG_ACTIVE_SYM_ENABLE;
11771f64ae7cSThierry Reding 	value |= SOR_DP_CONFIG_DISPARITY_NEGATIVE;
1178*a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_CONFIG0);
11796b6b6042SThierry Reding 
11806b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_AUDIO_HBLANK_SYMBOLS);
11816b6b6042SThierry Reding 	value &= ~SOR_DP_AUDIO_HBLANK_SYMBOLS_MASK;
11827890b576SThierry Reding 	value |= config.hblank_symbols & 0xffff;
11836b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_AUDIO_HBLANK_SYMBOLS);
11846b6b6042SThierry Reding 
11856b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_AUDIO_VBLANK_SYMBOLS);
11866b6b6042SThierry Reding 	value &= ~SOR_DP_AUDIO_VBLANK_SYMBOLS_MASK;
11877890b576SThierry Reding 	value |= config.vblank_symbols & 0xffff;
11886b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_AUDIO_VBLANK_SYMBOLS);
11896b6b6042SThierry Reding 
11906b6b6042SThierry Reding 	/* enable pad calibration logic */
1191*a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
11926b6b6042SThierry Reding 	value |= SOR_DP_PADCTL_PAD_CAL_PD;
1193*a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
11946b6b6042SThierry Reding 
11956b6b6042SThierry Reding 	if (sor->dpaux) {
11966b6b6042SThierry Reding 		u8 rate, lanes;
11976b6b6042SThierry Reding 
11986b6b6042SThierry Reding 		err = drm_dp_link_probe(aux, &link);
11996b6b6042SThierry Reding 		if (err < 0) {
12006b6b6042SThierry Reding 			dev_err(sor->dev, "failed to probe eDP link: %d\n",
12016b6b6042SThierry Reding 				err);
120286f5c52dSThierry Reding 			goto unlock;
12036b6b6042SThierry Reding 		}
12046b6b6042SThierry Reding 
12056b6b6042SThierry Reding 		err = drm_dp_link_power_up(aux, &link);
12066b6b6042SThierry Reding 		if (err < 0) {
12076b6b6042SThierry Reding 			dev_err(sor->dev, "failed to power up eDP link: %d\n",
12086b6b6042SThierry Reding 				err);
120986f5c52dSThierry Reding 			goto unlock;
12106b6b6042SThierry Reding 		}
12116b6b6042SThierry Reding 
12126b6b6042SThierry Reding 		err = drm_dp_link_configure(aux, &link);
12136b6b6042SThierry Reding 		if (err < 0) {
12146b6b6042SThierry Reding 			dev_err(sor->dev, "failed to configure eDP link: %d\n",
12156b6b6042SThierry Reding 				err);
121686f5c52dSThierry Reding 			goto unlock;
12176b6b6042SThierry Reding 		}
12186b6b6042SThierry Reding 
12196b6b6042SThierry Reding 		rate = drm_dp_link_rate_to_bw_code(link.rate);
12206b6b6042SThierry Reding 		lanes = link.num_lanes;
12216b6b6042SThierry Reding 
12226b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
12236b6b6042SThierry Reding 		value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
12246b6b6042SThierry Reding 		value |= SOR_CLK_CNTRL_DP_LINK_SPEED(rate);
12256b6b6042SThierry Reding 		tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
12266b6b6042SThierry Reding 
1227*a9a9e4fdSThierry Reding 		value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
12286b6b6042SThierry Reding 		value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
12296b6b6042SThierry Reding 		value |= SOR_DP_LINKCTL_LANE_COUNT(lanes);
12306b6b6042SThierry Reding 
12316b6b6042SThierry Reding 		if (link.capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
12326b6b6042SThierry Reding 			value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
12336b6b6042SThierry Reding 
1234*a9a9e4fdSThierry Reding 		tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
12356b6b6042SThierry Reding 
12366b6b6042SThierry Reding 		/* disable training pattern generator */
12376b6b6042SThierry Reding 
12386b6b6042SThierry Reding 		for (i = 0; i < link.num_lanes; i++) {
12396b6b6042SThierry Reding 			unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
12406b6b6042SThierry Reding 					     SOR_DP_TPG_SCRAMBLER_GALIOS |
12416b6b6042SThierry Reding 					     SOR_DP_TPG_PATTERN_NONE;
12426b6b6042SThierry Reding 			value = (value << 8) | lane;
12436b6b6042SThierry Reding 		}
12446b6b6042SThierry Reding 
12456b6b6042SThierry Reding 		tegra_sor_writel(sor, value, SOR_DP_TPG);
12466b6b6042SThierry Reding 
12476b6b6042SThierry Reding 		err = tegra_sor_dp_train_fast(sor, &link);
12486b6b6042SThierry Reding 		if (err < 0) {
12496b6b6042SThierry Reding 			dev_err(sor->dev, "DP fast link training failed: %d\n",
12506b6b6042SThierry Reding 				err);
125186f5c52dSThierry Reding 			goto unlock;
12526b6b6042SThierry Reding 		}
12536b6b6042SThierry Reding 
12546b6b6042SThierry Reding 		dev_dbg(sor->dev, "fast link training succeeded\n");
12556b6b6042SThierry Reding 	}
12566b6b6042SThierry Reding 
12576b6b6042SThierry Reding 	err = tegra_sor_power_up(sor, 250);
12586b6b6042SThierry Reding 	if (err < 0) {
12596b6b6042SThierry Reding 		dev_err(sor->dev, "failed to power up SOR: %d\n", err);
126086f5c52dSThierry Reding 		goto unlock;
12616b6b6042SThierry Reding 	}
12626b6b6042SThierry Reding 
12636b6b6042SThierry Reding 	/*
12646b6b6042SThierry Reding 	 * configure panel (24bpp, vsync-, hsync-, DP-A protocol, complete
12656b6b6042SThierry Reding 	 * raster, associate with display controller)
12666b6b6042SThierry Reding 	 */
12673f4f3b5fSThierry Reding 	value = SOR_STATE_ASY_PROTOCOL_DP_A |
12686b6b6042SThierry Reding 		SOR_STATE_ASY_CRC_MODE_COMPLETE |
12696b6b6042SThierry Reding 		SOR_STATE_ASY_OWNER(dc->pipe + 1);
127034fa183bSThierry Reding 
12713f4f3b5fSThierry Reding 	if (mode->flags & DRM_MODE_FLAG_PHSYNC)
12723f4f3b5fSThierry Reding 		value &= ~SOR_STATE_ASY_HSYNCPOL;
12733f4f3b5fSThierry Reding 
12743f4f3b5fSThierry Reding 	if (mode->flags & DRM_MODE_FLAG_NHSYNC)
12753f4f3b5fSThierry Reding 		value |= SOR_STATE_ASY_HSYNCPOL;
12763f4f3b5fSThierry Reding 
12773f4f3b5fSThierry Reding 	if (mode->flags & DRM_MODE_FLAG_PVSYNC)
12783f4f3b5fSThierry Reding 		value &= ~SOR_STATE_ASY_VSYNCPOL;
12793f4f3b5fSThierry Reding 
12803f4f3b5fSThierry Reding 	if (mode->flags & DRM_MODE_FLAG_NVSYNC)
12813f4f3b5fSThierry Reding 		value |= SOR_STATE_ASY_VSYNCPOL;
12823f4f3b5fSThierry Reding 
128334fa183bSThierry Reding 	switch (config.bits_per_pixel) {
128434fa183bSThierry Reding 	case 24:
128534fa183bSThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
128634fa183bSThierry Reding 		break;
128734fa183bSThierry Reding 
128834fa183bSThierry Reding 	case 18:
128934fa183bSThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_18_444;
129034fa183bSThierry Reding 		break;
129134fa183bSThierry Reding 
129234fa183bSThierry Reding 	default:
129334fa183bSThierry Reding 		BUG();
129434fa183bSThierry Reding 		break;
129534fa183bSThierry Reding 	}
129634fa183bSThierry Reding 
1297*a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
12986b6b6042SThierry Reding 
12996b6b6042SThierry Reding 	/*
13006b6b6042SThierry Reding 	 * TODO: The video timing programming below doesn't seem to match the
13016b6b6042SThierry Reding 	 * register definitions.
13026b6b6042SThierry Reding 	 */
13036b6b6042SThierry Reding 
13046b6b6042SThierry Reding 	value = ((mode->vtotal & 0x7fff) << 16) | (mode->htotal & 0x7fff);
1305*a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_HEAD_STATE1(0));
13066b6b6042SThierry Reding 
13076b6b6042SThierry Reding 	vse = mode->vsync_end - mode->vsync_start - 1;
13086b6b6042SThierry Reding 	hse = mode->hsync_end - mode->hsync_start - 1;
13096b6b6042SThierry Reding 
13106b6b6042SThierry Reding 	value = ((vse & 0x7fff) << 16) | (hse & 0x7fff);
1311*a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_HEAD_STATE2(0));
13126b6b6042SThierry Reding 
13136b6b6042SThierry Reding 	vbe = vse + (mode->vsync_start - mode->vdisplay);
13146b6b6042SThierry Reding 	hbe = hse + (mode->hsync_start - mode->hdisplay);
13156b6b6042SThierry Reding 
13166b6b6042SThierry Reding 	value = ((vbe & 0x7fff) << 16) | (hbe & 0x7fff);
1317*a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_HEAD_STATE3(0));
13186b6b6042SThierry Reding 
13196b6b6042SThierry Reding 	vbs = vbe + mode->vdisplay;
13206b6b6042SThierry Reding 	hbs = hbe + mode->hdisplay;
13216b6b6042SThierry Reding 
13226b6b6042SThierry Reding 	value = ((vbs & 0x7fff) << 16) | (hbs & 0x7fff);
1323*a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_HEAD_STATE4(0));
13246b6b6042SThierry Reding 
13256b6b6042SThierry Reding 	/* CSTM (LVDS, link A/B, upper) */
1326143b1df2SStéphane Marchesin 	value = SOR_CSTM_LVDS | SOR_CSTM_LINK_ACT_A | SOR_CSTM_LINK_ACT_B |
13276b6b6042SThierry Reding 		SOR_CSTM_UPPER;
13286b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CSTM);
13296b6b6042SThierry Reding 
13306b6b6042SThierry Reding 	/* PWM setup */
13316b6b6042SThierry Reding 	err = tegra_sor_setup_pwm(sor, 250);
13326b6b6042SThierry Reding 	if (err < 0) {
13336b6b6042SThierry Reding 		dev_err(sor->dev, "failed to setup PWM: %d\n", err);
133486f5c52dSThierry Reding 		goto unlock;
13356b6b6042SThierry Reding 	}
13366b6b6042SThierry Reding 
1337666cb873SThierry Reding 	tegra_sor_update(sor);
1338666cb873SThierry Reding 
13396b6b6042SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
13406b6b6042SThierry Reding 	value |= SOR_ENABLE;
13416b6b6042SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
13426b6b6042SThierry Reding 
1343666cb873SThierry Reding 	tegra_dc_commit(dc);
13446b6b6042SThierry Reding 
13456b6b6042SThierry Reding 	err = tegra_sor_attach(sor);
13466b6b6042SThierry Reding 	if (err < 0) {
13476b6b6042SThierry Reding 		dev_err(sor->dev, "failed to attach SOR: %d\n", err);
134886f5c52dSThierry Reding 		goto unlock;
13496b6b6042SThierry Reding 	}
13506b6b6042SThierry Reding 
13516b6b6042SThierry Reding 	err = tegra_sor_wakeup(sor);
13526b6b6042SThierry Reding 	if (err < 0) {
13536b6b6042SThierry Reding 		dev_err(sor->dev, "failed to enable DC: %d\n", err);
135486f5c52dSThierry Reding 		goto unlock;
13556b6b6042SThierry Reding 	}
13566b6b6042SThierry Reding 
13576fad8f66SThierry Reding 	if (output->panel)
13586fad8f66SThierry Reding 		drm_panel_enable(output->panel);
13596fad8f66SThierry Reding 
13606b6b6042SThierry Reding 	sor->enabled = true;
13616b6b6042SThierry Reding 
136286f5c52dSThierry Reding unlock:
136386f5c52dSThierry Reding 	mutex_unlock(&sor->lock);
13646b6b6042SThierry Reding }
13656b6b6042SThierry Reding 
13666fad8f66SThierry Reding static void tegra_sor_encoder_disable(struct drm_encoder *encoder)
13676b6b6042SThierry Reding {
13686fad8f66SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
13696fad8f66SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
13706b6b6042SThierry Reding 	struct tegra_sor *sor = to_sor(output);
13716fad8f66SThierry Reding 	u32 value;
13726fad8f66SThierry Reding 	int err;
137386f5c52dSThierry Reding 
137486f5c52dSThierry Reding 	mutex_lock(&sor->lock);
13756b6b6042SThierry Reding 
13766b6b6042SThierry Reding 	if (!sor->enabled)
137786f5c52dSThierry Reding 		goto unlock;
13786b6b6042SThierry Reding 
13796fad8f66SThierry Reding 	if (output->panel)
13806fad8f66SThierry Reding 		drm_panel_disable(output->panel);
13816fad8f66SThierry Reding 
13826b6b6042SThierry Reding 	err = tegra_sor_detach(sor);
13836b6b6042SThierry Reding 	if (err < 0) {
13846b6b6042SThierry Reding 		dev_err(sor->dev, "failed to detach SOR: %d\n", err);
138586f5c52dSThierry Reding 		goto unlock;
13866b6b6042SThierry Reding 	}
13876b6b6042SThierry Reding 
1388*a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE1);
13896b6b6042SThierry Reding 	tegra_sor_update(sor);
13906b6b6042SThierry Reding 
13916b6b6042SThierry Reding 	/*
13926b6b6042SThierry Reding 	 * The following accesses registers of the display controller, so make
13936b6b6042SThierry Reding 	 * sure it's only executed when the output is attached to one.
13946b6b6042SThierry Reding 	 */
13956b6b6042SThierry Reding 	if (dc) {
13966b6b6042SThierry Reding 		value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
13976b6b6042SThierry Reding 		value &= ~SOR_ENABLE;
13986b6b6042SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
13996b6b6042SThierry Reding 
140062b9e063SThierry Reding 		tegra_dc_commit(dc);
14016b6b6042SThierry Reding 	}
14026b6b6042SThierry Reding 
14036b6b6042SThierry Reding 	err = tegra_sor_power_down(sor);
14046b6b6042SThierry Reding 	if (err < 0) {
14056b6b6042SThierry Reding 		dev_err(sor->dev, "failed to power down SOR: %d\n", err);
140686f5c52dSThierry Reding 		goto unlock;
14076b6b6042SThierry Reding 	}
14086b6b6042SThierry Reding 
14096b6b6042SThierry Reding 	if (sor->dpaux) {
14106b6b6042SThierry Reding 		err = tegra_dpaux_disable(sor->dpaux);
14116b6b6042SThierry Reding 		if (err < 0) {
14126b6b6042SThierry Reding 			dev_err(sor->dev, "failed to disable DP: %d\n", err);
141386f5c52dSThierry Reding 			goto unlock;
14146b6b6042SThierry Reding 		}
14156b6b6042SThierry Reding 	}
14166b6b6042SThierry Reding 
14176b6b6042SThierry Reding 	err = tegra_io_rail_power_off(TEGRA_IO_RAIL_LVDS);
14186b6b6042SThierry Reding 	if (err < 0) {
14196b6b6042SThierry Reding 		dev_err(sor->dev, "failed to power off I/O rail: %d\n", err);
142086f5c52dSThierry Reding 		goto unlock;
14216b6b6042SThierry Reding 	}
14226b6b6042SThierry Reding 
14236fad8f66SThierry Reding 	if (output->panel)
14246fad8f66SThierry Reding 		drm_panel_unprepare(output->panel);
14256fad8f66SThierry Reding 
14266b6b6042SThierry Reding 	clk_disable_unprepare(sor->clk);
14276fad8f66SThierry Reding 	reset_control_assert(sor->rst);
14286b6b6042SThierry Reding 
14296b6b6042SThierry Reding 	sor->enabled = false;
14306b6b6042SThierry Reding 
143186f5c52dSThierry Reding unlock:
143286f5c52dSThierry Reding 	mutex_unlock(&sor->lock);
14336b6b6042SThierry Reding }
14346b6b6042SThierry Reding 
143582f1511cSThierry Reding static int
143682f1511cSThierry Reding tegra_sor_encoder_atomic_check(struct drm_encoder *encoder,
143782f1511cSThierry Reding 			       struct drm_crtc_state *crtc_state,
143882f1511cSThierry Reding 			       struct drm_connector_state *conn_state)
143982f1511cSThierry Reding {
144082f1511cSThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
144182f1511cSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(conn_state->crtc);
144282f1511cSThierry Reding 	unsigned long pclk = crtc_state->mode.clock * 1000;
144382f1511cSThierry Reding 	struct tegra_sor *sor = to_sor(output);
144482f1511cSThierry Reding 	int err;
144582f1511cSThierry Reding 
144682f1511cSThierry Reding 	err = tegra_dc_state_setup_clock(dc, crtc_state, sor->clk_parent,
144782f1511cSThierry Reding 					 pclk, 0);
144882f1511cSThierry Reding 	if (err < 0) {
144982f1511cSThierry Reding 		dev_err(output->dev, "failed to setup CRTC state: %d\n", err);
145082f1511cSThierry Reding 		return err;
145182f1511cSThierry Reding 	}
145282f1511cSThierry Reding 
145382f1511cSThierry Reding 	return 0;
145482f1511cSThierry Reding }
145582f1511cSThierry Reding 
14566fad8f66SThierry Reding static const struct drm_encoder_helper_funcs tegra_sor_encoder_helper_funcs = {
14576fad8f66SThierry Reding 	.dpms = tegra_sor_encoder_dpms,
14586fad8f66SThierry Reding 	.prepare = tegra_sor_encoder_prepare,
14596fad8f66SThierry Reding 	.commit = tegra_sor_encoder_commit,
14606fad8f66SThierry Reding 	.mode_set = tegra_sor_encoder_mode_set,
14616fad8f66SThierry Reding 	.disable = tegra_sor_encoder_disable,
146282f1511cSThierry Reding 	.atomic_check = tegra_sor_encoder_atomic_check,
14636b6b6042SThierry Reding };
14646b6b6042SThierry Reding 
14656b6b6042SThierry Reding static int tegra_sor_init(struct host1x_client *client)
14666b6b6042SThierry Reding {
14679910f5c4SThierry Reding 	struct drm_device *drm = dev_get_drvdata(client->parent);
14686b6b6042SThierry Reding 	struct tegra_sor *sor = host1x_client_to_sor(client);
14696b6b6042SThierry Reding 	int err;
14706b6b6042SThierry Reding 
14716b6b6042SThierry Reding 	if (!sor->dpaux)
14726b6b6042SThierry Reding 		return -ENODEV;
14736b6b6042SThierry Reding 
14746b6b6042SThierry Reding 	sor->output.dev = sor->dev;
14756b6b6042SThierry Reding 
14766fad8f66SThierry Reding 	drm_connector_init(drm, &sor->output.connector,
14776fad8f66SThierry Reding 			   &tegra_sor_connector_funcs,
14786fad8f66SThierry Reding 			   DRM_MODE_CONNECTOR_eDP);
14796fad8f66SThierry Reding 	drm_connector_helper_add(&sor->output.connector,
14806fad8f66SThierry Reding 				 &tegra_sor_connector_helper_funcs);
14816fad8f66SThierry Reding 	sor->output.connector.dpms = DRM_MODE_DPMS_OFF;
14826fad8f66SThierry Reding 
14836fad8f66SThierry Reding 	drm_encoder_init(drm, &sor->output.encoder, &tegra_sor_encoder_funcs,
14846fad8f66SThierry Reding 			 DRM_MODE_ENCODER_TMDS);
14856fad8f66SThierry Reding 	drm_encoder_helper_add(&sor->output.encoder,
14866fad8f66SThierry Reding 			       &tegra_sor_encoder_helper_funcs);
14876fad8f66SThierry Reding 
14886fad8f66SThierry Reding 	drm_mode_connector_attach_encoder(&sor->output.connector,
14896fad8f66SThierry Reding 					  &sor->output.encoder);
14906fad8f66SThierry Reding 	drm_connector_register(&sor->output.connector);
14916fad8f66SThierry Reding 
1492ea130b24SThierry Reding 	err = tegra_output_init(drm, &sor->output);
1493ea130b24SThierry Reding 	if (err < 0) {
1494ea130b24SThierry Reding 		dev_err(client->dev, "failed to initialize output: %d\n", err);
1495ea130b24SThierry Reding 		return err;
1496ea130b24SThierry Reding 	}
14976fad8f66SThierry Reding 
1498ea130b24SThierry Reding 	sor->output.encoder.possible_crtcs = 0x3;
14996b6b6042SThierry Reding 
1500a82752e1SThierry Reding 	if (IS_ENABLED(CONFIG_DEBUG_FS)) {
15011b0c7b48SThierry Reding 		err = tegra_sor_debugfs_init(sor, drm->primary);
1502a82752e1SThierry Reding 		if (err < 0)
1503a82752e1SThierry Reding 			dev_err(sor->dev, "debugfs setup failed: %d\n", err);
1504a82752e1SThierry Reding 	}
1505a82752e1SThierry Reding 
15066b6b6042SThierry Reding 	if (sor->dpaux) {
15076b6b6042SThierry Reding 		err = tegra_dpaux_attach(sor->dpaux, &sor->output);
15086b6b6042SThierry Reding 		if (err < 0) {
15096b6b6042SThierry Reding 			dev_err(sor->dev, "failed to attach DP: %d\n", err);
15106b6b6042SThierry Reding 			return err;
15116b6b6042SThierry Reding 		}
15126b6b6042SThierry Reding 	}
15136b6b6042SThierry Reding 
1514535a65dbSTomeu Vizoso 	/*
1515535a65dbSTomeu Vizoso 	 * XXX: Remove this reset once proper hand-over from firmware to
1516535a65dbSTomeu Vizoso 	 * kernel is possible.
1517535a65dbSTomeu Vizoso 	 */
1518535a65dbSTomeu Vizoso 	err = reset_control_assert(sor->rst);
1519535a65dbSTomeu Vizoso 	if (err < 0) {
1520535a65dbSTomeu Vizoso 		dev_err(sor->dev, "failed to assert SOR reset: %d\n", err);
1521535a65dbSTomeu Vizoso 		return err;
1522535a65dbSTomeu Vizoso 	}
1523535a65dbSTomeu Vizoso 
15246fad8f66SThierry Reding 	err = clk_prepare_enable(sor->clk);
15256fad8f66SThierry Reding 	if (err < 0) {
15266fad8f66SThierry Reding 		dev_err(sor->dev, "failed to enable clock: %d\n", err);
15276fad8f66SThierry Reding 		return err;
15286fad8f66SThierry Reding 	}
15296fad8f66SThierry Reding 
1530535a65dbSTomeu Vizoso 	usleep_range(1000, 3000);
1531535a65dbSTomeu Vizoso 
1532535a65dbSTomeu Vizoso 	err = reset_control_deassert(sor->rst);
1533535a65dbSTomeu Vizoso 	if (err < 0) {
1534535a65dbSTomeu Vizoso 		dev_err(sor->dev, "failed to deassert SOR reset: %d\n", err);
1535535a65dbSTomeu Vizoso 		return err;
1536535a65dbSTomeu Vizoso 	}
1537535a65dbSTomeu Vizoso 
15386fad8f66SThierry Reding 	err = clk_prepare_enable(sor->clk_safe);
15396fad8f66SThierry Reding 	if (err < 0)
15406fad8f66SThierry Reding 		return err;
15416fad8f66SThierry Reding 
15426fad8f66SThierry Reding 	err = clk_prepare_enable(sor->clk_dp);
15436fad8f66SThierry Reding 	if (err < 0)
15446fad8f66SThierry Reding 		return err;
15456fad8f66SThierry Reding 
15466b6b6042SThierry Reding 	return 0;
15476b6b6042SThierry Reding }
15486b6b6042SThierry Reding 
15496b6b6042SThierry Reding static int tegra_sor_exit(struct host1x_client *client)
15506b6b6042SThierry Reding {
15516b6b6042SThierry Reding 	struct tegra_sor *sor = host1x_client_to_sor(client);
15526b6b6042SThierry Reding 	int err;
15536b6b6042SThierry Reding 
1554328ec69eSThierry Reding 	tegra_output_exit(&sor->output);
1555328ec69eSThierry Reding 
15566b6b6042SThierry Reding 	if (sor->dpaux) {
15576b6b6042SThierry Reding 		err = tegra_dpaux_detach(sor->dpaux);
15586b6b6042SThierry Reding 		if (err < 0) {
15596b6b6042SThierry Reding 			dev_err(sor->dev, "failed to detach DP: %d\n", err);
15606b6b6042SThierry Reding 			return err;
15616b6b6042SThierry Reding 		}
15626b6b6042SThierry Reding 	}
15636b6b6042SThierry Reding 
15646fad8f66SThierry Reding 	clk_disable_unprepare(sor->clk_safe);
15656fad8f66SThierry Reding 	clk_disable_unprepare(sor->clk_dp);
15666fad8f66SThierry Reding 	clk_disable_unprepare(sor->clk);
15676fad8f66SThierry Reding 
15684009c224SThierry Reding 	if (IS_ENABLED(CONFIG_DEBUG_FS))
15694009c224SThierry Reding 		tegra_sor_debugfs_exit(sor);
1570a82752e1SThierry Reding 
15716b6b6042SThierry Reding 	return 0;
15726b6b6042SThierry Reding }
15736b6b6042SThierry Reding 
15746b6b6042SThierry Reding static const struct host1x_client_ops sor_client_ops = {
15756b6b6042SThierry Reding 	.init = tegra_sor_init,
15766b6b6042SThierry Reding 	.exit = tegra_sor_exit,
15776b6b6042SThierry Reding };
15786b6b6042SThierry Reding 
15796b6b6042SThierry Reding static int tegra_sor_probe(struct platform_device *pdev)
15806b6b6042SThierry Reding {
15816b6b6042SThierry Reding 	struct device_node *np;
15826b6b6042SThierry Reding 	struct tegra_sor *sor;
15836b6b6042SThierry Reding 	struct resource *regs;
15846b6b6042SThierry Reding 	int err;
15856b6b6042SThierry Reding 
15866b6b6042SThierry Reding 	sor = devm_kzalloc(&pdev->dev, sizeof(*sor), GFP_KERNEL);
15876b6b6042SThierry Reding 	if (!sor)
15886b6b6042SThierry Reding 		return -ENOMEM;
15896b6b6042SThierry Reding 
15906b6b6042SThierry Reding 	sor->output.dev = sor->dev = &pdev->dev;
15916b6b6042SThierry Reding 
15926b6b6042SThierry Reding 	np = of_parse_phandle(pdev->dev.of_node, "nvidia,dpaux", 0);
15936b6b6042SThierry Reding 	if (np) {
15946b6b6042SThierry Reding 		sor->dpaux = tegra_dpaux_find_by_of_node(np);
15956b6b6042SThierry Reding 		of_node_put(np);
15966b6b6042SThierry Reding 
15976b6b6042SThierry Reding 		if (!sor->dpaux)
15986b6b6042SThierry Reding 			return -EPROBE_DEFER;
15996b6b6042SThierry Reding 	}
16006b6b6042SThierry Reding 
16016b6b6042SThierry Reding 	err = tegra_output_probe(&sor->output);
16026b6b6042SThierry Reding 	if (err < 0)
16036b6b6042SThierry Reding 		return err;
16046b6b6042SThierry Reding 
16056b6b6042SThierry Reding 	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
16066b6b6042SThierry Reding 	sor->regs = devm_ioremap_resource(&pdev->dev, regs);
16076b6b6042SThierry Reding 	if (IS_ERR(sor->regs))
16086b6b6042SThierry Reding 		return PTR_ERR(sor->regs);
16096b6b6042SThierry Reding 
16106b6b6042SThierry Reding 	sor->rst = devm_reset_control_get(&pdev->dev, "sor");
16116b6b6042SThierry Reding 	if (IS_ERR(sor->rst))
16126b6b6042SThierry Reding 		return PTR_ERR(sor->rst);
16136b6b6042SThierry Reding 
16146b6b6042SThierry Reding 	sor->clk = devm_clk_get(&pdev->dev, NULL);
16156b6b6042SThierry Reding 	if (IS_ERR(sor->clk))
16166b6b6042SThierry Reding 		return PTR_ERR(sor->clk);
16176b6b6042SThierry Reding 
16186b6b6042SThierry Reding 	sor->clk_parent = devm_clk_get(&pdev->dev, "parent");
16196b6b6042SThierry Reding 	if (IS_ERR(sor->clk_parent))
16206b6b6042SThierry Reding 		return PTR_ERR(sor->clk_parent);
16216b6b6042SThierry Reding 
16226b6b6042SThierry Reding 	sor->clk_safe = devm_clk_get(&pdev->dev, "safe");
16236b6b6042SThierry Reding 	if (IS_ERR(sor->clk_safe))
16246b6b6042SThierry Reding 		return PTR_ERR(sor->clk_safe);
16256b6b6042SThierry Reding 
16266b6b6042SThierry Reding 	sor->clk_dp = devm_clk_get(&pdev->dev, "dp");
16276b6b6042SThierry Reding 	if (IS_ERR(sor->clk_dp))
16286b6b6042SThierry Reding 		return PTR_ERR(sor->clk_dp);
16296b6b6042SThierry Reding 
16306b6b6042SThierry Reding 	INIT_LIST_HEAD(&sor->client.list);
16316b6b6042SThierry Reding 	sor->client.ops = &sor_client_ops;
16326b6b6042SThierry Reding 	sor->client.dev = &pdev->dev;
16336b6b6042SThierry Reding 
163486f5c52dSThierry Reding 	mutex_init(&sor->lock);
163586f5c52dSThierry Reding 
16366b6b6042SThierry Reding 	err = host1x_client_register(&sor->client);
16376b6b6042SThierry Reding 	if (err < 0) {
16386b6b6042SThierry Reding 		dev_err(&pdev->dev, "failed to register host1x client: %d\n",
16396b6b6042SThierry Reding 			err);
16406b6b6042SThierry Reding 		return err;
16416b6b6042SThierry Reding 	}
16426b6b6042SThierry Reding 
16436b6b6042SThierry Reding 	platform_set_drvdata(pdev, sor);
16446b6b6042SThierry Reding 
16456b6b6042SThierry Reding 	return 0;
16466b6b6042SThierry Reding }
16476b6b6042SThierry Reding 
16486b6b6042SThierry Reding static int tegra_sor_remove(struct platform_device *pdev)
16496b6b6042SThierry Reding {
16506b6b6042SThierry Reding 	struct tegra_sor *sor = platform_get_drvdata(pdev);
16516b6b6042SThierry Reding 	int err;
16526b6b6042SThierry Reding 
16536b6b6042SThierry Reding 	err = host1x_client_unregister(&sor->client);
16546b6b6042SThierry Reding 	if (err < 0) {
16556b6b6042SThierry Reding 		dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
16566b6b6042SThierry Reding 			err);
16576b6b6042SThierry Reding 		return err;
16586b6b6042SThierry Reding 	}
16596b6b6042SThierry Reding 
1660328ec69eSThierry Reding 	tegra_output_remove(&sor->output);
16616b6b6042SThierry Reding 
16626b6b6042SThierry Reding 	return 0;
16636b6b6042SThierry Reding }
16646b6b6042SThierry Reding 
16656b6b6042SThierry Reding static const struct of_device_id tegra_sor_of_match[] = {
16666b6b6042SThierry Reding 	{ .compatible = "nvidia,tegra124-sor", },
16676b6b6042SThierry Reding 	{ },
16686b6b6042SThierry Reding };
1669ef70728cSStephen Warren MODULE_DEVICE_TABLE(of, tegra_sor_of_match);
16706b6b6042SThierry Reding 
16716b6b6042SThierry Reding struct platform_driver tegra_sor_driver = {
16726b6b6042SThierry Reding 	.driver = {
16736b6b6042SThierry Reding 		.name = "tegra-sor",
16746b6b6042SThierry Reding 		.of_match_table = tegra_sor_of_match,
16756b6b6042SThierry Reding 	},
16766b6b6042SThierry Reding 	.probe = tegra_sor_probe,
16776b6b6042SThierry Reding 	.remove = tegra_sor_remove,
16786b6b6042SThierry Reding };
1679