xref: /openbmc/linux/drivers/gpu/drm/tegra/sor.c (revision a198359e39c0e47a995d8e88638d5738ae4cfbe2)
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>
15459cc2c6SThierry Reding #include <linux/regulator/consumer.h>
166b6b6042SThierry Reding #include <linux/reset.h>
17306a7f91SThierry Reding 
187232398aSThierry Reding #include <soc/tegra/pmc.h>
196b6b6042SThierry Reding 
204aa3df71SThierry Reding #include <drm/drm_atomic_helper.h>
216b6b6042SThierry Reding #include <drm/drm_dp_helper.h>
226fad8f66SThierry Reding #include <drm/drm_panel.h>
236b6b6042SThierry Reding 
246b6b6042SThierry Reding #include "dc.h"
256b6b6042SThierry Reding #include "drm.h"
266b6b6042SThierry Reding #include "sor.h"
276b6b6042SThierry Reding 
28459cc2c6SThierry Reding #define SOR_REKEY 0x38
29459cc2c6SThierry Reding 
30459cc2c6SThierry Reding struct tegra_sor_hdmi_settings {
31459cc2c6SThierry Reding 	unsigned long frequency;
32459cc2c6SThierry Reding 
33459cc2c6SThierry Reding 	u8 vcocap;
34459cc2c6SThierry Reding 	u8 ichpmp;
35459cc2c6SThierry Reding 	u8 loadadj;
36459cc2c6SThierry Reding 	u8 termadj;
37459cc2c6SThierry Reding 	u8 tx_pu;
38459cc2c6SThierry Reding 	u8 bg_vref;
39459cc2c6SThierry Reding 
40459cc2c6SThierry Reding 	u8 drive_current[4];
41459cc2c6SThierry Reding 	u8 preemphasis[4];
42459cc2c6SThierry Reding };
43459cc2c6SThierry Reding 
44459cc2c6SThierry Reding #if 1
45459cc2c6SThierry Reding static const struct tegra_sor_hdmi_settings tegra210_sor_hdmi_defaults[] = {
46459cc2c6SThierry Reding 	{
47459cc2c6SThierry Reding 		.frequency = 54000000,
48459cc2c6SThierry Reding 		.vcocap = 0x0,
49459cc2c6SThierry Reding 		.ichpmp = 0x1,
50459cc2c6SThierry Reding 		.loadadj = 0x3,
51459cc2c6SThierry Reding 		.termadj = 0x9,
52459cc2c6SThierry Reding 		.tx_pu = 0x10,
53459cc2c6SThierry Reding 		.bg_vref = 0x8,
54459cc2c6SThierry Reding 		.drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
55459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
56459cc2c6SThierry Reding 	}, {
57459cc2c6SThierry Reding 		.frequency = 75000000,
58459cc2c6SThierry Reding 		.vcocap = 0x3,
59459cc2c6SThierry Reding 		.ichpmp = 0x1,
60459cc2c6SThierry Reding 		.loadadj = 0x3,
61459cc2c6SThierry Reding 		.termadj = 0x9,
62459cc2c6SThierry Reding 		.tx_pu = 0x40,
63459cc2c6SThierry Reding 		.bg_vref = 0x8,
64459cc2c6SThierry Reding 		.drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
65459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
66459cc2c6SThierry Reding 	}, {
67459cc2c6SThierry Reding 		.frequency = 150000000,
68459cc2c6SThierry Reding 		.vcocap = 0x3,
69459cc2c6SThierry Reding 		.ichpmp = 0x1,
70459cc2c6SThierry Reding 		.loadadj = 0x3,
71459cc2c6SThierry Reding 		.termadj = 0x9,
72459cc2c6SThierry Reding 		.tx_pu = 0x66,
73459cc2c6SThierry Reding 		.bg_vref = 0x8,
74459cc2c6SThierry Reding 		.drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
75459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
76459cc2c6SThierry Reding 	}, {
77459cc2c6SThierry Reding 		.frequency = 300000000,
78459cc2c6SThierry Reding 		.vcocap = 0x3,
79459cc2c6SThierry Reding 		.ichpmp = 0x1,
80459cc2c6SThierry Reding 		.loadadj = 0x3,
81459cc2c6SThierry Reding 		.termadj = 0x9,
82459cc2c6SThierry Reding 		.tx_pu = 0x66,
83459cc2c6SThierry Reding 		.bg_vref = 0xa,
84459cc2c6SThierry Reding 		.drive_current = { 0x33, 0x3f, 0x3f, 0x3f },
85459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x17, 0x17, 0x17 },
86459cc2c6SThierry Reding 	}, {
87459cc2c6SThierry Reding 		.frequency = 600000000,
88459cc2c6SThierry Reding 		.vcocap = 0x3,
89459cc2c6SThierry Reding 		.ichpmp = 0x1,
90459cc2c6SThierry Reding 		.loadadj = 0x3,
91459cc2c6SThierry Reding 		.termadj = 0x9,
92459cc2c6SThierry Reding 		.tx_pu = 0x66,
93459cc2c6SThierry Reding 		.bg_vref = 0x8,
94459cc2c6SThierry Reding 		.drive_current = { 0x33, 0x3f, 0x3f, 0x3f },
95459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
96459cc2c6SThierry Reding 	},
97459cc2c6SThierry Reding };
98459cc2c6SThierry Reding #else
99459cc2c6SThierry Reding static const struct tegra_sor_hdmi_settings tegra210_sor_hdmi_defaults[] = {
100459cc2c6SThierry Reding 	{
101459cc2c6SThierry Reding 		.frequency = 75000000,
102459cc2c6SThierry Reding 		.vcocap = 0x3,
103459cc2c6SThierry Reding 		.ichpmp = 0x1,
104459cc2c6SThierry Reding 		.loadadj = 0x3,
105459cc2c6SThierry Reding 		.termadj = 0x9,
106459cc2c6SThierry Reding 		.tx_pu = 0x40,
107459cc2c6SThierry Reding 		.bg_vref = 0x8,
108459cc2c6SThierry Reding 		.drive_current = { 0x29, 0x29, 0x29, 0x29 },
109459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
110459cc2c6SThierry Reding 	}, {
111459cc2c6SThierry Reding 		.frequency = 150000000,
112459cc2c6SThierry Reding 		.vcocap = 0x3,
113459cc2c6SThierry Reding 		.ichpmp = 0x1,
114459cc2c6SThierry Reding 		.loadadj = 0x3,
115459cc2c6SThierry Reding 		.termadj = 0x9,
116459cc2c6SThierry Reding 		.tx_pu = 0x66,
117459cc2c6SThierry Reding 		.bg_vref = 0x8,
118459cc2c6SThierry Reding 		.drive_current = { 0x30, 0x37, 0x37, 0x37 },
119459cc2c6SThierry Reding 		.preemphasis = { 0x01, 0x02, 0x02, 0x02 },
120459cc2c6SThierry Reding 	}, {
121459cc2c6SThierry Reding 		.frequency = 300000000,
122459cc2c6SThierry Reding 		.vcocap = 0x3,
123459cc2c6SThierry Reding 		.ichpmp = 0x6,
124459cc2c6SThierry Reding 		.loadadj = 0x3,
125459cc2c6SThierry Reding 		.termadj = 0x9,
126459cc2c6SThierry Reding 		.tx_pu = 0x66,
127459cc2c6SThierry Reding 		.bg_vref = 0xf,
128459cc2c6SThierry Reding 		.drive_current = { 0x30, 0x37, 0x37, 0x37 },
129459cc2c6SThierry Reding 		.preemphasis = { 0x10, 0x3e, 0x3e, 0x3e },
130459cc2c6SThierry Reding 	}, {
131459cc2c6SThierry Reding 		.frequency = 600000000,
132459cc2c6SThierry Reding 		.vcocap = 0x3,
133459cc2c6SThierry Reding 		.ichpmp = 0xa,
134459cc2c6SThierry Reding 		.loadadj = 0x3,
135459cc2c6SThierry Reding 		.termadj = 0xb,
136459cc2c6SThierry Reding 		.tx_pu = 0x66,
137459cc2c6SThierry Reding 		.bg_vref = 0xe,
138459cc2c6SThierry Reding 		.drive_current = { 0x35, 0x3e, 0x3e, 0x3e },
139459cc2c6SThierry Reding 		.preemphasis = { 0x02, 0x3f, 0x3f, 0x3f },
140459cc2c6SThierry Reding 	},
141459cc2c6SThierry Reding };
142459cc2c6SThierry Reding #endif
143459cc2c6SThierry Reding 
144459cc2c6SThierry Reding struct tegra_sor_soc {
145459cc2c6SThierry Reding 	bool supports_edp;
146459cc2c6SThierry Reding 	bool supports_lvds;
147459cc2c6SThierry Reding 	bool supports_hdmi;
148459cc2c6SThierry Reding 	bool supports_dp;
149459cc2c6SThierry Reding 
150459cc2c6SThierry Reding 	const struct tegra_sor_hdmi_settings *settings;
151459cc2c6SThierry Reding 	unsigned int num_settings;
152459cc2c6SThierry Reding };
153459cc2c6SThierry Reding 
154459cc2c6SThierry Reding struct tegra_sor;
155459cc2c6SThierry Reding 
156459cc2c6SThierry Reding struct tegra_sor_ops {
157459cc2c6SThierry Reding 	const char *name;
158459cc2c6SThierry Reding 	int (*probe)(struct tegra_sor *sor);
159459cc2c6SThierry Reding 	int (*remove)(struct tegra_sor *sor);
160459cc2c6SThierry Reding };
161459cc2c6SThierry Reding 
1626b6b6042SThierry Reding struct tegra_sor {
1636b6b6042SThierry Reding 	struct host1x_client client;
1646b6b6042SThierry Reding 	struct tegra_output output;
1656b6b6042SThierry Reding 	struct device *dev;
1666b6b6042SThierry Reding 
167459cc2c6SThierry Reding 	const struct tegra_sor_soc *soc;
1686b6b6042SThierry Reding 	void __iomem *regs;
1696b6b6042SThierry Reding 
1706b6b6042SThierry Reding 	struct reset_control *rst;
1716b6b6042SThierry Reding 	struct clk *clk_parent;
1726b6b6042SThierry Reding 	struct clk *clk_safe;
1736b6b6042SThierry Reding 	struct clk *clk_dp;
1746b6b6042SThierry Reding 	struct clk *clk;
1756b6b6042SThierry Reding 
1769542c237SThierry Reding 	struct drm_dp_aux *aux;
1776b6b6042SThierry Reding 
178dab16336SThierry Reding 	struct drm_info_list *debugfs_files;
179dab16336SThierry Reding 	struct drm_minor *minor;
180a82752e1SThierry Reding 	struct dentry *debugfs;
181459cc2c6SThierry Reding 
182459cc2c6SThierry Reding 	const struct tegra_sor_ops *ops;
183459cc2c6SThierry Reding 
184459cc2c6SThierry Reding 	/* for HDMI 2.0 */
185459cc2c6SThierry Reding 	struct tegra_sor_hdmi_settings *settings;
186459cc2c6SThierry Reding 	unsigned int num_settings;
187459cc2c6SThierry Reding 
188459cc2c6SThierry Reding 	struct regulator *avdd_io_supply;
189459cc2c6SThierry Reding 	struct regulator *vdd_pll_supply;
190459cc2c6SThierry Reding 	struct regulator *hdmi_supply;
1916b6b6042SThierry Reding };
1926b6b6042SThierry Reding 
19334fa183bSThierry Reding struct tegra_sor_config {
19434fa183bSThierry Reding 	u32 bits_per_pixel;
19534fa183bSThierry Reding 
19634fa183bSThierry Reding 	u32 active_polarity;
19734fa183bSThierry Reding 	u32 active_count;
19834fa183bSThierry Reding 	u32 tu_size;
19934fa183bSThierry Reding 	u32 active_frac;
20034fa183bSThierry Reding 	u32 watermark;
2017890b576SThierry Reding 
2027890b576SThierry Reding 	u32 hblank_symbols;
2037890b576SThierry Reding 	u32 vblank_symbols;
20434fa183bSThierry Reding };
20534fa183bSThierry Reding 
2066b6b6042SThierry Reding static inline struct tegra_sor *
2076b6b6042SThierry Reding host1x_client_to_sor(struct host1x_client *client)
2086b6b6042SThierry Reding {
2096b6b6042SThierry Reding 	return container_of(client, struct tegra_sor, client);
2106b6b6042SThierry Reding }
2116b6b6042SThierry Reding 
2126b6b6042SThierry Reding static inline struct tegra_sor *to_sor(struct tegra_output *output)
2136b6b6042SThierry Reding {
2146b6b6042SThierry Reding 	return container_of(output, struct tegra_sor, output);
2156b6b6042SThierry Reding }
2166b6b6042SThierry Reding 
21728fe2076SThierry Reding static inline u32 tegra_sor_readl(struct tegra_sor *sor, unsigned long offset)
2186b6b6042SThierry Reding {
2196b6b6042SThierry Reding 	return readl(sor->regs + (offset << 2));
2206b6b6042SThierry Reding }
2216b6b6042SThierry Reding 
22228fe2076SThierry Reding static inline void tegra_sor_writel(struct tegra_sor *sor, u32 value,
2236b6b6042SThierry Reding 				    unsigned long offset)
2246b6b6042SThierry Reding {
2256b6b6042SThierry Reding 	writel(value, sor->regs + (offset << 2));
2266b6b6042SThierry Reding }
2276b6b6042SThierry Reding 
22825bb2cecSThierry Reding static int tegra_sor_set_parent_clock(struct tegra_sor *sor, struct clk *parent)
22925bb2cecSThierry Reding {
23025bb2cecSThierry Reding 	int err;
23125bb2cecSThierry Reding 
23225bb2cecSThierry Reding 	clk_disable_unprepare(sor->clk);
23325bb2cecSThierry Reding 
23425bb2cecSThierry Reding 	err = clk_set_parent(sor->clk, parent);
23525bb2cecSThierry Reding 	if (err < 0)
23625bb2cecSThierry Reding 		return err;
23725bb2cecSThierry Reding 
23825bb2cecSThierry Reding 	err = clk_prepare_enable(sor->clk);
23925bb2cecSThierry Reding 	if (err < 0)
24025bb2cecSThierry Reding 		return err;
24125bb2cecSThierry Reding 
24225bb2cecSThierry Reding 	return 0;
24325bb2cecSThierry Reding }
24425bb2cecSThierry Reding 
2456b6b6042SThierry Reding static int tegra_sor_dp_train_fast(struct tegra_sor *sor,
2466b6b6042SThierry Reding 				   struct drm_dp_link *link)
2476b6b6042SThierry Reding {
2486b6b6042SThierry Reding 	unsigned int i;
2496b6b6042SThierry Reding 	u8 pattern;
25028fe2076SThierry Reding 	u32 value;
2516b6b6042SThierry Reding 	int err;
2526b6b6042SThierry Reding 
2536b6b6042SThierry Reding 	/* setup lane parameters */
2546b6b6042SThierry Reding 	value = SOR_LANE_DRIVE_CURRENT_LANE3(0x40) |
2556b6b6042SThierry Reding 		SOR_LANE_DRIVE_CURRENT_LANE2(0x40) |
2566b6b6042SThierry Reding 		SOR_LANE_DRIVE_CURRENT_LANE1(0x40) |
2576b6b6042SThierry Reding 		SOR_LANE_DRIVE_CURRENT_LANE0(0x40);
258a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT0);
2596b6b6042SThierry Reding 
2606b6b6042SThierry Reding 	value = SOR_LANE_PREEMPHASIS_LANE3(0x0f) |
2616b6b6042SThierry Reding 		SOR_LANE_PREEMPHASIS_LANE2(0x0f) |
2626b6b6042SThierry Reding 		SOR_LANE_PREEMPHASIS_LANE1(0x0f) |
2636b6b6042SThierry Reding 		SOR_LANE_PREEMPHASIS_LANE0(0x0f);
264a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS0);
2656b6b6042SThierry Reding 
266a9a9e4fdSThierry Reding 	value = SOR_LANE_POSTCURSOR_LANE3(0x00) |
267a9a9e4fdSThierry Reding 		SOR_LANE_POSTCURSOR_LANE2(0x00) |
268a9a9e4fdSThierry Reding 		SOR_LANE_POSTCURSOR_LANE1(0x00) |
269a9a9e4fdSThierry Reding 		SOR_LANE_POSTCURSOR_LANE0(0x00);
270a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_POSTCURSOR0);
2716b6b6042SThierry Reding 
2726b6b6042SThierry Reding 	/* disable LVDS mode */
2736b6b6042SThierry Reding 	tegra_sor_writel(sor, 0, SOR_LVDS);
2746b6b6042SThierry Reding 
275a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
2766b6b6042SThierry Reding 	value |= SOR_DP_PADCTL_TX_PU_ENABLE;
2776b6b6042SThierry Reding 	value &= ~SOR_DP_PADCTL_TX_PU_MASK;
2786b6b6042SThierry Reding 	value |= SOR_DP_PADCTL_TX_PU(2); /* XXX: don't hardcode? */
279a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
2806b6b6042SThierry Reding 
281a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
2826b6b6042SThierry Reding 	value |= SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
2836b6b6042SThierry Reding 		 SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0;
284a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
2856b6b6042SThierry Reding 
2866b6b6042SThierry Reding 	usleep_range(10, 100);
2876b6b6042SThierry Reding 
288a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
2896b6b6042SThierry Reding 	value &= ~(SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
2906b6b6042SThierry Reding 		   SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0);
291a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
2926b6b6042SThierry Reding 
2939542c237SThierry Reding 	err = drm_dp_aux_prepare(sor->aux, DP_SET_ANSI_8B10B);
2946b6b6042SThierry Reding 	if (err < 0)
2956b6b6042SThierry Reding 		return err;
2966b6b6042SThierry Reding 
2976b6b6042SThierry Reding 	for (i = 0, value = 0; i < link->num_lanes; i++) {
2986b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
2996b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_NONE |
3006b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_TRAIN1;
3016b6b6042SThierry Reding 		value = (value << 8) | lane;
3026b6b6042SThierry Reding 	}
3036b6b6042SThierry Reding 
3046b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
3056b6b6042SThierry Reding 
3066b6b6042SThierry Reding 	pattern = DP_TRAINING_PATTERN_1;
3076b6b6042SThierry Reding 
3089542c237SThierry Reding 	err = drm_dp_aux_train(sor->aux, link, pattern);
3096b6b6042SThierry Reding 	if (err < 0)
3106b6b6042SThierry Reding 		return err;
3116b6b6042SThierry Reding 
312a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_SPARE0);
3136b6b6042SThierry Reding 	value |= SOR_DP_SPARE_SEQ_ENABLE;
3146b6b6042SThierry Reding 	value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
3156b6b6042SThierry Reding 	value |= SOR_DP_SPARE_MACRO_SOR_CLK;
316a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_SPARE0);
3176b6b6042SThierry Reding 
3186b6b6042SThierry Reding 	for (i = 0, value = 0; i < link->num_lanes; i++) {
3196b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
3206b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_NONE |
3216b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_TRAIN2;
3226b6b6042SThierry Reding 		value = (value << 8) | lane;
3236b6b6042SThierry Reding 	}
3246b6b6042SThierry Reding 
3256b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
3266b6b6042SThierry Reding 
3276b6b6042SThierry Reding 	pattern = DP_LINK_SCRAMBLING_DISABLE | DP_TRAINING_PATTERN_2;
3286b6b6042SThierry Reding 
3299542c237SThierry Reding 	err = drm_dp_aux_train(sor->aux, link, pattern);
3306b6b6042SThierry Reding 	if (err < 0)
3316b6b6042SThierry Reding 		return err;
3326b6b6042SThierry Reding 
3336b6b6042SThierry Reding 	for (i = 0, value = 0; i < link->num_lanes; i++) {
3346b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
3356b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_GALIOS |
3366b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_NONE;
3376b6b6042SThierry Reding 		value = (value << 8) | lane;
3386b6b6042SThierry Reding 	}
3396b6b6042SThierry Reding 
3406b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
3416b6b6042SThierry Reding 
3426b6b6042SThierry Reding 	pattern = DP_TRAINING_PATTERN_DISABLE;
3436b6b6042SThierry Reding 
3449542c237SThierry Reding 	err = drm_dp_aux_train(sor->aux, link, pattern);
3456b6b6042SThierry Reding 	if (err < 0)
3466b6b6042SThierry Reding 		return err;
3476b6b6042SThierry Reding 
3486b6b6042SThierry Reding 	return 0;
3496b6b6042SThierry Reding }
3506b6b6042SThierry Reding 
351459cc2c6SThierry Reding static void tegra_sor_dp_term_calibrate(struct tegra_sor *sor)
352459cc2c6SThierry Reding {
353459cc2c6SThierry Reding 	u32 mask = 0x08, adj = 0, value;
354459cc2c6SThierry Reding 
355459cc2c6SThierry Reding 	/* enable pad calibration logic */
356459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
357459cc2c6SThierry Reding 	value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
358459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
359459cc2c6SThierry Reding 
360459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL1);
361459cc2c6SThierry Reding 	value |= SOR_PLL1_TMDS_TERM;
362459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL1);
363459cc2c6SThierry Reding 
364459cc2c6SThierry Reding 	while (mask) {
365459cc2c6SThierry Reding 		adj |= mask;
366459cc2c6SThierry Reding 
367459cc2c6SThierry Reding 		value = tegra_sor_readl(sor, SOR_PLL1);
368459cc2c6SThierry Reding 		value &= ~SOR_PLL1_TMDS_TERMADJ_MASK;
369459cc2c6SThierry Reding 		value |= SOR_PLL1_TMDS_TERMADJ(adj);
370459cc2c6SThierry Reding 		tegra_sor_writel(sor, value, SOR_PLL1);
371459cc2c6SThierry Reding 
372459cc2c6SThierry Reding 		usleep_range(100, 200);
373459cc2c6SThierry Reding 
374459cc2c6SThierry Reding 		value = tegra_sor_readl(sor, SOR_PLL1);
375459cc2c6SThierry Reding 		if (value & SOR_PLL1_TERM_COMPOUT)
376459cc2c6SThierry Reding 			adj &= ~mask;
377459cc2c6SThierry Reding 
378459cc2c6SThierry Reding 		mask >>= 1;
379459cc2c6SThierry Reding 	}
380459cc2c6SThierry Reding 
381459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL1);
382459cc2c6SThierry Reding 	value &= ~SOR_PLL1_TMDS_TERMADJ_MASK;
383459cc2c6SThierry Reding 	value |= SOR_PLL1_TMDS_TERMADJ(adj);
384459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL1);
385459cc2c6SThierry Reding 
386459cc2c6SThierry Reding 	/* disable pad calibration logic */
387459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
388459cc2c6SThierry Reding 	value |= SOR_DP_PADCTL_PAD_CAL_PD;
389459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
390459cc2c6SThierry Reding }
391459cc2c6SThierry Reding 
3926b6b6042SThierry Reding static void tegra_sor_super_update(struct tegra_sor *sor)
3936b6b6042SThierry Reding {
394a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
395a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 1, SOR_SUPER_STATE0);
396a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
3976b6b6042SThierry Reding }
3986b6b6042SThierry Reding 
3996b6b6042SThierry Reding static void tegra_sor_update(struct tegra_sor *sor)
4006b6b6042SThierry Reding {
401a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE0);
402a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 1, SOR_STATE0);
403a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE0);
4046b6b6042SThierry Reding }
4056b6b6042SThierry Reding 
4066b6b6042SThierry Reding static int tegra_sor_setup_pwm(struct tegra_sor *sor, unsigned long timeout)
4076b6b6042SThierry Reding {
40828fe2076SThierry Reding 	u32 value;
4096b6b6042SThierry Reding 
4106b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWM_DIV);
4116b6b6042SThierry Reding 	value &= ~SOR_PWM_DIV_MASK;
4126b6b6042SThierry Reding 	value |= 0x400; /* period */
4136b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWM_DIV);
4146b6b6042SThierry Reding 
4156b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWM_CTL);
4166b6b6042SThierry Reding 	value &= ~SOR_PWM_CTL_DUTY_CYCLE_MASK;
4176b6b6042SThierry Reding 	value |= 0x400; /* duty cycle */
4186b6b6042SThierry Reding 	value &= ~SOR_PWM_CTL_CLK_SEL; /* clock source: PCLK */
4196b6b6042SThierry Reding 	value |= SOR_PWM_CTL_TRIGGER;
4206b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWM_CTL);
4216b6b6042SThierry Reding 
4226b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
4236b6b6042SThierry Reding 
4246b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
4256b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWM_CTL);
4266b6b6042SThierry Reding 		if ((value & SOR_PWM_CTL_TRIGGER) == 0)
4276b6b6042SThierry Reding 			return 0;
4286b6b6042SThierry Reding 
4296b6b6042SThierry Reding 		usleep_range(25, 100);
4306b6b6042SThierry Reding 	}
4316b6b6042SThierry Reding 
4326b6b6042SThierry Reding 	return -ETIMEDOUT;
4336b6b6042SThierry Reding }
4346b6b6042SThierry Reding 
4356b6b6042SThierry Reding static int tegra_sor_attach(struct tegra_sor *sor)
4366b6b6042SThierry Reding {
4376b6b6042SThierry Reding 	unsigned long value, timeout;
4386b6b6042SThierry Reding 
4396b6b6042SThierry Reding 	/* wake up in normal mode */
440a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
4416b6b6042SThierry Reding 	value |= SOR_SUPER_STATE_HEAD_MODE_AWAKE;
4426b6b6042SThierry Reding 	value |= SOR_SUPER_STATE_MODE_NORMAL;
443a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
4446b6b6042SThierry Reding 	tegra_sor_super_update(sor);
4456b6b6042SThierry Reding 
4466b6b6042SThierry Reding 	/* attach */
447a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
4486b6b6042SThierry Reding 	value |= SOR_SUPER_STATE_ATTACHED;
449a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
4506b6b6042SThierry Reding 	tegra_sor_super_update(sor);
4516b6b6042SThierry Reding 
4526b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
4536b6b6042SThierry Reding 
4546b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
4556b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_TEST);
4566b6b6042SThierry Reding 		if ((value & SOR_TEST_ATTACHED) != 0)
4576b6b6042SThierry Reding 			return 0;
4586b6b6042SThierry Reding 
4596b6b6042SThierry Reding 		usleep_range(25, 100);
4606b6b6042SThierry Reding 	}
4616b6b6042SThierry Reding 
4626b6b6042SThierry Reding 	return -ETIMEDOUT;
4636b6b6042SThierry Reding }
4646b6b6042SThierry Reding 
4656b6b6042SThierry Reding static int tegra_sor_wakeup(struct tegra_sor *sor)
4666b6b6042SThierry Reding {
4676b6b6042SThierry Reding 	unsigned long value, timeout;
4686b6b6042SThierry Reding 
4696b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
4706b6b6042SThierry Reding 
4716b6b6042SThierry Reding 	/* wait for head to wake up */
4726b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
4736b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_TEST);
4746b6b6042SThierry Reding 		value &= SOR_TEST_HEAD_MODE_MASK;
4756b6b6042SThierry Reding 
4766b6b6042SThierry Reding 		if (value == SOR_TEST_HEAD_MODE_AWAKE)
4776b6b6042SThierry Reding 			return 0;
4786b6b6042SThierry Reding 
4796b6b6042SThierry Reding 		usleep_range(25, 100);
4806b6b6042SThierry Reding 	}
4816b6b6042SThierry Reding 
4826b6b6042SThierry Reding 	return -ETIMEDOUT;
4836b6b6042SThierry Reding }
4846b6b6042SThierry Reding 
4856b6b6042SThierry Reding static int tegra_sor_power_up(struct tegra_sor *sor, unsigned long timeout)
4866b6b6042SThierry Reding {
48728fe2076SThierry Reding 	u32 value;
4886b6b6042SThierry Reding 
4896b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWR);
4906b6b6042SThierry Reding 	value |= SOR_PWR_TRIGGER | SOR_PWR_NORMAL_STATE_PU;
4916b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWR);
4926b6b6042SThierry Reding 
4936b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
4946b6b6042SThierry Reding 
4956b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
4966b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWR);
4976b6b6042SThierry Reding 		if ((value & SOR_PWR_TRIGGER) == 0)
4986b6b6042SThierry Reding 			return 0;
4996b6b6042SThierry Reding 
5006b6b6042SThierry Reding 		usleep_range(25, 100);
5016b6b6042SThierry Reding 	}
5026b6b6042SThierry Reding 
5036b6b6042SThierry Reding 	return -ETIMEDOUT;
5046b6b6042SThierry Reding }
5056b6b6042SThierry Reding 
50634fa183bSThierry Reding struct tegra_sor_params {
50734fa183bSThierry Reding 	/* number of link clocks per line */
50834fa183bSThierry Reding 	unsigned int num_clocks;
50934fa183bSThierry Reding 	/* ratio between input and output */
51034fa183bSThierry Reding 	u64 ratio;
51134fa183bSThierry Reding 	/* precision factor */
51234fa183bSThierry Reding 	u64 precision;
51334fa183bSThierry Reding 
51434fa183bSThierry Reding 	unsigned int active_polarity;
51534fa183bSThierry Reding 	unsigned int active_count;
51634fa183bSThierry Reding 	unsigned int active_frac;
51734fa183bSThierry Reding 	unsigned int tu_size;
51834fa183bSThierry Reding 	unsigned int error;
51934fa183bSThierry Reding };
52034fa183bSThierry Reding 
52134fa183bSThierry Reding static int tegra_sor_compute_params(struct tegra_sor *sor,
52234fa183bSThierry Reding 				    struct tegra_sor_params *params,
52334fa183bSThierry Reding 				    unsigned int tu_size)
52434fa183bSThierry Reding {
52534fa183bSThierry Reding 	u64 active_sym, active_count, frac, approx;
52634fa183bSThierry Reding 	u32 active_polarity, active_frac = 0;
52734fa183bSThierry Reding 	const u64 f = params->precision;
52834fa183bSThierry Reding 	s64 error;
52934fa183bSThierry Reding 
53034fa183bSThierry Reding 	active_sym = params->ratio * tu_size;
53134fa183bSThierry Reding 	active_count = div_u64(active_sym, f) * f;
53234fa183bSThierry Reding 	frac = active_sym - active_count;
53334fa183bSThierry Reding 
53434fa183bSThierry Reding 	/* fraction < 0.5 */
53534fa183bSThierry Reding 	if (frac >= (f / 2)) {
53634fa183bSThierry Reding 		active_polarity = 1;
53734fa183bSThierry Reding 		frac = f - frac;
53834fa183bSThierry Reding 	} else {
53934fa183bSThierry Reding 		active_polarity = 0;
54034fa183bSThierry Reding 	}
54134fa183bSThierry Reding 
54234fa183bSThierry Reding 	if (frac != 0) {
54334fa183bSThierry Reding 		frac = div_u64(f * f,  frac); /* 1/fraction */
54434fa183bSThierry Reding 		if (frac <= (15 * f)) {
54534fa183bSThierry Reding 			active_frac = div_u64(frac, f);
54634fa183bSThierry Reding 
54734fa183bSThierry Reding 			/* round up */
54834fa183bSThierry Reding 			if (active_polarity)
54934fa183bSThierry Reding 				active_frac++;
55034fa183bSThierry Reding 		} else {
55134fa183bSThierry Reding 			active_frac = active_polarity ? 1 : 15;
55234fa183bSThierry Reding 		}
55334fa183bSThierry Reding 	}
55434fa183bSThierry Reding 
55534fa183bSThierry Reding 	if (active_frac == 1)
55634fa183bSThierry Reding 		active_polarity = 0;
55734fa183bSThierry Reding 
55834fa183bSThierry Reding 	if (active_polarity == 1) {
55934fa183bSThierry Reding 		if (active_frac) {
56034fa183bSThierry Reding 			approx = active_count + (active_frac * (f - 1)) * f;
56134fa183bSThierry Reding 			approx = div_u64(approx, active_frac * f);
56234fa183bSThierry Reding 		} else {
56334fa183bSThierry Reding 			approx = active_count + f;
56434fa183bSThierry Reding 		}
56534fa183bSThierry Reding 	} else {
56634fa183bSThierry Reding 		if (active_frac)
56734fa183bSThierry Reding 			approx = active_count + div_u64(f, active_frac);
56834fa183bSThierry Reding 		else
56934fa183bSThierry Reding 			approx = active_count;
57034fa183bSThierry Reding 	}
57134fa183bSThierry Reding 
57234fa183bSThierry Reding 	error = div_s64(active_sym - approx, tu_size);
57334fa183bSThierry Reding 	error *= params->num_clocks;
57434fa183bSThierry Reding 
57579211c8eSAndrew Morton 	if (error <= 0 && abs(error) < params->error) {
57634fa183bSThierry Reding 		params->active_count = div_u64(active_count, f);
57734fa183bSThierry Reding 		params->active_polarity = active_polarity;
57834fa183bSThierry Reding 		params->active_frac = active_frac;
57979211c8eSAndrew Morton 		params->error = abs(error);
58034fa183bSThierry Reding 		params->tu_size = tu_size;
58134fa183bSThierry Reding 
58234fa183bSThierry Reding 		if (error == 0)
58334fa183bSThierry Reding 			return true;
58434fa183bSThierry Reding 	}
58534fa183bSThierry Reding 
58634fa183bSThierry Reding 	return false;
58734fa183bSThierry Reding }
58834fa183bSThierry Reding 
589*a198359eSThierry Reding static int tegra_sor_compute_config(struct tegra_sor *sor,
59080444495SThierry Reding 				    const struct drm_display_mode *mode,
59134fa183bSThierry Reding 				    struct tegra_sor_config *config,
59234fa183bSThierry Reding 				    struct drm_dp_link *link)
59334fa183bSThierry Reding {
59434fa183bSThierry Reding 	const u64 f = 100000, link_rate = link->rate * 1000;
59534fa183bSThierry Reding 	const u64 pclk = mode->clock * 1000;
5967890b576SThierry Reding 	u64 input, output, watermark, num;
59734fa183bSThierry Reding 	struct tegra_sor_params params;
59834fa183bSThierry Reding 	u32 num_syms_per_line;
59934fa183bSThierry Reding 	unsigned int i;
60034fa183bSThierry Reding 
60134fa183bSThierry Reding 	if (!link_rate || !link->num_lanes || !pclk || !config->bits_per_pixel)
60234fa183bSThierry Reding 		return -EINVAL;
60334fa183bSThierry Reding 
60434fa183bSThierry Reding 	output = link_rate * 8 * link->num_lanes;
60534fa183bSThierry Reding 	input = pclk * config->bits_per_pixel;
60634fa183bSThierry Reding 
60734fa183bSThierry Reding 	if (input >= output)
60834fa183bSThierry Reding 		return -ERANGE;
60934fa183bSThierry Reding 
61034fa183bSThierry Reding 	memset(&params, 0, sizeof(params));
61134fa183bSThierry Reding 	params.ratio = div64_u64(input * f, output);
61234fa183bSThierry Reding 	params.num_clocks = div_u64(link_rate * mode->hdisplay, pclk);
61334fa183bSThierry Reding 	params.precision = f;
61434fa183bSThierry Reding 	params.error = 64 * f;
61534fa183bSThierry Reding 	params.tu_size = 64;
61634fa183bSThierry Reding 
61734fa183bSThierry Reding 	for (i = params.tu_size; i >= 32; i--)
61834fa183bSThierry Reding 		if (tegra_sor_compute_params(sor, &params, i))
61934fa183bSThierry Reding 			break;
62034fa183bSThierry Reding 
62134fa183bSThierry Reding 	if (params.active_frac == 0) {
62234fa183bSThierry Reding 		config->active_polarity = 0;
62334fa183bSThierry Reding 		config->active_count = params.active_count;
62434fa183bSThierry Reding 
62534fa183bSThierry Reding 		if (!params.active_polarity)
62634fa183bSThierry Reding 			config->active_count--;
62734fa183bSThierry Reding 
62834fa183bSThierry Reding 		config->tu_size = params.tu_size;
62934fa183bSThierry Reding 		config->active_frac = 1;
63034fa183bSThierry Reding 	} else {
63134fa183bSThierry Reding 		config->active_polarity = params.active_polarity;
63234fa183bSThierry Reding 		config->active_count = params.active_count;
63334fa183bSThierry Reding 		config->active_frac = params.active_frac;
63434fa183bSThierry Reding 		config->tu_size = params.tu_size;
63534fa183bSThierry Reding 	}
63634fa183bSThierry Reding 
63734fa183bSThierry Reding 	dev_dbg(sor->dev,
63834fa183bSThierry Reding 		"polarity: %d active count: %d tu size: %d active frac: %d\n",
63934fa183bSThierry Reding 		config->active_polarity, config->active_count,
64034fa183bSThierry Reding 		config->tu_size, config->active_frac);
64134fa183bSThierry Reding 
64234fa183bSThierry Reding 	watermark = params.ratio * config->tu_size * (f - params.ratio);
64334fa183bSThierry Reding 	watermark = div_u64(watermark, f);
64434fa183bSThierry Reding 
64534fa183bSThierry Reding 	watermark = div_u64(watermark + params.error, f);
64634fa183bSThierry Reding 	config->watermark = watermark + (config->bits_per_pixel / 8) + 2;
64734fa183bSThierry Reding 	num_syms_per_line = (mode->hdisplay * config->bits_per_pixel) *
64834fa183bSThierry Reding 			    (link->num_lanes * 8);
64934fa183bSThierry Reding 
65034fa183bSThierry Reding 	if (config->watermark > 30) {
65134fa183bSThierry Reding 		config->watermark = 30;
65234fa183bSThierry Reding 		dev_err(sor->dev,
65334fa183bSThierry Reding 			"unable to compute TU size, forcing watermark to %u\n",
65434fa183bSThierry Reding 			config->watermark);
65534fa183bSThierry Reding 	} else if (config->watermark > num_syms_per_line) {
65634fa183bSThierry Reding 		config->watermark = num_syms_per_line;
65734fa183bSThierry Reding 		dev_err(sor->dev, "watermark too high, forcing to %u\n",
65834fa183bSThierry Reding 			config->watermark);
65934fa183bSThierry Reding 	}
66034fa183bSThierry Reding 
6617890b576SThierry Reding 	/* compute the number of symbols per horizontal blanking interval */
6627890b576SThierry Reding 	num = ((mode->htotal - mode->hdisplay) - 7) * link_rate;
6637890b576SThierry Reding 	config->hblank_symbols = div_u64(num, pclk);
6647890b576SThierry Reding 
6657890b576SThierry Reding 	if (link->capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
6667890b576SThierry Reding 		config->hblank_symbols -= 3;
6677890b576SThierry Reding 
6687890b576SThierry Reding 	config->hblank_symbols -= 12 / link->num_lanes;
6697890b576SThierry Reding 
6707890b576SThierry Reding 	/* compute the number of symbols per vertical blanking interval */
6717890b576SThierry Reding 	num = (mode->hdisplay - 25) * link_rate;
6727890b576SThierry Reding 	config->vblank_symbols = div_u64(num, pclk);
6737890b576SThierry Reding 	config->vblank_symbols -= 36 / link->num_lanes + 4;
6747890b576SThierry Reding 
6757890b576SThierry Reding 	dev_dbg(sor->dev, "blank symbols: H:%u V:%u\n", config->hblank_symbols,
6767890b576SThierry Reding 		config->vblank_symbols);
6777890b576SThierry Reding 
67834fa183bSThierry Reding 	return 0;
67934fa183bSThierry Reding }
68034fa183bSThierry Reding 
6816fad8f66SThierry Reding static int tegra_sor_detach(struct tegra_sor *sor)
6826b6b6042SThierry Reding {
6836fad8f66SThierry Reding 	unsigned long value, timeout;
6846fad8f66SThierry Reding 
6856fad8f66SThierry Reding 	/* switch to safe mode */
686a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
6876fad8f66SThierry Reding 	value &= ~SOR_SUPER_STATE_MODE_NORMAL;
688a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
6896fad8f66SThierry Reding 	tegra_sor_super_update(sor);
6906fad8f66SThierry Reding 
6916fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
6926fad8f66SThierry Reding 
6936fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
6946fad8f66SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWR);
6956fad8f66SThierry Reding 		if (value & SOR_PWR_MODE_SAFE)
6966fad8f66SThierry Reding 			break;
6976fad8f66SThierry Reding 	}
6986fad8f66SThierry Reding 
6996fad8f66SThierry Reding 	if ((value & SOR_PWR_MODE_SAFE) == 0)
7006fad8f66SThierry Reding 		return -ETIMEDOUT;
7016fad8f66SThierry Reding 
7026fad8f66SThierry Reding 	/* go to sleep */
703a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
7046fad8f66SThierry Reding 	value &= ~SOR_SUPER_STATE_HEAD_MODE_MASK;
705a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
7066fad8f66SThierry Reding 	tegra_sor_super_update(sor);
7076fad8f66SThierry Reding 
7086fad8f66SThierry Reding 	/* detach */
709a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
7106fad8f66SThierry Reding 	value &= ~SOR_SUPER_STATE_ATTACHED;
711a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
7126fad8f66SThierry Reding 	tegra_sor_super_update(sor);
7136fad8f66SThierry Reding 
7146fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
7156fad8f66SThierry Reding 
7166fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
7176fad8f66SThierry Reding 		value = tegra_sor_readl(sor, SOR_TEST);
7186fad8f66SThierry Reding 		if ((value & SOR_TEST_ATTACHED) == 0)
7196fad8f66SThierry Reding 			break;
7206fad8f66SThierry Reding 
7216fad8f66SThierry Reding 		usleep_range(25, 100);
7226fad8f66SThierry Reding 	}
7236fad8f66SThierry Reding 
7246fad8f66SThierry Reding 	if ((value & SOR_TEST_ATTACHED) != 0)
7256fad8f66SThierry Reding 		return -ETIMEDOUT;
7266fad8f66SThierry Reding 
7276fad8f66SThierry Reding 	return 0;
7286fad8f66SThierry Reding }
7296fad8f66SThierry Reding 
7306fad8f66SThierry Reding static int tegra_sor_power_down(struct tegra_sor *sor)
7316fad8f66SThierry Reding {
7326fad8f66SThierry Reding 	unsigned long value, timeout;
7336fad8f66SThierry Reding 	int err;
7346fad8f66SThierry Reding 
7356fad8f66SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWR);
7366fad8f66SThierry Reding 	value &= ~SOR_PWR_NORMAL_STATE_PU;
7376fad8f66SThierry Reding 	value |= SOR_PWR_TRIGGER;
7386fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWR);
7396fad8f66SThierry Reding 
7406fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
7416fad8f66SThierry Reding 
7426fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
7436fad8f66SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWR);
7446fad8f66SThierry Reding 		if ((value & SOR_PWR_TRIGGER) == 0)
7456fad8f66SThierry Reding 			return 0;
7466fad8f66SThierry Reding 
7476fad8f66SThierry Reding 		usleep_range(25, 100);
7486fad8f66SThierry Reding 	}
7496fad8f66SThierry Reding 
7506fad8f66SThierry Reding 	if ((value & SOR_PWR_TRIGGER) != 0)
7516fad8f66SThierry Reding 		return -ETIMEDOUT;
7526fad8f66SThierry Reding 
75325bb2cecSThierry Reding 	/* switch to safe parent clock */
75425bb2cecSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
7556fad8f66SThierry Reding 	if (err < 0)
7566fad8f66SThierry Reding 		dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
7576fad8f66SThierry Reding 
758a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
7596fad8f66SThierry Reding 	value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
7606fad8f66SThierry Reding 		   SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2);
761a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
7626fad8f66SThierry Reding 
7636fad8f66SThierry Reding 	/* stop lane sequencer */
7646fad8f66SThierry Reding 	value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_UP |
7656fad8f66SThierry Reding 		SOR_LANE_SEQ_CTL_POWER_STATE_DOWN;
7666fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
7676fad8f66SThierry Reding 
7686fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
7696fad8f66SThierry Reding 
7706fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
7716fad8f66SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
7726fad8f66SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
7736fad8f66SThierry Reding 			break;
7746fad8f66SThierry Reding 
7756fad8f66SThierry Reding 		usleep_range(25, 100);
7766fad8f66SThierry Reding 	}
7776fad8f66SThierry Reding 
7786fad8f66SThierry Reding 	if ((value & SOR_LANE_SEQ_CTL_TRIGGER) != 0)
7796fad8f66SThierry Reding 		return -ETIMEDOUT;
7806fad8f66SThierry Reding 
781a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
782a9a9e4fdSThierry Reding 	value |= SOR_PLL2_PORT_POWERDOWN;
783a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
7846fad8f66SThierry Reding 
7856fad8f66SThierry Reding 	usleep_range(20, 100);
7866fad8f66SThierry Reding 
787a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL0);
788a9a9e4fdSThierry Reding 	value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
789a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL0);
7906fad8f66SThierry Reding 
791a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
792a9a9e4fdSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD;
793a9a9e4fdSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
794a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
7956fad8f66SThierry Reding 
7966fad8f66SThierry Reding 	usleep_range(20, 100);
7976fad8f66SThierry Reding 
7986fad8f66SThierry Reding 	return 0;
7996fad8f66SThierry Reding }
8006fad8f66SThierry Reding 
8016fad8f66SThierry Reding static int tegra_sor_crc_wait(struct tegra_sor *sor, unsigned long timeout)
8026fad8f66SThierry Reding {
8036fad8f66SThierry Reding 	u32 value;
8046fad8f66SThierry Reding 
8056fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
8066fad8f66SThierry Reding 
8076fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
808a9a9e4fdSThierry Reding 		value = tegra_sor_readl(sor, SOR_CRCA);
809a9a9e4fdSThierry Reding 		if (value & SOR_CRCA_VALID)
8106fad8f66SThierry Reding 			return 0;
8116fad8f66SThierry Reding 
8126fad8f66SThierry Reding 		usleep_range(100, 200);
8136fad8f66SThierry Reding 	}
8146fad8f66SThierry Reding 
8156fad8f66SThierry Reding 	return -ETIMEDOUT;
8166fad8f66SThierry Reding }
8176fad8f66SThierry Reding 
818530239a8SThierry Reding static int tegra_sor_show_crc(struct seq_file *s, void *data)
8196fad8f66SThierry Reding {
820530239a8SThierry Reding 	struct drm_info_node *node = s->private;
821530239a8SThierry Reding 	struct tegra_sor *sor = node->info_ent->data;
822850bab44SThierry Reding 	struct drm_crtc *crtc = sor->output.encoder.crtc;
823850bab44SThierry Reding 	struct drm_device *drm = node->minor->dev;
824530239a8SThierry Reding 	int err = 0;
8256fad8f66SThierry Reding 	u32 value;
8266fad8f66SThierry Reding 
827850bab44SThierry Reding 	drm_modeset_lock_all(drm);
8286fad8f66SThierry Reding 
829850bab44SThierry Reding 	if (!crtc || !crtc->state->active) {
830850bab44SThierry Reding 		err = -EBUSY;
8316fad8f66SThierry Reding 		goto unlock;
8326fad8f66SThierry Reding 	}
8336fad8f66SThierry Reding 
834a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
8356fad8f66SThierry Reding 	value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
836a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
8376fad8f66SThierry Reding 
8386fad8f66SThierry Reding 	value = tegra_sor_readl(sor, SOR_CRC_CNTRL);
8396fad8f66SThierry Reding 	value |= SOR_CRC_CNTRL_ENABLE;
8406fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_CRC_CNTRL);
8416fad8f66SThierry Reding 
8426fad8f66SThierry Reding 	value = tegra_sor_readl(sor, SOR_TEST);
8436fad8f66SThierry Reding 	value &= ~SOR_TEST_CRC_POST_SERIALIZE;
8446fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_TEST);
8456fad8f66SThierry Reding 
8466fad8f66SThierry Reding 	err = tegra_sor_crc_wait(sor, 100);
8476fad8f66SThierry Reding 	if (err < 0)
8486fad8f66SThierry Reding 		goto unlock;
8496fad8f66SThierry Reding 
850a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, SOR_CRCA_RESET, SOR_CRCA);
851a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_CRCB);
8526fad8f66SThierry Reding 
853530239a8SThierry Reding 	seq_printf(s, "%08x\n", value);
8546fad8f66SThierry Reding 
8556fad8f66SThierry Reding unlock:
856850bab44SThierry Reding 	drm_modeset_unlock_all(drm);
8576fad8f66SThierry Reding 	return err;
8586fad8f66SThierry Reding }
8596fad8f66SThierry Reding 
860dab16336SThierry Reding static int tegra_sor_show_regs(struct seq_file *s, void *data)
861dab16336SThierry Reding {
862dab16336SThierry Reding 	struct drm_info_node *node = s->private;
863dab16336SThierry Reding 	struct tegra_sor *sor = node->info_ent->data;
864850bab44SThierry Reding 	struct drm_crtc *crtc = sor->output.encoder.crtc;
865850bab44SThierry Reding 	struct drm_device *drm = node->minor->dev;
866850bab44SThierry Reding 	int err = 0;
867850bab44SThierry Reding 
868850bab44SThierry Reding 	drm_modeset_lock_all(drm);
869850bab44SThierry Reding 
870850bab44SThierry Reding 	if (!crtc || !crtc->state->active) {
871850bab44SThierry Reding 		err = -EBUSY;
872850bab44SThierry Reding 		goto unlock;
873850bab44SThierry Reding 	}
874dab16336SThierry Reding 
875dab16336SThierry Reding #define DUMP_REG(name)						\
876dab16336SThierry Reding 	seq_printf(s, "%-38s %#05x %08x\n", #name, name,	\
877dab16336SThierry Reding 		   tegra_sor_readl(sor, name))
878dab16336SThierry Reding 
879dab16336SThierry Reding 	DUMP_REG(SOR_CTXSW);
880a9a9e4fdSThierry Reding 	DUMP_REG(SOR_SUPER_STATE0);
881a9a9e4fdSThierry Reding 	DUMP_REG(SOR_SUPER_STATE1);
882a9a9e4fdSThierry Reding 	DUMP_REG(SOR_STATE0);
883a9a9e4fdSThierry Reding 	DUMP_REG(SOR_STATE1);
884a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE0(0));
885a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE0(1));
886a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE1(0));
887a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE1(1));
888a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE2(0));
889a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE2(1));
890a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE3(0));
891a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE3(1));
892a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE4(0));
893a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE4(1));
894a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE5(0));
895a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE5(1));
896dab16336SThierry Reding 	DUMP_REG(SOR_CRC_CNTRL);
897dab16336SThierry Reding 	DUMP_REG(SOR_DP_DEBUG_MVID);
898dab16336SThierry Reding 	DUMP_REG(SOR_CLK_CNTRL);
899dab16336SThierry Reding 	DUMP_REG(SOR_CAP);
900dab16336SThierry Reding 	DUMP_REG(SOR_PWR);
901dab16336SThierry Reding 	DUMP_REG(SOR_TEST);
902a9a9e4fdSThierry Reding 	DUMP_REG(SOR_PLL0);
903a9a9e4fdSThierry Reding 	DUMP_REG(SOR_PLL1);
904a9a9e4fdSThierry Reding 	DUMP_REG(SOR_PLL2);
905a9a9e4fdSThierry Reding 	DUMP_REG(SOR_PLL3);
906dab16336SThierry Reding 	DUMP_REG(SOR_CSTM);
907dab16336SThierry Reding 	DUMP_REG(SOR_LVDS);
908a9a9e4fdSThierry Reding 	DUMP_REG(SOR_CRCA);
909a9a9e4fdSThierry Reding 	DUMP_REG(SOR_CRCB);
910dab16336SThierry Reding 	DUMP_REG(SOR_BLANK);
911dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_CTL);
912dab16336SThierry Reding 	DUMP_REG(SOR_LANE_SEQ_CTL);
913dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(0));
914dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(1));
915dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(2));
916dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(3));
917dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(4));
918dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(5));
919dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(6));
920dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(7));
921dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(8));
922dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(9));
923dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(10));
924dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(11));
925dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(12));
926dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(13));
927dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(14));
928dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(15));
929dab16336SThierry Reding 	DUMP_REG(SOR_PWM_DIV);
930dab16336SThierry Reding 	DUMP_REG(SOR_PWM_CTL);
931a9a9e4fdSThierry Reding 	DUMP_REG(SOR_VCRC_A0);
932a9a9e4fdSThierry Reding 	DUMP_REG(SOR_VCRC_A1);
933a9a9e4fdSThierry Reding 	DUMP_REG(SOR_VCRC_B0);
934a9a9e4fdSThierry Reding 	DUMP_REG(SOR_VCRC_B1);
935a9a9e4fdSThierry Reding 	DUMP_REG(SOR_CCRC_A0);
936a9a9e4fdSThierry Reding 	DUMP_REG(SOR_CCRC_A1);
937a9a9e4fdSThierry Reding 	DUMP_REG(SOR_CCRC_B0);
938a9a9e4fdSThierry Reding 	DUMP_REG(SOR_CCRC_B1);
939a9a9e4fdSThierry Reding 	DUMP_REG(SOR_EDATA_A0);
940a9a9e4fdSThierry Reding 	DUMP_REG(SOR_EDATA_A1);
941a9a9e4fdSThierry Reding 	DUMP_REG(SOR_EDATA_B0);
942a9a9e4fdSThierry Reding 	DUMP_REG(SOR_EDATA_B1);
943a9a9e4fdSThierry Reding 	DUMP_REG(SOR_COUNT_A0);
944a9a9e4fdSThierry Reding 	DUMP_REG(SOR_COUNT_A1);
945a9a9e4fdSThierry Reding 	DUMP_REG(SOR_COUNT_B0);
946a9a9e4fdSThierry Reding 	DUMP_REG(SOR_COUNT_B1);
947a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DEBUG_A0);
948a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DEBUG_A1);
949a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DEBUG_B0);
950a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DEBUG_B1);
951dab16336SThierry Reding 	DUMP_REG(SOR_TRIG);
952dab16336SThierry Reding 	DUMP_REG(SOR_MSCHECK);
953dab16336SThierry Reding 	DUMP_REG(SOR_XBAR_CTRL);
954dab16336SThierry Reding 	DUMP_REG(SOR_XBAR_POL);
955a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_LINKCTL0);
956a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_LINKCTL1);
957a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE_DRIVE_CURRENT0);
958a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE_DRIVE_CURRENT1);
959a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE4_DRIVE_CURRENT0);
960a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE4_DRIVE_CURRENT1);
961a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE_PREEMPHASIS0);
962a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE_PREEMPHASIS1);
963a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE4_PREEMPHASIS0);
964a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE4_PREEMPHASIS1);
965a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE_POSTCURSOR0);
966a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE_POSTCURSOR1);
967a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_CONFIG0);
968a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_CONFIG1);
969a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_MN0);
970a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_MN1);
971a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_PADCTL0);
972a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_PADCTL1);
973a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_DEBUG0);
974a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_DEBUG1);
975a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_SPARE0);
976a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_SPARE1);
977dab16336SThierry Reding 	DUMP_REG(SOR_DP_AUDIO_CTRL);
978dab16336SThierry Reding 	DUMP_REG(SOR_DP_AUDIO_HBLANK_SYMBOLS);
979dab16336SThierry Reding 	DUMP_REG(SOR_DP_AUDIO_VBLANK_SYMBOLS);
980dab16336SThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_HEADER);
981a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK0);
982a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK1);
983a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK2);
984a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK3);
985a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK4);
986a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK5);
987a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK6);
988dab16336SThierry Reding 	DUMP_REG(SOR_DP_TPG);
989dab16336SThierry Reding 	DUMP_REG(SOR_DP_TPG_CONFIG);
990a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_LQ_CSTM0);
991a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_LQ_CSTM1);
992a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_LQ_CSTM2);
993dab16336SThierry Reding 
994dab16336SThierry Reding #undef DUMP_REG
995dab16336SThierry Reding 
996850bab44SThierry Reding unlock:
997850bab44SThierry Reding 	drm_modeset_unlock_all(drm);
998850bab44SThierry Reding 	return err;
999dab16336SThierry Reding }
1000dab16336SThierry Reding 
1001dab16336SThierry Reding static const struct drm_info_list debugfs_files[] = {
1002530239a8SThierry Reding 	{ "crc", tegra_sor_show_crc, 0, NULL },
1003dab16336SThierry Reding 	{ "regs", tegra_sor_show_regs, 0, NULL },
1004dab16336SThierry Reding };
1005dab16336SThierry Reding 
10066fad8f66SThierry Reding static int tegra_sor_debugfs_init(struct tegra_sor *sor,
10076fad8f66SThierry Reding 				  struct drm_minor *minor)
10086fad8f66SThierry Reding {
1009459cc2c6SThierry Reding 	const char *name = sor->soc->supports_dp ? "sor1" : "sor";
1010dab16336SThierry Reding 	unsigned int i;
1011530239a8SThierry Reding 	int err;
10126fad8f66SThierry Reding 
1013459cc2c6SThierry Reding 	sor->debugfs = debugfs_create_dir(name, minor->debugfs_root);
10146fad8f66SThierry Reding 	if (!sor->debugfs)
10156fad8f66SThierry Reding 		return -ENOMEM;
10166fad8f66SThierry Reding 
1017dab16336SThierry Reding 	sor->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1018dab16336SThierry Reding 				     GFP_KERNEL);
1019dab16336SThierry Reding 	if (!sor->debugfs_files) {
10206fad8f66SThierry Reding 		err = -ENOMEM;
10216fad8f66SThierry Reding 		goto remove;
10226fad8f66SThierry Reding 	}
10236fad8f66SThierry Reding 
1024dab16336SThierry Reding 	for (i = 0; i < ARRAY_SIZE(debugfs_files); i++)
1025dab16336SThierry Reding 		sor->debugfs_files[i].data = sor;
1026dab16336SThierry Reding 
1027dab16336SThierry Reding 	err = drm_debugfs_create_files(sor->debugfs_files,
1028dab16336SThierry Reding 				       ARRAY_SIZE(debugfs_files),
1029dab16336SThierry Reding 				       sor->debugfs, minor);
1030dab16336SThierry Reding 	if (err < 0)
1031dab16336SThierry Reding 		goto free;
1032dab16336SThierry Reding 
10333ff1f22cSThierry Reding 	sor->minor = minor;
10343ff1f22cSThierry Reding 
1035530239a8SThierry Reding 	return 0;
10366fad8f66SThierry Reding 
1037dab16336SThierry Reding free:
1038dab16336SThierry Reding 	kfree(sor->debugfs_files);
1039dab16336SThierry Reding 	sor->debugfs_files = NULL;
10406fad8f66SThierry Reding remove:
1041dab16336SThierry Reding 	debugfs_remove_recursive(sor->debugfs);
10426fad8f66SThierry Reding 	sor->debugfs = NULL;
10436fad8f66SThierry Reding 	return err;
10446fad8f66SThierry Reding }
10456fad8f66SThierry Reding 
10464009c224SThierry Reding static void tegra_sor_debugfs_exit(struct tegra_sor *sor)
10476fad8f66SThierry Reding {
1048dab16336SThierry Reding 	drm_debugfs_remove_files(sor->debugfs_files, ARRAY_SIZE(debugfs_files),
1049dab16336SThierry Reding 				 sor->minor);
1050dab16336SThierry Reding 	sor->minor = NULL;
1051dab16336SThierry Reding 
1052dab16336SThierry Reding 	kfree(sor->debugfs_files);
1053066d30f8SThierry Reding 	sor->debugfs_files = NULL;
1054dab16336SThierry Reding 
1055dab16336SThierry Reding 	debugfs_remove_recursive(sor->debugfs);
1056066d30f8SThierry Reding 	sor->debugfs = NULL;
10576fad8f66SThierry Reding }
10586fad8f66SThierry Reding 
10596fad8f66SThierry Reding static enum drm_connector_status
10606fad8f66SThierry Reding tegra_sor_connector_detect(struct drm_connector *connector, bool force)
10616fad8f66SThierry Reding {
10626fad8f66SThierry Reding 	struct tegra_output *output = connector_to_output(connector);
10636fad8f66SThierry Reding 	struct tegra_sor *sor = to_sor(output);
10646fad8f66SThierry Reding 
10659542c237SThierry Reding 	if (sor->aux)
10669542c237SThierry Reding 		return drm_dp_aux_detect(sor->aux);
10676fad8f66SThierry Reding 
1068459cc2c6SThierry Reding 	return tegra_output_connector_detect(connector, force);
10696fad8f66SThierry Reding }
10706fad8f66SThierry Reding 
10716fad8f66SThierry Reding static const struct drm_connector_funcs tegra_sor_connector_funcs = {
1072850bab44SThierry Reding 	.dpms = drm_atomic_helper_connector_dpms,
10739d44189fSThierry Reding 	.reset = drm_atomic_helper_connector_reset,
10746fad8f66SThierry Reding 	.detect = tegra_sor_connector_detect,
10756fad8f66SThierry Reding 	.fill_modes = drm_helper_probe_single_connector_modes,
10766fad8f66SThierry Reding 	.destroy = tegra_output_connector_destroy,
10779d44189fSThierry Reding 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
10784aa3df71SThierry Reding 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
10796fad8f66SThierry Reding };
10806fad8f66SThierry Reding 
10816fad8f66SThierry Reding static int tegra_sor_connector_get_modes(struct drm_connector *connector)
10826fad8f66SThierry Reding {
10836fad8f66SThierry Reding 	struct tegra_output *output = connector_to_output(connector);
10846fad8f66SThierry Reding 	struct tegra_sor *sor = to_sor(output);
10856fad8f66SThierry Reding 	int err;
10866fad8f66SThierry Reding 
10879542c237SThierry Reding 	if (sor->aux)
10889542c237SThierry Reding 		drm_dp_aux_enable(sor->aux);
10896fad8f66SThierry Reding 
10906fad8f66SThierry Reding 	err = tegra_output_connector_get_modes(connector);
10916fad8f66SThierry Reding 
10929542c237SThierry Reding 	if (sor->aux)
10939542c237SThierry Reding 		drm_dp_aux_disable(sor->aux);
10946fad8f66SThierry Reding 
10956fad8f66SThierry Reding 	return err;
10966fad8f66SThierry Reding }
10976fad8f66SThierry Reding 
10986fad8f66SThierry Reding static enum drm_mode_status
10996fad8f66SThierry Reding tegra_sor_connector_mode_valid(struct drm_connector *connector,
11006fad8f66SThierry Reding 			       struct drm_display_mode *mode)
11016fad8f66SThierry Reding {
11026fad8f66SThierry Reding 	return MODE_OK;
11036fad8f66SThierry Reding }
11046fad8f66SThierry Reding 
11056fad8f66SThierry Reding static const struct drm_connector_helper_funcs tegra_sor_connector_helper_funcs = {
11066fad8f66SThierry Reding 	.get_modes = tegra_sor_connector_get_modes,
11076fad8f66SThierry Reding 	.mode_valid = tegra_sor_connector_mode_valid,
11086fad8f66SThierry Reding 	.best_encoder = tegra_output_connector_best_encoder,
11096fad8f66SThierry Reding };
11106fad8f66SThierry Reding 
11116fad8f66SThierry Reding static const struct drm_encoder_funcs tegra_sor_encoder_funcs = {
11126fad8f66SThierry Reding 	.destroy = tegra_output_encoder_destroy,
11136fad8f66SThierry Reding };
11146fad8f66SThierry Reding 
1115850bab44SThierry Reding static void tegra_sor_edp_disable(struct drm_encoder *encoder)
11166fad8f66SThierry Reding {
1117850bab44SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
1118850bab44SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
1119850bab44SThierry Reding 	struct tegra_sor *sor = to_sor(output);
1120850bab44SThierry Reding 	u32 value;
1121850bab44SThierry Reding 	int err;
1122850bab44SThierry Reding 
1123850bab44SThierry Reding 	if (output->panel)
1124850bab44SThierry Reding 		drm_panel_disable(output->panel);
1125850bab44SThierry Reding 
1126850bab44SThierry Reding 	err = tegra_sor_detach(sor);
1127850bab44SThierry Reding 	if (err < 0)
1128850bab44SThierry Reding 		dev_err(sor->dev, "failed to detach SOR: %d\n", err);
1129850bab44SThierry Reding 
1130850bab44SThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE1);
1131850bab44SThierry Reding 	tegra_sor_update(sor);
1132850bab44SThierry Reding 
1133850bab44SThierry Reding 	/*
1134850bab44SThierry Reding 	 * The following accesses registers of the display controller, so make
1135850bab44SThierry Reding 	 * sure it's only executed when the output is attached to one.
1136850bab44SThierry Reding 	 */
1137850bab44SThierry Reding 	if (dc) {
1138850bab44SThierry Reding 		value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
1139850bab44SThierry Reding 		value &= ~SOR_ENABLE;
1140850bab44SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
1141850bab44SThierry Reding 
1142850bab44SThierry Reding 		tegra_dc_commit(dc);
11436fad8f66SThierry Reding 	}
11446fad8f66SThierry Reding 
1145850bab44SThierry Reding 	err = tegra_sor_power_down(sor);
1146850bab44SThierry Reding 	if (err < 0)
1147850bab44SThierry Reding 		dev_err(sor->dev, "failed to power down SOR: %d\n", err);
1148850bab44SThierry Reding 
11499542c237SThierry Reding 	if (sor->aux) {
11509542c237SThierry Reding 		err = drm_dp_aux_disable(sor->aux);
1151850bab44SThierry Reding 		if (err < 0)
1152850bab44SThierry Reding 			dev_err(sor->dev, "failed to disable DP: %d\n", err);
11536fad8f66SThierry Reding 	}
11546fad8f66SThierry Reding 
1155850bab44SThierry Reding 	err = tegra_io_rail_power_off(TEGRA_IO_RAIL_LVDS);
1156850bab44SThierry Reding 	if (err < 0)
1157850bab44SThierry Reding 		dev_err(sor->dev, "failed to power off I/O rail: %d\n", err);
1158850bab44SThierry Reding 
1159850bab44SThierry Reding 	if (output->panel)
1160850bab44SThierry Reding 		drm_panel_unprepare(output->panel);
1161850bab44SThierry Reding 
1162850bab44SThierry Reding 	reset_control_assert(sor->rst);
1163850bab44SThierry Reding 	clk_disable_unprepare(sor->clk);
11646fad8f66SThierry Reding }
11656fad8f66SThierry Reding 
1166459cc2c6SThierry Reding #if 0
1167459cc2c6SThierry Reding static int calc_h_ref_to_sync(const struct drm_display_mode *mode,
1168459cc2c6SThierry Reding 			      unsigned int *value)
1169459cc2c6SThierry Reding {
1170459cc2c6SThierry Reding 	unsigned int hfp, hsw, hbp, a = 0, b;
1171459cc2c6SThierry Reding 
1172459cc2c6SThierry Reding 	hfp = mode->hsync_start - mode->hdisplay;
1173459cc2c6SThierry Reding 	hsw = mode->hsync_end - mode->hsync_start;
1174459cc2c6SThierry Reding 	hbp = mode->htotal - mode->hsync_end;
1175459cc2c6SThierry Reding 
1176459cc2c6SThierry Reding 	pr_info("hfp: %u, hsw: %u, hbp: %u\n", hfp, hsw, hbp);
1177459cc2c6SThierry Reding 
1178459cc2c6SThierry Reding 	b = hfp - 1;
1179459cc2c6SThierry Reding 
1180459cc2c6SThierry Reding 	pr_info("a: %u, b: %u\n", a, b);
1181459cc2c6SThierry Reding 	pr_info("a + hsw + hbp = %u\n", a + hsw + hbp);
1182459cc2c6SThierry Reding 
1183459cc2c6SThierry Reding 	if (a + hsw + hbp <= 11) {
1184459cc2c6SThierry Reding 		a = 1 + 11 - hsw - hbp;
1185459cc2c6SThierry Reding 		pr_info("a: %u\n", a);
1186459cc2c6SThierry Reding 	}
1187459cc2c6SThierry Reding 
1188459cc2c6SThierry Reding 	if (a > b)
1189459cc2c6SThierry Reding 		return -EINVAL;
1190459cc2c6SThierry Reding 
1191459cc2c6SThierry Reding 	if (hsw < 1)
1192459cc2c6SThierry Reding 		return -EINVAL;
1193459cc2c6SThierry Reding 
1194459cc2c6SThierry Reding 	if (mode->hdisplay < 16)
1195459cc2c6SThierry Reding 		return -EINVAL;
1196459cc2c6SThierry Reding 
1197459cc2c6SThierry Reding 	if (value) {
1198459cc2c6SThierry Reding 		if (b > a && a % 2)
1199459cc2c6SThierry Reding 			*value = a + 1;
1200459cc2c6SThierry Reding 		else
1201459cc2c6SThierry Reding 			*value = a;
1202459cc2c6SThierry Reding 	}
1203459cc2c6SThierry Reding 
1204459cc2c6SThierry Reding 	return 0;
1205459cc2c6SThierry Reding }
1206459cc2c6SThierry Reding #endif
1207459cc2c6SThierry Reding 
1208850bab44SThierry Reding static void tegra_sor_edp_enable(struct drm_encoder *encoder)
12096fad8f66SThierry Reding {
1210850bab44SThierry Reding 	struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
12116fad8f66SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
12126fad8f66SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
12136b6b6042SThierry Reding 	unsigned int vbe, vse, hbe, hse, vbs, hbs, i;
12146b6b6042SThierry Reding 	struct tegra_sor *sor = to_sor(output);
121534fa183bSThierry Reding 	struct tegra_sor_config config;
121634fa183bSThierry Reding 	struct drm_dp_link link;
121701b9bea0SThierry Reding 	u8 rate, lanes;
121886f5c52dSThierry Reding 	int err = 0;
121928fe2076SThierry Reding 	u32 value;
122086f5c52dSThierry Reding 
12216b6b6042SThierry Reding 	err = clk_prepare_enable(sor->clk);
12226b6b6042SThierry Reding 	if (err < 0)
1223850bab44SThierry Reding 		dev_err(sor->dev, "failed to enable clock: %d\n", err);
12246b6b6042SThierry Reding 
12256b6b6042SThierry Reding 	reset_control_deassert(sor->rst);
12266b6b6042SThierry Reding 
12276fad8f66SThierry Reding 	if (output->panel)
12286fad8f66SThierry Reding 		drm_panel_prepare(output->panel);
12296fad8f66SThierry Reding 
12309542c237SThierry Reding 	err = drm_dp_aux_enable(sor->aux);
12316b6b6042SThierry Reding 	if (err < 0)
12326b6b6042SThierry Reding 		dev_err(sor->dev, "failed to enable DP: %d\n", err);
123334fa183bSThierry Reding 
12349542c237SThierry Reding 	err = drm_dp_link_probe(sor->aux, &link);
123534fa183bSThierry Reding 	if (err < 0) {
123601b9bea0SThierry Reding 		dev_err(sor->dev, "failed to probe eDP link: %d\n", err);
1237850bab44SThierry Reding 		return;
123834fa183bSThierry Reding 	}
12396b6b6042SThierry Reding 
124025bb2cecSThierry Reding 	/* switch to safe parent clock */
124125bb2cecSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
12426b6b6042SThierry Reding 	if (err < 0)
12436b6b6042SThierry Reding 		dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
12446b6b6042SThierry Reding 
124534fa183bSThierry Reding 	memset(&config, 0, sizeof(config));
1246054b1bd1SStéphane Marchesin 	config.bits_per_pixel = output->connector.display_info.bpc * 3;
124734fa183bSThierry Reding 
1248*a198359eSThierry Reding 	err = tegra_sor_compute_config(sor, mode, &config, &link);
124934fa183bSThierry Reding 	if (err < 0)
1250*a198359eSThierry Reding 		dev_err(sor->dev, "failed to compute configuration: %d\n", err);
125134fa183bSThierry Reding 
12526b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
12536b6b6042SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
12546b6b6042SThierry Reding 	value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
12556b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
12566b6b6042SThierry Reding 
1257a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1258a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
1259a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
12606b6b6042SThierry Reding 	usleep_range(20, 100);
12616b6b6042SThierry Reding 
1262a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL3);
1263a9a9e4fdSThierry Reding 	value |= SOR_PLL3_PLL_VDD_MODE_3V3;
1264a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL3);
12656b6b6042SThierry Reding 
1266a9a9e4fdSThierry Reding 	value = SOR_PLL0_ICHPMP(0xf) | SOR_PLL0_VCOCAP_RST |
1267a9a9e4fdSThierry Reding 		SOR_PLL0_PLLREG_LEVEL_V45 | SOR_PLL0_RESISTOR_EXT;
1268a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL0);
12696b6b6042SThierry Reding 
1270a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1271a9a9e4fdSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD;
1272a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1273a9a9e4fdSThierry Reding 	value |= SOR_PLL2_LVDS_ENABLE;
1274a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
12756b6b6042SThierry Reding 
1276a9a9e4fdSThierry Reding 	value = SOR_PLL1_TERM_COMPOUT | SOR_PLL1_TMDS_TERM;
1277a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL1);
12786b6b6042SThierry Reding 
12796b6b6042SThierry Reding 	while (true) {
1280a9a9e4fdSThierry Reding 		value = tegra_sor_readl(sor, SOR_PLL2);
1281a9a9e4fdSThierry Reding 		if ((value & SOR_PLL2_SEQ_PLLCAPPD_ENFORCE) == 0)
12826b6b6042SThierry Reding 			break;
12836b6b6042SThierry Reding 
12846b6b6042SThierry Reding 		usleep_range(250, 1000);
12856b6b6042SThierry Reding 	}
12866b6b6042SThierry Reding 
1287a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1288a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
1289a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_PORT_POWERDOWN;
1290a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
12916b6b6042SThierry Reding 
12926b6b6042SThierry Reding 	/*
12936b6b6042SThierry Reding 	 * power up
12946b6b6042SThierry Reding 	 */
12956b6b6042SThierry Reding 
12966b6b6042SThierry Reding 	/* set safe link bandwidth (1.62 Gbps) */
12976b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
12986b6b6042SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
12996b6b6042SThierry Reding 	value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G1_62;
13006b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
13016b6b6042SThierry Reding 
13026b6b6042SThierry Reding 	/* step 1 */
1303a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1304a9a9e4fdSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE | SOR_PLL2_PORT_POWERDOWN |
1305a9a9e4fdSThierry Reding 		 SOR_PLL2_BANDGAP_POWERDOWN;
1306a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
13076b6b6042SThierry Reding 
1308a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL0);
1309a9a9e4fdSThierry Reding 	value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
1310a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL0);
13116b6b6042SThierry Reding 
1312a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
13136b6b6042SThierry Reding 	value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
1314a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
13156b6b6042SThierry Reding 
13166b6b6042SThierry Reding 	/* step 2 */
13176b6b6042SThierry Reding 	err = tegra_io_rail_power_on(TEGRA_IO_RAIL_LVDS);
1318850bab44SThierry Reding 	if (err < 0)
13196b6b6042SThierry Reding 		dev_err(sor->dev, "failed to power on I/O rail: %d\n", err);
13206b6b6042SThierry Reding 
13216b6b6042SThierry Reding 	usleep_range(5, 100);
13226b6b6042SThierry Reding 
13236b6b6042SThierry Reding 	/* step 3 */
1324a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1325a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
1326a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
13276b6b6042SThierry Reding 
13286b6b6042SThierry Reding 	usleep_range(20, 100);
13296b6b6042SThierry Reding 
13306b6b6042SThierry Reding 	/* step 4 */
1331a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL0);
1332a9a9e4fdSThierry Reding 	value &= ~SOR_PLL0_VCOPD;
1333a9a9e4fdSThierry Reding 	value &= ~SOR_PLL0_PWR;
1334a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL0);
13356b6b6042SThierry Reding 
1336a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1337a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1338a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
13396b6b6042SThierry Reding 
13406b6b6042SThierry Reding 	usleep_range(200, 1000);
13416b6b6042SThierry Reding 
13426b6b6042SThierry Reding 	/* step 5 */
1343a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1344a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_PORT_POWERDOWN;
1345a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
13466b6b6042SThierry Reding 
134725bb2cecSThierry Reding 	/* switch to DP parent clock */
134825bb2cecSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_dp);
13496b6b6042SThierry Reding 	if (err < 0)
135025bb2cecSThierry Reding 		dev_err(sor->dev, "failed to set parent clock: %d\n", err);
13516b6b6042SThierry Reding 
1352899451b7SThierry Reding 	/* power DP lanes */
1353a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
1354899451b7SThierry Reding 
1355899451b7SThierry Reding 	if (link.num_lanes <= 2)
1356899451b7SThierry Reding 		value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2);
1357899451b7SThierry Reding 	else
1358899451b7SThierry Reding 		value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2;
1359899451b7SThierry Reding 
1360899451b7SThierry Reding 	if (link.num_lanes <= 1)
1361899451b7SThierry Reding 		value &= ~SOR_DP_PADCTL_PD_TXD_1;
1362899451b7SThierry Reding 	else
1363899451b7SThierry Reding 		value |= SOR_DP_PADCTL_PD_TXD_1;
1364899451b7SThierry Reding 
1365899451b7SThierry Reding 	if (link.num_lanes == 0)
1366899451b7SThierry Reding 		value &= ~SOR_DP_PADCTL_PD_TXD_0;
1367899451b7SThierry Reding 	else
1368899451b7SThierry Reding 		value |= SOR_DP_PADCTL_PD_TXD_0;
1369899451b7SThierry Reding 
1370a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
13716b6b6042SThierry Reding 
1372a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
13736b6b6042SThierry Reding 	value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
13740c90a184SThierry Reding 	value |= SOR_DP_LINKCTL_LANE_COUNT(link.num_lanes);
1375a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
13766b6b6042SThierry Reding 
13776b6b6042SThierry Reding 	/* start lane sequencer */
13786b6b6042SThierry Reding 	value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
13796b6b6042SThierry Reding 		SOR_LANE_SEQ_CTL_POWER_STATE_UP;
13806b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
13816b6b6042SThierry Reding 
13826b6b6042SThierry Reding 	while (true) {
13836b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
13846b6b6042SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
13856b6b6042SThierry Reding 			break;
13866b6b6042SThierry Reding 
13876b6b6042SThierry Reding 		usleep_range(250, 1000);
13886b6b6042SThierry Reding 	}
13896b6b6042SThierry Reding 
1390a4263fedSThierry Reding 	/* set link bandwidth */
13916b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
13926b6b6042SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
1393a4263fedSThierry Reding 	value |= drm_dp_link_rate_to_bw_code(link.rate) << 2;
13946b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
13956b6b6042SThierry Reding 
13966b6b6042SThierry Reding 	/* set linkctl */
1397a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
13986b6b6042SThierry Reding 	value |= SOR_DP_LINKCTL_ENABLE;
13996b6b6042SThierry Reding 
14006b6b6042SThierry Reding 	value &= ~SOR_DP_LINKCTL_TU_SIZE_MASK;
140134fa183bSThierry Reding 	value |= SOR_DP_LINKCTL_TU_SIZE(config.tu_size);
14026b6b6042SThierry Reding 
14036b6b6042SThierry Reding 	value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
1404a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
14056b6b6042SThierry Reding 
14066b6b6042SThierry Reding 	for (i = 0, value = 0; i < 4; i++) {
14076b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
14086b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_GALIOS |
14096b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_NONE;
14106b6b6042SThierry Reding 		value = (value << 8) | lane;
14116b6b6042SThierry Reding 	}
14126b6b6042SThierry Reding 
14136b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
14146b6b6042SThierry Reding 
1415a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_CONFIG0);
14166b6b6042SThierry Reding 	value &= ~SOR_DP_CONFIG_WATERMARK_MASK;
141734fa183bSThierry Reding 	value |= SOR_DP_CONFIG_WATERMARK(config.watermark);
14186b6b6042SThierry Reding 
14196b6b6042SThierry Reding 	value &= ~SOR_DP_CONFIG_ACTIVE_SYM_COUNT_MASK;
142034fa183bSThierry Reding 	value |= SOR_DP_CONFIG_ACTIVE_SYM_COUNT(config.active_count);
14216b6b6042SThierry Reding 
14226b6b6042SThierry Reding 	value &= ~SOR_DP_CONFIG_ACTIVE_SYM_FRAC_MASK;
142334fa183bSThierry Reding 	value |= SOR_DP_CONFIG_ACTIVE_SYM_FRAC(config.active_frac);
14246b6b6042SThierry Reding 
142534fa183bSThierry Reding 	if (config.active_polarity)
142634fa183bSThierry Reding 		value |= SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
142734fa183bSThierry Reding 	else
142834fa183bSThierry Reding 		value &= ~SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
14296b6b6042SThierry Reding 
14306b6b6042SThierry Reding 	value |= SOR_DP_CONFIG_ACTIVE_SYM_ENABLE;
14311f64ae7cSThierry Reding 	value |= SOR_DP_CONFIG_DISPARITY_NEGATIVE;
1432a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_CONFIG0);
14336b6b6042SThierry Reding 
14346b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_AUDIO_HBLANK_SYMBOLS);
14356b6b6042SThierry Reding 	value &= ~SOR_DP_AUDIO_HBLANK_SYMBOLS_MASK;
14367890b576SThierry Reding 	value |= config.hblank_symbols & 0xffff;
14376b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_AUDIO_HBLANK_SYMBOLS);
14386b6b6042SThierry Reding 
14396b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_AUDIO_VBLANK_SYMBOLS);
14406b6b6042SThierry Reding 	value &= ~SOR_DP_AUDIO_VBLANK_SYMBOLS_MASK;
14417890b576SThierry Reding 	value |= config.vblank_symbols & 0xffff;
14426b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_AUDIO_VBLANK_SYMBOLS);
14436b6b6042SThierry Reding 
14446b6b6042SThierry Reding 	/* enable pad calibration logic */
1445a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
14466b6b6042SThierry Reding 	value |= SOR_DP_PADCTL_PAD_CAL_PD;
1447a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
14486b6b6042SThierry Reding 
14499542c237SThierry Reding 	err = drm_dp_link_probe(sor->aux, &link);
1450850bab44SThierry Reding 	if (err < 0)
145101b9bea0SThierry Reding 		dev_err(sor->dev, "failed to probe eDP link: %d\n", err);
14526b6b6042SThierry Reding 
14539542c237SThierry Reding 	err = drm_dp_link_power_up(sor->aux, &link);
1454850bab44SThierry Reding 	if (err < 0)
145501b9bea0SThierry Reding 		dev_err(sor->dev, "failed to power up eDP link: %d\n", err);
14566b6b6042SThierry Reding 
14579542c237SThierry Reding 	err = drm_dp_link_configure(sor->aux, &link);
1458850bab44SThierry Reding 	if (err < 0)
145901b9bea0SThierry Reding 		dev_err(sor->dev, "failed to configure eDP link: %d\n", err);
14606b6b6042SThierry Reding 
14616b6b6042SThierry Reding 	rate = drm_dp_link_rate_to_bw_code(link.rate);
14626b6b6042SThierry Reding 	lanes = link.num_lanes;
14636b6b6042SThierry Reding 
14646b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
14656b6b6042SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
14666b6b6042SThierry Reding 	value |= SOR_CLK_CNTRL_DP_LINK_SPEED(rate);
14676b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
14686b6b6042SThierry Reding 
1469a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
14706b6b6042SThierry Reding 	value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
14716b6b6042SThierry Reding 	value |= SOR_DP_LINKCTL_LANE_COUNT(lanes);
14726b6b6042SThierry Reding 
14736b6b6042SThierry Reding 	if (link.capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
14746b6b6042SThierry Reding 		value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
14756b6b6042SThierry Reding 
1476a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
14776b6b6042SThierry Reding 
14786b6b6042SThierry Reding 	/* disable training pattern generator */
14796b6b6042SThierry Reding 
14806b6b6042SThierry Reding 	for (i = 0; i < link.num_lanes; i++) {
14816b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
14826b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_GALIOS |
14836b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_NONE;
14846b6b6042SThierry Reding 		value = (value << 8) | lane;
14856b6b6042SThierry Reding 	}
14866b6b6042SThierry Reding 
14876b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
14886b6b6042SThierry Reding 
14896b6b6042SThierry Reding 	err = tegra_sor_dp_train_fast(sor, &link);
149001b9bea0SThierry Reding 	if (err < 0)
149101b9bea0SThierry Reding 		dev_err(sor->dev, "DP fast link training failed: %d\n", err);
14926b6b6042SThierry Reding 
14936b6b6042SThierry Reding 	dev_dbg(sor->dev, "fast link training succeeded\n");
14946b6b6042SThierry Reding 
14956b6b6042SThierry Reding 	err = tegra_sor_power_up(sor, 250);
1496850bab44SThierry Reding 	if (err < 0)
14976b6b6042SThierry Reding 		dev_err(sor->dev, "failed to power up SOR: %d\n", err);
14986b6b6042SThierry Reding 
14996b6b6042SThierry Reding 	/*
15006b6b6042SThierry Reding 	 * configure panel (24bpp, vsync-, hsync-, DP-A protocol, complete
15016b6b6042SThierry Reding 	 * raster, associate with display controller)
15026b6b6042SThierry Reding 	 */
15033f4f3b5fSThierry Reding 	value = SOR_STATE_ASY_PROTOCOL_DP_A |
15046b6b6042SThierry Reding 		SOR_STATE_ASY_CRC_MODE_COMPLETE |
15056b6b6042SThierry Reding 		SOR_STATE_ASY_OWNER(dc->pipe + 1);
150634fa183bSThierry Reding 
15073f4f3b5fSThierry Reding 	if (mode->flags & DRM_MODE_FLAG_PHSYNC)
15083f4f3b5fSThierry Reding 		value &= ~SOR_STATE_ASY_HSYNCPOL;
15093f4f3b5fSThierry Reding 
15103f4f3b5fSThierry Reding 	if (mode->flags & DRM_MODE_FLAG_NHSYNC)
15113f4f3b5fSThierry Reding 		value |= SOR_STATE_ASY_HSYNCPOL;
15123f4f3b5fSThierry Reding 
15133f4f3b5fSThierry Reding 	if (mode->flags & DRM_MODE_FLAG_PVSYNC)
15143f4f3b5fSThierry Reding 		value &= ~SOR_STATE_ASY_VSYNCPOL;
15153f4f3b5fSThierry Reding 
15163f4f3b5fSThierry Reding 	if (mode->flags & DRM_MODE_FLAG_NVSYNC)
15173f4f3b5fSThierry Reding 		value |= SOR_STATE_ASY_VSYNCPOL;
15183f4f3b5fSThierry Reding 
151934fa183bSThierry Reding 	switch (config.bits_per_pixel) {
152034fa183bSThierry Reding 	case 24:
152134fa183bSThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
152234fa183bSThierry Reding 		break;
152334fa183bSThierry Reding 
152434fa183bSThierry Reding 	case 18:
152534fa183bSThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_18_444;
152634fa183bSThierry Reding 		break;
152734fa183bSThierry Reding 
152834fa183bSThierry Reding 	default:
152934fa183bSThierry Reding 		BUG();
153034fa183bSThierry Reding 		break;
153134fa183bSThierry Reding 	}
153234fa183bSThierry Reding 
1533a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
15346b6b6042SThierry Reding 
15356b6b6042SThierry Reding 	/*
15366b6b6042SThierry Reding 	 * TODO: The video timing programming below doesn't seem to match the
15376b6b6042SThierry Reding 	 * register definitions.
15386b6b6042SThierry Reding 	 */
15396b6b6042SThierry Reding 
15406b6b6042SThierry Reding 	value = ((mode->vtotal & 0x7fff) << 16) | (mode->htotal & 0x7fff);
154151511d05SThierry Reding 	tegra_sor_writel(sor, value, SOR_HEAD_STATE1(dc->pipe));
15426b6b6042SThierry Reding 
15436b6b6042SThierry Reding 	vse = mode->vsync_end - mode->vsync_start - 1;
15446b6b6042SThierry Reding 	hse = mode->hsync_end - mode->hsync_start - 1;
15456b6b6042SThierry Reding 
15466b6b6042SThierry Reding 	value = ((vse & 0x7fff) << 16) | (hse & 0x7fff);
154751511d05SThierry Reding 	tegra_sor_writel(sor, value, SOR_HEAD_STATE2(dc->pipe));
15486b6b6042SThierry Reding 
15496b6b6042SThierry Reding 	vbe = vse + (mode->vsync_start - mode->vdisplay);
15506b6b6042SThierry Reding 	hbe = hse + (mode->hsync_start - mode->hdisplay);
15516b6b6042SThierry Reding 
15526b6b6042SThierry Reding 	value = ((vbe & 0x7fff) << 16) | (hbe & 0x7fff);
155351511d05SThierry Reding 	tegra_sor_writel(sor, value, SOR_HEAD_STATE3(dc->pipe));
15546b6b6042SThierry Reding 
15556b6b6042SThierry Reding 	vbs = vbe + mode->vdisplay;
15566b6b6042SThierry Reding 	hbs = hbe + mode->hdisplay;
15576b6b6042SThierry Reding 
15586b6b6042SThierry Reding 	value = ((vbs & 0x7fff) << 16) | (hbs & 0x7fff);
155951511d05SThierry Reding 	tegra_sor_writel(sor, value, SOR_HEAD_STATE4(dc->pipe));
156051511d05SThierry Reding 
156151511d05SThierry Reding 	tegra_sor_writel(sor, 0x1, SOR_HEAD_STATE5(dc->pipe));
15626b6b6042SThierry Reding 
15636b6b6042SThierry Reding 	/* CSTM (LVDS, link A/B, upper) */
1564143b1df2SStéphane Marchesin 	value = SOR_CSTM_LVDS | SOR_CSTM_LINK_ACT_A | SOR_CSTM_LINK_ACT_B |
15656b6b6042SThierry Reding 		SOR_CSTM_UPPER;
15666b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CSTM);
15676b6b6042SThierry Reding 
15686b6b6042SThierry Reding 	/* PWM setup */
15696b6b6042SThierry Reding 	err = tegra_sor_setup_pwm(sor, 250);
1570850bab44SThierry Reding 	if (err < 0)
15716b6b6042SThierry Reding 		dev_err(sor->dev, "failed to setup PWM: %d\n", err);
15726b6b6042SThierry Reding 
1573666cb873SThierry Reding 	tegra_sor_update(sor);
1574666cb873SThierry Reding 
15756b6b6042SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
15766b6b6042SThierry Reding 	value |= SOR_ENABLE;
15776b6b6042SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
15786b6b6042SThierry Reding 
1579666cb873SThierry Reding 	tegra_dc_commit(dc);
15806b6b6042SThierry Reding 
15816b6b6042SThierry Reding 	err = tegra_sor_attach(sor);
1582850bab44SThierry Reding 	if (err < 0)
15836b6b6042SThierry Reding 		dev_err(sor->dev, "failed to attach SOR: %d\n", err);
15846b6b6042SThierry Reding 
15856b6b6042SThierry Reding 	err = tegra_sor_wakeup(sor);
1586850bab44SThierry Reding 	if (err < 0)
15876b6b6042SThierry Reding 		dev_err(sor->dev, "failed to enable DC: %d\n", err);
15886b6b6042SThierry Reding 
15896fad8f66SThierry Reding 	if (output->panel)
15906fad8f66SThierry Reding 		drm_panel_enable(output->panel);
15916b6b6042SThierry Reding }
15926b6b6042SThierry Reding 
159382f1511cSThierry Reding static int
159482f1511cSThierry Reding tegra_sor_encoder_atomic_check(struct drm_encoder *encoder,
159582f1511cSThierry Reding 			       struct drm_crtc_state *crtc_state,
159682f1511cSThierry Reding 			       struct drm_connector_state *conn_state)
159782f1511cSThierry Reding {
159882f1511cSThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
159982f1511cSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(conn_state->crtc);
160082f1511cSThierry Reding 	unsigned long pclk = crtc_state->mode.clock * 1000;
160182f1511cSThierry Reding 	struct tegra_sor *sor = to_sor(output);
160282f1511cSThierry Reding 	int err;
160382f1511cSThierry Reding 
160482f1511cSThierry Reding 	err = tegra_dc_state_setup_clock(dc, crtc_state, sor->clk_parent,
160582f1511cSThierry Reding 					 pclk, 0);
160682f1511cSThierry Reding 	if (err < 0) {
160782f1511cSThierry Reding 		dev_err(output->dev, "failed to setup CRTC state: %d\n", err);
160882f1511cSThierry Reding 		return err;
160982f1511cSThierry Reding 	}
161082f1511cSThierry Reding 
161182f1511cSThierry Reding 	return 0;
161282f1511cSThierry Reding }
161382f1511cSThierry Reding 
1614459cc2c6SThierry Reding static const struct drm_encoder_helper_funcs tegra_sor_edp_helpers = {
1615850bab44SThierry Reding 	.disable = tegra_sor_edp_disable,
1616850bab44SThierry Reding 	.enable = tegra_sor_edp_enable,
161782f1511cSThierry Reding 	.atomic_check = tegra_sor_encoder_atomic_check,
16186b6b6042SThierry Reding };
16196b6b6042SThierry Reding 
1620459cc2c6SThierry Reding static inline u32 tegra_sor_hdmi_subpack(const u8 *ptr, size_t size)
1621459cc2c6SThierry Reding {
1622459cc2c6SThierry Reding 	u32 value = 0;
1623459cc2c6SThierry Reding 	size_t i;
1624459cc2c6SThierry Reding 
1625459cc2c6SThierry Reding 	for (i = size; i > 0; i--)
1626459cc2c6SThierry Reding 		value = (value << 8) | ptr[i - 1];
1627459cc2c6SThierry Reding 
1628459cc2c6SThierry Reding 	return value;
1629459cc2c6SThierry Reding }
1630459cc2c6SThierry Reding 
1631459cc2c6SThierry Reding static void tegra_sor_hdmi_write_infopack(struct tegra_sor *sor,
1632459cc2c6SThierry Reding 					  const void *data, size_t size)
1633459cc2c6SThierry Reding {
1634459cc2c6SThierry Reding 	const u8 *ptr = data;
1635459cc2c6SThierry Reding 	unsigned long offset;
1636459cc2c6SThierry Reding 	size_t i, j;
1637459cc2c6SThierry Reding 	u32 value;
1638459cc2c6SThierry Reding 
1639459cc2c6SThierry Reding 	switch (ptr[0]) {
1640459cc2c6SThierry Reding 	case HDMI_INFOFRAME_TYPE_AVI:
1641459cc2c6SThierry Reding 		offset = SOR_HDMI_AVI_INFOFRAME_HEADER;
1642459cc2c6SThierry Reding 		break;
1643459cc2c6SThierry Reding 
1644459cc2c6SThierry Reding 	case HDMI_INFOFRAME_TYPE_AUDIO:
1645459cc2c6SThierry Reding 		offset = SOR_HDMI_AUDIO_INFOFRAME_HEADER;
1646459cc2c6SThierry Reding 		break;
1647459cc2c6SThierry Reding 
1648459cc2c6SThierry Reding 	case HDMI_INFOFRAME_TYPE_VENDOR:
1649459cc2c6SThierry Reding 		offset = SOR_HDMI_VSI_INFOFRAME_HEADER;
1650459cc2c6SThierry Reding 		break;
1651459cc2c6SThierry Reding 
1652459cc2c6SThierry Reding 	default:
1653459cc2c6SThierry Reding 		dev_err(sor->dev, "unsupported infoframe type: %02x\n",
1654459cc2c6SThierry Reding 			ptr[0]);
1655459cc2c6SThierry Reding 		return;
1656459cc2c6SThierry Reding 	}
1657459cc2c6SThierry Reding 
1658459cc2c6SThierry Reding 	value = INFOFRAME_HEADER_TYPE(ptr[0]) |
1659459cc2c6SThierry Reding 		INFOFRAME_HEADER_VERSION(ptr[1]) |
1660459cc2c6SThierry Reding 		INFOFRAME_HEADER_LEN(ptr[2]);
1661459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, offset);
1662459cc2c6SThierry Reding 	offset++;
1663459cc2c6SThierry Reding 
1664459cc2c6SThierry Reding 	/*
1665459cc2c6SThierry Reding 	 * Each subpack contains 7 bytes, divided into:
1666459cc2c6SThierry Reding 	 * - subpack_low: bytes 0 - 3
1667459cc2c6SThierry Reding 	 * - subpack_high: bytes 4 - 6 (with byte 7 padded to 0x00)
1668459cc2c6SThierry Reding 	 */
1669459cc2c6SThierry Reding 	for (i = 3, j = 0; i < size; i += 7, j += 8) {
1670459cc2c6SThierry Reding 		size_t rem = size - i, num = min_t(size_t, rem, 4);
1671459cc2c6SThierry Reding 
1672459cc2c6SThierry Reding 		value = tegra_sor_hdmi_subpack(&ptr[i], num);
1673459cc2c6SThierry Reding 		tegra_sor_writel(sor, value, offset++);
1674459cc2c6SThierry Reding 
1675459cc2c6SThierry Reding 		num = min_t(size_t, rem - num, 3);
1676459cc2c6SThierry Reding 
1677459cc2c6SThierry Reding 		value = tegra_sor_hdmi_subpack(&ptr[i + 4], num);
1678459cc2c6SThierry Reding 		tegra_sor_writel(sor, value, offset++);
1679459cc2c6SThierry Reding 	}
1680459cc2c6SThierry Reding }
1681459cc2c6SThierry Reding 
1682459cc2c6SThierry Reding static int
1683459cc2c6SThierry Reding tegra_sor_hdmi_setup_avi_infoframe(struct tegra_sor *sor,
1684459cc2c6SThierry Reding 				   const struct drm_display_mode *mode)
1685459cc2c6SThierry Reding {
1686459cc2c6SThierry Reding 	u8 buffer[HDMI_INFOFRAME_SIZE(AVI)];
1687459cc2c6SThierry Reding 	struct hdmi_avi_infoframe frame;
1688459cc2c6SThierry Reding 	u32 value;
1689459cc2c6SThierry Reding 	int err;
1690459cc2c6SThierry Reding 
1691459cc2c6SThierry Reding 	/* disable AVI infoframe */
1692459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI_AVI_INFOFRAME_CTRL);
1693459cc2c6SThierry Reding 	value &= ~INFOFRAME_CTRL_SINGLE;
1694459cc2c6SThierry Reding 	value &= ~INFOFRAME_CTRL_OTHER;
1695459cc2c6SThierry Reding 	value &= ~INFOFRAME_CTRL_ENABLE;
1696459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_AVI_INFOFRAME_CTRL);
1697459cc2c6SThierry Reding 
1698459cc2c6SThierry Reding 	err = drm_hdmi_avi_infoframe_from_display_mode(&frame, mode);
1699459cc2c6SThierry Reding 	if (err < 0) {
1700459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to setup AVI infoframe: %d\n", err);
1701459cc2c6SThierry Reding 		return err;
1702459cc2c6SThierry Reding 	}
1703459cc2c6SThierry Reding 
1704459cc2c6SThierry Reding 	err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
1705459cc2c6SThierry Reding 	if (err < 0) {
1706459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to pack AVI infoframe: %d\n", err);
1707459cc2c6SThierry Reding 		return err;
1708459cc2c6SThierry Reding 	}
1709459cc2c6SThierry Reding 
1710459cc2c6SThierry Reding 	tegra_sor_hdmi_write_infopack(sor, buffer, err);
1711459cc2c6SThierry Reding 
1712459cc2c6SThierry Reding 	/* enable AVI infoframe */
1713459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI_AVI_INFOFRAME_CTRL);
1714459cc2c6SThierry Reding 	value |= INFOFRAME_CTRL_CHECKSUM_ENABLE;
1715459cc2c6SThierry Reding 	value |= INFOFRAME_CTRL_ENABLE;
1716459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_AVI_INFOFRAME_CTRL);
1717459cc2c6SThierry Reding 
1718459cc2c6SThierry Reding 	return 0;
1719459cc2c6SThierry Reding }
1720459cc2c6SThierry Reding 
1721459cc2c6SThierry Reding static void tegra_sor_hdmi_disable_audio_infoframe(struct tegra_sor *sor)
1722459cc2c6SThierry Reding {
1723459cc2c6SThierry Reding 	u32 value;
1724459cc2c6SThierry Reding 
1725459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
1726459cc2c6SThierry Reding 	value &= ~INFOFRAME_CTRL_ENABLE;
1727459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
1728459cc2c6SThierry Reding }
1729459cc2c6SThierry Reding 
1730459cc2c6SThierry Reding static struct tegra_sor_hdmi_settings *
1731459cc2c6SThierry Reding tegra_sor_hdmi_find_settings(struct tegra_sor *sor, unsigned long frequency)
1732459cc2c6SThierry Reding {
1733459cc2c6SThierry Reding 	unsigned int i;
1734459cc2c6SThierry Reding 
1735459cc2c6SThierry Reding 	for (i = 0; i < sor->num_settings; i++)
1736459cc2c6SThierry Reding 		if (frequency <= sor->settings[i].frequency)
1737459cc2c6SThierry Reding 			return &sor->settings[i];
1738459cc2c6SThierry Reding 
1739459cc2c6SThierry Reding 	return NULL;
1740459cc2c6SThierry Reding }
1741459cc2c6SThierry Reding 
1742459cc2c6SThierry Reding static void tegra_sor_hdmi_disable(struct drm_encoder *encoder)
1743459cc2c6SThierry Reding {
1744459cc2c6SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
1745459cc2c6SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
1746459cc2c6SThierry Reding 	struct tegra_sor *sor = to_sor(output);
1747459cc2c6SThierry Reding 	u32 value;
1748459cc2c6SThierry Reding 	int err;
1749459cc2c6SThierry Reding 
1750459cc2c6SThierry Reding 	err = tegra_sor_detach(sor);
1751459cc2c6SThierry Reding 	if (err < 0)
1752459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to detach SOR: %d\n", err);
1753459cc2c6SThierry Reding 
1754459cc2c6SThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE1);
1755459cc2c6SThierry Reding 	tegra_sor_update(sor);
1756459cc2c6SThierry Reding 
1757459cc2c6SThierry Reding 	/* disable display to SOR clock */
1758459cc2c6SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
1759459cc2c6SThierry Reding 	value &= ~SOR1_TIMING_CYA;
1760459cc2c6SThierry Reding 	value &= ~SOR1_ENABLE;
1761459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
1762459cc2c6SThierry Reding 
1763459cc2c6SThierry Reding 	tegra_dc_commit(dc);
1764459cc2c6SThierry Reding 
1765459cc2c6SThierry Reding 	err = tegra_sor_power_down(sor);
1766459cc2c6SThierry Reding 	if (err < 0)
1767459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to power down SOR: %d\n", err);
1768459cc2c6SThierry Reding 
1769459cc2c6SThierry Reding 	err = tegra_io_rail_power_off(TEGRA_IO_RAIL_HDMI);
1770459cc2c6SThierry Reding 	if (err < 0)
1771459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to power off HDMI rail: %d\n", err);
1772459cc2c6SThierry Reding 
1773459cc2c6SThierry Reding 	reset_control_assert(sor->rst);
1774459cc2c6SThierry Reding 	usleep_range(1000, 2000);
1775459cc2c6SThierry Reding 	clk_disable_unprepare(sor->clk);
1776459cc2c6SThierry Reding }
1777459cc2c6SThierry Reding 
1778459cc2c6SThierry Reding static void tegra_sor_hdmi_enable(struct drm_encoder *encoder)
1779459cc2c6SThierry Reding {
1780459cc2c6SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
1781459cc2c6SThierry Reding 	unsigned int h_ref_to_sync = 1, pulse_start, max_ac;
1782459cc2c6SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
1783459cc2c6SThierry Reding 	unsigned int vbe, vse, hbe, hse, vbs, hbs, div;
1784459cc2c6SThierry Reding 	struct tegra_sor_hdmi_settings *settings;
1785459cc2c6SThierry Reding 	struct tegra_sor *sor = to_sor(output);
1786459cc2c6SThierry Reding 	struct drm_display_mode *mode;
1787459cc2c6SThierry Reding 	struct drm_display_info *info;
1788459cc2c6SThierry Reding 	u32 value;
1789459cc2c6SThierry Reding 	int err;
1790459cc2c6SThierry Reding 
1791459cc2c6SThierry Reding 	mode = &encoder->crtc->state->adjusted_mode;
1792459cc2c6SThierry Reding 	info = &output->connector.display_info;
1793459cc2c6SThierry Reding 
1794459cc2c6SThierry Reding 	err = clk_prepare_enable(sor->clk);
1795459cc2c6SThierry Reding 	if (err < 0)
1796459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to enable clock: %d\n", err);
1797459cc2c6SThierry Reding 
1798459cc2c6SThierry Reding 	usleep_range(1000, 2000);
1799459cc2c6SThierry Reding 
1800459cc2c6SThierry Reding 	reset_control_deassert(sor->rst);
1801459cc2c6SThierry Reding 
180225bb2cecSThierry Reding 	/* switch to safe parent clock */
180325bb2cecSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
1804459cc2c6SThierry Reding 	if (err < 0)
1805459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
1806459cc2c6SThierry Reding 
1807459cc2c6SThierry Reding 	div = clk_get_rate(sor->clk) / 1000000 * 4;
1808459cc2c6SThierry Reding 
1809459cc2c6SThierry Reding 	err = tegra_io_rail_power_on(TEGRA_IO_RAIL_HDMI);
1810459cc2c6SThierry Reding 	if (err < 0)
1811459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to power on HDMI rail: %d\n", err);
1812459cc2c6SThierry Reding 
1813459cc2c6SThierry Reding 	usleep_range(20, 100);
1814459cc2c6SThierry Reding 
1815459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1816459cc2c6SThierry Reding 	value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
1817459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
1818459cc2c6SThierry Reding 
1819459cc2c6SThierry Reding 	usleep_range(20, 100);
1820459cc2c6SThierry Reding 
1821459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL3);
1822459cc2c6SThierry Reding 	value &= ~SOR_PLL3_PLL_VDD_MODE_3V3;
1823459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL3);
1824459cc2c6SThierry Reding 
1825459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL0);
1826459cc2c6SThierry Reding 	value &= ~SOR_PLL0_VCOPD;
1827459cc2c6SThierry Reding 	value &= ~SOR_PLL0_PWR;
1828459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL0);
1829459cc2c6SThierry Reding 
1830459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1831459cc2c6SThierry Reding 	value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1832459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
1833459cc2c6SThierry Reding 
1834459cc2c6SThierry Reding 	usleep_range(200, 400);
1835459cc2c6SThierry Reding 
1836459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1837459cc2c6SThierry Reding 	value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
1838459cc2c6SThierry Reding 	value &= ~SOR_PLL2_PORT_POWERDOWN;
1839459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
1840459cc2c6SThierry Reding 
1841459cc2c6SThierry Reding 	usleep_range(20, 100);
1842459cc2c6SThierry Reding 
1843459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
1844459cc2c6SThierry Reding 	value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
1845459cc2c6SThierry Reding 		 SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2;
1846459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
1847459cc2c6SThierry Reding 
1848459cc2c6SThierry Reding 	while (true) {
1849459cc2c6SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
1850459cc2c6SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_STATE_BUSY) == 0)
1851459cc2c6SThierry Reding 			break;
1852459cc2c6SThierry Reding 
1853459cc2c6SThierry Reding 		usleep_range(250, 1000);
1854459cc2c6SThierry Reding 	}
1855459cc2c6SThierry Reding 
1856459cc2c6SThierry Reding 	value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
1857459cc2c6SThierry Reding 		SOR_LANE_SEQ_CTL_POWER_STATE_UP | SOR_LANE_SEQ_CTL_DELAY(5);
1858459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
1859459cc2c6SThierry Reding 
1860459cc2c6SThierry Reding 	while (true) {
1861459cc2c6SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
1862459cc2c6SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
1863459cc2c6SThierry Reding 			break;
1864459cc2c6SThierry Reding 
1865459cc2c6SThierry Reding 		usleep_range(250, 1000);
1866459cc2c6SThierry Reding 	}
1867459cc2c6SThierry Reding 
1868459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
1869459cc2c6SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
1870459cc2c6SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
1871459cc2c6SThierry Reding 
1872459cc2c6SThierry Reding 	if (mode->clock < 340000)
1873459cc2c6SThierry Reding 		value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G2_70;
1874459cc2c6SThierry Reding 	else
1875459cc2c6SThierry Reding 		value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G5_40;
1876459cc2c6SThierry Reding 
1877459cc2c6SThierry Reding 	value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK;
1878459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
1879459cc2c6SThierry Reding 
1880459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_SPARE0);
1881459cc2c6SThierry Reding 	value |= SOR_DP_SPARE_DISP_VIDEO_PREAMBLE;
1882459cc2c6SThierry Reding 	value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
1883459cc2c6SThierry Reding 	value |= SOR_DP_SPARE_SEQ_ENABLE;
1884459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_SPARE0);
1885459cc2c6SThierry Reding 
1886459cc2c6SThierry Reding 	value = SOR_SEQ_CTL_PU_PC(0) | SOR_SEQ_CTL_PU_PC_ALT(0) |
1887459cc2c6SThierry Reding 		SOR_SEQ_CTL_PD_PC(8) | SOR_SEQ_CTL_PD_PC_ALT(8);
1888459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_SEQ_CTL);
1889459cc2c6SThierry Reding 
1890459cc2c6SThierry Reding 	value = SOR_SEQ_INST_DRIVE_PWM_OUT_LO | SOR_SEQ_INST_HALT |
1891459cc2c6SThierry Reding 		SOR_SEQ_INST_WAIT_VSYNC | SOR_SEQ_INST_WAIT(1);
1892459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_SEQ_INST(0));
1893459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_SEQ_INST(8));
1894459cc2c6SThierry Reding 
1895459cc2c6SThierry Reding 	/* program the reference clock */
1896459cc2c6SThierry Reding 	value = SOR_REFCLK_DIV_INT(div) | SOR_REFCLK_DIV_FRAC(div);
1897459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_REFCLK);
1898459cc2c6SThierry Reding 
1899459cc2c6SThierry Reding 	/* XXX don't hardcode */
1900459cc2c6SThierry Reding 	value = SOR_XBAR_CTRL_LINK1_XSEL(4, 4) |
1901459cc2c6SThierry Reding 		SOR_XBAR_CTRL_LINK1_XSEL(3, 3) |
1902459cc2c6SThierry Reding 		SOR_XBAR_CTRL_LINK1_XSEL(2, 2) |
1903459cc2c6SThierry Reding 		SOR_XBAR_CTRL_LINK1_XSEL(1, 1) |
1904459cc2c6SThierry Reding 		SOR_XBAR_CTRL_LINK1_XSEL(0, 0) |
1905459cc2c6SThierry Reding 		SOR_XBAR_CTRL_LINK0_XSEL(4, 4) |
1906459cc2c6SThierry Reding 		SOR_XBAR_CTRL_LINK0_XSEL(3, 3) |
1907459cc2c6SThierry Reding 		SOR_XBAR_CTRL_LINK0_XSEL(2, 0) |
1908459cc2c6SThierry Reding 		SOR_XBAR_CTRL_LINK0_XSEL(1, 1) |
1909459cc2c6SThierry Reding 		SOR_XBAR_CTRL_LINK0_XSEL(0, 2);
1910459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_XBAR_CTRL);
1911459cc2c6SThierry Reding 
1912459cc2c6SThierry Reding 	tegra_sor_writel(sor, 0x00000000, SOR_XBAR_POL);
1913459cc2c6SThierry Reding 
191425bb2cecSThierry Reding 	/* switch to parent clock */
191525bb2cecSThierry Reding 	err = tegra_sor_set_parent_clock(sor, sor->clk_parent);
1916459cc2c6SThierry Reding 	if (err < 0)
1917459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to set parent clock: %d\n", err);
1918459cc2c6SThierry Reding 
1919459cc2c6SThierry Reding 	value = SOR_INPUT_CONTROL_HDMI_SRC_SELECT(dc->pipe);
1920459cc2c6SThierry Reding 
1921459cc2c6SThierry Reding 	/* XXX is this the proper check? */
1922459cc2c6SThierry Reding 	if (mode->clock < 75000)
1923459cc2c6SThierry Reding 		value |= SOR_INPUT_CONTROL_ARM_VIDEO_RANGE_LIMITED;
1924459cc2c6SThierry Reding 
1925459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_INPUT_CONTROL);
1926459cc2c6SThierry Reding 
1927459cc2c6SThierry Reding 	max_ac = ((mode->htotal - mode->hdisplay) - SOR_REKEY - 18) / 32;
1928459cc2c6SThierry Reding 
1929459cc2c6SThierry Reding 	value = SOR_HDMI_CTRL_ENABLE | SOR_HDMI_CTRL_MAX_AC_PACKET(max_ac) |
1930459cc2c6SThierry Reding 		SOR_HDMI_CTRL_AUDIO_LAYOUT | SOR_HDMI_CTRL_REKEY(SOR_REKEY);
1931459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_CTRL);
1932459cc2c6SThierry Reding 
1933459cc2c6SThierry Reding 	/* H_PULSE2 setup */
1934459cc2c6SThierry Reding 	pulse_start = h_ref_to_sync + (mode->hsync_end - mode->hsync_start) +
1935459cc2c6SThierry Reding 		      (mode->htotal - mode->hsync_end) - 10;
1936459cc2c6SThierry Reding 
1937459cc2c6SThierry Reding 	value = PULSE_LAST_END_A | PULSE_QUAL_VACTIVE |
1938459cc2c6SThierry Reding 		PULSE_POLARITY_HIGH | PULSE_MODE_NORMAL;
1939459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_CONTROL);
1940459cc2c6SThierry Reding 
1941459cc2c6SThierry Reding 	value = PULSE_END(pulse_start + 8) | PULSE_START(pulse_start);
1942459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_POSITION_A);
1943459cc2c6SThierry Reding 
1944459cc2c6SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_SIGNAL_OPTIONS0);
1945459cc2c6SThierry Reding 	value |= H_PULSE2_ENABLE;
1946459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_SIGNAL_OPTIONS0);
1947459cc2c6SThierry Reding 
1948459cc2c6SThierry Reding 	/* infoframe setup */
1949459cc2c6SThierry Reding 	err = tegra_sor_hdmi_setup_avi_infoframe(sor, mode);
1950459cc2c6SThierry Reding 	if (err < 0)
1951459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to setup AVI infoframe: %d\n", err);
1952459cc2c6SThierry Reding 
1953459cc2c6SThierry Reding 	/* XXX HDMI audio support not implemented yet */
1954459cc2c6SThierry Reding 	tegra_sor_hdmi_disable_audio_infoframe(sor);
1955459cc2c6SThierry Reding 
1956459cc2c6SThierry Reding 	/* use single TMDS protocol */
1957459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
1958459cc2c6SThierry Reding 	value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
1959459cc2c6SThierry Reding 	value |= SOR_STATE_ASY_PROTOCOL_SINGLE_TMDS_A;
1960459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
1961459cc2c6SThierry Reding 
1962459cc2c6SThierry Reding 	/* power up pad calibration */
1963459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
1964459cc2c6SThierry Reding 	value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
1965459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
1966459cc2c6SThierry Reding 
1967459cc2c6SThierry Reding 	/* production settings */
1968459cc2c6SThierry Reding 	settings = tegra_sor_hdmi_find_settings(sor, mode->clock * 1000);
1969db8b42fbSDan Carpenter 	if (!settings) {
1970db8b42fbSDan Carpenter 		dev_err(sor->dev, "no settings for pixel clock %d Hz\n",
1971db8b42fbSDan Carpenter 			mode->clock * 1000);
1972459cc2c6SThierry Reding 		return;
1973459cc2c6SThierry Reding 	}
1974459cc2c6SThierry Reding 
1975459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL0);
1976459cc2c6SThierry Reding 	value &= ~SOR_PLL0_ICHPMP_MASK;
1977459cc2c6SThierry Reding 	value &= ~SOR_PLL0_VCOCAP_MASK;
1978459cc2c6SThierry Reding 	value |= SOR_PLL0_ICHPMP(settings->ichpmp);
1979459cc2c6SThierry Reding 	value |= SOR_PLL0_VCOCAP(settings->vcocap);
1980459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL0);
1981459cc2c6SThierry Reding 
1982459cc2c6SThierry Reding 	tegra_sor_dp_term_calibrate(sor);
1983459cc2c6SThierry Reding 
1984459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL1);
1985459cc2c6SThierry Reding 	value &= ~SOR_PLL1_LOADADJ_MASK;
1986459cc2c6SThierry Reding 	value |= SOR_PLL1_LOADADJ(settings->loadadj);
1987459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL1);
1988459cc2c6SThierry Reding 
1989459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL3);
1990459cc2c6SThierry Reding 	value &= ~SOR_PLL3_BG_VREF_LEVEL_MASK;
1991459cc2c6SThierry Reding 	value |= SOR_PLL3_BG_VREF_LEVEL(settings->bg_vref);
1992459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL3);
1993459cc2c6SThierry Reding 
1994459cc2c6SThierry Reding 	value = settings->drive_current[0] << 24 |
1995459cc2c6SThierry Reding 		settings->drive_current[1] << 16 |
1996459cc2c6SThierry Reding 		settings->drive_current[2] <<  8 |
1997459cc2c6SThierry Reding 		settings->drive_current[3] <<  0;
1998459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT0);
1999459cc2c6SThierry Reding 
2000459cc2c6SThierry Reding 	value = settings->preemphasis[0] << 24 |
2001459cc2c6SThierry Reding 		settings->preemphasis[1] << 16 |
2002459cc2c6SThierry Reding 		settings->preemphasis[2] <<  8 |
2003459cc2c6SThierry Reding 		settings->preemphasis[3] <<  0;
2004459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS0);
2005459cc2c6SThierry Reding 
2006459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
2007459cc2c6SThierry Reding 	value &= ~SOR_DP_PADCTL_TX_PU_MASK;
2008459cc2c6SThierry Reding 	value |= SOR_DP_PADCTL_TX_PU_ENABLE;
2009459cc2c6SThierry Reding 	value |= SOR_DP_PADCTL_TX_PU(settings->tx_pu);
2010459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
2011459cc2c6SThierry Reding 
2012459cc2c6SThierry Reding 	/* power down pad calibration */
2013459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
2014459cc2c6SThierry Reding 	value |= SOR_DP_PADCTL_PAD_CAL_PD;
2015459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
2016459cc2c6SThierry Reding 
2017459cc2c6SThierry Reding 	/* miscellaneous display controller settings */
2018459cc2c6SThierry Reding 	value = VSYNC_H_POSITION(1);
2019459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_TIMING_OPTIONS);
2020459cc2c6SThierry Reding 
2021459cc2c6SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_COLOR_CONTROL);
2022459cc2c6SThierry Reding 	value &= ~DITHER_CONTROL_MASK;
2023459cc2c6SThierry Reding 	value &= ~BASE_COLOR_SIZE_MASK;
2024459cc2c6SThierry Reding 
2025459cc2c6SThierry Reding 	switch (info->bpc) {
2026459cc2c6SThierry Reding 	case 6:
2027459cc2c6SThierry Reding 		value |= BASE_COLOR_SIZE_666;
2028459cc2c6SThierry Reding 		break;
2029459cc2c6SThierry Reding 
2030459cc2c6SThierry Reding 	case 8:
2031459cc2c6SThierry Reding 		value |= BASE_COLOR_SIZE_888;
2032459cc2c6SThierry Reding 		break;
2033459cc2c6SThierry Reding 
2034459cc2c6SThierry Reding 	default:
2035459cc2c6SThierry Reding 		WARN(1, "%u bits-per-color not supported\n", info->bpc);
2036459cc2c6SThierry Reding 		break;
2037459cc2c6SThierry Reding 	}
2038459cc2c6SThierry Reding 
2039459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_COLOR_CONTROL);
2040459cc2c6SThierry Reding 
2041459cc2c6SThierry Reding 	err = tegra_sor_power_up(sor, 250);
2042459cc2c6SThierry Reding 	if (err < 0)
2043459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to power up SOR: %d\n", err);
2044459cc2c6SThierry Reding 
2045459cc2c6SThierry Reding 	/* configure mode */
2046459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
2047459cc2c6SThierry Reding 	value &= ~SOR_STATE_ASY_PIXELDEPTH_MASK;
2048459cc2c6SThierry Reding 	value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
2049459cc2c6SThierry Reding 	value &= ~SOR_STATE_ASY_OWNER_MASK;
2050459cc2c6SThierry Reding 
2051459cc2c6SThierry Reding 	value |= SOR_STATE_ASY_CRC_MODE_COMPLETE |
2052459cc2c6SThierry Reding 		 SOR_STATE_ASY_OWNER(dc->pipe + 1);
2053459cc2c6SThierry Reding 
2054459cc2c6SThierry Reding 	if (mode->flags & DRM_MODE_FLAG_PHSYNC)
2055459cc2c6SThierry Reding 		value &= ~SOR_STATE_ASY_HSYNCPOL;
2056459cc2c6SThierry Reding 
2057459cc2c6SThierry Reding 	if (mode->flags & DRM_MODE_FLAG_NHSYNC)
2058459cc2c6SThierry Reding 		value |= SOR_STATE_ASY_HSYNCPOL;
2059459cc2c6SThierry Reding 
2060459cc2c6SThierry Reding 	if (mode->flags & DRM_MODE_FLAG_PVSYNC)
2061459cc2c6SThierry Reding 		value &= ~SOR_STATE_ASY_VSYNCPOL;
2062459cc2c6SThierry Reding 
2063459cc2c6SThierry Reding 	if (mode->flags & DRM_MODE_FLAG_NVSYNC)
2064459cc2c6SThierry Reding 		value |= SOR_STATE_ASY_VSYNCPOL;
2065459cc2c6SThierry Reding 
2066459cc2c6SThierry Reding 	switch (info->bpc) {
2067459cc2c6SThierry Reding 	case 8:
2068459cc2c6SThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
2069459cc2c6SThierry Reding 		break;
2070459cc2c6SThierry Reding 
2071459cc2c6SThierry Reding 	case 6:
2072459cc2c6SThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_18_444;
2073459cc2c6SThierry Reding 		break;
2074459cc2c6SThierry Reding 
2075459cc2c6SThierry Reding 	default:
2076459cc2c6SThierry Reding 		BUG();
2077459cc2c6SThierry Reding 		break;
2078459cc2c6SThierry Reding 	}
2079459cc2c6SThierry Reding 
2080459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
2081459cc2c6SThierry Reding 
2082459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_HEAD_STATE0(dc->pipe));
2083459cc2c6SThierry Reding 	value &= ~SOR_HEAD_STATE_RANGECOMPRESS_MASK;
2084459cc2c6SThierry Reding 	value &= ~SOR_HEAD_STATE_DYNRANGE_MASK;
2085459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HEAD_STATE0(dc->pipe));
2086459cc2c6SThierry Reding 
2087459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_HEAD_STATE0(dc->pipe));
2088459cc2c6SThierry Reding 	value &= ~SOR_HEAD_STATE_COLORSPACE_MASK;
2089459cc2c6SThierry Reding 	value |= SOR_HEAD_STATE_COLORSPACE_RGB;
2090459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HEAD_STATE0(dc->pipe));
2091459cc2c6SThierry Reding 
2092459cc2c6SThierry Reding 	/*
2093459cc2c6SThierry Reding 	 * TODO: The video timing programming below doesn't seem to match the
2094459cc2c6SThierry Reding 	 * register definitions.
2095459cc2c6SThierry Reding 	 */
2096459cc2c6SThierry Reding 
2097459cc2c6SThierry Reding 	value = ((mode->vtotal & 0x7fff) << 16) | (mode->htotal & 0x7fff);
2098459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HEAD_STATE1(dc->pipe));
2099459cc2c6SThierry Reding 
2100459cc2c6SThierry Reding 	/* sync end = sync width - 1 */
2101459cc2c6SThierry Reding 	vse = mode->vsync_end - mode->vsync_start - 1;
2102459cc2c6SThierry Reding 	hse = mode->hsync_end - mode->hsync_start - 1;
2103459cc2c6SThierry Reding 
2104459cc2c6SThierry Reding 	value = ((vse & 0x7fff) << 16) | (hse & 0x7fff);
2105459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HEAD_STATE2(dc->pipe));
2106459cc2c6SThierry Reding 
2107459cc2c6SThierry Reding 	/* blank end = sync end + back porch */
2108459cc2c6SThierry Reding 	vbe = vse + (mode->vtotal - mode->vsync_end);
2109459cc2c6SThierry Reding 	hbe = hse + (mode->htotal - mode->hsync_end);
2110459cc2c6SThierry Reding 
2111459cc2c6SThierry Reding 	value = ((vbe & 0x7fff) << 16) | (hbe & 0x7fff);
2112459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HEAD_STATE3(dc->pipe));
2113459cc2c6SThierry Reding 
2114459cc2c6SThierry Reding 	/* blank start = blank end + active */
2115459cc2c6SThierry Reding 	vbs = vbe + mode->vdisplay;
2116459cc2c6SThierry Reding 	hbs = hbe + mode->hdisplay;
2117459cc2c6SThierry Reding 
2118459cc2c6SThierry Reding 	value = ((vbs & 0x7fff) << 16) | (hbs & 0x7fff);
2119459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HEAD_STATE4(dc->pipe));
2120459cc2c6SThierry Reding 
2121459cc2c6SThierry Reding 	tegra_sor_writel(sor, 0x1, SOR_HEAD_STATE5(dc->pipe));
2122459cc2c6SThierry Reding 
2123459cc2c6SThierry Reding 	tegra_sor_update(sor);
2124459cc2c6SThierry Reding 
2125459cc2c6SThierry Reding 	err = tegra_sor_attach(sor);
2126459cc2c6SThierry Reding 	if (err < 0)
2127459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to attach SOR: %d\n", err);
2128459cc2c6SThierry Reding 
2129459cc2c6SThierry Reding 	/* enable display to SOR clock and generate HDMI preamble */
2130459cc2c6SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
2131459cc2c6SThierry Reding 	value |= SOR1_ENABLE | SOR1_TIMING_CYA;
2132459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
2133459cc2c6SThierry Reding 
2134459cc2c6SThierry Reding 	tegra_dc_commit(dc);
2135459cc2c6SThierry Reding 
2136459cc2c6SThierry Reding 	err = tegra_sor_wakeup(sor);
2137459cc2c6SThierry Reding 	if (err < 0)
2138459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to wakeup SOR: %d\n", err);
2139459cc2c6SThierry Reding }
2140459cc2c6SThierry Reding 
2141459cc2c6SThierry Reding static const struct drm_encoder_helper_funcs tegra_sor_hdmi_helpers = {
2142459cc2c6SThierry Reding 	.disable = tegra_sor_hdmi_disable,
2143459cc2c6SThierry Reding 	.enable = tegra_sor_hdmi_enable,
2144459cc2c6SThierry Reding 	.atomic_check = tegra_sor_encoder_atomic_check,
2145459cc2c6SThierry Reding };
2146459cc2c6SThierry Reding 
21476b6b6042SThierry Reding static int tegra_sor_init(struct host1x_client *client)
21486b6b6042SThierry Reding {
21499910f5c4SThierry Reding 	struct drm_device *drm = dev_get_drvdata(client->parent);
2150459cc2c6SThierry Reding 	const struct drm_encoder_helper_funcs *helpers = NULL;
21516b6b6042SThierry Reding 	struct tegra_sor *sor = host1x_client_to_sor(client);
2152459cc2c6SThierry Reding 	int connector = DRM_MODE_CONNECTOR_Unknown;
2153459cc2c6SThierry Reding 	int encoder = DRM_MODE_ENCODER_NONE;
21546b6b6042SThierry Reding 	int err;
21556b6b6042SThierry Reding 
21569542c237SThierry Reding 	if (!sor->aux) {
2157459cc2c6SThierry Reding 		if (sor->soc->supports_hdmi) {
2158459cc2c6SThierry Reding 			connector = DRM_MODE_CONNECTOR_HDMIA;
2159459cc2c6SThierry Reding 			encoder = DRM_MODE_ENCODER_TMDS;
2160459cc2c6SThierry Reding 			helpers = &tegra_sor_hdmi_helpers;
2161459cc2c6SThierry Reding 		} else if (sor->soc->supports_lvds) {
2162459cc2c6SThierry Reding 			connector = DRM_MODE_CONNECTOR_LVDS;
2163459cc2c6SThierry Reding 			encoder = DRM_MODE_ENCODER_LVDS;
2164459cc2c6SThierry Reding 		}
2165459cc2c6SThierry Reding 	} else {
2166459cc2c6SThierry Reding 		if (sor->soc->supports_edp) {
2167459cc2c6SThierry Reding 			connector = DRM_MODE_CONNECTOR_eDP;
2168459cc2c6SThierry Reding 			encoder = DRM_MODE_ENCODER_TMDS;
2169459cc2c6SThierry Reding 			helpers = &tegra_sor_edp_helpers;
2170459cc2c6SThierry Reding 		} else if (sor->soc->supports_dp) {
2171459cc2c6SThierry Reding 			connector = DRM_MODE_CONNECTOR_DisplayPort;
2172459cc2c6SThierry Reding 			encoder = DRM_MODE_ENCODER_TMDS;
2173459cc2c6SThierry Reding 		}
2174459cc2c6SThierry Reding 	}
21756b6b6042SThierry Reding 
21766b6b6042SThierry Reding 	sor->output.dev = sor->dev;
21776b6b6042SThierry Reding 
21786fad8f66SThierry Reding 	drm_connector_init(drm, &sor->output.connector,
21796fad8f66SThierry Reding 			   &tegra_sor_connector_funcs,
2180459cc2c6SThierry Reding 			   connector);
21816fad8f66SThierry Reding 	drm_connector_helper_add(&sor->output.connector,
21826fad8f66SThierry Reding 				 &tegra_sor_connector_helper_funcs);
21836fad8f66SThierry Reding 	sor->output.connector.dpms = DRM_MODE_DPMS_OFF;
21846fad8f66SThierry Reding 
21856fad8f66SThierry Reding 	drm_encoder_init(drm, &sor->output.encoder, &tegra_sor_encoder_funcs,
218613a3d91fSVille Syrjälä 			 encoder, NULL);
2187459cc2c6SThierry Reding 	drm_encoder_helper_add(&sor->output.encoder, helpers);
21886fad8f66SThierry Reding 
21896fad8f66SThierry Reding 	drm_mode_connector_attach_encoder(&sor->output.connector,
21906fad8f66SThierry Reding 					  &sor->output.encoder);
21916fad8f66SThierry Reding 	drm_connector_register(&sor->output.connector);
21926fad8f66SThierry Reding 
2193ea130b24SThierry Reding 	err = tegra_output_init(drm, &sor->output);
2194ea130b24SThierry Reding 	if (err < 0) {
2195ea130b24SThierry Reding 		dev_err(client->dev, "failed to initialize output: %d\n", err);
2196ea130b24SThierry Reding 		return err;
2197ea130b24SThierry Reding 	}
21986fad8f66SThierry Reding 
2199ea130b24SThierry Reding 	sor->output.encoder.possible_crtcs = 0x3;
22006b6b6042SThierry Reding 
2201a82752e1SThierry Reding 	if (IS_ENABLED(CONFIG_DEBUG_FS)) {
22021b0c7b48SThierry Reding 		err = tegra_sor_debugfs_init(sor, drm->primary);
2203a82752e1SThierry Reding 		if (err < 0)
2204a82752e1SThierry Reding 			dev_err(sor->dev, "debugfs setup failed: %d\n", err);
2205a82752e1SThierry Reding 	}
2206a82752e1SThierry Reding 
22079542c237SThierry Reding 	if (sor->aux) {
22089542c237SThierry Reding 		err = drm_dp_aux_attach(sor->aux, &sor->output);
22096b6b6042SThierry Reding 		if (err < 0) {
22106b6b6042SThierry Reding 			dev_err(sor->dev, "failed to attach DP: %d\n", err);
22116b6b6042SThierry Reding 			return err;
22126b6b6042SThierry Reding 		}
22136b6b6042SThierry Reding 	}
22146b6b6042SThierry Reding 
2215535a65dbSTomeu Vizoso 	/*
2216535a65dbSTomeu Vizoso 	 * XXX: Remove this reset once proper hand-over from firmware to
2217535a65dbSTomeu Vizoso 	 * kernel is possible.
2218535a65dbSTomeu Vizoso 	 */
2219535a65dbSTomeu Vizoso 	err = reset_control_assert(sor->rst);
2220535a65dbSTomeu Vizoso 	if (err < 0) {
2221535a65dbSTomeu Vizoso 		dev_err(sor->dev, "failed to assert SOR reset: %d\n", err);
2222535a65dbSTomeu Vizoso 		return err;
2223535a65dbSTomeu Vizoso 	}
2224535a65dbSTomeu Vizoso 
22256fad8f66SThierry Reding 	err = clk_prepare_enable(sor->clk);
22266fad8f66SThierry Reding 	if (err < 0) {
22276fad8f66SThierry Reding 		dev_err(sor->dev, "failed to enable clock: %d\n", err);
22286fad8f66SThierry Reding 		return err;
22296fad8f66SThierry Reding 	}
22306fad8f66SThierry Reding 
2231535a65dbSTomeu Vizoso 	usleep_range(1000, 3000);
2232535a65dbSTomeu Vizoso 
2233535a65dbSTomeu Vizoso 	err = reset_control_deassert(sor->rst);
2234535a65dbSTomeu Vizoso 	if (err < 0) {
2235535a65dbSTomeu Vizoso 		dev_err(sor->dev, "failed to deassert SOR reset: %d\n", err);
2236535a65dbSTomeu Vizoso 		return err;
2237535a65dbSTomeu Vizoso 	}
2238535a65dbSTomeu Vizoso 
22396fad8f66SThierry Reding 	err = clk_prepare_enable(sor->clk_safe);
22406fad8f66SThierry Reding 	if (err < 0)
22416fad8f66SThierry Reding 		return err;
22426fad8f66SThierry Reding 
22436fad8f66SThierry Reding 	err = clk_prepare_enable(sor->clk_dp);
22446fad8f66SThierry Reding 	if (err < 0)
22456fad8f66SThierry Reding 		return err;
22466fad8f66SThierry Reding 
22476b6b6042SThierry Reding 	return 0;
22486b6b6042SThierry Reding }
22496b6b6042SThierry Reding 
22506b6b6042SThierry Reding static int tegra_sor_exit(struct host1x_client *client)
22516b6b6042SThierry Reding {
22526b6b6042SThierry Reding 	struct tegra_sor *sor = host1x_client_to_sor(client);
22536b6b6042SThierry Reding 	int err;
22546b6b6042SThierry Reding 
2255328ec69eSThierry Reding 	tegra_output_exit(&sor->output);
2256328ec69eSThierry Reding 
22579542c237SThierry Reding 	if (sor->aux) {
22589542c237SThierry Reding 		err = drm_dp_aux_detach(sor->aux);
22596b6b6042SThierry Reding 		if (err < 0) {
22606b6b6042SThierry Reding 			dev_err(sor->dev, "failed to detach DP: %d\n", err);
22616b6b6042SThierry Reding 			return err;
22626b6b6042SThierry Reding 		}
22636b6b6042SThierry Reding 	}
22646b6b6042SThierry Reding 
22656fad8f66SThierry Reding 	clk_disable_unprepare(sor->clk_safe);
22666fad8f66SThierry Reding 	clk_disable_unprepare(sor->clk_dp);
22676fad8f66SThierry Reding 	clk_disable_unprepare(sor->clk);
22686fad8f66SThierry Reding 
22694009c224SThierry Reding 	if (IS_ENABLED(CONFIG_DEBUG_FS))
22704009c224SThierry Reding 		tegra_sor_debugfs_exit(sor);
2271a82752e1SThierry Reding 
22726b6b6042SThierry Reding 	return 0;
22736b6b6042SThierry Reding }
22746b6b6042SThierry Reding 
22756b6b6042SThierry Reding static const struct host1x_client_ops sor_client_ops = {
22766b6b6042SThierry Reding 	.init = tegra_sor_init,
22776b6b6042SThierry Reding 	.exit = tegra_sor_exit,
22786b6b6042SThierry Reding };
22796b6b6042SThierry Reding 
2280459cc2c6SThierry Reding static const struct tegra_sor_ops tegra_sor_edp_ops = {
2281459cc2c6SThierry Reding 	.name = "eDP",
2282459cc2c6SThierry Reding };
2283459cc2c6SThierry Reding 
2284459cc2c6SThierry Reding static int tegra_sor_hdmi_probe(struct tegra_sor *sor)
2285459cc2c6SThierry Reding {
2286459cc2c6SThierry Reding 	int err;
2287459cc2c6SThierry Reding 
2288459cc2c6SThierry Reding 	sor->avdd_io_supply = devm_regulator_get(sor->dev, "avdd-io");
2289459cc2c6SThierry Reding 	if (IS_ERR(sor->avdd_io_supply)) {
2290459cc2c6SThierry Reding 		dev_err(sor->dev, "cannot get AVDD I/O supply: %ld\n",
2291459cc2c6SThierry Reding 			PTR_ERR(sor->avdd_io_supply));
2292459cc2c6SThierry Reding 		return PTR_ERR(sor->avdd_io_supply);
2293459cc2c6SThierry Reding 	}
2294459cc2c6SThierry Reding 
2295459cc2c6SThierry Reding 	err = regulator_enable(sor->avdd_io_supply);
2296459cc2c6SThierry Reding 	if (err < 0) {
2297459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to enable AVDD I/O supply: %d\n",
2298459cc2c6SThierry Reding 			err);
2299459cc2c6SThierry Reding 		return err;
2300459cc2c6SThierry Reding 	}
2301459cc2c6SThierry Reding 
2302459cc2c6SThierry Reding 	sor->vdd_pll_supply = devm_regulator_get(sor->dev, "vdd-pll");
2303459cc2c6SThierry Reding 	if (IS_ERR(sor->vdd_pll_supply)) {
2304459cc2c6SThierry Reding 		dev_err(sor->dev, "cannot get VDD PLL supply: %ld\n",
2305459cc2c6SThierry Reding 			PTR_ERR(sor->vdd_pll_supply));
2306459cc2c6SThierry Reding 		return PTR_ERR(sor->vdd_pll_supply);
2307459cc2c6SThierry Reding 	}
2308459cc2c6SThierry Reding 
2309459cc2c6SThierry Reding 	err = regulator_enable(sor->vdd_pll_supply);
2310459cc2c6SThierry Reding 	if (err < 0) {
2311459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to enable VDD PLL supply: %d\n",
2312459cc2c6SThierry Reding 			err);
2313459cc2c6SThierry Reding 		return err;
2314459cc2c6SThierry Reding 	}
2315459cc2c6SThierry Reding 
2316459cc2c6SThierry Reding 	sor->hdmi_supply = devm_regulator_get(sor->dev, "hdmi");
2317459cc2c6SThierry Reding 	if (IS_ERR(sor->hdmi_supply)) {
2318459cc2c6SThierry Reding 		dev_err(sor->dev, "cannot get HDMI supply: %ld\n",
2319459cc2c6SThierry Reding 			PTR_ERR(sor->hdmi_supply));
2320459cc2c6SThierry Reding 		return PTR_ERR(sor->hdmi_supply);
2321459cc2c6SThierry Reding 	}
2322459cc2c6SThierry Reding 
2323459cc2c6SThierry Reding 	err = regulator_enable(sor->hdmi_supply);
2324459cc2c6SThierry Reding 	if (err < 0) {
2325459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to enable HDMI supply: %d\n", err);
2326459cc2c6SThierry Reding 		return err;
2327459cc2c6SThierry Reding 	}
2328459cc2c6SThierry Reding 
2329459cc2c6SThierry Reding 	return 0;
2330459cc2c6SThierry Reding }
2331459cc2c6SThierry Reding 
2332459cc2c6SThierry Reding static int tegra_sor_hdmi_remove(struct tegra_sor *sor)
2333459cc2c6SThierry Reding {
2334459cc2c6SThierry Reding 	regulator_disable(sor->hdmi_supply);
2335459cc2c6SThierry Reding 	regulator_disable(sor->vdd_pll_supply);
2336459cc2c6SThierry Reding 	regulator_disable(sor->avdd_io_supply);
2337459cc2c6SThierry Reding 
2338459cc2c6SThierry Reding 	return 0;
2339459cc2c6SThierry Reding }
2340459cc2c6SThierry Reding 
2341459cc2c6SThierry Reding static const struct tegra_sor_ops tegra_sor_hdmi_ops = {
2342459cc2c6SThierry Reding 	.name = "HDMI",
2343459cc2c6SThierry Reding 	.probe = tegra_sor_hdmi_probe,
2344459cc2c6SThierry Reding 	.remove = tegra_sor_hdmi_remove,
2345459cc2c6SThierry Reding };
2346459cc2c6SThierry Reding 
2347459cc2c6SThierry Reding static const struct tegra_sor_soc tegra124_sor = {
2348459cc2c6SThierry Reding 	.supports_edp = true,
2349459cc2c6SThierry Reding 	.supports_lvds = true,
2350459cc2c6SThierry Reding 	.supports_hdmi = false,
2351459cc2c6SThierry Reding 	.supports_dp = false,
2352459cc2c6SThierry Reding };
2353459cc2c6SThierry Reding 
2354459cc2c6SThierry Reding static const struct tegra_sor_soc tegra210_sor = {
2355459cc2c6SThierry Reding 	.supports_edp = true,
2356459cc2c6SThierry Reding 	.supports_lvds = false,
2357459cc2c6SThierry Reding 	.supports_hdmi = false,
2358459cc2c6SThierry Reding 	.supports_dp = false,
2359459cc2c6SThierry Reding };
2360459cc2c6SThierry Reding 
2361459cc2c6SThierry Reding static const struct tegra_sor_soc tegra210_sor1 = {
2362459cc2c6SThierry Reding 	.supports_edp = false,
2363459cc2c6SThierry Reding 	.supports_lvds = false,
2364459cc2c6SThierry Reding 	.supports_hdmi = true,
2365459cc2c6SThierry Reding 	.supports_dp = true,
2366459cc2c6SThierry Reding 
2367459cc2c6SThierry Reding 	.num_settings = ARRAY_SIZE(tegra210_sor_hdmi_defaults),
2368459cc2c6SThierry Reding 	.settings = tegra210_sor_hdmi_defaults,
2369459cc2c6SThierry Reding };
2370459cc2c6SThierry Reding 
2371459cc2c6SThierry Reding static const struct of_device_id tegra_sor_of_match[] = {
2372459cc2c6SThierry Reding 	{ .compatible = "nvidia,tegra210-sor1", .data = &tegra210_sor1 },
2373459cc2c6SThierry Reding 	{ .compatible = "nvidia,tegra210-sor", .data = &tegra210_sor },
2374459cc2c6SThierry Reding 	{ .compatible = "nvidia,tegra124-sor", .data = &tegra124_sor },
2375459cc2c6SThierry Reding 	{ },
2376459cc2c6SThierry Reding };
2377459cc2c6SThierry Reding MODULE_DEVICE_TABLE(of, tegra_sor_of_match);
2378459cc2c6SThierry Reding 
23796b6b6042SThierry Reding static int tegra_sor_probe(struct platform_device *pdev)
23806b6b6042SThierry Reding {
2381459cc2c6SThierry Reding 	const struct of_device_id *match;
23826b6b6042SThierry Reding 	struct device_node *np;
23836b6b6042SThierry Reding 	struct tegra_sor *sor;
23846b6b6042SThierry Reding 	struct resource *regs;
23856b6b6042SThierry Reding 	int err;
23866b6b6042SThierry Reding 
2387459cc2c6SThierry Reding 	match = of_match_device(tegra_sor_of_match, &pdev->dev);
2388459cc2c6SThierry Reding 
23896b6b6042SThierry Reding 	sor = devm_kzalloc(&pdev->dev, sizeof(*sor), GFP_KERNEL);
23906b6b6042SThierry Reding 	if (!sor)
23916b6b6042SThierry Reding 		return -ENOMEM;
23926b6b6042SThierry Reding 
23936b6b6042SThierry Reding 	sor->output.dev = sor->dev = &pdev->dev;
2394459cc2c6SThierry Reding 	sor->soc = match->data;
2395459cc2c6SThierry Reding 
2396459cc2c6SThierry Reding 	sor->settings = devm_kmemdup(&pdev->dev, sor->soc->settings,
2397459cc2c6SThierry Reding 				     sor->soc->num_settings *
2398459cc2c6SThierry Reding 					sizeof(*sor->settings),
2399459cc2c6SThierry Reding 				     GFP_KERNEL);
2400459cc2c6SThierry Reding 	if (!sor->settings)
2401459cc2c6SThierry Reding 		return -ENOMEM;
2402459cc2c6SThierry Reding 
2403459cc2c6SThierry Reding 	sor->num_settings = sor->soc->num_settings;
24046b6b6042SThierry Reding 
24056b6b6042SThierry Reding 	np = of_parse_phandle(pdev->dev.of_node, "nvidia,dpaux", 0);
24066b6b6042SThierry Reding 	if (np) {
24079542c237SThierry Reding 		sor->aux = drm_dp_aux_find_by_of_node(np);
24086b6b6042SThierry Reding 		of_node_put(np);
24096b6b6042SThierry Reding 
24109542c237SThierry Reding 		if (!sor->aux)
24116b6b6042SThierry Reding 			return -EPROBE_DEFER;
24126b6b6042SThierry Reding 	}
24136b6b6042SThierry Reding 
24149542c237SThierry Reding 	if (!sor->aux) {
2415459cc2c6SThierry Reding 		if (sor->soc->supports_hdmi) {
2416459cc2c6SThierry Reding 			sor->ops = &tegra_sor_hdmi_ops;
2417459cc2c6SThierry Reding 		} else if (sor->soc->supports_lvds) {
2418459cc2c6SThierry Reding 			dev_err(&pdev->dev, "LVDS not supported yet\n");
2419459cc2c6SThierry Reding 			return -ENODEV;
2420459cc2c6SThierry Reding 		} else {
2421459cc2c6SThierry Reding 			dev_err(&pdev->dev, "unknown (non-DP) support\n");
2422459cc2c6SThierry Reding 			return -ENODEV;
2423459cc2c6SThierry Reding 		}
2424459cc2c6SThierry Reding 	} else {
2425459cc2c6SThierry Reding 		if (sor->soc->supports_edp) {
2426459cc2c6SThierry Reding 			sor->ops = &tegra_sor_edp_ops;
2427459cc2c6SThierry Reding 		} else if (sor->soc->supports_dp) {
2428459cc2c6SThierry Reding 			dev_err(&pdev->dev, "DisplayPort not supported yet\n");
2429459cc2c6SThierry Reding 			return -ENODEV;
2430459cc2c6SThierry Reding 		} else {
2431459cc2c6SThierry Reding 			dev_err(&pdev->dev, "unknown (DP) support\n");
2432459cc2c6SThierry Reding 			return -ENODEV;
2433459cc2c6SThierry Reding 		}
2434459cc2c6SThierry Reding 	}
2435459cc2c6SThierry Reding 
24366b6b6042SThierry Reding 	err = tegra_output_probe(&sor->output);
24374dbdc740SThierry Reding 	if (err < 0) {
24384dbdc740SThierry Reding 		dev_err(&pdev->dev, "failed to probe output: %d\n", err);
24396b6b6042SThierry Reding 		return err;
24404dbdc740SThierry Reding 	}
24416b6b6042SThierry Reding 
2442459cc2c6SThierry Reding 	if (sor->ops && sor->ops->probe) {
2443459cc2c6SThierry Reding 		err = sor->ops->probe(sor);
2444459cc2c6SThierry Reding 		if (err < 0) {
2445459cc2c6SThierry Reding 			dev_err(&pdev->dev, "failed to probe %s: %d\n",
2446459cc2c6SThierry Reding 				sor->ops->name, err);
2447459cc2c6SThierry Reding 			goto output;
2448459cc2c6SThierry Reding 		}
2449459cc2c6SThierry Reding 	}
2450459cc2c6SThierry Reding 
24516b6b6042SThierry Reding 	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
24526b6b6042SThierry Reding 	sor->regs = devm_ioremap_resource(&pdev->dev, regs);
2453459cc2c6SThierry Reding 	if (IS_ERR(sor->regs)) {
2454459cc2c6SThierry Reding 		err = PTR_ERR(sor->regs);
2455459cc2c6SThierry Reding 		goto remove;
2456459cc2c6SThierry Reding 	}
24576b6b6042SThierry Reding 
24586b6b6042SThierry Reding 	sor->rst = devm_reset_control_get(&pdev->dev, "sor");
24594dbdc740SThierry Reding 	if (IS_ERR(sor->rst)) {
2460459cc2c6SThierry Reding 		err = PTR_ERR(sor->rst);
2461459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get reset control: %d\n", err);
2462459cc2c6SThierry Reding 		goto remove;
24634dbdc740SThierry Reding 	}
24646b6b6042SThierry Reding 
24656b6b6042SThierry Reding 	sor->clk = devm_clk_get(&pdev->dev, NULL);
24664dbdc740SThierry Reding 	if (IS_ERR(sor->clk)) {
2467459cc2c6SThierry Reding 		err = PTR_ERR(sor->clk);
2468459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get module clock: %d\n", err);
2469459cc2c6SThierry Reding 		goto remove;
24704dbdc740SThierry Reding 	}
24716b6b6042SThierry Reding 
24726b6b6042SThierry Reding 	sor->clk_parent = devm_clk_get(&pdev->dev, "parent");
24734dbdc740SThierry Reding 	if (IS_ERR(sor->clk_parent)) {
2474459cc2c6SThierry Reding 		err = PTR_ERR(sor->clk_parent);
2475459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get parent clock: %d\n", err);
2476459cc2c6SThierry Reding 		goto remove;
24774dbdc740SThierry Reding 	}
24786b6b6042SThierry Reding 
24796b6b6042SThierry Reding 	sor->clk_safe = devm_clk_get(&pdev->dev, "safe");
24804dbdc740SThierry Reding 	if (IS_ERR(sor->clk_safe)) {
2481459cc2c6SThierry Reding 		err = PTR_ERR(sor->clk_safe);
2482459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get safe clock: %d\n", err);
2483459cc2c6SThierry Reding 		goto remove;
24844dbdc740SThierry Reding 	}
24856b6b6042SThierry Reding 
24866b6b6042SThierry Reding 	sor->clk_dp = devm_clk_get(&pdev->dev, "dp");
24874dbdc740SThierry Reding 	if (IS_ERR(sor->clk_dp)) {
2488459cc2c6SThierry Reding 		err = PTR_ERR(sor->clk_dp);
2489459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get DP clock: %d\n", err);
2490459cc2c6SThierry Reding 		goto remove;
24914dbdc740SThierry Reding 	}
24926b6b6042SThierry Reding 
24936b6b6042SThierry Reding 	INIT_LIST_HEAD(&sor->client.list);
24946b6b6042SThierry Reding 	sor->client.ops = &sor_client_ops;
24956b6b6042SThierry Reding 	sor->client.dev = &pdev->dev;
24966b6b6042SThierry Reding 
24976b6b6042SThierry Reding 	err = host1x_client_register(&sor->client);
24986b6b6042SThierry Reding 	if (err < 0) {
24996b6b6042SThierry Reding 		dev_err(&pdev->dev, "failed to register host1x client: %d\n",
25006b6b6042SThierry Reding 			err);
2501459cc2c6SThierry Reding 		goto remove;
25026b6b6042SThierry Reding 	}
25036b6b6042SThierry Reding 
25046b6b6042SThierry Reding 	platform_set_drvdata(pdev, sor);
25056b6b6042SThierry Reding 
25066b6b6042SThierry Reding 	return 0;
2507459cc2c6SThierry Reding 
2508459cc2c6SThierry Reding remove:
2509459cc2c6SThierry Reding 	if (sor->ops && sor->ops->remove)
2510459cc2c6SThierry Reding 		sor->ops->remove(sor);
2511459cc2c6SThierry Reding output:
2512459cc2c6SThierry Reding 	tegra_output_remove(&sor->output);
2513459cc2c6SThierry Reding 	return err;
25146b6b6042SThierry Reding }
25156b6b6042SThierry Reding 
25166b6b6042SThierry Reding static int tegra_sor_remove(struct platform_device *pdev)
25176b6b6042SThierry Reding {
25186b6b6042SThierry Reding 	struct tegra_sor *sor = platform_get_drvdata(pdev);
25196b6b6042SThierry Reding 	int err;
25206b6b6042SThierry Reding 
25216b6b6042SThierry Reding 	err = host1x_client_unregister(&sor->client);
25226b6b6042SThierry Reding 	if (err < 0) {
25236b6b6042SThierry Reding 		dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
25246b6b6042SThierry Reding 			err);
25256b6b6042SThierry Reding 		return err;
25266b6b6042SThierry Reding 	}
25276b6b6042SThierry Reding 
2528459cc2c6SThierry Reding 	if (sor->ops && sor->ops->remove) {
2529459cc2c6SThierry Reding 		err = sor->ops->remove(sor);
2530459cc2c6SThierry Reding 		if (err < 0)
2531459cc2c6SThierry Reding 			dev_err(&pdev->dev, "failed to remove SOR: %d\n", err);
2532459cc2c6SThierry Reding 	}
2533459cc2c6SThierry Reding 
2534328ec69eSThierry Reding 	tegra_output_remove(&sor->output);
25356b6b6042SThierry Reding 
25366b6b6042SThierry Reding 	return 0;
25376b6b6042SThierry Reding }
25386b6b6042SThierry Reding 
25396b6b6042SThierry Reding struct platform_driver tegra_sor_driver = {
25406b6b6042SThierry Reding 	.driver = {
25416b6b6042SThierry Reding 		.name = "tegra-sor",
25426b6b6042SThierry Reding 		.of_match_table = tegra_sor_of_match,
25436b6b6042SThierry Reding 	},
25446b6b6042SThierry Reding 	.probe = tegra_sor_probe,
25456b6b6042SThierry Reding 	.remove = tegra_sor_remove,
25466b6b6042SThierry Reding };
2547