blob: 7463ea02a0831fdd35dc16620ba46d5d58c67a4c [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 Redinga82752e2014-01-31 10:02:15 +010010#include <linux/debugfs.h>
Thierry Reding6fad8f62014-11-28 15:41:34 +010011#include <linux/gpio.h>
Thierry Reding6b6b6042013-11-15 16:06:05 +010012#include <linux/io.h>
13#include <linux/platform_device.h>
14#include <linux/reset.h>
Thierry Reding306a7f92014-07-17 13:17:24 +020015
Thierry Reding72323982014-07-11 13:19:06 +020016#include <soc/tegra/pmc.h>
Thierry Reding6b6b6042013-11-15 16:06:05 +010017
Thierry Reding4aa3df72014-11-24 16:27:13 +010018#include <drm/drm_atomic_helper.h>
Thierry Reding6b6b6042013-11-15 16:06:05 +010019#include <drm/drm_dp_helper.h>
Thierry Reding6fad8f62014-11-28 15:41:34 +010020#include <drm/drm_panel.h>
Thierry Reding6b6b6042013-11-15 16:06:05 +010021
22#include "dc.h"
23#include "drm.h"
24#include "sor.h"
25
26struct tegra_sor {
27 struct host1x_client client;
28 struct tegra_output output;
29 struct device *dev;
30
31 void __iomem *regs;
32
33 struct reset_control *rst;
34 struct clk *clk_parent;
35 struct clk *clk_safe;
36 struct clk *clk_dp;
37 struct clk *clk;
38
39 struct tegra_dpaux *dpaux;
40
Thierry Reding86f5c522014-03-26 11:13:16 +010041 struct mutex lock;
Thierry Reding6b6b6042013-11-15 16:06:05 +010042 bool enabled;
Thierry Redinga82752e2014-01-31 10:02:15 +010043
44 struct dentry *debugfs;
Thierry Reding6b6b6042013-11-15 16:06:05 +010045};
46
Thierry Reding34fa1832014-06-05 16:31:10 +020047struct tegra_sor_config {
48 u32 bits_per_pixel;
49
50 u32 active_polarity;
51 u32 active_count;
52 u32 tu_size;
53 u32 active_frac;
54 u32 watermark;
Thierry Reding7890b572014-06-05 16:12:46 +020055
56 u32 hblank_symbols;
57 u32 vblank_symbols;
Thierry Reding34fa1832014-06-05 16:31:10 +020058};
59
Thierry Reding6b6b6042013-11-15 16:06:05 +010060static inline struct tegra_sor *
61host1x_client_to_sor(struct host1x_client *client)
62{
63 return container_of(client, struct tegra_sor, client);
64}
65
66static inline struct tegra_sor *to_sor(struct tegra_output *output)
67{
68 return container_of(output, struct tegra_sor, output);
69}
70
71static inline unsigned long tegra_sor_readl(struct tegra_sor *sor,
72 unsigned long offset)
73{
74 return readl(sor->regs + (offset << 2));
75}
76
77static inline void tegra_sor_writel(struct tegra_sor *sor, unsigned long value,
78 unsigned long offset)
79{
80 writel(value, sor->regs + (offset << 2));
81}
82
83static int tegra_sor_dp_train_fast(struct tegra_sor *sor,
84 struct drm_dp_link *link)
85{
86 unsigned long value;
87 unsigned int i;
88 u8 pattern;
89 int err;
90
91 /* setup lane parameters */
92 value = SOR_LANE_DRIVE_CURRENT_LANE3(0x40) |
93 SOR_LANE_DRIVE_CURRENT_LANE2(0x40) |
94 SOR_LANE_DRIVE_CURRENT_LANE1(0x40) |
95 SOR_LANE_DRIVE_CURRENT_LANE0(0x40);
96 tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT_0);
97
98 value = SOR_LANE_PREEMPHASIS_LANE3(0x0f) |
99 SOR_LANE_PREEMPHASIS_LANE2(0x0f) |
100 SOR_LANE_PREEMPHASIS_LANE1(0x0f) |
101 SOR_LANE_PREEMPHASIS_LANE0(0x0f);
102 tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS_0);
103
104 value = SOR_LANE_POST_CURSOR_LANE3(0x00) |
105 SOR_LANE_POST_CURSOR_LANE2(0x00) |
106 SOR_LANE_POST_CURSOR_LANE1(0x00) |
107 SOR_LANE_POST_CURSOR_LANE0(0x00);
108 tegra_sor_writel(sor, value, SOR_LANE_POST_CURSOR_0);
109
110 /* disable LVDS mode */
111 tegra_sor_writel(sor, 0, SOR_LVDS);
112
113 value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
114 value |= SOR_DP_PADCTL_TX_PU_ENABLE;
115 value &= ~SOR_DP_PADCTL_TX_PU_MASK;
116 value |= SOR_DP_PADCTL_TX_PU(2); /* XXX: don't hardcode? */
117 tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
118
119 value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
120 value |= SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
121 SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0;
122 tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
123
124 usleep_range(10, 100);
125
126 value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
127 value &= ~(SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
128 SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0);
129 tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
130
131 err = tegra_dpaux_prepare(sor->dpaux, DP_SET_ANSI_8B10B);
132 if (err < 0)
133 return err;
134
135 for (i = 0, value = 0; i < link->num_lanes; i++) {
136 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
137 SOR_DP_TPG_SCRAMBLER_NONE |
138 SOR_DP_TPG_PATTERN_TRAIN1;
139 value = (value << 8) | lane;
140 }
141
142 tegra_sor_writel(sor, value, SOR_DP_TPG);
143
144 pattern = DP_TRAINING_PATTERN_1;
145
146 err = tegra_dpaux_train(sor->dpaux, link, pattern);
147 if (err < 0)
148 return err;
149
150 value = tegra_sor_readl(sor, SOR_DP_SPARE_0);
151 value |= SOR_DP_SPARE_SEQ_ENABLE;
152 value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
153 value |= SOR_DP_SPARE_MACRO_SOR_CLK;
154 tegra_sor_writel(sor, value, SOR_DP_SPARE_0);
155
156 for (i = 0, value = 0; i < link->num_lanes; i++) {
157 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
158 SOR_DP_TPG_SCRAMBLER_NONE |
159 SOR_DP_TPG_PATTERN_TRAIN2;
160 value = (value << 8) | lane;
161 }
162
163 tegra_sor_writel(sor, value, SOR_DP_TPG);
164
165 pattern = DP_LINK_SCRAMBLING_DISABLE | DP_TRAINING_PATTERN_2;
166
167 err = tegra_dpaux_train(sor->dpaux, link, pattern);
168 if (err < 0)
169 return err;
170
171 for (i = 0, value = 0; i < link->num_lanes; i++) {
172 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
173 SOR_DP_TPG_SCRAMBLER_GALIOS |
174 SOR_DP_TPG_PATTERN_NONE;
175 value = (value << 8) | lane;
176 }
177
178 tegra_sor_writel(sor, value, SOR_DP_TPG);
179
180 pattern = DP_TRAINING_PATTERN_DISABLE;
181
182 err = tegra_dpaux_train(sor->dpaux, link, pattern);
183 if (err < 0)
184 return err;
185
186 return 0;
187}
188
189static void tegra_sor_super_update(struct tegra_sor *sor)
190{
191 tegra_sor_writel(sor, 0, SOR_SUPER_STATE_0);
192 tegra_sor_writel(sor, 1, SOR_SUPER_STATE_0);
193 tegra_sor_writel(sor, 0, SOR_SUPER_STATE_0);
194}
195
196static void tegra_sor_update(struct tegra_sor *sor)
197{
198 tegra_sor_writel(sor, 0, SOR_STATE_0);
199 tegra_sor_writel(sor, 1, SOR_STATE_0);
200 tegra_sor_writel(sor, 0, SOR_STATE_0);
201}
202
203static int tegra_sor_setup_pwm(struct tegra_sor *sor, unsigned long timeout)
204{
205 unsigned long value;
206
207 value = tegra_sor_readl(sor, SOR_PWM_DIV);
208 value &= ~SOR_PWM_DIV_MASK;
209 value |= 0x400; /* period */
210 tegra_sor_writel(sor, value, SOR_PWM_DIV);
211
212 value = tegra_sor_readl(sor, SOR_PWM_CTL);
213 value &= ~SOR_PWM_CTL_DUTY_CYCLE_MASK;
214 value |= 0x400; /* duty cycle */
215 value &= ~SOR_PWM_CTL_CLK_SEL; /* clock source: PCLK */
216 value |= SOR_PWM_CTL_TRIGGER;
217 tegra_sor_writel(sor, value, SOR_PWM_CTL);
218
219 timeout = jiffies + msecs_to_jiffies(timeout);
220
221 while (time_before(jiffies, timeout)) {
222 value = tegra_sor_readl(sor, SOR_PWM_CTL);
223 if ((value & SOR_PWM_CTL_TRIGGER) == 0)
224 return 0;
225
226 usleep_range(25, 100);
227 }
228
229 return -ETIMEDOUT;
230}
231
232static int tegra_sor_attach(struct tegra_sor *sor)
233{
234 unsigned long value, timeout;
235
236 /* wake up in normal mode */
237 value = tegra_sor_readl(sor, SOR_SUPER_STATE_1);
238 value |= SOR_SUPER_STATE_HEAD_MODE_AWAKE;
239 value |= SOR_SUPER_STATE_MODE_NORMAL;
240 tegra_sor_writel(sor, value, SOR_SUPER_STATE_1);
241 tegra_sor_super_update(sor);
242
243 /* attach */
244 value = tegra_sor_readl(sor, SOR_SUPER_STATE_1);
245 value |= SOR_SUPER_STATE_ATTACHED;
246 tegra_sor_writel(sor, value, SOR_SUPER_STATE_1);
247 tegra_sor_super_update(sor);
248
249 timeout = jiffies + msecs_to_jiffies(250);
250
251 while (time_before(jiffies, timeout)) {
252 value = tegra_sor_readl(sor, SOR_TEST);
253 if ((value & SOR_TEST_ATTACHED) != 0)
254 return 0;
255
256 usleep_range(25, 100);
257 }
258
259 return -ETIMEDOUT;
260}
261
262static int tegra_sor_wakeup(struct tegra_sor *sor)
263{
264 struct tegra_dc *dc = to_tegra_dc(sor->output.encoder.crtc);
265 unsigned long value, timeout;
266
267 /* enable display controller outputs */
268 value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
269 value |= PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
270 PW4_ENABLE | PM0_ENABLE | PM1_ENABLE;
271 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
272
Thierry Reding62b9e062014-11-21 17:33:33 +0100273 tegra_dc_commit(dc);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100274
275 timeout = jiffies + msecs_to_jiffies(250);
276
277 /* wait for head to wake up */
278 while (time_before(jiffies, timeout)) {
279 value = tegra_sor_readl(sor, SOR_TEST);
280 value &= SOR_TEST_HEAD_MODE_MASK;
281
282 if (value == SOR_TEST_HEAD_MODE_AWAKE)
283 return 0;
284
285 usleep_range(25, 100);
286 }
287
288 return -ETIMEDOUT;
289}
290
291static int tegra_sor_power_up(struct tegra_sor *sor, unsigned long timeout)
292{
293 unsigned long value;
294
295 value = tegra_sor_readl(sor, SOR_PWR);
296 value |= SOR_PWR_TRIGGER | SOR_PWR_NORMAL_STATE_PU;
297 tegra_sor_writel(sor, value, SOR_PWR);
298
299 timeout = jiffies + msecs_to_jiffies(timeout);
300
301 while (time_before(jiffies, timeout)) {
302 value = tegra_sor_readl(sor, SOR_PWR);
303 if ((value & SOR_PWR_TRIGGER) == 0)
304 return 0;
305
306 usleep_range(25, 100);
307 }
308
309 return -ETIMEDOUT;
310}
311
Thierry Reding34fa1832014-06-05 16:31:10 +0200312struct tegra_sor_params {
313 /* number of link clocks per line */
314 unsigned int num_clocks;
315 /* ratio between input and output */
316 u64 ratio;
317 /* precision factor */
318 u64 precision;
319
320 unsigned int active_polarity;
321 unsigned int active_count;
322 unsigned int active_frac;
323 unsigned int tu_size;
324 unsigned int error;
325};
326
327static int tegra_sor_compute_params(struct tegra_sor *sor,
328 struct tegra_sor_params *params,
329 unsigned int tu_size)
330{
331 u64 active_sym, active_count, frac, approx;
332 u32 active_polarity, active_frac = 0;
333 const u64 f = params->precision;
334 s64 error;
335
336 active_sym = params->ratio * tu_size;
337 active_count = div_u64(active_sym, f) * f;
338 frac = active_sym - active_count;
339
340 /* fraction < 0.5 */
341 if (frac >= (f / 2)) {
342 active_polarity = 1;
343 frac = f - frac;
344 } else {
345 active_polarity = 0;
346 }
347
348 if (frac != 0) {
349 frac = div_u64(f * f, frac); /* 1/fraction */
350 if (frac <= (15 * f)) {
351 active_frac = div_u64(frac, f);
352
353 /* round up */
354 if (active_polarity)
355 active_frac++;
356 } else {
357 active_frac = active_polarity ? 1 : 15;
358 }
359 }
360
361 if (active_frac == 1)
362 active_polarity = 0;
363
364 if (active_polarity == 1) {
365 if (active_frac) {
366 approx = active_count + (active_frac * (f - 1)) * f;
367 approx = div_u64(approx, active_frac * f);
368 } else {
369 approx = active_count + f;
370 }
371 } else {
372 if (active_frac)
373 approx = active_count + div_u64(f, active_frac);
374 else
375 approx = active_count;
376 }
377
378 error = div_s64(active_sym - approx, tu_size);
379 error *= params->num_clocks;
380
381 if (error <= 0 && abs64(error) < params->error) {
382 params->active_count = div_u64(active_count, f);
383 params->active_polarity = active_polarity;
384 params->active_frac = active_frac;
385 params->error = abs64(error);
386 params->tu_size = tu_size;
387
388 if (error == 0)
389 return true;
390 }
391
392 return false;
393}
394
395static int tegra_sor_calc_config(struct tegra_sor *sor,
396 struct drm_display_mode *mode,
397 struct tegra_sor_config *config,
398 struct drm_dp_link *link)
399{
400 const u64 f = 100000, link_rate = link->rate * 1000;
401 const u64 pclk = mode->clock * 1000;
Thierry Reding7890b572014-06-05 16:12:46 +0200402 u64 input, output, watermark, num;
Thierry Reding34fa1832014-06-05 16:31:10 +0200403 struct tegra_sor_params params;
Thierry Reding34fa1832014-06-05 16:31:10 +0200404 u32 num_syms_per_line;
405 unsigned int i;
406
407 if (!link_rate || !link->num_lanes || !pclk || !config->bits_per_pixel)
408 return -EINVAL;
409
410 output = link_rate * 8 * link->num_lanes;
411 input = pclk * config->bits_per_pixel;
412
413 if (input >= output)
414 return -ERANGE;
415
416 memset(&params, 0, sizeof(params));
417 params.ratio = div64_u64(input * f, output);
418 params.num_clocks = div_u64(link_rate * mode->hdisplay, pclk);
419 params.precision = f;
420 params.error = 64 * f;
421 params.tu_size = 64;
422
423 for (i = params.tu_size; i >= 32; i--)
424 if (tegra_sor_compute_params(sor, &params, i))
425 break;
426
427 if (params.active_frac == 0) {
428 config->active_polarity = 0;
429 config->active_count = params.active_count;
430
431 if (!params.active_polarity)
432 config->active_count--;
433
434 config->tu_size = params.tu_size;
435 config->active_frac = 1;
436 } else {
437 config->active_polarity = params.active_polarity;
438 config->active_count = params.active_count;
439 config->active_frac = params.active_frac;
440 config->tu_size = params.tu_size;
441 }
442
443 dev_dbg(sor->dev,
444 "polarity: %d active count: %d tu size: %d active frac: %d\n",
445 config->active_polarity, config->active_count,
446 config->tu_size, config->active_frac);
447
448 watermark = params.ratio * config->tu_size * (f - params.ratio);
449 watermark = div_u64(watermark, f);
450
451 watermark = div_u64(watermark + params.error, f);
452 config->watermark = watermark + (config->bits_per_pixel / 8) + 2;
453 num_syms_per_line = (mode->hdisplay * config->bits_per_pixel) *
454 (link->num_lanes * 8);
455
456 if (config->watermark > 30) {
457 config->watermark = 30;
458 dev_err(sor->dev,
459 "unable to compute TU size, forcing watermark to %u\n",
460 config->watermark);
461 } else if (config->watermark > num_syms_per_line) {
462 config->watermark = num_syms_per_line;
463 dev_err(sor->dev, "watermark too high, forcing to %u\n",
464 config->watermark);
465 }
466
Thierry Reding7890b572014-06-05 16:12:46 +0200467 /* compute the number of symbols per horizontal blanking interval */
468 num = ((mode->htotal - mode->hdisplay) - 7) * link_rate;
469 config->hblank_symbols = div_u64(num, pclk);
470
471 if (link->capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
472 config->hblank_symbols -= 3;
473
474 config->hblank_symbols -= 12 / link->num_lanes;
475
476 /* compute the number of symbols per vertical blanking interval */
477 num = (mode->hdisplay - 25) * link_rate;
478 config->vblank_symbols = div_u64(num, pclk);
479 config->vblank_symbols -= 36 / link->num_lanes + 4;
480
481 dev_dbg(sor->dev, "blank symbols: H:%u V:%u\n", config->hblank_symbols,
482 config->vblank_symbols);
483
Thierry Reding34fa1832014-06-05 16:31:10 +0200484 return 0;
485}
486
Thierry Reding6fad8f62014-11-28 15:41:34 +0100487static int tegra_sor_detach(struct tegra_sor *sor)
Thierry Reding6b6b6042013-11-15 16:06:05 +0100488{
Thierry Reding6fad8f62014-11-28 15:41:34 +0100489 unsigned long value, timeout;
490
491 /* switch to safe mode */
492 value = tegra_sor_readl(sor, SOR_SUPER_STATE_1);
493 value &= ~SOR_SUPER_STATE_MODE_NORMAL;
494 tegra_sor_writel(sor, value, SOR_SUPER_STATE_1);
495 tegra_sor_super_update(sor);
496
497 timeout = jiffies + msecs_to_jiffies(250);
498
499 while (time_before(jiffies, timeout)) {
500 value = tegra_sor_readl(sor, SOR_PWR);
501 if (value & SOR_PWR_MODE_SAFE)
502 break;
503 }
504
505 if ((value & SOR_PWR_MODE_SAFE) == 0)
506 return -ETIMEDOUT;
507
508 /* go to sleep */
509 value = tegra_sor_readl(sor, SOR_SUPER_STATE_1);
510 value &= ~SOR_SUPER_STATE_HEAD_MODE_MASK;
511 tegra_sor_writel(sor, value, SOR_SUPER_STATE_1);
512 tegra_sor_super_update(sor);
513
514 /* detach */
515 value = tegra_sor_readl(sor, SOR_SUPER_STATE_1);
516 value &= ~SOR_SUPER_STATE_ATTACHED;
517 tegra_sor_writel(sor, value, SOR_SUPER_STATE_1);
518 tegra_sor_super_update(sor);
519
520 timeout = jiffies + msecs_to_jiffies(250);
521
522 while (time_before(jiffies, timeout)) {
523 value = tegra_sor_readl(sor, SOR_TEST);
524 if ((value & SOR_TEST_ATTACHED) == 0)
525 break;
526
527 usleep_range(25, 100);
528 }
529
530 if ((value & SOR_TEST_ATTACHED) != 0)
531 return -ETIMEDOUT;
532
533 return 0;
534}
535
536static int tegra_sor_power_down(struct tegra_sor *sor)
537{
538 unsigned long value, timeout;
539 int err;
540
541 value = tegra_sor_readl(sor, SOR_PWR);
542 value &= ~SOR_PWR_NORMAL_STATE_PU;
543 value |= SOR_PWR_TRIGGER;
544 tegra_sor_writel(sor, value, SOR_PWR);
545
546 timeout = jiffies + msecs_to_jiffies(250);
547
548 while (time_before(jiffies, timeout)) {
549 value = tegra_sor_readl(sor, SOR_PWR);
550 if ((value & SOR_PWR_TRIGGER) == 0)
551 return 0;
552
553 usleep_range(25, 100);
554 }
555
556 if ((value & SOR_PWR_TRIGGER) != 0)
557 return -ETIMEDOUT;
558
559 err = clk_set_parent(sor->clk, sor->clk_safe);
560 if (err < 0)
561 dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
562
563 value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
564 value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
565 SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2);
566 tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
567
568 /* stop lane sequencer */
569 value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_UP |
570 SOR_LANE_SEQ_CTL_POWER_STATE_DOWN;
571 tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
572
573 timeout = jiffies + msecs_to_jiffies(250);
574
575 while (time_before(jiffies, timeout)) {
576 value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
577 if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
578 break;
579
580 usleep_range(25, 100);
581 }
582
583 if ((value & SOR_LANE_SEQ_CTL_TRIGGER) != 0)
584 return -ETIMEDOUT;
585
586 value = tegra_sor_readl(sor, SOR_PLL_2);
587 value |= SOR_PLL_2_PORT_POWERDOWN;
588 tegra_sor_writel(sor, value, SOR_PLL_2);
589
590 usleep_range(20, 100);
591
592 value = tegra_sor_readl(sor, SOR_PLL_0);
593 value |= SOR_PLL_0_POWER_OFF;
594 value |= SOR_PLL_0_VCOPD;
595 tegra_sor_writel(sor, value, SOR_PLL_0);
596
597 value = tegra_sor_readl(sor, SOR_PLL_2);
598 value |= SOR_PLL_2_SEQ_PLLCAPPD;
599 value |= SOR_PLL_2_SEQ_PLLCAPPD_ENFORCE;
600 tegra_sor_writel(sor, value, SOR_PLL_2);
601
602 usleep_range(20, 100);
603
604 return 0;
605}
606
607static int tegra_sor_crc_open(struct inode *inode, struct file *file)
608{
609 file->private_data = inode->i_private;
610
611 return 0;
612}
613
614static int tegra_sor_crc_release(struct inode *inode, struct file *file)
615{
616 return 0;
617}
618
619static int tegra_sor_crc_wait(struct tegra_sor *sor, unsigned long timeout)
620{
621 u32 value;
622
623 timeout = jiffies + msecs_to_jiffies(timeout);
624
625 while (time_before(jiffies, timeout)) {
626 value = tegra_sor_readl(sor, SOR_CRC_A);
627 if (value & SOR_CRC_A_VALID)
628 return 0;
629
630 usleep_range(100, 200);
631 }
632
633 return -ETIMEDOUT;
634}
635
636static ssize_t tegra_sor_crc_read(struct file *file, char __user *buffer,
637 size_t size, loff_t *ppos)
638{
639 struct tegra_sor *sor = file->private_data;
640 ssize_t num, err;
641 char buf[10];
642 u32 value;
643
644 mutex_lock(&sor->lock);
645
646 if (!sor->enabled) {
647 err = -EAGAIN;
648 goto unlock;
649 }
650
651 value = tegra_sor_readl(sor, SOR_STATE_1);
652 value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
653 tegra_sor_writel(sor, value, SOR_STATE_1);
654
655 value = tegra_sor_readl(sor, SOR_CRC_CNTRL);
656 value |= SOR_CRC_CNTRL_ENABLE;
657 tegra_sor_writel(sor, value, SOR_CRC_CNTRL);
658
659 value = tegra_sor_readl(sor, SOR_TEST);
660 value &= ~SOR_TEST_CRC_POST_SERIALIZE;
661 tegra_sor_writel(sor, value, SOR_TEST);
662
663 err = tegra_sor_crc_wait(sor, 100);
664 if (err < 0)
665 goto unlock;
666
667 tegra_sor_writel(sor, SOR_CRC_A_RESET, SOR_CRC_A);
668 value = tegra_sor_readl(sor, SOR_CRC_B);
669
670 num = scnprintf(buf, sizeof(buf), "%08x\n", value);
671
672 err = simple_read_from_buffer(buffer, size, ppos, buf, num);
673
674unlock:
675 mutex_unlock(&sor->lock);
676 return err;
677}
678
679static const struct file_operations tegra_sor_crc_fops = {
680 .owner = THIS_MODULE,
681 .open = tegra_sor_crc_open,
682 .read = tegra_sor_crc_read,
683 .release = tegra_sor_crc_release,
684};
685
686static int tegra_sor_debugfs_init(struct tegra_sor *sor,
687 struct drm_minor *minor)
688{
689 struct dentry *entry;
690 int err = 0;
691
692 sor->debugfs = debugfs_create_dir("sor", minor->debugfs_root);
693 if (!sor->debugfs)
694 return -ENOMEM;
695
696 entry = debugfs_create_file("crc", 0644, sor->debugfs, sor,
697 &tegra_sor_crc_fops);
698 if (!entry) {
699 dev_err(sor->dev,
700 "cannot create /sys/kernel/debug/dri/%s/sor/crc\n",
701 minor->debugfs_root->d_name.name);
702 err = -ENOMEM;
703 goto remove;
704 }
705
706 return err;
707
708remove:
709 debugfs_remove(sor->debugfs);
710 sor->debugfs = NULL;
711 return err;
712}
713
Thierry Reding4009c222014-12-19 15:47:30 +0100714static void tegra_sor_debugfs_exit(struct tegra_sor *sor)
Thierry Reding6fad8f62014-11-28 15:41:34 +0100715{
716 debugfs_remove_recursive(sor->debugfs);
717 sor->debugfs = NULL;
Thierry Reding6fad8f62014-11-28 15:41:34 +0100718}
719
720static void tegra_sor_connector_dpms(struct drm_connector *connector, int mode)
721{
722}
723
724static enum drm_connector_status
725tegra_sor_connector_detect(struct drm_connector *connector, bool force)
726{
727 struct tegra_output *output = connector_to_output(connector);
728 struct tegra_sor *sor = to_sor(output);
729
730 if (sor->dpaux)
731 return tegra_dpaux_detect(sor->dpaux);
732
733 return connector_status_unknown;
734}
735
736static const struct drm_connector_funcs tegra_sor_connector_funcs = {
737 .dpms = tegra_sor_connector_dpms,
Thierry Reding9d441892014-11-24 17:02:53 +0100738 .reset = drm_atomic_helper_connector_reset,
Thierry Reding6fad8f62014-11-28 15:41:34 +0100739 .detect = tegra_sor_connector_detect,
740 .fill_modes = drm_helper_probe_single_connector_modes,
741 .destroy = tegra_output_connector_destroy,
Thierry Reding9d441892014-11-24 17:02:53 +0100742 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
Thierry Reding4aa3df72014-11-24 16:27:13 +0100743 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
Thierry Reding6fad8f62014-11-28 15:41:34 +0100744};
745
746static int tegra_sor_connector_get_modes(struct drm_connector *connector)
747{
748 struct tegra_output *output = connector_to_output(connector);
749 struct tegra_sor *sor = to_sor(output);
750 int err;
751
752 if (sor->dpaux)
753 tegra_dpaux_enable(sor->dpaux);
754
755 err = tegra_output_connector_get_modes(connector);
756
757 if (sor->dpaux)
758 tegra_dpaux_disable(sor->dpaux);
759
760 return err;
761}
762
763static enum drm_mode_status
764tegra_sor_connector_mode_valid(struct drm_connector *connector,
765 struct drm_display_mode *mode)
766{
767 return MODE_OK;
768}
769
770static const struct drm_connector_helper_funcs tegra_sor_connector_helper_funcs = {
771 .get_modes = tegra_sor_connector_get_modes,
772 .mode_valid = tegra_sor_connector_mode_valid,
773 .best_encoder = tegra_output_connector_best_encoder,
774};
775
776static const struct drm_encoder_funcs tegra_sor_encoder_funcs = {
777 .destroy = tegra_output_encoder_destroy,
778};
779
780static void tegra_sor_encoder_dpms(struct drm_encoder *encoder, int mode)
781{
782}
783
784static bool tegra_sor_encoder_mode_fixup(struct drm_encoder *encoder,
785 const struct drm_display_mode *mode,
786 struct drm_display_mode *adjusted)
787{
788 struct tegra_output *output = encoder_to_output(encoder);
789 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
790 unsigned long pclk = mode->clock * 1000;
791 struct tegra_sor *sor = to_sor(output);
792 int err;
793
794 err = tegra_dc_setup_clock(dc, sor->clk_parent, pclk, 0);
795 if (err < 0) {
796 dev_err(output->dev, "failed to setup DC clock: %d\n", err);
797 return false;
798 }
799
800 err = clk_set_rate(sor->clk_parent, pclk);
801 if (err < 0) {
802 dev_err(output->dev, "failed to set clock rate to %lu Hz\n",
803 pclk);
804 return false;
805 }
806
807 return true;
808}
809
810static void tegra_sor_encoder_prepare(struct drm_encoder *encoder)
811{
812}
813
814static void tegra_sor_encoder_commit(struct drm_encoder *encoder)
815{
816}
817
818static void tegra_sor_encoder_mode_set(struct drm_encoder *encoder,
819 struct drm_display_mode *mode,
820 struct drm_display_mode *adjusted)
821{
822 struct tegra_output *output = encoder_to_output(encoder);
823 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100824 unsigned int vbe, vse, hbe, hse, vbs, hbs, i;
825 struct tegra_sor *sor = to_sor(output);
Thierry Reding34fa1832014-06-05 16:31:10 +0200826 struct tegra_sor_config config;
827 struct drm_dp_link link;
828 struct drm_dp_aux *aux;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100829 unsigned long value;
Thierry Reding86f5c522014-03-26 11:13:16 +0100830 int err = 0;
831
832 mutex_lock(&sor->lock);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100833
834 if (sor->enabled)
Thierry Reding86f5c522014-03-26 11:13:16 +0100835 goto unlock;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100836
837 err = clk_prepare_enable(sor->clk);
838 if (err < 0)
Thierry Reding86f5c522014-03-26 11:13:16 +0100839 goto unlock;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100840
841 reset_control_deassert(sor->rst);
842
Thierry Reding6fad8f62014-11-28 15:41:34 +0100843 if (output->panel)
844 drm_panel_prepare(output->panel);
845
Thierry Reding34fa1832014-06-05 16:31:10 +0200846 /* FIXME: properly convert to struct drm_dp_aux */
847 aux = (struct drm_dp_aux *)sor->dpaux;
848
Thierry Reding6b6b6042013-11-15 16:06:05 +0100849 if (sor->dpaux) {
850 err = tegra_dpaux_enable(sor->dpaux);
851 if (err < 0)
852 dev_err(sor->dev, "failed to enable DP: %d\n", err);
Thierry Reding34fa1832014-06-05 16:31:10 +0200853
854 err = drm_dp_link_probe(aux, &link);
855 if (err < 0) {
856 dev_err(sor->dev, "failed to probe eDP link: %d\n",
857 err);
Dan Carpenter2263c462014-06-11 10:06:09 +0300858 goto unlock;
Thierry Reding34fa1832014-06-05 16:31:10 +0200859 }
Thierry Reding6b6b6042013-11-15 16:06:05 +0100860 }
861
862 err = clk_set_parent(sor->clk, sor->clk_safe);
863 if (err < 0)
864 dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
865
Thierry Reding34fa1832014-06-05 16:31:10 +0200866 memset(&config, 0, sizeof(config));
Stéphane Marchesin054b1bd2014-06-19 18:18:29 -0700867 config.bits_per_pixel = output->connector.display_info.bpc * 3;
Thierry Reding34fa1832014-06-05 16:31:10 +0200868
869 err = tegra_sor_calc_config(sor, mode, &config, &link);
870 if (err < 0)
871 dev_err(sor->dev, "failed to compute link configuration: %d\n",
872 err);
873
Thierry Reding6b6b6042013-11-15 16:06:05 +0100874 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
875 value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
876 value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
877 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
878
879 value = tegra_sor_readl(sor, SOR_PLL_2);
880 value &= ~SOR_PLL_2_BANDGAP_POWERDOWN;
881 tegra_sor_writel(sor, value, SOR_PLL_2);
882 usleep_range(20, 100);
883
884 value = tegra_sor_readl(sor, SOR_PLL_3);
885 value |= SOR_PLL_3_PLL_VDD_MODE_V3_3;
886 tegra_sor_writel(sor, value, SOR_PLL_3);
887
888 value = SOR_PLL_0_ICHPMP(0xf) | SOR_PLL_0_VCOCAP_RST |
889 SOR_PLL_0_PLLREG_LEVEL_V45 | SOR_PLL_0_RESISTOR_EXT;
890 tegra_sor_writel(sor, value, SOR_PLL_0);
891
892 value = tegra_sor_readl(sor, SOR_PLL_2);
893 value |= SOR_PLL_2_SEQ_PLLCAPPD;
894 value &= ~SOR_PLL_2_SEQ_PLLCAPPD_ENFORCE;
895 value |= SOR_PLL_2_LVDS_ENABLE;
896 tegra_sor_writel(sor, value, SOR_PLL_2);
897
898 value = SOR_PLL_1_TERM_COMPOUT | SOR_PLL_1_TMDS_TERM;
899 tegra_sor_writel(sor, value, SOR_PLL_1);
900
901 while (true) {
902 value = tegra_sor_readl(sor, SOR_PLL_2);
903 if ((value & SOR_PLL_2_SEQ_PLLCAPPD_ENFORCE) == 0)
904 break;
905
906 usleep_range(250, 1000);
907 }
908
909 value = tegra_sor_readl(sor, SOR_PLL_2);
910 value &= ~SOR_PLL_2_POWERDOWN_OVERRIDE;
911 value &= ~SOR_PLL_2_PORT_POWERDOWN;
912 tegra_sor_writel(sor, value, SOR_PLL_2);
913
914 /*
915 * power up
916 */
917
918 /* set safe link bandwidth (1.62 Gbps) */
919 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
920 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
921 value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G1_62;
922 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
923
924 /* step 1 */
925 value = tegra_sor_readl(sor, SOR_PLL_2);
926 value |= SOR_PLL_2_SEQ_PLLCAPPD_ENFORCE | SOR_PLL_2_PORT_POWERDOWN |
927 SOR_PLL_2_BANDGAP_POWERDOWN;
928 tegra_sor_writel(sor, value, SOR_PLL_2);
929
930 value = tegra_sor_readl(sor, SOR_PLL_0);
931 value |= SOR_PLL_0_VCOPD | SOR_PLL_0_POWER_OFF;
932 tegra_sor_writel(sor, value, SOR_PLL_0);
933
934 value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
935 value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
936 tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
937
938 /* step 2 */
939 err = tegra_io_rail_power_on(TEGRA_IO_RAIL_LVDS);
940 if (err < 0) {
941 dev_err(sor->dev, "failed to power on I/O rail: %d\n", err);
Thierry Reding86f5c522014-03-26 11:13:16 +0100942 goto unlock;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100943 }
944
945 usleep_range(5, 100);
946
947 /* step 3 */
948 value = tegra_sor_readl(sor, SOR_PLL_2);
949 value &= ~SOR_PLL_2_BANDGAP_POWERDOWN;
950 tegra_sor_writel(sor, value, SOR_PLL_2);
951
952 usleep_range(20, 100);
953
954 /* step 4 */
955 value = tegra_sor_readl(sor, SOR_PLL_0);
956 value &= ~SOR_PLL_0_POWER_OFF;
957 value &= ~SOR_PLL_0_VCOPD;
958 tegra_sor_writel(sor, value, SOR_PLL_0);
959
960 value = tegra_sor_readl(sor, SOR_PLL_2);
961 value &= ~SOR_PLL_2_SEQ_PLLCAPPD_ENFORCE;
962 tegra_sor_writel(sor, value, SOR_PLL_2);
963
964 usleep_range(200, 1000);
965
966 /* step 5 */
967 value = tegra_sor_readl(sor, SOR_PLL_2);
968 value &= ~SOR_PLL_2_PORT_POWERDOWN;
969 tegra_sor_writel(sor, value, SOR_PLL_2);
970
971 /* switch to DP clock */
972 err = clk_set_parent(sor->clk, sor->clk_dp);
973 if (err < 0)
974 dev_err(sor->dev, "failed to set DP parent clock: %d\n", err);
975
Thierry Reding899451b2014-06-05 16:19:48 +0200976 /* power DP lanes */
Thierry Reding6b6b6042013-11-15 16:06:05 +0100977 value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
Thierry Reding899451b2014-06-05 16:19:48 +0200978
979 if (link.num_lanes <= 2)
980 value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2);
981 else
982 value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2;
983
984 if (link.num_lanes <= 1)
985 value &= ~SOR_DP_PADCTL_PD_TXD_1;
986 else
987 value |= SOR_DP_PADCTL_PD_TXD_1;
988
989 if (link.num_lanes == 0)
990 value &= ~SOR_DP_PADCTL_PD_TXD_0;
991 else
992 value |= SOR_DP_PADCTL_PD_TXD_0;
993
Thierry Reding6b6b6042013-11-15 16:06:05 +0100994 tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
995
996 value = tegra_sor_readl(sor, SOR_DP_LINKCTL_0);
997 value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
Thierry Reding0c90a182014-06-05 16:29:46 +0200998 value |= SOR_DP_LINKCTL_LANE_COUNT(link.num_lanes);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100999 tegra_sor_writel(sor, value, SOR_DP_LINKCTL_0);
1000
1001 /* start lane sequencer */
1002 value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
1003 SOR_LANE_SEQ_CTL_POWER_STATE_UP;
1004 tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
1005
1006 while (true) {
1007 value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
1008 if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
1009 break;
1010
1011 usleep_range(250, 1000);
1012 }
1013
Thierry Redinga4263fe2014-06-05 16:16:23 +02001014 /* set link bandwidth */
Thierry Reding6b6b6042013-11-15 16:06:05 +01001015 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
1016 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
Thierry Redinga4263fe2014-06-05 16:16:23 +02001017 value |= drm_dp_link_rate_to_bw_code(link.rate) << 2;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001018 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
1019
1020 /* set linkctl */
1021 value = tegra_sor_readl(sor, SOR_DP_LINKCTL_0);
1022 value |= SOR_DP_LINKCTL_ENABLE;
1023
1024 value &= ~SOR_DP_LINKCTL_TU_SIZE_MASK;
Thierry Reding34fa1832014-06-05 16:31:10 +02001025 value |= SOR_DP_LINKCTL_TU_SIZE(config.tu_size);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001026
1027 value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
1028 tegra_sor_writel(sor, value, SOR_DP_LINKCTL_0);
1029
1030 for (i = 0, value = 0; i < 4; i++) {
1031 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
1032 SOR_DP_TPG_SCRAMBLER_GALIOS |
1033 SOR_DP_TPG_PATTERN_NONE;
1034 value = (value << 8) | lane;
1035 }
1036
1037 tegra_sor_writel(sor, value, SOR_DP_TPG);
1038
1039 value = tegra_sor_readl(sor, SOR_DP_CONFIG_0);
1040 value &= ~SOR_DP_CONFIG_WATERMARK_MASK;
Thierry Reding34fa1832014-06-05 16:31:10 +02001041 value |= SOR_DP_CONFIG_WATERMARK(config.watermark);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001042
1043 value &= ~SOR_DP_CONFIG_ACTIVE_SYM_COUNT_MASK;
Thierry Reding34fa1832014-06-05 16:31:10 +02001044 value |= SOR_DP_CONFIG_ACTIVE_SYM_COUNT(config.active_count);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001045
1046 value &= ~SOR_DP_CONFIG_ACTIVE_SYM_FRAC_MASK;
Thierry Reding34fa1832014-06-05 16:31:10 +02001047 value |= SOR_DP_CONFIG_ACTIVE_SYM_FRAC(config.active_frac);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001048
Thierry Reding34fa1832014-06-05 16:31:10 +02001049 if (config.active_polarity)
1050 value |= SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
1051 else
1052 value &= ~SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001053
1054 value |= SOR_DP_CONFIG_ACTIVE_SYM_ENABLE;
Thierry Reding1f64ae72014-06-05 16:20:27 +02001055 value |= SOR_DP_CONFIG_DISPARITY_NEGATIVE;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001056 tegra_sor_writel(sor, value, SOR_DP_CONFIG_0);
1057
1058 value = tegra_sor_readl(sor, SOR_DP_AUDIO_HBLANK_SYMBOLS);
1059 value &= ~SOR_DP_AUDIO_HBLANK_SYMBOLS_MASK;
Thierry Reding7890b572014-06-05 16:12:46 +02001060 value |= config.hblank_symbols & 0xffff;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001061 tegra_sor_writel(sor, value, SOR_DP_AUDIO_HBLANK_SYMBOLS);
1062
1063 value = tegra_sor_readl(sor, SOR_DP_AUDIO_VBLANK_SYMBOLS);
1064 value &= ~SOR_DP_AUDIO_VBLANK_SYMBOLS_MASK;
Thierry Reding7890b572014-06-05 16:12:46 +02001065 value |= config.vblank_symbols & 0xffff;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001066 tegra_sor_writel(sor, value, SOR_DP_AUDIO_VBLANK_SYMBOLS);
1067
1068 /* enable pad calibration logic */
1069 value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
1070 value |= SOR_DP_PADCTL_PAD_CAL_PD;
1071 tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
1072
1073 if (sor->dpaux) {
Thierry Reding6b6b6042013-11-15 16:06:05 +01001074 u8 rate, lanes;
1075
1076 err = drm_dp_link_probe(aux, &link);
1077 if (err < 0) {
1078 dev_err(sor->dev, "failed to probe eDP link: %d\n",
1079 err);
Thierry Reding86f5c522014-03-26 11:13:16 +01001080 goto unlock;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001081 }
1082
1083 err = drm_dp_link_power_up(aux, &link);
1084 if (err < 0) {
1085 dev_err(sor->dev, "failed to power up eDP link: %d\n",
1086 err);
Thierry Reding86f5c522014-03-26 11:13:16 +01001087 goto unlock;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001088 }
1089
1090 err = drm_dp_link_configure(aux, &link);
1091 if (err < 0) {
1092 dev_err(sor->dev, "failed to configure eDP link: %d\n",
1093 err);
Thierry Reding86f5c522014-03-26 11:13:16 +01001094 goto unlock;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001095 }
1096
1097 rate = drm_dp_link_rate_to_bw_code(link.rate);
1098 lanes = link.num_lanes;
1099
1100 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
1101 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
1102 value |= SOR_CLK_CNTRL_DP_LINK_SPEED(rate);
1103 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
1104
1105 value = tegra_sor_readl(sor, SOR_DP_LINKCTL_0);
1106 value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
1107 value |= SOR_DP_LINKCTL_LANE_COUNT(lanes);
1108
1109 if (link.capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
1110 value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
1111
1112 tegra_sor_writel(sor, value, SOR_DP_LINKCTL_0);
1113
1114 /* disable training pattern generator */
1115
1116 for (i = 0; i < link.num_lanes; i++) {
1117 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
1118 SOR_DP_TPG_SCRAMBLER_GALIOS |
1119 SOR_DP_TPG_PATTERN_NONE;
1120 value = (value << 8) | lane;
1121 }
1122
1123 tegra_sor_writel(sor, value, SOR_DP_TPG);
1124
1125 err = tegra_sor_dp_train_fast(sor, &link);
1126 if (err < 0) {
1127 dev_err(sor->dev, "DP fast link training failed: %d\n",
1128 err);
Thierry Reding86f5c522014-03-26 11:13:16 +01001129 goto unlock;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001130 }
1131
1132 dev_dbg(sor->dev, "fast link training succeeded\n");
1133 }
1134
1135 err = tegra_sor_power_up(sor, 250);
1136 if (err < 0) {
1137 dev_err(sor->dev, "failed to power up SOR: %d\n", err);
Thierry Reding86f5c522014-03-26 11:13:16 +01001138 goto unlock;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001139 }
1140
1141 /* start display controller in continuous mode */
1142 value = tegra_dc_readl(dc, DC_CMD_STATE_ACCESS);
1143 value |= WRITE_MUX;
1144 tegra_dc_writel(dc, value, DC_CMD_STATE_ACCESS);
1145
1146 tegra_dc_writel(dc, VSYNC_H_POSITION(1), DC_DISP_DISP_TIMING_OPTIONS);
1147 tegra_dc_writel(dc, DISP_CTRL_MODE_C_DISPLAY, DC_CMD_DISPLAY_COMMAND);
1148
1149 value = tegra_dc_readl(dc, DC_CMD_STATE_ACCESS);
1150 value &= ~WRITE_MUX;
1151 tegra_dc_writel(dc, value, DC_CMD_STATE_ACCESS);
1152
1153 /*
1154 * configure panel (24bpp, vsync-, hsync-, DP-A protocol, complete
1155 * raster, associate with display controller)
1156 */
Thierry Reding3f4f3b52014-07-07 15:35:06 +02001157 value = SOR_STATE_ASY_PROTOCOL_DP_A |
Thierry Reding6b6b6042013-11-15 16:06:05 +01001158 SOR_STATE_ASY_CRC_MODE_COMPLETE |
1159 SOR_STATE_ASY_OWNER(dc->pipe + 1);
Thierry Reding34fa1832014-06-05 16:31:10 +02001160
Thierry Reding3f4f3b52014-07-07 15:35:06 +02001161 if (mode->flags & DRM_MODE_FLAG_PHSYNC)
1162 value &= ~SOR_STATE_ASY_HSYNCPOL;
1163
1164 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1165 value |= SOR_STATE_ASY_HSYNCPOL;
1166
1167 if (mode->flags & DRM_MODE_FLAG_PVSYNC)
1168 value &= ~SOR_STATE_ASY_VSYNCPOL;
1169
1170 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1171 value |= SOR_STATE_ASY_VSYNCPOL;
1172
Thierry Reding34fa1832014-06-05 16:31:10 +02001173 switch (config.bits_per_pixel) {
1174 case 24:
1175 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
1176 break;
1177
1178 case 18:
1179 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_18_444;
1180 break;
1181
1182 default:
1183 BUG();
1184 break;
1185 }
1186
Thierry Reding6b6b6042013-11-15 16:06:05 +01001187 tegra_sor_writel(sor, value, SOR_STATE_1);
1188
1189 /*
1190 * TODO: The video timing programming below doesn't seem to match the
1191 * register definitions.
1192 */
1193
1194 value = ((mode->vtotal & 0x7fff) << 16) | (mode->htotal & 0x7fff);
1195 tegra_sor_writel(sor, value, SOR_HEAD_STATE_1(0));
1196
1197 vse = mode->vsync_end - mode->vsync_start - 1;
1198 hse = mode->hsync_end - mode->hsync_start - 1;
1199
1200 value = ((vse & 0x7fff) << 16) | (hse & 0x7fff);
1201 tegra_sor_writel(sor, value, SOR_HEAD_STATE_2(0));
1202
1203 vbe = vse + (mode->vsync_start - mode->vdisplay);
1204 hbe = hse + (mode->hsync_start - mode->hdisplay);
1205
1206 value = ((vbe & 0x7fff) << 16) | (hbe & 0x7fff);
1207 tegra_sor_writel(sor, value, SOR_HEAD_STATE_3(0));
1208
1209 vbs = vbe + mode->vdisplay;
1210 hbs = hbe + mode->hdisplay;
1211
1212 value = ((vbs & 0x7fff) << 16) | (hbs & 0x7fff);
1213 tegra_sor_writel(sor, value, SOR_HEAD_STATE_4(0));
1214
Thierry Reding6b6b6042013-11-15 16:06:05 +01001215 /* CSTM (LVDS, link A/B, upper) */
Stéphane Marchesin143b1df2014-05-22 20:32:47 -07001216 value = SOR_CSTM_LVDS | SOR_CSTM_LINK_ACT_A | SOR_CSTM_LINK_ACT_B |
Thierry Reding6b6b6042013-11-15 16:06:05 +01001217 SOR_CSTM_UPPER;
1218 tegra_sor_writel(sor, value, SOR_CSTM);
1219
1220 /* PWM setup */
1221 err = tegra_sor_setup_pwm(sor, 250);
1222 if (err < 0) {
1223 dev_err(sor->dev, "failed to setup PWM: %d\n", err);
Thierry Reding86f5c522014-03-26 11:13:16 +01001224 goto unlock;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001225 }
1226
1227 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
1228 value |= SOR_ENABLE;
1229 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
1230
1231 tegra_sor_update(sor);
1232
1233 err = tegra_sor_attach(sor);
1234 if (err < 0) {
1235 dev_err(sor->dev, "failed to attach SOR: %d\n", err);
Thierry Reding86f5c522014-03-26 11:13:16 +01001236 goto unlock;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001237 }
1238
1239 err = tegra_sor_wakeup(sor);
1240 if (err < 0) {
1241 dev_err(sor->dev, "failed to enable DC: %d\n", err);
Thierry Reding86f5c522014-03-26 11:13:16 +01001242 goto unlock;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001243 }
1244
Thierry Reding6fad8f62014-11-28 15:41:34 +01001245 if (output->panel)
1246 drm_panel_enable(output->panel);
1247
Thierry Reding6b6b6042013-11-15 16:06:05 +01001248 sor->enabled = true;
1249
Thierry Reding86f5c522014-03-26 11:13:16 +01001250unlock:
1251 mutex_unlock(&sor->lock);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001252}
1253
Thierry Reding6fad8f62014-11-28 15:41:34 +01001254static void tegra_sor_encoder_disable(struct drm_encoder *encoder)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001255{
Thierry Reding6fad8f62014-11-28 15:41:34 +01001256 struct tegra_output *output = encoder_to_output(encoder);
1257 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001258 struct tegra_sor *sor = to_sor(output);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001259 u32 value;
1260 int err;
Thierry Reding86f5c522014-03-26 11:13:16 +01001261
1262 mutex_lock(&sor->lock);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001263
1264 if (!sor->enabled)
Thierry Reding86f5c522014-03-26 11:13:16 +01001265 goto unlock;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001266
Thierry Reding6fad8f62014-11-28 15:41:34 +01001267 if (output->panel)
1268 drm_panel_disable(output->panel);
1269
Thierry Reding6b6b6042013-11-15 16:06:05 +01001270 err = tegra_sor_detach(sor);
1271 if (err < 0) {
1272 dev_err(sor->dev, "failed to detach SOR: %d\n", err);
Thierry Reding86f5c522014-03-26 11:13:16 +01001273 goto unlock;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001274 }
1275
1276 tegra_sor_writel(sor, 0, SOR_STATE_1);
1277 tegra_sor_update(sor);
1278
1279 /*
1280 * The following accesses registers of the display controller, so make
1281 * sure it's only executed when the output is attached to one.
1282 */
1283 if (dc) {
Thierry Reding6b6b6042013-11-15 16:06:05 +01001284 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
1285 value &= ~SOR_ENABLE;
1286 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
1287
Thierry Reding62b9e062014-11-21 17:33:33 +01001288 tegra_dc_commit(dc);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001289 }
1290
1291 err = tegra_sor_power_down(sor);
1292 if (err < 0) {
1293 dev_err(sor->dev, "failed to power down SOR: %d\n", err);
Thierry Reding86f5c522014-03-26 11:13:16 +01001294 goto unlock;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001295 }
1296
1297 if (sor->dpaux) {
1298 err = tegra_dpaux_disable(sor->dpaux);
1299 if (err < 0) {
1300 dev_err(sor->dev, "failed to disable DP: %d\n", err);
Thierry Reding86f5c522014-03-26 11:13:16 +01001301 goto unlock;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001302 }
1303 }
1304
1305 err = tegra_io_rail_power_off(TEGRA_IO_RAIL_LVDS);
1306 if (err < 0) {
1307 dev_err(sor->dev, "failed to power off I/O rail: %d\n", err);
Thierry Reding86f5c522014-03-26 11:13:16 +01001308 goto unlock;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001309 }
1310
Thierry Reding6fad8f62014-11-28 15:41:34 +01001311 if (output->panel)
1312 drm_panel_unprepare(output->panel);
1313
Thierry Reding6b6b6042013-11-15 16:06:05 +01001314 clk_disable_unprepare(sor->clk);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001315 reset_control_assert(sor->rst);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001316
1317 sor->enabled = false;
1318
Thierry Reding86f5c522014-03-26 11:13:16 +01001319unlock:
1320 mutex_unlock(&sor->lock);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001321}
1322
Thierry Reding82f15112014-12-08 17:26:46 +01001323static int
1324tegra_sor_encoder_atomic_check(struct drm_encoder *encoder,
1325 struct drm_crtc_state *crtc_state,
1326 struct drm_connector_state *conn_state)
1327{
1328 struct tegra_output *output = encoder_to_output(encoder);
1329 struct tegra_dc *dc = to_tegra_dc(conn_state->crtc);
1330 unsigned long pclk = crtc_state->mode.clock * 1000;
1331 struct tegra_sor *sor = to_sor(output);
1332 int err;
1333
1334 err = tegra_dc_state_setup_clock(dc, crtc_state, sor->clk_parent,
1335 pclk, 0);
1336 if (err < 0) {
1337 dev_err(output->dev, "failed to setup CRTC state: %d\n", err);
1338 return err;
1339 }
1340
1341 return 0;
1342}
1343
Thierry Reding6fad8f62014-11-28 15:41:34 +01001344static const struct drm_encoder_helper_funcs tegra_sor_encoder_helper_funcs = {
1345 .dpms = tegra_sor_encoder_dpms,
1346 .mode_fixup = tegra_sor_encoder_mode_fixup,
1347 .prepare = tegra_sor_encoder_prepare,
1348 .commit = tegra_sor_encoder_commit,
1349 .mode_set = tegra_sor_encoder_mode_set,
1350 .disable = tegra_sor_encoder_disable,
Thierry Reding82f15112014-12-08 17:26:46 +01001351 .atomic_check = tegra_sor_encoder_atomic_check,
Thierry Reding6b6b6042013-11-15 16:06:05 +01001352};
1353
1354static int tegra_sor_init(struct host1x_client *client)
1355{
Thierry Reding9910f5c2014-05-22 09:57:15 +02001356 struct drm_device *drm = dev_get_drvdata(client->parent);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001357 struct tegra_sor *sor = host1x_client_to_sor(client);
1358 int err;
1359
1360 if (!sor->dpaux)
1361 return -ENODEV;
1362
Thierry Reding6b6b6042013-11-15 16:06:05 +01001363 sor->output.dev = sor->dev;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001364
Thierry Reding6fad8f62014-11-28 15:41:34 +01001365 drm_connector_init(drm, &sor->output.connector,
1366 &tegra_sor_connector_funcs,
1367 DRM_MODE_CONNECTOR_eDP);
1368 drm_connector_helper_add(&sor->output.connector,
1369 &tegra_sor_connector_helper_funcs);
1370 sor->output.connector.dpms = DRM_MODE_DPMS_OFF;
1371
Thierry Reding6fad8f62014-11-28 15:41:34 +01001372 drm_encoder_init(drm, &sor->output.encoder, &tegra_sor_encoder_funcs,
1373 DRM_MODE_ENCODER_TMDS);
1374 drm_encoder_helper_add(&sor->output.encoder,
1375 &tegra_sor_encoder_helper_funcs);
1376
1377 drm_mode_connector_attach_encoder(&sor->output.connector,
1378 &sor->output.encoder);
1379 drm_connector_register(&sor->output.connector);
1380
Thierry Redingea130b22014-12-19 15:51:35 +01001381 err = tegra_output_init(drm, &sor->output);
1382 if (err < 0) {
1383 dev_err(client->dev, "failed to initialize output: %d\n", err);
1384 return err;
1385 }
Thierry Reding6fad8f62014-11-28 15:41:34 +01001386
Thierry Redingea130b22014-12-19 15:51:35 +01001387 sor->output.encoder.possible_crtcs = 0x3;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001388
Thierry Redinga82752e2014-01-31 10:02:15 +01001389 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
Thierry Reding1b0c7b42014-05-28 13:46:12 +02001390 err = tegra_sor_debugfs_init(sor, drm->primary);
Thierry Redinga82752e2014-01-31 10:02:15 +01001391 if (err < 0)
1392 dev_err(sor->dev, "debugfs setup failed: %d\n", err);
1393 }
1394
Thierry Reding6b6b6042013-11-15 16:06:05 +01001395 if (sor->dpaux) {
1396 err = tegra_dpaux_attach(sor->dpaux, &sor->output);
1397 if (err < 0) {
1398 dev_err(sor->dev, "failed to attach DP: %d\n", err);
1399 return err;
1400 }
1401 }
1402
Thierry Reding6fad8f62014-11-28 15:41:34 +01001403 err = clk_prepare_enable(sor->clk);
1404 if (err < 0) {
1405 dev_err(sor->dev, "failed to enable clock: %d\n", err);
1406 return err;
1407 }
1408
1409 err = clk_prepare_enable(sor->clk_safe);
1410 if (err < 0)
1411 return err;
1412
1413 err = clk_prepare_enable(sor->clk_dp);
1414 if (err < 0)
1415 return err;
1416
Thierry Reding6b6b6042013-11-15 16:06:05 +01001417 return 0;
1418}
1419
1420static int tegra_sor_exit(struct host1x_client *client)
1421{
1422 struct tegra_sor *sor = host1x_client_to_sor(client);
1423 int err;
1424
Thierry Reding328ec692014-12-19 15:55:08 +01001425 tegra_output_exit(&sor->output);
1426
Thierry Reding6b6b6042013-11-15 16:06:05 +01001427 if (sor->dpaux) {
1428 err = tegra_dpaux_detach(sor->dpaux);
1429 if (err < 0) {
1430 dev_err(sor->dev, "failed to detach DP: %d\n", err);
1431 return err;
1432 }
1433 }
1434
Thierry Reding6fad8f62014-11-28 15:41:34 +01001435 clk_disable_unprepare(sor->clk_safe);
1436 clk_disable_unprepare(sor->clk_dp);
1437 clk_disable_unprepare(sor->clk);
1438
Thierry Reding4009c222014-12-19 15:47:30 +01001439 if (IS_ENABLED(CONFIG_DEBUG_FS))
1440 tegra_sor_debugfs_exit(sor);
Thierry Redinga82752e2014-01-31 10:02:15 +01001441
Thierry Reding6b6b6042013-11-15 16:06:05 +01001442 return 0;
1443}
1444
1445static const struct host1x_client_ops sor_client_ops = {
1446 .init = tegra_sor_init,
1447 .exit = tegra_sor_exit,
1448};
1449
1450static int tegra_sor_probe(struct platform_device *pdev)
1451{
1452 struct device_node *np;
1453 struct tegra_sor *sor;
1454 struct resource *regs;
1455 int err;
1456
1457 sor = devm_kzalloc(&pdev->dev, sizeof(*sor), GFP_KERNEL);
1458 if (!sor)
1459 return -ENOMEM;
1460
1461 sor->output.dev = sor->dev = &pdev->dev;
1462
1463 np = of_parse_phandle(pdev->dev.of_node, "nvidia,dpaux", 0);
1464 if (np) {
1465 sor->dpaux = tegra_dpaux_find_by_of_node(np);
1466 of_node_put(np);
1467
1468 if (!sor->dpaux)
1469 return -EPROBE_DEFER;
1470 }
1471
1472 err = tegra_output_probe(&sor->output);
1473 if (err < 0)
1474 return err;
1475
1476 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1477 sor->regs = devm_ioremap_resource(&pdev->dev, regs);
1478 if (IS_ERR(sor->regs))
1479 return PTR_ERR(sor->regs);
1480
1481 sor->rst = devm_reset_control_get(&pdev->dev, "sor");
1482 if (IS_ERR(sor->rst))
1483 return PTR_ERR(sor->rst);
1484
1485 sor->clk = devm_clk_get(&pdev->dev, NULL);
1486 if (IS_ERR(sor->clk))
1487 return PTR_ERR(sor->clk);
1488
1489 sor->clk_parent = devm_clk_get(&pdev->dev, "parent");
1490 if (IS_ERR(sor->clk_parent))
1491 return PTR_ERR(sor->clk_parent);
1492
Thierry Reding6b6b6042013-11-15 16:06:05 +01001493 sor->clk_safe = devm_clk_get(&pdev->dev, "safe");
1494 if (IS_ERR(sor->clk_safe))
1495 return PTR_ERR(sor->clk_safe);
1496
Thierry Reding6b6b6042013-11-15 16:06:05 +01001497 sor->clk_dp = devm_clk_get(&pdev->dev, "dp");
1498 if (IS_ERR(sor->clk_dp))
1499 return PTR_ERR(sor->clk_dp);
1500
Thierry Reding6b6b6042013-11-15 16:06:05 +01001501 INIT_LIST_HEAD(&sor->client.list);
1502 sor->client.ops = &sor_client_ops;
1503 sor->client.dev = &pdev->dev;
1504
Thierry Reding86f5c522014-03-26 11:13:16 +01001505 mutex_init(&sor->lock);
1506
Thierry Reding6b6b6042013-11-15 16:06:05 +01001507 err = host1x_client_register(&sor->client);
1508 if (err < 0) {
1509 dev_err(&pdev->dev, "failed to register host1x client: %d\n",
1510 err);
1511 return err;
1512 }
1513
1514 platform_set_drvdata(pdev, sor);
1515
1516 return 0;
1517}
1518
1519static int tegra_sor_remove(struct platform_device *pdev)
1520{
1521 struct tegra_sor *sor = platform_get_drvdata(pdev);
1522 int err;
1523
1524 err = host1x_client_unregister(&sor->client);
1525 if (err < 0) {
1526 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
1527 err);
1528 return err;
1529 }
1530
Thierry Reding328ec692014-12-19 15:55:08 +01001531 tegra_output_remove(&sor->output);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001532
1533 return 0;
1534}
1535
1536static const struct of_device_id tegra_sor_of_match[] = {
1537 { .compatible = "nvidia,tegra124-sor", },
1538 { },
1539};
Stephen Warrenef707282014-06-18 16:21:55 -06001540MODULE_DEVICE_TABLE(of, tegra_sor_of_match);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001541
1542struct platform_driver tegra_sor_driver = {
1543 .driver = {
1544 .name = "tegra-sor",
1545 .of_match_table = tegra_sor_of_match,
1546 },
1547 .probe = tegra_sor_probe,
1548 .remove = tegra_sor_remove,
1549};