xref: /openbmc/linux/drivers/gpu/drm/tegra/sor.c (revision aaff8bd2e824b6256e6cc1bd4eb3714de0683996)
16b6b6042SThierry Reding /*
26b6b6042SThierry Reding  * Copyright (C) 2013 NVIDIA Corporation
36b6b6042SThierry Reding  *
46b6b6042SThierry Reding  * This program is free software; you can redistribute it and/or modify
56b6b6042SThierry Reding  * it under the terms of the GNU General Public License version 2 as
66b6b6042SThierry Reding  * published by the Free Software Foundation.
76b6b6042SThierry Reding  */
86b6b6042SThierry Reding 
96b6b6042SThierry Reding #include <linux/clk.h>
10a82752e1SThierry Reding #include <linux/debugfs.h>
116fad8f66SThierry Reding #include <linux/gpio.h>
126b6b6042SThierry Reding #include <linux/io.h>
13459cc2c6SThierry Reding #include <linux/of_device.h>
146b6b6042SThierry Reding #include <linux/platform_device.h>
15*aaff8bd2SThierry Reding #include <linux/pm_runtime.h>
16459cc2c6SThierry Reding #include <linux/regulator/consumer.h>
176b6b6042SThierry Reding #include <linux/reset.h>
18306a7f91SThierry Reding 
197232398aSThierry Reding #include <soc/tegra/pmc.h>
206b6b6042SThierry Reding 
214aa3df71SThierry Reding #include <drm/drm_atomic_helper.h>
226b6b6042SThierry Reding #include <drm/drm_dp_helper.h>
236fad8f66SThierry Reding #include <drm/drm_panel.h>
246b6b6042SThierry Reding 
256b6b6042SThierry Reding #include "dc.h"
266b6b6042SThierry Reding #include "drm.h"
276b6b6042SThierry Reding #include "sor.h"
286b6b6042SThierry Reding 
29459cc2c6SThierry Reding #define SOR_REKEY 0x38
30459cc2c6SThierry Reding 
31459cc2c6SThierry Reding struct tegra_sor_hdmi_settings {
32459cc2c6SThierry Reding 	unsigned long frequency;
33459cc2c6SThierry Reding 
34459cc2c6SThierry Reding 	u8 vcocap;
35459cc2c6SThierry Reding 	u8 ichpmp;
36459cc2c6SThierry Reding 	u8 loadadj;
37459cc2c6SThierry Reding 	u8 termadj;
38459cc2c6SThierry Reding 	u8 tx_pu;
39459cc2c6SThierry Reding 	u8 bg_vref;
40459cc2c6SThierry Reding 
41459cc2c6SThierry Reding 	u8 drive_current[4];
42459cc2c6SThierry Reding 	u8 preemphasis[4];
43459cc2c6SThierry Reding };
44459cc2c6SThierry Reding 
45459cc2c6SThierry Reding #if 1
46459cc2c6SThierry Reding static const struct tegra_sor_hdmi_settings tegra210_sor_hdmi_defaults[] = {
47459cc2c6SThierry Reding 	{
48459cc2c6SThierry Reding 		.frequency = 54000000,
49459cc2c6SThierry Reding 		.vcocap = 0x0,
50459cc2c6SThierry Reding 		.ichpmp = 0x1,
51459cc2c6SThierry Reding 		.loadadj = 0x3,
52459cc2c6SThierry Reding 		.termadj = 0x9,
53459cc2c6SThierry Reding 		.tx_pu = 0x10,
54459cc2c6SThierry Reding 		.bg_vref = 0x8,
55459cc2c6SThierry Reding 		.drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
56459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
57459cc2c6SThierry Reding 	}, {
58459cc2c6SThierry Reding 		.frequency = 75000000,
59459cc2c6SThierry Reding 		.vcocap = 0x3,
60459cc2c6SThierry Reding 		.ichpmp = 0x1,
61459cc2c6SThierry Reding 		.loadadj = 0x3,
62459cc2c6SThierry Reding 		.termadj = 0x9,
63459cc2c6SThierry Reding 		.tx_pu = 0x40,
64459cc2c6SThierry Reding 		.bg_vref = 0x8,
65459cc2c6SThierry Reding 		.drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
66459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
67459cc2c6SThierry Reding 	}, {
68459cc2c6SThierry Reding 		.frequency = 150000000,
69459cc2c6SThierry Reding 		.vcocap = 0x3,
70459cc2c6SThierry Reding 		.ichpmp = 0x1,
71459cc2c6SThierry Reding 		.loadadj = 0x3,
72459cc2c6SThierry Reding 		.termadj = 0x9,
73459cc2c6SThierry Reding 		.tx_pu = 0x66,
74459cc2c6SThierry Reding 		.bg_vref = 0x8,
75459cc2c6SThierry Reding 		.drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
76459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
77459cc2c6SThierry Reding 	}, {
78459cc2c6SThierry Reding 		.frequency = 300000000,
79459cc2c6SThierry Reding 		.vcocap = 0x3,
80459cc2c6SThierry Reding 		.ichpmp = 0x1,
81459cc2c6SThierry Reding 		.loadadj = 0x3,
82459cc2c6SThierry Reding 		.termadj = 0x9,
83459cc2c6SThierry Reding 		.tx_pu = 0x66,
84459cc2c6SThierry Reding 		.bg_vref = 0xa,
85459cc2c6SThierry Reding 		.drive_current = { 0x33, 0x3f, 0x3f, 0x3f },
86459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x17, 0x17, 0x17 },
87459cc2c6SThierry Reding 	}, {
88459cc2c6SThierry Reding 		.frequency = 600000000,
89459cc2c6SThierry Reding 		.vcocap = 0x3,
90459cc2c6SThierry Reding 		.ichpmp = 0x1,
91459cc2c6SThierry Reding 		.loadadj = 0x3,
92459cc2c6SThierry Reding 		.termadj = 0x9,
93459cc2c6SThierry Reding 		.tx_pu = 0x66,
94459cc2c6SThierry Reding 		.bg_vref = 0x8,
95459cc2c6SThierry Reding 		.drive_current = { 0x33, 0x3f, 0x3f, 0x3f },
96459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
97459cc2c6SThierry Reding 	},
98459cc2c6SThierry Reding };
99459cc2c6SThierry Reding #else
100459cc2c6SThierry Reding static const struct tegra_sor_hdmi_settings tegra210_sor_hdmi_defaults[] = {
101459cc2c6SThierry Reding 	{
102459cc2c6SThierry Reding 		.frequency = 75000000,
103459cc2c6SThierry Reding 		.vcocap = 0x3,
104459cc2c6SThierry Reding 		.ichpmp = 0x1,
105459cc2c6SThierry Reding 		.loadadj = 0x3,
106459cc2c6SThierry Reding 		.termadj = 0x9,
107459cc2c6SThierry Reding 		.tx_pu = 0x40,
108459cc2c6SThierry Reding 		.bg_vref = 0x8,
109459cc2c6SThierry Reding 		.drive_current = { 0x29, 0x29, 0x29, 0x29 },
110459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
111459cc2c6SThierry Reding 	}, {
112459cc2c6SThierry Reding 		.frequency = 150000000,
113459cc2c6SThierry Reding 		.vcocap = 0x3,
114459cc2c6SThierry Reding 		.ichpmp = 0x1,
115459cc2c6SThierry Reding 		.loadadj = 0x3,
116459cc2c6SThierry Reding 		.termadj = 0x9,
117459cc2c6SThierry Reding 		.tx_pu = 0x66,
118459cc2c6SThierry Reding 		.bg_vref = 0x8,
119459cc2c6SThierry Reding 		.drive_current = { 0x30, 0x37, 0x37, 0x37 },
120459cc2c6SThierry Reding 		.preemphasis = { 0x01, 0x02, 0x02, 0x02 },
121459cc2c6SThierry Reding 	}, {
122459cc2c6SThierry Reding 		.frequency = 300000000,
123459cc2c6SThierry Reding 		.vcocap = 0x3,
124459cc2c6SThierry Reding 		.ichpmp = 0x6,
125459cc2c6SThierry Reding 		.loadadj = 0x3,
126459cc2c6SThierry Reding 		.termadj = 0x9,
127459cc2c6SThierry Reding 		.tx_pu = 0x66,
128459cc2c6SThierry Reding 		.bg_vref = 0xf,
129459cc2c6SThierry Reding 		.drive_current = { 0x30, 0x37, 0x37, 0x37 },
130459cc2c6SThierry Reding 		.preemphasis = { 0x10, 0x3e, 0x3e, 0x3e },
131459cc2c6SThierry Reding 	}, {
132459cc2c6SThierry Reding 		.frequency = 600000000,
133459cc2c6SThierry Reding 		.vcocap = 0x3,
134459cc2c6SThierry Reding 		.ichpmp = 0xa,
135459cc2c6SThierry Reding 		.loadadj = 0x3,
136459cc2c6SThierry Reding 		.termadj = 0xb,
137459cc2c6SThierry Reding 		.tx_pu = 0x66,
138459cc2c6SThierry Reding 		.bg_vref = 0xe,
139459cc2c6SThierry Reding 		.drive_current = { 0x35, 0x3e, 0x3e, 0x3e },
140459cc2c6SThierry Reding 		.preemphasis = { 0x02, 0x3f, 0x3f, 0x3f },
141459cc2c6SThierry Reding 	},
142459cc2c6SThierry Reding };
143459cc2c6SThierry Reding #endif
144459cc2c6SThierry Reding 
145459cc2c6SThierry Reding struct tegra_sor_soc {
146459cc2c6SThierry Reding 	bool supports_edp;
147459cc2c6SThierry Reding 	bool supports_lvds;
148459cc2c6SThierry Reding 	bool supports_hdmi;
149459cc2c6SThierry Reding 	bool supports_dp;
150459cc2c6SThierry Reding 
151459cc2c6SThierry Reding 	const struct tegra_sor_hdmi_settings *settings;
152459cc2c6SThierry Reding 	unsigned int num_settings;
153459cc2c6SThierry Reding };
154459cc2c6SThierry Reding 
155459cc2c6SThierry Reding struct tegra_sor;
156459cc2c6SThierry Reding 
157459cc2c6SThierry Reding struct tegra_sor_ops {
158459cc2c6SThierry Reding 	const char *name;
159459cc2c6SThierry Reding 	int (*probe)(struct tegra_sor *sor);
160459cc2c6SThierry Reding 	int (*remove)(struct tegra_sor *sor);
161459cc2c6SThierry Reding };
162459cc2c6SThierry Reding 
1636b6b6042SThierry Reding struct tegra_sor {
1646b6b6042SThierry Reding 	struct host1x_client client;
1656b6b6042SThierry Reding 	struct tegra_output output;
1666b6b6042SThierry Reding 	struct device *dev;
1676b6b6042SThierry Reding 
168459cc2c6SThierry Reding 	const struct tegra_sor_soc *soc;
1696b6b6042SThierry Reding 	void __iomem *regs;
1706b6b6042SThierry Reding 
1716b6b6042SThierry Reding 	struct reset_control *rst;
1726b6b6042SThierry Reding 	struct clk *clk_parent;
1736b6b6042SThierry Reding 	struct clk *clk_safe;
1746b6b6042SThierry Reding 	struct clk *clk_dp;
1756b6b6042SThierry Reding 	struct clk *clk;
1766b6b6042SThierry Reding 
1779542c237SThierry Reding 	struct drm_dp_aux *aux;
1786b6b6042SThierry Reding 
179dab16336SThierry Reding 	struct drm_info_list *debugfs_files;
180dab16336SThierry Reding 	struct drm_minor *minor;
181a82752e1SThierry Reding 	struct dentry *debugfs;
182459cc2c6SThierry Reding 
183459cc2c6SThierry Reding 	const struct tegra_sor_ops *ops;
184459cc2c6SThierry Reding 
185459cc2c6SThierry Reding 	/* for HDMI 2.0 */
186459cc2c6SThierry Reding 	struct tegra_sor_hdmi_settings *settings;
187459cc2c6SThierry Reding 	unsigned int num_settings;
188459cc2c6SThierry Reding 
189459cc2c6SThierry Reding 	struct regulator *avdd_io_supply;
190459cc2c6SThierry Reding 	struct regulator *vdd_pll_supply;
191459cc2c6SThierry Reding 	struct regulator *hdmi_supply;
1926b6b6042SThierry Reding };
1936b6b6042SThierry Reding 
194c31efa7aSThierry Reding struct tegra_sor_state {
195c31efa7aSThierry Reding 	struct drm_connector_state base;
196c31efa7aSThierry Reding 
197c31efa7aSThierry Reding 	unsigned int bpc;
198c31efa7aSThierry Reding };
199c31efa7aSThierry Reding 
200c31efa7aSThierry Reding static inline struct tegra_sor_state *
201c31efa7aSThierry Reding to_sor_state(struct drm_connector_state *state)
202c31efa7aSThierry Reding {
203c31efa7aSThierry Reding 	return container_of(state, struct tegra_sor_state, base);
204c31efa7aSThierry Reding }
205c31efa7aSThierry Reding 
20634fa183bSThierry Reding struct tegra_sor_config {
20734fa183bSThierry Reding 	u32 bits_per_pixel;
20834fa183bSThierry Reding 
20934fa183bSThierry Reding 	u32 active_polarity;
21034fa183bSThierry Reding 	u32 active_count;
21134fa183bSThierry Reding 	u32 tu_size;
21234fa183bSThierry Reding 	u32 active_frac;
21334fa183bSThierry Reding 	u32 watermark;
2147890b576SThierry Reding 
2157890b576SThierry Reding 	u32 hblank_symbols;
2167890b576SThierry Reding 	u32 vblank_symbols;
21734fa183bSThierry Reding };
21834fa183bSThierry Reding 
2196b6b6042SThierry Reding static inline struct tegra_sor *
2206b6b6042SThierry Reding host1x_client_to_sor(struct host1x_client *client)
2216b6b6042SThierry Reding {
2226b6b6042SThierry Reding 	return container_of(client, struct tegra_sor, client);
2236b6b6042SThierry Reding }
2246b6b6042SThierry Reding 
2256b6b6042SThierry Reding static inline struct tegra_sor *to_sor(struct tegra_output *output)
2266b6b6042SThierry Reding {
2276b6b6042SThierry Reding 	return container_of(output, struct tegra_sor, output);
2286b6b6042SThierry Reding }
2296b6b6042SThierry Reding 
23028fe2076SThierry Reding static inline u32 tegra_sor_readl(struct tegra_sor *sor, unsigned long offset)
2316b6b6042SThierry Reding {
2326b6b6042SThierry Reding 	return readl(sor->regs + (offset << 2));
2336b6b6042SThierry Reding }
2346b6b6042SThierry Reding 
23528fe2076SThierry Reding static inline void tegra_sor_writel(struct tegra_sor *sor, u32 value,
2366b6b6042SThierry Reding 				    unsigned long offset)
2376b6b6042SThierry Reding {
2386b6b6042SThierry Reding 	writel(value, sor->regs + (offset << 2));
2396b6b6042SThierry Reding }
2406b6b6042SThierry Reding 
24125bb2cecSThierry Reding static int tegra_sor_set_parent_clock(struct tegra_sor *sor, struct clk *parent)
24225bb2cecSThierry Reding {
24325bb2cecSThierry Reding 	int err;
24425bb2cecSThierry Reding 
24525bb2cecSThierry Reding 	clk_disable_unprepare(sor->clk);
24625bb2cecSThierry Reding 
24725bb2cecSThierry Reding 	err = clk_set_parent(sor->clk, parent);
24825bb2cecSThierry Reding 	if (err < 0)
24925bb2cecSThierry Reding 		return err;
25025bb2cecSThierry Reding 
25125bb2cecSThierry Reding 	err = clk_prepare_enable(sor->clk);
25225bb2cecSThierry Reding 	if (err < 0)
25325bb2cecSThierry Reding 		return err;
25425bb2cecSThierry Reding 
25525bb2cecSThierry Reding 	return 0;
25625bb2cecSThierry Reding }
25725bb2cecSThierry Reding 
2586b6b6042SThierry Reding static int tegra_sor_dp_train_fast(struct tegra_sor *sor,
2596b6b6042SThierry Reding 				   struct drm_dp_link *link)
2606b6b6042SThierry Reding {
2616b6b6042SThierry Reding 	unsigned int i;
2626b6b6042SThierry Reding 	u8 pattern;
26328fe2076SThierry Reding 	u32 value;
2646b6b6042SThierry Reding 	int err;
2656b6b6042SThierry Reding 
2666b6b6042SThierry Reding 	/* setup lane parameters */
2676b6b6042SThierry Reding 	value = SOR_LANE_DRIVE_CURRENT_LANE3(0x40) |
2686b6b6042SThierry Reding 		SOR_LANE_DRIVE_CURRENT_LANE2(0x40) |
2696b6b6042SThierry Reding 		SOR_LANE_DRIVE_CURRENT_LANE1(0x40) |
2706b6b6042SThierry Reding 		SOR_LANE_DRIVE_CURRENT_LANE0(0x40);
271a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT0);
2726b6b6042SThierry Reding 
2736b6b6042SThierry Reding 	value = SOR_LANE_PREEMPHASIS_LANE3(0x0f) |
2746b6b6042SThierry Reding 		SOR_LANE_PREEMPHASIS_LANE2(0x0f) |
2756b6b6042SThierry Reding 		SOR_LANE_PREEMPHASIS_LANE1(0x0f) |
2766b6b6042SThierry Reding 		SOR_LANE_PREEMPHASIS_LANE0(0x0f);
277a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS0);
2786b6b6042SThierry Reding 
279a9a9e4fdSThierry Reding 	value = SOR_LANE_POSTCURSOR_LANE3(0x00) |
280a9a9e4fdSThierry Reding 		SOR_LANE_POSTCURSOR_LANE2(0x00) |
281a9a9e4fdSThierry Reding 		SOR_LANE_POSTCURSOR_LANE1(0x00) |
282a9a9e4fdSThierry Reding 		SOR_LANE_POSTCURSOR_LANE0(0x00);
283a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_POSTCURSOR0);
2846b6b6042SThierry Reding 
2856b6b6042SThierry Reding 	/* disable LVDS mode */
2866b6b6042SThierry Reding 	tegra_sor_writel(sor, 0, SOR_LVDS);
2876b6b6042SThierry Reding 
288a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
2896b6b6042SThierry Reding 	value |= SOR_DP_PADCTL_TX_PU_ENABLE;
2906b6b6042SThierry Reding 	value &= ~SOR_DP_PADCTL_TX_PU_MASK;
2916b6b6042SThierry Reding 	value |= SOR_DP_PADCTL_TX_PU(2); /* XXX: don't hardcode? */
292a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
2936b6b6042SThierry Reding 
294a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
2956b6b6042SThierry Reding 	value |= SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
2966b6b6042SThierry Reding 		 SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0;
297a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
2986b6b6042SThierry Reding 
2996b6b6042SThierry Reding 	usleep_range(10, 100);
3006b6b6042SThierry Reding 
301a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
3026b6b6042SThierry Reding 	value &= ~(SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
3036b6b6042SThierry Reding 		   SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0);
304a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
3056b6b6042SThierry Reding 
3069542c237SThierry Reding 	err = drm_dp_aux_prepare(sor->aux, DP_SET_ANSI_8B10B);
3076b6b6042SThierry Reding 	if (err < 0)
3086b6b6042SThierry Reding 		return err;
3096b6b6042SThierry Reding 
3106b6b6042SThierry Reding 	for (i = 0, value = 0; i < link->num_lanes; i++) {
3116b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
3126b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_NONE |
3136b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_TRAIN1;
3146b6b6042SThierry Reding 		value = (value << 8) | lane;
3156b6b6042SThierry Reding 	}
3166b6b6042SThierry Reding 
3176b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
3186b6b6042SThierry Reding 
3196b6b6042SThierry Reding 	pattern = DP_TRAINING_PATTERN_1;
3206b6b6042SThierry Reding 
3219542c237SThierry Reding 	err = drm_dp_aux_train(sor->aux, link, pattern);
3226b6b6042SThierry Reding 	if (err < 0)
3236b6b6042SThierry Reding 		return err;
3246b6b6042SThierry Reding 
325a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_SPARE0);
3266b6b6042SThierry Reding 	value |= SOR_DP_SPARE_SEQ_ENABLE;
3276b6b6042SThierry Reding 	value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
3286b6b6042SThierry Reding 	value |= SOR_DP_SPARE_MACRO_SOR_CLK;
329a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_SPARE0);
3306b6b6042SThierry Reding 
3316b6b6042SThierry Reding 	for (i = 0, value = 0; i < link->num_lanes; i++) {
3326b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
3336b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_NONE |
3346b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_TRAIN2;
3356b6b6042SThierry Reding 		value = (value << 8) | lane;
3366b6b6042SThierry Reding 	}
3376b6b6042SThierry Reding 
3386b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
3396b6b6042SThierry Reding 
3406b6b6042SThierry Reding 	pattern = DP_LINK_SCRAMBLING_DISABLE | DP_TRAINING_PATTERN_2;
3416b6b6042SThierry Reding 
3429542c237SThierry Reding 	err = drm_dp_aux_train(sor->aux, link, pattern);
3436b6b6042SThierry Reding 	if (err < 0)
3446b6b6042SThierry Reding 		return err;
3456b6b6042SThierry Reding 
3466b6b6042SThierry Reding 	for (i = 0, value = 0; i < link->num_lanes; i++) {
3476b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
3486b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_GALIOS |
3496b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_NONE;
3506b6b6042SThierry Reding 		value = (value << 8) | lane;
3516b6b6042SThierry Reding 	}
3526b6b6042SThierry Reding 
3536b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
3546b6b6042SThierry Reding 
3556b6b6042SThierry Reding 	pattern = DP_TRAINING_PATTERN_DISABLE;
3566b6b6042SThierry Reding 
3579542c237SThierry Reding 	err = drm_dp_aux_train(sor->aux, link, pattern);
3586b6b6042SThierry Reding 	if (err < 0)
3596b6b6042SThierry Reding 		return err;
3606b6b6042SThierry Reding 
3616b6b6042SThierry Reding 	return 0;
3626b6b6042SThierry Reding }
3636b6b6042SThierry Reding 
364459cc2c6SThierry Reding static void tegra_sor_dp_term_calibrate(struct tegra_sor *sor)
365459cc2c6SThierry Reding {
366459cc2c6SThierry Reding 	u32 mask = 0x08, adj = 0, value;
367459cc2c6SThierry Reding 
368459cc2c6SThierry Reding 	/* enable pad calibration logic */
369459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
370459cc2c6SThierry Reding 	value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
371459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
372459cc2c6SThierry Reding 
373459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL1);
374459cc2c6SThierry Reding 	value |= SOR_PLL1_TMDS_TERM;
375459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL1);
376459cc2c6SThierry Reding 
377459cc2c6SThierry Reding 	while (mask) {
378459cc2c6SThierry Reding 		adj |= mask;
379459cc2c6SThierry Reding 
380459cc2c6SThierry Reding 		value = tegra_sor_readl(sor, SOR_PLL1);
381459cc2c6SThierry Reding 		value &= ~SOR_PLL1_TMDS_TERMADJ_MASK;
382459cc2c6SThierry Reding 		value |= SOR_PLL1_TMDS_TERMADJ(adj);
383459cc2c6SThierry Reding 		tegra_sor_writel(sor, value, SOR_PLL1);
384459cc2c6SThierry Reding 
385459cc2c6SThierry Reding 		usleep_range(100, 200);
386459cc2c6SThierry Reding 
387459cc2c6SThierry Reding 		value = tegra_sor_readl(sor, SOR_PLL1);
388459cc2c6SThierry Reding 		if (value & SOR_PLL1_TERM_COMPOUT)
389459cc2c6SThierry Reding 			adj &= ~mask;
390459cc2c6SThierry Reding 
391459cc2c6SThierry Reding 		mask >>= 1;
392459cc2c6SThierry Reding 	}
393459cc2c6SThierry Reding 
394459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL1);
395459cc2c6SThierry Reding 	value &= ~SOR_PLL1_TMDS_TERMADJ_MASK;
396459cc2c6SThierry Reding 	value |= SOR_PLL1_TMDS_TERMADJ(adj);
397459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL1);
398459cc2c6SThierry Reding 
399459cc2c6SThierry Reding 	/* disable pad calibration logic */
400459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
401459cc2c6SThierry Reding 	value |= SOR_DP_PADCTL_PAD_CAL_PD;
402459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
403459cc2c6SThierry Reding }
404459cc2c6SThierry Reding 
4056b6b6042SThierry Reding static void tegra_sor_super_update(struct tegra_sor *sor)
4066b6b6042SThierry Reding {
407a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
408a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 1, SOR_SUPER_STATE0);
409a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
4106b6b6042SThierry Reding }
4116b6b6042SThierry Reding 
4126b6b6042SThierry Reding static void tegra_sor_update(struct tegra_sor *sor)
4136b6b6042SThierry Reding {
414a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE0);
415a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 1, SOR_STATE0);
416a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE0);
4176b6b6042SThierry Reding }
4186b6b6042SThierry Reding 
4196b6b6042SThierry Reding static int tegra_sor_setup_pwm(struct tegra_sor *sor, unsigned long timeout)
4206b6b6042SThierry Reding {
42128fe2076SThierry Reding 	u32 value;
4226b6b6042SThierry Reding 
4236b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWM_DIV);
4246b6b6042SThierry Reding 	value &= ~SOR_PWM_DIV_MASK;
4256b6b6042SThierry Reding 	value |= 0x400; /* period */
4266b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWM_DIV);
4276b6b6042SThierry Reding 
4286b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWM_CTL);
4296b6b6042SThierry Reding 	value &= ~SOR_PWM_CTL_DUTY_CYCLE_MASK;
4306b6b6042SThierry Reding 	value |= 0x400; /* duty cycle */
4316b6b6042SThierry Reding 	value &= ~SOR_PWM_CTL_CLK_SEL; /* clock source: PCLK */
4326b6b6042SThierry Reding 	value |= SOR_PWM_CTL_TRIGGER;
4336b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWM_CTL);
4346b6b6042SThierry Reding 
4356b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
4366b6b6042SThierry Reding 
4376b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
4386b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWM_CTL);
4396b6b6042SThierry Reding 		if ((value & SOR_PWM_CTL_TRIGGER) == 0)
4406b6b6042SThierry Reding 			return 0;
4416b6b6042SThierry Reding 
4426b6b6042SThierry Reding 		usleep_range(25, 100);
4436b6b6042SThierry Reding 	}
4446b6b6042SThierry Reding 
4456b6b6042SThierry Reding 	return -ETIMEDOUT;
4466b6b6042SThierry Reding }
4476b6b6042SThierry Reding 
4486b6b6042SThierry Reding static int tegra_sor_attach(struct tegra_sor *sor)
4496b6b6042SThierry Reding {
4506b6b6042SThierry Reding 	unsigned long value, timeout;
4516b6b6042SThierry Reding 
4526b6b6042SThierry Reding 	/* wake up in normal mode */
453a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
4546b6b6042SThierry Reding 	value |= SOR_SUPER_STATE_HEAD_MODE_AWAKE;
4556b6b6042SThierry Reding 	value |= SOR_SUPER_STATE_MODE_NORMAL;
456a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
4576b6b6042SThierry Reding 	tegra_sor_super_update(sor);
4586b6b6042SThierry Reding 
4596b6b6042SThierry Reding 	/* attach */
460a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
4616b6b6042SThierry Reding 	value |= SOR_SUPER_STATE_ATTACHED;
462a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
4636b6b6042SThierry Reding 	tegra_sor_super_update(sor);
4646b6b6042SThierry Reding 
4656b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
4666b6b6042SThierry Reding 
4676b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
4686b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_TEST);
4696b6b6042SThierry Reding 		if ((value & SOR_TEST_ATTACHED) != 0)
4706b6b6042SThierry Reding 			return 0;
4716b6b6042SThierry Reding 
4726b6b6042SThierry Reding 		usleep_range(25, 100);
4736b6b6042SThierry Reding 	}
4746b6b6042SThierry Reding 
4756b6b6042SThierry Reding 	return -ETIMEDOUT;
4766b6b6042SThierry Reding }
4776b6b6042SThierry Reding 
4786b6b6042SThierry Reding static int tegra_sor_wakeup(struct tegra_sor *sor)
4796b6b6042SThierry Reding {
4806b6b6042SThierry Reding 	unsigned long value, timeout;
4816b6b6042SThierry Reding 
4826b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
4836b6b6042SThierry Reding 
4846b6b6042SThierry Reding 	/* wait for head to wake up */
4856b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
4866b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_TEST);
4876b6b6042SThierry Reding 		value &= SOR_TEST_HEAD_MODE_MASK;
4886b6b6042SThierry Reding 
4896b6b6042SThierry Reding 		if (value == SOR_TEST_HEAD_MODE_AWAKE)
4906b6b6042SThierry Reding 			return 0;
4916b6b6042SThierry Reding 
4926b6b6042SThierry Reding 		usleep_range(25, 100);
4936b6b6042SThierry Reding 	}
4946b6b6042SThierry Reding 
4956b6b6042SThierry Reding 	return -ETIMEDOUT;
4966b6b6042SThierry Reding }
4976b6b6042SThierry Reding 
4986b6b6042SThierry Reding static int tegra_sor_power_up(struct tegra_sor *sor, unsigned long timeout)
4996b6b6042SThierry Reding {
50028fe2076SThierry Reding 	u32 value;
5016b6b6042SThierry Reding 
5026b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWR);
5036b6b6042SThierry Reding 	value |= SOR_PWR_TRIGGER | SOR_PWR_NORMAL_STATE_PU;
5046b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWR);
5056b6b6042SThierry Reding 
5066b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
5076b6b6042SThierry Reding 
5086b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
5096b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWR);
5106b6b6042SThierry Reding 		if ((value & SOR_PWR_TRIGGER) == 0)
5116b6b6042SThierry Reding 			return 0;
5126b6b6042SThierry Reding 
5136b6b6042SThierry Reding 		usleep_range(25, 100);
5146b6b6042SThierry Reding 	}
5156b6b6042SThierry Reding 
5166b6b6042SThierry Reding 	return -ETIMEDOUT;
5176b6b6042SThierry Reding }
5186b6b6042SThierry Reding 
51934fa183bSThierry Reding struct tegra_sor_params {
52034fa183bSThierry Reding 	/* number of link clocks per line */
52134fa183bSThierry Reding 	unsigned int num_clocks;
52234fa183bSThierry Reding 	/* ratio between input and output */
52334fa183bSThierry Reding 	u64 ratio;
52434fa183bSThierry Reding 	/* precision factor */
52534fa183bSThierry Reding 	u64 precision;
52634fa183bSThierry Reding 
52734fa183bSThierry Reding 	unsigned int active_polarity;
52834fa183bSThierry Reding 	unsigned int active_count;
52934fa183bSThierry Reding 	unsigned int active_frac;
53034fa183bSThierry Reding 	unsigned int tu_size;
53134fa183bSThierry Reding 	unsigned int error;
53234fa183bSThierry Reding };
53334fa183bSThierry Reding 
53434fa183bSThierry Reding static int tegra_sor_compute_params(struct tegra_sor *sor,
53534fa183bSThierry Reding 				    struct tegra_sor_params *params,
53634fa183bSThierry Reding 				    unsigned int tu_size)
53734fa183bSThierry Reding {
53834fa183bSThierry Reding 	u64 active_sym, active_count, frac, approx;
53934fa183bSThierry Reding 	u32 active_polarity, active_frac = 0;
54034fa183bSThierry Reding 	const u64 f = params->precision;
54134fa183bSThierry Reding 	s64 error;
54234fa183bSThierry Reding 
54334fa183bSThierry Reding 	active_sym = params->ratio * tu_size;
54434fa183bSThierry Reding 	active_count = div_u64(active_sym, f) * f;
54534fa183bSThierry Reding 	frac = active_sym - active_count;
54634fa183bSThierry Reding 
54734fa183bSThierry Reding 	/* fraction < 0.5 */
54834fa183bSThierry Reding 	if (frac >= (f / 2)) {
54934fa183bSThierry Reding 		active_polarity = 1;
55034fa183bSThierry Reding 		frac = f - frac;
55134fa183bSThierry Reding 	} else {
55234fa183bSThierry Reding 		active_polarity = 0;
55334fa183bSThierry Reding 	}
55434fa183bSThierry Reding 
55534fa183bSThierry Reding 	if (frac != 0) {
55634fa183bSThierry Reding 		frac = div_u64(f * f,  frac); /* 1/fraction */
55734fa183bSThierry Reding 		if (frac <= (15 * f)) {
55834fa183bSThierry Reding 			active_frac = div_u64(frac, f);
55934fa183bSThierry Reding 
56034fa183bSThierry Reding 			/* round up */
56134fa183bSThierry Reding 			if (active_polarity)
56234fa183bSThierry Reding 				active_frac++;
56334fa183bSThierry Reding 		} else {
56434fa183bSThierry Reding 			active_frac = active_polarity ? 1 : 15;
56534fa183bSThierry Reding 		}
56634fa183bSThierry Reding 	}
56734fa183bSThierry Reding 
56834fa183bSThierry Reding 	if (active_frac == 1)
56934fa183bSThierry Reding 		active_polarity = 0;
57034fa183bSThierry Reding 
57134fa183bSThierry Reding 	if (active_polarity == 1) {
57234fa183bSThierry Reding 		if (active_frac) {
57334fa183bSThierry Reding 			approx = active_count + (active_frac * (f - 1)) * f;
57434fa183bSThierry Reding 			approx = div_u64(approx, active_frac * f);
57534fa183bSThierry Reding 		} else {
57634fa183bSThierry Reding 			approx = active_count + f;
57734fa183bSThierry Reding 		}
57834fa183bSThierry Reding 	} else {
57934fa183bSThierry Reding 		if (active_frac)
58034fa183bSThierry Reding 			approx = active_count + div_u64(f, active_frac);
58134fa183bSThierry Reding 		else
58234fa183bSThierry Reding 			approx = active_count;
58334fa183bSThierry Reding 	}
58434fa183bSThierry Reding 
58534fa183bSThierry Reding 	error = div_s64(active_sym - approx, tu_size);
58634fa183bSThierry Reding 	error *= params->num_clocks;
58734fa183bSThierry Reding 
58879211c8eSAndrew Morton 	if (error <= 0 && abs(error) < params->error) {
58934fa183bSThierry Reding 		params->active_count = div_u64(active_count, f);
59034fa183bSThierry Reding 		params->active_polarity = active_polarity;
59134fa183bSThierry Reding 		params->active_frac = active_frac;
59279211c8eSAndrew Morton 		params->error = abs(error);
59334fa183bSThierry Reding 		params->tu_size = tu_size;
59434fa183bSThierry Reding 
59534fa183bSThierry Reding 		if (error == 0)
59634fa183bSThierry Reding 			return true;
59734fa183bSThierry Reding 	}
59834fa183bSThierry Reding 
59934fa183bSThierry Reding 	return false;
60034fa183bSThierry Reding }
60134fa183bSThierry Reding 
602a198359eSThierry Reding static int tegra_sor_compute_config(struct tegra_sor *sor,
60380444495SThierry Reding 				    const struct drm_display_mode *mode,
60434fa183bSThierry Reding 				    struct tegra_sor_config *config,
60534fa183bSThierry Reding 				    struct drm_dp_link *link)
60634fa183bSThierry Reding {
60734fa183bSThierry Reding 	const u64 f = 100000, link_rate = link->rate * 1000;
60834fa183bSThierry Reding 	const u64 pclk = mode->clock * 1000;
6097890b576SThierry Reding 	u64 input, output, watermark, num;
61034fa183bSThierry Reding 	struct tegra_sor_params params;
61134fa183bSThierry Reding 	u32 num_syms_per_line;
61234fa183bSThierry Reding 	unsigned int i;
61334fa183bSThierry Reding 
61434fa183bSThierry Reding 	if (!link_rate || !link->num_lanes || !pclk || !config->bits_per_pixel)
61534fa183bSThierry Reding 		return -EINVAL;
61634fa183bSThierry Reding 
61734fa183bSThierry Reding 	output = link_rate * 8 * link->num_lanes;
61834fa183bSThierry Reding 	input = pclk * config->bits_per_pixel;
61934fa183bSThierry Reding 
62034fa183bSThierry Reding 	if (input >= output)
62134fa183bSThierry Reding 		return -ERANGE;
62234fa183bSThierry Reding 
62334fa183bSThierry Reding 	memset(&params, 0, sizeof(params));
62434fa183bSThierry Reding 	params.ratio = div64_u64(input * f, output);
62534fa183bSThierry Reding 	params.num_clocks = div_u64(link_rate * mode->hdisplay, pclk);
62634fa183bSThierry Reding 	params.precision = f;
62734fa183bSThierry Reding 	params.error = 64 * f;
62834fa183bSThierry Reding 	params.tu_size = 64;
62934fa183bSThierry Reding 
63034fa183bSThierry Reding 	for (i = params.tu_size; i >= 32; i--)
63134fa183bSThierry Reding 		if (tegra_sor_compute_params(sor, &params, i))
63234fa183bSThierry Reding 			break;
63334fa183bSThierry Reding 
63434fa183bSThierry Reding 	if (params.active_frac == 0) {
63534fa183bSThierry Reding 		config->active_polarity = 0;
63634fa183bSThierry Reding 		config->active_count = params.active_count;
63734fa183bSThierry Reding 
63834fa183bSThierry Reding 		if (!params.active_polarity)
63934fa183bSThierry Reding 			config->active_count--;
64034fa183bSThierry Reding 
64134fa183bSThierry Reding 		config->tu_size = params.tu_size;
64234fa183bSThierry Reding 		config->active_frac = 1;
64334fa183bSThierry Reding 	} else {
64434fa183bSThierry Reding 		config->active_polarity = params.active_polarity;
64534fa183bSThierry Reding 		config->active_count = params.active_count;
64634fa183bSThierry Reding 		config->active_frac = params.active_frac;
64734fa183bSThierry Reding 		config->tu_size = params.tu_size;
64834fa183bSThierry Reding 	}
64934fa183bSThierry Reding 
65034fa183bSThierry Reding 	dev_dbg(sor->dev,
65134fa183bSThierry Reding 		"polarity: %d active count: %d tu size: %d active frac: %d\n",
65234fa183bSThierry Reding 		config->active_polarity, config->active_count,
65334fa183bSThierry Reding 		config->tu_size, config->active_frac);
65434fa183bSThierry Reding 
65534fa183bSThierry Reding 	watermark = params.ratio * config->tu_size * (f - params.ratio);
65634fa183bSThierry Reding 	watermark = div_u64(watermark, f);
65734fa183bSThierry Reding 
65834fa183bSThierry Reding 	watermark = div_u64(watermark + params.error, f);
65934fa183bSThierry Reding 	config->watermark = watermark + (config->bits_per_pixel / 8) + 2;
66034fa183bSThierry Reding 	num_syms_per_line = (mode->hdisplay * config->bits_per_pixel) *
66134fa183bSThierry Reding 			    (link->num_lanes * 8);
66234fa183bSThierry Reding 
66334fa183bSThierry Reding 	if (config->watermark > 30) {
66434fa183bSThierry Reding 		config->watermark = 30;
66534fa183bSThierry Reding 		dev_err(sor->dev,
66634fa183bSThierry Reding 			"unable to compute TU size, forcing watermark to %u\n",
66734fa183bSThierry Reding 			config->watermark);
66834fa183bSThierry Reding 	} else if (config->watermark > num_syms_per_line) {
66934fa183bSThierry Reding 		config->watermark = num_syms_per_line;
67034fa183bSThierry Reding 		dev_err(sor->dev, "watermark too high, forcing to %u\n",
67134fa183bSThierry Reding 			config->watermark);
67234fa183bSThierry Reding 	}
67334fa183bSThierry Reding 
6747890b576SThierry Reding 	/* compute the number of symbols per horizontal blanking interval */
6757890b576SThierry Reding 	num = ((mode->htotal - mode->hdisplay) - 7) * link_rate;
6767890b576SThierry Reding 	config->hblank_symbols = div_u64(num, pclk);
6777890b576SThierry Reding 
6787890b576SThierry Reding 	if (link->capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
6797890b576SThierry Reding 		config->hblank_symbols -= 3;
6807890b576SThierry Reding 
6817890b576SThierry Reding 	config->hblank_symbols -= 12 / link->num_lanes;
6827890b576SThierry Reding 
6837890b576SThierry Reding 	/* compute the number of symbols per vertical blanking interval */
6847890b576SThierry Reding 	num = (mode->hdisplay - 25) * link_rate;
6857890b576SThierry Reding 	config->vblank_symbols = div_u64(num, pclk);
6867890b576SThierry Reding 	config->vblank_symbols -= 36 / link->num_lanes + 4;
6877890b576SThierry Reding 
6887890b576SThierry Reding 	dev_dbg(sor->dev, "blank symbols: H:%u V:%u\n", config->hblank_symbols,
6897890b576SThierry Reding 		config->vblank_symbols);
6907890b576SThierry Reding 
69134fa183bSThierry Reding 	return 0;
69234fa183bSThierry Reding }
69334fa183bSThierry Reding 
694402f6bcdSThierry Reding static void tegra_sor_apply_config(struct tegra_sor *sor,
695402f6bcdSThierry Reding 				   const struct tegra_sor_config *config)
696402f6bcdSThierry Reding {
697402f6bcdSThierry Reding 	u32 value;
698402f6bcdSThierry Reding 
699402f6bcdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
700402f6bcdSThierry Reding 	value &= ~SOR_DP_LINKCTL_TU_SIZE_MASK;
701402f6bcdSThierry Reding 	value |= SOR_DP_LINKCTL_TU_SIZE(config->tu_size);
702402f6bcdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
703402f6bcdSThierry Reding 
704402f6bcdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_CONFIG0);
705402f6bcdSThierry Reding 	value &= ~SOR_DP_CONFIG_WATERMARK_MASK;
706402f6bcdSThierry Reding 	value |= SOR_DP_CONFIG_WATERMARK(config->watermark);
707402f6bcdSThierry Reding 
708402f6bcdSThierry Reding 	value &= ~SOR_DP_CONFIG_ACTIVE_SYM_COUNT_MASK;
709402f6bcdSThierry Reding 	value |= SOR_DP_CONFIG_ACTIVE_SYM_COUNT(config->active_count);
710402f6bcdSThierry Reding 
711402f6bcdSThierry Reding 	value &= ~SOR_DP_CONFIG_ACTIVE_SYM_FRAC_MASK;
712402f6bcdSThierry Reding 	value |= SOR_DP_CONFIG_ACTIVE_SYM_FRAC(config->active_frac);
713402f6bcdSThierry Reding 
714402f6bcdSThierry Reding 	if (config->active_polarity)
715402f6bcdSThierry Reding 		value |= SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
716402f6bcdSThierry Reding 	else
717402f6bcdSThierry Reding 		value &= ~SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
718402f6bcdSThierry Reding 
719402f6bcdSThierry Reding 	value |= SOR_DP_CONFIG_ACTIVE_SYM_ENABLE;
720402f6bcdSThierry Reding 	value |= SOR_DP_CONFIG_DISPARITY_NEGATIVE;
721402f6bcdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_CONFIG0);
722402f6bcdSThierry Reding 
723402f6bcdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_AUDIO_HBLANK_SYMBOLS);
724402f6bcdSThierry Reding 	value &= ~SOR_DP_AUDIO_HBLANK_SYMBOLS_MASK;
725402f6bcdSThierry Reding 	value |= config->hblank_symbols & 0xffff;
726402f6bcdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_AUDIO_HBLANK_SYMBOLS);
727402f6bcdSThierry Reding 
728402f6bcdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_AUDIO_VBLANK_SYMBOLS);
729402f6bcdSThierry Reding 	value &= ~SOR_DP_AUDIO_VBLANK_SYMBOLS_MASK;
730402f6bcdSThierry Reding 	value |= config->vblank_symbols & 0xffff;
731402f6bcdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_AUDIO_VBLANK_SYMBOLS);
732402f6bcdSThierry Reding }
733402f6bcdSThierry Reding 
7342bd1dd39SThierry Reding static void tegra_sor_mode_set(struct tegra_sor *sor,
7352bd1dd39SThierry Reding 			       const struct drm_display_mode *mode,
736c31efa7aSThierry Reding 			       struct tegra_sor_state *state)
7372bd1dd39SThierry Reding {
7382bd1dd39SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(sor->output.encoder.crtc);
7392bd1dd39SThierry Reding 	unsigned int vbe, vse, hbe, hse, vbs, hbs;
7402bd1dd39SThierry Reding 	u32 value;
7412bd1dd39SThierry Reding 
7422bd1dd39SThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
7432bd1dd39SThierry Reding 	value &= ~SOR_STATE_ASY_PIXELDEPTH_MASK;
7442bd1dd39SThierry Reding 	value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
7452bd1dd39SThierry Reding 	value &= ~SOR_STATE_ASY_OWNER_MASK;
7462bd1dd39SThierry Reding 
7472bd1dd39SThierry Reding 	value |= SOR_STATE_ASY_CRC_MODE_COMPLETE |
7482bd1dd39SThierry Reding 		 SOR_STATE_ASY_OWNER(dc->pipe + 1);
7492bd1dd39SThierry Reding 
7502bd1dd39SThierry Reding 	if (mode->flags & DRM_MODE_FLAG_PHSYNC)
7512bd1dd39SThierry Reding 		value &= ~SOR_STATE_ASY_HSYNCPOL;
7522bd1dd39SThierry Reding 
7532bd1dd39SThierry Reding 	if (mode->flags & DRM_MODE_FLAG_NHSYNC)
7542bd1dd39SThierry Reding 		value |= SOR_STATE_ASY_HSYNCPOL;
7552bd1dd39SThierry Reding 
7562bd1dd39SThierry Reding 	if (mode->flags & DRM_MODE_FLAG_PVSYNC)
7572bd1dd39SThierry Reding 		value &= ~SOR_STATE_ASY_VSYNCPOL;
7582bd1dd39SThierry Reding 
7592bd1dd39SThierry Reding 	if (mode->flags & DRM_MODE_FLAG_NVSYNC)
7602bd1dd39SThierry Reding 		value |= SOR_STATE_ASY_VSYNCPOL;
7612bd1dd39SThierry Reding 
762c31efa7aSThierry Reding 	switch (state->bpc) {
763c31efa7aSThierry Reding 	case 16:
764c31efa7aSThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_48_444;
765c31efa7aSThierry Reding 		break;
766c31efa7aSThierry Reding 
767c31efa7aSThierry Reding 	case 12:
768c31efa7aSThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_36_444;
769c31efa7aSThierry Reding 		break;
770c31efa7aSThierry Reding 
771c31efa7aSThierry Reding 	case 10:
772c31efa7aSThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_30_444;
773c31efa7aSThierry Reding 		break;
774c31efa7aSThierry Reding 
7752bd1dd39SThierry Reding 	case 8:
7762bd1dd39SThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
7772bd1dd39SThierry Reding 		break;
7782bd1dd39SThierry Reding 
7792bd1dd39SThierry Reding 	case 6:
7802bd1dd39SThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_18_444;
7812bd1dd39SThierry Reding 		break;
7822bd1dd39SThierry Reding 
7832bd1dd39SThierry Reding 	default:
784c31efa7aSThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
7852bd1dd39SThierry Reding 		break;
7862bd1dd39SThierry Reding 	}
7872bd1dd39SThierry Reding 
7882bd1dd39SThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
7892bd1dd39SThierry Reding 
7902bd1dd39SThierry Reding 	/*
7912bd1dd39SThierry Reding 	 * TODO: The video timing programming below doesn't seem to match the
7922bd1dd39SThierry Reding 	 * register definitions.
7932bd1dd39SThierry Reding 	 */
7942bd1dd39SThierry Reding 
7952bd1dd39SThierry Reding 	value = ((mode->vtotal & 0x7fff) << 16) | (mode->htotal & 0x7fff);
7962bd1dd39SThierry Reding 	tegra_sor_writel(sor, value, SOR_HEAD_STATE1(dc->pipe));
7972bd1dd39SThierry Reding 
7982bd1dd39SThierry Reding 	/* sync end = sync width - 1 */
7992bd1dd39SThierry Reding 	vse = mode->vsync_end - mode->vsync_start - 1;
8002bd1dd39SThierry Reding 	hse = mode->hsync_end - mode->hsync_start - 1;
8012bd1dd39SThierry Reding 
8022bd1dd39SThierry Reding 	value = ((vse & 0x7fff) << 16) | (hse & 0x7fff);
8032bd1dd39SThierry Reding 	tegra_sor_writel(sor, value, SOR_HEAD_STATE2(dc->pipe));
8042bd1dd39SThierry Reding 
8052bd1dd39SThierry Reding 	/* blank end = sync end + back porch */
8062bd1dd39SThierry Reding 	vbe = vse + (mode->vtotal - mode->vsync_end);
8072bd1dd39SThierry Reding 	hbe = hse + (mode->htotal - mode->hsync_end);
8082bd1dd39SThierry Reding 
8092bd1dd39SThierry Reding 	value = ((vbe & 0x7fff) << 16) | (hbe & 0x7fff);
8102bd1dd39SThierry Reding 	tegra_sor_writel(sor, value, SOR_HEAD_STATE3(dc->pipe));
8112bd1dd39SThierry Reding 
8122bd1dd39SThierry Reding 	/* blank start = blank end + active */
8132bd1dd39SThierry Reding 	vbs = vbe + mode->vdisplay;
8142bd1dd39SThierry Reding 	hbs = hbe + mode->hdisplay;
8152bd1dd39SThierry Reding 
8162bd1dd39SThierry Reding 	value = ((vbs & 0x7fff) << 16) | (hbs & 0x7fff);
8172bd1dd39SThierry Reding 	tegra_sor_writel(sor, value, SOR_HEAD_STATE4(dc->pipe));
8182bd1dd39SThierry Reding 
8192bd1dd39SThierry Reding 	/* XXX interlacing support */
8202bd1dd39SThierry Reding 	tegra_sor_writel(sor, 0x001, SOR_HEAD_STATE5(dc->pipe));
8212bd1dd39SThierry Reding }
8222bd1dd39SThierry Reding 
8236fad8f66SThierry Reding static int tegra_sor_detach(struct tegra_sor *sor)
8246b6b6042SThierry Reding {
8256fad8f66SThierry Reding 	unsigned long value, timeout;
8266fad8f66SThierry Reding 
8276fad8f66SThierry Reding 	/* switch to safe mode */
828a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
8296fad8f66SThierry Reding 	value &= ~SOR_SUPER_STATE_MODE_NORMAL;
830a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
8316fad8f66SThierry Reding 	tegra_sor_super_update(sor);
8326fad8f66SThierry Reding 
8336fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
8346fad8f66SThierry Reding 
8356fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
8366fad8f66SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWR);
8376fad8f66SThierry Reding 		if (value & SOR_PWR_MODE_SAFE)
8386fad8f66SThierry Reding 			break;
8396fad8f66SThierry Reding 	}
8406fad8f66SThierry Reding 
8416fad8f66SThierry Reding 	if ((value & SOR_PWR_MODE_SAFE) == 0)
8426fad8f66SThierry Reding 		return -ETIMEDOUT;
8436fad8f66SThierry Reding 
8446fad8f66SThierry Reding 	/* go to sleep */
845a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
8466fad8f66SThierry Reding 	value &= ~SOR_SUPER_STATE_HEAD_MODE_MASK;
847a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
8486fad8f66SThierry Reding 	tegra_sor_super_update(sor);
8496fad8f66SThierry Reding 
8506fad8f66SThierry Reding 	/* detach */
851a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
8526fad8f66SThierry Reding 	value &= ~SOR_SUPER_STATE_ATTACHED;
853a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
8546fad8f66SThierry Reding 	tegra_sor_super_update(sor);
8556fad8f66SThierry Reding 
8566fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
8576fad8f66SThierry Reding 
8586fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
8596fad8f66SThierry Reding 		value = tegra_sor_readl(sor, SOR_TEST);
8606fad8f66SThierry Reding 		if ((value & SOR_TEST_ATTACHED) == 0)
8616fad8f66SThierry Reding 			break;
8626fad8f66SThierry Reding 
8636fad8f66SThierry Reding 		usleep_range(25, 100);
8646fad8f66SThierry Reding 	}
8656fad8f66SThierry Reding 
8666fad8f66SThierry Reding 	if ((value & SOR_TEST_ATTACHED) != 0)
8676fad8f66SThierry Reding 		return -ETIMEDOUT;
8686fad8f66SThierry Reding 
8696fad8f66SThierry Reding 	return 0;
8706fad8f66SThierry Reding }
8716fad8f66SThierry Reding 
8726fad8f66SThierry Reding static int tegra_sor_power_down(struct tegra_sor *sor)
8736fad8f66SThierry Reding {
8746fad8f66SThierry Reding 	unsigned long value, timeout;
8756fad8f66SThierry Reding 	int err;
8766fad8f66SThierry Reding 
8776fad8f66SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWR);
8786fad8f66SThierry Reding 	value &= ~SOR_PWR_NORMAL_STATE_PU;
8796fad8f66SThierry Reding 	value |= SOR_PWR_TRIGGER;
8806fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWR);
8816fad8f66SThierry Reding 
8826fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
8836fad8f66SThierry Reding 
8846fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
8856fad8f66SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWR);
8866fad8f66SThierry Reding 		if ((value & SOR_PWR_TRIGGER) == 0)
8876fad8f66SThierry Reding 			return 0;
8886fad8f66SThierry Reding 
8896fad8f66SThierry Reding 		usleep_range(25, 100);
8906fad8f66SThierry Reding 	}
8916fad8f66SThierry Reding 
8926fad8f66SThierry Reding 	if ((value & SOR_PWR_TRIGGER) != 0)
8936fad8f66SThierry Reding 		return -ETIMEDOUT;
8946fad8f66SThierry Reding 
89525bb2cecSThierry Reding 	/* switch to safe parent clock */
89625bb2cecSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
8976fad8f66SThierry Reding 	if (err < 0)
8986fad8f66SThierry Reding 		dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
8996fad8f66SThierry Reding 
900a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
9016fad8f66SThierry Reding 	value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
9026fad8f66SThierry Reding 		   SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2);
903a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
9046fad8f66SThierry Reding 
9056fad8f66SThierry Reding 	/* stop lane sequencer */
9066fad8f66SThierry Reding 	value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_UP |
9076fad8f66SThierry Reding 		SOR_LANE_SEQ_CTL_POWER_STATE_DOWN;
9086fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
9096fad8f66SThierry Reding 
9106fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
9116fad8f66SThierry Reding 
9126fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
9136fad8f66SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
9146fad8f66SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
9156fad8f66SThierry Reding 			break;
9166fad8f66SThierry Reding 
9176fad8f66SThierry Reding 		usleep_range(25, 100);
9186fad8f66SThierry Reding 	}
9196fad8f66SThierry Reding 
9206fad8f66SThierry Reding 	if ((value & SOR_LANE_SEQ_CTL_TRIGGER) != 0)
9216fad8f66SThierry Reding 		return -ETIMEDOUT;
9226fad8f66SThierry Reding 
923a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
924a9a9e4fdSThierry Reding 	value |= SOR_PLL2_PORT_POWERDOWN;
925a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
9266fad8f66SThierry Reding 
9276fad8f66SThierry Reding 	usleep_range(20, 100);
9286fad8f66SThierry Reding 
929a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL0);
930a9a9e4fdSThierry Reding 	value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
931a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL0);
9326fad8f66SThierry Reding 
933a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
934a9a9e4fdSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD;
935a9a9e4fdSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
936a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
9376fad8f66SThierry Reding 
9386fad8f66SThierry Reding 	usleep_range(20, 100);
9396fad8f66SThierry Reding 
9406fad8f66SThierry Reding 	return 0;
9416fad8f66SThierry Reding }
9426fad8f66SThierry Reding 
9436fad8f66SThierry Reding static int tegra_sor_crc_wait(struct tegra_sor *sor, unsigned long timeout)
9446fad8f66SThierry Reding {
9456fad8f66SThierry Reding 	u32 value;
9466fad8f66SThierry Reding 
9476fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
9486fad8f66SThierry Reding 
9496fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
950a9a9e4fdSThierry Reding 		value = tegra_sor_readl(sor, SOR_CRCA);
951a9a9e4fdSThierry Reding 		if (value & SOR_CRCA_VALID)
9526fad8f66SThierry Reding 			return 0;
9536fad8f66SThierry Reding 
9546fad8f66SThierry Reding 		usleep_range(100, 200);
9556fad8f66SThierry Reding 	}
9566fad8f66SThierry Reding 
9576fad8f66SThierry Reding 	return -ETIMEDOUT;
9586fad8f66SThierry Reding }
9596fad8f66SThierry Reding 
960530239a8SThierry Reding static int tegra_sor_show_crc(struct seq_file *s, void *data)
9616fad8f66SThierry Reding {
962530239a8SThierry Reding 	struct drm_info_node *node = s->private;
963530239a8SThierry Reding 	struct tegra_sor *sor = node->info_ent->data;
964850bab44SThierry Reding 	struct drm_crtc *crtc = sor->output.encoder.crtc;
965850bab44SThierry Reding 	struct drm_device *drm = node->minor->dev;
966530239a8SThierry Reding 	int err = 0;
9676fad8f66SThierry Reding 	u32 value;
9686fad8f66SThierry Reding 
969850bab44SThierry Reding 	drm_modeset_lock_all(drm);
9706fad8f66SThierry Reding 
971850bab44SThierry Reding 	if (!crtc || !crtc->state->active) {
972850bab44SThierry Reding 		err = -EBUSY;
9736fad8f66SThierry Reding 		goto unlock;
9746fad8f66SThierry Reding 	}
9756fad8f66SThierry Reding 
976a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
9776fad8f66SThierry Reding 	value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
978a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
9796fad8f66SThierry Reding 
9806fad8f66SThierry Reding 	value = tegra_sor_readl(sor, SOR_CRC_CNTRL);
9816fad8f66SThierry Reding 	value |= SOR_CRC_CNTRL_ENABLE;
9826fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_CRC_CNTRL);
9836fad8f66SThierry Reding 
9846fad8f66SThierry Reding 	value = tegra_sor_readl(sor, SOR_TEST);
9856fad8f66SThierry Reding 	value &= ~SOR_TEST_CRC_POST_SERIALIZE;
9866fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_TEST);
9876fad8f66SThierry Reding 
9886fad8f66SThierry Reding 	err = tegra_sor_crc_wait(sor, 100);
9896fad8f66SThierry Reding 	if (err < 0)
9906fad8f66SThierry Reding 		goto unlock;
9916fad8f66SThierry Reding 
992a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, SOR_CRCA_RESET, SOR_CRCA);
993a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_CRCB);
9946fad8f66SThierry Reding 
995530239a8SThierry Reding 	seq_printf(s, "%08x\n", value);
9966fad8f66SThierry Reding 
9976fad8f66SThierry Reding unlock:
998850bab44SThierry Reding 	drm_modeset_unlock_all(drm);
9996fad8f66SThierry Reding 	return err;
10006fad8f66SThierry Reding }
10016fad8f66SThierry Reding 
1002dab16336SThierry Reding static int tegra_sor_show_regs(struct seq_file *s, void *data)
1003dab16336SThierry Reding {
1004dab16336SThierry Reding 	struct drm_info_node *node = s->private;
1005dab16336SThierry Reding 	struct tegra_sor *sor = node->info_ent->data;
1006850bab44SThierry Reding 	struct drm_crtc *crtc = sor->output.encoder.crtc;
1007850bab44SThierry Reding 	struct drm_device *drm = node->minor->dev;
1008850bab44SThierry Reding 	int err = 0;
1009850bab44SThierry Reding 
1010850bab44SThierry Reding 	drm_modeset_lock_all(drm);
1011850bab44SThierry Reding 
1012850bab44SThierry Reding 	if (!crtc || !crtc->state->active) {
1013850bab44SThierry Reding 		err = -EBUSY;
1014850bab44SThierry Reding 		goto unlock;
1015850bab44SThierry Reding 	}
1016dab16336SThierry Reding 
1017dab16336SThierry Reding #define DUMP_REG(name)						\
1018dab16336SThierry Reding 	seq_printf(s, "%-38s %#05x %08x\n", #name, name,	\
1019dab16336SThierry Reding 		   tegra_sor_readl(sor, name))
1020dab16336SThierry Reding 
1021dab16336SThierry Reding 	DUMP_REG(SOR_CTXSW);
1022a9a9e4fdSThierry Reding 	DUMP_REG(SOR_SUPER_STATE0);
1023a9a9e4fdSThierry Reding 	DUMP_REG(SOR_SUPER_STATE1);
1024a9a9e4fdSThierry Reding 	DUMP_REG(SOR_STATE0);
1025a9a9e4fdSThierry Reding 	DUMP_REG(SOR_STATE1);
1026a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE0(0));
1027a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE0(1));
1028a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE1(0));
1029a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE1(1));
1030a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE2(0));
1031a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE2(1));
1032a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE3(0));
1033a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE3(1));
1034a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE4(0));
1035a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE4(1));
1036a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE5(0));
1037a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE5(1));
1038dab16336SThierry Reding 	DUMP_REG(SOR_CRC_CNTRL);
1039dab16336SThierry Reding 	DUMP_REG(SOR_DP_DEBUG_MVID);
1040dab16336SThierry Reding 	DUMP_REG(SOR_CLK_CNTRL);
1041dab16336SThierry Reding 	DUMP_REG(SOR_CAP);
1042dab16336SThierry Reding 	DUMP_REG(SOR_PWR);
1043dab16336SThierry Reding 	DUMP_REG(SOR_TEST);
1044a9a9e4fdSThierry Reding 	DUMP_REG(SOR_PLL0);
1045a9a9e4fdSThierry Reding 	DUMP_REG(SOR_PLL1);
1046a9a9e4fdSThierry Reding 	DUMP_REG(SOR_PLL2);
1047a9a9e4fdSThierry Reding 	DUMP_REG(SOR_PLL3);
1048dab16336SThierry Reding 	DUMP_REG(SOR_CSTM);
1049dab16336SThierry Reding 	DUMP_REG(SOR_LVDS);
1050a9a9e4fdSThierry Reding 	DUMP_REG(SOR_CRCA);
1051a9a9e4fdSThierry Reding 	DUMP_REG(SOR_CRCB);
1052dab16336SThierry Reding 	DUMP_REG(SOR_BLANK);
1053dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_CTL);
1054dab16336SThierry Reding 	DUMP_REG(SOR_LANE_SEQ_CTL);
1055dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(0));
1056dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(1));
1057dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(2));
1058dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(3));
1059dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(4));
1060dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(5));
1061dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(6));
1062dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(7));
1063dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(8));
1064dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(9));
1065dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(10));
1066dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(11));
1067dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(12));
1068dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(13));
1069dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(14));
1070dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(15));
1071dab16336SThierry Reding 	DUMP_REG(SOR_PWM_DIV);
1072dab16336SThierry Reding 	DUMP_REG(SOR_PWM_CTL);
1073a9a9e4fdSThierry Reding 	DUMP_REG(SOR_VCRC_A0);
1074a9a9e4fdSThierry Reding 	DUMP_REG(SOR_VCRC_A1);
1075a9a9e4fdSThierry Reding 	DUMP_REG(SOR_VCRC_B0);
1076a9a9e4fdSThierry Reding 	DUMP_REG(SOR_VCRC_B1);
1077a9a9e4fdSThierry Reding 	DUMP_REG(SOR_CCRC_A0);
1078a9a9e4fdSThierry Reding 	DUMP_REG(SOR_CCRC_A1);
1079a9a9e4fdSThierry Reding 	DUMP_REG(SOR_CCRC_B0);
1080a9a9e4fdSThierry Reding 	DUMP_REG(SOR_CCRC_B1);
1081a9a9e4fdSThierry Reding 	DUMP_REG(SOR_EDATA_A0);
1082a9a9e4fdSThierry Reding 	DUMP_REG(SOR_EDATA_A1);
1083a9a9e4fdSThierry Reding 	DUMP_REG(SOR_EDATA_B0);
1084a9a9e4fdSThierry Reding 	DUMP_REG(SOR_EDATA_B1);
1085a9a9e4fdSThierry Reding 	DUMP_REG(SOR_COUNT_A0);
1086a9a9e4fdSThierry Reding 	DUMP_REG(SOR_COUNT_A1);
1087a9a9e4fdSThierry Reding 	DUMP_REG(SOR_COUNT_B0);
1088a9a9e4fdSThierry Reding 	DUMP_REG(SOR_COUNT_B1);
1089a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DEBUG_A0);
1090a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DEBUG_A1);
1091a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DEBUG_B0);
1092a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DEBUG_B1);
1093dab16336SThierry Reding 	DUMP_REG(SOR_TRIG);
1094dab16336SThierry Reding 	DUMP_REG(SOR_MSCHECK);
1095dab16336SThierry Reding 	DUMP_REG(SOR_XBAR_CTRL);
1096dab16336SThierry Reding 	DUMP_REG(SOR_XBAR_POL);
1097a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_LINKCTL0);
1098a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_LINKCTL1);
1099a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE_DRIVE_CURRENT0);
1100a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE_DRIVE_CURRENT1);
1101a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE4_DRIVE_CURRENT0);
1102a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE4_DRIVE_CURRENT1);
1103a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE_PREEMPHASIS0);
1104a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE_PREEMPHASIS1);
1105a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE4_PREEMPHASIS0);
1106a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE4_PREEMPHASIS1);
1107a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE_POSTCURSOR0);
1108a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE_POSTCURSOR1);
1109a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_CONFIG0);
1110a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_CONFIG1);
1111a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_MN0);
1112a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_MN1);
1113a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_PADCTL0);
1114a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_PADCTL1);
1115a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_DEBUG0);
1116a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_DEBUG1);
1117a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_SPARE0);
1118a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_SPARE1);
1119dab16336SThierry Reding 	DUMP_REG(SOR_DP_AUDIO_CTRL);
1120dab16336SThierry Reding 	DUMP_REG(SOR_DP_AUDIO_HBLANK_SYMBOLS);
1121dab16336SThierry Reding 	DUMP_REG(SOR_DP_AUDIO_VBLANK_SYMBOLS);
1122dab16336SThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_HEADER);
1123a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK0);
1124a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK1);
1125a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK2);
1126a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK3);
1127a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK4);
1128a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK5);
1129a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK6);
1130dab16336SThierry Reding 	DUMP_REG(SOR_DP_TPG);
1131dab16336SThierry Reding 	DUMP_REG(SOR_DP_TPG_CONFIG);
1132a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_LQ_CSTM0);
1133a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_LQ_CSTM1);
1134a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_LQ_CSTM2);
1135dab16336SThierry Reding 
1136dab16336SThierry Reding #undef DUMP_REG
1137dab16336SThierry Reding 
1138850bab44SThierry Reding unlock:
1139850bab44SThierry Reding 	drm_modeset_unlock_all(drm);
1140850bab44SThierry Reding 	return err;
1141dab16336SThierry Reding }
1142dab16336SThierry Reding 
1143dab16336SThierry Reding static const struct drm_info_list debugfs_files[] = {
1144530239a8SThierry Reding 	{ "crc", tegra_sor_show_crc, 0, NULL },
1145dab16336SThierry Reding 	{ "regs", tegra_sor_show_regs, 0, NULL },
1146dab16336SThierry Reding };
1147dab16336SThierry Reding 
11486fad8f66SThierry Reding static int tegra_sor_debugfs_init(struct tegra_sor *sor,
11496fad8f66SThierry Reding 				  struct drm_minor *minor)
11506fad8f66SThierry Reding {
1151459cc2c6SThierry Reding 	const char *name = sor->soc->supports_dp ? "sor1" : "sor";
1152dab16336SThierry Reding 	unsigned int i;
1153530239a8SThierry Reding 	int err;
11546fad8f66SThierry Reding 
1155459cc2c6SThierry Reding 	sor->debugfs = debugfs_create_dir(name, minor->debugfs_root);
11566fad8f66SThierry Reding 	if (!sor->debugfs)
11576fad8f66SThierry Reding 		return -ENOMEM;
11586fad8f66SThierry Reding 
1159dab16336SThierry Reding 	sor->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1160dab16336SThierry Reding 				     GFP_KERNEL);
1161dab16336SThierry Reding 	if (!sor->debugfs_files) {
11626fad8f66SThierry Reding 		err = -ENOMEM;
11636fad8f66SThierry Reding 		goto remove;
11646fad8f66SThierry Reding 	}
11656fad8f66SThierry Reding 
1166dab16336SThierry Reding 	for (i = 0; i < ARRAY_SIZE(debugfs_files); i++)
1167dab16336SThierry Reding 		sor->debugfs_files[i].data = sor;
1168dab16336SThierry Reding 
1169dab16336SThierry Reding 	err = drm_debugfs_create_files(sor->debugfs_files,
1170dab16336SThierry Reding 				       ARRAY_SIZE(debugfs_files),
1171dab16336SThierry Reding 				       sor->debugfs, minor);
1172dab16336SThierry Reding 	if (err < 0)
1173dab16336SThierry Reding 		goto free;
1174dab16336SThierry Reding 
11753ff1f22cSThierry Reding 	sor->minor = minor;
11763ff1f22cSThierry Reding 
1177530239a8SThierry Reding 	return 0;
11786fad8f66SThierry Reding 
1179dab16336SThierry Reding free:
1180dab16336SThierry Reding 	kfree(sor->debugfs_files);
1181dab16336SThierry Reding 	sor->debugfs_files = NULL;
11826fad8f66SThierry Reding remove:
1183dab16336SThierry Reding 	debugfs_remove_recursive(sor->debugfs);
11846fad8f66SThierry Reding 	sor->debugfs = NULL;
11856fad8f66SThierry Reding 	return err;
11866fad8f66SThierry Reding }
11876fad8f66SThierry Reding 
11884009c224SThierry Reding static void tegra_sor_debugfs_exit(struct tegra_sor *sor)
11896fad8f66SThierry Reding {
1190dab16336SThierry Reding 	drm_debugfs_remove_files(sor->debugfs_files, ARRAY_SIZE(debugfs_files),
1191dab16336SThierry Reding 				 sor->minor);
1192dab16336SThierry Reding 	sor->minor = NULL;
1193dab16336SThierry Reding 
1194dab16336SThierry Reding 	kfree(sor->debugfs_files);
1195066d30f8SThierry Reding 	sor->debugfs_files = NULL;
1196dab16336SThierry Reding 
1197dab16336SThierry Reding 	debugfs_remove_recursive(sor->debugfs);
1198066d30f8SThierry Reding 	sor->debugfs = NULL;
11996fad8f66SThierry Reding }
12006fad8f66SThierry Reding 
1201c31efa7aSThierry Reding static void tegra_sor_connector_reset(struct drm_connector *connector)
1202c31efa7aSThierry Reding {
1203c31efa7aSThierry Reding 	struct tegra_sor_state *state;
1204c31efa7aSThierry Reding 
1205c31efa7aSThierry Reding 	state = kzalloc(sizeof(*state), GFP_KERNEL);
1206c31efa7aSThierry Reding 	if (!state)
1207c31efa7aSThierry Reding 		return;
1208c31efa7aSThierry Reding 
1209c31efa7aSThierry Reding 	if (connector->state) {
1210c31efa7aSThierry Reding 		__drm_atomic_helper_connector_destroy_state(connector->state);
1211c31efa7aSThierry Reding 		kfree(connector->state);
1212c31efa7aSThierry Reding 	}
1213c31efa7aSThierry Reding 
1214c31efa7aSThierry Reding 	__drm_atomic_helper_connector_reset(connector, &state->base);
1215c31efa7aSThierry Reding }
1216c31efa7aSThierry Reding 
12176fad8f66SThierry Reding static enum drm_connector_status
12186fad8f66SThierry Reding tegra_sor_connector_detect(struct drm_connector *connector, bool force)
12196fad8f66SThierry Reding {
12206fad8f66SThierry Reding 	struct tegra_output *output = connector_to_output(connector);
12216fad8f66SThierry Reding 	struct tegra_sor *sor = to_sor(output);
12226fad8f66SThierry Reding 
12239542c237SThierry Reding 	if (sor->aux)
12249542c237SThierry Reding 		return drm_dp_aux_detect(sor->aux);
12256fad8f66SThierry Reding 
1226459cc2c6SThierry Reding 	return tegra_output_connector_detect(connector, force);
12276fad8f66SThierry Reding }
12286fad8f66SThierry Reding 
1229c31efa7aSThierry Reding static struct drm_connector_state *
1230c31efa7aSThierry Reding tegra_sor_connector_duplicate_state(struct drm_connector *connector)
1231c31efa7aSThierry Reding {
1232c31efa7aSThierry Reding 	struct tegra_sor_state *state = to_sor_state(connector->state);
1233c31efa7aSThierry Reding 	struct tegra_sor_state *copy;
1234c31efa7aSThierry Reding 
1235c31efa7aSThierry Reding 	copy = kmemdup(state, sizeof(*state), GFP_KERNEL);
1236c31efa7aSThierry Reding 	if (!copy)
1237c31efa7aSThierry Reding 		return NULL;
1238c31efa7aSThierry Reding 
1239c31efa7aSThierry Reding 	__drm_atomic_helper_connector_duplicate_state(connector, &copy->base);
1240c31efa7aSThierry Reding 
1241c31efa7aSThierry Reding 	return &copy->base;
1242c31efa7aSThierry Reding }
1243c31efa7aSThierry Reding 
12446fad8f66SThierry Reding static const struct drm_connector_funcs tegra_sor_connector_funcs = {
1245850bab44SThierry Reding 	.dpms = drm_atomic_helper_connector_dpms,
1246c31efa7aSThierry Reding 	.reset = tegra_sor_connector_reset,
12476fad8f66SThierry Reding 	.detect = tegra_sor_connector_detect,
12486fad8f66SThierry Reding 	.fill_modes = drm_helper_probe_single_connector_modes,
12496fad8f66SThierry Reding 	.destroy = tegra_output_connector_destroy,
1250c31efa7aSThierry Reding 	.atomic_duplicate_state = tegra_sor_connector_duplicate_state,
12514aa3df71SThierry Reding 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
12526fad8f66SThierry Reding };
12536fad8f66SThierry Reding 
12546fad8f66SThierry Reding static int tegra_sor_connector_get_modes(struct drm_connector *connector)
12556fad8f66SThierry Reding {
12566fad8f66SThierry Reding 	struct tegra_output *output = connector_to_output(connector);
12576fad8f66SThierry Reding 	struct tegra_sor *sor = to_sor(output);
12586fad8f66SThierry Reding 	int err;
12596fad8f66SThierry Reding 
12609542c237SThierry Reding 	if (sor->aux)
12619542c237SThierry Reding 		drm_dp_aux_enable(sor->aux);
12626fad8f66SThierry Reding 
12636fad8f66SThierry Reding 	err = tegra_output_connector_get_modes(connector);
12646fad8f66SThierry Reding 
12659542c237SThierry Reding 	if (sor->aux)
12669542c237SThierry Reding 		drm_dp_aux_disable(sor->aux);
12676fad8f66SThierry Reding 
12686fad8f66SThierry Reding 	return err;
12696fad8f66SThierry Reding }
12706fad8f66SThierry Reding 
12716fad8f66SThierry Reding static enum drm_mode_status
12726fad8f66SThierry Reding tegra_sor_connector_mode_valid(struct drm_connector *connector,
12736fad8f66SThierry Reding 			       struct drm_display_mode *mode)
12746fad8f66SThierry Reding {
12756fad8f66SThierry Reding 	return MODE_OK;
12766fad8f66SThierry Reding }
12776fad8f66SThierry Reding 
12786fad8f66SThierry Reding static const struct drm_connector_helper_funcs tegra_sor_connector_helper_funcs = {
12796fad8f66SThierry Reding 	.get_modes = tegra_sor_connector_get_modes,
12806fad8f66SThierry Reding 	.mode_valid = tegra_sor_connector_mode_valid,
12816fad8f66SThierry Reding 	.best_encoder = tegra_output_connector_best_encoder,
12826fad8f66SThierry Reding };
12836fad8f66SThierry Reding 
12846fad8f66SThierry Reding static const struct drm_encoder_funcs tegra_sor_encoder_funcs = {
12856fad8f66SThierry Reding 	.destroy = tegra_output_encoder_destroy,
12866fad8f66SThierry Reding };
12876fad8f66SThierry Reding 
1288850bab44SThierry Reding static void tegra_sor_edp_disable(struct drm_encoder *encoder)
12896fad8f66SThierry Reding {
1290850bab44SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
1291850bab44SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
1292850bab44SThierry Reding 	struct tegra_sor *sor = to_sor(output);
1293850bab44SThierry Reding 	u32 value;
1294850bab44SThierry Reding 	int err;
1295850bab44SThierry Reding 
1296850bab44SThierry Reding 	if (output->panel)
1297850bab44SThierry Reding 		drm_panel_disable(output->panel);
1298850bab44SThierry Reding 
1299850bab44SThierry Reding 	err = tegra_sor_detach(sor);
1300850bab44SThierry Reding 	if (err < 0)
1301850bab44SThierry Reding 		dev_err(sor->dev, "failed to detach SOR: %d\n", err);
1302850bab44SThierry Reding 
1303850bab44SThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE1);
1304850bab44SThierry Reding 	tegra_sor_update(sor);
1305850bab44SThierry Reding 
1306850bab44SThierry Reding 	/*
1307850bab44SThierry Reding 	 * The following accesses registers of the display controller, so make
1308850bab44SThierry Reding 	 * sure it's only executed when the output is attached to one.
1309850bab44SThierry Reding 	 */
1310850bab44SThierry Reding 	if (dc) {
1311850bab44SThierry Reding 		value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
1312850bab44SThierry Reding 		value &= ~SOR_ENABLE;
1313850bab44SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
1314850bab44SThierry Reding 
1315850bab44SThierry Reding 		tegra_dc_commit(dc);
13166fad8f66SThierry Reding 	}
13176fad8f66SThierry Reding 
1318850bab44SThierry Reding 	err = tegra_sor_power_down(sor);
1319850bab44SThierry Reding 	if (err < 0)
1320850bab44SThierry Reding 		dev_err(sor->dev, "failed to power down SOR: %d\n", err);
1321850bab44SThierry Reding 
13229542c237SThierry Reding 	if (sor->aux) {
13239542c237SThierry Reding 		err = drm_dp_aux_disable(sor->aux);
1324850bab44SThierry Reding 		if (err < 0)
1325850bab44SThierry Reding 			dev_err(sor->dev, "failed to disable DP: %d\n", err);
13266fad8f66SThierry Reding 	}
13276fad8f66SThierry Reding 
1328850bab44SThierry Reding 	err = tegra_io_rail_power_off(TEGRA_IO_RAIL_LVDS);
1329850bab44SThierry Reding 	if (err < 0)
1330850bab44SThierry Reding 		dev_err(sor->dev, "failed to power off I/O rail: %d\n", err);
1331850bab44SThierry Reding 
1332850bab44SThierry Reding 	if (output->panel)
1333850bab44SThierry Reding 		drm_panel_unprepare(output->panel);
1334850bab44SThierry Reding 
1335*aaff8bd2SThierry Reding 	pm_runtime_put(sor->dev);
13366fad8f66SThierry Reding }
13376fad8f66SThierry Reding 
1338459cc2c6SThierry Reding #if 0
1339459cc2c6SThierry Reding static int calc_h_ref_to_sync(const struct drm_display_mode *mode,
1340459cc2c6SThierry Reding 			      unsigned int *value)
1341459cc2c6SThierry Reding {
1342459cc2c6SThierry Reding 	unsigned int hfp, hsw, hbp, a = 0, b;
1343459cc2c6SThierry Reding 
1344459cc2c6SThierry Reding 	hfp = mode->hsync_start - mode->hdisplay;
1345459cc2c6SThierry Reding 	hsw = mode->hsync_end - mode->hsync_start;
1346459cc2c6SThierry Reding 	hbp = mode->htotal - mode->hsync_end;
1347459cc2c6SThierry Reding 
1348459cc2c6SThierry Reding 	pr_info("hfp: %u, hsw: %u, hbp: %u\n", hfp, hsw, hbp);
1349459cc2c6SThierry Reding 
1350459cc2c6SThierry Reding 	b = hfp - 1;
1351459cc2c6SThierry Reding 
1352459cc2c6SThierry Reding 	pr_info("a: %u, b: %u\n", a, b);
1353459cc2c6SThierry Reding 	pr_info("a + hsw + hbp = %u\n", a + hsw + hbp);
1354459cc2c6SThierry Reding 
1355459cc2c6SThierry Reding 	if (a + hsw + hbp <= 11) {
1356459cc2c6SThierry Reding 		a = 1 + 11 - hsw - hbp;
1357459cc2c6SThierry Reding 		pr_info("a: %u\n", a);
1358459cc2c6SThierry Reding 	}
1359459cc2c6SThierry Reding 
1360459cc2c6SThierry Reding 	if (a > b)
1361459cc2c6SThierry Reding 		return -EINVAL;
1362459cc2c6SThierry Reding 
1363459cc2c6SThierry Reding 	if (hsw < 1)
1364459cc2c6SThierry Reding 		return -EINVAL;
1365459cc2c6SThierry Reding 
1366459cc2c6SThierry Reding 	if (mode->hdisplay < 16)
1367459cc2c6SThierry Reding 		return -EINVAL;
1368459cc2c6SThierry Reding 
1369459cc2c6SThierry Reding 	if (value) {
1370459cc2c6SThierry Reding 		if (b > a && a % 2)
1371459cc2c6SThierry Reding 			*value = a + 1;
1372459cc2c6SThierry Reding 		else
1373459cc2c6SThierry Reding 			*value = a;
1374459cc2c6SThierry Reding 	}
1375459cc2c6SThierry Reding 
1376459cc2c6SThierry Reding 	return 0;
1377459cc2c6SThierry Reding }
1378459cc2c6SThierry Reding #endif
1379459cc2c6SThierry Reding 
1380850bab44SThierry Reding static void tegra_sor_edp_enable(struct drm_encoder *encoder)
13816fad8f66SThierry Reding {
1382850bab44SThierry Reding 	struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
13836fad8f66SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
13846fad8f66SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
13856b6b6042SThierry Reding 	struct tegra_sor *sor = to_sor(output);
138634fa183bSThierry Reding 	struct tegra_sor_config config;
1387c31efa7aSThierry Reding 	struct tegra_sor_state *state;
138834fa183bSThierry Reding 	struct drm_dp_link link;
138901b9bea0SThierry Reding 	u8 rate, lanes;
13902bd1dd39SThierry Reding 	unsigned int i;
139186f5c52dSThierry Reding 	int err = 0;
139228fe2076SThierry Reding 	u32 value;
139386f5c52dSThierry Reding 
1394c31efa7aSThierry Reding 	state = to_sor_state(output->connector.state);
13952bd1dd39SThierry Reding 
1396*aaff8bd2SThierry Reding 	pm_runtime_get_sync(sor->dev);
13976b6b6042SThierry Reding 
13986fad8f66SThierry Reding 	if (output->panel)
13996fad8f66SThierry Reding 		drm_panel_prepare(output->panel);
14006fad8f66SThierry Reding 
14019542c237SThierry Reding 	err = drm_dp_aux_enable(sor->aux);
14026b6b6042SThierry Reding 	if (err < 0)
14036b6b6042SThierry Reding 		dev_err(sor->dev, "failed to enable DP: %d\n", err);
140434fa183bSThierry Reding 
14059542c237SThierry Reding 	err = drm_dp_link_probe(sor->aux, &link);
140634fa183bSThierry Reding 	if (err < 0) {
140701b9bea0SThierry Reding 		dev_err(sor->dev, "failed to probe eDP link: %d\n", err);
1408850bab44SThierry Reding 		return;
140934fa183bSThierry Reding 	}
14106b6b6042SThierry Reding 
141125bb2cecSThierry Reding 	/* switch to safe parent clock */
141225bb2cecSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
14136b6b6042SThierry Reding 	if (err < 0)
14146b6b6042SThierry Reding 		dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
14156b6b6042SThierry Reding 
141634fa183bSThierry Reding 	memset(&config, 0, sizeof(config));
1417c31efa7aSThierry Reding 	config.bits_per_pixel = state->bpc * 3;
141834fa183bSThierry Reding 
1419a198359eSThierry Reding 	err = tegra_sor_compute_config(sor, mode, &config, &link);
142034fa183bSThierry Reding 	if (err < 0)
1421a198359eSThierry Reding 		dev_err(sor->dev, "failed to compute configuration: %d\n", err);
142234fa183bSThierry Reding 
14236b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
14246b6b6042SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
14256b6b6042SThierry Reding 	value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
14266b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
14276b6b6042SThierry Reding 
1428a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1429a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
1430a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
14316b6b6042SThierry Reding 	usleep_range(20, 100);
14326b6b6042SThierry Reding 
1433a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL3);
1434a9a9e4fdSThierry Reding 	value |= SOR_PLL3_PLL_VDD_MODE_3V3;
1435a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL3);
14366b6b6042SThierry Reding 
1437a9a9e4fdSThierry Reding 	value = SOR_PLL0_ICHPMP(0xf) | SOR_PLL0_VCOCAP_RST |
1438a9a9e4fdSThierry Reding 		SOR_PLL0_PLLREG_LEVEL_V45 | SOR_PLL0_RESISTOR_EXT;
1439a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL0);
14406b6b6042SThierry Reding 
1441a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1442a9a9e4fdSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD;
1443a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1444a9a9e4fdSThierry Reding 	value |= SOR_PLL2_LVDS_ENABLE;
1445a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
14466b6b6042SThierry Reding 
1447a9a9e4fdSThierry Reding 	value = SOR_PLL1_TERM_COMPOUT | SOR_PLL1_TMDS_TERM;
1448a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL1);
14496b6b6042SThierry Reding 
14506b6b6042SThierry Reding 	while (true) {
1451a9a9e4fdSThierry Reding 		value = tegra_sor_readl(sor, SOR_PLL2);
1452a9a9e4fdSThierry Reding 		if ((value & SOR_PLL2_SEQ_PLLCAPPD_ENFORCE) == 0)
14536b6b6042SThierry Reding 			break;
14546b6b6042SThierry Reding 
14556b6b6042SThierry Reding 		usleep_range(250, 1000);
14566b6b6042SThierry Reding 	}
14576b6b6042SThierry Reding 
1458a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1459a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
1460a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_PORT_POWERDOWN;
1461a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
14626b6b6042SThierry Reding 
14636b6b6042SThierry Reding 	/*
14646b6b6042SThierry Reding 	 * power up
14656b6b6042SThierry Reding 	 */
14666b6b6042SThierry Reding 
14676b6b6042SThierry Reding 	/* set safe link bandwidth (1.62 Gbps) */
14686b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
14696b6b6042SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
14706b6b6042SThierry Reding 	value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G1_62;
14716b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
14726b6b6042SThierry Reding 
14736b6b6042SThierry Reding 	/* step 1 */
1474a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1475a9a9e4fdSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE | SOR_PLL2_PORT_POWERDOWN |
1476a9a9e4fdSThierry Reding 		 SOR_PLL2_BANDGAP_POWERDOWN;
1477a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
14786b6b6042SThierry Reding 
1479a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL0);
1480a9a9e4fdSThierry Reding 	value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
1481a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL0);
14826b6b6042SThierry Reding 
1483a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
14846b6b6042SThierry Reding 	value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
1485a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
14866b6b6042SThierry Reding 
14876b6b6042SThierry Reding 	/* step 2 */
14886b6b6042SThierry Reding 	err = tegra_io_rail_power_on(TEGRA_IO_RAIL_LVDS);
1489850bab44SThierry Reding 	if (err < 0)
14906b6b6042SThierry Reding 		dev_err(sor->dev, "failed to power on I/O rail: %d\n", err);
14916b6b6042SThierry Reding 
14926b6b6042SThierry Reding 	usleep_range(5, 100);
14936b6b6042SThierry Reding 
14946b6b6042SThierry Reding 	/* step 3 */
1495a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1496a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
1497a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
14986b6b6042SThierry Reding 
14996b6b6042SThierry Reding 	usleep_range(20, 100);
15006b6b6042SThierry Reding 
15016b6b6042SThierry Reding 	/* step 4 */
1502a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL0);
1503a9a9e4fdSThierry Reding 	value &= ~SOR_PLL0_VCOPD;
1504a9a9e4fdSThierry Reding 	value &= ~SOR_PLL0_PWR;
1505a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL0);
15066b6b6042SThierry Reding 
1507a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1508a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1509a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
15106b6b6042SThierry Reding 
15116b6b6042SThierry Reding 	usleep_range(200, 1000);
15126b6b6042SThierry Reding 
15136b6b6042SThierry Reding 	/* step 5 */
1514a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1515a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_PORT_POWERDOWN;
1516a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
15176b6b6042SThierry Reding 
151825bb2cecSThierry Reding 	/* switch to DP parent clock */
151925bb2cecSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_dp);
15206b6b6042SThierry Reding 	if (err < 0)
152125bb2cecSThierry Reding 		dev_err(sor->dev, "failed to set parent clock: %d\n", err);
15226b6b6042SThierry Reding 
1523899451b7SThierry Reding 	/* power DP lanes */
1524a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
1525899451b7SThierry Reding 
1526899451b7SThierry Reding 	if (link.num_lanes <= 2)
1527899451b7SThierry Reding 		value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2);
1528899451b7SThierry Reding 	else
1529899451b7SThierry Reding 		value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2;
1530899451b7SThierry Reding 
1531899451b7SThierry Reding 	if (link.num_lanes <= 1)
1532899451b7SThierry Reding 		value &= ~SOR_DP_PADCTL_PD_TXD_1;
1533899451b7SThierry Reding 	else
1534899451b7SThierry Reding 		value |= SOR_DP_PADCTL_PD_TXD_1;
1535899451b7SThierry Reding 
1536899451b7SThierry Reding 	if (link.num_lanes == 0)
1537899451b7SThierry Reding 		value &= ~SOR_DP_PADCTL_PD_TXD_0;
1538899451b7SThierry Reding 	else
1539899451b7SThierry Reding 		value |= SOR_DP_PADCTL_PD_TXD_0;
1540899451b7SThierry Reding 
1541a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
15426b6b6042SThierry Reding 
1543a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
15446b6b6042SThierry Reding 	value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
15450c90a184SThierry Reding 	value |= SOR_DP_LINKCTL_LANE_COUNT(link.num_lanes);
1546a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
15476b6b6042SThierry Reding 
15486b6b6042SThierry Reding 	/* start lane sequencer */
15496b6b6042SThierry Reding 	value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
15506b6b6042SThierry Reding 		SOR_LANE_SEQ_CTL_POWER_STATE_UP;
15516b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
15526b6b6042SThierry Reding 
15536b6b6042SThierry Reding 	while (true) {
15546b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
15556b6b6042SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
15566b6b6042SThierry Reding 			break;
15576b6b6042SThierry Reding 
15586b6b6042SThierry Reding 		usleep_range(250, 1000);
15596b6b6042SThierry Reding 	}
15606b6b6042SThierry Reding 
1561a4263fedSThierry Reding 	/* set link bandwidth */
15626b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
15636b6b6042SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
1564a4263fedSThierry Reding 	value |= drm_dp_link_rate_to_bw_code(link.rate) << 2;
15656b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
15666b6b6042SThierry Reding 
1567402f6bcdSThierry Reding 	tegra_sor_apply_config(sor, &config);
1568402f6bcdSThierry Reding 
1569402f6bcdSThierry Reding 	/* enable link */
1570a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
15716b6b6042SThierry Reding 	value |= SOR_DP_LINKCTL_ENABLE;
15726b6b6042SThierry Reding 	value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
1573a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
15746b6b6042SThierry Reding 
15756b6b6042SThierry Reding 	for (i = 0, value = 0; i < 4; i++) {
15766b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
15776b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_GALIOS |
15786b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_NONE;
15796b6b6042SThierry Reding 		value = (value << 8) | lane;
15806b6b6042SThierry Reding 	}
15816b6b6042SThierry Reding 
15826b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
15836b6b6042SThierry Reding 
15846b6b6042SThierry Reding 	/* enable pad calibration logic */
1585a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
15866b6b6042SThierry Reding 	value |= SOR_DP_PADCTL_PAD_CAL_PD;
1587a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
15886b6b6042SThierry Reding 
15899542c237SThierry Reding 	err = drm_dp_link_probe(sor->aux, &link);
1590850bab44SThierry Reding 	if (err < 0)
159101b9bea0SThierry Reding 		dev_err(sor->dev, "failed to probe eDP link: %d\n", err);
15926b6b6042SThierry Reding 
15939542c237SThierry Reding 	err = drm_dp_link_power_up(sor->aux, &link);
1594850bab44SThierry Reding 	if (err < 0)
159501b9bea0SThierry Reding 		dev_err(sor->dev, "failed to power up eDP link: %d\n", err);
15966b6b6042SThierry Reding 
15979542c237SThierry Reding 	err = drm_dp_link_configure(sor->aux, &link);
1598850bab44SThierry Reding 	if (err < 0)
159901b9bea0SThierry Reding 		dev_err(sor->dev, "failed to configure eDP link: %d\n", err);
16006b6b6042SThierry Reding 
16016b6b6042SThierry Reding 	rate = drm_dp_link_rate_to_bw_code(link.rate);
16026b6b6042SThierry Reding 	lanes = link.num_lanes;
16036b6b6042SThierry Reding 
16046b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
16056b6b6042SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
16066b6b6042SThierry Reding 	value |= SOR_CLK_CNTRL_DP_LINK_SPEED(rate);
16076b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
16086b6b6042SThierry Reding 
1609a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
16106b6b6042SThierry Reding 	value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
16116b6b6042SThierry Reding 	value |= SOR_DP_LINKCTL_LANE_COUNT(lanes);
16126b6b6042SThierry Reding 
16136b6b6042SThierry Reding 	if (link.capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
16146b6b6042SThierry Reding 		value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
16156b6b6042SThierry Reding 
1616a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
16176b6b6042SThierry Reding 
16186b6b6042SThierry Reding 	/* disable training pattern generator */
16196b6b6042SThierry Reding 
16206b6b6042SThierry Reding 	for (i = 0; i < link.num_lanes; i++) {
16216b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
16226b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_GALIOS |
16236b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_NONE;
16246b6b6042SThierry Reding 		value = (value << 8) | lane;
16256b6b6042SThierry Reding 	}
16266b6b6042SThierry Reding 
16276b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
16286b6b6042SThierry Reding 
16296b6b6042SThierry Reding 	err = tegra_sor_dp_train_fast(sor, &link);
163001b9bea0SThierry Reding 	if (err < 0)
163101b9bea0SThierry Reding 		dev_err(sor->dev, "DP fast link training failed: %d\n", err);
16326b6b6042SThierry Reding 
16336b6b6042SThierry Reding 	dev_dbg(sor->dev, "fast link training succeeded\n");
16346b6b6042SThierry Reding 
16356b6b6042SThierry Reding 	err = tegra_sor_power_up(sor, 250);
1636850bab44SThierry Reding 	if (err < 0)
16376b6b6042SThierry Reding 		dev_err(sor->dev, "failed to power up SOR: %d\n", err);
16386b6b6042SThierry Reding 
16396b6b6042SThierry Reding 	/* CSTM (LVDS, link A/B, upper) */
1640143b1df2SStéphane Marchesin 	value = SOR_CSTM_LVDS | SOR_CSTM_LINK_ACT_A | SOR_CSTM_LINK_ACT_B |
16416b6b6042SThierry Reding 		SOR_CSTM_UPPER;
16426b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CSTM);
16436b6b6042SThierry Reding 
16442bd1dd39SThierry Reding 	/* use DP-A protocol */
16452bd1dd39SThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
16462bd1dd39SThierry Reding 	value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
16472bd1dd39SThierry Reding 	value |= SOR_STATE_ASY_PROTOCOL_DP_A;
16482bd1dd39SThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
16492bd1dd39SThierry Reding 
1650c31efa7aSThierry Reding 	tegra_sor_mode_set(sor, mode, state);
16512bd1dd39SThierry Reding 
16526b6b6042SThierry Reding 	/* PWM setup */
16536b6b6042SThierry Reding 	err = tegra_sor_setup_pwm(sor, 250);
1654850bab44SThierry Reding 	if (err < 0)
16556b6b6042SThierry Reding 		dev_err(sor->dev, "failed to setup PWM: %d\n", err);
16566b6b6042SThierry Reding 
1657666cb873SThierry Reding 	tegra_sor_update(sor);
1658666cb873SThierry Reding 
16596b6b6042SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
16606b6b6042SThierry Reding 	value |= SOR_ENABLE;
16616b6b6042SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
16626b6b6042SThierry Reding 
1663666cb873SThierry Reding 	tegra_dc_commit(dc);
16646b6b6042SThierry Reding 
16656b6b6042SThierry Reding 	err = tegra_sor_attach(sor);
1666850bab44SThierry Reding 	if (err < 0)
16676b6b6042SThierry Reding 		dev_err(sor->dev, "failed to attach SOR: %d\n", err);
16686b6b6042SThierry Reding 
16696b6b6042SThierry Reding 	err = tegra_sor_wakeup(sor);
1670850bab44SThierry Reding 	if (err < 0)
16716b6b6042SThierry Reding 		dev_err(sor->dev, "failed to enable DC: %d\n", err);
16726b6b6042SThierry Reding 
16736fad8f66SThierry Reding 	if (output->panel)
16746fad8f66SThierry Reding 		drm_panel_enable(output->panel);
16756b6b6042SThierry Reding }
16766b6b6042SThierry Reding 
167782f1511cSThierry Reding static int
167882f1511cSThierry Reding tegra_sor_encoder_atomic_check(struct drm_encoder *encoder,
167982f1511cSThierry Reding 			       struct drm_crtc_state *crtc_state,
168082f1511cSThierry Reding 			       struct drm_connector_state *conn_state)
168182f1511cSThierry Reding {
168282f1511cSThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
1683c31efa7aSThierry Reding 	struct tegra_sor_state *state = to_sor_state(conn_state);
168482f1511cSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(conn_state->crtc);
168582f1511cSThierry Reding 	unsigned long pclk = crtc_state->mode.clock * 1000;
168682f1511cSThierry Reding 	struct tegra_sor *sor = to_sor(output);
1687c31efa7aSThierry Reding 	struct drm_display_info *info;
168882f1511cSThierry Reding 	int err;
168982f1511cSThierry Reding 
1690c31efa7aSThierry Reding 	info = &output->connector.display_info;
1691c31efa7aSThierry Reding 
169282f1511cSThierry Reding 	err = tegra_dc_state_setup_clock(dc, crtc_state, sor->clk_parent,
169382f1511cSThierry Reding 					 pclk, 0);
169482f1511cSThierry Reding 	if (err < 0) {
169582f1511cSThierry Reding 		dev_err(output->dev, "failed to setup CRTC state: %d\n", err);
169682f1511cSThierry Reding 		return err;
169782f1511cSThierry Reding 	}
169882f1511cSThierry Reding 
1699c31efa7aSThierry Reding 	switch (info->bpc) {
1700c31efa7aSThierry Reding 	case 8:
1701c31efa7aSThierry Reding 	case 6:
1702c31efa7aSThierry Reding 		state->bpc = info->bpc;
1703c31efa7aSThierry Reding 		break;
1704c31efa7aSThierry Reding 
1705c31efa7aSThierry Reding 	default:
1706c31efa7aSThierry Reding 		DRM_DEBUG_KMS("%u bits-per-color not supported\n", info->bpc);
1707c31efa7aSThierry Reding 		state->bpc = 8;
1708c31efa7aSThierry Reding 		break;
1709c31efa7aSThierry Reding 	}
1710c31efa7aSThierry Reding 
171182f1511cSThierry Reding 	return 0;
171282f1511cSThierry Reding }
171382f1511cSThierry Reding 
1714459cc2c6SThierry Reding static const struct drm_encoder_helper_funcs tegra_sor_edp_helpers = {
1715850bab44SThierry Reding 	.disable = tegra_sor_edp_disable,
1716850bab44SThierry Reding 	.enable = tegra_sor_edp_enable,
171782f1511cSThierry Reding 	.atomic_check = tegra_sor_encoder_atomic_check,
17186b6b6042SThierry Reding };
17196b6b6042SThierry Reding 
1720459cc2c6SThierry Reding static inline u32 tegra_sor_hdmi_subpack(const u8 *ptr, size_t size)
1721459cc2c6SThierry Reding {
1722459cc2c6SThierry Reding 	u32 value = 0;
1723459cc2c6SThierry Reding 	size_t i;
1724459cc2c6SThierry Reding 
1725459cc2c6SThierry Reding 	for (i = size; i > 0; i--)
1726459cc2c6SThierry Reding 		value = (value << 8) | ptr[i - 1];
1727459cc2c6SThierry Reding 
1728459cc2c6SThierry Reding 	return value;
1729459cc2c6SThierry Reding }
1730459cc2c6SThierry Reding 
1731459cc2c6SThierry Reding static void tegra_sor_hdmi_write_infopack(struct tegra_sor *sor,
1732459cc2c6SThierry Reding 					  const void *data, size_t size)
1733459cc2c6SThierry Reding {
1734459cc2c6SThierry Reding 	const u8 *ptr = data;
1735459cc2c6SThierry Reding 	unsigned long offset;
1736459cc2c6SThierry Reding 	size_t i, j;
1737459cc2c6SThierry Reding 	u32 value;
1738459cc2c6SThierry Reding 
1739459cc2c6SThierry Reding 	switch (ptr[0]) {
1740459cc2c6SThierry Reding 	case HDMI_INFOFRAME_TYPE_AVI:
1741459cc2c6SThierry Reding 		offset = SOR_HDMI_AVI_INFOFRAME_HEADER;
1742459cc2c6SThierry Reding 		break;
1743459cc2c6SThierry Reding 
1744459cc2c6SThierry Reding 	case HDMI_INFOFRAME_TYPE_AUDIO:
1745459cc2c6SThierry Reding 		offset = SOR_HDMI_AUDIO_INFOFRAME_HEADER;
1746459cc2c6SThierry Reding 		break;
1747459cc2c6SThierry Reding 
1748459cc2c6SThierry Reding 	case HDMI_INFOFRAME_TYPE_VENDOR:
1749459cc2c6SThierry Reding 		offset = SOR_HDMI_VSI_INFOFRAME_HEADER;
1750459cc2c6SThierry Reding 		break;
1751459cc2c6SThierry Reding 
1752459cc2c6SThierry Reding 	default:
1753459cc2c6SThierry Reding 		dev_err(sor->dev, "unsupported infoframe type: %02x\n",
1754459cc2c6SThierry Reding 			ptr[0]);
1755459cc2c6SThierry Reding 		return;
1756459cc2c6SThierry Reding 	}
1757459cc2c6SThierry Reding 
1758459cc2c6SThierry Reding 	value = INFOFRAME_HEADER_TYPE(ptr[0]) |
1759459cc2c6SThierry Reding 		INFOFRAME_HEADER_VERSION(ptr[1]) |
1760459cc2c6SThierry Reding 		INFOFRAME_HEADER_LEN(ptr[2]);
1761459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, offset);
1762459cc2c6SThierry Reding 	offset++;
1763459cc2c6SThierry Reding 
1764459cc2c6SThierry Reding 	/*
1765459cc2c6SThierry Reding 	 * Each subpack contains 7 bytes, divided into:
1766459cc2c6SThierry Reding 	 * - subpack_low: bytes 0 - 3
1767459cc2c6SThierry Reding 	 * - subpack_high: bytes 4 - 6 (with byte 7 padded to 0x00)
1768459cc2c6SThierry Reding 	 */
1769459cc2c6SThierry Reding 	for (i = 3, j = 0; i < size; i += 7, j += 8) {
1770459cc2c6SThierry Reding 		size_t rem = size - i, num = min_t(size_t, rem, 4);
1771459cc2c6SThierry Reding 
1772459cc2c6SThierry Reding 		value = tegra_sor_hdmi_subpack(&ptr[i], num);
1773459cc2c6SThierry Reding 		tegra_sor_writel(sor, value, offset++);
1774459cc2c6SThierry Reding 
1775459cc2c6SThierry Reding 		num = min_t(size_t, rem - num, 3);
1776459cc2c6SThierry Reding 
1777459cc2c6SThierry Reding 		value = tegra_sor_hdmi_subpack(&ptr[i + 4], num);
1778459cc2c6SThierry Reding 		tegra_sor_writel(sor, value, offset++);
1779459cc2c6SThierry Reding 	}
1780459cc2c6SThierry Reding }
1781459cc2c6SThierry Reding 
1782459cc2c6SThierry Reding static int
1783459cc2c6SThierry Reding tegra_sor_hdmi_setup_avi_infoframe(struct tegra_sor *sor,
1784459cc2c6SThierry Reding 				   const struct drm_display_mode *mode)
1785459cc2c6SThierry Reding {
1786459cc2c6SThierry Reding 	u8 buffer[HDMI_INFOFRAME_SIZE(AVI)];
1787459cc2c6SThierry Reding 	struct hdmi_avi_infoframe frame;
1788459cc2c6SThierry Reding 	u32 value;
1789459cc2c6SThierry Reding 	int err;
1790459cc2c6SThierry Reding 
1791459cc2c6SThierry Reding 	/* disable AVI infoframe */
1792459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI_AVI_INFOFRAME_CTRL);
1793459cc2c6SThierry Reding 	value &= ~INFOFRAME_CTRL_SINGLE;
1794459cc2c6SThierry Reding 	value &= ~INFOFRAME_CTRL_OTHER;
1795459cc2c6SThierry Reding 	value &= ~INFOFRAME_CTRL_ENABLE;
1796459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_AVI_INFOFRAME_CTRL);
1797459cc2c6SThierry Reding 
1798459cc2c6SThierry Reding 	err = drm_hdmi_avi_infoframe_from_display_mode(&frame, mode);
1799459cc2c6SThierry Reding 	if (err < 0) {
1800459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to setup AVI infoframe: %d\n", err);
1801459cc2c6SThierry Reding 		return err;
1802459cc2c6SThierry Reding 	}
1803459cc2c6SThierry Reding 
1804459cc2c6SThierry Reding 	err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
1805459cc2c6SThierry Reding 	if (err < 0) {
1806459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to pack AVI infoframe: %d\n", err);
1807459cc2c6SThierry Reding 		return err;
1808459cc2c6SThierry Reding 	}
1809459cc2c6SThierry Reding 
1810459cc2c6SThierry Reding 	tegra_sor_hdmi_write_infopack(sor, buffer, err);
1811459cc2c6SThierry Reding 
1812459cc2c6SThierry Reding 	/* enable AVI infoframe */
1813459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI_AVI_INFOFRAME_CTRL);
1814459cc2c6SThierry Reding 	value |= INFOFRAME_CTRL_CHECKSUM_ENABLE;
1815459cc2c6SThierry Reding 	value |= INFOFRAME_CTRL_ENABLE;
1816459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_AVI_INFOFRAME_CTRL);
1817459cc2c6SThierry Reding 
1818459cc2c6SThierry Reding 	return 0;
1819459cc2c6SThierry Reding }
1820459cc2c6SThierry Reding 
1821459cc2c6SThierry Reding static void tegra_sor_hdmi_disable_audio_infoframe(struct tegra_sor *sor)
1822459cc2c6SThierry Reding {
1823459cc2c6SThierry Reding 	u32 value;
1824459cc2c6SThierry Reding 
1825459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
1826459cc2c6SThierry Reding 	value &= ~INFOFRAME_CTRL_ENABLE;
1827459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
1828459cc2c6SThierry Reding }
1829459cc2c6SThierry Reding 
1830459cc2c6SThierry Reding static struct tegra_sor_hdmi_settings *
1831459cc2c6SThierry Reding tegra_sor_hdmi_find_settings(struct tegra_sor *sor, unsigned long frequency)
1832459cc2c6SThierry Reding {
1833459cc2c6SThierry Reding 	unsigned int i;
1834459cc2c6SThierry Reding 
1835459cc2c6SThierry Reding 	for (i = 0; i < sor->num_settings; i++)
1836459cc2c6SThierry Reding 		if (frequency <= sor->settings[i].frequency)
1837459cc2c6SThierry Reding 			return &sor->settings[i];
1838459cc2c6SThierry Reding 
1839459cc2c6SThierry Reding 	return NULL;
1840459cc2c6SThierry Reding }
1841459cc2c6SThierry Reding 
1842459cc2c6SThierry Reding static void tegra_sor_hdmi_disable(struct drm_encoder *encoder)
1843459cc2c6SThierry Reding {
1844459cc2c6SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
1845459cc2c6SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
1846459cc2c6SThierry Reding 	struct tegra_sor *sor = to_sor(output);
1847459cc2c6SThierry Reding 	u32 value;
1848459cc2c6SThierry Reding 	int err;
1849459cc2c6SThierry Reding 
1850459cc2c6SThierry Reding 	err = tegra_sor_detach(sor);
1851459cc2c6SThierry Reding 	if (err < 0)
1852459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to detach SOR: %d\n", err);
1853459cc2c6SThierry Reding 
1854459cc2c6SThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE1);
1855459cc2c6SThierry Reding 	tegra_sor_update(sor);
1856459cc2c6SThierry Reding 
1857459cc2c6SThierry Reding 	/* disable display to SOR clock */
1858459cc2c6SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
1859459cc2c6SThierry Reding 	value &= ~SOR1_TIMING_CYA;
1860459cc2c6SThierry Reding 	value &= ~SOR1_ENABLE;
1861459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
1862459cc2c6SThierry Reding 
1863459cc2c6SThierry Reding 	tegra_dc_commit(dc);
1864459cc2c6SThierry Reding 
1865459cc2c6SThierry Reding 	err = tegra_sor_power_down(sor);
1866459cc2c6SThierry Reding 	if (err < 0)
1867459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to power down SOR: %d\n", err);
1868459cc2c6SThierry Reding 
1869459cc2c6SThierry Reding 	err = tegra_io_rail_power_off(TEGRA_IO_RAIL_HDMI);
1870459cc2c6SThierry Reding 	if (err < 0)
1871459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to power off HDMI rail: %d\n", err);
1872459cc2c6SThierry Reding 
1873*aaff8bd2SThierry Reding 	pm_runtime_put(sor->dev);
1874459cc2c6SThierry Reding }
1875459cc2c6SThierry Reding 
1876459cc2c6SThierry Reding static void tegra_sor_hdmi_enable(struct drm_encoder *encoder)
1877459cc2c6SThierry Reding {
1878459cc2c6SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
1879459cc2c6SThierry Reding 	unsigned int h_ref_to_sync = 1, pulse_start, max_ac;
1880459cc2c6SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
1881459cc2c6SThierry Reding 	struct tegra_sor_hdmi_settings *settings;
1882459cc2c6SThierry Reding 	struct tegra_sor *sor = to_sor(output);
1883c31efa7aSThierry Reding 	struct tegra_sor_state *state;
1884459cc2c6SThierry Reding 	struct drm_display_mode *mode;
18852bd1dd39SThierry Reding 	unsigned int div;
1886459cc2c6SThierry Reding 	u32 value;
1887459cc2c6SThierry Reding 	int err;
1888459cc2c6SThierry Reding 
1889c31efa7aSThierry Reding 	state = to_sor_state(output->connector.state);
1890459cc2c6SThierry Reding 	mode = &encoder->crtc->state->adjusted_mode;
1891459cc2c6SThierry Reding 
1892*aaff8bd2SThierry Reding 	pm_runtime_get_sync(sor->dev);
1893459cc2c6SThierry Reding 
189425bb2cecSThierry Reding 	/* switch to safe parent clock */
189525bb2cecSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
1896459cc2c6SThierry Reding 	if (err < 0)
1897459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
1898459cc2c6SThierry Reding 
1899459cc2c6SThierry Reding 	div = clk_get_rate(sor->clk) / 1000000 * 4;
1900459cc2c6SThierry Reding 
1901459cc2c6SThierry Reding 	err = tegra_io_rail_power_on(TEGRA_IO_RAIL_HDMI);
1902459cc2c6SThierry Reding 	if (err < 0)
1903459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to power on HDMI rail: %d\n", err);
1904459cc2c6SThierry Reding 
1905459cc2c6SThierry Reding 	usleep_range(20, 100);
1906459cc2c6SThierry Reding 
1907459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1908459cc2c6SThierry Reding 	value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
1909459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
1910459cc2c6SThierry Reding 
1911459cc2c6SThierry Reding 	usleep_range(20, 100);
1912459cc2c6SThierry Reding 
1913459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL3);
1914459cc2c6SThierry Reding 	value &= ~SOR_PLL3_PLL_VDD_MODE_3V3;
1915459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL3);
1916459cc2c6SThierry Reding 
1917459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL0);
1918459cc2c6SThierry Reding 	value &= ~SOR_PLL0_VCOPD;
1919459cc2c6SThierry Reding 	value &= ~SOR_PLL0_PWR;
1920459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL0);
1921459cc2c6SThierry Reding 
1922459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1923459cc2c6SThierry Reding 	value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1924459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
1925459cc2c6SThierry Reding 
1926459cc2c6SThierry Reding 	usleep_range(200, 400);
1927459cc2c6SThierry Reding 
1928459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1929459cc2c6SThierry Reding 	value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
1930459cc2c6SThierry Reding 	value &= ~SOR_PLL2_PORT_POWERDOWN;
1931459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
1932459cc2c6SThierry Reding 
1933459cc2c6SThierry Reding 	usleep_range(20, 100);
1934459cc2c6SThierry Reding 
1935459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
1936459cc2c6SThierry Reding 	value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
1937459cc2c6SThierry Reding 		 SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2;
1938459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
1939459cc2c6SThierry Reding 
1940459cc2c6SThierry Reding 	while (true) {
1941459cc2c6SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
1942459cc2c6SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_STATE_BUSY) == 0)
1943459cc2c6SThierry Reding 			break;
1944459cc2c6SThierry Reding 
1945459cc2c6SThierry Reding 		usleep_range(250, 1000);
1946459cc2c6SThierry Reding 	}
1947459cc2c6SThierry Reding 
1948459cc2c6SThierry Reding 	value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
1949459cc2c6SThierry Reding 		SOR_LANE_SEQ_CTL_POWER_STATE_UP | SOR_LANE_SEQ_CTL_DELAY(5);
1950459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
1951459cc2c6SThierry Reding 
1952459cc2c6SThierry Reding 	while (true) {
1953459cc2c6SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
1954459cc2c6SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
1955459cc2c6SThierry Reding 			break;
1956459cc2c6SThierry Reding 
1957459cc2c6SThierry Reding 		usleep_range(250, 1000);
1958459cc2c6SThierry Reding 	}
1959459cc2c6SThierry Reding 
1960459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
1961459cc2c6SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
1962459cc2c6SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
1963459cc2c6SThierry Reding 
1964459cc2c6SThierry Reding 	if (mode->clock < 340000)
1965459cc2c6SThierry Reding 		value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G2_70;
1966459cc2c6SThierry Reding 	else
1967459cc2c6SThierry Reding 		value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G5_40;
1968459cc2c6SThierry Reding 
1969459cc2c6SThierry Reding 	value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK;
1970459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
1971459cc2c6SThierry Reding 
1972459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_SPARE0);
1973459cc2c6SThierry Reding 	value |= SOR_DP_SPARE_DISP_VIDEO_PREAMBLE;
1974459cc2c6SThierry Reding 	value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
1975459cc2c6SThierry Reding 	value |= SOR_DP_SPARE_SEQ_ENABLE;
1976459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_SPARE0);
1977459cc2c6SThierry Reding 
1978459cc2c6SThierry Reding 	value = SOR_SEQ_CTL_PU_PC(0) | SOR_SEQ_CTL_PU_PC_ALT(0) |
1979459cc2c6SThierry Reding 		SOR_SEQ_CTL_PD_PC(8) | SOR_SEQ_CTL_PD_PC_ALT(8);
1980459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_SEQ_CTL);
1981459cc2c6SThierry Reding 
1982459cc2c6SThierry Reding 	value = SOR_SEQ_INST_DRIVE_PWM_OUT_LO | SOR_SEQ_INST_HALT |
1983459cc2c6SThierry Reding 		SOR_SEQ_INST_WAIT_VSYNC | SOR_SEQ_INST_WAIT(1);
1984459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_SEQ_INST(0));
1985459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_SEQ_INST(8));
1986459cc2c6SThierry Reding 
1987459cc2c6SThierry Reding 	/* program the reference clock */
1988459cc2c6SThierry Reding 	value = SOR_REFCLK_DIV_INT(div) | SOR_REFCLK_DIV_FRAC(div);
1989459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_REFCLK);
1990459cc2c6SThierry Reding 
1991459cc2c6SThierry Reding 	/* XXX don't hardcode */
1992459cc2c6SThierry Reding 	value = SOR_XBAR_CTRL_LINK1_XSEL(4, 4) |
1993459cc2c6SThierry Reding 		SOR_XBAR_CTRL_LINK1_XSEL(3, 3) |
1994459cc2c6SThierry Reding 		SOR_XBAR_CTRL_LINK1_XSEL(2, 2) |
1995459cc2c6SThierry Reding 		SOR_XBAR_CTRL_LINK1_XSEL(1, 1) |
1996459cc2c6SThierry Reding 		SOR_XBAR_CTRL_LINK1_XSEL(0, 0) |
1997459cc2c6SThierry Reding 		SOR_XBAR_CTRL_LINK0_XSEL(4, 4) |
1998459cc2c6SThierry Reding 		SOR_XBAR_CTRL_LINK0_XSEL(3, 3) |
1999459cc2c6SThierry Reding 		SOR_XBAR_CTRL_LINK0_XSEL(2, 0) |
2000459cc2c6SThierry Reding 		SOR_XBAR_CTRL_LINK0_XSEL(1, 1) |
2001459cc2c6SThierry Reding 		SOR_XBAR_CTRL_LINK0_XSEL(0, 2);
2002459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_XBAR_CTRL);
2003459cc2c6SThierry Reding 
2004459cc2c6SThierry Reding 	tegra_sor_writel(sor, 0x00000000, SOR_XBAR_POL);
2005459cc2c6SThierry Reding 
200625bb2cecSThierry Reding 	/* switch to parent clock */
200725bb2cecSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_parent);
2008459cc2c6SThierry Reding 	if (err < 0)
2009459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to set parent clock: %d\n", err);
2010459cc2c6SThierry Reding 
2011459cc2c6SThierry Reding 	value = SOR_INPUT_CONTROL_HDMI_SRC_SELECT(dc->pipe);
2012459cc2c6SThierry Reding 
2013459cc2c6SThierry Reding 	/* XXX is this the proper check? */
2014459cc2c6SThierry Reding 	if (mode->clock < 75000)
2015459cc2c6SThierry Reding 		value |= SOR_INPUT_CONTROL_ARM_VIDEO_RANGE_LIMITED;
2016459cc2c6SThierry Reding 
2017459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_INPUT_CONTROL);
2018459cc2c6SThierry Reding 
2019459cc2c6SThierry Reding 	max_ac = ((mode->htotal - mode->hdisplay) - SOR_REKEY - 18) / 32;
2020459cc2c6SThierry Reding 
2021459cc2c6SThierry Reding 	value = SOR_HDMI_CTRL_ENABLE | SOR_HDMI_CTRL_MAX_AC_PACKET(max_ac) |
2022459cc2c6SThierry Reding 		SOR_HDMI_CTRL_AUDIO_LAYOUT | SOR_HDMI_CTRL_REKEY(SOR_REKEY);
2023459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_CTRL);
2024459cc2c6SThierry Reding 
2025459cc2c6SThierry Reding 	/* H_PULSE2 setup */
2026459cc2c6SThierry Reding 	pulse_start = h_ref_to_sync + (mode->hsync_end - mode->hsync_start) +
2027459cc2c6SThierry Reding 		      (mode->htotal - mode->hsync_end) - 10;
2028459cc2c6SThierry Reding 
2029459cc2c6SThierry Reding 	value = PULSE_LAST_END_A | PULSE_QUAL_VACTIVE |
2030459cc2c6SThierry Reding 		PULSE_POLARITY_HIGH | PULSE_MODE_NORMAL;
2031459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_CONTROL);
2032459cc2c6SThierry Reding 
2033459cc2c6SThierry Reding 	value = PULSE_END(pulse_start + 8) | PULSE_START(pulse_start);
2034459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_POSITION_A);
2035459cc2c6SThierry Reding 
2036459cc2c6SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_SIGNAL_OPTIONS0);
2037459cc2c6SThierry Reding 	value |= H_PULSE2_ENABLE;
2038459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_SIGNAL_OPTIONS0);
2039459cc2c6SThierry Reding 
2040459cc2c6SThierry Reding 	/* infoframe setup */
2041459cc2c6SThierry Reding 	err = tegra_sor_hdmi_setup_avi_infoframe(sor, mode);
2042459cc2c6SThierry Reding 	if (err < 0)
2043459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to setup AVI infoframe: %d\n", err);
2044459cc2c6SThierry Reding 
2045459cc2c6SThierry Reding 	/* XXX HDMI audio support not implemented yet */
2046459cc2c6SThierry Reding 	tegra_sor_hdmi_disable_audio_infoframe(sor);
2047459cc2c6SThierry Reding 
2048459cc2c6SThierry Reding 	/* use single TMDS protocol */
2049459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
2050459cc2c6SThierry Reding 	value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
2051459cc2c6SThierry Reding 	value |= SOR_STATE_ASY_PROTOCOL_SINGLE_TMDS_A;
2052459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
2053459cc2c6SThierry Reding 
2054459cc2c6SThierry Reding 	/* power up pad calibration */
2055459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
2056459cc2c6SThierry Reding 	value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
2057459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
2058459cc2c6SThierry Reding 
2059459cc2c6SThierry Reding 	/* production settings */
2060459cc2c6SThierry Reding 	settings = tegra_sor_hdmi_find_settings(sor, mode->clock * 1000);
2061db8b42fbSDan Carpenter 	if (!settings) {
2062db8b42fbSDan Carpenter 		dev_err(sor->dev, "no settings for pixel clock %d Hz\n",
2063db8b42fbSDan Carpenter 			mode->clock * 1000);
2064459cc2c6SThierry Reding 		return;
2065459cc2c6SThierry Reding 	}
2066459cc2c6SThierry Reding 
2067459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL0);
2068459cc2c6SThierry Reding 	value &= ~SOR_PLL0_ICHPMP_MASK;
2069459cc2c6SThierry Reding 	value &= ~SOR_PLL0_VCOCAP_MASK;
2070459cc2c6SThierry Reding 	value |= SOR_PLL0_ICHPMP(settings->ichpmp);
2071459cc2c6SThierry Reding 	value |= SOR_PLL0_VCOCAP(settings->vcocap);
2072459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL0);
2073459cc2c6SThierry Reding 
2074459cc2c6SThierry Reding 	tegra_sor_dp_term_calibrate(sor);
2075459cc2c6SThierry Reding 
2076459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL1);
2077459cc2c6SThierry Reding 	value &= ~SOR_PLL1_LOADADJ_MASK;
2078459cc2c6SThierry Reding 	value |= SOR_PLL1_LOADADJ(settings->loadadj);
2079459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL1);
2080459cc2c6SThierry Reding 
2081459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL3);
2082459cc2c6SThierry Reding 	value &= ~SOR_PLL3_BG_VREF_LEVEL_MASK;
2083459cc2c6SThierry Reding 	value |= SOR_PLL3_BG_VREF_LEVEL(settings->bg_vref);
2084459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL3);
2085459cc2c6SThierry Reding 
2086459cc2c6SThierry Reding 	value = settings->drive_current[0] << 24 |
2087459cc2c6SThierry Reding 		settings->drive_current[1] << 16 |
2088459cc2c6SThierry Reding 		settings->drive_current[2] <<  8 |
2089459cc2c6SThierry Reding 		settings->drive_current[3] <<  0;
2090459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT0);
2091459cc2c6SThierry Reding 
2092459cc2c6SThierry Reding 	value = settings->preemphasis[0] << 24 |
2093459cc2c6SThierry Reding 		settings->preemphasis[1] << 16 |
2094459cc2c6SThierry Reding 		settings->preemphasis[2] <<  8 |
2095459cc2c6SThierry Reding 		settings->preemphasis[3] <<  0;
2096459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS0);
2097459cc2c6SThierry Reding 
2098459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
2099459cc2c6SThierry Reding 	value &= ~SOR_DP_PADCTL_TX_PU_MASK;
2100459cc2c6SThierry Reding 	value |= SOR_DP_PADCTL_TX_PU_ENABLE;
2101459cc2c6SThierry Reding 	value |= SOR_DP_PADCTL_TX_PU(settings->tx_pu);
2102459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
2103459cc2c6SThierry Reding 
2104459cc2c6SThierry Reding 	/* power down pad calibration */
2105459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
2106459cc2c6SThierry Reding 	value |= SOR_DP_PADCTL_PAD_CAL_PD;
2107459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
2108459cc2c6SThierry Reding 
2109459cc2c6SThierry Reding 	/* miscellaneous display controller settings */
2110459cc2c6SThierry Reding 	value = VSYNC_H_POSITION(1);
2111459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_TIMING_OPTIONS);
2112459cc2c6SThierry Reding 
2113459cc2c6SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_COLOR_CONTROL);
2114459cc2c6SThierry Reding 	value &= ~DITHER_CONTROL_MASK;
2115459cc2c6SThierry Reding 	value &= ~BASE_COLOR_SIZE_MASK;
2116459cc2c6SThierry Reding 
2117c31efa7aSThierry Reding 	switch (state->bpc) {
2118459cc2c6SThierry Reding 	case 6:
2119459cc2c6SThierry Reding 		value |= BASE_COLOR_SIZE_666;
2120459cc2c6SThierry Reding 		break;
2121459cc2c6SThierry Reding 
2122459cc2c6SThierry Reding 	case 8:
2123459cc2c6SThierry Reding 		value |= BASE_COLOR_SIZE_888;
2124459cc2c6SThierry Reding 		break;
2125459cc2c6SThierry Reding 
2126459cc2c6SThierry Reding 	default:
2127c31efa7aSThierry Reding 		WARN(1, "%u bits-per-color not supported\n", state->bpc);
2128c31efa7aSThierry Reding 		value |= BASE_COLOR_SIZE_888;
2129459cc2c6SThierry Reding 		break;
2130459cc2c6SThierry Reding 	}
2131459cc2c6SThierry Reding 
2132459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_COLOR_CONTROL);
2133459cc2c6SThierry Reding 
2134459cc2c6SThierry Reding 	err = tegra_sor_power_up(sor, 250);
2135459cc2c6SThierry Reding 	if (err < 0)
2136459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to power up SOR: %d\n", err);
2137459cc2c6SThierry Reding 
21382bd1dd39SThierry Reding 	/* configure dynamic range of output */
2139459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_HEAD_STATE0(dc->pipe));
2140459cc2c6SThierry Reding 	value &= ~SOR_HEAD_STATE_RANGECOMPRESS_MASK;
2141459cc2c6SThierry Reding 	value &= ~SOR_HEAD_STATE_DYNRANGE_MASK;
2142459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HEAD_STATE0(dc->pipe));
2143459cc2c6SThierry Reding 
21442bd1dd39SThierry Reding 	/* configure colorspace */
2145459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_HEAD_STATE0(dc->pipe));
2146459cc2c6SThierry Reding 	value &= ~SOR_HEAD_STATE_COLORSPACE_MASK;
2147459cc2c6SThierry Reding 	value |= SOR_HEAD_STATE_COLORSPACE_RGB;
2148459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HEAD_STATE0(dc->pipe));
2149459cc2c6SThierry Reding 
2150c31efa7aSThierry Reding 	tegra_sor_mode_set(sor, mode, state);
2151459cc2c6SThierry Reding 
2152459cc2c6SThierry Reding 	tegra_sor_update(sor);
2153459cc2c6SThierry Reding 
2154459cc2c6SThierry Reding 	err = tegra_sor_attach(sor);
2155459cc2c6SThierry Reding 	if (err < 0)
2156459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to attach SOR: %d\n", err);
2157459cc2c6SThierry Reding 
2158459cc2c6SThierry Reding 	/* enable display to SOR clock and generate HDMI preamble */
2159459cc2c6SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
2160459cc2c6SThierry Reding 	value |= SOR1_ENABLE | SOR1_TIMING_CYA;
2161459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
2162459cc2c6SThierry Reding 
2163459cc2c6SThierry Reding 	tegra_dc_commit(dc);
2164459cc2c6SThierry Reding 
2165459cc2c6SThierry Reding 	err = tegra_sor_wakeup(sor);
2166459cc2c6SThierry Reding 	if (err < 0)
2167459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to wakeup SOR: %d\n", err);
2168459cc2c6SThierry Reding }
2169459cc2c6SThierry Reding 
2170459cc2c6SThierry Reding static const struct drm_encoder_helper_funcs tegra_sor_hdmi_helpers = {
2171459cc2c6SThierry Reding 	.disable = tegra_sor_hdmi_disable,
2172459cc2c6SThierry Reding 	.enable = tegra_sor_hdmi_enable,
2173459cc2c6SThierry Reding 	.atomic_check = tegra_sor_encoder_atomic_check,
2174459cc2c6SThierry Reding };
2175459cc2c6SThierry Reding 
21766b6b6042SThierry Reding static int tegra_sor_init(struct host1x_client *client)
21776b6b6042SThierry Reding {
21789910f5c4SThierry Reding 	struct drm_device *drm = dev_get_drvdata(client->parent);
2179459cc2c6SThierry Reding 	const struct drm_encoder_helper_funcs *helpers = NULL;
21806b6b6042SThierry Reding 	struct tegra_sor *sor = host1x_client_to_sor(client);
2181459cc2c6SThierry Reding 	int connector = DRM_MODE_CONNECTOR_Unknown;
2182459cc2c6SThierry Reding 	int encoder = DRM_MODE_ENCODER_NONE;
21836b6b6042SThierry Reding 	int err;
21846b6b6042SThierry Reding 
21859542c237SThierry Reding 	if (!sor->aux) {
2186459cc2c6SThierry Reding 		if (sor->soc->supports_hdmi) {
2187459cc2c6SThierry Reding 			connector = DRM_MODE_CONNECTOR_HDMIA;
2188459cc2c6SThierry Reding 			encoder = DRM_MODE_ENCODER_TMDS;
2189459cc2c6SThierry Reding 			helpers = &tegra_sor_hdmi_helpers;
2190459cc2c6SThierry Reding 		} else if (sor->soc->supports_lvds) {
2191459cc2c6SThierry Reding 			connector = DRM_MODE_CONNECTOR_LVDS;
2192459cc2c6SThierry Reding 			encoder = DRM_MODE_ENCODER_LVDS;
2193459cc2c6SThierry Reding 		}
2194459cc2c6SThierry Reding 	} else {
2195459cc2c6SThierry Reding 		if (sor->soc->supports_edp) {
2196459cc2c6SThierry Reding 			connector = DRM_MODE_CONNECTOR_eDP;
2197459cc2c6SThierry Reding 			encoder = DRM_MODE_ENCODER_TMDS;
2198459cc2c6SThierry Reding 			helpers = &tegra_sor_edp_helpers;
2199459cc2c6SThierry Reding 		} else if (sor->soc->supports_dp) {
2200459cc2c6SThierry Reding 			connector = DRM_MODE_CONNECTOR_DisplayPort;
2201459cc2c6SThierry Reding 			encoder = DRM_MODE_ENCODER_TMDS;
2202459cc2c6SThierry Reding 		}
2203459cc2c6SThierry Reding 	}
22046b6b6042SThierry Reding 
22056b6b6042SThierry Reding 	sor->output.dev = sor->dev;
22066b6b6042SThierry Reding 
22076fad8f66SThierry Reding 	drm_connector_init(drm, &sor->output.connector,
22086fad8f66SThierry Reding 			   &tegra_sor_connector_funcs,
2209459cc2c6SThierry Reding 			   connector);
22106fad8f66SThierry Reding 	drm_connector_helper_add(&sor->output.connector,
22116fad8f66SThierry Reding 				 &tegra_sor_connector_helper_funcs);
22126fad8f66SThierry Reding 	sor->output.connector.dpms = DRM_MODE_DPMS_OFF;
22136fad8f66SThierry Reding 
22146fad8f66SThierry Reding 	drm_encoder_init(drm, &sor->output.encoder, &tegra_sor_encoder_funcs,
221513a3d91fSVille Syrjälä 			 encoder, NULL);
2216459cc2c6SThierry Reding 	drm_encoder_helper_add(&sor->output.encoder, helpers);
22176fad8f66SThierry Reding 
22186fad8f66SThierry Reding 	drm_mode_connector_attach_encoder(&sor->output.connector,
22196fad8f66SThierry Reding 					  &sor->output.encoder);
22206fad8f66SThierry Reding 	drm_connector_register(&sor->output.connector);
22216fad8f66SThierry Reding 
2222ea130b24SThierry Reding 	err = tegra_output_init(drm, &sor->output);
2223ea130b24SThierry Reding 	if (err < 0) {
2224ea130b24SThierry Reding 		dev_err(client->dev, "failed to initialize output: %d\n", err);
2225ea130b24SThierry Reding 		return err;
2226ea130b24SThierry Reding 	}
22276fad8f66SThierry Reding 
2228ea130b24SThierry Reding 	sor->output.encoder.possible_crtcs = 0x3;
22296b6b6042SThierry Reding 
2230a82752e1SThierry Reding 	if (IS_ENABLED(CONFIG_DEBUG_FS)) {
22311b0c7b48SThierry Reding 		err = tegra_sor_debugfs_init(sor, drm->primary);
2232a82752e1SThierry Reding 		if (err < 0)
2233a82752e1SThierry Reding 			dev_err(sor->dev, "debugfs setup failed: %d\n", err);
2234a82752e1SThierry Reding 	}
2235a82752e1SThierry Reding 
22369542c237SThierry Reding 	if (sor->aux) {
22379542c237SThierry Reding 		err = drm_dp_aux_attach(sor->aux, &sor->output);
22386b6b6042SThierry Reding 		if (err < 0) {
22396b6b6042SThierry Reding 			dev_err(sor->dev, "failed to attach DP: %d\n", err);
22406b6b6042SThierry Reding 			return err;
22416b6b6042SThierry Reding 		}
22426b6b6042SThierry Reding 	}
22436b6b6042SThierry Reding 
2244535a65dbSTomeu Vizoso 	/*
2245535a65dbSTomeu Vizoso 	 * XXX: Remove this reset once proper hand-over from firmware to
2246535a65dbSTomeu Vizoso 	 * kernel is possible.
2247535a65dbSTomeu Vizoso 	 */
2248535a65dbSTomeu Vizoso 	err = reset_control_assert(sor->rst);
2249535a65dbSTomeu Vizoso 	if (err < 0) {
2250535a65dbSTomeu Vizoso 		dev_err(sor->dev, "failed to assert SOR reset: %d\n", err);
2251535a65dbSTomeu Vizoso 		return err;
2252535a65dbSTomeu Vizoso 	}
2253535a65dbSTomeu Vizoso 
22546fad8f66SThierry Reding 	err = clk_prepare_enable(sor->clk);
22556fad8f66SThierry Reding 	if (err < 0) {
22566fad8f66SThierry Reding 		dev_err(sor->dev, "failed to enable clock: %d\n", err);
22576fad8f66SThierry Reding 		return err;
22586fad8f66SThierry Reding 	}
22596fad8f66SThierry Reding 
2260535a65dbSTomeu Vizoso 	usleep_range(1000, 3000);
2261535a65dbSTomeu Vizoso 
2262535a65dbSTomeu Vizoso 	err = reset_control_deassert(sor->rst);
2263535a65dbSTomeu Vizoso 	if (err < 0) {
2264535a65dbSTomeu Vizoso 		dev_err(sor->dev, "failed to deassert SOR reset: %d\n", err);
2265535a65dbSTomeu Vizoso 		return err;
2266535a65dbSTomeu Vizoso 	}
2267535a65dbSTomeu Vizoso 
22686fad8f66SThierry Reding 	err = clk_prepare_enable(sor->clk_safe);
22696fad8f66SThierry Reding 	if (err < 0)
22706fad8f66SThierry Reding 		return err;
22716fad8f66SThierry Reding 
22726fad8f66SThierry Reding 	err = clk_prepare_enable(sor->clk_dp);
22736fad8f66SThierry Reding 	if (err < 0)
22746fad8f66SThierry Reding 		return err;
22756fad8f66SThierry Reding 
22766b6b6042SThierry Reding 	return 0;
22776b6b6042SThierry Reding }
22786b6b6042SThierry Reding 
22796b6b6042SThierry Reding static int tegra_sor_exit(struct host1x_client *client)
22806b6b6042SThierry Reding {
22816b6b6042SThierry Reding 	struct tegra_sor *sor = host1x_client_to_sor(client);
22826b6b6042SThierry Reding 	int err;
22836b6b6042SThierry Reding 
2284328ec69eSThierry Reding 	tegra_output_exit(&sor->output);
2285328ec69eSThierry Reding 
22869542c237SThierry Reding 	if (sor->aux) {
22879542c237SThierry Reding 		err = drm_dp_aux_detach(sor->aux);
22886b6b6042SThierry Reding 		if (err < 0) {
22896b6b6042SThierry Reding 			dev_err(sor->dev, "failed to detach DP: %d\n", err);
22906b6b6042SThierry Reding 			return err;
22916b6b6042SThierry Reding 		}
22926b6b6042SThierry Reding 	}
22936b6b6042SThierry Reding 
22946fad8f66SThierry Reding 	clk_disable_unprepare(sor->clk_safe);
22956fad8f66SThierry Reding 	clk_disable_unprepare(sor->clk_dp);
22966fad8f66SThierry Reding 	clk_disable_unprepare(sor->clk);
22976fad8f66SThierry Reding 
22984009c224SThierry Reding 	if (IS_ENABLED(CONFIG_DEBUG_FS))
22994009c224SThierry Reding 		tegra_sor_debugfs_exit(sor);
2300a82752e1SThierry Reding 
23016b6b6042SThierry Reding 	return 0;
23026b6b6042SThierry Reding }
23036b6b6042SThierry Reding 
23046b6b6042SThierry Reding static const struct host1x_client_ops sor_client_ops = {
23056b6b6042SThierry Reding 	.init = tegra_sor_init,
23066b6b6042SThierry Reding 	.exit = tegra_sor_exit,
23076b6b6042SThierry Reding };
23086b6b6042SThierry Reding 
2309459cc2c6SThierry Reding static const struct tegra_sor_ops tegra_sor_edp_ops = {
2310459cc2c6SThierry Reding 	.name = "eDP",
2311459cc2c6SThierry Reding };
2312459cc2c6SThierry Reding 
2313459cc2c6SThierry Reding static int tegra_sor_hdmi_probe(struct tegra_sor *sor)
2314459cc2c6SThierry Reding {
2315459cc2c6SThierry Reding 	int err;
2316459cc2c6SThierry Reding 
2317459cc2c6SThierry Reding 	sor->avdd_io_supply = devm_regulator_get(sor->dev, "avdd-io");
2318459cc2c6SThierry Reding 	if (IS_ERR(sor->avdd_io_supply)) {
2319459cc2c6SThierry Reding 		dev_err(sor->dev, "cannot get AVDD I/O supply: %ld\n",
2320459cc2c6SThierry Reding 			PTR_ERR(sor->avdd_io_supply));
2321459cc2c6SThierry Reding 		return PTR_ERR(sor->avdd_io_supply);
2322459cc2c6SThierry Reding 	}
2323459cc2c6SThierry Reding 
2324459cc2c6SThierry Reding 	err = regulator_enable(sor->avdd_io_supply);
2325459cc2c6SThierry Reding 	if (err < 0) {
2326459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to enable AVDD I/O supply: %d\n",
2327459cc2c6SThierry Reding 			err);
2328459cc2c6SThierry Reding 		return err;
2329459cc2c6SThierry Reding 	}
2330459cc2c6SThierry Reding 
2331459cc2c6SThierry Reding 	sor->vdd_pll_supply = devm_regulator_get(sor->dev, "vdd-pll");
2332459cc2c6SThierry Reding 	if (IS_ERR(sor->vdd_pll_supply)) {
2333459cc2c6SThierry Reding 		dev_err(sor->dev, "cannot get VDD PLL supply: %ld\n",
2334459cc2c6SThierry Reding 			PTR_ERR(sor->vdd_pll_supply));
2335459cc2c6SThierry Reding 		return PTR_ERR(sor->vdd_pll_supply);
2336459cc2c6SThierry Reding 	}
2337459cc2c6SThierry Reding 
2338459cc2c6SThierry Reding 	err = regulator_enable(sor->vdd_pll_supply);
2339459cc2c6SThierry Reding 	if (err < 0) {
2340459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to enable VDD PLL supply: %d\n",
2341459cc2c6SThierry Reding 			err);
2342459cc2c6SThierry Reding 		return err;
2343459cc2c6SThierry Reding 	}
2344459cc2c6SThierry Reding 
2345459cc2c6SThierry Reding 	sor->hdmi_supply = devm_regulator_get(sor->dev, "hdmi");
2346459cc2c6SThierry Reding 	if (IS_ERR(sor->hdmi_supply)) {
2347459cc2c6SThierry Reding 		dev_err(sor->dev, "cannot get HDMI supply: %ld\n",
2348459cc2c6SThierry Reding 			PTR_ERR(sor->hdmi_supply));
2349459cc2c6SThierry Reding 		return PTR_ERR(sor->hdmi_supply);
2350459cc2c6SThierry Reding 	}
2351459cc2c6SThierry Reding 
2352459cc2c6SThierry Reding 	err = regulator_enable(sor->hdmi_supply);
2353459cc2c6SThierry Reding 	if (err < 0) {
2354459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to enable HDMI supply: %d\n", err);
2355459cc2c6SThierry Reding 		return err;
2356459cc2c6SThierry Reding 	}
2357459cc2c6SThierry Reding 
2358459cc2c6SThierry Reding 	return 0;
2359459cc2c6SThierry Reding }
2360459cc2c6SThierry Reding 
2361459cc2c6SThierry Reding static int tegra_sor_hdmi_remove(struct tegra_sor *sor)
2362459cc2c6SThierry Reding {
2363459cc2c6SThierry Reding 	regulator_disable(sor->hdmi_supply);
2364459cc2c6SThierry Reding 	regulator_disable(sor->vdd_pll_supply);
2365459cc2c6SThierry Reding 	regulator_disable(sor->avdd_io_supply);
2366459cc2c6SThierry Reding 
2367459cc2c6SThierry Reding 	return 0;
2368459cc2c6SThierry Reding }
2369459cc2c6SThierry Reding 
2370459cc2c6SThierry Reding static const struct tegra_sor_ops tegra_sor_hdmi_ops = {
2371459cc2c6SThierry Reding 	.name = "HDMI",
2372459cc2c6SThierry Reding 	.probe = tegra_sor_hdmi_probe,
2373459cc2c6SThierry Reding 	.remove = tegra_sor_hdmi_remove,
2374459cc2c6SThierry Reding };
2375459cc2c6SThierry Reding 
2376459cc2c6SThierry Reding static const struct tegra_sor_soc tegra124_sor = {
2377459cc2c6SThierry Reding 	.supports_edp = true,
2378459cc2c6SThierry Reding 	.supports_lvds = true,
2379459cc2c6SThierry Reding 	.supports_hdmi = false,
2380459cc2c6SThierry Reding 	.supports_dp = false,
2381459cc2c6SThierry Reding };
2382459cc2c6SThierry Reding 
2383459cc2c6SThierry Reding static const struct tegra_sor_soc tegra210_sor = {
2384459cc2c6SThierry Reding 	.supports_edp = true,
2385459cc2c6SThierry Reding 	.supports_lvds = false,
2386459cc2c6SThierry Reding 	.supports_hdmi = false,
2387459cc2c6SThierry Reding 	.supports_dp = false,
2388459cc2c6SThierry Reding };
2389459cc2c6SThierry Reding 
2390459cc2c6SThierry Reding static const struct tegra_sor_soc tegra210_sor1 = {
2391459cc2c6SThierry Reding 	.supports_edp = false,
2392459cc2c6SThierry Reding 	.supports_lvds = false,
2393459cc2c6SThierry Reding 	.supports_hdmi = true,
2394459cc2c6SThierry Reding 	.supports_dp = true,
2395459cc2c6SThierry Reding 
2396459cc2c6SThierry Reding 	.num_settings = ARRAY_SIZE(tegra210_sor_hdmi_defaults),
2397459cc2c6SThierry Reding 	.settings = tegra210_sor_hdmi_defaults,
2398459cc2c6SThierry Reding };
2399459cc2c6SThierry Reding 
2400459cc2c6SThierry Reding static const struct of_device_id tegra_sor_of_match[] = {
2401459cc2c6SThierry Reding 	{ .compatible = "nvidia,tegra210-sor1", .data = &tegra210_sor1 },
2402459cc2c6SThierry Reding 	{ .compatible = "nvidia,tegra210-sor", .data = &tegra210_sor },
2403459cc2c6SThierry Reding 	{ .compatible = "nvidia,tegra124-sor", .data = &tegra124_sor },
2404459cc2c6SThierry Reding 	{ },
2405459cc2c6SThierry Reding };
2406459cc2c6SThierry Reding MODULE_DEVICE_TABLE(of, tegra_sor_of_match);
2407459cc2c6SThierry Reding 
24086b6b6042SThierry Reding static int tegra_sor_probe(struct platform_device *pdev)
24096b6b6042SThierry Reding {
2410459cc2c6SThierry Reding 	const struct of_device_id *match;
24116b6b6042SThierry Reding 	struct device_node *np;
24126b6b6042SThierry Reding 	struct tegra_sor *sor;
24136b6b6042SThierry Reding 	struct resource *regs;
24146b6b6042SThierry Reding 	int err;
24156b6b6042SThierry Reding 
2416459cc2c6SThierry Reding 	match = of_match_device(tegra_sor_of_match, &pdev->dev);
2417459cc2c6SThierry Reding 
24186b6b6042SThierry Reding 	sor = devm_kzalloc(&pdev->dev, sizeof(*sor), GFP_KERNEL);
24196b6b6042SThierry Reding 	if (!sor)
24206b6b6042SThierry Reding 		return -ENOMEM;
24216b6b6042SThierry Reding 
24226b6b6042SThierry Reding 	sor->output.dev = sor->dev = &pdev->dev;
2423459cc2c6SThierry Reding 	sor->soc = match->data;
2424459cc2c6SThierry Reding 
2425459cc2c6SThierry Reding 	sor->settings = devm_kmemdup(&pdev->dev, sor->soc->settings,
2426459cc2c6SThierry Reding 				     sor->soc->num_settings *
2427459cc2c6SThierry Reding 					sizeof(*sor->settings),
2428459cc2c6SThierry Reding 				     GFP_KERNEL);
2429459cc2c6SThierry Reding 	if (!sor->settings)
2430459cc2c6SThierry Reding 		return -ENOMEM;
2431459cc2c6SThierry Reding 
2432459cc2c6SThierry Reding 	sor->num_settings = sor->soc->num_settings;
24336b6b6042SThierry Reding 
24346b6b6042SThierry Reding 	np = of_parse_phandle(pdev->dev.of_node, "nvidia,dpaux", 0);
24356b6b6042SThierry Reding 	if (np) {
24369542c237SThierry Reding 		sor->aux = drm_dp_aux_find_by_of_node(np);
24376b6b6042SThierry Reding 		of_node_put(np);
24386b6b6042SThierry Reding 
24399542c237SThierry Reding 		if (!sor->aux)
24406b6b6042SThierry Reding 			return -EPROBE_DEFER;
24416b6b6042SThierry Reding 	}
24426b6b6042SThierry Reding 
24439542c237SThierry Reding 	if (!sor->aux) {
2444459cc2c6SThierry Reding 		if (sor->soc->supports_hdmi) {
2445459cc2c6SThierry Reding 			sor->ops = &tegra_sor_hdmi_ops;
2446459cc2c6SThierry Reding 		} else if (sor->soc->supports_lvds) {
2447459cc2c6SThierry Reding 			dev_err(&pdev->dev, "LVDS not supported yet\n");
2448459cc2c6SThierry Reding 			return -ENODEV;
2449459cc2c6SThierry Reding 		} else {
2450459cc2c6SThierry Reding 			dev_err(&pdev->dev, "unknown (non-DP) support\n");
2451459cc2c6SThierry Reding 			return -ENODEV;
2452459cc2c6SThierry Reding 		}
2453459cc2c6SThierry Reding 	} else {
2454459cc2c6SThierry Reding 		if (sor->soc->supports_edp) {
2455459cc2c6SThierry Reding 			sor->ops = &tegra_sor_edp_ops;
2456459cc2c6SThierry Reding 		} else if (sor->soc->supports_dp) {
2457459cc2c6SThierry Reding 			dev_err(&pdev->dev, "DisplayPort not supported yet\n");
2458459cc2c6SThierry Reding 			return -ENODEV;
2459459cc2c6SThierry Reding 		} else {
2460459cc2c6SThierry Reding 			dev_err(&pdev->dev, "unknown (DP) support\n");
2461459cc2c6SThierry Reding 			return -ENODEV;
2462459cc2c6SThierry Reding 		}
2463459cc2c6SThierry Reding 	}
2464459cc2c6SThierry Reding 
24656b6b6042SThierry Reding 	err = tegra_output_probe(&sor->output);
24664dbdc740SThierry Reding 	if (err < 0) {
24674dbdc740SThierry Reding 		dev_err(&pdev->dev, "failed to probe output: %d\n", err);
24686b6b6042SThierry Reding 		return err;
24694dbdc740SThierry Reding 	}
24706b6b6042SThierry Reding 
2471459cc2c6SThierry Reding 	if (sor->ops && sor->ops->probe) {
2472459cc2c6SThierry Reding 		err = sor->ops->probe(sor);
2473459cc2c6SThierry Reding 		if (err < 0) {
2474459cc2c6SThierry Reding 			dev_err(&pdev->dev, "failed to probe %s: %d\n",
2475459cc2c6SThierry Reding 				sor->ops->name, err);
2476459cc2c6SThierry Reding 			goto output;
2477459cc2c6SThierry Reding 		}
2478459cc2c6SThierry Reding 	}
2479459cc2c6SThierry Reding 
24806b6b6042SThierry Reding 	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
24816b6b6042SThierry Reding 	sor->regs = devm_ioremap_resource(&pdev->dev, regs);
2482459cc2c6SThierry Reding 	if (IS_ERR(sor->regs)) {
2483459cc2c6SThierry Reding 		err = PTR_ERR(sor->regs);
2484459cc2c6SThierry Reding 		goto remove;
2485459cc2c6SThierry Reding 	}
24866b6b6042SThierry Reding 
24876b6b6042SThierry Reding 	sor->rst = devm_reset_control_get(&pdev->dev, "sor");
24884dbdc740SThierry Reding 	if (IS_ERR(sor->rst)) {
2489459cc2c6SThierry Reding 		err = PTR_ERR(sor->rst);
2490459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get reset control: %d\n", err);
2491459cc2c6SThierry Reding 		goto remove;
24924dbdc740SThierry Reding 	}
24936b6b6042SThierry Reding 
24946b6b6042SThierry Reding 	sor->clk = devm_clk_get(&pdev->dev, NULL);
24954dbdc740SThierry Reding 	if (IS_ERR(sor->clk)) {
2496459cc2c6SThierry Reding 		err = PTR_ERR(sor->clk);
2497459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get module clock: %d\n", err);
2498459cc2c6SThierry Reding 		goto remove;
24994dbdc740SThierry Reding 	}
25006b6b6042SThierry Reding 
25016b6b6042SThierry Reding 	sor->clk_parent = devm_clk_get(&pdev->dev, "parent");
25024dbdc740SThierry Reding 	if (IS_ERR(sor->clk_parent)) {
2503459cc2c6SThierry Reding 		err = PTR_ERR(sor->clk_parent);
2504459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get parent clock: %d\n", err);
2505459cc2c6SThierry Reding 		goto remove;
25064dbdc740SThierry Reding 	}
25076b6b6042SThierry Reding 
25086b6b6042SThierry Reding 	sor->clk_safe = devm_clk_get(&pdev->dev, "safe");
25094dbdc740SThierry Reding 	if (IS_ERR(sor->clk_safe)) {
2510459cc2c6SThierry Reding 		err = PTR_ERR(sor->clk_safe);
2511459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get safe clock: %d\n", err);
2512459cc2c6SThierry Reding 		goto remove;
25134dbdc740SThierry Reding 	}
25146b6b6042SThierry Reding 
25156b6b6042SThierry Reding 	sor->clk_dp = devm_clk_get(&pdev->dev, "dp");
25164dbdc740SThierry Reding 	if (IS_ERR(sor->clk_dp)) {
2517459cc2c6SThierry Reding 		err = PTR_ERR(sor->clk_dp);
2518459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get DP clock: %d\n", err);
2519459cc2c6SThierry Reding 		goto remove;
25204dbdc740SThierry Reding 	}
25216b6b6042SThierry Reding 
2522*aaff8bd2SThierry Reding 	platform_set_drvdata(pdev, sor);
2523*aaff8bd2SThierry Reding 	pm_runtime_enable(&pdev->dev);
2524*aaff8bd2SThierry Reding 
25256b6b6042SThierry Reding 	INIT_LIST_HEAD(&sor->client.list);
25266b6b6042SThierry Reding 	sor->client.ops = &sor_client_ops;
25276b6b6042SThierry Reding 	sor->client.dev = &pdev->dev;
25286b6b6042SThierry Reding 
25296b6b6042SThierry Reding 	err = host1x_client_register(&sor->client);
25306b6b6042SThierry Reding 	if (err < 0) {
25316b6b6042SThierry Reding 		dev_err(&pdev->dev, "failed to register host1x client: %d\n",
25326b6b6042SThierry Reding 			err);
2533459cc2c6SThierry Reding 		goto remove;
25346b6b6042SThierry Reding 	}
25356b6b6042SThierry Reding 
25366b6b6042SThierry Reding 	return 0;
2537459cc2c6SThierry Reding 
2538459cc2c6SThierry Reding remove:
2539459cc2c6SThierry Reding 	if (sor->ops && sor->ops->remove)
2540459cc2c6SThierry Reding 		sor->ops->remove(sor);
2541459cc2c6SThierry Reding output:
2542459cc2c6SThierry Reding 	tegra_output_remove(&sor->output);
2543459cc2c6SThierry Reding 	return err;
25446b6b6042SThierry Reding }
25456b6b6042SThierry Reding 
25466b6b6042SThierry Reding static int tegra_sor_remove(struct platform_device *pdev)
25476b6b6042SThierry Reding {
25486b6b6042SThierry Reding 	struct tegra_sor *sor = platform_get_drvdata(pdev);
25496b6b6042SThierry Reding 	int err;
25506b6b6042SThierry Reding 
2551*aaff8bd2SThierry Reding 	pm_runtime_disable(&pdev->dev);
2552*aaff8bd2SThierry Reding 
25536b6b6042SThierry Reding 	err = host1x_client_unregister(&sor->client);
25546b6b6042SThierry Reding 	if (err < 0) {
25556b6b6042SThierry Reding 		dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
25566b6b6042SThierry Reding 			err);
25576b6b6042SThierry Reding 		return err;
25586b6b6042SThierry Reding 	}
25596b6b6042SThierry Reding 
2560459cc2c6SThierry Reding 	if (sor->ops && sor->ops->remove) {
2561459cc2c6SThierry Reding 		err = sor->ops->remove(sor);
2562459cc2c6SThierry Reding 		if (err < 0)
2563459cc2c6SThierry Reding 			dev_err(&pdev->dev, "failed to remove SOR: %d\n", err);
2564459cc2c6SThierry Reding 	}
2565459cc2c6SThierry Reding 
2566328ec69eSThierry Reding 	tegra_output_remove(&sor->output);
25676b6b6042SThierry Reding 
25686b6b6042SThierry Reding 	return 0;
25696b6b6042SThierry Reding }
25706b6b6042SThierry Reding 
2571*aaff8bd2SThierry Reding #ifdef CONFIG_PM
2572*aaff8bd2SThierry Reding static int tegra_sor_suspend(struct device *dev)
2573*aaff8bd2SThierry Reding {
2574*aaff8bd2SThierry Reding 	struct tegra_sor *sor = dev_get_drvdata(dev);
2575*aaff8bd2SThierry Reding 	int err;
2576*aaff8bd2SThierry Reding 
2577*aaff8bd2SThierry Reding 	err = reset_control_assert(sor->rst);
2578*aaff8bd2SThierry Reding 	if (err < 0) {
2579*aaff8bd2SThierry Reding 		dev_err(dev, "failed to assert reset: %d\n", err);
2580*aaff8bd2SThierry Reding 		return err;
2581*aaff8bd2SThierry Reding 	}
2582*aaff8bd2SThierry Reding 
2583*aaff8bd2SThierry Reding 	usleep_range(1000, 2000);
2584*aaff8bd2SThierry Reding 
2585*aaff8bd2SThierry Reding 	clk_disable_unprepare(sor->clk);
2586*aaff8bd2SThierry Reding 
2587*aaff8bd2SThierry Reding 	return 0;
2588*aaff8bd2SThierry Reding }
2589*aaff8bd2SThierry Reding 
2590*aaff8bd2SThierry Reding static int tegra_sor_resume(struct device *dev)
2591*aaff8bd2SThierry Reding {
2592*aaff8bd2SThierry Reding 	struct tegra_sor *sor = dev_get_drvdata(dev);
2593*aaff8bd2SThierry Reding 	int err;
2594*aaff8bd2SThierry Reding 
2595*aaff8bd2SThierry Reding 	err = clk_prepare_enable(sor->clk);
2596*aaff8bd2SThierry Reding 	if (err < 0) {
2597*aaff8bd2SThierry Reding 		dev_err(dev, "failed to enable clock: %d\n", err);
2598*aaff8bd2SThierry Reding 		return err;
2599*aaff8bd2SThierry Reding 	}
2600*aaff8bd2SThierry Reding 
2601*aaff8bd2SThierry Reding 	usleep_range(1000, 2000);
2602*aaff8bd2SThierry Reding 
2603*aaff8bd2SThierry Reding 	err = reset_control_deassert(sor->rst);
2604*aaff8bd2SThierry Reding 	if (err < 0) {
2605*aaff8bd2SThierry Reding 		dev_err(dev, "failed to deassert reset: %d\n", err);
2606*aaff8bd2SThierry Reding 		clk_disable_unprepare(sor->clk);
2607*aaff8bd2SThierry Reding 		return err;
2608*aaff8bd2SThierry Reding 	}
2609*aaff8bd2SThierry Reding 
2610*aaff8bd2SThierry Reding 	return 0;
2611*aaff8bd2SThierry Reding }
2612*aaff8bd2SThierry Reding #endif
2613*aaff8bd2SThierry Reding 
2614*aaff8bd2SThierry Reding static const struct dev_pm_ops tegra_sor_pm_ops = {
2615*aaff8bd2SThierry Reding 	SET_RUNTIME_PM_OPS(tegra_sor_suspend, tegra_sor_resume, NULL)
2616*aaff8bd2SThierry Reding };
2617*aaff8bd2SThierry Reding 
26186b6b6042SThierry Reding struct platform_driver tegra_sor_driver = {
26196b6b6042SThierry Reding 	.driver = {
26206b6b6042SThierry Reding 		.name = "tegra-sor",
26216b6b6042SThierry Reding 		.of_match_table = tegra_sor_of_match,
2622*aaff8bd2SThierry Reding 		.pm = &tegra_sor_pm_ops,
26236b6b6042SThierry Reding 	},
26246b6b6042SThierry Reding 	.probe = tegra_sor_probe,
26256b6b6042SThierry Reding 	.remove = tegra_sor_remove,
26266b6b6042SThierry Reding };
2627