blob: 1414e20a0b716ebc790e4594222f9f845fe26f1d [file] [log] [blame]
Thierry Reding6b6b6042013-11-15 16:06:05 +01001/*
2 * Copyright (C) 2013 NVIDIA Corporation
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8
9#include <linux/clk.h>
Thierry Redinga82752e2014-01-31 10:02:15 +010010#include <linux/debugfs.h>
Thierry Reding6fad8f62014-11-28 15:41:34 +010011#include <linux/gpio.h>
Thierry Reding6b6b6042013-11-15 16:06:05 +010012#include <linux/io.h>
Thierry Reding459cc2c2015-07-30 10:34:24 +020013#include <linux/of_device.h>
Thierry Reding6b6b6042013-11-15 16:06:05 +010014#include <linux/platform_device.h>
Thierry Reding459cc2c2015-07-30 10:34:24 +020015#include <linux/regulator/consumer.h>
Thierry Reding6b6b6042013-11-15 16:06:05 +010016#include <linux/reset.h>
Thierry Reding306a7f92014-07-17 13:17:24 +020017
Thierry Reding72323982014-07-11 13:19:06 +020018#include <soc/tegra/pmc.h>
Thierry Reding6b6b6042013-11-15 16:06:05 +010019
Thierry Reding4aa3df72014-11-24 16:27:13 +010020#include <drm/drm_atomic_helper.h>
Thierry Reding6b6b6042013-11-15 16:06:05 +010021#include <drm/drm_dp_helper.h>
Thierry Reding6fad8f62014-11-28 15:41:34 +010022#include <drm/drm_panel.h>
Thierry Reding6b6b6042013-11-15 16:06:05 +010023
24#include "dc.h"
25#include "drm.h"
26#include "sor.h"
27
Thierry Reding459cc2c2015-07-30 10:34:24 +020028#define SOR_REKEY 0x38
29
30struct tegra_sor_hdmi_settings {
31 unsigned long frequency;
32
33 u8 vcocap;
34 u8 ichpmp;
35 u8 loadadj;
36 u8 termadj;
37 u8 tx_pu;
38 u8 bg_vref;
39
40 u8 drive_current[4];
41 u8 preemphasis[4];
42};
43
44#if 1
45static const struct tegra_sor_hdmi_settings tegra210_sor_hdmi_defaults[] = {
46 {
47 .frequency = 54000000,
48 .vcocap = 0x0,
49 .ichpmp = 0x1,
50 .loadadj = 0x3,
51 .termadj = 0x9,
52 .tx_pu = 0x10,
53 .bg_vref = 0x8,
54 .drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
55 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
56 }, {
57 .frequency = 75000000,
58 .vcocap = 0x3,
59 .ichpmp = 0x1,
60 .loadadj = 0x3,
61 .termadj = 0x9,
62 .tx_pu = 0x40,
63 .bg_vref = 0x8,
64 .drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
65 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
66 }, {
67 .frequency = 150000000,
68 .vcocap = 0x3,
69 .ichpmp = 0x1,
70 .loadadj = 0x3,
71 .termadj = 0x9,
72 .tx_pu = 0x66,
73 .bg_vref = 0x8,
74 .drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
75 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
76 }, {
77 .frequency = 300000000,
78 .vcocap = 0x3,
79 .ichpmp = 0x1,
80 .loadadj = 0x3,
81 .termadj = 0x9,
82 .tx_pu = 0x66,
83 .bg_vref = 0xa,
84 .drive_current = { 0x33, 0x3f, 0x3f, 0x3f },
85 .preemphasis = { 0x00, 0x17, 0x17, 0x17 },
86 }, {
87 .frequency = 600000000,
88 .vcocap = 0x3,
89 .ichpmp = 0x1,
90 .loadadj = 0x3,
91 .termadj = 0x9,
92 .tx_pu = 0x66,
93 .bg_vref = 0x8,
94 .drive_current = { 0x33, 0x3f, 0x3f, 0x3f },
95 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
96 },
97};
98#else
99static const struct tegra_sor_hdmi_settings tegra210_sor_hdmi_defaults[] = {
100 {
101 .frequency = 75000000,
102 .vcocap = 0x3,
103 .ichpmp = 0x1,
104 .loadadj = 0x3,
105 .termadj = 0x9,
106 .tx_pu = 0x40,
107 .bg_vref = 0x8,
108 .drive_current = { 0x29, 0x29, 0x29, 0x29 },
109 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
110 }, {
111 .frequency = 150000000,
112 .vcocap = 0x3,
113 .ichpmp = 0x1,
114 .loadadj = 0x3,
115 .termadj = 0x9,
116 .tx_pu = 0x66,
117 .bg_vref = 0x8,
118 .drive_current = { 0x30, 0x37, 0x37, 0x37 },
119 .preemphasis = { 0x01, 0x02, 0x02, 0x02 },
120 }, {
121 .frequency = 300000000,
122 .vcocap = 0x3,
123 .ichpmp = 0x6,
124 .loadadj = 0x3,
125 .termadj = 0x9,
126 .tx_pu = 0x66,
127 .bg_vref = 0xf,
128 .drive_current = { 0x30, 0x37, 0x37, 0x37 },
129 .preemphasis = { 0x10, 0x3e, 0x3e, 0x3e },
130 }, {
131 .frequency = 600000000,
132 .vcocap = 0x3,
133 .ichpmp = 0xa,
134 .loadadj = 0x3,
135 .termadj = 0xb,
136 .tx_pu = 0x66,
137 .bg_vref = 0xe,
138 .drive_current = { 0x35, 0x3e, 0x3e, 0x3e },
139 .preemphasis = { 0x02, 0x3f, 0x3f, 0x3f },
140 },
141};
142#endif
143
144struct tegra_sor_soc {
145 bool supports_edp;
146 bool supports_lvds;
147 bool supports_hdmi;
148 bool supports_dp;
149
150 const struct tegra_sor_hdmi_settings *settings;
151 unsigned int num_settings;
152};
153
154struct tegra_sor;
155
156struct tegra_sor_ops {
157 const char *name;
158 int (*probe)(struct tegra_sor *sor);
159 int (*remove)(struct tegra_sor *sor);
160};
161
Thierry Reding6b6b6042013-11-15 16:06:05 +0100162struct tegra_sor {
163 struct host1x_client client;
164 struct tegra_output output;
165 struct device *dev;
166
Thierry Reding459cc2c2015-07-30 10:34:24 +0200167 const struct tegra_sor_soc *soc;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100168 void __iomem *regs;
169
170 struct reset_control *rst;
171 struct clk *clk_parent;
172 struct clk *clk_safe;
173 struct clk *clk_dp;
174 struct clk *clk;
175
Thierry Reding9542c232015-07-08 13:39:09 +0200176 struct drm_dp_aux *aux;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100177
Thierry Redingdab16332015-01-26 16:04:08 +0100178 struct drm_info_list *debugfs_files;
179 struct drm_minor *minor;
Thierry Redinga82752e2014-01-31 10:02:15 +0100180 struct dentry *debugfs;
Thierry Reding459cc2c2015-07-30 10:34:24 +0200181
182 const struct tegra_sor_ops *ops;
183
184 /* for HDMI 2.0 */
185 struct tegra_sor_hdmi_settings *settings;
186 unsigned int num_settings;
187
188 struct regulator *avdd_io_supply;
189 struct regulator *vdd_pll_supply;
190 struct regulator *hdmi_supply;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100191};
192
Thierry Reding34fa1832014-06-05 16:31:10 +0200193struct tegra_sor_config {
194 u32 bits_per_pixel;
195
196 u32 active_polarity;
197 u32 active_count;
198 u32 tu_size;
199 u32 active_frac;
200 u32 watermark;
Thierry Reding7890b572014-06-05 16:12:46 +0200201
202 u32 hblank_symbols;
203 u32 vblank_symbols;
Thierry Reding34fa1832014-06-05 16:31:10 +0200204};
205
Thierry Reding6b6b6042013-11-15 16:06:05 +0100206static inline struct tegra_sor *
207host1x_client_to_sor(struct host1x_client *client)
208{
209 return container_of(client, struct tegra_sor, client);
210}
211
212static inline struct tegra_sor *to_sor(struct tegra_output *output)
213{
214 return container_of(output, struct tegra_sor, output);
215}
216
Thierry Reding28fe2072015-01-26 16:02:48 +0100217static inline u32 tegra_sor_readl(struct tegra_sor *sor, unsigned long offset)
Thierry Reding6b6b6042013-11-15 16:06:05 +0100218{
219 return readl(sor->regs + (offset << 2));
220}
221
Thierry Reding28fe2072015-01-26 16:02:48 +0100222static inline void tegra_sor_writel(struct tegra_sor *sor, u32 value,
Thierry Reding6b6b6042013-11-15 16:06:05 +0100223 unsigned long offset)
224{
225 writel(value, sor->regs + (offset << 2));
226}
227
228static int tegra_sor_dp_train_fast(struct tegra_sor *sor,
229 struct drm_dp_link *link)
230{
Thierry Reding6b6b6042013-11-15 16:06:05 +0100231 unsigned int i;
232 u8 pattern;
Thierry Reding28fe2072015-01-26 16:02:48 +0100233 u32 value;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100234 int err;
235
236 /* setup lane parameters */
237 value = SOR_LANE_DRIVE_CURRENT_LANE3(0x40) |
238 SOR_LANE_DRIVE_CURRENT_LANE2(0x40) |
239 SOR_LANE_DRIVE_CURRENT_LANE1(0x40) |
240 SOR_LANE_DRIVE_CURRENT_LANE0(0x40);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200241 tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100242
243 value = SOR_LANE_PREEMPHASIS_LANE3(0x0f) |
244 SOR_LANE_PREEMPHASIS_LANE2(0x0f) |
245 SOR_LANE_PREEMPHASIS_LANE1(0x0f) |
246 SOR_LANE_PREEMPHASIS_LANE0(0x0f);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200247 tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100248
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200249 value = SOR_LANE_POSTCURSOR_LANE3(0x00) |
250 SOR_LANE_POSTCURSOR_LANE2(0x00) |
251 SOR_LANE_POSTCURSOR_LANE1(0x00) |
252 SOR_LANE_POSTCURSOR_LANE0(0x00);
253 tegra_sor_writel(sor, value, SOR_LANE_POSTCURSOR0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100254
255 /* disable LVDS mode */
256 tegra_sor_writel(sor, 0, SOR_LVDS);
257
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200258 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100259 value |= SOR_DP_PADCTL_TX_PU_ENABLE;
260 value &= ~SOR_DP_PADCTL_TX_PU_MASK;
261 value |= SOR_DP_PADCTL_TX_PU(2); /* XXX: don't hardcode? */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200262 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100263
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200264 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100265 value |= SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
266 SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200267 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100268
269 usleep_range(10, 100);
270
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200271 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100272 value &= ~(SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
273 SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200274 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100275
Thierry Reding9542c232015-07-08 13:39:09 +0200276 err = drm_dp_aux_prepare(sor->aux, DP_SET_ANSI_8B10B);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100277 if (err < 0)
278 return err;
279
280 for (i = 0, value = 0; i < link->num_lanes; i++) {
281 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
282 SOR_DP_TPG_SCRAMBLER_NONE |
283 SOR_DP_TPG_PATTERN_TRAIN1;
284 value = (value << 8) | lane;
285 }
286
287 tegra_sor_writel(sor, value, SOR_DP_TPG);
288
289 pattern = DP_TRAINING_PATTERN_1;
290
Thierry Reding9542c232015-07-08 13:39:09 +0200291 err = drm_dp_aux_train(sor->aux, link, pattern);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100292 if (err < 0)
293 return err;
294
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200295 value = tegra_sor_readl(sor, SOR_DP_SPARE0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100296 value |= SOR_DP_SPARE_SEQ_ENABLE;
297 value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
298 value |= SOR_DP_SPARE_MACRO_SOR_CLK;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200299 tegra_sor_writel(sor, value, SOR_DP_SPARE0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100300
301 for (i = 0, value = 0; i < link->num_lanes; i++) {
302 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
303 SOR_DP_TPG_SCRAMBLER_NONE |
304 SOR_DP_TPG_PATTERN_TRAIN2;
305 value = (value << 8) | lane;
306 }
307
308 tegra_sor_writel(sor, value, SOR_DP_TPG);
309
310 pattern = DP_LINK_SCRAMBLING_DISABLE | DP_TRAINING_PATTERN_2;
311
Thierry Reding9542c232015-07-08 13:39:09 +0200312 err = drm_dp_aux_train(sor->aux, link, pattern);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100313 if (err < 0)
314 return err;
315
316 for (i = 0, value = 0; i < link->num_lanes; i++) {
317 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
318 SOR_DP_TPG_SCRAMBLER_GALIOS |
319 SOR_DP_TPG_PATTERN_NONE;
320 value = (value << 8) | lane;
321 }
322
323 tegra_sor_writel(sor, value, SOR_DP_TPG);
324
325 pattern = DP_TRAINING_PATTERN_DISABLE;
326
Thierry Reding9542c232015-07-08 13:39:09 +0200327 err = drm_dp_aux_train(sor->aux, link, pattern);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100328 if (err < 0)
329 return err;
330
331 return 0;
332}
333
Thierry Reding459cc2c2015-07-30 10:34:24 +0200334static void tegra_sor_dp_term_calibrate(struct tegra_sor *sor)
335{
336 u32 mask = 0x08, adj = 0, value;
337
338 /* enable pad calibration logic */
339 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
340 value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
341 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
342
343 value = tegra_sor_readl(sor, SOR_PLL1);
344 value |= SOR_PLL1_TMDS_TERM;
345 tegra_sor_writel(sor, value, SOR_PLL1);
346
347 while (mask) {
348 adj |= mask;
349
350 value = tegra_sor_readl(sor, SOR_PLL1);
351 value &= ~SOR_PLL1_TMDS_TERMADJ_MASK;
352 value |= SOR_PLL1_TMDS_TERMADJ(adj);
353 tegra_sor_writel(sor, value, SOR_PLL1);
354
355 usleep_range(100, 200);
356
357 value = tegra_sor_readl(sor, SOR_PLL1);
358 if (value & SOR_PLL1_TERM_COMPOUT)
359 adj &= ~mask;
360
361 mask >>= 1;
362 }
363
364 value = tegra_sor_readl(sor, SOR_PLL1);
365 value &= ~SOR_PLL1_TMDS_TERMADJ_MASK;
366 value |= SOR_PLL1_TMDS_TERMADJ(adj);
367 tegra_sor_writel(sor, value, SOR_PLL1);
368
369 /* disable pad calibration logic */
370 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
371 value |= SOR_DP_PADCTL_PAD_CAL_PD;
372 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
373}
374
Thierry Reding6b6b6042013-11-15 16:06:05 +0100375static void tegra_sor_super_update(struct tegra_sor *sor)
376{
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200377 tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
378 tegra_sor_writel(sor, 1, SOR_SUPER_STATE0);
379 tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100380}
381
382static void tegra_sor_update(struct tegra_sor *sor)
383{
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200384 tegra_sor_writel(sor, 0, SOR_STATE0);
385 tegra_sor_writel(sor, 1, SOR_STATE0);
386 tegra_sor_writel(sor, 0, SOR_STATE0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100387}
388
389static int tegra_sor_setup_pwm(struct tegra_sor *sor, unsigned long timeout)
390{
Thierry Reding28fe2072015-01-26 16:02:48 +0100391 u32 value;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100392
393 value = tegra_sor_readl(sor, SOR_PWM_DIV);
394 value &= ~SOR_PWM_DIV_MASK;
395 value |= 0x400; /* period */
396 tegra_sor_writel(sor, value, SOR_PWM_DIV);
397
398 value = tegra_sor_readl(sor, SOR_PWM_CTL);
399 value &= ~SOR_PWM_CTL_DUTY_CYCLE_MASK;
400 value |= 0x400; /* duty cycle */
401 value &= ~SOR_PWM_CTL_CLK_SEL; /* clock source: PCLK */
402 value |= SOR_PWM_CTL_TRIGGER;
403 tegra_sor_writel(sor, value, SOR_PWM_CTL);
404
405 timeout = jiffies + msecs_to_jiffies(timeout);
406
407 while (time_before(jiffies, timeout)) {
408 value = tegra_sor_readl(sor, SOR_PWM_CTL);
409 if ((value & SOR_PWM_CTL_TRIGGER) == 0)
410 return 0;
411
412 usleep_range(25, 100);
413 }
414
415 return -ETIMEDOUT;
416}
417
418static int tegra_sor_attach(struct tegra_sor *sor)
419{
420 unsigned long value, timeout;
421
422 /* wake up in normal mode */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200423 value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100424 value |= SOR_SUPER_STATE_HEAD_MODE_AWAKE;
425 value |= SOR_SUPER_STATE_MODE_NORMAL;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200426 tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100427 tegra_sor_super_update(sor);
428
429 /* attach */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200430 value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100431 value |= SOR_SUPER_STATE_ATTACHED;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200432 tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100433 tegra_sor_super_update(sor);
434
435 timeout = jiffies + msecs_to_jiffies(250);
436
437 while (time_before(jiffies, timeout)) {
438 value = tegra_sor_readl(sor, SOR_TEST);
439 if ((value & SOR_TEST_ATTACHED) != 0)
440 return 0;
441
442 usleep_range(25, 100);
443 }
444
445 return -ETIMEDOUT;
446}
447
448static int tegra_sor_wakeup(struct tegra_sor *sor)
449{
Thierry Reding6b6b6042013-11-15 16:06:05 +0100450 unsigned long value, timeout;
451
Thierry Reding6b6b6042013-11-15 16:06:05 +0100452 timeout = jiffies + msecs_to_jiffies(250);
453
454 /* wait for head to wake up */
455 while (time_before(jiffies, timeout)) {
456 value = tegra_sor_readl(sor, SOR_TEST);
457 value &= SOR_TEST_HEAD_MODE_MASK;
458
459 if (value == SOR_TEST_HEAD_MODE_AWAKE)
460 return 0;
461
462 usleep_range(25, 100);
463 }
464
465 return -ETIMEDOUT;
466}
467
468static int tegra_sor_power_up(struct tegra_sor *sor, unsigned long timeout)
469{
Thierry Reding28fe2072015-01-26 16:02:48 +0100470 u32 value;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100471
472 value = tegra_sor_readl(sor, SOR_PWR);
473 value |= SOR_PWR_TRIGGER | SOR_PWR_NORMAL_STATE_PU;
474 tegra_sor_writel(sor, value, SOR_PWR);
475
476 timeout = jiffies + msecs_to_jiffies(timeout);
477
478 while (time_before(jiffies, timeout)) {
479 value = tegra_sor_readl(sor, SOR_PWR);
480 if ((value & SOR_PWR_TRIGGER) == 0)
481 return 0;
482
483 usleep_range(25, 100);
484 }
485
486 return -ETIMEDOUT;
487}
488
Thierry Reding34fa1832014-06-05 16:31:10 +0200489struct tegra_sor_params {
490 /* number of link clocks per line */
491 unsigned int num_clocks;
492 /* ratio between input and output */
493 u64 ratio;
494 /* precision factor */
495 u64 precision;
496
497 unsigned int active_polarity;
498 unsigned int active_count;
499 unsigned int active_frac;
500 unsigned int tu_size;
501 unsigned int error;
502};
503
504static int tegra_sor_compute_params(struct tegra_sor *sor,
505 struct tegra_sor_params *params,
506 unsigned int tu_size)
507{
508 u64 active_sym, active_count, frac, approx;
509 u32 active_polarity, active_frac = 0;
510 const u64 f = params->precision;
511 s64 error;
512
513 active_sym = params->ratio * tu_size;
514 active_count = div_u64(active_sym, f) * f;
515 frac = active_sym - active_count;
516
517 /* fraction < 0.5 */
518 if (frac >= (f / 2)) {
519 active_polarity = 1;
520 frac = f - frac;
521 } else {
522 active_polarity = 0;
523 }
524
525 if (frac != 0) {
526 frac = div_u64(f * f, frac); /* 1/fraction */
527 if (frac <= (15 * f)) {
528 active_frac = div_u64(frac, f);
529
530 /* round up */
531 if (active_polarity)
532 active_frac++;
533 } else {
534 active_frac = active_polarity ? 1 : 15;
535 }
536 }
537
538 if (active_frac == 1)
539 active_polarity = 0;
540
541 if (active_polarity == 1) {
542 if (active_frac) {
543 approx = active_count + (active_frac * (f - 1)) * f;
544 approx = div_u64(approx, active_frac * f);
545 } else {
546 approx = active_count + f;
547 }
548 } else {
549 if (active_frac)
550 approx = active_count + div_u64(f, active_frac);
551 else
552 approx = active_count;
553 }
554
555 error = div_s64(active_sym - approx, tu_size);
556 error *= params->num_clocks;
557
Andrew Morton79211c82015-11-09 14:58:13 -0800558 if (error <= 0 && abs(error) < params->error) {
Thierry Reding34fa1832014-06-05 16:31:10 +0200559 params->active_count = div_u64(active_count, f);
560 params->active_polarity = active_polarity;
561 params->active_frac = active_frac;
Andrew Morton79211c82015-11-09 14:58:13 -0800562 params->error = abs(error);
Thierry Reding34fa1832014-06-05 16:31:10 +0200563 params->tu_size = tu_size;
564
565 if (error == 0)
566 return true;
567 }
568
569 return false;
570}
571
572static int tegra_sor_calc_config(struct tegra_sor *sor,
Thierry Reding80444492015-07-29 18:20:01 +0200573 const struct drm_display_mode *mode,
Thierry Reding34fa1832014-06-05 16:31:10 +0200574 struct tegra_sor_config *config,
575 struct drm_dp_link *link)
576{
577 const u64 f = 100000, link_rate = link->rate * 1000;
578 const u64 pclk = mode->clock * 1000;
Thierry Reding7890b572014-06-05 16:12:46 +0200579 u64 input, output, watermark, num;
Thierry Reding34fa1832014-06-05 16:31:10 +0200580 struct tegra_sor_params params;
Thierry Reding34fa1832014-06-05 16:31:10 +0200581 u32 num_syms_per_line;
582 unsigned int i;
583
584 if (!link_rate || !link->num_lanes || !pclk || !config->bits_per_pixel)
585 return -EINVAL;
586
587 output = link_rate * 8 * link->num_lanes;
588 input = pclk * config->bits_per_pixel;
589
590 if (input >= output)
591 return -ERANGE;
592
593 memset(&params, 0, sizeof(params));
594 params.ratio = div64_u64(input * f, output);
595 params.num_clocks = div_u64(link_rate * mode->hdisplay, pclk);
596 params.precision = f;
597 params.error = 64 * f;
598 params.tu_size = 64;
599
600 for (i = params.tu_size; i >= 32; i--)
601 if (tegra_sor_compute_params(sor, &params, i))
602 break;
603
604 if (params.active_frac == 0) {
605 config->active_polarity = 0;
606 config->active_count = params.active_count;
607
608 if (!params.active_polarity)
609 config->active_count--;
610
611 config->tu_size = params.tu_size;
612 config->active_frac = 1;
613 } else {
614 config->active_polarity = params.active_polarity;
615 config->active_count = params.active_count;
616 config->active_frac = params.active_frac;
617 config->tu_size = params.tu_size;
618 }
619
620 dev_dbg(sor->dev,
621 "polarity: %d active count: %d tu size: %d active frac: %d\n",
622 config->active_polarity, config->active_count,
623 config->tu_size, config->active_frac);
624
625 watermark = params.ratio * config->tu_size * (f - params.ratio);
626 watermark = div_u64(watermark, f);
627
628 watermark = div_u64(watermark + params.error, f);
629 config->watermark = watermark + (config->bits_per_pixel / 8) + 2;
630 num_syms_per_line = (mode->hdisplay * config->bits_per_pixel) *
631 (link->num_lanes * 8);
632
633 if (config->watermark > 30) {
634 config->watermark = 30;
635 dev_err(sor->dev,
636 "unable to compute TU size, forcing watermark to %u\n",
637 config->watermark);
638 } else if (config->watermark > num_syms_per_line) {
639 config->watermark = num_syms_per_line;
640 dev_err(sor->dev, "watermark too high, forcing to %u\n",
641 config->watermark);
642 }
643
Thierry Reding7890b572014-06-05 16:12:46 +0200644 /* compute the number of symbols per horizontal blanking interval */
645 num = ((mode->htotal - mode->hdisplay) - 7) * link_rate;
646 config->hblank_symbols = div_u64(num, pclk);
647
648 if (link->capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
649 config->hblank_symbols -= 3;
650
651 config->hblank_symbols -= 12 / link->num_lanes;
652
653 /* compute the number of symbols per vertical blanking interval */
654 num = (mode->hdisplay - 25) * link_rate;
655 config->vblank_symbols = div_u64(num, pclk);
656 config->vblank_symbols -= 36 / link->num_lanes + 4;
657
658 dev_dbg(sor->dev, "blank symbols: H:%u V:%u\n", config->hblank_symbols,
659 config->vblank_symbols);
660
Thierry Reding34fa1832014-06-05 16:31:10 +0200661 return 0;
662}
663
Thierry Reding6fad8f62014-11-28 15:41:34 +0100664static int tegra_sor_detach(struct tegra_sor *sor)
Thierry Reding6b6b6042013-11-15 16:06:05 +0100665{
Thierry Reding6fad8f62014-11-28 15:41:34 +0100666 unsigned long value, timeout;
667
668 /* switch to safe mode */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200669 value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100670 value &= ~SOR_SUPER_STATE_MODE_NORMAL;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200671 tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100672 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 */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200686 value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100687 value &= ~SOR_SUPER_STATE_HEAD_MODE_MASK;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200688 tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100689 tegra_sor_super_update(sor);
690
691 /* detach */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200692 value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100693 value &= ~SOR_SUPER_STATE_ATTACHED;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200694 tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100695 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
713static 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
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200740 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100741 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);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200743 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100744
745 /* stop lane sequencer */
746 value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_UP |
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
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200763 value = tegra_sor_readl(sor, SOR_PLL2);
764 value |= SOR_PLL2_PORT_POWERDOWN;
765 tegra_sor_writel(sor, value, SOR_PLL2);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100766
767 usleep_range(20, 100);
768
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200769 value = tegra_sor_readl(sor, SOR_PLL0);
770 value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
771 tegra_sor_writel(sor, value, SOR_PLL0);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100772
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200773 value = tegra_sor_readl(sor, SOR_PLL2);
774 value |= SOR_PLL2_SEQ_PLLCAPPD;
775 value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
776 tegra_sor_writel(sor, value, SOR_PLL2);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100777
778 usleep_range(20, 100);
779
780 return 0;
781}
782
Thierry Reding6fad8f62014-11-28 15:41:34 +0100783static int tegra_sor_crc_wait(struct tegra_sor *sor, unsigned long timeout)
784{
785 u32 value;
786
787 timeout = jiffies + msecs_to_jiffies(timeout);
788
789 while (time_before(jiffies, timeout)) {
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200790 value = tegra_sor_readl(sor, SOR_CRCA);
791 if (value & SOR_CRCA_VALID)
Thierry Reding6fad8f62014-11-28 15:41:34 +0100792 return 0;
793
794 usleep_range(100, 200);
795 }
796
797 return -ETIMEDOUT;
798}
799
Thierry Reding530239a2015-08-06 11:04:54 +0200800static int tegra_sor_show_crc(struct seq_file *s, void *data)
Thierry Reding6fad8f62014-11-28 15:41:34 +0100801{
Thierry Reding530239a2015-08-06 11:04:54 +0200802 struct drm_info_node *node = s->private;
803 struct tegra_sor *sor = node->info_ent->data;
Thierry Reding850bab42015-07-29 17:58:41 +0200804 struct drm_crtc *crtc = sor->output.encoder.crtc;
805 struct drm_device *drm = node->minor->dev;
Thierry Reding530239a2015-08-06 11:04:54 +0200806 int err = 0;
Thierry Reding6fad8f62014-11-28 15:41:34 +0100807 u32 value;
808
Thierry Reding850bab42015-07-29 17:58:41 +0200809 drm_modeset_lock_all(drm);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100810
Thierry Reding850bab42015-07-29 17:58:41 +0200811 if (!crtc || !crtc->state->active) {
812 err = -EBUSY;
Thierry Reding6fad8f62014-11-28 15:41:34 +0100813 goto unlock;
814 }
815
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200816 value = tegra_sor_readl(sor, SOR_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100817 value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200818 tegra_sor_writel(sor, value, SOR_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100819
820 value = tegra_sor_readl(sor, SOR_CRC_CNTRL);
821 value |= SOR_CRC_CNTRL_ENABLE;
822 tegra_sor_writel(sor, value, SOR_CRC_CNTRL);
823
824 value = tegra_sor_readl(sor, SOR_TEST);
825 value &= ~SOR_TEST_CRC_POST_SERIALIZE;
826 tegra_sor_writel(sor, value, SOR_TEST);
827
828 err = tegra_sor_crc_wait(sor, 100);
829 if (err < 0)
830 goto unlock;
831
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200832 tegra_sor_writel(sor, SOR_CRCA_RESET, SOR_CRCA);
833 value = tegra_sor_readl(sor, SOR_CRCB);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100834
Thierry Reding530239a2015-08-06 11:04:54 +0200835 seq_printf(s, "%08x\n", value);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100836
837unlock:
Thierry Reding850bab42015-07-29 17:58:41 +0200838 drm_modeset_unlock_all(drm);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100839 return err;
840}
841
Thierry Redingdab16332015-01-26 16:04:08 +0100842static int tegra_sor_show_regs(struct seq_file *s, void *data)
843{
844 struct drm_info_node *node = s->private;
845 struct tegra_sor *sor = node->info_ent->data;
Thierry Reding850bab42015-07-29 17:58:41 +0200846 struct drm_crtc *crtc = sor->output.encoder.crtc;
847 struct drm_device *drm = node->minor->dev;
848 int err = 0;
849
850 drm_modeset_lock_all(drm);
851
852 if (!crtc || !crtc->state->active) {
853 err = -EBUSY;
854 goto unlock;
855 }
Thierry Redingdab16332015-01-26 16:04:08 +0100856
857#define DUMP_REG(name) \
858 seq_printf(s, "%-38s %#05x %08x\n", #name, name, \
859 tegra_sor_readl(sor, name))
860
861 DUMP_REG(SOR_CTXSW);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200862 DUMP_REG(SOR_SUPER_STATE0);
863 DUMP_REG(SOR_SUPER_STATE1);
864 DUMP_REG(SOR_STATE0);
865 DUMP_REG(SOR_STATE1);
866 DUMP_REG(SOR_HEAD_STATE0(0));
867 DUMP_REG(SOR_HEAD_STATE0(1));
868 DUMP_REG(SOR_HEAD_STATE1(0));
869 DUMP_REG(SOR_HEAD_STATE1(1));
870 DUMP_REG(SOR_HEAD_STATE2(0));
871 DUMP_REG(SOR_HEAD_STATE2(1));
872 DUMP_REG(SOR_HEAD_STATE3(0));
873 DUMP_REG(SOR_HEAD_STATE3(1));
874 DUMP_REG(SOR_HEAD_STATE4(0));
875 DUMP_REG(SOR_HEAD_STATE4(1));
876 DUMP_REG(SOR_HEAD_STATE5(0));
877 DUMP_REG(SOR_HEAD_STATE5(1));
Thierry Redingdab16332015-01-26 16:04:08 +0100878 DUMP_REG(SOR_CRC_CNTRL);
879 DUMP_REG(SOR_DP_DEBUG_MVID);
880 DUMP_REG(SOR_CLK_CNTRL);
881 DUMP_REG(SOR_CAP);
882 DUMP_REG(SOR_PWR);
883 DUMP_REG(SOR_TEST);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200884 DUMP_REG(SOR_PLL0);
885 DUMP_REG(SOR_PLL1);
886 DUMP_REG(SOR_PLL2);
887 DUMP_REG(SOR_PLL3);
Thierry Redingdab16332015-01-26 16:04:08 +0100888 DUMP_REG(SOR_CSTM);
889 DUMP_REG(SOR_LVDS);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200890 DUMP_REG(SOR_CRCA);
891 DUMP_REG(SOR_CRCB);
Thierry Redingdab16332015-01-26 16:04:08 +0100892 DUMP_REG(SOR_BLANK);
893 DUMP_REG(SOR_SEQ_CTL);
894 DUMP_REG(SOR_LANE_SEQ_CTL);
895 DUMP_REG(SOR_SEQ_INST(0));
896 DUMP_REG(SOR_SEQ_INST(1));
897 DUMP_REG(SOR_SEQ_INST(2));
898 DUMP_REG(SOR_SEQ_INST(3));
899 DUMP_REG(SOR_SEQ_INST(4));
900 DUMP_REG(SOR_SEQ_INST(5));
901 DUMP_REG(SOR_SEQ_INST(6));
902 DUMP_REG(SOR_SEQ_INST(7));
903 DUMP_REG(SOR_SEQ_INST(8));
904 DUMP_REG(SOR_SEQ_INST(9));
905 DUMP_REG(SOR_SEQ_INST(10));
906 DUMP_REG(SOR_SEQ_INST(11));
907 DUMP_REG(SOR_SEQ_INST(12));
908 DUMP_REG(SOR_SEQ_INST(13));
909 DUMP_REG(SOR_SEQ_INST(14));
910 DUMP_REG(SOR_SEQ_INST(15));
911 DUMP_REG(SOR_PWM_DIV);
912 DUMP_REG(SOR_PWM_CTL);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200913 DUMP_REG(SOR_VCRC_A0);
914 DUMP_REG(SOR_VCRC_A1);
915 DUMP_REG(SOR_VCRC_B0);
916 DUMP_REG(SOR_VCRC_B1);
917 DUMP_REG(SOR_CCRC_A0);
918 DUMP_REG(SOR_CCRC_A1);
919 DUMP_REG(SOR_CCRC_B0);
920 DUMP_REG(SOR_CCRC_B1);
921 DUMP_REG(SOR_EDATA_A0);
922 DUMP_REG(SOR_EDATA_A1);
923 DUMP_REG(SOR_EDATA_B0);
924 DUMP_REG(SOR_EDATA_B1);
925 DUMP_REG(SOR_COUNT_A0);
926 DUMP_REG(SOR_COUNT_A1);
927 DUMP_REG(SOR_COUNT_B0);
928 DUMP_REG(SOR_COUNT_B1);
929 DUMP_REG(SOR_DEBUG_A0);
930 DUMP_REG(SOR_DEBUG_A1);
931 DUMP_REG(SOR_DEBUG_B0);
932 DUMP_REG(SOR_DEBUG_B1);
Thierry Redingdab16332015-01-26 16:04:08 +0100933 DUMP_REG(SOR_TRIG);
934 DUMP_REG(SOR_MSCHECK);
935 DUMP_REG(SOR_XBAR_CTRL);
936 DUMP_REG(SOR_XBAR_POL);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200937 DUMP_REG(SOR_DP_LINKCTL0);
938 DUMP_REG(SOR_DP_LINKCTL1);
939 DUMP_REG(SOR_LANE_DRIVE_CURRENT0);
940 DUMP_REG(SOR_LANE_DRIVE_CURRENT1);
941 DUMP_REG(SOR_LANE4_DRIVE_CURRENT0);
942 DUMP_REG(SOR_LANE4_DRIVE_CURRENT1);
943 DUMP_REG(SOR_LANE_PREEMPHASIS0);
944 DUMP_REG(SOR_LANE_PREEMPHASIS1);
945 DUMP_REG(SOR_LANE4_PREEMPHASIS0);
946 DUMP_REG(SOR_LANE4_PREEMPHASIS1);
947 DUMP_REG(SOR_LANE_POSTCURSOR0);
948 DUMP_REG(SOR_LANE_POSTCURSOR1);
949 DUMP_REG(SOR_DP_CONFIG0);
950 DUMP_REG(SOR_DP_CONFIG1);
951 DUMP_REG(SOR_DP_MN0);
952 DUMP_REG(SOR_DP_MN1);
953 DUMP_REG(SOR_DP_PADCTL0);
954 DUMP_REG(SOR_DP_PADCTL1);
955 DUMP_REG(SOR_DP_DEBUG0);
956 DUMP_REG(SOR_DP_DEBUG1);
957 DUMP_REG(SOR_DP_SPARE0);
958 DUMP_REG(SOR_DP_SPARE1);
Thierry Redingdab16332015-01-26 16:04:08 +0100959 DUMP_REG(SOR_DP_AUDIO_CTRL);
960 DUMP_REG(SOR_DP_AUDIO_HBLANK_SYMBOLS);
961 DUMP_REG(SOR_DP_AUDIO_VBLANK_SYMBOLS);
962 DUMP_REG(SOR_DP_GENERIC_INFOFRAME_HEADER);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200963 DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK0);
964 DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK1);
965 DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK2);
966 DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK3);
967 DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK4);
968 DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK5);
969 DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK6);
Thierry Redingdab16332015-01-26 16:04:08 +0100970 DUMP_REG(SOR_DP_TPG);
971 DUMP_REG(SOR_DP_TPG_CONFIG);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200972 DUMP_REG(SOR_DP_LQ_CSTM0);
973 DUMP_REG(SOR_DP_LQ_CSTM1);
974 DUMP_REG(SOR_DP_LQ_CSTM2);
Thierry Redingdab16332015-01-26 16:04:08 +0100975
976#undef DUMP_REG
977
Thierry Reding850bab42015-07-29 17:58:41 +0200978unlock:
979 drm_modeset_unlock_all(drm);
980 return err;
Thierry Redingdab16332015-01-26 16:04:08 +0100981}
982
983static const struct drm_info_list debugfs_files[] = {
Thierry Reding530239a2015-08-06 11:04:54 +0200984 { "crc", tegra_sor_show_crc, 0, NULL },
Thierry Redingdab16332015-01-26 16:04:08 +0100985 { "regs", tegra_sor_show_regs, 0, NULL },
986};
987
Thierry Reding6fad8f62014-11-28 15:41:34 +0100988static int tegra_sor_debugfs_init(struct tegra_sor *sor,
989 struct drm_minor *minor)
990{
Thierry Reding459cc2c2015-07-30 10:34:24 +0200991 const char *name = sor->soc->supports_dp ? "sor1" : "sor";
Thierry Redingdab16332015-01-26 16:04:08 +0100992 unsigned int i;
Thierry Reding530239a2015-08-06 11:04:54 +0200993 int err;
Thierry Reding6fad8f62014-11-28 15:41:34 +0100994
Thierry Reding459cc2c2015-07-30 10:34:24 +0200995 sor->debugfs = debugfs_create_dir(name, minor->debugfs_root);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100996 if (!sor->debugfs)
997 return -ENOMEM;
998
Thierry Redingdab16332015-01-26 16:04:08 +0100999 sor->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1000 GFP_KERNEL);
1001 if (!sor->debugfs_files) {
Thierry Reding6fad8f62014-11-28 15:41:34 +01001002 err = -ENOMEM;
1003 goto remove;
1004 }
1005
Thierry Redingdab16332015-01-26 16:04:08 +01001006 for (i = 0; i < ARRAY_SIZE(debugfs_files); i++)
1007 sor->debugfs_files[i].data = sor;
1008
1009 err = drm_debugfs_create_files(sor->debugfs_files,
1010 ARRAY_SIZE(debugfs_files),
1011 sor->debugfs, minor);
1012 if (err < 0)
1013 goto free;
1014
Thierry Reding3ff1f222015-07-03 14:14:29 +02001015 sor->minor = minor;
1016
Thierry Reding530239a2015-08-06 11:04:54 +02001017 return 0;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001018
Thierry Redingdab16332015-01-26 16:04:08 +01001019free:
1020 kfree(sor->debugfs_files);
1021 sor->debugfs_files = NULL;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001022remove:
Thierry Redingdab16332015-01-26 16:04:08 +01001023 debugfs_remove_recursive(sor->debugfs);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001024 sor->debugfs = NULL;
1025 return err;
1026}
1027
Thierry Reding4009c222014-12-19 15:47:30 +01001028static void tegra_sor_debugfs_exit(struct tegra_sor *sor)
Thierry Reding6fad8f62014-11-28 15:41:34 +01001029{
Thierry Redingdab16332015-01-26 16:04:08 +01001030 drm_debugfs_remove_files(sor->debugfs_files, ARRAY_SIZE(debugfs_files),
1031 sor->minor);
1032 sor->minor = NULL;
1033
1034 kfree(sor->debugfs_files);
Thierry Reding066d30f2015-07-03 14:16:30 +02001035 sor->debugfs_files = NULL;
Thierry Redingdab16332015-01-26 16:04:08 +01001036
1037 debugfs_remove_recursive(sor->debugfs);
Thierry Reding066d30f2015-07-03 14:16:30 +02001038 sor->debugfs = NULL;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001039}
1040
Thierry Reding6fad8f62014-11-28 15:41:34 +01001041static enum drm_connector_status
1042tegra_sor_connector_detect(struct drm_connector *connector, bool force)
1043{
1044 struct tegra_output *output = connector_to_output(connector);
1045 struct tegra_sor *sor = to_sor(output);
1046
Thierry Reding9542c232015-07-08 13:39:09 +02001047 if (sor->aux)
1048 return drm_dp_aux_detect(sor->aux);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001049
Thierry Reding459cc2c2015-07-30 10:34:24 +02001050 return tegra_output_connector_detect(connector, force);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001051}
1052
1053static const struct drm_connector_funcs tegra_sor_connector_funcs = {
Thierry Reding850bab42015-07-29 17:58:41 +02001054 .dpms = drm_atomic_helper_connector_dpms,
Thierry Reding9d441892014-11-24 17:02:53 +01001055 .reset = drm_atomic_helper_connector_reset,
Thierry Reding6fad8f62014-11-28 15:41:34 +01001056 .detect = tegra_sor_connector_detect,
1057 .fill_modes = drm_helper_probe_single_connector_modes,
1058 .destroy = tegra_output_connector_destroy,
Thierry Reding9d441892014-11-24 17:02:53 +01001059 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
Thierry Reding4aa3df72014-11-24 16:27:13 +01001060 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
Thierry Reding6fad8f62014-11-28 15:41:34 +01001061};
1062
1063static int tegra_sor_connector_get_modes(struct drm_connector *connector)
1064{
1065 struct tegra_output *output = connector_to_output(connector);
1066 struct tegra_sor *sor = to_sor(output);
1067 int err;
1068
Thierry Reding9542c232015-07-08 13:39:09 +02001069 if (sor->aux)
1070 drm_dp_aux_enable(sor->aux);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001071
1072 err = tegra_output_connector_get_modes(connector);
1073
Thierry Reding9542c232015-07-08 13:39:09 +02001074 if (sor->aux)
1075 drm_dp_aux_disable(sor->aux);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001076
1077 return err;
1078}
1079
1080static enum drm_mode_status
1081tegra_sor_connector_mode_valid(struct drm_connector *connector,
1082 struct drm_display_mode *mode)
1083{
1084 return MODE_OK;
1085}
1086
1087static const struct drm_connector_helper_funcs tegra_sor_connector_helper_funcs = {
1088 .get_modes = tegra_sor_connector_get_modes,
1089 .mode_valid = tegra_sor_connector_mode_valid,
1090 .best_encoder = tegra_output_connector_best_encoder,
1091};
1092
1093static const struct drm_encoder_funcs tegra_sor_encoder_funcs = {
1094 .destroy = tegra_output_encoder_destroy,
1095};
1096
Thierry Reding850bab42015-07-29 17:58:41 +02001097static void tegra_sor_edp_disable(struct drm_encoder *encoder)
Thierry Reding6fad8f62014-11-28 15:41:34 +01001098{
Thierry Reding850bab42015-07-29 17:58:41 +02001099 struct tegra_output *output = encoder_to_output(encoder);
1100 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
1101 struct tegra_sor *sor = to_sor(output);
1102 u32 value;
1103 int err;
1104
1105 if (output->panel)
1106 drm_panel_disable(output->panel);
1107
1108 err = tegra_sor_detach(sor);
1109 if (err < 0)
1110 dev_err(sor->dev, "failed to detach SOR: %d\n", err);
1111
1112 tegra_sor_writel(sor, 0, SOR_STATE1);
1113 tegra_sor_update(sor);
1114
1115 /*
1116 * The following accesses registers of the display controller, so make
1117 * sure it's only executed when the output is attached to one.
1118 */
1119 if (dc) {
1120 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
1121 value &= ~SOR_ENABLE;
1122 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
1123
1124 tegra_dc_commit(dc);
1125 }
1126
1127 err = tegra_sor_power_down(sor);
1128 if (err < 0)
1129 dev_err(sor->dev, "failed to power down SOR: %d\n", err);
1130
Thierry Reding9542c232015-07-08 13:39:09 +02001131 if (sor->aux) {
1132 err = drm_dp_aux_disable(sor->aux);
Thierry Reding850bab42015-07-29 17:58:41 +02001133 if (err < 0)
1134 dev_err(sor->dev, "failed to disable DP: %d\n", err);
1135 }
1136
1137 err = tegra_io_rail_power_off(TEGRA_IO_RAIL_LVDS);
1138 if (err < 0)
1139 dev_err(sor->dev, "failed to power off I/O rail: %d\n", err);
1140
1141 if (output->panel)
1142 drm_panel_unprepare(output->panel);
1143
1144 reset_control_assert(sor->rst);
1145 clk_disable_unprepare(sor->clk);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001146}
1147
Thierry Reding459cc2c2015-07-30 10:34:24 +02001148#if 0
1149static int calc_h_ref_to_sync(const struct drm_display_mode *mode,
1150 unsigned int *value)
1151{
1152 unsigned int hfp, hsw, hbp, a = 0, b;
1153
1154 hfp = mode->hsync_start - mode->hdisplay;
1155 hsw = mode->hsync_end - mode->hsync_start;
1156 hbp = mode->htotal - mode->hsync_end;
1157
1158 pr_info("hfp: %u, hsw: %u, hbp: %u\n", hfp, hsw, hbp);
1159
1160 b = hfp - 1;
1161
1162 pr_info("a: %u, b: %u\n", a, b);
1163 pr_info("a + hsw + hbp = %u\n", a + hsw + hbp);
1164
1165 if (a + hsw + hbp <= 11) {
1166 a = 1 + 11 - hsw - hbp;
1167 pr_info("a: %u\n", a);
1168 }
1169
1170 if (a > b)
1171 return -EINVAL;
1172
1173 if (hsw < 1)
1174 return -EINVAL;
1175
1176 if (mode->hdisplay < 16)
1177 return -EINVAL;
1178
1179 if (value) {
1180 if (b > a && a % 2)
1181 *value = a + 1;
1182 else
1183 *value = a;
1184 }
1185
1186 return 0;
1187}
1188#endif
1189
Thierry Reding850bab42015-07-29 17:58:41 +02001190static void tegra_sor_edp_enable(struct drm_encoder *encoder)
Thierry Reding6fad8f62014-11-28 15:41:34 +01001191{
Thierry Reding850bab42015-07-29 17:58:41 +02001192 struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001193 struct tegra_output *output = encoder_to_output(encoder);
1194 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001195 unsigned int vbe, vse, hbe, hse, vbs, hbs, i;
1196 struct tegra_sor *sor = to_sor(output);
Thierry Reding34fa1832014-06-05 16:31:10 +02001197 struct tegra_sor_config config;
1198 struct drm_dp_link link;
Thierry Reding86f5c522014-03-26 11:13:16 +01001199 int err = 0;
Thierry Reding28fe2072015-01-26 16:02:48 +01001200 u32 value;
Thierry Reding86f5c522014-03-26 11:13:16 +01001201
Thierry Reding6b6b6042013-11-15 16:06:05 +01001202 err = clk_prepare_enable(sor->clk);
1203 if (err < 0)
Thierry Reding850bab42015-07-29 17:58:41 +02001204 dev_err(sor->dev, "failed to enable clock: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001205
1206 reset_control_deassert(sor->rst);
1207
Thierry Reding6fad8f62014-11-28 15:41:34 +01001208 if (output->panel)
1209 drm_panel_prepare(output->panel);
1210
Thierry Reding9542c232015-07-08 13:39:09 +02001211 if (sor->aux) {
1212 err = drm_dp_aux_enable(sor->aux);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001213 if (err < 0)
1214 dev_err(sor->dev, "failed to enable DP: %d\n", err);
Thierry Reding34fa1832014-06-05 16:31:10 +02001215
Thierry Reding9542c232015-07-08 13:39:09 +02001216 err = drm_dp_link_probe(sor->aux, &link);
Thierry Reding34fa1832014-06-05 16:31:10 +02001217 if (err < 0) {
1218 dev_err(sor->dev, "failed to probe eDP link: %d\n",
1219 err);
Thierry Reding850bab42015-07-29 17:58:41 +02001220 return;
Thierry Reding34fa1832014-06-05 16:31:10 +02001221 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01001222 }
1223
1224 err = clk_set_parent(sor->clk, sor->clk_safe);
1225 if (err < 0)
1226 dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
1227
Thierry Reding34fa1832014-06-05 16:31:10 +02001228 memset(&config, 0, sizeof(config));
Stéphane Marchesin054b1bd2014-06-19 18:18:29 -07001229 config.bits_per_pixel = output->connector.display_info.bpc * 3;
Thierry Reding34fa1832014-06-05 16:31:10 +02001230
1231 err = tegra_sor_calc_config(sor, mode, &config, &link);
1232 if (err < 0)
1233 dev_err(sor->dev, "failed to compute link configuration: %d\n",
1234 err);
1235
Thierry Reding6b6b6042013-11-15 16:06:05 +01001236 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
1237 value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
1238 value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
1239 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
1240
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001241 value = tegra_sor_readl(sor, SOR_PLL2);
1242 value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
1243 tegra_sor_writel(sor, value, SOR_PLL2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001244 usleep_range(20, 100);
1245
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001246 value = tegra_sor_readl(sor, SOR_PLL3);
1247 value |= SOR_PLL3_PLL_VDD_MODE_3V3;
1248 tegra_sor_writel(sor, value, SOR_PLL3);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001249
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001250 value = SOR_PLL0_ICHPMP(0xf) | SOR_PLL0_VCOCAP_RST |
1251 SOR_PLL0_PLLREG_LEVEL_V45 | SOR_PLL0_RESISTOR_EXT;
1252 tegra_sor_writel(sor, value, SOR_PLL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001253
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001254 value = tegra_sor_readl(sor, SOR_PLL2);
1255 value |= SOR_PLL2_SEQ_PLLCAPPD;
1256 value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1257 value |= SOR_PLL2_LVDS_ENABLE;
1258 tegra_sor_writel(sor, value, SOR_PLL2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001259
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001260 value = SOR_PLL1_TERM_COMPOUT | SOR_PLL1_TMDS_TERM;
1261 tegra_sor_writel(sor, value, SOR_PLL1);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001262
1263 while (true) {
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001264 value = tegra_sor_readl(sor, SOR_PLL2);
1265 if ((value & SOR_PLL2_SEQ_PLLCAPPD_ENFORCE) == 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001266 break;
1267
1268 usleep_range(250, 1000);
1269 }
1270
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001271 value = tegra_sor_readl(sor, SOR_PLL2);
1272 value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
1273 value &= ~SOR_PLL2_PORT_POWERDOWN;
1274 tegra_sor_writel(sor, value, SOR_PLL2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001275
1276 /*
1277 * power up
1278 */
1279
1280 /* set safe link bandwidth (1.62 Gbps) */
1281 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
1282 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
1283 value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G1_62;
1284 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
1285
1286 /* step 1 */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001287 value = tegra_sor_readl(sor, SOR_PLL2);
1288 value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE | SOR_PLL2_PORT_POWERDOWN |
1289 SOR_PLL2_BANDGAP_POWERDOWN;
1290 tegra_sor_writel(sor, value, SOR_PLL2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001291
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001292 value = tegra_sor_readl(sor, SOR_PLL0);
1293 value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
1294 tegra_sor_writel(sor, value, SOR_PLL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001295
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001296 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001297 value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001298 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001299
1300 /* step 2 */
1301 err = tegra_io_rail_power_on(TEGRA_IO_RAIL_LVDS);
Thierry Reding850bab42015-07-29 17:58:41 +02001302 if (err < 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001303 dev_err(sor->dev, "failed to power on I/O rail: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001304
1305 usleep_range(5, 100);
1306
1307 /* step 3 */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001308 value = tegra_sor_readl(sor, SOR_PLL2);
1309 value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
1310 tegra_sor_writel(sor, value, SOR_PLL2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001311
1312 usleep_range(20, 100);
1313
1314 /* step 4 */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001315 value = tegra_sor_readl(sor, SOR_PLL0);
1316 value &= ~SOR_PLL0_VCOPD;
1317 value &= ~SOR_PLL0_PWR;
1318 tegra_sor_writel(sor, value, SOR_PLL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001319
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001320 value = tegra_sor_readl(sor, SOR_PLL2);
1321 value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1322 tegra_sor_writel(sor, value, SOR_PLL2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001323
1324 usleep_range(200, 1000);
1325
1326 /* step 5 */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001327 value = tegra_sor_readl(sor, SOR_PLL2);
1328 value &= ~SOR_PLL2_PORT_POWERDOWN;
1329 tegra_sor_writel(sor, value, SOR_PLL2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001330
1331 /* switch to DP clock */
1332 err = clk_set_parent(sor->clk, sor->clk_dp);
1333 if (err < 0)
1334 dev_err(sor->dev, "failed to set DP parent clock: %d\n", err);
1335
Thierry Reding899451b2014-06-05 16:19:48 +02001336 /* power DP lanes */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001337 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
Thierry Reding899451b2014-06-05 16:19:48 +02001338
1339 if (link.num_lanes <= 2)
1340 value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2);
1341 else
1342 value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2;
1343
1344 if (link.num_lanes <= 1)
1345 value &= ~SOR_DP_PADCTL_PD_TXD_1;
1346 else
1347 value |= SOR_DP_PADCTL_PD_TXD_1;
1348
1349 if (link.num_lanes == 0)
1350 value &= ~SOR_DP_PADCTL_PD_TXD_0;
1351 else
1352 value |= SOR_DP_PADCTL_PD_TXD_0;
1353
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001354 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001355
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001356 value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001357 value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
Thierry Reding0c90a182014-06-05 16:29:46 +02001358 value |= SOR_DP_LINKCTL_LANE_COUNT(link.num_lanes);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001359 tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001360
1361 /* start lane sequencer */
1362 value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
1363 SOR_LANE_SEQ_CTL_POWER_STATE_UP;
1364 tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
1365
1366 while (true) {
1367 value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
1368 if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
1369 break;
1370
1371 usleep_range(250, 1000);
1372 }
1373
Thierry Redinga4263fe2014-06-05 16:16:23 +02001374 /* set link bandwidth */
Thierry Reding6b6b6042013-11-15 16:06:05 +01001375 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
1376 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
Thierry Redinga4263fe2014-06-05 16:16:23 +02001377 value |= drm_dp_link_rate_to_bw_code(link.rate) << 2;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001378 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
1379
1380 /* set linkctl */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001381 value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001382 value |= SOR_DP_LINKCTL_ENABLE;
1383
1384 value &= ~SOR_DP_LINKCTL_TU_SIZE_MASK;
Thierry Reding34fa1832014-06-05 16:31:10 +02001385 value |= SOR_DP_LINKCTL_TU_SIZE(config.tu_size);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001386
1387 value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001388 tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001389
1390 for (i = 0, value = 0; i < 4; i++) {
1391 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
1392 SOR_DP_TPG_SCRAMBLER_GALIOS |
1393 SOR_DP_TPG_PATTERN_NONE;
1394 value = (value << 8) | lane;
1395 }
1396
1397 tegra_sor_writel(sor, value, SOR_DP_TPG);
1398
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001399 value = tegra_sor_readl(sor, SOR_DP_CONFIG0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001400 value &= ~SOR_DP_CONFIG_WATERMARK_MASK;
Thierry Reding34fa1832014-06-05 16:31:10 +02001401 value |= SOR_DP_CONFIG_WATERMARK(config.watermark);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001402
1403 value &= ~SOR_DP_CONFIG_ACTIVE_SYM_COUNT_MASK;
Thierry Reding34fa1832014-06-05 16:31:10 +02001404 value |= SOR_DP_CONFIG_ACTIVE_SYM_COUNT(config.active_count);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001405
1406 value &= ~SOR_DP_CONFIG_ACTIVE_SYM_FRAC_MASK;
Thierry Reding34fa1832014-06-05 16:31:10 +02001407 value |= SOR_DP_CONFIG_ACTIVE_SYM_FRAC(config.active_frac);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001408
Thierry Reding34fa1832014-06-05 16:31:10 +02001409 if (config.active_polarity)
1410 value |= SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
1411 else
1412 value &= ~SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001413
1414 value |= SOR_DP_CONFIG_ACTIVE_SYM_ENABLE;
Thierry Reding1f64ae72014-06-05 16:20:27 +02001415 value |= SOR_DP_CONFIG_DISPARITY_NEGATIVE;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001416 tegra_sor_writel(sor, value, SOR_DP_CONFIG0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001417
1418 value = tegra_sor_readl(sor, SOR_DP_AUDIO_HBLANK_SYMBOLS);
1419 value &= ~SOR_DP_AUDIO_HBLANK_SYMBOLS_MASK;
Thierry Reding7890b572014-06-05 16:12:46 +02001420 value |= config.hblank_symbols & 0xffff;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001421 tegra_sor_writel(sor, value, SOR_DP_AUDIO_HBLANK_SYMBOLS);
1422
1423 value = tegra_sor_readl(sor, SOR_DP_AUDIO_VBLANK_SYMBOLS);
1424 value &= ~SOR_DP_AUDIO_VBLANK_SYMBOLS_MASK;
Thierry Reding7890b572014-06-05 16:12:46 +02001425 value |= config.vblank_symbols & 0xffff;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001426 tegra_sor_writel(sor, value, SOR_DP_AUDIO_VBLANK_SYMBOLS);
1427
1428 /* enable pad calibration logic */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001429 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001430 value |= SOR_DP_PADCTL_PAD_CAL_PD;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001431 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001432
Thierry Reding9542c232015-07-08 13:39:09 +02001433 if (sor->aux) {
Thierry Reding6b6b6042013-11-15 16:06:05 +01001434 u8 rate, lanes;
1435
Thierry Reding9542c232015-07-08 13:39:09 +02001436 err = drm_dp_link_probe(sor->aux, &link);
Thierry Reding850bab42015-07-29 17:58:41 +02001437 if (err < 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001438 dev_err(sor->dev, "failed to probe eDP link: %d\n",
1439 err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001440
Thierry Reding9542c232015-07-08 13:39:09 +02001441 err = drm_dp_link_power_up(sor->aux, &link);
Thierry Reding850bab42015-07-29 17:58:41 +02001442 if (err < 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001443 dev_err(sor->dev, "failed to power up eDP link: %d\n",
1444 err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001445
Thierry Reding9542c232015-07-08 13:39:09 +02001446 err = drm_dp_link_configure(sor->aux, &link);
Thierry Reding850bab42015-07-29 17:58:41 +02001447 if (err < 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001448 dev_err(sor->dev, "failed to configure eDP link: %d\n",
1449 err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001450
1451 rate = drm_dp_link_rate_to_bw_code(link.rate);
1452 lanes = link.num_lanes;
1453
1454 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
1455 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
1456 value |= SOR_CLK_CNTRL_DP_LINK_SPEED(rate);
1457 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
1458
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001459 value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001460 value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
1461 value |= SOR_DP_LINKCTL_LANE_COUNT(lanes);
1462
1463 if (link.capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
1464 value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
1465
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001466 tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001467
1468 /* disable training pattern generator */
1469
1470 for (i = 0; i < link.num_lanes; i++) {
1471 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
1472 SOR_DP_TPG_SCRAMBLER_GALIOS |
1473 SOR_DP_TPG_PATTERN_NONE;
1474 value = (value << 8) | lane;
1475 }
1476
1477 tegra_sor_writel(sor, value, SOR_DP_TPG);
1478
1479 err = tegra_sor_dp_train_fast(sor, &link);
1480 if (err < 0) {
1481 dev_err(sor->dev, "DP fast link training failed: %d\n",
1482 err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001483 }
1484
1485 dev_dbg(sor->dev, "fast link training succeeded\n");
1486 }
1487
1488 err = tegra_sor_power_up(sor, 250);
Thierry Reding850bab42015-07-29 17:58:41 +02001489 if (err < 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001490 dev_err(sor->dev, "failed to power up SOR: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001491
Thierry Reding6b6b6042013-11-15 16:06:05 +01001492 /*
1493 * configure panel (24bpp, vsync-, hsync-, DP-A protocol, complete
1494 * raster, associate with display controller)
1495 */
Thierry Reding3f4f3b52014-07-07 15:35:06 +02001496 value = SOR_STATE_ASY_PROTOCOL_DP_A |
Thierry Reding6b6b6042013-11-15 16:06:05 +01001497 SOR_STATE_ASY_CRC_MODE_COMPLETE |
1498 SOR_STATE_ASY_OWNER(dc->pipe + 1);
Thierry Reding34fa1832014-06-05 16:31:10 +02001499
Thierry Reding3f4f3b52014-07-07 15:35:06 +02001500 if (mode->flags & DRM_MODE_FLAG_PHSYNC)
1501 value &= ~SOR_STATE_ASY_HSYNCPOL;
1502
1503 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1504 value |= SOR_STATE_ASY_HSYNCPOL;
1505
1506 if (mode->flags & DRM_MODE_FLAG_PVSYNC)
1507 value &= ~SOR_STATE_ASY_VSYNCPOL;
1508
1509 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1510 value |= SOR_STATE_ASY_VSYNCPOL;
1511
Thierry Reding34fa1832014-06-05 16:31:10 +02001512 switch (config.bits_per_pixel) {
1513 case 24:
1514 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
1515 break;
1516
1517 case 18:
1518 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_18_444;
1519 break;
1520
1521 default:
1522 BUG();
1523 break;
1524 }
1525
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001526 tegra_sor_writel(sor, value, SOR_STATE1);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001527
1528 /*
1529 * TODO: The video timing programming below doesn't seem to match the
1530 * register definitions.
1531 */
1532
1533 value = ((mode->vtotal & 0x7fff) << 16) | (mode->htotal & 0x7fff);
Thierry Reding51511d02015-07-30 18:47:07 +02001534 tegra_sor_writel(sor, value, SOR_HEAD_STATE1(dc->pipe));
Thierry Reding6b6b6042013-11-15 16:06:05 +01001535
1536 vse = mode->vsync_end - mode->vsync_start - 1;
1537 hse = mode->hsync_end - mode->hsync_start - 1;
1538
1539 value = ((vse & 0x7fff) << 16) | (hse & 0x7fff);
Thierry Reding51511d02015-07-30 18:47:07 +02001540 tegra_sor_writel(sor, value, SOR_HEAD_STATE2(dc->pipe));
Thierry Reding6b6b6042013-11-15 16:06:05 +01001541
1542 vbe = vse + (mode->vsync_start - mode->vdisplay);
1543 hbe = hse + (mode->hsync_start - mode->hdisplay);
1544
1545 value = ((vbe & 0x7fff) << 16) | (hbe & 0x7fff);
Thierry Reding51511d02015-07-30 18:47:07 +02001546 tegra_sor_writel(sor, value, SOR_HEAD_STATE3(dc->pipe));
Thierry Reding6b6b6042013-11-15 16:06:05 +01001547
1548 vbs = vbe + mode->vdisplay;
1549 hbs = hbe + mode->hdisplay;
1550
1551 value = ((vbs & 0x7fff) << 16) | (hbs & 0x7fff);
Thierry Reding51511d02015-07-30 18:47:07 +02001552 tegra_sor_writel(sor, value, SOR_HEAD_STATE4(dc->pipe));
1553
1554 tegra_sor_writel(sor, 0x1, SOR_HEAD_STATE5(dc->pipe));
Thierry Reding6b6b6042013-11-15 16:06:05 +01001555
Thierry Reding6b6b6042013-11-15 16:06:05 +01001556 /* CSTM (LVDS, link A/B, upper) */
Stéphane Marchesin143b1df2014-05-22 20:32:47 -07001557 value = SOR_CSTM_LVDS | SOR_CSTM_LINK_ACT_A | SOR_CSTM_LINK_ACT_B |
Thierry Reding6b6b6042013-11-15 16:06:05 +01001558 SOR_CSTM_UPPER;
1559 tegra_sor_writel(sor, value, SOR_CSTM);
1560
1561 /* PWM setup */
1562 err = tegra_sor_setup_pwm(sor, 250);
Thierry Reding850bab42015-07-29 17:58:41 +02001563 if (err < 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001564 dev_err(sor->dev, "failed to setup PWM: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001565
Thierry Reding666cb872014-12-08 16:32:47 +01001566 tegra_sor_update(sor);
1567
Thierry Reding6b6b6042013-11-15 16:06:05 +01001568 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
1569 value |= SOR_ENABLE;
1570 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
1571
Thierry Reding666cb872014-12-08 16:32:47 +01001572 tegra_dc_commit(dc);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001573
1574 err = tegra_sor_attach(sor);
Thierry Reding850bab42015-07-29 17:58:41 +02001575 if (err < 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001576 dev_err(sor->dev, "failed to attach SOR: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001577
1578 err = tegra_sor_wakeup(sor);
Thierry Reding850bab42015-07-29 17:58:41 +02001579 if (err < 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001580 dev_err(sor->dev, "failed to enable DC: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001581
Thierry Reding6fad8f62014-11-28 15:41:34 +01001582 if (output->panel)
1583 drm_panel_enable(output->panel);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001584}
1585
Thierry Reding82f15112014-12-08 17:26:46 +01001586static int
1587tegra_sor_encoder_atomic_check(struct drm_encoder *encoder,
1588 struct drm_crtc_state *crtc_state,
1589 struct drm_connector_state *conn_state)
1590{
1591 struct tegra_output *output = encoder_to_output(encoder);
1592 struct tegra_dc *dc = to_tegra_dc(conn_state->crtc);
1593 unsigned long pclk = crtc_state->mode.clock * 1000;
1594 struct tegra_sor *sor = to_sor(output);
1595 int err;
1596
1597 err = tegra_dc_state_setup_clock(dc, crtc_state, sor->clk_parent,
1598 pclk, 0);
1599 if (err < 0) {
1600 dev_err(output->dev, "failed to setup CRTC state: %d\n", err);
1601 return err;
1602 }
1603
1604 return 0;
1605}
1606
Thierry Reding459cc2c2015-07-30 10:34:24 +02001607static const struct drm_encoder_helper_funcs tegra_sor_edp_helpers = {
Thierry Reding850bab42015-07-29 17:58:41 +02001608 .disable = tegra_sor_edp_disable,
1609 .enable = tegra_sor_edp_enable,
Thierry Reding82f15112014-12-08 17:26:46 +01001610 .atomic_check = tegra_sor_encoder_atomic_check,
Thierry Reding6b6b6042013-11-15 16:06:05 +01001611};
1612
Thierry Reding459cc2c2015-07-30 10:34:24 +02001613static inline u32 tegra_sor_hdmi_subpack(const u8 *ptr, size_t size)
1614{
1615 u32 value = 0;
1616 size_t i;
1617
1618 for (i = size; i > 0; i--)
1619 value = (value << 8) | ptr[i - 1];
1620
1621 return value;
1622}
1623
1624static void tegra_sor_hdmi_write_infopack(struct tegra_sor *sor,
1625 const void *data, size_t size)
1626{
1627 const u8 *ptr = data;
1628 unsigned long offset;
1629 size_t i, j;
1630 u32 value;
1631
1632 switch (ptr[0]) {
1633 case HDMI_INFOFRAME_TYPE_AVI:
1634 offset = SOR_HDMI_AVI_INFOFRAME_HEADER;
1635 break;
1636
1637 case HDMI_INFOFRAME_TYPE_AUDIO:
1638 offset = SOR_HDMI_AUDIO_INFOFRAME_HEADER;
1639 break;
1640
1641 case HDMI_INFOFRAME_TYPE_VENDOR:
1642 offset = SOR_HDMI_VSI_INFOFRAME_HEADER;
1643 break;
1644
1645 default:
1646 dev_err(sor->dev, "unsupported infoframe type: %02x\n",
1647 ptr[0]);
1648 return;
1649 }
1650
1651 value = INFOFRAME_HEADER_TYPE(ptr[0]) |
1652 INFOFRAME_HEADER_VERSION(ptr[1]) |
1653 INFOFRAME_HEADER_LEN(ptr[2]);
1654 tegra_sor_writel(sor, value, offset);
1655 offset++;
1656
1657 /*
1658 * Each subpack contains 7 bytes, divided into:
1659 * - subpack_low: bytes 0 - 3
1660 * - subpack_high: bytes 4 - 6 (with byte 7 padded to 0x00)
1661 */
1662 for (i = 3, j = 0; i < size; i += 7, j += 8) {
1663 size_t rem = size - i, num = min_t(size_t, rem, 4);
1664
1665 value = tegra_sor_hdmi_subpack(&ptr[i], num);
1666 tegra_sor_writel(sor, value, offset++);
1667
1668 num = min_t(size_t, rem - num, 3);
1669
1670 value = tegra_sor_hdmi_subpack(&ptr[i + 4], num);
1671 tegra_sor_writel(sor, value, offset++);
1672 }
1673}
1674
1675static int
1676tegra_sor_hdmi_setup_avi_infoframe(struct tegra_sor *sor,
1677 const struct drm_display_mode *mode)
1678{
1679 u8 buffer[HDMI_INFOFRAME_SIZE(AVI)];
1680 struct hdmi_avi_infoframe frame;
1681 u32 value;
1682 int err;
1683
1684 /* disable AVI infoframe */
1685 value = tegra_sor_readl(sor, SOR_HDMI_AVI_INFOFRAME_CTRL);
1686 value &= ~INFOFRAME_CTRL_SINGLE;
1687 value &= ~INFOFRAME_CTRL_OTHER;
1688 value &= ~INFOFRAME_CTRL_ENABLE;
1689 tegra_sor_writel(sor, value, SOR_HDMI_AVI_INFOFRAME_CTRL);
1690
1691 err = drm_hdmi_avi_infoframe_from_display_mode(&frame, mode);
1692 if (err < 0) {
1693 dev_err(sor->dev, "failed to setup AVI infoframe: %d\n", err);
1694 return err;
1695 }
1696
1697 err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
1698 if (err < 0) {
1699 dev_err(sor->dev, "failed to pack AVI infoframe: %d\n", err);
1700 return err;
1701 }
1702
1703 tegra_sor_hdmi_write_infopack(sor, buffer, err);
1704
1705 /* enable AVI infoframe */
1706 value = tegra_sor_readl(sor, SOR_HDMI_AVI_INFOFRAME_CTRL);
1707 value |= INFOFRAME_CTRL_CHECKSUM_ENABLE;
1708 value |= INFOFRAME_CTRL_ENABLE;
1709 tegra_sor_writel(sor, value, SOR_HDMI_AVI_INFOFRAME_CTRL);
1710
1711 return 0;
1712}
1713
1714static void tegra_sor_hdmi_disable_audio_infoframe(struct tegra_sor *sor)
1715{
1716 u32 value;
1717
1718 value = tegra_sor_readl(sor, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
1719 value &= ~INFOFRAME_CTRL_ENABLE;
1720 tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
1721}
1722
1723static struct tegra_sor_hdmi_settings *
1724tegra_sor_hdmi_find_settings(struct tegra_sor *sor, unsigned long frequency)
1725{
1726 unsigned int i;
1727
1728 for (i = 0; i < sor->num_settings; i++)
1729 if (frequency <= sor->settings[i].frequency)
1730 return &sor->settings[i];
1731
1732 return NULL;
1733}
1734
1735static void tegra_sor_hdmi_disable(struct drm_encoder *encoder)
1736{
1737 struct tegra_output *output = encoder_to_output(encoder);
1738 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
1739 struct tegra_sor *sor = to_sor(output);
1740 u32 value;
1741 int err;
1742
1743 err = tegra_sor_detach(sor);
1744 if (err < 0)
1745 dev_err(sor->dev, "failed to detach SOR: %d\n", err);
1746
1747 tegra_sor_writel(sor, 0, SOR_STATE1);
1748 tegra_sor_update(sor);
1749
1750 /* disable display to SOR clock */
1751 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
1752 value &= ~SOR1_TIMING_CYA;
1753 value &= ~SOR1_ENABLE;
1754 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
1755
1756 tegra_dc_commit(dc);
1757
1758 err = tegra_sor_power_down(sor);
1759 if (err < 0)
1760 dev_err(sor->dev, "failed to power down SOR: %d\n", err);
1761
1762 err = tegra_io_rail_power_off(TEGRA_IO_RAIL_HDMI);
1763 if (err < 0)
1764 dev_err(sor->dev, "failed to power off HDMI rail: %d\n", err);
1765
1766 reset_control_assert(sor->rst);
1767 usleep_range(1000, 2000);
1768 clk_disable_unprepare(sor->clk);
1769}
1770
1771static void tegra_sor_hdmi_enable(struct drm_encoder *encoder)
1772{
1773 struct tegra_output *output = encoder_to_output(encoder);
1774 unsigned int h_ref_to_sync = 1, pulse_start, max_ac;
1775 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
1776 unsigned int vbe, vse, hbe, hse, vbs, hbs, div;
1777 struct tegra_sor_hdmi_settings *settings;
1778 struct tegra_sor *sor = to_sor(output);
1779 struct drm_display_mode *mode;
1780 struct drm_display_info *info;
1781 u32 value;
1782 int err;
1783
1784 mode = &encoder->crtc->state->adjusted_mode;
1785 info = &output->connector.display_info;
1786
1787 err = clk_prepare_enable(sor->clk);
1788 if (err < 0)
1789 dev_err(sor->dev, "failed to enable clock: %d\n", err);
1790
1791 usleep_range(1000, 2000);
1792
1793 reset_control_deassert(sor->rst);
1794
1795 err = clk_set_parent(sor->clk, sor->clk_safe);
1796 if (err < 0)
1797 dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
1798
1799 div = clk_get_rate(sor->clk) / 1000000 * 4;
1800
1801 err = tegra_io_rail_power_on(TEGRA_IO_RAIL_HDMI);
1802 if (err < 0)
1803 dev_err(sor->dev, "failed to power on HDMI rail: %d\n", err);
1804
1805 usleep_range(20, 100);
1806
1807 value = tegra_sor_readl(sor, SOR_PLL2);
1808 value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
1809 tegra_sor_writel(sor, value, SOR_PLL2);
1810
1811 usleep_range(20, 100);
1812
1813 value = tegra_sor_readl(sor, SOR_PLL3);
1814 value &= ~SOR_PLL3_PLL_VDD_MODE_3V3;
1815 tegra_sor_writel(sor, value, SOR_PLL3);
1816
1817 value = tegra_sor_readl(sor, SOR_PLL0);
1818 value &= ~SOR_PLL0_VCOPD;
1819 value &= ~SOR_PLL0_PWR;
1820 tegra_sor_writel(sor, value, SOR_PLL0);
1821
1822 value = tegra_sor_readl(sor, SOR_PLL2);
1823 value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1824 tegra_sor_writel(sor, value, SOR_PLL2);
1825
1826 usleep_range(200, 400);
1827
1828 value = tegra_sor_readl(sor, SOR_PLL2);
1829 value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
1830 value &= ~SOR_PLL2_PORT_POWERDOWN;
1831 tegra_sor_writel(sor, value, SOR_PLL2);
1832
1833 usleep_range(20, 100);
1834
1835 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
1836 value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
1837 SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2;
1838 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
1839
1840 while (true) {
1841 value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
1842 if ((value & SOR_LANE_SEQ_CTL_STATE_BUSY) == 0)
1843 break;
1844
1845 usleep_range(250, 1000);
1846 }
1847
1848 value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
1849 SOR_LANE_SEQ_CTL_POWER_STATE_UP | SOR_LANE_SEQ_CTL_DELAY(5);
1850 tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
1851
1852 while (true) {
1853 value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
1854 if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
1855 break;
1856
1857 usleep_range(250, 1000);
1858 }
1859
1860 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
1861 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
1862 value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
1863
1864 if (mode->clock < 340000)
1865 value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G2_70;
1866 else
1867 value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G5_40;
1868
1869 value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK;
1870 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
1871
1872 value = tegra_sor_readl(sor, SOR_DP_SPARE0);
1873 value |= SOR_DP_SPARE_DISP_VIDEO_PREAMBLE;
1874 value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
1875 value |= SOR_DP_SPARE_SEQ_ENABLE;
1876 tegra_sor_writel(sor, value, SOR_DP_SPARE0);
1877
1878 value = SOR_SEQ_CTL_PU_PC(0) | SOR_SEQ_CTL_PU_PC_ALT(0) |
1879 SOR_SEQ_CTL_PD_PC(8) | SOR_SEQ_CTL_PD_PC_ALT(8);
1880 tegra_sor_writel(sor, value, SOR_SEQ_CTL);
1881
1882 value = SOR_SEQ_INST_DRIVE_PWM_OUT_LO | SOR_SEQ_INST_HALT |
1883 SOR_SEQ_INST_WAIT_VSYNC | SOR_SEQ_INST_WAIT(1);
1884 tegra_sor_writel(sor, value, SOR_SEQ_INST(0));
1885 tegra_sor_writel(sor, value, SOR_SEQ_INST(8));
1886
1887 /* program the reference clock */
1888 value = SOR_REFCLK_DIV_INT(div) | SOR_REFCLK_DIV_FRAC(div);
1889 tegra_sor_writel(sor, value, SOR_REFCLK);
1890
1891 /* XXX don't hardcode */
1892 value = SOR_XBAR_CTRL_LINK1_XSEL(4, 4) |
1893 SOR_XBAR_CTRL_LINK1_XSEL(3, 3) |
1894 SOR_XBAR_CTRL_LINK1_XSEL(2, 2) |
1895 SOR_XBAR_CTRL_LINK1_XSEL(1, 1) |
1896 SOR_XBAR_CTRL_LINK1_XSEL(0, 0) |
1897 SOR_XBAR_CTRL_LINK0_XSEL(4, 4) |
1898 SOR_XBAR_CTRL_LINK0_XSEL(3, 3) |
1899 SOR_XBAR_CTRL_LINK0_XSEL(2, 0) |
1900 SOR_XBAR_CTRL_LINK0_XSEL(1, 1) |
1901 SOR_XBAR_CTRL_LINK0_XSEL(0, 2);
1902 tegra_sor_writel(sor, value, SOR_XBAR_CTRL);
1903
1904 tegra_sor_writel(sor, 0x00000000, SOR_XBAR_POL);
1905
1906 err = clk_set_parent(sor->clk, sor->clk_parent);
1907 if (err < 0)
1908 dev_err(sor->dev, "failed to set parent clock: %d\n", err);
1909
1910 value = SOR_INPUT_CONTROL_HDMI_SRC_SELECT(dc->pipe);
1911
1912 /* XXX is this the proper check? */
1913 if (mode->clock < 75000)
1914 value |= SOR_INPUT_CONTROL_ARM_VIDEO_RANGE_LIMITED;
1915
1916 tegra_sor_writel(sor, value, SOR_INPUT_CONTROL);
1917
1918 max_ac = ((mode->htotal - mode->hdisplay) - SOR_REKEY - 18) / 32;
1919
1920 value = SOR_HDMI_CTRL_ENABLE | SOR_HDMI_CTRL_MAX_AC_PACKET(max_ac) |
1921 SOR_HDMI_CTRL_AUDIO_LAYOUT | SOR_HDMI_CTRL_REKEY(SOR_REKEY);
1922 tegra_sor_writel(sor, value, SOR_HDMI_CTRL);
1923
1924 /* H_PULSE2 setup */
1925 pulse_start = h_ref_to_sync + (mode->hsync_end - mode->hsync_start) +
1926 (mode->htotal - mode->hsync_end) - 10;
1927
1928 value = PULSE_LAST_END_A | PULSE_QUAL_VACTIVE |
1929 PULSE_POLARITY_HIGH | PULSE_MODE_NORMAL;
1930 tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_CONTROL);
1931
1932 value = PULSE_END(pulse_start + 8) | PULSE_START(pulse_start);
1933 tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_POSITION_A);
1934
1935 value = tegra_dc_readl(dc, DC_DISP_DISP_SIGNAL_OPTIONS0);
1936 value |= H_PULSE2_ENABLE;
1937 tegra_dc_writel(dc, value, DC_DISP_DISP_SIGNAL_OPTIONS0);
1938
1939 /* infoframe setup */
1940 err = tegra_sor_hdmi_setup_avi_infoframe(sor, mode);
1941 if (err < 0)
1942 dev_err(sor->dev, "failed to setup AVI infoframe: %d\n", err);
1943
1944 /* XXX HDMI audio support not implemented yet */
1945 tegra_sor_hdmi_disable_audio_infoframe(sor);
1946
1947 /* use single TMDS protocol */
1948 value = tegra_sor_readl(sor, SOR_STATE1);
1949 value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
1950 value |= SOR_STATE_ASY_PROTOCOL_SINGLE_TMDS_A;
1951 tegra_sor_writel(sor, value, SOR_STATE1);
1952
1953 /* power up pad calibration */
1954 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
1955 value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
1956 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
1957
1958 /* production settings */
1959 settings = tegra_sor_hdmi_find_settings(sor, mode->clock * 1000);
1960 if (IS_ERR(settings)) {
1961 dev_err(sor->dev, "no settings for pixel clock %d Hz: %ld\n",
1962 mode->clock * 1000, PTR_ERR(settings));
1963 return;
1964 }
1965
1966 value = tegra_sor_readl(sor, SOR_PLL0);
1967 value &= ~SOR_PLL0_ICHPMP_MASK;
1968 value &= ~SOR_PLL0_VCOCAP_MASK;
1969 value |= SOR_PLL0_ICHPMP(settings->ichpmp);
1970 value |= SOR_PLL0_VCOCAP(settings->vcocap);
1971 tegra_sor_writel(sor, value, SOR_PLL0);
1972
1973 tegra_sor_dp_term_calibrate(sor);
1974
1975 value = tegra_sor_readl(sor, SOR_PLL1);
1976 value &= ~SOR_PLL1_LOADADJ_MASK;
1977 value |= SOR_PLL1_LOADADJ(settings->loadadj);
1978 tegra_sor_writel(sor, value, SOR_PLL1);
1979
1980 value = tegra_sor_readl(sor, SOR_PLL3);
1981 value &= ~SOR_PLL3_BG_VREF_LEVEL_MASK;
1982 value |= SOR_PLL3_BG_VREF_LEVEL(settings->bg_vref);
1983 tegra_sor_writel(sor, value, SOR_PLL3);
1984
1985 value = settings->drive_current[0] << 24 |
1986 settings->drive_current[1] << 16 |
1987 settings->drive_current[2] << 8 |
1988 settings->drive_current[3] << 0;
1989 tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT0);
1990
1991 value = settings->preemphasis[0] << 24 |
1992 settings->preemphasis[1] << 16 |
1993 settings->preemphasis[2] << 8 |
1994 settings->preemphasis[3] << 0;
1995 tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS0);
1996
1997 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
1998 value &= ~SOR_DP_PADCTL_TX_PU_MASK;
1999 value |= SOR_DP_PADCTL_TX_PU_ENABLE;
2000 value |= SOR_DP_PADCTL_TX_PU(settings->tx_pu);
2001 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
2002
2003 /* power down pad calibration */
2004 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
2005 value |= SOR_DP_PADCTL_PAD_CAL_PD;
2006 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
2007
2008 /* miscellaneous display controller settings */
2009 value = VSYNC_H_POSITION(1);
2010 tegra_dc_writel(dc, value, DC_DISP_DISP_TIMING_OPTIONS);
2011
2012 value = tegra_dc_readl(dc, DC_DISP_DISP_COLOR_CONTROL);
2013 value &= ~DITHER_CONTROL_MASK;
2014 value &= ~BASE_COLOR_SIZE_MASK;
2015
2016 switch (info->bpc) {
2017 case 6:
2018 value |= BASE_COLOR_SIZE_666;
2019 break;
2020
2021 case 8:
2022 value |= BASE_COLOR_SIZE_888;
2023 break;
2024
2025 default:
2026 WARN(1, "%u bits-per-color not supported\n", info->bpc);
2027 break;
2028 }
2029
2030 tegra_dc_writel(dc, value, DC_DISP_DISP_COLOR_CONTROL);
2031
2032 err = tegra_sor_power_up(sor, 250);
2033 if (err < 0)
2034 dev_err(sor->dev, "failed to power up SOR: %d\n", err);
2035
2036 /* configure mode */
2037 value = tegra_sor_readl(sor, SOR_STATE1);
2038 value &= ~SOR_STATE_ASY_PIXELDEPTH_MASK;
2039 value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
2040 value &= ~SOR_STATE_ASY_OWNER_MASK;
2041
2042 value |= SOR_STATE_ASY_CRC_MODE_COMPLETE |
2043 SOR_STATE_ASY_OWNER(dc->pipe + 1);
2044
2045 if (mode->flags & DRM_MODE_FLAG_PHSYNC)
2046 value &= ~SOR_STATE_ASY_HSYNCPOL;
2047
2048 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
2049 value |= SOR_STATE_ASY_HSYNCPOL;
2050
2051 if (mode->flags & DRM_MODE_FLAG_PVSYNC)
2052 value &= ~SOR_STATE_ASY_VSYNCPOL;
2053
2054 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
2055 value |= SOR_STATE_ASY_VSYNCPOL;
2056
2057 switch (info->bpc) {
2058 case 8:
2059 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
2060 break;
2061
2062 case 6:
2063 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_18_444;
2064 break;
2065
2066 default:
2067 BUG();
2068 break;
2069 }
2070
2071 tegra_sor_writel(sor, value, SOR_STATE1);
2072
2073 value = tegra_sor_readl(sor, SOR_HEAD_STATE0(dc->pipe));
2074 value &= ~SOR_HEAD_STATE_RANGECOMPRESS_MASK;
2075 value &= ~SOR_HEAD_STATE_DYNRANGE_MASK;
2076 tegra_sor_writel(sor, value, SOR_HEAD_STATE0(dc->pipe));
2077
2078 value = tegra_sor_readl(sor, SOR_HEAD_STATE0(dc->pipe));
2079 value &= ~SOR_HEAD_STATE_COLORSPACE_MASK;
2080 value |= SOR_HEAD_STATE_COLORSPACE_RGB;
2081 tegra_sor_writel(sor, value, SOR_HEAD_STATE0(dc->pipe));
2082
2083 /*
2084 * TODO: The video timing programming below doesn't seem to match the
2085 * register definitions.
2086 */
2087
2088 value = ((mode->vtotal & 0x7fff) << 16) | (mode->htotal & 0x7fff);
2089 tegra_sor_writel(sor, value, SOR_HEAD_STATE1(dc->pipe));
2090
2091 /* sync end = sync width - 1 */
2092 vse = mode->vsync_end - mode->vsync_start - 1;
2093 hse = mode->hsync_end - mode->hsync_start - 1;
2094
2095 value = ((vse & 0x7fff) << 16) | (hse & 0x7fff);
2096 tegra_sor_writel(sor, value, SOR_HEAD_STATE2(dc->pipe));
2097
2098 /* blank end = sync end + back porch */
2099 vbe = vse + (mode->vtotal - mode->vsync_end);
2100 hbe = hse + (mode->htotal - mode->hsync_end);
2101
2102 value = ((vbe & 0x7fff) << 16) | (hbe & 0x7fff);
2103 tegra_sor_writel(sor, value, SOR_HEAD_STATE3(dc->pipe));
2104
2105 /* blank start = blank end + active */
2106 vbs = vbe + mode->vdisplay;
2107 hbs = hbe + mode->hdisplay;
2108
2109 value = ((vbs & 0x7fff) << 16) | (hbs & 0x7fff);
2110 tegra_sor_writel(sor, value, SOR_HEAD_STATE4(dc->pipe));
2111
2112 tegra_sor_writel(sor, 0x1, SOR_HEAD_STATE5(dc->pipe));
2113
2114 tegra_sor_update(sor);
2115
2116 err = tegra_sor_attach(sor);
2117 if (err < 0)
2118 dev_err(sor->dev, "failed to attach SOR: %d\n", err);
2119
2120 /* enable display to SOR clock and generate HDMI preamble */
2121 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
2122 value |= SOR1_ENABLE | SOR1_TIMING_CYA;
2123 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
2124
2125 tegra_dc_commit(dc);
2126
2127 err = tegra_sor_wakeup(sor);
2128 if (err < 0)
2129 dev_err(sor->dev, "failed to wakeup SOR: %d\n", err);
2130}
2131
2132static const struct drm_encoder_helper_funcs tegra_sor_hdmi_helpers = {
2133 .disable = tegra_sor_hdmi_disable,
2134 .enable = tegra_sor_hdmi_enable,
2135 .atomic_check = tegra_sor_encoder_atomic_check,
2136};
2137
Thierry Reding6b6b6042013-11-15 16:06:05 +01002138static int tegra_sor_init(struct host1x_client *client)
2139{
Thierry Reding9910f5c2014-05-22 09:57:15 +02002140 struct drm_device *drm = dev_get_drvdata(client->parent);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002141 const struct drm_encoder_helper_funcs *helpers = NULL;
Thierry Reding6b6b6042013-11-15 16:06:05 +01002142 struct tegra_sor *sor = host1x_client_to_sor(client);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002143 int connector = DRM_MODE_CONNECTOR_Unknown;
2144 int encoder = DRM_MODE_ENCODER_NONE;
Thierry Reding6b6b6042013-11-15 16:06:05 +01002145 int err;
2146
Thierry Reding9542c232015-07-08 13:39:09 +02002147 if (!sor->aux) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002148 if (sor->soc->supports_hdmi) {
2149 connector = DRM_MODE_CONNECTOR_HDMIA;
2150 encoder = DRM_MODE_ENCODER_TMDS;
2151 helpers = &tegra_sor_hdmi_helpers;
2152 } else if (sor->soc->supports_lvds) {
2153 connector = DRM_MODE_CONNECTOR_LVDS;
2154 encoder = DRM_MODE_ENCODER_LVDS;
2155 }
2156 } else {
2157 if (sor->soc->supports_edp) {
2158 connector = DRM_MODE_CONNECTOR_eDP;
2159 encoder = DRM_MODE_ENCODER_TMDS;
2160 helpers = &tegra_sor_edp_helpers;
2161 } else if (sor->soc->supports_dp) {
2162 connector = DRM_MODE_CONNECTOR_DisplayPort;
2163 encoder = DRM_MODE_ENCODER_TMDS;
2164 }
2165 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01002166
Thierry Reding6b6b6042013-11-15 16:06:05 +01002167 sor->output.dev = sor->dev;
Thierry Reding6b6b6042013-11-15 16:06:05 +01002168
Thierry Reding6fad8f62014-11-28 15:41:34 +01002169 drm_connector_init(drm, &sor->output.connector,
2170 &tegra_sor_connector_funcs,
Thierry Reding459cc2c2015-07-30 10:34:24 +02002171 connector);
Thierry Reding6fad8f62014-11-28 15:41:34 +01002172 drm_connector_helper_add(&sor->output.connector,
2173 &tegra_sor_connector_helper_funcs);
2174 sor->output.connector.dpms = DRM_MODE_DPMS_OFF;
2175
Thierry Reding6fad8f62014-11-28 15:41:34 +01002176 drm_encoder_init(drm, &sor->output.encoder, &tegra_sor_encoder_funcs,
Thierry Reding459cc2c2015-07-30 10:34:24 +02002177 encoder);
2178 drm_encoder_helper_add(&sor->output.encoder, helpers);
Thierry Reding6fad8f62014-11-28 15:41:34 +01002179
2180 drm_mode_connector_attach_encoder(&sor->output.connector,
2181 &sor->output.encoder);
2182 drm_connector_register(&sor->output.connector);
2183
Thierry Redingea130b22014-12-19 15:51:35 +01002184 err = tegra_output_init(drm, &sor->output);
2185 if (err < 0) {
2186 dev_err(client->dev, "failed to initialize output: %d\n", err);
2187 return err;
2188 }
Thierry Reding6fad8f62014-11-28 15:41:34 +01002189
Thierry Redingea130b22014-12-19 15:51:35 +01002190 sor->output.encoder.possible_crtcs = 0x3;
Thierry Reding6b6b6042013-11-15 16:06:05 +01002191
Thierry Redinga82752e2014-01-31 10:02:15 +01002192 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
Thierry Reding1b0c7b42014-05-28 13:46:12 +02002193 err = tegra_sor_debugfs_init(sor, drm->primary);
Thierry Redinga82752e2014-01-31 10:02:15 +01002194 if (err < 0)
2195 dev_err(sor->dev, "debugfs setup failed: %d\n", err);
2196 }
2197
Thierry Reding9542c232015-07-08 13:39:09 +02002198 if (sor->aux) {
2199 err = drm_dp_aux_attach(sor->aux, &sor->output);
Thierry Reding6b6b6042013-11-15 16:06:05 +01002200 if (err < 0) {
2201 dev_err(sor->dev, "failed to attach DP: %d\n", err);
2202 return err;
2203 }
2204 }
2205
Tomeu Vizoso535a65d2015-03-30 10:33:03 +02002206 /*
2207 * XXX: Remove this reset once proper hand-over from firmware to
2208 * kernel is possible.
2209 */
2210 err = reset_control_assert(sor->rst);
2211 if (err < 0) {
2212 dev_err(sor->dev, "failed to assert SOR reset: %d\n", err);
2213 return err;
2214 }
2215
Thierry Reding6fad8f62014-11-28 15:41:34 +01002216 err = clk_prepare_enable(sor->clk);
2217 if (err < 0) {
2218 dev_err(sor->dev, "failed to enable clock: %d\n", err);
2219 return err;
2220 }
2221
Tomeu Vizoso535a65d2015-03-30 10:33:03 +02002222 usleep_range(1000, 3000);
2223
2224 err = reset_control_deassert(sor->rst);
2225 if (err < 0) {
2226 dev_err(sor->dev, "failed to deassert SOR reset: %d\n", err);
2227 return err;
2228 }
2229
Thierry Reding6fad8f62014-11-28 15:41:34 +01002230 err = clk_prepare_enable(sor->clk_safe);
2231 if (err < 0)
2232 return err;
2233
2234 err = clk_prepare_enable(sor->clk_dp);
2235 if (err < 0)
2236 return err;
2237
Thierry Reding6b6b6042013-11-15 16:06:05 +01002238 return 0;
2239}
2240
2241static int tegra_sor_exit(struct host1x_client *client)
2242{
2243 struct tegra_sor *sor = host1x_client_to_sor(client);
2244 int err;
2245
Thierry Reding328ec692014-12-19 15:55:08 +01002246 tegra_output_exit(&sor->output);
2247
Thierry Reding9542c232015-07-08 13:39:09 +02002248 if (sor->aux) {
2249 err = drm_dp_aux_detach(sor->aux);
Thierry Reding6b6b6042013-11-15 16:06:05 +01002250 if (err < 0) {
2251 dev_err(sor->dev, "failed to detach DP: %d\n", err);
2252 return err;
2253 }
2254 }
2255
Thierry Reding6fad8f62014-11-28 15:41:34 +01002256 clk_disable_unprepare(sor->clk_safe);
2257 clk_disable_unprepare(sor->clk_dp);
2258 clk_disable_unprepare(sor->clk);
2259
Thierry Reding4009c222014-12-19 15:47:30 +01002260 if (IS_ENABLED(CONFIG_DEBUG_FS))
2261 tegra_sor_debugfs_exit(sor);
Thierry Redinga82752e2014-01-31 10:02:15 +01002262
Thierry Reding6b6b6042013-11-15 16:06:05 +01002263 return 0;
2264}
2265
2266static const struct host1x_client_ops sor_client_ops = {
2267 .init = tegra_sor_init,
2268 .exit = tegra_sor_exit,
2269};
2270
Thierry Reding459cc2c2015-07-30 10:34:24 +02002271static const struct tegra_sor_ops tegra_sor_edp_ops = {
2272 .name = "eDP",
2273};
2274
2275static int tegra_sor_hdmi_probe(struct tegra_sor *sor)
2276{
2277 int err;
2278
2279 sor->avdd_io_supply = devm_regulator_get(sor->dev, "avdd-io");
2280 if (IS_ERR(sor->avdd_io_supply)) {
2281 dev_err(sor->dev, "cannot get AVDD I/O supply: %ld\n",
2282 PTR_ERR(sor->avdd_io_supply));
2283 return PTR_ERR(sor->avdd_io_supply);
2284 }
2285
2286 err = regulator_enable(sor->avdd_io_supply);
2287 if (err < 0) {
2288 dev_err(sor->dev, "failed to enable AVDD I/O supply: %d\n",
2289 err);
2290 return err;
2291 }
2292
2293 sor->vdd_pll_supply = devm_regulator_get(sor->dev, "vdd-pll");
2294 if (IS_ERR(sor->vdd_pll_supply)) {
2295 dev_err(sor->dev, "cannot get VDD PLL supply: %ld\n",
2296 PTR_ERR(sor->vdd_pll_supply));
2297 return PTR_ERR(sor->vdd_pll_supply);
2298 }
2299
2300 err = regulator_enable(sor->vdd_pll_supply);
2301 if (err < 0) {
2302 dev_err(sor->dev, "failed to enable VDD PLL supply: %d\n",
2303 err);
2304 return err;
2305 }
2306
2307 sor->hdmi_supply = devm_regulator_get(sor->dev, "hdmi");
2308 if (IS_ERR(sor->hdmi_supply)) {
2309 dev_err(sor->dev, "cannot get HDMI supply: %ld\n",
2310 PTR_ERR(sor->hdmi_supply));
2311 return PTR_ERR(sor->hdmi_supply);
2312 }
2313
2314 err = regulator_enable(sor->hdmi_supply);
2315 if (err < 0) {
2316 dev_err(sor->dev, "failed to enable HDMI supply: %d\n", err);
2317 return err;
2318 }
2319
2320 return 0;
2321}
2322
2323static int tegra_sor_hdmi_remove(struct tegra_sor *sor)
2324{
2325 regulator_disable(sor->hdmi_supply);
2326 regulator_disable(sor->vdd_pll_supply);
2327 regulator_disable(sor->avdd_io_supply);
2328
2329 return 0;
2330}
2331
2332static const struct tegra_sor_ops tegra_sor_hdmi_ops = {
2333 .name = "HDMI",
2334 .probe = tegra_sor_hdmi_probe,
2335 .remove = tegra_sor_hdmi_remove,
2336};
2337
2338static const struct tegra_sor_soc tegra124_sor = {
2339 .supports_edp = true,
2340 .supports_lvds = true,
2341 .supports_hdmi = false,
2342 .supports_dp = false,
2343};
2344
2345static const struct tegra_sor_soc tegra210_sor = {
2346 .supports_edp = true,
2347 .supports_lvds = false,
2348 .supports_hdmi = false,
2349 .supports_dp = false,
2350};
2351
2352static const struct tegra_sor_soc tegra210_sor1 = {
2353 .supports_edp = false,
2354 .supports_lvds = false,
2355 .supports_hdmi = true,
2356 .supports_dp = true,
2357
2358 .num_settings = ARRAY_SIZE(tegra210_sor_hdmi_defaults),
2359 .settings = tegra210_sor_hdmi_defaults,
2360};
2361
2362static const struct of_device_id tegra_sor_of_match[] = {
2363 { .compatible = "nvidia,tegra210-sor1", .data = &tegra210_sor1 },
2364 { .compatible = "nvidia,tegra210-sor", .data = &tegra210_sor },
2365 { .compatible = "nvidia,tegra124-sor", .data = &tegra124_sor },
2366 { },
2367};
2368MODULE_DEVICE_TABLE(of, tegra_sor_of_match);
2369
Thierry Reding6b6b6042013-11-15 16:06:05 +01002370static int tegra_sor_probe(struct platform_device *pdev)
2371{
Thierry Reding459cc2c2015-07-30 10:34:24 +02002372 const struct of_device_id *match;
Thierry Reding6b6b6042013-11-15 16:06:05 +01002373 struct device_node *np;
2374 struct tegra_sor *sor;
2375 struct resource *regs;
2376 int err;
2377
Thierry Reding459cc2c2015-07-30 10:34:24 +02002378 match = of_match_device(tegra_sor_of_match, &pdev->dev);
2379
Thierry Reding6b6b6042013-11-15 16:06:05 +01002380 sor = devm_kzalloc(&pdev->dev, sizeof(*sor), GFP_KERNEL);
2381 if (!sor)
2382 return -ENOMEM;
2383
2384 sor->output.dev = sor->dev = &pdev->dev;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002385 sor->soc = match->data;
2386
2387 sor->settings = devm_kmemdup(&pdev->dev, sor->soc->settings,
2388 sor->soc->num_settings *
2389 sizeof(*sor->settings),
2390 GFP_KERNEL);
2391 if (!sor->settings)
2392 return -ENOMEM;
2393
2394 sor->num_settings = sor->soc->num_settings;
Thierry Reding6b6b6042013-11-15 16:06:05 +01002395
2396 np = of_parse_phandle(pdev->dev.of_node, "nvidia,dpaux", 0);
2397 if (np) {
Thierry Reding9542c232015-07-08 13:39:09 +02002398 sor->aux = drm_dp_aux_find_by_of_node(np);
Thierry Reding6b6b6042013-11-15 16:06:05 +01002399 of_node_put(np);
2400
Thierry Reding9542c232015-07-08 13:39:09 +02002401 if (!sor->aux)
Thierry Reding6b6b6042013-11-15 16:06:05 +01002402 return -EPROBE_DEFER;
2403 }
2404
Thierry Reding9542c232015-07-08 13:39:09 +02002405 if (!sor->aux) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002406 if (sor->soc->supports_hdmi) {
2407 sor->ops = &tegra_sor_hdmi_ops;
2408 } else if (sor->soc->supports_lvds) {
2409 dev_err(&pdev->dev, "LVDS not supported yet\n");
2410 return -ENODEV;
2411 } else {
2412 dev_err(&pdev->dev, "unknown (non-DP) support\n");
2413 return -ENODEV;
2414 }
2415 } else {
2416 if (sor->soc->supports_edp) {
2417 sor->ops = &tegra_sor_edp_ops;
2418 } else if (sor->soc->supports_dp) {
2419 dev_err(&pdev->dev, "DisplayPort not supported yet\n");
2420 return -ENODEV;
2421 } else {
2422 dev_err(&pdev->dev, "unknown (DP) support\n");
2423 return -ENODEV;
2424 }
2425 }
2426
Thierry Reding6b6b6042013-11-15 16:06:05 +01002427 err = tegra_output_probe(&sor->output);
Thierry Reding4dbdc742015-04-27 15:04:26 +02002428 if (err < 0) {
2429 dev_err(&pdev->dev, "failed to probe output: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01002430 return err;
Thierry Reding4dbdc742015-04-27 15:04:26 +02002431 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01002432
Thierry Reding459cc2c2015-07-30 10:34:24 +02002433 if (sor->ops && sor->ops->probe) {
2434 err = sor->ops->probe(sor);
2435 if (err < 0) {
2436 dev_err(&pdev->dev, "failed to probe %s: %d\n",
2437 sor->ops->name, err);
2438 goto output;
2439 }
2440 }
2441
Thierry Reding6b6b6042013-11-15 16:06:05 +01002442 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2443 sor->regs = devm_ioremap_resource(&pdev->dev, regs);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002444 if (IS_ERR(sor->regs)) {
2445 err = PTR_ERR(sor->regs);
2446 goto remove;
2447 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01002448
2449 sor->rst = devm_reset_control_get(&pdev->dev, "sor");
Thierry Reding4dbdc742015-04-27 15:04:26 +02002450 if (IS_ERR(sor->rst)) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002451 err = PTR_ERR(sor->rst);
2452 dev_err(&pdev->dev, "failed to get reset control: %d\n", err);
2453 goto remove;
Thierry Reding4dbdc742015-04-27 15:04:26 +02002454 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01002455
2456 sor->clk = devm_clk_get(&pdev->dev, NULL);
Thierry Reding4dbdc742015-04-27 15:04:26 +02002457 if (IS_ERR(sor->clk)) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002458 err = PTR_ERR(sor->clk);
2459 dev_err(&pdev->dev, "failed to get module clock: %d\n", err);
2460 goto remove;
Thierry Reding4dbdc742015-04-27 15:04:26 +02002461 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01002462
2463 sor->clk_parent = devm_clk_get(&pdev->dev, "parent");
Thierry Reding4dbdc742015-04-27 15:04:26 +02002464 if (IS_ERR(sor->clk_parent)) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002465 err = PTR_ERR(sor->clk_parent);
2466 dev_err(&pdev->dev, "failed to get parent clock: %d\n", err);
2467 goto remove;
Thierry Reding4dbdc742015-04-27 15:04:26 +02002468 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01002469
Thierry Reding6b6b6042013-11-15 16:06:05 +01002470 sor->clk_safe = devm_clk_get(&pdev->dev, "safe");
Thierry Reding4dbdc742015-04-27 15:04:26 +02002471 if (IS_ERR(sor->clk_safe)) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002472 err = PTR_ERR(sor->clk_safe);
2473 dev_err(&pdev->dev, "failed to get safe clock: %d\n", err);
2474 goto remove;
Thierry Reding4dbdc742015-04-27 15:04:26 +02002475 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01002476
Thierry Reding6b6b6042013-11-15 16:06:05 +01002477 sor->clk_dp = devm_clk_get(&pdev->dev, "dp");
Thierry Reding4dbdc742015-04-27 15:04:26 +02002478 if (IS_ERR(sor->clk_dp)) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002479 err = PTR_ERR(sor->clk_dp);
2480 dev_err(&pdev->dev, "failed to get DP clock: %d\n", err);
2481 goto remove;
Thierry Reding4dbdc742015-04-27 15:04:26 +02002482 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01002483
Thierry Reding6b6b6042013-11-15 16:06:05 +01002484 INIT_LIST_HEAD(&sor->client.list);
2485 sor->client.ops = &sor_client_ops;
2486 sor->client.dev = &pdev->dev;
2487
Thierry Reding6b6b6042013-11-15 16:06:05 +01002488 err = host1x_client_register(&sor->client);
2489 if (err < 0) {
2490 dev_err(&pdev->dev, "failed to register host1x client: %d\n",
2491 err);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002492 goto remove;
Thierry Reding6b6b6042013-11-15 16:06:05 +01002493 }
2494
2495 platform_set_drvdata(pdev, sor);
2496
2497 return 0;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002498
2499remove:
2500 if (sor->ops && sor->ops->remove)
2501 sor->ops->remove(sor);
2502output:
2503 tegra_output_remove(&sor->output);
2504 return err;
Thierry Reding6b6b6042013-11-15 16:06:05 +01002505}
2506
2507static int tegra_sor_remove(struct platform_device *pdev)
2508{
2509 struct tegra_sor *sor = platform_get_drvdata(pdev);
2510 int err;
2511
2512 err = host1x_client_unregister(&sor->client);
2513 if (err < 0) {
2514 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
2515 err);
2516 return err;
2517 }
2518
Thierry Reding459cc2c2015-07-30 10:34:24 +02002519 if (sor->ops && sor->ops->remove) {
2520 err = sor->ops->remove(sor);
2521 if (err < 0)
2522 dev_err(&pdev->dev, "failed to remove SOR: %d\n", err);
2523 }
2524
Thierry Reding328ec692014-12-19 15:55:08 +01002525 tegra_output_remove(&sor->output);
Thierry Reding6b6b6042013-11-15 16:06:05 +01002526
2527 return 0;
2528}
2529
Thierry Reding6b6b6042013-11-15 16:06:05 +01002530struct platform_driver tegra_sor_driver = {
2531 .driver = {
2532 .name = "tegra-sor",
2533 .of_match_table = tegra_sor_of_match,
2534 },
2535 .probe = tegra_sor_probe,
2536 .remove = tegra_sor_remove,
2537};