blob: 9bc2cb701c04250d92cc95ee0d2d3ea5e159c665 [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
Thierry Redingdab16332015-01-26 16:04:08 +010044 struct drm_info_list *debugfs_files;
45 struct drm_minor *minor;
Thierry Redinga82752e2014-01-31 10:02:15 +010046 struct dentry *debugfs;
Thierry Reding6b6b6042013-11-15 16:06:05 +010047};
48
Thierry Reding34fa1832014-06-05 16:31:10 +020049struct tegra_sor_config {
50 u32 bits_per_pixel;
51
52 u32 active_polarity;
53 u32 active_count;
54 u32 tu_size;
55 u32 active_frac;
56 u32 watermark;
Thierry Reding7890b572014-06-05 16:12:46 +020057
58 u32 hblank_symbols;
59 u32 vblank_symbols;
Thierry Reding34fa1832014-06-05 16:31:10 +020060};
61
Thierry Reding6b6b6042013-11-15 16:06:05 +010062static inline struct tegra_sor *
63host1x_client_to_sor(struct host1x_client *client)
64{
65 return container_of(client, struct tegra_sor, client);
66}
67
68static inline struct tegra_sor *to_sor(struct tegra_output *output)
69{
70 return container_of(output, struct tegra_sor, output);
71}
72
Thierry Reding28fe2072015-01-26 16:02:48 +010073static inline u32 tegra_sor_readl(struct tegra_sor *sor, unsigned long offset)
Thierry Reding6b6b6042013-11-15 16:06:05 +010074{
75 return readl(sor->regs + (offset << 2));
76}
77
Thierry Reding28fe2072015-01-26 16:02:48 +010078static inline void tegra_sor_writel(struct tegra_sor *sor, u32 value,
Thierry Reding6b6b6042013-11-15 16:06:05 +010079 unsigned long offset)
80{
81 writel(value, sor->regs + (offset << 2));
82}
83
84static int tegra_sor_dp_train_fast(struct tegra_sor *sor,
85 struct drm_dp_link *link)
86{
Thierry Reding6b6b6042013-11-15 16:06:05 +010087 unsigned int i;
88 u8 pattern;
Thierry Reding28fe2072015-01-26 16:02:48 +010089 u32 value;
Thierry Reding6b6b6042013-11-15 16:06:05 +010090 int err;
91
92 /* setup lane parameters */
93 value = SOR_LANE_DRIVE_CURRENT_LANE3(0x40) |
94 SOR_LANE_DRIVE_CURRENT_LANE2(0x40) |
95 SOR_LANE_DRIVE_CURRENT_LANE1(0x40) |
96 SOR_LANE_DRIVE_CURRENT_LANE0(0x40);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +020097 tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT0);
Thierry Reding6b6b6042013-11-15 16:06:05 +010098
99 value = SOR_LANE_PREEMPHASIS_LANE3(0x0f) |
100 SOR_LANE_PREEMPHASIS_LANE2(0x0f) |
101 SOR_LANE_PREEMPHASIS_LANE1(0x0f) |
102 SOR_LANE_PREEMPHASIS_LANE0(0x0f);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200103 tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100104
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200105 value = SOR_LANE_POSTCURSOR_LANE3(0x00) |
106 SOR_LANE_POSTCURSOR_LANE2(0x00) |
107 SOR_LANE_POSTCURSOR_LANE1(0x00) |
108 SOR_LANE_POSTCURSOR_LANE0(0x00);
109 tegra_sor_writel(sor, value, SOR_LANE_POSTCURSOR0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100110
111 /* disable LVDS mode */
112 tegra_sor_writel(sor, 0, SOR_LVDS);
113
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200114 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100115 value |= SOR_DP_PADCTL_TX_PU_ENABLE;
116 value &= ~SOR_DP_PADCTL_TX_PU_MASK;
117 value |= SOR_DP_PADCTL_TX_PU(2); /* XXX: don't hardcode? */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200118 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100119
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200120 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100121 value |= SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
122 SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200123 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100124
125 usleep_range(10, 100);
126
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200127 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100128 value &= ~(SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
129 SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200130 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100131
132 err = tegra_dpaux_prepare(sor->dpaux, DP_SET_ANSI_8B10B);
133 if (err < 0)
134 return err;
135
136 for (i = 0, value = 0; i < link->num_lanes; i++) {
137 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
138 SOR_DP_TPG_SCRAMBLER_NONE |
139 SOR_DP_TPG_PATTERN_TRAIN1;
140 value = (value << 8) | lane;
141 }
142
143 tegra_sor_writel(sor, value, SOR_DP_TPG);
144
145 pattern = DP_TRAINING_PATTERN_1;
146
147 err = tegra_dpaux_train(sor->dpaux, link, pattern);
148 if (err < 0)
149 return err;
150
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200151 value = tegra_sor_readl(sor, SOR_DP_SPARE0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100152 value |= SOR_DP_SPARE_SEQ_ENABLE;
153 value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
154 value |= SOR_DP_SPARE_MACRO_SOR_CLK;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200155 tegra_sor_writel(sor, value, SOR_DP_SPARE0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100156
157 for (i = 0, value = 0; i < link->num_lanes; i++) {
158 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
159 SOR_DP_TPG_SCRAMBLER_NONE |
160 SOR_DP_TPG_PATTERN_TRAIN2;
161 value = (value << 8) | lane;
162 }
163
164 tegra_sor_writel(sor, value, SOR_DP_TPG);
165
166 pattern = DP_LINK_SCRAMBLING_DISABLE | DP_TRAINING_PATTERN_2;
167
168 err = tegra_dpaux_train(sor->dpaux, link, pattern);
169 if (err < 0)
170 return err;
171
172 for (i = 0, value = 0; i < link->num_lanes; i++) {
173 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
174 SOR_DP_TPG_SCRAMBLER_GALIOS |
175 SOR_DP_TPG_PATTERN_NONE;
176 value = (value << 8) | lane;
177 }
178
179 tegra_sor_writel(sor, value, SOR_DP_TPG);
180
181 pattern = DP_TRAINING_PATTERN_DISABLE;
182
183 err = tegra_dpaux_train(sor->dpaux, link, pattern);
184 if (err < 0)
185 return err;
186
187 return 0;
188}
189
190static void tegra_sor_super_update(struct tegra_sor *sor)
191{
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200192 tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
193 tegra_sor_writel(sor, 1, SOR_SUPER_STATE0);
194 tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100195}
196
197static void tegra_sor_update(struct tegra_sor *sor)
198{
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200199 tegra_sor_writel(sor, 0, SOR_STATE0);
200 tegra_sor_writel(sor, 1, SOR_STATE0);
201 tegra_sor_writel(sor, 0, SOR_STATE0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100202}
203
204static int tegra_sor_setup_pwm(struct tegra_sor *sor, unsigned long timeout)
205{
Thierry Reding28fe2072015-01-26 16:02:48 +0100206 u32 value;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100207
208 value = tegra_sor_readl(sor, SOR_PWM_DIV);
209 value &= ~SOR_PWM_DIV_MASK;
210 value |= 0x400; /* period */
211 tegra_sor_writel(sor, value, SOR_PWM_DIV);
212
213 value = tegra_sor_readl(sor, SOR_PWM_CTL);
214 value &= ~SOR_PWM_CTL_DUTY_CYCLE_MASK;
215 value |= 0x400; /* duty cycle */
216 value &= ~SOR_PWM_CTL_CLK_SEL; /* clock source: PCLK */
217 value |= SOR_PWM_CTL_TRIGGER;
218 tegra_sor_writel(sor, value, SOR_PWM_CTL);
219
220 timeout = jiffies + msecs_to_jiffies(timeout);
221
222 while (time_before(jiffies, timeout)) {
223 value = tegra_sor_readl(sor, SOR_PWM_CTL);
224 if ((value & SOR_PWM_CTL_TRIGGER) == 0)
225 return 0;
226
227 usleep_range(25, 100);
228 }
229
230 return -ETIMEDOUT;
231}
232
233static int tegra_sor_attach(struct tegra_sor *sor)
234{
235 unsigned long value, timeout;
236
237 /* wake up in normal mode */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200238 value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100239 value |= SOR_SUPER_STATE_HEAD_MODE_AWAKE;
240 value |= SOR_SUPER_STATE_MODE_NORMAL;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200241 tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100242 tegra_sor_super_update(sor);
243
244 /* attach */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200245 value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100246 value |= SOR_SUPER_STATE_ATTACHED;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200247 tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100248 tegra_sor_super_update(sor);
249
250 timeout = jiffies + msecs_to_jiffies(250);
251
252 while (time_before(jiffies, timeout)) {
253 value = tegra_sor_readl(sor, SOR_TEST);
254 if ((value & SOR_TEST_ATTACHED) != 0)
255 return 0;
256
257 usleep_range(25, 100);
258 }
259
260 return -ETIMEDOUT;
261}
262
263static int tegra_sor_wakeup(struct tegra_sor *sor)
264{
Thierry Reding6b6b6042013-11-15 16:06:05 +0100265 unsigned long value, timeout;
266
Thierry Reding6b6b6042013-11-15 16:06:05 +0100267 timeout = jiffies + msecs_to_jiffies(250);
268
269 /* wait for head to wake up */
270 while (time_before(jiffies, timeout)) {
271 value = tegra_sor_readl(sor, SOR_TEST);
272 value &= SOR_TEST_HEAD_MODE_MASK;
273
274 if (value == SOR_TEST_HEAD_MODE_AWAKE)
275 return 0;
276
277 usleep_range(25, 100);
278 }
279
280 return -ETIMEDOUT;
281}
282
283static int tegra_sor_power_up(struct tegra_sor *sor, unsigned long timeout)
284{
Thierry Reding28fe2072015-01-26 16:02:48 +0100285 u32 value;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100286
287 value = tegra_sor_readl(sor, SOR_PWR);
288 value |= SOR_PWR_TRIGGER | SOR_PWR_NORMAL_STATE_PU;
289 tegra_sor_writel(sor, value, SOR_PWR);
290
291 timeout = jiffies + msecs_to_jiffies(timeout);
292
293 while (time_before(jiffies, timeout)) {
294 value = tegra_sor_readl(sor, SOR_PWR);
295 if ((value & SOR_PWR_TRIGGER) == 0)
296 return 0;
297
298 usleep_range(25, 100);
299 }
300
301 return -ETIMEDOUT;
302}
303
Thierry Reding34fa1832014-06-05 16:31:10 +0200304struct tegra_sor_params {
305 /* number of link clocks per line */
306 unsigned int num_clocks;
307 /* ratio between input and output */
308 u64 ratio;
309 /* precision factor */
310 u64 precision;
311
312 unsigned int active_polarity;
313 unsigned int active_count;
314 unsigned int active_frac;
315 unsigned int tu_size;
316 unsigned int error;
317};
318
319static int tegra_sor_compute_params(struct tegra_sor *sor,
320 struct tegra_sor_params *params,
321 unsigned int tu_size)
322{
323 u64 active_sym, active_count, frac, approx;
324 u32 active_polarity, active_frac = 0;
325 const u64 f = params->precision;
326 s64 error;
327
328 active_sym = params->ratio * tu_size;
329 active_count = div_u64(active_sym, f) * f;
330 frac = active_sym - active_count;
331
332 /* fraction < 0.5 */
333 if (frac >= (f / 2)) {
334 active_polarity = 1;
335 frac = f - frac;
336 } else {
337 active_polarity = 0;
338 }
339
340 if (frac != 0) {
341 frac = div_u64(f * f, frac); /* 1/fraction */
342 if (frac <= (15 * f)) {
343 active_frac = div_u64(frac, f);
344
345 /* round up */
346 if (active_polarity)
347 active_frac++;
348 } else {
349 active_frac = active_polarity ? 1 : 15;
350 }
351 }
352
353 if (active_frac == 1)
354 active_polarity = 0;
355
356 if (active_polarity == 1) {
357 if (active_frac) {
358 approx = active_count + (active_frac * (f - 1)) * f;
359 approx = div_u64(approx, active_frac * f);
360 } else {
361 approx = active_count + f;
362 }
363 } else {
364 if (active_frac)
365 approx = active_count + div_u64(f, active_frac);
366 else
367 approx = active_count;
368 }
369
370 error = div_s64(active_sym - approx, tu_size);
371 error *= params->num_clocks;
372
373 if (error <= 0 && abs64(error) < params->error) {
374 params->active_count = div_u64(active_count, f);
375 params->active_polarity = active_polarity;
376 params->active_frac = active_frac;
377 params->error = abs64(error);
378 params->tu_size = tu_size;
379
380 if (error == 0)
381 return true;
382 }
383
384 return false;
385}
386
387static int tegra_sor_calc_config(struct tegra_sor *sor,
Thierry Reding80444492015-07-29 18:20:01 +0200388 const struct drm_display_mode *mode,
Thierry Reding34fa1832014-06-05 16:31:10 +0200389 struct tegra_sor_config *config,
390 struct drm_dp_link *link)
391{
392 const u64 f = 100000, link_rate = link->rate * 1000;
393 const u64 pclk = mode->clock * 1000;
Thierry Reding7890b572014-06-05 16:12:46 +0200394 u64 input, output, watermark, num;
Thierry Reding34fa1832014-06-05 16:31:10 +0200395 struct tegra_sor_params params;
Thierry Reding34fa1832014-06-05 16:31:10 +0200396 u32 num_syms_per_line;
397 unsigned int i;
398
399 if (!link_rate || !link->num_lanes || !pclk || !config->bits_per_pixel)
400 return -EINVAL;
401
402 output = link_rate * 8 * link->num_lanes;
403 input = pclk * config->bits_per_pixel;
404
405 if (input >= output)
406 return -ERANGE;
407
408 memset(&params, 0, sizeof(params));
409 params.ratio = div64_u64(input * f, output);
410 params.num_clocks = div_u64(link_rate * mode->hdisplay, pclk);
411 params.precision = f;
412 params.error = 64 * f;
413 params.tu_size = 64;
414
415 for (i = params.tu_size; i >= 32; i--)
416 if (tegra_sor_compute_params(sor, &params, i))
417 break;
418
419 if (params.active_frac == 0) {
420 config->active_polarity = 0;
421 config->active_count = params.active_count;
422
423 if (!params.active_polarity)
424 config->active_count--;
425
426 config->tu_size = params.tu_size;
427 config->active_frac = 1;
428 } else {
429 config->active_polarity = params.active_polarity;
430 config->active_count = params.active_count;
431 config->active_frac = params.active_frac;
432 config->tu_size = params.tu_size;
433 }
434
435 dev_dbg(sor->dev,
436 "polarity: %d active count: %d tu size: %d active frac: %d\n",
437 config->active_polarity, config->active_count,
438 config->tu_size, config->active_frac);
439
440 watermark = params.ratio * config->tu_size * (f - params.ratio);
441 watermark = div_u64(watermark, f);
442
443 watermark = div_u64(watermark + params.error, f);
444 config->watermark = watermark + (config->bits_per_pixel / 8) + 2;
445 num_syms_per_line = (mode->hdisplay * config->bits_per_pixel) *
446 (link->num_lanes * 8);
447
448 if (config->watermark > 30) {
449 config->watermark = 30;
450 dev_err(sor->dev,
451 "unable to compute TU size, forcing watermark to %u\n",
452 config->watermark);
453 } else if (config->watermark > num_syms_per_line) {
454 config->watermark = num_syms_per_line;
455 dev_err(sor->dev, "watermark too high, forcing to %u\n",
456 config->watermark);
457 }
458
Thierry Reding7890b572014-06-05 16:12:46 +0200459 /* compute the number of symbols per horizontal blanking interval */
460 num = ((mode->htotal - mode->hdisplay) - 7) * link_rate;
461 config->hblank_symbols = div_u64(num, pclk);
462
463 if (link->capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
464 config->hblank_symbols -= 3;
465
466 config->hblank_symbols -= 12 / link->num_lanes;
467
468 /* compute the number of symbols per vertical blanking interval */
469 num = (mode->hdisplay - 25) * link_rate;
470 config->vblank_symbols = div_u64(num, pclk);
471 config->vblank_symbols -= 36 / link->num_lanes + 4;
472
473 dev_dbg(sor->dev, "blank symbols: H:%u V:%u\n", config->hblank_symbols,
474 config->vblank_symbols);
475
Thierry Reding34fa1832014-06-05 16:31:10 +0200476 return 0;
477}
478
Thierry Reding6fad8f62014-11-28 15:41:34 +0100479static int tegra_sor_detach(struct tegra_sor *sor)
Thierry Reding6b6b6042013-11-15 16:06:05 +0100480{
Thierry Reding6fad8f62014-11-28 15:41:34 +0100481 unsigned long value, timeout;
482
483 /* switch to safe mode */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200484 value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100485 value &= ~SOR_SUPER_STATE_MODE_NORMAL;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200486 tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100487 tegra_sor_super_update(sor);
488
489 timeout = jiffies + msecs_to_jiffies(250);
490
491 while (time_before(jiffies, timeout)) {
492 value = tegra_sor_readl(sor, SOR_PWR);
493 if (value & SOR_PWR_MODE_SAFE)
494 break;
495 }
496
497 if ((value & SOR_PWR_MODE_SAFE) == 0)
498 return -ETIMEDOUT;
499
500 /* go to sleep */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200501 value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100502 value &= ~SOR_SUPER_STATE_HEAD_MODE_MASK;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200503 tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100504 tegra_sor_super_update(sor);
505
506 /* detach */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200507 value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100508 value &= ~SOR_SUPER_STATE_ATTACHED;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200509 tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100510 tegra_sor_super_update(sor);
511
512 timeout = jiffies + msecs_to_jiffies(250);
513
514 while (time_before(jiffies, timeout)) {
515 value = tegra_sor_readl(sor, SOR_TEST);
516 if ((value & SOR_TEST_ATTACHED) == 0)
517 break;
518
519 usleep_range(25, 100);
520 }
521
522 if ((value & SOR_TEST_ATTACHED) != 0)
523 return -ETIMEDOUT;
524
525 return 0;
526}
527
528static int tegra_sor_power_down(struct tegra_sor *sor)
529{
530 unsigned long value, timeout;
531 int err;
532
533 value = tegra_sor_readl(sor, SOR_PWR);
534 value &= ~SOR_PWR_NORMAL_STATE_PU;
535 value |= SOR_PWR_TRIGGER;
536 tegra_sor_writel(sor, value, SOR_PWR);
537
538 timeout = jiffies + msecs_to_jiffies(250);
539
540 while (time_before(jiffies, timeout)) {
541 value = tegra_sor_readl(sor, SOR_PWR);
542 if ((value & SOR_PWR_TRIGGER) == 0)
543 return 0;
544
545 usleep_range(25, 100);
546 }
547
548 if ((value & SOR_PWR_TRIGGER) != 0)
549 return -ETIMEDOUT;
550
551 err = clk_set_parent(sor->clk, sor->clk_safe);
552 if (err < 0)
553 dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
554
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200555 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100556 value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
557 SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200558 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100559
560 /* stop lane sequencer */
561 value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_UP |
562 SOR_LANE_SEQ_CTL_POWER_STATE_DOWN;
563 tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
564
565 timeout = jiffies + msecs_to_jiffies(250);
566
567 while (time_before(jiffies, timeout)) {
568 value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
569 if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
570 break;
571
572 usleep_range(25, 100);
573 }
574
575 if ((value & SOR_LANE_SEQ_CTL_TRIGGER) != 0)
576 return -ETIMEDOUT;
577
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200578 value = tegra_sor_readl(sor, SOR_PLL2);
579 value |= SOR_PLL2_PORT_POWERDOWN;
580 tegra_sor_writel(sor, value, SOR_PLL2);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100581
582 usleep_range(20, 100);
583
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200584 value = tegra_sor_readl(sor, SOR_PLL0);
585 value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
586 tegra_sor_writel(sor, value, SOR_PLL0);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100587
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200588 value = tegra_sor_readl(sor, SOR_PLL2);
589 value |= SOR_PLL2_SEQ_PLLCAPPD;
590 value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
591 tegra_sor_writel(sor, value, SOR_PLL2);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100592
593 usleep_range(20, 100);
594
595 return 0;
596}
597
Thierry Reding6fad8f62014-11-28 15:41:34 +0100598static int tegra_sor_crc_wait(struct tegra_sor *sor, unsigned long timeout)
599{
600 u32 value;
601
602 timeout = jiffies + msecs_to_jiffies(timeout);
603
604 while (time_before(jiffies, timeout)) {
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200605 value = tegra_sor_readl(sor, SOR_CRCA);
606 if (value & SOR_CRCA_VALID)
Thierry Reding6fad8f62014-11-28 15:41:34 +0100607 return 0;
608
609 usleep_range(100, 200);
610 }
611
612 return -ETIMEDOUT;
613}
614
Thierry Reding530239a2015-08-06 11:04:54 +0200615static int tegra_sor_show_crc(struct seq_file *s, void *data)
Thierry Reding6fad8f62014-11-28 15:41:34 +0100616{
Thierry Reding530239a2015-08-06 11:04:54 +0200617 struct drm_info_node *node = s->private;
618 struct tegra_sor *sor = node->info_ent->data;
619 int err = 0;
Thierry Reding6fad8f62014-11-28 15:41:34 +0100620 u32 value;
621
622 mutex_lock(&sor->lock);
623
624 if (!sor->enabled) {
625 err = -EAGAIN;
626 goto unlock;
627 }
628
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200629 value = tegra_sor_readl(sor, SOR_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100630 value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200631 tegra_sor_writel(sor, value, SOR_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100632
633 value = tegra_sor_readl(sor, SOR_CRC_CNTRL);
634 value |= SOR_CRC_CNTRL_ENABLE;
635 tegra_sor_writel(sor, value, SOR_CRC_CNTRL);
636
637 value = tegra_sor_readl(sor, SOR_TEST);
638 value &= ~SOR_TEST_CRC_POST_SERIALIZE;
639 tegra_sor_writel(sor, value, SOR_TEST);
640
641 err = tegra_sor_crc_wait(sor, 100);
642 if (err < 0)
643 goto unlock;
644
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200645 tegra_sor_writel(sor, SOR_CRCA_RESET, SOR_CRCA);
646 value = tegra_sor_readl(sor, SOR_CRCB);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100647
Thierry Reding530239a2015-08-06 11:04:54 +0200648 seq_printf(s, "%08x\n", value);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100649
650unlock:
651 mutex_unlock(&sor->lock);
652 return err;
653}
654
Thierry Redingdab16332015-01-26 16:04:08 +0100655static int tegra_sor_show_regs(struct seq_file *s, void *data)
656{
657 struct drm_info_node *node = s->private;
658 struct tegra_sor *sor = node->info_ent->data;
659
660#define DUMP_REG(name) \
661 seq_printf(s, "%-38s %#05x %08x\n", #name, name, \
662 tegra_sor_readl(sor, name))
663
664 DUMP_REG(SOR_CTXSW);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200665 DUMP_REG(SOR_SUPER_STATE0);
666 DUMP_REG(SOR_SUPER_STATE1);
667 DUMP_REG(SOR_STATE0);
668 DUMP_REG(SOR_STATE1);
669 DUMP_REG(SOR_HEAD_STATE0(0));
670 DUMP_REG(SOR_HEAD_STATE0(1));
671 DUMP_REG(SOR_HEAD_STATE1(0));
672 DUMP_REG(SOR_HEAD_STATE1(1));
673 DUMP_REG(SOR_HEAD_STATE2(0));
674 DUMP_REG(SOR_HEAD_STATE2(1));
675 DUMP_REG(SOR_HEAD_STATE3(0));
676 DUMP_REG(SOR_HEAD_STATE3(1));
677 DUMP_REG(SOR_HEAD_STATE4(0));
678 DUMP_REG(SOR_HEAD_STATE4(1));
679 DUMP_REG(SOR_HEAD_STATE5(0));
680 DUMP_REG(SOR_HEAD_STATE5(1));
Thierry Redingdab16332015-01-26 16:04:08 +0100681 DUMP_REG(SOR_CRC_CNTRL);
682 DUMP_REG(SOR_DP_DEBUG_MVID);
683 DUMP_REG(SOR_CLK_CNTRL);
684 DUMP_REG(SOR_CAP);
685 DUMP_REG(SOR_PWR);
686 DUMP_REG(SOR_TEST);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200687 DUMP_REG(SOR_PLL0);
688 DUMP_REG(SOR_PLL1);
689 DUMP_REG(SOR_PLL2);
690 DUMP_REG(SOR_PLL3);
Thierry Redingdab16332015-01-26 16:04:08 +0100691 DUMP_REG(SOR_CSTM);
692 DUMP_REG(SOR_LVDS);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200693 DUMP_REG(SOR_CRCA);
694 DUMP_REG(SOR_CRCB);
Thierry Redingdab16332015-01-26 16:04:08 +0100695 DUMP_REG(SOR_BLANK);
696 DUMP_REG(SOR_SEQ_CTL);
697 DUMP_REG(SOR_LANE_SEQ_CTL);
698 DUMP_REG(SOR_SEQ_INST(0));
699 DUMP_REG(SOR_SEQ_INST(1));
700 DUMP_REG(SOR_SEQ_INST(2));
701 DUMP_REG(SOR_SEQ_INST(3));
702 DUMP_REG(SOR_SEQ_INST(4));
703 DUMP_REG(SOR_SEQ_INST(5));
704 DUMP_REG(SOR_SEQ_INST(6));
705 DUMP_REG(SOR_SEQ_INST(7));
706 DUMP_REG(SOR_SEQ_INST(8));
707 DUMP_REG(SOR_SEQ_INST(9));
708 DUMP_REG(SOR_SEQ_INST(10));
709 DUMP_REG(SOR_SEQ_INST(11));
710 DUMP_REG(SOR_SEQ_INST(12));
711 DUMP_REG(SOR_SEQ_INST(13));
712 DUMP_REG(SOR_SEQ_INST(14));
713 DUMP_REG(SOR_SEQ_INST(15));
714 DUMP_REG(SOR_PWM_DIV);
715 DUMP_REG(SOR_PWM_CTL);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200716 DUMP_REG(SOR_VCRC_A0);
717 DUMP_REG(SOR_VCRC_A1);
718 DUMP_REG(SOR_VCRC_B0);
719 DUMP_REG(SOR_VCRC_B1);
720 DUMP_REG(SOR_CCRC_A0);
721 DUMP_REG(SOR_CCRC_A1);
722 DUMP_REG(SOR_CCRC_B0);
723 DUMP_REG(SOR_CCRC_B1);
724 DUMP_REG(SOR_EDATA_A0);
725 DUMP_REG(SOR_EDATA_A1);
726 DUMP_REG(SOR_EDATA_B0);
727 DUMP_REG(SOR_EDATA_B1);
728 DUMP_REG(SOR_COUNT_A0);
729 DUMP_REG(SOR_COUNT_A1);
730 DUMP_REG(SOR_COUNT_B0);
731 DUMP_REG(SOR_COUNT_B1);
732 DUMP_REG(SOR_DEBUG_A0);
733 DUMP_REG(SOR_DEBUG_A1);
734 DUMP_REG(SOR_DEBUG_B0);
735 DUMP_REG(SOR_DEBUG_B1);
Thierry Redingdab16332015-01-26 16:04:08 +0100736 DUMP_REG(SOR_TRIG);
737 DUMP_REG(SOR_MSCHECK);
738 DUMP_REG(SOR_XBAR_CTRL);
739 DUMP_REG(SOR_XBAR_POL);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200740 DUMP_REG(SOR_DP_LINKCTL0);
741 DUMP_REG(SOR_DP_LINKCTL1);
742 DUMP_REG(SOR_LANE_DRIVE_CURRENT0);
743 DUMP_REG(SOR_LANE_DRIVE_CURRENT1);
744 DUMP_REG(SOR_LANE4_DRIVE_CURRENT0);
745 DUMP_REG(SOR_LANE4_DRIVE_CURRENT1);
746 DUMP_REG(SOR_LANE_PREEMPHASIS0);
747 DUMP_REG(SOR_LANE_PREEMPHASIS1);
748 DUMP_REG(SOR_LANE4_PREEMPHASIS0);
749 DUMP_REG(SOR_LANE4_PREEMPHASIS1);
750 DUMP_REG(SOR_LANE_POSTCURSOR0);
751 DUMP_REG(SOR_LANE_POSTCURSOR1);
752 DUMP_REG(SOR_DP_CONFIG0);
753 DUMP_REG(SOR_DP_CONFIG1);
754 DUMP_REG(SOR_DP_MN0);
755 DUMP_REG(SOR_DP_MN1);
756 DUMP_REG(SOR_DP_PADCTL0);
757 DUMP_REG(SOR_DP_PADCTL1);
758 DUMP_REG(SOR_DP_DEBUG0);
759 DUMP_REG(SOR_DP_DEBUG1);
760 DUMP_REG(SOR_DP_SPARE0);
761 DUMP_REG(SOR_DP_SPARE1);
Thierry Redingdab16332015-01-26 16:04:08 +0100762 DUMP_REG(SOR_DP_AUDIO_CTRL);
763 DUMP_REG(SOR_DP_AUDIO_HBLANK_SYMBOLS);
764 DUMP_REG(SOR_DP_AUDIO_VBLANK_SYMBOLS);
765 DUMP_REG(SOR_DP_GENERIC_INFOFRAME_HEADER);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200766 DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK0);
767 DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK1);
768 DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK2);
769 DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK3);
770 DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK4);
771 DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK5);
772 DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK6);
Thierry Redingdab16332015-01-26 16:04:08 +0100773 DUMP_REG(SOR_DP_TPG);
774 DUMP_REG(SOR_DP_TPG_CONFIG);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200775 DUMP_REG(SOR_DP_LQ_CSTM0);
776 DUMP_REG(SOR_DP_LQ_CSTM1);
777 DUMP_REG(SOR_DP_LQ_CSTM2);
Thierry Redingdab16332015-01-26 16:04:08 +0100778
779#undef DUMP_REG
780
781 return 0;
782}
783
784static const struct drm_info_list debugfs_files[] = {
Thierry Reding530239a2015-08-06 11:04:54 +0200785 { "crc", tegra_sor_show_crc, 0, NULL },
Thierry Redingdab16332015-01-26 16:04:08 +0100786 { "regs", tegra_sor_show_regs, 0, NULL },
787};
788
Thierry Reding6fad8f62014-11-28 15:41:34 +0100789static int tegra_sor_debugfs_init(struct tegra_sor *sor,
790 struct drm_minor *minor)
791{
Thierry Redingdab16332015-01-26 16:04:08 +0100792 unsigned int i;
Thierry Reding530239a2015-08-06 11:04:54 +0200793 int err;
Thierry Reding6fad8f62014-11-28 15:41:34 +0100794
795 sor->debugfs = debugfs_create_dir("sor", minor->debugfs_root);
796 if (!sor->debugfs)
797 return -ENOMEM;
798
Thierry Redingdab16332015-01-26 16:04:08 +0100799 sor->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
800 GFP_KERNEL);
801 if (!sor->debugfs_files) {
Thierry Reding6fad8f62014-11-28 15:41:34 +0100802 err = -ENOMEM;
803 goto remove;
804 }
805
Thierry Redingdab16332015-01-26 16:04:08 +0100806 for (i = 0; i < ARRAY_SIZE(debugfs_files); i++)
807 sor->debugfs_files[i].data = sor;
808
809 err = drm_debugfs_create_files(sor->debugfs_files,
810 ARRAY_SIZE(debugfs_files),
811 sor->debugfs, minor);
812 if (err < 0)
813 goto free;
814
Thierry Reding3ff1f222015-07-03 14:14:29 +0200815 sor->minor = minor;
816
Thierry Reding530239a2015-08-06 11:04:54 +0200817 return 0;
Thierry Reding6fad8f62014-11-28 15:41:34 +0100818
Thierry Redingdab16332015-01-26 16:04:08 +0100819free:
820 kfree(sor->debugfs_files);
821 sor->debugfs_files = NULL;
Thierry Reding6fad8f62014-11-28 15:41:34 +0100822remove:
Thierry Redingdab16332015-01-26 16:04:08 +0100823 debugfs_remove_recursive(sor->debugfs);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100824 sor->debugfs = NULL;
825 return err;
826}
827
Thierry Reding4009c222014-12-19 15:47:30 +0100828static void tegra_sor_debugfs_exit(struct tegra_sor *sor)
Thierry Reding6fad8f62014-11-28 15:41:34 +0100829{
Thierry Redingdab16332015-01-26 16:04:08 +0100830 drm_debugfs_remove_files(sor->debugfs_files, ARRAY_SIZE(debugfs_files),
831 sor->minor);
832 sor->minor = NULL;
833
834 kfree(sor->debugfs_files);
Thierry Reding066d30f2015-07-03 14:16:30 +0200835 sor->debugfs_files = NULL;
Thierry Redingdab16332015-01-26 16:04:08 +0100836
837 debugfs_remove_recursive(sor->debugfs);
Thierry Reding066d30f2015-07-03 14:16:30 +0200838 sor->debugfs = NULL;
Thierry Reding6fad8f62014-11-28 15:41:34 +0100839}
840
841static void tegra_sor_connector_dpms(struct drm_connector *connector, int mode)
842{
843}
844
845static enum drm_connector_status
846tegra_sor_connector_detect(struct drm_connector *connector, bool force)
847{
848 struct tegra_output *output = connector_to_output(connector);
849 struct tegra_sor *sor = to_sor(output);
850
851 if (sor->dpaux)
852 return tegra_dpaux_detect(sor->dpaux);
853
854 return connector_status_unknown;
855}
856
857static const struct drm_connector_funcs tegra_sor_connector_funcs = {
858 .dpms = tegra_sor_connector_dpms,
Thierry Reding9d441892014-11-24 17:02:53 +0100859 .reset = drm_atomic_helper_connector_reset,
Thierry Reding6fad8f62014-11-28 15:41:34 +0100860 .detect = tegra_sor_connector_detect,
861 .fill_modes = drm_helper_probe_single_connector_modes,
862 .destroy = tegra_output_connector_destroy,
Thierry Reding9d441892014-11-24 17:02:53 +0100863 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
Thierry Reding4aa3df72014-11-24 16:27:13 +0100864 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
Thierry Reding6fad8f62014-11-28 15:41:34 +0100865};
866
867static int tegra_sor_connector_get_modes(struct drm_connector *connector)
868{
869 struct tegra_output *output = connector_to_output(connector);
870 struct tegra_sor *sor = to_sor(output);
871 int err;
872
873 if (sor->dpaux)
874 tegra_dpaux_enable(sor->dpaux);
875
876 err = tegra_output_connector_get_modes(connector);
877
878 if (sor->dpaux)
879 tegra_dpaux_disable(sor->dpaux);
880
881 return err;
882}
883
884static enum drm_mode_status
885tegra_sor_connector_mode_valid(struct drm_connector *connector,
886 struct drm_display_mode *mode)
887{
888 return MODE_OK;
889}
890
891static const struct drm_connector_helper_funcs tegra_sor_connector_helper_funcs = {
892 .get_modes = tegra_sor_connector_get_modes,
893 .mode_valid = tegra_sor_connector_mode_valid,
894 .best_encoder = tegra_output_connector_best_encoder,
895};
896
897static const struct drm_encoder_funcs tegra_sor_encoder_funcs = {
898 .destroy = tegra_output_encoder_destroy,
899};
900
901static void tegra_sor_encoder_dpms(struct drm_encoder *encoder, int mode)
902{
903}
904
Thierry Reding6fad8f62014-11-28 15:41:34 +0100905static void tegra_sor_encoder_prepare(struct drm_encoder *encoder)
906{
907}
908
909static void tegra_sor_encoder_commit(struct drm_encoder *encoder)
910{
911}
912
913static void tegra_sor_encoder_mode_set(struct drm_encoder *encoder,
914 struct drm_display_mode *mode,
915 struct drm_display_mode *adjusted)
916{
917 struct tegra_output *output = encoder_to_output(encoder);
918 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100919 unsigned int vbe, vse, hbe, hse, vbs, hbs, i;
920 struct tegra_sor *sor = to_sor(output);
Thierry Reding34fa1832014-06-05 16:31:10 +0200921 struct tegra_sor_config config;
922 struct drm_dp_link link;
923 struct drm_dp_aux *aux;
Thierry Reding86f5c522014-03-26 11:13:16 +0100924 int err = 0;
Thierry Reding28fe2072015-01-26 16:02:48 +0100925 u32 value;
Thierry Reding86f5c522014-03-26 11:13:16 +0100926
927 mutex_lock(&sor->lock);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100928
929 if (sor->enabled)
Thierry Reding86f5c522014-03-26 11:13:16 +0100930 goto unlock;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100931
932 err = clk_prepare_enable(sor->clk);
933 if (err < 0)
Thierry Reding86f5c522014-03-26 11:13:16 +0100934 goto unlock;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100935
936 reset_control_deassert(sor->rst);
937
Thierry Reding6fad8f62014-11-28 15:41:34 +0100938 if (output->panel)
939 drm_panel_prepare(output->panel);
940
Thierry Reding34fa1832014-06-05 16:31:10 +0200941 /* FIXME: properly convert to struct drm_dp_aux */
942 aux = (struct drm_dp_aux *)sor->dpaux;
943
Thierry Reding6b6b6042013-11-15 16:06:05 +0100944 if (sor->dpaux) {
945 err = tegra_dpaux_enable(sor->dpaux);
946 if (err < 0)
947 dev_err(sor->dev, "failed to enable DP: %d\n", err);
Thierry Reding34fa1832014-06-05 16:31:10 +0200948
949 err = drm_dp_link_probe(aux, &link);
950 if (err < 0) {
951 dev_err(sor->dev, "failed to probe eDP link: %d\n",
952 err);
Dan Carpenter2263c462014-06-11 10:06:09 +0300953 goto unlock;
Thierry Reding34fa1832014-06-05 16:31:10 +0200954 }
Thierry Reding6b6b6042013-11-15 16:06:05 +0100955 }
956
957 err = clk_set_parent(sor->clk, sor->clk_safe);
958 if (err < 0)
959 dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
960
Thierry Reding34fa1832014-06-05 16:31:10 +0200961 memset(&config, 0, sizeof(config));
Stéphane Marchesin054b1bd2014-06-19 18:18:29 -0700962 config.bits_per_pixel = output->connector.display_info.bpc * 3;
Thierry Reding34fa1832014-06-05 16:31:10 +0200963
964 err = tegra_sor_calc_config(sor, mode, &config, &link);
965 if (err < 0)
966 dev_err(sor->dev, "failed to compute link configuration: %d\n",
967 err);
968
Thierry Reding6b6b6042013-11-15 16:06:05 +0100969 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
970 value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
971 value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
972 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
973
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200974 value = tegra_sor_readl(sor, SOR_PLL2);
975 value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
976 tegra_sor_writel(sor, value, SOR_PLL2);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100977 usleep_range(20, 100);
978
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200979 value = tegra_sor_readl(sor, SOR_PLL3);
980 value |= SOR_PLL3_PLL_VDD_MODE_3V3;
981 tegra_sor_writel(sor, value, SOR_PLL3);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100982
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200983 value = SOR_PLL0_ICHPMP(0xf) | SOR_PLL0_VCOCAP_RST |
984 SOR_PLL0_PLLREG_LEVEL_V45 | SOR_PLL0_RESISTOR_EXT;
985 tegra_sor_writel(sor, value, SOR_PLL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100986
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200987 value = tegra_sor_readl(sor, SOR_PLL2);
988 value |= SOR_PLL2_SEQ_PLLCAPPD;
989 value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
990 value |= SOR_PLL2_LVDS_ENABLE;
991 tegra_sor_writel(sor, value, SOR_PLL2);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100992
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200993 value = SOR_PLL1_TERM_COMPOUT | SOR_PLL1_TMDS_TERM;
994 tegra_sor_writel(sor, value, SOR_PLL1);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100995
996 while (true) {
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200997 value = tegra_sor_readl(sor, SOR_PLL2);
998 if ((value & SOR_PLL2_SEQ_PLLCAPPD_ENFORCE) == 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +0100999 break;
1000
1001 usleep_range(250, 1000);
1002 }
1003
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001004 value = tegra_sor_readl(sor, SOR_PLL2);
1005 value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
1006 value &= ~SOR_PLL2_PORT_POWERDOWN;
1007 tegra_sor_writel(sor, value, SOR_PLL2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001008
1009 /*
1010 * power up
1011 */
1012
1013 /* set safe link bandwidth (1.62 Gbps) */
1014 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
1015 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
1016 value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G1_62;
1017 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
1018
1019 /* step 1 */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001020 value = tegra_sor_readl(sor, SOR_PLL2);
1021 value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE | SOR_PLL2_PORT_POWERDOWN |
1022 SOR_PLL2_BANDGAP_POWERDOWN;
1023 tegra_sor_writel(sor, value, SOR_PLL2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001024
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001025 value = tegra_sor_readl(sor, SOR_PLL0);
1026 value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
1027 tegra_sor_writel(sor, value, SOR_PLL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001028
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001029 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001030 value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001031 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001032
1033 /* step 2 */
1034 err = tegra_io_rail_power_on(TEGRA_IO_RAIL_LVDS);
1035 if (err < 0) {
1036 dev_err(sor->dev, "failed to power on I/O rail: %d\n", err);
Thierry Reding86f5c522014-03-26 11:13:16 +01001037 goto unlock;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001038 }
1039
1040 usleep_range(5, 100);
1041
1042 /* step 3 */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001043 value = tegra_sor_readl(sor, SOR_PLL2);
1044 value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
1045 tegra_sor_writel(sor, value, SOR_PLL2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001046
1047 usleep_range(20, 100);
1048
1049 /* step 4 */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001050 value = tegra_sor_readl(sor, SOR_PLL0);
1051 value &= ~SOR_PLL0_VCOPD;
1052 value &= ~SOR_PLL0_PWR;
1053 tegra_sor_writel(sor, value, SOR_PLL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001054
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001055 value = tegra_sor_readl(sor, SOR_PLL2);
1056 value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1057 tegra_sor_writel(sor, value, SOR_PLL2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001058
1059 usleep_range(200, 1000);
1060
1061 /* step 5 */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001062 value = tegra_sor_readl(sor, SOR_PLL2);
1063 value &= ~SOR_PLL2_PORT_POWERDOWN;
1064 tegra_sor_writel(sor, value, SOR_PLL2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001065
1066 /* switch to DP clock */
1067 err = clk_set_parent(sor->clk, sor->clk_dp);
1068 if (err < 0)
1069 dev_err(sor->dev, "failed to set DP parent clock: %d\n", err);
1070
Thierry Reding899451b2014-06-05 16:19:48 +02001071 /* power DP lanes */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001072 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
Thierry Reding899451b2014-06-05 16:19:48 +02001073
1074 if (link.num_lanes <= 2)
1075 value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2);
1076 else
1077 value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2;
1078
1079 if (link.num_lanes <= 1)
1080 value &= ~SOR_DP_PADCTL_PD_TXD_1;
1081 else
1082 value |= SOR_DP_PADCTL_PD_TXD_1;
1083
1084 if (link.num_lanes == 0)
1085 value &= ~SOR_DP_PADCTL_PD_TXD_0;
1086 else
1087 value |= SOR_DP_PADCTL_PD_TXD_0;
1088
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001089 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001090
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001091 value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001092 value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
Thierry Reding0c90a182014-06-05 16:29:46 +02001093 value |= SOR_DP_LINKCTL_LANE_COUNT(link.num_lanes);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001094 tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001095
1096 /* start lane sequencer */
1097 value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
1098 SOR_LANE_SEQ_CTL_POWER_STATE_UP;
1099 tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
1100
1101 while (true) {
1102 value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
1103 if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
1104 break;
1105
1106 usleep_range(250, 1000);
1107 }
1108
Thierry Redinga4263fe2014-06-05 16:16:23 +02001109 /* set link bandwidth */
Thierry Reding6b6b6042013-11-15 16:06:05 +01001110 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
1111 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
Thierry Redinga4263fe2014-06-05 16:16:23 +02001112 value |= drm_dp_link_rate_to_bw_code(link.rate) << 2;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001113 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
1114
1115 /* set linkctl */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001116 value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001117 value |= SOR_DP_LINKCTL_ENABLE;
1118
1119 value &= ~SOR_DP_LINKCTL_TU_SIZE_MASK;
Thierry Reding34fa1832014-06-05 16:31:10 +02001120 value |= SOR_DP_LINKCTL_TU_SIZE(config.tu_size);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001121
1122 value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001123 tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001124
1125 for (i = 0, value = 0; i < 4; i++) {
1126 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
1127 SOR_DP_TPG_SCRAMBLER_GALIOS |
1128 SOR_DP_TPG_PATTERN_NONE;
1129 value = (value << 8) | lane;
1130 }
1131
1132 tegra_sor_writel(sor, value, SOR_DP_TPG);
1133
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001134 value = tegra_sor_readl(sor, SOR_DP_CONFIG0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001135 value &= ~SOR_DP_CONFIG_WATERMARK_MASK;
Thierry Reding34fa1832014-06-05 16:31:10 +02001136 value |= SOR_DP_CONFIG_WATERMARK(config.watermark);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001137
1138 value &= ~SOR_DP_CONFIG_ACTIVE_SYM_COUNT_MASK;
Thierry Reding34fa1832014-06-05 16:31:10 +02001139 value |= SOR_DP_CONFIG_ACTIVE_SYM_COUNT(config.active_count);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001140
1141 value &= ~SOR_DP_CONFIG_ACTIVE_SYM_FRAC_MASK;
Thierry Reding34fa1832014-06-05 16:31:10 +02001142 value |= SOR_DP_CONFIG_ACTIVE_SYM_FRAC(config.active_frac);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001143
Thierry Reding34fa1832014-06-05 16:31:10 +02001144 if (config.active_polarity)
1145 value |= SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
1146 else
1147 value &= ~SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001148
1149 value |= SOR_DP_CONFIG_ACTIVE_SYM_ENABLE;
Thierry Reding1f64ae72014-06-05 16:20:27 +02001150 value |= SOR_DP_CONFIG_DISPARITY_NEGATIVE;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001151 tegra_sor_writel(sor, value, SOR_DP_CONFIG0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001152
1153 value = tegra_sor_readl(sor, SOR_DP_AUDIO_HBLANK_SYMBOLS);
1154 value &= ~SOR_DP_AUDIO_HBLANK_SYMBOLS_MASK;
Thierry Reding7890b572014-06-05 16:12:46 +02001155 value |= config.hblank_symbols & 0xffff;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001156 tegra_sor_writel(sor, value, SOR_DP_AUDIO_HBLANK_SYMBOLS);
1157
1158 value = tegra_sor_readl(sor, SOR_DP_AUDIO_VBLANK_SYMBOLS);
1159 value &= ~SOR_DP_AUDIO_VBLANK_SYMBOLS_MASK;
Thierry Reding7890b572014-06-05 16:12:46 +02001160 value |= config.vblank_symbols & 0xffff;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001161 tegra_sor_writel(sor, value, SOR_DP_AUDIO_VBLANK_SYMBOLS);
1162
1163 /* enable pad calibration logic */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001164 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001165 value |= SOR_DP_PADCTL_PAD_CAL_PD;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001166 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001167
1168 if (sor->dpaux) {
Thierry Reding6b6b6042013-11-15 16:06:05 +01001169 u8 rate, lanes;
1170
1171 err = drm_dp_link_probe(aux, &link);
1172 if (err < 0) {
1173 dev_err(sor->dev, "failed to probe eDP link: %d\n",
1174 err);
Thierry Reding86f5c522014-03-26 11:13:16 +01001175 goto unlock;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001176 }
1177
1178 err = drm_dp_link_power_up(aux, &link);
1179 if (err < 0) {
1180 dev_err(sor->dev, "failed to power up eDP link: %d\n",
1181 err);
Thierry Reding86f5c522014-03-26 11:13:16 +01001182 goto unlock;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001183 }
1184
1185 err = drm_dp_link_configure(aux, &link);
1186 if (err < 0) {
1187 dev_err(sor->dev, "failed to configure eDP link: %d\n",
1188 err);
Thierry Reding86f5c522014-03-26 11:13:16 +01001189 goto unlock;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001190 }
1191
1192 rate = drm_dp_link_rate_to_bw_code(link.rate);
1193 lanes = link.num_lanes;
1194
1195 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
1196 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
1197 value |= SOR_CLK_CNTRL_DP_LINK_SPEED(rate);
1198 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
1199
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001200 value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001201 value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
1202 value |= SOR_DP_LINKCTL_LANE_COUNT(lanes);
1203
1204 if (link.capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
1205 value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
1206
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001207 tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001208
1209 /* disable training pattern generator */
1210
1211 for (i = 0; i < link.num_lanes; i++) {
1212 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
1213 SOR_DP_TPG_SCRAMBLER_GALIOS |
1214 SOR_DP_TPG_PATTERN_NONE;
1215 value = (value << 8) | lane;
1216 }
1217
1218 tegra_sor_writel(sor, value, SOR_DP_TPG);
1219
1220 err = tegra_sor_dp_train_fast(sor, &link);
1221 if (err < 0) {
1222 dev_err(sor->dev, "DP fast link training failed: %d\n",
1223 err);
Thierry Reding86f5c522014-03-26 11:13:16 +01001224 goto unlock;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001225 }
1226
1227 dev_dbg(sor->dev, "fast link training succeeded\n");
1228 }
1229
1230 err = tegra_sor_power_up(sor, 250);
1231 if (err < 0) {
1232 dev_err(sor->dev, "failed to power up SOR: %d\n", err);
Thierry Reding86f5c522014-03-26 11:13:16 +01001233 goto unlock;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001234 }
1235
Thierry Reding6b6b6042013-11-15 16:06:05 +01001236 /*
1237 * configure panel (24bpp, vsync-, hsync-, DP-A protocol, complete
1238 * raster, associate with display controller)
1239 */
Thierry Reding3f4f3b52014-07-07 15:35:06 +02001240 value = SOR_STATE_ASY_PROTOCOL_DP_A |
Thierry Reding6b6b6042013-11-15 16:06:05 +01001241 SOR_STATE_ASY_CRC_MODE_COMPLETE |
1242 SOR_STATE_ASY_OWNER(dc->pipe + 1);
Thierry Reding34fa1832014-06-05 16:31:10 +02001243
Thierry Reding3f4f3b52014-07-07 15:35:06 +02001244 if (mode->flags & DRM_MODE_FLAG_PHSYNC)
1245 value &= ~SOR_STATE_ASY_HSYNCPOL;
1246
1247 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1248 value |= SOR_STATE_ASY_HSYNCPOL;
1249
1250 if (mode->flags & DRM_MODE_FLAG_PVSYNC)
1251 value &= ~SOR_STATE_ASY_VSYNCPOL;
1252
1253 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1254 value |= SOR_STATE_ASY_VSYNCPOL;
1255
Thierry Reding34fa1832014-06-05 16:31:10 +02001256 switch (config.bits_per_pixel) {
1257 case 24:
1258 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
1259 break;
1260
1261 case 18:
1262 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_18_444;
1263 break;
1264
1265 default:
1266 BUG();
1267 break;
1268 }
1269
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001270 tegra_sor_writel(sor, value, SOR_STATE1);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001271
1272 /*
1273 * TODO: The video timing programming below doesn't seem to match the
1274 * register definitions.
1275 */
1276
1277 value = ((mode->vtotal & 0x7fff) << 16) | (mode->htotal & 0x7fff);
Thierry Reding51511d02015-07-30 18:47:07 +02001278 tegra_sor_writel(sor, value, SOR_HEAD_STATE1(dc->pipe));
Thierry Reding6b6b6042013-11-15 16:06:05 +01001279
1280 vse = mode->vsync_end - mode->vsync_start - 1;
1281 hse = mode->hsync_end - mode->hsync_start - 1;
1282
1283 value = ((vse & 0x7fff) << 16) | (hse & 0x7fff);
Thierry Reding51511d02015-07-30 18:47:07 +02001284 tegra_sor_writel(sor, value, SOR_HEAD_STATE2(dc->pipe));
Thierry Reding6b6b6042013-11-15 16:06:05 +01001285
1286 vbe = vse + (mode->vsync_start - mode->vdisplay);
1287 hbe = hse + (mode->hsync_start - mode->hdisplay);
1288
1289 value = ((vbe & 0x7fff) << 16) | (hbe & 0x7fff);
Thierry Reding51511d02015-07-30 18:47:07 +02001290 tegra_sor_writel(sor, value, SOR_HEAD_STATE3(dc->pipe));
Thierry Reding6b6b6042013-11-15 16:06:05 +01001291
1292 vbs = vbe + mode->vdisplay;
1293 hbs = hbe + mode->hdisplay;
1294
1295 value = ((vbs & 0x7fff) << 16) | (hbs & 0x7fff);
Thierry Reding51511d02015-07-30 18:47:07 +02001296 tegra_sor_writel(sor, value, SOR_HEAD_STATE4(dc->pipe));
1297
1298 tegra_sor_writel(sor, 0x1, SOR_HEAD_STATE5(dc->pipe));
Thierry Reding6b6b6042013-11-15 16:06:05 +01001299
Thierry Reding6b6b6042013-11-15 16:06:05 +01001300 /* CSTM (LVDS, link A/B, upper) */
Stéphane Marchesin143b1df2014-05-22 20:32:47 -07001301 value = SOR_CSTM_LVDS | SOR_CSTM_LINK_ACT_A | SOR_CSTM_LINK_ACT_B |
Thierry Reding6b6b6042013-11-15 16:06:05 +01001302 SOR_CSTM_UPPER;
1303 tegra_sor_writel(sor, value, SOR_CSTM);
1304
1305 /* PWM setup */
1306 err = tegra_sor_setup_pwm(sor, 250);
1307 if (err < 0) {
1308 dev_err(sor->dev, "failed to setup PWM: %d\n", err);
Thierry Reding86f5c522014-03-26 11:13:16 +01001309 goto unlock;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001310 }
1311
Thierry Reding666cb872014-12-08 16:32:47 +01001312 tegra_sor_update(sor);
1313
Thierry Reding6b6b6042013-11-15 16:06:05 +01001314 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
1315 value |= SOR_ENABLE;
1316 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
1317
Thierry Reding666cb872014-12-08 16:32:47 +01001318 tegra_dc_commit(dc);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001319
1320 err = tegra_sor_attach(sor);
1321 if (err < 0) {
1322 dev_err(sor->dev, "failed to attach SOR: %d\n", err);
Thierry Reding86f5c522014-03-26 11:13:16 +01001323 goto unlock;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001324 }
1325
1326 err = tegra_sor_wakeup(sor);
1327 if (err < 0) {
1328 dev_err(sor->dev, "failed to enable DC: %d\n", err);
Thierry Reding86f5c522014-03-26 11:13:16 +01001329 goto unlock;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001330 }
1331
Thierry Reding6fad8f62014-11-28 15:41:34 +01001332 if (output->panel)
1333 drm_panel_enable(output->panel);
1334
Thierry Reding6b6b6042013-11-15 16:06:05 +01001335 sor->enabled = true;
1336
Thierry Reding86f5c522014-03-26 11:13:16 +01001337unlock:
1338 mutex_unlock(&sor->lock);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001339}
1340
Thierry Reding6fad8f62014-11-28 15:41:34 +01001341static void tegra_sor_encoder_disable(struct drm_encoder *encoder)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001342{
Thierry Reding6fad8f62014-11-28 15:41:34 +01001343 struct tegra_output *output = encoder_to_output(encoder);
1344 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001345 struct tegra_sor *sor = to_sor(output);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001346 u32 value;
1347 int err;
Thierry Reding86f5c522014-03-26 11:13:16 +01001348
1349 mutex_lock(&sor->lock);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001350
1351 if (!sor->enabled)
Thierry Reding86f5c522014-03-26 11:13:16 +01001352 goto unlock;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001353
Thierry Reding6fad8f62014-11-28 15:41:34 +01001354 if (output->panel)
1355 drm_panel_disable(output->panel);
1356
Thierry Reding6b6b6042013-11-15 16:06:05 +01001357 err = tegra_sor_detach(sor);
1358 if (err < 0) {
1359 dev_err(sor->dev, "failed to detach SOR: %d\n", err);
Thierry Reding86f5c522014-03-26 11:13:16 +01001360 goto unlock;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001361 }
1362
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001363 tegra_sor_writel(sor, 0, SOR_STATE1);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001364 tegra_sor_update(sor);
1365
1366 /*
1367 * The following accesses registers of the display controller, so make
1368 * sure it's only executed when the output is attached to one.
1369 */
1370 if (dc) {
Thierry Reding6b6b6042013-11-15 16:06:05 +01001371 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
1372 value &= ~SOR_ENABLE;
1373 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
1374
Thierry Reding62b9e062014-11-21 17:33:33 +01001375 tegra_dc_commit(dc);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001376 }
1377
1378 err = tegra_sor_power_down(sor);
1379 if (err < 0) {
1380 dev_err(sor->dev, "failed to power down SOR: %d\n", err);
Thierry Reding86f5c522014-03-26 11:13:16 +01001381 goto unlock;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001382 }
1383
1384 if (sor->dpaux) {
1385 err = tegra_dpaux_disable(sor->dpaux);
1386 if (err < 0) {
1387 dev_err(sor->dev, "failed to disable DP: %d\n", err);
Thierry Reding86f5c522014-03-26 11:13:16 +01001388 goto unlock;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001389 }
1390 }
1391
1392 err = tegra_io_rail_power_off(TEGRA_IO_RAIL_LVDS);
1393 if (err < 0) {
1394 dev_err(sor->dev, "failed to power off I/O rail: %d\n", err);
Thierry Reding86f5c522014-03-26 11:13:16 +01001395 goto unlock;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001396 }
1397
Thierry Reding6fad8f62014-11-28 15:41:34 +01001398 if (output->panel)
1399 drm_panel_unprepare(output->panel);
1400
Thierry Reding6b6b6042013-11-15 16:06:05 +01001401 clk_disable_unprepare(sor->clk);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001402 reset_control_assert(sor->rst);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001403
1404 sor->enabled = false;
1405
Thierry Reding86f5c522014-03-26 11:13:16 +01001406unlock:
1407 mutex_unlock(&sor->lock);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001408}
1409
Thierry Reding82f15112014-12-08 17:26:46 +01001410static int
1411tegra_sor_encoder_atomic_check(struct drm_encoder *encoder,
1412 struct drm_crtc_state *crtc_state,
1413 struct drm_connector_state *conn_state)
1414{
1415 struct tegra_output *output = encoder_to_output(encoder);
1416 struct tegra_dc *dc = to_tegra_dc(conn_state->crtc);
1417 unsigned long pclk = crtc_state->mode.clock * 1000;
1418 struct tegra_sor *sor = to_sor(output);
1419 int err;
1420
1421 err = tegra_dc_state_setup_clock(dc, crtc_state, sor->clk_parent,
1422 pclk, 0);
1423 if (err < 0) {
1424 dev_err(output->dev, "failed to setup CRTC state: %d\n", err);
1425 return err;
1426 }
1427
1428 return 0;
1429}
1430
Thierry Reding6fad8f62014-11-28 15:41:34 +01001431static const struct drm_encoder_helper_funcs tegra_sor_encoder_helper_funcs = {
1432 .dpms = tegra_sor_encoder_dpms,
Thierry Reding6fad8f62014-11-28 15:41:34 +01001433 .prepare = tegra_sor_encoder_prepare,
1434 .commit = tegra_sor_encoder_commit,
1435 .mode_set = tegra_sor_encoder_mode_set,
1436 .disable = tegra_sor_encoder_disable,
Thierry Reding82f15112014-12-08 17:26:46 +01001437 .atomic_check = tegra_sor_encoder_atomic_check,
Thierry Reding6b6b6042013-11-15 16:06:05 +01001438};
1439
1440static int tegra_sor_init(struct host1x_client *client)
1441{
Thierry Reding9910f5c2014-05-22 09:57:15 +02001442 struct drm_device *drm = dev_get_drvdata(client->parent);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001443 struct tegra_sor *sor = host1x_client_to_sor(client);
1444 int err;
1445
1446 if (!sor->dpaux)
1447 return -ENODEV;
1448
Thierry Reding6b6b6042013-11-15 16:06:05 +01001449 sor->output.dev = sor->dev;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001450
Thierry Reding6fad8f62014-11-28 15:41:34 +01001451 drm_connector_init(drm, &sor->output.connector,
1452 &tegra_sor_connector_funcs,
1453 DRM_MODE_CONNECTOR_eDP);
1454 drm_connector_helper_add(&sor->output.connector,
1455 &tegra_sor_connector_helper_funcs);
1456 sor->output.connector.dpms = DRM_MODE_DPMS_OFF;
1457
Thierry Reding6fad8f62014-11-28 15:41:34 +01001458 drm_encoder_init(drm, &sor->output.encoder, &tegra_sor_encoder_funcs,
1459 DRM_MODE_ENCODER_TMDS);
1460 drm_encoder_helper_add(&sor->output.encoder,
1461 &tegra_sor_encoder_helper_funcs);
1462
1463 drm_mode_connector_attach_encoder(&sor->output.connector,
1464 &sor->output.encoder);
1465 drm_connector_register(&sor->output.connector);
1466
Thierry Redingea130b22014-12-19 15:51:35 +01001467 err = tegra_output_init(drm, &sor->output);
1468 if (err < 0) {
1469 dev_err(client->dev, "failed to initialize output: %d\n", err);
1470 return err;
1471 }
Thierry Reding6fad8f62014-11-28 15:41:34 +01001472
Thierry Redingea130b22014-12-19 15:51:35 +01001473 sor->output.encoder.possible_crtcs = 0x3;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001474
Thierry Redinga82752e2014-01-31 10:02:15 +01001475 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
Thierry Reding1b0c7b42014-05-28 13:46:12 +02001476 err = tegra_sor_debugfs_init(sor, drm->primary);
Thierry Redinga82752e2014-01-31 10:02:15 +01001477 if (err < 0)
1478 dev_err(sor->dev, "debugfs setup failed: %d\n", err);
1479 }
1480
Thierry Reding6b6b6042013-11-15 16:06:05 +01001481 if (sor->dpaux) {
1482 err = tegra_dpaux_attach(sor->dpaux, &sor->output);
1483 if (err < 0) {
1484 dev_err(sor->dev, "failed to attach DP: %d\n", err);
1485 return err;
1486 }
1487 }
1488
Tomeu Vizoso535a65d2015-03-30 10:33:03 +02001489 /*
1490 * XXX: Remove this reset once proper hand-over from firmware to
1491 * kernel is possible.
1492 */
1493 err = reset_control_assert(sor->rst);
1494 if (err < 0) {
1495 dev_err(sor->dev, "failed to assert SOR reset: %d\n", err);
1496 return err;
1497 }
1498
Thierry Reding6fad8f62014-11-28 15:41:34 +01001499 err = clk_prepare_enable(sor->clk);
1500 if (err < 0) {
1501 dev_err(sor->dev, "failed to enable clock: %d\n", err);
1502 return err;
1503 }
1504
Tomeu Vizoso535a65d2015-03-30 10:33:03 +02001505 usleep_range(1000, 3000);
1506
1507 err = reset_control_deassert(sor->rst);
1508 if (err < 0) {
1509 dev_err(sor->dev, "failed to deassert SOR reset: %d\n", err);
1510 return err;
1511 }
1512
Thierry Reding6fad8f62014-11-28 15:41:34 +01001513 err = clk_prepare_enable(sor->clk_safe);
1514 if (err < 0)
1515 return err;
1516
1517 err = clk_prepare_enable(sor->clk_dp);
1518 if (err < 0)
1519 return err;
1520
Thierry Reding6b6b6042013-11-15 16:06:05 +01001521 return 0;
1522}
1523
1524static int tegra_sor_exit(struct host1x_client *client)
1525{
1526 struct tegra_sor *sor = host1x_client_to_sor(client);
1527 int err;
1528
Thierry Reding328ec692014-12-19 15:55:08 +01001529 tegra_output_exit(&sor->output);
1530
Thierry Reding6b6b6042013-11-15 16:06:05 +01001531 if (sor->dpaux) {
1532 err = tegra_dpaux_detach(sor->dpaux);
1533 if (err < 0) {
1534 dev_err(sor->dev, "failed to detach DP: %d\n", err);
1535 return err;
1536 }
1537 }
1538
Thierry Reding6fad8f62014-11-28 15:41:34 +01001539 clk_disable_unprepare(sor->clk_safe);
1540 clk_disable_unprepare(sor->clk_dp);
1541 clk_disable_unprepare(sor->clk);
1542
Thierry Reding4009c222014-12-19 15:47:30 +01001543 if (IS_ENABLED(CONFIG_DEBUG_FS))
1544 tegra_sor_debugfs_exit(sor);
Thierry Redinga82752e2014-01-31 10:02:15 +01001545
Thierry Reding6b6b6042013-11-15 16:06:05 +01001546 return 0;
1547}
1548
1549static const struct host1x_client_ops sor_client_ops = {
1550 .init = tegra_sor_init,
1551 .exit = tegra_sor_exit,
1552};
1553
1554static int tegra_sor_probe(struct platform_device *pdev)
1555{
1556 struct device_node *np;
1557 struct tegra_sor *sor;
1558 struct resource *regs;
1559 int err;
1560
1561 sor = devm_kzalloc(&pdev->dev, sizeof(*sor), GFP_KERNEL);
1562 if (!sor)
1563 return -ENOMEM;
1564
1565 sor->output.dev = sor->dev = &pdev->dev;
1566
1567 np = of_parse_phandle(pdev->dev.of_node, "nvidia,dpaux", 0);
1568 if (np) {
1569 sor->dpaux = tegra_dpaux_find_by_of_node(np);
1570 of_node_put(np);
1571
1572 if (!sor->dpaux)
1573 return -EPROBE_DEFER;
1574 }
1575
1576 err = tegra_output_probe(&sor->output);
Thierry Reding4dbdc742015-04-27 15:04:26 +02001577 if (err < 0) {
1578 dev_err(&pdev->dev, "failed to probe output: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001579 return err;
Thierry Reding4dbdc742015-04-27 15:04:26 +02001580 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01001581
1582 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1583 sor->regs = devm_ioremap_resource(&pdev->dev, regs);
1584 if (IS_ERR(sor->regs))
1585 return PTR_ERR(sor->regs);
1586
1587 sor->rst = devm_reset_control_get(&pdev->dev, "sor");
Thierry Reding4dbdc742015-04-27 15:04:26 +02001588 if (IS_ERR(sor->rst)) {
1589 dev_err(&pdev->dev, "failed to get reset control: %ld\n",
1590 PTR_ERR(sor->rst));
Thierry Reding6b6b6042013-11-15 16:06:05 +01001591 return PTR_ERR(sor->rst);
Thierry Reding4dbdc742015-04-27 15:04:26 +02001592 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01001593
1594 sor->clk = devm_clk_get(&pdev->dev, NULL);
Thierry Reding4dbdc742015-04-27 15:04:26 +02001595 if (IS_ERR(sor->clk)) {
1596 dev_err(&pdev->dev, "failed to get module clock: %ld\n",
1597 PTR_ERR(sor->clk));
Thierry Reding6b6b6042013-11-15 16:06:05 +01001598 return PTR_ERR(sor->clk);
Thierry Reding4dbdc742015-04-27 15:04:26 +02001599 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01001600
1601 sor->clk_parent = devm_clk_get(&pdev->dev, "parent");
Thierry Reding4dbdc742015-04-27 15:04:26 +02001602 if (IS_ERR(sor->clk_parent)) {
1603 dev_err(&pdev->dev, "failed to get parent clock: %ld\n",
1604 PTR_ERR(sor->clk_parent));
Thierry Reding6b6b6042013-11-15 16:06:05 +01001605 return PTR_ERR(sor->clk_parent);
Thierry Reding4dbdc742015-04-27 15:04:26 +02001606 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01001607
Thierry Reding6b6b6042013-11-15 16:06:05 +01001608 sor->clk_safe = devm_clk_get(&pdev->dev, "safe");
Thierry Reding4dbdc742015-04-27 15:04:26 +02001609 if (IS_ERR(sor->clk_safe)) {
1610 dev_err(&pdev->dev, "failed to get safe clock: %ld\n",
1611 PTR_ERR(sor->clk_safe));
Thierry Reding6b6b6042013-11-15 16:06:05 +01001612 return PTR_ERR(sor->clk_safe);
Thierry Reding4dbdc742015-04-27 15:04:26 +02001613 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01001614
Thierry Reding6b6b6042013-11-15 16:06:05 +01001615 sor->clk_dp = devm_clk_get(&pdev->dev, "dp");
Thierry Reding4dbdc742015-04-27 15:04:26 +02001616 if (IS_ERR(sor->clk_dp)) {
1617 dev_err(&pdev->dev, "failed to get DP clock: %ld\n",
1618 PTR_ERR(sor->clk_dp));
Thierry Reding6b6b6042013-11-15 16:06:05 +01001619 return PTR_ERR(sor->clk_dp);
Thierry Reding4dbdc742015-04-27 15:04:26 +02001620 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01001621
Thierry Reding6b6b6042013-11-15 16:06:05 +01001622 INIT_LIST_HEAD(&sor->client.list);
1623 sor->client.ops = &sor_client_ops;
1624 sor->client.dev = &pdev->dev;
1625
Thierry Reding86f5c522014-03-26 11:13:16 +01001626 mutex_init(&sor->lock);
1627
Thierry Reding6b6b6042013-11-15 16:06:05 +01001628 err = host1x_client_register(&sor->client);
1629 if (err < 0) {
1630 dev_err(&pdev->dev, "failed to register host1x client: %d\n",
1631 err);
1632 return err;
1633 }
1634
1635 platform_set_drvdata(pdev, sor);
1636
1637 return 0;
1638}
1639
1640static int tegra_sor_remove(struct platform_device *pdev)
1641{
1642 struct tegra_sor *sor = platform_get_drvdata(pdev);
1643 int err;
1644
1645 err = host1x_client_unregister(&sor->client);
1646 if (err < 0) {
1647 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
1648 err);
1649 return err;
1650 }
1651
Thierry Reding328ec692014-12-19 15:55:08 +01001652 tegra_output_remove(&sor->output);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001653
1654 return 0;
1655}
1656
1657static const struct of_device_id tegra_sor_of_match[] = {
1658 { .compatible = "nvidia,tegra124-sor", },
1659 { },
1660};
Stephen Warrenef707282014-06-18 16:21:55 -06001661MODULE_DEVICE_TABLE(of, tegra_sor_of_match);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001662
1663struct platform_driver tegra_sor_driver = {
1664 .driver = {
1665 .name = "tegra-sor",
1666 .of_match_table = tegra_sor_of_match,
1667 },
1668 .probe = tegra_sor_probe,
1669 .remove = tegra_sor_remove,
1670};