xref: /openbmc/linux/drivers/gpu/drm/tegra/sor.c (revision a82752e19954cb741a7a582482e5c05c50d7acd3)
16b6b6042SThierry Reding /*
26b6b6042SThierry Reding  * Copyright (C) 2013 NVIDIA Corporation
36b6b6042SThierry Reding  *
46b6b6042SThierry Reding  * This program is free software; you can redistribute it and/or modify
56b6b6042SThierry Reding  * it under the terms of the GNU General Public License version 2 as
66b6b6042SThierry Reding  * published by the Free Software Foundation.
76b6b6042SThierry Reding  */
86b6b6042SThierry Reding 
96b6b6042SThierry Reding #include <linux/clk.h>
10*a82752e1SThierry Reding #include <linux/debugfs.h>
116b6b6042SThierry Reding #include <linux/io.h>
126b6b6042SThierry Reding #include <linux/platform_device.h>
136b6b6042SThierry Reding #include <linux/reset.h>
146b6b6042SThierry Reding #include <linux/tegra-powergate.h>
156b6b6042SThierry Reding 
166b6b6042SThierry Reding #include <drm/drm_dp_helper.h>
176b6b6042SThierry Reding 
186b6b6042SThierry Reding #include "dc.h"
196b6b6042SThierry Reding #include "drm.h"
206b6b6042SThierry Reding #include "sor.h"
216b6b6042SThierry Reding 
226b6b6042SThierry Reding struct tegra_sor {
236b6b6042SThierry Reding 	struct host1x_client client;
246b6b6042SThierry Reding 	struct tegra_output output;
256b6b6042SThierry Reding 	struct device *dev;
266b6b6042SThierry Reding 
276b6b6042SThierry Reding 	void __iomem *regs;
286b6b6042SThierry Reding 
296b6b6042SThierry Reding 	struct reset_control *rst;
306b6b6042SThierry Reding 	struct clk *clk_parent;
316b6b6042SThierry Reding 	struct clk *clk_safe;
326b6b6042SThierry Reding 	struct clk *clk_dp;
336b6b6042SThierry Reding 	struct clk *clk;
346b6b6042SThierry Reding 
356b6b6042SThierry Reding 	struct tegra_dpaux *dpaux;
366b6b6042SThierry Reding 
376b6b6042SThierry Reding 	bool enabled;
38*a82752e1SThierry Reding 
39*a82752e1SThierry Reding 	struct dentry *debugfs;
406b6b6042SThierry Reding };
416b6b6042SThierry Reding 
426b6b6042SThierry Reding static inline struct tegra_sor *
436b6b6042SThierry Reding host1x_client_to_sor(struct host1x_client *client)
446b6b6042SThierry Reding {
456b6b6042SThierry Reding 	return container_of(client, struct tegra_sor, client);
466b6b6042SThierry Reding }
476b6b6042SThierry Reding 
486b6b6042SThierry Reding static inline struct tegra_sor *to_sor(struct tegra_output *output)
496b6b6042SThierry Reding {
506b6b6042SThierry Reding 	return container_of(output, struct tegra_sor, output);
516b6b6042SThierry Reding }
526b6b6042SThierry Reding 
536b6b6042SThierry Reding static inline unsigned long tegra_sor_readl(struct tegra_sor *sor,
546b6b6042SThierry Reding 					    unsigned long offset)
556b6b6042SThierry Reding {
566b6b6042SThierry Reding 	return readl(sor->regs + (offset << 2));
576b6b6042SThierry Reding }
586b6b6042SThierry Reding 
596b6b6042SThierry Reding static inline void tegra_sor_writel(struct tegra_sor *sor, unsigned long value,
606b6b6042SThierry Reding 				    unsigned long offset)
616b6b6042SThierry Reding {
626b6b6042SThierry Reding 	writel(value, sor->regs + (offset << 2));
636b6b6042SThierry Reding }
646b6b6042SThierry Reding 
656b6b6042SThierry Reding static int tegra_sor_dp_train_fast(struct tegra_sor *sor,
666b6b6042SThierry Reding 				   struct drm_dp_link *link)
676b6b6042SThierry Reding {
686b6b6042SThierry Reding 	unsigned long value;
696b6b6042SThierry Reding 	unsigned int i;
706b6b6042SThierry Reding 	u8 pattern;
716b6b6042SThierry Reding 	int err;
726b6b6042SThierry Reding 
736b6b6042SThierry Reding 	/* setup lane parameters */
746b6b6042SThierry Reding 	value = SOR_LANE_DRIVE_CURRENT_LANE3(0x40) |
756b6b6042SThierry Reding 		SOR_LANE_DRIVE_CURRENT_LANE2(0x40) |
766b6b6042SThierry Reding 		SOR_LANE_DRIVE_CURRENT_LANE1(0x40) |
776b6b6042SThierry Reding 		SOR_LANE_DRIVE_CURRENT_LANE0(0x40);
786b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT_0);
796b6b6042SThierry Reding 
806b6b6042SThierry Reding 	value = SOR_LANE_PREEMPHASIS_LANE3(0x0f) |
816b6b6042SThierry Reding 		SOR_LANE_PREEMPHASIS_LANE2(0x0f) |
826b6b6042SThierry Reding 		SOR_LANE_PREEMPHASIS_LANE1(0x0f) |
836b6b6042SThierry Reding 		SOR_LANE_PREEMPHASIS_LANE0(0x0f);
846b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS_0);
856b6b6042SThierry Reding 
866b6b6042SThierry Reding 	value = SOR_LANE_POST_CURSOR_LANE3(0x00) |
876b6b6042SThierry Reding 		SOR_LANE_POST_CURSOR_LANE2(0x00) |
886b6b6042SThierry Reding 		SOR_LANE_POST_CURSOR_LANE1(0x00) |
896b6b6042SThierry Reding 		SOR_LANE_POST_CURSOR_LANE0(0x00);
906b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_POST_CURSOR_0);
916b6b6042SThierry Reding 
926b6b6042SThierry Reding 	/* disable LVDS mode */
936b6b6042SThierry Reding 	tegra_sor_writel(sor, 0, SOR_LVDS);
946b6b6042SThierry Reding 
956b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
966b6b6042SThierry Reding 	value |= SOR_DP_PADCTL_TX_PU_ENABLE;
976b6b6042SThierry Reding 	value &= ~SOR_DP_PADCTL_TX_PU_MASK;
986b6b6042SThierry Reding 	value |= SOR_DP_PADCTL_TX_PU(2); /* XXX: don't hardcode? */
996b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
1006b6b6042SThierry Reding 
1016b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
1026b6b6042SThierry Reding 	value |= SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
1036b6b6042SThierry Reding 		 SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0;
1046b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
1056b6b6042SThierry Reding 
1066b6b6042SThierry Reding 	usleep_range(10, 100);
1076b6b6042SThierry Reding 
1086b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
1096b6b6042SThierry Reding 	value &= ~(SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
1106b6b6042SThierry Reding 		   SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0);
1116b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
1126b6b6042SThierry Reding 
1136b6b6042SThierry Reding 	err = tegra_dpaux_prepare(sor->dpaux, DP_SET_ANSI_8B10B);
1146b6b6042SThierry Reding 	if (err < 0)
1156b6b6042SThierry Reding 		return err;
1166b6b6042SThierry Reding 
1176b6b6042SThierry Reding 	for (i = 0, value = 0; i < link->num_lanes; i++) {
1186b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
1196b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_NONE |
1206b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_TRAIN1;
1216b6b6042SThierry Reding 		value = (value << 8) | lane;
1226b6b6042SThierry Reding 	}
1236b6b6042SThierry Reding 
1246b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
1256b6b6042SThierry Reding 
1266b6b6042SThierry Reding 	pattern = DP_TRAINING_PATTERN_1;
1276b6b6042SThierry Reding 
1286b6b6042SThierry Reding 	err = tegra_dpaux_train(sor->dpaux, link, pattern);
1296b6b6042SThierry Reding 	if (err < 0)
1306b6b6042SThierry Reding 		return err;
1316b6b6042SThierry Reding 
1326b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_SPARE_0);
1336b6b6042SThierry Reding 	value |= SOR_DP_SPARE_SEQ_ENABLE;
1346b6b6042SThierry Reding 	value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
1356b6b6042SThierry Reding 	value |= SOR_DP_SPARE_MACRO_SOR_CLK;
1366b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_SPARE_0);
1376b6b6042SThierry Reding 
1386b6b6042SThierry Reding 	for (i = 0, value = 0; i < link->num_lanes; i++) {
1396b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
1406b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_NONE |
1416b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_TRAIN2;
1426b6b6042SThierry Reding 		value = (value << 8) | lane;
1436b6b6042SThierry Reding 	}
1446b6b6042SThierry Reding 
1456b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
1466b6b6042SThierry Reding 
1476b6b6042SThierry Reding 	pattern = DP_LINK_SCRAMBLING_DISABLE | DP_TRAINING_PATTERN_2;
1486b6b6042SThierry Reding 
1496b6b6042SThierry Reding 	err = tegra_dpaux_train(sor->dpaux, link, pattern);
1506b6b6042SThierry Reding 	if (err < 0)
1516b6b6042SThierry Reding 		return err;
1526b6b6042SThierry Reding 
1536b6b6042SThierry Reding 	for (i = 0, value = 0; i < link->num_lanes; i++) {
1546b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
1556b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_GALIOS |
1566b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_NONE;
1576b6b6042SThierry Reding 		value = (value << 8) | lane;
1586b6b6042SThierry Reding 	}
1596b6b6042SThierry Reding 
1606b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
1616b6b6042SThierry Reding 
1626b6b6042SThierry Reding 	pattern = DP_TRAINING_PATTERN_DISABLE;
1636b6b6042SThierry Reding 
1646b6b6042SThierry Reding 	err = tegra_dpaux_train(sor->dpaux, link, pattern);
1656b6b6042SThierry Reding 	if (err < 0)
1666b6b6042SThierry Reding 		return err;
1676b6b6042SThierry Reding 
1686b6b6042SThierry Reding 	return 0;
1696b6b6042SThierry Reding }
1706b6b6042SThierry Reding 
1716b6b6042SThierry Reding static void tegra_sor_super_update(struct tegra_sor *sor)
1726b6b6042SThierry Reding {
1736b6b6042SThierry Reding 	tegra_sor_writel(sor, 0, SOR_SUPER_STATE_0);
1746b6b6042SThierry Reding 	tegra_sor_writel(sor, 1, SOR_SUPER_STATE_0);
1756b6b6042SThierry Reding 	tegra_sor_writel(sor, 0, SOR_SUPER_STATE_0);
1766b6b6042SThierry Reding }
1776b6b6042SThierry Reding 
1786b6b6042SThierry Reding static void tegra_sor_update(struct tegra_sor *sor)
1796b6b6042SThierry Reding {
1806b6b6042SThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE_0);
1816b6b6042SThierry Reding 	tegra_sor_writel(sor, 1, SOR_STATE_0);
1826b6b6042SThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE_0);
1836b6b6042SThierry Reding }
1846b6b6042SThierry Reding 
1856b6b6042SThierry Reding static int tegra_sor_setup_pwm(struct tegra_sor *sor, unsigned long timeout)
1866b6b6042SThierry Reding {
1876b6b6042SThierry Reding 	unsigned long value;
1886b6b6042SThierry Reding 
1896b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWM_DIV);
1906b6b6042SThierry Reding 	value &= ~SOR_PWM_DIV_MASK;
1916b6b6042SThierry Reding 	value |= 0x400; /* period */
1926b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWM_DIV);
1936b6b6042SThierry Reding 
1946b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWM_CTL);
1956b6b6042SThierry Reding 	value &= ~SOR_PWM_CTL_DUTY_CYCLE_MASK;
1966b6b6042SThierry Reding 	value |= 0x400; /* duty cycle */
1976b6b6042SThierry Reding 	value &= ~SOR_PWM_CTL_CLK_SEL; /* clock source: PCLK */
1986b6b6042SThierry Reding 	value |= SOR_PWM_CTL_TRIGGER;
1996b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWM_CTL);
2006b6b6042SThierry Reding 
2016b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
2026b6b6042SThierry Reding 
2036b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
2046b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWM_CTL);
2056b6b6042SThierry Reding 		if ((value & SOR_PWM_CTL_TRIGGER) == 0)
2066b6b6042SThierry Reding 			return 0;
2076b6b6042SThierry Reding 
2086b6b6042SThierry Reding 		usleep_range(25, 100);
2096b6b6042SThierry Reding 	}
2106b6b6042SThierry Reding 
2116b6b6042SThierry Reding 	return -ETIMEDOUT;
2126b6b6042SThierry Reding }
2136b6b6042SThierry Reding 
2146b6b6042SThierry Reding static int tegra_sor_attach(struct tegra_sor *sor)
2156b6b6042SThierry Reding {
2166b6b6042SThierry Reding 	unsigned long value, timeout;
2176b6b6042SThierry Reding 
2186b6b6042SThierry Reding 	/* wake up in normal mode */
2196b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE_1);
2206b6b6042SThierry Reding 	value |= SOR_SUPER_STATE_HEAD_MODE_AWAKE;
2216b6b6042SThierry Reding 	value |= SOR_SUPER_STATE_MODE_NORMAL;
2226b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE_1);
2236b6b6042SThierry Reding 	tegra_sor_super_update(sor);
2246b6b6042SThierry Reding 
2256b6b6042SThierry Reding 	/* attach */
2266b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE_1);
2276b6b6042SThierry Reding 	value |= SOR_SUPER_STATE_ATTACHED;
2286b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE_1);
2296b6b6042SThierry Reding 	tegra_sor_super_update(sor);
2306b6b6042SThierry Reding 
2316b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
2326b6b6042SThierry Reding 
2336b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
2346b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_TEST);
2356b6b6042SThierry Reding 		if ((value & SOR_TEST_ATTACHED) != 0)
2366b6b6042SThierry Reding 			return 0;
2376b6b6042SThierry Reding 
2386b6b6042SThierry Reding 		usleep_range(25, 100);
2396b6b6042SThierry Reding 	}
2406b6b6042SThierry Reding 
2416b6b6042SThierry Reding 	return -ETIMEDOUT;
2426b6b6042SThierry Reding }
2436b6b6042SThierry Reding 
2446b6b6042SThierry Reding static int tegra_sor_wakeup(struct tegra_sor *sor)
2456b6b6042SThierry Reding {
2466b6b6042SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(sor->output.encoder.crtc);
2476b6b6042SThierry Reding 	unsigned long value, timeout;
2486b6b6042SThierry Reding 
2496b6b6042SThierry Reding 	/* enable display controller outputs */
2506b6b6042SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
2516b6b6042SThierry Reding 	value |= PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
2526b6b6042SThierry Reding 		 PW4_ENABLE | PM0_ENABLE | PM1_ENABLE;
2536b6b6042SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
2546b6b6042SThierry Reding 
2556b6b6042SThierry Reding 	tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
2566b6b6042SThierry Reding 	tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
2576b6b6042SThierry Reding 
2586b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
2596b6b6042SThierry Reding 
2606b6b6042SThierry Reding 	/* wait for head to wake up */
2616b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
2626b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_TEST);
2636b6b6042SThierry Reding 		value &= SOR_TEST_HEAD_MODE_MASK;
2646b6b6042SThierry Reding 
2656b6b6042SThierry Reding 		if (value == SOR_TEST_HEAD_MODE_AWAKE)
2666b6b6042SThierry Reding 			return 0;
2676b6b6042SThierry Reding 
2686b6b6042SThierry Reding 		usleep_range(25, 100);
2696b6b6042SThierry Reding 	}
2706b6b6042SThierry Reding 
2716b6b6042SThierry Reding 	return -ETIMEDOUT;
2726b6b6042SThierry Reding }
2736b6b6042SThierry Reding 
2746b6b6042SThierry Reding static int tegra_sor_power_up(struct tegra_sor *sor, unsigned long timeout)
2756b6b6042SThierry Reding {
2766b6b6042SThierry Reding 	unsigned long value;
2776b6b6042SThierry Reding 
2786b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWR);
2796b6b6042SThierry Reding 	value |= SOR_PWR_TRIGGER | SOR_PWR_NORMAL_STATE_PU;
2806b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWR);
2816b6b6042SThierry Reding 
2826b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
2836b6b6042SThierry Reding 
2846b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
2856b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWR);
2866b6b6042SThierry Reding 		if ((value & SOR_PWR_TRIGGER) == 0)
2876b6b6042SThierry Reding 			return 0;
2886b6b6042SThierry Reding 
2896b6b6042SThierry Reding 		usleep_range(25, 100);
2906b6b6042SThierry Reding 	}
2916b6b6042SThierry Reding 
2926b6b6042SThierry Reding 	return -ETIMEDOUT;
2936b6b6042SThierry Reding }
2946b6b6042SThierry Reding 
2956b6b6042SThierry Reding static int tegra_output_sor_enable(struct tegra_output *output)
2966b6b6042SThierry Reding {
2976b6b6042SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(output->encoder.crtc);
2986b6b6042SThierry Reding 	struct drm_display_mode *mode = &dc->base.mode;
2996b6b6042SThierry Reding 	unsigned int vbe, vse, hbe, hse, vbs, hbs, i;
3006b6b6042SThierry Reding 	struct tegra_sor *sor = to_sor(output);
3016b6b6042SThierry Reding 	unsigned long value;
3026b6b6042SThierry Reding 	int err;
3036b6b6042SThierry Reding 
3046b6b6042SThierry Reding 	if (sor->enabled)
3056b6b6042SThierry Reding 		return 0;
3066b6b6042SThierry Reding 
3076b6b6042SThierry Reding 	err = clk_prepare_enable(sor->clk);
3086b6b6042SThierry Reding 	if (err < 0)
3096b6b6042SThierry Reding 		return err;
3106b6b6042SThierry Reding 
3116b6b6042SThierry Reding 	reset_control_deassert(sor->rst);
3126b6b6042SThierry Reding 
3136b6b6042SThierry Reding 	if (sor->dpaux) {
3146b6b6042SThierry Reding 		err = tegra_dpaux_enable(sor->dpaux);
3156b6b6042SThierry Reding 		if (err < 0)
3166b6b6042SThierry Reding 			dev_err(sor->dev, "failed to enable DP: %d\n", err);
3176b6b6042SThierry Reding 	}
3186b6b6042SThierry Reding 
3196b6b6042SThierry Reding 	err = clk_set_parent(sor->clk, sor->clk_safe);
3206b6b6042SThierry Reding 	if (err < 0)
3216b6b6042SThierry Reding 		dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
3226b6b6042SThierry Reding 
3236b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
3246b6b6042SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
3256b6b6042SThierry Reding 	value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
3266b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
3276b6b6042SThierry Reding 
3286b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL_2);
3296b6b6042SThierry Reding 	value &= ~SOR_PLL_2_BANDGAP_POWERDOWN;
3306b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL_2);
3316b6b6042SThierry Reding 	usleep_range(20, 100);
3326b6b6042SThierry Reding 
3336b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL_3);
3346b6b6042SThierry Reding 	value |= SOR_PLL_3_PLL_VDD_MODE_V3_3;
3356b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL_3);
3366b6b6042SThierry Reding 
3376b6b6042SThierry Reding 	value = SOR_PLL_0_ICHPMP(0xf) | SOR_PLL_0_VCOCAP_RST |
3386b6b6042SThierry Reding 		SOR_PLL_0_PLLREG_LEVEL_V45 | SOR_PLL_0_RESISTOR_EXT;
3396b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL_0);
3406b6b6042SThierry Reding 
3416b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL_2);
3426b6b6042SThierry Reding 	value |= SOR_PLL_2_SEQ_PLLCAPPD;
3436b6b6042SThierry Reding 	value &= ~SOR_PLL_2_SEQ_PLLCAPPD_ENFORCE;
3446b6b6042SThierry Reding 	value |= SOR_PLL_2_LVDS_ENABLE;
3456b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL_2);
3466b6b6042SThierry Reding 
3476b6b6042SThierry Reding 	value = SOR_PLL_1_TERM_COMPOUT | SOR_PLL_1_TMDS_TERM;
3486b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL_1);
3496b6b6042SThierry Reding 
3506b6b6042SThierry Reding 	while (true) {
3516b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_PLL_2);
3526b6b6042SThierry Reding 		if ((value & SOR_PLL_2_SEQ_PLLCAPPD_ENFORCE) == 0)
3536b6b6042SThierry Reding 			break;
3546b6b6042SThierry Reding 
3556b6b6042SThierry Reding 		usleep_range(250, 1000);
3566b6b6042SThierry Reding 	}
3576b6b6042SThierry Reding 
3586b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL_2);
3596b6b6042SThierry Reding 	value &= ~SOR_PLL_2_POWERDOWN_OVERRIDE;
3606b6b6042SThierry Reding 	value &= ~SOR_PLL_2_PORT_POWERDOWN;
3616b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL_2);
3626b6b6042SThierry Reding 
3636b6b6042SThierry Reding 	/*
3646b6b6042SThierry Reding 	 * power up
3656b6b6042SThierry Reding 	 */
3666b6b6042SThierry Reding 
3676b6b6042SThierry Reding 	/* set safe link bandwidth (1.62 Gbps) */
3686b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
3696b6b6042SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
3706b6b6042SThierry Reding 	value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G1_62;
3716b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
3726b6b6042SThierry Reding 
3736b6b6042SThierry Reding 	/* step 1 */
3746b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL_2);
3756b6b6042SThierry Reding 	value |= SOR_PLL_2_SEQ_PLLCAPPD_ENFORCE | SOR_PLL_2_PORT_POWERDOWN |
3766b6b6042SThierry Reding 		 SOR_PLL_2_BANDGAP_POWERDOWN;
3776b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL_2);
3786b6b6042SThierry Reding 
3796b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL_0);
3806b6b6042SThierry Reding 	value |= SOR_PLL_0_VCOPD | SOR_PLL_0_POWER_OFF;
3816b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL_0);
3826b6b6042SThierry Reding 
3836b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
3846b6b6042SThierry Reding 	value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
3856b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
3866b6b6042SThierry Reding 
3876b6b6042SThierry Reding 	/* step 2 */
3886b6b6042SThierry Reding 	err = tegra_io_rail_power_on(TEGRA_IO_RAIL_LVDS);
3896b6b6042SThierry Reding 	if (err < 0) {
3906b6b6042SThierry Reding 		dev_err(sor->dev, "failed to power on I/O rail: %d\n", err);
3916b6b6042SThierry Reding 		return err;
3926b6b6042SThierry Reding 	}
3936b6b6042SThierry Reding 
3946b6b6042SThierry Reding 	usleep_range(5, 100);
3956b6b6042SThierry Reding 
3966b6b6042SThierry Reding 	/* step 3 */
3976b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL_2);
3986b6b6042SThierry Reding 	value &= ~SOR_PLL_2_BANDGAP_POWERDOWN;
3996b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL_2);
4006b6b6042SThierry Reding 
4016b6b6042SThierry Reding 	usleep_range(20, 100);
4026b6b6042SThierry Reding 
4036b6b6042SThierry Reding 	/* step 4 */
4046b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL_0);
4056b6b6042SThierry Reding 	value &= ~SOR_PLL_0_POWER_OFF;
4066b6b6042SThierry Reding 	value &= ~SOR_PLL_0_VCOPD;
4076b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL_0);
4086b6b6042SThierry Reding 
4096b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL_2);
4106b6b6042SThierry Reding 	value &= ~SOR_PLL_2_SEQ_PLLCAPPD_ENFORCE;
4116b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL_2);
4126b6b6042SThierry Reding 
4136b6b6042SThierry Reding 	usleep_range(200, 1000);
4146b6b6042SThierry Reding 
4156b6b6042SThierry Reding 	/* step 5 */
4166b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL_2);
4176b6b6042SThierry Reding 	value &= ~SOR_PLL_2_PORT_POWERDOWN;
4186b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL_2);
4196b6b6042SThierry Reding 
4206b6b6042SThierry Reding 	/* switch to DP clock */
4216b6b6042SThierry Reding 	err = clk_set_parent(sor->clk, sor->clk_dp);
4226b6b6042SThierry Reding 	if (err < 0)
4236b6b6042SThierry Reding 		dev_err(sor->dev, "failed to set DP parent clock: %d\n", err);
4246b6b6042SThierry Reding 
4256b6b6042SThierry Reding 	/* power dplanes (XXX parameterize based on link?) */
4266b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
4276b6b6042SThierry Reding 	value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
4286b6b6042SThierry Reding 		 SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2;
4296b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
4306b6b6042SThierry Reding 
4316b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL_0);
4326b6b6042SThierry Reding 	value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
4336b6b6042SThierry Reding 	value |= SOR_DP_LINKCTL_LANE_COUNT(4);
4346b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL_0);
4356b6b6042SThierry Reding 
4366b6b6042SThierry Reding 	/* start lane sequencer */
4376b6b6042SThierry Reding 	value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
4386b6b6042SThierry Reding 		SOR_LANE_SEQ_CTL_POWER_STATE_UP;
4396b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
4406b6b6042SThierry Reding 
4416b6b6042SThierry Reding 	while (true) {
4426b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
4436b6b6042SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
4446b6b6042SThierry Reding 			break;
4456b6b6042SThierry Reding 
4466b6b6042SThierry Reding 		usleep_range(250, 1000);
4476b6b6042SThierry Reding 	}
4486b6b6042SThierry Reding 
4496b6b6042SThierry Reding 	/* set link bandwidth (2.7 GHz, XXX: parameterize based on link?) */
4506b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
4516b6b6042SThierry Reding 	value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
4526b6b6042SThierry Reding 	value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G2_70;
4536b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
4546b6b6042SThierry Reding 
4556b6b6042SThierry Reding 	/* set linkctl */
4566b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_LINKCTL_0);
4576b6b6042SThierry Reding 	value |= SOR_DP_LINKCTL_ENABLE;
4586b6b6042SThierry Reding 
4596b6b6042SThierry Reding 	value &= ~SOR_DP_LINKCTL_TU_SIZE_MASK;
4606b6b6042SThierry Reding 	value |= SOR_DP_LINKCTL_TU_SIZE(59); /* XXX: don't hardcode? */
4616b6b6042SThierry Reding 
4626b6b6042SThierry Reding 	value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
4636b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_LINKCTL_0);
4646b6b6042SThierry Reding 
4656b6b6042SThierry Reding 	for (i = 0, value = 0; i < 4; i++) {
4666b6b6042SThierry Reding 		unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
4676b6b6042SThierry Reding 				     SOR_DP_TPG_SCRAMBLER_GALIOS |
4686b6b6042SThierry Reding 				     SOR_DP_TPG_PATTERN_NONE;
4696b6b6042SThierry Reding 		value = (value << 8) | lane;
4706b6b6042SThierry Reding 	}
4716b6b6042SThierry Reding 
4726b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_TPG);
4736b6b6042SThierry Reding 
4746b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_CONFIG_0);
4756b6b6042SThierry Reding 	value &= ~SOR_DP_CONFIG_WATERMARK_MASK;
4766b6b6042SThierry Reding 	value |= SOR_DP_CONFIG_WATERMARK(14); /* XXX: don't hardcode? */
4776b6b6042SThierry Reding 
4786b6b6042SThierry Reding 	value &= ~SOR_DP_CONFIG_ACTIVE_SYM_COUNT_MASK;
4796b6b6042SThierry Reding 	value |= SOR_DP_CONFIG_ACTIVE_SYM_COUNT(47); /* XXX: don't hardcode? */
4806b6b6042SThierry Reding 
4816b6b6042SThierry Reding 	value &= ~SOR_DP_CONFIG_ACTIVE_SYM_FRAC_MASK;
4826b6b6042SThierry Reding 	value |= SOR_DP_CONFIG_ACTIVE_SYM_FRAC(9); /* XXX: don't hardcode? */
4836b6b6042SThierry Reding 
4846b6b6042SThierry Reding 	value &= ~SOR_DP_CONFIG_ACTIVE_SYM_POLARITY; /* XXX: don't hardcode? */
4856b6b6042SThierry Reding 
4866b6b6042SThierry Reding 	value |= SOR_DP_CONFIG_ACTIVE_SYM_ENABLE;
4876b6b6042SThierry Reding 	value |= SOR_DP_CONFIG_DISPARITY_NEGATIVE; /* XXX: don't hardcode? */
4886b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_CONFIG_0);
4896b6b6042SThierry Reding 
4906b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_AUDIO_HBLANK_SYMBOLS);
4916b6b6042SThierry Reding 	value &= ~SOR_DP_AUDIO_HBLANK_SYMBOLS_MASK;
4926b6b6042SThierry Reding 	value |= 137; /* XXX: don't hardcode? */
4936b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_AUDIO_HBLANK_SYMBOLS);
4946b6b6042SThierry Reding 
4956b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_AUDIO_VBLANK_SYMBOLS);
4966b6b6042SThierry Reding 	value &= ~SOR_DP_AUDIO_VBLANK_SYMBOLS_MASK;
4976b6b6042SThierry Reding 	value |= 2368; /* XXX: don't hardcode? */
4986b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_AUDIO_VBLANK_SYMBOLS);
4996b6b6042SThierry Reding 
5006b6b6042SThierry Reding 	/* enable pad calibration logic */
5016b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
5026b6b6042SThierry Reding 	value |= SOR_DP_PADCTL_PAD_CAL_PD;
5036b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
5046b6b6042SThierry Reding 
5056b6b6042SThierry Reding 	if (sor->dpaux) {
5066b6b6042SThierry Reding 		/* FIXME: properly convert to struct drm_dp_aux */
5076b6b6042SThierry Reding 		struct drm_dp_aux *aux = (struct drm_dp_aux *)sor->dpaux;
5086b6b6042SThierry Reding 		struct drm_dp_link link;
5096b6b6042SThierry Reding 		u8 rate, lanes;
5106b6b6042SThierry Reding 
5116b6b6042SThierry Reding 		err = drm_dp_link_probe(aux, &link);
5126b6b6042SThierry Reding 		if (err < 0) {
5136b6b6042SThierry Reding 			dev_err(sor->dev, "failed to probe eDP link: %d\n",
5146b6b6042SThierry Reding 				err);
5156b6b6042SThierry Reding 			return err;
5166b6b6042SThierry Reding 		}
5176b6b6042SThierry Reding 
5186b6b6042SThierry Reding 		err = drm_dp_link_power_up(aux, &link);
5196b6b6042SThierry Reding 		if (err < 0) {
5206b6b6042SThierry Reding 			dev_err(sor->dev, "failed to power up eDP link: %d\n",
5216b6b6042SThierry Reding 				err);
5226b6b6042SThierry Reding 			return err;
5236b6b6042SThierry Reding 		}
5246b6b6042SThierry Reding 
5256b6b6042SThierry Reding 		err = drm_dp_link_configure(aux, &link);
5266b6b6042SThierry Reding 		if (err < 0) {
5276b6b6042SThierry Reding 			dev_err(sor->dev, "failed to configure eDP link: %d\n",
5286b6b6042SThierry Reding 				err);
5296b6b6042SThierry Reding 			return err;
5306b6b6042SThierry Reding 		}
5316b6b6042SThierry Reding 
5326b6b6042SThierry Reding 		rate = drm_dp_link_rate_to_bw_code(link.rate);
5336b6b6042SThierry Reding 		lanes = link.num_lanes;
5346b6b6042SThierry Reding 
5356b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
5366b6b6042SThierry Reding 		value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
5376b6b6042SThierry Reding 		value |= SOR_CLK_CNTRL_DP_LINK_SPEED(rate);
5386b6b6042SThierry Reding 		tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
5396b6b6042SThierry Reding 
5406b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_DP_LINKCTL_0);
5416b6b6042SThierry Reding 		value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
5426b6b6042SThierry Reding 		value |= SOR_DP_LINKCTL_LANE_COUNT(lanes);
5436b6b6042SThierry Reding 
5446b6b6042SThierry Reding 		if (link.capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
5456b6b6042SThierry Reding 			value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
5466b6b6042SThierry Reding 
5476b6b6042SThierry Reding 		tegra_sor_writel(sor, value, SOR_DP_LINKCTL_0);
5486b6b6042SThierry Reding 
5496b6b6042SThierry Reding 		/* disable training pattern generator */
5506b6b6042SThierry Reding 
5516b6b6042SThierry Reding 		for (i = 0; i < link.num_lanes; i++) {
5526b6b6042SThierry Reding 			unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
5536b6b6042SThierry Reding 					     SOR_DP_TPG_SCRAMBLER_GALIOS |
5546b6b6042SThierry Reding 					     SOR_DP_TPG_PATTERN_NONE;
5556b6b6042SThierry Reding 			value = (value << 8) | lane;
5566b6b6042SThierry Reding 		}
5576b6b6042SThierry Reding 
5586b6b6042SThierry Reding 		tegra_sor_writel(sor, value, SOR_DP_TPG);
5596b6b6042SThierry Reding 
5606b6b6042SThierry Reding 		err = tegra_sor_dp_train_fast(sor, &link);
5616b6b6042SThierry Reding 		if (err < 0) {
5626b6b6042SThierry Reding 			dev_err(sor->dev, "DP fast link training failed: %d\n",
5636b6b6042SThierry Reding 				err);
5646b6b6042SThierry Reding 			return err;
5656b6b6042SThierry Reding 		}
5666b6b6042SThierry Reding 
5676b6b6042SThierry Reding 		dev_dbg(sor->dev, "fast link training succeeded\n");
5686b6b6042SThierry Reding 	}
5696b6b6042SThierry Reding 
5706b6b6042SThierry Reding 	err = tegra_sor_power_up(sor, 250);
5716b6b6042SThierry Reding 	if (err < 0) {
5726b6b6042SThierry Reding 		dev_err(sor->dev, "failed to power up SOR: %d\n", err);
5736b6b6042SThierry Reding 		return err;
5746b6b6042SThierry Reding 	}
5756b6b6042SThierry Reding 
5766b6b6042SThierry Reding 	/* start display controller in continuous mode */
5776b6b6042SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_STATE_ACCESS);
5786b6b6042SThierry Reding 	value |= WRITE_MUX;
5796b6b6042SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_STATE_ACCESS);
5806b6b6042SThierry Reding 
5816b6b6042SThierry Reding 	tegra_dc_writel(dc, VSYNC_H_POSITION(1), DC_DISP_DISP_TIMING_OPTIONS);
5826b6b6042SThierry Reding 	tegra_dc_writel(dc, DISP_CTRL_MODE_C_DISPLAY, DC_CMD_DISPLAY_COMMAND);
5836b6b6042SThierry Reding 
5846b6b6042SThierry Reding 	value = tegra_dc_readl(dc, DC_CMD_STATE_ACCESS);
5856b6b6042SThierry Reding 	value &= ~WRITE_MUX;
5866b6b6042SThierry Reding 	tegra_dc_writel(dc, value, DC_CMD_STATE_ACCESS);
5876b6b6042SThierry Reding 
5886b6b6042SThierry Reding 	/*
5896b6b6042SThierry Reding 	 * configure panel (24bpp, vsync-, hsync-, DP-A protocol, complete
5906b6b6042SThierry Reding 	 * raster, associate with display controller)
5916b6b6042SThierry Reding 	 */
5926b6b6042SThierry Reding 	value = SOR_STATE_ASY_PIXELDEPTH_BPP_24_444 |
5936b6b6042SThierry Reding 		SOR_STATE_ASY_VSYNCPOL |
5946b6b6042SThierry Reding 		SOR_STATE_ASY_HSYNCPOL |
5956b6b6042SThierry Reding 		SOR_STATE_ASY_PROTOCOL_DP_A |
5966b6b6042SThierry Reding 		SOR_STATE_ASY_CRC_MODE_COMPLETE |
5976b6b6042SThierry Reding 		SOR_STATE_ASY_OWNER(dc->pipe + 1);
5986b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE_1);
5996b6b6042SThierry Reding 
6006b6b6042SThierry Reding 	/*
6016b6b6042SThierry Reding 	 * TODO: The video timing programming below doesn't seem to match the
6026b6b6042SThierry Reding 	 * register definitions.
6036b6b6042SThierry Reding 	 */
6046b6b6042SThierry Reding 
6056b6b6042SThierry Reding 	value = ((mode->vtotal & 0x7fff) << 16) | (mode->htotal & 0x7fff);
6066b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_HEAD_STATE_1(0));
6076b6b6042SThierry Reding 
6086b6b6042SThierry Reding 	vse = mode->vsync_end - mode->vsync_start - 1;
6096b6b6042SThierry Reding 	hse = mode->hsync_end - mode->hsync_start - 1;
6106b6b6042SThierry Reding 
6116b6b6042SThierry Reding 	value = ((vse & 0x7fff) << 16) | (hse & 0x7fff);
6126b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_HEAD_STATE_2(0));
6136b6b6042SThierry Reding 
6146b6b6042SThierry Reding 	vbe = vse + (mode->vsync_start - mode->vdisplay);
6156b6b6042SThierry Reding 	hbe = hse + (mode->hsync_start - mode->hdisplay);
6166b6b6042SThierry Reding 
6176b6b6042SThierry Reding 	value = ((vbe & 0x7fff) << 16) | (hbe & 0x7fff);
6186b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_HEAD_STATE_3(0));
6196b6b6042SThierry Reding 
6206b6b6042SThierry Reding 	vbs = vbe + mode->vdisplay;
6216b6b6042SThierry Reding 	hbs = hbe + mode->hdisplay;
6226b6b6042SThierry Reding 
6236b6b6042SThierry Reding 	value = ((vbs & 0x7fff) << 16) | (hbs & 0x7fff);
6246b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_HEAD_STATE_4(0));
6256b6b6042SThierry Reding 
6266b6b6042SThierry Reding 	/* XXX interlaced mode */
6276b6b6042SThierry Reding 	tegra_sor_writel(sor, 0x00000001, SOR_HEAD_STATE_5(0));
6286b6b6042SThierry Reding 
6296b6b6042SThierry Reding 	/* CSTM (LVDS, link A/B, upper) */
6306b6b6042SThierry Reding 	value = SOR_CSTM_LVDS | SOR_CSTM_LINK_ACT_B | SOR_CSTM_LINK_ACT_B |
6316b6b6042SThierry Reding 		SOR_CSTM_UPPER;
6326b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_CSTM);
6336b6b6042SThierry Reding 
6346b6b6042SThierry Reding 	/* PWM setup */
6356b6b6042SThierry Reding 	err = tegra_sor_setup_pwm(sor, 250);
6366b6b6042SThierry Reding 	if (err < 0) {
6376b6b6042SThierry Reding 		dev_err(sor->dev, "failed to setup PWM: %d\n", err);
6386b6b6042SThierry Reding 		return err;
6396b6b6042SThierry Reding 	}
6406b6b6042SThierry Reding 
6416b6b6042SThierry Reding 	value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
6426b6b6042SThierry Reding 	value |= SOR_ENABLE;
6436b6b6042SThierry Reding 	tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
6446b6b6042SThierry Reding 
6456b6b6042SThierry Reding 	tegra_sor_update(sor);
6466b6b6042SThierry Reding 
6476b6b6042SThierry Reding 	err = tegra_sor_attach(sor);
6486b6b6042SThierry Reding 	if (err < 0) {
6496b6b6042SThierry Reding 		dev_err(sor->dev, "failed to attach SOR: %d\n", err);
6506b6b6042SThierry Reding 		return err;
6516b6b6042SThierry Reding 	}
6526b6b6042SThierry Reding 
6536b6b6042SThierry Reding 	err = tegra_sor_wakeup(sor);
6546b6b6042SThierry Reding 	if (err < 0) {
6556b6b6042SThierry Reding 		dev_err(sor->dev, "failed to enable DC: %d\n", err);
6566b6b6042SThierry Reding 		return err;
6576b6b6042SThierry Reding 	}
6586b6b6042SThierry Reding 
6596b6b6042SThierry Reding 	sor->enabled = true;
6606b6b6042SThierry Reding 
6616b6b6042SThierry Reding 	return 0;
6626b6b6042SThierry Reding }
6636b6b6042SThierry Reding 
6646b6b6042SThierry Reding static int tegra_sor_detach(struct tegra_sor *sor)
6656b6b6042SThierry Reding {
6666b6b6042SThierry Reding 	unsigned long value, timeout;
6676b6b6042SThierry Reding 
6686b6b6042SThierry Reding 	/* switch to safe mode */
6696b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE_1);
6706b6b6042SThierry Reding 	value &= ~SOR_SUPER_STATE_MODE_NORMAL;
6716b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE_1);
6726b6b6042SThierry Reding 	tegra_sor_super_update(sor);
6736b6b6042SThierry Reding 
6746b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
6756b6b6042SThierry Reding 
6766b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
6776b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWR);
6786b6b6042SThierry Reding 		if (value & SOR_PWR_MODE_SAFE)
6796b6b6042SThierry Reding 			break;
6806b6b6042SThierry Reding 	}
6816b6b6042SThierry Reding 
6826b6b6042SThierry Reding 	if ((value & SOR_PWR_MODE_SAFE) == 0)
6836b6b6042SThierry Reding 		return -ETIMEDOUT;
6846b6b6042SThierry Reding 
6856b6b6042SThierry Reding 	/* go to sleep */
6866b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE_1);
6876b6b6042SThierry Reding 	value &= ~SOR_SUPER_STATE_HEAD_MODE_MASK;
6886b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE_1);
6896b6b6042SThierry Reding 	tegra_sor_super_update(sor);
6906b6b6042SThierry Reding 
6916b6b6042SThierry Reding 	/* detach */
6926b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_SUPER_STATE_1);
6936b6b6042SThierry Reding 	value &= ~SOR_SUPER_STATE_ATTACHED;
6946b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_SUPER_STATE_1);
6956b6b6042SThierry Reding 	tegra_sor_super_update(sor);
6966b6b6042SThierry Reding 
6976b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
6986b6b6042SThierry Reding 
6996b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
7006b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_TEST);
7016b6b6042SThierry Reding 		if ((value & SOR_TEST_ATTACHED) == 0)
7026b6b6042SThierry Reding 			break;
7036b6b6042SThierry Reding 
7046b6b6042SThierry Reding 		usleep_range(25, 100);
7056b6b6042SThierry Reding 	}
7066b6b6042SThierry Reding 
7076b6b6042SThierry Reding 	if ((value & SOR_TEST_ATTACHED) != 0)
7086b6b6042SThierry Reding 		return -ETIMEDOUT;
7096b6b6042SThierry Reding 
7106b6b6042SThierry Reding 	return 0;
7116b6b6042SThierry Reding }
7126b6b6042SThierry Reding 
7136b6b6042SThierry Reding static int tegra_sor_power_down(struct tegra_sor *sor)
7146b6b6042SThierry Reding {
7156b6b6042SThierry Reding 	unsigned long value, timeout;
7166b6b6042SThierry Reding 	int err;
7176b6b6042SThierry Reding 
7186b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PWR);
7196b6b6042SThierry Reding 	value &= ~SOR_PWR_NORMAL_STATE_PU;
7206b6b6042SThierry Reding 	value |= SOR_PWR_TRIGGER;
7216b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PWR);
7226b6b6042SThierry Reding 
7236b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
7246b6b6042SThierry Reding 
7256b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
7266b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_PWR);
7276b6b6042SThierry Reding 		if ((value & SOR_PWR_TRIGGER) == 0)
7286b6b6042SThierry Reding 			return 0;
7296b6b6042SThierry Reding 
7306b6b6042SThierry Reding 		usleep_range(25, 100);
7316b6b6042SThierry Reding 	}
7326b6b6042SThierry Reding 
7336b6b6042SThierry Reding 	if ((value & SOR_PWR_TRIGGER) != 0)
7346b6b6042SThierry Reding 		return -ETIMEDOUT;
7356b6b6042SThierry Reding 
7366b6b6042SThierry Reding 	err = clk_set_parent(sor->clk, sor->clk_safe);
7376b6b6042SThierry Reding 	if (err < 0)
7386b6b6042SThierry Reding 		dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
7396b6b6042SThierry Reding 
7406b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
7416b6b6042SThierry Reding 	value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
7426b6b6042SThierry Reding 		   SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2);
7436b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
7446b6b6042SThierry Reding 
7456b6b6042SThierry Reding 	/* stop lane sequencer */
7466b6b6042SThierry Reding 	value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
7476b6b6042SThierry Reding 		SOR_LANE_SEQ_CTL_POWER_STATE_DOWN;
7486b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
7496b6b6042SThierry Reding 
7506b6b6042SThierry Reding 	timeout = jiffies + msecs_to_jiffies(250);
7516b6b6042SThierry Reding 
7526b6b6042SThierry Reding 	while (time_before(jiffies, timeout)) {
7536b6b6042SThierry Reding 		value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
7546b6b6042SThierry Reding 		if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
7556b6b6042SThierry Reding 			break;
7566b6b6042SThierry Reding 
7576b6b6042SThierry Reding 		usleep_range(25, 100);
7586b6b6042SThierry Reding 	}
7596b6b6042SThierry Reding 
7606b6b6042SThierry Reding 	if ((value & SOR_LANE_SEQ_CTL_TRIGGER) != 0)
7616b6b6042SThierry Reding 		return -ETIMEDOUT;
7626b6b6042SThierry Reding 
7636b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL_2);
7646b6b6042SThierry Reding 	value |= SOR_PLL_2_PORT_POWERDOWN;
7656b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL_2);
7666b6b6042SThierry Reding 
7676b6b6042SThierry Reding 	usleep_range(20, 100);
7686b6b6042SThierry Reding 
7696b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL_0);
7706b6b6042SThierry Reding 	value |= SOR_PLL_0_POWER_OFF;
7716b6b6042SThierry Reding 	value |= SOR_PLL_0_VCOPD;
7726b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL_0);
7736b6b6042SThierry Reding 
7746b6b6042SThierry Reding 	value = tegra_sor_readl(sor, SOR_PLL_2);
7756b6b6042SThierry Reding 	value |= SOR_PLL_2_SEQ_PLLCAPPD;
7766b6b6042SThierry Reding 	value |= SOR_PLL_2_SEQ_PLLCAPPD_ENFORCE;
7776b6b6042SThierry Reding 	tegra_sor_writel(sor, value, SOR_PLL_2);
7786b6b6042SThierry Reding 
7796b6b6042SThierry Reding 	usleep_range(20, 100);
7806b6b6042SThierry Reding 
7816b6b6042SThierry Reding 	return 0;
7826b6b6042SThierry Reding }
7836b6b6042SThierry Reding 
7846b6b6042SThierry Reding static int tegra_output_sor_disable(struct tegra_output *output)
7856b6b6042SThierry Reding {
7866b6b6042SThierry Reding 	struct tegra_dc *dc = to_tegra_dc(output->encoder.crtc);
7876b6b6042SThierry Reding 	struct tegra_sor *sor = to_sor(output);
7886b6b6042SThierry Reding 	unsigned long value;
7896b6b6042SThierry Reding 	int err;
7906b6b6042SThierry Reding 
7916b6b6042SThierry Reding 	if (!sor->enabled)
7926b6b6042SThierry Reding 		return 0;
7936b6b6042SThierry Reding 
7946b6b6042SThierry Reding 	err = tegra_sor_detach(sor);
7956b6b6042SThierry Reding 	if (err < 0) {
7966b6b6042SThierry Reding 		dev_err(sor->dev, "failed to detach SOR: %d\n", err);
7976b6b6042SThierry Reding 		return err;
7986b6b6042SThierry Reding 	}
7996b6b6042SThierry Reding 
8006b6b6042SThierry Reding 	tegra_sor_writel(sor, 0, SOR_STATE_1);
8016b6b6042SThierry Reding 	tegra_sor_update(sor);
8026b6b6042SThierry Reding 
8036b6b6042SThierry Reding 	/*
8046b6b6042SThierry Reding 	 * The following accesses registers of the display controller, so make
8056b6b6042SThierry Reding 	 * sure it's only executed when the output is attached to one.
8066b6b6042SThierry Reding 	 */
8076b6b6042SThierry Reding 	if (dc) {
8086b6b6042SThierry Reding 		/*
8096b6b6042SThierry Reding 		 * XXX: We can't do this here because it causes the SOR to go
8106b6b6042SThierry Reding 		 * into an erroneous state and the output will look scrambled
8116b6b6042SThierry Reding 		 * the next time it is enabled. Presumably this is because we
8126b6b6042SThierry Reding 		 * should be doing this only on the next VBLANK. A possible
8136b6b6042SThierry Reding 		 * solution would be to queue a "power-off" event to trigger
8146b6b6042SThierry Reding 		 * this code to be run during the next VBLANK.
8156b6b6042SThierry Reding 		 */
8166b6b6042SThierry Reding 		/*
8176b6b6042SThierry Reding 		value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
8186b6b6042SThierry Reding 		value &= ~(PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
8196b6b6042SThierry Reding 			   PW4_ENABLE | PM0_ENABLE | PM1_ENABLE);
8206b6b6042SThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
8216b6b6042SThierry Reding 		*/
8226b6b6042SThierry Reding 
8236b6b6042SThierry Reding 		value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
8246b6b6042SThierry Reding 		value &= ~DISP_CTRL_MODE_MASK;
8256b6b6042SThierry Reding 		tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
8266b6b6042SThierry Reding 
8276b6b6042SThierry Reding 		value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
8286b6b6042SThierry Reding 		value &= ~SOR_ENABLE;
8296b6b6042SThierry Reding 		tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
8306b6b6042SThierry Reding 
8316b6b6042SThierry Reding 		tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
8326b6b6042SThierry Reding 		tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
8336b6b6042SThierry Reding 	}
8346b6b6042SThierry Reding 
8356b6b6042SThierry Reding 	err = tegra_sor_power_down(sor);
8366b6b6042SThierry Reding 	if (err < 0) {
8376b6b6042SThierry Reding 		dev_err(sor->dev, "failed to power down SOR: %d\n", err);
8386b6b6042SThierry Reding 		return err;
8396b6b6042SThierry Reding 	}
8406b6b6042SThierry Reding 
8416b6b6042SThierry Reding 	if (sor->dpaux) {
8426b6b6042SThierry Reding 		err = tegra_dpaux_disable(sor->dpaux);
8436b6b6042SThierry Reding 		if (err < 0) {
8446b6b6042SThierry Reding 			dev_err(sor->dev, "failed to disable DP: %d\n", err);
8456b6b6042SThierry Reding 			return err;
8466b6b6042SThierry Reding 		}
8476b6b6042SThierry Reding 	}
8486b6b6042SThierry Reding 
8496b6b6042SThierry Reding 	err = tegra_io_rail_power_off(TEGRA_IO_RAIL_LVDS);
8506b6b6042SThierry Reding 	if (err < 0) {
8516b6b6042SThierry Reding 		dev_err(sor->dev, "failed to power off I/O rail: %d\n", err);
8526b6b6042SThierry Reding 		return err;
8536b6b6042SThierry Reding 	}
8546b6b6042SThierry Reding 
8556b6b6042SThierry Reding 	reset_control_assert(sor->rst);
8566b6b6042SThierry Reding 	clk_disable_unprepare(sor->clk);
8576b6b6042SThierry Reding 
8586b6b6042SThierry Reding 	sor->enabled = false;
8596b6b6042SThierry Reding 
8606b6b6042SThierry Reding 	return 0;
8616b6b6042SThierry Reding }
8626b6b6042SThierry Reding 
8636b6b6042SThierry Reding static int tegra_output_sor_setup_clock(struct tegra_output *output,
8646b6b6042SThierry Reding 					struct clk *clk, unsigned long pclk)
8656b6b6042SThierry Reding {
8666b6b6042SThierry Reding 	struct tegra_sor *sor = to_sor(output);
8676b6b6042SThierry Reding 	int err;
8686b6b6042SThierry Reding 
8696b6b6042SThierry Reding 	/* round to next MHz */
8706b6b6042SThierry Reding 	pclk = DIV_ROUND_UP(pclk / 2, 1000000) * 1000000;
8716b6b6042SThierry Reding 
8726b6b6042SThierry Reding 	err = clk_set_parent(clk, sor->clk_parent);
8736b6b6042SThierry Reding 	if (err < 0) {
8746b6b6042SThierry Reding 		dev_err(sor->dev, "failed to set parent clock: %d\n", err);
8756b6b6042SThierry Reding 		return err;
8766b6b6042SThierry Reding 	}
8776b6b6042SThierry Reding 
8786b6b6042SThierry Reding 	err = clk_set_rate(sor->clk_parent, pclk);
8796b6b6042SThierry Reding 	if (err < 0) {
8806b6b6042SThierry Reding 		dev_err(sor->dev, "failed to set base clock rate to %lu Hz\n",
8816b6b6042SThierry Reding 			pclk * 2);
8826b6b6042SThierry Reding 		return err;
8836b6b6042SThierry Reding 	}
8846b6b6042SThierry Reding 
8856b6b6042SThierry Reding 	return 0;
8866b6b6042SThierry Reding }
8876b6b6042SThierry Reding 
8886b6b6042SThierry Reding static int tegra_output_sor_check_mode(struct tegra_output *output,
8896b6b6042SThierry Reding 				       struct drm_display_mode *mode,
8906b6b6042SThierry Reding 				       enum drm_mode_status *status)
8916b6b6042SThierry Reding {
8926b6b6042SThierry Reding 	/*
8936b6b6042SThierry Reding 	 * FIXME: For now, always assume that the mode is okay.
8946b6b6042SThierry Reding 	 */
8956b6b6042SThierry Reding 
8966b6b6042SThierry Reding 	*status = MODE_OK;
8976b6b6042SThierry Reding 
8986b6b6042SThierry Reding 	return 0;
8996b6b6042SThierry Reding }
9006b6b6042SThierry Reding 
9016b6b6042SThierry Reding static enum drm_connector_status
9026b6b6042SThierry Reding tegra_output_sor_detect(struct tegra_output *output)
9036b6b6042SThierry Reding {
9046b6b6042SThierry Reding 	struct tegra_sor *sor = to_sor(output);
9056b6b6042SThierry Reding 
9066b6b6042SThierry Reding 	if (sor->dpaux)
9076b6b6042SThierry Reding 		return tegra_dpaux_detect(sor->dpaux);
9086b6b6042SThierry Reding 
9096b6b6042SThierry Reding 	return connector_status_unknown;
9106b6b6042SThierry Reding }
9116b6b6042SThierry Reding 
9126b6b6042SThierry Reding static const struct tegra_output_ops sor_ops = {
9136b6b6042SThierry Reding 	.enable = tegra_output_sor_enable,
9146b6b6042SThierry Reding 	.disable = tegra_output_sor_disable,
9156b6b6042SThierry Reding 	.setup_clock = tegra_output_sor_setup_clock,
9166b6b6042SThierry Reding 	.check_mode = tegra_output_sor_check_mode,
9176b6b6042SThierry Reding 	.detect = tegra_output_sor_detect,
9186b6b6042SThierry Reding };
9196b6b6042SThierry Reding 
920*a82752e1SThierry Reding static int tegra_sor_crc_open(struct inode *inode, struct file *file)
921*a82752e1SThierry Reding {
922*a82752e1SThierry Reding 	file->private_data = inode->i_private;
923*a82752e1SThierry Reding 
924*a82752e1SThierry Reding 	return 0;
925*a82752e1SThierry Reding }
926*a82752e1SThierry Reding 
927*a82752e1SThierry Reding static int tegra_sor_crc_release(struct inode *inode, struct file *file)
928*a82752e1SThierry Reding {
929*a82752e1SThierry Reding 	return 0;
930*a82752e1SThierry Reding }
931*a82752e1SThierry Reding 
932*a82752e1SThierry Reding static int tegra_sor_crc_wait(struct tegra_sor *sor, unsigned long timeout)
933*a82752e1SThierry Reding {
934*a82752e1SThierry Reding 	u32 value;
935*a82752e1SThierry Reding 
936*a82752e1SThierry Reding 	timeout = jiffies + msecs_to_jiffies(timeout);
937*a82752e1SThierry Reding 
938*a82752e1SThierry Reding 	while (time_before(jiffies, timeout)) {
939*a82752e1SThierry Reding 		value = tegra_sor_readl(sor, SOR_CRC_A);
940*a82752e1SThierry Reding 		if (value & SOR_CRC_A_VALID)
941*a82752e1SThierry Reding 			return 0;
942*a82752e1SThierry Reding 
943*a82752e1SThierry Reding 		usleep_range(100, 200);
944*a82752e1SThierry Reding 	}
945*a82752e1SThierry Reding 
946*a82752e1SThierry Reding 	return -ETIMEDOUT;
947*a82752e1SThierry Reding }
948*a82752e1SThierry Reding 
949*a82752e1SThierry Reding static ssize_t tegra_sor_crc_read(struct file *file, char __user *buffer,
950*a82752e1SThierry Reding 				  size_t size, loff_t *ppos)
951*a82752e1SThierry Reding {
952*a82752e1SThierry Reding 	struct tegra_sor *sor = file->private_data;
953*a82752e1SThierry Reding 	char buf[10];
954*a82752e1SThierry Reding 	ssize_t num;
955*a82752e1SThierry Reding 	u32 value;
956*a82752e1SThierry Reding 	int err;
957*a82752e1SThierry Reding 
958*a82752e1SThierry Reding 	value = tegra_sor_readl(sor, SOR_STATE_1);
959*a82752e1SThierry Reding 	value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
960*a82752e1SThierry Reding 	tegra_sor_writel(sor, value, SOR_STATE_1);
961*a82752e1SThierry Reding 
962*a82752e1SThierry Reding 	value = tegra_sor_readl(sor, SOR_CRC_CNTRL);
963*a82752e1SThierry Reding 	value |= SOR_CRC_CNTRL_ENABLE;
964*a82752e1SThierry Reding 	tegra_sor_writel(sor, value, SOR_CRC_CNTRL);
965*a82752e1SThierry Reding 
966*a82752e1SThierry Reding 	value = tegra_sor_readl(sor, SOR_TEST);
967*a82752e1SThierry Reding 	value &= ~SOR_TEST_CRC_POST_SERIALIZE;
968*a82752e1SThierry Reding 	tegra_sor_writel(sor, value, SOR_TEST);
969*a82752e1SThierry Reding 
970*a82752e1SThierry Reding 	err = tegra_sor_crc_wait(sor, 100);
971*a82752e1SThierry Reding 	if (err < 0)
972*a82752e1SThierry Reding 		return err;
973*a82752e1SThierry Reding 
974*a82752e1SThierry Reding 	tegra_sor_writel(sor, SOR_CRC_A_RESET, SOR_CRC_A);
975*a82752e1SThierry Reding 	value = tegra_sor_readl(sor, SOR_CRC_B);
976*a82752e1SThierry Reding 
977*a82752e1SThierry Reding 	num = scnprintf(buf, sizeof(buf), "%08x\n", value);
978*a82752e1SThierry Reding 
979*a82752e1SThierry Reding 	return simple_read_from_buffer(buffer, size, ppos, buf, num);
980*a82752e1SThierry Reding }
981*a82752e1SThierry Reding 
982*a82752e1SThierry Reding static const struct file_operations tegra_sor_crc_fops = {
983*a82752e1SThierry Reding 	.owner = THIS_MODULE,
984*a82752e1SThierry Reding 	.open = tegra_sor_crc_open,
985*a82752e1SThierry Reding 	.read = tegra_sor_crc_read,
986*a82752e1SThierry Reding 	.release = tegra_sor_crc_release,
987*a82752e1SThierry Reding };
988*a82752e1SThierry Reding 
989*a82752e1SThierry Reding static int tegra_sor_debugfs_init(struct tegra_sor *sor, struct dentry *root)
990*a82752e1SThierry Reding {
991*a82752e1SThierry Reding 	struct dentry *entry;
992*a82752e1SThierry Reding 	int err = 0;
993*a82752e1SThierry Reding 
994*a82752e1SThierry Reding 	sor->debugfs = debugfs_create_dir("sor", root);
995*a82752e1SThierry Reding 	if (!sor->debugfs)
996*a82752e1SThierry Reding 		return -ENOMEM;
997*a82752e1SThierry Reding 
998*a82752e1SThierry Reding 	entry = debugfs_create_file("crc", 0644, sor->debugfs, sor,
999*a82752e1SThierry Reding 				    &tegra_sor_crc_fops);
1000*a82752e1SThierry Reding 	if (!entry) {
1001*a82752e1SThierry Reding 		dev_err(sor->dev,
1002*a82752e1SThierry Reding 			"cannot create /sys/kernel/debug/dri/%s/sor/crc\n",
1003*a82752e1SThierry Reding 			root->d_name.name);
1004*a82752e1SThierry Reding 		err = -ENOMEM;
1005*a82752e1SThierry Reding 		goto remove;
1006*a82752e1SThierry Reding 	}
1007*a82752e1SThierry Reding 
1008*a82752e1SThierry Reding 	return err;
1009*a82752e1SThierry Reding 
1010*a82752e1SThierry Reding remove:
1011*a82752e1SThierry Reding 	debugfs_remove(sor->debugfs);
1012*a82752e1SThierry Reding 	sor->debugfs = NULL;
1013*a82752e1SThierry Reding 	return err;
1014*a82752e1SThierry Reding }
1015*a82752e1SThierry Reding 
1016*a82752e1SThierry Reding static int tegra_sor_debugfs_exit(struct tegra_sor *sor)
1017*a82752e1SThierry Reding {
1018*a82752e1SThierry Reding 	debugfs_remove(sor->debugfs);
1019*a82752e1SThierry Reding 	sor->debugfs = NULL;
1020*a82752e1SThierry Reding 
1021*a82752e1SThierry Reding 	return 0;
1022*a82752e1SThierry Reding }
1023*a82752e1SThierry Reding 
10246b6b6042SThierry Reding static int tegra_sor_init(struct host1x_client *client)
10256b6b6042SThierry Reding {
10266b6b6042SThierry Reding 	struct tegra_drm *tegra = dev_get_drvdata(client->parent);
10276b6b6042SThierry Reding 	struct tegra_sor *sor = host1x_client_to_sor(client);
10286b6b6042SThierry Reding 	int err;
10296b6b6042SThierry Reding 
10306b6b6042SThierry Reding 	if (!sor->dpaux)
10316b6b6042SThierry Reding 		return -ENODEV;
10326b6b6042SThierry Reding 
10336b6b6042SThierry Reding 	sor->output.type = TEGRA_OUTPUT_EDP;
10346b6b6042SThierry Reding 
10356b6b6042SThierry Reding 	sor->output.dev = sor->dev;
10366b6b6042SThierry Reding 	sor->output.ops = &sor_ops;
10376b6b6042SThierry Reding 
10386b6b6042SThierry Reding 	err = tegra_output_init(tegra->drm, &sor->output);
10396b6b6042SThierry Reding 	if (err < 0) {
10406b6b6042SThierry Reding 		dev_err(sor->dev, "output setup failed: %d\n", err);
10416b6b6042SThierry Reding 		return err;
10426b6b6042SThierry Reding 	}
10436b6b6042SThierry Reding 
1044*a82752e1SThierry Reding 	if (IS_ENABLED(CONFIG_DEBUG_FS)) {
1045*a82752e1SThierry Reding 		struct dentry *root = tegra->drm->primary->debugfs_root;
1046*a82752e1SThierry Reding 
1047*a82752e1SThierry Reding 		err = tegra_sor_debugfs_init(sor, root);
1048*a82752e1SThierry Reding 		if (err < 0)
1049*a82752e1SThierry Reding 			dev_err(sor->dev, "debugfs setup failed: %d\n", err);
1050*a82752e1SThierry Reding 	}
1051*a82752e1SThierry Reding 
10526b6b6042SThierry Reding 	if (sor->dpaux) {
10536b6b6042SThierry Reding 		err = tegra_dpaux_attach(sor->dpaux, &sor->output);
10546b6b6042SThierry Reding 		if (err < 0) {
10556b6b6042SThierry Reding 			dev_err(sor->dev, "failed to attach DP: %d\n", err);
10566b6b6042SThierry Reding 			return err;
10576b6b6042SThierry Reding 		}
10586b6b6042SThierry Reding 	}
10596b6b6042SThierry Reding 
10606b6b6042SThierry Reding 	return 0;
10616b6b6042SThierry Reding }
10626b6b6042SThierry Reding 
10636b6b6042SThierry Reding static int tegra_sor_exit(struct host1x_client *client)
10646b6b6042SThierry Reding {
10656b6b6042SThierry Reding 	struct tegra_sor *sor = host1x_client_to_sor(client);
10666b6b6042SThierry Reding 	int err;
10676b6b6042SThierry Reding 
10686b6b6042SThierry Reding 	err = tegra_output_disable(&sor->output);
10696b6b6042SThierry Reding 	if (err < 0) {
10706b6b6042SThierry Reding 		dev_err(sor->dev, "output failed to disable: %d\n", err);
10716b6b6042SThierry Reding 		return err;
10726b6b6042SThierry Reding 	}
10736b6b6042SThierry Reding 
10746b6b6042SThierry Reding 	if (sor->dpaux) {
10756b6b6042SThierry Reding 		err = tegra_dpaux_detach(sor->dpaux);
10766b6b6042SThierry Reding 		if (err < 0) {
10776b6b6042SThierry Reding 			dev_err(sor->dev, "failed to detach DP: %d\n", err);
10786b6b6042SThierry Reding 			return err;
10796b6b6042SThierry Reding 		}
10806b6b6042SThierry Reding 	}
10816b6b6042SThierry Reding 
1082*a82752e1SThierry Reding 	if (IS_ENABLED(CONFIG_DEBUG_FS)) {
1083*a82752e1SThierry Reding 		err = tegra_sor_debugfs_exit(sor);
1084*a82752e1SThierry Reding 		if (err < 0)
1085*a82752e1SThierry Reding 			dev_err(sor->dev, "debugfs cleanup failed: %d\n", err);
1086*a82752e1SThierry Reding 	}
1087*a82752e1SThierry Reding 
10886b6b6042SThierry Reding 	err = tegra_output_exit(&sor->output);
10896b6b6042SThierry Reding 	if (err < 0) {
10906b6b6042SThierry Reding 		dev_err(sor->dev, "output cleanup failed: %d\n", err);
10916b6b6042SThierry Reding 		return err;
10926b6b6042SThierry Reding 	}
10936b6b6042SThierry Reding 
10946b6b6042SThierry Reding 	return 0;
10956b6b6042SThierry Reding }
10966b6b6042SThierry Reding 
10976b6b6042SThierry Reding static const struct host1x_client_ops sor_client_ops = {
10986b6b6042SThierry Reding 	.init = tegra_sor_init,
10996b6b6042SThierry Reding 	.exit = tegra_sor_exit,
11006b6b6042SThierry Reding };
11016b6b6042SThierry Reding 
11026b6b6042SThierry Reding static int tegra_sor_probe(struct platform_device *pdev)
11036b6b6042SThierry Reding {
11046b6b6042SThierry Reding 	struct device_node *np;
11056b6b6042SThierry Reding 	struct tegra_sor *sor;
11066b6b6042SThierry Reding 	struct resource *regs;
11076b6b6042SThierry Reding 	int err;
11086b6b6042SThierry Reding 
11096b6b6042SThierry Reding 	sor = devm_kzalloc(&pdev->dev, sizeof(*sor), GFP_KERNEL);
11106b6b6042SThierry Reding 	if (!sor)
11116b6b6042SThierry Reding 		return -ENOMEM;
11126b6b6042SThierry Reding 
11136b6b6042SThierry Reding 	sor->output.dev = sor->dev = &pdev->dev;
11146b6b6042SThierry Reding 
11156b6b6042SThierry Reding 	np = of_parse_phandle(pdev->dev.of_node, "nvidia,dpaux", 0);
11166b6b6042SThierry Reding 	if (np) {
11176b6b6042SThierry Reding 		sor->dpaux = tegra_dpaux_find_by_of_node(np);
11186b6b6042SThierry Reding 		of_node_put(np);
11196b6b6042SThierry Reding 
11206b6b6042SThierry Reding 		if (!sor->dpaux)
11216b6b6042SThierry Reding 			return -EPROBE_DEFER;
11226b6b6042SThierry Reding 	}
11236b6b6042SThierry Reding 
11246b6b6042SThierry Reding 	err = tegra_output_probe(&sor->output);
11256b6b6042SThierry Reding 	if (err < 0)
11266b6b6042SThierry Reding 		return err;
11276b6b6042SThierry Reding 
11286b6b6042SThierry Reding 	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
11296b6b6042SThierry Reding 	sor->regs = devm_ioremap_resource(&pdev->dev, regs);
11306b6b6042SThierry Reding 	if (IS_ERR(sor->regs))
11316b6b6042SThierry Reding 		return PTR_ERR(sor->regs);
11326b6b6042SThierry Reding 
11336b6b6042SThierry Reding 	sor->rst = devm_reset_control_get(&pdev->dev, "sor");
11346b6b6042SThierry Reding 	if (IS_ERR(sor->rst))
11356b6b6042SThierry Reding 		return PTR_ERR(sor->rst);
11366b6b6042SThierry Reding 
11376b6b6042SThierry Reding 	sor->clk = devm_clk_get(&pdev->dev, NULL);
11386b6b6042SThierry Reding 	if (IS_ERR(sor->clk))
11396b6b6042SThierry Reding 		return PTR_ERR(sor->clk);
11406b6b6042SThierry Reding 
11416b6b6042SThierry Reding 	sor->clk_parent = devm_clk_get(&pdev->dev, "parent");
11426b6b6042SThierry Reding 	if (IS_ERR(sor->clk_parent))
11436b6b6042SThierry Reding 		return PTR_ERR(sor->clk_parent);
11446b6b6042SThierry Reding 
11456b6b6042SThierry Reding 	err = clk_prepare_enable(sor->clk_parent);
11466b6b6042SThierry Reding 	if (err < 0)
11476b6b6042SThierry Reding 		return err;
11486b6b6042SThierry Reding 
11496b6b6042SThierry Reding 	sor->clk_safe = devm_clk_get(&pdev->dev, "safe");
11506b6b6042SThierry Reding 	if (IS_ERR(sor->clk_safe))
11516b6b6042SThierry Reding 		return PTR_ERR(sor->clk_safe);
11526b6b6042SThierry Reding 
11536b6b6042SThierry Reding 	err = clk_prepare_enable(sor->clk_safe);
11546b6b6042SThierry Reding 	if (err < 0)
11556b6b6042SThierry Reding 		return err;
11566b6b6042SThierry Reding 
11576b6b6042SThierry Reding 	sor->clk_dp = devm_clk_get(&pdev->dev, "dp");
11586b6b6042SThierry Reding 	if (IS_ERR(sor->clk_dp))
11596b6b6042SThierry Reding 		return PTR_ERR(sor->clk_dp);
11606b6b6042SThierry Reding 
11616b6b6042SThierry Reding 	err = clk_prepare_enable(sor->clk_dp);
11626b6b6042SThierry Reding 	if (err < 0)
11636b6b6042SThierry Reding 		return err;
11646b6b6042SThierry Reding 
11656b6b6042SThierry Reding 	INIT_LIST_HEAD(&sor->client.list);
11666b6b6042SThierry Reding 	sor->client.ops = &sor_client_ops;
11676b6b6042SThierry Reding 	sor->client.dev = &pdev->dev;
11686b6b6042SThierry Reding 
11696b6b6042SThierry Reding 	err = host1x_client_register(&sor->client);
11706b6b6042SThierry Reding 	if (err < 0) {
11716b6b6042SThierry Reding 		dev_err(&pdev->dev, "failed to register host1x client: %d\n",
11726b6b6042SThierry Reding 			err);
11736b6b6042SThierry Reding 		return err;
11746b6b6042SThierry Reding 	}
11756b6b6042SThierry Reding 
11766b6b6042SThierry Reding 	platform_set_drvdata(pdev, sor);
11776b6b6042SThierry Reding 
11786b6b6042SThierry Reding 	return 0;
11796b6b6042SThierry Reding }
11806b6b6042SThierry Reding 
11816b6b6042SThierry Reding static int tegra_sor_remove(struct platform_device *pdev)
11826b6b6042SThierry Reding {
11836b6b6042SThierry Reding 	struct tegra_sor *sor = platform_get_drvdata(pdev);
11846b6b6042SThierry Reding 	int err;
11856b6b6042SThierry Reding 
11866b6b6042SThierry Reding 	err = host1x_client_unregister(&sor->client);
11876b6b6042SThierry Reding 	if (err < 0) {
11886b6b6042SThierry Reding 		dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
11896b6b6042SThierry Reding 			err);
11906b6b6042SThierry Reding 		return err;
11916b6b6042SThierry Reding 	}
11926b6b6042SThierry Reding 
11936b6b6042SThierry Reding 	clk_disable_unprepare(sor->clk_parent);
11946b6b6042SThierry Reding 	clk_disable_unprepare(sor->clk_safe);
11956b6b6042SThierry Reding 	clk_disable_unprepare(sor->clk_dp);
11966b6b6042SThierry Reding 	clk_disable_unprepare(sor->clk);
11976b6b6042SThierry Reding 
11986b6b6042SThierry Reding 	return 0;
11996b6b6042SThierry Reding }
12006b6b6042SThierry Reding 
12016b6b6042SThierry Reding static const struct of_device_id tegra_sor_of_match[] = {
12026b6b6042SThierry Reding 	{ .compatible = "nvidia,tegra124-sor", },
12036b6b6042SThierry Reding 	{ },
12046b6b6042SThierry Reding };
12056b6b6042SThierry Reding 
12066b6b6042SThierry Reding struct platform_driver tegra_sor_driver = {
12076b6b6042SThierry Reding 	.driver = {
12086b6b6042SThierry Reding 		.name = "tegra-sor",
12096b6b6042SThierry Reding 		.of_match_table = tegra_sor_of_match,
12106b6b6042SThierry Reding 	},
12116b6b6042SThierry Reding 	.probe = tegra_sor_probe,
12126b6b6042SThierry Reding 	.remove = tegra_sor_remove,
12136b6b6042SThierry Reding };
1214