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/clk-provider.h>
11 #include <linux/debugfs.h>
12 #include <linux/gpio.h>
14 #include <linux/of_device.h>
15 #include <linux/platform_device.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/reset.h>
20 #include <soc/tegra/pmc.h>
22 #include <drm/drm_atomic_helper.h>
23 #include <drm/drm_dp_helper.h>
24 #include <drm/drm_panel.h>
25 #include <drm/drm_scdc_helper.h>
33 #define SOR_REKEY 0x38
35 struct tegra_sor_hdmi_settings {
36 unsigned long frequency;
55 static const struct tegra_sor_hdmi_settings tegra210_sor_hdmi_defaults[] = {
57 .frequency = 54000000,
69 .drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
70 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
72 .frequency = 75000000,
84 .drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
85 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
87 .frequency = 150000000,
99 .drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
100 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
102 .frequency = 300000000,
110 .bg_vref_level = 0xa,
114 .drive_current = { 0x33, 0x3f, 0x3f, 0x3f },
115 .preemphasis = { 0x00, 0x17, 0x17, 0x17 },
117 .frequency = 600000000,
125 .bg_vref_level = 0x8,
129 .drive_current = { 0x33, 0x3f, 0x3f, 0x3f },
130 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
134 static const struct tegra_sor_hdmi_settings tegra210_sor_hdmi_defaults[] = {
136 .frequency = 75000000,
144 .bg_vref_level = 0x8,
148 .drive_current = { 0x29, 0x29, 0x29, 0x29 },
149 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
151 .frequency = 150000000,
159 .bg_vref_level = 0x8,
163 .drive_current = { 0x30, 0x37, 0x37, 0x37 },
164 .preemphasis = { 0x01, 0x02, 0x02, 0x02 },
166 .frequency = 300000000,
174 .bg_vref_level = 0xf,
178 .drive_current = { 0x30, 0x37, 0x37, 0x37 },
179 .preemphasis = { 0x10, 0x3e, 0x3e, 0x3e },
181 .frequency = 600000000,
189 .bg_vref_level = 0xe,
193 .drive_current = { 0x35, 0x3e, 0x3e, 0x3e },
194 .preemphasis = { 0x02, 0x3f, 0x3f, 0x3f },
199 static const struct tegra_sor_hdmi_settings tegra186_sor_hdmi_defaults[] = {
201 .frequency = 54000000,
213 .drive_current = { 0x3a, 0x3a, 0x3a, 0x33 },
214 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
216 .frequency = 75000000,
228 .drive_current = { 0x3a, 0x3a, 0x3a, 0x33 },
229 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
231 .frequency = 150000000,
237 .tx_pu_value = 0x66 /* 0 */,
242 .sparepll = 0x00, /* 0x34 */
243 .drive_current = { 0x3a, 0x3a, 0x3a, 0x37 },
244 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
246 .frequency = 300000000,
258 .drive_current = { 0x3d, 0x3d, 0x3d, 0x33 },
259 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
261 .frequency = 600000000,
273 .drive_current = { 0x3d, 0x3d, 0x3d, 0x33 },
274 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
278 static const struct tegra_sor_hdmi_settings tegra194_sor_hdmi_defaults[] = {
280 .frequency = 54000000,
292 .drive_current = { 0x3a, 0x3a, 0x3a, 0x33 },
293 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
295 .frequency = 75000000,
307 .drive_current = { 0x3a, 0x3a, 0x3a, 0x33 },
308 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
310 .frequency = 150000000,
316 .tx_pu_value = 0x66 /* 0 */,
321 .sparepll = 0x00, /* 0x34 */
322 .drive_current = { 0x3a, 0x3a, 0x3a, 0x37 },
323 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
325 .frequency = 300000000,
337 .drive_current = { 0x3d, 0x3d, 0x3d, 0x33 },
338 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
340 .frequency = 600000000,
352 .drive_current = { 0x3d, 0x3d, 0x3d, 0x33 },
353 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
357 struct tegra_sor_regs {
358 unsigned int head_state0;
359 unsigned int head_state1;
360 unsigned int head_state2;
361 unsigned int head_state3;
362 unsigned int head_state4;
363 unsigned int head_state5;
368 unsigned int dp_padctl0;
369 unsigned int dp_padctl2;
372 struct tegra_sor_soc {
378 const struct tegra_sor_regs *regs;
381 const struct tegra_sor_hdmi_settings *settings;
382 unsigned int num_settings;
389 struct tegra_sor_ops {
391 int (*probe)(struct tegra_sor *sor);
392 int (*remove)(struct tegra_sor *sor);
396 struct host1x_client client;
397 struct tegra_output output;
400 const struct tegra_sor_soc *soc;
405 struct reset_control *rst;
406 struct clk *clk_parent;
407 struct clk *clk_safe;
415 struct drm_dp_aux *aux;
417 struct drm_info_list *debugfs_files;
419 const struct tegra_sor_ops *ops;
420 enum tegra_io_pad pad;
423 struct tegra_sor_hdmi_settings *settings;
424 unsigned int num_settings;
426 struct regulator *avdd_io_supply;
427 struct regulator *vdd_pll_supply;
428 struct regulator *hdmi_supply;
430 struct delayed_work scdc;
433 struct tegra_hda_format format;
436 struct tegra_sor_state {
437 struct drm_connector_state base;
439 unsigned int link_speed;
444 static inline struct tegra_sor_state *
445 to_sor_state(struct drm_connector_state *state)
447 return container_of(state, struct tegra_sor_state, base);
450 struct tegra_sor_config {
463 static inline struct tegra_sor *
464 host1x_client_to_sor(struct host1x_client *client)
466 return container_of(client, struct tegra_sor, client);
469 static inline struct tegra_sor *to_sor(struct tegra_output *output)
471 return container_of(output, struct tegra_sor, output);
474 static inline u32 tegra_sor_readl(struct tegra_sor *sor, unsigned int offset)
476 u32 value = readl(sor->regs + (offset << 2));
478 trace_sor_readl(sor->dev, offset, value);
483 static inline void tegra_sor_writel(struct tegra_sor *sor, u32 value,
486 trace_sor_writel(sor->dev, offset, value);
487 writel(value, sor->regs + (offset << 2));
490 static int tegra_sor_set_parent_clock(struct tegra_sor *sor, struct clk *parent)
494 clk_disable_unprepare(sor->clk);
496 err = clk_set_parent(sor->clk_out, parent);
500 err = clk_prepare_enable(sor->clk);
507 struct tegra_clk_sor_pad {
509 struct tegra_sor *sor;
512 static inline struct tegra_clk_sor_pad *to_pad(struct clk_hw *hw)
514 return container_of(hw, struct tegra_clk_sor_pad, hw);
517 static const char * const tegra_clk_sor_pad_parents[] = {
518 "pll_d2_out0", "pll_dp"
521 static int tegra_clk_sor_pad_set_parent(struct clk_hw *hw, u8 index)
523 struct tegra_clk_sor_pad *pad = to_pad(hw);
524 struct tegra_sor *sor = pad->sor;
527 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
528 value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
532 value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK;
536 value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
540 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
545 static u8 tegra_clk_sor_pad_get_parent(struct clk_hw *hw)
547 struct tegra_clk_sor_pad *pad = to_pad(hw);
548 struct tegra_sor *sor = pad->sor;
552 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
554 switch (value & SOR_CLK_CNTRL_DP_CLK_SEL_MASK) {
555 case SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK:
556 case SOR_CLK_CNTRL_DP_CLK_SEL_DIFF_PCLK:
560 case SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK:
561 case SOR_CLK_CNTRL_DP_CLK_SEL_DIFF_DPCLK:
569 static const struct clk_ops tegra_clk_sor_pad_ops = {
570 .set_parent = tegra_clk_sor_pad_set_parent,
571 .get_parent = tegra_clk_sor_pad_get_parent,
574 static struct clk *tegra_clk_sor_pad_register(struct tegra_sor *sor,
577 struct tegra_clk_sor_pad *pad;
578 struct clk_init_data init;
581 pad = devm_kzalloc(sor->dev, sizeof(*pad), GFP_KERNEL);
583 return ERR_PTR(-ENOMEM);
589 init.parent_names = tegra_clk_sor_pad_parents;
590 init.num_parents = ARRAY_SIZE(tegra_clk_sor_pad_parents);
591 init.ops = &tegra_clk_sor_pad_ops;
593 pad->hw.init = &init;
595 clk = devm_clk_register(sor->dev, &pad->hw);
600 static int tegra_sor_dp_train_fast(struct tegra_sor *sor,
601 struct drm_dp_link *link)
608 /* setup lane parameters */
609 value = SOR_LANE_DRIVE_CURRENT_LANE3(0x40) |
610 SOR_LANE_DRIVE_CURRENT_LANE2(0x40) |
611 SOR_LANE_DRIVE_CURRENT_LANE1(0x40) |
612 SOR_LANE_DRIVE_CURRENT_LANE0(0x40);
613 tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT0);
615 value = SOR_LANE_PREEMPHASIS_LANE3(0x0f) |
616 SOR_LANE_PREEMPHASIS_LANE2(0x0f) |
617 SOR_LANE_PREEMPHASIS_LANE1(0x0f) |
618 SOR_LANE_PREEMPHASIS_LANE0(0x0f);
619 tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS0);
621 value = SOR_LANE_POSTCURSOR_LANE3(0x00) |
622 SOR_LANE_POSTCURSOR_LANE2(0x00) |
623 SOR_LANE_POSTCURSOR_LANE1(0x00) |
624 SOR_LANE_POSTCURSOR_LANE0(0x00);
625 tegra_sor_writel(sor, value, SOR_LANE_POSTCURSOR0);
627 /* disable LVDS mode */
628 tegra_sor_writel(sor, 0, SOR_LVDS);
630 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
631 value |= SOR_DP_PADCTL_TX_PU_ENABLE;
632 value &= ~SOR_DP_PADCTL_TX_PU_MASK;
633 value |= SOR_DP_PADCTL_TX_PU(2); /* XXX: don't hardcode? */
634 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
636 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
637 value |= SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
638 SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0;
639 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
641 usleep_range(10, 100);
643 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
644 value &= ~(SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
645 SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0);
646 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
648 err = drm_dp_aux_prepare(sor->aux, DP_SET_ANSI_8B10B);
652 for (i = 0, value = 0; i < link->num_lanes; i++) {
653 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
654 SOR_DP_TPG_SCRAMBLER_NONE |
655 SOR_DP_TPG_PATTERN_TRAIN1;
656 value = (value << 8) | lane;
659 tegra_sor_writel(sor, value, SOR_DP_TPG);
661 pattern = DP_TRAINING_PATTERN_1;
663 err = drm_dp_aux_train(sor->aux, link, pattern);
667 value = tegra_sor_readl(sor, SOR_DP_SPARE0);
668 value |= SOR_DP_SPARE_SEQ_ENABLE;
669 value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
670 value |= SOR_DP_SPARE_MACRO_SOR_CLK;
671 tegra_sor_writel(sor, value, SOR_DP_SPARE0);
673 for (i = 0, value = 0; i < link->num_lanes; i++) {
674 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
675 SOR_DP_TPG_SCRAMBLER_NONE |
676 SOR_DP_TPG_PATTERN_TRAIN2;
677 value = (value << 8) | lane;
680 tegra_sor_writel(sor, value, SOR_DP_TPG);
682 pattern = DP_LINK_SCRAMBLING_DISABLE | DP_TRAINING_PATTERN_2;
684 err = drm_dp_aux_train(sor->aux, link, pattern);
688 for (i = 0, value = 0; i < link->num_lanes; i++) {
689 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
690 SOR_DP_TPG_SCRAMBLER_GALIOS |
691 SOR_DP_TPG_PATTERN_NONE;
692 value = (value << 8) | lane;
695 tegra_sor_writel(sor, value, SOR_DP_TPG);
697 pattern = DP_TRAINING_PATTERN_DISABLE;
699 err = drm_dp_aux_train(sor->aux, link, pattern);
706 static void tegra_sor_super_update(struct tegra_sor *sor)
708 tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
709 tegra_sor_writel(sor, 1, SOR_SUPER_STATE0);
710 tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
713 static void tegra_sor_update(struct tegra_sor *sor)
715 tegra_sor_writel(sor, 0, SOR_STATE0);
716 tegra_sor_writel(sor, 1, SOR_STATE0);
717 tegra_sor_writel(sor, 0, SOR_STATE0);
720 static int tegra_sor_setup_pwm(struct tegra_sor *sor, unsigned long timeout)
724 value = tegra_sor_readl(sor, SOR_PWM_DIV);
725 value &= ~SOR_PWM_DIV_MASK;
726 value |= 0x400; /* period */
727 tegra_sor_writel(sor, value, SOR_PWM_DIV);
729 value = tegra_sor_readl(sor, SOR_PWM_CTL);
730 value &= ~SOR_PWM_CTL_DUTY_CYCLE_MASK;
731 value |= 0x400; /* duty cycle */
732 value &= ~SOR_PWM_CTL_CLK_SEL; /* clock source: PCLK */
733 value |= SOR_PWM_CTL_TRIGGER;
734 tegra_sor_writel(sor, value, SOR_PWM_CTL);
736 timeout = jiffies + msecs_to_jiffies(timeout);
738 while (time_before(jiffies, timeout)) {
739 value = tegra_sor_readl(sor, SOR_PWM_CTL);
740 if ((value & SOR_PWM_CTL_TRIGGER) == 0)
743 usleep_range(25, 100);
749 static int tegra_sor_attach(struct tegra_sor *sor)
751 unsigned long value, timeout;
753 /* wake up in normal mode */
754 value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
755 value |= SOR_SUPER_STATE_HEAD_MODE_AWAKE;
756 value |= SOR_SUPER_STATE_MODE_NORMAL;
757 tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
758 tegra_sor_super_update(sor);
761 value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
762 value |= SOR_SUPER_STATE_ATTACHED;
763 tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
764 tegra_sor_super_update(sor);
766 timeout = jiffies + msecs_to_jiffies(250);
768 while (time_before(jiffies, timeout)) {
769 value = tegra_sor_readl(sor, SOR_TEST);
770 if ((value & SOR_TEST_ATTACHED) != 0)
773 usleep_range(25, 100);
779 static int tegra_sor_wakeup(struct tegra_sor *sor)
781 unsigned long value, timeout;
783 timeout = jiffies + msecs_to_jiffies(250);
785 /* wait for head to wake up */
786 while (time_before(jiffies, timeout)) {
787 value = tegra_sor_readl(sor, SOR_TEST);
788 value &= SOR_TEST_HEAD_MODE_MASK;
790 if (value == SOR_TEST_HEAD_MODE_AWAKE)
793 usleep_range(25, 100);
799 static int tegra_sor_power_up(struct tegra_sor *sor, unsigned long timeout)
803 value = tegra_sor_readl(sor, SOR_PWR);
804 value |= SOR_PWR_TRIGGER | SOR_PWR_NORMAL_STATE_PU;
805 tegra_sor_writel(sor, value, SOR_PWR);
807 timeout = jiffies + msecs_to_jiffies(timeout);
809 while (time_before(jiffies, timeout)) {
810 value = tegra_sor_readl(sor, SOR_PWR);
811 if ((value & SOR_PWR_TRIGGER) == 0)
814 usleep_range(25, 100);
820 struct tegra_sor_params {
821 /* number of link clocks per line */
822 unsigned int num_clocks;
823 /* ratio between input and output */
825 /* precision factor */
828 unsigned int active_polarity;
829 unsigned int active_count;
830 unsigned int active_frac;
831 unsigned int tu_size;
835 static int tegra_sor_compute_params(struct tegra_sor *sor,
836 struct tegra_sor_params *params,
837 unsigned int tu_size)
839 u64 active_sym, active_count, frac, approx;
840 u32 active_polarity, active_frac = 0;
841 const u64 f = params->precision;
844 active_sym = params->ratio * tu_size;
845 active_count = div_u64(active_sym, f) * f;
846 frac = active_sym - active_count;
849 if (frac >= (f / 2)) {
857 frac = div_u64(f * f, frac); /* 1/fraction */
858 if (frac <= (15 * f)) {
859 active_frac = div_u64(frac, f);
865 active_frac = active_polarity ? 1 : 15;
869 if (active_frac == 1)
872 if (active_polarity == 1) {
874 approx = active_count + (active_frac * (f - 1)) * f;
875 approx = div_u64(approx, active_frac * f);
877 approx = active_count + f;
881 approx = active_count + div_u64(f, active_frac);
883 approx = active_count;
886 error = div_s64(active_sym - approx, tu_size);
887 error *= params->num_clocks;
889 if (error <= 0 && abs(error) < params->error) {
890 params->active_count = div_u64(active_count, f);
891 params->active_polarity = active_polarity;
892 params->active_frac = active_frac;
893 params->error = abs(error);
894 params->tu_size = tu_size;
903 static int tegra_sor_compute_config(struct tegra_sor *sor,
904 const struct drm_display_mode *mode,
905 struct tegra_sor_config *config,
906 struct drm_dp_link *link)
908 const u64 f = 100000, link_rate = link->rate * 1000;
909 const u64 pclk = mode->clock * 1000;
910 u64 input, output, watermark, num;
911 struct tegra_sor_params params;
912 u32 num_syms_per_line;
915 if (!link_rate || !link->num_lanes || !pclk || !config->bits_per_pixel)
918 output = link_rate * 8 * link->num_lanes;
919 input = pclk * config->bits_per_pixel;
924 memset(¶ms, 0, sizeof(params));
925 params.ratio = div64_u64(input * f, output);
926 params.num_clocks = div_u64(link_rate * mode->hdisplay, pclk);
927 params.precision = f;
928 params.error = 64 * f;
931 for (i = params.tu_size; i >= 32; i--)
932 if (tegra_sor_compute_params(sor, ¶ms, i))
935 if (params.active_frac == 0) {
936 config->active_polarity = 0;
937 config->active_count = params.active_count;
939 if (!params.active_polarity)
940 config->active_count--;
942 config->tu_size = params.tu_size;
943 config->active_frac = 1;
945 config->active_polarity = params.active_polarity;
946 config->active_count = params.active_count;
947 config->active_frac = params.active_frac;
948 config->tu_size = params.tu_size;
952 "polarity: %d active count: %d tu size: %d active frac: %d\n",
953 config->active_polarity, config->active_count,
954 config->tu_size, config->active_frac);
956 watermark = params.ratio * config->tu_size * (f - params.ratio);
957 watermark = div_u64(watermark, f);
959 watermark = div_u64(watermark + params.error, f);
960 config->watermark = watermark + (config->bits_per_pixel / 8) + 2;
961 num_syms_per_line = (mode->hdisplay * config->bits_per_pixel) *
962 (link->num_lanes * 8);
964 if (config->watermark > 30) {
965 config->watermark = 30;
967 "unable to compute TU size, forcing watermark to %u\n",
969 } else if (config->watermark > num_syms_per_line) {
970 config->watermark = num_syms_per_line;
971 dev_err(sor->dev, "watermark too high, forcing to %u\n",
975 /* compute the number of symbols per horizontal blanking interval */
976 num = ((mode->htotal - mode->hdisplay) - 7) * link_rate;
977 config->hblank_symbols = div_u64(num, pclk);
979 if (link->capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
980 config->hblank_symbols -= 3;
982 config->hblank_symbols -= 12 / link->num_lanes;
984 /* compute the number of symbols per vertical blanking interval */
985 num = (mode->hdisplay - 25) * link_rate;
986 config->vblank_symbols = div_u64(num, pclk);
987 config->vblank_symbols -= 36 / link->num_lanes + 4;
989 dev_dbg(sor->dev, "blank symbols: H:%u V:%u\n", config->hblank_symbols,
990 config->vblank_symbols);
995 static void tegra_sor_apply_config(struct tegra_sor *sor,
996 const struct tegra_sor_config *config)
1000 value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
1001 value &= ~SOR_DP_LINKCTL_TU_SIZE_MASK;
1002 value |= SOR_DP_LINKCTL_TU_SIZE(config->tu_size);
1003 tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
1005 value = tegra_sor_readl(sor, SOR_DP_CONFIG0);
1006 value &= ~SOR_DP_CONFIG_WATERMARK_MASK;
1007 value |= SOR_DP_CONFIG_WATERMARK(config->watermark);
1009 value &= ~SOR_DP_CONFIG_ACTIVE_SYM_COUNT_MASK;
1010 value |= SOR_DP_CONFIG_ACTIVE_SYM_COUNT(config->active_count);
1012 value &= ~SOR_DP_CONFIG_ACTIVE_SYM_FRAC_MASK;
1013 value |= SOR_DP_CONFIG_ACTIVE_SYM_FRAC(config->active_frac);
1015 if (config->active_polarity)
1016 value |= SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
1018 value &= ~SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
1020 value |= SOR_DP_CONFIG_ACTIVE_SYM_ENABLE;
1021 value |= SOR_DP_CONFIG_DISPARITY_NEGATIVE;
1022 tegra_sor_writel(sor, value, SOR_DP_CONFIG0);
1024 value = tegra_sor_readl(sor, SOR_DP_AUDIO_HBLANK_SYMBOLS);
1025 value &= ~SOR_DP_AUDIO_HBLANK_SYMBOLS_MASK;
1026 value |= config->hblank_symbols & 0xffff;
1027 tegra_sor_writel(sor, value, SOR_DP_AUDIO_HBLANK_SYMBOLS);
1029 value = tegra_sor_readl(sor, SOR_DP_AUDIO_VBLANK_SYMBOLS);
1030 value &= ~SOR_DP_AUDIO_VBLANK_SYMBOLS_MASK;
1031 value |= config->vblank_symbols & 0xffff;
1032 tegra_sor_writel(sor, value, SOR_DP_AUDIO_VBLANK_SYMBOLS);
1035 static void tegra_sor_mode_set(struct tegra_sor *sor,
1036 const struct drm_display_mode *mode,
1037 struct tegra_sor_state *state)
1039 struct tegra_dc *dc = to_tegra_dc(sor->output.encoder.crtc);
1040 unsigned int vbe, vse, hbe, hse, vbs, hbs;
1043 value = tegra_sor_readl(sor, SOR_STATE1);
1044 value &= ~SOR_STATE_ASY_PIXELDEPTH_MASK;
1045 value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
1046 value &= ~SOR_STATE_ASY_OWNER_MASK;
1048 value |= SOR_STATE_ASY_CRC_MODE_COMPLETE |
1049 SOR_STATE_ASY_OWNER(dc->pipe + 1);
1051 if (mode->flags & DRM_MODE_FLAG_PHSYNC)
1052 value &= ~SOR_STATE_ASY_HSYNCPOL;
1054 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1055 value |= SOR_STATE_ASY_HSYNCPOL;
1057 if (mode->flags & DRM_MODE_FLAG_PVSYNC)
1058 value &= ~SOR_STATE_ASY_VSYNCPOL;
1060 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1061 value |= SOR_STATE_ASY_VSYNCPOL;
1063 switch (state->bpc) {
1065 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_48_444;
1069 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_36_444;
1073 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_30_444;
1077 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
1081 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_18_444;
1085 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
1089 tegra_sor_writel(sor, value, SOR_STATE1);
1092 * TODO: The video timing programming below doesn't seem to match the
1093 * register definitions.
1096 value = ((mode->vtotal & 0x7fff) << 16) | (mode->htotal & 0x7fff);
1097 tegra_sor_writel(sor, value, sor->soc->regs->head_state1 + dc->pipe);
1099 /* sync end = sync width - 1 */
1100 vse = mode->vsync_end - mode->vsync_start - 1;
1101 hse = mode->hsync_end - mode->hsync_start - 1;
1103 value = ((vse & 0x7fff) << 16) | (hse & 0x7fff);
1104 tegra_sor_writel(sor, value, sor->soc->regs->head_state2 + dc->pipe);
1106 /* blank end = sync end + back porch */
1107 vbe = vse + (mode->vtotal - mode->vsync_end);
1108 hbe = hse + (mode->htotal - mode->hsync_end);
1110 value = ((vbe & 0x7fff) << 16) | (hbe & 0x7fff);
1111 tegra_sor_writel(sor, value, sor->soc->regs->head_state3 + dc->pipe);
1113 /* blank start = blank end + active */
1114 vbs = vbe + mode->vdisplay;
1115 hbs = hbe + mode->hdisplay;
1117 value = ((vbs & 0x7fff) << 16) | (hbs & 0x7fff);
1118 tegra_sor_writel(sor, value, sor->soc->regs->head_state4 + dc->pipe);
1120 /* XXX interlacing support */
1121 tegra_sor_writel(sor, 0x001, sor->soc->regs->head_state5 + dc->pipe);
1124 static int tegra_sor_detach(struct tegra_sor *sor)
1126 unsigned long value, timeout;
1128 /* switch to safe mode */
1129 value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
1130 value &= ~SOR_SUPER_STATE_MODE_NORMAL;
1131 tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
1132 tegra_sor_super_update(sor);
1134 timeout = jiffies + msecs_to_jiffies(250);
1136 while (time_before(jiffies, timeout)) {
1137 value = tegra_sor_readl(sor, SOR_PWR);
1138 if (value & SOR_PWR_MODE_SAFE)
1142 if ((value & SOR_PWR_MODE_SAFE) == 0)
1146 value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
1147 value &= ~SOR_SUPER_STATE_HEAD_MODE_MASK;
1148 tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
1149 tegra_sor_super_update(sor);
1152 value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
1153 value &= ~SOR_SUPER_STATE_ATTACHED;
1154 tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
1155 tegra_sor_super_update(sor);
1157 timeout = jiffies + msecs_to_jiffies(250);
1159 while (time_before(jiffies, timeout)) {
1160 value = tegra_sor_readl(sor, SOR_TEST);
1161 if ((value & SOR_TEST_ATTACHED) == 0)
1164 usleep_range(25, 100);
1167 if ((value & SOR_TEST_ATTACHED) != 0)
1173 static int tegra_sor_power_down(struct tegra_sor *sor)
1175 unsigned long value, timeout;
1178 value = tegra_sor_readl(sor, SOR_PWR);
1179 value &= ~SOR_PWR_NORMAL_STATE_PU;
1180 value |= SOR_PWR_TRIGGER;
1181 tegra_sor_writel(sor, value, SOR_PWR);
1183 timeout = jiffies + msecs_to_jiffies(250);
1185 while (time_before(jiffies, timeout)) {
1186 value = tegra_sor_readl(sor, SOR_PWR);
1187 if ((value & SOR_PWR_TRIGGER) == 0)
1190 usleep_range(25, 100);
1193 if ((value & SOR_PWR_TRIGGER) != 0)
1196 /* switch to safe parent clock */
1197 err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
1199 dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
1203 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
1204 value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
1205 SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2);
1206 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
1208 /* stop lane sequencer */
1209 value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_UP |
1210 SOR_LANE_SEQ_CTL_POWER_STATE_DOWN;
1211 tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
1213 timeout = jiffies + msecs_to_jiffies(250);
1215 while (time_before(jiffies, timeout)) {
1216 value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
1217 if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
1220 usleep_range(25, 100);
1223 if ((value & SOR_LANE_SEQ_CTL_TRIGGER) != 0)
1226 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1227 value |= SOR_PLL2_PORT_POWERDOWN;
1228 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
1230 usleep_range(20, 100);
1232 value = tegra_sor_readl(sor, sor->soc->regs->pll0);
1233 value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
1234 tegra_sor_writel(sor, value, sor->soc->regs->pll0);
1236 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1237 value |= SOR_PLL2_SEQ_PLLCAPPD;
1238 value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1239 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
1241 usleep_range(20, 100);
1246 static int tegra_sor_crc_wait(struct tegra_sor *sor, unsigned long timeout)
1250 timeout = jiffies + msecs_to_jiffies(timeout);
1252 while (time_before(jiffies, timeout)) {
1253 value = tegra_sor_readl(sor, SOR_CRCA);
1254 if (value & SOR_CRCA_VALID)
1257 usleep_range(100, 200);
1263 static int tegra_sor_show_crc(struct seq_file *s, void *data)
1265 struct drm_info_node *node = s->private;
1266 struct tegra_sor *sor = node->info_ent->data;
1267 struct drm_crtc *crtc = sor->output.encoder.crtc;
1268 struct drm_device *drm = node->minor->dev;
1272 drm_modeset_lock_all(drm);
1274 if (!crtc || !crtc->state->active) {
1279 value = tegra_sor_readl(sor, SOR_STATE1);
1280 value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
1281 tegra_sor_writel(sor, value, SOR_STATE1);
1283 value = tegra_sor_readl(sor, SOR_CRC_CNTRL);
1284 value |= SOR_CRC_CNTRL_ENABLE;
1285 tegra_sor_writel(sor, value, SOR_CRC_CNTRL);
1287 value = tegra_sor_readl(sor, SOR_TEST);
1288 value &= ~SOR_TEST_CRC_POST_SERIALIZE;
1289 tegra_sor_writel(sor, value, SOR_TEST);
1291 err = tegra_sor_crc_wait(sor, 100);
1295 tegra_sor_writel(sor, SOR_CRCA_RESET, SOR_CRCA);
1296 value = tegra_sor_readl(sor, SOR_CRCB);
1298 seq_printf(s, "%08x\n", value);
1301 drm_modeset_unlock_all(drm);
1305 #define DEBUGFS_REG32(_name) { .name = #_name, .offset = _name }
1307 static const struct debugfs_reg32 tegra_sor_regs[] = {
1308 DEBUGFS_REG32(SOR_CTXSW),
1309 DEBUGFS_REG32(SOR_SUPER_STATE0),
1310 DEBUGFS_REG32(SOR_SUPER_STATE1),
1311 DEBUGFS_REG32(SOR_STATE0),
1312 DEBUGFS_REG32(SOR_STATE1),
1313 DEBUGFS_REG32(SOR_HEAD_STATE0(0)),
1314 DEBUGFS_REG32(SOR_HEAD_STATE0(1)),
1315 DEBUGFS_REG32(SOR_HEAD_STATE1(0)),
1316 DEBUGFS_REG32(SOR_HEAD_STATE1(1)),
1317 DEBUGFS_REG32(SOR_HEAD_STATE2(0)),
1318 DEBUGFS_REG32(SOR_HEAD_STATE2(1)),
1319 DEBUGFS_REG32(SOR_HEAD_STATE3(0)),
1320 DEBUGFS_REG32(SOR_HEAD_STATE3(1)),
1321 DEBUGFS_REG32(SOR_HEAD_STATE4(0)),
1322 DEBUGFS_REG32(SOR_HEAD_STATE4(1)),
1323 DEBUGFS_REG32(SOR_HEAD_STATE5(0)),
1324 DEBUGFS_REG32(SOR_HEAD_STATE5(1)),
1325 DEBUGFS_REG32(SOR_CRC_CNTRL),
1326 DEBUGFS_REG32(SOR_DP_DEBUG_MVID),
1327 DEBUGFS_REG32(SOR_CLK_CNTRL),
1328 DEBUGFS_REG32(SOR_CAP),
1329 DEBUGFS_REG32(SOR_PWR),
1330 DEBUGFS_REG32(SOR_TEST),
1331 DEBUGFS_REG32(SOR_PLL0),
1332 DEBUGFS_REG32(SOR_PLL1),
1333 DEBUGFS_REG32(SOR_PLL2),
1334 DEBUGFS_REG32(SOR_PLL3),
1335 DEBUGFS_REG32(SOR_CSTM),
1336 DEBUGFS_REG32(SOR_LVDS),
1337 DEBUGFS_REG32(SOR_CRCA),
1338 DEBUGFS_REG32(SOR_CRCB),
1339 DEBUGFS_REG32(SOR_BLANK),
1340 DEBUGFS_REG32(SOR_SEQ_CTL),
1341 DEBUGFS_REG32(SOR_LANE_SEQ_CTL),
1342 DEBUGFS_REG32(SOR_SEQ_INST(0)),
1343 DEBUGFS_REG32(SOR_SEQ_INST(1)),
1344 DEBUGFS_REG32(SOR_SEQ_INST(2)),
1345 DEBUGFS_REG32(SOR_SEQ_INST(3)),
1346 DEBUGFS_REG32(SOR_SEQ_INST(4)),
1347 DEBUGFS_REG32(SOR_SEQ_INST(5)),
1348 DEBUGFS_REG32(SOR_SEQ_INST(6)),
1349 DEBUGFS_REG32(SOR_SEQ_INST(7)),
1350 DEBUGFS_REG32(SOR_SEQ_INST(8)),
1351 DEBUGFS_REG32(SOR_SEQ_INST(9)),
1352 DEBUGFS_REG32(SOR_SEQ_INST(10)),
1353 DEBUGFS_REG32(SOR_SEQ_INST(11)),
1354 DEBUGFS_REG32(SOR_SEQ_INST(12)),
1355 DEBUGFS_REG32(SOR_SEQ_INST(13)),
1356 DEBUGFS_REG32(SOR_SEQ_INST(14)),
1357 DEBUGFS_REG32(SOR_SEQ_INST(15)),
1358 DEBUGFS_REG32(SOR_PWM_DIV),
1359 DEBUGFS_REG32(SOR_PWM_CTL),
1360 DEBUGFS_REG32(SOR_VCRC_A0),
1361 DEBUGFS_REG32(SOR_VCRC_A1),
1362 DEBUGFS_REG32(SOR_VCRC_B0),
1363 DEBUGFS_REG32(SOR_VCRC_B1),
1364 DEBUGFS_REG32(SOR_CCRC_A0),
1365 DEBUGFS_REG32(SOR_CCRC_A1),
1366 DEBUGFS_REG32(SOR_CCRC_B0),
1367 DEBUGFS_REG32(SOR_CCRC_B1),
1368 DEBUGFS_REG32(SOR_EDATA_A0),
1369 DEBUGFS_REG32(SOR_EDATA_A1),
1370 DEBUGFS_REG32(SOR_EDATA_B0),
1371 DEBUGFS_REG32(SOR_EDATA_B1),
1372 DEBUGFS_REG32(SOR_COUNT_A0),
1373 DEBUGFS_REG32(SOR_COUNT_A1),
1374 DEBUGFS_REG32(SOR_COUNT_B0),
1375 DEBUGFS_REG32(SOR_COUNT_B1),
1376 DEBUGFS_REG32(SOR_DEBUG_A0),
1377 DEBUGFS_REG32(SOR_DEBUG_A1),
1378 DEBUGFS_REG32(SOR_DEBUG_B0),
1379 DEBUGFS_REG32(SOR_DEBUG_B1),
1380 DEBUGFS_REG32(SOR_TRIG),
1381 DEBUGFS_REG32(SOR_MSCHECK),
1382 DEBUGFS_REG32(SOR_XBAR_CTRL),
1383 DEBUGFS_REG32(SOR_XBAR_POL),
1384 DEBUGFS_REG32(SOR_DP_LINKCTL0),
1385 DEBUGFS_REG32(SOR_DP_LINKCTL1),
1386 DEBUGFS_REG32(SOR_LANE_DRIVE_CURRENT0),
1387 DEBUGFS_REG32(SOR_LANE_DRIVE_CURRENT1),
1388 DEBUGFS_REG32(SOR_LANE4_DRIVE_CURRENT0),
1389 DEBUGFS_REG32(SOR_LANE4_DRIVE_CURRENT1),
1390 DEBUGFS_REG32(SOR_LANE_PREEMPHASIS0),
1391 DEBUGFS_REG32(SOR_LANE_PREEMPHASIS1),
1392 DEBUGFS_REG32(SOR_LANE4_PREEMPHASIS0),
1393 DEBUGFS_REG32(SOR_LANE4_PREEMPHASIS1),
1394 DEBUGFS_REG32(SOR_LANE_POSTCURSOR0),
1395 DEBUGFS_REG32(SOR_LANE_POSTCURSOR1),
1396 DEBUGFS_REG32(SOR_DP_CONFIG0),
1397 DEBUGFS_REG32(SOR_DP_CONFIG1),
1398 DEBUGFS_REG32(SOR_DP_MN0),
1399 DEBUGFS_REG32(SOR_DP_MN1),
1400 DEBUGFS_REG32(SOR_DP_PADCTL0),
1401 DEBUGFS_REG32(SOR_DP_PADCTL1),
1402 DEBUGFS_REG32(SOR_DP_PADCTL2),
1403 DEBUGFS_REG32(SOR_DP_DEBUG0),
1404 DEBUGFS_REG32(SOR_DP_DEBUG1),
1405 DEBUGFS_REG32(SOR_DP_SPARE0),
1406 DEBUGFS_REG32(SOR_DP_SPARE1),
1407 DEBUGFS_REG32(SOR_DP_AUDIO_CTRL),
1408 DEBUGFS_REG32(SOR_DP_AUDIO_HBLANK_SYMBOLS),
1409 DEBUGFS_REG32(SOR_DP_AUDIO_VBLANK_SYMBOLS),
1410 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_HEADER),
1411 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK0),
1412 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK1),
1413 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK2),
1414 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK3),
1415 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK4),
1416 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK5),
1417 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK6),
1418 DEBUGFS_REG32(SOR_DP_TPG),
1419 DEBUGFS_REG32(SOR_DP_TPG_CONFIG),
1420 DEBUGFS_REG32(SOR_DP_LQ_CSTM0),
1421 DEBUGFS_REG32(SOR_DP_LQ_CSTM1),
1422 DEBUGFS_REG32(SOR_DP_LQ_CSTM2),
1425 static int tegra_sor_show_regs(struct seq_file *s, void *data)
1427 struct drm_info_node *node = s->private;
1428 struct tegra_sor *sor = node->info_ent->data;
1429 struct drm_crtc *crtc = sor->output.encoder.crtc;
1430 struct drm_device *drm = node->minor->dev;
1434 drm_modeset_lock_all(drm);
1436 if (!crtc || !crtc->state->active) {
1441 for (i = 0; i < ARRAY_SIZE(tegra_sor_regs); i++) {
1442 unsigned int offset = tegra_sor_regs[i].offset;
1444 seq_printf(s, "%-38s %#05x %08x\n", tegra_sor_regs[i].name,
1445 offset, tegra_sor_readl(sor, offset));
1449 drm_modeset_unlock_all(drm);
1453 static const struct drm_info_list debugfs_files[] = {
1454 { "crc", tegra_sor_show_crc, 0, NULL },
1455 { "regs", tegra_sor_show_regs, 0, NULL },
1458 static int tegra_sor_late_register(struct drm_connector *connector)
1460 struct tegra_output *output = connector_to_output(connector);
1461 unsigned int i, count = ARRAY_SIZE(debugfs_files);
1462 struct drm_minor *minor = connector->dev->primary;
1463 struct dentry *root = connector->debugfs_entry;
1464 struct tegra_sor *sor = to_sor(output);
1467 sor->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1469 if (!sor->debugfs_files)
1472 for (i = 0; i < count; i++)
1473 sor->debugfs_files[i].data = sor;
1475 err = drm_debugfs_create_files(sor->debugfs_files, count, root, minor);
1482 kfree(sor->debugfs_files);
1483 sor->debugfs_files = NULL;
1488 static void tegra_sor_early_unregister(struct drm_connector *connector)
1490 struct tegra_output *output = connector_to_output(connector);
1491 unsigned int count = ARRAY_SIZE(debugfs_files);
1492 struct tegra_sor *sor = to_sor(output);
1494 drm_debugfs_remove_files(sor->debugfs_files, count,
1495 connector->dev->primary);
1496 kfree(sor->debugfs_files);
1497 sor->debugfs_files = NULL;
1500 static void tegra_sor_connector_reset(struct drm_connector *connector)
1502 struct tegra_sor_state *state;
1504 state = kzalloc(sizeof(*state), GFP_KERNEL);
1508 if (connector->state) {
1509 __drm_atomic_helper_connector_destroy_state(connector->state);
1510 kfree(connector->state);
1513 __drm_atomic_helper_connector_reset(connector, &state->base);
1516 static enum drm_connector_status
1517 tegra_sor_connector_detect(struct drm_connector *connector, bool force)
1519 struct tegra_output *output = connector_to_output(connector);
1520 struct tegra_sor *sor = to_sor(output);
1523 return drm_dp_aux_detect(sor->aux);
1525 return tegra_output_connector_detect(connector, force);
1528 static struct drm_connector_state *
1529 tegra_sor_connector_duplicate_state(struct drm_connector *connector)
1531 struct tegra_sor_state *state = to_sor_state(connector->state);
1532 struct tegra_sor_state *copy;
1534 copy = kmemdup(state, sizeof(*state), GFP_KERNEL);
1538 __drm_atomic_helper_connector_duplicate_state(connector, ©->base);
1543 static const struct drm_connector_funcs tegra_sor_connector_funcs = {
1544 .reset = tegra_sor_connector_reset,
1545 .detect = tegra_sor_connector_detect,
1546 .fill_modes = drm_helper_probe_single_connector_modes,
1547 .destroy = tegra_output_connector_destroy,
1548 .atomic_duplicate_state = tegra_sor_connector_duplicate_state,
1549 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1550 .late_register = tegra_sor_late_register,
1551 .early_unregister = tegra_sor_early_unregister,
1554 static int tegra_sor_connector_get_modes(struct drm_connector *connector)
1556 struct tegra_output *output = connector_to_output(connector);
1557 struct tegra_sor *sor = to_sor(output);
1561 drm_dp_aux_enable(sor->aux);
1563 err = tegra_output_connector_get_modes(connector);
1566 drm_dp_aux_disable(sor->aux);
1571 static enum drm_mode_status
1572 tegra_sor_connector_mode_valid(struct drm_connector *connector,
1573 struct drm_display_mode *mode)
1578 static const struct drm_connector_helper_funcs tegra_sor_connector_helper_funcs = {
1579 .get_modes = tegra_sor_connector_get_modes,
1580 .mode_valid = tegra_sor_connector_mode_valid,
1583 static const struct drm_encoder_funcs tegra_sor_encoder_funcs = {
1584 .destroy = tegra_output_encoder_destroy,
1587 static void tegra_sor_edp_disable(struct drm_encoder *encoder)
1589 struct tegra_output *output = encoder_to_output(encoder);
1590 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
1591 struct tegra_sor *sor = to_sor(output);
1596 drm_panel_disable(output->panel);
1598 err = tegra_sor_detach(sor);
1600 dev_err(sor->dev, "failed to detach SOR: %d\n", err);
1602 tegra_sor_writel(sor, 0, SOR_STATE1);
1603 tegra_sor_update(sor);
1606 * The following accesses registers of the display controller, so make
1607 * sure it's only executed when the output is attached to one.
1610 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
1611 value &= ~SOR_ENABLE(0);
1612 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
1614 tegra_dc_commit(dc);
1617 err = tegra_sor_power_down(sor);
1619 dev_err(sor->dev, "failed to power down SOR: %d\n", err);
1622 err = drm_dp_aux_disable(sor->aux);
1624 dev_err(sor->dev, "failed to disable DP: %d\n", err);
1627 err = tegra_io_pad_power_disable(sor->pad);
1629 dev_err(sor->dev, "failed to power off I/O pad: %d\n", err);
1632 drm_panel_unprepare(output->panel);
1634 pm_runtime_put(sor->dev);
1638 static int calc_h_ref_to_sync(const struct drm_display_mode *mode,
1639 unsigned int *value)
1641 unsigned int hfp, hsw, hbp, a = 0, b;
1643 hfp = mode->hsync_start - mode->hdisplay;
1644 hsw = mode->hsync_end - mode->hsync_start;
1645 hbp = mode->htotal - mode->hsync_end;
1647 pr_info("hfp: %u, hsw: %u, hbp: %u\n", hfp, hsw, hbp);
1651 pr_info("a: %u, b: %u\n", a, b);
1652 pr_info("a + hsw + hbp = %u\n", a + hsw + hbp);
1654 if (a + hsw + hbp <= 11) {
1655 a = 1 + 11 - hsw - hbp;
1656 pr_info("a: %u\n", a);
1665 if (mode->hdisplay < 16)
1679 static void tegra_sor_edp_enable(struct drm_encoder *encoder)
1681 struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
1682 struct tegra_output *output = encoder_to_output(encoder);
1683 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
1684 struct tegra_sor *sor = to_sor(output);
1685 struct tegra_sor_config config;
1686 struct tegra_sor_state *state;
1687 struct drm_dp_link link;
1693 state = to_sor_state(output->connector.state);
1695 pm_runtime_get_sync(sor->dev);
1698 drm_panel_prepare(output->panel);
1700 err = drm_dp_aux_enable(sor->aux);
1702 dev_err(sor->dev, "failed to enable DP: %d\n", err);
1704 err = drm_dp_link_probe(sor->aux, &link);
1706 dev_err(sor->dev, "failed to probe eDP link: %d\n", err);
1710 /* switch to safe parent clock */
1711 err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
1713 dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
1715 memset(&config, 0, sizeof(config));
1716 config.bits_per_pixel = state->bpc * 3;
1718 err = tegra_sor_compute_config(sor, mode, &config, &link);
1720 dev_err(sor->dev, "failed to compute configuration: %d\n", err);
1722 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
1723 value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
1724 value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
1725 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
1727 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1728 value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
1729 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
1730 usleep_range(20, 100);
1732 value = tegra_sor_readl(sor, sor->soc->regs->pll3);
1733 value |= SOR_PLL3_PLL_VDD_MODE_3V3;
1734 tegra_sor_writel(sor, value, sor->soc->regs->pll3);
1736 value = SOR_PLL0_ICHPMP(0xf) | SOR_PLL0_VCOCAP_RST |
1737 SOR_PLL0_PLLREG_LEVEL_V45 | SOR_PLL0_RESISTOR_EXT;
1738 tegra_sor_writel(sor, value, sor->soc->regs->pll0);
1740 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1741 value |= SOR_PLL2_SEQ_PLLCAPPD;
1742 value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1743 value |= SOR_PLL2_LVDS_ENABLE;
1744 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
1746 value = SOR_PLL1_TERM_COMPOUT | SOR_PLL1_TMDS_TERM;
1747 tegra_sor_writel(sor, value, sor->soc->regs->pll1);
1750 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1751 if ((value & SOR_PLL2_SEQ_PLLCAPPD_ENFORCE) == 0)
1754 usleep_range(250, 1000);
1757 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1758 value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
1759 value &= ~SOR_PLL2_PORT_POWERDOWN;
1760 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
1766 /* set safe link bandwidth (1.62 Gbps) */
1767 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
1768 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
1769 value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G1_62;
1770 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
1773 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1774 value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE | SOR_PLL2_PORT_POWERDOWN |
1775 SOR_PLL2_BANDGAP_POWERDOWN;
1776 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
1778 value = tegra_sor_readl(sor, sor->soc->regs->pll0);
1779 value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
1780 tegra_sor_writel(sor, value, sor->soc->regs->pll0);
1782 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
1783 value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
1784 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
1787 err = tegra_io_pad_power_enable(sor->pad);
1789 dev_err(sor->dev, "failed to power on I/O pad: %d\n", err);
1791 usleep_range(5, 100);
1794 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1795 value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
1796 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
1798 usleep_range(20, 100);
1801 value = tegra_sor_readl(sor, sor->soc->regs->pll0);
1802 value &= ~SOR_PLL0_VCOPD;
1803 value &= ~SOR_PLL0_PWR;
1804 tegra_sor_writel(sor, value, sor->soc->regs->pll0);
1806 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1807 value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1808 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
1810 usleep_range(200, 1000);
1813 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
1814 value &= ~SOR_PLL2_PORT_POWERDOWN;
1815 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
1817 /* XXX not in TRM */
1818 for (value = 0, i = 0; i < 5; i++)
1819 value |= SOR_XBAR_CTRL_LINK0_XSEL(i, sor->xbar_cfg[i]) |
1820 SOR_XBAR_CTRL_LINK1_XSEL(i, i);
1822 tegra_sor_writel(sor, 0x00000000, SOR_XBAR_POL);
1823 tegra_sor_writel(sor, value, SOR_XBAR_CTRL);
1825 /* switch to DP parent clock */
1826 err = tegra_sor_set_parent_clock(sor, sor->clk_dp);
1828 dev_err(sor->dev, "failed to set parent clock: %d\n", err);
1830 /* power DP lanes */
1831 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
1833 if (link.num_lanes <= 2)
1834 value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2);
1836 value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2;
1838 if (link.num_lanes <= 1)
1839 value &= ~SOR_DP_PADCTL_PD_TXD_1;
1841 value |= SOR_DP_PADCTL_PD_TXD_1;
1843 if (link.num_lanes == 0)
1844 value &= ~SOR_DP_PADCTL_PD_TXD_0;
1846 value |= SOR_DP_PADCTL_PD_TXD_0;
1848 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
1850 value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
1851 value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
1852 value |= SOR_DP_LINKCTL_LANE_COUNT(link.num_lanes);
1853 tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
1855 /* start lane sequencer */
1856 value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
1857 SOR_LANE_SEQ_CTL_POWER_STATE_UP;
1858 tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
1861 value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
1862 if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
1865 usleep_range(250, 1000);
1868 /* set link bandwidth */
1869 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
1870 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
1871 value |= drm_dp_link_rate_to_bw_code(link.rate) << 2;
1872 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
1874 tegra_sor_apply_config(sor, &config);
1877 value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
1878 value |= SOR_DP_LINKCTL_ENABLE;
1879 value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
1880 tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
1882 for (i = 0, value = 0; i < 4; i++) {
1883 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
1884 SOR_DP_TPG_SCRAMBLER_GALIOS |
1885 SOR_DP_TPG_PATTERN_NONE;
1886 value = (value << 8) | lane;
1889 tegra_sor_writel(sor, value, SOR_DP_TPG);
1891 /* enable pad calibration logic */
1892 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
1893 value |= SOR_DP_PADCTL_PAD_CAL_PD;
1894 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
1896 err = drm_dp_link_probe(sor->aux, &link);
1898 dev_err(sor->dev, "failed to probe eDP link: %d\n", err);
1900 err = drm_dp_link_power_up(sor->aux, &link);
1902 dev_err(sor->dev, "failed to power up eDP link: %d\n", err);
1904 err = drm_dp_link_configure(sor->aux, &link);
1906 dev_err(sor->dev, "failed to configure eDP link: %d\n", err);
1908 rate = drm_dp_link_rate_to_bw_code(link.rate);
1909 lanes = link.num_lanes;
1911 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
1912 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
1913 value |= SOR_CLK_CNTRL_DP_LINK_SPEED(rate);
1914 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
1916 value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
1917 value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
1918 value |= SOR_DP_LINKCTL_LANE_COUNT(lanes);
1920 if (link.capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
1921 value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
1923 tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
1925 /* disable training pattern generator */
1927 for (i = 0; i < link.num_lanes; i++) {
1928 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
1929 SOR_DP_TPG_SCRAMBLER_GALIOS |
1930 SOR_DP_TPG_PATTERN_NONE;
1931 value = (value << 8) | lane;
1934 tegra_sor_writel(sor, value, SOR_DP_TPG);
1936 err = tegra_sor_dp_train_fast(sor, &link);
1938 dev_err(sor->dev, "DP fast link training failed: %d\n", err);
1940 dev_dbg(sor->dev, "fast link training succeeded\n");
1942 err = tegra_sor_power_up(sor, 250);
1944 dev_err(sor->dev, "failed to power up SOR: %d\n", err);
1946 /* CSTM (LVDS, link A/B, upper) */
1947 value = SOR_CSTM_LVDS | SOR_CSTM_LINK_ACT_A | SOR_CSTM_LINK_ACT_B |
1949 tegra_sor_writel(sor, value, SOR_CSTM);
1951 /* use DP-A protocol */
1952 value = tegra_sor_readl(sor, SOR_STATE1);
1953 value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
1954 value |= SOR_STATE_ASY_PROTOCOL_DP_A;
1955 tegra_sor_writel(sor, value, SOR_STATE1);
1957 tegra_sor_mode_set(sor, mode, state);
1960 err = tegra_sor_setup_pwm(sor, 250);
1962 dev_err(sor->dev, "failed to setup PWM: %d\n", err);
1964 tegra_sor_update(sor);
1966 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
1967 value |= SOR_ENABLE(0);
1968 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
1970 tegra_dc_commit(dc);
1972 err = tegra_sor_attach(sor);
1974 dev_err(sor->dev, "failed to attach SOR: %d\n", err);
1976 err = tegra_sor_wakeup(sor);
1978 dev_err(sor->dev, "failed to enable DC: %d\n", err);
1981 drm_panel_enable(output->panel);
1985 tegra_sor_encoder_atomic_check(struct drm_encoder *encoder,
1986 struct drm_crtc_state *crtc_state,
1987 struct drm_connector_state *conn_state)
1989 struct tegra_output *output = encoder_to_output(encoder);
1990 struct tegra_sor_state *state = to_sor_state(conn_state);
1991 struct tegra_dc *dc = to_tegra_dc(conn_state->crtc);
1992 unsigned long pclk = crtc_state->mode.clock * 1000;
1993 struct tegra_sor *sor = to_sor(output);
1994 struct drm_display_info *info;
1997 info = &output->connector.display_info;
2000 * For HBR2 modes, the SOR brick needs to use the x20 multiplier, so
2001 * the pixel clock must be corrected accordingly.
2003 if (pclk >= 340000000) {
2004 state->link_speed = 20;
2005 state->pclk = pclk / 2;
2007 state->link_speed = 10;
2011 err = tegra_dc_state_setup_clock(dc, crtc_state, sor->clk_parent,
2014 dev_err(output->dev, "failed to setup CRTC state: %d\n", err);
2018 switch (info->bpc) {
2021 state->bpc = info->bpc;
2025 DRM_DEBUG_KMS("%u bits-per-color not supported\n", info->bpc);
2033 static const struct drm_encoder_helper_funcs tegra_sor_edp_helpers = {
2034 .disable = tegra_sor_edp_disable,
2035 .enable = tegra_sor_edp_enable,
2036 .atomic_check = tegra_sor_encoder_atomic_check,
2039 static inline u32 tegra_sor_hdmi_subpack(const u8 *ptr, size_t size)
2044 for (i = size; i > 0; i--)
2045 value = (value << 8) | ptr[i - 1];
2050 static void tegra_sor_hdmi_write_infopack(struct tegra_sor *sor,
2051 const void *data, size_t size)
2053 const u8 *ptr = data;
2054 unsigned long offset;
2059 case HDMI_INFOFRAME_TYPE_AVI:
2060 offset = SOR_HDMI_AVI_INFOFRAME_HEADER;
2063 case HDMI_INFOFRAME_TYPE_AUDIO:
2064 offset = SOR_HDMI_AUDIO_INFOFRAME_HEADER;
2067 case HDMI_INFOFRAME_TYPE_VENDOR:
2068 offset = SOR_HDMI_VSI_INFOFRAME_HEADER;
2072 dev_err(sor->dev, "unsupported infoframe type: %02x\n",
2077 value = INFOFRAME_HEADER_TYPE(ptr[0]) |
2078 INFOFRAME_HEADER_VERSION(ptr[1]) |
2079 INFOFRAME_HEADER_LEN(ptr[2]);
2080 tegra_sor_writel(sor, value, offset);
2084 * Each subpack contains 7 bytes, divided into:
2085 * - subpack_low: bytes 0 - 3
2086 * - subpack_high: bytes 4 - 6 (with byte 7 padded to 0x00)
2088 for (i = 3, j = 0; i < size; i += 7, j += 8) {
2089 size_t rem = size - i, num = min_t(size_t, rem, 4);
2091 value = tegra_sor_hdmi_subpack(&ptr[i], num);
2092 tegra_sor_writel(sor, value, offset++);
2094 num = min_t(size_t, rem - num, 3);
2096 value = tegra_sor_hdmi_subpack(&ptr[i + 4], num);
2097 tegra_sor_writel(sor, value, offset++);
2102 tegra_sor_hdmi_setup_avi_infoframe(struct tegra_sor *sor,
2103 const struct drm_display_mode *mode)
2105 u8 buffer[HDMI_INFOFRAME_SIZE(AVI)];
2106 struct hdmi_avi_infoframe frame;
2110 /* disable AVI infoframe */
2111 value = tegra_sor_readl(sor, SOR_HDMI_AVI_INFOFRAME_CTRL);
2112 value &= ~INFOFRAME_CTRL_SINGLE;
2113 value &= ~INFOFRAME_CTRL_OTHER;
2114 value &= ~INFOFRAME_CTRL_ENABLE;
2115 tegra_sor_writel(sor, value, SOR_HDMI_AVI_INFOFRAME_CTRL);
2117 err = drm_hdmi_avi_infoframe_from_display_mode(&frame,
2118 &sor->output.connector, mode);
2120 dev_err(sor->dev, "failed to setup AVI infoframe: %d\n", err);
2124 err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
2126 dev_err(sor->dev, "failed to pack AVI infoframe: %d\n", err);
2130 tegra_sor_hdmi_write_infopack(sor, buffer, err);
2132 /* enable AVI infoframe */
2133 value = tegra_sor_readl(sor, SOR_HDMI_AVI_INFOFRAME_CTRL);
2134 value |= INFOFRAME_CTRL_CHECKSUM_ENABLE;
2135 value |= INFOFRAME_CTRL_ENABLE;
2136 tegra_sor_writel(sor, value, SOR_HDMI_AVI_INFOFRAME_CTRL);
2141 static void tegra_sor_write_eld(struct tegra_sor *sor)
2143 size_t length = drm_eld_size(sor->output.connector.eld), i;
2145 for (i = 0; i < length; i++)
2146 tegra_sor_writel(sor, i << 8 | sor->output.connector.eld[i],
2147 SOR_AUDIO_HDA_ELD_BUFWR);
2150 * The HDA codec will always report an ELD buffer size of 96 bytes and
2151 * the HDA codec driver will check that each byte read from the buffer
2152 * is valid. Therefore every byte must be written, even if no 96 bytes
2153 * were parsed from EDID.
2155 for (i = length; i < 96; i++)
2156 tegra_sor_writel(sor, i << 8 | 0, SOR_AUDIO_HDA_ELD_BUFWR);
2159 static void tegra_sor_audio_prepare(struct tegra_sor *sor)
2163 tegra_sor_write_eld(sor);
2165 value = SOR_AUDIO_HDA_PRESENSE_ELDV | SOR_AUDIO_HDA_PRESENSE_PD;
2166 tegra_sor_writel(sor, value, SOR_AUDIO_HDA_PRESENSE);
2169 static void tegra_sor_audio_unprepare(struct tegra_sor *sor)
2171 tegra_sor_writel(sor, 0, SOR_AUDIO_HDA_PRESENSE);
2174 static int tegra_sor_hdmi_enable_audio_infoframe(struct tegra_sor *sor)
2176 u8 buffer[HDMI_INFOFRAME_SIZE(AUDIO)];
2177 struct hdmi_audio_infoframe frame;
2181 err = hdmi_audio_infoframe_init(&frame);
2183 dev_err(sor->dev, "failed to setup audio infoframe: %d\n", err);
2187 frame.channels = sor->format.channels;
2189 err = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
2191 dev_err(sor->dev, "failed to pack audio infoframe: %d\n", err);
2195 tegra_sor_hdmi_write_infopack(sor, buffer, err);
2197 value = tegra_sor_readl(sor, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
2198 value |= INFOFRAME_CTRL_CHECKSUM_ENABLE;
2199 value |= INFOFRAME_CTRL_ENABLE;
2200 tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
2205 static void tegra_sor_hdmi_audio_enable(struct tegra_sor *sor)
2209 value = tegra_sor_readl(sor, SOR_AUDIO_CNTRL);
2211 /* select HDA audio input */
2212 value &= ~SOR_AUDIO_CNTRL_SOURCE_SELECT(SOURCE_SELECT_MASK);
2213 value |= SOR_AUDIO_CNTRL_SOURCE_SELECT(SOURCE_SELECT_HDA);
2215 /* inject null samples */
2216 if (sor->format.channels != 2)
2217 value &= ~SOR_AUDIO_CNTRL_INJECT_NULLSMPL;
2219 value |= SOR_AUDIO_CNTRL_INJECT_NULLSMPL;
2221 value |= SOR_AUDIO_CNTRL_AFIFO_FLUSH;
2223 tegra_sor_writel(sor, value, SOR_AUDIO_CNTRL);
2225 /* enable advertising HBR capability */
2226 tegra_sor_writel(sor, SOR_AUDIO_SPARE_HBR_ENABLE, SOR_AUDIO_SPARE);
2228 tegra_sor_writel(sor, 0, SOR_HDMI_ACR_CTRL);
2230 value = SOR_HDMI_SPARE_ACR_PRIORITY_HIGH |
2231 SOR_HDMI_SPARE_CTS_RESET(1) |
2232 SOR_HDMI_SPARE_HW_CTS_ENABLE;
2233 tegra_sor_writel(sor, value, SOR_HDMI_SPARE);
2236 value = SOR_HDMI_ACR_SUBPACK_LOW_SB1(0);
2237 tegra_sor_writel(sor, value, SOR_HDMI_ACR_0441_SUBPACK_LOW);
2239 /* allow packet to be sent */
2240 value = SOR_HDMI_ACR_SUBPACK_HIGH_ENABLE;
2241 tegra_sor_writel(sor, value, SOR_HDMI_ACR_0441_SUBPACK_HIGH);
2243 /* reset N counter and enable lookup */
2244 value = SOR_HDMI_AUDIO_N_RESET | SOR_HDMI_AUDIO_N_LOOKUP;
2245 tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_N);
2247 value = (24000 * 4096) / (128 * sor->format.sample_rate / 1000);
2248 tegra_sor_writel(sor, value, SOR_AUDIO_AVAL_0320);
2249 tegra_sor_writel(sor, 4096, SOR_AUDIO_NVAL_0320);
2251 tegra_sor_writel(sor, 20000, SOR_AUDIO_AVAL_0441);
2252 tegra_sor_writel(sor, 4704, SOR_AUDIO_NVAL_0441);
2254 tegra_sor_writel(sor, 20000, SOR_AUDIO_AVAL_0882);
2255 tegra_sor_writel(sor, 9408, SOR_AUDIO_NVAL_0882);
2257 tegra_sor_writel(sor, 20000, SOR_AUDIO_AVAL_1764);
2258 tegra_sor_writel(sor, 18816, SOR_AUDIO_NVAL_1764);
2260 value = (24000 * 6144) / (128 * sor->format.sample_rate / 1000);
2261 tegra_sor_writel(sor, value, SOR_AUDIO_AVAL_0480);
2262 tegra_sor_writel(sor, 6144, SOR_AUDIO_NVAL_0480);
2264 value = (24000 * 12288) / (128 * sor->format.sample_rate / 1000);
2265 tegra_sor_writel(sor, value, SOR_AUDIO_AVAL_0960);
2266 tegra_sor_writel(sor, 12288, SOR_AUDIO_NVAL_0960);
2268 value = (24000 * 24576) / (128 * sor->format.sample_rate / 1000);
2269 tegra_sor_writel(sor, value, SOR_AUDIO_AVAL_1920);
2270 tegra_sor_writel(sor, 24576, SOR_AUDIO_NVAL_1920);
2272 value = tegra_sor_readl(sor, SOR_HDMI_AUDIO_N);
2273 value &= ~SOR_HDMI_AUDIO_N_RESET;
2274 tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_N);
2276 tegra_sor_hdmi_enable_audio_infoframe(sor);
2279 static void tegra_sor_hdmi_disable_audio_infoframe(struct tegra_sor *sor)
2283 value = tegra_sor_readl(sor, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
2284 value &= ~INFOFRAME_CTRL_ENABLE;
2285 tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
2288 static void tegra_sor_hdmi_audio_disable(struct tegra_sor *sor)
2290 tegra_sor_hdmi_disable_audio_infoframe(sor);
2293 static struct tegra_sor_hdmi_settings *
2294 tegra_sor_hdmi_find_settings(struct tegra_sor *sor, unsigned long frequency)
2298 for (i = 0; i < sor->num_settings; i++)
2299 if (frequency <= sor->settings[i].frequency)
2300 return &sor->settings[i];
2305 static void tegra_sor_hdmi_disable_scrambling(struct tegra_sor *sor)
2309 value = tegra_sor_readl(sor, SOR_HDMI2_CTRL);
2310 value &= ~SOR_HDMI2_CTRL_CLOCK_MODE_DIV_BY_4;
2311 value &= ~SOR_HDMI2_CTRL_SCRAMBLE;
2312 tegra_sor_writel(sor, value, SOR_HDMI2_CTRL);
2315 static void tegra_sor_hdmi_scdc_disable(struct tegra_sor *sor)
2317 struct i2c_adapter *ddc = sor->output.ddc;
2319 drm_scdc_set_high_tmds_clock_ratio(ddc, false);
2320 drm_scdc_set_scrambling(ddc, false);
2322 tegra_sor_hdmi_disable_scrambling(sor);
2325 static void tegra_sor_hdmi_scdc_stop(struct tegra_sor *sor)
2327 if (sor->scdc_enabled) {
2328 cancel_delayed_work_sync(&sor->scdc);
2329 tegra_sor_hdmi_scdc_disable(sor);
2333 static void tegra_sor_hdmi_enable_scrambling(struct tegra_sor *sor)
2337 value = tegra_sor_readl(sor, SOR_HDMI2_CTRL);
2338 value |= SOR_HDMI2_CTRL_CLOCK_MODE_DIV_BY_4;
2339 value |= SOR_HDMI2_CTRL_SCRAMBLE;
2340 tegra_sor_writel(sor, value, SOR_HDMI2_CTRL);
2343 static void tegra_sor_hdmi_scdc_enable(struct tegra_sor *sor)
2345 struct i2c_adapter *ddc = sor->output.ddc;
2347 drm_scdc_set_high_tmds_clock_ratio(ddc, true);
2348 drm_scdc_set_scrambling(ddc, true);
2350 tegra_sor_hdmi_enable_scrambling(sor);
2353 static void tegra_sor_hdmi_scdc_work(struct work_struct *work)
2355 struct tegra_sor *sor = container_of(work, struct tegra_sor, scdc.work);
2356 struct i2c_adapter *ddc = sor->output.ddc;
2358 if (!drm_scdc_get_scrambling_status(ddc)) {
2359 DRM_DEBUG_KMS("SCDC not scrambled\n");
2360 tegra_sor_hdmi_scdc_enable(sor);
2363 schedule_delayed_work(&sor->scdc, msecs_to_jiffies(5000));
2366 static void tegra_sor_hdmi_scdc_start(struct tegra_sor *sor)
2368 struct drm_scdc *scdc = &sor->output.connector.display_info.hdmi.scdc;
2369 struct drm_display_mode *mode;
2371 mode = &sor->output.encoder.crtc->state->adjusted_mode;
2373 if (mode->clock >= 340000 && scdc->supported) {
2374 schedule_delayed_work(&sor->scdc, msecs_to_jiffies(5000));
2375 tegra_sor_hdmi_scdc_enable(sor);
2376 sor->scdc_enabled = true;
2380 static void tegra_sor_hdmi_disable(struct drm_encoder *encoder)
2382 struct tegra_output *output = encoder_to_output(encoder);
2383 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
2384 struct tegra_sor *sor = to_sor(output);
2388 tegra_sor_audio_unprepare(sor);
2389 tegra_sor_hdmi_scdc_stop(sor);
2391 err = tegra_sor_detach(sor);
2393 dev_err(sor->dev, "failed to detach SOR: %d\n", err);
2395 tegra_sor_writel(sor, 0, SOR_STATE1);
2396 tegra_sor_update(sor);
2398 /* disable display to SOR clock */
2399 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
2401 if (!sor->soc->has_nvdisplay)
2402 value &= ~(SOR1_TIMING_CYA | SOR_ENABLE(1));
2404 value &= ~SOR_ENABLE(sor->index);
2406 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
2408 tegra_dc_commit(dc);
2410 err = tegra_sor_power_down(sor);
2412 dev_err(sor->dev, "failed to power down SOR: %d\n", err);
2414 err = tegra_io_pad_power_disable(sor->pad);
2416 dev_err(sor->dev, "failed to power off I/O pad: %d\n", err);
2418 pm_runtime_put(sor->dev);
2421 static void tegra_sor_hdmi_enable(struct drm_encoder *encoder)
2423 struct tegra_output *output = encoder_to_output(encoder);
2424 unsigned int h_ref_to_sync = 1, pulse_start, max_ac;
2425 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
2426 struct tegra_sor_hdmi_settings *settings;
2427 struct tegra_sor *sor = to_sor(output);
2428 struct tegra_sor_state *state;
2429 struct drm_display_mode *mode;
2430 unsigned long rate, pclk;
2431 unsigned int div, i;
2435 state = to_sor_state(output->connector.state);
2436 mode = &encoder->crtc->state->adjusted_mode;
2437 pclk = mode->clock * 1000;
2439 pm_runtime_get_sync(sor->dev);
2441 /* switch to safe parent clock */
2442 err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
2444 dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
2448 div = clk_get_rate(sor->clk) / 1000000 * 4;
2450 err = tegra_io_pad_power_enable(sor->pad);
2452 dev_err(sor->dev, "failed to power on I/O pad: %d\n", err);
2454 usleep_range(20, 100);
2456 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
2457 value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
2458 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
2460 usleep_range(20, 100);
2462 value = tegra_sor_readl(sor, sor->soc->regs->pll3);
2463 value &= ~SOR_PLL3_PLL_VDD_MODE_3V3;
2464 tegra_sor_writel(sor, value, sor->soc->regs->pll3);
2466 value = tegra_sor_readl(sor, sor->soc->regs->pll0);
2467 value &= ~SOR_PLL0_VCOPD;
2468 value &= ~SOR_PLL0_PWR;
2469 tegra_sor_writel(sor, value, sor->soc->regs->pll0);
2471 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
2472 value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
2473 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
2475 usleep_range(200, 400);
2477 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
2478 value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
2479 value &= ~SOR_PLL2_PORT_POWERDOWN;
2480 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
2482 usleep_range(20, 100);
2484 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
2485 value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
2486 SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2;
2487 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
2490 value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
2491 if ((value & SOR_LANE_SEQ_CTL_STATE_BUSY) == 0)
2494 usleep_range(250, 1000);
2497 value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
2498 SOR_LANE_SEQ_CTL_POWER_STATE_UP | SOR_LANE_SEQ_CTL_DELAY(5);
2499 tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
2502 value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
2503 if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
2506 usleep_range(250, 1000);
2509 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
2510 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
2511 value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
2513 if (mode->clock < 340000) {
2514 DRM_DEBUG_KMS("setting 2.7 GHz link speed\n");
2515 value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G2_70;
2517 DRM_DEBUG_KMS("setting 5.4 GHz link speed\n");
2518 value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G5_40;
2521 value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK;
2522 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
2524 /* SOR pad PLL stabilization time */
2525 usleep_range(250, 1000);
2527 value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
2528 value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
2529 value |= SOR_DP_LINKCTL_LANE_COUNT(4);
2530 tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
2532 value = tegra_sor_readl(sor, SOR_DP_SPARE0);
2533 value &= ~SOR_DP_SPARE_DISP_VIDEO_PREAMBLE;
2534 value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
2535 value &= ~SOR_DP_SPARE_SEQ_ENABLE;
2536 value &= ~SOR_DP_SPARE_MACRO_SOR_CLK;
2537 tegra_sor_writel(sor, value, SOR_DP_SPARE0);
2539 value = SOR_SEQ_CTL_PU_PC(0) | SOR_SEQ_CTL_PU_PC_ALT(0) |
2540 SOR_SEQ_CTL_PD_PC(8) | SOR_SEQ_CTL_PD_PC_ALT(8);
2541 tegra_sor_writel(sor, value, SOR_SEQ_CTL);
2543 value = SOR_SEQ_INST_DRIVE_PWM_OUT_LO | SOR_SEQ_INST_HALT |
2544 SOR_SEQ_INST_WAIT_VSYNC | SOR_SEQ_INST_WAIT(1);
2545 tegra_sor_writel(sor, value, SOR_SEQ_INST(0));
2546 tegra_sor_writel(sor, value, SOR_SEQ_INST(8));
2548 if (!sor->soc->has_nvdisplay) {
2549 /* program the reference clock */
2550 value = SOR_REFCLK_DIV_INT(div) | SOR_REFCLK_DIV_FRAC(div);
2551 tegra_sor_writel(sor, value, SOR_REFCLK);
2554 /* XXX not in TRM */
2555 for (value = 0, i = 0; i < 5; i++)
2556 value |= SOR_XBAR_CTRL_LINK0_XSEL(i, sor->xbar_cfg[i]) |
2557 SOR_XBAR_CTRL_LINK1_XSEL(i, i);
2559 tegra_sor_writel(sor, 0x00000000, SOR_XBAR_POL);
2560 tegra_sor_writel(sor, value, SOR_XBAR_CTRL);
2562 /* switch to parent clock */
2563 err = clk_set_parent(sor->clk, sor->clk_parent);
2565 dev_err(sor->dev, "failed to set parent clock: %d\n", err);
2569 err = tegra_sor_set_parent_clock(sor, sor->clk_pad);
2571 dev_err(sor->dev, "failed to set pad clock: %d\n", err);
2575 /* adjust clock rate for HDMI 2.0 modes */
2576 rate = clk_get_rate(sor->clk_parent);
2578 if (mode->clock >= 340000)
2581 DRM_DEBUG_KMS("setting clock to %lu Hz, mode: %lu Hz\n", rate, pclk);
2583 clk_set_rate(sor->clk, rate);
2585 if (!sor->soc->has_nvdisplay) {
2586 value = SOR_INPUT_CONTROL_HDMI_SRC_SELECT(dc->pipe);
2588 /* XXX is this the proper check? */
2589 if (mode->clock < 75000)
2590 value |= SOR_INPUT_CONTROL_ARM_VIDEO_RANGE_LIMITED;
2592 tegra_sor_writel(sor, value, SOR_INPUT_CONTROL);
2595 max_ac = ((mode->htotal - mode->hdisplay) - SOR_REKEY - 18) / 32;
2597 value = SOR_HDMI_CTRL_ENABLE | SOR_HDMI_CTRL_MAX_AC_PACKET(max_ac) |
2598 SOR_HDMI_CTRL_AUDIO_LAYOUT | SOR_HDMI_CTRL_REKEY(SOR_REKEY);
2599 tegra_sor_writel(sor, value, SOR_HDMI_CTRL);
2601 if (!dc->soc->has_nvdisplay) {
2602 /* H_PULSE2 setup */
2603 pulse_start = h_ref_to_sync +
2604 (mode->hsync_end - mode->hsync_start) +
2605 (mode->htotal - mode->hsync_end) - 10;
2607 value = PULSE_LAST_END_A | PULSE_QUAL_VACTIVE |
2608 PULSE_POLARITY_HIGH | PULSE_MODE_NORMAL;
2609 tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_CONTROL);
2611 value = PULSE_END(pulse_start + 8) | PULSE_START(pulse_start);
2612 tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_POSITION_A);
2614 value = tegra_dc_readl(dc, DC_DISP_DISP_SIGNAL_OPTIONS0);
2615 value |= H_PULSE2_ENABLE;
2616 tegra_dc_writel(dc, value, DC_DISP_DISP_SIGNAL_OPTIONS0);
2619 /* infoframe setup */
2620 err = tegra_sor_hdmi_setup_avi_infoframe(sor, mode);
2622 dev_err(sor->dev, "failed to setup AVI infoframe: %d\n", err);
2624 /* XXX HDMI audio support not implemented yet */
2625 tegra_sor_hdmi_disable_audio_infoframe(sor);
2627 /* use single TMDS protocol */
2628 value = tegra_sor_readl(sor, SOR_STATE1);
2629 value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
2630 value |= SOR_STATE_ASY_PROTOCOL_SINGLE_TMDS_A;
2631 tegra_sor_writel(sor, value, SOR_STATE1);
2633 /* power up pad calibration */
2634 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
2635 value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
2636 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
2638 /* production settings */
2639 settings = tegra_sor_hdmi_find_settings(sor, mode->clock * 1000);
2641 dev_err(sor->dev, "no settings for pixel clock %d Hz\n",
2642 mode->clock * 1000);
2646 value = tegra_sor_readl(sor, sor->soc->regs->pll0);
2647 value &= ~SOR_PLL0_ICHPMP_MASK;
2648 value &= ~SOR_PLL0_FILTER_MASK;
2649 value &= ~SOR_PLL0_VCOCAP_MASK;
2650 value |= SOR_PLL0_ICHPMP(settings->ichpmp);
2651 value |= SOR_PLL0_FILTER(settings->filter);
2652 value |= SOR_PLL0_VCOCAP(settings->vcocap);
2653 tegra_sor_writel(sor, value, sor->soc->regs->pll0);
2655 /* XXX not in TRM */
2656 value = tegra_sor_readl(sor, sor->soc->regs->pll1);
2657 value &= ~SOR_PLL1_LOADADJ_MASK;
2658 value &= ~SOR_PLL1_TMDS_TERMADJ_MASK;
2659 value |= SOR_PLL1_LOADADJ(settings->loadadj);
2660 value |= SOR_PLL1_TMDS_TERMADJ(settings->tmds_termadj);
2661 value |= SOR_PLL1_TMDS_TERM;
2662 tegra_sor_writel(sor, value, sor->soc->regs->pll1);
2664 value = tegra_sor_readl(sor, sor->soc->regs->pll3);
2665 value &= ~SOR_PLL3_BG_TEMP_COEF_MASK;
2666 value &= ~SOR_PLL3_BG_VREF_LEVEL_MASK;
2667 value &= ~SOR_PLL3_AVDD10_LEVEL_MASK;
2668 value &= ~SOR_PLL3_AVDD14_LEVEL_MASK;
2669 value |= SOR_PLL3_BG_TEMP_COEF(settings->bg_temp_coef);
2670 value |= SOR_PLL3_BG_VREF_LEVEL(settings->bg_vref_level);
2671 value |= SOR_PLL3_AVDD10_LEVEL(settings->avdd10_level);
2672 value |= SOR_PLL3_AVDD14_LEVEL(settings->avdd14_level);
2673 tegra_sor_writel(sor, value, sor->soc->regs->pll3);
2675 value = settings->drive_current[3] << 24 |
2676 settings->drive_current[2] << 16 |
2677 settings->drive_current[1] << 8 |
2678 settings->drive_current[0] << 0;
2679 tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT0);
2681 value = settings->preemphasis[3] << 24 |
2682 settings->preemphasis[2] << 16 |
2683 settings->preemphasis[1] << 8 |
2684 settings->preemphasis[0] << 0;
2685 tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS0);
2687 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
2688 value &= ~SOR_DP_PADCTL_TX_PU_MASK;
2689 value |= SOR_DP_PADCTL_TX_PU_ENABLE;
2690 value |= SOR_DP_PADCTL_TX_PU(settings->tx_pu_value);
2691 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
2693 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl2);
2694 value &= ~SOR_DP_PADCTL_SPAREPLL_MASK;
2695 value |= SOR_DP_PADCTL_SPAREPLL(settings->sparepll);
2696 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl2);
2698 /* power down pad calibration */
2699 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
2700 value |= SOR_DP_PADCTL_PAD_CAL_PD;
2701 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
2703 if (!dc->soc->has_nvdisplay) {
2704 /* miscellaneous display controller settings */
2705 value = VSYNC_H_POSITION(1);
2706 tegra_dc_writel(dc, value, DC_DISP_DISP_TIMING_OPTIONS);
2709 value = tegra_dc_readl(dc, DC_DISP_DISP_COLOR_CONTROL);
2710 value &= ~DITHER_CONTROL_MASK;
2711 value &= ~BASE_COLOR_SIZE_MASK;
2713 switch (state->bpc) {
2715 value |= BASE_COLOR_SIZE_666;
2719 value |= BASE_COLOR_SIZE_888;
2723 value |= BASE_COLOR_SIZE_101010;
2727 value |= BASE_COLOR_SIZE_121212;
2731 WARN(1, "%u bits-per-color not supported\n", state->bpc);
2732 value |= BASE_COLOR_SIZE_888;
2736 tegra_dc_writel(dc, value, DC_DISP_DISP_COLOR_CONTROL);
2738 /* XXX set display head owner */
2739 value = tegra_sor_readl(sor, SOR_STATE1);
2740 value &= ~SOR_STATE_ASY_OWNER_MASK;
2741 value |= SOR_STATE_ASY_OWNER(1 + dc->pipe);
2742 tegra_sor_writel(sor, value, SOR_STATE1);
2744 err = tegra_sor_power_up(sor, 250);
2746 dev_err(sor->dev, "failed to power up SOR: %d\n", err);
2748 /* configure dynamic range of output */
2749 value = tegra_sor_readl(sor, sor->soc->regs->head_state0 + dc->pipe);
2750 value &= ~SOR_HEAD_STATE_RANGECOMPRESS_MASK;
2751 value &= ~SOR_HEAD_STATE_DYNRANGE_MASK;
2752 tegra_sor_writel(sor, value, sor->soc->regs->head_state0 + dc->pipe);
2754 /* configure colorspace */
2755 value = tegra_sor_readl(sor, sor->soc->regs->head_state0 + dc->pipe);
2756 value &= ~SOR_HEAD_STATE_COLORSPACE_MASK;
2757 value |= SOR_HEAD_STATE_COLORSPACE_RGB;
2758 tegra_sor_writel(sor, value, sor->soc->regs->head_state0 + dc->pipe);
2760 tegra_sor_mode_set(sor, mode, state);
2762 tegra_sor_update(sor);
2764 /* program preamble timing in SOR (XXX) */
2765 value = tegra_sor_readl(sor, SOR_DP_SPARE0);
2766 value &= ~SOR_DP_SPARE_DISP_VIDEO_PREAMBLE;
2767 tegra_sor_writel(sor, value, SOR_DP_SPARE0);
2769 err = tegra_sor_attach(sor);
2771 dev_err(sor->dev, "failed to attach SOR: %d\n", err);
2773 /* enable display to SOR clock and generate HDMI preamble */
2774 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
2776 if (!sor->soc->has_nvdisplay)
2777 value |= SOR_ENABLE(1) | SOR1_TIMING_CYA;
2779 value |= SOR_ENABLE(sor->index);
2781 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
2783 if (dc->soc->has_nvdisplay) {
2784 value = tegra_dc_readl(dc, DC_DISP_CORE_SOR_SET_CONTROL(sor->index));
2785 value &= ~PROTOCOL_MASK;
2786 value |= PROTOCOL_SINGLE_TMDS_A;
2787 tegra_dc_writel(dc, value, DC_DISP_CORE_SOR_SET_CONTROL(sor->index));
2790 tegra_dc_commit(dc);
2792 err = tegra_sor_wakeup(sor);
2794 dev_err(sor->dev, "failed to wakeup SOR: %d\n", err);
2796 tegra_sor_hdmi_scdc_start(sor);
2797 tegra_sor_audio_prepare(sor);
2800 static const struct drm_encoder_helper_funcs tegra_sor_hdmi_helpers = {
2801 .disable = tegra_sor_hdmi_disable,
2802 .enable = tegra_sor_hdmi_enable,
2803 .atomic_check = tegra_sor_encoder_atomic_check,
2806 static int tegra_sor_init(struct host1x_client *client)
2808 struct drm_device *drm = dev_get_drvdata(client->parent);
2809 const struct drm_encoder_helper_funcs *helpers = NULL;
2810 struct tegra_sor *sor = host1x_client_to_sor(client);
2811 int connector = DRM_MODE_CONNECTOR_Unknown;
2812 int encoder = DRM_MODE_ENCODER_NONE;
2817 if (sor->soc->supports_hdmi) {
2818 connector = DRM_MODE_CONNECTOR_HDMIA;
2819 encoder = DRM_MODE_ENCODER_TMDS;
2820 helpers = &tegra_sor_hdmi_helpers;
2821 } else if (sor->soc->supports_lvds) {
2822 connector = DRM_MODE_CONNECTOR_LVDS;
2823 encoder = DRM_MODE_ENCODER_LVDS;
2826 if (sor->soc->supports_edp) {
2827 connector = DRM_MODE_CONNECTOR_eDP;
2828 encoder = DRM_MODE_ENCODER_TMDS;
2829 helpers = &tegra_sor_edp_helpers;
2830 } else if (sor->soc->supports_dp) {
2831 connector = DRM_MODE_CONNECTOR_DisplayPort;
2832 encoder = DRM_MODE_ENCODER_TMDS;
2836 sor->output.dev = sor->dev;
2838 drm_connector_init(drm, &sor->output.connector,
2839 &tegra_sor_connector_funcs,
2841 drm_connector_helper_add(&sor->output.connector,
2842 &tegra_sor_connector_helper_funcs);
2843 sor->output.connector.dpms = DRM_MODE_DPMS_OFF;
2845 drm_encoder_init(drm, &sor->output.encoder, &tegra_sor_encoder_funcs,
2847 drm_encoder_helper_add(&sor->output.encoder, helpers);
2849 drm_connector_attach_encoder(&sor->output.connector,
2850 &sor->output.encoder);
2851 drm_connector_register(&sor->output.connector);
2853 err = tegra_output_init(drm, &sor->output);
2855 dev_err(client->dev, "failed to initialize output: %d\n", err);
2859 tegra_output_find_possible_crtcs(&sor->output, drm);
2862 err = drm_dp_aux_attach(sor->aux, &sor->output);
2864 dev_err(sor->dev, "failed to attach DP: %d\n", err);
2870 * XXX: Remove this reset once proper hand-over from firmware to
2871 * kernel is possible.
2874 err = reset_control_assert(sor->rst);
2876 dev_err(sor->dev, "failed to assert SOR reset: %d\n",
2882 err = clk_prepare_enable(sor->clk);
2884 dev_err(sor->dev, "failed to enable clock: %d\n", err);
2888 usleep_range(1000, 3000);
2891 err = reset_control_deassert(sor->rst);
2893 dev_err(sor->dev, "failed to deassert SOR reset: %d\n",
2899 err = clk_prepare_enable(sor->clk_safe);
2903 err = clk_prepare_enable(sor->clk_dp);
2908 * Enable and unmask the HDA codec SCRATCH0 register interrupt. This
2909 * is used for interoperability between the HDA codec driver and the
2912 value = SOR_INT_CODEC_SCRATCH1 | SOR_INT_CODEC_SCRATCH0;
2913 tegra_sor_writel(sor, value, SOR_INT_ENABLE);
2914 tegra_sor_writel(sor, value, SOR_INT_MASK);
2919 static int tegra_sor_exit(struct host1x_client *client)
2921 struct tegra_sor *sor = host1x_client_to_sor(client);
2924 tegra_sor_writel(sor, 0, SOR_INT_MASK);
2925 tegra_sor_writel(sor, 0, SOR_INT_ENABLE);
2927 tegra_output_exit(&sor->output);
2930 err = drm_dp_aux_detach(sor->aux);
2932 dev_err(sor->dev, "failed to detach DP: %d\n", err);
2937 clk_disable_unprepare(sor->clk_safe);
2938 clk_disable_unprepare(sor->clk_dp);
2939 clk_disable_unprepare(sor->clk);
2944 static const struct host1x_client_ops sor_client_ops = {
2945 .init = tegra_sor_init,
2946 .exit = tegra_sor_exit,
2949 static const struct tegra_sor_ops tegra_sor_edp_ops = {
2953 static int tegra_sor_hdmi_probe(struct tegra_sor *sor)
2957 sor->avdd_io_supply = devm_regulator_get(sor->dev, "avdd-io");
2958 if (IS_ERR(sor->avdd_io_supply)) {
2959 dev_err(sor->dev, "cannot get AVDD I/O supply: %ld\n",
2960 PTR_ERR(sor->avdd_io_supply));
2961 return PTR_ERR(sor->avdd_io_supply);
2964 err = regulator_enable(sor->avdd_io_supply);
2966 dev_err(sor->dev, "failed to enable AVDD I/O supply: %d\n",
2971 sor->vdd_pll_supply = devm_regulator_get(sor->dev, "vdd-pll");
2972 if (IS_ERR(sor->vdd_pll_supply)) {
2973 dev_err(sor->dev, "cannot get VDD PLL supply: %ld\n",
2974 PTR_ERR(sor->vdd_pll_supply));
2975 return PTR_ERR(sor->vdd_pll_supply);
2978 err = regulator_enable(sor->vdd_pll_supply);
2980 dev_err(sor->dev, "failed to enable VDD PLL supply: %d\n",
2985 sor->hdmi_supply = devm_regulator_get(sor->dev, "hdmi");
2986 if (IS_ERR(sor->hdmi_supply)) {
2987 dev_err(sor->dev, "cannot get HDMI supply: %ld\n",
2988 PTR_ERR(sor->hdmi_supply));
2989 return PTR_ERR(sor->hdmi_supply);
2992 err = regulator_enable(sor->hdmi_supply);
2994 dev_err(sor->dev, "failed to enable HDMI supply: %d\n", err);
2998 INIT_DELAYED_WORK(&sor->scdc, tegra_sor_hdmi_scdc_work);
3003 static int tegra_sor_hdmi_remove(struct tegra_sor *sor)
3005 regulator_disable(sor->hdmi_supply);
3006 regulator_disable(sor->vdd_pll_supply);
3007 regulator_disable(sor->avdd_io_supply);
3012 static const struct tegra_sor_ops tegra_sor_hdmi_ops = {
3014 .probe = tegra_sor_hdmi_probe,
3015 .remove = tegra_sor_hdmi_remove,
3018 static const u8 tegra124_sor_xbar_cfg[5] = {
3022 static const struct tegra_sor_regs tegra124_sor_regs = {
3023 .head_state0 = 0x05,
3024 .head_state1 = 0x07,
3025 .head_state2 = 0x09,
3026 .head_state3 = 0x0b,
3027 .head_state4 = 0x0d,
3028 .head_state5 = 0x0f,
3037 static const struct tegra_sor_soc tegra124_sor = {
3038 .supports_edp = true,
3039 .supports_lvds = true,
3040 .supports_hdmi = false,
3041 .supports_dp = false,
3042 .regs = &tegra124_sor_regs,
3043 .has_nvdisplay = false,
3044 .xbar_cfg = tegra124_sor_xbar_cfg,
3047 static const struct tegra_sor_regs tegra210_sor_regs = {
3048 .head_state0 = 0x05,
3049 .head_state1 = 0x07,
3050 .head_state2 = 0x09,
3051 .head_state3 = 0x0b,
3052 .head_state4 = 0x0d,
3053 .head_state5 = 0x0f,
3062 static const struct tegra_sor_soc tegra210_sor = {
3063 .supports_edp = true,
3064 .supports_lvds = false,
3065 .supports_hdmi = false,
3066 .supports_dp = false,
3067 .regs = &tegra210_sor_regs,
3068 .has_nvdisplay = false,
3069 .xbar_cfg = tegra124_sor_xbar_cfg,
3072 static const u8 tegra210_sor_xbar_cfg[5] = {
3076 static const struct tegra_sor_soc tegra210_sor1 = {
3077 .supports_edp = false,
3078 .supports_lvds = false,
3079 .supports_hdmi = true,
3080 .supports_dp = true,
3082 .regs = &tegra210_sor_regs,
3083 .has_nvdisplay = false,
3085 .num_settings = ARRAY_SIZE(tegra210_sor_hdmi_defaults),
3086 .settings = tegra210_sor_hdmi_defaults,
3088 .xbar_cfg = tegra210_sor_xbar_cfg,
3091 static const struct tegra_sor_regs tegra186_sor_regs = {
3092 .head_state0 = 0x151,
3093 .head_state1 = 0x154,
3094 .head_state2 = 0x157,
3095 .head_state3 = 0x15a,
3096 .head_state4 = 0x15d,
3097 .head_state5 = 0x160,
3102 .dp_padctl0 = 0x168,
3103 .dp_padctl2 = 0x16a,
3106 static const struct tegra_sor_soc tegra186_sor = {
3107 .supports_edp = false,
3108 .supports_lvds = false,
3109 .supports_hdmi = false,
3110 .supports_dp = true,
3112 .regs = &tegra186_sor_regs,
3113 .has_nvdisplay = true,
3115 .xbar_cfg = tegra124_sor_xbar_cfg,
3118 static const struct tegra_sor_soc tegra186_sor1 = {
3119 .supports_edp = false,
3120 .supports_lvds = false,
3121 .supports_hdmi = true,
3122 .supports_dp = true,
3124 .regs = &tegra186_sor_regs,
3125 .has_nvdisplay = true,
3127 .num_settings = ARRAY_SIZE(tegra186_sor_hdmi_defaults),
3128 .settings = tegra186_sor_hdmi_defaults,
3130 .xbar_cfg = tegra124_sor_xbar_cfg,
3133 static const struct tegra_sor_regs tegra194_sor_regs = {
3134 .head_state0 = 0x151,
3135 .head_state1 = 0x155,
3136 .head_state2 = 0x159,
3137 .head_state3 = 0x15d,
3138 .head_state4 = 0x161,
3139 .head_state5 = 0x165,
3144 .dp_padctl0 = 0x16e,
3145 .dp_padctl2 = 0x16f,
3148 static const struct tegra_sor_soc tegra194_sor = {
3149 .supports_edp = true,
3150 .supports_lvds = false,
3151 .supports_hdmi = true,
3152 .supports_dp = true,
3154 .regs = &tegra194_sor_regs,
3155 .has_nvdisplay = true,
3157 .num_settings = ARRAY_SIZE(tegra194_sor_hdmi_defaults),
3158 .settings = tegra194_sor_hdmi_defaults,
3160 .xbar_cfg = tegra210_sor_xbar_cfg,
3163 static const struct of_device_id tegra_sor_of_match[] = {
3164 { .compatible = "nvidia,tegra194-sor", .data = &tegra194_sor },
3165 { .compatible = "nvidia,tegra186-sor1", .data = &tegra186_sor1 },
3166 { .compatible = "nvidia,tegra186-sor", .data = &tegra186_sor },
3167 { .compatible = "nvidia,tegra210-sor1", .data = &tegra210_sor1 },
3168 { .compatible = "nvidia,tegra210-sor", .data = &tegra210_sor },
3169 { .compatible = "nvidia,tegra124-sor", .data = &tegra124_sor },
3172 MODULE_DEVICE_TABLE(of, tegra_sor_of_match);
3174 static int tegra_sor_parse_dt(struct tegra_sor *sor)
3176 struct device_node *np = sor->dev->of_node;
3182 if (sor->soc->has_nvdisplay) {
3183 err = of_property_read_u32(np, "nvidia,interface", &value);
3190 * override the default that we already set for Tegra210 and
3193 sor->pad = TEGRA_IO_PAD_HDMI_DP0 + sor->index;
3196 err = of_property_read_u32_array(np, "nvidia,xbar-cfg", xbar_cfg, 5);
3198 /* fall back to default per-SoC XBAR configuration */
3199 for (i = 0; i < 5; i++)
3200 sor->xbar_cfg[i] = sor->soc->xbar_cfg[i];
3202 /* copy cells to SOR XBAR configuration */
3203 for (i = 0; i < 5; i++)
3204 sor->xbar_cfg[i] = xbar_cfg[i];
3210 static irqreturn_t tegra_sor_irq(int irq, void *data)
3212 struct tegra_sor *sor = data;
3215 value = tegra_sor_readl(sor, SOR_INT_STATUS);
3216 tegra_sor_writel(sor, value, SOR_INT_STATUS);
3218 if (value & SOR_INT_CODEC_SCRATCH0) {
3219 value = tegra_sor_readl(sor, SOR_AUDIO_HDA_CODEC_SCRATCH0);
3221 if (value & SOR_AUDIO_HDA_CODEC_SCRATCH0_VALID) {
3222 unsigned int format;
3224 format = value & SOR_AUDIO_HDA_CODEC_SCRATCH0_FMT_MASK;
3226 tegra_hda_parse_format(format, &sor->format);
3228 tegra_sor_hdmi_audio_enable(sor);
3230 tegra_sor_hdmi_audio_disable(sor);
3237 static int tegra_sor_probe(struct platform_device *pdev)
3239 struct device_node *np;
3240 struct tegra_sor *sor;
3241 struct resource *regs;
3244 sor = devm_kzalloc(&pdev->dev, sizeof(*sor), GFP_KERNEL);
3248 sor->soc = of_device_get_match_data(&pdev->dev);
3249 sor->output.dev = sor->dev = &pdev->dev;
3251 sor->settings = devm_kmemdup(&pdev->dev, sor->soc->settings,
3252 sor->soc->num_settings *
3253 sizeof(*sor->settings),
3258 sor->num_settings = sor->soc->num_settings;
3260 np = of_parse_phandle(pdev->dev.of_node, "nvidia,dpaux", 0);
3262 sor->aux = drm_dp_aux_find_by_of_node(np);
3266 return -EPROBE_DEFER;
3270 if (sor->soc->supports_hdmi) {
3271 sor->ops = &tegra_sor_hdmi_ops;
3272 sor->pad = TEGRA_IO_PAD_HDMI;
3273 } else if (sor->soc->supports_lvds) {
3274 dev_err(&pdev->dev, "LVDS not supported yet\n");
3277 dev_err(&pdev->dev, "unknown (non-DP) support\n");
3281 if (sor->soc->supports_edp) {
3282 sor->ops = &tegra_sor_edp_ops;
3283 sor->pad = TEGRA_IO_PAD_LVDS;
3284 } else if (sor->soc->supports_dp) {
3285 dev_err(&pdev->dev, "DisplayPort not supported yet\n");
3288 dev_err(&pdev->dev, "unknown (DP) support\n");
3293 err = tegra_sor_parse_dt(sor);
3297 err = tegra_output_probe(&sor->output);
3299 dev_err(&pdev->dev, "failed to probe output: %d\n", err);
3303 if (sor->ops && sor->ops->probe) {
3304 err = sor->ops->probe(sor);
3306 dev_err(&pdev->dev, "failed to probe %s: %d\n",
3307 sor->ops->name, err);
3312 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3313 sor->regs = devm_ioremap_resource(&pdev->dev, regs);
3314 if (IS_ERR(sor->regs)) {
3315 err = PTR_ERR(sor->regs);
3319 err = platform_get_irq(pdev, 0);
3321 dev_err(&pdev->dev, "failed to get IRQ: %d\n", err);
3327 err = devm_request_irq(sor->dev, sor->irq, tegra_sor_irq, 0,
3328 dev_name(sor->dev), sor);
3330 dev_err(&pdev->dev, "failed to request IRQ: %d\n", err);
3334 sor->rst = devm_reset_control_get(&pdev->dev, "sor");
3335 if (IS_ERR(sor->rst)) {
3336 err = PTR_ERR(sor->rst);
3338 if (err != -EBUSY || WARN_ON(!pdev->dev.pm_domain)) {
3339 dev_err(&pdev->dev, "failed to get reset control: %d\n",
3345 * At this point, the reset control is most likely being used
3346 * by the generic power domain implementation. With any luck
3347 * the power domain will have taken care of resetting the SOR
3348 * and we don't have to do anything.
3353 sor->clk = devm_clk_get(&pdev->dev, NULL);
3354 if (IS_ERR(sor->clk)) {
3355 err = PTR_ERR(sor->clk);
3356 dev_err(&pdev->dev, "failed to get module clock: %d\n", err);
3360 if (sor->soc->supports_hdmi || sor->soc->supports_dp) {
3361 struct device_node *np = pdev->dev.of_node;
3365 * For backwards compatibility with Tegra210 device trees,
3366 * fall back to the old clock name "source" if the new "out"
3367 * clock is not available.
3369 if (of_property_match_string(np, "clock-names", "out") < 0)
3374 sor->clk_out = devm_clk_get(&pdev->dev, name);
3375 if (IS_ERR(sor->clk_out)) {
3376 err = PTR_ERR(sor->clk_out);
3377 dev_err(sor->dev, "failed to get %s clock: %d\n",
3382 /* fall back to the module clock on SOR0 (eDP/LVDS only) */
3383 sor->clk_out = sor->clk;
3386 sor->clk_parent = devm_clk_get(&pdev->dev, "parent");
3387 if (IS_ERR(sor->clk_parent)) {
3388 err = PTR_ERR(sor->clk_parent);
3389 dev_err(&pdev->dev, "failed to get parent clock: %d\n", err);
3393 sor->clk_safe = devm_clk_get(&pdev->dev, "safe");
3394 if (IS_ERR(sor->clk_safe)) {
3395 err = PTR_ERR(sor->clk_safe);
3396 dev_err(&pdev->dev, "failed to get safe clock: %d\n", err);
3400 sor->clk_dp = devm_clk_get(&pdev->dev, "dp");
3401 if (IS_ERR(sor->clk_dp)) {
3402 err = PTR_ERR(sor->clk_dp);
3403 dev_err(&pdev->dev, "failed to get DP clock: %d\n", err);
3408 * Starting with Tegra186, the BPMP provides an implementation for
3409 * the pad output clock, so we have to look it up from device tree.
3411 sor->clk_pad = devm_clk_get(&pdev->dev, "pad");
3412 if (IS_ERR(sor->clk_pad)) {
3413 if (sor->clk_pad != ERR_PTR(-ENOENT)) {
3414 err = PTR_ERR(sor->clk_pad);
3419 * If the pad output clock is not available, then we assume
3420 * we're on Tegra210 or earlier and have to provide our own
3423 sor->clk_pad = NULL;
3427 * The bootloader may have set up the SOR such that it's module clock
3428 * is sourced by one of the display PLLs. However, that doesn't work
3429 * without properly having set up other bits of the SOR.
3431 err = clk_set_parent(sor->clk_out, sor->clk_safe);
3433 dev_err(&pdev->dev, "failed to use safe clock: %d\n", err);
3437 platform_set_drvdata(pdev, sor);
3438 pm_runtime_enable(&pdev->dev);
3441 * On Tegra210 and earlier, provide our own implementation for the
3444 if (!sor->clk_pad) {
3445 err = pm_runtime_get_sync(&pdev->dev);
3447 dev_err(&pdev->dev, "failed to get runtime PM: %d\n",
3452 sor->clk_pad = tegra_clk_sor_pad_register(sor,
3454 pm_runtime_put(&pdev->dev);
3457 if (IS_ERR(sor->clk_pad)) {
3458 err = PTR_ERR(sor->clk_pad);
3459 dev_err(&pdev->dev, "failed to register SOR pad clock: %d\n",
3464 INIT_LIST_HEAD(&sor->client.list);
3465 sor->client.ops = &sor_client_ops;
3466 sor->client.dev = &pdev->dev;
3468 err = host1x_client_register(&sor->client);
3470 dev_err(&pdev->dev, "failed to register host1x client: %d\n",
3478 if (sor->ops && sor->ops->remove)
3479 sor->ops->remove(sor);
3481 tegra_output_remove(&sor->output);
3485 static int tegra_sor_remove(struct platform_device *pdev)
3487 struct tegra_sor *sor = platform_get_drvdata(pdev);
3490 pm_runtime_disable(&pdev->dev);
3492 err = host1x_client_unregister(&sor->client);
3494 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
3499 if (sor->ops && sor->ops->remove) {
3500 err = sor->ops->remove(sor);
3502 dev_err(&pdev->dev, "failed to remove SOR: %d\n", err);
3505 tegra_output_remove(&sor->output);
3511 static int tegra_sor_suspend(struct device *dev)
3513 struct tegra_sor *sor = dev_get_drvdata(dev);
3517 err = reset_control_assert(sor->rst);
3519 dev_err(dev, "failed to assert reset: %d\n", err);
3524 usleep_range(1000, 2000);
3526 clk_disable_unprepare(sor->clk);
3531 static int tegra_sor_resume(struct device *dev)
3533 struct tegra_sor *sor = dev_get_drvdata(dev);
3536 err = clk_prepare_enable(sor->clk);
3538 dev_err(dev, "failed to enable clock: %d\n", err);
3542 usleep_range(1000, 2000);
3545 err = reset_control_deassert(sor->rst);
3547 dev_err(dev, "failed to deassert reset: %d\n", err);
3548 clk_disable_unprepare(sor->clk);
3557 static const struct dev_pm_ops tegra_sor_pm_ops = {
3558 SET_RUNTIME_PM_OPS(tegra_sor_suspend, tegra_sor_resume, NULL)
3561 struct platform_driver tegra_sor_driver = {
3563 .name = "tegra-sor",
3564 .of_match_table = tegra_sor_of_match,
3565 .pm = &tegra_sor_pm_ops,
3567 .probe = tegra_sor_probe,
3568 .remove = tegra_sor_remove,