blob: d51399587aca3f45a2ad5625eea98f4cf2aead0f [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 Redingb2992212015-10-01 14:25:03 +020010#include <linux/clk-provider.h>
Thierry Redinga82752e2014-01-31 10:02:15 +010011#include <linux/debugfs.h>
Thierry Reding6fad8f62014-11-28 15:41:34 +010012#include <linux/gpio.h>
Thierry Reding6b6b6042013-11-15 16:06:05 +010013#include <linux/io.h>
Thierry Reding459cc2c2015-07-30 10:34:24 +020014#include <linux/of_device.h>
Thierry Reding6b6b6042013-11-15 16:06:05 +010015#include <linux/platform_device.h>
Thierry Redingaaff8bd2015-08-07 16:04:54 +020016#include <linux/pm_runtime.h>
Thierry Reding459cc2c2015-07-30 10:34:24 +020017#include <linux/regulator/consumer.h>
Thierry Reding6b6b6042013-11-15 16:06:05 +010018#include <linux/reset.h>
Thierry Reding306a7f92014-07-17 13:17:24 +020019
Thierry Reding72323982014-07-11 13:19:06 +020020#include <soc/tegra/pmc.h>
Thierry Reding6b6b6042013-11-15 16:06:05 +010021
Thierry Reding4aa3df72014-11-24 16:27:13 +010022#include <drm/drm_atomic_helper.h>
Thierry Reding6b6b6042013-11-15 16:06:05 +010023#include <drm/drm_dp_helper.h>
Thierry Reding6fad8f62014-11-28 15:41:34 +010024#include <drm/drm_panel.h>
Thierry Reding6b6b6042013-11-15 16:06:05 +010025
26#include "dc.h"
27#include "drm.h"
28#include "sor.h"
Thierry Reding932f6522017-08-15 15:41:14 +020029#include "trace.h"
Thierry Reding6b6b6042013-11-15 16:06:05 +010030
Thierry Reding459cc2c2015-07-30 10:34:24 +020031#define SOR_REKEY 0x38
32
33struct tegra_sor_hdmi_settings {
34 unsigned long frequency;
35
36 u8 vcocap;
37 u8 ichpmp;
38 u8 loadadj;
39 u8 termadj;
40 u8 tx_pu;
41 u8 bg_vref;
42
43 u8 drive_current[4];
44 u8 preemphasis[4];
45};
46
47#if 1
48static const struct tegra_sor_hdmi_settings tegra210_sor_hdmi_defaults[] = {
49 {
50 .frequency = 54000000,
51 .vcocap = 0x0,
52 .ichpmp = 0x1,
53 .loadadj = 0x3,
54 .termadj = 0x9,
55 .tx_pu = 0x10,
56 .bg_vref = 0x8,
57 .drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
58 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
59 }, {
60 .frequency = 75000000,
61 .vcocap = 0x3,
62 .ichpmp = 0x1,
63 .loadadj = 0x3,
64 .termadj = 0x9,
65 .tx_pu = 0x40,
66 .bg_vref = 0x8,
67 .drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
68 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
69 }, {
70 .frequency = 150000000,
71 .vcocap = 0x3,
72 .ichpmp = 0x1,
73 .loadadj = 0x3,
74 .termadj = 0x9,
75 .tx_pu = 0x66,
76 .bg_vref = 0x8,
77 .drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
78 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
79 }, {
80 .frequency = 300000000,
81 .vcocap = 0x3,
82 .ichpmp = 0x1,
83 .loadadj = 0x3,
84 .termadj = 0x9,
85 .tx_pu = 0x66,
86 .bg_vref = 0xa,
87 .drive_current = { 0x33, 0x3f, 0x3f, 0x3f },
88 .preemphasis = { 0x00, 0x17, 0x17, 0x17 },
89 }, {
90 .frequency = 600000000,
91 .vcocap = 0x3,
92 .ichpmp = 0x1,
93 .loadadj = 0x3,
94 .termadj = 0x9,
95 .tx_pu = 0x66,
96 .bg_vref = 0x8,
97 .drive_current = { 0x33, 0x3f, 0x3f, 0x3f },
98 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
99 },
100};
101#else
102static const struct tegra_sor_hdmi_settings tegra210_sor_hdmi_defaults[] = {
103 {
104 .frequency = 75000000,
105 .vcocap = 0x3,
106 .ichpmp = 0x1,
107 .loadadj = 0x3,
108 .termadj = 0x9,
109 .tx_pu = 0x40,
110 .bg_vref = 0x8,
111 .drive_current = { 0x29, 0x29, 0x29, 0x29 },
112 .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
113 }, {
114 .frequency = 150000000,
115 .vcocap = 0x3,
116 .ichpmp = 0x1,
117 .loadadj = 0x3,
118 .termadj = 0x9,
119 .tx_pu = 0x66,
120 .bg_vref = 0x8,
121 .drive_current = { 0x30, 0x37, 0x37, 0x37 },
122 .preemphasis = { 0x01, 0x02, 0x02, 0x02 },
123 }, {
124 .frequency = 300000000,
125 .vcocap = 0x3,
126 .ichpmp = 0x6,
127 .loadadj = 0x3,
128 .termadj = 0x9,
129 .tx_pu = 0x66,
130 .bg_vref = 0xf,
131 .drive_current = { 0x30, 0x37, 0x37, 0x37 },
132 .preemphasis = { 0x10, 0x3e, 0x3e, 0x3e },
133 }, {
134 .frequency = 600000000,
135 .vcocap = 0x3,
136 .ichpmp = 0xa,
137 .loadadj = 0x3,
138 .termadj = 0xb,
139 .tx_pu = 0x66,
140 .bg_vref = 0xe,
141 .drive_current = { 0x35, 0x3e, 0x3e, 0x3e },
142 .preemphasis = { 0x02, 0x3f, 0x3f, 0x3f },
143 },
144};
145#endif
146
Thierry Reding880cee02017-10-12 19:04:17 +0200147struct tegra_sor_regs {
148 unsigned int head_state0;
149 unsigned int head_state1;
150 unsigned int head_state2;
151 unsigned int head_state3;
152 unsigned int head_state4;
153 unsigned int head_state5;
154 unsigned int pll0;
155 unsigned int pll1;
156 unsigned int pll2;
157 unsigned int pll3;
158 unsigned int dp_padctl0;
159 unsigned int dp_padctl2;
160};
161
Thierry Reding459cc2c2015-07-30 10:34:24 +0200162struct tegra_sor_soc {
163 bool supports_edp;
164 bool supports_lvds;
165 bool supports_hdmi;
166 bool supports_dp;
167
Thierry Reding880cee02017-10-12 19:04:17 +0200168 const struct tegra_sor_regs *regs;
169
Thierry Reding459cc2c2015-07-30 10:34:24 +0200170 const struct tegra_sor_hdmi_settings *settings;
171 unsigned int num_settings;
Thierry Reding30b49432015-08-03 15:50:32 +0200172
173 const u8 *xbar_cfg;
Thierry Reding459cc2c2015-07-30 10:34:24 +0200174};
175
176struct tegra_sor;
177
178struct tegra_sor_ops {
179 const char *name;
180 int (*probe)(struct tegra_sor *sor);
181 int (*remove)(struct tegra_sor *sor);
182};
183
Thierry Reding6b6b6042013-11-15 16:06:05 +0100184struct tegra_sor {
185 struct host1x_client client;
186 struct tegra_output output;
187 struct device *dev;
188
Thierry Reding459cc2c2015-07-30 10:34:24 +0200189 const struct tegra_sor_soc *soc;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100190 void __iomem *regs;
191
192 struct reset_control *rst;
193 struct clk *clk_parent;
194 struct clk *clk_safe;
Thierry Redinge1335e22017-10-12 17:53:11 +0200195 struct clk *clk_out;
196 struct clk *clk_pad;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100197 struct clk *clk_dp;
198 struct clk *clk;
199
Thierry Reding9542c232015-07-08 13:39:09 +0200200 struct drm_dp_aux *aux;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100201
Thierry Redingdab16332015-01-26 16:04:08 +0100202 struct drm_info_list *debugfs_files;
Thierry Reding459cc2c2015-07-30 10:34:24 +0200203
204 const struct tegra_sor_ops *ops;
205
206 /* for HDMI 2.0 */
207 struct tegra_sor_hdmi_settings *settings;
208 unsigned int num_settings;
209
210 struct regulator *avdd_io_supply;
211 struct regulator *vdd_pll_supply;
212 struct regulator *hdmi_supply;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100213};
214
Thierry Redingc31efa72015-09-08 16:09:22 +0200215struct tegra_sor_state {
216 struct drm_connector_state base;
217
218 unsigned int bpc;
219};
220
221static inline struct tegra_sor_state *
222to_sor_state(struct drm_connector_state *state)
223{
224 return container_of(state, struct tegra_sor_state, base);
225}
226
Thierry Reding34fa1832014-06-05 16:31:10 +0200227struct tegra_sor_config {
228 u32 bits_per_pixel;
229
230 u32 active_polarity;
231 u32 active_count;
232 u32 tu_size;
233 u32 active_frac;
234 u32 watermark;
Thierry Reding7890b572014-06-05 16:12:46 +0200235
236 u32 hblank_symbols;
237 u32 vblank_symbols;
Thierry Reding34fa1832014-06-05 16:31:10 +0200238};
239
Thierry Reding6b6b6042013-11-15 16:06:05 +0100240static inline struct tegra_sor *
241host1x_client_to_sor(struct host1x_client *client)
242{
243 return container_of(client, struct tegra_sor, client);
244}
245
246static inline struct tegra_sor *to_sor(struct tegra_output *output)
247{
248 return container_of(output, struct tegra_sor, output);
249}
250
Thierry Reding5c5f1302017-08-15 15:41:09 +0200251static inline u32 tegra_sor_readl(struct tegra_sor *sor, unsigned int offset)
Thierry Reding6b6b6042013-11-15 16:06:05 +0100252{
Thierry Reding932f6522017-08-15 15:41:14 +0200253 u32 value = readl(sor->regs + (offset << 2));
254
255 trace_sor_readl(sor->dev, offset, value);
256
257 return value;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100258}
259
Thierry Reding28fe2072015-01-26 16:02:48 +0100260static inline void tegra_sor_writel(struct tegra_sor *sor, u32 value,
Thierry Reding5c5f1302017-08-15 15:41:09 +0200261 unsigned int offset)
Thierry Reding6b6b6042013-11-15 16:06:05 +0100262{
Thierry Reding932f6522017-08-15 15:41:14 +0200263 trace_sor_writel(sor->dev, offset, value);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100264 writel(value, sor->regs + (offset << 2));
265}
266
Thierry Reding25bb2ce2015-08-03 14:23:29 +0200267static int tegra_sor_set_parent_clock(struct tegra_sor *sor, struct clk *parent)
268{
269 int err;
270
271 clk_disable_unprepare(sor->clk);
272
Thierry Redinge1335e22017-10-12 17:53:11 +0200273 err = clk_set_parent(sor->clk_out, parent);
Thierry Reding25bb2ce2015-08-03 14:23:29 +0200274 if (err < 0)
275 return err;
276
277 err = clk_prepare_enable(sor->clk);
278 if (err < 0)
279 return err;
280
281 return 0;
282}
283
Thierry Redinge1335e22017-10-12 17:53:11 +0200284struct tegra_clk_sor_pad {
Thierry Redingb2992212015-10-01 14:25:03 +0200285 struct clk_hw hw;
286 struct tegra_sor *sor;
287};
288
Thierry Redinge1335e22017-10-12 17:53:11 +0200289static inline struct tegra_clk_sor_pad *to_pad(struct clk_hw *hw)
Thierry Redingb2992212015-10-01 14:25:03 +0200290{
Thierry Redinge1335e22017-10-12 17:53:11 +0200291 return container_of(hw, struct tegra_clk_sor_pad, hw);
Thierry Redingb2992212015-10-01 14:25:03 +0200292}
293
Thierry Redinge1335e22017-10-12 17:53:11 +0200294static const char * const tegra_clk_sor_pad_parents[] = {
Thierry Redingb2992212015-10-01 14:25:03 +0200295 "pll_d2_out0", "pll_dp"
296};
297
Thierry Redinge1335e22017-10-12 17:53:11 +0200298static int tegra_clk_sor_pad_set_parent(struct clk_hw *hw, u8 index)
Thierry Redingb2992212015-10-01 14:25:03 +0200299{
Thierry Redinge1335e22017-10-12 17:53:11 +0200300 struct tegra_clk_sor_pad *pad = to_pad(hw);
301 struct tegra_sor *sor = pad->sor;
Thierry Redingb2992212015-10-01 14:25:03 +0200302 u32 value;
303
304 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
305 value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
306
307 switch (index) {
308 case 0:
309 value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK;
310 break;
311
312 case 1:
313 value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
314 break;
315 }
316
317 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
318
319 return 0;
320}
321
Thierry Redinge1335e22017-10-12 17:53:11 +0200322static u8 tegra_clk_sor_pad_get_parent(struct clk_hw *hw)
Thierry Redingb2992212015-10-01 14:25:03 +0200323{
Thierry Redinge1335e22017-10-12 17:53:11 +0200324 struct tegra_clk_sor_pad *pad = to_pad(hw);
325 struct tegra_sor *sor = pad->sor;
Thierry Redingb2992212015-10-01 14:25:03 +0200326 u8 parent = U8_MAX;
327 u32 value;
328
329 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
330
331 switch (value & SOR_CLK_CNTRL_DP_CLK_SEL_MASK) {
332 case SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK:
333 case SOR_CLK_CNTRL_DP_CLK_SEL_DIFF_PCLK:
334 parent = 0;
335 break;
336
337 case SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK:
338 case SOR_CLK_CNTRL_DP_CLK_SEL_DIFF_DPCLK:
339 parent = 1;
340 break;
341 }
342
343 return parent;
344}
345
Thierry Redinge1335e22017-10-12 17:53:11 +0200346static const struct clk_ops tegra_clk_sor_pad_ops = {
347 .set_parent = tegra_clk_sor_pad_set_parent,
348 .get_parent = tegra_clk_sor_pad_get_parent,
Thierry Redingb2992212015-10-01 14:25:03 +0200349};
350
Thierry Redinge1335e22017-10-12 17:53:11 +0200351static struct clk *tegra_clk_sor_pad_register(struct tegra_sor *sor,
352 const char *name)
Thierry Redingb2992212015-10-01 14:25:03 +0200353{
Thierry Redinge1335e22017-10-12 17:53:11 +0200354 struct tegra_clk_sor_pad *pad;
Thierry Redingb2992212015-10-01 14:25:03 +0200355 struct clk_init_data init;
356 struct clk *clk;
357
Thierry Redinge1335e22017-10-12 17:53:11 +0200358 pad = devm_kzalloc(sor->dev, sizeof(*pad), GFP_KERNEL);
359 if (!pad)
Thierry Redingb2992212015-10-01 14:25:03 +0200360 return ERR_PTR(-ENOMEM);
361
Thierry Redinge1335e22017-10-12 17:53:11 +0200362 pad->sor = sor;
Thierry Redingb2992212015-10-01 14:25:03 +0200363
364 init.name = name;
365 init.flags = 0;
Thierry Redinge1335e22017-10-12 17:53:11 +0200366 init.parent_names = tegra_clk_sor_pad_parents;
367 init.num_parents = ARRAY_SIZE(tegra_clk_sor_pad_parents);
368 init.ops = &tegra_clk_sor_pad_ops;
Thierry Redingb2992212015-10-01 14:25:03 +0200369
Thierry Redinge1335e22017-10-12 17:53:11 +0200370 pad->hw.init = &init;
Thierry Redingb2992212015-10-01 14:25:03 +0200371
Thierry Redinge1335e22017-10-12 17:53:11 +0200372 clk = devm_clk_register(sor->dev, &pad->hw);
Thierry Redingb2992212015-10-01 14:25:03 +0200373
374 return clk;
375}
376
Thierry Reding6b6b6042013-11-15 16:06:05 +0100377static int tegra_sor_dp_train_fast(struct tegra_sor *sor,
378 struct drm_dp_link *link)
379{
Thierry Reding6b6b6042013-11-15 16:06:05 +0100380 unsigned int i;
381 u8 pattern;
Thierry Reding28fe2072015-01-26 16:02:48 +0100382 u32 value;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100383 int err;
384
385 /* setup lane parameters */
386 value = SOR_LANE_DRIVE_CURRENT_LANE3(0x40) |
387 SOR_LANE_DRIVE_CURRENT_LANE2(0x40) |
388 SOR_LANE_DRIVE_CURRENT_LANE1(0x40) |
389 SOR_LANE_DRIVE_CURRENT_LANE0(0x40);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200390 tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100391
392 value = SOR_LANE_PREEMPHASIS_LANE3(0x0f) |
393 SOR_LANE_PREEMPHASIS_LANE2(0x0f) |
394 SOR_LANE_PREEMPHASIS_LANE1(0x0f) |
395 SOR_LANE_PREEMPHASIS_LANE0(0x0f);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200396 tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100397
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200398 value = SOR_LANE_POSTCURSOR_LANE3(0x00) |
399 SOR_LANE_POSTCURSOR_LANE2(0x00) |
400 SOR_LANE_POSTCURSOR_LANE1(0x00) |
401 SOR_LANE_POSTCURSOR_LANE0(0x00);
402 tegra_sor_writel(sor, value, SOR_LANE_POSTCURSOR0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100403
404 /* disable LVDS mode */
405 tegra_sor_writel(sor, 0, SOR_LVDS);
406
Thierry Reding880cee02017-10-12 19:04:17 +0200407 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100408 value |= SOR_DP_PADCTL_TX_PU_ENABLE;
409 value &= ~SOR_DP_PADCTL_TX_PU_MASK;
410 value |= SOR_DP_PADCTL_TX_PU(2); /* XXX: don't hardcode? */
Thierry Reding880cee02017-10-12 19:04:17 +0200411 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100412
Thierry Reding880cee02017-10-12 19:04:17 +0200413 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100414 value |= SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
415 SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0;
Thierry Reding880cee02017-10-12 19:04:17 +0200416 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100417
418 usleep_range(10, 100);
419
Thierry Reding880cee02017-10-12 19:04:17 +0200420 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100421 value &= ~(SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
422 SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0);
Thierry Reding880cee02017-10-12 19:04:17 +0200423 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100424
Thierry Reding9542c232015-07-08 13:39:09 +0200425 err = drm_dp_aux_prepare(sor->aux, DP_SET_ANSI_8B10B);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100426 if (err < 0)
427 return err;
428
429 for (i = 0, value = 0; i < link->num_lanes; i++) {
430 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
431 SOR_DP_TPG_SCRAMBLER_NONE |
432 SOR_DP_TPG_PATTERN_TRAIN1;
433 value = (value << 8) | lane;
434 }
435
436 tegra_sor_writel(sor, value, SOR_DP_TPG);
437
438 pattern = DP_TRAINING_PATTERN_1;
439
Thierry Reding9542c232015-07-08 13:39:09 +0200440 err = drm_dp_aux_train(sor->aux, link, pattern);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100441 if (err < 0)
442 return err;
443
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200444 value = tegra_sor_readl(sor, SOR_DP_SPARE0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100445 value |= SOR_DP_SPARE_SEQ_ENABLE;
446 value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
447 value |= SOR_DP_SPARE_MACRO_SOR_CLK;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200448 tegra_sor_writel(sor, value, SOR_DP_SPARE0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100449
450 for (i = 0, value = 0; i < link->num_lanes; i++) {
451 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
452 SOR_DP_TPG_SCRAMBLER_NONE |
453 SOR_DP_TPG_PATTERN_TRAIN2;
454 value = (value << 8) | lane;
455 }
456
457 tegra_sor_writel(sor, value, SOR_DP_TPG);
458
459 pattern = DP_LINK_SCRAMBLING_DISABLE | DP_TRAINING_PATTERN_2;
460
Thierry Reding9542c232015-07-08 13:39:09 +0200461 err = drm_dp_aux_train(sor->aux, link, pattern);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100462 if (err < 0)
463 return err;
464
465 for (i = 0, value = 0; i < link->num_lanes; i++) {
466 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
467 SOR_DP_TPG_SCRAMBLER_GALIOS |
468 SOR_DP_TPG_PATTERN_NONE;
469 value = (value << 8) | lane;
470 }
471
472 tegra_sor_writel(sor, value, SOR_DP_TPG);
473
474 pattern = DP_TRAINING_PATTERN_DISABLE;
475
Thierry Reding9542c232015-07-08 13:39:09 +0200476 err = drm_dp_aux_train(sor->aux, link, pattern);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100477 if (err < 0)
478 return err;
479
480 return 0;
481}
482
Thierry Reding459cc2c2015-07-30 10:34:24 +0200483static void tegra_sor_dp_term_calibrate(struct tegra_sor *sor)
484{
485 u32 mask = 0x08, adj = 0, value;
486
487 /* enable pad calibration logic */
488 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
489 value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
490 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
491
492 value = tegra_sor_readl(sor, SOR_PLL1);
493 value |= SOR_PLL1_TMDS_TERM;
494 tegra_sor_writel(sor, value, SOR_PLL1);
495
496 while (mask) {
497 adj |= mask;
498
499 value = tegra_sor_readl(sor, SOR_PLL1);
500 value &= ~SOR_PLL1_TMDS_TERMADJ_MASK;
501 value |= SOR_PLL1_TMDS_TERMADJ(adj);
502 tegra_sor_writel(sor, value, SOR_PLL1);
503
504 usleep_range(100, 200);
505
506 value = tegra_sor_readl(sor, SOR_PLL1);
507 if (value & SOR_PLL1_TERM_COMPOUT)
508 adj &= ~mask;
509
510 mask >>= 1;
511 }
512
513 value = tegra_sor_readl(sor, SOR_PLL1);
514 value &= ~SOR_PLL1_TMDS_TERMADJ_MASK;
515 value |= SOR_PLL1_TMDS_TERMADJ(adj);
516 tegra_sor_writel(sor, value, SOR_PLL1);
517
518 /* disable pad calibration logic */
519 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
520 value |= SOR_DP_PADCTL_PAD_CAL_PD;
521 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
522}
523
Thierry Reding6b6b6042013-11-15 16:06:05 +0100524static void tegra_sor_super_update(struct tegra_sor *sor)
525{
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200526 tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
527 tegra_sor_writel(sor, 1, SOR_SUPER_STATE0);
528 tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100529}
530
531static void tegra_sor_update(struct tegra_sor *sor)
532{
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200533 tegra_sor_writel(sor, 0, SOR_STATE0);
534 tegra_sor_writel(sor, 1, SOR_STATE0);
535 tegra_sor_writel(sor, 0, SOR_STATE0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100536}
537
538static int tegra_sor_setup_pwm(struct tegra_sor *sor, unsigned long timeout)
539{
Thierry Reding28fe2072015-01-26 16:02:48 +0100540 u32 value;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100541
542 value = tegra_sor_readl(sor, SOR_PWM_DIV);
543 value &= ~SOR_PWM_DIV_MASK;
544 value |= 0x400; /* period */
545 tegra_sor_writel(sor, value, SOR_PWM_DIV);
546
547 value = tegra_sor_readl(sor, SOR_PWM_CTL);
548 value &= ~SOR_PWM_CTL_DUTY_CYCLE_MASK;
549 value |= 0x400; /* duty cycle */
550 value &= ~SOR_PWM_CTL_CLK_SEL; /* clock source: PCLK */
551 value |= SOR_PWM_CTL_TRIGGER;
552 tegra_sor_writel(sor, value, SOR_PWM_CTL);
553
554 timeout = jiffies + msecs_to_jiffies(timeout);
555
556 while (time_before(jiffies, timeout)) {
557 value = tegra_sor_readl(sor, SOR_PWM_CTL);
558 if ((value & SOR_PWM_CTL_TRIGGER) == 0)
559 return 0;
560
561 usleep_range(25, 100);
562 }
563
564 return -ETIMEDOUT;
565}
566
567static int tegra_sor_attach(struct tegra_sor *sor)
568{
569 unsigned long value, timeout;
570
571 /* wake up in normal mode */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200572 value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100573 value |= SOR_SUPER_STATE_HEAD_MODE_AWAKE;
574 value |= SOR_SUPER_STATE_MODE_NORMAL;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200575 tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100576 tegra_sor_super_update(sor);
577
578 /* attach */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200579 value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100580 value |= SOR_SUPER_STATE_ATTACHED;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200581 tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100582 tegra_sor_super_update(sor);
583
584 timeout = jiffies + msecs_to_jiffies(250);
585
586 while (time_before(jiffies, timeout)) {
587 value = tegra_sor_readl(sor, SOR_TEST);
588 if ((value & SOR_TEST_ATTACHED) != 0)
589 return 0;
590
591 usleep_range(25, 100);
592 }
593
594 return -ETIMEDOUT;
595}
596
597static int tegra_sor_wakeup(struct tegra_sor *sor)
598{
Thierry Reding6b6b6042013-11-15 16:06:05 +0100599 unsigned long value, timeout;
600
Thierry Reding6b6b6042013-11-15 16:06:05 +0100601 timeout = jiffies + msecs_to_jiffies(250);
602
603 /* wait for head to wake up */
604 while (time_before(jiffies, timeout)) {
605 value = tegra_sor_readl(sor, SOR_TEST);
606 value &= SOR_TEST_HEAD_MODE_MASK;
607
608 if (value == SOR_TEST_HEAD_MODE_AWAKE)
609 return 0;
610
611 usleep_range(25, 100);
612 }
613
614 return -ETIMEDOUT;
615}
616
617static int tegra_sor_power_up(struct tegra_sor *sor, unsigned long timeout)
618{
Thierry Reding28fe2072015-01-26 16:02:48 +0100619 u32 value;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100620
621 value = tegra_sor_readl(sor, SOR_PWR);
622 value |= SOR_PWR_TRIGGER | SOR_PWR_NORMAL_STATE_PU;
623 tegra_sor_writel(sor, value, SOR_PWR);
624
625 timeout = jiffies + msecs_to_jiffies(timeout);
626
627 while (time_before(jiffies, timeout)) {
628 value = tegra_sor_readl(sor, SOR_PWR);
629 if ((value & SOR_PWR_TRIGGER) == 0)
630 return 0;
631
632 usleep_range(25, 100);
633 }
634
635 return -ETIMEDOUT;
636}
637
Thierry Reding34fa1832014-06-05 16:31:10 +0200638struct tegra_sor_params {
639 /* number of link clocks per line */
640 unsigned int num_clocks;
641 /* ratio between input and output */
642 u64 ratio;
643 /* precision factor */
644 u64 precision;
645
646 unsigned int active_polarity;
647 unsigned int active_count;
648 unsigned int active_frac;
649 unsigned int tu_size;
650 unsigned int error;
651};
652
653static int tegra_sor_compute_params(struct tegra_sor *sor,
654 struct tegra_sor_params *params,
655 unsigned int tu_size)
656{
657 u64 active_sym, active_count, frac, approx;
658 u32 active_polarity, active_frac = 0;
659 const u64 f = params->precision;
660 s64 error;
661
662 active_sym = params->ratio * tu_size;
663 active_count = div_u64(active_sym, f) * f;
664 frac = active_sym - active_count;
665
666 /* fraction < 0.5 */
667 if (frac >= (f / 2)) {
668 active_polarity = 1;
669 frac = f - frac;
670 } else {
671 active_polarity = 0;
672 }
673
674 if (frac != 0) {
675 frac = div_u64(f * f, frac); /* 1/fraction */
676 if (frac <= (15 * f)) {
677 active_frac = div_u64(frac, f);
678
679 /* round up */
680 if (active_polarity)
681 active_frac++;
682 } else {
683 active_frac = active_polarity ? 1 : 15;
684 }
685 }
686
687 if (active_frac == 1)
688 active_polarity = 0;
689
690 if (active_polarity == 1) {
691 if (active_frac) {
692 approx = active_count + (active_frac * (f - 1)) * f;
693 approx = div_u64(approx, active_frac * f);
694 } else {
695 approx = active_count + f;
696 }
697 } else {
698 if (active_frac)
699 approx = active_count + div_u64(f, active_frac);
700 else
701 approx = active_count;
702 }
703
704 error = div_s64(active_sym - approx, tu_size);
705 error *= params->num_clocks;
706
Andrew Morton79211c82015-11-09 14:58:13 -0800707 if (error <= 0 && abs(error) < params->error) {
Thierry Reding34fa1832014-06-05 16:31:10 +0200708 params->active_count = div_u64(active_count, f);
709 params->active_polarity = active_polarity;
710 params->active_frac = active_frac;
Andrew Morton79211c82015-11-09 14:58:13 -0800711 params->error = abs(error);
Thierry Reding34fa1832014-06-05 16:31:10 +0200712 params->tu_size = tu_size;
713
714 if (error == 0)
715 return true;
716 }
717
718 return false;
719}
720
Thierry Redinga1983592015-07-21 16:46:52 +0200721static int tegra_sor_compute_config(struct tegra_sor *sor,
722 const struct drm_display_mode *mode,
723 struct tegra_sor_config *config,
724 struct drm_dp_link *link)
Thierry Reding34fa1832014-06-05 16:31:10 +0200725{
726 const u64 f = 100000, link_rate = link->rate * 1000;
727 const u64 pclk = mode->clock * 1000;
Thierry Reding7890b572014-06-05 16:12:46 +0200728 u64 input, output, watermark, num;
Thierry Reding34fa1832014-06-05 16:31:10 +0200729 struct tegra_sor_params params;
Thierry Reding34fa1832014-06-05 16:31:10 +0200730 u32 num_syms_per_line;
731 unsigned int i;
732
733 if (!link_rate || !link->num_lanes || !pclk || !config->bits_per_pixel)
734 return -EINVAL;
735
736 output = link_rate * 8 * link->num_lanes;
737 input = pclk * config->bits_per_pixel;
738
739 if (input >= output)
740 return -ERANGE;
741
742 memset(&params, 0, sizeof(params));
743 params.ratio = div64_u64(input * f, output);
744 params.num_clocks = div_u64(link_rate * mode->hdisplay, pclk);
745 params.precision = f;
746 params.error = 64 * f;
747 params.tu_size = 64;
748
749 for (i = params.tu_size; i >= 32; i--)
750 if (tegra_sor_compute_params(sor, &params, i))
751 break;
752
753 if (params.active_frac == 0) {
754 config->active_polarity = 0;
755 config->active_count = params.active_count;
756
757 if (!params.active_polarity)
758 config->active_count--;
759
760 config->tu_size = params.tu_size;
761 config->active_frac = 1;
762 } else {
763 config->active_polarity = params.active_polarity;
764 config->active_count = params.active_count;
765 config->active_frac = params.active_frac;
766 config->tu_size = params.tu_size;
767 }
768
769 dev_dbg(sor->dev,
770 "polarity: %d active count: %d tu size: %d active frac: %d\n",
771 config->active_polarity, config->active_count,
772 config->tu_size, config->active_frac);
773
774 watermark = params.ratio * config->tu_size * (f - params.ratio);
775 watermark = div_u64(watermark, f);
776
777 watermark = div_u64(watermark + params.error, f);
778 config->watermark = watermark + (config->bits_per_pixel / 8) + 2;
779 num_syms_per_line = (mode->hdisplay * config->bits_per_pixel) *
780 (link->num_lanes * 8);
781
782 if (config->watermark > 30) {
783 config->watermark = 30;
784 dev_err(sor->dev,
785 "unable to compute TU size, forcing watermark to %u\n",
786 config->watermark);
787 } else if (config->watermark > num_syms_per_line) {
788 config->watermark = num_syms_per_line;
789 dev_err(sor->dev, "watermark too high, forcing to %u\n",
790 config->watermark);
791 }
792
Thierry Reding7890b572014-06-05 16:12:46 +0200793 /* compute the number of symbols per horizontal blanking interval */
794 num = ((mode->htotal - mode->hdisplay) - 7) * link_rate;
795 config->hblank_symbols = div_u64(num, pclk);
796
797 if (link->capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
798 config->hblank_symbols -= 3;
799
800 config->hblank_symbols -= 12 / link->num_lanes;
801
802 /* compute the number of symbols per vertical blanking interval */
803 num = (mode->hdisplay - 25) * link_rate;
804 config->vblank_symbols = div_u64(num, pclk);
805 config->vblank_symbols -= 36 / link->num_lanes + 4;
806
807 dev_dbg(sor->dev, "blank symbols: H:%u V:%u\n", config->hblank_symbols,
808 config->vblank_symbols);
809
Thierry Reding34fa1832014-06-05 16:31:10 +0200810 return 0;
811}
812
Thierry Reding402f6bc2015-07-21 16:48:19 +0200813static void tegra_sor_apply_config(struct tegra_sor *sor,
814 const struct tegra_sor_config *config)
815{
816 u32 value;
817
818 value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
819 value &= ~SOR_DP_LINKCTL_TU_SIZE_MASK;
820 value |= SOR_DP_LINKCTL_TU_SIZE(config->tu_size);
821 tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
822
823 value = tegra_sor_readl(sor, SOR_DP_CONFIG0);
824 value &= ~SOR_DP_CONFIG_WATERMARK_MASK;
825 value |= SOR_DP_CONFIG_WATERMARK(config->watermark);
826
827 value &= ~SOR_DP_CONFIG_ACTIVE_SYM_COUNT_MASK;
828 value |= SOR_DP_CONFIG_ACTIVE_SYM_COUNT(config->active_count);
829
830 value &= ~SOR_DP_CONFIG_ACTIVE_SYM_FRAC_MASK;
831 value |= SOR_DP_CONFIG_ACTIVE_SYM_FRAC(config->active_frac);
832
833 if (config->active_polarity)
834 value |= SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
835 else
836 value &= ~SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
837
838 value |= SOR_DP_CONFIG_ACTIVE_SYM_ENABLE;
839 value |= SOR_DP_CONFIG_DISPARITY_NEGATIVE;
840 tegra_sor_writel(sor, value, SOR_DP_CONFIG0);
841
842 value = tegra_sor_readl(sor, SOR_DP_AUDIO_HBLANK_SYMBOLS);
843 value &= ~SOR_DP_AUDIO_HBLANK_SYMBOLS_MASK;
844 value |= config->hblank_symbols & 0xffff;
845 tegra_sor_writel(sor, value, SOR_DP_AUDIO_HBLANK_SYMBOLS);
846
847 value = tegra_sor_readl(sor, SOR_DP_AUDIO_VBLANK_SYMBOLS);
848 value &= ~SOR_DP_AUDIO_VBLANK_SYMBOLS_MASK;
849 value |= config->vblank_symbols & 0xffff;
850 tegra_sor_writel(sor, value, SOR_DP_AUDIO_VBLANK_SYMBOLS);
851}
852
Thierry Reding2bd1dd32015-08-03 15:46:15 +0200853static void tegra_sor_mode_set(struct tegra_sor *sor,
854 const struct drm_display_mode *mode,
Thierry Redingc31efa72015-09-08 16:09:22 +0200855 struct tegra_sor_state *state)
Thierry Reding2bd1dd32015-08-03 15:46:15 +0200856{
857 struct tegra_dc *dc = to_tegra_dc(sor->output.encoder.crtc);
858 unsigned int vbe, vse, hbe, hse, vbs, hbs;
859 u32 value;
860
861 value = tegra_sor_readl(sor, SOR_STATE1);
862 value &= ~SOR_STATE_ASY_PIXELDEPTH_MASK;
863 value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
864 value &= ~SOR_STATE_ASY_OWNER_MASK;
865
866 value |= SOR_STATE_ASY_CRC_MODE_COMPLETE |
867 SOR_STATE_ASY_OWNER(dc->pipe + 1);
868
869 if (mode->flags & DRM_MODE_FLAG_PHSYNC)
870 value &= ~SOR_STATE_ASY_HSYNCPOL;
871
872 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
873 value |= SOR_STATE_ASY_HSYNCPOL;
874
875 if (mode->flags & DRM_MODE_FLAG_PVSYNC)
876 value &= ~SOR_STATE_ASY_VSYNCPOL;
877
878 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
879 value |= SOR_STATE_ASY_VSYNCPOL;
880
Thierry Redingc31efa72015-09-08 16:09:22 +0200881 switch (state->bpc) {
882 case 16:
883 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_48_444;
884 break;
885
886 case 12:
887 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_36_444;
888 break;
889
890 case 10:
891 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_30_444;
892 break;
893
Thierry Reding2bd1dd32015-08-03 15:46:15 +0200894 case 8:
895 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
896 break;
897
898 case 6:
899 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_18_444;
900 break;
901
902 default:
Thierry Redingc31efa72015-09-08 16:09:22 +0200903 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
Thierry Reding2bd1dd32015-08-03 15:46:15 +0200904 break;
905 }
906
907 tegra_sor_writel(sor, value, SOR_STATE1);
908
909 /*
910 * TODO: The video timing programming below doesn't seem to match the
911 * register definitions.
912 */
913
914 value = ((mode->vtotal & 0x7fff) << 16) | (mode->htotal & 0x7fff);
Thierry Reding880cee02017-10-12 19:04:17 +0200915 tegra_sor_writel(sor, value, sor->soc->regs->head_state1 + dc->pipe);
Thierry Reding2bd1dd32015-08-03 15:46:15 +0200916
917 /* sync end = sync width - 1 */
918 vse = mode->vsync_end - mode->vsync_start - 1;
919 hse = mode->hsync_end - mode->hsync_start - 1;
920
921 value = ((vse & 0x7fff) << 16) | (hse & 0x7fff);
Thierry Reding880cee02017-10-12 19:04:17 +0200922 tegra_sor_writel(sor, value, sor->soc->regs->head_state2 + dc->pipe);
Thierry Reding2bd1dd32015-08-03 15:46:15 +0200923
924 /* blank end = sync end + back porch */
925 vbe = vse + (mode->vtotal - mode->vsync_end);
926 hbe = hse + (mode->htotal - mode->hsync_end);
927
928 value = ((vbe & 0x7fff) << 16) | (hbe & 0x7fff);
Thierry Reding880cee02017-10-12 19:04:17 +0200929 tegra_sor_writel(sor, value, sor->soc->regs->head_state3 + dc->pipe);
Thierry Reding2bd1dd32015-08-03 15:46:15 +0200930
931 /* blank start = blank end + active */
932 vbs = vbe + mode->vdisplay;
933 hbs = hbe + mode->hdisplay;
934
935 value = ((vbs & 0x7fff) << 16) | (hbs & 0x7fff);
Thierry Reding880cee02017-10-12 19:04:17 +0200936 tegra_sor_writel(sor, value, sor->soc->regs->head_state4 + dc->pipe);
Thierry Reding2bd1dd32015-08-03 15:46:15 +0200937
938 /* XXX interlacing support */
Thierry Reding880cee02017-10-12 19:04:17 +0200939 tegra_sor_writel(sor, 0x001, sor->soc->regs->head_state5 + dc->pipe);
Thierry Reding2bd1dd32015-08-03 15:46:15 +0200940}
941
Thierry Reding6fad8f62014-11-28 15:41:34 +0100942static int tegra_sor_detach(struct tegra_sor *sor)
Thierry Reding6b6b6042013-11-15 16:06:05 +0100943{
Thierry Reding6fad8f62014-11-28 15:41:34 +0100944 unsigned long value, timeout;
945
946 /* switch to safe mode */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200947 value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100948 value &= ~SOR_SUPER_STATE_MODE_NORMAL;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200949 tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100950 tegra_sor_super_update(sor);
951
952 timeout = jiffies + msecs_to_jiffies(250);
953
954 while (time_before(jiffies, timeout)) {
955 value = tegra_sor_readl(sor, SOR_PWR);
956 if (value & SOR_PWR_MODE_SAFE)
957 break;
958 }
959
960 if ((value & SOR_PWR_MODE_SAFE) == 0)
961 return -ETIMEDOUT;
962
963 /* go to sleep */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200964 value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100965 value &= ~SOR_SUPER_STATE_HEAD_MODE_MASK;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200966 tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100967 tegra_sor_super_update(sor);
968
969 /* detach */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200970 value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100971 value &= ~SOR_SUPER_STATE_ATTACHED;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200972 tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100973 tegra_sor_super_update(sor);
974
975 timeout = jiffies + msecs_to_jiffies(250);
976
977 while (time_before(jiffies, timeout)) {
978 value = tegra_sor_readl(sor, SOR_TEST);
979 if ((value & SOR_TEST_ATTACHED) == 0)
980 break;
981
982 usleep_range(25, 100);
983 }
984
985 if ((value & SOR_TEST_ATTACHED) != 0)
986 return -ETIMEDOUT;
987
988 return 0;
989}
990
991static int tegra_sor_power_down(struct tegra_sor *sor)
992{
993 unsigned long value, timeout;
994 int err;
995
996 value = tegra_sor_readl(sor, SOR_PWR);
997 value &= ~SOR_PWR_NORMAL_STATE_PU;
998 value |= SOR_PWR_TRIGGER;
999 tegra_sor_writel(sor, value, SOR_PWR);
1000
1001 timeout = jiffies + msecs_to_jiffies(250);
1002
1003 while (time_before(jiffies, timeout)) {
1004 value = tegra_sor_readl(sor, SOR_PWR);
1005 if ((value & SOR_PWR_TRIGGER) == 0)
1006 return 0;
1007
1008 usleep_range(25, 100);
1009 }
1010
1011 if ((value & SOR_PWR_TRIGGER) != 0)
1012 return -ETIMEDOUT;
1013
Thierry Reding25bb2ce2015-08-03 14:23:29 +02001014 /* switch to safe parent clock */
1015 err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
Thierry Redinge1335e22017-10-12 17:53:11 +02001016 if (err < 0) {
Thierry Reding6fad8f62014-11-28 15:41:34 +01001017 dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
Thierry Redinge1335e22017-10-12 17:53:11 +02001018 return err;
1019 }
Thierry Reding6fad8f62014-11-28 15:41:34 +01001020
Thierry Reding880cee02017-10-12 19:04:17 +02001021 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001022 value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
1023 SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2);
Thierry Reding880cee02017-10-12 19:04:17 +02001024 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001025
1026 /* stop lane sequencer */
1027 value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_UP |
1028 SOR_LANE_SEQ_CTL_POWER_STATE_DOWN;
1029 tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
1030
1031 timeout = jiffies + msecs_to_jiffies(250);
1032
1033 while (time_before(jiffies, timeout)) {
1034 value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
1035 if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
1036 break;
1037
1038 usleep_range(25, 100);
1039 }
1040
1041 if ((value & SOR_LANE_SEQ_CTL_TRIGGER) != 0)
1042 return -ETIMEDOUT;
1043
Thierry Reding880cee02017-10-12 19:04:17 +02001044 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001045 value |= SOR_PLL2_PORT_POWERDOWN;
Thierry Reding880cee02017-10-12 19:04:17 +02001046 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001047
1048 usleep_range(20, 100);
1049
Thierry Reding880cee02017-10-12 19:04:17 +02001050 value = tegra_sor_readl(sor, sor->soc->regs->pll0);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001051 value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
Thierry Reding880cee02017-10-12 19:04:17 +02001052 tegra_sor_writel(sor, value, sor->soc->regs->pll0);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001053
Thierry Reding880cee02017-10-12 19:04:17 +02001054 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001055 value |= SOR_PLL2_SEQ_PLLCAPPD;
1056 value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
Thierry Reding880cee02017-10-12 19:04:17 +02001057 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001058
1059 usleep_range(20, 100);
1060
1061 return 0;
1062}
1063
Thierry Reding6fad8f62014-11-28 15:41:34 +01001064static int tegra_sor_crc_wait(struct tegra_sor *sor, unsigned long timeout)
1065{
1066 u32 value;
1067
1068 timeout = jiffies + msecs_to_jiffies(timeout);
1069
1070 while (time_before(jiffies, timeout)) {
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001071 value = tegra_sor_readl(sor, SOR_CRCA);
1072 if (value & SOR_CRCA_VALID)
Thierry Reding6fad8f62014-11-28 15:41:34 +01001073 return 0;
1074
1075 usleep_range(100, 200);
1076 }
1077
1078 return -ETIMEDOUT;
1079}
1080
Thierry Reding530239a2015-08-06 11:04:54 +02001081static int tegra_sor_show_crc(struct seq_file *s, void *data)
Thierry Reding6fad8f62014-11-28 15:41:34 +01001082{
Thierry Reding530239a2015-08-06 11:04:54 +02001083 struct drm_info_node *node = s->private;
1084 struct tegra_sor *sor = node->info_ent->data;
Thierry Reding850bab42015-07-29 17:58:41 +02001085 struct drm_crtc *crtc = sor->output.encoder.crtc;
1086 struct drm_device *drm = node->minor->dev;
Thierry Reding530239a2015-08-06 11:04:54 +02001087 int err = 0;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001088 u32 value;
1089
Thierry Reding850bab42015-07-29 17:58:41 +02001090 drm_modeset_lock_all(drm);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001091
Thierry Reding850bab42015-07-29 17:58:41 +02001092 if (!crtc || !crtc->state->active) {
1093 err = -EBUSY;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001094 goto unlock;
1095 }
1096
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001097 value = tegra_sor_readl(sor, SOR_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001098 value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001099 tegra_sor_writel(sor, value, SOR_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001100
1101 value = tegra_sor_readl(sor, SOR_CRC_CNTRL);
1102 value |= SOR_CRC_CNTRL_ENABLE;
1103 tegra_sor_writel(sor, value, SOR_CRC_CNTRL);
1104
1105 value = tegra_sor_readl(sor, SOR_TEST);
1106 value &= ~SOR_TEST_CRC_POST_SERIALIZE;
1107 tegra_sor_writel(sor, value, SOR_TEST);
1108
1109 err = tegra_sor_crc_wait(sor, 100);
1110 if (err < 0)
1111 goto unlock;
1112
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001113 tegra_sor_writel(sor, SOR_CRCA_RESET, SOR_CRCA);
1114 value = tegra_sor_readl(sor, SOR_CRCB);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001115
Thierry Reding530239a2015-08-06 11:04:54 +02001116 seq_printf(s, "%08x\n", value);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001117
1118unlock:
Thierry Reding850bab42015-07-29 17:58:41 +02001119 drm_modeset_unlock_all(drm);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001120 return err;
1121}
1122
Thierry Reding062f5b2c2017-11-10 12:21:51 +01001123#define DEBUGFS_REG32(_name) { .name = #_name, .offset = _name }
1124
1125static const struct debugfs_reg32 tegra_sor_regs[] = {
1126 DEBUGFS_REG32(SOR_CTXSW),
1127 DEBUGFS_REG32(SOR_SUPER_STATE0),
1128 DEBUGFS_REG32(SOR_SUPER_STATE1),
1129 DEBUGFS_REG32(SOR_STATE0),
1130 DEBUGFS_REG32(SOR_STATE1),
1131 DEBUGFS_REG32(SOR_HEAD_STATE0(0)),
1132 DEBUGFS_REG32(SOR_HEAD_STATE0(1)),
1133 DEBUGFS_REG32(SOR_HEAD_STATE1(0)),
1134 DEBUGFS_REG32(SOR_HEAD_STATE1(1)),
1135 DEBUGFS_REG32(SOR_HEAD_STATE2(0)),
1136 DEBUGFS_REG32(SOR_HEAD_STATE2(1)),
1137 DEBUGFS_REG32(SOR_HEAD_STATE3(0)),
1138 DEBUGFS_REG32(SOR_HEAD_STATE3(1)),
1139 DEBUGFS_REG32(SOR_HEAD_STATE4(0)),
1140 DEBUGFS_REG32(SOR_HEAD_STATE4(1)),
1141 DEBUGFS_REG32(SOR_HEAD_STATE5(0)),
1142 DEBUGFS_REG32(SOR_HEAD_STATE5(1)),
1143 DEBUGFS_REG32(SOR_CRC_CNTRL),
1144 DEBUGFS_REG32(SOR_DP_DEBUG_MVID),
1145 DEBUGFS_REG32(SOR_CLK_CNTRL),
1146 DEBUGFS_REG32(SOR_CAP),
1147 DEBUGFS_REG32(SOR_PWR),
1148 DEBUGFS_REG32(SOR_TEST),
1149 DEBUGFS_REG32(SOR_PLL0),
1150 DEBUGFS_REG32(SOR_PLL1),
1151 DEBUGFS_REG32(SOR_PLL2),
1152 DEBUGFS_REG32(SOR_PLL3),
1153 DEBUGFS_REG32(SOR_CSTM),
1154 DEBUGFS_REG32(SOR_LVDS),
1155 DEBUGFS_REG32(SOR_CRCA),
1156 DEBUGFS_REG32(SOR_CRCB),
1157 DEBUGFS_REG32(SOR_BLANK),
1158 DEBUGFS_REG32(SOR_SEQ_CTL),
1159 DEBUGFS_REG32(SOR_LANE_SEQ_CTL),
1160 DEBUGFS_REG32(SOR_SEQ_INST(0)),
1161 DEBUGFS_REG32(SOR_SEQ_INST(1)),
1162 DEBUGFS_REG32(SOR_SEQ_INST(2)),
1163 DEBUGFS_REG32(SOR_SEQ_INST(3)),
1164 DEBUGFS_REG32(SOR_SEQ_INST(4)),
1165 DEBUGFS_REG32(SOR_SEQ_INST(5)),
1166 DEBUGFS_REG32(SOR_SEQ_INST(6)),
1167 DEBUGFS_REG32(SOR_SEQ_INST(7)),
1168 DEBUGFS_REG32(SOR_SEQ_INST(8)),
1169 DEBUGFS_REG32(SOR_SEQ_INST(9)),
1170 DEBUGFS_REG32(SOR_SEQ_INST(10)),
1171 DEBUGFS_REG32(SOR_SEQ_INST(11)),
1172 DEBUGFS_REG32(SOR_SEQ_INST(12)),
1173 DEBUGFS_REG32(SOR_SEQ_INST(13)),
1174 DEBUGFS_REG32(SOR_SEQ_INST(14)),
1175 DEBUGFS_REG32(SOR_SEQ_INST(15)),
1176 DEBUGFS_REG32(SOR_PWM_DIV),
1177 DEBUGFS_REG32(SOR_PWM_CTL),
1178 DEBUGFS_REG32(SOR_VCRC_A0),
1179 DEBUGFS_REG32(SOR_VCRC_A1),
1180 DEBUGFS_REG32(SOR_VCRC_B0),
1181 DEBUGFS_REG32(SOR_VCRC_B1),
1182 DEBUGFS_REG32(SOR_CCRC_A0),
1183 DEBUGFS_REG32(SOR_CCRC_A1),
1184 DEBUGFS_REG32(SOR_CCRC_B0),
1185 DEBUGFS_REG32(SOR_CCRC_B1),
1186 DEBUGFS_REG32(SOR_EDATA_A0),
1187 DEBUGFS_REG32(SOR_EDATA_A1),
1188 DEBUGFS_REG32(SOR_EDATA_B0),
1189 DEBUGFS_REG32(SOR_EDATA_B1),
1190 DEBUGFS_REG32(SOR_COUNT_A0),
1191 DEBUGFS_REG32(SOR_COUNT_A1),
1192 DEBUGFS_REG32(SOR_COUNT_B0),
1193 DEBUGFS_REG32(SOR_COUNT_B1),
1194 DEBUGFS_REG32(SOR_DEBUG_A0),
1195 DEBUGFS_REG32(SOR_DEBUG_A1),
1196 DEBUGFS_REG32(SOR_DEBUG_B0),
1197 DEBUGFS_REG32(SOR_DEBUG_B1),
1198 DEBUGFS_REG32(SOR_TRIG),
1199 DEBUGFS_REG32(SOR_MSCHECK),
1200 DEBUGFS_REG32(SOR_XBAR_CTRL),
1201 DEBUGFS_REG32(SOR_XBAR_POL),
1202 DEBUGFS_REG32(SOR_DP_LINKCTL0),
1203 DEBUGFS_REG32(SOR_DP_LINKCTL1),
1204 DEBUGFS_REG32(SOR_LANE_DRIVE_CURRENT0),
1205 DEBUGFS_REG32(SOR_LANE_DRIVE_CURRENT1),
1206 DEBUGFS_REG32(SOR_LANE4_DRIVE_CURRENT0),
1207 DEBUGFS_REG32(SOR_LANE4_DRIVE_CURRENT1),
1208 DEBUGFS_REG32(SOR_LANE_PREEMPHASIS0),
1209 DEBUGFS_REG32(SOR_LANE_PREEMPHASIS1),
1210 DEBUGFS_REG32(SOR_LANE4_PREEMPHASIS0),
1211 DEBUGFS_REG32(SOR_LANE4_PREEMPHASIS1),
1212 DEBUGFS_REG32(SOR_LANE_POSTCURSOR0),
1213 DEBUGFS_REG32(SOR_LANE_POSTCURSOR1),
1214 DEBUGFS_REG32(SOR_DP_CONFIG0),
1215 DEBUGFS_REG32(SOR_DP_CONFIG1),
1216 DEBUGFS_REG32(SOR_DP_MN0),
1217 DEBUGFS_REG32(SOR_DP_MN1),
1218 DEBUGFS_REG32(SOR_DP_PADCTL0),
1219 DEBUGFS_REG32(SOR_DP_PADCTL1),
1220 DEBUGFS_REG32(SOR_DP_DEBUG0),
1221 DEBUGFS_REG32(SOR_DP_DEBUG1),
1222 DEBUGFS_REG32(SOR_DP_SPARE0),
1223 DEBUGFS_REG32(SOR_DP_SPARE1),
1224 DEBUGFS_REG32(SOR_DP_AUDIO_CTRL),
1225 DEBUGFS_REG32(SOR_DP_AUDIO_HBLANK_SYMBOLS),
1226 DEBUGFS_REG32(SOR_DP_AUDIO_VBLANK_SYMBOLS),
1227 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_HEADER),
1228 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK0),
1229 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK1),
1230 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK2),
1231 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK3),
1232 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK4),
1233 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK5),
1234 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK6),
1235 DEBUGFS_REG32(SOR_DP_TPG),
1236 DEBUGFS_REG32(SOR_DP_TPG_CONFIG),
1237 DEBUGFS_REG32(SOR_DP_LQ_CSTM0),
1238 DEBUGFS_REG32(SOR_DP_LQ_CSTM1),
1239 DEBUGFS_REG32(SOR_DP_LQ_CSTM2),
1240};
1241
Thierry Redingdab16332015-01-26 16:04:08 +01001242static int tegra_sor_show_regs(struct seq_file *s, void *data)
1243{
1244 struct drm_info_node *node = s->private;
1245 struct tegra_sor *sor = node->info_ent->data;
Thierry Reding850bab42015-07-29 17:58:41 +02001246 struct drm_crtc *crtc = sor->output.encoder.crtc;
1247 struct drm_device *drm = node->minor->dev;
Thierry Reding062f5b2c2017-11-10 12:21:51 +01001248 unsigned int i;
Thierry Reding850bab42015-07-29 17:58:41 +02001249 int err = 0;
1250
1251 drm_modeset_lock_all(drm);
1252
1253 if (!crtc || !crtc->state->active) {
1254 err = -EBUSY;
1255 goto unlock;
1256 }
Thierry Redingdab16332015-01-26 16:04:08 +01001257
Thierry Reding062f5b2c2017-11-10 12:21:51 +01001258 for (i = 0; i < ARRAY_SIZE(tegra_sor_regs); i++) {
1259 unsigned int offset = tegra_sor_regs[i].offset;
Thierry Redingdab16332015-01-26 16:04:08 +01001260
Thierry Reding062f5b2c2017-11-10 12:21:51 +01001261 seq_printf(s, "%-38s %#05x %08x\n", tegra_sor_regs[i].name,
1262 offset, tegra_sor_readl(sor, offset));
1263 }
Thierry Redingdab16332015-01-26 16:04:08 +01001264
Thierry Reding850bab42015-07-29 17:58:41 +02001265unlock:
1266 drm_modeset_unlock_all(drm);
1267 return err;
Thierry Redingdab16332015-01-26 16:04:08 +01001268}
1269
1270static const struct drm_info_list debugfs_files[] = {
Thierry Reding530239a2015-08-06 11:04:54 +02001271 { "crc", tegra_sor_show_crc, 0, NULL },
Thierry Redingdab16332015-01-26 16:04:08 +01001272 { "regs", tegra_sor_show_regs, 0, NULL },
1273};
1274
Thierry Reding5b8e0432017-11-08 13:20:01 +01001275static int tegra_sor_late_register(struct drm_connector *connector)
Thierry Reding6fad8f62014-11-28 15:41:34 +01001276{
Thierry Reding5b8e0432017-11-08 13:20:01 +01001277 struct tegra_output *output = connector_to_output(connector);
1278 unsigned int i, count = ARRAY_SIZE(debugfs_files);
1279 struct drm_minor *minor = connector->dev->primary;
1280 struct dentry *root = connector->debugfs_entry;
1281 struct tegra_sor *sor = to_sor(output);
Thierry Reding530239a2015-08-06 11:04:54 +02001282 int err;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001283
Thierry Redingdab16332015-01-26 16:04:08 +01001284 sor->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1285 GFP_KERNEL);
Thierry Reding5b8e0432017-11-08 13:20:01 +01001286 if (!sor->debugfs_files)
1287 return -ENOMEM;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001288
Thierry Reding5b8e0432017-11-08 13:20:01 +01001289 for (i = 0; i < count; i++)
Thierry Redingdab16332015-01-26 16:04:08 +01001290 sor->debugfs_files[i].data = sor;
1291
Thierry Reding5b8e0432017-11-08 13:20:01 +01001292 err = drm_debugfs_create_files(sor->debugfs_files, count, root, minor);
Thierry Redingdab16332015-01-26 16:04:08 +01001293 if (err < 0)
1294 goto free;
1295
Thierry Reding530239a2015-08-06 11:04:54 +02001296 return 0;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001297
Thierry Redingdab16332015-01-26 16:04:08 +01001298free:
1299 kfree(sor->debugfs_files);
1300 sor->debugfs_files = NULL;
Thierry Reding5b8e0432017-11-08 13:20:01 +01001301
Thierry Reding6fad8f62014-11-28 15:41:34 +01001302 return err;
1303}
1304
Thierry Reding5b8e0432017-11-08 13:20:01 +01001305static void tegra_sor_early_unregister(struct drm_connector *connector)
Thierry Reding6fad8f62014-11-28 15:41:34 +01001306{
Thierry Reding5b8e0432017-11-08 13:20:01 +01001307 struct tegra_output *output = connector_to_output(connector);
1308 unsigned int count = ARRAY_SIZE(debugfs_files);
1309 struct tegra_sor *sor = to_sor(output);
Thierry Redingd92e6002017-10-12 19:07:54 +02001310
Thierry Reding5b8e0432017-11-08 13:20:01 +01001311 drm_debugfs_remove_files(sor->debugfs_files, count,
1312 connector->dev->primary);
Thierry Redingdab16332015-01-26 16:04:08 +01001313 kfree(sor->debugfs_files);
Thierry Reding066d30f2015-07-03 14:16:30 +02001314 sor->debugfs_files = NULL;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001315}
1316
Thierry Redingc31efa72015-09-08 16:09:22 +02001317static void tegra_sor_connector_reset(struct drm_connector *connector)
1318{
1319 struct tegra_sor_state *state;
1320
1321 state = kzalloc(sizeof(*state), GFP_KERNEL);
1322 if (!state)
1323 return;
1324
1325 if (connector->state) {
1326 __drm_atomic_helper_connector_destroy_state(connector->state);
1327 kfree(connector->state);
1328 }
1329
1330 __drm_atomic_helper_connector_reset(connector, &state->base);
1331}
1332
Thierry Reding6fad8f62014-11-28 15:41:34 +01001333static enum drm_connector_status
1334tegra_sor_connector_detect(struct drm_connector *connector, bool force)
1335{
1336 struct tegra_output *output = connector_to_output(connector);
1337 struct tegra_sor *sor = to_sor(output);
1338
Thierry Reding9542c232015-07-08 13:39:09 +02001339 if (sor->aux)
1340 return drm_dp_aux_detect(sor->aux);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001341
Thierry Reding459cc2c2015-07-30 10:34:24 +02001342 return tegra_output_connector_detect(connector, force);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001343}
1344
Thierry Redingc31efa72015-09-08 16:09:22 +02001345static struct drm_connector_state *
1346tegra_sor_connector_duplicate_state(struct drm_connector *connector)
1347{
1348 struct tegra_sor_state *state = to_sor_state(connector->state);
1349 struct tegra_sor_state *copy;
1350
1351 copy = kmemdup(state, sizeof(*state), GFP_KERNEL);
1352 if (!copy)
1353 return NULL;
1354
1355 __drm_atomic_helper_connector_duplicate_state(connector, &copy->base);
1356
1357 return &copy->base;
1358}
1359
Thierry Reding6fad8f62014-11-28 15:41:34 +01001360static const struct drm_connector_funcs tegra_sor_connector_funcs = {
Thierry Redingc31efa72015-09-08 16:09:22 +02001361 .reset = tegra_sor_connector_reset,
Thierry Reding6fad8f62014-11-28 15:41:34 +01001362 .detect = tegra_sor_connector_detect,
1363 .fill_modes = drm_helper_probe_single_connector_modes,
1364 .destroy = tegra_output_connector_destroy,
Thierry Redingc31efa72015-09-08 16:09:22 +02001365 .atomic_duplicate_state = tegra_sor_connector_duplicate_state,
Thierry Reding4aa3df72014-11-24 16:27:13 +01001366 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
Thierry Reding5b8e0432017-11-08 13:20:01 +01001367 .late_register = tegra_sor_late_register,
1368 .early_unregister = tegra_sor_early_unregister,
Thierry Reding6fad8f62014-11-28 15:41:34 +01001369};
1370
1371static int tegra_sor_connector_get_modes(struct drm_connector *connector)
1372{
1373 struct tegra_output *output = connector_to_output(connector);
1374 struct tegra_sor *sor = to_sor(output);
1375 int err;
1376
Thierry Reding9542c232015-07-08 13:39:09 +02001377 if (sor->aux)
1378 drm_dp_aux_enable(sor->aux);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001379
1380 err = tegra_output_connector_get_modes(connector);
1381
Thierry Reding9542c232015-07-08 13:39:09 +02001382 if (sor->aux)
1383 drm_dp_aux_disable(sor->aux);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001384
1385 return err;
1386}
1387
1388static enum drm_mode_status
1389tegra_sor_connector_mode_valid(struct drm_connector *connector,
1390 struct drm_display_mode *mode)
1391{
Thierry Reding64ea25c2016-07-12 16:52:22 +02001392 /* HDMI 2.0 modes are not yet supported */
1393 if (mode->clock > 340000)
1394 return MODE_NOCLOCK;
1395
Thierry Reding6fad8f62014-11-28 15:41:34 +01001396 return MODE_OK;
1397}
1398
1399static const struct drm_connector_helper_funcs tegra_sor_connector_helper_funcs = {
1400 .get_modes = tegra_sor_connector_get_modes,
1401 .mode_valid = tegra_sor_connector_mode_valid,
Thierry Reding6fad8f62014-11-28 15:41:34 +01001402};
1403
1404static const struct drm_encoder_funcs tegra_sor_encoder_funcs = {
1405 .destroy = tegra_output_encoder_destroy,
1406};
1407
Thierry Reding850bab42015-07-29 17:58:41 +02001408static void tegra_sor_edp_disable(struct drm_encoder *encoder)
Thierry Reding6fad8f62014-11-28 15:41:34 +01001409{
Thierry Reding850bab42015-07-29 17:58:41 +02001410 struct tegra_output *output = encoder_to_output(encoder);
1411 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
1412 struct tegra_sor *sor = to_sor(output);
1413 u32 value;
1414 int err;
1415
1416 if (output->panel)
1417 drm_panel_disable(output->panel);
1418
1419 err = tegra_sor_detach(sor);
1420 if (err < 0)
1421 dev_err(sor->dev, "failed to detach SOR: %d\n", err);
1422
1423 tegra_sor_writel(sor, 0, SOR_STATE1);
1424 tegra_sor_update(sor);
1425
1426 /*
1427 * The following accesses registers of the display controller, so make
1428 * sure it's only executed when the output is attached to one.
1429 */
1430 if (dc) {
1431 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
1432 value &= ~SOR_ENABLE;
1433 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
1434
1435 tegra_dc_commit(dc);
1436 }
1437
1438 err = tegra_sor_power_down(sor);
1439 if (err < 0)
1440 dev_err(sor->dev, "failed to power down SOR: %d\n", err);
1441
Thierry Reding9542c232015-07-08 13:39:09 +02001442 if (sor->aux) {
1443 err = drm_dp_aux_disable(sor->aux);
Thierry Reding850bab42015-07-29 17:58:41 +02001444 if (err < 0)
1445 dev_err(sor->dev, "failed to disable DP: %d\n", err);
1446 }
1447
1448 err = tegra_io_rail_power_off(TEGRA_IO_RAIL_LVDS);
1449 if (err < 0)
1450 dev_err(sor->dev, "failed to power off I/O rail: %d\n", err);
1451
1452 if (output->panel)
1453 drm_panel_unprepare(output->panel);
1454
Thierry Redingaaff8bd2015-08-07 16:04:54 +02001455 pm_runtime_put(sor->dev);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001456}
1457
Thierry Reding459cc2c2015-07-30 10:34:24 +02001458#if 0
1459static int calc_h_ref_to_sync(const struct drm_display_mode *mode,
1460 unsigned int *value)
1461{
1462 unsigned int hfp, hsw, hbp, a = 0, b;
1463
1464 hfp = mode->hsync_start - mode->hdisplay;
1465 hsw = mode->hsync_end - mode->hsync_start;
1466 hbp = mode->htotal - mode->hsync_end;
1467
1468 pr_info("hfp: %u, hsw: %u, hbp: %u\n", hfp, hsw, hbp);
1469
1470 b = hfp - 1;
1471
1472 pr_info("a: %u, b: %u\n", a, b);
1473 pr_info("a + hsw + hbp = %u\n", a + hsw + hbp);
1474
1475 if (a + hsw + hbp <= 11) {
1476 a = 1 + 11 - hsw - hbp;
1477 pr_info("a: %u\n", a);
1478 }
1479
1480 if (a > b)
1481 return -EINVAL;
1482
1483 if (hsw < 1)
1484 return -EINVAL;
1485
1486 if (mode->hdisplay < 16)
1487 return -EINVAL;
1488
1489 if (value) {
1490 if (b > a && a % 2)
1491 *value = a + 1;
1492 else
1493 *value = a;
1494 }
1495
1496 return 0;
1497}
1498#endif
1499
Thierry Reding850bab42015-07-29 17:58:41 +02001500static void tegra_sor_edp_enable(struct drm_encoder *encoder)
Thierry Reding6fad8f62014-11-28 15:41:34 +01001501{
Thierry Reding850bab42015-07-29 17:58:41 +02001502 struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001503 struct tegra_output *output = encoder_to_output(encoder);
1504 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001505 struct tegra_sor *sor = to_sor(output);
Thierry Reding34fa1832014-06-05 16:31:10 +02001506 struct tegra_sor_config config;
Thierry Redingc31efa72015-09-08 16:09:22 +02001507 struct tegra_sor_state *state;
Thierry Reding34fa1832014-06-05 16:31:10 +02001508 struct drm_dp_link link;
Thierry Reding01b9bea2015-11-11 17:15:29 +01001509 u8 rate, lanes;
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001510 unsigned int i;
Thierry Reding86f5c522014-03-26 11:13:16 +01001511 int err = 0;
Thierry Reding28fe2072015-01-26 16:02:48 +01001512 u32 value;
Thierry Reding86f5c522014-03-26 11:13:16 +01001513
Thierry Redingc31efa72015-09-08 16:09:22 +02001514 state = to_sor_state(output->connector.state);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001515
Thierry Redingaaff8bd2015-08-07 16:04:54 +02001516 pm_runtime_get_sync(sor->dev);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001517
Thierry Reding6fad8f62014-11-28 15:41:34 +01001518 if (output->panel)
1519 drm_panel_prepare(output->panel);
1520
Thierry Reding01b9bea2015-11-11 17:15:29 +01001521 err = drm_dp_aux_enable(sor->aux);
1522 if (err < 0)
1523 dev_err(sor->dev, "failed to enable DP: %d\n", err);
Thierry Reding34fa1832014-06-05 16:31:10 +02001524
Thierry Reding01b9bea2015-11-11 17:15:29 +01001525 err = drm_dp_link_probe(sor->aux, &link);
1526 if (err < 0) {
1527 dev_err(sor->dev, "failed to probe eDP link: %d\n", err);
1528 return;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001529 }
1530
Thierry Reding25bb2ce2015-08-03 14:23:29 +02001531 /* switch to safe parent clock */
1532 err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001533 if (err < 0)
1534 dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
1535
Thierry Reding34fa1832014-06-05 16:31:10 +02001536 memset(&config, 0, sizeof(config));
Thierry Redingc31efa72015-09-08 16:09:22 +02001537 config.bits_per_pixel = state->bpc * 3;
Thierry Reding34fa1832014-06-05 16:31:10 +02001538
Thierry Redinga1983592015-07-21 16:46:52 +02001539 err = tegra_sor_compute_config(sor, mode, &config, &link);
Thierry Reding34fa1832014-06-05 16:31:10 +02001540 if (err < 0)
Thierry Redinga1983592015-07-21 16:46:52 +02001541 dev_err(sor->dev, "failed to compute configuration: %d\n", err);
Thierry Reding34fa1832014-06-05 16:31:10 +02001542
Thierry Reding6b6b6042013-11-15 16:06:05 +01001543 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
1544 value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
1545 value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
1546 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
1547
Thierry Reding880cee02017-10-12 19:04:17 +02001548 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001549 value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
Thierry Reding880cee02017-10-12 19:04:17 +02001550 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001551 usleep_range(20, 100);
1552
Thierry Reding880cee02017-10-12 19:04:17 +02001553 value = tegra_sor_readl(sor, sor->soc->regs->pll3);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001554 value |= SOR_PLL3_PLL_VDD_MODE_3V3;
Thierry Reding880cee02017-10-12 19:04:17 +02001555 tegra_sor_writel(sor, value, sor->soc->regs->pll3);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001556
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001557 value = SOR_PLL0_ICHPMP(0xf) | SOR_PLL0_VCOCAP_RST |
1558 SOR_PLL0_PLLREG_LEVEL_V45 | SOR_PLL0_RESISTOR_EXT;
Thierry Reding880cee02017-10-12 19:04:17 +02001559 tegra_sor_writel(sor, value, sor->soc->regs->pll0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001560
Thierry Reding880cee02017-10-12 19:04:17 +02001561 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001562 value |= SOR_PLL2_SEQ_PLLCAPPD;
1563 value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1564 value |= SOR_PLL2_LVDS_ENABLE;
Thierry Reding880cee02017-10-12 19:04:17 +02001565 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001566
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001567 value = SOR_PLL1_TERM_COMPOUT | SOR_PLL1_TMDS_TERM;
Thierry Reding880cee02017-10-12 19:04:17 +02001568 tegra_sor_writel(sor, value, sor->soc->regs->pll1);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001569
1570 while (true) {
Thierry Reding880cee02017-10-12 19:04:17 +02001571 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001572 if ((value & SOR_PLL2_SEQ_PLLCAPPD_ENFORCE) == 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001573 break;
1574
1575 usleep_range(250, 1000);
1576 }
1577
Thierry Reding880cee02017-10-12 19:04:17 +02001578 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001579 value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
1580 value &= ~SOR_PLL2_PORT_POWERDOWN;
Thierry Reding880cee02017-10-12 19:04:17 +02001581 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001582
1583 /*
1584 * power up
1585 */
1586
1587 /* set safe link bandwidth (1.62 Gbps) */
1588 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
1589 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
1590 value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G1_62;
1591 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
1592
1593 /* step 1 */
Thierry Reding880cee02017-10-12 19:04:17 +02001594 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001595 value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE | SOR_PLL2_PORT_POWERDOWN |
1596 SOR_PLL2_BANDGAP_POWERDOWN;
Thierry Reding880cee02017-10-12 19:04:17 +02001597 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001598
Thierry Reding880cee02017-10-12 19:04:17 +02001599 value = tegra_sor_readl(sor, sor->soc->regs->pll0);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001600 value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
Thierry Reding880cee02017-10-12 19:04:17 +02001601 tegra_sor_writel(sor, value, sor->soc->regs->pll0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001602
Thierry Reding880cee02017-10-12 19:04:17 +02001603 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001604 value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
Thierry Reding880cee02017-10-12 19:04:17 +02001605 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001606
1607 /* step 2 */
1608 err = tegra_io_rail_power_on(TEGRA_IO_RAIL_LVDS);
Thierry Reding850bab42015-07-29 17:58:41 +02001609 if (err < 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001610 dev_err(sor->dev, "failed to power on I/O rail: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001611
1612 usleep_range(5, 100);
1613
1614 /* step 3 */
Thierry Reding880cee02017-10-12 19:04:17 +02001615 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001616 value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
Thierry Reding880cee02017-10-12 19:04:17 +02001617 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001618
1619 usleep_range(20, 100);
1620
1621 /* step 4 */
Thierry Reding880cee02017-10-12 19:04:17 +02001622 value = tegra_sor_readl(sor, sor->soc->regs->pll0);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001623 value &= ~SOR_PLL0_VCOPD;
1624 value &= ~SOR_PLL0_PWR;
Thierry Reding880cee02017-10-12 19:04:17 +02001625 tegra_sor_writel(sor, value, sor->soc->regs->pll0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001626
Thierry Reding880cee02017-10-12 19:04:17 +02001627 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001628 value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
Thierry Reding880cee02017-10-12 19:04:17 +02001629 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001630
1631 usleep_range(200, 1000);
1632
1633 /* step 5 */
Thierry Reding880cee02017-10-12 19:04:17 +02001634 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001635 value &= ~SOR_PLL2_PORT_POWERDOWN;
Thierry Reding880cee02017-10-12 19:04:17 +02001636 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001637
Thierry Reding30b49432015-08-03 15:50:32 +02001638 /* XXX not in TRM */
1639 for (value = 0, i = 0; i < 5; i++)
1640 value |= SOR_XBAR_CTRL_LINK0_XSEL(i, sor->soc->xbar_cfg[i]) |
1641 SOR_XBAR_CTRL_LINK1_XSEL(i, i);
1642
1643 tegra_sor_writel(sor, 0x00000000, SOR_XBAR_POL);
1644 tegra_sor_writel(sor, value, SOR_XBAR_CTRL);
1645
Thierry Reding25bb2ce2015-08-03 14:23:29 +02001646 /* switch to DP parent clock */
1647 err = tegra_sor_set_parent_clock(sor, sor->clk_dp);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001648 if (err < 0)
Thierry Reding25bb2ce2015-08-03 14:23:29 +02001649 dev_err(sor->dev, "failed to set parent clock: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001650
Thierry Reding899451b2014-06-05 16:19:48 +02001651 /* power DP lanes */
Thierry Reding880cee02017-10-12 19:04:17 +02001652 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding899451b2014-06-05 16:19:48 +02001653
1654 if (link.num_lanes <= 2)
1655 value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2);
1656 else
1657 value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2;
1658
1659 if (link.num_lanes <= 1)
1660 value &= ~SOR_DP_PADCTL_PD_TXD_1;
1661 else
1662 value |= SOR_DP_PADCTL_PD_TXD_1;
1663
1664 if (link.num_lanes == 0)
1665 value &= ~SOR_DP_PADCTL_PD_TXD_0;
1666 else
1667 value |= SOR_DP_PADCTL_PD_TXD_0;
1668
Thierry Reding880cee02017-10-12 19:04:17 +02001669 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001670
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001671 value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001672 value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
Thierry Reding0c90a182014-06-05 16:29:46 +02001673 value |= SOR_DP_LINKCTL_LANE_COUNT(link.num_lanes);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001674 tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001675
1676 /* start lane sequencer */
1677 value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
1678 SOR_LANE_SEQ_CTL_POWER_STATE_UP;
1679 tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
1680
1681 while (true) {
1682 value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
1683 if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
1684 break;
1685
1686 usleep_range(250, 1000);
1687 }
1688
Thierry Redinga4263fe2014-06-05 16:16:23 +02001689 /* set link bandwidth */
Thierry Reding6b6b6042013-11-15 16:06:05 +01001690 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
1691 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
Thierry Redinga4263fe2014-06-05 16:16:23 +02001692 value |= drm_dp_link_rate_to_bw_code(link.rate) << 2;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001693 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
1694
Thierry Reding402f6bc2015-07-21 16:48:19 +02001695 tegra_sor_apply_config(sor, &config);
1696
1697 /* enable link */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001698 value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001699 value |= SOR_DP_LINKCTL_ENABLE;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001700 value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001701 tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001702
1703 for (i = 0, value = 0; i < 4; i++) {
1704 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
1705 SOR_DP_TPG_SCRAMBLER_GALIOS |
1706 SOR_DP_TPG_PATTERN_NONE;
1707 value = (value << 8) | lane;
1708 }
1709
1710 tegra_sor_writel(sor, value, SOR_DP_TPG);
1711
Thierry Reding6b6b6042013-11-15 16:06:05 +01001712 /* enable pad calibration logic */
Thierry Reding880cee02017-10-12 19:04:17 +02001713 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001714 value |= SOR_DP_PADCTL_PAD_CAL_PD;
Thierry Reding880cee02017-10-12 19:04:17 +02001715 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001716
Thierry Reding01b9bea2015-11-11 17:15:29 +01001717 err = drm_dp_link_probe(sor->aux, &link);
1718 if (err < 0)
1719 dev_err(sor->dev, "failed to probe eDP link: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001720
Thierry Reding01b9bea2015-11-11 17:15:29 +01001721 err = drm_dp_link_power_up(sor->aux, &link);
1722 if (err < 0)
1723 dev_err(sor->dev, "failed to power up eDP link: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001724
Thierry Reding01b9bea2015-11-11 17:15:29 +01001725 err = drm_dp_link_configure(sor->aux, &link);
1726 if (err < 0)
1727 dev_err(sor->dev, "failed to configure eDP link: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001728
Thierry Reding01b9bea2015-11-11 17:15:29 +01001729 rate = drm_dp_link_rate_to_bw_code(link.rate);
1730 lanes = link.num_lanes;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001731
Thierry Reding01b9bea2015-11-11 17:15:29 +01001732 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
1733 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
1734 value |= SOR_CLK_CNTRL_DP_LINK_SPEED(rate);
1735 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001736
Thierry Reding01b9bea2015-11-11 17:15:29 +01001737 value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
1738 value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
1739 value |= SOR_DP_LINKCTL_LANE_COUNT(lanes);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001740
Thierry Reding01b9bea2015-11-11 17:15:29 +01001741 if (link.capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
1742 value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001743
Thierry Reding01b9bea2015-11-11 17:15:29 +01001744 tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001745
Thierry Reding01b9bea2015-11-11 17:15:29 +01001746 /* disable training pattern generator */
Thierry Reding6b6b6042013-11-15 16:06:05 +01001747
Thierry Reding01b9bea2015-11-11 17:15:29 +01001748 for (i = 0; i < link.num_lanes; i++) {
1749 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
1750 SOR_DP_TPG_SCRAMBLER_GALIOS |
1751 SOR_DP_TPG_PATTERN_NONE;
1752 value = (value << 8) | lane;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001753 }
1754
Thierry Reding01b9bea2015-11-11 17:15:29 +01001755 tegra_sor_writel(sor, value, SOR_DP_TPG);
1756
1757 err = tegra_sor_dp_train_fast(sor, &link);
1758 if (err < 0)
1759 dev_err(sor->dev, "DP fast link training failed: %d\n", err);
1760
1761 dev_dbg(sor->dev, "fast link training succeeded\n");
1762
Thierry Reding6b6b6042013-11-15 16:06:05 +01001763 err = tegra_sor_power_up(sor, 250);
Thierry Reding850bab42015-07-29 17:58:41 +02001764 if (err < 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001765 dev_err(sor->dev, "failed to power up SOR: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001766
Thierry Reding6b6b6042013-11-15 16:06:05 +01001767 /* CSTM (LVDS, link A/B, upper) */
Stéphane Marchesin143b1df2014-05-22 20:32:47 -07001768 value = SOR_CSTM_LVDS | SOR_CSTM_LINK_ACT_A | SOR_CSTM_LINK_ACT_B |
Thierry Reding6b6b6042013-11-15 16:06:05 +01001769 SOR_CSTM_UPPER;
1770 tegra_sor_writel(sor, value, SOR_CSTM);
1771
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001772 /* use DP-A protocol */
1773 value = tegra_sor_readl(sor, SOR_STATE1);
1774 value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
1775 value |= SOR_STATE_ASY_PROTOCOL_DP_A;
1776 tegra_sor_writel(sor, value, SOR_STATE1);
1777
Thierry Redingc31efa72015-09-08 16:09:22 +02001778 tegra_sor_mode_set(sor, mode, state);
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001779
Thierry Reding6b6b6042013-11-15 16:06:05 +01001780 /* PWM setup */
1781 err = tegra_sor_setup_pwm(sor, 250);
Thierry Reding850bab42015-07-29 17:58:41 +02001782 if (err < 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001783 dev_err(sor->dev, "failed to setup PWM: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001784
Thierry Reding666cb872014-12-08 16:32:47 +01001785 tegra_sor_update(sor);
1786
Thierry Reding6b6b6042013-11-15 16:06:05 +01001787 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
1788 value |= SOR_ENABLE;
1789 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
1790
Thierry Reding666cb872014-12-08 16:32:47 +01001791 tegra_dc_commit(dc);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001792
1793 err = tegra_sor_attach(sor);
Thierry Reding850bab42015-07-29 17:58:41 +02001794 if (err < 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001795 dev_err(sor->dev, "failed to attach SOR: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001796
1797 err = tegra_sor_wakeup(sor);
Thierry Reding850bab42015-07-29 17:58:41 +02001798 if (err < 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001799 dev_err(sor->dev, "failed to enable DC: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001800
Thierry Reding6fad8f62014-11-28 15:41:34 +01001801 if (output->panel)
1802 drm_panel_enable(output->panel);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001803}
1804
Thierry Reding82f15112014-12-08 17:26:46 +01001805static int
1806tegra_sor_encoder_atomic_check(struct drm_encoder *encoder,
1807 struct drm_crtc_state *crtc_state,
1808 struct drm_connector_state *conn_state)
1809{
1810 struct tegra_output *output = encoder_to_output(encoder);
Thierry Redingc31efa72015-09-08 16:09:22 +02001811 struct tegra_sor_state *state = to_sor_state(conn_state);
Thierry Reding82f15112014-12-08 17:26:46 +01001812 struct tegra_dc *dc = to_tegra_dc(conn_state->crtc);
1813 unsigned long pclk = crtc_state->mode.clock * 1000;
1814 struct tegra_sor *sor = to_sor(output);
Thierry Redingc31efa72015-09-08 16:09:22 +02001815 struct drm_display_info *info;
Thierry Reding82f15112014-12-08 17:26:46 +01001816 int err;
1817
Thierry Redingc31efa72015-09-08 16:09:22 +02001818 info = &output->connector.display_info;
1819
Thierry Reding82f15112014-12-08 17:26:46 +01001820 err = tegra_dc_state_setup_clock(dc, crtc_state, sor->clk_parent,
1821 pclk, 0);
1822 if (err < 0) {
1823 dev_err(output->dev, "failed to setup CRTC state: %d\n", err);
1824 return err;
1825 }
1826
Thierry Redingc31efa72015-09-08 16:09:22 +02001827 switch (info->bpc) {
1828 case 8:
1829 case 6:
1830 state->bpc = info->bpc;
1831 break;
1832
1833 default:
1834 DRM_DEBUG_KMS("%u bits-per-color not supported\n", info->bpc);
1835 state->bpc = 8;
1836 break;
1837 }
1838
Thierry Reding82f15112014-12-08 17:26:46 +01001839 return 0;
1840}
1841
Thierry Reding459cc2c2015-07-30 10:34:24 +02001842static const struct drm_encoder_helper_funcs tegra_sor_edp_helpers = {
Thierry Reding850bab42015-07-29 17:58:41 +02001843 .disable = tegra_sor_edp_disable,
1844 .enable = tegra_sor_edp_enable,
Thierry Reding82f15112014-12-08 17:26:46 +01001845 .atomic_check = tegra_sor_encoder_atomic_check,
Thierry Reding6b6b6042013-11-15 16:06:05 +01001846};
1847
Thierry Reding459cc2c2015-07-30 10:34:24 +02001848static inline u32 tegra_sor_hdmi_subpack(const u8 *ptr, size_t size)
1849{
1850 u32 value = 0;
1851 size_t i;
1852
1853 for (i = size; i > 0; i--)
1854 value = (value << 8) | ptr[i - 1];
1855
1856 return value;
1857}
1858
1859static void tegra_sor_hdmi_write_infopack(struct tegra_sor *sor,
1860 const void *data, size_t size)
1861{
1862 const u8 *ptr = data;
1863 unsigned long offset;
1864 size_t i, j;
1865 u32 value;
1866
1867 switch (ptr[0]) {
1868 case HDMI_INFOFRAME_TYPE_AVI:
1869 offset = SOR_HDMI_AVI_INFOFRAME_HEADER;
1870 break;
1871
1872 case HDMI_INFOFRAME_TYPE_AUDIO:
1873 offset = SOR_HDMI_AUDIO_INFOFRAME_HEADER;
1874 break;
1875
1876 case HDMI_INFOFRAME_TYPE_VENDOR:
1877 offset = SOR_HDMI_VSI_INFOFRAME_HEADER;
1878 break;
1879
1880 default:
1881 dev_err(sor->dev, "unsupported infoframe type: %02x\n",
1882 ptr[0]);
1883 return;
1884 }
1885
1886 value = INFOFRAME_HEADER_TYPE(ptr[0]) |
1887 INFOFRAME_HEADER_VERSION(ptr[1]) |
1888 INFOFRAME_HEADER_LEN(ptr[2]);
1889 tegra_sor_writel(sor, value, offset);
1890 offset++;
1891
1892 /*
1893 * Each subpack contains 7 bytes, divided into:
1894 * - subpack_low: bytes 0 - 3
1895 * - subpack_high: bytes 4 - 6 (with byte 7 padded to 0x00)
1896 */
1897 for (i = 3, j = 0; i < size; i += 7, j += 8) {
1898 size_t rem = size - i, num = min_t(size_t, rem, 4);
1899
1900 value = tegra_sor_hdmi_subpack(&ptr[i], num);
1901 tegra_sor_writel(sor, value, offset++);
1902
1903 num = min_t(size_t, rem - num, 3);
1904
1905 value = tegra_sor_hdmi_subpack(&ptr[i + 4], num);
1906 tegra_sor_writel(sor, value, offset++);
1907 }
1908}
1909
1910static int
1911tegra_sor_hdmi_setup_avi_infoframe(struct tegra_sor *sor,
1912 const struct drm_display_mode *mode)
1913{
1914 u8 buffer[HDMI_INFOFRAME_SIZE(AVI)];
1915 struct hdmi_avi_infoframe frame;
1916 u32 value;
1917 int err;
1918
1919 /* disable AVI infoframe */
1920 value = tegra_sor_readl(sor, SOR_HDMI_AVI_INFOFRAME_CTRL);
1921 value &= ~INFOFRAME_CTRL_SINGLE;
1922 value &= ~INFOFRAME_CTRL_OTHER;
1923 value &= ~INFOFRAME_CTRL_ENABLE;
1924 tegra_sor_writel(sor, value, SOR_HDMI_AVI_INFOFRAME_CTRL);
1925
Shashank Sharma0c1f5282017-07-13 21:03:07 +05301926 err = drm_hdmi_avi_infoframe_from_display_mode(&frame, mode, false);
Thierry Reding459cc2c2015-07-30 10:34:24 +02001927 if (err < 0) {
1928 dev_err(sor->dev, "failed to setup AVI infoframe: %d\n", err);
1929 return err;
1930 }
1931
1932 err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
1933 if (err < 0) {
1934 dev_err(sor->dev, "failed to pack AVI infoframe: %d\n", err);
1935 return err;
1936 }
1937
1938 tegra_sor_hdmi_write_infopack(sor, buffer, err);
1939
1940 /* enable AVI infoframe */
1941 value = tegra_sor_readl(sor, SOR_HDMI_AVI_INFOFRAME_CTRL);
1942 value |= INFOFRAME_CTRL_CHECKSUM_ENABLE;
1943 value |= INFOFRAME_CTRL_ENABLE;
1944 tegra_sor_writel(sor, value, SOR_HDMI_AVI_INFOFRAME_CTRL);
1945
1946 return 0;
1947}
1948
1949static void tegra_sor_hdmi_disable_audio_infoframe(struct tegra_sor *sor)
1950{
1951 u32 value;
1952
1953 value = tegra_sor_readl(sor, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
1954 value &= ~INFOFRAME_CTRL_ENABLE;
1955 tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
1956}
1957
1958static struct tegra_sor_hdmi_settings *
1959tegra_sor_hdmi_find_settings(struct tegra_sor *sor, unsigned long frequency)
1960{
1961 unsigned int i;
1962
1963 for (i = 0; i < sor->num_settings; i++)
1964 if (frequency <= sor->settings[i].frequency)
1965 return &sor->settings[i];
1966
1967 return NULL;
1968}
1969
1970static void tegra_sor_hdmi_disable(struct drm_encoder *encoder)
1971{
1972 struct tegra_output *output = encoder_to_output(encoder);
1973 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
1974 struct tegra_sor *sor = to_sor(output);
1975 u32 value;
1976 int err;
1977
1978 err = tegra_sor_detach(sor);
1979 if (err < 0)
1980 dev_err(sor->dev, "failed to detach SOR: %d\n", err);
1981
1982 tegra_sor_writel(sor, 0, SOR_STATE1);
1983 tegra_sor_update(sor);
1984
1985 /* disable display to SOR clock */
1986 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
1987 value &= ~SOR1_TIMING_CYA;
1988 value &= ~SOR1_ENABLE;
1989 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
1990
1991 tegra_dc_commit(dc);
1992
1993 err = tegra_sor_power_down(sor);
1994 if (err < 0)
1995 dev_err(sor->dev, "failed to power down SOR: %d\n", err);
1996
1997 err = tegra_io_rail_power_off(TEGRA_IO_RAIL_HDMI);
1998 if (err < 0)
1999 dev_err(sor->dev, "failed to power off HDMI rail: %d\n", err);
2000
Thierry Redingaaff8bd2015-08-07 16:04:54 +02002001 pm_runtime_put(sor->dev);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002002}
2003
2004static void tegra_sor_hdmi_enable(struct drm_encoder *encoder)
2005{
2006 struct tegra_output *output = encoder_to_output(encoder);
2007 unsigned int h_ref_to_sync = 1, pulse_start, max_ac;
2008 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002009 struct tegra_sor_hdmi_settings *settings;
2010 struct tegra_sor *sor = to_sor(output);
Thierry Redingc31efa72015-09-08 16:09:22 +02002011 struct tegra_sor_state *state;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002012 struct drm_display_mode *mode;
Thierry Reding30b49432015-08-03 15:50:32 +02002013 unsigned int div, i;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002014 u32 value;
2015 int err;
2016
Thierry Redingc31efa72015-09-08 16:09:22 +02002017 state = to_sor_state(output->connector.state);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002018 mode = &encoder->crtc->state->adjusted_mode;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002019
Thierry Redingaaff8bd2015-08-07 16:04:54 +02002020 pm_runtime_get_sync(sor->dev);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002021
Thierry Reding25bb2ce2015-08-03 14:23:29 +02002022 /* switch to safe parent clock */
2023 err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
Thierry Redinge1335e22017-10-12 17:53:11 +02002024 if (err < 0) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002025 dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
Thierry Redinge1335e22017-10-12 17:53:11 +02002026 return;
2027 }
Thierry Reding459cc2c2015-07-30 10:34:24 +02002028
2029 div = clk_get_rate(sor->clk) / 1000000 * 4;
2030
2031 err = tegra_io_rail_power_on(TEGRA_IO_RAIL_HDMI);
2032 if (err < 0)
2033 dev_err(sor->dev, "failed to power on HDMI rail: %d\n", err);
2034
2035 usleep_range(20, 100);
2036
Thierry Reding880cee02017-10-12 19:04:17 +02002037 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002038 value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
Thierry Reding880cee02017-10-12 19:04:17 +02002039 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002040
2041 usleep_range(20, 100);
2042
Thierry Reding880cee02017-10-12 19:04:17 +02002043 value = tegra_sor_readl(sor, sor->soc->regs->pll3);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002044 value &= ~SOR_PLL3_PLL_VDD_MODE_3V3;
Thierry Reding880cee02017-10-12 19:04:17 +02002045 tegra_sor_writel(sor, value, sor->soc->regs->pll3);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002046
Thierry Reding880cee02017-10-12 19:04:17 +02002047 value = tegra_sor_readl(sor, sor->soc->regs->pll0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002048 value &= ~SOR_PLL0_VCOPD;
2049 value &= ~SOR_PLL0_PWR;
Thierry Reding880cee02017-10-12 19:04:17 +02002050 tegra_sor_writel(sor, value, sor->soc->regs->pll0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002051
Thierry Reding880cee02017-10-12 19:04:17 +02002052 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002053 value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
Thierry Reding880cee02017-10-12 19:04:17 +02002054 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002055
2056 usleep_range(200, 400);
2057
Thierry Reding880cee02017-10-12 19:04:17 +02002058 value = tegra_sor_readl(sor, sor->soc->regs->pll2);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002059 value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
2060 value &= ~SOR_PLL2_PORT_POWERDOWN;
Thierry Reding880cee02017-10-12 19:04:17 +02002061 tegra_sor_writel(sor, value, sor->soc->regs->pll2);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002062
2063 usleep_range(20, 100);
2064
Thierry Reding880cee02017-10-12 19:04:17 +02002065 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002066 value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
2067 SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2;
Thierry Reding880cee02017-10-12 19:04:17 +02002068 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002069
2070 while (true) {
2071 value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
2072 if ((value & SOR_LANE_SEQ_CTL_STATE_BUSY) == 0)
2073 break;
2074
2075 usleep_range(250, 1000);
2076 }
2077
2078 value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
2079 SOR_LANE_SEQ_CTL_POWER_STATE_UP | SOR_LANE_SEQ_CTL_DELAY(5);
2080 tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
2081
2082 while (true) {
2083 value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
2084 if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
2085 break;
2086
2087 usleep_range(250, 1000);
2088 }
2089
2090 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
2091 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
2092 value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
2093
2094 if (mode->clock < 340000)
2095 value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G2_70;
2096 else
2097 value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G5_40;
2098
2099 value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK;
2100 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
2101
2102 value = tegra_sor_readl(sor, SOR_DP_SPARE0);
2103 value |= SOR_DP_SPARE_DISP_VIDEO_PREAMBLE;
2104 value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
2105 value |= SOR_DP_SPARE_SEQ_ENABLE;
2106 tegra_sor_writel(sor, value, SOR_DP_SPARE0);
2107
2108 value = SOR_SEQ_CTL_PU_PC(0) | SOR_SEQ_CTL_PU_PC_ALT(0) |
2109 SOR_SEQ_CTL_PD_PC(8) | SOR_SEQ_CTL_PD_PC_ALT(8);
2110 tegra_sor_writel(sor, value, SOR_SEQ_CTL);
2111
2112 value = SOR_SEQ_INST_DRIVE_PWM_OUT_LO | SOR_SEQ_INST_HALT |
2113 SOR_SEQ_INST_WAIT_VSYNC | SOR_SEQ_INST_WAIT(1);
2114 tegra_sor_writel(sor, value, SOR_SEQ_INST(0));
2115 tegra_sor_writel(sor, value, SOR_SEQ_INST(8));
2116
2117 /* program the reference clock */
2118 value = SOR_REFCLK_DIV_INT(div) | SOR_REFCLK_DIV_FRAC(div);
2119 tegra_sor_writel(sor, value, SOR_REFCLK);
2120
Thierry Reding30b49432015-08-03 15:50:32 +02002121 /* XXX not in TRM */
2122 for (value = 0, i = 0; i < 5; i++)
2123 value |= SOR_XBAR_CTRL_LINK0_XSEL(i, sor->soc->xbar_cfg[i]) |
2124 SOR_XBAR_CTRL_LINK1_XSEL(i, i);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002125
2126 tegra_sor_writel(sor, 0x00000000, SOR_XBAR_POL);
Thierry Reding30b49432015-08-03 15:50:32 +02002127 tegra_sor_writel(sor, value, SOR_XBAR_CTRL);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002128
Thierry Reding25bb2ce2015-08-03 14:23:29 +02002129 /* switch to parent clock */
Thierry Redinge1335e22017-10-12 17:53:11 +02002130 err = clk_set_parent(sor->clk, sor->clk_parent);
2131 if (err < 0) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002132 dev_err(sor->dev, "failed to set parent clock: %d\n", err);
Thierry Redinge1335e22017-10-12 17:53:11 +02002133 return;
2134 }
2135
2136 err = tegra_sor_set_parent_clock(sor, sor->clk_pad);
2137 if (err < 0) {
2138 dev_err(sor->dev, "failed to set pad clock: %d\n", err);
2139 return;
2140 }
Thierry Reding459cc2c2015-07-30 10:34:24 +02002141
2142 value = SOR_INPUT_CONTROL_HDMI_SRC_SELECT(dc->pipe);
2143
2144 /* XXX is this the proper check? */
2145 if (mode->clock < 75000)
2146 value |= SOR_INPUT_CONTROL_ARM_VIDEO_RANGE_LIMITED;
2147
2148 tegra_sor_writel(sor, value, SOR_INPUT_CONTROL);
2149
2150 max_ac = ((mode->htotal - mode->hdisplay) - SOR_REKEY - 18) / 32;
2151
2152 value = SOR_HDMI_CTRL_ENABLE | SOR_HDMI_CTRL_MAX_AC_PACKET(max_ac) |
2153 SOR_HDMI_CTRL_AUDIO_LAYOUT | SOR_HDMI_CTRL_REKEY(SOR_REKEY);
2154 tegra_sor_writel(sor, value, SOR_HDMI_CTRL);
2155
2156 /* H_PULSE2 setup */
2157 pulse_start = h_ref_to_sync + (mode->hsync_end - mode->hsync_start) +
2158 (mode->htotal - mode->hsync_end) - 10;
2159
2160 value = PULSE_LAST_END_A | PULSE_QUAL_VACTIVE |
2161 PULSE_POLARITY_HIGH | PULSE_MODE_NORMAL;
2162 tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_CONTROL);
2163
2164 value = PULSE_END(pulse_start + 8) | PULSE_START(pulse_start);
2165 tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_POSITION_A);
2166
2167 value = tegra_dc_readl(dc, DC_DISP_DISP_SIGNAL_OPTIONS0);
2168 value |= H_PULSE2_ENABLE;
2169 tegra_dc_writel(dc, value, DC_DISP_DISP_SIGNAL_OPTIONS0);
2170
2171 /* infoframe setup */
2172 err = tegra_sor_hdmi_setup_avi_infoframe(sor, mode);
2173 if (err < 0)
2174 dev_err(sor->dev, "failed to setup AVI infoframe: %d\n", err);
2175
2176 /* XXX HDMI audio support not implemented yet */
2177 tegra_sor_hdmi_disable_audio_infoframe(sor);
2178
2179 /* use single TMDS protocol */
2180 value = tegra_sor_readl(sor, SOR_STATE1);
2181 value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
2182 value |= SOR_STATE_ASY_PROTOCOL_SINGLE_TMDS_A;
2183 tegra_sor_writel(sor, value, SOR_STATE1);
2184
2185 /* power up pad calibration */
Thierry Reding880cee02017-10-12 19:04:17 +02002186 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002187 value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
Thierry Reding880cee02017-10-12 19:04:17 +02002188 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002189
2190 /* production settings */
2191 settings = tegra_sor_hdmi_find_settings(sor, mode->clock * 1000);
Dan Carpenterdb8b42f2015-08-17 17:37:03 +03002192 if (!settings) {
2193 dev_err(sor->dev, "no settings for pixel clock %d Hz\n",
2194 mode->clock * 1000);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002195 return;
2196 }
2197
Thierry Reding880cee02017-10-12 19:04:17 +02002198 value = tegra_sor_readl(sor, sor->soc->regs->pll0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002199 value &= ~SOR_PLL0_ICHPMP_MASK;
2200 value &= ~SOR_PLL0_VCOCAP_MASK;
2201 value |= SOR_PLL0_ICHPMP(settings->ichpmp);
2202 value |= SOR_PLL0_VCOCAP(settings->vcocap);
Thierry Reding880cee02017-10-12 19:04:17 +02002203 tegra_sor_writel(sor, value, sor->soc->regs->pll0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002204
2205 tegra_sor_dp_term_calibrate(sor);
2206
Thierry Reding880cee02017-10-12 19:04:17 +02002207 value = tegra_sor_readl(sor, sor->soc->regs->pll1);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002208 value &= ~SOR_PLL1_LOADADJ_MASK;
2209 value |= SOR_PLL1_LOADADJ(settings->loadadj);
Thierry Reding880cee02017-10-12 19:04:17 +02002210 tegra_sor_writel(sor, value, sor->soc->regs->pll1);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002211
Thierry Reding880cee02017-10-12 19:04:17 +02002212 value = tegra_sor_readl(sor, sor->soc->regs->pll3);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002213 value &= ~SOR_PLL3_BG_VREF_LEVEL_MASK;
2214 value |= SOR_PLL3_BG_VREF_LEVEL(settings->bg_vref);
Thierry Reding880cee02017-10-12 19:04:17 +02002215 tegra_sor_writel(sor, value, sor->soc->regs->pll3);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002216
2217 value = settings->drive_current[0] << 24 |
2218 settings->drive_current[1] << 16 |
2219 settings->drive_current[2] << 8 |
2220 settings->drive_current[3] << 0;
2221 tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT0);
2222
2223 value = settings->preemphasis[0] << 24 |
2224 settings->preemphasis[1] << 16 |
2225 settings->preemphasis[2] << 8 |
2226 settings->preemphasis[3] << 0;
2227 tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS0);
2228
Thierry Reding880cee02017-10-12 19:04:17 +02002229 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002230 value &= ~SOR_DP_PADCTL_TX_PU_MASK;
2231 value |= SOR_DP_PADCTL_TX_PU_ENABLE;
2232 value |= SOR_DP_PADCTL_TX_PU(settings->tx_pu);
Thierry Reding880cee02017-10-12 19:04:17 +02002233 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002234
2235 /* power down pad calibration */
Thierry Reding880cee02017-10-12 19:04:17 +02002236 value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002237 value |= SOR_DP_PADCTL_PAD_CAL_PD;
Thierry Reding880cee02017-10-12 19:04:17 +02002238 tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002239
2240 /* miscellaneous display controller settings */
2241 value = VSYNC_H_POSITION(1);
2242 tegra_dc_writel(dc, value, DC_DISP_DISP_TIMING_OPTIONS);
2243
2244 value = tegra_dc_readl(dc, DC_DISP_DISP_COLOR_CONTROL);
2245 value &= ~DITHER_CONTROL_MASK;
2246 value &= ~BASE_COLOR_SIZE_MASK;
2247
Thierry Redingc31efa72015-09-08 16:09:22 +02002248 switch (state->bpc) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002249 case 6:
2250 value |= BASE_COLOR_SIZE_666;
2251 break;
2252
2253 case 8:
2254 value |= BASE_COLOR_SIZE_888;
2255 break;
2256
2257 default:
Thierry Redingc31efa72015-09-08 16:09:22 +02002258 WARN(1, "%u bits-per-color not supported\n", state->bpc);
2259 value |= BASE_COLOR_SIZE_888;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002260 break;
2261 }
2262
2263 tegra_dc_writel(dc, value, DC_DISP_DISP_COLOR_CONTROL);
2264
2265 err = tegra_sor_power_up(sor, 250);
2266 if (err < 0)
2267 dev_err(sor->dev, "failed to power up SOR: %d\n", err);
2268
Thierry Reding2bd1dd32015-08-03 15:46:15 +02002269 /* configure dynamic range of output */
Thierry Reding880cee02017-10-12 19:04:17 +02002270 value = tegra_sor_readl(sor, sor->soc->regs->head_state0 + dc->pipe);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002271 value &= ~SOR_HEAD_STATE_RANGECOMPRESS_MASK;
2272 value &= ~SOR_HEAD_STATE_DYNRANGE_MASK;
Thierry Reding880cee02017-10-12 19:04:17 +02002273 tegra_sor_writel(sor, value, sor->soc->regs->head_state0 + dc->pipe);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002274
Thierry Reding2bd1dd32015-08-03 15:46:15 +02002275 /* configure colorspace */
Thierry Reding880cee02017-10-12 19:04:17 +02002276 value = tegra_sor_readl(sor, sor->soc->regs->head_state0 + dc->pipe);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002277 value &= ~SOR_HEAD_STATE_COLORSPACE_MASK;
2278 value |= SOR_HEAD_STATE_COLORSPACE_RGB;
Thierry Reding880cee02017-10-12 19:04:17 +02002279 tegra_sor_writel(sor, value, sor->soc->regs->head_state0 + dc->pipe);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002280
Thierry Redingc31efa72015-09-08 16:09:22 +02002281 tegra_sor_mode_set(sor, mode, state);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002282
2283 tegra_sor_update(sor);
2284
2285 err = tegra_sor_attach(sor);
2286 if (err < 0)
2287 dev_err(sor->dev, "failed to attach SOR: %d\n", err);
2288
2289 /* enable display to SOR clock and generate HDMI preamble */
2290 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
2291 value |= SOR1_ENABLE | SOR1_TIMING_CYA;
2292 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
2293
2294 tegra_dc_commit(dc);
2295
2296 err = tegra_sor_wakeup(sor);
2297 if (err < 0)
2298 dev_err(sor->dev, "failed to wakeup SOR: %d\n", err);
2299}
2300
2301static const struct drm_encoder_helper_funcs tegra_sor_hdmi_helpers = {
2302 .disable = tegra_sor_hdmi_disable,
2303 .enable = tegra_sor_hdmi_enable,
2304 .atomic_check = tegra_sor_encoder_atomic_check,
2305};
2306
Thierry Reding6b6b6042013-11-15 16:06:05 +01002307static int tegra_sor_init(struct host1x_client *client)
2308{
Thierry Reding9910f5c2014-05-22 09:57:15 +02002309 struct drm_device *drm = dev_get_drvdata(client->parent);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002310 const struct drm_encoder_helper_funcs *helpers = NULL;
Thierry Reding6b6b6042013-11-15 16:06:05 +01002311 struct tegra_sor *sor = host1x_client_to_sor(client);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002312 int connector = DRM_MODE_CONNECTOR_Unknown;
2313 int encoder = DRM_MODE_ENCODER_NONE;
Thierry Reding6b6b6042013-11-15 16:06:05 +01002314 int err;
2315
Thierry Reding9542c232015-07-08 13:39:09 +02002316 if (!sor->aux) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002317 if (sor->soc->supports_hdmi) {
2318 connector = DRM_MODE_CONNECTOR_HDMIA;
2319 encoder = DRM_MODE_ENCODER_TMDS;
2320 helpers = &tegra_sor_hdmi_helpers;
2321 } else if (sor->soc->supports_lvds) {
2322 connector = DRM_MODE_CONNECTOR_LVDS;
2323 encoder = DRM_MODE_ENCODER_LVDS;
2324 }
2325 } else {
2326 if (sor->soc->supports_edp) {
2327 connector = DRM_MODE_CONNECTOR_eDP;
2328 encoder = DRM_MODE_ENCODER_TMDS;
2329 helpers = &tegra_sor_edp_helpers;
2330 } else if (sor->soc->supports_dp) {
2331 connector = DRM_MODE_CONNECTOR_DisplayPort;
2332 encoder = DRM_MODE_ENCODER_TMDS;
2333 }
2334 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01002335
Thierry Reding6b6b6042013-11-15 16:06:05 +01002336 sor->output.dev = sor->dev;
Thierry Reding6b6b6042013-11-15 16:06:05 +01002337
Thierry Reding6fad8f62014-11-28 15:41:34 +01002338 drm_connector_init(drm, &sor->output.connector,
2339 &tegra_sor_connector_funcs,
Thierry Reding459cc2c2015-07-30 10:34:24 +02002340 connector);
Thierry Reding6fad8f62014-11-28 15:41:34 +01002341 drm_connector_helper_add(&sor->output.connector,
2342 &tegra_sor_connector_helper_funcs);
2343 sor->output.connector.dpms = DRM_MODE_DPMS_OFF;
2344
Thierry Reding6fad8f62014-11-28 15:41:34 +01002345 drm_encoder_init(drm, &sor->output.encoder, &tegra_sor_encoder_funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +02002346 encoder, NULL);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002347 drm_encoder_helper_add(&sor->output.encoder, helpers);
Thierry Reding6fad8f62014-11-28 15:41:34 +01002348
2349 drm_mode_connector_attach_encoder(&sor->output.connector,
2350 &sor->output.encoder);
2351 drm_connector_register(&sor->output.connector);
2352
Thierry Redingea130b22014-12-19 15:51:35 +01002353 err = tegra_output_init(drm, &sor->output);
2354 if (err < 0) {
2355 dev_err(client->dev, "failed to initialize output: %d\n", err);
2356 return err;
2357 }
Thierry Reding6fad8f62014-11-28 15:41:34 +01002358
Thierry Redingea130b22014-12-19 15:51:35 +01002359 sor->output.encoder.possible_crtcs = 0x3;
Thierry Reding6b6b6042013-11-15 16:06:05 +01002360
Thierry Reding9542c232015-07-08 13:39:09 +02002361 if (sor->aux) {
2362 err = drm_dp_aux_attach(sor->aux, &sor->output);
Thierry Reding6b6b6042013-11-15 16:06:05 +01002363 if (err < 0) {
2364 dev_err(sor->dev, "failed to attach DP: %d\n", err);
2365 return err;
2366 }
2367 }
2368
Tomeu Vizoso535a65d2015-03-30 10:33:03 +02002369 /*
2370 * XXX: Remove this reset once proper hand-over from firmware to
2371 * kernel is possible.
2372 */
Jon Hunterf8c79122016-07-01 14:21:38 +01002373 if (sor->rst) {
2374 err = reset_control_assert(sor->rst);
2375 if (err < 0) {
2376 dev_err(sor->dev, "failed to assert SOR reset: %d\n",
2377 err);
2378 return err;
2379 }
Tomeu Vizoso535a65d2015-03-30 10:33:03 +02002380 }
2381
Thierry Reding6fad8f62014-11-28 15:41:34 +01002382 err = clk_prepare_enable(sor->clk);
2383 if (err < 0) {
2384 dev_err(sor->dev, "failed to enable clock: %d\n", err);
2385 return err;
2386 }
2387
Tomeu Vizoso535a65d2015-03-30 10:33:03 +02002388 usleep_range(1000, 3000);
2389
Jon Hunterf8c79122016-07-01 14:21:38 +01002390 if (sor->rst) {
2391 err = reset_control_deassert(sor->rst);
2392 if (err < 0) {
2393 dev_err(sor->dev, "failed to deassert SOR reset: %d\n",
2394 err);
2395 return err;
2396 }
Tomeu Vizoso535a65d2015-03-30 10:33:03 +02002397 }
2398
Thierry Reding6fad8f62014-11-28 15:41:34 +01002399 err = clk_prepare_enable(sor->clk_safe);
2400 if (err < 0)
2401 return err;
2402
2403 err = clk_prepare_enable(sor->clk_dp);
2404 if (err < 0)
2405 return err;
2406
Thierry Reding6b6b6042013-11-15 16:06:05 +01002407 return 0;
2408}
2409
2410static int tegra_sor_exit(struct host1x_client *client)
2411{
2412 struct tegra_sor *sor = host1x_client_to_sor(client);
2413 int err;
2414
Thierry Reding328ec692014-12-19 15:55:08 +01002415 tegra_output_exit(&sor->output);
2416
Thierry Reding9542c232015-07-08 13:39:09 +02002417 if (sor->aux) {
2418 err = drm_dp_aux_detach(sor->aux);
Thierry Reding6b6b6042013-11-15 16:06:05 +01002419 if (err < 0) {
2420 dev_err(sor->dev, "failed to detach DP: %d\n", err);
2421 return err;
2422 }
2423 }
2424
Thierry Reding6fad8f62014-11-28 15:41:34 +01002425 clk_disable_unprepare(sor->clk_safe);
2426 clk_disable_unprepare(sor->clk_dp);
2427 clk_disable_unprepare(sor->clk);
2428
Thierry Reding6b6b6042013-11-15 16:06:05 +01002429 return 0;
2430}
2431
2432static const struct host1x_client_ops sor_client_ops = {
2433 .init = tegra_sor_init,
2434 .exit = tegra_sor_exit,
2435};
2436
Thierry Reding459cc2c2015-07-30 10:34:24 +02002437static const struct tegra_sor_ops tegra_sor_edp_ops = {
2438 .name = "eDP",
2439};
2440
2441static int tegra_sor_hdmi_probe(struct tegra_sor *sor)
2442{
2443 int err;
2444
2445 sor->avdd_io_supply = devm_regulator_get(sor->dev, "avdd-io");
2446 if (IS_ERR(sor->avdd_io_supply)) {
2447 dev_err(sor->dev, "cannot get AVDD I/O supply: %ld\n",
2448 PTR_ERR(sor->avdd_io_supply));
2449 return PTR_ERR(sor->avdd_io_supply);
2450 }
2451
2452 err = regulator_enable(sor->avdd_io_supply);
2453 if (err < 0) {
2454 dev_err(sor->dev, "failed to enable AVDD I/O supply: %d\n",
2455 err);
2456 return err;
2457 }
2458
2459 sor->vdd_pll_supply = devm_regulator_get(sor->dev, "vdd-pll");
2460 if (IS_ERR(sor->vdd_pll_supply)) {
2461 dev_err(sor->dev, "cannot get VDD PLL supply: %ld\n",
2462 PTR_ERR(sor->vdd_pll_supply));
2463 return PTR_ERR(sor->vdd_pll_supply);
2464 }
2465
2466 err = regulator_enable(sor->vdd_pll_supply);
2467 if (err < 0) {
2468 dev_err(sor->dev, "failed to enable VDD PLL supply: %d\n",
2469 err);
2470 return err;
2471 }
2472
2473 sor->hdmi_supply = devm_regulator_get(sor->dev, "hdmi");
2474 if (IS_ERR(sor->hdmi_supply)) {
2475 dev_err(sor->dev, "cannot get HDMI supply: %ld\n",
2476 PTR_ERR(sor->hdmi_supply));
2477 return PTR_ERR(sor->hdmi_supply);
2478 }
2479
2480 err = regulator_enable(sor->hdmi_supply);
2481 if (err < 0) {
2482 dev_err(sor->dev, "failed to enable HDMI supply: %d\n", err);
2483 return err;
2484 }
2485
2486 return 0;
2487}
2488
2489static int tegra_sor_hdmi_remove(struct tegra_sor *sor)
2490{
2491 regulator_disable(sor->hdmi_supply);
2492 regulator_disable(sor->vdd_pll_supply);
2493 regulator_disable(sor->avdd_io_supply);
2494
2495 return 0;
2496}
2497
2498static const struct tegra_sor_ops tegra_sor_hdmi_ops = {
2499 .name = "HDMI",
2500 .probe = tegra_sor_hdmi_probe,
2501 .remove = tegra_sor_hdmi_remove,
2502};
2503
Thierry Reding30b49432015-08-03 15:50:32 +02002504static const u8 tegra124_sor_xbar_cfg[5] = {
2505 0, 1, 2, 3, 4
2506};
2507
Thierry Reding880cee02017-10-12 19:04:17 +02002508static const struct tegra_sor_regs tegra124_sor_regs = {
2509 .head_state0 = 0x05,
2510 .head_state1 = 0x07,
2511 .head_state2 = 0x09,
2512 .head_state3 = 0x0b,
2513 .head_state4 = 0x0d,
2514 .head_state5 = 0x0f,
2515 .pll0 = 0x17,
2516 .pll1 = 0x18,
2517 .pll2 = 0x19,
2518 .pll3 = 0x1a,
2519 .dp_padctl0 = 0x5c,
2520 .dp_padctl2 = 0x73,
2521};
2522
Thierry Reding459cc2c2015-07-30 10:34:24 +02002523static const struct tegra_sor_soc tegra124_sor = {
2524 .supports_edp = true,
2525 .supports_lvds = true,
2526 .supports_hdmi = false,
2527 .supports_dp = false,
Thierry Reding880cee02017-10-12 19:04:17 +02002528 .regs = &tegra124_sor_regs,
Thierry Reding30b49432015-08-03 15:50:32 +02002529 .xbar_cfg = tegra124_sor_xbar_cfg,
Thierry Reding459cc2c2015-07-30 10:34:24 +02002530};
2531
Thierry Reding880cee02017-10-12 19:04:17 +02002532static const struct tegra_sor_regs tegra210_sor_regs = {
2533 .head_state0 = 0x05,
2534 .head_state1 = 0x07,
2535 .head_state2 = 0x09,
2536 .head_state3 = 0x0b,
2537 .head_state4 = 0x0d,
2538 .head_state5 = 0x0f,
2539 .pll0 = 0x17,
2540 .pll1 = 0x18,
2541 .pll2 = 0x19,
2542 .pll3 = 0x1a,
2543 .dp_padctl0 = 0x5c,
2544 .dp_padctl2 = 0x73,
2545};
2546
Thierry Reding459cc2c2015-07-30 10:34:24 +02002547static const struct tegra_sor_soc tegra210_sor = {
2548 .supports_edp = true,
2549 .supports_lvds = false,
2550 .supports_hdmi = false,
2551 .supports_dp = false,
Thierry Reding880cee02017-10-12 19:04:17 +02002552 .regs = &tegra210_sor_regs,
Thierry Reding30b49432015-08-03 15:50:32 +02002553 .xbar_cfg = tegra124_sor_xbar_cfg,
2554};
2555
2556static const u8 tegra210_sor_xbar_cfg[5] = {
2557 2, 1, 0, 3, 4
Thierry Reding459cc2c2015-07-30 10:34:24 +02002558};
2559
2560static const struct tegra_sor_soc tegra210_sor1 = {
2561 .supports_edp = false,
2562 .supports_lvds = false,
2563 .supports_hdmi = true,
2564 .supports_dp = true,
2565
Thierry Reding880cee02017-10-12 19:04:17 +02002566 .regs = &tegra210_sor_regs,
2567
Thierry Reding459cc2c2015-07-30 10:34:24 +02002568 .num_settings = ARRAY_SIZE(tegra210_sor_hdmi_defaults),
2569 .settings = tegra210_sor_hdmi_defaults,
Thierry Reding30b49432015-08-03 15:50:32 +02002570
2571 .xbar_cfg = tegra210_sor_xbar_cfg,
Thierry Reding459cc2c2015-07-30 10:34:24 +02002572};
2573
2574static const struct of_device_id tegra_sor_of_match[] = {
2575 { .compatible = "nvidia,tegra210-sor1", .data = &tegra210_sor1 },
2576 { .compatible = "nvidia,tegra210-sor", .data = &tegra210_sor },
2577 { .compatible = "nvidia,tegra124-sor", .data = &tegra124_sor },
2578 { },
2579};
2580MODULE_DEVICE_TABLE(of, tegra_sor_of_match);
2581
Thierry Reding6b6b6042013-11-15 16:06:05 +01002582static int tegra_sor_probe(struct platform_device *pdev)
2583{
2584 struct device_node *np;
2585 struct tegra_sor *sor;
2586 struct resource *regs;
2587 int err;
2588
2589 sor = devm_kzalloc(&pdev->dev, sizeof(*sor), GFP_KERNEL);
2590 if (!sor)
2591 return -ENOMEM;
2592
Thierry Reding5faea3d2017-08-21 17:33:14 +02002593 sor->soc = of_device_get_match_data(&pdev->dev);
Thierry Reding6b6b6042013-11-15 16:06:05 +01002594 sor->output.dev = sor->dev = &pdev->dev;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002595
2596 sor->settings = devm_kmemdup(&pdev->dev, sor->soc->settings,
2597 sor->soc->num_settings *
2598 sizeof(*sor->settings),
2599 GFP_KERNEL);
2600 if (!sor->settings)
2601 return -ENOMEM;
2602
2603 sor->num_settings = sor->soc->num_settings;
Thierry Reding6b6b6042013-11-15 16:06:05 +01002604
2605 np = of_parse_phandle(pdev->dev.of_node, "nvidia,dpaux", 0);
2606 if (np) {
Thierry Reding9542c232015-07-08 13:39:09 +02002607 sor->aux = drm_dp_aux_find_by_of_node(np);
Thierry Reding6b6b6042013-11-15 16:06:05 +01002608 of_node_put(np);
2609
Thierry Reding9542c232015-07-08 13:39:09 +02002610 if (!sor->aux)
Thierry Reding6b6b6042013-11-15 16:06:05 +01002611 return -EPROBE_DEFER;
2612 }
2613
Thierry Reding9542c232015-07-08 13:39:09 +02002614 if (!sor->aux) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002615 if (sor->soc->supports_hdmi) {
2616 sor->ops = &tegra_sor_hdmi_ops;
2617 } else if (sor->soc->supports_lvds) {
2618 dev_err(&pdev->dev, "LVDS not supported yet\n");
2619 return -ENODEV;
2620 } else {
2621 dev_err(&pdev->dev, "unknown (non-DP) support\n");
2622 return -ENODEV;
2623 }
2624 } else {
2625 if (sor->soc->supports_edp) {
2626 sor->ops = &tegra_sor_edp_ops;
2627 } else if (sor->soc->supports_dp) {
2628 dev_err(&pdev->dev, "DisplayPort not supported yet\n");
2629 return -ENODEV;
2630 } else {
2631 dev_err(&pdev->dev, "unknown (DP) support\n");
2632 return -ENODEV;
2633 }
2634 }
2635
Thierry Reding6b6b6042013-11-15 16:06:05 +01002636 err = tegra_output_probe(&sor->output);
Thierry Reding4dbdc742015-04-27 15:04:26 +02002637 if (err < 0) {
2638 dev_err(&pdev->dev, "failed to probe output: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01002639 return err;
Thierry Reding4dbdc742015-04-27 15:04:26 +02002640 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01002641
Thierry Reding459cc2c2015-07-30 10:34:24 +02002642 if (sor->ops && sor->ops->probe) {
2643 err = sor->ops->probe(sor);
2644 if (err < 0) {
2645 dev_err(&pdev->dev, "failed to probe %s: %d\n",
2646 sor->ops->name, err);
2647 goto output;
2648 }
2649 }
2650
Thierry Reding6b6b6042013-11-15 16:06:05 +01002651 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2652 sor->regs = devm_ioremap_resource(&pdev->dev, regs);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002653 if (IS_ERR(sor->regs)) {
2654 err = PTR_ERR(sor->regs);
2655 goto remove;
2656 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01002657
Jon Hunterf8c79122016-07-01 14:21:38 +01002658 if (!pdev->dev.pm_domain) {
2659 sor->rst = devm_reset_control_get(&pdev->dev, "sor");
2660 if (IS_ERR(sor->rst)) {
2661 err = PTR_ERR(sor->rst);
2662 dev_err(&pdev->dev, "failed to get reset control: %d\n",
2663 err);
2664 goto remove;
2665 }
Thierry Reding4dbdc742015-04-27 15:04:26 +02002666 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01002667
2668 sor->clk = devm_clk_get(&pdev->dev, NULL);
Thierry Reding4dbdc742015-04-27 15:04:26 +02002669 if (IS_ERR(sor->clk)) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002670 err = PTR_ERR(sor->clk);
2671 dev_err(&pdev->dev, "failed to get module clock: %d\n", err);
2672 goto remove;
Thierry Reding4dbdc742015-04-27 15:04:26 +02002673 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01002674
Thierry Reding618dee32016-06-09 17:53:57 +02002675 if (sor->soc->supports_hdmi || sor->soc->supports_dp) {
Thierry Redinge1335e22017-10-12 17:53:11 +02002676 struct device_node *np = pdev->dev.of_node;
2677 const char *name;
2678
2679 /*
2680 * For backwards compatibility with Tegra210 device trees,
2681 * fall back to the old clock name "source" if the new "out"
2682 * clock is not available.
2683 */
2684 if (of_property_match_string(np, "clock-names", "out") < 0)
2685 name = "source";
2686 else
2687 name = "out";
2688
2689 sor->clk_out = devm_clk_get(&pdev->dev, name);
2690 if (IS_ERR(sor->clk_out)) {
2691 err = PTR_ERR(sor->clk_out);
2692 dev_err(sor->dev, "failed to get %s clock: %d\n",
2693 name, err);
Thierry Reding618dee32016-06-09 17:53:57 +02002694 goto remove;
2695 }
2696 }
2697
Thierry Reding6b6b6042013-11-15 16:06:05 +01002698 sor->clk_parent = devm_clk_get(&pdev->dev, "parent");
Thierry Reding4dbdc742015-04-27 15:04:26 +02002699 if (IS_ERR(sor->clk_parent)) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002700 err = PTR_ERR(sor->clk_parent);
2701 dev_err(&pdev->dev, "failed to get parent clock: %d\n", err);
2702 goto remove;
Thierry Reding4dbdc742015-04-27 15:04:26 +02002703 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01002704
Thierry Reding6b6b6042013-11-15 16:06:05 +01002705 sor->clk_safe = devm_clk_get(&pdev->dev, "safe");
Thierry Reding4dbdc742015-04-27 15:04:26 +02002706 if (IS_ERR(sor->clk_safe)) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002707 err = PTR_ERR(sor->clk_safe);
2708 dev_err(&pdev->dev, "failed to get safe clock: %d\n", err);
2709 goto remove;
Thierry Reding4dbdc742015-04-27 15:04:26 +02002710 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01002711
Thierry Reding6b6b6042013-11-15 16:06:05 +01002712 sor->clk_dp = devm_clk_get(&pdev->dev, "dp");
Thierry Reding4dbdc742015-04-27 15:04:26 +02002713 if (IS_ERR(sor->clk_dp)) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002714 err = PTR_ERR(sor->clk_dp);
2715 dev_err(&pdev->dev, "failed to get DP clock: %d\n", err);
2716 goto remove;
Thierry Reding4dbdc742015-04-27 15:04:26 +02002717 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01002718
Thierry Redinge1335e22017-10-12 17:53:11 +02002719 /*
2720 * Starting with Tegra186, the BPMP provides an implementation for
2721 * the pad output clock, so we have to look it up from device tree.
2722 */
2723 sor->clk_pad = devm_clk_get(&pdev->dev, "pad");
2724 if (IS_ERR(sor->clk_pad)) {
2725 if (sor->clk_pad != ERR_PTR(-ENOENT)) {
2726 err = PTR_ERR(sor->clk_pad);
2727 goto remove;
2728 }
2729
2730 /*
2731 * If the pad output clock is not available, then we assume
2732 * we're on Tegra210 or earlier and have to provide our own
2733 * implementation.
2734 */
2735 sor->clk_pad = NULL;
2736 }
2737
2738 /*
2739 * The bootloader may have set up the SOR such that it's module clock
2740 * is sourced by one of the display PLLs. However, that doesn't work
2741 * without properly having set up other bits of the SOR.
2742 */
2743 err = clk_set_parent(sor->clk_out, sor->clk_safe);
2744 if (err < 0) {
2745 dev_err(&pdev->dev, "failed to use safe clock: %d\n", err);
2746 goto remove;
2747 }
2748
Thierry Redingaaff8bd2015-08-07 16:04:54 +02002749 platform_set_drvdata(pdev, sor);
2750 pm_runtime_enable(&pdev->dev);
2751
Thierry Redinge1335e22017-10-12 17:53:11 +02002752 /*
2753 * On Tegra210 and earlier, provide our own implementation for the
2754 * pad output clock.
2755 */
2756 if (!sor->clk_pad) {
2757 err = pm_runtime_get_sync(&pdev->dev);
2758 if (err < 0) {
2759 dev_err(&pdev->dev, "failed to get runtime PM: %d\n",
2760 err);
2761 goto remove;
2762 }
Thierry Redingb2992212015-10-01 14:25:03 +02002763
Thierry Redinge1335e22017-10-12 17:53:11 +02002764 sor->clk_pad = tegra_clk_sor_pad_register(sor,
2765 "sor1_pad_clkout");
2766 pm_runtime_put(&pdev->dev);
2767 }
2768
2769 if (IS_ERR(sor->clk_pad)) {
2770 err = PTR_ERR(sor->clk_pad);
2771 dev_err(&pdev->dev, "failed to register SOR pad clock: %d\n",
2772 err);
Thierry Redingb2992212015-10-01 14:25:03 +02002773 goto remove;
2774 }
2775
Thierry Reding6b6b6042013-11-15 16:06:05 +01002776 INIT_LIST_HEAD(&sor->client.list);
2777 sor->client.ops = &sor_client_ops;
2778 sor->client.dev = &pdev->dev;
2779
Thierry Reding6b6b6042013-11-15 16:06:05 +01002780 err = host1x_client_register(&sor->client);
2781 if (err < 0) {
2782 dev_err(&pdev->dev, "failed to register host1x client: %d\n",
2783 err);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002784 goto remove;
Thierry Reding6b6b6042013-11-15 16:06:05 +01002785 }
2786
Thierry Reding6b6b6042013-11-15 16:06:05 +01002787 return 0;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002788
2789remove:
2790 if (sor->ops && sor->ops->remove)
2791 sor->ops->remove(sor);
2792output:
2793 tegra_output_remove(&sor->output);
2794 return err;
Thierry Reding6b6b6042013-11-15 16:06:05 +01002795}
2796
2797static int tegra_sor_remove(struct platform_device *pdev)
2798{
2799 struct tegra_sor *sor = platform_get_drvdata(pdev);
2800 int err;
2801
Thierry Redingaaff8bd2015-08-07 16:04:54 +02002802 pm_runtime_disable(&pdev->dev);
2803
Thierry Reding6b6b6042013-11-15 16:06:05 +01002804 err = host1x_client_unregister(&sor->client);
2805 if (err < 0) {
2806 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
2807 err);
2808 return err;
2809 }
2810
Thierry Reding459cc2c2015-07-30 10:34:24 +02002811 if (sor->ops && sor->ops->remove) {
2812 err = sor->ops->remove(sor);
2813 if (err < 0)
2814 dev_err(&pdev->dev, "failed to remove SOR: %d\n", err);
2815 }
2816
Thierry Reding328ec692014-12-19 15:55:08 +01002817 tegra_output_remove(&sor->output);
Thierry Reding6b6b6042013-11-15 16:06:05 +01002818
2819 return 0;
2820}
2821
Thierry Redingaaff8bd2015-08-07 16:04:54 +02002822#ifdef CONFIG_PM
2823static int tegra_sor_suspend(struct device *dev)
2824{
2825 struct tegra_sor *sor = dev_get_drvdata(dev);
2826 int err;
2827
Jon Hunterf8c79122016-07-01 14:21:38 +01002828 if (sor->rst) {
2829 err = reset_control_assert(sor->rst);
2830 if (err < 0) {
2831 dev_err(dev, "failed to assert reset: %d\n", err);
2832 return err;
2833 }
Thierry Redingaaff8bd2015-08-07 16:04:54 +02002834 }
2835
2836 usleep_range(1000, 2000);
2837
2838 clk_disable_unprepare(sor->clk);
2839
2840 return 0;
2841}
2842
2843static int tegra_sor_resume(struct device *dev)
2844{
2845 struct tegra_sor *sor = dev_get_drvdata(dev);
2846 int err;
2847
2848 err = clk_prepare_enable(sor->clk);
2849 if (err < 0) {
2850 dev_err(dev, "failed to enable clock: %d\n", err);
2851 return err;
2852 }
2853
2854 usleep_range(1000, 2000);
2855
Jon Hunterf8c79122016-07-01 14:21:38 +01002856 if (sor->rst) {
2857 err = reset_control_deassert(sor->rst);
2858 if (err < 0) {
2859 dev_err(dev, "failed to deassert reset: %d\n", err);
2860 clk_disable_unprepare(sor->clk);
2861 return err;
2862 }
Thierry Redingaaff8bd2015-08-07 16:04:54 +02002863 }
2864
2865 return 0;
2866}
2867#endif
2868
2869static const struct dev_pm_ops tegra_sor_pm_ops = {
2870 SET_RUNTIME_PM_OPS(tegra_sor_suspend, tegra_sor_resume, NULL)
2871};
2872
Thierry Reding6b6b6042013-11-15 16:06:05 +01002873struct platform_driver tegra_sor_driver = {
2874 .driver = {
2875 .name = "tegra-sor",
2876 .of_match_table = tegra_sor_of_match,
Thierry Redingaaff8bd2015-08-07 16:04:54 +02002877 .pm = &tegra_sor_pm_ops,
Thierry Reding6b6b6042013-11-15 16:06:05 +01002878 },
2879 .probe = tegra_sor_probe,
2880 .remove = tegra_sor_remove,
2881};