blob: 4e354ee4b20379c9d093fcbbdf1b1397e7586de0 [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 Reding6b6b6042013-11-15 16:06:05 +010011#include <linux/io.h>
12#include <linux/platform_device.h>
13#include <linux/reset.h>
14#include <linux/tegra-powergate.h>
15
16#include <drm/drm_dp_helper.h>
17
18#include "dc.h"
19#include "drm.h"
20#include "sor.h"
21
22struct tegra_sor {
23 struct host1x_client client;
24 struct tegra_output output;
25 struct device *dev;
26
27 void __iomem *regs;
28
29 struct reset_control *rst;
30 struct clk *clk_parent;
31 struct clk *clk_safe;
32 struct clk *clk_dp;
33 struct clk *clk;
34
35 struct tegra_dpaux *dpaux;
36
Thierry Reding86f5c522014-03-26 11:13:16 +010037 struct mutex lock;
Thierry Reding6b6b6042013-11-15 16:06:05 +010038 bool enabled;
Thierry Redinga82752e2014-01-31 10:02:15 +010039
40 struct dentry *debugfs;
Thierry Reding6b6b6042013-11-15 16:06:05 +010041};
42
Thierry Reding34fa1832014-06-05 16:31:10 +020043struct tegra_sor_config {
44 u32 bits_per_pixel;
45
46 u32 active_polarity;
47 u32 active_count;
48 u32 tu_size;
49 u32 active_frac;
50 u32 watermark;
Thierry Reding7890b572014-06-05 16:12:46 +020051
52 u32 hblank_symbols;
53 u32 vblank_symbols;
Thierry Reding34fa1832014-06-05 16:31:10 +020054};
55
Thierry Reding6b6b6042013-11-15 16:06:05 +010056static inline struct tegra_sor *
57host1x_client_to_sor(struct host1x_client *client)
58{
59 return container_of(client, struct tegra_sor, client);
60}
61
62static inline struct tegra_sor *to_sor(struct tegra_output *output)
63{
64 return container_of(output, struct tegra_sor, output);
65}
66
67static inline unsigned long tegra_sor_readl(struct tegra_sor *sor,
68 unsigned long offset)
69{
70 return readl(sor->regs + (offset << 2));
71}
72
73static inline void tegra_sor_writel(struct tegra_sor *sor, unsigned long value,
74 unsigned long offset)
75{
76 writel(value, sor->regs + (offset << 2));
77}
78
79static int tegra_sor_dp_train_fast(struct tegra_sor *sor,
80 struct drm_dp_link *link)
81{
82 unsigned long value;
83 unsigned int i;
84 u8 pattern;
85 int err;
86
87 /* setup lane parameters */
88 value = SOR_LANE_DRIVE_CURRENT_LANE3(0x40) |
89 SOR_LANE_DRIVE_CURRENT_LANE2(0x40) |
90 SOR_LANE_DRIVE_CURRENT_LANE1(0x40) |
91 SOR_LANE_DRIVE_CURRENT_LANE0(0x40);
92 tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT_0);
93
94 value = SOR_LANE_PREEMPHASIS_LANE3(0x0f) |
95 SOR_LANE_PREEMPHASIS_LANE2(0x0f) |
96 SOR_LANE_PREEMPHASIS_LANE1(0x0f) |
97 SOR_LANE_PREEMPHASIS_LANE0(0x0f);
98 tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS_0);
99
100 value = SOR_LANE_POST_CURSOR_LANE3(0x00) |
101 SOR_LANE_POST_CURSOR_LANE2(0x00) |
102 SOR_LANE_POST_CURSOR_LANE1(0x00) |
103 SOR_LANE_POST_CURSOR_LANE0(0x00);
104 tegra_sor_writel(sor, value, SOR_LANE_POST_CURSOR_0);
105
106 /* disable LVDS mode */
107 tegra_sor_writel(sor, 0, SOR_LVDS);
108
109 value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
110 value |= SOR_DP_PADCTL_TX_PU_ENABLE;
111 value &= ~SOR_DP_PADCTL_TX_PU_MASK;
112 value |= SOR_DP_PADCTL_TX_PU(2); /* XXX: don't hardcode? */
113 tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
114
115 value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
116 value |= SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
117 SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0;
118 tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
119
120 usleep_range(10, 100);
121
122 value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
123 value &= ~(SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
124 SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0);
125 tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
126
127 err = tegra_dpaux_prepare(sor->dpaux, DP_SET_ANSI_8B10B);
128 if (err < 0)
129 return err;
130
131 for (i = 0, value = 0; i < link->num_lanes; i++) {
132 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
133 SOR_DP_TPG_SCRAMBLER_NONE |
134 SOR_DP_TPG_PATTERN_TRAIN1;
135 value = (value << 8) | lane;
136 }
137
138 tegra_sor_writel(sor, value, SOR_DP_TPG);
139
140 pattern = DP_TRAINING_PATTERN_1;
141
142 err = tegra_dpaux_train(sor->dpaux, link, pattern);
143 if (err < 0)
144 return err;
145
146 value = tegra_sor_readl(sor, SOR_DP_SPARE_0);
147 value |= SOR_DP_SPARE_SEQ_ENABLE;
148 value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
149 value |= SOR_DP_SPARE_MACRO_SOR_CLK;
150 tegra_sor_writel(sor, value, SOR_DP_SPARE_0);
151
152 for (i = 0, value = 0; i < link->num_lanes; i++) {
153 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
154 SOR_DP_TPG_SCRAMBLER_NONE |
155 SOR_DP_TPG_PATTERN_TRAIN2;
156 value = (value << 8) | lane;
157 }
158
159 tegra_sor_writel(sor, value, SOR_DP_TPG);
160
161 pattern = DP_LINK_SCRAMBLING_DISABLE | DP_TRAINING_PATTERN_2;
162
163 err = tegra_dpaux_train(sor->dpaux, link, pattern);
164 if (err < 0)
165 return err;
166
167 for (i = 0, value = 0; i < link->num_lanes; i++) {
168 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
169 SOR_DP_TPG_SCRAMBLER_GALIOS |
170 SOR_DP_TPG_PATTERN_NONE;
171 value = (value << 8) | lane;
172 }
173
174 tegra_sor_writel(sor, value, SOR_DP_TPG);
175
176 pattern = DP_TRAINING_PATTERN_DISABLE;
177
178 err = tegra_dpaux_train(sor->dpaux, link, pattern);
179 if (err < 0)
180 return err;
181
182 return 0;
183}
184
185static void tegra_sor_super_update(struct tegra_sor *sor)
186{
187 tegra_sor_writel(sor, 0, SOR_SUPER_STATE_0);
188 tegra_sor_writel(sor, 1, SOR_SUPER_STATE_0);
189 tegra_sor_writel(sor, 0, SOR_SUPER_STATE_0);
190}
191
192static void tegra_sor_update(struct tegra_sor *sor)
193{
194 tegra_sor_writel(sor, 0, SOR_STATE_0);
195 tegra_sor_writel(sor, 1, SOR_STATE_0);
196 tegra_sor_writel(sor, 0, SOR_STATE_0);
197}
198
199static int tegra_sor_setup_pwm(struct tegra_sor *sor, unsigned long timeout)
200{
201 unsigned long value;
202
203 value = tegra_sor_readl(sor, SOR_PWM_DIV);
204 value &= ~SOR_PWM_DIV_MASK;
205 value |= 0x400; /* period */
206 tegra_sor_writel(sor, value, SOR_PWM_DIV);
207
208 value = tegra_sor_readl(sor, SOR_PWM_CTL);
209 value &= ~SOR_PWM_CTL_DUTY_CYCLE_MASK;
210 value |= 0x400; /* duty cycle */
211 value &= ~SOR_PWM_CTL_CLK_SEL; /* clock source: PCLK */
212 value |= SOR_PWM_CTL_TRIGGER;
213 tegra_sor_writel(sor, value, SOR_PWM_CTL);
214
215 timeout = jiffies + msecs_to_jiffies(timeout);
216
217 while (time_before(jiffies, timeout)) {
218 value = tegra_sor_readl(sor, SOR_PWM_CTL);
219 if ((value & SOR_PWM_CTL_TRIGGER) == 0)
220 return 0;
221
222 usleep_range(25, 100);
223 }
224
225 return -ETIMEDOUT;
226}
227
228static int tegra_sor_attach(struct tegra_sor *sor)
229{
230 unsigned long value, timeout;
231
232 /* wake up in normal mode */
233 value = tegra_sor_readl(sor, SOR_SUPER_STATE_1);
234 value |= SOR_SUPER_STATE_HEAD_MODE_AWAKE;
235 value |= SOR_SUPER_STATE_MODE_NORMAL;
236 tegra_sor_writel(sor, value, SOR_SUPER_STATE_1);
237 tegra_sor_super_update(sor);
238
239 /* attach */
240 value = tegra_sor_readl(sor, SOR_SUPER_STATE_1);
241 value |= SOR_SUPER_STATE_ATTACHED;
242 tegra_sor_writel(sor, value, SOR_SUPER_STATE_1);
243 tegra_sor_super_update(sor);
244
245 timeout = jiffies + msecs_to_jiffies(250);
246
247 while (time_before(jiffies, timeout)) {
248 value = tegra_sor_readl(sor, SOR_TEST);
249 if ((value & SOR_TEST_ATTACHED) != 0)
250 return 0;
251
252 usleep_range(25, 100);
253 }
254
255 return -ETIMEDOUT;
256}
257
258static int tegra_sor_wakeup(struct tegra_sor *sor)
259{
260 struct tegra_dc *dc = to_tegra_dc(sor->output.encoder.crtc);
261 unsigned long value, timeout;
262
263 /* enable display controller outputs */
264 value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
265 value |= PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
266 PW4_ENABLE | PM0_ENABLE | PM1_ENABLE;
267 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
268
269 tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
270 tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
271
272 timeout = jiffies + msecs_to_jiffies(250);
273
274 /* wait for head to wake up */
275 while (time_before(jiffies, timeout)) {
276 value = tegra_sor_readl(sor, SOR_TEST);
277 value &= SOR_TEST_HEAD_MODE_MASK;
278
279 if (value == SOR_TEST_HEAD_MODE_AWAKE)
280 return 0;
281
282 usleep_range(25, 100);
283 }
284
285 return -ETIMEDOUT;
286}
287
288static int tegra_sor_power_up(struct tegra_sor *sor, unsigned long timeout)
289{
290 unsigned long value;
291
292 value = tegra_sor_readl(sor, SOR_PWR);
293 value |= SOR_PWR_TRIGGER | SOR_PWR_NORMAL_STATE_PU;
294 tegra_sor_writel(sor, value, SOR_PWR);
295
296 timeout = jiffies + msecs_to_jiffies(timeout);
297
298 while (time_before(jiffies, timeout)) {
299 value = tegra_sor_readl(sor, SOR_PWR);
300 if ((value & SOR_PWR_TRIGGER) == 0)
301 return 0;
302
303 usleep_range(25, 100);
304 }
305
306 return -ETIMEDOUT;
307}
308
Thierry Reding34fa1832014-06-05 16:31:10 +0200309struct tegra_sor_params {
310 /* number of link clocks per line */
311 unsigned int num_clocks;
312 /* ratio between input and output */
313 u64 ratio;
314 /* precision factor */
315 u64 precision;
316
317 unsigned int active_polarity;
318 unsigned int active_count;
319 unsigned int active_frac;
320 unsigned int tu_size;
321 unsigned int error;
322};
323
324static int tegra_sor_compute_params(struct tegra_sor *sor,
325 struct tegra_sor_params *params,
326 unsigned int tu_size)
327{
328 u64 active_sym, active_count, frac, approx;
329 u32 active_polarity, active_frac = 0;
330 const u64 f = params->precision;
331 s64 error;
332
333 active_sym = params->ratio * tu_size;
334 active_count = div_u64(active_sym, f) * f;
335 frac = active_sym - active_count;
336
337 /* fraction < 0.5 */
338 if (frac >= (f / 2)) {
339 active_polarity = 1;
340 frac = f - frac;
341 } else {
342 active_polarity = 0;
343 }
344
345 if (frac != 0) {
346 frac = div_u64(f * f, frac); /* 1/fraction */
347 if (frac <= (15 * f)) {
348 active_frac = div_u64(frac, f);
349
350 /* round up */
351 if (active_polarity)
352 active_frac++;
353 } else {
354 active_frac = active_polarity ? 1 : 15;
355 }
356 }
357
358 if (active_frac == 1)
359 active_polarity = 0;
360
361 if (active_polarity == 1) {
362 if (active_frac) {
363 approx = active_count + (active_frac * (f - 1)) * f;
364 approx = div_u64(approx, active_frac * f);
365 } else {
366 approx = active_count + f;
367 }
368 } else {
369 if (active_frac)
370 approx = active_count + div_u64(f, active_frac);
371 else
372 approx = active_count;
373 }
374
375 error = div_s64(active_sym - approx, tu_size);
376 error *= params->num_clocks;
377
378 if (error <= 0 && abs64(error) < params->error) {
379 params->active_count = div_u64(active_count, f);
380 params->active_polarity = active_polarity;
381 params->active_frac = active_frac;
382 params->error = abs64(error);
383 params->tu_size = tu_size;
384
385 if (error == 0)
386 return true;
387 }
388
389 return false;
390}
391
392static int tegra_sor_calc_config(struct tegra_sor *sor,
393 struct drm_display_mode *mode,
394 struct tegra_sor_config *config,
395 struct drm_dp_link *link)
396{
397 const u64 f = 100000, link_rate = link->rate * 1000;
398 const u64 pclk = mode->clock * 1000;
Thierry Reding7890b572014-06-05 16:12:46 +0200399 u64 input, output, watermark, num;
Thierry Reding34fa1832014-06-05 16:31:10 +0200400 struct tegra_sor_params params;
Thierry Reding34fa1832014-06-05 16:31:10 +0200401 u32 num_syms_per_line;
402 unsigned int i;
403
404 if (!link_rate || !link->num_lanes || !pclk || !config->bits_per_pixel)
405 return -EINVAL;
406
407 output = link_rate * 8 * link->num_lanes;
408 input = pclk * config->bits_per_pixel;
409
410 if (input >= output)
411 return -ERANGE;
412
413 memset(&params, 0, sizeof(params));
414 params.ratio = div64_u64(input * f, output);
415 params.num_clocks = div_u64(link_rate * mode->hdisplay, pclk);
416 params.precision = f;
417 params.error = 64 * f;
418 params.tu_size = 64;
419
420 for (i = params.tu_size; i >= 32; i--)
421 if (tegra_sor_compute_params(sor, &params, i))
422 break;
423
424 if (params.active_frac == 0) {
425 config->active_polarity = 0;
426 config->active_count = params.active_count;
427
428 if (!params.active_polarity)
429 config->active_count--;
430
431 config->tu_size = params.tu_size;
432 config->active_frac = 1;
433 } else {
434 config->active_polarity = params.active_polarity;
435 config->active_count = params.active_count;
436 config->active_frac = params.active_frac;
437 config->tu_size = params.tu_size;
438 }
439
440 dev_dbg(sor->dev,
441 "polarity: %d active count: %d tu size: %d active frac: %d\n",
442 config->active_polarity, config->active_count,
443 config->tu_size, config->active_frac);
444
445 watermark = params.ratio * config->tu_size * (f - params.ratio);
446 watermark = div_u64(watermark, f);
447
448 watermark = div_u64(watermark + params.error, f);
449 config->watermark = watermark + (config->bits_per_pixel / 8) + 2;
450 num_syms_per_line = (mode->hdisplay * config->bits_per_pixel) *
451 (link->num_lanes * 8);
452
453 if (config->watermark > 30) {
454 config->watermark = 30;
455 dev_err(sor->dev,
456 "unable to compute TU size, forcing watermark to %u\n",
457 config->watermark);
458 } else if (config->watermark > num_syms_per_line) {
459 config->watermark = num_syms_per_line;
460 dev_err(sor->dev, "watermark too high, forcing to %u\n",
461 config->watermark);
462 }
463
Thierry Reding7890b572014-06-05 16:12:46 +0200464 /* compute the number of symbols per horizontal blanking interval */
465 num = ((mode->htotal - mode->hdisplay) - 7) * link_rate;
466 config->hblank_symbols = div_u64(num, pclk);
467
468 if (link->capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
469 config->hblank_symbols -= 3;
470
471 config->hblank_symbols -= 12 / link->num_lanes;
472
473 /* compute the number of symbols per vertical blanking interval */
474 num = (mode->hdisplay - 25) * link_rate;
475 config->vblank_symbols = div_u64(num, pclk);
476 config->vblank_symbols -= 36 / link->num_lanes + 4;
477
478 dev_dbg(sor->dev, "blank symbols: H:%u V:%u\n", config->hblank_symbols,
479 config->vblank_symbols);
480
Thierry Reding34fa1832014-06-05 16:31:10 +0200481 return 0;
482}
483
Thierry Reding6b6b6042013-11-15 16:06:05 +0100484static int tegra_output_sor_enable(struct tegra_output *output)
485{
486 struct tegra_dc *dc = to_tegra_dc(output->encoder.crtc);
487 struct drm_display_mode *mode = &dc->base.mode;
488 unsigned int vbe, vse, hbe, hse, vbs, hbs, i;
489 struct tegra_sor *sor = to_sor(output);
Thierry Reding34fa1832014-06-05 16:31:10 +0200490 struct tegra_sor_config config;
491 struct drm_dp_link link;
492 struct drm_dp_aux *aux;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100493 unsigned long value;
Thierry Reding86f5c522014-03-26 11:13:16 +0100494 int err = 0;
495
496 mutex_lock(&sor->lock);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100497
498 if (sor->enabled)
Thierry Reding86f5c522014-03-26 11:13:16 +0100499 goto unlock;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100500
501 err = clk_prepare_enable(sor->clk);
502 if (err < 0)
Thierry Reding86f5c522014-03-26 11:13:16 +0100503 goto unlock;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100504
505 reset_control_deassert(sor->rst);
506
Thierry Reding34fa1832014-06-05 16:31:10 +0200507 /* FIXME: properly convert to struct drm_dp_aux */
508 aux = (struct drm_dp_aux *)sor->dpaux;
509
Thierry Reding6b6b6042013-11-15 16:06:05 +0100510 if (sor->dpaux) {
511 err = tegra_dpaux_enable(sor->dpaux);
512 if (err < 0)
513 dev_err(sor->dev, "failed to enable DP: %d\n", err);
Thierry Reding34fa1832014-06-05 16:31:10 +0200514
515 err = drm_dp_link_probe(aux, &link);
516 if (err < 0) {
517 dev_err(sor->dev, "failed to probe eDP link: %d\n",
518 err);
519 return err;
520 }
Thierry Reding6b6b6042013-11-15 16:06:05 +0100521 }
522
523 err = clk_set_parent(sor->clk, sor->clk_safe);
524 if (err < 0)
525 dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
526
Thierry Reding34fa1832014-06-05 16:31:10 +0200527 memset(&config, 0, sizeof(config));
528 config.bits_per_pixel = 24; /* XXX: don't hardcode? */
529
530 err = tegra_sor_calc_config(sor, mode, &config, &link);
531 if (err < 0)
532 dev_err(sor->dev, "failed to compute link configuration: %d\n",
533 err);
534
Thierry Reding6b6b6042013-11-15 16:06:05 +0100535 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
536 value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
537 value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
538 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
539
540 value = tegra_sor_readl(sor, SOR_PLL_2);
541 value &= ~SOR_PLL_2_BANDGAP_POWERDOWN;
542 tegra_sor_writel(sor, value, SOR_PLL_2);
543 usleep_range(20, 100);
544
545 value = tegra_sor_readl(sor, SOR_PLL_3);
546 value |= SOR_PLL_3_PLL_VDD_MODE_V3_3;
547 tegra_sor_writel(sor, value, SOR_PLL_3);
548
549 value = SOR_PLL_0_ICHPMP(0xf) | SOR_PLL_0_VCOCAP_RST |
550 SOR_PLL_0_PLLREG_LEVEL_V45 | SOR_PLL_0_RESISTOR_EXT;
551 tegra_sor_writel(sor, value, SOR_PLL_0);
552
553 value = tegra_sor_readl(sor, SOR_PLL_2);
554 value |= SOR_PLL_2_SEQ_PLLCAPPD;
555 value &= ~SOR_PLL_2_SEQ_PLLCAPPD_ENFORCE;
556 value |= SOR_PLL_2_LVDS_ENABLE;
557 tegra_sor_writel(sor, value, SOR_PLL_2);
558
559 value = SOR_PLL_1_TERM_COMPOUT | SOR_PLL_1_TMDS_TERM;
560 tegra_sor_writel(sor, value, SOR_PLL_1);
561
562 while (true) {
563 value = tegra_sor_readl(sor, SOR_PLL_2);
564 if ((value & SOR_PLL_2_SEQ_PLLCAPPD_ENFORCE) == 0)
565 break;
566
567 usleep_range(250, 1000);
568 }
569
570 value = tegra_sor_readl(sor, SOR_PLL_2);
571 value &= ~SOR_PLL_2_POWERDOWN_OVERRIDE;
572 value &= ~SOR_PLL_2_PORT_POWERDOWN;
573 tegra_sor_writel(sor, value, SOR_PLL_2);
574
575 /*
576 * power up
577 */
578
579 /* set safe link bandwidth (1.62 Gbps) */
580 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
581 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
582 value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G1_62;
583 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
584
585 /* step 1 */
586 value = tegra_sor_readl(sor, SOR_PLL_2);
587 value |= SOR_PLL_2_SEQ_PLLCAPPD_ENFORCE | SOR_PLL_2_PORT_POWERDOWN |
588 SOR_PLL_2_BANDGAP_POWERDOWN;
589 tegra_sor_writel(sor, value, SOR_PLL_2);
590
591 value = tegra_sor_readl(sor, SOR_PLL_0);
592 value |= SOR_PLL_0_VCOPD | SOR_PLL_0_POWER_OFF;
593 tegra_sor_writel(sor, value, SOR_PLL_0);
594
595 value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
596 value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
597 tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
598
599 /* step 2 */
600 err = tegra_io_rail_power_on(TEGRA_IO_RAIL_LVDS);
601 if (err < 0) {
602 dev_err(sor->dev, "failed to power on I/O rail: %d\n", err);
Thierry Reding86f5c522014-03-26 11:13:16 +0100603 goto unlock;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100604 }
605
606 usleep_range(5, 100);
607
608 /* step 3 */
609 value = tegra_sor_readl(sor, SOR_PLL_2);
610 value &= ~SOR_PLL_2_BANDGAP_POWERDOWN;
611 tegra_sor_writel(sor, value, SOR_PLL_2);
612
613 usleep_range(20, 100);
614
615 /* step 4 */
616 value = tegra_sor_readl(sor, SOR_PLL_0);
617 value &= ~SOR_PLL_0_POWER_OFF;
618 value &= ~SOR_PLL_0_VCOPD;
619 tegra_sor_writel(sor, value, SOR_PLL_0);
620
621 value = tegra_sor_readl(sor, SOR_PLL_2);
622 value &= ~SOR_PLL_2_SEQ_PLLCAPPD_ENFORCE;
623 tegra_sor_writel(sor, value, SOR_PLL_2);
624
625 usleep_range(200, 1000);
626
627 /* step 5 */
628 value = tegra_sor_readl(sor, SOR_PLL_2);
629 value &= ~SOR_PLL_2_PORT_POWERDOWN;
630 tegra_sor_writel(sor, value, SOR_PLL_2);
631
632 /* switch to DP clock */
633 err = clk_set_parent(sor->clk, sor->clk_dp);
634 if (err < 0)
635 dev_err(sor->dev, "failed to set DP parent clock: %d\n", err);
636
637 /* power dplanes (XXX parameterize based on link?) */
638 value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
639 value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
640 SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2;
641 tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
642
643 value = tegra_sor_readl(sor, SOR_DP_LINKCTL_0);
644 value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
645 value |= SOR_DP_LINKCTL_LANE_COUNT(4);
646 tegra_sor_writel(sor, value, SOR_DP_LINKCTL_0);
647
648 /* start lane sequencer */
649 value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
650 SOR_LANE_SEQ_CTL_POWER_STATE_UP;
651 tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
652
653 while (true) {
654 value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
655 if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
656 break;
657
658 usleep_range(250, 1000);
659 }
660
Thierry Redinga4263fe2014-06-05 16:16:23 +0200661 /* set link bandwidth */
Thierry Reding6b6b6042013-11-15 16:06:05 +0100662 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
663 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
Thierry Redinga4263fe2014-06-05 16:16:23 +0200664 value |= drm_dp_link_rate_to_bw_code(link.rate) << 2;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100665 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
666
667 /* set linkctl */
668 value = tegra_sor_readl(sor, SOR_DP_LINKCTL_0);
669 value |= SOR_DP_LINKCTL_ENABLE;
670
671 value &= ~SOR_DP_LINKCTL_TU_SIZE_MASK;
Thierry Reding34fa1832014-06-05 16:31:10 +0200672 value |= SOR_DP_LINKCTL_TU_SIZE(config.tu_size);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100673
674 value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
675 tegra_sor_writel(sor, value, SOR_DP_LINKCTL_0);
676
677 for (i = 0, value = 0; i < 4; i++) {
678 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
679 SOR_DP_TPG_SCRAMBLER_GALIOS |
680 SOR_DP_TPG_PATTERN_NONE;
681 value = (value << 8) | lane;
682 }
683
684 tegra_sor_writel(sor, value, SOR_DP_TPG);
685
686 value = tegra_sor_readl(sor, SOR_DP_CONFIG_0);
687 value &= ~SOR_DP_CONFIG_WATERMARK_MASK;
Thierry Reding34fa1832014-06-05 16:31:10 +0200688 value |= SOR_DP_CONFIG_WATERMARK(config.watermark);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100689
690 value &= ~SOR_DP_CONFIG_ACTIVE_SYM_COUNT_MASK;
Thierry Reding34fa1832014-06-05 16:31:10 +0200691 value |= SOR_DP_CONFIG_ACTIVE_SYM_COUNT(config.active_count);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100692
693 value &= ~SOR_DP_CONFIG_ACTIVE_SYM_FRAC_MASK;
Thierry Reding34fa1832014-06-05 16:31:10 +0200694 value |= SOR_DP_CONFIG_ACTIVE_SYM_FRAC(config.active_frac);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100695
Thierry Reding34fa1832014-06-05 16:31:10 +0200696 if (config.active_polarity)
697 value |= SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
698 else
699 value &= ~SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100700
701 value |= SOR_DP_CONFIG_ACTIVE_SYM_ENABLE;
702 value |= SOR_DP_CONFIG_DISPARITY_NEGATIVE; /* XXX: don't hardcode? */
703 tegra_sor_writel(sor, value, SOR_DP_CONFIG_0);
704
705 value = tegra_sor_readl(sor, SOR_DP_AUDIO_HBLANK_SYMBOLS);
706 value &= ~SOR_DP_AUDIO_HBLANK_SYMBOLS_MASK;
Thierry Reding7890b572014-06-05 16:12:46 +0200707 value |= config.hblank_symbols & 0xffff;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100708 tegra_sor_writel(sor, value, SOR_DP_AUDIO_HBLANK_SYMBOLS);
709
710 value = tegra_sor_readl(sor, SOR_DP_AUDIO_VBLANK_SYMBOLS);
711 value &= ~SOR_DP_AUDIO_VBLANK_SYMBOLS_MASK;
Thierry Reding7890b572014-06-05 16:12:46 +0200712 value |= config.vblank_symbols & 0xffff;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100713 tegra_sor_writel(sor, value, SOR_DP_AUDIO_VBLANK_SYMBOLS);
714
715 /* enable pad calibration logic */
716 value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
717 value |= SOR_DP_PADCTL_PAD_CAL_PD;
718 tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
719
720 if (sor->dpaux) {
Thierry Reding6b6b6042013-11-15 16:06:05 +0100721 u8 rate, lanes;
722
723 err = drm_dp_link_probe(aux, &link);
724 if (err < 0) {
725 dev_err(sor->dev, "failed to probe eDP link: %d\n",
726 err);
Thierry Reding86f5c522014-03-26 11:13:16 +0100727 goto unlock;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100728 }
729
730 err = drm_dp_link_power_up(aux, &link);
731 if (err < 0) {
732 dev_err(sor->dev, "failed to power up eDP link: %d\n",
733 err);
Thierry Reding86f5c522014-03-26 11:13:16 +0100734 goto unlock;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100735 }
736
737 err = drm_dp_link_configure(aux, &link);
738 if (err < 0) {
739 dev_err(sor->dev, "failed to configure eDP link: %d\n",
740 err);
Thierry Reding86f5c522014-03-26 11:13:16 +0100741 goto unlock;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100742 }
743
744 rate = drm_dp_link_rate_to_bw_code(link.rate);
745 lanes = link.num_lanes;
746
747 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
748 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
749 value |= SOR_CLK_CNTRL_DP_LINK_SPEED(rate);
750 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
751
752 value = tegra_sor_readl(sor, SOR_DP_LINKCTL_0);
753 value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
754 value |= SOR_DP_LINKCTL_LANE_COUNT(lanes);
755
756 if (link.capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
757 value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
758
759 tegra_sor_writel(sor, value, SOR_DP_LINKCTL_0);
760
761 /* disable training pattern generator */
762
763 for (i = 0; i < link.num_lanes; i++) {
764 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
765 SOR_DP_TPG_SCRAMBLER_GALIOS |
766 SOR_DP_TPG_PATTERN_NONE;
767 value = (value << 8) | lane;
768 }
769
770 tegra_sor_writel(sor, value, SOR_DP_TPG);
771
772 err = tegra_sor_dp_train_fast(sor, &link);
773 if (err < 0) {
774 dev_err(sor->dev, "DP fast link training failed: %d\n",
775 err);
Thierry Reding86f5c522014-03-26 11:13:16 +0100776 goto unlock;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100777 }
778
779 dev_dbg(sor->dev, "fast link training succeeded\n");
780 }
781
782 err = tegra_sor_power_up(sor, 250);
783 if (err < 0) {
784 dev_err(sor->dev, "failed to power up SOR: %d\n", err);
Thierry Reding86f5c522014-03-26 11:13:16 +0100785 goto unlock;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100786 }
787
788 /* start display controller in continuous mode */
789 value = tegra_dc_readl(dc, DC_CMD_STATE_ACCESS);
790 value |= WRITE_MUX;
791 tegra_dc_writel(dc, value, DC_CMD_STATE_ACCESS);
792
793 tegra_dc_writel(dc, VSYNC_H_POSITION(1), DC_DISP_DISP_TIMING_OPTIONS);
794 tegra_dc_writel(dc, DISP_CTRL_MODE_C_DISPLAY, DC_CMD_DISPLAY_COMMAND);
795
796 value = tegra_dc_readl(dc, DC_CMD_STATE_ACCESS);
797 value &= ~WRITE_MUX;
798 tegra_dc_writel(dc, value, DC_CMD_STATE_ACCESS);
799
800 /*
801 * configure panel (24bpp, vsync-, hsync-, DP-A protocol, complete
802 * raster, associate with display controller)
803 */
Thierry Reding34fa1832014-06-05 16:31:10 +0200804 value = SOR_STATE_ASY_VSYNCPOL |
Thierry Reding6b6b6042013-11-15 16:06:05 +0100805 SOR_STATE_ASY_HSYNCPOL |
806 SOR_STATE_ASY_PROTOCOL_DP_A |
807 SOR_STATE_ASY_CRC_MODE_COMPLETE |
808 SOR_STATE_ASY_OWNER(dc->pipe + 1);
Thierry Reding34fa1832014-06-05 16:31:10 +0200809
810 switch (config.bits_per_pixel) {
811 case 24:
812 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
813 break;
814
815 case 18:
816 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_18_444;
817 break;
818
819 default:
820 BUG();
821 break;
822 }
823
Thierry Reding6b6b6042013-11-15 16:06:05 +0100824 tegra_sor_writel(sor, value, SOR_STATE_1);
825
826 /*
827 * TODO: The video timing programming below doesn't seem to match the
828 * register definitions.
829 */
830
831 value = ((mode->vtotal & 0x7fff) << 16) | (mode->htotal & 0x7fff);
832 tegra_sor_writel(sor, value, SOR_HEAD_STATE_1(0));
833
834 vse = mode->vsync_end - mode->vsync_start - 1;
835 hse = mode->hsync_end - mode->hsync_start - 1;
836
837 value = ((vse & 0x7fff) << 16) | (hse & 0x7fff);
838 tegra_sor_writel(sor, value, SOR_HEAD_STATE_2(0));
839
840 vbe = vse + (mode->vsync_start - mode->vdisplay);
841 hbe = hse + (mode->hsync_start - mode->hdisplay);
842
843 value = ((vbe & 0x7fff) << 16) | (hbe & 0x7fff);
844 tegra_sor_writel(sor, value, SOR_HEAD_STATE_3(0));
845
846 vbs = vbe + mode->vdisplay;
847 hbs = hbe + mode->hdisplay;
848
849 value = ((vbs & 0x7fff) << 16) | (hbs & 0x7fff);
850 tegra_sor_writel(sor, value, SOR_HEAD_STATE_4(0));
851
852 /* XXX interlaced mode */
853 tegra_sor_writel(sor, 0x00000001, SOR_HEAD_STATE_5(0));
854
855 /* CSTM (LVDS, link A/B, upper) */
Stéphane Marchesin143b1df2014-05-22 20:32:47 -0700856 value = SOR_CSTM_LVDS | SOR_CSTM_LINK_ACT_A | SOR_CSTM_LINK_ACT_B |
Thierry Reding6b6b6042013-11-15 16:06:05 +0100857 SOR_CSTM_UPPER;
858 tegra_sor_writel(sor, value, SOR_CSTM);
859
860 /* PWM setup */
861 err = tegra_sor_setup_pwm(sor, 250);
862 if (err < 0) {
863 dev_err(sor->dev, "failed to setup PWM: %d\n", err);
Thierry Reding86f5c522014-03-26 11:13:16 +0100864 goto unlock;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100865 }
866
867 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
868 value |= SOR_ENABLE;
869 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
870
871 tegra_sor_update(sor);
872
873 err = tegra_sor_attach(sor);
874 if (err < 0) {
875 dev_err(sor->dev, "failed to attach SOR: %d\n", err);
Thierry Reding86f5c522014-03-26 11:13:16 +0100876 goto unlock;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100877 }
878
879 err = tegra_sor_wakeup(sor);
880 if (err < 0) {
881 dev_err(sor->dev, "failed to enable DC: %d\n", err);
Thierry Reding86f5c522014-03-26 11:13:16 +0100882 goto unlock;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100883 }
884
885 sor->enabled = true;
886
Thierry Reding86f5c522014-03-26 11:13:16 +0100887unlock:
888 mutex_unlock(&sor->lock);
889 return err;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100890}
891
892static int tegra_sor_detach(struct tegra_sor *sor)
893{
894 unsigned long value, timeout;
895
896 /* switch to safe mode */
897 value = tegra_sor_readl(sor, SOR_SUPER_STATE_1);
898 value &= ~SOR_SUPER_STATE_MODE_NORMAL;
899 tegra_sor_writel(sor, value, SOR_SUPER_STATE_1);
900 tegra_sor_super_update(sor);
901
902 timeout = jiffies + msecs_to_jiffies(250);
903
904 while (time_before(jiffies, timeout)) {
905 value = tegra_sor_readl(sor, SOR_PWR);
906 if (value & SOR_PWR_MODE_SAFE)
907 break;
908 }
909
910 if ((value & SOR_PWR_MODE_SAFE) == 0)
911 return -ETIMEDOUT;
912
913 /* go to sleep */
914 value = tegra_sor_readl(sor, SOR_SUPER_STATE_1);
915 value &= ~SOR_SUPER_STATE_HEAD_MODE_MASK;
916 tegra_sor_writel(sor, value, SOR_SUPER_STATE_1);
917 tegra_sor_super_update(sor);
918
919 /* detach */
920 value = tegra_sor_readl(sor, SOR_SUPER_STATE_1);
921 value &= ~SOR_SUPER_STATE_ATTACHED;
922 tegra_sor_writel(sor, value, SOR_SUPER_STATE_1);
923 tegra_sor_super_update(sor);
924
925 timeout = jiffies + msecs_to_jiffies(250);
926
927 while (time_before(jiffies, timeout)) {
928 value = tegra_sor_readl(sor, SOR_TEST);
929 if ((value & SOR_TEST_ATTACHED) == 0)
930 break;
931
932 usleep_range(25, 100);
933 }
934
935 if ((value & SOR_TEST_ATTACHED) != 0)
936 return -ETIMEDOUT;
937
938 return 0;
939}
940
941static int tegra_sor_power_down(struct tegra_sor *sor)
942{
943 unsigned long value, timeout;
944 int err;
945
946 value = tegra_sor_readl(sor, SOR_PWR);
947 value &= ~SOR_PWR_NORMAL_STATE_PU;
948 value |= SOR_PWR_TRIGGER;
949 tegra_sor_writel(sor, value, SOR_PWR);
950
951 timeout = jiffies + msecs_to_jiffies(250);
952
953 while (time_before(jiffies, timeout)) {
954 value = tegra_sor_readl(sor, SOR_PWR);
955 if ((value & SOR_PWR_TRIGGER) == 0)
956 return 0;
957
958 usleep_range(25, 100);
959 }
960
961 if ((value & SOR_PWR_TRIGGER) != 0)
962 return -ETIMEDOUT;
963
964 err = clk_set_parent(sor->clk, sor->clk_safe);
965 if (err < 0)
966 dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
967
968 value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
969 value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
970 SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2);
971 tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
972
973 /* stop lane sequencer */
Stéphane Marchesinca185c62014-05-22 20:32:48 -0700974 value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_UP |
Thierry Reding6b6b6042013-11-15 16:06:05 +0100975 SOR_LANE_SEQ_CTL_POWER_STATE_DOWN;
976 tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
977
978 timeout = jiffies + msecs_to_jiffies(250);
979
980 while (time_before(jiffies, timeout)) {
981 value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
982 if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
983 break;
984
985 usleep_range(25, 100);
986 }
987
988 if ((value & SOR_LANE_SEQ_CTL_TRIGGER) != 0)
989 return -ETIMEDOUT;
990
991 value = tegra_sor_readl(sor, SOR_PLL_2);
992 value |= SOR_PLL_2_PORT_POWERDOWN;
993 tegra_sor_writel(sor, value, SOR_PLL_2);
994
995 usleep_range(20, 100);
996
997 value = tegra_sor_readl(sor, SOR_PLL_0);
998 value |= SOR_PLL_0_POWER_OFF;
999 value |= SOR_PLL_0_VCOPD;
1000 tegra_sor_writel(sor, value, SOR_PLL_0);
1001
1002 value = tegra_sor_readl(sor, SOR_PLL_2);
1003 value |= SOR_PLL_2_SEQ_PLLCAPPD;
1004 value |= SOR_PLL_2_SEQ_PLLCAPPD_ENFORCE;
1005 tegra_sor_writel(sor, value, SOR_PLL_2);
1006
1007 usleep_range(20, 100);
1008
1009 return 0;
1010}
1011
1012static int tegra_output_sor_disable(struct tegra_output *output)
1013{
1014 struct tegra_dc *dc = to_tegra_dc(output->encoder.crtc);
1015 struct tegra_sor *sor = to_sor(output);
1016 unsigned long value;
Thierry Reding86f5c522014-03-26 11:13:16 +01001017 int err = 0;
1018
1019 mutex_lock(&sor->lock);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001020
1021 if (!sor->enabled)
Thierry Reding86f5c522014-03-26 11:13:16 +01001022 goto unlock;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001023
1024 err = tegra_sor_detach(sor);
1025 if (err < 0) {
1026 dev_err(sor->dev, "failed to detach SOR: %d\n", err);
Thierry Reding86f5c522014-03-26 11:13:16 +01001027 goto unlock;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001028 }
1029
1030 tegra_sor_writel(sor, 0, SOR_STATE_1);
1031 tegra_sor_update(sor);
1032
1033 /*
1034 * The following accesses registers of the display controller, so make
1035 * sure it's only executed when the output is attached to one.
1036 */
1037 if (dc) {
1038 /*
1039 * XXX: We can't do this here because it causes the SOR to go
1040 * into an erroneous state and the output will look scrambled
1041 * the next time it is enabled. Presumably this is because we
1042 * should be doing this only on the next VBLANK. A possible
1043 * solution would be to queue a "power-off" event to trigger
1044 * this code to be run during the next VBLANK.
1045 */
1046 /*
1047 value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
1048 value &= ~(PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
1049 PW4_ENABLE | PM0_ENABLE | PM1_ENABLE);
1050 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
1051 */
1052
1053 value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
1054 value &= ~DISP_CTRL_MODE_MASK;
1055 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
1056
1057 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
1058 value &= ~SOR_ENABLE;
1059 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
1060
1061 tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
1062 tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
1063 }
1064
1065 err = tegra_sor_power_down(sor);
1066 if (err < 0) {
1067 dev_err(sor->dev, "failed to power down SOR: %d\n", err);
Thierry Reding86f5c522014-03-26 11:13:16 +01001068 goto unlock;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001069 }
1070
1071 if (sor->dpaux) {
1072 err = tegra_dpaux_disable(sor->dpaux);
1073 if (err < 0) {
1074 dev_err(sor->dev, "failed to disable DP: %d\n", err);
Thierry Reding86f5c522014-03-26 11:13:16 +01001075 goto unlock;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001076 }
1077 }
1078
1079 err = tegra_io_rail_power_off(TEGRA_IO_RAIL_LVDS);
1080 if (err < 0) {
1081 dev_err(sor->dev, "failed to power off I/O rail: %d\n", err);
Thierry Reding86f5c522014-03-26 11:13:16 +01001082 goto unlock;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001083 }
1084
1085 reset_control_assert(sor->rst);
1086 clk_disable_unprepare(sor->clk);
1087
1088 sor->enabled = false;
1089
Thierry Reding86f5c522014-03-26 11:13:16 +01001090unlock:
1091 mutex_unlock(&sor->lock);
1092 return err;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001093}
1094
1095static int tegra_output_sor_setup_clock(struct tegra_output *output,
Thierry Reding91eded92014-03-26 13:32:21 +01001096 struct clk *clk, unsigned long pclk,
1097 unsigned int *div)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001098{
1099 struct tegra_sor *sor = to_sor(output);
1100 int err;
1101
Thierry Reding6b6b6042013-11-15 16:06:05 +01001102 err = clk_set_parent(clk, sor->clk_parent);
1103 if (err < 0) {
1104 dev_err(sor->dev, "failed to set parent clock: %d\n", err);
1105 return err;
1106 }
1107
1108 err = clk_set_rate(sor->clk_parent, pclk);
1109 if (err < 0) {
Thierry Reding91eded92014-03-26 13:32:21 +01001110 dev_err(sor->dev, "failed to set clock rate to %lu Hz\n", pclk);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001111 return err;
1112 }
1113
Thierry Reding91eded92014-03-26 13:32:21 +01001114 *div = 0;
1115
Thierry Reding6b6b6042013-11-15 16:06:05 +01001116 return 0;
1117}
1118
1119static int tegra_output_sor_check_mode(struct tegra_output *output,
1120 struct drm_display_mode *mode,
1121 enum drm_mode_status *status)
1122{
1123 /*
1124 * FIXME: For now, always assume that the mode is okay.
1125 */
1126
1127 *status = MODE_OK;
1128
1129 return 0;
1130}
1131
1132static enum drm_connector_status
1133tegra_output_sor_detect(struct tegra_output *output)
1134{
1135 struct tegra_sor *sor = to_sor(output);
1136
1137 if (sor->dpaux)
1138 return tegra_dpaux_detect(sor->dpaux);
1139
1140 return connector_status_unknown;
1141}
1142
1143static const struct tegra_output_ops sor_ops = {
1144 .enable = tegra_output_sor_enable,
1145 .disable = tegra_output_sor_disable,
1146 .setup_clock = tegra_output_sor_setup_clock,
1147 .check_mode = tegra_output_sor_check_mode,
1148 .detect = tegra_output_sor_detect,
1149};
1150
Thierry Redinga82752e2014-01-31 10:02:15 +01001151static int tegra_sor_crc_open(struct inode *inode, struct file *file)
1152{
1153 file->private_data = inode->i_private;
1154
1155 return 0;
1156}
1157
1158static int tegra_sor_crc_release(struct inode *inode, struct file *file)
1159{
1160 return 0;
1161}
1162
1163static int tegra_sor_crc_wait(struct tegra_sor *sor, unsigned long timeout)
1164{
1165 u32 value;
1166
1167 timeout = jiffies + msecs_to_jiffies(timeout);
1168
1169 while (time_before(jiffies, timeout)) {
1170 value = tegra_sor_readl(sor, SOR_CRC_A);
1171 if (value & SOR_CRC_A_VALID)
1172 return 0;
1173
1174 usleep_range(100, 200);
1175 }
1176
1177 return -ETIMEDOUT;
1178}
1179
1180static ssize_t tegra_sor_crc_read(struct file *file, char __user *buffer,
1181 size_t size, loff_t *ppos)
1182{
1183 struct tegra_sor *sor = file->private_data;
Thierry Reding86f5c522014-03-26 11:13:16 +01001184 ssize_t num, err;
Thierry Redinga82752e2014-01-31 10:02:15 +01001185 char buf[10];
Thierry Redinga82752e2014-01-31 10:02:15 +01001186 u32 value;
Thierry Reding86f5c522014-03-26 11:13:16 +01001187
1188 mutex_lock(&sor->lock);
1189
1190 if (!sor->enabled) {
1191 err = -EAGAIN;
1192 goto unlock;
1193 }
Thierry Redinga82752e2014-01-31 10:02:15 +01001194
1195 value = tegra_sor_readl(sor, SOR_STATE_1);
1196 value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
1197 tegra_sor_writel(sor, value, SOR_STATE_1);
1198
1199 value = tegra_sor_readl(sor, SOR_CRC_CNTRL);
1200 value |= SOR_CRC_CNTRL_ENABLE;
1201 tegra_sor_writel(sor, value, SOR_CRC_CNTRL);
1202
1203 value = tegra_sor_readl(sor, SOR_TEST);
1204 value &= ~SOR_TEST_CRC_POST_SERIALIZE;
1205 tegra_sor_writel(sor, value, SOR_TEST);
1206
1207 err = tegra_sor_crc_wait(sor, 100);
1208 if (err < 0)
Thierry Reding86f5c522014-03-26 11:13:16 +01001209 goto unlock;
Thierry Redinga82752e2014-01-31 10:02:15 +01001210
1211 tegra_sor_writel(sor, SOR_CRC_A_RESET, SOR_CRC_A);
1212 value = tegra_sor_readl(sor, SOR_CRC_B);
1213
1214 num = scnprintf(buf, sizeof(buf), "%08x\n", value);
1215
Thierry Reding86f5c522014-03-26 11:13:16 +01001216 err = simple_read_from_buffer(buffer, size, ppos, buf, num);
1217
1218unlock:
1219 mutex_unlock(&sor->lock);
1220 return err;
Thierry Redinga82752e2014-01-31 10:02:15 +01001221}
1222
1223static const struct file_operations tegra_sor_crc_fops = {
1224 .owner = THIS_MODULE,
1225 .open = tegra_sor_crc_open,
1226 .read = tegra_sor_crc_read,
1227 .release = tegra_sor_crc_release,
1228};
1229
Thierry Reding1b0c7b42014-05-28 13:46:12 +02001230static int tegra_sor_debugfs_init(struct tegra_sor *sor,
1231 struct drm_minor *minor)
Thierry Redinga82752e2014-01-31 10:02:15 +01001232{
1233 struct dentry *entry;
1234 int err = 0;
1235
Thierry Reding1b0c7b42014-05-28 13:46:12 +02001236 sor->debugfs = debugfs_create_dir("sor", minor->debugfs_root);
Thierry Redinga82752e2014-01-31 10:02:15 +01001237 if (!sor->debugfs)
1238 return -ENOMEM;
1239
1240 entry = debugfs_create_file("crc", 0644, sor->debugfs, sor,
1241 &tegra_sor_crc_fops);
1242 if (!entry) {
1243 dev_err(sor->dev,
1244 "cannot create /sys/kernel/debug/dri/%s/sor/crc\n",
Thierry Reding1b0c7b42014-05-28 13:46:12 +02001245 minor->debugfs_root->d_name.name);
Thierry Redinga82752e2014-01-31 10:02:15 +01001246 err = -ENOMEM;
1247 goto remove;
1248 }
1249
1250 return err;
1251
1252remove:
1253 debugfs_remove(sor->debugfs);
1254 sor->debugfs = NULL;
1255 return err;
1256}
1257
1258static int tegra_sor_debugfs_exit(struct tegra_sor *sor)
1259{
Thierry Reding95781842014-04-25 16:48:36 +02001260 debugfs_remove_recursive(sor->debugfs);
Thierry Redinga82752e2014-01-31 10:02:15 +01001261 sor->debugfs = NULL;
1262
1263 return 0;
1264}
1265
Thierry Reding6b6b6042013-11-15 16:06:05 +01001266static int tegra_sor_init(struct host1x_client *client)
1267{
Thierry Reding9910f5c2014-05-22 09:57:15 +02001268 struct drm_device *drm = dev_get_drvdata(client->parent);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001269 struct tegra_sor *sor = host1x_client_to_sor(client);
1270 int err;
1271
1272 if (!sor->dpaux)
1273 return -ENODEV;
1274
1275 sor->output.type = TEGRA_OUTPUT_EDP;
1276
1277 sor->output.dev = sor->dev;
1278 sor->output.ops = &sor_ops;
1279
Thierry Reding9910f5c2014-05-22 09:57:15 +02001280 err = tegra_output_init(drm, &sor->output);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001281 if (err < 0) {
1282 dev_err(sor->dev, "output setup failed: %d\n", err);
1283 return err;
1284 }
1285
Thierry Redinga82752e2014-01-31 10:02:15 +01001286 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
Thierry Reding1b0c7b42014-05-28 13:46:12 +02001287 err = tegra_sor_debugfs_init(sor, drm->primary);
Thierry Redinga82752e2014-01-31 10:02:15 +01001288 if (err < 0)
1289 dev_err(sor->dev, "debugfs setup failed: %d\n", err);
1290 }
1291
Thierry Reding6b6b6042013-11-15 16:06:05 +01001292 if (sor->dpaux) {
1293 err = tegra_dpaux_attach(sor->dpaux, &sor->output);
1294 if (err < 0) {
1295 dev_err(sor->dev, "failed to attach DP: %d\n", err);
1296 return err;
1297 }
1298 }
1299
1300 return 0;
1301}
1302
1303static int tegra_sor_exit(struct host1x_client *client)
1304{
1305 struct tegra_sor *sor = host1x_client_to_sor(client);
1306 int err;
1307
1308 err = tegra_output_disable(&sor->output);
1309 if (err < 0) {
1310 dev_err(sor->dev, "output failed to disable: %d\n", err);
1311 return err;
1312 }
1313
1314 if (sor->dpaux) {
1315 err = tegra_dpaux_detach(sor->dpaux);
1316 if (err < 0) {
1317 dev_err(sor->dev, "failed to detach DP: %d\n", err);
1318 return err;
1319 }
1320 }
1321
Thierry Redinga82752e2014-01-31 10:02:15 +01001322 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
1323 err = tegra_sor_debugfs_exit(sor);
1324 if (err < 0)
1325 dev_err(sor->dev, "debugfs cleanup failed: %d\n", err);
1326 }
1327
Thierry Reding6b6b6042013-11-15 16:06:05 +01001328 err = tegra_output_exit(&sor->output);
1329 if (err < 0) {
1330 dev_err(sor->dev, "output cleanup failed: %d\n", err);
1331 return err;
1332 }
1333
1334 return 0;
1335}
1336
1337static const struct host1x_client_ops sor_client_ops = {
1338 .init = tegra_sor_init,
1339 .exit = tegra_sor_exit,
1340};
1341
1342static int tegra_sor_probe(struct platform_device *pdev)
1343{
1344 struct device_node *np;
1345 struct tegra_sor *sor;
1346 struct resource *regs;
1347 int err;
1348
1349 sor = devm_kzalloc(&pdev->dev, sizeof(*sor), GFP_KERNEL);
1350 if (!sor)
1351 return -ENOMEM;
1352
1353 sor->output.dev = sor->dev = &pdev->dev;
1354
1355 np = of_parse_phandle(pdev->dev.of_node, "nvidia,dpaux", 0);
1356 if (np) {
1357 sor->dpaux = tegra_dpaux_find_by_of_node(np);
1358 of_node_put(np);
1359
1360 if (!sor->dpaux)
1361 return -EPROBE_DEFER;
1362 }
1363
1364 err = tegra_output_probe(&sor->output);
1365 if (err < 0)
1366 return err;
1367
1368 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1369 sor->regs = devm_ioremap_resource(&pdev->dev, regs);
1370 if (IS_ERR(sor->regs))
1371 return PTR_ERR(sor->regs);
1372
1373 sor->rst = devm_reset_control_get(&pdev->dev, "sor");
1374 if (IS_ERR(sor->rst))
1375 return PTR_ERR(sor->rst);
1376
1377 sor->clk = devm_clk_get(&pdev->dev, NULL);
1378 if (IS_ERR(sor->clk))
1379 return PTR_ERR(sor->clk);
1380
1381 sor->clk_parent = devm_clk_get(&pdev->dev, "parent");
1382 if (IS_ERR(sor->clk_parent))
1383 return PTR_ERR(sor->clk_parent);
1384
1385 err = clk_prepare_enable(sor->clk_parent);
1386 if (err < 0)
1387 return err;
1388
1389 sor->clk_safe = devm_clk_get(&pdev->dev, "safe");
1390 if (IS_ERR(sor->clk_safe))
1391 return PTR_ERR(sor->clk_safe);
1392
1393 err = clk_prepare_enable(sor->clk_safe);
1394 if (err < 0)
1395 return err;
1396
1397 sor->clk_dp = devm_clk_get(&pdev->dev, "dp");
1398 if (IS_ERR(sor->clk_dp))
1399 return PTR_ERR(sor->clk_dp);
1400
1401 err = clk_prepare_enable(sor->clk_dp);
1402 if (err < 0)
1403 return err;
1404
1405 INIT_LIST_HEAD(&sor->client.list);
1406 sor->client.ops = &sor_client_ops;
1407 sor->client.dev = &pdev->dev;
1408
Thierry Reding86f5c522014-03-26 11:13:16 +01001409 mutex_init(&sor->lock);
1410
Thierry Reding6b6b6042013-11-15 16:06:05 +01001411 err = host1x_client_register(&sor->client);
1412 if (err < 0) {
1413 dev_err(&pdev->dev, "failed to register host1x client: %d\n",
1414 err);
1415 return err;
1416 }
1417
1418 platform_set_drvdata(pdev, sor);
1419
1420 return 0;
1421}
1422
1423static int tegra_sor_remove(struct platform_device *pdev)
1424{
1425 struct tegra_sor *sor = platform_get_drvdata(pdev);
1426 int err;
1427
1428 err = host1x_client_unregister(&sor->client);
1429 if (err < 0) {
1430 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
1431 err);
1432 return err;
1433 }
1434
1435 clk_disable_unprepare(sor->clk_parent);
1436 clk_disable_unprepare(sor->clk_safe);
1437 clk_disable_unprepare(sor->clk_dp);
1438 clk_disable_unprepare(sor->clk);
1439
1440 return 0;
1441}
1442
1443static const struct of_device_id tegra_sor_of_match[] = {
1444 { .compatible = "nvidia,tegra124-sor", },
1445 { },
1446};
1447
1448struct platform_driver tegra_sor_driver = {
1449 .driver = {
1450 .name = "tegra-sor",
1451 .of_match_table = tegra_sor_of_match,
1452 },
1453 .probe = tegra_sor_probe,
1454 .remove = tegra_sor_remove,
1455};