blob: 01b31805f719dfe6a1b7bbd8153f484eb88175ad [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 Reding402f6bc2015-07-21 16:48:19 +0200681static void tegra_sor_apply_config(struct tegra_sor *sor,
682 const struct tegra_sor_config *config)
683{
684 u32 value;
685
686 value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
687 value &= ~SOR_DP_LINKCTL_TU_SIZE_MASK;
688 value |= SOR_DP_LINKCTL_TU_SIZE(config->tu_size);
689 tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
690
691 value = tegra_sor_readl(sor, SOR_DP_CONFIG0);
692 value &= ~SOR_DP_CONFIG_WATERMARK_MASK;
693 value |= SOR_DP_CONFIG_WATERMARK(config->watermark);
694
695 value &= ~SOR_DP_CONFIG_ACTIVE_SYM_COUNT_MASK;
696 value |= SOR_DP_CONFIG_ACTIVE_SYM_COUNT(config->active_count);
697
698 value &= ~SOR_DP_CONFIG_ACTIVE_SYM_FRAC_MASK;
699 value |= SOR_DP_CONFIG_ACTIVE_SYM_FRAC(config->active_frac);
700
701 if (config->active_polarity)
702 value |= SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
703 else
704 value &= ~SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
705
706 value |= SOR_DP_CONFIG_ACTIVE_SYM_ENABLE;
707 value |= SOR_DP_CONFIG_DISPARITY_NEGATIVE;
708 tegra_sor_writel(sor, value, SOR_DP_CONFIG0);
709
710 value = tegra_sor_readl(sor, SOR_DP_AUDIO_HBLANK_SYMBOLS);
711 value &= ~SOR_DP_AUDIO_HBLANK_SYMBOLS_MASK;
712 value |= config->hblank_symbols & 0xffff;
713 tegra_sor_writel(sor, value, SOR_DP_AUDIO_HBLANK_SYMBOLS);
714
715 value = tegra_sor_readl(sor, SOR_DP_AUDIO_VBLANK_SYMBOLS);
716 value &= ~SOR_DP_AUDIO_VBLANK_SYMBOLS_MASK;
717 value |= config->vblank_symbols & 0xffff;
718 tegra_sor_writel(sor, value, SOR_DP_AUDIO_VBLANK_SYMBOLS);
719}
720
Thierry Reding2bd1dd32015-08-03 15:46:15 +0200721static void tegra_sor_mode_set(struct tegra_sor *sor,
722 const struct drm_display_mode *mode,
723 const struct drm_display_info *info)
724{
725 struct tegra_dc *dc = to_tegra_dc(sor->output.encoder.crtc);
726 unsigned int vbe, vse, hbe, hse, vbs, hbs;
727 u32 value;
728
729 value = tegra_sor_readl(sor, SOR_STATE1);
730 value &= ~SOR_STATE_ASY_PIXELDEPTH_MASK;
731 value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
732 value &= ~SOR_STATE_ASY_OWNER_MASK;
733
734 value |= SOR_STATE_ASY_CRC_MODE_COMPLETE |
735 SOR_STATE_ASY_OWNER(dc->pipe + 1);
736
737 if (mode->flags & DRM_MODE_FLAG_PHSYNC)
738 value &= ~SOR_STATE_ASY_HSYNCPOL;
739
740 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
741 value |= SOR_STATE_ASY_HSYNCPOL;
742
743 if (mode->flags & DRM_MODE_FLAG_PVSYNC)
744 value &= ~SOR_STATE_ASY_VSYNCPOL;
745
746 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
747 value |= SOR_STATE_ASY_VSYNCPOL;
748
749 switch (info->bpc) {
750 case 8:
751 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
752 break;
753
754 case 6:
755 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_18_444;
756 break;
757
758 default:
759 BUG();
760 break;
761 }
762
763 tegra_sor_writel(sor, value, SOR_STATE1);
764
765 /*
766 * TODO: The video timing programming below doesn't seem to match the
767 * register definitions.
768 */
769
770 value = ((mode->vtotal & 0x7fff) << 16) | (mode->htotal & 0x7fff);
771 tegra_sor_writel(sor, value, SOR_HEAD_STATE1(dc->pipe));
772
773 /* sync end = sync width - 1 */
774 vse = mode->vsync_end - mode->vsync_start - 1;
775 hse = mode->hsync_end - mode->hsync_start - 1;
776
777 value = ((vse & 0x7fff) << 16) | (hse & 0x7fff);
778 tegra_sor_writel(sor, value, SOR_HEAD_STATE2(dc->pipe));
779
780 /* blank end = sync end + back porch */
781 vbe = vse + (mode->vtotal - mode->vsync_end);
782 hbe = hse + (mode->htotal - mode->hsync_end);
783
784 value = ((vbe & 0x7fff) << 16) | (hbe & 0x7fff);
785 tegra_sor_writel(sor, value, SOR_HEAD_STATE3(dc->pipe));
786
787 /* blank start = blank end + active */
788 vbs = vbe + mode->vdisplay;
789 hbs = hbe + mode->hdisplay;
790
791 value = ((vbs & 0x7fff) << 16) | (hbs & 0x7fff);
792 tegra_sor_writel(sor, value, SOR_HEAD_STATE4(dc->pipe));
793
794 /* XXX interlacing support */
795 tegra_sor_writel(sor, 0x001, SOR_HEAD_STATE5(dc->pipe));
796}
797
Thierry Reding6fad8f62014-11-28 15:41:34 +0100798static int tegra_sor_detach(struct tegra_sor *sor)
Thierry Reding6b6b6042013-11-15 16:06:05 +0100799{
Thierry Reding6fad8f62014-11-28 15:41:34 +0100800 unsigned long value, timeout;
801
802 /* switch to safe mode */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200803 value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100804 value &= ~SOR_SUPER_STATE_MODE_NORMAL;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200805 tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100806 tegra_sor_super_update(sor);
807
808 timeout = jiffies + msecs_to_jiffies(250);
809
810 while (time_before(jiffies, timeout)) {
811 value = tegra_sor_readl(sor, SOR_PWR);
812 if (value & SOR_PWR_MODE_SAFE)
813 break;
814 }
815
816 if ((value & SOR_PWR_MODE_SAFE) == 0)
817 return -ETIMEDOUT;
818
819 /* go to sleep */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200820 value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100821 value &= ~SOR_SUPER_STATE_HEAD_MODE_MASK;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200822 tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100823 tegra_sor_super_update(sor);
824
825 /* detach */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200826 value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100827 value &= ~SOR_SUPER_STATE_ATTACHED;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200828 tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100829 tegra_sor_super_update(sor);
830
831 timeout = jiffies + msecs_to_jiffies(250);
832
833 while (time_before(jiffies, timeout)) {
834 value = tegra_sor_readl(sor, SOR_TEST);
835 if ((value & SOR_TEST_ATTACHED) == 0)
836 break;
837
838 usleep_range(25, 100);
839 }
840
841 if ((value & SOR_TEST_ATTACHED) != 0)
842 return -ETIMEDOUT;
843
844 return 0;
845}
846
847static int tegra_sor_power_down(struct tegra_sor *sor)
848{
849 unsigned long value, timeout;
850 int err;
851
852 value = tegra_sor_readl(sor, SOR_PWR);
853 value &= ~SOR_PWR_NORMAL_STATE_PU;
854 value |= SOR_PWR_TRIGGER;
855 tegra_sor_writel(sor, value, SOR_PWR);
856
857 timeout = jiffies + msecs_to_jiffies(250);
858
859 while (time_before(jiffies, timeout)) {
860 value = tegra_sor_readl(sor, SOR_PWR);
861 if ((value & SOR_PWR_TRIGGER) == 0)
862 return 0;
863
864 usleep_range(25, 100);
865 }
866
867 if ((value & SOR_PWR_TRIGGER) != 0)
868 return -ETIMEDOUT;
869
Thierry Reding25bb2ce2015-08-03 14:23:29 +0200870 /* switch to safe parent clock */
871 err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100872 if (err < 0)
873 dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
874
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200875 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100876 value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
877 SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200878 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100879
880 /* stop lane sequencer */
881 value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_UP |
882 SOR_LANE_SEQ_CTL_POWER_STATE_DOWN;
883 tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
884
885 timeout = jiffies + msecs_to_jiffies(250);
886
887 while (time_before(jiffies, timeout)) {
888 value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
889 if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
890 break;
891
892 usleep_range(25, 100);
893 }
894
895 if ((value & SOR_LANE_SEQ_CTL_TRIGGER) != 0)
896 return -ETIMEDOUT;
897
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200898 value = tegra_sor_readl(sor, SOR_PLL2);
899 value |= SOR_PLL2_PORT_POWERDOWN;
900 tegra_sor_writel(sor, value, SOR_PLL2);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100901
902 usleep_range(20, 100);
903
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200904 value = tegra_sor_readl(sor, SOR_PLL0);
905 value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
906 tegra_sor_writel(sor, value, SOR_PLL0);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100907
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200908 value = tegra_sor_readl(sor, SOR_PLL2);
909 value |= SOR_PLL2_SEQ_PLLCAPPD;
910 value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
911 tegra_sor_writel(sor, value, SOR_PLL2);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100912
913 usleep_range(20, 100);
914
915 return 0;
916}
917
Thierry Reding6fad8f62014-11-28 15:41:34 +0100918static int tegra_sor_crc_wait(struct tegra_sor *sor, unsigned long timeout)
919{
920 u32 value;
921
922 timeout = jiffies + msecs_to_jiffies(timeout);
923
924 while (time_before(jiffies, timeout)) {
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200925 value = tegra_sor_readl(sor, SOR_CRCA);
926 if (value & SOR_CRCA_VALID)
Thierry Reding6fad8f62014-11-28 15:41:34 +0100927 return 0;
928
929 usleep_range(100, 200);
930 }
931
932 return -ETIMEDOUT;
933}
934
Thierry Reding530239a2015-08-06 11:04:54 +0200935static int tegra_sor_show_crc(struct seq_file *s, void *data)
Thierry Reding6fad8f62014-11-28 15:41:34 +0100936{
Thierry Reding530239a2015-08-06 11:04:54 +0200937 struct drm_info_node *node = s->private;
938 struct tegra_sor *sor = node->info_ent->data;
Thierry Reding850bab42015-07-29 17:58:41 +0200939 struct drm_crtc *crtc = sor->output.encoder.crtc;
940 struct drm_device *drm = node->minor->dev;
Thierry Reding530239a2015-08-06 11:04:54 +0200941 int err = 0;
Thierry Reding6fad8f62014-11-28 15:41:34 +0100942 u32 value;
943
Thierry Reding850bab42015-07-29 17:58:41 +0200944 drm_modeset_lock_all(drm);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100945
Thierry Reding850bab42015-07-29 17:58:41 +0200946 if (!crtc || !crtc->state->active) {
947 err = -EBUSY;
Thierry Reding6fad8f62014-11-28 15:41:34 +0100948 goto unlock;
949 }
950
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200951 value = tegra_sor_readl(sor, SOR_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100952 value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200953 tegra_sor_writel(sor, value, SOR_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100954
955 value = tegra_sor_readl(sor, SOR_CRC_CNTRL);
956 value |= SOR_CRC_CNTRL_ENABLE;
957 tegra_sor_writel(sor, value, SOR_CRC_CNTRL);
958
959 value = tegra_sor_readl(sor, SOR_TEST);
960 value &= ~SOR_TEST_CRC_POST_SERIALIZE;
961 tegra_sor_writel(sor, value, SOR_TEST);
962
963 err = tegra_sor_crc_wait(sor, 100);
964 if (err < 0)
965 goto unlock;
966
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200967 tegra_sor_writel(sor, SOR_CRCA_RESET, SOR_CRCA);
968 value = tegra_sor_readl(sor, SOR_CRCB);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100969
Thierry Reding530239a2015-08-06 11:04:54 +0200970 seq_printf(s, "%08x\n", value);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100971
972unlock:
Thierry Reding850bab42015-07-29 17:58:41 +0200973 drm_modeset_unlock_all(drm);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100974 return err;
975}
976
Thierry Redingdab16332015-01-26 16:04:08 +0100977static int tegra_sor_show_regs(struct seq_file *s, void *data)
978{
979 struct drm_info_node *node = s->private;
980 struct tegra_sor *sor = node->info_ent->data;
Thierry Reding850bab42015-07-29 17:58:41 +0200981 struct drm_crtc *crtc = sor->output.encoder.crtc;
982 struct drm_device *drm = node->minor->dev;
983 int err = 0;
984
985 drm_modeset_lock_all(drm);
986
987 if (!crtc || !crtc->state->active) {
988 err = -EBUSY;
989 goto unlock;
990 }
Thierry Redingdab16332015-01-26 16:04:08 +0100991
992#define DUMP_REG(name) \
993 seq_printf(s, "%-38s %#05x %08x\n", #name, name, \
994 tegra_sor_readl(sor, name))
995
996 DUMP_REG(SOR_CTXSW);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200997 DUMP_REG(SOR_SUPER_STATE0);
998 DUMP_REG(SOR_SUPER_STATE1);
999 DUMP_REG(SOR_STATE0);
1000 DUMP_REG(SOR_STATE1);
1001 DUMP_REG(SOR_HEAD_STATE0(0));
1002 DUMP_REG(SOR_HEAD_STATE0(1));
1003 DUMP_REG(SOR_HEAD_STATE1(0));
1004 DUMP_REG(SOR_HEAD_STATE1(1));
1005 DUMP_REG(SOR_HEAD_STATE2(0));
1006 DUMP_REG(SOR_HEAD_STATE2(1));
1007 DUMP_REG(SOR_HEAD_STATE3(0));
1008 DUMP_REG(SOR_HEAD_STATE3(1));
1009 DUMP_REG(SOR_HEAD_STATE4(0));
1010 DUMP_REG(SOR_HEAD_STATE4(1));
1011 DUMP_REG(SOR_HEAD_STATE5(0));
1012 DUMP_REG(SOR_HEAD_STATE5(1));
Thierry Redingdab16332015-01-26 16:04:08 +01001013 DUMP_REG(SOR_CRC_CNTRL);
1014 DUMP_REG(SOR_DP_DEBUG_MVID);
1015 DUMP_REG(SOR_CLK_CNTRL);
1016 DUMP_REG(SOR_CAP);
1017 DUMP_REG(SOR_PWR);
1018 DUMP_REG(SOR_TEST);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001019 DUMP_REG(SOR_PLL0);
1020 DUMP_REG(SOR_PLL1);
1021 DUMP_REG(SOR_PLL2);
1022 DUMP_REG(SOR_PLL3);
Thierry Redingdab16332015-01-26 16:04:08 +01001023 DUMP_REG(SOR_CSTM);
1024 DUMP_REG(SOR_LVDS);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001025 DUMP_REG(SOR_CRCA);
1026 DUMP_REG(SOR_CRCB);
Thierry Redingdab16332015-01-26 16:04:08 +01001027 DUMP_REG(SOR_BLANK);
1028 DUMP_REG(SOR_SEQ_CTL);
1029 DUMP_REG(SOR_LANE_SEQ_CTL);
1030 DUMP_REG(SOR_SEQ_INST(0));
1031 DUMP_REG(SOR_SEQ_INST(1));
1032 DUMP_REG(SOR_SEQ_INST(2));
1033 DUMP_REG(SOR_SEQ_INST(3));
1034 DUMP_REG(SOR_SEQ_INST(4));
1035 DUMP_REG(SOR_SEQ_INST(5));
1036 DUMP_REG(SOR_SEQ_INST(6));
1037 DUMP_REG(SOR_SEQ_INST(7));
1038 DUMP_REG(SOR_SEQ_INST(8));
1039 DUMP_REG(SOR_SEQ_INST(9));
1040 DUMP_REG(SOR_SEQ_INST(10));
1041 DUMP_REG(SOR_SEQ_INST(11));
1042 DUMP_REG(SOR_SEQ_INST(12));
1043 DUMP_REG(SOR_SEQ_INST(13));
1044 DUMP_REG(SOR_SEQ_INST(14));
1045 DUMP_REG(SOR_SEQ_INST(15));
1046 DUMP_REG(SOR_PWM_DIV);
1047 DUMP_REG(SOR_PWM_CTL);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001048 DUMP_REG(SOR_VCRC_A0);
1049 DUMP_REG(SOR_VCRC_A1);
1050 DUMP_REG(SOR_VCRC_B0);
1051 DUMP_REG(SOR_VCRC_B1);
1052 DUMP_REG(SOR_CCRC_A0);
1053 DUMP_REG(SOR_CCRC_A1);
1054 DUMP_REG(SOR_CCRC_B0);
1055 DUMP_REG(SOR_CCRC_B1);
1056 DUMP_REG(SOR_EDATA_A0);
1057 DUMP_REG(SOR_EDATA_A1);
1058 DUMP_REG(SOR_EDATA_B0);
1059 DUMP_REG(SOR_EDATA_B1);
1060 DUMP_REG(SOR_COUNT_A0);
1061 DUMP_REG(SOR_COUNT_A1);
1062 DUMP_REG(SOR_COUNT_B0);
1063 DUMP_REG(SOR_COUNT_B1);
1064 DUMP_REG(SOR_DEBUG_A0);
1065 DUMP_REG(SOR_DEBUG_A1);
1066 DUMP_REG(SOR_DEBUG_B0);
1067 DUMP_REG(SOR_DEBUG_B1);
Thierry Redingdab16332015-01-26 16:04:08 +01001068 DUMP_REG(SOR_TRIG);
1069 DUMP_REG(SOR_MSCHECK);
1070 DUMP_REG(SOR_XBAR_CTRL);
1071 DUMP_REG(SOR_XBAR_POL);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001072 DUMP_REG(SOR_DP_LINKCTL0);
1073 DUMP_REG(SOR_DP_LINKCTL1);
1074 DUMP_REG(SOR_LANE_DRIVE_CURRENT0);
1075 DUMP_REG(SOR_LANE_DRIVE_CURRENT1);
1076 DUMP_REG(SOR_LANE4_DRIVE_CURRENT0);
1077 DUMP_REG(SOR_LANE4_DRIVE_CURRENT1);
1078 DUMP_REG(SOR_LANE_PREEMPHASIS0);
1079 DUMP_REG(SOR_LANE_PREEMPHASIS1);
1080 DUMP_REG(SOR_LANE4_PREEMPHASIS0);
1081 DUMP_REG(SOR_LANE4_PREEMPHASIS1);
1082 DUMP_REG(SOR_LANE_POSTCURSOR0);
1083 DUMP_REG(SOR_LANE_POSTCURSOR1);
1084 DUMP_REG(SOR_DP_CONFIG0);
1085 DUMP_REG(SOR_DP_CONFIG1);
1086 DUMP_REG(SOR_DP_MN0);
1087 DUMP_REG(SOR_DP_MN1);
1088 DUMP_REG(SOR_DP_PADCTL0);
1089 DUMP_REG(SOR_DP_PADCTL1);
1090 DUMP_REG(SOR_DP_DEBUG0);
1091 DUMP_REG(SOR_DP_DEBUG1);
1092 DUMP_REG(SOR_DP_SPARE0);
1093 DUMP_REG(SOR_DP_SPARE1);
Thierry Redingdab16332015-01-26 16:04:08 +01001094 DUMP_REG(SOR_DP_AUDIO_CTRL);
1095 DUMP_REG(SOR_DP_AUDIO_HBLANK_SYMBOLS);
1096 DUMP_REG(SOR_DP_AUDIO_VBLANK_SYMBOLS);
1097 DUMP_REG(SOR_DP_GENERIC_INFOFRAME_HEADER);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001098 DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK0);
1099 DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK1);
1100 DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK2);
1101 DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK3);
1102 DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK4);
1103 DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK5);
1104 DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK6);
Thierry Redingdab16332015-01-26 16:04:08 +01001105 DUMP_REG(SOR_DP_TPG);
1106 DUMP_REG(SOR_DP_TPG_CONFIG);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001107 DUMP_REG(SOR_DP_LQ_CSTM0);
1108 DUMP_REG(SOR_DP_LQ_CSTM1);
1109 DUMP_REG(SOR_DP_LQ_CSTM2);
Thierry Redingdab16332015-01-26 16:04:08 +01001110
1111#undef DUMP_REG
1112
Thierry Reding850bab42015-07-29 17:58:41 +02001113unlock:
1114 drm_modeset_unlock_all(drm);
1115 return err;
Thierry Redingdab16332015-01-26 16:04:08 +01001116}
1117
1118static const struct drm_info_list debugfs_files[] = {
Thierry Reding530239a2015-08-06 11:04:54 +02001119 { "crc", tegra_sor_show_crc, 0, NULL },
Thierry Redingdab16332015-01-26 16:04:08 +01001120 { "regs", tegra_sor_show_regs, 0, NULL },
1121};
1122
Thierry Reding6fad8f62014-11-28 15:41:34 +01001123static int tegra_sor_debugfs_init(struct tegra_sor *sor,
1124 struct drm_minor *minor)
1125{
Thierry Reding459cc2c2015-07-30 10:34:24 +02001126 const char *name = sor->soc->supports_dp ? "sor1" : "sor";
Thierry Redingdab16332015-01-26 16:04:08 +01001127 unsigned int i;
Thierry Reding530239a2015-08-06 11:04:54 +02001128 int err;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001129
Thierry Reding459cc2c2015-07-30 10:34:24 +02001130 sor->debugfs = debugfs_create_dir(name, minor->debugfs_root);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001131 if (!sor->debugfs)
1132 return -ENOMEM;
1133
Thierry Redingdab16332015-01-26 16:04:08 +01001134 sor->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1135 GFP_KERNEL);
1136 if (!sor->debugfs_files) {
Thierry Reding6fad8f62014-11-28 15:41:34 +01001137 err = -ENOMEM;
1138 goto remove;
1139 }
1140
Thierry Redingdab16332015-01-26 16:04:08 +01001141 for (i = 0; i < ARRAY_SIZE(debugfs_files); i++)
1142 sor->debugfs_files[i].data = sor;
1143
1144 err = drm_debugfs_create_files(sor->debugfs_files,
1145 ARRAY_SIZE(debugfs_files),
1146 sor->debugfs, minor);
1147 if (err < 0)
1148 goto free;
1149
Thierry Reding3ff1f222015-07-03 14:14:29 +02001150 sor->minor = minor;
1151
Thierry Reding530239a2015-08-06 11:04:54 +02001152 return 0;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001153
Thierry Redingdab16332015-01-26 16:04:08 +01001154free:
1155 kfree(sor->debugfs_files);
1156 sor->debugfs_files = NULL;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001157remove:
Thierry Redingdab16332015-01-26 16:04:08 +01001158 debugfs_remove_recursive(sor->debugfs);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001159 sor->debugfs = NULL;
1160 return err;
1161}
1162
Thierry Reding4009c222014-12-19 15:47:30 +01001163static void tegra_sor_debugfs_exit(struct tegra_sor *sor)
Thierry Reding6fad8f62014-11-28 15:41:34 +01001164{
Thierry Redingdab16332015-01-26 16:04:08 +01001165 drm_debugfs_remove_files(sor->debugfs_files, ARRAY_SIZE(debugfs_files),
1166 sor->minor);
1167 sor->minor = NULL;
1168
1169 kfree(sor->debugfs_files);
Thierry Reding066d30f2015-07-03 14:16:30 +02001170 sor->debugfs_files = NULL;
Thierry Redingdab16332015-01-26 16:04:08 +01001171
1172 debugfs_remove_recursive(sor->debugfs);
Thierry Reding066d30f2015-07-03 14:16:30 +02001173 sor->debugfs = NULL;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001174}
1175
Thierry Reding6fad8f62014-11-28 15:41:34 +01001176static enum drm_connector_status
1177tegra_sor_connector_detect(struct drm_connector *connector, bool force)
1178{
1179 struct tegra_output *output = connector_to_output(connector);
1180 struct tegra_sor *sor = to_sor(output);
1181
Thierry Reding9542c232015-07-08 13:39:09 +02001182 if (sor->aux)
1183 return drm_dp_aux_detect(sor->aux);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001184
Thierry Reding459cc2c2015-07-30 10:34:24 +02001185 return tegra_output_connector_detect(connector, force);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001186}
1187
1188static const struct drm_connector_funcs tegra_sor_connector_funcs = {
Thierry Reding850bab42015-07-29 17:58:41 +02001189 .dpms = drm_atomic_helper_connector_dpms,
Thierry Reding9d441892014-11-24 17:02:53 +01001190 .reset = drm_atomic_helper_connector_reset,
Thierry Reding6fad8f62014-11-28 15:41:34 +01001191 .detect = tegra_sor_connector_detect,
1192 .fill_modes = drm_helper_probe_single_connector_modes,
1193 .destroy = tegra_output_connector_destroy,
Thierry Reding9d441892014-11-24 17:02:53 +01001194 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
Thierry Reding4aa3df72014-11-24 16:27:13 +01001195 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
Thierry Reding6fad8f62014-11-28 15:41:34 +01001196};
1197
1198static int tegra_sor_connector_get_modes(struct drm_connector *connector)
1199{
1200 struct tegra_output *output = connector_to_output(connector);
1201 struct tegra_sor *sor = to_sor(output);
1202 int err;
1203
Thierry Reding9542c232015-07-08 13:39:09 +02001204 if (sor->aux)
1205 drm_dp_aux_enable(sor->aux);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001206
1207 err = tegra_output_connector_get_modes(connector);
1208
Thierry Reding9542c232015-07-08 13:39:09 +02001209 if (sor->aux)
1210 drm_dp_aux_disable(sor->aux);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001211
1212 return err;
1213}
1214
1215static enum drm_mode_status
1216tegra_sor_connector_mode_valid(struct drm_connector *connector,
1217 struct drm_display_mode *mode)
1218{
1219 return MODE_OK;
1220}
1221
1222static const struct drm_connector_helper_funcs tegra_sor_connector_helper_funcs = {
1223 .get_modes = tegra_sor_connector_get_modes,
1224 .mode_valid = tegra_sor_connector_mode_valid,
1225 .best_encoder = tegra_output_connector_best_encoder,
1226};
1227
1228static const struct drm_encoder_funcs tegra_sor_encoder_funcs = {
1229 .destroy = tegra_output_encoder_destroy,
1230};
1231
Thierry Reding850bab42015-07-29 17:58:41 +02001232static void tegra_sor_edp_disable(struct drm_encoder *encoder)
Thierry Reding6fad8f62014-11-28 15:41:34 +01001233{
Thierry Reding850bab42015-07-29 17:58:41 +02001234 struct tegra_output *output = encoder_to_output(encoder);
1235 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
1236 struct tegra_sor *sor = to_sor(output);
1237 u32 value;
1238 int err;
1239
1240 if (output->panel)
1241 drm_panel_disable(output->panel);
1242
1243 err = tegra_sor_detach(sor);
1244 if (err < 0)
1245 dev_err(sor->dev, "failed to detach SOR: %d\n", err);
1246
1247 tegra_sor_writel(sor, 0, SOR_STATE1);
1248 tegra_sor_update(sor);
1249
1250 /*
1251 * The following accesses registers of the display controller, so make
1252 * sure it's only executed when the output is attached to one.
1253 */
1254 if (dc) {
1255 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
1256 value &= ~SOR_ENABLE;
1257 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
1258
1259 tegra_dc_commit(dc);
1260 }
1261
1262 err = tegra_sor_power_down(sor);
1263 if (err < 0)
1264 dev_err(sor->dev, "failed to power down SOR: %d\n", err);
1265
Thierry Reding9542c232015-07-08 13:39:09 +02001266 if (sor->aux) {
1267 err = drm_dp_aux_disable(sor->aux);
Thierry Reding850bab42015-07-29 17:58:41 +02001268 if (err < 0)
1269 dev_err(sor->dev, "failed to disable DP: %d\n", err);
1270 }
1271
1272 err = tegra_io_rail_power_off(TEGRA_IO_RAIL_LVDS);
1273 if (err < 0)
1274 dev_err(sor->dev, "failed to power off I/O rail: %d\n", err);
1275
1276 if (output->panel)
1277 drm_panel_unprepare(output->panel);
1278
1279 reset_control_assert(sor->rst);
1280 clk_disable_unprepare(sor->clk);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001281}
1282
Thierry Reding459cc2c2015-07-30 10:34:24 +02001283#if 0
1284static int calc_h_ref_to_sync(const struct drm_display_mode *mode,
1285 unsigned int *value)
1286{
1287 unsigned int hfp, hsw, hbp, a = 0, b;
1288
1289 hfp = mode->hsync_start - mode->hdisplay;
1290 hsw = mode->hsync_end - mode->hsync_start;
1291 hbp = mode->htotal - mode->hsync_end;
1292
1293 pr_info("hfp: %u, hsw: %u, hbp: %u\n", hfp, hsw, hbp);
1294
1295 b = hfp - 1;
1296
1297 pr_info("a: %u, b: %u\n", a, b);
1298 pr_info("a + hsw + hbp = %u\n", a + hsw + hbp);
1299
1300 if (a + hsw + hbp <= 11) {
1301 a = 1 + 11 - hsw - hbp;
1302 pr_info("a: %u\n", a);
1303 }
1304
1305 if (a > b)
1306 return -EINVAL;
1307
1308 if (hsw < 1)
1309 return -EINVAL;
1310
1311 if (mode->hdisplay < 16)
1312 return -EINVAL;
1313
1314 if (value) {
1315 if (b > a && a % 2)
1316 *value = a + 1;
1317 else
1318 *value = a;
1319 }
1320
1321 return 0;
1322}
1323#endif
1324
Thierry Reding850bab42015-07-29 17:58:41 +02001325static void tegra_sor_edp_enable(struct drm_encoder *encoder)
Thierry Reding6fad8f62014-11-28 15:41:34 +01001326{
Thierry Reding850bab42015-07-29 17:58:41 +02001327 struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001328 struct tegra_output *output = encoder_to_output(encoder);
1329 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001330 struct tegra_sor *sor = to_sor(output);
Thierry Reding34fa1832014-06-05 16:31:10 +02001331 struct tegra_sor_config config;
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001332 struct drm_display_info *info;
Thierry Reding34fa1832014-06-05 16:31:10 +02001333 struct drm_dp_link link;
Thierry Reding01b9bea2015-11-11 17:15:29 +01001334 u8 rate, lanes;
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001335 unsigned int i;
Thierry Reding86f5c522014-03-26 11:13:16 +01001336 int err = 0;
Thierry Reding28fe2072015-01-26 16:02:48 +01001337 u32 value;
Thierry Reding86f5c522014-03-26 11:13:16 +01001338
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001339 info = &output->connector.display_info;
1340
Thierry Reding6b6b6042013-11-15 16:06:05 +01001341 err = clk_prepare_enable(sor->clk);
1342 if (err < 0)
Thierry Reding850bab42015-07-29 17:58:41 +02001343 dev_err(sor->dev, "failed to enable clock: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001344
1345 reset_control_deassert(sor->rst);
1346
Thierry Reding6fad8f62014-11-28 15:41:34 +01001347 if (output->panel)
1348 drm_panel_prepare(output->panel);
1349
Thierry Reding01b9bea2015-11-11 17:15:29 +01001350 err = drm_dp_aux_enable(sor->aux);
1351 if (err < 0)
1352 dev_err(sor->dev, "failed to enable DP: %d\n", err);
Thierry Reding34fa1832014-06-05 16:31:10 +02001353
Thierry Reding01b9bea2015-11-11 17:15:29 +01001354 err = drm_dp_link_probe(sor->aux, &link);
1355 if (err < 0) {
1356 dev_err(sor->dev, "failed to probe eDP link: %d\n", err);
1357 return;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001358 }
1359
Thierry Reding25bb2ce2015-08-03 14:23:29 +02001360 /* switch to safe parent clock */
1361 err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001362 if (err < 0)
1363 dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
1364
Thierry Reding34fa1832014-06-05 16:31:10 +02001365 memset(&config, 0, sizeof(config));
Stéphane Marchesin054b1bd2014-06-19 18:18:29 -07001366 config.bits_per_pixel = output->connector.display_info.bpc * 3;
Thierry Reding34fa1832014-06-05 16:31:10 +02001367
Thierry Redinga1983592015-07-21 16:46:52 +02001368 err = tegra_sor_compute_config(sor, mode, &config, &link);
Thierry Reding34fa1832014-06-05 16:31:10 +02001369 if (err < 0)
Thierry Redinga1983592015-07-21 16:46:52 +02001370 dev_err(sor->dev, "failed to compute configuration: %d\n", err);
Thierry Reding34fa1832014-06-05 16:31:10 +02001371
Thierry Reding6b6b6042013-11-15 16:06:05 +01001372 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
1373 value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
1374 value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
1375 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
1376
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001377 value = tegra_sor_readl(sor, SOR_PLL2);
1378 value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
1379 tegra_sor_writel(sor, value, SOR_PLL2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001380 usleep_range(20, 100);
1381
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001382 value = tegra_sor_readl(sor, SOR_PLL3);
1383 value |= SOR_PLL3_PLL_VDD_MODE_3V3;
1384 tegra_sor_writel(sor, value, SOR_PLL3);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001385
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001386 value = SOR_PLL0_ICHPMP(0xf) | SOR_PLL0_VCOCAP_RST |
1387 SOR_PLL0_PLLREG_LEVEL_V45 | SOR_PLL0_RESISTOR_EXT;
1388 tegra_sor_writel(sor, value, SOR_PLL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001389
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001390 value = tegra_sor_readl(sor, SOR_PLL2);
1391 value |= SOR_PLL2_SEQ_PLLCAPPD;
1392 value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1393 value |= SOR_PLL2_LVDS_ENABLE;
1394 tegra_sor_writel(sor, value, SOR_PLL2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001395
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001396 value = SOR_PLL1_TERM_COMPOUT | SOR_PLL1_TMDS_TERM;
1397 tegra_sor_writel(sor, value, SOR_PLL1);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001398
1399 while (true) {
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001400 value = tegra_sor_readl(sor, SOR_PLL2);
1401 if ((value & SOR_PLL2_SEQ_PLLCAPPD_ENFORCE) == 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001402 break;
1403
1404 usleep_range(250, 1000);
1405 }
1406
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001407 value = tegra_sor_readl(sor, SOR_PLL2);
1408 value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
1409 value &= ~SOR_PLL2_PORT_POWERDOWN;
1410 tegra_sor_writel(sor, value, SOR_PLL2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001411
1412 /*
1413 * power up
1414 */
1415
1416 /* set safe link bandwidth (1.62 Gbps) */
1417 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
1418 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
1419 value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G1_62;
1420 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
1421
1422 /* step 1 */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001423 value = tegra_sor_readl(sor, SOR_PLL2);
1424 value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE | SOR_PLL2_PORT_POWERDOWN |
1425 SOR_PLL2_BANDGAP_POWERDOWN;
1426 tegra_sor_writel(sor, value, SOR_PLL2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001427
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001428 value = tegra_sor_readl(sor, SOR_PLL0);
1429 value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
1430 tegra_sor_writel(sor, value, SOR_PLL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001431
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001432 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001433 value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001434 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001435
1436 /* step 2 */
1437 err = tegra_io_rail_power_on(TEGRA_IO_RAIL_LVDS);
Thierry Reding850bab42015-07-29 17:58:41 +02001438 if (err < 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001439 dev_err(sor->dev, "failed to power on I/O rail: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001440
1441 usleep_range(5, 100);
1442
1443 /* step 3 */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001444 value = tegra_sor_readl(sor, SOR_PLL2);
1445 value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
1446 tegra_sor_writel(sor, value, SOR_PLL2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001447
1448 usleep_range(20, 100);
1449
1450 /* step 4 */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001451 value = tegra_sor_readl(sor, SOR_PLL0);
1452 value &= ~SOR_PLL0_VCOPD;
1453 value &= ~SOR_PLL0_PWR;
1454 tegra_sor_writel(sor, value, SOR_PLL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001455
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001456 value = tegra_sor_readl(sor, SOR_PLL2);
1457 value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1458 tegra_sor_writel(sor, value, SOR_PLL2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001459
1460 usleep_range(200, 1000);
1461
1462 /* step 5 */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001463 value = tegra_sor_readl(sor, SOR_PLL2);
1464 value &= ~SOR_PLL2_PORT_POWERDOWN;
1465 tegra_sor_writel(sor, value, SOR_PLL2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001466
Thierry Reding25bb2ce2015-08-03 14:23:29 +02001467 /* switch to DP parent clock */
1468 err = tegra_sor_set_parent_clock(sor, sor->clk_dp);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001469 if (err < 0)
Thierry Reding25bb2ce2015-08-03 14:23:29 +02001470 dev_err(sor->dev, "failed to set parent clock: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001471
Thierry Reding899451b2014-06-05 16:19:48 +02001472 /* power DP lanes */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001473 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
Thierry Reding899451b2014-06-05 16:19:48 +02001474
1475 if (link.num_lanes <= 2)
1476 value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2);
1477 else
1478 value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2;
1479
1480 if (link.num_lanes <= 1)
1481 value &= ~SOR_DP_PADCTL_PD_TXD_1;
1482 else
1483 value |= SOR_DP_PADCTL_PD_TXD_1;
1484
1485 if (link.num_lanes == 0)
1486 value &= ~SOR_DP_PADCTL_PD_TXD_0;
1487 else
1488 value |= SOR_DP_PADCTL_PD_TXD_0;
1489
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001490 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001491
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001492 value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001493 value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
Thierry Reding0c90a182014-06-05 16:29:46 +02001494 value |= SOR_DP_LINKCTL_LANE_COUNT(link.num_lanes);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001495 tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001496
1497 /* start lane sequencer */
1498 value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
1499 SOR_LANE_SEQ_CTL_POWER_STATE_UP;
1500 tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
1501
1502 while (true) {
1503 value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
1504 if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
1505 break;
1506
1507 usleep_range(250, 1000);
1508 }
1509
Thierry Redinga4263fe2014-06-05 16:16:23 +02001510 /* set link bandwidth */
Thierry Reding6b6b6042013-11-15 16:06:05 +01001511 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
1512 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
Thierry Redinga4263fe2014-06-05 16:16:23 +02001513 value |= drm_dp_link_rate_to_bw_code(link.rate) << 2;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001514 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
1515
Thierry Reding402f6bc2015-07-21 16:48:19 +02001516 tegra_sor_apply_config(sor, &config);
1517
1518 /* enable link */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001519 value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001520 value |= SOR_DP_LINKCTL_ENABLE;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001521 value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001522 tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001523
1524 for (i = 0, value = 0; i < 4; i++) {
1525 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
1526 SOR_DP_TPG_SCRAMBLER_GALIOS |
1527 SOR_DP_TPG_PATTERN_NONE;
1528 value = (value << 8) | lane;
1529 }
1530
1531 tegra_sor_writel(sor, value, SOR_DP_TPG);
1532
Thierry Reding6b6b6042013-11-15 16:06:05 +01001533 /* enable pad calibration logic */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001534 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001535 value |= SOR_DP_PADCTL_PAD_CAL_PD;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001536 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001537
Thierry Reding01b9bea2015-11-11 17:15:29 +01001538 err = drm_dp_link_probe(sor->aux, &link);
1539 if (err < 0)
1540 dev_err(sor->dev, "failed to probe eDP link: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001541
Thierry Reding01b9bea2015-11-11 17:15:29 +01001542 err = drm_dp_link_power_up(sor->aux, &link);
1543 if (err < 0)
1544 dev_err(sor->dev, "failed to power up eDP link: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001545
Thierry Reding01b9bea2015-11-11 17:15:29 +01001546 err = drm_dp_link_configure(sor->aux, &link);
1547 if (err < 0)
1548 dev_err(sor->dev, "failed to configure eDP link: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001549
Thierry Reding01b9bea2015-11-11 17:15:29 +01001550 rate = drm_dp_link_rate_to_bw_code(link.rate);
1551 lanes = link.num_lanes;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001552
Thierry Reding01b9bea2015-11-11 17:15:29 +01001553 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
1554 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
1555 value |= SOR_CLK_CNTRL_DP_LINK_SPEED(rate);
1556 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001557
Thierry Reding01b9bea2015-11-11 17:15:29 +01001558 value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
1559 value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
1560 value |= SOR_DP_LINKCTL_LANE_COUNT(lanes);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001561
Thierry Reding01b9bea2015-11-11 17:15:29 +01001562 if (link.capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
1563 value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001564
Thierry Reding01b9bea2015-11-11 17:15:29 +01001565 tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001566
Thierry Reding01b9bea2015-11-11 17:15:29 +01001567 /* disable training pattern generator */
Thierry Reding6b6b6042013-11-15 16:06:05 +01001568
Thierry Reding01b9bea2015-11-11 17:15:29 +01001569 for (i = 0; i < link.num_lanes; i++) {
1570 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
1571 SOR_DP_TPG_SCRAMBLER_GALIOS |
1572 SOR_DP_TPG_PATTERN_NONE;
1573 value = (value << 8) | lane;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001574 }
1575
Thierry Reding01b9bea2015-11-11 17:15:29 +01001576 tegra_sor_writel(sor, value, SOR_DP_TPG);
1577
1578 err = tegra_sor_dp_train_fast(sor, &link);
1579 if (err < 0)
1580 dev_err(sor->dev, "DP fast link training failed: %d\n", err);
1581
1582 dev_dbg(sor->dev, "fast link training succeeded\n");
1583
Thierry Reding6b6b6042013-11-15 16:06:05 +01001584 err = tegra_sor_power_up(sor, 250);
Thierry Reding850bab42015-07-29 17:58:41 +02001585 if (err < 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001586 dev_err(sor->dev, "failed to power up SOR: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001587
Thierry Reding6b6b6042013-11-15 16:06:05 +01001588 /* CSTM (LVDS, link A/B, upper) */
Stéphane Marchesin143b1df2014-05-22 20:32:47 -07001589 value = SOR_CSTM_LVDS | SOR_CSTM_LINK_ACT_A | SOR_CSTM_LINK_ACT_B |
Thierry Reding6b6b6042013-11-15 16:06:05 +01001590 SOR_CSTM_UPPER;
1591 tegra_sor_writel(sor, value, SOR_CSTM);
1592
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001593 /* use DP-A protocol */
1594 value = tegra_sor_readl(sor, SOR_STATE1);
1595 value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
1596 value |= SOR_STATE_ASY_PROTOCOL_DP_A;
1597 tegra_sor_writel(sor, value, SOR_STATE1);
1598
1599 tegra_sor_mode_set(sor, mode, info);
1600
Thierry Reding6b6b6042013-11-15 16:06:05 +01001601 /* PWM setup */
1602 err = tegra_sor_setup_pwm(sor, 250);
Thierry Reding850bab42015-07-29 17:58:41 +02001603 if (err < 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001604 dev_err(sor->dev, "failed to setup PWM: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001605
Thierry Reding666cb872014-12-08 16:32:47 +01001606 tegra_sor_update(sor);
1607
Thierry Reding6b6b6042013-11-15 16:06:05 +01001608 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
1609 value |= SOR_ENABLE;
1610 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
1611
Thierry Reding666cb872014-12-08 16:32:47 +01001612 tegra_dc_commit(dc);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001613
1614 err = tegra_sor_attach(sor);
Thierry Reding850bab42015-07-29 17:58:41 +02001615 if (err < 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001616 dev_err(sor->dev, "failed to attach SOR: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001617
1618 err = tegra_sor_wakeup(sor);
Thierry Reding850bab42015-07-29 17:58:41 +02001619 if (err < 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001620 dev_err(sor->dev, "failed to enable DC: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001621
Thierry Reding6fad8f62014-11-28 15:41:34 +01001622 if (output->panel)
1623 drm_panel_enable(output->panel);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001624}
1625
Thierry Reding82f15112014-12-08 17:26:46 +01001626static int
1627tegra_sor_encoder_atomic_check(struct drm_encoder *encoder,
1628 struct drm_crtc_state *crtc_state,
1629 struct drm_connector_state *conn_state)
1630{
1631 struct tegra_output *output = encoder_to_output(encoder);
1632 struct tegra_dc *dc = to_tegra_dc(conn_state->crtc);
1633 unsigned long pclk = crtc_state->mode.clock * 1000;
1634 struct tegra_sor *sor = to_sor(output);
1635 int err;
1636
1637 err = tegra_dc_state_setup_clock(dc, crtc_state, sor->clk_parent,
1638 pclk, 0);
1639 if (err < 0) {
1640 dev_err(output->dev, "failed to setup CRTC state: %d\n", err);
1641 return err;
1642 }
1643
1644 return 0;
1645}
1646
Thierry Reding459cc2c2015-07-30 10:34:24 +02001647static const struct drm_encoder_helper_funcs tegra_sor_edp_helpers = {
Thierry Reding850bab42015-07-29 17:58:41 +02001648 .disable = tegra_sor_edp_disable,
1649 .enable = tegra_sor_edp_enable,
Thierry Reding82f15112014-12-08 17:26:46 +01001650 .atomic_check = tegra_sor_encoder_atomic_check,
Thierry Reding6b6b6042013-11-15 16:06:05 +01001651};
1652
Thierry Reding459cc2c2015-07-30 10:34:24 +02001653static inline u32 tegra_sor_hdmi_subpack(const u8 *ptr, size_t size)
1654{
1655 u32 value = 0;
1656 size_t i;
1657
1658 for (i = size; i > 0; i--)
1659 value = (value << 8) | ptr[i - 1];
1660
1661 return value;
1662}
1663
1664static void tegra_sor_hdmi_write_infopack(struct tegra_sor *sor,
1665 const void *data, size_t size)
1666{
1667 const u8 *ptr = data;
1668 unsigned long offset;
1669 size_t i, j;
1670 u32 value;
1671
1672 switch (ptr[0]) {
1673 case HDMI_INFOFRAME_TYPE_AVI:
1674 offset = SOR_HDMI_AVI_INFOFRAME_HEADER;
1675 break;
1676
1677 case HDMI_INFOFRAME_TYPE_AUDIO:
1678 offset = SOR_HDMI_AUDIO_INFOFRAME_HEADER;
1679 break;
1680
1681 case HDMI_INFOFRAME_TYPE_VENDOR:
1682 offset = SOR_HDMI_VSI_INFOFRAME_HEADER;
1683 break;
1684
1685 default:
1686 dev_err(sor->dev, "unsupported infoframe type: %02x\n",
1687 ptr[0]);
1688 return;
1689 }
1690
1691 value = INFOFRAME_HEADER_TYPE(ptr[0]) |
1692 INFOFRAME_HEADER_VERSION(ptr[1]) |
1693 INFOFRAME_HEADER_LEN(ptr[2]);
1694 tegra_sor_writel(sor, value, offset);
1695 offset++;
1696
1697 /*
1698 * Each subpack contains 7 bytes, divided into:
1699 * - subpack_low: bytes 0 - 3
1700 * - subpack_high: bytes 4 - 6 (with byte 7 padded to 0x00)
1701 */
1702 for (i = 3, j = 0; i < size; i += 7, j += 8) {
1703 size_t rem = size - i, num = min_t(size_t, rem, 4);
1704
1705 value = tegra_sor_hdmi_subpack(&ptr[i], num);
1706 tegra_sor_writel(sor, value, offset++);
1707
1708 num = min_t(size_t, rem - num, 3);
1709
1710 value = tegra_sor_hdmi_subpack(&ptr[i + 4], num);
1711 tegra_sor_writel(sor, value, offset++);
1712 }
1713}
1714
1715static int
1716tegra_sor_hdmi_setup_avi_infoframe(struct tegra_sor *sor,
1717 const struct drm_display_mode *mode)
1718{
1719 u8 buffer[HDMI_INFOFRAME_SIZE(AVI)];
1720 struct hdmi_avi_infoframe frame;
1721 u32 value;
1722 int err;
1723
1724 /* disable AVI infoframe */
1725 value = tegra_sor_readl(sor, SOR_HDMI_AVI_INFOFRAME_CTRL);
1726 value &= ~INFOFRAME_CTRL_SINGLE;
1727 value &= ~INFOFRAME_CTRL_OTHER;
1728 value &= ~INFOFRAME_CTRL_ENABLE;
1729 tegra_sor_writel(sor, value, SOR_HDMI_AVI_INFOFRAME_CTRL);
1730
1731 err = drm_hdmi_avi_infoframe_from_display_mode(&frame, mode);
1732 if (err < 0) {
1733 dev_err(sor->dev, "failed to setup AVI infoframe: %d\n", err);
1734 return err;
1735 }
1736
1737 err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
1738 if (err < 0) {
1739 dev_err(sor->dev, "failed to pack AVI infoframe: %d\n", err);
1740 return err;
1741 }
1742
1743 tegra_sor_hdmi_write_infopack(sor, buffer, err);
1744
1745 /* enable AVI infoframe */
1746 value = tegra_sor_readl(sor, SOR_HDMI_AVI_INFOFRAME_CTRL);
1747 value |= INFOFRAME_CTRL_CHECKSUM_ENABLE;
1748 value |= INFOFRAME_CTRL_ENABLE;
1749 tegra_sor_writel(sor, value, SOR_HDMI_AVI_INFOFRAME_CTRL);
1750
1751 return 0;
1752}
1753
1754static void tegra_sor_hdmi_disable_audio_infoframe(struct tegra_sor *sor)
1755{
1756 u32 value;
1757
1758 value = tegra_sor_readl(sor, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
1759 value &= ~INFOFRAME_CTRL_ENABLE;
1760 tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
1761}
1762
1763static struct tegra_sor_hdmi_settings *
1764tegra_sor_hdmi_find_settings(struct tegra_sor *sor, unsigned long frequency)
1765{
1766 unsigned int i;
1767
1768 for (i = 0; i < sor->num_settings; i++)
1769 if (frequency <= sor->settings[i].frequency)
1770 return &sor->settings[i];
1771
1772 return NULL;
1773}
1774
1775static void tegra_sor_hdmi_disable(struct drm_encoder *encoder)
1776{
1777 struct tegra_output *output = encoder_to_output(encoder);
1778 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
1779 struct tegra_sor *sor = to_sor(output);
1780 u32 value;
1781 int err;
1782
1783 err = tegra_sor_detach(sor);
1784 if (err < 0)
1785 dev_err(sor->dev, "failed to detach SOR: %d\n", err);
1786
1787 tegra_sor_writel(sor, 0, SOR_STATE1);
1788 tegra_sor_update(sor);
1789
1790 /* disable display to SOR clock */
1791 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
1792 value &= ~SOR1_TIMING_CYA;
1793 value &= ~SOR1_ENABLE;
1794 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
1795
1796 tegra_dc_commit(dc);
1797
1798 err = tegra_sor_power_down(sor);
1799 if (err < 0)
1800 dev_err(sor->dev, "failed to power down SOR: %d\n", err);
1801
1802 err = tegra_io_rail_power_off(TEGRA_IO_RAIL_HDMI);
1803 if (err < 0)
1804 dev_err(sor->dev, "failed to power off HDMI rail: %d\n", err);
1805
1806 reset_control_assert(sor->rst);
1807 usleep_range(1000, 2000);
1808 clk_disable_unprepare(sor->clk);
1809}
1810
1811static void tegra_sor_hdmi_enable(struct drm_encoder *encoder)
1812{
1813 struct tegra_output *output = encoder_to_output(encoder);
1814 unsigned int h_ref_to_sync = 1, pulse_start, max_ac;
1815 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
Thierry Reding459cc2c2015-07-30 10:34:24 +02001816 struct tegra_sor_hdmi_settings *settings;
1817 struct tegra_sor *sor = to_sor(output);
1818 struct drm_display_mode *mode;
1819 struct drm_display_info *info;
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001820 unsigned int div;
Thierry Reding459cc2c2015-07-30 10:34:24 +02001821 u32 value;
1822 int err;
1823
1824 mode = &encoder->crtc->state->adjusted_mode;
1825 info = &output->connector.display_info;
1826
1827 err = clk_prepare_enable(sor->clk);
1828 if (err < 0)
1829 dev_err(sor->dev, "failed to enable clock: %d\n", err);
1830
1831 usleep_range(1000, 2000);
1832
1833 reset_control_deassert(sor->rst);
1834
Thierry Reding25bb2ce2015-08-03 14:23:29 +02001835 /* switch to safe parent clock */
1836 err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
Thierry Reding459cc2c2015-07-30 10:34:24 +02001837 if (err < 0)
1838 dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
1839
1840 div = clk_get_rate(sor->clk) / 1000000 * 4;
1841
1842 err = tegra_io_rail_power_on(TEGRA_IO_RAIL_HDMI);
1843 if (err < 0)
1844 dev_err(sor->dev, "failed to power on HDMI rail: %d\n", err);
1845
1846 usleep_range(20, 100);
1847
1848 value = tegra_sor_readl(sor, SOR_PLL2);
1849 value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
1850 tegra_sor_writel(sor, value, SOR_PLL2);
1851
1852 usleep_range(20, 100);
1853
1854 value = tegra_sor_readl(sor, SOR_PLL3);
1855 value &= ~SOR_PLL3_PLL_VDD_MODE_3V3;
1856 tegra_sor_writel(sor, value, SOR_PLL3);
1857
1858 value = tegra_sor_readl(sor, SOR_PLL0);
1859 value &= ~SOR_PLL0_VCOPD;
1860 value &= ~SOR_PLL0_PWR;
1861 tegra_sor_writel(sor, value, SOR_PLL0);
1862
1863 value = tegra_sor_readl(sor, SOR_PLL2);
1864 value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1865 tegra_sor_writel(sor, value, SOR_PLL2);
1866
1867 usleep_range(200, 400);
1868
1869 value = tegra_sor_readl(sor, SOR_PLL2);
1870 value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
1871 value &= ~SOR_PLL2_PORT_POWERDOWN;
1872 tegra_sor_writel(sor, value, SOR_PLL2);
1873
1874 usleep_range(20, 100);
1875
1876 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
1877 value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
1878 SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2;
1879 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
1880
1881 while (true) {
1882 value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
1883 if ((value & SOR_LANE_SEQ_CTL_STATE_BUSY) == 0)
1884 break;
1885
1886 usleep_range(250, 1000);
1887 }
1888
1889 value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
1890 SOR_LANE_SEQ_CTL_POWER_STATE_UP | SOR_LANE_SEQ_CTL_DELAY(5);
1891 tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
1892
1893 while (true) {
1894 value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
1895 if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
1896 break;
1897
1898 usleep_range(250, 1000);
1899 }
1900
1901 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
1902 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
1903 value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
1904
1905 if (mode->clock < 340000)
1906 value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G2_70;
1907 else
1908 value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G5_40;
1909
1910 value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK;
1911 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
1912
1913 value = tegra_sor_readl(sor, SOR_DP_SPARE0);
1914 value |= SOR_DP_SPARE_DISP_VIDEO_PREAMBLE;
1915 value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
1916 value |= SOR_DP_SPARE_SEQ_ENABLE;
1917 tegra_sor_writel(sor, value, SOR_DP_SPARE0);
1918
1919 value = SOR_SEQ_CTL_PU_PC(0) | SOR_SEQ_CTL_PU_PC_ALT(0) |
1920 SOR_SEQ_CTL_PD_PC(8) | SOR_SEQ_CTL_PD_PC_ALT(8);
1921 tegra_sor_writel(sor, value, SOR_SEQ_CTL);
1922
1923 value = SOR_SEQ_INST_DRIVE_PWM_OUT_LO | SOR_SEQ_INST_HALT |
1924 SOR_SEQ_INST_WAIT_VSYNC | SOR_SEQ_INST_WAIT(1);
1925 tegra_sor_writel(sor, value, SOR_SEQ_INST(0));
1926 tegra_sor_writel(sor, value, SOR_SEQ_INST(8));
1927
1928 /* program the reference clock */
1929 value = SOR_REFCLK_DIV_INT(div) | SOR_REFCLK_DIV_FRAC(div);
1930 tegra_sor_writel(sor, value, SOR_REFCLK);
1931
1932 /* XXX don't hardcode */
1933 value = SOR_XBAR_CTRL_LINK1_XSEL(4, 4) |
1934 SOR_XBAR_CTRL_LINK1_XSEL(3, 3) |
1935 SOR_XBAR_CTRL_LINK1_XSEL(2, 2) |
1936 SOR_XBAR_CTRL_LINK1_XSEL(1, 1) |
1937 SOR_XBAR_CTRL_LINK1_XSEL(0, 0) |
1938 SOR_XBAR_CTRL_LINK0_XSEL(4, 4) |
1939 SOR_XBAR_CTRL_LINK0_XSEL(3, 3) |
1940 SOR_XBAR_CTRL_LINK0_XSEL(2, 0) |
1941 SOR_XBAR_CTRL_LINK0_XSEL(1, 1) |
1942 SOR_XBAR_CTRL_LINK0_XSEL(0, 2);
1943 tegra_sor_writel(sor, value, SOR_XBAR_CTRL);
1944
1945 tegra_sor_writel(sor, 0x00000000, SOR_XBAR_POL);
1946
Thierry Reding25bb2ce2015-08-03 14:23:29 +02001947 /* switch to parent clock */
1948 err = tegra_sor_set_parent_clock(sor, sor->clk_parent);
Thierry Reding459cc2c2015-07-30 10:34:24 +02001949 if (err < 0)
1950 dev_err(sor->dev, "failed to set parent clock: %d\n", err);
1951
1952 value = SOR_INPUT_CONTROL_HDMI_SRC_SELECT(dc->pipe);
1953
1954 /* XXX is this the proper check? */
1955 if (mode->clock < 75000)
1956 value |= SOR_INPUT_CONTROL_ARM_VIDEO_RANGE_LIMITED;
1957
1958 tegra_sor_writel(sor, value, SOR_INPUT_CONTROL);
1959
1960 max_ac = ((mode->htotal - mode->hdisplay) - SOR_REKEY - 18) / 32;
1961
1962 value = SOR_HDMI_CTRL_ENABLE | SOR_HDMI_CTRL_MAX_AC_PACKET(max_ac) |
1963 SOR_HDMI_CTRL_AUDIO_LAYOUT | SOR_HDMI_CTRL_REKEY(SOR_REKEY);
1964 tegra_sor_writel(sor, value, SOR_HDMI_CTRL);
1965
1966 /* H_PULSE2 setup */
1967 pulse_start = h_ref_to_sync + (mode->hsync_end - mode->hsync_start) +
1968 (mode->htotal - mode->hsync_end) - 10;
1969
1970 value = PULSE_LAST_END_A | PULSE_QUAL_VACTIVE |
1971 PULSE_POLARITY_HIGH | PULSE_MODE_NORMAL;
1972 tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_CONTROL);
1973
1974 value = PULSE_END(pulse_start + 8) | PULSE_START(pulse_start);
1975 tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_POSITION_A);
1976
1977 value = tegra_dc_readl(dc, DC_DISP_DISP_SIGNAL_OPTIONS0);
1978 value |= H_PULSE2_ENABLE;
1979 tegra_dc_writel(dc, value, DC_DISP_DISP_SIGNAL_OPTIONS0);
1980
1981 /* infoframe setup */
1982 err = tegra_sor_hdmi_setup_avi_infoframe(sor, mode);
1983 if (err < 0)
1984 dev_err(sor->dev, "failed to setup AVI infoframe: %d\n", err);
1985
1986 /* XXX HDMI audio support not implemented yet */
1987 tegra_sor_hdmi_disable_audio_infoframe(sor);
1988
1989 /* use single TMDS protocol */
1990 value = tegra_sor_readl(sor, SOR_STATE1);
1991 value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
1992 value |= SOR_STATE_ASY_PROTOCOL_SINGLE_TMDS_A;
1993 tegra_sor_writel(sor, value, SOR_STATE1);
1994
1995 /* power up pad calibration */
1996 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
1997 value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
1998 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
1999
2000 /* production settings */
2001 settings = tegra_sor_hdmi_find_settings(sor, mode->clock * 1000);
Dan Carpenterdb8b42f2015-08-17 17:37:03 +03002002 if (!settings) {
2003 dev_err(sor->dev, "no settings for pixel clock %d Hz\n",
2004 mode->clock * 1000);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002005 return;
2006 }
2007
2008 value = tegra_sor_readl(sor, SOR_PLL0);
2009 value &= ~SOR_PLL0_ICHPMP_MASK;
2010 value &= ~SOR_PLL0_VCOCAP_MASK;
2011 value |= SOR_PLL0_ICHPMP(settings->ichpmp);
2012 value |= SOR_PLL0_VCOCAP(settings->vcocap);
2013 tegra_sor_writel(sor, value, SOR_PLL0);
2014
2015 tegra_sor_dp_term_calibrate(sor);
2016
2017 value = tegra_sor_readl(sor, SOR_PLL1);
2018 value &= ~SOR_PLL1_LOADADJ_MASK;
2019 value |= SOR_PLL1_LOADADJ(settings->loadadj);
2020 tegra_sor_writel(sor, value, SOR_PLL1);
2021
2022 value = tegra_sor_readl(sor, SOR_PLL3);
2023 value &= ~SOR_PLL3_BG_VREF_LEVEL_MASK;
2024 value |= SOR_PLL3_BG_VREF_LEVEL(settings->bg_vref);
2025 tegra_sor_writel(sor, value, SOR_PLL3);
2026
2027 value = settings->drive_current[0] << 24 |
2028 settings->drive_current[1] << 16 |
2029 settings->drive_current[2] << 8 |
2030 settings->drive_current[3] << 0;
2031 tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT0);
2032
2033 value = settings->preemphasis[0] << 24 |
2034 settings->preemphasis[1] << 16 |
2035 settings->preemphasis[2] << 8 |
2036 settings->preemphasis[3] << 0;
2037 tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS0);
2038
2039 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
2040 value &= ~SOR_DP_PADCTL_TX_PU_MASK;
2041 value |= SOR_DP_PADCTL_TX_PU_ENABLE;
2042 value |= SOR_DP_PADCTL_TX_PU(settings->tx_pu);
2043 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
2044
2045 /* power down pad calibration */
2046 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
2047 value |= SOR_DP_PADCTL_PAD_CAL_PD;
2048 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
2049
2050 /* miscellaneous display controller settings */
2051 value = VSYNC_H_POSITION(1);
2052 tegra_dc_writel(dc, value, DC_DISP_DISP_TIMING_OPTIONS);
2053
2054 value = tegra_dc_readl(dc, DC_DISP_DISP_COLOR_CONTROL);
2055 value &= ~DITHER_CONTROL_MASK;
2056 value &= ~BASE_COLOR_SIZE_MASK;
2057
2058 switch (info->bpc) {
2059 case 6:
2060 value |= BASE_COLOR_SIZE_666;
2061 break;
2062
2063 case 8:
2064 value |= BASE_COLOR_SIZE_888;
2065 break;
2066
2067 default:
2068 WARN(1, "%u bits-per-color not supported\n", info->bpc);
2069 break;
2070 }
2071
2072 tegra_dc_writel(dc, value, DC_DISP_DISP_COLOR_CONTROL);
2073
2074 err = tegra_sor_power_up(sor, 250);
2075 if (err < 0)
2076 dev_err(sor->dev, "failed to power up SOR: %d\n", err);
2077
Thierry Reding2bd1dd32015-08-03 15:46:15 +02002078 /* configure dynamic range of output */
Thierry Reding459cc2c2015-07-30 10:34:24 +02002079 value = tegra_sor_readl(sor, SOR_HEAD_STATE0(dc->pipe));
2080 value &= ~SOR_HEAD_STATE_RANGECOMPRESS_MASK;
2081 value &= ~SOR_HEAD_STATE_DYNRANGE_MASK;
2082 tegra_sor_writel(sor, value, SOR_HEAD_STATE0(dc->pipe));
2083
Thierry Reding2bd1dd32015-08-03 15:46:15 +02002084 /* configure colorspace */
Thierry Reding459cc2c2015-07-30 10:34:24 +02002085 value = tegra_sor_readl(sor, SOR_HEAD_STATE0(dc->pipe));
2086 value &= ~SOR_HEAD_STATE_COLORSPACE_MASK;
2087 value |= SOR_HEAD_STATE_COLORSPACE_RGB;
2088 tegra_sor_writel(sor, value, SOR_HEAD_STATE0(dc->pipe));
2089
Thierry Reding2bd1dd32015-08-03 15:46:15 +02002090 tegra_sor_mode_set(sor, mode, info);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002091
2092 tegra_sor_update(sor);
2093
2094 err = tegra_sor_attach(sor);
2095 if (err < 0)
2096 dev_err(sor->dev, "failed to attach SOR: %d\n", err);
2097
2098 /* enable display to SOR clock and generate HDMI preamble */
2099 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
2100 value |= SOR1_ENABLE | SOR1_TIMING_CYA;
2101 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
2102
2103 tegra_dc_commit(dc);
2104
2105 err = tegra_sor_wakeup(sor);
2106 if (err < 0)
2107 dev_err(sor->dev, "failed to wakeup SOR: %d\n", err);
2108}
2109
2110static const struct drm_encoder_helper_funcs tegra_sor_hdmi_helpers = {
2111 .disable = tegra_sor_hdmi_disable,
2112 .enable = tegra_sor_hdmi_enable,
2113 .atomic_check = tegra_sor_encoder_atomic_check,
2114};
2115
Thierry Reding6b6b6042013-11-15 16:06:05 +01002116static int tegra_sor_init(struct host1x_client *client)
2117{
Thierry Reding9910f5c2014-05-22 09:57:15 +02002118 struct drm_device *drm = dev_get_drvdata(client->parent);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002119 const struct drm_encoder_helper_funcs *helpers = NULL;
Thierry Reding6b6b6042013-11-15 16:06:05 +01002120 struct tegra_sor *sor = host1x_client_to_sor(client);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002121 int connector = DRM_MODE_CONNECTOR_Unknown;
2122 int encoder = DRM_MODE_ENCODER_NONE;
Thierry Reding6b6b6042013-11-15 16:06:05 +01002123 int err;
2124
Thierry Reding9542c232015-07-08 13:39:09 +02002125 if (!sor->aux) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002126 if (sor->soc->supports_hdmi) {
2127 connector = DRM_MODE_CONNECTOR_HDMIA;
2128 encoder = DRM_MODE_ENCODER_TMDS;
2129 helpers = &tegra_sor_hdmi_helpers;
2130 } else if (sor->soc->supports_lvds) {
2131 connector = DRM_MODE_CONNECTOR_LVDS;
2132 encoder = DRM_MODE_ENCODER_LVDS;
2133 }
2134 } else {
2135 if (sor->soc->supports_edp) {
2136 connector = DRM_MODE_CONNECTOR_eDP;
2137 encoder = DRM_MODE_ENCODER_TMDS;
2138 helpers = &tegra_sor_edp_helpers;
2139 } else if (sor->soc->supports_dp) {
2140 connector = DRM_MODE_CONNECTOR_DisplayPort;
2141 encoder = DRM_MODE_ENCODER_TMDS;
2142 }
2143 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01002144
Thierry Reding6b6b6042013-11-15 16:06:05 +01002145 sor->output.dev = sor->dev;
Thierry Reding6b6b6042013-11-15 16:06:05 +01002146
Thierry Reding6fad8f62014-11-28 15:41:34 +01002147 drm_connector_init(drm, &sor->output.connector,
2148 &tegra_sor_connector_funcs,
Thierry Reding459cc2c2015-07-30 10:34:24 +02002149 connector);
Thierry Reding6fad8f62014-11-28 15:41:34 +01002150 drm_connector_helper_add(&sor->output.connector,
2151 &tegra_sor_connector_helper_funcs);
2152 sor->output.connector.dpms = DRM_MODE_DPMS_OFF;
2153
Thierry Reding6fad8f62014-11-28 15:41:34 +01002154 drm_encoder_init(drm, &sor->output.encoder, &tegra_sor_encoder_funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +02002155 encoder, NULL);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002156 drm_encoder_helper_add(&sor->output.encoder, helpers);
Thierry Reding6fad8f62014-11-28 15:41:34 +01002157
2158 drm_mode_connector_attach_encoder(&sor->output.connector,
2159 &sor->output.encoder);
2160 drm_connector_register(&sor->output.connector);
2161
Thierry Redingea130b22014-12-19 15:51:35 +01002162 err = tegra_output_init(drm, &sor->output);
2163 if (err < 0) {
2164 dev_err(client->dev, "failed to initialize output: %d\n", err);
2165 return err;
2166 }
Thierry Reding6fad8f62014-11-28 15:41:34 +01002167
Thierry Redingea130b22014-12-19 15:51:35 +01002168 sor->output.encoder.possible_crtcs = 0x3;
Thierry Reding6b6b6042013-11-15 16:06:05 +01002169
Thierry Redinga82752e2014-01-31 10:02:15 +01002170 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
Thierry Reding1b0c7b42014-05-28 13:46:12 +02002171 err = tegra_sor_debugfs_init(sor, drm->primary);
Thierry Redinga82752e2014-01-31 10:02:15 +01002172 if (err < 0)
2173 dev_err(sor->dev, "debugfs setup failed: %d\n", err);
2174 }
2175
Thierry Reding9542c232015-07-08 13:39:09 +02002176 if (sor->aux) {
2177 err = drm_dp_aux_attach(sor->aux, &sor->output);
Thierry Reding6b6b6042013-11-15 16:06:05 +01002178 if (err < 0) {
2179 dev_err(sor->dev, "failed to attach DP: %d\n", err);
2180 return err;
2181 }
2182 }
2183
Tomeu Vizoso535a65d2015-03-30 10:33:03 +02002184 /*
2185 * XXX: Remove this reset once proper hand-over from firmware to
2186 * kernel is possible.
2187 */
2188 err = reset_control_assert(sor->rst);
2189 if (err < 0) {
2190 dev_err(sor->dev, "failed to assert SOR reset: %d\n", err);
2191 return err;
2192 }
2193
Thierry Reding6fad8f62014-11-28 15:41:34 +01002194 err = clk_prepare_enable(sor->clk);
2195 if (err < 0) {
2196 dev_err(sor->dev, "failed to enable clock: %d\n", err);
2197 return err;
2198 }
2199
Tomeu Vizoso535a65d2015-03-30 10:33:03 +02002200 usleep_range(1000, 3000);
2201
2202 err = reset_control_deassert(sor->rst);
2203 if (err < 0) {
2204 dev_err(sor->dev, "failed to deassert SOR reset: %d\n", err);
2205 return err;
2206 }
2207
Thierry Reding6fad8f62014-11-28 15:41:34 +01002208 err = clk_prepare_enable(sor->clk_safe);
2209 if (err < 0)
2210 return err;
2211
2212 err = clk_prepare_enable(sor->clk_dp);
2213 if (err < 0)
2214 return err;
2215
Thierry Reding6b6b6042013-11-15 16:06:05 +01002216 return 0;
2217}
2218
2219static int tegra_sor_exit(struct host1x_client *client)
2220{
2221 struct tegra_sor *sor = host1x_client_to_sor(client);
2222 int err;
2223
Thierry Reding328ec692014-12-19 15:55:08 +01002224 tegra_output_exit(&sor->output);
2225
Thierry Reding9542c232015-07-08 13:39:09 +02002226 if (sor->aux) {
2227 err = drm_dp_aux_detach(sor->aux);
Thierry Reding6b6b6042013-11-15 16:06:05 +01002228 if (err < 0) {
2229 dev_err(sor->dev, "failed to detach DP: %d\n", err);
2230 return err;
2231 }
2232 }
2233
Thierry Reding6fad8f62014-11-28 15:41:34 +01002234 clk_disable_unprepare(sor->clk_safe);
2235 clk_disable_unprepare(sor->clk_dp);
2236 clk_disable_unprepare(sor->clk);
2237
Thierry Reding4009c222014-12-19 15:47:30 +01002238 if (IS_ENABLED(CONFIG_DEBUG_FS))
2239 tegra_sor_debugfs_exit(sor);
Thierry Redinga82752e2014-01-31 10:02:15 +01002240
Thierry Reding6b6b6042013-11-15 16:06:05 +01002241 return 0;
2242}
2243
2244static const struct host1x_client_ops sor_client_ops = {
2245 .init = tegra_sor_init,
2246 .exit = tegra_sor_exit,
2247};
2248
Thierry Reding459cc2c2015-07-30 10:34:24 +02002249static const struct tegra_sor_ops tegra_sor_edp_ops = {
2250 .name = "eDP",
2251};
2252
2253static int tegra_sor_hdmi_probe(struct tegra_sor *sor)
2254{
2255 int err;
2256
2257 sor->avdd_io_supply = devm_regulator_get(sor->dev, "avdd-io");
2258 if (IS_ERR(sor->avdd_io_supply)) {
2259 dev_err(sor->dev, "cannot get AVDD I/O supply: %ld\n",
2260 PTR_ERR(sor->avdd_io_supply));
2261 return PTR_ERR(sor->avdd_io_supply);
2262 }
2263
2264 err = regulator_enable(sor->avdd_io_supply);
2265 if (err < 0) {
2266 dev_err(sor->dev, "failed to enable AVDD I/O supply: %d\n",
2267 err);
2268 return err;
2269 }
2270
2271 sor->vdd_pll_supply = devm_regulator_get(sor->dev, "vdd-pll");
2272 if (IS_ERR(sor->vdd_pll_supply)) {
2273 dev_err(sor->dev, "cannot get VDD PLL supply: %ld\n",
2274 PTR_ERR(sor->vdd_pll_supply));
2275 return PTR_ERR(sor->vdd_pll_supply);
2276 }
2277
2278 err = regulator_enable(sor->vdd_pll_supply);
2279 if (err < 0) {
2280 dev_err(sor->dev, "failed to enable VDD PLL supply: %d\n",
2281 err);
2282 return err;
2283 }
2284
2285 sor->hdmi_supply = devm_regulator_get(sor->dev, "hdmi");
2286 if (IS_ERR(sor->hdmi_supply)) {
2287 dev_err(sor->dev, "cannot get HDMI supply: %ld\n",
2288 PTR_ERR(sor->hdmi_supply));
2289 return PTR_ERR(sor->hdmi_supply);
2290 }
2291
2292 err = regulator_enable(sor->hdmi_supply);
2293 if (err < 0) {
2294 dev_err(sor->dev, "failed to enable HDMI supply: %d\n", err);
2295 return err;
2296 }
2297
2298 return 0;
2299}
2300
2301static int tegra_sor_hdmi_remove(struct tegra_sor *sor)
2302{
2303 regulator_disable(sor->hdmi_supply);
2304 regulator_disable(sor->vdd_pll_supply);
2305 regulator_disable(sor->avdd_io_supply);
2306
2307 return 0;
2308}
2309
2310static const struct tegra_sor_ops tegra_sor_hdmi_ops = {
2311 .name = "HDMI",
2312 .probe = tegra_sor_hdmi_probe,
2313 .remove = tegra_sor_hdmi_remove,
2314};
2315
2316static const struct tegra_sor_soc tegra124_sor = {
2317 .supports_edp = true,
2318 .supports_lvds = true,
2319 .supports_hdmi = false,
2320 .supports_dp = false,
2321};
2322
2323static const struct tegra_sor_soc tegra210_sor = {
2324 .supports_edp = true,
2325 .supports_lvds = false,
2326 .supports_hdmi = false,
2327 .supports_dp = false,
2328};
2329
2330static const struct tegra_sor_soc tegra210_sor1 = {
2331 .supports_edp = false,
2332 .supports_lvds = false,
2333 .supports_hdmi = true,
2334 .supports_dp = true,
2335
2336 .num_settings = ARRAY_SIZE(tegra210_sor_hdmi_defaults),
2337 .settings = tegra210_sor_hdmi_defaults,
2338};
2339
2340static const struct of_device_id tegra_sor_of_match[] = {
2341 { .compatible = "nvidia,tegra210-sor1", .data = &tegra210_sor1 },
2342 { .compatible = "nvidia,tegra210-sor", .data = &tegra210_sor },
2343 { .compatible = "nvidia,tegra124-sor", .data = &tegra124_sor },
2344 { },
2345};
2346MODULE_DEVICE_TABLE(of, tegra_sor_of_match);
2347
Thierry Reding6b6b6042013-11-15 16:06:05 +01002348static int tegra_sor_probe(struct platform_device *pdev)
2349{
Thierry Reding459cc2c2015-07-30 10:34:24 +02002350 const struct of_device_id *match;
Thierry Reding6b6b6042013-11-15 16:06:05 +01002351 struct device_node *np;
2352 struct tegra_sor *sor;
2353 struct resource *regs;
2354 int err;
2355
Thierry Reding459cc2c2015-07-30 10:34:24 +02002356 match = of_match_device(tegra_sor_of_match, &pdev->dev);
2357
Thierry Reding6b6b6042013-11-15 16:06:05 +01002358 sor = devm_kzalloc(&pdev->dev, sizeof(*sor), GFP_KERNEL);
2359 if (!sor)
2360 return -ENOMEM;
2361
2362 sor->output.dev = sor->dev = &pdev->dev;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002363 sor->soc = match->data;
2364
2365 sor->settings = devm_kmemdup(&pdev->dev, sor->soc->settings,
2366 sor->soc->num_settings *
2367 sizeof(*sor->settings),
2368 GFP_KERNEL);
2369 if (!sor->settings)
2370 return -ENOMEM;
2371
2372 sor->num_settings = sor->soc->num_settings;
Thierry Reding6b6b6042013-11-15 16:06:05 +01002373
2374 np = of_parse_phandle(pdev->dev.of_node, "nvidia,dpaux", 0);
2375 if (np) {
Thierry Reding9542c232015-07-08 13:39:09 +02002376 sor->aux = drm_dp_aux_find_by_of_node(np);
Thierry Reding6b6b6042013-11-15 16:06:05 +01002377 of_node_put(np);
2378
Thierry Reding9542c232015-07-08 13:39:09 +02002379 if (!sor->aux)
Thierry Reding6b6b6042013-11-15 16:06:05 +01002380 return -EPROBE_DEFER;
2381 }
2382
Thierry Reding9542c232015-07-08 13:39:09 +02002383 if (!sor->aux) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002384 if (sor->soc->supports_hdmi) {
2385 sor->ops = &tegra_sor_hdmi_ops;
2386 } else if (sor->soc->supports_lvds) {
2387 dev_err(&pdev->dev, "LVDS not supported yet\n");
2388 return -ENODEV;
2389 } else {
2390 dev_err(&pdev->dev, "unknown (non-DP) support\n");
2391 return -ENODEV;
2392 }
2393 } else {
2394 if (sor->soc->supports_edp) {
2395 sor->ops = &tegra_sor_edp_ops;
2396 } else if (sor->soc->supports_dp) {
2397 dev_err(&pdev->dev, "DisplayPort not supported yet\n");
2398 return -ENODEV;
2399 } else {
2400 dev_err(&pdev->dev, "unknown (DP) support\n");
2401 return -ENODEV;
2402 }
2403 }
2404
Thierry Reding6b6b6042013-11-15 16:06:05 +01002405 err = tegra_output_probe(&sor->output);
Thierry Reding4dbdc742015-04-27 15:04:26 +02002406 if (err < 0) {
2407 dev_err(&pdev->dev, "failed to probe output: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01002408 return err;
Thierry Reding4dbdc742015-04-27 15:04:26 +02002409 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01002410
Thierry Reding459cc2c2015-07-30 10:34:24 +02002411 if (sor->ops && sor->ops->probe) {
2412 err = sor->ops->probe(sor);
2413 if (err < 0) {
2414 dev_err(&pdev->dev, "failed to probe %s: %d\n",
2415 sor->ops->name, err);
2416 goto output;
2417 }
2418 }
2419
Thierry Reding6b6b6042013-11-15 16:06:05 +01002420 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2421 sor->regs = devm_ioremap_resource(&pdev->dev, regs);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002422 if (IS_ERR(sor->regs)) {
2423 err = PTR_ERR(sor->regs);
2424 goto remove;
2425 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01002426
2427 sor->rst = devm_reset_control_get(&pdev->dev, "sor");
Thierry Reding4dbdc742015-04-27 15:04:26 +02002428 if (IS_ERR(sor->rst)) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002429 err = PTR_ERR(sor->rst);
2430 dev_err(&pdev->dev, "failed to get reset control: %d\n", err);
2431 goto remove;
Thierry Reding4dbdc742015-04-27 15:04:26 +02002432 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01002433
2434 sor->clk = devm_clk_get(&pdev->dev, NULL);
Thierry Reding4dbdc742015-04-27 15:04:26 +02002435 if (IS_ERR(sor->clk)) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002436 err = PTR_ERR(sor->clk);
2437 dev_err(&pdev->dev, "failed to get module clock: %d\n", err);
2438 goto remove;
Thierry Reding4dbdc742015-04-27 15:04:26 +02002439 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01002440
2441 sor->clk_parent = devm_clk_get(&pdev->dev, "parent");
Thierry Reding4dbdc742015-04-27 15:04:26 +02002442 if (IS_ERR(sor->clk_parent)) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002443 err = PTR_ERR(sor->clk_parent);
2444 dev_err(&pdev->dev, "failed to get parent clock: %d\n", err);
2445 goto remove;
Thierry Reding4dbdc742015-04-27 15:04:26 +02002446 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01002447
Thierry Reding6b6b6042013-11-15 16:06:05 +01002448 sor->clk_safe = devm_clk_get(&pdev->dev, "safe");
Thierry Reding4dbdc742015-04-27 15:04:26 +02002449 if (IS_ERR(sor->clk_safe)) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002450 err = PTR_ERR(sor->clk_safe);
2451 dev_err(&pdev->dev, "failed to get safe clock: %d\n", err);
2452 goto remove;
Thierry Reding4dbdc742015-04-27 15:04:26 +02002453 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01002454
Thierry Reding6b6b6042013-11-15 16:06:05 +01002455 sor->clk_dp = devm_clk_get(&pdev->dev, "dp");
Thierry Reding4dbdc742015-04-27 15:04:26 +02002456 if (IS_ERR(sor->clk_dp)) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002457 err = PTR_ERR(sor->clk_dp);
2458 dev_err(&pdev->dev, "failed to get DP clock: %d\n", err);
2459 goto remove;
Thierry Reding4dbdc742015-04-27 15:04:26 +02002460 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01002461
Thierry Reding6b6b6042013-11-15 16:06:05 +01002462 INIT_LIST_HEAD(&sor->client.list);
2463 sor->client.ops = &sor_client_ops;
2464 sor->client.dev = &pdev->dev;
2465
Thierry Reding6b6b6042013-11-15 16:06:05 +01002466 err = host1x_client_register(&sor->client);
2467 if (err < 0) {
2468 dev_err(&pdev->dev, "failed to register host1x client: %d\n",
2469 err);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002470 goto remove;
Thierry Reding6b6b6042013-11-15 16:06:05 +01002471 }
2472
2473 platform_set_drvdata(pdev, sor);
2474
2475 return 0;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002476
2477remove:
2478 if (sor->ops && sor->ops->remove)
2479 sor->ops->remove(sor);
2480output:
2481 tegra_output_remove(&sor->output);
2482 return err;
Thierry Reding6b6b6042013-11-15 16:06:05 +01002483}
2484
2485static int tegra_sor_remove(struct platform_device *pdev)
2486{
2487 struct tegra_sor *sor = platform_get_drvdata(pdev);
2488 int err;
2489
2490 err = host1x_client_unregister(&sor->client);
2491 if (err < 0) {
2492 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
2493 err);
2494 return err;
2495 }
2496
Thierry Reding459cc2c2015-07-30 10:34:24 +02002497 if (sor->ops && sor->ops->remove) {
2498 err = sor->ops->remove(sor);
2499 if (err < 0)
2500 dev_err(&pdev->dev, "failed to remove SOR: %d\n", err);
2501 }
2502
Thierry Reding328ec692014-12-19 15:55:08 +01002503 tegra_output_remove(&sor->output);
Thierry Reding6b6b6042013-11-15 16:06:05 +01002504
2505 return 0;
2506}
2507
Thierry Reding6b6b6042013-11-15 16:06:05 +01002508struct platform_driver tegra_sor_driver = {
2509 .driver = {
2510 .name = "tegra-sor",
2511 .of_match_table = tegra_sor_of_match,
2512 },
2513 .probe = tegra_sor_probe,
2514 .remove = tegra_sor_remove,
2515};