xref: /openbmc/linux/drivers/gpu/drm/tegra/sor.c (revision 880cee0b7ff379ebcf1f3d839fa59d1bcd726797)
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>
10b299221cSThierry Reding #include <linux/clk-provider.h>
11a82752e1SThierry Reding #include <linux/debugfs.h>
126fad8f66SThierry Reding #include <linux/gpio.h>
136b6b6042SThierry Reding #include <linux/io.h>
14459cc2c6SThierry Reding #include <linux/of_device.h>
156b6b6042SThierry Reding #include <linux/platform_device.h>
16aaff8bd2SThierry Reding #include <linux/pm_runtime.h>
17459cc2c6SThierry Reding #include <linux/regulator/consumer.h>
186b6b6042SThierry Reding #include <linux/reset.h>
19306a7f91SThierry Reding 
207232398aSThierry Reding #include <soc/tegra/pmc.h>
216b6b6042SThierry Reding 
224aa3df71SThierry Reding #include <drm/drm_atomic_helper.h>
236b6b6042SThierry Reding #include <drm/drm_dp_helper.h>
246fad8f66SThierry Reding #include <drm/drm_panel.h>
256b6b6042SThierry Reding 
266b6b6042SThierry Reding #include "dc.h"
276b6b6042SThierry Reding #include "drm.h"
286b6b6042SThierry Reding #include "sor.h"
29932f6529SThierry Reding #include "trace.h"
306b6b6042SThierry Reding 
31459cc2c6SThierry Reding #define SOR_REKEY 0x38
32459cc2c6SThierry Reding 
33459cc2c6SThierry Reding struct tegra_sor_hdmi_settings {
34459cc2c6SThierry Reding 	unsigned long frequency;
35459cc2c6SThierry Reding 
36459cc2c6SThierry Reding 	u8 vcocap;
37459cc2c6SThierry Reding 	u8 ichpmp;
38459cc2c6SThierry Reding 	u8 loadadj;
39459cc2c6SThierry Reding 	u8 termadj;
40459cc2c6SThierry Reding 	u8 tx_pu;
41459cc2c6SThierry Reding 	u8 bg_vref;
42459cc2c6SThierry Reding 
43459cc2c6SThierry Reding 	u8 drive_current[4];
44459cc2c6SThierry Reding 	u8 preemphasis[4];
45459cc2c6SThierry Reding };
46459cc2c6SThierry Reding 
47459cc2c6SThierry Reding #if 1
48459cc2c6SThierry Reding static const struct tegra_sor_hdmi_settings tegra210_sor_hdmi_defaults[] = {
49459cc2c6SThierry Reding 	{
50459cc2c6SThierry Reding 		.frequency = 54000000,
51459cc2c6SThierry Reding 		.vcocap = 0x0,
52459cc2c6SThierry Reding 		.ichpmp = 0x1,
53459cc2c6SThierry Reding 		.loadadj = 0x3,
54459cc2c6SThierry Reding 		.termadj = 0x9,
55459cc2c6SThierry Reding 		.tx_pu = 0x10,
56459cc2c6SThierry Reding 		.bg_vref = 0x8,
57459cc2c6SThierry Reding 		.drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
58459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
59459cc2c6SThierry Reding 	}, {
60459cc2c6SThierry Reding 		.frequency = 75000000,
61459cc2c6SThierry Reding 		.vcocap = 0x3,
62459cc2c6SThierry Reding 		.ichpmp = 0x1,
63459cc2c6SThierry Reding 		.loadadj = 0x3,
64459cc2c6SThierry Reding 		.termadj = 0x9,
65459cc2c6SThierry Reding 		.tx_pu = 0x40,
66459cc2c6SThierry Reding 		.bg_vref = 0x8,
67459cc2c6SThierry Reding 		.drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
68459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
69459cc2c6SThierry Reding 	}, {
70459cc2c6SThierry Reding 		.frequency = 150000000,
71459cc2c6SThierry Reding 		.vcocap = 0x3,
72459cc2c6SThierry Reding 		.ichpmp = 0x1,
73459cc2c6SThierry Reding 		.loadadj = 0x3,
74459cc2c6SThierry Reding 		.termadj = 0x9,
75459cc2c6SThierry Reding 		.tx_pu = 0x66,
76459cc2c6SThierry Reding 		.bg_vref = 0x8,
77459cc2c6SThierry Reding 		.drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
78459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
79459cc2c6SThierry Reding 	}, {
80459cc2c6SThierry Reding 		.frequency = 300000000,
81459cc2c6SThierry Reding 		.vcocap = 0x3,
82459cc2c6SThierry Reding 		.ichpmp = 0x1,
83459cc2c6SThierry Reding 		.loadadj = 0x3,
84459cc2c6SThierry Reding 		.termadj = 0x9,
85459cc2c6SThierry Reding 		.tx_pu = 0x66,
86459cc2c6SThierry Reding 		.bg_vref = 0xa,
87459cc2c6SThierry Reding 		.drive_current = { 0x33, 0x3f, 0x3f, 0x3f },
88459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x17, 0x17, 0x17 },
89459cc2c6SThierry Reding 	}, {
90459cc2c6SThierry Reding 		.frequency = 600000000,
91459cc2c6SThierry Reding 		.vcocap = 0x3,
92459cc2c6SThierry Reding 		.ichpmp = 0x1,
93459cc2c6SThierry Reding 		.loadadj = 0x3,
94459cc2c6SThierry Reding 		.termadj = 0x9,
95459cc2c6SThierry Reding 		.tx_pu = 0x66,
96459cc2c6SThierry Reding 		.bg_vref = 0x8,
97459cc2c6SThierry Reding 		.drive_current = { 0x33, 0x3f, 0x3f, 0x3f },
98459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
99459cc2c6SThierry Reding 	},
100459cc2c6SThierry Reding };
101459cc2c6SThierry Reding #else
102459cc2c6SThierry Reding static const struct tegra_sor_hdmi_settings tegra210_sor_hdmi_defaults[] = {
103459cc2c6SThierry Reding 	{
104459cc2c6SThierry Reding 		.frequency = 75000000,
105459cc2c6SThierry Reding 		.vcocap = 0x3,
106459cc2c6SThierry Reding 		.ichpmp = 0x1,
107459cc2c6SThierry Reding 		.loadadj = 0x3,
108459cc2c6SThierry Reding 		.termadj = 0x9,
109459cc2c6SThierry Reding 		.tx_pu = 0x40,
110459cc2c6SThierry Reding 		.bg_vref = 0x8,
111459cc2c6SThierry Reding 		.drive_current = { 0x29, 0x29, 0x29, 0x29 },
112459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
113459cc2c6SThierry Reding 	}, {
114459cc2c6SThierry Reding 		.frequency = 150000000,
115459cc2c6SThierry Reding 		.vcocap = 0x3,
116459cc2c6SThierry Reding 		.ichpmp = 0x1,
117459cc2c6SThierry Reding 		.loadadj = 0x3,
118459cc2c6SThierry Reding 		.termadj = 0x9,
119459cc2c6SThierry Reding 		.tx_pu = 0x66,
120459cc2c6SThierry Reding 		.bg_vref = 0x8,
121459cc2c6SThierry Reding 		.drive_current = { 0x30, 0x37, 0x37, 0x37 },
122459cc2c6SThierry Reding 		.preemphasis = { 0x01, 0x02, 0x02, 0x02 },
123459cc2c6SThierry Reding 	}, {
124459cc2c6SThierry Reding 		.frequency = 300000000,
125459cc2c6SThierry Reding 		.vcocap = 0x3,
126459cc2c6SThierry Reding 		.ichpmp = 0x6,
127459cc2c6SThierry Reding 		.loadadj = 0x3,
128459cc2c6SThierry Reding 		.termadj = 0x9,
129459cc2c6SThierry Reding 		.tx_pu = 0x66,
130459cc2c6SThierry Reding 		.bg_vref = 0xf,
131459cc2c6SThierry Reding 		.drive_current = { 0x30, 0x37, 0x37, 0x37 },
132459cc2c6SThierry Reding 		.preemphasis = { 0x10, 0x3e, 0x3e, 0x3e },
133459cc2c6SThierry Reding 	}, {
134459cc2c6SThierry Reding 		.frequency = 600000000,
135459cc2c6SThierry Reding 		.vcocap = 0x3,
136459cc2c6SThierry Reding 		.ichpmp = 0xa,
137459cc2c6SThierry Reding 		.loadadj = 0x3,
138459cc2c6SThierry Reding 		.termadj = 0xb,
139459cc2c6SThierry Reding 		.tx_pu = 0x66,
140459cc2c6SThierry Reding 		.bg_vref = 0xe,
141459cc2c6SThierry Reding 		.drive_current = { 0x35, 0x3e, 0x3e, 0x3e },
142459cc2c6SThierry Reding 		.preemphasis = { 0x02, 0x3f, 0x3f, 0x3f },
143459cc2c6SThierry Reding 	},
144459cc2c6SThierry Reding };
145459cc2c6SThierry Reding #endif
146459cc2c6SThierry Reding 
147*880cee0bSThierry Reding struct tegra_sor_regs {
148*880cee0bSThierry Reding 	unsigned int head_state0;
149*880cee0bSThierry Reding 	unsigned int head_state1;
150*880cee0bSThierry Reding 	unsigned int head_state2;
151*880cee0bSThierry Reding 	unsigned int head_state3;
152*880cee0bSThierry Reding 	unsigned int head_state4;
153*880cee0bSThierry Reding 	unsigned int head_state5;
154*880cee0bSThierry Reding 	unsigned int pll0;
155*880cee0bSThierry Reding 	unsigned int pll1;
156*880cee0bSThierry Reding 	unsigned int pll2;
157*880cee0bSThierry Reding 	unsigned int pll3;
158*880cee0bSThierry Reding 	unsigned int dp_padctl0;
159*880cee0bSThierry Reding 	unsigned int dp_padctl2;
160*880cee0bSThierry Reding };
161*880cee0bSThierry Reding 
162459cc2c6SThierry Reding struct tegra_sor_soc {
163459cc2c6SThierry Reding 	bool supports_edp;
164459cc2c6SThierry Reding 	bool supports_lvds;
165459cc2c6SThierry Reding 	bool supports_hdmi;
166459cc2c6SThierry Reding 	bool supports_dp;
167459cc2c6SThierry Reding 
168*880cee0bSThierry Reding 	const struct tegra_sor_regs *regs;
169*880cee0bSThierry Reding 
170459cc2c6SThierry Reding 	const struct tegra_sor_hdmi_settings *settings;
171459cc2c6SThierry Reding 	unsigned int num_settings;
17230b49435SThierry Reding 
17330b49435SThierry Reding 	const u8 *xbar_cfg;
174459cc2c6SThierry Reding };
175459cc2c6SThierry Reding 
176459cc2c6SThierry Reding struct tegra_sor;
177459cc2c6SThierry Reding 
178459cc2c6SThierry Reding struct tegra_sor_ops {
179459cc2c6SThierry Reding 	const char *name;
180459cc2c6SThierry Reding 	int (*probe)(struct tegra_sor *sor);
181459cc2c6SThierry Reding 	int (*remove)(struct tegra_sor *sor);
182459cc2c6SThierry Reding };
183459cc2c6SThierry Reding 
1846b6b6042SThierry Reding struct tegra_sor {
1856b6b6042SThierry Reding 	struct host1x_client client;
1866b6b6042SThierry Reding 	struct tegra_output output;
1876b6b6042SThierry Reding 	struct device *dev;
1886b6b6042SThierry Reding 
189459cc2c6SThierry Reding 	const struct tegra_sor_soc *soc;
1906b6b6042SThierry Reding 	void __iomem *regs;
1916b6b6042SThierry Reding 
1926b6b6042SThierry Reding 	struct reset_control *rst;
1936b6b6042SThierry Reding 	struct clk *clk_parent;
1946b6b6042SThierry Reding 	struct clk *clk_safe;
195e1335e2fSThierry Reding 	struct clk *clk_out;
196e1335e2fSThierry Reding 	struct clk *clk_pad;
1976b6b6042SThierry Reding 	struct clk *clk_dp;
1986b6b6042SThierry Reding 	struct clk *clk;
1996b6b6042SThierry Reding 
2009542c237SThierry Reding 	struct drm_dp_aux *aux;
2016b6b6042SThierry Reding 
202dab16336SThierry Reding 	struct drm_info_list *debugfs_files;
203459cc2c6SThierry Reding 
204459cc2c6SThierry Reding 	const struct tegra_sor_ops *ops;
205459cc2c6SThierry Reding 
206459cc2c6SThierry Reding 	/* for HDMI 2.0 */
207459cc2c6SThierry Reding 	struct tegra_sor_hdmi_settings *settings;
208459cc2c6SThierry Reding 	unsigned int num_settings;
209459cc2c6SThierry Reding 
210459cc2c6SThierry Reding 	struct regulator *avdd_io_supply;
211459cc2c6SThierry Reding 	struct regulator *vdd_pll_supply;
212459cc2c6SThierry Reding 	struct regulator *hdmi_supply;
2136b6b6042SThierry Reding };
2146b6b6042SThierry Reding 
215c31efa7aSThierry Reding struct tegra_sor_state {
216c31efa7aSThierry Reding 	struct drm_connector_state base;
217c31efa7aSThierry Reding 
218c31efa7aSThierry Reding 	unsigned int bpc;
219c31efa7aSThierry Reding };
220c31efa7aSThierry Reding 
221c31efa7aSThierry Reding static inline struct tegra_sor_state *
222c31efa7aSThierry Reding to_sor_state(struct drm_connector_state *state)
223c31efa7aSThierry Reding {
224c31efa7aSThierry Reding 	return container_of(state, struct tegra_sor_state, base);
225c31efa7aSThierry Reding }
226c31efa7aSThierry Reding 
22734fa183bSThierry Reding struct tegra_sor_config {
22834fa183bSThierry Reding 	u32 bits_per_pixel;
22934fa183bSThierry Reding 
23034fa183bSThierry Reding 	u32 active_polarity;
23134fa183bSThierry Reding 	u32 active_count;
23234fa183bSThierry Reding 	u32 tu_size;
23334fa183bSThierry Reding 	u32 active_frac;
23434fa183bSThierry Reding 	u32 watermark;
2357890b576SThierry Reding 
2367890b576SThierry Reding 	u32 hblank_symbols;
2377890b576SThierry Reding 	u32 vblank_symbols;
23834fa183bSThierry Reding };
23934fa183bSThierry Reding 
2406b6b6042SThierry Reding static inline struct tegra_sor *
2416b6b6042SThierry Reding host1x_client_to_sor(struct host1x_client *client)
2426b6b6042SThierry Reding {
2436b6b6042SThierry Reding 	return container_of(client, struct tegra_sor, client);
2446b6b6042SThierry Reding }
2456b6b6042SThierry Reding 
2466b6b6042SThierry Reding static inline struct tegra_sor *to_sor(struct tegra_output *output)
2476b6b6042SThierry Reding {
2486b6b6042SThierry Reding 	return container_of(output, struct tegra_sor, output);
2496b6b6042SThierry Reding }
2506b6b6042SThierry Reding 
2515c5f1301SThierry Reding static inline u32 tegra_sor_readl(struct tegra_sor *sor, unsigned int offset)
2526b6b6042SThierry Reding {
253932f6529SThierry Reding 	u32 value = readl(sor->regs + (offset << 2));
254932f6529SThierry Reding 
255932f6529SThierry Reding 	trace_sor_readl(sor->dev, offset, value);
256932f6529SThierry Reding 
257932f6529SThierry Reding 	return value;
2586b6b6042SThierry Reding }
2596b6b6042SThierry Reding 
26028fe2076SThierry Reding static inline void tegra_sor_writel(struct tegra_sor *sor, u32 value,
2615c5f1301SThierry Reding 				    unsigned int offset)
2626b6b6042SThierry Reding {
263932f6529SThierry Reding 	trace_sor_writel(sor->dev, offset, value);
2646b6b6042SThierry Reding 	writel(value, sor->regs + (offset << 2));
2656b6b6042SThierry Reding }
2666b6b6042SThierry Reding 
26725bb2cecSThierry Reding static int tegra_sor_set_parent_clock(struct tegra_sor *sor, struct clk *parent)
26825bb2cecSThierry Reding {
26925bb2cecSThierry Reding 	int err;
27025bb2cecSThierry Reding 
27125bb2cecSThierry Reding 	clk_disable_unprepare(sor->clk);
27225bb2cecSThierry Reding 
273e1335e2fSThierry Reding 	err = clk_set_parent(sor->clk_out, parent);
27425bb2cecSThierry Reding 	if (err < 0)
27525bb2cecSThierry Reding 		return err;
27625bb2cecSThierry Reding 
27725bb2cecSThierry Reding 	err = clk_prepare_enable(sor->clk);
27825bb2cecSThierry Reding 	if (err < 0)
27925bb2cecSThierry Reding 		return err;
28025bb2cecSThierry Reding 
28125bb2cecSThierry Reding 	return 0;
28225bb2cecSThierry Reding }
28325bb2cecSThierry Reding 
284e1335e2fSThierry Reding struct tegra_clk_sor_pad {
285b299221cSThierry Reding 	struct clk_hw hw;
286b299221cSThierry Reding 	struct tegra_sor *sor;
287b299221cSThierry Reding };
288b299221cSThierry Reding 
289e1335e2fSThierry Reding static inline struct tegra_clk_sor_pad *to_pad(struct clk_hw *hw)
290b299221cSThierry Reding {
291e1335e2fSThierry Reding 	return container_of(hw, struct tegra_clk_sor_pad, hw);
292b299221cSThierry Reding }
293b299221cSThierry Reding 
294e1335e2fSThierry Reding static const char * const tegra_clk_sor_pad_parents[] = {
295b299221cSThierry Reding 	"pll_d2_out0", "pll_dp"
296b299221cSThierry Reding };
297b299221cSThierry Reding 
298e1335e2fSThierry Reding static int tegra_clk_sor_pad_set_parent(struct clk_hw *hw, u8 index)
299b299221cSThierry Reding {
300e1335e2fSThierry Reding 	struct tegra_clk_sor_pad *pad = to_pad(hw);
301e1335e2fSThierry Reding 	struct tegra_sor *sor = pad->sor;
302b299221cSThierry Reding 	u32 value;
303b299221cSThierry Reding 
304b299221cSThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
305b299221cSThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
306b299221cSThierry Reding 
307b299221cSThierry Reding 	switch (index) {
308b299221cSThierry Reding 	case 0:
309b299221cSThierry Reding 		value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK;
310b299221cSThierry Reding 		break;
311b299221cSThierry Reding 
312b299221cSThierry Reding 	case 1:
313b299221cSThierry Reding 		value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
314b299221cSThierry Reding 		break;
315b299221cSThierry Reding 	}
316b299221cSThierry Reding 
317b299221cSThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
318b299221cSThierry Reding 
319b299221cSThierry Reding 	return 0;
320b299221cSThierry Reding }
321b299221cSThierry Reding 
322e1335e2fSThierry Reding static u8 tegra_clk_sor_pad_get_parent(struct clk_hw *hw)
323b299221cSThierry Reding {
324e1335e2fSThierry Reding 	struct tegra_clk_sor_pad *pad = to_pad(hw);
325e1335e2fSThierry Reding 	struct tegra_sor *sor = pad->sor;
326b299221cSThierry Reding 	u8 parent = U8_MAX;
327b299221cSThierry Reding 	u32 value;
328b299221cSThierry Reding 
329b299221cSThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
330b299221cSThierry Reding 
331b299221cSThierry Reding 	switch (value & SOR_CLK_CNTRL_DP_CLK_SEL_MASK) {
332b299221cSThierry Reding 	case SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK:
333b299221cSThierry Reding 	case SOR_CLK_CNTRL_DP_CLK_SEL_DIFF_PCLK:
334b299221cSThierry Reding 		parent = 0;
335b299221cSThierry Reding 		break;
336b299221cSThierry Reding 
337b299221cSThierry Reding 	case SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK:
338b299221cSThierry Reding 	case SOR_CLK_CNTRL_DP_CLK_SEL_DIFF_DPCLK:
339b299221cSThierry Reding 		parent = 1;
340b299221cSThierry Reding 		break;
341b299221cSThierry Reding 	}
342b299221cSThierry Reding 
343b299221cSThierry Reding 	return parent;
344b299221cSThierry Reding }
345b299221cSThierry Reding 
346e1335e2fSThierry Reding static const struct clk_ops tegra_clk_sor_pad_ops = {
347e1335e2fSThierry Reding 	.set_parent = tegra_clk_sor_pad_set_parent,
348e1335e2fSThierry Reding 	.get_parent = tegra_clk_sor_pad_get_parent,
349b299221cSThierry Reding };
350b299221cSThierry Reding 
351e1335e2fSThierry Reding static struct clk *tegra_clk_sor_pad_register(struct tegra_sor *sor,
352b299221cSThierry Reding 					      const char *name)
353b299221cSThierry Reding {
354e1335e2fSThierry Reding 	struct tegra_clk_sor_pad *pad;
355b299221cSThierry Reding 	struct clk_init_data init;
356b299221cSThierry Reding 	struct clk *clk;
357b299221cSThierry Reding 
358e1335e2fSThierry Reding 	pad = devm_kzalloc(sor->dev, sizeof(*pad), GFP_KERNEL);
359e1335e2fSThierry Reding 	if (!pad)
360b299221cSThierry Reding 		return ERR_PTR(-ENOMEM);
361b299221cSThierry Reding 
362e1335e2fSThierry Reding 	pad->sor = sor;
363b299221cSThierry Reding 
364b299221cSThierry Reding 	init.name = name;
365b299221cSThierry Reding 	init.flags = 0;
366e1335e2fSThierry Reding 	init.parent_names = tegra_clk_sor_pad_parents;
367e1335e2fSThierry Reding 	init.num_parents = ARRAY_SIZE(tegra_clk_sor_pad_parents);
368e1335e2fSThierry Reding 	init.ops = &tegra_clk_sor_pad_ops;
369b299221cSThierry Reding 
370e1335e2fSThierry Reding 	pad->hw.init = &init;
371b299221cSThierry Reding 
372e1335e2fSThierry Reding 	clk = devm_clk_register(sor->dev, &pad->hw);
373b299221cSThierry Reding 
374b299221cSThierry Reding 	return clk;
375b299221cSThierry Reding }
376b299221cSThierry Reding 
3776b6b6042SThierry Reding static int tegra_sor_dp_train_fast(struct tegra_sor *sor,
3786b6b6042SThierry Reding 				   struct drm_dp_link *link)
3796b6b6042SThierry Reding {
3806b6b6042SThierry Reding 	unsigned int i;
3816b6b6042SThierry Reding 	u8 pattern;
38228fe2076SThierry Reding 	u32 value;
3836b6b6042SThierry Reding 	int err;
3846b6b6042SThierry Reding 
3856b6b6042SThierry Reding 	/* setup lane parameters */
3866b6b6042SThierry Reding 	value = SOR_LANE_DRIVE_CURRENT_LANE3(0x40) |
3876b6b6042SThierry Reding 		SOR_LANE_DRIVE_CURRENT_LANE2(0x40) |
3886b6b6042SThierry Reding 		SOR_LANE_DRIVE_CURRENT_LANE1(0x40) |
3896b6b6042SThierry Reding 		SOR_LANE_DRIVE_CURRENT_LANE0(0x40);
390a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT0);
3916b6b6042SThierry Reding 
3926b6b6042SThierry Reding 	value = SOR_LANE_PREEMPHASIS_LANE3(0x0f) |
3936b6b6042SThierry Reding 		SOR_LANE_PREEMPHASIS_LANE2(0x0f) |
3946b6b6042SThierry Reding 		SOR_LANE_PREEMPHASIS_LANE1(0x0f) |
3956b6b6042SThierry Reding 		SOR_LANE_PREEMPHASIS_LANE0(0x0f);
396a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS0);
3976b6b6042SThierry Reding 
398a9a9e4fdSThierry Reding 	value = SOR_LANE_POSTCURSOR_LANE3(0x00) |
399a9a9e4fdSThierry Reding 		SOR_LANE_POSTCURSOR_LANE2(0x00) |
400a9a9e4fdSThierry Reding 		SOR_LANE_POSTCURSOR_LANE1(0x00) |
401a9a9e4fdSThierry Reding 		SOR_LANE_POSTCURSOR_LANE0(0x00);
402a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_POSTCURSOR0);
4036b6b6042SThierry Reding 
4046b6b6042SThierry Reding 	/* disable LVDS mode */
4056b6b6042SThierry Reding 	tegra_sor_writel(sor, 0, SOR_LVDS);
4066b6b6042SThierry Reding 
407*880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
4086b6b6042SThierry Reding 	value |= SOR_DP_PADCTL_TX_PU_ENABLE;
4096b6b6042SThierry Reding 	value &= ~SOR_DP_PADCTL_TX_PU_MASK;
4106b6b6042SThierry Reding 	value |= SOR_DP_PADCTL_TX_PU(2); /* XXX: don't hardcode? */
411*880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
4126b6b6042SThierry Reding 
413*880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
4146b6b6042SThierry Reding 	value |= SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
4156b6b6042SThierry Reding 		 SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0;
416*880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
4176b6b6042SThierry Reding 
4186b6b6042SThierry Reding 	usleep_range(10, 100);
4196b6b6042SThierry Reding 
420*880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
4216b6b6042SThierry Reding 	value &= ~(SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
4226b6b6042SThierry Reding 		   SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0);
423*880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
4246b6b6042SThierry Reding 
4259542c237SThierry Reding 	err = drm_dp_aux_prepare(sor->aux, DP_SET_ANSI_8B10B);
4266b6b6042SThierry Reding 	if (err < 0)
4276b6b6042SThierry Reding 		return err;
4286b6b6042SThierry Reding 
4296b6b6042SThierry Reding 	for (i = 0, value = 0; i < link->num_lanes; i++) {
4306b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
4316b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_NONE |
4326b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_TRAIN1;
4336b6b6042SThierry Reding 		value = (value << 8) | lane;
4346b6b6042SThierry Reding 	}
4356b6b6042SThierry Reding 
4366b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
4376b6b6042SThierry Reding 
4386b6b6042SThierry Reding 	pattern = DP_TRAINING_PATTERN_1;
4396b6b6042SThierry Reding 
4409542c237SThierry Reding 	err = drm_dp_aux_train(sor->aux, link, pattern);
4416b6b6042SThierry Reding 	if (err < 0)
4426b6b6042SThierry Reding 		return err;
4436b6b6042SThierry Reding 
444a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_SPARE0);
4456b6b6042SThierry Reding 	value |= SOR_DP_SPARE_SEQ_ENABLE;
4466b6b6042SThierry Reding 	value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
4476b6b6042SThierry Reding 	value |= SOR_DP_SPARE_MACRO_SOR_CLK;
448a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_SPARE0);
4496b6b6042SThierry Reding 
4506b6b6042SThierry Reding 	for (i = 0, value = 0; i < link->num_lanes; i++) {
4516b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
4526b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_NONE |
4536b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_TRAIN2;
4546b6b6042SThierry Reding 		value = (value << 8) | lane;
4556b6b6042SThierry Reding 	}
4566b6b6042SThierry Reding 
4576b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
4586b6b6042SThierry Reding 
4596b6b6042SThierry Reding 	pattern = DP_LINK_SCRAMBLING_DISABLE | DP_TRAINING_PATTERN_2;
4606b6b6042SThierry Reding 
4619542c237SThierry Reding 	err = drm_dp_aux_train(sor->aux, link, pattern);
4626b6b6042SThierry Reding 	if (err < 0)
4636b6b6042SThierry Reding 		return err;
4646b6b6042SThierry Reding 
4656b6b6042SThierry Reding 	for (i = 0, value = 0; i < link->num_lanes; i++) {
4666b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
4676b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_GALIOS |
4686b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_NONE;
4696b6b6042SThierry Reding 		value = (value << 8) | lane;
4706b6b6042SThierry Reding 	}
4716b6b6042SThierry Reding 
4726b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
4736b6b6042SThierry Reding 
4746b6b6042SThierry Reding 	pattern = DP_TRAINING_PATTERN_DISABLE;
4756b6b6042SThierry Reding 
4769542c237SThierry Reding 	err = drm_dp_aux_train(sor->aux, link, pattern);
4776b6b6042SThierry Reding 	if (err < 0)
4786b6b6042SThierry Reding 		return err;
4796b6b6042SThierry Reding 
4806b6b6042SThierry Reding 	return 0;
4816b6b6042SThierry Reding }
4826b6b6042SThierry Reding 
483459cc2c6SThierry Reding static void tegra_sor_dp_term_calibrate(struct tegra_sor *sor)
484459cc2c6SThierry Reding {
485459cc2c6SThierry Reding 	u32 mask = 0x08, adj = 0, value;
486459cc2c6SThierry Reding 
487459cc2c6SThierry Reding 	/* enable pad calibration logic */
488459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
489459cc2c6SThierry Reding 	value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
490459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
491459cc2c6SThierry Reding 
492459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL1);
493459cc2c6SThierry Reding 	value |= SOR_PLL1_TMDS_TERM;
494459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL1);
495459cc2c6SThierry Reding 
496459cc2c6SThierry Reding 	while (mask) {
497459cc2c6SThierry Reding 		adj |= mask;
498459cc2c6SThierry Reding 
499459cc2c6SThierry Reding 		value = tegra_sor_readl(sor, SOR_PLL1);
500459cc2c6SThierry Reding 		value &= ~SOR_PLL1_TMDS_TERMADJ_MASK;
501459cc2c6SThierry Reding 		value |= SOR_PLL1_TMDS_TERMADJ(adj);
502459cc2c6SThierry Reding 		tegra_sor_writel(sor, value, SOR_PLL1);
503459cc2c6SThierry Reding 
504459cc2c6SThierry Reding 		usleep_range(100, 200);
505459cc2c6SThierry Reding 
506459cc2c6SThierry Reding 		value = tegra_sor_readl(sor, SOR_PLL1);
507459cc2c6SThierry Reding 		if (value & SOR_PLL1_TERM_COMPOUT)
508459cc2c6SThierry Reding 			adj &= ~mask;
509459cc2c6SThierry Reding 
510459cc2c6SThierry Reding 		mask >>= 1;
511459cc2c6SThierry Reding 	}
512459cc2c6SThierry Reding 
513459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL1);
514459cc2c6SThierry Reding 	value &= ~SOR_PLL1_TMDS_TERMADJ_MASK;
515459cc2c6SThierry Reding 	value |= SOR_PLL1_TMDS_TERMADJ(adj);
516459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL1);
517459cc2c6SThierry Reding 
518459cc2c6SThierry Reding 	/* disable pad calibration logic */
519459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
520459cc2c6SThierry Reding 	value |= SOR_DP_PADCTL_PAD_CAL_PD;
521459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
522459cc2c6SThierry Reding }
523459cc2c6SThierry Reding 
5246b6b6042SThierry Reding static void tegra_sor_super_update(struct tegra_sor *sor)
5256b6b6042SThierry Reding {
526a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
527a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 1, SOR_SUPER_STATE0);
528a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
5296b6b6042SThierry Reding }
5306b6b6042SThierry Reding 
5316b6b6042SThierry Reding static void tegra_sor_update(struct tegra_sor *sor)
5326b6b6042SThierry Reding {
533a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE0);
534a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 1, SOR_STATE0);
535a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE0);
5366b6b6042SThierry Reding }
5376b6b6042SThierry Reding 
5386b6b6042SThierry Reding static int tegra_sor_setup_pwm(struct tegra_sor *sor, unsigned long timeout)
5396b6b6042SThierry Reding {
54028fe2076SThierry Reding 	u32 value;
5416b6b6042SThierry Reding 
5426b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWM_DIV);
5436b6b6042SThierry Reding 	value &= ~SOR_PWM_DIV_MASK;
5446b6b6042SThierry Reding 	value |= 0x400; /* period */
5456b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWM_DIV);
5466b6b6042SThierry Reding 
5476b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWM_CTL);
5486b6b6042SThierry Reding 	value &= ~SOR_PWM_CTL_DUTY_CYCLE_MASK;
5496b6b6042SThierry Reding 	value |= 0x400; /* duty cycle */
5506b6b6042SThierry Reding 	value &= ~SOR_PWM_CTL_CLK_SEL; /* clock source: PCLK */
5516b6b6042SThierry Reding 	value |= SOR_PWM_CTL_TRIGGER;
5526b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWM_CTL);
5536b6b6042SThierry Reding 
5546b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
5556b6b6042SThierry Reding 
5566b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
5576b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWM_CTL);
5586b6b6042SThierry Reding 		if ((value & SOR_PWM_CTL_TRIGGER) == 0)
5596b6b6042SThierry Reding 			return 0;
5606b6b6042SThierry Reding 
5616b6b6042SThierry Reding 		usleep_range(25, 100);
5626b6b6042SThierry Reding 	}
5636b6b6042SThierry Reding 
5646b6b6042SThierry Reding 	return -ETIMEDOUT;
5656b6b6042SThierry Reding }
5666b6b6042SThierry Reding 
5676b6b6042SThierry Reding static int tegra_sor_attach(struct tegra_sor *sor)
5686b6b6042SThierry Reding {
5696b6b6042SThierry Reding 	unsigned long value, timeout;
5706b6b6042SThierry Reding 
5716b6b6042SThierry Reding 	/* wake up in normal mode */
572a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
5736b6b6042SThierry Reding 	value |= SOR_SUPER_STATE_HEAD_MODE_AWAKE;
5746b6b6042SThierry Reding 	value |= SOR_SUPER_STATE_MODE_NORMAL;
575a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
5766b6b6042SThierry Reding 	tegra_sor_super_update(sor);
5776b6b6042SThierry Reding 
5786b6b6042SThierry Reding 	/* attach */
579a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
5806b6b6042SThierry Reding 	value |= SOR_SUPER_STATE_ATTACHED;
581a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
5826b6b6042SThierry Reding 	tegra_sor_super_update(sor);
5836b6b6042SThierry Reding 
5846b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
5856b6b6042SThierry Reding 
5866b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
5876b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_TEST);
5886b6b6042SThierry Reding 		if ((value & SOR_TEST_ATTACHED) != 0)
5896b6b6042SThierry Reding 			return 0;
5906b6b6042SThierry Reding 
5916b6b6042SThierry Reding 		usleep_range(25, 100);
5926b6b6042SThierry Reding 	}
5936b6b6042SThierry Reding 
5946b6b6042SThierry Reding 	return -ETIMEDOUT;
5956b6b6042SThierry Reding }
5966b6b6042SThierry Reding 
5976b6b6042SThierry Reding static int tegra_sor_wakeup(struct tegra_sor *sor)
5986b6b6042SThierry Reding {
5996b6b6042SThierry Reding 	unsigned long value, timeout;
6006b6b6042SThierry Reding 
6016b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
6026b6b6042SThierry Reding 
6036b6b6042SThierry Reding 	/* wait for head to wake up */
6046b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
6056b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_TEST);
6066b6b6042SThierry Reding 		value &= SOR_TEST_HEAD_MODE_MASK;
6076b6b6042SThierry Reding 
6086b6b6042SThierry Reding 		if (value == SOR_TEST_HEAD_MODE_AWAKE)
6096b6b6042SThierry Reding 			return 0;
6106b6b6042SThierry Reding 
6116b6b6042SThierry Reding 		usleep_range(25, 100);
6126b6b6042SThierry Reding 	}
6136b6b6042SThierry Reding 
6146b6b6042SThierry Reding 	return -ETIMEDOUT;
6156b6b6042SThierry Reding }
6166b6b6042SThierry Reding 
6176b6b6042SThierry Reding static int tegra_sor_power_up(struct tegra_sor *sor, unsigned long timeout)
6186b6b6042SThierry Reding {
61928fe2076SThierry Reding 	u32 value;
6206b6b6042SThierry Reding 
6216b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWR);
6226b6b6042SThierry Reding 	value |= SOR_PWR_TRIGGER | SOR_PWR_NORMAL_STATE_PU;
6236b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWR);
6246b6b6042SThierry Reding 
6256b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
6266b6b6042SThierry Reding 
6276b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
6286b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWR);
6296b6b6042SThierry Reding 		if ((value & SOR_PWR_TRIGGER) == 0)
6306b6b6042SThierry Reding 			return 0;
6316b6b6042SThierry Reding 
6326b6b6042SThierry Reding 		usleep_range(25, 100);
6336b6b6042SThierry Reding 	}
6346b6b6042SThierry Reding 
6356b6b6042SThierry Reding 	return -ETIMEDOUT;
6366b6b6042SThierry Reding }
6376b6b6042SThierry Reding 
63834fa183bSThierry Reding struct tegra_sor_params {
63934fa183bSThierry Reding 	/* number of link clocks per line */
64034fa183bSThierry Reding 	unsigned int num_clocks;
64134fa183bSThierry Reding 	/* ratio between input and output */
64234fa183bSThierry Reding 	u64 ratio;
64334fa183bSThierry Reding 	/* precision factor */
64434fa183bSThierry Reding 	u64 precision;
64534fa183bSThierry Reding 
64634fa183bSThierry Reding 	unsigned int active_polarity;
64734fa183bSThierry Reding 	unsigned int active_count;
64834fa183bSThierry Reding 	unsigned int active_frac;
64934fa183bSThierry Reding 	unsigned int tu_size;
65034fa183bSThierry Reding 	unsigned int error;
65134fa183bSThierry Reding };
65234fa183bSThierry Reding 
65334fa183bSThierry Reding static int tegra_sor_compute_params(struct tegra_sor *sor,
65434fa183bSThierry Reding 				    struct tegra_sor_params *params,
65534fa183bSThierry Reding 				    unsigned int tu_size)
65634fa183bSThierry Reding {
65734fa183bSThierry Reding 	u64 active_sym, active_count, frac, approx;
65834fa183bSThierry Reding 	u32 active_polarity, active_frac = 0;
65934fa183bSThierry Reding 	const u64 f = params->precision;
66034fa183bSThierry Reding 	s64 error;
66134fa183bSThierry Reding 
66234fa183bSThierry Reding 	active_sym = params->ratio * tu_size;
66334fa183bSThierry Reding 	active_count = div_u64(active_sym, f) * f;
66434fa183bSThierry Reding 	frac = active_sym - active_count;
66534fa183bSThierry Reding 
66634fa183bSThierry Reding 	/* fraction < 0.5 */
66734fa183bSThierry Reding 	if (frac >= (f / 2)) {
66834fa183bSThierry Reding 		active_polarity = 1;
66934fa183bSThierry Reding 		frac = f - frac;
67034fa183bSThierry Reding 	} else {
67134fa183bSThierry Reding 		active_polarity = 0;
67234fa183bSThierry Reding 	}
67334fa183bSThierry Reding 
67434fa183bSThierry Reding 	if (frac != 0) {
67534fa183bSThierry Reding 		frac = div_u64(f * f,  frac); /* 1/fraction */
67634fa183bSThierry Reding 		if (frac <= (15 * f)) {
67734fa183bSThierry Reding 			active_frac = div_u64(frac, f);
67834fa183bSThierry Reding 
67934fa183bSThierry Reding 			/* round up */
68034fa183bSThierry Reding 			if (active_polarity)
68134fa183bSThierry Reding 				active_frac++;
68234fa183bSThierry Reding 		} else {
68334fa183bSThierry Reding 			active_frac = active_polarity ? 1 : 15;
68434fa183bSThierry Reding 		}
68534fa183bSThierry Reding 	}
68634fa183bSThierry Reding 
68734fa183bSThierry Reding 	if (active_frac == 1)
68834fa183bSThierry Reding 		active_polarity = 0;
68934fa183bSThierry Reding 
69034fa183bSThierry Reding 	if (active_polarity == 1) {
69134fa183bSThierry Reding 		if (active_frac) {
69234fa183bSThierry Reding 			approx = active_count + (active_frac * (f - 1)) * f;
69334fa183bSThierry Reding 			approx = div_u64(approx, active_frac * f);
69434fa183bSThierry Reding 		} else {
69534fa183bSThierry Reding 			approx = active_count + f;
69634fa183bSThierry Reding 		}
69734fa183bSThierry Reding 	} else {
69834fa183bSThierry Reding 		if (active_frac)
69934fa183bSThierry Reding 			approx = active_count + div_u64(f, active_frac);
70034fa183bSThierry Reding 		else
70134fa183bSThierry Reding 			approx = active_count;
70234fa183bSThierry Reding 	}
70334fa183bSThierry Reding 
70434fa183bSThierry Reding 	error = div_s64(active_sym - approx, tu_size);
70534fa183bSThierry Reding 	error *= params->num_clocks;
70634fa183bSThierry Reding 
70779211c8eSAndrew Morton 	if (error <= 0 && abs(error) < params->error) {
70834fa183bSThierry Reding 		params->active_count = div_u64(active_count, f);
70934fa183bSThierry Reding 		params->active_polarity = active_polarity;
71034fa183bSThierry Reding 		params->active_frac = active_frac;
71179211c8eSAndrew Morton 		params->error = abs(error);
71234fa183bSThierry Reding 		params->tu_size = tu_size;
71334fa183bSThierry Reding 
71434fa183bSThierry Reding 		if (error == 0)
71534fa183bSThierry Reding 			return true;
71634fa183bSThierry Reding 	}
71734fa183bSThierry Reding 
71834fa183bSThierry Reding 	return false;
71934fa183bSThierry Reding }
72034fa183bSThierry Reding 
721a198359eSThierry Reding static int tegra_sor_compute_config(struct tegra_sor *sor,
72280444495SThierry Reding 				    const struct drm_display_mode *mode,
72334fa183bSThierry Reding 				    struct tegra_sor_config *config,
72434fa183bSThierry Reding 				    struct drm_dp_link *link)
72534fa183bSThierry Reding {
72634fa183bSThierry Reding 	const u64 f = 100000, link_rate = link->rate * 1000;
72734fa183bSThierry Reding 	const u64 pclk = mode->clock * 1000;
7287890b576SThierry Reding 	u64 input, output, watermark, num;
72934fa183bSThierry Reding 	struct tegra_sor_params params;
73034fa183bSThierry Reding 	u32 num_syms_per_line;
73134fa183bSThierry Reding 	unsigned int i;
73234fa183bSThierry Reding 
73334fa183bSThierry Reding 	if (!link_rate || !link->num_lanes || !pclk || !config->bits_per_pixel)
73434fa183bSThierry Reding 		return -EINVAL;
73534fa183bSThierry Reding 
73634fa183bSThierry Reding 	output = link_rate * 8 * link->num_lanes;
73734fa183bSThierry Reding 	input = pclk * config->bits_per_pixel;
73834fa183bSThierry Reding 
73934fa183bSThierry Reding 	if (input >= output)
74034fa183bSThierry Reding 		return -ERANGE;
74134fa183bSThierry Reding 
74234fa183bSThierry Reding 	memset(&params, 0, sizeof(params));
74334fa183bSThierry Reding 	params.ratio = div64_u64(input * f, output);
74434fa183bSThierry Reding 	params.num_clocks = div_u64(link_rate * mode->hdisplay, pclk);
74534fa183bSThierry Reding 	params.precision = f;
74634fa183bSThierry Reding 	params.error = 64 * f;
74734fa183bSThierry Reding 	params.tu_size = 64;
74834fa183bSThierry Reding 
74934fa183bSThierry Reding 	for (i = params.tu_size; i >= 32; i--)
75034fa183bSThierry Reding 		if (tegra_sor_compute_params(sor, &params, i))
75134fa183bSThierry Reding 			break;
75234fa183bSThierry Reding 
75334fa183bSThierry Reding 	if (params.active_frac == 0) {
75434fa183bSThierry Reding 		config->active_polarity = 0;
75534fa183bSThierry Reding 		config->active_count = params.active_count;
75634fa183bSThierry Reding 
75734fa183bSThierry Reding 		if (!params.active_polarity)
75834fa183bSThierry Reding 			config->active_count--;
75934fa183bSThierry Reding 
76034fa183bSThierry Reding 		config->tu_size = params.tu_size;
76134fa183bSThierry Reding 		config->active_frac = 1;
76234fa183bSThierry Reding 	} else {
76334fa183bSThierry Reding 		config->active_polarity = params.active_polarity;
76434fa183bSThierry Reding 		config->active_count = params.active_count;
76534fa183bSThierry Reding 		config->active_frac = params.active_frac;
76634fa183bSThierry Reding 		config->tu_size = params.tu_size;
76734fa183bSThierry Reding 	}
76834fa183bSThierry Reding 
76934fa183bSThierry Reding 	dev_dbg(sor->dev,
77034fa183bSThierry Reding 		"polarity: %d active count: %d tu size: %d active frac: %d\n",
77134fa183bSThierry Reding 		config->active_polarity, config->active_count,
77234fa183bSThierry Reding 		config->tu_size, config->active_frac);
77334fa183bSThierry Reding 
77434fa183bSThierry Reding 	watermark = params.ratio * config->tu_size * (f - params.ratio);
77534fa183bSThierry Reding 	watermark = div_u64(watermark, f);
77634fa183bSThierry Reding 
77734fa183bSThierry Reding 	watermark = div_u64(watermark + params.error, f);
77834fa183bSThierry Reding 	config->watermark = watermark + (config->bits_per_pixel / 8) + 2;
77934fa183bSThierry Reding 	num_syms_per_line = (mode->hdisplay * config->bits_per_pixel) *
78034fa183bSThierry Reding 			    (link->num_lanes * 8);
78134fa183bSThierry Reding 
78234fa183bSThierry Reding 	if (config->watermark > 30) {
78334fa183bSThierry Reding 		config->watermark = 30;
78434fa183bSThierry Reding 		dev_err(sor->dev,
78534fa183bSThierry Reding 			"unable to compute TU size, forcing watermark to %u\n",
78634fa183bSThierry Reding 			config->watermark);
78734fa183bSThierry Reding 	} else if (config->watermark > num_syms_per_line) {
78834fa183bSThierry Reding 		config->watermark = num_syms_per_line;
78934fa183bSThierry Reding 		dev_err(sor->dev, "watermark too high, forcing to %u\n",
79034fa183bSThierry Reding 			config->watermark);
79134fa183bSThierry Reding 	}
79234fa183bSThierry Reding 
7937890b576SThierry Reding 	/* compute the number of symbols per horizontal blanking interval */
7947890b576SThierry Reding 	num = ((mode->htotal - mode->hdisplay) - 7) * link_rate;
7957890b576SThierry Reding 	config->hblank_symbols = div_u64(num, pclk);
7967890b576SThierry Reding 
7977890b576SThierry Reding 	if (link->capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
7987890b576SThierry Reding 		config->hblank_symbols -= 3;
7997890b576SThierry Reding 
8007890b576SThierry Reding 	config->hblank_symbols -= 12 / link->num_lanes;
8017890b576SThierry Reding 
8027890b576SThierry Reding 	/* compute the number of symbols per vertical blanking interval */
8037890b576SThierry Reding 	num = (mode->hdisplay - 25) * link_rate;
8047890b576SThierry Reding 	config->vblank_symbols = div_u64(num, pclk);
8057890b576SThierry Reding 	config->vblank_symbols -= 36 / link->num_lanes + 4;
8067890b576SThierry Reding 
8077890b576SThierry Reding 	dev_dbg(sor->dev, "blank symbols: H:%u V:%u\n", config->hblank_symbols,
8087890b576SThierry Reding 		config->vblank_symbols);
8097890b576SThierry Reding 
81034fa183bSThierry Reding 	return 0;
81134fa183bSThierry Reding }
81234fa183bSThierry Reding 
813402f6bcdSThierry Reding static void tegra_sor_apply_config(struct tegra_sor *sor,
814402f6bcdSThierry Reding 				   const struct tegra_sor_config *config)
815402f6bcdSThierry Reding {
816402f6bcdSThierry Reding 	u32 value;
817402f6bcdSThierry Reding 
818402f6bcdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
819402f6bcdSThierry Reding 	value &= ~SOR_DP_LINKCTL_TU_SIZE_MASK;
820402f6bcdSThierry Reding 	value |= SOR_DP_LINKCTL_TU_SIZE(config->tu_size);
821402f6bcdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
822402f6bcdSThierry Reding 
823402f6bcdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_CONFIG0);
824402f6bcdSThierry Reding 	value &= ~SOR_DP_CONFIG_WATERMARK_MASK;
825402f6bcdSThierry Reding 	value |= SOR_DP_CONFIG_WATERMARK(config->watermark);
826402f6bcdSThierry Reding 
827402f6bcdSThierry Reding 	value &= ~SOR_DP_CONFIG_ACTIVE_SYM_COUNT_MASK;
828402f6bcdSThierry Reding 	value |= SOR_DP_CONFIG_ACTIVE_SYM_COUNT(config->active_count);
829402f6bcdSThierry Reding 
830402f6bcdSThierry Reding 	value &= ~SOR_DP_CONFIG_ACTIVE_SYM_FRAC_MASK;
831402f6bcdSThierry Reding 	value |= SOR_DP_CONFIG_ACTIVE_SYM_FRAC(config->active_frac);
832402f6bcdSThierry Reding 
833402f6bcdSThierry Reding 	if (config->active_polarity)
834402f6bcdSThierry Reding 		value |= SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
835402f6bcdSThierry Reding 	else
836402f6bcdSThierry Reding 		value &= ~SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
837402f6bcdSThierry Reding 
838402f6bcdSThierry Reding 	value |= SOR_DP_CONFIG_ACTIVE_SYM_ENABLE;
839402f6bcdSThierry Reding 	value |= SOR_DP_CONFIG_DISPARITY_NEGATIVE;
840402f6bcdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_CONFIG0);
841402f6bcdSThierry Reding 
842402f6bcdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_AUDIO_HBLANK_SYMBOLS);
843402f6bcdSThierry Reding 	value &= ~SOR_DP_AUDIO_HBLANK_SYMBOLS_MASK;
844402f6bcdSThierry Reding 	value |= config->hblank_symbols & 0xffff;
845402f6bcdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_AUDIO_HBLANK_SYMBOLS);
846402f6bcdSThierry Reding 
847402f6bcdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_AUDIO_VBLANK_SYMBOLS);
848402f6bcdSThierry Reding 	value &= ~SOR_DP_AUDIO_VBLANK_SYMBOLS_MASK;
849402f6bcdSThierry Reding 	value |= config->vblank_symbols & 0xffff;
850402f6bcdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_AUDIO_VBLANK_SYMBOLS);
851402f6bcdSThierry Reding }
852402f6bcdSThierry Reding 
8532bd1dd39SThierry Reding static void tegra_sor_mode_set(struct tegra_sor *sor,
8542bd1dd39SThierry Reding 			       const struct drm_display_mode *mode,
855c31efa7aSThierry Reding 			       struct tegra_sor_state *state)
8562bd1dd39SThierry Reding {
8572bd1dd39SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(sor->output.encoder.crtc);
8582bd1dd39SThierry Reding 	unsigned int vbe, vse, hbe, hse, vbs, hbs;
8592bd1dd39SThierry Reding 	u32 value;
8602bd1dd39SThierry Reding 
8612bd1dd39SThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
8622bd1dd39SThierry Reding 	value &= ~SOR_STATE_ASY_PIXELDEPTH_MASK;
8632bd1dd39SThierry Reding 	value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
8642bd1dd39SThierry Reding 	value &= ~SOR_STATE_ASY_OWNER_MASK;
8652bd1dd39SThierry Reding 
8662bd1dd39SThierry Reding 	value |= SOR_STATE_ASY_CRC_MODE_COMPLETE |
8672bd1dd39SThierry Reding 		 SOR_STATE_ASY_OWNER(dc->pipe + 1);
8682bd1dd39SThierry Reding 
8692bd1dd39SThierry Reding 	if (mode->flags & DRM_MODE_FLAG_PHSYNC)
8702bd1dd39SThierry Reding 		value &= ~SOR_STATE_ASY_HSYNCPOL;
8712bd1dd39SThierry Reding 
8722bd1dd39SThierry Reding 	if (mode->flags & DRM_MODE_FLAG_NHSYNC)
8732bd1dd39SThierry Reding 		value |= SOR_STATE_ASY_HSYNCPOL;
8742bd1dd39SThierry Reding 
8752bd1dd39SThierry Reding 	if (mode->flags & DRM_MODE_FLAG_PVSYNC)
8762bd1dd39SThierry Reding 		value &= ~SOR_STATE_ASY_VSYNCPOL;
8772bd1dd39SThierry Reding 
8782bd1dd39SThierry Reding 	if (mode->flags & DRM_MODE_FLAG_NVSYNC)
8792bd1dd39SThierry Reding 		value |= SOR_STATE_ASY_VSYNCPOL;
8802bd1dd39SThierry Reding 
881c31efa7aSThierry Reding 	switch (state->bpc) {
882c31efa7aSThierry Reding 	case 16:
883c31efa7aSThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_48_444;
884c31efa7aSThierry Reding 		break;
885c31efa7aSThierry Reding 
886c31efa7aSThierry Reding 	case 12:
887c31efa7aSThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_36_444;
888c31efa7aSThierry Reding 		break;
889c31efa7aSThierry Reding 
890c31efa7aSThierry Reding 	case 10:
891c31efa7aSThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_30_444;
892c31efa7aSThierry Reding 		break;
893c31efa7aSThierry Reding 
8942bd1dd39SThierry Reding 	case 8:
8952bd1dd39SThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
8962bd1dd39SThierry Reding 		break;
8972bd1dd39SThierry Reding 
8982bd1dd39SThierry Reding 	case 6:
8992bd1dd39SThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_18_444;
9002bd1dd39SThierry Reding 		break;
9012bd1dd39SThierry Reding 
9022bd1dd39SThierry Reding 	default:
903c31efa7aSThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
9042bd1dd39SThierry Reding 		break;
9052bd1dd39SThierry Reding 	}
9062bd1dd39SThierry Reding 
9072bd1dd39SThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
9082bd1dd39SThierry Reding 
9092bd1dd39SThierry Reding 	/*
9102bd1dd39SThierry Reding 	 * TODO: The video timing programming below doesn't seem to match the
9112bd1dd39SThierry Reding 	 * register definitions.
9122bd1dd39SThierry Reding 	 */
9132bd1dd39SThierry Reding 
9142bd1dd39SThierry Reding 	value = ((mode->vtotal & 0x7fff) << 16) | (mode->htotal & 0x7fff);
915*880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->head_state1 + dc->pipe);
9162bd1dd39SThierry Reding 
9172bd1dd39SThierry Reding 	/* sync end = sync width - 1 */
9182bd1dd39SThierry Reding 	vse = mode->vsync_end - mode->vsync_start - 1;
9192bd1dd39SThierry Reding 	hse = mode->hsync_end - mode->hsync_start - 1;
9202bd1dd39SThierry Reding 
9212bd1dd39SThierry Reding 	value = ((vse & 0x7fff) << 16) | (hse & 0x7fff);
922*880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->head_state2 + dc->pipe);
9232bd1dd39SThierry Reding 
9242bd1dd39SThierry Reding 	/* blank end = sync end + back porch */
9252bd1dd39SThierry Reding 	vbe = vse + (mode->vtotal - mode->vsync_end);
9262bd1dd39SThierry Reding 	hbe = hse + (mode->htotal - mode->hsync_end);
9272bd1dd39SThierry Reding 
9282bd1dd39SThierry Reding 	value = ((vbe & 0x7fff) << 16) | (hbe & 0x7fff);
929*880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->head_state3 + dc->pipe);
9302bd1dd39SThierry Reding 
9312bd1dd39SThierry Reding 	/* blank start = blank end + active */
9322bd1dd39SThierry Reding 	vbs = vbe + mode->vdisplay;
9332bd1dd39SThierry Reding 	hbs = hbe + mode->hdisplay;
9342bd1dd39SThierry Reding 
9352bd1dd39SThierry Reding 	value = ((vbs & 0x7fff) << 16) | (hbs & 0x7fff);
936*880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->head_state4 + dc->pipe);
9372bd1dd39SThierry Reding 
9382bd1dd39SThierry Reding 	/* XXX interlacing support */
939*880cee0bSThierry Reding 	tegra_sor_writel(sor, 0x001, sor->soc->regs->head_state5 + dc->pipe);
9402bd1dd39SThierry Reding }
9412bd1dd39SThierry Reding 
9426fad8f66SThierry Reding static int tegra_sor_detach(struct tegra_sor *sor)
9436b6b6042SThierry Reding {
9446fad8f66SThierry Reding 	unsigned long value, timeout;
9456fad8f66SThierry Reding 
9466fad8f66SThierry Reding 	/* switch to safe mode */
947a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
9486fad8f66SThierry Reding 	value &= ~SOR_SUPER_STATE_MODE_NORMAL;
949a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
9506fad8f66SThierry Reding 	tegra_sor_super_update(sor);
9516fad8f66SThierry Reding 
9526fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
9536fad8f66SThierry Reding 
9546fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
9556fad8f66SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWR);
9566fad8f66SThierry Reding 		if (value & SOR_PWR_MODE_SAFE)
9576fad8f66SThierry Reding 			break;
9586fad8f66SThierry Reding 	}
9596fad8f66SThierry Reding 
9606fad8f66SThierry Reding 	if ((value & SOR_PWR_MODE_SAFE) == 0)
9616fad8f66SThierry Reding 		return -ETIMEDOUT;
9626fad8f66SThierry Reding 
9636fad8f66SThierry Reding 	/* go to sleep */
964a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
9656fad8f66SThierry Reding 	value &= ~SOR_SUPER_STATE_HEAD_MODE_MASK;
966a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
9676fad8f66SThierry Reding 	tegra_sor_super_update(sor);
9686fad8f66SThierry Reding 
9696fad8f66SThierry Reding 	/* detach */
970a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
9716fad8f66SThierry Reding 	value &= ~SOR_SUPER_STATE_ATTACHED;
972a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
9736fad8f66SThierry Reding 	tegra_sor_super_update(sor);
9746fad8f66SThierry Reding 
9756fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
9766fad8f66SThierry Reding 
9776fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
9786fad8f66SThierry Reding 		value = tegra_sor_readl(sor, SOR_TEST);
9796fad8f66SThierry Reding 		if ((value & SOR_TEST_ATTACHED) == 0)
9806fad8f66SThierry Reding 			break;
9816fad8f66SThierry Reding 
9826fad8f66SThierry Reding 		usleep_range(25, 100);
9836fad8f66SThierry Reding 	}
9846fad8f66SThierry Reding 
9856fad8f66SThierry Reding 	if ((value & SOR_TEST_ATTACHED) != 0)
9866fad8f66SThierry Reding 		return -ETIMEDOUT;
9876fad8f66SThierry Reding 
9886fad8f66SThierry Reding 	return 0;
9896fad8f66SThierry Reding }
9906fad8f66SThierry Reding 
9916fad8f66SThierry Reding static int tegra_sor_power_down(struct tegra_sor *sor)
9926fad8f66SThierry Reding {
9936fad8f66SThierry Reding 	unsigned long value, timeout;
9946fad8f66SThierry Reding 	int err;
9956fad8f66SThierry Reding 
9966fad8f66SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWR);
9976fad8f66SThierry Reding 	value &= ~SOR_PWR_NORMAL_STATE_PU;
9986fad8f66SThierry Reding 	value |= SOR_PWR_TRIGGER;
9996fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWR);
10006fad8f66SThierry Reding 
10016fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
10026fad8f66SThierry Reding 
10036fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
10046fad8f66SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWR);
10056fad8f66SThierry Reding 		if ((value & SOR_PWR_TRIGGER) == 0)
10066fad8f66SThierry Reding 			return 0;
10076fad8f66SThierry Reding 
10086fad8f66SThierry Reding 		usleep_range(25, 100);
10096fad8f66SThierry Reding 	}
10106fad8f66SThierry Reding 
10116fad8f66SThierry Reding 	if ((value & SOR_PWR_TRIGGER) != 0)
10126fad8f66SThierry Reding 		return -ETIMEDOUT;
10136fad8f66SThierry Reding 
101425bb2cecSThierry Reding 	/* switch to safe parent clock */
101525bb2cecSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
1016e1335e2fSThierry Reding 	if (err < 0) {
10176fad8f66SThierry Reding 		dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
1018e1335e2fSThierry Reding 		return err;
1019e1335e2fSThierry Reding 	}
10206fad8f66SThierry Reding 
1021*880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
10226fad8f66SThierry Reding 	value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
10236fad8f66SThierry Reding 		   SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2);
1024*880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
10256fad8f66SThierry Reding 
10266fad8f66SThierry Reding 	/* stop lane sequencer */
10276fad8f66SThierry Reding 	value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_UP |
10286fad8f66SThierry Reding 		SOR_LANE_SEQ_CTL_POWER_STATE_DOWN;
10296fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
10306fad8f66SThierry Reding 
10316fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
10326fad8f66SThierry Reding 
10336fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
10346fad8f66SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
10356fad8f66SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
10366fad8f66SThierry Reding 			break;
10376fad8f66SThierry Reding 
10386fad8f66SThierry Reding 		usleep_range(25, 100);
10396fad8f66SThierry Reding 	}
10406fad8f66SThierry Reding 
10416fad8f66SThierry Reding 	if ((value & SOR_LANE_SEQ_CTL_TRIGGER) != 0)
10426fad8f66SThierry Reding 		return -ETIMEDOUT;
10436fad8f66SThierry Reding 
1044*880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1045a9a9e4fdSThierry Reding 	value |= SOR_PLL2_PORT_POWERDOWN;
1046*880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
10476fad8f66SThierry Reding 
10486fad8f66SThierry Reding 	usleep_range(20, 100);
10496fad8f66SThierry Reding 
1050*880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll0);
1051a9a9e4fdSThierry Reding 	value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
1052*880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll0);
10536fad8f66SThierry Reding 
1054*880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1055a9a9e4fdSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD;
1056a9a9e4fdSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1057*880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
10586fad8f66SThierry Reding 
10596fad8f66SThierry Reding 	usleep_range(20, 100);
10606fad8f66SThierry Reding 
10616fad8f66SThierry Reding 	return 0;
10626fad8f66SThierry Reding }
10636fad8f66SThierry Reding 
10646fad8f66SThierry Reding static int tegra_sor_crc_wait(struct tegra_sor *sor, unsigned long timeout)
10656fad8f66SThierry Reding {
10666fad8f66SThierry Reding 	u32 value;
10676fad8f66SThierry Reding 
10686fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
10696fad8f66SThierry Reding 
10706fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
1071a9a9e4fdSThierry Reding 		value = tegra_sor_readl(sor, SOR_CRCA);
1072a9a9e4fdSThierry Reding 		if (value & SOR_CRCA_VALID)
10736fad8f66SThierry Reding 			return 0;
10746fad8f66SThierry Reding 
10756fad8f66SThierry Reding 		usleep_range(100, 200);
10766fad8f66SThierry Reding 	}
10776fad8f66SThierry Reding 
10786fad8f66SThierry Reding 	return -ETIMEDOUT;
10796fad8f66SThierry Reding }
10806fad8f66SThierry Reding 
1081530239a8SThierry Reding static int tegra_sor_show_crc(struct seq_file *s, void *data)
10826fad8f66SThierry Reding {
1083530239a8SThierry Reding 	struct drm_info_node *node = s->private;
1084530239a8SThierry Reding 	struct tegra_sor *sor = node->info_ent->data;
1085850bab44SThierry Reding 	struct drm_crtc *crtc = sor->output.encoder.crtc;
1086850bab44SThierry Reding 	struct drm_device *drm = node->minor->dev;
1087530239a8SThierry Reding 	int err = 0;
10886fad8f66SThierry Reding 	u32 value;
10896fad8f66SThierry Reding 
1090850bab44SThierry Reding 	drm_modeset_lock_all(drm);
10916fad8f66SThierry Reding 
1092850bab44SThierry Reding 	if (!crtc || !crtc->state->active) {
1093850bab44SThierry Reding 		err = -EBUSY;
10946fad8f66SThierry Reding 		goto unlock;
10956fad8f66SThierry Reding 	}
10966fad8f66SThierry Reding 
1097a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
10986fad8f66SThierry Reding 	value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
1099a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
11006fad8f66SThierry Reding 
11016fad8f66SThierry Reding 	value = tegra_sor_readl(sor, SOR_CRC_CNTRL);
11026fad8f66SThierry Reding 	value |= SOR_CRC_CNTRL_ENABLE;
11036fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_CRC_CNTRL);
11046fad8f66SThierry Reding 
11056fad8f66SThierry Reding 	value = tegra_sor_readl(sor, SOR_TEST);
11066fad8f66SThierry Reding 	value &= ~SOR_TEST_CRC_POST_SERIALIZE;
11076fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_TEST);
11086fad8f66SThierry Reding 
11096fad8f66SThierry Reding 	err = tegra_sor_crc_wait(sor, 100);
11106fad8f66SThierry Reding 	if (err < 0)
11116fad8f66SThierry Reding 		goto unlock;
11126fad8f66SThierry Reding 
1113a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, SOR_CRCA_RESET, SOR_CRCA);
1114a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_CRCB);
11156fad8f66SThierry Reding 
1116530239a8SThierry Reding 	seq_printf(s, "%08x\n", value);
11176fad8f66SThierry Reding 
11186fad8f66SThierry Reding unlock:
1119850bab44SThierry Reding 	drm_modeset_unlock_all(drm);
11206fad8f66SThierry Reding 	return err;
11216fad8f66SThierry Reding }
11226fad8f66SThierry Reding 
1123062f5b2cSThierry Reding #define DEBUGFS_REG32(_name) { .name = #_name, .offset = _name }
1124062f5b2cSThierry Reding 
1125062f5b2cSThierry Reding static const struct debugfs_reg32 tegra_sor_regs[] = {
1126062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CTXSW),
1127062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SUPER_STATE0),
1128062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SUPER_STATE1),
1129062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_STATE0),
1130062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_STATE1),
1131062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE0(0)),
1132062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE0(1)),
1133062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE1(0)),
1134062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE1(1)),
1135062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE2(0)),
1136062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE2(1)),
1137062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE3(0)),
1138062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE3(1)),
1139062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE4(0)),
1140062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE4(1)),
1141062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE5(0)),
1142062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_HEAD_STATE5(1)),
1143062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CRC_CNTRL),
1144062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_DEBUG_MVID),
1145062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CLK_CNTRL),
1146062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CAP),
1147062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PWR),
1148062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_TEST),
1149062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PLL0),
1150062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PLL1),
1151062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PLL2),
1152062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PLL3),
1153062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CSTM),
1154062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LVDS),
1155062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CRCA),
1156062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CRCB),
1157062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_BLANK),
1158062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_CTL),
1159062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_SEQ_CTL),
1160062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(0)),
1161062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(1)),
1162062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(2)),
1163062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(3)),
1164062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(4)),
1165062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(5)),
1166062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(6)),
1167062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(7)),
1168062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(8)),
1169062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(9)),
1170062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(10)),
1171062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(11)),
1172062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(12)),
1173062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(13)),
1174062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(14)),
1175062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_SEQ_INST(15)),
1176062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PWM_DIV),
1177062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_PWM_CTL),
1178062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_VCRC_A0),
1179062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_VCRC_A1),
1180062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_VCRC_B0),
1181062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_VCRC_B1),
1182062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CCRC_A0),
1183062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CCRC_A1),
1184062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CCRC_B0),
1185062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_CCRC_B1),
1186062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_EDATA_A0),
1187062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_EDATA_A1),
1188062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_EDATA_B0),
1189062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_EDATA_B1),
1190062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_COUNT_A0),
1191062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_COUNT_A1),
1192062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_COUNT_B0),
1193062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_COUNT_B1),
1194062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DEBUG_A0),
1195062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DEBUG_A1),
1196062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DEBUG_B0),
1197062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DEBUG_B1),
1198062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_TRIG),
1199062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_MSCHECK),
1200062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_XBAR_CTRL),
1201062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_XBAR_POL),
1202062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_LINKCTL0),
1203062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_LINKCTL1),
1204062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_DRIVE_CURRENT0),
1205062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_DRIVE_CURRENT1),
1206062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE4_DRIVE_CURRENT0),
1207062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE4_DRIVE_CURRENT1),
1208062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_PREEMPHASIS0),
1209062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_PREEMPHASIS1),
1210062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE4_PREEMPHASIS0),
1211062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE4_PREEMPHASIS1),
1212062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_POSTCURSOR0),
1213062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_LANE_POSTCURSOR1),
1214062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_CONFIG0),
1215062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_CONFIG1),
1216062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_MN0),
1217062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_MN1),
1218062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_PADCTL0),
1219062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_PADCTL1),
1220062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_DEBUG0),
1221062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_DEBUG1),
1222062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_SPARE0),
1223062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_SPARE1),
1224062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_AUDIO_CTRL),
1225062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_AUDIO_HBLANK_SYMBOLS),
1226062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_AUDIO_VBLANK_SYMBOLS),
1227062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_HEADER),
1228062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK0),
1229062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK1),
1230062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK2),
1231062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK3),
1232062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK4),
1233062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK5),
1234062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK6),
1235062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_TPG),
1236062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_TPG_CONFIG),
1237062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_LQ_CSTM0),
1238062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_LQ_CSTM1),
1239062f5b2cSThierry Reding 	DEBUGFS_REG32(SOR_DP_LQ_CSTM2),
1240062f5b2cSThierry Reding };
1241062f5b2cSThierry Reding 
1242dab16336SThierry Reding static int tegra_sor_show_regs(struct seq_file *s, void *data)
1243dab16336SThierry Reding {
1244dab16336SThierry Reding 	struct drm_info_node *node = s->private;
1245dab16336SThierry Reding 	struct tegra_sor *sor = node->info_ent->data;
1246850bab44SThierry Reding 	struct drm_crtc *crtc = sor->output.encoder.crtc;
1247850bab44SThierry Reding 	struct drm_device *drm = node->minor->dev;
1248062f5b2cSThierry Reding 	unsigned int i;
1249850bab44SThierry Reding 	int err = 0;
1250850bab44SThierry Reding 
1251850bab44SThierry Reding 	drm_modeset_lock_all(drm);
1252850bab44SThierry Reding 
1253850bab44SThierry Reding 	if (!crtc || !crtc->state->active) {
1254850bab44SThierry Reding 		err = -EBUSY;
1255850bab44SThierry Reding 		goto unlock;
1256850bab44SThierry Reding 	}
1257dab16336SThierry Reding 
1258062f5b2cSThierry Reding 	for (i = 0; i < ARRAY_SIZE(tegra_sor_regs); i++) {
1259062f5b2cSThierry Reding 		unsigned int offset = tegra_sor_regs[i].offset;
1260dab16336SThierry Reding 
1261062f5b2cSThierry Reding 		seq_printf(s, "%-38s %#05x %08x\n", tegra_sor_regs[i].name,
1262062f5b2cSThierry Reding 			   offset, tegra_sor_readl(sor, offset));
1263062f5b2cSThierry Reding 	}
1264dab16336SThierry Reding 
1265850bab44SThierry Reding unlock:
1266850bab44SThierry Reding 	drm_modeset_unlock_all(drm);
1267850bab44SThierry Reding 	return err;
1268dab16336SThierry Reding }
1269dab16336SThierry Reding 
1270dab16336SThierry Reding static const struct drm_info_list debugfs_files[] = {
1271530239a8SThierry Reding 	{ "crc", tegra_sor_show_crc, 0, NULL },
1272dab16336SThierry Reding 	{ "regs", tegra_sor_show_regs, 0, NULL },
1273dab16336SThierry Reding };
1274dab16336SThierry Reding 
12755b8e043bSThierry Reding static int tegra_sor_late_register(struct drm_connector *connector)
12766fad8f66SThierry Reding {
12775b8e043bSThierry Reding 	struct tegra_output *output = connector_to_output(connector);
12785b8e043bSThierry Reding 	unsigned int i, count = ARRAY_SIZE(debugfs_files);
12795b8e043bSThierry Reding 	struct drm_minor *minor = connector->dev->primary;
12805b8e043bSThierry Reding 	struct dentry *root = connector->debugfs_entry;
12815b8e043bSThierry Reding 	struct tegra_sor *sor = to_sor(output);
1282530239a8SThierry Reding 	int err;
12836fad8f66SThierry Reding 
1284dab16336SThierry Reding 	sor->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1285dab16336SThierry Reding 				     GFP_KERNEL);
12865b8e043bSThierry Reding 	if (!sor->debugfs_files)
12875b8e043bSThierry Reding 		return -ENOMEM;
12886fad8f66SThierry Reding 
12895b8e043bSThierry Reding 	for (i = 0; i < count; i++)
1290dab16336SThierry Reding 		sor->debugfs_files[i].data = sor;
1291dab16336SThierry Reding 
12925b8e043bSThierry Reding 	err = drm_debugfs_create_files(sor->debugfs_files, count, root, minor);
1293dab16336SThierry Reding 	if (err < 0)
1294dab16336SThierry Reding 		goto free;
1295dab16336SThierry Reding 
1296530239a8SThierry Reding 	return 0;
12976fad8f66SThierry Reding 
1298dab16336SThierry Reding free:
1299dab16336SThierry Reding 	kfree(sor->debugfs_files);
1300dab16336SThierry Reding 	sor->debugfs_files = NULL;
13015b8e043bSThierry Reding 
13026fad8f66SThierry Reding 	return err;
13036fad8f66SThierry Reding }
13046fad8f66SThierry Reding 
13055b8e043bSThierry Reding static void tegra_sor_early_unregister(struct drm_connector *connector)
13066fad8f66SThierry Reding {
13075b8e043bSThierry Reding 	struct tegra_output *output = connector_to_output(connector);
13085b8e043bSThierry Reding 	unsigned int count = ARRAY_SIZE(debugfs_files);
13095b8e043bSThierry Reding 	struct tegra_sor *sor = to_sor(output);
1310d92e6009SThierry Reding 
13115b8e043bSThierry Reding 	drm_debugfs_remove_files(sor->debugfs_files, count,
13125b8e043bSThierry Reding 				 connector->dev->primary);
1313dab16336SThierry Reding 	kfree(sor->debugfs_files);
1314066d30f8SThierry Reding 	sor->debugfs_files = NULL;
13156fad8f66SThierry Reding }
13166fad8f66SThierry Reding 
1317c31efa7aSThierry Reding static void tegra_sor_connector_reset(struct drm_connector *connector)
1318c31efa7aSThierry Reding {
1319c31efa7aSThierry Reding 	struct tegra_sor_state *state;
1320c31efa7aSThierry Reding 
1321c31efa7aSThierry Reding 	state = kzalloc(sizeof(*state), GFP_KERNEL);
1322c31efa7aSThierry Reding 	if (!state)
1323c31efa7aSThierry Reding 		return;
1324c31efa7aSThierry Reding 
1325c31efa7aSThierry Reding 	if (connector->state) {
1326c31efa7aSThierry Reding 		__drm_atomic_helper_connector_destroy_state(connector->state);
1327c31efa7aSThierry Reding 		kfree(connector->state);
1328c31efa7aSThierry Reding 	}
1329c31efa7aSThierry Reding 
1330c31efa7aSThierry Reding 	__drm_atomic_helper_connector_reset(connector, &state->base);
1331c31efa7aSThierry Reding }
1332c31efa7aSThierry Reding 
13336fad8f66SThierry Reding static enum drm_connector_status
13346fad8f66SThierry Reding tegra_sor_connector_detect(struct drm_connector *connector, bool force)
13356fad8f66SThierry Reding {
13366fad8f66SThierry Reding 	struct tegra_output *output = connector_to_output(connector);
13376fad8f66SThierry Reding 	struct tegra_sor *sor = to_sor(output);
13386fad8f66SThierry Reding 
13399542c237SThierry Reding 	if (sor->aux)
13409542c237SThierry Reding 		return drm_dp_aux_detect(sor->aux);
13416fad8f66SThierry Reding 
1342459cc2c6SThierry Reding 	return tegra_output_connector_detect(connector, force);
13436fad8f66SThierry Reding }
13446fad8f66SThierry Reding 
1345c31efa7aSThierry Reding static struct drm_connector_state *
1346c31efa7aSThierry Reding tegra_sor_connector_duplicate_state(struct drm_connector *connector)
1347c31efa7aSThierry Reding {
1348c31efa7aSThierry Reding 	struct tegra_sor_state *state = to_sor_state(connector->state);
1349c31efa7aSThierry Reding 	struct tegra_sor_state *copy;
1350c31efa7aSThierry Reding 
1351c31efa7aSThierry Reding 	copy = kmemdup(state, sizeof(*state), GFP_KERNEL);
1352c31efa7aSThierry Reding 	if (!copy)
1353c31efa7aSThierry Reding 		return NULL;
1354c31efa7aSThierry Reding 
1355c31efa7aSThierry Reding 	__drm_atomic_helper_connector_duplicate_state(connector, &copy->base);
1356c31efa7aSThierry Reding 
1357c31efa7aSThierry Reding 	return &copy->base;
1358c31efa7aSThierry Reding }
1359c31efa7aSThierry Reding 
13606fad8f66SThierry Reding static const struct drm_connector_funcs tegra_sor_connector_funcs = {
1361c31efa7aSThierry Reding 	.reset = tegra_sor_connector_reset,
13626fad8f66SThierry Reding 	.detect = tegra_sor_connector_detect,
13636fad8f66SThierry Reding 	.fill_modes = drm_helper_probe_single_connector_modes,
13646fad8f66SThierry Reding 	.destroy = tegra_output_connector_destroy,
1365c31efa7aSThierry Reding 	.atomic_duplicate_state = tegra_sor_connector_duplicate_state,
13664aa3df71SThierry Reding 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
13675b8e043bSThierry Reding 	.late_register = tegra_sor_late_register,
13685b8e043bSThierry Reding 	.early_unregister = tegra_sor_early_unregister,
13696fad8f66SThierry Reding };
13706fad8f66SThierry Reding 
13716fad8f66SThierry Reding static int tegra_sor_connector_get_modes(struct drm_connector *connector)
13726fad8f66SThierry Reding {
13736fad8f66SThierry Reding 	struct tegra_output *output = connector_to_output(connector);
13746fad8f66SThierry Reding 	struct tegra_sor *sor = to_sor(output);
13756fad8f66SThierry Reding 	int err;
13766fad8f66SThierry Reding 
13779542c237SThierry Reding 	if (sor->aux)
13789542c237SThierry Reding 		drm_dp_aux_enable(sor->aux);
13796fad8f66SThierry Reding 
13806fad8f66SThierry Reding 	err = tegra_output_connector_get_modes(connector);
13816fad8f66SThierry Reding 
13829542c237SThierry Reding 	if (sor->aux)
13839542c237SThierry Reding 		drm_dp_aux_disable(sor->aux);
13846fad8f66SThierry Reding 
13856fad8f66SThierry Reding 	return err;
13866fad8f66SThierry Reding }
13876fad8f66SThierry Reding 
13886fad8f66SThierry Reding static enum drm_mode_status
13896fad8f66SThierry Reding tegra_sor_connector_mode_valid(struct drm_connector *connector,
13906fad8f66SThierry Reding 			       struct drm_display_mode *mode)
13916fad8f66SThierry Reding {
139264ea25c3SThierry Reding 	/* HDMI 2.0 modes are not yet supported */
139364ea25c3SThierry Reding 	if (mode->clock > 340000)
139464ea25c3SThierry Reding 		return MODE_NOCLOCK;
139564ea25c3SThierry Reding 
13966fad8f66SThierry Reding 	return MODE_OK;
13976fad8f66SThierry Reding }
13986fad8f66SThierry Reding 
13996fad8f66SThierry Reding static const struct drm_connector_helper_funcs tegra_sor_connector_helper_funcs = {
14006fad8f66SThierry Reding 	.get_modes = tegra_sor_connector_get_modes,
14016fad8f66SThierry Reding 	.mode_valid = tegra_sor_connector_mode_valid,
14026fad8f66SThierry Reding };
14036fad8f66SThierry Reding 
14046fad8f66SThierry Reding static const struct drm_encoder_funcs tegra_sor_encoder_funcs = {
14056fad8f66SThierry Reding 	.destroy = tegra_output_encoder_destroy,
14066fad8f66SThierry Reding };
14076fad8f66SThierry Reding 
1408850bab44SThierry Reding static void tegra_sor_edp_disable(struct drm_encoder *encoder)
14096fad8f66SThierry Reding {
1410850bab44SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
1411850bab44SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
1412850bab44SThierry Reding 	struct tegra_sor *sor = to_sor(output);
1413850bab44SThierry Reding 	u32 value;
1414850bab44SThierry Reding 	int err;
1415850bab44SThierry Reding 
1416850bab44SThierry Reding 	if (output->panel)
1417850bab44SThierry Reding 		drm_panel_disable(output->panel);
1418850bab44SThierry Reding 
1419850bab44SThierry Reding 	err = tegra_sor_detach(sor);
1420850bab44SThierry Reding 	if (err < 0)
1421850bab44SThierry Reding 		dev_err(sor->dev, "failed to detach SOR: %d\n", err);
1422850bab44SThierry Reding 
1423850bab44SThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE1);
1424850bab44SThierry Reding 	tegra_sor_update(sor);
1425850bab44SThierry Reding 
1426850bab44SThierry Reding 	/*
1427850bab44SThierry Reding 	 * The following accesses registers of the display controller, so make
1428850bab44SThierry Reding 	 * sure it's only executed when the output is attached to one.
1429850bab44SThierry Reding 	 */
1430850bab44SThierry Reding 	if (dc) {
1431850bab44SThierry Reding 		value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
1432850bab44SThierry Reding 		value &= ~SOR_ENABLE;
1433850bab44SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
1434850bab44SThierry Reding 
1435850bab44SThierry Reding 		tegra_dc_commit(dc);
14366fad8f66SThierry Reding 	}
14376fad8f66SThierry Reding 
1438850bab44SThierry Reding 	err = tegra_sor_power_down(sor);
1439850bab44SThierry Reding 	if (err < 0)
1440850bab44SThierry Reding 		dev_err(sor->dev, "failed to power down SOR: %d\n", err);
1441850bab44SThierry Reding 
14429542c237SThierry Reding 	if (sor->aux) {
14439542c237SThierry Reding 		err = drm_dp_aux_disable(sor->aux);
1444850bab44SThierry Reding 		if (err < 0)
1445850bab44SThierry Reding 			dev_err(sor->dev, "failed to disable DP: %d\n", err);
14466fad8f66SThierry Reding 	}
14476fad8f66SThierry Reding 
1448850bab44SThierry Reding 	err = tegra_io_rail_power_off(TEGRA_IO_RAIL_LVDS);
1449850bab44SThierry Reding 	if (err < 0)
1450850bab44SThierry Reding 		dev_err(sor->dev, "failed to power off I/O rail: %d\n", err);
1451850bab44SThierry Reding 
1452850bab44SThierry Reding 	if (output->panel)
1453850bab44SThierry Reding 		drm_panel_unprepare(output->panel);
1454850bab44SThierry Reding 
1455aaff8bd2SThierry Reding 	pm_runtime_put(sor->dev);
14566fad8f66SThierry Reding }
14576fad8f66SThierry Reding 
1458459cc2c6SThierry Reding #if 0
1459459cc2c6SThierry Reding static int calc_h_ref_to_sync(const struct drm_display_mode *mode,
1460459cc2c6SThierry Reding 			      unsigned int *value)
1461459cc2c6SThierry Reding {
1462459cc2c6SThierry Reding 	unsigned int hfp, hsw, hbp, a = 0, b;
1463459cc2c6SThierry Reding 
1464459cc2c6SThierry Reding 	hfp = mode->hsync_start - mode->hdisplay;
1465459cc2c6SThierry Reding 	hsw = mode->hsync_end - mode->hsync_start;
1466459cc2c6SThierry Reding 	hbp = mode->htotal - mode->hsync_end;
1467459cc2c6SThierry Reding 
1468459cc2c6SThierry Reding 	pr_info("hfp: %u, hsw: %u, hbp: %u\n", hfp, hsw, hbp);
1469459cc2c6SThierry Reding 
1470459cc2c6SThierry Reding 	b = hfp - 1;
1471459cc2c6SThierry Reding 
1472459cc2c6SThierry Reding 	pr_info("a: %u, b: %u\n", a, b);
1473459cc2c6SThierry Reding 	pr_info("a + hsw + hbp = %u\n", a + hsw + hbp);
1474459cc2c6SThierry Reding 
1475459cc2c6SThierry Reding 	if (a + hsw + hbp <= 11) {
1476459cc2c6SThierry Reding 		a = 1 + 11 - hsw - hbp;
1477459cc2c6SThierry Reding 		pr_info("a: %u\n", a);
1478459cc2c6SThierry Reding 	}
1479459cc2c6SThierry Reding 
1480459cc2c6SThierry Reding 	if (a > b)
1481459cc2c6SThierry Reding 		return -EINVAL;
1482459cc2c6SThierry Reding 
1483459cc2c6SThierry Reding 	if (hsw < 1)
1484459cc2c6SThierry Reding 		return -EINVAL;
1485459cc2c6SThierry Reding 
1486459cc2c6SThierry Reding 	if (mode->hdisplay < 16)
1487459cc2c6SThierry Reding 		return -EINVAL;
1488459cc2c6SThierry Reding 
1489459cc2c6SThierry Reding 	if (value) {
1490459cc2c6SThierry Reding 		if (b > a && a % 2)
1491459cc2c6SThierry Reding 			*value = a + 1;
1492459cc2c6SThierry Reding 		else
1493459cc2c6SThierry Reding 			*value = a;
1494459cc2c6SThierry Reding 	}
1495459cc2c6SThierry Reding 
1496459cc2c6SThierry Reding 	return 0;
1497459cc2c6SThierry Reding }
1498459cc2c6SThierry Reding #endif
1499459cc2c6SThierry Reding 
1500850bab44SThierry Reding static void tegra_sor_edp_enable(struct drm_encoder *encoder)
15016fad8f66SThierry Reding {
1502850bab44SThierry Reding 	struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
15036fad8f66SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
15046fad8f66SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
15056b6b6042SThierry Reding 	struct tegra_sor *sor = to_sor(output);
150634fa183bSThierry Reding 	struct tegra_sor_config config;
1507c31efa7aSThierry Reding 	struct tegra_sor_state *state;
150834fa183bSThierry Reding 	struct drm_dp_link link;
150901b9bea0SThierry Reding 	u8 rate, lanes;
15102bd1dd39SThierry Reding 	unsigned int i;
151186f5c52dSThierry Reding 	int err = 0;
151228fe2076SThierry Reding 	u32 value;
151386f5c52dSThierry Reding 
1514c31efa7aSThierry Reding 	state = to_sor_state(output->connector.state);
15156b6b6042SThierry Reding 
1516aaff8bd2SThierry Reding 	pm_runtime_get_sync(sor->dev);
15176b6b6042SThierry Reding 
15186fad8f66SThierry Reding 	if (output->panel)
15196fad8f66SThierry Reding 		drm_panel_prepare(output->panel);
15206fad8f66SThierry Reding 
15219542c237SThierry Reding 	err = drm_dp_aux_enable(sor->aux);
15226b6b6042SThierry Reding 	if (err < 0)
15236b6b6042SThierry Reding 		dev_err(sor->dev, "failed to enable DP: %d\n", err);
152434fa183bSThierry Reding 
15259542c237SThierry Reding 	err = drm_dp_link_probe(sor->aux, &link);
152634fa183bSThierry Reding 	if (err < 0) {
152701b9bea0SThierry Reding 		dev_err(sor->dev, "failed to probe eDP link: %d\n", err);
1528850bab44SThierry Reding 		return;
152934fa183bSThierry Reding 	}
15306b6b6042SThierry Reding 
153125bb2cecSThierry Reding 	/* switch to safe parent clock */
153225bb2cecSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
15336b6b6042SThierry Reding 	if (err < 0)
15346b6b6042SThierry Reding 		dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
15356b6b6042SThierry Reding 
153634fa183bSThierry Reding 	memset(&config, 0, sizeof(config));
1537c31efa7aSThierry Reding 	config.bits_per_pixel = state->bpc * 3;
153834fa183bSThierry Reding 
1539a198359eSThierry Reding 	err = tegra_sor_compute_config(sor, mode, &config, &link);
154034fa183bSThierry Reding 	if (err < 0)
1541a198359eSThierry Reding 		dev_err(sor->dev, "failed to compute configuration: %d\n", err);
154234fa183bSThierry Reding 
15436b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
15446b6b6042SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
15456b6b6042SThierry Reding 	value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
15466b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
15476b6b6042SThierry Reding 
1548*880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1549a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
1550*880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
15516b6b6042SThierry Reding 	usleep_range(20, 100);
15526b6b6042SThierry Reding 
1553*880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll3);
1554a9a9e4fdSThierry Reding 	value |= SOR_PLL3_PLL_VDD_MODE_3V3;
1555*880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll3);
15566b6b6042SThierry Reding 
1557a9a9e4fdSThierry Reding 	value = SOR_PLL0_ICHPMP(0xf) | SOR_PLL0_VCOCAP_RST |
1558a9a9e4fdSThierry Reding 		SOR_PLL0_PLLREG_LEVEL_V45 | SOR_PLL0_RESISTOR_EXT;
1559*880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll0);
15606b6b6042SThierry Reding 
1561*880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1562a9a9e4fdSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD;
1563a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1564a9a9e4fdSThierry Reding 	value |= SOR_PLL2_LVDS_ENABLE;
1565*880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
15666b6b6042SThierry Reding 
1567a9a9e4fdSThierry Reding 	value = SOR_PLL1_TERM_COMPOUT | SOR_PLL1_TMDS_TERM;
1568*880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll1);
15696b6b6042SThierry Reding 
15706b6b6042SThierry Reding 	while (true) {
1571*880cee0bSThierry Reding 		value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1572a9a9e4fdSThierry Reding 		if ((value & SOR_PLL2_SEQ_PLLCAPPD_ENFORCE) == 0)
15736b6b6042SThierry Reding 			break;
15746b6b6042SThierry Reding 
15756b6b6042SThierry Reding 		usleep_range(250, 1000);
15766b6b6042SThierry Reding 	}
15776b6b6042SThierry Reding 
1578*880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1579a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
1580a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_PORT_POWERDOWN;
1581*880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
15826b6b6042SThierry Reding 
15836b6b6042SThierry Reding 	/*
15846b6b6042SThierry Reding 	 * power up
15856b6b6042SThierry Reding 	 */
15866b6b6042SThierry Reding 
15876b6b6042SThierry Reding 	/* set safe link bandwidth (1.62 Gbps) */
15886b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
15896b6b6042SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
15906b6b6042SThierry Reding 	value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G1_62;
15916b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
15926b6b6042SThierry Reding 
15936b6b6042SThierry Reding 	/* step 1 */
1594*880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1595a9a9e4fdSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE | SOR_PLL2_PORT_POWERDOWN |
1596a9a9e4fdSThierry Reding 		 SOR_PLL2_BANDGAP_POWERDOWN;
1597*880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
15986b6b6042SThierry Reding 
1599*880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll0);
1600a9a9e4fdSThierry Reding 	value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
1601*880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll0);
16026b6b6042SThierry Reding 
1603*880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
16046b6b6042SThierry Reding 	value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
1605*880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
16066b6b6042SThierry Reding 
16076b6b6042SThierry Reding 	/* step 2 */
16086b6b6042SThierry Reding 	err = tegra_io_rail_power_on(TEGRA_IO_RAIL_LVDS);
1609850bab44SThierry Reding 	if (err < 0)
16106b6b6042SThierry Reding 		dev_err(sor->dev, "failed to power on I/O rail: %d\n", err);
16116b6b6042SThierry Reding 
16126b6b6042SThierry Reding 	usleep_range(5, 100);
16136b6b6042SThierry Reding 
16146b6b6042SThierry Reding 	/* step 3 */
1615*880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1616a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
1617*880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
16186b6b6042SThierry Reding 
16196b6b6042SThierry Reding 	usleep_range(20, 100);
16206b6b6042SThierry Reding 
16216b6b6042SThierry Reding 	/* step 4 */
1622*880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll0);
1623a9a9e4fdSThierry Reding 	value &= ~SOR_PLL0_VCOPD;
1624a9a9e4fdSThierry Reding 	value &= ~SOR_PLL0_PWR;
1625*880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll0);
16266b6b6042SThierry Reding 
1627*880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1628a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1629*880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
16306b6b6042SThierry Reding 
16316b6b6042SThierry Reding 	usleep_range(200, 1000);
16326b6b6042SThierry Reding 
16336b6b6042SThierry Reding 	/* step 5 */
1634*880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1635a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_PORT_POWERDOWN;
1636*880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
16376b6b6042SThierry Reding 
163830b49435SThierry Reding 	/* XXX not in TRM */
163930b49435SThierry Reding 	for (value = 0, i = 0; i < 5; i++)
164030b49435SThierry Reding 		value |= SOR_XBAR_CTRL_LINK0_XSEL(i, sor->soc->xbar_cfg[i]) |
164130b49435SThierry Reding 			 SOR_XBAR_CTRL_LINK1_XSEL(i, i);
164230b49435SThierry Reding 
164330b49435SThierry Reding 	tegra_sor_writel(sor, 0x00000000, SOR_XBAR_POL);
164430b49435SThierry Reding 	tegra_sor_writel(sor, value, SOR_XBAR_CTRL);
164530b49435SThierry Reding 
164625bb2cecSThierry Reding 	/* switch to DP parent clock */
164725bb2cecSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_dp);
16486b6b6042SThierry Reding 	if (err < 0)
164925bb2cecSThierry Reding 		dev_err(sor->dev, "failed to set parent clock: %d\n", err);
16506b6b6042SThierry Reding 
1651899451b7SThierry Reding 	/* power DP lanes */
1652*880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
1653899451b7SThierry Reding 
1654899451b7SThierry Reding 	if (link.num_lanes <= 2)
1655899451b7SThierry Reding 		value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2);
1656899451b7SThierry Reding 	else
1657899451b7SThierry Reding 		value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2;
1658899451b7SThierry Reding 
1659899451b7SThierry Reding 	if (link.num_lanes <= 1)
1660899451b7SThierry Reding 		value &= ~SOR_DP_PADCTL_PD_TXD_1;
1661899451b7SThierry Reding 	else
1662899451b7SThierry Reding 		value |= SOR_DP_PADCTL_PD_TXD_1;
1663899451b7SThierry Reding 
1664899451b7SThierry Reding 	if (link.num_lanes == 0)
1665899451b7SThierry Reding 		value &= ~SOR_DP_PADCTL_PD_TXD_0;
1666899451b7SThierry Reding 	else
1667899451b7SThierry Reding 		value |= SOR_DP_PADCTL_PD_TXD_0;
1668899451b7SThierry Reding 
1669*880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
16706b6b6042SThierry Reding 
1671a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
16726b6b6042SThierry Reding 	value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
16730c90a184SThierry Reding 	value |= SOR_DP_LINKCTL_LANE_COUNT(link.num_lanes);
1674a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
16756b6b6042SThierry Reding 
16766b6b6042SThierry Reding 	/* start lane sequencer */
16776b6b6042SThierry Reding 	value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
16786b6b6042SThierry Reding 		SOR_LANE_SEQ_CTL_POWER_STATE_UP;
16796b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
16806b6b6042SThierry Reding 
16816b6b6042SThierry Reding 	while (true) {
16826b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
16836b6b6042SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
16846b6b6042SThierry Reding 			break;
16856b6b6042SThierry Reding 
16866b6b6042SThierry Reding 		usleep_range(250, 1000);
16876b6b6042SThierry Reding 	}
16886b6b6042SThierry Reding 
1689a4263fedSThierry Reding 	/* set link bandwidth */
16906b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
16916b6b6042SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
1692a4263fedSThierry Reding 	value |= drm_dp_link_rate_to_bw_code(link.rate) << 2;
16936b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
16946b6b6042SThierry Reding 
1695402f6bcdSThierry Reding 	tegra_sor_apply_config(sor, &config);
1696402f6bcdSThierry Reding 
1697402f6bcdSThierry Reding 	/* enable link */
1698a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
16996b6b6042SThierry Reding 	value |= SOR_DP_LINKCTL_ENABLE;
17006b6b6042SThierry Reding 	value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
1701a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
17026b6b6042SThierry Reding 
17036b6b6042SThierry Reding 	for (i = 0, value = 0; i < 4; i++) {
17046b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
17056b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_GALIOS |
17066b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_NONE;
17076b6b6042SThierry Reding 		value = (value << 8) | lane;
17086b6b6042SThierry Reding 	}
17096b6b6042SThierry Reding 
17106b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
17116b6b6042SThierry Reding 
17126b6b6042SThierry Reding 	/* enable pad calibration logic */
1713*880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
17146b6b6042SThierry Reding 	value |= SOR_DP_PADCTL_PAD_CAL_PD;
1715*880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
17166b6b6042SThierry Reding 
17179542c237SThierry Reding 	err = drm_dp_link_probe(sor->aux, &link);
1718850bab44SThierry Reding 	if (err < 0)
171901b9bea0SThierry Reding 		dev_err(sor->dev, "failed to probe eDP link: %d\n", err);
17206b6b6042SThierry Reding 
17219542c237SThierry Reding 	err = drm_dp_link_power_up(sor->aux, &link);
1722850bab44SThierry Reding 	if (err < 0)
172301b9bea0SThierry Reding 		dev_err(sor->dev, "failed to power up eDP link: %d\n", err);
17246b6b6042SThierry Reding 
17259542c237SThierry Reding 	err = drm_dp_link_configure(sor->aux, &link);
1726850bab44SThierry Reding 	if (err < 0)
172701b9bea0SThierry Reding 		dev_err(sor->dev, "failed to configure eDP link: %d\n", err);
17286b6b6042SThierry Reding 
17296b6b6042SThierry Reding 	rate = drm_dp_link_rate_to_bw_code(link.rate);
17306b6b6042SThierry Reding 	lanes = link.num_lanes;
17316b6b6042SThierry Reding 
17326b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
17336b6b6042SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
17346b6b6042SThierry Reding 	value |= SOR_CLK_CNTRL_DP_LINK_SPEED(rate);
17356b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
17366b6b6042SThierry Reding 
1737a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
17386b6b6042SThierry Reding 	value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
17396b6b6042SThierry Reding 	value |= SOR_DP_LINKCTL_LANE_COUNT(lanes);
17406b6b6042SThierry Reding 
17416b6b6042SThierry Reding 	if (link.capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
17426b6b6042SThierry Reding 		value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
17436b6b6042SThierry Reding 
1744a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
17456b6b6042SThierry Reding 
17466b6b6042SThierry Reding 	/* disable training pattern generator */
17476b6b6042SThierry Reding 
17486b6b6042SThierry Reding 	for (i = 0; i < link.num_lanes; i++) {
17496b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
17506b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_GALIOS |
17516b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_NONE;
17526b6b6042SThierry Reding 		value = (value << 8) | lane;
17536b6b6042SThierry Reding 	}
17546b6b6042SThierry Reding 
17556b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
17566b6b6042SThierry Reding 
17576b6b6042SThierry Reding 	err = tegra_sor_dp_train_fast(sor, &link);
175801b9bea0SThierry Reding 	if (err < 0)
175901b9bea0SThierry Reding 		dev_err(sor->dev, "DP fast link training failed: %d\n", err);
17606b6b6042SThierry Reding 
17616b6b6042SThierry Reding 	dev_dbg(sor->dev, "fast link training succeeded\n");
17626b6b6042SThierry Reding 
17636b6b6042SThierry Reding 	err = tegra_sor_power_up(sor, 250);
1764850bab44SThierry Reding 	if (err < 0)
17656b6b6042SThierry Reding 		dev_err(sor->dev, "failed to power up SOR: %d\n", err);
17666b6b6042SThierry Reding 
17676b6b6042SThierry Reding 	/* CSTM (LVDS, link A/B, upper) */
1768143b1df2SStéphane Marchesin 	value = SOR_CSTM_LVDS | SOR_CSTM_LINK_ACT_A | SOR_CSTM_LINK_ACT_B |
17696b6b6042SThierry Reding 		SOR_CSTM_UPPER;
17706b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CSTM);
17716b6b6042SThierry Reding 
17722bd1dd39SThierry Reding 	/* use DP-A protocol */
17732bd1dd39SThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
17742bd1dd39SThierry Reding 	value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
17752bd1dd39SThierry Reding 	value |= SOR_STATE_ASY_PROTOCOL_DP_A;
17762bd1dd39SThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
17772bd1dd39SThierry Reding 
1778c31efa7aSThierry Reding 	tegra_sor_mode_set(sor, mode, state);
17792bd1dd39SThierry Reding 
17806b6b6042SThierry Reding 	/* PWM setup */
17816b6b6042SThierry Reding 	err = tegra_sor_setup_pwm(sor, 250);
1782850bab44SThierry Reding 	if (err < 0)
17836b6b6042SThierry Reding 		dev_err(sor->dev, "failed to setup PWM: %d\n", err);
17846b6b6042SThierry Reding 
1785666cb873SThierry Reding 	tegra_sor_update(sor);
1786666cb873SThierry Reding 
17876b6b6042SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
17886b6b6042SThierry Reding 	value |= SOR_ENABLE;
17896b6b6042SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
17906b6b6042SThierry Reding 
1791666cb873SThierry Reding 	tegra_dc_commit(dc);
17926b6b6042SThierry Reding 
17936b6b6042SThierry Reding 	err = tegra_sor_attach(sor);
1794850bab44SThierry Reding 	if (err < 0)
17956b6b6042SThierry Reding 		dev_err(sor->dev, "failed to attach SOR: %d\n", err);
17966b6b6042SThierry Reding 
17976b6b6042SThierry Reding 	err = tegra_sor_wakeup(sor);
1798850bab44SThierry Reding 	if (err < 0)
17996b6b6042SThierry Reding 		dev_err(sor->dev, "failed to enable DC: %d\n", err);
18006b6b6042SThierry Reding 
18016fad8f66SThierry Reding 	if (output->panel)
18026fad8f66SThierry Reding 		drm_panel_enable(output->panel);
18036b6b6042SThierry Reding }
18046b6b6042SThierry Reding 
180582f1511cSThierry Reding static int
180682f1511cSThierry Reding tegra_sor_encoder_atomic_check(struct drm_encoder *encoder,
180782f1511cSThierry Reding 			       struct drm_crtc_state *crtc_state,
180882f1511cSThierry Reding 			       struct drm_connector_state *conn_state)
180982f1511cSThierry Reding {
181082f1511cSThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
1811c31efa7aSThierry Reding 	struct tegra_sor_state *state = to_sor_state(conn_state);
181282f1511cSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(conn_state->crtc);
181382f1511cSThierry Reding 	unsigned long pclk = crtc_state->mode.clock * 1000;
181482f1511cSThierry Reding 	struct tegra_sor *sor = to_sor(output);
1815c31efa7aSThierry Reding 	struct drm_display_info *info;
181682f1511cSThierry Reding 	int err;
181782f1511cSThierry Reding 
1818c31efa7aSThierry Reding 	info = &output->connector.display_info;
1819c31efa7aSThierry Reding 
182082f1511cSThierry Reding 	err = tegra_dc_state_setup_clock(dc, crtc_state, sor->clk_parent,
182182f1511cSThierry Reding 					 pclk, 0);
182282f1511cSThierry Reding 	if (err < 0) {
182382f1511cSThierry Reding 		dev_err(output->dev, "failed to setup CRTC state: %d\n", err);
182482f1511cSThierry Reding 		return err;
182582f1511cSThierry Reding 	}
182682f1511cSThierry Reding 
1827c31efa7aSThierry Reding 	switch (info->bpc) {
1828c31efa7aSThierry Reding 	case 8:
1829c31efa7aSThierry Reding 	case 6:
1830c31efa7aSThierry Reding 		state->bpc = info->bpc;
1831c31efa7aSThierry Reding 		break;
1832c31efa7aSThierry Reding 
1833c31efa7aSThierry Reding 	default:
1834c31efa7aSThierry Reding 		DRM_DEBUG_KMS("%u bits-per-color not supported\n", info->bpc);
1835c31efa7aSThierry Reding 		state->bpc = 8;
1836c31efa7aSThierry Reding 		break;
1837c31efa7aSThierry Reding 	}
1838c31efa7aSThierry Reding 
183982f1511cSThierry Reding 	return 0;
184082f1511cSThierry Reding }
184182f1511cSThierry Reding 
1842459cc2c6SThierry Reding static const struct drm_encoder_helper_funcs tegra_sor_edp_helpers = {
1843850bab44SThierry Reding 	.disable = tegra_sor_edp_disable,
1844850bab44SThierry Reding 	.enable = tegra_sor_edp_enable,
184582f1511cSThierry Reding 	.atomic_check = tegra_sor_encoder_atomic_check,
18466b6b6042SThierry Reding };
18476b6b6042SThierry Reding 
1848459cc2c6SThierry Reding static inline u32 tegra_sor_hdmi_subpack(const u8 *ptr, size_t size)
1849459cc2c6SThierry Reding {
1850459cc2c6SThierry Reding 	u32 value = 0;
1851459cc2c6SThierry Reding 	size_t i;
1852459cc2c6SThierry Reding 
1853459cc2c6SThierry Reding 	for (i = size; i > 0; i--)
1854459cc2c6SThierry Reding 		value = (value << 8) | ptr[i - 1];
1855459cc2c6SThierry Reding 
1856459cc2c6SThierry Reding 	return value;
1857459cc2c6SThierry Reding }
1858459cc2c6SThierry Reding 
1859459cc2c6SThierry Reding static void tegra_sor_hdmi_write_infopack(struct tegra_sor *sor,
1860459cc2c6SThierry Reding 					  const void *data, size_t size)
1861459cc2c6SThierry Reding {
1862459cc2c6SThierry Reding 	const u8 *ptr = data;
1863459cc2c6SThierry Reding 	unsigned long offset;
1864459cc2c6SThierry Reding 	size_t i, j;
1865459cc2c6SThierry Reding 	u32 value;
1866459cc2c6SThierry Reding 
1867459cc2c6SThierry Reding 	switch (ptr[0]) {
1868459cc2c6SThierry Reding 	case HDMI_INFOFRAME_TYPE_AVI:
1869459cc2c6SThierry Reding 		offset = SOR_HDMI_AVI_INFOFRAME_HEADER;
1870459cc2c6SThierry Reding 		break;
1871459cc2c6SThierry Reding 
1872459cc2c6SThierry Reding 	case HDMI_INFOFRAME_TYPE_AUDIO:
1873459cc2c6SThierry Reding 		offset = SOR_HDMI_AUDIO_INFOFRAME_HEADER;
1874459cc2c6SThierry Reding 		break;
1875459cc2c6SThierry Reding 
1876459cc2c6SThierry Reding 	case HDMI_INFOFRAME_TYPE_VENDOR:
1877459cc2c6SThierry Reding 		offset = SOR_HDMI_VSI_INFOFRAME_HEADER;
1878459cc2c6SThierry Reding 		break;
1879459cc2c6SThierry Reding 
1880459cc2c6SThierry Reding 	default:
1881459cc2c6SThierry Reding 		dev_err(sor->dev, "unsupported infoframe type: %02x\n",
1882459cc2c6SThierry Reding 			ptr[0]);
1883459cc2c6SThierry Reding 		return;
1884459cc2c6SThierry Reding 	}
1885459cc2c6SThierry Reding 
1886459cc2c6SThierry Reding 	value = INFOFRAME_HEADER_TYPE(ptr[0]) |
1887459cc2c6SThierry Reding 		INFOFRAME_HEADER_VERSION(ptr[1]) |
1888459cc2c6SThierry Reding 		INFOFRAME_HEADER_LEN(ptr[2]);
1889459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, offset);
1890459cc2c6SThierry Reding 	offset++;
1891459cc2c6SThierry Reding 
1892459cc2c6SThierry Reding 	/*
1893459cc2c6SThierry Reding 	 * Each subpack contains 7 bytes, divided into:
1894459cc2c6SThierry Reding 	 * - subpack_low: bytes 0 - 3
1895459cc2c6SThierry Reding 	 * - subpack_high: bytes 4 - 6 (with byte 7 padded to 0x00)
1896459cc2c6SThierry Reding 	 */
1897459cc2c6SThierry Reding 	for (i = 3, j = 0; i < size; i += 7, j += 8) {
1898459cc2c6SThierry Reding 		size_t rem = size - i, num = min_t(size_t, rem, 4);
1899459cc2c6SThierry Reding 
1900459cc2c6SThierry Reding 		value = tegra_sor_hdmi_subpack(&ptr[i], num);
1901459cc2c6SThierry Reding 		tegra_sor_writel(sor, value, offset++);
1902459cc2c6SThierry Reding 
1903459cc2c6SThierry Reding 		num = min_t(size_t, rem - num, 3);
1904459cc2c6SThierry Reding 
1905459cc2c6SThierry Reding 		value = tegra_sor_hdmi_subpack(&ptr[i + 4], num);
1906459cc2c6SThierry Reding 		tegra_sor_writel(sor, value, offset++);
1907459cc2c6SThierry Reding 	}
1908459cc2c6SThierry Reding }
1909459cc2c6SThierry Reding 
1910459cc2c6SThierry Reding static int
1911459cc2c6SThierry Reding tegra_sor_hdmi_setup_avi_infoframe(struct tegra_sor *sor,
1912459cc2c6SThierry Reding 				   const struct drm_display_mode *mode)
1913459cc2c6SThierry Reding {
1914459cc2c6SThierry Reding 	u8 buffer[HDMI_INFOFRAME_SIZE(AVI)];
1915459cc2c6SThierry Reding 	struct hdmi_avi_infoframe frame;
1916459cc2c6SThierry Reding 	u32 value;
1917459cc2c6SThierry Reding 	int err;
1918459cc2c6SThierry Reding 
1919459cc2c6SThierry Reding 	/* disable AVI infoframe */
1920459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI_AVI_INFOFRAME_CTRL);
1921459cc2c6SThierry Reding 	value &= ~INFOFRAME_CTRL_SINGLE;
1922459cc2c6SThierry Reding 	value &= ~INFOFRAME_CTRL_OTHER;
1923459cc2c6SThierry Reding 	value &= ~INFOFRAME_CTRL_ENABLE;
1924459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_AVI_INFOFRAME_CTRL);
1925459cc2c6SThierry Reding 
19260c1f528cSShashank Sharma 	err = drm_hdmi_avi_infoframe_from_display_mode(&frame, mode, false);
1927459cc2c6SThierry Reding 	if (err < 0) {
1928459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to setup AVI infoframe: %d\n", err);
1929459cc2c6SThierry Reding 		return err;
1930459cc2c6SThierry Reding 	}
1931459cc2c6SThierry Reding 
1932459cc2c6SThierry Reding 	err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
1933459cc2c6SThierry Reding 	if (err < 0) {
1934459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to pack AVI infoframe: %d\n", err);
1935459cc2c6SThierry Reding 		return err;
1936459cc2c6SThierry Reding 	}
1937459cc2c6SThierry Reding 
1938459cc2c6SThierry Reding 	tegra_sor_hdmi_write_infopack(sor, buffer, err);
1939459cc2c6SThierry Reding 
1940459cc2c6SThierry Reding 	/* enable AVI infoframe */
1941459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI_AVI_INFOFRAME_CTRL);
1942459cc2c6SThierry Reding 	value |= INFOFRAME_CTRL_CHECKSUM_ENABLE;
1943459cc2c6SThierry Reding 	value |= INFOFRAME_CTRL_ENABLE;
1944459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_AVI_INFOFRAME_CTRL);
1945459cc2c6SThierry Reding 
1946459cc2c6SThierry Reding 	return 0;
1947459cc2c6SThierry Reding }
1948459cc2c6SThierry Reding 
1949459cc2c6SThierry Reding static void tegra_sor_hdmi_disable_audio_infoframe(struct tegra_sor *sor)
1950459cc2c6SThierry Reding {
1951459cc2c6SThierry Reding 	u32 value;
1952459cc2c6SThierry Reding 
1953459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
1954459cc2c6SThierry Reding 	value &= ~INFOFRAME_CTRL_ENABLE;
1955459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
1956459cc2c6SThierry Reding }
1957459cc2c6SThierry Reding 
1958459cc2c6SThierry Reding static struct tegra_sor_hdmi_settings *
1959459cc2c6SThierry Reding tegra_sor_hdmi_find_settings(struct tegra_sor *sor, unsigned long frequency)
1960459cc2c6SThierry Reding {
1961459cc2c6SThierry Reding 	unsigned int i;
1962459cc2c6SThierry Reding 
1963459cc2c6SThierry Reding 	for (i = 0; i < sor->num_settings; i++)
1964459cc2c6SThierry Reding 		if (frequency <= sor->settings[i].frequency)
1965459cc2c6SThierry Reding 			return &sor->settings[i];
1966459cc2c6SThierry Reding 
1967459cc2c6SThierry Reding 	return NULL;
1968459cc2c6SThierry Reding }
1969459cc2c6SThierry Reding 
1970459cc2c6SThierry Reding static void tegra_sor_hdmi_disable(struct drm_encoder *encoder)
1971459cc2c6SThierry Reding {
1972459cc2c6SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
1973459cc2c6SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
1974459cc2c6SThierry Reding 	struct tegra_sor *sor = to_sor(output);
1975459cc2c6SThierry Reding 	u32 value;
1976459cc2c6SThierry Reding 	int err;
1977459cc2c6SThierry Reding 
1978459cc2c6SThierry Reding 	err = tegra_sor_detach(sor);
1979459cc2c6SThierry Reding 	if (err < 0)
1980459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to detach SOR: %d\n", err);
1981459cc2c6SThierry Reding 
1982459cc2c6SThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE1);
1983459cc2c6SThierry Reding 	tegra_sor_update(sor);
1984459cc2c6SThierry Reding 
1985459cc2c6SThierry Reding 	/* disable display to SOR clock */
1986459cc2c6SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
1987459cc2c6SThierry Reding 	value &= ~SOR1_TIMING_CYA;
1988459cc2c6SThierry Reding 	value &= ~SOR1_ENABLE;
1989459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
1990459cc2c6SThierry Reding 
1991459cc2c6SThierry Reding 	tegra_dc_commit(dc);
1992459cc2c6SThierry Reding 
1993459cc2c6SThierry Reding 	err = tegra_sor_power_down(sor);
1994459cc2c6SThierry Reding 	if (err < 0)
1995459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to power down SOR: %d\n", err);
1996459cc2c6SThierry Reding 
1997459cc2c6SThierry Reding 	err = tegra_io_rail_power_off(TEGRA_IO_RAIL_HDMI);
1998459cc2c6SThierry Reding 	if (err < 0)
1999459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to power off HDMI rail: %d\n", err);
2000459cc2c6SThierry Reding 
2001aaff8bd2SThierry Reding 	pm_runtime_put(sor->dev);
2002459cc2c6SThierry Reding }
2003459cc2c6SThierry Reding 
2004459cc2c6SThierry Reding static void tegra_sor_hdmi_enable(struct drm_encoder *encoder)
2005459cc2c6SThierry Reding {
2006459cc2c6SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
2007459cc2c6SThierry Reding 	unsigned int h_ref_to_sync = 1, pulse_start, max_ac;
2008459cc2c6SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
2009459cc2c6SThierry Reding 	struct tegra_sor_hdmi_settings *settings;
2010459cc2c6SThierry Reding 	struct tegra_sor *sor = to_sor(output);
2011c31efa7aSThierry Reding 	struct tegra_sor_state *state;
2012459cc2c6SThierry Reding 	struct drm_display_mode *mode;
201330b49435SThierry Reding 	unsigned int div, i;
2014459cc2c6SThierry Reding 	u32 value;
2015459cc2c6SThierry Reding 	int err;
2016459cc2c6SThierry Reding 
2017c31efa7aSThierry Reding 	state = to_sor_state(output->connector.state);
2018459cc2c6SThierry Reding 	mode = &encoder->crtc->state->adjusted_mode;
2019459cc2c6SThierry Reding 
2020aaff8bd2SThierry Reding 	pm_runtime_get_sync(sor->dev);
2021459cc2c6SThierry Reding 
202225bb2cecSThierry Reding 	/* switch to safe parent clock */
202325bb2cecSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
2024e1335e2fSThierry Reding 	if (err < 0) {
2025459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
2026e1335e2fSThierry Reding 		return;
2027e1335e2fSThierry Reding 	}
2028459cc2c6SThierry Reding 
2029459cc2c6SThierry Reding 	div = clk_get_rate(sor->clk) / 1000000 * 4;
2030459cc2c6SThierry Reding 
2031459cc2c6SThierry Reding 	err = tegra_io_rail_power_on(TEGRA_IO_RAIL_HDMI);
2032459cc2c6SThierry Reding 	if (err < 0)
2033459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to power on HDMI rail: %d\n", err);
2034459cc2c6SThierry Reding 
2035459cc2c6SThierry Reding 	usleep_range(20, 100);
2036459cc2c6SThierry Reding 
2037*880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
2038459cc2c6SThierry Reding 	value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
2039*880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
2040459cc2c6SThierry Reding 
2041459cc2c6SThierry Reding 	usleep_range(20, 100);
2042459cc2c6SThierry Reding 
2043*880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll3);
2044459cc2c6SThierry Reding 	value &= ~SOR_PLL3_PLL_VDD_MODE_3V3;
2045*880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll3);
2046459cc2c6SThierry Reding 
2047*880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll0);
2048459cc2c6SThierry Reding 	value &= ~SOR_PLL0_VCOPD;
2049459cc2c6SThierry Reding 	value &= ~SOR_PLL0_PWR;
2050*880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll0);
2051459cc2c6SThierry Reding 
2052*880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
2053459cc2c6SThierry Reding 	value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
2054*880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
2055459cc2c6SThierry Reding 
2056459cc2c6SThierry Reding 	usleep_range(200, 400);
2057459cc2c6SThierry Reding 
2058*880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll2);
2059459cc2c6SThierry Reding 	value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
2060459cc2c6SThierry Reding 	value &= ~SOR_PLL2_PORT_POWERDOWN;
2061*880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll2);
2062459cc2c6SThierry Reding 
2063459cc2c6SThierry Reding 	usleep_range(20, 100);
2064459cc2c6SThierry Reding 
2065*880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
2066459cc2c6SThierry Reding 	value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
2067459cc2c6SThierry Reding 		 SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2;
2068*880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
2069459cc2c6SThierry Reding 
2070459cc2c6SThierry Reding 	while (true) {
2071459cc2c6SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
2072459cc2c6SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_STATE_BUSY) == 0)
2073459cc2c6SThierry Reding 			break;
2074459cc2c6SThierry Reding 
2075459cc2c6SThierry Reding 		usleep_range(250, 1000);
2076459cc2c6SThierry Reding 	}
2077459cc2c6SThierry Reding 
2078459cc2c6SThierry Reding 	value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
2079459cc2c6SThierry Reding 		SOR_LANE_SEQ_CTL_POWER_STATE_UP | SOR_LANE_SEQ_CTL_DELAY(5);
2080459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
2081459cc2c6SThierry Reding 
2082459cc2c6SThierry Reding 	while (true) {
2083459cc2c6SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
2084459cc2c6SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
2085459cc2c6SThierry Reding 			break;
2086459cc2c6SThierry Reding 
2087459cc2c6SThierry Reding 		usleep_range(250, 1000);
2088459cc2c6SThierry Reding 	}
2089459cc2c6SThierry Reding 
2090459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
2091459cc2c6SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
2092459cc2c6SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
2093459cc2c6SThierry Reding 
2094459cc2c6SThierry Reding 	if (mode->clock < 340000)
2095459cc2c6SThierry Reding 		value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G2_70;
2096459cc2c6SThierry Reding 	else
2097459cc2c6SThierry Reding 		value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G5_40;
2098459cc2c6SThierry Reding 
2099459cc2c6SThierry Reding 	value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK;
2100459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
2101459cc2c6SThierry Reding 
2102459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_SPARE0);
2103459cc2c6SThierry Reding 	value |= SOR_DP_SPARE_DISP_VIDEO_PREAMBLE;
2104459cc2c6SThierry Reding 	value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
2105459cc2c6SThierry Reding 	value |= SOR_DP_SPARE_SEQ_ENABLE;
2106459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_SPARE0);
2107459cc2c6SThierry Reding 
2108459cc2c6SThierry Reding 	value = SOR_SEQ_CTL_PU_PC(0) | SOR_SEQ_CTL_PU_PC_ALT(0) |
2109459cc2c6SThierry Reding 		SOR_SEQ_CTL_PD_PC(8) | SOR_SEQ_CTL_PD_PC_ALT(8);
2110459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_SEQ_CTL);
2111459cc2c6SThierry Reding 
2112459cc2c6SThierry Reding 	value = SOR_SEQ_INST_DRIVE_PWM_OUT_LO | SOR_SEQ_INST_HALT |
2113459cc2c6SThierry Reding 		SOR_SEQ_INST_WAIT_VSYNC | SOR_SEQ_INST_WAIT(1);
2114459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_SEQ_INST(0));
2115459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_SEQ_INST(8));
2116459cc2c6SThierry Reding 
2117459cc2c6SThierry Reding 	/* program the reference clock */
2118459cc2c6SThierry Reding 	value = SOR_REFCLK_DIV_INT(div) | SOR_REFCLK_DIV_FRAC(div);
2119459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_REFCLK);
2120459cc2c6SThierry Reding 
212130b49435SThierry Reding 	/* XXX not in TRM */
212230b49435SThierry Reding 	for (value = 0, i = 0; i < 5; i++)
212330b49435SThierry Reding 		value |= SOR_XBAR_CTRL_LINK0_XSEL(i, sor->soc->xbar_cfg[i]) |
212430b49435SThierry Reding 			 SOR_XBAR_CTRL_LINK1_XSEL(i, i);
2125459cc2c6SThierry Reding 
2126459cc2c6SThierry Reding 	tegra_sor_writel(sor, 0x00000000, SOR_XBAR_POL);
212730b49435SThierry Reding 	tegra_sor_writel(sor, value, SOR_XBAR_CTRL);
2128459cc2c6SThierry Reding 
212925bb2cecSThierry Reding 	/* switch to parent clock */
2130e1335e2fSThierry Reding 	err = clk_set_parent(sor->clk, sor->clk_parent);
2131e1335e2fSThierry Reding 	if (err < 0) {
2132459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to set parent clock: %d\n", err);
2133e1335e2fSThierry Reding 		return;
2134e1335e2fSThierry Reding 	}
2135e1335e2fSThierry Reding 
2136e1335e2fSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_pad);
2137e1335e2fSThierry Reding 	if (err < 0) {
2138e1335e2fSThierry Reding 		dev_err(sor->dev, "failed to set pad clock: %d\n", err);
2139e1335e2fSThierry Reding 		return;
2140e1335e2fSThierry Reding 	}
2141459cc2c6SThierry Reding 
2142459cc2c6SThierry Reding 	value = SOR_INPUT_CONTROL_HDMI_SRC_SELECT(dc->pipe);
2143459cc2c6SThierry Reding 
2144459cc2c6SThierry Reding 	/* XXX is this the proper check? */
2145459cc2c6SThierry Reding 	if (mode->clock < 75000)
2146459cc2c6SThierry Reding 		value |= SOR_INPUT_CONTROL_ARM_VIDEO_RANGE_LIMITED;
2147459cc2c6SThierry Reding 
2148459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_INPUT_CONTROL);
2149459cc2c6SThierry Reding 
2150459cc2c6SThierry Reding 	max_ac = ((mode->htotal - mode->hdisplay) - SOR_REKEY - 18) / 32;
2151459cc2c6SThierry Reding 
2152459cc2c6SThierry Reding 	value = SOR_HDMI_CTRL_ENABLE | SOR_HDMI_CTRL_MAX_AC_PACKET(max_ac) |
2153459cc2c6SThierry Reding 		SOR_HDMI_CTRL_AUDIO_LAYOUT | SOR_HDMI_CTRL_REKEY(SOR_REKEY);
2154459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_CTRL);
2155459cc2c6SThierry Reding 
2156459cc2c6SThierry Reding 	/* H_PULSE2 setup */
2157459cc2c6SThierry Reding 	pulse_start = h_ref_to_sync + (mode->hsync_end - mode->hsync_start) +
2158459cc2c6SThierry Reding 		      (mode->htotal - mode->hsync_end) - 10;
2159459cc2c6SThierry Reding 
2160459cc2c6SThierry Reding 	value = PULSE_LAST_END_A | PULSE_QUAL_VACTIVE |
2161459cc2c6SThierry Reding 		PULSE_POLARITY_HIGH | PULSE_MODE_NORMAL;
2162459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_CONTROL);
2163459cc2c6SThierry Reding 
2164459cc2c6SThierry Reding 	value = PULSE_END(pulse_start + 8) | PULSE_START(pulse_start);
2165459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_POSITION_A);
2166459cc2c6SThierry Reding 
2167459cc2c6SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_SIGNAL_OPTIONS0);
2168459cc2c6SThierry Reding 	value |= H_PULSE2_ENABLE;
2169459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_SIGNAL_OPTIONS0);
2170459cc2c6SThierry Reding 
2171459cc2c6SThierry Reding 	/* infoframe setup */
2172459cc2c6SThierry Reding 	err = tegra_sor_hdmi_setup_avi_infoframe(sor, mode);
2173459cc2c6SThierry Reding 	if (err < 0)
2174459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to setup AVI infoframe: %d\n", err);
2175459cc2c6SThierry Reding 
2176459cc2c6SThierry Reding 	/* XXX HDMI audio support not implemented yet */
2177459cc2c6SThierry Reding 	tegra_sor_hdmi_disable_audio_infoframe(sor);
2178459cc2c6SThierry Reding 
2179459cc2c6SThierry Reding 	/* use single TMDS protocol */
2180459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
2181459cc2c6SThierry Reding 	value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
2182459cc2c6SThierry Reding 	value |= SOR_STATE_ASY_PROTOCOL_SINGLE_TMDS_A;
2183459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
2184459cc2c6SThierry Reding 
2185459cc2c6SThierry Reding 	/* power up pad calibration */
2186*880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
2187459cc2c6SThierry Reding 	value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
2188*880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
2189459cc2c6SThierry Reding 
2190459cc2c6SThierry Reding 	/* production settings */
2191459cc2c6SThierry Reding 	settings = tegra_sor_hdmi_find_settings(sor, mode->clock * 1000);
2192db8b42fbSDan Carpenter 	if (!settings) {
2193db8b42fbSDan Carpenter 		dev_err(sor->dev, "no settings for pixel clock %d Hz\n",
2194db8b42fbSDan Carpenter 			mode->clock * 1000);
2195459cc2c6SThierry Reding 		return;
2196459cc2c6SThierry Reding 	}
2197459cc2c6SThierry Reding 
2198*880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll0);
2199459cc2c6SThierry Reding 	value &= ~SOR_PLL0_ICHPMP_MASK;
2200459cc2c6SThierry Reding 	value &= ~SOR_PLL0_VCOCAP_MASK;
2201459cc2c6SThierry Reding 	value |= SOR_PLL0_ICHPMP(settings->ichpmp);
2202459cc2c6SThierry Reding 	value |= SOR_PLL0_VCOCAP(settings->vcocap);
2203*880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll0);
2204459cc2c6SThierry Reding 
2205459cc2c6SThierry Reding 	tegra_sor_dp_term_calibrate(sor);
2206459cc2c6SThierry Reding 
2207*880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll1);
2208459cc2c6SThierry Reding 	value &= ~SOR_PLL1_LOADADJ_MASK;
2209459cc2c6SThierry Reding 	value |= SOR_PLL1_LOADADJ(settings->loadadj);
2210*880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll1);
2211459cc2c6SThierry Reding 
2212*880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->pll3);
2213459cc2c6SThierry Reding 	value &= ~SOR_PLL3_BG_VREF_LEVEL_MASK;
2214459cc2c6SThierry Reding 	value |= SOR_PLL3_BG_VREF_LEVEL(settings->bg_vref);
2215*880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->pll3);
2216459cc2c6SThierry Reding 
2217459cc2c6SThierry Reding 	value = settings->drive_current[0] << 24 |
2218459cc2c6SThierry Reding 		settings->drive_current[1] << 16 |
2219459cc2c6SThierry Reding 		settings->drive_current[2] <<  8 |
2220459cc2c6SThierry Reding 		settings->drive_current[3] <<  0;
2221459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT0);
2222459cc2c6SThierry Reding 
2223459cc2c6SThierry Reding 	value = settings->preemphasis[0] << 24 |
2224459cc2c6SThierry Reding 		settings->preemphasis[1] << 16 |
2225459cc2c6SThierry Reding 		settings->preemphasis[2] <<  8 |
2226459cc2c6SThierry Reding 		settings->preemphasis[3] <<  0;
2227459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS0);
2228459cc2c6SThierry Reding 
2229*880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
2230459cc2c6SThierry Reding 	value &= ~SOR_DP_PADCTL_TX_PU_MASK;
2231459cc2c6SThierry Reding 	value |= SOR_DP_PADCTL_TX_PU_ENABLE;
2232459cc2c6SThierry Reding 	value |= SOR_DP_PADCTL_TX_PU(settings->tx_pu);
2233*880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
2234459cc2c6SThierry Reding 
2235459cc2c6SThierry Reding 	/* power down pad calibration */
2236*880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
2237459cc2c6SThierry Reding 	value |= SOR_DP_PADCTL_PAD_CAL_PD;
2238*880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
2239459cc2c6SThierry Reding 
2240459cc2c6SThierry Reding 	/* miscellaneous display controller settings */
2241459cc2c6SThierry Reding 	value = VSYNC_H_POSITION(1);
2242459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_TIMING_OPTIONS);
2243459cc2c6SThierry Reding 
2244459cc2c6SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_COLOR_CONTROL);
2245459cc2c6SThierry Reding 	value &= ~DITHER_CONTROL_MASK;
2246459cc2c6SThierry Reding 	value &= ~BASE_COLOR_SIZE_MASK;
2247459cc2c6SThierry Reding 
2248c31efa7aSThierry Reding 	switch (state->bpc) {
2249459cc2c6SThierry Reding 	case 6:
2250459cc2c6SThierry Reding 		value |= BASE_COLOR_SIZE_666;
2251459cc2c6SThierry Reding 		break;
2252459cc2c6SThierry Reding 
2253459cc2c6SThierry Reding 	case 8:
2254459cc2c6SThierry Reding 		value |= BASE_COLOR_SIZE_888;
2255459cc2c6SThierry Reding 		break;
2256459cc2c6SThierry Reding 
2257459cc2c6SThierry Reding 	default:
2258c31efa7aSThierry Reding 		WARN(1, "%u bits-per-color not supported\n", state->bpc);
2259c31efa7aSThierry Reding 		value |= BASE_COLOR_SIZE_888;
2260459cc2c6SThierry Reding 		break;
2261459cc2c6SThierry Reding 	}
2262459cc2c6SThierry Reding 
2263459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_COLOR_CONTROL);
2264459cc2c6SThierry Reding 
2265459cc2c6SThierry Reding 	err = tegra_sor_power_up(sor, 250);
2266459cc2c6SThierry Reding 	if (err < 0)
2267459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to power up SOR: %d\n", err);
2268459cc2c6SThierry Reding 
22692bd1dd39SThierry Reding 	/* configure dynamic range of output */
2270*880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->head_state0 + dc->pipe);
2271459cc2c6SThierry Reding 	value &= ~SOR_HEAD_STATE_RANGECOMPRESS_MASK;
2272459cc2c6SThierry Reding 	value &= ~SOR_HEAD_STATE_DYNRANGE_MASK;
2273*880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->head_state0 + dc->pipe);
2274459cc2c6SThierry Reding 
22752bd1dd39SThierry Reding 	/* configure colorspace */
2276*880cee0bSThierry Reding 	value = tegra_sor_readl(sor, sor->soc->regs->head_state0 + dc->pipe);
2277459cc2c6SThierry Reding 	value &= ~SOR_HEAD_STATE_COLORSPACE_MASK;
2278459cc2c6SThierry Reding 	value |= SOR_HEAD_STATE_COLORSPACE_RGB;
2279*880cee0bSThierry Reding 	tegra_sor_writel(sor, value, sor->soc->regs->head_state0 + dc->pipe);
2280459cc2c6SThierry Reding 
2281c31efa7aSThierry Reding 	tegra_sor_mode_set(sor, mode, state);
2282459cc2c6SThierry Reding 
2283459cc2c6SThierry Reding 	tegra_sor_update(sor);
2284459cc2c6SThierry Reding 
2285459cc2c6SThierry Reding 	err = tegra_sor_attach(sor);
2286459cc2c6SThierry Reding 	if (err < 0)
2287459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to attach SOR: %d\n", err);
2288459cc2c6SThierry Reding 
2289459cc2c6SThierry Reding 	/* enable display to SOR clock and generate HDMI preamble */
2290459cc2c6SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
2291459cc2c6SThierry Reding 	value |= SOR1_ENABLE | SOR1_TIMING_CYA;
2292459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
2293459cc2c6SThierry Reding 
2294459cc2c6SThierry Reding 	tegra_dc_commit(dc);
2295459cc2c6SThierry Reding 
2296459cc2c6SThierry Reding 	err = tegra_sor_wakeup(sor);
2297459cc2c6SThierry Reding 	if (err < 0)
2298459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to wakeup SOR: %d\n", err);
2299459cc2c6SThierry Reding }
2300459cc2c6SThierry Reding 
2301459cc2c6SThierry Reding static const struct drm_encoder_helper_funcs tegra_sor_hdmi_helpers = {
2302459cc2c6SThierry Reding 	.disable = tegra_sor_hdmi_disable,
2303459cc2c6SThierry Reding 	.enable = tegra_sor_hdmi_enable,
2304459cc2c6SThierry Reding 	.atomic_check = tegra_sor_encoder_atomic_check,
2305459cc2c6SThierry Reding };
2306459cc2c6SThierry Reding 
23076b6b6042SThierry Reding static int tegra_sor_init(struct host1x_client *client)
23086b6b6042SThierry Reding {
23099910f5c4SThierry Reding 	struct drm_device *drm = dev_get_drvdata(client->parent);
2310459cc2c6SThierry Reding 	const struct drm_encoder_helper_funcs *helpers = NULL;
23116b6b6042SThierry Reding 	struct tegra_sor *sor = host1x_client_to_sor(client);
2312459cc2c6SThierry Reding 	int connector = DRM_MODE_CONNECTOR_Unknown;
2313459cc2c6SThierry Reding 	int encoder = DRM_MODE_ENCODER_NONE;
23146b6b6042SThierry Reding 	int err;
23156b6b6042SThierry Reding 
23169542c237SThierry Reding 	if (!sor->aux) {
2317459cc2c6SThierry Reding 		if (sor->soc->supports_hdmi) {
2318459cc2c6SThierry Reding 			connector = DRM_MODE_CONNECTOR_HDMIA;
2319459cc2c6SThierry Reding 			encoder = DRM_MODE_ENCODER_TMDS;
2320459cc2c6SThierry Reding 			helpers = &tegra_sor_hdmi_helpers;
2321459cc2c6SThierry Reding 		} else if (sor->soc->supports_lvds) {
2322459cc2c6SThierry Reding 			connector = DRM_MODE_CONNECTOR_LVDS;
2323459cc2c6SThierry Reding 			encoder = DRM_MODE_ENCODER_LVDS;
2324459cc2c6SThierry Reding 		}
2325459cc2c6SThierry Reding 	} else {
2326459cc2c6SThierry Reding 		if (sor->soc->supports_edp) {
2327459cc2c6SThierry Reding 			connector = DRM_MODE_CONNECTOR_eDP;
2328459cc2c6SThierry Reding 			encoder = DRM_MODE_ENCODER_TMDS;
2329459cc2c6SThierry Reding 			helpers = &tegra_sor_edp_helpers;
2330459cc2c6SThierry Reding 		} else if (sor->soc->supports_dp) {
2331459cc2c6SThierry Reding 			connector = DRM_MODE_CONNECTOR_DisplayPort;
2332459cc2c6SThierry Reding 			encoder = DRM_MODE_ENCODER_TMDS;
2333459cc2c6SThierry Reding 		}
2334459cc2c6SThierry Reding 	}
23356b6b6042SThierry Reding 
23366b6b6042SThierry Reding 	sor->output.dev = sor->dev;
23376b6b6042SThierry Reding 
23386fad8f66SThierry Reding 	drm_connector_init(drm, &sor->output.connector,
23396fad8f66SThierry Reding 			   &tegra_sor_connector_funcs,
2340459cc2c6SThierry Reding 			   connector);
23416fad8f66SThierry Reding 	drm_connector_helper_add(&sor->output.connector,
23426fad8f66SThierry Reding 				 &tegra_sor_connector_helper_funcs);
23436fad8f66SThierry Reding 	sor->output.connector.dpms = DRM_MODE_DPMS_OFF;
23446fad8f66SThierry Reding 
23456fad8f66SThierry Reding 	drm_encoder_init(drm, &sor->output.encoder, &tegra_sor_encoder_funcs,
234613a3d91fSVille Syrjälä 			 encoder, NULL);
2347459cc2c6SThierry Reding 	drm_encoder_helper_add(&sor->output.encoder, helpers);
23486fad8f66SThierry Reding 
23496fad8f66SThierry Reding 	drm_mode_connector_attach_encoder(&sor->output.connector,
23506fad8f66SThierry Reding 					  &sor->output.encoder);
23516fad8f66SThierry Reding 	drm_connector_register(&sor->output.connector);
23526fad8f66SThierry Reding 
2353ea130b24SThierry Reding 	err = tegra_output_init(drm, &sor->output);
2354ea130b24SThierry Reding 	if (err < 0) {
2355ea130b24SThierry Reding 		dev_err(client->dev, "failed to initialize output: %d\n", err);
2356ea130b24SThierry Reding 		return err;
2357ea130b24SThierry Reding 	}
23586fad8f66SThierry Reding 
2359ea130b24SThierry Reding 	sor->output.encoder.possible_crtcs = 0x3;
23606b6b6042SThierry Reding 
23619542c237SThierry Reding 	if (sor->aux) {
23629542c237SThierry Reding 		err = drm_dp_aux_attach(sor->aux, &sor->output);
23636b6b6042SThierry Reding 		if (err < 0) {
23646b6b6042SThierry Reding 			dev_err(sor->dev, "failed to attach DP: %d\n", err);
23656b6b6042SThierry Reding 			return err;
23666b6b6042SThierry Reding 		}
23676b6b6042SThierry Reding 	}
23686b6b6042SThierry Reding 
2369535a65dbSTomeu Vizoso 	/*
2370535a65dbSTomeu Vizoso 	 * XXX: Remove this reset once proper hand-over from firmware to
2371535a65dbSTomeu Vizoso 	 * kernel is possible.
2372535a65dbSTomeu Vizoso 	 */
2373f8c79120SJon Hunter 	if (sor->rst) {
2374535a65dbSTomeu Vizoso 		err = reset_control_assert(sor->rst);
2375535a65dbSTomeu Vizoso 		if (err < 0) {
2376f8c79120SJon Hunter 			dev_err(sor->dev, "failed to assert SOR reset: %d\n",
2377f8c79120SJon Hunter 				err);
2378535a65dbSTomeu Vizoso 			return err;
2379535a65dbSTomeu Vizoso 		}
2380f8c79120SJon Hunter 	}
2381535a65dbSTomeu Vizoso 
23826fad8f66SThierry Reding 	err = clk_prepare_enable(sor->clk);
23836fad8f66SThierry Reding 	if (err < 0) {
23846fad8f66SThierry Reding 		dev_err(sor->dev, "failed to enable clock: %d\n", err);
23856fad8f66SThierry Reding 		return err;
23866fad8f66SThierry Reding 	}
23876fad8f66SThierry Reding 
2388535a65dbSTomeu Vizoso 	usleep_range(1000, 3000);
2389535a65dbSTomeu Vizoso 
2390f8c79120SJon Hunter 	if (sor->rst) {
2391535a65dbSTomeu Vizoso 		err = reset_control_deassert(sor->rst);
2392535a65dbSTomeu Vizoso 		if (err < 0) {
2393f8c79120SJon Hunter 			dev_err(sor->dev, "failed to deassert SOR reset: %d\n",
2394f8c79120SJon Hunter 				err);
2395535a65dbSTomeu Vizoso 			return err;
2396535a65dbSTomeu Vizoso 		}
2397f8c79120SJon Hunter 	}
2398535a65dbSTomeu Vizoso 
23996fad8f66SThierry Reding 	err = clk_prepare_enable(sor->clk_safe);
24006fad8f66SThierry Reding 	if (err < 0)
24016fad8f66SThierry Reding 		return err;
24026fad8f66SThierry Reding 
24036fad8f66SThierry Reding 	err = clk_prepare_enable(sor->clk_dp);
24046fad8f66SThierry Reding 	if (err < 0)
24056fad8f66SThierry Reding 		return err;
24066fad8f66SThierry Reding 
24076b6b6042SThierry Reding 	return 0;
24086b6b6042SThierry Reding }
24096b6b6042SThierry Reding 
24106b6b6042SThierry Reding static int tegra_sor_exit(struct host1x_client *client)
24116b6b6042SThierry Reding {
24126b6b6042SThierry Reding 	struct tegra_sor *sor = host1x_client_to_sor(client);
24136b6b6042SThierry Reding 	int err;
24146b6b6042SThierry Reding 
2415328ec69eSThierry Reding 	tegra_output_exit(&sor->output);
2416328ec69eSThierry Reding 
24179542c237SThierry Reding 	if (sor->aux) {
24189542c237SThierry Reding 		err = drm_dp_aux_detach(sor->aux);
24196b6b6042SThierry Reding 		if (err < 0) {
24206b6b6042SThierry Reding 			dev_err(sor->dev, "failed to detach DP: %d\n", err);
24216b6b6042SThierry Reding 			return err;
24226b6b6042SThierry Reding 		}
24236b6b6042SThierry Reding 	}
24246b6b6042SThierry Reding 
24256fad8f66SThierry Reding 	clk_disable_unprepare(sor->clk_safe);
24266fad8f66SThierry Reding 	clk_disable_unprepare(sor->clk_dp);
24276fad8f66SThierry Reding 	clk_disable_unprepare(sor->clk);
24286fad8f66SThierry Reding 
24296b6b6042SThierry Reding 	return 0;
24306b6b6042SThierry Reding }
24316b6b6042SThierry Reding 
24326b6b6042SThierry Reding static const struct host1x_client_ops sor_client_ops = {
24336b6b6042SThierry Reding 	.init = tegra_sor_init,
24346b6b6042SThierry Reding 	.exit = tegra_sor_exit,
24356b6b6042SThierry Reding };
24366b6b6042SThierry Reding 
2437459cc2c6SThierry Reding static const struct tegra_sor_ops tegra_sor_edp_ops = {
2438459cc2c6SThierry Reding 	.name = "eDP",
2439459cc2c6SThierry Reding };
2440459cc2c6SThierry Reding 
2441459cc2c6SThierry Reding static int tegra_sor_hdmi_probe(struct tegra_sor *sor)
2442459cc2c6SThierry Reding {
2443459cc2c6SThierry Reding 	int err;
2444459cc2c6SThierry Reding 
2445459cc2c6SThierry Reding 	sor->avdd_io_supply = devm_regulator_get(sor->dev, "avdd-io");
2446459cc2c6SThierry Reding 	if (IS_ERR(sor->avdd_io_supply)) {
2447459cc2c6SThierry Reding 		dev_err(sor->dev, "cannot get AVDD I/O supply: %ld\n",
2448459cc2c6SThierry Reding 			PTR_ERR(sor->avdd_io_supply));
2449459cc2c6SThierry Reding 		return PTR_ERR(sor->avdd_io_supply);
2450459cc2c6SThierry Reding 	}
2451459cc2c6SThierry Reding 
2452459cc2c6SThierry Reding 	err = regulator_enable(sor->avdd_io_supply);
2453459cc2c6SThierry Reding 	if (err < 0) {
2454459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to enable AVDD I/O supply: %d\n",
2455459cc2c6SThierry Reding 			err);
2456459cc2c6SThierry Reding 		return err;
2457459cc2c6SThierry Reding 	}
2458459cc2c6SThierry Reding 
2459459cc2c6SThierry Reding 	sor->vdd_pll_supply = devm_regulator_get(sor->dev, "vdd-pll");
2460459cc2c6SThierry Reding 	if (IS_ERR(sor->vdd_pll_supply)) {
2461459cc2c6SThierry Reding 		dev_err(sor->dev, "cannot get VDD PLL supply: %ld\n",
2462459cc2c6SThierry Reding 			PTR_ERR(sor->vdd_pll_supply));
2463459cc2c6SThierry Reding 		return PTR_ERR(sor->vdd_pll_supply);
2464459cc2c6SThierry Reding 	}
2465459cc2c6SThierry Reding 
2466459cc2c6SThierry Reding 	err = regulator_enable(sor->vdd_pll_supply);
2467459cc2c6SThierry Reding 	if (err < 0) {
2468459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to enable VDD PLL supply: %d\n",
2469459cc2c6SThierry Reding 			err);
2470459cc2c6SThierry Reding 		return err;
2471459cc2c6SThierry Reding 	}
2472459cc2c6SThierry Reding 
2473459cc2c6SThierry Reding 	sor->hdmi_supply = devm_regulator_get(sor->dev, "hdmi");
2474459cc2c6SThierry Reding 	if (IS_ERR(sor->hdmi_supply)) {
2475459cc2c6SThierry Reding 		dev_err(sor->dev, "cannot get HDMI supply: %ld\n",
2476459cc2c6SThierry Reding 			PTR_ERR(sor->hdmi_supply));
2477459cc2c6SThierry Reding 		return PTR_ERR(sor->hdmi_supply);
2478459cc2c6SThierry Reding 	}
2479459cc2c6SThierry Reding 
2480459cc2c6SThierry Reding 	err = regulator_enable(sor->hdmi_supply);
2481459cc2c6SThierry Reding 	if (err < 0) {
2482459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to enable HDMI supply: %d\n", err);
2483459cc2c6SThierry Reding 		return err;
2484459cc2c6SThierry Reding 	}
2485459cc2c6SThierry Reding 
2486459cc2c6SThierry Reding 	return 0;
2487459cc2c6SThierry Reding }
2488459cc2c6SThierry Reding 
2489459cc2c6SThierry Reding static int tegra_sor_hdmi_remove(struct tegra_sor *sor)
2490459cc2c6SThierry Reding {
2491459cc2c6SThierry Reding 	regulator_disable(sor->hdmi_supply);
2492459cc2c6SThierry Reding 	regulator_disable(sor->vdd_pll_supply);
2493459cc2c6SThierry Reding 	regulator_disable(sor->avdd_io_supply);
2494459cc2c6SThierry Reding 
2495459cc2c6SThierry Reding 	return 0;
2496459cc2c6SThierry Reding }
2497459cc2c6SThierry Reding 
2498459cc2c6SThierry Reding static const struct tegra_sor_ops tegra_sor_hdmi_ops = {
2499459cc2c6SThierry Reding 	.name = "HDMI",
2500459cc2c6SThierry Reding 	.probe = tegra_sor_hdmi_probe,
2501459cc2c6SThierry Reding 	.remove = tegra_sor_hdmi_remove,
2502459cc2c6SThierry Reding };
2503459cc2c6SThierry Reding 
250430b49435SThierry Reding static const u8 tegra124_sor_xbar_cfg[5] = {
250530b49435SThierry Reding 	0, 1, 2, 3, 4
250630b49435SThierry Reding };
250730b49435SThierry Reding 
2508*880cee0bSThierry Reding static const struct tegra_sor_regs tegra124_sor_regs = {
2509*880cee0bSThierry Reding 	.head_state0 = 0x05,
2510*880cee0bSThierry Reding 	.head_state1 = 0x07,
2511*880cee0bSThierry Reding 	.head_state2 = 0x09,
2512*880cee0bSThierry Reding 	.head_state3 = 0x0b,
2513*880cee0bSThierry Reding 	.head_state4 = 0x0d,
2514*880cee0bSThierry Reding 	.head_state5 = 0x0f,
2515*880cee0bSThierry Reding 	.pll0 = 0x17,
2516*880cee0bSThierry Reding 	.pll1 = 0x18,
2517*880cee0bSThierry Reding 	.pll2 = 0x19,
2518*880cee0bSThierry Reding 	.pll3 = 0x1a,
2519*880cee0bSThierry Reding 	.dp_padctl0 = 0x5c,
2520*880cee0bSThierry Reding 	.dp_padctl2 = 0x73,
2521*880cee0bSThierry Reding };
2522*880cee0bSThierry Reding 
2523459cc2c6SThierry Reding static const struct tegra_sor_soc tegra124_sor = {
2524459cc2c6SThierry Reding 	.supports_edp = true,
2525459cc2c6SThierry Reding 	.supports_lvds = true,
2526459cc2c6SThierry Reding 	.supports_hdmi = false,
2527459cc2c6SThierry Reding 	.supports_dp = false,
2528*880cee0bSThierry Reding 	.regs = &tegra124_sor_regs,
252930b49435SThierry Reding 	.xbar_cfg = tegra124_sor_xbar_cfg,
2530459cc2c6SThierry Reding };
2531459cc2c6SThierry Reding 
2532*880cee0bSThierry Reding static const struct tegra_sor_regs tegra210_sor_regs = {
2533*880cee0bSThierry Reding 	.head_state0 = 0x05,
2534*880cee0bSThierry Reding 	.head_state1 = 0x07,
2535*880cee0bSThierry Reding 	.head_state2 = 0x09,
2536*880cee0bSThierry Reding 	.head_state3 = 0x0b,
2537*880cee0bSThierry Reding 	.head_state4 = 0x0d,
2538*880cee0bSThierry Reding 	.head_state5 = 0x0f,
2539*880cee0bSThierry Reding 	.pll0 = 0x17,
2540*880cee0bSThierry Reding 	.pll1 = 0x18,
2541*880cee0bSThierry Reding 	.pll2 = 0x19,
2542*880cee0bSThierry Reding 	.pll3 = 0x1a,
2543*880cee0bSThierry Reding 	.dp_padctl0 = 0x5c,
2544*880cee0bSThierry Reding 	.dp_padctl2 = 0x73,
2545*880cee0bSThierry Reding };
2546*880cee0bSThierry Reding 
2547459cc2c6SThierry Reding static const struct tegra_sor_soc tegra210_sor = {
2548459cc2c6SThierry Reding 	.supports_edp = true,
2549459cc2c6SThierry Reding 	.supports_lvds = false,
2550459cc2c6SThierry Reding 	.supports_hdmi = false,
2551459cc2c6SThierry Reding 	.supports_dp = false,
2552*880cee0bSThierry Reding 	.regs = &tegra210_sor_regs,
255330b49435SThierry Reding 	.xbar_cfg = tegra124_sor_xbar_cfg,
255430b49435SThierry Reding };
255530b49435SThierry Reding 
255630b49435SThierry Reding static const u8 tegra210_sor_xbar_cfg[5] = {
255730b49435SThierry Reding 	2, 1, 0, 3, 4
2558459cc2c6SThierry Reding };
2559459cc2c6SThierry Reding 
2560459cc2c6SThierry Reding static const struct tegra_sor_soc tegra210_sor1 = {
2561459cc2c6SThierry Reding 	.supports_edp = false,
2562459cc2c6SThierry Reding 	.supports_lvds = false,
2563459cc2c6SThierry Reding 	.supports_hdmi = true,
2564459cc2c6SThierry Reding 	.supports_dp = true,
2565459cc2c6SThierry Reding 
2566*880cee0bSThierry Reding 	.regs = &tegra210_sor_regs,
2567*880cee0bSThierry Reding 
2568459cc2c6SThierry Reding 	.num_settings = ARRAY_SIZE(tegra210_sor_hdmi_defaults),
2569459cc2c6SThierry Reding 	.settings = tegra210_sor_hdmi_defaults,
257030b49435SThierry Reding 
257130b49435SThierry Reding 	.xbar_cfg = tegra210_sor_xbar_cfg,
2572459cc2c6SThierry Reding };
2573459cc2c6SThierry Reding 
2574459cc2c6SThierry Reding static const struct of_device_id tegra_sor_of_match[] = {
2575459cc2c6SThierry Reding 	{ .compatible = "nvidia,tegra210-sor1", .data = &tegra210_sor1 },
2576459cc2c6SThierry Reding 	{ .compatible = "nvidia,tegra210-sor", .data = &tegra210_sor },
2577459cc2c6SThierry Reding 	{ .compatible = "nvidia,tegra124-sor", .data = &tegra124_sor },
2578459cc2c6SThierry Reding 	{ },
2579459cc2c6SThierry Reding };
2580459cc2c6SThierry Reding MODULE_DEVICE_TABLE(of, tegra_sor_of_match);
2581459cc2c6SThierry Reding 
25826b6b6042SThierry Reding static int tegra_sor_probe(struct platform_device *pdev)
25836b6b6042SThierry Reding {
25846b6b6042SThierry Reding 	struct device_node *np;
25856b6b6042SThierry Reding 	struct tegra_sor *sor;
25866b6b6042SThierry Reding 	struct resource *regs;
25876b6b6042SThierry Reding 	int err;
25886b6b6042SThierry Reding 
25896b6b6042SThierry Reding 	sor = devm_kzalloc(&pdev->dev, sizeof(*sor), GFP_KERNEL);
25906b6b6042SThierry Reding 	if (!sor)
25916b6b6042SThierry Reding 		return -ENOMEM;
25926b6b6042SThierry Reding 
25935faea3d0SThierry Reding 	sor->soc = of_device_get_match_data(&pdev->dev);
25946b6b6042SThierry Reding 	sor->output.dev = sor->dev = &pdev->dev;
2595459cc2c6SThierry Reding 
2596459cc2c6SThierry Reding 	sor->settings = devm_kmemdup(&pdev->dev, sor->soc->settings,
2597459cc2c6SThierry Reding 				     sor->soc->num_settings *
2598459cc2c6SThierry Reding 					sizeof(*sor->settings),
2599459cc2c6SThierry Reding 				     GFP_KERNEL);
2600459cc2c6SThierry Reding 	if (!sor->settings)
2601459cc2c6SThierry Reding 		return -ENOMEM;
2602459cc2c6SThierry Reding 
2603459cc2c6SThierry Reding 	sor->num_settings = sor->soc->num_settings;
26046b6b6042SThierry Reding 
26056b6b6042SThierry Reding 	np = of_parse_phandle(pdev->dev.of_node, "nvidia,dpaux", 0);
26066b6b6042SThierry Reding 	if (np) {
26079542c237SThierry Reding 		sor->aux = drm_dp_aux_find_by_of_node(np);
26086b6b6042SThierry Reding 		of_node_put(np);
26096b6b6042SThierry Reding 
26109542c237SThierry Reding 		if (!sor->aux)
26116b6b6042SThierry Reding 			return -EPROBE_DEFER;
26126b6b6042SThierry Reding 	}
26136b6b6042SThierry Reding 
26149542c237SThierry Reding 	if (!sor->aux) {
2615459cc2c6SThierry Reding 		if (sor->soc->supports_hdmi) {
2616459cc2c6SThierry Reding 			sor->ops = &tegra_sor_hdmi_ops;
2617459cc2c6SThierry Reding 		} else if (sor->soc->supports_lvds) {
2618459cc2c6SThierry Reding 			dev_err(&pdev->dev, "LVDS not supported yet\n");
2619459cc2c6SThierry Reding 			return -ENODEV;
2620459cc2c6SThierry Reding 		} else {
2621459cc2c6SThierry Reding 			dev_err(&pdev->dev, "unknown (non-DP) support\n");
2622459cc2c6SThierry Reding 			return -ENODEV;
2623459cc2c6SThierry Reding 		}
2624459cc2c6SThierry Reding 	} else {
2625459cc2c6SThierry Reding 		if (sor->soc->supports_edp) {
2626459cc2c6SThierry Reding 			sor->ops = &tegra_sor_edp_ops;
2627459cc2c6SThierry Reding 		} else if (sor->soc->supports_dp) {
2628459cc2c6SThierry Reding 			dev_err(&pdev->dev, "DisplayPort not supported yet\n");
2629459cc2c6SThierry Reding 			return -ENODEV;
2630459cc2c6SThierry Reding 		} else {
2631459cc2c6SThierry Reding 			dev_err(&pdev->dev, "unknown (DP) support\n");
2632459cc2c6SThierry Reding 			return -ENODEV;
2633459cc2c6SThierry Reding 		}
2634459cc2c6SThierry Reding 	}
2635459cc2c6SThierry Reding 
26366b6b6042SThierry Reding 	err = tegra_output_probe(&sor->output);
26374dbdc740SThierry Reding 	if (err < 0) {
26384dbdc740SThierry Reding 		dev_err(&pdev->dev, "failed to probe output: %d\n", err);
26396b6b6042SThierry Reding 		return err;
26404dbdc740SThierry Reding 	}
26416b6b6042SThierry Reding 
2642459cc2c6SThierry Reding 	if (sor->ops && sor->ops->probe) {
2643459cc2c6SThierry Reding 		err = sor->ops->probe(sor);
2644459cc2c6SThierry Reding 		if (err < 0) {
2645459cc2c6SThierry Reding 			dev_err(&pdev->dev, "failed to probe %s: %d\n",
2646459cc2c6SThierry Reding 				sor->ops->name, err);
2647459cc2c6SThierry Reding 			goto output;
2648459cc2c6SThierry Reding 		}
2649459cc2c6SThierry Reding 	}
2650459cc2c6SThierry Reding 
26516b6b6042SThierry Reding 	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
26526b6b6042SThierry Reding 	sor->regs = devm_ioremap_resource(&pdev->dev, regs);
2653459cc2c6SThierry Reding 	if (IS_ERR(sor->regs)) {
2654459cc2c6SThierry Reding 		err = PTR_ERR(sor->regs);
2655459cc2c6SThierry Reding 		goto remove;
2656459cc2c6SThierry Reding 	}
26576b6b6042SThierry Reding 
2658f8c79120SJon Hunter 	if (!pdev->dev.pm_domain) {
26596b6b6042SThierry Reding 		sor->rst = devm_reset_control_get(&pdev->dev, "sor");
26604dbdc740SThierry Reding 		if (IS_ERR(sor->rst)) {
2661459cc2c6SThierry Reding 			err = PTR_ERR(sor->rst);
2662f8c79120SJon Hunter 			dev_err(&pdev->dev, "failed to get reset control: %d\n",
2663f8c79120SJon Hunter 				err);
2664459cc2c6SThierry Reding 			goto remove;
26654dbdc740SThierry Reding 		}
2666f8c79120SJon Hunter 	}
26676b6b6042SThierry Reding 
26686b6b6042SThierry Reding 	sor->clk = devm_clk_get(&pdev->dev, NULL);
26694dbdc740SThierry Reding 	if (IS_ERR(sor->clk)) {
2670459cc2c6SThierry Reding 		err = PTR_ERR(sor->clk);
2671459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get module clock: %d\n", err);
2672459cc2c6SThierry Reding 		goto remove;
26734dbdc740SThierry Reding 	}
26746b6b6042SThierry Reding 
2675618dee39SThierry Reding 	if (sor->soc->supports_hdmi || sor->soc->supports_dp) {
2676e1335e2fSThierry Reding 		struct device_node *np = pdev->dev.of_node;
2677e1335e2fSThierry Reding 		const char *name;
2678e1335e2fSThierry Reding 
2679e1335e2fSThierry Reding 		/*
2680e1335e2fSThierry Reding 		 * For backwards compatibility with Tegra210 device trees,
2681e1335e2fSThierry Reding 		 * fall back to the old clock name "source" if the new "out"
2682e1335e2fSThierry Reding 		 * clock is not available.
2683e1335e2fSThierry Reding 		 */
2684e1335e2fSThierry Reding 		if (of_property_match_string(np, "clock-names", "out") < 0)
2685e1335e2fSThierry Reding 			name = "source";
2686e1335e2fSThierry Reding 		else
2687e1335e2fSThierry Reding 			name = "out";
2688e1335e2fSThierry Reding 
2689e1335e2fSThierry Reding 		sor->clk_out = devm_clk_get(&pdev->dev, name);
2690e1335e2fSThierry Reding 		if (IS_ERR(sor->clk_out)) {
2691e1335e2fSThierry Reding 			err = PTR_ERR(sor->clk_out);
2692e1335e2fSThierry Reding 			dev_err(sor->dev, "failed to get %s clock: %d\n",
2693e1335e2fSThierry Reding 				name, err);
2694618dee39SThierry Reding 			goto remove;
2695618dee39SThierry Reding 		}
2696618dee39SThierry Reding 	}
2697618dee39SThierry Reding 
26986b6b6042SThierry Reding 	sor->clk_parent = devm_clk_get(&pdev->dev, "parent");
26994dbdc740SThierry Reding 	if (IS_ERR(sor->clk_parent)) {
2700459cc2c6SThierry Reding 		err = PTR_ERR(sor->clk_parent);
2701459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get parent clock: %d\n", err);
2702459cc2c6SThierry Reding 		goto remove;
27034dbdc740SThierry Reding 	}
27046b6b6042SThierry Reding 
27056b6b6042SThierry Reding 	sor->clk_safe = devm_clk_get(&pdev->dev, "safe");
27064dbdc740SThierry Reding 	if (IS_ERR(sor->clk_safe)) {
2707459cc2c6SThierry Reding 		err = PTR_ERR(sor->clk_safe);
2708459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get safe clock: %d\n", err);
2709459cc2c6SThierry Reding 		goto remove;
27104dbdc740SThierry Reding 	}
27116b6b6042SThierry Reding 
27126b6b6042SThierry Reding 	sor->clk_dp = devm_clk_get(&pdev->dev, "dp");
27134dbdc740SThierry Reding 	if (IS_ERR(sor->clk_dp)) {
2714459cc2c6SThierry Reding 		err = PTR_ERR(sor->clk_dp);
2715459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get DP clock: %d\n", err);
2716459cc2c6SThierry Reding 		goto remove;
27174dbdc740SThierry Reding 	}
27186b6b6042SThierry Reding 
2719e1335e2fSThierry Reding 	/*
2720e1335e2fSThierry Reding 	 * Starting with Tegra186, the BPMP provides an implementation for
2721e1335e2fSThierry Reding 	 * the pad output clock, so we have to look it up from device tree.
2722e1335e2fSThierry Reding 	 */
2723e1335e2fSThierry Reding 	sor->clk_pad = devm_clk_get(&pdev->dev, "pad");
2724e1335e2fSThierry Reding 	if (IS_ERR(sor->clk_pad)) {
2725e1335e2fSThierry Reding 		if (sor->clk_pad != ERR_PTR(-ENOENT)) {
2726e1335e2fSThierry Reding 			err = PTR_ERR(sor->clk_pad);
2727e1335e2fSThierry Reding 			goto remove;
2728e1335e2fSThierry Reding 		}
2729e1335e2fSThierry Reding 
2730e1335e2fSThierry Reding 		/*
2731e1335e2fSThierry Reding 		 * If the pad output clock is not available, then we assume
2732e1335e2fSThierry Reding 		 * we're on Tegra210 or earlier and have to provide our own
2733e1335e2fSThierry Reding 		 * implementation.
2734e1335e2fSThierry Reding 		 */
2735e1335e2fSThierry Reding 		sor->clk_pad = NULL;
2736e1335e2fSThierry Reding 	}
2737e1335e2fSThierry Reding 
2738e1335e2fSThierry Reding 	/*
2739e1335e2fSThierry Reding 	 * The bootloader may have set up the SOR such that it's module clock
2740e1335e2fSThierry Reding 	 * is sourced by one of the display PLLs. However, that doesn't work
2741e1335e2fSThierry Reding 	 * without properly having set up other bits of the SOR.
2742e1335e2fSThierry Reding 	 */
2743e1335e2fSThierry Reding 	err = clk_set_parent(sor->clk_out, sor->clk_safe);
2744e1335e2fSThierry Reding 	if (err < 0) {
2745e1335e2fSThierry Reding 		dev_err(&pdev->dev, "failed to use safe clock: %d\n", err);
2746e1335e2fSThierry Reding 		goto remove;
2747e1335e2fSThierry Reding 	}
2748e1335e2fSThierry Reding 
2749aaff8bd2SThierry Reding 	platform_set_drvdata(pdev, sor);
2750aaff8bd2SThierry Reding 	pm_runtime_enable(&pdev->dev);
2751aaff8bd2SThierry Reding 
2752e1335e2fSThierry Reding 	/*
2753e1335e2fSThierry Reding 	 * On Tegra210 and earlier, provide our own implementation for the
2754e1335e2fSThierry Reding 	 * pad output clock.
2755e1335e2fSThierry Reding 	 */
2756e1335e2fSThierry Reding 	if (!sor->clk_pad) {
2757e1335e2fSThierry Reding 		err = pm_runtime_get_sync(&pdev->dev);
2758e1335e2fSThierry Reding 		if (err < 0) {
2759e1335e2fSThierry Reding 			dev_err(&pdev->dev, "failed to get runtime PM: %d\n",
2760e1335e2fSThierry Reding 				err);
2761e1335e2fSThierry Reding 			goto remove;
2762e1335e2fSThierry Reding 		}
2763b299221cSThierry Reding 
2764e1335e2fSThierry Reding 		sor->clk_pad = tegra_clk_sor_pad_register(sor,
2765e1335e2fSThierry Reding 							  "sor1_pad_clkout");
2766e1335e2fSThierry Reding 		pm_runtime_put(&pdev->dev);
2767e1335e2fSThierry Reding 	}
2768e1335e2fSThierry Reding 
2769e1335e2fSThierry Reding 	if (IS_ERR(sor->clk_pad)) {
2770e1335e2fSThierry Reding 		err = PTR_ERR(sor->clk_pad);
2771e1335e2fSThierry Reding 		dev_err(&pdev->dev, "failed to register SOR pad clock: %d\n",
2772e1335e2fSThierry Reding 			err);
2773b299221cSThierry Reding 		goto remove;
2774b299221cSThierry Reding 	}
2775b299221cSThierry Reding 
27766b6b6042SThierry Reding 	INIT_LIST_HEAD(&sor->client.list);
27776b6b6042SThierry Reding 	sor->client.ops = &sor_client_ops;
27786b6b6042SThierry Reding 	sor->client.dev = &pdev->dev;
27796b6b6042SThierry Reding 
27806b6b6042SThierry Reding 	err = host1x_client_register(&sor->client);
27816b6b6042SThierry Reding 	if (err < 0) {
27826b6b6042SThierry Reding 		dev_err(&pdev->dev, "failed to register host1x client: %d\n",
27836b6b6042SThierry Reding 			err);
2784459cc2c6SThierry Reding 		goto remove;
27856b6b6042SThierry Reding 	}
27866b6b6042SThierry Reding 
27876b6b6042SThierry Reding 	return 0;
2788459cc2c6SThierry Reding 
2789459cc2c6SThierry Reding remove:
2790459cc2c6SThierry Reding 	if (sor->ops && sor->ops->remove)
2791459cc2c6SThierry Reding 		sor->ops->remove(sor);
2792459cc2c6SThierry Reding output:
2793459cc2c6SThierry Reding 	tegra_output_remove(&sor->output);
2794459cc2c6SThierry Reding 	return err;
27956b6b6042SThierry Reding }
27966b6b6042SThierry Reding 
27976b6b6042SThierry Reding static int tegra_sor_remove(struct platform_device *pdev)
27986b6b6042SThierry Reding {
27996b6b6042SThierry Reding 	struct tegra_sor *sor = platform_get_drvdata(pdev);
28006b6b6042SThierry Reding 	int err;
28016b6b6042SThierry Reding 
2802aaff8bd2SThierry Reding 	pm_runtime_disable(&pdev->dev);
2803aaff8bd2SThierry Reding 
28046b6b6042SThierry Reding 	err = host1x_client_unregister(&sor->client);
28056b6b6042SThierry Reding 	if (err < 0) {
28066b6b6042SThierry Reding 		dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
28076b6b6042SThierry Reding 			err);
28086b6b6042SThierry Reding 		return err;
28096b6b6042SThierry Reding 	}
28106b6b6042SThierry Reding 
2811459cc2c6SThierry Reding 	if (sor->ops && sor->ops->remove) {
2812459cc2c6SThierry Reding 		err = sor->ops->remove(sor);
2813459cc2c6SThierry Reding 		if (err < 0)
2814459cc2c6SThierry Reding 			dev_err(&pdev->dev, "failed to remove SOR: %d\n", err);
2815459cc2c6SThierry Reding 	}
2816459cc2c6SThierry Reding 
2817328ec69eSThierry Reding 	tegra_output_remove(&sor->output);
28186b6b6042SThierry Reding 
28196b6b6042SThierry Reding 	return 0;
28206b6b6042SThierry Reding }
28216b6b6042SThierry Reding 
2822aaff8bd2SThierry Reding #ifdef CONFIG_PM
2823aaff8bd2SThierry Reding static int tegra_sor_suspend(struct device *dev)
2824aaff8bd2SThierry Reding {
2825aaff8bd2SThierry Reding 	struct tegra_sor *sor = dev_get_drvdata(dev);
2826aaff8bd2SThierry Reding 	int err;
2827aaff8bd2SThierry Reding 
2828f8c79120SJon Hunter 	if (sor->rst) {
2829aaff8bd2SThierry Reding 		err = reset_control_assert(sor->rst);
2830aaff8bd2SThierry Reding 		if (err < 0) {
2831aaff8bd2SThierry Reding 			dev_err(dev, "failed to assert reset: %d\n", err);
2832aaff8bd2SThierry Reding 			return err;
2833aaff8bd2SThierry Reding 		}
2834f8c79120SJon Hunter 	}
2835aaff8bd2SThierry Reding 
2836aaff8bd2SThierry Reding 	usleep_range(1000, 2000);
2837aaff8bd2SThierry Reding 
2838aaff8bd2SThierry Reding 	clk_disable_unprepare(sor->clk);
2839aaff8bd2SThierry Reding 
2840aaff8bd2SThierry Reding 	return 0;
2841aaff8bd2SThierry Reding }
2842aaff8bd2SThierry Reding 
2843aaff8bd2SThierry Reding static int tegra_sor_resume(struct device *dev)
2844aaff8bd2SThierry Reding {
2845aaff8bd2SThierry Reding 	struct tegra_sor *sor = dev_get_drvdata(dev);
2846aaff8bd2SThierry Reding 	int err;
2847aaff8bd2SThierry Reding 
2848aaff8bd2SThierry Reding 	err = clk_prepare_enable(sor->clk);
2849aaff8bd2SThierry Reding 	if (err < 0) {
2850aaff8bd2SThierry Reding 		dev_err(dev, "failed to enable clock: %d\n", err);
2851aaff8bd2SThierry Reding 		return err;
2852aaff8bd2SThierry Reding 	}
2853aaff8bd2SThierry Reding 
2854aaff8bd2SThierry Reding 	usleep_range(1000, 2000);
2855aaff8bd2SThierry Reding 
2856f8c79120SJon Hunter 	if (sor->rst) {
2857aaff8bd2SThierry Reding 		err = reset_control_deassert(sor->rst);
2858aaff8bd2SThierry Reding 		if (err < 0) {
2859aaff8bd2SThierry Reding 			dev_err(dev, "failed to deassert reset: %d\n", err);
2860aaff8bd2SThierry Reding 			clk_disable_unprepare(sor->clk);
2861aaff8bd2SThierry Reding 			return err;
2862aaff8bd2SThierry Reding 		}
2863f8c79120SJon Hunter 	}
2864aaff8bd2SThierry Reding 
2865aaff8bd2SThierry Reding 	return 0;
2866aaff8bd2SThierry Reding }
2867aaff8bd2SThierry Reding #endif
2868aaff8bd2SThierry Reding 
2869aaff8bd2SThierry Reding static const struct dev_pm_ops tegra_sor_pm_ops = {
2870aaff8bd2SThierry Reding 	SET_RUNTIME_PM_OPS(tegra_sor_suspend, tegra_sor_resume, NULL)
2871aaff8bd2SThierry Reding };
2872aaff8bd2SThierry Reding 
28736b6b6042SThierry Reding struct platform_driver tegra_sor_driver = {
28746b6b6042SThierry Reding 	.driver = {
28756b6b6042SThierry Reding 		.name = "tegra-sor",
28766b6b6042SThierry Reding 		.of_match_table = tegra_sor_of_match,
2877aaff8bd2SThierry Reding 		.pm = &tegra_sor_pm_ops,
28786b6b6042SThierry Reding 	},
28796b6b6042SThierry Reding 	.probe = tegra_sor_probe,
28806b6b6042SThierry Reding 	.remove = tegra_sor_remove,
28816b6b6042SThierry Reding };
2882