xref: /openbmc/linux/drivers/gpu/drm/tegra/sor.c (revision 30b4943558b9c482c30fc09cb899d6b03f67fb11)
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"
296b6b6042SThierry Reding 
30459cc2c6SThierry Reding #define SOR_REKEY 0x38
31459cc2c6SThierry Reding 
32459cc2c6SThierry Reding struct tegra_sor_hdmi_settings {
33459cc2c6SThierry Reding 	unsigned long frequency;
34459cc2c6SThierry Reding 
35459cc2c6SThierry Reding 	u8 vcocap;
36459cc2c6SThierry Reding 	u8 ichpmp;
37459cc2c6SThierry Reding 	u8 loadadj;
38459cc2c6SThierry Reding 	u8 termadj;
39459cc2c6SThierry Reding 	u8 tx_pu;
40459cc2c6SThierry Reding 	u8 bg_vref;
41459cc2c6SThierry Reding 
42459cc2c6SThierry Reding 	u8 drive_current[4];
43459cc2c6SThierry Reding 	u8 preemphasis[4];
44459cc2c6SThierry Reding };
45459cc2c6SThierry Reding 
46459cc2c6SThierry Reding #if 1
47459cc2c6SThierry Reding static const struct tegra_sor_hdmi_settings tegra210_sor_hdmi_defaults[] = {
48459cc2c6SThierry Reding 	{
49459cc2c6SThierry Reding 		.frequency = 54000000,
50459cc2c6SThierry Reding 		.vcocap = 0x0,
51459cc2c6SThierry Reding 		.ichpmp = 0x1,
52459cc2c6SThierry Reding 		.loadadj = 0x3,
53459cc2c6SThierry Reding 		.termadj = 0x9,
54459cc2c6SThierry Reding 		.tx_pu = 0x10,
55459cc2c6SThierry Reding 		.bg_vref = 0x8,
56459cc2c6SThierry Reding 		.drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
57459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
58459cc2c6SThierry Reding 	}, {
59459cc2c6SThierry Reding 		.frequency = 75000000,
60459cc2c6SThierry Reding 		.vcocap = 0x3,
61459cc2c6SThierry Reding 		.ichpmp = 0x1,
62459cc2c6SThierry Reding 		.loadadj = 0x3,
63459cc2c6SThierry Reding 		.termadj = 0x9,
64459cc2c6SThierry Reding 		.tx_pu = 0x40,
65459cc2c6SThierry Reding 		.bg_vref = 0x8,
66459cc2c6SThierry Reding 		.drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
67459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
68459cc2c6SThierry Reding 	}, {
69459cc2c6SThierry Reding 		.frequency = 150000000,
70459cc2c6SThierry Reding 		.vcocap = 0x3,
71459cc2c6SThierry Reding 		.ichpmp = 0x1,
72459cc2c6SThierry Reding 		.loadadj = 0x3,
73459cc2c6SThierry Reding 		.termadj = 0x9,
74459cc2c6SThierry Reding 		.tx_pu = 0x66,
75459cc2c6SThierry Reding 		.bg_vref = 0x8,
76459cc2c6SThierry Reding 		.drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
77459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
78459cc2c6SThierry Reding 	}, {
79459cc2c6SThierry Reding 		.frequency = 300000000,
80459cc2c6SThierry Reding 		.vcocap = 0x3,
81459cc2c6SThierry Reding 		.ichpmp = 0x1,
82459cc2c6SThierry Reding 		.loadadj = 0x3,
83459cc2c6SThierry Reding 		.termadj = 0x9,
84459cc2c6SThierry Reding 		.tx_pu = 0x66,
85459cc2c6SThierry Reding 		.bg_vref = 0xa,
86459cc2c6SThierry Reding 		.drive_current = { 0x33, 0x3f, 0x3f, 0x3f },
87459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x17, 0x17, 0x17 },
88459cc2c6SThierry Reding 	}, {
89459cc2c6SThierry Reding 		.frequency = 600000000,
90459cc2c6SThierry Reding 		.vcocap = 0x3,
91459cc2c6SThierry Reding 		.ichpmp = 0x1,
92459cc2c6SThierry Reding 		.loadadj = 0x3,
93459cc2c6SThierry Reding 		.termadj = 0x9,
94459cc2c6SThierry Reding 		.tx_pu = 0x66,
95459cc2c6SThierry Reding 		.bg_vref = 0x8,
96459cc2c6SThierry Reding 		.drive_current = { 0x33, 0x3f, 0x3f, 0x3f },
97459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
98459cc2c6SThierry Reding 	},
99459cc2c6SThierry Reding };
100459cc2c6SThierry Reding #else
101459cc2c6SThierry Reding static const struct tegra_sor_hdmi_settings tegra210_sor_hdmi_defaults[] = {
102459cc2c6SThierry Reding 	{
103459cc2c6SThierry Reding 		.frequency = 75000000,
104459cc2c6SThierry Reding 		.vcocap = 0x3,
105459cc2c6SThierry Reding 		.ichpmp = 0x1,
106459cc2c6SThierry Reding 		.loadadj = 0x3,
107459cc2c6SThierry Reding 		.termadj = 0x9,
108459cc2c6SThierry Reding 		.tx_pu = 0x40,
109459cc2c6SThierry Reding 		.bg_vref = 0x8,
110459cc2c6SThierry Reding 		.drive_current = { 0x29, 0x29, 0x29, 0x29 },
111459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
112459cc2c6SThierry Reding 	}, {
113459cc2c6SThierry Reding 		.frequency = 150000000,
114459cc2c6SThierry Reding 		.vcocap = 0x3,
115459cc2c6SThierry Reding 		.ichpmp = 0x1,
116459cc2c6SThierry Reding 		.loadadj = 0x3,
117459cc2c6SThierry Reding 		.termadj = 0x9,
118459cc2c6SThierry Reding 		.tx_pu = 0x66,
119459cc2c6SThierry Reding 		.bg_vref = 0x8,
120459cc2c6SThierry Reding 		.drive_current = { 0x30, 0x37, 0x37, 0x37 },
121459cc2c6SThierry Reding 		.preemphasis = { 0x01, 0x02, 0x02, 0x02 },
122459cc2c6SThierry Reding 	}, {
123459cc2c6SThierry Reding 		.frequency = 300000000,
124459cc2c6SThierry Reding 		.vcocap = 0x3,
125459cc2c6SThierry Reding 		.ichpmp = 0x6,
126459cc2c6SThierry Reding 		.loadadj = 0x3,
127459cc2c6SThierry Reding 		.termadj = 0x9,
128459cc2c6SThierry Reding 		.tx_pu = 0x66,
129459cc2c6SThierry Reding 		.bg_vref = 0xf,
130459cc2c6SThierry Reding 		.drive_current = { 0x30, 0x37, 0x37, 0x37 },
131459cc2c6SThierry Reding 		.preemphasis = { 0x10, 0x3e, 0x3e, 0x3e },
132459cc2c6SThierry Reding 	}, {
133459cc2c6SThierry Reding 		.frequency = 600000000,
134459cc2c6SThierry Reding 		.vcocap = 0x3,
135459cc2c6SThierry Reding 		.ichpmp = 0xa,
136459cc2c6SThierry Reding 		.loadadj = 0x3,
137459cc2c6SThierry Reding 		.termadj = 0xb,
138459cc2c6SThierry Reding 		.tx_pu = 0x66,
139459cc2c6SThierry Reding 		.bg_vref = 0xe,
140459cc2c6SThierry Reding 		.drive_current = { 0x35, 0x3e, 0x3e, 0x3e },
141459cc2c6SThierry Reding 		.preemphasis = { 0x02, 0x3f, 0x3f, 0x3f },
142459cc2c6SThierry Reding 	},
143459cc2c6SThierry Reding };
144459cc2c6SThierry Reding #endif
145459cc2c6SThierry Reding 
146459cc2c6SThierry Reding struct tegra_sor_soc {
147459cc2c6SThierry Reding 	bool supports_edp;
148459cc2c6SThierry Reding 	bool supports_lvds;
149459cc2c6SThierry Reding 	bool supports_hdmi;
150459cc2c6SThierry Reding 	bool supports_dp;
151459cc2c6SThierry Reding 
152459cc2c6SThierry Reding 	const struct tegra_sor_hdmi_settings *settings;
153459cc2c6SThierry Reding 	unsigned int num_settings;
154*30b49435SThierry Reding 
155*30b49435SThierry Reding 	const u8 *xbar_cfg;
156459cc2c6SThierry Reding };
157459cc2c6SThierry Reding 
158459cc2c6SThierry Reding struct tegra_sor;
159459cc2c6SThierry Reding 
160459cc2c6SThierry Reding struct tegra_sor_ops {
161459cc2c6SThierry Reding 	const char *name;
162459cc2c6SThierry Reding 	int (*probe)(struct tegra_sor *sor);
163459cc2c6SThierry Reding 	int (*remove)(struct tegra_sor *sor);
164459cc2c6SThierry Reding };
165459cc2c6SThierry Reding 
1666b6b6042SThierry Reding struct tegra_sor {
1676b6b6042SThierry Reding 	struct host1x_client client;
1686b6b6042SThierry Reding 	struct tegra_output output;
1696b6b6042SThierry Reding 	struct device *dev;
1706b6b6042SThierry Reding 
171459cc2c6SThierry Reding 	const struct tegra_sor_soc *soc;
1726b6b6042SThierry Reding 	void __iomem *regs;
1736b6b6042SThierry Reding 
1746b6b6042SThierry Reding 	struct reset_control *rst;
1756b6b6042SThierry Reding 	struct clk *clk_parent;
176b299221cSThierry Reding 	struct clk *clk_brick;
1776b6b6042SThierry Reding 	struct clk *clk_safe;
178618dee39SThierry Reding 	struct clk *clk_src;
1796b6b6042SThierry Reding 	struct clk *clk_dp;
1806b6b6042SThierry Reding 	struct clk *clk;
1816b6b6042SThierry Reding 
1829542c237SThierry Reding 	struct drm_dp_aux *aux;
1836b6b6042SThierry Reding 
184dab16336SThierry Reding 	struct drm_info_list *debugfs_files;
185dab16336SThierry Reding 	struct drm_minor *minor;
186a82752e1SThierry Reding 	struct dentry *debugfs;
187459cc2c6SThierry Reding 
188459cc2c6SThierry Reding 	const struct tegra_sor_ops *ops;
189459cc2c6SThierry Reding 
190459cc2c6SThierry Reding 	/* for HDMI 2.0 */
191459cc2c6SThierry Reding 	struct tegra_sor_hdmi_settings *settings;
192459cc2c6SThierry Reding 	unsigned int num_settings;
193459cc2c6SThierry Reding 
194459cc2c6SThierry Reding 	struct regulator *avdd_io_supply;
195459cc2c6SThierry Reding 	struct regulator *vdd_pll_supply;
196459cc2c6SThierry Reding 	struct regulator *hdmi_supply;
1976b6b6042SThierry Reding };
1986b6b6042SThierry Reding 
199c31efa7aSThierry Reding struct tegra_sor_state {
200c31efa7aSThierry Reding 	struct drm_connector_state base;
201c31efa7aSThierry Reding 
202c31efa7aSThierry Reding 	unsigned int bpc;
203c31efa7aSThierry Reding };
204c31efa7aSThierry Reding 
205c31efa7aSThierry Reding static inline struct tegra_sor_state *
206c31efa7aSThierry Reding to_sor_state(struct drm_connector_state *state)
207c31efa7aSThierry Reding {
208c31efa7aSThierry Reding 	return container_of(state, struct tegra_sor_state, base);
209c31efa7aSThierry Reding }
210c31efa7aSThierry Reding 
21134fa183bSThierry Reding struct tegra_sor_config {
21234fa183bSThierry Reding 	u32 bits_per_pixel;
21334fa183bSThierry Reding 
21434fa183bSThierry Reding 	u32 active_polarity;
21534fa183bSThierry Reding 	u32 active_count;
21634fa183bSThierry Reding 	u32 tu_size;
21734fa183bSThierry Reding 	u32 active_frac;
21834fa183bSThierry Reding 	u32 watermark;
2197890b576SThierry Reding 
2207890b576SThierry Reding 	u32 hblank_symbols;
2217890b576SThierry Reding 	u32 vblank_symbols;
22234fa183bSThierry Reding };
22334fa183bSThierry Reding 
2246b6b6042SThierry Reding static inline struct tegra_sor *
2256b6b6042SThierry Reding host1x_client_to_sor(struct host1x_client *client)
2266b6b6042SThierry Reding {
2276b6b6042SThierry Reding 	return container_of(client, struct tegra_sor, client);
2286b6b6042SThierry Reding }
2296b6b6042SThierry Reding 
2306b6b6042SThierry Reding static inline struct tegra_sor *to_sor(struct tegra_output *output)
2316b6b6042SThierry Reding {
2326b6b6042SThierry Reding 	return container_of(output, struct tegra_sor, output);
2336b6b6042SThierry Reding }
2346b6b6042SThierry Reding 
23528fe2076SThierry Reding static inline u32 tegra_sor_readl(struct tegra_sor *sor, unsigned long offset)
2366b6b6042SThierry Reding {
2376b6b6042SThierry Reding 	return readl(sor->regs + (offset << 2));
2386b6b6042SThierry Reding }
2396b6b6042SThierry Reding 
24028fe2076SThierry Reding static inline void tegra_sor_writel(struct tegra_sor *sor, u32 value,
2416b6b6042SThierry Reding 				    unsigned long offset)
2426b6b6042SThierry Reding {
2436b6b6042SThierry Reding 	writel(value, sor->regs + (offset << 2));
2446b6b6042SThierry Reding }
2456b6b6042SThierry Reding 
24625bb2cecSThierry Reding static int tegra_sor_set_parent_clock(struct tegra_sor *sor, struct clk *parent)
24725bb2cecSThierry Reding {
24825bb2cecSThierry Reding 	int err;
24925bb2cecSThierry Reding 
25025bb2cecSThierry Reding 	clk_disable_unprepare(sor->clk);
25125bb2cecSThierry Reding 
25225bb2cecSThierry Reding 	err = clk_set_parent(sor->clk, parent);
25325bb2cecSThierry Reding 	if (err < 0)
25425bb2cecSThierry Reding 		return err;
25525bb2cecSThierry Reding 
25625bb2cecSThierry Reding 	err = clk_prepare_enable(sor->clk);
25725bb2cecSThierry Reding 	if (err < 0)
25825bb2cecSThierry Reding 		return err;
25925bb2cecSThierry Reding 
26025bb2cecSThierry Reding 	return 0;
26125bb2cecSThierry Reding }
26225bb2cecSThierry Reding 
263b299221cSThierry Reding struct tegra_clk_sor_brick {
264b299221cSThierry Reding 	struct clk_hw hw;
265b299221cSThierry Reding 	struct tegra_sor *sor;
266b299221cSThierry Reding };
267b299221cSThierry Reding 
268b299221cSThierry Reding static inline struct tegra_clk_sor_brick *to_brick(struct clk_hw *hw)
269b299221cSThierry Reding {
270b299221cSThierry Reding 	return container_of(hw, struct tegra_clk_sor_brick, hw);
271b299221cSThierry Reding }
272b299221cSThierry Reding 
273b299221cSThierry Reding static const char * const tegra_clk_sor_brick_parents[] = {
274b299221cSThierry Reding 	"pll_d2_out0", "pll_dp"
275b299221cSThierry Reding };
276b299221cSThierry Reding 
277b299221cSThierry Reding static int tegra_clk_sor_brick_set_parent(struct clk_hw *hw, u8 index)
278b299221cSThierry Reding {
279b299221cSThierry Reding 	struct tegra_clk_sor_brick *brick = to_brick(hw);
280b299221cSThierry Reding 	struct tegra_sor *sor = brick->sor;
281b299221cSThierry Reding 	u32 value;
282b299221cSThierry Reding 
283b299221cSThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
284b299221cSThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
285b299221cSThierry Reding 
286b299221cSThierry Reding 	switch (index) {
287b299221cSThierry Reding 	case 0:
288b299221cSThierry Reding 		value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK;
289b299221cSThierry Reding 		break;
290b299221cSThierry Reding 
291b299221cSThierry Reding 	case 1:
292b299221cSThierry Reding 		value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
293b299221cSThierry Reding 		break;
294b299221cSThierry Reding 	}
295b299221cSThierry Reding 
296b299221cSThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
297b299221cSThierry Reding 
298b299221cSThierry Reding 	return 0;
299b299221cSThierry Reding }
300b299221cSThierry Reding 
301b299221cSThierry Reding static u8 tegra_clk_sor_brick_get_parent(struct clk_hw *hw)
302b299221cSThierry Reding {
303b299221cSThierry Reding 	struct tegra_clk_sor_brick *brick = to_brick(hw);
304b299221cSThierry Reding 	struct tegra_sor *sor = brick->sor;
305b299221cSThierry Reding 	u8 parent = U8_MAX;
306b299221cSThierry Reding 	u32 value;
307b299221cSThierry Reding 
308b299221cSThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
309b299221cSThierry Reding 
310b299221cSThierry Reding 	switch (value & SOR_CLK_CNTRL_DP_CLK_SEL_MASK) {
311b299221cSThierry Reding 	case SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK:
312b299221cSThierry Reding 	case SOR_CLK_CNTRL_DP_CLK_SEL_DIFF_PCLK:
313b299221cSThierry Reding 		parent = 0;
314b299221cSThierry Reding 		break;
315b299221cSThierry Reding 
316b299221cSThierry Reding 	case SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK:
317b299221cSThierry Reding 	case SOR_CLK_CNTRL_DP_CLK_SEL_DIFF_DPCLK:
318b299221cSThierry Reding 		parent = 1;
319b299221cSThierry Reding 		break;
320b299221cSThierry Reding 	}
321b299221cSThierry Reding 
322b299221cSThierry Reding 	return parent;
323b299221cSThierry Reding }
324b299221cSThierry Reding 
325b299221cSThierry Reding static const struct clk_ops tegra_clk_sor_brick_ops = {
326b299221cSThierry Reding 	.set_parent = tegra_clk_sor_brick_set_parent,
327b299221cSThierry Reding 	.get_parent = tegra_clk_sor_brick_get_parent,
328b299221cSThierry Reding };
329b299221cSThierry Reding 
330b299221cSThierry Reding static struct clk *tegra_clk_sor_brick_register(struct tegra_sor *sor,
331b299221cSThierry Reding 						const char *name)
332b299221cSThierry Reding {
333b299221cSThierry Reding 	struct tegra_clk_sor_brick *brick;
334b299221cSThierry Reding 	struct clk_init_data init;
335b299221cSThierry Reding 	struct clk *clk;
336b299221cSThierry Reding 
337b299221cSThierry Reding 	brick = devm_kzalloc(sor->dev, sizeof(*brick), GFP_KERNEL);
338b299221cSThierry Reding 	if (!brick)
339b299221cSThierry Reding 		return ERR_PTR(-ENOMEM);
340b299221cSThierry Reding 
341b299221cSThierry Reding 	brick->sor = sor;
342b299221cSThierry Reding 
343b299221cSThierry Reding 	init.name = name;
344b299221cSThierry Reding 	init.flags = 0;
345b299221cSThierry Reding 	init.parent_names = tegra_clk_sor_brick_parents;
346b299221cSThierry Reding 	init.num_parents = ARRAY_SIZE(tegra_clk_sor_brick_parents);
347b299221cSThierry Reding 	init.ops = &tegra_clk_sor_brick_ops;
348b299221cSThierry Reding 
349b299221cSThierry Reding 	brick->hw.init = &init;
350b299221cSThierry Reding 
351b299221cSThierry Reding 	clk = devm_clk_register(sor->dev, &brick->hw);
352b299221cSThierry Reding 	if (IS_ERR(clk))
353b299221cSThierry Reding 		kfree(brick);
354b299221cSThierry Reding 
355b299221cSThierry Reding 	return clk;
356b299221cSThierry Reding }
357b299221cSThierry Reding 
3586b6b6042SThierry Reding static int tegra_sor_dp_train_fast(struct tegra_sor *sor,
3596b6b6042SThierry Reding 				   struct drm_dp_link *link)
3606b6b6042SThierry Reding {
3616b6b6042SThierry Reding 	unsigned int i;
3626b6b6042SThierry Reding 	u8 pattern;
36328fe2076SThierry Reding 	u32 value;
3646b6b6042SThierry Reding 	int err;
3656b6b6042SThierry Reding 
3666b6b6042SThierry Reding 	/* setup lane parameters */
3676b6b6042SThierry Reding 	value = SOR_LANE_DRIVE_CURRENT_LANE3(0x40) |
3686b6b6042SThierry Reding 		SOR_LANE_DRIVE_CURRENT_LANE2(0x40) |
3696b6b6042SThierry Reding 		SOR_LANE_DRIVE_CURRENT_LANE1(0x40) |
3706b6b6042SThierry Reding 		SOR_LANE_DRIVE_CURRENT_LANE0(0x40);
371a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT0);
3726b6b6042SThierry Reding 
3736b6b6042SThierry Reding 	value = SOR_LANE_PREEMPHASIS_LANE3(0x0f) |
3746b6b6042SThierry Reding 		SOR_LANE_PREEMPHASIS_LANE2(0x0f) |
3756b6b6042SThierry Reding 		SOR_LANE_PREEMPHASIS_LANE1(0x0f) |
3766b6b6042SThierry Reding 		SOR_LANE_PREEMPHASIS_LANE0(0x0f);
377a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS0);
3786b6b6042SThierry Reding 
379a9a9e4fdSThierry Reding 	value = SOR_LANE_POSTCURSOR_LANE3(0x00) |
380a9a9e4fdSThierry Reding 		SOR_LANE_POSTCURSOR_LANE2(0x00) |
381a9a9e4fdSThierry Reding 		SOR_LANE_POSTCURSOR_LANE1(0x00) |
382a9a9e4fdSThierry Reding 		SOR_LANE_POSTCURSOR_LANE0(0x00);
383a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_POSTCURSOR0);
3846b6b6042SThierry Reding 
3856b6b6042SThierry Reding 	/* disable LVDS mode */
3866b6b6042SThierry Reding 	tegra_sor_writel(sor, 0, SOR_LVDS);
3876b6b6042SThierry Reding 
388a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
3896b6b6042SThierry Reding 	value |= SOR_DP_PADCTL_TX_PU_ENABLE;
3906b6b6042SThierry Reding 	value &= ~SOR_DP_PADCTL_TX_PU_MASK;
3916b6b6042SThierry Reding 	value |= SOR_DP_PADCTL_TX_PU(2); /* XXX: don't hardcode? */
392a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
3936b6b6042SThierry Reding 
394a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
3956b6b6042SThierry Reding 	value |= SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
3966b6b6042SThierry Reding 		 SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0;
397a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
3986b6b6042SThierry Reding 
3996b6b6042SThierry Reding 	usleep_range(10, 100);
4006b6b6042SThierry Reding 
401a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
4026b6b6042SThierry Reding 	value &= ~(SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
4036b6b6042SThierry Reding 		   SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0);
404a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
4056b6b6042SThierry Reding 
4069542c237SThierry Reding 	err = drm_dp_aux_prepare(sor->aux, DP_SET_ANSI_8B10B);
4076b6b6042SThierry Reding 	if (err < 0)
4086b6b6042SThierry Reding 		return err;
4096b6b6042SThierry Reding 
4106b6b6042SThierry Reding 	for (i = 0, value = 0; i < link->num_lanes; i++) {
4116b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
4126b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_NONE |
4136b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_TRAIN1;
4146b6b6042SThierry Reding 		value = (value << 8) | lane;
4156b6b6042SThierry Reding 	}
4166b6b6042SThierry Reding 
4176b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
4186b6b6042SThierry Reding 
4196b6b6042SThierry Reding 	pattern = DP_TRAINING_PATTERN_1;
4206b6b6042SThierry Reding 
4219542c237SThierry Reding 	err = drm_dp_aux_train(sor->aux, link, pattern);
4226b6b6042SThierry Reding 	if (err < 0)
4236b6b6042SThierry Reding 		return err;
4246b6b6042SThierry Reding 
425a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_SPARE0);
4266b6b6042SThierry Reding 	value |= SOR_DP_SPARE_SEQ_ENABLE;
4276b6b6042SThierry Reding 	value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
4286b6b6042SThierry Reding 	value |= SOR_DP_SPARE_MACRO_SOR_CLK;
429a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_SPARE0);
4306b6b6042SThierry Reding 
4316b6b6042SThierry Reding 	for (i = 0, value = 0; i < link->num_lanes; i++) {
4326b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
4336b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_NONE |
4346b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_TRAIN2;
4356b6b6042SThierry Reding 		value = (value << 8) | lane;
4366b6b6042SThierry Reding 	}
4376b6b6042SThierry Reding 
4386b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
4396b6b6042SThierry Reding 
4406b6b6042SThierry Reding 	pattern = DP_LINK_SCRAMBLING_DISABLE | DP_TRAINING_PATTERN_2;
4416b6b6042SThierry Reding 
4429542c237SThierry Reding 	err = drm_dp_aux_train(sor->aux, link, pattern);
4436b6b6042SThierry Reding 	if (err < 0)
4446b6b6042SThierry Reding 		return err;
4456b6b6042SThierry Reding 
4466b6b6042SThierry Reding 	for (i = 0, value = 0; i < link->num_lanes; i++) {
4476b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
4486b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_GALIOS |
4496b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_NONE;
4506b6b6042SThierry Reding 		value = (value << 8) | lane;
4516b6b6042SThierry Reding 	}
4526b6b6042SThierry Reding 
4536b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
4546b6b6042SThierry Reding 
4556b6b6042SThierry Reding 	pattern = DP_TRAINING_PATTERN_DISABLE;
4566b6b6042SThierry Reding 
4579542c237SThierry Reding 	err = drm_dp_aux_train(sor->aux, link, pattern);
4586b6b6042SThierry Reding 	if (err < 0)
4596b6b6042SThierry Reding 		return err;
4606b6b6042SThierry Reding 
4616b6b6042SThierry Reding 	return 0;
4626b6b6042SThierry Reding }
4636b6b6042SThierry Reding 
464459cc2c6SThierry Reding static void tegra_sor_dp_term_calibrate(struct tegra_sor *sor)
465459cc2c6SThierry Reding {
466459cc2c6SThierry Reding 	u32 mask = 0x08, adj = 0, value;
467459cc2c6SThierry Reding 
468459cc2c6SThierry Reding 	/* enable pad calibration logic */
469459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
470459cc2c6SThierry Reding 	value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
471459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
472459cc2c6SThierry Reding 
473459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL1);
474459cc2c6SThierry Reding 	value |= SOR_PLL1_TMDS_TERM;
475459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL1);
476459cc2c6SThierry Reding 
477459cc2c6SThierry Reding 	while (mask) {
478459cc2c6SThierry Reding 		adj |= mask;
479459cc2c6SThierry Reding 
480459cc2c6SThierry Reding 		value = tegra_sor_readl(sor, SOR_PLL1);
481459cc2c6SThierry Reding 		value &= ~SOR_PLL1_TMDS_TERMADJ_MASK;
482459cc2c6SThierry Reding 		value |= SOR_PLL1_TMDS_TERMADJ(adj);
483459cc2c6SThierry Reding 		tegra_sor_writel(sor, value, SOR_PLL1);
484459cc2c6SThierry Reding 
485459cc2c6SThierry Reding 		usleep_range(100, 200);
486459cc2c6SThierry Reding 
487459cc2c6SThierry Reding 		value = tegra_sor_readl(sor, SOR_PLL1);
488459cc2c6SThierry Reding 		if (value & SOR_PLL1_TERM_COMPOUT)
489459cc2c6SThierry Reding 			adj &= ~mask;
490459cc2c6SThierry Reding 
491459cc2c6SThierry Reding 		mask >>= 1;
492459cc2c6SThierry Reding 	}
493459cc2c6SThierry Reding 
494459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL1);
495459cc2c6SThierry Reding 	value &= ~SOR_PLL1_TMDS_TERMADJ_MASK;
496459cc2c6SThierry Reding 	value |= SOR_PLL1_TMDS_TERMADJ(adj);
497459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL1);
498459cc2c6SThierry Reding 
499459cc2c6SThierry Reding 	/* disable pad calibration logic */
500459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
501459cc2c6SThierry Reding 	value |= SOR_DP_PADCTL_PAD_CAL_PD;
502459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
503459cc2c6SThierry Reding }
504459cc2c6SThierry Reding 
5056b6b6042SThierry Reding static void tegra_sor_super_update(struct tegra_sor *sor)
5066b6b6042SThierry Reding {
507a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
508a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 1, SOR_SUPER_STATE0);
509a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
5106b6b6042SThierry Reding }
5116b6b6042SThierry Reding 
5126b6b6042SThierry Reding static void tegra_sor_update(struct tegra_sor *sor)
5136b6b6042SThierry Reding {
514a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE0);
515a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 1, SOR_STATE0);
516a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE0);
5176b6b6042SThierry Reding }
5186b6b6042SThierry Reding 
5196b6b6042SThierry Reding static int tegra_sor_setup_pwm(struct tegra_sor *sor, unsigned long timeout)
5206b6b6042SThierry Reding {
52128fe2076SThierry Reding 	u32 value;
5226b6b6042SThierry Reding 
5236b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWM_DIV);
5246b6b6042SThierry Reding 	value &= ~SOR_PWM_DIV_MASK;
5256b6b6042SThierry Reding 	value |= 0x400; /* period */
5266b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWM_DIV);
5276b6b6042SThierry Reding 
5286b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWM_CTL);
5296b6b6042SThierry Reding 	value &= ~SOR_PWM_CTL_DUTY_CYCLE_MASK;
5306b6b6042SThierry Reding 	value |= 0x400; /* duty cycle */
5316b6b6042SThierry Reding 	value &= ~SOR_PWM_CTL_CLK_SEL; /* clock source: PCLK */
5326b6b6042SThierry Reding 	value |= SOR_PWM_CTL_TRIGGER;
5336b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWM_CTL);
5346b6b6042SThierry Reding 
5356b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
5366b6b6042SThierry Reding 
5376b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
5386b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWM_CTL);
5396b6b6042SThierry Reding 		if ((value & SOR_PWM_CTL_TRIGGER) == 0)
5406b6b6042SThierry Reding 			return 0;
5416b6b6042SThierry Reding 
5426b6b6042SThierry Reding 		usleep_range(25, 100);
5436b6b6042SThierry Reding 	}
5446b6b6042SThierry Reding 
5456b6b6042SThierry Reding 	return -ETIMEDOUT;
5466b6b6042SThierry Reding }
5476b6b6042SThierry Reding 
5486b6b6042SThierry Reding static int tegra_sor_attach(struct tegra_sor *sor)
5496b6b6042SThierry Reding {
5506b6b6042SThierry Reding 	unsigned long value, timeout;
5516b6b6042SThierry Reding 
5526b6b6042SThierry Reding 	/* wake up in normal mode */
553a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
5546b6b6042SThierry Reding 	value |= SOR_SUPER_STATE_HEAD_MODE_AWAKE;
5556b6b6042SThierry Reding 	value |= SOR_SUPER_STATE_MODE_NORMAL;
556a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
5576b6b6042SThierry Reding 	tegra_sor_super_update(sor);
5586b6b6042SThierry Reding 
5596b6b6042SThierry Reding 	/* attach */
560a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
5616b6b6042SThierry Reding 	value |= SOR_SUPER_STATE_ATTACHED;
562a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
5636b6b6042SThierry Reding 	tegra_sor_super_update(sor);
5646b6b6042SThierry Reding 
5656b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
5666b6b6042SThierry Reding 
5676b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
5686b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_TEST);
5696b6b6042SThierry Reding 		if ((value & SOR_TEST_ATTACHED) != 0)
5706b6b6042SThierry Reding 			return 0;
5716b6b6042SThierry Reding 
5726b6b6042SThierry Reding 		usleep_range(25, 100);
5736b6b6042SThierry Reding 	}
5746b6b6042SThierry Reding 
5756b6b6042SThierry Reding 	return -ETIMEDOUT;
5766b6b6042SThierry Reding }
5776b6b6042SThierry Reding 
5786b6b6042SThierry Reding static int tegra_sor_wakeup(struct tegra_sor *sor)
5796b6b6042SThierry Reding {
5806b6b6042SThierry Reding 	unsigned long value, timeout;
5816b6b6042SThierry Reding 
5826b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
5836b6b6042SThierry Reding 
5846b6b6042SThierry Reding 	/* wait for head to wake up */
5856b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
5866b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_TEST);
5876b6b6042SThierry Reding 		value &= SOR_TEST_HEAD_MODE_MASK;
5886b6b6042SThierry Reding 
5896b6b6042SThierry Reding 		if (value == SOR_TEST_HEAD_MODE_AWAKE)
5906b6b6042SThierry Reding 			return 0;
5916b6b6042SThierry Reding 
5926b6b6042SThierry Reding 		usleep_range(25, 100);
5936b6b6042SThierry Reding 	}
5946b6b6042SThierry Reding 
5956b6b6042SThierry Reding 	return -ETIMEDOUT;
5966b6b6042SThierry Reding }
5976b6b6042SThierry Reding 
5986b6b6042SThierry Reding static int tegra_sor_power_up(struct tegra_sor *sor, unsigned long timeout)
5996b6b6042SThierry Reding {
60028fe2076SThierry Reding 	u32 value;
6016b6b6042SThierry Reding 
6026b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWR);
6036b6b6042SThierry Reding 	value |= SOR_PWR_TRIGGER | SOR_PWR_NORMAL_STATE_PU;
6046b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWR);
6056b6b6042SThierry Reding 
6066b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
6076b6b6042SThierry Reding 
6086b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
6096b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWR);
6106b6b6042SThierry Reding 		if ((value & SOR_PWR_TRIGGER) == 0)
6116b6b6042SThierry Reding 			return 0;
6126b6b6042SThierry Reding 
6136b6b6042SThierry Reding 		usleep_range(25, 100);
6146b6b6042SThierry Reding 	}
6156b6b6042SThierry Reding 
6166b6b6042SThierry Reding 	return -ETIMEDOUT;
6176b6b6042SThierry Reding }
6186b6b6042SThierry Reding 
61934fa183bSThierry Reding struct tegra_sor_params {
62034fa183bSThierry Reding 	/* number of link clocks per line */
62134fa183bSThierry Reding 	unsigned int num_clocks;
62234fa183bSThierry Reding 	/* ratio between input and output */
62334fa183bSThierry Reding 	u64 ratio;
62434fa183bSThierry Reding 	/* precision factor */
62534fa183bSThierry Reding 	u64 precision;
62634fa183bSThierry Reding 
62734fa183bSThierry Reding 	unsigned int active_polarity;
62834fa183bSThierry Reding 	unsigned int active_count;
62934fa183bSThierry Reding 	unsigned int active_frac;
63034fa183bSThierry Reding 	unsigned int tu_size;
63134fa183bSThierry Reding 	unsigned int error;
63234fa183bSThierry Reding };
63334fa183bSThierry Reding 
63434fa183bSThierry Reding static int tegra_sor_compute_params(struct tegra_sor *sor,
63534fa183bSThierry Reding 				    struct tegra_sor_params *params,
63634fa183bSThierry Reding 				    unsigned int tu_size)
63734fa183bSThierry Reding {
63834fa183bSThierry Reding 	u64 active_sym, active_count, frac, approx;
63934fa183bSThierry Reding 	u32 active_polarity, active_frac = 0;
64034fa183bSThierry Reding 	const u64 f = params->precision;
64134fa183bSThierry Reding 	s64 error;
64234fa183bSThierry Reding 
64334fa183bSThierry Reding 	active_sym = params->ratio * tu_size;
64434fa183bSThierry Reding 	active_count = div_u64(active_sym, f) * f;
64534fa183bSThierry Reding 	frac = active_sym - active_count;
64634fa183bSThierry Reding 
64734fa183bSThierry Reding 	/* fraction < 0.5 */
64834fa183bSThierry Reding 	if (frac >= (f / 2)) {
64934fa183bSThierry Reding 		active_polarity = 1;
65034fa183bSThierry Reding 		frac = f - frac;
65134fa183bSThierry Reding 	} else {
65234fa183bSThierry Reding 		active_polarity = 0;
65334fa183bSThierry Reding 	}
65434fa183bSThierry Reding 
65534fa183bSThierry Reding 	if (frac != 0) {
65634fa183bSThierry Reding 		frac = div_u64(f * f,  frac); /* 1/fraction */
65734fa183bSThierry Reding 		if (frac <= (15 * f)) {
65834fa183bSThierry Reding 			active_frac = div_u64(frac, f);
65934fa183bSThierry Reding 
66034fa183bSThierry Reding 			/* round up */
66134fa183bSThierry Reding 			if (active_polarity)
66234fa183bSThierry Reding 				active_frac++;
66334fa183bSThierry Reding 		} else {
66434fa183bSThierry Reding 			active_frac = active_polarity ? 1 : 15;
66534fa183bSThierry Reding 		}
66634fa183bSThierry Reding 	}
66734fa183bSThierry Reding 
66834fa183bSThierry Reding 	if (active_frac == 1)
66934fa183bSThierry Reding 		active_polarity = 0;
67034fa183bSThierry Reding 
67134fa183bSThierry Reding 	if (active_polarity == 1) {
67234fa183bSThierry Reding 		if (active_frac) {
67334fa183bSThierry Reding 			approx = active_count + (active_frac * (f - 1)) * f;
67434fa183bSThierry Reding 			approx = div_u64(approx, active_frac * f);
67534fa183bSThierry Reding 		} else {
67634fa183bSThierry Reding 			approx = active_count + f;
67734fa183bSThierry Reding 		}
67834fa183bSThierry Reding 	} else {
67934fa183bSThierry Reding 		if (active_frac)
68034fa183bSThierry Reding 			approx = active_count + div_u64(f, active_frac);
68134fa183bSThierry Reding 		else
68234fa183bSThierry Reding 			approx = active_count;
68334fa183bSThierry Reding 	}
68434fa183bSThierry Reding 
68534fa183bSThierry Reding 	error = div_s64(active_sym - approx, tu_size);
68634fa183bSThierry Reding 	error *= params->num_clocks;
68734fa183bSThierry Reding 
68879211c8eSAndrew Morton 	if (error <= 0 && abs(error) < params->error) {
68934fa183bSThierry Reding 		params->active_count = div_u64(active_count, f);
69034fa183bSThierry Reding 		params->active_polarity = active_polarity;
69134fa183bSThierry Reding 		params->active_frac = active_frac;
69279211c8eSAndrew Morton 		params->error = abs(error);
69334fa183bSThierry Reding 		params->tu_size = tu_size;
69434fa183bSThierry Reding 
69534fa183bSThierry Reding 		if (error == 0)
69634fa183bSThierry Reding 			return true;
69734fa183bSThierry Reding 	}
69834fa183bSThierry Reding 
69934fa183bSThierry Reding 	return false;
70034fa183bSThierry Reding }
70134fa183bSThierry Reding 
702a198359eSThierry Reding static int tegra_sor_compute_config(struct tegra_sor *sor,
70380444495SThierry Reding 				    const struct drm_display_mode *mode,
70434fa183bSThierry Reding 				    struct tegra_sor_config *config,
70534fa183bSThierry Reding 				    struct drm_dp_link *link)
70634fa183bSThierry Reding {
70734fa183bSThierry Reding 	const u64 f = 100000, link_rate = link->rate * 1000;
70834fa183bSThierry Reding 	const u64 pclk = mode->clock * 1000;
7097890b576SThierry Reding 	u64 input, output, watermark, num;
71034fa183bSThierry Reding 	struct tegra_sor_params params;
71134fa183bSThierry Reding 	u32 num_syms_per_line;
71234fa183bSThierry Reding 	unsigned int i;
71334fa183bSThierry Reding 
71434fa183bSThierry Reding 	if (!link_rate || !link->num_lanes || !pclk || !config->bits_per_pixel)
71534fa183bSThierry Reding 		return -EINVAL;
71634fa183bSThierry Reding 
71734fa183bSThierry Reding 	output = link_rate * 8 * link->num_lanes;
71834fa183bSThierry Reding 	input = pclk * config->bits_per_pixel;
71934fa183bSThierry Reding 
72034fa183bSThierry Reding 	if (input >= output)
72134fa183bSThierry Reding 		return -ERANGE;
72234fa183bSThierry Reding 
72334fa183bSThierry Reding 	memset(&params, 0, sizeof(params));
72434fa183bSThierry Reding 	params.ratio = div64_u64(input * f, output);
72534fa183bSThierry Reding 	params.num_clocks = div_u64(link_rate * mode->hdisplay, pclk);
72634fa183bSThierry Reding 	params.precision = f;
72734fa183bSThierry Reding 	params.error = 64 * f;
72834fa183bSThierry Reding 	params.tu_size = 64;
72934fa183bSThierry Reding 
73034fa183bSThierry Reding 	for (i = params.tu_size; i >= 32; i--)
73134fa183bSThierry Reding 		if (tegra_sor_compute_params(sor, &params, i))
73234fa183bSThierry Reding 			break;
73334fa183bSThierry Reding 
73434fa183bSThierry Reding 	if (params.active_frac == 0) {
73534fa183bSThierry Reding 		config->active_polarity = 0;
73634fa183bSThierry Reding 		config->active_count = params.active_count;
73734fa183bSThierry Reding 
73834fa183bSThierry Reding 		if (!params.active_polarity)
73934fa183bSThierry Reding 			config->active_count--;
74034fa183bSThierry Reding 
74134fa183bSThierry Reding 		config->tu_size = params.tu_size;
74234fa183bSThierry Reding 		config->active_frac = 1;
74334fa183bSThierry Reding 	} else {
74434fa183bSThierry Reding 		config->active_polarity = params.active_polarity;
74534fa183bSThierry Reding 		config->active_count = params.active_count;
74634fa183bSThierry Reding 		config->active_frac = params.active_frac;
74734fa183bSThierry Reding 		config->tu_size = params.tu_size;
74834fa183bSThierry Reding 	}
74934fa183bSThierry Reding 
75034fa183bSThierry Reding 	dev_dbg(sor->dev,
75134fa183bSThierry Reding 		"polarity: %d active count: %d tu size: %d active frac: %d\n",
75234fa183bSThierry Reding 		config->active_polarity, config->active_count,
75334fa183bSThierry Reding 		config->tu_size, config->active_frac);
75434fa183bSThierry Reding 
75534fa183bSThierry Reding 	watermark = params.ratio * config->tu_size * (f - params.ratio);
75634fa183bSThierry Reding 	watermark = div_u64(watermark, f);
75734fa183bSThierry Reding 
75834fa183bSThierry Reding 	watermark = div_u64(watermark + params.error, f);
75934fa183bSThierry Reding 	config->watermark = watermark + (config->bits_per_pixel / 8) + 2;
76034fa183bSThierry Reding 	num_syms_per_line = (mode->hdisplay * config->bits_per_pixel) *
76134fa183bSThierry Reding 			    (link->num_lanes * 8);
76234fa183bSThierry Reding 
76334fa183bSThierry Reding 	if (config->watermark > 30) {
76434fa183bSThierry Reding 		config->watermark = 30;
76534fa183bSThierry Reding 		dev_err(sor->dev,
76634fa183bSThierry Reding 			"unable to compute TU size, forcing watermark to %u\n",
76734fa183bSThierry Reding 			config->watermark);
76834fa183bSThierry Reding 	} else if (config->watermark > num_syms_per_line) {
76934fa183bSThierry Reding 		config->watermark = num_syms_per_line;
77034fa183bSThierry Reding 		dev_err(sor->dev, "watermark too high, forcing to %u\n",
77134fa183bSThierry Reding 			config->watermark);
77234fa183bSThierry Reding 	}
77334fa183bSThierry Reding 
7747890b576SThierry Reding 	/* compute the number of symbols per horizontal blanking interval */
7757890b576SThierry Reding 	num = ((mode->htotal - mode->hdisplay) - 7) * link_rate;
7767890b576SThierry Reding 	config->hblank_symbols = div_u64(num, pclk);
7777890b576SThierry Reding 
7787890b576SThierry Reding 	if (link->capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
7797890b576SThierry Reding 		config->hblank_symbols -= 3;
7807890b576SThierry Reding 
7817890b576SThierry Reding 	config->hblank_symbols -= 12 / link->num_lanes;
7827890b576SThierry Reding 
7837890b576SThierry Reding 	/* compute the number of symbols per vertical blanking interval */
7847890b576SThierry Reding 	num = (mode->hdisplay - 25) * link_rate;
7857890b576SThierry Reding 	config->vblank_symbols = div_u64(num, pclk);
7867890b576SThierry Reding 	config->vblank_symbols -= 36 / link->num_lanes + 4;
7877890b576SThierry Reding 
7887890b576SThierry Reding 	dev_dbg(sor->dev, "blank symbols: H:%u V:%u\n", config->hblank_symbols,
7897890b576SThierry Reding 		config->vblank_symbols);
7907890b576SThierry Reding 
79134fa183bSThierry Reding 	return 0;
79234fa183bSThierry Reding }
79334fa183bSThierry Reding 
794402f6bcdSThierry Reding static void tegra_sor_apply_config(struct tegra_sor *sor,
795402f6bcdSThierry Reding 				   const struct tegra_sor_config *config)
796402f6bcdSThierry Reding {
797402f6bcdSThierry Reding 	u32 value;
798402f6bcdSThierry Reding 
799402f6bcdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
800402f6bcdSThierry Reding 	value &= ~SOR_DP_LINKCTL_TU_SIZE_MASK;
801402f6bcdSThierry Reding 	value |= SOR_DP_LINKCTL_TU_SIZE(config->tu_size);
802402f6bcdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
803402f6bcdSThierry Reding 
804402f6bcdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_CONFIG0);
805402f6bcdSThierry Reding 	value &= ~SOR_DP_CONFIG_WATERMARK_MASK;
806402f6bcdSThierry Reding 	value |= SOR_DP_CONFIG_WATERMARK(config->watermark);
807402f6bcdSThierry Reding 
808402f6bcdSThierry Reding 	value &= ~SOR_DP_CONFIG_ACTIVE_SYM_COUNT_MASK;
809402f6bcdSThierry Reding 	value |= SOR_DP_CONFIG_ACTIVE_SYM_COUNT(config->active_count);
810402f6bcdSThierry Reding 
811402f6bcdSThierry Reding 	value &= ~SOR_DP_CONFIG_ACTIVE_SYM_FRAC_MASK;
812402f6bcdSThierry Reding 	value |= SOR_DP_CONFIG_ACTIVE_SYM_FRAC(config->active_frac);
813402f6bcdSThierry Reding 
814402f6bcdSThierry Reding 	if (config->active_polarity)
815402f6bcdSThierry Reding 		value |= SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
816402f6bcdSThierry Reding 	else
817402f6bcdSThierry Reding 		value &= ~SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
818402f6bcdSThierry Reding 
819402f6bcdSThierry Reding 	value |= SOR_DP_CONFIG_ACTIVE_SYM_ENABLE;
820402f6bcdSThierry Reding 	value |= SOR_DP_CONFIG_DISPARITY_NEGATIVE;
821402f6bcdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_CONFIG0);
822402f6bcdSThierry Reding 
823402f6bcdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_AUDIO_HBLANK_SYMBOLS);
824402f6bcdSThierry Reding 	value &= ~SOR_DP_AUDIO_HBLANK_SYMBOLS_MASK;
825402f6bcdSThierry Reding 	value |= config->hblank_symbols & 0xffff;
826402f6bcdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_AUDIO_HBLANK_SYMBOLS);
827402f6bcdSThierry Reding 
828402f6bcdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_AUDIO_VBLANK_SYMBOLS);
829402f6bcdSThierry Reding 	value &= ~SOR_DP_AUDIO_VBLANK_SYMBOLS_MASK;
830402f6bcdSThierry Reding 	value |= config->vblank_symbols & 0xffff;
831402f6bcdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_AUDIO_VBLANK_SYMBOLS);
832402f6bcdSThierry Reding }
833402f6bcdSThierry Reding 
8342bd1dd39SThierry Reding static void tegra_sor_mode_set(struct tegra_sor *sor,
8352bd1dd39SThierry Reding 			       const struct drm_display_mode *mode,
836c31efa7aSThierry Reding 			       struct tegra_sor_state *state)
8372bd1dd39SThierry Reding {
8382bd1dd39SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(sor->output.encoder.crtc);
8392bd1dd39SThierry Reding 	unsigned int vbe, vse, hbe, hse, vbs, hbs;
8402bd1dd39SThierry Reding 	u32 value;
8412bd1dd39SThierry Reding 
8422bd1dd39SThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
8432bd1dd39SThierry Reding 	value &= ~SOR_STATE_ASY_PIXELDEPTH_MASK;
8442bd1dd39SThierry Reding 	value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
8452bd1dd39SThierry Reding 	value &= ~SOR_STATE_ASY_OWNER_MASK;
8462bd1dd39SThierry Reding 
8472bd1dd39SThierry Reding 	value |= SOR_STATE_ASY_CRC_MODE_COMPLETE |
8482bd1dd39SThierry Reding 		 SOR_STATE_ASY_OWNER(dc->pipe + 1);
8492bd1dd39SThierry Reding 
8502bd1dd39SThierry Reding 	if (mode->flags & DRM_MODE_FLAG_PHSYNC)
8512bd1dd39SThierry Reding 		value &= ~SOR_STATE_ASY_HSYNCPOL;
8522bd1dd39SThierry Reding 
8532bd1dd39SThierry Reding 	if (mode->flags & DRM_MODE_FLAG_NHSYNC)
8542bd1dd39SThierry Reding 		value |= SOR_STATE_ASY_HSYNCPOL;
8552bd1dd39SThierry Reding 
8562bd1dd39SThierry Reding 	if (mode->flags & DRM_MODE_FLAG_PVSYNC)
8572bd1dd39SThierry Reding 		value &= ~SOR_STATE_ASY_VSYNCPOL;
8582bd1dd39SThierry Reding 
8592bd1dd39SThierry Reding 	if (mode->flags & DRM_MODE_FLAG_NVSYNC)
8602bd1dd39SThierry Reding 		value |= SOR_STATE_ASY_VSYNCPOL;
8612bd1dd39SThierry Reding 
862c31efa7aSThierry Reding 	switch (state->bpc) {
863c31efa7aSThierry Reding 	case 16:
864c31efa7aSThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_48_444;
865c31efa7aSThierry Reding 		break;
866c31efa7aSThierry Reding 
867c31efa7aSThierry Reding 	case 12:
868c31efa7aSThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_36_444;
869c31efa7aSThierry Reding 		break;
870c31efa7aSThierry Reding 
871c31efa7aSThierry Reding 	case 10:
872c31efa7aSThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_30_444;
873c31efa7aSThierry Reding 		break;
874c31efa7aSThierry Reding 
8752bd1dd39SThierry Reding 	case 8:
8762bd1dd39SThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
8772bd1dd39SThierry Reding 		break;
8782bd1dd39SThierry Reding 
8792bd1dd39SThierry Reding 	case 6:
8802bd1dd39SThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_18_444;
8812bd1dd39SThierry Reding 		break;
8822bd1dd39SThierry Reding 
8832bd1dd39SThierry Reding 	default:
884c31efa7aSThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
8852bd1dd39SThierry Reding 		break;
8862bd1dd39SThierry Reding 	}
8872bd1dd39SThierry Reding 
8882bd1dd39SThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
8892bd1dd39SThierry Reding 
8902bd1dd39SThierry Reding 	/*
8912bd1dd39SThierry Reding 	 * TODO: The video timing programming below doesn't seem to match the
8922bd1dd39SThierry Reding 	 * register definitions.
8932bd1dd39SThierry Reding 	 */
8942bd1dd39SThierry Reding 
8952bd1dd39SThierry Reding 	value = ((mode->vtotal & 0x7fff) << 16) | (mode->htotal & 0x7fff);
8962bd1dd39SThierry Reding 	tegra_sor_writel(sor, value, SOR_HEAD_STATE1(dc->pipe));
8972bd1dd39SThierry Reding 
8982bd1dd39SThierry Reding 	/* sync end = sync width - 1 */
8992bd1dd39SThierry Reding 	vse = mode->vsync_end - mode->vsync_start - 1;
9002bd1dd39SThierry Reding 	hse = mode->hsync_end - mode->hsync_start - 1;
9012bd1dd39SThierry Reding 
9022bd1dd39SThierry Reding 	value = ((vse & 0x7fff) << 16) | (hse & 0x7fff);
9032bd1dd39SThierry Reding 	tegra_sor_writel(sor, value, SOR_HEAD_STATE2(dc->pipe));
9042bd1dd39SThierry Reding 
9052bd1dd39SThierry Reding 	/* blank end = sync end + back porch */
9062bd1dd39SThierry Reding 	vbe = vse + (mode->vtotal - mode->vsync_end);
9072bd1dd39SThierry Reding 	hbe = hse + (mode->htotal - mode->hsync_end);
9082bd1dd39SThierry Reding 
9092bd1dd39SThierry Reding 	value = ((vbe & 0x7fff) << 16) | (hbe & 0x7fff);
9102bd1dd39SThierry Reding 	tegra_sor_writel(sor, value, SOR_HEAD_STATE3(dc->pipe));
9112bd1dd39SThierry Reding 
9122bd1dd39SThierry Reding 	/* blank start = blank end + active */
9132bd1dd39SThierry Reding 	vbs = vbe + mode->vdisplay;
9142bd1dd39SThierry Reding 	hbs = hbe + mode->hdisplay;
9152bd1dd39SThierry Reding 
9162bd1dd39SThierry Reding 	value = ((vbs & 0x7fff) << 16) | (hbs & 0x7fff);
9172bd1dd39SThierry Reding 	tegra_sor_writel(sor, value, SOR_HEAD_STATE4(dc->pipe));
9182bd1dd39SThierry Reding 
9192bd1dd39SThierry Reding 	/* XXX interlacing support */
9202bd1dd39SThierry Reding 	tegra_sor_writel(sor, 0x001, SOR_HEAD_STATE5(dc->pipe));
9212bd1dd39SThierry Reding }
9222bd1dd39SThierry Reding 
9236fad8f66SThierry Reding static int tegra_sor_detach(struct tegra_sor *sor)
9246b6b6042SThierry Reding {
9256fad8f66SThierry Reding 	unsigned long value, timeout;
9266fad8f66SThierry Reding 
9276fad8f66SThierry Reding 	/* switch to safe mode */
928a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
9296fad8f66SThierry Reding 	value &= ~SOR_SUPER_STATE_MODE_NORMAL;
930a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
9316fad8f66SThierry Reding 	tegra_sor_super_update(sor);
9326fad8f66SThierry Reding 
9336fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
9346fad8f66SThierry Reding 
9356fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
9366fad8f66SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWR);
9376fad8f66SThierry Reding 		if (value & SOR_PWR_MODE_SAFE)
9386fad8f66SThierry Reding 			break;
9396fad8f66SThierry Reding 	}
9406fad8f66SThierry Reding 
9416fad8f66SThierry Reding 	if ((value & SOR_PWR_MODE_SAFE) == 0)
9426fad8f66SThierry Reding 		return -ETIMEDOUT;
9436fad8f66SThierry Reding 
9446fad8f66SThierry Reding 	/* go to sleep */
945a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
9466fad8f66SThierry Reding 	value &= ~SOR_SUPER_STATE_HEAD_MODE_MASK;
947a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
9486fad8f66SThierry Reding 	tegra_sor_super_update(sor);
9496fad8f66SThierry Reding 
9506fad8f66SThierry Reding 	/* detach */
951a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
9526fad8f66SThierry Reding 	value &= ~SOR_SUPER_STATE_ATTACHED;
953a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
9546fad8f66SThierry Reding 	tegra_sor_super_update(sor);
9556fad8f66SThierry Reding 
9566fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
9576fad8f66SThierry Reding 
9586fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
9596fad8f66SThierry Reding 		value = tegra_sor_readl(sor, SOR_TEST);
9606fad8f66SThierry Reding 		if ((value & SOR_TEST_ATTACHED) == 0)
9616fad8f66SThierry Reding 			break;
9626fad8f66SThierry Reding 
9636fad8f66SThierry Reding 		usleep_range(25, 100);
9646fad8f66SThierry Reding 	}
9656fad8f66SThierry Reding 
9666fad8f66SThierry Reding 	if ((value & SOR_TEST_ATTACHED) != 0)
9676fad8f66SThierry Reding 		return -ETIMEDOUT;
9686fad8f66SThierry Reding 
9696fad8f66SThierry Reding 	return 0;
9706fad8f66SThierry Reding }
9716fad8f66SThierry Reding 
9726fad8f66SThierry Reding static int tegra_sor_power_down(struct tegra_sor *sor)
9736fad8f66SThierry Reding {
9746fad8f66SThierry Reding 	unsigned long value, timeout;
9756fad8f66SThierry Reding 	int err;
9766fad8f66SThierry Reding 
9776fad8f66SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWR);
9786fad8f66SThierry Reding 	value &= ~SOR_PWR_NORMAL_STATE_PU;
9796fad8f66SThierry Reding 	value |= SOR_PWR_TRIGGER;
9806fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWR);
9816fad8f66SThierry Reding 
9826fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
9836fad8f66SThierry Reding 
9846fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
9856fad8f66SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWR);
9866fad8f66SThierry Reding 		if ((value & SOR_PWR_TRIGGER) == 0)
9876fad8f66SThierry Reding 			return 0;
9886fad8f66SThierry Reding 
9896fad8f66SThierry Reding 		usleep_range(25, 100);
9906fad8f66SThierry Reding 	}
9916fad8f66SThierry Reding 
9926fad8f66SThierry Reding 	if ((value & SOR_PWR_TRIGGER) != 0)
9936fad8f66SThierry Reding 		return -ETIMEDOUT;
9946fad8f66SThierry Reding 
99525bb2cecSThierry Reding 	/* switch to safe parent clock */
99625bb2cecSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
9976fad8f66SThierry Reding 	if (err < 0)
9986fad8f66SThierry Reding 		dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
9996fad8f66SThierry Reding 
1000a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
10016fad8f66SThierry Reding 	value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
10026fad8f66SThierry Reding 		   SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2);
1003a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
10046fad8f66SThierry Reding 
10056fad8f66SThierry Reding 	/* stop lane sequencer */
10066fad8f66SThierry Reding 	value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_UP |
10076fad8f66SThierry Reding 		SOR_LANE_SEQ_CTL_POWER_STATE_DOWN;
10086fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
10096fad8f66SThierry Reding 
10106fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
10116fad8f66SThierry Reding 
10126fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
10136fad8f66SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
10146fad8f66SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
10156fad8f66SThierry Reding 			break;
10166fad8f66SThierry Reding 
10176fad8f66SThierry Reding 		usleep_range(25, 100);
10186fad8f66SThierry Reding 	}
10196fad8f66SThierry Reding 
10206fad8f66SThierry Reding 	if ((value & SOR_LANE_SEQ_CTL_TRIGGER) != 0)
10216fad8f66SThierry Reding 		return -ETIMEDOUT;
10226fad8f66SThierry Reding 
1023a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1024a9a9e4fdSThierry Reding 	value |= SOR_PLL2_PORT_POWERDOWN;
1025a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
10266fad8f66SThierry Reding 
10276fad8f66SThierry Reding 	usleep_range(20, 100);
10286fad8f66SThierry Reding 
1029a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL0);
1030a9a9e4fdSThierry Reding 	value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
1031a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL0);
10326fad8f66SThierry Reding 
1033a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1034a9a9e4fdSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD;
1035a9a9e4fdSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1036a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
10376fad8f66SThierry Reding 
10386fad8f66SThierry Reding 	usleep_range(20, 100);
10396fad8f66SThierry Reding 
10406fad8f66SThierry Reding 	return 0;
10416fad8f66SThierry Reding }
10426fad8f66SThierry Reding 
10436fad8f66SThierry Reding static int tegra_sor_crc_wait(struct tegra_sor *sor, unsigned long timeout)
10446fad8f66SThierry Reding {
10456fad8f66SThierry Reding 	u32 value;
10466fad8f66SThierry Reding 
10476fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
10486fad8f66SThierry Reding 
10496fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
1050a9a9e4fdSThierry Reding 		value = tegra_sor_readl(sor, SOR_CRCA);
1051a9a9e4fdSThierry Reding 		if (value & SOR_CRCA_VALID)
10526fad8f66SThierry Reding 			return 0;
10536fad8f66SThierry Reding 
10546fad8f66SThierry Reding 		usleep_range(100, 200);
10556fad8f66SThierry Reding 	}
10566fad8f66SThierry Reding 
10576fad8f66SThierry Reding 	return -ETIMEDOUT;
10586fad8f66SThierry Reding }
10596fad8f66SThierry Reding 
1060530239a8SThierry Reding static int tegra_sor_show_crc(struct seq_file *s, void *data)
10616fad8f66SThierry Reding {
1062530239a8SThierry Reding 	struct drm_info_node *node = s->private;
1063530239a8SThierry Reding 	struct tegra_sor *sor = node->info_ent->data;
1064850bab44SThierry Reding 	struct drm_crtc *crtc = sor->output.encoder.crtc;
1065850bab44SThierry Reding 	struct drm_device *drm = node->minor->dev;
1066530239a8SThierry Reding 	int err = 0;
10676fad8f66SThierry Reding 	u32 value;
10686fad8f66SThierry Reding 
1069850bab44SThierry Reding 	drm_modeset_lock_all(drm);
10706fad8f66SThierry Reding 
1071850bab44SThierry Reding 	if (!crtc || !crtc->state->active) {
1072850bab44SThierry Reding 		err = -EBUSY;
10736fad8f66SThierry Reding 		goto unlock;
10746fad8f66SThierry Reding 	}
10756fad8f66SThierry Reding 
1076a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
10776fad8f66SThierry Reding 	value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
1078a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
10796fad8f66SThierry Reding 
10806fad8f66SThierry Reding 	value = tegra_sor_readl(sor, SOR_CRC_CNTRL);
10816fad8f66SThierry Reding 	value |= SOR_CRC_CNTRL_ENABLE;
10826fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_CRC_CNTRL);
10836fad8f66SThierry Reding 
10846fad8f66SThierry Reding 	value = tegra_sor_readl(sor, SOR_TEST);
10856fad8f66SThierry Reding 	value &= ~SOR_TEST_CRC_POST_SERIALIZE;
10866fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_TEST);
10876fad8f66SThierry Reding 
10886fad8f66SThierry Reding 	err = tegra_sor_crc_wait(sor, 100);
10896fad8f66SThierry Reding 	if (err < 0)
10906fad8f66SThierry Reding 		goto unlock;
10916fad8f66SThierry Reding 
1092a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, SOR_CRCA_RESET, SOR_CRCA);
1093a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_CRCB);
10946fad8f66SThierry Reding 
1095530239a8SThierry Reding 	seq_printf(s, "%08x\n", value);
10966fad8f66SThierry Reding 
10976fad8f66SThierry Reding unlock:
1098850bab44SThierry Reding 	drm_modeset_unlock_all(drm);
10996fad8f66SThierry Reding 	return err;
11006fad8f66SThierry Reding }
11016fad8f66SThierry Reding 
1102dab16336SThierry Reding static int tegra_sor_show_regs(struct seq_file *s, void *data)
1103dab16336SThierry Reding {
1104dab16336SThierry Reding 	struct drm_info_node *node = s->private;
1105dab16336SThierry Reding 	struct tegra_sor *sor = node->info_ent->data;
1106850bab44SThierry Reding 	struct drm_crtc *crtc = sor->output.encoder.crtc;
1107850bab44SThierry Reding 	struct drm_device *drm = node->minor->dev;
1108850bab44SThierry Reding 	int err = 0;
1109850bab44SThierry Reding 
1110850bab44SThierry Reding 	drm_modeset_lock_all(drm);
1111850bab44SThierry Reding 
1112850bab44SThierry Reding 	if (!crtc || !crtc->state->active) {
1113850bab44SThierry Reding 		err = -EBUSY;
1114850bab44SThierry Reding 		goto unlock;
1115850bab44SThierry Reding 	}
1116dab16336SThierry Reding 
1117dab16336SThierry Reding #define DUMP_REG(name)						\
1118dab16336SThierry Reding 	seq_printf(s, "%-38s %#05x %08x\n", #name, name,	\
1119dab16336SThierry Reding 		   tegra_sor_readl(sor, name))
1120dab16336SThierry Reding 
1121dab16336SThierry Reding 	DUMP_REG(SOR_CTXSW);
1122a9a9e4fdSThierry Reding 	DUMP_REG(SOR_SUPER_STATE0);
1123a9a9e4fdSThierry Reding 	DUMP_REG(SOR_SUPER_STATE1);
1124a9a9e4fdSThierry Reding 	DUMP_REG(SOR_STATE0);
1125a9a9e4fdSThierry Reding 	DUMP_REG(SOR_STATE1);
1126a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE0(0));
1127a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE0(1));
1128a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE1(0));
1129a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE1(1));
1130a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE2(0));
1131a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE2(1));
1132a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE3(0));
1133a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE3(1));
1134a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE4(0));
1135a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE4(1));
1136a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE5(0));
1137a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE5(1));
1138dab16336SThierry Reding 	DUMP_REG(SOR_CRC_CNTRL);
1139dab16336SThierry Reding 	DUMP_REG(SOR_DP_DEBUG_MVID);
1140dab16336SThierry Reding 	DUMP_REG(SOR_CLK_CNTRL);
1141dab16336SThierry Reding 	DUMP_REG(SOR_CAP);
1142dab16336SThierry Reding 	DUMP_REG(SOR_PWR);
1143dab16336SThierry Reding 	DUMP_REG(SOR_TEST);
1144a9a9e4fdSThierry Reding 	DUMP_REG(SOR_PLL0);
1145a9a9e4fdSThierry Reding 	DUMP_REG(SOR_PLL1);
1146a9a9e4fdSThierry Reding 	DUMP_REG(SOR_PLL2);
1147a9a9e4fdSThierry Reding 	DUMP_REG(SOR_PLL3);
1148dab16336SThierry Reding 	DUMP_REG(SOR_CSTM);
1149dab16336SThierry Reding 	DUMP_REG(SOR_LVDS);
1150a9a9e4fdSThierry Reding 	DUMP_REG(SOR_CRCA);
1151a9a9e4fdSThierry Reding 	DUMP_REG(SOR_CRCB);
1152dab16336SThierry Reding 	DUMP_REG(SOR_BLANK);
1153dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_CTL);
1154dab16336SThierry Reding 	DUMP_REG(SOR_LANE_SEQ_CTL);
1155dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(0));
1156dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(1));
1157dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(2));
1158dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(3));
1159dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(4));
1160dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(5));
1161dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(6));
1162dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(7));
1163dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(8));
1164dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(9));
1165dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(10));
1166dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(11));
1167dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(12));
1168dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(13));
1169dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(14));
1170dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(15));
1171dab16336SThierry Reding 	DUMP_REG(SOR_PWM_DIV);
1172dab16336SThierry Reding 	DUMP_REG(SOR_PWM_CTL);
1173a9a9e4fdSThierry Reding 	DUMP_REG(SOR_VCRC_A0);
1174a9a9e4fdSThierry Reding 	DUMP_REG(SOR_VCRC_A1);
1175a9a9e4fdSThierry Reding 	DUMP_REG(SOR_VCRC_B0);
1176a9a9e4fdSThierry Reding 	DUMP_REG(SOR_VCRC_B1);
1177a9a9e4fdSThierry Reding 	DUMP_REG(SOR_CCRC_A0);
1178a9a9e4fdSThierry Reding 	DUMP_REG(SOR_CCRC_A1);
1179a9a9e4fdSThierry Reding 	DUMP_REG(SOR_CCRC_B0);
1180a9a9e4fdSThierry Reding 	DUMP_REG(SOR_CCRC_B1);
1181a9a9e4fdSThierry Reding 	DUMP_REG(SOR_EDATA_A0);
1182a9a9e4fdSThierry Reding 	DUMP_REG(SOR_EDATA_A1);
1183a9a9e4fdSThierry Reding 	DUMP_REG(SOR_EDATA_B0);
1184a9a9e4fdSThierry Reding 	DUMP_REG(SOR_EDATA_B1);
1185a9a9e4fdSThierry Reding 	DUMP_REG(SOR_COUNT_A0);
1186a9a9e4fdSThierry Reding 	DUMP_REG(SOR_COUNT_A1);
1187a9a9e4fdSThierry Reding 	DUMP_REG(SOR_COUNT_B0);
1188a9a9e4fdSThierry Reding 	DUMP_REG(SOR_COUNT_B1);
1189a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DEBUG_A0);
1190a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DEBUG_A1);
1191a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DEBUG_B0);
1192a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DEBUG_B1);
1193dab16336SThierry Reding 	DUMP_REG(SOR_TRIG);
1194dab16336SThierry Reding 	DUMP_REG(SOR_MSCHECK);
1195dab16336SThierry Reding 	DUMP_REG(SOR_XBAR_CTRL);
1196dab16336SThierry Reding 	DUMP_REG(SOR_XBAR_POL);
1197a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_LINKCTL0);
1198a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_LINKCTL1);
1199a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE_DRIVE_CURRENT0);
1200a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE_DRIVE_CURRENT1);
1201a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE4_DRIVE_CURRENT0);
1202a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE4_DRIVE_CURRENT1);
1203a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE_PREEMPHASIS0);
1204a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE_PREEMPHASIS1);
1205a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE4_PREEMPHASIS0);
1206a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE4_PREEMPHASIS1);
1207a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE_POSTCURSOR0);
1208a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE_POSTCURSOR1);
1209a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_CONFIG0);
1210a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_CONFIG1);
1211a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_MN0);
1212a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_MN1);
1213a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_PADCTL0);
1214a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_PADCTL1);
1215a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_DEBUG0);
1216a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_DEBUG1);
1217a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_SPARE0);
1218a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_SPARE1);
1219dab16336SThierry Reding 	DUMP_REG(SOR_DP_AUDIO_CTRL);
1220dab16336SThierry Reding 	DUMP_REG(SOR_DP_AUDIO_HBLANK_SYMBOLS);
1221dab16336SThierry Reding 	DUMP_REG(SOR_DP_AUDIO_VBLANK_SYMBOLS);
1222dab16336SThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_HEADER);
1223a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK0);
1224a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK1);
1225a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK2);
1226a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK3);
1227a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK4);
1228a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK5);
1229a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK6);
1230dab16336SThierry Reding 	DUMP_REG(SOR_DP_TPG);
1231dab16336SThierry Reding 	DUMP_REG(SOR_DP_TPG_CONFIG);
1232a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_LQ_CSTM0);
1233a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_LQ_CSTM1);
1234a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_LQ_CSTM2);
1235dab16336SThierry Reding 
1236dab16336SThierry Reding #undef DUMP_REG
1237dab16336SThierry Reding 
1238850bab44SThierry Reding unlock:
1239850bab44SThierry Reding 	drm_modeset_unlock_all(drm);
1240850bab44SThierry Reding 	return err;
1241dab16336SThierry Reding }
1242dab16336SThierry Reding 
1243dab16336SThierry Reding static const struct drm_info_list debugfs_files[] = {
1244530239a8SThierry Reding 	{ "crc", tegra_sor_show_crc, 0, NULL },
1245dab16336SThierry Reding 	{ "regs", tegra_sor_show_regs, 0, NULL },
1246dab16336SThierry Reding };
1247dab16336SThierry Reding 
12486fad8f66SThierry Reding static int tegra_sor_debugfs_init(struct tegra_sor *sor,
12496fad8f66SThierry Reding 				  struct drm_minor *minor)
12506fad8f66SThierry Reding {
1251459cc2c6SThierry Reding 	const char *name = sor->soc->supports_dp ? "sor1" : "sor";
1252dab16336SThierry Reding 	unsigned int i;
1253530239a8SThierry Reding 	int err;
12546fad8f66SThierry Reding 
1255459cc2c6SThierry Reding 	sor->debugfs = debugfs_create_dir(name, minor->debugfs_root);
12566fad8f66SThierry Reding 	if (!sor->debugfs)
12576fad8f66SThierry Reding 		return -ENOMEM;
12586fad8f66SThierry Reding 
1259dab16336SThierry Reding 	sor->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1260dab16336SThierry Reding 				     GFP_KERNEL);
1261dab16336SThierry Reding 	if (!sor->debugfs_files) {
12626fad8f66SThierry Reding 		err = -ENOMEM;
12636fad8f66SThierry Reding 		goto remove;
12646fad8f66SThierry Reding 	}
12656fad8f66SThierry Reding 
1266dab16336SThierry Reding 	for (i = 0; i < ARRAY_SIZE(debugfs_files); i++)
1267dab16336SThierry Reding 		sor->debugfs_files[i].data = sor;
1268dab16336SThierry Reding 
1269dab16336SThierry Reding 	err = drm_debugfs_create_files(sor->debugfs_files,
1270dab16336SThierry Reding 				       ARRAY_SIZE(debugfs_files),
1271dab16336SThierry Reding 				       sor->debugfs, minor);
1272dab16336SThierry Reding 	if (err < 0)
1273dab16336SThierry Reding 		goto free;
1274dab16336SThierry Reding 
12753ff1f22cSThierry Reding 	sor->minor = minor;
12763ff1f22cSThierry Reding 
1277530239a8SThierry Reding 	return 0;
12786fad8f66SThierry Reding 
1279dab16336SThierry Reding free:
1280dab16336SThierry Reding 	kfree(sor->debugfs_files);
1281dab16336SThierry Reding 	sor->debugfs_files = NULL;
12826fad8f66SThierry Reding remove:
1283dab16336SThierry Reding 	debugfs_remove_recursive(sor->debugfs);
12846fad8f66SThierry Reding 	sor->debugfs = NULL;
12856fad8f66SThierry Reding 	return err;
12866fad8f66SThierry Reding }
12876fad8f66SThierry Reding 
12884009c224SThierry Reding static void tegra_sor_debugfs_exit(struct tegra_sor *sor)
12896fad8f66SThierry Reding {
1290dab16336SThierry Reding 	drm_debugfs_remove_files(sor->debugfs_files, ARRAY_SIZE(debugfs_files),
1291dab16336SThierry Reding 				 sor->minor);
1292dab16336SThierry Reding 	sor->minor = NULL;
1293dab16336SThierry Reding 
1294dab16336SThierry Reding 	kfree(sor->debugfs_files);
1295066d30f8SThierry Reding 	sor->debugfs_files = NULL;
1296dab16336SThierry Reding 
1297dab16336SThierry Reding 	debugfs_remove_recursive(sor->debugfs);
1298066d30f8SThierry Reding 	sor->debugfs = NULL;
12996fad8f66SThierry Reding }
13006fad8f66SThierry Reding 
1301c31efa7aSThierry Reding static void tegra_sor_connector_reset(struct drm_connector *connector)
1302c31efa7aSThierry Reding {
1303c31efa7aSThierry Reding 	struct tegra_sor_state *state;
1304c31efa7aSThierry Reding 
1305c31efa7aSThierry Reding 	state = kzalloc(sizeof(*state), GFP_KERNEL);
1306c31efa7aSThierry Reding 	if (!state)
1307c31efa7aSThierry Reding 		return;
1308c31efa7aSThierry Reding 
1309c31efa7aSThierry Reding 	if (connector->state) {
1310c31efa7aSThierry Reding 		__drm_atomic_helper_connector_destroy_state(connector->state);
1311c31efa7aSThierry Reding 		kfree(connector->state);
1312c31efa7aSThierry Reding 	}
1313c31efa7aSThierry Reding 
1314c31efa7aSThierry Reding 	__drm_atomic_helper_connector_reset(connector, &state->base);
1315c31efa7aSThierry Reding }
1316c31efa7aSThierry Reding 
13176fad8f66SThierry Reding static enum drm_connector_status
13186fad8f66SThierry Reding tegra_sor_connector_detect(struct drm_connector *connector, bool force)
13196fad8f66SThierry Reding {
13206fad8f66SThierry Reding 	struct tegra_output *output = connector_to_output(connector);
13216fad8f66SThierry Reding 	struct tegra_sor *sor = to_sor(output);
13226fad8f66SThierry Reding 
13239542c237SThierry Reding 	if (sor->aux)
13249542c237SThierry Reding 		return drm_dp_aux_detect(sor->aux);
13256fad8f66SThierry Reding 
1326459cc2c6SThierry Reding 	return tegra_output_connector_detect(connector, force);
13276fad8f66SThierry Reding }
13286fad8f66SThierry Reding 
1329c31efa7aSThierry Reding static struct drm_connector_state *
1330c31efa7aSThierry Reding tegra_sor_connector_duplicate_state(struct drm_connector *connector)
1331c31efa7aSThierry Reding {
1332c31efa7aSThierry Reding 	struct tegra_sor_state *state = to_sor_state(connector->state);
1333c31efa7aSThierry Reding 	struct tegra_sor_state *copy;
1334c31efa7aSThierry Reding 
1335c31efa7aSThierry Reding 	copy = kmemdup(state, sizeof(*state), GFP_KERNEL);
1336c31efa7aSThierry Reding 	if (!copy)
1337c31efa7aSThierry Reding 		return NULL;
1338c31efa7aSThierry Reding 
1339c31efa7aSThierry Reding 	__drm_atomic_helper_connector_duplicate_state(connector, &copy->base);
1340c31efa7aSThierry Reding 
1341c31efa7aSThierry Reding 	return &copy->base;
1342c31efa7aSThierry Reding }
1343c31efa7aSThierry Reding 
13446fad8f66SThierry Reding static const struct drm_connector_funcs tegra_sor_connector_funcs = {
1345850bab44SThierry Reding 	.dpms = drm_atomic_helper_connector_dpms,
1346c31efa7aSThierry Reding 	.reset = tegra_sor_connector_reset,
13476fad8f66SThierry Reding 	.detect = tegra_sor_connector_detect,
13486fad8f66SThierry Reding 	.fill_modes = drm_helper_probe_single_connector_modes,
13496fad8f66SThierry Reding 	.destroy = tegra_output_connector_destroy,
1350c31efa7aSThierry Reding 	.atomic_duplicate_state = tegra_sor_connector_duplicate_state,
13514aa3df71SThierry Reding 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
13526fad8f66SThierry Reding };
13536fad8f66SThierry Reding 
13546fad8f66SThierry Reding static int tegra_sor_connector_get_modes(struct drm_connector *connector)
13556fad8f66SThierry Reding {
13566fad8f66SThierry Reding 	struct tegra_output *output = connector_to_output(connector);
13576fad8f66SThierry Reding 	struct tegra_sor *sor = to_sor(output);
13586fad8f66SThierry Reding 	int err;
13596fad8f66SThierry Reding 
13609542c237SThierry Reding 	if (sor->aux)
13619542c237SThierry Reding 		drm_dp_aux_enable(sor->aux);
13626fad8f66SThierry Reding 
13636fad8f66SThierry Reding 	err = tegra_output_connector_get_modes(connector);
13646fad8f66SThierry Reding 
13659542c237SThierry Reding 	if (sor->aux)
13669542c237SThierry Reding 		drm_dp_aux_disable(sor->aux);
13676fad8f66SThierry Reding 
13686fad8f66SThierry Reding 	return err;
13696fad8f66SThierry Reding }
13706fad8f66SThierry Reding 
13716fad8f66SThierry Reding static enum drm_mode_status
13726fad8f66SThierry Reding tegra_sor_connector_mode_valid(struct drm_connector *connector,
13736fad8f66SThierry Reding 			       struct drm_display_mode *mode)
13746fad8f66SThierry Reding {
13756fad8f66SThierry Reding 	return MODE_OK;
13766fad8f66SThierry Reding }
13776fad8f66SThierry Reding 
13786fad8f66SThierry Reding static const struct drm_connector_helper_funcs tegra_sor_connector_helper_funcs = {
13796fad8f66SThierry Reding 	.get_modes = tegra_sor_connector_get_modes,
13806fad8f66SThierry Reding 	.mode_valid = tegra_sor_connector_mode_valid,
13816fad8f66SThierry Reding 	.best_encoder = tegra_output_connector_best_encoder,
13826fad8f66SThierry Reding };
13836fad8f66SThierry Reding 
13846fad8f66SThierry Reding static const struct drm_encoder_funcs tegra_sor_encoder_funcs = {
13856fad8f66SThierry Reding 	.destroy = tegra_output_encoder_destroy,
13866fad8f66SThierry Reding };
13876fad8f66SThierry Reding 
1388850bab44SThierry Reding static void tegra_sor_edp_disable(struct drm_encoder *encoder)
13896fad8f66SThierry Reding {
1390850bab44SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
1391850bab44SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
1392850bab44SThierry Reding 	struct tegra_sor *sor = to_sor(output);
1393850bab44SThierry Reding 	u32 value;
1394850bab44SThierry Reding 	int err;
1395850bab44SThierry Reding 
1396850bab44SThierry Reding 	if (output->panel)
1397850bab44SThierry Reding 		drm_panel_disable(output->panel);
1398850bab44SThierry Reding 
1399850bab44SThierry Reding 	err = tegra_sor_detach(sor);
1400850bab44SThierry Reding 	if (err < 0)
1401850bab44SThierry Reding 		dev_err(sor->dev, "failed to detach SOR: %d\n", err);
1402850bab44SThierry Reding 
1403850bab44SThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE1);
1404850bab44SThierry Reding 	tegra_sor_update(sor);
1405850bab44SThierry Reding 
1406850bab44SThierry Reding 	/*
1407850bab44SThierry Reding 	 * The following accesses registers of the display controller, so make
1408850bab44SThierry Reding 	 * sure it's only executed when the output is attached to one.
1409850bab44SThierry Reding 	 */
1410850bab44SThierry Reding 	if (dc) {
1411850bab44SThierry Reding 		value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
1412850bab44SThierry Reding 		value &= ~SOR_ENABLE;
1413850bab44SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
1414850bab44SThierry Reding 
1415850bab44SThierry Reding 		tegra_dc_commit(dc);
14166fad8f66SThierry Reding 	}
14176fad8f66SThierry Reding 
1418850bab44SThierry Reding 	err = tegra_sor_power_down(sor);
1419850bab44SThierry Reding 	if (err < 0)
1420850bab44SThierry Reding 		dev_err(sor->dev, "failed to power down SOR: %d\n", err);
1421850bab44SThierry Reding 
14229542c237SThierry Reding 	if (sor->aux) {
14239542c237SThierry Reding 		err = drm_dp_aux_disable(sor->aux);
1424850bab44SThierry Reding 		if (err < 0)
1425850bab44SThierry Reding 			dev_err(sor->dev, "failed to disable DP: %d\n", err);
14266fad8f66SThierry Reding 	}
14276fad8f66SThierry Reding 
1428850bab44SThierry Reding 	err = tegra_io_rail_power_off(TEGRA_IO_RAIL_LVDS);
1429850bab44SThierry Reding 	if (err < 0)
1430850bab44SThierry Reding 		dev_err(sor->dev, "failed to power off I/O rail: %d\n", err);
1431850bab44SThierry Reding 
1432850bab44SThierry Reding 	if (output->panel)
1433850bab44SThierry Reding 		drm_panel_unprepare(output->panel);
1434850bab44SThierry Reding 
1435aaff8bd2SThierry Reding 	pm_runtime_put(sor->dev);
14366fad8f66SThierry Reding }
14376fad8f66SThierry Reding 
1438459cc2c6SThierry Reding #if 0
1439459cc2c6SThierry Reding static int calc_h_ref_to_sync(const struct drm_display_mode *mode,
1440459cc2c6SThierry Reding 			      unsigned int *value)
1441459cc2c6SThierry Reding {
1442459cc2c6SThierry Reding 	unsigned int hfp, hsw, hbp, a = 0, b;
1443459cc2c6SThierry Reding 
1444459cc2c6SThierry Reding 	hfp = mode->hsync_start - mode->hdisplay;
1445459cc2c6SThierry Reding 	hsw = mode->hsync_end - mode->hsync_start;
1446459cc2c6SThierry Reding 	hbp = mode->htotal - mode->hsync_end;
1447459cc2c6SThierry Reding 
1448459cc2c6SThierry Reding 	pr_info("hfp: %u, hsw: %u, hbp: %u\n", hfp, hsw, hbp);
1449459cc2c6SThierry Reding 
1450459cc2c6SThierry Reding 	b = hfp - 1;
1451459cc2c6SThierry Reding 
1452459cc2c6SThierry Reding 	pr_info("a: %u, b: %u\n", a, b);
1453459cc2c6SThierry Reding 	pr_info("a + hsw + hbp = %u\n", a + hsw + hbp);
1454459cc2c6SThierry Reding 
1455459cc2c6SThierry Reding 	if (a + hsw + hbp <= 11) {
1456459cc2c6SThierry Reding 		a = 1 + 11 - hsw - hbp;
1457459cc2c6SThierry Reding 		pr_info("a: %u\n", a);
1458459cc2c6SThierry Reding 	}
1459459cc2c6SThierry Reding 
1460459cc2c6SThierry Reding 	if (a > b)
1461459cc2c6SThierry Reding 		return -EINVAL;
1462459cc2c6SThierry Reding 
1463459cc2c6SThierry Reding 	if (hsw < 1)
1464459cc2c6SThierry Reding 		return -EINVAL;
1465459cc2c6SThierry Reding 
1466459cc2c6SThierry Reding 	if (mode->hdisplay < 16)
1467459cc2c6SThierry Reding 		return -EINVAL;
1468459cc2c6SThierry Reding 
1469459cc2c6SThierry Reding 	if (value) {
1470459cc2c6SThierry Reding 		if (b > a && a % 2)
1471459cc2c6SThierry Reding 			*value = a + 1;
1472459cc2c6SThierry Reding 		else
1473459cc2c6SThierry Reding 			*value = a;
1474459cc2c6SThierry Reding 	}
1475459cc2c6SThierry Reding 
1476459cc2c6SThierry Reding 	return 0;
1477459cc2c6SThierry Reding }
1478459cc2c6SThierry Reding #endif
1479459cc2c6SThierry Reding 
1480850bab44SThierry Reding static void tegra_sor_edp_enable(struct drm_encoder *encoder)
14816fad8f66SThierry Reding {
1482850bab44SThierry Reding 	struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
14836fad8f66SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
14846fad8f66SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
14856b6b6042SThierry Reding 	struct tegra_sor *sor = to_sor(output);
148634fa183bSThierry Reding 	struct tegra_sor_config config;
1487c31efa7aSThierry Reding 	struct tegra_sor_state *state;
148834fa183bSThierry Reding 	struct drm_dp_link link;
148901b9bea0SThierry Reding 	u8 rate, lanes;
14902bd1dd39SThierry Reding 	unsigned int i;
149186f5c52dSThierry Reding 	int err = 0;
149228fe2076SThierry Reding 	u32 value;
149386f5c52dSThierry Reding 
1494c31efa7aSThierry Reding 	state = to_sor_state(output->connector.state);
14952bd1dd39SThierry Reding 
1496aaff8bd2SThierry Reding 	pm_runtime_get_sync(sor->dev);
14976b6b6042SThierry Reding 
14986fad8f66SThierry Reding 	if (output->panel)
14996fad8f66SThierry Reding 		drm_panel_prepare(output->panel);
15006fad8f66SThierry Reding 
15019542c237SThierry Reding 	err = drm_dp_aux_enable(sor->aux);
15026b6b6042SThierry Reding 	if (err < 0)
15036b6b6042SThierry Reding 		dev_err(sor->dev, "failed to enable DP: %d\n", err);
150434fa183bSThierry Reding 
15059542c237SThierry Reding 	err = drm_dp_link_probe(sor->aux, &link);
150634fa183bSThierry Reding 	if (err < 0) {
150701b9bea0SThierry Reding 		dev_err(sor->dev, "failed to probe eDP link: %d\n", err);
1508850bab44SThierry Reding 		return;
150934fa183bSThierry Reding 	}
15106b6b6042SThierry Reding 
151125bb2cecSThierry Reding 	/* switch to safe parent clock */
151225bb2cecSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
15136b6b6042SThierry Reding 	if (err < 0)
15146b6b6042SThierry Reding 		dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
15156b6b6042SThierry Reding 
151634fa183bSThierry Reding 	memset(&config, 0, sizeof(config));
1517c31efa7aSThierry Reding 	config.bits_per_pixel = state->bpc * 3;
151834fa183bSThierry Reding 
1519a198359eSThierry Reding 	err = tegra_sor_compute_config(sor, mode, &config, &link);
152034fa183bSThierry Reding 	if (err < 0)
1521a198359eSThierry Reding 		dev_err(sor->dev, "failed to compute configuration: %d\n", err);
152234fa183bSThierry Reding 
15236b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
15246b6b6042SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
15256b6b6042SThierry Reding 	value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
15266b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
15276b6b6042SThierry Reding 
1528a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1529a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
1530a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
15316b6b6042SThierry Reding 	usleep_range(20, 100);
15326b6b6042SThierry Reding 
1533a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL3);
1534a9a9e4fdSThierry Reding 	value |= SOR_PLL3_PLL_VDD_MODE_3V3;
1535a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL3);
15366b6b6042SThierry Reding 
1537a9a9e4fdSThierry Reding 	value = SOR_PLL0_ICHPMP(0xf) | SOR_PLL0_VCOCAP_RST |
1538a9a9e4fdSThierry Reding 		SOR_PLL0_PLLREG_LEVEL_V45 | SOR_PLL0_RESISTOR_EXT;
1539a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL0);
15406b6b6042SThierry Reding 
1541a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1542a9a9e4fdSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD;
1543a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1544a9a9e4fdSThierry Reding 	value |= SOR_PLL2_LVDS_ENABLE;
1545a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
15466b6b6042SThierry Reding 
1547a9a9e4fdSThierry Reding 	value = SOR_PLL1_TERM_COMPOUT | SOR_PLL1_TMDS_TERM;
1548a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL1);
15496b6b6042SThierry Reding 
15506b6b6042SThierry Reding 	while (true) {
1551a9a9e4fdSThierry Reding 		value = tegra_sor_readl(sor, SOR_PLL2);
1552a9a9e4fdSThierry Reding 		if ((value & SOR_PLL2_SEQ_PLLCAPPD_ENFORCE) == 0)
15536b6b6042SThierry Reding 			break;
15546b6b6042SThierry Reding 
15556b6b6042SThierry Reding 		usleep_range(250, 1000);
15566b6b6042SThierry Reding 	}
15576b6b6042SThierry Reding 
1558a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1559a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
1560a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_PORT_POWERDOWN;
1561a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
15626b6b6042SThierry Reding 
15636b6b6042SThierry Reding 	/*
15646b6b6042SThierry Reding 	 * power up
15656b6b6042SThierry Reding 	 */
15666b6b6042SThierry Reding 
15676b6b6042SThierry Reding 	/* set safe link bandwidth (1.62 Gbps) */
15686b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
15696b6b6042SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
15706b6b6042SThierry Reding 	value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G1_62;
15716b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
15726b6b6042SThierry Reding 
15736b6b6042SThierry Reding 	/* step 1 */
1574a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1575a9a9e4fdSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE | SOR_PLL2_PORT_POWERDOWN |
1576a9a9e4fdSThierry Reding 		 SOR_PLL2_BANDGAP_POWERDOWN;
1577a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
15786b6b6042SThierry Reding 
1579a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL0);
1580a9a9e4fdSThierry Reding 	value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
1581a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL0);
15826b6b6042SThierry Reding 
1583a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
15846b6b6042SThierry Reding 	value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
1585a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
15866b6b6042SThierry Reding 
15876b6b6042SThierry Reding 	/* step 2 */
15886b6b6042SThierry Reding 	err = tegra_io_rail_power_on(TEGRA_IO_RAIL_LVDS);
1589850bab44SThierry Reding 	if (err < 0)
15906b6b6042SThierry Reding 		dev_err(sor->dev, "failed to power on I/O rail: %d\n", err);
15916b6b6042SThierry Reding 
15926b6b6042SThierry Reding 	usleep_range(5, 100);
15936b6b6042SThierry Reding 
15946b6b6042SThierry Reding 	/* step 3 */
1595a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1596a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
1597a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
15986b6b6042SThierry Reding 
15996b6b6042SThierry Reding 	usleep_range(20, 100);
16006b6b6042SThierry Reding 
16016b6b6042SThierry Reding 	/* step 4 */
1602a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL0);
1603a9a9e4fdSThierry Reding 	value &= ~SOR_PLL0_VCOPD;
1604a9a9e4fdSThierry Reding 	value &= ~SOR_PLL0_PWR;
1605a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL0);
16066b6b6042SThierry Reding 
1607a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1608a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1609a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
16106b6b6042SThierry Reding 
16116b6b6042SThierry Reding 	usleep_range(200, 1000);
16126b6b6042SThierry Reding 
16136b6b6042SThierry Reding 	/* step 5 */
1614a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1615a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_PORT_POWERDOWN;
1616a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
16176b6b6042SThierry Reding 
1618*30b49435SThierry Reding 	/* XXX not in TRM */
1619*30b49435SThierry Reding 	for (value = 0, i = 0; i < 5; i++)
1620*30b49435SThierry Reding 		value |= SOR_XBAR_CTRL_LINK0_XSEL(i, sor->soc->xbar_cfg[i]) |
1621*30b49435SThierry Reding 			 SOR_XBAR_CTRL_LINK1_XSEL(i, i);
1622*30b49435SThierry Reding 
1623*30b49435SThierry Reding 	tegra_sor_writel(sor, 0x00000000, SOR_XBAR_POL);
1624*30b49435SThierry Reding 	tegra_sor_writel(sor, value, SOR_XBAR_CTRL);
1625*30b49435SThierry Reding 
162625bb2cecSThierry Reding 	/* switch to DP parent clock */
162725bb2cecSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_dp);
16286b6b6042SThierry Reding 	if (err < 0)
162925bb2cecSThierry Reding 		dev_err(sor->dev, "failed to set parent clock: %d\n", err);
16306b6b6042SThierry Reding 
1631899451b7SThierry Reding 	/* power DP lanes */
1632a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
1633899451b7SThierry Reding 
1634899451b7SThierry Reding 	if (link.num_lanes <= 2)
1635899451b7SThierry Reding 		value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2);
1636899451b7SThierry Reding 	else
1637899451b7SThierry Reding 		value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2;
1638899451b7SThierry Reding 
1639899451b7SThierry Reding 	if (link.num_lanes <= 1)
1640899451b7SThierry Reding 		value &= ~SOR_DP_PADCTL_PD_TXD_1;
1641899451b7SThierry Reding 	else
1642899451b7SThierry Reding 		value |= SOR_DP_PADCTL_PD_TXD_1;
1643899451b7SThierry Reding 
1644899451b7SThierry Reding 	if (link.num_lanes == 0)
1645899451b7SThierry Reding 		value &= ~SOR_DP_PADCTL_PD_TXD_0;
1646899451b7SThierry Reding 	else
1647899451b7SThierry Reding 		value |= SOR_DP_PADCTL_PD_TXD_0;
1648899451b7SThierry Reding 
1649a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
16506b6b6042SThierry Reding 
1651a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
16526b6b6042SThierry Reding 	value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
16530c90a184SThierry Reding 	value |= SOR_DP_LINKCTL_LANE_COUNT(link.num_lanes);
1654a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
16556b6b6042SThierry Reding 
16566b6b6042SThierry Reding 	/* start lane sequencer */
16576b6b6042SThierry Reding 	value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
16586b6b6042SThierry Reding 		SOR_LANE_SEQ_CTL_POWER_STATE_UP;
16596b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
16606b6b6042SThierry Reding 
16616b6b6042SThierry Reding 	while (true) {
16626b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
16636b6b6042SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
16646b6b6042SThierry Reding 			break;
16656b6b6042SThierry Reding 
16666b6b6042SThierry Reding 		usleep_range(250, 1000);
16676b6b6042SThierry Reding 	}
16686b6b6042SThierry Reding 
1669a4263fedSThierry Reding 	/* set link bandwidth */
16706b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
16716b6b6042SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
1672a4263fedSThierry Reding 	value |= drm_dp_link_rate_to_bw_code(link.rate) << 2;
16736b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
16746b6b6042SThierry Reding 
1675402f6bcdSThierry Reding 	tegra_sor_apply_config(sor, &config);
1676402f6bcdSThierry Reding 
1677402f6bcdSThierry Reding 	/* enable link */
1678a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
16796b6b6042SThierry Reding 	value |= SOR_DP_LINKCTL_ENABLE;
16806b6b6042SThierry Reding 	value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
1681a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
16826b6b6042SThierry Reding 
16836b6b6042SThierry Reding 	for (i = 0, value = 0; i < 4; i++) {
16846b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
16856b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_GALIOS |
16866b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_NONE;
16876b6b6042SThierry Reding 		value = (value << 8) | lane;
16886b6b6042SThierry Reding 	}
16896b6b6042SThierry Reding 
16906b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
16916b6b6042SThierry Reding 
16926b6b6042SThierry Reding 	/* enable pad calibration logic */
1693a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
16946b6b6042SThierry Reding 	value |= SOR_DP_PADCTL_PAD_CAL_PD;
1695a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
16966b6b6042SThierry Reding 
16979542c237SThierry Reding 	err = drm_dp_link_probe(sor->aux, &link);
1698850bab44SThierry Reding 	if (err < 0)
169901b9bea0SThierry Reding 		dev_err(sor->dev, "failed to probe eDP link: %d\n", err);
17006b6b6042SThierry Reding 
17019542c237SThierry Reding 	err = drm_dp_link_power_up(sor->aux, &link);
1702850bab44SThierry Reding 	if (err < 0)
170301b9bea0SThierry Reding 		dev_err(sor->dev, "failed to power up eDP link: %d\n", err);
17046b6b6042SThierry Reding 
17059542c237SThierry Reding 	err = drm_dp_link_configure(sor->aux, &link);
1706850bab44SThierry Reding 	if (err < 0)
170701b9bea0SThierry Reding 		dev_err(sor->dev, "failed to configure eDP link: %d\n", err);
17086b6b6042SThierry Reding 
17096b6b6042SThierry Reding 	rate = drm_dp_link_rate_to_bw_code(link.rate);
17106b6b6042SThierry Reding 	lanes = link.num_lanes;
17116b6b6042SThierry Reding 
17126b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
17136b6b6042SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
17146b6b6042SThierry Reding 	value |= SOR_CLK_CNTRL_DP_LINK_SPEED(rate);
17156b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
17166b6b6042SThierry Reding 
1717a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
17186b6b6042SThierry Reding 	value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
17196b6b6042SThierry Reding 	value |= SOR_DP_LINKCTL_LANE_COUNT(lanes);
17206b6b6042SThierry Reding 
17216b6b6042SThierry Reding 	if (link.capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
17226b6b6042SThierry Reding 		value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
17236b6b6042SThierry Reding 
1724a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
17256b6b6042SThierry Reding 
17266b6b6042SThierry Reding 	/* disable training pattern generator */
17276b6b6042SThierry Reding 
17286b6b6042SThierry Reding 	for (i = 0; i < link.num_lanes; i++) {
17296b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
17306b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_GALIOS |
17316b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_NONE;
17326b6b6042SThierry Reding 		value = (value << 8) | lane;
17336b6b6042SThierry Reding 	}
17346b6b6042SThierry Reding 
17356b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
17366b6b6042SThierry Reding 
17376b6b6042SThierry Reding 	err = tegra_sor_dp_train_fast(sor, &link);
173801b9bea0SThierry Reding 	if (err < 0)
173901b9bea0SThierry Reding 		dev_err(sor->dev, "DP fast link training failed: %d\n", err);
17406b6b6042SThierry Reding 
17416b6b6042SThierry Reding 	dev_dbg(sor->dev, "fast link training succeeded\n");
17426b6b6042SThierry Reding 
17436b6b6042SThierry Reding 	err = tegra_sor_power_up(sor, 250);
1744850bab44SThierry Reding 	if (err < 0)
17456b6b6042SThierry Reding 		dev_err(sor->dev, "failed to power up SOR: %d\n", err);
17466b6b6042SThierry Reding 
17476b6b6042SThierry Reding 	/* CSTM (LVDS, link A/B, upper) */
1748143b1df2SStéphane Marchesin 	value = SOR_CSTM_LVDS | SOR_CSTM_LINK_ACT_A | SOR_CSTM_LINK_ACT_B |
17496b6b6042SThierry Reding 		SOR_CSTM_UPPER;
17506b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CSTM);
17516b6b6042SThierry Reding 
17522bd1dd39SThierry Reding 	/* use DP-A protocol */
17532bd1dd39SThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
17542bd1dd39SThierry Reding 	value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
17552bd1dd39SThierry Reding 	value |= SOR_STATE_ASY_PROTOCOL_DP_A;
17562bd1dd39SThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
17572bd1dd39SThierry Reding 
1758c31efa7aSThierry Reding 	tegra_sor_mode_set(sor, mode, state);
17592bd1dd39SThierry Reding 
17606b6b6042SThierry Reding 	/* PWM setup */
17616b6b6042SThierry Reding 	err = tegra_sor_setup_pwm(sor, 250);
1762850bab44SThierry Reding 	if (err < 0)
17636b6b6042SThierry Reding 		dev_err(sor->dev, "failed to setup PWM: %d\n", err);
17646b6b6042SThierry Reding 
1765666cb873SThierry Reding 	tegra_sor_update(sor);
1766666cb873SThierry Reding 
17676b6b6042SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
17686b6b6042SThierry Reding 	value |= SOR_ENABLE;
17696b6b6042SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
17706b6b6042SThierry Reding 
1771666cb873SThierry Reding 	tegra_dc_commit(dc);
17726b6b6042SThierry Reding 
17736b6b6042SThierry Reding 	err = tegra_sor_attach(sor);
1774850bab44SThierry Reding 	if (err < 0)
17756b6b6042SThierry Reding 		dev_err(sor->dev, "failed to attach SOR: %d\n", err);
17766b6b6042SThierry Reding 
17776b6b6042SThierry Reding 	err = tegra_sor_wakeup(sor);
1778850bab44SThierry Reding 	if (err < 0)
17796b6b6042SThierry Reding 		dev_err(sor->dev, "failed to enable DC: %d\n", err);
17806b6b6042SThierry Reding 
17816fad8f66SThierry Reding 	if (output->panel)
17826fad8f66SThierry Reding 		drm_panel_enable(output->panel);
17836b6b6042SThierry Reding }
17846b6b6042SThierry Reding 
178582f1511cSThierry Reding static int
178682f1511cSThierry Reding tegra_sor_encoder_atomic_check(struct drm_encoder *encoder,
178782f1511cSThierry Reding 			       struct drm_crtc_state *crtc_state,
178882f1511cSThierry Reding 			       struct drm_connector_state *conn_state)
178982f1511cSThierry Reding {
179082f1511cSThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
1791c31efa7aSThierry Reding 	struct tegra_sor_state *state = to_sor_state(conn_state);
179282f1511cSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(conn_state->crtc);
179382f1511cSThierry Reding 	unsigned long pclk = crtc_state->mode.clock * 1000;
179482f1511cSThierry Reding 	struct tegra_sor *sor = to_sor(output);
1795c31efa7aSThierry Reding 	struct drm_display_info *info;
179682f1511cSThierry Reding 	int err;
179782f1511cSThierry Reding 
1798c31efa7aSThierry Reding 	info = &output->connector.display_info;
1799c31efa7aSThierry Reding 
180082f1511cSThierry Reding 	err = tegra_dc_state_setup_clock(dc, crtc_state, sor->clk_parent,
180182f1511cSThierry Reding 					 pclk, 0);
180282f1511cSThierry Reding 	if (err < 0) {
180382f1511cSThierry Reding 		dev_err(output->dev, "failed to setup CRTC state: %d\n", err);
180482f1511cSThierry Reding 		return err;
180582f1511cSThierry Reding 	}
180682f1511cSThierry Reding 
1807c31efa7aSThierry Reding 	switch (info->bpc) {
1808c31efa7aSThierry Reding 	case 8:
1809c31efa7aSThierry Reding 	case 6:
1810c31efa7aSThierry Reding 		state->bpc = info->bpc;
1811c31efa7aSThierry Reding 		break;
1812c31efa7aSThierry Reding 
1813c31efa7aSThierry Reding 	default:
1814c31efa7aSThierry Reding 		DRM_DEBUG_KMS("%u bits-per-color not supported\n", info->bpc);
1815c31efa7aSThierry Reding 		state->bpc = 8;
1816c31efa7aSThierry Reding 		break;
1817c31efa7aSThierry Reding 	}
1818c31efa7aSThierry Reding 
181982f1511cSThierry Reding 	return 0;
182082f1511cSThierry Reding }
182182f1511cSThierry Reding 
1822459cc2c6SThierry Reding static const struct drm_encoder_helper_funcs tegra_sor_edp_helpers = {
1823850bab44SThierry Reding 	.disable = tegra_sor_edp_disable,
1824850bab44SThierry Reding 	.enable = tegra_sor_edp_enable,
182582f1511cSThierry Reding 	.atomic_check = tegra_sor_encoder_atomic_check,
18266b6b6042SThierry Reding };
18276b6b6042SThierry Reding 
1828459cc2c6SThierry Reding static inline u32 tegra_sor_hdmi_subpack(const u8 *ptr, size_t size)
1829459cc2c6SThierry Reding {
1830459cc2c6SThierry Reding 	u32 value = 0;
1831459cc2c6SThierry Reding 	size_t i;
1832459cc2c6SThierry Reding 
1833459cc2c6SThierry Reding 	for (i = size; i > 0; i--)
1834459cc2c6SThierry Reding 		value = (value << 8) | ptr[i - 1];
1835459cc2c6SThierry Reding 
1836459cc2c6SThierry Reding 	return value;
1837459cc2c6SThierry Reding }
1838459cc2c6SThierry Reding 
1839459cc2c6SThierry Reding static void tegra_sor_hdmi_write_infopack(struct tegra_sor *sor,
1840459cc2c6SThierry Reding 					  const void *data, size_t size)
1841459cc2c6SThierry Reding {
1842459cc2c6SThierry Reding 	const u8 *ptr = data;
1843459cc2c6SThierry Reding 	unsigned long offset;
1844459cc2c6SThierry Reding 	size_t i, j;
1845459cc2c6SThierry Reding 	u32 value;
1846459cc2c6SThierry Reding 
1847459cc2c6SThierry Reding 	switch (ptr[0]) {
1848459cc2c6SThierry Reding 	case HDMI_INFOFRAME_TYPE_AVI:
1849459cc2c6SThierry Reding 		offset = SOR_HDMI_AVI_INFOFRAME_HEADER;
1850459cc2c6SThierry Reding 		break;
1851459cc2c6SThierry Reding 
1852459cc2c6SThierry Reding 	case HDMI_INFOFRAME_TYPE_AUDIO:
1853459cc2c6SThierry Reding 		offset = SOR_HDMI_AUDIO_INFOFRAME_HEADER;
1854459cc2c6SThierry Reding 		break;
1855459cc2c6SThierry Reding 
1856459cc2c6SThierry Reding 	case HDMI_INFOFRAME_TYPE_VENDOR:
1857459cc2c6SThierry Reding 		offset = SOR_HDMI_VSI_INFOFRAME_HEADER;
1858459cc2c6SThierry Reding 		break;
1859459cc2c6SThierry Reding 
1860459cc2c6SThierry Reding 	default:
1861459cc2c6SThierry Reding 		dev_err(sor->dev, "unsupported infoframe type: %02x\n",
1862459cc2c6SThierry Reding 			ptr[0]);
1863459cc2c6SThierry Reding 		return;
1864459cc2c6SThierry Reding 	}
1865459cc2c6SThierry Reding 
1866459cc2c6SThierry Reding 	value = INFOFRAME_HEADER_TYPE(ptr[0]) |
1867459cc2c6SThierry Reding 		INFOFRAME_HEADER_VERSION(ptr[1]) |
1868459cc2c6SThierry Reding 		INFOFRAME_HEADER_LEN(ptr[2]);
1869459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, offset);
1870459cc2c6SThierry Reding 	offset++;
1871459cc2c6SThierry Reding 
1872459cc2c6SThierry Reding 	/*
1873459cc2c6SThierry Reding 	 * Each subpack contains 7 bytes, divided into:
1874459cc2c6SThierry Reding 	 * - subpack_low: bytes 0 - 3
1875459cc2c6SThierry Reding 	 * - subpack_high: bytes 4 - 6 (with byte 7 padded to 0x00)
1876459cc2c6SThierry Reding 	 */
1877459cc2c6SThierry Reding 	for (i = 3, j = 0; i < size; i += 7, j += 8) {
1878459cc2c6SThierry Reding 		size_t rem = size - i, num = min_t(size_t, rem, 4);
1879459cc2c6SThierry Reding 
1880459cc2c6SThierry Reding 		value = tegra_sor_hdmi_subpack(&ptr[i], num);
1881459cc2c6SThierry Reding 		tegra_sor_writel(sor, value, offset++);
1882459cc2c6SThierry Reding 
1883459cc2c6SThierry Reding 		num = min_t(size_t, rem - num, 3);
1884459cc2c6SThierry Reding 
1885459cc2c6SThierry Reding 		value = tegra_sor_hdmi_subpack(&ptr[i + 4], num);
1886459cc2c6SThierry Reding 		tegra_sor_writel(sor, value, offset++);
1887459cc2c6SThierry Reding 	}
1888459cc2c6SThierry Reding }
1889459cc2c6SThierry Reding 
1890459cc2c6SThierry Reding static int
1891459cc2c6SThierry Reding tegra_sor_hdmi_setup_avi_infoframe(struct tegra_sor *sor,
1892459cc2c6SThierry Reding 				   const struct drm_display_mode *mode)
1893459cc2c6SThierry Reding {
1894459cc2c6SThierry Reding 	u8 buffer[HDMI_INFOFRAME_SIZE(AVI)];
1895459cc2c6SThierry Reding 	struct hdmi_avi_infoframe frame;
1896459cc2c6SThierry Reding 	u32 value;
1897459cc2c6SThierry Reding 	int err;
1898459cc2c6SThierry Reding 
1899459cc2c6SThierry Reding 	/* disable AVI infoframe */
1900459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI_AVI_INFOFRAME_CTRL);
1901459cc2c6SThierry Reding 	value &= ~INFOFRAME_CTRL_SINGLE;
1902459cc2c6SThierry Reding 	value &= ~INFOFRAME_CTRL_OTHER;
1903459cc2c6SThierry Reding 	value &= ~INFOFRAME_CTRL_ENABLE;
1904459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_AVI_INFOFRAME_CTRL);
1905459cc2c6SThierry Reding 
1906459cc2c6SThierry Reding 	err = drm_hdmi_avi_infoframe_from_display_mode(&frame, mode);
1907459cc2c6SThierry Reding 	if (err < 0) {
1908459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to setup AVI infoframe: %d\n", err);
1909459cc2c6SThierry Reding 		return err;
1910459cc2c6SThierry Reding 	}
1911459cc2c6SThierry Reding 
1912459cc2c6SThierry Reding 	err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
1913459cc2c6SThierry Reding 	if (err < 0) {
1914459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to pack AVI infoframe: %d\n", err);
1915459cc2c6SThierry Reding 		return err;
1916459cc2c6SThierry Reding 	}
1917459cc2c6SThierry Reding 
1918459cc2c6SThierry Reding 	tegra_sor_hdmi_write_infopack(sor, buffer, err);
1919459cc2c6SThierry Reding 
1920459cc2c6SThierry Reding 	/* enable AVI infoframe */
1921459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI_AVI_INFOFRAME_CTRL);
1922459cc2c6SThierry Reding 	value |= INFOFRAME_CTRL_CHECKSUM_ENABLE;
1923459cc2c6SThierry Reding 	value |= INFOFRAME_CTRL_ENABLE;
1924459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_AVI_INFOFRAME_CTRL);
1925459cc2c6SThierry Reding 
1926459cc2c6SThierry Reding 	return 0;
1927459cc2c6SThierry Reding }
1928459cc2c6SThierry Reding 
1929459cc2c6SThierry Reding static void tegra_sor_hdmi_disable_audio_infoframe(struct tegra_sor *sor)
1930459cc2c6SThierry Reding {
1931459cc2c6SThierry Reding 	u32 value;
1932459cc2c6SThierry Reding 
1933459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
1934459cc2c6SThierry Reding 	value &= ~INFOFRAME_CTRL_ENABLE;
1935459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
1936459cc2c6SThierry Reding }
1937459cc2c6SThierry Reding 
1938459cc2c6SThierry Reding static struct tegra_sor_hdmi_settings *
1939459cc2c6SThierry Reding tegra_sor_hdmi_find_settings(struct tegra_sor *sor, unsigned long frequency)
1940459cc2c6SThierry Reding {
1941459cc2c6SThierry Reding 	unsigned int i;
1942459cc2c6SThierry Reding 
1943459cc2c6SThierry Reding 	for (i = 0; i < sor->num_settings; i++)
1944459cc2c6SThierry Reding 		if (frequency <= sor->settings[i].frequency)
1945459cc2c6SThierry Reding 			return &sor->settings[i];
1946459cc2c6SThierry Reding 
1947459cc2c6SThierry Reding 	return NULL;
1948459cc2c6SThierry Reding }
1949459cc2c6SThierry Reding 
1950459cc2c6SThierry Reding static void tegra_sor_hdmi_disable(struct drm_encoder *encoder)
1951459cc2c6SThierry Reding {
1952459cc2c6SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
1953459cc2c6SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
1954459cc2c6SThierry Reding 	struct tegra_sor *sor = to_sor(output);
1955459cc2c6SThierry Reding 	u32 value;
1956459cc2c6SThierry Reding 	int err;
1957459cc2c6SThierry Reding 
1958459cc2c6SThierry Reding 	err = tegra_sor_detach(sor);
1959459cc2c6SThierry Reding 	if (err < 0)
1960459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to detach SOR: %d\n", err);
1961459cc2c6SThierry Reding 
1962459cc2c6SThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE1);
1963459cc2c6SThierry Reding 	tegra_sor_update(sor);
1964459cc2c6SThierry Reding 
1965459cc2c6SThierry Reding 	/* disable display to SOR clock */
1966459cc2c6SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
1967459cc2c6SThierry Reding 	value &= ~SOR1_TIMING_CYA;
1968459cc2c6SThierry Reding 	value &= ~SOR1_ENABLE;
1969459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
1970459cc2c6SThierry Reding 
1971459cc2c6SThierry Reding 	tegra_dc_commit(dc);
1972459cc2c6SThierry Reding 
1973459cc2c6SThierry Reding 	err = tegra_sor_power_down(sor);
1974459cc2c6SThierry Reding 	if (err < 0)
1975459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to power down SOR: %d\n", err);
1976459cc2c6SThierry Reding 
1977459cc2c6SThierry Reding 	err = tegra_io_rail_power_off(TEGRA_IO_RAIL_HDMI);
1978459cc2c6SThierry Reding 	if (err < 0)
1979459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to power off HDMI rail: %d\n", err);
1980459cc2c6SThierry Reding 
1981aaff8bd2SThierry Reding 	pm_runtime_put(sor->dev);
1982459cc2c6SThierry Reding }
1983459cc2c6SThierry Reding 
1984459cc2c6SThierry Reding static void tegra_sor_hdmi_enable(struct drm_encoder *encoder)
1985459cc2c6SThierry Reding {
1986459cc2c6SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
1987459cc2c6SThierry Reding 	unsigned int h_ref_to_sync = 1, pulse_start, max_ac;
1988459cc2c6SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
1989459cc2c6SThierry Reding 	struct tegra_sor_hdmi_settings *settings;
1990459cc2c6SThierry Reding 	struct tegra_sor *sor = to_sor(output);
1991c31efa7aSThierry Reding 	struct tegra_sor_state *state;
1992459cc2c6SThierry Reding 	struct drm_display_mode *mode;
1993*30b49435SThierry Reding 	unsigned int div, i;
1994459cc2c6SThierry Reding 	u32 value;
1995459cc2c6SThierry Reding 	int err;
1996459cc2c6SThierry Reding 
1997c31efa7aSThierry Reding 	state = to_sor_state(output->connector.state);
1998459cc2c6SThierry Reding 	mode = &encoder->crtc->state->adjusted_mode;
1999459cc2c6SThierry Reding 
2000aaff8bd2SThierry Reding 	pm_runtime_get_sync(sor->dev);
2001459cc2c6SThierry Reding 
200225bb2cecSThierry Reding 	/* switch to safe parent clock */
200325bb2cecSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
2004459cc2c6SThierry Reding 	if (err < 0)
2005459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
2006459cc2c6SThierry Reding 
2007459cc2c6SThierry Reding 	div = clk_get_rate(sor->clk) / 1000000 * 4;
2008459cc2c6SThierry Reding 
2009459cc2c6SThierry Reding 	err = tegra_io_rail_power_on(TEGRA_IO_RAIL_HDMI);
2010459cc2c6SThierry Reding 	if (err < 0)
2011459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to power on HDMI rail: %d\n", err);
2012459cc2c6SThierry Reding 
2013459cc2c6SThierry Reding 	usleep_range(20, 100);
2014459cc2c6SThierry Reding 
2015459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
2016459cc2c6SThierry Reding 	value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
2017459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
2018459cc2c6SThierry Reding 
2019459cc2c6SThierry Reding 	usleep_range(20, 100);
2020459cc2c6SThierry Reding 
2021459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL3);
2022459cc2c6SThierry Reding 	value &= ~SOR_PLL3_PLL_VDD_MODE_3V3;
2023459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL3);
2024459cc2c6SThierry Reding 
2025459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL0);
2026459cc2c6SThierry Reding 	value &= ~SOR_PLL0_VCOPD;
2027459cc2c6SThierry Reding 	value &= ~SOR_PLL0_PWR;
2028459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL0);
2029459cc2c6SThierry Reding 
2030459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
2031459cc2c6SThierry Reding 	value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
2032459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
2033459cc2c6SThierry Reding 
2034459cc2c6SThierry Reding 	usleep_range(200, 400);
2035459cc2c6SThierry Reding 
2036459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
2037459cc2c6SThierry Reding 	value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
2038459cc2c6SThierry Reding 	value &= ~SOR_PLL2_PORT_POWERDOWN;
2039459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
2040459cc2c6SThierry Reding 
2041459cc2c6SThierry Reding 	usleep_range(20, 100);
2042459cc2c6SThierry Reding 
2043459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
2044459cc2c6SThierry Reding 	value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
2045459cc2c6SThierry Reding 		 SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2;
2046459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
2047459cc2c6SThierry Reding 
2048459cc2c6SThierry Reding 	while (true) {
2049459cc2c6SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
2050459cc2c6SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_STATE_BUSY) == 0)
2051459cc2c6SThierry Reding 			break;
2052459cc2c6SThierry Reding 
2053459cc2c6SThierry Reding 		usleep_range(250, 1000);
2054459cc2c6SThierry Reding 	}
2055459cc2c6SThierry Reding 
2056459cc2c6SThierry Reding 	value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
2057459cc2c6SThierry Reding 		SOR_LANE_SEQ_CTL_POWER_STATE_UP | SOR_LANE_SEQ_CTL_DELAY(5);
2058459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
2059459cc2c6SThierry Reding 
2060459cc2c6SThierry Reding 	while (true) {
2061459cc2c6SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
2062459cc2c6SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
2063459cc2c6SThierry Reding 			break;
2064459cc2c6SThierry Reding 
2065459cc2c6SThierry Reding 		usleep_range(250, 1000);
2066459cc2c6SThierry Reding 	}
2067459cc2c6SThierry Reding 
2068459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
2069459cc2c6SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
2070459cc2c6SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
2071459cc2c6SThierry Reding 
2072459cc2c6SThierry Reding 	if (mode->clock < 340000)
2073459cc2c6SThierry Reding 		value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G2_70;
2074459cc2c6SThierry Reding 	else
2075459cc2c6SThierry Reding 		value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G5_40;
2076459cc2c6SThierry Reding 
2077459cc2c6SThierry Reding 	value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK;
2078459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
2079459cc2c6SThierry Reding 
2080459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_SPARE0);
2081459cc2c6SThierry Reding 	value |= SOR_DP_SPARE_DISP_VIDEO_PREAMBLE;
2082459cc2c6SThierry Reding 	value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
2083459cc2c6SThierry Reding 	value |= SOR_DP_SPARE_SEQ_ENABLE;
2084459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_SPARE0);
2085459cc2c6SThierry Reding 
2086459cc2c6SThierry Reding 	value = SOR_SEQ_CTL_PU_PC(0) | SOR_SEQ_CTL_PU_PC_ALT(0) |
2087459cc2c6SThierry Reding 		SOR_SEQ_CTL_PD_PC(8) | SOR_SEQ_CTL_PD_PC_ALT(8);
2088459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_SEQ_CTL);
2089459cc2c6SThierry Reding 
2090459cc2c6SThierry Reding 	value = SOR_SEQ_INST_DRIVE_PWM_OUT_LO | SOR_SEQ_INST_HALT |
2091459cc2c6SThierry Reding 		SOR_SEQ_INST_WAIT_VSYNC | SOR_SEQ_INST_WAIT(1);
2092459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_SEQ_INST(0));
2093459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_SEQ_INST(8));
2094459cc2c6SThierry Reding 
2095459cc2c6SThierry Reding 	/* program the reference clock */
2096459cc2c6SThierry Reding 	value = SOR_REFCLK_DIV_INT(div) | SOR_REFCLK_DIV_FRAC(div);
2097459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_REFCLK);
2098459cc2c6SThierry Reding 
2099*30b49435SThierry Reding 	/* XXX not in TRM */
2100*30b49435SThierry Reding 	for (value = 0, i = 0; i < 5; i++)
2101*30b49435SThierry Reding 		value |= SOR_XBAR_CTRL_LINK0_XSEL(i, sor->soc->xbar_cfg[i]) |
2102*30b49435SThierry Reding 			 SOR_XBAR_CTRL_LINK1_XSEL(i, i);
2103459cc2c6SThierry Reding 
2104459cc2c6SThierry Reding 	tegra_sor_writel(sor, 0x00000000, SOR_XBAR_POL);
2105*30b49435SThierry Reding 	tegra_sor_writel(sor, value, SOR_XBAR_CTRL);
2106459cc2c6SThierry Reding 
210725bb2cecSThierry Reding 	/* switch to parent clock */
2108618dee39SThierry Reding 	err = clk_set_parent(sor->clk_src, sor->clk_parent);
2109618dee39SThierry Reding 	if (err < 0)
2110618dee39SThierry Reding 		dev_err(sor->dev, "failed to set source clock: %d\n", err);
2111618dee39SThierry Reding 
2112618dee39SThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_src);
2113459cc2c6SThierry Reding 	if (err < 0)
2114459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to set parent clock: %d\n", err);
2115459cc2c6SThierry Reding 
2116459cc2c6SThierry Reding 	value = SOR_INPUT_CONTROL_HDMI_SRC_SELECT(dc->pipe);
2117459cc2c6SThierry Reding 
2118459cc2c6SThierry Reding 	/* XXX is this the proper check? */
2119459cc2c6SThierry Reding 	if (mode->clock < 75000)
2120459cc2c6SThierry Reding 		value |= SOR_INPUT_CONTROL_ARM_VIDEO_RANGE_LIMITED;
2121459cc2c6SThierry Reding 
2122459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_INPUT_CONTROL);
2123459cc2c6SThierry Reding 
2124459cc2c6SThierry Reding 	max_ac = ((mode->htotal - mode->hdisplay) - SOR_REKEY - 18) / 32;
2125459cc2c6SThierry Reding 
2126459cc2c6SThierry Reding 	value = SOR_HDMI_CTRL_ENABLE | SOR_HDMI_CTRL_MAX_AC_PACKET(max_ac) |
2127459cc2c6SThierry Reding 		SOR_HDMI_CTRL_AUDIO_LAYOUT | SOR_HDMI_CTRL_REKEY(SOR_REKEY);
2128459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_CTRL);
2129459cc2c6SThierry Reding 
2130459cc2c6SThierry Reding 	/* H_PULSE2 setup */
2131459cc2c6SThierry Reding 	pulse_start = h_ref_to_sync + (mode->hsync_end - mode->hsync_start) +
2132459cc2c6SThierry Reding 		      (mode->htotal - mode->hsync_end) - 10;
2133459cc2c6SThierry Reding 
2134459cc2c6SThierry Reding 	value = PULSE_LAST_END_A | PULSE_QUAL_VACTIVE |
2135459cc2c6SThierry Reding 		PULSE_POLARITY_HIGH | PULSE_MODE_NORMAL;
2136459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_CONTROL);
2137459cc2c6SThierry Reding 
2138459cc2c6SThierry Reding 	value = PULSE_END(pulse_start + 8) | PULSE_START(pulse_start);
2139459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_POSITION_A);
2140459cc2c6SThierry Reding 
2141459cc2c6SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_SIGNAL_OPTIONS0);
2142459cc2c6SThierry Reding 	value |= H_PULSE2_ENABLE;
2143459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_SIGNAL_OPTIONS0);
2144459cc2c6SThierry Reding 
2145459cc2c6SThierry Reding 	/* infoframe setup */
2146459cc2c6SThierry Reding 	err = tegra_sor_hdmi_setup_avi_infoframe(sor, mode);
2147459cc2c6SThierry Reding 	if (err < 0)
2148459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to setup AVI infoframe: %d\n", err);
2149459cc2c6SThierry Reding 
2150459cc2c6SThierry Reding 	/* XXX HDMI audio support not implemented yet */
2151459cc2c6SThierry Reding 	tegra_sor_hdmi_disable_audio_infoframe(sor);
2152459cc2c6SThierry Reding 
2153459cc2c6SThierry Reding 	/* use single TMDS protocol */
2154459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
2155459cc2c6SThierry Reding 	value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
2156459cc2c6SThierry Reding 	value |= SOR_STATE_ASY_PROTOCOL_SINGLE_TMDS_A;
2157459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
2158459cc2c6SThierry Reding 
2159459cc2c6SThierry Reding 	/* power up pad calibration */
2160459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
2161459cc2c6SThierry Reding 	value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
2162459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
2163459cc2c6SThierry Reding 
2164459cc2c6SThierry Reding 	/* production settings */
2165459cc2c6SThierry Reding 	settings = tegra_sor_hdmi_find_settings(sor, mode->clock * 1000);
2166db8b42fbSDan Carpenter 	if (!settings) {
2167db8b42fbSDan Carpenter 		dev_err(sor->dev, "no settings for pixel clock %d Hz\n",
2168db8b42fbSDan Carpenter 			mode->clock * 1000);
2169459cc2c6SThierry Reding 		return;
2170459cc2c6SThierry Reding 	}
2171459cc2c6SThierry Reding 
2172459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL0);
2173459cc2c6SThierry Reding 	value &= ~SOR_PLL0_ICHPMP_MASK;
2174459cc2c6SThierry Reding 	value &= ~SOR_PLL0_VCOCAP_MASK;
2175459cc2c6SThierry Reding 	value |= SOR_PLL0_ICHPMP(settings->ichpmp);
2176459cc2c6SThierry Reding 	value |= SOR_PLL0_VCOCAP(settings->vcocap);
2177459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL0);
2178459cc2c6SThierry Reding 
2179459cc2c6SThierry Reding 	tegra_sor_dp_term_calibrate(sor);
2180459cc2c6SThierry Reding 
2181459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL1);
2182459cc2c6SThierry Reding 	value &= ~SOR_PLL1_LOADADJ_MASK;
2183459cc2c6SThierry Reding 	value |= SOR_PLL1_LOADADJ(settings->loadadj);
2184459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL1);
2185459cc2c6SThierry Reding 
2186459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL3);
2187459cc2c6SThierry Reding 	value &= ~SOR_PLL3_BG_VREF_LEVEL_MASK;
2188459cc2c6SThierry Reding 	value |= SOR_PLL3_BG_VREF_LEVEL(settings->bg_vref);
2189459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL3);
2190459cc2c6SThierry Reding 
2191459cc2c6SThierry Reding 	value = settings->drive_current[0] << 24 |
2192459cc2c6SThierry Reding 		settings->drive_current[1] << 16 |
2193459cc2c6SThierry Reding 		settings->drive_current[2] <<  8 |
2194459cc2c6SThierry Reding 		settings->drive_current[3] <<  0;
2195459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT0);
2196459cc2c6SThierry Reding 
2197459cc2c6SThierry Reding 	value = settings->preemphasis[0] << 24 |
2198459cc2c6SThierry Reding 		settings->preemphasis[1] << 16 |
2199459cc2c6SThierry Reding 		settings->preemphasis[2] <<  8 |
2200459cc2c6SThierry Reding 		settings->preemphasis[3] <<  0;
2201459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS0);
2202459cc2c6SThierry Reding 
2203459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
2204459cc2c6SThierry Reding 	value &= ~SOR_DP_PADCTL_TX_PU_MASK;
2205459cc2c6SThierry Reding 	value |= SOR_DP_PADCTL_TX_PU_ENABLE;
2206459cc2c6SThierry Reding 	value |= SOR_DP_PADCTL_TX_PU(settings->tx_pu);
2207459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
2208459cc2c6SThierry Reding 
2209459cc2c6SThierry Reding 	/* power down pad calibration */
2210459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
2211459cc2c6SThierry Reding 	value |= SOR_DP_PADCTL_PAD_CAL_PD;
2212459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
2213459cc2c6SThierry Reding 
2214459cc2c6SThierry Reding 	/* miscellaneous display controller settings */
2215459cc2c6SThierry Reding 	value = VSYNC_H_POSITION(1);
2216459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_TIMING_OPTIONS);
2217459cc2c6SThierry Reding 
2218459cc2c6SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_COLOR_CONTROL);
2219459cc2c6SThierry Reding 	value &= ~DITHER_CONTROL_MASK;
2220459cc2c6SThierry Reding 	value &= ~BASE_COLOR_SIZE_MASK;
2221459cc2c6SThierry Reding 
2222c31efa7aSThierry Reding 	switch (state->bpc) {
2223459cc2c6SThierry Reding 	case 6:
2224459cc2c6SThierry Reding 		value |= BASE_COLOR_SIZE_666;
2225459cc2c6SThierry Reding 		break;
2226459cc2c6SThierry Reding 
2227459cc2c6SThierry Reding 	case 8:
2228459cc2c6SThierry Reding 		value |= BASE_COLOR_SIZE_888;
2229459cc2c6SThierry Reding 		break;
2230459cc2c6SThierry Reding 
2231459cc2c6SThierry Reding 	default:
2232c31efa7aSThierry Reding 		WARN(1, "%u bits-per-color not supported\n", state->bpc);
2233c31efa7aSThierry Reding 		value |= BASE_COLOR_SIZE_888;
2234459cc2c6SThierry Reding 		break;
2235459cc2c6SThierry Reding 	}
2236459cc2c6SThierry Reding 
2237459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_COLOR_CONTROL);
2238459cc2c6SThierry Reding 
2239459cc2c6SThierry Reding 	err = tegra_sor_power_up(sor, 250);
2240459cc2c6SThierry Reding 	if (err < 0)
2241459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to power up SOR: %d\n", err);
2242459cc2c6SThierry Reding 
22432bd1dd39SThierry Reding 	/* configure dynamic range of output */
2244459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_HEAD_STATE0(dc->pipe));
2245459cc2c6SThierry Reding 	value &= ~SOR_HEAD_STATE_RANGECOMPRESS_MASK;
2246459cc2c6SThierry Reding 	value &= ~SOR_HEAD_STATE_DYNRANGE_MASK;
2247459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HEAD_STATE0(dc->pipe));
2248459cc2c6SThierry Reding 
22492bd1dd39SThierry Reding 	/* configure colorspace */
2250459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_HEAD_STATE0(dc->pipe));
2251459cc2c6SThierry Reding 	value &= ~SOR_HEAD_STATE_COLORSPACE_MASK;
2252459cc2c6SThierry Reding 	value |= SOR_HEAD_STATE_COLORSPACE_RGB;
2253459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HEAD_STATE0(dc->pipe));
2254459cc2c6SThierry Reding 
2255c31efa7aSThierry Reding 	tegra_sor_mode_set(sor, mode, state);
2256459cc2c6SThierry Reding 
2257459cc2c6SThierry Reding 	tegra_sor_update(sor);
2258459cc2c6SThierry Reding 
2259459cc2c6SThierry Reding 	err = tegra_sor_attach(sor);
2260459cc2c6SThierry Reding 	if (err < 0)
2261459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to attach SOR: %d\n", err);
2262459cc2c6SThierry Reding 
2263459cc2c6SThierry Reding 	/* enable display to SOR clock and generate HDMI preamble */
2264459cc2c6SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
2265459cc2c6SThierry Reding 	value |= SOR1_ENABLE | SOR1_TIMING_CYA;
2266459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
2267459cc2c6SThierry Reding 
2268459cc2c6SThierry Reding 	tegra_dc_commit(dc);
2269459cc2c6SThierry Reding 
2270459cc2c6SThierry Reding 	err = tegra_sor_wakeup(sor);
2271459cc2c6SThierry Reding 	if (err < 0)
2272459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to wakeup SOR: %d\n", err);
2273459cc2c6SThierry Reding }
2274459cc2c6SThierry Reding 
2275459cc2c6SThierry Reding static const struct drm_encoder_helper_funcs tegra_sor_hdmi_helpers = {
2276459cc2c6SThierry Reding 	.disable = tegra_sor_hdmi_disable,
2277459cc2c6SThierry Reding 	.enable = tegra_sor_hdmi_enable,
2278459cc2c6SThierry Reding 	.atomic_check = tegra_sor_encoder_atomic_check,
2279459cc2c6SThierry Reding };
2280459cc2c6SThierry Reding 
22816b6b6042SThierry Reding static int tegra_sor_init(struct host1x_client *client)
22826b6b6042SThierry Reding {
22839910f5c4SThierry Reding 	struct drm_device *drm = dev_get_drvdata(client->parent);
2284459cc2c6SThierry Reding 	const struct drm_encoder_helper_funcs *helpers = NULL;
22856b6b6042SThierry Reding 	struct tegra_sor *sor = host1x_client_to_sor(client);
2286459cc2c6SThierry Reding 	int connector = DRM_MODE_CONNECTOR_Unknown;
2287459cc2c6SThierry Reding 	int encoder = DRM_MODE_ENCODER_NONE;
22886b6b6042SThierry Reding 	int err;
22896b6b6042SThierry Reding 
22909542c237SThierry Reding 	if (!sor->aux) {
2291459cc2c6SThierry Reding 		if (sor->soc->supports_hdmi) {
2292459cc2c6SThierry Reding 			connector = DRM_MODE_CONNECTOR_HDMIA;
2293459cc2c6SThierry Reding 			encoder = DRM_MODE_ENCODER_TMDS;
2294459cc2c6SThierry Reding 			helpers = &tegra_sor_hdmi_helpers;
2295459cc2c6SThierry Reding 		} else if (sor->soc->supports_lvds) {
2296459cc2c6SThierry Reding 			connector = DRM_MODE_CONNECTOR_LVDS;
2297459cc2c6SThierry Reding 			encoder = DRM_MODE_ENCODER_LVDS;
2298459cc2c6SThierry Reding 		}
2299459cc2c6SThierry Reding 	} else {
2300459cc2c6SThierry Reding 		if (sor->soc->supports_edp) {
2301459cc2c6SThierry Reding 			connector = DRM_MODE_CONNECTOR_eDP;
2302459cc2c6SThierry Reding 			encoder = DRM_MODE_ENCODER_TMDS;
2303459cc2c6SThierry Reding 			helpers = &tegra_sor_edp_helpers;
2304459cc2c6SThierry Reding 		} else if (sor->soc->supports_dp) {
2305459cc2c6SThierry Reding 			connector = DRM_MODE_CONNECTOR_DisplayPort;
2306459cc2c6SThierry Reding 			encoder = DRM_MODE_ENCODER_TMDS;
2307459cc2c6SThierry Reding 		}
2308459cc2c6SThierry Reding 	}
23096b6b6042SThierry Reding 
23106b6b6042SThierry Reding 	sor->output.dev = sor->dev;
23116b6b6042SThierry Reding 
23126fad8f66SThierry Reding 	drm_connector_init(drm, &sor->output.connector,
23136fad8f66SThierry Reding 			   &tegra_sor_connector_funcs,
2314459cc2c6SThierry Reding 			   connector);
23156fad8f66SThierry Reding 	drm_connector_helper_add(&sor->output.connector,
23166fad8f66SThierry Reding 				 &tegra_sor_connector_helper_funcs);
23176fad8f66SThierry Reding 	sor->output.connector.dpms = DRM_MODE_DPMS_OFF;
23186fad8f66SThierry Reding 
23196fad8f66SThierry Reding 	drm_encoder_init(drm, &sor->output.encoder, &tegra_sor_encoder_funcs,
232013a3d91fSVille Syrjälä 			 encoder, NULL);
2321459cc2c6SThierry Reding 	drm_encoder_helper_add(&sor->output.encoder, helpers);
23226fad8f66SThierry Reding 
23236fad8f66SThierry Reding 	drm_mode_connector_attach_encoder(&sor->output.connector,
23246fad8f66SThierry Reding 					  &sor->output.encoder);
23256fad8f66SThierry Reding 	drm_connector_register(&sor->output.connector);
23266fad8f66SThierry Reding 
2327ea130b24SThierry Reding 	err = tegra_output_init(drm, &sor->output);
2328ea130b24SThierry Reding 	if (err < 0) {
2329ea130b24SThierry Reding 		dev_err(client->dev, "failed to initialize output: %d\n", err);
2330ea130b24SThierry Reding 		return err;
2331ea130b24SThierry Reding 	}
23326fad8f66SThierry Reding 
2333ea130b24SThierry Reding 	sor->output.encoder.possible_crtcs = 0x3;
23346b6b6042SThierry Reding 
2335a82752e1SThierry Reding 	if (IS_ENABLED(CONFIG_DEBUG_FS)) {
23361b0c7b48SThierry Reding 		err = tegra_sor_debugfs_init(sor, drm->primary);
2337a82752e1SThierry Reding 		if (err < 0)
2338a82752e1SThierry Reding 			dev_err(sor->dev, "debugfs setup failed: %d\n", err);
2339a82752e1SThierry Reding 	}
2340a82752e1SThierry Reding 
23419542c237SThierry Reding 	if (sor->aux) {
23429542c237SThierry Reding 		err = drm_dp_aux_attach(sor->aux, &sor->output);
23436b6b6042SThierry Reding 		if (err < 0) {
23446b6b6042SThierry Reding 			dev_err(sor->dev, "failed to attach DP: %d\n", err);
23456b6b6042SThierry Reding 			return err;
23466b6b6042SThierry Reding 		}
23476b6b6042SThierry Reding 	}
23486b6b6042SThierry Reding 
2349535a65dbSTomeu Vizoso 	/*
2350535a65dbSTomeu Vizoso 	 * XXX: Remove this reset once proper hand-over from firmware to
2351535a65dbSTomeu Vizoso 	 * kernel is possible.
2352535a65dbSTomeu Vizoso 	 */
2353535a65dbSTomeu Vizoso 	err = reset_control_assert(sor->rst);
2354535a65dbSTomeu Vizoso 	if (err < 0) {
2355535a65dbSTomeu Vizoso 		dev_err(sor->dev, "failed to assert SOR reset: %d\n", err);
2356535a65dbSTomeu Vizoso 		return err;
2357535a65dbSTomeu Vizoso 	}
2358535a65dbSTomeu Vizoso 
23596fad8f66SThierry Reding 	err = clk_prepare_enable(sor->clk);
23606fad8f66SThierry Reding 	if (err < 0) {
23616fad8f66SThierry Reding 		dev_err(sor->dev, "failed to enable clock: %d\n", err);
23626fad8f66SThierry Reding 		return err;
23636fad8f66SThierry Reding 	}
23646fad8f66SThierry Reding 
2365535a65dbSTomeu Vizoso 	usleep_range(1000, 3000);
2366535a65dbSTomeu Vizoso 
2367535a65dbSTomeu Vizoso 	err = reset_control_deassert(sor->rst);
2368535a65dbSTomeu Vizoso 	if (err < 0) {
2369535a65dbSTomeu Vizoso 		dev_err(sor->dev, "failed to deassert SOR reset: %d\n", err);
2370535a65dbSTomeu Vizoso 		return err;
2371535a65dbSTomeu Vizoso 	}
2372535a65dbSTomeu Vizoso 
23736fad8f66SThierry Reding 	err = clk_prepare_enable(sor->clk_safe);
23746fad8f66SThierry Reding 	if (err < 0)
23756fad8f66SThierry Reding 		return err;
23766fad8f66SThierry Reding 
23776fad8f66SThierry Reding 	err = clk_prepare_enable(sor->clk_dp);
23786fad8f66SThierry Reding 	if (err < 0)
23796fad8f66SThierry Reding 		return err;
23806fad8f66SThierry Reding 
23816b6b6042SThierry Reding 	return 0;
23826b6b6042SThierry Reding }
23836b6b6042SThierry Reding 
23846b6b6042SThierry Reding static int tegra_sor_exit(struct host1x_client *client)
23856b6b6042SThierry Reding {
23866b6b6042SThierry Reding 	struct tegra_sor *sor = host1x_client_to_sor(client);
23876b6b6042SThierry Reding 	int err;
23886b6b6042SThierry Reding 
2389328ec69eSThierry Reding 	tegra_output_exit(&sor->output);
2390328ec69eSThierry Reding 
23919542c237SThierry Reding 	if (sor->aux) {
23929542c237SThierry Reding 		err = drm_dp_aux_detach(sor->aux);
23936b6b6042SThierry Reding 		if (err < 0) {
23946b6b6042SThierry Reding 			dev_err(sor->dev, "failed to detach DP: %d\n", err);
23956b6b6042SThierry Reding 			return err;
23966b6b6042SThierry Reding 		}
23976b6b6042SThierry Reding 	}
23986b6b6042SThierry Reding 
23996fad8f66SThierry Reding 	clk_disable_unprepare(sor->clk_safe);
24006fad8f66SThierry Reding 	clk_disable_unprepare(sor->clk_dp);
24016fad8f66SThierry Reding 	clk_disable_unprepare(sor->clk);
24026fad8f66SThierry Reding 
24034009c224SThierry Reding 	if (IS_ENABLED(CONFIG_DEBUG_FS))
24044009c224SThierry Reding 		tegra_sor_debugfs_exit(sor);
2405a82752e1SThierry Reding 
24066b6b6042SThierry Reding 	return 0;
24076b6b6042SThierry Reding }
24086b6b6042SThierry Reding 
24096b6b6042SThierry Reding static const struct host1x_client_ops sor_client_ops = {
24106b6b6042SThierry Reding 	.init = tegra_sor_init,
24116b6b6042SThierry Reding 	.exit = tegra_sor_exit,
24126b6b6042SThierry Reding };
24136b6b6042SThierry Reding 
2414459cc2c6SThierry Reding static const struct tegra_sor_ops tegra_sor_edp_ops = {
2415459cc2c6SThierry Reding 	.name = "eDP",
2416459cc2c6SThierry Reding };
2417459cc2c6SThierry Reding 
2418459cc2c6SThierry Reding static int tegra_sor_hdmi_probe(struct tegra_sor *sor)
2419459cc2c6SThierry Reding {
2420459cc2c6SThierry Reding 	int err;
2421459cc2c6SThierry Reding 
2422459cc2c6SThierry Reding 	sor->avdd_io_supply = devm_regulator_get(sor->dev, "avdd-io");
2423459cc2c6SThierry Reding 	if (IS_ERR(sor->avdd_io_supply)) {
2424459cc2c6SThierry Reding 		dev_err(sor->dev, "cannot get AVDD I/O supply: %ld\n",
2425459cc2c6SThierry Reding 			PTR_ERR(sor->avdd_io_supply));
2426459cc2c6SThierry Reding 		return PTR_ERR(sor->avdd_io_supply);
2427459cc2c6SThierry Reding 	}
2428459cc2c6SThierry Reding 
2429459cc2c6SThierry Reding 	err = regulator_enable(sor->avdd_io_supply);
2430459cc2c6SThierry Reding 	if (err < 0) {
2431459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to enable AVDD I/O supply: %d\n",
2432459cc2c6SThierry Reding 			err);
2433459cc2c6SThierry Reding 		return err;
2434459cc2c6SThierry Reding 	}
2435459cc2c6SThierry Reding 
2436459cc2c6SThierry Reding 	sor->vdd_pll_supply = devm_regulator_get(sor->dev, "vdd-pll");
2437459cc2c6SThierry Reding 	if (IS_ERR(sor->vdd_pll_supply)) {
2438459cc2c6SThierry Reding 		dev_err(sor->dev, "cannot get VDD PLL supply: %ld\n",
2439459cc2c6SThierry Reding 			PTR_ERR(sor->vdd_pll_supply));
2440459cc2c6SThierry Reding 		return PTR_ERR(sor->vdd_pll_supply);
2441459cc2c6SThierry Reding 	}
2442459cc2c6SThierry Reding 
2443459cc2c6SThierry Reding 	err = regulator_enable(sor->vdd_pll_supply);
2444459cc2c6SThierry Reding 	if (err < 0) {
2445459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to enable VDD PLL supply: %d\n",
2446459cc2c6SThierry Reding 			err);
2447459cc2c6SThierry Reding 		return err;
2448459cc2c6SThierry Reding 	}
2449459cc2c6SThierry Reding 
2450459cc2c6SThierry Reding 	sor->hdmi_supply = devm_regulator_get(sor->dev, "hdmi");
2451459cc2c6SThierry Reding 	if (IS_ERR(sor->hdmi_supply)) {
2452459cc2c6SThierry Reding 		dev_err(sor->dev, "cannot get HDMI supply: %ld\n",
2453459cc2c6SThierry Reding 			PTR_ERR(sor->hdmi_supply));
2454459cc2c6SThierry Reding 		return PTR_ERR(sor->hdmi_supply);
2455459cc2c6SThierry Reding 	}
2456459cc2c6SThierry Reding 
2457459cc2c6SThierry Reding 	err = regulator_enable(sor->hdmi_supply);
2458459cc2c6SThierry Reding 	if (err < 0) {
2459459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to enable HDMI supply: %d\n", err);
2460459cc2c6SThierry Reding 		return err;
2461459cc2c6SThierry Reding 	}
2462459cc2c6SThierry Reding 
2463459cc2c6SThierry Reding 	return 0;
2464459cc2c6SThierry Reding }
2465459cc2c6SThierry Reding 
2466459cc2c6SThierry Reding static int tegra_sor_hdmi_remove(struct tegra_sor *sor)
2467459cc2c6SThierry Reding {
2468459cc2c6SThierry Reding 	regulator_disable(sor->hdmi_supply);
2469459cc2c6SThierry Reding 	regulator_disable(sor->vdd_pll_supply);
2470459cc2c6SThierry Reding 	regulator_disable(sor->avdd_io_supply);
2471459cc2c6SThierry Reding 
2472459cc2c6SThierry Reding 	return 0;
2473459cc2c6SThierry Reding }
2474459cc2c6SThierry Reding 
2475459cc2c6SThierry Reding static const struct tegra_sor_ops tegra_sor_hdmi_ops = {
2476459cc2c6SThierry Reding 	.name = "HDMI",
2477459cc2c6SThierry Reding 	.probe = tegra_sor_hdmi_probe,
2478459cc2c6SThierry Reding 	.remove = tegra_sor_hdmi_remove,
2479459cc2c6SThierry Reding };
2480459cc2c6SThierry Reding 
2481*30b49435SThierry Reding static const u8 tegra124_sor_xbar_cfg[5] = {
2482*30b49435SThierry Reding 	0, 1, 2, 3, 4
2483*30b49435SThierry Reding };
2484*30b49435SThierry Reding 
2485459cc2c6SThierry Reding static const struct tegra_sor_soc tegra124_sor = {
2486459cc2c6SThierry Reding 	.supports_edp = true,
2487459cc2c6SThierry Reding 	.supports_lvds = true,
2488459cc2c6SThierry Reding 	.supports_hdmi = false,
2489459cc2c6SThierry Reding 	.supports_dp = false,
2490*30b49435SThierry Reding 	.xbar_cfg = tegra124_sor_xbar_cfg,
2491459cc2c6SThierry Reding };
2492459cc2c6SThierry Reding 
2493459cc2c6SThierry Reding static const struct tegra_sor_soc tegra210_sor = {
2494459cc2c6SThierry Reding 	.supports_edp = true,
2495459cc2c6SThierry Reding 	.supports_lvds = false,
2496459cc2c6SThierry Reding 	.supports_hdmi = false,
2497459cc2c6SThierry Reding 	.supports_dp = false,
2498*30b49435SThierry Reding 	.xbar_cfg = tegra124_sor_xbar_cfg,
2499*30b49435SThierry Reding };
2500*30b49435SThierry Reding 
2501*30b49435SThierry Reding static const u8 tegra210_sor_xbar_cfg[5] = {
2502*30b49435SThierry Reding 	2, 1, 0, 3, 4
2503459cc2c6SThierry Reding };
2504459cc2c6SThierry Reding 
2505459cc2c6SThierry Reding static const struct tegra_sor_soc tegra210_sor1 = {
2506459cc2c6SThierry Reding 	.supports_edp = false,
2507459cc2c6SThierry Reding 	.supports_lvds = false,
2508459cc2c6SThierry Reding 	.supports_hdmi = true,
2509459cc2c6SThierry Reding 	.supports_dp = true,
2510459cc2c6SThierry Reding 
2511459cc2c6SThierry Reding 	.num_settings = ARRAY_SIZE(tegra210_sor_hdmi_defaults),
2512459cc2c6SThierry Reding 	.settings = tegra210_sor_hdmi_defaults,
2513*30b49435SThierry Reding 
2514*30b49435SThierry Reding 	.xbar_cfg = tegra210_sor_xbar_cfg,
2515459cc2c6SThierry Reding };
2516459cc2c6SThierry Reding 
2517459cc2c6SThierry Reding static const struct of_device_id tegra_sor_of_match[] = {
2518459cc2c6SThierry Reding 	{ .compatible = "nvidia,tegra210-sor1", .data = &tegra210_sor1 },
2519459cc2c6SThierry Reding 	{ .compatible = "nvidia,tegra210-sor", .data = &tegra210_sor },
2520459cc2c6SThierry Reding 	{ .compatible = "nvidia,tegra124-sor", .data = &tegra124_sor },
2521459cc2c6SThierry Reding 	{ },
2522459cc2c6SThierry Reding };
2523459cc2c6SThierry Reding MODULE_DEVICE_TABLE(of, tegra_sor_of_match);
2524459cc2c6SThierry Reding 
25256b6b6042SThierry Reding static int tegra_sor_probe(struct platform_device *pdev)
25266b6b6042SThierry Reding {
2527459cc2c6SThierry Reding 	const struct of_device_id *match;
25286b6b6042SThierry Reding 	struct device_node *np;
25296b6b6042SThierry Reding 	struct tegra_sor *sor;
25306b6b6042SThierry Reding 	struct resource *regs;
25316b6b6042SThierry Reding 	int err;
25326b6b6042SThierry Reding 
2533459cc2c6SThierry Reding 	match = of_match_device(tegra_sor_of_match, &pdev->dev);
2534459cc2c6SThierry Reding 
25356b6b6042SThierry Reding 	sor = devm_kzalloc(&pdev->dev, sizeof(*sor), GFP_KERNEL);
25366b6b6042SThierry Reding 	if (!sor)
25376b6b6042SThierry Reding 		return -ENOMEM;
25386b6b6042SThierry Reding 
25396b6b6042SThierry Reding 	sor->output.dev = sor->dev = &pdev->dev;
2540459cc2c6SThierry Reding 	sor->soc = match->data;
2541459cc2c6SThierry Reding 
2542459cc2c6SThierry Reding 	sor->settings = devm_kmemdup(&pdev->dev, sor->soc->settings,
2543459cc2c6SThierry Reding 				     sor->soc->num_settings *
2544459cc2c6SThierry Reding 					sizeof(*sor->settings),
2545459cc2c6SThierry Reding 				     GFP_KERNEL);
2546459cc2c6SThierry Reding 	if (!sor->settings)
2547459cc2c6SThierry Reding 		return -ENOMEM;
2548459cc2c6SThierry Reding 
2549459cc2c6SThierry Reding 	sor->num_settings = sor->soc->num_settings;
25506b6b6042SThierry Reding 
25516b6b6042SThierry Reding 	np = of_parse_phandle(pdev->dev.of_node, "nvidia,dpaux", 0);
25526b6b6042SThierry Reding 	if (np) {
25539542c237SThierry Reding 		sor->aux = drm_dp_aux_find_by_of_node(np);
25546b6b6042SThierry Reding 		of_node_put(np);
25556b6b6042SThierry Reding 
25569542c237SThierry Reding 		if (!sor->aux)
25576b6b6042SThierry Reding 			return -EPROBE_DEFER;
25586b6b6042SThierry Reding 	}
25596b6b6042SThierry Reding 
25609542c237SThierry Reding 	if (!sor->aux) {
2561459cc2c6SThierry Reding 		if (sor->soc->supports_hdmi) {
2562459cc2c6SThierry Reding 			sor->ops = &tegra_sor_hdmi_ops;
2563459cc2c6SThierry Reding 		} else if (sor->soc->supports_lvds) {
2564459cc2c6SThierry Reding 			dev_err(&pdev->dev, "LVDS not supported yet\n");
2565459cc2c6SThierry Reding 			return -ENODEV;
2566459cc2c6SThierry Reding 		} else {
2567459cc2c6SThierry Reding 			dev_err(&pdev->dev, "unknown (non-DP) support\n");
2568459cc2c6SThierry Reding 			return -ENODEV;
2569459cc2c6SThierry Reding 		}
2570459cc2c6SThierry Reding 	} else {
2571459cc2c6SThierry Reding 		if (sor->soc->supports_edp) {
2572459cc2c6SThierry Reding 			sor->ops = &tegra_sor_edp_ops;
2573459cc2c6SThierry Reding 		} else if (sor->soc->supports_dp) {
2574459cc2c6SThierry Reding 			dev_err(&pdev->dev, "DisplayPort not supported yet\n");
2575459cc2c6SThierry Reding 			return -ENODEV;
2576459cc2c6SThierry Reding 		} else {
2577459cc2c6SThierry Reding 			dev_err(&pdev->dev, "unknown (DP) support\n");
2578459cc2c6SThierry Reding 			return -ENODEV;
2579459cc2c6SThierry Reding 		}
2580459cc2c6SThierry Reding 	}
2581459cc2c6SThierry Reding 
25826b6b6042SThierry Reding 	err = tegra_output_probe(&sor->output);
25834dbdc740SThierry Reding 	if (err < 0) {
25844dbdc740SThierry Reding 		dev_err(&pdev->dev, "failed to probe output: %d\n", err);
25856b6b6042SThierry Reding 		return err;
25864dbdc740SThierry Reding 	}
25876b6b6042SThierry Reding 
2588459cc2c6SThierry Reding 	if (sor->ops && sor->ops->probe) {
2589459cc2c6SThierry Reding 		err = sor->ops->probe(sor);
2590459cc2c6SThierry Reding 		if (err < 0) {
2591459cc2c6SThierry Reding 			dev_err(&pdev->dev, "failed to probe %s: %d\n",
2592459cc2c6SThierry Reding 				sor->ops->name, err);
2593459cc2c6SThierry Reding 			goto output;
2594459cc2c6SThierry Reding 		}
2595459cc2c6SThierry Reding 	}
2596459cc2c6SThierry Reding 
25976b6b6042SThierry Reding 	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
25986b6b6042SThierry Reding 	sor->regs = devm_ioremap_resource(&pdev->dev, regs);
2599459cc2c6SThierry Reding 	if (IS_ERR(sor->regs)) {
2600459cc2c6SThierry Reding 		err = PTR_ERR(sor->regs);
2601459cc2c6SThierry Reding 		goto remove;
2602459cc2c6SThierry Reding 	}
26036b6b6042SThierry Reding 
26046b6b6042SThierry Reding 	sor->rst = devm_reset_control_get(&pdev->dev, "sor");
26054dbdc740SThierry Reding 	if (IS_ERR(sor->rst)) {
2606459cc2c6SThierry Reding 		err = PTR_ERR(sor->rst);
2607459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get reset control: %d\n", err);
2608459cc2c6SThierry Reding 		goto remove;
26094dbdc740SThierry Reding 	}
26106b6b6042SThierry Reding 
26116b6b6042SThierry Reding 	sor->clk = devm_clk_get(&pdev->dev, NULL);
26124dbdc740SThierry Reding 	if (IS_ERR(sor->clk)) {
2613459cc2c6SThierry Reding 		err = PTR_ERR(sor->clk);
2614459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get module clock: %d\n", err);
2615459cc2c6SThierry Reding 		goto remove;
26164dbdc740SThierry Reding 	}
26176b6b6042SThierry Reding 
2618618dee39SThierry Reding 	if (sor->soc->supports_hdmi || sor->soc->supports_dp) {
2619618dee39SThierry Reding 		sor->clk_src = devm_clk_get(&pdev->dev, "source");
2620618dee39SThierry Reding 		if (IS_ERR(sor->clk_src)) {
2621618dee39SThierry Reding 			err = PTR_ERR(sor->clk_src);
2622618dee39SThierry Reding 			dev_err(sor->dev, "failed to get source clock: %d\n",
2623618dee39SThierry Reding 				err);
2624618dee39SThierry Reding 			goto remove;
2625618dee39SThierry Reding 		}
2626618dee39SThierry Reding 	}
2627618dee39SThierry Reding 
26286b6b6042SThierry Reding 	sor->clk_parent = devm_clk_get(&pdev->dev, "parent");
26294dbdc740SThierry Reding 	if (IS_ERR(sor->clk_parent)) {
2630459cc2c6SThierry Reding 		err = PTR_ERR(sor->clk_parent);
2631459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get parent clock: %d\n", err);
2632459cc2c6SThierry Reding 		goto remove;
26334dbdc740SThierry Reding 	}
26346b6b6042SThierry Reding 
26356b6b6042SThierry Reding 	sor->clk_safe = devm_clk_get(&pdev->dev, "safe");
26364dbdc740SThierry Reding 	if (IS_ERR(sor->clk_safe)) {
2637459cc2c6SThierry Reding 		err = PTR_ERR(sor->clk_safe);
2638459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get safe clock: %d\n", err);
2639459cc2c6SThierry Reding 		goto remove;
26404dbdc740SThierry Reding 	}
26416b6b6042SThierry Reding 
26426b6b6042SThierry Reding 	sor->clk_dp = devm_clk_get(&pdev->dev, "dp");
26434dbdc740SThierry Reding 	if (IS_ERR(sor->clk_dp)) {
2644459cc2c6SThierry Reding 		err = PTR_ERR(sor->clk_dp);
2645459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get DP clock: %d\n", err);
2646459cc2c6SThierry Reding 		goto remove;
26474dbdc740SThierry Reding 	}
26486b6b6042SThierry Reding 
2649aaff8bd2SThierry Reding 	platform_set_drvdata(pdev, sor);
2650aaff8bd2SThierry Reding 	pm_runtime_enable(&pdev->dev);
2651aaff8bd2SThierry Reding 
2652b299221cSThierry Reding 	pm_runtime_get_sync(&pdev->dev);
2653b299221cSThierry Reding 	sor->clk_brick = tegra_clk_sor_brick_register(sor, "sor1_brick");
2654b299221cSThierry Reding 	pm_runtime_put(&pdev->dev);
2655b299221cSThierry Reding 
2656b299221cSThierry Reding 	if (IS_ERR(sor->clk_brick)) {
2657b299221cSThierry Reding 		err = PTR_ERR(sor->clk_brick);
2658b299221cSThierry Reding 		dev_err(&pdev->dev, "failed to register SOR clock: %d\n", err);
2659b299221cSThierry Reding 		goto remove;
2660b299221cSThierry Reding 	}
2661b299221cSThierry Reding 
26626b6b6042SThierry Reding 	INIT_LIST_HEAD(&sor->client.list);
26636b6b6042SThierry Reding 	sor->client.ops = &sor_client_ops;
26646b6b6042SThierry Reding 	sor->client.dev = &pdev->dev;
26656b6b6042SThierry Reding 
26666b6b6042SThierry Reding 	err = host1x_client_register(&sor->client);
26676b6b6042SThierry Reding 	if (err < 0) {
26686b6b6042SThierry Reding 		dev_err(&pdev->dev, "failed to register host1x client: %d\n",
26696b6b6042SThierry Reding 			err);
2670459cc2c6SThierry Reding 		goto remove;
26716b6b6042SThierry Reding 	}
26726b6b6042SThierry Reding 
26736b6b6042SThierry Reding 	return 0;
2674459cc2c6SThierry Reding 
2675459cc2c6SThierry Reding remove:
2676459cc2c6SThierry Reding 	if (sor->ops && sor->ops->remove)
2677459cc2c6SThierry Reding 		sor->ops->remove(sor);
2678459cc2c6SThierry Reding output:
2679459cc2c6SThierry Reding 	tegra_output_remove(&sor->output);
2680459cc2c6SThierry Reding 	return err;
26816b6b6042SThierry Reding }
26826b6b6042SThierry Reding 
26836b6b6042SThierry Reding static int tegra_sor_remove(struct platform_device *pdev)
26846b6b6042SThierry Reding {
26856b6b6042SThierry Reding 	struct tegra_sor *sor = platform_get_drvdata(pdev);
26866b6b6042SThierry Reding 	int err;
26876b6b6042SThierry Reding 
2688aaff8bd2SThierry Reding 	pm_runtime_disable(&pdev->dev);
2689aaff8bd2SThierry Reding 
26906b6b6042SThierry Reding 	err = host1x_client_unregister(&sor->client);
26916b6b6042SThierry Reding 	if (err < 0) {
26926b6b6042SThierry Reding 		dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
26936b6b6042SThierry Reding 			err);
26946b6b6042SThierry Reding 		return err;
26956b6b6042SThierry Reding 	}
26966b6b6042SThierry Reding 
2697459cc2c6SThierry Reding 	if (sor->ops && sor->ops->remove) {
2698459cc2c6SThierry Reding 		err = sor->ops->remove(sor);
2699459cc2c6SThierry Reding 		if (err < 0)
2700459cc2c6SThierry Reding 			dev_err(&pdev->dev, "failed to remove SOR: %d\n", err);
2701459cc2c6SThierry Reding 	}
2702459cc2c6SThierry Reding 
2703328ec69eSThierry Reding 	tegra_output_remove(&sor->output);
27046b6b6042SThierry Reding 
27056b6b6042SThierry Reding 	return 0;
27066b6b6042SThierry Reding }
27076b6b6042SThierry Reding 
2708aaff8bd2SThierry Reding #ifdef CONFIG_PM
2709aaff8bd2SThierry Reding static int tegra_sor_suspend(struct device *dev)
2710aaff8bd2SThierry Reding {
2711aaff8bd2SThierry Reding 	struct tegra_sor *sor = dev_get_drvdata(dev);
2712aaff8bd2SThierry Reding 	int err;
2713aaff8bd2SThierry Reding 
2714aaff8bd2SThierry Reding 	err = reset_control_assert(sor->rst);
2715aaff8bd2SThierry Reding 	if (err < 0) {
2716aaff8bd2SThierry Reding 		dev_err(dev, "failed to assert reset: %d\n", err);
2717aaff8bd2SThierry Reding 		return err;
2718aaff8bd2SThierry Reding 	}
2719aaff8bd2SThierry Reding 
2720aaff8bd2SThierry Reding 	usleep_range(1000, 2000);
2721aaff8bd2SThierry Reding 
2722aaff8bd2SThierry Reding 	clk_disable_unprepare(sor->clk);
2723aaff8bd2SThierry Reding 
2724aaff8bd2SThierry Reding 	return 0;
2725aaff8bd2SThierry Reding }
2726aaff8bd2SThierry Reding 
2727aaff8bd2SThierry Reding static int tegra_sor_resume(struct device *dev)
2728aaff8bd2SThierry Reding {
2729aaff8bd2SThierry Reding 	struct tegra_sor *sor = dev_get_drvdata(dev);
2730aaff8bd2SThierry Reding 	int err;
2731aaff8bd2SThierry Reding 
2732aaff8bd2SThierry Reding 	err = clk_prepare_enable(sor->clk);
2733aaff8bd2SThierry Reding 	if (err < 0) {
2734aaff8bd2SThierry Reding 		dev_err(dev, "failed to enable clock: %d\n", err);
2735aaff8bd2SThierry Reding 		return err;
2736aaff8bd2SThierry Reding 	}
2737aaff8bd2SThierry Reding 
2738aaff8bd2SThierry Reding 	usleep_range(1000, 2000);
2739aaff8bd2SThierry Reding 
2740aaff8bd2SThierry Reding 	err = reset_control_deassert(sor->rst);
2741aaff8bd2SThierry Reding 	if (err < 0) {
2742aaff8bd2SThierry Reding 		dev_err(dev, "failed to deassert reset: %d\n", err);
2743aaff8bd2SThierry Reding 		clk_disable_unprepare(sor->clk);
2744aaff8bd2SThierry Reding 		return err;
2745aaff8bd2SThierry Reding 	}
2746aaff8bd2SThierry Reding 
2747aaff8bd2SThierry Reding 	return 0;
2748aaff8bd2SThierry Reding }
2749aaff8bd2SThierry Reding #endif
2750aaff8bd2SThierry Reding 
2751aaff8bd2SThierry Reding static const struct dev_pm_ops tegra_sor_pm_ops = {
2752aaff8bd2SThierry Reding 	SET_RUNTIME_PM_OPS(tegra_sor_suspend, tegra_sor_resume, NULL)
2753aaff8bd2SThierry Reding };
2754aaff8bd2SThierry Reding 
27556b6b6042SThierry Reding struct platform_driver tegra_sor_driver = {
27566b6b6042SThierry Reding 	.driver = {
27576b6b6042SThierry Reding 		.name = "tegra-sor",
27586b6b6042SThierry Reding 		.of_match_table = tegra_sor_of_match,
2759aaff8bd2SThierry Reding 		.pm = &tegra_sor_pm_ops,
27606b6b6042SThierry Reding 	},
27616b6b6042SThierry Reding 	.probe = tegra_sor_probe,
27626b6b6042SThierry Reding 	.remove = tegra_sor_remove,
27636b6b6042SThierry Reding };
2764