blob: 79602debf2311893f596fcb262432ecc119c6cfb [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 Reding9542c232015-07-08 13:39:09 +0200413 struct drm_dp_aux *aux;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100414
Thierry Redingdab16332015-01-26 16:04:08 +0100415 struct drm_info_list *debugfs_files;
Thierry Reding459cc2c2015-07-30 10:34:24 +0200416
417 const struct tegra_sor_ops *ops;
Thierry Redingc57997b2017-10-12 19:12:57 +0200418 enum tegra_io_pad pad;
Thierry Reding459cc2c2015-07-30 10:34:24 +0200419
420 /* for HDMI 2.0 */
421 struct tegra_sor_hdmi_settings *settings;
422 unsigned int num_settings;
423
424 struct regulator *avdd_io_supply;
425 struct regulator *vdd_pll_supply;
426 struct regulator *hdmi_supply;
Thierry Reding36e90222017-10-12 19:14:21 +0200427
428 struct delayed_work scdc;
429 bool scdc_enabled;
Thierry Reding8e2988a72018-12-03 15:46:03 +0100430
Thierry Redingfad7b802019-01-03 15:23:16 +0100431 struct tegra_hda_format format;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100432};
433
Thierry Redingc31efa72015-09-08 16:09:22 +0200434struct tegra_sor_state {
435 struct drm_connector_state base;
436
Thierry Reding36e90222017-10-12 19:14:21 +0200437 unsigned int link_speed;
438 unsigned long pclk;
Thierry Redingc31efa72015-09-08 16:09:22 +0200439 unsigned int bpc;
440};
441
442static inline struct tegra_sor_state *
443to_sor_state(struct drm_connector_state *state)
444{
445 return container_of(state, struct tegra_sor_state, base);
446}
447
Thierry Reding34fa1832014-06-05 16:31:10 +0200448struct tegra_sor_config {
449 u32 bits_per_pixel;
450
451 u32 active_polarity;
452 u32 active_count;
453 u32 tu_size;
454 u32 active_frac;
455 u32 watermark;
Thierry Reding7890b572014-06-05 16:12:46 +0200456
457 u32 hblank_symbols;
458 u32 vblank_symbols;
Thierry Reding34fa1832014-06-05 16:31:10 +0200459};
460
Thierry Reding6b6b6042013-11-15 16:06:05 +0100461static inline struct tegra_sor *
462host1x_client_to_sor(struct host1x_client *client)
463{
464 return container_of(client, struct tegra_sor, client);
465}
466
467static inline struct tegra_sor *to_sor(struct tegra_output *output)
468{
469 return container_of(output, struct tegra_sor, output);
470}
471
Thierry Reding5c5f1302017-08-15 15:41:09 +0200472static inline u32 tegra_sor_readl(struct tegra_sor *sor, unsigned int offset)
Thierry Reding6b6b6042013-11-15 16:06:05 +0100473{
Thierry Reding932f6522017-08-15 15:41:14 +0200474 u32 value = readl(sor->regs + (offset << 2));
475
476 trace_sor_readl(sor->dev, offset, value);
477
478 return value;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100479}
480
Thierry Reding28fe2072015-01-26 16:02:48 +0100481static inline void tegra_sor_writel(struct tegra_sor *sor, u32 value,
Thierry Reding5c5f1302017-08-15 15:41:09 +0200482 unsigned int offset)
Thierry Reding6b6b6042013-11-15 16:06:05 +0100483{
Thierry Reding932f6522017-08-15 15:41:14 +0200484 trace_sor_writel(sor->dev, offset, value);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100485 writel(value, sor->regs + (offset << 2));
486}
487
Thierry Reding25bb2ce2015-08-03 14:23:29 +0200488static int tegra_sor_set_parent_clock(struct tegra_sor *sor, struct clk *parent)
489{
490 int err;
491
492 clk_disable_unprepare(sor->clk);
493
Thierry Redinge1335e22017-10-12 17:53:11 +0200494 err = clk_set_parent(sor->clk_out, parent);
Thierry Reding25bb2ce2015-08-03 14:23:29 +0200495 if (err < 0)
496 return err;
497
498 err = clk_prepare_enable(sor->clk);
499 if (err < 0)
500 return err;
501
502 return 0;
503}
504
Thierry Redinge1335e22017-10-12 17:53:11 +0200505struct tegra_clk_sor_pad {
Thierry Redingb2992212015-10-01 14:25:03 +0200506 struct clk_hw hw;
507 struct tegra_sor *sor;
508};
509
Thierry Redinge1335e22017-10-12 17:53:11 +0200510static inline struct tegra_clk_sor_pad *to_pad(struct clk_hw *hw)
Thierry Redingb2992212015-10-01 14:25:03 +0200511{
Thierry Redinge1335e22017-10-12 17:53:11 +0200512 return container_of(hw, struct tegra_clk_sor_pad, hw);
Thierry Redingb2992212015-10-01 14:25:03 +0200513}
514
Thierry Redinge1335e22017-10-12 17:53:11 +0200515static const char * const tegra_clk_sor_pad_parents[] = {
Thierry Redingb2992212015-10-01 14:25:03 +0200516 "pll_d2_out0", "pll_dp"
517};
518
Thierry Redinge1335e22017-10-12 17:53:11 +0200519static int tegra_clk_sor_pad_set_parent(struct clk_hw *hw, u8 index)
Thierry Redingb2992212015-10-01 14:25:03 +0200520{
Thierry Redinge1335e22017-10-12 17:53:11 +0200521 struct tegra_clk_sor_pad *pad = to_pad(hw);
522 struct tegra_sor *sor = pad->sor;
Thierry Redingb2992212015-10-01 14:25:03 +0200523 u32 value;
524
525 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
526 value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
527
528 switch (index) {
529 case 0:
530 value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK;
531 break;
532
533 case 1:
534 value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
535 break;
536 }
537
538 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
539
540 return 0;
541}
542
Thierry Redinge1335e22017-10-12 17:53:11 +0200543static u8 tegra_clk_sor_pad_get_parent(struct clk_hw *hw)
Thierry Redingb2992212015-10-01 14:25:03 +0200544{
Thierry Redinge1335e22017-10-12 17:53:11 +0200545 struct tegra_clk_sor_pad *pad = to_pad(hw);
546 struct tegra_sor *sor = pad->sor;
Thierry Redingb2992212015-10-01 14:25:03 +0200547 u8 parent = U8_MAX;
548 u32 value;
549
550 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
551
552 switch (value & SOR_CLK_CNTRL_DP_CLK_SEL_MASK) {
553 case SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK:
554 case SOR_CLK_CNTRL_DP_CLK_SEL_DIFF_PCLK:
555 parent = 0;
556 break;
557
558 case SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK:
559 case SOR_CLK_CNTRL_DP_CLK_SEL_DIFF_DPCLK:
560 parent = 1;
561 break;
562 }
563
564 return parent;
565}
566
Thierry Redinge1335e22017-10-12 17:53:11 +0200567static const struct clk_ops tegra_clk_sor_pad_ops = {
568 .set_parent = tegra_clk_sor_pad_set_parent,
569 .get_parent = tegra_clk_sor_pad_get_parent,
Thierry Redingb2992212015-10-01 14:25:03 +0200570};
571
Thierry Redinge1335e22017-10-12 17:53:11 +0200572static struct clk *tegra_clk_sor_pad_register(struct tegra_sor *sor,
573 const char *name)
Thierry Redingb2992212015-10-01 14:25:03 +0200574{
Thierry Redinge1335e22017-10-12 17:53:11 +0200575 struct tegra_clk_sor_pad *pad;
Thierry Redingb2992212015-10-01 14:25:03 +0200576 struct clk_init_data init;
577 struct clk *clk;
578
Thierry Redinge1335e22017-10-12 17:53:11 +0200579 pad = devm_kzalloc(sor->dev, sizeof(*pad), GFP_KERNEL);
580 if (!pad)
Thierry Redingb2992212015-10-01 14:25:03 +0200581 return ERR_PTR(-ENOMEM);
582
Thierry Redinge1335e22017-10-12 17:53:11 +0200583 pad->sor = sor;
Thierry Redingb2992212015-10-01 14:25:03 +0200584
585 init.name = name;
586 init.flags = 0;
Thierry Redinge1335e22017-10-12 17:53:11 +0200587 init.parent_names = tegra_clk_sor_pad_parents;
588 init.num_parents = ARRAY_SIZE(tegra_clk_sor_pad_parents);
589 init.ops = &tegra_clk_sor_pad_ops;
Thierry Redingb2992212015-10-01 14:25:03 +0200590
Thierry Redinge1335e22017-10-12 17:53:11 +0200591 pad->hw.init = &init;
Thierry Redingb2992212015-10-01 14:25:03 +0200592
Thierry Redinge1335e22017-10-12 17:53:11 +0200593 clk = devm_clk_register(sor->dev, &pad->hw);
Thierry Redingb2992212015-10-01 14:25:03 +0200594
595 return clk;
596}
597
Thierry Reding6b6b6042013-11-15 16:06:05 +0100598static int tegra_sor_dp_train_fast(struct tegra_sor *sor,
599 struct drm_dp_link *link)
600{
Thierry Reding6b6b6042013-11-15 16:06:05 +0100601 unsigned int i;
602 u8 pattern;
Thierry Reding28fe2072015-01-26 16:02:48 +0100603 u32 value;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100604 int err;
605
606 /* setup lane parameters */
607 value = SOR_LANE_DRIVE_CURRENT_LANE3(0x40) |
608 SOR_LANE_DRIVE_CURRENT_LANE2(0x40) |
609 SOR_LANE_DRIVE_CURRENT_LANE1(0x40) |
610 SOR_LANE_DRIVE_CURRENT_LANE0(0x40);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200611 tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100612
613 value = SOR_LANE_PREEMPHASIS_LANE3(0x0f) |
614 SOR_LANE_PREEMPHASIS_LANE2(0x0f) |
615 SOR_LANE_PREEMPHASIS_LANE1(0x0f) |
616 SOR_LANE_PREEMPHASIS_LANE0(0x0f);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200617 tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100618
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200619 value = SOR_LANE_POSTCURSOR_LANE3(0x00) |
620 SOR_LANE_POSTCURSOR_LANE2(0x00) |
621 SOR_LANE_POSTCURSOR_LANE1(0x00) |
622 SOR_LANE_POSTCURSOR_LANE0(0x00);
623 tegra_sor_writel(sor, value, SOR_LANE_POSTCURSOR0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100624
625 /* disable LVDS mode */
626 tegra_sor_writel(sor, 0, SOR_LVDS);
627
Thierry Reding880cee02017-10-12 19:04:17 +0200628 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100629 value |= SOR_DP_PADCTL_TX_PU_ENABLE;
630 value &= ~SOR_DP_PADCTL_TX_PU_MASK;
631 value |= SOR_DP_PADCTL_TX_PU(2); /* XXX: don't hardcode? */
Thierry Reding880cee02017-10-12 19:04:17 +0200632 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100633
Thierry Reding880cee02017-10-12 19:04:17 +0200634 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100635 value |= SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
636 SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0;
Thierry Reding880cee02017-10-12 19:04:17 +0200637 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100638
639 usleep_range(10, 100);
640
Thierry Reding880cee02017-10-12 19:04:17 +0200641 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100642 value &= ~(SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
643 SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0);
Thierry Reding880cee02017-10-12 19:04:17 +0200644 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100645
Thierry Reding9542c232015-07-08 13:39:09 +0200646 err = drm_dp_aux_prepare(sor->aux, DP_SET_ANSI_8B10B);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100647 if (err < 0)
648 return err;
649
650 for (i = 0, value = 0; i < link->num_lanes; i++) {
651 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
652 SOR_DP_TPG_SCRAMBLER_NONE |
653 SOR_DP_TPG_PATTERN_TRAIN1;
654 value = (value << 8) | lane;
655 }
656
657 tegra_sor_writel(sor, value, SOR_DP_TPG);
658
659 pattern = DP_TRAINING_PATTERN_1;
660
Thierry Reding9542c232015-07-08 13:39:09 +0200661 err = drm_dp_aux_train(sor->aux, link, pattern);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100662 if (err < 0)
663 return err;
664
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200665 value = tegra_sor_readl(sor, SOR_DP_SPARE0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100666 value |= SOR_DP_SPARE_SEQ_ENABLE;
667 value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
668 value |= SOR_DP_SPARE_MACRO_SOR_CLK;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200669 tegra_sor_writel(sor, value, SOR_DP_SPARE0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100670
671 for (i = 0, value = 0; i < link->num_lanes; i++) {
672 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
673 SOR_DP_TPG_SCRAMBLER_NONE |
674 SOR_DP_TPG_PATTERN_TRAIN2;
675 value = (value << 8) | lane;
676 }
677
678 tegra_sor_writel(sor, value, SOR_DP_TPG);
679
680 pattern = DP_LINK_SCRAMBLING_DISABLE | DP_TRAINING_PATTERN_2;
681
Thierry Reding9542c232015-07-08 13:39:09 +0200682 err = drm_dp_aux_train(sor->aux, link, pattern);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100683 if (err < 0)
684 return err;
685
686 for (i = 0, value = 0; i < link->num_lanes; i++) {
687 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
688 SOR_DP_TPG_SCRAMBLER_GALIOS |
689 SOR_DP_TPG_PATTERN_NONE;
690 value = (value << 8) | lane;
691 }
692
693 tegra_sor_writel(sor, value, SOR_DP_TPG);
694
695 pattern = DP_TRAINING_PATTERN_DISABLE;
696
Thierry Reding9542c232015-07-08 13:39:09 +0200697 err = drm_dp_aux_train(sor->aux, link, pattern);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100698 if (err < 0)
699 return err;
700
701 return 0;
702}
703
704static void tegra_sor_super_update(struct tegra_sor *sor)
705{
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200706 tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
707 tegra_sor_writel(sor, 1, SOR_SUPER_STATE0);
708 tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100709}
710
711static void tegra_sor_update(struct tegra_sor *sor)
712{
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200713 tegra_sor_writel(sor, 0, SOR_STATE0);
714 tegra_sor_writel(sor, 1, SOR_STATE0);
715 tegra_sor_writel(sor, 0, SOR_STATE0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100716}
717
718static int tegra_sor_setup_pwm(struct tegra_sor *sor, unsigned long timeout)
719{
Thierry Reding28fe2072015-01-26 16:02:48 +0100720 u32 value;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100721
722 value = tegra_sor_readl(sor, SOR_PWM_DIV);
723 value &= ~SOR_PWM_DIV_MASK;
724 value |= 0x400; /* period */
725 tegra_sor_writel(sor, value, SOR_PWM_DIV);
726
727 value = tegra_sor_readl(sor, SOR_PWM_CTL);
728 value &= ~SOR_PWM_CTL_DUTY_CYCLE_MASK;
729 value |= 0x400; /* duty cycle */
730 value &= ~SOR_PWM_CTL_CLK_SEL; /* clock source: PCLK */
731 value |= SOR_PWM_CTL_TRIGGER;
732 tegra_sor_writel(sor, value, SOR_PWM_CTL);
733
734 timeout = jiffies + msecs_to_jiffies(timeout);
735
736 while (time_before(jiffies, timeout)) {
737 value = tegra_sor_readl(sor, SOR_PWM_CTL);
738 if ((value & SOR_PWM_CTL_TRIGGER) == 0)
739 return 0;
740
741 usleep_range(25, 100);
742 }
743
744 return -ETIMEDOUT;
745}
746
747static int tegra_sor_attach(struct tegra_sor *sor)
748{
749 unsigned long value, timeout;
750
751 /* wake up in normal mode */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200752 value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100753 value |= SOR_SUPER_STATE_HEAD_MODE_AWAKE;
754 value |= SOR_SUPER_STATE_MODE_NORMAL;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200755 tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100756 tegra_sor_super_update(sor);
757
758 /* attach */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200759 value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100760 value |= SOR_SUPER_STATE_ATTACHED;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200761 tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100762 tegra_sor_super_update(sor);
763
764 timeout = jiffies + msecs_to_jiffies(250);
765
766 while (time_before(jiffies, timeout)) {
767 value = tegra_sor_readl(sor, SOR_TEST);
768 if ((value & SOR_TEST_ATTACHED) != 0)
769 return 0;
770
771 usleep_range(25, 100);
772 }
773
774 return -ETIMEDOUT;
775}
776
777static int tegra_sor_wakeup(struct tegra_sor *sor)
778{
Thierry Reding6b6b6042013-11-15 16:06:05 +0100779 unsigned long value, timeout;
780
Thierry Reding6b6b6042013-11-15 16:06:05 +0100781 timeout = jiffies + msecs_to_jiffies(250);
782
783 /* wait for head to wake up */
784 while (time_before(jiffies, timeout)) {
785 value = tegra_sor_readl(sor, SOR_TEST);
786 value &= SOR_TEST_HEAD_MODE_MASK;
787
788 if (value == SOR_TEST_HEAD_MODE_AWAKE)
789 return 0;
790
791 usleep_range(25, 100);
792 }
793
794 return -ETIMEDOUT;
795}
796
797static int tegra_sor_power_up(struct tegra_sor *sor, unsigned long timeout)
798{
Thierry Reding28fe2072015-01-26 16:02:48 +0100799 u32 value;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100800
801 value = tegra_sor_readl(sor, SOR_PWR);
802 value |= SOR_PWR_TRIGGER | SOR_PWR_NORMAL_STATE_PU;
803 tegra_sor_writel(sor, value, SOR_PWR);
804
805 timeout = jiffies + msecs_to_jiffies(timeout);
806
807 while (time_before(jiffies, timeout)) {
808 value = tegra_sor_readl(sor, SOR_PWR);
809 if ((value & SOR_PWR_TRIGGER) == 0)
810 return 0;
811
812 usleep_range(25, 100);
813 }
814
815 return -ETIMEDOUT;
816}
817
Thierry Reding34fa1832014-06-05 16:31:10 +0200818struct tegra_sor_params {
819 /* number of link clocks per line */
820 unsigned int num_clocks;
821 /* ratio between input and output */
822 u64 ratio;
823 /* precision factor */
824 u64 precision;
825
826 unsigned int active_polarity;
827 unsigned int active_count;
828 unsigned int active_frac;
829 unsigned int tu_size;
830 unsigned int error;
831};
832
833static int tegra_sor_compute_params(struct tegra_sor *sor,
834 struct tegra_sor_params *params,
835 unsigned int tu_size)
836{
837 u64 active_sym, active_count, frac, approx;
838 u32 active_polarity, active_frac = 0;
839 const u64 f = params->precision;
840 s64 error;
841
842 active_sym = params->ratio * tu_size;
843 active_count = div_u64(active_sym, f) * f;
844 frac = active_sym - active_count;
845
846 /* fraction < 0.5 */
847 if (frac >= (f / 2)) {
848 active_polarity = 1;
849 frac = f - frac;
850 } else {
851 active_polarity = 0;
852 }
853
854 if (frac != 0) {
855 frac = div_u64(f * f, frac); /* 1/fraction */
856 if (frac <= (15 * f)) {
857 active_frac = div_u64(frac, f);
858
859 /* round up */
860 if (active_polarity)
861 active_frac++;
862 } else {
863 active_frac = active_polarity ? 1 : 15;
864 }
865 }
866
867 if (active_frac == 1)
868 active_polarity = 0;
869
870 if (active_polarity == 1) {
871 if (active_frac) {
872 approx = active_count + (active_frac * (f - 1)) * f;
873 approx = div_u64(approx, active_frac * f);
874 } else {
875 approx = active_count + f;
876 }
877 } else {
878 if (active_frac)
879 approx = active_count + div_u64(f, active_frac);
880 else
881 approx = active_count;
882 }
883
884 error = div_s64(active_sym - approx, tu_size);
885 error *= params->num_clocks;
886
Andrew Morton79211c82015-11-09 14:58:13 -0800887 if (error <= 0 && abs(error) < params->error) {
Thierry Reding34fa1832014-06-05 16:31:10 +0200888 params->active_count = div_u64(active_count, f);
889 params->active_polarity = active_polarity;
890 params->active_frac = active_frac;
Andrew Morton79211c82015-11-09 14:58:13 -0800891 params->error = abs(error);
Thierry Reding34fa1832014-06-05 16:31:10 +0200892 params->tu_size = tu_size;
893
894 if (error == 0)
895 return true;
896 }
897
898 return false;
899}
900
Thierry Redinga1983592015-07-21 16:46:52 +0200901static int tegra_sor_compute_config(struct tegra_sor *sor,
902 const struct drm_display_mode *mode,
903 struct tegra_sor_config *config,
904 struct drm_dp_link *link)
Thierry Reding34fa1832014-06-05 16:31:10 +0200905{
906 const u64 f = 100000, link_rate = link->rate * 1000;
907 const u64 pclk = mode->clock * 1000;
Thierry Reding7890b572014-06-05 16:12:46 +0200908 u64 input, output, watermark, num;
Thierry Reding34fa1832014-06-05 16:31:10 +0200909 struct tegra_sor_params params;
Thierry Reding34fa1832014-06-05 16:31:10 +0200910 u32 num_syms_per_line;
911 unsigned int i;
912
913 if (!link_rate || !link->num_lanes || !pclk || !config->bits_per_pixel)
914 return -EINVAL;
915
916 output = link_rate * 8 * link->num_lanes;
917 input = pclk * config->bits_per_pixel;
918
919 if (input >= output)
920 return -ERANGE;
921
922 memset(&params, 0, sizeof(params));
923 params.ratio = div64_u64(input * f, output);
924 params.num_clocks = div_u64(link_rate * mode->hdisplay, pclk);
925 params.precision = f;
926 params.error = 64 * f;
927 params.tu_size = 64;
928
929 for (i = params.tu_size; i >= 32; i--)
930 if (tegra_sor_compute_params(sor, &params, i))
931 break;
932
933 if (params.active_frac == 0) {
934 config->active_polarity = 0;
935 config->active_count = params.active_count;
936
937 if (!params.active_polarity)
938 config->active_count--;
939
940 config->tu_size = params.tu_size;
941 config->active_frac = 1;
942 } else {
943 config->active_polarity = params.active_polarity;
944 config->active_count = params.active_count;
945 config->active_frac = params.active_frac;
946 config->tu_size = params.tu_size;
947 }
948
949 dev_dbg(sor->dev,
950 "polarity: %d active count: %d tu size: %d active frac: %d\n",
951 config->active_polarity, config->active_count,
952 config->tu_size, config->active_frac);
953
954 watermark = params.ratio * config->tu_size * (f - params.ratio);
955 watermark = div_u64(watermark, f);
956
957 watermark = div_u64(watermark + params.error, f);
958 config->watermark = watermark + (config->bits_per_pixel / 8) + 2;
959 num_syms_per_line = (mode->hdisplay * config->bits_per_pixel) *
960 (link->num_lanes * 8);
961
962 if (config->watermark > 30) {
963 config->watermark = 30;
964 dev_err(sor->dev,
965 "unable to compute TU size, forcing watermark to %u\n",
966 config->watermark);
967 } else if (config->watermark > num_syms_per_line) {
968 config->watermark = num_syms_per_line;
969 dev_err(sor->dev, "watermark too high, forcing to %u\n",
970 config->watermark);
971 }
972
Thierry Reding7890b572014-06-05 16:12:46 +0200973 /* compute the number of symbols per horizontal blanking interval */
974 num = ((mode->htotal - mode->hdisplay) - 7) * link_rate;
975 config->hblank_symbols = div_u64(num, pclk);
976
977 if (link->capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
978 config->hblank_symbols -= 3;
979
980 config->hblank_symbols -= 12 / link->num_lanes;
981
982 /* compute the number of symbols per vertical blanking interval */
983 num = (mode->hdisplay - 25) * link_rate;
984 config->vblank_symbols = div_u64(num, pclk);
985 config->vblank_symbols -= 36 / link->num_lanes + 4;
986
987 dev_dbg(sor->dev, "blank symbols: H:%u V:%u\n", config->hblank_symbols,
988 config->vblank_symbols);
989
Thierry Reding34fa1832014-06-05 16:31:10 +0200990 return 0;
991}
992
Thierry Reding402f6bc2015-07-21 16:48:19 +0200993static void tegra_sor_apply_config(struct tegra_sor *sor,
994 const struct tegra_sor_config *config)
995{
996 u32 value;
997
998 value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
999 value &= ~SOR_DP_LINKCTL_TU_SIZE_MASK;
1000 value |= SOR_DP_LINKCTL_TU_SIZE(config->tu_size);
1001 tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
1002
1003 value = tegra_sor_readl(sor, SOR_DP_CONFIG0);
1004 value &= ~SOR_DP_CONFIG_WATERMARK_MASK;
1005 value |= SOR_DP_CONFIG_WATERMARK(config->watermark);
1006
1007 value &= ~SOR_DP_CONFIG_ACTIVE_SYM_COUNT_MASK;
1008 value |= SOR_DP_CONFIG_ACTIVE_SYM_COUNT(config->active_count);
1009
1010 value &= ~SOR_DP_CONFIG_ACTIVE_SYM_FRAC_MASK;
1011 value |= SOR_DP_CONFIG_ACTIVE_SYM_FRAC(config->active_frac);
1012
1013 if (config->active_polarity)
1014 value |= SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
1015 else
1016 value &= ~SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
1017
1018 value |= SOR_DP_CONFIG_ACTIVE_SYM_ENABLE;
1019 value |= SOR_DP_CONFIG_DISPARITY_NEGATIVE;
1020 tegra_sor_writel(sor, value, SOR_DP_CONFIG0);
1021
1022 value = tegra_sor_readl(sor, SOR_DP_AUDIO_HBLANK_SYMBOLS);
1023 value &= ~SOR_DP_AUDIO_HBLANK_SYMBOLS_MASK;
1024 value |= config->hblank_symbols & 0xffff;
1025 tegra_sor_writel(sor, value, SOR_DP_AUDIO_HBLANK_SYMBOLS);
1026
1027 value = tegra_sor_readl(sor, SOR_DP_AUDIO_VBLANK_SYMBOLS);
1028 value &= ~SOR_DP_AUDIO_VBLANK_SYMBOLS_MASK;
1029 value |= config->vblank_symbols & 0xffff;
1030 tegra_sor_writel(sor, value, SOR_DP_AUDIO_VBLANK_SYMBOLS);
1031}
1032
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001033static void tegra_sor_mode_set(struct tegra_sor *sor,
1034 const struct drm_display_mode *mode,
Thierry Redingc31efa72015-09-08 16:09:22 +02001035 struct tegra_sor_state *state)
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001036{
1037 struct tegra_dc *dc = to_tegra_dc(sor->output.encoder.crtc);
1038 unsigned int vbe, vse, hbe, hse, vbs, hbs;
1039 u32 value;
1040
1041 value = tegra_sor_readl(sor, SOR_STATE1);
1042 value &= ~SOR_STATE_ASY_PIXELDEPTH_MASK;
1043 value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
1044 value &= ~SOR_STATE_ASY_OWNER_MASK;
1045
1046 value |= SOR_STATE_ASY_CRC_MODE_COMPLETE |
1047 SOR_STATE_ASY_OWNER(dc->pipe + 1);
1048
1049 if (mode->flags & DRM_MODE_FLAG_PHSYNC)
1050 value &= ~SOR_STATE_ASY_HSYNCPOL;
1051
1052 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1053 value |= SOR_STATE_ASY_HSYNCPOL;
1054
1055 if (mode->flags & DRM_MODE_FLAG_PVSYNC)
1056 value &= ~SOR_STATE_ASY_VSYNCPOL;
1057
1058 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1059 value |= SOR_STATE_ASY_VSYNCPOL;
1060
Thierry Redingc31efa72015-09-08 16:09:22 +02001061 switch (state->bpc) {
1062 case 16:
1063 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_48_444;
1064 break;
1065
1066 case 12:
1067 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_36_444;
1068 break;
1069
1070 case 10:
1071 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_30_444;
1072 break;
1073
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001074 case 8:
1075 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
1076 break;
1077
1078 case 6:
1079 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_18_444;
1080 break;
1081
1082 default:
Thierry Redingc31efa72015-09-08 16:09:22 +02001083 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001084 break;
1085 }
1086
1087 tegra_sor_writel(sor, value, SOR_STATE1);
1088
1089 /*
1090 * TODO: The video timing programming below doesn't seem to match the
1091 * register definitions.
1092 */
1093
1094 value = ((mode->vtotal & 0x7fff) << 16) | (mode->htotal & 0x7fff);
Thierry Reding880cee02017-10-12 19:04:17 +02001095 tegra_sor_writel(sor, value, sor->soc->regs->head_state1 + dc->pipe);
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001096
1097 /* sync end = sync width - 1 */
1098 vse = mode->vsync_end - mode->vsync_start - 1;
1099 hse = mode->hsync_end - mode->hsync_start - 1;
1100
1101 value = ((vse & 0x7fff) << 16) | (hse & 0x7fff);
Thierry Reding880cee02017-10-12 19:04:17 +02001102 tegra_sor_writel(sor, value, sor->soc->regs->head_state2 + dc->pipe);
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001103
1104 /* blank end = sync end + back porch */
1105 vbe = vse + (mode->vtotal - mode->vsync_end);
1106 hbe = hse + (mode->htotal - mode->hsync_end);
1107
1108 value = ((vbe & 0x7fff) << 16) | (hbe & 0x7fff);
Thierry Reding880cee02017-10-12 19:04:17 +02001109 tegra_sor_writel(sor, value, sor->soc->regs->head_state3 + dc->pipe);
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001110
1111 /* blank start = blank end + active */
1112 vbs = vbe + mode->vdisplay;
1113 hbs = hbe + mode->hdisplay;
1114
1115 value = ((vbs & 0x7fff) << 16) | (hbs & 0x7fff);
Thierry Reding880cee02017-10-12 19:04:17 +02001116 tegra_sor_writel(sor, value, sor->soc->regs->head_state4 + dc->pipe);
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001117
1118 /* XXX interlacing support */
Thierry Reding880cee02017-10-12 19:04:17 +02001119 tegra_sor_writel(sor, 0x001, sor->soc->regs->head_state5 + dc->pipe);
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001120}
1121
Thierry Reding6fad8f62014-11-28 15:41:34 +01001122static int tegra_sor_detach(struct tegra_sor *sor)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001123{
Thierry Reding6fad8f62014-11-28 15:41:34 +01001124 unsigned long value, timeout;
1125
1126 /* switch to safe mode */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001127 value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001128 value &= ~SOR_SUPER_STATE_MODE_NORMAL;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001129 tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001130 tegra_sor_super_update(sor);
1131
1132 timeout = jiffies + msecs_to_jiffies(250);
1133
1134 while (time_before(jiffies, timeout)) {
1135 value = tegra_sor_readl(sor, SOR_PWR);
1136 if (value & SOR_PWR_MODE_SAFE)
1137 break;
1138 }
1139
1140 if ((value & SOR_PWR_MODE_SAFE) == 0)
1141 return -ETIMEDOUT;
1142
1143 /* go to sleep */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001144 value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001145 value &= ~SOR_SUPER_STATE_HEAD_MODE_MASK;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001146 tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001147 tegra_sor_super_update(sor);
1148
1149 /* detach */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001150 value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001151 value &= ~SOR_SUPER_STATE_ATTACHED;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001152 tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001153 tegra_sor_super_update(sor);
1154
1155 timeout = jiffies + msecs_to_jiffies(250);
1156
1157 while (time_before(jiffies, timeout)) {
1158 value = tegra_sor_readl(sor, SOR_TEST);
1159 if ((value & SOR_TEST_ATTACHED) == 0)
1160 break;
1161
1162 usleep_range(25, 100);
1163 }
1164
1165 if ((value & SOR_TEST_ATTACHED) != 0)
1166 return -ETIMEDOUT;
1167
1168 return 0;
1169}
1170
1171static int tegra_sor_power_down(struct tegra_sor *sor)
1172{
1173 unsigned long value, timeout;
1174 int err;
1175
1176 value = tegra_sor_readl(sor, SOR_PWR);
1177 value &= ~SOR_PWR_NORMAL_STATE_PU;
1178 value |= SOR_PWR_TRIGGER;
1179 tegra_sor_writel(sor, value, SOR_PWR);
1180
1181 timeout = jiffies + msecs_to_jiffies(250);
1182
1183 while (time_before(jiffies, timeout)) {
1184 value = tegra_sor_readl(sor, SOR_PWR);
1185 if ((value & SOR_PWR_TRIGGER) == 0)
1186 return 0;
1187
1188 usleep_range(25, 100);
1189 }
1190
1191 if ((value & SOR_PWR_TRIGGER) != 0)
1192 return -ETIMEDOUT;
1193
Thierry Reding25bb2ce2015-08-03 14:23:29 +02001194 /* switch to safe parent clock */
1195 err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
Thierry Redinge1335e22017-10-12 17:53:11 +02001196 if (err < 0) {
Thierry Reding6fad8f62014-11-28 15:41:34 +01001197 dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
Thierry Redinge1335e22017-10-12 17:53:11 +02001198 return err;
1199 }
Thierry Reding6fad8f62014-11-28 15:41:34 +01001200
Thierry Reding880cee02017-10-12 19:04:17 +02001201 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001202 value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
1203 SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2);
Thierry Reding880cee02017-10-12 19:04:17 +02001204 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001205
1206 /* stop lane sequencer */
1207 value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_UP |
1208 SOR_LANE_SEQ_CTL_POWER_STATE_DOWN;
1209 tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
1210
1211 timeout = jiffies + msecs_to_jiffies(250);
1212
1213 while (time_before(jiffies, timeout)) {
1214 value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
1215 if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
1216 break;
1217
1218 usleep_range(25, 100);
1219 }
1220
1221 if ((value & SOR_LANE_SEQ_CTL_TRIGGER) != 0)
1222 return -ETIMEDOUT;
1223
Thierry Reding880cee02017-10-12 19:04:17 +02001224 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001225 value |= SOR_PLL2_PORT_POWERDOWN;
Thierry Reding880cee02017-10-12 19:04:17 +02001226 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001227
1228 usleep_range(20, 100);
1229
Thierry Reding880cee02017-10-12 19:04:17 +02001230 value = tegra_sor_readl(sor, sor->soc->regs->pll0);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001231 value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
Thierry Reding880cee02017-10-12 19:04:17 +02001232 tegra_sor_writel(sor, value, sor->soc->regs->pll0);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001233
Thierry Reding880cee02017-10-12 19:04:17 +02001234 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001235 value |= SOR_PLL2_SEQ_PLLCAPPD;
1236 value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
Thierry Reding880cee02017-10-12 19:04:17 +02001237 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001238
1239 usleep_range(20, 100);
1240
1241 return 0;
1242}
1243
Thierry Reding6fad8f62014-11-28 15:41:34 +01001244static int tegra_sor_crc_wait(struct tegra_sor *sor, unsigned long timeout)
1245{
1246 u32 value;
1247
1248 timeout = jiffies + msecs_to_jiffies(timeout);
1249
1250 while (time_before(jiffies, timeout)) {
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001251 value = tegra_sor_readl(sor, SOR_CRCA);
1252 if (value & SOR_CRCA_VALID)
Thierry Reding6fad8f62014-11-28 15:41:34 +01001253 return 0;
1254
1255 usleep_range(100, 200);
1256 }
1257
1258 return -ETIMEDOUT;
1259}
1260
Thierry Reding530239a2015-08-06 11:04:54 +02001261static int tegra_sor_show_crc(struct seq_file *s, void *data)
Thierry Reding6fad8f62014-11-28 15:41:34 +01001262{
Thierry Reding530239a2015-08-06 11:04:54 +02001263 struct drm_info_node *node = s->private;
1264 struct tegra_sor *sor = node->info_ent->data;
Thierry Reding850bab42015-07-29 17:58:41 +02001265 struct drm_crtc *crtc = sor->output.encoder.crtc;
1266 struct drm_device *drm = node->minor->dev;
Thierry Reding530239a2015-08-06 11:04:54 +02001267 int err = 0;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001268 u32 value;
1269
Thierry Reding850bab42015-07-29 17:58:41 +02001270 drm_modeset_lock_all(drm);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001271
Thierry Reding850bab42015-07-29 17:58:41 +02001272 if (!crtc || !crtc->state->active) {
1273 err = -EBUSY;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001274 goto unlock;
1275 }
1276
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001277 value = tegra_sor_readl(sor, SOR_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001278 value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001279 tegra_sor_writel(sor, value, SOR_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001280
1281 value = tegra_sor_readl(sor, SOR_CRC_CNTRL);
1282 value |= SOR_CRC_CNTRL_ENABLE;
1283 tegra_sor_writel(sor, value, SOR_CRC_CNTRL);
1284
1285 value = tegra_sor_readl(sor, SOR_TEST);
1286 value &= ~SOR_TEST_CRC_POST_SERIALIZE;
1287 tegra_sor_writel(sor, value, SOR_TEST);
1288
1289 err = tegra_sor_crc_wait(sor, 100);
1290 if (err < 0)
1291 goto unlock;
1292
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001293 tegra_sor_writel(sor, SOR_CRCA_RESET, SOR_CRCA);
1294 value = tegra_sor_readl(sor, SOR_CRCB);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001295
Thierry Reding530239a2015-08-06 11:04:54 +02001296 seq_printf(s, "%08x\n", value);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001297
1298unlock:
Thierry Reding850bab42015-07-29 17:58:41 +02001299 drm_modeset_unlock_all(drm);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001300 return err;
1301}
1302
Thierry Reding062f5b2c2017-11-10 12:21:51 +01001303#define DEBUGFS_REG32(_name) { .name = #_name, .offset = _name }
1304
1305static const struct debugfs_reg32 tegra_sor_regs[] = {
1306 DEBUGFS_REG32(SOR_CTXSW),
1307 DEBUGFS_REG32(SOR_SUPER_STATE0),
1308 DEBUGFS_REG32(SOR_SUPER_STATE1),
1309 DEBUGFS_REG32(SOR_STATE0),
1310 DEBUGFS_REG32(SOR_STATE1),
1311 DEBUGFS_REG32(SOR_HEAD_STATE0(0)),
1312 DEBUGFS_REG32(SOR_HEAD_STATE0(1)),
1313 DEBUGFS_REG32(SOR_HEAD_STATE1(0)),
1314 DEBUGFS_REG32(SOR_HEAD_STATE1(1)),
1315 DEBUGFS_REG32(SOR_HEAD_STATE2(0)),
1316 DEBUGFS_REG32(SOR_HEAD_STATE2(1)),
1317 DEBUGFS_REG32(SOR_HEAD_STATE3(0)),
1318 DEBUGFS_REG32(SOR_HEAD_STATE3(1)),
1319 DEBUGFS_REG32(SOR_HEAD_STATE4(0)),
1320 DEBUGFS_REG32(SOR_HEAD_STATE4(1)),
1321 DEBUGFS_REG32(SOR_HEAD_STATE5(0)),
1322 DEBUGFS_REG32(SOR_HEAD_STATE5(1)),
1323 DEBUGFS_REG32(SOR_CRC_CNTRL),
1324 DEBUGFS_REG32(SOR_DP_DEBUG_MVID),
1325 DEBUGFS_REG32(SOR_CLK_CNTRL),
1326 DEBUGFS_REG32(SOR_CAP),
1327 DEBUGFS_REG32(SOR_PWR),
1328 DEBUGFS_REG32(SOR_TEST),
1329 DEBUGFS_REG32(SOR_PLL0),
1330 DEBUGFS_REG32(SOR_PLL1),
1331 DEBUGFS_REG32(SOR_PLL2),
1332 DEBUGFS_REG32(SOR_PLL3),
1333 DEBUGFS_REG32(SOR_CSTM),
1334 DEBUGFS_REG32(SOR_LVDS),
1335 DEBUGFS_REG32(SOR_CRCA),
1336 DEBUGFS_REG32(SOR_CRCB),
1337 DEBUGFS_REG32(SOR_BLANK),
1338 DEBUGFS_REG32(SOR_SEQ_CTL),
1339 DEBUGFS_REG32(SOR_LANE_SEQ_CTL),
1340 DEBUGFS_REG32(SOR_SEQ_INST(0)),
1341 DEBUGFS_REG32(SOR_SEQ_INST(1)),
1342 DEBUGFS_REG32(SOR_SEQ_INST(2)),
1343 DEBUGFS_REG32(SOR_SEQ_INST(3)),
1344 DEBUGFS_REG32(SOR_SEQ_INST(4)),
1345 DEBUGFS_REG32(SOR_SEQ_INST(5)),
1346 DEBUGFS_REG32(SOR_SEQ_INST(6)),
1347 DEBUGFS_REG32(SOR_SEQ_INST(7)),
1348 DEBUGFS_REG32(SOR_SEQ_INST(8)),
1349 DEBUGFS_REG32(SOR_SEQ_INST(9)),
1350 DEBUGFS_REG32(SOR_SEQ_INST(10)),
1351 DEBUGFS_REG32(SOR_SEQ_INST(11)),
1352 DEBUGFS_REG32(SOR_SEQ_INST(12)),
1353 DEBUGFS_REG32(SOR_SEQ_INST(13)),
1354 DEBUGFS_REG32(SOR_SEQ_INST(14)),
1355 DEBUGFS_REG32(SOR_SEQ_INST(15)),
1356 DEBUGFS_REG32(SOR_PWM_DIV),
1357 DEBUGFS_REG32(SOR_PWM_CTL),
1358 DEBUGFS_REG32(SOR_VCRC_A0),
1359 DEBUGFS_REG32(SOR_VCRC_A1),
1360 DEBUGFS_REG32(SOR_VCRC_B0),
1361 DEBUGFS_REG32(SOR_VCRC_B1),
1362 DEBUGFS_REG32(SOR_CCRC_A0),
1363 DEBUGFS_REG32(SOR_CCRC_A1),
1364 DEBUGFS_REG32(SOR_CCRC_B0),
1365 DEBUGFS_REG32(SOR_CCRC_B1),
1366 DEBUGFS_REG32(SOR_EDATA_A0),
1367 DEBUGFS_REG32(SOR_EDATA_A1),
1368 DEBUGFS_REG32(SOR_EDATA_B0),
1369 DEBUGFS_REG32(SOR_EDATA_B1),
1370 DEBUGFS_REG32(SOR_COUNT_A0),
1371 DEBUGFS_REG32(SOR_COUNT_A1),
1372 DEBUGFS_REG32(SOR_COUNT_B0),
1373 DEBUGFS_REG32(SOR_COUNT_B1),
1374 DEBUGFS_REG32(SOR_DEBUG_A0),
1375 DEBUGFS_REG32(SOR_DEBUG_A1),
1376 DEBUGFS_REG32(SOR_DEBUG_B0),
1377 DEBUGFS_REG32(SOR_DEBUG_B1),
1378 DEBUGFS_REG32(SOR_TRIG),
1379 DEBUGFS_REG32(SOR_MSCHECK),
1380 DEBUGFS_REG32(SOR_XBAR_CTRL),
1381 DEBUGFS_REG32(SOR_XBAR_POL),
1382 DEBUGFS_REG32(SOR_DP_LINKCTL0),
1383 DEBUGFS_REG32(SOR_DP_LINKCTL1),
1384 DEBUGFS_REG32(SOR_LANE_DRIVE_CURRENT0),
1385 DEBUGFS_REG32(SOR_LANE_DRIVE_CURRENT1),
1386 DEBUGFS_REG32(SOR_LANE4_DRIVE_CURRENT0),
1387 DEBUGFS_REG32(SOR_LANE4_DRIVE_CURRENT1),
1388 DEBUGFS_REG32(SOR_LANE_PREEMPHASIS0),
1389 DEBUGFS_REG32(SOR_LANE_PREEMPHASIS1),
1390 DEBUGFS_REG32(SOR_LANE4_PREEMPHASIS0),
1391 DEBUGFS_REG32(SOR_LANE4_PREEMPHASIS1),
1392 DEBUGFS_REG32(SOR_LANE_POSTCURSOR0),
1393 DEBUGFS_REG32(SOR_LANE_POSTCURSOR1),
1394 DEBUGFS_REG32(SOR_DP_CONFIG0),
1395 DEBUGFS_REG32(SOR_DP_CONFIG1),
1396 DEBUGFS_REG32(SOR_DP_MN0),
1397 DEBUGFS_REG32(SOR_DP_MN1),
1398 DEBUGFS_REG32(SOR_DP_PADCTL0),
1399 DEBUGFS_REG32(SOR_DP_PADCTL1),
Thierry Redingc57997b2017-10-12 19:12:57 +02001400 DEBUGFS_REG32(SOR_DP_PADCTL2),
Thierry Reding062f5b2c2017-11-10 12:21:51 +01001401 DEBUGFS_REG32(SOR_DP_DEBUG0),
1402 DEBUGFS_REG32(SOR_DP_DEBUG1),
1403 DEBUGFS_REG32(SOR_DP_SPARE0),
1404 DEBUGFS_REG32(SOR_DP_SPARE1),
1405 DEBUGFS_REG32(SOR_DP_AUDIO_CTRL),
1406 DEBUGFS_REG32(SOR_DP_AUDIO_HBLANK_SYMBOLS),
1407 DEBUGFS_REG32(SOR_DP_AUDIO_VBLANK_SYMBOLS),
1408 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_HEADER),
1409 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK0),
1410 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK1),
1411 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK2),
1412 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK3),
1413 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK4),
1414 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK5),
1415 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK6),
1416 DEBUGFS_REG32(SOR_DP_TPG),
1417 DEBUGFS_REG32(SOR_DP_TPG_CONFIG),
1418 DEBUGFS_REG32(SOR_DP_LQ_CSTM0),
1419 DEBUGFS_REG32(SOR_DP_LQ_CSTM1),
1420 DEBUGFS_REG32(SOR_DP_LQ_CSTM2),
1421};
1422
Thierry Redingdab16332015-01-26 16:04:08 +01001423static int tegra_sor_show_regs(struct seq_file *s, void *data)
1424{
1425 struct drm_info_node *node = s->private;
1426 struct tegra_sor *sor = node->info_ent->data;
Thierry Reding850bab42015-07-29 17:58:41 +02001427 struct drm_crtc *crtc = sor->output.encoder.crtc;
1428 struct drm_device *drm = node->minor->dev;
Thierry Reding062f5b2c2017-11-10 12:21:51 +01001429 unsigned int i;
Thierry Reding850bab42015-07-29 17:58:41 +02001430 int err = 0;
1431
1432 drm_modeset_lock_all(drm);
1433
1434 if (!crtc || !crtc->state->active) {
1435 err = -EBUSY;
1436 goto unlock;
1437 }
Thierry Redingdab16332015-01-26 16:04:08 +01001438
Thierry Reding062f5b2c2017-11-10 12:21:51 +01001439 for (i = 0; i < ARRAY_SIZE(tegra_sor_regs); i++) {
1440 unsigned int offset = tegra_sor_regs[i].offset;
Thierry Redingdab16332015-01-26 16:04:08 +01001441
Thierry Reding062f5b2c2017-11-10 12:21:51 +01001442 seq_printf(s, "%-38s %#05x %08x\n", tegra_sor_regs[i].name,
1443 offset, tegra_sor_readl(sor, offset));
1444 }
Thierry Redingdab16332015-01-26 16:04:08 +01001445
Thierry Reding850bab42015-07-29 17:58:41 +02001446unlock:
1447 drm_modeset_unlock_all(drm);
1448 return err;
Thierry Redingdab16332015-01-26 16:04:08 +01001449}
1450
1451static const struct drm_info_list debugfs_files[] = {
Thierry Reding530239a2015-08-06 11:04:54 +02001452 { "crc", tegra_sor_show_crc, 0, NULL },
Thierry Redingdab16332015-01-26 16:04:08 +01001453 { "regs", tegra_sor_show_regs, 0, NULL },
1454};
1455
Thierry Reding5b8e0432017-11-08 13:20:01 +01001456static int tegra_sor_late_register(struct drm_connector *connector)
Thierry Reding6fad8f62014-11-28 15:41:34 +01001457{
Thierry Reding5b8e0432017-11-08 13:20:01 +01001458 struct tegra_output *output = connector_to_output(connector);
1459 unsigned int i, count = ARRAY_SIZE(debugfs_files);
1460 struct drm_minor *minor = connector->dev->primary;
1461 struct dentry *root = connector->debugfs_entry;
1462 struct tegra_sor *sor = to_sor(output);
Thierry Reding530239a2015-08-06 11:04:54 +02001463 int err;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001464
Thierry Redingdab16332015-01-26 16:04:08 +01001465 sor->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1466 GFP_KERNEL);
Thierry Reding5b8e0432017-11-08 13:20:01 +01001467 if (!sor->debugfs_files)
1468 return -ENOMEM;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001469
Thierry Reding5b8e0432017-11-08 13:20:01 +01001470 for (i = 0; i < count; i++)
Thierry Redingdab16332015-01-26 16:04:08 +01001471 sor->debugfs_files[i].data = sor;
1472
Thierry Reding5b8e0432017-11-08 13:20:01 +01001473 err = drm_debugfs_create_files(sor->debugfs_files, count, root, minor);
Thierry Redingdab16332015-01-26 16:04:08 +01001474 if (err < 0)
1475 goto free;
1476
Thierry Reding530239a2015-08-06 11:04:54 +02001477 return 0;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001478
Thierry Redingdab16332015-01-26 16:04:08 +01001479free:
1480 kfree(sor->debugfs_files);
1481 sor->debugfs_files = NULL;
Thierry Reding5b8e0432017-11-08 13:20:01 +01001482
Thierry Reding6fad8f62014-11-28 15:41:34 +01001483 return err;
1484}
1485
Thierry Reding5b8e0432017-11-08 13:20:01 +01001486static void tegra_sor_early_unregister(struct drm_connector *connector)
Thierry Reding6fad8f62014-11-28 15:41:34 +01001487{
Thierry Reding5b8e0432017-11-08 13:20:01 +01001488 struct tegra_output *output = connector_to_output(connector);
1489 unsigned int count = ARRAY_SIZE(debugfs_files);
1490 struct tegra_sor *sor = to_sor(output);
Thierry Redingd92e6002017-10-12 19:07:54 +02001491
Thierry Reding5b8e0432017-11-08 13:20:01 +01001492 drm_debugfs_remove_files(sor->debugfs_files, count,
1493 connector->dev->primary);
Thierry Redingdab16332015-01-26 16:04:08 +01001494 kfree(sor->debugfs_files);
Thierry Reding066d30f2015-07-03 14:16:30 +02001495 sor->debugfs_files = NULL;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001496}
1497
Thierry Redingc31efa72015-09-08 16:09:22 +02001498static void tegra_sor_connector_reset(struct drm_connector *connector)
1499{
1500 struct tegra_sor_state *state;
1501
1502 state = kzalloc(sizeof(*state), GFP_KERNEL);
1503 if (!state)
1504 return;
1505
1506 if (connector->state) {
1507 __drm_atomic_helper_connector_destroy_state(connector->state);
1508 kfree(connector->state);
1509 }
1510
1511 __drm_atomic_helper_connector_reset(connector, &state->base);
1512}
1513
Thierry Reding6fad8f62014-11-28 15:41:34 +01001514static enum drm_connector_status
1515tegra_sor_connector_detect(struct drm_connector *connector, bool force)
1516{
1517 struct tegra_output *output = connector_to_output(connector);
1518 struct tegra_sor *sor = to_sor(output);
1519
Thierry Reding9542c232015-07-08 13:39:09 +02001520 if (sor->aux)
1521 return drm_dp_aux_detect(sor->aux);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001522
Thierry Reding459cc2c2015-07-30 10:34:24 +02001523 return tegra_output_connector_detect(connector, force);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001524}
1525
Thierry Redingc31efa72015-09-08 16:09:22 +02001526static struct drm_connector_state *
1527tegra_sor_connector_duplicate_state(struct drm_connector *connector)
1528{
1529 struct tegra_sor_state *state = to_sor_state(connector->state);
1530 struct tegra_sor_state *copy;
1531
1532 copy = kmemdup(state, sizeof(*state), GFP_KERNEL);
1533 if (!copy)
1534 return NULL;
1535
1536 __drm_atomic_helper_connector_duplicate_state(connector, &copy->base);
1537
1538 return &copy->base;
1539}
1540
Thierry Reding6fad8f62014-11-28 15:41:34 +01001541static const struct drm_connector_funcs tegra_sor_connector_funcs = {
Thierry Redingc31efa72015-09-08 16:09:22 +02001542 .reset = tegra_sor_connector_reset,
Thierry Reding6fad8f62014-11-28 15:41:34 +01001543 .detect = tegra_sor_connector_detect,
1544 .fill_modes = drm_helper_probe_single_connector_modes,
1545 .destroy = tegra_output_connector_destroy,
Thierry Redingc31efa72015-09-08 16:09:22 +02001546 .atomic_duplicate_state = tegra_sor_connector_duplicate_state,
Thierry Reding4aa3df72014-11-24 16:27:13 +01001547 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
Thierry Reding5b8e0432017-11-08 13:20:01 +01001548 .late_register = tegra_sor_late_register,
1549 .early_unregister = tegra_sor_early_unregister,
Thierry Reding6fad8f62014-11-28 15:41:34 +01001550};
1551
1552static int tegra_sor_connector_get_modes(struct drm_connector *connector)
1553{
1554 struct tegra_output *output = connector_to_output(connector);
1555 struct tegra_sor *sor = to_sor(output);
1556 int err;
1557
Thierry Reding9542c232015-07-08 13:39:09 +02001558 if (sor->aux)
1559 drm_dp_aux_enable(sor->aux);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001560
1561 err = tegra_output_connector_get_modes(connector);
1562
Thierry Reding9542c232015-07-08 13:39:09 +02001563 if (sor->aux)
1564 drm_dp_aux_disable(sor->aux);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001565
1566 return err;
1567}
1568
1569static enum drm_mode_status
1570tegra_sor_connector_mode_valid(struct drm_connector *connector,
1571 struct drm_display_mode *mode)
1572{
1573 return MODE_OK;
1574}
1575
1576static const struct drm_connector_helper_funcs tegra_sor_connector_helper_funcs = {
1577 .get_modes = tegra_sor_connector_get_modes,
1578 .mode_valid = tegra_sor_connector_mode_valid,
Thierry Reding6fad8f62014-11-28 15:41:34 +01001579};
1580
1581static const struct drm_encoder_funcs tegra_sor_encoder_funcs = {
1582 .destroy = tegra_output_encoder_destroy,
1583};
1584
Thierry Reding850bab42015-07-29 17:58:41 +02001585static void tegra_sor_edp_disable(struct drm_encoder *encoder)
Thierry Reding6fad8f62014-11-28 15:41:34 +01001586{
Thierry Reding850bab42015-07-29 17:58:41 +02001587 struct tegra_output *output = encoder_to_output(encoder);
1588 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
1589 struct tegra_sor *sor = to_sor(output);
1590 u32 value;
1591 int err;
1592
1593 if (output->panel)
1594 drm_panel_disable(output->panel);
1595
1596 err = tegra_sor_detach(sor);
1597 if (err < 0)
1598 dev_err(sor->dev, "failed to detach SOR: %d\n", err);
1599
1600 tegra_sor_writel(sor, 0, SOR_STATE1);
1601 tegra_sor_update(sor);
1602
1603 /*
1604 * The following accesses registers of the display controller, so make
1605 * sure it's only executed when the output is attached to one.
1606 */
1607 if (dc) {
1608 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
Thierry Redingc57997b2017-10-12 19:12:57 +02001609 value &= ~SOR_ENABLE(0);
Thierry Reding850bab42015-07-29 17:58:41 +02001610 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
1611
1612 tegra_dc_commit(dc);
1613 }
1614
1615 err = tegra_sor_power_down(sor);
1616 if (err < 0)
1617 dev_err(sor->dev, "failed to power down SOR: %d\n", err);
1618
Thierry Reding9542c232015-07-08 13:39:09 +02001619 if (sor->aux) {
1620 err = drm_dp_aux_disable(sor->aux);
Thierry Reding850bab42015-07-29 17:58:41 +02001621 if (err < 0)
1622 dev_err(sor->dev, "failed to disable DP: %d\n", err);
1623 }
1624
Thierry Redingc57997b2017-10-12 19:12:57 +02001625 err = tegra_io_pad_power_disable(sor->pad);
Thierry Reding850bab42015-07-29 17:58:41 +02001626 if (err < 0)
Thierry Redingc57997b2017-10-12 19:12:57 +02001627 dev_err(sor->dev, "failed to power off I/O pad: %d\n", err);
Thierry Reding850bab42015-07-29 17:58:41 +02001628
1629 if (output->panel)
1630 drm_panel_unprepare(output->panel);
1631
Thierry Redingaaff8bd2015-08-07 16:04:54 +02001632 pm_runtime_put(sor->dev);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001633}
1634
Thierry Reding459cc2c2015-07-30 10:34:24 +02001635#if 0
1636static int calc_h_ref_to_sync(const struct drm_display_mode *mode,
1637 unsigned int *value)
1638{
1639 unsigned int hfp, hsw, hbp, a = 0, b;
1640
1641 hfp = mode->hsync_start - mode->hdisplay;
1642 hsw = mode->hsync_end - mode->hsync_start;
1643 hbp = mode->htotal - mode->hsync_end;
1644
1645 pr_info("hfp: %u, hsw: %u, hbp: %u\n", hfp, hsw, hbp);
1646
1647 b = hfp - 1;
1648
1649 pr_info("a: %u, b: %u\n", a, b);
1650 pr_info("a + hsw + hbp = %u\n", a + hsw + hbp);
1651
1652 if (a + hsw + hbp <= 11) {
1653 a = 1 + 11 - hsw - hbp;
1654 pr_info("a: %u\n", a);
1655 }
1656
1657 if (a > b)
1658 return -EINVAL;
1659
1660 if (hsw < 1)
1661 return -EINVAL;
1662
1663 if (mode->hdisplay < 16)
1664 return -EINVAL;
1665
1666 if (value) {
1667 if (b > a && a % 2)
1668 *value = a + 1;
1669 else
1670 *value = a;
1671 }
1672
1673 return 0;
1674}
1675#endif
1676
Thierry Reding850bab42015-07-29 17:58:41 +02001677static void tegra_sor_edp_enable(struct drm_encoder *encoder)
Thierry Reding6fad8f62014-11-28 15:41:34 +01001678{
Thierry Reding850bab42015-07-29 17:58:41 +02001679 struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001680 struct tegra_output *output = encoder_to_output(encoder);
1681 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001682 struct tegra_sor *sor = to_sor(output);
Thierry Reding34fa1832014-06-05 16:31:10 +02001683 struct tegra_sor_config config;
Thierry Redingc31efa72015-09-08 16:09:22 +02001684 struct tegra_sor_state *state;
Thierry Reding34fa1832014-06-05 16:31:10 +02001685 struct drm_dp_link link;
Thierry Reding01b9bea2015-11-11 17:15:29 +01001686 u8 rate, lanes;
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001687 unsigned int i;
Thierry Reding86f5c522014-03-26 11:13:16 +01001688 int err = 0;
Thierry Reding28fe2072015-01-26 16:02:48 +01001689 u32 value;
Thierry Reding86f5c522014-03-26 11:13:16 +01001690
Thierry Redingc31efa72015-09-08 16:09:22 +02001691 state = to_sor_state(output->connector.state);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001692
Thierry Redingaaff8bd2015-08-07 16:04:54 +02001693 pm_runtime_get_sync(sor->dev);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001694
Thierry Reding6fad8f62014-11-28 15:41:34 +01001695 if (output->panel)
1696 drm_panel_prepare(output->panel);
1697
Thierry Reding01b9bea2015-11-11 17:15:29 +01001698 err = drm_dp_aux_enable(sor->aux);
1699 if (err < 0)
1700 dev_err(sor->dev, "failed to enable DP: %d\n", err);
Thierry Reding34fa1832014-06-05 16:31:10 +02001701
Thierry Reding01b9bea2015-11-11 17:15:29 +01001702 err = drm_dp_link_probe(sor->aux, &link);
1703 if (err < 0) {
1704 dev_err(sor->dev, "failed to probe eDP link: %d\n", err);
1705 return;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001706 }
1707
Thierry Reding25bb2ce2015-08-03 14:23:29 +02001708 /* switch to safe parent clock */
1709 err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001710 if (err < 0)
1711 dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
1712
Thierry Reding34fa1832014-06-05 16:31:10 +02001713 memset(&config, 0, sizeof(config));
Thierry Redingc31efa72015-09-08 16:09:22 +02001714 config.bits_per_pixel = state->bpc * 3;
Thierry Reding34fa1832014-06-05 16:31:10 +02001715
Thierry Redinga1983592015-07-21 16:46:52 +02001716 err = tegra_sor_compute_config(sor, mode, &config, &link);
Thierry Reding34fa1832014-06-05 16:31:10 +02001717 if (err < 0)
Thierry Redinga1983592015-07-21 16:46:52 +02001718 dev_err(sor->dev, "failed to compute configuration: %d\n", err);
Thierry Reding34fa1832014-06-05 16:31:10 +02001719
Thierry Reding6b6b6042013-11-15 16:06:05 +01001720 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
1721 value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
1722 value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
1723 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
1724
Thierry Reding880cee02017-10-12 19:04:17 +02001725 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001726 value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
Thierry Reding880cee02017-10-12 19:04:17 +02001727 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001728 usleep_range(20, 100);
1729
Thierry Reding880cee02017-10-12 19:04:17 +02001730 value = tegra_sor_readl(sor, sor->soc->regs->pll3);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001731 value |= SOR_PLL3_PLL_VDD_MODE_3V3;
Thierry Reding880cee02017-10-12 19:04:17 +02001732 tegra_sor_writel(sor, value, sor->soc->regs->pll3);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001733
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001734 value = SOR_PLL0_ICHPMP(0xf) | SOR_PLL0_VCOCAP_RST |
1735 SOR_PLL0_PLLREG_LEVEL_V45 | SOR_PLL0_RESISTOR_EXT;
Thierry Reding880cee02017-10-12 19:04:17 +02001736 tegra_sor_writel(sor, value, sor->soc->regs->pll0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001737
Thierry Reding880cee02017-10-12 19:04:17 +02001738 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001739 value |= SOR_PLL2_SEQ_PLLCAPPD;
1740 value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1741 value |= SOR_PLL2_LVDS_ENABLE;
Thierry Reding880cee02017-10-12 19:04:17 +02001742 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001743
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001744 value = SOR_PLL1_TERM_COMPOUT | SOR_PLL1_TMDS_TERM;
Thierry Reding880cee02017-10-12 19:04:17 +02001745 tegra_sor_writel(sor, value, sor->soc->regs->pll1);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001746
1747 while (true) {
Thierry Reding880cee02017-10-12 19:04:17 +02001748 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001749 if ((value & SOR_PLL2_SEQ_PLLCAPPD_ENFORCE) == 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001750 break;
1751
1752 usleep_range(250, 1000);
1753 }
1754
Thierry Reding880cee02017-10-12 19:04:17 +02001755 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001756 value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
1757 value &= ~SOR_PLL2_PORT_POWERDOWN;
Thierry Reding880cee02017-10-12 19:04:17 +02001758 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001759
1760 /*
1761 * power up
1762 */
1763
1764 /* set safe link bandwidth (1.62 Gbps) */
1765 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
1766 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
1767 value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G1_62;
1768 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
1769
1770 /* step 1 */
Thierry Reding880cee02017-10-12 19:04:17 +02001771 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001772 value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE | SOR_PLL2_PORT_POWERDOWN |
1773 SOR_PLL2_BANDGAP_POWERDOWN;
Thierry Reding880cee02017-10-12 19:04:17 +02001774 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001775
Thierry Reding880cee02017-10-12 19:04:17 +02001776 value = tegra_sor_readl(sor, sor->soc->regs->pll0);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001777 value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
Thierry Reding880cee02017-10-12 19:04:17 +02001778 tegra_sor_writel(sor, value, sor->soc->regs->pll0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001779
Thierry Reding880cee02017-10-12 19:04:17 +02001780 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001781 value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
Thierry Reding880cee02017-10-12 19:04:17 +02001782 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001783
1784 /* step 2 */
Thierry Redingc57997b2017-10-12 19:12:57 +02001785 err = tegra_io_pad_power_enable(sor->pad);
Thierry Reding850bab42015-07-29 17:58:41 +02001786 if (err < 0)
Thierry Redingc57997b2017-10-12 19:12:57 +02001787 dev_err(sor->dev, "failed to power on I/O pad: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001788
1789 usleep_range(5, 100);
1790
1791 /* step 3 */
Thierry Reding880cee02017-10-12 19:04:17 +02001792 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001793 value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
Thierry Reding880cee02017-10-12 19:04:17 +02001794 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001795
1796 usleep_range(20, 100);
1797
1798 /* step 4 */
Thierry Reding880cee02017-10-12 19:04:17 +02001799 value = tegra_sor_readl(sor, sor->soc->regs->pll0);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001800 value &= ~SOR_PLL0_VCOPD;
1801 value &= ~SOR_PLL0_PWR;
Thierry Reding880cee02017-10-12 19:04:17 +02001802 tegra_sor_writel(sor, value, sor->soc->regs->pll0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001803
Thierry Reding880cee02017-10-12 19:04:17 +02001804 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001805 value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
Thierry Reding880cee02017-10-12 19:04:17 +02001806 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001807
1808 usleep_range(200, 1000);
1809
1810 /* step 5 */
Thierry Reding880cee02017-10-12 19:04:17 +02001811 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001812 value &= ~SOR_PLL2_PORT_POWERDOWN;
Thierry Reding880cee02017-10-12 19:04:17 +02001813 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001814
Thierry Reding30b49432015-08-03 15:50:32 +02001815 /* XXX not in TRM */
1816 for (value = 0, i = 0; i < 5; i++)
1817 value |= SOR_XBAR_CTRL_LINK0_XSEL(i, sor->soc->xbar_cfg[i]) |
1818 SOR_XBAR_CTRL_LINK1_XSEL(i, i);
1819
1820 tegra_sor_writel(sor, 0x00000000, SOR_XBAR_POL);
1821 tegra_sor_writel(sor, value, SOR_XBAR_CTRL);
1822
Thierry Reding25bb2ce2015-08-03 14:23:29 +02001823 /* switch to DP parent clock */
1824 err = tegra_sor_set_parent_clock(sor, sor->clk_dp);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001825 if (err < 0)
Thierry Reding25bb2ce2015-08-03 14:23:29 +02001826 dev_err(sor->dev, "failed to set parent clock: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001827
Thierry Reding899451b2014-06-05 16:19:48 +02001828 /* power DP lanes */
Thierry Reding880cee02017-10-12 19:04:17 +02001829 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding899451b2014-06-05 16:19:48 +02001830
1831 if (link.num_lanes <= 2)
1832 value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2);
1833 else
1834 value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2;
1835
1836 if (link.num_lanes <= 1)
1837 value &= ~SOR_DP_PADCTL_PD_TXD_1;
1838 else
1839 value |= SOR_DP_PADCTL_PD_TXD_1;
1840
1841 if (link.num_lanes == 0)
1842 value &= ~SOR_DP_PADCTL_PD_TXD_0;
1843 else
1844 value |= SOR_DP_PADCTL_PD_TXD_0;
1845
Thierry Reding880cee02017-10-12 19:04:17 +02001846 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001847
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001848 value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001849 value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
Thierry Reding0c90a182014-06-05 16:29:46 +02001850 value |= SOR_DP_LINKCTL_LANE_COUNT(link.num_lanes);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001851 tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001852
1853 /* start lane sequencer */
1854 value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
1855 SOR_LANE_SEQ_CTL_POWER_STATE_UP;
1856 tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
1857
1858 while (true) {
1859 value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
1860 if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
1861 break;
1862
1863 usleep_range(250, 1000);
1864 }
1865
Thierry Redinga4263fe2014-06-05 16:16:23 +02001866 /* set link bandwidth */
Thierry Reding6b6b6042013-11-15 16:06:05 +01001867 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
1868 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
Thierry Redinga4263fe2014-06-05 16:16:23 +02001869 value |= drm_dp_link_rate_to_bw_code(link.rate) << 2;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001870 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
1871
Thierry Reding402f6bc2015-07-21 16:48:19 +02001872 tegra_sor_apply_config(sor, &config);
1873
1874 /* enable link */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001875 value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001876 value |= SOR_DP_LINKCTL_ENABLE;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001877 value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001878 tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001879
1880 for (i = 0, value = 0; i < 4; i++) {
1881 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
1882 SOR_DP_TPG_SCRAMBLER_GALIOS |
1883 SOR_DP_TPG_PATTERN_NONE;
1884 value = (value << 8) | lane;
1885 }
1886
1887 tegra_sor_writel(sor, value, SOR_DP_TPG);
1888
Thierry Reding6b6b6042013-11-15 16:06:05 +01001889 /* enable pad calibration logic */
Thierry Reding880cee02017-10-12 19:04:17 +02001890 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001891 value |= SOR_DP_PADCTL_PAD_CAL_PD;
Thierry Reding880cee02017-10-12 19:04:17 +02001892 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001893
Thierry Reding01b9bea2015-11-11 17:15:29 +01001894 err = drm_dp_link_probe(sor->aux, &link);
1895 if (err < 0)
1896 dev_err(sor->dev, "failed to probe eDP link: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001897
Thierry Reding01b9bea2015-11-11 17:15:29 +01001898 err = drm_dp_link_power_up(sor->aux, &link);
1899 if (err < 0)
1900 dev_err(sor->dev, "failed to power up eDP link: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001901
Thierry Reding01b9bea2015-11-11 17:15:29 +01001902 err = drm_dp_link_configure(sor->aux, &link);
1903 if (err < 0)
1904 dev_err(sor->dev, "failed to configure eDP link: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001905
Thierry Reding01b9bea2015-11-11 17:15:29 +01001906 rate = drm_dp_link_rate_to_bw_code(link.rate);
1907 lanes = link.num_lanes;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001908
Thierry Reding01b9bea2015-11-11 17:15:29 +01001909 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
1910 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
1911 value |= SOR_CLK_CNTRL_DP_LINK_SPEED(rate);
1912 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001913
Thierry Reding01b9bea2015-11-11 17:15:29 +01001914 value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
1915 value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
1916 value |= SOR_DP_LINKCTL_LANE_COUNT(lanes);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001917
Thierry Reding01b9bea2015-11-11 17:15:29 +01001918 if (link.capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
1919 value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001920
Thierry Reding01b9bea2015-11-11 17:15:29 +01001921 tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001922
Thierry Reding01b9bea2015-11-11 17:15:29 +01001923 /* disable training pattern generator */
Thierry Reding6b6b6042013-11-15 16:06:05 +01001924
Thierry Reding01b9bea2015-11-11 17:15:29 +01001925 for (i = 0; i < link.num_lanes; i++) {
1926 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
1927 SOR_DP_TPG_SCRAMBLER_GALIOS |
1928 SOR_DP_TPG_PATTERN_NONE;
1929 value = (value << 8) | lane;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001930 }
1931
Thierry Reding01b9bea2015-11-11 17:15:29 +01001932 tegra_sor_writel(sor, value, SOR_DP_TPG);
1933
1934 err = tegra_sor_dp_train_fast(sor, &link);
1935 if (err < 0)
1936 dev_err(sor->dev, "DP fast link training failed: %d\n", err);
1937
1938 dev_dbg(sor->dev, "fast link training succeeded\n");
1939
Thierry Reding6b6b6042013-11-15 16:06:05 +01001940 err = tegra_sor_power_up(sor, 250);
Thierry Reding850bab42015-07-29 17:58:41 +02001941 if (err < 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001942 dev_err(sor->dev, "failed to power up SOR: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001943
Thierry Reding6b6b6042013-11-15 16:06:05 +01001944 /* CSTM (LVDS, link A/B, upper) */
Stéphane Marchesin143b1df2014-05-22 20:32:47 -07001945 value = SOR_CSTM_LVDS | SOR_CSTM_LINK_ACT_A | SOR_CSTM_LINK_ACT_B |
Thierry Reding6b6b6042013-11-15 16:06:05 +01001946 SOR_CSTM_UPPER;
1947 tegra_sor_writel(sor, value, SOR_CSTM);
1948
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001949 /* use DP-A protocol */
1950 value = tegra_sor_readl(sor, SOR_STATE1);
1951 value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
1952 value |= SOR_STATE_ASY_PROTOCOL_DP_A;
1953 tegra_sor_writel(sor, value, SOR_STATE1);
1954
Thierry Redingc31efa72015-09-08 16:09:22 +02001955 tegra_sor_mode_set(sor, mode, state);
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001956
Thierry Reding6b6b6042013-11-15 16:06:05 +01001957 /* PWM setup */
1958 err = tegra_sor_setup_pwm(sor, 250);
Thierry Reding850bab42015-07-29 17:58:41 +02001959 if (err < 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001960 dev_err(sor->dev, "failed to setup PWM: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001961
Thierry Reding666cb872014-12-08 16:32:47 +01001962 tegra_sor_update(sor);
1963
Thierry Reding6b6b6042013-11-15 16:06:05 +01001964 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
Thierry Redingc57997b2017-10-12 19:12:57 +02001965 value |= SOR_ENABLE(0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001966 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
1967
Thierry Reding666cb872014-12-08 16:32:47 +01001968 tegra_dc_commit(dc);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001969
1970 err = tegra_sor_attach(sor);
Thierry Reding850bab42015-07-29 17:58:41 +02001971 if (err < 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001972 dev_err(sor->dev, "failed to attach SOR: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001973
1974 err = tegra_sor_wakeup(sor);
Thierry Reding850bab42015-07-29 17:58:41 +02001975 if (err < 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001976 dev_err(sor->dev, "failed to enable DC: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001977
Thierry Reding6fad8f62014-11-28 15:41:34 +01001978 if (output->panel)
1979 drm_panel_enable(output->panel);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001980}
1981
Thierry Reding82f15112014-12-08 17:26:46 +01001982static int
1983tegra_sor_encoder_atomic_check(struct drm_encoder *encoder,
1984 struct drm_crtc_state *crtc_state,
1985 struct drm_connector_state *conn_state)
1986{
1987 struct tegra_output *output = encoder_to_output(encoder);
Thierry Redingc31efa72015-09-08 16:09:22 +02001988 struct tegra_sor_state *state = to_sor_state(conn_state);
Thierry Reding82f15112014-12-08 17:26:46 +01001989 struct tegra_dc *dc = to_tegra_dc(conn_state->crtc);
1990 unsigned long pclk = crtc_state->mode.clock * 1000;
1991 struct tegra_sor *sor = to_sor(output);
Thierry Redingc31efa72015-09-08 16:09:22 +02001992 struct drm_display_info *info;
Thierry Reding82f15112014-12-08 17:26:46 +01001993 int err;
1994
Thierry Redingc31efa72015-09-08 16:09:22 +02001995 info = &output->connector.display_info;
1996
Thierry Reding36e90222017-10-12 19:14:21 +02001997 /*
1998 * For HBR2 modes, the SOR brick needs to use the x20 multiplier, so
1999 * the pixel clock must be corrected accordingly.
2000 */
2001 if (pclk >= 340000000) {
2002 state->link_speed = 20;
2003 state->pclk = pclk / 2;
2004 } else {
2005 state->link_speed = 10;
2006 state->pclk = pclk;
2007 }
2008
Thierry Reding82f15112014-12-08 17:26:46 +01002009 err = tegra_dc_state_setup_clock(dc, crtc_state, sor->clk_parent,
2010 pclk, 0);
2011 if (err < 0) {
2012 dev_err(output->dev, "failed to setup CRTC state: %d\n", err);
2013 return err;
2014 }
2015
Thierry Redingc31efa72015-09-08 16:09:22 +02002016 switch (info->bpc) {
2017 case 8:
2018 case 6:
2019 state->bpc = info->bpc;
2020 break;
2021
2022 default:
2023 DRM_DEBUG_KMS("%u bits-per-color not supported\n", info->bpc);
2024 state->bpc = 8;
2025 break;
2026 }
2027
Thierry Reding82f15112014-12-08 17:26:46 +01002028 return 0;
2029}
2030
Thierry Reding459cc2c2015-07-30 10:34:24 +02002031static const struct drm_encoder_helper_funcs tegra_sor_edp_helpers = {
Thierry Reding850bab42015-07-29 17:58:41 +02002032 .disable = tegra_sor_edp_disable,
2033 .enable = tegra_sor_edp_enable,
Thierry Reding82f15112014-12-08 17:26:46 +01002034 .atomic_check = tegra_sor_encoder_atomic_check,
Thierry Reding6b6b6042013-11-15 16:06:05 +01002035};
2036
Thierry Reding459cc2c2015-07-30 10:34:24 +02002037static inline u32 tegra_sor_hdmi_subpack(const u8 *ptr, size_t size)
2038{
2039 u32 value = 0;
2040 size_t i;
2041
2042 for (i = size; i > 0; i--)
2043 value = (value << 8) | ptr[i - 1];
2044
2045 return value;
2046}
2047
2048static void tegra_sor_hdmi_write_infopack(struct tegra_sor *sor,
2049 const void *data, size_t size)
2050{
2051 const u8 *ptr = data;
2052 unsigned long offset;
2053 size_t i, j;
2054 u32 value;
2055
2056 switch (ptr[0]) {
2057 case HDMI_INFOFRAME_TYPE_AVI:
2058 offset = SOR_HDMI_AVI_INFOFRAME_HEADER;
2059 break;
2060
2061 case HDMI_INFOFRAME_TYPE_AUDIO:
2062 offset = SOR_HDMI_AUDIO_INFOFRAME_HEADER;
2063 break;
2064
2065 case HDMI_INFOFRAME_TYPE_VENDOR:
2066 offset = SOR_HDMI_VSI_INFOFRAME_HEADER;
2067 break;
2068
2069 default:
2070 dev_err(sor->dev, "unsupported infoframe type: %02x\n",
2071 ptr[0]);
2072 return;
2073 }
2074
2075 value = INFOFRAME_HEADER_TYPE(ptr[0]) |
2076 INFOFRAME_HEADER_VERSION(ptr[1]) |
2077 INFOFRAME_HEADER_LEN(ptr[2]);
2078 tegra_sor_writel(sor, value, offset);
2079 offset++;
2080
2081 /*
2082 * Each subpack contains 7 bytes, divided into:
2083 * - subpack_low: bytes 0 - 3
2084 * - subpack_high: bytes 4 - 6 (with byte 7 padded to 0x00)
2085 */
2086 for (i = 3, j = 0; i < size; i += 7, j += 8) {
2087 size_t rem = size - i, num = min_t(size_t, rem, 4);
2088
2089 value = tegra_sor_hdmi_subpack(&ptr[i], num);
2090 tegra_sor_writel(sor, value, offset++);
2091
2092 num = min_t(size_t, rem - num, 3);
2093
2094 value = tegra_sor_hdmi_subpack(&ptr[i + 4], num);
2095 tegra_sor_writel(sor, value, offset++);
2096 }
2097}
2098
2099static int
2100tegra_sor_hdmi_setup_avi_infoframe(struct tegra_sor *sor,
2101 const struct drm_display_mode *mode)
2102{
2103 u8 buffer[HDMI_INFOFRAME_SIZE(AVI)];
2104 struct hdmi_avi_infoframe frame;
2105 u32 value;
2106 int err;
2107
2108 /* disable AVI infoframe */
2109 value = tegra_sor_readl(sor, SOR_HDMI_AVI_INFOFRAME_CTRL);
2110 value &= ~INFOFRAME_CTRL_SINGLE;
2111 value &= ~INFOFRAME_CTRL_OTHER;
2112 value &= ~INFOFRAME_CTRL_ENABLE;
2113 tegra_sor_writel(sor, value, SOR_HDMI_AVI_INFOFRAME_CTRL);
2114
Shashank Sharma0c1f5282017-07-13 21:03:07 +05302115 err = drm_hdmi_avi_infoframe_from_display_mode(&frame, mode, false);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002116 if (err < 0) {
2117 dev_err(sor->dev, "failed to setup AVI infoframe: %d\n", err);
2118 return err;
2119 }
2120
2121 err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
2122 if (err < 0) {
2123 dev_err(sor->dev, "failed to pack AVI infoframe: %d\n", err);
2124 return err;
2125 }
2126
2127 tegra_sor_hdmi_write_infopack(sor, buffer, err);
2128
2129 /* enable AVI infoframe */
2130 value = tegra_sor_readl(sor, SOR_HDMI_AVI_INFOFRAME_CTRL);
2131 value |= INFOFRAME_CTRL_CHECKSUM_ENABLE;
2132 value |= INFOFRAME_CTRL_ENABLE;
2133 tegra_sor_writel(sor, value, SOR_HDMI_AVI_INFOFRAME_CTRL);
2134
2135 return 0;
2136}
2137
Thierry Reding8e2988a72018-12-03 15:46:03 +01002138static void tegra_sor_write_eld(struct tegra_sor *sor)
2139{
2140 size_t length = drm_eld_size(sor->output.connector.eld), i;
2141
2142 for (i = 0; i < length; i++)
2143 tegra_sor_writel(sor, i << 8 | sor->output.connector.eld[i],
2144 SOR_AUDIO_HDA_ELD_BUFWR);
2145
2146 /*
2147 * The HDA codec will always report an ELD buffer size of 96 bytes and
2148 * the HDA codec driver will check that each byte read from the buffer
2149 * is valid. Therefore every byte must be written, even if no 96 bytes
2150 * were parsed from EDID.
2151 */
2152 for (i = length; i < 96; i++)
2153 tegra_sor_writel(sor, i << 8 | 0, SOR_AUDIO_HDA_ELD_BUFWR);
2154}
2155
2156static void tegra_sor_audio_prepare(struct tegra_sor *sor)
2157{
2158 u32 value;
2159
2160 tegra_sor_write_eld(sor);
2161
2162 value = SOR_AUDIO_HDA_PRESENSE_ELDV | SOR_AUDIO_HDA_PRESENSE_PD;
2163 tegra_sor_writel(sor, value, SOR_AUDIO_HDA_PRESENSE);
2164}
2165
2166static void tegra_sor_audio_unprepare(struct tegra_sor *sor)
2167{
2168 tegra_sor_writel(sor, 0, SOR_AUDIO_HDA_PRESENSE);
2169}
2170
2171static int tegra_sor_hdmi_enable_audio_infoframe(struct tegra_sor *sor)
2172{
2173 u8 buffer[HDMI_INFOFRAME_SIZE(AUDIO)];
2174 struct hdmi_audio_infoframe frame;
2175 u32 value;
2176 int err;
2177
2178 err = hdmi_audio_infoframe_init(&frame);
2179 if (err < 0) {
2180 dev_err(sor->dev, "failed to setup audio infoframe: %d\n", err);
2181 return err;
2182 }
2183
Thierry Redingfad7b802019-01-03 15:23:16 +01002184 frame.channels = sor->format.channels;
Thierry Reding8e2988a72018-12-03 15:46:03 +01002185
2186 err = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
2187 if (err < 0) {
2188 dev_err(sor->dev, "failed to pack audio infoframe: %d\n", err);
2189 return err;
2190 }
2191
2192 tegra_sor_hdmi_write_infopack(sor, buffer, err);
2193
2194 value = tegra_sor_readl(sor, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
2195 value |= INFOFRAME_CTRL_CHECKSUM_ENABLE;
2196 value |= INFOFRAME_CTRL_ENABLE;
2197 tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
2198
2199 return 0;
2200}
2201
2202static void tegra_sor_hdmi_audio_enable(struct tegra_sor *sor)
2203{
2204 u32 value;
2205
2206 value = tegra_sor_readl(sor, SOR_AUDIO_CNTRL);
2207
2208 /* select HDA audio input */
2209 value &= ~SOR_AUDIO_CNTRL_SOURCE_SELECT(SOURCE_SELECT_MASK);
2210 value |= SOR_AUDIO_CNTRL_SOURCE_SELECT(SOURCE_SELECT_HDA);
2211
2212 /* inject null samples */
Thierry Redingfad7b802019-01-03 15:23:16 +01002213 if (sor->format.channels != 2)
Thierry Reding8e2988a72018-12-03 15:46:03 +01002214 value &= ~SOR_AUDIO_CNTRL_INJECT_NULLSMPL;
2215 else
2216 value |= SOR_AUDIO_CNTRL_INJECT_NULLSMPL;
2217
2218 value |= SOR_AUDIO_CNTRL_AFIFO_FLUSH;
2219
2220 tegra_sor_writel(sor, value, SOR_AUDIO_CNTRL);
2221
2222 /* enable advertising HBR capability */
2223 tegra_sor_writel(sor, SOR_AUDIO_SPARE_HBR_ENABLE, SOR_AUDIO_SPARE);
2224
2225 tegra_sor_writel(sor, 0, SOR_HDMI_ACR_CTRL);
2226
2227 value = SOR_HDMI_SPARE_ACR_PRIORITY_HIGH |
2228 SOR_HDMI_SPARE_CTS_RESET(1) |
2229 SOR_HDMI_SPARE_HW_CTS_ENABLE;
2230 tegra_sor_writel(sor, value, SOR_HDMI_SPARE);
2231
2232 /* enable HW CTS */
2233 value = SOR_HDMI_ACR_SUBPACK_LOW_SB1(0);
2234 tegra_sor_writel(sor, value, SOR_HDMI_ACR_0441_SUBPACK_LOW);
2235
2236 /* allow packet to be sent */
2237 value = SOR_HDMI_ACR_SUBPACK_HIGH_ENABLE;
2238 tegra_sor_writel(sor, value, SOR_HDMI_ACR_0441_SUBPACK_HIGH);
2239
2240 /* reset N counter and enable lookup */
2241 value = SOR_HDMI_AUDIO_N_RESET | SOR_HDMI_AUDIO_N_LOOKUP;
2242 tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_N);
2243
Thierry Redingfad7b802019-01-03 15:23:16 +01002244 value = (24000 * 4096) / (128 * sor->format.sample_rate / 1000);
Thierry Reding8e2988a72018-12-03 15:46:03 +01002245 tegra_sor_writel(sor, value, SOR_AUDIO_AVAL_0320);
2246 tegra_sor_writel(sor, 4096, SOR_AUDIO_NVAL_0320);
2247
2248 tegra_sor_writel(sor, 20000, SOR_AUDIO_AVAL_0441);
2249 tegra_sor_writel(sor, 4704, SOR_AUDIO_NVAL_0441);
2250
2251 tegra_sor_writel(sor, 20000, SOR_AUDIO_AVAL_0882);
2252 tegra_sor_writel(sor, 9408, SOR_AUDIO_NVAL_0882);
2253
2254 tegra_sor_writel(sor, 20000, SOR_AUDIO_AVAL_1764);
2255 tegra_sor_writel(sor, 18816, SOR_AUDIO_NVAL_1764);
2256
Thierry Redingfad7b802019-01-03 15:23:16 +01002257 value = (24000 * 6144) / (128 * sor->format.sample_rate / 1000);
Thierry Reding8e2988a72018-12-03 15:46:03 +01002258 tegra_sor_writel(sor, value, SOR_AUDIO_AVAL_0480);
2259 tegra_sor_writel(sor, 6144, SOR_AUDIO_NVAL_0480);
2260
Thierry Redingfad7b802019-01-03 15:23:16 +01002261 value = (24000 * 12288) / (128 * sor->format.sample_rate / 1000);
Thierry Reding8e2988a72018-12-03 15:46:03 +01002262 tegra_sor_writel(sor, value, SOR_AUDIO_AVAL_0960);
2263 tegra_sor_writel(sor, 12288, SOR_AUDIO_NVAL_0960);
2264
Thierry Redingfad7b802019-01-03 15:23:16 +01002265 value = (24000 * 24576) / (128 * sor->format.sample_rate / 1000);
Thierry Reding8e2988a72018-12-03 15:46:03 +01002266 tegra_sor_writel(sor, value, SOR_AUDIO_AVAL_1920);
2267 tegra_sor_writel(sor, 24576, SOR_AUDIO_NVAL_1920);
2268
2269 value = tegra_sor_readl(sor, SOR_HDMI_AUDIO_N);
2270 value &= ~SOR_HDMI_AUDIO_N_RESET;
2271 tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_N);
2272
2273 tegra_sor_hdmi_enable_audio_infoframe(sor);
2274}
2275
Thierry Reding459cc2c2015-07-30 10:34:24 +02002276static void tegra_sor_hdmi_disable_audio_infoframe(struct tegra_sor *sor)
2277{
2278 u32 value;
2279
2280 value = tegra_sor_readl(sor, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
2281 value &= ~INFOFRAME_CTRL_ENABLE;
2282 tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
2283}
2284
Thierry Reding8e2988a72018-12-03 15:46:03 +01002285static void tegra_sor_hdmi_audio_disable(struct tegra_sor *sor)
2286{
2287 tegra_sor_hdmi_disable_audio_infoframe(sor);
2288}
2289
Thierry Reding459cc2c2015-07-30 10:34:24 +02002290static struct tegra_sor_hdmi_settings *
2291tegra_sor_hdmi_find_settings(struct tegra_sor *sor, unsigned long frequency)
2292{
2293 unsigned int i;
2294
2295 for (i = 0; i < sor->num_settings; i++)
2296 if (frequency <= sor->settings[i].frequency)
2297 return &sor->settings[i];
2298
2299 return NULL;
2300}
2301
Thierry Reding36e90222017-10-12 19:14:21 +02002302static void tegra_sor_hdmi_disable_scrambling(struct tegra_sor *sor)
2303{
2304 u32 value;
2305
2306 value = tegra_sor_readl(sor, SOR_HDMI2_CTRL);
2307 value &= ~SOR_HDMI2_CTRL_CLOCK_MODE_DIV_BY_4;
2308 value &= ~SOR_HDMI2_CTRL_SCRAMBLE;
2309 tegra_sor_writel(sor, value, SOR_HDMI2_CTRL);
2310}
2311
2312static void tegra_sor_hdmi_scdc_disable(struct tegra_sor *sor)
2313{
2314 struct i2c_adapter *ddc = sor->output.ddc;
2315
2316 drm_scdc_set_high_tmds_clock_ratio(ddc, false);
2317 drm_scdc_set_scrambling(ddc, false);
2318
2319 tegra_sor_hdmi_disable_scrambling(sor);
2320}
2321
2322static void tegra_sor_hdmi_scdc_stop(struct tegra_sor *sor)
2323{
2324 if (sor->scdc_enabled) {
2325 cancel_delayed_work_sync(&sor->scdc);
2326 tegra_sor_hdmi_scdc_disable(sor);
2327 }
2328}
2329
2330static void tegra_sor_hdmi_enable_scrambling(struct tegra_sor *sor)
2331{
2332 u32 value;
2333
2334 value = tegra_sor_readl(sor, SOR_HDMI2_CTRL);
2335 value |= SOR_HDMI2_CTRL_CLOCK_MODE_DIV_BY_4;
2336 value |= SOR_HDMI2_CTRL_SCRAMBLE;
2337 tegra_sor_writel(sor, value, SOR_HDMI2_CTRL);
2338}
2339
2340static void tegra_sor_hdmi_scdc_enable(struct tegra_sor *sor)
2341{
2342 struct i2c_adapter *ddc = sor->output.ddc;
2343
2344 drm_scdc_set_high_tmds_clock_ratio(ddc, true);
2345 drm_scdc_set_scrambling(ddc, true);
2346
2347 tegra_sor_hdmi_enable_scrambling(sor);
2348}
2349
2350static void tegra_sor_hdmi_scdc_work(struct work_struct *work)
2351{
2352 struct tegra_sor *sor = container_of(work, struct tegra_sor, scdc.work);
2353 struct i2c_adapter *ddc = sor->output.ddc;
2354
2355 if (!drm_scdc_get_scrambling_status(ddc)) {
2356 DRM_DEBUG_KMS("SCDC not scrambled\n");
2357 tegra_sor_hdmi_scdc_enable(sor);
2358 }
2359
2360 schedule_delayed_work(&sor->scdc, msecs_to_jiffies(5000));
2361}
2362
2363static void tegra_sor_hdmi_scdc_start(struct tegra_sor *sor)
2364{
2365 struct drm_scdc *scdc = &sor->output.connector.display_info.hdmi.scdc;
2366 struct drm_display_mode *mode;
2367
2368 mode = &sor->output.encoder.crtc->state->adjusted_mode;
2369
2370 if (mode->clock >= 340000 && scdc->supported) {
2371 schedule_delayed_work(&sor->scdc, msecs_to_jiffies(5000));
2372 tegra_sor_hdmi_scdc_enable(sor);
2373 sor->scdc_enabled = true;
2374 }
2375}
2376
Thierry Reding459cc2c2015-07-30 10:34:24 +02002377static void tegra_sor_hdmi_disable(struct drm_encoder *encoder)
2378{
2379 struct tegra_output *output = encoder_to_output(encoder);
2380 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
2381 struct tegra_sor *sor = to_sor(output);
2382 u32 value;
2383 int err;
2384
Thierry Reding8e2988a72018-12-03 15:46:03 +01002385 tegra_sor_audio_unprepare(sor);
Thierry Reding36e90222017-10-12 19:14:21 +02002386 tegra_sor_hdmi_scdc_stop(sor);
2387
Thierry Reding459cc2c2015-07-30 10:34:24 +02002388 err = tegra_sor_detach(sor);
2389 if (err < 0)
2390 dev_err(sor->dev, "failed to detach SOR: %d\n", err);
2391
2392 tegra_sor_writel(sor, 0, SOR_STATE1);
2393 tegra_sor_update(sor);
2394
2395 /* disable display to SOR clock */
2396 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
Thierry Redingc57997b2017-10-12 19:12:57 +02002397
2398 if (!sor->soc->has_nvdisplay)
2399 value &= ~(SOR1_TIMING_CYA | SOR_ENABLE(1));
2400 else
2401 value &= ~SOR_ENABLE(sor->index);
2402
Thierry Reding459cc2c2015-07-30 10:34:24 +02002403 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
2404
2405 tegra_dc_commit(dc);
2406
2407 err = tegra_sor_power_down(sor);
2408 if (err < 0)
2409 dev_err(sor->dev, "failed to power down SOR: %d\n", err);
2410
Thierry Redingc57997b2017-10-12 19:12:57 +02002411 err = tegra_io_pad_power_disable(sor->pad);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002412 if (err < 0)
Thierry Redingc57997b2017-10-12 19:12:57 +02002413 dev_err(sor->dev, "failed to power off I/O pad: %d\n", err);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002414
Thierry Redingaaff8bd2015-08-07 16:04:54 +02002415 pm_runtime_put(sor->dev);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002416}
2417
2418static void tegra_sor_hdmi_enable(struct drm_encoder *encoder)
2419{
2420 struct tegra_output *output = encoder_to_output(encoder);
2421 unsigned int h_ref_to_sync = 1, pulse_start, max_ac;
2422 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002423 struct tegra_sor_hdmi_settings *settings;
2424 struct tegra_sor *sor = to_sor(output);
Thierry Redingc31efa72015-09-08 16:09:22 +02002425 struct tegra_sor_state *state;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002426 struct drm_display_mode *mode;
Thierry Reding36e90222017-10-12 19:14:21 +02002427 unsigned long rate, pclk;
Thierry Reding30b49432015-08-03 15:50:32 +02002428 unsigned int div, i;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002429 u32 value;
2430 int err;
2431
Thierry Redingc31efa72015-09-08 16:09:22 +02002432 state = to_sor_state(output->connector.state);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002433 mode = &encoder->crtc->state->adjusted_mode;
Thierry Reding36e90222017-10-12 19:14:21 +02002434 pclk = mode->clock * 1000;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002435
Thierry Redingaaff8bd2015-08-07 16:04:54 +02002436 pm_runtime_get_sync(sor->dev);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002437
Thierry Reding25bb2ce2015-08-03 14:23:29 +02002438 /* switch to safe parent clock */
2439 err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
Thierry Redinge1335e22017-10-12 17:53:11 +02002440 if (err < 0) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002441 dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
Thierry Redinge1335e22017-10-12 17:53:11 +02002442 return;
2443 }
Thierry Reding459cc2c2015-07-30 10:34:24 +02002444
2445 div = clk_get_rate(sor->clk) / 1000000 * 4;
2446
Thierry Redingc57997b2017-10-12 19:12:57 +02002447 err = tegra_io_pad_power_enable(sor->pad);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002448 if (err < 0)
Thierry Redingc57997b2017-10-12 19:12:57 +02002449 dev_err(sor->dev, "failed to power on I/O pad: %d\n", err);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002450
2451 usleep_range(20, 100);
2452
Thierry Reding880cee02017-10-12 19:04:17 +02002453 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002454 value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
Thierry Reding880cee02017-10-12 19:04:17 +02002455 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002456
2457 usleep_range(20, 100);
2458
Thierry Reding880cee02017-10-12 19:04:17 +02002459 value = tegra_sor_readl(sor, sor->soc->regs->pll3);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002460 value &= ~SOR_PLL3_PLL_VDD_MODE_3V3;
Thierry Reding880cee02017-10-12 19:04:17 +02002461 tegra_sor_writel(sor, value, sor->soc->regs->pll3);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002462
Thierry Reding880cee02017-10-12 19:04:17 +02002463 value = tegra_sor_readl(sor, sor->soc->regs->pll0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002464 value &= ~SOR_PLL0_VCOPD;
2465 value &= ~SOR_PLL0_PWR;
Thierry Reding880cee02017-10-12 19:04:17 +02002466 tegra_sor_writel(sor, value, sor->soc->regs->pll0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002467
Thierry Reding880cee02017-10-12 19:04:17 +02002468 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002469 value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
Thierry Reding880cee02017-10-12 19:04:17 +02002470 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002471
2472 usleep_range(200, 400);
2473
Thierry Reding880cee02017-10-12 19:04:17 +02002474 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002475 value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
2476 value &= ~SOR_PLL2_PORT_POWERDOWN;
Thierry Reding880cee02017-10-12 19:04:17 +02002477 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002478
2479 usleep_range(20, 100);
2480
Thierry Reding880cee02017-10-12 19:04:17 +02002481 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002482 value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
2483 SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2;
Thierry Reding880cee02017-10-12 19:04:17 +02002484 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002485
2486 while (true) {
2487 value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
2488 if ((value & SOR_LANE_SEQ_CTL_STATE_BUSY) == 0)
2489 break;
2490
2491 usleep_range(250, 1000);
2492 }
2493
2494 value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
2495 SOR_LANE_SEQ_CTL_POWER_STATE_UP | SOR_LANE_SEQ_CTL_DELAY(5);
2496 tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
2497
2498 while (true) {
2499 value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
2500 if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
2501 break;
2502
2503 usleep_range(250, 1000);
2504 }
2505
2506 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
2507 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
2508 value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
2509
Thierry Reding36e90222017-10-12 19:14:21 +02002510 if (mode->clock < 340000) {
2511 DRM_DEBUG_KMS("setting 2.7 GHz link speed\n");
Thierry Reding459cc2c2015-07-30 10:34:24 +02002512 value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G2_70;
Thierry Reding36e90222017-10-12 19:14:21 +02002513 } else {
2514 DRM_DEBUG_KMS("setting 5.4 GHz link speed\n");
Thierry Reding459cc2c2015-07-30 10:34:24 +02002515 value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G5_40;
Thierry Reding36e90222017-10-12 19:14:21 +02002516 }
Thierry Reding459cc2c2015-07-30 10:34:24 +02002517
2518 value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK;
2519 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
2520
Thierry Redingc57997b2017-10-12 19:12:57 +02002521 /* SOR pad PLL stabilization time */
2522 usleep_range(250, 1000);
2523
2524 value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
2525 value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
2526 value |= SOR_DP_LINKCTL_LANE_COUNT(4);
2527 tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
2528
Thierry Reding459cc2c2015-07-30 10:34:24 +02002529 value = tegra_sor_readl(sor, SOR_DP_SPARE0);
Thierry Redingc57997b2017-10-12 19:12:57 +02002530 value &= ~SOR_DP_SPARE_DISP_VIDEO_PREAMBLE;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002531 value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
Thierry Redingc57997b2017-10-12 19:12:57 +02002532 value &= ~SOR_DP_SPARE_SEQ_ENABLE;
2533 value &= ~SOR_DP_SPARE_MACRO_SOR_CLK;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002534 tegra_sor_writel(sor, value, SOR_DP_SPARE0);
2535
2536 value = SOR_SEQ_CTL_PU_PC(0) | SOR_SEQ_CTL_PU_PC_ALT(0) |
2537 SOR_SEQ_CTL_PD_PC(8) | SOR_SEQ_CTL_PD_PC_ALT(8);
2538 tegra_sor_writel(sor, value, SOR_SEQ_CTL);
2539
2540 value = SOR_SEQ_INST_DRIVE_PWM_OUT_LO | SOR_SEQ_INST_HALT |
2541 SOR_SEQ_INST_WAIT_VSYNC | SOR_SEQ_INST_WAIT(1);
2542 tegra_sor_writel(sor, value, SOR_SEQ_INST(0));
2543 tegra_sor_writel(sor, value, SOR_SEQ_INST(8));
2544
Thierry Redingc57997b2017-10-12 19:12:57 +02002545 if (!sor->soc->has_nvdisplay) {
2546 /* program the reference clock */
2547 value = SOR_REFCLK_DIV_INT(div) | SOR_REFCLK_DIV_FRAC(div);
2548 tegra_sor_writel(sor, value, SOR_REFCLK);
2549 }
Thierry Reding459cc2c2015-07-30 10:34:24 +02002550
Thierry Reding30b49432015-08-03 15:50:32 +02002551 /* XXX not in TRM */
2552 for (value = 0, i = 0; i < 5; i++)
2553 value |= SOR_XBAR_CTRL_LINK0_XSEL(i, sor->soc->xbar_cfg[i]) |
2554 SOR_XBAR_CTRL_LINK1_XSEL(i, i);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002555
2556 tegra_sor_writel(sor, 0x00000000, SOR_XBAR_POL);
Thierry Reding30b49432015-08-03 15:50:32 +02002557 tegra_sor_writel(sor, value, SOR_XBAR_CTRL);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002558
Thierry Reding25bb2ce2015-08-03 14:23:29 +02002559 /* switch to parent clock */
Thierry Redinge1335e22017-10-12 17:53:11 +02002560 err = clk_set_parent(sor->clk, sor->clk_parent);
2561 if (err < 0) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002562 dev_err(sor->dev, "failed to set parent clock: %d\n", err);
Thierry Redinge1335e22017-10-12 17:53:11 +02002563 return;
2564 }
2565
2566 err = tegra_sor_set_parent_clock(sor, sor->clk_pad);
2567 if (err < 0) {
2568 dev_err(sor->dev, "failed to set pad clock: %d\n", err);
2569 return;
2570 }
Thierry Reding459cc2c2015-07-30 10:34:24 +02002571
Thierry Reding36e90222017-10-12 19:14:21 +02002572 /* adjust clock rate for HDMI 2.0 modes */
2573 rate = clk_get_rate(sor->clk_parent);
2574
2575 if (mode->clock >= 340000)
2576 rate /= 2;
2577
2578 DRM_DEBUG_KMS("setting clock to %lu Hz, mode: %lu Hz\n", rate, pclk);
2579
2580 clk_set_rate(sor->clk, rate);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002581
Thierry Redingc57997b2017-10-12 19:12:57 +02002582 if (!sor->soc->has_nvdisplay) {
2583 value = SOR_INPUT_CONTROL_HDMI_SRC_SELECT(dc->pipe);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002584
Thierry Redingc57997b2017-10-12 19:12:57 +02002585 /* XXX is this the proper check? */
2586 if (mode->clock < 75000)
2587 value |= SOR_INPUT_CONTROL_ARM_VIDEO_RANGE_LIMITED;
2588
2589 tegra_sor_writel(sor, value, SOR_INPUT_CONTROL);
2590 }
Thierry Reding459cc2c2015-07-30 10:34:24 +02002591
2592 max_ac = ((mode->htotal - mode->hdisplay) - SOR_REKEY - 18) / 32;
2593
2594 value = SOR_HDMI_CTRL_ENABLE | SOR_HDMI_CTRL_MAX_AC_PACKET(max_ac) |
2595 SOR_HDMI_CTRL_AUDIO_LAYOUT | SOR_HDMI_CTRL_REKEY(SOR_REKEY);
2596 tegra_sor_writel(sor, value, SOR_HDMI_CTRL);
2597
Thierry Redingc57997b2017-10-12 19:12:57 +02002598 if (!dc->soc->has_nvdisplay) {
2599 /* H_PULSE2 setup */
2600 pulse_start = h_ref_to_sync +
2601 (mode->hsync_end - mode->hsync_start) +
2602 (mode->htotal - mode->hsync_end) - 10;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002603
Thierry Redingc57997b2017-10-12 19:12:57 +02002604 value = PULSE_LAST_END_A | PULSE_QUAL_VACTIVE |
2605 PULSE_POLARITY_HIGH | PULSE_MODE_NORMAL;
2606 tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_CONTROL);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002607
Thierry Redingc57997b2017-10-12 19:12:57 +02002608 value = PULSE_END(pulse_start + 8) | PULSE_START(pulse_start);
2609 tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_POSITION_A);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002610
Thierry Redingc57997b2017-10-12 19:12:57 +02002611 value = tegra_dc_readl(dc, DC_DISP_DISP_SIGNAL_OPTIONS0);
2612 value |= H_PULSE2_ENABLE;
2613 tegra_dc_writel(dc, value, DC_DISP_DISP_SIGNAL_OPTIONS0);
2614 }
Thierry Reding459cc2c2015-07-30 10:34:24 +02002615
2616 /* infoframe setup */
2617 err = tegra_sor_hdmi_setup_avi_infoframe(sor, mode);
2618 if (err < 0)
2619 dev_err(sor->dev, "failed to setup AVI infoframe: %d\n", err);
2620
2621 /* XXX HDMI audio support not implemented yet */
2622 tegra_sor_hdmi_disable_audio_infoframe(sor);
2623
2624 /* use single TMDS protocol */
2625 value = tegra_sor_readl(sor, SOR_STATE1);
2626 value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
2627 value |= SOR_STATE_ASY_PROTOCOL_SINGLE_TMDS_A;
2628 tegra_sor_writel(sor, value, SOR_STATE1);
2629
2630 /* power up pad calibration */
Thierry Reding880cee02017-10-12 19:04:17 +02002631 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002632 value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
Thierry Reding880cee02017-10-12 19:04:17 +02002633 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002634
2635 /* production settings */
2636 settings = tegra_sor_hdmi_find_settings(sor, mode->clock * 1000);
Dan Carpenterdb8b42f2015-08-17 17:37:03 +03002637 if (!settings) {
2638 dev_err(sor->dev, "no settings for pixel clock %d Hz\n",
2639 mode->clock * 1000);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002640 return;
2641 }
2642
Thierry Reding880cee02017-10-12 19:04:17 +02002643 value = tegra_sor_readl(sor, sor->soc->regs->pll0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002644 value &= ~SOR_PLL0_ICHPMP_MASK;
Thierry Redingc57997b2017-10-12 19:12:57 +02002645 value &= ~SOR_PLL0_FILTER_MASK;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002646 value &= ~SOR_PLL0_VCOCAP_MASK;
2647 value |= SOR_PLL0_ICHPMP(settings->ichpmp);
Thierry Redingc57997b2017-10-12 19:12:57 +02002648 value |= SOR_PLL0_FILTER(settings->filter);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002649 value |= SOR_PLL0_VCOCAP(settings->vcocap);
Thierry Reding880cee02017-10-12 19:04:17 +02002650 tegra_sor_writel(sor, value, sor->soc->regs->pll0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002651
Thierry Redingc57997b2017-10-12 19:12:57 +02002652 /* XXX not in TRM */
Thierry Reding880cee02017-10-12 19:04:17 +02002653 value = tegra_sor_readl(sor, sor->soc->regs->pll1);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002654 value &= ~SOR_PLL1_LOADADJ_MASK;
Thierry Redingc57997b2017-10-12 19:12:57 +02002655 value &= ~SOR_PLL1_TMDS_TERMADJ_MASK;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002656 value |= SOR_PLL1_LOADADJ(settings->loadadj);
Thierry Redingc57997b2017-10-12 19:12:57 +02002657 value |= SOR_PLL1_TMDS_TERMADJ(settings->tmds_termadj);
2658 value |= SOR_PLL1_TMDS_TERM;
Thierry Reding880cee02017-10-12 19:04:17 +02002659 tegra_sor_writel(sor, value, sor->soc->regs->pll1);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002660
Thierry Reding880cee02017-10-12 19:04:17 +02002661 value = tegra_sor_readl(sor, sor->soc->regs->pll3);
Thierry Redingc57997b2017-10-12 19:12:57 +02002662 value &= ~SOR_PLL3_BG_TEMP_COEF_MASK;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002663 value &= ~SOR_PLL3_BG_VREF_LEVEL_MASK;
Thierry Redingc57997b2017-10-12 19:12:57 +02002664 value &= ~SOR_PLL3_AVDD10_LEVEL_MASK;
2665 value &= ~SOR_PLL3_AVDD14_LEVEL_MASK;
2666 value |= SOR_PLL3_BG_TEMP_COEF(settings->bg_temp_coef);
2667 value |= SOR_PLL3_BG_VREF_LEVEL(settings->bg_vref_level);
2668 value |= SOR_PLL3_AVDD10_LEVEL(settings->avdd10_level);
2669 value |= SOR_PLL3_AVDD14_LEVEL(settings->avdd14_level);
Thierry Reding880cee02017-10-12 19:04:17 +02002670 tegra_sor_writel(sor, value, sor->soc->regs->pll3);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002671
Thierry Redingc57997b2017-10-12 19:12:57 +02002672 value = settings->drive_current[3] << 24 |
2673 settings->drive_current[2] << 16 |
2674 settings->drive_current[1] << 8 |
2675 settings->drive_current[0] << 0;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002676 tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT0);
2677
Thierry Redingc57997b2017-10-12 19:12:57 +02002678 value = settings->preemphasis[3] << 24 |
2679 settings->preemphasis[2] << 16 |
2680 settings->preemphasis[1] << 8 |
2681 settings->preemphasis[0] << 0;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002682 tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS0);
2683
Thierry Reding880cee02017-10-12 19:04:17 +02002684 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002685 value &= ~SOR_DP_PADCTL_TX_PU_MASK;
2686 value |= SOR_DP_PADCTL_TX_PU_ENABLE;
Thierry Redingc57997b2017-10-12 19:12:57 +02002687 value |= SOR_DP_PADCTL_TX_PU(settings->tx_pu_value);
Thierry Reding880cee02017-10-12 19:04:17 +02002688 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002689
Thierry Redingc57997b2017-10-12 19:12:57 +02002690 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl2);
2691 value &= ~SOR_DP_PADCTL_SPAREPLL_MASK;
2692 value |= SOR_DP_PADCTL_SPAREPLL(settings->sparepll);
2693 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl2);
2694
Thierry Reding459cc2c2015-07-30 10:34:24 +02002695 /* power down pad calibration */
Thierry Reding880cee02017-10-12 19:04:17 +02002696 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002697 value |= SOR_DP_PADCTL_PAD_CAL_PD;
Thierry Reding880cee02017-10-12 19:04:17 +02002698 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002699
Thierry Redingc57997b2017-10-12 19:12:57 +02002700 if (!dc->soc->has_nvdisplay) {
2701 /* miscellaneous display controller settings */
2702 value = VSYNC_H_POSITION(1);
2703 tegra_dc_writel(dc, value, DC_DISP_DISP_TIMING_OPTIONS);
2704 }
Thierry Reding459cc2c2015-07-30 10:34:24 +02002705
2706 value = tegra_dc_readl(dc, DC_DISP_DISP_COLOR_CONTROL);
2707 value &= ~DITHER_CONTROL_MASK;
2708 value &= ~BASE_COLOR_SIZE_MASK;
2709
Thierry Redingc31efa72015-09-08 16:09:22 +02002710 switch (state->bpc) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002711 case 6:
2712 value |= BASE_COLOR_SIZE_666;
2713 break;
2714
2715 case 8:
2716 value |= BASE_COLOR_SIZE_888;
2717 break;
2718
Thierry Redingc57997b2017-10-12 19:12:57 +02002719 case 10:
2720 value |= BASE_COLOR_SIZE_101010;
2721 break;
2722
2723 case 12:
2724 value |= BASE_COLOR_SIZE_121212;
2725 break;
2726
Thierry Reding459cc2c2015-07-30 10:34:24 +02002727 default:
Thierry Redingc31efa72015-09-08 16:09:22 +02002728 WARN(1, "%u bits-per-color not supported\n", state->bpc);
2729 value |= BASE_COLOR_SIZE_888;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002730 break;
2731 }
2732
2733 tegra_dc_writel(dc, value, DC_DISP_DISP_COLOR_CONTROL);
2734
Thierry Redingc57997b2017-10-12 19:12:57 +02002735 /* XXX set display head owner */
2736 value = tegra_sor_readl(sor, SOR_STATE1);
2737 value &= ~SOR_STATE_ASY_OWNER_MASK;
2738 value |= SOR_STATE_ASY_OWNER(1 + dc->pipe);
2739 tegra_sor_writel(sor, value, SOR_STATE1);
2740
Thierry Reding459cc2c2015-07-30 10:34:24 +02002741 err = tegra_sor_power_up(sor, 250);
2742 if (err < 0)
2743 dev_err(sor->dev, "failed to power up SOR: %d\n", err);
2744
Thierry Reding2bd1dd32015-08-03 15:46:15 +02002745 /* configure dynamic range of output */
Thierry Reding880cee02017-10-12 19:04:17 +02002746 value = tegra_sor_readl(sor, sor->soc->regs->head_state0 + dc->pipe);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002747 value &= ~SOR_HEAD_STATE_RANGECOMPRESS_MASK;
2748 value &= ~SOR_HEAD_STATE_DYNRANGE_MASK;
Thierry Reding880cee02017-10-12 19:04:17 +02002749 tegra_sor_writel(sor, value, sor->soc->regs->head_state0 + dc->pipe);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002750
Thierry Reding2bd1dd32015-08-03 15:46:15 +02002751 /* configure colorspace */
Thierry Reding880cee02017-10-12 19:04:17 +02002752 value = tegra_sor_readl(sor, sor->soc->regs->head_state0 + dc->pipe);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002753 value &= ~SOR_HEAD_STATE_COLORSPACE_MASK;
2754 value |= SOR_HEAD_STATE_COLORSPACE_RGB;
Thierry Reding880cee02017-10-12 19:04:17 +02002755 tegra_sor_writel(sor, value, sor->soc->regs->head_state0 + dc->pipe);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002756
Thierry Redingc31efa72015-09-08 16:09:22 +02002757 tegra_sor_mode_set(sor, mode, state);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002758
2759 tegra_sor_update(sor);
2760
Thierry Redingc57997b2017-10-12 19:12:57 +02002761 /* program preamble timing in SOR (XXX) */
2762 value = tegra_sor_readl(sor, SOR_DP_SPARE0);
2763 value &= ~SOR_DP_SPARE_DISP_VIDEO_PREAMBLE;
2764 tegra_sor_writel(sor, value, SOR_DP_SPARE0);
2765
Thierry Reding459cc2c2015-07-30 10:34:24 +02002766 err = tegra_sor_attach(sor);
2767 if (err < 0)
2768 dev_err(sor->dev, "failed to attach SOR: %d\n", err);
2769
2770 /* enable display to SOR clock and generate HDMI preamble */
2771 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
Thierry Redingc57997b2017-10-12 19:12:57 +02002772
2773 if (!sor->soc->has_nvdisplay)
2774 value |= SOR_ENABLE(1) | SOR1_TIMING_CYA;
2775 else
2776 value |= SOR_ENABLE(sor->index);
2777
Thierry Reding459cc2c2015-07-30 10:34:24 +02002778 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
2779
Thierry Redingc57997b2017-10-12 19:12:57 +02002780 if (dc->soc->has_nvdisplay) {
2781 value = tegra_dc_readl(dc, DC_DISP_CORE_SOR_SET_CONTROL(sor->index));
2782 value &= ~PROTOCOL_MASK;
2783 value |= PROTOCOL_SINGLE_TMDS_A;
2784 tegra_dc_writel(dc, value, DC_DISP_CORE_SOR_SET_CONTROL(sor->index));
2785 }
2786
Thierry Reding459cc2c2015-07-30 10:34:24 +02002787 tegra_dc_commit(dc);
2788
2789 err = tegra_sor_wakeup(sor);
2790 if (err < 0)
2791 dev_err(sor->dev, "failed to wakeup SOR: %d\n", err);
Thierry Reding36e90222017-10-12 19:14:21 +02002792
2793 tegra_sor_hdmi_scdc_start(sor);
Thierry Reding8e2988a72018-12-03 15:46:03 +01002794 tegra_sor_audio_prepare(sor);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002795}
2796
2797static const struct drm_encoder_helper_funcs tegra_sor_hdmi_helpers = {
2798 .disable = tegra_sor_hdmi_disable,
2799 .enable = tegra_sor_hdmi_enable,
2800 .atomic_check = tegra_sor_encoder_atomic_check,
2801};
2802
Thierry Reding6b6b6042013-11-15 16:06:05 +01002803static int tegra_sor_init(struct host1x_client *client)
2804{
Thierry Reding9910f5c2014-05-22 09:57:15 +02002805 struct drm_device *drm = dev_get_drvdata(client->parent);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002806 const struct drm_encoder_helper_funcs *helpers = NULL;
Thierry Reding6b6b6042013-11-15 16:06:05 +01002807 struct tegra_sor *sor = host1x_client_to_sor(client);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002808 int connector = DRM_MODE_CONNECTOR_Unknown;
2809 int encoder = DRM_MODE_ENCODER_NONE;
Thierry Reding8e2988a72018-12-03 15:46:03 +01002810 u32 value;
Thierry Reding6b6b6042013-11-15 16:06:05 +01002811 int err;
2812
Thierry Reding9542c232015-07-08 13:39:09 +02002813 if (!sor->aux) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002814 if (sor->soc->supports_hdmi) {
2815 connector = DRM_MODE_CONNECTOR_HDMIA;
2816 encoder = DRM_MODE_ENCODER_TMDS;
2817 helpers = &tegra_sor_hdmi_helpers;
2818 } else if (sor->soc->supports_lvds) {
2819 connector = DRM_MODE_CONNECTOR_LVDS;
2820 encoder = DRM_MODE_ENCODER_LVDS;
2821 }
2822 } else {
2823 if (sor->soc->supports_edp) {
2824 connector = DRM_MODE_CONNECTOR_eDP;
2825 encoder = DRM_MODE_ENCODER_TMDS;
2826 helpers = &tegra_sor_edp_helpers;
2827 } else if (sor->soc->supports_dp) {
2828 connector = DRM_MODE_CONNECTOR_DisplayPort;
2829 encoder = DRM_MODE_ENCODER_TMDS;
2830 }
2831 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01002832
Thierry Reding6b6b6042013-11-15 16:06:05 +01002833 sor->output.dev = sor->dev;
Thierry Reding6b6b6042013-11-15 16:06:05 +01002834
Thierry Reding6fad8f62014-11-28 15:41:34 +01002835 drm_connector_init(drm, &sor->output.connector,
2836 &tegra_sor_connector_funcs,
Thierry Reding459cc2c2015-07-30 10:34:24 +02002837 connector);
Thierry Reding6fad8f62014-11-28 15:41:34 +01002838 drm_connector_helper_add(&sor->output.connector,
2839 &tegra_sor_connector_helper_funcs);
2840 sor->output.connector.dpms = DRM_MODE_DPMS_OFF;
2841
Thierry Reding6fad8f62014-11-28 15:41:34 +01002842 drm_encoder_init(drm, &sor->output.encoder, &tegra_sor_encoder_funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +02002843 encoder, NULL);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002844 drm_encoder_helper_add(&sor->output.encoder, helpers);
Thierry Reding6fad8f62014-11-28 15:41:34 +01002845
Daniel Vettercde4c442018-07-09 10:40:07 +02002846 drm_connector_attach_encoder(&sor->output.connector,
Thierry Reding6fad8f62014-11-28 15:41:34 +01002847 &sor->output.encoder);
2848 drm_connector_register(&sor->output.connector);
2849
Thierry Redingea130b22014-12-19 15:51:35 +01002850 err = tegra_output_init(drm, &sor->output);
2851 if (err < 0) {
2852 dev_err(client->dev, "failed to initialize output: %d\n", err);
2853 return err;
2854 }
Thierry Reding6fad8f62014-11-28 15:41:34 +01002855
Thierry Redingc57997b2017-10-12 19:12:57 +02002856 tegra_output_find_possible_crtcs(&sor->output, drm);
Thierry Reding6b6b6042013-11-15 16:06:05 +01002857
Thierry Reding9542c232015-07-08 13:39:09 +02002858 if (sor->aux) {
2859 err = drm_dp_aux_attach(sor->aux, &sor->output);
Thierry Reding6b6b6042013-11-15 16:06:05 +01002860 if (err < 0) {
2861 dev_err(sor->dev, "failed to attach DP: %d\n", err);
2862 return err;
2863 }
2864 }
2865
Tomeu Vizoso535a65d2015-03-30 10:33:03 +02002866 /*
2867 * XXX: Remove this reset once proper hand-over from firmware to
2868 * kernel is possible.
2869 */
Jon Hunterf8c79122016-07-01 14:21:38 +01002870 if (sor->rst) {
2871 err = reset_control_assert(sor->rst);
2872 if (err < 0) {
2873 dev_err(sor->dev, "failed to assert SOR reset: %d\n",
2874 err);
2875 return err;
2876 }
Tomeu Vizoso535a65d2015-03-30 10:33:03 +02002877 }
2878
Thierry Reding6fad8f62014-11-28 15:41:34 +01002879 err = clk_prepare_enable(sor->clk);
2880 if (err < 0) {
2881 dev_err(sor->dev, "failed to enable clock: %d\n", err);
2882 return err;
2883 }
2884
Tomeu Vizoso535a65d2015-03-30 10:33:03 +02002885 usleep_range(1000, 3000);
2886
Jon Hunterf8c79122016-07-01 14:21:38 +01002887 if (sor->rst) {
2888 err = reset_control_deassert(sor->rst);
2889 if (err < 0) {
2890 dev_err(sor->dev, "failed to deassert SOR reset: %d\n",
2891 err);
2892 return err;
2893 }
Tomeu Vizoso535a65d2015-03-30 10:33:03 +02002894 }
2895
Thierry Reding6fad8f62014-11-28 15:41:34 +01002896 err = clk_prepare_enable(sor->clk_safe);
2897 if (err < 0)
2898 return err;
2899
2900 err = clk_prepare_enable(sor->clk_dp);
2901 if (err < 0)
2902 return err;
2903
Thierry Reding8e2988a72018-12-03 15:46:03 +01002904 /*
2905 * Enable and unmask the HDA codec SCRATCH0 register interrupt. This
2906 * is used for interoperability between the HDA codec driver and the
2907 * HDMI/DP driver.
2908 */
2909 value = SOR_INT_CODEC_SCRATCH1 | SOR_INT_CODEC_SCRATCH0;
2910 tegra_sor_writel(sor, value, SOR_INT_ENABLE);
2911 tegra_sor_writel(sor, value, SOR_INT_MASK);
2912
Thierry Reding6b6b6042013-11-15 16:06:05 +01002913 return 0;
2914}
2915
2916static int tegra_sor_exit(struct host1x_client *client)
2917{
2918 struct tegra_sor *sor = host1x_client_to_sor(client);
2919 int err;
2920
Thierry Reding8e2988a72018-12-03 15:46:03 +01002921 tegra_sor_writel(sor, 0, SOR_INT_MASK);
2922 tegra_sor_writel(sor, 0, SOR_INT_ENABLE);
2923
Thierry Reding328ec692014-12-19 15:55:08 +01002924 tegra_output_exit(&sor->output);
2925
Thierry Reding9542c232015-07-08 13:39:09 +02002926 if (sor->aux) {
2927 err = drm_dp_aux_detach(sor->aux);
Thierry Reding6b6b6042013-11-15 16:06:05 +01002928 if (err < 0) {
2929 dev_err(sor->dev, "failed to detach DP: %d\n", err);
2930 return err;
2931 }
2932 }
2933
Thierry Reding6fad8f62014-11-28 15:41:34 +01002934 clk_disable_unprepare(sor->clk_safe);
2935 clk_disable_unprepare(sor->clk_dp);
2936 clk_disable_unprepare(sor->clk);
2937
Thierry Reding6b6b6042013-11-15 16:06:05 +01002938 return 0;
2939}
2940
2941static const struct host1x_client_ops sor_client_ops = {
2942 .init = tegra_sor_init,
2943 .exit = tegra_sor_exit,
2944};
2945
Thierry Reding459cc2c2015-07-30 10:34:24 +02002946static const struct tegra_sor_ops tegra_sor_edp_ops = {
2947 .name = "eDP",
2948};
2949
2950static int tegra_sor_hdmi_probe(struct tegra_sor *sor)
2951{
2952 int err;
2953
2954 sor->avdd_io_supply = devm_regulator_get(sor->dev, "avdd-io");
2955 if (IS_ERR(sor->avdd_io_supply)) {
2956 dev_err(sor->dev, "cannot get AVDD I/O supply: %ld\n",
2957 PTR_ERR(sor->avdd_io_supply));
2958 return PTR_ERR(sor->avdd_io_supply);
2959 }
2960
2961 err = regulator_enable(sor->avdd_io_supply);
2962 if (err < 0) {
2963 dev_err(sor->dev, "failed to enable AVDD I/O supply: %d\n",
2964 err);
2965 return err;
2966 }
2967
2968 sor->vdd_pll_supply = devm_regulator_get(sor->dev, "vdd-pll");
2969 if (IS_ERR(sor->vdd_pll_supply)) {
2970 dev_err(sor->dev, "cannot get VDD PLL supply: %ld\n",
2971 PTR_ERR(sor->vdd_pll_supply));
2972 return PTR_ERR(sor->vdd_pll_supply);
2973 }
2974
2975 err = regulator_enable(sor->vdd_pll_supply);
2976 if (err < 0) {
2977 dev_err(sor->dev, "failed to enable VDD PLL supply: %d\n",
2978 err);
2979 return err;
2980 }
2981
2982 sor->hdmi_supply = devm_regulator_get(sor->dev, "hdmi");
2983 if (IS_ERR(sor->hdmi_supply)) {
2984 dev_err(sor->dev, "cannot get HDMI supply: %ld\n",
2985 PTR_ERR(sor->hdmi_supply));
2986 return PTR_ERR(sor->hdmi_supply);
2987 }
2988
2989 err = regulator_enable(sor->hdmi_supply);
2990 if (err < 0) {
2991 dev_err(sor->dev, "failed to enable HDMI supply: %d\n", err);
2992 return err;
2993 }
2994
Thierry Reding36e90222017-10-12 19:14:21 +02002995 INIT_DELAYED_WORK(&sor->scdc, tegra_sor_hdmi_scdc_work);
2996
Thierry Reding459cc2c2015-07-30 10:34:24 +02002997 return 0;
2998}
2999
3000static int tegra_sor_hdmi_remove(struct tegra_sor *sor)
3001{
3002 regulator_disable(sor->hdmi_supply);
3003 regulator_disable(sor->vdd_pll_supply);
3004 regulator_disable(sor->avdd_io_supply);
3005
3006 return 0;
3007}
3008
3009static const struct tegra_sor_ops tegra_sor_hdmi_ops = {
3010 .name = "HDMI",
3011 .probe = tegra_sor_hdmi_probe,
3012 .remove = tegra_sor_hdmi_remove,
3013};
3014
Thierry Reding30b49432015-08-03 15:50:32 +02003015static const u8 tegra124_sor_xbar_cfg[5] = {
3016 0, 1, 2, 3, 4
3017};
3018
Thierry Reding880cee02017-10-12 19:04:17 +02003019static const struct tegra_sor_regs tegra124_sor_regs = {
3020 .head_state0 = 0x05,
3021 .head_state1 = 0x07,
3022 .head_state2 = 0x09,
3023 .head_state3 = 0x0b,
3024 .head_state4 = 0x0d,
3025 .head_state5 = 0x0f,
3026 .pll0 = 0x17,
3027 .pll1 = 0x18,
3028 .pll2 = 0x19,
3029 .pll3 = 0x1a,
3030 .dp_padctl0 = 0x5c,
3031 .dp_padctl2 = 0x73,
3032};
3033
Thierry Reding459cc2c2015-07-30 10:34:24 +02003034static const struct tegra_sor_soc tegra124_sor = {
3035 .supports_edp = true,
3036 .supports_lvds = true,
3037 .supports_hdmi = false,
3038 .supports_dp = false,
Thierry Reding880cee02017-10-12 19:04:17 +02003039 .regs = &tegra124_sor_regs,
Thierry Redingc57997b2017-10-12 19:12:57 +02003040 .has_nvdisplay = false,
Thierry Reding30b49432015-08-03 15:50:32 +02003041 .xbar_cfg = tegra124_sor_xbar_cfg,
Thierry Reding459cc2c2015-07-30 10:34:24 +02003042};
3043
Thierry Reding880cee02017-10-12 19:04:17 +02003044static const struct tegra_sor_regs tegra210_sor_regs = {
3045 .head_state0 = 0x05,
3046 .head_state1 = 0x07,
3047 .head_state2 = 0x09,
3048 .head_state3 = 0x0b,
3049 .head_state4 = 0x0d,
3050 .head_state5 = 0x0f,
3051 .pll0 = 0x17,
3052 .pll1 = 0x18,
3053 .pll2 = 0x19,
3054 .pll3 = 0x1a,
3055 .dp_padctl0 = 0x5c,
3056 .dp_padctl2 = 0x73,
3057};
3058
Thierry Reding459cc2c2015-07-30 10:34:24 +02003059static const struct tegra_sor_soc tegra210_sor = {
3060 .supports_edp = true,
3061 .supports_lvds = false,
3062 .supports_hdmi = false,
3063 .supports_dp = false,
Thierry Reding880cee02017-10-12 19:04:17 +02003064 .regs = &tegra210_sor_regs,
Thierry Redingc57997b2017-10-12 19:12:57 +02003065 .has_nvdisplay = false,
Thierry Reding30b49432015-08-03 15:50:32 +02003066 .xbar_cfg = tegra124_sor_xbar_cfg,
3067};
3068
3069static const u8 tegra210_sor_xbar_cfg[5] = {
3070 2, 1, 0, 3, 4
Thierry Reding459cc2c2015-07-30 10:34:24 +02003071};
3072
3073static const struct tegra_sor_soc tegra210_sor1 = {
3074 .supports_edp = false,
3075 .supports_lvds = false,
3076 .supports_hdmi = true,
3077 .supports_dp = true,
3078
Thierry Reding880cee02017-10-12 19:04:17 +02003079 .regs = &tegra210_sor_regs,
Thierry Redingc57997b2017-10-12 19:12:57 +02003080 .has_nvdisplay = false,
Thierry Reding880cee02017-10-12 19:04:17 +02003081
Thierry Reding459cc2c2015-07-30 10:34:24 +02003082 .num_settings = ARRAY_SIZE(tegra210_sor_hdmi_defaults),
3083 .settings = tegra210_sor_hdmi_defaults,
Thierry Reding30b49432015-08-03 15:50:32 +02003084
3085 .xbar_cfg = tegra210_sor_xbar_cfg,
Thierry Reding459cc2c2015-07-30 10:34:24 +02003086};
3087
Thierry Redingc57997b2017-10-12 19:12:57 +02003088static const struct tegra_sor_regs tegra186_sor_regs = {
3089 .head_state0 = 0x151,
3090 .head_state1 = 0x154,
3091 .head_state2 = 0x157,
3092 .head_state3 = 0x15a,
3093 .head_state4 = 0x15d,
3094 .head_state5 = 0x160,
3095 .pll0 = 0x163,
3096 .pll1 = 0x164,
3097 .pll2 = 0x165,
3098 .pll3 = 0x166,
3099 .dp_padctl0 = 0x168,
3100 .dp_padctl2 = 0x16a,
3101};
3102
3103static const struct tegra_sor_soc tegra186_sor = {
3104 .supports_edp = false,
3105 .supports_lvds = false,
3106 .supports_hdmi = false,
3107 .supports_dp = true,
3108
3109 .regs = &tegra186_sor_regs,
3110 .has_nvdisplay = true,
3111
3112 .xbar_cfg = tegra124_sor_xbar_cfg,
3113};
3114
3115static const struct tegra_sor_soc tegra186_sor1 = {
3116 .supports_edp = false,
3117 .supports_lvds = false,
3118 .supports_hdmi = true,
3119 .supports_dp = true,
3120
3121 .regs = &tegra186_sor_regs,
3122 .has_nvdisplay = true,
3123
3124 .num_settings = ARRAY_SIZE(tegra186_sor_hdmi_defaults),
3125 .settings = tegra186_sor_hdmi_defaults,
3126
3127 .xbar_cfg = tegra124_sor_xbar_cfg,
3128};
3129
Thierry Reding9b6c14b2018-09-21 12:27:46 +02003130static const struct tegra_sor_regs tegra194_sor_regs = {
3131 .head_state0 = 0x151,
3132 .head_state1 = 0x155,
3133 .head_state2 = 0x159,
3134 .head_state3 = 0x15d,
3135 .head_state4 = 0x161,
3136 .head_state5 = 0x165,
3137 .pll0 = 0x169,
3138 .pll1 = 0x16a,
3139 .pll2 = 0x16b,
3140 .pll3 = 0x16c,
3141 .dp_padctl0 = 0x16e,
3142 .dp_padctl2 = 0x16f,
3143};
3144
3145static const struct tegra_sor_soc tegra194_sor = {
3146 .supports_edp = true,
3147 .supports_lvds = false,
3148 .supports_hdmi = true,
3149 .supports_dp = true,
3150
3151 .regs = &tegra194_sor_regs,
3152 .has_nvdisplay = true,
3153
3154 .num_settings = ARRAY_SIZE(tegra194_sor_hdmi_defaults),
3155 .settings = tegra194_sor_hdmi_defaults,
3156
3157 .xbar_cfg = tegra210_sor_xbar_cfg,
3158};
3159
Thierry Reding459cc2c2015-07-30 10:34:24 +02003160static const struct of_device_id tegra_sor_of_match[] = {
Thierry Reding9b6c14b2018-09-21 12:27:46 +02003161 { .compatible = "nvidia,tegra194-sor", .data = &tegra194_sor },
Thierry Redingc57997b2017-10-12 19:12:57 +02003162 { .compatible = "nvidia,tegra186-sor1", .data = &tegra186_sor1 },
3163 { .compatible = "nvidia,tegra186-sor", .data = &tegra186_sor },
Thierry Reding459cc2c2015-07-30 10:34:24 +02003164 { .compatible = "nvidia,tegra210-sor1", .data = &tegra210_sor1 },
3165 { .compatible = "nvidia,tegra210-sor", .data = &tegra210_sor },
3166 { .compatible = "nvidia,tegra124-sor", .data = &tegra124_sor },
3167 { },
3168};
3169MODULE_DEVICE_TABLE(of, tegra_sor_of_match);
3170
Thierry Redingc57997b2017-10-12 19:12:57 +02003171static int tegra_sor_parse_dt(struct tegra_sor *sor)
3172{
3173 struct device_node *np = sor->dev->of_node;
3174 u32 value;
3175 int err;
3176
3177 if (sor->soc->has_nvdisplay) {
3178 err = of_property_read_u32(np, "nvidia,interface", &value);
3179 if (err < 0)
3180 return err;
3181
3182 sor->index = value;
3183
3184 /*
3185 * override the default that we already set for Tegra210 and
3186 * earlier
3187 */
3188 sor->pad = TEGRA_IO_PAD_HDMI_DP0 + sor->index;
3189 }
3190
3191 return 0;
3192}
3193
Thierry Reding8e2988a72018-12-03 15:46:03 +01003194static irqreturn_t tegra_sor_irq(int irq, void *data)
3195{
3196 struct tegra_sor *sor = data;
3197 u32 value;
3198
3199 value = tegra_sor_readl(sor, SOR_INT_STATUS);
3200 tegra_sor_writel(sor, value, SOR_INT_STATUS);
3201
3202 if (value & SOR_INT_CODEC_SCRATCH0) {
3203 value = tegra_sor_readl(sor, SOR_AUDIO_HDA_CODEC_SCRATCH0);
3204
3205 if (value & SOR_AUDIO_HDA_CODEC_SCRATCH0_VALID) {
Thierry Redingcd54fb92019-01-03 15:23:15 +01003206 unsigned int format;
Thierry Reding8e2988a72018-12-03 15:46:03 +01003207
3208 format = value & SOR_AUDIO_HDA_CODEC_SCRATCH0_FMT_MASK;
3209
Thierry Redingfad7b802019-01-03 15:23:16 +01003210 tegra_hda_parse_format(format, &sor->format);
Thierry Reding8e2988a72018-12-03 15:46:03 +01003211
3212 tegra_sor_hdmi_audio_enable(sor);
3213 } else {
3214 tegra_sor_hdmi_audio_disable(sor);
3215 }
3216 }
3217
3218 return IRQ_HANDLED;
3219}
3220
Thierry Reding6b6b6042013-11-15 16:06:05 +01003221static int tegra_sor_probe(struct platform_device *pdev)
3222{
3223 struct device_node *np;
3224 struct tegra_sor *sor;
3225 struct resource *regs;
3226 int err;
3227
3228 sor = devm_kzalloc(&pdev->dev, sizeof(*sor), GFP_KERNEL);
3229 if (!sor)
3230 return -ENOMEM;
3231
Thierry Reding5faea3d2017-08-21 17:33:14 +02003232 sor->soc = of_device_get_match_data(&pdev->dev);
Thierry Reding6b6b6042013-11-15 16:06:05 +01003233 sor->output.dev = sor->dev = &pdev->dev;
Thierry Reding459cc2c2015-07-30 10:34:24 +02003234
3235 sor->settings = devm_kmemdup(&pdev->dev, sor->soc->settings,
3236 sor->soc->num_settings *
3237 sizeof(*sor->settings),
3238 GFP_KERNEL);
3239 if (!sor->settings)
3240 return -ENOMEM;
3241
3242 sor->num_settings = sor->soc->num_settings;
Thierry Reding6b6b6042013-11-15 16:06:05 +01003243
3244 np = of_parse_phandle(pdev->dev.of_node, "nvidia,dpaux", 0);
3245 if (np) {
Thierry Reding9542c232015-07-08 13:39:09 +02003246 sor->aux = drm_dp_aux_find_by_of_node(np);
Thierry Reding6b6b6042013-11-15 16:06:05 +01003247 of_node_put(np);
3248
Thierry Reding9542c232015-07-08 13:39:09 +02003249 if (!sor->aux)
Thierry Reding6b6b6042013-11-15 16:06:05 +01003250 return -EPROBE_DEFER;
3251 }
3252
Thierry Reding9542c232015-07-08 13:39:09 +02003253 if (!sor->aux) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02003254 if (sor->soc->supports_hdmi) {
3255 sor->ops = &tegra_sor_hdmi_ops;
Thierry Redingc57997b2017-10-12 19:12:57 +02003256 sor->pad = TEGRA_IO_PAD_HDMI;
Thierry Reding459cc2c2015-07-30 10:34:24 +02003257 } else if (sor->soc->supports_lvds) {
3258 dev_err(&pdev->dev, "LVDS not supported yet\n");
3259 return -ENODEV;
3260 } else {
3261 dev_err(&pdev->dev, "unknown (non-DP) support\n");
3262 return -ENODEV;
3263 }
3264 } else {
3265 if (sor->soc->supports_edp) {
3266 sor->ops = &tegra_sor_edp_ops;
Thierry Redingc57997b2017-10-12 19:12:57 +02003267 sor->pad = TEGRA_IO_PAD_LVDS;
Thierry Reding459cc2c2015-07-30 10:34:24 +02003268 } else if (sor->soc->supports_dp) {
3269 dev_err(&pdev->dev, "DisplayPort not supported yet\n");
3270 return -ENODEV;
3271 } else {
3272 dev_err(&pdev->dev, "unknown (DP) support\n");
3273 return -ENODEV;
3274 }
3275 }
3276
Thierry Redingc57997b2017-10-12 19:12:57 +02003277 err = tegra_sor_parse_dt(sor);
3278 if (err < 0)
3279 return err;
3280
Thierry Reding6b6b6042013-11-15 16:06:05 +01003281 err = tegra_output_probe(&sor->output);
Thierry Reding4dbdc742015-04-27 15:04:26 +02003282 if (err < 0) {
3283 dev_err(&pdev->dev, "failed to probe output: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01003284 return err;
Thierry Reding4dbdc742015-04-27 15:04:26 +02003285 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01003286
Thierry Reding459cc2c2015-07-30 10:34:24 +02003287 if (sor->ops && sor->ops->probe) {
3288 err = sor->ops->probe(sor);
3289 if (err < 0) {
3290 dev_err(&pdev->dev, "failed to probe %s: %d\n",
3291 sor->ops->name, err);
3292 goto output;
3293 }
3294 }
3295
Thierry Reding6b6b6042013-11-15 16:06:05 +01003296 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3297 sor->regs = devm_ioremap_resource(&pdev->dev, regs);
Thierry Reding459cc2c2015-07-30 10:34:24 +02003298 if (IS_ERR(sor->regs)) {
3299 err = PTR_ERR(sor->regs);
3300 goto remove;
3301 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01003302
Thierry Reding8e2988a72018-12-03 15:46:03 +01003303 err = platform_get_irq(pdev, 0);
3304 if (err < 0) {
3305 dev_err(&pdev->dev, "failed to get IRQ: %d\n", err);
3306 goto remove;
3307 }
3308
3309 sor->irq = err;
3310
3311 err = devm_request_irq(sor->dev, sor->irq, tegra_sor_irq, 0,
3312 dev_name(sor->dev), sor);
3313 if (err < 0) {
3314 dev_err(&pdev->dev, "failed to request IRQ: %d\n", err);
3315 goto remove;
3316 }
3317
Thierry Reding180b46e2018-12-06 18:56:47 +01003318 sor->rst = devm_reset_control_get(&pdev->dev, "sor");
3319 if (IS_ERR(sor->rst)) {
3320 err = PTR_ERR(sor->rst);
3321
3322 if (err != -EBUSY || WARN_ON(!pdev->dev.pm_domain)) {
Jon Hunterf8c79122016-07-01 14:21:38 +01003323 dev_err(&pdev->dev, "failed to get reset control: %d\n",
3324 err);
3325 goto remove;
3326 }
Thierry Reding180b46e2018-12-06 18:56:47 +01003327
3328 /*
3329 * At this point, the reset control is most likely being used
3330 * by the generic power domain implementation. With any luck
3331 * the power domain will have taken care of resetting the SOR
3332 * and we don't have to do anything.
3333 */
3334 sor->rst = NULL;
Thierry Reding4dbdc742015-04-27 15:04:26 +02003335 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01003336
3337 sor->clk = devm_clk_get(&pdev->dev, NULL);
Thierry Reding4dbdc742015-04-27 15:04:26 +02003338 if (IS_ERR(sor->clk)) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02003339 err = PTR_ERR(sor->clk);
3340 dev_err(&pdev->dev, "failed to get module clock: %d\n", err);
3341 goto remove;
Thierry Reding4dbdc742015-04-27 15:04:26 +02003342 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01003343
Thierry Reding618dee32016-06-09 17:53:57 +02003344 if (sor->soc->supports_hdmi || sor->soc->supports_dp) {
Thierry Redinge1335e22017-10-12 17:53:11 +02003345 struct device_node *np = pdev->dev.of_node;
3346 const char *name;
3347
3348 /*
3349 * For backwards compatibility with Tegra210 device trees,
3350 * fall back to the old clock name "source" if the new "out"
3351 * clock is not available.
3352 */
3353 if (of_property_match_string(np, "clock-names", "out") < 0)
3354 name = "source";
3355 else
3356 name = "out";
3357
3358 sor->clk_out = devm_clk_get(&pdev->dev, name);
3359 if (IS_ERR(sor->clk_out)) {
3360 err = PTR_ERR(sor->clk_out);
3361 dev_err(sor->dev, "failed to get %s clock: %d\n",
3362 name, err);
Thierry Reding618dee32016-06-09 17:53:57 +02003363 goto remove;
3364 }
Thierry Reding1087fac2017-12-14 13:37:53 +01003365 } else {
Thierry Redingd7805372018-01-10 13:04:58 +01003366 /* fall back to the module clock on SOR0 (eDP/LVDS only) */
Thierry Reding1087fac2017-12-14 13:37:53 +01003367 sor->clk_out = sor->clk;
Thierry Reding618dee32016-06-09 17:53:57 +02003368 }
3369
Thierry Reding6b6b6042013-11-15 16:06:05 +01003370 sor->clk_parent = devm_clk_get(&pdev->dev, "parent");
Thierry Reding4dbdc742015-04-27 15:04:26 +02003371 if (IS_ERR(sor->clk_parent)) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02003372 err = PTR_ERR(sor->clk_parent);
3373 dev_err(&pdev->dev, "failed to get parent clock: %d\n", err);
3374 goto remove;
Thierry Reding4dbdc742015-04-27 15:04:26 +02003375 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01003376
Thierry Reding6b6b6042013-11-15 16:06:05 +01003377 sor->clk_safe = devm_clk_get(&pdev->dev, "safe");
Thierry Reding4dbdc742015-04-27 15:04:26 +02003378 if (IS_ERR(sor->clk_safe)) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02003379 err = PTR_ERR(sor->clk_safe);
3380 dev_err(&pdev->dev, "failed to get safe clock: %d\n", err);
3381 goto remove;
Thierry Reding4dbdc742015-04-27 15:04:26 +02003382 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01003383
Thierry Reding6b6b6042013-11-15 16:06:05 +01003384 sor->clk_dp = devm_clk_get(&pdev->dev, "dp");
Thierry Reding4dbdc742015-04-27 15:04:26 +02003385 if (IS_ERR(sor->clk_dp)) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02003386 err = PTR_ERR(sor->clk_dp);
3387 dev_err(&pdev->dev, "failed to get DP clock: %d\n", err);
3388 goto remove;
Thierry Reding4dbdc742015-04-27 15:04:26 +02003389 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01003390
Thierry Redinge1335e22017-10-12 17:53:11 +02003391 /*
3392 * Starting with Tegra186, the BPMP provides an implementation for
3393 * the pad output clock, so we have to look it up from device tree.
3394 */
3395 sor->clk_pad = devm_clk_get(&pdev->dev, "pad");
3396 if (IS_ERR(sor->clk_pad)) {
3397 if (sor->clk_pad != ERR_PTR(-ENOENT)) {
3398 err = PTR_ERR(sor->clk_pad);
3399 goto remove;
3400 }
3401
3402 /*
3403 * If the pad output clock is not available, then we assume
3404 * we're on Tegra210 or earlier and have to provide our own
3405 * implementation.
3406 */
3407 sor->clk_pad = NULL;
3408 }
3409
3410 /*
3411 * The bootloader may have set up the SOR such that it's module clock
3412 * is sourced by one of the display PLLs. However, that doesn't work
3413 * without properly having set up other bits of the SOR.
3414 */
3415 err = clk_set_parent(sor->clk_out, sor->clk_safe);
3416 if (err < 0) {
3417 dev_err(&pdev->dev, "failed to use safe clock: %d\n", err);
3418 goto remove;
3419 }
3420
Thierry Redingaaff8bd2015-08-07 16:04:54 +02003421 platform_set_drvdata(pdev, sor);
3422 pm_runtime_enable(&pdev->dev);
3423
Thierry Redinge1335e22017-10-12 17:53:11 +02003424 /*
3425 * On Tegra210 and earlier, provide our own implementation for the
3426 * pad output clock.
3427 */
3428 if (!sor->clk_pad) {
3429 err = pm_runtime_get_sync(&pdev->dev);
3430 if (err < 0) {
3431 dev_err(&pdev->dev, "failed to get runtime PM: %d\n",
3432 err);
3433 goto remove;
3434 }
Thierry Redingb2992212015-10-01 14:25:03 +02003435
Thierry Redinge1335e22017-10-12 17:53:11 +02003436 sor->clk_pad = tegra_clk_sor_pad_register(sor,
3437 "sor1_pad_clkout");
3438 pm_runtime_put(&pdev->dev);
3439 }
3440
3441 if (IS_ERR(sor->clk_pad)) {
3442 err = PTR_ERR(sor->clk_pad);
3443 dev_err(&pdev->dev, "failed to register SOR pad clock: %d\n",
3444 err);
Thierry Redingb2992212015-10-01 14:25:03 +02003445 goto remove;
3446 }
3447
Thierry Reding6b6b6042013-11-15 16:06:05 +01003448 INIT_LIST_HEAD(&sor->client.list);
3449 sor->client.ops = &sor_client_ops;
3450 sor->client.dev = &pdev->dev;
3451
Thierry Reding6b6b6042013-11-15 16:06:05 +01003452 err = host1x_client_register(&sor->client);
3453 if (err < 0) {
3454 dev_err(&pdev->dev, "failed to register host1x client: %d\n",
3455 err);
Thierry Reding459cc2c2015-07-30 10:34:24 +02003456 goto remove;
Thierry Reding6b6b6042013-11-15 16:06:05 +01003457 }
3458
Thierry Reding6b6b6042013-11-15 16:06:05 +01003459 return 0;
Thierry Reding459cc2c2015-07-30 10:34:24 +02003460
3461remove:
3462 if (sor->ops && sor->ops->remove)
3463 sor->ops->remove(sor);
3464output:
3465 tegra_output_remove(&sor->output);
3466 return err;
Thierry Reding6b6b6042013-11-15 16:06:05 +01003467}
3468
3469static int tegra_sor_remove(struct platform_device *pdev)
3470{
3471 struct tegra_sor *sor = platform_get_drvdata(pdev);
3472 int err;
3473
Thierry Redingaaff8bd2015-08-07 16:04:54 +02003474 pm_runtime_disable(&pdev->dev);
3475
Thierry Reding6b6b6042013-11-15 16:06:05 +01003476 err = host1x_client_unregister(&sor->client);
3477 if (err < 0) {
3478 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
3479 err);
3480 return err;
3481 }
3482
Thierry Reding459cc2c2015-07-30 10:34:24 +02003483 if (sor->ops && sor->ops->remove) {
3484 err = sor->ops->remove(sor);
3485 if (err < 0)
3486 dev_err(&pdev->dev, "failed to remove SOR: %d\n", err);
3487 }
3488
Thierry Reding328ec692014-12-19 15:55:08 +01003489 tegra_output_remove(&sor->output);
Thierry Reding6b6b6042013-11-15 16:06:05 +01003490
3491 return 0;
3492}
3493
Thierry Redingaaff8bd2015-08-07 16:04:54 +02003494#ifdef CONFIG_PM
3495static int tegra_sor_suspend(struct device *dev)
3496{
3497 struct tegra_sor *sor = dev_get_drvdata(dev);
3498 int err;
3499
Jon Hunterf8c79122016-07-01 14:21:38 +01003500 if (sor->rst) {
3501 err = reset_control_assert(sor->rst);
3502 if (err < 0) {
3503 dev_err(dev, "failed to assert reset: %d\n", err);
3504 return err;
3505 }
Thierry Redingaaff8bd2015-08-07 16:04:54 +02003506 }
3507
3508 usleep_range(1000, 2000);
3509
3510 clk_disable_unprepare(sor->clk);
3511
3512 return 0;
3513}
3514
3515static int tegra_sor_resume(struct device *dev)
3516{
3517 struct tegra_sor *sor = dev_get_drvdata(dev);
3518 int err;
3519
3520 err = clk_prepare_enable(sor->clk);
3521 if (err < 0) {
3522 dev_err(dev, "failed to enable clock: %d\n", err);
3523 return err;
3524 }
3525
3526 usleep_range(1000, 2000);
3527
Jon Hunterf8c79122016-07-01 14:21:38 +01003528 if (sor->rst) {
3529 err = reset_control_deassert(sor->rst);
3530 if (err < 0) {
3531 dev_err(dev, "failed to deassert reset: %d\n", err);
3532 clk_disable_unprepare(sor->clk);
3533 return err;
3534 }
Thierry Redingaaff8bd2015-08-07 16:04:54 +02003535 }
3536
3537 return 0;
3538}
3539#endif
3540
3541static const struct dev_pm_ops tegra_sor_pm_ops = {
3542 SET_RUNTIME_PM_OPS(tegra_sor_suspend, tegra_sor_resume, NULL)
3543};
3544
Thierry Reding6b6b6042013-11-15 16:06:05 +01003545struct platform_driver tegra_sor_driver = {
3546 .driver = {
3547 .name = "tegra-sor",
3548 .of_match_table = tegra_sor_of_match,
Thierry Redingaaff8bd2015-08-07 16:04:54 +02003549 .pm = &tegra_sor_pm_ops,
Thierry Reding6b6b6042013-11-15 16:06:05 +01003550 },
3551 .probe = tegra_sor_probe,
3552 .remove = tegra_sor_remove,
3553};