blob: 2fba6c2bd48661bebc629b00cf3510e494200aca [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
147struct tegra_sor_soc {
148 bool supports_edp;
149 bool supports_lvds;
150 bool supports_hdmi;
151 bool supports_dp;
152
153 const struct tegra_sor_hdmi_settings *settings;
154 unsigned int num_settings;
Thierry Reding30b49432015-08-03 15:50:32 +0200155
156 const u8 *xbar_cfg;
Thierry Reding459cc2c2015-07-30 10:34:24 +0200157};
158
159struct tegra_sor;
160
161struct tegra_sor_ops {
162 const char *name;
163 int (*probe)(struct tegra_sor *sor);
164 int (*remove)(struct tegra_sor *sor);
165};
166
Thierry Reding6b6b6042013-11-15 16:06:05 +0100167struct tegra_sor {
168 struct host1x_client client;
169 struct tegra_output output;
170 struct device *dev;
171
Thierry Reding459cc2c2015-07-30 10:34:24 +0200172 const struct tegra_sor_soc *soc;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100173 void __iomem *regs;
174
175 struct reset_control *rst;
176 struct clk *clk_parent;
177 struct clk *clk_safe;
Thierry Redinge1335e22017-10-12 17:53:11 +0200178 struct clk *clk_out;
179 struct clk *clk_pad;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100180 struct clk *clk_dp;
181 struct clk *clk;
182
Thierry Reding9542c232015-07-08 13:39:09 +0200183 struct drm_dp_aux *aux;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100184
Thierry Redingdab16332015-01-26 16:04:08 +0100185 struct drm_info_list *debugfs_files;
Thierry Reding459cc2c2015-07-30 10:34:24 +0200186
187 const struct tegra_sor_ops *ops;
188
189 /* for HDMI 2.0 */
190 struct tegra_sor_hdmi_settings *settings;
191 unsigned int num_settings;
192
193 struct regulator *avdd_io_supply;
194 struct regulator *vdd_pll_supply;
195 struct regulator *hdmi_supply;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100196};
197
Thierry Redingc31efa72015-09-08 16:09:22 +0200198struct tegra_sor_state {
199 struct drm_connector_state base;
200
201 unsigned int bpc;
202};
203
204static inline struct tegra_sor_state *
205to_sor_state(struct drm_connector_state *state)
206{
207 return container_of(state, struct tegra_sor_state, base);
208}
209
Thierry Reding34fa1832014-06-05 16:31:10 +0200210struct tegra_sor_config {
211 u32 bits_per_pixel;
212
213 u32 active_polarity;
214 u32 active_count;
215 u32 tu_size;
216 u32 active_frac;
217 u32 watermark;
Thierry Reding7890b572014-06-05 16:12:46 +0200218
219 u32 hblank_symbols;
220 u32 vblank_symbols;
Thierry Reding34fa1832014-06-05 16:31:10 +0200221};
222
Thierry Reding6b6b6042013-11-15 16:06:05 +0100223static inline struct tegra_sor *
224host1x_client_to_sor(struct host1x_client *client)
225{
226 return container_of(client, struct tegra_sor, client);
227}
228
229static inline struct tegra_sor *to_sor(struct tegra_output *output)
230{
231 return container_of(output, struct tegra_sor, output);
232}
233
Thierry Reding5c5f1302017-08-15 15:41:09 +0200234static inline u32 tegra_sor_readl(struct tegra_sor *sor, unsigned int offset)
Thierry Reding6b6b6042013-11-15 16:06:05 +0100235{
Thierry Reding932f6522017-08-15 15:41:14 +0200236 u32 value = readl(sor->regs + (offset << 2));
237
238 trace_sor_readl(sor->dev, offset, value);
239
240 return value;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100241}
242
Thierry Reding28fe2072015-01-26 16:02:48 +0100243static inline void tegra_sor_writel(struct tegra_sor *sor, u32 value,
Thierry Reding5c5f1302017-08-15 15:41:09 +0200244 unsigned int offset)
Thierry Reding6b6b6042013-11-15 16:06:05 +0100245{
Thierry Reding932f6522017-08-15 15:41:14 +0200246 trace_sor_writel(sor->dev, offset, value);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100247 writel(value, sor->regs + (offset << 2));
248}
249
Thierry Reding25bb2ce2015-08-03 14:23:29 +0200250static int tegra_sor_set_parent_clock(struct tegra_sor *sor, struct clk *parent)
251{
252 int err;
253
254 clk_disable_unprepare(sor->clk);
255
Thierry Redinge1335e22017-10-12 17:53:11 +0200256 err = clk_set_parent(sor->clk_out, parent);
Thierry Reding25bb2ce2015-08-03 14:23:29 +0200257 if (err < 0)
258 return err;
259
260 err = clk_prepare_enable(sor->clk);
261 if (err < 0)
262 return err;
263
264 return 0;
265}
266
Thierry Redinge1335e22017-10-12 17:53:11 +0200267struct tegra_clk_sor_pad {
Thierry Redingb2992212015-10-01 14:25:03 +0200268 struct clk_hw hw;
269 struct tegra_sor *sor;
270};
271
Thierry Redinge1335e22017-10-12 17:53:11 +0200272static inline struct tegra_clk_sor_pad *to_pad(struct clk_hw *hw)
Thierry Redingb2992212015-10-01 14:25:03 +0200273{
Thierry Redinge1335e22017-10-12 17:53:11 +0200274 return container_of(hw, struct tegra_clk_sor_pad, hw);
Thierry Redingb2992212015-10-01 14:25:03 +0200275}
276
Thierry Redinge1335e22017-10-12 17:53:11 +0200277static const char * const tegra_clk_sor_pad_parents[] = {
Thierry Redingb2992212015-10-01 14:25:03 +0200278 "pll_d2_out0", "pll_dp"
279};
280
Thierry Redinge1335e22017-10-12 17:53:11 +0200281static int tegra_clk_sor_pad_set_parent(struct clk_hw *hw, u8 index)
Thierry Redingb2992212015-10-01 14:25:03 +0200282{
Thierry Redinge1335e22017-10-12 17:53:11 +0200283 struct tegra_clk_sor_pad *pad = to_pad(hw);
284 struct tegra_sor *sor = pad->sor;
Thierry Redingb2992212015-10-01 14:25:03 +0200285 u32 value;
286
287 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
288 value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
289
290 switch (index) {
291 case 0:
292 value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK;
293 break;
294
295 case 1:
296 value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
297 break;
298 }
299
300 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
301
302 return 0;
303}
304
Thierry Redinge1335e22017-10-12 17:53:11 +0200305static u8 tegra_clk_sor_pad_get_parent(struct clk_hw *hw)
Thierry Redingb2992212015-10-01 14:25:03 +0200306{
Thierry Redinge1335e22017-10-12 17:53:11 +0200307 struct tegra_clk_sor_pad *pad = to_pad(hw);
308 struct tegra_sor *sor = pad->sor;
Thierry Redingb2992212015-10-01 14:25:03 +0200309 u8 parent = U8_MAX;
310 u32 value;
311
312 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
313
314 switch (value & SOR_CLK_CNTRL_DP_CLK_SEL_MASK) {
315 case SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK:
316 case SOR_CLK_CNTRL_DP_CLK_SEL_DIFF_PCLK:
317 parent = 0;
318 break;
319
320 case SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK:
321 case SOR_CLK_CNTRL_DP_CLK_SEL_DIFF_DPCLK:
322 parent = 1;
323 break;
324 }
325
326 return parent;
327}
328
Thierry Redinge1335e22017-10-12 17:53:11 +0200329static const struct clk_ops tegra_clk_sor_pad_ops = {
330 .set_parent = tegra_clk_sor_pad_set_parent,
331 .get_parent = tegra_clk_sor_pad_get_parent,
Thierry Redingb2992212015-10-01 14:25:03 +0200332};
333
Thierry Redinge1335e22017-10-12 17:53:11 +0200334static struct clk *tegra_clk_sor_pad_register(struct tegra_sor *sor,
335 const char *name)
Thierry Redingb2992212015-10-01 14:25:03 +0200336{
Thierry Redinge1335e22017-10-12 17:53:11 +0200337 struct tegra_clk_sor_pad *pad;
Thierry Redingb2992212015-10-01 14:25:03 +0200338 struct clk_init_data init;
339 struct clk *clk;
340
Thierry Redinge1335e22017-10-12 17:53:11 +0200341 pad = devm_kzalloc(sor->dev, sizeof(*pad), GFP_KERNEL);
342 if (!pad)
Thierry Redingb2992212015-10-01 14:25:03 +0200343 return ERR_PTR(-ENOMEM);
344
Thierry Redinge1335e22017-10-12 17:53:11 +0200345 pad->sor = sor;
Thierry Redingb2992212015-10-01 14:25:03 +0200346
347 init.name = name;
348 init.flags = 0;
Thierry Redinge1335e22017-10-12 17:53:11 +0200349 init.parent_names = tegra_clk_sor_pad_parents;
350 init.num_parents = ARRAY_SIZE(tegra_clk_sor_pad_parents);
351 init.ops = &tegra_clk_sor_pad_ops;
Thierry Redingb2992212015-10-01 14:25:03 +0200352
Thierry Redinge1335e22017-10-12 17:53:11 +0200353 pad->hw.init = &init;
Thierry Redingb2992212015-10-01 14:25:03 +0200354
Thierry Redinge1335e22017-10-12 17:53:11 +0200355 clk = devm_clk_register(sor->dev, &pad->hw);
Thierry Redingb2992212015-10-01 14:25:03 +0200356
357 return clk;
358}
359
Thierry Reding6b6b6042013-11-15 16:06:05 +0100360static int tegra_sor_dp_train_fast(struct tegra_sor *sor,
361 struct drm_dp_link *link)
362{
Thierry Reding6b6b6042013-11-15 16:06:05 +0100363 unsigned int i;
364 u8 pattern;
Thierry Reding28fe2072015-01-26 16:02:48 +0100365 u32 value;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100366 int err;
367
368 /* setup lane parameters */
369 value = SOR_LANE_DRIVE_CURRENT_LANE3(0x40) |
370 SOR_LANE_DRIVE_CURRENT_LANE2(0x40) |
371 SOR_LANE_DRIVE_CURRENT_LANE1(0x40) |
372 SOR_LANE_DRIVE_CURRENT_LANE0(0x40);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200373 tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100374
375 value = SOR_LANE_PREEMPHASIS_LANE3(0x0f) |
376 SOR_LANE_PREEMPHASIS_LANE2(0x0f) |
377 SOR_LANE_PREEMPHASIS_LANE1(0x0f) |
378 SOR_LANE_PREEMPHASIS_LANE0(0x0f);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200379 tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100380
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200381 value = SOR_LANE_POSTCURSOR_LANE3(0x00) |
382 SOR_LANE_POSTCURSOR_LANE2(0x00) |
383 SOR_LANE_POSTCURSOR_LANE1(0x00) |
384 SOR_LANE_POSTCURSOR_LANE0(0x00);
385 tegra_sor_writel(sor, value, SOR_LANE_POSTCURSOR0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100386
387 /* disable LVDS mode */
388 tegra_sor_writel(sor, 0, SOR_LVDS);
389
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200390 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100391 value |= SOR_DP_PADCTL_TX_PU_ENABLE;
392 value &= ~SOR_DP_PADCTL_TX_PU_MASK;
393 value |= SOR_DP_PADCTL_TX_PU(2); /* XXX: don't hardcode? */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200394 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100395
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200396 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100397 value |= SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
398 SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200399 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100400
401 usleep_range(10, 100);
402
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200403 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100404 value &= ~(SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
405 SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200406 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100407
Thierry Reding9542c232015-07-08 13:39:09 +0200408 err = drm_dp_aux_prepare(sor->aux, DP_SET_ANSI_8B10B);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100409 if (err < 0)
410 return err;
411
412 for (i = 0, value = 0; i < link->num_lanes; i++) {
413 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
414 SOR_DP_TPG_SCRAMBLER_NONE |
415 SOR_DP_TPG_PATTERN_TRAIN1;
416 value = (value << 8) | lane;
417 }
418
419 tegra_sor_writel(sor, value, SOR_DP_TPG);
420
421 pattern = DP_TRAINING_PATTERN_1;
422
Thierry Reding9542c232015-07-08 13:39:09 +0200423 err = drm_dp_aux_train(sor->aux, link, pattern);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100424 if (err < 0)
425 return err;
426
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200427 value = tegra_sor_readl(sor, SOR_DP_SPARE0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100428 value |= SOR_DP_SPARE_SEQ_ENABLE;
429 value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
430 value |= SOR_DP_SPARE_MACRO_SOR_CLK;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200431 tegra_sor_writel(sor, value, SOR_DP_SPARE0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100432
433 for (i = 0, value = 0; i < link->num_lanes; i++) {
434 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
435 SOR_DP_TPG_SCRAMBLER_NONE |
436 SOR_DP_TPG_PATTERN_TRAIN2;
437 value = (value << 8) | lane;
438 }
439
440 tegra_sor_writel(sor, value, SOR_DP_TPG);
441
442 pattern = DP_LINK_SCRAMBLING_DISABLE | DP_TRAINING_PATTERN_2;
443
Thierry Reding9542c232015-07-08 13:39:09 +0200444 err = drm_dp_aux_train(sor->aux, link, pattern);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100445 if (err < 0)
446 return err;
447
448 for (i = 0, value = 0; i < link->num_lanes; i++) {
449 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
450 SOR_DP_TPG_SCRAMBLER_GALIOS |
451 SOR_DP_TPG_PATTERN_NONE;
452 value = (value << 8) | lane;
453 }
454
455 tegra_sor_writel(sor, value, SOR_DP_TPG);
456
457 pattern = DP_TRAINING_PATTERN_DISABLE;
458
Thierry Reding9542c232015-07-08 13:39:09 +0200459 err = drm_dp_aux_train(sor->aux, link, pattern);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100460 if (err < 0)
461 return err;
462
463 return 0;
464}
465
Thierry Reding459cc2c2015-07-30 10:34:24 +0200466static void tegra_sor_dp_term_calibrate(struct tegra_sor *sor)
467{
468 u32 mask = 0x08, adj = 0, value;
469
470 /* enable pad calibration logic */
471 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
472 value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
473 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
474
475 value = tegra_sor_readl(sor, SOR_PLL1);
476 value |= SOR_PLL1_TMDS_TERM;
477 tegra_sor_writel(sor, value, SOR_PLL1);
478
479 while (mask) {
480 adj |= mask;
481
482 value = tegra_sor_readl(sor, SOR_PLL1);
483 value &= ~SOR_PLL1_TMDS_TERMADJ_MASK;
484 value |= SOR_PLL1_TMDS_TERMADJ(adj);
485 tegra_sor_writel(sor, value, SOR_PLL1);
486
487 usleep_range(100, 200);
488
489 value = tegra_sor_readl(sor, SOR_PLL1);
490 if (value & SOR_PLL1_TERM_COMPOUT)
491 adj &= ~mask;
492
493 mask >>= 1;
494 }
495
496 value = tegra_sor_readl(sor, SOR_PLL1);
497 value &= ~SOR_PLL1_TMDS_TERMADJ_MASK;
498 value |= SOR_PLL1_TMDS_TERMADJ(adj);
499 tegra_sor_writel(sor, value, SOR_PLL1);
500
501 /* disable pad calibration logic */
502 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
503 value |= SOR_DP_PADCTL_PAD_CAL_PD;
504 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
505}
506
Thierry Reding6b6b6042013-11-15 16:06:05 +0100507static void tegra_sor_super_update(struct tegra_sor *sor)
508{
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200509 tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
510 tegra_sor_writel(sor, 1, SOR_SUPER_STATE0);
511 tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100512}
513
514static void tegra_sor_update(struct tegra_sor *sor)
515{
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200516 tegra_sor_writel(sor, 0, SOR_STATE0);
517 tegra_sor_writel(sor, 1, SOR_STATE0);
518 tegra_sor_writel(sor, 0, SOR_STATE0);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100519}
520
521static int tegra_sor_setup_pwm(struct tegra_sor *sor, unsigned long timeout)
522{
Thierry Reding28fe2072015-01-26 16:02:48 +0100523 u32 value;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100524
525 value = tegra_sor_readl(sor, SOR_PWM_DIV);
526 value &= ~SOR_PWM_DIV_MASK;
527 value |= 0x400; /* period */
528 tegra_sor_writel(sor, value, SOR_PWM_DIV);
529
530 value = tegra_sor_readl(sor, SOR_PWM_CTL);
531 value &= ~SOR_PWM_CTL_DUTY_CYCLE_MASK;
532 value |= 0x400; /* duty cycle */
533 value &= ~SOR_PWM_CTL_CLK_SEL; /* clock source: PCLK */
534 value |= SOR_PWM_CTL_TRIGGER;
535 tegra_sor_writel(sor, value, SOR_PWM_CTL);
536
537 timeout = jiffies + msecs_to_jiffies(timeout);
538
539 while (time_before(jiffies, timeout)) {
540 value = tegra_sor_readl(sor, SOR_PWM_CTL);
541 if ((value & SOR_PWM_CTL_TRIGGER) == 0)
542 return 0;
543
544 usleep_range(25, 100);
545 }
546
547 return -ETIMEDOUT;
548}
549
550static int tegra_sor_attach(struct tegra_sor *sor)
551{
552 unsigned long value, timeout;
553
554 /* wake up in normal mode */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200555 value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100556 value |= SOR_SUPER_STATE_HEAD_MODE_AWAKE;
557 value |= SOR_SUPER_STATE_MODE_NORMAL;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200558 tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100559 tegra_sor_super_update(sor);
560
561 /* attach */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200562 value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100563 value |= SOR_SUPER_STATE_ATTACHED;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200564 tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100565 tegra_sor_super_update(sor);
566
567 timeout = jiffies + msecs_to_jiffies(250);
568
569 while (time_before(jiffies, timeout)) {
570 value = tegra_sor_readl(sor, SOR_TEST);
571 if ((value & SOR_TEST_ATTACHED) != 0)
572 return 0;
573
574 usleep_range(25, 100);
575 }
576
577 return -ETIMEDOUT;
578}
579
580static int tegra_sor_wakeup(struct tegra_sor *sor)
581{
Thierry Reding6b6b6042013-11-15 16:06:05 +0100582 unsigned long value, timeout;
583
Thierry Reding6b6b6042013-11-15 16:06:05 +0100584 timeout = jiffies + msecs_to_jiffies(250);
585
586 /* wait for head to wake up */
587 while (time_before(jiffies, timeout)) {
588 value = tegra_sor_readl(sor, SOR_TEST);
589 value &= SOR_TEST_HEAD_MODE_MASK;
590
591 if (value == SOR_TEST_HEAD_MODE_AWAKE)
592 return 0;
593
594 usleep_range(25, 100);
595 }
596
597 return -ETIMEDOUT;
598}
599
600static int tegra_sor_power_up(struct tegra_sor *sor, unsigned long timeout)
601{
Thierry Reding28fe2072015-01-26 16:02:48 +0100602 u32 value;
Thierry Reding6b6b6042013-11-15 16:06:05 +0100603
604 value = tegra_sor_readl(sor, SOR_PWR);
605 value |= SOR_PWR_TRIGGER | SOR_PWR_NORMAL_STATE_PU;
606 tegra_sor_writel(sor, value, SOR_PWR);
607
608 timeout = jiffies + msecs_to_jiffies(timeout);
609
610 while (time_before(jiffies, timeout)) {
611 value = tegra_sor_readl(sor, SOR_PWR);
612 if ((value & SOR_PWR_TRIGGER) == 0)
613 return 0;
614
615 usleep_range(25, 100);
616 }
617
618 return -ETIMEDOUT;
619}
620
Thierry Reding34fa1832014-06-05 16:31:10 +0200621struct tegra_sor_params {
622 /* number of link clocks per line */
623 unsigned int num_clocks;
624 /* ratio between input and output */
625 u64 ratio;
626 /* precision factor */
627 u64 precision;
628
629 unsigned int active_polarity;
630 unsigned int active_count;
631 unsigned int active_frac;
632 unsigned int tu_size;
633 unsigned int error;
634};
635
636static int tegra_sor_compute_params(struct tegra_sor *sor,
637 struct tegra_sor_params *params,
638 unsigned int tu_size)
639{
640 u64 active_sym, active_count, frac, approx;
641 u32 active_polarity, active_frac = 0;
642 const u64 f = params->precision;
643 s64 error;
644
645 active_sym = params->ratio * tu_size;
646 active_count = div_u64(active_sym, f) * f;
647 frac = active_sym - active_count;
648
649 /* fraction < 0.5 */
650 if (frac >= (f / 2)) {
651 active_polarity = 1;
652 frac = f - frac;
653 } else {
654 active_polarity = 0;
655 }
656
657 if (frac != 0) {
658 frac = div_u64(f * f, frac); /* 1/fraction */
659 if (frac <= (15 * f)) {
660 active_frac = div_u64(frac, f);
661
662 /* round up */
663 if (active_polarity)
664 active_frac++;
665 } else {
666 active_frac = active_polarity ? 1 : 15;
667 }
668 }
669
670 if (active_frac == 1)
671 active_polarity = 0;
672
673 if (active_polarity == 1) {
674 if (active_frac) {
675 approx = active_count + (active_frac * (f - 1)) * f;
676 approx = div_u64(approx, active_frac * f);
677 } else {
678 approx = active_count + f;
679 }
680 } else {
681 if (active_frac)
682 approx = active_count + div_u64(f, active_frac);
683 else
684 approx = active_count;
685 }
686
687 error = div_s64(active_sym - approx, tu_size);
688 error *= params->num_clocks;
689
Andrew Morton79211c82015-11-09 14:58:13 -0800690 if (error <= 0 && abs(error) < params->error) {
Thierry Reding34fa1832014-06-05 16:31:10 +0200691 params->active_count = div_u64(active_count, f);
692 params->active_polarity = active_polarity;
693 params->active_frac = active_frac;
Andrew Morton79211c82015-11-09 14:58:13 -0800694 params->error = abs(error);
Thierry Reding34fa1832014-06-05 16:31:10 +0200695 params->tu_size = tu_size;
696
697 if (error == 0)
698 return true;
699 }
700
701 return false;
702}
703
Thierry Redinga1983592015-07-21 16:46:52 +0200704static int tegra_sor_compute_config(struct tegra_sor *sor,
705 const struct drm_display_mode *mode,
706 struct tegra_sor_config *config,
707 struct drm_dp_link *link)
Thierry Reding34fa1832014-06-05 16:31:10 +0200708{
709 const u64 f = 100000, link_rate = link->rate * 1000;
710 const u64 pclk = mode->clock * 1000;
Thierry Reding7890b572014-06-05 16:12:46 +0200711 u64 input, output, watermark, num;
Thierry Reding34fa1832014-06-05 16:31:10 +0200712 struct tegra_sor_params params;
Thierry Reding34fa1832014-06-05 16:31:10 +0200713 u32 num_syms_per_line;
714 unsigned int i;
715
716 if (!link_rate || !link->num_lanes || !pclk || !config->bits_per_pixel)
717 return -EINVAL;
718
719 output = link_rate * 8 * link->num_lanes;
720 input = pclk * config->bits_per_pixel;
721
722 if (input >= output)
723 return -ERANGE;
724
725 memset(&params, 0, sizeof(params));
726 params.ratio = div64_u64(input * f, output);
727 params.num_clocks = div_u64(link_rate * mode->hdisplay, pclk);
728 params.precision = f;
729 params.error = 64 * f;
730 params.tu_size = 64;
731
732 for (i = params.tu_size; i >= 32; i--)
733 if (tegra_sor_compute_params(sor, &params, i))
734 break;
735
736 if (params.active_frac == 0) {
737 config->active_polarity = 0;
738 config->active_count = params.active_count;
739
740 if (!params.active_polarity)
741 config->active_count--;
742
743 config->tu_size = params.tu_size;
744 config->active_frac = 1;
745 } else {
746 config->active_polarity = params.active_polarity;
747 config->active_count = params.active_count;
748 config->active_frac = params.active_frac;
749 config->tu_size = params.tu_size;
750 }
751
752 dev_dbg(sor->dev,
753 "polarity: %d active count: %d tu size: %d active frac: %d\n",
754 config->active_polarity, config->active_count,
755 config->tu_size, config->active_frac);
756
757 watermark = params.ratio * config->tu_size * (f - params.ratio);
758 watermark = div_u64(watermark, f);
759
760 watermark = div_u64(watermark + params.error, f);
761 config->watermark = watermark + (config->bits_per_pixel / 8) + 2;
762 num_syms_per_line = (mode->hdisplay * config->bits_per_pixel) *
763 (link->num_lanes * 8);
764
765 if (config->watermark > 30) {
766 config->watermark = 30;
767 dev_err(sor->dev,
768 "unable to compute TU size, forcing watermark to %u\n",
769 config->watermark);
770 } else if (config->watermark > num_syms_per_line) {
771 config->watermark = num_syms_per_line;
772 dev_err(sor->dev, "watermark too high, forcing to %u\n",
773 config->watermark);
774 }
775
Thierry Reding7890b572014-06-05 16:12:46 +0200776 /* compute the number of symbols per horizontal blanking interval */
777 num = ((mode->htotal - mode->hdisplay) - 7) * link_rate;
778 config->hblank_symbols = div_u64(num, pclk);
779
780 if (link->capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
781 config->hblank_symbols -= 3;
782
783 config->hblank_symbols -= 12 / link->num_lanes;
784
785 /* compute the number of symbols per vertical blanking interval */
786 num = (mode->hdisplay - 25) * link_rate;
787 config->vblank_symbols = div_u64(num, pclk);
788 config->vblank_symbols -= 36 / link->num_lanes + 4;
789
790 dev_dbg(sor->dev, "blank symbols: H:%u V:%u\n", config->hblank_symbols,
791 config->vblank_symbols);
792
Thierry Reding34fa1832014-06-05 16:31:10 +0200793 return 0;
794}
795
Thierry Reding402f6bc2015-07-21 16:48:19 +0200796static void tegra_sor_apply_config(struct tegra_sor *sor,
797 const struct tegra_sor_config *config)
798{
799 u32 value;
800
801 value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
802 value &= ~SOR_DP_LINKCTL_TU_SIZE_MASK;
803 value |= SOR_DP_LINKCTL_TU_SIZE(config->tu_size);
804 tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
805
806 value = tegra_sor_readl(sor, SOR_DP_CONFIG0);
807 value &= ~SOR_DP_CONFIG_WATERMARK_MASK;
808 value |= SOR_DP_CONFIG_WATERMARK(config->watermark);
809
810 value &= ~SOR_DP_CONFIG_ACTIVE_SYM_COUNT_MASK;
811 value |= SOR_DP_CONFIG_ACTIVE_SYM_COUNT(config->active_count);
812
813 value &= ~SOR_DP_CONFIG_ACTIVE_SYM_FRAC_MASK;
814 value |= SOR_DP_CONFIG_ACTIVE_SYM_FRAC(config->active_frac);
815
816 if (config->active_polarity)
817 value |= SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
818 else
819 value &= ~SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
820
821 value |= SOR_DP_CONFIG_ACTIVE_SYM_ENABLE;
822 value |= SOR_DP_CONFIG_DISPARITY_NEGATIVE;
823 tegra_sor_writel(sor, value, SOR_DP_CONFIG0);
824
825 value = tegra_sor_readl(sor, SOR_DP_AUDIO_HBLANK_SYMBOLS);
826 value &= ~SOR_DP_AUDIO_HBLANK_SYMBOLS_MASK;
827 value |= config->hblank_symbols & 0xffff;
828 tegra_sor_writel(sor, value, SOR_DP_AUDIO_HBLANK_SYMBOLS);
829
830 value = tegra_sor_readl(sor, SOR_DP_AUDIO_VBLANK_SYMBOLS);
831 value &= ~SOR_DP_AUDIO_VBLANK_SYMBOLS_MASK;
832 value |= config->vblank_symbols & 0xffff;
833 tegra_sor_writel(sor, value, SOR_DP_AUDIO_VBLANK_SYMBOLS);
834}
835
Thierry Reding2bd1dd32015-08-03 15:46:15 +0200836static void tegra_sor_mode_set(struct tegra_sor *sor,
837 const struct drm_display_mode *mode,
Thierry Redingc31efa72015-09-08 16:09:22 +0200838 struct tegra_sor_state *state)
Thierry Reding2bd1dd32015-08-03 15:46:15 +0200839{
840 struct tegra_dc *dc = to_tegra_dc(sor->output.encoder.crtc);
841 unsigned int vbe, vse, hbe, hse, vbs, hbs;
842 u32 value;
843
844 value = tegra_sor_readl(sor, SOR_STATE1);
845 value &= ~SOR_STATE_ASY_PIXELDEPTH_MASK;
846 value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
847 value &= ~SOR_STATE_ASY_OWNER_MASK;
848
849 value |= SOR_STATE_ASY_CRC_MODE_COMPLETE |
850 SOR_STATE_ASY_OWNER(dc->pipe + 1);
851
852 if (mode->flags & DRM_MODE_FLAG_PHSYNC)
853 value &= ~SOR_STATE_ASY_HSYNCPOL;
854
855 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
856 value |= SOR_STATE_ASY_HSYNCPOL;
857
858 if (mode->flags & DRM_MODE_FLAG_PVSYNC)
859 value &= ~SOR_STATE_ASY_VSYNCPOL;
860
861 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
862 value |= SOR_STATE_ASY_VSYNCPOL;
863
Thierry Redingc31efa72015-09-08 16:09:22 +0200864 switch (state->bpc) {
865 case 16:
866 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_48_444;
867 break;
868
869 case 12:
870 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_36_444;
871 break;
872
873 case 10:
874 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_30_444;
875 break;
876
Thierry Reding2bd1dd32015-08-03 15:46:15 +0200877 case 8:
878 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
879 break;
880
881 case 6:
882 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_18_444;
883 break;
884
885 default:
Thierry Redingc31efa72015-09-08 16:09:22 +0200886 value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
Thierry Reding2bd1dd32015-08-03 15:46:15 +0200887 break;
888 }
889
890 tegra_sor_writel(sor, value, SOR_STATE1);
891
892 /*
893 * TODO: The video timing programming below doesn't seem to match the
894 * register definitions.
895 */
896
897 value = ((mode->vtotal & 0x7fff) << 16) | (mode->htotal & 0x7fff);
898 tegra_sor_writel(sor, value, SOR_HEAD_STATE1(dc->pipe));
899
900 /* sync end = sync width - 1 */
901 vse = mode->vsync_end - mode->vsync_start - 1;
902 hse = mode->hsync_end - mode->hsync_start - 1;
903
904 value = ((vse & 0x7fff) << 16) | (hse & 0x7fff);
905 tegra_sor_writel(sor, value, SOR_HEAD_STATE2(dc->pipe));
906
907 /* blank end = sync end + back porch */
908 vbe = vse + (mode->vtotal - mode->vsync_end);
909 hbe = hse + (mode->htotal - mode->hsync_end);
910
911 value = ((vbe & 0x7fff) << 16) | (hbe & 0x7fff);
912 tegra_sor_writel(sor, value, SOR_HEAD_STATE3(dc->pipe));
913
914 /* blank start = blank end + active */
915 vbs = vbe + mode->vdisplay;
916 hbs = hbe + mode->hdisplay;
917
918 value = ((vbs & 0x7fff) << 16) | (hbs & 0x7fff);
919 tegra_sor_writel(sor, value, SOR_HEAD_STATE4(dc->pipe));
920
921 /* XXX interlacing support */
922 tegra_sor_writel(sor, 0x001, SOR_HEAD_STATE5(dc->pipe));
923}
924
Thierry Reding6fad8f62014-11-28 15:41:34 +0100925static int tegra_sor_detach(struct tegra_sor *sor)
Thierry Reding6b6b6042013-11-15 16:06:05 +0100926{
Thierry Reding6fad8f62014-11-28 15:41:34 +0100927 unsigned long value, timeout;
928
929 /* switch to safe mode */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200930 value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100931 value &= ~SOR_SUPER_STATE_MODE_NORMAL;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200932 tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100933 tegra_sor_super_update(sor);
934
935 timeout = jiffies + msecs_to_jiffies(250);
936
937 while (time_before(jiffies, timeout)) {
938 value = tegra_sor_readl(sor, SOR_PWR);
939 if (value & SOR_PWR_MODE_SAFE)
940 break;
941 }
942
943 if ((value & SOR_PWR_MODE_SAFE) == 0)
944 return -ETIMEDOUT;
945
946 /* go to sleep */
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_HEAD_MODE_MASK;
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 /* detach */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200953 value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100954 value &= ~SOR_SUPER_STATE_ATTACHED;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +0200955 tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +0100956 tegra_sor_super_update(sor);
957
958 timeout = jiffies + msecs_to_jiffies(250);
959
960 while (time_before(jiffies, timeout)) {
961 value = tegra_sor_readl(sor, SOR_TEST);
962 if ((value & SOR_TEST_ATTACHED) == 0)
963 break;
964
965 usleep_range(25, 100);
966 }
967
968 if ((value & SOR_TEST_ATTACHED) != 0)
969 return -ETIMEDOUT;
970
971 return 0;
972}
973
974static int tegra_sor_power_down(struct tegra_sor *sor)
975{
976 unsigned long value, timeout;
977 int err;
978
979 value = tegra_sor_readl(sor, SOR_PWR);
980 value &= ~SOR_PWR_NORMAL_STATE_PU;
981 value |= SOR_PWR_TRIGGER;
982 tegra_sor_writel(sor, value, SOR_PWR);
983
984 timeout = jiffies + msecs_to_jiffies(250);
985
986 while (time_before(jiffies, timeout)) {
987 value = tegra_sor_readl(sor, SOR_PWR);
988 if ((value & SOR_PWR_TRIGGER) == 0)
989 return 0;
990
991 usleep_range(25, 100);
992 }
993
994 if ((value & SOR_PWR_TRIGGER) != 0)
995 return -ETIMEDOUT;
996
Thierry Reding25bb2ce2015-08-03 14:23:29 +0200997 /* switch to safe parent clock */
998 err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
Thierry Redinge1335e22017-10-12 17:53:11 +0200999 if (err < 0) {
Thierry Reding6fad8f62014-11-28 15:41:34 +01001000 dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
Thierry Redinge1335e22017-10-12 17:53:11 +02001001 return err;
1002 }
Thierry Reding6fad8f62014-11-28 15:41:34 +01001003
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001004 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001005 value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
1006 SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001007 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001008
1009 /* stop lane sequencer */
1010 value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_UP |
1011 SOR_LANE_SEQ_CTL_POWER_STATE_DOWN;
1012 tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
1013
1014 timeout = jiffies + msecs_to_jiffies(250);
1015
1016 while (time_before(jiffies, timeout)) {
1017 value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
1018 if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
1019 break;
1020
1021 usleep_range(25, 100);
1022 }
1023
1024 if ((value & SOR_LANE_SEQ_CTL_TRIGGER) != 0)
1025 return -ETIMEDOUT;
1026
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001027 value = tegra_sor_readl(sor, SOR_PLL2);
1028 value |= SOR_PLL2_PORT_POWERDOWN;
1029 tegra_sor_writel(sor, value, SOR_PLL2);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001030
1031 usleep_range(20, 100);
1032
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001033 value = tegra_sor_readl(sor, SOR_PLL0);
1034 value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
1035 tegra_sor_writel(sor, value, SOR_PLL0);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001036
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001037 value = tegra_sor_readl(sor, SOR_PLL2);
1038 value |= SOR_PLL2_SEQ_PLLCAPPD;
1039 value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1040 tegra_sor_writel(sor, value, SOR_PLL2);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001041
1042 usleep_range(20, 100);
1043
1044 return 0;
1045}
1046
Thierry Reding6fad8f62014-11-28 15:41:34 +01001047static int tegra_sor_crc_wait(struct tegra_sor *sor, unsigned long timeout)
1048{
1049 u32 value;
1050
1051 timeout = jiffies + msecs_to_jiffies(timeout);
1052
1053 while (time_before(jiffies, timeout)) {
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001054 value = tegra_sor_readl(sor, SOR_CRCA);
1055 if (value & SOR_CRCA_VALID)
Thierry Reding6fad8f62014-11-28 15:41:34 +01001056 return 0;
1057
1058 usleep_range(100, 200);
1059 }
1060
1061 return -ETIMEDOUT;
1062}
1063
Thierry Reding530239a2015-08-06 11:04:54 +02001064static int tegra_sor_show_crc(struct seq_file *s, void *data)
Thierry Reding6fad8f62014-11-28 15:41:34 +01001065{
Thierry Reding530239a2015-08-06 11:04:54 +02001066 struct drm_info_node *node = s->private;
1067 struct tegra_sor *sor = node->info_ent->data;
Thierry Reding850bab42015-07-29 17:58:41 +02001068 struct drm_crtc *crtc = sor->output.encoder.crtc;
1069 struct drm_device *drm = node->minor->dev;
Thierry Reding530239a2015-08-06 11:04:54 +02001070 int err = 0;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001071 u32 value;
1072
Thierry Reding850bab42015-07-29 17:58:41 +02001073 drm_modeset_lock_all(drm);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001074
Thierry Reding850bab42015-07-29 17:58:41 +02001075 if (!crtc || !crtc->state->active) {
1076 err = -EBUSY;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001077 goto unlock;
1078 }
1079
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001080 value = tegra_sor_readl(sor, SOR_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001081 value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001082 tegra_sor_writel(sor, value, SOR_STATE1);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001083
1084 value = tegra_sor_readl(sor, SOR_CRC_CNTRL);
1085 value |= SOR_CRC_CNTRL_ENABLE;
1086 tegra_sor_writel(sor, value, SOR_CRC_CNTRL);
1087
1088 value = tegra_sor_readl(sor, SOR_TEST);
1089 value &= ~SOR_TEST_CRC_POST_SERIALIZE;
1090 tegra_sor_writel(sor, value, SOR_TEST);
1091
1092 err = tegra_sor_crc_wait(sor, 100);
1093 if (err < 0)
1094 goto unlock;
1095
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001096 tegra_sor_writel(sor, SOR_CRCA_RESET, SOR_CRCA);
1097 value = tegra_sor_readl(sor, SOR_CRCB);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001098
Thierry Reding530239a2015-08-06 11:04:54 +02001099 seq_printf(s, "%08x\n", value);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001100
1101unlock:
Thierry Reding850bab42015-07-29 17:58:41 +02001102 drm_modeset_unlock_all(drm);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001103 return err;
1104}
1105
Thierry Reding062f5b2c2017-11-10 12:21:51 +01001106#define DEBUGFS_REG32(_name) { .name = #_name, .offset = _name }
1107
1108static const struct debugfs_reg32 tegra_sor_regs[] = {
1109 DEBUGFS_REG32(SOR_CTXSW),
1110 DEBUGFS_REG32(SOR_SUPER_STATE0),
1111 DEBUGFS_REG32(SOR_SUPER_STATE1),
1112 DEBUGFS_REG32(SOR_STATE0),
1113 DEBUGFS_REG32(SOR_STATE1),
1114 DEBUGFS_REG32(SOR_HEAD_STATE0(0)),
1115 DEBUGFS_REG32(SOR_HEAD_STATE0(1)),
1116 DEBUGFS_REG32(SOR_HEAD_STATE1(0)),
1117 DEBUGFS_REG32(SOR_HEAD_STATE1(1)),
1118 DEBUGFS_REG32(SOR_HEAD_STATE2(0)),
1119 DEBUGFS_REG32(SOR_HEAD_STATE2(1)),
1120 DEBUGFS_REG32(SOR_HEAD_STATE3(0)),
1121 DEBUGFS_REG32(SOR_HEAD_STATE3(1)),
1122 DEBUGFS_REG32(SOR_HEAD_STATE4(0)),
1123 DEBUGFS_REG32(SOR_HEAD_STATE4(1)),
1124 DEBUGFS_REG32(SOR_HEAD_STATE5(0)),
1125 DEBUGFS_REG32(SOR_HEAD_STATE5(1)),
1126 DEBUGFS_REG32(SOR_CRC_CNTRL),
1127 DEBUGFS_REG32(SOR_DP_DEBUG_MVID),
1128 DEBUGFS_REG32(SOR_CLK_CNTRL),
1129 DEBUGFS_REG32(SOR_CAP),
1130 DEBUGFS_REG32(SOR_PWR),
1131 DEBUGFS_REG32(SOR_TEST),
1132 DEBUGFS_REG32(SOR_PLL0),
1133 DEBUGFS_REG32(SOR_PLL1),
1134 DEBUGFS_REG32(SOR_PLL2),
1135 DEBUGFS_REG32(SOR_PLL3),
1136 DEBUGFS_REG32(SOR_CSTM),
1137 DEBUGFS_REG32(SOR_LVDS),
1138 DEBUGFS_REG32(SOR_CRCA),
1139 DEBUGFS_REG32(SOR_CRCB),
1140 DEBUGFS_REG32(SOR_BLANK),
1141 DEBUGFS_REG32(SOR_SEQ_CTL),
1142 DEBUGFS_REG32(SOR_LANE_SEQ_CTL),
1143 DEBUGFS_REG32(SOR_SEQ_INST(0)),
1144 DEBUGFS_REG32(SOR_SEQ_INST(1)),
1145 DEBUGFS_REG32(SOR_SEQ_INST(2)),
1146 DEBUGFS_REG32(SOR_SEQ_INST(3)),
1147 DEBUGFS_REG32(SOR_SEQ_INST(4)),
1148 DEBUGFS_REG32(SOR_SEQ_INST(5)),
1149 DEBUGFS_REG32(SOR_SEQ_INST(6)),
1150 DEBUGFS_REG32(SOR_SEQ_INST(7)),
1151 DEBUGFS_REG32(SOR_SEQ_INST(8)),
1152 DEBUGFS_REG32(SOR_SEQ_INST(9)),
1153 DEBUGFS_REG32(SOR_SEQ_INST(10)),
1154 DEBUGFS_REG32(SOR_SEQ_INST(11)),
1155 DEBUGFS_REG32(SOR_SEQ_INST(12)),
1156 DEBUGFS_REG32(SOR_SEQ_INST(13)),
1157 DEBUGFS_REG32(SOR_SEQ_INST(14)),
1158 DEBUGFS_REG32(SOR_SEQ_INST(15)),
1159 DEBUGFS_REG32(SOR_PWM_DIV),
1160 DEBUGFS_REG32(SOR_PWM_CTL),
1161 DEBUGFS_REG32(SOR_VCRC_A0),
1162 DEBUGFS_REG32(SOR_VCRC_A1),
1163 DEBUGFS_REG32(SOR_VCRC_B0),
1164 DEBUGFS_REG32(SOR_VCRC_B1),
1165 DEBUGFS_REG32(SOR_CCRC_A0),
1166 DEBUGFS_REG32(SOR_CCRC_A1),
1167 DEBUGFS_REG32(SOR_CCRC_B0),
1168 DEBUGFS_REG32(SOR_CCRC_B1),
1169 DEBUGFS_REG32(SOR_EDATA_A0),
1170 DEBUGFS_REG32(SOR_EDATA_A1),
1171 DEBUGFS_REG32(SOR_EDATA_B0),
1172 DEBUGFS_REG32(SOR_EDATA_B1),
1173 DEBUGFS_REG32(SOR_COUNT_A0),
1174 DEBUGFS_REG32(SOR_COUNT_A1),
1175 DEBUGFS_REG32(SOR_COUNT_B0),
1176 DEBUGFS_REG32(SOR_COUNT_B1),
1177 DEBUGFS_REG32(SOR_DEBUG_A0),
1178 DEBUGFS_REG32(SOR_DEBUG_A1),
1179 DEBUGFS_REG32(SOR_DEBUG_B0),
1180 DEBUGFS_REG32(SOR_DEBUG_B1),
1181 DEBUGFS_REG32(SOR_TRIG),
1182 DEBUGFS_REG32(SOR_MSCHECK),
1183 DEBUGFS_REG32(SOR_XBAR_CTRL),
1184 DEBUGFS_REG32(SOR_XBAR_POL),
1185 DEBUGFS_REG32(SOR_DP_LINKCTL0),
1186 DEBUGFS_REG32(SOR_DP_LINKCTL1),
1187 DEBUGFS_REG32(SOR_LANE_DRIVE_CURRENT0),
1188 DEBUGFS_REG32(SOR_LANE_DRIVE_CURRENT1),
1189 DEBUGFS_REG32(SOR_LANE4_DRIVE_CURRENT0),
1190 DEBUGFS_REG32(SOR_LANE4_DRIVE_CURRENT1),
1191 DEBUGFS_REG32(SOR_LANE_PREEMPHASIS0),
1192 DEBUGFS_REG32(SOR_LANE_PREEMPHASIS1),
1193 DEBUGFS_REG32(SOR_LANE4_PREEMPHASIS0),
1194 DEBUGFS_REG32(SOR_LANE4_PREEMPHASIS1),
1195 DEBUGFS_REG32(SOR_LANE_POSTCURSOR0),
1196 DEBUGFS_REG32(SOR_LANE_POSTCURSOR1),
1197 DEBUGFS_REG32(SOR_DP_CONFIG0),
1198 DEBUGFS_REG32(SOR_DP_CONFIG1),
1199 DEBUGFS_REG32(SOR_DP_MN0),
1200 DEBUGFS_REG32(SOR_DP_MN1),
1201 DEBUGFS_REG32(SOR_DP_PADCTL0),
1202 DEBUGFS_REG32(SOR_DP_PADCTL1),
1203 DEBUGFS_REG32(SOR_DP_DEBUG0),
1204 DEBUGFS_REG32(SOR_DP_DEBUG1),
1205 DEBUGFS_REG32(SOR_DP_SPARE0),
1206 DEBUGFS_REG32(SOR_DP_SPARE1),
1207 DEBUGFS_REG32(SOR_DP_AUDIO_CTRL),
1208 DEBUGFS_REG32(SOR_DP_AUDIO_HBLANK_SYMBOLS),
1209 DEBUGFS_REG32(SOR_DP_AUDIO_VBLANK_SYMBOLS),
1210 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_HEADER),
1211 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK0),
1212 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK1),
1213 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK2),
1214 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK3),
1215 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK4),
1216 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK5),
1217 DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK6),
1218 DEBUGFS_REG32(SOR_DP_TPG),
1219 DEBUGFS_REG32(SOR_DP_TPG_CONFIG),
1220 DEBUGFS_REG32(SOR_DP_LQ_CSTM0),
1221 DEBUGFS_REG32(SOR_DP_LQ_CSTM1),
1222 DEBUGFS_REG32(SOR_DP_LQ_CSTM2),
1223};
1224
Thierry Redingdab16332015-01-26 16:04:08 +01001225static int tegra_sor_show_regs(struct seq_file *s, void *data)
1226{
1227 struct drm_info_node *node = s->private;
1228 struct tegra_sor *sor = node->info_ent->data;
Thierry Reding850bab42015-07-29 17:58:41 +02001229 struct drm_crtc *crtc = sor->output.encoder.crtc;
1230 struct drm_device *drm = node->minor->dev;
Thierry Reding062f5b2c2017-11-10 12:21:51 +01001231 unsigned int i;
Thierry Reding850bab42015-07-29 17:58:41 +02001232 int err = 0;
1233
1234 drm_modeset_lock_all(drm);
1235
1236 if (!crtc || !crtc->state->active) {
1237 err = -EBUSY;
1238 goto unlock;
1239 }
Thierry Redingdab16332015-01-26 16:04:08 +01001240
Thierry Reding062f5b2c2017-11-10 12:21:51 +01001241 for (i = 0; i < ARRAY_SIZE(tegra_sor_regs); i++) {
1242 unsigned int offset = tegra_sor_regs[i].offset;
Thierry Redingdab16332015-01-26 16:04:08 +01001243
Thierry Reding062f5b2c2017-11-10 12:21:51 +01001244 seq_printf(s, "%-38s %#05x %08x\n", tegra_sor_regs[i].name,
1245 offset, tegra_sor_readl(sor, offset));
1246 }
Thierry Redingdab16332015-01-26 16:04:08 +01001247
Thierry Reding850bab42015-07-29 17:58:41 +02001248unlock:
1249 drm_modeset_unlock_all(drm);
1250 return err;
Thierry Redingdab16332015-01-26 16:04:08 +01001251}
1252
1253static const struct drm_info_list debugfs_files[] = {
Thierry Reding530239a2015-08-06 11:04:54 +02001254 { "crc", tegra_sor_show_crc, 0, NULL },
Thierry Redingdab16332015-01-26 16:04:08 +01001255 { "regs", tegra_sor_show_regs, 0, NULL },
1256};
1257
Thierry Reding5b8e0432017-11-08 13:20:01 +01001258static int tegra_sor_late_register(struct drm_connector *connector)
Thierry Reding6fad8f62014-11-28 15:41:34 +01001259{
Thierry Reding5b8e0432017-11-08 13:20:01 +01001260 struct tegra_output *output = connector_to_output(connector);
1261 unsigned int i, count = ARRAY_SIZE(debugfs_files);
1262 struct drm_minor *minor = connector->dev->primary;
1263 struct dentry *root = connector->debugfs_entry;
1264 struct tegra_sor *sor = to_sor(output);
Thierry Reding530239a2015-08-06 11:04:54 +02001265 int err;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001266
Thierry Redingdab16332015-01-26 16:04:08 +01001267 sor->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1268 GFP_KERNEL);
Thierry Reding5b8e0432017-11-08 13:20:01 +01001269 if (!sor->debugfs_files)
1270 return -ENOMEM;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001271
Thierry Reding5b8e0432017-11-08 13:20:01 +01001272 for (i = 0; i < count; i++)
Thierry Redingdab16332015-01-26 16:04:08 +01001273 sor->debugfs_files[i].data = sor;
1274
Thierry Reding5b8e0432017-11-08 13:20:01 +01001275 err = drm_debugfs_create_files(sor->debugfs_files, count, root, minor);
Thierry Redingdab16332015-01-26 16:04:08 +01001276 if (err < 0)
1277 goto free;
1278
Thierry Reding530239a2015-08-06 11:04:54 +02001279 return 0;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001280
Thierry Redingdab16332015-01-26 16:04:08 +01001281free:
1282 kfree(sor->debugfs_files);
1283 sor->debugfs_files = NULL;
Thierry Reding5b8e0432017-11-08 13:20:01 +01001284
Thierry Reding6fad8f62014-11-28 15:41:34 +01001285 return err;
1286}
1287
Thierry Reding5b8e0432017-11-08 13:20:01 +01001288static void tegra_sor_early_unregister(struct drm_connector *connector)
Thierry Reding6fad8f62014-11-28 15:41:34 +01001289{
Thierry Reding5b8e0432017-11-08 13:20:01 +01001290 struct tegra_output *output = connector_to_output(connector);
1291 unsigned int count = ARRAY_SIZE(debugfs_files);
1292 struct tegra_sor *sor = to_sor(output);
Thierry Redingd92e6002017-10-12 19:07:54 +02001293
Thierry Reding5b8e0432017-11-08 13:20:01 +01001294 drm_debugfs_remove_files(sor->debugfs_files, count,
1295 connector->dev->primary);
Thierry Redingdab16332015-01-26 16:04:08 +01001296 kfree(sor->debugfs_files);
Thierry Reding066d30f2015-07-03 14:16:30 +02001297 sor->debugfs_files = NULL;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001298}
1299
Thierry Redingc31efa72015-09-08 16:09:22 +02001300static void tegra_sor_connector_reset(struct drm_connector *connector)
1301{
1302 struct tegra_sor_state *state;
1303
1304 state = kzalloc(sizeof(*state), GFP_KERNEL);
1305 if (!state)
1306 return;
1307
1308 if (connector->state) {
1309 __drm_atomic_helper_connector_destroy_state(connector->state);
1310 kfree(connector->state);
1311 }
1312
1313 __drm_atomic_helper_connector_reset(connector, &state->base);
1314}
1315
Thierry Reding6fad8f62014-11-28 15:41:34 +01001316static enum drm_connector_status
1317tegra_sor_connector_detect(struct drm_connector *connector, bool force)
1318{
1319 struct tegra_output *output = connector_to_output(connector);
1320 struct tegra_sor *sor = to_sor(output);
1321
Thierry Reding9542c232015-07-08 13:39:09 +02001322 if (sor->aux)
1323 return drm_dp_aux_detect(sor->aux);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001324
Thierry Reding459cc2c2015-07-30 10:34:24 +02001325 return tegra_output_connector_detect(connector, force);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001326}
1327
Thierry Redingc31efa72015-09-08 16:09:22 +02001328static struct drm_connector_state *
1329tegra_sor_connector_duplicate_state(struct drm_connector *connector)
1330{
1331 struct tegra_sor_state *state = to_sor_state(connector->state);
1332 struct tegra_sor_state *copy;
1333
1334 copy = kmemdup(state, sizeof(*state), GFP_KERNEL);
1335 if (!copy)
1336 return NULL;
1337
1338 __drm_atomic_helper_connector_duplicate_state(connector, &copy->base);
1339
1340 return &copy->base;
1341}
1342
Thierry Reding6fad8f62014-11-28 15:41:34 +01001343static const struct drm_connector_funcs tegra_sor_connector_funcs = {
Thierry Redingc31efa72015-09-08 16:09:22 +02001344 .reset = tegra_sor_connector_reset,
Thierry Reding6fad8f62014-11-28 15:41:34 +01001345 .detect = tegra_sor_connector_detect,
1346 .fill_modes = drm_helper_probe_single_connector_modes,
1347 .destroy = tegra_output_connector_destroy,
Thierry Redingc31efa72015-09-08 16:09:22 +02001348 .atomic_duplicate_state = tegra_sor_connector_duplicate_state,
Thierry Reding4aa3df72014-11-24 16:27:13 +01001349 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
Thierry Reding5b8e0432017-11-08 13:20:01 +01001350 .late_register = tegra_sor_late_register,
1351 .early_unregister = tegra_sor_early_unregister,
Thierry Reding6fad8f62014-11-28 15:41:34 +01001352};
1353
1354static int tegra_sor_connector_get_modes(struct drm_connector *connector)
1355{
1356 struct tegra_output *output = connector_to_output(connector);
1357 struct tegra_sor *sor = to_sor(output);
1358 int err;
1359
Thierry Reding9542c232015-07-08 13:39:09 +02001360 if (sor->aux)
1361 drm_dp_aux_enable(sor->aux);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001362
1363 err = tegra_output_connector_get_modes(connector);
1364
Thierry Reding9542c232015-07-08 13:39:09 +02001365 if (sor->aux)
1366 drm_dp_aux_disable(sor->aux);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001367
1368 return err;
1369}
1370
1371static enum drm_mode_status
1372tegra_sor_connector_mode_valid(struct drm_connector *connector,
1373 struct drm_display_mode *mode)
1374{
Thierry Reding64ea25c2016-07-12 16:52:22 +02001375 /* HDMI 2.0 modes are not yet supported */
1376 if (mode->clock > 340000)
1377 return MODE_NOCLOCK;
1378
Thierry Reding6fad8f62014-11-28 15:41:34 +01001379 return MODE_OK;
1380}
1381
1382static const struct drm_connector_helper_funcs tegra_sor_connector_helper_funcs = {
1383 .get_modes = tegra_sor_connector_get_modes,
1384 .mode_valid = tegra_sor_connector_mode_valid,
Thierry Reding6fad8f62014-11-28 15:41:34 +01001385};
1386
1387static const struct drm_encoder_funcs tegra_sor_encoder_funcs = {
1388 .destroy = tegra_output_encoder_destroy,
1389};
1390
Thierry Reding850bab42015-07-29 17:58:41 +02001391static void tegra_sor_edp_disable(struct drm_encoder *encoder)
Thierry Reding6fad8f62014-11-28 15:41:34 +01001392{
Thierry Reding850bab42015-07-29 17:58:41 +02001393 struct tegra_output *output = encoder_to_output(encoder);
1394 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
1395 struct tegra_sor *sor = to_sor(output);
1396 u32 value;
1397 int err;
1398
1399 if (output->panel)
1400 drm_panel_disable(output->panel);
1401
1402 err = tegra_sor_detach(sor);
1403 if (err < 0)
1404 dev_err(sor->dev, "failed to detach SOR: %d\n", err);
1405
1406 tegra_sor_writel(sor, 0, SOR_STATE1);
1407 tegra_sor_update(sor);
1408
1409 /*
1410 * The following accesses registers of the display controller, so make
1411 * sure it's only executed when the output is attached to one.
1412 */
1413 if (dc) {
1414 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
1415 value &= ~SOR_ENABLE;
1416 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
1417
1418 tegra_dc_commit(dc);
1419 }
1420
1421 err = tegra_sor_power_down(sor);
1422 if (err < 0)
1423 dev_err(sor->dev, "failed to power down SOR: %d\n", err);
1424
Thierry Reding9542c232015-07-08 13:39:09 +02001425 if (sor->aux) {
1426 err = drm_dp_aux_disable(sor->aux);
Thierry Reding850bab42015-07-29 17:58:41 +02001427 if (err < 0)
1428 dev_err(sor->dev, "failed to disable DP: %d\n", err);
1429 }
1430
1431 err = tegra_io_rail_power_off(TEGRA_IO_RAIL_LVDS);
1432 if (err < 0)
1433 dev_err(sor->dev, "failed to power off I/O rail: %d\n", err);
1434
1435 if (output->panel)
1436 drm_panel_unprepare(output->panel);
1437
Thierry Redingaaff8bd2015-08-07 16:04:54 +02001438 pm_runtime_put(sor->dev);
Thierry Reding6fad8f62014-11-28 15:41:34 +01001439}
1440
Thierry Reding459cc2c2015-07-30 10:34:24 +02001441#if 0
1442static int calc_h_ref_to_sync(const struct drm_display_mode *mode,
1443 unsigned int *value)
1444{
1445 unsigned int hfp, hsw, hbp, a = 0, b;
1446
1447 hfp = mode->hsync_start - mode->hdisplay;
1448 hsw = mode->hsync_end - mode->hsync_start;
1449 hbp = mode->htotal - mode->hsync_end;
1450
1451 pr_info("hfp: %u, hsw: %u, hbp: %u\n", hfp, hsw, hbp);
1452
1453 b = hfp - 1;
1454
1455 pr_info("a: %u, b: %u\n", a, b);
1456 pr_info("a + hsw + hbp = %u\n", a + hsw + hbp);
1457
1458 if (a + hsw + hbp <= 11) {
1459 a = 1 + 11 - hsw - hbp;
1460 pr_info("a: %u\n", a);
1461 }
1462
1463 if (a > b)
1464 return -EINVAL;
1465
1466 if (hsw < 1)
1467 return -EINVAL;
1468
1469 if (mode->hdisplay < 16)
1470 return -EINVAL;
1471
1472 if (value) {
1473 if (b > a && a % 2)
1474 *value = a + 1;
1475 else
1476 *value = a;
1477 }
1478
1479 return 0;
1480}
1481#endif
1482
Thierry Reding850bab42015-07-29 17:58:41 +02001483static void tegra_sor_edp_enable(struct drm_encoder *encoder)
Thierry Reding6fad8f62014-11-28 15:41:34 +01001484{
Thierry Reding850bab42015-07-29 17:58:41 +02001485 struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
Thierry Reding6fad8f62014-11-28 15:41:34 +01001486 struct tegra_output *output = encoder_to_output(encoder);
1487 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001488 struct tegra_sor *sor = to_sor(output);
Thierry Reding34fa1832014-06-05 16:31:10 +02001489 struct tegra_sor_config config;
Thierry Redingc31efa72015-09-08 16:09:22 +02001490 struct tegra_sor_state *state;
Thierry Reding34fa1832014-06-05 16:31:10 +02001491 struct drm_dp_link link;
Thierry Reding01b9bea2015-11-11 17:15:29 +01001492 u8 rate, lanes;
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001493 unsigned int i;
Thierry Reding86f5c522014-03-26 11:13:16 +01001494 int err = 0;
Thierry Reding28fe2072015-01-26 16:02:48 +01001495 u32 value;
Thierry Reding86f5c522014-03-26 11:13:16 +01001496
Thierry Redingc31efa72015-09-08 16:09:22 +02001497 state = to_sor_state(output->connector.state);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001498
Thierry Redingaaff8bd2015-08-07 16:04:54 +02001499 pm_runtime_get_sync(sor->dev);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001500
Thierry Reding6fad8f62014-11-28 15:41:34 +01001501 if (output->panel)
1502 drm_panel_prepare(output->panel);
1503
Thierry Reding01b9bea2015-11-11 17:15:29 +01001504 err = drm_dp_aux_enable(sor->aux);
1505 if (err < 0)
1506 dev_err(sor->dev, "failed to enable DP: %d\n", err);
Thierry Reding34fa1832014-06-05 16:31:10 +02001507
Thierry Reding01b9bea2015-11-11 17:15:29 +01001508 err = drm_dp_link_probe(sor->aux, &link);
1509 if (err < 0) {
1510 dev_err(sor->dev, "failed to probe eDP link: %d\n", err);
1511 return;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001512 }
1513
Thierry Reding25bb2ce2015-08-03 14:23:29 +02001514 /* switch to safe parent clock */
1515 err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001516 if (err < 0)
1517 dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
1518
Thierry Reding34fa1832014-06-05 16:31:10 +02001519 memset(&config, 0, sizeof(config));
Thierry Redingc31efa72015-09-08 16:09:22 +02001520 config.bits_per_pixel = state->bpc * 3;
Thierry Reding34fa1832014-06-05 16:31:10 +02001521
Thierry Redinga1983592015-07-21 16:46:52 +02001522 err = tegra_sor_compute_config(sor, mode, &config, &link);
Thierry Reding34fa1832014-06-05 16:31:10 +02001523 if (err < 0)
Thierry Redinga1983592015-07-21 16:46:52 +02001524 dev_err(sor->dev, "failed to compute configuration: %d\n", err);
Thierry Reding34fa1832014-06-05 16:31:10 +02001525
Thierry Reding6b6b6042013-11-15 16:06:05 +01001526 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
1527 value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
1528 value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
1529 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
1530
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001531 value = tegra_sor_readl(sor, SOR_PLL2);
1532 value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
1533 tegra_sor_writel(sor, value, SOR_PLL2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001534 usleep_range(20, 100);
1535
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001536 value = tegra_sor_readl(sor, SOR_PLL3);
1537 value |= SOR_PLL3_PLL_VDD_MODE_3V3;
1538 tegra_sor_writel(sor, value, SOR_PLL3);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001539
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001540 value = SOR_PLL0_ICHPMP(0xf) | SOR_PLL0_VCOCAP_RST |
1541 SOR_PLL0_PLLREG_LEVEL_V45 | SOR_PLL0_RESISTOR_EXT;
1542 tegra_sor_writel(sor, value, SOR_PLL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001543
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001544 value = tegra_sor_readl(sor, SOR_PLL2);
1545 value |= SOR_PLL2_SEQ_PLLCAPPD;
1546 value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1547 value |= SOR_PLL2_LVDS_ENABLE;
1548 tegra_sor_writel(sor, value, SOR_PLL2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001549
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001550 value = SOR_PLL1_TERM_COMPOUT | SOR_PLL1_TMDS_TERM;
1551 tegra_sor_writel(sor, value, SOR_PLL1);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001552
1553 while (true) {
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001554 value = tegra_sor_readl(sor, SOR_PLL2);
1555 if ((value & SOR_PLL2_SEQ_PLLCAPPD_ENFORCE) == 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001556 break;
1557
1558 usleep_range(250, 1000);
1559 }
1560
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001561 value = tegra_sor_readl(sor, SOR_PLL2);
1562 value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
1563 value &= ~SOR_PLL2_PORT_POWERDOWN;
1564 tegra_sor_writel(sor, value, SOR_PLL2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001565
1566 /*
1567 * power up
1568 */
1569
1570 /* set safe link bandwidth (1.62 Gbps) */
1571 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
1572 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
1573 value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G1_62;
1574 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
1575
1576 /* step 1 */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001577 value = tegra_sor_readl(sor, SOR_PLL2);
1578 value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE | SOR_PLL2_PORT_POWERDOWN |
1579 SOR_PLL2_BANDGAP_POWERDOWN;
1580 tegra_sor_writel(sor, value, SOR_PLL2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001581
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001582 value = tegra_sor_readl(sor, SOR_PLL0);
1583 value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
1584 tegra_sor_writel(sor, value, SOR_PLL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001585
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001586 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001587 value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001588 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001589
1590 /* step 2 */
1591 err = tegra_io_rail_power_on(TEGRA_IO_RAIL_LVDS);
Thierry Reding850bab42015-07-29 17:58:41 +02001592 if (err < 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001593 dev_err(sor->dev, "failed to power on I/O rail: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001594
1595 usleep_range(5, 100);
1596
1597 /* step 3 */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001598 value = tegra_sor_readl(sor, SOR_PLL2);
1599 value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
1600 tegra_sor_writel(sor, value, SOR_PLL2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001601
1602 usleep_range(20, 100);
1603
1604 /* step 4 */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001605 value = tegra_sor_readl(sor, SOR_PLL0);
1606 value &= ~SOR_PLL0_VCOPD;
1607 value &= ~SOR_PLL0_PWR;
1608 tegra_sor_writel(sor, value, SOR_PLL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001609
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001610 value = tegra_sor_readl(sor, SOR_PLL2);
1611 value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
1612 tegra_sor_writel(sor, value, SOR_PLL2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001613
1614 usleep_range(200, 1000);
1615
1616 /* step 5 */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001617 value = tegra_sor_readl(sor, SOR_PLL2);
1618 value &= ~SOR_PLL2_PORT_POWERDOWN;
1619 tegra_sor_writel(sor, value, SOR_PLL2);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001620
Thierry Reding30b49432015-08-03 15:50:32 +02001621 /* XXX not in TRM */
1622 for (value = 0, i = 0; i < 5; i++)
1623 value |= SOR_XBAR_CTRL_LINK0_XSEL(i, sor->soc->xbar_cfg[i]) |
1624 SOR_XBAR_CTRL_LINK1_XSEL(i, i);
1625
1626 tegra_sor_writel(sor, 0x00000000, SOR_XBAR_POL);
1627 tegra_sor_writel(sor, value, SOR_XBAR_CTRL);
1628
Thierry Reding25bb2ce2015-08-03 14:23:29 +02001629 /* switch to DP parent clock */
1630 err = tegra_sor_set_parent_clock(sor, sor->clk_dp);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001631 if (err < 0)
Thierry Reding25bb2ce2015-08-03 14:23:29 +02001632 dev_err(sor->dev, "failed to set parent clock: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001633
Thierry Reding899451b2014-06-05 16:19:48 +02001634 /* power DP lanes */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001635 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
Thierry Reding899451b2014-06-05 16:19:48 +02001636
1637 if (link.num_lanes <= 2)
1638 value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2);
1639 else
1640 value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2;
1641
1642 if (link.num_lanes <= 1)
1643 value &= ~SOR_DP_PADCTL_PD_TXD_1;
1644 else
1645 value |= SOR_DP_PADCTL_PD_TXD_1;
1646
1647 if (link.num_lanes == 0)
1648 value &= ~SOR_DP_PADCTL_PD_TXD_0;
1649 else
1650 value |= SOR_DP_PADCTL_PD_TXD_0;
1651
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001652 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001653
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001654 value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001655 value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
Thierry Reding0c90a182014-06-05 16:29:46 +02001656 value |= SOR_DP_LINKCTL_LANE_COUNT(link.num_lanes);
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001657 tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001658
1659 /* start lane sequencer */
1660 value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
1661 SOR_LANE_SEQ_CTL_POWER_STATE_UP;
1662 tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
1663
1664 while (true) {
1665 value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
1666 if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
1667 break;
1668
1669 usleep_range(250, 1000);
1670 }
1671
Thierry Redinga4263fe2014-06-05 16:16:23 +02001672 /* set link bandwidth */
Thierry Reding6b6b6042013-11-15 16:06:05 +01001673 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
1674 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
Thierry Redinga4263fe2014-06-05 16:16:23 +02001675 value |= drm_dp_link_rate_to_bw_code(link.rate) << 2;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001676 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
1677
Thierry Reding402f6bc2015-07-21 16:48:19 +02001678 tegra_sor_apply_config(sor, &config);
1679
1680 /* enable link */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001681 value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001682 value |= SOR_DP_LINKCTL_ENABLE;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001683 value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001684 tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001685
1686 for (i = 0, value = 0; i < 4; i++) {
1687 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
1688 SOR_DP_TPG_SCRAMBLER_GALIOS |
1689 SOR_DP_TPG_PATTERN_NONE;
1690 value = (value << 8) | lane;
1691 }
1692
1693 tegra_sor_writel(sor, value, SOR_DP_TPG);
1694
Thierry Reding6b6b6042013-11-15 16:06:05 +01001695 /* enable pad calibration logic */
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001696 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001697 value |= SOR_DP_PADCTL_PAD_CAL_PD;
Thierry Redinga9a9e4f2015-04-27 15:01:14 +02001698 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001699
Thierry Reding01b9bea2015-11-11 17:15:29 +01001700 err = drm_dp_link_probe(sor->aux, &link);
1701 if (err < 0)
1702 dev_err(sor->dev, "failed to probe eDP link: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001703
Thierry Reding01b9bea2015-11-11 17:15:29 +01001704 err = drm_dp_link_power_up(sor->aux, &link);
1705 if (err < 0)
1706 dev_err(sor->dev, "failed to power up eDP link: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001707
Thierry Reding01b9bea2015-11-11 17:15:29 +01001708 err = drm_dp_link_configure(sor->aux, &link);
1709 if (err < 0)
1710 dev_err(sor->dev, "failed to configure eDP link: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001711
Thierry Reding01b9bea2015-11-11 17:15:29 +01001712 rate = drm_dp_link_rate_to_bw_code(link.rate);
1713 lanes = link.num_lanes;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001714
Thierry Reding01b9bea2015-11-11 17:15:29 +01001715 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
1716 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
1717 value |= SOR_CLK_CNTRL_DP_LINK_SPEED(rate);
1718 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001719
Thierry Reding01b9bea2015-11-11 17:15:29 +01001720 value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
1721 value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
1722 value |= SOR_DP_LINKCTL_LANE_COUNT(lanes);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001723
Thierry Reding01b9bea2015-11-11 17:15:29 +01001724 if (link.capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
1725 value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001726
Thierry Reding01b9bea2015-11-11 17:15:29 +01001727 tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001728
Thierry Reding01b9bea2015-11-11 17:15:29 +01001729 /* disable training pattern generator */
Thierry Reding6b6b6042013-11-15 16:06:05 +01001730
Thierry Reding01b9bea2015-11-11 17:15:29 +01001731 for (i = 0; i < link.num_lanes; i++) {
1732 unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
1733 SOR_DP_TPG_SCRAMBLER_GALIOS |
1734 SOR_DP_TPG_PATTERN_NONE;
1735 value = (value << 8) | lane;
Thierry Reding6b6b6042013-11-15 16:06:05 +01001736 }
1737
Thierry Reding01b9bea2015-11-11 17:15:29 +01001738 tegra_sor_writel(sor, value, SOR_DP_TPG);
1739
1740 err = tegra_sor_dp_train_fast(sor, &link);
1741 if (err < 0)
1742 dev_err(sor->dev, "DP fast link training failed: %d\n", err);
1743
1744 dev_dbg(sor->dev, "fast link training succeeded\n");
1745
Thierry Reding6b6b6042013-11-15 16:06:05 +01001746 err = tegra_sor_power_up(sor, 250);
Thierry Reding850bab42015-07-29 17:58:41 +02001747 if (err < 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001748 dev_err(sor->dev, "failed to power up SOR: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001749
Thierry Reding6b6b6042013-11-15 16:06:05 +01001750 /* CSTM (LVDS, link A/B, upper) */
Stéphane Marchesin143b1df2014-05-22 20:32:47 -07001751 value = SOR_CSTM_LVDS | SOR_CSTM_LINK_ACT_A | SOR_CSTM_LINK_ACT_B |
Thierry Reding6b6b6042013-11-15 16:06:05 +01001752 SOR_CSTM_UPPER;
1753 tegra_sor_writel(sor, value, SOR_CSTM);
1754
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001755 /* use DP-A protocol */
1756 value = tegra_sor_readl(sor, SOR_STATE1);
1757 value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
1758 value |= SOR_STATE_ASY_PROTOCOL_DP_A;
1759 tegra_sor_writel(sor, value, SOR_STATE1);
1760
Thierry Redingc31efa72015-09-08 16:09:22 +02001761 tegra_sor_mode_set(sor, mode, state);
Thierry Reding2bd1dd32015-08-03 15:46:15 +02001762
Thierry Reding6b6b6042013-11-15 16:06:05 +01001763 /* PWM setup */
1764 err = tegra_sor_setup_pwm(sor, 250);
Thierry Reding850bab42015-07-29 17:58:41 +02001765 if (err < 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001766 dev_err(sor->dev, "failed to setup PWM: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001767
Thierry Reding666cb872014-12-08 16:32:47 +01001768 tegra_sor_update(sor);
1769
Thierry Reding6b6b6042013-11-15 16:06:05 +01001770 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
1771 value |= SOR_ENABLE;
1772 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
1773
Thierry Reding666cb872014-12-08 16:32:47 +01001774 tegra_dc_commit(dc);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001775
1776 err = tegra_sor_attach(sor);
Thierry Reding850bab42015-07-29 17:58:41 +02001777 if (err < 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001778 dev_err(sor->dev, "failed to attach SOR: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001779
1780 err = tegra_sor_wakeup(sor);
Thierry Reding850bab42015-07-29 17:58:41 +02001781 if (err < 0)
Thierry Reding6b6b6042013-11-15 16:06:05 +01001782 dev_err(sor->dev, "failed to enable DC: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001783
Thierry Reding6fad8f62014-11-28 15:41:34 +01001784 if (output->panel)
1785 drm_panel_enable(output->panel);
Thierry Reding6b6b6042013-11-15 16:06:05 +01001786}
1787
Thierry Reding82f15112014-12-08 17:26:46 +01001788static int
1789tegra_sor_encoder_atomic_check(struct drm_encoder *encoder,
1790 struct drm_crtc_state *crtc_state,
1791 struct drm_connector_state *conn_state)
1792{
1793 struct tegra_output *output = encoder_to_output(encoder);
Thierry Redingc31efa72015-09-08 16:09:22 +02001794 struct tegra_sor_state *state = to_sor_state(conn_state);
Thierry Reding82f15112014-12-08 17:26:46 +01001795 struct tegra_dc *dc = to_tegra_dc(conn_state->crtc);
1796 unsigned long pclk = crtc_state->mode.clock * 1000;
1797 struct tegra_sor *sor = to_sor(output);
Thierry Redingc31efa72015-09-08 16:09:22 +02001798 struct drm_display_info *info;
Thierry Reding82f15112014-12-08 17:26:46 +01001799 int err;
1800
Thierry Redingc31efa72015-09-08 16:09:22 +02001801 info = &output->connector.display_info;
1802
Thierry Reding82f15112014-12-08 17:26:46 +01001803 err = tegra_dc_state_setup_clock(dc, crtc_state, sor->clk_parent,
1804 pclk, 0);
1805 if (err < 0) {
1806 dev_err(output->dev, "failed to setup CRTC state: %d\n", err);
1807 return err;
1808 }
1809
Thierry Redingc31efa72015-09-08 16:09:22 +02001810 switch (info->bpc) {
1811 case 8:
1812 case 6:
1813 state->bpc = info->bpc;
1814 break;
1815
1816 default:
1817 DRM_DEBUG_KMS("%u bits-per-color not supported\n", info->bpc);
1818 state->bpc = 8;
1819 break;
1820 }
1821
Thierry Reding82f15112014-12-08 17:26:46 +01001822 return 0;
1823}
1824
Thierry Reding459cc2c2015-07-30 10:34:24 +02001825static const struct drm_encoder_helper_funcs tegra_sor_edp_helpers = {
Thierry Reding850bab42015-07-29 17:58:41 +02001826 .disable = tegra_sor_edp_disable,
1827 .enable = tegra_sor_edp_enable,
Thierry Reding82f15112014-12-08 17:26:46 +01001828 .atomic_check = tegra_sor_encoder_atomic_check,
Thierry Reding6b6b6042013-11-15 16:06:05 +01001829};
1830
Thierry Reding459cc2c2015-07-30 10:34:24 +02001831static inline u32 tegra_sor_hdmi_subpack(const u8 *ptr, size_t size)
1832{
1833 u32 value = 0;
1834 size_t i;
1835
1836 for (i = size; i > 0; i--)
1837 value = (value << 8) | ptr[i - 1];
1838
1839 return value;
1840}
1841
1842static void tegra_sor_hdmi_write_infopack(struct tegra_sor *sor,
1843 const void *data, size_t size)
1844{
1845 const u8 *ptr = data;
1846 unsigned long offset;
1847 size_t i, j;
1848 u32 value;
1849
1850 switch (ptr[0]) {
1851 case HDMI_INFOFRAME_TYPE_AVI:
1852 offset = SOR_HDMI_AVI_INFOFRAME_HEADER;
1853 break;
1854
1855 case HDMI_INFOFRAME_TYPE_AUDIO:
1856 offset = SOR_HDMI_AUDIO_INFOFRAME_HEADER;
1857 break;
1858
1859 case HDMI_INFOFRAME_TYPE_VENDOR:
1860 offset = SOR_HDMI_VSI_INFOFRAME_HEADER;
1861 break;
1862
1863 default:
1864 dev_err(sor->dev, "unsupported infoframe type: %02x\n",
1865 ptr[0]);
1866 return;
1867 }
1868
1869 value = INFOFRAME_HEADER_TYPE(ptr[0]) |
1870 INFOFRAME_HEADER_VERSION(ptr[1]) |
1871 INFOFRAME_HEADER_LEN(ptr[2]);
1872 tegra_sor_writel(sor, value, offset);
1873 offset++;
1874
1875 /*
1876 * Each subpack contains 7 bytes, divided into:
1877 * - subpack_low: bytes 0 - 3
1878 * - subpack_high: bytes 4 - 6 (with byte 7 padded to 0x00)
1879 */
1880 for (i = 3, j = 0; i < size; i += 7, j += 8) {
1881 size_t rem = size - i, num = min_t(size_t, rem, 4);
1882
1883 value = tegra_sor_hdmi_subpack(&ptr[i], num);
1884 tegra_sor_writel(sor, value, offset++);
1885
1886 num = min_t(size_t, rem - num, 3);
1887
1888 value = tegra_sor_hdmi_subpack(&ptr[i + 4], num);
1889 tegra_sor_writel(sor, value, offset++);
1890 }
1891}
1892
1893static int
1894tegra_sor_hdmi_setup_avi_infoframe(struct tegra_sor *sor,
1895 const struct drm_display_mode *mode)
1896{
1897 u8 buffer[HDMI_INFOFRAME_SIZE(AVI)];
1898 struct hdmi_avi_infoframe frame;
1899 u32 value;
1900 int err;
1901
1902 /* disable AVI infoframe */
1903 value = tegra_sor_readl(sor, SOR_HDMI_AVI_INFOFRAME_CTRL);
1904 value &= ~INFOFRAME_CTRL_SINGLE;
1905 value &= ~INFOFRAME_CTRL_OTHER;
1906 value &= ~INFOFRAME_CTRL_ENABLE;
1907 tegra_sor_writel(sor, value, SOR_HDMI_AVI_INFOFRAME_CTRL);
1908
Shashank Sharma0c1f5282017-07-13 21:03:07 +05301909 err = drm_hdmi_avi_infoframe_from_display_mode(&frame, mode, false);
Thierry Reding459cc2c2015-07-30 10:34:24 +02001910 if (err < 0) {
1911 dev_err(sor->dev, "failed to setup AVI infoframe: %d\n", err);
1912 return err;
1913 }
1914
1915 err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
1916 if (err < 0) {
1917 dev_err(sor->dev, "failed to pack AVI infoframe: %d\n", err);
1918 return err;
1919 }
1920
1921 tegra_sor_hdmi_write_infopack(sor, buffer, err);
1922
1923 /* enable AVI infoframe */
1924 value = tegra_sor_readl(sor, SOR_HDMI_AVI_INFOFRAME_CTRL);
1925 value |= INFOFRAME_CTRL_CHECKSUM_ENABLE;
1926 value |= INFOFRAME_CTRL_ENABLE;
1927 tegra_sor_writel(sor, value, SOR_HDMI_AVI_INFOFRAME_CTRL);
1928
1929 return 0;
1930}
1931
1932static void tegra_sor_hdmi_disable_audio_infoframe(struct tegra_sor *sor)
1933{
1934 u32 value;
1935
1936 value = tegra_sor_readl(sor, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
1937 value &= ~INFOFRAME_CTRL_ENABLE;
1938 tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
1939}
1940
1941static struct tegra_sor_hdmi_settings *
1942tegra_sor_hdmi_find_settings(struct tegra_sor *sor, unsigned long frequency)
1943{
1944 unsigned int i;
1945
1946 for (i = 0; i < sor->num_settings; i++)
1947 if (frequency <= sor->settings[i].frequency)
1948 return &sor->settings[i];
1949
1950 return NULL;
1951}
1952
1953static void tegra_sor_hdmi_disable(struct drm_encoder *encoder)
1954{
1955 struct tegra_output *output = encoder_to_output(encoder);
1956 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
1957 struct tegra_sor *sor = to_sor(output);
1958 u32 value;
1959 int err;
1960
1961 err = tegra_sor_detach(sor);
1962 if (err < 0)
1963 dev_err(sor->dev, "failed to detach SOR: %d\n", err);
1964
1965 tegra_sor_writel(sor, 0, SOR_STATE1);
1966 tegra_sor_update(sor);
1967
1968 /* disable display to SOR clock */
1969 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
1970 value &= ~SOR1_TIMING_CYA;
1971 value &= ~SOR1_ENABLE;
1972 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
1973
1974 tegra_dc_commit(dc);
1975
1976 err = tegra_sor_power_down(sor);
1977 if (err < 0)
1978 dev_err(sor->dev, "failed to power down SOR: %d\n", err);
1979
1980 err = tegra_io_rail_power_off(TEGRA_IO_RAIL_HDMI);
1981 if (err < 0)
1982 dev_err(sor->dev, "failed to power off HDMI rail: %d\n", err);
1983
Thierry Redingaaff8bd2015-08-07 16:04:54 +02001984 pm_runtime_put(sor->dev);
Thierry Reding459cc2c2015-07-30 10:34:24 +02001985}
1986
1987static void tegra_sor_hdmi_enable(struct drm_encoder *encoder)
1988{
1989 struct tegra_output *output = encoder_to_output(encoder);
1990 unsigned int h_ref_to_sync = 1, pulse_start, max_ac;
1991 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
Thierry Reding459cc2c2015-07-30 10:34:24 +02001992 struct tegra_sor_hdmi_settings *settings;
1993 struct tegra_sor *sor = to_sor(output);
Thierry Redingc31efa72015-09-08 16:09:22 +02001994 struct tegra_sor_state *state;
Thierry Reding459cc2c2015-07-30 10:34:24 +02001995 struct drm_display_mode *mode;
Thierry Reding30b49432015-08-03 15:50:32 +02001996 unsigned int div, i;
Thierry Reding459cc2c2015-07-30 10:34:24 +02001997 u32 value;
1998 int err;
1999
Thierry Redingc31efa72015-09-08 16:09:22 +02002000 state = to_sor_state(output->connector.state);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002001 mode = &encoder->crtc->state->adjusted_mode;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002002
Thierry Redingaaff8bd2015-08-07 16:04:54 +02002003 pm_runtime_get_sync(sor->dev);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002004
Thierry Reding25bb2ce2015-08-03 14:23:29 +02002005 /* switch to safe parent clock */
2006 err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
Thierry Redinge1335e22017-10-12 17:53:11 +02002007 if (err < 0) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002008 dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
Thierry Redinge1335e22017-10-12 17:53:11 +02002009 return;
2010 }
Thierry Reding459cc2c2015-07-30 10:34:24 +02002011
2012 div = clk_get_rate(sor->clk) / 1000000 * 4;
2013
2014 err = tegra_io_rail_power_on(TEGRA_IO_RAIL_HDMI);
2015 if (err < 0)
2016 dev_err(sor->dev, "failed to power on HDMI rail: %d\n", err);
2017
2018 usleep_range(20, 100);
2019
2020 value = tegra_sor_readl(sor, SOR_PLL2);
2021 value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
2022 tegra_sor_writel(sor, value, SOR_PLL2);
2023
2024 usleep_range(20, 100);
2025
2026 value = tegra_sor_readl(sor, SOR_PLL3);
2027 value &= ~SOR_PLL3_PLL_VDD_MODE_3V3;
2028 tegra_sor_writel(sor, value, SOR_PLL3);
2029
2030 value = tegra_sor_readl(sor, SOR_PLL0);
2031 value &= ~SOR_PLL0_VCOPD;
2032 value &= ~SOR_PLL0_PWR;
2033 tegra_sor_writel(sor, value, SOR_PLL0);
2034
2035 value = tegra_sor_readl(sor, SOR_PLL2);
2036 value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
2037 tegra_sor_writel(sor, value, SOR_PLL2);
2038
2039 usleep_range(200, 400);
2040
2041 value = tegra_sor_readl(sor, SOR_PLL2);
2042 value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
2043 value &= ~SOR_PLL2_PORT_POWERDOWN;
2044 tegra_sor_writel(sor, value, SOR_PLL2);
2045
2046 usleep_range(20, 100);
2047
2048 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
2049 value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
2050 SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2;
2051 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
2052
2053 while (true) {
2054 value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
2055 if ((value & SOR_LANE_SEQ_CTL_STATE_BUSY) == 0)
2056 break;
2057
2058 usleep_range(250, 1000);
2059 }
2060
2061 value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
2062 SOR_LANE_SEQ_CTL_POWER_STATE_UP | SOR_LANE_SEQ_CTL_DELAY(5);
2063 tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
2064
2065 while (true) {
2066 value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
2067 if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
2068 break;
2069
2070 usleep_range(250, 1000);
2071 }
2072
2073 value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
2074 value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
2075 value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
2076
2077 if (mode->clock < 340000)
2078 value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G2_70;
2079 else
2080 value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G5_40;
2081
2082 value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK;
2083 tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
2084
2085 value = tegra_sor_readl(sor, SOR_DP_SPARE0);
2086 value |= SOR_DP_SPARE_DISP_VIDEO_PREAMBLE;
2087 value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
2088 value |= SOR_DP_SPARE_SEQ_ENABLE;
2089 tegra_sor_writel(sor, value, SOR_DP_SPARE0);
2090
2091 value = SOR_SEQ_CTL_PU_PC(0) | SOR_SEQ_CTL_PU_PC_ALT(0) |
2092 SOR_SEQ_CTL_PD_PC(8) | SOR_SEQ_CTL_PD_PC_ALT(8);
2093 tegra_sor_writel(sor, value, SOR_SEQ_CTL);
2094
2095 value = SOR_SEQ_INST_DRIVE_PWM_OUT_LO | SOR_SEQ_INST_HALT |
2096 SOR_SEQ_INST_WAIT_VSYNC | SOR_SEQ_INST_WAIT(1);
2097 tegra_sor_writel(sor, value, SOR_SEQ_INST(0));
2098 tegra_sor_writel(sor, value, SOR_SEQ_INST(8));
2099
2100 /* program the reference clock */
2101 value = SOR_REFCLK_DIV_INT(div) | SOR_REFCLK_DIV_FRAC(div);
2102 tegra_sor_writel(sor, value, SOR_REFCLK);
2103
Thierry Reding30b49432015-08-03 15:50:32 +02002104 /* XXX not in TRM */
2105 for (value = 0, i = 0; i < 5; i++)
2106 value |= SOR_XBAR_CTRL_LINK0_XSEL(i, sor->soc->xbar_cfg[i]) |
2107 SOR_XBAR_CTRL_LINK1_XSEL(i, i);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002108
2109 tegra_sor_writel(sor, 0x00000000, SOR_XBAR_POL);
Thierry Reding30b49432015-08-03 15:50:32 +02002110 tegra_sor_writel(sor, value, SOR_XBAR_CTRL);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002111
Thierry Reding25bb2ce2015-08-03 14:23:29 +02002112 /* switch to parent clock */
Thierry Redinge1335e22017-10-12 17:53:11 +02002113 err = clk_set_parent(sor->clk, sor->clk_parent);
2114 if (err < 0) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002115 dev_err(sor->dev, "failed to set parent clock: %d\n", err);
Thierry Redinge1335e22017-10-12 17:53:11 +02002116 return;
2117 }
2118
2119 err = tegra_sor_set_parent_clock(sor, sor->clk_pad);
2120 if (err < 0) {
2121 dev_err(sor->dev, "failed to set pad clock: %d\n", err);
2122 return;
2123 }
Thierry Reding459cc2c2015-07-30 10:34:24 +02002124
2125 value = SOR_INPUT_CONTROL_HDMI_SRC_SELECT(dc->pipe);
2126
2127 /* XXX is this the proper check? */
2128 if (mode->clock < 75000)
2129 value |= SOR_INPUT_CONTROL_ARM_VIDEO_RANGE_LIMITED;
2130
2131 tegra_sor_writel(sor, value, SOR_INPUT_CONTROL);
2132
2133 max_ac = ((mode->htotal - mode->hdisplay) - SOR_REKEY - 18) / 32;
2134
2135 value = SOR_HDMI_CTRL_ENABLE | SOR_HDMI_CTRL_MAX_AC_PACKET(max_ac) |
2136 SOR_HDMI_CTRL_AUDIO_LAYOUT | SOR_HDMI_CTRL_REKEY(SOR_REKEY);
2137 tegra_sor_writel(sor, value, SOR_HDMI_CTRL);
2138
2139 /* H_PULSE2 setup */
2140 pulse_start = h_ref_to_sync + (mode->hsync_end - mode->hsync_start) +
2141 (mode->htotal - mode->hsync_end) - 10;
2142
2143 value = PULSE_LAST_END_A | PULSE_QUAL_VACTIVE |
2144 PULSE_POLARITY_HIGH | PULSE_MODE_NORMAL;
2145 tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_CONTROL);
2146
2147 value = PULSE_END(pulse_start + 8) | PULSE_START(pulse_start);
2148 tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_POSITION_A);
2149
2150 value = tegra_dc_readl(dc, DC_DISP_DISP_SIGNAL_OPTIONS0);
2151 value |= H_PULSE2_ENABLE;
2152 tegra_dc_writel(dc, value, DC_DISP_DISP_SIGNAL_OPTIONS0);
2153
2154 /* infoframe setup */
2155 err = tegra_sor_hdmi_setup_avi_infoframe(sor, mode);
2156 if (err < 0)
2157 dev_err(sor->dev, "failed to setup AVI infoframe: %d\n", err);
2158
2159 /* XXX HDMI audio support not implemented yet */
2160 tegra_sor_hdmi_disable_audio_infoframe(sor);
2161
2162 /* use single TMDS protocol */
2163 value = tegra_sor_readl(sor, SOR_STATE1);
2164 value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
2165 value |= SOR_STATE_ASY_PROTOCOL_SINGLE_TMDS_A;
2166 tegra_sor_writel(sor, value, SOR_STATE1);
2167
2168 /* power up pad calibration */
2169 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
2170 value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
2171 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
2172
2173 /* production settings */
2174 settings = tegra_sor_hdmi_find_settings(sor, mode->clock * 1000);
Dan Carpenterdb8b42f2015-08-17 17:37:03 +03002175 if (!settings) {
2176 dev_err(sor->dev, "no settings for pixel clock %d Hz\n",
2177 mode->clock * 1000);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002178 return;
2179 }
2180
2181 value = tegra_sor_readl(sor, SOR_PLL0);
2182 value &= ~SOR_PLL0_ICHPMP_MASK;
2183 value &= ~SOR_PLL0_VCOCAP_MASK;
2184 value |= SOR_PLL0_ICHPMP(settings->ichpmp);
2185 value |= SOR_PLL0_VCOCAP(settings->vcocap);
2186 tegra_sor_writel(sor, value, SOR_PLL0);
2187
2188 tegra_sor_dp_term_calibrate(sor);
2189
2190 value = tegra_sor_readl(sor, SOR_PLL1);
2191 value &= ~SOR_PLL1_LOADADJ_MASK;
2192 value |= SOR_PLL1_LOADADJ(settings->loadadj);
2193 tegra_sor_writel(sor, value, SOR_PLL1);
2194
2195 value = tegra_sor_readl(sor, SOR_PLL3);
2196 value &= ~SOR_PLL3_BG_VREF_LEVEL_MASK;
2197 value |= SOR_PLL3_BG_VREF_LEVEL(settings->bg_vref);
2198 tegra_sor_writel(sor, value, SOR_PLL3);
2199
2200 value = settings->drive_current[0] << 24 |
2201 settings->drive_current[1] << 16 |
2202 settings->drive_current[2] << 8 |
2203 settings->drive_current[3] << 0;
2204 tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT0);
2205
2206 value = settings->preemphasis[0] << 24 |
2207 settings->preemphasis[1] << 16 |
2208 settings->preemphasis[2] << 8 |
2209 settings->preemphasis[3] << 0;
2210 tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS0);
2211
2212 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
2213 value &= ~SOR_DP_PADCTL_TX_PU_MASK;
2214 value |= SOR_DP_PADCTL_TX_PU_ENABLE;
2215 value |= SOR_DP_PADCTL_TX_PU(settings->tx_pu);
2216 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
2217
2218 /* power down pad calibration */
2219 value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
2220 value |= SOR_DP_PADCTL_PAD_CAL_PD;
2221 tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
2222
2223 /* miscellaneous display controller settings */
2224 value = VSYNC_H_POSITION(1);
2225 tegra_dc_writel(dc, value, DC_DISP_DISP_TIMING_OPTIONS);
2226
2227 value = tegra_dc_readl(dc, DC_DISP_DISP_COLOR_CONTROL);
2228 value &= ~DITHER_CONTROL_MASK;
2229 value &= ~BASE_COLOR_SIZE_MASK;
2230
Thierry Redingc31efa72015-09-08 16:09:22 +02002231 switch (state->bpc) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002232 case 6:
2233 value |= BASE_COLOR_SIZE_666;
2234 break;
2235
2236 case 8:
2237 value |= BASE_COLOR_SIZE_888;
2238 break;
2239
2240 default:
Thierry Redingc31efa72015-09-08 16:09:22 +02002241 WARN(1, "%u bits-per-color not supported\n", state->bpc);
2242 value |= BASE_COLOR_SIZE_888;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002243 break;
2244 }
2245
2246 tegra_dc_writel(dc, value, DC_DISP_DISP_COLOR_CONTROL);
2247
2248 err = tegra_sor_power_up(sor, 250);
2249 if (err < 0)
2250 dev_err(sor->dev, "failed to power up SOR: %d\n", err);
2251
Thierry Reding2bd1dd32015-08-03 15:46:15 +02002252 /* configure dynamic range of output */
Thierry Reding459cc2c2015-07-30 10:34:24 +02002253 value = tegra_sor_readl(sor, SOR_HEAD_STATE0(dc->pipe));
2254 value &= ~SOR_HEAD_STATE_RANGECOMPRESS_MASK;
2255 value &= ~SOR_HEAD_STATE_DYNRANGE_MASK;
2256 tegra_sor_writel(sor, value, SOR_HEAD_STATE0(dc->pipe));
2257
Thierry Reding2bd1dd32015-08-03 15:46:15 +02002258 /* configure colorspace */
Thierry Reding459cc2c2015-07-30 10:34:24 +02002259 value = tegra_sor_readl(sor, SOR_HEAD_STATE0(dc->pipe));
2260 value &= ~SOR_HEAD_STATE_COLORSPACE_MASK;
2261 value |= SOR_HEAD_STATE_COLORSPACE_RGB;
2262 tegra_sor_writel(sor, value, SOR_HEAD_STATE0(dc->pipe));
2263
Thierry Redingc31efa72015-09-08 16:09:22 +02002264 tegra_sor_mode_set(sor, mode, state);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002265
2266 tegra_sor_update(sor);
2267
2268 err = tegra_sor_attach(sor);
2269 if (err < 0)
2270 dev_err(sor->dev, "failed to attach SOR: %d\n", err);
2271
2272 /* enable display to SOR clock and generate HDMI preamble */
2273 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
2274 value |= SOR1_ENABLE | SOR1_TIMING_CYA;
2275 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
2276
2277 tegra_dc_commit(dc);
2278
2279 err = tegra_sor_wakeup(sor);
2280 if (err < 0)
2281 dev_err(sor->dev, "failed to wakeup SOR: %d\n", err);
2282}
2283
2284static const struct drm_encoder_helper_funcs tegra_sor_hdmi_helpers = {
2285 .disable = tegra_sor_hdmi_disable,
2286 .enable = tegra_sor_hdmi_enable,
2287 .atomic_check = tegra_sor_encoder_atomic_check,
2288};
2289
Thierry Reding6b6b6042013-11-15 16:06:05 +01002290static int tegra_sor_init(struct host1x_client *client)
2291{
Thierry Reding9910f5c2014-05-22 09:57:15 +02002292 struct drm_device *drm = dev_get_drvdata(client->parent);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002293 const struct drm_encoder_helper_funcs *helpers = NULL;
Thierry Reding6b6b6042013-11-15 16:06:05 +01002294 struct tegra_sor *sor = host1x_client_to_sor(client);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002295 int connector = DRM_MODE_CONNECTOR_Unknown;
2296 int encoder = DRM_MODE_ENCODER_NONE;
Thierry Reding6b6b6042013-11-15 16:06:05 +01002297 int err;
2298
Thierry Reding9542c232015-07-08 13:39:09 +02002299 if (!sor->aux) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002300 if (sor->soc->supports_hdmi) {
2301 connector = DRM_MODE_CONNECTOR_HDMIA;
2302 encoder = DRM_MODE_ENCODER_TMDS;
2303 helpers = &tegra_sor_hdmi_helpers;
2304 } else if (sor->soc->supports_lvds) {
2305 connector = DRM_MODE_CONNECTOR_LVDS;
2306 encoder = DRM_MODE_ENCODER_LVDS;
2307 }
2308 } else {
2309 if (sor->soc->supports_edp) {
2310 connector = DRM_MODE_CONNECTOR_eDP;
2311 encoder = DRM_MODE_ENCODER_TMDS;
2312 helpers = &tegra_sor_edp_helpers;
2313 } else if (sor->soc->supports_dp) {
2314 connector = DRM_MODE_CONNECTOR_DisplayPort;
2315 encoder = DRM_MODE_ENCODER_TMDS;
2316 }
2317 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01002318
Thierry Reding6b6b6042013-11-15 16:06:05 +01002319 sor->output.dev = sor->dev;
Thierry Reding6b6b6042013-11-15 16:06:05 +01002320
Thierry Reding6fad8f62014-11-28 15:41:34 +01002321 drm_connector_init(drm, &sor->output.connector,
2322 &tegra_sor_connector_funcs,
Thierry Reding459cc2c2015-07-30 10:34:24 +02002323 connector);
Thierry Reding6fad8f62014-11-28 15:41:34 +01002324 drm_connector_helper_add(&sor->output.connector,
2325 &tegra_sor_connector_helper_funcs);
2326 sor->output.connector.dpms = DRM_MODE_DPMS_OFF;
2327
Thierry Reding6fad8f62014-11-28 15:41:34 +01002328 drm_encoder_init(drm, &sor->output.encoder, &tegra_sor_encoder_funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +02002329 encoder, NULL);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002330 drm_encoder_helper_add(&sor->output.encoder, helpers);
Thierry Reding6fad8f62014-11-28 15:41:34 +01002331
2332 drm_mode_connector_attach_encoder(&sor->output.connector,
2333 &sor->output.encoder);
2334 drm_connector_register(&sor->output.connector);
2335
Thierry Redingea130b22014-12-19 15:51:35 +01002336 err = tegra_output_init(drm, &sor->output);
2337 if (err < 0) {
2338 dev_err(client->dev, "failed to initialize output: %d\n", err);
2339 return err;
2340 }
Thierry Reding6fad8f62014-11-28 15:41:34 +01002341
Thierry Redingea130b22014-12-19 15:51:35 +01002342 sor->output.encoder.possible_crtcs = 0x3;
Thierry Reding6b6b6042013-11-15 16:06:05 +01002343
Thierry Reding9542c232015-07-08 13:39:09 +02002344 if (sor->aux) {
2345 err = drm_dp_aux_attach(sor->aux, &sor->output);
Thierry Reding6b6b6042013-11-15 16:06:05 +01002346 if (err < 0) {
2347 dev_err(sor->dev, "failed to attach DP: %d\n", err);
2348 return err;
2349 }
2350 }
2351
Tomeu Vizoso535a65d2015-03-30 10:33:03 +02002352 /*
2353 * XXX: Remove this reset once proper hand-over from firmware to
2354 * kernel is possible.
2355 */
Jon Hunterf8c79122016-07-01 14:21:38 +01002356 if (sor->rst) {
2357 err = reset_control_assert(sor->rst);
2358 if (err < 0) {
2359 dev_err(sor->dev, "failed to assert SOR reset: %d\n",
2360 err);
2361 return err;
2362 }
Tomeu Vizoso535a65d2015-03-30 10:33:03 +02002363 }
2364
Thierry Reding6fad8f62014-11-28 15:41:34 +01002365 err = clk_prepare_enable(sor->clk);
2366 if (err < 0) {
2367 dev_err(sor->dev, "failed to enable clock: %d\n", err);
2368 return err;
2369 }
2370
Tomeu Vizoso535a65d2015-03-30 10:33:03 +02002371 usleep_range(1000, 3000);
2372
Jon Hunterf8c79122016-07-01 14:21:38 +01002373 if (sor->rst) {
2374 err = reset_control_deassert(sor->rst);
2375 if (err < 0) {
2376 dev_err(sor->dev, "failed to deassert 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_safe);
2383 if (err < 0)
2384 return err;
2385
2386 err = clk_prepare_enable(sor->clk_dp);
2387 if (err < 0)
2388 return err;
2389
Thierry Reding6b6b6042013-11-15 16:06:05 +01002390 return 0;
2391}
2392
2393static int tegra_sor_exit(struct host1x_client *client)
2394{
2395 struct tegra_sor *sor = host1x_client_to_sor(client);
2396 int err;
2397
Thierry Reding328ec692014-12-19 15:55:08 +01002398 tegra_output_exit(&sor->output);
2399
Thierry Reding9542c232015-07-08 13:39:09 +02002400 if (sor->aux) {
2401 err = drm_dp_aux_detach(sor->aux);
Thierry Reding6b6b6042013-11-15 16:06:05 +01002402 if (err < 0) {
2403 dev_err(sor->dev, "failed to detach DP: %d\n", err);
2404 return err;
2405 }
2406 }
2407
Thierry Reding6fad8f62014-11-28 15:41:34 +01002408 clk_disable_unprepare(sor->clk_safe);
2409 clk_disable_unprepare(sor->clk_dp);
2410 clk_disable_unprepare(sor->clk);
2411
Thierry Reding6b6b6042013-11-15 16:06:05 +01002412 return 0;
2413}
2414
2415static const struct host1x_client_ops sor_client_ops = {
2416 .init = tegra_sor_init,
2417 .exit = tegra_sor_exit,
2418};
2419
Thierry Reding459cc2c2015-07-30 10:34:24 +02002420static const struct tegra_sor_ops tegra_sor_edp_ops = {
2421 .name = "eDP",
2422};
2423
2424static int tegra_sor_hdmi_probe(struct tegra_sor *sor)
2425{
2426 int err;
2427
2428 sor->avdd_io_supply = devm_regulator_get(sor->dev, "avdd-io");
2429 if (IS_ERR(sor->avdd_io_supply)) {
2430 dev_err(sor->dev, "cannot get AVDD I/O supply: %ld\n",
2431 PTR_ERR(sor->avdd_io_supply));
2432 return PTR_ERR(sor->avdd_io_supply);
2433 }
2434
2435 err = regulator_enable(sor->avdd_io_supply);
2436 if (err < 0) {
2437 dev_err(sor->dev, "failed to enable AVDD I/O supply: %d\n",
2438 err);
2439 return err;
2440 }
2441
2442 sor->vdd_pll_supply = devm_regulator_get(sor->dev, "vdd-pll");
2443 if (IS_ERR(sor->vdd_pll_supply)) {
2444 dev_err(sor->dev, "cannot get VDD PLL supply: %ld\n",
2445 PTR_ERR(sor->vdd_pll_supply));
2446 return PTR_ERR(sor->vdd_pll_supply);
2447 }
2448
2449 err = regulator_enable(sor->vdd_pll_supply);
2450 if (err < 0) {
2451 dev_err(sor->dev, "failed to enable VDD PLL supply: %d\n",
2452 err);
2453 return err;
2454 }
2455
2456 sor->hdmi_supply = devm_regulator_get(sor->dev, "hdmi");
2457 if (IS_ERR(sor->hdmi_supply)) {
2458 dev_err(sor->dev, "cannot get HDMI supply: %ld\n",
2459 PTR_ERR(sor->hdmi_supply));
2460 return PTR_ERR(sor->hdmi_supply);
2461 }
2462
2463 err = regulator_enable(sor->hdmi_supply);
2464 if (err < 0) {
2465 dev_err(sor->dev, "failed to enable HDMI supply: %d\n", err);
2466 return err;
2467 }
2468
2469 return 0;
2470}
2471
2472static int tegra_sor_hdmi_remove(struct tegra_sor *sor)
2473{
2474 regulator_disable(sor->hdmi_supply);
2475 regulator_disable(sor->vdd_pll_supply);
2476 regulator_disable(sor->avdd_io_supply);
2477
2478 return 0;
2479}
2480
2481static const struct tegra_sor_ops tegra_sor_hdmi_ops = {
2482 .name = "HDMI",
2483 .probe = tegra_sor_hdmi_probe,
2484 .remove = tegra_sor_hdmi_remove,
2485};
2486
Thierry Reding30b49432015-08-03 15:50:32 +02002487static const u8 tegra124_sor_xbar_cfg[5] = {
2488 0, 1, 2, 3, 4
2489};
2490
Thierry Reding459cc2c2015-07-30 10:34:24 +02002491static const struct tegra_sor_soc tegra124_sor = {
2492 .supports_edp = true,
2493 .supports_lvds = true,
2494 .supports_hdmi = false,
2495 .supports_dp = false,
Thierry Reding30b49432015-08-03 15:50:32 +02002496 .xbar_cfg = tegra124_sor_xbar_cfg,
Thierry Reding459cc2c2015-07-30 10:34:24 +02002497};
2498
2499static const struct tegra_sor_soc tegra210_sor = {
2500 .supports_edp = true,
2501 .supports_lvds = false,
2502 .supports_hdmi = false,
2503 .supports_dp = false,
Thierry Reding30b49432015-08-03 15:50:32 +02002504 .xbar_cfg = tegra124_sor_xbar_cfg,
2505};
2506
2507static const u8 tegra210_sor_xbar_cfg[5] = {
2508 2, 1, 0, 3, 4
Thierry Reding459cc2c2015-07-30 10:34:24 +02002509};
2510
2511static const struct tegra_sor_soc tegra210_sor1 = {
2512 .supports_edp = false,
2513 .supports_lvds = false,
2514 .supports_hdmi = true,
2515 .supports_dp = true,
2516
2517 .num_settings = ARRAY_SIZE(tegra210_sor_hdmi_defaults),
2518 .settings = tegra210_sor_hdmi_defaults,
Thierry Reding30b49432015-08-03 15:50:32 +02002519
2520 .xbar_cfg = tegra210_sor_xbar_cfg,
Thierry Reding459cc2c2015-07-30 10:34:24 +02002521};
2522
2523static const struct of_device_id tegra_sor_of_match[] = {
2524 { .compatible = "nvidia,tegra210-sor1", .data = &tegra210_sor1 },
2525 { .compatible = "nvidia,tegra210-sor", .data = &tegra210_sor },
2526 { .compatible = "nvidia,tegra124-sor", .data = &tegra124_sor },
2527 { },
2528};
2529MODULE_DEVICE_TABLE(of, tegra_sor_of_match);
2530
Thierry Reding6b6b6042013-11-15 16:06:05 +01002531static int tegra_sor_probe(struct platform_device *pdev)
2532{
2533 struct device_node *np;
2534 struct tegra_sor *sor;
2535 struct resource *regs;
2536 int err;
2537
2538 sor = devm_kzalloc(&pdev->dev, sizeof(*sor), GFP_KERNEL);
2539 if (!sor)
2540 return -ENOMEM;
2541
Thierry Reding5faea3d2017-08-21 17:33:14 +02002542 sor->soc = of_device_get_match_data(&pdev->dev);
Thierry Reding6b6b6042013-11-15 16:06:05 +01002543 sor->output.dev = sor->dev = &pdev->dev;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002544
2545 sor->settings = devm_kmemdup(&pdev->dev, sor->soc->settings,
2546 sor->soc->num_settings *
2547 sizeof(*sor->settings),
2548 GFP_KERNEL);
2549 if (!sor->settings)
2550 return -ENOMEM;
2551
2552 sor->num_settings = sor->soc->num_settings;
Thierry Reding6b6b6042013-11-15 16:06:05 +01002553
2554 np = of_parse_phandle(pdev->dev.of_node, "nvidia,dpaux", 0);
2555 if (np) {
Thierry Reding9542c232015-07-08 13:39:09 +02002556 sor->aux = drm_dp_aux_find_by_of_node(np);
Thierry Reding6b6b6042013-11-15 16:06:05 +01002557 of_node_put(np);
2558
Thierry Reding9542c232015-07-08 13:39:09 +02002559 if (!sor->aux)
Thierry Reding6b6b6042013-11-15 16:06:05 +01002560 return -EPROBE_DEFER;
2561 }
2562
Thierry Reding9542c232015-07-08 13:39:09 +02002563 if (!sor->aux) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002564 if (sor->soc->supports_hdmi) {
2565 sor->ops = &tegra_sor_hdmi_ops;
2566 } else if (sor->soc->supports_lvds) {
2567 dev_err(&pdev->dev, "LVDS not supported yet\n");
2568 return -ENODEV;
2569 } else {
2570 dev_err(&pdev->dev, "unknown (non-DP) support\n");
2571 return -ENODEV;
2572 }
2573 } else {
2574 if (sor->soc->supports_edp) {
2575 sor->ops = &tegra_sor_edp_ops;
2576 } else if (sor->soc->supports_dp) {
2577 dev_err(&pdev->dev, "DisplayPort not supported yet\n");
2578 return -ENODEV;
2579 } else {
2580 dev_err(&pdev->dev, "unknown (DP) support\n");
2581 return -ENODEV;
2582 }
2583 }
2584
Thierry Reding6b6b6042013-11-15 16:06:05 +01002585 err = tegra_output_probe(&sor->output);
Thierry Reding4dbdc742015-04-27 15:04:26 +02002586 if (err < 0) {
2587 dev_err(&pdev->dev, "failed to probe output: %d\n", err);
Thierry Reding6b6b6042013-11-15 16:06:05 +01002588 return err;
Thierry Reding4dbdc742015-04-27 15:04:26 +02002589 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01002590
Thierry Reding459cc2c2015-07-30 10:34:24 +02002591 if (sor->ops && sor->ops->probe) {
2592 err = sor->ops->probe(sor);
2593 if (err < 0) {
2594 dev_err(&pdev->dev, "failed to probe %s: %d\n",
2595 sor->ops->name, err);
2596 goto output;
2597 }
2598 }
2599
Thierry Reding6b6b6042013-11-15 16:06:05 +01002600 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2601 sor->regs = devm_ioremap_resource(&pdev->dev, regs);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002602 if (IS_ERR(sor->regs)) {
2603 err = PTR_ERR(sor->regs);
2604 goto remove;
2605 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01002606
Jon Hunterf8c79122016-07-01 14:21:38 +01002607 if (!pdev->dev.pm_domain) {
2608 sor->rst = devm_reset_control_get(&pdev->dev, "sor");
2609 if (IS_ERR(sor->rst)) {
2610 err = PTR_ERR(sor->rst);
2611 dev_err(&pdev->dev, "failed to get reset control: %d\n",
2612 err);
2613 goto remove;
2614 }
Thierry Reding4dbdc742015-04-27 15:04:26 +02002615 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01002616
2617 sor->clk = devm_clk_get(&pdev->dev, NULL);
Thierry Reding4dbdc742015-04-27 15:04:26 +02002618 if (IS_ERR(sor->clk)) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002619 err = PTR_ERR(sor->clk);
2620 dev_err(&pdev->dev, "failed to get module clock: %d\n", err);
2621 goto remove;
Thierry Reding4dbdc742015-04-27 15:04:26 +02002622 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01002623
Thierry Reding618dee32016-06-09 17:53:57 +02002624 if (sor->soc->supports_hdmi || sor->soc->supports_dp) {
Thierry Redinge1335e22017-10-12 17:53:11 +02002625 struct device_node *np = pdev->dev.of_node;
2626 const char *name;
2627
2628 /*
2629 * For backwards compatibility with Tegra210 device trees,
2630 * fall back to the old clock name "source" if the new "out"
2631 * clock is not available.
2632 */
2633 if (of_property_match_string(np, "clock-names", "out") < 0)
2634 name = "source";
2635 else
2636 name = "out";
2637
2638 sor->clk_out = devm_clk_get(&pdev->dev, name);
2639 if (IS_ERR(sor->clk_out)) {
2640 err = PTR_ERR(sor->clk_out);
2641 dev_err(sor->dev, "failed to get %s clock: %d\n",
2642 name, err);
Thierry Reding618dee32016-06-09 17:53:57 +02002643 goto remove;
2644 }
2645 }
2646
Thierry Reding6b6b6042013-11-15 16:06:05 +01002647 sor->clk_parent = devm_clk_get(&pdev->dev, "parent");
Thierry Reding4dbdc742015-04-27 15:04:26 +02002648 if (IS_ERR(sor->clk_parent)) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002649 err = PTR_ERR(sor->clk_parent);
2650 dev_err(&pdev->dev, "failed to get parent clock: %d\n", err);
2651 goto remove;
Thierry Reding4dbdc742015-04-27 15:04:26 +02002652 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01002653
Thierry Reding6b6b6042013-11-15 16:06:05 +01002654 sor->clk_safe = devm_clk_get(&pdev->dev, "safe");
Thierry Reding4dbdc742015-04-27 15:04:26 +02002655 if (IS_ERR(sor->clk_safe)) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002656 err = PTR_ERR(sor->clk_safe);
2657 dev_err(&pdev->dev, "failed to get safe clock: %d\n", err);
2658 goto remove;
Thierry Reding4dbdc742015-04-27 15:04:26 +02002659 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01002660
Thierry Reding6b6b6042013-11-15 16:06:05 +01002661 sor->clk_dp = devm_clk_get(&pdev->dev, "dp");
Thierry Reding4dbdc742015-04-27 15:04:26 +02002662 if (IS_ERR(sor->clk_dp)) {
Thierry Reding459cc2c2015-07-30 10:34:24 +02002663 err = PTR_ERR(sor->clk_dp);
2664 dev_err(&pdev->dev, "failed to get DP clock: %d\n", err);
2665 goto remove;
Thierry Reding4dbdc742015-04-27 15:04:26 +02002666 }
Thierry Reding6b6b6042013-11-15 16:06:05 +01002667
Thierry Redinge1335e22017-10-12 17:53:11 +02002668 /*
2669 * Starting with Tegra186, the BPMP provides an implementation for
2670 * the pad output clock, so we have to look it up from device tree.
2671 */
2672 sor->clk_pad = devm_clk_get(&pdev->dev, "pad");
2673 if (IS_ERR(sor->clk_pad)) {
2674 if (sor->clk_pad != ERR_PTR(-ENOENT)) {
2675 err = PTR_ERR(sor->clk_pad);
2676 goto remove;
2677 }
2678
2679 /*
2680 * If the pad output clock is not available, then we assume
2681 * we're on Tegra210 or earlier and have to provide our own
2682 * implementation.
2683 */
2684 sor->clk_pad = NULL;
2685 }
2686
2687 /*
2688 * The bootloader may have set up the SOR such that it's module clock
2689 * is sourced by one of the display PLLs. However, that doesn't work
2690 * without properly having set up other bits of the SOR.
2691 */
2692 err = clk_set_parent(sor->clk_out, sor->clk_safe);
2693 if (err < 0) {
2694 dev_err(&pdev->dev, "failed to use safe clock: %d\n", err);
2695 goto remove;
2696 }
2697
Thierry Redingaaff8bd2015-08-07 16:04:54 +02002698 platform_set_drvdata(pdev, sor);
2699 pm_runtime_enable(&pdev->dev);
2700
Thierry Redinge1335e22017-10-12 17:53:11 +02002701 /*
2702 * On Tegra210 and earlier, provide our own implementation for the
2703 * pad output clock.
2704 */
2705 if (!sor->clk_pad) {
2706 err = pm_runtime_get_sync(&pdev->dev);
2707 if (err < 0) {
2708 dev_err(&pdev->dev, "failed to get runtime PM: %d\n",
2709 err);
2710 goto remove;
2711 }
Thierry Redingb2992212015-10-01 14:25:03 +02002712
Thierry Redinge1335e22017-10-12 17:53:11 +02002713 sor->clk_pad = tegra_clk_sor_pad_register(sor,
2714 "sor1_pad_clkout");
2715 pm_runtime_put(&pdev->dev);
2716 }
2717
2718 if (IS_ERR(sor->clk_pad)) {
2719 err = PTR_ERR(sor->clk_pad);
2720 dev_err(&pdev->dev, "failed to register SOR pad clock: %d\n",
2721 err);
Thierry Redingb2992212015-10-01 14:25:03 +02002722 goto remove;
2723 }
2724
Thierry Reding6b6b6042013-11-15 16:06:05 +01002725 INIT_LIST_HEAD(&sor->client.list);
2726 sor->client.ops = &sor_client_ops;
2727 sor->client.dev = &pdev->dev;
2728
Thierry Reding6b6b6042013-11-15 16:06:05 +01002729 err = host1x_client_register(&sor->client);
2730 if (err < 0) {
2731 dev_err(&pdev->dev, "failed to register host1x client: %d\n",
2732 err);
Thierry Reding459cc2c2015-07-30 10:34:24 +02002733 goto remove;
Thierry Reding6b6b6042013-11-15 16:06:05 +01002734 }
2735
Thierry Reding6b6b6042013-11-15 16:06:05 +01002736 return 0;
Thierry Reding459cc2c2015-07-30 10:34:24 +02002737
2738remove:
2739 if (sor->ops && sor->ops->remove)
2740 sor->ops->remove(sor);
2741output:
2742 tegra_output_remove(&sor->output);
2743 return err;
Thierry Reding6b6b6042013-11-15 16:06:05 +01002744}
2745
2746static int tegra_sor_remove(struct platform_device *pdev)
2747{
2748 struct tegra_sor *sor = platform_get_drvdata(pdev);
2749 int err;
2750
Thierry Redingaaff8bd2015-08-07 16:04:54 +02002751 pm_runtime_disable(&pdev->dev);
2752
Thierry Reding6b6b6042013-11-15 16:06:05 +01002753 err = host1x_client_unregister(&sor->client);
2754 if (err < 0) {
2755 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
2756 err);
2757 return err;
2758 }
2759
Thierry Reding459cc2c2015-07-30 10:34:24 +02002760 if (sor->ops && sor->ops->remove) {
2761 err = sor->ops->remove(sor);
2762 if (err < 0)
2763 dev_err(&pdev->dev, "failed to remove SOR: %d\n", err);
2764 }
2765
Thierry Reding328ec692014-12-19 15:55:08 +01002766 tegra_output_remove(&sor->output);
Thierry Reding6b6b6042013-11-15 16:06:05 +01002767
2768 return 0;
2769}
2770
Thierry Redingaaff8bd2015-08-07 16:04:54 +02002771#ifdef CONFIG_PM
2772static int tegra_sor_suspend(struct device *dev)
2773{
2774 struct tegra_sor *sor = dev_get_drvdata(dev);
2775 int err;
2776
Jon Hunterf8c79122016-07-01 14:21:38 +01002777 if (sor->rst) {
2778 err = reset_control_assert(sor->rst);
2779 if (err < 0) {
2780 dev_err(dev, "failed to assert reset: %d\n", err);
2781 return err;
2782 }
Thierry Redingaaff8bd2015-08-07 16:04:54 +02002783 }
2784
2785 usleep_range(1000, 2000);
2786
2787 clk_disable_unprepare(sor->clk);
2788
2789 return 0;
2790}
2791
2792static int tegra_sor_resume(struct device *dev)
2793{
2794 struct tegra_sor *sor = dev_get_drvdata(dev);
2795 int err;
2796
2797 err = clk_prepare_enable(sor->clk);
2798 if (err < 0) {
2799 dev_err(dev, "failed to enable clock: %d\n", err);
2800 return err;
2801 }
2802
2803 usleep_range(1000, 2000);
2804
Jon Hunterf8c79122016-07-01 14:21:38 +01002805 if (sor->rst) {
2806 err = reset_control_deassert(sor->rst);
2807 if (err < 0) {
2808 dev_err(dev, "failed to deassert reset: %d\n", err);
2809 clk_disable_unprepare(sor->clk);
2810 return err;
2811 }
Thierry Redingaaff8bd2015-08-07 16:04:54 +02002812 }
2813
2814 return 0;
2815}
2816#endif
2817
2818static const struct dev_pm_ops tegra_sor_pm_ops = {
2819 SET_RUNTIME_PM_OPS(tegra_sor_suspend, tegra_sor_resume, NULL)
2820};
2821
Thierry Reding6b6b6042013-11-15 16:06:05 +01002822struct platform_driver tegra_sor_driver = {
2823 .driver = {
2824 .name = "tegra-sor",
2825 .of_match_table = tegra_sor_of_match,
Thierry Redingaaff8bd2015-08-07 16:04:54 +02002826 .pm = &tegra_sor_pm_ops,
Thierry Reding6b6b6042013-11-15 16:06:05 +01002827 },
2828 .probe = tegra_sor_probe,
2829 .remove = tegra_sor_remove,
2830};