blob: fb536c1f51110d5aedd620c12b514c4a8d8c5648 [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
Thierry Reding25bb2ce2015-08-03 14:23:29 +0200228static int tegra_sor_set_parent_clock(struct tegra_sor *sor, struct clk *parent)
229{
230 int err;
231
232 clk_disable_unprepare(sor->clk);
233
234 err = clk_set_parent(sor->clk, parent);
235 if (err < 0)
236 return err;
237
238 err = clk_prepare_enable(sor->clk);
239 if (err < 0)
240 return err;
241
242 return 0;
243}
244
Thierry Reding6b6b6042013-11-15 16:06:05 +0100245static int tegra_sor_dp_train_fast(struct tegra_sor *sor,
246 struct drm_dp_link *link)
247{
Thierry Reding6b6b6042013-11-15 16:06:05 +0100248 unsigned int i;
249 u8 pattern;
Thierry Reding28fe2072015-01-26 16:02:48 +0100250 u32 value;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100251 int err;
252
253 /* setup lane parameters */
254 value = SOR_LANE_DRIVE_CURRENT_LANE3(0x40) |
255 SOR_LANE_DRIVE_CURRENT_LANE2(0x40) |
256 SOR_LANE_DRIVE_CURRENT_LANE1(0x40) |
257 SOR_LANE_DRIVE_CURRENT_LANE0(0x40);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200258 tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100259
260 value = SOR_LANE_PREEMPHASIS_LANE3(0x0f) |
261 SOR_LANE_PREEMPHASIS_LANE2(0x0f) |
262 SOR_LANE_PREEMPHASIS_LANE1(0x0f) |
263 SOR_LANE_PREEMPHASIS_LANE0(0x0f);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200264 tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100265
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200266 value = SOR_LANE_POSTCURSOR_LANE3(0x00) |
267 SOR_LANE_POSTCURSOR_LANE2(0x00) |
268 SOR_LANE_POSTCURSOR_LANE1(0x00) |
269 SOR_LANE_POSTCURSOR_LANE0(0x00);
270 tegra_sor_writel(sor, value, SOR_LANE_POSTCURSOR0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100271
272 /* disable LVDS mode */
273 tegra_sor_writel(sor, 0, SOR_LVDS);
274
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200275 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100276 value |= SOR_DP_PADCTL_TX_PU_ENABLE;
277 value &= ~SOR_DP_PADCTL_TX_PU_MASK;
278 value |= SOR_DP_PADCTL_TX_PU(2); /* XXX: don't hardcode? */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200279 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100280
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200281 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100282 value |= SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
283 SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200284 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100285
286 usleep_range(10, 100);
287
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200288 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100289 value &= ~(SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
290 SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200291 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100292
Thierry Reding9542c232015-07-08 13:39:09 +0200293 err = drm_dp_aux_prepare(sor->aux, DP_SET_ANSI_8B10B);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100294 if (err < 0)
295 return err;
296
297 for (i = 0, value = 0; i < link->num_lanes; i++) {
298 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
299 SOR_DP_TPG_SCRAMBLER_NONE |
300 SOR_DP_TPG_PATTERN_TRAIN1;
301 value = (value << 8) | lane;
302 }
303
304 tegra_sor_writel(sor, value, SOR_DP_TPG);
305
306 pattern = DP_TRAINING_PATTERN_1;
307
Thierry Reding9542c232015-07-08 13:39:09 +0200308 err = drm_dp_aux_train(sor->aux, link, pattern);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100309 if (err < 0)
310 return err;
311
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200312 value = tegra_sor_readl(sor, SOR_DP_SPARE0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100313 value |= SOR_DP_SPARE_SEQ_ENABLE;
314 value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
315 value |= SOR_DP_SPARE_MACRO_SOR_CLK;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200316 tegra_sor_writel(sor, value, SOR_DP_SPARE0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100317
318 for (i = 0, value = 0; i < link->num_lanes; i++) {
319 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
320 SOR_DP_TPG_SCRAMBLER_NONE |
321 SOR_DP_TPG_PATTERN_TRAIN2;
322 value = (value << 8) | lane;
323 }
324
325 tegra_sor_writel(sor, value, SOR_DP_TPG);
326
327 pattern = DP_LINK_SCRAMBLING_DISABLE | DP_TRAINING_PATTERN_2;
328
Thierry Reding9542c232015-07-08 13:39:09 +0200329 err = drm_dp_aux_train(sor->aux, link, pattern);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100330 if (err < 0)
331 return err;
332
333 for (i = 0, value = 0; i < link->num_lanes; i++) {
334 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
335 SOR_DP_TPG_SCRAMBLER_GALIOS |
336 SOR_DP_TPG_PATTERN_NONE;
337 value = (value << 8) | lane;
338 }
339
340 tegra_sor_writel(sor, value, SOR_DP_TPG);
341
342 pattern = DP_TRAINING_PATTERN_DISABLE;
343
Thierry Reding9542c232015-07-08 13:39:09 +0200344 err = drm_dp_aux_train(sor->aux, link, pattern);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100345 if (err < 0)
346 return err;
347
348 return 0;
349}
350
Thierry Reding459cc2c2015-07-30 10:34:24 +0200351static void tegra_sor_dp_term_calibrate(struct tegra_sor *sor)
352{
353 u32 mask = 0x08, adj = 0, value;
354
355 /* enable pad calibration logic */
356 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
357 value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
358 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
359
360 value = tegra_sor_readl(sor, SOR_PLL1);
361 value |= SOR_PLL1_TMDS_TERM;
362 tegra_sor_writel(sor, value, SOR_PLL1);
363
364 while (mask) {
365 adj |= mask;
366
367 value = tegra_sor_readl(sor, SOR_PLL1);
368 value &= ~SOR_PLL1_TMDS_TERMADJ_MASK;
369 value |= SOR_PLL1_TMDS_TERMADJ(adj);
370 tegra_sor_writel(sor, value, SOR_PLL1);
371
372 usleep_range(100, 200);
373
374 value = tegra_sor_readl(sor, SOR_PLL1);
375 if (value & SOR_PLL1_TERM_COMPOUT)
376 adj &= ~mask;
377
378 mask >>= 1;
379 }
380
381 value = tegra_sor_readl(sor, SOR_PLL1);
382 value &= ~SOR_PLL1_TMDS_TERMADJ_MASK;
383 value |= SOR_PLL1_TMDS_TERMADJ(adj);
384 tegra_sor_writel(sor, value, SOR_PLL1);
385
386 /* disable pad calibration logic */
387 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
388 value |= SOR_DP_PADCTL_PAD_CAL_PD;
389 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
390}
391
Thierry Reding6b6b6042013-11-15 16:06:05 +0100392static void tegra_sor_super_update(struct tegra_sor *sor)
393{
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200394 tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
395 tegra_sor_writel(sor, 1, SOR_SUPER_STATE0);
396 tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100397}
398
399static void tegra_sor_update(struct tegra_sor *sor)
400{
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200401 tegra_sor_writel(sor, 0, SOR_STATE0);
402 tegra_sor_writel(sor, 1, SOR_STATE0);
403 tegra_sor_writel(sor, 0, SOR_STATE0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100404}
405
406static int tegra_sor_setup_pwm(struct tegra_sor *sor, unsigned long timeout)
407{
Thierry Reding28fe2072015-01-26 16:02:48 +0100408 u32 value;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100409
410 value = tegra_sor_readl(sor, SOR_PWM_DIV);
411 value &= ~SOR_PWM_DIV_MASK;
412 value |= 0x400; /* period */
413 tegra_sor_writel(sor, value, SOR_PWM_DIV);
414
415 value = tegra_sor_readl(sor, SOR_PWM_CTL);
416 value &= ~SOR_PWM_CTL_DUTY_CYCLE_MASK;
417 value |= 0x400; /* duty cycle */
418 value &= ~SOR_PWM_CTL_CLK_SEL; /* clock source: PCLK */
419 value |= SOR_PWM_CTL_TRIGGER;
420 tegra_sor_writel(sor, value, SOR_PWM_CTL);
421
422 timeout = jiffies + msecs_to_jiffies(timeout);
423
424 while (time_before(jiffies, timeout)) {
425 value = tegra_sor_readl(sor, SOR_PWM_CTL);
426 if ((value & SOR_PWM_CTL_TRIGGER) == 0)
427 return 0;
428
429 usleep_range(25, 100);
430 }
431
432 return -ETIMEDOUT;
433}
434
435static int tegra_sor_attach(struct tegra_sor *sor)
436{
437 unsigned long value, timeout;
438
439 /* wake up in normal mode */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200440 value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100441 value |= SOR_SUPER_STATE_HEAD_MODE_AWAKE;
442 value |= SOR_SUPER_STATE_MODE_NORMAL;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200443 tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100444 tegra_sor_super_update(sor);
445
446 /* attach */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200447 value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100448 value |= SOR_SUPER_STATE_ATTACHED;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200449 tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100450 tegra_sor_super_update(sor);
451
452 timeout = jiffies + msecs_to_jiffies(250);
453
454 while (time_before(jiffies, timeout)) {
455 value = tegra_sor_readl(sor, SOR_TEST);
456 if ((value & SOR_TEST_ATTACHED) != 0)
457 return 0;
458
459 usleep_range(25, 100);
460 }
461
462 return -ETIMEDOUT;
463}
464
465static int tegra_sor_wakeup(struct tegra_sor *sor)
466{
Thierry Reding6b6b6042013-11-15 16:06:05 +0100467 unsigned long value, timeout;
468
Thierry Reding6b6b6042013-11-15 16:06:05 +0100469 timeout = jiffies + msecs_to_jiffies(250);
470
471 /* wait for head to wake up */
472 while (time_before(jiffies, timeout)) {
473 value = tegra_sor_readl(sor, SOR_TEST);
474 value &= SOR_TEST_HEAD_MODE_MASK;
475
476 if (value == SOR_TEST_HEAD_MODE_AWAKE)
477 return 0;
478
479 usleep_range(25, 100);
480 }
481
482 return -ETIMEDOUT;
483}
484
485static int tegra_sor_power_up(struct tegra_sor *sor, unsigned long timeout)
486{
Thierry Reding28fe2072015-01-26 16:02:48 +0100487 u32 value;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100488
489 value = tegra_sor_readl(sor, SOR_PWR);
490 value |= SOR_PWR_TRIGGER | SOR_PWR_NORMAL_STATE_PU;
491 tegra_sor_writel(sor, value, SOR_PWR);
492
493 timeout = jiffies + msecs_to_jiffies(timeout);
494
495 while (time_before(jiffies, timeout)) {
496 value = tegra_sor_readl(sor, SOR_PWR);
497 if ((value & SOR_PWR_TRIGGER) == 0)
498 return 0;
499
500 usleep_range(25, 100);
501 }
502
503 return -ETIMEDOUT;
504}
505
Thierry Reding34fa1832014-06-05 16:31:10 +0200506struct tegra_sor_params {
507 /* number of link clocks per line */
508 unsigned int num_clocks;
509 /* ratio between input and output */
510 u64 ratio;
511 /* precision factor */
512 u64 precision;
513
514 unsigned int active_polarity;
515 unsigned int active_count;
516 unsigned int active_frac;
517 unsigned int tu_size;
518 unsigned int error;
519};
520
521static int tegra_sor_compute_params(struct tegra_sor *sor,
522 struct tegra_sor_params *params,
523 unsigned int tu_size)
524{
525 u64 active_sym, active_count, frac, approx;
526 u32 active_polarity, active_frac = 0;
527 const u64 f = params->precision;
528 s64 error;
529
530 active_sym = params->ratio * tu_size;
531 active_count = div_u64(active_sym, f) * f;
532 frac = active_sym - active_count;
533
534 /* fraction < 0.5 */
535 if (frac >= (f / 2)) {
536 active_polarity = 1;
537 frac = f - frac;
538 } else {
539 active_polarity = 0;
540 }
541
542 if (frac != 0) {
543 frac = div_u64(f * f, frac); /* 1/fraction */
544 if (frac <= (15 * f)) {
545 active_frac = div_u64(frac, f);
546
547 /* round up */
548 if (active_polarity)
549 active_frac++;
550 } else {
551 active_frac = active_polarity ? 1 : 15;
552 }
553 }
554
555 if (active_frac == 1)
556 active_polarity = 0;
557
558 if (active_polarity == 1) {
559 if (active_frac) {
560 approx = active_count + (active_frac * (f - 1)) * f;
561 approx = div_u64(approx, active_frac * f);
562 } else {
563 approx = active_count + f;
564 }
565 } else {
566 if (active_frac)
567 approx = active_count + div_u64(f, active_frac);
568 else
569 approx = active_count;
570 }
571
572 error = div_s64(active_sym - approx, tu_size);
573 error *= params->num_clocks;
574
Andrew Morton79211c82015-11-09 14:58:13 -0800575 if (error <= 0 && abs(error) < params->error) {
Thierry Reding34fa1832014-06-05 16:31:10 +0200576 params->active_count = div_u64(active_count, f);
577 params->active_polarity = active_polarity;
578 params->active_frac = active_frac;
Andrew Morton79211c82015-11-09 14:58:13 -0800579 params->error = abs(error);
Thierry Reding34fa1832014-06-05 16:31:10 +0200580 params->tu_size = tu_size;
581
582 if (error == 0)
583 return true;
584 }
585
586 return false;
587}
588
Thierry Redinga1983592015-07-21 16:46:52 +0200589static int tegra_sor_compute_config(struct tegra_sor *sor,
590 const struct drm_display_mode *mode,
591 struct tegra_sor_config *config,
592 struct drm_dp_link *link)
Thierry Reding34fa1832014-06-05 16:31:10 +0200593{
594 const u64 f = 100000, link_rate = link->rate * 1000;
595 const u64 pclk = mode->clock * 1000;
Thierry Reding7890b572014-06-05 16:12:46 +0200596 u64 input, output, watermark, num;
Thierry Reding34fa1832014-06-05 16:31:10 +0200597 struct tegra_sor_params params;
Thierry Reding34fa1832014-06-05 16:31:10 +0200598 u32 num_syms_per_line;
599 unsigned int i;
600
601 if (!link_rate || !link->num_lanes || !pclk || !config->bits_per_pixel)
602 return -EINVAL;
603
604 output = link_rate * 8 * link->num_lanes;
605 input = pclk * config->bits_per_pixel;
606
607 if (input >= output)
608 return -ERANGE;
609
610 memset(&params, 0, sizeof(params));
611 params.ratio = div64_u64(input * f, output);
612 params.num_clocks = div_u64(link_rate * mode->hdisplay, pclk);
613 params.precision = f;
614 params.error = 64 * f;
615 params.tu_size = 64;
616
617 for (i = params.tu_size; i >= 32; i--)
618 if (tegra_sor_compute_params(sor, &params, i))
619 break;
620
621 if (params.active_frac == 0) {
622 config->active_polarity = 0;
623 config->active_count = params.active_count;
624
625 if (!params.active_polarity)
626 config->active_count--;
627
628 config->tu_size = params.tu_size;
629 config->active_frac = 1;
630 } else {
631 config->active_polarity = params.active_polarity;
632 config->active_count = params.active_count;
633 config->active_frac = params.active_frac;
634 config->tu_size = params.tu_size;
635 }
636
637 dev_dbg(sor->dev,
638 "polarity: %d active count: %d tu size: %d active frac: %d\n",
639 config->active_polarity, config->active_count,
640 config->tu_size, config->active_frac);
641
642 watermark = params.ratio * config->tu_size * (f - params.ratio);
643 watermark = div_u64(watermark, f);
644
645 watermark = div_u64(watermark + params.error, f);
646 config->watermark = watermark + (config->bits_per_pixel / 8) + 2;
647 num_syms_per_line = (mode->hdisplay * config->bits_per_pixel) *
648 (link->num_lanes * 8);
649
650 if (config->watermark > 30) {
651 config->watermark = 30;
652 dev_err(sor->dev,
653 "unable to compute TU size, forcing watermark to %u\n",
654 config->watermark);
655 } else if (config->watermark > num_syms_per_line) {
656 config->watermark = num_syms_per_line;
657 dev_err(sor->dev, "watermark too high, forcing to %u\n",
658 config->watermark);
659 }
660
Thierry Reding7890b572014-06-05 16:12:46 +0200661 /* compute the number of symbols per horizontal blanking interval */
662 num = ((mode->htotal - mode->hdisplay) - 7) * link_rate;
663 config->hblank_symbols = div_u64(num, pclk);
664
665 if (link->capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
666 config->hblank_symbols -= 3;
667
668 config->hblank_symbols -= 12 / link->num_lanes;
669
670 /* compute the number of symbols per vertical blanking interval */
671 num = (mode->hdisplay - 25) * link_rate;
672 config->vblank_symbols = div_u64(num, pclk);
673 config->vblank_symbols -= 36 / link->num_lanes + 4;
674
675 dev_dbg(sor->dev, "blank symbols: H:%u V:%u\n", config->hblank_symbols,
676 config->vblank_symbols);
677
Thierry Reding34fa1832014-06-05 16:31:10 +0200678 return 0;
679}
680
Thierry Reding6fad8f62014-11-28 15:41:34 +0100681static int tegra_sor_detach(struct tegra_sor *sor)
Thierry Reding6b6b6042013-11-15 16:06:05 +0100682{
Thierry Reding6fad8f62014-11-28 15:41:34 +0100683 unsigned long value, timeout;
684
685 /* switch to safe mode */
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_MODE_NORMAL;
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 timeout = jiffies + msecs_to_jiffies(250);
692
693 while (time_before(jiffies, timeout)) {
694 value = tegra_sor_readl(sor, SOR_PWR);
695 if (value & SOR_PWR_MODE_SAFE)
696 break;
697 }
698
699 if ((value & SOR_PWR_MODE_SAFE) == 0)
700 return -ETIMEDOUT;
701
702 /* go to sleep */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200703 value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100704 value &= ~SOR_SUPER_STATE_HEAD_MODE_MASK;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200705 tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100706 tegra_sor_super_update(sor);
707
708 /* detach */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200709 value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100710 value &= ~SOR_SUPER_STATE_ATTACHED;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200711 tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100712 tegra_sor_super_update(sor);
713
714 timeout = jiffies + msecs_to_jiffies(250);
715
716 while (time_before(jiffies, timeout)) {
717 value = tegra_sor_readl(sor, SOR_TEST);
718 if ((value & SOR_TEST_ATTACHED) == 0)
719 break;
720
721 usleep_range(25, 100);
722 }
723
724 if ((value & SOR_TEST_ATTACHED) != 0)
725 return -ETIMEDOUT;
726
727 return 0;
728}
729
730static int tegra_sor_power_down(struct tegra_sor *sor)
731{
732 unsigned long value, timeout;
733 int err;
734
735 value = tegra_sor_readl(sor, SOR_PWR);
736 value &= ~SOR_PWR_NORMAL_STATE_PU;
737 value |= SOR_PWR_TRIGGER;
738 tegra_sor_writel(sor, value, SOR_PWR);
739
740 timeout = jiffies + msecs_to_jiffies(250);
741
742 while (time_before(jiffies, timeout)) {
743 value = tegra_sor_readl(sor, SOR_PWR);
744 if ((value & SOR_PWR_TRIGGER) == 0)
745 return 0;
746
747 usleep_range(25, 100);
748 }
749
750 if ((value & SOR_PWR_TRIGGER) != 0)
751 return -ETIMEDOUT;
752
Thierry Reding25bb2ce2015-08-03 14:23:29 +0200753 /* switch to safe parent clock */
754 err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100755 if (err < 0)
756 dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
757
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200758 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100759 value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
760 SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200761 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100762
763 /* stop lane sequencer */
764 value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_UP |
765 SOR_LANE_SEQ_CTL_POWER_STATE_DOWN;
766 tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
767
768 timeout = jiffies + msecs_to_jiffies(250);
769
770 while (time_before(jiffies, timeout)) {
771 value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
772 if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
773 break;
774
775 usleep_range(25, 100);
776 }
777
778 if ((value & SOR_LANE_SEQ_CTL_TRIGGER) != 0)
779 return -ETIMEDOUT;
780
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200781 value = tegra_sor_readl(sor, SOR_PLL2);
782 value |= SOR_PLL2_PORT_POWERDOWN;
783 tegra_sor_writel(sor, value, SOR_PLL2);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100784
785 usleep_range(20, 100);
786
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200787 value = tegra_sor_readl(sor, SOR_PLL0);
788 value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
789 tegra_sor_writel(sor, value, SOR_PLL0);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100790
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200791 value = tegra_sor_readl(sor, SOR_PLL2);
792 value |= SOR_PLL2_SEQ_PLLCAPPD;
793 value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
794 tegra_sor_writel(sor, value, SOR_PLL2);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100795
796 usleep_range(20, 100);
797
798 return 0;
799}
800
Thierry Reding6fad8f62014-11-28 15:41:34 +0100801static int tegra_sor_crc_wait(struct tegra_sor *sor, unsigned long timeout)
802{
803 u32 value;
804
805 timeout = jiffies + msecs_to_jiffies(timeout);
806
807 while (time_before(jiffies, timeout)) {
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200808 value = tegra_sor_readl(sor, SOR_CRCA);
809 if (value & SOR_CRCA_VALID)
Thierry Reding6fad8f62014-11-28 15:41:34 +0100810 return 0;
811
812 usleep_range(100, 200);
813 }
814
815 return -ETIMEDOUT;
816}
817
Thierry Reding530239a2015-08-06 11:04:54 +0200818static int tegra_sor_show_crc(struct seq_file *s, void *data)
Thierry Reding6fad8f62014-11-28 15:41:34 +0100819{
Thierry Reding530239a2015-08-06 11:04:54 +0200820 struct drm_info_node *node = s->private;
821 struct tegra_sor *sor = node->info_ent->data;
Thierry Reding850bab42015-07-29 17:58:41 +0200822 struct drm_crtc *crtc = sor->output.encoder.crtc;
823 struct drm_device *drm = node->minor->dev;
Thierry Reding530239a2015-08-06 11:04:54 +0200824 int err = 0;
Thierry Reding6fad8f62014-11-28 15:41:34 +0100825 u32 value;
826
Thierry Reding850bab42015-07-29 17:58:41 +0200827 drm_modeset_lock_all(drm);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100828
Thierry Reding850bab42015-07-29 17:58:41 +0200829 if (!crtc || !crtc->state->active) {
830 err = -EBUSY;
Thierry Reding6fad8f62014-11-28 15:41:34 +0100831 goto unlock;
832 }
833
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200834 value = tegra_sor_readl(sor, SOR_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100835 value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200836 tegra_sor_writel(sor, value, SOR_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100837
838 value = tegra_sor_readl(sor, SOR_CRC_CNTRL);
839 value |= SOR_CRC_CNTRL_ENABLE;
840 tegra_sor_writel(sor, value, SOR_CRC_CNTRL);
841
842 value = tegra_sor_readl(sor, SOR_TEST);
843 value &= ~SOR_TEST_CRC_POST_SERIALIZE;
844 tegra_sor_writel(sor, value, SOR_TEST);
845
846 err = tegra_sor_crc_wait(sor, 100);
847 if (err < 0)
848 goto unlock;
849
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200850 tegra_sor_writel(sor, SOR_CRCA_RESET, SOR_CRCA);
851 value = tegra_sor_readl(sor, SOR_CRCB);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100852
Thierry Reding530239a2015-08-06 11:04:54 +0200853 seq_printf(s, "%08x\n", value);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100854
855unlock:
Thierry Reding850bab42015-07-29 17:58:41 +0200856 drm_modeset_unlock_all(drm);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100857 return err;
858}
859
Thierry Redingdab16332015-01-26 16:04:08 +0100860static int tegra_sor_show_regs(struct seq_file *s, void *data)
861{
862 struct drm_info_node *node = s->private;
863 struct tegra_sor *sor = node->info_ent->data;
Thierry Reding850bab42015-07-29 17:58:41 +0200864 struct drm_crtc *crtc = sor->output.encoder.crtc;
865 struct drm_device *drm = node->minor->dev;
866 int err = 0;
867
868 drm_modeset_lock_all(drm);
869
870 if (!crtc || !crtc->state->active) {
871 err = -EBUSY;
872 goto unlock;
873 }
Thierry Redingdab16332015-01-26 16:04:08 +0100874
875#define DUMP_REG(name) \
876 seq_printf(s, "%-38s %#05x %08x\n", #name, name, \
877 tegra_sor_readl(sor, name))
878
879 DUMP_REG(SOR_CTXSW);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200880 DUMP_REG(SOR_SUPER_STATE0);
881 DUMP_REG(SOR_SUPER_STATE1);
882 DUMP_REG(SOR_STATE0);
883 DUMP_REG(SOR_STATE1);
884 DUMP_REG(SOR_HEAD_STATE0(0));
885 DUMP_REG(SOR_HEAD_STATE0(1));
886 DUMP_REG(SOR_HEAD_STATE1(0));
887 DUMP_REG(SOR_HEAD_STATE1(1));
888 DUMP_REG(SOR_HEAD_STATE2(0));
889 DUMP_REG(SOR_HEAD_STATE2(1));
890 DUMP_REG(SOR_HEAD_STATE3(0));
891 DUMP_REG(SOR_HEAD_STATE3(1));
892 DUMP_REG(SOR_HEAD_STATE4(0));
893 DUMP_REG(SOR_HEAD_STATE4(1));
894 DUMP_REG(SOR_HEAD_STATE5(0));
895 DUMP_REG(SOR_HEAD_STATE5(1));
Thierry Redingdab16332015-01-26 16:04:08 +0100896 DUMP_REG(SOR_CRC_CNTRL);
897 DUMP_REG(SOR_DP_DEBUG_MVID);
898 DUMP_REG(SOR_CLK_CNTRL);
899 DUMP_REG(SOR_CAP);
900 DUMP_REG(SOR_PWR);
901 DUMP_REG(SOR_TEST);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200902 DUMP_REG(SOR_PLL0);
903 DUMP_REG(SOR_PLL1);
904 DUMP_REG(SOR_PLL2);
905 DUMP_REG(SOR_PLL3);
Thierry Redingdab16332015-01-26 16:04:08 +0100906 DUMP_REG(SOR_CSTM);
907 DUMP_REG(SOR_LVDS);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200908 DUMP_REG(SOR_CRCA);
909 DUMP_REG(SOR_CRCB);
Thierry Redingdab16332015-01-26 16:04:08 +0100910 DUMP_REG(SOR_BLANK);
911 DUMP_REG(SOR_SEQ_CTL);
912 DUMP_REG(SOR_LANE_SEQ_CTL);
913 DUMP_REG(SOR_SEQ_INST(0));
914 DUMP_REG(SOR_SEQ_INST(1));
915 DUMP_REG(SOR_SEQ_INST(2));
916 DUMP_REG(SOR_SEQ_INST(3));
917 DUMP_REG(SOR_SEQ_INST(4));
918 DUMP_REG(SOR_SEQ_INST(5));
919 DUMP_REG(SOR_SEQ_INST(6));
920 DUMP_REG(SOR_SEQ_INST(7));
921 DUMP_REG(SOR_SEQ_INST(8));
922 DUMP_REG(SOR_SEQ_INST(9));
923 DUMP_REG(SOR_SEQ_INST(10));
924 DUMP_REG(SOR_SEQ_INST(11));
925 DUMP_REG(SOR_SEQ_INST(12));
926 DUMP_REG(SOR_SEQ_INST(13));
927 DUMP_REG(SOR_SEQ_INST(14));
928 DUMP_REG(SOR_SEQ_INST(15));
929 DUMP_REG(SOR_PWM_DIV);
930 DUMP_REG(SOR_PWM_CTL);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200931 DUMP_REG(SOR_VCRC_A0);
932 DUMP_REG(SOR_VCRC_A1);
933 DUMP_REG(SOR_VCRC_B0);
934 DUMP_REG(SOR_VCRC_B1);
935 DUMP_REG(SOR_CCRC_A0);
936 DUMP_REG(SOR_CCRC_A1);
937 DUMP_REG(SOR_CCRC_B0);
938 DUMP_REG(SOR_CCRC_B1);
939 DUMP_REG(SOR_EDATA_A0);
940 DUMP_REG(SOR_EDATA_A1);
941 DUMP_REG(SOR_EDATA_B0);
942 DUMP_REG(SOR_EDATA_B1);
943 DUMP_REG(SOR_COUNT_A0);
944 DUMP_REG(SOR_COUNT_A1);
945 DUMP_REG(SOR_COUNT_B0);
946 DUMP_REG(SOR_COUNT_B1);
947 DUMP_REG(SOR_DEBUG_A0);
948 DUMP_REG(SOR_DEBUG_A1);
949 DUMP_REG(SOR_DEBUG_B0);
950 DUMP_REG(SOR_DEBUG_B1);
Thierry Redingdab16332015-01-26 16:04:08 +0100951 DUMP_REG(SOR_TRIG);
952 DUMP_REG(SOR_MSCHECK);
953 DUMP_REG(SOR_XBAR_CTRL);
954 DUMP_REG(SOR_XBAR_POL);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200955 DUMP_REG(SOR_DP_LINKCTL0);
956 DUMP_REG(SOR_DP_LINKCTL1);
957 DUMP_REG(SOR_LANE_DRIVE_CURRENT0);
958 DUMP_REG(SOR_LANE_DRIVE_CURRENT1);
959 DUMP_REG(SOR_LANE4_DRIVE_CURRENT0);
960 DUMP_REG(SOR_LANE4_DRIVE_CURRENT1);
961 DUMP_REG(SOR_LANE_PREEMPHASIS0);
962 DUMP_REG(SOR_LANE_PREEMPHASIS1);
963 DUMP_REG(SOR_LANE4_PREEMPHASIS0);
964 DUMP_REG(SOR_LANE4_PREEMPHASIS1);
965 DUMP_REG(SOR_LANE_POSTCURSOR0);
966 DUMP_REG(SOR_LANE_POSTCURSOR1);
967 DUMP_REG(SOR_DP_CONFIG0);
968 DUMP_REG(SOR_DP_CONFIG1);
969 DUMP_REG(SOR_DP_MN0);
970 DUMP_REG(SOR_DP_MN1);
971 DUMP_REG(SOR_DP_PADCTL0);
972 DUMP_REG(SOR_DP_PADCTL1);
973 DUMP_REG(SOR_DP_DEBUG0);
974 DUMP_REG(SOR_DP_DEBUG1);
975 DUMP_REG(SOR_DP_SPARE0);
976 DUMP_REG(SOR_DP_SPARE1);
Thierry Redingdab16332015-01-26 16:04:08 +0100977 DUMP_REG(SOR_DP_AUDIO_CTRL);
978 DUMP_REG(SOR_DP_AUDIO_HBLANK_SYMBOLS);
979 DUMP_REG(SOR_DP_AUDIO_VBLANK_SYMBOLS);
980 DUMP_REG(SOR_DP_GENERIC_INFOFRAME_HEADER);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200981 DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK0);
982 DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK1);
983 DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK2);
984 DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK3);
985 DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK4);
986 DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK5);
987 DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK6);
Thierry Redingdab16332015-01-26 16:04:08 +0100988 DUMP_REG(SOR_DP_TPG);
989 DUMP_REG(SOR_DP_TPG_CONFIG);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200990 DUMP_REG(SOR_DP_LQ_CSTM0);
991 DUMP_REG(SOR_DP_LQ_CSTM1);
992 DUMP_REG(SOR_DP_LQ_CSTM2);
Thierry Redingdab16332015-01-26 16:04:08 +0100993
994#undef DUMP_REG
995
Thierry Reding850bab42015-07-29 17:58:41 +0200996unlock:
997 drm_modeset_unlock_all(drm);
998 return err;
Thierry Redingdab16332015-01-26 16:04:08 +0100999}
1000
1001static const struct drm_info_list debugfs_files[] = {
Thierry Reding530239a2015-08-06 11:04:54 +02001002 { "crc", tegra_sor_show_crc, 0, NULL },
Thierry Redingdab16332015-01-26 16:04:08 +01001003 { "regs", tegra_sor_show_regs, 0, NULL },
1004};
1005
Thierry Reding6fad8f62014-11-28 15:41:34 +01001006static int tegra_sor_debugfs_init(struct tegra_sor *sor,
1007 struct drm_minor *minor)
1008{
Thierry Reding459cc2c2015-07-30 10:34:24 +02001009 const char *name = sor->soc->supports_dp ? "sor1" : "sor";
Thierry Redingdab16332015-01-26 16:04:08 +01001010 unsigned int i;
Thierry Reding530239a2015-08-06 11:04:54 +02001011 int err;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001012
Thierry Reding459cc2c2015-07-30 10:34:24 +02001013 sor->debugfs = debugfs_create_dir(name, minor->debugfs_root);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001014 if (!sor->debugfs)
1015 return -ENOMEM;
1016
Thierry Redingdab16332015-01-26 16:04:08 +01001017 sor->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1018 GFP_KERNEL);
1019 if (!sor->debugfs_files) {
Thierry Reding6fad8f62014-11-28 15:41:34 +01001020 err = -ENOMEM;
1021 goto remove;
1022 }
1023
Thierry Redingdab16332015-01-26 16:04:08 +01001024 for (i = 0; i < ARRAY_SIZE(debugfs_files); i++)
1025 sor->debugfs_files[i].data = sor;
1026
1027 err = drm_debugfs_create_files(sor->debugfs_files,
1028 ARRAY_SIZE(debugfs_files),
1029 sor->debugfs, minor);
1030 if (err < 0)
1031 goto free;
1032
Thierry Reding3ff1f222015-07-03 14:14:29 +02001033 sor->minor = minor;
1034
Thierry Reding530239a2015-08-06 11:04:54 +02001035 return 0;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001036
Thierry Redingdab16332015-01-26 16:04:08 +01001037free:
1038 kfree(sor->debugfs_files);
1039 sor->debugfs_files = NULL;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001040remove:
Thierry Redingdab16332015-01-26 16:04:08 +01001041 debugfs_remove_recursive(sor->debugfs);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001042 sor->debugfs = NULL;
1043 return err;
1044}
1045
Thierry Reding4009c222014-12-19 15:47:30 +01001046static void tegra_sor_debugfs_exit(struct tegra_sor *sor)
Thierry Reding6fad8f62014-11-28 15:41:34 +01001047{
Thierry Redingdab16332015-01-26 16:04:08 +01001048 drm_debugfs_remove_files(sor->debugfs_files, ARRAY_SIZE(debugfs_files),
1049 sor->minor);
1050 sor->minor = NULL;
1051
1052 kfree(sor->debugfs_files);
Thierry Reding066d30f2015-07-03 14:16:30 +02001053 sor->debugfs_files = NULL;
Thierry Redingdab16332015-01-26 16:04:08 +01001054
1055 debugfs_remove_recursive(sor->debugfs);
Thierry Reding066d30f2015-07-03 14:16:30 +02001056 sor->debugfs = NULL;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001057}
1058
Thierry Reding6fad8f62014-11-28 15:41:34 +01001059static enum drm_connector_status
1060tegra_sor_connector_detect(struct drm_connector *connector, bool force)
1061{
1062 struct tegra_output *output = connector_to_output(connector);
1063 struct tegra_sor *sor = to_sor(output);
1064
Thierry Reding9542c232015-07-08 13:39:09 +02001065 if (sor->aux)
1066 return drm_dp_aux_detect(sor->aux);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001067
Thierry Reding459cc2c2015-07-30 10:34:24 +02001068 return tegra_output_connector_detect(connector, force);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001069}
1070
1071static const struct drm_connector_funcs tegra_sor_connector_funcs = {
Thierry Reding850bab42015-07-29 17:58:41 +02001072 .dpms = drm_atomic_helper_connector_dpms,
Thierry Reding9d441892014-11-24 17:02:53 +01001073 .reset = drm_atomic_helper_connector_reset,
Thierry Reding6fad8f62014-11-28 15:41:34 +01001074 .detect = tegra_sor_connector_detect,
1075 .fill_modes = drm_helper_probe_single_connector_modes,
1076 .destroy = tegra_output_connector_destroy,
Thierry Reding9d441892014-11-24 17:02:53 +01001077 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
Thierry Reding4aa3df72014-11-24 16:27:13 +01001078 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
Thierry Reding6fad8f62014-11-28 15:41:34 +01001079};
1080
1081static int tegra_sor_connector_get_modes(struct drm_connector *connector)
1082{
1083 struct tegra_output *output = connector_to_output(connector);
1084 struct tegra_sor *sor = to_sor(output);
1085 int err;
1086
Thierry Reding9542c232015-07-08 13:39:09 +02001087 if (sor->aux)
1088 drm_dp_aux_enable(sor->aux);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001089
1090 err = tegra_output_connector_get_modes(connector);
1091
Thierry Reding9542c232015-07-08 13:39:09 +02001092 if (sor->aux)
1093 drm_dp_aux_disable(sor->aux);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001094
1095 return err;
1096}
1097
1098static enum drm_mode_status
1099tegra_sor_connector_mode_valid(struct drm_connector *connector,
1100 struct drm_display_mode *mode)
1101{
1102 return MODE_OK;
1103}
1104
1105static const struct drm_connector_helper_funcs tegra_sor_connector_helper_funcs = {
1106 .get_modes = tegra_sor_connector_get_modes,
1107 .mode_valid = tegra_sor_connector_mode_valid,
1108 .best_encoder = tegra_output_connector_best_encoder,
1109};
1110
1111static const struct drm_encoder_funcs tegra_sor_encoder_funcs = {
1112 .destroy = tegra_output_encoder_destroy,
1113};
1114
Thierry Reding850bab42015-07-29 17:58:41 +02001115static void tegra_sor_edp_disable(struct drm_encoder *encoder)
Thierry Reding6fad8f62014-11-28 15:41:34 +01001116{
Thierry Reding850bab42015-07-29 17:58:41 +02001117 struct tegra_output *output = encoder_to_output(encoder);
1118 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
1119 struct tegra_sor *sor = to_sor(output);
1120 u32 value;
1121 int err;
1122
1123 if (output->panel)
1124 drm_panel_disable(output->panel);
1125
1126 err = tegra_sor_detach(sor);
1127 if (err < 0)
1128 dev_err(sor->dev, "failed to detach SOR: %d\n", err);
1129
1130 tegra_sor_writel(sor, 0, SOR_STATE1);
1131 tegra_sor_update(sor);
1132
1133 /*
1134 * The following accesses registers of the display controller, so make
1135 * sure it's only executed when the output is attached to one.
1136 */
1137 if (dc) {
1138 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
1139 value &= ~SOR_ENABLE;
1140 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
1141
1142 tegra_dc_commit(dc);
1143 }
1144
1145 err = tegra_sor_power_down(sor);
1146 if (err < 0)
1147 dev_err(sor->dev, "failed to power down SOR: %d\n", err);
1148
Thierry Reding9542c232015-07-08 13:39:09 +02001149 if (sor->aux) {
1150 err = drm_dp_aux_disable(sor->aux);
Thierry Reding850bab42015-07-29 17:58:41 +02001151 if (err < 0)
1152 dev_err(sor->dev, "failed to disable DP: %d\n", err);
1153 }
1154
1155 err = tegra_io_rail_power_off(TEGRA_IO_RAIL_LVDS);
1156 if (err < 0)
1157 dev_err(sor->dev, "failed to power off I/O rail: %d\n", err);
1158
1159 if (output->panel)
1160 drm_panel_unprepare(output->panel);
1161
1162 reset_control_assert(sor->rst);
1163 clk_disable_unprepare(sor->clk);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001164}
1165
Thierry Reding459cc2c2015-07-30 10:34:24 +02001166#if 0
1167static int calc_h_ref_to_sync(const struct drm_display_mode *mode,
1168 unsigned int *value)
1169{
1170 unsigned int hfp, hsw, hbp, a = 0, b;
1171
1172 hfp = mode->hsync_start - mode->hdisplay;
1173 hsw = mode->hsync_end - mode->hsync_start;
1174 hbp = mode->htotal - mode->hsync_end;
1175
1176 pr_info("hfp: %u, hsw: %u, hbp: %u\n", hfp, hsw, hbp);
1177
1178 b = hfp - 1;
1179
1180 pr_info("a: %u, b: %u\n", a, b);
1181 pr_info("a + hsw + hbp = %u\n", a + hsw + hbp);
1182
1183 if (a + hsw + hbp <= 11) {
1184 a = 1 + 11 - hsw - hbp;
1185 pr_info("a: %u\n", a);
1186 }
1187
1188 if (a > b)
1189 return -EINVAL;
1190
1191 if (hsw < 1)
1192 return -EINVAL;
1193
1194 if (mode->hdisplay < 16)
1195 return -EINVAL;
1196
1197 if (value) {
1198 if (b > a && a % 2)
1199 *value = a + 1;
1200 else
1201 *value = a;
1202 }
1203
1204 return 0;
1205}
1206#endif
1207
Thierry Reding850bab42015-07-29 17:58:41 +02001208static void tegra_sor_edp_enable(struct drm_encoder *encoder)
Thierry Reding6fad8f62014-11-28 15:41:34 +01001209{
Thierry Reding850bab42015-07-29 17:58:41 +02001210 struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001211 struct tegra_output *output = encoder_to_output(encoder);
1212 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001213 unsigned int vbe, vse, hbe, hse, vbs, hbs, i;
1214 struct tegra_sor *sor = to_sor(output);
Thierry Reding34fa1832014-06-05 16:31:10 +02001215 struct tegra_sor_config config;
1216 struct drm_dp_link link;
Thierry Reding01b9bea2015-11-11 17:15:29 +01001217 u8 rate, lanes;
Thierry Reding86f5c522014-03-26 11:13:16 +01001218 int err = 0;
Thierry Reding28fe2072015-01-26 16:02:48 +01001219 u32 value;
Thierry Reding86f5c522014-03-26 11:13:16 +01001220
Thierry Reding6b6b6042013-11-15 16:06:05 +01001221 err = clk_prepare_enable(sor->clk);
1222 if (err < 0)
Thierry Reding850bab42015-07-29 17:58:41 +02001223 dev_err(sor->dev, "failed to enable clock: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001224
1225 reset_control_deassert(sor->rst);
1226
Thierry Reding6fad8f62014-11-28 15:41:34 +01001227 if (output->panel)
1228 drm_panel_prepare(output->panel);
1229
Thierry Reding01b9bea2015-11-11 17:15:29 +01001230 err = drm_dp_aux_enable(sor->aux);
1231 if (err < 0)
1232 dev_err(sor->dev, "failed to enable DP: %d\n", err);
Thierry Reding34fa1832014-06-05 16:31:10 +02001233
Thierry Reding01b9bea2015-11-11 17:15:29 +01001234 err = drm_dp_link_probe(sor->aux, &link);
1235 if (err < 0) {
1236 dev_err(sor->dev, "failed to probe eDP link: %d\n", err);
1237 return;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001238 }
1239
Thierry Reding25bb2ce2015-08-03 14:23:29 +02001240 /* switch to safe parent clock */
1241 err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001242 if (err < 0)
1243 dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
1244
Thierry Reding34fa1832014-06-05 16:31:10 +02001245 memset(&config, 0, sizeof(config));
Stéphane Marchesin054b1bd2014-06-19 18:18:29 -07001246 config.bits_per_pixel = output->connector.display_info.bpc * 3;
Thierry Reding34fa1832014-06-05 16:31:10 +02001247
Thierry Redinga1983592015-07-21 16:46:52 +02001248 err = tegra_sor_compute_config(sor, mode, &config, &link);
Thierry Reding34fa1832014-06-05 16:31:10 +02001249 if (err < 0)
Thierry Redinga1983592015-07-21 16:46:52 +02001250 dev_err(sor->dev, "failed to compute configuration: %d\n", err);
Thierry Reding34fa1832014-06-05 16:31:10 +02001251
Thierry Reding6b6b6042013-11-15 16:06:05 +01001252 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
1253 value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
1254 value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
1255 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
1256
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001257 value = tegra_sor_readl(sor, SOR_PLL2);
1258 value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
1259 tegra_sor_writel(sor, value, SOR_PLL2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001260 usleep_range(20, 100);
1261
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001262 value = tegra_sor_readl(sor, SOR_PLL3);
1263 value |= SOR_PLL3_PLL_VDD_MODE_3V3;
1264 tegra_sor_writel(sor, value, SOR_PLL3);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001265
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001266 value = SOR_PLL0_ICHPMP(0xf) | SOR_PLL0_VCOCAP_RST |
1267 SOR_PLL0_PLLREG_LEVEL_V45 | SOR_PLL0_RESISTOR_EXT;
1268 tegra_sor_writel(sor, value, SOR_PLL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001269
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001270 value = tegra_sor_readl(sor, SOR_PLL2);
1271 value |= SOR_PLL2_SEQ_PLLCAPPD;
1272 value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1273 value |= SOR_PLL2_LVDS_ENABLE;
1274 tegra_sor_writel(sor, value, SOR_PLL2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001275
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001276 value = SOR_PLL1_TERM_COMPOUT | SOR_PLL1_TMDS_TERM;
1277 tegra_sor_writel(sor, value, SOR_PLL1);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001278
1279 while (true) {
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001280 value = tegra_sor_readl(sor, SOR_PLL2);
1281 if ((value & SOR_PLL2_SEQ_PLLCAPPD_ENFORCE) == 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001282 break;
1283
1284 usleep_range(250, 1000);
1285 }
1286
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001287 value = tegra_sor_readl(sor, SOR_PLL2);
1288 value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
1289 value &= ~SOR_PLL2_PORT_POWERDOWN;
1290 tegra_sor_writel(sor, value, SOR_PLL2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001291
1292 /*
1293 * power up
1294 */
1295
1296 /* set safe link bandwidth (1.62 Gbps) */
1297 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
1298 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
1299 value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G1_62;
1300 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
1301
1302 /* step 1 */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001303 value = tegra_sor_readl(sor, SOR_PLL2);
1304 value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE | SOR_PLL2_PORT_POWERDOWN |
1305 SOR_PLL2_BANDGAP_POWERDOWN;
1306 tegra_sor_writel(sor, value, SOR_PLL2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001307
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001308 value = tegra_sor_readl(sor, SOR_PLL0);
1309 value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
1310 tegra_sor_writel(sor, value, SOR_PLL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001311
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001312 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001313 value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001314 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001315
1316 /* step 2 */
1317 err = tegra_io_rail_power_on(TEGRA_IO_RAIL_LVDS);
Thierry Reding850bab42015-07-29 17:58:41 +02001318 if (err < 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001319 dev_err(sor->dev, "failed to power on I/O rail: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001320
1321 usleep_range(5, 100);
1322
1323 /* step 3 */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001324 value = tegra_sor_readl(sor, SOR_PLL2);
1325 value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
1326 tegra_sor_writel(sor, value, SOR_PLL2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001327
1328 usleep_range(20, 100);
1329
1330 /* step 4 */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001331 value = tegra_sor_readl(sor, SOR_PLL0);
1332 value &= ~SOR_PLL0_VCOPD;
1333 value &= ~SOR_PLL0_PWR;
1334 tegra_sor_writel(sor, value, SOR_PLL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001335
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001336 value = tegra_sor_readl(sor, SOR_PLL2);
1337 value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1338 tegra_sor_writel(sor, value, SOR_PLL2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001339
1340 usleep_range(200, 1000);
1341
1342 /* step 5 */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001343 value = tegra_sor_readl(sor, SOR_PLL2);
1344 value &= ~SOR_PLL2_PORT_POWERDOWN;
1345 tegra_sor_writel(sor, value, SOR_PLL2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001346
Thierry Reding25bb2ce2015-08-03 14:23:29 +02001347 /* switch to DP parent clock */
1348 err = tegra_sor_set_parent_clock(sor, sor->clk_dp);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001349 if (err < 0)
Thierry Reding25bb2ce2015-08-03 14:23:29 +02001350 dev_err(sor->dev, "failed to set parent clock: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001351
Thierry Reding899451b2014-06-05 16:19:48 +02001352 /* power DP lanes */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001353 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
Thierry Reding899451b2014-06-05 16:19:48 +02001354
1355 if (link.num_lanes <= 2)
1356 value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2);
1357 else
1358 value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2;
1359
1360 if (link.num_lanes <= 1)
1361 value &= ~SOR_DP_PADCTL_PD_TXD_1;
1362 else
1363 value |= SOR_DP_PADCTL_PD_TXD_1;
1364
1365 if (link.num_lanes == 0)
1366 value &= ~SOR_DP_PADCTL_PD_TXD_0;
1367 else
1368 value |= SOR_DP_PADCTL_PD_TXD_0;
1369
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001370 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001371
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001372 value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001373 value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
Thierry Reding0c90a182014-06-05 16:29:46 +02001374 value |= SOR_DP_LINKCTL_LANE_COUNT(link.num_lanes);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001375 tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001376
1377 /* start lane sequencer */
1378 value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
1379 SOR_LANE_SEQ_CTL_POWER_STATE_UP;
1380 tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
1381
1382 while (true) {
1383 value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
1384 if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
1385 break;
1386
1387 usleep_range(250, 1000);
1388 }
1389
Thierry Redinga4263fe2014-06-05 16:16:23 +02001390 /* set link bandwidth */
Thierry Reding6b6b6042013-11-15 16:06:05 +01001391 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
1392 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
Thierry Redinga4263fe2014-06-05 16:16:23 +02001393 value |= drm_dp_link_rate_to_bw_code(link.rate) << 2;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001394 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
1395
1396 /* set linkctl */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001397 value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001398 value |= SOR_DP_LINKCTL_ENABLE;
1399
1400 value &= ~SOR_DP_LINKCTL_TU_SIZE_MASK;
Thierry Reding34fa1832014-06-05 16:31:10 +02001401 value |= SOR_DP_LINKCTL_TU_SIZE(config.tu_size);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001402
1403 value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001404 tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001405
1406 for (i = 0, value = 0; i < 4; i++) {
1407 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
1408 SOR_DP_TPG_SCRAMBLER_GALIOS |
1409 SOR_DP_TPG_PATTERN_NONE;
1410 value = (value << 8) | lane;
1411 }
1412
1413 tegra_sor_writel(sor, value, SOR_DP_TPG);
1414
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001415 value = tegra_sor_readl(sor, SOR_DP_CONFIG0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001416 value &= ~SOR_DP_CONFIG_WATERMARK_MASK;
Thierry Reding34fa1832014-06-05 16:31:10 +02001417 value |= SOR_DP_CONFIG_WATERMARK(config.watermark);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001418
1419 value &= ~SOR_DP_CONFIG_ACTIVE_SYM_COUNT_MASK;
Thierry Reding34fa1832014-06-05 16:31:10 +02001420 value |= SOR_DP_CONFIG_ACTIVE_SYM_COUNT(config.active_count);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001421
1422 value &= ~SOR_DP_CONFIG_ACTIVE_SYM_FRAC_MASK;
Thierry Reding34fa1832014-06-05 16:31:10 +02001423 value |= SOR_DP_CONFIG_ACTIVE_SYM_FRAC(config.active_frac);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001424
Thierry Reding34fa1832014-06-05 16:31:10 +02001425 if (config.active_polarity)
1426 value |= SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
1427 else
1428 value &= ~SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001429
1430 value |= SOR_DP_CONFIG_ACTIVE_SYM_ENABLE;
Thierry Reding1f64ae72014-06-05 16:20:27 +02001431 value |= SOR_DP_CONFIG_DISPARITY_NEGATIVE;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001432 tegra_sor_writel(sor, value, SOR_DP_CONFIG0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001433
1434 value = tegra_sor_readl(sor, SOR_DP_AUDIO_HBLANK_SYMBOLS);
1435 value &= ~SOR_DP_AUDIO_HBLANK_SYMBOLS_MASK;
Thierry Reding7890b572014-06-05 16:12:46 +02001436 value |= config.hblank_symbols & 0xffff;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001437 tegra_sor_writel(sor, value, SOR_DP_AUDIO_HBLANK_SYMBOLS);
1438
1439 value = tegra_sor_readl(sor, SOR_DP_AUDIO_VBLANK_SYMBOLS);
1440 value &= ~SOR_DP_AUDIO_VBLANK_SYMBOLS_MASK;
Thierry Reding7890b572014-06-05 16:12:46 +02001441 value |= config.vblank_symbols & 0xffff;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001442 tegra_sor_writel(sor, value, SOR_DP_AUDIO_VBLANK_SYMBOLS);
1443
1444 /* enable pad calibration logic */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001445 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001446 value |= SOR_DP_PADCTL_PAD_CAL_PD;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001447 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001448
Thierry Reding01b9bea2015-11-11 17:15:29 +01001449 err = drm_dp_link_probe(sor->aux, &link);
1450 if (err < 0)
1451 dev_err(sor->dev, "failed to probe eDP link: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001452
Thierry Reding01b9bea2015-11-11 17:15:29 +01001453 err = drm_dp_link_power_up(sor->aux, &link);
1454 if (err < 0)
1455 dev_err(sor->dev, "failed to power up eDP link: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001456
Thierry Reding01b9bea2015-11-11 17:15:29 +01001457 err = drm_dp_link_configure(sor->aux, &link);
1458 if (err < 0)
1459 dev_err(sor->dev, "failed to configure eDP link: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001460
Thierry Reding01b9bea2015-11-11 17:15:29 +01001461 rate = drm_dp_link_rate_to_bw_code(link.rate);
1462 lanes = link.num_lanes;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001463
Thierry Reding01b9bea2015-11-11 17:15:29 +01001464 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
1465 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
1466 value |= SOR_CLK_CNTRL_DP_LINK_SPEED(rate);
1467 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001468
Thierry Reding01b9bea2015-11-11 17:15:29 +01001469 value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
1470 value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
1471 value |= SOR_DP_LINKCTL_LANE_COUNT(lanes);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001472
Thierry Reding01b9bea2015-11-11 17:15:29 +01001473 if (link.capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
1474 value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001475
Thierry Reding01b9bea2015-11-11 17:15:29 +01001476 tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001477
Thierry Reding01b9bea2015-11-11 17:15:29 +01001478 /* disable training pattern generator */
Thierry Reding6b6b6042013-11-15 16:06:05 +01001479
Thierry Reding01b9bea2015-11-11 17:15:29 +01001480 for (i = 0; i < link.num_lanes; i++) {
1481 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
1482 SOR_DP_TPG_SCRAMBLER_GALIOS |
1483 SOR_DP_TPG_PATTERN_NONE;
1484 value = (value << 8) | lane;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001485 }
1486
Thierry Reding01b9bea2015-11-11 17:15:29 +01001487 tegra_sor_writel(sor, value, SOR_DP_TPG);
1488
1489 err = tegra_sor_dp_train_fast(sor, &link);
1490 if (err < 0)
1491 dev_err(sor->dev, "DP fast link training failed: %d\n", err);
1492
1493 dev_dbg(sor->dev, "fast link training succeeded\n");
1494
Thierry Reding6b6b6042013-11-15 16:06:05 +01001495 err = tegra_sor_power_up(sor, 250);
Thierry Reding850bab42015-07-29 17:58:41 +02001496 if (err < 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001497 dev_err(sor->dev, "failed to power up SOR: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001498
Thierry Reding6b6b6042013-11-15 16:06:05 +01001499 /*
1500 * configure panel (24bpp, vsync-, hsync-, DP-A protocol, complete
1501 * raster, associate with display controller)
1502 */
Thierry Reding3f4f3b52014-07-07 15:35:06 +02001503 value = SOR_STATE_ASY_PROTOCOL_DP_A |
Thierry Reding6b6b6042013-11-15 16:06:05 +01001504 SOR_STATE_ASY_CRC_MODE_COMPLETE |
1505 SOR_STATE_ASY_OWNER(dc->pipe + 1);
Thierry Reding34fa1832014-06-05 16:31:10 +02001506
Thierry Reding3f4f3b52014-07-07 15:35:06 +02001507 if (mode->flags & DRM_MODE_FLAG_PHSYNC)
1508 value &= ~SOR_STATE_ASY_HSYNCPOL;
1509
1510 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1511 value |= SOR_STATE_ASY_HSYNCPOL;
1512
1513 if (mode->flags & DRM_MODE_FLAG_PVSYNC)
1514 value &= ~SOR_STATE_ASY_VSYNCPOL;
1515
1516 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1517 value |= SOR_STATE_ASY_VSYNCPOL;
1518
Thierry Reding34fa1832014-06-05 16:31:10 +02001519 switch (config.bits_per_pixel) {
1520 case 24:
1521 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
1522 break;
1523
1524 case 18:
1525 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_18_444;
1526 break;
1527
1528 default:
1529 BUG();
1530 break;
1531 }
1532
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001533 tegra_sor_writel(sor, value, SOR_STATE1);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001534
1535 /*
1536 * TODO: The video timing programming below doesn't seem to match the
1537 * register definitions.
1538 */
1539
1540 value = ((mode->vtotal & 0x7fff) << 16) | (mode->htotal & 0x7fff);
Thierry Reding51511d02015-07-30 18:47:07 +02001541 tegra_sor_writel(sor, value, SOR_HEAD_STATE1(dc->pipe));
Thierry Reding6b6b6042013-11-15 16:06:05 +01001542
1543 vse = mode->vsync_end - mode->vsync_start - 1;
1544 hse = mode->hsync_end - mode->hsync_start - 1;
1545
1546 value = ((vse & 0x7fff) << 16) | (hse & 0x7fff);
Thierry Reding51511d02015-07-30 18:47:07 +02001547 tegra_sor_writel(sor, value, SOR_HEAD_STATE2(dc->pipe));
Thierry Reding6b6b6042013-11-15 16:06:05 +01001548
1549 vbe = vse + (mode->vsync_start - mode->vdisplay);
1550 hbe = hse + (mode->hsync_start - mode->hdisplay);
1551
1552 value = ((vbe & 0x7fff) << 16) | (hbe & 0x7fff);
Thierry Reding51511d02015-07-30 18:47:07 +02001553 tegra_sor_writel(sor, value, SOR_HEAD_STATE3(dc->pipe));
Thierry Reding6b6b6042013-11-15 16:06:05 +01001554
1555 vbs = vbe + mode->vdisplay;
1556 hbs = hbe + mode->hdisplay;
1557
1558 value = ((vbs & 0x7fff) << 16) | (hbs & 0x7fff);
Thierry Reding51511d02015-07-30 18:47:07 +02001559 tegra_sor_writel(sor, value, SOR_HEAD_STATE4(dc->pipe));
1560
1561 tegra_sor_writel(sor, 0x1, SOR_HEAD_STATE5(dc->pipe));
Thierry Reding6b6b6042013-11-15 16:06:05 +01001562
Thierry Reding6b6b6042013-11-15 16:06:05 +01001563 /* CSTM (LVDS, link A/B, upper) */
Stéphane Marchesin143b1df2014-05-22 20:32:47 -07001564 value = SOR_CSTM_LVDS | SOR_CSTM_LINK_ACT_A | SOR_CSTM_LINK_ACT_B |
Thierry Reding6b6b6042013-11-15 16:06:05 +01001565 SOR_CSTM_UPPER;
1566 tegra_sor_writel(sor, value, SOR_CSTM);
1567
1568 /* PWM setup */
1569 err = tegra_sor_setup_pwm(sor, 250);
Thierry Reding850bab42015-07-29 17:58:41 +02001570 if (err < 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001571 dev_err(sor->dev, "failed to setup PWM: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001572
Thierry Reding666cb872014-12-08 16:32:47 +01001573 tegra_sor_update(sor);
1574
Thierry Reding6b6b6042013-11-15 16:06:05 +01001575 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
1576 value |= SOR_ENABLE;
1577 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
1578
Thierry Reding666cb872014-12-08 16:32:47 +01001579 tegra_dc_commit(dc);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001580
1581 err = tegra_sor_attach(sor);
Thierry Reding850bab42015-07-29 17:58:41 +02001582 if (err < 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001583 dev_err(sor->dev, "failed to attach SOR: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001584
1585 err = tegra_sor_wakeup(sor);
Thierry Reding850bab42015-07-29 17:58:41 +02001586 if (err < 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001587 dev_err(sor->dev, "failed to enable DC: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001588
Thierry Reding6fad8f62014-11-28 15:41:34 +01001589 if (output->panel)
1590 drm_panel_enable(output->panel);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001591}
1592
Thierry Reding82f15112014-12-08 17:26:46 +01001593static int
1594tegra_sor_encoder_atomic_check(struct drm_encoder *encoder,
1595 struct drm_crtc_state *crtc_state,
1596 struct drm_connector_state *conn_state)
1597{
1598 struct tegra_output *output = encoder_to_output(encoder);
1599 struct tegra_dc *dc = to_tegra_dc(conn_state->crtc);
1600 unsigned long pclk = crtc_state->mode.clock * 1000;
1601 struct tegra_sor *sor = to_sor(output);
1602 int err;
1603
1604 err = tegra_dc_state_setup_clock(dc, crtc_state, sor->clk_parent,
1605 pclk, 0);
1606 if (err < 0) {
1607 dev_err(output->dev, "failed to setup CRTC state: %d\n", err);
1608 return err;
1609 }
1610
1611 return 0;
1612}
1613
Thierry Reding459cc2c2015-07-30 10:34:24 +02001614static const struct drm_encoder_helper_funcs tegra_sor_edp_helpers = {
Thierry Reding850bab42015-07-29 17:58:41 +02001615 .disable = tegra_sor_edp_disable,
1616 .enable = tegra_sor_edp_enable,
Thierry Reding82f15112014-12-08 17:26:46 +01001617 .atomic_check = tegra_sor_encoder_atomic_check,
Thierry Reding6b6b6042013-11-15 16:06:05 +01001618};
1619
Thierry Reding459cc2c2015-07-30 10:34:24 +02001620static inline u32 tegra_sor_hdmi_subpack(const u8 *ptr, size_t size)
1621{
1622 u32 value = 0;
1623 size_t i;
1624
1625 for (i = size; i > 0; i--)
1626 value = (value << 8) | ptr[i - 1];
1627
1628 return value;
1629}
1630
1631static void tegra_sor_hdmi_write_infopack(struct tegra_sor *sor,
1632 const void *data, size_t size)
1633{
1634 const u8 *ptr = data;
1635 unsigned long offset;
1636 size_t i, j;
1637 u32 value;
1638
1639 switch (ptr[0]) {
1640 case HDMI_INFOFRAME_TYPE_AVI:
1641 offset = SOR_HDMI_AVI_INFOFRAME_HEADER;
1642 break;
1643
1644 case HDMI_INFOFRAME_TYPE_AUDIO:
1645 offset = SOR_HDMI_AUDIO_INFOFRAME_HEADER;
1646 break;
1647
1648 case HDMI_INFOFRAME_TYPE_VENDOR:
1649 offset = SOR_HDMI_VSI_INFOFRAME_HEADER;
1650 break;
1651
1652 default:
1653 dev_err(sor->dev, "unsupported infoframe type: %02x\n",
1654 ptr[0]);
1655 return;
1656 }
1657
1658 value = INFOFRAME_HEADER_TYPE(ptr[0]) |
1659 INFOFRAME_HEADER_VERSION(ptr[1]) |
1660 INFOFRAME_HEADER_LEN(ptr[2]);
1661 tegra_sor_writel(sor, value, offset);
1662 offset++;
1663
1664 /*
1665 * Each subpack contains 7 bytes, divided into:
1666 * - subpack_low: bytes 0 - 3
1667 * - subpack_high: bytes 4 - 6 (with byte 7 padded to 0x00)
1668 */
1669 for (i = 3, j = 0; i < size; i += 7, j += 8) {
1670 size_t rem = size - i, num = min_t(size_t, rem, 4);
1671
1672 value = tegra_sor_hdmi_subpack(&ptr[i], num);
1673 tegra_sor_writel(sor, value, offset++);
1674
1675 num = min_t(size_t, rem - num, 3);
1676
1677 value = tegra_sor_hdmi_subpack(&ptr[i + 4], num);
1678 tegra_sor_writel(sor, value, offset++);
1679 }
1680}
1681
1682static int
1683tegra_sor_hdmi_setup_avi_infoframe(struct tegra_sor *sor,
1684 const struct drm_display_mode *mode)
1685{
1686 u8 buffer[HDMI_INFOFRAME_SIZE(AVI)];
1687 struct hdmi_avi_infoframe frame;
1688 u32 value;
1689 int err;
1690
1691 /* disable AVI infoframe */
1692 value = tegra_sor_readl(sor, SOR_HDMI_AVI_INFOFRAME_CTRL);
1693 value &= ~INFOFRAME_CTRL_SINGLE;
1694 value &= ~INFOFRAME_CTRL_OTHER;
1695 value &= ~INFOFRAME_CTRL_ENABLE;
1696 tegra_sor_writel(sor, value, SOR_HDMI_AVI_INFOFRAME_CTRL);
1697
1698 err = drm_hdmi_avi_infoframe_from_display_mode(&frame, mode);
1699 if (err < 0) {
1700 dev_err(sor->dev, "failed to setup AVI infoframe: %d\n", err);
1701 return err;
1702 }
1703
1704 err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
1705 if (err < 0) {
1706 dev_err(sor->dev, "failed to pack AVI infoframe: %d\n", err);
1707 return err;
1708 }
1709
1710 tegra_sor_hdmi_write_infopack(sor, buffer, err);
1711
1712 /* enable AVI infoframe */
1713 value = tegra_sor_readl(sor, SOR_HDMI_AVI_INFOFRAME_CTRL);
1714 value |= INFOFRAME_CTRL_CHECKSUM_ENABLE;
1715 value |= INFOFRAME_CTRL_ENABLE;
1716 tegra_sor_writel(sor, value, SOR_HDMI_AVI_INFOFRAME_CTRL);
1717
1718 return 0;
1719}
1720
1721static void tegra_sor_hdmi_disable_audio_infoframe(struct tegra_sor *sor)
1722{
1723 u32 value;
1724
1725 value = tegra_sor_readl(sor, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
1726 value &= ~INFOFRAME_CTRL_ENABLE;
1727 tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
1728}
1729
1730static struct tegra_sor_hdmi_settings *
1731tegra_sor_hdmi_find_settings(struct tegra_sor *sor, unsigned long frequency)
1732{
1733 unsigned int i;
1734
1735 for (i = 0; i < sor->num_settings; i++)
1736 if (frequency <= sor->settings[i].frequency)
1737 return &sor->settings[i];
1738
1739 return NULL;
1740}
1741
1742static void tegra_sor_hdmi_disable(struct drm_encoder *encoder)
1743{
1744 struct tegra_output *output = encoder_to_output(encoder);
1745 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
1746 struct tegra_sor *sor = to_sor(output);
1747 u32 value;
1748 int err;
1749
1750 err = tegra_sor_detach(sor);
1751 if (err < 0)
1752 dev_err(sor->dev, "failed to detach SOR: %d\n", err);
1753
1754 tegra_sor_writel(sor, 0, SOR_STATE1);
1755 tegra_sor_update(sor);
1756
1757 /* disable display to SOR clock */
1758 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
1759 value &= ~SOR1_TIMING_CYA;
1760 value &= ~SOR1_ENABLE;
1761 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
1762
1763 tegra_dc_commit(dc);
1764
1765 err = tegra_sor_power_down(sor);
1766 if (err < 0)
1767 dev_err(sor->dev, "failed to power down SOR: %d\n", err);
1768
1769 err = tegra_io_rail_power_off(TEGRA_IO_RAIL_HDMI);
1770 if (err < 0)
1771 dev_err(sor->dev, "failed to power off HDMI rail: %d\n", err);
1772
1773 reset_control_assert(sor->rst);
1774 usleep_range(1000, 2000);
1775 clk_disable_unprepare(sor->clk);
1776}
1777
1778static void tegra_sor_hdmi_enable(struct drm_encoder *encoder)
1779{
1780 struct tegra_output *output = encoder_to_output(encoder);
1781 unsigned int h_ref_to_sync = 1, pulse_start, max_ac;
1782 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
1783 unsigned int vbe, vse, hbe, hse, vbs, hbs, div;
1784 struct tegra_sor_hdmi_settings *settings;
1785 struct tegra_sor *sor = to_sor(output);
1786 struct drm_display_mode *mode;
1787 struct drm_display_info *info;
1788 u32 value;
1789 int err;
1790
1791 mode = &encoder->crtc->state->adjusted_mode;
1792 info = &output->connector.display_info;
1793
1794 err = clk_prepare_enable(sor->clk);
1795 if (err < 0)
1796 dev_err(sor->dev, "failed to enable clock: %d\n", err);
1797
1798 usleep_range(1000, 2000);
1799
1800 reset_control_deassert(sor->rst);
1801
Thierry Reding25bb2ce2015-08-03 14:23:29 +02001802 /* switch to safe parent clock */
1803 err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
Thierry Reding459cc2c2015-07-30 10:34:24 +02001804 if (err < 0)
1805 dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
1806
1807 div = clk_get_rate(sor->clk) / 1000000 * 4;
1808
1809 err = tegra_io_rail_power_on(TEGRA_IO_RAIL_HDMI);
1810 if (err < 0)
1811 dev_err(sor->dev, "failed to power on HDMI rail: %d\n", err);
1812
1813 usleep_range(20, 100);
1814
1815 value = tegra_sor_readl(sor, SOR_PLL2);
1816 value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
1817 tegra_sor_writel(sor, value, SOR_PLL2);
1818
1819 usleep_range(20, 100);
1820
1821 value = tegra_sor_readl(sor, SOR_PLL3);
1822 value &= ~SOR_PLL3_PLL_VDD_MODE_3V3;
1823 tegra_sor_writel(sor, value, SOR_PLL3);
1824
1825 value = tegra_sor_readl(sor, SOR_PLL0);
1826 value &= ~SOR_PLL0_VCOPD;
1827 value &= ~SOR_PLL0_PWR;
1828 tegra_sor_writel(sor, value, SOR_PLL0);
1829
1830 value = tegra_sor_readl(sor, SOR_PLL2);
1831 value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1832 tegra_sor_writel(sor, value, SOR_PLL2);
1833
1834 usleep_range(200, 400);
1835
1836 value = tegra_sor_readl(sor, SOR_PLL2);
1837 value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
1838 value &= ~SOR_PLL2_PORT_POWERDOWN;
1839 tegra_sor_writel(sor, value, SOR_PLL2);
1840
1841 usleep_range(20, 100);
1842
1843 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
1844 value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
1845 SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2;
1846 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
1847
1848 while (true) {
1849 value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
1850 if ((value & SOR_LANE_SEQ_CTL_STATE_BUSY) == 0)
1851 break;
1852
1853 usleep_range(250, 1000);
1854 }
1855
1856 value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
1857 SOR_LANE_SEQ_CTL_POWER_STATE_UP | SOR_LANE_SEQ_CTL_DELAY(5);
1858 tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
1859
1860 while (true) {
1861 value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
1862 if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
1863 break;
1864
1865 usleep_range(250, 1000);
1866 }
1867
1868 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
1869 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
1870 value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
1871
1872 if (mode->clock < 340000)
1873 value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G2_70;
1874 else
1875 value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G5_40;
1876
1877 value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK;
1878 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
1879
1880 value = tegra_sor_readl(sor, SOR_DP_SPARE0);
1881 value |= SOR_DP_SPARE_DISP_VIDEO_PREAMBLE;
1882 value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
1883 value |= SOR_DP_SPARE_SEQ_ENABLE;
1884 tegra_sor_writel(sor, value, SOR_DP_SPARE0);
1885
1886 value = SOR_SEQ_CTL_PU_PC(0) | SOR_SEQ_CTL_PU_PC_ALT(0) |
1887 SOR_SEQ_CTL_PD_PC(8) | SOR_SEQ_CTL_PD_PC_ALT(8);
1888 tegra_sor_writel(sor, value, SOR_SEQ_CTL);
1889
1890 value = SOR_SEQ_INST_DRIVE_PWM_OUT_LO | SOR_SEQ_INST_HALT |
1891 SOR_SEQ_INST_WAIT_VSYNC | SOR_SEQ_INST_WAIT(1);
1892 tegra_sor_writel(sor, value, SOR_SEQ_INST(0));
1893 tegra_sor_writel(sor, value, SOR_SEQ_INST(8));
1894
1895 /* program the reference clock */
1896 value = SOR_REFCLK_DIV_INT(div) | SOR_REFCLK_DIV_FRAC(div);
1897 tegra_sor_writel(sor, value, SOR_REFCLK);
1898
1899 /* XXX don't hardcode */
1900 value = SOR_XBAR_CTRL_LINK1_XSEL(4, 4) |
1901 SOR_XBAR_CTRL_LINK1_XSEL(3, 3) |
1902 SOR_XBAR_CTRL_LINK1_XSEL(2, 2) |
1903 SOR_XBAR_CTRL_LINK1_XSEL(1, 1) |
1904 SOR_XBAR_CTRL_LINK1_XSEL(0, 0) |
1905 SOR_XBAR_CTRL_LINK0_XSEL(4, 4) |
1906 SOR_XBAR_CTRL_LINK0_XSEL(3, 3) |
1907 SOR_XBAR_CTRL_LINK0_XSEL(2, 0) |
1908 SOR_XBAR_CTRL_LINK0_XSEL(1, 1) |
1909 SOR_XBAR_CTRL_LINK0_XSEL(0, 2);
1910 tegra_sor_writel(sor, value, SOR_XBAR_CTRL);
1911
1912 tegra_sor_writel(sor, 0x00000000, SOR_XBAR_POL);
1913
Thierry Reding25bb2ce2015-08-03 14:23:29 +02001914 /* switch to parent clock */
1915 err = tegra_sor_set_parent_clock(sor, sor->clk_parent);
Thierry Reding459cc2c2015-07-30 10:34:24 +02001916 if (err < 0)
1917 dev_err(sor->dev, "failed to set parent clock: %d\n", err);
1918
1919 value = SOR_INPUT_CONTROL_HDMI_SRC_SELECT(dc->pipe);
1920
1921 /* XXX is this the proper check? */
1922 if (mode->clock < 75000)
1923 value |= SOR_INPUT_CONTROL_ARM_VIDEO_RANGE_LIMITED;
1924
1925 tegra_sor_writel(sor, value, SOR_INPUT_CONTROL);
1926
1927 max_ac = ((mode->htotal - mode->hdisplay) - SOR_REKEY - 18) / 32;
1928
1929 value = SOR_HDMI_CTRL_ENABLE | SOR_HDMI_CTRL_MAX_AC_PACKET(max_ac) |
1930 SOR_HDMI_CTRL_AUDIO_LAYOUT | SOR_HDMI_CTRL_REKEY(SOR_REKEY);
1931 tegra_sor_writel(sor, value, SOR_HDMI_CTRL);
1932
1933 /* H_PULSE2 setup */
1934 pulse_start = h_ref_to_sync + (mode->hsync_end - mode->hsync_start) +
1935 (mode->htotal - mode->hsync_end) - 10;
1936
1937 value = PULSE_LAST_END_A | PULSE_QUAL_VACTIVE |
1938 PULSE_POLARITY_HIGH | PULSE_MODE_NORMAL;
1939 tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_CONTROL);
1940
1941 value = PULSE_END(pulse_start + 8) | PULSE_START(pulse_start);
1942 tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_POSITION_A);
1943
1944 value = tegra_dc_readl(dc, DC_DISP_DISP_SIGNAL_OPTIONS0);
1945 value |= H_PULSE2_ENABLE;
1946 tegra_dc_writel(dc, value, DC_DISP_DISP_SIGNAL_OPTIONS0);
1947
1948 /* infoframe setup */
1949 err = tegra_sor_hdmi_setup_avi_infoframe(sor, mode);
1950 if (err < 0)
1951 dev_err(sor->dev, "failed to setup AVI infoframe: %d\n", err);
1952
1953 /* XXX HDMI audio support not implemented yet */
1954 tegra_sor_hdmi_disable_audio_infoframe(sor);
1955
1956 /* use single TMDS protocol */
1957 value = tegra_sor_readl(sor, SOR_STATE1);
1958 value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
1959 value |= SOR_STATE_ASY_PROTOCOL_SINGLE_TMDS_A;
1960 tegra_sor_writel(sor, value, SOR_STATE1);
1961
1962 /* power up pad calibration */
1963 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
1964 value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
1965 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
1966
1967 /* production settings */
1968 settings = tegra_sor_hdmi_find_settings(sor, mode->clock * 1000);
Dan Carpenterdb8b42f2015-08-17 17:37:03 +03001969 if (!settings) {
1970 dev_err(sor->dev, "no settings for pixel clock %d Hz\n",
1971 mode->clock * 1000);
Thierry Reding459cc2c2015-07-30 10:34:24 +02001972 return;
1973 }
1974
1975 value = tegra_sor_readl(sor, SOR_PLL0);
1976 value &= ~SOR_PLL0_ICHPMP_MASK;
1977 value &= ~SOR_PLL0_VCOCAP_MASK;
1978 value |= SOR_PLL0_ICHPMP(settings->ichpmp);
1979 value |= SOR_PLL0_VCOCAP(settings->vcocap);
1980 tegra_sor_writel(sor, value, SOR_PLL0);
1981
1982 tegra_sor_dp_term_calibrate(sor);
1983
1984 value = tegra_sor_readl(sor, SOR_PLL1);
1985 value &= ~SOR_PLL1_LOADADJ_MASK;
1986 value |= SOR_PLL1_LOADADJ(settings->loadadj);
1987 tegra_sor_writel(sor, value, SOR_PLL1);
1988
1989 value = tegra_sor_readl(sor, SOR_PLL3);
1990 value &= ~SOR_PLL3_BG_VREF_LEVEL_MASK;
1991 value |= SOR_PLL3_BG_VREF_LEVEL(settings->bg_vref);
1992 tegra_sor_writel(sor, value, SOR_PLL3);
1993
1994 value = settings->drive_current[0] << 24 |
1995 settings->drive_current[1] << 16 |
1996 settings->drive_current[2] << 8 |
1997 settings->drive_current[3] << 0;
1998 tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT0);
1999
2000 value = settings->preemphasis[0] << 24 |
2001 settings->preemphasis[1] << 16 |
2002 settings->preemphasis[2] << 8 |
2003 settings->preemphasis[3] << 0;
2004 tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS0);
2005
2006 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
2007 value &= ~SOR_DP_PADCTL_TX_PU_MASK;
2008 value |= SOR_DP_PADCTL_TX_PU_ENABLE;
2009 value |= SOR_DP_PADCTL_TX_PU(settings->tx_pu);
2010 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
2011
2012 /* power down pad calibration */
2013 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
2014 value |= SOR_DP_PADCTL_PAD_CAL_PD;
2015 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
2016
2017 /* miscellaneous display controller settings */
2018 value = VSYNC_H_POSITION(1);
2019 tegra_dc_writel(dc, value, DC_DISP_DISP_TIMING_OPTIONS);
2020
2021 value = tegra_dc_readl(dc, DC_DISP_DISP_COLOR_CONTROL);
2022 value &= ~DITHER_CONTROL_MASK;
2023 value &= ~BASE_COLOR_SIZE_MASK;
2024
2025 switch (info->bpc) {
2026 case 6:
2027 value |= BASE_COLOR_SIZE_666;
2028 break;
2029
2030 case 8:
2031 value |= BASE_COLOR_SIZE_888;
2032 break;
2033
2034 default:
2035 WARN(1, "%u bits-per-color not supported\n", info->bpc);
2036 break;
2037 }
2038
2039 tegra_dc_writel(dc, value, DC_DISP_DISP_COLOR_CONTROL);
2040
2041 err = tegra_sor_power_up(sor, 250);
2042 if (err < 0)
2043 dev_err(sor->dev, "failed to power up SOR: %d\n", err);
2044
2045 /* configure mode */
2046 value = tegra_sor_readl(sor, SOR_STATE1);
2047 value &= ~SOR_STATE_ASY_PIXELDEPTH_MASK;
2048 value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
2049 value &= ~SOR_STATE_ASY_OWNER_MASK;
2050
2051 value |= SOR_STATE_ASY_CRC_MODE_COMPLETE |
2052 SOR_STATE_ASY_OWNER(dc->pipe + 1);
2053
2054 if (mode->flags & DRM_MODE_FLAG_PHSYNC)
2055 value &= ~SOR_STATE_ASY_HSYNCPOL;
2056
2057 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
2058 value |= SOR_STATE_ASY_HSYNCPOL;
2059
2060 if (mode->flags & DRM_MODE_FLAG_PVSYNC)
2061 value &= ~SOR_STATE_ASY_VSYNCPOL;
2062
2063 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
2064 value |= SOR_STATE_ASY_VSYNCPOL;
2065
2066 switch (info->bpc) {
2067 case 8:
2068 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
2069 break;
2070
2071 case 6:
2072 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_18_444;
2073 break;
2074
2075 default:
2076 BUG();
2077 break;
2078 }
2079
2080 tegra_sor_writel(sor, value, SOR_STATE1);
2081
2082 value = tegra_sor_readl(sor, SOR_HEAD_STATE0(dc->pipe));
2083 value &= ~SOR_HEAD_STATE_RANGECOMPRESS_MASK;
2084 value &= ~SOR_HEAD_STATE_DYNRANGE_MASK;
2085 tegra_sor_writel(sor, value, SOR_HEAD_STATE0(dc->pipe));
2086
2087 value = tegra_sor_readl(sor, SOR_HEAD_STATE0(dc->pipe));
2088 value &= ~SOR_HEAD_STATE_COLORSPACE_MASK;
2089 value |= SOR_HEAD_STATE_COLORSPACE_RGB;
2090 tegra_sor_writel(sor, value, SOR_HEAD_STATE0(dc->pipe));
2091
2092 /*
2093 * TODO: The video timing programming below doesn't seem to match the
2094 * register definitions.
2095 */
2096
2097 value = ((mode->vtotal & 0x7fff) << 16) | (mode->htotal & 0x7fff);
2098 tegra_sor_writel(sor, value, SOR_HEAD_STATE1(dc->pipe));
2099
2100 /* sync end = sync width - 1 */
2101 vse = mode->vsync_end - mode->vsync_start - 1;
2102 hse = mode->hsync_end - mode->hsync_start - 1;
2103
2104 value = ((vse & 0x7fff) << 16) | (hse & 0x7fff);
2105 tegra_sor_writel(sor, value, SOR_HEAD_STATE2(dc->pipe));
2106
2107 /* blank end = sync end + back porch */
2108 vbe = vse + (mode->vtotal - mode->vsync_end);
2109 hbe = hse + (mode->htotal - mode->hsync_end);
2110
2111 value = ((vbe & 0x7fff) << 16) | (hbe & 0x7fff);
2112 tegra_sor_writel(sor, value, SOR_HEAD_STATE3(dc->pipe));
2113
2114 /* blank start = blank end + active */
2115 vbs = vbe + mode->vdisplay;
2116 hbs = hbe + mode->hdisplay;
2117
2118 value = ((vbs & 0x7fff) << 16) | (hbs & 0x7fff);
2119 tegra_sor_writel(sor, value, SOR_HEAD_STATE4(dc->pipe));
2120
2121 tegra_sor_writel(sor, 0x1, SOR_HEAD_STATE5(dc->pipe));
2122
2123 tegra_sor_update(sor);
2124
2125 err = tegra_sor_attach(sor);
2126 if (err < 0)
2127 dev_err(sor->dev, "failed to attach SOR: %d\n", err);
2128
2129 /* enable display to SOR clock and generate HDMI preamble */
2130 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
2131 value |= SOR1_ENABLE | SOR1_TIMING_CYA;
2132 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
2133
2134 tegra_dc_commit(dc);
2135
2136 err = tegra_sor_wakeup(sor);
2137 if (err < 0)
2138 dev_err(sor->dev, "failed to wakeup SOR: %d\n", err);
2139}
2140
2141static const struct drm_encoder_helper_funcs tegra_sor_hdmi_helpers = {
2142 .disable = tegra_sor_hdmi_disable,
2143 .enable = tegra_sor_hdmi_enable,
2144 .atomic_check = tegra_sor_encoder_atomic_check,
2145};
2146
Thierry Reding6b6b6042013-11-15 16:06:05 +01002147static int tegra_sor_init(struct host1x_client *client)
2148{
Thierry Reding9910f5c2014-05-22 09:57:15 +02002149 struct drm_device *drm = dev_get_drvdata(client->parent);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002150 const struct drm_encoder_helper_funcs *helpers = NULL;
Thierry Reding6b6b6042013-11-15 16:06:05 +01002151 struct tegra_sor *sor = host1x_client_to_sor(client);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002152 int connector = DRM_MODE_CONNECTOR_Unknown;
2153 int encoder = DRM_MODE_ENCODER_NONE;
Thierry Reding6b6b6042013-11-15 16:06:05 +01002154 int err;
2155
Thierry Reding9542c232015-07-08 13:39:09 +02002156 if (!sor->aux) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002157 if (sor->soc->supports_hdmi) {
2158 connector = DRM_MODE_CONNECTOR_HDMIA;
2159 encoder = DRM_MODE_ENCODER_TMDS;
2160 helpers = &tegra_sor_hdmi_helpers;
2161 } else if (sor->soc->supports_lvds) {
2162 connector = DRM_MODE_CONNECTOR_LVDS;
2163 encoder = DRM_MODE_ENCODER_LVDS;
2164 }
2165 } else {
2166 if (sor->soc->supports_edp) {
2167 connector = DRM_MODE_CONNECTOR_eDP;
2168 encoder = DRM_MODE_ENCODER_TMDS;
2169 helpers = &tegra_sor_edp_helpers;
2170 } else if (sor->soc->supports_dp) {
2171 connector = DRM_MODE_CONNECTOR_DisplayPort;
2172 encoder = DRM_MODE_ENCODER_TMDS;
2173 }
2174 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01002175
Thierry Reding6b6b6042013-11-15 16:06:05 +01002176 sor->output.dev = sor->dev;
Thierry Reding6b6b6042013-11-15 16:06:05 +01002177
Thierry Reding6fad8f62014-11-28 15:41:34 +01002178 drm_connector_init(drm, &sor->output.connector,
2179 &tegra_sor_connector_funcs,
Thierry Reding459cc2c2015-07-30 10:34:24 +02002180 connector);
Thierry Reding6fad8f62014-11-28 15:41:34 +01002181 drm_connector_helper_add(&sor->output.connector,
2182 &tegra_sor_connector_helper_funcs);
2183 sor->output.connector.dpms = DRM_MODE_DPMS_OFF;
2184
Thierry Reding6fad8f62014-11-28 15:41:34 +01002185 drm_encoder_init(drm, &sor->output.encoder, &tegra_sor_encoder_funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +02002186 encoder, NULL);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002187 drm_encoder_helper_add(&sor->output.encoder, helpers);
Thierry Reding6fad8f62014-11-28 15:41:34 +01002188
2189 drm_mode_connector_attach_encoder(&sor->output.connector,
2190 &sor->output.encoder);
2191 drm_connector_register(&sor->output.connector);
2192
Thierry Redingea130b22014-12-19 15:51:35 +01002193 err = tegra_output_init(drm, &sor->output);
2194 if (err < 0) {
2195 dev_err(client->dev, "failed to initialize output: %d\n", err);
2196 return err;
2197 }
Thierry Reding6fad8f62014-11-28 15:41:34 +01002198
Thierry Redingea130b22014-12-19 15:51:35 +01002199 sor->output.encoder.possible_crtcs = 0x3;
Thierry Reding6b6b6042013-11-15 16:06:05 +01002200
Thierry Redinga82752e2014-01-31 10:02:15 +01002201 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
Thierry Reding1b0c7b42014-05-28 13:46:12 +02002202 err = tegra_sor_debugfs_init(sor, drm->primary);
Thierry Redinga82752e2014-01-31 10:02:15 +01002203 if (err < 0)
2204 dev_err(sor->dev, "debugfs setup failed: %d\n", err);
2205 }
2206
Thierry Reding9542c232015-07-08 13:39:09 +02002207 if (sor->aux) {
2208 err = drm_dp_aux_attach(sor->aux, &sor->output);
Thierry Reding6b6b6042013-11-15 16:06:05 +01002209 if (err < 0) {
2210 dev_err(sor->dev, "failed to attach DP: %d\n", err);
2211 return err;
2212 }
2213 }
2214
Tomeu Vizoso535a65d2015-03-30 10:33:03 +02002215 /*
2216 * XXX: Remove this reset once proper hand-over from firmware to
2217 * kernel is possible.
2218 */
2219 err = reset_control_assert(sor->rst);
2220 if (err < 0) {
2221 dev_err(sor->dev, "failed to assert SOR reset: %d\n", err);
2222 return err;
2223 }
2224
Thierry Reding6fad8f62014-11-28 15:41:34 +01002225 err = clk_prepare_enable(sor->clk);
2226 if (err < 0) {
2227 dev_err(sor->dev, "failed to enable clock: %d\n", err);
2228 return err;
2229 }
2230
Tomeu Vizoso535a65d2015-03-30 10:33:03 +02002231 usleep_range(1000, 3000);
2232
2233 err = reset_control_deassert(sor->rst);
2234 if (err < 0) {
2235 dev_err(sor->dev, "failed to deassert SOR reset: %d\n", err);
2236 return err;
2237 }
2238
Thierry Reding6fad8f62014-11-28 15:41:34 +01002239 err = clk_prepare_enable(sor->clk_safe);
2240 if (err < 0)
2241 return err;
2242
2243 err = clk_prepare_enable(sor->clk_dp);
2244 if (err < 0)
2245 return err;
2246
Thierry Reding6b6b6042013-11-15 16:06:05 +01002247 return 0;
2248}
2249
2250static int tegra_sor_exit(struct host1x_client *client)
2251{
2252 struct tegra_sor *sor = host1x_client_to_sor(client);
2253 int err;
2254
Thierry Reding328ec692014-12-19 15:55:08 +01002255 tegra_output_exit(&sor->output);
2256
Thierry Reding9542c232015-07-08 13:39:09 +02002257 if (sor->aux) {
2258 err = drm_dp_aux_detach(sor->aux);
Thierry Reding6b6b6042013-11-15 16:06:05 +01002259 if (err < 0) {
2260 dev_err(sor->dev, "failed to detach DP: %d\n", err);
2261 return err;
2262 }
2263 }
2264
Thierry Reding6fad8f62014-11-28 15:41:34 +01002265 clk_disable_unprepare(sor->clk_safe);
2266 clk_disable_unprepare(sor->clk_dp);
2267 clk_disable_unprepare(sor->clk);
2268
Thierry Reding4009c222014-12-19 15:47:30 +01002269 if (IS_ENABLED(CONFIG_DEBUG_FS))
2270 tegra_sor_debugfs_exit(sor);
Thierry Redinga82752e2014-01-31 10:02:15 +01002271
Thierry Reding6b6b6042013-11-15 16:06:05 +01002272 return 0;
2273}
2274
2275static const struct host1x_client_ops sor_client_ops = {
2276 .init = tegra_sor_init,
2277 .exit = tegra_sor_exit,
2278};
2279
Thierry Reding459cc2c2015-07-30 10:34:24 +02002280static const struct tegra_sor_ops tegra_sor_edp_ops = {
2281 .name = "eDP",
2282};
2283
2284static int tegra_sor_hdmi_probe(struct tegra_sor *sor)
2285{
2286 int err;
2287
2288 sor->avdd_io_supply = devm_regulator_get(sor->dev, "avdd-io");
2289 if (IS_ERR(sor->avdd_io_supply)) {
2290 dev_err(sor->dev, "cannot get AVDD I/O supply: %ld\n",
2291 PTR_ERR(sor->avdd_io_supply));
2292 return PTR_ERR(sor->avdd_io_supply);
2293 }
2294
2295 err = regulator_enable(sor->avdd_io_supply);
2296 if (err < 0) {
2297 dev_err(sor->dev, "failed to enable AVDD I/O supply: %d\n",
2298 err);
2299 return err;
2300 }
2301
2302 sor->vdd_pll_supply = devm_regulator_get(sor->dev, "vdd-pll");
2303 if (IS_ERR(sor->vdd_pll_supply)) {
2304 dev_err(sor->dev, "cannot get VDD PLL supply: %ld\n",
2305 PTR_ERR(sor->vdd_pll_supply));
2306 return PTR_ERR(sor->vdd_pll_supply);
2307 }
2308
2309 err = regulator_enable(sor->vdd_pll_supply);
2310 if (err < 0) {
2311 dev_err(sor->dev, "failed to enable VDD PLL supply: %d\n",
2312 err);
2313 return err;
2314 }
2315
2316 sor->hdmi_supply = devm_regulator_get(sor->dev, "hdmi");
2317 if (IS_ERR(sor->hdmi_supply)) {
2318 dev_err(sor->dev, "cannot get HDMI supply: %ld\n",
2319 PTR_ERR(sor->hdmi_supply));
2320 return PTR_ERR(sor->hdmi_supply);
2321 }
2322
2323 err = regulator_enable(sor->hdmi_supply);
2324 if (err < 0) {
2325 dev_err(sor->dev, "failed to enable HDMI supply: %d\n", err);
2326 return err;
2327 }
2328
2329 return 0;
2330}
2331
2332static int tegra_sor_hdmi_remove(struct tegra_sor *sor)
2333{
2334 regulator_disable(sor->hdmi_supply);
2335 regulator_disable(sor->vdd_pll_supply);
2336 regulator_disable(sor->avdd_io_supply);
2337
2338 return 0;
2339}
2340
2341static const struct tegra_sor_ops tegra_sor_hdmi_ops = {
2342 .name = "HDMI",
2343 .probe = tegra_sor_hdmi_probe,
2344 .remove = tegra_sor_hdmi_remove,
2345};
2346
2347static const struct tegra_sor_soc tegra124_sor = {
2348 .supports_edp = true,
2349 .supports_lvds = true,
2350 .supports_hdmi = false,
2351 .supports_dp = false,
2352};
2353
2354static const struct tegra_sor_soc tegra210_sor = {
2355 .supports_edp = true,
2356 .supports_lvds = false,
2357 .supports_hdmi = false,
2358 .supports_dp = false,
2359};
2360
2361static const struct tegra_sor_soc tegra210_sor1 = {
2362 .supports_edp = false,
2363 .supports_lvds = false,
2364 .supports_hdmi = true,
2365 .supports_dp = true,
2366
2367 .num_settings = ARRAY_SIZE(tegra210_sor_hdmi_defaults),
2368 .settings = tegra210_sor_hdmi_defaults,
2369};
2370
2371static const struct of_device_id tegra_sor_of_match[] = {
2372 { .compatible = "nvidia,tegra210-sor1", .data = &tegra210_sor1 },
2373 { .compatible = "nvidia,tegra210-sor", .data = &tegra210_sor },
2374 { .compatible = "nvidia,tegra124-sor", .data = &tegra124_sor },
2375 { },
2376};
2377MODULE_DEVICE_TABLE(of, tegra_sor_of_match);
2378
Thierry Reding6b6b6042013-11-15 16:06:05 +01002379static int tegra_sor_probe(struct platform_device *pdev)
2380{
Thierry Reding459cc2c2015-07-30 10:34:24 +02002381 const struct of_device_id *match;
Thierry Reding6b6b6042013-11-15 16:06:05 +01002382 struct device_node *np;
2383 struct tegra_sor *sor;
2384 struct resource *regs;
2385 int err;
2386
Thierry Reding459cc2c2015-07-30 10:34:24 +02002387 match = of_match_device(tegra_sor_of_match, &pdev->dev);
2388
Thierry Reding6b6b6042013-11-15 16:06:05 +01002389 sor = devm_kzalloc(&pdev->dev, sizeof(*sor), GFP_KERNEL);
2390 if (!sor)
2391 return -ENOMEM;
2392
2393 sor->output.dev = sor->dev = &pdev->dev;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002394 sor->soc = match->data;
2395
2396 sor->settings = devm_kmemdup(&pdev->dev, sor->soc->settings,
2397 sor->soc->num_settings *
2398 sizeof(*sor->settings),
2399 GFP_KERNEL);
2400 if (!sor->settings)
2401 return -ENOMEM;
2402
2403 sor->num_settings = sor->soc->num_settings;
Thierry Reding6b6b6042013-11-15 16:06:05 +01002404
2405 np = of_parse_phandle(pdev->dev.of_node, "nvidia,dpaux", 0);
2406 if (np) {
Thierry Reding9542c232015-07-08 13:39:09 +02002407 sor->aux = drm_dp_aux_find_by_of_node(np);
Thierry Reding6b6b6042013-11-15 16:06:05 +01002408 of_node_put(np);
2409
Thierry Reding9542c232015-07-08 13:39:09 +02002410 if (!sor->aux)
Thierry Reding6b6b6042013-11-15 16:06:05 +01002411 return -EPROBE_DEFER;
2412 }
2413
Thierry Reding9542c232015-07-08 13:39:09 +02002414 if (!sor->aux) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002415 if (sor->soc->supports_hdmi) {
2416 sor->ops = &tegra_sor_hdmi_ops;
2417 } else if (sor->soc->supports_lvds) {
2418 dev_err(&pdev->dev, "LVDS not supported yet\n");
2419 return -ENODEV;
2420 } else {
2421 dev_err(&pdev->dev, "unknown (non-DP) support\n");
2422 return -ENODEV;
2423 }
2424 } else {
2425 if (sor->soc->supports_edp) {
2426 sor->ops = &tegra_sor_edp_ops;
2427 } else if (sor->soc->supports_dp) {
2428 dev_err(&pdev->dev, "DisplayPort not supported yet\n");
2429 return -ENODEV;
2430 } else {
2431 dev_err(&pdev->dev, "unknown (DP) support\n");
2432 return -ENODEV;
2433 }
2434 }
2435
Thierry Reding6b6b6042013-11-15 16:06:05 +01002436 err = tegra_output_probe(&sor->output);
Thierry Reding4dbdc742015-04-27 15:04:26 +02002437 if (err < 0) {
2438 dev_err(&pdev->dev, "failed to probe output: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01002439 return err;
Thierry Reding4dbdc742015-04-27 15:04:26 +02002440 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01002441
Thierry Reding459cc2c2015-07-30 10:34:24 +02002442 if (sor->ops && sor->ops->probe) {
2443 err = sor->ops->probe(sor);
2444 if (err < 0) {
2445 dev_err(&pdev->dev, "failed to probe %s: %d\n",
2446 sor->ops->name, err);
2447 goto output;
2448 }
2449 }
2450
Thierry Reding6b6b6042013-11-15 16:06:05 +01002451 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2452 sor->regs = devm_ioremap_resource(&pdev->dev, regs);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002453 if (IS_ERR(sor->regs)) {
2454 err = PTR_ERR(sor->regs);
2455 goto remove;
2456 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01002457
2458 sor->rst = devm_reset_control_get(&pdev->dev, "sor");
Thierry Reding4dbdc742015-04-27 15:04:26 +02002459 if (IS_ERR(sor->rst)) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002460 err = PTR_ERR(sor->rst);
2461 dev_err(&pdev->dev, "failed to get reset control: %d\n", err);
2462 goto remove;
Thierry Reding4dbdc742015-04-27 15:04:26 +02002463 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01002464
2465 sor->clk = devm_clk_get(&pdev->dev, NULL);
Thierry Reding4dbdc742015-04-27 15:04:26 +02002466 if (IS_ERR(sor->clk)) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002467 err = PTR_ERR(sor->clk);
2468 dev_err(&pdev->dev, "failed to get module clock: %d\n", err);
2469 goto remove;
Thierry Reding4dbdc742015-04-27 15:04:26 +02002470 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01002471
2472 sor->clk_parent = devm_clk_get(&pdev->dev, "parent");
Thierry Reding4dbdc742015-04-27 15:04:26 +02002473 if (IS_ERR(sor->clk_parent)) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002474 err = PTR_ERR(sor->clk_parent);
2475 dev_err(&pdev->dev, "failed to get parent clock: %d\n", err);
2476 goto remove;
Thierry Reding4dbdc742015-04-27 15:04:26 +02002477 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01002478
Thierry Reding6b6b6042013-11-15 16:06:05 +01002479 sor->clk_safe = devm_clk_get(&pdev->dev, "safe");
Thierry Reding4dbdc742015-04-27 15:04:26 +02002480 if (IS_ERR(sor->clk_safe)) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002481 err = PTR_ERR(sor->clk_safe);
2482 dev_err(&pdev->dev, "failed to get safe clock: %d\n", err);
2483 goto remove;
Thierry Reding4dbdc742015-04-27 15:04:26 +02002484 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01002485
Thierry Reding6b6b6042013-11-15 16:06:05 +01002486 sor->clk_dp = devm_clk_get(&pdev->dev, "dp");
Thierry Reding4dbdc742015-04-27 15:04:26 +02002487 if (IS_ERR(sor->clk_dp)) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002488 err = PTR_ERR(sor->clk_dp);
2489 dev_err(&pdev->dev, "failed to get DP clock: %d\n", err);
2490 goto remove;
Thierry Reding4dbdc742015-04-27 15:04:26 +02002491 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01002492
Thierry Reding6b6b6042013-11-15 16:06:05 +01002493 INIT_LIST_HEAD(&sor->client.list);
2494 sor->client.ops = &sor_client_ops;
2495 sor->client.dev = &pdev->dev;
2496
Thierry Reding6b6b6042013-11-15 16:06:05 +01002497 err = host1x_client_register(&sor->client);
2498 if (err < 0) {
2499 dev_err(&pdev->dev, "failed to register host1x client: %d\n",
2500 err);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002501 goto remove;
Thierry Reding6b6b6042013-11-15 16:06:05 +01002502 }
2503
2504 platform_set_drvdata(pdev, sor);
2505
2506 return 0;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002507
2508remove:
2509 if (sor->ops && sor->ops->remove)
2510 sor->ops->remove(sor);
2511output:
2512 tegra_output_remove(&sor->output);
2513 return err;
Thierry Reding6b6b6042013-11-15 16:06:05 +01002514}
2515
2516static int tegra_sor_remove(struct platform_device *pdev)
2517{
2518 struct tegra_sor *sor = platform_get_drvdata(pdev);
2519 int err;
2520
2521 err = host1x_client_unregister(&sor->client);
2522 if (err < 0) {
2523 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
2524 err);
2525 return err;
2526 }
2527
Thierry Reding459cc2c2015-07-30 10:34:24 +02002528 if (sor->ops && sor->ops->remove) {
2529 err = sor->ops->remove(sor);
2530 if (err < 0)
2531 dev_err(&pdev->dev, "failed to remove SOR: %d\n", err);
2532 }
2533
Thierry Reding328ec692014-12-19 15:55:08 +01002534 tegra_output_remove(&sor->output);
Thierry Reding6b6b6042013-11-15 16:06:05 +01002535
2536 return 0;
2537}
2538
Thierry Reding6b6b6042013-11-15 16:06:05 +01002539struct platform_driver tegra_sor_driver = {
2540 .driver = {
2541 .name = "tegra-sor",
2542 .of_match_table = tegra_sor_of_match,
2543 },
2544 .probe = tegra_sor_probe,
2545 .remove = tegra_sor_remove,
2546};