blob: 66816104ba722f36e3654bb1353e2faf23718713 [file] [log] [blame]
Thierry Redingdec72732013-09-03 08:45:46 +02001/*
2 * Copyright (C) 2013 NVIDIA Corporation
3 *
Thierry Reding9a2ac2d2014-02-11 15:52:01 +01004 * 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.
Thierry Redingdec72732013-09-03 08:45:46 +02007 */
8
9#include <linux/clk.h>
10#include <linux/debugfs.h>
11#include <linux/host1x.h>
12#include <linux/module.h>
13#include <linux/of.h>
Thierry Redinge94236c2014-10-07 16:10:24 +020014#include <linux/of_platform.h>
Thierry Redingdec72732013-09-03 08:45:46 +020015#include <linux/platform_device.h>
16#include <linux/reset.h>
17
Thierry Reding3b077af2014-03-14 14:07:50 +010018#include <linux/regulator/consumer.h>
19
Thierry Redingdec72732013-09-03 08:45:46 +020020#include <drm/drm_mipi_dsi.h>
21#include <drm/drm_panel.h>
22
23#include <video/mipi_display.h>
24
25#include "dc.h"
26#include "drm.h"
27#include "dsi.h"
28#include "mipi-phy.h"
29
Thierry Redingdec72732013-09-03 08:45:46 +020030struct tegra_dsi {
31 struct host1x_client client;
32 struct tegra_output output;
33 struct device *dev;
34
35 void __iomem *regs;
36
37 struct reset_control *rst;
38 struct clk *clk_parent;
39 struct clk *clk_lp;
40 struct clk *clk;
41
42 struct drm_info_list *debugfs_files;
43 struct drm_minor *minor;
44 struct dentry *debugfs;
45
Thierry Reding17297a22014-03-14 14:13:15 +010046 unsigned long flags;
Thierry Redingdec72732013-09-03 08:45:46 +020047 enum mipi_dsi_pixel_format format;
48 unsigned int lanes;
49
50 struct tegra_mipi_device *mipi;
51 struct mipi_dsi_host host;
Thierry Reding3b077af2014-03-14 14:07:50 +010052
53 struct regulator *vdd;
Thierry Reding334ae6b2014-03-14 14:15:10 +010054 bool enabled;
Thierry Reding976cebc2014-08-06 09:14:28 +020055
56 unsigned int video_fifo_depth;
57 unsigned int host_fifo_depth;
Thierry Redinge94236c2014-10-07 16:10:24 +020058
59 /* for ganged-mode support */
60 struct tegra_dsi *master;
61 struct tegra_dsi *slave;
Thierry Redingdec72732013-09-03 08:45:46 +020062};
63
64static inline struct tegra_dsi *
65host1x_client_to_dsi(struct host1x_client *client)
66{
67 return container_of(client, struct tegra_dsi, client);
68}
69
70static inline struct tegra_dsi *host_to_tegra(struct mipi_dsi_host *host)
71{
72 return container_of(host, struct tegra_dsi, host);
73}
74
75static inline struct tegra_dsi *to_dsi(struct tegra_output *output)
76{
77 return container_of(output, struct tegra_dsi, output);
78}
79
80static inline unsigned long tegra_dsi_readl(struct tegra_dsi *dsi,
81 unsigned long reg)
82{
83 return readl(dsi->regs + (reg << 2));
84}
85
86static inline void tegra_dsi_writel(struct tegra_dsi *dsi, unsigned long value,
87 unsigned long reg)
88{
89 writel(value, dsi->regs + (reg << 2));
90}
91
92static int tegra_dsi_show_regs(struct seq_file *s, void *data)
93{
94 struct drm_info_node *node = s->private;
95 struct tegra_dsi *dsi = node->info_ent->data;
96
97#define DUMP_REG(name) \
98 seq_printf(s, "%-32s %#05x %08lx\n", #name, name, \
99 tegra_dsi_readl(dsi, name))
100
101 DUMP_REG(DSI_INCR_SYNCPT);
102 DUMP_REG(DSI_INCR_SYNCPT_CONTROL);
103 DUMP_REG(DSI_INCR_SYNCPT_ERROR);
104 DUMP_REG(DSI_CTXSW);
105 DUMP_REG(DSI_RD_DATA);
106 DUMP_REG(DSI_WR_DATA);
107 DUMP_REG(DSI_POWER_CONTROL);
108 DUMP_REG(DSI_INT_ENABLE);
109 DUMP_REG(DSI_INT_STATUS);
110 DUMP_REG(DSI_INT_MASK);
111 DUMP_REG(DSI_HOST_CONTROL);
112 DUMP_REG(DSI_CONTROL);
113 DUMP_REG(DSI_SOL_DELAY);
114 DUMP_REG(DSI_MAX_THRESHOLD);
115 DUMP_REG(DSI_TRIGGER);
116 DUMP_REG(DSI_TX_CRC);
117 DUMP_REG(DSI_STATUS);
118
119 DUMP_REG(DSI_INIT_SEQ_CONTROL);
120 DUMP_REG(DSI_INIT_SEQ_DATA_0);
121 DUMP_REG(DSI_INIT_SEQ_DATA_1);
122 DUMP_REG(DSI_INIT_SEQ_DATA_2);
123 DUMP_REG(DSI_INIT_SEQ_DATA_3);
124 DUMP_REG(DSI_INIT_SEQ_DATA_4);
125 DUMP_REG(DSI_INIT_SEQ_DATA_5);
126 DUMP_REG(DSI_INIT_SEQ_DATA_6);
127 DUMP_REG(DSI_INIT_SEQ_DATA_7);
128
129 DUMP_REG(DSI_PKT_SEQ_0_LO);
130 DUMP_REG(DSI_PKT_SEQ_0_HI);
131 DUMP_REG(DSI_PKT_SEQ_1_LO);
132 DUMP_REG(DSI_PKT_SEQ_1_HI);
133 DUMP_REG(DSI_PKT_SEQ_2_LO);
134 DUMP_REG(DSI_PKT_SEQ_2_HI);
135 DUMP_REG(DSI_PKT_SEQ_3_LO);
136 DUMP_REG(DSI_PKT_SEQ_3_HI);
137 DUMP_REG(DSI_PKT_SEQ_4_LO);
138 DUMP_REG(DSI_PKT_SEQ_4_HI);
139 DUMP_REG(DSI_PKT_SEQ_5_LO);
140 DUMP_REG(DSI_PKT_SEQ_5_HI);
141
142 DUMP_REG(DSI_DCS_CMDS);
143
144 DUMP_REG(DSI_PKT_LEN_0_1);
145 DUMP_REG(DSI_PKT_LEN_2_3);
146 DUMP_REG(DSI_PKT_LEN_4_5);
147 DUMP_REG(DSI_PKT_LEN_6_7);
148
149 DUMP_REG(DSI_PHY_TIMING_0);
150 DUMP_REG(DSI_PHY_TIMING_1);
151 DUMP_REG(DSI_PHY_TIMING_2);
152 DUMP_REG(DSI_BTA_TIMING);
153
154 DUMP_REG(DSI_TIMEOUT_0);
155 DUMP_REG(DSI_TIMEOUT_1);
156 DUMP_REG(DSI_TO_TALLY);
157
158 DUMP_REG(DSI_PAD_CONTROL_0);
159 DUMP_REG(DSI_PAD_CONTROL_CD);
160 DUMP_REG(DSI_PAD_CD_STATUS);
161 DUMP_REG(DSI_VIDEO_MODE_CONTROL);
162 DUMP_REG(DSI_PAD_CONTROL_1);
163 DUMP_REG(DSI_PAD_CONTROL_2);
164 DUMP_REG(DSI_PAD_CONTROL_3);
165 DUMP_REG(DSI_PAD_CONTROL_4);
166
167 DUMP_REG(DSI_GANGED_MODE_CONTROL);
168 DUMP_REG(DSI_GANGED_MODE_START);
169 DUMP_REG(DSI_GANGED_MODE_SIZE);
170
171 DUMP_REG(DSI_RAW_DATA_BYTE_COUNT);
172 DUMP_REG(DSI_ULTRA_LOW_POWER_CONTROL);
173
174 DUMP_REG(DSI_INIT_SEQ_DATA_8);
175 DUMP_REG(DSI_INIT_SEQ_DATA_9);
176 DUMP_REG(DSI_INIT_SEQ_DATA_10);
177 DUMP_REG(DSI_INIT_SEQ_DATA_11);
178 DUMP_REG(DSI_INIT_SEQ_DATA_12);
179 DUMP_REG(DSI_INIT_SEQ_DATA_13);
180 DUMP_REG(DSI_INIT_SEQ_DATA_14);
181 DUMP_REG(DSI_INIT_SEQ_DATA_15);
182
183#undef DUMP_REG
184
185 return 0;
186}
187
188static struct drm_info_list debugfs_files[] = {
189 { "regs", tegra_dsi_show_regs, 0, NULL },
190};
191
192static int tegra_dsi_debugfs_init(struct tegra_dsi *dsi,
193 struct drm_minor *minor)
194{
195 const char *name = dev_name(dsi->dev);
196 unsigned int i;
197 int err;
198
199 dsi->debugfs = debugfs_create_dir(name, minor->debugfs_root);
200 if (!dsi->debugfs)
201 return -ENOMEM;
202
203 dsi->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
204 GFP_KERNEL);
205 if (!dsi->debugfs_files) {
206 err = -ENOMEM;
207 goto remove;
208 }
209
210 for (i = 0; i < ARRAY_SIZE(debugfs_files); i++)
211 dsi->debugfs_files[i].data = dsi;
212
213 err = drm_debugfs_create_files(dsi->debugfs_files,
214 ARRAY_SIZE(debugfs_files),
215 dsi->debugfs, minor);
216 if (err < 0)
217 goto free;
218
219 dsi->minor = minor;
220
221 return 0;
222
223free:
224 kfree(dsi->debugfs_files);
225 dsi->debugfs_files = NULL;
226remove:
227 debugfs_remove(dsi->debugfs);
228 dsi->debugfs = NULL;
229
230 return err;
231}
232
233static int tegra_dsi_debugfs_exit(struct tegra_dsi *dsi)
234{
235 drm_debugfs_remove_files(dsi->debugfs_files, ARRAY_SIZE(debugfs_files),
236 dsi->minor);
237 dsi->minor = NULL;
238
239 kfree(dsi->debugfs_files);
240 dsi->debugfs_files = NULL;
241
242 debugfs_remove(dsi->debugfs);
243 dsi->debugfs = NULL;
244
245 return 0;
246}
247
248#define PKT_ID0(id) ((((id) & 0x3f) << 3) | (1 << 9))
249#define PKT_LEN0(len) (((len) & 0x07) << 0)
250#define PKT_ID1(id) ((((id) & 0x3f) << 13) | (1 << 19))
251#define PKT_LEN1(len) (((len) & 0x07) << 10)
252#define PKT_ID2(id) ((((id) & 0x3f) << 23) | (1 << 29))
253#define PKT_LEN2(len) (((len) & 0x07) << 20)
254
255#define PKT_LP (1 << 30)
256#define NUM_PKT_SEQ 12
257
Thierry Reding17297a22014-03-14 14:13:15 +0100258/*
259 * non-burst mode with sync pulses
260 */
261static const u32 pkt_seq_video_non_burst_sync_pulses[NUM_PKT_SEQ] = {
Thierry Redingdec72732013-09-03 08:45:46 +0200262 [ 0] = PKT_ID0(MIPI_DSI_V_SYNC_START) | PKT_LEN0(0) |
263 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
264 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0) |
265 PKT_LP,
266 [ 1] = 0,
267 [ 2] = PKT_ID0(MIPI_DSI_V_SYNC_END) | PKT_LEN0(0) |
268 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
269 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0) |
270 PKT_LP,
271 [ 3] = 0,
272 [ 4] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
273 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
274 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0) |
275 PKT_LP,
276 [ 5] = 0,
277 [ 6] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
278 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
279 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0),
280 [ 7] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(2) |
281 PKT_ID1(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN1(3) |
282 PKT_ID2(MIPI_DSI_BLANKING_PACKET) | PKT_LEN2(4),
283 [ 8] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
284 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
285 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0) |
286 PKT_LP,
287 [ 9] = 0,
288 [10] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
289 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
290 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0),
291 [11] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(2) |
292 PKT_ID1(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN1(3) |
293 PKT_ID2(MIPI_DSI_BLANKING_PACKET) | PKT_LEN2(4),
294};
295
Thierry Reding17297a22014-03-14 14:13:15 +0100296/*
297 * non-burst mode with sync events
298 */
299static const u32 pkt_seq_video_non_burst_sync_events[NUM_PKT_SEQ] = {
300 [ 0] = PKT_ID0(MIPI_DSI_V_SYNC_START) | PKT_LEN0(0) |
301 PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION) | PKT_LEN1(7) |
302 PKT_LP,
303 [ 1] = 0,
304 [ 2] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
305 PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION) | PKT_LEN1(7) |
306 PKT_LP,
307 [ 3] = 0,
308 [ 4] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
309 PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION) | PKT_LEN1(7) |
310 PKT_LP,
311 [ 5] = 0,
312 [ 6] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
313 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(2) |
314 PKT_ID2(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN2(3),
315 [ 7] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(4),
316 [ 8] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
317 PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION) | PKT_LEN1(7) |
318 PKT_LP,
319 [ 9] = 0,
320 [10] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
321 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(2) |
322 PKT_ID2(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN2(3),
323 [11] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(4),
324};
325
Thierry Reding337b4432014-11-13 15:02:46 +0100326static const u32 pkt_seq_command_mode[NUM_PKT_SEQ] = {
327 [ 0] = 0,
328 [ 1] = 0,
329 [ 2] = 0,
330 [ 3] = 0,
331 [ 4] = 0,
332 [ 5] = 0,
333 [ 6] = PKT_ID0(MIPI_DSI_DCS_LONG_WRITE) | PKT_LEN0(3) | PKT_LP,
334 [ 7] = 0,
335 [ 8] = 0,
336 [ 9] = 0,
337 [10] = PKT_ID0(MIPI_DSI_DCS_LONG_WRITE) | PKT_LEN0(5) | PKT_LP,
338 [11] = 0,
339};
340
Thierry Redingdec72732013-09-03 08:45:46 +0200341static int tegra_dsi_set_phy_timing(struct tegra_dsi *dsi)
342{
343 struct mipi_dphy_timing timing;
344 unsigned long value, period;
345 long rate;
346 int err;
347
348 rate = clk_get_rate(dsi->clk);
349 if (rate < 0)
350 return rate;
351
352 period = DIV_ROUND_CLOSEST(1000000000UL, rate * 2);
353
354 err = mipi_dphy_timing_get_default(&timing, period);
355 if (err < 0)
356 return err;
357
358 err = mipi_dphy_timing_validate(&timing, period);
359 if (err < 0) {
360 dev_err(dsi->dev, "failed to validate D-PHY timing: %d\n", err);
361 return err;
362 }
363
364 /*
365 * The D-PHY timing fields below are expressed in byte-clock cycles,
366 * so multiply the period by 8.
367 */
368 period *= 8;
369
370 value = DSI_TIMING_FIELD(timing.hsexit, period, 1) << 24 |
371 DSI_TIMING_FIELD(timing.hstrail, period, 0) << 16 |
372 DSI_TIMING_FIELD(timing.hszero, period, 3) << 8 |
373 DSI_TIMING_FIELD(timing.hsprepare, period, 1);
374 tegra_dsi_writel(dsi, value, DSI_PHY_TIMING_0);
375
376 value = DSI_TIMING_FIELD(timing.clktrail, period, 1) << 24 |
377 DSI_TIMING_FIELD(timing.clkpost, period, 1) << 16 |
378 DSI_TIMING_FIELD(timing.clkzero, period, 1) << 8 |
379 DSI_TIMING_FIELD(timing.lpx, period, 1);
380 tegra_dsi_writel(dsi, value, DSI_PHY_TIMING_1);
381
382 value = DSI_TIMING_FIELD(timing.clkprepare, period, 1) << 16 |
383 DSI_TIMING_FIELD(timing.clkpre, period, 1) << 8 |
384 DSI_TIMING_FIELD(0xff * period, period, 0) << 0;
385 tegra_dsi_writel(dsi, value, DSI_PHY_TIMING_2);
386
387 value = DSI_TIMING_FIELD(timing.taget, period, 1) << 16 |
388 DSI_TIMING_FIELD(timing.tasure, period, 1) << 8 |
389 DSI_TIMING_FIELD(timing.tago, period, 1);
390 tegra_dsi_writel(dsi, value, DSI_BTA_TIMING);
391
392 return 0;
393}
394
395static int tegra_dsi_get_muldiv(enum mipi_dsi_pixel_format format,
396 unsigned int *mulp, unsigned int *divp)
397{
398 switch (format) {
399 case MIPI_DSI_FMT_RGB666_PACKED:
400 case MIPI_DSI_FMT_RGB888:
401 *mulp = 3;
402 *divp = 1;
403 break;
404
405 case MIPI_DSI_FMT_RGB565:
406 *mulp = 2;
407 *divp = 1;
408 break;
409
410 case MIPI_DSI_FMT_RGB666:
411 *mulp = 9;
412 *divp = 4;
413 break;
414
415 default:
416 return -EINVAL;
417 }
418
419 return 0;
420}
421
Thierry Redingf7d68892014-03-13 08:50:39 +0100422static int tegra_dsi_get_format(enum mipi_dsi_pixel_format format,
423 enum tegra_dsi_format *fmt)
424{
425 switch (format) {
426 case MIPI_DSI_FMT_RGB888:
427 *fmt = TEGRA_DSI_FORMAT_24P;
428 break;
429
430 case MIPI_DSI_FMT_RGB666:
431 *fmt = TEGRA_DSI_FORMAT_18NP;
432 break;
433
434 case MIPI_DSI_FMT_RGB666_PACKED:
435 *fmt = TEGRA_DSI_FORMAT_18P;
436 break;
437
438 case MIPI_DSI_FMT_RGB565:
439 *fmt = TEGRA_DSI_FORMAT_16P;
440 break;
441
442 default:
443 return -EINVAL;
444 }
445
446 return 0;
447}
448
Thierry Redinge94236c2014-10-07 16:10:24 +0200449static void tegra_dsi_ganged_enable(struct tegra_dsi *dsi, unsigned int start,
450 unsigned int size)
451{
452 u32 value;
453
454 tegra_dsi_writel(dsi, start, DSI_GANGED_MODE_START);
455 tegra_dsi_writel(dsi, size << 16 | size, DSI_GANGED_MODE_SIZE);
456
457 value = DSI_GANGED_MODE_CONTROL_ENABLE;
458 tegra_dsi_writel(dsi, value, DSI_GANGED_MODE_CONTROL);
459}
460
Thierry Reding563eff12014-11-13 14:44:27 +0100461static void tegra_dsi_enable(struct tegra_dsi *dsi)
Thierry Redingdec72732013-09-03 08:45:46 +0200462{
Thierry Reding563eff12014-11-13 14:44:27 +0100463 u32 value;
Thierry Redingdec72732013-09-03 08:45:46 +0200464
Thierry Reding563eff12014-11-13 14:44:27 +0100465 value = tegra_dsi_readl(dsi, DSI_POWER_CONTROL);
466 value |= DSI_POWER_CONTROL_ENABLE;
467 tegra_dsi_writel(dsi, value, DSI_POWER_CONTROL);
Thierry Redinge94236c2014-10-07 16:10:24 +0200468
469 if (dsi->slave)
470 tegra_dsi_enable(dsi->slave);
471}
472
473static unsigned int tegra_dsi_get_lanes(struct tegra_dsi *dsi)
474{
475 if (dsi->master)
476 return dsi->master->lanes + dsi->lanes;
477
478 if (dsi->slave)
479 return dsi->lanes + dsi->slave->lanes;
480
481 return dsi->lanes;
Thierry Reding563eff12014-11-13 14:44:27 +0100482}
483
484static int tegra_dsi_configure(struct tegra_dsi *dsi, unsigned int pipe,
485 const struct drm_display_mode *mode)
486{
487 unsigned int hact, hsw, hbp, hfp, i, mul, div;
488 enum tegra_dsi_format format;
489 const u32 *pkt_seq;
490 u32 value;
491 int err;
Thierry Reding334ae6b2014-03-14 14:15:10 +0100492
Thierry Reding17297a22014-03-14 14:13:15 +0100493 if (dsi->flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE) {
494 DRM_DEBUG_KMS("Non-burst video mode with sync pulses\n");
495 pkt_seq = pkt_seq_video_non_burst_sync_pulses;
Thierry Reding337b4432014-11-13 15:02:46 +0100496 } else if (dsi->flags & MIPI_DSI_MODE_VIDEO) {
Thierry Reding17297a22014-03-14 14:13:15 +0100497 DRM_DEBUG_KMS("Non-burst video mode with sync events\n");
498 pkt_seq = pkt_seq_video_non_burst_sync_events;
Thierry Reding337b4432014-11-13 15:02:46 +0100499 } else {
500 DRM_DEBUG_KMS("Command mode\n");
501 pkt_seq = pkt_seq_command_mode;
Thierry Reding17297a22014-03-14 14:13:15 +0100502 }
503
Thierry Redingdec72732013-09-03 08:45:46 +0200504 err = tegra_dsi_get_muldiv(dsi->format, &mul, &div);
505 if (err < 0)
506 return err;
507
Thierry Redingf7d68892014-03-13 08:50:39 +0100508 err = tegra_dsi_get_format(dsi->format, &format);
509 if (err < 0)
510 return err;
511
Thierry Redingf7d68892014-03-13 08:50:39 +0100512 value = DSI_CONTROL_CHANNEL(0) | DSI_CONTROL_FORMAT(format) |
Thierry Redingdec72732013-09-03 08:45:46 +0200513 DSI_CONTROL_LANES(dsi->lanes - 1) |
Thierry Reding563eff12014-11-13 14:44:27 +0100514 DSI_CONTROL_SOURCE(pipe);
Thierry Redingdec72732013-09-03 08:45:46 +0200515 tegra_dsi_writel(dsi, value, DSI_CONTROL);
516
Thierry Reding976cebc2014-08-06 09:14:28 +0200517 tegra_dsi_writel(dsi, dsi->video_fifo_depth, DSI_MAX_THRESHOLD);
Thierry Redingdec72732013-09-03 08:45:46 +0200518
Thierry Reding563eff12014-11-13 14:44:27 +0100519 value = DSI_HOST_CONTROL_HS;
Thierry Redingdec72732013-09-03 08:45:46 +0200520 tegra_dsi_writel(dsi, value, DSI_HOST_CONTROL);
521
522 value = tegra_dsi_readl(dsi, DSI_CONTROL);
Thierry Reding563eff12014-11-13 14:44:27 +0100523
Alexandre Courbot0c6b1e42014-07-08 21:32:13 +0900524 if (dsi->flags & MIPI_DSI_CLOCK_NON_CONTINUOUS)
525 value |= DSI_CONTROL_HS_CLK_CTRL;
Thierry Reding563eff12014-11-13 14:44:27 +0100526
Thierry Redingdec72732013-09-03 08:45:46 +0200527 value &= ~DSI_CONTROL_TX_TRIG(3);
Thierry Reding337b4432014-11-13 15:02:46 +0100528
529 /* enable DCS commands for command mode */
530 if (dsi->flags & MIPI_DSI_MODE_VIDEO)
531 value &= ~DSI_CONTROL_DCS_ENABLE;
532 else
533 value |= DSI_CONTROL_DCS_ENABLE;
534
Thierry Redingdec72732013-09-03 08:45:46 +0200535 value |= DSI_CONTROL_VIDEO_ENABLE;
536 value &= ~DSI_CONTROL_HOST_ENABLE;
537 tegra_dsi_writel(dsi, value, DSI_CONTROL);
538
539 err = tegra_dsi_set_phy_timing(dsi);
540 if (err < 0)
541 return err;
542
543 for (i = 0; i < NUM_PKT_SEQ; i++)
544 tegra_dsi_writel(dsi, pkt_seq[i], DSI_PKT_SEQ_0_LO + i);
545
Thierry Reding337b4432014-11-13 15:02:46 +0100546 if (dsi->flags & MIPI_DSI_MODE_VIDEO) {
547 /* horizontal active pixels */
548 hact = mode->hdisplay * mul / div;
Thierry Redingdec72732013-09-03 08:45:46 +0200549
Thierry Reding337b4432014-11-13 15:02:46 +0100550 /* horizontal sync width */
551 hsw = (mode->hsync_end - mode->hsync_start) * mul / div;
552 hsw -= 10;
Thierry Redingdec72732013-09-03 08:45:46 +0200553
Thierry Reding337b4432014-11-13 15:02:46 +0100554 /* horizontal back porch */
555 hbp = (mode->htotal - mode->hsync_end) * mul / div;
556 hbp -= 14;
Thierry Redingdec72732013-09-03 08:45:46 +0200557
Thierry Reding337b4432014-11-13 15:02:46 +0100558 /* horizontal front porch */
559 hfp = (mode->hsync_start - mode->hdisplay) * mul / div;
560 hfp -= 8;
Thierry Redingdec72732013-09-03 08:45:46 +0200561
Thierry Reding337b4432014-11-13 15:02:46 +0100562 tegra_dsi_writel(dsi, hsw << 16 | 0, DSI_PKT_LEN_0_1);
563 tegra_dsi_writel(dsi, hact << 16 | hbp, DSI_PKT_LEN_2_3);
564 tegra_dsi_writel(dsi, hfp, DSI_PKT_LEN_4_5);
565 tegra_dsi_writel(dsi, 0x0f0f << 16, DSI_PKT_LEN_6_7);
Thierry Redingdec72732013-09-03 08:45:46 +0200566
Thierry Reding337b4432014-11-13 15:02:46 +0100567 /* set SOL delay (for non-burst mode only) */
568 tegra_dsi_writel(dsi, 8 * mul / div, DSI_SOL_DELAY);
Thierry Redinge94236c2014-10-07 16:10:24 +0200569
570 /* TODO: implement ganged mode */
Thierry Reding337b4432014-11-13 15:02:46 +0100571 } else {
572 u16 bytes;
573
Thierry Redinge94236c2014-10-07 16:10:24 +0200574 if (dsi->master || dsi->slave) {
575 /*
576 * For ganged mode, assume symmetric left-right mode.
577 */
578 bytes = 1 + (mode->hdisplay / 2) * mul / div;
579 } else {
580 /* 1 byte (DCS command) + pixel data */
581 bytes = 1 + mode->hdisplay * mul / div;
582 }
Thierry Reding337b4432014-11-13 15:02:46 +0100583
584 tegra_dsi_writel(dsi, 0, DSI_PKT_LEN_0_1);
585 tegra_dsi_writel(dsi, bytes << 16, DSI_PKT_LEN_2_3);
586 tegra_dsi_writel(dsi, bytes << 16, DSI_PKT_LEN_4_5);
587 tegra_dsi_writel(dsi, 0, DSI_PKT_LEN_6_7);
588
589 value = MIPI_DCS_WRITE_MEMORY_START << 8 |
590 MIPI_DCS_WRITE_MEMORY_CONTINUE;
591 tegra_dsi_writel(dsi, value, DSI_DCS_CMDS);
592
Thierry Redinge94236c2014-10-07 16:10:24 +0200593 /* set SOL delay */
594 if (dsi->master || dsi->slave) {
595 unsigned int lanes = tegra_dsi_get_lanes(dsi);
596 unsigned long delay, bclk, bclk_ganged;
597
598 /* SOL to valid, valid to FIFO and FIFO write delay */
599 delay = 4 + 4 + 2;
600 delay = DIV_ROUND_UP(delay * mul, div * lanes);
601 /* FIFO read delay */
602 delay = delay + 6;
603
604 bclk = DIV_ROUND_UP(mode->htotal * mul, div * lanes);
605 bclk_ganged = DIV_ROUND_UP(bclk * lanes / 2, lanes);
606 value = bclk - bclk_ganged + delay + 20;
607 } else {
608 /* TODO: revisit for non-ganged mode */
609 value = 8 * mul / div;
610 }
Thierry Reding337b4432014-11-13 15:02:46 +0100611
612 tegra_dsi_writel(dsi, value, DSI_SOL_DELAY);
613 }
Thierry Redingdec72732013-09-03 08:45:46 +0200614
Thierry Redinge94236c2014-10-07 16:10:24 +0200615 if (dsi->slave) {
616 err = tegra_dsi_configure(dsi->slave, pipe, mode);
617 if (err < 0)
618 return err;
619
620 /*
621 * TODO: Support modes other than symmetrical left-right
622 * split.
623 */
624 tegra_dsi_ganged_enable(dsi, 0, mode->hdisplay / 2);
625 tegra_dsi_ganged_enable(dsi->slave, mode->hdisplay / 2,
626 mode->hdisplay / 2);
627 }
628
Thierry Reding563eff12014-11-13 14:44:27 +0100629 return 0;
630}
631
632static int tegra_output_dsi_enable(struct tegra_output *output)
633{
634 struct tegra_dc *dc = to_tegra_dc(output->encoder.crtc);
635 const struct drm_display_mode *mode = &dc->base.mode;
636 struct tegra_dsi *dsi = to_dsi(output);
637 u32 value;
638 int err;
639
640 if (dsi->enabled)
641 return 0;
642
643 err = tegra_dsi_configure(dsi, dc->pipe, mode);
644 if (err < 0)
645 return err;
646
Thierry Redingdec72732013-09-03 08:45:46 +0200647 /* enable display controller */
648 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
649 value |= DSI_ENABLE;
650 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
651
Thierry Redingdec72732013-09-03 08:45:46 +0200652 value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
653 value &= ~DISP_CTRL_MODE_MASK;
654 value |= DISP_CTRL_MODE_C_DISPLAY;
655 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
656
Thierry Reding72d30282013-12-12 11:06:55 +0100657 value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
658 value |= PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
659 PW4_ENABLE | PM0_ENABLE | PM1_ENABLE;
660 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
661
Thierry Redingdec72732013-09-03 08:45:46 +0200662 tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
663 tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
664
665 /* enable DSI controller */
Thierry Reding563eff12014-11-13 14:44:27 +0100666 tegra_dsi_enable(dsi);
Thierry Redingdec72732013-09-03 08:45:46 +0200667
Thierry Reding334ae6b2014-03-14 14:15:10 +0100668 dsi->enabled = true;
669
Thierry Redingdec72732013-09-03 08:45:46 +0200670 return 0;
671}
672
Thierry Reding563eff12014-11-13 14:44:27 +0100673static int tegra_dsi_wait_idle(struct tegra_dsi *dsi, unsigned long timeout)
674{
675 u32 value;
676
677 timeout = jiffies + msecs_to_jiffies(timeout);
678
679 while (time_before(jiffies, timeout)) {
680 value = tegra_dsi_readl(dsi, DSI_STATUS);
681 if (value & DSI_STATUS_IDLE)
682 return 0;
683
684 usleep_range(1000, 2000);
685 }
686
687 return -ETIMEDOUT;
688}
689
690static void tegra_dsi_video_disable(struct tegra_dsi *dsi)
691{
692 u32 value;
693
694 value = tegra_dsi_readl(dsi, DSI_CONTROL);
695 value &= ~DSI_CONTROL_VIDEO_ENABLE;
696 tegra_dsi_writel(dsi, value, DSI_CONTROL);
Thierry Redinge94236c2014-10-07 16:10:24 +0200697
698 if (dsi->slave)
699 tegra_dsi_video_disable(dsi->slave);
700}
701
702static void tegra_dsi_ganged_disable(struct tegra_dsi *dsi)
703{
704 tegra_dsi_writel(dsi, 0, DSI_GANGED_MODE_START);
705 tegra_dsi_writel(dsi, 0, DSI_GANGED_MODE_SIZE);
706 tegra_dsi_writel(dsi, 0, DSI_GANGED_MODE_CONTROL);
Thierry Reding563eff12014-11-13 14:44:27 +0100707}
708
709static void tegra_dsi_disable(struct tegra_dsi *dsi)
710{
711 u32 value;
712
Thierry Redinge94236c2014-10-07 16:10:24 +0200713 if (dsi->slave) {
714 tegra_dsi_ganged_disable(dsi->slave);
715 tegra_dsi_ganged_disable(dsi);
716 }
717
Thierry Reding563eff12014-11-13 14:44:27 +0100718 value = tegra_dsi_readl(dsi, DSI_POWER_CONTROL);
719 value &= ~DSI_POWER_CONTROL_ENABLE;
720 tegra_dsi_writel(dsi, value, DSI_POWER_CONTROL);
721
Thierry Redinge94236c2014-10-07 16:10:24 +0200722 if (dsi->slave)
723 tegra_dsi_disable(dsi->slave);
724
Thierry Reding563eff12014-11-13 14:44:27 +0100725 usleep_range(5000, 10000);
726}
727
Thierry Redingdec72732013-09-03 08:45:46 +0200728static int tegra_output_dsi_disable(struct tegra_output *output)
729{
730 struct tegra_dc *dc = to_tegra_dc(output->encoder.crtc);
731 struct tegra_dsi *dsi = to_dsi(output);
732 unsigned long value;
Thierry Reding563eff12014-11-13 14:44:27 +0100733 int err;
Thierry Redingdec72732013-09-03 08:45:46 +0200734
Thierry Reding334ae6b2014-03-14 14:15:10 +0100735 if (!dsi->enabled)
736 return 0;
737
Thierry Reding563eff12014-11-13 14:44:27 +0100738 tegra_dsi_video_disable(dsi);
Thierry Redingdec72732013-09-03 08:45:46 +0200739
740 /*
Thierry Reding72d30282013-12-12 11:06:55 +0100741 * The following accesses registers of the display controller, so make
742 * sure it's only executed when the output is attached to one.
Thierry Redingdec72732013-09-03 08:45:46 +0200743 */
744 if (dc) {
Thierry Reding72d30282013-12-12 11:06:55 +0100745 value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
746 value &= ~(PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
747 PW4_ENABLE | PM0_ENABLE | PM1_ENABLE);
748 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
749
Thierry Redingdec72732013-09-03 08:45:46 +0200750 value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
751 value &= ~DISP_CTRL_MODE_MASK;
752 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
753
754 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
755 value &= ~DSI_ENABLE;
756 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
Thierry Reding72d30282013-12-12 11:06:55 +0100757
758 tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
759 tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
Thierry Redingdec72732013-09-03 08:45:46 +0200760 }
761
Thierry Reding563eff12014-11-13 14:44:27 +0100762 err = tegra_dsi_wait_idle(dsi, 100);
763 if (err < 0)
764 dev_dbg(dsi->dev, "failed to idle DSI: %d\n", err);
765
766 tegra_dsi_disable(dsi);
767
Thierry Reding334ae6b2014-03-14 14:15:10 +0100768 dsi->enabled = false;
769
Thierry Redingdec72732013-09-03 08:45:46 +0200770 return 0;
771}
772
Thierry Reding3f6b4062014-11-13 14:50:33 +0100773static void tegra_dsi_set_timeout(struct tegra_dsi *dsi, unsigned long bclk,
774 unsigned int vrefresh)
775{
776 unsigned int timeout;
777 u32 value;
778
779 /* one frame high-speed transmission timeout */
780 timeout = (bclk / vrefresh) / 512;
781 value = DSI_TIMEOUT_LRX(0x2000) | DSI_TIMEOUT_HTX(timeout);
782 tegra_dsi_writel(dsi, value, DSI_TIMEOUT_0);
783
784 /* 2 ms peripheral timeout for panel */
785 timeout = 2 * bclk / 512 * 1000;
786 value = DSI_TIMEOUT_PR(timeout) | DSI_TIMEOUT_TA(0x2000);
787 tegra_dsi_writel(dsi, value, DSI_TIMEOUT_1);
788
789 value = DSI_TALLY_TA(0) | DSI_TALLY_LRX(0) | DSI_TALLY_HTX(0);
790 tegra_dsi_writel(dsi, value, DSI_TO_TALLY);
Thierry Redinge94236c2014-10-07 16:10:24 +0200791
792 if (dsi->slave)
793 tegra_dsi_set_timeout(dsi->slave, bclk, vrefresh);
Thierry Reding3f6b4062014-11-13 14:50:33 +0100794}
795
Thierry Redingdec72732013-09-03 08:45:46 +0200796static int tegra_output_dsi_setup_clock(struct tegra_output *output,
Thierry Reding91eded92014-03-26 13:32:21 +0100797 struct clk *clk, unsigned long pclk,
798 unsigned int *divp)
Thierry Redingdec72732013-09-03 08:45:46 +0200799{
800 struct tegra_dc *dc = to_tegra_dc(output->encoder.crtc);
801 struct drm_display_mode *mode = &dc->base.mode;
Thierry Redingdec72732013-09-03 08:45:46 +0200802 struct tegra_dsi *dsi = to_dsi(output);
Thierry Redinge94236c2014-10-07 16:10:24 +0200803 unsigned int mul, div, vrefresh, lanes;
Thierry Reding3f6b4062014-11-13 14:50:33 +0100804 unsigned long bclk, plld;
Thierry Redingdec72732013-09-03 08:45:46 +0200805 int err;
806
Thierry Redinge94236c2014-10-07 16:10:24 +0200807 lanes = tegra_dsi_get_lanes(dsi);
808
Thierry Redingdec72732013-09-03 08:45:46 +0200809 err = tegra_dsi_get_muldiv(dsi->format, &mul, &div);
810 if (err < 0)
811 return err;
812
Thierry Redinge94236c2014-10-07 16:10:24 +0200813 DRM_DEBUG_KMS("mul: %u, div: %u, lanes: %u\n", mul, div, lanes);
Thierry Redingdec72732013-09-03 08:45:46 +0200814 vrefresh = drm_mode_vrefresh(mode);
Thierry Reding91eded92014-03-26 13:32:21 +0100815 DRM_DEBUG_KMS("vrefresh: %u\n", vrefresh);
Thierry Redingdec72732013-09-03 08:45:46 +0200816
Thierry Reding91eded92014-03-26 13:32:21 +0100817 /* compute byte clock */
Thierry Redinge94236c2014-10-07 16:10:24 +0200818 bclk = (pclk * mul) / (div * lanes);
Thierry Reding91eded92014-03-26 13:32:21 +0100819
820 /*
821 * Compute bit clock and round up to the next MHz.
822 */
823 plld = DIV_ROUND_UP(bclk * 8, 1000000) * 1000000;
824
825 /*
826 * We divide the frequency by two here, but we make up for that by
827 * setting the shift clock divider (further below) to half of the
828 * correct value.
829 */
830 plld /= 2;
Thierry Redingdec72732013-09-03 08:45:46 +0200831
832 err = clk_set_parent(clk, dsi->clk_parent);
833 if (err < 0) {
834 dev_err(dsi->dev, "failed to set parent clock: %d\n", err);
835 return err;
836 }
837
Thierry Reding91eded92014-03-26 13:32:21 +0100838 err = clk_set_rate(dsi->clk_parent, plld);
Thierry Redingdec72732013-09-03 08:45:46 +0200839 if (err < 0) {
840 dev_err(dsi->dev, "failed to set base clock rate to %lu Hz\n",
Thierry Reding91eded92014-03-26 13:32:21 +0100841 plld);
Thierry Redingdec72732013-09-03 08:45:46 +0200842 return err;
843 }
844
845 /*
Thierry Reding91eded92014-03-26 13:32:21 +0100846 * Derive pixel clock from bit clock using the shift clock divider.
847 * Note that this is only half of what we would expect, but we need
848 * that to make up for the fact that we divided the bit clock by a
849 * factor of two above.
850 *
851 * It's not clear exactly why this is necessary, but the display is
852 * not working properly otherwise. Perhaps the PLLs cannot generate
853 * frequencies sufficiently high.
854 */
Thierry Redinge94236c2014-10-07 16:10:24 +0200855 *divp = ((8 * mul) / (div * lanes)) - 2;
Thierry Reding91eded92014-03-26 13:32:21 +0100856
857 /*
Thierry Redingdec72732013-09-03 08:45:46 +0200858 * XXX: Move the below somewhere else so that we don't need to have
859 * access to the vrefresh in this function?
860 */
Thierry Reding3f6b4062014-11-13 14:50:33 +0100861 tegra_dsi_set_timeout(dsi, bclk, vrefresh);
Thierry Redingdec72732013-09-03 08:45:46 +0200862
863 return 0;
864}
865
866static int tegra_output_dsi_check_mode(struct tegra_output *output,
867 struct drm_display_mode *mode,
868 enum drm_mode_status *status)
869{
870 /*
871 * FIXME: For now, always assume that the mode is okay.
872 */
873
874 *status = MODE_OK;
875
876 return 0;
877}
878
879static const struct tegra_output_ops dsi_ops = {
880 .enable = tegra_output_dsi_enable,
881 .disable = tegra_output_dsi_disable,
882 .setup_clock = tegra_output_dsi_setup_clock,
883 .check_mode = tegra_output_dsi_check_mode,
884};
885
886static int tegra_dsi_pad_enable(struct tegra_dsi *dsi)
887{
888 unsigned long value;
889
890 value = DSI_PAD_CONTROL_VS1_PULLDN(0) | DSI_PAD_CONTROL_VS1_PDIO(0);
891 tegra_dsi_writel(dsi, value, DSI_PAD_CONTROL_0);
892
893 return 0;
894}
895
896static int tegra_dsi_pad_calibrate(struct tegra_dsi *dsi)
897{
Thierry Reding183ef282014-11-13 14:27:29 +0100898 u32 value;
Thierry Redingdec72732013-09-03 08:45:46 +0200899
900 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_0);
901 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_1);
902 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_2);
903 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_3);
904 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_4);
905
906 /* start calibration */
907 tegra_dsi_pad_enable(dsi);
908
909 value = DSI_PAD_SLEW_UP(0x7) | DSI_PAD_SLEW_DN(0x7) |
910 DSI_PAD_LP_UP(0x1) | DSI_PAD_LP_DN(0x1) |
911 DSI_PAD_OUT_CLK(0x0);
912 tegra_dsi_writel(dsi, value, DSI_PAD_CONTROL_2);
913
914 return tegra_mipi_calibrate(dsi->mipi);
915}
916
917static int tegra_dsi_init(struct host1x_client *client)
918{
Thierry Reding9910f5c2014-05-22 09:57:15 +0200919 struct drm_device *drm = dev_get_drvdata(client->parent);
Thierry Redingdec72732013-09-03 08:45:46 +0200920 struct tegra_dsi *dsi = host1x_client_to_dsi(client);
Thierry Redingdec72732013-09-03 08:45:46 +0200921 int err;
922
Thierry Redinge94236c2014-10-07 16:10:24 +0200923 /* Gangsters must not register their own outputs. */
924 if (!dsi->master) {
925 dsi->output.type = TEGRA_OUTPUT_DSI;
926 dsi->output.dev = client->dev;
927 dsi->output.ops = &dsi_ops;
Thierry Redingdec72732013-09-03 08:45:46 +0200928
Thierry Redinge94236c2014-10-07 16:10:24 +0200929 err = tegra_output_init(drm, &dsi->output);
930 if (err < 0) {
931 dev_err(client->dev, "output setup failed: %d\n", err);
932 return err;
933 }
Thierry Redingdec72732013-09-03 08:45:46 +0200934 }
935
936 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
Thierry Reding9910f5c2014-05-22 09:57:15 +0200937 err = tegra_dsi_debugfs_init(dsi, drm->primary);
Thierry Redingdec72732013-09-03 08:45:46 +0200938 if (err < 0)
939 dev_err(dsi->dev, "debugfs setup failed: %d\n", err);
940 }
941
Thierry Redingdec72732013-09-03 08:45:46 +0200942 return 0;
943}
944
945static int tegra_dsi_exit(struct host1x_client *client)
946{
947 struct tegra_dsi *dsi = host1x_client_to_dsi(client);
948 int err;
949
950 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
951 err = tegra_dsi_debugfs_exit(dsi);
952 if (err < 0)
953 dev_err(dsi->dev, "debugfs cleanup failed: %d\n", err);
954 }
955
Thierry Redinge94236c2014-10-07 16:10:24 +0200956 if (!dsi->master) {
957 err = tegra_output_disable(&dsi->output);
958 if (err < 0) {
959 dev_err(client->dev, "output failed to disable: %d\n",
960 err);
961 return err;
962 }
Thierry Redingdec72732013-09-03 08:45:46 +0200963
Thierry Redinge94236c2014-10-07 16:10:24 +0200964 err = tegra_output_exit(&dsi->output);
965 if (err < 0) {
966 dev_err(client->dev, "output cleanup failed: %d\n",
967 err);
968 return err;
969 }
Thierry Redingdec72732013-09-03 08:45:46 +0200970 }
971
972 return 0;
973}
974
975static const struct host1x_client_ops dsi_client_ops = {
976 .init = tegra_dsi_init,
977 .exit = tegra_dsi_exit,
978};
979
980static int tegra_dsi_setup_clocks(struct tegra_dsi *dsi)
981{
982 struct clk *parent;
983 int err;
984
985 parent = clk_get_parent(dsi->clk);
986 if (!parent)
987 return -EINVAL;
988
989 err = clk_set_parent(parent, dsi->clk_parent);
990 if (err < 0)
991 return err;
992
993 return 0;
994}
995
Thierry Redinge94236c2014-10-07 16:10:24 +0200996static int tegra_dsi_ganged_setup(struct tegra_dsi *dsi)
997{
998 struct clk *parent;
999 int err;
1000
1001 /* make sure both DSI controllers share the same PLL */
1002 parent = clk_get_parent(dsi->slave->clk);
1003 if (!parent)
1004 return -EINVAL;
1005
1006 err = clk_set_parent(parent, dsi->clk_parent);
1007 if (err < 0)
1008 return err;
1009
1010 return 0;
1011}
1012
Thierry Redingdec72732013-09-03 08:45:46 +02001013static int tegra_dsi_host_attach(struct mipi_dsi_host *host,
1014 struct mipi_dsi_device *device)
1015{
1016 struct tegra_dsi *dsi = host_to_tegra(host);
Thierry Redingdec72732013-09-03 08:45:46 +02001017
Thierry Reding17297a22014-03-14 14:13:15 +01001018 dsi->flags = device->mode_flags;
Thierry Redingdec72732013-09-03 08:45:46 +02001019 dsi->format = device->format;
1020 dsi->lanes = device->lanes;
1021
Thierry Redinge94236c2014-10-07 16:10:24 +02001022 if (dsi->slave) {
1023 int err;
1024
1025 dev_dbg(dsi->dev, "attaching dual-channel device %s\n",
1026 dev_name(&device->dev));
1027
1028 err = tegra_dsi_ganged_setup(dsi);
1029 if (err < 0) {
1030 dev_err(dsi->dev, "failed to set up ganged mode: %d\n",
1031 err);
1032 return err;
1033 }
1034 }
1035
1036 /*
1037 * Slaves don't have a panel associated with them, so they provide
1038 * merely the second channel.
1039 */
1040 if (!dsi->master) {
1041 struct tegra_output *output = &dsi->output;
1042
1043 output->panel = of_drm_find_panel(device->dev.of_node);
1044 if (output->panel && output->connector.dev) {
1045 drm_panel_attach(output->panel, &output->connector);
Thierry Redingdec72732013-09-03 08:45:46 +02001046 drm_helper_hpd_irq_event(output->connector.dev);
Thierry Redinge94236c2014-10-07 16:10:24 +02001047 }
Thierry Redingdec72732013-09-03 08:45:46 +02001048 }
1049
1050 return 0;
1051}
1052
1053static int tegra_dsi_host_detach(struct mipi_dsi_host *host,
1054 struct mipi_dsi_device *device)
1055{
1056 struct tegra_dsi *dsi = host_to_tegra(host);
1057 struct tegra_output *output = &dsi->output;
1058
1059 if (output->panel && &device->dev == output->panel->dev) {
Thierry Redingba3df972014-11-13 14:54:01 +01001060 output->panel = NULL;
1061
Thierry Redingdec72732013-09-03 08:45:46 +02001062 if (output->connector.dev)
1063 drm_helper_hpd_irq_event(output->connector.dev);
Thierry Redingdec72732013-09-03 08:45:46 +02001064 }
1065
1066 return 0;
1067}
1068
1069static const struct mipi_dsi_host_ops tegra_dsi_host_ops = {
1070 .attach = tegra_dsi_host_attach,
1071 .detach = tegra_dsi_host_detach,
1072};
1073
Thierry Redinge94236c2014-10-07 16:10:24 +02001074static int tegra_dsi_ganged_probe(struct tegra_dsi *dsi)
1075{
1076 struct device_node *np;
1077
1078 np = of_parse_phandle(dsi->dev->of_node, "nvidia,ganged-mode", 0);
1079 if (np) {
1080 struct platform_device *gangster = of_find_device_by_node(np);
1081
1082 dsi->slave = platform_get_drvdata(gangster);
1083 of_node_put(np);
1084
1085 if (!dsi->slave)
1086 return -EPROBE_DEFER;
1087
1088 dsi->slave->master = dsi;
1089 }
1090
1091 return 0;
1092}
1093
Thierry Redingdec72732013-09-03 08:45:46 +02001094static int tegra_dsi_probe(struct platform_device *pdev)
1095{
1096 struct tegra_dsi *dsi;
1097 struct resource *regs;
1098 int err;
1099
1100 dsi = devm_kzalloc(&pdev->dev, sizeof(*dsi), GFP_KERNEL);
1101 if (!dsi)
1102 return -ENOMEM;
1103
1104 dsi->output.dev = dsi->dev = &pdev->dev;
Thierry Reding976cebc2014-08-06 09:14:28 +02001105 dsi->video_fifo_depth = 1920;
1106 dsi->host_fifo_depth = 64;
Thierry Redingdec72732013-09-03 08:45:46 +02001107
Thierry Redinge94236c2014-10-07 16:10:24 +02001108 err = tegra_dsi_ganged_probe(dsi);
1109 if (err < 0)
1110 return err;
1111
Thierry Redingdec72732013-09-03 08:45:46 +02001112 err = tegra_output_probe(&dsi->output);
1113 if (err < 0)
1114 return err;
1115
Thierry Redingba3df972014-11-13 14:54:01 +01001116 dsi->output.connector.polled = DRM_CONNECTOR_POLL_HPD;
1117
Thierry Redingdec72732013-09-03 08:45:46 +02001118 /*
1119 * Assume these values by default. When a DSI peripheral driver
1120 * attaches to the DSI host, the parameters will be taken from
1121 * the attached device.
1122 */
Thierry Reding17297a22014-03-14 14:13:15 +01001123 dsi->flags = MIPI_DSI_MODE_VIDEO;
Thierry Redingdec72732013-09-03 08:45:46 +02001124 dsi->format = MIPI_DSI_FMT_RGB888;
1125 dsi->lanes = 4;
1126
1127 dsi->rst = devm_reset_control_get(&pdev->dev, "dsi");
1128 if (IS_ERR(dsi->rst))
1129 return PTR_ERR(dsi->rst);
1130
Thierry Reding183ef282014-11-13 14:27:29 +01001131 err = reset_control_deassert(dsi->rst);
1132 if (err < 0) {
1133 dev_err(&pdev->dev, "failed to bring DSI out of reset: %d\n",
1134 err);
1135 return err;
1136 }
1137
Thierry Redingdec72732013-09-03 08:45:46 +02001138 dsi->clk = devm_clk_get(&pdev->dev, NULL);
1139 if (IS_ERR(dsi->clk)) {
1140 dev_err(&pdev->dev, "cannot get DSI clock\n");
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001141 err = PTR_ERR(dsi->clk);
1142 goto reset;
Thierry Redingdec72732013-09-03 08:45:46 +02001143 }
1144
1145 err = clk_prepare_enable(dsi->clk);
1146 if (err < 0) {
1147 dev_err(&pdev->dev, "cannot enable DSI clock\n");
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001148 goto reset;
Thierry Redingdec72732013-09-03 08:45:46 +02001149 }
1150
1151 dsi->clk_lp = devm_clk_get(&pdev->dev, "lp");
1152 if (IS_ERR(dsi->clk_lp)) {
1153 dev_err(&pdev->dev, "cannot get low-power clock\n");
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001154 err = PTR_ERR(dsi->clk_lp);
1155 goto disable_clk;
Thierry Redingdec72732013-09-03 08:45:46 +02001156 }
1157
1158 err = clk_prepare_enable(dsi->clk_lp);
1159 if (err < 0) {
1160 dev_err(&pdev->dev, "cannot enable low-power clock\n");
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001161 goto disable_clk;
Thierry Redingdec72732013-09-03 08:45:46 +02001162 }
1163
1164 dsi->clk_parent = devm_clk_get(&pdev->dev, "parent");
1165 if (IS_ERR(dsi->clk_parent)) {
1166 dev_err(&pdev->dev, "cannot get parent clock\n");
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001167 err = PTR_ERR(dsi->clk_parent);
1168 goto disable_clk_lp;
Thierry Redingdec72732013-09-03 08:45:46 +02001169 }
1170
Thierry Reding3b077af2014-03-14 14:07:50 +01001171 dsi->vdd = devm_regulator_get(&pdev->dev, "avdd-dsi-csi");
1172 if (IS_ERR(dsi->vdd)) {
1173 dev_err(&pdev->dev, "cannot get VDD supply\n");
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001174 err = PTR_ERR(dsi->vdd);
1175 goto disable_clk_lp;
Thierry Reding3b077af2014-03-14 14:07:50 +01001176 }
1177
1178 err = regulator_enable(dsi->vdd);
1179 if (err < 0) {
1180 dev_err(&pdev->dev, "cannot enable VDD supply\n");
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001181 goto disable_clk_lp;
Thierry Reding3b077af2014-03-14 14:07:50 +01001182 }
1183
Thierry Redingdec72732013-09-03 08:45:46 +02001184 err = tegra_dsi_setup_clocks(dsi);
1185 if (err < 0) {
1186 dev_err(&pdev->dev, "cannot setup clocks\n");
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001187 goto disable_vdd;
Thierry Redingdec72732013-09-03 08:45:46 +02001188 }
1189
1190 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1191 dsi->regs = devm_ioremap_resource(&pdev->dev, regs);
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001192 if (IS_ERR(dsi->regs)) {
1193 err = PTR_ERR(dsi->regs);
1194 goto disable_vdd;
1195 }
Thierry Redingdec72732013-09-03 08:45:46 +02001196
Thierry Redingdec72732013-09-03 08:45:46 +02001197 dsi->mipi = tegra_mipi_request(&pdev->dev);
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001198 if (IS_ERR(dsi->mipi)) {
1199 err = PTR_ERR(dsi->mipi);
1200 goto disable_vdd;
1201 }
Thierry Redingdec72732013-09-03 08:45:46 +02001202
Thierry Reding183ef282014-11-13 14:27:29 +01001203 err = tegra_dsi_pad_calibrate(dsi);
1204 if (err < 0) {
1205 dev_err(dsi->dev, "MIPI calibration failed: %d\n", err);
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001206 goto mipi_free;
Thierry Reding183ef282014-11-13 14:27:29 +01001207 }
1208
Thierry Redingdec72732013-09-03 08:45:46 +02001209 dsi->host.ops = &tegra_dsi_host_ops;
1210 dsi->host.dev = &pdev->dev;
1211
1212 err = mipi_dsi_host_register(&dsi->host);
1213 if (err < 0) {
1214 dev_err(&pdev->dev, "failed to register DSI host: %d\n", err);
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001215 goto mipi_free;
Thierry Redingdec72732013-09-03 08:45:46 +02001216 }
1217
1218 INIT_LIST_HEAD(&dsi->client.list);
1219 dsi->client.ops = &dsi_client_ops;
1220 dsi->client.dev = &pdev->dev;
1221
1222 err = host1x_client_register(&dsi->client);
1223 if (err < 0) {
1224 dev_err(&pdev->dev, "failed to register host1x client: %d\n",
1225 err);
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001226 goto unregister;
Thierry Redingdec72732013-09-03 08:45:46 +02001227 }
1228
1229 platform_set_drvdata(pdev, dsi);
1230
1231 return 0;
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001232
1233unregister:
1234 mipi_dsi_host_unregister(&dsi->host);
1235mipi_free:
1236 tegra_mipi_free(dsi->mipi);
1237disable_vdd:
1238 regulator_disable(dsi->vdd);
1239disable_clk_lp:
1240 clk_disable_unprepare(dsi->clk_lp);
1241disable_clk:
1242 clk_disable_unprepare(dsi->clk);
1243reset:
1244 reset_control_assert(dsi->rst);
1245 return err;
Thierry Redingdec72732013-09-03 08:45:46 +02001246}
1247
1248static int tegra_dsi_remove(struct platform_device *pdev)
1249{
1250 struct tegra_dsi *dsi = platform_get_drvdata(pdev);
1251 int err;
1252
1253 err = host1x_client_unregister(&dsi->client);
1254 if (err < 0) {
1255 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
1256 err);
1257 return err;
1258 }
1259
1260 mipi_dsi_host_unregister(&dsi->host);
1261 tegra_mipi_free(dsi->mipi);
1262
Thierry Reding3b077af2014-03-14 14:07:50 +01001263 regulator_disable(dsi->vdd);
Thierry Redingdec72732013-09-03 08:45:46 +02001264 clk_disable_unprepare(dsi->clk_lp);
1265 clk_disable_unprepare(dsi->clk);
Thierry Redingcb825d82014-03-14 14:25:43 +01001266 reset_control_assert(dsi->rst);
Thierry Redingdec72732013-09-03 08:45:46 +02001267
1268 err = tegra_output_remove(&dsi->output);
1269 if (err < 0) {
1270 dev_err(&pdev->dev, "failed to remove output: %d\n", err);
1271 return err;
1272 }
1273
1274 return 0;
1275}
1276
1277static const struct of_device_id tegra_dsi_of_match[] = {
1278 { .compatible = "nvidia,tegra114-dsi", },
1279 { },
1280};
Stephen Warrenef707282014-06-18 16:21:55 -06001281MODULE_DEVICE_TABLE(of, tegra_dsi_of_match);
Thierry Redingdec72732013-09-03 08:45:46 +02001282
1283struct platform_driver tegra_dsi_driver = {
1284 .driver = {
1285 .name = "tegra-dsi",
1286 .of_match_table = tegra_dsi_of_match,
1287 },
1288 .probe = tegra_dsi_probe,
1289 .remove = tegra_dsi_remove,
1290};