blob: d90bf5f6a67a014957efe8eeb8214bc208c9e025 [file] [log] [blame]
Thierry Reding6b6b6042013-11-15 16:06:05 +01001/*
2 * Copyright (C) 2013 NVIDIA Corporation
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8
9#include <linux/clk.h>
Thierry Redingb2992212015-10-01 14:25:03 +020010#include <linux/clk-provider.h>
Thierry Redinga82752e2014-01-31 10:02:15 +010011#include <linux/debugfs.h>
Thierry Reding6fad8f62014-11-28 15:41:34 +010012#include <linux/gpio.h>
Thierry Reding6b6b6042013-11-15 16:06:05 +010013#include <linux/io.h>
Thierry Reding459cc2c2015-07-30 10:34:24 +020014#include <linux/of_device.h>
Thierry Reding6b6b6042013-11-15 16:06:05 +010015#include <linux/platform_device.h>
Thierry Redingaaff8bd2015-08-07 16:04:54 +020016#include <linux/pm_runtime.h>
Thierry Reding459cc2c2015-07-30 10:34:24 +020017#include <linux/regulator/consumer.h>
Thierry Reding6b6b6042013-11-15 16:06:05 +010018#include <linux/reset.h>
Thierry Reding306a7f92014-07-17 13:17:24 +020019
Thierry Reding72323982014-07-11 13:19:06 +020020#include <soc/tegra/pmc.h>
Thierry Reding6b6b6042013-11-15 16:06:05 +010021
Thierry Reding4aa3df72014-11-24 16:27:13 +010022#include <drm/drm_atomic_helper.h>
Thierry Reding6b6b6042013-11-15 16:06:05 +010023#include <drm/drm_dp_helper.h>
Thierry Reding6fad8f62014-11-28 15:41:34 +010024#include <drm/drm_panel.h>
Thierry Reding36e90222017-10-12 19:14:21 +020025#include <drm/drm_scdc_helper.h>
Thierry Reding6b6b6042013-11-15 16:06:05 +010026
27#include "dc.h"
28#include "drm.h"
29#include "sor.h"
Thierry Reding932f6522017-08-15 15:41:14 +020030#include "trace.h"
Thierry Reding6b6b6042013-11-15 16:06:05 +010031
Thierry Redingc57997b2017-10-12 19:12:57 +020032/*
33 * XXX Remove this after the commit adding it to soc/tegra/pmc.h has been
34 * merged. Having this around after the commit is merged should be safe since
35 * the preprocessor will effectively replace all occurrences and therefore no
36 * duplicate will be defined.
37 */
38#define TEGRA_IO_PAD_HDMI_DP0 26
39
Thierry Reding459cc2c2015-07-30 10:34:24 +020040#define SOR_REKEY 0x38
41
42struct tegra_sor_hdmi_settings {
43 unsigned long frequency;
44
45 u8 vcocap;
Thierry Redingc57997b2017-10-12 19:12:57 +020046 u8 filter;
Thierry Reding459cc2c2015-07-30 10:34:24 +020047 u8 ichpmp;
48 u8 loadadj;
Thierry Redingc57997b2017-10-12 19:12:57 +020049 u8 tmds_termadj;
50 u8 tx_pu_value;
51 u8 bg_temp_coef;
52 u8 bg_vref_level;
53 u8 avdd10_level;
54 u8 avdd14_level;
55 u8 sparepll;
Thierry Reding459cc2c2015-07-30 10:34:24 +020056
57 u8 drive_current[4];
58 u8 preemphasis[4];
59};
60
61#if 1
62static const struct tegra_sor_hdmi_settings tegra210_sor_hdmi_defaults[] = {
63 {
64 .frequency = 54000000,
65 .vcocap = 0x0,
Thierry Redingc57997b2017-10-12 19:12:57 +020066 .filter = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +020067 .ichpmp = 0x1,
68 .loadadj = 0x3,
Thierry Redingc57997b2017-10-12 19:12:57 +020069 .tmds_termadj = 0x9,
70 .tx_pu_value = 0x10,
71 .bg_temp_coef = 0x3,
72 .bg_vref_level = 0x8,
73 .avdd10_level = 0x4,
74 .avdd14_level = 0x4,
75 .sparepll = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +020076 .drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
77 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
78 }, {
79 .frequency = 75000000,
80 .vcocap = 0x3,
Thierry Redingc57997b2017-10-12 19:12:57 +020081 .filter = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +020082 .ichpmp = 0x1,
83 .loadadj = 0x3,
Thierry Redingc57997b2017-10-12 19:12:57 +020084 .tmds_termadj = 0x9,
85 .tx_pu_value = 0x40,
86 .bg_temp_coef = 0x3,
87 .bg_vref_level = 0x8,
88 .avdd10_level = 0x4,
89 .avdd14_level = 0x4,
90 .sparepll = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +020091 .drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
92 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
93 }, {
94 .frequency = 150000000,
95 .vcocap = 0x3,
Thierry Redingc57997b2017-10-12 19:12:57 +020096 .filter = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +020097 .ichpmp = 0x1,
98 .loadadj = 0x3,
Thierry Redingc57997b2017-10-12 19:12:57 +020099 .tmds_termadj = 0x9,
100 .tx_pu_value = 0x66,
101 .bg_temp_coef = 0x3,
102 .bg_vref_level = 0x8,
103 .avdd10_level = 0x4,
104 .avdd14_level = 0x4,
105 .sparepll = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +0200106 .drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
107 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
108 }, {
109 .frequency = 300000000,
110 .vcocap = 0x3,
Thierry Redingc57997b2017-10-12 19:12:57 +0200111 .filter = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +0200112 .ichpmp = 0x1,
113 .loadadj = 0x3,
Thierry Redingc57997b2017-10-12 19:12:57 +0200114 .tmds_termadj = 0x9,
115 .tx_pu_value = 0x66,
116 .bg_temp_coef = 0x3,
117 .bg_vref_level = 0xa,
118 .avdd10_level = 0x4,
119 .avdd14_level = 0x4,
120 .sparepll = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +0200121 .drive_current = { 0x33, 0x3f, 0x3f, 0x3f },
122 .preemphasis = { 0x00, 0x17, 0x17, 0x17 },
123 }, {
124 .frequency = 600000000,
125 .vcocap = 0x3,
Thierry Redingc57997b2017-10-12 19:12:57 +0200126 .filter = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +0200127 .ichpmp = 0x1,
128 .loadadj = 0x3,
Thierry Redingc57997b2017-10-12 19:12:57 +0200129 .tmds_termadj = 0x9,
130 .tx_pu_value = 0x66,
131 .bg_temp_coef = 0x3,
132 .bg_vref_level = 0x8,
133 .avdd10_level = 0x4,
134 .avdd14_level = 0x4,
135 .sparepll = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +0200136 .drive_current = { 0x33, 0x3f, 0x3f, 0x3f },
137 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
138 },
139};
140#else
141static const struct tegra_sor_hdmi_settings tegra210_sor_hdmi_defaults[] = {
142 {
143 .frequency = 75000000,
144 .vcocap = 0x3,
Thierry Redingc57997b2017-10-12 19:12:57 +0200145 .filter = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +0200146 .ichpmp = 0x1,
147 .loadadj = 0x3,
Thierry Redingc57997b2017-10-12 19:12:57 +0200148 .tmds_termadj = 0x9,
149 .tx_pu_value = 0x40,
150 .bg_temp_coef = 0x3,
151 .bg_vref_level = 0x8,
152 .avdd10_level = 0x4,
153 .avdd14_level = 0x4,
154 .sparepll = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +0200155 .drive_current = { 0x29, 0x29, 0x29, 0x29 },
156 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
157 }, {
158 .frequency = 150000000,
159 .vcocap = 0x3,
Thierry Redingc57997b2017-10-12 19:12:57 +0200160 .filter = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +0200161 .ichpmp = 0x1,
162 .loadadj = 0x3,
Thierry Redingc57997b2017-10-12 19:12:57 +0200163 .tmds_termadj = 0x9,
164 .tx_pu_value = 0x66,
165 .bg_temp_coef = 0x3,
166 .bg_vref_level = 0x8,
167 .avdd10_level = 0x4,
168 .avdd14_level = 0x4,
169 .sparepll = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +0200170 .drive_current = { 0x30, 0x37, 0x37, 0x37 },
171 .preemphasis = { 0x01, 0x02, 0x02, 0x02 },
172 }, {
173 .frequency = 300000000,
174 .vcocap = 0x3,
Thierry Redingc57997b2017-10-12 19:12:57 +0200175 .filter = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +0200176 .ichpmp = 0x6,
177 .loadadj = 0x3,
Thierry Redingc57997b2017-10-12 19:12:57 +0200178 .tmds_termadj = 0x9,
179 .tx_pu_value = 0x66,
180 .bg_temp_coef = 0x3,
181 .bg_vref_level = 0xf,
182 .avdd10_level = 0x4,
183 .avdd14_level = 0x4,
184 .sparepll = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +0200185 .drive_current = { 0x30, 0x37, 0x37, 0x37 },
186 .preemphasis = { 0x10, 0x3e, 0x3e, 0x3e },
187 }, {
188 .frequency = 600000000,
189 .vcocap = 0x3,
Thierry Redingc57997b2017-10-12 19:12:57 +0200190 .filter = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +0200191 .ichpmp = 0xa,
192 .loadadj = 0x3,
Thierry Redingc57997b2017-10-12 19:12:57 +0200193 .tmds_termadj = 0xb,
194 .tx_pu_value = 0x66,
195 .bg_temp_coef = 0x3,
196 .bg_vref_level = 0xe,
197 .avdd10_level = 0x4,
198 .avdd14_level = 0x4,
199 .sparepll = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +0200200 .drive_current = { 0x35, 0x3e, 0x3e, 0x3e },
201 .preemphasis = { 0x02, 0x3f, 0x3f, 0x3f },
202 },
203};
204#endif
205
Thierry Redingc57997b2017-10-12 19:12:57 +0200206static const struct tegra_sor_hdmi_settings tegra186_sor_hdmi_defaults[] = {
207 {
208 .frequency = 54000000,
209 .vcocap = 0,
210 .filter = 5,
211 .ichpmp = 5,
212 .loadadj = 3,
213 .tmds_termadj = 0xf,
214 .tx_pu_value = 0,
215 .bg_temp_coef = 3,
216 .bg_vref_level = 8,
217 .avdd10_level = 4,
218 .avdd14_level = 4,
219 .sparepll = 0x54,
220 .drive_current = { 0x3a, 0x3a, 0x3a, 0x33 },
221 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
222 }, {
223 .frequency = 75000000,
224 .vcocap = 1,
225 .filter = 5,
226 .ichpmp = 5,
227 .loadadj = 3,
228 .tmds_termadj = 0xf,
229 .tx_pu_value = 0,
230 .bg_temp_coef = 3,
231 .bg_vref_level = 8,
232 .avdd10_level = 4,
233 .avdd14_level = 4,
234 .sparepll = 0x44,
235 .drive_current = { 0x3a, 0x3a, 0x3a, 0x33 },
236 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
237 }, {
238 .frequency = 150000000,
239 .vcocap = 3,
240 .filter = 5,
241 .ichpmp = 5,
242 .loadadj = 3,
243 .tmds_termadj = 15,
244 .tx_pu_value = 0x66 /* 0 */,
245 .bg_temp_coef = 3,
246 .bg_vref_level = 8,
247 .avdd10_level = 4,
248 .avdd14_level = 4,
249 .sparepll = 0x00, /* 0x34 */
250 .drive_current = { 0x3a, 0x3a, 0x3a, 0x37 },
251 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
252 }, {
253 .frequency = 300000000,
254 .vcocap = 3,
255 .filter = 5,
256 .ichpmp = 5,
257 .loadadj = 3,
258 .tmds_termadj = 15,
259 .tx_pu_value = 64,
260 .bg_temp_coef = 3,
261 .bg_vref_level = 8,
262 .avdd10_level = 4,
263 .avdd14_level = 4,
264 .sparepll = 0x34,
265 .drive_current = { 0x3d, 0x3d, 0x3d, 0x33 },
266 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
267 }, {
268 .frequency = 600000000,
269 .vcocap = 3,
270 .filter = 5,
271 .ichpmp = 5,
272 .loadadj = 3,
273 .tmds_termadj = 12,
274 .tx_pu_value = 96,
275 .bg_temp_coef = 3,
276 .bg_vref_level = 8,
277 .avdd10_level = 4,
278 .avdd14_level = 4,
279 .sparepll = 0x34,
280 .drive_current = { 0x3d, 0x3d, 0x3d, 0x33 },
281 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
282 }
283};
284
Thierry Reding9b6c14b2018-09-21 12:27:46 +0200285static const struct tegra_sor_hdmi_settings tegra194_sor_hdmi_defaults[] = {
286 {
287 .frequency = 54000000,
288 .vcocap = 0,
289 .filter = 5,
290 .ichpmp = 5,
291 .loadadj = 3,
292 .tmds_termadj = 0xf,
293 .tx_pu_value = 0,
294 .bg_temp_coef = 3,
295 .bg_vref_level = 8,
296 .avdd10_level = 4,
297 .avdd14_level = 4,
298 .sparepll = 0x54,
299 .drive_current = { 0x3a, 0x3a, 0x3a, 0x33 },
300 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
301 }, {
302 .frequency = 75000000,
303 .vcocap = 1,
304 .filter = 5,
305 .ichpmp = 5,
306 .loadadj = 3,
307 .tmds_termadj = 0xf,
308 .tx_pu_value = 0,
309 .bg_temp_coef = 3,
310 .bg_vref_level = 8,
311 .avdd10_level = 4,
312 .avdd14_level = 4,
313 .sparepll = 0x44,
314 .drive_current = { 0x3a, 0x3a, 0x3a, 0x33 },
315 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
316 }, {
317 .frequency = 150000000,
318 .vcocap = 3,
319 .filter = 5,
320 .ichpmp = 5,
321 .loadadj = 3,
322 .tmds_termadj = 15,
323 .tx_pu_value = 0x66 /* 0 */,
324 .bg_temp_coef = 3,
325 .bg_vref_level = 8,
326 .avdd10_level = 4,
327 .avdd14_level = 4,
328 .sparepll = 0x00, /* 0x34 */
329 .drive_current = { 0x3a, 0x3a, 0x3a, 0x37 },
330 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
331 }, {
332 .frequency = 300000000,
333 .vcocap = 3,
334 .filter = 5,
335 .ichpmp = 5,
336 .loadadj = 3,
337 .tmds_termadj = 15,
338 .tx_pu_value = 64,
339 .bg_temp_coef = 3,
340 .bg_vref_level = 8,
341 .avdd10_level = 4,
342 .avdd14_level = 4,
343 .sparepll = 0x34,
344 .drive_current = { 0x3d, 0x3d, 0x3d, 0x33 },
345 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
346 }, {
347 .frequency = 600000000,
348 .vcocap = 3,
349 .filter = 5,
350 .ichpmp = 5,
351 .loadadj = 3,
352 .tmds_termadj = 12,
353 .tx_pu_value = 96,
354 .bg_temp_coef = 3,
355 .bg_vref_level = 8,
356 .avdd10_level = 4,
357 .avdd14_level = 4,
358 .sparepll = 0x34,
359 .drive_current = { 0x3d, 0x3d, 0x3d, 0x33 },
360 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
361 }
362};
363
Thierry Reding880cee02017-10-12 19:04:17 +0200364struct tegra_sor_regs {
365 unsigned int head_state0;
366 unsigned int head_state1;
367 unsigned int head_state2;
368 unsigned int head_state3;
369 unsigned int head_state4;
370 unsigned int head_state5;
371 unsigned int pll0;
372 unsigned int pll1;
373 unsigned int pll2;
374 unsigned int pll3;
375 unsigned int dp_padctl0;
376 unsigned int dp_padctl2;
377};
378
Thierry Reding459cc2c2015-07-30 10:34:24 +0200379struct tegra_sor_soc {
380 bool supports_edp;
381 bool supports_lvds;
382 bool supports_hdmi;
383 bool supports_dp;
384
Thierry Reding880cee02017-10-12 19:04:17 +0200385 const struct tegra_sor_regs *regs;
Thierry Redingc57997b2017-10-12 19:12:57 +0200386 bool has_nvdisplay;
Thierry Reding880cee02017-10-12 19:04:17 +0200387
Thierry Reding459cc2c2015-07-30 10:34:24 +0200388 const struct tegra_sor_hdmi_settings *settings;
389 unsigned int num_settings;
Thierry Reding30b49432015-08-03 15:50:32 +0200390
391 const u8 *xbar_cfg;
Thierry Reding459cc2c2015-07-30 10:34:24 +0200392};
393
394struct tegra_sor;
395
396struct tegra_sor_ops {
397 const char *name;
398 int (*probe)(struct tegra_sor *sor);
399 int (*remove)(struct tegra_sor *sor);
400};
401
Thierry Reding6b6b6042013-11-15 16:06:05 +0100402struct tegra_sor {
403 struct host1x_client client;
404 struct tegra_output output;
405 struct device *dev;
406
Thierry Reding459cc2c2015-07-30 10:34:24 +0200407 const struct tegra_sor_soc *soc;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100408 void __iomem *regs;
Thierry Redingc57997b2017-10-12 19:12:57 +0200409 unsigned int index;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100410
411 struct reset_control *rst;
412 struct clk *clk_parent;
413 struct clk *clk_safe;
Thierry Redinge1335e22017-10-12 17:53:11 +0200414 struct clk *clk_out;
415 struct clk *clk_pad;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100416 struct clk *clk_dp;
417 struct clk *clk;
418
Thierry Reding9542c232015-07-08 13:39:09 +0200419 struct drm_dp_aux *aux;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100420
Thierry Redingdab16332015-01-26 16:04:08 +0100421 struct drm_info_list *debugfs_files;
Thierry Reding459cc2c2015-07-30 10:34:24 +0200422
423 const struct tegra_sor_ops *ops;
Thierry Redingc57997b2017-10-12 19:12:57 +0200424 enum tegra_io_pad pad;
Thierry Reding459cc2c2015-07-30 10:34:24 +0200425
426 /* for HDMI 2.0 */
427 struct tegra_sor_hdmi_settings *settings;
428 unsigned int num_settings;
429
430 struct regulator *avdd_io_supply;
431 struct regulator *vdd_pll_supply;
432 struct regulator *hdmi_supply;
Thierry Reding36e90222017-10-12 19:14:21 +0200433
434 struct delayed_work scdc;
435 bool scdc_enabled;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100436};
437
Thierry Redingc31efa72015-09-08 16:09:22 +0200438struct tegra_sor_state {
439 struct drm_connector_state base;
440
Thierry Reding36e90222017-10-12 19:14:21 +0200441 unsigned int link_speed;
442 unsigned long pclk;
Thierry Redingc31efa72015-09-08 16:09:22 +0200443 unsigned int bpc;
444};
445
446static inline struct tegra_sor_state *
447to_sor_state(struct drm_connector_state *state)
448{
449 return container_of(state, struct tegra_sor_state, base);
450}
451
Thierry Reding34fa1832014-06-05 16:31:10 +0200452struct tegra_sor_config {
453 u32 bits_per_pixel;
454
455 u32 active_polarity;
456 u32 active_count;
457 u32 tu_size;
458 u32 active_frac;
459 u32 watermark;
Thierry Reding7890b572014-06-05 16:12:46 +0200460
461 u32 hblank_symbols;
462 u32 vblank_symbols;
Thierry Reding34fa1832014-06-05 16:31:10 +0200463};
464
Thierry Reding6b6b6042013-11-15 16:06:05 +0100465static inline struct tegra_sor *
466host1x_client_to_sor(struct host1x_client *client)
467{
468 return container_of(client, struct tegra_sor, client);
469}
470
471static inline struct tegra_sor *to_sor(struct tegra_output *output)
472{
473 return container_of(output, struct tegra_sor, output);
474}
475
Thierry Reding5c5f1302017-08-15 15:41:09 +0200476static inline u32 tegra_sor_readl(struct tegra_sor *sor, unsigned int offset)
Thierry Reding6b6b6042013-11-15 16:06:05 +0100477{
Thierry Reding932f6522017-08-15 15:41:14 +0200478 u32 value = readl(sor->regs + (offset << 2));
479
480 trace_sor_readl(sor->dev, offset, value);
481
482 return value;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100483}
484
Thierry Reding28fe2072015-01-26 16:02:48 +0100485static inline void tegra_sor_writel(struct tegra_sor *sor, u32 value,
Thierry Reding5c5f1302017-08-15 15:41:09 +0200486 unsigned int offset)
Thierry Reding6b6b6042013-11-15 16:06:05 +0100487{
Thierry Reding932f6522017-08-15 15:41:14 +0200488 trace_sor_writel(sor->dev, offset, value);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100489 writel(value, sor->regs + (offset << 2));
490}
491
Thierry Reding25bb2ce2015-08-03 14:23:29 +0200492static int tegra_sor_set_parent_clock(struct tegra_sor *sor, struct clk *parent)
493{
494 int err;
495
496 clk_disable_unprepare(sor->clk);
497
Thierry Redinge1335e22017-10-12 17:53:11 +0200498 err = clk_set_parent(sor->clk_out, parent);
Thierry Reding25bb2ce2015-08-03 14:23:29 +0200499 if (err < 0)
500 return err;
501
502 err = clk_prepare_enable(sor->clk);
503 if (err < 0)
504 return err;
505
506 return 0;
507}
508
Thierry Redinge1335e22017-10-12 17:53:11 +0200509struct tegra_clk_sor_pad {
Thierry Redingb2992212015-10-01 14:25:03 +0200510 struct clk_hw hw;
511 struct tegra_sor *sor;
512};
513
Thierry Redinge1335e22017-10-12 17:53:11 +0200514static inline struct tegra_clk_sor_pad *to_pad(struct clk_hw *hw)
Thierry Redingb2992212015-10-01 14:25:03 +0200515{
Thierry Redinge1335e22017-10-12 17:53:11 +0200516 return container_of(hw, struct tegra_clk_sor_pad, hw);
Thierry Redingb2992212015-10-01 14:25:03 +0200517}
518
Thierry Redinge1335e22017-10-12 17:53:11 +0200519static const char * const tegra_clk_sor_pad_parents[] = {
Thierry Redingb2992212015-10-01 14:25:03 +0200520 "pll_d2_out0", "pll_dp"
521};
522
Thierry Redinge1335e22017-10-12 17:53:11 +0200523static int tegra_clk_sor_pad_set_parent(struct clk_hw *hw, u8 index)
Thierry Redingb2992212015-10-01 14:25:03 +0200524{
Thierry Redinge1335e22017-10-12 17:53:11 +0200525 struct tegra_clk_sor_pad *pad = to_pad(hw);
526 struct tegra_sor *sor = pad->sor;
Thierry Redingb2992212015-10-01 14:25:03 +0200527 u32 value;
528
529 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
530 value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
531
532 switch (index) {
533 case 0:
534 value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK;
535 break;
536
537 case 1:
538 value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
539 break;
540 }
541
542 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
543
544 return 0;
545}
546
Thierry Redinge1335e22017-10-12 17:53:11 +0200547static u8 tegra_clk_sor_pad_get_parent(struct clk_hw *hw)
Thierry Redingb2992212015-10-01 14:25:03 +0200548{
Thierry Redinge1335e22017-10-12 17:53:11 +0200549 struct tegra_clk_sor_pad *pad = to_pad(hw);
550 struct tegra_sor *sor = pad->sor;
Thierry Redingb2992212015-10-01 14:25:03 +0200551 u8 parent = U8_MAX;
552 u32 value;
553
554 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
555
556 switch (value & SOR_CLK_CNTRL_DP_CLK_SEL_MASK) {
557 case SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK:
558 case SOR_CLK_CNTRL_DP_CLK_SEL_DIFF_PCLK:
559 parent = 0;
560 break;
561
562 case SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK:
563 case SOR_CLK_CNTRL_DP_CLK_SEL_DIFF_DPCLK:
564 parent = 1;
565 break;
566 }
567
568 return parent;
569}
570
Thierry Redinge1335e22017-10-12 17:53:11 +0200571static const struct clk_ops tegra_clk_sor_pad_ops = {
572 .set_parent = tegra_clk_sor_pad_set_parent,
573 .get_parent = tegra_clk_sor_pad_get_parent,
Thierry Redingb2992212015-10-01 14:25:03 +0200574};
575
Thierry Redinge1335e22017-10-12 17:53:11 +0200576static struct clk *tegra_clk_sor_pad_register(struct tegra_sor *sor,
577 const char *name)
Thierry Redingb2992212015-10-01 14:25:03 +0200578{
Thierry Redinge1335e22017-10-12 17:53:11 +0200579 struct tegra_clk_sor_pad *pad;
Thierry Redingb2992212015-10-01 14:25:03 +0200580 struct clk_init_data init;
581 struct clk *clk;
582
Thierry Redinge1335e22017-10-12 17:53:11 +0200583 pad = devm_kzalloc(sor->dev, sizeof(*pad), GFP_KERNEL);
584 if (!pad)
Thierry Redingb2992212015-10-01 14:25:03 +0200585 return ERR_PTR(-ENOMEM);
586
Thierry Redinge1335e22017-10-12 17:53:11 +0200587 pad->sor = sor;
Thierry Redingb2992212015-10-01 14:25:03 +0200588
589 init.name = name;
590 init.flags = 0;
Thierry Redinge1335e22017-10-12 17:53:11 +0200591 init.parent_names = tegra_clk_sor_pad_parents;
592 init.num_parents = ARRAY_SIZE(tegra_clk_sor_pad_parents);
593 init.ops = &tegra_clk_sor_pad_ops;
Thierry Redingb2992212015-10-01 14:25:03 +0200594
Thierry Redinge1335e22017-10-12 17:53:11 +0200595 pad->hw.init = &init;
Thierry Redingb2992212015-10-01 14:25:03 +0200596
Thierry Redinge1335e22017-10-12 17:53:11 +0200597 clk = devm_clk_register(sor->dev, &pad->hw);
Thierry Redingb2992212015-10-01 14:25:03 +0200598
599 return clk;
600}
601
Thierry Reding6b6b6042013-11-15 16:06:05 +0100602static int tegra_sor_dp_train_fast(struct tegra_sor *sor,
603 struct drm_dp_link *link)
604{
Thierry Reding6b6b6042013-11-15 16:06:05 +0100605 unsigned int i;
606 u8 pattern;
Thierry Reding28fe2072015-01-26 16:02:48 +0100607 u32 value;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100608 int err;
609
610 /* setup lane parameters */
611 value = SOR_LANE_DRIVE_CURRENT_LANE3(0x40) |
612 SOR_LANE_DRIVE_CURRENT_LANE2(0x40) |
613 SOR_LANE_DRIVE_CURRENT_LANE1(0x40) |
614 SOR_LANE_DRIVE_CURRENT_LANE0(0x40);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200615 tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100616
617 value = SOR_LANE_PREEMPHASIS_LANE3(0x0f) |
618 SOR_LANE_PREEMPHASIS_LANE2(0x0f) |
619 SOR_LANE_PREEMPHASIS_LANE1(0x0f) |
620 SOR_LANE_PREEMPHASIS_LANE0(0x0f);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200621 tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100622
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200623 value = SOR_LANE_POSTCURSOR_LANE3(0x00) |
624 SOR_LANE_POSTCURSOR_LANE2(0x00) |
625 SOR_LANE_POSTCURSOR_LANE1(0x00) |
626 SOR_LANE_POSTCURSOR_LANE0(0x00);
627 tegra_sor_writel(sor, value, SOR_LANE_POSTCURSOR0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100628
629 /* disable LVDS mode */
630 tegra_sor_writel(sor, 0, SOR_LVDS);
631
Thierry Reding880cee02017-10-12 19:04:17 +0200632 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100633 value |= SOR_DP_PADCTL_TX_PU_ENABLE;
634 value &= ~SOR_DP_PADCTL_TX_PU_MASK;
635 value |= SOR_DP_PADCTL_TX_PU(2); /* XXX: don't hardcode? */
Thierry Reding880cee02017-10-12 19:04:17 +0200636 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100637
Thierry Reding880cee02017-10-12 19:04:17 +0200638 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100639 value |= SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
640 SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0;
Thierry Reding880cee02017-10-12 19:04:17 +0200641 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100642
643 usleep_range(10, 100);
644
Thierry Reding880cee02017-10-12 19:04:17 +0200645 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100646 value &= ~(SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
647 SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0);
Thierry Reding880cee02017-10-12 19:04:17 +0200648 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100649
Thierry Reding9542c232015-07-08 13:39:09 +0200650 err = drm_dp_aux_prepare(sor->aux, DP_SET_ANSI_8B10B);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100651 if (err < 0)
652 return err;
653
654 for (i = 0, value = 0; i < link->num_lanes; i++) {
655 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
656 SOR_DP_TPG_SCRAMBLER_NONE |
657 SOR_DP_TPG_PATTERN_TRAIN1;
658 value = (value << 8) | lane;
659 }
660
661 tegra_sor_writel(sor, value, SOR_DP_TPG);
662
663 pattern = DP_TRAINING_PATTERN_1;
664
Thierry Reding9542c232015-07-08 13:39:09 +0200665 err = drm_dp_aux_train(sor->aux, link, pattern);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100666 if (err < 0)
667 return err;
668
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200669 value = tegra_sor_readl(sor, SOR_DP_SPARE0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100670 value |= SOR_DP_SPARE_SEQ_ENABLE;
671 value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
672 value |= SOR_DP_SPARE_MACRO_SOR_CLK;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200673 tegra_sor_writel(sor, value, SOR_DP_SPARE0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100674
675 for (i = 0, value = 0; i < link->num_lanes; i++) {
676 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
677 SOR_DP_TPG_SCRAMBLER_NONE |
678 SOR_DP_TPG_PATTERN_TRAIN2;
679 value = (value << 8) | lane;
680 }
681
682 tegra_sor_writel(sor, value, SOR_DP_TPG);
683
684 pattern = DP_LINK_SCRAMBLING_DISABLE | DP_TRAINING_PATTERN_2;
685
Thierry Reding9542c232015-07-08 13:39:09 +0200686 err = drm_dp_aux_train(sor->aux, link, pattern);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100687 if (err < 0)
688 return err;
689
690 for (i = 0, value = 0; i < link->num_lanes; i++) {
691 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
692 SOR_DP_TPG_SCRAMBLER_GALIOS |
693 SOR_DP_TPG_PATTERN_NONE;
694 value = (value << 8) | lane;
695 }
696
697 tegra_sor_writel(sor, value, SOR_DP_TPG);
698
699 pattern = DP_TRAINING_PATTERN_DISABLE;
700
Thierry Reding9542c232015-07-08 13:39:09 +0200701 err = drm_dp_aux_train(sor->aux, link, pattern);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100702 if (err < 0)
703 return err;
704
705 return 0;
706}
707
708static void tegra_sor_super_update(struct tegra_sor *sor)
709{
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200710 tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
711 tegra_sor_writel(sor, 1, SOR_SUPER_STATE0);
712 tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100713}
714
715static void tegra_sor_update(struct tegra_sor *sor)
716{
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200717 tegra_sor_writel(sor, 0, SOR_STATE0);
718 tegra_sor_writel(sor, 1, SOR_STATE0);
719 tegra_sor_writel(sor, 0, SOR_STATE0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100720}
721
722static int tegra_sor_setup_pwm(struct tegra_sor *sor, unsigned long timeout)
723{
Thierry Reding28fe2072015-01-26 16:02:48 +0100724 u32 value;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100725
726 value = tegra_sor_readl(sor, SOR_PWM_DIV);
727 value &= ~SOR_PWM_DIV_MASK;
728 value |= 0x400; /* period */
729 tegra_sor_writel(sor, value, SOR_PWM_DIV);
730
731 value = tegra_sor_readl(sor, SOR_PWM_CTL);
732 value &= ~SOR_PWM_CTL_DUTY_CYCLE_MASK;
733 value |= 0x400; /* duty cycle */
734 value &= ~SOR_PWM_CTL_CLK_SEL; /* clock source: PCLK */
735 value |= SOR_PWM_CTL_TRIGGER;
736 tegra_sor_writel(sor, value, SOR_PWM_CTL);
737
738 timeout = jiffies + msecs_to_jiffies(timeout);
739
740 while (time_before(jiffies, timeout)) {
741 value = tegra_sor_readl(sor, SOR_PWM_CTL);
742 if ((value & SOR_PWM_CTL_TRIGGER) == 0)
743 return 0;
744
745 usleep_range(25, 100);
746 }
747
748 return -ETIMEDOUT;
749}
750
751static int tegra_sor_attach(struct tegra_sor *sor)
752{
753 unsigned long value, timeout;
754
755 /* wake up in normal mode */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200756 value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100757 value |= SOR_SUPER_STATE_HEAD_MODE_AWAKE;
758 value |= SOR_SUPER_STATE_MODE_NORMAL;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200759 tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100760 tegra_sor_super_update(sor);
761
762 /* attach */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200763 value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100764 value |= SOR_SUPER_STATE_ATTACHED;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200765 tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100766 tegra_sor_super_update(sor);
767
768 timeout = jiffies + msecs_to_jiffies(250);
769
770 while (time_before(jiffies, timeout)) {
771 value = tegra_sor_readl(sor, SOR_TEST);
772 if ((value & SOR_TEST_ATTACHED) != 0)
773 return 0;
774
775 usleep_range(25, 100);
776 }
777
778 return -ETIMEDOUT;
779}
780
781static int tegra_sor_wakeup(struct tegra_sor *sor)
782{
Thierry Reding6b6b6042013-11-15 16:06:05 +0100783 unsigned long value, timeout;
784
Thierry Reding6b6b6042013-11-15 16:06:05 +0100785 timeout = jiffies + msecs_to_jiffies(250);
786
787 /* wait for head to wake up */
788 while (time_before(jiffies, timeout)) {
789 value = tegra_sor_readl(sor, SOR_TEST);
790 value &= SOR_TEST_HEAD_MODE_MASK;
791
792 if (value == SOR_TEST_HEAD_MODE_AWAKE)
793 return 0;
794
795 usleep_range(25, 100);
796 }
797
798 return -ETIMEDOUT;
799}
800
801static int tegra_sor_power_up(struct tegra_sor *sor, unsigned long timeout)
802{
Thierry Reding28fe2072015-01-26 16:02:48 +0100803 u32 value;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100804
805 value = tegra_sor_readl(sor, SOR_PWR);
806 value |= SOR_PWR_TRIGGER | SOR_PWR_NORMAL_STATE_PU;
807 tegra_sor_writel(sor, value, SOR_PWR);
808
809 timeout = jiffies + msecs_to_jiffies(timeout);
810
811 while (time_before(jiffies, timeout)) {
812 value = tegra_sor_readl(sor, SOR_PWR);
813 if ((value & SOR_PWR_TRIGGER) == 0)
814 return 0;
815
816 usleep_range(25, 100);
817 }
818
819 return -ETIMEDOUT;
820}
821
Thierry Reding34fa1832014-06-05 16:31:10 +0200822struct tegra_sor_params {
823 /* number of link clocks per line */
824 unsigned int num_clocks;
825 /* ratio between input and output */
826 u64 ratio;
827 /* precision factor */
828 u64 precision;
829
830 unsigned int active_polarity;
831 unsigned int active_count;
832 unsigned int active_frac;
833 unsigned int tu_size;
834 unsigned int error;
835};
836
837static int tegra_sor_compute_params(struct tegra_sor *sor,
838 struct tegra_sor_params *params,
839 unsigned int tu_size)
840{
841 u64 active_sym, active_count, frac, approx;
842 u32 active_polarity, active_frac = 0;
843 const u64 f = params->precision;
844 s64 error;
845
846 active_sym = params->ratio * tu_size;
847 active_count = div_u64(active_sym, f) * f;
848 frac = active_sym - active_count;
849
850 /* fraction < 0.5 */
851 if (frac >= (f / 2)) {
852 active_polarity = 1;
853 frac = f - frac;
854 } else {
855 active_polarity = 0;
856 }
857
858 if (frac != 0) {
859 frac = div_u64(f * f, frac); /* 1/fraction */
860 if (frac <= (15 * f)) {
861 active_frac = div_u64(frac, f);
862
863 /* round up */
864 if (active_polarity)
865 active_frac++;
866 } else {
867 active_frac = active_polarity ? 1 : 15;
868 }
869 }
870
871 if (active_frac == 1)
872 active_polarity = 0;
873
874 if (active_polarity == 1) {
875 if (active_frac) {
876 approx = active_count + (active_frac * (f - 1)) * f;
877 approx = div_u64(approx, active_frac * f);
878 } else {
879 approx = active_count + f;
880 }
881 } else {
882 if (active_frac)
883 approx = active_count + div_u64(f, active_frac);
884 else
885 approx = active_count;
886 }
887
888 error = div_s64(active_sym - approx, tu_size);
889 error *= params->num_clocks;
890
Andrew Morton79211c82015-11-09 14:58:13 -0800891 if (error <= 0 && abs(error) < params->error) {
Thierry Reding34fa1832014-06-05 16:31:10 +0200892 params->active_count = div_u64(active_count, f);
893 params->active_polarity = active_polarity;
894 params->active_frac = active_frac;
Andrew Morton79211c82015-11-09 14:58:13 -0800895 params->error = abs(error);
Thierry Reding34fa1832014-06-05 16:31:10 +0200896 params->tu_size = tu_size;
897
898 if (error == 0)
899 return true;
900 }
901
902 return false;
903}
904
Thierry Redinga1983592015-07-21 16:46:52 +0200905static int tegra_sor_compute_config(struct tegra_sor *sor,
906 const struct drm_display_mode *mode,
907 struct tegra_sor_config *config,
908 struct drm_dp_link *link)
Thierry Reding34fa1832014-06-05 16:31:10 +0200909{
910 const u64 f = 100000, link_rate = link->rate * 1000;
911 const u64 pclk = mode->clock * 1000;
Thierry Reding7890b572014-06-05 16:12:46 +0200912 u64 input, output, watermark, num;
Thierry Reding34fa1832014-06-05 16:31:10 +0200913 struct tegra_sor_params params;
Thierry Reding34fa1832014-06-05 16:31:10 +0200914 u32 num_syms_per_line;
915 unsigned int i;
916
917 if (!link_rate || !link->num_lanes || !pclk || !config->bits_per_pixel)
918 return -EINVAL;
919
920 output = link_rate * 8 * link->num_lanes;
921 input = pclk * config->bits_per_pixel;
922
923 if (input >= output)
924 return -ERANGE;
925
926 memset(&params, 0, sizeof(params));
927 params.ratio = div64_u64(input * f, output);
928 params.num_clocks = div_u64(link_rate * mode->hdisplay, pclk);
929 params.precision = f;
930 params.error = 64 * f;
931 params.tu_size = 64;
932
933 for (i = params.tu_size; i >= 32; i--)
934 if (tegra_sor_compute_params(sor, &params, i))
935 break;
936
937 if (params.active_frac == 0) {
938 config->active_polarity = 0;
939 config->active_count = params.active_count;
940
941 if (!params.active_polarity)
942 config->active_count--;
943
944 config->tu_size = params.tu_size;
945 config->active_frac = 1;
946 } else {
947 config->active_polarity = params.active_polarity;
948 config->active_count = params.active_count;
949 config->active_frac = params.active_frac;
950 config->tu_size = params.tu_size;
951 }
952
953 dev_dbg(sor->dev,
954 "polarity: %d active count: %d tu size: %d active frac: %d\n",
955 config->active_polarity, config->active_count,
956 config->tu_size, config->active_frac);
957
958 watermark = params.ratio * config->tu_size * (f - params.ratio);
959 watermark = div_u64(watermark, f);
960
961 watermark = div_u64(watermark + params.error, f);
962 config->watermark = watermark + (config->bits_per_pixel / 8) + 2;
963 num_syms_per_line = (mode->hdisplay * config->bits_per_pixel) *
964 (link->num_lanes * 8);
965
966 if (config->watermark > 30) {
967 config->watermark = 30;
968 dev_err(sor->dev,
969 "unable to compute TU size, forcing watermark to %u\n",
970 config->watermark);
971 } else if (config->watermark > num_syms_per_line) {
972 config->watermark = num_syms_per_line;
973 dev_err(sor->dev, "watermark too high, forcing to %u\n",
974 config->watermark);
975 }
976
Thierry Reding7890b572014-06-05 16:12:46 +0200977 /* compute the number of symbols per horizontal blanking interval */
978 num = ((mode->htotal - mode->hdisplay) - 7) * link_rate;
979 config->hblank_symbols = div_u64(num, pclk);
980
981 if (link->capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
982 config->hblank_symbols -= 3;
983
984 config->hblank_symbols -= 12 / link->num_lanes;
985
986 /* compute the number of symbols per vertical blanking interval */
987 num = (mode->hdisplay - 25) * link_rate;
988 config->vblank_symbols = div_u64(num, pclk);
989 config->vblank_symbols -= 36 / link->num_lanes + 4;
990
991 dev_dbg(sor->dev, "blank symbols: H:%u V:%u\n", config->hblank_symbols,
992 config->vblank_symbols);
993
Thierry Reding34fa1832014-06-05 16:31:10 +0200994 return 0;
995}
996
Thierry Reding402f6bc2015-07-21 16:48:19 +0200997static void tegra_sor_apply_config(struct tegra_sor *sor,
998 const struct tegra_sor_config *config)
999{
1000 u32 value;
1001
1002 value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
1003 value &= ~SOR_DP_LINKCTL_TU_SIZE_MASK;
1004 value |= SOR_DP_LINKCTL_TU_SIZE(config->tu_size);
1005 tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
1006
1007 value = tegra_sor_readl(sor, SOR_DP_CONFIG0);
1008 value &= ~SOR_DP_CONFIG_WATERMARK_MASK;
1009 value |= SOR_DP_CONFIG_WATERMARK(config->watermark);
1010
1011 value &= ~SOR_DP_CONFIG_ACTIVE_SYM_COUNT_MASK;
1012 value |= SOR_DP_CONFIG_ACTIVE_SYM_COUNT(config->active_count);
1013
1014 value &= ~SOR_DP_CONFIG_ACTIVE_SYM_FRAC_MASK;
1015 value |= SOR_DP_CONFIG_ACTIVE_SYM_FRAC(config->active_frac);
1016
1017 if (config->active_polarity)
1018 value |= SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
1019 else
1020 value &= ~SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
1021
1022 value |= SOR_DP_CONFIG_ACTIVE_SYM_ENABLE;
1023 value |= SOR_DP_CONFIG_DISPARITY_NEGATIVE;
1024 tegra_sor_writel(sor, value, SOR_DP_CONFIG0);
1025
1026 value = tegra_sor_readl(sor, SOR_DP_AUDIO_HBLANK_SYMBOLS);
1027 value &= ~SOR_DP_AUDIO_HBLANK_SYMBOLS_MASK;
1028 value |= config->hblank_symbols & 0xffff;
1029 tegra_sor_writel(sor, value, SOR_DP_AUDIO_HBLANK_SYMBOLS);
1030
1031 value = tegra_sor_readl(sor, SOR_DP_AUDIO_VBLANK_SYMBOLS);
1032 value &= ~SOR_DP_AUDIO_VBLANK_SYMBOLS_MASK;
1033 value |= config->vblank_symbols & 0xffff;
1034 tegra_sor_writel(sor, value, SOR_DP_AUDIO_VBLANK_SYMBOLS);
1035}
1036
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001037static void tegra_sor_mode_set(struct tegra_sor *sor,
1038 const struct drm_display_mode *mode,
Thierry Redingc31efa72015-09-08 16:09:22 +02001039 struct tegra_sor_state *state)
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001040{
1041 struct tegra_dc *dc = to_tegra_dc(sor->output.encoder.crtc);
1042 unsigned int vbe, vse, hbe, hse, vbs, hbs;
1043 u32 value;
1044
1045 value = tegra_sor_readl(sor, SOR_STATE1);
1046 value &= ~SOR_STATE_ASY_PIXELDEPTH_MASK;
1047 value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
1048 value &= ~SOR_STATE_ASY_OWNER_MASK;
1049
1050 value |= SOR_STATE_ASY_CRC_MODE_COMPLETE |
1051 SOR_STATE_ASY_OWNER(dc->pipe + 1);
1052
1053 if (mode->flags & DRM_MODE_FLAG_PHSYNC)
1054 value &= ~SOR_STATE_ASY_HSYNCPOL;
1055
1056 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1057 value |= SOR_STATE_ASY_HSYNCPOL;
1058
1059 if (mode->flags & DRM_MODE_FLAG_PVSYNC)
1060 value &= ~SOR_STATE_ASY_VSYNCPOL;
1061
1062 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1063 value |= SOR_STATE_ASY_VSYNCPOL;
1064
Thierry Redingc31efa72015-09-08 16:09:22 +02001065 switch (state->bpc) {
1066 case 16:
1067 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_48_444;
1068 break;
1069
1070 case 12:
1071 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_36_444;
1072 break;
1073
1074 case 10:
1075 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_30_444;
1076 break;
1077
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001078 case 8:
1079 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
1080 break;
1081
1082 case 6:
1083 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_18_444;
1084 break;
1085
1086 default:
Thierry Redingc31efa72015-09-08 16:09:22 +02001087 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001088 break;
1089 }
1090
1091 tegra_sor_writel(sor, value, SOR_STATE1);
1092
1093 /*
1094 * TODO: The video timing programming below doesn't seem to match the
1095 * register definitions.
1096 */
1097
1098 value = ((mode->vtotal & 0x7fff) << 16) | (mode->htotal & 0x7fff);
Thierry Reding880cee02017-10-12 19:04:17 +02001099 tegra_sor_writel(sor, value, sor->soc->regs->head_state1 + dc->pipe);
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001100
1101 /* sync end = sync width - 1 */
1102 vse = mode->vsync_end - mode->vsync_start - 1;
1103 hse = mode->hsync_end - mode->hsync_start - 1;
1104
1105 value = ((vse & 0x7fff) << 16) | (hse & 0x7fff);
Thierry Reding880cee02017-10-12 19:04:17 +02001106 tegra_sor_writel(sor, value, sor->soc->regs->head_state2 + dc->pipe);
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001107
1108 /* blank end = sync end + back porch */
1109 vbe = vse + (mode->vtotal - mode->vsync_end);
1110 hbe = hse + (mode->htotal - mode->hsync_end);
1111
1112 value = ((vbe & 0x7fff) << 16) | (hbe & 0x7fff);
Thierry Reding880cee02017-10-12 19:04:17 +02001113 tegra_sor_writel(sor, value, sor->soc->regs->head_state3 + dc->pipe);
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001114
1115 /* blank start = blank end + active */
1116 vbs = vbe + mode->vdisplay;
1117 hbs = hbe + mode->hdisplay;
1118
1119 value = ((vbs & 0x7fff) << 16) | (hbs & 0x7fff);
Thierry Reding880cee02017-10-12 19:04:17 +02001120 tegra_sor_writel(sor, value, sor->soc->regs->head_state4 + dc->pipe);
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001121
1122 /* XXX interlacing support */
Thierry Reding880cee02017-10-12 19:04:17 +02001123 tegra_sor_writel(sor, 0x001, sor->soc->regs->head_state5 + dc->pipe);
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001124}
1125
Thierry Reding6fad8f62014-11-28 15:41:34 +01001126static int tegra_sor_detach(struct tegra_sor *sor)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001127{
Thierry Reding6fad8f62014-11-28 15:41:34 +01001128 unsigned long value, timeout;
1129
1130 /* switch to safe mode */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001131 value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001132 value &= ~SOR_SUPER_STATE_MODE_NORMAL;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001133 tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001134 tegra_sor_super_update(sor);
1135
1136 timeout = jiffies + msecs_to_jiffies(250);
1137
1138 while (time_before(jiffies, timeout)) {
1139 value = tegra_sor_readl(sor, SOR_PWR);
1140 if (value & SOR_PWR_MODE_SAFE)
1141 break;
1142 }
1143
1144 if ((value & SOR_PWR_MODE_SAFE) == 0)
1145 return -ETIMEDOUT;
1146
1147 /* go to sleep */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001148 value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001149 value &= ~SOR_SUPER_STATE_HEAD_MODE_MASK;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001150 tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001151 tegra_sor_super_update(sor);
1152
1153 /* detach */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001154 value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001155 value &= ~SOR_SUPER_STATE_ATTACHED;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001156 tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001157 tegra_sor_super_update(sor);
1158
1159 timeout = jiffies + msecs_to_jiffies(250);
1160
1161 while (time_before(jiffies, timeout)) {
1162 value = tegra_sor_readl(sor, SOR_TEST);
1163 if ((value & SOR_TEST_ATTACHED) == 0)
1164 break;
1165
1166 usleep_range(25, 100);
1167 }
1168
1169 if ((value & SOR_TEST_ATTACHED) != 0)
1170 return -ETIMEDOUT;
1171
1172 return 0;
1173}
1174
1175static int tegra_sor_power_down(struct tegra_sor *sor)
1176{
1177 unsigned long value, timeout;
1178 int err;
1179
1180 value = tegra_sor_readl(sor, SOR_PWR);
1181 value &= ~SOR_PWR_NORMAL_STATE_PU;
1182 value |= SOR_PWR_TRIGGER;
1183 tegra_sor_writel(sor, value, SOR_PWR);
1184
1185 timeout = jiffies + msecs_to_jiffies(250);
1186
1187 while (time_before(jiffies, timeout)) {
1188 value = tegra_sor_readl(sor, SOR_PWR);
1189 if ((value & SOR_PWR_TRIGGER) == 0)
1190 return 0;
1191
1192 usleep_range(25, 100);
1193 }
1194
1195 if ((value & SOR_PWR_TRIGGER) != 0)
1196 return -ETIMEDOUT;
1197
Thierry Reding25bb2ce2015-08-03 14:23:29 +02001198 /* switch to safe parent clock */
1199 err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
Thierry Redinge1335e22017-10-12 17:53:11 +02001200 if (err < 0) {
Thierry Reding6fad8f62014-11-28 15:41:34 +01001201 dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
Thierry Redinge1335e22017-10-12 17:53:11 +02001202 return err;
1203 }
Thierry Reding6fad8f62014-11-28 15:41:34 +01001204
Thierry Reding880cee02017-10-12 19:04:17 +02001205 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001206 value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
1207 SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2);
Thierry Reding880cee02017-10-12 19:04:17 +02001208 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001209
1210 /* stop lane sequencer */
1211 value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_UP |
1212 SOR_LANE_SEQ_CTL_POWER_STATE_DOWN;
1213 tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
1214
1215 timeout = jiffies + msecs_to_jiffies(250);
1216
1217 while (time_before(jiffies, timeout)) {
1218 value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
1219 if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
1220 break;
1221
1222 usleep_range(25, 100);
1223 }
1224
1225 if ((value & SOR_LANE_SEQ_CTL_TRIGGER) != 0)
1226 return -ETIMEDOUT;
1227
Thierry Reding880cee02017-10-12 19:04:17 +02001228 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001229 value |= SOR_PLL2_PORT_POWERDOWN;
Thierry Reding880cee02017-10-12 19:04:17 +02001230 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001231
1232 usleep_range(20, 100);
1233
Thierry Reding880cee02017-10-12 19:04:17 +02001234 value = tegra_sor_readl(sor, sor->soc->regs->pll0);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001235 value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
Thierry Reding880cee02017-10-12 19:04:17 +02001236 tegra_sor_writel(sor, value, sor->soc->regs->pll0);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001237
Thierry Reding880cee02017-10-12 19:04:17 +02001238 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001239 value |= SOR_PLL2_SEQ_PLLCAPPD;
1240 value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
Thierry Reding880cee02017-10-12 19:04:17 +02001241 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001242
1243 usleep_range(20, 100);
1244
1245 return 0;
1246}
1247
Thierry Reding6fad8f62014-11-28 15:41:34 +01001248static int tegra_sor_crc_wait(struct tegra_sor *sor, unsigned long timeout)
1249{
1250 u32 value;
1251
1252 timeout = jiffies + msecs_to_jiffies(timeout);
1253
1254 while (time_before(jiffies, timeout)) {
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001255 value = tegra_sor_readl(sor, SOR_CRCA);
1256 if (value & SOR_CRCA_VALID)
Thierry Reding6fad8f62014-11-28 15:41:34 +01001257 return 0;
1258
1259 usleep_range(100, 200);
1260 }
1261
1262 return -ETIMEDOUT;
1263}
1264
Thierry Reding530239a2015-08-06 11:04:54 +02001265static int tegra_sor_show_crc(struct seq_file *s, void *data)
Thierry Reding6fad8f62014-11-28 15:41:34 +01001266{
Thierry Reding530239a2015-08-06 11:04:54 +02001267 struct drm_info_node *node = s->private;
1268 struct tegra_sor *sor = node->info_ent->data;
Thierry Reding850bab42015-07-29 17:58:41 +02001269 struct drm_crtc *crtc = sor->output.encoder.crtc;
1270 struct drm_device *drm = node->minor->dev;
Thierry Reding530239a2015-08-06 11:04:54 +02001271 int err = 0;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001272 u32 value;
1273
Thierry Reding850bab42015-07-29 17:58:41 +02001274 drm_modeset_lock_all(drm);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001275
Thierry Reding850bab42015-07-29 17:58:41 +02001276 if (!crtc || !crtc->state->active) {
1277 err = -EBUSY;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001278 goto unlock;
1279 }
1280
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001281 value = tegra_sor_readl(sor, SOR_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001282 value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001283 tegra_sor_writel(sor, value, SOR_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001284
1285 value = tegra_sor_readl(sor, SOR_CRC_CNTRL);
1286 value |= SOR_CRC_CNTRL_ENABLE;
1287 tegra_sor_writel(sor, value, SOR_CRC_CNTRL);
1288
1289 value = tegra_sor_readl(sor, SOR_TEST);
1290 value &= ~SOR_TEST_CRC_POST_SERIALIZE;
1291 tegra_sor_writel(sor, value, SOR_TEST);
1292
1293 err = tegra_sor_crc_wait(sor, 100);
1294 if (err < 0)
1295 goto unlock;
1296
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001297 tegra_sor_writel(sor, SOR_CRCA_RESET, SOR_CRCA);
1298 value = tegra_sor_readl(sor, SOR_CRCB);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001299
Thierry Reding530239a2015-08-06 11:04:54 +02001300 seq_printf(s, "%08x\n", value);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001301
1302unlock:
Thierry Reding850bab42015-07-29 17:58:41 +02001303 drm_modeset_unlock_all(drm);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001304 return err;
1305}
1306
Thierry Reding062f5b2c2017-11-10 12:21:51 +01001307#define DEBUGFS_REG32(_name) { .name = #_name, .offset = _name }
1308
1309static const struct debugfs_reg32 tegra_sor_regs[] = {
1310 DEBUGFS_REG32(SOR_CTXSW),
1311 DEBUGFS_REG32(SOR_SUPER_STATE0),
1312 DEBUGFS_REG32(SOR_SUPER_STATE1),
1313 DEBUGFS_REG32(SOR_STATE0),
1314 DEBUGFS_REG32(SOR_STATE1),
1315 DEBUGFS_REG32(SOR_HEAD_STATE0(0)),
1316 DEBUGFS_REG32(SOR_HEAD_STATE0(1)),
1317 DEBUGFS_REG32(SOR_HEAD_STATE1(0)),
1318 DEBUGFS_REG32(SOR_HEAD_STATE1(1)),
1319 DEBUGFS_REG32(SOR_HEAD_STATE2(0)),
1320 DEBUGFS_REG32(SOR_HEAD_STATE2(1)),
1321 DEBUGFS_REG32(SOR_HEAD_STATE3(0)),
1322 DEBUGFS_REG32(SOR_HEAD_STATE3(1)),
1323 DEBUGFS_REG32(SOR_HEAD_STATE4(0)),
1324 DEBUGFS_REG32(SOR_HEAD_STATE4(1)),
1325 DEBUGFS_REG32(SOR_HEAD_STATE5(0)),
1326 DEBUGFS_REG32(SOR_HEAD_STATE5(1)),
1327 DEBUGFS_REG32(SOR_CRC_CNTRL),
1328 DEBUGFS_REG32(SOR_DP_DEBUG_MVID),
1329 DEBUGFS_REG32(SOR_CLK_CNTRL),
1330 DEBUGFS_REG32(SOR_CAP),
1331 DEBUGFS_REG32(SOR_PWR),
1332 DEBUGFS_REG32(SOR_TEST),
1333 DEBUGFS_REG32(SOR_PLL0),
1334 DEBUGFS_REG32(SOR_PLL1),
1335 DEBUGFS_REG32(SOR_PLL2),
1336 DEBUGFS_REG32(SOR_PLL3),
1337 DEBUGFS_REG32(SOR_CSTM),
1338 DEBUGFS_REG32(SOR_LVDS),
1339 DEBUGFS_REG32(SOR_CRCA),
1340 DEBUGFS_REG32(SOR_CRCB),
1341 DEBUGFS_REG32(SOR_BLANK),
1342 DEBUGFS_REG32(SOR_SEQ_CTL),
1343 DEBUGFS_REG32(SOR_LANE_SEQ_CTL),
1344 DEBUGFS_REG32(SOR_SEQ_INST(0)),
1345 DEBUGFS_REG32(SOR_SEQ_INST(1)),
1346 DEBUGFS_REG32(SOR_SEQ_INST(2)),
1347 DEBUGFS_REG32(SOR_SEQ_INST(3)),
1348 DEBUGFS_REG32(SOR_SEQ_INST(4)),
1349 DEBUGFS_REG32(SOR_SEQ_INST(5)),
1350 DEBUGFS_REG32(SOR_SEQ_INST(6)),
1351 DEBUGFS_REG32(SOR_SEQ_INST(7)),
1352 DEBUGFS_REG32(SOR_SEQ_INST(8)),
1353 DEBUGFS_REG32(SOR_SEQ_INST(9)),
1354 DEBUGFS_REG32(SOR_SEQ_INST(10)),
1355 DEBUGFS_REG32(SOR_SEQ_INST(11)),
1356 DEBUGFS_REG32(SOR_SEQ_INST(12)),
1357 DEBUGFS_REG32(SOR_SEQ_INST(13)),
1358 DEBUGFS_REG32(SOR_SEQ_INST(14)),
1359 DEBUGFS_REG32(SOR_SEQ_INST(15)),
1360 DEBUGFS_REG32(SOR_PWM_DIV),
1361 DEBUGFS_REG32(SOR_PWM_CTL),
1362 DEBUGFS_REG32(SOR_VCRC_A0),
1363 DEBUGFS_REG32(SOR_VCRC_A1),
1364 DEBUGFS_REG32(SOR_VCRC_B0),
1365 DEBUGFS_REG32(SOR_VCRC_B1),
1366 DEBUGFS_REG32(SOR_CCRC_A0),
1367 DEBUGFS_REG32(SOR_CCRC_A1),
1368 DEBUGFS_REG32(SOR_CCRC_B0),
1369 DEBUGFS_REG32(SOR_CCRC_B1),
1370 DEBUGFS_REG32(SOR_EDATA_A0),
1371 DEBUGFS_REG32(SOR_EDATA_A1),
1372 DEBUGFS_REG32(SOR_EDATA_B0),
1373 DEBUGFS_REG32(SOR_EDATA_B1),
1374 DEBUGFS_REG32(SOR_COUNT_A0),
1375 DEBUGFS_REG32(SOR_COUNT_A1),
1376 DEBUGFS_REG32(SOR_COUNT_B0),
1377 DEBUGFS_REG32(SOR_COUNT_B1),
1378 DEBUGFS_REG32(SOR_DEBUG_A0),
1379 DEBUGFS_REG32(SOR_DEBUG_A1),
1380 DEBUGFS_REG32(SOR_DEBUG_B0),
1381 DEBUGFS_REG32(SOR_DEBUG_B1),
1382 DEBUGFS_REG32(SOR_TRIG),
1383 DEBUGFS_REG32(SOR_MSCHECK),
1384 DEBUGFS_REG32(SOR_XBAR_CTRL),
1385 DEBUGFS_REG32(SOR_XBAR_POL),
1386 DEBUGFS_REG32(SOR_DP_LINKCTL0),
1387 DEBUGFS_REG32(SOR_DP_LINKCTL1),
1388 DEBUGFS_REG32(SOR_LANE_DRIVE_CURRENT0),
1389 DEBUGFS_REG32(SOR_LANE_DRIVE_CURRENT1),
1390 DEBUGFS_REG32(SOR_LANE4_DRIVE_CURRENT0),
1391 DEBUGFS_REG32(SOR_LANE4_DRIVE_CURRENT1),
1392 DEBUGFS_REG32(SOR_LANE_PREEMPHASIS0),
1393 DEBUGFS_REG32(SOR_LANE_PREEMPHASIS1),
1394 DEBUGFS_REG32(SOR_LANE4_PREEMPHASIS0),
1395 DEBUGFS_REG32(SOR_LANE4_PREEMPHASIS1),
1396 DEBUGFS_REG32(SOR_LANE_POSTCURSOR0),
1397 DEBUGFS_REG32(SOR_LANE_POSTCURSOR1),
1398 DEBUGFS_REG32(SOR_DP_CONFIG0),
1399 DEBUGFS_REG32(SOR_DP_CONFIG1),
1400 DEBUGFS_REG32(SOR_DP_MN0),
1401 DEBUGFS_REG32(SOR_DP_MN1),
1402 DEBUGFS_REG32(SOR_DP_PADCTL0),
1403 DEBUGFS_REG32(SOR_DP_PADCTL1),
Thierry Redingc57997b2017-10-12 19:12:57 +02001404 DEBUGFS_REG32(SOR_DP_PADCTL2),
Thierry Reding062f5b2c2017-11-10 12:21:51 +01001405 DEBUGFS_REG32(SOR_DP_DEBUG0),
1406 DEBUGFS_REG32(SOR_DP_DEBUG1),
1407 DEBUGFS_REG32(SOR_DP_SPARE0),
1408 DEBUGFS_REG32(SOR_DP_SPARE1),
1409 DEBUGFS_REG32(SOR_DP_AUDIO_CTRL),
1410 DEBUGFS_REG32(SOR_DP_AUDIO_HBLANK_SYMBOLS),
1411 DEBUGFS_REG32(SOR_DP_AUDIO_VBLANK_SYMBOLS),
1412 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_HEADER),
1413 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK0),
1414 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK1),
1415 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK2),
1416 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK3),
1417 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK4),
1418 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK5),
1419 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK6),
1420 DEBUGFS_REG32(SOR_DP_TPG),
1421 DEBUGFS_REG32(SOR_DP_TPG_CONFIG),
1422 DEBUGFS_REG32(SOR_DP_LQ_CSTM0),
1423 DEBUGFS_REG32(SOR_DP_LQ_CSTM1),
1424 DEBUGFS_REG32(SOR_DP_LQ_CSTM2),
1425};
1426
Thierry Redingdab16332015-01-26 16:04:08 +01001427static int tegra_sor_show_regs(struct seq_file *s, void *data)
1428{
1429 struct drm_info_node *node = s->private;
1430 struct tegra_sor *sor = node->info_ent->data;
Thierry Reding850bab42015-07-29 17:58:41 +02001431 struct drm_crtc *crtc = sor->output.encoder.crtc;
1432 struct drm_device *drm = node->minor->dev;
Thierry Reding062f5b2c2017-11-10 12:21:51 +01001433 unsigned int i;
Thierry Reding850bab42015-07-29 17:58:41 +02001434 int err = 0;
1435
1436 drm_modeset_lock_all(drm);
1437
1438 if (!crtc || !crtc->state->active) {
1439 err = -EBUSY;
1440 goto unlock;
1441 }
Thierry Redingdab16332015-01-26 16:04:08 +01001442
Thierry Reding062f5b2c2017-11-10 12:21:51 +01001443 for (i = 0; i < ARRAY_SIZE(tegra_sor_regs); i++) {
1444 unsigned int offset = tegra_sor_regs[i].offset;
Thierry Redingdab16332015-01-26 16:04:08 +01001445
Thierry Reding062f5b2c2017-11-10 12:21:51 +01001446 seq_printf(s, "%-38s %#05x %08x\n", tegra_sor_regs[i].name,
1447 offset, tegra_sor_readl(sor, offset));
1448 }
Thierry Redingdab16332015-01-26 16:04:08 +01001449
Thierry Reding850bab42015-07-29 17:58:41 +02001450unlock:
1451 drm_modeset_unlock_all(drm);
1452 return err;
Thierry Redingdab16332015-01-26 16:04:08 +01001453}
1454
1455static const struct drm_info_list debugfs_files[] = {
Thierry Reding530239a2015-08-06 11:04:54 +02001456 { "crc", tegra_sor_show_crc, 0, NULL },
Thierry Redingdab16332015-01-26 16:04:08 +01001457 { "regs", tegra_sor_show_regs, 0, NULL },
1458};
1459
Thierry Reding5b8e0432017-11-08 13:20:01 +01001460static int tegra_sor_late_register(struct drm_connector *connector)
Thierry Reding6fad8f62014-11-28 15:41:34 +01001461{
Thierry Reding5b8e0432017-11-08 13:20:01 +01001462 struct tegra_output *output = connector_to_output(connector);
1463 unsigned int i, count = ARRAY_SIZE(debugfs_files);
1464 struct drm_minor *minor = connector->dev->primary;
1465 struct dentry *root = connector->debugfs_entry;
1466 struct tegra_sor *sor = to_sor(output);
Thierry Reding530239a2015-08-06 11:04:54 +02001467 int err;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001468
Thierry Redingdab16332015-01-26 16:04:08 +01001469 sor->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1470 GFP_KERNEL);
Thierry Reding5b8e0432017-11-08 13:20:01 +01001471 if (!sor->debugfs_files)
1472 return -ENOMEM;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001473
Thierry Reding5b8e0432017-11-08 13:20:01 +01001474 for (i = 0; i < count; i++)
Thierry Redingdab16332015-01-26 16:04:08 +01001475 sor->debugfs_files[i].data = sor;
1476
Thierry Reding5b8e0432017-11-08 13:20:01 +01001477 err = drm_debugfs_create_files(sor->debugfs_files, count, root, minor);
Thierry Redingdab16332015-01-26 16:04:08 +01001478 if (err < 0)
1479 goto free;
1480
Thierry Reding530239a2015-08-06 11:04:54 +02001481 return 0;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001482
Thierry Redingdab16332015-01-26 16:04:08 +01001483free:
1484 kfree(sor->debugfs_files);
1485 sor->debugfs_files = NULL;
Thierry Reding5b8e0432017-11-08 13:20:01 +01001486
Thierry Reding6fad8f62014-11-28 15:41:34 +01001487 return err;
1488}
1489
Thierry Reding5b8e0432017-11-08 13:20:01 +01001490static void tegra_sor_early_unregister(struct drm_connector *connector)
Thierry Reding6fad8f62014-11-28 15:41:34 +01001491{
Thierry Reding5b8e0432017-11-08 13:20:01 +01001492 struct tegra_output *output = connector_to_output(connector);
1493 unsigned int count = ARRAY_SIZE(debugfs_files);
1494 struct tegra_sor *sor = to_sor(output);
Thierry Redingd92e60092017-10-12 19:07:54 +02001495
Thierry Reding5b8e0432017-11-08 13:20:01 +01001496 drm_debugfs_remove_files(sor->debugfs_files, count,
1497 connector->dev->primary);
Thierry Redingdab16332015-01-26 16:04:08 +01001498 kfree(sor->debugfs_files);
Thierry Reding066d30f2015-07-03 14:16:30 +02001499 sor->debugfs_files = NULL;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001500}
1501
Thierry Redingc31efa72015-09-08 16:09:22 +02001502static void tegra_sor_connector_reset(struct drm_connector *connector)
1503{
1504 struct tegra_sor_state *state;
1505
1506 state = kzalloc(sizeof(*state), GFP_KERNEL);
1507 if (!state)
1508 return;
1509
1510 if (connector->state) {
1511 __drm_atomic_helper_connector_destroy_state(connector->state);
1512 kfree(connector->state);
1513 }
1514
1515 __drm_atomic_helper_connector_reset(connector, &state->base);
1516}
1517
Thierry Reding6fad8f62014-11-28 15:41:34 +01001518static enum drm_connector_status
1519tegra_sor_connector_detect(struct drm_connector *connector, bool force)
1520{
1521 struct tegra_output *output = connector_to_output(connector);
1522 struct tegra_sor *sor = to_sor(output);
1523
Thierry Reding9542c232015-07-08 13:39:09 +02001524 if (sor->aux)
1525 return drm_dp_aux_detect(sor->aux);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001526
Thierry Reding459cc2c2015-07-30 10:34:24 +02001527 return tegra_output_connector_detect(connector, force);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001528}
1529
Thierry Redingc31efa72015-09-08 16:09:22 +02001530static struct drm_connector_state *
1531tegra_sor_connector_duplicate_state(struct drm_connector *connector)
1532{
1533 struct tegra_sor_state *state = to_sor_state(connector->state);
1534 struct tegra_sor_state *copy;
1535
1536 copy = kmemdup(state, sizeof(*state), GFP_KERNEL);
1537 if (!copy)
1538 return NULL;
1539
1540 __drm_atomic_helper_connector_duplicate_state(connector, &copy->base);
1541
1542 return &copy->base;
1543}
1544
Thierry Reding6fad8f62014-11-28 15:41:34 +01001545static const struct drm_connector_funcs tegra_sor_connector_funcs = {
Thierry Redingc31efa72015-09-08 16:09:22 +02001546 .reset = tegra_sor_connector_reset,
Thierry Reding6fad8f62014-11-28 15:41:34 +01001547 .detect = tegra_sor_connector_detect,
1548 .fill_modes = drm_helper_probe_single_connector_modes,
1549 .destroy = tegra_output_connector_destroy,
Thierry Redingc31efa72015-09-08 16:09:22 +02001550 .atomic_duplicate_state = tegra_sor_connector_duplicate_state,
Thierry Reding4aa3df72014-11-24 16:27:13 +01001551 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
Thierry Reding5b8e0432017-11-08 13:20:01 +01001552 .late_register = tegra_sor_late_register,
1553 .early_unregister = tegra_sor_early_unregister,
Thierry Reding6fad8f62014-11-28 15:41:34 +01001554};
1555
1556static int tegra_sor_connector_get_modes(struct drm_connector *connector)
1557{
1558 struct tegra_output *output = connector_to_output(connector);
1559 struct tegra_sor *sor = to_sor(output);
1560 int err;
1561
Thierry Reding9542c232015-07-08 13:39:09 +02001562 if (sor->aux)
1563 drm_dp_aux_enable(sor->aux);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001564
1565 err = tegra_output_connector_get_modes(connector);
1566
Thierry Reding9542c232015-07-08 13:39:09 +02001567 if (sor->aux)
1568 drm_dp_aux_disable(sor->aux);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001569
1570 return err;
1571}
1572
1573static enum drm_mode_status
1574tegra_sor_connector_mode_valid(struct drm_connector *connector,
1575 struct drm_display_mode *mode)
1576{
1577 return MODE_OK;
1578}
1579
1580static const struct drm_connector_helper_funcs tegra_sor_connector_helper_funcs = {
1581 .get_modes = tegra_sor_connector_get_modes,
1582 .mode_valid = tegra_sor_connector_mode_valid,
Thierry Reding6fad8f62014-11-28 15:41:34 +01001583};
1584
1585static const struct drm_encoder_funcs tegra_sor_encoder_funcs = {
1586 .destroy = tegra_output_encoder_destroy,
1587};
1588
Thierry Reding850bab42015-07-29 17:58:41 +02001589static void tegra_sor_edp_disable(struct drm_encoder *encoder)
Thierry Reding6fad8f62014-11-28 15:41:34 +01001590{
Thierry Reding850bab42015-07-29 17:58:41 +02001591 struct tegra_output *output = encoder_to_output(encoder);
1592 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
1593 struct tegra_sor *sor = to_sor(output);
1594 u32 value;
1595 int err;
1596
1597 if (output->panel)
1598 drm_panel_disable(output->panel);
1599
1600 err = tegra_sor_detach(sor);
1601 if (err < 0)
1602 dev_err(sor->dev, "failed to detach SOR: %d\n", err);
1603
1604 tegra_sor_writel(sor, 0, SOR_STATE1);
1605 tegra_sor_update(sor);
1606
1607 /*
1608 * The following accesses registers of the display controller, so make
1609 * sure it's only executed when the output is attached to one.
1610 */
1611 if (dc) {
1612 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
Thierry Redingc57997b2017-10-12 19:12:57 +02001613 value &= ~SOR_ENABLE(0);
Thierry Reding850bab42015-07-29 17:58:41 +02001614 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
1615
1616 tegra_dc_commit(dc);
1617 }
1618
1619 err = tegra_sor_power_down(sor);
1620 if (err < 0)
1621 dev_err(sor->dev, "failed to power down SOR: %d\n", err);
1622
Thierry Reding9542c232015-07-08 13:39:09 +02001623 if (sor->aux) {
1624 err = drm_dp_aux_disable(sor->aux);
Thierry Reding850bab42015-07-29 17:58:41 +02001625 if (err < 0)
1626 dev_err(sor->dev, "failed to disable DP: %d\n", err);
1627 }
1628
Thierry Redingc57997b2017-10-12 19:12:57 +02001629 err = tegra_io_pad_power_disable(sor->pad);
Thierry Reding850bab42015-07-29 17:58:41 +02001630 if (err < 0)
Thierry Redingc57997b2017-10-12 19:12:57 +02001631 dev_err(sor->dev, "failed to power off I/O pad: %d\n", err);
Thierry Reding850bab42015-07-29 17:58:41 +02001632
1633 if (output->panel)
1634 drm_panel_unprepare(output->panel);
1635
Thierry Redingaaff8bd2015-08-07 16:04:54 +02001636 pm_runtime_put(sor->dev);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001637}
1638
Thierry Reding459cc2c2015-07-30 10:34:24 +02001639#if 0
1640static int calc_h_ref_to_sync(const struct drm_display_mode *mode,
1641 unsigned int *value)
1642{
1643 unsigned int hfp, hsw, hbp, a = 0, b;
1644
1645 hfp = mode->hsync_start - mode->hdisplay;
1646 hsw = mode->hsync_end - mode->hsync_start;
1647 hbp = mode->htotal - mode->hsync_end;
1648
1649 pr_info("hfp: %u, hsw: %u, hbp: %u\n", hfp, hsw, hbp);
1650
1651 b = hfp - 1;
1652
1653 pr_info("a: %u, b: %u\n", a, b);
1654 pr_info("a + hsw + hbp = %u\n", a + hsw + hbp);
1655
1656 if (a + hsw + hbp <= 11) {
1657 a = 1 + 11 - hsw - hbp;
1658 pr_info("a: %u\n", a);
1659 }
1660
1661 if (a > b)
1662 return -EINVAL;
1663
1664 if (hsw < 1)
1665 return -EINVAL;
1666
1667 if (mode->hdisplay < 16)
1668 return -EINVAL;
1669
1670 if (value) {
1671 if (b > a && a % 2)
1672 *value = a + 1;
1673 else
1674 *value = a;
1675 }
1676
1677 return 0;
1678}
1679#endif
1680
Thierry Reding850bab42015-07-29 17:58:41 +02001681static void tegra_sor_edp_enable(struct drm_encoder *encoder)
Thierry Reding6fad8f62014-11-28 15:41:34 +01001682{
Thierry Reding850bab42015-07-29 17:58:41 +02001683 struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001684 struct tegra_output *output = encoder_to_output(encoder);
1685 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001686 struct tegra_sor *sor = to_sor(output);
Thierry Reding34fa1832014-06-05 16:31:10 +02001687 struct tegra_sor_config config;
Thierry Redingc31efa72015-09-08 16:09:22 +02001688 struct tegra_sor_state *state;
Thierry Reding34fa1832014-06-05 16:31:10 +02001689 struct drm_dp_link link;
Thierry Reding01b9bea2015-11-11 17:15:29 +01001690 u8 rate, lanes;
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001691 unsigned int i;
Thierry Reding86f5c522014-03-26 11:13:16 +01001692 int err = 0;
Thierry Reding28fe2072015-01-26 16:02:48 +01001693 u32 value;
Thierry Reding86f5c522014-03-26 11:13:16 +01001694
Thierry Redingc31efa72015-09-08 16:09:22 +02001695 state = to_sor_state(output->connector.state);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001696
Thierry Redingaaff8bd2015-08-07 16:04:54 +02001697 pm_runtime_get_sync(sor->dev);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001698
Thierry Reding6fad8f62014-11-28 15:41:34 +01001699 if (output->panel)
1700 drm_panel_prepare(output->panel);
1701
Thierry Reding01b9bea2015-11-11 17:15:29 +01001702 err = drm_dp_aux_enable(sor->aux);
1703 if (err < 0)
1704 dev_err(sor->dev, "failed to enable DP: %d\n", err);
Thierry Reding34fa1832014-06-05 16:31:10 +02001705
Thierry Reding01b9bea2015-11-11 17:15:29 +01001706 err = drm_dp_link_probe(sor->aux, &link);
1707 if (err < 0) {
1708 dev_err(sor->dev, "failed to probe eDP link: %d\n", err);
1709 return;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001710 }
1711
Thierry Reding25bb2ce2015-08-03 14:23:29 +02001712 /* switch to safe parent clock */
1713 err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001714 if (err < 0)
1715 dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
1716
Thierry Reding34fa1832014-06-05 16:31:10 +02001717 memset(&config, 0, sizeof(config));
Thierry Redingc31efa72015-09-08 16:09:22 +02001718 config.bits_per_pixel = state->bpc * 3;
Thierry Reding34fa1832014-06-05 16:31:10 +02001719
Thierry Redinga1983592015-07-21 16:46:52 +02001720 err = tegra_sor_compute_config(sor, mode, &config, &link);
Thierry Reding34fa1832014-06-05 16:31:10 +02001721 if (err < 0)
Thierry Redinga1983592015-07-21 16:46:52 +02001722 dev_err(sor->dev, "failed to compute configuration: %d\n", err);
Thierry Reding34fa1832014-06-05 16:31:10 +02001723
Thierry Reding6b6b6042013-11-15 16:06:05 +01001724 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
1725 value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
1726 value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
1727 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
1728
Thierry Reding880cee02017-10-12 19:04:17 +02001729 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001730 value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
Thierry Reding880cee02017-10-12 19:04:17 +02001731 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001732 usleep_range(20, 100);
1733
Thierry Reding880cee02017-10-12 19:04:17 +02001734 value = tegra_sor_readl(sor, sor->soc->regs->pll3);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001735 value |= SOR_PLL3_PLL_VDD_MODE_3V3;
Thierry Reding880cee02017-10-12 19:04:17 +02001736 tegra_sor_writel(sor, value, sor->soc->regs->pll3);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001737
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001738 value = SOR_PLL0_ICHPMP(0xf) | SOR_PLL0_VCOCAP_RST |
1739 SOR_PLL0_PLLREG_LEVEL_V45 | SOR_PLL0_RESISTOR_EXT;
Thierry Reding880cee02017-10-12 19:04:17 +02001740 tegra_sor_writel(sor, value, sor->soc->regs->pll0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001741
Thierry Reding880cee02017-10-12 19:04:17 +02001742 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001743 value |= SOR_PLL2_SEQ_PLLCAPPD;
1744 value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1745 value |= SOR_PLL2_LVDS_ENABLE;
Thierry Reding880cee02017-10-12 19:04:17 +02001746 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001747
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001748 value = SOR_PLL1_TERM_COMPOUT | SOR_PLL1_TMDS_TERM;
Thierry Reding880cee02017-10-12 19:04:17 +02001749 tegra_sor_writel(sor, value, sor->soc->regs->pll1);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001750
1751 while (true) {
Thierry Reding880cee02017-10-12 19:04:17 +02001752 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001753 if ((value & SOR_PLL2_SEQ_PLLCAPPD_ENFORCE) == 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001754 break;
1755
1756 usleep_range(250, 1000);
1757 }
1758
Thierry Reding880cee02017-10-12 19:04:17 +02001759 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001760 value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
1761 value &= ~SOR_PLL2_PORT_POWERDOWN;
Thierry Reding880cee02017-10-12 19:04:17 +02001762 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001763
1764 /*
1765 * power up
1766 */
1767
1768 /* set safe link bandwidth (1.62 Gbps) */
1769 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
1770 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
1771 value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G1_62;
1772 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
1773
1774 /* step 1 */
Thierry Reding880cee02017-10-12 19:04:17 +02001775 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001776 value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE | SOR_PLL2_PORT_POWERDOWN |
1777 SOR_PLL2_BANDGAP_POWERDOWN;
Thierry Reding880cee02017-10-12 19:04:17 +02001778 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001779
Thierry Reding880cee02017-10-12 19:04:17 +02001780 value = tegra_sor_readl(sor, sor->soc->regs->pll0);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001781 value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
Thierry Reding880cee02017-10-12 19:04:17 +02001782 tegra_sor_writel(sor, value, sor->soc->regs->pll0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001783
Thierry Reding880cee02017-10-12 19:04:17 +02001784 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001785 value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
Thierry Reding880cee02017-10-12 19:04:17 +02001786 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001787
1788 /* step 2 */
Thierry Redingc57997b2017-10-12 19:12:57 +02001789 err = tegra_io_pad_power_enable(sor->pad);
Thierry Reding850bab42015-07-29 17:58:41 +02001790 if (err < 0)
Thierry Redingc57997b2017-10-12 19:12:57 +02001791 dev_err(sor->dev, "failed to power on I/O pad: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001792
1793 usleep_range(5, 100);
1794
1795 /* step 3 */
Thierry Reding880cee02017-10-12 19:04:17 +02001796 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001797 value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
Thierry Reding880cee02017-10-12 19:04:17 +02001798 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001799
1800 usleep_range(20, 100);
1801
1802 /* step 4 */
Thierry Reding880cee02017-10-12 19:04:17 +02001803 value = tegra_sor_readl(sor, sor->soc->regs->pll0);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001804 value &= ~SOR_PLL0_VCOPD;
1805 value &= ~SOR_PLL0_PWR;
Thierry Reding880cee02017-10-12 19:04:17 +02001806 tegra_sor_writel(sor, value, sor->soc->regs->pll0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001807
Thierry Reding880cee02017-10-12 19:04:17 +02001808 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001809 value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
Thierry Reding880cee02017-10-12 19:04:17 +02001810 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001811
1812 usleep_range(200, 1000);
1813
1814 /* step 5 */
Thierry Reding880cee02017-10-12 19:04:17 +02001815 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001816 value &= ~SOR_PLL2_PORT_POWERDOWN;
Thierry Reding880cee02017-10-12 19:04:17 +02001817 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001818
Thierry Reding30b49432015-08-03 15:50:32 +02001819 /* XXX not in TRM */
1820 for (value = 0, i = 0; i < 5; i++)
1821 value |= SOR_XBAR_CTRL_LINK0_XSEL(i, sor->soc->xbar_cfg[i]) |
1822 SOR_XBAR_CTRL_LINK1_XSEL(i, i);
1823
1824 tegra_sor_writel(sor, 0x00000000, SOR_XBAR_POL);
1825 tegra_sor_writel(sor, value, SOR_XBAR_CTRL);
1826
Thierry Reding25bb2ce2015-08-03 14:23:29 +02001827 /* switch to DP parent clock */
1828 err = tegra_sor_set_parent_clock(sor, sor->clk_dp);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001829 if (err < 0)
Thierry Reding25bb2ce2015-08-03 14:23:29 +02001830 dev_err(sor->dev, "failed to set parent clock: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001831
Thierry Reding899451b2014-06-05 16:19:48 +02001832 /* power DP lanes */
Thierry Reding880cee02017-10-12 19:04:17 +02001833 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding899451b2014-06-05 16:19:48 +02001834
1835 if (link.num_lanes <= 2)
1836 value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2);
1837 else
1838 value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2;
1839
1840 if (link.num_lanes <= 1)
1841 value &= ~SOR_DP_PADCTL_PD_TXD_1;
1842 else
1843 value |= SOR_DP_PADCTL_PD_TXD_1;
1844
1845 if (link.num_lanes == 0)
1846 value &= ~SOR_DP_PADCTL_PD_TXD_0;
1847 else
1848 value |= SOR_DP_PADCTL_PD_TXD_0;
1849
Thierry Reding880cee02017-10-12 19:04:17 +02001850 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001851
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001852 value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001853 value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
Thierry Reding0c90a182014-06-05 16:29:46 +02001854 value |= SOR_DP_LINKCTL_LANE_COUNT(link.num_lanes);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001855 tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001856
1857 /* start lane sequencer */
1858 value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
1859 SOR_LANE_SEQ_CTL_POWER_STATE_UP;
1860 tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
1861
1862 while (true) {
1863 value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
1864 if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
1865 break;
1866
1867 usleep_range(250, 1000);
1868 }
1869
Thierry Redinga4263fe2014-06-05 16:16:23 +02001870 /* set link bandwidth */
Thierry Reding6b6b6042013-11-15 16:06:05 +01001871 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
1872 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
Thierry Redinga4263fe2014-06-05 16:16:23 +02001873 value |= drm_dp_link_rate_to_bw_code(link.rate) << 2;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001874 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
1875
Thierry Reding402f6bc2015-07-21 16:48:19 +02001876 tegra_sor_apply_config(sor, &config);
1877
1878 /* enable link */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001879 value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001880 value |= SOR_DP_LINKCTL_ENABLE;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001881 value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001882 tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001883
1884 for (i = 0, value = 0; i < 4; i++) {
1885 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
1886 SOR_DP_TPG_SCRAMBLER_GALIOS |
1887 SOR_DP_TPG_PATTERN_NONE;
1888 value = (value << 8) | lane;
1889 }
1890
1891 tegra_sor_writel(sor, value, SOR_DP_TPG);
1892
Thierry Reding6b6b6042013-11-15 16:06:05 +01001893 /* enable pad calibration logic */
Thierry Reding880cee02017-10-12 19:04:17 +02001894 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001895 value |= SOR_DP_PADCTL_PAD_CAL_PD;
Thierry Reding880cee02017-10-12 19:04:17 +02001896 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001897
Thierry Reding01b9bea2015-11-11 17:15:29 +01001898 err = drm_dp_link_probe(sor->aux, &link);
1899 if (err < 0)
1900 dev_err(sor->dev, "failed to probe eDP link: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001901
Thierry Reding01b9bea2015-11-11 17:15:29 +01001902 err = drm_dp_link_power_up(sor->aux, &link);
1903 if (err < 0)
1904 dev_err(sor->dev, "failed to power up eDP link: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001905
Thierry Reding01b9bea2015-11-11 17:15:29 +01001906 err = drm_dp_link_configure(sor->aux, &link);
1907 if (err < 0)
1908 dev_err(sor->dev, "failed to configure eDP link: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001909
Thierry Reding01b9bea2015-11-11 17:15:29 +01001910 rate = drm_dp_link_rate_to_bw_code(link.rate);
1911 lanes = link.num_lanes;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001912
Thierry Reding01b9bea2015-11-11 17:15:29 +01001913 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
1914 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
1915 value |= SOR_CLK_CNTRL_DP_LINK_SPEED(rate);
1916 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001917
Thierry Reding01b9bea2015-11-11 17:15:29 +01001918 value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
1919 value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
1920 value |= SOR_DP_LINKCTL_LANE_COUNT(lanes);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001921
Thierry Reding01b9bea2015-11-11 17:15:29 +01001922 if (link.capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
1923 value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001924
Thierry Reding01b9bea2015-11-11 17:15:29 +01001925 tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001926
Thierry Reding01b9bea2015-11-11 17:15:29 +01001927 /* disable training pattern generator */
Thierry Reding6b6b6042013-11-15 16:06:05 +01001928
Thierry Reding01b9bea2015-11-11 17:15:29 +01001929 for (i = 0; i < link.num_lanes; i++) {
1930 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
1931 SOR_DP_TPG_SCRAMBLER_GALIOS |
1932 SOR_DP_TPG_PATTERN_NONE;
1933 value = (value << 8) | lane;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001934 }
1935
Thierry Reding01b9bea2015-11-11 17:15:29 +01001936 tegra_sor_writel(sor, value, SOR_DP_TPG);
1937
1938 err = tegra_sor_dp_train_fast(sor, &link);
1939 if (err < 0)
1940 dev_err(sor->dev, "DP fast link training failed: %d\n", err);
1941
1942 dev_dbg(sor->dev, "fast link training succeeded\n");
1943
Thierry Reding6b6b6042013-11-15 16:06:05 +01001944 err = tegra_sor_power_up(sor, 250);
Thierry Reding850bab42015-07-29 17:58:41 +02001945 if (err < 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001946 dev_err(sor->dev, "failed to power up SOR: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001947
Thierry Reding6b6b6042013-11-15 16:06:05 +01001948 /* CSTM (LVDS, link A/B, upper) */
Stéphane Marchesin143b1df2014-05-22 20:32:47 -07001949 value = SOR_CSTM_LVDS | SOR_CSTM_LINK_ACT_A | SOR_CSTM_LINK_ACT_B |
Thierry Reding6b6b6042013-11-15 16:06:05 +01001950 SOR_CSTM_UPPER;
1951 tegra_sor_writel(sor, value, SOR_CSTM);
1952
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001953 /* use DP-A protocol */
1954 value = tegra_sor_readl(sor, SOR_STATE1);
1955 value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
1956 value |= SOR_STATE_ASY_PROTOCOL_DP_A;
1957 tegra_sor_writel(sor, value, SOR_STATE1);
1958
Thierry Redingc31efa72015-09-08 16:09:22 +02001959 tegra_sor_mode_set(sor, mode, state);
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001960
Thierry Reding6b6b6042013-11-15 16:06:05 +01001961 /* PWM setup */
1962 err = tegra_sor_setup_pwm(sor, 250);
Thierry Reding850bab42015-07-29 17:58:41 +02001963 if (err < 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001964 dev_err(sor->dev, "failed to setup PWM: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001965
Thierry Reding666cb872014-12-08 16:32:47 +01001966 tegra_sor_update(sor);
1967
Thierry Reding6b6b6042013-11-15 16:06:05 +01001968 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
Thierry Redingc57997b2017-10-12 19:12:57 +02001969 value |= SOR_ENABLE(0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001970 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
1971
Thierry Reding666cb872014-12-08 16:32:47 +01001972 tegra_dc_commit(dc);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001973
1974 err = tegra_sor_attach(sor);
Thierry Reding850bab42015-07-29 17:58:41 +02001975 if (err < 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001976 dev_err(sor->dev, "failed to attach SOR: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001977
1978 err = tegra_sor_wakeup(sor);
Thierry Reding850bab42015-07-29 17:58:41 +02001979 if (err < 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001980 dev_err(sor->dev, "failed to enable DC: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001981
Thierry Reding6fad8f62014-11-28 15:41:34 +01001982 if (output->panel)
1983 drm_panel_enable(output->panel);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001984}
1985
Thierry Reding82f15112014-12-08 17:26:46 +01001986static int
1987tegra_sor_encoder_atomic_check(struct drm_encoder *encoder,
1988 struct drm_crtc_state *crtc_state,
1989 struct drm_connector_state *conn_state)
1990{
1991 struct tegra_output *output = encoder_to_output(encoder);
Thierry Redingc31efa72015-09-08 16:09:22 +02001992 struct tegra_sor_state *state = to_sor_state(conn_state);
Thierry Reding82f15112014-12-08 17:26:46 +01001993 struct tegra_dc *dc = to_tegra_dc(conn_state->crtc);
1994 unsigned long pclk = crtc_state->mode.clock * 1000;
1995 struct tegra_sor *sor = to_sor(output);
Thierry Redingc31efa72015-09-08 16:09:22 +02001996 struct drm_display_info *info;
Thierry Reding82f15112014-12-08 17:26:46 +01001997 int err;
1998
Thierry Redingc31efa72015-09-08 16:09:22 +02001999 info = &output->connector.display_info;
2000
Thierry Reding36e90222017-10-12 19:14:21 +02002001 /*
2002 * For HBR2 modes, the SOR brick needs to use the x20 multiplier, so
2003 * the pixel clock must be corrected accordingly.
2004 */
2005 if (pclk >= 340000000) {
2006 state->link_speed = 20;
2007 state->pclk = pclk / 2;
2008 } else {
2009 state->link_speed = 10;
2010 state->pclk = pclk;
2011 }
2012
Thierry Reding82f15112014-12-08 17:26:46 +01002013 err = tegra_dc_state_setup_clock(dc, crtc_state, sor->clk_parent,
2014 pclk, 0);
2015 if (err < 0) {
2016 dev_err(output->dev, "failed to setup CRTC state: %d\n", err);
2017 return err;
2018 }
2019
Thierry Redingc31efa72015-09-08 16:09:22 +02002020 switch (info->bpc) {
2021 case 8:
2022 case 6:
2023 state->bpc = info->bpc;
2024 break;
2025
2026 default:
2027 DRM_DEBUG_KMS("%u bits-per-color not supported\n", info->bpc);
2028 state->bpc = 8;
2029 break;
2030 }
2031
Thierry Reding82f15112014-12-08 17:26:46 +01002032 return 0;
2033}
2034
Thierry Reding459cc2c2015-07-30 10:34:24 +02002035static const struct drm_encoder_helper_funcs tegra_sor_edp_helpers = {
Thierry Reding850bab42015-07-29 17:58:41 +02002036 .disable = tegra_sor_edp_disable,
2037 .enable = tegra_sor_edp_enable,
Thierry Reding82f15112014-12-08 17:26:46 +01002038 .atomic_check = tegra_sor_encoder_atomic_check,
Thierry Reding6b6b6042013-11-15 16:06:05 +01002039};
2040
Thierry Reding459cc2c2015-07-30 10:34:24 +02002041static inline u32 tegra_sor_hdmi_subpack(const u8 *ptr, size_t size)
2042{
2043 u32 value = 0;
2044 size_t i;
2045
2046 for (i = size; i > 0; i--)
2047 value = (value << 8) | ptr[i - 1];
2048
2049 return value;
2050}
2051
2052static void tegra_sor_hdmi_write_infopack(struct tegra_sor *sor,
2053 const void *data, size_t size)
2054{
2055 const u8 *ptr = data;
2056 unsigned long offset;
2057 size_t i, j;
2058 u32 value;
2059
2060 switch (ptr[0]) {
2061 case HDMI_INFOFRAME_TYPE_AVI:
2062 offset = SOR_HDMI_AVI_INFOFRAME_HEADER;
2063 break;
2064
2065 case HDMI_INFOFRAME_TYPE_AUDIO:
2066 offset = SOR_HDMI_AUDIO_INFOFRAME_HEADER;
2067 break;
2068
2069 case HDMI_INFOFRAME_TYPE_VENDOR:
2070 offset = SOR_HDMI_VSI_INFOFRAME_HEADER;
2071 break;
2072
2073 default:
2074 dev_err(sor->dev, "unsupported infoframe type: %02x\n",
2075 ptr[0]);
2076 return;
2077 }
2078
2079 value = INFOFRAME_HEADER_TYPE(ptr[0]) |
2080 INFOFRAME_HEADER_VERSION(ptr[1]) |
2081 INFOFRAME_HEADER_LEN(ptr[2]);
2082 tegra_sor_writel(sor, value, offset);
2083 offset++;
2084
2085 /*
2086 * Each subpack contains 7 bytes, divided into:
2087 * - subpack_low: bytes 0 - 3
2088 * - subpack_high: bytes 4 - 6 (with byte 7 padded to 0x00)
2089 */
2090 for (i = 3, j = 0; i < size; i += 7, j += 8) {
2091 size_t rem = size - i, num = min_t(size_t, rem, 4);
2092
2093 value = tegra_sor_hdmi_subpack(&ptr[i], num);
2094 tegra_sor_writel(sor, value, offset++);
2095
2096 num = min_t(size_t, rem - num, 3);
2097
2098 value = tegra_sor_hdmi_subpack(&ptr[i + 4], num);
2099 tegra_sor_writel(sor, value, offset++);
2100 }
2101}
2102
2103static int
2104tegra_sor_hdmi_setup_avi_infoframe(struct tegra_sor *sor,
2105 const struct drm_display_mode *mode)
2106{
2107 u8 buffer[HDMI_INFOFRAME_SIZE(AVI)];
2108 struct hdmi_avi_infoframe frame;
2109 u32 value;
2110 int err;
2111
2112 /* disable AVI infoframe */
2113 value = tegra_sor_readl(sor, SOR_HDMI_AVI_INFOFRAME_CTRL);
2114 value &= ~INFOFRAME_CTRL_SINGLE;
2115 value &= ~INFOFRAME_CTRL_OTHER;
2116 value &= ~INFOFRAME_CTRL_ENABLE;
2117 tegra_sor_writel(sor, value, SOR_HDMI_AVI_INFOFRAME_CTRL);
2118
Ville Syrjälä13d0add2019-01-08 19:28:25 +02002119 err = drm_hdmi_avi_infoframe_from_display_mode(&frame,
2120 &sor->output.connector, mode);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002121 if (err < 0) {
2122 dev_err(sor->dev, "failed to setup AVI infoframe: %d\n", err);
2123 return err;
2124 }
2125
2126 err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
2127 if (err < 0) {
2128 dev_err(sor->dev, "failed to pack AVI infoframe: %d\n", err);
2129 return err;
2130 }
2131
2132 tegra_sor_hdmi_write_infopack(sor, buffer, err);
2133
2134 /* enable AVI infoframe */
2135 value = tegra_sor_readl(sor, SOR_HDMI_AVI_INFOFRAME_CTRL);
2136 value |= INFOFRAME_CTRL_CHECKSUM_ENABLE;
2137 value |= INFOFRAME_CTRL_ENABLE;
2138 tegra_sor_writel(sor, value, SOR_HDMI_AVI_INFOFRAME_CTRL);
2139
2140 return 0;
2141}
2142
2143static void tegra_sor_hdmi_disable_audio_infoframe(struct tegra_sor *sor)
2144{
2145 u32 value;
2146
2147 value = tegra_sor_readl(sor, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
2148 value &= ~INFOFRAME_CTRL_ENABLE;
2149 tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
2150}
2151
2152static struct tegra_sor_hdmi_settings *
2153tegra_sor_hdmi_find_settings(struct tegra_sor *sor, unsigned long frequency)
2154{
2155 unsigned int i;
2156
2157 for (i = 0; i < sor->num_settings; i++)
2158 if (frequency <= sor->settings[i].frequency)
2159 return &sor->settings[i];
2160
2161 return NULL;
2162}
2163
Thierry Reding36e90222017-10-12 19:14:21 +02002164static void tegra_sor_hdmi_disable_scrambling(struct tegra_sor *sor)
2165{
2166 u32 value;
2167
2168 value = tegra_sor_readl(sor, SOR_HDMI2_CTRL);
2169 value &= ~SOR_HDMI2_CTRL_CLOCK_MODE_DIV_BY_4;
2170 value &= ~SOR_HDMI2_CTRL_SCRAMBLE;
2171 tegra_sor_writel(sor, value, SOR_HDMI2_CTRL);
2172}
2173
2174static void tegra_sor_hdmi_scdc_disable(struct tegra_sor *sor)
2175{
2176 struct i2c_adapter *ddc = sor->output.ddc;
2177
2178 drm_scdc_set_high_tmds_clock_ratio(ddc, false);
2179 drm_scdc_set_scrambling(ddc, false);
2180
2181 tegra_sor_hdmi_disable_scrambling(sor);
2182}
2183
2184static void tegra_sor_hdmi_scdc_stop(struct tegra_sor *sor)
2185{
2186 if (sor->scdc_enabled) {
2187 cancel_delayed_work_sync(&sor->scdc);
2188 tegra_sor_hdmi_scdc_disable(sor);
2189 }
2190}
2191
2192static void tegra_sor_hdmi_enable_scrambling(struct tegra_sor *sor)
2193{
2194 u32 value;
2195
2196 value = tegra_sor_readl(sor, SOR_HDMI2_CTRL);
2197 value |= SOR_HDMI2_CTRL_CLOCK_MODE_DIV_BY_4;
2198 value |= SOR_HDMI2_CTRL_SCRAMBLE;
2199 tegra_sor_writel(sor, value, SOR_HDMI2_CTRL);
2200}
2201
2202static void tegra_sor_hdmi_scdc_enable(struct tegra_sor *sor)
2203{
2204 struct i2c_adapter *ddc = sor->output.ddc;
2205
2206 drm_scdc_set_high_tmds_clock_ratio(ddc, true);
2207 drm_scdc_set_scrambling(ddc, true);
2208
2209 tegra_sor_hdmi_enable_scrambling(sor);
2210}
2211
2212static void tegra_sor_hdmi_scdc_work(struct work_struct *work)
2213{
2214 struct tegra_sor *sor = container_of(work, struct tegra_sor, scdc.work);
2215 struct i2c_adapter *ddc = sor->output.ddc;
2216
2217 if (!drm_scdc_get_scrambling_status(ddc)) {
2218 DRM_DEBUG_KMS("SCDC not scrambled\n");
2219 tegra_sor_hdmi_scdc_enable(sor);
2220 }
2221
2222 schedule_delayed_work(&sor->scdc, msecs_to_jiffies(5000));
2223}
2224
2225static void tegra_sor_hdmi_scdc_start(struct tegra_sor *sor)
2226{
2227 struct drm_scdc *scdc = &sor->output.connector.display_info.hdmi.scdc;
2228 struct drm_display_mode *mode;
2229
2230 mode = &sor->output.encoder.crtc->state->adjusted_mode;
2231
2232 if (mode->clock >= 340000 && scdc->supported) {
2233 schedule_delayed_work(&sor->scdc, msecs_to_jiffies(5000));
2234 tegra_sor_hdmi_scdc_enable(sor);
2235 sor->scdc_enabled = true;
2236 }
2237}
2238
Thierry Reding459cc2c2015-07-30 10:34:24 +02002239static void tegra_sor_hdmi_disable(struct drm_encoder *encoder)
2240{
2241 struct tegra_output *output = encoder_to_output(encoder);
2242 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
2243 struct tegra_sor *sor = to_sor(output);
2244 u32 value;
2245 int err;
2246
Thierry Reding36e90222017-10-12 19:14:21 +02002247 tegra_sor_hdmi_scdc_stop(sor);
2248
Thierry Reding459cc2c2015-07-30 10:34:24 +02002249 err = tegra_sor_detach(sor);
2250 if (err < 0)
2251 dev_err(sor->dev, "failed to detach SOR: %d\n", err);
2252
2253 tegra_sor_writel(sor, 0, SOR_STATE1);
2254 tegra_sor_update(sor);
2255
2256 /* disable display to SOR clock */
2257 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
Thierry Redingc57997b2017-10-12 19:12:57 +02002258
2259 if (!sor->soc->has_nvdisplay)
2260 value &= ~(SOR1_TIMING_CYA | SOR_ENABLE(1));
2261 else
2262 value &= ~SOR_ENABLE(sor->index);
2263
Thierry Reding459cc2c2015-07-30 10:34:24 +02002264 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
2265
2266 tegra_dc_commit(dc);
2267
2268 err = tegra_sor_power_down(sor);
2269 if (err < 0)
2270 dev_err(sor->dev, "failed to power down SOR: %d\n", err);
2271
Thierry Redingc57997b2017-10-12 19:12:57 +02002272 err = tegra_io_pad_power_disable(sor->pad);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002273 if (err < 0)
Thierry Redingc57997b2017-10-12 19:12:57 +02002274 dev_err(sor->dev, "failed to power off I/O pad: %d\n", err);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002275
Thierry Redingaaff8bd2015-08-07 16:04:54 +02002276 pm_runtime_put(sor->dev);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002277}
2278
2279static void tegra_sor_hdmi_enable(struct drm_encoder *encoder)
2280{
2281 struct tegra_output *output = encoder_to_output(encoder);
2282 unsigned int h_ref_to_sync = 1, pulse_start, max_ac;
2283 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002284 struct tegra_sor_hdmi_settings *settings;
2285 struct tegra_sor *sor = to_sor(output);
Thierry Redingc31efa72015-09-08 16:09:22 +02002286 struct tegra_sor_state *state;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002287 struct drm_display_mode *mode;
Thierry Reding36e90222017-10-12 19:14:21 +02002288 unsigned long rate, pclk;
Thierry Reding30b49432015-08-03 15:50:32 +02002289 unsigned int div, i;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002290 u32 value;
2291 int err;
2292
Thierry Redingc31efa72015-09-08 16:09:22 +02002293 state = to_sor_state(output->connector.state);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002294 mode = &encoder->crtc->state->adjusted_mode;
Thierry Reding36e90222017-10-12 19:14:21 +02002295 pclk = mode->clock * 1000;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002296
Thierry Redingaaff8bd2015-08-07 16:04:54 +02002297 pm_runtime_get_sync(sor->dev);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002298
Thierry Reding25bb2ce2015-08-03 14:23:29 +02002299 /* switch to safe parent clock */
2300 err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
Thierry Redinge1335e22017-10-12 17:53:11 +02002301 if (err < 0) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002302 dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
Thierry Redinge1335e22017-10-12 17:53:11 +02002303 return;
2304 }
Thierry Reding459cc2c2015-07-30 10:34:24 +02002305
2306 div = clk_get_rate(sor->clk) / 1000000 * 4;
2307
Thierry Redingc57997b2017-10-12 19:12:57 +02002308 err = tegra_io_pad_power_enable(sor->pad);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002309 if (err < 0)
Thierry Redingc57997b2017-10-12 19:12:57 +02002310 dev_err(sor->dev, "failed to power on I/O pad: %d\n", err);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002311
2312 usleep_range(20, 100);
2313
Thierry Reding880cee02017-10-12 19:04:17 +02002314 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002315 value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
Thierry Reding880cee02017-10-12 19:04:17 +02002316 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002317
2318 usleep_range(20, 100);
2319
Thierry Reding880cee02017-10-12 19:04:17 +02002320 value = tegra_sor_readl(sor, sor->soc->regs->pll3);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002321 value &= ~SOR_PLL3_PLL_VDD_MODE_3V3;
Thierry Reding880cee02017-10-12 19:04:17 +02002322 tegra_sor_writel(sor, value, sor->soc->regs->pll3);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002323
Thierry Reding880cee02017-10-12 19:04:17 +02002324 value = tegra_sor_readl(sor, sor->soc->regs->pll0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002325 value &= ~SOR_PLL0_VCOPD;
2326 value &= ~SOR_PLL0_PWR;
Thierry Reding880cee02017-10-12 19:04:17 +02002327 tegra_sor_writel(sor, value, sor->soc->regs->pll0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002328
Thierry Reding880cee02017-10-12 19:04:17 +02002329 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002330 value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
Thierry Reding880cee02017-10-12 19:04:17 +02002331 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002332
2333 usleep_range(200, 400);
2334
Thierry Reding880cee02017-10-12 19:04:17 +02002335 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002336 value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
2337 value &= ~SOR_PLL2_PORT_POWERDOWN;
Thierry Reding880cee02017-10-12 19:04:17 +02002338 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002339
2340 usleep_range(20, 100);
2341
Thierry Reding880cee02017-10-12 19:04:17 +02002342 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002343 value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
2344 SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2;
Thierry Reding880cee02017-10-12 19:04:17 +02002345 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002346
2347 while (true) {
2348 value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
2349 if ((value & SOR_LANE_SEQ_CTL_STATE_BUSY) == 0)
2350 break;
2351
2352 usleep_range(250, 1000);
2353 }
2354
2355 value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
2356 SOR_LANE_SEQ_CTL_POWER_STATE_UP | SOR_LANE_SEQ_CTL_DELAY(5);
2357 tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
2358
2359 while (true) {
2360 value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
2361 if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
2362 break;
2363
2364 usleep_range(250, 1000);
2365 }
2366
2367 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
2368 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
2369 value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
2370
Thierry Reding36e90222017-10-12 19:14:21 +02002371 if (mode->clock < 340000) {
2372 DRM_DEBUG_KMS("setting 2.7 GHz link speed\n");
Thierry Reding459cc2c2015-07-30 10:34:24 +02002373 value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G2_70;
Thierry Reding36e90222017-10-12 19:14:21 +02002374 } else {
2375 DRM_DEBUG_KMS("setting 5.4 GHz link speed\n");
Thierry Reding459cc2c2015-07-30 10:34:24 +02002376 value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G5_40;
Thierry Reding36e90222017-10-12 19:14:21 +02002377 }
Thierry Reding459cc2c2015-07-30 10:34:24 +02002378
2379 value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK;
2380 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
2381
Thierry Redingc57997b2017-10-12 19:12:57 +02002382 /* SOR pad PLL stabilization time */
2383 usleep_range(250, 1000);
2384
2385 value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
2386 value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
2387 value |= SOR_DP_LINKCTL_LANE_COUNT(4);
2388 tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
2389
Thierry Reding459cc2c2015-07-30 10:34:24 +02002390 value = tegra_sor_readl(sor, SOR_DP_SPARE0);
Thierry Redingc57997b2017-10-12 19:12:57 +02002391 value &= ~SOR_DP_SPARE_DISP_VIDEO_PREAMBLE;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002392 value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
Thierry Redingc57997b2017-10-12 19:12:57 +02002393 value &= ~SOR_DP_SPARE_SEQ_ENABLE;
2394 value &= ~SOR_DP_SPARE_MACRO_SOR_CLK;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002395 tegra_sor_writel(sor, value, SOR_DP_SPARE0);
2396
2397 value = SOR_SEQ_CTL_PU_PC(0) | SOR_SEQ_CTL_PU_PC_ALT(0) |
2398 SOR_SEQ_CTL_PD_PC(8) | SOR_SEQ_CTL_PD_PC_ALT(8);
2399 tegra_sor_writel(sor, value, SOR_SEQ_CTL);
2400
2401 value = SOR_SEQ_INST_DRIVE_PWM_OUT_LO | SOR_SEQ_INST_HALT |
2402 SOR_SEQ_INST_WAIT_VSYNC | SOR_SEQ_INST_WAIT(1);
2403 tegra_sor_writel(sor, value, SOR_SEQ_INST(0));
2404 tegra_sor_writel(sor, value, SOR_SEQ_INST(8));
2405
Thierry Redingc57997b2017-10-12 19:12:57 +02002406 if (!sor->soc->has_nvdisplay) {
2407 /* program the reference clock */
2408 value = SOR_REFCLK_DIV_INT(div) | SOR_REFCLK_DIV_FRAC(div);
2409 tegra_sor_writel(sor, value, SOR_REFCLK);
2410 }
Thierry Reding459cc2c2015-07-30 10:34:24 +02002411
Thierry Reding30b49432015-08-03 15:50:32 +02002412 /* XXX not in TRM */
2413 for (value = 0, i = 0; i < 5; i++)
2414 value |= SOR_XBAR_CTRL_LINK0_XSEL(i, sor->soc->xbar_cfg[i]) |
2415 SOR_XBAR_CTRL_LINK1_XSEL(i, i);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002416
2417 tegra_sor_writel(sor, 0x00000000, SOR_XBAR_POL);
Thierry Reding30b49432015-08-03 15:50:32 +02002418 tegra_sor_writel(sor, value, SOR_XBAR_CTRL);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002419
Thierry Reding25bb2ce2015-08-03 14:23:29 +02002420 /* switch to parent clock */
Thierry Redinge1335e22017-10-12 17:53:11 +02002421 err = clk_set_parent(sor->clk, sor->clk_parent);
2422 if (err < 0) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002423 dev_err(sor->dev, "failed to set parent clock: %d\n", err);
Thierry Redinge1335e22017-10-12 17:53:11 +02002424 return;
2425 }
2426
2427 err = tegra_sor_set_parent_clock(sor, sor->clk_pad);
2428 if (err < 0) {
2429 dev_err(sor->dev, "failed to set pad clock: %d\n", err);
2430 return;
2431 }
Thierry Reding459cc2c2015-07-30 10:34:24 +02002432
Thierry Reding36e90222017-10-12 19:14:21 +02002433 /* adjust clock rate for HDMI 2.0 modes */
2434 rate = clk_get_rate(sor->clk_parent);
2435
2436 if (mode->clock >= 340000)
2437 rate /= 2;
2438
2439 DRM_DEBUG_KMS("setting clock to %lu Hz, mode: %lu Hz\n", rate, pclk);
2440
2441 clk_set_rate(sor->clk, rate);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002442
Thierry Redingc57997b2017-10-12 19:12:57 +02002443 if (!sor->soc->has_nvdisplay) {
2444 value = SOR_INPUT_CONTROL_HDMI_SRC_SELECT(dc->pipe);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002445
Thierry Redingc57997b2017-10-12 19:12:57 +02002446 /* XXX is this the proper check? */
2447 if (mode->clock < 75000)
2448 value |= SOR_INPUT_CONTROL_ARM_VIDEO_RANGE_LIMITED;
2449
2450 tegra_sor_writel(sor, value, SOR_INPUT_CONTROL);
2451 }
Thierry Reding459cc2c2015-07-30 10:34:24 +02002452
2453 max_ac = ((mode->htotal - mode->hdisplay) - SOR_REKEY - 18) / 32;
2454
2455 value = SOR_HDMI_CTRL_ENABLE | SOR_HDMI_CTRL_MAX_AC_PACKET(max_ac) |
2456 SOR_HDMI_CTRL_AUDIO_LAYOUT | SOR_HDMI_CTRL_REKEY(SOR_REKEY);
2457 tegra_sor_writel(sor, value, SOR_HDMI_CTRL);
2458
Thierry Redingc57997b2017-10-12 19:12:57 +02002459 if (!dc->soc->has_nvdisplay) {
2460 /* H_PULSE2 setup */
2461 pulse_start = h_ref_to_sync +
2462 (mode->hsync_end - mode->hsync_start) +
2463 (mode->htotal - mode->hsync_end) - 10;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002464
Thierry Redingc57997b2017-10-12 19:12:57 +02002465 value = PULSE_LAST_END_A | PULSE_QUAL_VACTIVE |
2466 PULSE_POLARITY_HIGH | PULSE_MODE_NORMAL;
2467 tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_CONTROL);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002468
Thierry Redingc57997b2017-10-12 19:12:57 +02002469 value = PULSE_END(pulse_start + 8) | PULSE_START(pulse_start);
2470 tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_POSITION_A);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002471
Thierry Redingc57997b2017-10-12 19:12:57 +02002472 value = tegra_dc_readl(dc, DC_DISP_DISP_SIGNAL_OPTIONS0);
2473 value |= H_PULSE2_ENABLE;
2474 tegra_dc_writel(dc, value, DC_DISP_DISP_SIGNAL_OPTIONS0);
2475 }
Thierry Reding459cc2c2015-07-30 10:34:24 +02002476
2477 /* infoframe setup */
2478 err = tegra_sor_hdmi_setup_avi_infoframe(sor, mode);
2479 if (err < 0)
2480 dev_err(sor->dev, "failed to setup AVI infoframe: %d\n", err);
2481
2482 /* XXX HDMI audio support not implemented yet */
2483 tegra_sor_hdmi_disable_audio_infoframe(sor);
2484
2485 /* use single TMDS protocol */
2486 value = tegra_sor_readl(sor, SOR_STATE1);
2487 value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
2488 value |= SOR_STATE_ASY_PROTOCOL_SINGLE_TMDS_A;
2489 tegra_sor_writel(sor, value, SOR_STATE1);
2490
2491 /* power up pad calibration */
Thierry Reding880cee02017-10-12 19:04:17 +02002492 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002493 value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
Thierry Reding880cee02017-10-12 19:04:17 +02002494 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002495
2496 /* production settings */
2497 settings = tegra_sor_hdmi_find_settings(sor, mode->clock * 1000);
Dan Carpenterdb8b42f2015-08-17 17:37:03 +03002498 if (!settings) {
2499 dev_err(sor->dev, "no settings for pixel clock %d Hz\n",
2500 mode->clock * 1000);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002501 return;
2502 }
2503
Thierry Reding880cee02017-10-12 19:04:17 +02002504 value = tegra_sor_readl(sor, sor->soc->regs->pll0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002505 value &= ~SOR_PLL0_ICHPMP_MASK;
Thierry Redingc57997b2017-10-12 19:12:57 +02002506 value &= ~SOR_PLL0_FILTER_MASK;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002507 value &= ~SOR_PLL0_VCOCAP_MASK;
2508 value |= SOR_PLL0_ICHPMP(settings->ichpmp);
Thierry Redingc57997b2017-10-12 19:12:57 +02002509 value |= SOR_PLL0_FILTER(settings->filter);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002510 value |= SOR_PLL0_VCOCAP(settings->vcocap);
Thierry Reding880cee02017-10-12 19:04:17 +02002511 tegra_sor_writel(sor, value, sor->soc->regs->pll0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002512
Thierry Redingc57997b2017-10-12 19:12:57 +02002513 /* XXX not in TRM */
Thierry Reding880cee02017-10-12 19:04:17 +02002514 value = tegra_sor_readl(sor, sor->soc->regs->pll1);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002515 value &= ~SOR_PLL1_LOADADJ_MASK;
Thierry Redingc57997b2017-10-12 19:12:57 +02002516 value &= ~SOR_PLL1_TMDS_TERMADJ_MASK;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002517 value |= SOR_PLL1_LOADADJ(settings->loadadj);
Thierry Redingc57997b2017-10-12 19:12:57 +02002518 value |= SOR_PLL1_TMDS_TERMADJ(settings->tmds_termadj);
2519 value |= SOR_PLL1_TMDS_TERM;
Thierry Reding880cee02017-10-12 19:04:17 +02002520 tegra_sor_writel(sor, value, sor->soc->regs->pll1);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002521
Thierry Reding880cee02017-10-12 19:04:17 +02002522 value = tegra_sor_readl(sor, sor->soc->regs->pll3);
Thierry Redingc57997b2017-10-12 19:12:57 +02002523 value &= ~SOR_PLL3_BG_TEMP_COEF_MASK;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002524 value &= ~SOR_PLL3_BG_VREF_LEVEL_MASK;
Thierry Redingc57997b2017-10-12 19:12:57 +02002525 value &= ~SOR_PLL3_AVDD10_LEVEL_MASK;
2526 value &= ~SOR_PLL3_AVDD14_LEVEL_MASK;
2527 value |= SOR_PLL3_BG_TEMP_COEF(settings->bg_temp_coef);
2528 value |= SOR_PLL3_BG_VREF_LEVEL(settings->bg_vref_level);
2529 value |= SOR_PLL3_AVDD10_LEVEL(settings->avdd10_level);
2530 value |= SOR_PLL3_AVDD14_LEVEL(settings->avdd14_level);
Thierry Reding880cee02017-10-12 19:04:17 +02002531 tegra_sor_writel(sor, value, sor->soc->regs->pll3);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002532
Thierry Redingc57997b2017-10-12 19:12:57 +02002533 value = settings->drive_current[3] << 24 |
2534 settings->drive_current[2] << 16 |
2535 settings->drive_current[1] << 8 |
2536 settings->drive_current[0] << 0;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002537 tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT0);
2538
Thierry Redingc57997b2017-10-12 19:12:57 +02002539 value = settings->preemphasis[3] << 24 |
2540 settings->preemphasis[2] << 16 |
2541 settings->preemphasis[1] << 8 |
2542 settings->preemphasis[0] << 0;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002543 tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS0);
2544
Thierry Reding880cee02017-10-12 19:04:17 +02002545 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002546 value &= ~SOR_DP_PADCTL_TX_PU_MASK;
2547 value |= SOR_DP_PADCTL_TX_PU_ENABLE;
Thierry Redingc57997b2017-10-12 19:12:57 +02002548 value |= SOR_DP_PADCTL_TX_PU(settings->tx_pu_value);
Thierry Reding880cee02017-10-12 19:04:17 +02002549 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002550
Thierry Redingc57997b2017-10-12 19:12:57 +02002551 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl2);
2552 value &= ~SOR_DP_PADCTL_SPAREPLL_MASK;
2553 value |= SOR_DP_PADCTL_SPAREPLL(settings->sparepll);
2554 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl2);
2555
Thierry Reding459cc2c2015-07-30 10:34:24 +02002556 /* power down pad calibration */
Thierry Reding880cee02017-10-12 19:04:17 +02002557 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002558 value |= SOR_DP_PADCTL_PAD_CAL_PD;
Thierry Reding880cee02017-10-12 19:04:17 +02002559 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002560
Thierry Redingc57997b2017-10-12 19:12:57 +02002561 if (!dc->soc->has_nvdisplay) {
2562 /* miscellaneous display controller settings */
2563 value = VSYNC_H_POSITION(1);
2564 tegra_dc_writel(dc, value, DC_DISP_DISP_TIMING_OPTIONS);
2565 }
Thierry Reding459cc2c2015-07-30 10:34:24 +02002566
2567 value = tegra_dc_readl(dc, DC_DISP_DISP_COLOR_CONTROL);
2568 value &= ~DITHER_CONTROL_MASK;
2569 value &= ~BASE_COLOR_SIZE_MASK;
2570
Thierry Redingc31efa72015-09-08 16:09:22 +02002571 switch (state->bpc) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002572 case 6:
2573 value |= BASE_COLOR_SIZE_666;
2574 break;
2575
2576 case 8:
2577 value |= BASE_COLOR_SIZE_888;
2578 break;
2579
Thierry Redingc57997b2017-10-12 19:12:57 +02002580 case 10:
2581 value |= BASE_COLOR_SIZE_101010;
2582 break;
2583
2584 case 12:
2585 value |= BASE_COLOR_SIZE_121212;
2586 break;
2587
Thierry Reding459cc2c2015-07-30 10:34:24 +02002588 default:
Thierry Redingc31efa72015-09-08 16:09:22 +02002589 WARN(1, "%u bits-per-color not supported\n", state->bpc);
2590 value |= BASE_COLOR_SIZE_888;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002591 break;
2592 }
2593
2594 tegra_dc_writel(dc, value, DC_DISP_DISP_COLOR_CONTROL);
2595
Thierry Redingc57997b2017-10-12 19:12:57 +02002596 /* XXX set display head owner */
2597 value = tegra_sor_readl(sor, SOR_STATE1);
2598 value &= ~SOR_STATE_ASY_OWNER_MASK;
2599 value |= SOR_STATE_ASY_OWNER(1 + dc->pipe);
2600 tegra_sor_writel(sor, value, SOR_STATE1);
2601
Thierry Reding459cc2c2015-07-30 10:34:24 +02002602 err = tegra_sor_power_up(sor, 250);
2603 if (err < 0)
2604 dev_err(sor->dev, "failed to power up SOR: %d\n", err);
2605
Thierry Reding2bd1dd32015-08-03 15:46:15 +02002606 /* configure dynamic range of output */
Thierry Reding880cee02017-10-12 19:04:17 +02002607 value = tegra_sor_readl(sor, sor->soc->regs->head_state0 + dc->pipe);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002608 value &= ~SOR_HEAD_STATE_RANGECOMPRESS_MASK;
2609 value &= ~SOR_HEAD_STATE_DYNRANGE_MASK;
Thierry Reding880cee02017-10-12 19:04:17 +02002610 tegra_sor_writel(sor, value, sor->soc->regs->head_state0 + dc->pipe);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002611
Thierry Reding2bd1dd32015-08-03 15:46:15 +02002612 /* configure colorspace */
Thierry Reding880cee02017-10-12 19:04:17 +02002613 value = tegra_sor_readl(sor, sor->soc->regs->head_state0 + dc->pipe);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002614 value &= ~SOR_HEAD_STATE_COLORSPACE_MASK;
2615 value |= SOR_HEAD_STATE_COLORSPACE_RGB;
Thierry Reding880cee02017-10-12 19:04:17 +02002616 tegra_sor_writel(sor, value, sor->soc->regs->head_state0 + dc->pipe);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002617
Thierry Redingc31efa72015-09-08 16:09:22 +02002618 tegra_sor_mode_set(sor, mode, state);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002619
2620 tegra_sor_update(sor);
2621
Thierry Redingc57997b2017-10-12 19:12:57 +02002622 /* program preamble timing in SOR (XXX) */
2623 value = tegra_sor_readl(sor, SOR_DP_SPARE0);
2624 value &= ~SOR_DP_SPARE_DISP_VIDEO_PREAMBLE;
2625 tegra_sor_writel(sor, value, SOR_DP_SPARE0);
2626
Thierry Reding459cc2c2015-07-30 10:34:24 +02002627 err = tegra_sor_attach(sor);
2628 if (err < 0)
2629 dev_err(sor->dev, "failed to attach SOR: %d\n", err);
2630
2631 /* enable display to SOR clock and generate HDMI preamble */
2632 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
Thierry Redingc57997b2017-10-12 19:12:57 +02002633
2634 if (!sor->soc->has_nvdisplay)
2635 value |= SOR_ENABLE(1) | SOR1_TIMING_CYA;
2636 else
2637 value |= SOR_ENABLE(sor->index);
2638
Thierry Reding459cc2c2015-07-30 10:34:24 +02002639 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
2640
Thierry Redingc57997b2017-10-12 19:12:57 +02002641 if (dc->soc->has_nvdisplay) {
2642 value = tegra_dc_readl(dc, DC_DISP_CORE_SOR_SET_CONTROL(sor->index));
2643 value &= ~PROTOCOL_MASK;
2644 value |= PROTOCOL_SINGLE_TMDS_A;
2645 tegra_dc_writel(dc, value, DC_DISP_CORE_SOR_SET_CONTROL(sor->index));
2646 }
2647
Thierry Reding459cc2c2015-07-30 10:34:24 +02002648 tegra_dc_commit(dc);
2649
2650 err = tegra_sor_wakeup(sor);
2651 if (err < 0)
2652 dev_err(sor->dev, "failed to wakeup SOR: %d\n", err);
Thierry Reding36e90222017-10-12 19:14:21 +02002653
2654 tegra_sor_hdmi_scdc_start(sor);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002655}
2656
2657static const struct drm_encoder_helper_funcs tegra_sor_hdmi_helpers = {
2658 .disable = tegra_sor_hdmi_disable,
2659 .enable = tegra_sor_hdmi_enable,
2660 .atomic_check = tegra_sor_encoder_atomic_check,
2661};
2662
Thierry Reding6b6b6042013-11-15 16:06:05 +01002663static int tegra_sor_init(struct host1x_client *client)
2664{
Thierry Reding9910f5c2014-05-22 09:57:15 +02002665 struct drm_device *drm = dev_get_drvdata(client->parent);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002666 const struct drm_encoder_helper_funcs *helpers = NULL;
Thierry Reding6b6b6042013-11-15 16:06:05 +01002667 struct tegra_sor *sor = host1x_client_to_sor(client);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002668 int connector = DRM_MODE_CONNECTOR_Unknown;
2669 int encoder = DRM_MODE_ENCODER_NONE;
Thierry Reding6b6b6042013-11-15 16:06:05 +01002670 int err;
2671
Thierry Reding9542c232015-07-08 13:39:09 +02002672 if (!sor->aux) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002673 if (sor->soc->supports_hdmi) {
2674 connector = DRM_MODE_CONNECTOR_HDMIA;
2675 encoder = DRM_MODE_ENCODER_TMDS;
2676 helpers = &tegra_sor_hdmi_helpers;
2677 } else if (sor->soc->supports_lvds) {
2678 connector = DRM_MODE_CONNECTOR_LVDS;
2679 encoder = DRM_MODE_ENCODER_LVDS;
2680 }
2681 } else {
2682 if (sor->soc->supports_edp) {
2683 connector = DRM_MODE_CONNECTOR_eDP;
2684 encoder = DRM_MODE_ENCODER_TMDS;
2685 helpers = &tegra_sor_edp_helpers;
2686 } else if (sor->soc->supports_dp) {
2687 connector = DRM_MODE_CONNECTOR_DisplayPort;
2688 encoder = DRM_MODE_ENCODER_TMDS;
2689 }
2690 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01002691
Thierry Reding6b6b6042013-11-15 16:06:05 +01002692 sor->output.dev = sor->dev;
Thierry Reding6b6b6042013-11-15 16:06:05 +01002693
Thierry Reding6fad8f62014-11-28 15:41:34 +01002694 drm_connector_init(drm, &sor->output.connector,
2695 &tegra_sor_connector_funcs,
Thierry Reding459cc2c2015-07-30 10:34:24 +02002696 connector);
Thierry Reding6fad8f62014-11-28 15:41:34 +01002697 drm_connector_helper_add(&sor->output.connector,
2698 &tegra_sor_connector_helper_funcs);
2699 sor->output.connector.dpms = DRM_MODE_DPMS_OFF;
2700
Thierry Reding6fad8f62014-11-28 15:41:34 +01002701 drm_encoder_init(drm, &sor->output.encoder, &tegra_sor_encoder_funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +02002702 encoder, NULL);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002703 drm_encoder_helper_add(&sor->output.encoder, helpers);
Thierry Reding6fad8f62014-11-28 15:41:34 +01002704
Daniel Vettercde4c442018-07-09 10:40:07 +02002705 drm_connector_attach_encoder(&sor->output.connector,
Thierry Reding6fad8f62014-11-28 15:41:34 +01002706 &sor->output.encoder);
2707 drm_connector_register(&sor->output.connector);
2708
Thierry Redingea130b22014-12-19 15:51:35 +01002709 err = tegra_output_init(drm, &sor->output);
2710 if (err < 0) {
2711 dev_err(client->dev, "failed to initialize output: %d\n", err);
2712 return err;
2713 }
Thierry Reding6fad8f62014-11-28 15:41:34 +01002714
Thierry Redingc57997b2017-10-12 19:12:57 +02002715 tegra_output_find_possible_crtcs(&sor->output, drm);
Thierry Reding6b6b6042013-11-15 16:06:05 +01002716
Thierry Reding9542c232015-07-08 13:39:09 +02002717 if (sor->aux) {
2718 err = drm_dp_aux_attach(sor->aux, &sor->output);
Thierry Reding6b6b6042013-11-15 16:06:05 +01002719 if (err < 0) {
2720 dev_err(sor->dev, "failed to attach DP: %d\n", err);
2721 return err;
2722 }
2723 }
2724
Tomeu Vizoso535a65d2015-03-30 10:33:03 +02002725 /*
2726 * XXX: Remove this reset once proper hand-over from firmware to
2727 * kernel is possible.
2728 */
Jon Hunterf8c79122016-07-01 14:21:38 +01002729 if (sor->rst) {
2730 err = reset_control_assert(sor->rst);
2731 if (err < 0) {
2732 dev_err(sor->dev, "failed to assert SOR reset: %d\n",
2733 err);
2734 return err;
2735 }
Tomeu Vizoso535a65d2015-03-30 10:33:03 +02002736 }
2737
Thierry Reding6fad8f62014-11-28 15:41:34 +01002738 err = clk_prepare_enable(sor->clk);
2739 if (err < 0) {
2740 dev_err(sor->dev, "failed to enable clock: %d\n", err);
2741 return err;
2742 }
2743
Tomeu Vizoso535a65d2015-03-30 10:33:03 +02002744 usleep_range(1000, 3000);
2745
Jon Hunterf8c79122016-07-01 14:21:38 +01002746 if (sor->rst) {
2747 err = reset_control_deassert(sor->rst);
2748 if (err < 0) {
2749 dev_err(sor->dev, "failed to deassert SOR reset: %d\n",
2750 err);
2751 return err;
2752 }
Tomeu Vizoso535a65d2015-03-30 10:33:03 +02002753 }
2754
Thierry Reding6fad8f62014-11-28 15:41:34 +01002755 err = clk_prepare_enable(sor->clk_safe);
2756 if (err < 0)
2757 return err;
2758
2759 err = clk_prepare_enable(sor->clk_dp);
2760 if (err < 0)
2761 return err;
2762
Thierry Reding6b6b6042013-11-15 16:06:05 +01002763 return 0;
2764}
2765
2766static int tegra_sor_exit(struct host1x_client *client)
2767{
2768 struct tegra_sor *sor = host1x_client_to_sor(client);
2769 int err;
2770
Thierry Reding328ec692014-12-19 15:55:08 +01002771 tegra_output_exit(&sor->output);
2772
Thierry Reding9542c232015-07-08 13:39:09 +02002773 if (sor->aux) {
2774 err = drm_dp_aux_detach(sor->aux);
Thierry Reding6b6b6042013-11-15 16:06:05 +01002775 if (err < 0) {
2776 dev_err(sor->dev, "failed to detach DP: %d\n", err);
2777 return err;
2778 }
2779 }
2780
Thierry Reding6fad8f62014-11-28 15:41:34 +01002781 clk_disable_unprepare(sor->clk_safe);
2782 clk_disable_unprepare(sor->clk_dp);
2783 clk_disable_unprepare(sor->clk);
2784
Thierry Reding6b6b6042013-11-15 16:06:05 +01002785 return 0;
2786}
2787
2788static const struct host1x_client_ops sor_client_ops = {
2789 .init = tegra_sor_init,
2790 .exit = tegra_sor_exit,
2791};
2792
Thierry Reding459cc2c2015-07-30 10:34:24 +02002793static const struct tegra_sor_ops tegra_sor_edp_ops = {
2794 .name = "eDP",
2795};
2796
2797static int tegra_sor_hdmi_probe(struct tegra_sor *sor)
2798{
2799 int err;
2800
2801 sor->avdd_io_supply = devm_regulator_get(sor->dev, "avdd-io");
2802 if (IS_ERR(sor->avdd_io_supply)) {
2803 dev_err(sor->dev, "cannot get AVDD I/O supply: %ld\n",
2804 PTR_ERR(sor->avdd_io_supply));
2805 return PTR_ERR(sor->avdd_io_supply);
2806 }
2807
2808 err = regulator_enable(sor->avdd_io_supply);
2809 if (err < 0) {
2810 dev_err(sor->dev, "failed to enable AVDD I/O supply: %d\n",
2811 err);
2812 return err;
2813 }
2814
2815 sor->vdd_pll_supply = devm_regulator_get(sor->dev, "vdd-pll");
2816 if (IS_ERR(sor->vdd_pll_supply)) {
2817 dev_err(sor->dev, "cannot get VDD PLL supply: %ld\n",
2818 PTR_ERR(sor->vdd_pll_supply));
2819 return PTR_ERR(sor->vdd_pll_supply);
2820 }
2821
2822 err = regulator_enable(sor->vdd_pll_supply);
2823 if (err < 0) {
2824 dev_err(sor->dev, "failed to enable VDD PLL supply: %d\n",
2825 err);
2826 return err;
2827 }
2828
2829 sor->hdmi_supply = devm_regulator_get(sor->dev, "hdmi");
2830 if (IS_ERR(sor->hdmi_supply)) {
2831 dev_err(sor->dev, "cannot get HDMI supply: %ld\n",
2832 PTR_ERR(sor->hdmi_supply));
2833 return PTR_ERR(sor->hdmi_supply);
2834 }
2835
2836 err = regulator_enable(sor->hdmi_supply);
2837 if (err < 0) {
2838 dev_err(sor->dev, "failed to enable HDMI supply: %d\n", err);
2839 return err;
2840 }
2841
Thierry Reding36e90222017-10-12 19:14:21 +02002842 INIT_DELAYED_WORK(&sor->scdc, tegra_sor_hdmi_scdc_work);
2843
Thierry Reding459cc2c2015-07-30 10:34:24 +02002844 return 0;
2845}
2846
2847static int tegra_sor_hdmi_remove(struct tegra_sor *sor)
2848{
2849 regulator_disable(sor->hdmi_supply);
2850 regulator_disable(sor->vdd_pll_supply);
2851 regulator_disable(sor->avdd_io_supply);
2852
2853 return 0;
2854}
2855
2856static const struct tegra_sor_ops tegra_sor_hdmi_ops = {
2857 .name = "HDMI",
2858 .probe = tegra_sor_hdmi_probe,
2859 .remove = tegra_sor_hdmi_remove,
2860};
2861
Thierry Reding30b49432015-08-03 15:50:32 +02002862static const u8 tegra124_sor_xbar_cfg[5] = {
2863 0, 1, 2, 3, 4
2864};
2865
Thierry Reding880cee02017-10-12 19:04:17 +02002866static const struct tegra_sor_regs tegra124_sor_regs = {
2867 .head_state0 = 0x05,
2868 .head_state1 = 0x07,
2869 .head_state2 = 0x09,
2870 .head_state3 = 0x0b,
2871 .head_state4 = 0x0d,
2872 .head_state5 = 0x0f,
2873 .pll0 = 0x17,
2874 .pll1 = 0x18,
2875 .pll2 = 0x19,
2876 .pll3 = 0x1a,
2877 .dp_padctl0 = 0x5c,
2878 .dp_padctl2 = 0x73,
2879};
2880
Thierry Reding459cc2c2015-07-30 10:34:24 +02002881static const struct tegra_sor_soc tegra124_sor = {
2882 .supports_edp = true,
2883 .supports_lvds = true,
2884 .supports_hdmi = false,
2885 .supports_dp = false,
Thierry Reding880cee02017-10-12 19:04:17 +02002886 .regs = &tegra124_sor_regs,
Thierry Redingc57997b2017-10-12 19:12:57 +02002887 .has_nvdisplay = false,
Thierry Reding30b49432015-08-03 15:50:32 +02002888 .xbar_cfg = tegra124_sor_xbar_cfg,
Thierry Reding459cc2c2015-07-30 10:34:24 +02002889};
2890
Thierry Reding880cee02017-10-12 19:04:17 +02002891static const struct tegra_sor_regs tegra210_sor_regs = {
2892 .head_state0 = 0x05,
2893 .head_state1 = 0x07,
2894 .head_state2 = 0x09,
2895 .head_state3 = 0x0b,
2896 .head_state4 = 0x0d,
2897 .head_state5 = 0x0f,
2898 .pll0 = 0x17,
2899 .pll1 = 0x18,
2900 .pll2 = 0x19,
2901 .pll3 = 0x1a,
2902 .dp_padctl0 = 0x5c,
2903 .dp_padctl2 = 0x73,
2904};
2905
Thierry Reding459cc2c2015-07-30 10:34:24 +02002906static const struct tegra_sor_soc tegra210_sor = {
2907 .supports_edp = true,
2908 .supports_lvds = false,
2909 .supports_hdmi = false,
2910 .supports_dp = false,
Thierry Reding880cee02017-10-12 19:04:17 +02002911 .regs = &tegra210_sor_regs,
Thierry Redingc57997b2017-10-12 19:12:57 +02002912 .has_nvdisplay = false,
Thierry Reding30b49432015-08-03 15:50:32 +02002913 .xbar_cfg = tegra124_sor_xbar_cfg,
2914};
2915
2916static const u8 tegra210_sor_xbar_cfg[5] = {
2917 2, 1, 0, 3, 4
Thierry Reding459cc2c2015-07-30 10:34:24 +02002918};
2919
2920static const struct tegra_sor_soc tegra210_sor1 = {
2921 .supports_edp = false,
2922 .supports_lvds = false,
2923 .supports_hdmi = true,
2924 .supports_dp = true,
2925
Thierry Reding880cee02017-10-12 19:04:17 +02002926 .regs = &tegra210_sor_regs,
Thierry Redingc57997b2017-10-12 19:12:57 +02002927 .has_nvdisplay = false,
Thierry Reding880cee02017-10-12 19:04:17 +02002928
Thierry Reding459cc2c2015-07-30 10:34:24 +02002929 .num_settings = ARRAY_SIZE(tegra210_sor_hdmi_defaults),
2930 .settings = tegra210_sor_hdmi_defaults,
Thierry Reding30b49432015-08-03 15:50:32 +02002931
2932 .xbar_cfg = tegra210_sor_xbar_cfg,
Thierry Reding459cc2c2015-07-30 10:34:24 +02002933};
2934
Thierry Redingc57997b2017-10-12 19:12:57 +02002935static const struct tegra_sor_regs tegra186_sor_regs = {
2936 .head_state0 = 0x151,
2937 .head_state1 = 0x154,
2938 .head_state2 = 0x157,
2939 .head_state3 = 0x15a,
2940 .head_state4 = 0x15d,
2941 .head_state5 = 0x160,
2942 .pll0 = 0x163,
2943 .pll1 = 0x164,
2944 .pll2 = 0x165,
2945 .pll3 = 0x166,
2946 .dp_padctl0 = 0x168,
2947 .dp_padctl2 = 0x16a,
2948};
2949
2950static const struct tegra_sor_soc tegra186_sor = {
2951 .supports_edp = false,
2952 .supports_lvds = false,
2953 .supports_hdmi = false,
2954 .supports_dp = true,
2955
2956 .regs = &tegra186_sor_regs,
2957 .has_nvdisplay = true,
2958
2959 .xbar_cfg = tegra124_sor_xbar_cfg,
2960};
2961
2962static const struct tegra_sor_soc tegra186_sor1 = {
2963 .supports_edp = false,
2964 .supports_lvds = false,
2965 .supports_hdmi = true,
2966 .supports_dp = true,
2967
2968 .regs = &tegra186_sor_regs,
2969 .has_nvdisplay = true,
2970
2971 .num_settings = ARRAY_SIZE(tegra186_sor_hdmi_defaults),
2972 .settings = tegra186_sor_hdmi_defaults,
2973
2974 .xbar_cfg = tegra124_sor_xbar_cfg,
2975};
2976
Thierry Reding9b6c14b2018-09-21 12:27:46 +02002977static const struct tegra_sor_regs tegra194_sor_regs = {
2978 .head_state0 = 0x151,
2979 .head_state1 = 0x155,
2980 .head_state2 = 0x159,
2981 .head_state3 = 0x15d,
2982 .head_state4 = 0x161,
2983 .head_state5 = 0x165,
2984 .pll0 = 0x169,
2985 .pll1 = 0x16a,
2986 .pll2 = 0x16b,
2987 .pll3 = 0x16c,
2988 .dp_padctl0 = 0x16e,
2989 .dp_padctl2 = 0x16f,
2990};
2991
2992static const struct tegra_sor_soc tegra194_sor = {
2993 .supports_edp = true,
2994 .supports_lvds = false,
2995 .supports_hdmi = true,
2996 .supports_dp = true,
2997
2998 .regs = &tegra194_sor_regs,
2999 .has_nvdisplay = true,
3000
3001 .num_settings = ARRAY_SIZE(tegra194_sor_hdmi_defaults),
3002 .settings = tegra194_sor_hdmi_defaults,
3003
3004 .xbar_cfg = tegra210_sor_xbar_cfg,
3005};
3006
Thierry Reding459cc2c2015-07-30 10:34:24 +02003007static const struct of_device_id tegra_sor_of_match[] = {
Thierry Reding9b6c14b2018-09-21 12:27:46 +02003008 { .compatible = "nvidia,tegra194-sor", .data = &tegra194_sor },
Thierry Redingc57997b2017-10-12 19:12:57 +02003009 { .compatible = "nvidia,tegra186-sor1", .data = &tegra186_sor1 },
3010 { .compatible = "nvidia,tegra186-sor", .data = &tegra186_sor },
Thierry Reding459cc2c2015-07-30 10:34:24 +02003011 { .compatible = "nvidia,tegra210-sor1", .data = &tegra210_sor1 },
3012 { .compatible = "nvidia,tegra210-sor", .data = &tegra210_sor },
3013 { .compatible = "nvidia,tegra124-sor", .data = &tegra124_sor },
3014 { },
3015};
3016MODULE_DEVICE_TABLE(of, tegra_sor_of_match);
3017
Thierry Redingc57997b2017-10-12 19:12:57 +02003018static int tegra_sor_parse_dt(struct tegra_sor *sor)
3019{
3020 struct device_node *np = sor->dev->of_node;
3021 u32 value;
3022 int err;
3023
3024 if (sor->soc->has_nvdisplay) {
3025 err = of_property_read_u32(np, "nvidia,interface", &value);
3026 if (err < 0)
3027 return err;
3028
3029 sor->index = value;
3030
3031 /*
3032 * override the default that we already set for Tegra210 and
3033 * earlier
3034 */
3035 sor->pad = TEGRA_IO_PAD_HDMI_DP0 + sor->index;
3036 }
3037
3038 return 0;
3039}
3040
Thierry Reding6b6b6042013-11-15 16:06:05 +01003041static int tegra_sor_probe(struct platform_device *pdev)
3042{
3043 struct device_node *np;
3044 struct tegra_sor *sor;
3045 struct resource *regs;
3046 int err;
3047
3048 sor = devm_kzalloc(&pdev->dev, sizeof(*sor), GFP_KERNEL);
3049 if (!sor)
3050 return -ENOMEM;
3051
Thierry Reding5faea3d2017-08-21 17:33:14 +02003052 sor->soc = of_device_get_match_data(&pdev->dev);
Thierry Reding6b6b6042013-11-15 16:06:05 +01003053 sor->output.dev = sor->dev = &pdev->dev;
Thierry Reding459cc2c2015-07-30 10:34:24 +02003054
3055 sor->settings = devm_kmemdup(&pdev->dev, sor->soc->settings,
3056 sor->soc->num_settings *
3057 sizeof(*sor->settings),
3058 GFP_KERNEL);
3059 if (!sor->settings)
3060 return -ENOMEM;
3061
3062 sor->num_settings = sor->soc->num_settings;
Thierry Reding6b6b6042013-11-15 16:06:05 +01003063
3064 np = of_parse_phandle(pdev->dev.of_node, "nvidia,dpaux", 0);
3065 if (np) {
Thierry Reding9542c232015-07-08 13:39:09 +02003066 sor->aux = drm_dp_aux_find_by_of_node(np);
Thierry Reding6b6b6042013-11-15 16:06:05 +01003067 of_node_put(np);
3068
Thierry Reding9542c232015-07-08 13:39:09 +02003069 if (!sor->aux)
Thierry Reding6b6b6042013-11-15 16:06:05 +01003070 return -EPROBE_DEFER;
3071 }
3072
Thierry Reding9542c232015-07-08 13:39:09 +02003073 if (!sor->aux) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02003074 if (sor->soc->supports_hdmi) {
3075 sor->ops = &tegra_sor_hdmi_ops;
Thierry Redingc57997b2017-10-12 19:12:57 +02003076 sor->pad = TEGRA_IO_PAD_HDMI;
Thierry Reding459cc2c2015-07-30 10:34:24 +02003077 } else if (sor->soc->supports_lvds) {
3078 dev_err(&pdev->dev, "LVDS not supported yet\n");
3079 return -ENODEV;
3080 } else {
3081 dev_err(&pdev->dev, "unknown (non-DP) support\n");
3082 return -ENODEV;
3083 }
3084 } else {
3085 if (sor->soc->supports_edp) {
3086 sor->ops = &tegra_sor_edp_ops;
Thierry Redingc57997b2017-10-12 19:12:57 +02003087 sor->pad = TEGRA_IO_PAD_LVDS;
Thierry Reding459cc2c2015-07-30 10:34:24 +02003088 } else if (sor->soc->supports_dp) {
3089 dev_err(&pdev->dev, "DisplayPort not supported yet\n");
3090 return -ENODEV;
3091 } else {
3092 dev_err(&pdev->dev, "unknown (DP) support\n");
3093 return -ENODEV;
3094 }
3095 }
3096
Thierry Redingc57997b2017-10-12 19:12:57 +02003097 err = tegra_sor_parse_dt(sor);
3098 if (err < 0)
3099 return err;
3100
Thierry Reding6b6b6042013-11-15 16:06:05 +01003101 err = tegra_output_probe(&sor->output);
Thierry Reding4dbdc742015-04-27 15:04:26 +02003102 if (err < 0) {
3103 dev_err(&pdev->dev, "failed to probe output: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01003104 return err;
Thierry Reding4dbdc742015-04-27 15:04:26 +02003105 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01003106
Thierry Reding459cc2c2015-07-30 10:34:24 +02003107 if (sor->ops && sor->ops->probe) {
3108 err = sor->ops->probe(sor);
3109 if (err < 0) {
3110 dev_err(&pdev->dev, "failed to probe %s: %d\n",
3111 sor->ops->name, err);
3112 goto output;
3113 }
3114 }
3115
Thierry Reding6b6b6042013-11-15 16:06:05 +01003116 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3117 sor->regs = devm_ioremap_resource(&pdev->dev, regs);
Thierry Reding459cc2c2015-07-30 10:34:24 +02003118 if (IS_ERR(sor->regs)) {
3119 err = PTR_ERR(sor->regs);
3120 goto remove;
3121 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01003122
Jon Hunterf8c79122016-07-01 14:21:38 +01003123 if (!pdev->dev.pm_domain) {
3124 sor->rst = devm_reset_control_get(&pdev->dev, "sor");
3125 if (IS_ERR(sor->rst)) {
3126 err = PTR_ERR(sor->rst);
3127 dev_err(&pdev->dev, "failed to get reset control: %d\n",
3128 err);
3129 goto remove;
3130 }
Thierry Reding4dbdc742015-04-27 15:04:26 +02003131 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01003132
3133 sor->clk = devm_clk_get(&pdev->dev, NULL);
Thierry Reding4dbdc742015-04-27 15:04:26 +02003134 if (IS_ERR(sor->clk)) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02003135 err = PTR_ERR(sor->clk);
3136 dev_err(&pdev->dev, "failed to get module clock: %d\n", err);
3137 goto remove;
Thierry Reding4dbdc742015-04-27 15:04:26 +02003138 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01003139
Thierry Reding618dee32016-06-09 17:53:57 +02003140 if (sor->soc->supports_hdmi || sor->soc->supports_dp) {
Thierry Redinge1335e22017-10-12 17:53:11 +02003141 struct device_node *np = pdev->dev.of_node;
3142 const char *name;
3143
3144 /*
3145 * For backwards compatibility with Tegra210 device trees,
3146 * fall back to the old clock name "source" if the new "out"
3147 * clock is not available.
3148 */
3149 if (of_property_match_string(np, "clock-names", "out") < 0)
3150 name = "source";
3151 else
3152 name = "out";
3153
3154 sor->clk_out = devm_clk_get(&pdev->dev, name);
3155 if (IS_ERR(sor->clk_out)) {
3156 err = PTR_ERR(sor->clk_out);
3157 dev_err(sor->dev, "failed to get %s clock: %d\n",
3158 name, err);
Thierry Reding618dee32016-06-09 17:53:57 +02003159 goto remove;
3160 }
Thierry Reding1087fac2017-12-14 13:37:53 +01003161 } else {
Thierry Redingd7805372018-01-10 13:04:58 +01003162 /* fall back to the module clock on SOR0 (eDP/LVDS only) */
Thierry Reding1087fac2017-12-14 13:37:53 +01003163 sor->clk_out = sor->clk;
Thierry Reding618dee32016-06-09 17:53:57 +02003164 }
3165
Thierry Reding6b6b6042013-11-15 16:06:05 +01003166 sor->clk_parent = devm_clk_get(&pdev->dev, "parent");
Thierry Reding4dbdc742015-04-27 15:04:26 +02003167 if (IS_ERR(sor->clk_parent)) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02003168 err = PTR_ERR(sor->clk_parent);
3169 dev_err(&pdev->dev, "failed to get parent clock: %d\n", err);
3170 goto remove;
Thierry Reding4dbdc742015-04-27 15:04:26 +02003171 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01003172
Thierry Reding6b6b6042013-11-15 16:06:05 +01003173 sor->clk_safe = devm_clk_get(&pdev->dev, "safe");
Thierry Reding4dbdc742015-04-27 15:04:26 +02003174 if (IS_ERR(sor->clk_safe)) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02003175 err = PTR_ERR(sor->clk_safe);
3176 dev_err(&pdev->dev, "failed to get safe clock: %d\n", err);
3177 goto remove;
Thierry Reding4dbdc742015-04-27 15:04:26 +02003178 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01003179
Thierry Reding6b6b6042013-11-15 16:06:05 +01003180 sor->clk_dp = devm_clk_get(&pdev->dev, "dp");
Thierry Reding4dbdc742015-04-27 15:04:26 +02003181 if (IS_ERR(sor->clk_dp)) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02003182 err = PTR_ERR(sor->clk_dp);
3183 dev_err(&pdev->dev, "failed to get DP clock: %d\n", err);
3184 goto remove;
Thierry Reding4dbdc742015-04-27 15:04:26 +02003185 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01003186
Thierry Redinge1335e22017-10-12 17:53:11 +02003187 /*
3188 * Starting with Tegra186, the BPMP provides an implementation for
3189 * the pad output clock, so we have to look it up from device tree.
3190 */
3191 sor->clk_pad = devm_clk_get(&pdev->dev, "pad");
3192 if (IS_ERR(sor->clk_pad)) {
3193 if (sor->clk_pad != ERR_PTR(-ENOENT)) {
3194 err = PTR_ERR(sor->clk_pad);
3195 goto remove;
3196 }
3197
3198 /*
3199 * If the pad output clock is not available, then we assume
3200 * we're on Tegra210 or earlier and have to provide our own
3201 * implementation.
3202 */
3203 sor->clk_pad = NULL;
3204 }
3205
3206 /*
3207 * The bootloader may have set up the SOR such that it's module clock
3208 * is sourced by one of the display PLLs. However, that doesn't work
3209 * without properly having set up other bits of the SOR.
3210 */
3211 err = clk_set_parent(sor->clk_out, sor->clk_safe);
3212 if (err < 0) {
3213 dev_err(&pdev->dev, "failed to use safe clock: %d\n", err);
3214 goto remove;
3215 }
3216
Thierry Redingaaff8bd2015-08-07 16:04:54 +02003217 platform_set_drvdata(pdev, sor);
3218 pm_runtime_enable(&pdev->dev);
3219
Thierry Redinge1335e22017-10-12 17:53:11 +02003220 /*
3221 * On Tegra210 and earlier, provide our own implementation for the
3222 * pad output clock.
3223 */
3224 if (!sor->clk_pad) {
3225 err = pm_runtime_get_sync(&pdev->dev);
3226 if (err < 0) {
3227 dev_err(&pdev->dev, "failed to get runtime PM: %d\n",
3228 err);
3229 goto remove;
3230 }
Thierry Redingb2992212015-10-01 14:25:03 +02003231
Thierry Redinge1335e22017-10-12 17:53:11 +02003232 sor->clk_pad = tegra_clk_sor_pad_register(sor,
3233 "sor1_pad_clkout");
3234 pm_runtime_put(&pdev->dev);
3235 }
3236
3237 if (IS_ERR(sor->clk_pad)) {
3238 err = PTR_ERR(sor->clk_pad);
3239 dev_err(&pdev->dev, "failed to register SOR pad clock: %d\n",
3240 err);
Thierry Redingb2992212015-10-01 14:25:03 +02003241 goto remove;
3242 }
3243
Thierry Reding6b6b6042013-11-15 16:06:05 +01003244 INIT_LIST_HEAD(&sor->client.list);
3245 sor->client.ops = &sor_client_ops;
3246 sor->client.dev = &pdev->dev;
3247
Thierry Reding6b6b6042013-11-15 16:06:05 +01003248 err = host1x_client_register(&sor->client);
3249 if (err < 0) {
3250 dev_err(&pdev->dev, "failed to register host1x client: %d\n",
3251 err);
Thierry Reding459cc2c2015-07-30 10:34:24 +02003252 goto remove;
Thierry Reding6b6b6042013-11-15 16:06:05 +01003253 }
3254
Thierry Reding6b6b6042013-11-15 16:06:05 +01003255 return 0;
Thierry Reding459cc2c2015-07-30 10:34:24 +02003256
3257remove:
3258 if (sor->ops && sor->ops->remove)
3259 sor->ops->remove(sor);
3260output:
3261 tegra_output_remove(&sor->output);
3262 return err;
Thierry Reding6b6b6042013-11-15 16:06:05 +01003263}
3264
3265static int tegra_sor_remove(struct platform_device *pdev)
3266{
3267 struct tegra_sor *sor = platform_get_drvdata(pdev);
3268 int err;
3269
Thierry Redingaaff8bd2015-08-07 16:04:54 +02003270 pm_runtime_disable(&pdev->dev);
3271
Thierry Reding6b6b6042013-11-15 16:06:05 +01003272 err = host1x_client_unregister(&sor->client);
3273 if (err < 0) {
3274 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
3275 err);
3276 return err;
3277 }
3278
Thierry Reding459cc2c2015-07-30 10:34:24 +02003279 if (sor->ops && sor->ops->remove) {
3280 err = sor->ops->remove(sor);
3281 if (err < 0)
3282 dev_err(&pdev->dev, "failed to remove SOR: %d\n", err);
3283 }
3284
Thierry Reding328ec692014-12-19 15:55:08 +01003285 tegra_output_remove(&sor->output);
Thierry Reding6b6b6042013-11-15 16:06:05 +01003286
3287 return 0;
3288}
3289
Thierry Redingaaff8bd2015-08-07 16:04:54 +02003290#ifdef CONFIG_PM
3291static int tegra_sor_suspend(struct device *dev)
3292{
3293 struct tegra_sor *sor = dev_get_drvdata(dev);
3294 int err;
3295
Jon Hunterf8c79122016-07-01 14:21:38 +01003296 if (sor->rst) {
3297 err = reset_control_assert(sor->rst);
3298 if (err < 0) {
3299 dev_err(dev, "failed to assert reset: %d\n", err);
3300 return err;
3301 }
Thierry Redingaaff8bd2015-08-07 16:04:54 +02003302 }
3303
3304 usleep_range(1000, 2000);
3305
3306 clk_disable_unprepare(sor->clk);
3307
3308 return 0;
3309}
3310
3311static int tegra_sor_resume(struct device *dev)
3312{
3313 struct tegra_sor *sor = dev_get_drvdata(dev);
3314 int err;
3315
3316 err = clk_prepare_enable(sor->clk);
3317 if (err < 0) {
3318 dev_err(dev, "failed to enable clock: %d\n", err);
3319 return err;
3320 }
3321
3322 usleep_range(1000, 2000);
3323
Jon Hunterf8c79122016-07-01 14:21:38 +01003324 if (sor->rst) {
3325 err = reset_control_deassert(sor->rst);
3326 if (err < 0) {
3327 dev_err(dev, "failed to deassert reset: %d\n", err);
3328 clk_disable_unprepare(sor->clk);
3329 return err;
3330 }
Thierry Redingaaff8bd2015-08-07 16:04:54 +02003331 }
3332
3333 return 0;
3334}
3335#endif
3336
3337static const struct dev_pm_ops tegra_sor_pm_ops = {
3338 SET_RUNTIME_PM_OPS(tegra_sor_suspend, tegra_sor_resume, NULL)
3339};
3340
Thierry Reding6b6b6042013-11-15 16:06:05 +01003341struct platform_driver tegra_sor_driver = {
3342 .driver = {
3343 .name = "tegra-sor",
3344 .of_match_table = tegra_sor_of_match,
Thierry Redingaaff8bd2015-08-07 16:04:54 +02003345 .pm = &tegra_sor_pm_ops,
Thierry Reding6b6b6042013-11-15 16:06:05 +01003346 },
3347 .probe = tegra_sor_probe,
3348 .remove = tegra_sor_remove,
3349};