Thierry Reding | 6b6b604 | 2013-11-15 16:06:05 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 NVIDIA Corporation |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/clk.h> |
Thierry Reding | a82752e | 2014-01-31 10:02:15 +0100 | [diff] [blame] | 10 | #include <linux/debugfs.h> |
Thierry Reding | 6b6b604 | 2013-11-15 16:06:05 +0100 | [diff] [blame] | 11 | #include <linux/io.h> |
| 12 | #include <linux/platform_device.h> |
| 13 | #include <linux/reset.h> |
| 14 | #include <linux/tegra-powergate.h> |
| 15 | |
| 16 | #include <drm/drm_dp_helper.h> |
| 17 | |
| 18 | #include "dc.h" |
| 19 | #include "drm.h" |
| 20 | #include "sor.h" |
| 21 | |
| 22 | struct tegra_sor { |
| 23 | struct host1x_client client; |
| 24 | struct tegra_output output; |
| 25 | struct device *dev; |
| 26 | |
| 27 | void __iomem *regs; |
| 28 | |
| 29 | struct reset_control *rst; |
| 30 | struct clk *clk_parent; |
| 31 | struct clk *clk_safe; |
| 32 | struct clk *clk_dp; |
| 33 | struct clk *clk; |
| 34 | |
| 35 | struct tegra_dpaux *dpaux; |
| 36 | |
| 37 | bool enabled; |
Thierry Reding | a82752e | 2014-01-31 10:02:15 +0100 | [diff] [blame] | 38 | |
| 39 | struct dentry *debugfs; |
Thierry Reding | 6b6b604 | 2013-11-15 16:06:05 +0100 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | static inline struct tegra_sor * |
| 43 | host1x_client_to_sor(struct host1x_client *client) |
| 44 | { |
| 45 | return container_of(client, struct tegra_sor, client); |
| 46 | } |
| 47 | |
| 48 | static inline struct tegra_sor *to_sor(struct tegra_output *output) |
| 49 | { |
| 50 | return container_of(output, struct tegra_sor, output); |
| 51 | } |
| 52 | |
| 53 | static inline unsigned long tegra_sor_readl(struct tegra_sor *sor, |
| 54 | unsigned long offset) |
| 55 | { |
| 56 | return readl(sor->regs + (offset << 2)); |
| 57 | } |
| 58 | |
| 59 | static inline void tegra_sor_writel(struct tegra_sor *sor, unsigned long value, |
| 60 | unsigned long offset) |
| 61 | { |
| 62 | writel(value, sor->regs + (offset << 2)); |
| 63 | } |
| 64 | |
| 65 | static int tegra_sor_dp_train_fast(struct tegra_sor *sor, |
| 66 | struct drm_dp_link *link) |
| 67 | { |
| 68 | unsigned long value; |
| 69 | unsigned int i; |
| 70 | u8 pattern; |
| 71 | int err; |
| 72 | |
| 73 | /* setup lane parameters */ |
| 74 | value = SOR_LANE_DRIVE_CURRENT_LANE3(0x40) | |
| 75 | SOR_LANE_DRIVE_CURRENT_LANE2(0x40) | |
| 76 | SOR_LANE_DRIVE_CURRENT_LANE1(0x40) | |
| 77 | SOR_LANE_DRIVE_CURRENT_LANE0(0x40); |
| 78 | tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT_0); |
| 79 | |
| 80 | value = SOR_LANE_PREEMPHASIS_LANE3(0x0f) | |
| 81 | SOR_LANE_PREEMPHASIS_LANE2(0x0f) | |
| 82 | SOR_LANE_PREEMPHASIS_LANE1(0x0f) | |
| 83 | SOR_LANE_PREEMPHASIS_LANE0(0x0f); |
| 84 | tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS_0); |
| 85 | |
| 86 | value = SOR_LANE_POST_CURSOR_LANE3(0x00) | |
| 87 | SOR_LANE_POST_CURSOR_LANE2(0x00) | |
| 88 | SOR_LANE_POST_CURSOR_LANE1(0x00) | |
| 89 | SOR_LANE_POST_CURSOR_LANE0(0x00); |
| 90 | tegra_sor_writel(sor, value, SOR_LANE_POST_CURSOR_0); |
| 91 | |
| 92 | /* disable LVDS mode */ |
| 93 | tegra_sor_writel(sor, 0, SOR_LVDS); |
| 94 | |
| 95 | value = tegra_sor_readl(sor, SOR_DP_PADCTL_0); |
| 96 | value |= SOR_DP_PADCTL_TX_PU_ENABLE; |
| 97 | value &= ~SOR_DP_PADCTL_TX_PU_MASK; |
| 98 | value |= SOR_DP_PADCTL_TX_PU(2); /* XXX: don't hardcode? */ |
| 99 | tegra_sor_writel(sor, value, SOR_DP_PADCTL_0); |
| 100 | |
| 101 | value = tegra_sor_readl(sor, SOR_DP_PADCTL_0); |
| 102 | value |= SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 | |
| 103 | SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0; |
| 104 | tegra_sor_writel(sor, value, SOR_DP_PADCTL_0); |
| 105 | |
| 106 | usleep_range(10, 100); |
| 107 | |
| 108 | value = tegra_sor_readl(sor, SOR_DP_PADCTL_0); |
| 109 | value &= ~(SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 | |
| 110 | SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0); |
| 111 | tegra_sor_writel(sor, value, SOR_DP_PADCTL_0); |
| 112 | |
| 113 | err = tegra_dpaux_prepare(sor->dpaux, DP_SET_ANSI_8B10B); |
| 114 | if (err < 0) |
| 115 | return err; |
| 116 | |
| 117 | for (i = 0, value = 0; i < link->num_lanes; i++) { |
| 118 | unsigned long lane = SOR_DP_TPG_CHANNEL_CODING | |
| 119 | SOR_DP_TPG_SCRAMBLER_NONE | |
| 120 | SOR_DP_TPG_PATTERN_TRAIN1; |
| 121 | value = (value << 8) | lane; |
| 122 | } |
| 123 | |
| 124 | tegra_sor_writel(sor, value, SOR_DP_TPG); |
| 125 | |
| 126 | pattern = DP_TRAINING_PATTERN_1; |
| 127 | |
| 128 | err = tegra_dpaux_train(sor->dpaux, link, pattern); |
| 129 | if (err < 0) |
| 130 | return err; |
| 131 | |
| 132 | value = tegra_sor_readl(sor, SOR_DP_SPARE_0); |
| 133 | value |= SOR_DP_SPARE_SEQ_ENABLE; |
| 134 | value &= ~SOR_DP_SPARE_PANEL_INTERNAL; |
| 135 | value |= SOR_DP_SPARE_MACRO_SOR_CLK; |
| 136 | tegra_sor_writel(sor, value, SOR_DP_SPARE_0); |
| 137 | |
| 138 | for (i = 0, value = 0; i < link->num_lanes; i++) { |
| 139 | unsigned long lane = SOR_DP_TPG_CHANNEL_CODING | |
| 140 | SOR_DP_TPG_SCRAMBLER_NONE | |
| 141 | SOR_DP_TPG_PATTERN_TRAIN2; |
| 142 | value = (value << 8) | lane; |
| 143 | } |
| 144 | |
| 145 | tegra_sor_writel(sor, value, SOR_DP_TPG); |
| 146 | |
| 147 | pattern = DP_LINK_SCRAMBLING_DISABLE | DP_TRAINING_PATTERN_2; |
| 148 | |
| 149 | err = tegra_dpaux_train(sor->dpaux, link, pattern); |
| 150 | if (err < 0) |
| 151 | return err; |
| 152 | |
| 153 | for (i = 0, value = 0; i < link->num_lanes; i++) { |
| 154 | unsigned long lane = SOR_DP_TPG_CHANNEL_CODING | |
| 155 | SOR_DP_TPG_SCRAMBLER_GALIOS | |
| 156 | SOR_DP_TPG_PATTERN_NONE; |
| 157 | value = (value << 8) | lane; |
| 158 | } |
| 159 | |
| 160 | tegra_sor_writel(sor, value, SOR_DP_TPG); |
| 161 | |
| 162 | pattern = DP_TRAINING_PATTERN_DISABLE; |
| 163 | |
| 164 | err = tegra_dpaux_train(sor->dpaux, link, pattern); |
| 165 | if (err < 0) |
| 166 | return err; |
| 167 | |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | static void tegra_sor_super_update(struct tegra_sor *sor) |
| 172 | { |
| 173 | tegra_sor_writel(sor, 0, SOR_SUPER_STATE_0); |
| 174 | tegra_sor_writel(sor, 1, SOR_SUPER_STATE_0); |
| 175 | tegra_sor_writel(sor, 0, SOR_SUPER_STATE_0); |
| 176 | } |
| 177 | |
| 178 | static void tegra_sor_update(struct tegra_sor *sor) |
| 179 | { |
| 180 | tegra_sor_writel(sor, 0, SOR_STATE_0); |
| 181 | tegra_sor_writel(sor, 1, SOR_STATE_0); |
| 182 | tegra_sor_writel(sor, 0, SOR_STATE_0); |
| 183 | } |
| 184 | |
| 185 | static int tegra_sor_setup_pwm(struct tegra_sor *sor, unsigned long timeout) |
| 186 | { |
| 187 | unsigned long value; |
| 188 | |
| 189 | value = tegra_sor_readl(sor, SOR_PWM_DIV); |
| 190 | value &= ~SOR_PWM_DIV_MASK; |
| 191 | value |= 0x400; /* period */ |
| 192 | tegra_sor_writel(sor, value, SOR_PWM_DIV); |
| 193 | |
| 194 | value = tegra_sor_readl(sor, SOR_PWM_CTL); |
| 195 | value &= ~SOR_PWM_CTL_DUTY_CYCLE_MASK; |
| 196 | value |= 0x400; /* duty cycle */ |
| 197 | value &= ~SOR_PWM_CTL_CLK_SEL; /* clock source: PCLK */ |
| 198 | value |= SOR_PWM_CTL_TRIGGER; |
| 199 | tegra_sor_writel(sor, value, SOR_PWM_CTL); |
| 200 | |
| 201 | timeout = jiffies + msecs_to_jiffies(timeout); |
| 202 | |
| 203 | while (time_before(jiffies, timeout)) { |
| 204 | value = tegra_sor_readl(sor, SOR_PWM_CTL); |
| 205 | if ((value & SOR_PWM_CTL_TRIGGER) == 0) |
| 206 | return 0; |
| 207 | |
| 208 | usleep_range(25, 100); |
| 209 | } |
| 210 | |
| 211 | return -ETIMEDOUT; |
| 212 | } |
| 213 | |
| 214 | static int tegra_sor_attach(struct tegra_sor *sor) |
| 215 | { |
| 216 | unsigned long value, timeout; |
| 217 | |
| 218 | /* wake up in normal mode */ |
| 219 | value = tegra_sor_readl(sor, SOR_SUPER_STATE_1); |
| 220 | value |= SOR_SUPER_STATE_HEAD_MODE_AWAKE; |
| 221 | value |= SOR_SUPER_STATE_MODE_NORMAL; |
| 222 | tegra_sor_writel(sor, value, SOR_SUPER_STATE_1); |
| 223 | tegra_sor_super_update(sor); |
| 224 | |
| 225 | /* attach */ |
| 226 | value = tegra_sor_readl(sor, SOR_SUPER_STATE_1); |
| 227 | value |= SOR_SUPER_STATE_ATTACHED; |
| 228 | tegra_sor_writel(sor, value, SOR_SUPER_STATE_1); |
| 229 | tegra_sor_super_update(sor); |
| 230 | |
| 231 | timeout = jiffies + msecs_to_jiffies(250); |
| 232 | |
| 233 | while (time_before(jiffies, timeout)) { |
| 234 | value = tegra_sor_readl(sor, SOR_TEST); |
| 235 | if ((value & SOR_TEST_ATTACHED) != 0) |
| 236 | return 0; |
| 237 | |
| 238 | usleep_range(25, 100); |
| 239 | } |
| 240 | |
| 241 | return -ETIMEDOUT; |
| 242 | } |
| 243 | |
| 244 | static int tegra_sor_wakeup(struct tegra_sor *sor) |
| 245 | { |
| 246 | struct tegra_dc *dc = to_tegra_dc(sor->output.encoder.crtc); |
| 247 | unsigned long value, timeout; |
| 248 | |
| 249 | /* enable display controller outputs */ |
| 250 | value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL); |
| 251 | value |= PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE | |
| 252 | PW4_ENABLE | PM0_ENABLE | PM1_ENABLE; |
| 253 | tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL); |
| 254 | |
| 255 | tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL); |
| 256 | tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL); |
| 257 | |
| 258 | timeout = jiffies + msecs_to_jiffies(250); |
| 259 | |
| 260 | /* wait for head to wake up */ |
| 261 | while (time_before(jiffies, timeout)) { |
| 262 | value = tegra_sor_readl(sor, SOR_TEST); |
| 263 | value &= SOR_TEST_HEAD_MODE_MASK; |
| 264 | |
| 265 | if (value == SOR_TEST_HEAD_MODE_AWAKE) |
| 266 | return 0; |
| 267 | |
| 268 | usleep_range(25, 100); |
| 269 | } |
| 270 | |
| 271 | return -ETIMEDOUT; |
| 272 | } |
| 273 | |
| 274 | static int tegra_sor_power_up(struct tegra_sor *sor, unsigned long timeout) |
| 275 | { |
| 276 | unsigned long value; |
| 277 | |
| 278 | value = tegra_sor_readl(sor, SOR_PWR); |
| 279 | value |= SOR_PWR_TRIGGER | SOR_PWR_NORMAL_STATE_PU; |
| 280 | tegra_sor_writel(sor, value, SOR_PWR); |
| 281 | |
| 282 | timeout = jiffies + msecs_to_jiffies(timeout); |
| 283 | |
| 284 | while (time_before(jiffies, timeout)) { |
| 285 | value = tegra_sor_readl(sor, SOR_PWR); |
| 286 | if ((value & SOR_PWR_TRIGGER) == 0) |
| 287 | return 0; |
| 288 | |
| 289 | usleep_range(25, 100); |
| 290 | } |
| 291 | |
| 292 | return -ETIMEDOUT; |
| 293 | } |
| 294 | |
| 295 | static int tegra_output_sor_enable(struct tegra_output *output) |
| 296 | { |
| 297 | struct tegra_dc *dc = to_tegra_dc(output->encoder.crtc); |
| 298 | struct drm_display_mode *mode = &dc->base.mode; |
| 299 | unsigned int vbe, vse, hbe, hse, vbs, hbs, i; |
| 300 | struct tegra_sor *sor = to_sor(output); |
| 301 | unsigned long value; |
| 302 | int err; |
| 303 | |
| 304 | if (sor->enabled) |
| 305 | return 0; |
| 306 | |
| 307 | err = clk_prepare_enable(sor->clk); |
| 308 | if (err < 0) |
| 309 | return err; |
| 310 | |
| 311 | reset_control_deassert(sor->rst); |
| 312 | |
| 313 | if (sor->dpaux) { |
| 314 | err = tegra_dpaux_enable(sor->dpaux); |
| 315 | if (err < 0) |
| 316 | dev_err(sor->dev, "failed to enable DP: %d\n", err); |
| 317 | } |
| 318 | |
| 319 | err = clk_set_parent(sor->clk, sor->clk_safe); |
| 320 | if (err < 0) |
| 321 | dev_err(sor->dev, "failed to set safe parent clock: %d\n", err); |
| 322 | |
| 323 | value = tegra_sor_readl(sor, SOR_CLK_CNTRL); |
| 324 | value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK; |
| 325 | value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK; |
| 326 | tegra_sor_writel(sor, value, SOR_CLK_CNTRL); |
| 327 | |
| 328 | value = tegra_sor_readl(sor, SOR_PLL_2); |
| 329 | value &= ~SOR_PLL_2_BANDGAP_POWERDOWN; |
| 330 | tegra_sor_writel(sor, value, SOR_PLL_2); |
| 331 | usleep_range(20, 100); |
| 332 | |
| 333 | value = tegra_sor_readl(sor, SOR_PLL_3); |
| 334 | value |= SOR_PLL_3_PLL_VDD_MODE_V3_3; |
| 335 | tegra_sor_writel(sor, value, SOR_PLL_3); |
| 336 | |
| 337 | value = SOR_PLL_0_ICHPMP(0xf) | SOR_PLL_0_VCOCAP_RST | |
| 338 | SOR_PLL_0_PLLREG_LEVEL_V45 | SOR_PLL_0_RESISTOR_EXT; |
| 339 | tegra_sor_writel(sor, value, SOR_PLL_0); |
| 340 | |
| 341 | value = tegra_sor_readl(sor, SOR_PLL_2); |
| 342 | value |= SOR_PLL_2_SEQ_PLLCAPPD; |
| 343 | value &= ~SOR_PLL_2_SEQ_PLLCAPPD_ENFORCE; |
| 344 | value |= SOR_PLL_2_LVDS_ENABLE; |
| 345 | tegra_sor_writel(sor, value, SOR_PLL_2); |
| 346 | |
| 347 | value = SOR_PLL_1_TERM_COMPOUT | SOR_PLL_1_TMDS_TERM; |
| 348 | tegra_sor_writel(sor, value, SOR_PLL_1); |
| 349 | |
| 350 | while (true) { |
| 351 | value = tegra_sor_readl(sor, SOR_PLL_2); |
| 352 | if ((value & SOR_PLL_2_SEQ_PLLCAPPD_ENFORCE) == 0) |
| 353 | break; |
| 354 | |
| 355 | usleep_range(250, 1000); |
| 356 | } |
| 357 | |
| 358 | value = tegra_sor_readl(sor, SOR_PLL_2); |
| 359 | value &= ~SOR_PLL_2_POWERDOWN_OVERRIDE; |
| 360 | value &= ~SOR_PLL_2_PORT_POWERDOWN; |
| 361 | tegra_sor_writel(sor, value, SOR_PLL_2); |
| 362 | |
| 363 | /* |
| 364 | * power up |
| 365 | */ |
| 366 | |
| 367 | /* set safe link bandwidth (1.62 Gbps) */ |
| 368 | value = tegra_sor_readl(sor, SOR_CLK_CNTRL); |
| 369 | value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK; |
| 370 | value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G1_62; |
| 371 | tegra_sor_writel(sor, value, SOR_CLK_CNTRL); |
| 372 | |
| 373 | /* step 1 */ |
| 374 | value = tegra_sor_readl(sor, SOR_PLL_2); |
| 375 | value |= SOR_PLL_2_SEQ_PLLCAPPD_ENFORCE | SOR_PLL_2_PORT_POWERDOWN | |
| 376 | SOR_PLL_2_BANDGAP_POWERDOWN; |
| 377 | tegra_sor_writel(sor, value, SOR_PLL_2); |
| 378 | |
| 379 | value = tegra_sor_readl(sor, SOR_PLL_0); |
| 380 | value |= SOR_PLL_0_VCOPD | SOR_PLL_0_POWER_OFF; |
| 381 | tegra_sor_writel(sor, value, SOR_PLL_0); |
| 382 | |
| 383 | value = tegra_sor_readl(sor, SOR_DP_PADCTL_0); |
| 384 | value &= ~SOR_DP_PADCTL_PAD_CAL_PD; |
| 385 | tegra_sor_writel(sor, value, SOR_DP_PADCTL_0); |
| 386 | |
| 387 | /* step 2 */ |
| 388 | err = tegra_io_rail_power_on(TEGRA_IO_RAIL_LVDS); |
| 389 | if (err < 0) { |
| 390 | dev_err(sor->dev, "failed to power on I/O rail: %d\n", err); |
| 391 | return err; |
| 392 | } |
| 393 | |
| 394 | usleep_range(5, 100); |
| 395 | |
| 396 | /* step 3 */ |
| 397 | value = tegra_sor_readl(sor, SOR_PLL_2); |
| 398 | value &= ~SOR_PLL_2_BANDGAP_POWERDOWN; |
| 399 | tegra_sor_writel(sor, value, SOR_PLL_2); |
| 400 | |
| 401 | usleep_range(20, 100); |
| 402 | |
| 403 | /* step 4 */ |
| 404 | value = tegra_sor_readl(sor, SOR_PLL_0); |
| 405 | value &= ~SOR_PLL_0_POWER_OFF; |
| 406 | value &= ~SOR_PLL_0_VCOPD; |
| 407 | tegra_sor_writel(sor, value, SOR_PLL_0); |
| 408 | |
| 409 | value = tegra_sor_readl(sor, SOR_PLL_2); |
| 410 | value &= ~SOR_PLL_2_SEQ_PLLCAPPD_ENFORCE; |
| 411 | tegra_sor_writel(sor, value, SOR_PLL_2); |
| 412 | |
| 413 | usleep_range(200, 1000); |
| 414 | |
| 415 | /* step 5 */ |
| 416 | value = tegra_sor_readl(sor, SOR_PLL_2); |
| 417 | value &= ~SOR_PLL_2_PORT_POWERDOWN; |
| 418 | tegra_sor_writel(sor, value, SOR_PLL_2); |
| 419 | |
| 420 | /* switch to DP clock */ |
| 421 | err = clk_set_parent(sor->clk, sor->clk_dp); |
| 422 | if (err < 0) |
| 423 | dev_err(sor->dev, "failed to set DP parent clock: %d\n", err); |
| 424 | |
| 425 | /* power dplanes (XXX parameterize based on link?) */ |
| 426 | value = tegra_sor_readl(sor, SOR_DP_PADCTL_0); |
| 427 | value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 | |
| 428 | SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2; |
| 429 | tegra_sor_writel(sor, value, SOR_DP_PADCTL_0); |
| 430 | |
| 431 | value = tegra_sor_readl(sor, SOR_DP_LINKCTL_0); |
| 432 | value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK; |
| 433 | value |= SOR_DP_LINKCTL_LANE_COUNT(4); |
| 434 | tegra_sor_writel(sor, value, SOR_DP_LINKCTL_0); |
| 435 | |
| 436 | /* start lane sequencer */ |
| 437 | value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN | |
| 438 | SOR_LANE_SEQ_CTL_POWER_STATE_UP; |
| 439 | tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL); |
| 440 | |
| 441 | while (true) { |
| 442 | value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL); |
| 443 | if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0) |
| 444 | break; |
| 445 | |
| 446 | usleep_range(250, 1000); |
| 447 | } |
| 448 | |
| 449 | /* set link bandwidth (2.7 GHz, XXX: parameterize based on link?) */ |
| 450 | value = tegra_sor_readl(sor, SOR_CLK_CNTRL); |
| 451 | value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK; |
| 452 | value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G2_70; |
| 453 | tegra_sor_writel(sor, value, SOR_CLK_CNTRL); |
| 454 | |
| 455 | /* set linkctl */ |
| 456 | value = tegra_sor_readl(sor, SOR_DP_LINKCTL_0); |
| 457 | value |= SOR_DP_LINKCTL_ENABLE; |
| 458 | |
| 459 | value &= ~SOR_DP_LINKCTL_TU_SIZE_MASK; |
| 460 | value |= SOR_DP_LINKCTL_TU_SIZE(59); /* XXX: don't hardcode? */ |
| 461 | |
| 462 | value |= SOR_DP_LINKCTL_ENHANCED_FRAME; |
| 463 | tegra_sor_writel(sor, value, SOR_DP_LINKCTL_0); |
| 464 | |
| 465 | for (i = 0, value = 0; i < 4; i++) { |
| 466 | unsigned long lane = SOR_DP_TPG_CHANNEL_CODING | |
| 467 | SOR_DP_TPG_SCRAMBLER_GALIOS | |
| 468 | SOR_DP_TPG_PATTERN_NONE; |
| 469 | value = (value << 8) | lane; |
| 470 | } |
| 471 | |
| 472 | tegra_sor_writel(sor, value, SOR_DP_TPG); |
| 473 | |
| 474 | value = tegra_sor_readl(sor, SOR_DP_CONFIG_0); |
| 475 | value &= ~SOR_DP_CONFIG_WATERMARK_MASK; |
| 476 | value |= SOR_DP_CONFIG_WATERMARK(14); /* XXX: don't hardcode? */ |
| 477 | |
| 478 | value &= ~SOR_DP_CONFIG_ACTIVE_SYM_COUNT_MASK; |
| 479 | value |= SOR_DP_CONFIG_ACTIVE_SYM_COUNT(47); /* XXX: don't hardcode? */ |
| 480 | |
| 481 | value &= ~SOR_DP_CONFIG_ACTIVE_SYM_FRAC_MASK; |
| 482 | value |= SOR_DP_CONFIG_ACTIVE_SYM_FRAC(9); /* XXX: don't hardcode? */ |
| 483 | |
| 484 | value &= ~SOR_DP_CONFIG_ACTIVE_SYM_POLARITY; /* XXX: don't hardcode? */ |
| 485 | |
| 486 | value |= SOR_DP_CONFIG_ACTIVE_SYM_ENABLE; |
| 487 | value |= SOR_DP_CONFIG_DISPARITY_NEGATIVE; /* XXX: don't hardcode? */ |
| 488 | tegra_sor_writel(sor, value, SOR_DP_CONFIG_0); |
| 489 | |
| 490 | value = tegra_sor_readl(sor, SOR_DP_AUDIO_HBLANK_SYMBOLS); |
| 491 | value &= ~SOR_DP_AUDIO_HBLANK_SYMBOLS_MASK; |
| 492 | value |= 137; /* XXX: don't hardcode? */ |
| 493 | tegra_sor_writel(sor, value, SOR_DP_AUDIO_HBLANK_SYMBOLS); |
| 494 | |
| 495 | value = tegra_sor_readl(sor, SOR_DP_AUDIO_VBLANK_SYMBOLS); |
| 496 | value &= ~SOR_DP_AUDIO_VBLANK_SYMBOLS_MASK; |
| 497 | value |= 2368; /* XXX: don't hardcode? */ |
| 498 | tegra_sor_writel(sor, value, SOR_DP_AUDIO_VBLANK_SYMBOLS); |
| 499 | |
| 500 | /* enable pad calibration logic */ |
| 501 | value = tegra_sor_readl(sor, SOR_DP_PADCTL_0); |
| 502 | value |= SOR_DP_PADCTL_PAD_CAL_PD; |
| 503 | tegra_sor_writel(sor, value, SOR_DP_PADCTL_0); |
| 504 | |
| 505 | if (sor->dpaux) { |
| 506 | /* FIXME: properly convert to struct drm_dp_aux */ |
| 507 | struct drm_dp_aux *aux = (struct drm_dp_aux *)sor->dpaux; |
| 508 | struct drm_dp_link link; |
| 509 | u8 rate, lanes; |
| 510 | |
| 511 | err = drm_dp_link_probe(aux, &link); |
| 512 | if (err < 0) { |
| 513 | dev_err(sor->dev, "failed to probe eDP link: %d\n", |
| 514 | err); |
| 515 | return err; |
| 516 | } |
| 517 | |
| 518 | err = drm_dp_link_power_up(aux, &link); |
| 519 | if (err < 0) { |
| 520 | dev_err(sor->dev, "failed to power up eDP link: %d\n", |
| 521 | err); |
| 522 | return err; |
| 523 | } |
| 524 | |
| 525 | err = drm_dp_link_configure(aux, &link); |
| 526 | if (err < 0) { |
| 527 | dev_err(sor->dev, "failed to configure eDP link: %d\n", |
| 528 | err); |
| 529 | return err; |
| 530 | } |
| 531 | |
| 532 | rate = drm_dp_link_rate_to_bw_code(link.rate); |
| 533 | lanes = link.num_lanes; |
| 534 | |
| 535 | value = tegra_sor_readl(sor, SOR_CLK_CNTRL); |
| 536 | value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK; |
| 537 | value |= SOR_CLK_CNTRL_DP_LINK_SPEED(rate); |
| 538 | tegra_sor_writel(sor, value, SOR_CLK_CNTRL); |
| 539 | |
| 540 | value = tegra_sor_readl(sor, SOR_DP_LINKCTL_0); |
| 541 | value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK; |
| 542 | value |= SOR_DP_LINKCTL_LANE_COUNT(lanes); |
| 543 | |
| 544 | if (link.capabilities & DP_LINK_CAP_ENHANCED_FRAMING) |
| 545 | value |= SOR_DP_LINKCTL_ENHANCED_FRAME; |
| 546 | |
| 547 | tegra_sor_writel(sor, value, SOR_DP_LINKCTL_0); |
| 548 | |
| 549 | /* disable training pattern generator */ |
| 550 | |
| 551 | for (i = 0; i < link.num_lanes; i++) { |
| 552 | unsigned long lane = SOR_DP_TPG_CHANNEL_CODING | |
| 553 | SOR_DP_TPG_SCRAMBLER_GALIOS | |
| 554 | SOR_DP_TPG_PATTERN_NONE; |
| 555 | value = (value << 8) | lane; |
| 556 | } |
| 557 | |
| 558 | tegra_sor_writel(sor, value, SOR_DP_TPG); |
| 559 | |
| 560 | err = tegra_sor_dp_train_fast(sor, &link); |
| 561 | if (err < 0) { |
| 562 | dev_err(sor->dev, "DP fast link training failed: %d\n", |
| 563 | err); |
| 564 | return err; |
| 565 | } |
| 566 | |
| 567 | dev_dbg(sor->dev, "fast link training succeeded\n"); |
| 568 | } |
| 569 | |
| 570 | err = tegra_sor_power_up(sor, 250); |
| 571 | if (err < 0) { |
| 572 | dev_err(sor->dev, "failed to power up SOR: %d\n", err); |
| 573 | return err; |
| 574 | } |
| 575 | |
| 576 | /* start display controller in continuous mode */ |
| 577 | value = tegra_dc_readl(dc, DC_CMD_STATE_ACCESS); |
| 578 | value |= WRITE_MUX; |
| 579 | tegra_dc_writel(dc, value, DC_CMD_STATE_ACCESS); |
| 580 | |
| 581 | tegra_dc_writel(dc, VSYNC_H_POSITION(1), DC_DISP_DISP_TIMING_OPTIONS); |
| 582 | tegra_dc_writel(dc, DISP_CTRL_MODE_C_DISPLAY, DC_CMD_DISPLAY_COMMAND); |
| 583 | |
| 584 | value = tegra_dc_readl(dc, DC_CMD_STATE_ACCESS); |
| 585 | value &= ~WRITE_MUX; |
| 586 | tegra_dc_writel(dc, value, DC_CMD_STATE_ACCESS); |
| 587 | |
| 588 | /* |
| 589 | * configure panel (24bpp, vsync-, hsync-, DP-A protocol, complete |
| 590 | * raster, associate with display controller) |
| 591 | */ |
| 592 | value = SOR_STATE_ASY_PIXELDEPTH_BPP_24_444 | |
| 593 | SOR_STATE_ASY_VSYNCPOL | |
| 594 | SOR_STATE_ASY_HSYNCPOL | |
| 595 | SOR_STATE_ASY_PROTOCOL_DP_A | |
| 596 | SOR_STATE_ASY_CRC_MODE_COMPLETE | |
| 597 | SOR_STATE_ASY_OWNER(dc->pipe + 1); |
| 598 | tegra_sor_writel(sor, value, SOR_STATE_1); |
| 599 | |
| 600 | /* |
| 601 | * TODO: The video timing programming below doesn't seem to match the |
| 602 | * register definitions. |
| 603 | */ |
| 604 | |
| 605 | value = ((mode->vtotal & 0x7fff) << 16) | (mode->htotal & 0x7fff); |
| 606 | tegra_sor_writel(sor, value, SOR_HEAD_STATE_1(0)); |
| 607 | |
| 608 | vse = mode->vsync_end - mode->vsync_start - 1; |
| 609 | hse = mode->hsync_end - mode->hsync_start - 1; |
| 610 | |
| 611 | value = ((vse & 0x7fff) << 16) | (hse & 0x7fff); |
| 612 | tegra_sor_writel(sor, value, SOR_HEAD_STATE_2(0)); |
| 613 | |
| 614 | vbe = vse + (mode->vsync_start - mode->vdisplay); |
| 615 | hbe = hse + (mode->hsync_start - mode->hdisplay); |
| 616 | |
| 617 | value = ((vbe & 0x7fff) << 16) | (hbe & 0x7fff); |
| 618 | tegra_sor_writel(sor, value, SOR_HEAD_STATE_3(0)); |
| 619 | |
| 620 | vbs = vbe + mode->vdisplay; |
| 621 | hbs = hbe + mode->hdisplay; |
| 622 | |
| 623 | value = ((vbs & 0x7fff) << 16) | (hbs & 0x7fff); |
| 624 | tegra_sor_writel(sor, value, SOR_HEAD_STATE_4(0)); |
| 625 | |
| 626 | /* XXX interlaced mode */ |
| 627 | tegra_sor_writel(sor, 0x00000001, SOR_HEAD_STATE_5(0)); |
| 628 | |
| 629 | /* CSTM (LVDS, link A/B, upper) */ |
| 630 | value = SOR_CSTM_LVDS | SOR_CSTM_LINK_ACT_B | SOR_CSTM_LINK_ACT_B | |
| 631 | SOR_CSTM_UPPER; |
| 632 | tegra_sor_writel(sor, value, SOR_CSTM); |
| 633 | |
| 634 | /* PWM setup */ |
| 635 | err = tegra_sor_setup_pwm(sor, 250); |
| 636 | if (err < 0) { |
| 637 | dev_err(sor->dev, "failed to setup PWM: %d\n", err); |
| 638 | return err; |
| 639 | } |
| 640 | |
| 641 | value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS); |
| 642 | value |= SOR_ENABLE; |
| 643 | tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS); |
| 644 | |
| 645 | tegra_sor_update(sor); |
| 646 | |
| 647 | err = tegra_sor_attach(sor); |
| 648 | if (err < 0) { |
| 649 | dev_err(sor->dev, "failed to attach SOR: %d\n", err); |
| 650 | return err; |
| 651 | } |
| 652 | |
| 653 | err = tegra_sor_wakeup(sor); |
| 654 | if (err < 0) { |
| 655 | dev_err(sor->dev, "failed to enable DC: %d\n", err); |
| 656 | return err; |
| 657 | } |
| 658 | |
| 659 | sor->enabled = true; |
| 660 | |
| 661 | return 0; |
| 662 | } |
| 663 | |
| 664 | static int tegra_sor_detach(struct tegra_sor *sor) |
| 665 | { |
| 666 | unsigned long value, timeout; |
| 667 | |
| 668 | /* switch to safe mode */ |
| 669 | value = tegra_sor_readl(sor, SOR_SUPER_STATE_1); |
| 670 | value &= ~SOR_SUPER_STATE_MODE_NORMAL; |
| 671 | tegra_sor_writel(sor, value, SOR_SUPER_STATE_1); |
| 672 | tegra_sor_super_update(sor); |
| 673 | |
| 674 | timeout = jiffies + msecs_to_jiffies(250); |
| 675 | |
| 676 | while (time_before(jiffies, timeout)) { |
| 677 | value = tegra_sor_readl(sor, SOR_PWR); |
| 678 | if (value & SOR_PWR_MODE_SAFE) |
| 679 | break; |
| 680 | } |
| 681 | |
| 682 | if ((value & SOR_PWR_MODE_SAFE) == 0) |
| 683 | return -ETIMEDOUT; |
| 684 | |
| 685 | /* go to sleep */ |
| 686 | value = tegra_sor_readl(sor, SOR_SUPER_STATE_1); |
| 687 | value &= ~SOR_SUPER_STATE_HEAD_MODE_MASK; |
| 688 | tegra_sor_writel(sor, value, SOR_SUPER_STATE_1); |
| 689 | tegra_sor_super_update(sor); |
| 690 | |
| 691 | /* detach */ |
| 692 | value = tegra_sor_readl(sor, SOR_SUPER_STATE_1); |
| 693 | value &= ~SOR_SUPER_STATE_ATTACHED; |
| 694 | tegra_sor_writel(sor, value, SOR_SUPER_STATE_1); |
| 695 | tegra_sor_super_update(sor); |
| 696 | |
| 697 | timeout = jiffies + msecs_to_jiffies(250); |
| 698 | |
| 699 | while (time_before(jiffies, timeout)) { |
| 700 | value = tegra_sor_readl(sor, SOR_TEST); |
| 701 | if ((value & SOR_TEST_ATTACHED) == 0) |
| 702 | break; |
| 703 | |
| 704 | usleep_range(25, 100); |
| 705 | } |
| 706 | |
| 707 | if ((value & SOR_TEST_ATTACHED) != 0) |
| 708 | return -ETIMEDOUT; |
| 709 | |
| 710 | return 0; |
| 711 | } |
| 712 | |
| 713 | static int tegra_sor_power_down(struct tegra_sor *sor) |
| 714 | { |
| 715 | unsigned long value, timeout; |
| 716 | int err; |
| 717 | |
| 718 | value = tegra_sor_readl(sor, SOR_PWR); |
| 719 | value &= ~SOR_PWR_NORMAL_STATE_PU; |
| 720 | value |= SOR_PWR_TRIGGER; |
| 721 | tegra_sor_writel(sor, value, SOR_PWR); |
| 722 | |
| 723 | timeout = jiffies + msecs_to_jiffies(250); |
| 724 | |
| 725 | while (time_before(jiffies, timeout)) { |
| 726 | value = tegra_sor_readl(sor, SOR_PWR); |
| 727 | if ((value & SOR_PWR_TRIGGER) == 0) |
| 728 | return 0; |
| 729 | |
| 730 | usleep_range(25, 100); |
| 731 | } |
| 732 | |
| 733 | if ((value & SOR_PWR_TRIGGER) != 0) |
| 734 | return -ETIMEDOUT; |
| 735 | |
| 736 | err = clk_set_parent(sor->clk, sor->clk_safe); |
| 737 | if (err < 0) |
| 738 | dev_err(sor->dev, "failed to set safe parent clock: %d\n", err); |
| 739 | |
| 740 | value = tegra_sor_readl(sor, SOR_DP_PADCTL_0); |
| 741 | value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 | |
| 742 | SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2); |
| 743 | tegra_sor_writel(sor, value, SOR_DP_PADCTL_0); |
| 744 | |
| 745 | /* stop lane sequencer */ |
| 746 | value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN | |
| 747 | SOR_LANE_SEQ_CTL_POWER_STATE_DOWN; |
| 748 | tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL); |
| 749 | |
| 750 | timeout = jiffies + msecs_to_jiffies(250); |
| 751 | |
| 752 | while (time_before(jiffies, timeout)) { |
| 753 | value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL); |
| 754 | if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0) |
| 755 | break; |
| 756 | |
| 757 | usleep_range(25, 100); |
| 758 | } |
| 759 | |
| 760 | if ((value & SOR_LANE_SEQ_CTL_TRIGGER) != 0) |
| 761 | return -ETIMEDOUT; |
| 762 | |
| 763 | value = tegra_sor_readl(sor, SOR_PLL_2); |
| 764 | value |= SOR_PLL_2_PORT_POWERDOWN; |
| 765 | tegra_sor_writel(sor, value, SOR_PLL_2); |
| 766 | |
| 767 | usleep_range(20, 100); |
| 768 | |
| 769 | value = tegra_sor_readl(sor, SOR_PLL_0); |
| 770 | value |= SOR_PLL_0_POWER_OFF; |
| 771 | value |= SOR_PLL_0_VCOPD; |
| 772 | tegra_sor_writel(sor, value, SOR_PLL_0); |
| 773 | |
| 774 | value = tegra_sor_readl(sor, SOR_PLL_2); |
| 775 | value |= SOR_PLL_2_SEQ_PLLCAPPD; |
| 776 | value |= SOR_PLL_2_SEQ_PLLCAPPD_ENFORCE; |
| 777 | tegra_sor_writel(sor, value, SOR_PLL_2); |
| 778 | |
| 779 | usleep_range(20, 100); |
| 780 | |
| 781 | return 0; |
| 782 | } |
| 783 | |
| 784 | static int tegra_output_sor_disable(struct tegra_output *output) |
| 785 | { |
| 786 | struct tegra_dc *dc = to_tegra_dc(output->encoder.crtc); |
| 787 | struct tegra_sor *sor = to_sor(output); |
| 788 | unsigned long value; |
| 789 | int err; |
| 790 | |
| 791 | if (!sor->enabled) |
| 792 | return 0; |
| 793 | |
| 794 | err = tegra_sor_detach(sor); |
| 795 | if (err < 0) { |
| 796 | dev_err(sor->dev, "failed to detach SOR: %d\n", err); |
| 797 | return err; |
| 798 | } |
| 799 | |
| 800 | tegra_sor_writel(sor, 0, SOR_STATE_1); |
| 801 | tegra_sor_update(sor); |
| 802 | |
| 803 | /* |
| 804 | * The following accesses registers of the display controller, so make |
| 805 | * sure it's only executed when the output is attached to one. |
| 806 | */ |
| 807 | if (dc) { |
| 808 | /* |
| 809 | * XXX: We can't do this here because it causes the SOR to go |
| 810 | * into an erroneous state and the output will look scrambled |
| 811 | * the next time it is enabled. Presumably this is because we |
| 812 | * should be doing this only on the next VBLANK. A possible |
| 813 | * solution would be to queue a "power-off" event to trigger |
| 814 | * this code to be run during the next VBLANK. |
| 815 | */ |
| 816 | /* |
| 817 | value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL); |
| 818 | value &= ~(PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE | |
| 819 | PW4_ENABLE | PM0_ENABLE | PM1_ENABLE); |
| 820 | tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL); |
| 821 | */ |
| 822 | |
| 823 | value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND); |
| 824 | value &= ~DISP_CTRL_MODE_MASK; |
| 825 | tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND); |
| 826 | |
| 827 | value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS); |
| 828 | value &= ~SOR_ENABLE; |
| 829 | tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS); |
| 830 | |
| 831 | tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL); |
| 832 | tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL); |
| 833 | } |
| 834 | |
| 835 | err = tegra_sor_power_down(sor); |
| 836 | if (err < 0) { |
| 837 | dev_err(sor->dev, "failed to power down SOR: %d\n", err); |
| 838 | return err; |
| 839 | } |
| 840 | |
| 841 | if (sor->dpaux) { |
| 842 | err = tegra_dpaux_disable(sor->dpaux); |
| 843 | if (err < 0) { |
| 844 | dev_err(sor->dev, "failed to disable DP: %d\n", err); |
| 845 | return err; |
| 846 | } |
| 847 | } |
| 848 | |
| 849 | err = tegra_io_rail_power_off(TEGRA_IO_RAIL_LVDS); |
| 850 | if (err < 0) { |
| 851 | dev_err(sor->dev, "failed to power off I/O rail: %d\n", err); |
| 852 | return err; |
| 853 | } |
| 854 | |
| 855 | reset_control_assert(sor->rst); |
| 856 | clk_disable_unprepare(sor->clk); |
| 857 | |
| 858 | sor->enabled = false; |
| 859 | |
| 860 | return 0; |
| 861 | } |
| 862 | |
| 863 | static int tegra_output_sor_setup_clock(struct tegra_output *output, |
Thierry Reding | 91eded9 | 2014-03-26 13:32:21 +0100 | [diff] [blame^] | 864 | struct clk *clk, unsigned long pclk, |
| 865 | unsigned int *div) |
Thierry Reding | 6b6b604 | 2013-11-15 16:06:05 +0100 | [diff] [blame] | 866 | { |
| 867 | struct tegra_sor *sor = to_sor(output); |
| 868 | int err; |
| 869 | |
| 870 | /* round to next MHz */ |
Thierry Reding | 91eded9 | 2014-03-26 13:32:21 +0100 | [diff] [blame^] | 871 | pclk = DIV_ROUND_UP(pclk, 1000000) * 1000000; |
Thierry Reding | 6b6b604 | 2013-11-15 16:06:05 +0100 | [diff] [blame] | 872 | |
| 873 | err = clk_set_parent(clk, sor->clk_parent); |
| 874 | if (err < 0) { |
| 875 | dev_err(sor->dev, "failed to set parent clock: %d\n", err); |
| 876 | return err; |
| 877 | } |
| 878 | |
| 879 | err = clk_set_rate(sor->clk_parent, pclk); |
| 880 | if (err < 0) { |
Thierry Reding | 91eded9 | 2014-03-26 13:32:21 +0100 | [diff] [blame^] | 881 | dev_err(sor->dev, "failed to set clock rate to %lu Hz\n", pclk); |
Thierry Reding | 6b6b604 | 2013-11-15 16:06:05 +0100 | [diff] [blame] | 882 | return err; |
| 883 | } |
| 884 | |
Thierry Reding | 91eded9 | 2014-03-26 13:32:21 +0100 | [diff] [blame^] | 885 | *div = 0; |
| 886 | |
Thierry Reding | 6b6b604 | 2013-11-15 16:06:05 +0100 | [diff] [blame] | 887 | return 0; |
| 888 | } |
| 889 | |
| 890 | static int tegra_output_sor_check_mode(struct tegra_output *output, |
| 891 | struct drm_display_mode *mode, |
| 892 | enum drm_mode_status *status) |
| 893 | { |
| 894 | /* |
| 895 | * FIXME: For now, always assume that the mode is okay. |
| 896 | */ |
| 897 | |
| 898 | *status = MODE_OK; |
| 899 | |
| 900 | return 0; |
| 901 | } |
| 902 | |
| 903 | static enum drm_connector_status |
| 904 | tegra_output_sor_detect(struct tegra_output *output) |
| 905 | { |
| 906 | struct tegra_sor *sor = to_sor(output); |
| 907 | |
| 908 | if (sor->dpaux) |
| 909 | return tegra_dpaux_detect(sor->dpaux); |
| 910 | |
| 911 | return connector_status_unknown; |
| 912 | } |
| 913 | |
| 914 | static const struct tegra_output_ops sor_ops = { |
| 915 | .enable = tegra_output_sor_enable, |
| 916 | .disable = tegra_output_sor_disable, |
| 917 | .setup_clock = tegra_output_sor_setup_clock, |
| 918 | .check_mode = tegra_output_sor_check_mode, |
| 919 | .detect = tegra_output_sor_detect, |
| 920 | }; |
| 921 | |
Thierry Reding | a82752e | 2014-01-31 10:02:15 +0100 | [diff] [blame] | 922 | static int tegra_sor_crc_open(struct inode *inode, struct file *file) |
| 923 | { |
| 924 | file->private_data = inode->i_private; |
| 925 | |
| 926 | return 0; |
| 927 | } |
| 928 | |
| 929 | static int tegra_sor_crc_release(struct inode *inode, struct file *file) |
| 930 | { |
| 931 | return 0; |
| 932 | } |
| 933 | |
| 934 | static int tegra_sor_crc_wait(struct tegra_sor *sor, unsigned long timeout) |
| 935 | { |
| 936 | u32 value; |
| 937 | |
| 938 | timeout = jiffies + msecs_to_jiffies(timeout); |
| 939 | |
| 940 | while (time_before(jiffies, timeout)) { |
| 941 | value = tegra_sor_readl(sor, SOR_CRC_A); |
| 942 | if (value & SOR_CRC_A_VALID) |
| 943 | return 0; |
| 944 | |
| 945 | usleep_range(100, 200); |
| 946 | } |
| 947 | |
| 948 | return -ETIMEDOUT; |
| 949 | } |
| 950 | |
| 951 | static ssize_t tegra_sor_crc_read(struct file *file, char __user *buffer, |
| 952 | size_t size, loff_t *ppos) |
| 953 | { |
| 954 | struct tegra_sor *sor = file->private_data; |
| 955 | char buf[10]; |
| 956 | ssize_t num; |
| 957 | u32 value; |
| 958 | int err; |
| 959 | |
| 960 | value = tegra_sor_readl(sor, SOR_STATE_1); |
| 961 | value &= ~SOR_STATE_ASY_CRC_MODE_MASK; |
| 962 | tegra_sor_writel(sor, value, SOR_STATE_1); |
| 963 | |
| 964 | value = tegra_sor_readl(sor, SOR_CRC_CNTRL); |
| 965 | value |= SOR_CRC_CNTRL_ENABLE; |
| 966 | tegra_sor_writel(sor, value, SOR_CRC_CNTRL); |
| 967 | |
| 968 | value = tegra_sor_readl(sor, SOR_TEST); |
| 969 | value &= ~SOR_TEST_CRC_POST_SERIALIZE; |
| 970 | tegra_sor_writel(sor, value, SOR_TEST); |
| 971 | |
| 972 | err = tegra_sor_crc_wait(sor, 100); |
| 973 | if (err < 0) |
| 974 | return err; |
| 975 | |
| 976 | tegra_sor_writel(sor, SOR_CRC_A_RESET, SOR_CRC_A); |
| 977 | value = tegra_sor_readl(sor, SOR_CRC_B); |
| 978 | |
| 979 | num = scnprintf(buf, sizeof(buf), "%08x\n", value); |
| 980 | |
| 981 | return simple_read_from_buffer(buffer, size, ppos, buf, num); |
| 982 | } |
| 983 | |
| 984 | static const struct file_operations tegra_sor_crc_fops = { |
| 985 | .owner = THIS_MODULE, |
| 986 | .open = tegra_sor_crc_open, |
| 987 | .read = tegra_sor_crc_read, |
| 988 | .release = tegra_sor_crc_release, |
| 989 | }; |
| 990 | |
| 991 | static int tegra_sor_debugfs_init(struct tegra_sor *sor, struct dentry *root) |
| 992 | { |
| 993 | struct dentry *entry; |
| 994 | int err = 0; |
| 995 | |
| 996 | sor->debugfs = debugfs_create_dir("sor", root); |
| 997 | if (!sor->debugfs) |
| 998 | return -ENOMEM; |
| 999 | |
| 1000 | entry = debugfs_create_file("crc", 0644, sor->debugfs, sor, |
| 1001 | &tegra_sor_crc_fops); |
| 1002 | if (!entry) { |
| 1003 | dev_err(sor->dev, |
| 1004 | "cannot create /sys/kernel/debug/dri/%s/sor/crc\n", |
| 1005 | root->d_name.name); |
| 1006 | err = -ENOMEM; |
| 1007 | goto remove; |
| 1008 | } |
| 1009 | |
| 1010 | return err; |
| 1011 | |
| 1012 | remove: |
| 1013 | debugfs_remove(sor->debugfs); |
| 1014 | sor->debugfs = NULL; |
| 1015 | return err; |
| 1016 | } |
| 1017 | |
| 1018 | static int tegra_sor_debugfs_exit(struct tegra_sor *sor) |
| 1019 | { |
| 1020 | debugfs_remove(sor->debugfs); |
| 1021 | sor->debugfs = NULL; |
| 1022 | |
| 1023 | return 0; |
| 1024 | } |
| 1025 | |
Thierry Reding | 6b6b604 | 2013-11-15 16:06:05 +0100 | [diff] [blame] | 1026 | static int tegra_sor_init(struct host1x_client *client) |
| 1027 | { |
| 1028 | struct tegra_drm *tegra = dev_get_drvdata(client->parent); |
| 1029 | struct tegra_sor *sor = host1x_client_to_sor(client); |
| 1030 | int err; |
| 1031 | |
| 1032 | if (!sor->dpaux) |
| 1033 | return -ENODEV; |
| 1034 | |
| 1035 | sor->output.type = TEGRA_OUTPUT_EDP; |
| 1036 | |
| 1037 | sor->output.dev = sor->dev; |
| 1038 | sor->output.ops = &sor_ops; |
| 1039 | |
| 1040 | err = tegra_output_init(tegra->drm, &sor->output); |
| 1041 | if (err < 0) { |
| 1042 | dev_err(sor->dev, "output setup failed: %d\n", err); |
| 1043 | return err; |
| 1044 | } |
| 1045 | |
Thierry Reding | a82752e | 2014-01-31 10:02:15 +0100 | [diff] [blame] | 1046 | if (IS_ENABLED(CONFIG_DEBUG_FS)) { |
| 1047 | struct dentry *root = tegra->drm->primary->debugfs_root; |
| 1048 | |
| 1049 | err = tegra_sor_debugfs_init(sor, root); |
| 1050 | if (err < 0) |
| 1051 | dev_err(sor->dev, "debugfs setup failed: %d\n", err); |
| 1052 | } |
| 1053 | |
Thierry Reding | 6b6b604 | 2013-11-15 16:06:05 +0100 | [diff] [blame] | 1054 | if (sor->dpaux) { |
| 1055 | err = tegra_dpaux_attach(sor->dpaux, &sor->output); |
| 1056 | if (err < 0) { |
| 1057 | dev_err(sor->dev, "failed to attach DP: %d\n", err); |
| 1058 | return err; |
| 1059 | } |
| 1060 | } |
| 1061 | |
| 1062 | return 0; |
| 1063 | } |
| 1064 | |
| 1065 | static int tegra_sor_exit(struct host1x_client *client) |
| 1066 | { |
| 1067 | struct tegra_sor *sor = host1x_client_to_sor(client); |
| 1068 | int err; |
| 1069 | |
| 1070 | err = tegra_output_disable(&sor->output); |
| 1071 | if (err < 0) { |
| 1072 | dev_err(sor->dev, "output failed to disable: %d\n", err); |
| 1073 | return err; |
| 1074 | } |
| 1075 | |
| 1076 | if (sor->dpaux) { |
| 1077 | err = tegra_dpaux_detach(sor->dpaux); |
| 1078 | if (err < 0) { |
| 1079 | dev_err(sor->dev, "failed to detach DP: %d\n", err); |
| 1080 | return err; |
| 1081 | } |
| 1082 | } |
| 1083 | |
Thierry Reding | a82752e | 2014-01-31 10:02:15 +0100 | [diff] [blame] | 1084 | if (IS_ENABLED(CONFIG_DEBUG_FS)) { |
| 1085 | err = tegra_sor_debugfs_exit(sor); |
| 1086 | if (err < 0) |
| 1087 | dev_err(sor->dev, "debugfs cleanup failed: %d\n", err); |
| 1088 | } |
| 1089 | |
Thierry Reding | 6b6b604 | 2013-11-15 16:06:05 +0100 | [diff] [blame] | 1090 | err = tegra_output_exit(&sor->output); |
| 1091 | if (err < 0) { |
| 1092 | dev_err(sor->dev, "output cleanup failed: %d\n", err); |
| 1093 | return err; |
| 1094 | } |
| 1095 | |
| 1096 | return 0; |
| 1097 | } |
| 1098 | |
| 1099 | static const struct host1x_client_ops sor_client_ops = { |
| 1100 | .init = tegra_sor_init, |
| 1101 | .exit = tegra_sor_exit, |
| 1102 | }; |
| 1103 | |
| 1104 | static int tegra_sor_probe(struct platform_device *pdev) |
| 1105 | { |
| 1106 | struct device_node *np; |
| 1107 | struct tegra_sor *sor; |
| 1108 | struct resource *regs; |
| 1109 | int err; |
| 1110 | |
| 1111 | sor = devm_kzalloc(&pdev->dev, sizeof(*sor), GFP_KERNEL); |
| 1112 | if (!sor) |
| 1113 | return -ENOMEM; |
| 1114 | |
| 1115 | sor->output.dev = sor->dev = &pdev->dev; |
| 1116 | |
| 1117 | np = of_parse_phandle(pdev->dev.of_node, "nvidia,dpaux", 0); |
| 1118 | if (np) { |
| 1119 | sor->dpaux = tegra_dpaux_find_by_of_node(np); |
| 1120 | of_node_put(np); |
| 1121 | |
| 1122 | if (!sor->dpaux) |
| 1123 | return -EPROBE_DEFER; |
| 1124 | } |
| 1125 | |
| 1126 | err = tegra_output_probe(&sor->output); |
| 1127 | if (err < 0) |
| 1128 | return err; |
| 1129 | |
| 1130 | regs = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1131 | sor->regs = devm_ioremap_resource(&pdev->dev, regs); |
| 1132 | if (IS_ERR(sor->regs)) |
| 1133 | return PTR_ERR(sor->regs); |
| 1134 | |
| 1135 | sor->rst = devm_reset_control_get(&pdev->dev, "sor"); |
| 1136 | if (IS_ERR(sor->rst)) |
| 1137 | return PTR_ERR(sor->rst); |
| 1138 | |
| 1139 | sor->clk = devm_clk_get(&pdev->dev, NULL); |
| 1140 | if (IS_ERR(sor->clk)) |
| 1141 | return PTR_ERR(sor->clk); |
| 1142 | |
| 1143 | sor->clk_parent = devm_clk_get(&pdev->dev, "parent"); |
| 1144 | if (IS_ERR(sor->clk_parent)) |
| 1145 | return PTR_ERR(sor->clk_parent); |
| 1146 | |
| 1147 | err = clk_prepare_enable(sor->clk_parent); |
| 1148 | if (err < 0) |
| 1149 | return err; |
| 1150 | |
| 1151 | sor->clk_safe = devm_clk_get(&pdev->dev, "safe"); |
| 1152 | if (IS_ERR(sor->clk_safe)) |
| 1153 | return PTR_ERR(sor->clk_safe); |
| 1154 | |
| 1155 | err = clk_prepare_enable(sor->clk_safe); |
| 1156 | if (err < 0) |
| 1157 | return err; |
| 1158 | |
| 1159 | sor->clk_dp = devm_clk_get(&pdev->dev, "dp"); |
| 1160 | if (IS_ERR(sor->clk_dp)) |
| 1161 | return PTR_ERR(sor->clk_dp); |
| 1162 | |
| 1163 | err = clk_prepare_enable(sor->clk_dp); |
| 1164 | if (err < 0) |
| 1165 | return err; |
| 1166 | |
| 1167 | INIT_LIST_HEAD(&sor->client.list); |
| 1168 | sor->client.ops = &sor_client_ops; |
| 1169 | sor->client.dev = &pdev->dev; |
| 1170 | |
| 1171 | err = host1x_client_register(&sor->client); |
| 1172 | if (err < 0) { |
| 1173 | dev_err(&pdev->dev, "failed to register host1x client: %d\n", |
| 1174 | err); |
| 1175 | return err; |
| 1176 | } |
| 1177 | |
| 1178 | platform_set_drvdata(pdev, sor); |
| 1179 | |
| 1180 | return 0; |
| 1181 | } |
| 1182 | |
| 1183 | static int tegra_sor_remove(struct platform_device *pdev) |
| 1184 | { |
| 1185 | struct tegra_sor *sor = platform_get_drvdata(pdev); |
| 1186 | int err; |
| 1187 | |
| 1188 | err = host1x_client_unregister(&sor->client); |
| 1189 | if (err < 0) { |
| 1190 | dev_err(&pdev->dev, "failed to unregister host1x client: %d\n", |
| 1191 | err); |
| 1192 | return err; |
| 1193 | } |
| 1194 | |
| 1195 | clk_disable_unprepare(sor->clk_parent); |
| 1196 | clk_disable_unprepare(sor->clk_safe); |
| 1197 | clk_disable_unprepare(sor->clk_dp); |
| 1198 | clk_disable_unprepare(sor->clk); |
| 1199 | |
| 1200 | return 0; |
| 1201 | } |
| 1202 | |
| 1203 | static const struct of_device_id tegra_sor_of_match[] = { |
| 1204 | { .compatible = "nvidia,tegra124-sor", }, |
| 1205 | { }, |
| 1206 | }; |
| 1207 | |
| 1208 | struct platform_driver tegra_sor_driver = { |
| 1209 | .driver = { |
| 1210 | .name = "tegra-sor", |
| 1211 | .of_match_table = tegra_sor_of_match, |
| 1212 | }, |
| 1213 | .probe = tegra_sor_probe, |
| 1214 | .remove = tegra_sor_remove, |
| 1215 | }; |