xref: /openbmc/linux/drivers/gpu/drm/tegra/sor.c (revision 459cc2c6800b545a482e428a631d99bca8da7790)
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>
13*459cc2c6SThierry Reding #include <linux/of_device.h>
146b6b6042SThierry Reding #include <linux/platform_device.h>
15*459cc2c6SThierry 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 
28*459cc2c6SThierry Reding #define SOR_REKEY 0x38
29*459cc2c6SThierry Reding 
30*459cc2c6SThierry Reding struct tegra_sor_hdmi_settings {
31*459cc2c6SThierry Reding 	unsigned long frequency;
32*459cc2c6SThierry Reding 
33*459cc2c6SThierry Reding 	u8 vcocap;
34*459cc2c6SThierry Reding 	u8 ichpmp;
35*459cc2c6SThierry Reding 	u8 loadadj;
36*459cc2c6SThierry Reding 	u8 termadj;
37*459cc2c6SThierry Reding 	u8 tx_pu;
38*459cc2c6SThierry Reding 	u8 bg_vref;
39*459cc2c6SThierry Reding 
40*459cc2c6SThierry Reding 	u8 drive_current[4];
41*459cc2c6SThierry Reding 	u8 preemphasis[4];
42*459cc2c6SThierry Reding };
43*459cc2c6SThierry Reding 
44*459cc2c6SThierry Reding #if 1
45*459cc2c6SThierry Reding static const struct tegra_sor_hdmi_settings tegra210_sor_hdmi_defaults[] = {
46*459cc2c6SThierry Reding 	{
47*459cc2c6SThierry Reding 		.frequency = 54000000,
48*459cc2c6SThierry Reding 		.vcocap = 0x0,
49*459cc2c6SThierry Reding 		.ichpmp = 0x1,
50*459cc2c6SThierry Reding 		.loadadj = 0x3,
51*459cc2c6SThierry Reding 		.termadj = 0x9,
52*459cc2c6SThierry Reding 		.tx_pu = 0x10,
53*459cc2c6SThierry Reding 		.bg_vref = 0x8,
54*459cc2c6SThierry Reding 		.drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
55*459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
56*459cc2c6SThierry Reding 	}, {
57*459cc2c6SThierry Reding 		.frequency = 75000000,
58*459cc2c6SThierry Reding 		.vcocap = 0x3,
59*459cc2c6SThierry Reding 		.ichpmp = 0x1,
60*459cc2c6SThierry Reding 		.loadadj = 0x3,
61*459cc2c6SThierry Reding 		.termadj = 0x9,
62*459cc2c6SThierry Reding 		.tx_pu = 0x40,
63*459cc2c6SThierry Reding 		.bg_vref = 0x8,
64*459cc2c6SThierry Reding 		.drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
65*459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
66*459cc2c6SThierry Reding 	}, {
67*459cc2c6SThierry Reding 		.frequency = 150000000,
68*459cc2c6SThierry Reding 		.vcocap = 0x3,
69*459cc2c6SThierry Reding 		.ichpmp = 0x1,
70*459cc2c6SThierry Reding 		.loadadj = 0x3,
71*459cc2c6SThierry Reding 		.termadj = 0x9,
72*459cc2c6SThierry Reding 		.tx_pu = 0x66,
73*459cc2c6SThierry Reding 		.bg_vref = 0x8,
74*459cc2c6SThierry Reding 		.drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
75*459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
76*459cc2c6SThierry Reding 	}, {
77*459cc2c6SThierry Reding 		.frequency = 300000000,
78*459cc2c6SThierry Reding 		.vcocap = 0x3,
79*459cc2c6SThierry Reding 		.ichpmp = 0x1,
80*459cc2c6SThierry Reding 		.loadadj = 0x3,
81*459cc2c6SThierry Reding 		.termadj = 0x9,
82*459cc2c6SThierry Reding 		.tx_pu = 0x66,
83*459cc2c6SThierry Reding 		.bg_vref = 0xa,
84*459cc2c6SThierry Reding 		.drive_current = { 0x33, 0x3f, 0x3f, 0x3f },
85*459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x17, 0x17, 0x17 },
86*459cc2c6SThierry Reding 	}, {
87*459cc2c6SThierry Reding 		.frequency = 600000000,
88*459cc2c6SThierry Reding 		.vcocap = 0x3,
89*459cc2c6SThierry Reding 		.ichpmp = 0x1,
90*459cc2c6SThierry Reding 		.loadadj = 0x3,
91*459cc2c6SThierry Reding 		.termadj = 0x9,
92*459cc2c6SThierry Reding 		.tx_pu = 0x66,
93*459cc2c6SThierry Reding 		.bg_vref = 0x8,
94*459cc2c6SThierry Reding 		.drive_current = { 0x33, 0x3f, 0x3f, 0x3f },
95*459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
96*459cc2c6SThierry Reding 	},
97*459cc2c6SThierry Reding };
98*459cc2c6SThierry Reding #else
99*459cc2c6SThierry Reding static const struct tegra_sor_hdmi_settings tegra210_sor_hdmi_defaults[] = {
100*459cc2c6SThierry Reding 	{
101*459cc2c6SThierry Reding 		.frequency = 75000000,
102*459cc2c6SThierry Reding 		.vcocap = 0x3,
103*459cc2c6SThierry Reding 		.ichpmp = 0x1,
104*459cc2c6SThierry Reding 		.loadadj = 0x3,
105*459cc2c6SThierry Reding 		.termadj = 0x9,
106*459cc2c6SThierry Reding 		.tx_pu = 0x40,
107*459cc2c6SThierry Reding 		.bg_vref = 0x8,
108*459cc2c6SThierry Reding 		.drive_current = { 0x29, 0x29, 0x29, 0x29 },
109*459cc2c6SThierry Reding 		.preemphasis = { 0x00, 0x00, 0x00, 0x00 },
110*459cc2c6SThierry Reding 	}, {
111*459cc2c6SThierry Reding 		.frequency = 150000000,
112*459cc2c6SThierry Reding 		.vcocap = 0x3,
113*459cc2c6SThierry Reding 		.ichpmp = 0x1,
114*459cc2c6SThierry Reding 		.loadadj = 0x3,
115*459cc2c6SThierry Reding 		.termadj = 0x9,
116*459cc2c6SThierry Reding 		.tx_pu = 0x66,
117*459cc2c6SThierry Reding 		.bg_vref = 0x8,
118*459cc2c6SThierry Reding 		.drive_current = { 0x30, 0x37, 0x37, 0x37 },
119*459cc2c6SThierry Reding 		.preemphasis = { 0x01, 0x02, 0x02, 0x02 },
120*459cc2c6SThierry Reding 	}, {
121*459cc2c6SThierry Reding 		.frequency = 300000000,
122*459cc2c6SThierry Reding 		.vcocap = 0x3,
123*459cc2c6SThierry Reding 		.ichpmp = 0x6,
124*459cc2c6SThierry Reding 		.loadadj = 0x3,
125*459cc2c6SThierry Reding 		.termadj = 0x9,
126*459cc2c6SThierry Reding 		.tx_pu = 0x66,
127*459cc2c6SThierry Reding 		.bg_vref = 0xf,
128*459cc2c6SThierry Reding 		.drive_current = { 0x30, 0x37, 0x37, 0x37 },
129*459cc2c6SThierry Reding 		.preemphasis = { 0x10, 0x3e, 0x3e, 0x3e },
130*459cc2c6SThierry Reding 	}, {
131*459cc2c6SThierry Reding 		.frequency = 600000000,
132*459cc2c6SThierry Reding 		.vcocap = 0x3,
133*459cc2c6SThierry Reding 		.ichpmp = 0xa,
134*459cc2c6SThierry Reding 		.loadadj = 0x3,
135*459cc2c6SThierry Reding 		.termadj = 0xb,
136*459cc2c6SThierry Reding 		.tx_pu = 0x66,
137*459cc2c6SThierry Reding 		.bg_vref = 0xe,
138*459cc2c6SThierry Reding 		.drive_current = { 0x35, 0x3e, 0x3e, 0x3e },
139*459cc2c6SThierry Reding 		.preemphasis = { 0x02, 0x3f, 0x3f, 0x3f },
140*459cc2c6SThierry Reding 	},
141*459cc2c6SThierry Reding };
142*459cc2c6SThierry Reding #endif
143*459cc2c6SThierry Reding 
144*459cc2c6SThierry Reding struct tegra_sor_soc {
145*459cc2c6SThierry Reding 	bool supports_edp;
146*459cc2c6SThierry Reding 	bool supports_lvds;
147*459cc2c6SThierry Reding 	bool supports_hdmi;
148*459cc2c6SThierry Reding 	bool supports_dp;
149*459cc2c6SThierry Reding 
150*459cc2c6SThierry Reding 	const struct tegra_sor_hdmi_settings *settings;
151*459cc2c6SThierry Reding 	unsigned int num_settings;
152*459cc2c6SThierry Reding };
153*459cc2c6SThierry Reding 
154*459cc2c6SThierry Reding struct tegra_sor;
155*459cc2c6SThierry Reding 
156*459cc2c6SThierry Reding struct tegra_sor_ops {
157*459cc2c6SThierry Reding 	const char *name;
158*459cc2c6SThierry Reding 	int (*probe)(struct tegra_sor *sor);
159*459cc2c6SThierry Reding 	int (*remove)(struct tegra_sor *sor);
160*459cc2c6SThierry Reding };
161*459cc2c6SThierry 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 
167*459cc2c6SThierry 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 
1766b6b6042SThierry Reding 	struct tegra_dpaux *dpaux;
1776b6b6042SThierry Reding 
178dab16336SThierry Reding 	struct drm_info_list *debugfs_files;
179dab16336SThierry Reding 	struct drm_minor *minor;
180a82752e1SThierry Reding 	struct dentry *debugfs;
181*459cc2c6SThierry Reding 
182*459cc2c6SThierry Reding 	const struct tegra_sor_ops *ops;
183*459cc2c6SThierry Reding 
184*459cc2c6SThierry Reding 	/* for HDMI 2.0 */
185*459cc2c6SThierry Reding 	struct tegra_sor_hdmi_settings *settings;
186*459cc2c6SThierry Reding 	unsigned int num_settings;
187*459cc2c6SThierry Reding 
188*459cc2c6SThierry Reding 	struct regulator *avdd_io_supply;
189*459cc2c6SThierry Reding 	struct regulator *vdd_pll_supply;
190*459cc2c6SThierry 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 
2286b6b6042SThierry Reding static int tegra_sor_dp_train_fast(struct tegra_sor *sor,
2296b6b6042SThierry Reding 				   struct drm_dp_link *link)
2306b6b6042SThierry Reding {
2316b6b6042SThierry Reding 	unsigned int i;
2326b6b6042SThierry Reding 	u8 pattern;
23328fe2076SThierry Reding 	u32 value;
2346b6b6042SThierry Reding 	int err;
2356b6b6042SThierry Reding 
2366b6b6042SThierry Reding 	/* setup lane parameters */
2376b6b6042SThierry Reding 	value = SOR_LANE_DRIVE_CURRENT_LANE3(0x40) |
2386b6b6042SThierry Reding 		SOR_LANE_DRIVE_CURRENT_LANE2(0x40) |
2396b6b6042SThierry Reding 		SOR_LANE_DRIVE_CURRENT_LANE1(0x40) |
2406b6b6042SThierry Reding 		SOR_LANE_DRIVE_CURRENT_LANE0(0x40);
241a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT0);
2426b6b6042SThierry Reding 
2436b6b6042SThierry Reding 	value = SOR_LANE_PREEMPHASIS_LANE3(0x0f) |
2446b6b6042SThierry Reding 		SOR_LANE_PREEMPHASIS_LANE2(0x0f) |
2456b6b6042SThierry Reding 		SOR_LANE_PREEMPHASIS_LANE1(0x0f) |
2466b6b6042SThierry Reding 		SOR_LANE_PREEMPHASIS_LANE0(0x0f);
247a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS0);
2486b6b6042SThierry Reding 
249a9a9e4fdSThierry Reding 	value = SOR_LANE_POSTCURSOR_LANE3(0x00) |
250a9a9e4fdSThierry Reding 		SOR_LANE_POSTCURSOR_LANE2(0x00) |
251a9a9e4fdSThierry Reding 		SOR_LANE_POSTCURSOR_LANE1(0x00) |
252a9a9e4fdSThierry Reding 		SOR_LANE_POSTCURSOR_LANE0(0x00);
253a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_POSTCURSOR0);
2546b6b6042SThierry Reding 
2556b6b6042SThierry Reding 	/* disable LVDS mode */
2566b6b6042SThierry Reding 	tegra_sor_writel(sor, 0, SOR_LVDS);
2576b6b6042SThierry Reding 
258a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
2596b6b6042SThierry Reding 	value |= SOR_DP_PADCTL_TX_PU_ENABLE;
2606b6b6042SThierry Reding 	value &= ~SOR_DP_PADCTL_TX_PU_MASK;
2616b6b6042SThierry Reding 	value |= SOR_DP_PADCTL_TX_PU(2); /* XXX: don't hardcode? */
262a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
2636b6b6042SThierry Reding 
264a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
2656b6b6042SThierry Reding 	value |= SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
2666b6b6042SThierry Reding 		 SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0;
267a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
2686b6b6042SThierry Reding 
2696b6b6042SThierry Reding 	usleep_range(10, 100);
2706b6b6042SThierry Reding 
271a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
2726b6b6042SThierry Reding 	value &= ~(SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
2736b6b6042SThierry Reding 		   SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0);
274a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
2756b6b6042SThierry Reding 
2766b6b6042SThierry Reding 	err = tegra_dpaux_prepare(sor->dpaux, DP_SET_ANSI_8B10B);
2776b6b6042SThierry Reding 	if (err < 0)
2786b6b6042SThierry Reding 		return err;
2796b6b6042SThierry Reding 
2806b6b6042SThierry Reding 	for (i = 0, value = 0; i < link->num_lanes; i++) {
2816b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
2826b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_NONE |
2836b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_TRAIN1;
2846b6b6042SThierry Reding 		value = (value << 8) | lane;
2856b6b6042SThierry Reding 	}
2866b6b6042SThierry Reding 
2876b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
2886b6b6042SThierry Reding 
2896b6b6042SThierry Reding 	pattern = DP_TRAINING_PATTERN_1;
2906b6b6042SThierry Reding 
2916b6b6042SThierry Reding 	err = tegra_dpaux_train(sor->dpaux, link, pattern);
2926b6b6042SThierry Reding 	if (err < 0)
2936b6b6042SThierry Reding 		return err;
2946b6b6042SThierry Reding 
295a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_SPARE0);
2966b6b6042SThierry Reding 	value |= SOR_DP_SPARE_SEQ_ENABLE;
2976b6b6042SThierry Reding 	value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
2986b6b6042SThierry Reding 	value |= SOR_DP_SPARE_MACRO_SOR_CLK;
299a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_SPARE0);
3006b6b6042SThierry Reding 
3016b6b6042SThierry Reding 	for (i = 0, value = 0; i < link->num_lanes; i++) {
3026b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
3036b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_NONE |
3046b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_TRAIN2;
3056b6b6042SThierry Reding 		value = (value << 8) | lane;
3066b6b6042SThierry Reding 	}
3076b6b6042SThierry Reding 
3086b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
3096b6b6042SThierry Reding 
3106b6b6042SThierry Reding 	pattern = DP_LINK_SCRAMBLING_DISABLE | DP_TRAINING_PATTERN_2;
3116b6b6042SThierry Reding 
3126b6b6042SThierry Reding 	err = tegra_dpaux_train(sor->dpaux, link, pattern);
3136b6b6042SThierry Reding 	if (err < 0)
3146b6b6042SThierry Reding 		return err;
3156b6b6042SThierry Reding 
3166b6b6042SThierry Reding 	for (i = 0, value = 0; i < link->num_lanes; i++) {
3176b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
3186b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_GALIOS |
3196b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_NONE;
3206b6b6042SThierry Reding 		value = (value << 8) | lane;
3216b6b6042SThierry Reding 	}
3226b6b6042SThierry Reding 
3236b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
3246b6b6042SThierry Reding 
3256b6b6042SThierry Reding 	pattern = DP_TRAINING_PATTERN_DISABLE;
3266b6b6042SThierry Reding 
3276b6b6042SThierry Reding 	err = tegra_dpaux_train(sor->dpaux, link, pattern);
3286b6b6042SThierry Reding 	if (err < 0)
3296b6b6042SThierry Reding 		return err;
3306b6b6042SThierry Reding 
3316b6b6042SThierry Reding 	return 0;
3326b6b6042SThierry Reding }
3336b6b6042SThierry Reding 
334*459cc2c6SThierry Reding static void tegra_sor_dp_term_calibrate(struct tegra_sor *sor)
335*459cc2c6SThierry Reding {
336*459cc2c6SThierry Reding 	u32 mask = 0x08, adj = 0, value;
337*459cc2c6SThierry Reding 
338*459cc2c6SThierry Reding 	/* enable pad calibration logic */
339*459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
340*459cc2c6SThierry Reding 	value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
341*459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
342*459cc2c6SThierry Reding 
343*459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL1);
344*459cc2c6SThierry Reding 	value |= SOR_PLL1_TMDS_TERM;
345*459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL1);
346*459cc2c6SThierry Reding 
347*459cc2c6SThierry Reding 	while (mask) {
348*459cc2c6SThierry Reding 		adj |= mask;
349*459cc2c6SThierry Reding 
350*459cc2c6SThierry Reding 		value = tegra_sor_readl(sor, SOR_PLL1);
351*459cc2c6SThierry Reding 		value &= ~SOR_PLL1_TMDS_TERMADJ_MASK;
352*459cc2c6SThierry Reding 		value |= SOR_PLL1_TMDS_TERMADJ(adj);
353*459cc2c6SThierry Reding 		tegra_sor_writel(sor, value, SOR_PLL1);
354*459cc2c6SThierry Reding 
355*459cc2c6SThierry Reding 		usleep_range(100, 200);
356*459cc2c6SThierry Reding 
357*459cc2c6SThierry Reding 		value = tegra_sor_readl(sor, SOR_PLL1);
358*459cc2c6SThierry Reding 		if (value & SOR_PLL1_TERM_COMPOUT)
359*459cc2c6SThierry Reding 			adj &= ~mask;
360*459cc2c6SThierry Reding 
361*459cc2c6SThierry Reding 		mask >>= 1;
362*459cc2c6SThierry Reding 	}
363*459cc2c6SThierry Reding 
364*459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL1);
365*459cc2c6SThierry Reding 	value &= ~SOR_PLL1_TMDS_TERMADJ_MASK;
366*459cc2c6SThierry Reding 	value |= SOR_PLL1_TMDS_TERMADJ(adj);
367*459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL1);
368*459cc2c6SThierry Reding 
369*459cc2c6SThierry Reding 	/* disable pad calibration logic */
370*459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
371*459cc2c6SThierry Reding 	value |= SOR_DP_PADCTL_PAD_CAL_PD;
372*459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
373*459cc2c6SThierry Reding }
374*459cc2c6SThierry Reding 
3756b6b6042SThierry Reding static void tegra_sor_super_update(struct tegra_sor *sor)
3766b6b6042SThierry Reding {
377a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
378a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 1, SOR_SUPER_STATE0);
379a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
3806b6b6042SThierry Reding }
3816b6b6042SThierry Reding 
3826b6b6042SThierry Reding static void tegra_sor_update(struct tegra_sor *sor)
3836b6b6042SThierry Reding {
384a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE0);
385a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 1, SOR_STATE0);
386a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE0);
3876b6b6042SThierry Reding }
3886b6b6042SThierry Reding 
3896b6b6042SThierry Reding static int tegra_sor_setup_pwm(struct tegra_sor *sor, unsigned long timeout)
3906b6b6042SThierry Reding {
39128fe2076SThierry Reding 	u32 value;
3926b6b6042SThierry Reding 
3936b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWM_DIV);
3946b6b6042SThierry Reding 	value &= ~SOR_PWM_DIV_MASK;
3956b6b6042SThierry Reding 	value |= 0x400; /* period */
3966b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWM_DIV);
3976b6b6042SThierry Reding 
3986b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWM_CTL);
3996b6b6042SThierry Reding 	value &= ~SOR_PWM_CTL_DUTY_CYCLE_MASK;
4006b6b6042SThierry Reding 	value |= 0x400; /* duty cycle */
4016b6b6042SThierry Reding 	value &= ~SOR_PWM_CTL_CLK_SEL; /* clock source: PCLK */
4026b6b6042SThierry Reding 	value |= SOR_PWM_CTL_TRIGGER;
4036b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWM_CTL);
4046b6b6042SThierry Reding 
4056b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
4066b6b6042SThierry Reding 
4076b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
4086b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWM_CTL);
4096b6b6042SThierry Reding 		if ((value & SOR_PWM_CTL_TRIGGER) == 0)
4106b6b6042SThierry Reding 			return 0;
4116b6b6042SThierry Reding 
4126b6b6042SThierry Reding 		usleep_range(25, 100);
4136b6b6042SThierry Reding 	}
4146b6b6042SThierry Reding 
4156b6b6042SThierry Reding 	return -ETIMEDOUT;
4166b6b6042SThierry Reding }
4176b6b6042SThierry Reding 
4186b6b6042SThierry Reding static int tegra_sor_attach(struct tegra_sor *sor)
4196b6b6042SThierry Reding {
4206b6b6042SThierry Reding 	unsigned long value, timeout;
4216b6b6042SThierry Reding 
4226b6b6042SThierry Reding 	/* wake up in normal mode */
423a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
4246b6b6042SThierry Reding 	value |= SOR_SUPER_STATE_HEAD_MODE_AWAKE;
4256b6b6042SThierry Reding 	value |= SOR_SUPER_STATE_MODE_NORMAL;
426a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
4276b6b6042SThierry Reding 	tegra_sor_super_update(sor);
4286b6b6042SThierry Reding 
4296b6b6042SThierry Reding 	/* attach */
430a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
4316b6b6042SThierry Reding 	value |= SOR_SUPER_STATE_ATTACHED;
432a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
4336b6b6042SThierry Reding 	tegra_sor_super_update(sor);
4346b6b6042SThierry Reding 
4356b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
4366b6b6042SThierry Reding 
4376b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
4386b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_TEST);
4396b6b6042SThierry Reding 		if ((value & SOR_TEST_ATTACHED) != 0)
4406b6b6042SThierry Reding 			return 0;
4416b6b6042SThierry Reding 
4426b6b6042SThierry Reding 		usleep_range(25, 100);
4436b6b6042SThierry Reding 	}
4446b6b6042SThierry Reding 
4456b6b6042SThierry Reding 	return -ETIMEDOUT;
4466b6b6042SThierry Reding }
4476b6b6042SThierry Reding 
4486b6b6042SThierry Reding static int tegra_sor_wakeup(struct tegra_sor *sor)
4496b6b6042SThierry Reding {
4506b6b6042SThierry Reding 	unsigned long value, timeout;
4516b6b6042SThierry Reding 
4526b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
4536b6b6042SThierry Reding 
4546b6b6042SThierry Reding 	/* wait for head to wake up */
4556b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
4566b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_TEST);
4576b6b6042SThierry Reding 		value &= SOR_TEST_HEAD_MODE_MASK;
4586b6b6042SThierry Reding 
4596b6b6042SThierry Reding 		if (value == SOR_TEST_HEAD_MODE_AWAKE)
4606b6b6042SThierry Reding 			return 0;
4616b6b6042SThierry Reding 
4626b6b6042SThierry Reding 		usleep_range(25, 100);
4636b6b6042SThierry Reding 	}
4646b6b6042SThierry Reding 
4656b6b6042SThierry Reding 	return -ETIMEDOUT;
4666b6b6042SThierry Reding }
4676b6b6042SThierry Reding 
4686b6b6042SThierry Reding static int tegra_sor_power_up(struct tegra_sor *sor, unsigned long timeout)
4696b6b6042SThierry Reding {
47028fe2076SThierry Reding 	u32 value;
4716b6b6042SThierry Reding 
4726b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWR);
4736b6b6042SThierry Reding 	value |= SOR_PWR_TRIGGER | SOR_PWR_NORMAL_STATE_PU;
4746b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWR);
4756b6b6042SThierry Reding 
4766b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
4776b6b6042SThierry Reding 
4786b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
4796b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWR);
4806b6b6042SThierry Reding 		if ((value & SOR_PWR_TRIGGER) == 0)
4816b6b6042SThierry Reding 			return 0;
4826b6b6042SThierry Reding 
4836b6b6042SThierry Reding 		usleep_range(25, 100);
4846b6b6042SThierry Reding 	}
4856b6b6042SThierry Reding 
4866b6b6042SThierry Reding 	return -ETIMEDOUT;
4876b6b6042SThierry Reding }
4886b6b6042SThierry Reding 
48934fa183bSThierry Reding struct tegra_sor_params {
49034fa183bSThierry Reding 	/* number of link clocks per line */
49134fa183bSThierry Reding 	unsigned int num_clocks;
49234fa183bSThierry Reding 	/* ratio between input and output */
49334fa183bSThierry Reding 	u64 ratio;
49434fa183bSThierry Reding 	/* precision factor */
49534fa183bSThierry Reding 	u64 precision;
49634fa183bSThierry Reding 
49734fa183bSThierry Reding 	unsigned int active_polarity;
49834fa183bSThierry Reding 	unsigned int active_count;
49934fa183bSThierry Reding 	unsigned int active_frac;
50034fa183bSThierry Reding 	unsigned int tu_size;
50134fa183bSThierry Reding 	unsigned int error;
50234fa183bSThierry Reding };
50334fa183bSThierry Reding 
50434fa183bSThierry Reding static int tegra_sor_compute_params(struct tegra_sor *sor,
50534fa183bSThierry Reding 				    struct tegra_sor_params *params,
50634fa183bSThierry Reding 				    unsigned int tu_size)
50734fa183bSThierry Reding {
50834fa183bSThierry Reding 	u64 active_sym, active_count, frac, approx;
50934fa183bSThierry Reding 	u32 active_polarity, active_frac = 0;
51034fa183bSThierry Reding 	const u64 f = params->precision;
51134fa183bSThierry Reding 	s64 error;
51234fa183bSThierry Reding 
51334fa183bSThierry Reding 	active_sym = params->ratio * tu_size;
51434fa183bSThierry Reding 	active_count = div_u64(active_sym, f) * f;
51534fa183bSThierry Reding 	frac = active_sym - active_count;
51634fa183bSThierry Reding 
51734fa183bSThierry Reding 	/* fraction < 0.5 */
51834fa183bSThierry Reding 	if (frac >= (f / 2)) {
51934fa183bSThierry Reding 		active_polarity = 1;
52034fa183bSThierry Reding 		frac = f - frac;
52134fa183bSThierry Reding 	} else {
52234fa183bSThierry Reding 		active_polarity = 0;
52334fa183bSThierry Reding 	}
52434fa183bSThierry Reding 
52534fa183bSThierry Reding 	if (frac != 0) {
52634fa183bSThierry Reding 		frac = div_u64(f * f,  frac); /* 1/fraction */
52734fa183bSThierry Reding 		if (frac <= (15 * f)) {
52834fa183bSThierry Reding 			active_frac = div_u64(frac, f);
52934fa183bSThierry Reding 
53034fa183bSThierry Reding 			/* round up */
53134fa183bSThierry Reding 			if (active_polarity)
53234fa183bSThierry Reding 				active_frac++;
53334fa183bSThierry Reding 		} else {
53434fa183bSThierry Reding 			active_frac = active_polarity ? 1 : 15;
53534fa183bSThierry Reding 		}
53634fa183bSThierry Reding 	}
53734fa183bSThierry Reding 
53834fa183bSThierry Reding 	if (active_frac == 1)
53934fa183bSThierry Reding 		active_polarity = 0;
54034fa183bSThierry Reding 
54134fa183bSThierry Reding 	if (active_polarity == 1) {
54234fa183bSThierry Reding 		if (active_frac) {
54334fa183bSThierry Reding 			approx = active_count + (active_frac * (f - 1)) * f;
54434fa183bSThierry Reding 			approx = div_u64(approx, active_frac * f);
54534fa183bSThierry Reding 		} else {
54634fa183bSThierry Reding 			approx = active_count + f;
54734fa183bSThierry Reding 		}
54834fa183bSThierry Reding 	} else {
54934fa183bSThierry Reding 		if (active_frac)
55034fa183bSThierry Reding 			approx = active_count + div_u64(f, active_frac);
55134fa183bSThierry Reding 		else
55234fa183bSThierry Reding 			approx = active_count;
55334fa183bSThierry Reding 	}
55434fa183bSThierry Reding 
55534fa183bSThierry Reding 	error = div_s64(active_sym - approx, tu_size);
55634fa183bSThierry Reding 	error *= params->num_clocks;
55734fa183bSThierry Reding 
55834fa183bSThierry Reding 	if (error <= 0 && abs64(error) < params->error) {
55934fa183bSThierry Reding 		params->active_count = div_u64(active_count, f);
56034fa183bSThierry Reding 		params->active_polarity = active_polarity;
56134fa183bSThierry Reding 		params->active_frac = active_frac;
56234fa183bSThierry Reding 		params->error = abs64(error);
56334fa183bSThierry Reding 		params->tu_size = tu_size;
56434fa183bSThierry Reding 
56534fa183bSThierry Reding 		if (error == 0)
56634fa183bSThierry Reding 			return true;
56734fa183bSThierry Reding 	}
56834fa183bSThierry Reding 
56934fa183bSThierry Reding 	return false;
57034fa183bSThierry Reding }
57134fa183bSThierry Reding 
57234fa183bSThierry Reding static int tegra_sor_calc_config(struct tegra_sor *sor,
57380444495SThierry Reding 				 const struct drm_display_mode *mode,
57434fa183bSThierry Reding 				 struct tegra_sor_config *config,
57534fa183bSThierry Reding 				 struct drm_dp_link *link)
57634fa183bSThierry Reding {
57734fa183bSThierry Reding 	const u64 f = 100000, link_rate = link->rate * 1000;
57834fa183bSThierry Reding 	const u64 pclk = mode->clock * 1000;
5797890b576SThierry Reding 	u64 input, output, watermark, num;
58034fa183bSThierry Reding 	struct tegra_sor_params params;
58134fa183bSThierry Reding 	u32 num_syms_per_line;
58234fa183bSThierry Reding 	unsigned int i;
58334fa183bSThierry Reding 
58434fa183bSThierry Reding 	if (!link_rate || !link->num_lanes || !pclk || !config->bits_per_pixel)
58534fa183bSThierry Reding 		return -EINVAL;
58634fa183bSThierry Reding 
58734fa183bSThierry Reding 	output = link_rate * 8 * link->num_lanes;
58834fa183bSThierry Reding 	input = pclk * config->bits_per_pixel;
58934fa183bSThierry Reding 
59034fa183bSThierry Reding 	if (input >= output)
59134fa183bSThierry Reding 		return -ERANGE;
59234fa183bSThierry Reding 
59334fa183bSThierry Reding 	memset(&params, 0, sizeof(params));
59434fa183bSThierry Reding 	params.ratio = div64_u64(input * f, output);
59534fa183bSThierry Reding 	params.num_clocks = div_u64(link_rate * mode->hdisplay, pclk);
59634fa183bSThierry Reding 	params.precision = f;
59734fa183bSThierry Reding 	params.error = 64 * f;
59834fa183bSThierry Reding 	params.tu_size = 64;
59934fa183bSThierry Reding 
60034fa183bSThierry Reding 	for (i = params.tu_size; i >= 32; i--)
60134fa183bSThierry Reding 		if (tegra_sor_compute_params(sor, &params, i))
60234fa183bSThierry Reding 			break;
60334fa183bSThierry Reding 
60434fa183bSThierry Reding 	if (params.active_frac == 0) {
60534fa183bSThierry Reding 		config->active_polarity = 0;
60634fa183bSThierry Reding 		config->active_count = params.active_count;
60734fa183bSThierry Reding 
60834fa183bSThierry Reding 		if (!params.active_polarity)
60934fa183bSThierry Reding 			config->active_count--;
61034fa183bSThierry Reding 
61134fa183bSThierry Reding 		config->tu_size = params.tu_size;
61234fa183bSThierry Reding 		config->active_frac = 1;
61334fa183bSThierry Reding 	} else {
61434fa183bSThierry Reding 		config->active_polarity = params.active_polarity;
61534fa183bSThierry Reding 		config->active_count = params.active_count;
61634fa183bSThierry Reding 		config->active_frac = params.active_frac;
61734fa183bSThierry Reding 		config->tu_size = params.tu_size;
61834fa183bSThierry Reding 	}
61934fa183bSThierry Reding 
62034fa183bSThierry Reding 	dev_dbg(sor->dev,
62134fa183bSThierry Reding 		"polarity: %d active count: %d tu size: %d active frac: %d\n",
62234fa183bSThierry Reding 		config->active_polarity, config->active_count,
62334fa183bSThierry Reding 		config->tu_size, config->active_frac);
62434fa183bSThierry Reding 
62534fa183bSThierry Reding 	watermark = params.ratio * config->tu_size * (f - params.ratio);
62634fa183bSThierry Reding 	watermark = div_u64(watermark, f);
62734fa183bSThierry Reding 
62834fa183bSThierry Reding 	watermark = div_u64(watermark + params.error, f);
62934fa183bSThierry Reding 	config->watermark = watermark + (config->bits_per_pixel / 8) + 2;
63034fa183bSThierry Reding 	num_syms_per_line = (mode->hdisplay * config->bits_per_pixel) *
63134fa183bSThierry Reding 			    (link->num_lanes * 8);
63234fa183bSThierry Reding 
63334fa183bSThierry Reding 	if (config->watermark > 30) {
63434fa183bSThierry Reding 		config->watermark = 30;
63534fa183bSThierry Reding 		dev_err(sor->dev,
63634fa183bSThierry Reding 			"unable to compute TU size, forcing watermark to %u\n",
63734fa183bSThierry Reding 			config->watermark);
63834fa183bSThierry Reding 	} else if (config->watermark > num_syms_per_line) {
63934fa183bSThierry Reding 		config->watermark = num_syms_per_line;
64034fa183bSThierry Reding 		dev_err(sor->dev, "watermark too high, forcing to %u\n",
64134fa183bSThierry Reding 			config->watermark);
64234fa183bSThierry Reding 	}
64334fa183bSThierry Reding 
6447890b576SThierry Reding 	/* compute the number of symbols per horizontal blanking interval */
6457890b576SThierry Reding 	num = ((mode->htotal - mode->hdisplay) - 7) * link_rate;
6467890b576SThierry Reding 	config->hblank_symbols = div_u64(num, pclk);
6477890b576SThierry Reding 
6487890b576SThierry Reding 	if (link->capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
6497890b576SThierry Reding 		config->hblank_symbols -= 3;
6507890b576SThierry Reding 
6517890b576SThierry Reding 	config->hblank_symbols -= 12 / link->num_lanes;
6527890b576SThierry Reding 
6537890b576SThierry Reding 	/* compute the number of symbols per vertical blanking interval */
6547890b576SThierry Reding 	num = (mode->hdisplay - 25) * link_rate;
6557890b576SThierry Reding 	config->vblank_symbols = div_u64(num, pclk);
6567890b576SThierry Reding 	config->vblank_symbols -= 36 / link->num_lanes + 4;
6577890b576SThierry Reding 
6587890b576SThierry Reding 	dev_dbg(sor->dev, "blank symbols: H:%u V:%u\n", config->hblank_symbols,
6597890b576SThierry Reding 		config->vblank_symbols);
6607890b576SThierry Reding 
66134fa183bSThierry Reding 	return 0;
66234fa183bSThierry Reding }
66334fa183bSThierry Reding 
6646fad8f66SThierry Reding static int tegra_sor_detach(struct tegra_sor *sor)
6656b6b6042SThierry Reding {
6666fad8f66SThierry Reding 	unsigned long value, timeout;
6676fad8f66SThierry Reding 
6686fad8f66SThierry Reding 	/* switch to safe mode */
669a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
6706fad8f66SThierry Reding 	value &= ~SOR_SUPER_STATE_MODE_NORMAL;
671a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
6726fad8f66SThierry Reding 	tegra_sor_super_update(sor);
6736fad8f66SThierry Reding 
6746fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
6756fad8f66SThierry Reding 
6766fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
6776fad8f66SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWR);
6786fad8f66SThierry Reding 		if (value & SOR_PWR_MODE_SAFE)
6796fad8f66SThierry Reding 			break;
6806fad8f66SThierry Reding 	}
6816fad8f66SThierry Reding 
6826fad8f66SThierry Reding 	if ((value & SOR_PWR_MODE_SAFE) == 0)
6836fad8f66SThierry Reding 		return -ETIMEDOUT;
6846fad8f66SThierry Reding 
6856fad8f66SThierry Reding 	/* go to sleep */
686a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
6876fad8f66SThierry Reding 	value &= ~SOR_SUPER_STATE_HEAD_MODE_MASK;
688a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
6896fad8f66SThierry Reding 	tegra_sor_super_update(sor);
6906fad8f66SThierry Reding 
6916fad8f66SThierry Reding 	/* detach */
692a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
6936fad8f66SThierry Reding 	value &= ~SOR_SUPER_STATE_ATTACHED;
694a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
6956fad8f66SThierry Reding 	tegra_sor_super_update(sor);
6966fad8f66SThierry Reding 
6976fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
6986fad8f66SThierry Reding 
6996fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
7006fad8f66SThierry Reding 		value = tegra_sor_readl(sor, SOR_TEST);
7016fad8f66SThierry Reding 		if ((value & SOR_TEST_ATTACHED) == 0)
7026fad8f66SThierry Reding 			break;
7036fad8f66SThierry Reding 
7046fad8f66SThierry Reding 		usleep_range(25, 100);
7056fad8f66SThierry Reding 	}
7066fad8f66SThierry Reding 
7076fad8f66SThierry Reding 	if ((value & SOR_TEST_ATTACHED) != 0)
7086fad8f66SThierry Reding 		return -ETIMEDOUT;
7096fad8f66SThierry Reding 
7106fad8f66SThierry Reding 	return 0;
7116fad8f66SThierry Reding }
7126fad8f66SThierry Reding 
7136fad8f66SThierry Reding static int tegra_sor_power_down(struct tegra_sor *sor)
7146fad8f66SThierry Reding {
7156fad8f66SThierry Reding 	unsigned long value, timeout;
7166fad8f66SThierry Reding 	int err;
7176fad8f66SThierry Reding 
7186fad8f66SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWR);
7196fad8f66SThierry Reding 	value &= ~SOR_PWR_NORMAL_STATE_PU;
7206fad8f66SThierry Reding 	value |= SOR_PWR_TRIGGER;
7216fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWR);
7226fad8f66SThierry Reding 
7236fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
7246fad8f66SThierry Reding 
7256fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
7266fad8f66SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWR);
7276fad8f66SThierry Reding 		if ((value & SOR_PWR_TRIGGER) == 0)
7286fad8f66SThierry Reding 			return 0;
7296fad8f66SThierry Reding 
7306fad8f66SThierry Reding 		usleep_range(25, 100);
7316fad8f66SThierry Reding 	}
7326fad8f66SThierry Reding 
7336fad8f66SThierry Reding 	if ((value & SOR_PWR_TRIGGER) != 0)
7346fad8f66SThierry Reding 		return -ETIMEDOUT;
7356fad8f66SThierry Reding 
7366fad8f66SThierry Reding 	err = clk_set_parent(sor->clk, sor->clk_safe);
7376fad8f66SThierry Reding 	if (err < 0)
7386fad8f66SThierry Reding 		dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
7396fad8f66SThierry Reding 
740a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
7416fad8f66SThierry Reding 	value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
7426fad8f66SThierry Reding 		   SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2);
743a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
7446fad8f66SThierry Reding 
7456fad8f66SThierry Reding 	/* stop lane sequencer */
7466fad8f66SThierry Reding 	value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_UP |
7476fad8f66SThierry Reding 		SOR_LANE_SEQ_CTL_POWER_STATE_DOWN;
7486fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
7496fad8f66SThierry Reding 
7506fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
7516fad8f66SThierry Reding 
7526fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
7536fad8f66SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
7546fad8f66SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
7556fad8f66SThierry Reding 			break;
7566fad8f66SThierry Reding 
7576fad8f66SThierry Reding 		usleep_range(25, 100);
7586fad8f66SThierry Reding 	}
7596fad8f66SThierry Reding 
7606fad8f66SThierry Reding 	if ((value & SOR_LANE_SEQ_CTL_TRIGGER) != 0)
7616fad8f66SThierry Reding 		return -ETIMEDOUT;
7626fad8f66SThierry Reding 
763a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
764a9a9e4fdSThierry Reding 	value |= SOR_PLL2_PORT_POWERDOWN;
765a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
7666fad8f66SThierry Reding 
7676fad8f66SThierry Reding 	usleep_range(20, 100);
7686fad8f66SThierry Reding 
769a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL0);
770a9a9e4fdSThierry Reding 	value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
771a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL0);
7726fad8f66SThierry Reding 
773a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
774a9a9e4fdSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD;
775a9a9e4fdSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
776a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
7776fad8f66SThierry Reding 
7786fad8f66SThierry Reding 	usleep_range(20, 100);
7796fad8f66SThierry Reding 
7806fad8f66SThierry Reding 	return 0;
7816fad8f66SThierry Reding }
7826fad8f66SThierry Reding 
7836fad8f66SThierry Reding static int tegra_sor_crc_wait(struct tegra_sor *sor, unsigned long timeout)
7846fad8f66SThierry Reding {
7856fad8f66SThierry Reding 	u32 value;
7866fad8f66SThierry Reding 
7876fad8f66SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
7886fad8f66SThierry Reding 
7896fad8f66SThierry Reding 	while (time_before(jiffies, timeout)) {
790a9a9e4fdSThierry Reding 		value = tegra_sor_readl(sor, SOR_CRCA);
791a9a9e4fdSThierry Reding 		if (value & SOR_CRCA_VALID)
7926fad8f66SThierry Reding 			return 0;
7936fad8f66SThierry Reding 
7946fad8f66SThierry Reding 		usleep_range(100, 200);
7956fad8f66SThierry Reding 	}
7966fad8f66SThierry Reding 
7976fad8f66SThierry Reding 	return -ETIMEDOUT;
7986fad8f66SThierry Reding }
7996fad8f66SThierry Reding 
800530239a8SThierry Reding static int tegra_sor_show_crc(struct seq_file *s, void *data)
8016fad8f66SThierry Reding {
802530239a8SThierry Reding 	struct drm_info_node *node = s->private;
803530239a8SThierry Reding 	struct tegra_sor *sor = node->info_ent->data;
804850bab44SThierry Reding 	struct drm_crtc *crtc = sor->output.encoder.crtc;
805850bab44SThierry Reding 	struct drm_device *drm = node->minor->dev;
806530239a8SThierry Reding 	int err = 0;
8076fad8f66SThierry Reding 	u32 value;
8086fad8f66SThierry Reding 
809850bab44SThierry Reding 	drm_modeset_lock_all(drm);
8106fad8f66SThierry Reding 
811850bab44SThierry Reding 	if (!crtc || !crtc->state->active) {
812850bab44SThierry Reding 		err = -EBUSY;
8136fad8f66SThierry Reding 		goto unlock;
8146fad8f66SThierry Reding 	}
8156fad8f66SThierry Reding 
816a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
8176fad8f66SThierry Reding 	value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
818a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
8196fad8f66SThierry Reding 
8206fad8f66SThierry Reding 	value = tegra_sor_readl(sor, SOR_CRC_CNTRL);
8216fad8f66SThierry Reding 	value |= SOR_CRC_CNTRL_ENABLE;
8226fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_CRC_CNTRL);
8236fad8f66SThierry Reding 
8246fad8f66SThierry Reding 	value = tegra_sor_readl(sor, SOR_TEST);
8256fad8f66SThierry Reding 	value &= ~SOR_TEST_CRC_POST_SERIALIZE;
8266fad8f66SThierry Reding 	tegra_sor_writel(sor, value, SOR_TEST);
8276fad8f66SThierry Reding 
8286fad8f66SThierry Reding 	err = tegra_sor_crc_wait(sor, 100);
8296fad8f66SThierry Reding 	if (err < 0)
8306fad8f66SThierry Reding 		goto unlock;
8316fad8f66SThierry Reding 
832a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, SOR_CRCA_RESET, SOR_CRCA);
833a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_CRCB);
8346fad8f66SThierry Reding 
835530239a8SThierry Reding 	seq_printf(s, "%08x\n", value);
8366fad8f66SThierry Reding 
8376fad8f66SThierry Reding unlock:
838850bab44SThierry Reding 	drm_modeset_unlock_all(drm);
8396fad8f66SThierry Reding 	return err;
8406fad8f66SThierry Reding }
8416fad8f66SThierry Reding 
842dab16336SThierry Reding static int tegra_sor_show_regs(struct seq_file *s, void *data)
843dab16336SThierry Reding {
844dab16336SThierry Reding 	struct drm_info_node *node = s->private;
845dab16336SThierry Reding 	struct tegra_sor *sor = node->info_ent->data;
846850bab44SThierry Reding 	struct drm_crtc *crtc = sor->output.encoder.crtc;
847850bab44SThierry Reding 	struct drm_device *drm = node->minor->dev;
848850bab44SThierry Reding 	int err = 0;
849850bab44SThierry Reding 
850850bab44SThierry Reding 	drm_modeset_lock_all(drm);
851850bab44SThierry Reding 
852850bab44SThierry Reding 	if (!crtc || !crtc->state->active) {
853850bab44SThierry Reding 		err = -EBUSY;
854850bab44SThierry Reding 		goto unlock;
855850bab44SThierry Reding 	}
856dab16336SThierry Reding 
857dab16336SThierry Reding #define DUMP_REG(name)						\
858dab16336SThierry Reding 	seq_printf(s, "%-38s %#05x %08x\n", #name, name,	\
859dab16336SThierry Reding 		   tegra_sor_readl(sor, name))
860dab16336SThierry Reding 
861dab16336SThierry Reding 	DUMP_REG(SOR_CTXSW);
862a9a9e4fdSThierry Reding 	DUMP_REG(SOR_SUPER_STATE0);
863a9a9e4fdSThierry Reding 	DUMP_REG(SOR_SUPER_STATE1);
864a9a9e4fdSThierry Reding 	DUMP_REG(SOR_STATE0);
865a9a9e4fdSThierry Reding 	DUMP_REG(SOR_STATE1);
866a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE0(0));
867a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE0(1));
868a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE1(0));
869a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE1(1));
870a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE2(0));
871a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE2(1));
872a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE3(0));
873a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE3(1));
874a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE4(0));
875a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE4(1));
876a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE5(0));
877a9a9e4fdSThierry Reding 	DUMP_REG(SOR_HEAD_STATE5(1));
878dab16336SThierry Reding 	DUMP_REG(SOR_CRC_CNTRL);
879dab16336SThierry Reding 	DUMP_REG(SOR_DP_DEBUG_MVID);
880dab16336SThierry Reding 	DUMP_REG(SOR_CLK_CNTRL);
881dab16336SThierry Reding 	DUMP_REG(SOR_CAP);
882dab16336SThierry Reding 	DUMP_REG(SOR_PWR);
883dab16336SThierry Reding 	DUMP_REG(SOR_TEST);
884a9a9e4fdSThierry Reding 	DUMP_REG(SOR_PLL0);
885a9a9e4fdSThierry Reding 	DUMP_REG(SOR_PLL1);
886a9a9e4fdSThierry Reding 	DUMP_REG(SOR_PLL2);
887a9a9e4fdSThierry Reding 	DUMP_REG(SOR_PLL3);
888dab16336SThierry Reding 	DUMP_REG(SOR_CSTM);
889dab16336SThierry Reding 	DUMP_REG(SOR_LVDS);
890a9a9e4fdSThierry Reding 	DUMP_REG(SOR_CRCA);
891a9a9e4fdSThierry Reding 	DUMP_REG(SOR_CRCB);
892dab16336SThierry Reding 	DUMP_REG(SOR_BLANK);
893dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_CTL);
894dab16336SThierry Reding 	DUMP_REG(SOR_LANE_SEQ_CTL);
895dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(0));
896dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(1));
897dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(2));
898dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(3));
899dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(4));
900dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(5));
901dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(6));
902dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(7));
903dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(8));
904dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(9));
905dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(10));
906dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(11));
907dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(12));
908dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(13));
909dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(14));
910dab16336SThierry Reding 	DUMP_REG(SOR_SEQ_INST(15));
911dab16336SThierry Reding 	DUMP_REG(SOR_PWM_DIV);
912dab16336SThierry Reding 	DUMP_REG(SOR_PWM_CTL);
913a9a9e4fdSThierry Reding 	DUMP_REG(SOR_VCRC_A0);
914a9a9e4fdSThierry Reding 	DUMP_REG(SOR_VCRC_A1);
915a9a9e4fdSThierry Reding 	DUMP_REG(SOR_VCRC_B0);
916a9a9e4fdSThierry Reding 	DUMP_REG(SOR_VCRC_B1);
917a9a9e4fdSThierry Reding 	DUMP_REG(SOR_CCRC_A0);
918a9a9e4fdSThierry Reding 	DUMP_REG(SOR_CCRC_A1);
919a9a9e4fdSThierry Reding 	DUMP_REG(SOR_CCRC_B0);
920a9a9e4fdSThierry Reding 	DUMP_REG(SOR_CCRC_B1);
921a9a9e4fdSThierry Reding 	DUMP_REG(SOR_EDATA_A0);
922a9a9e4fdSThierry Reding 	DUMP_REG(SOR_EDATA_A1);
923a9a9e4fdSThierry Reding 	DUMP_REG(SOR_EDATA_B0);
924a9a9e4fdSThierry Reding 	DUMP_REG(SOR_EDATA_B1);
925a9a9e4fdSThierry Reding 	DUMP_REG(SOR_COUNT_A0);
926a9a9e4fdSThierry Reding 	DUMP_REG(SOR_COUNT_A1);
927a9a9e4fdSThierry Reding 	DUMP_REG(SOR_COUNT_B0);
928a9a9e4fdSThierry Reding 	DUMP_REG(SOR_COUNT_B1);
929a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DEBUG_A0);
930a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DEBUG_A1);
931a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DEBUG_B0);
932a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DEBUG_B1);
933dab16336SThierry Reding 	DUMP_REG(SOR_TRIG);
934dab16336SThierry Reding 	DUMP_REG(SOR_MSCHECK);
935dab16336SThierry Reding 	DUMP_REG(SOR_XBAR_CTRL);
936dab16336SThierry Reding 	DUMP_REG(SOR_XBAR_POL);
937a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_LINKCTL0);
938a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_LINKCTL1);
939a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE_DRIVE_CURRENT0);
940a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE_DRIVE_CURRENT1);
941a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE4_DRIVE_CURRENT0);
942a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE4_DRIVE_CURRENT1);
943a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE_PREEMPHASIS0);
944a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE_PREEMPHASIS1);
945a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE4_PREEMPHASIS0);
946a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE4_PREEMPHASIS1);
947a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE_POSTCURSOR0);
948a9a9e4fdSThierry Reding 	DUMP_REG(SOR_LANE_POSTCURSOR1);
949a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_CONFIG0);
950a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_CONFIG1);
951a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_MN0);
952a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_MN1);
953a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_PADCTL0);
954a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_PADCTL1);
955a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_DEBUG0);
956a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_DEBUG1);
957a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_SPARE0);
958a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_SPARE1);
959dab16336SThierry Reding 	DUMP_REG(SOR_DP_AUDIO_CTRL);
960dab16336SThierry Reding 	DUMP_REG(SOR_DP_AUDIO_HBLANK_SYMBOLS);
961dab16336SThierry Reding 	DUMP_REG(SOR_DP_AUDIO_VBLANK_SYMBOLS);
962dab16336SThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_HEADER);
963a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK0);
964a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK1);
965a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK2);
966a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK3);
967a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK4);
968a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK5);
969a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK6);
970dab16336SThierry Reding 	DUMP_REG(SOR_DP_TPG);
971dab16336SThierry Reding 	DUMP_REG(SOR_DP_TPG_CONFIG);
972a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_LQ_CSTM0);
973a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_LQ_CSTM1);
974a9a9e4fdSThierry Reding 	DUMP_REG(SOR_DP_LQ_CSTM2);
975dab16336SThierry Reding 
976dab16336SThierry Reding #undef DUMP_REG
977dab16336SThierry Reding 
978850bab44SThierry Reding unlock:
979850bab44SThierry Reding 	drm_modeset_unlock_all(drm);
980850bab44SThierry Reding 	return err;
981dab16336SThierry Reding }
982dab16336SThierry Reding 
983dab16336SThierry Reding static const struct drm_info_list debugfs_files[] = {
984530239a8SThierry Reding 	{ "crc", tegra_sor_show_crc, 0, NULL },
985dab16336SThierry Reding 	{ "regs", tegra_sor_show_regs, 0, NULL },
986dab16336SThierry Reding };
987dab16336SThierry Reding 
9886fad8f66SThierry Reding static int tegra_sor_debugfs_init(struct tegra_sor *sor,
9896fad8f66SThierry Reding 				  struct drm_minor *minor)
9906fad8f66SThierry Reding {
991*459cc2c6SThierry Reding 	const char *name = sor->soc->supports_dp ? "sor1" : "sor";
992dab16336SThierry Reding 	unsigned int i;
993530239a8SThierry Reding 	int err;
9946fad8f66SThierry Reding 
995*459cc2c6SThierry Reding 	sor->debugfs = debugfs_create_dir(name, minor->debugfs_root);
9966fad8f66SThierry Reding 	if (!sor->debugfs)
9976fad8f66SThierry Reding 		return -ENOMEM;
9986fad8f66SThierry Reding 
999dab16336SThierry Reding 	sor->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1000dab16336SThierry Reding 				     GFP_KERNEL);
1001dab16336SThierry Reding 	if (!sor->debugfs_files) {
10026fad8f66SThierry Reding 		err = -ENOMEM;
10036fad8f66SThierry Reding 		goto remove;
10046fad8f66SThierry Reding 	}
10056fad8f66SThierry Reding 
1006dab16336SThierry Reding 	for (i = 0; i < ARRAY_SIZE(debugfs_files); i++)
1007dab16336SThierry Reding 		sor->debugfs_files[i].data = sor;
1008dab16336SThierry Reding 
1009dab16336SThierry Reding 	err = drm_debugfs_create_files(sor->debugfs_files,
1010dab16336SThierry Reding 				       ARRAY_SIZE(debugfs_files),
1011dab16336SThierry Reding 				       sor->debugfs, minor);
1012dab16336SThierry Reding 	if (err < 0)
1013dab16336SThierry Reding 		goto free;
1014dab16336SThierry Reding 
10153ff1f22cSThierry Reding 	sor->minor = minor;
10163ff1f22cSThierry Reding 
1017530239a8SThierry Reding 	return 0;
10186fad8f66SThierry Reding 
1019dab16336SThierry Reding free:
1020dab16336SThierry Reding 	kfree(sor->debugfs_files);
1021dab16336SThierry Reding 	sor->debugfs_files = NULL;
10226fad8f66SThierry Reding remove:
1023dab16336SThierry Reding 	debugfs_remove_recursive(sor->debugfs);
10246fad8f66SThierry Reding 	sor->debugfs = NULL;
10256fad8f66SThierry Reding 	return err;
10266fad8f66SThierry Reding }
10276fad8f66SThierry Reding 
10284009c224SThierry Reding static void tegra_sor_debugfs_exit(struct tegra_sor *sor)
10296fad8f66SThierry Reding {
1030dab16336SThierry Reding 	drm_debugfs_remove_files(sor->debugfs_files, ARRAY_SIZE(debugfs_files),
1031dab16336SThierry Reding 				 sor->minor);
1032dab16336SThierry Reding 	sor->minor = NULL;
1033dab16336SThierry Reding 
1034dab16336SThierry Reding 	kfree(sor->debugfs_files);
1035066d30f8SThierry Reding 	sor->debugfs_files = NULL;
1036dab16336SThierry Reding 
1037dab16336SThierry Reding 	debugfs_remove_recursive(sor->debugfs);
1038066d30f8SThierry Reding 	sor->debugfs = NULL;
10396fad8f66SThierry Reding }
10406fad8f66SThierry Reding 
10416fad8f66SThierry Reding static enum drm_connector_status
10426fad8f66SThierry Reding tegra_sor_connector_detect(struct drm_connector *connector, bool force)
10436fad8f66SThierry Reding {
10446fad8f66SThierry Reding 	struct tegra_output *output = connector_to_output(connector);
10456fad8f66SThierry Reding 	struct tegra_sor *sor = to_sor(output);
10466fad8f66SThierry Reding 
10476fad8f66SThierry Reding 	if (sor->dpaux)
10486fad8f66SThierry Reding 		return tegra_dpaux_detect(sor->dpaux);
10496fad8f66SThierry Reding 
1050*459cc2c6SThierry Reding 	return tegra_output_connector_detect(connector, force);
10516fad8f66SThierry Reding }
10526fad8f66SThierry Reding 
10536fad8f66SThierry Reding static const struct drm_connector_funcs tegra_sor_connector_funcs = {
1054850bab44SThierry Reding 	.dpms = drm_atomic_helper_connector_dpms,
10559d44189fSThierry Reding 	.reset = drm_atomic_helper_connector_reset,
10566fad8f66SThierry Reding 	.detect = tegra_sor_connector_detect,
10576fad8f66SThierry Reding 	.fill_modes = drm_helper_probe_single_connector_modes,
10586fad8f66SThierry Reding 	.destroy = tegra_output_connector_destroy,
10599d44189fSThierry Reding 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
10604aa3df71SThierry Reding 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
10616fad8f66SThierry Reding };
10626fad8f66SThierry Reding 
10636fad8f66SThierry Reding static int tegra_sor_connector_get_modes(struct drm_connector *connector)
10646fad8f66SThierry Reding {
10656fad8f66SThierry Reding 	struct tegra_output *output = connector_to_output(connector);
10666fad8f66SThierry Reding 	struct tegra_sor *sor = to_sor(output);
10676fad8f66SThierry Reding 	int err;
10686fad8f66SThierry Reding 
10696fad8f66SThierry Reding 	if (sor->dpaux)
10706fad8f66SThierry Reding 		tegra_dpaux_enable(sor->dpaux);
10716fad8f66SThierry Reding 
10726fad8f66SThierry Reding 	err = tegra_output_connector_get_modes(connector);
10736fad8f66SThierry Reding 
10746fad8f66SThierry Reding 	if (sor->dpaux)
10756fad8f66SThierry Reding 		tegra_dpaux_disable(sor->dpaux);
10766fad8f66SThierry Reding 
10776fad8f66SThierry Reding 	return err;
10786fad8f66SThierry Reding }
10796fad8f66SThierry Reding 
10806fad8f66SThierry Reding static enum drm_mode_status
10816fad8f66SThierry Reding tegra_sor_connector_mode_valid(struct drm_connector *connector,
10826fad8f66SThierry Reding 			       struct drm_display_mode *mode)
10836fad8f66SThierry Reding {
10846fad8f66SThierry Reding 	return MODE_OK;
10856fad8f66SThierry Reding }
10866fad8f66SThierry Reding 
10876fad8f66SThierry Reding static const struct drm_connector_helper_funcs tegra_sor_connector_helper_funcs = {
10886fad8f66SThierry Reding 	.get_modes = tegra_sor_connector_get_modes,
10896fad8f66SThierry Reding 	.mode_valid = tegra_sor_connector_mode_valid,
10906fad8f66SThierry Reding 	.best_encoder = tegra_output_connector_best_encoder,
10916fad8f66SThierry Reding };
10926fad8f66SThierry Reding 
10936fad8f66SThierry Reding static const struct drm_encoder_funcs tegra_sor_encoder_funcs = {
10946fad8f66SThierry Reding 	.destroy = tegra_output_encoder_destroy,
10956fad8f66SThierry Reding };
10966fad8f66SThierry Reding 
1097850bab44SThierry Reding static void tegra_sor_edp_disable(struct drm_encoder *encoder)
10986fad8f66SThierry Reding {
1099850bab44SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
1100850bab44SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
1101850bab44SThierry Reding 	struct tegra_sor *sor = to_sor(output);
1102850bab44SThierry Reding 	u32 value;
1103850bab44SThierry Reding 	int err;
1104850bab44SThierry Reding 
1105850bab44SThierry Reding 	if (output->panel)
1106850bab44SThierry Reding 		drm_panel_disable(output->panel);
1107850bab44SThierry Reding 
1108850bab44SThierry Reding 	err = tegra_sor_detach(sor);
1109850bab44SThierry Reding 	if (err < 0)
1110850bab44SThierry Reding 		dev_err(sor->dev, "failed to detach SOR: %d\n", err);
1111850bab44SThierry Reding 
1112850bab44SThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE1);
1113850bab44SThierry Reding 	tegra_sor_update(sor);
1114850bab44SThierry Reding 
1115850bab44SThierry Reding 	/*
1116850bab44SThierry Reding 	 * The following accesses registers of the display controller, so make
1117850bab44SThierry Reding 	 * sure it's only executed when the output is attached to one.
1118850bab44SThierry Reding 	 */
1119850bab44SThierry Reding 	if (dc) {
1120850bab44SThierry Reding 		value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
1121850bab44SThierry Reding 		value &= ~SOR_ENABLE;
1122850bab44SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
1123850bab44SThierry Reding 
1124850bab44SThierry Reding 		tegra_dc_commit(dc);
11256fad8f66SThierry Reding 	}
11266fad8f66SThierry Reding 
1127850bab44SThierry Reding 	err = tegra_sor_power_down(sor);
1128850bab44SThierry Reding 	if (err < 0)
1129850bab44SThierry Reding 		dev_err(sor->dev, "failed to power down SOR: %d\n", err);
1130850bab44SThierry Reding 
1131850bab44SThierry Reding 	if (sor->dpaux) {
1132850bab44SThierry Reding 		err = tegra_dpaux_disable(sor->dpaux);
1133850bab44SThierry Reding 		if (err < 0)
1134850bab44SThierry Reding 			dev_err(sor->dev, "failed to disable DP: %d\n", err);
11356fad8f66SThierry Reding 	}
11366fad8f66SThierry Reding 
1137850bab44SThierry Reding 	err = tegra_io_rail_power_off(TEGRA_IO_RAIL_LVDS);
1138850bab44SThierry Reding 	if (err < 0)
1139850bab44SThierry Reding 		dev_err(sor->dev, "failed to power off I/O rail: %d\n", err);
1140850bab44SThierry Reding 
1141850bab44SThierry Reding 	if (output->panel)
1142850bab44SThierry Reding 		drm_panel_unprepare(output->panel);
1143850bab44SThierry Reding 
1144850bab44SThierry Reding 	reset_control_assert(sor->rst);
1145850bab44SThierry Reding 	clk_disable_unprepare(sor->clk);
11466fad8f66SThierry Reding }
11476fad8f66SThierry Reding 
1148*459cc2c6SThierry Reding #if 0
1149*459cc2c6SThierry Reding static int calc_h_ref_to_sync(const struct drm_display_mode *mode,
1150*459cc2c6SThierry Reding 			      unsigned int *value)
1151*459cc2c6SThierry Reding {
1152*459cc2c6SThierry Reding 	unsigned int hfp, hsw, hbp, a = 0, b;
1153*459cc2c6SThierry Reding 
1154*459cc2c6SThierry Reding 	hfp = mode->hsync_start - mode->hdisplay;
1155*459cc2c6SThierry Reding 	hsw = mode->hsync_end - mode->hsync_start;
1156*459cc2c6SThierry Reding 	hbp = mode->htotal - mode->hsync_end;
1157*459cc2c6SThierry Reding 
1158*459cc2c6SThierry Reding 	pr_info("hfp: %u, hsw: %u, hbp: %u\n", hfp, hsw, hbp);
1159*459cc2c6SThierry Reding 
1160*459cc2c6SThierry Reding 	b = hfp - 1;
1161*459cc2c6SThierry Reding 
1162*459cc2c6SThierry Reding 	pr_info("a: %u, b: %u\n", a, b);
1163*459cc2c6SThierry Reding 	pr_info("a + hsw + hbp = %u\n", a + hsw + hbp);
1164*459cc2c6SThierry Reding 
1165*459cc2c6SThierry Reding 	if (a + hsw + hbp <= 11) {
1166*459cc2c6SThierry Reding 		a = 1 + 11 - hsw - hbp;
1167*459cc2c6SThierry Reding 		pr_info("a: %u\n", a);
1168*459cc2c6SThierry Reding 	}
1169*459cc2c6SThierry Reding 
1170*459cc2c6SThierry Reding 	if (a > b)
1171*459cc2c6SThierry Reding 		return -EINVAL;
1172*459cc2c6SThierry Reding 
1173*459cc2c6SThierry Reding 	if (hsw < 1)
1174*459cc2c6SThierry Reding 		return -EINVAL;
1175*459cc2c6SThierry Reding 
1176*459cc2c6SThierry Reding 	if (mode->hdisplay < 16)
1177*459cc2c6SThierry Reding 		return -EINVAL;
1178*459cc2c6SThierry Reding 
1179*459cc2c6SThierry Reding 	if (value) {
1180*459cc2c6SThierry Reding 		if (b > a && a % 2)
1181*459cc2c6SThierry Reding 			*value = a + 1;
1182*459cc2c6SThierry Reding 		else
1183*459cc2c6SThierry Reding 			*value = a;
1184*459cc2c6SThierry Reding 	}
1185*459cc2c6SThierry Reding 
1186*459cc2c6SThierry Reding 	return 0;
1187*459cc2c6SThierry Reding }
1188*459cc2c6SThierry Reding #endif
1189*459cc2c6SThierry Reding 
1190850bab44SThierry Reding static void tegra_sor_edp_enable(struct drm_encoder *encoder)
11916fad8f66SThierry Reding {
1192850bab44SThierry Reding 	struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
11936fad8f66SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
11946fad8f66SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
11956b6b6042SThierry Reding 	unsigned int vbe, vse, hbe, hse, vbs, hbs, i;
11966b6b6042SThierry Reding 	struct tegra_sor *sor = to_sor(output);
119734fa183bSThierry Reding 	struct tegra_sor_config config;
119834fa183bSThierry Reding 	struct drm_dp_link link;
119934fa183bSThierry Reding 	struct drm_dp_aux *aux;
120086f5c52dSThierry Reding 	int err = 0;
120128fe2076SThierry Reding 	u32 value;
120286f5c52dSThierry Reding 
12036b6b6042SThierry Reding 	err = clk_prepare_enable(sor->clk);
12046b6b6042SThierry Reding 	if (err < 0)
1205850bab44SThierry Reding 		dev_err(sor->dev, "failed to enable clock: %d\n", err);
12066b6b6042SThierry Reding 
12076b6b6042SThierry Reding 	reset_control_deassert(sor->rst);
12086b6b6042SThierry Reding 
12096fad8f66SThierry Reding 	if (output->panel)
12106fad8f66SThierry Reding 		drm_panel_prepare(output->panel);
12116fad8f66SThierry Reding 
121234fa183bSThierry Reding 	/* FIXME: properly convert to struct drm_dp_aux */
121334fa183bSThierry Reding 	aux = (struct drm_dp_aux *)sor->dpaux;
121434fa183bSThierry Reding 
12156b6b6042SThierry Reding 	if (sor->dpaux) {
12166b6b6042SThierry Reding 		err = tegra_dpaux_enable(sor->dpaux);
12176b6b6042SThierry Reding 		if (err < 0)
12186b6b6042SThierry Reding 			dev_err(sor->dev, "failed to enable DP: %d\n", err);
121934fa183bSThierry Reding 
122034fa183bSThierry Reding 		err = drm_dp_link_probe(aux, &link);
122134fa183bSThierry Reding 		if (err < 0) {
122234fa183bSThierry Reding 			dev_err(sor->dev, "failed to probe eDP link: %d\n",
122334fa183bSThierry Reding 				err);
1224850bab44SThierry Reding 			return;
122534fa183bSThierry Reding 		}
12266b6b6042SThierry Reding 	}
12276b6b6042SThierry Reding 
12286b6b6042SThierry Reding 	err = clk_set_parent(sor->clk, sor->clk_safe);
12296b6b6042SThierry Reding 	if (err < 0)
12306b6b6042SThierry Reding 		dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
12316b6b6042SThierry Reding 
123234fa183bSThierry Reding 	memset(&config, 0, sizeof(config));
1233054b1bd1SStéphane Marchesin 	config.bits_per_pixel = output->connector.display_info.bpc * 3;
123434fa183bSThierry Reding 
123534fa183bSThierry Reding 	err = tegra_sor_calc_config(sor, mode, &config, &link);
123634fa183bSThierry Reding 	if (err < 0)
123734fa183bSThierry Reding 		dev_err(sor->dev, "failed to compute link configuration: %d\n",
123834fa183bSThierry Reding 			err);
123934fa183bSThierry Reding 
12406b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
12416b6b6042SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
12426b6b6042SThierry Reding 	value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
12436b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
12446b6b6042SThierry Reding 
1245a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1246a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
1247a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
12486b6b6042SThierry Reding 	usleep_range(20, 100);
12496b6b6042SThierry Reding 
1250a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL3);
1251a9a9e4fdSThierry Reding 	value |= SOR_PLL3_PLL_VDD_MODE_3V3;
1252a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL3);
12536b6b6042SThierry Reding 
1254a9a9e4fdSThierry Reding 	value = SOR_PLL0_ICHPMP(0xf) | SOR_PLL0_VCOCAP_RST |
1255a9a9e4fdSThierry Reding 		SOR_PLL0_PLLREG_LEVEL_V45 | SOR_PLL0_RESISTOR_EXT;
1256a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL0);
12576b6b6042SThierry Reding 
1258a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1259a9a9e4fdSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD;
1260a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1261a9a9e4fdSThierry Reding 	value |= SOR_PLL2_LVDS_ENABLE;
1262a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
12636b6b6042SThierry Reding 
1264a9a9e4fdSThierry Reding 	value = SOR_PLL1_TERM_COMPOUT | SOR_PLL1_TMDS_TERM;
1265a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL1);
12666b6b6042SThierry Reding 
12676b6b6042SThierry Reding 	while (true) {
1268a9a9e4fdSThierry Reding 		value = tegra_sor_readl(sor, SOR_PLL2);
1269a9a9e4fdSThierry Reding 		if ((value & SOR_PLL2_SEQ_PLLCAPPD_ENFORCE) == 0)
12706b6b6042SThierry Reding 			break;
12716b6b6042SThierry Reding 
12726b6b6042SThierry Reding 		usleep_range(250, 1000);
12736b6b6042SThierry Reding 	}
12746b6b6042SThierry Reding 
1275a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1276a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
1277a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_PORT_POWERDOWN;
1278a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
12796b6b6042SThierry Reding 
12806b6b6042SThierry Reding 	/*
12816b6b6042SThierry Reding 	 * power up
12826b6b6042SThierry Reding 	 */
12836b6b6042SThierry Reding 
12846b6b6042SThierry Reding 	/* set safe link bandwidth (1.62 Gbps) */
12856b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
12866b6b6042SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
12876b6b6042SThierry Reding 	value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G1_62;
12886b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
12896b6b6042SThierry Reding 
12906b6b6042SThierry Reding 	/* step 1 */
1291a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1292a9a9e4fdSThierry Reding 	value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE | SOR_PLL2_PORT_POWERDOWN |
1293a9a9e4fdSThierry Reding 		 SOR_PLL2_BANDGAP_POWERDOWN;
1294a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
12956b6b6042SThierry Reding 
1296a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL0);
1297a9a9e4fdSThierry Reding 	value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
1298a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL0);
12996b6b6042SThierry Reding 
1300a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
13016b6b6042SThierry Reding 	value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
1302a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
13036b6b6042SThierry Reding 
13046b6b6042SThierry Reding 	/* step 2 */
13056b6b6042SThierry Reding 	err = tegra_io_rail_power_on(TEGRA_IO_RAIL_LVDS);
1306850bab44SThierry Reding 	if (err < 0)
13076b6b6042SThierry Reding 		dev_err(sor->dev, "failed to power on I/O rail: %d\n", err);
13086b6b6042SThierry Reding 
13096b6b6042SThierry Reding 	usleep_range(5, 100);
13106b6b6042SThierry Reding 
13116b6b6042SThierry Reding 	/* step 3 */
1312a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1313a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
1314a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
13156b6b6042SThierry Reding 
13166b6b6042SThierry Reding 	usleep_range(20, 100);
13176b6b6042SThierry Reding 
13186b6b6042SThierry Reding 	/* step 4 */
1319a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL0);
1320a9a9e4fdSThierry Reding 	value &= ~SOR_PLL0_VCOPD;
1321a9a9e4fdSThierry Reding 	value &= ~SOR_PLL0_PWR;
1322a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL0);
13236b6b6042SThierry Reding 
1324a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1325a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1326a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
13276b6b6042SThierry Reding 
13286b6b6042SThierry Reding 	usleep_range(200, 1000);
13296b6b6042SThierry Reding 
13306b6b6042SThierry Reding 	/* step 5 */
1331a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1332a9a9e4fdSThierry Reding 	value &= ~SOR_PLL2_PORT_POWERDOWN;
1333a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
13346b6b6042SThierry Reding 
13356b6b6042SThierry Reding 	/* switch to DP clock */
13366b6b6042SThierry Reding 	err = clk_set_parent(sor->clk, sor->clk_dp);
13376b6b6042SThierry Reding 	if (err < 0)
13386b6b6042SThierry Reding 		dev_err(sor->dev, "failed to set DP parent clock: %d\n", err);
13396b6b6042SThierry Reding 
1340899451b7SThierry Reding 	/* power DP lanes */
1341a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
1342899451b7SThierry Reding 
1343899451b7SThierry Reding 	if (link.num_lanes <= 2)
1344899451b7SThierry Reding 		value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2);
1345899451b7SThierry Reding 	else
1346899451b7SThierry Reding 		value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2;
1347899451b7SThierry Reding 
1348899451b7SThierry Reding 	if (link.num_lanes <= 1)
1349899451b7SThierry Reding 		value &= ~SOR_DP_PADCTL_PD_TXD_1;
1350899451b7SThierry Reding 	else
1351899451b7SThierry Reding 		value |= SOR_DP_PADCTL_PD_TXD_1;
1352899451b7SThierry Reding 
1353899451b7SThierry Reding 	if (link.num_lanes == 0)
1354899451b7SThierry Reding 		value &= ~SOR_DP_PADCTL_PD_TXD_0;
1355899451b7SThierry Reding 	else
1356899451b7SThierry Reding 		value |= SOR_DP_PADCTL_PD_TXD_0;
1357899451b7SThierry Reding 
1358a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
13596b6b6042SThierry Reding 
1360a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
13616b6b6042SThierry Reding 	value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
13620c90a184SThierry Reding 	value |= SOR_DP_LINKCTL_LANE_COUNT(link.num_lanes);
1363a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
13646b6b6042SThierry Reding 
13656b6b6042SThierry Reding 	/* start lane sequencer */
13666b6b6042SThierry Reding 	value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
13676b6b6042SThierry Reding 		SOR_LANE_SEQ_CTL_POWER_STATE_UP;
13686b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
13696b6b6042SThierry Reding 
13706b6b6042SThierry Reding 	while (true) {
13716b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
13726b6b6042SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
13736b6b6042SThierry Reding 			break;
13746b6b6042SThierry Reding 
13756b6b6042SThierry Reding 		usleep_range(250, 1000);
13766b6b6042SThierry Reding 	}
13776b6b6042SThierry Reding 
1378a4263fedSThierry Reding 	/* set link bandwidth */
13796b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
13806b6b6042SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
1381a4263fedSThierry Reding 	value |= drm_dp_link_rate_to_bw_code(link.rate) << 2;
13826b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
13836b6b6042SThierry Reding 
13846b6b6042SThierry Reding 	/* set linkctl */
1385a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
13866b6b6042SThierry Reding 	value |= SOR_DP_LINKCTL_ENABLE;
13876b6b6042SThierry Reding 
13886b6b6042SThierry Reding 	value &= ~SOR_DP_LINKCTL_TU_SIZE_MASK;
138934fa183bSThierry Reding 	value |= SOR_DP_LINKCTL_TU_SIZE(config.tu_size);
13906b6b6042SThierry Reding 
13916b6b6042SThierry Reding 	value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
1392a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
13936b6b6042SThierry Reding 
13946b6b6042SThierry Reding 	for (i = 0, value = 0; i < 4; i++) {
13956b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
13966b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_GALIOS |
13976b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_NONE;
13986b6b6042SThierry Reding 		value = (value << 8) | lane;
13996b6b6042SThierry Reding 	}
14006b6b6042SThierry Reding 
14016b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
14026b6b6042SThierry Reding 
1403a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_CONFIG0);
14046b6b6042SThierry Reding 	value &= ~SOR_DP_CONFIG_WATERMARK_MASK;
140534fa183bSThierry Reding 	value |= SOR_DP_CONFIG_WATERMARK(config.watermark);
14066b6b6042SThierry Reding 
14076b6b6042SThierry Reding 	value &= ~SOR_DP_CONFIG_ACTIVE_SYM_COUNT_MASK;
140834fa183bSThierry Reding 	value |= SOR_DP_CONFIG_ACTIVE_SYM_COUNT(config.active_count);
14096b6b6042SThierry Reding 
14106b6b6042SThierry Reding 	value &= ~SOR_DP_CONFIG_ACTIVE_SYM_FRAC_MASK;
141134fa183bSThierry Reding 	value |= SOR_DP_CONFIG_ACTIVE_SYM_FRAC(config.active_frac);
14126b6b6042SThierry Reding 
141334fa183bSThierry Reding 	if (config.active_polarity)
141434fa183bSThierry Reding 		value |= SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
141534fa183bSThierry Reding 	else
141634fa183bSThierry Reding 		value &= ~SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
14176b6b6042SThierry Reding 
14186b6b6042SThierry Reding 	value |= SOR_DP_CONFIG_ACTIVE_SYM_ENABLE;
14191f64ae7cSThierry Reding 	value |= SOR_DP_CONFIG_DISPARITY_NEGATIVE;
1420a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_CONFIG0);
14216b6b6042SThierry Reding 
14226b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_AUDIO_HBLANK_SYMBOLS);
14236b6b6042SThierry Reding 	value &= ~SOR_DP_AUDIO_HBLANK_SYMBOLS_MASK;
14247890b576SThierry Reding 	value |= config.hblank_symbols & 0xffff;
14256b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_AUDIO_HBLANK_SYMBOLS);
14266b6b6042SThierry Reding 
14276b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_AUDIO_VBLANK_SYMBOLS);
14286b6b6042SThierry Reding 	value &= ~SOR_DP_AUDIO_VBLANK_SYMBOLS_MASK;
14297890b576SThierry Reding 	value |= config.vblank_symbols & 0xffff;
14306b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_AUDIO_VBLANK_SYMBOLS);
14316b6b6042SThierry Reding 
14326b6b6042SThierry Reding 	/* enable pad calibration logic */
1433a9a9e4fdSThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
14346b6b6042SThierry Reding 	value |= SOR_DP_PADCTL_PAD_CAL_PD;
1435a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
14366b6b6042SThierry Reding 
14376b6b6042SThierry Reding 	if (sor->dpaux) {
14386b6b6042SThierry Reding 		u8 rate, lanes;
14396b6b6042SThierry Reding 
14406b6b6042SThierry Reding 		err = drm_dp_link_probe(aux, &link);
1441850bab44SThierry Reding 		if (err < 0)
14426b6b6042SThierry Reding 			dev_err(sor->dev, "failed to probe eDP link: %d\n",
14436b6b6042SThierry Reding 				err);
14446b6b6042SThierry Reding 
14456b6b6042SThierry Reding 		err = drm_dp_link_power_up(aux, &link);
1446850bab44SThierry Reding 		if (err < 0)
14476b6b6042SThierry Reding 			dev_err(sor->dev, "failed to power up eDP link: %d\n",
14486b6b6042SThierry Reding 				err);
14496b6b6042SThierry Reding 
14506b6b6042SThierry Reding 		err = drm_dp_link_configure(aux, &link);
1451850bab44SThierry Reding 		if (err < 0)
14526b6b6042SThierry Reding 			dev_err(sor->dev, "failed to configure eDP link: %d\n",
14536b6b6042SThierry Reding 				err);
14546b6b6042SThierry Reding 
14556b6b6042SThierry Reding 		rate = drm_dp_link_rate_to_bw_code(link.rate);
14566b6b6042SThierry Reding 		lanes = link.num_lanes;
14576b6b6042SThierry Reding 
14586b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
14596b6b6042SThierry Reding 		value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
14606b6b6042SThierry Reding 		value |= SOR_CLK_CNTRL_DP_LINK_SPEED(rate);
14616b6b6042SThierry Reding 		tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
14626b6b6042SThierry Reding 
1463a9a9e4fdSThierry Reding 		value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
14646b6b6042SThierry Reding 		value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
14656b6b6042SThierry Reding 		value |= SOR_DP_LINKCTL_LANE_COUNT(lanes);
14666b6b6042SThierry Reding 
14676b6b6042SThierry Reding 		if (link.capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
14686b6b6042SThierry Reding 			value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
14696b6b6042SThierry Reding 
1470a9a9e4fdSThierry Reding 		tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
14716b6b6042SThierry Reding 
14726b6b6042SThierry Reding 		/* disable training pattern generator */
14736b6b6042SThierry Reding 
14746b6b6042SThierry Reding 		for (i = 0; i < link.num_lanes; i++) {
14756b6b6042SThierry Reding 			unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
14766b6b6042SThierry Reding 					     SOR_DP_TPG_SCRAMBLER_GALIOS |
14776b6b6042SThierry Reding 					     SOR_DP_TPG_PATTERN_NONE;
14786b6b6042SThierry Reding 			value = (value << 8) | lane;
14796b6b6042SThierry Reding 		}
14806b6b6042SThierry Reding 
14816b6b6042SThierry Reding 		tegra_sor_writel(sor, value, SOR_DP_TPG);
14826b6b6042SThierry Reding 
14836b6b6042SThierry Reding 		err = tegra_sor_dp_train_fast(sor, &link);
14846b6b6042SThierry Reding 		if (err < 0) {
14856b6b6042SThierry Reding 			dev_err(sor->dev, "DP fast link training failed: %d\n",
14866b6b6042SThierry Reding 				err);
14876b6b6042SThierry Reding 		}
14886b6b6042SThierry Reding 
14896b6b6042SThierry Reding 		dev_dbg(sor->dev, "fast link training succeeded\n");
14906b6b6042SThierry Reding 	}
14916b6b6042SThierry Reding 
14926b6b6042SThierry Reding 	err = tegra_sor_power_up(sor, 250);
1493850bab44SThierry Reding 	if (err < 0)
14946b6b6042SThierry Reding 		dev_err(sor->dev, "failed to power up SOR: %d\n", err);
14956b6b6042SThierry Reding 
14966b6b6042SThierry Reding 	/*
14976b6b6042SThierry Reding 	 * configure panel (24bpp, vsync-, hsync-, DP-A protocol, complete
14986b6b6042SThierry Reding 	 * raster, associate with display controller)
14996b6b6042SThierry Reding 	 */
15003f4f3b5fSThierry Reding 	value = SOR_STATE_ASY_PROTOCOL_DP_A |
15016b6b6042SThierry Reding 		SOR_STATE_ASY_CRC_MODE_COMPLETE |
15026b6b6042SThierry Reding 		SOR_STATE_ASY_OWNER(dc->pipe + 1);
150334fa183bSThierry Reding 
15043f4f3b5fSThierry Reding 	if (mode->flags & DRM_MODE_FLAG_PHSYNC)
15053f4f3b5fSThierry Reding 		value &= ~SOR_STATE_ASY_HSYNCPOL;
15063f4f3b5fSThierry Reding 
15073f4f3b5fSThierry Reding 	if (mode->flags & DRM_MODE_FLAG_NHSYNC)
15083f4f3b5fSThierry Reding 		value |= SOR_STATE_ASY_HSYNCPOL;
15093f4f3b5fSThierry Reding 
15103f4f3b5fSThierry Reding 	if (mode->flags & DRM_MODE_FLAG_PVSYNC)
15113f4f3b5fSThierry Reding 		value &= ~SOR_STATE_ASY_VSYNCPOL;
15123f4f3b5fSThierry Reding 
15133f4f3b5fSThierry Reding 	if (mode->flags & DRM_MODE_FLAG_NVSYNC)
15143f4f3b5fSThierry Reding 		value |= SOR_STATE_ASY_VSYNCPOL;
15153f4f3b5fSThierry Reding 
151634fa183bSThierry Reding 	switch (config.bits_per_pixel) {
151734fa183bSThierry Reding 	case 24:
151834fa183bSThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
151934fa183bSThierry Reding 		break;
152034fa183bSThierry Reding 
152134fa183bSThierry Reding 	case 18:
152234fa183bSThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_18_444;
152334fa183bSThierry Reding 		break;
152434fa183bSThierry Reding 
152534fa183bSThierry Reding 	default:
152634fa183bSThierry Reding 		BUG();
152734fa183bSThierry Reding 		break;
152834fa183bSThierry Reding 	}
152934fa183bSThierry Reding 
1530a9a9e4fdSThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
15316b6b6042SThierry Reding 
15326b6b6042SThierry Reding 	/*
15336b6b6042SThierry Reding 	 * TODO: The video timing programming below doesn't seem to match the
15346b6b6042SThierry Reding 	 * register definitions.
15356b6b6042SThierry Reding 	 */
15366b6b6042SThierry Reding 
15376b6b6042SThierry Reding 	value = ((mode->vtotal & 0x7fff) << 16) | (mode->htotal & 0x7fff);
153851511d05SThierry Reding 	tegra_sor_writel(sor, value, SOR_HEAD_STATE1(dc->pipe));
15396b6b6042SThierry Reding 
15406b6b6042SThierry Reding 	vse = mode->vsync_end - mode->vsync_start - 1;
15416b6b6042SThierry Reding 	hse = mode->hsync_end - mode->hsync_start - 1;
15426b6b6042SThierry Reding 
15436b6b6042SThierry Reding 	value = ((vse & 0x7fff) << 16) | (hse & 0x7fff);
154451511d05SThierry Reding 	tegra_sor_writel(sor, value, SOR_HEAD_STATE2(dc->pipe));
15456b6b6042SThierry Reding 
15466b6b6042SThierry Reding 	vbe = vse + (mode->vsync_start - mode->vdisplay);
15476b6b6042SThierry Reding 	hbe = hse + (mode->hsync_start - mode->hdisplay);
15486b6b6042SThierry Reding 
15496b6b6042SThierry Reding 	value = ((vbe & 0x7fff) << 16) | (hbe & 0x7fff);
155051511d05SThierry Reding 	tegra_sor_writel(sor, value, SOR_HEAD_STATE3(dc->pipe));
15516b6b6042SThierry Reding 
15526b6b6042SThierry Reding 	vbs = vbe + mode->vdisplay;
15536b6b6042SThierry Reding 	hbs = hbe + mode->hdisplay;
15546b6b6042SThierry Reding 
15556b6b6042SThierry Reding 	value = ((vbs & 0x7fff) << 16) | (hbs & 0x7fff);
155651511d05SThierry Reding 	tegra_sor_writel(sor, value, SOR_HEAD_STATE4(dc->pipe));
155751511d05SThierry Reding 
155851511d05SThierry Reding 	tegra_sor_writel(sor, 0x1, SOR_HEAD_STATE5(dc->pipe));
15596b6b6042SThierry Reding 
15606b6b6042SThierry Reding 	/* CSTM (LVDS, link A/B, upper) */
1561143b1df2SStéphane Marchesin 	value = SOR_CSTM_LVDS | SOR_CSTM_LINK_ACT_A | SOR_CSTM_LINK_ACT_B |
15626b6b6042SThierry Reding 		SOR_CSTM_UPPER;
15636b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CSTM);
15646b6b6042SThierry Reding 
15656b6b6042SThierry Reding 	/* PWM setup */
15666b6b6042SThierry Reding 	err = tegra_sor_setup_pwm(sor, 250);
1567850bab44SThierry Reding 	if (err < 0)
15686b6b6042SThierry Reding 		dev_err(sor->dev, "failed to setup PWM: %d\n", err);
15696b6b6042SThierry Reding 
1570666cb873SThierry Reding 	tegra_sor_update(sor);
1571666cb873SThierry Reding 
15726b6b6042SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
15736b6b6042SThierry Reding 	value |= SOR_ENABLE;
15746b6b6042SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
15756b6b6042SThierry Reding 
1576666cb873SThierry Reding 	tegra_dc_commit(dc);
15776b6b6042SThierry Reding 
15786b6b6042SThierry Reding 	err = tegra_sor_attach(sor);
1579850bab44SThierry Reding 	if (err < 0)
15806b6b6042SThierry Reding 		dev_err(sor->dev, "failed to attach SOR: %d\n", err);
15816b6b6042SThierry Reding 
15826b6b6042SThierry Reding 	err = tegra_sor_wakeup(sor);
1583850bab44SThierry Reding 	if (err < 0)
15846b6b6042SThierry Reding 		dev_err(sor->dev, "failed to enable DC: %d\n", err);
15856b6b6042SThierry Reding 
15866fad8f66SThierry Reding 	if (output->panel)
15876fad8f66SThierry Reding 		drm_panel_enable(output->panel);
15886b6b6042SThierry Reding }
15896b6b6042SThierry Reding 
159082f1511cSThierry Reding static int
159182f1511cSThierry Reding tegra_sor_encoder_atomic_check(struct drm_encoder *encoder,
159282f1511cSThierry Reding 			       struct drm_crtc_state *crtc_state,
159382f1511cSThierry Reding 			       struct drm_connector_state *conn_state)
159482f1511cSThierry Reding {
159582f1511cSThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
159682f1511cSThierry Reding 	struct tegra_dc *dc = to_tegra_dc(conn_state->crtc);
159782f1511cSThierry Reding 	unsigned long pclk = crtc_state->mode.clock * 1000;
159882f1511cSThierry Reding 	struct tegra_sor *sor = to_sor(output);
159982f1511cSThierry Reding 	int err;
160082f1511cSThierry Reding 
160182f1511cSThierry Reding 	err = tegra_dc_state_setup_clock(dc, crtc_state, sor->clk_parent,
160282f1511cSThierry Reding 					 pclk, 0);
160382f1511cSThierry Reding 	if (err < 0) {
160482f1511cSThierry Reding 		dev_err(output->dev, "failed to setup CRTC state: %d\n", err);
160582f1511cSThierry Reding 		return err;
160682f1511cSThierry Reding 	}
160782f1511cSThierry Reding 
160882f1511cSThierry Reding 	return 0;
160982f1511cSThierry Reding }
161082f1511cSThierry Reding 
1611*459cc2c6SThierry Reding static const struct drm_encoder_helper_funcs tegra_sor_edp_helpers = {
1612850bab44SThierry Reding 	.disable = tegra_sor_edp_disable,
1613850bab44SThierry Reding 	.enable = tegra_sor_edp_enable,
161482f1511cSThierry Reding 	.atomic_check = tegra_sor_encoder_atomic_check,
16156b6b6042SThierry Reding };
16166b6b6042SThierry Reding 
1617*459cc2c6SThierry Reding static inline u32 tegra_sor_hdmi_subpack(const u8 *ptr, size_t size)
1618*459cc2c6SThierry Reding {
1619*459cc2c6SThierry Reding 	u32 value = 0;
1620*459cc2c6SThierry Reding 	size_t i;
1621*459cc2c6SThierry Reding 
1622*459cc2c6SThierry Reding 	for (i = size; i > 0; i--)
1623*459cc2c6SThierry Reding 		value = (value << 8) | ptr[i - 1];
1624*459cc2c6SThierry Reding 
1625*459cc2c6SThierry Reding 	return value;
1626*459cc2c6SThierry Reding }
1627*459cc2c6SThierry Reding 
1628*459cc2c6SThierry Reding static void tegra_sor_hdmi_write_infopack(struct tegra_sor *sor,
1629*459cc2c6SThierry Reding 					  const void *data, size_t size)
1630*459cc2c6SThierry Reding {
1631*459cc2c6SThierry Reding 	const u8 *ptr = data;
1632*459cc2c6SThierry Reding 	unsigned long offset;
1633*459cc2c6SThierry Reding 	size_t i, j;
1634*459cc2c6SThierry Reding 	u32 value;
1635*459cc2c6SThierry Reding 
1636*459cc2c6SThierry Reding 	switch (ptr[0]) {
1637*459cc2c6SThierry Reding 	case HDMI_INFOFRAME_TYPE_AVI:
1638*459cc2c6SThierry Reding 		offset = SOR_HDMI_AVI_INFOFRAME_HEADER;
1639*459cc2c6SThierry Reding 		break;
1640*459cc2c6SThierry Reding 
1641*459cc2c6SThierry Reding 	case HDMI_INFOFRAME_TYPE_AUDIO:
1642*459cc2c6SThierry Reding 		offset = SOR_HDMI_AUDIO_INFOFRAME_HEADER;
1643*459cc2c6SThierry Reding 		break;
1644*459cc2c6SThierry Reding 
1645*459cc2c6SThierry Reding 	case HDMI_INFOFRAME_TYPE_VENDOR:
1646*459cc2c6SThierry Reding 		offset = SOR_HDMI_VSI_INFOFRAME_HEADER;
1647*459cc2c6SThierry Reding 		break;
1648*459cc2c6SThierry Reding 
1649*459cc2c6SThierry Reding 	default:
1650*459cc2c6SThierry Reding 		dev_err(sor->dev, "unsupported infoframe type: %02x\n",
1651*459cc2c6SThierry Reding 			ptr[0]);
1652*459cc2c6SThierry Reding 		return;
1653*459cc2c6SThierry Reding 	}
1654*459cc2c6SThierry Reding 
1655*459cc2c6SThierry Reding 	value = INFOFRAME_HEADER_TYPE(ptr[0]) |
1656*459cc2c6SThierry Reding 		INFOFRAME_HEADER_VERSION(ptr[1]) |
1657*459cc2c6SThierry Reding 		INFOFRAME_HEADER_LEN(ptr[2]);
1658*459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, offset);
1659*459cc2c6SThierry Reding 	offset++;
1660*459cc2c6SThierry Reding 
1661*459cc2c6SThierry Reding 	/*
1662*459cc2c6SThierry Reding 	 * Each subpack contains 7 bytes, divided into:
1663*459cc2c6SThierry Reding 	 * - subpack_low: bytes 0 - 3
1664*459cc2c6SThierry Reding 	 * - subpack_high: bytes 4 - 6 (with byte 7 padded to 0x00)
1665*459cc2c6SThierry Reding 	 */
1666*459cc2c6SThierry Reding 	for (i = 3, j = 0; i < size; i += 7, j += 8) {
1667*459cc2c6SThierry Reding 		size_t rem = size - i, num = min_t(size_t, rem, 4);
1668*459cc2c6SThierry Reding 
1669*459cc2c6SThierry Reding 		value = tegra_sor_hdmi_subpack(&ptr[i], num);
1670*459cc2c6SThierry Reding 		tegra_sor_writel(sor, value, offset++);
1671*459cc2c6SThierry Reding 
1672*459cc2c6SThierry Reding 		num = min_t(size_t, rem - num, 3);
1673*459cc2c6SThierry Reding 
1674*459cc2c6SThierry Reding 		value = tegra_sor_hdmi_subpack(&ptr[i + 4], num);
1675*459cc2c6SThierry Reding 		tegra_sor_writel(sor, value, offset++);
1676*459cc2c6SThierry Reding 	}
1677*459cc2c6SThierry Reding }
1678*459cc2c6SThierry Reding 
1679*459cc2c6SThierry Reding static int
1680*459cc2c6SThierry Reding tegra_sor_hdmi_setup_avi_infoframe(struct tegra_sor *sor,
1681*459cc2c6SThierry Reding 				   const struct drm_display_mode *mode)
1682*459cc2c6SThierry Reding {
1683*459cc2c6SThierry Reding 	u8 buffer[HDMI_INFOFRAME_SIZE(AVI)];
1684*459cc2c6SThierry Reding 	struct hdmi_avi_infoframe frame;
1685*459cc2c6SThierry Reding 	u32 value;
1686*459cc2c6SThierry Reding 	int err;
1687*459cc2c6SThierry Reding 
1688*459cc2c6SThierry Reding 	/* disable AVI infoframe */
1689*459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI_AVI_INFOFRAME_CTRL);
1690*459cc2c6SThierry Reding 	value &= ~INFOFRAME_CTRL_SINGLE;
1691*459cc2c6SThierry Reding 	value &= ~INFOFRAME_CTRL_OTHER;
1692*459cc2c6SThierry Reding 	value &= ~INFOFRAME_CTRL_ENABLE;
1693*459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_AVI_INFOFRAME_CTRL);
1694*459cc2c6SThierry Reding 
1695*459cc2c6SThierry Reding 	err = drm_hdmi_avi_infoframe_from_display_mode(&frame, mode);
1696*459cc2c6SThierry Reding 	if (err < 0) {
1697*459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to setup AVI infoframe: %d\n", err);
1698*459cc2c6SThierry Reding 		return err;
1699*459cc2c6SThierry Reding 	}
1700*459cc2c6SThierry Reding 
1701*459cc2c6SThierry Reding 	err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
1702*459cc2c6SThierry Reding 	if (err < 0) {
1703*459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to pack AVI infoframe: %d\n", err);
1704*459cc2c6SThierry Reding 		return err;
1705*459cc2c6SThierry Reding 	}
1706*459cc2c6SThierry Reding 
1707*459cc2c6SThierry Reding 	tegra_sor_hdmi_write_infopack(sor, buffer, err);
1708*459cc2c6SThierry Reding 
1709*459cc2c6SThierry Reding 	/* enable AVI infoframe */
1710*459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI_AVI_INFOFRAME_CTRL);
1711*459cc2c6SThierry Reding 	value |= INFOFRAME_CTRL_CHECKSUM_ENABLE;
1712*459cc2c6SThierry Reding 	value |= INFOFRAME_CTRL_ENABLE;
1713*459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_AVI_INFOFRAME_CTRL);
1714*459cc2c6SThierry Reding 
1715*459cc2c6SThierry Reding 	return 0;
1716*459cc2c6SThierry Reding }
1717*459cc2c6SThierry Reding 
1718*459cc2c6SThierry Reding static void tegra_sor_hdmi_disable_audio_infoframe(struct tegra_sor *sor)
1719*459cc2c6SThierry Reding {
1720*459cc2c6SThierry Reding 	u32 value;
1721*459cc2c6SThierry Reding 
1722*459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
1723*459cc2c6SThierry Reding 	value &= ~INFOFRAME_CTRL_ENABLE;
1724*459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
1725*459cc2c6SThierry Reding }
1726*459cc2c6SThierry Reding 
1727*459cc2c6SThierry Reding static struct tegra_sor_hdmi_settings *
1728*459cc2c6SThierry Reding tegra_sor_hdmi_find_settings(struct tegra_sor *sor, unsigned long frequency)
1729*459cc2c6SThierry Reding {
1730*459cc2c6SThierry Reding 	unsigned int i;
1731*459cc2c6SThierry Reding 
1732*459cc2c6SThierry Reding 	for (i = 0; i < sor->num_settings; i++)
1733*459cc2c6SThierry Reding 		if (frequency <= sor->settings[i].frequency)
1734*459cc2c6SThierry Reding 			return &sor->settings[i];
1735*459cc2c6SThierry Reding 
1736*459cc2c6SThierry Reding 	return NULL;
1737*459cc2c6SThierry Reding }
1738*459cc2c6SThierry Reding 
1739*459cc2c6SThierry Reding static void tegra_sor_hdmi_disable(struct drm_encoder *encoder)
1740*459cc2c6SThierry Reding {
1741*459cc2c6SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
1742*459cc2c6SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
1743*459cc2c6SThierry Reding 	struct tegra_sor *sor = to_sor(output);
1744*459cc2c6SThierry Reding 	u32 value;
1745*459cc2c6SThierry Reding 	int err;
1746*459cc2c6SThierry Reding 
1747*459cc2c6SThierry Reding 	err = tegra_sor_detach(sor);
1748*459cc2c6SThierry Reding 	if (err < 0)
1749*459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to detach SOR: %d\n", err);
1750*459cc2c6SThierry Reding 
1751*459cc2c6SThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE1);
1752*459cc2c6SThierry Reding 	tegra_sor_update(sor);
1753*459cc2c6SThierry Reding 
1754*459cc2c6SThierry Reding 	/* disable display to SOR clock */
1755*459cc2c6SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
1756*459cc2c6SThierry Reding 	value &= ~SOR1_TIMING_CYA;
1757*459cc2c6SThierry Reding 	value &= ~SOR1_ENABLE;
1758*459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
1759*459cc2c6SThierry Reding 
1760*459cc2c6SThierry Reding 	tegra_dc_commit(dc);
1761*459cc2c6SThierry Reding 
1762*459cc2c6SThierry Reding 	err = tegra_sor_power_down(sor);
1763*459cc2c6SThierry Reding 	if (err < 0)
1764*459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to power down SOR: %d\n", err);
1765*459cc2c6SThierry Reding 
1766*459cc2c6SThierry Reding 	err = tegra_io_rail_power_off(TEGRA_IO_RAIL_HDMI);
1767*459cc2c6SThierry Reding 	if (err < 0)
1768*459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to power off HDMI rail: %d\n", err);
1769*459cc2c6SThierry Reding 
1770*459cc2c6SThierry Reding 	reset_control_assert(sor->rst);
1771*459cc2c6SThierry Reding 	usleep_range(1000, 2000);
1772*459cc2c6SThierry Reding 	clk_disable_unprepare(sor->clk);
1773*459cc2c6SThierry Reding }
1774*459cc2c6SThierry Reding 
1775*459cc2c6SThierry Reding static void tegra_sor_hdmi_enable(struct drm_encoder *encoder)
1776*459cc2c6SThierry Reding {
1777*459cc2c6SThierry Reding 	struct tegra_output *output = encoder_to_output(encoder);
1778*459cc2c6SThierry Reding 	unsigned int h_ref_to_sync = 1, pulse_start, max_ac;
1779*459cc2c6SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
1780*459cc2c6SThierry Reding 	unsigned int vbe, vse, hbe, hse, vbs, hbs, div;
1781*459cc2c6SThierry Reding 	struct tegra_sor_hdmi_settings *settings;
1782*459cc2c6SThierry Reding 	struct tegra_sor *sor = to_sor(output);
1783*459cc2c6SThierry Reding 	struct drm_display_mode *mode;
1784*459cc2c6SThierry Reding 	struct drm_display_info *info;
1785*459cc2c6SThierry Reding 	u32 value;
1786*459cc2c6SThierry Reding 	int err;
1787*459cc2c6SThierry Reding 
1788*459cc2c6SThierry Reding 	mode = &encoder->crtc->state->adjusted_mode;
1789*459cc2c6SThierry Reding 	info = &output->connector.display_info;
1790*459cc2c6SThierry Reding 
1791*459cc2c6SThierry Reding 	err = clk_prepare_enable(sor->clk);
1792*459cc2c6SThierry Reding 	if (err < 0)
1793*459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to enable clock: %d\n", err);
1794*459cc2c6SThierry Reding 
1795*459cc2c6SThierry Reding 	usleep_range(1000, 2000);
1796*459cc2c6SThierry Reding 
1797*459cc2c6SThierry Reding 	reset_control_deassert(sor->rst);
1798*459cc2c6SThierry Reding 
1799*459cc2c6SThierry Reding 	err = clk_set_parent(sor->clk, sor->clk_safe);
1800*459cc2c6SThierry Reding 	if (err < 0)
1801*459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
1802*459cc2c6SThierry Reding 
1803*459cc2c6SThierry Reding 	div = clk_get_rate(sor->clk) / 1000000 * 4;
1804*459cc2c6SThierry Reding 
1805*459cc2c6SThierry Reding 	err = tegra_io_rail_power_on(TEGRA_IO_RAIL_HDMI);
1806*459cc2c6SThierry Reding 	if (err < 0)
1807*459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to power on HDMI rail: %d\n", err);
1808*459cc2c6SThierry Reding 
1809*459cc2c6SThierry Reding 	usleep_range(20, 100);
1810*459cc2c6SThierry Reding 
1811*459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1812*459cc2c6SThierry Reding 	value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
1813*459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
1814*459cc2c6SThierry Reding 
1815*459cc2c6SThierry Reding 	usleep_range(20, 100);
1816*459cc2c6SThierry Reding 
1817*459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL3);
1818*459cc2c6SThierry Reding 	value &= ~SOR_PLL3_PLL_VDD_MODE_3V3;
1819*459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL3);
1820*459cc2c6SThierry Reding 
1821*459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL0);
1822*459cc2c6SThierry Reding 	value &= ~SOR_PLL0_VCOPD;
1823*459cc2c6SThierry Reding 	value &= ~SOR_PLL0_PWR;
1824*459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL0);
1825*459cc2c6SThierry Reding 
1826*459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1827*459cc2c6SThierry Reding 	value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1828*459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
1829*459cc2c6SThierry Reding 
1830*459cc2c6SThierry Reding 	usleep_range(200, 400);
1831*459cc2c6SThierry Reding 
1832*459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL2);
1833*459cc2c6SThierry Reding 	value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
1834*459cc2c6SThierry Reding 	value &= ~SOR_PLL2_PORT_POWERDOWN;
1835*459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL2);
1836*459cc2c6SThierry Reding 
1837*459cc2c6SThierry Reding 	usleep_range(20, 100);
1838*459cc2c6SThierry Reding 
1839*459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
1840*459cc2c6SThierry Reding 	value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
1841*459cc2c6SThierry Reding 		 SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2;
1842*459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
1843*459cc2c6SThierry Reding 
1844*459cc2c6SThierry Reding 	while (true) {
1845*459cc2c6SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
1846*459cc2c6SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_STATE_BUSY) == 0)
1847*459cc2c6SThierry Reding 			break;
1848*459cc2c6SThierry Reding 
1849*459cc2c6SThierry Reding 		usleep_range(250, 1000);
1850*459cc2c6SThierry Reding 	}
1851*459cc2c6SThierry Reding 
1852*459cc2c6SThierry Reding 	value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
1853*459cc2c6SThierry Reding 		SOR_LANE_SEQ_CTL_POWER_STATE_UP | SOR_LANE_SEQ_CTL_DELAY(5);
1854*459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
1855*459cc2c6SThierry Reding 
1856*459cc2c6SThierry Reding 	while (true) {
1857*459cc2c6SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
1858*459cc2c6SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
1859*459cc2c6SThierry Reding 			break;
1860*459cc2c6SThierry Reding 
1861*459cc2c6SThierry Reding 		usleep_range(250, 1000);
1862*459cc2c6SThierry Reding 	}
1863*459cc2c6SThierry Reding 
1864*459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
1865*459cc2c6SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
1866*459cc2c6SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
1867*459cc2c6SThierry Reding 
1868*459cc2c6SThierry Reding 	if (mode->clock < 340000)
1869*459cc2c6SThierry Reding 		value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G2_70;
1870*459cc2c6SThierry Reding 	else
1871*459cc2c6SThierry Reding 		value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G5_40;
1872*459cc2c6SThierry Reding 
1873*459cc2c6SThierry Reding 	value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK;
1874*459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
1875*459cc2c6SThierry Reding 
1876*459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_SPARE0);
1877*459cc2c6SThierry Reding 	value |= SOR_DP_SPARE_DISP_VIDEO_PREAMBLE;
1878*459cc2c6SThierry Reding 	value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
1879*459cc2c6SThierry Reding 	value |= SOR_DP_SPARE_SEQ_ENABLE;
1880*459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_SPARE0);
1881*459cc2c6SThierry Reding 
1882*459cc2c6SThierry Reding 	value = SOR_SEQ_CTL_PU_PC(0) | SOR_SEQ_CTL_PU_PC_ALT(0) |
1883*459cc2c6SThierry Reding 		SOR_SEQ_CTL_PD_PC(8) | SOR_SEQ_CTL_PD_PC_ALT(8);
1884*459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_SEQ_CTL);
1885*459cc2c6SThierry Reding 
1886*459cc2c6SThierry Reding 	value = SOR_SEQ_INST_DRIVE_PWM_OUT_LO | SOR_SEQ_INST_HALT |
1887*459cc2c6SThierry Reding 		SOR_SEQ_INST_WAIT_VSYNC | SOR_SEQ_INST_WAIT(1);
1888*459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_SEQ_INST(0));
1889*459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_SEQ_INST(8));
1890*459cc2c6SThierry Reding 
1891*459cc2c6SThierry Reding 	/* program the reference clock */
1892*459cc2c6SThierry Reding 	value = SOR_REFCLK_DIV_INT(div) | SOR_REFCLK_DIV_FRAC(div);
1893*459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_REFCLK);
1894*459cc2c6SThierry Reding 
1895*459cc2c6SThierry Reding 	/* XXX don't hardcode */
1896*459cc2c6SThierry Reding 	value = SOR_XBAR_CTRL_LINK1_XSEL(4, 4) |
1897*459cc2c6SThierry Reding 		SOR_XBAR_CTRL_LINK1_XSEL(3, 3) |
1898*459cc2c6SThierry Reding 		SOR_XBAR_CTRL_LINK1_XSEL(2, 2) |
1899*459cc2c6SThierry Reding 		SOR_XBAR_CTRL_LINK1_XSEL(1, 1) |
1900*459cc2c6SThierry Reding 		SOR_XBAR_CTRL_LINK1_XSEL(0, 0) |
1901*459cc2c6SThierry Reding 		SOR_XBAR_CTRL_LINK0_XSEL(4, 4) |
1902*459cc2c6SThierry Reding 		SOR_XBAR_CTRL_LINK0_XSEL(3, 3) |
1903*459cc2c6SThierry Reding 		SOR_XBAR_CTRL_LINK0_XSEL(2, 0) |
1904*459cc2c6SThierry Reding 		SOR_XBAR_CTRL_LINK0_XSEL(1, 1) |
1905*459cc2c6SThierry Reding 		SOR_XBAR_CTRL_LINK0_XSEL(0, 2);
1906*459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_XBAR_CTRL);
1907*459cc2c6SThierry Reding 
1908*459cc2c6SThierry Reding 	tegra_sor_writel(sor, 0x00000000, SOR_XBAR_POL);
1909*459cc2c6SThierry Reding 
1910*459cc2c6SThierry Reding 	err = clk_set_parent(sor->clk, sor->clk_parent);
1911*459cc2c6SThierry Reding 	if (err < 0)
1912*459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to set parent clock: %d\n", err);
1913*459cc2c6SThierry Reding 
1914*459cc2c6SThierry Reding 	value = SOR_INPUT_CONTROL_HDMI_SRC_SELECT(dc->pipe);
1915*459cc2c6SThierry Reding 
1916*459cc2c6SThierry Reding 	/* XXX is this the proper check? */
1917*459cc2c6SThierry Reding 	if (mode->clock < 75000)
1918*459cc2c6SThierry Reding 		value |= SOR_INPUT_CONTROL_ARM_VIDEO_RANGE_LIMITED;
1919*459cc2c6SThierry Reding 
1920*459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_INPUT_CONTROL);
1921*459cc2c6SThierry Reding 
1922*459cc2c6SThierry Reding 	max_ac = ((mode->htotal - mode->hdisplay) - SOR_REKEY - 18) / 32;
1923*459cc2c6SThierry Reding 
1924*459cc2c6SThierry Reding 	value = SOR_HDMI_CTRL_ENABLE | SOR_HDMI_CTRL_MAX_AC_PACKET(max_ac) |
1925*459cc2c6SThierry Reding 		SOR_HDMI_CTRL_AUDIO_LAYOUT | SOR_HDMI_CTRL_REKEY(SOR_REKEY);
1926*459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HDMI_CTRL);
1927*459cc2c6SThierry Reding 
1928*459cc2c6SThierry Reding 	/* H_PULSE2 setup */
1929*459cc2c6SThierry Reding 	pulse_start = h_ref_to_sync + (mode->hsync_end - mode->hsync_start) +
1930*459cc2c6SThierry Reding 		      (mode->htotal - mode->hsync_end) - 10;
1931*459cc2c6SThierry Reding 
1932*459cc2c6SThierry Reding 	value = PULSE_LAST_END_A | PULSE_QUAL_VACTIVE |
1933*459cc2c6SThierry Reding 		PULSE_POLARITY_HIGH | PULSE_MODE_NORMAL;
1934*459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_CONTROL);
1935*459cc2c6SThierry Reding 
1936*459cc2c6SThierry Reding 	value = PULSE_END(pulse_start + 8) | PULSE_START(pulse_start);
1937*459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_POSITION_A);
1938*459cc2c6SThierry Reding 
1939*459cc2c6SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_SIGNAL_OPTIONS0);
1940*459cc2c6SThierry Reding 	value |= H_PULSE2_ENABLE;
1941*459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_SIGNAL_OPTIONS0);
1942*459cc2c6SThierry Reding 
1943*459cc2c6SThierry Reding 	/* infoframe setup */
1944*459cc2c6SThierry Reding 	err = tegra_sor_hdmi_setup_avi_infoframe(sor, mode);
1945*459cc2c6SThierry Reding 	if (err < 0)
1946*459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to setup AVI infoframe: %d\n", err);
1947*459cc2c6SThierry Reding 
1948*459cc2c6SThierry Reding 	/* XXX HDMI audio support not implemented yet */
1949*459cc2c6SThierry Reding 	tegra_sor_hdmi_disable_audio_infoframe(sor);
1950*459cc2c6SThierry Reding 
1951*459cc2c6SThierry Reding 	/* use single TMDS protocol */
1952*459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
1953*459cc2c6SThierry Reding 	value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
1954*459cc2c6SThierry Reding 	value |= SOR_STATE_ASY_PROTOCOL_SINGLE_TMDS_A;
1955*459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
1956*459cc2c6SThierry Reding 
1957*459cc2c6SThierry Reding 	/* power up pad calibration */
1958*459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
1959*459cc2c6SThierry Reding 	value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
1960*459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
1961*459cc2c6SThierry Reding 
1962*459cc2c6SThierry Reding 	/* production settings */
1963*459cc2c6SThierry Reding 	settings = tegra_sor_hdmi_find_settings(sor, mode->clock * 1000);
1964*459cc2c6SThierry Reding 	if (IS_ERR(settings)) {
1965*459cc2c6SThierry Reding 		dev_err(sor->dev, "no settings for pixel clock %d Hz: %ld\n",
1966*459cc2c6SThierry Reding 			mode->clock * 1000, PTR_ERR(settings));
1967*459cc2c6SThierry Reding 		return;
1968*459cc2c6SThierry Reding 	}
1969*459cc2c6SThierry Reding 
1970*459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL0);
1971*459cc2c6SThierry Reding 	value &= ~SOR_PLL0_ICHPMP_MASK;
1972*459cc2c6SThierry Reding 	value &= ~SOR_PLL0_VCOCAP_MASK;
1973*459cc2c6SThierry Reding 	value |= SOR_PLL0_ICHPMP(settings->ichpmp);
1974*459cc2c6SThierry Reding 	value |= SOR_PLL0_VCOCAP(settings->vcocap);
1975*459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL0);
1976*459cc2c6SThierry Reding 
1977*459cc2c6SThierry Reding 	tegra_sor_dp_term_calibrate(sor);
1978*459cc2c6SThierry Reding 
1979*459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL1);
1980*459cc2c6SThierry Reding 	value &= ~SOR_PLL1_LOADADJ_MASK;
1981*459cc2c6SThierry Reding 	value |= SOR_PLL1_LOADADJ(settings->loadadj);
1982*459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL1);
1983*459cc2c6SThierry Reding 
1984*459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL3);
1985*459cc2c6SThierry Reding 	value &= ~SOR_PLL3_BG_VREF_LEVEL_MASK;
1986*459cc2c6SThierry Reding 	value |= SOR_PLL3_BG_VREF_LEVEL(settings->bg_vref);
1987*459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL3);
1988*459cc2c6SThierry Reding 
1989*459cc2c6SThierry Reding 	value = settings->drive_current[0] << 24 |
1990*459cc2c6SThierry Reding 		settings->drive_current[1] << 16 |
1991*459cc2c6SThierry Reding 		settings->drive_current[2] <<  8 |
1992*459cc2c6SThierry Reding 		settings->drive_current[3] <<  0;
1993*459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT0);
1994*459cc2c6SThierry Reding 
1995*459cc2c6SThierry Reding 	value = settings->preemphasis[0] << 24 |
1996*459cc2c6SThierry Reding 		settings->preemphasis[1] << 16 |
1997*459cc2c6SThierry Reding 		settings->preemphasis[2] <<  8 |
1998*459cc2c6SThierry Reding 		settings->preemphasis[3] <<  0;
1999*459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS0);
2000*459cc2c6SThierry Reding 
2001*459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
2002*459cc2c6SThierry Reding 	value &= ~SOR_DP_PADCTL_TX_PU_MASK;
2003*459cc2c6SThierry Reding 	value |= SOR_DP_PADCTL_TX_PU_ENABLE;
2004*459cc2c6SThierry Reding 	value |= SOR_DP_PADCTL_TX_PU(settings->tx_pu);
2005*459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
2006*459cc2c6SThierry Reding 
2007*459cc2c6SThierry Reding 	/* power down pad calibration */
2008*459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
2009*459cc2c6SThierry Reding 	value |= SOR_DP_PADCTL_PAD_CAL_PD;
2010*459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
2011*459cc2c6SThierry Reding 
2012*459cc2c6SThierry Reding 	/* miscellaneous display controller settings */
2013*459cc2c6SThierry Reding 	value = VSYNC_H_POSITION(1);
2014*459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_TIMING_OPTIONS);
2015*459cc2c6SThierry Reding 
2016*459cc2c6SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_COLOR_CONTROL);
2017*459cc2c6SThierry Reding 	value &= ~DITHER_CONTROL_MASK;
2018*459cc2c6SThierry Reding 	value &= ~BASE_COLOR_SIZE_MASK;
2019*459cc2c6SThierry Reding 
2020*459cc2c6SThierry Reding 	switch (info->bpc) {
2021*459cc2c6SThierry Reding 	case 6:
2022*459cc2c6SThierry Reding 		value |= BASE_COLOR_SIZE_666;
2023*459cc2c6SThierry Reding 		break;
2024*459cc2c6SThierry Reding 
2025*459cc2c6SThierry Reding 	case 8:
2026*459cc2c6SThierry Reding 		value |= BASE_COLOR_SIZE_888;
2027*459cc2c6SThierry Reding 		break;
2028*459cc2c6SThierry Reding 
2029*459cc2c6SThierry Reding 	default:
2030*459cc2c6SThierry Reding 		WARN(1, "%u bits-per-color not supported\n", info->bpc);
2031*459cc2c6SThierry Reding 		break;
2032*459cc2c6SThierry Reding 	}
2033*459cc2c6SThierry Reding 
2034*459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_COLOR_CONTROL);
2035*459cc2c6SThierry Reding 
2036*459cc2c6SThierry Reding 	err = tegra_sor_power_up(sor, 250);
2037*459cc2c6SThierry Reding 	if (err < 0)
2038*459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to power up SOR: %d\n", err);
2039*459cc2c6SThierry Reding 
2040*459cc2c6SThierry Reding 	/* configure mode */
2041*459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE1);
2042*459cc2c6SThierry Reding 	value &= ~SOR_STATE_ASY_PIXELDEPTH_MASK;
2043*459cc2c6SThierry Reding 	value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
2044*459cc2c6SThierry Reding 	value &= ~SOR_STATE_ASY_OWNER_MASK;
2045*459cc2c6SThierry Reding 
2046*459cc2c6SThierry Reding 	value |= SOR_STATE_ASY_CRC_MODE_COMPLETE |
2047*459cc2c6SThierry Reding 		 SOR_STATE_ASY_OWNER(dc->pipe + 1);
2048*459cc2c6SThierry Reding 
2049*459cc2c6SThierry Reding 	if (mode->flags & DRM_MODE_FLAG_PHSYNC)
2050*459cc2c6SThierry Reding 		value &= ~SOR_STATE_ASY_HSYNCPOL;
2051*459cc2c6SThierry Reding 
2052*459cc2c6SThierry Reding 	if (mode->flags & DRM_MODE_FLAG_NHSYNC)
2053*459cc2c6SThierry Reding 		value |= SOR_STATE_ASY_HSYNCPOL;
2054*459cc2c6SThierry Reding 
2055*459cc2c6SThierry Reding 	if (mode->flags & DRM_MODE_FLAG_PVSYNC)
2056*459cc2c6SThierry Reding 		value &= ~SOR_STATE_ASY_VSYNCPOL;
2057*459cc2c6SThierry Reding 
2058*459cc2c6SThierry Reding 	if (mode->flags & DRM_MODE_FLAG_NVSYNC)
2059*459cc2c6SThierry Reding 		value |= SOR_STATE_ASY_VSYNCPOL;
2060*459cc2c6SThierry Reding 
2061*459cc2c6SThierry Reding 	switch (info->bpc) {
2062*459cc2c6SThierry Reding 	case 8:
2063*459cc2c6SThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
2064*459cc2c6SThierry Reding 		break;
2065*459cc2c6SThierry Reding 
2066*459cc2c6SThierry Reding 	case 6:
2067*459cc2c6SThierry Reding 		value |= SOR_STATE_ASY_PIXELDEPTH_BPP_18_444;
2068*459cc2c6SThierry Reding 		break;
2069*459cc2c6SThierry Reding 
2070*459cc2c6SThierry Reding 	default:
2071*459cc2c6SThierry Reding 		BUG();
2072*459cc2c6SThierry Reding 		break;
2073*459cc2c6SThierry Reding 	}
2074*459cc2c6SThierry Reding 
2075*459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE1);
2076*459cc2c6SThierry Reding 
2077*459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_HEAD_STATE0(dc->pipe));
2078*459cc2c6SThierry Reding 	value &= ~SOR_HEAD_STATE_RANGECOMPRESS_MASK;
2079*459cc2c6SThierry Reding 	value &= ~SOR_HEAD_STATE_DYNRANGE_MASK;
2080*459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HEAD_STATE0(dc->pipe));
2081*459cc2c6SThierry Reding 
2082*459cc2c6SThierry Reding 	value = tegra_sor_readl(sor, SOR_HEAD_STATE0(dc->pipe));
2083*459cc2c6SThierry Reding 	value &= ~SOR_HEAD_STATE_COLORSPACE_MASK;
2084*459cc2c6SThierry Reding 	value |= SOR_HEAD_STATE_COLORSPACE_RGB;
2085*459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HEAD_STATE0(dc->pipe));
2086*459cc2c6SThierry Reding 
2087*459cc2c6SThierry Reding 	/*
2088*459cc2c6SThierry Reding 	 * TODO: The video timing programming below doesn't seem to match the
2089*459cc2c6SThierry Reding 	 * register definitions.
2090*459cc2c6SThierry Reding 	 */
2091*459cc2c6SThierry Reding 
2092*459cc2c6SThierry Reding 	value = ((mode->vtotal & 0x7fff) << 16) | (mode->htotal & 0x7fff);
2093*459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HEAD_STATE1(dc->pipe));
2094*459cc2c6SThierry Reding 
2095*459cc2c6SThierry Reding 	/* sync end = sync width - 1 */
2096*459cc2c6SThierry Reding 	vse = mode->vsync_end - mode->vsync_start - 1;
2097*459cc2c6SThierry Reding 	hse = mode->hsync_end - mode->hsync_start - 1;
2098*459cc2c6SThierry Reding 
2099*459cc2c6SThierry Reding 	value = ((vse & 0x7fff) << 16) | (hse & 0x7fff);
2100*459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HEAD_STATE2(dc->pipe));
2101*459cc2c6SThierry Reding 
2102*459cc2c6SThierry Reding 	/* blank end = sync end + back porch */
2103*459cc2c6SThierry Reding 	vbe = vse + (mode->vtotal - mode->vsync_end);
2104*459cc2c6SThierry Reding 	hbe = hse + (mode->htotal - mode->hsync_end);
2105*459cc2c6SThierry Reding 
2106*459cc2c6SThierry Reding 	value = ((vbe & 0x7fff) << 16) | (hbe & 0x7fff);
2107*459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HEAD_STATE3(dc->pipe));
2108*459cc2c6SThierry Reding 
2109*459cc2c6SThierry Reding 	/* blank start = blank end + active */
2110*459cc2c6SThierry Reding 	vbs = vbe + mode->vdisplay;
2111*459cc2c6SThierry Reding 	hbs = hbe + mode->hdisplay;
2112*459cc2c6SThierry Reding 
2113*459cc2c6SThierry Reding 	value = ((vbs & 0x7fff) << 16) | (hbs & 0x7fff);
2114*459cc2c6SThierry Reding 	tegra_sor_writel(sor, value, SOR_HEAD_STATE4(dc->pipe));
2115*459cc2c6SThierry Reding 
2116*459cc2c6SThierry Reding 	tegra_sor_writel(sor, 0x1, SOR_HEAD_STATE5(dc->pipe));
2117*459cc2c6SThierry Reding 
2118*459cc2c6SThierry Reding 	tegra_sor_update(sor);
2119*459cc2c6SThierry Reding 
2120*459cc2c6SThierry Reding 	err = tegra_sor_attach(sor);
2121*459cc2c6SThierry Reding 	if (err < 0)
2122*459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to attach SOR: %d\n", err);
2123*459cc2c6SThierry Reding 
2124*459cc2c6SThierry Reding 	/* enable display to SOR clock and generate HDMI preamble */
2125*459cc2c6SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
2126*459cc2c6SThierry Reding 	value |= SOR1_ENABLE | SOR1_TIMING_CYA;
2127*459cc2c6SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
2128*459cc2c6SThierry Reding 
2129*459cc2c6SThierry Reding 	tegra_dc_commit(dc);
2130*459cc2c6SThierry Reding 
2131*459cc2c6SThierry Reding 	err = tegra_sor_wakeup(sor);
2132*459cc2c6SThierry Reding 	if (err < 0)
2133*459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to wakeup SOR: %d\n", err);
2134*459cc2c6SThierry Reding }
2135*459cc2c6SThierry Reding 
2136*459cc2c6SThierry Reding static const struct drm_encoder_helper_funcs tegra_sor_hdmi_helpers = {
2137*459cc2c6SThierry Reding 	.disable = tegra_sor_hdmi_disable,
2138*459cc2c6SThierry Reding 	.enable = tegra_sor_hdmi_enable,
2139*459cc2c6SThierry Reding 	.atomic_check = tegra_sor_encoder_atomic_check,
2140*459cc2c6SThierry Reding };
2141*459cc2c6SThierry Reding 
21426b6b6042SThierry Reding static int tegra_sor_init(struct host1x_client *client)
21436b6b6042SThierry Reding {
21449910f5c4SThierry Reding 	struct drm_device *drm = dev_get_drvdata(client->parent);
2145*459cc2c6SThierry Reding 	const struct drm_encoder_helper_funcs *helpers = NULL;
21466b6b6042SThierry Reding 	struct tegra_sor *sor = host1x_client_to_sor(client);
2147*459cc2c6SThierry Reding 	int connector = DRM_MODE_CONNECTOR_Unknown;
2148*459cc2c6SThierry Reding 	int encoder = DRM_MODE_ENCODER_NONE;
21496b6b6042SThierry Reding 	int err;
21506b6b6042SThierry Reding 
2151*459cc2c6SThierry Reding 	if (!sor->dpaux) {
2152*459cc2c6SThierry Reding 		if (sor->soc->supports_hdmi) {
2153*459cc2c6SThierry Reding 			connector = DRM_MODE_CONNECTOR_HDMIA;
2154*459cc2c6SThierry Reding 			encoder = DRM_MODE_ENCODER_TMDS;
2155*459cc2c6SThierry Reding 			helpers = &tegra_sor_hdmi_helpers;
2156*459cc2c6SThierry Reding 		} else if (sor->soc->supports_lvds) {
2157*459cc2c6SThierry Reding 			connector = DRM_MODE_CONNECTOR_LVDS;
2158*459cc2c6SThierry Reding 			encoder = DRM_MODE_ENCODER_LVDS;
2159*459cc2c6SThierry Reding 		}
2160*459cc2c6SThierry Reding 	} else {
2161*459cc2c6SThierry Reding 		if (sor->soc->supports_edp) {
2162*459cc2c6SThierry Reding 			connector = DRM_MODE_CONNECTOR_eDP;
2163*459cc2c6SThierry Reding 			encoder = DRM_MODE_ENCODER_TMDS;
2164*459cc2c6SThierry Reding 			helpers = &tegra_sor_edp_helpers;
2165*459cc2c6SThierry Reding 		} else if (sor->soc->supports_dp) {
2166*459cc2c6SThierry Reding 			connector = DRM_MODE_CONNECTOR_DisplayPort;
2167*459cc2c6SThierry Reding 			encoder = DRM_MODE_ENCODER_TMDS;
2168*459cc2c6SThierry Reding 		}
2169*459cc2c6SThierry Reding 	}
21706b6b6042SThierry Reding 
21716b6b6042SThierry Reding 	sor->output.dev = sor->dev;
21726b6b6042SThierry Reding 
21736fad8f66SThierry Reding 	drm_connector_init(drm, &sor->output.connector,
21746fad8f66SThierry Reding 			   &tegra_sor_connector_funcs,
2175*459cc2c6SThierry Reding 			   connector);
21766fad8f66SThierry Reding 	drm_connector_helper_add(&sor->output.connector,
21776fad8f66SThierry Reding 				 &tegra_sor_connector_helper_funcs);
21786fad8f66SThierry Reding 	sor->output.connector.dpms = DRM_MODE_DPMS_OFF;
21796fad8f66SThierry Reding 
21806fad8f66SThierry Reding 	drm_encoder_init(drm, &sor->output.encoder, &tegra_sor_encoder_funcs,
2181*459cc2c6SThierry Reding 			 encoder);
2182*459cc2c6SThierry Reding 	drm_encoder_helper_add(&sor->output.encoder, helpers);
21836fad8f66SThierry Reding 
21846fad8f66SThierry Reding 	drm_mode_connector_attach_encoder(&sor->output.connector,
21856fad8f66SThierry Reding 					  &sor->output.encoder);
21866fad8f66SThierry Reding 	drm_connector_register(&sor->output.connector);
21876fad8f66SThierry Reding 
2188ea130b24SThierry Reding 	err = tegra_output_init(drm, &sor->output);
2189ea130b24SThierry Reding 	if (err < 0) {
2190ea130b24SThierry Reding 		dev_err(client->dev, "failed to initialize output: %d\n", err);
2191ea130b24SThierry Reding 		return err;
2192ea130b24SThierry Reding 	}
21936fad8f66SThierry Reding 
2194ea130b24SThierry Reding 	sor->output.encoder.possible_crtcs = 0x3;
21956b6b6042SThierry Reding 
2196a82752e1SThierry Reding 	if (IS_ENABLED(CONFIG_DEBUG_FS)) {
21971b0c7b48SThierry Reding 		err = tegra_sor_debugfs_init(sor, drm->primary);
2198a82752e1SThierry Reding 		if (err < 0)
2199a82752e1SThierry Reding 			dev_err(sor->dev, "debugfs setup failed: %d\n", err);
2200a82752e1SThierry Reding 	}
2201a82752e1SThierry Reding 
22026b6b6042SThierry Reding 	if (sor->dpaux) {
22036b6b6042SThierry Reding 		err = tegra_dpaux_attach(sor->dpaux, &sor->output);
22046b6b6042SThierry Reding 		if (err < 0) {
22056b6b6042SThierry Reding 			dev_err(sor->dev, "failed to attach DP: %d\n", err);
22066b6b6042SThierry Reding 			return err;
22076b6b6042SThierry Reding 		}
22086b6b6042SThierry Reding 	}
22096b6b6042SThierry Reding 
2210535a65dbSTomeu Vizoso 	/*
2211535a65dbSTomeu Vizoso 	 * XXX: Remove this reset once proper hand-over from firmware to
2212535a65dbSTomeu Vizoso 	 * kernel is possible.
2213535a65dbSTomeu Vizoso 	 */
2214535a65dbSTomeu Vizoso 	err = reset_control_assert(sor->rst);
2215535a65dbSTomeu Vizoso 	if (err < 0) {
2216535a65dbSTomeu Vizoso 		dev_err(sor->dev, "failed to assert SOR reset: %d\n", err);
2217535a65dbSTomeu Vizoso 		return err;
2218535a65dbSTomeu Vizoso 	}
2219535a65dbSTomeu Vizoso 
22206fad8f66SThierry Reding 	err = clk_prepare_enable(sor->clk);
22216fad8f66SThierry Reding 	if (err < 0) {
22226fad8f66SThierry Reding 		dev_err(sor->dev, "failed to enable clock: %d\n", err);
22236fad8f66SThierry Reding 		return err;
22246fad8f66SThierry Reding 	}
22256fad8f66SThierry Reding 
2226535a65dbSTomeu Vizoso 	usleep_range(1000, 3000);
2227535a65dbSTomeu Vizoso 
2228535a65dbSTomeu Vizoso 	err = reset_control_deassert(sor->rst);
2229535a65dbSTomeu Vizoso 	if (err < 0) {
2230535a65dbSTomeu Vizoso 		dev_err(sor->dev, "failed to deassert SOR reset: %d\n", err);
2231535a65dbSTomeu Vizoso 		return err;
2232535a65dbSTomeu Vizoso 	}
2233535a65dbSTomeu Vizoso 
22346fad8f66SThierry Reding 	err = clk_prepare_enable(sor->clk_safe);
22356fad8f66SThierry Reding 	if (err < 0)
22366fad8f66SThierry Reding 		return err;
22376fad8f66SThierry Reding 
22386fad8f66SThierry Reding 	err = clk_prepare_enable(sor->clk_dp);
22396fad8f66SThierry Reding 	if (err < 0)
22406fad8f66SThierry Reding 		return err;
22416fad8f66SThierry Reding 
22426b6b6042SThierry Reding 	return 0;
22436b6b6042SThierry Reding }
22446b6b6042SThierry Reding 
22456b6b6042SThierry Reding static int tegra_sor_exit(struct host1x_client *client)
22466b6b6042SThierry Reding {
22476b6b6042SThierry Reding 	struct tegra_sor *sor = host1x_client_to_sor(client);
22486b6b6042SThierry Reding 	int err;
22496b6b6042SThierry Reding 
2250328ec69eSThierry Reding 	tegra_output_exit(&sor->output);
2251328ec69eSThierry Reding 
22526b6b6042SThierry Reding 	if (sor->dpaux) {
22536b6b6042SThierry Reding 		err = tegra_dpaux_detach(sor->dpaux);
22546b6b6042SThierry Reding 		if (err < 0) {
22556b6b6042SThierry Reding 			dev_err(sor->dev, "failed to detach DP: %d\n", err);
22566b6b6042SThierry Reding 			return err;
22576b6b6042SThierry Reding 		}
22586b6b6042SThierry Reding 	}
22596b6b6042SThierry Reding 
22606fad8f66SThierry Reding 	clk_disable_unprepare(sor->clk_safe);
22616fad8f66SThierry Reding 	clk_disable_unprepare(sor->clk_dp);
22626fad8f66SThierry Reding 	clk_disable_unprepare(sor->clk);
22636fad8f66SThierry Reding 
22644009c224SThierry Reding 	if (IS_ENABLED(CONFIG_DEBUG_FS))
22654009c224SThierry Reding 		tegra_sor_debugfs_exit(sor);
2266a82752e1SThierry Reding 
22676b6b6042SThierry Reding 	return 0;
22686b6b6042SThierry Reding }
22696b6b6042SThierry Reding 
22706b6b6042SThierry Reding static const struct host1x_client_ops sor_client_ops = {
22716b6b6042SThierry Reding 	.init = tegra_sor_init,
22726b6b6042SThierry Reding 	.exit = tegra_sor_exit,
22736b6b6042SThierry Reding };
22746b6b6042SThierry Reding 
2275*459cc2c6SThierry Reding static const struct tegra_sor_ops tegra_sor_edp_ops = {
2276*459cc2c6SThierry Reding 	.name = "eDP",
2277*459cc2c6SThierry Reding };
2278*459cc2c6SThierry Reding 
2279*459cc2c6SThierry Reding static int tegra_sor_hdmi_probe(struct tegra_sor *sor)
2280*459cc2c6SThierry Reding {
2281*459cc2c6SThierry Reding 	int err;
2282*459cc2c6SThierry Reding 
2283*459cc2c6SThierry Reding 	sor->avdd_io_supply = devm_regulator_get(sor->dev, "avdd-io");
2284*459cc2c6SThierry Reding 	if (IS_ERR(sor->avdd_io_supply)) {
2285*459cc2c6SThierry Reding 		dev_err(sor->dev, "cannot get AVDD I/O supply: %ld\n",
2286*459cc2c6SThierry Reding 			PTR_ERR(sor->avdd_io_supply));
2287*459cc2c6SThierry Reding 		return PTR_ERR(sor->avdd_io_supply);
2288*459cc2c6SThierry Reding 	}
2289*459cc2c6SThierry Reding 
2290*459cc2c6SThierry Reding 	err = regulator_enable(sor->avdd_io_supply);
2291*459cc2c6SThierry Reding 	if (err < 0) {
2292*459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to enable AVDD I/O supply: %d\n",
2293*459cc2c6SThierry Reding 			err);
2294*459cc2c6SThierry Reding 		return err;
2295*459cc2c6SThierry Reding 	}
2296*459cc2c6SThierry Reding 
2297*459cc2c6SThierry Reding 	sor->vdd_pll_supply = devm_regulator_get(sor->dev, "vdd-pll");
2298*459cc2c6SThierry Reding 	if (IS_ERR(sor->vdd_pll_supply)) {
2299*459cc2c6SThierry Reding 		dev_err(sor->dev, "cannot get VDD PLL supply: %ld\n",
2300*459cc2c6SThierry Reding 			PTR_ERR(sor->vdd_pll_supply));
2301*459cc2c6SThierry Reding 		return PTR_ERR(sor->vdd_pll_supply);
2302*459cc2c6SThierry Reding 	}
2303*459cc2c6SThierry Reding 
2304*459cc2c6SThierry Reding 	err = regulator_enable(sor->vdd_pll_supply);
2305*459cc2c6SThierry Reding 	if (err < 0) {
2306*459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to enable VDD PLL supply: %d\n",
2307*459cc2c6SThierry Reding 			err);
2308*459cc2c6SThierry Reding 		return err;
2309*459cc2c6SThierry Reding 	}
2310*459cc2c6SThierry Reding 
2311*459cc2c6SThierry Reding 	sor->hdmi_supply = devm_regulator_get(sor->dev, "hdmi");
2312*459cc2c6SThierry Reding 	if (IS_ERR(sor->hdmi_supply)) {
2313*459cc2c6SThierry Reding 		dev_err(sor->dev, "cannot get HDMI supply: %ld\n",
2314*459cc2c6SThierry Reding 			PTR_ERR(sor->hdmi_supply));
2315*459cc2c6SThierry Reding 		return PTR_ERR(sor->hdmi_supply);
2316*459cc2c6SThierry Reding 	}
2317*459cc2c6SThierry Reding 
2318*459cc2c6SThierry Reding 	err = regulator_enable(sor->hdmi_supply);
2319*459cc2c6SThierry Reding 	if (err < 0) {
2320*459cc2c6SThierry Reding 		dev_err(sor->dev, "failed to enable HDMI supply: %d\n", err);
2321*459cc2c6SThierry Reding 		return err;
2322*459cc2c6SThierry Reding 	}
2323*459cc2c6SThierry Reding 
2324*459cc2c6SThierry Reding 	return 0;
2325*459cc2c6SThierry Reding }
2326*459cc2c6SThierry Reding 
2327*459cc2c6SThierry Reding static int tegra_sor_hdmi_remove(struct tegra_sor *sor)
2328*459cc2c6SThierry Reding {
2329*459cc2c6SThierry Reding 	regulator_disable(sor->hdmi_supply);
2330*459cc2c6SThierry Reding 	regulator_disable(sor->vdd_pll_supply);
2331*459cc2c6SThierry Reding 	regulator_disable(sor->avdd_io_supply);
2332*459cc2c6SThierry Reding 
2333*459cc2c6SThierry Reding 	return 0;
2334*459cc2c6SThierry Reding }
2335*459cc2c6SThierry Reding 
2336*459cc2c6SThierry Reding static const struct tegra_sor_ops tegra_sor_hdmi_ops = {
2337*459cc2c6SThierry Reding 	.name = "HDMI",
2338*459cc2c6SThierry Reding 	.probe = tegra_sor_hdmi_probe,
2339*459cc2c6SThierry Reding 	.remove = tegra_sor_hdmi_remove,
2340*459cc2c6SThierry Reding };
2341*459cc2c6SThierry Reding 
2342*459cc2c6SThierry Reding static const struct tegra_sor_soc tegra124_sor = {
2343*459cc2c6SThierry Reding 	.supports_edp = true,
2344*459cc2c6SThierry Reding 	.supports_lvds = true,
2345*459cc2c6SThierry Reding 	.supports_hdmi = false,
2346*459cc2c6SThierry Reding 	.supports_dp = false,
2347*459cc2c6SThierry Reding };
2348*459cc2c6SThierry Reding 
2349*459cc2c6SThierry Reding static const struct tegra_sor_soc tegra210_sor = {
2350*459cc2c6SThierry Reding 	.supports_edp = true,
2351*459cc2c6SThierry Reding 	.supports_lvds = false,
2352*459cc2c6SThierry Reding 	.supports_hdmi = false,
2353*459cc2c6SThierry Reding 	.supports_dp = false,
2354*459cc2c6SThierry Reding };
2355*459cc2c6SThierry Reding 
2356*459cc2c6SThierry Reding static const struct tegra_sor_soc tegra210_sor1 = {
2357*459cc2c6SThierry Reding 	.supports_edp = false,
2358*459cc2c6SThierry Reding 	.supports_lvds = false,
2359*459cc2c6SThierry Reding 	.supports_hdmi = true,
2360*459cc2c6SThierry Reding 	.supports_dp = true,
2361*459cc2c6SThierry Reding 
2362*459cc2c6SThierry Reding 	.num_settings = ARRAY_SIZE(tegra210_sor_hdmi_defaults),
2363*459cc2c6SThierry Reding 	.settings = tegra210_sor_hdmi_defaults,
2364*459cc2c6SThierry Reding };
2365*459cc2c6SThierry Reding 
2366*459cc2c6SThierry Reding static const struct of_device_id tegra_sor_of_match[] = {
2367*459cc2c6SThierry Reding 	{ .compatible = "nvidia,tegra210-sor1", .data = &tegra210_sor1 },
2368*459cc2c6SThierry Reding 	{ .compatible = "nvidia,tegra210-sor", .data = &tegra210_sor },
2369*459cc2c6SThierry Reding 	{ .compatible = "nvidia,tegra124-sor", .data = &tegra124_sor },
2370*459cc2c6SThierry Reding 	{ },
2371*459cc2c6SThierry Reding };
2372*459cc2c6SThierry Reding MODULE_DEVICE_TABLE(of, tegra_sor_of_match);
2373*459cc2c6SThierry Reding 
23746b6b6042SThierry Reding static int tegra_sor_probe(struct platform_device *pdev)
23756b6b6042SThierry Reding {
2376*459cc2c6SThierry Reding 	const struct of_device_id *match;
23776b6b6042SThierry Reding 	struct device_node *np;
23786b6b6042SThierry Reding 	struct tegra_sor *sor;
23796b6b6042SThierry Reding 	struct resource *regs;
23806b6b6042SThierry Reding 	int err;
23816b6b6042SThierry Reding 
2382*459cc2c6SThierry Reding 	match = of_match_device(tegra_sor_of_match, &pdev->dev);
2383*459cc2c6SThierry Reding 
23846b6b6042SThierry Reding 	sor = devm_kzalloc(&pdev->dev, sizeof(*sor), GFP_KERNEL);
23856b6b6042SThierry Reding 	if (!sor)
23866b6b6042SThierry Reding 		return -ENOMEM;
23876b6b6042SThierry Reding 
23886b6b6042SThierry Reding 	sor->output.dev = sor->dev = &pdev->dev;
2389*459cc2c6SThierry Reding 	sor->soc = match->data;
2390*459cc2c6SThierry Reding 
2391*459cc2c6SThierry Reding 	sor->settings = devm_kmemdup(&pdev->dev, sor->soc->settings,
2392*459cc2c6SThierry Reding 				     sor->soc->num_settings *
2393*459cc2c6SThierry Reding 					sizeof(*sor->settings),
2394*459cc2c6SThierry Reding 				     GFP_KERNEL);
2395*459cc2c6SThierry Reding 	if (!sor->settings)
2396*459cc2c6SThierry Reding 		return -ENOMEM;
2397*459cc2c6SThierry Reding 
2398*459cc2c6SThierry Reding 	sor->num_settings = sor->soc->num_settings;
23996b6b6042SThierry Reding 
24006b6b6042SThierry Reding 	np = of_parse_phandle(pdev->dev.of_node, "nvidia,dpaux", 0);
24016b6b6042SThierry Reding 	if (np) {
24026b6b6042SThierry Reding 		sor->dpaux = tegra_dpaux_find_by_of_node(np);
24036b6b6042SThierry Reding 		of_node_put(np);
24046b6b6042SThierry Reding 
24056b6b6042SThierry Reding 		if (!sor->dpaux)
24066b6b6042SThierry Reding 			return -EPROBE_DEFER;
24076b6b6042SThierry Reding 	}
24086b6b6042SThierry Reding 
2409*459cc2c6SThierry Reding 	if (!sor->dpaux) {
2410*459cc2c6SThierry Reding 		if (sor->soc->supports_hdmi) {
2411*459cc2c6SThierry Reding 			sor->ops = &tegra_sor_hdmi_ops;
2412*459cc2c6SThierry Reding 		} else if (sor->soc->supports_lvds) {
2413*459cc2c6SThierry Reding 			dev_err(&pdev->dev, "LVDS not supported yet\n");
2414*459cc2c6SThierry Reding 			return -ENODEV;
2415*459cc2c6SThierry Reding 		} else {
2416*459cc2c6SThierry Reding 			dev_err(&pdev->dev, "unknown (non-DP) support\n");
2417*459cc2c6SThierry Reding 			return -ENODEV;
2418*459cc2c6SThierry Reding 		}
2419*459cc2c6SThierry Reding 	} else {
2420*459cc2c6SThierry Reding 		if (sor->soc->supports_edp) {
2421*459cc2c6SThierry Reding 			sor->ops = &tegra_sor_edp_ops;
2422*459cc2c6SThierry Reding 		} else if (sor->soc->supports_dp) {
2423*459cc2c6SThierry Reding 			dev_err(&pdev->dev, "DisplayPort not supported yet\n");
2424*459cc2c6SThierry Reding 			return -ENODEV;
2425*459cc2c6SThierry Reding 		} else {
2426*459cc2c6SThierry Reding 			dev_err(&pdev->dev, "unknown (DP) support\n");
2427*459cc2c6SThierry Reding 			return -ENODEV;
2428*459cc2c6SThierry Reding 		}
2429*459cc2c6SThierry Reding 	}
2430*459cc2c6SThierry Reding 
24316b6b6042SThierry Reding 	err = tegra_output_probe(&sor->output);
24324dbdc740SThierry Reding 	if (err < 0) {
24334dbdc740SThierry Reding 		dev_err(&pdev->dev, "failed to probe output: %d\n", err);
24346b6b6042SThierry Reding 		return err;
24354dbdc740SThierry Reding 	}
24366b6b6042SThierry Reding 
2437*459cc2c6SThierry Reding 	if (sor->ops && sor->ops->probe) {
2438*459cc2c6SThierry Reding 		err = sor->ops->probe(sor);
2439*459cc2c6SThierry Reding 		if (err < 0) {
2440*459cc2c6SThierry Reding 			dev_err(&pdev->dev, "failed to probe %s: %d\n",
2441*459cc2c6SThierry Reding 				sor->ops->name, err);
2442*459cc2c6SThierry Reding 			goto output;
2443*459cc2c6SThierry Reding 		}
2444*459cc2c6SThierry Reding 	}
2445*459cc2c6SThierry Reding 
24466b6b6042SThierry Reding 	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
24476b6b6042SThierry Reding 	sor->regs = devm_ioremap_resource(&pdev->dev, regs);
2448*459cc2c6SThierry Reding 	if (IS_ERR(sor->regs)) {
2449*459cc2c6SThierry Reding 		err = PTR_ERR(sor->regs);
2450*459cc2c6SThierry Reding 		goto remove;
2451*459cc2c6SThierry Reding 	}
24526b6b6042SThierry Reding 
24536b6b6042SThierry Reding 	sor->rst = devm_reset_control_get(&pdev->dev, "sor");
24544dbdc740SThierry Reding 	if (IS_ERR(sor->rst)) {
2455*459cc2c6SThierry Reding 		err = PTR_ERR(sor->rst);
2456*459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get reset control: %d\n", err);
2457*459cc2c6SThierry Reding 		goto remove;
24584dbdc740SThierry Reding 	}
24596b6b6042SThierry Reding 
24606b6b6042SThierry Reding 	sor->clk = devm_clk_get(&pdev->dev, NULL);
24614dbdc740SThierry Reding 	if (IS_ERR(sor->clk)) {
2462*459cc2c6SThierry Reding 		err = PTR_ERR(sor->clk);
2463*459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get module clock: %d\n", err);
2464*459cc2c6SThierry Reding 		goto remove;
24654dbdc740SThierry Reding 	}
24666b6b6042SThierry Reding 
24676b6b6042SThierry Reding 	sor->clk_parent = devm_clk_get(&pdev->dev, "parent");
24684dbdc740SThierry Reding 	if (IS_ERR(sor->clk_parent)) {
2469*459cc2c6SThierry Reding 		err = PTR_ERR(sor->clk_parent);
2470*459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get parent clock: %d\n", err);
2471*459cc2c6SThierry Reding 		goto remove;
24724dbdc740SThierry Reding 	}
24736b6b6042SThierry Reding 
24746b6b6042SThierry Reding 	sor->clk_safe = devm_clk_get(&pdev->dev, "safe");
24754dbdc740SThierry Reding 	if (IS_ERR(sor->clk_safe)) {
2476*459cc2c6SThierry Reding 		err = PTR_ERR(sor->clk_safe);
2477*459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get safe clock: %d\n", err);
2478*459cc2c6SThierry Reding 		goto remove;
24794dbdc740SThierry Reding 	}
24806b6b6042SThierry Reding 
24816b6b6042SThierry Reding 	sor->clk_dp = devm_clk_get(&pdev->dev, "dp");
24824dbdc740SThierry Reding 	if (IS_ERR(sor->clk_dp)) {
2483*459cc2c6SThierry Reding 		err = PTR_ERR(sor->clk_dp);
2484*459cc2c6SThierry Reding 		dev_err(&pdev->dev, "failed to get DP clock: %d\n", err);
2485*459cc2c6SThierry Reding 		goto remove;
24864dbdc740SThierry Reding 	}
24876b6b6042SThierry Reding 
24886b6b6042SThierry Reding 	INIT_LIST_HEAD(&sor->client.list);
24896b6b6042SThierry Reding 	sor->client.ops = &sor_client_ops;
24906b6b6042SThierry Reding 	sor->client.dev = &pdev->dev;
24916b6b6042SThierry Reding 
24926b6b6042SThierry Reding 	err = host1x_client_register(&sor->client);
24936b6b6042SThierry Reding 	if (err < 0) {
24946b6b6042SThierry Reding 		dev_err(&pdev->dev, "failed to register host1x client: %d\n",
24956b6b6042SThierry Reding 			err);
2496*459cc2c6SThierry Reding 		goto remove;
24976b6b6042SThierry Reding 	}
24986b6b6042SThierry Reding 
24996b6b6042SThierry Reding 	platform_set_drvdata(pdev, sor);
25006b6b6042SThierry Reding 
25016b6b6042SThierry Reding 	return 0;
2502*459cc2c6SThierry Reding 
2503*459cc2c6SThierry Reding remove:
2504*459cc2c6SThierry Reding 	if (sor->ops && sor->ops->remove)
2505*459cc2c6SThierry Reding 		sor->ops->remove(sor);
2506*459cc2c6SThierry Reding output:
2507*459cc2c6SThierry Reding 	tegra_output_remove(&sor->output);
2508*459cc2c6SThierry Reding 	return err;
25096b6b6042SThierry Reding }
25106b6b6042SThierry Reding 
25116b6b6042SThierry Reding static int tegra_sor_remove(struct platform_device *pdev)
25126b6b6042SThierry Reding {
25136b6b6042SThierry Reding 	struct tegra_sor *sor = platform_get_drvdata(pdev);
25146b6b6042SThierry Reding 	int err;
25156b6b6042SThierry Reding 
25166b6b6042SThierry Reding 	err = host1x_client_unregister(&sor->client);
25176b6b6042SThierry Reding 	if (err < 0) {
25186b6b6042SThierry Reding 		dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
25196b6b6042SThierry Reding 			err);
25206b6b6042SThierry Reding 		return err;
25216b6b6042SThierry Reding 	}
25226b6b6042SThierry Reding 
2523*459cc2c6SThierry Reding 	if (sor->ops && sor->ops->remove) {
2524*459cc2c6SThierry Reding 		err = sor->ops->remove(sor);
2525*459cc2c6SThierry Reding 		if (err < 0)
2526*459cc2c6SThierry Reding 			dev_err(&pdev->dev, "failed to remove SOR: %d\n", err);
2527*459cc2c6SThierry Reding 	}
2528*459cc2c6SThierry Reding 
2529328ec69eSThierry Reding 	tegra_output_remove(&sor->output);
25306b6b6042SThierry Reding 
25316b6b6042SThierry Reding 	return 0;
25326b6b6042SThierry Reding }
25336b6b6042SThierry Reding 
25346b6b6042SThierry Reding struct platform_driver tegra_sor_driver = {
25356b6b6042SThierry Reding 	.driver = {
25366b6b6042SThierry Reding 		.name = "tegra-sor",
25376b6b6042SThierry Reding 		.of_match_table = tegra_sor_of_match,
25386b6b6042SThierry Reding 	},
25396b6b6042SThierry Reding 	.probe = tegra_sor_probe,
25406b6b6042SThierry Reding 	.remove = tegra_sor_remove,
25416b6b6042SThierry Reding };
2542