2 * Copyright (C) 2013 NVIDIA Corporation
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.
10 #include <linux/debugfs.h>
12 #include <linux/platform_device.h>
13 #include <linux/reset.h>
14 #include <linux/tegra-powergate.h>
16 #include <drm/drm_dp_helper.h>
23 struct host1x_client client;
24 struct tegra_output output;
29 struct reset_control *rst;
30 struct clk *clk_parent;
35 struct tegra_dpaux *dpaux;
40 struct dentry *debugfs;
43 struct tegra_sor_config {
53 static inline struct tegra_sor *
54 host1x_client_to_sor(struct host1x_client *client)
56 return container_of(client, struct tegra_sor, client);
59 static inline struct tegra_sor *to_sor(struct tegra_output *output)
61 return container_of(output, struct tegra_sor, output);
64 static inline unsigned long tegra_sor_readl(struct tegra_sor *sor,
67 return readl(sor->regs + (offset << 2));
70 static inline void tegra_sor_writel(struct tegra_sor *sor, unsigned long value,
73 writel(value, sor->regs + (offset << 2));
76 static int tegra_sor_dp_train_fast(struct tegra_sor *sor,
77 struct drm_dp_link *link)
84 /* setup lane parameters */
85 value = SOR_LANE_DRIVE_CURRENT_LANE3(0x40) |
86 SOR_LANE_DRIVE_CURRENT_LANE2(0x40) |
87 SOR_LANE_DRIVE_CURRENT_LANE1(0x40) |
88 SOR_LANE_DRIVE_CURRENT_LANE0(0x40);
89 tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT_0);
91 value = SOR_LANE_PREEMPHASIS_LANE3(0x0f) |
92 SOR_LANE_PREEMPHASIS_LANE2(0x0f) |
93 SOR_LANE_PREEMPHASIS_LANE1(0x0f) |
94 SOR_LANE_PREEMPHASIS_LANE0(0x0f);
95 tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS_0);
97 value = SOR_LANE_POST_CURSOR_LANE3(0x00) |
98 SOR_LANE_POST_CURSOR_LANE2(0x00) |
99 SOR_LANE_POST_CURSOR_LANE1(0x00) |
100 SOR_LANE_POST_CURSOR_LANE0(0x00);
101 tegra_sor_writel(sor, value, SOR_LANE_POST_CURSOR_0);
103 /* disable LVDS mode */
104 tegra_sor_writel(sor, 0, SOR_LVDS);
106 value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
107 value |= SOR_DP_PADCTL_TX_PU_ENABLE;
108 value &= ~SOR_DP_PADCTL_TX_PU_MASK;
109 value |= SOR_DP_PADCTL_TX_PU(2); /* XXX: don't hardcode? */
110 tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
112 value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
113 value |= SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
114 SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0;
115 tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
117 usleep_range(10, 100);
119 value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
120 value &= ~(SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
121 SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0);
122 tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
124 err = tegra_dpaux_prepare(sor->dpaux, DP_SET_ANSI_8B10B);
128 for (i = 0, value = 0; i < link->num_lanes; i++) {
129 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
130 SOR_DP_TPG_SCRAMBLER_NONE |
131 SOR_DP_TPG_PATTERN_TRAIN1;
132 value = (value << 8) | lane;
135 tegra_sor_writel(sor, value, SOR_DP_TPG);
137 pattern = DP_TRAINING_PATTERN_1;
139 err = tegra_dpaux_train(sor->dpaux, link, pattern);
143 value = tegra_sor_readl(sor, SOR_DP_SPARE_0);
144 value |= SOR_DP_SPARE_SEQ_ENABLE;
145 value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
146 value |= SOR_DP_SPARE_MACRO_SOR_CLK;
147 tegra_sor_writel(sor, value, SOR_DP_SPARE_0);
149 for (i = 0, value = 0; i < link->num_lanes; i++) {
150 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
151 SOR_DP_TPG_SCRAMBLER_NONE |
152 SOR_DP_TPG_PATTERN_TRAIN2;
153 value = (value << 8) | lane;
156 tegra_sor_writel(sor, value, SOR_DP_TPG);
158 pattern = DP_LINK_SCRAMBLING_DISABLE | DP_TRAINING_PATTERN_2;
160 err = tegra_dpaux_train(sor->dpaux, link, pattern);
164 for (i = 0, value = 0; i < link->num_lanes; i++) {
165 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
166 SOR_DP_TPG_SCRAMBLER_GALIOS |
167 SOR_DP_TPG_PATTERN_NONE;
168 value = (value << 8) | lane;
171 tegra_sor_writel(sor, value, SOR_DP_TPG);
173 pattern = DP_TRAINING_PATTERN_DISABLE;
175 err = tegra_dpaux_train(sor->dpaux, link, pattern);
182 static void tegra_sor_super_update(struct tegra_sor *sor)
184 tegra_sor_writel(sor, 0, SOR_SUPER_STATE_0);
185 tegra_sor_writel(sor, 1, SOR_SUPER_STATE_0);
186 tegra_sor_writel(sor, 0, SOR_SUPER_STATE_0);
189 static void tegra_sor_update(struct tegra_sor *sor)
191 tegra_sor_writel(sor, 0, SOR_STATE_0);
192 tegra_sor_writel(sor, 1, SOR_STATE_0);
193 tegra_sor_writel(sor, 0, SOR_STATE_0);
196 static int tegra_sor_setup_pwm(struct tegra_sor *sor, unsigned long timeout)
200 value = tegra_sor_readl(sor, SOR_PWM_DIV);
201 value &= ~SOR_PWM_DIV_MASK;
202 value |= 0x400; /* period */
203 tegra_sor_writel(sor, value, SOR_PWM_DIV);
205 value = tegra_sor_readl(sor, SOR_PWM_CTL);
206 value &= ~SOR_PWM_CTL_DUTY_CYCLE_MASK;
207 value |= 0x400; /* duty cycle */
208 value &= ~SOR_PWM_CTL_CLK_SEL; /* clock source: PCLK */
209 value |= SOR_PWM_CTL_TRIGGER;
210 tegra_sor_writel(sor, value, SOR_PWM_CTL);
212 timeout = jiffies + msecs_to_jiffies(timeout);
214 while (time_before(jiffies, timeout)) {
215 value = tegra_sor_readl(sor, SOR_PWM_CTL);
216 if ((value & SOR_PWM_CTL_TRIGGER) == 0)
219 usleep_range(25, 100);
225 static int tegra_sor_attach(struct tegra_sor *sor)
227 unsigned long value, timeout;
229 /* wake up in normal mode */
230 value = tegra_sor_readl(sor, SOR_SUPER_STATE_1);
231 value |= SOR_SUPER_STATE_HEAD_MODE_AWAKE;
232 value |= SOR_SUPER_STATE_MODE_NORMAL;
233 tegra_sor_writel(sor, value, SOR_SUPER_STATE_1);
234 tegra_sor_super_update(sor);
237 value = tegra_sor_readl(sor, SOR_SUPER_STATE_1);
238 value |= SOR_SUPER_STATE_ATTACHED;
239 tegra_sor_writel(sor, value, SOR_SUPER_STATE_1);
240 tegra_sor_super_update(sor);
242 timeout = jiffies + msecs_to_jiffies(250);
244 while (time_before(jiffies, timeout)) {
245 value = tegra_sor_readl(sor, SOR_TEST);
246 if ((value & SOR_TEST_ATTACHED) != 0)
249 usleep_range(25, 100);
255 static int tegra_sor_wakeup(struct tegra_sor *sor)
257 struct tegra_dc *dc = to_tegra_dc(sor->output.encoder.crtc);
258 unsigned long value, timeout;
260 /* enable display controller outputs */
261 value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
262 value |= PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
263 PW4_ENABLE | PM0_ENABLE | PM1_ENABLE;
264 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
266 tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
267 tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
269 timeout = jiffies + msecs_to_jiffies(250);
271 /* wait for head to wake up */
272 while (time_before(jiffies, timeout)) {
273 value = tegra_sor_readl(sor, SOR_TEST);
274 value &= SOR_TEST_HEAD_MODE_MASK;
276 if (value == SOR_TEST_HEAD_MODE_AWAKE)
279 usleep_range(25, 100);
285 static int tegra_sor_power_up(struct tegra_sor *sor, unsigned long timeout)
289 value = tegra_sor_readl(sor, SOR_PWR);
290 value |= SOR_PWR_TRIGGER | SOR_PWR_NORMAL_STATE_PU;
291 tegra_sor_writel(sor, value, SOR_PWR);
293 timeout = jiffies + msecs_to_jiffies(timeout);
295 while (time_before(jiffies, timeout)) {
296 value = tegra_sor_readl(sor, SOR_PWR);
297 if ((value & SOR_PWR_TRIGGER) == 0)
300 usleep_range(25, 100);
306 struct tegra_sor_params {
307 /* number of link clocks per line */
308 unsigned int num_clocks;
309 /* ratio between input and output */
311 /* precision factor */
314 unsigned int active_polarity;
315 unsigned int active_count;
316 unsigned int active_frac;
317 unsigned int tu_size;
321 static int tegra_sor_compute_params(struct tegra_sor *sor,
322 struct tegra_sor_params *params,
323 unsigned int tu_size)
325 u64 active_sym, active_count, frac, approx;
326 u32 active_polarity, active_frac = 0;
327 const u64 f = params->precision;
330 active_sym = params->ratio * tu_size;
331 active_count = div_u64(active_sym, f) * f;
332 frac = active_sym - active_count;
335 if (frac >= (f / 2)) {
343 frac = div_u64(f * f, frac); /* 1/fraction */
344 if (frac <= (15 * f)) {
345 active_frac = div_u64(frac, f);
351 active_frac = active_polarity ? 1 : 15;
355 if (active_frac == 1)
358 if (active_polarity == 1) {
360 approx = active_count + (active_frac * (f - 1)) * f;
361 approx = div_u64(approx, active_frac * f);
363 approx = active_count + f;
367 approx = active_count + div_u64(f, active_frac);
369 approx = active_count;
372 error = div_s64(active_sym - approx, tu_size);
373 error *= params->num_clocks;
375 if (error <= 0 && abs64(error) < params->error) {
376 params->active_count = div_u64(active_count, f);
377 params->active_polarity = active_polarity;
378 params->active_frac = active_frac;
379 params->error = abs64(error);
380 params->tu_size = tu_size;
389 static int tegra_sor_calc_config(struct tegra_sor *sor,
390 struct drm_display_mode *mode,
391 struct tegra_sor_config *config,
392 struct drm_dp_link *link)
394 const u64 f = 100000, link_rate = link->rate * 1000;
395 const u64 pclk = mode->clock * 1000;
396 struct tegra_sor_params params;
397 u64 input, output, watermark;
398 u32 num_syms_per_line;
401 if (!link_rate || !link->num_lanes || !pclk || !config->bits_per_pixel)
404 output = link_rate * 8 * link->num_lanes;
405 input = pclk * config->bits_per_pixel;
410 memset(¶ms, 0, sizeof(params));
411 params.ratio = div64_u64(input * f, output);
412 params.num_clocks = div_u64(link_rate * mode->hdisplay, pclk);
413 params.precision = f;
414 params.error = 64 * f;
417 for (i = params.tu_size; i >= 32; i--)
418 if (tegra_sor_compute_params(sor, ¶ms, i))
421 if (params.active_frac == 0) {
422 config->active_polarity = 0;
423 config->active_count = params.active_count;
425 if (!params.active_polarity)
426 config->active_count--;
428 config->tu_size = params.tu_size;
429 config->active_frac = 1;
431 config->active_polarity = params.active_polarity;
432 config->active_count = params.active_count;
433 config->active_frac = params.active_frac;
434 config->tu_size = params.tu_size;
438 "polarity: %d active count: %d tu size: %d active frac: %d\n",
439 config->active_polarity, config->active_count,
440 config->tu_size, config->active_frac);
442 watermark = params.ratio * config->tu_size * (f - params.ratio);
443 watermark = div_u64(watermark, f);
445 watermark = div_u64(watermark + params.error, f);
446 config->watermark = watermark + (config->bits_per_pixel / 8) + 2;
447 num_syms_per_line = (mode->hdisplay * config->bits_per_pixel) *
448 (link->num_lanes * 8);
450 if (config->watermark > 30) {
451 config->watermark = 30;
453 "unable to compute TU size, forcing watermark to %u\n",
455 } else if (config->watermark > num_syms_per_line) {
456 config->watermark = num_syms_per_line;
457 dev_err(sor->dev, "watermark too high, forcing to %u\n",
464 static int tegra_output_sor_enable(struct tegra_output *output)
466 struct tegra_dc *dc = to_tegra_dc(output->encoder.crtc);
467 struct drm_display_mode *mode = &dc->base.mode;
468 unsigned int vbe, vse, hbe, hse, vbs, hbs, i;
469 struct tegra_sor *sor = to_sor(output);
470 struct tegra_sor_config config;
471 struct drm_dp_link link;
472 struct drm_dp_aux *aux;
476 mutex_lock(&sor->lock);
481 err = clk_prepare_enable(sor->clk);
485 reset_control_deassert(sor->rst);
487 /* FIXME: properly convert to struct drm_dp_aux */
488 aux = (struct drm_dp_aux *)sor->dpaux;
491 err = tegra_dpaux_enable(sor->dpaux);
493 dev_err(sor->dev, "failed to enable DP: %d\n", err);
495 err = drm_dp_link_probe(aux, &link);
497 dev_err(sor->dev, "failed to probe eDP link: %d\n",
503 err = clk_set_parent(sor->clk, sor->clk_safe);
505 dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
507 memset(&config, 0, sizeof(config));
508 config.bits_per_pixel = 24; /* XXX: don't hardcode? */
510 err = tegra_sor_calc_config(sor, mode, &config, &link);
512 dev_err(sor->dev, "failed to compute link configuration: %d\n",
515 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
516 value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
517 value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
518 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
520 value = tegra_sor_readl(sor, SOR_PLL_2);
521 value &= ~SOR_PLL_2_BANDGAP_POWERDOWN;
522 tegra_sor_writel(sor, value, SOR_PLL_2);
523 usleep_range(20, 100);
525 value = tegra_sor_readl(sor, SOR_PLL_3);
526 value |= SOR_PLL_3_PLL_VDD_MODE_V3_3;
527 tegra_sor_writel(sor, value, SOR_PLL_3);
529 value = SOR_PLL_0_ICHPMP(0xf) | SOR_PLL_0_VCOCAP_RST |
530 SOR_PLL_0_PLLREG_LEVEL_V45 | SOR_PLL_0_RESISTOR_EXT;
531 tegra_sor_writel(sor, value, SOR_PLL_0);
533 value = tegra_sor_readl(sor, SOR_PLL_2);
534 value |= SOR_PLL_2_SEQ_PLLCAPPD;
535 value &= ~SOR_PLL_2_SEQ_PLLCAPPD_ENFORCE;
536 value |= SOR_PLL_2_LVDS_ENABLE;
537 tegra_sor_writel(sor, value, SOR_PLL_2);
539 value = SOR_PLL_1_TERM_COMPOUT | SOR_PLL_1_TMDS_TERM;
540 tegra_sor_writel(sor, value, SOR_PLL_1);
543 value = tegra_sor_readl(sor, SOR_PLL_2);
544 if ((value & SOR_PLL_2_SEQ_PLLCAPPD_ENFORCE) == 0)
547 usleep_range(250, 1000);
550 value = tegra_sor_readl(sor, SOR_PLL_2);
551 value &= ~SOR_PLL_2_POWERDOWN_OVERRIDE;
552 value &= ~SOR_PLL_2_PORT_POWERDOWN;
553 tegra_sor_writel(sor, value, SOR_PLL_2);
559 /* set safe link bandwidth (1.62 Gbps) */
560 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
561 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
562 value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G1_62;
563 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
566 value = tegra_sor_readl(sor, SOR_PLL_2);
567 value |= SOR_PLL_2_SEQ_PLLCAPPD_ENFORCE | SOR_PLL_2_PORT_POWERDOWN |
568 SOR_PLL_2_BANDGAP_POWERDOWN;
569 tegra_sor_writel(sor, value, SOR_PLL_2);
571 value = tegra_sor_readl(sor, SOR_PLL_0);
572 value |= SOR_PLL_0_VCOPD | SOR_PLL_0_POWER_OFF;
573 tegra_sor_writel(sor, value, SOR_PLL_0);
575 value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
576 value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
577 tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
580 err = tegra_io_rail_power_on(TEGRA_IO_RAIL_LVDS);
582 dev_err(sor->dev, "failed to power on I/O rail: %d\n", err);
586 usleep_range(5, 100);
589 value = tegra_sor_readl(sor, SOR_PLL_2);
590 value &= ~SOR_PLL_2_BANDGAP_POWERDOWN;
591 tegra_sor_writel(sor, value, SOR_PLL_2);
593 usleep_range(20, 100);
596 value = tegra_sor_readl(sor, SOR_PLL_0);
597 value &= ~SOR_PLL_0_POWER_OFF;
598 value &= ~SOR_PLL_0_VCOPD;
599 tegra_sor_writel(sor, value, SOR_PLL_0);
601 value = tegra_sor_readl(sor, SOR_PLL_2);
602 value &= ~SOR_PLL_2_SEQ_PLLCAPPD_ENFORCE;
603 tegra_sor_writel(sor, value, SOR_PLL_2);
605 usleep_range(200, 1000);
608 value = tegra_sor_readl(sor, SOR_PLL_2);
609 value &= ~SOR_PLL_2_PORT_POWERDOWN;
610 tegra_sor_writel(sor, value, SOR_PLL_2);
612 /* switch to DP clock */
613 err = clk_set_parent(sor->clk, sor->clk_dp);
615 dev_err(sor->dev, "failed to set DP parent clock: %d\n", err);
617 /* power dplanes (XXX parameterize based on link?) */
618 value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
619 value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
620 SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2;
621 tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
623 value = tegra_sor_readl(sor, SOR_DP_LINKCTL_0);
624 value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
625 value |= SOR_DP_LINKCTL_LANE_COUNT(4);
626 tegra_sor_writel(sor, value, SOR_DP_LINKCTL_0);
628 /* start lane sequencer */
629 value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
630 SOR_LANE_SEQ_CTL_POWER_STATE_UP;
631 tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
634 value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
635 if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
638 usleep_range(250, 1000);
641 /* set link bandwidth (2.7 GHz, XXX: parameterize based on link?) */
642 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
643 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
644 value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G2_70;
645 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
648 value = tegra_sor_readl(sor, SOR_DP_LINKCTL_0);
649 value |= SOR_DP_LINKCTL_ENABLE;
651 value &= ~SOR_DP_LINKCTL_TU_SIZE_MASK;
652 value |= SOR_DP_LINKCTL_TU_SIZE(config.tu_size);
654 value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
655 tegra_sor_writel(sor, value, SOR_DP_LINKCTL_0);
657 for (i = 0, value = 0; i < 4; i++) {
658 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
659 SOR_DP_TPG_SCRAMBLER_GALIOS |
660 SOR_DP_TPG_PATTERN_NONE;
661 value = (value << 8) | lane;
664 tegra_sor_writel(sor, value, SOR_DP_TPG);
666 value = tegra_sor_readl(sor, SOR_DP_CONFIG_0);
667 value &= ~SOR_DP_CONFIG_WATERMARK_MASK;
668 value |= SOR_DP_CONFIG_WATERMARK(config.watermark);
670 value &= ~SOR_DP_CONFIG_ACTIVE_SYM_COUNT_MASK;
671 value |= SOR_DP_CONFIG_ACTIVE_SYM_COUNT(config.active_count);
673 value &= ~SOR_DP_CONFIG_ACTIVE_SYM_FRAC_MASK;
674 value |= SOR_DP_CONFIG_ACTIVE_SYM_FRAC(config.active_frac);
676 if (config.active_polarity)
677 value |= SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
679 value &= ~SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
681 value |= SOR_DP_CONFIG_ACTIVE_SYM_ENABLE;
682 value |= SOR_DP_CONFIG_DISPARITY_NEGATIVE; /* XXX: don't hardcode? */
683 tegra_sor_writel(sor, value, SOR_DP_CONFIG_0);
685 value = tegra_sor_readl(sor, SOR_DP_AUDIO_HBLANK_SYMBOLS);
686 value &= ~SOR_DP_AUDIO_HBLANK_SYMBOLS_MASK;
687 value |= 137; /* XXX: don't hardcode? */
688 tegra_sor_writel(sor, value, SOR_DP_AUDIO_HBLANK_SYMBOLS);
690 value = tegra_sor_readl(sor, SOR_DP_AUDIO_VBLANK_SYMBOLS);
691 value &= ~SOR_DP_AUDIO_VBLANK_SYMBOLS_MASK;
692 value |= 2368; /* XXX: don't hardcode? */
693 tegra_sor_writel(sor, value, SOR_DP_AUDIO_VBLANK_SYMBOLS);
695 /* enable pad calibration logic */
696 value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
697 value |= SOR_DP_PADCTL_PAD_CAL_PD;
698 tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
703 err = drm_dp_link_probe(aux, &link);
705 dev_err(sor->dev, "failed to probe eDP link: %d\n",
710 err = drm_dp_link_power_up(aux, &link);
712 dev_err(sor->dev, "failed to power up eDP link: %d\n",
717 err = drm_dp_link_configure(aux, &link);
719 dev_err(sor->dev, "failed to configure eDP link: %d\n",
724 rate = drm_dp_link_rate_to_bw_code(link.rate);
725 lanes = link.num_lanes;
727 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
728 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
729 value |= SOR_CLK_CNTRL_DP_LINK_SPEED(rate);
730 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
732 value = tegra_sor_readl(sor, SOR_DP_LINKCTL_0);
733 value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
734 value |= SOR_DP_LINKCTL_LANE_COUNT(lanes);
736 if (link.capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
737 value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
739 tegra_sor_writel(sor, value, SOR_DP_LINKCTL_0);
741 /* disable training pattern generator */
743 for (i = 0; i < link.num_lanes; i++) {
744 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
745 SOR_DP_TPG_SCRAMBLER_GALIOS |
746 SOR_DP_TPG_PATTERN_NONE;
747 value = (value << 8) | lane;
750 tegra_sor_writel(sor, value, SOR_DP_TPG);
752 err = tegra_sor_dp_train_fast(sor, &link);
754 dev_err(sor->dev, "DP fast link training failed: %d\n",
759 dev_dbg(sor->dev, "fast link training succeeded\n");
762 err = tegra_sor_power_up(sor, 250);
764 dev_err(sor->dev, "failed to power up SOR: %d\n", err);
768 /* start display controller in continuous mode */
769 value = tegra_dc_readl(dc, DC_CMD_STATE_ACCESS);
771 tegra_dc_writel(dc, value, DC_CMD_STATE_ACCESS);
773 tegra_dc_writel(dc, VSYNC_H_POSITION(1), DC_DISP_DISP_TIMING_OPTIONS);
774 tegra_dc_writel(dc, DISP_CTRL_MODE_C_DISPLAY, DC_CMD_DISPLAY_COMMAND);
776 value = tegra_dc_readl(dc, DC_CMD_STATE_ACCESS);
778 tegra_dc_writel(dc, value, DC_CMD_STATE_ACCESS);
781 * configure panel (24bpp, vsync-, hsync-, DP-A protocol, complete
782 * raster, associate with display controller)
784 value = SOR_STATE_ASY_VSYNCPOL |
785 SOR_STATE_ASY_HSYNCPOL |
786 SOR_STATE_ASY_PROTOCOL_DP_A |
787 SOR_STATE_ASY_CRC_MODE_COMPLETE |
788 SOR_STATE_ASY_OWNER(dc->pipe + 1);
790 switch (config.bits_per_pixel) {
792 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
796 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_18_444;
804 tegra_sor_writel(sor, value, SOR_STATE_1);
807 * TODO: The video timing programming below doesn't seem to match the
808 * register definitions.
811 value = ((mode->vtotal & 0x7fff) << 16) | (mode->htotal & 0x7fff);
812 tegra_sor_writel(sor, value, SOR_HEAD_STATE_1(0));
814 vse = mode->vsync_end - mode->vsync_start - 1;
815 hse = mode->hsync_end - mode->hsync_start - 1;
817 value = ((vse & 0x7fff) << 16) | (hse & 0x7fff);
818 tegra_sor_writel(sor, value, SOR_HEAD_STATE_2(0));
820 vbe = vse + (mode->vsync_start - mode->vdisplay);
821 hbe = hse + (mode->hsync_start - mode->hdisplay);
823 value = ((vbe & 0x7fff) << 16) | (hbe & 0x7fff);
824 tegra_sor_writel(sor, value, SOR_HEAD_STATE_3(0));
826 vbs = vbe + mode->vdisplay;
827 hbs = hbe + mode->hdisplay;
829 value = ((vbs & 0x7fff) << 16) | (hbs & 0x7fff);
830 tegra_sor_writel(sor, value, SOR_HEAD_STATE_4(0));
832 /* XXX interlaced mode */
833 tegra_sor_writel(sor, 0x00000001, SOR_HEAD_STATE_5(0));
835 /* CSTM (LVDS, link A/B, upper) */
836 value = SOR_CSTM_LVDS | SOR_CSTM_LINK_ACT_A | SOR_CSTM_LINK_ACT_B |
838 tegra_sor_writel(sor, value, SOR_CSTM);
841 err = tegra_sor_setup_pwm(sor, 250);
843 dev_err(sor->dev, "failed to setup PWM: %d\n", err);
847 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
849 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
851 tegra_sor_update(sor);
853 err = tegra_sor_attach(sor);
855 dev_err(sor->dev, "failed to attach SOR: %d\n", err);
859 err = tegra_sor_wakeup(sor);
861 dev_err(sor->dev, "failed to enable DC: %d\n", err);
868 mutex_unlock(&sor->lock);
872 static int tegra_sor_detach(struct tegra_sor *sor)
874 unsigned long value, timeout;
876 /* switch to safe mode */
877 value = tegra_sor_readl(sor, SOR_SUPER_STATE_1);
878 value &= ~SOR_SUPER_STATE_MODE_NORMAL;
879 tegra_sor_writel(sor, value, SOR_SUPER_STATE_1);
880 tegra_sor_super_update(sor);
882 timeout = jiffies + msecs_to_jiffies(250);
884 while (time_before(jiffies, timeout)) {
885 value = tegra_sor_readl(sor, SOR_PWR);
886 if (value & SOR_PWR_MODE_SAFE)
890 if ((value & SOR_PWR_MODE_SAFE) == 0)
894 value = tegra_sor_readl(sor, SOR_SUPER_STATE_1);
895 value &= ~SOR_SUPER_STATE_HEAD_MODE_MASK;
896 tegra_sor_writel(sor, value, SOR_SUPER_STATE_1);
897 tegra_sor_super_update(sor);
900 value = tegra_sor_readl(sor, SOR_SUPER_STATE_1);
901 value &= ~SOR_SUPER_STATE_ATTACHED;
902 tegra_sor_writel(sor, value, SOR_SUPER_STATE_1);
903 tegra_sor_super_update(sor);
905 timeout = jiffies + msecs_to_jiffies(250);
907 while (time_before(jiffies, timeout)) {
908 value = tegra_sor_readl(sor, SOR_TEST);
909 if ((value & SOR_TEST_ATTACHED) == 0)
912 usleep_range(25, 100);
915 if ((value & SOR_TEST_ATTACHED) != 0)
921 static int tegra_sor_power_down(struct tegra_sor *sor)
923 unsigned long value, timeout;
926 value = tegra_sor_readl(sor, SOR_PWR);
927 value &= ~SOR_PWR_NORMAL_STATE_PU;
928 value |= SOR_PWR_TRIGGER;
929 tegra_sor_writel(sor, value, SOR_PWR);
931 timeout = jiffies + msecs_to_jiffies(250);
933 while (time_before(jiffies, timeout)) {
934 value = tegra_sor_readl(sor, SOR_PWR);
935 if ((value & SOR_PWR_TRIGGER) == 0)
938 usleep_range(25, 100);
941 if ((value & SOR_PWR_TRIGGER) != 0)
944 err = clk_set_parent(sor->clk, sor->clk_safe);
946 dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
948 value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
949 value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
950 SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2);
951 tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
953 /* stop lane sequencer */
954 value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_UP |
955 SOR_LANE_SEQ_CTL_POWER_STATE_DOWN;
956 tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
958 timeout = jiffies + msecs_to_jiffies(250);
960 while (time_before(jiffies, timeout)) {
961 value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
962 if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
965 usleep_range(25, 100);
968 if ((value & SOR_LANE_SEQ_CTL_TRIGGER) != 0)
971 value = tegra_sor_readl(sor, SOR_PLL_2);
972 value |= SOR_PLL_2_PORT_POWERDOWN;
973 tegra_sor_writel(sor, value, SOR_PLL_2);
975 usleep_range(20, 100);
977 value = tegra_sor_readl(sor, SOR_PLL_0);
978 value |= SOR_PLL_0_POWER_OFF;
979 value |= SOR_PLL_0_VCOPD;
980 tegra_sor_writel(sor, value, SOR_PLL_0);
982 value = tegra_sor_readl(sor, SOR_PLL_2);
983 value |= SOR_PLL_2_SEQ_PLLCAPPD;
984 value |= SOR_PLL_2_SEQ_PLLCAPPD_ENFORCE;
985 tegra_sor_writel(sor, value, SOR_PLL_2);
987 usleep_range(20, 100);
992 static int tegra_output_sor_disable(struct tegra_output *output)
994 struct tegra_dc *dc = to_tegra_dc(output->encoder.crtc);
995 struct tegra_sor *sor = to_sor(output);
999 mutex_lock(&sor->lock);
1004 err = tegra_sor_detach(sor);
1006 dev_err(sor->dev, "failed to detach SOR: %d\n", err);
1010 tegra_sor_writel(sor, 0, SOR_STATE_1);
1011 tegra_sor_update(sor);
1014 * The following accesses registers of the display controller, so make
1015 * sure it's only executed when the output is attached to one.
1019 * XXX: We can't do this here because it causes the SOR to go
1020 * into an erroneous state and the output will look scrambled
1021 * the next time it is enabled. Presumably this is because we
1022 * should be doing this only on the next VBLANK. A possible
1023 * solution would be to queue a "power-off" event to trigger
1024 * this code to be run during the next VBLANK.
1027 value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
1028 value &= ~(PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
1029 PW4_ENABLE | PM0_ENABLE | PM1_ENABLE);
1030 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
1033 value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
1034 value &= ~DISP_CTRL_MODE_MASK;
1035 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
1037 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
1038 value &= ~SOR_ENABLE;
1039 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
1041 tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
1042 tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
1045 err = tegra_sor_power_down(sor);
1047 dev_err(sor->dev, "failed to power down SOR: %d\n", err);
1052 err = tegra_dpaux_disable(sor->dpaux);
1054 dev_err(sor->dev, "failed to disable DP: %d\n", err);
1059 err = tegra_io_rail_power_off(TEGRA_IO_RAIL_LVDS);
1061 dev_err(sor->dev, "failed to power off I/O rail: %d\n", err);
1065 reset_control_assert(sor->rst);
1066 clk_disable_unprepare(sor->clk);
1068 sor->enabled = false;
1071 mutex_unlock(&sor->lock);
1075 static int tegra_output_sor_setup_clock(struct tegra_output *output,
1076 struct clk *clk, unsigned long pclk,
1079 struct tegra_sor *sor = to_sor(output);
1082 err = clk_set_parent(clk, sor->clk_parent);
1084 dev_err(sor->dev, "failed to set parent clock: %d\n", err);
1088 err = clk_set_rate(sor->clk_parent, pclk);
1090 dev_err(sor->dev, "failed to set clock rate to %lu Hz\n", pclk);
1099 static int tegra_output_sor_check_mode(struct tegra_output *output,
1100 struct drm_display_mode *mode,
1101 enum drm_mode_status *status)
1104 * FIXME: For now, always assume that the mode is okay.
1112 static enum drm_connector_status
1113 tegra_output_sor_detect(struct tegra_output *output)
1115 struct tegra_sor *sor = to_sor(output);
1118 return tegra_dpaux_detect(sor->dpaux);
1120 return connector_status_unknown;
1123 static const struct tegra_output_ops sor_ops = {
1124 .enable = tegra_output_sor_enable,
1125 .disable = tegra_output_sor_disable,
1126 .setup_clock = tegra_output_sor_setup_clock,
1127 .check_mode = tegra_output_sor_check_mode,
1128 .detect = tegra_output_sor_detect,
1131 static int tegra_sor_crc_open(struct inode *inode, struct file *file)
1133 file->private_data = inode->i_private;
1138 static int tegra_sor_crc_release(struct inode *inode, struct file *file)
1143 static int tegra_sor_crc_wait(struct tegra_sor *sor, unsigned long timeout)
1147 timeout = jiffies + msecs_to_jiffies(timeout);
1149 while (time_before(jiffies, timeout)) {
1150 value = tegra_sor_readl(sor, SOR_CRC_A);
1151 if (value & SOR_CRC_A_VALID)
1154 usleep_range(100, 200);
1160 static ssize_t tegra_sor_crc_read(struct file *file, char __user *buffer,
1161 size_t size, loff_t *ppos)
1163 struct tegra_sor *sor = file->private_data;
1168 mutex_lock(&sor->lock);
1170 if (!sor->enabled) {
1175 value = tegra_sor_readl(sor, SOR_STATE_1);
1176 value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
1177 tegra_sor_writel(sor, value, SOR_STATE_1);
1179 value = tegra_sor_readl(sor, SOR_CRC_CNTRL);
1180 value |= SOR_CRC_CNTRL_ENABLE;
1181 tegra_sor_writel(sor, value, SOR_CRC_CNTRL);
1183 value = tegra_sor_readl(sor, SOR_TEST);
1184 value &= ~SOR_TEST_CRC_POST_SERIALIZE;
1185 tegra_sor_writel(sor, value, SOR_TEST);
1187 err = tegra_sor_crc_wait(sor, 100);
1191 tegra_sor_writel(sor, SOR_CRC_A_RESET, SOR_CRC_A);
1192 value = tegra_sor_readl(sor, SOR_CRC_B);
1194 num = scnprintf(buf, sizeof(buf), "%08x\n", value);
1196 err = simple_read_from_buffer(buffer, size, ppos, buf, num);
1199 mutex_unlock(&sor->lock);
1203 static const struct file_operations tegra_sor_crc_fops = {
1204 .owner = THIS_MODULE,
1205 .open = tegra_sor_crc_open,
1206 .read = tegra_sor_crc_read,
1207 .release = tegra_sor_crc_release,
1210 static int tegra_sor_debugfs_init(struct tegra_sor *sor,
1211 struct drm_minor *minor)
1213 struct dentry *entry;
1216 sor->debugfs = debugfs_create_dir("sor", minor->debugfs_root);
1220 entry = debugfs_create_file("crc", 0644, sor->debugfs, sor,
1221 &tegra_sor_crc_fops);
1224 "cannot create /sys/kernel/debug/dri/%s/sor/crc\n",
1225 minor->debugfs_root->d_name.name);
1233 debugfs_remove(sor->debugfs);
1234 sor->debugfs = NULL;
1238 static int tegra_sor_debugfs_exit(struct tegra_sor *sor)
1240 debugfs_remove_recursive(sor->debugfs);
1241 sor->debugfs = NULL;
1246 static int tegra_sor_init(struct host1x_client *client)
1248 struct drm_device *drm = dev_get_drvdata(client->parent);
1249 struct tegra_sor *sor = host1x_client_to_sor(client);
1255 sor->output.type = TEGRA_OUTPUT_EDP;
1257 sor->output.dev = sor->dev;
1258 sor->output.ops = &sor_ops;
1260 err = tegra_output_init(drm, &sor->output);
1262 dev_err(sor->dev, "output setup failed: %d\n", err);
1266 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
1267 err = tegra_sor_debugfs_init(sor, drm->primary);
1269 dev_err(sor->dev, "debugfs setup failed: %d\n", err);
1273 err = tegra_dpaux_attach(sor->dpaux, &sor->output);
1275 dev_err(sor->dev, "failed to attach DP: %d\n", err);
1283 static int tegra_sor_exit(struct host1x_client *client)
1285 struct tegra_sor *sor = host1x_client_to_sor(client);
1288 err = tegra_output_disable(&sor->output);
1290 dev_err(sor->dev, "output failed to disable: %d\n", err);
1295 err = tegra_dpaux_detach(sor->dpaux);
1297 dev_err(sor->dev, "failed to detach DP: %d\n", err);
1302 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
1303 err = tegra_sor_debugfs_exit(sor);
1305 dev_err(sor->dev, "debugfs cleanup failed: %d\n", err);
1308 err = tegra_output_exit(&sor->output);
1310 dev_err(sor->dev, "output cleanup failed: %d\n", err);
1317 static const struct host1x_client_ops sor_client_ops = {
1318 .init = tegra_sor_init,
1319 .exit = tegra_sor_exit,
1322 static int tegra_sor_probe(struct platform_device *pdev)
1324 struct device_node *np;
1325 struct tegra_sor *sor;
1326 struct resource *regs;
1329 sor = devm_kzalloc(&pdev->dev, sizeof(*sor), GFP_KERNEL);
1333 sor->output.dev = sor->dev = &pdev->dev;
1335 np = of_parse_phandle(pdev->dev.of_node, "nvidia,dpaux", 0);
1337 sor->dpaux = tegra_dpaux_find_by_of_node(np);
1341 return -EPROBE_DEFER;
1344 err = tegra_output_probe(&sor->output);
1348 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1349 sor->regs = devm_ioremap_resource(&pdev->dev, regs);
1350 if (IS_ERR(sor->regs))
1351 return PTR_ERR(sor->regs);
1353 sor->rst = devm_reset_control_get(&pdev->dev, "sor");
1354 if (IS_ERR(sor->rst))
1355 return PTR_ERR(sor->rst);
1357 sor->clk = devm_clk_get(&pdev->dev, NULL);
1358 if (IS_ERR(sor->clk))
1359 return PTR_ERR(sor->clk);
1361 sor->clk_parent = devm_clk_get(&pdev->dev, "parent");
1362 if (IS_ERR(sor->clk_parent))
1363 return PTR_ERR(sor->clk_parent);
1365 err = clk_prepare_enable(sor->clk_parent);
1369 sor->clk_safe = devm_clk_get(&pdev->dev, "safe");
1370 if (IS_ERR(sor->clk_safe))
1371 return PTR_ERR(sor->clk_safe);
1373 err = clk_prepare_enable(sor->clk_safe);
1377 sor->clk_dp = devm_clk_get(&pdev->dev, "dp");
1378 if (IS_ERR(sor->clk_dp))
1379 return PTR_ERR(sor->clk_dp);
1381 err = clk_prepare_enable(sor->clk_dp);
1385 INIT_LIST_HEAD(&sor->client.list);
1386 sor->client.ops = &sor_client_ops;
1387 sor->client.dev = &pdev->dev;
1389 mutex_init(&sor->lock);
1391 err = host1x_client_register(&sor->client);
1393 dev_err(&pdev->dev, "failed to register host1x client: %d\n",
1398 platform_set_drvdata(pdev, sor);
1403 static int tegra_sor_remove(struct platform_device *pdev)
1405 struct tegra_sor *sor = platform_get_drvdata(pdev);
1408 err = host1x_client_unregister(&sor->client);
1410 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
1415 clk_disable_unprepare(sor->clk_parent);
1416 clk_disable_unprepare(sor->clk_safe);
1417 clk_disable_unprepare(sor->clk_dp);
1418 clk_disable_unprepare(sor->clk);
1423 static const struct of_device_id tegra_sor_of_match[] = {
1424 { .compatible = "nvidia,tegra124-sor", },
1428 struct platform_driver tegra_sor_driver = {
1430 .name = "tegra-sor",
1431 .of_match_table = tegra_sor_of_match,
1433 .probe = tegra_sor_probe,
1434 .remove = tegra_sor_remove,