blob: beb8fda258264367f05f7a9fc4534de4637c0e58 [file] [log] [blame]
Thierry Reding6b6b6042013-11-15 16:06:05 +01001/*
2 * Copyright (C) 2013 NVIDIA Corporation
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8
9#include <linux/clk.h>
Thierry Redingb2992212015-10-01 14:25:03 +020010#include <linux/clk-provider.h>
Thierry Redinga82752e2014-01-31 10:02:15 +010011#include <linux/debugfs.h>
Thierry Reding6fad8f62014-11-28 15:41:34 +010012#include <linux/gpio.h>
Thierry Reding6b6b6042013-11-15 16:06:05 +010013#include <linux/io.h>
Thierry Reding459cc2c2015-07-30 10:34:24 +020014#include <linux/of_device.h>
Thierry Reding6b6b6042013-11-15 16:06:05 +010015#include <linux/platform_device.h>
Thierry Redingaaff8bd2015-08-07 16:04:54 +020016#include <linux/pm_runtime.h>
Thierry Reding459cc2c2015-07-30 10:34:24 +020017#include <linux/regulator/consumer.h>
Thierry Reding6b6b6042013-11-15 16:06:05 +010018#include <linux/reset.h>
Thierry Reding306a7f92014-07-17 13:17:24 +020019
Thierry Reding72323982014-07-11 13:19:06 +020020#include <soc/tegra/pmc.h>
Thierry Reding6b6b6042013-11-15 16:06:05 +010021
Thierry Reding4aa3df72014-11-24 16:27:13 +010022#include <drm/drm_atomic_helper.h>
Thierry Reding6b6b6042013-11-15 16:06:05 +010023#include <drm/drm_dp_helper.h>
Thierry Reding6fad8f62014-11-28 15:41:34 +010024#include <drm/drm_panel.h>
Thierry Reding36e90222017-10-12 19:14:21 +020025#include <drm/drm_scdc_helper.h>
Thierry Reding6b6b6042013-11-15 16:06:05 +010026
27#include "dc.h"
28#include "drm.h"
Thierry Redingfad7b802019-01-03 15:23:16 +010029#include "hda.h"
Thierry Reding6b6b6042013-11-15 16:06:05 +010030#include "sor.h"
Thierry Reding932f6522017-08-15 15:41:14 +020031#include "trace.h"
Thierry Reding6b6b6042013-11-15 16:06:05 +010032
Thierry Reding459cc2c2015-07-30 10:34:24 +020033#define SOR_REKEY 0x38
34
35struct tegra_sor_hdmi_settings {
36 unsigned long frequency;
37
38 u8 vcocap;
Thierry Redingc57997b2017-10-12 19:12:57 +020039 u8 filter;
Thierry Reding459cc2c2015-07-30 10:34:24 +020040 u8 ichpmp;
41 u8 loadadj;
Thierry Redingc57997b2017-10-12 19:12:57 +020042 u8 tmds_termadj;
43 u8 tx_pu_value;
44 u8 bg_temp_coef;
45 u8 bg_vref_level;
46 u8 avdd10_level;
47 u8 avdd14_level;
48 u8 sparepll;
Thierry Reding459cc2c2015-07-30 10:34:24 +020049
50 u8 drive_current[4];
51 u8 preemphasis[4];
52};
53
54#if 1
55static const struct tegra_sor_hdmi_settings tegra210_sor_hdmi_defaults[] = {
56 {
57 .frequency = 54000000,
58 .vcocap = 0x0,
Thierry Redingc57997b2017-10-12 19:12:57 +020059 .filter = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +020060 .ichpmp = 0x1,
61 .loadadj = 0x3,
Thierry Redingc57997b2017-10-12 19:12:57 +020062 .tmds_termadj = 0x9,
63 .tx_pu_value = 0x10,
64 .bg_temp_coef = 0x3,
65 .bg_vref_level = 0x8,
66 .avdd10_level = 0x4,
67 .avdd14_level = 0x4,
68 .sparepll = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +020069 .drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
70 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
71 }, {
72 .frequency = 75000000,
73 .vcocap = 0x3,
Thierry Redingc57997b2017-10-12 19:12:57 +020074 .filter = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +020075 .ichpmp = 0x1,
76 .loadadj = 0x3,
Thierry Redingc57997b2017-10-12 19:12:57 +020077 .tmds_termadj = 0x9,
78 .tx_pu_value = 0x40,
79 .bg_temp_coef = 0x3,
80 .bg_vref_level = 0x8,
81 .avdd10_level = 0x4,
82 .avdd14_level = 0x4,
83 .sparepll = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +020084 .drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
85 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
86 }, {
87 .frequency = 150000000,
88 .vcocap = 0x3,
Thierry Redingc57997b2017-10-12 19:12:57 +020089 .filter = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +020090 .ichpmp = 0x1,
91 .loadadj = 0x3,
Thierry Redingc57997b2017-10-12 19:12:57 +020092 .tmds_termadj = 0x9,
93 .tx_pu_value = 0x66,
94 .bg_temp_coef = 0x3,
95 .bg_vref_level = 0x8,
96 .avdd10_level = 0x4,
97 .avdd14_level = 0x4,
98 .sparepll = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +020099 .drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
100 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
101 }, {
102 .frequency = 300000000,
103 .vcocap = 0x3,
Thierry Redingc57997b2017-10-12 19:12:57 +0200104 .filter = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +0200105 .ichpmp = 0x1,
106 .loadadj = 0x3,
Thierry Redingc57997b2017-10-12 19:12:57 +0200107 .tmds_termadj = 0x9,
108 .tx_pu_value = 0x66,
109 .bg_temp_coef = 0x3,
110 .bg_vref_level = 0xa,
111 .avdd10_level = 0x4,
112 .avdd14_level = 0x4,
113 .sparepll = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +0200114 .drive_current = { 0x33, 0x3f, 0x3f, 0x3f },
115 .preemphasis = { 0x00, 0x17, 0x17, 0x17 },
116 }, {
117 .frequency = 600000000,
118 .vcocap = 0x3,
Thierry Redingc57997b2017-10-12 19:12:57 +0200119 .filter = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +0200120 .ichpmp = 0x1,
121 .loadadj = 0x3,
Thierry Redingc57997b2017-10-12 19:12:57 +0200122 .tmds_termadj = 0x9,
123 .tx_pu_value = 0x66,
124 .bg_temp_coef = 0x3,
125 .bg_vref_level = 0x8,
126 .avdd10_level = 0x4,
127 .avdd14_level = 0x4,
128 .sparepll = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +0200129 .drive_current = { 0x33, 0x3f, 0x3f, 0x3f },
130 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
131 },
132};
133#else
134static const struct tegra_sor_hdmi_settings tegra210_sor_hdmi_defaults[] = {
135 {
136 .frequency = 75000000,
137 .vcocap = 0x3,
Thierry Redingc57997b2017-10-12 19:12:57 +0200138 .filter = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +0200139 .ichpmp = 0x1,
140 .loadadj = 0x3,
Thierry Redingc57997b2017-10-12 19:12:57 +0200141 .tmds_termadj = 0x9,
142 .tx_pu_value = 0x40,
143 .bg_temp_coef = 0x3,
144 .bg_vref_level = 0x8,
145 .avdd10_level = 0x4,
146 .avdd14_level = 0x4,
147 .sparepll = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +0200148 .drive_current = { 0x29, 0x29, 0x29, 0x29 },
149 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
150 }, {
151 .frequency = 150000000,
152 .vcocap = 0x3,
Thierry Redingc57997b2017-10-12 19:12:57 +0200153 .filter = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +0200154 .ichpmp = 0x1,
155 .loadadj = 0x3,
Thierry Redingc57997b2017-10-12 19:12:57 +0200156 .tmds_termadj = 0x9,
157 .tx_pu_value = 0x66,
158 .bg_temp_coef = 0x3,
159 .bg_vref_level = 0x8,
160 .avdd10_level = 0x4,
161 .avdd14_level = 0x4,
162 .sparepll = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +0200163 .drive_current = { 0x30, 0x37, 0x37, 0x37 },
164 .preemphasis = { 0x01, 0x02, 0x02, 0x02 },
165 }, {
166 .frequency = 300000000,
167 .vcocap = 0x3,
Thierry Redingc57997b2017-10-12 19:12:57 +0200168 .filter = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +0200169 .ichpmp = 0x6,
170 .loadadj = 0x3,
Thierry Redingc57997b2017-10-12 19:12:57 +0200171 .tmds_termadj = 0x9,
172 .tx_pu_value = 0x66,
173 .bg_temp_coef = 0x3,
174 .bg_vref_level = 0xf,
175 .avdd10_level = 0x4,
176 .avdd14_level = 0x4,
177 .sparepll = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +0200178 .drive_current = { 0x30, 0x37, 0x37, 0x37 },
179 .preemphasis = { 0x10, 0x3e, 0x3e, 0x3e },
180 }, {
181 .frequency = 600000000,
182 .vcocap = 0x3,
Thierry Redingc57997b2017-10-12 19:12:57 +0200183 .filter = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +0200184 .ichpmp = 0xa,
185 .loadadj = 0x3,
Thierry Redingc57997b2017-10-12 19:12:57 +0200186 .tmds_termadj = 0xb,
187 .tx_pu_value = 0x66,
188 .bg_temp_coef = 0x3,
189 .bg_vref_level = 0xe,
190 .avdd10_level = 0x4,
191 .avdd14_level = 0x4,
192 .sparepll = 0x0,
Thierry Reding459cc2c2015-07-30 10:34:24 +0200193 .drive_current = { 0x35, 0x3e, 0x3e, 0x3e },
194 .preemphasis = { 0x02, 0x3f, 0x3f, 0x3f },
195 },
196};
197#endif
198
Thierry Redingc57997b2017-10-12 19:12:57 +0200199static const struct tegra_sor_hdmi_settings tegra186_sor_hdmi_defaults[] = {
200 {
201 .frequency = 54000000,
202 .vcocap = 0,
203 .filter = 5,
204 .ichpmp = 5,
205 .loadadj = 3,
206 .tmds_termadj = 0xf,
207 .tx_pu_value = 0,
208 .bg_temp_coef = 3,
209 .bg_vref_level = 8,
210 .avdd10_level = 4,
211 .avdd14_level = 4,
212 .sparepll = 0x54,
213 .drive_current = { 0x3a, 0x3a, 0x3a, 0x33 },
214 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
215 }, {
216 .frequency = 75000000,
217 .vcocap = 1,
218 .filter = 5,
219 .ichpmp = 5,
220 .loadadj = 3,
221 .tmds_termadj = 0xf,
222 .tx_pu_value = 0,
223 .bg_temp_coef = 3,
224 .bg_vref_level = 8,
225 .avdd10_level = 4,
226 .avdd14_level = 4,
227 .sparepll = 0x44,
228 .drive_current = { 0x3a, 0x3a, 0x3a, 0x33 },
229 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
230 }, {
231 .frequency = 150000000,
232 .vcocap = 3,
233 .filter = 5,
234 .ichpmp = 5,
235 .loadadj = 3,
236 .tmds_termadj = 15,
237 .tx_pu_value = 0x66 /* 0 */,
238 .bg_temp_coef = 3,
239 .bg_vref_level = 8,
240 .avdd10_level = 4,
241 .avdd14_level = 4,
242 .sparepll = 0x00, /* 0x34 */
243 .drive_current = { 0x3a, 0x3a, 0x3a, 0x37 },
244 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
245 }, {
246 .frequency = 300000000,
247 .vcocap = 3,
248 .filter = 5,
249 .ichpmp = 5,
250 .loadadj = 3,
251 .tmds_termadj = 15,
252 .tx_pu_value = 64,
253 .bg_temp_coef = 3,
254 .bg_vref_level = 8,
255 .avdd10_level = 4,
256 .avdd14_level = 4,
257 .sparepll = 0x34,
258 .drive_current = { 0x3d, 0x3d, 0x3d, 0x33 },
259 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
260 }, {
261 .frequency = 600000000,
262 .vcocap = 3,
263 .filter = 5,
264 .ichpmp = 5,
265 .loadadj = 3,
266 .tmds_termadj = 12,
267 .tx_pu_value = 96,
268 .bg_temp_coef = 3,
269 .bg_vref_level = 8,
270 .avdd10_level = 4,
271 .avdd14_level = 4,
272 .sparepll = 0x34,
273 .drive_current = { 0x3d, 0x3d, 0x3d, 0x33 },
274 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
275 }
276};
277
Thierry Reding9b6c14b2018-09-21 12:27:46 +0200278static const struct tegra_sor_hdmi_settings tegra194_sor_hdmi_defaults[] = {
279 {
280 .frequency = 54000000,
281 .vcocap = 0,
282 .filter = 5,
283 .ichpmp = 5,
284 .loadadj = 3,
285 .tmds_termadj = 0xf,
286 .tx_pu_value = 0,
287 .bg_temp_coef = 3,
288 .bg_vref_level = 8,
289 .avdd10_level = 4,
290 .avdd14_level = 4,
291 .sparepll = 0x54,
292 .drive_current = { 0x3a, 0x3a, 0x3a, 0x33 },
293 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
294 }, {
295 .frequency = 75000000,
296 .vcocap = 1,
297 .filter = 5,
298 .ichpmp = 5,
299 .loadadj = 3,
300 .tmds_termadj = 0xf,
301 .tx_pu_value = 0,
302 .bg_temp_coef = 3,
303 .bg_vref_level = 8,
304 .avdd10_level = 4,
305 .avdd14_level = 4,
306 .sparepll = 0x44,
307 .drive_current = { 0x3a, 0x3a, 0x3a, 0x33 },
308 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
309 }, {
310 .frequency = 150000000,
311 .vcocap = 3,
312 .filter = 5,
313 .ichpmp = 5,
314 .loadadj = 3,
315 .tmds_termadj = 15,
316 .tx_pu_value = 0x66 /* 0 */,
317 .bg_temp_coef = 3,
318 .bg_vref_level = 8,
319 .avdd10_level = 4,
320 .avdd14_level = 4,
321 .sparepll = 0x00, /* 0x34 */
322 .drive_current = { 0x3a, 0x3a, 0x3a, 0x37 },
323 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
324 }, {
325 .frequency = 300000000,
326 .vcocap = 3,
327 .filter = 5,
328 .ichpmp = 5,
329 .loadadj = 3,
330 .tmds_termadj = 15,
331 .tx_pu_value = 64,
332 .bg_temp_coef = 3,
333 .bg_vref_level = 8,
334 .avdd10_level = 4,
335 .avdd14_level = 4,
336 .sparepll = 0x34,
337 .drive_current = { 0x3d, 0x3d, 0x3d, 0x33 },
338 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
339 }, {
340 .frequency = 600000000,
341 .vcocap = 3,
342 .filter = 5,
343 .ichpmp = 5,
344 .loadadj = 3,
345 .tmds_termadj = 12,
346 .tx_pu_value = 96,
347 .bg_temp_coef = 3,
348 .bg_vref_level = 8,
349 .avdd10_level = 4,
350 .avdd14_level = 4,
351 .sparepll = 0x34,
352 .drive_current = { 0x3d, 0x3d, 0x3d, 0x33 },
353 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
354 }
355};
356
Thierry Reding880cee02017-10-12 19:04:17 +0200357struct tegra_sor_regs {
358 unsigned int head_state0;
359 unsigned int head_state1;
360 unsigned int head_state2;
361 unsigned int head_state3;
362 unsigned int head_state4;
363 unsigned int head_state5;
364 unsigned int pll0;
365 unsigned int pll1;
366 unsigned int pll2;
367 unsigned int pll3;
368 unsigned int dp_padctl0;
369 unsigned int dp_padctl2;
370};
371
Thierry Reding459cc2c2015-07-30 10:34:24 +0200372struct tegra_sor_soc {
373 bool supports_edp;
374 bool supports_lvds;
375 bool supports_hdmi;
376 bool supports_dp;
377
Thierry Reding880cee02017-10-12 19:04:17 +0200378 const struct tegra_sor_regs *regs;
Thierry Redingc57997b2017-10-12 19:12:57 +0200379 bool has_nvdisplay;
Thierry Reding880cee02017-10-12 19:04:17 +0200380
Thierry Reding459cc2c2015-07-30 10:34:24 +0200381 const struct tegra_sor_hdmi_settings *settings;
382 unsigned int num_settings;
Thierry Reding30b49432015-08-03 15:50:32 +0200383
384 const u8 *xbar_cfg;
Thierry Reding459cc2c2015-07-30 10:34:24 +0200385};
386
387struct tegra_sor;
388
389struct tegra_sor_ops {
390 const char *name;
391 int (*probe)(struct tegra_sor *sor);
392 int (*remove)(struct tegra_sor *sor);
393};
394
Thierry Reding6b6b6042013-11-15 16:06:05 +0100395struct tegra_sor {
396 struct host1x_client client;
397 struct tegra_output output;
398 struct device *dev;
399
Thierry Reding459cc2c2015-07-30 10:34:24 +0200400 const struct tegra_sor_soc *soc;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100401 void __iomem *regs;
Thierry Redingc57997b2017-10-12 19:12:57 +0200402 unsigned int index;
Thierry Reding8e2988a72018-12-03 15:46:03 +0100403 unsigned int irq;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100404
405 struct reset_control *rst;
406 struct clk *clk_parent;
407 struct clk *clk_safe;
Thierry Redinge1335e22017-10-12 17:53:11 +0200408 struct clk *clk_out;
409 struct clk *clk_pad;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100410 struct clk *clk_dp;
411 struct clk *clk;
412
Thierry Reding6d6c8152019-01-25 11:00:58 +0100413 u8 xbar_cfg[5];
414
Thierry Reding9542c232015-07-08 13:39:09 +0200415 struct drm_dp_aux *aux;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100416
Thierry Redingdab16332015-01-26 16:04:08 +0100417 struct drm_info_list *debugfs_files;
Thierry Reding459cc2c2015-07-30 10:34:24 +0200418
419 const struct tegra_sor_ops *ops;
Thierry Redingc57997b2017-10-12 19:12:57 +0200420 enum tegra_io_pad pad;
Thierry Reding459cc2c2015-07-30 10:34:24 +0200421
422 /* for HDMI 2.0 */
423 struct tegra_sor_hdmi_settings *settings;
424 unsigned int num_settings;
425
426 struct regulator *avdd_io_supply;
427 struct regulator *vdd_pll_supply;
428 struct regulator *hdmi_supply;
Thierry Reding36e90222017-10-12 19:14:21 +0200429
430 struct delayed_work scdc;
431 bool scdc_enabled;
Thierry Reding8e2988a72018-12-03 15:46:03 +0100432
Thierry Redingfad7b802019-01-03 15:23:16 +0100433 struct tegra_hda_format format;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100434};
435
Thierry Redingc31efa72015-09-08 16:09:22 +0200436struct tegra_sor_state {
437 struct drm_connector_state base;
438
Thierry Reding36e90222017-10-12 19:14:21 +0200439 unsigned int link_speed;
440 unsigned long pclk;
Thierry Redingc31efa72015-09-08 16:09:22 +0200441 unsigned int bpc;
442};
443
444static inline struct tegra_sor_state *
445to_sor_state(struct drm_connector_state *state)
446{
447 return container_of(state, struct tegra_sor_state, base);
448}
449
Thierry Reding34fa1832014-06-05 16:31:10 +0200450struct tegra_sor_config {
451 u32 bits_per_pixel;
452
453 u32 active_polarity;
454 u32 active_count;
455 u32 tu_size;
456 u32 active_frac;
457 u32 watermark;
Thierry Reding7890b572014-06-05 16:12:46 +0200458
459 u32 hblank_symbols;
460 u32 vblank_symbols;
Thierry Reding34fa1832014-06-05 16:31:10 +0200461};
462
Thierry Reding6b6b6042013-11-15 16:06:05 +0100463static inline struct tegra_sor *
464host1x_client_to_sor(struct host1x_client *client)
465{
466 return container_of(client, struct tegra_sor, client);
467}
468
469static inline struct tegra_sor *to_sor(struct tegra_output *output)
470{
471 return container_of(output, struct tegra_sor, output);
472}
473
Thierry Reding5c5f1302017-08-15 15:41:09 +0200474static inline u32 tegra_sor_readl(struct tegra_sor *sor, unsigned int offset)
Thierry Reding6b6b6042013-11-15 16:06:05 +0100475{
Thierry Reding932f6522017-08-15 15:41:14 +0200476 u32 value = readl(sor->regs + (offset << 2));
477
478 trace_sor_readl(sor->dev, offset, value);
479
480 return value;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100481}
482
Thierry Reding28fe2072015-01-26 16:02:48 +0100483static inline void tegra_sor_writel(struct tegra_sor *sor, u32 value,
Thierry Reding5c5f1302017-08-15 15:41:09 +0200484 unsigned int offset)
Thierry Reding6b6b6042013-11-15 16:06:05 +0100485{
Thierry Reding932f6522017-08-15 15:41:14 +0200486 trace_sor_writel(sor->dev, offset, value);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100487 writel(value, sor->regs + (offset << 2));
488}
489
Thierry Reding25bb2ce2015-08-03 14:23:29 +0200490static int tegra_sor_set_parent_clock(struct tegra_sor *sor, struct clk *parent)
491{
492 int err;
493
494 clk_disable_unprepare(sor->clk);
495
Thierry Redinge1335e22017-10-12 17:53:11 +0200496 err = clk_set_parent(sor->clk_out, parent);
Thierry Reding25bb2ce2015-08-03 14:23:29 +0200497 if (err < 0)
498 return err;
499
500 err = clk_prepare_enable(sor->clk);
501 if (err < 0)
502 return err;
503
504 return 0;
505}
506
Thierry Redinge1335e22017-10-12 17:53:11 +0200507struct tegra_clk_sor_pad {
Thierry Redingb2992212015-10-01 14:25:03 +0200508 struct clk_hw hw;
509 struct tegra_sor *sor;
510};
511
Thierry Redinge1335e22017-10-12 17:53:11 +0200512static inline struct tegra_clk_sor_pad *to_pad(struct clk_hw *hw)
Thierry Redingb2992212015-10-01 14:25:03 +0200513{
Thierry Redinge1335e22017-10-12 17:53:11 +0200514 return container_of(hw, struct tegra_clk_sor_pad, hw);
Thierry Redingb2992212015-10-01 14:25:03 +0200515}
516
Thierry Redinge1335e22017-10-12 17:53:11 +0200517static const char * const tegra_clk_sor_pad_parents[] = {
Thierry Redingb2992212015-10-01 14:25:03 +0200518 "pll_d2_out0", "pll_dp"
519};
520
Thierry Redinge1335e22017-10-12 17:53:11 +0200521static int tegra_clk_sor_pad_set_parent(struct clk_hw *hw, u8 index)
Thierry Redingb2992212015-10-01 14:25:03 +0200522{
Thierry Redinge1335e22017-10-12 17:53:11 +0200523 struct tegra_clk_sor_pad *pad = to_pad(hw);
524 struct tegra_sor *sor = pad->sor;
Thierry Redingb2992212015-10-01 14:25:03 +0200525 u32 value;
526
527 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
528 value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
529
530 switch (index) {
531 case 0:
532 value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK;
533 break;
534
535 case 1:
536 value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
537 break;
538 }
539
540 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
541
542 return 0;
543}
544
Thierry Redinge1335e22017-10-12 17:53:11 +0200545static u8 tegra_clk_sor_pad_get_parent(struct clk_hw *hw)
Thierry Redingb2992212015-10-01 14:25:03 +0200546{
Thierry Redinge1335e22017-10-12 17:53:11 +0200547 struct tegra_clk_sor_pad *pad = to_pad(hw);
548 struct tegra_sor *sor = pad->sor;
Thierry Redingb2992212015-10-01 14:25:03 +0200549 u8 parent = U8_MAX;
550 u32 value;
551
552 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
553
554 switch (value & SOR_CLK_CNTRL_DP_CLK_SEL_MASK) {
555 case SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK:
556 case SOR_CLK_CNTRL_DP_CLK_SEL_DIFF_PCLK:
557 parent = 0;
558 break;
559
560 case SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK:
561 case SOR_CLK_CNTRL_DP_CLK_SEL_DIFF_DPCLK:
562 parent = 1;
563 break;
564 }
565
566 return parent;
567}
568
Thierry Redinge1335e22017-10-12 17:53:11 +0200569static const struct clk_ops tegra_clk_sor_pad_ops = {
570 .set_parent = tegra_clk_sor_pad_set_parent,
571 .get_parent = tegra_clk_sor_pad_get_parent,
Thierry Redingb2992212015-10-01 14:25:03 +0200572};
573
Thierry Redinge1335e22017-10-12 17:53:11 +0200574static struct clk *tegra_clk_sor_pad_register(struct tegra_sor *sor,
575 const char *name)
Thierry Redingb2992212015-10-01 14:25:03 +0200576{
Thierry Redinge1335e22017-10-12 17:53:11 +0200577 struct tegra_clk_sor_pad *pad;
Thierry Redingb2992212015-10-01 14:25:03 +0200578 struct clk_init_data init;
579 struct clk *clk;
580
Thierry Redinge1335e22017-10-12 17:53:11 +0200581 pad = devm_kzalloc(sor->dev, sizeof(*pad), GFP_KERNEL);
582 if (!pad)
Thierry Redingb2992212015-10-01 14:25:03 +0200583 return ERR_PTR(-ENOMEM);
584
Thierry Redinge1335e22017-10-12 17:53:11 +0200585 pad->sor = sor;
Thierry Redingb2992212015-10-01 14:25:03 +0200586
587 init.name = name;
588 init.flags = 0;
Thierry Redinge1335e22017-10-12 17:53:11 +0200589 init.parent_names = tegra_clk_sor_pad_parents;
590 init.num_parents = ARRAY_SIZE(tegra_clk_sor_pad_parents);
591 init.ops = &tegra_clk_sor_pad_ops;
Thierry Redingb2992212015-10-01 14:25:03 +0200592
Thierry Redinge1335e22017-10-12 17:53:11 +0200593 pad->hw.init = &init;
Thierry Redingb2992212015-10-01 14:25:03 +0200594
Thierry Redinge1335e22017-10-12 17:53:11 +0200595 clk = devm_clk_register(sor->dev, &pad->hw);
Thierry Redingb2992212015-10-01 14:25:03 +0200596
597 return clk;
598}
599
Thierry Reding6b6b6042013-11-15 16:06:05 +0100600static int tegra_sor_dp_train_fast(struct tegra_sor *sor,
601 struct drm_dp_link *link)
602{
Thierry Reding6b6b6042013-11-15 16:06:05 +0100603 unsigned int i;
604 u8 pattern;
Thierry Reding28fe2072015-01-26 16:02:48 +0100605 u32 value;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100606 int err;
607
608 /* setup lane parameters */
609 value = SOR_LANE_DRIVE_CURRENT_LANE3(0x40) |
610 SOR_LANE_DRIVE_CURRENT_LANE2(0x40) |
611 SOR_LANE_DRIVE_CURRENT_LANE1(0x40) |
612 SOR_LANE_DRIVE_CURRENT_LANE0(0x40);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200613 tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100614
615 value = SOR_LANE_PREEMPHASIS_LANE3(0x0f) |
616 SOR_LANE_PREEMPHASIS_LANE2(0x0f) |
617 SOR_LANE_PREEMPHASIS_LANE1(0x0f) |
618 SOR_LANE_PREEMPHASIS_LANE0(0x0f);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200619 tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100620
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200621 value = SOR_LANE_POSTCURSOR_LANE3(0x00) |
622 SOR_LANE_POSTCURSOR_LANE2(0x00) |
623 SOR_LANE_POSTCURSOR_LANE1(0x00) |
624 SOR_LANE_POSTCURSOR_LANE0(0x00);
625 tegra_sor_writel(sor, value, SOR_LANE_POSTCURSOR0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100626
627 /* disable LVDS mode */
628 tegra_sor_writel(sor, 0, SOR_LVDS);
629
Thierry Reding880cee02017-10-12 19:04:17 +0200630 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100631 value |= SOR_DP_PADCTL_TX_PU_ENABLE;
632 value &= ~SOR_DP_PADCTL_TX_PU_MASK;
633 value |= SOR_DP_PADCTL_TX_PU(2); /* XXX: don't hardcode? */
Thierry Reding880cee02017-10-12 19:04:17 +0200634 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100635
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_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
638 SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0;
Thierry Reding880cee02017-10-12 19:04:17 +0200639 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100640
641 usleep_range(10, 100);
642
Thierry Reding880cee02017-10-12 19:04:17 +0200643 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100644 value &= ~(SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
645 SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0);
Thierry Reding880cee02017-10-12 19:04:17 +0200646 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100647
Thierry Reding9542c232015-07-08 13:39:09 +0200648 err = drm_dp_aux_prepare(sor->aux, DP_SET_ANSI_8B10B);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100649 if (err < 0)
650 return err;
651
652 for (i = 0, value = 0; i < link->num_lanes; i++) {
653 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
654 SOR_DP_TPG_SCRAMBLER_NONE |
655 SOR_DP_TPG_PATTERN_TRAIN1;
656 value = (value << 8) | lane;
657 }
658
659 tegra_sor_writel(sor, value, SOR_DP_TPG);
660
661 pattern = DP_TRAINING_PATTERN_1;
662
Thierry Reding9542c232015-07-08 13:39:09 +0200663 err = drm_dp_aux_train(sor->aux, link, pattern);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100664 if (err < 0)
665 return err;
666
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200667 value = tegra_sor_readl(sor, SOR_DP_SPARE0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100668 value |= SOR_DP_SPARE_SEQ_ENABLE;
669 value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
670 value |= SOR_DP_SPARE_MACRO_SOR_CLK;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200671 tegra_sor_writel(sor, value, SOR_DP_SPARE0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100672
673 for (i = 0, value = 0; i < link->num_lanes; i++) {
674 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
675 SOR_DP_TPG_SCRAMBLER_NONE |
676 SOR_DP_TPG_PATTERN_TRAIN2;
677 value = (value << 8) | lane;
678 }
679
680 tegra_sor_writel(sor, value, SOR_DP_TPG);
681
682 pattern = DP_LINK_SCRAMBLING_DISABLE | DP_TRAINING_PATTERN_2;
683
Thierry Reding9542c232015-07-08 13:39:09 +0200684 err = drm_dp_aux_train(sor->aux, link, pattern);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100685 if (err < 0)
686 return err;
687
688 for (i = 0, value = 0; i < link->num_lanes; i++) {
689 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
690 SOR_DP_TPG_SCRAMBLER_GALIOS |
691 SOR_DP_TPG_PATTERN_NONE;
692 value = (value << 8) | lane;
693 }
694
695 tegra_sor_writel(sor, value, SOR_DP_TPG);
696
697 pattern = DP_TRAINING_PATTERN_DISABLE;
698
Thierry Reding9542c232015-07-08 13:39:09 +0200699 err = drm_dp_aux_train(sor->aux, link, pattern);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100700 if (err < 0)
701 return err;
702
703 return 0;
704}
705
706static void tegra_sor_super_update(struct tegra_sor *sor)
707{
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200708 tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
709 tegra_sor_writel(sor, 1, SOR_SUPER_STATE0);
710 tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100711}
712
713static void tegra_sor_update(struct tegra_sor *sor)
714{
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200715 tegra_sor_writel(sor, 0, SOR_STATE0);
716 tegra_sor_writel(sor, 1, SOR_STATE0);
717 tegra_sor_writel(sor, 0, SOR_STATE0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100718}
719
720static int tegra_sor_setup_pwm(struct tegra_sor *sor, unsigned long timeout)
721{
Thierry Reding28fe2072015-01-26 16:02:48 +0100722 u32 value;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100723
724 value = tegra_sor_readl(sor, SOR_PWM_DIV);
725 value &= ~SOR_PWM_DIV_MASK;
726 value |= 0x400; /* period */
727 tegra_sor_writel(sor, value, SOR_PWM_DIV);
728
729 value = tegra_sor_readl(sor, SOR_PWM_CTL);
730 value &= ~SOR_PWM_CTL_DUTY_CYCLE_MASK;
731 value |= 0x400; /* duty cycle */
732 value &= ~SOR_PWM_CTL_CLK_SEL; /* clock source: PCLK */
733 value |= SOR_PWM_CTL_TRIGGER;
734 tegra_sor_writel(sor, value, SOR_PWM_CTL);
735
736 timeout = jiffies + msecs_to_jiffies(timeout);
737
738 while (time_before(jiffies, timeout)) {
739 value = tegra_sor_readl(sor, SOR_PWM_CTL);
740 if ((value & SOR_PWM_CTL_TRIGGER) == 0)
741 return 0;
742
743 usleep_range(25, 100);
744 }
745
746 return -ETIMEDOUT;
747}
748
749static int tegra_sor_attach(struct tegra_sor *sor)
750{
751 unsigned long value, timeout;
752
753 /* wake up in normal mode */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200754 value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100755 value |= SOR_SUPER_STATE_HEAD_MODE_AWAKE;
756 value |= SOR_SUPER_STATE_MODE_NORMAL;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200757 tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100758 tegra_sor_super_update(sor);
759
760 /* attach */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200761 value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100762 value |= SOR_SUPER_STATE_ATTACHED;
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 timeout = jiffies + msecs_to_jiffies(250);
767
768 while (time_before(jiffies, timeout)) {
769 value = tegra_sor_readl(sor, SOR_TEST);
770 if ((value & SOR_TEST_ATTACHED) != 0)
771 return 0;
772
773 usleep_range(25, 100);
774 }
775
776 return -ETIMEDOUT;
777}
778
779static int tegra_sor_wakeup(struct tegra_sor *sor)
780{
Thierry Reding6b6b6042013-11-15 16:06:05 +0100781 unsigned long value, timeout;
782
Thierry Reding6b6b6042013-11-15 16:06:05 +0100783 timeout = jiffies + msecs_to_jiffies(250);
784
785 /* wait for head to wake up */
786 while (time_before(jiffies, timeout)) {
787 value = tegra_sor_readl(sor, SOR_TEST);
788 value &= SOR_TEST_HEAD_MODE_MASK;
789
790 if (value == SOR_TEST_HEAD_MODE_AWAKE)
791 return 0;
792
793 usleep_range(25, 100);
794 }
795
796 return -ETIMEDOUT;
797}
798
799static int tegra_sor_power_up(struct tegra_sor *sor, unsigned long timeout)
800{
Thierry Reding28fe2072015-01-26 16:02:48 +0100801 u32 value;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100802
803 value = tegra_sor_readl(sor, SOR_PWR);
804 value |= SOR_PWR_TRIGGER | SOR_PWR_NORMAL_STATE_PU;
805 tegra_sor_writel(sor, value, SOR_PWR);
806
807 timeout = jiffies + msecs_to_jiffies(timeout);
808
809 while (time_before(jiffies, timeout)) {
810 value = tegra_sor_readl(sor, SOR_PWR);
811 if ((value & SOR_PWR_TRIGGER) == 0)
812 return 0;
813
814 usleep_range(25, 100);
815 }
816
817 return -ETIMEDOUT;
818}
819
Thierry Reding34fa1832014-06-05 16:31:10 +0200820struct tegra_sor_params {
821 /* number of link clocks per line */
822 unsigned int num_clocks;
823 /* ratio between input and output */
824 u64 ratio;
825 /* precision factor */
826 u64 precision;
827
828 unsigned int active_polarity;
829 unsigned int active_count;
830 unsigned int active_frac;
831 unsigned int tu_size;
832 unsigned int error;
833};
834
835static int tegra_sor_compute_params(struct tegra_sor *sor,
836 struct tegra_sor_params *params,
837 unsigned int tu_size)
838{
839 u64 active_sym, active_count, frac, approx;
840 u32 active_polarity, active_frac = 0;
841 const u64 f = params->precision;
842 s64 error;
843
844 active_sym = params->ratio * tu_size;
845 active_count = div_u64(active_sym, f) * f;
846 frac = active_sym - active_count;
847
848 /* fraction < 0.5 */
849 if (frac >= (f / 2)) {
850 active_polarity = 1;
851 frac = f - frac;
852 } else {
853 active_polarity = 0;
854 }
855
856 if (frac != 0) {
857 frac = div_u64(f * f, frac); /* 1/fraction */
858 if (frac <= (15 * f)) {
859 active_frac = div_u64(frac, f);
860
861 /* round up */
862 if (active_polarity)
863 active_frac++;
864 } else {
865 active_frac = active_polarity ? 1 : 15;
866 }
867 }
868
869 if (active_frac == 1)
870 active_polarity = 0;
871
872 if (active_polarity == 1) {
873 if (active_frac) {
874 approx = active_count + (active_frac * (f - 1)) * f;
875 approx = div_u64(approx, active_frac * f);
876 } else {
877 approx = active_count + f;
878 }
879 } else {
880 if (active_frac)
881 approx = active_count + div_u64(f, active_frac);
882 else
883 approx = active_count;
884 }
885
886 error = div_s64(active_sym - approx, tu_size);
887 error *= params->num_clocks;
888
Andrew Morton79211c82015-11-09 14:58:13 -0800889 if (error <= 0 && abs(error) < params->error) {
Thierry Reding34fa1832014-06-05 16:31:10 +0200890 params->active_count = div_u64(active_count, f);
891 params->active_polarity = active_polarity;
892 params->active_frac = active_frac;
Andrew Morton79211c82015-11-09 14:58:13 -0800893 params->error = abs(error);
Thierry Reding34fa1832014-06-05 16:31:10 +0200894 params->tu_size = tu_size;
895
896 if (error == 0)
897 return true;
898 }
899
900 return false;
901}
902
Thierry Redinga1983592015-07-21 16:46:52 +0200903static int tegra_sor_compute_config(struct tegra_sor *sor,
904 const struct drm_display_mode *mode,
905 struct tegra_sor_config *config,
906 struct drm_dp_link *link)
Thierry Reding34fa1832014-06-05 16:31:10 +0200907{
908 const u64 f = 100000, link_rate = link->rate * 1000;
909 const u64 pclk = mode->clock * 1000;
Thierry Reding7890b572014-06-05 16:12:46 +0200910 u64 input, output, watermark, num;
Thierry Reding34fa1832014-06-05 16:31:10 +0200911 struct tegra_sor_params params;
Thierry Reding34fa1832014-06-05 16:31:10 +0200912 u32 num_syms_per_line;
913 unsigned int i;
914
915 if (!link_rate || !link->num_lanes || !pclk || !config->bits_per_pixel)
916 return -EINVAL;
917
918 output = link_rate * 8 * link->num_lanes;
919 input = pclk * config->bits_per_pixel;
920
921 if (input >= output)
922 return -ERANGE;
923
924 memset(&params, 0, sizeof(params));
925 params.ratio = div64_u64(input * f, output);
926 params.num_clocks = div_u64(link_rate * mode->hdisplay, pclk);
927 params.precision = f;
928 params.error = 64 * f;
929 params.tu_size = 64;
930
931 for (i = params.tu_size; i >= 32; i--)
932 if (tegra_sor_compute_params(sor, &params, i))
933 break;
934
935 if (params.active_frac == 0) {
936 config->active_polarity = 0;
937 config->active_count = params.active_count;
938
939 if (!params.active_polarity)
940 config->active_count--;
941
942 config->tu_size = params.tu_size;
943 config->active_frac = 1;
944 } else {
945 config->active_polarity = params.active_polarity;
946 config->active_count = params.active_count;
947 config->active_frac = params.active_frac;
948 config->tu_size = params.tu_size;
949 }
950
951 dev_dbg(sor->dev,
952 "polarity: %d active count: %d tu size: %d active frac: %d\n",
953 config->active_polarity, config->active_count,
954 config->tu_size, config->active_frac);
955
956 watermark = params.ratio * config->tu_size * (f - params.ratio);
957 watermark = div_u64(watermark, f);
958
959 watermark = div_u64(watermark + params.error, f);
960 config->watermark = watermark + (config->bits_per_pixel / 8) + 2;
961 num_syms_per_line = (mode->hdisplay * config->bits_per_pixel) *
962 (link->num_lanes * 8);
963
964 if (config->watermark > 30) {
965 config->watermark = 30;
966 dev_err(sor->dev,
967 "unable to compute TU size, forcing watermark to %u\n",
968 config->watermark);
969 } else if (config->watermark > num_syms_per_line) {
970 config->watermark = num_syms_per_line;
971 dev_err(sor->dev, "watermark too high, forcing to %u\n",
972 config->watermark);
973 }
974
Thierry Reding7890b572014-06-05 16:12:46 +0200975 /* compute the number of symbols per horizontal blanking interval */
976 num = ((mode->htotal - mode->hdisplay) - 7) * link_rate;
977 config->hblank_symbols = div_u64(num, pclk);
978
979 if (link->capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
980 config->hblank_symbols -= 3;
981
982 config->hblank_symbols -= 12 / link->num_lanes;
983
984 /* compute the number of symbols per vertical blanking interval */
985 num = (mode->hdisplay - 25) * link_rate;
986 config->vblank_symbols = div_u64(num, pclk);
987 config->vblank_symbols -= 36 / link->num_lanes + 4;
988
989 dev_dbg(sor->dev, "blank symbols: H:%u V:%u\n", config->hblank_symbols,
990 config->vblank_symbols);
991
Thierry Reding34fa1832014-06-05 16:31:10 +0200992 return 0;
993}
994
Thierry Reding402f6bc2015-07-21 16:48:19 +0200995static void tegra_sor_apply_config(struct tegra_sor *sor,
996 const struct tegra_sor_config *config)
997{
998 u32 value;
999
1000 value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
1001 value &= ~SOR_DP_LINKCTL_TU_SIZE_MASK;
1002 value |= SOR_DP_LINKCTL_TU_SIZE(config->tu_size);
1003 tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
1004
1005 value = tegra_sor_readl(sor, SOR_DP_CONFIG0);
1006 value &= ~SOR_DP_CONFIG_WATERMARK_MASK;
1007 value |= SOR_DP_CONFIG_WATERMARK(config->watermark);
1008
1009 value &= ~SOR_DP_CONFIG_ACTIVE_SYM_COUNT_MASK;
1010 value |= SOR_DP_CONFIG_ACTIVE_SYM_COUNT(config->active_count);
1011
1012 value &= ~SOR_DP_CONFIG_ACTIVE_SYM_FRAC_MASK;
1013 value |= SOR_DP_CONFIG_ACTIVE_SYM_FRAC(config->active_frac);
1014
1015 if (config->active_polarity)
1016 value |= SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
1017 else
1018 value &= ~SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
1019
1020 value |= SOR_DP_CONFIG_ACTIVE_SYM_ENABLE;
1021 value |= SOR_DP_CONFIG_DISPARITY_NEGATIVE;
1022 tegra_sor_writel(sor, value, SOR_DP_CONFIG0);
1023
1024 value = tegra_sor_readl(sor, SOR_DP_AUDIO_HBLANK_SYMBOLS);
1025 value &= ~SOR_DP_AUDIO_HBLANK_SYMBOLS_MASK;
1026 value |= config->hblank_symbols & 0xffff;
1027 tegra_sor_writel(sor, value, SOR_DP_AUDIO_HBLANK_SYMBOLS);
1028
1029 value = tegra_sor_readl(sor, SOR_DP_AUDIO_VBLANK_SYMBOLS);
1030 value &= ~SOR_DP_AUDIO_VBLANK_SYMBOLS_MASK;
1031 value |= config->vblank_symbols & 0xffff;
1032 tegra_sor_writel(sor, value, SOR_DP_AUDIO_VBLANK_SYMBOLS);
1033}
1034
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001035static void tegra_sor_mode_set(struct tegra_sor *sor,
1036 const struct drm_display_mode *mode,
Thierry Redingc31efa72015-09-08 16:09:22 +02001037 struct tegra_sor_state *state)
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001038{
1039 struct tegra_dc *dc = to_tegra_dc(sor->output.encoder.crtc);
1040 unsigned int vbe, vse, hbe, hse, vbs, hbs;
1041 u32 value;
1042
1043 value = tegra_sor_readl(sor, SOR_STATE1);
1044 value &= ~SOR_STATE_ASY_PIXELDEPTH_MASK;
1045 value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
1046 value &= ~SOR_STATE_ASY_OWNER_MASK;
1047
1048 value |= SOR_STATE_ASY_CRC_MODE_COMPLETE |
1049 SOR_STATE_ASY_OWNER(dc->pipe + 1);
1050
1051 if (mode->flags & DRM_MODE_FLAG_PHSYNC)
1052 value &= ~SOR_STATE_ASY_HSYNCPOL;
1053
1054 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1055 value |= SOR_STATE_ASY_HSYNCPOL;
1056
1057 if (mode->flags & DRM_MODE_FLAG_PVSYNC)
1058 value &= ~SOR_STATE_ASY_VSYNCPOL;
1059
1060 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1061 value |= SOR_STATE_ASY_VSYNCPOL;
1062
Thierry Redingc31efa72015-09-08 16:09:22 +02001063 switch (state->bpc) {
1064 case 16:
1065 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_48_444;
1066 break;
1067
1068 case 12:
1069 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_36_444;
1070 break;
1071
1072 case 10:
1073 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_30_444;
1074 break;
1075
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001076 case 8:
1077 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
1078 break;
1079
1080 case 6:
1081 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_18_444;
1082 break;
1083
1084 default:
Thierry Redingc31efa72015-09-08 16:09:22 +02001085 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001086 break;
1087 }
1088
1089 tegra_sor_writel(sor, value, SOR_STATE1);
1090
1091 /*
1092 * TODO: The video timing programming below doesn't seem to match the
1093 * register definitions.
1094 */
1095
1096 value = ((mode->vtotal & 0x7fff) << 16) | (mode->htotal & 0x7fff);
Thierry Reding880cee02017-10-12 19:04:17 +02001097 tegra_sor_writel(sor, value, sor->soc->regs->head_state1 + dc->pipe);
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001098
1099 /* sync end = sync width - 1 */
1100 vse = mode->vsync_end - mode->vsync_start - 1;
1101 hse = mode->hsync_end - mode->hsync_start - 1;
1102
1103 value = ((vse & 0x7fff) << 16) | (hse & 0x7fff);
Thierry Reding880cee02017-10-12 19:04:17 +02001104 tegra_sor_writel(sor, value, sor->soc->regs->head_state2 + dc->pipe);
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001105
1106 /* blank end = sync end + back porch */
1107 vbe = vse + (mode->vtotal - mode->vsync_end);
1108 hbe = hse + (mode->htotal - mode->hsync_end);
1109
1110 value = ((vbe & 0x7fff) << 16) | (hbe & 0x7fff);
Thierry Reding880cee02017-10-12 19:04:17 +02001111 tegra_sor_writel(sor, value, sor->soc->regs->head_state3 + dc->pipe);
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001112
1113 /* blank start = blank end + active */
1114 vbs = vbe + mode->vdisplay;
1115 hbs = hbe + mode->hdisplay;
1116
1117 value = ((vbs & 0x7fff) << 16) | (hbs & 0x7fff);
Thierry Reding880cee02017-10-12 19:04:17 +02001118 tegra_sor_writel(sor, value, sor->soc->regs->head_state4 + dc->pipe);
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001119
1120 /* XXX interlacing support */
Thierry Reding880cee02017-10-12 19:04:17 +02001121 tegra_sor_writel(sor, 0x001, sor->soc->regs->head_state5 + dc->pipe);
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001122}
1123
Thierry Reding6fad8f62014-11-28 15:41:34 +01001124static int tegra_sor_detach(struct tegra_sor *sor)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001125{
Thierry Reding6fad8f62014-11-28 15:41:34 +01001126 unsigned long value, timeout;
1127
1128 /* switch to safe mode */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001129 value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001130 value &= ~SOR_SUPER_STATE_MODE_NORMAL;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001131 tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001132 tegra_sor_super_update(sor);
1133
1134 timeout = jiffies + msecs_to_jiffies(250);
1135
1136 while (time_before(jiffies, timeout)) {
1137 value = tegra_sor_readl(sor, SOR_PWR);
1138 if (value & SOR_PWR_MODE_SAFE)
1139 break;
1140 }
1141
1142 if ((value & SOR_PWR_MODE_SAFE) == 0)
1143 return -ETIMEDOUT;
1144
1145 /* go to sleep */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001146 value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001147 value &= ~SOR_SUPER_STATE_HEAD_MODE_MASK;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001148 tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001149 tegra_sor_super_update(sor);
1150
1151 /* detach */
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_ATTACHED;
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 timeout = jiffies + msecs_to_jiffies(250);
1158
1159 while (time_before(jiffies, timeout)) {
1160 value = tegra_sor_readl(sor, SOR_TEST);
1161 if ((value & SOR_TEST_ATTACHED) == 0)
1162 break;
1163
1164 usleep_range(25, 100);
1165 }
1166
1167 if ((value & SOR_TEST_ATTACHED) != 0)
1168 return -ETIMEDOUT;
1169
1170 return 0;
1171}
1172
1173static int tegra_sor_power_down(struct tegra_sor *sor)
1174{
1175 unsigned long value, timeout;
1176 int err;
1177
1178 value = tegra_sor_readl(sor, SOR_PWR);
1179 value &= ~SOR_PWR_NORMAL_STATE_PU;
1180 value |= SOR_PWR_TRIGGER;
1181 tegra_sor_writel(sor, value, SOR_PWR);
1182
1183 timeout = jiffies + msecs_to_jiffies(250);
1184
1185 while (time_before(jiffies, timeout)) {
1186 value = tegra_sor_readl(sor, SOR_PWR);
1187 if ((value & SOR_PWR_TRIGGER) == 0)
1188 return 0;
1189
1190 usleep_range(25, 100);
1191 }
1192
1193 if ((value & SOR_PWR_TRIGGER) != 0)
1194 return -ETIMEDOUT;
1195
Thierry Reding25bb2ce2015-08-03 14:23:29 +02001196 /* switch to safe parent clock */
1197 err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
Thierry Redinge1335e22017-10-12 17:53:11 +02001198 if (err < 0) {
Thierry Reding6fad8f62014-11-28 15:41:34 +01001199 dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
Thierry Redinge1335e22017-10-12 17:53:11 +02001200 return err;
1201 }
Thierry Reding6fad8f62014-11-28 15:41:34 +01001202
Thierry Reding880cee02017-10-12 19:04:17 +02001203 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001204 value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
1205 SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2);
Thierry Reding880cee02017-10-12 19:04:17 +02001206 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001207
1208 /* stop lane sequencer */
1209 value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_UP |
1210 SOR_LANE_SEQ_CTL_POWER_STATE_DOWN;
1211 tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
1212
1213 timeout = jiffies + msecs_to_jiffies(250);
1214
1215 while (time_before(jiffies, timeout)) {
1216 value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
1217 if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
1218 break;
1219
1220 usleep_range(25, 100);
1221 }
1222
1223 if ((value & SOR_LANE_SEQ_CTL_TRIGGER) != 0)
1224 return -ETIMEDOUT;
1225
Thierry Reding880cee02017-10-12 19:04:17 +02001226 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001227 value |= SOR_PLL2_PORT_POWERDOWN;
Thierry Reding880cee02017-10-12 19:04:17 +02001228 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001229
1230 usleep_range(20, 100);
1231
Thierry Reding880cee02017-10-12 19:04:17 +02001232 value = tegra_sor_readl(sor, sor->soc->regs->pll0);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001233 value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
Thierry Reding880cee02017-10-12 19:04:17 +02001234 tegra_sor_writel(sor, value, sor->soc->regs->pll0);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001235
Thierry Reding880cee02017-10-12 19:04:17 +02001236 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001237 value |= SOR_PLL2_SEQ_PLLCAPPD;
1238 value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
Thierry Reding880cee02017-10-12 19:04:17 +02001239 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001240
1241 usleep_range(20, 100);
1242
1243 return 0;
1244}
1245
Thierry Reding6fad8f62014-11-28 15:41:34 +01001246static int tegra_sor_crc_wait(struct tegra_sor *sor, unsigned long timeout)
1247{
1248 u32 value;
1249
1250 timeout = jiffies + msecs_to_jiffies(timeout);
1251
1252 while (time_before(jiffies, timeout)) {
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001253 value = tegra_sor_readl(sor, SOR_CRCA);
1254 if (value & SOR_CRCA_VALID)
Thierry Reding6fad8f62014-11-28 15:41:34 +01001255 return 0;
1256
1257 usleep_range(100, 200);
1258 }
1259
1260 return -ETIMEDOUT;
1261}
1262
Thierry Reding530239a2015-08-06 11:04:54 +02001263static int tegra_sor_show_crc(struct seq_file *s, void *data)
Thierry Reding6fad8f62014-11-28 15:41:34 +01001264{
Thierry Reding530239a2015-08-06 11:04:54 +02001265 struct drm_info_node *node = s->private;
1266 struct tegra_sor *sor = node->info_ent->data;
Thierry Reding850bab42015-07-29 17:58:41 +02001267 struct drm_crtc *crtc = sor->output.encoder.crtc;
1268 struct drm_device *drm = node->minor->dev;
Thierry Reding530239a2015-08-06 11:04:54 +02001269 int err = 0;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001270 u32 value;
1271
Thierry Reding850bab42015-07-29 17:58:41 +02001272 drm_modeset_lock_all(drm);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001273
Thierry Reding850bab42015-07-29 17:58:41 +02001274 if (!crtc || !crtc->state->active) {
1275 err = -EBUSY;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001276 goto unlock;
1277 }
1278
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001279 value = tegra_sor_readl(sor, SOR_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001280 value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001281 tegra_sor_writel(sor, value, SOR_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001282
1283 value = tegra_sor_readl(sor, SOR_CRC_CNTRL);
1284 value |= SOR_CRC_CNTRL_ENABLE;
1285 tegra_sor_writel(sor, value, SOR_CRC_CNTRL);
1286
1287 value = tegra_sor_readl(sor, SOR_TEST);
1288 value &= ~SOR_TEST_CRC_POST_SERIALIZE;
1289 tegra_sor_writel(sor, value, SOR_TEST);
1290
1291 err = tegra_sor_crc_wait(sor, 100);
1292 if (err < 0)
1293 goto unlock;
1294
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001295 tegra_sor_writel(sor, SOR_CRCA_RESET, SOR_CRCA);
1296 value = tegra_sor_readl(sor, SOR_CRCB);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001297
Thierry Reding530239a2015-08-06 11:04:54 +02001298 seq_printf(s, "%08x\n", value);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001299
1300unlock:
Thierry Reding850bab42015-07-29 17:58:41 +02001301 drm_modeset_unlock_all(drm);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001302 return err;
1303}
1304
Thierry Reding062f5b2c2017-11-10 12:21:51 +01001305#define DEBUGFS_REG32(_name) { .name = #_name, .offset = _name }
1306
1307static const struct debugfs_reg32 tegra_sor_regs[] = {
1308 DEBUGFS_REG32(SOR_CTXSW),
1309 DEBUGFS_REG32(SOR_SUPER_STATE0),
1310 DEBUGFS_REG32(SOR_SUPER_STATE1),
1311 DEBUGFS_REG32(SOR_STATE0),
1312 DEBUGFS_REG32(SOR_STATE1),
1313 DEBUGFS_REG32(SOR_HEAD_STATE0(0)),
1314 DEBUGFS_REG32(SOR_HEAD_STATE0(1)),
1315 DEBUGFS_REG32(SOR_HEAD_STATE1(0)),
1316 DEBUGFS_REG32(SOR_HEAD_STATE1(1)),
1317 DEBUGFS_REG32(SOR_HEAD_STATE2(0)),
1318 DEBUGFS_REG32(SOR_HEAD_STATE2(1)),
1319 DEBUGFS_REG32(SOR_HEAD_STATE3(0)),
1320 DEBUGFS_REG32(SOR_HEAD_STATE3(1)),
1321 DEBUGFS_REG32(SOR_HEAD_STATE4(0)),
1322 DEBUGFS_REG32(SOR_HEAD_STATE4(1)),
1323 DEBUGFS_REG32(SOR_HEAD_STATE5(0)),
1324 DEBUGFS_REG32(SOR_HEAD_STATE5(1)),
1325 DEBUGFS_REG32(SOR_CRC_CNTRL),
1326 DEBUGFS_REG32(SOR_DP_DEBUG_MVID),
1327 DEBUGFS_REG32(SOR_CLK_CNTRL),
1328 DEBUGFS_REG32(SOR_CAP),
1329 DEBUGFS_REG32(SOR_PWR),
1330 DEBUGFS_REG32(SOR_TEST),
1331 DEBUGFS_REG32(SOR_PLL0),
1332 DEBUGFS_REG32(SOR_PLL1),
1333 DEBUGFS_REG32(SOR_PLL2),
1334 DEBUGFS_REG32(SOR_PLL3),
1335 DEBUGFS_REG32(SOR_CSTM),
1336 DEBUGFS_REG32(SOR_LVDS),
1337 DEBUGFS_REG32(SOR_CRCA),
1338 DEBUGFS_REG32(SOR_CRCB),
1339 DEBUGFS_REG32(SOR_BLANK),
1340 DEBUGFS_REG32(SOR_SEQ_CTL),
1341 DEBUGFS_REG32(SOR_LANE_SEQ_CTL),
1342 DEBUGFS_REG32(SOR_SEQ_INST(0)),
1343 DEBUGFS_REG32(SOR_SEQ_INST(1)),
1344 DEBUGFS_REG32(SOR_SEQ_INST(2)),
1345 DEBUGFS_REG32(SOR_SEQ_INST(3)),
1346 DEBUGFS_REG32(SOR_SEQ_INST(4)),
1347 DEBUGFS_REG32(SOR_SEQ_INST(5)),
1348 DEBUGFS_REG32(SOR_SEQ_INST(6)),
1349 DEBUGFS_REG32(SOR_SEQ_INST(7)),
1350 DEBUGFS_REG32(SOR_SEQ_INST(8)),
1351 DEBUGFS_REG32(SOR_SEQ_INST(9)),
1352 DEBUGFS_REG32(SOR_SEQ_INST(10)),
1353 DEBUGFS_REG32(SOR_SEQ_INST(11)),
1354 DEBUGFS_REG32(SOR_SEQ_INST(12)),
1355 DEBUGFS_REG32(SOR_SEQ_INST(13)),
1356 DEBUGFS_REG32(SOR_SEQ_INST(14)),
1357 DEBUGFS_REG32(SOR_SEQ_INST(15)),
1358 DEBUGFS_REG32(SOR_PWM_DIV),
1359 DEBUGFS_REG32(SOR_PWM_CTL),
1360 DEBUGFS_REG32(SOR_VCRC_A0),
1361 DEBUGFS_REG32(SOR_VCRC_A1),
1362 DEBUGFS_REG32(SOR_VCRC_B0),
1363 DEBUGFS_REG32(SOR_VCRC_B1),
1364 DEBUGFS_REG32(SOR_CCRC_A0),
1365 DEBUGFS_REG32(SOR_CCRC_A1),
1366 DEBUGFS_REG32(SOR_CCRC_B0),
1367 DEBUGFS_REG32(SOR_CCRC_B1),
1368 DEBUGFS_REG32(SOR_EDATA_A0),
1369 DEBUGFS_REG32(SOR_EDATA_A1),
1370 DEBUGFS_REG32(SOR_EDATA_B0),
1371 DEBUGFS_REG32(SOR_EDATA_B1),
1372 DEBUGFS_REG32(SOR_COUNT_A0),
1373 DEBUGFS_REG32(SOR_COUNT_A1),
1374 DEBUGFS_REG32(SOR_COUNT_B0),
1375 DEBUGFS_REG32(SOR_COUNT_B1),
1376 DEBUGFS_REG32(SOR_DEBUG_A0),
1377 DEBUGFS_REG32(SOR_DEBUG_A1),
1378 DEBUGFS_REG32(SOR_DEBUG_B0),
1379 DEBUGFS_REG32(SOR_DEBUG_B1),
1380 DEBUGFS_REG32(SOR_TRIG),
1381 DEBUGFS_REG32(SOR_MSCHECK),
1382 DEBUGFS_REG32(SOR_XBAR_CTRL),
1383 DEBUGFS_REG32(SOR_XBAR_POL),
1384 DEBUGFS_REG32(SOR_DP_LINKCTL0),
1385 DEBUGFS_REG32(SOR_DP_LINKCTL1),
1386 DEBUGFS_REG32(SOR_LANE_DRIVE_CURRENT0),
1387 DEBUGFS_REG32(SOR_LANE_DRIVE_CURRENT1),
1388 DEBUGFS_REG32(SOR_LANE4_DRIVE_CURRENT0),
1389 DEBUGFS_REG32(SOR_LANE4_DRIVE_CURRENT1),
1390 DEBUGFS_REG32(SOR_LANE_PREEMPHASIS0),
1391 DEBUGFS_REG32(SOR_LANE_PREEMPHASIS1),
1392 DEBUGFS_REG32(SOR_LANE4_PREEMPHASIS0),
1393 DEBUGFS_REG32(SOR_LANE4_PREEMPHASIS1),
1394 DEBUGFS_REG32(SOR_LANE_POSTCURSOR0),
1395 DEBUGFS_REG32(SOR_LANE_POSTCURSOR1),
1396 DEBUGFS_REG32(SOR_DP_CONFIG0),
1397 DEBUGFS_REG32(SOR_DP_CONFIG1),
1398 DEBUGFS_REG32(SOR_DP_MN0),
1399 DEBUGFS_REG32(SOR_DP_MN1),
1400 DEBUGFS_REG32(SOR_DP_PADCTL0),
1401 DEBUGFS_REG32(SOR_DP_PADCTL1),
Thierry Redingc57997b2017-10-12 19:12:57 +02001402 DEBUGFS_REG32(SOR_DP_PADCTL2),
Thierry Reding062f5b2c2017-11-10 12:21:51 +01001403 DEBUGFS_REG32(SOR_DP_DEBUG0),
1404 DEBUGFS_REG32(SOR_DP_DEBUG1),
1405 DEBUGFS_REG32(SOR_DP_SPARE0),
1406 DEBUGFS_REG32(SOR_DP_SPARE1),
1407 DEBUGFS_REG32(SOR_DP_AUDIO_CTRL),
1408 DEBUGFS_REG32(SOR_DP_AUDIO_HBLANK_SYMBOLS),
1409 DEBUGFS_REG32(SOR_DP_AUDIO_VBLANK_SYMBOLS),
1410 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_HEADER),
1411 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK0),
1412 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK1),
1413 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK2),
1414 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK3),
1415 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK4),
1416 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK5),
1417 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK6),
1418 DEBUGFS_REG32(SOR_DP_TPG),
1419 DEBUGFS_REG32(SOR_DP_TPG_CONFIG),
1420 DEBUGFS_REG32(SOR_DP_LQ_CSTM0),
1421 DEBUGFS_REG32(SOR_DP_LQ_CSTM1),
1422 DEBUGFS_REG32(SOR_DP_LQ_CSTM2),
1423};
1424
Thierry Redingdab16332015-01-26 16:04:08 +01001425static int tegra_sor_show_regs(struct seq_file *s, void *data)
1426{
1427 struct drm_info_node *node = s->private;
1428 struct tegra_sor *sor = node->info_ent->data;
Thierry Reding850bab42015-07-29 17:58:41 +02001429 struct drm_crtc *crtc = sor->output.encoder.crtc;
1430 struct drm_device *drm = node->minor->dev;
Thierry Reding062f5b2c2017-11-10 12:21:51 +01001431 unsigned int i;
Thierry Reding850bab42015-07-29 17:58:41 +02001432 int err = 0;
1433
1434 drm_modeset_lock_all(drm);
1435
1436 if (!crtc || !crtc->state->active) {
1437 err = -EBUSY;
1438 goto unlock;
1439 }
Thierry Redingdab16332015-01-26 16:04:08 +01001440
Thierry Reding062f5b2c2017-11-10 12:21:51 +01001441 for (i = 0; i < ARRAY_SIZE(tegra_sor_regs); i++) {
1442 unsigned int offset = tegra_sor_regs[i].offset;
Thierry Redingdab16332015-01-26 16:04:08 +01001443
Thierry Reding062f5b2c2017-11-10 12:21:51 +01001444 seq_printf(s, "%-38s %#05x %08x\n", tegra_sor_regs[i].name,
1445 offset, tegra_sor_readl(sor, offset));
1446 }
Thierry Redingdab16332015-01-26 16:04:08 +01001447
Thierry Reding850bab42015-07-29 17:58:41 +02001448unlock:
1449 drm_modeset_unlock_all(drm);
1450 return err;
Thierry Redingdab16332015-01-26 16:04:08 +01001451}
1452
1453static const struct drm_info_list debugfs_files[] = {
Thierry Reding530239a2015-08-06 11:04:54 +02001454 { "crc", tegra_sor_show_crc, 0, NULL },
Thierry Redingdab16332015-01-26 16:04:08 +01001455 { "regs", tegra_sor_show_regs, 0, NULL },
1456};
1457
Thierry Reding5b8e0432017-11-08 13:20:01 +01001458static int tegra_sor_late_register(struct drm_connector *connector)
Thierry Reding6fad8f62014-11-28 15:41:34 +01001459{
Thierry Reding5b8e0432017-11-08 13:20:01 +01001460 struct tegra_output *output = connector_to_output(connector);
1461 unsigned int i, count = ARRAY_SIZE(debugfs_files);
1462 struct drm_minor *minor = connector->dev->primary;
1463 struct dentry *root = connector->debugfs_entry;
1464 struct tegra_sor *sor = to_sor(output);
Thierry Reding530239a2015-08-06 11:04:54 +02001465 int err;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001466
Thierry Redingdab16332015-01-26 16:04:08 +01001467 sor->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1468 GFP_KERNEL);
Thierry Reding5b8e0432017-11-08 13:20:01 +01001469 if (!sor->debugfs_files)
1470 return -ENOMEM;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001471
Thierry Reding5b8e0432017-11-08 13:20:01 +01001472 for (i = 0; i < count; i++)
Thierry Redingdab16332015-01-26 16:04:08 +01001473 sor->debugfs_files[i].data = sor;
1474
Thierry Reding5b8e0432017-11-08 13:20:01 +01001475 err = drm_debugfs_create_files(sor->debugfs_files, count, root, minor);
Thierry Redingdab16332015-01-26 16:04:08 +01001476 if (err < 0)
1477 goto free;
1478
Thierry Reding530239a2015-08-06 11:04:54 +02001479 return 0;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001480
Thierry Redingdab16332015-01-26 16:04:08 +01001481free:
1482 kfree(sor->debugfs_files);
1483 sor->debugfs_files = NULL;
Thierry Reding5b8e0432017-11-08 13:20:01 +01001484
Thierry Reding6fad8f62014-11-28 15:41:34 +01001485 return err;
1486}
1487
Thierry Reding5b8e0432017-11-08 13:20:01 +01001488static void tegra_sor_early_unregister(struct drm_connector *connector)
Thierry Reding6fad8f62014-11-28 15:41:34 +01001489{
Thierry Reding5b8e0432017-11-08 13:20:01 +01001490 struct tegra_output *output = connector_to_output(connector);
1491 unsigned int count = ARRAY_SIZE(debugfs_files);
1492 struct tegra_sor *sor = to_sor(output);
Thierry Redingd92e6002017-10-12 19:07:54 +02001493
Thierry Reding5b8e0432017-11-08 13:20:01 +01001494 drm_debugfs_remove_files(sor->debugfs_files, count,
1495 connector->dev->primary);
Thierry Redingdab16332015-01-26 16:04:08 +01001496 kfree(sor->debugfs_files);
Thierry Reding066d30f2015-07-03 14:16:30 +02001497 sor->debugfs_files = NULL;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001498}
1499
Thierry Redingc31efa72015-09-08 16:09:22 +02001500static void tegra_sor_connector_reset(struct drm_connector *connector)
1501{
1502 struct tegra_sor_state *state;
1503
1504 state = kzalloc(sizeof(*state), GFP_KERNEL);
1505 if (!state)
1506 return;
1507
1508 if (connector->state) {
1509 __drm_atomic_helper_connector_destroy_state(connector->state);
1510 kfree(connector->state);
1511 }
1512
1513 __drm_atomic_helper_connector_reset(connector, &state->base);
1514}
1515
Thierry Reding6fad8f62014-11-28 15:41:34 +01001516static enum drm_connector_status
1517tegra_sor_connector_detect(struct drm_connector *connector, bool force)
1518{
1519 struct tegra_output *output = connector_to_output(connector);
1520 struct tegra_sor *sor = to_sor(output);
1521
Thierry Reding9542c232015-07-08 13:39:09 +02001522 if (sor->aux)
1523 return drm_dp_aux_detect(sor->aux);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001524
Thierry Reding459cc2c2015-07-30 10:34:24 +02001525 return tegra_output_connector_detect(connector, force);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001526}
1527
Thierry Redingc31efa72015-09-08 16:09:22 +02001528static struct drm_connector_state *
1529tegra_sor_connector_duplicate_state(struct drm_connector *connector)
1530{
1531 struct tegra_sor_state *state = to_sor_state(connector->state);
1532 struct tegra_sor_state *copy;
1533
1534 copy = kmemdup(state, sizeof(*state), GFP_KERNEL);
1535 if (!copy)
1536 return NULL;
1537
1538 __drm_atomic_helper_connector_duplicate_state(connector, &copy->base);
1539
1540 return &copy->base;
1541}
1542
Thierry Reding6fad8f62014-11-28 15:41:34 +01001543static const struct drm_connector_funcs tegra_sor_connector_funcs = {
Thierry Redingc31efa72015-09-08 16:09:22 +02001544 .reset = tegra_sor_connector_reset,
Thierry Reding6fad8f62014-11-28 15:41:34 +01001545 .detect = tegra_sor_connector_detect,
1546 .fill_modes = drm_helper_probe_single_connector_modes,
1547 .destroy = tegra_output_connector_destroy,
Thierry Redingc31efa72015-09-08 16:09:22 +02001548 .atomic_duplicate_state = tegra_sor_connector_duplicate_state,
Thierry Reding4aa3df72014-11-24 16:27:13 +01001549 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
Thierry Reding5b8e0432017-11-08 13:20:01 +01001550 .late_register = tegra_sor_late_register,
1551 .early_unregister = tegra_sor_early_unregister,
Thierry Reding6fad8f62014-11-28 15:41:34 +01001552};
1553
1554static int tegra_sor_connector_get_modes(struct drm_connector *connector)
1555{
1556 struct tegra_output *output = connector_to_output(connector);
1557 struct tegra_sor *sor = to_sor(output);
1558 int err;
1559
Thierry Reding9542c232015-07-08 13:39:09 +02001560 if (sor->aux)
1561 drm_dp_aux_enable(sor->aux);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001562
1563 err = tegra_output_connector_get_modes(connector);
1564
Thierry Reding9542c232015-07-08 13:39:09 +02001565 if (sor->aux)
1566 drm_dp_aux_disable(sor->aux);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001567
1568 return err;
1569}
1570
1571static enum drm_mode_status
1572tegra_sor_connector_mode_valid(struct drm_connector *connector,
1573 struct drm_display_mode *mode)
1574{
1575 return MODE_OK;
1576}
1577
1578static const struct drm_connector_helper_funcs tegra_sor_connector_helper_funcs = {
1579 .get_modes = tegra_sor_connector_get_modes,
1580 .mode_valid = tegra_sor_connector_mode_valid,
Thierry Reding6fad8f62014-11-28 15:41:34 +01001581};
1582
1583static const struct drm_encoder_funcs tegra_sor_encoder_funcs = {
1584 .destroy = tegra_output_encoder_destroy,
1585};
1586
Thierry Reding850bab42015-07-29 17:58:41 +02001587static void tegra_sor_edp_disable(struct drm_encoder *encoder)
Thierry Reding6fad8f62014-11-28 15:41:34 +01001588{
Thierry Reding850bab42015-07-29 17:58:41 +02001589 struct tegra_output *output = encoder_to_output(encoder);
1590 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
1591 struct tegra_sor *sor = to_sor(output);
1592 u32 value;
1593 int err;
1594
1595 if (output->panel)
1596 drm_panel_disable(output->panel);
1597
1598 err = tegra_sor_detach(sor);
1599 if (err < 0)
1600 dev_err(sor->dev, "failed to detach SOR: %d\n", err);
1601
1602 tegra_sor_writel(sor, 0, SOR_STATE1);
1603 tegra_sor_update(sor);
1604
1605 /*
1606 * The following accesses registers of the display controller, so make
1607 * sure it's only executed when the output is attached to one.
1608 */
1609 if (dc) {
1610 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
Thierry Redingc57997b2017-10-12 19:12:57 +02001611 value &= ~SOR_ENABLE(0);
Thierry Reding850bab42015-07-29 17:58:41 +02001612 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
1613
1614 tegra_dc_commit(dc);
1615 }
1616
1617 err = tegra_sor_power_down(sor);
1618 if (err < 0)
1619 dev_err(sor->dev, "failed to power down SOR: %d\n", err);
1620
Thierry Reding9542c232015-07-08 13:39:09 +02001621 if (sor->aux) {
1622 err = drm_dp_aux_disable(sor->aux);
Thierry Reding850bab42015-07-29 17:58:41 +02001623 if (err < 0)
1624 dev_err(sor->dev, "failed to disable DP: %d\n", err);
1625 }
1626
Thierry Redingc57997b2017-10-12 19:12:57 +02001627 err = tegra_io_pad_power_disable(sor->pad);
Thierry Reding850bab42015-07-29 17:58:41 +02001628 if (err < 0)
Thierry Redingc57997b2017-10-12 19:12:57 +02001629 dev_err(sor->dev, "failed to power off I/O pad: %d\n", err);
Thierry Reding850bab42015-07-29 17:58:41 +02001630
1631 if (output->panel)
1632 drm_panel_unprepare(output->panel);
1633
Thierry Redingaaff8bd2015-08-07 16:04:54 +02001634 pm_runtime_put(sor->dev);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001635}
1636
Thierry Reding459cc2c2015-07-30 10:34:24 +02001637#if 0
1638static int calc_h_ref_to_sync(const struct drm_display_mode *mode,
1639 unsigned int *value)
1640{
1641 unsigned int hfp, hsw, hbp, a = 0, b;
1642
1643 hfp = mode->hsync_start - mode->hdisplay;
1644 hsw = mode->hsync_end - mode->hsync_start;
1645 hbp = mode->htotal - mode->hsync_end;
1646
1647 pr_info("hfp: %u, hsw: %u, hbp: %u\n", hfp, hsw, hbp);
1648
1649 b = hfp - 1;
1650
1651 pr_info("a: %u, b: %u\n", a, b);
1652 pr_info("a + hsw + hbp = %u\n", a + hsw + hbp);
1653
1654 if (a + hsw + hbp <= 11) {
1655 a = 1 + 11 - hsw - hbp;
1656 pr_info("a: %u\n", a);
1657 }
1658
1659 if (a > b)
1660 return -EINVAL;
1661
1662 if (hsw < 1)
1663 return -EINVAL;
1664
1665 if (mode->hdisplay < 16)
1666 return -EINVAL;
1667
1668 if (value) {
1669 if (b > a && a % 2)
1670 *value = a + 1;
1671 else
1672 *value = a;
1673 }
1674
1675 return 0;
1676}
1677#endif
1678
Thierry Reding850bab42015-07-29 17:58:41 +02001679static void tegra_sor_edp_enable(struct drm_encoder *encoder)
Thierry Reding6fad8f62014-11-28 15:41:34 +01001680{
Thierry Reding850bab42015-07-29 17:58:41 +02001681 struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001682 struct tegra_output *output = encoder_to_output(encoder);
1683 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001684 struct tegra_sor *sor = to_sor(output);
Thierry Reding34fa1832014-06-05 16:31:10 +02001685 struct tegra_sor_config config;
Thierry Redingc31efa72015-09-08 16:09:22 +02001686 struct tegra_sor_state *state;
Thierry Reding34fa1832014-06-05 16:31:10 +02001687 struct drm_dp_link link;
Thierry Reding01b9bea2015-11-11 17:15:29 +01001688 u8 rate, lanes;
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001689 unsigned int i;
Thierry Reding86f5c522014-03-26 11:13:16 +01001690 int err = 0;
Thierry Reding28fe2072015-01-26 16:02:48 +01001691 u32 value;
Thierry Reding86f5c522014-03-26 11:13:16 +01001692
Thierry Redingc31efa72015-09-08 16:09:22 +02001693 state = to_sor_state(output->connector.state);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001694
Thierry Redingaaff8bd2015-08-07 16:04:54 +02001695 pm_runtime_get_sync(sor->dev);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001696
Thierry Reding6fad8f62014-11-28 15:41:34 +01001697 if (output->panel)
1698 drm_panel_prepare(output->panel);
1699
Thierry Reding01b9bea2015-11-11 17:15:29 +01001700 err = drm_dp_aux_enable(sor->aux);
1701 if (err < 0)
1702 dev_err(sor->dev, "failed to enable DP: %d\n", err);
Thierry Reding34fa1832014-06-05 16:31:10 +02001703
Thierry Reding01b9bea2015-11-11 17:15:29 +01001704 err = drm_dp_link_probe(sor->aux, &link);
1705 if (err < 0) {
1706 dev_err(sor->dev, "failed to probe eDP link: %d\n", err);
1707 return;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001708 }
1709
Thierry Reding25bb2ce2015-08-03 14:23:29 +02001710 /* switch to safe parent clock */
1711 err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001712 if (err < 0)
1713 dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
1714
Thierry Reding34fa1832014-06-05 16:31:10 +02001715 memset(&config, 0, sizeof(config));
Thierry Redingc31efa72015-09-08 16:09:22 +02001716 config.bits_per_pixel = state->bpc * 3;
Thierry Reding34fa1832014-06-05 16:31:10 +02001717
Thierry Redinga1983592015-07-21 16:46:52 +02001718 err = tegra_sor_compute_config(sor, mode, &config, &link);
Thierry Reding34fa1832014-06-05 16:31:10 +02001719 if (err < 0)
Thierry Redinga1983592015-07-21 16:46:52 +02001720 dev_err(sor->dev, "failed to compute configuration: %d\n", err);
Thierry Reding34fa1832014-06-05 16:31:10 +02001721
Thierry Reding6b6b6042013-11-15 16:06:05 +01001722 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
1723 value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
1724 value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
1725 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
1726
Thierry Reding880cee02017-10-12 19:04:17 +02001727 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001728 value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
Thierry Reding880cee02017-10-12 19:04:17 +02001729 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001730 usleep_range(20, 100);
1731
Thierry Reding880cee02017-10-12 19:04:17 +02001732 value = tegra_sor_readl(sor, sor->soc->regs->pll3);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001733 value |= SOR_PLL3_PLL_VDD_MODE_3V3;
Thierry Reding880cee02017-10-12 19:04:17 +02001734 tegra_sor_writel(sor, value, sor->soc->regs->pll3);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001735
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001736 value = SOR_PLL0_ICHPMP(0xf) | SOR_PLL0_VCOCAP_RST |
1737 SOR_PLL0_PLLREG_LEVEL_V45 | SOR_PLL0_RESISTOR_EXT;
Thierry Reding880cee02017-10-12 19:04:17 +02001738 tegra_sor_writel(sor, value, sor->soc->regs->pll0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001739
Thierry Reding880cee02017-10-12 19:04:17 +02001740 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001741 value |= SOR_PLL2_SEQ_PLLCAPPD;
1742 value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1743 value |= SOR_PLL2_LVDS_ENABLE;
Thierry Reding880cee02017-10-12 19:04:17 +02001744 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001745
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001746 value = SOR_PLL1_TERM_COMPOUT | SOR_PLL1_TMDS_TERM;
Thierry Reding880cee02017-10-12 19:04:17 +02001747 tegra_sor_writel(sor, value, sor->soc->regs->pll1);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001748
1749 while (true) {
Thierry Reding880cee02017-10-12 19:04:17 +02001750 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001751 if ((value & SOR_PLL2_SEQ_PLLCAPPD_ENFORCE) == 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001752 break;
1753
1754 usleep_range(250, 1000);
1755 }
1756
Thierry Reding880cee02017-10-12 19:04:17 +02001757 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001758 value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
1759 value &= ~SOR_PLL2_PORT_POWERDOWN;
Thierry Reding880cee02017-10-12 19:04:17 +02001760 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001761
1762 /*
1763 * power up
1764 */
1765
1766 /* set safe link bandwidth (1.62 Gbps) */
1767 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
1768 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
1769 value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G1_62;
1770 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
1771
1772 /* step 1 */
Thierry Reding880cee02017-10-12 19:04:17 +02001773 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001774 value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE | SOR_PLL2_PORT_POWERDOWN |
1775 SOR_PLL2_BANDGAP_POWERDOWN;
Thierry Reding880cee02017-10-12 19:04:17 +02001776 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001777
Thierry Reding880cee02017-10-12 19:04:17 +02001778 value = tegra_sor_readl(sor, sor->soc->regs->pll0);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001779 value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
Thierry Reding880cee02017-10-12 19:04:17 +02001780 tegra_sor_writel(sor, value, sor->soc->regs->pll0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001781
Thierry Reding880cee02017-10-12 19:04:17 +02001782 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001783 value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
Thierry Reding880cee02017-10-12 19:04:17 +02001784 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001785
1786 /* step 2 */
Thierry Redingc57997b2017-10-12 19:12:57 +02001787 err = tegra_io_pad_power_enable(sor->pad);
Thierry Reding850bab42015-07-29 17:58:41 +02001788 if (err < 0)
Thierry Redingc57997b2017-10-12 19:12:57 +02001789 dev_err(sor->dev, "failed to power on I/O pad: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001790
1791 usleep_range(5, 100);
1792
1793 /* step 3 */
Thierry Reding880cee02017-10-12 19:04:17 +02001794 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001795 value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
Thierry Reding880cee02017-10-12 19:04:17 +02001796 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001797
1798 usleep_range(20, 100);
1799
1800 /* step 4 */
Thierry Reding880cee02017-10-12 19:04:17 +02001801 value = tegra_sor_readl(sor, sor->soc->regs->pll0);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001802 value &= ~SOR_PLL0_VCOPD;
1803 value &= ~SOR_PLL0_PWR;
Thierry Reding880cee02017-10-12 19:04:17 +02001804 tegra_sor_writel(sor, value, sor->soc->regs->pll0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001805
Thierry Reding880cee02017-10-12 19:04:17 +02001806 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001807 value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
Thierry Reding880cee02017-10-12 19:04:17 +02001808 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001809
1810 usleep_range(200, 1000);
1811
1812 /* step 5 */
Thierry Reding880cee02017-10-12 19:04:17 +02001813 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001814 value &= ~SOR_PLL2_PORT_POWERDOWN;
Thierry Reding880cee02017-10-12 19:04:17 +02001815 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001816
Thierry Reding30b49432015-08-03 15:50:32 +02001817 /* XXX not in TRM */
1818 for (value = 0, i = 0; i < 5; i++)
Thierry Reding6d6c8152019-01-25 11:00:58 +01001819 value |= SOR_XBAR_CTRL_LINK0_XSEL(i, sor->xbar_cfg[i]) |
Thierry Reding30b49432015-08-03 15:50:32 +02001820 SOR_XBAR_CTRL_LINK1_XSEL(i, i);
1821
1822 tegra_sor_writel(sor, 0x00000000, SOR_XBAR_POL);
1823 tegra_sor_writel(sor, value, SOR_XBAR_CTRL);
1824
Thierry Reding25bb2ce2015-08-03 14:23:29 +02001825 /* switch to DP parent clock */
1826 err = tegra_sor_set_parent_clock(sor, sor->clk_dp);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001827 if (err < 0)
Thierry Reding25bb2ce2015-08-03 14:23:29 +02001828 dev_err(sor->dev, "failed to set parent clock: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001829
Thierry Reding899451b2014-06-05 16:19:48 +02001830 /* power DP lanes */
Thierry Reding880cee02017-10-12 19:04:17 +02001831 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding899451b2014-06-05 16:19:48 +02001832
1833 if (link.num_lanes <= 2)
1834 value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2);
1835 else
1836 value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2;
1837
1838 if (link.num_lanes <= 1)
1839 value &= ~SOR_DP_PADCTL_PD_TXD_1;
1840 else
1841 value |= SOR_DP_PADCTL_PD_TXD_1;
1842
1843 if (link.num_lanes == 0)
1844 value &= ~SOR_DP_PADCTL_PD_TXD_0;
1845 else
1846 value |= SOR_DP_PADCTL_PD_TXD_0;
1847
Thierry Reding880cee02017-10-12 19:04:17 +02001848 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001849
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001850 value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001851 value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
Thierry Reding0c90a182014-06-05 16:29:46 +02001852 value |= SOR_DP_LINKCTL_LANE_COUNT(link.num_lanes);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001853 tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001854
1855 /* start lane sequencer */
1856 value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
1857 SOR_LANE_SEQ_CTL_POWER_STATE_UP;
1858 tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
1859
1860 while (true) {
1861 value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
1862 if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
1863 break;
1864
1865 usleep_range(250, 1000);
1866 }
1867
Thierry Redinga4263fe2014-06-05 16:16:23 +02001868 /* set link bandwidth */
Thierry Reding6b6b6042013-11-15 16:06:05 +01001869 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
1870 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
Thierry Redinga4263fe2014-06-05 16:16:23 +02001871 value |= drm_dp_link_rate_to_bw_code(link.rate) << 2;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001872 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
1873
Thierry Reding402f6bc2015-07-21 16:48:19 +02001874 tegra_sor_apply_config(sor, &config);
1875
1876 /* enable link */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001877 value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001878 value |= SOR_DP_LINKCTL_ENABLE;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001879 value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001880 tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001881
1882 for (i = 0, value = 0; i < 4; i++) {
1883 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
1884 SOR_DP_TPG_SCRAMBLER_GALIOS |
1885 SOR_DP_TPG_PATTERN_NONE;
1886 value = (value << 8) | lane;
1887 }
1888
1889 tegra_sor_writel(sor, value, SOR_DP_TPG);
1890
Thierry Reding6b6b6042013-11-15 16:06:05 +01001891 /* enable pad calibration logic */
Thierry Reding880cee02017-10-12 19:04:17 +02001892 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001893 value |= SOR_DP_PADCTL_PAD_CAL_PD;
Thierry Reding880cee02017-10-12 19:04:17 +02001894 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001895
Thierry Reding01b9bea2015-11-11 17:15:29 +01001896 err = drm_dp_link_probe(sor->aux, &link);
1897 if (err < 0)
1898 dev_err(sor->dev, "failed to probe eDP link: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001899
Thierry Reding01b9bea2015-11-11 17:15:29 +01001900 err = drm_dp_link_power_up(sor->aux, &link);
1901 if (err < 0)
1902 dev_err(sor->dev, "failed to power up eDP link: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001903
Thierry Reding01b9bea2015-11-11 17:15:29 +01001904 err = drm_dp_link_configure(sor->aux, &link);
1905 if (err < 0)
1906 dev_err(sor->dev, "failed to configure eDP link: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001907
Thierry Reding01b9bea2015-11-11 17:15:29 +01001908 rate = drm_dp_link_rate_to_bw_code(link.rate);
1909 lanes = link.num_lanes;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001910
Thierry Reding01b9bea2015-11-11 17:15:29 +01001911 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
1912 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
1913 value |= SOR_CLK_CNTRL_DP_LINK_SPEED(rate);
1914 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001915
Thierry Reding01b9bea2015-11-11 17:15:29 +01001916 value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
1917 value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
1918 value |= SOR_DP_LINKCTL_LANE_COUNT(lanes);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001919
Thierry Reding01b9bea2015-11-11 17:15:29 +01001920 if (link.capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
1921 value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001922
Thierry Reding01b9bea2015-11-11 17:15:29 +01001923 tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001924
Thierry Reding01b9bea2015-11-11 17:15:29 +01001925 /* disable training pattern generator */
Thierry Reding6b6b6042013-11-15 16:06:05 +01001926
Thierry Reding01b9bea2015-11-11 17:15:29 +01001927 for (i = 0; i < link.num_lanes; i++) {
1928 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
1929 SOR_DP_TPG_SCRAMBLER_GALIOS |
1930 SOR_DP_TPG_PATTERN_NONE;
1931 value = (value << 8) | lane;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001932 }
1933
Thierry Reding01b9bea2015-11-11 17:15:29 +01001934 tegra_sor_writel(sor, value, SOR_DP_TPG);
1935
1936 err = tegra_sor_dp_train_fast(sor, &link);
1937 if (err < 0)
1938 dev_err(sor->dev, "DP fast link training failed: %d\n", err);
1939
1940 dev_dbg(sor->dev, "fast link training succeeded\n");
1941
Thierry Reding6b6b6042013-11-15 16:06:05 +01001942 err = tegra_sor_power_up(sor, 250);
Thierry Reding850bab42015-07-29 17:58:41 +02001943 if (err < 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001944 dev_err(sor->dev, "failed to power up SOR: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001945
Thierry Reding6b6b6042013-11-15 16:06:05 +01001946 /* CSTM (LVDS, link A/B, upper) */
Stéphane Marchesin143b1df2014-05-22 20:32:47 -07001947 value = SOR_CSTM_LVDS | SOR_CSTM_LINK_ACT_A | SOR_CSTM_LINK_ACT_B |
Thierry Reding6b6b6042013-11-15 16:06:05 +01001948 SOR_CSTM_UPPER;
1949 tegra_sor_writel(sor, value, SOR_CSTM);
1950
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001951 /* use DP-A protocol */
1952 value = tegra_sor_readl(sor, SOR_STATE1);
1953 value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
1954 value |= SOR_STATE_ASY_PROTOCOL_DP_A;
1955 tegra_sor_writel(sor, value, SOR_STATE1);
1956
Thierry Redingc31efa72015-09-08 16:09:22 +02001957 tegra_sor_mode_set(sor, mode, state);
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001958
Thierry Reding6b6b6042013-11-15 16:06:05 +01001959 /* PWM setup */
1960 err = tegra_sor_setup_pwm(sor, 250);
Thierry Reding850bab42015-07-29 17:58:41 +02001961 if (err < 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001962 dev_err(sor->dev, "failed to setup PWM: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001963
Thierry Reding666cb872014-12-08 16:32:47 +01001964 tegra_sor_update(sor);
1965
Thierry Reding6b6b6042013-11-15 16:06:05 +01001966 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
Thierry Redingc57997b2017-10-12 19:12:57 +02001967 value |= SOR_ENABLE(0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001968 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
1969
Thierry Reding666cb872014-12-08 16:32:47 +01001970 tegra_dc_commit(dc);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001971
1972 err = tegra_sor_attach(sor);
Thierry Reding850bab42015-07-29 17:58:41 +02001973 if (err < 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001974 dev_err(sor->dev, "failed to attach SOR: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001975
1976 err = tegra_sor_wakeup(sor);
Thierry Reding850bab42015-07-29 17:58:41 +02001977 if (err < 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001978 dev_err(sor->dev, "failed to enable DC: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001979
Thierry Reding6fad8f62014-11-28 15:41:34 +01001980 if (output->panel)
1981 drm_panel_enable(output->panel);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001982}
1983
Thierry Reding82f15112014-12-08 17:26:46 +01001984static int
1985tegra_sor_encoder_atomic_check(struct drm_encoder *encoder,
1986 struct drm_crtc_state *crtc_state,
1987 struct drm_connector_state *conn_state)
1988{
1989 struct tegra_output *output = encoder_to_output(encoder);
Thierry Redingc31efa72015-09-08 16:09:22 +02001990 struct tegra_sor_state *state = to_sor_state(conn_state);
Thierry Reding82f15112014-12-08 17:26:46 +01001991 struct tegra_dc *dc = to_tegra_dc(conn_state->crtc);
1992 unsigned long pclk = crtc_state->mode.clock * 1000;
1993 struct tegra_sor *sor = to_sor(output);
Thierry Redingc31efa72015-09-08 16:09:22 +02001994 struct drm_display_info *info;
Thierry Reding82f15112014-12-08 17:26:46 +01001995 int err;
1996
Thierry Redingc31efa72015-09-08 16:09:22 +02001997 info = &output->connector.display_info;
1998
Thierry Reding36e90222017-10-12 19:14:21 +02001999 /*
2000 * For HBR2 modes, the SOR brick needs to use the x20 multiplier, so
2001 * the pixel clock must be corrected accordingly.
2002 */
2003 if (pclk >= 340000000) {
2004 state->link_speed = 20;
2005 state->pclk = pclk / 2;
2006 } else {
2007 state->link_speed = 10;
2008 state->pclk = pclk;
2009 }
2010
Thierry Reding82f15112014-12-08 17:26:46 +01002011 err = tegra_dc_state_setup_clock(dc, crtc_state, sor->clk_parent,
2012 pclk, 0);
2013 if (err < 0) {
2014 dev_err(output->dev, "failed to setup CRTC state: %d\n", err);
2015 return err;
2016 }
2017
Thierry Redingc31efa72015-09-08 16:09:22 +02002018 switch (info->bpc) {
2019 case 8:
2020 case 6:
2021 state->bpc = info->bpc;
2022 break;
2023
2024 default:
2025 DRM_DEBUG_KMS("%u bits-per-color not supported\n", info->bpc);
2026 state->bpc = 8;
2027 break;
2028 }
2029
Thierry Reding82f15112014-12-08 17:26:46 +01002030 return 0;
2031}
2032
Thierry Reding459cc2c2015-07-30 10:34:24 +02002033static const struct drm_encoder_helper_funcs tegra_sor_edp_helpers = {
Thierry Reding850bab42015-07-29 17:58:41 +02002034 .disable = tegra_sor_edp_disable,
2035 .enable = tegra_sor_edp_enable,
Thierry Reding82f15112014-12-08 17:26:46 +01002036 .atomic_check = tegra_sor_encoder_atomic_check,
Thierry Reding6b6b6042013-11-15 16:06:05 +01002037};
2038
Thierry Reding459cc2c2015-07-30 10:34:24 +02002039static inline u32 tegra_sor_hdmi_subpack(const u8 *ptr, size_t size)
2040{
2041 u32 value = 0;
2042 size_t i;
2043
2044 for (i = size; i > 0; i--)
2045 value = (value << 8) | ptr[i - 1];
2046
2047 return value;
2048}
2049
2050static void tegra_sor_hdmi_write_infopack(struct tegra_sor *sor,
2051 const void *data, size_t size)
2052{
2053 const u8 *ptr = data;
2054 unsigned long offset;
2055 size_t i, j;
2056 u32 value;
2057
2058 switch (ptr[0]) {
2059 case HDMI_INFOFRAME_TYPE_AVI:
2060 offset = SOR_HDMI_AVI_INFOFRAME_HEADER;
2061 break;
2062
2063 case HDMI_INFOFRAME_TYPE_AUDIO:
2064 offset = SOR_HDMI_AUDIO_INFOFRAME_HEADER;
2065 break;
2066
2067 case HDMI_INFOFRAME_TYPE_VENDOR:
2068 offset = SOR_HDMI_VSI_INFOFRAME_HEADER;
2069 break;
2070
2071 default:
2072 dev_err(sor->dev, "unsupported infoframe type: %02x\n",
2073 ptr[0]);
2074 return;
2075 }
2076
2077 value = INFOFRAME_HEADER_TYPE(ptr[0]) |
2078 INFOFRAME_HEADER_VERSION(ptr[1]) |
2079 INFOFRAME_HEADER_LEN(ptr[2]);
2080 tegra_sor_writel(sor, value, offset);
2081 offset++;
2082
2083 /*
2084 * Each subpack contains 7 bytes, divided into:
2085 * - subpack_low: bytes 0 - 3
2086 * - subpack_high: bytes 4 - 6 (with byte 7 padded to 0x00)
2087 */
2088 for (i = 3, j = 0; i < size; i += 7, j += 8) {
2089 size_t rem = size - i, num = min_t(size_t, rem, 4);
2090
2091 value = tegra_sor_hdmi_subpack(&ptr[i], num);
2092 tegra_sor_writel(sor, value, offset++);
2093
2094 num = min_t(size_t, rem - num, 3);
2095
2096 value = tegra_sor_hdmi_subpack(&ptr[i + 4], num);
2097 tegra_sor_writel(sor, value, offset++);
2098 }
2099}
2100
2101static int
2102tegra_sor_hdmi_setup_avi_infoframe(struct tegra_sor *sor,
2103 const struct drm_display_mode *mode)
2104{
2105 u8 buffer[HDMI_INFOFRAME_SIZE(AVI)];
2106 struct hdmi_avi_infoframe frame;
2107 u32 value;
2108 int err;
2109
2110 /* disable AVI infoframe */
2111 value = tegra_sor_readl(sor, SOR_HDMI_AVI_INFOFRAME_CTRL);
2112 value &= ~INFOFRAME_CTRL_SINGLE;
2113 value &= ~INFOFRAME_CTRL_OTHER;
2114 value &= ~INFOFRAME_CTRL_ENABLE;
2115 tegra_sor_writel(sor, value, SOR_HDMI_AVI_INFOFRAME_CTRL);
2116
Shashank Sharma0c1f5282017-07-13 21:03:07 +05302117 err = drm_hdmi_avi_infoframe_from_display_mode(&frame, mode, false);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002118 if (err < 0) {
2119 dev_err(sor->dev, "failed to setup AVI infoframe: %d\n", err);
2120 return err;
2121 }
2122
2123 err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
2124 if (err < 0) {
2125 dev_err(sor->dev, "failed to pack AVI infoframe: %d\n", err);
2126 return err;
2127 }
2128
2129 tegra_sor_hdmi_write_infopack(sor, buffer, err);
2130
2131 /* enable AVI infoframe */
2132 value = tegra_sor_readl(sor, SOR_HDMI_AVI_INFOFRAME_CTRL);
2133 value |= INFOFRAME_CTRL_CHECKSUM_ENABLE;
2134 value |= INFOFRAME_CTRL_ENABLE;
2135 tegra_sor_writel(sor, value, SOR_HDMI_AVI_INFOFRAME_CTRL);
2136
2137 return 0;
2138}
2139
Thierry Reding8e2988a72018-12-03 15:46:03 +01002140static void tegra_sor_write_eld(struct tegra_sor *sor)
2141{
2142 size_t length = drm_eld_size(sor->output.connector.eld), i;
2143
2144 for (i = 0; i < length; i++)
2145 tegra_sor_writel(sor, i << 8 | sor->output.connector.eld[i],
2146 SOR_AUDIO_HDA_ELD_BUFWR);
2147
2148 /*
2149 * The HDA codec will always report an ELD buffer size of 96 bytes and
2150 * the HDA codec driver will check that each byte read from the buffer
2151 * is valid. Therefore every byte must be written, even if no 96 bytes
2152 * were parsed from EDID.
2153 */
2154 for (i = length; i < 96; i++)
2155 tegra_sor_writel(sor, i << 8 | 0, SOR_AUDIO_HDA_ELD_BUFWR);
2156}
2157
2158static void tegra_sor_audio_prepare(struct tegra_sor *sor)
2159{
2160 u32 value;
2161
2162 tegra_sor_write_eld(sor);
2163
2164 value = SOR_AUDIO_HDA_PRESENSE_ELDV | SOR_AUDIO_HDA_PRESENSE_PD;
2165 tegra_sor_writel(sor, value, SOR_AUDIO_HDA_PRESENSE);
2166}
2167
2168static void tegra_sor_audio_unprepare(struct tegra_sor *sor)
2169{
2170 tegra_sor_writel(sor, 0, SOR_AUDIO_HDA_PRESENSE);
2171}
2172
2173static int tegra_sor_hdmi_enable_audio_infoframe(struct tegra_sor *sor)
2174{
2175 u8 buffer[HDMI_INFOFRAME_SIZE(AUDIO)];
2176 struct hdmi_audio_infoframe frame;
2177 u32 value;
2178 int err;
2179
2180 err = hdmi_audio_infoframe_init(&frame);
2181 if (err < 0) {
2182 dev_err(sor->dev, "failed to setup audio infoframe: %d\n", err);
2183 return err;
2184 }
2185
Thierry Redingfad7b802019-01-03 15:23:16 +01002186 frame.channels = sor->format.channels;
Thierry Reding8e2988a72018-12-03 15:46:03 +01002187
2188 err = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
2189 if (err < 0) {
2190 dev_err(sor->dev, "failed to pack audio infoframe: %d\n", err);
2191 return err;
2192 }
2193
2194 tegra_sor_hdmi_write_infopack(sor, buffer, err);
2195
2196 value = tegra_sor_readl(sor, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
2197 value |= INFOFRAME_CTRL_CHECKSUM_ENABLE;
2198 value |= INFOFRAME_CTRL_ENABLE;
2199 tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
2200
2201 return 0;
2202}
2203
2204static void tegra_sor_hdmi_audio_enable(struct tegra_sor *sor)
2205{
2206 u32 value;
2207
2208 value = tegra_sor_readl(sor, SOR_AUDIO_CNTRL);
2209
2210 /* select HDA audio input */
2211 value &= ~SOR_AUDIO_CNTRL_SOURCE_SELECT(SOURCE_SELECT_MASK);
2212 value |= SOR_AUDIO_CNTRL_SOURCE_SELECT(SOURCE_SELECT_HDA);
2213
2214 /* inject null samples */
Thierry Redingfad7b802019-01-03 15:23:16 +01002215 if (sor->format.channels != 2)
Thierry Reding8e2988a72018-12-03 15:46:03 +01002216 value &= ~SOR_AUDIO_CNTRL_INJECT_NULLSMPL;
2217 else
2218 value |= SOR_AUDIO_CNTRL_INJECT_NULLSMPL;
2219
2220 value |= SOR_AUDIO_CNTRL_AFIFO_FLUSH;
2221
2222 tegra_sor_writel(sor, value, SOR_AUDIO_CNTRL);
2223
2224 /* enable advertising HBR capability */
2225 tegra_sor_writel(sor, SOR_AUDIO_SPARE_HBR_ENABLE, SOR_AUDIO_SPARE);
2226
2227 tegra_sor_writel(sor, 0, SOR_HDMI_ACR_CTRL);
2228
2229 value = SOR_HDMI_SPARE_ACR_PRIORITY_HIGH |
2230 SOR_HDMI_SPARE_CTS_RESET(1) |
2231 SOR_HDMI_SPARE_HW_CTS_ENABLE;
2232 tegra_sor_writel(sor, value, SOR_HDMI_SPARE);
2233
2234 /* enable HW CTS */
2235 value = SOR_HDMI_ACR_SUBPACK_LOW_SB1(0);
2236 tegra_sor_writel(sor, value, SOR_HDMI_ACR_0441_SUBPACK_LOW);
2237
2238 /* allow packet to be sent */
2239 value = SOR_HDMI_ACR_SUBPACK_HIGH_ENABLE;
2240 tegra_sor_writel(sor, value, SOR_HDMI_ACR_0441_SUBPACK_HIGH);
2241
2242 /* reset N counter and enable lookup */
2243 value = SOR_HDMI_AUDIO_N_RESET | SOR_HDMI_AUDIO_N_LOOKUP;
2244 tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_N);
2245
Thierry Redingfad7b802019-01-03 15:23:16 +01002246 value = (24000 * 4096) / (128 * sor->format.sample_rate / 1000);
Thierry Reding8e2988a72018-12-03 15:46:03 +01002247 tegra_sor_writel(sor, value, SOR_AUDIO_AVAL_0320);
2248 tegra_sor_writel(sor, 4096, SOR_AUDIO_NVAL_0320);
2249
2250 tegra_sor_writel(sor, 20000, SOR_AUDIO_AVAL_0441);
2251 tegra_sor_writel(sor, 4704, SOR_AUDIO_NVAL_0441);
2252
2253 tegra_sor_writel(sor, 20000, SOR_AUDIO_AVAL_0882);
2254 tegra_sor_writel(sor, 9408, SOR_AUDIO_NVAL_0882);
2255
2256 tegra_sor_writel(sor, 20000, SOR_AUDIO_AVAL_1764);
2257 tegra_sor_writel(sor, 18816, SOR_AUDIO_NVAL_1764);
2258
Thierry Redingfad7b802019-01-03 15:23:16 +01002259 value = (24000 * 6144) / (128 * sor->format.sample_rate / 1000);
Thierry Reding8e2988a72018-12-03 15:46:03 +01002260 tegra_sor_writel(sor, value, SOR_AUDIO_AVAL_0480);
2261 tegra_sor_writel(sor, 6144, SOR_AUDIO_NVAL_0480);
2262
Thierry Redingfad7b802019-01-03 15:23:16 +01002263 value = (24000 * 12288) / (128 * sor->format.sample_rate / 1000);
Thierry Reding8e2988a72018-12-03 15:46:03 +01002264 tegra_sor_writel(sor, value, SOR_AUDIO_AVAL_0960);
2265 tegra_sor_writel(sor, 12288, SOR_AUDIO_NVAL_0960);
2266
Thierry Redingfad7b802019-01-03 15:23:16 +01002267 value = (24000 * 24576) / (128 * sor->format.sample_rate / 1000);
Thierry Reding8e2988a72018-12-03 15:46:03 +01002268 tegra_sor_writel(sor, value, SOR_AUDIO_AVAL_1920);
2269 tegra_sor_writel(sor, 24576, SOR_AUDIO_NVAL_1920);
2270
2271 value = tegra_sor_readl(sor, SOR_HDMI_AUDIO_N);
2272 value &= ~SOR_HDMI_AUDIO_N_RESET;
2273 tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_N);
2274
2275 tegra_sor_hdmi_enable_audio_infoframe(sor);
2276}
2277
Thierry Reding459cc2c2015-07-30 10:34:24 +02002278static void tegra_sor_hdmi_disable_audio_infoframe(struct tegra_sor *sor)
2279{
2280 u32 value;
2281
2282 value = tegra_sor_readl(sor, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
2283 value &= ~INFOFRAME_CTRL_ENABLE;
2284 tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
2285}
2286
Thierry Reding8e2988a72018-12-03 15:46:03 +01002287static void tegra_sor_hdmi_audio_disable(struct tegra_sor *sor)
2288{
2289 tegra_sor_hdmi_disable_audio_infoframe(sor);
2290}
2291
Thierry Reding459cc2c2015-07-30 10:34:24 +02002292static struct tegra_sor_hdmi_settings *
2293tegra_sor_hdmi_find_settings(struct tegra_sor *sor, unsigned long frequency)
2294{
2295 unsigned int i;
2296
2297 for (i = 0; i < sor->num_settings; i++)
2298 if (frequency <= sor->settings[i].frequency)
2299 return &sor->settings[i];
2300
2301 return NULL;
2302}
2303
Thierry Reding36e90222017-10-12 19:14:21 +02002304static void tegra_sor_hdmi_disable_scrambling(struct tegra_sor *sor)
2305{
2306 u32 value;
2307
2308 value = tegra_sor_readl(sor, SOR_HDMI2_CTRL);
2309 value &= ~SOR_HDMI2_CTRL_CLOCK_MODE_DIV_BY_4;
2310 value &= ~SOR_HDMI2_CTRL_SCRAMBLE;
2311 tegra_sor_writel(sor, value, SOR_HDMI2_CTRL);
2312}
2313
2314static void tegra_sor_hdmi_scdc_disable(struct tegra_sor *sor)
2315{
2316 struct i2c_adapter *ddc = sor->output.ddc;
2317
2318 drm_scdc_set_high_tmds_clock_ratio(ddc, false);
2319 drm_scdc_set_scrambling(ddc, false);
2320
2321 tegra_sor_hdmi_disable_scrambling(sor);
2322}
2323
2324static void tegra_sor_hdmi_scdc_stop(struct tegra_sor *sor)
2325{
2326 if (sor->scdc_enabled) {
2327 cancel_delayed_work_sync(&sor->scdc);
2328 tegra_sor_hdmi_scdc_disable(sor);
2329 }
2330}
2331
2332static void tegra_sor_hdmi_enable_scrambling(struct tegra_sor *sor)
2333{
2334 u32 value;
2335
2336 value = tegra_sor_readl(sor, SOR_HDMI2_CTRL);
2337 value |= SOR_HDMI2_CTRL_CLOCK_MODE_DIV_BY_4;
2338 value |= SOR_HDMI2_CTRL_SCRAMBLE;
2339 tegra_sor_writel(sor, value, SOR_HDMI2_CTRL);
2340}
2341
2342static void tegra_sor_hdmi_scdc_enable(struct tegra_sor *sor)
2343{
2344 struct i2c_adapter *ddc = sor->output.ddc;
2345
2346 drm_scdc_set_high_tmds_clock_ratio(ddc, true);
2347 drm_scdc_set_scrambling(ddc, true);
2348
2349 tegra_sor_hdmi_enable_scrambling(sor);
2350}
2351
2352static void tegra_sor_hdmi_scdc_work(struct work_struct *work)
2353{
2354 struct tegra_sor *sor = container_of(work, struct tegra_sor, scdc.work);
2355 struct i2c_adapter *ddc = sor->output.ddc;
2356
2357 if (!drm_scdc_get_scrambling_status(ddc)) {
2358 DRM_DEBUG_KMS("SCDC not scrambled\n");
2359 tegra_sor_hdmi_scdc_enable(sor);
2360 }
2361
2362 schedule_delayed_work(&sor->scdc, msecs_to_jiffies(5000));
2363}
2364
2365static void tegra_sor_hdmi_scdc_start(struct tegra_sor *sor)
2366{
2367 struct drm_scdc *scdc = &sor->output.connector.display_info.hdmi.scdc;
2368 struct drm_display_mode *mode;
2369
2370 mode = &sor->output.encoder.crtc->state->adjusted_mode;
2371
2372 if (mode->clock >= 340000 && scdc->supported) {
2373 schedule_delayed_work(&sor->scdc, msecs_to_jiffies(5000));
2374 tegra_sor_hdmi_scdc_enable(sor);
2375 sor->scdc_enabled = true;
2376 }
2377}
2378
Thierry Reding459cc2c2015-07-30 10:34:24 +02002379static void tegra_sor_hdmi_disable(struct drm_encoder *encoder)
2380{
2381 struct tegra_output *output = encoder_to_output(encoder);
2382 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
2383 struct tegra_sor *sor = to_sor(output);
2384 u32 value;
2385 int err;
2386
Thierry Reding8e2988a72018-12-03 15:46:03 +01002387 tegra_sor_audio_unprepare(sor);
Thierry Reding36e90222017-10-12 19:14:21 +02002388 tegra_sor_hdmi_scdc_stop(sor);
2389
Thierry Reding459cc2c2015-07-30 10:34:24 +02002390 err = tegra_sor_detach(sor);
2391 if (err < 0)
2392 dev_err(sor->dev, "failed to detach SOR: %d\n", err);
2393
2394 tegra_sor_writel(sor, 0, SOR_STATE1);
2395 tegra_sor_update(sor);
2396
2397 /* disable display to SOR clock */
2398 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
Thierry Redingc57997b2017-10-12 19:12:57 +02002399
2400 if (!sor->soc->has_nvdisplay)
2401 value &= ~(SOR1_TIMING_CYA | SOR_ENABLE(1));
2402 else
2403 value &= ~SOR_ENABLE(sor->index);
2404
Thierry Reding459cc2c2015-07-30 10:34:24 +02002405 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
2406
2407 tegra_dc_commit(dc);
2408
2409 err = tegra_sor_power_down(sor);
2410 if (err < 0)
2411 dev_err(sor->dev, "failed to power down SOR: %d\n", err);
2412
Thierry Redingc57997b2017-10-12 19:12:57 +02002413 err = tegra_io_pad_power_disable(sor->pad);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002414 if (err < 0)
Thierry Redingc57997b2017-10-12 19:12:57 +02002415 dev_err(sor->dev, "failed to power off I/O pad: %d\n", err);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002416
Thierry Redingaaff8bd2015-08-07 16:04:54 +02002417 pm_runtime_put(sor->dev);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002418}
2419
2420static void tegra_sor_hdmi_enable(struct drm_encoder *encoder)
2421{
2422 struct tegra_output *output = encoder_to_output(encoder);
2423 unsigned int h_ref_to_sync = 1, pulse_start, max_ac;
2424 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002425 struct tegra_sor_hdmi_settings *settings;
2426 struct tegra_sor *sor = to_sor(output);
Thierry Redingc31efa72015-09-08 16:09:22 +02002427 struct tegra_sor_state *state;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002428 struct drm_display_mode *mode;
Thierry Reding36e90222017-10-12 19:14:21 +02002429 unsigned long rate, pclk;
Thierry Reding30b49432015-08-03 15:50:32 +02002430 unsigned int div, i;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002431 u32 value;
2432 int err;
2433
Thierry Redingc31efa72015-09-08 16:09:22 +02002434 state = to_sor_state(output->connector.state);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002435 mode = &encoder->crtc->state->adjusted_mode;
Thierry Reding36e90222017-10-12 19:14:21 +02002436 pclk = mode->clock * 1000;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002437
Thierry Redingaaff8bd2015-08-07 16:04:54 +02002438 pm_runtime_get_sync(sor->dev);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002439
Thierry Reding25bb2ce2015-08-03 14:23:29 +02002440 /* switch to safe parent clock */
2441 err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
Thierry Redinge1335e22017-10-12 17:53:11 +02002442 if (err < 0) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002443 dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
Thierry Redinge1335e22017-10-12 17:53:11 +02002444 return;
2445 }
Thierry Reding459cc2c2015-07-30 10:34:24 +02002446
2447 div = clk_get_rate(sor->clk) / 1000000 * 4;
2448
Thierry Redingc57997b2017-10-12 19:12:57 +02002449 err = tegra_io_pad_power_enable(sor->pad);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002450 if (err < 0)
Thierry Redingc57997b2017-10-12 19:12:57 +02002451 dev_err(sor->dev, "failed to power on I/O pad: %d\n", err);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002452
2453 usleep_range(20, 100);
2454
Thierry Reding880cee02017-10-12 19:04:17 +02002455 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002456 value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
Thierry Reding880cee02017-10-12 19:04:17 +02002457 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
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->pll3);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002462 value &= ~SOR_PLL3_PLL_VDD_MODE_3V3;
Thierry Reding880cee02017-10-12 19:04:17 +02002463 tegra_sor_writel(sor, value, sor->soc->regs->pll3);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002464
Thierry Reding880cee02017-10-12 19:04:17 +02002465 value = tegra_sor_readl(sor, sor->soc->regs->pll0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002466 value &= ~SOR_PLL0_VCOPD;
2467 value &= ~SOR_PLL0_PWR;
Thierry Reding880cee02017-10-12 19:04:17 +02002468 tegra_sor_writel(sor, value, sor->soc->regs->pll0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002469
Thierry Reding880cee02017-10-12 19:04:17 +02002470 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002471 value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
Thierry Reding880cee02017-10-12 19:04:17 +02002472 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002473
2474 usleep_range(200, 400);
2475
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_POWERDOWN_OVERRIDE;
2478 value &= ~SOR_PLL2_PORT_POWERDOWN;
Thierry Reding880cee02017-10-12 19:04:17 +02002479 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002480
2481 usleep_range(20, 100);
2482
Thierry Reding880cee02017-10-12 19:04:17 +02002483 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002484 value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
2485 SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2;
Thierry Reding880cee02017-10-12 19:04:17 +02002486 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002487
2488 while (true) {
2489 value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
2490 if ((value & SOR_LANE_SEQ_CTL_STATE_BUSY) == 0)
2491 break;
2492
2493 usleep_range(250, 1000);
2494 }
2495
2496 value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
2497 SOR_LANE_SEQ_CTL_POWER_STATE_UP | SOR_LANE_SEQ_CTL_DELAY(5);
2498 tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
2499
2500 while (true) {
2501 value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
2502 if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
2503 break;
2504
2505 usleep_range(250, 1000);
2506 }
2507
2508 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
2509 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
2510 value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
2511
Thierry Reding36e90222017-10-12 19:14:21 +02002512 if (mode->clock < 340000) {
2513 DRM_DEBUG_KMS("setting 2.7 GHz link speed\n");
Thierry Reding459cc2c2015-07-30 10:34:24 +02002514 value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G2_70;
Thierry Reding36e90222017-10-12 19:14:21 +02002515 } else {
2516 DRM_DEBUG_KMS("setting 5.4 GHz link speed\n");
Thierry Reding459cc2c2015-07-30 10:34:24 +02002517 value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G5_40;
Thierry Reding36e90222017-10-12 19:14:21 +02002518 }
Thierry Reding459cc2c2015-07-30 10:34:24 +02002519
2520 value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK;
2521 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
2522
Thierry Redingc57997b2017-10-12 19:12:57 +02002523 /* SOR pad PLL stabilization time */
2524 usleep_range(250, 1000);
2525
2526 value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
2527 value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
2528 value |= SOR_DP_LINKCTL_LANE_COUNT(4);
2529 tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
2530
Thierry Reding459cc2c2015-07-30 10:34:24 +02002531 value = tegra_sor_readl(sor, SOR_DP_SPARE0);
Thierry Redingc57997b2017-10-12 19:12:57 +02002532 value &= ~SOR_DP_SPARE_DISP_VIDEO_PREAMBLE;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002533 value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
Thierry Redingc57997b2017-10-12 19:12:57 +02002534 value &= ~SOR_DP_SPARE_SEQ_ENABLE;
2535 value &= ~SOR_DP_SPARE_MACRO_SOR_CLK;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002536 tegra_sor_writel(sor, value, SOR_DP_SPARE0);
2537
2538 value = SOR_SEQ_CTL_PU_PC(0) | SOR_SEQ_CTL_PU_PC_ALT(0) |
2539 SOR_SEQ_CTL_PD_PC(8) | SOR_SEQ_CTL_PD_PC_ALT(8);
2540 tegra_sor_writel(sor, value, SOR_SEQ_CTL);
2541
2542 value = SOR_SEQ_INST_DRIVE_PWM_OUT_LO | SOR_SEQ_INST_HALT |
2543 SOR_SEQ_INST_WAIT_VSYNC | SOR_SEQ_INST_WAIT(1);
2544 tegra_sor_writel(sor, value, SOR_SEQ_INST(0));
2545 tegra_sor_writel(sor, value, SOR_SEQ_INST(8));
2546
Thierry Redingc57997b2017-10-12 19:12:57 +02002547 if (!sor->soc->has_nvdisplay) {
2548 /* program the reference clock */
2549 value = SOR_REFCLK_DIV_INT(div) | SOR_REFCLK_DIV_FRAC(div);
2550 tegra_sor_writel(sor, value, SOR_REFCLK);
2551 }
Thierry Reding459cc2c2015-07-30 10:34:24 +02002552
Thierry Reding30b49432015-08-03 15:50:32 +02002553 /* XXX not in TRM */
2554 for (value = 0, i = 0; i < 5; i++)
Thierry Reding6d6c8152019-01-25 11:00:58 +01002555 value |= SOR_XBAR_CTRL_LINK0_XSEL(i, sor->xbar_cfg[i]) |
Thierry Reding30b49432015-08-03 15:50:32 +02002556 SOR_XBAR_CTRL_LINK1_XSEL(i, i);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002557
2558 tegra_sor_writel(sor, 0x00000000, SOR_XBAR_POL);
Thierry Reding30b49432015-08-03 15:50:32 +02002559 tegra_sor_writel(sor, value, SOR_XBAR_CTRL);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002560
Thierry Reding25bb2ce2015-08-03 14:23:29 +02002561 /* switch to parent clock */
Thierry Redinge1335e22017-10-12 17:53:11 +02002562 err = clk_set_parent(sor->clk, sor->clk_parent);
2563 if (err < 0) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002564 dev_err(sor->dev, "failed to set parent clock: %d\n", err);
Thierry Redinge1335e22017-10-12 17:53:11 +02002565 return;
2566 }
2567
2568 err = tegra_sor_set_parent_clock(sor, sor->clk_pad);
2569 if (err < 0) {
2570 dev_err(sor->dev, "failed to set pad clock: %d\n", err);
2571 return;
2572 }
Thierry Reding459cc2c2015-07-30 10:34:24 +02002573
Thierry Reding36e90222017-10-12 19:14:21 +02002574 /* adjust clock rate for HDMI 2.0 modes */
2575 rate = clk_get_rate(sor->clk_parent);
2576
2577 if (mode->clock >= 340000)
2578 rate /= 2;
2579
2580 DRM_DEBUG_KMS("setting clock to %lu Hz, mode: %lu Hz\n", rate, pclk);
2581
2582 clk_set_rate(sor->clk, rate);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002583
Thierry Redingc57997b2017-10-12 19:12:57 +02002584 if (!sor->soc->has_nvdisplay) {
2585 value = SOR_INPUT_CONTROL_HDMI_SRC_SELECT(dc->pipe);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002586
Thierry Redingc57997b2017-10-12 19:12:57 +02002587 /* XXX is this the proper check? */
2588 if (mode->clock < 75000)
2589 value |= SOR_INPUT_CONTROL_ARM_VIDEO_RANGE_LIMITED;
2590
2591 tegra_sor_writel(sor, value, SOR_INPUT_CONTROL);
2592 }
Thierry Reding459cc2c2015-07-30 10:34:24 +02002593
2594 max_ac = ((mode->htotal - mode->hdisplay) - SOR_REKEY - 18) / 32;
2595
2596 value = SOR_HDMI_CTRL_ENABLE | SOR_HDMI_CTRL_MAX_AC_PACKET(max_ac) |
2597 SOR_HDMI_CTRL_AUDIO_LAYOUT | SOR_HDMI_CTRL_REKEY(SOR_REKEY);
2598 tegra_sor_writel(sor, value, SOR_HDMI_CTRL);
2599
Thierry Redingc57997b2017-10-12 19:12:57 +02002600 if (!dc->soc->has_nvdisplay) {
2601 /* H_PULSE2 setup */
2602 pulse_start = h_ref_to_sync +
2603 (mode->hsync_end - mode->hsync_start) +
2604 (mode->htotal - mode->hsync_end) - 10;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002605
Thierry Redingc57997b2017-10-12 19:12:57 +02002606 value = PULSE_LAST_END_A | PULSE_QUAL_VACTIVE |
2607 PULSE_POLARITY_HIGH | PULSE_MODE_NORMAL;
2608 tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_CONTROL);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002609
Thierry Redingc57997b2017-10-12 19:12:57 +02002610 value = PULSE_END(pulse_start + 8) | PULSE_START(pulse_start);
2611 tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_POSITION_A);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002612
Thierry Redingc57997b2017-10-12 19:12:57 +02002613 value = tegra_dc_readl(dc, DC_DISP_DISP_SIGNAL_OPTIONS0);
2614 value |= H_PULSE2_ENABLE;
2615 tegra_dc_writel(dc, value, DC_DISP_DISP_SIGNAL_OPTIONS0);
2616 }
Thierry Reding459cc2c2015-07-30 10:34:24 +02002617
2618 /* infoframe setup */
2619 err = tegra_sor_hdmi_setup_avi_infoframe(sor, mode);
2620 if (err < 0)
2621 dev_err(sor->dev, "failed to setup AVI infoframe: %d\n", err);
2622
2623 /* XXX HDMI audio support not implemented yet */
2624 tegra_sor_hdmi_disable_audio_infoframe(sor);
2625
2626 /* use single TMDS protocol */
2627 value = tegra_sor_readl(sor, SOR_STATE1);
2628 value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
2629 value |= SOR_STATE_ASY_PROTOCOL_SINGLE_TMDS_A;
2630 tegra_sor_writel(sor, value, SOR_STATE1);
2631
2632 /* power up pad calibration */
Thierry Reding880cee02017-10-12 19:04:17 +02002633 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002634 value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
Thierry Reding880cee02017-10-12 19:04:17 +02002635 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002636
2637 /* production settings */
2638 settings = tegra_sor_hdmi_find_settings(sor, mode->clock * 1000);
Dan Carpenterdb8b42f2015-08-17 17:37:03 +03002639 if (!settings) {
2640 dev_err(sor->dev, "no settings for pixel clock %d Hz\n",
2641 mode->clock * 1000);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002642 return;
2643 }
2644
Thierry Reding880cee02017-10-12 19:04:17 +02002645 value = tegra_sor_readl(sor, sor->soc->regs->pll0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002646 value &= ~SOR_PLL0_ICHPMP_MASK;
Thierry Redingc57997b2017-10-12 19:12:57 +02002647 value &= ~SOR_PLL0_FILTER_MASK;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002648 value &= ~SOR_PLL0_VCOCAP_MASK;
2649 value |= SOR_PLL0_ICHPMP(settings->ichpmp);
Thierry Redingc57997b2017-10-12 19:12:57 +02002650 value |= SOR_PLL0_FILTER(settings->filter);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002651 value |= SOR_PLL0_VCOCAP(settings->vcocap);
Thierry Reding880cee02017-10-12 19:04:17 +02002652 tegra_sor_writel(sor, value, sor->soc->regs->pll0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002653
Thierry Redingc57997b2017-10-12 19:12:57 +02002654 /* XXX not in TRM */
Thierry Reding880cee02017-10-12 19:04:17 +02002655 value = tegra_sor_readl(sor, sor->soc->regs->pll1);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002656 value &= ~SOR_PLL1_LOADADJ_MASK;
Thierry Redingc57997b2017-10-12 19:12:57 +02002657 value &= ~SOR_PLL1_TMDS_TERMADJ_MASK;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002658 value |= SOR_PLL1_LOADADJ(settings->loadadj);
Thierry Redingc57997b2017-10-12 19:12:57 +02002659 value |= SOR_PLL1_TMDS_TERMADJ(settings->tmds_termadj);
2660 value |= SOR_PLL1_TMDS_TERM;
Thierry Reding880cee02017-10-12 19:04:17 +02002661 tegra_sor_writel(sor, value, sor->soc->regs->pll1);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002662
Thierry Reding880cee02017-10-12 19:04:17 +02002663 value = tegra_sor_readl(sor, sor->soc->regs->pll3);
Thierry Redingc57997b2017-10-12 19:12:57 +02002664 value &= ~SOR_PLL3_BG_TEMP_COEF_MASK;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002665 value &= ~SOR_PLL3_BG_VREF_LEVEL_MASK;
Thierry Redingc57997b2017-10-12 19:12:57 +02002666 value &= ~SOR_PLL3_AVDD10_LEVEL_MASK;
2667 value &= ~SOR_PLL3_AVDD14_LEVEL_MASK;
2668 value |= SOR_PLL3_BG_TEMP_COEF(settings->bg_temp_coef);
2669 value |= SOR_PLL3_BG_VREF_LEVEL(settings->bg_vref_level);
2670 value |= SOR_PLL3_AVDD10_LEVEL(settings->avdd10_level);
2671 value |= SOR_PLL3_AVDD14_LEVEL(settings->avdd14_level);
Thierry Reding880cee02017-10-12 19:04:17 +02002672 tegra_sor_writel(sor, value, sor->soc->regs->pll3);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002673
Thierry Redingc57997b2017-10-12 19:12:57 +02002674 value = settings->drive_current[3] << 24 |
2675 settings->drive_current[2] << 16 |
2676 settings->drive_current[1] << 8 |
2677 settings->drive_current[0] << 0;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002678 tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT0);
2679
Thierry Redingc57997b2017-10-12 19:12:57 +02002680 value = settings->preemphasis[3] << 24 |
2681 settings->preemphasis[2] << 16 |
2682 settings->preemphasis[1] << 8 |
2683 settings->preemphasis[0] << 0;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002684 tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS0);
2685
Thierry Reding880cee02017-10-12 19:04:17 +02002686 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002687 value &= ~SOR_DP_PADCTL_TX_PU_MASK;
2688 value |= SOR_DP_PADCTL_TX_PU_ENABLE;
Thierry Redingc57997b2017-10-12 19:12:57 +02002689 value |= SOR_DP_PADCTL_TX_PU(settings->tx_pu_value);
Thierry Reding880cee02017-10-12 19:04:17 +02002690 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002691
Thierry Redingc57997b2017-10-12 19:12:57 +02002692 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl2);
2693 value &= ~SOR_DP_PADCTL_SPAREPLL_MASK;
2694 value |= SOR_DP_PADCTL_SPAREPLL(settings->sparepll);
2695 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl2);
2696
Thierry Reding459cc2c2015-07-30 10:34:24 +02002697 /* power down pad calibration */
Thierry Reding880cee02017-10-12 19:04:17 +02002698 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002699 value |= SOR_DP_PADCTL_PAD_CAL_PD;
Thierry Reding880cee02017-10-12 19:04:17 +02002700 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002701
Thierry Redingc57997b2017-10-12 19:12:57 +02002702 if (!dc->soc->has_nvdisplay) {
2703 /* miscellaneous display controller settings */
2704 value = VSYNC_H_POSITION(1);
2705 tegra_dc_writel(dc, value, DC_DISP_DISP_TIMING_OPTIONS);
2706 }
Thierry Reding459cc2c2015-07-30 10:34:24 +02002707
2708 value = tegra_dc_readl(dc, DC_DISP_DISP_COLOR_CONTROL);
2709 value &= ~DITHER_CONTROL_MASK;
2710 value &= ~BASE_COLOR_SIZE_MASK;
2711
Thierry Redingc31efa72015-09-08 16:09:22 +02002712 switch (state->bpc) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002713 case 6:
2714 value |= BASE_COLOR_SIZE_666;
2715 break;
2716
2717 case 8:
2718 value |= BASE_COLOR_SIZE_888;
2719 break;
2720
Thierry Redingc57997b2017-10-12 19:12:57 +02002721 case 10:
2722 value |= BASE_COLOR_SIZE_101010;
2723 break;
2724
2725 case 12:
2726 value |= BASE_COLOR_SIZE_121212;
2727 break;
2728
Thierry Reding459cc2c2015-07-30 10:34:24 +02002729 default:
Thierry Redingc31efa72015-09-08 16:09:22 +02002730 WARN(1, "%u bits-per-color not supported\n", state->bpc);
2731 value |= BASE_COLOR_SIZE_888;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002732 break;
2733 }
2734
2735 tegra_dc_writel(dc, value, DC_DISP_DISP_COLOR_CONTROL);
2736
Thierry Redingc57997b2017-10-12 19:12:57 +02002737 /* XXX set display head owner */
2738 value = tegra_sor_readl(sor, SOR_STATE1);
2739 value &= ~SOR_STATE_ASY_OWNER_MASK;
2740 value |= SOR_STATE_ASY_OWNER(1 + dc->pipe);
2741 tegra_sor_writel(sor, value, SOR_STATE1);
2742
Thierry Reding459cc2c2015-07-30 10:34:24 +02002743 err = tegra_sor_power_up(sor, 250);
2744 if (err < 0)
2745 dev_err(sor->dev, "failed to power up SOR: %d\n", err);
2746
Thierry Reding2bd1dd32015-08-03 15:46:15 +02002747 /* configure dynamic range of output */
Thierry Reding880cee02017-10-12 19:04:17 +02002748 value = tegra_sor_readl(sor, sor->soc->regs->head_state0 + dc->pipe);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002749 value &= ~SOR_HEAD_STATE_RANGECOMPRESS_MASK;
2750 value &= ~SOR_HEAD_STATE_DYNRANGE_MASK;
Thierry Reding880cee02017-10-12 19:04:17 +02002751 tegra_sor_writel(sor, value, sor->soc->regs->head_state0 + dc->pipe);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002752
Thierry Reding2bd1dd32015-08-03 15:46:15 +02002753 /* configure colorspace */
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_COLORSPACE_MASK;
2756 value |= SOR_HEAD_STATE_COLORSPACE_RGB;
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 Redingc31efa72015-09-08 16:09:22 +02002759 tegra_sor_mode_set(sor, mode, state);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002760
2761 tegra_sor_update(sor);
2762
Thierry Redingc57997b2017-10-12 19:12:57 +02002763 /* program preamble timing in SOR (XXX) */
2764 value = tegra_sor_readl(sor, SOR_DP_SPARE0);
2765 value &= ~SOR_DP_SPARE_DISP_VIDEO_PREAMBLE;
2766 tegra_sor_writel(sor, value, SOR_DP_SPARE0);
2767
Thierry Reding459cc2c2015-07-30 10:34:24 +02002768 err = tegra_sor_attach(sor);
2769 if (err < 0)
2770 dev_err(sor->dev, "failed to attach SOR: %d\n", err);
2771
2772 /* enable display to SOR clock and generate HDMI preamble */
2773 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
Thierry Redingc57997b2017-10-12 19:12:57 +02002774
2775 if (!sor->soc->has_nvdisplay)
2776 value |= SOR_ENABLE(1) | SOR1_TIMING_CYA;
2777 else
2778 value |= SOR_ENABLE(sor->index);
2779
Thierry Reding459cc2c2015-07-30 10:34:24 +02002780 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
2781
Thierry Redingc57997b2017-10-12 19:12:57 +02002782 if (dc->soc->has_nvdisplay) {
2783 value = tegra_dc_readl(dc, DC_DISP_CORE_SOR_SET_CONTROL(sor->index));
2784 value &= ~PROTOCOL_MASK;
2785 value |= PROTOCOL_SINGLE_TMDS_A;
2786 tegra_dc_writel(dc, value, DC_DISP_CORE_SOR_SET_CONTROL(sor->index));
2787 }
2788
Thierry Reding459cc2c2015-07-30 10:34:24 +02002789 tegra_dc_commit(dc);
2790
2791 err = tegra_sor_wakeup(sor);
2792 if (err < 0)
2793 dev_err(sor->dev, "failed to wakeup SOR: %d\n", err);
Thierry Reding36e90222017-10-12 19:14:21 +02002794
2795 tegra_sor_hdmi_scdc_start(sor);
Thierry Reding8e2988a72018-12-03 15:46:03 +01002796 tegra_sor_audio_prepare(sor);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002797}
2798
2799static const struct drm_encoder_helper_funcs tegra_sor_hdmi_helpers = {
2800 .disable = tegra_sor_hdmi_disable,
2801 .enable = tegra_sor_hdmi_enable,
2802 .atomic_check = tegra_sor_encoder_atomic_check,
2803};
2804
Thierry Reding6b6b6042013-11-15 16:06:05 +01002805static int tegra_sor_init(struct host1x_client *client)
2806{
Thierry Reding9910f5c2014-05-22 09:57:15 +02002807 struct drm_device *drm = dev_get_drvdata(client->parent);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002808 const struct drm_encoder_helper_funcs *helpers = NULL;
Thierry Reding6b6b6042013-11-15 16:06:05 +01002809 struct tegra_sor *sor = host1x_client_to_sor(client);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002810 int connector = DRM_MODE_CONNECTOR_Unknown;
2811 int encoder = DRM_MODE_ENCODER_NONE;
Thierry Reding8e2988a72018-12-03 15:46:03 +01002812 u32 value;
Thierry Reding6b6b6042013-11-15 16:06:05 +01002813 int err;
2814
Thierry Reding9542c232015-07-08 13:39:09 +02002815 if (!sor->aux) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002816 if (sor->soc->supports_hdmi) {
2817 connector = DRM_MODE_CONNECTOR_HDMIA;
2818 encoder = DRM_MODE_ENCODER_TMDS;
2819 helpers = &tegra_sor_hdmi_helpers;
2820 } else if (sor->soc->supports_lvds) {
2821 connector = DRM_MODE_CONNECTOR_LVDS;
2822 encoder = DRM_MODE_ENCODER_LVDS;
2823 }
2824 } else {
2825 if (sor->soc->supports_edp) {
2826 connector = DRM_MODE_CONNECTOR_eDP;
2827 encoder = DRM_MODE_ENCODER_TMDS;
2828 helpers = &tegra_sor_edp_helpers;
2829 } else if (sor->soc->supports_dp) {
2830 connector = DRM_MODE_CONNECTOR_DisplayPort;
2831 encoder = DRM_MODE_ENCODER_TMDS;
2832 }
2833 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01002834
Thierry Reding6b6b6042013-11-15 16:06:05 +01002835 sor->output.dev = sor->dev;
Thierry Reding6b6b6042013-11-15 16:06:05 +01002836
Thierry Reding6fad8f62014-11-28 15:41:34 +01002837 drm_connector_init(drm, &sor->output.connector,
2838 &tegra_sor_connector_funcs,
Thierry Reding459cc2c2015-07-30 10:34:24 +02002839 connector);
Thierry Reding6fad8f62014-11-28 15:41:34 +01002840 drm_connector_helper_add(&sor->output.connector,
2841 &tegra_sor_connector_helper_funcs);
2842 sor->output.connector.dpms = DRM_MODE_DPMS_OFF;
2843
Thierry Reding6fad8f62014-11-28 15:41:34 +01002844 drm_encoder_init(drm, &sor->output.encoder, &tegra_sor_encoder_funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +02002845 encoder, NULL);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002846 drm_encoder_helper_add(&sor->output.encoder, helpers);
Thierry Reding6fad8f62014-11-28 15:41:34 +01002847
Daniel Vettercde4c442018-07-09 10:40:07 +02002848 drm_connector_attach_encoder(&sor->output.connector,
Thierry Reding6fad8f62014-11-28 15:41:34 +01002849 &sor->output.encoder);
2850 drm_connector_register(&sor->output.connector);
2851
Thierry Redingea130b22014-12-19 15:51:35 +01002852 err = tegra_output_init(drm, &sor->output);
2853 if (err < 0) {
2854 dev_err(client->dev, "failed to initialize output: %d\n", err);
2855 return err;
2856 }
Thierry Reding6fad8f62014-11-28 15:41:34 +01002857
Thierry Redingc57997b2017-10-12 19:12:57 +02002858 tegra_output_find_possible_crtcs(&sor->output, drm);
Thierry Reding6b6b6042013-11-15 16:06:05 +01002859
Thierry Reding9542c232015-07-08 13:39:09 +02002860 if (sor->aux) {
2861 err = drm_dp_aux_attach(sor->aux, &sor->output);
Thierry Reding6b6b6042013-11-15 16:06:05 +01002862 if (err < 0) {
2863 dev_err(sor->dev, "failed to attach DP: %d\n", err);
2864 return err;
2865 }
2866 }
2867
Tomeu Vizoso535a65d2015-03-30 10:33:03 +02002868 /*
2869 * XXX: Remove this reset once proper hand-over from firmware to
2870 * kernel is possible.
2871 */
Jon Hunterf8c79122016-07-01 14:21:38 +01002872 if (sor->rst) {
2873 err = reset_control_assert(sor->rst);
2874 if (err < 0) {
2875 dev_err(sor->dev, "failed to assert SOR reset: %d\n",
2876 err);
2877 return err;
2878 }
Tomeu Vizoso535a65d2015-03-30 10:33:03 +02002879 }
2880
Thierry Reding6fad8f62014-11-28 15:41:34 +01002881 err = clk_prepare_enable(sor->clk);
2882 if (err < 0) {
2883 dev_err(sor->dev, "failed to enable clock: %d\n", err);
2884 return err;
2885 }
2886
Tomeu Vizoso535a65d2015-03-30 10:33:03 +02002887 usleep_range(1000, 3000);
2888
Jon Hunterf8c79122016-07-01 14:21:38 +01002889 if (sor->rst) {
2890 err = reset_control_deassert(sor->rst);
2891 if (err < 0) {
2892 dev_err(sor->dev, "failed to deassert SOR reset: %d\n",
2893 err);
2894 return err;
2895 }
Tomeu Vizoso535a65d2015-03-30 10:33:03 +02002896 }
2897
Thierry Reding6fad8f62014-11-28 15:41:34 +01002898 err = clk_prepare_enable(sor->clk_safe);
2899 if (err < 0)
2900 return err;
2901
2902 err = clk_prepare_enable(sor->clk_dp);
2903 if (err < 0)
2904 return err;
2905
Thierry Reding8e2988a72018-12-03 15:46:03 +01002906 /*
2907 * Enable and unmask the HDA codec SCRATCH0 register interrupt. This
2908 * is used for interoperability between the HDA codec driver and the
2909 * HDMI/DP driver.
2910 */
2911 value = SOR_INT_CODEC_SCRATCH1 | SOR_INT_CODEC_SCRATCH0;
2912 tegra_sor_writel(sor, value, SOR_INT_ENABLE);
2913 tegra_sor_writel(sor, value, SOR_INT_MASK);
2914
Thierry Reding6b6b6042013-11-15 16:06:05 +01002915 return 0;
2916}
2917
2918static int tegra_sor_exit(struct host1x_client *client)
2919{
2920 struct tegra_sor *sor = host1x_client_to_sor(client);
2921 int err;
2922
Thierry Reding8e2988a72018-12-03 15:46:03 +01002923 tegra_sor_writel(sor, 0, SOR_INT_MASK);
2924 tegra_sor_writel(sor, 0, SOR_INT_ENABLE);
2925
Thierry Reding328ec692014-12-19 15:55:08 +01002926 tegra_output_exit(&sor->output);
2927
Thierry Reding9542c232015-07-08 13:39:09 +02002928 if (sor->aux) {
2929 err = drm_dp_aux_detach(sor->aux);
Thierry Reding6b6b6042013-11-15 16:06:05 +01002930 if (err < 0) {
2931 dev_err(sor->dev, "failed to detach DP: %d\n", err);
2932 return err;
2933 }
2934 }
2935
Thierry Reding6fad8f62014-11-28 15:41:34 +01002936 clk_disable_unprepare(sor->clk_safe);
2937 clk_disable_unprepare(sor->clk_dp);
2938 clk_disable_unprepare(sor->clk);
2939
Thierry Reding6b6b6042013-11-15 16:06:05 +01002940 return 0;
2941}
2942
2943static const struct host1x_client_ops sor_client_ops = {
2944 .init = tegra_sor_init,
2945 .exit = tegra_sor_exit,
2946};
2947
Thierry Reding459cc2c2015-07-30 10:34:24 +02002948static const struct tegra_sor_ops tegra_sor_edp_ops = {
2949 .name = "eDP",
2950};
2951
2952static int tegra_sor_hdmi_probe(struct tegra_sor *sor)
2953{
2954 int err;
2955
2956 sor->avdd_io_supply = devm_regulator_get(sor->dev, "avdd-io");
2957 if (IS_ERR(sor->avdd_io_supply)) {
2958 dev_err(sor->dev, "cannot get AVDD I/O supply: %ld\n",
2959 PTR_ERR(sor->avdd_io_supply));
2960 return PTR_ERR(sor->avdd_io_supply);
2961 }
2962
2963 err = regulator_enable(sor->avdd_io_supply);
2964 if (err < 0) {
2965 dev_err(sor->dev, "failed to enable AVDD I/O supply: %d\n",
2966 err);
2967 return err;
2968 }
2969
2970 sor->vdd_pll_supply = devm_regulator_get(sor->dev, "vdd-pll");
2971 if (IS_ERR(sor->vdd_pll_supply)) {
2972 dev_err(sor->dev, "cannot get VDD PLL supply: %ld\n",
2973 PTR_ERR(sor->vdd_pll_supply));
2974 return PTR_ERR(sor->vdd_pll_supply);
2975 }
2976
2977 err = regulator_enable(sor->vdd_pll_supply);
2978 if (err < 0) {
2979 dev_err(sor->dev, "failed to enable VDD PLL supply: %d\n",
2980 err);
2981 return err;
2982 }
2983
2984 sor->hdmi_supply = devm_regulator_get(sor->dev, "hdmi");
2985 if (IS_ERR(sor->hdmi_supply)) {
2986 dev_err(sor->dev, "cannot get HDMI supply: %ld\n",
2987 PTR_ERR(sor->hdmi_supply));
2988 return PTR_ERR(sor->hdmi_supply);
2989 }
2990
2991 err = regulator_enable(sor->hdmi_supply);
2992 if (err < 0) {
2993 dev_err(sor->dev, "failed to enable HDMI supply: %d\n", err);
2994 return err;
2995 }
2996
Thierry Reding36e90222017-10-12 19:14:21 +02002997 INIT_DELAYED_WORK(&sor->scdc, tegra_sor_hdmi_scdc_work);
2998
Thierry Reding459cc2c2015-07-30 10:34:24 +02002999 return 0;
3000}
3001
3002static int tegra_sor_hdmi_remove(struct tegra_sor *sor)
3003{
3004 regulator_disable(sor->hdmi_supply);
3005 regulator_disable(sor->vdd_pll_supply);
3006 regulator_disable(sor->avdd_io_supply);
3007
3008 return 0;
3009}
3010
3011static const struct tegra_sor_ops tegra_sor_hdmi_ops = {
3012 .name = "HDMI",
3013 .probe = tegra_sor_hdmi_probe,
3014 .remove = tegra_sor_hdmi_remove,
3015};
3016
Thierry Reding30b49432015-08-03 15:50:32 +02003017static const u8 tegra124_sor_xbar_cfg[5] = {
3018 0, 1, 2, 3, 4
3019};
3020
Thierry Reding880cee02017-10-12 19:04:17 +02003021static const struct tegra_sor_regs tegra124_sor_regs = {
3022 .head_state0 = 0x05,
3023 .head_state1 = 0x07,
3024 .head_state2 = 0x09,
3025 .head_state3 = 0x0b,
3026 .head_state4 = 0x0d,
3027 .head_state5 = 0x0f,
3028 .pll0 = 0x17,
3029 .pll1 = 0x18,
3030 .pll2 = 0x19,
3031 .pll3 = 0x1a,
3032 .dp_padctl0 = 0x5c,
3033 .dp_padctl2 = 0x73,
3034};
3035
Thierry Reding459cc2c2015-07-30 10:34:24 +02003036static const struct tegra_sor_soc tegra124_sor = {
3037 .supports_edp = true,
3038 .supports_lvds = true,
3039 .supports_hdmi = false,
3040 .supports_dp = false,
Thierry Reding880cee02017-10-12 19:04:17 +02003041 .regs = &tegra124_sor_regs,
Thierry Redingc57997b2017-10-12 19:12:57 +02003042 .has_nvdisplay = false,
Thierry Reding30b49432015-08-03 15:50:32 +02003043 .xbar_cfg = tegra124_sor_xbar_cfg,
Thierry Reding459cc2c2015-07-30 10:34:24 +02003044};
3045
Thierry Reding880cee02017-10-12 19:04:17 +02003046static const struct tegra_sor_regs tegra210_sor_regs = {
3047 .head_state0 = 0x05,
3048 .head_state1 = 0x07,
3049 .head_state2 = 0x09,
3050 .head_state3 = 0x0b,
3051 .head_state4 = 0x0d,
3052 .head_state5 = 0x0f,
3053 .pll0 = 0x17,
3054 .pll1 = 0x18,
3055 .pll2 = 0x19,
3056 .pll3 = 0x1a,
3057 .dp_padctl0 = 0x5c,
3058 .dp_padctl2 = 0x73,
3059};
3060
Thierry Reding459cc2c2015-07-30 10:34:24 +02003061static const struct tegra_sor_soc tegra210_sor = {
3062 .supports_edp = true,
3063 .supports_lvds = false,
3064 .supports_hdmi = false,
3065 .supports_dp = false,
Thierry Reding880cee02017-10-12 19:04:17 +02003066 .regs = &tegra210_sor_regs,
Thierry Redingc57997b2017-10-12 19:12:57 +02003067 .has_nvdisplay = false,
Thierry Reding30b49432015-08-03 15:50:32 +02003068 .xbar_cfg = tegra124_sor_xbar_cfg,
3069};
3070
3071static const u8 tegra210_sor_xbar_cfg[5] = {
3072 2, 1, 0, 3, 4
Thierry Reding459cc2c2015-07-30 10:34:24 +02003073};
3074
3075static const struct tegra_sor_soc tegra210_sor1 = {
3076 .supports_edp = false,
3077 .supports_lvds = false,
3078 .supports_hdmi = true,
3079 .supports_dp = true,
3080
Thierry Reding880cee02017-10-12 19:04:17 +02003081 .regs = &tegra210_sor_regs,
Thierry Redingc57997b2017-10-12 19:12:57 +02003082 .has_nvdisplay = false,
Thierry Reding880cee02017-10-12 19:04:17 +02003083
Thierry Reding459cc2c2015-07-30 10:34:24 +02003084 .num_settings = ARRAY_SIZE(tegra210_sor_hdmi_defaults),
3085 .settings = tegra210_sor_hdmi_defaults,
Thierry Reding30b49432015-08-03 15:50:32 +02003086
3087 .xbar_cfg = tegra210_sor_xbar_cfg,
Thierry Reding459cc2c2015-07-30 10:34:24 +02003088};
3089
Thierry Redingc57997b2017-10-12 19:12:57 +02003090static const struct tegra_sor_regs tegra186_sor_regs = {
3091 .head_state0 = 0x151,
3092 .head_state1 = 0x154,
3093 .head_state2 = 0x157,
3094 .head_state3 = 0x15a,
3095 .head_state4 = 0x15d,
3096 .head_state5 = 0x160,
3097 .pll0 = 0x163,
3098 .pll1 = 0x164,
3099 .pll2 = 0x165,
3100 .pll3 = 0x166,
3101 .dp_padctl0 = 0x168,
3102 .dp_padctl2 = 0x16a,
3103};
3104
3105static const struct tegra_sor_soc tegra186_sor = {
3106 .supports_edp = false,
3107 .supports_lvds = false,
3108 .supports_hdmi = false,
3109 .supports_dp = true,
3110
3111 .regs = &tegra186_sor_regs,
3112 .has_nvdisplay = true,
3113
3114 .xbar_cfg = tegra124_sor_xbar_cfg,
3115};
3116
3117static const struct tegra_sor_soc tegra186_sor1 = {
3118 .supports_edp = false,
3119 .supports_lvds = false,
3120 .supports_hdmi = true,
3121 .supports_dp = true,
3122
3123 .regs = &tegra186_sor_regs,
3124 .has_nvdisplay = true,
3125
3126 .num_settings = ARRAY_SIZE(tegra186_sor_hdmi_defaults),
3127 .settings = tegra186_sor_hdmi_defaults,
3128
3129 .xbar_cfg = tegra124_sor_xbar_cfg,
3130};
3131
Thierry Reding9b6c14b2018-09-21 12:27:46 +02003132static const struct tegra_sor_regs tegra194_sor_regs = {
3133 .head_state0 = 0x151,
3134 .head_state1 = 0x155,
3135 .head_state2 = 0x159,
3136 .head_state3 = 0x15d,
3137 .head_state4 = 0x161,
3138 .head_state5 = 0x165,
3139 .pll0 = 0x169,
3140 .pll1 = 0x16a,
3141 .pll2 = 0x16b,
3142 .pll3 = 0x16c,
3143 .dp_padctl0 = 0x16e,
3144 .dp_padctl2 = 0x16f,
3145};
3146
3147static const struct tegra_sor_soc tegra194_sor = {
3148 .supports_edp = true,
3149 .supports_lvds = false,
3150 .supports_hdmi = true,
3151 .supports_dp = true,
3152
3153 .regs = &tegra194_sor_regs,
3154 .has_nvdisplay = true,
3155
3156 .num_settings = ARRAY_SIZE(tegra194_sor_hdmi_defaults),
3157 .settings = tegra194_sor_hdmi_defaults,
3158
3159 .xbar_cfg = tegra210_sor_xbar_cfg,
3160};
3161
Thierry Reding459cc2c2015-07-30 10:34:24 +02003162static const struct of_device_id tegra_sor_of_match[] = {
Thierry Reding9b6c14b2018-09-21 12:27:46 +02003163 { .compatible = "nvidia,tegra194-sor", .data = &tegra194_sor },
Thierry Redingc57997b2017-10-12 19:12:57 +02003164 { .compatible = "nvidia,tegra186-sor1", .data = &tegra186_sor1 },
3165 { .compatible = "nvidia,tegra186-sor", .data = &tegra186_sor },
Thierry Reding459cc2c2015-07-30 10:34:24 +02003166 { .compatible = "nvidia,tegra210-sor1", .data = &tegra210_sor1 },
3167 { .compatible = "nvidia,tegra210-sor", .data = &tegra210_sor },
3168 { .compatible = "nvidia,tegra124-sor", .data = &tegra124_sor },
3169 { },
3170};
3171MODULE_DEVICE_TABLE(of, tegra_sor_of_match);
3172
Thierry Redingc57997b2017-10-12 19:12:57 +02003173static int tegra_sor_parse_dt(struct tegra_sor *sor)
3174{
3175 struct device_node *np = sor->dev->of_node;
Thierry Reding6d6c8152019-01-25 11:00:58 +01003176 u32 xbar_cfg[5];
3177 unsigned int i;
Thierry Redingc57997b2017-10-12 19:12:57 +02003178 u32 value;
3179 int err;
3180
3181 if (sor->soc->has_nvdisplay) {
3182 err = of_property_read_u32(np, "nvidia,interface", &value);
3183 if (err < 0)
3184 return err;
3185
3186 sor->index = value;
3187
3188 /*
3189 * override the default that we already set for Tegra210 and
3190 * earlier
3191 */
3192 sor->pad = TEGRA_IO_PAD_HDMI_DP0 + sor->index;
3193 }
3194
Thierry Reding6d6c8152019-01-25 11:00:58 +01003195 err = of_property_read_u32_array(np, "nvidia,xbar-cfg", xbar_cfg, 5);
3196 if (err < 0) {
3197 /* fall back to default per-SoC XBAR configuration */
3198 for (i = 0; i < 5; i++)
3199 sor->xbar_cfg[i] = sor->soc->xbar_cfg[i];
3200 } else {
3201 /* copy cells to SOR XBAR configuration */
3202 for (i = 0; i < 5; i++)
3203 sor->xbar_cfg[i] = xbar_cfg[i];
3204 }
3205
Thierry Redingc57997b2017-10-12 19:12:57 +02003206 return 0;
3207}
3208
Thierry Reding8e2988a72018-12-03 15:46:03 +01003209static irqreturn_t tegra_sor_irq(int irq, void *data)
3210{
3211 struct tegra_sor *sor = data;
3212 u32 value;
3213
3214 value = tegra_sor_readl(sor, SOR_INT_STATUS);
3215 tegra_sor_writel(sor, value, SOR_INT_STATUS);
3216
3217 if (value & SOR_INT_CODEC_SCRATCH0) {
3218 value = tegra_sor_readl(sor, SOR_AUDIO_HDA_CODEC_SCRATCH0);
3219
3220 if (value & SOR_AUDIO_HDA_CODEC_SCRATCH0_VALID) {
Thierry Redingcd54fb92019-01-03 15:23:15 +01003221 unsigned int format;
Thierry Reding8e2988a72018-12-03 15:46:03 +01003222
3223 format = value & SOR_AUDIO_HDA_CODEC_SCRATCH0_FMT_MASK;
3224
Thierry Redingfad7b802019-01-03 15:23:16 +01003225 tegra_hda_parse_format(format, &sor->format);
Thierry Reding8e2988a72018-12-03 15:46:03 +01003226
3227 tegra_sor_hdmi_audio_enable(sor);
3228 } else {
3229 tegra_sor_hdmi_audio_disable(sor);
3230 }
3231 }
3232
3233 return IRQ_HANDLED;
3234}
3235
Thierry Reding6b6b6042013-11-15 16:06:05 +01003236static int tegra_sor_probe(struct platform_device *pdev)
3237{
3238 struct device_node *np;
3239 struct tegra_sor *sor;
3240 struct resource *regs;
3241 int err;
3242
3243 sor = devm_kzalloc(&pdev->dev, sizeof(*sor), GFP_KERNEL);
3244 if (!sor)
3245 return -ENOMEM;
3246
Thierry Reding5faea3d2017-08-21 17:33:14 +02003247 sor->soc = of_device_get_match_data(&pdev->dev);
Thierry Reding6b6b6042013-11-15 16:06:05 +01003248 sor->output.dev = sor->dev = &pdev->dev;
Thierry Reding459cc2c2015-07-30 10:34:24 +02003249
3250 sor->settings = devm_kmemdup(&pdev->dev, sor->soc->settings,
3251 sor->soc->num_settings *
3252 sizeof(*sor->settings),
3253 GFP_KERNEL);
3254 if (!sor->settings)
3255 return -ENOMEM;
3256
3257 sor->num_settings = sor->soc->num_settings;
Thierry Reding6b6b6042013-11-15 16:06:05 +01003258
3259 np = of_parse_phandle(pdev->dev.of_node, "nvidia,dpaux", 0);
3260 if (np) {
Thierry Reding9542c232015-07-08 13:39:09 +02003261 sor->aux = drm_dp_aux_find_by_of_node(np);
Thierry Reding6b6b6042013-11-15 16:06:05 +01003262 of_node_put(np);
3263
Thierry Reding9542c232015-07-08 13:39:09 +02003264 if (!sor->aux)
Thierry Reding6b6b6042013-11-15 16:06:05 +01003265 return -EPROBE_DEFER;
3266 }
3267
Thierry Reding9542c232015-07-08 13:39:09 +02003268 if (!sor->aux) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02003269 if (sor->soc->supports_hdmi) {
3270 sor->ops = &tegra_sor_hdmi_ops;
Thierry Redingc57997b2017-10-12 19:12:57 +02003271 sor->pad = TEGRA_IO_PAD_HDMI;
Thierry Reding459cc2c2015-07-30 10:34:24 +02003272 } else if (sor->soc->supports_lvds) {
3273 dev_err(&pdev->dev, "LVDS not supported yet\n");
3274 return -ENODEV;
3275 } else {
3276 dev_err(&pdev->dev, "unknown (non-DP) support\n");
3277 return -ENODEV;
3278 }
3279 } else {
3280 if (sor->soc->supports_edp) {
3281 sor->ops = &tegra_sor_edp_ops;
Thierry Redingc57997b2017-10-12 19:12:57 +02003282 sor->pad = TEGRA_IO_PAD_LVDS;
Thierry Reding459cc2c2015-07-30 10:34:24 +02003283 } else if (sor->soc->supports_dp) {
3284 dev_err(&pdev->dev, "DisplayPort not supported yet\n");
3285 return -ENODEV;
3286 } else {
3287 dev_err(&pdev->dev, "unknown (DP) support\n");
3288 return -ENODEV;
3289 }
3290 }
3291
Thierry Redingc57997b2017-10-12 19:12:57 +02003292 err = tegra_sor_parse_dt(sor);
3293 if (err < 0)
3294 return err;
3295
Thierry Reding6b6b6042013-11-15 16:06:05 +01003296 err = tegra_output_probe(&sor->output);
Thierry Reding4dbdc742015-04-27 15:04:26 +02003297 if (err < 0) {
3298 dev_err(&pdev->dev, "failed to probe output: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01003299 return err;
Thierry Reding4dbdc742015-04-27 15:04:26 +02003300 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01003301
Thierry Reding459cc2c2015-07-30 10:34:24 +02003302 if (sor->ops && sor->ops->probe) {
3303 err = sor->ops->probe(sor);
3304 if (err < 0) {
3305 dev_err(&pdev->dev, "failed to probe %s: %d\n",
3306 sor->ops->name, err);
3307 goto output;
3308 }
3309 }
3310
Thierry Reding6b6b6042013-11-15 16:06:05 +01003311 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3312 sor->regs = devm_ioremap_resource(&pdev->dev, regs);
Thierry Reding459cc2c2015-07-30 10:34:24 +02003313 if (IS_ERR(sor->regs)) {
3314 err = PTR_ERR(sor->regs);
3315 goto remove;
3316 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01003317
Thierry Reding8e2988a72018-12-03 15:46:03 +01003318 err = platform_get_irq(pdev, 0);
3319 if (err < 0) {
3320 dev_err(&pdev->dev, "failed to get IRQ: %d\n", err);
3321 goto remove;
3322 }
3323
3324 sor->irq = err;
3325
3326 err = devm_request_irq(sor->dev, sor->irq, tegra_sor_irq, 0,
3327 dev_name(sor->dev), sor);
3328 if (err < 0) {
3329 dev_err(&pdev->dev, "failed to request IRQ: %d\n", err);
3330 goto remove;
3331 }
3332
Thierry Reding180b46e2018-12-06 18:56:47 +01003333 sor->rst = devm_reset_control_get(&pdev->dev, "sor");
3334 if (IS_ERR(sor->rst)) {
3335 err = PTR_ERR(sor->rst);
3336
3337 if (err != -EBUSY || WARN_ON(!pdev->dev.pm_domain)) {
Jon Hunterf8c79122016-07-01 14:21:38 +01003338 dev_err(&pdev->dev, "failed to get reset control: %d\n",
3339 err);
3340 goto remove;
3341 }
Thierry Reding180b46e2018-12-06 18:56:47 +01003342
3343 /*
3344 * At this point, the reset control is most likely being used
3345 * by the generic power domain implementation. With any luck
3346 * the power domain will have taken care of resetting the SOR
3347 * and we don't have to do anything.
3348 */
3349 sor->rst = NULL;
Thierry Reding4dbdc742015-04-27 15:04:26 +02003350 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01003351
3352 sor->clk = devm_clk_get(&pdev->dev, NULL);
Thierry Reding4dbdc742015-04-27 15:04:26 +02003353 if (IS_ERR(sor->clk)) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02003354 err = PTR_ERR(sor->clk);
3355 dev_err(&pdev->dev, "failed to get module clock: %d\n", err);
3356 goto remove;
Thierry Reding4dbdc742015-04-27 15:04:26 +02003357 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01003358
Thierry Reding618dee32016-06-09 17:53:57 +02003359 if (sor->soc->supports_hdmi || sor->soc->supports_dp) {
Thierry Redinge1335e22017-10-12 17:53:11 +02003360 struct device_node *np = pdev->dev.of_node;
3361 const char *name;
3362
3363 /*
3364 * For backwards compatibility with Tegra210 device trees,
3365 * fall back to the old clock name "source" if the new "out"
3366 * clock is not available.
3367 */
3368 if (of_property_match_string(np, "clock-names", "out") < 0)
3369 name = "source";
3370 else
3371 name = "out";
3372
3373 sor->clk_out = devm_clk_get(&pdev->dev, name);
3374 if (IS_ERR(sor->clk_out)) {
3375 err = PTR_ERR(sor->clk_out);
3376 dev_err(sor->dev, "failed to get %s clock: %d\n",
3377 name, err);
Thierry Reding618dee32016-06-09 17:53:57 +02003378 goto remove;
3379 }
Thierry Reding1087fac2017-12-14 13:37:53 +01003380 } else {
Thierry Redingd7805372018-01-10 13:04:58 +01003381 /* fall back to the module clock on SOR0 (eDP/LVDS only) */
Thierry Reding1087fac2017-12-14 13:37:53 +01003382 sor->clk_out = sor->clk;
Thierry Reding618dee32016-06-09 17:53:57 +02003383 }
3384
Thierry Reding6b6b6042013-11-15 16:06:05 +01003385 sor->clk_parent = devm_clk_get(&pdev->dev, "parent");
Thierry Reding4dbdc742015-04-27 15:04:26 +02003386 if (IS_ERR(sor->clk_parent)) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02003387 err = PTR_ERR(sor->clk_parent);
3388 dev_err(&pdev->dev, "failed to get parent clock: %d\n", err);
3389 goto remove;
Thierry Reding4dbdc742015-04-27 15:04:26 +02003390 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01003391
Thierry Reding6b6b6042013-11-15 16:06:05 +01003392 sor->clk_safe = devm_clk_get(&pdev->dev, "safe");
Thierry Reding4dbdc742015-04-27 15:04:26 +02003393 if (IS_ERR(sor->clk_safe)) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02003394 err = PTR_ERR(sor->clk_safe);
3395 dev_err(&pdev->dev, "failed to get safe clock: %d\n", err);
3396 goto remove;
Thierry Reding4dbdc742015-04-27 15:04:26 +02003397 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01003398
Thierry Reding6b6b6042013-11-15 16:06:05 +01003399 sor->clk_dp = devm_clk_get(&pdev->dev, "dp");
Thierry Reding4dbdc742015-04-27 15:04:26 +02003400 if (IS_ERR(sor->clk_dp)) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02003401 err = PTR_ERR(sor->clk_dp);
3402 dev_err(&pdev->dev, "failed to get DP clock: %d\n", err);
3403 goto remove;
Thierry Reding4dbdc742015-04-27 15:04:26 +02003404 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01003405
Thierry Redinge1335e22017-10-12 17:53:11 +02003406 /*
3407 * Starting with Tegra186, the BPMP provides an implementation for
3408 * the pad output clock, so we have to look it up from device tree.
3409 */
3410 sor->clk_pad = devm_clk_get(&pdev->dev, "pad");
3411 if (IS_ERR(sor->clk_pad)) {
3412 if (sor->clk_pad != ERR_PTR(-ENOENT)) {
3413 err = PTR_ERR(sor->clk_pad);
3414 goto remove;
3415 }
3416
3417 /*
3418 * If the pad output clock is not available, then we assume
3419 * we're on Tegra210 or earlier and have to provide our own
3420 * implementation.
3421 */
3422 sor->clk_pad = NULL;
3423 }
3424
3425 /*
3426 * The bootloader may have set up the SOR such that it's module clock
3427 * is sourced by one of the display PLLs. However, that doesn't work
3428 * without properly having set up other bits of the SOR.
3429 */
3430 err = clk_set_parent(sor->clk_out, sor->clk_safe);
3431 if (err < 0) {
3432 dev_err(&pdev->dev, "failed to use safe clock: %d\n", err);
3433 goto remove;
3434 }
3435
Thierry Redingaaff8bd2015-08-07 16:04:54 +02003436 platform_set_drvdata(pdev, sor);
3437 pm_runtime_enable(&pdev->dev);
3438
Thierry Redinge1335e22017-10-12 17:53:11 +02003439 /*
3440 * On Tegra210 and earlier, provide our own implementation for the
3441 * pad output clock.
3442 */
3443 if (!sor->clk_pad) {
3444 err = pm_runtime_get_sync(&pdev->dev);
3445 if (err < 0) {
3446 dev_err(&pdev->dev, "failed to get runtime PM: %d\n",
3447 err);
3448 goto remove;
3449 }
Thierry Redingb2992212015-10-01 14:25:03 +02003450
Thierry Redinge1335e22017-10-12 17:53:11 +02003451 sor->clk_pad = tegra_clk_sor_pad_register(sor,
3452 "sor1_pad_clkout");
3453 pm_runtime_put(&pdev->dev);
3454 }
3455
3456 if (IS_ERR(sor->clk_pad)) {
3457 err = PTR_ERR(sor->clk_pad);
3458 dev_err(&pdev->dev, "failed to register SOR pad clock: %d\n",
3459 err);
Thierry Redingb2992212015-10-01 14:25:03 +02003460 goto remove;
3461 }
3462
Thierry Reding6b6b6042013-11-15 16:06:05 +01003463 INIT_LIST_HEAD(&sor->client.list);
3464 sor->client.ops = &sor_client_ops;
3465 sor->client.dev = &pdev->dev;
3466
Thierry Reding6b6b6042013-11-15 16:06:05 +01003467 err = host1x_client_register(&sor->client);
3468 if (err < 0) {
3469 dev_err(&pdev->dev, "failed to register host1x client: %d\n",
3470 err);
Thierry Reding459cc2c2015-07-30 10:34:24 +02003471 goto remove;
Thierry Reding6b6b6042013-11-15 16:06:05 +01003472 }
3473
Thierry Reding6b6b6042013-11-15 16:06:05 +01003474 return 0;
Thierry Reding459cc2c2015-07-30 10:34:24 +02003475
3476remove:
3477 if (sor->ops && sor->ops->remove)
3478 sor->ops->remove(sor);
3479output:
3480 tegra_output_remove(&sor->output);
3481 return err;
Thierry Reding6b6b6042013-11-15 16:06:05 +01003482}
3483
3484static int tegra_sor_remove(struct platform_device *pdev)
3485{
3486 struct tegra_sor *sor = platform_get_drvdata(pdev);
3487 int err;
3488
Thierry Redingaaff8bd2015-08-07 16:04:54 +02003489 pm_runtime_disable(&pdev->dev);
3490
Thierry Reding6b6b6042013-11-15 16:06:05 +01003491 err = host1x_client_unregister(&sor->client);
3492 if (err < 0) {
3493 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
3494 err);
3495 return err;
3496 }
3497
Thierry Reding459cc2c2015-07-30 10:34:24 +02003498 if (sor->ops && sor->ops->remove) {
3499 err = sor->ops->remove(sor);
3500 if (err < 0)
3501 dev_err(&pdev->dev, "failed to remove SOR: %d\n", err);
3502 }
3503
Thierry Reding328ec692014-12-19 15:55:08 +01003504 tegra_output_remove(&sor->output);
Thierry Reding6b6b6042013-11-15 16:06:05 +01003505
3506 return 0;
3507}
3508
Thierry Redingaaff8bd2015-08-07 16:04:54 +02003509#ifdef CONFIG_PM
3510static int tegra_sor_suspend(struct device *dev)
3511{
3512 struct tegra_sor *sor = dev_get_drvdata(dev);
3513 int err;
3514
Jon Hunterf8c79122016-07-01 14:21:38 +01003515 if (sor->rst) {
3516 err = reset_control_assert(sor->rst);
3517 if (err < 0) {
3518 dev_err(dev, "failed to assert reset: %d\n", err);
3519 return err;
3520 }
Thierry Redingaaff8bd2015-08-07 16:04:54 +02003521 }
3522
3523 usleep_range(1000, 2000);
3524
3525 clk_disable_unprepare(sor->clk);
3526
3527 return 0;
3528}
3529
3530static int tegra_sor_resume(struct device *dev)
3531{
3532 struct tegra_sor *sor = dev_get_drvdata(dev);
3533 int err;
3534
3535 err = clk_prepare_enable(sor->clk);
3536 if (err < 0) {
3537 dev_err(dev, "failed to enable clock: %d\n", err);
3538 return err;
3539 }
3540
3541 usleep_range(1000, 2000);
3542
Jon Hunterf8c79122016-07-01 14:21:38 +01003543 if (sor->rst) {
3544 err = reset_control_deassert(sor->rst);
3545 if (err < 0) {
3546 dev_err(dev, "failed to deassert reset: %d\n", err);
3547 clk_disable_unprepare(sor->clk);
3548 return err;
3549 }
Thierry Redingaaff8bd2015-08-07 16:04:54 +02003550 }
3551
3552 return 0;
3553}
3554#endif
3555
3556static const struct dev_pm_ops tegra_sor_pm_ops = {
3557 SET_RUNTIME_PM_OPS(tegra_sor_suspend, tegra_sor_resume, NULL)
3558};
3559
Thierry Reding6b6b6042013-11-15 16:06:05 +01003560struct platform_driver tegra_sor_driver = {
3561 .driver = {
3562 .name = "tegra-sor",
3563 .of_match_table = tegra_sor_of_match,
Thierry Redingaaff8bd2015-08-07 16:04:54 +02003564 .pm = &tegra_sor_pm_ops,
Thierry Reding6b6b6042013-11-15 16:06:05 +01003565 },
3566 .probe = tegra_sor_probe,
3567 .remove = tegra_sor_remove,
3568};