blob: 7839223aa04011308c81f062e3f33cfc37ba4482 [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 Reding8e2988a72018-12-03 15:46:03 +010022#include <sound/hda_verbs.h>
23
Thierry Reding4aa3df72014-11-24 16:27:13 +010024#include <drm/drm_atomic_helper.h>
Thierry Reding6b6b6042013-11-15 16:06:05 +010025#include <drm/drm_dp_helper.h>
Thierry Reding6fad8f62014-11-28 15:41:34 +010026#include <drm/drm_panel.h>
Thierry Reding36e90222017-10-12 19:14:21 +020027#include <drm/drm_scdc_helper.h>
Thierry Reding6b6b6042013-11-15 16:06:05 +010028
29#include "dc.h"
30#include "drm.h"
31#include "sor.h"
Thierry Reding932f6522017-08-15 15:41:14 +020032#include "trace.h"
Thierry Reding6b6b6042013-11-15 16:06:05 +010033
Thierry Reding459cc2c2015-07-30 10:34:24 +020034#define SOR_REKEY 0x38
35
36struct tegra_sor_hdmi_settings {
37 unsigned long frequency;
38
39 u8 vcocap;
Thierry Redingc57997b2017-10-12 19:12:57 +020040 u8 filter;
Thierry Reding459cc2c2015-07-30 10:34:24 +020041 u8 ichpmp;
42 u8 loadadj;
Thierry Redingc57997b2017-10-12 19:12:57 +020043 u8 tmds_termadj;
44 u8 tx_pu_value;
45 u8 bg_temp_coef;
46 u8 bg_vref_level;
47 u8 avdd10_level;
48 u8 avdd14_level;
49 u8 sparepll;
Thierry Reding459cc2c2015-07-30 10:34:24 +020050
51 u8 drive_current[4];
52 u8 preemphasis[4];
53};
54
55#if 1
56static const struct tegra_sor_hdmi_settings tegra210_sor_hdmi_defaults[] = {
57 {
58 .frequency = 54000000,
59 .vcocap = 0x0,
Thierry Redingc57997b2017-10-12 19:12:57 +020060 .filter = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +020061 .ichpmp = 0x1,
62 .loadadj = 0x3,
Thierry Redingc57997b2017-10-12 19:12:57 +020063 .tmds_termadj = 0x9,
64 .tx_pu_value = 0x10,
65 .bg_temp_coef = 0x3,
66 .bg_vref_level = 0x8,
67 .avdd10_level = 0x4,
68 .avdd14_level = 0x4,
69 .sparepll = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +020070 .drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
71 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
72 }, {
73 .frequency = 75000000,
74 .vcocap = 0x3,
Thierry Redingc57997b2017-10-12 19:12:57 +020075 .filter = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +020076 .ichpmp = 0x1,
77 .loadadj = 0x3,
Thierry Redingc57997b2017-10-12 19:12:57 +020078 .tmds_termadj = 0x9,
79 .tx_pu_value = 0x40,
80 .bg_temp_coef = 0x3,
81 .bg_vref_level = 0x8,
82 .avdd10_level = 0x4,
83 .avdd14_level = 0x4,
84 .sparepll = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +020085 .drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
86 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
87 }, {
88 .frequency = 150000000,
89 .vcocap = 0x3,
Thierry Redingc57997b2017-10-12 19:12:57 +020090 .filter = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +020091 .ichpmp = 0x1,
92 .loadadj = 0x3,
Thierry Redingc57997b2017-10-12 19:12:57 +020093 .tmds_termadj = 0x9,
94 .tx_pu_value = 0x66,
95 .bg_temp_coef = 0x3,
96 .bg_vref_level = 0x8,
97 .avdd10_level = 0x4,
98 .avdd14_level = 0x4,
99 .sparepll = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +0200100 .drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
101 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
102 }, {
103 .frequency = 300000000,
104 .vcocap = 0x3,
Thierry Redingc57997b2017-10-12 19:12:57 +0200105 .filter = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +0200106 .ichpmp = 0x1,
107 .loadadj = 0x3,
Thierry Redingc57997b2017-10-12 19:12:57 +0200108 .tmds_termadj = 0x9,
109 .tx_pu_value = 0x66,
110 .bg_temp_coef = 0x3,
111 .bg_vref_level = 0xa,
112 .avdd10_level = 0x4,
113 .avdd14_level = 0x4,
114 .sparepll = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +0200115 .drive_current = { 0x33, 0x3f, 0x3f, 0x3f },
116 .preemphasis = { 0x00, 0x17, 0x17, 0x17 },
117 }, {
118 .frequency = 600000000,
119 .vcocap = 0x3,
Thierry Redingc57997b2017-10-12 19:12:57 +0200120 .filter = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +0200121 .ichpmp = 0x1,
122 .loadadj = 0x3,
Thierry Redingc57997b2017-10-12 19:12:57 +0200123 .tmds_termadj = 0x9,
124 .tx_pu_value = 0x66,
125 .bg_temp_coef = 0x3,
126 .bg_vref_level = 0x8,
127 .avdd10_level = 0x4,
128 .avdd14_level = 0x4,
129 .sparepll = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +0200130 .drive_current = { 0x33, 0x3f, 0x3f, 0x3f },
131 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
132 },
133};
134#else
135static const struct tegra_sor_hdmi_settings tegra210_sor_hdmi_defaults[] = {
136 {
137 .frequency = 75000000,
138 .vcocap = 0x3,
Thierry Redingc57997b2017-10-12 19:12:57 +0200139 .filter = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +0200140 .ichpmp = 0x1,
141 .loadadj = 0x3,
Thierry Redingc57997b2017-10-12 19:12:57 +0200142 .tmds_termadj = 0x9,
143 .tx_pu_value = 0x40,
144 .bg_temp_coef = 0x3,
145 .bg_vref_level = 0x8,
146 .avdd10_level = 0x4,
147 .avdd14_level = 0x4,
148 .sparepll = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +0200149 .drive_current = { 0x29, 0x29, 0x29, 0x29 },
150 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
151 }, {
152 .frequency = 150000000,
153 .vcocap = 0x3,
Thierry Redingc57997b2017-10-12 19:12:57 +0200154 .filter = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +0200155 .ichpmp = 0x1,
156 .loadadj = 0x3,
Thierry Redingc57997b2017-10-12 19:12:57 +0200157 .tmds_termadj = 0x9,
158 .tx_pu_value = 0x66,
159 .bg_temp_coef = 0x3,
160 .bg_vref_level = 0x8,
161 .avdd10_level = 0x4,
162 .avdd14_level = 0x4,
163 .sparepll = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +0200164 .drive_current = { 0x30, 0x37, 0x37, 0x37 },
165 .preemphasis = { 0x01, 0x02, 0x02, 0x02 },
166 }, {
167 .frequency = 300000000,
168 .vcocap = 0x3,
Thierry Redingc57997b2017-10-12 19:12:57 +0200169 .filter = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +0200170 .ichpmp = 0x6,
171 .loadadj = 0x3,
Thierry Redingc57997b2017-10-12 19:12:57 +0200172 .tmds_termadj = 0x9,
173 .tx_pu_value = 0x66,
174 .bg_temp_coef = 0x3,
175 .bg_vref_level = 0xf,
176 .avdd10_level = 0x4,
177 .avdd14_level = 0x4,
178 .sparepll = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +0200179 .drive_current = { 0x30, 0x37, 0x37, 0x37 },
180 .preemphasis = { 0x10, 0x3e, 0x3e, 0x3e },
181 }, {
182 .frequency = 600000000,
183 .vcocap = 0x3,
Thierry Redingc57997b2017-10-12 19:12:57 +0200184 .filter = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +0200185 .ichpmp = 0xa,
186 .loadadj = 0x3,
Thierry Redingc57997b2017-10-12 19:12:57 +0200187 .tmds_termadj = 0xb,
188 .tx_pu_value = 0x66,
189 .bg_temp_coef = 0x3,
190 .bg_vref_level = 0xe,
191 .avdd10_level = 0x4,
192 .avdd14_level = 0x4,
193 .sparepll = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +0200194 .drive_current = { 0x35, 0x3e, 0x3e, 0x3e },
195 .preemphasis = { 0x02, 0x3f, 0x3f, 0x3f },
196 },
197};
198#endif
199
Thierry Redingc57997b2017-10-12 19:12:57 +0200200static const struct tegra_sor_hdmi_settings tegra186_sor_hdmi_defaults[] = {
201 {
202 .frequency = 54000000,
203 .vcocap = 0,
204 .filter = 5,
205 .ichpmp = 5,
206 .loadadj = 3,
207 .tmds_termadj = 0xf,
208 .tx_pu_value = 0,
209 .bg_temp_coef = 3,
210 .bg_vref_level = 8,
211 .avdd10_level = 4,
212 .avdd14_level = 4,
213 .sparepll = 0x54,
214 .drive_current = { 0x3a, 0x3a, 0x3a, 0x33 },
215 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
216 }, {
217 .frequency = 75000000,
218 .vcocap = 1,
219 .filter = 5,
220 .ichpmp = 5,
221 .loadadj = 3,
222 .tmds_termadj = 0xf,
223 .tx_pu_value = 0,
224 .bg_temp_coef = 3,
225 .bg_vref_level = 8,
226 .avdd10_level = 4,
227 .avdd14_level = 4,
228 .sparepll = 0x44,
229 .drive_current = { 0x3a, 0x3a, 0x3a, 0x33 },
230 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
231 }, {
232 .frequency = 150000000,
233 .vcocap = 3,
234 .filter = 5,
235 .ichpmp = 5,
236 .loadadj = 3,
237 .tmds_termadj = 15,
238 .tx_pu_value = 0x66 /* 0 */,
239 .bg_temp_coef = 3,
240 .bg_vref_level = 8,
241 .avdd10_level = 4,
242 .avdd14_level = 4,
243 .sparepll = 0x00, /* 0x34 */
244 .drive_current = { 0x3a, 0x3a, 0x3a, 0x37 },
245 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
246 }, {
247 .frequency = 300000000,
248 .vcocap = 3,
249 .filter = 5,
250 .ichpmp = 5,
251 .loadadj = 3,
252 .tmds_termadj = 15,
253 .tx_pu_value = 64,
254 .bg_temp_coef = 3,
255 .bg_vref_level = 8,
256 .avdd10_level = 4,
257 .avdd14_level = 4,
258 .sparepll = 0x34,
259 .drive_current = { 0x3d, 0x3d, 0x3d, 0x33 },
260 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
261 }, {
262 .frequency = 600000000,
263 .vcocap = 3,
264 .filter = 5,
265 .ichpmp = 5,
266 .loadadj = 3,
267 .tmds_termadj = 12,
268 .tx_pu_value = 96,
269 .bg_temp_coef = 3,
270 .bg_vref_level = 8,
271 .avdd10_level = 4,
272 .avdd14_level = 4,
273 .sparepll = 0x34,
274 .drive_current = { 0x3d, 0x3d, 0x3d, 0x33 },
275 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
276 }
277};
278
Thierry Reding9b6c14b2018-09-21 12:27:46 +0200279static const struct tegra_sor_hdmi_settings tegra194_sor_hdmi_defaults[] = {
280 {
281 .frequency = 54000000,
282 .vcocap = 0,
283 .filter = 5,
284 .ichpmp = 5,
285 .loadadj = 3,
286 .tmds_termadj = 0xf,
287 .tx_pu_value = 0,
288 .bg_temp_coef = 3,
289 .bg_vref_level = 8,
290 .avdd10_level = 4,
291 .avdd14_level = 4,
292 .sparepll = 0x54,
293 .drive_current = { 0x3a, 0x3a, 0x3a, 0x33 },
294 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
295 }, {
296 .frequency = 75000000,
297 .vcocap = 1,
298 .filter = 5,
299 .ichpmp = 5,
300 .loadadj = 3,
301 .tmds_termadj = 0xf,
302 .tx_pu_value = 0,
303 .bg_temp_coef = 3,
304 .bg_vref_level = 8,
305 .avdd10_level = 4,
306 .avdd14_level = 4,
307 .sparepll = 0x44,
308 .drive_current = { 0x3a, 0x3a, 0x3a, 0x33 },
309 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
310 }, {
311 .frequency = 150000000,
312 .vcocap = 3,
313 .filter = 5,
314 .ichpmp = 5,
315 .loadadj = 3,
316 .tmds_termadj = 15,
317 .tx_pu_value = 0x66 /* 0 */,
318 .bg_temp_coef = 3,
319 .bg_vref_level = 8,
320 .avdd10_level = 4,
321 .avdd14_level = 4,
322 .sparepll = 0x00, /* 0x34 */
323 .drive_current = { 0x3a, 0x3a, 0x3a, 0x37 },
324 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
325 }, {
326 .frequency = 300000000,
327 .vcocap = 3,
328 .filter = 5,
329 .ichpmp = 5,
330 .loadadj = 3,
331 .tmds_termadj = 15,
332 .tx_pu_value = 64,
333 .bg_temp_coef = 3,
334 .bg_vref_level = 8,
335 .avdd10_level = 4,
336 .avdd14_level = 4,
337 .sparepll = 0x34,
338 .drive_current = { 0x3d, 0x3d, 0x3d, 0x33 },
339 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
340 }, {
341 .frequency = 600000000,
342 .vcocap = 3,
343 .filter = 5,
344 .ichpmp = 5,
345 .loadadj = 3,
346 .tmds_termadj = 12,
347 .tx_pu_value = 96,
348 .bg_temp_coef = 3,
349 .bg_vref_level = 8,
350 .avdd10_level = 4,
351 .avdd14_level = 4,
352 .sparepll = 0x34,
353 .drive_current = { 0x3d, 0x3d, 0x3d, 0x33 },
354 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
355 }
356};
357
Thierry Reding880cee02017-10-12 19:04:17 +0200358struct tegra_sor_regs {
359 unsigned int head_state0;
360 unsigned int head_state1;
361 unsigned int head_state2;
362 unsigned int head_state3;
363 unsigned int head_state4;
364 unsigned int head_state5;
365 unsigned int pll0;
366 unsigned int pll1;
367 unsigned int pll2;
368 unsigned int pll3;
369 unsigned int dp_padctl0;
370 unsigned int dp_padctl2;
371};
372
Thierry Reding459cc2c2015-07-30 10:34:24 +0200373struct tegra_sor_soc {
374 bool supports_edp;
375 bool supports_lvds;
376 bool supports_hdmi;
377 bool supports_dp;
378
Thierry Reding880cee02017-10-12 19:04:17 +0200379 const struct tegra_sor_regs *regs;
Thierry Redingc57997b2017-10-12 19:12:57 +0200380 bool has_nvdisplay;
Thierry Reding880cee02017-10-12 19:04:17 +0200381
Thierry Reding459cc2c2015-07-30 10:34:24 +0200382 const struct tegra_sor_hdmi_settings *settings;
383 unsigned int num_settings;
Thierry Reding30b49432015-08-03 15:50:32 +0200384
385 const u8 *xbar_cfg;
Thierry Reding459cc2c2015-07-30 10:34:24 +0200386};
387
388struct tegra_sor;
389
390struct tegra_sor_ops {
391 const char *name;
392 int (*probe)(struct tegra_sor *sor);
393 int (*remove)(struct tegra_sor *sor);
394};
395
Thierry Redingcd54fb92019-01-03 15:23:15 +0100396struct tegra_sor_audio {
397 unsigned int sample_rate;
398 unsigned int channels;
399 unsigned int bits;
400 bool pcm;
401};
402
Thierry Reding6b6b6042013-11-15 16:06:05 +0100403struct tegra_sor {
404 struct host1x_client client;
405 struct tegra_output output;
406 struct device *dev;
407
Thierry Reding459cc2c2015-07-30 10:34:24 +0200408 const struct tegra_sor_soc *soc;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100409 void __iomem *regs;
Thierry Redingc57997b2017-10-12 19:12:57 +0200410 unsigned int index;
Thierry Reding8e2988a72018-12-03 15:46:03 +0100411 unsigned int irq;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100412
413 struct reset_control *rst;
414 struct clk *clk_parent;
415 struct clk *clk_safe;
Thierry Redinge1335e22017-10-12 17:53:11 +0200416 struct clk *clk_out;
417 struct clk *clk_pad;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100418 struct clk *clk_dp;
419 struct clk *clk;
420
Thierry Reding9542c232015-07-08 13:39:09 +0200421 struct drm_dp_aux *aux;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100422
Thierry Redingdab16332015-01-26 16:04:08 +0100423 struct drm_info_list *debugfs_files;
Thierry Reding459cc2c2015-07-30 10:34:24 +0200424
425 const struct tegra_sor_ops *ops;
Thierry Redingc57997b2017-10-12 19:12:57 +0200426 enum tegra_io_pad pad;
Thierry Reding459cc2c2015-07-30 10:34:24 +0200427
428 /* for HDMI 2.0 */
429 struct tegra_sor_hdmi_settings *settings;
430 unsigned int num_settings;
431
432 struct regulator *avdd_io_supply;
433 struct regulator *vdd_pll_supply;
434 struct regulator *hdmi_supply;
Thierry Reding36e90222017-10-12 19:14:21 +0200435
436 struct delayed_work scdc;
437 bool scdc_enabled;
Thierry Reding8e2988a72018-12-03 15:46:03 +0100438
Thierry Redingcd54fb92019-01-03 15:23:15 +0100439 struct tegra_sor_audio audio;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100440};
441
Thierry Redingc31efa72015-09-08 16:09:22 +0200442struct tegra_sor_state {
443 struct drm_connector_state base;
444
Thierry Reding36e90222017-10-12 19:14:21 +0200445 unsigned int link_speed;
446 unsigned long pclk;
Thierry Redingc31efa72015-09-08 16:09:22 +0200447 unsigned int bpc;
448};
449
450static inline struct tegra_sor_state *
451to_sor_state(struct drm_connector_state *state)
452{
453 return container_of(state, struct tegra_sor_state, base);
454}
455
Thierry Reding34fa1832014-06-05 16:31:10 +0200456struct tegra_sor_config {
457 u32 bits_per_pixel;
458
459 u32 active_polarity;
460 u32 active_count;
461 u32 tu_size;
462 u32 active_frac;
463 u32 watermark;
Thierry Reding7890b572014-06-05 16:12:46 +0200464
465 u32 hblank_symbols;
466 u32 vblank_symbols;
Thierry Reding34fa1832014-06-05 16:31:10 +0200467};
468
Thierry Reding6b6b6042013-11-15 16:06:05 +0100469static inline struct tegra_sor *
470host1x_client_to_sor(struct host1x_client *client)
471{
472 return container_of(client, struct tegra_sor, client);
473}
474
475static inline struct tegra_sor *to_sor(struct tegra_output *output)
476{
477 return container_of(output, struct tegra_sor, output);
478}
479
Thierry Reding5c5f1302017-08-15 15:41:09 +0200480static inline u32 tegra_sor_readl(struct tegra_sor *sor, unsigned int offset)
Thierry Reding6b6b6042013-11-15 16:06:05 +0100481{
Thierry Reding932f6522017-08-15 15:41:14 +0200482 u32 value = readl(sor->regs + (offset << 2));
483
484 trace_sor_readl(sor->dev, offset, value);
485
486 return value;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100487}
488
Thierry Reding28fe2072015-01-26 16:02:48 +0100489static inline void tegra_sor_writel(struct tegra_sor *sor, u32 value,
Thierry Reding5c5f1302017-08-15 15:41:09 +0200490 unsigned int offset)
Thierry Reding6b6b6042013-11-15 16:06:05 +0100491{
Thierry Reding932f6522017-08-15 15:41:14 +0200492 trace_sor_writel(sor->dev, offset, value);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100493 writel(value, sor->regs + (offset << 2));
494}
495
Thierry Reding25bb2ce2015-08-03 14:23:29 +0200496static int tegra_sor_set_parent_clock(struct tegra_sor *sor, struct clk *parent)
497{
498 int err;
499
500 clk_disable_unprepare(sor->clk);
501
Thierry Redinge1335e22017-10-12 17:53:11 +0200502 err = clk_set_parent(sor->clk_out, parent);
Thierry Reding25bb2ce2015-08-03 14:23:29 +0200503 if (err < 0)
504 return err;
505
506 err = clk_prepare_enable(sor->clk);
507 if (err < 0)
508 return err;
509
510 return 0;
511}
512
Thierry Redinge1335e22017-10-12 17:53:11 +0200513struct tegra_clk_sor_pad {
Thierry Redingb2992212015-10-01 14:25:03 +0200514 struct clk_hw hw;
515 struct tegra_sor *sor;
516};
517
Thierry Redinge1335e22017-10-12 17:53:11 +0200518static inline struct tegra_clk_sor_pad *to_pad(struct clk_hw *hw)
Thierry Redingb2992212015-10-01 14:25:03 +0200519{
Thierry Redinge1335e22017-10-12 17:53:11 +0200520 return container_of(hw, struct tegra_clk_sor_pad, hw);
Thierry Redingb2992212015-10-01 14:25:03 +0200521}
522
Thierry Redinge1335e22017-10-12 17:53:11 +0200523static const char * const tegra_clk_sor_pad_parents[] = {
Thierry Redingb2992212015-10-01 14:25:03 +0200524 "pll_d2_out0", "pll_dp"
525};
526
Thierry Redinge1335e22017-10-12 17:53:11 +0200527static int tegra_clk_sor_pad_set_parent(struct clk_hw *hw, u8 index)
Thierry Redingb2992212015-10-01 14:25:03 +0200528{
Thierry Redinge1335e22017-10-12 17:53:11 +0200529 struct tegra_clk_sor_pad *pad = to_pad(hw);
530 struct tegra_sor *sor = pad->sor;
Thierry Redingb2992212015-10-01 14:25:03 +0200531 u32 value;
532
533 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
534 value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
535
536 switch (index) {
537 case 0:
538 value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK;
539 break;
540
541 case 1:
542 value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
543 break;
544 }
545
546 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
547
548 return 0;
549}
550
Thierry Redinge1335e22017-10-12 17:53:11 +0200551static u8 tegra_clk_sor_pad_get_parent(struct clk_hw *hw)
Thierry Redingb2992212015-10-01 14:25:03 +0200552{
Thierry Redinge1335e22017-10-12 17:53:11 +0200553 struct tegra_clk_sor_pad *pad = to_pad(hw);
554 struct tegra_sor *sor = pad->sor;
Thierry Redingb2992212015-10-01 14:25:03 +0200555 u8 parent = U8_MAX;
556 u32 value;
557
558 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
559
560 switch (value & SOR_CLK_CNTRL_DP_CLK_SEL_MASK) {
561 case SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK:
562 case SOR_CLK_CNTRL_DP_CLK_SEL_DIFF_PCLK:
563 parent = 0;
564 break;
565
566 case SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK:
567 case SOR_CLK_CNTRL_DP_CLK_SEL_DIFF_DPCLK:
568 parent = 1;
569 break;
570 }
571
572 return parent;
573}
574
Thierry Redinge1335e22017-10-12 17:53:11 +0200575static const struct clk_ops tegra_clk_sor_pad_ops = {
576 .set_parent = tegra_clk_sor_pad_set_parent,
577 .get_parent = tegra_clk_sor_pad_get_parent,
Thierry Redingb2992212015-10-01 14:25:03 +0200578};
579
Thierry Redinge1335e22017-10-12 17:53:11 +0200580static struct clk *tegra_clk_sor_pad_register(struct tegra_sor *sor,
581 const char *name)
Thierry Redingb2992212015-10-01 14:25:03 +0200582{
Thierry Redinge1335e22017-10-12 17:53:11 +0200583 struct tegra_clk_sor_pad *pad;
Thierry Redingb2992212015-10-01 14:25:03 +0200584 struct clk_init_data init;
585 struct clk *clk;
586
Thierry Redinge1335e22017-10-12 17:53:11 +0200587 pad = devm_kzalloc(sor->dev, sizeof(*pad), GFP_KERNEL);
588 if (!pad)
Thierry Redingb2992212015-10-01 14:25:03 +0200589 return ERR_PTR(-ENOMEM);
590
Thierry Redinge1335e22017-10-12 17:53:11 +0200591 pad->sor = sor;
Thierry Redingb2992212015-10-01 14:25:03 +0200592
593 init.name = name;
594 init.flags = 0;
Thierry Redinge1335e22017-10-12 17:53:11 +0200595 init.parent_names = tegra_clk_sor_pad_parents;
596 init.num_parents = ARRAY_SIZE(tegra_clk_sor_pad_parents);
597 init.ops = &tegra_clk_sor_pad_ops;
Thierry Redingb2992212015-10-01 14:25:03 +0200598
Thierry Redinge1335e22017-10-12 17:53:11 +0200599 pad->hw.init = &init;
Thierry Redingb2992212015-10-01 14:25:03 +0200600
Thierry Redinge1335e22017-10-12 17:53:11 +0200601 clk = devm_clk_register(sor->dev, &pad->hw);
Thierry Redingb2992212015-10-01 14:25:03 +0200602
603 return clk;
604}
605
Thierry Reding6b6b6042013-11-15 16:06:05 +0100606static int tegra_sor_dp_train_fast(struct tegra_sor *sor,
607 struct drm_dp_link *link)
608{
Thierry Reding6b6b6042013-11-15 16:06:05 +0100609 unsigned int i;
610 u8 pattern;
Thierry Reding28fe2072015-01-26 16:02:48 +0100611 u32 value;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100612 int err;
613
614 /* setup lane parameters */
615 value = SOR_LANE_DRIVE_CURRENT_LANE3(0x40) |
616 SOR_LANE_DRIVE_CURRENT_LANE2(0x40) |
617 SOR_LANE_DRIVE_CURRENT_LANE1(0x40) |
618 SOR_LANE_DRIVE_CURRENT_LANE0(0x40);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200619 tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100620
621 value = SOR_LANE_PREEMPHASIS_LANE3(0x0f) |
622 SOR_LANE_PREEMPHASIS_LANE2(0x0f) |
623 SOR_LANE_PREEMPHASIS_LANE1(0x0f) |
624 SOR_LANE_PREEMPHASIS_LANE0(0x0f);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200625 tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100626
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200627 value = SOR_LANE_POSTCURSOR_LANE3(0x00) |
628 SOR_LANE_POSTCURSOR_LANE2(0x00) |
629 SOR_LANE_POSTCURSOR_LANE1(0x00) |
630 SOR_LANE_POSTCURSOR_LANE0(0x00);
631 tegra_sor_writel(sor, value, SOR_LANE_POSTCURSOR0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100632
633 /* disable LVDS mode */
634 tegra_sor_writel(sor, 0, SOR_LVDS);
635
Thierry Reding880cee02017-10-12 19:04:17 +0200636 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100637 value |= SOR_DP_PADCTL_TX_PU_ENABLE;
638 value &= ~SOR_DP_PADCTL_TX_PU_MASK;
639 value |= SOR_DP_PADCTL_TX_PU(2); /* XXX: don't hardcode? */
Thierry Reding880cee02017-10-12 19:04:17 +0200640 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100641
Thierry Reding880cee02017-10-12 19:04:17 +0200642 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100643 value |= SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
644 SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0;
Thierry Reding880cee02017-10-12 19:04:17 +0200645 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100646
647 usleep_range(10, 100);
648
Thierry Reding880cee02017-10-12 19:04:17 +0200649 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100650 value &= ~(SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
651 SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0);
Thierry Reding880cee02017-10-12 19:04:17 +0200652 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100653
Thierry Reding9542c232015-07-08 13:39:09 +0200654 err = drm_dp_aux_prepare(sor->aux, DP_SET_ANSI_8B10B);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100655 if (err < 0)
656 return err;
657
658 for (i = 0, value = 0; i < link->num_lanes; i++) {
659 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
660 SOR_DP_TPG_SCRAMBLER_NONE |
661 SOR_DP_TPG_PATTERN_TRAIN1;
662 value = (value << 8) | lane;
663 }
664
665 tegra_sor_writel(sor, value, SOR_DP_TPG);
666
667 pattern = DP_TRAINING_PATTERN_1;
668
Thierry Reding9542c232015-07-08 13:39:09 +0200669 err = drm_dp_aux_train(sor->aux, link, pattern);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100670 if (err < 0)
671 return err;
672
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200673 value = tegra_sor_readl(sor, SOR_DP_SPARE0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100674 value |= SOR_DP_SPARE_SEQ_ENABLE;
675 value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
676 value |= SOR_DP_SPARE_MACRO_SOR_CLK;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200677 tegra_sor_writel(sor, value, SOR_DP_SPARE0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100678
679 for (i = 0, value = 0; i < link->num_lanes; i++) {
680 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
681 SOR_DP_TPG_SCRAMBLER_NONE |
682 SOR_DP_TPG_PATTERN_TRAIN2;
683 value = (value << 8) | lane;
684 }
685
686 tegra_sor_writel(sor, value, SOR_DP_TPG);
687
688 pattern = DP_LINK_SCRAMBLING_DISABLE | DP_TRAINING_PATTERN_2;
689
Thierry Reding9542c232015-07-08 13:39:09 +0200690 err = drm_dp_aux_train(sor->aux, link, pattern);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100691 if (err < 0)
692 return err;
693
694 for (i = 0, value = 0; i < link->num_lanes; i++) {
695 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
696 SOR_DP_TPG_SCRAMBLER_GALIOS |
697 SOR_DP_TPG_PATTERN_NONE;
698 value = (value << 8) | lane;
699 }
700
701 tegra_sor_writel(sor, value, SOR_DP_TPG);
702
703 pattern = DP_TRAINING_PATTERN_DISABLE;
704
Thierry Reding9542c232015-07-08 13:39:09 +0200705 err = drm_dp_aux_train(sor->aux, link, pattern);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100706 if (err < 0)
707 return err;
708
709 return 0;
710}
711
712static void tegra_sor_super_update(struct tegra_sor *sor)
713{
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200714 tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
715 tegra_sor_writel(sor, 1, SOR_SUPER_STATE0);
716 tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100717}
718
719static void tegra_sor_update(struct tegra_sor *sor)
720{
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200721 tegra_sor_writel(sor, 0, SOR_STATE0);
722 tegra_sor_writel(sor, 1, SOR_STATE0);
723 tegra_sor_writel(sor, 0, SOR_STATE0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100724}
725
726static int tegra_sor_setup_pwm(struct tegra_sor *sor, unsigned long timeout)
727{
Thierry Reding28fe2072015-01-26 16:02:48 +0100728 u32 value;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100729
730 value = tegra_sor_readl(sor, SOR_PWM_DIV);
731 value &= ~SOR_PWM_DIV_MASK;
732 value |= 0x400; /* period */
733 tegra_sor_writel(sor, value, SOR_PWM_DIV);
734
735 value = tegra_sor_readl(sor, SOR_PWM_CTL);
736 value &= ~SOR_PWM_CTL_DUTY_CYCLE_MASK;
737 value |= 0x400; /* duty cycle */
738 value &= ~SOR_PWM_CTL_CLK_SEL; /* clock source: PCLK */
739 value |= SOR_PWM_CTL_TRIGGER;
740 tegra_sor_writel(sor, value, SOR_PWM_CTL);
741
742 timeout = jiffies + msecs_to_jiffies(timeout);
743
744 while (time_before(jiffies, timeout)) {
745 value = tegra_sor_readl(sor, SOR_PWM_CTL);
746 if ((value & SOR_PWM_CTL_TRIGGER) == 0)
747 return 0;
748
749 usleep_range(25, 100);
750 }
751
752 return -ETIMEDOUT;
753}
754
755static int tegra_sor_attach(struct tegra_sor *sor)
756{
757 unsigned long value, timeout;
758
759 /* wake up in normal mode */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200760 value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100761 value |= SOR_SUPER_STATE_HEAD_MODE_AWAKE;
762 value |= SOR_SUPER_STATE_MODE_NORMAL;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200763 tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100764 tegra_sor_super_update(sor);
765
766 /* attach */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200767 value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100768 value |= SOR_SUPER_STATE_ATTACHED;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200769 tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100770 tegra_sor_super_update(sor);
771
772 timeout = jiffies + msecs_to_jiffies(250);
773
774 while (time_before(jiffies, timeout)) {
775 value = tegra_sor_readl(sor, SOR_TEST);
776 if ((value & SOR_TEST_ATTACHED) != 0)
777 return 0;
778
779 usleep_range(25, 100);
780 }
781
782 return -ETIMEDOUT;
783}
784
785static int tegra_sor_wakeup(struct tegra_sor *sor)
786{
Thierry Reding6b6b6042013-11-15 16:06:05 +0100787 unsigned long value, timeout;
788
Thierry Reding6b6b6042013-11-15 16:06:05 +0100789 timeout = jiffies + msecs_to_jiffies(250);
790
791 /* wait for head to wake up */
792 while (time_before(jiffies, timeout)) {
793 value = tegra_sor_readl(sor, SOR_TEST);
794 value &= SOR_TEST_HEAD_MODE_MASK;
795
796 if (value == SOR_TEST_HEAD_MODE_AWAKE)
797 return 0;
798
799 usleep_range(25, 100);
800 }
801
802 return -ETIMEDOUT;
803}
804
805static int tegra_sor_power_up(struct tegra_sor *sor, unsigned long timeout)
806{
Thierry Reding28fe2072015-01-26 16:02:48 +0100807 u32 value;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100808
809 value = tegra_sor_readl(sor, SOR_PWR);
810 value |= SOR_PWR_TRIGGER | SOR_PWR_NORMAL_STATE_PU;
811 tegra_sor_writel(sor, value, SOR_PWR);
812
813 timeout = jiffies + msecs_to_jiffies(timeout);
814
815 while (time_before(jiffies, timeout)) {
816 value = tegra_sor_readl(sor, SOR_PWR);
817 if ((value & SOR_PWR_TRIGGER) == 0)
818 return 0;
819
820 usleep_range(25, 100);
821 }
822
823 return -ETIMEDOUT;
824}
825
Thierry Reding34fa1832014-06-05 16:31:10 +0200826struct tegra_sor_params {
827 /* number of link clocks per line */
828 unsigned int num_clocks;
829 /* ratio between input and output */
830 u64 ratio;
831 /* precision factor */
832 u64 precision;
833
834 unsigned int active_polarity;
835 unsigned int active_count;
836 unsigned int active_frac;
837 unsigned int tu_size;
838 unsigned int error;
839};
840
841static int tegra_sor_compute_params(struct tegra_sor *sor,
842 struct tegra_sor_params *params,
843 unsigned int tu_size)
844{
845 u64 active_sym, active_count, frac, approx;
846 u32 active_polarity, active_frac = 0;
847 const u64 f = params->precision;
848 s64 error;
849
850 active_sym = params->ratio * tu_size;
851 active_count = div_u64(active_sym, f) * f;
852 frac = active_sym - active_count;
853
854 /* fraction < 0.5 */
855 if (frac >= (f / 2)) {
856 active_polarity = 1;
857 frac = f - frac;
858 } else {
859 active_polarity = 0;
860 }
861
862 if (frac != 0) {
863 frac = div_u64(f * f, frac); /* 1/fraction */
864 if (frac <= (15 * f)) {
865 active_frac = div_u64(frac, f);
866
867 /* round up */
868 if (active_polarity)
869 active_frac++;
870 } else {
871 active_frac = active_polarity ? 1 : 15;
872 }
873 }
874
875 if (active_frac == 1)
876 active_polarity = 0;
877
878 if (active_polarity == 1) {
879 if (active_frac) {
880 approx = active_count + (active_frac * (f - 1)) * f;
881 approx = div_u64(approx, active_frac * f);
882 } else {
883 approx = active_count + f;
884 }
885 } else {
886 if (active_frac)
887 approx = active_count + div_u64(f, active_frac);
888 else
889 approx = active_count;
890 }
891
892 error = div_s64(active_sym - approx, tu_size);
893 error *= params->num_clocks;
894
Andrew Morton79211c82015-11-09 14:58:13 -0800895 if (error <= 0 && abs(error) < params->error) {
Thierry Reding34fa1832014-06-05 16:31:10 +0200896 params->active_count = div_u64(active_count, f);
897 params->active_polarity = active_polarity;
898 params->active_frac = active_frac;
Andrew Morton79211c82015-11-09 14:58:13 -0800899 params->error = abs(error);
Thierry Reding34fa1832014-06-05 16:31:10 +0200900 params->tu_size = tu_size;
901
902 if (error == 0)
903 return true;
904 }
905
906 return false;
907}
908
Thierry Redinga1983592015-07-21 16:46:52 +0200909static int tegra_sor_compute_config(struct tegra_sor *sor,
910 const struct drm_display_mode *mode,
911 struct tegra_sor_config *config,
912 struct drm_dp_link *link)
Thierry Reding34fa1832014-06-05 16:31:10 +0200913{
914 const u64 f = 100000, link_rate = link->rate * 1000;
915 const u64 pclk = mode->clock * 1000;
Thierry Reding7890b572014-06-05 16:12:46 +0200916 u64 input, output, watermark, num;
Thierry Reding34fa1832014-06-05 16:31:10 +0200917 struct tegra_sor_params params;
Thierry Reding34fa1832014-06-05 16:31:10 +0200918 u32 num_syms_per_line;
919 unsigned int i;
920
921 if (!link_rate || !link->num_lanes || !pclk || !config->bits_per_pixel)
922 return -EINVAL;
923
924 output = link_rate * 8 * link->num_lanes;
925 input = pclk * config->bits_per_pixel;
926
927 if (input >= output)
928 return -ERANGE;
929
930 memset(&params, 0, sizeof(params));
931 params.ratio = div64_u64(input * f, output);
932 params.num_clocks = div_u64(link_rate * mode->hdisplay, pclk);
933 params.precision = f;
934 params.error = 64 * f;
935 params.tu_size = 64;
936
937 for (i = params.tu_size; i >= 32; i--)
938 if (tegra_sor_compute_params(sor, &params, i))
939 break;
940
941 if (params.active_frac == 0) {
942 config->active_polarity = 0;
943 config->active_count = params.active_count;
944
945 if (!params.active_polarity)
946 config->active_count--;
947
948 config->tu_size = params.tu_size;
949 config->active_frac = 1;
950 } else {
951 config->active_polarity = params.active_polarity;
952 config->active_count = params.active_count;
953 config->active_frac = params.active_frac;
954 config->tu_size = params.tu_size;
955 }
956
957 dev_dbg(sor->dev,
958 "polarity: %d active count: %d tu size: %d active frac: %d\n",
959 config->active_polarity, config->active_count,
960 config->tu_size, config->active_frac);
961
962 watermark = params.ratio * config->tu_size * (f - params.ratio);
963 watermark = div_u64(watermark, f);
964
965 watermark = div_u64(watermark + params.error, f);
966 config->watermark = watermark + (config->bits_per_pixel / 8) + 2;
967 num_syms_per_line = (mode->hdisplay * config->bits_per_pixel) *
968 (link->num_lanes * 8);
969
970 if (config->watermark > 30) {
971 config->watermark = 30;
972 dev_err(sor->dev,
973 "unable to compute TU size, forcing watermark to %u\n",
974 config->watermark);
975 } else if (config->watermark > num_syms_per_line) {
976 config->watermark = num_syms_per_line;
977 dev_err(sor->dev, "watermark too high, forcing to %u\n",
978 config->watermark);
979 }
980
Thierry Reding7890b572014-06-05 16:12:46 +0200981 /* compute the number of symbols per horizontal blanking interval */
982 num = ((mode->htotal - mode->hdisplay) - 7) * link_rate;
983 config->hblank_symbols = div_u64(num, pclk);
984
985 if (link->capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
986 config->hblank_symbols -= 3;
987
988 config->hblank_symbols -= 12 / link->num_lanes;
989
990 /* compute the number of symbols per vertical blanking interval */
991 num = (mode->hdisplay - 25) * link_rate;
992 config->vblank_symbols = div_u64(num, pclk);
993 config->vblank_symbols -= 36 / link->num_lanes + 4;
994
995 dev_dbg(sor->dev, "blank symbols: H:%u V:%u\n", config->hblank_symbols,
996 config->vblank_symbols);
997
Thierry Reding34fa1832014-06-05 16:31:10 +0200998 return 0;
999}
1000
Thierry Reding402f6bc2015-07-21 16:48:19 +02001001static void tegra_sor_apply_config(struct tegra_sor *sor,
1002 const struct tegra_sor_config *config)
1003{
1004 u32 value;
1005
1006 value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
1007 value &= ~SOR_DP_LINKCTL_TU_SIZE_MASK;
1008 value |= SOR_DP_LINKCTL_TU_SIZE(config->tu_size);
1009 tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
1010
1011 value = tegra_sor_readl(sor, SOR_DP_CONFIG0);
1012 value &= ~SOR_DP_CONFIG_WATERMARK_MASK;
1013 value |= SOR_DP_CONFIG_WATERMARK(config->watermark);
1014
1015 value &= ~SOR_DP_CONFIG_ACTIVE_SYM_COUNT_MASK;
1016 value |= SOR_DP_CONFIG_ACTIVE_SYM_COUNT(config->active_count);
1017
1018 value &= ~SOR_DP_CONFIG_ACTIVE_SYM_FRAC_MASK;
1019 value |= SOR_DP_CONFIG_ACTIVE_SYM_FRAC(config->active_frac);
1020
1021 if (config->active_polarity)
1022 value |= SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
1023 else
1024 value &= ~SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
1025
1026 value |= SOR_DP_CONFIG_ACTIVE_SYM_ENABLE;
1027 value |= SOR_DP_CONFIG_DISPARITY_NEGATIVE;
1028 tegra_sor_writel(sor, value, SOR_DP_CONFIG0);
1029
1030 value = tegra_sor_readl(sor, SOR_DP_AUDIO_HBLANK_SYMBOLS);
1031 value &= ~SOR_DP_AUDIO_HBLANK_SYMBOLS_MASK;
1032 value |= config->hblank_symbols & 0xffff;
1033 tegra_sor_writel(sor, value, SOR_DP_AUDIO_HBLANK_SYMBOLS);
1034
1035 value = tegra_sor_readl(sor, SOR_DP_AUDIO_VBLANK_SYMBOLS);
1036 value &= ~SOR_DP_AUDIO_VBLANK_SYMBOLS_MASK;
1037 value |= config->vblank_symbols & 0xffff;
1038 tegra_sor_writel(sor, value, SOR_DP_AUDIO_VBLANK_SYMBOLS);
1039}
1040
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001041static void tegra_sor_mode_set(struct tegra_sor *sor,
1042 const struct drm_display_mode *mode,
Thierry Redingc31efa72015-09-08 16:09:22 +02001043 struct tegra_sor_state *state)
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001044{
1045 struct tegra_dc *dc = to_tegra_dc(sor->output.encoder.crtc);
1046 unsigned int vbe, vse, hbe, hse, vbs, hbs;
1047 u32 value;
1048
1049 value = tegra_sor_readl(sor, SOR_STATE1);
1050 value &= ~SOR_STATE_ASY_PIXELDEPTH_MASK;
1051 value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
1052 value &= ~SOR_STATE_ASY_OWNER_MASK;
1053
1054 value |= SOR_STATE_ASY_CRC_MODE_COMPLETE |
1055 SOR_STATE_ASY_OWNER(dc->pipe + 1);
1056
1057 if (mode->flags & DRM_MODE_FLAG_PHSYNC)
1058 value &= ~SOR_STATE_ASY_HSYNCPOL;
1059
1060 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1061 value |= SOR_STATE_ASY_HSYNCPOL;
1062
1063 if (mode->flags & DRM_MODE_FLAG_PVSYNC)
1064 value &= ~SOR_STATE_ASY_VSYNCPOL;
1065
1066 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1067 value |= SOR_STATE_ASY_VSYNCPOL;
1068
Thierry Redingc31efa72015-09-08 16:09:22 +02001069 switch (state->bpc) {
1070 case 16:
1071 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_48_444;
1072 break;
1073
1074 case 12:
1075 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_36_444;
1076 break;
1077
1078 case 10:
1079 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_30_444;
1080 break;
1081
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001082 case 8:
1083 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
1084 break;
1085
1086 case 6:
1087 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_18_444;
1088 break;
1089
1090 default:
Thierry Redingc31efa72015-09-08 16:09:22 +02001091 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001092 break;
1093 }
1094
1095 tegra_sor_writel(sor, value, SOR_STATE1);
1096
1097 /*
1098 * TODO: The video timing programming below doesn't seem to match the
1099 * register definitions.
1100 */
1101
1102 value = ((mode->vtotal & 0x7fff) << 16) | (mode->htotal & 0x7fff);
Thierry Reding880cee02017-10-12 19:04:17 +02001103 tegra_sor_writel(sor, value, sor->soc->regs->head_state1 + dc->pipe);
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001104
1105 /* sync end = sync width - 1 */
1106 vse = mode->vsync_end - mode->vsync_start - 1;
1107 hse = mode->hsync_end - mode->hsync_start - 1;
1108
1109 value = ((vse & 0x7fff) << 16) | (hse & 0x7fff);
Thierry Reding880cee02017-10-12 19:04:17 +02001110 tegra_sor_writel(sor, value, sor->soc->regs->head_state2 + dc->pipe);
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001111
1112 /* blank end = sync end + back porch */
1113 vbe = vse + (mode->vtotal - mode->vsync_end);
1114 hbe = hse + (mode->htotal - mode->hsync_end);
1115
1116 value = ((vbe & 0x7fff) << 16) | (hbe & 0x7fff);
Thierry Reding880cee02017-10-12 19:04:17 +02001117 tegra_sor_writel(sor, value, sor->soc->regs->head_state3 + dc->pipe);
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001118
1119 /* blank start = blank end + active */
1120 vbs = vbe + mode->vdisplay;
1121 hbs = hbe + mode->hdisplay;
1122
1123 value = ((vbs & 0x7fff) << 16) | (hbs & 0x7fff);
Thierry Reding880cee02017-10-12 19:04:17 +02001124 tegra_sor_writel(sor, value, sor->soc->regs->head_state4 + dc->pipe);
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001125
1126 /* XXX interlacing support */
Thierry Reding880cee02017-10-12 19:04:17 +02001127 tegra_sor_writel(sor, 0x001, sor->soc->regs->head_state5 + dc->pipe);
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001128}
1129
Thierry Reding6fad8f62014-11-28 15:41:34 +01001130static int tegra_sor_detach(struct tegra_sor *sor)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001131{
Thierry Reding6fad8f62014-11-28 15:41:34 +01001132 unsigned long value, timeout;
1133
1134 /* switch to safe mode */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001135 value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001136 value &= ~SOR_SUPER_STATE_MODE_NORMAL;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001137 tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001138 tegra_sor_super_update(sor);
1139
1140 timeout = jiffies + msecs_to_jiffies(250);
1141
1142 while (time_before(jiffies, timeout)) {
1143 value = tegra_sor_readl(sor, SOR_PWR);
1144 if (value & SOR_PWR_MODE_SAFE)
1145 break;
1146 }
1147
1148 if ((value & SOR_PWR_MODE_SAFE) == 0)
1149 return -ETIMEDOUT;
1150
1151 /* go to sleep */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001152 value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001153 value &= ~SOR_SUPER_STATE_HEAD_MODE_MASK;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001154 tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001155 tegra_sor_super_update(sor);
1156
1157 /* detach */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001158 value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001159 value &= ~SOR_SUPER_STATE_ATTACHED;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001160 tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001161 tegra_sor_super_update(sor);
1162
1163 timeout = jiffies + msecs_to_jiffies(250);
1164
1165 while (time_before(jiffies, timeout)) {
1166 value = tegra_sor_readl(sor, SOR_TEST);
1167 if ((value & SOR_TEST_ATTACHED) == 0)
1168 break;
1169
1170 usleep_range(25, 100);
1171 }
1172
1173 if ((value & SOR_TEST_ATTACHED) != 0)
1174 return -ETIMEDOUT;
1175
1176 return 0;
1177}
1178
1179static int tegra_sor_power_down(struct tegra_sor *sor)
1180{
1181 unsigned long value, timeout;
1182 int err;
1183
1184 value = tegra_sor_readl(sor, SOR_PWR);
1185 value &= ~SOR_PWR_NORMAL_STATE_PU;
1186 value |= SOR_PWR_TRIGGER;
1187 tegra_sor_writel(sor, value, SOR_PWR);
1188
1189 timeout = jiffies + msecs_to_jiffies(250);
1190
1191 while (time_before(jiffies, timeout)) {
1192 value = tegra_sor_readl(sor, SOR_PWR);
1193 if ((value & SOR_PWR_TRIGGER) == 0)
1194 return 0;
1195
1196 usleep_range(25, 100);
1197 }
1198
1199 if ((value & SOR_PWR_TRIGGER) != 0)
1200 return -ETIMEDOUT;
1201
Thierry Reding25bb2ce2015-08-03 14:23:29 +02001202 /* switch to safe parent clock */
1203 err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
Thierry Redinge1335e22017-10-12 17:53:11 +02001204 if (err < 0) {
Thierry Reding6fad8f62014-11-28 15:41:34 +01001205 dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
Thierry Redinge1335e22017-10-12 17:53:11 +02001206 return err;
1207 }
Thierry Reding6fad8f62014-11-28 15:41:34 +01001208
Thierry Reding880cee02017-10-12 19:04:17 +02001209 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001210 value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
1211 SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2);
Thierry Reding880cee02017-10-12 19:04:17 +02001212 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001213
1214 /* stop lane sequencer */
1215 value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_UP |
1216 SOR_LANE_SEQ_CTL_POWER_STATE_DOWN;
1217 tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
1218
1219 timeout = jiffies + msecs_to_jiffies(250);
1220
1221 while (time_before(jiffies, timeout)) {
1222 value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
1223 if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
1224 break;
1225
1226 usleep_range(25, 100);
1227 }
1228
1229 if ((value & SOR_LANE_SEQ_CTL_TRIGGER) != 0)
1230 return -ETIMEDOUT;
1231
Thierry Reding880cee02017-10-12 19:04:17 +02001232 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001233 value |= SOR_PLL2_PORT_POWERDOWN;
Thierry Reding880cee02017-10-12 19:04:17 +02001234 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001235
1236 usleep_range(20, 100);
1237
Thierry Reding880cee02017-10-12 19:04:17 +02001238 value = tegra_sor_readl(sor, sor->soc->regs->pll0);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001239 value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
Thierry Reding880cee02017-10-12 19:04:17 +02001240 tegra_sor_writel(sor, value, sor->soc->regs->pll0);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001241
Thierry Reding880cee02017-10-12 19:04:17 +02001242 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001243 value |= SOR_PLL2_SEQ_PLLCAPPD;
1244 value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
Thierry Reding880cee02017-10-12 19:04:17 +02001245 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001246
1247 usleep_range(20, 100);
1248
1249 return 0;
1250}
1251
Thierry Reding6fad8f62014-11-28 15:41:34 +01001252static int tegra_sor_crc_wait(struct tegra_sor *sor, unsigned long timeout)
1253{
1254 u32 value;
1255
1256 timeout = jiffies + msecs_to_jiffies(timeout);
1257
1258 while (time_before(jiffies, timeout)) {
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001259 value = tegra_sor_readl(sor, SOR_CRCA);
1260 if (value & SOR_CRCA_VALID)
Thierry Reding6fad8f62014-11-28 15:41:34 +01001261 return 0;
1262
1263 usleep_range(100, 200);
1264 }
1265
1266 return -ETIMEDOUT;
1267}
1268
Thierry Reding530239a2015-08-06 11:04:54 +02001269static int tegra_sor_show_crc(struct seq_file *s, void *data)
Thierry Reding6fad8f62014-11-28 15:41:34 +01001270{
Thierry Reding530239a2015-08-06 11:04:54 +02001271 struct drm_info_node *node = s->private;
1272 struct tegra_sor *sor = node->info_ent->data;
Thierry Reding850bab42015-07-29 17:58:41 +02001273 struct drm_crtc *crtc = sor->output.encoder.crtc;
1274 struct drm_device *drm = node->minor->dev;
Thierry Reding530239a2015-08-06 11:04:54 +02001275 int err = 0;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001276 u32 value;
1277
Thierry Reding850bab42015-07-29 17:58:41 +02001278 drm_modeset_lock_all(drm);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001279
Thierry Reding850bab42015-07-29 17:58:41 +02001280 if (!crtc || !crtc->state->active) {
1281 err = -EBUSY;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001282 goto unlock;
1283 }
1284
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001285 value = tegra_sor_readl(sor, SOR_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001286 value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001287 tegra_sor_writel(sor, value, SOR_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001288
1289 value = tegra_sor_readl(sor, SOR_CRC_CNTRL);
1290 value |= SOR_CRC_CNTRL_ENABLE;
1291 tegra_sor_writel(sor, value, SOR_CRC_CNTRL);
1292
1293 value = tegra_sor_readl(sor, SOR_TEST);
1294 value &= ~SOR_TEST_CRC_POST_SERIALIZE;
1295 tegra_sor_writel(sor, value, SOR_TEST);
1296
1297 err = tegra_sor_crc_wait(sor, 100);
1298 if (err < 0)
1299 goto unlock;
1300
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001301 tegra_sor_writel(sor, SOR_CRCA_RESET, SOR_CRCA);
1302 value = tegra_sor_readl(sor, SOR_CRCB);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001303
Thierry Reding530239a2015-08-06 11:04:54 +02001304 seq_printf(s, "%08x\n", value);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001305
1306unlock:
Thierry Reding850bab42015-07-29 17:58:41 +02001307 drm_modeset_unlock_all(drm);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001308 return err;
1309}
1310
Thierry Reding062f5b2c2017-11-10 12:21:51 +01001311#define DEBUGFS_REG32(_name) { .name = #_name, .offset = _name }
1312
1313static const struct debugfs_reg32 tegra_sor_regs[] = {
1314 DEBUGFS_REG32(SOR_CTXSW),
1315 DEBUGFS_REG32(SOR_SUPER_STATE0),
1316 DEBUGFS_REG32(SOR_SUPER_STATE1),
1317 DEBUGFS_REG32(SOR_STATE0),
1318 DEBUGFS_REG32(SOR_STATE1),
1319 DEBUGFS_REG32(SOR_HEAD_STATE0(0)),
1320 DEBUGFS_REG32(SOR_HEAD_STATE0(1)),
1321 DEBUGFS_REG32(SOR_HEAD_STATE1(0)),
1322 DEBUGFS_REG32(SOR_HEAD_STATE1(1)),
1323 DEBUGFS_REG32(SOR_HEAD_STATE2(0)),
1324 DEBUGFS_REG32(SOR_HEAD_STATE2(1)),
1325 DEBUGFS_REG32(SOR_HEAD_STATE3(0)),
1326 DEBUGFS_REG32(SOR_HEAD_STATE3(1)),
1327 DEBUGFS_REG32(SOR_HEAD_STATE4(0)),
1328 DEBUGFS_REG32(SOR_HEAD_STATE4(1)),
1329 DEBUGFS_REG32(SOR_HEAD_STATE5(0)),
1330 DEBUGFS_REG32(SOR_HEAD_STATE5(1)),
1331 DEBUGFS_REG32(SOR_CRC_CNTRL),
1332 DEBUGFS_REG32(SOR_DP_DEBUG_MVID),
1333 DEBUGFS_REG32(SOR_CLK_CNTRL),
1334 DEBUGFS_REG32(SOR_CAP),
1335 DEBUGFS_REG32(SOR_PWR),
1336 DEBUGFS_REG32(SOR_TEST),
1337 DEBUGFS_REG32(SOR_PLL0),
1338 DEBUGFS_REG32(SOR_PLL1),
1339 DEBUGFS_REG32(SOR_PLL2),
1340 DEBUGFS_REG32(SOR_PLL3),
1341 DEBUGFS_REG32(SOR_CSTM),
1342 DEBUGFS_REG32(SOR_LVDS),
1343 DEBUGFS_REG32(SOR_CRCA),
1344 DEBUGFS_REG32(SOR_CRCB),
1345 DEBUGFS_REG32(SOR_BLANK),
1346 DEBUGFS_REG32(SOR_SEQ_CTL),
1347 DEBUGFS_REG32(SOR_LANE_SEQ_CTL),
1348 DEBUGFS_REG32(SOR_SEQ_INST(0)),
1349 DEBUGFS_REG32(SOR_SEQ_INST(1)),
1350 DEBUGFS_REG32(SOR_SEQ_INST(2)),
1351 DEBUGFS_REG32(SOR_SEQ_INST(3)),
1352 DEBUGFS_REG32(SOR_SEQ_INST(4)),
1353 DEBUGFS_REG32(SOR_SEQ_INST(5)),
1354 DEBUGFS_REG32(SOR_SEQ_INST(6)),
1355 DEBUGFS_REG32(SOR_SEQ_INST(7)),
1356 DEBUGFS_REG32(SOR_SEQ_INST(8)),
1357 DEBUGFS_REG32(SOR_SEQ_INST(9)),
1358 DEBUGFS_REG32(SOR_SEQ_INST(10)),
1359 DEBUGFS_REG32(SOR_SEQ_INST(11)),
1360 DEBUGFS_REG32(SOR_SEQ_INST(12)),
1361 DEBUGFS_REG32(SOR_SEQ_INST(13)),
1362 DEBUGFS_REG32(SOR_SEQ_INST(14)),
1363 DEBUGFS_REG32(SOR_SEQ_INST(15)),
1364 DEBUGFS_REG32(SOR_PWM_DIV),
1365 DEBUGFS_REG32(SOR_PWM_CTL),
1366 DEBUGFS_REG32(SOR_VCRC_A0),
1367 DEBUGFS_REG32(SOR_VCRC_A1),
1368 DEBUGFS_REG32(SOR_VCRC_B0),
1369 DEBUGFS_REG32(SOR_VCRC_B1),
1370 DEBUGFS_REG32(SOR_CCRC_A0),
1371 DEBUGFS_REG32(SOR_CCRC_A1),
1372 DEBUGFS_REG32(SOR_CCRC_B0),
1373 DEBUGFS_REG32(SOR_CCRC_B1),
1374 DEBUGFS_REG32(SOR_EDATA_A0),
1375 DEBUGFS_REG32(SOR_EDATA_A1),
1376 DEBUGFS_REG32(SOR_EDATA_B0),
1377 DEBUGFS_REG32(SOR_EDATA_B1),
1378 DEBUGFS_REG32(SOR_COUNT_A0),
1379 DEBUGFS_REG32(SOR_COUNT_A1),
1380 DEBUGFS_REG32(SOR_COUNT_B0),
1381 DEBUGFS_REG32(SOR_COUNT_B1),
1382 DEBUGFS_REG32(SOR_DEBUG_A0),
1383 DEBUGFS_REG32(SOR_DEBUG_A1),
1384 DEBUGFS_REG32(SOR_DEBUG_B0),
1385 DEBUGFS_REG32(SOR_DEBUG_B1),
1386 DEBUGFS_REG32(SOR_TRIG),
1387 DEBUGFS_REG32(SOR_MSCHECK),
1388 DEBUGFS_REG32(SOR_XBAR_CTRL),
1389 DEBUGFS_REG32(SOR_XBAR_POL),
1390 DEBUGFS_REG32(SOR_DP_LINKCTL0),
1391 DEBUGFS_REG32(SOR_DP_LINKCTL1),
1392 DEBUGFS_REG32(SOR_LANE_DRIVE_CURRENT0),
1393 DEBUGFS_REG32(SOR_LANE_DRIVE_CURRENT1),
1394 DEBUGFS_REG32(SOR_LANE4_DRIVE_CURRENT0),
1395 DEBUGFS_REG32(SOR_LANE4_DRIVE_CURRENT1),
1396 DEBUGFS_REG32(SOR_LANE_PREEMPHASIS0),
1397 DEBUGFS_REG32(SOR_LANE_PREEMPHASIS1),
1398 DEBUGFS_REG32(SOR_LANE4_PREEMPHASIS0),
1399 DEBUGFS_REG32(SOR_LANE4_PREEMPHASIS1),
1400 DEBUGFS_REG32(SOR_LANE_POSTCURSOR0),
1401 DEBUGFS_REG32(SOR_LANE_POSTCURSOR1),
1402 DEBUGFS_REG32(SOR_DP_CONFIG0),
1403 DEBUGFS_REG32(SOR_DP_CONFIG1),
1404 DEBUGFS_REG32(SOR_DP_MN0),
1405 DEBUGFS_REG32(SOR_DP_MN1),
1406 DEBUGFS_REG32(SOR_DP_PADCTL0),
1407 DEBUGFS_REG32(SOR_DP_PADCTL1),
Thierry Redingc57997b2017-10-12 19:12:57 +02001408 DEBUGFS_REG32(SOR_DP_PADCTL2),
Thierry Reding062f5b2c2017-11-10 12:21:51 +01001409 DEBUGFS_REG32(SOR_DP_DEBUG0),
1410 DEBUGFS_REG32(SOR_DP_DEBUG1),
1411 DEBUGFS_REG32(SOR_DP_SPARE0),
1412 DEBUGFS_REG32(SOR_DP_SPARE1),
1413 DEBUGFS_REG32(SOR_DP_AUDIO_CTRL),
1414 DEBUGFS_REG32(SOR_DP_AUDIO_HBLANK_SYMBOLS),
1415 DEBUGFS_REG32(SOR_DP_AUDIO_VBLANK_SYMBOLS),
1416 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_HEADER),
1417 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK0),
1418 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK1),
1419 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK2),
1420 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK3),
1421 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK4),
1422 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK5),
1423 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK6),
1424 DEBUGFS_REG32(SOR_DP_TPG),
1425 DEBUGFS_REG32(SOR_DP_TPG_CONFIG),
1426 DEBUGFS_REG32(SOR_DP_LQ_CSTM0),
1427 DEBUGFS_REG32(SOR_DP_LQ_CSTM1),
1428 DEBUGFS_REG32(SOR_DP_LQ_CSTM2),
1429};
1430
Thierry Redingdab16332015-01-26 16:04:08 +01001431static int tegra_sor_show_regs(struct seq_file *s, void *data)
1432{
1433 struct drm_info_node *node = s->private;
1434 struct tegra_sor *sor = node->info_ent->data;
Thierry Reding850bab42015-07-29 17:58:41 +02001435 struct drm_crtc *crtc = sor->output.encoder.crtc;
1436 struct drm_device *drm = node->minor->dev;
Thierry Reding062f5b2c2017-11-10 12:21:51 +01001437 unsigned int i;
Thierry Reding850bab42015-07-29 17:58:41 +02001438 int err = 0;
1439
1440 drm_modeset_lock_all(drm);
1441
1442 if (!crtc || !crtc->state->active) {
1443 err = -EBUSY;
1444 goto unlock;
1445 }
Thierry Redingdab16332015-01-26 16:04:08 +01001446
Thierry Reding062f5b2c2017-11-10 12:21:51 +01001447 for (i = 0; i < ARRAY_SIZE(tegra_sor_regs); i++) {
1448 unsigned int offset = tegra_sor_regs[i].offset;
Thierry Redingdab16332015-01-26 16:04:08 +01001449
Thierry Reding062f5b2c2017-11-10 12:21:51 +01001450 seq_printf(s, "%-38s %#05x %08x\n", tegra_sor_regs[i].name,
1451 offset, tegra_sor_readl(sor, offset));
1452 }
Thierry Redingdab16332015-01-26 16:04:08 +01001453
Thierry Reding850bab42015-07-29 17:58:41 +02001454unlock:
1455 drm_modeset_unlock_all(drm);
1456 return err;
Thierry Redingdab16332015-01-26 16:04:08 +01001457}
1458
1459static const struct drm_info_list debugfs_files[] = {
Thierry Reding530239a2015-08-06 11:04:54 +02001460 { "crc", tegra_sor_show_crc, 0, NULL },
Thierry Redingdab16332015-01-26 16:04:08 +01001461 { "regs", tegra_sor_show_regs, 0, NULL },
1462};
1463
Thierry Reding5b8e0432017-11-08 13:20:01 +01001464static int tegra_sor_late_register(struct drm_connector *connector)
Thierry Reding6fad8f62014-11-28 15:41:34 +01001465{
Thierry Reding5b8e0432017-11-08 13:20:01 +01001466 struct tegra_output *output = connector_to_output(connector);
1467 unsigned int i, count = ARRAY_SIZE(debugfs_files);
1468 struct drm_minor *minor = connector->dev->primary;
1469 struct dentry *root = connector->debugfs_entry;
1470 struct tegra_sor *sor = to_sor(output);
Thierry Reding530239a2015-08-06 11:04:54 +02001471 int err;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001472
Thierry Redingdab16332015-01-26 16:04:08 +01001473 sor->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1474 GFP_KERNEL);
Thierry Reding5b8e0432017-11-08 13:20:01 +01001475 if (!sor->debugfs_files)
1476 return -ENOMEM;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001477
Thierry Reding5b8e0432017-11-08 13:20:01 +01001478 for (i = 0; i < count; i++)
Thierry Redingdab16332015-01-26 16:04:08 +01001479 sor->debugfs_files[i].data = sor;
1480
Thierry Reding5b8e0432017-11-08 13:20:01 +01001481 err = drm_debugfs_create_files(sor->debugfs_files, count, root, minor);
Thierry Redingdab16332015-01-26 16:04:08 +01001482 if (err < 0)
1483 goto free;
1484
Thierry Reding530239a2015-08-06 11:04:54 +02001485 return 0;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001486
Thierry Redingdab16332015-01-26 16:04:08 +01001487free:
1488 kfree(sor->debugfs_files);
1489 sor->debugfs_files = NULL;
Thierry Reding5b8e0432017-11-08 13:20:01 +01001490
Thierry Reding6fad8f62014-11-28 15:41:34 +01001491 return err;
1492}
1493
Thierry Reding5b8e0432017-11-08 13:20:01 +01001494static void tegra_sor_early_unregister(struct drm_connector *connector)
Thierry Reding6fad8f62014-11-28 15:41:34 +01001495{
Thierry Reding5b8e0432017-11-08 13:20:01 +01001496 struct tegra_output *output = connector_to_output(connector);
1497 unsigned int count = ARRAY_SIZE(debugfs_files);
1498 struct tegra_sor *sor = to_sor(output);
Thierry Redingd92e6002017-10-12 19:07:54 +02001499
Thierry Reding5b8e0432017-11-08 13:20:01 +01001500 drm_debugfs_remove_files(sor->debugfs_files, count,
1501 connector->dev->primary);
Thierry Redingdab16332015-01-26 16:04:08 +01001502 kfree(sor->debugfs_files);
Thierry Reding066d30f2015-07-03 14:16:30 +02001503 sor->debugfs_files = NULL;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001504}
1505
Thierry Redingc31efa72015-09-08 16:09:22 +02001506static void tegra_sor_connector_reset(struct drm_connector *connector)
1507{
1508 struct tegra_sor_state *state;
1509
1510 state = kzalloc(sizeof(*state), GFP_KERNEL);
1511 if (!state)
1512 return;
1513
1514 if (connector->state) {
1515 __drm_atomic_helper_connector_destroy_state(connector->state);
1516 kfree(connector->state);
1517 }
1518
1519 __drm_atomic_helper_connector_reset(connector, &state->base);
1520}
1521
Thierry Reding6fad8f62014-11-28 15:41:34 +01001522static enum drm_connector_status
1523tegra_sor_connector_detect(struct drm_connector *connector, bool force)
1524{
1525 struct tegra_output *output = connector_to_output(connector);
1526 struct tegra_sor *sor = to_sor(output);
1527
Thierry Reding9542c232015-07-08 13:39:09 +02001528 if (sor->aux)
1529 return drm_dp_aux_detect(sor->aux);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001530
Thierry Reding459cc2c2015-07-30 10:34:24 +02001531 return tegra_output_connector_detect(connector, force);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001532}
1533
Thierry Redingc31efa72015-09-08 16:09:22 +02001534static struct drm_connector_state *
1535tegra_sor_connector_duplicate_state(struct drm_connector *connector)
1536{
1537 struct tegra_sor_state *state = to_sor_state(connector->state);
1538 struct tegra_sor_state *copy;
1539
1540 copy = kmemdup(state, sizeof(*state), GFP_KERNEL);
1541 if (!copy)
1542 return NULL;
1543
1544 __drm_atomic_helper_connector_duplicate_state(connector, &copy->base);
1545
1546 return &copy->base;
1547}
1548
Thierry Reding6fad8f62014-11-28 15:41:34 +01001549static const struct drm_connector_funcs tegra_sor_connector_funcs = {
Thierry Redingc31efa72015-09-08 16:09:22 +02001550 .reset = tegra_sor_connector_reset,
Thierry Reding6fad8f62014-11-28 15:41:34 +01001551 .detect = tegra_sor_connector_detect,
1552 .fill_modes = drm_helper_probe_single_connector_modes,
1553 .destroy = tegra_output_connector_destroy,
Thierry Redingc31efa72015-09-08 16:09:22 +02001554 .atomic_duplicate_state = tegra_sor_connector_duplicate_state,
Thierry Reding4aa3df72014-11-24 16:27:13 +01001555 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
Thierry Reding5b8e0432017-11-08 13:20:01 +01001556 .late_register = tegra_sor_late_register,
1557 .early_unregister = tegra_sor_early_unregister,
Thierry Reding6fad8f62014-11-28 15:41:34 +01001558};
1559
1560static int tegra_sor_connector_get_modes(struct drm_connector *connector)
1561{
1562 struct tegra_output *output = connector_to_output(connector);
1563 struct tegra_sor *sor = to_sor(output);
1564 int err;
1565
Thierry Reding9542c232015-07-08 13:39:09 +02001566 if (sor->aux)
1567 drm_dp_aux_enable(sor->aux);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001568
1569 err = tegra_output_connector_get_modes(connector);
1570
Thierry Reding9542c232015-07-08 13:39:09 +02001571 if (sor->aux)
1572 drm_dp_aux_disable(sor->aux);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001573
1574 return err;
1575}
1576
1577static enum drm_mode_status
1578tegra_sor_connector_mode_valid(struct drm_connector *connector,
1579 struct drm_display_mode *mode)
1580{
1581 return MODE_OK;
1582}
1583
1584static const struct drm_connector_helper_funcs tegra_sor_connector_helper_funcs = {
1585 .get_modes = tegra_sor_connector_get_modes,
1586 .mode_valid = tegra_sor_connector_mode_valid,
Thierry Reding6fad8f62014-11-28 15:41:34 +01001587};
1588
1589static const struct drm_encoder_funcs tegra_sor_encoder_funcs = {
1590 .destroy = tegra_output_encoder_destroy,
1591};
1592
Thierry Reding850bab42015-07-29 17:58:41 +02001593static void tegra_sor_edp_disable(struct drm_encoder *encoder)
Thierry Reding6fad8f62014-11-28 15:41:34 +01001594{
Thierry Reding850bab42015-07-29 17:58:41 +02001595 struct tegra_output *output = encoder_to_output(encoder);
1596 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
1597 struct tegra_sor *sor = to_sor(output);
1598 u32 value;
1599 int err;
1600
1601 if (output->panel)
1602 drm_panel_disable(output->panel);
1603
1604 err = tegra_sor_detach(sor);
1605 if (err < 0)
1606 dev_err(sor->dev, "failed to detach SOR: %d\n", err);
1607
1608 tegra_sor_writel(sor, 0, SOR_STATE1);
1609 tegra_sor_update(sor);
1610
1611 /*
1612 * The following accesses registers of the display controller, so make
1613 * sure it's only executed when the output is attached to one.
1614 */
1615 if (dc) {
1616 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
Thierry Redingc57997b2017-10-12 19:12:57 +02001617 value &= ~SOR_ENABLE(0);
Thierry Reding850bab42015-07-29 17:58:41 +02001618 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
1619
1620 tegra_dc_commit(dc);
1621 }
1622
1623 err = tegra_sor_power_down(sor);
1624 if (err < 0)
1625 dev_err(sor->dev, "failed to power down SOR: %d\n", err);
1626
Thierry Reding9542c232015-07-08 13:39:09 +02001627 if (sor->aux) {
1628 err = drm_dp_aux_disable(sor->aux);
Thierry Reding850bab42015-07-29 17:58:41 +02001629 if (err < 0)
1630 dev_err(sor->dev, "failed to disable DP: %d\n", err);
1631 }
1632
Thierry Redingc57997b2017-10-12 19:12:57 +02001633 err = tegra_io_pad_power_disable(sor->pad);
Thierry Reding850bab42015-07-29 17:58:41 +02001634 if (err < 0)
Thierry Redingc57997b2017-10-12 19:12:57 +02001635 dev_err(sor->dev, "failed to power off I/O pad: %d\n", err);
Thierry Reding850bab42015-07-29 17:58:41 +02001636
1637 if (output->panel)
1638 drm_panel_unprepare(output->panel);
1639
Thierry Redingaaff8bd2015-08-07 16:04:54 +02001640 pm_runtime_put(sor->dev);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001641}
1642
Thierry Reding459cc2c2015-07-30 10:34:24 +02001643#if 0
1644static int calc_h_ref_to_sync(const struct drm_display_mode *mode,
1645 unsigned int *value)
1646{
1647 unsigned int hfp, hsw, hbp, a = 0, b;
1648
1649 hfp = mode->hsync_start - mode->hdisplay;
1650 hsw = mode->hsync_end - mode->hsync_start;
1651 hbp = mode->htotal - mode->hsync_end;
1652
1653 pr_info("hfp: %u, hsw: %u, hbp: %u\n", hfp, hsw, hbp);
1654
1655 b = hfp - 1;
1656
1657 pr_info("a: %u, b: %u\n", a, b);
1658 pr_info("a + hsw + hbp = %u\n", a + hsw + hbp);
1659
1660 if (a + hsw + hbp <= 11) {
1661 a = 1 + 11 - hsw - hbp;
1662 pr_info("a: %u\n", a);
1663 }
1664
1665 if (a > b)
1666 return -EINVAL;
1667
1668 if (hsw < 1)
1669 return -EINVAL;
1670
1671 if (mode->hdisplay < 16)
1672 return -EINVAL;
1673
1674 if (value) {
1675 if (b > a && a % 2)
1676 *value = a + 1;
1677 else
1678 *value = a;
1679 }
1680
1681 return 0;
1682}
1683#endif
1684
Thierry Reding850bab42015-07-29 17:58:41 +02001685static void tegra_sor_edp_enable(struct drm_encoder *encoder)
Thierry Reding6fad8f62014-11-28 15:41:34 +01001686{
Thierry Reding850bab42015-07-29 17:58:41 +02001687 struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001688 struct tegra_output *output = encoder_to_output(encoder);
1689 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001690 struct tegra_sor *sor = to_sor(output);
Thierry Reding34fa1832014-06-05 16:31:10 +02001691 struct tegra_sor_config config;
Thierry Redingc31efa72015-09-08 16:09:22 +02001692 struct tegra_sor_state *state;
Thierry Reding34fa1832014-06-05 16:31:10 +02001693 struct drm_dp_link link;
Thierry Reding01b9bea2015-11-11 17:15:29 +01001694 u8 rate, lanes;
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001695 unsigned int i;
Thierry Reding86f5c522014-03-26 11:13:16 +01001696 int err = 0;
Thierry Reding28fe2072015-01-26 16:02:48 +01001697 u32 value;
Thierry Reding86f5c522014-03-26 11:13:16 +01001698
Thierry Redingc31efa72015-09-08 16:09:22 +02001699 state = to_sor_state(output->connector.state);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001700
Thierry Redingaaff8bd2015-08-07 16:04:54 +02001701 pm_runtime_get_sync(sor->dev);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001702
Thierry Reding6fad8f62014-11-28 15:41:34 +01001703 if (output->panel)
1704 drm_panel_prepare(output->panel);
1705
Thierry Reding01b9bea2015-11-11 17:15:29 +01001706 err = drm_dp_aux_enable(sor->aux);
1707 if (err < 0)
1708 dev_err(sor->dev, "failed to enable DP: %d\n", err);
Thierry Reding34fa1832014-06-05 16:31:10 +02001709
Thierry Reding01b9bea2015-11-11 17:15:29 +01001710 err = drm_dp_link_probe(sor->aux, &link);
1711 if (err < 0) {
1712 dev_err(sor->dev, "failed to probe eDP link: %d\n", err);
1713 return;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001714 }
1715
Thierry Reding25bb2ce2015-08-03 14:23:29 +02001716 /* switch to safe parent clock */
1717 err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001718 if (err < 0)
1719 dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
1720
Thierry Reding34fa1832014-06-05 16:31:10 +02001721 memset(&config, 0, sizeof(config));
Thierry Redingc31efa72015-09-08 16:09:22 +02001722 config.bits_per_pixel = state->bpc * 3;
Thierry Reding34fa1832014-06-05 16:31:10 +02001723
Thierry Redinga1983592015-07-21 16:46:52 +02001724 err = tegra_sor_compute_config(sor, mode, &config, &link);
Thierry Reding34fa1832014-06-05 16:31:10 +02001725 if (err < 0)
Thierry Redinga1983592015-07-21 16:46:52 +02001726 dev_err(sor->dev, "failed to compute configuration: %d\n", err);
Thierry Reding34fa1832014-06-05 16:31:10 +02001727
Thierry Reding6b6b6042013-11-15 16:06:05 +01001728 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
1729 value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
1730 value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
1731 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
1732
Thierry Reding880cee02017-10-12 19:04:17 +02001733 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001734 value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
Thierry Reding880cee02017-10-12 19:04:17 +02001735 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001736 usleep_range(20, 100);
1737
Thierry Reding880cee02017-10-12 19:04:17 +02001738 value = tegra_sor_readl(sor, sor->soc->regs->pll3);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001739 value |= SOR_PLL3_PLL_VDD_MODE_3V3;
Thierry Reding880cee02017-10-12 19:04:17 +02001740 tegra_sor_writel(sor, value, sor->soc->regs->pll3);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001741
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001742 value = SOR_PLL0_ICHPMP(0xf) | SOR_PLL0_VCOCAP_RST |
1743 SOR_PLL0_PLLREG_LEVEL_V45 | SOR_PLL0_RESISTOR_EXT;
Thierry Reding880cee02017-10-12 19:04:17 +02001744 tegra_sor_writel(sor, value, sor->soc->regs->pll0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001745
Thierry Reding880cee02017-10-12 19:04:17 +02001746 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001747 value |= SOR_PLL2_SEQ_PLLCAPPD;
1748 value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1749 value |= SOR_PLL2_LVDS_ENABLE;
Thierry Reding880cee02017-10-12 19:04:17 +02001750 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001751
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001752 value = SOR_PLL1_TERM_COMPOUT | SOR_PLL1_TMDS_TERM;
Thierry Reding880cee02017-10-12 19:04:17 +02001753 tegra_sor_writel(sor, value, sor->soc->regs->pll1);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001754
1755 while (true) {
Thierry Reding880cee02017-10-12 19:04:17 +02001756 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001757 if ((value & SOR_PLL2_SEQ_PLLCAPPD_ENFORCE) == 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001758 break;
1759
1760 usleep_range(250, 1000);
1761 }
1762
Thierry Reding880cee02017-10-12 19:04:17 +02001763 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001764 value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
1765 value &= ~SOR_PLL2_PORT_POWERDOWN;
Thierry Reding880cee02017-10-12 19:04:17 +02001766 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001767
1768 /*
1769 * power up
1770 */
1771
1772 /* set safe link bandwidth (1.62 Gbps) */
1773 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
1774 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
1775 value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G1_62;
1776 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
1777
1778 /* step 1 */
Thierry Reding880cee02017-10-12 19:04:17 +02001779 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001780 value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE | SOR_PLL2_PORT_POWERDOWN |
1781 SOR_PLL2_BANDGAP_POWERDOWN;
Thierry Reding880cee02017-10-12 19:04:17 +02001782 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001783
Thierry Reding880cee02017-10-12 19:04:17 +02001784 value = tegra_sor_readl(sor, sor->soc->regs->pll0);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001785 value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
Thierry Reding880cee02017-10-12 19:04:17 +02001786 tegra_sor_writel(sor, value, sor->soc->regs->pll0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001787
Thierry Reding880cee02017-10-12 19:04:17 +02001788 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001789 value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
Thierry Reding880cee02017-10-12 19:04:17 +02001790 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001791
1792 /* step 2 */
Thierry Redingc57997b2017-10-12 19:12:57 +02001793 err = tegra_io_pad_power_enable(sor->pad);
Thierry Reding850bab42015-07-29 17:58:41 +02001794 if (err < 0)
Thierry Redingc57997b2017-10-12 19:12:57 +02001795 dev_err(sor->dev, "failed to power on I/O pad: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001796
1797 usleep_range(5, 100);
1798
1799 /* step 3 */
Thierry Reding880cee02017-10-12 19:04:17 +02001800 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001801 value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
Thierry Reding880cee02017-10-12 19:04:17 +02001802 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001803
1804 usleep_range(20, 100);
1805
1806 /* step 4 */
Thierry Reding880cee02017-10-12 19:04:17 +02001807 value = tegra_sor_readl(sor, sor->soc->regs->pll0);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001808 value &= ~SOR_PLL0_VCOPD;
1809 value &= ~SOR_PLL0_PWR;
Thierry Reding880cee02017-10-12 19:04:17 +02001810 tegra_sor_writel(sor, value, sor->soc->regs->pll0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001811
Thierry Reding880cee02017-10-12 19:04:17 +02001812 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001813 value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
Thierry Reding880cee02017-10-12 19:04:17 +02001814 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001815
1816 usleep_range(200, 1000);
1817
1818 /* step 5 */
Thierry Reding880cee02017-10-12 19:04:17 +02001819 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001820 value &= ~SOR_PLL2_PORT_POWERDOWN;
Thierry Reding880cee02017-10-12 19:04:17 +02001821 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001822
Thierry Reding30b49432015-08-03 15:50:32 +02001823 /* XXX not in TRM */
1824 for (value = 0, i = 0; i < 5; i++)
1825 value |= SOR_XBAR_CTRL_LINK0_XSEL(i, sor->soc->xbar_cfg[i]) |
1826 SOR_XBAR_CTRL_LINK1_XSEL(i, i);
1827
1828 tegra_sor_writel(sor, 0x00000000, SOR_XBAR_POL);
1829 tegra_sor_writel(sor, value, SOR_XBAR_CTRL);
1830
Thierry Reding25bb2ce2015-08-03 14:23:29 +02001831 /* switch to DP parent clock */
1832 err = tegra_sor_set_parent_clock(sor, sor->clk_dp);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001833 if (err < 0)
Thierry Reding25bb2ce2015-08-03 14:23:29 +02001834 dev_err(sor->dev, "failed to set parent clock: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001835
Thierry Reding899451b2014-06-05 16:19:48 +02001836 /* power DP lanes */
Thierry Reding880cee02017-10-12 19:04:17 +02001837 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding899451b2014-06-05 16:19:48 +02001838
1839 if (link.num_lanes <= 2)
1840 value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2);
1841 else
1842 value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2;
1843
1844 if (link.num_lanes <= 1)
1845 value &= ~SOR_DP_PADCTL_PD_TXD_1;
1846 else
1847 value |= SOR_DP_PADCTL_PD_TXD_1;
1848
1849 if (link.num_lanes == 0)
1850 value &= ~SOR_DP_PADCTL_PD_TXD_0;
1851 else
1852 value |= SOR_DP_PADCTL_PD_TXD_0;
1853
Thierry Reding880cee02017-10-12 19:04:17 +02001854 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001855
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001856 value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001857 value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
Thierry Reding0c90a182014-06-05 16:29:46 +02001858 value |= SOR_DP_LINKCTL_LANE_COUNT(link.num_lanes);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001859 tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001860
1861 /* start lane sequencer */
1862 value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
1863 SOR_LANE_SEQ_CTL_POWER_STATE_UP;
1864 tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
1865
1866 while (true) {
1867 value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
1868 if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
1869 break;
1870
1871 usleep_range(250, 1000);
1872 }
1873
Thierry Redinga4263fe2014-06-05 16:16:23 +02001874 /* set link bandwidth */
Thierry Reding6b6b6042013-11-15 16:06:05 +01001875 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
1876 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
Thierry Redinga4263fe2014-06-05 16:16:23 +02001877 value |= drm_dp_link_rate_to_bw_code(link.rate) << 2;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001878 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
1879
Thierry Reding402f6bc2015-07-21 16:48:19 +02001880 tegra_sor_apply_config(sor, &config);
1881
1882 /* enable link */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001883 value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001884 value |= SOR_DP_LINKCTL_ENABLE;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001885 value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001886 tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001887
1888 for (i = 0, value = 0; i < 4; i++) {
1889 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
1890 SOR_DP_TPG_SCRAMBLER_GALIOS |
1891 SOR_DP_TPG_PATTERN_NONE;
1892 value = (value << 8) | lane;
1893 }
1894
1895 tegra_sor_writel(sor, value, SOR_DP_TPG);
1896
Thierry Reding6b6b6042013-11-15 16:06:05 +01001897 /* enable pad calibration logic */
Thierry Reding880cee02017-10-12 19:04:17 +02001898 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001899 value |= SOR_DP_PADCTL_PAD_CAL_PD;
Thierry Reding880cee02017-10-12 19:04:17 +02001900 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001901
Thierry Reding01b9bea2015-11-11 17:15:29 +01001902 err = drm_dp_link_probe(sor->aux, &link);
1903 if (err < 0)
1904 dev_err(sor->dev, "failed to probe 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_power_up(sor->aux, &link);
1907 if (err < 0)
1908 dev_err(sor->dev, "failed to power up eDP link: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001909
Thierry Reding01b9bea2015-11-11 17:15:29 +01001910 err = drm_dp_link_configure(sor->aux, &link);
1911 if (err < 0)
1912 dev_err(sor->dev, "failed to configure eDP link: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001913
Thierry Reding01b9bea2015-11-11 17:15:29 +01001914 rate = drm_dp_link_rate_to_bw_code(link.rate);
1915 lanes = link.num_lanes;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001916
Thierry Reding01b9bea2015-11-11 17:15:29 +01001917 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
1918 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
1919 value |= SOR_CLK_CNTRL_DP_LINK_SPEED(rate);
1920 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001921
Thierry Reding01b9bea2015-11-11 17:15:29 +01001922 value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
1923 value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
1924 value |= SOR_DP_LINKCTL_LANE_COUNT(lanes);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001925
Thierry Reding01b9bea2015-11-11 17:15:29 +01001926 if (link.capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
1927 value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001928
Thierry Reding01b9bea2015-11-11 17:15:29 +01001929 tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001930
Thierry Reding01b9bea2015-11-11 17:15:29 +01001931 /* disable training pattern generator */
Thierry Reding6b6b6042013-11-15 16:06:05 +01001932
Thierry Reding01b9bea2015-11-11 17:15:29 +01001933 for (i = 0; i < link.num_lanes; i++) {
1934 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
1935 SOR_DP_TPG_SCRAMBLER_GALIOS |
1936 SOR_DP_TPG_PATTERN_NONE;
1937 value = (value << 8) | lane;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001938 }
1939
Thierry Reding01b9bea2015-11-11 17:15:29 +01001940 tegra_sor_writel(sor, value, SOR_DP_TPG);
1941
1942 err = tegra_sor_dp_train_fast(sor, &link);
1943 if (err < 0)
1944 dev_err(sor->dev, "DP fast link training failed: %d\n", err);
1945
1946 dev_dbg(sor->dev, "fast link training succeeded\n");
1947
Thierry Reding6b6b6042013-11-15 16:06:05 +01001948 err = tegra_sor_power_up(sor, 250);
Thierry Reding850bab42015-07-29 17:58:41 +02001949 if (err < 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001950 dev_err(sor->dev, "failed to power up SOR: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001951
Thierry Reding6b6b6042013-11-15 16:06:05 +01001952 /* CSTM (LVDS, link A/B, upper) */
Stéphane Marchesin143b1df2014-05-22 20:32:47 -07001953 value = SOR_CSTM_LVDS | SOR_CSTM_LINK_ACT_A | SOR_CSTM_LINK_ACT_B |
Thierry Reding6b6b6042013-11-15 16:06:05 +01001954 SOR_CSTM_UPPER;
1955 tegra_sor_writel(sor, value, SOR_CSTM);
1956
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001957 /* use DP-A protocol */
1958 value = tegra_sor_readl(sor, SOR_STATE1);
1959 value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
1960 value |= SOR_STATE_ASY_PROTOCOL_DP_A;
1961 tegra_sor_writel(sor, value, SOR_STATE1);
1962
Thierry Redingc31efa72015-09-08 16:09:22 +02001963 tegra_sor_mode_set(sor, mode, state);
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001964
Thierry Reding6b6b6042013-11-15 16:06:05 +01001965 /* PWM setup */
1966 err = tegra_sor_setup_pwm(sor, 250);
Thierry Reding850bab42015-07-29 17:58:41 +02001967 if (err < 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001968 dev_err(sor->dev, "failed to setup PWM: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001969
Thierry Reding666cb872014-12-08 16:32:47 +01001970 tegra_sor_update(sor);
1971
Thierry Reding6b6b6042013-11-15 16:06:05 +01001972 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
Thierry Redingc57997b2017-10-12 19:12:57 +02001973 value |= SOR_ENABLE(0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001974 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
1975
Thierry Reding666cb872014-12-08 16:32:47 +01001976 tegra_dc_commit(dc);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001977
1978 err = tegra_sor_attach(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 attach SOR: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001981
1982 err = tegra_sor_wakeup(sor);
Thierry Reding850bab42015-07-29 17:58:41 +02001983 if (err < 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001984 dev_err(sor->dev, "failed to enable DC: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001985
Thierry Reding6fad8f62014-11-28 15:41:34 +01001986 if (output->panel)
1987 drm_panel_enable(output->panel);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001988}
1989
Thierry Reding82f15112014-12-08 17:26:46 +01001990static int
1991tegra_sor_encoder_atomic_check(struct drm_encoder *encoder,
1992 struct drm_crtc_state *crtc_state,
1993 struct drm_connector_state *conn_state)
1994{
1995 struct tegra_output *output = encoder_to_output(encoder);
Thierry Redingc31efa72015-09-08 16:09:22 +02001996 struct tegra_sor_state *state = to_sor_state(conn_state);
Thierry Reding82f15112014-12-08 17:26:46 +01001997 struct tegra_dc *dc = to_tegra_dc(conn_state->crtc);
1998 unsigned long pclk = crtc_state->mode.clock * 1000;
1999 struct tegra_sor *sor = to_sor(output);
Thierry Redingc31efa72015-09-08 16:09:22 +02002000 struct drm_display_info *info;
Thierry Reding82f15112014-12-08 17:26:46 +01002001 int err;
2002
Thierry Redingc31efa72015-09-08 16:09:22 +02002003 info = &output->connector.display_info;
2004
Thierry Reding36e90222017-10-12 19:14:21 +02002005 /*
2006 * For HBR2 modes, the SOR brick needs to use the x20 multiplier, so
2007 * the pixel clock must be corrected accordingly.
2008 */
2009 if (pclk >= 340000000) {
2010 state->link_speed = 20;
2011 state->pclk = pclk / 2;
2012 } else {
2013 state->link_speed = 10;
2014 state->pclk = pclk;
2015 }
2016
Thierry Reding82f15112014-12-08 17:26:46 +01002017 err = tegra_dc_state_setup_clock(dc, crtc_state, sor->clk_parent,
2018 pclk, 0);
2019 if (err < 0) {
2020 dev_err(output->dev, "failed to setup CRTC state: %d\n", err);
2021 return err;
2022 }
2023
Thierry Redingc31efa72015-09-08 16:09:22 +02002024 switch (info->bpc) {
2025 case 8:
2026 case 6:
2027 state->bpc = info->bpc;
2028 break;
2029
2030 default:
2031 DRM_DEBUG_KMS("%u bits-per-color not supported\n", info->bpc);
2032 state->bpc = 8;
2033 break;
2034 }
2035
Thierry Reding82f15112014-12-08 17:26:46 +01002036 return 0;
2037}
2038
Thierry Reding459cc2c2015-07-30 10:34:24 +02002039static const struct drm_encoder_helper_funcs tegra_sor_edp_helpers = {
Thierry Reding850bab42015-07-29 17:58:41 +02002040 .disable = tegra_sor_edp_disable,
2041 .enable = tegra_sor_edp_enable,
Thierry Reding82f15112014-12-08 17:26:46 +01002042 .atomic_check = tegra_sor_encoder_atomic_check,
Thierry Reding6b6b6042013-11-15 16:06:05 +01002043};
2044
Thierry Reding459cc2c2015-07-30 10:34:24 +02002045static inline u32 tegra_sor_hdmi_subpack(const u8 *ptr, size_t size)
2046{
2047 u32 value = 0;
2048 size_t i;
2049
2050 for (i = size; i > 0; i--)
2051 value = (value << 8) | ptr[i - 1];
2052
2053 return value;
2054}
2055
2056static void tegra_sor_hdmi_write_infopack(struct tegra_sor *sor,
2057 const void *data, size_t size)
2058{
2059 const u8 *ptr = data;
2060 unsigned long offset;
2061 size_t i, j;
2062 u32 value;
2063
2064 switch (ptr[0]) {
2065 case HDMI_INFOFRAME_TYPE_AVI:
2066 offset = SOR_HDMI_AVI_INFOFRAME_HEADER;
2067 break;
2068
2069 case HDMI_INFOFRAME_TYPE_AUDIO:
2070 offset = SOR_HDMI_AUDIO_INFOFRAME_HEADER;
2071 break;
2072
2073 case HDMI_INFOFRAME_TYPE_VENDOR:
2074 offset = SOR_HDMI_VSI_INFOFRAME_HEADER;
2075 break;
2076
2077 default:
2078 dev_err(sor->dev, "unsupported infoframe type: %02x\n",
2079 ptr[0]);
2080 return;
2081 }
2082
2083 value = INFOFRAME_HEADER_TYPE(ptr[0]) |
2084 INFOFRAME_HEADER_VERSION(ptr[1]) |
2085 INFOFRAME_HEADER_LEN(ptr[2]);
2086 tegra_sor_writel(sor, value, offset);
2087 offset++;
2088
2089 /*
2090 * Each subpack contains 7 bytes, divided into:
2091 * - subpack_low: bytes 0 - 3
2092 * - subpack_high: bytes 4 - 6 (with byte 7 padded to 0x00)
2093 */
2094 for (i = 3, j = 0; i < size; i += 7, j += 8) {
2095 size_t rem = size - i, num = min_t(size_t, rem, 4);
2096
2097 value = tegra_sor_hdmi_subpack(&ptr[i], num);
2098 tegra_sor_writel(sor, value, offset++);
2099
2100 num = min_t(size_t, rem - num, 3);
2101
2102 value = tegra_sor_hdmi_subpack(&ptr[i + 4], num);
2103 tegra_sor_writel(sor, value, offset++);
2104 }
2105}
2106
2107static int
2108tegra_sor_hdmi_setup_avi_infoframe(struct tegra_sor *sor,
2109 const struct drm_display_mode *mode)
2110{
2111 u8 buffer[HDMI_INFOFRAME_SIZE(AVI)];
2112 struct hdmi_avi_infoframe frame;
2113 u32 value;
2114 int err;
2115
2116 /* disable AVI infoframe */
2117 value = tegra_sor_readl(sor, SOR_HDMI_AVI_INFOFRAME_CTRL);
2118 value &= ~INFOFRAME_CTRL_SINGLE;
2119 value &= ~INFOFRAME_CTRL_OTHER;
2120 value &= ~INFOFRAME_CTRL_ENABLE;
2121 tegra_sor_writel(sor, value, SOR_HDMI_AVI_INFOFRAME_CTRL);
2122
Shashank Sharma0c1f5282017-07-13 21:03:07 +05302123 err = drm_hdmi_avi_infoframe_from_display_mode(&frame, mode, false);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002124 if (err < 0) {
2125 dev_err(sor->dev, "failed to setup AVI infoframe: %d\n", err);
2126 return err;
2127 }
2128
2129 err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
2130 if (err < 0) {
2131 dev_err(sor->dev, "failed to pack AVI infoframe: %d\n", err);
2132 return err;
2133 }
2134
2135 tegra_sor_hdmi_write_infopack(sor, buffer, err);
2136
2137 /* enable AVI infoframe */
2138 value = tegra_sor_readl(sor, SOR_HDMI_AVI_INFOFRAME_CTRL);
2139 value |= INFOFRAME_CTRL_CHECKSUM_ENABLE;
2140 value |= INFOFRAME_CTRL_ENABLE;
2141 tegra_sor_writel(sor, value, SOR_HDMI_AVI_INFOFRAME_CTRL);
2142
2143 return 0;
2144}
2145
Thierry Reding8e2988a72018-12-03 15:46:03 +01002146static void tegra_sor_write_eld(struct tegra_sor *sor)
2147{
2148 size_t length = drm_eld_size(sor->output.connector.eld), i;
2149
2150 for (i = 0; i < length; i++)
2151 tegra_sor_writel(sor, i << 8 | sor->output.connector.eld[i],
2152 SOR_AUDIO_HDA_ELD_BUFWR);
2153
2154 /*
2155 * The HDA codec will always report an ELD buffer size of 96 bytes and
2156 * the HDA codec driver will check that each byte read from the buffer
2157 * is valid. Therefore every byte must be written, even if no 96 bytes
2158 * were parsed from EDID.
2159 */
2160 for (i = length; i < 96; i++)
2161 tegra_sor_writel(sor, i << 8 | 0, SOR_AUDIO_HDA_ELD_BUFWR);
2162}
2163
2164static void tegra_sor_audio_prepare(struct tegra_sor *sor)
2165{
2166 u32 value;
2167
2168 tegra_sor_write_eld(sor);
2169
2170 value = SOR_AUDIO_HDA_PRESENSE_ELDV | SOR_AUDIO_HDA_PRESENSE_PD;
2171 tegra_sor_writel(sor, value, SOR_AUDIO_HDA_PRESENSE);
2172}
2173
2174static void tegra_sor_audio_unprepare(struct tegra_sor *sor)
2175{
2176 tegra_sor_writel(sor, 0, SOR_AUDIO_HDA_PRESENSE);
2177}
2178
2179static int tegra_sor_hdmi_enable_audio_infoframe(struct tegra_sor *sor)
2180{
2181 u8 buffer[HDMI_INFOFRAME_SIZE(AUDIO)];
2182 struct hdmi_audio_infoframe frame;
2183 u32 value;
2184 int err;
2185
2186 err = hdmi_audio_infoframe_init(&frame);
2187 if (err < 0) {
2188 dev_err(sor->dev, "failed to setup audio infoframe: %d\n", err);
2189 return err;
2190 }
2191
2192 frame.channels = sor->audio.channels;
2193
2194 err = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
2195 if (err < 0) {
2196 dev_err(sor->dev, "failed to pack audio infoframe: %d\n", err);
2197 return err;
2198 }
2199
2200 tegra_sor_hdmi_write_infopack(sor, buffer, err);
2201
2202 value = tegra_sor_readl(sor, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
2203 value |= INFOFRAME_CTRL_CHECKSUM_ENABLE;
2204 value |= INFOFRAME_CTRL_ENABLE;
2205 tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
2206
2207 return 0;
2208}
2209
2210static void tegra_sor_hdmi_audio_enable(struct tegra_sor *sor)
2211{
2212 u32 value;
2213
2214 value = tegra_sor_readl(sor, SOR_AUDIO_CNTRL);
2215
2216 /* select HDA audio input */
2217 value &= ~SOR_AUDIO_CNTRL_SOURCE_SELECT(SOURCE_SELECT_MASK);
2218 value |= SOR_AUDIO_CNTRL_SOURCE_SELECT(SOURCE_SELECT_HDA);
2219
2220 /* inject null samples */
2221 if (sor->audio.channels != 2)
2222 value &= ~SOR_AUDIO_CNTRL_INJECT_NULLSMPL;
2223 else
2224 value |= SOR_AUDIO_CNTRL_INJECT_NULLSMPL;
2225
2226 value |= SOR_AUDIO_CNTRL_AFIFO_FLUSH;
2227
2228 tegra_sor_writel(sor, value, SOR_AUDIO_CNTRL);
2229
2230 /* enable advertising HBR capability */
2231 tegra_sor_writel(sor, SOR_AUDIO_SPARE_HBR_ENABLE, SOR_AUDIO_SPARE);
2232
2233 tegra_sor_writel(sor, 0, SOR_HDMI_ACR_CTRL);
2234
2235 value = SOR_HDMI_SPARE_ACR_PRIORITY_HIGH |
2236 SOR_HDMI_SPARE_CTS_RESET(1) |
2237 SOR_HDMI_SPARE_HW_CTS_ENABLE;
2238 tegra_sor_writel(sor, value, SOR_HDMI_SPARE);
2239
2240 /* enable HW CTS */
2241 value = SOR_HDMI_ACR_SUBPACK_LOW_SB1(0);
2242 tegra_sor_writel(sor, value, SOR_HDMI_ACR_0441_SUBPACK_LOW);
2243
2244 /* allow packet to be sent */
2245 value = SOR_HDMI_ACR_SUBPACK_HIGH_ENABLE;
2246 tegra_sor_writel(sor, value, SOR_HDMI_ACR_0441_SUBPACK_HIGH);
2247
2248 /* reset N counter and enable lookup */
2249 value = SOR_HDMI_AUDIO_N_RESET | SOR_HDMI_AUDIO_N_LOOKUP;
2250 tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_N);
2251
2252 value = (24000 * 4096) / (128 * sor->audio.sample_rate / 1000);
2253 tegra_sor_writel(sor, value, SOR_AUDIO_AVAL_0320);
2254 tegra_sor_writel(sor, 4096, SOR_AUDIO_NVAL_0320);
2255
2256 tegra_sor_writel(sor, 20000, SOR_AUDIO_AVAL_0441);
2257 tegra_sor_writel(sor, 4704, SOR_AUDIO_NVAL_0441);
2258
2259 tegra_sor_writel(sor, 20000, SOR_AUDIO_AVAL_0882);
2260 tegra_sor_writel(sor, 9408, SOR_AUDIO_NVAL_0882);
2261
2262 tegra_sor_writel(sor, 20000, SOR_AUDIO_AVAL_1764);
2263 tegra_sor_writel(sor, 18816, SOR_AUDIO_NVAL_1764);
2264
2265 value = (24000 * 6144) / (128 * sor->audio.sample_rate / 1000);
2266 tegra_sor_writel(sor, value, SOR_AUDIO_AVAL_0480);
2267 tegra_sor_writel(sor, 6144, SOR_AUDIO_NVAL_0480);
2268
2269 value = (24000 * 12288) / (128 * sor->audio.sample_rate / 1000);
2270 tegra_sor_writel(sor, value, SOR_AUDIO_AVAL_0960);
2271 tegra_sor_writel(sor, 12288, SOR_AUDIO_NVAL_0960);
2272
2273 value = (24000 * 24576) / (128 * sor->audio.sample_rate / 1000);
2274 tegra_sor_writel(sor, value, SOR_AUDIO_AVAL_1920);
2275 tegra_sor_writel(sor, 24576, SOR_AUDIO_NVAL_1920);
2276
2277 value = tegra_sor_readl(sor, SOR_HDMI_AUDIO_N);
2278 value &= ~SOR_HDMI_AUDIO_N_RESET;
2279 tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_N);
2280
2281 tegra_sor_hdmi_enable_audio_infoframe(sor);
2282}
2283
Thierry Reding459cc2c2015-07-30 10:34:24 +02002284static void tegra_sor_hdmi_disable_audio_infoframe(struct tegra_sor *sor)
2285{
2286 u32 value;
2287
2288 value = tegra_sor_readl(sor, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
2289 value &= ~INFOFRAME_CTRL_ENABLE;
2290 tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
2291}
2292
Thierry Reding8e2988a72018-12-03 15:46:03 +01002293static void tegra_sor_hdmi_audio_disable(struct tegra_sor *sor)
2294{
2295 tegra_sor_hdmi_disable_audio_infoframe(sor);
2296}
2297
Thierry Reding459cc2c2015-07-30 10:34:24 +02002298static struct tegra_sor_hdmi_settings *
2299tegra_sor_hdmi_find_settings(struct tegra_sor *sor, unsigned long frequency)
2300{
2301 unsigned int i;
2302
2303 for (i = 0; i < sor->num_settings; i++)
2304 if (frequency <= sor->settings[i].frequency)
2305 return &sor->settings[i];
2306
2307 return NULL;
2308}
2309
Thierry Reding36e90222017-10-12 19:14:21 +02002310static void tegra_sor_hdmi_disable_scrambling(struct tegra_sor *sor)
2311{
2312 u32 value;
2313
2314 value = tegra_sor_readl(sor, SOR_HDMI2_CTRL);
2315 value &= ~SOR_HDMI2_CTRL_CLOCK_MODE_DIV_BY_4;
2316 value &= ~SOR_HDMI2_CTRL_SCRAMBLE;
2317 tegra_sor_writel(sor, value, SOR_HDMI2_CTRL);
2318}
2319
2320static void tegra_sor_hdmi_scdc_disable(struct tegra_sor *sor)
2321{
2322 struct i2c_adapter *ddc = sor->output.ddc;
2323
2324 drm_scdc_set_high_tmds_clock_ratio(ddc, false);
2325 drm_scdc_set_scrambling(ddc, false);
2326
2327 tegra_sor_hdmi_disable_scrambling(sor);
2328}
2329
2330static void tegra_sor_hdmi_scdc_stop(struct tegra_sor *sor)
2331{
2332 if (sor->scdc_enabled) {
2333 cancel_delayed_work_sync(&sor->scdc);
2334 tegra_sor_hdmi_scdc_disable(sor);
2335 }
2336}
2337
2338static void tegra_sor_hdmi_enable_scrambling(struct tegra_sor *sor)
2339{
2340 u32 value;
2341
2342 value = tegra_sor_readl(sor, SOR_HDMI2_CTRL);
2343 value |= SOR_HDMI2_CTRL_CLOCK_MODE_DIV_BY_4;
2344 value |= SOR_HDMI2_CTRL_SCRAMBLE;
2345 tegra_sor_writel(sor, value, SOR_HDMI2_CTRL);
2346}
2347
2348static void tegra_sor_hdmi_scdc_enable(struct tegra_sor *sor)
2349{
2350 struct i2c_adapter *ddc = sor->output.ddc;
2351
2352 drm_scdc_set_high_tmds_clock_ratio(ddc, true);
2353 drm_scdc_set_scrambling(ddc, true);
2354
2355 tegra_sor_hdmi_enable_scrambling(sor);
2356}
2357
2358static void tegra_sor_hdmi_scdc_work(struct work_struct *work)
2359{
2360 struct tegra_sor *sor = container_of(work, struct tegra_sor, scdc.work);
2361 struct i2c_adapter *ddc = sor->output.ddc;
2362
2363 if (!drm_scdc_get_scrambling_status(ddc)) {
2364 DRM_DEBUG_KMS("SCDC not scrambled\n");
2365 tegra_sor_hdmi_scdc_enable(sor);
2366 }
2367
2368 schedule_delayed_work(&sor->scdc, msecs_to_jiffies(5000));
2369}
2370
2371static void tegra_sor_hdmi_scdc_start(struct tegra_sor *sor)
2372{
2373 struct drm_scdc *scdc = &sor->output.connector.display_info.hdmi.scdc;
2374 struct drm_display_mode *mode;
2375
2376 mode = &sor->output.encoder.crtc->state->adjusted_mode;
2377
2378 if (mode->clock >= 340000 && scdc->supported) {
2379 schedule_delayed_work(&sor->scdc, msecs_to_jiffies(5000));
2380 tegra_sor_hdmi_scdc_enable(sor);
2381 sor->scdc_enabled = true;
2382 }
2383}
2384
Thierry Reding459cc2c2015-07-30 10:34:24 +02002385static void tegra_sor_hdmi_disable(struct drm_encoder *encoder)
2386{
2387 struct tegra_output *output = encoder_to_output(encoder);
2388 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
2389 struct tegra_sor *sor = to_sor(output);
2390 u32 value;
2391 int err;
2392
Thierry Reding8e2988a72018-12-03 15:46:03 +01002393 tegra_sor_audio_unprepare(sor);
Thierry Reding36e90222017-10-12 19:14:21 +02002394 tegra_sor_hdmi_scdc_stop(sor);
2395
Thierry Reding459cc2c2015-07-30 10:34:24 +02002396 err = tegra_sor_detach(sor);
2397 if (err < 0)
2398 dev_err(sor->dev, "failed to detach SOR: %d\n", err);
2399
2400 tegra_sor_writel(sor, 0, SOR_STATE1);
2401 tegra_sor_update(sor);
2402
2403 /* disable display to SOR clock */
2404 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
Thierry Redingc57997b2017-10-12 19:12:57 +02002405
2406 if (!sor->soc->has_nvdisplay)
2407 value &= ~(SOR1_TIMING_CYA | SOR_ENABLE(1));
2408 else
2409 value &= ~SOR_ENABLE(sor->index);
2410
Thierry Reding459cc2c2015-07-30 10:34:24 +02002411 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
2412
2413 tegra_dc_commit(dc);
2414
2415 err = tegra_sor_power_down(sor);
2416 if (err < 0)
2417 dev_err(sor->dev, "failed to power down SOR: %d\n", err);
2418
Thierry Redingc57997b2017-10-12 19:12:57 +02002419 err = tegra_io_pad_power_disable(sor->pad);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002420 if (err < 0)
Thierry Redingc57997b2017-10-12 19:12:57 +02002421 dev_err(sor->dev, "failed to power off I/O pad: %d\n", err);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002422
Thierry Redingaaff8bd2015-08-07 16:04:54 +02002423 pm_runtime_put(sor->dev);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002424}
2425
2426static void tegra_sor_hdmi_enable(struct drm_encoder *encoder)
2427{
2428 struct tegra_output *output = encoder_to_output(encoder);
2429 unsigned int h_ref_to_sync = 1, pulse_start, max_ac;
2430 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002431 struct tegra_sor_hdmi_settings *settings;
2432 struct tegra_sor *sor = to_sor(output);
Thierry Redingc31efa72015-09-08 16:09:22 +02002433 struct tegra_sor_state *state;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002434 struct drm_display_mode *mode;
Thierry Reding36e90222017-10-12 19:14:21 +02002435 unsigned long rate, pclk;
Thierry Reding30b49432015-08-03 15:50:32 +02002436 unsigned int div, i;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002437 u32 value;
2438 int err;
2439
Thierry Redingc31efa72015-09-08 16:09:22 +02002440 state = to_sor_state(output->connector.state);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002441 mode = &encoder->crtc->state->adjusted_mode;
Thierry Reding36e90222017-10-12 19:14:21 +02002442 pclk = mode->clock * 1000;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002443
Thierry Redingaaff8bd2015-08-07 16:04:54 +02002444 pm_runtime_get_sync(sor->dev);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002445
Thierry Reding25bb2ce2015-08-03 14:23:29 +02002446 /* switch to safe parent clock */
2447 err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
Thierry Redinge1335e22017-10-12 17:53:11 +02002448 if (err < 0) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002449 dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
Thierry Redinge1335e22017-10-12 17:53:11 +02002450 return;
2451 }
Thierry Reding459cc2c2015-07-30 10:34:24 +02002452
2453 div = clk_get_rate(sor->clk) / 1000000 * 4;
2454
Thierry Redingc57997b2017-10-12 19:12:57 +02002455 err = tegra_io_pad_power_enable(sor->pad);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002456 if (err < 0)
Thierry Redingc57997b2017-10-12 19:12:57 +02002457 dev_err(sor->dev, "failed to power on I/O pad: %d\n", err);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002458
2459 usleep_range(20, 100);
2460
Thierry Reding880cee02017-10-12 19:04:17 +02002461 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002462 value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
Thierry Reding880cee02017-10-12 19:04:17 +02002463 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002464
2465 usleep_range(20, 100);
2466
Thierry Reding880cee02017-10-12 19:04:17 +02002467 value = tegra_sor_readl(sor, sor->soc->regs->pll3);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002468 value &= ~SOR_PLL3_PLL_VDD_MODE_3V3;
Thierry Reding880cee02017-10-12 19:04:17 +02002469 tegra_sor_writel(sor, value, sor->soc->regs->pll3);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002470
Thierry Reding880cee02017-10-12 19:04:17 +02002471 value = tegra_sor_readl(sor, sor->soc->regs->pll0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002472 value &= ~SOR_PLL0_VCOPD;
2473 value &= ~SOR_PLL0_PWR;
Thierry Reding880cee02017-10-12 19:04:17 +02002474 tegra_sor_writel(sor, value, sor->soc->regs->pll0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002475
Thierry Reding880cee02017-10-12 19:04:17 +02002476 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002477 value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
Thierry Reding880cee02017-10-12 19:04:17 +02002478 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002479
2480 usleep_range(200, 400);
2481
Thierry Reding880cee02017-10-12 19:04:17 +02002482 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002483 value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
2484 value &= ~SOR_PLL2_PORT_POWERDOWN;
Thierry Reding880cee02017-10-12 19:04:17 +02002485 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002486
2487 usleep_range(20, 100);
2488
Thierry Reding880cee02017-10-12 19:04:17 +02002489 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002490 value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
2491 SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2;
Thierry Reding880cee02017-10-12 19:04:17 +02002492 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002493
2494 while (true) {
2495 value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
2496 if ((value & SOR_LANE_SEQ_CTL_STATE_BUSY) == 0)
2497 break;
2498
2499 usleep_range(250, 1000);
2500 }
2501
2502 value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
2503 SOR_LANE_SEQ_CTL_POWER_STATE_UP | SOR_LANE_SEQ_CTL_DELAY(5);
2504 tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
2505
2506 while (true) {
2507 value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
2508 if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
2509 break;
2510
2511 usleep_range(250, 1000);
2512 }
2513
2514 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
2515 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
2516 value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
2517
Thierry Reding36e90222017-10-12 19:14:21 +02002518 if (mode->clock < 340000) {
2519 DRM_DEBUG_KMS("setting 2.7 GHz link speed\n");
Thierry Reding459cc2c2015-07-30 10:34:24 +02002520 value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G2_70;
Thierry Reding36e90222017-10-12 19:14:21 +02002521 } else {
2522 DRM_DEBUG_KMS("setting 5.4 GHz link speed\n");
Thierry Reding459cc2c2015-07-30 10:34:24 +02002523 value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G5_40;
Thierry Reding36e90222017-10-12 19:14:21 +02002524 }
Thierry Reding459cc2c2015-07-30 10:34:24 +02002525
2526 value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK;
2527 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
2528
Thierry Redingc57997b2017-10-12 19:12:57 +02002529 /* SOR pad PLL stabilization time */
2530 usleep_range(250, 1000);
2531
2532 value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
2533 value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
2534 value |= SOR_DP_LINKCTL_LANE_COUNT(4);
2535 tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
2536
Thierry Reding459cc2c2015-07-30 10:34:24 +02002537 value = tegra_sor_readl(sor, SOR_DP_SPARE0);
Thierry Redingc57997b2017-10-12 19:12:57 +02002538 value &= ~SOR_DP_SPARE_DISP_VIDEO_PREAMBLE;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002539 value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
Thierry Redingc57997b2017-10-12 19:12:57 +02002540 value &= ~SOR_DP_SPARE_SEQ_ENABLE;
2541 value &= ~SOR_DP_SPARE_MACRO_SOR_CLK;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002542 tegra_sor_writel(sor, value, SOR_DP_SPARE0);
2543
2544 value = SOR_SEQ_CTL_PU_PC(0) | SOR_SEQ_CTL_PU_PC_ALT(0) |
2545 SOR_SEQ_CTL_PD_PC(8) | SOR_SEQ_CTL_PD_PC_ALT(8);
2546 tegra_sor_writel(sor, value, SOR_SEQ_CTL);
2547
2548 value = SOR_SEQ_INST_DRIVE_PWM_OUT_LO | SOR_SEQ_INST_HALT |
2549 SOR_SEQ_INST_WAIT_VSYNC | SOR_SEQ_INST_WAIT(1);
2550 tegra_sor_writel(sor, value, SOR_SEQ_INST(0));
2551 tegra_sor_writel(sor, value, SOR_SEQ_INST(8));
2552
Thierry Redingc57997b2017-10-12 19:12:57 +02002553 if (!sor->soc->has_nvdisplay) {
2554 /* program the reference clock */
2555 value = SOR_REFCLK_DIV_INT(div) | SOR_REFCLK_DIV_FRAC(div);
2556 tegra_sor_writel(sor, value, SOR_REFCLK);
2557 }
Thierry Reding459cc2c2015-07-30 10:34:24 +02002558
Thierry Reding30b49432015-08-03 15:50:32 +02002559 /* XXX not in TRM */
2560 for (value = 0, i = 0; i < 5; i++)
2561 value |= SOR_XBAR_CTRL_LINK0_XSEL(i, sor->soc->xbar_cfg[i]) |
2562 SOR_XBAR_CTRL_LINK1_XSEL(i, i);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002563
2564 tegra_sor_writel(sor, 0x00000000, SOR_XBAR_POL);
Thierry Reding30b49432015-08-03 15:50:32 +02002565 tegra_sor_writel(sor, value, SOR_XBAR_CTRL);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002566
Thierry Reding25bb2ce2015-08-03 14:23:29 +02002567 /* switch to parent clock */
Thierry Redinge1335e22017-10-12 17:53:11 +02002568 err = clk_set_parent(sor->clk, sor->clk_parent);
2569 if (err < 0) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002570 dev_err(sor->dev, "failed to set parent clock: %d\n", err);
Thierry Redinge1335e22017-10-12 17:53:11 +02002571 return;
2572 }
2573
2574 err = tegra_sor_set_parent_clock(sor, sor->clk_pad);
2575 if (err < 0) {
2576 dev_err(sor->dev, "failed to set pad clock: %d\n", err);
2577 return;
2578 }
Thierry Reding459cc2c2015-07-30 10:34:24 +02002579
Thierry Reding36e90222017-10-12 19:14:21 +02002580 /* adjust clock rate for HDMI 2.0 modes */
2581 rate = clk_get_rate(sor->clk_parent);
2582
2583 if (mode->clock >= 340000)
2584 rate /= 2;
2585
2586 DRM_DEBUG_KMS("setting clock to %lu Hz, mode: %lu Hz\n", rate, pclk);
2587
2588 clk_set_rate(sor->clk, rate);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002589
Thierry Redingc57997b2017-10-12 19:12:57 +02002590 if (!sor->soc->has_nvdisplay) {
2591 value = SOR_INPUT_CONTROL_HDMI_SRC_SELECT(dc->pipe);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002592
Thierry Redingc57997b2017-10-12 19:12:57 +02002593 /* XXX is this the proper check? */
2594 if (mode->clock < 75000)
2595 value |= SOR_INPUT_CONTROL_ARM_VIDEO_RANGE_LIMITED;
2596
2597 tegra_sor_writel(sor, value, SOR_INPUT_CONTROL);
2598 }
Thierry Reding459cc2c2015-07-30 10:34:24 +02002599
2600 max_ac = ((mode->htotal - mode->hdisplay) - SOR_REKEY - 18) / 32;
2601
2602 value = SOR_HDMI_CTRL_ENABLE | SOR_HDMI_CTRL_MAX_AC_PACKET(max_ac) |
2603 SOR_HDMI_CTRL_AUDIO_LAYOUT | SOR_HDMI_CTRL_REKEY(SOR_REKEY);
2604 tegra_sor_writel(sor, value, SOR_HDMI_CTRL);
2605
Thierry Redingc57997b2017-10-12 19:12:57 +02002606 if (!dc->soc->has_nvdisplay) {
2607 /* H_PULSE2 setup */
2608 pulse_start = h_ref_to_sync +
2609 (mode->hsync_end - mode->hsync_start) +
2610 (mode->htotal - mode->hsync_end) - 10;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002611
Thierry Redingc57997b2017-10-12 19:12:57 +02002612 value = PULSE_LAST_END_A | PULSE_QUAL_VACTIVE |
2613 PULSE_POLARITY_HIGH | PULSE_MODE_NORMAL;
2614 tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_CONTROL);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002615
Thierry Redingc57997b2017-10-12 19:12:57 +02002616 value = PULSE_END(pulse_start + 8) | PULSE_START(pulse_start);
2617 tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_POSITION_A);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002618
Thierry Redingc57997b2017-10-12 19:12:57 +02002619 value = tegra_dc_readl(dc, DC_DISP_DISP_SIGNAL_OPTIONS0);
2620 value |= H_PULSE2_ENABLE;
2621 tegra_dc_writel(dc, value, DC_DISP_DISP_SIGNAL_OPTIONS0);
2622 }
Thierry Reding459cc2c2015-07-30 10:34:24 +02002623
2624 /* infoframe setup */
2625 err = tegra_sor_hdmi_setup_avi_infoframe(sor, mode);
2626 if (err < 0)
2627 dev_err(sor->dev, "failed to setup AVI infoframe: %d\n", err);
2628
2629 /* XXX HDMI audio support not implemented yet */
2630 tegra_sor_hdmi_disable_audio_infoframe(sor);
2631
2632 /* use single TMDS protocol */
2633 value = tegra_sor_readl(sor, SOR_STATE1);
2634 value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
2635 value |= SOR_STATE_ASY_PROTOCOL_SINGLE_TMDS_A;
2636 tegra_sor_writel(sor, value, SOR_STATE1);
2637
2638 /* power up pad calibration */
Thierry Reding880cee02017-10-12 19:04:17 +02002639 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002640 value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
Thierry Reding880cee02017-10-12 19:04:17 +02002641 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002642
2643 /* production settings */
2644 settings = tegra_sor_hdmi_find_settings(sor, mode->clock * 1000);
Dan Carpenterdb8b42f2015-08-17 17:37:03 +03002645 if (!settings) {
2646 dev_err(sor->dev, "no settings for pixel clock %d Hz\n",
2647 mode->clock * 1000);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002648 return;
2649 }
2650
Thierry Reding880cee02017-10-12 19:04:17 +02002651 value = tegra_sor_readl(sor, sor->soc->regs->pll0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002652 value &= ~SOR_PLL0_ICHPMP_MASK;
Thierry Redingc57997b2017-10-12 19:12:57 +02002653 value &= ~SOR_PLL0_FILTER_MASK;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002654 value &= ~SOR_PLL0_VCOCAP_MASK;
2655 value |= SOR_PLL0_ICHPMP(settings->ichpmp);
Thierry Redingc57997b2017-10-12 19:12:57 +02002656 value |= SOR_PLL0_FILTER(settings->filter);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002657 value |= SOR_PLL0_VCOCAP(settings->vcocap);
Thierry Reding880cee02017-10-12 19:04:17 +02002658 tegra_sor_writel(sor, value, sor->soc->regs->pll0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002659
Thierry Redingc57997b2017-10-12 19:12:57 +02002660 /* XXX not in TRM */
Thierry Reding880cee02017-10-12 19:04:17 +02002661 value = tegra_sor_readl(sor, sor->soc->regs->pll1);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002662 value &= ~SOR_PLL1_LOADADJ_MASK;
Thierry Redingc57997b2017-10-12 19:12:57 +02002663 value &= ~SOR_PLL1_TMDS_TERMADJ_MASK;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002664 value |= SOR_PLL1_LOADADJ(settings->loadadj);
Thierry Redingc57997b2017-10-12 19:12:57 +02002665 value |= SOR_PLL1_TMDS_TERMADJ(settings->tmds_termadj);
2666 value |= SOR_PLL1_TMDS_TERM;
Thierry Reding880cee02017-10-12 19:04:17 +02002667 tegra_sor_writel(sor, value, sor->soc->regs->pll1);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002668
Thierry Reding880cee02017-10-12 19:04:17 +02002669 value = tegra_sor_readl(sor, sor->soc->regs->pll3);
Thierry Redingc57997b2017-10-12 19:12:57 +02002670 value &= ~SOR_PLL3_BG_TEMP_COEF_MASK;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002671 value &= ~SOR_PLL3_BG_VREF_LEVEL_MASK;
Thierry Redingc57997b2017-10-12 19:12:57 +02002672 value &= ~SOR_PLL3_AVDD10_LEVEL_MASK;
2673 value &= ~SOR_PLL3_AVDD14_LEVEL_MASK;
2674 value |= SOR_PLL3_BG_TEMP_COEF(settings->bg_temp_coef);
2675 value |= SOR_PLL3_BG_VREF_LEVEL(settings->bg_vref_level);
2676 value |= SOR_PLL3_AVDD10_LEVEL(settings->avdd10_level);
2677 value |= SOR_PLL3_AVDD14_LEVEL(settings->avdd14_level);
Thierry Reding880cee02017-10-12 19:04:17 +02002678 tegra_sor_writel(sor, value, sor->soc->regs->pll3);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002679
Thierry Redingc57997b2017-10-12 19:12:57 +02002680 value = settings->drive_current[3] << 24 |
2681 settings->drive_current[2] << 16 |
2682 settings->drive_current[1] << 8 |
2683 settings->drive_current[0] << 0;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002684 tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT0);
2685
Thierry Redingc57997b2017-10-12 19:12:57 +02002686 value = settings->preemphasis[3] << 24 |
2687 settings->preemphasis[2] << 16 |
2688 settings->preemphasis[1] << 8 |
2689 settings->preemphasis[0] << 0;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002690 tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS0);
2691
Thierry Reding880cee02017-10-12 19:04:17 +02002692 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002693 value &= ~SOR_DP_PADCTL_TX_PU_MASK;
2694 value |= SOR_DP_PADCTL_TX_PU_ENABLE;
Thierry Redingc57997b2017-10-12 19:12:57 +02002695 value |= SOR_DP_PADCTL_TX_PU(settings->tx_pu_value);
Thierry Reding880cee02017-10-12 19:04:17 +02002696 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002697
Thierry Redingc57997b2017-10-12 19:12:57 +02002698 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl2);
2699 value &= ~SOR_DP_PADCTL_SPAREPLL_MASK;
2700 value |= SOR_DP_PADCTL_SPAREPLL(settings->sparepll);
2701 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl2);
2702
Thierry Reding459cc2c2015-07-30 10:34:24 +02002703 /* power down pad calibration */
Thierry Reding880cee02017-10-12 19:04:17 +02002704 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002705 value |= SOR_DP_PADCTL_PAD_CAL_PD;
Thierry Reding880cee02017-10-12 19:04:17 +02002706 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002707
Thierry Redingc57997b2017-10-12 19:12:57 +02002708 if (!dc->soc->has_nvdisplay) {
2709 /* miscellaneous display controller settings */
2710 value = VSYNC_H_POSITION(1);
2711 tegra_dc_writel(dc, value, DC_DISP_DISP_TIMING_OPTIONS);
2712 }
Thierry Reding459cc2c2015-07-30 10:34:24 +02002713
2714 value = tegra_dc_readl(dc, DC_DISP_DISP_COLOR_CONTROL);
2715 value &= ~DITHER_CONTROL_MASK;
2716 value &= ~BASE_COLOR_SIZE_MASK;
2717
Thierry Redingc31efa72015-09-08 16:09:22 +02002718 switch (state->bpc) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002719 case 6:
2720 value |= BASE_COLOR_SIZE_666;
2721 break;
2722
2723 case 8:
2724 value |= BASE_COLOR_SIZE_888;
2725 break;
2726
Thierry Redingc57997b2017-10-12 19:12:57 +02002727 case 10:
2728 value |= BASE_COLOR_SIZE_101010;
2729 break;
2730
2731 case 12:
2732 value |= BASE_COLOR_SIZE_121212;
2733 break;
2734
Thierry Reding459cc2c2015-07-30 10:34:24 +02002735 default:
Thierry Redingc31efa72015-09-08 16:09:22 +02002736 WARN(1, "%u bits-per-color not supported\n", state->bpc);
2737 value |= BASE_COLOR_SIZE_888;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002738 break;
2739 }
2740
2741 tegra_dc_writel(dc, value, DC_DISP_DISP_COLOR_CONTROL);
2742
Thierry Redingc57997b2017-10-12 19:12:57 +02002743 /* XXX set display head owner */
2744 value = tegra_sor_readl(sor, SOR_STATE1);
2745 value &= ~SOR_STATE_ASY_OWNER_MASK;
2746 value |= SOR_STATE_ASY_OWNER(1 + dc->pipe);
2747 tegra_sor_writel(sor, value, SOR_STATE1);
2748
Thierry Reding459cc2c2015-07-30 10:34:24 +02002749 err = tegra_sor_power_up(sor, 250);
2750 if (err < 0)
2751 dev_err(sor->dev, "failed to power up SOR: %d\n", err);
2752
Thierry Reding2bd1dd32015-08-03 15:46:15 +02002753 /* configure dynamic range of output */
Thierry Reding880cee02017-10-12 19:04:17 +02002754 value = tegra_sor_readl(sor, sor->soc->regs->head_state0 + dc->pipe);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002755 value &= ~SOR_HEAD_STATE_RANGECOMPRESS_MASK;
2756 value &= ~SOR_HEAD_STATE_DYNRANGE_MASK;
Thierry Reding880cee02017-10-12 19:04:17 +02002757 tegra_sor_writel(sor, value, sor->soc->regs->head_state0 + dc->pipe);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002758
Thierry Reding2bd1dd32015-08-03 15:46:15 +02002759 /* configure colorspace */
Thierry Reding880cee02017-10-12 19:04:17 +02002760 value = tegra_sor_readl(sor, sor->soc->regs->head_state0 + dc->pipe);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002761 value &= ~SOR_HEAD_STATE_COLORSPACE_MASK;
2762 value |= SOR_HEAD_STATE_COLORSPACE_RGB;
Thierry Reding880cee02017-10-12 19:04:17 +02002763 tegra_sor_writel(sor, value, sor->soc->regs->head_state0 + dc->pipe);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002764
Thierry Redingc31efa72015-09-08 16:09:22 +02002765 tegra_sor_mode_set(sor, mode, state);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002766
2767 tegra_sor_update(sor);
2768
Thierry Redingc57997b2017-10-12 19:12:57 +02002769 /* program preamble timing in SOR (XXX) */
2770 value = tegra_sor_readl(sor, SOR_DP_SPARE0);
2771 value &= ~SOR_DP_SPARE_DISP_VIDEO_PREAMBLE;
2772 tegra_sor_writel(sor, value, SOR_DP_SPARE0);
2773
Thierry Reding459cc2c2015-07-30 10:34:24 +02002774 err = tegra_sor_attach(sor);
2775 if (err < 0)
2776 dev_err(sor->dev, "failed to attach SOR: %d\n", err);
2777
2778 /* enable display to SOR clock and generate HDMI preamble */
2779 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
Thierry Redingc57997b2017-10-12 19:12:57 +02002780
2781 if (!sor->soc->has_nvdisplay)
2782 value |= SOR_ENABLE(1) | SOR1_TIMING_CYA;
2783 else
2784 value |= SOR_ENABLE(sor->index);
2785
Thierry Reding459cc2c2015-07-30 10:34:24 +02002786 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
2787
Thierry Redingc57997b2017-10-12 19:12:57 +02002788 if (dc->soc->has_nvdisplay) {
2789 value = tegra_dc_readl(dc, DC_DISP_CORE_SOR_SET_CONTROL(sor->index));
2790 value &= ~PROTOCOL_MASK;
2791 value |= PROTOCOL_SINGLE_TMDS_A;
2792 tegra_dc_writel(dc, value, DC_DISP_CORE_SOR_SET_CONTROL(sor->index));
2793 }
2794
Thierry Reding459cc2c2015-07-30 10:34:24 +02002795 tegra_dc_commit(dc);
2796
2797 err = tegra_sor_wakeup(sor);
2798 if (err < 0)
2799 dev_err(sor->dev, "failed to wakeup SOR: %d\n", err);
Thierry Reding36e90222017-10-12 19:14:21 +02002800
2801 tegra_sor_hdmi_scdc_start(sor);
Thierry Reding8e2988a72018-12-03 15:46:03 +01002802 tegra_sor_audio_prepare(sor);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002803}
2804
2805static const struct drm_encoder_helper_funcs tegra_sor_hdmi_helpers = {
2806 .disable = tegra_sor_hdmi_disable,
2807 .enable = tegra_sor_hdmi_enable,
2808 .atomic_check = tegra_sor_encoder_atomic_check,
2809};
2810
Thierry Reding6b6b6042013-11-15 16:06:05 +01002811static int tegra_sor_init(struct host1x_client *client)
2812{
Thierry Reding9910f5c2014-05-22 09:57:15 +02002813 struct drm_device *drm = dev_get_drvdata(client->parent);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002814 const struct drm_encoder_helper_funcs *helpers = NULL;
Thierry Reding6b6b6042013-11-15 16:06:05 +01002815 struct tegra_sor *sor = host1x_client_to_sor(client);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002816 int connector = DRM_MODE_CONNECTOR_Unknown;
2817 int encoder = DRM_MODE_ENCODER_NONE;
Thierry Reding8e2988a72018-12-03 15:46:03 +01002818 u32 value;
Thierry Reding6b6b6042013-11-15 16:06:05 +01002819 int err;
2820
Thierry Reding9542c232015-07-08 13:39:09 +02002821 if (!sor->aux) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002822 if (sor->soc->supports_hdmi) {
2823 connector = DRM_MODE_CONNECTOR_HDMIA;
2824 encoder = DRM_MODE_ENCODER_TMDS;
2825 helpers = &tegra_sor_hdmi_helpers;
2826 } else if (sor->soc->supports_lvds) {
2827 connector = DRM_MODE_CONNECTOR_LVDS;
2828 encoder = DRM_MODE_ENCODER_LVDS;
2829 }
2830 } else {
2831 if (sor->soc->supports_edp) {
2832 connector = DRM_MODE_CONNECTOR_eDP;
2833 encoder = DRM_MODE_ENCODER_TMDS;
2834 helpers = &tegra_sor_edp_helpers;
2835 } else if (sor->soc->supports_dp) {
2836 connector = DRM_MODE_CONNECTOR_DisplayPort;
2837 encoder = DRM_MODE_ENCODER_TMDS;
2838 }
2839 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01002840
Thierry Reding6b6b6042013-11-15 16:06:05 +01002841 sor->output.dev = sor->dev;
Thierry Reding6b6b6042013-11-15 16:06:05 +01002842
Thierry Reding6fad8f62014-11-28 15:41:34 +01002843 drm_connector_init(drm, &sor->output.connector,
2844 &tegra_sor_connector_funcs,
Thierry Reding459cc2c2015-07-30 10:34:24 +02002845 connector);
Thierry Reding6fad8f62014-11-28 15:41:34 +01002846 drm_connector_helper_add(&sor->output.connector,
2847 &tegra_sor_connector_helper_funcs);
2848 sor->output.connector.dpms = DRM_MODE_DPMS_OFF;
2849
Thierry Reding6fad8f62014-11-28 15:41:34 +01002850 drm_encoder_init(drm, &sor->output.encoder, &tegra_sor_encoder_funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +02002851 encoder, NULL);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002852 drm_encoder_helper_add(&sor->output.encoder, helpers);
Thierry Reding6fad8f62014-11-28 15:41:34 +01002853
Daniel Vettercde4c442018-07-09 10:40:07 +02002854 drm_connector_attach_encoder(&sor->output.connector,
Thierry Reding6fad8f62014-11-28 15:41:34 +01002855 &sor->output.encoder);
2856 drm_connector_register(&sor->output.connector);
2857
Thierry Redingea130b22014-12-19 15:51:35 +01002858 err = tegra_output_init(drm, &sor->output);
2859 if (err < 0) {
2860 dev_err(client->dev, "failed to initialize output: %d\n", err);
2861 return err;
2862 }
Thierry Reding6fad8f62014-11-28 15:41:34 +01002863
Thierry Redingc57997b2017-10-12 19:12:57 +02002864 tegra_output_find_possible_crtcs(&sor->output, drm);
Thierry Reding6b6b6042013-11-15 16:06:05 +01002865
Thierry Reding9542c232015-07-08 13:39:09 +02002866 if (sor->aux) {
2867 err = drm_dp_aux_attach(sor->aux, &sor->output);
Thierry Reding6b6b6042013-11-15 16:06:05 +01002868 if (err < 0) {
2869 dev_err(sor->dev, "failed to attach DP: %d\n", err);
2870 return err;
2871 }
2872 }
2873
Tomeu Vizoso535a65d2015-03-30 10:33:03 +02002874 /*
2875 * XXX: Remove this reset once proper hand-over from firmware to
2876 * kernel is possible.
2877 */
Jon Hunterf8c79122016-07-01 14:21:38 +01002878 if (sor->rst) {
2879 err = reset_control_assert(sor->rst);
2880 if (err < 0) {
2881 dev_err(sor->dev, "failed to assert SOR reset: %d\n",
2882 err);
2883 return err;
2884 }
Tomeu Vizoso535a65d2015-03-30 10:33:03 +02002885 }
2886
Thierry Reding6fad8f62014-11-28 15:41:34 +01002887 err = clk_prepare_enable(sor->clk);
2888 if (err < 0) {
2889 dev_err(sor->dev, "failed to enable clock: %d\n", err);
2890 return err;
2891 }
2892
Tomeu Vizoso535a65d2015-03-30 10:33:03 +02002893 usleep_range(1000, 3000);
2894
Jon Hunterf8c79122016-07-01 14:21:38 +01002895 if (sor->rst) {
2896 err = reset_control_deassert(sor->rst);
2897 if (err < 0) {
2898 dev_err(sor->dev, "failed to deassert SOR reset: %d\n",
2899 err);
2900 return err;
2901 }
Tomeu Vizoso535a65d2015-03-30 10:33:03 +02002902 }
2903
Thierry Reding6fad8f62014-11-28 15:41:34 +01002904 err = clk_prepare_enable(sor->clk_safe);
2905 if (err < 0)
2906 return err;
2907
2908 err = clk_prepare_enable(sor->clk_dp);
2909 if (err < 0)
2910 return err;
2911
Thierry Reding8e2988a72018-12-03 15:46:03 +01002912 /*
2913 * Enable and unmask the HDA codec SCRATCH0 register interrupt. This
2914 * is used for interoperability between the HDA codec driver and the
2915 * HDMI/DP driver.
2916 */
2917 value = SOR_INT_CODEC_SCRATCH1 | SOR_INT_CODEC_SCRATCH0;
2918 tegra_sor_writel(sor, value, SOR_INT_ENABLE);
2919 tegra_sor_writel(sor, value, SOR_INT_MASK);
2920
Thierry Reding6b6b6042013-11-15 16:06:05 +01002921 return 0;
2922}
2923
2924static int tegra_sor_exit(struct host1x_client *client)
2925{
2926 struct tegra_sor *sor = host1x_client_to_sor(client);
2927 int err;
2928
Thierry Reding8e2988a72018-12-03 15:46:03 +01002929 tegra_sor_writel(sor, 0, SOR_INT_MASK);
2930 tegra_sor_writel(sor, 0, SOR_INT_ENABLE);
2931
Thierry Reding328ec692014-12-19 15:55:08 +01002932 tegra_output_exit(&sor->output);
2933
Thierry Reding9542c232015-07-08 13:39:09 +02002934 if (sor->aux) {
2935 err = drm_dp_aux_detach(sor->aux);
Thierry Reding6b6b6042013-11-15 16:06:05 +01002936 if (err < 0) {
2937 dev_err(sor->dev, "failed to detach DP: %d\n", err);
2938 return err;
2939 }
2940 }
2941
Thierry Reding6fad8f62014-11-28 15:41:34 +01002942 clk_disable_unprepare(sor->clk_safe);
2943 clk_disable_unprepare(sor->clk_dp);
2944 clk_disable_unprepare(sor->clk);
2945
Thierry Reding6b6b6042013-11-15 16:06:05 +01002946 return 0;
2947}
2948
2949static const struct host1x_client_ops sor_client_ops = {
2950 .init = tegra_sor_init,
2951 .exit = tegra_sor_exit,
2952};
2953
Thierry Reding459cc2c2015-07-30 10:34:24 +02002954static const struct tegra_sor_ops tegra_sor_edp_ops = {
2955 .name = "eDP",
2956};
2957
2958static int tegra_sor_hdmi_probe(struct tegra_sor *sor)
2959{
2960 int err;
2961
2962 sor->avdd_io_supply = devm_regulator_get(sor->dev, "avdd-io");
2963 if (IS_ERR(sor->avdd_io_supply)) {
2964 dev_err(sor->dev, "cannot get AVDD I/O supply: %ld\n",
2965 PTR_ERR(sor->avdd_io_supply));
2966 return PTR_ERR(sor->avdd_io_supply);
2967 }
2968
2969 err = regulator_enable(sor->avdd_io_supply);
2970 if (err < 0) {
2971 dev_err(sor->dev, "failed to enable AVDD I/O supply: %d\n",
2972 err);
2973 return err;
2974 }
2975
2976 sor->vdd_pll_supply = devm_regulator_get(sor->dev, "vdd-pll");
2977 if (IS_ERR(sor->vdd_pll_supply)) {
2978 dev_err(sor->dev, "cannot get VDD PLL supply: %ld\n",
2979 PTR_ERR(sor->vdd_pll_supply));
2980 return PTR_ERR(sor->vdd_pll_supply);
2981 }
2982
2983 err = regulator_enable(sor->vdd_pll_supply);
2984 if (err < 0) {
2985 dev_err(sor->dev, "failed to enable VDD PLL supply: %d\n",
2986 err);
2987 return err;
2988 }
2989
2990 sor->hdmi_supply = devm_regulator_get(sor->dev, "hdmi");
2991 if (IS_ERR(sor->hdmi_supply)) {
2992 dev_err(sor->dev, "cannot get HDMI supply: %ld\n",
2993 PTR_ERR(sor->hdmi_supply));
2994 return PTR_ERR(sor->hdmi_supply);
2995 }
2996
2997 err = regulator_enable(sor->hdmi_supply);
2998 if (err < 0) {
2999 dev_err(sor->dev, "failed to enable HDMI supply: %d\n", err);
3000 return err;
3001 }
3002
Thierry Reding36e90222017-10-12 19:14:21 +02003003 INIT_DELAYED_WORK(&sor->scdc, tegra_sor_hdmi_scdc_work);
3004
Thierry Reding459cc2c2015-07-30 10:34:24 +02003005 return 0;
3006}
3007
3008static int tegra_sor_hdmi_remove(struct tegra_sor *sor)
3009{
3010 regulator_disable(sor->hdmi_supply);
3011 regulator_disable(sor->vdd_pll_supply);
3012 regulator_disable(sor->avdd_io_supply);
3013
3014 return 0;
3015}
3016
3017static const struct tegra_sor_ops tegra_sor_hdmi_ops = {
3018 .name = "HDMI",
3019 .probe = tegra_sor_hdmi_probe,
3020 .remove = tegra_sor_hdmi_remove,
3021};
3022
Thierry Reding30b49432015-08-03 15:50:32 +02003023static const u8 tegra124_sor_xbar_cfg[5] = {
3024 0, 1, 2, 3, 4
3025};
3026
Thierry Reding880cee02017-10-12 19:04:17 +02003027static const struct tegra_sor_regs tegra124_sor_regs = {
3028 .head_state0 = 0x05,
3029 .head_state1 = 0x07,
3030 .head_state2 = 0x09,
3031 .head_state3 = 0x0b,
3032 .head_state4 = 0x0d,
3033 .head_state5 = 0x0f,
3034 .pll0 = 0x17,
3035 .pll1 = 0x18,
3036 .pll2 = 0x19,
3037 .pll3 = 0x1a,
3038 .dp_padctl0 = 0x5c,
3039 .dp_padctl2 = 0x73,
3040};
3041
Thierry Reding459cc2c2015-07-30 10:34:24 +02003042static const struct tegra_sor_soc tegra124_sor = {
3043 .supports_edp = true,
3044 .supports_lvds = true,
3045 .supports_hdmi = false,
3046 .supports_dp = false,
Thierry Reding880cee02017-10-12 19:04:17 +02003047 .regs = &tegra124_sor_regs,
Thierry Redingc57997b2017-10-12 19:12:57 +02003048 .has_nvdisplay = false,
Thierry Reding30b49432015-08-03 15:50:32 +02003049 .xbar_cfg = tegra124_sor_xbar_cfg,
Thierry Reding459cc2c2015-07-30 10:34:24 +02003050};
3051
Thierry Reding880cee02017-10-12 19:04:17 +02003052static const struct tegra_sor_regs tegra210_sor_regs = {
3053 .head_state0 = 0x05,
3054 .head_state1 = 0x07,
3055 .head_state2 = 0x09,
3056 .head_state3 = 0x0b,
3057 .head_state4 = 0x0d,
3058 .head_state5 = 0x0f,
3059 .pll0 = 0x17,
3060 .pll1 = 0x18,
3061 .pll2 = 0x19,
3062 .pll3 = 0x1a,
3063 .dp_padctl0 = 0x5c,
3064 .dp_padctl2 = 0x73,
3065};
3066
Thierry Reding459cc2c2015-07-30 10:34:24 +02003067static const struct tegra_sor_soc tegra210_sor = {
3068 .supports_edp = true,
3069 .supports_lvds = false,
3070 .supports_hdmi = false,
3071 .supports_dp = false,
Thierry Reding880cee02017-10-12 19:04:17 +02003072 .regs = &tegra210_sor_regs,
Thierry Redingc57997b2017-10-12 19:12:57 +02003073 .has_nvdisplay = false,
Thierry Reding30b49432015-08-03 15:50:32 +02003074 .xbar_cfg = tegra124_sor_xbar_cfg,
3075};
3076
3077static const u8 tegra210_sor_xbar_cfg[5] = {
3078 2, 1, 0, 3, 4
Thierry Reding459cc2c2015-07-30 10:34:24 +02003079};
3080
3081static const struct tegra_sor_soc tegra210_sor1 = {
3082 .supports_edp = false,
3083 .supports_lvds = false,
3084 .supports_hdmi = true,
3085 .supports_dp = true,
3086
Thierry Reding880cee02017-10-12 19:04:17 +02003087 .regs = &tegra210_sor_regs,
Thierry Redingc57997b2017-10-12 19:12:57 +02003088 .has_nvdisplay = false,
Thierry Reding880cee02017-10-12 19:04:17 +02003089
Thierry Reding459cc2c2015-07-30 10:34:24 +02003090 .num_settings = ARRAY_SIZE(tegra210_sor_hdmi_defaults),
3091 .settings = tegra210_sor_hdmi_defaults,
Thierry Reding30b49432015-08-03 15:50:32 +02003092
3093 .xbar_cfg = tegra210_sor_xbar_cfg,
Thierry Reding459cc2c2015-07-30 10:34:24 +02003094};
3095
Thierry Redingc57997b2017-10-12 19:12:57 +02003096static const struct tegra_sor_regs tegra186_sor_regs = {
3097 .head_state0 = 0x151,
3098 .head_state1 = 0x154,
3099 .head_state2 = 0x157,
3100 .head_state3 = 0x15a,
3101 .head_state4 = 0x15d,
3102 .head_state5 = 0x160,
3103 .pll0 = 0x163,
3104 .pll1 = 0x164,
3105 .pll2 = 0x165,
3106 .pll3 = 0x166,
3107 .dp_padctl0 = 0x168,
3108 .dp_padctl2 = 0x16a,
3109};
3110
3111static const struct tegra_sor_soc tegra186_sor = {
3112 .supports_edp = false,
3113 .supports_lvds = false,
3114 .supports_hdmi = false,
3115 .supports_dp = true,
3116
3117 .regs = &tegra186_sor_regs,
3118 .has_nvdisplay = true,
3119
3120 .xbar_cfg = tegra124_sor_xbar_cfg,
3121};
3122
3123static const struct tegra_sor_soc tegra186_sor1 = {
3124 .supports_edp = false,
3125 .supports_lvds = false,
3126 .supports_hdmi = true,
3127 .supports_dp = true,
3128
3129 .regs = &tegra186_sor_regs,
3130 .has_nvdisplay = true,
3131
3132 .num_settings = ARRAY_SIZE(tegra186_sor_hdmi_defaults),
3133 .settings = tegra186_sor_hdmi_defaults,
3134
3135 .xbar_cfg = tegra124_sor_xbar_cfg,
3136};
3137
Thierry Reding9b6c14b2018-09-21 12:27:46 +02003138static const struct tegra_sor_regs tegra194_sor_regs = {
3139 .head_state0 = 0x151,
3140 .head_state1 = 0x155,
3141 .head_state2 = 0x159,
3142 .head_state3 = 0x15d,
3143 .head_state4 = 0x161,
3144 .head_state5 = 0x165,
3145 .pll0 = 0x169,
3146 .pll1 = 0x16a,
3147 .pll2 = 0x16b,
3148 .pll3 = 0x16c,
3149 .dp_padctl0 = 0x16e,
3150 .dp_padctl2 = 0x16f,
3151};
3152
3153static const struct tegra_sor_soc tegra194_sor = {
3154 .supports_edp = true,
3155 .supports_lvds = false,
3156 .supports_hdmi = true,
3157 .supports_dp = true,
3158
3159 .regs = &tegra194_sor_regs,
3160 .has_nvdisplay = true,
3161
3162 .num_settings = ARRAY_SIZE(tegra194_sor_hdmi_defaults),
3163 .settings = tegra194_sor_hdmi_defaults,
3164
3165 .xbar_cfg = tegra210_sor_xbar_cfg,
3166};
3167
Thierry Reding459cc2c2015-07-30 10:34:24 +02003168static const struct of_device_id tegra_sor_of_match[] = {
Thierry Reding9b6c14b2018-09-21 12:27:46 +02003169 { .compatible = "nvidia,tegra194-sor", .data = &tegra194_sor },
Thierry Redingc57997b2017-10-12 19:12:57 +02003170 { .compatible = "nvidia,tegra186-sor1", .data = &tegra186_sor1 },
3171 { .compatible = "nvidia,tegra186-sor", .data = &tegra186_sor },
Thierry Reding459cc2c2015-07-30 10:34:24 +02003172 { .compatible = "nvidia,tegra210-sor1", .data = &tegra210_sor1 },
3173 { .compatible = "nvidia,tegra210-sor", .data = &tegra210_sor },
3174 { .compatible = "nvidia,tegra124-sor", .data = &tegra124_sor },
3175 { },
3176};
3177MODULE_DEVICE_TABLE(of, tegra_sor_of_match);
3178
Thierry Redingc57997b2017-10-12 19:12:57 +02003179static int tegra_sor_parse_dt(struct tegra_sor *sor)
3180{
3181 struct device_node *np = sor->dev->of_node;
3182 u32 value;
3183 int err;
3184
3185 if (sor->soc->has_nvdisplay) {
3186 err = of_property_read_u32(np, "nvidia,interface", &value);
3187 if (err < 0)
3188 return err;
3189
3190 sor->index = value;
3191
3192 /*
3193 * override the default that we already set for Tegra210 and
3194 * earlier
3195 */
3196 sor->pad = TEGRA_IO_PAD_HDMI_DP0 + sor->index;
3197 }
3198
3199 return 0;
3200}
3201
Thierry Redingcd54fb92019-01-03 15:23:15 +01003202static void tegra_hda_parse_format(unsigned int format,
3203 struct tegra_sor_audio *audio)
Thierry Reding8e2988a72018-12-03 15:46:03 +01003204{
Thierry Redingcd54fb92019-01-03 15:23:15 +01003205 unsigned int mul, div, bits, channels;
3206
3207 if (format & AC_FMT_TYPE_NON_PCM)
3208 audio->pcm = false;
3209 else
3210 audio->pcm = true;
Thierry Reding8e2988a72018-12-03 15:46:03 +01003211
3212 if (format & AC_FMT_BASE_44K)
Thierry Redingcd54fb92019-01-03 15:23:15 +01003213 audio->sample_rate = 44100;
Thierry Reding8e2988a72018-12-03 15:46:03 +01003214 else
Thierry Redingcd54fb92019-01-03 15:23:15 +01003215 audio->sample_rate = 48000;
Thierry Reding8e2988a72018-12-03 15:46:03 +01003216
3217 mul = (format & AC_FMT_MULT_MASK) >> AC_FMT_MULT_SHIFT;
3218 div = (format & AC_FMT_DIV_MASK) >> AC_FMT_DIV_SHIFT;
3219
Thierry Redingcd54fb92019-01-03 15:23:15 +01003220 audio->sample_rate = audio->sample_rate * (mul + 1) / (div + 1);
Thierry Reding8e2988a72018-12-03 15:46:03 +01003221
Thierry Redingcd54fb92019-01-03 15:23:15 +01003222 switch (format & AC_FMT_BITS_MASK) {
3223 case AC_FMT_BITS_8:
3224 audio->bits = 8;
3225 break;
3226
3227 case AC_FMT_BITS_16:
3228 audio->bits = 16;
3229 break;
3230
3231 case AC_FMT_BITS_20:
3232 audio->bits = 20;
3233 break;
3234
3235 case AC_FMT_BITS_24:
3236 audio->bits = 24;
3237 break;
3238
3239 case AC_FMT_BITS_32:
3240 audio->bits = 32;
3241 break;
3242
3243 default:
3244 bits = (format & AC_FMT_BITS_MASK) >> AC_FMT_BITS_SHIFT;
3245 WARN(1, "invalid number of bits: %#x\n", bits);
3246 audio->bits = 8;
3247 break;
3248 }
3249
3250 channels = (format & AC_FMT_CHAN_MASK) >> AC_FMT_CHAN_SHIFT;
3251
3252 /* channels are encoded as n - 1 */
3253 audio->channels = channels + 1;
Thierry Reding8e2988a72018-12-03 15:46:03 +01003254}
3255
3256static irqreturn_t tegra_sor_irq(int irq, void *data)
3257{
3258 struct tegra_sor *sor = data;
3259 u32 value;
3260
3261 value = tegra_sor_readl(sor, SOR_INT_STATUS);
3262 tegra_sor_writel(sor, value, SOR_INT_STATUS);
3263
3264 if (value & SOR_INT_CODEC_SCRATCH0) {
3265 value = tegra_sor_readl(sor, SOR_AUDIO_HDA_CODEC_SCRATCH0);
3266
3267 if (value & SOR_AUDIO_HDA_CODEC_SCRATCH0_VALID) {
Thierry Redingcd54fb92019-01-03 15:23:15 +01003268 unsigned int format;
Thierry Reding8e2988a72018-12-03 15:46:03 +01003269
3270 format = value & SOR_AUDIO_HDA_CODEC_SCRATCH0_FMT_MASK;
3271
Thierry Redingcd54fb92019-01-03 15:23:15 +01003272 tegra_hda_parse_format(format, &sor->audio);
Thierry Reding8e2988a72018-12-03 15:46:03 +01003273
3274 tegra_sor_hdmi_audio_enable(sor);
3275 } else {
3276 tegra_sor_hdmi_audio_disable(sor);
3277 }
3278 }
3279
3280 return IRQ_HANDLED;
3281}
3282
Thierry Reding6b6b6042013-11-15 16:06:05 +01003283static int tegra_sor_probe(struct platform_device *pdev)
3284{
3285 struct device_node *np;
3286 struct tegra_sor *sor;
3287 struct resource *regs;
3288 int err;
3289
3290 sor = devm_kzalloc(&pdev->dev, sizeof(*sor), GFP_KERNEL);
3291 if (!sor)
3292 return -ENOMEM;
3293
Thierry Reding5faea3d2017-08-21 17:33:14 +02003294 sor->soc = of_device_get_match_data(&pdev->dev);
Thierry Reding6b6b6042013-11-15 16:06:05 +01003295 sor->output.dev = sor->dev = &pdev->dev;
Thierry Reding459cc2c2015-07-30 10:34:24 +02003296
3297 sor->settings = devm_kmemdup(&pdev->dev, sor->soc->settings,
3298 sor->soc->num_settings *
3299 sizeof(*sor->settings),
3300 GFP_KERNEL);
3301 if (!sor->settings)
3302 return -ENOMEM;
3303
3304 sor->num_settings = sor->soc->num_settings;
Thierry Reding6b6b6042013-11-15 16:06:05 +01003305
3306 np = of_parse_phandle(pdev->dev.of_node, "nvidia,dpaux", 0);
3307 if (np) {
Thierry Reding9542c232015-07-08 13:39:09 +02003308 sor->aux = drm_dp_aux_find_by_of_node(np);
Thierry Reding6b6b6042013-11-15 16:06:05 +01003309 of_node_put(np);
3310
Thierry Reding9542c232015-07-08 13:39:09 +02003311 if (!sor->aux)
Thierry Reding6b6b6042013-11-15 16:06:05 +01003312 return -EPROBE_DEFER;
3313 }
3314
Thierry Reding9542c232015-07-08 13:39:09 +02003315 if (!sor->aux) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02003316 if (sor->soc->supports_hdmi) {
3317 sor->ops = &tegra_sor_hdmi_ops;
Thierry Redingc57997b2017-10-12 19:12:57 +02003318 sor->pad = TEGRA_IO_PAD_HDMI;
Thierry Reding459cc2c2015-07-30 10:34:24 +02003319 } else if (sor->soc->supports_lvds) {
3320 dev_err(&pdev->dev, "LVDS not supported yet\n");
3321 return -ENODEV;
3322 } else {
3323 dev_err(&pdev->dev, "unknown (non-DP) support\n");
3324 return -ENODEV;
3325 }
3326 } else {
3327 if (sor->soc->supports_edp) {
3328 sor->ops = &tegra_sor_edp_ops;
Thierry Redingc57997b2017-10-12 19:12:57 +02003329 sor->pad = TEGRA_IO_PAD_LVDS;
Thierry Reding459cc2c2015-07-30 10:34:24 +02003330 } else if (sor->soc->supports_dp) {
3331 dev_err(&pdev->dev, "DisplayPort not supported yet\n");
3332 return -ENODEV;
3333 } else {
3334 dev_err(&pdev->dev, "unknown (DP) support\n");
3335 return -ENODEV;
3336 }
3337 }
3338
Thierry Redingc57997b2017-10-12 19:12:57 +02003339 err = tegra_sor_parse_dt(sor);
3340 if (err < 0)
3341 return err;
3342
Thierry Reding6b6b6042013-11-15 16:06:05 +01003343 err = tegra_output_probe(&sor->output);
Thierry Reding4dbdc742015-04-27 15:04:26 +02003344 if (err < 0) {
3345 dev_err(&pdev->dev, "failed to probe output: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01003346 return err;
Thierry Reding4dbdc742015-04-27 15:04:26 +02003347 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01003348
Thierry Reding459cc2c2015-07-30 10:34:24 +02003349 if (sor->ops && sor->ops->probe) {
3350 err = sor->ops->probe(sor);
3351 if (err < 0) {
3352 dev_err(&pdev->dev, "failed to probe %s: %d\n",
3353 sor->ops->name, err);
3354 goto output;
3355 }
3356 }
3357
Thierry Reding6b6b6042013-11-15 16:06:05 +01003358 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3359 sor->regs = devm_ioremap_resource(&pdev->dev, regs);
Thierry Reding459cc2c2015-07-30 10:34:24 +02003360 if (IS_ERR(sor->regs)) {
3361 err = PTR_ERR(sor->regs);
3362 goto remove;
3363 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01003364
Thierry Reding8e2988a72018-12-03 15:46:03 +01003365 err = platform_get_irq(pdev, 0);
3366 if (err < 0) {
3367 dev_err(&pdev->dev, "failed to get IRQ: %d\n", err);
3368 goto remove;
3369 }
3370
3371 sor->irq = err;
3372
3373 err = devm_request_irq(sor->dev, sor->irq, tegra_sor_irq, 0,
3374 dev_name(sor->dev), sor);
3375 if (err < 0) {
3376 dev_err(&pdev->dev, "failed to request IRQ: %d\n", err);
3377 goto remove;
3378 }
3379
Thierry Reding180b46e2018-12-06 18:56:47 +01003380 sor->rst = devm_reset_control_get(&pdev->dev, "sor");
3381 if (IS_ERR(sor->rst)) {
3382 err = PTR_ERR(sor->rst);
3383
3384 if (err != -EBUSY || WARN_ON(!pdev->dev.pm_domain)) {
Jon Hunterf8c79122016-07-01 14:21:38 +01003385 dev_err(&pdev->dev, "failed to get reset control: %d\n",
3386 err);
3387 goto remove;
3388 }
Thierry Reding180b46e2018-12-06 18:56:47 +01003389
3390 /*
3391 * At this point, the reset control is most likely being used
3392 * by the generic power domain implementation. With any luck
3393 * the power domain will have taken care of resetting the SOR
3394 * and we don't have to do anything.
3395 */
3396 sor->rst = NULL;
Thierry Reding4dbdc742015-04-27 15:04:26 +02003397 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01003398
3399 sor->clk = devm_clk_get(&pdev->dev, NULL);
Thierry Reding4dbdc742015-04-27 15:04:26 +02003400 if (IS_ERR(sor->clk)) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02003401 err = PTR_ERR(sor->clk);
3402 dev_err(&pdev->dev, "failed to get module clock: %d\n", err);
3403 goto remove;
Thierry Reding4dbdc742015-04-27 15:04:26 +02003404 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01003405
Thierry Reding618dee32016-06-09 17:53:57 +02003406 if (sor->soc->supports_hdmi || sor->soc->supports_dp) {
Thierry Redinge1335e22017-10-12 17:53:11 +02003407 struct device_node *np = pdev->dev.of_node;
3408 const char *name;
3409
3410 /*
3411 * For backwards compatibility with Tegra210 device trees,
3412 * fall back to the old clock name "source" if the new "out"
3413 * clock is not available.
3414 */
3415 if (of_property_match_string(np, "clock-names", "out") < 0)
3416 name = "source";
3417 else
3418 name = "out";
3419
3420 sor->clk_out = devm_clk_get(&pdev->dev, name);
3421 if (IS_ERR(sor->clk_out)) {
3422 err = PTR_ERR(sor->clk_out);
3423 dev_err(sor->dev, "failed to get %s clock: %d\n",
3424 name, err);
Thierry Reding618dee32016-06-09 17:53:57 +02003425 goto remove;
3426 }
Thierry Reding1087fac2017-12-14 13:37:53 +01003427 } else {
Thierry Redingd7805372018-01-10 13:04:58 +01003428 /* fall back to the module clock on SOR0 (eDP/LVDS only) */
Thierry Reding1087fac2017-12-14 13:37:53 +01003429 sor->clk_out = sor->clk;
Thierry Reding618dee32016-06-09 17:53:57 +02003430 }
3431
Thierry Reding6b6b6042013-11-15 16:06:05 +01003432 sor->clk_parent = devm_clk_get(&pdev->dev, "parent");
Thierry Reding4dbdc742015-04-27 15:04:26 +02003433 if (IS_ERR(sor->clk_parent)) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02003434 err = PTR_ERR(sor->clk_parent);
3435 dev_err(&pdev->dev, "failed to get parent clock: %d\n", err);
3436 goto remove;
Thierry Reding4dbdc742015-04-27 15:04:26 +02003437 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01003438
Thierry Reding6b6b6042013-11-15 16:06:05 +01003439 sor->clk_safe = devm_clk_get(&pdev->dev, "safe");
Thierry Reding4dbdc742015-04-27 15:04:26 +02003440 if (IS_ERR(sor->clk_safe)) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02003441 err = PTR_ERR(sor->clk_safe);
3442 dev_err(&pdev->dev, "failed to get safe clock: %d\n", err);
3443 goto remove;
Thierry Reding4dbdc742015-04-27 15:04:26 +02003444 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01003445
Thierry Reding6b6b6042013-11-15 16:06:05 +01003446 sor->clk_dp = devm_clk_get(&pdev->dev, "dp");
Thierry Reding4dbdc742015-04-27 15:04:26 +02003447 if (IS_ERR(sor->clk_dp)) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02003448 err = PTR_ERR(sor->clk_dp);
3449 dev_err(&pdev->dev, "failed to get DP clock: %d\n", err);
3450 goto remove;
Thierry Reding4dbdc742015-04-27 15:04:26 +02003451 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01003452
Thierry Redinge1335e22017-10-12 17:53:11 +02003453 /*
3454 * Starting with Tegra186, the BPMP provides an implementation for
3455 * the pad output clock, so we have to look it up from device tree.
3456 */
3457 sor->clk_pad = devm_clk_get(&pdev->dev, "pad");
3458 if (IS_ERR(sor->clk_pad)) {
3459 if (sor->clk_pad != ERR_PTR(-ENOENT)) {
3460 err = PTR_ERR(sor->clk_pad);
3461 goto remove;
3462 }
3463
3464 /*
3465 * If the pad output clock is not available, then we assume
3466 * we're on Tegra210 or earlier and have to provide our own
3467 * implementation.
3468 */
3469 sor->clk_pad = NULL;
3470 }
3471
3472 /*
3473 * The bootloader may have set up the SOR such that it's module clock
3474 * is sourced by one of the display PLLs. However, that doesn't work
3475 * without properly having set up other bits of the SOR.
3476 */
3477 err = clk_set_parent(sor->clk_out, sor->clk_safe);
3478 if (err < 0) {
3479 dev_err(&pdev->dev, "failed to use safe clock: %d\n", err);
3480 goto remove;
3481 }
3482
Thierry Redingaaff8bd2015-08-07 16:04:54 +02003483 platform_set_drvdata(pdev, sor);
3484 pm_runtime_enable(&pdev->dev);
3485
Thierry Redinge1335e22017-10-12 17:53:11 +02003486 /*
3487 * On Tegra210 and earlier, provide our own implementation for the
3488 * pad output clock.
3489 */
3490 if (!sor->clk_pad) {
3491 err = pm_runtime_get_sync(&pdev->dev);
3492 if (err < 0) {
3493 dev_err(&pdev->dev, "failed to get runtime PM: %d\n",
3494 err);
3495 goto remove;
3496 }
Thierry Redingb2992212015-10-01 14:25:03 +02003497
Thierry Redinge1335e22017-10-12 17:53:11 +02003498 sor->clk_pad = tegra_clk_sor_pad_register(sor,
3499 "sor1_pad_clkout");
3500 pm_runtime_put(&pdev->dev);
3501 }
3502
3503 if (IS_ERR(sor->clk_pad)) {
3504 err = PTR_ERR(sor->clk_pad);
3505 dev_err(&pdev->dev, "failed to register SOR pad clock: %d\n",
3506 err);
Thierry Redingb2992212015-10-01 14:25:03 +02003507 goto remove;
3508 }
3509
Thierry Reding6b6b6042013-11-15 16:06:05 +01003510 INIT_LIST_HEAD(&sor->client.list);
3511 sor->client.ops = &sor_client_ops;
3512 sor->client.dev = &pdev->dev;
3513
Thierry Reding6b6b6042013-11-15 16:06:05 +01003514 err = host1x_client_register(&sor->client);
3515 if (err < 0) {
3516 dev_err(&pdev->dev, "failed to register host1x client: %d\n",
3517 err);
Thierry Reding459cc2c2015-07-30 10:34:24 +02003518 goto remove;
Thierry Reding6b6b6042013-11-15 16:06:05 +01003519 }
3520
Thierry Reding6b6b6042013-11-15 16:06:05 +01003521 return 0;
Thierry Reding459cc2c2015-07-30 10:34:24 +02003522
3523remove:
3524 if (sor->ops && sor->ops->remove)
3525 sor->ops->remove(sor);
3526output:
3527 tegra_output_remove(&sor->output);
3528 return err;
Thierry Reding6b6b6042013-11-15 16:06:05 +01003529}
3530
3531static int tegra_sor_remove(struct platform_device *pdev)
3532{
3533 struct tegra_sor *sor = platform_get_drvdata(pdev);
3534 int err;
3535
Thierry Redingaaff8bd2015-08-07 16:04:54 +02003536 pm_runtime_disable(&pdev->dev);
3537
Thierry Reding6b6b6042013-11-15 16:06:05 +01003538 err = host1x_client_unregister(&sor->client);
3539 if (err < 0) {
3540 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
3541 err);
3542 return err;
3543 }
3544
Thierry Reding459cc2c2015-07-30 10:34:24 +02003545 if (sor->ops && sor->ops->remove) {
3546 err = sor->ops->remove(sor);
3547 if (err < 0)
3548 dev_err(&pdev->dev, "failed to remove SOR: %d\n", err);
3549 }
3550
Thierry Reding328ec692014-12-19 15:55:08 +01003551 tegra_output_remove(&sor->output);
Thierry Reding6b6b6042013-11-15 16:06:05 +01003552
3553 return 0;
3554}
3555
Thierry Redingaaff8bd2015-08-07 16:04:54 +02003556#ifdef CONFIG_PM
3557static int tegra_sor_suspend(struct device *dev)
3558{
3559 struct tegra_sor *sor = dev_get_drvdata(dev);
3560 int err;
3561
Jon Hunterf8c79122016-07-01 14:21:38 +01003562 if (sor->rst) {
3563 err = reset_control_assert(sor->rst);
3564 if (err < 0) {
3565 dev_err(dev, "failed to assert reset: %d\n", err);
3566 return err;
3567 }
Thierry Redingaaff8bd2015-08-07 16:04:54 +02003568 }
3569
3570 usleep_range(1000, 2000);
3571
3572 clk_disable_unprepare(sor->clk);
3573
3574 return 0;
3575}
3576
3577static int tegra_sor_resume(struct device *dev)
3578{
3579 struct tegra_sor *sor = dev_get_drvdata(dev);
3580 int err;
3581
3582 err = clk_prepare_enable(sor->clk);
3583 if (err < 0) {
3584 dev_err(dev, "failed to enable clock: %d\n", err);
3585 return err;
3586 }
3587
3588 usleep_range(1000, 2000);
3589
Jon Hunterf8c79122016-07-01 14:21:38 +01003590 if (sor->rst) {
3591 err = reset_control_deassert(sor->rst);
3592 if (err < 0) {
3593 dev_err(dev, "failed to deassert reset: %d\n", err);
3594 clk_disable_unprepare(sor->clk);
3595 return err;
3596 }
Thierry Redingaaff8bd2015-08-07 16:04:54 +02003597 }
3598
3599 return 0;
3600}
3601#endif
3602
3603static const struct dev_pm_ops tegra_sor_pm_ops = {
3604 SET_RUNTIME_PM_OPS(tegra_sor_suspend, tegra_sor_resume, NULL)
3605};
3606
Thierry Reding6b6b6042013-11-15 16:06:05 +01003607struct platform_driver tegra_sor_driver = {
3608 .driver = {
3609 .name = "tegra-sor",
3610 .of_match_table = tegra_sor_of_match,
Thierry Redingaaff8bd2015-08-07 16:04:54 +02003611 .pm = &tegra_sor_pm_ops,
Thierry Reding6b6b6042013-11-15 16:06:05 +01003612 },
3613 .probe = tegra_sor_probe,
3614 .remove = tegra_sor_remove,
3615};