blob: 584b771d8b2fb63860e9eab50b047af31e9eec9f [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>
14#include <linux/platform_device.h>
15#include <linux/reset.h>
16
Thierry Reding3b077af2014-03-14 14:07:50 +010017#include <linux/regulator/consumer.h>
18
Thierry Redingdec72732013-09-03 08:45:46 +020019#include <drm/drm_mipi_dsi.h>
20#include <drm/drm_panel.h>
21
22#include <video/mipi_display.h>
23
24#include "dc.h"
25#include "drm.h"
26#include "dsi.h"
27#include "mipi-phy.h"
28
Thierry Redingdec72732013-09-03 08:45:46 +020029struct tegra_dsi {
30 struct host1x_client client;
31 struct tegra_output output;
32 struct device *dev;
33
34 void __iomem *regs;
35
36 struct reset_control *rst;
37 struct clk *clk_parent;
38 struct clk *clk_lp;
39 struct clk *clk;
40
41 struct drm_info_list *debugfs_files;
42 struct drm_minor *minor;
43 struct dentry *debugfs;
44
Thierry Reding17297a22014-03-14 14:13:15 +010045 unsigned long flags;
Thierry Redingdec72732013-09-03 08:45:46 +020046 enum mipi_dsi_pixel_format format;
47 unsigned int lanes;
48
49 struct tegra_mipi_device *mipi;
50 struct mipi_dsi_host host;
Thierry Reding3b077af2014-03-14 14:07:50 +010051
52 struct regulator *vdd;
Thierry Reding334ae6b2014-03-14 14:15:10 +010053 bool enabled;
Thierry Reding976cebc2014-08-06 09:14:28 +020054
55 unsigned int video_fifo_depth;
56 unsigned int host_fifo_depth;
Thierry Redingdec72732013-09-03 08:45:46 +020057};
58
59static inline struct tegra_dsi *
60host1x_client_to_dsi(struct host1x_client *client)
61{
62 return container_of(client, struct tegra_dsi, client);
63}
64
65static inline struct tegra_dsi *host_to_tegra(struct mipi_dsi_host *host)
66{
67 return container_of(host, struct tegra_dsi, host);
68}
69
70static inline struct tegra_dsi *to_dsi(struct tegra_output *output)
71{
72 return container_of(output, struct tegra_dsi, output);
73}
74
75static inline unsigned long tegra_dsi_readl(struct tegra_dsi *dsi,
76 unsigned long reg)
77{
78 return readl(dsi->regs + (reg << 2));
79}
80
81static inline void tegra_dsi_writel(struct tegra_dsi *dsi, unsigned long value,
82 unsigned long reg)
83{
84 writel(value, dsi->regs + (reg << 2));
85}
86
87static int tegra_dsi_show_regs(struct seq_file *s, void *data)
88{
89 struct drm_info_node *node = s->private;
90 struct tegra_dsi *dsi = node->info_ent->data;
91
92#define DUMP_REG(name) \
93 seq_printf(s, "%-32s %#05x %08lx\n", #name, name, \
94 tegra_dsi_readl(dsi, name))
95
96 DUMP_REG(DSI_INCR_SYNCPT);
97 DUMP_REG(DSI_INCR_SYNCPT_CONTROL);
98 DUMP_REG(DSI_INCR_SYNCPT_ERROR);
99 DUMP_REG(DSI_CTXSW);
100 DUMP_REG(DSI_RD_DATA);
101 DUMP_REG(DSI_WR_DATA);
102 DUMP_REG(DSI_POWER_CONTROL);
103 DUMP_REG(DSI_INT_ENABLE);
104 DUMP_REG(DSI_INT_STATUS);
105 DUMP_REG(DSI_INT_MASK);
106 DUMP_REG(DSI_HOST_CONTROL);
107 DUMP_REG(DSI_CONTROL);
108 DUMP_REG(DSI_SOL_DELAY);
109 DUMP_REG(DSI_MAX_THRESHOLD);
110 DUMP_REG(DSI_TRIGGER);
111 DUMP_REG(DSI_TX_CRC);
112 DUMP_REG(DSI_STATUS);
113
114 DUMP_REG(DSI_INIT_SEQ_CONTROL);
115 DUMP_REG(DSI_INIT_SEQ_DATA_0);
116 DUMP_REG(DSI_INIT_SEQ_DATA_1);
117 DUMP_REG(DSI_INIT_SEQ_DATA_2);
118 DUMP_REG(DSI_INIT_SEQ_DATA_3);
119 DUMP_REG(DSI_INIT_SEQ_DATA_4);
120 DUMP_REG(DSI_INIT_SEQ_DATA_5);
121 DUMP_REG(DSI_INIT_SEQ_DATA_6);
122 DUMP_REG(DSI_INIT_SEQ_DATA_7);
123
124 DUMP_REG(DSI_PKT_SEQ_0_LO);
125 DUMP_REG(DSI_PKT_SEQ_0_HI);
126 DUMP_REG(DSI_PKT_SEQ_1_LO);
127 DUMP_REG(DSI_PKT_SEQ_1_HI);
128 DUMP_REG(DSI_PKT_SEQ_2_LO);
129 DUMP_REG(DSI_PKT_SEQ_2_HI);
130 DUMP_REG(DSI_PKT_SEQ_3_LO);
131 DUMP_REG(DSI_PKT_SEQ_3_HI);
132 DUMP_REG(DSI_PKT_SEQ_4_LO);
133 DUMP_REG(DSI_PKT_SEQ_4_HI);
134 DUMP_REG(DSI_PKT_SEQ_5_LO);
135 DUMP_REG(DSI_PKT_SEQ_5_HI);
136
137 DUMP_REG(DSI_DCS_CMDS);
138
139 DUMP_REG(DSI_PKT_LEN_0_1);
140 DUMP_REG(DSI_PKT_LEN_2_3);
141 DUMP_REG(DSI_PKT_LEN_4_5);
142 DUMP_REG(DSI_PKT_LEN_6_7);
143
144 DUMP_REG(DSI_PHY_TIMING_0);
145 DUMP_REG(DSI_PHY_TIMING_1);
146 DUMP_REG(DSI_PHY_TIMING_2);
147 DUMP_REG(DSI_BTA_TIMING);
148
149 DUMP_REG(DSI_TIMEOUT_0);
150 DUMP_REG(DSI_TIMEOUT_1);
151 DUMP_REG(DSI_TO_TALLY);
152
153 DUMP_REG(DSI_PAD_CONTROL_0);
154 DUMP_REG(DSI_PAD_CONTROL_CD);
155 DUMP_REG(DSI_PAD_CD_STATUS);
156 DUMP_REG(DSI_VIDEO_MODE_CONTROL);
157 DUMP_REG(DSI_PAD_CONTROL_1);
158 DUMP_REG(DSI_PAD_CONTROL_2);
159 DUMP_REG(DSI_PAD_CONTROL_3);
160 DUMP_REG(DSI_PAD_CONTROL_4);
161
162 DUMP_REG(DSI_GANGED_MODE_CONTROL);
163 DUMP_REG(DSI_GANGED_MODE_START);
164 DUMP_REG(DSI_GANGED_MODE_SIZE);
165
166 DUMP_REG(DSI_RAW_DATA_BYTE_COUNT);
167 DUMP_REG(DSI_ULTRA_LOW_POWER_CONTROL);
168
169 DUMP_REG(DSI_INIT_SEQ_DATA_8);
170 DUMP_REG(DSI_INIT_SEQ_DATA_9);
171 DUMP_REG(DSI_INIT_SEQ_DATA_10);
172 DUMP_REG(DSI_INIT_SEQ_DATA_11);
173 DUMP_REG(DSI_INIT_SEQ_DATA_12);
174 DUMP_REG(DSI_INIT_SEQ_DATA_13);
175 DUMP_REG(DSI_INIT_SEQ_DATA_14);
176 DUMP_REG(DSI_INIT_SEQ_DATA_15);
177
178#undef DUMP_REG
179
180 return 0;
181}
182
183static struct drm_info_list debugfs_files[] = {
184 { "regs", tegra_dsi_show_regs, 0, NULL },
185};
186
187static int tegra_dsi_debugfs_init(struct tegra_dsi *dsi,
188 struct drm_minor *minor)
189{
190 const char *name = dev_name(dsi->dev);
191 unsigned int i;
192 int err;
193
194 dsi->debugfs = debugfs_create_dir(name, minor->debugfs_root);
195 if (!dsi->debugfs)
196 return -ENOMEM;
197
198 dsi->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
199 GFP_KERNEL);
200 if (!dsi->debugfs_files) {
201 err = -ENOMEM;
202 goto remove;
203 }
204
205 for (i = 0; i < ARRAY_SIZE(debugfs_files); i++)
206 dsi->debugfs_files[i].data = dsi;
207
208 err = drm_debugfs_create_files(dsi->debugfs_files,
209 ARRAY_SIZE(debugfs_files),
210 dsi->debugfs, minor);
211 if (err < 0)
212 goto free;
213
214 dsi->minor = minor;
215
216 return 0;
217
218free:
219 kfree(dsi->debugfs_files);
220 dsi->debugfs_files = NULL;
221remove:
222 debugfs_remove(dsi->debugfs);
223 dsi->debugfs = NULL;
224
225 return err;
226}
227
228static int tegra_dsi_debugfs_exit(struct tegra_dsi *dsi)
229{
230 drm_debugfs_remove_files(dsi->debugfs_files, ARRAY_SIZE(debugfs_files),
231 dsi->minor);
232 dsi->minor = NULL;
233
234 kfree(dsi->debugfs_files);
235 dsi->debugfs_files = NULL;
236
237 debugfs_remove(dsi->debugfs);
238 dsi->debugfs = NULL;
239
240 return 0;
241}
242
243#define PKT_ID0(id) ((((id) & 0x3f) << 3) | (1 << 9))
244#define PKT_LEN0(len) (((len) & 0x07) << 0)
245#define PKT_ID1(id) ((((id) & 0x3f) << 13) | (1 << 19))
246#define PKT_LEN1(len) (((len) & 0x07) << 10)
247#define PKT_ID2(id) ((((id) & 0x3f) << 23) | (1 << 29))
248#define PKT_LEN2(len) (((len) & 0x07) << 20)
249
250#define PKT_LP (1 << 30)
251#define NUM_PKT_SEQ 12
252
Thierry Reding17297a22014-03-14 14:13:15 +0100253/*
254 * non-burst mode with sync pulses
255 */
256static const u32 pkt_seq_video_non_burst_sync_pulses[NUM_PKT_SEQ] = {
Thierry Redingdec72732013-09-03 08:45:46 +0200257 [ 0] = PKT_ID0(MIPI_DSI_V_SYNC_START) | PKT_LEN0(0) |
258 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
259 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0) |
260 PKT_LP,
261 [ 1] = 0,
262 [ 2] = PKT_ID0(MIPI_DSI_V_SYNC_END) | 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 [ 3] = 0,
267 [ 4] = PKT_ID0(MIPI_DSI_H_SYNC_START) | 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 [ 5] = 0,
272 [ 6] = 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 [ 7] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(2) |
276 PKT_ID1(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN1(3) |
277 PKT_ID2(MIPI_DSI_BLANKING_PACKET) | PKT_LEN2(4),
278 [ 8] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
279 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
280 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0) |
281 PKT_LP,
282 [ 9] = 0,
283 [10] = 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 [11] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(2) |
287 PKT_ID1(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN1(3) |
288 PKT_ID2(MIPI_DSI_BLANKING_PACKET) | PKT_LEN2(4),
289};
290
Thierry Reding17297a22014-03-14 14:13:15 +0100291/*
292 * non-burst mode with sync events
293 */
294static const u32 pkt_seq_video_non_burst_sync_events[NUM_PKT_SEQ] = {
295 [ 0] = PKT_ID0(MIPI_DSI_V_SYNC_START) | PKT_LEN0(0) |
296 PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION) | PKT_LEN1(7) |
297 PKT_LP,
298 [ 1] = 0,
299 [ 2] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
300 PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION) | PKT_LEN1(7) |
301 PKT_LP,
302 [ 3] = 0,
303 [ 4] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
304 PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION) | PKT_LEN1(7) |
305 PKT_LP,
306 [ 5] = 0,
307 [ 6] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
308 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(2) |
309 PKT_ID2(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN2(3),
310 [ 7] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(4),
311 [ 8] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
312 PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION) | PKT_LEN1(7) |
313 PKT_LP,
314 [ 9] = 0,
315 [10] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
316 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(2) |
317 PKT_ID2(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN2(3),
318 [11] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(4),
319};
320
Thierry Redingdec72732013-09-03 08:45:46 +0200321static int tegra_dsi_set_phy_timing(struct tegra_dsi *dsi)
322{
323 struct mipi_dphy_timing timing;
324 unsigned long value, period;
325 long rate;
326 int err;
327
328 rate = clk_get_rate(dsi->clk);
329 if (rate < 0)
330 return rate;
331
332 period = DIV_ROUND_CLOSEST(1000000000UL, rate * 2);
333
334 err = mipi_dphy_timing_get_default(&timing, period);
335 if (err < 0)
336 return err;
337
338 err = mipi_dphy_timing_validate(&timing, period);
339 if (err < 0) {
340 dev_err(dsi->dev, "failed to validate D-PHY timing: %d\n", err);
341 return err;
342 }
343
344 /*
345 * The D-PHY timing fields below are expressed in byte-clock cycles,
346 * so multiply the period by 8.
347 */
348 period *= 8;
349
350 value = DSI_TIMING_FIELD(timing.hsexit, period, 1) << 24 |
351 DSI_TIMING_FIELD(timing.hstrail, period, 0) << 16 |
352 DSI_TIMING_FIELD(timing.hszero, period, 3) << 8 |
353 DSI_TIMING_FIELD(timing.hsprepare, period, 1);
354 tegra_dsi_writel(dsi, value, DSI_PHY_TIMING_0);
355
356 value = DSI_TIMING_FIELD(timing.clktrail, period, 1) << 24 |
357 DSI_TIMING_FIELD(timing.clkpost, period, 1) << 16 |
358 DSI_TIMING_FIELD(timing.clkzero, period, 1) << 8 |
359 DSI_TIMING_FIELD(timing.lpx, period, 1);
360 tegra_dsi_writel(dsi, value, DSI_PHY_TIMING_1);
361
362 value = DSI_TIMING_FIELD(timing.clkprepare, period, 1) << 16 |
363 DSI_TIMING_FIELD(timing.clkpre, period, 1) << 8 |
364 DSI_TIMING_FIELD(0xff * period, period, 0) << 0;
365 tegra_dsi_writel(dsi, value, DSI_PHY_TIMING_2);
366
367 value = DSI_TIMING_FIELD(timing.taget, period, 1) << 16 |
368 DSI_TIMING_FIELD(timing.tasure, period, 1) << 8 |
369 DSI_TIMING_FIELD(timing.tago, period, 1);
370 tegra_dsi_writel(dsi, value, DSI_BTA_TIMING);
371
372 return 0;
373}
374
375static int tegra_dsi_get_muldiv(enum mipi_dsi_pixel_format format,
376 unsigned int *mulp, unsigned int *divp)
377{
378 switch (format) {
379 case MIPI_DSI_FMT_RGB666_PACKED:
380 case MIPI_DSI_FMT_RGB888:
381 *mulp = 3;
382 *divp = 1;
383 break;
384
385 case MIPI_DSI_FMT_RGB565:
386 *mulp = 2;
387 *divp = 1;
388 break;
389
390 case MIPI_DSI_FMT_RGB666:
391 *mulp = 9;
392 *divp = 4;
393 break;
394
395 default:
396 return -EINVAL;
397 }
398
399 return 0;
400}
401
Thierry Redingf7d68892014-03-13 08:50:39 +0100402static int tegra_dsi_get_format(enum mipi_dsi_pixel_format format,
403 enum tegra_dsi_format *fmt)
404{
405 switch (format) {
406 case MIPI_DSI_FMT_RGB888:
407 *fmt = TEGRA_DSI_FORMAT_24P;
408 break;
409
410 case MIPI_DSI_FMT_RGB666:
411 *fmt = TEGRA_DSI_FORMAT_18NP;
412 break;
413
414 case MIPI_DSI_FMT_RGB666_PACKED:
415 *fmt = TEGRA_DSI_FORMAT_18P;
416 break;
417
418 case MIPI_DSI_FMT_RGB565:
419 *fmt = TEGRA_DSI_FORMAT_16P;
420 break;
421
422 default:
423 return -EINVAL;
424 }
425
426 return 0;
427}
428
Thierry Redingdec72732013-09-03 08:45:46 +0200429static int tegra_output_dsi_enable(struct tegra_output *output)
430{
431 struct tegra_dc *dc = to_tegra_dc(output->encoder.crtc);
432 struct drm_display_mode *mode = &dc->base.mode;
433 unsigned int hact, hsw, hbp, hfp, i, mul, div;
434 struct tegra_dsi *dsi = to_dsi(output);
Thierry Redingf7d68892014-03-13 08:50:39 +0100435 enum tegra_dsi_format format;
Thierry Redingdec72732013-09-03 08:45:46 +0200436 unsigned long value;
Thierry Reding17297a22014-03-14 14:13:15 +0100437 const u32 *pkt_seq;
Thierry Redingdec72732013-09-03 08:45:46 +0200438 int err;
439
Thierry Reding334ae6b2014-03-14 14:15:10 +0100440 if (dsi->enabled)
441 return 0;
442
Thierry Reding17297a22014-03-14 14:13:15 +0100443 if (dsi->flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE) {
444 DRM_DEBUG_KMS("Non-burst video mode with sync pulses\n");
445 pkt_seq = pkt_seq_video_non_burst_sync_pulses;
446 } else {
447 DRM_DEBUG_KMS("Non-burst video mode with sync events\n");
448 pkt_seq = pkt_seq_video_non_burst_sync_events;
449 }
450
Thierry Redingdec72732013-09-03 08:45:46 +0200451 err = tegra_dsi_get_muldiv(dsi->format, &mul, &div);
452 if (err < 0)
453 return err;
454
Thierry Redingf7d68892014-03-13 08:50:39 +0100455 err = tegra_dsi_get_format(dsi->format, &format);
456 if (err < 0)
457 return err;
458
Thierry Redingdec72732013-09-03 08:45:46 +0200459 err = clk_enable(dsi->clk);
460 if (err < 0)
461 return err;
462
463 reset_control_deassert(dsi->rst);
464
Thierry Redingf7d68892014-03-13 08:50:39 +0100465 value = DSI_CONTROL_CHANNEL(0) | DSI_CONTROL_FORMAT(format) |
Thierry Redingdec72732013-09-03 08:45:46 +0200466 DSI_CONTROL_LANES(dsi->lanes - 1) |
467 DSI_CONTROL_SOURCE(dc->pipe);
468 tegra_dsi_writel(dsi, value, DSI_CONTROL);
469
Thierry Reding976cebc2014-08-06 09:14:28 +0200470 tegra_dsi_writel(dsi, dsi->video_fifo_depth, DSI_MAX_THRESHOLD);
Thierry Redingdec72732013-09-03 08:45:46 +0200471
472 value = DSI_HOST_CONTROL_HS | DSI_HOST_CONTROL_CS |
473 DSI_HOST_CONTROL_ECC;
474 tegra_dsi_writel(dsi, value, DSI_HOST_CONTROL);
475
476 value = tegra_dsi_readl(dsi, DSI_CONTROL);
Alexandre Courbot0c6b1e42014-07-08 21:32:13 +0900477 if (dsi->flags & MIPI_DSI_CLOCK_NON_CONTINUOUS)
478 value |= DSI_CONTROL_HS_CLK_CTRL;
Thierry Redingdec72732013-09-03 08:45:46 +0200479 value &= ~DSI_CONTROL_TX_TRIG(3);
480 value &= ~DSI_CONTROL_DCS_ENABLE;
481 value |= DSI_CONTROL_VIDEO_ENABLE;
482 value &= ~DSI_CONTROL_HOST_ENABLE;
483 tegra_dsi_writel(dsi, value, DSI_CONTROL);
484
485 err = tegra_dsi_set_phy_timing(dsi);
486 if (err < 0)
487 return err;
488
489 for (i = 0; i < NUM_PKT_SEQ; i++)
490 tegra_dsi_writel(dsi, pkt_seq[i], DSI_PKT_SEQ_0_LO + i);
491
492 /* horizontal active pixels */
493 hact = mode->hdisplay * mul / div;
494
495 /* horizontal sync width */
496 hsw = (mode->hsync_end - mode->hsync_start) * mul / div;
497 hsw -= 10;
498
499 /* horizontal back porch */
500 hbp = (mode->htotal - mode->hsync_end) * mul / div;
501 hbp -= 14;
502
503 /* horizontal front porch */
504 hfp = (mode->hsync_start - mode->hdisplay) * mul / div;
505 hfp -= 8;
506
507 tegra_dsi_writel(dsi, hsw << 16 | 0, DSI_PKT_LEN_0_1);
508 tegra_dsi_writel(dsi, hact << 16 | hbp, DSI_PKT_LEN_2_3);
509 tegra_dsi_writel(dsi, hfp, DSI_PKT_LEN_4_5);
510 tegra_dsi_writel(dsi, 0x0f0f << 16, DSI_PKT_LEN_6_7);
511
512 /* set SOL delay */
513 tegra_dsi_writel(dsi, 8 * mul / div, DSI_SOL_DELAY);
514
515 /* enable display controller */
516 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
517 value |= DSI_ENABLE;
518 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
519
Thierry Redingdec72732013-09-03 08:45:46 +0200520 value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
521 value &= ~DISP_CTRL_MODE_MASK;
522 value |= DISP_CTRL_MODE_C_DISPLAY;
523 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
524
Thierry Reding72d30282013-12-12 11:06:55 +0100525 value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
526 value |= PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
527 PW4_ENABLE | PM0_ENABLE | PM1_ENABLE;
528 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
529
Thierry Redingdec72732013-09-03 08:45:46 +0200530 tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
531 tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
532
533 /* enable DSI controller */
534 value = tegra_dsi_readl(dsi, DSI_POWER_CONTROL);
535 value |= DSI_POWER_CONTROL_ENABLE;
536 tegra_dsi_writel(dsi, value, DSI_POWER_CONTROL);
537
Thierry Reding334ae6b2014-03-14 14:15:10 +0100538 dsi->enabled = true;
539
Thierry Redingdec72732013-09-03 08:45:46 +0200540 return 0;
541}
542
543static int tegra_output_dsi_disable(struct tegra_output *output)
544{
545 struct tegra_dc *dc = to_tegra_dc(output->encoder.crtc);
546 struct tegra_dsi *dsi = to_dsi(output);
547 unsigned long value;
548
Thierry Reding334ae6b2014-03-14 14:15:10 +0100549 if (!dsi->enabled)
550 return 0;
551
Thierry Redingdec72732013-09-03 08:45:46 +0200552 /* disable DSI controller */
553 value = tegra_dsi_readl(dsi, DSI_POWER_CONTROL);
Thierry Reding79eb7e52014-03-14 14:19:17 +0100554 value &= ~DSI_POWER_CONTROL_ENABLE;
Thierry Redingdec72732013-09-03 08:45:46 +0200555 tegra_dsi_writel(dsi, value, DSI_POWER_CONTROL);
556
557 /*
Thierry Reding72d30282013-12-12 11:06:55 +0100558 * The following accesses registers of the display controller, so make
559 * sure it's only executed when the output is attached to one.
Thierry Redingdec72732013-09-03 08:45:46 +0200560 */
561 if (dc) {
Thierry Reding72d30282013-12-12 11:06:55 +0100562 value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
563 value &= ~(PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
564 PW4_ENABLE | PM0_ENABLE | PM1_ENABLE);
565 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
566
Thierry Redingdec72732013-09-03 08:45:46 +0200567 value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
568 value &= ~DISP_CTRL_MODE_MASK;
569 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
570
571 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
572 value &= ~DSI_ENABLE;
573 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
Thierry Reding72d30282013-12-12 11:06:55 +0100574
575 tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
576 tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
Thierry Redingdec72732013-09-03 08:45:46 +0200577 }
578
579 clk_disable(dsi->clk);
580
Thierry Reding334ae6b2014-03-14 14:15:10 +0100581 dsi->enabled = false;
582
Thierry Redingdec72732013-09-03 08:45:46 +0200583 return 0;
584}
585
586static int tegra_output_dsi_setup_clock(struct tegra_output *output,
Thierry Reding91eded92014-03-26 13:32:21 +0100587 struct clk *clk, unsigned long pclk,
588 unsigned int *divp)
Thierry Redingdec72732013-09-03 08:45:46 +0200589{
590 struct tegra_dc *dc = to_tegra_dc(output->encoder.crtc);
591 struct drm_display_mode *mode = &dc->base.mode;
592 unsigned int timeout, mul, div, vrefresh;
593 struct tegra_dsi *dsi = to_dsi(output);
594 unsigned long bclk, plld, value;
Thierry Redingdec72732013-09-03 08:45:46 +0200595 int err;
596
597 err = tegra_dsi_get_muldiv(dsi->format, &mul, &div);
598 if (err < 0)
599 return err;
600
Thierry Reding91eded92014-03-26 13:32:21 +0100601 DRM_DEBUG_KMS("mul: %u, div: %u, lanes: %u\n", mul, div, dsi->lanes);
Thierry Redingdec72732013-09-03 08:45:46 +0200602 vrefresh = drm_mode_vrefresh(mode);
Thierry Reding91eded92014-03-26 13:32:21 +0100603 DRM_DEBUG_KMS("vrefresh: %u\n", vrefresh);
Thierry Redingdec72732013-09-03 08:45:46 +0200604
Thierry Reding91eded92014-03-26 13:32:21 +0100605 /* compute byte clock */
Thierry Redingdec72732013-09-03 08:45:46 +0200606 bclk = (pclk * mul) / (div * dsi->lanes);
Thierry Reding91eded92014-03-26 13:32:21 +0100607
608 /*
609 * Compute bit clock and round up to the next MHz.
610 */
611 plld = DIV_ROUND_UP(bclk * 8, 1000000) * 1000000;
612
613 /*
614 * We divide the frequency by two here, but we make up for that by
615 * setting the shift clock divider (further below) to half of the
616 * correct value.
617 */
618 plld /= 2;
Thierry Redingdec72732013-09-03 08:45:46 +0200619
620 err = clk_set_parent(clk, dsi->clk_parent);
621 if (err < 0) {
622 dev_err(dsi->dev, "failed to set parent clock: %d\n", err);
623 return err;
624 }
625
Thierry Reding91eded92014-03-26 13:32:21 +0100626 err = clk_set_rate(dsi->clk_parent, plld);
Thierry Redingdec72732013-09-03 08:45:46 +0200627 if (err < 0) {
628 dev_err(dsi->dev, "failed to set base clock rate to %lu Hz\n",
Thierry Reding91eded92014-03-26 13:32:21 +0100629 plld);
Thierry Redingdec72732013-09-03 08:45:46 +0200630 return err;
631 }
632
633 /*
Thierry Reding91eded92014-03-26 13:32:21 +0100634 * Derive pixel clock from bit clock using the shift clock divider.
635 * Note that this is only half of what we would expect, but we need
636 * that to make up for the fact that we divided the bit clock by a
637 * factor of two above.
638 *
639 * It's not clear exactly why this is necessary, but the display is
640 * not working properly otherwise. Perhaps the PLLs cannot generate
641 * frequencies sufficiently high.
642 */
643 *divp = ((8 * mul) / (div * dsi->lanes)) - 2;
644
645 /*
Thierry Redingdec72732013-09-03 08:45:46 +0200646 * XXX: Move the below somewhere else so that we don't need to have
647 * access to the vrefresh in this function?
648 */
649
650 /* one frame high-speed transmission timeout */
651 timeout = (bclk / vrefresh) / 512;
652 value = DSI_TIMEOUT_LRX(0x2000) | DSI_TIMEOUT_HTX(timeout);
653 tegra_dsi_writel(dsi, value, DSI_TIMEOUT_0);
654
655 /* 2 ms peripheral timeout for panel */
656 timeout = 2 * bclk / 512 * 1000;
657 value = DSI_TIMEOUT_PR(timeout) | DSI_TIMEOUT_TA(0x2000);
658 tegra_dsi_writel(dsi, value, DSI_TIMEOUT_1);
659
660 value = DSI_TALLY_TA(0) | DSI_TALLY_LRX(0) | DSI_TALLY_HTX(0);
661 tegra_dsi_writel(dsi, value, DSI_TO_TALLY);
662
663 return 0;
664}
665
666static int tegra_output_dsi_check_mode(struct tegra_output *output,
667 struct drm_display_mode *mode,
668 enum drm_mode_status *status)
669{
670 /*
671 * FIXME: For now, always assume that the mode is okay.
672 */
673
674 *status = MODE_OK;
675
676 return 0;
677}
678
679static const struct tegra_output_ops dsi_ops = {
680 .enable = tegra_output_dsi_enable,
681 .disable = tegra_output_dsi_disable,
682 .setup_clock = tegra_output_dsi_setup_clock,
683 .check_mode = tegra_output_dsi_check_mode,
684};
685
686static int tegra_dsi_pad_enable(struct tegra_dsi *dsi)
687{
688 unsigned long value;
689
690 value = DSI_PAD_CONTROL_VS1_PULLDN(0) | DSI_PAD_CONTROL_VS1_PDIO(0);
691 tegra_dsi_writel(dsi, value, DSI_PAD_CONTROL_0);
692
693 return 0;
694}
695
696static int tegra_dsi_pad_calibrate(struct tegra_dsi *dsi)
697{
698 unsigned long value;
699
700 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_0);
701 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_1);
702 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_2);
703 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_3);
704 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_4);
705
706 /* start calibration */
707 tegra_dsi_pad_enable(dsi);
708
709 value = DSI_PAD_SLEW_UP(0x7) | DSI_PAD_SLEW_DN(0x7) |
710 DSI_PAD_LP_UP(0x1) | DSI_PAD_LP_DN(0x1) |
711 DSI_PAD_OUT_CLK(0x0);
712 tegra_dsi_writel(dsi, value, DSI_PAD_CONTROL_2);
713
714 return tegra_mipi_calibrate(dsi->mipi);
715}
716
717static int tegra_dsi_init(struct host1x_client *client)
718{
Thierry Reding9910f5c2014-05-22 09:57:15 +0200719 struct drm_device *drm = dev_get_drvdata(client->parent);
Thierry Redingdec72732013-09-03 08:45:46 +0200720 struct tegra_dsi *dsi = host1x_client_to_dsi(client);
Thierry Redingdec72732013-09-03 08:45:46 +0200721 int err;
722
723 dsi->output.type = TEGRA_OUTPUT_DSI;
724 dsi->output.dev = client->dev;
725 dsi->output.ops = &dsi_ops;
726
Thierry Reding9910f5c2014-05-22 09:57:15 +0200727 err = tegra_output_init(drm, &dsi->output);
Thierry Redingdec72732013-09-03 08:45:46 +0200728 if (err < 0) {
729 dev_err(client->dev, "output setup failed: %d\n", err);
730 return err;
731 }
732
733 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
Thierry Reding9910f5c2014-05-22 09:57:15 +0200734 err = tegra_dsi_debugfs_init(dsi, drm->primary);
Thierry Redingdec72732013-09-03 08:45:46 +0200735 if (err < 0)
736 dev_err(dsi->dev, "debugfs setup failed: %d\n", err);
737 }
738
Thierry Redingdec72732013-09-03 08:45:46 +0200739 err = tegra_dsi_pad_calibrate(dsi);
740 if (err < 0) {
741 dev_err(dsi->dev, "MIPI calibration failed: %d\n", err);
742 return err;
743 }
744
Thierry Redingdec72732013-09-03 08:45:46 +0200745 return 0;
746}
747
748static int tegra_dsi_exit(struct host1x_client *client)
749{
750 struct tegra_dsi *dsi = host1x_client_to_dsi(client);
751 int err;
752
753 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
754 err = tegra_dsi_debugfs_exit(dsi);
755 if (err < 0)
756 dev_err(dsi->dev, "debugfs cleanup failed: %d\n", err);
757 }
758
759 err = tegra_output_disable(&dsi->output);
760 if (err < 0) {
761 dev_err(client->dev, "output failed to disable: %d\n", err);
762 return err;
763 }
764
765 err = tegra_output_exit(&dsi->output);
766 if (err < 0) {
767 dev_err(client->dev, "output cleanup failed: %d\n", err);
768 return err;
769 }
770
771 return 0;
772}
773
774static const struct host1x_client_ops dsi_client_ops = {
775 .init = tegra_dsi_init,
776 .exit = tegra_dsi_exit,
777};
778
779static int tegra_dsi_setup_clocks(struct tegra_dsi *dsi)
780{
781 struct clk *parent;
782 int err;
783
784 parent = clk_get_parent(dsi->clk);
785 if (!parent)
786 return -EINVAL;
787
788 err = clk_set_parent(parent, dsi->clk_parent);
789 if (err < 0)
790 return err;
791
792 return 0;
793}
794
Thierry Redingdec72732013-09-03 08:45:46 +0200795static int tegra_dsi_host_attach(struct mipi_dsi_host *host,
796 struct mipi_dsi_device *device)
797{
798 struct tegra_dsi *dsi = host_to_tegra(host);
799 struct tegra_output *output = &dsi->output;
800
Thierry Reding17297a22014-03-14 14:13:15 +0100801 dsi->flags = device->mode_flags;
Thierry Redingdec72732013-09-03 08:45:46 +0200802 dsi->format = device->format;
803 dsi->lanes = device->lanes;
804
805 output->panel = of_drm_find_panel(device->dev.of_node);
806 if (output->panel) {
807 if (output->connector.dev)
808 drm_helper_hpd_irq_event(output->connector.dev);
809 }
810
811 return 0;
812}
813
814static int tegra_dsi_host_detach(struct mipi_dsi_host *host,
815 struct mipi_dsi_device *device)
816{
817 struct tegra_dsi *dsi = host_to_tegra(host);
818 struct tegra_output *output = &dsi->output;
819
820 if (output->panel && &device->dev == output->panel->dev) {
821 if (output->connector.dev)
822 drm_helper_hpd_irq_event(output->connector.dev);
823
824 output->panel = NULL;
825 }
826
827 return 0;
828}
829
830static const struct mipi_dsi_host_ops tegra_dsi_host_ops = {
831 .attach = tegra_dsi_host_attach,
832 .detach = tegra_dsi_host_detach,
833};
834
835static int tegra_dsi_probe(struct platform_device *pdev)
836{
837 struct tegra_dsi *dsi;
838 struct resource *regs;
839 int err;
840
841 dsi = devm_kzalloc(&pdev->dev, sizeof(*dsi), GFP_KERNEL);
842 if (!dsi)
843 return -ENOMEM;
844
845 dsi->output.dev = dsi->dev = &pdev->dev;
Thierry Reding976cebc2014-08-06 09:14:28 +0200846 dsi->video_fifo_depth = 1920;
847 dsi->host_fifo_depth = 64;
Thierry Redingdec72732013-09-03 08:45:46 +0200848
849 err = tegra_output_probe(&dsi->output);
850 if (err < 0)
851 return err;
852
853 /*
854 * Assume these values by default. When a DSI peripheral driver
855 * attaches to the DSI host, the parameters will be taken from
856 * the attached device.
857 */
Thierry Reding17297a22014-03-14 14:13:15 +0100858 dsi->flags = MIPI_DSI_MODE_VIDEO;
Thierry Redingdec72732013-09-03 08:45:46 +0200859 dsi->format = MIPI_DSI_FMT_RGB888;
860 dsi->lanes = 4;
861
862 dsi->rst = devm_reset_control_get(&pdev->dev, "dsi");
863 if (IS_ERR(dsi->rst))
864 return PTR_ERR(dsi->rst);
865
866 dsi->clk = devm_clk_get(&pdev->dev, NULL);
867 if (IS_ERR(dsi->clk)) {
868 dev_err(&pdev->dev, "cannot get DSI clock\n");
869 return PTR_ERR(dsi->clk);
870 }
871
872 err = clk_prepare_enable(dsi->clk);
873 if (err < 0) {
874 dev_err(&pdev->dev, "cannot enable DSI clock\n");
875 return err;
876 }
877
878 dsi->clk_lp = devm_clk_get(&pdev->dev, "lp");
879 if (IS_ERR(dsi->clk_lp)) {
880 dev_err(&pdev->dev, "cannot get low-power clock\n");
881 return PTR_ERR(dsi->clk_lp);
882 }
883
884 err = clk_prepare_enable(dsi->clk_lp);
885 if (err < 0) {
886 dev_err(&pdev->dev, "cannot enable low-power clock\n");
887 return err;
888 }
889
890 dsi->clk_parent = devm_clk_get(&pdev->dev, "parent");
891 if (IS_ERR(dsi->clk_parent)) {
892 dev_err(&pdev->dev, "cannot get parent clock\n");
893 return PTR_ERR(dsi->clk_parent);
894 }
895
896 err = clk_prepare_enable(dsi->clk_parent);
897 if (err < 0) {
898 dev_err(&pdev->dev, "cannot enable parent clock\n");
899 return err;
900 }
901
Thierry Reding3b077af2014-03-14 14:07:50 +0100902 dsi->vdd = devm_regulator_get(&pdev->dev, "avdd-dsi-csi");
903 if (IS_ERR(dsi->vdd)) {
904 dev_err(&pdev->dev, "cannot get VDD supply\n");
905 return PTR_ERR(dsi->vdd);
906 }
907
908 err = regulator_enable(dsi->vdd);
909 if (err < 0) {
910 dev_err(&pdev->dev, "cannot enable VDD supply\n");
911 return err;
912 }
913
Thierry Redingdec72732013-09-03 08:45:46 +0200914 err = tegra_dsi_setup_clocks(dsi);
915 if (err < 0) {
916 dev_err(&pdev->dev, "cannot setup clocks\n");
917 return err;
918 }
919
920 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
921 dsi->regs = devm_ioremap_resource(&pdev->dev, regs);
Wei Yongjun85316ea2013-10-21 11:34:07 +0800922 if (IS_ERR(dsi->regs))
923 return PTR_ERR(dsi->regs);
Thierry Redingdec72732013-09-03 08:45:46 +0200924
Thierry Redingdec72732013-09-03 08:45:46 +0200925 dsi->mipi = tegra_mipi_request(&pdev->dev);
926 if (IS_ERR(dsi->mipi))
927 return PTR_ERR(dsi->mipi);
928
929 dsi->host.ops = &tegra_dsi_host_ops;
930 dsi->host.dev = &pdev->dev;
931
932 err = mipi_dsi_host_register(&dsi->host);
933 if (err < 0) {
934 dev_err(&pdev->dev, "failed to register DSI host: %d\n", err);
935 return err;
936 }
937
938 INIT_LIST_HEAD(&dsi->client.list);
939 dsi->client.ops = &dsi_client_ops;
940 dsi->client.dev = &pdev->dev;
941
942 err = host1x_client_register(&dsi->client);
943 if (err < 0) {
944 dev_err(&pdev->dev, "failed to register host1x client: %d\n",
945 err);
946 return err;
947 }
948
949 platform_set_drvdata(pdev, dsi);
950
951 return 0;
952}
953
954static int tegra_dsi_remove(struct platform_device *pdev)
955{
956 struct tegra_dsi *dsi = platform_get_drvdata(pdev);
957 int err;
958
959 err = host1x_client_unregister(&dsi->client);
960 if (err < 0) {
961 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
962 err);
963 return err;
964 }
965
966 mipi_dsi_host_unregister(&dsi->host);
967 tegra_mipi_free(dsi->mipi);
968
Thierry Reding3b077af2014-03-14 14:07:50 +0100969 regulator_disable(dsi->vdd);
Thierry Redingdec72732013-09-03 08:45:46 +0200970 clk_disable_unprepare(dsi->clk_parent);
971 clk_disable_unprepare(dsi->clk_lp);
972 clk_disable_unprepare(dsi->clk);
Thierry Redingcb825d82014-03-14 14:25:43 +0100973 reset_control_assert(dsi->rst);
Thierry Redingdec72732013-09-03 08:45:46 +0200974
975 err = tegra_output_remove(&dsi->output);
976 if (err < 0) {
977 dev_err(&pdev->dev, "failed to remove output: %d\n", err);
978 return err;
979 }
980
981 return 0;
982}
983
984static const struct of_device_id tegra_dsi_of_match[] = {
985 { .compatible = "nvidia,tegra114-dsi", },
986 { },
987};
Stephen Warrenef707282014-06-18 16:21:55 -0600988MODULE_DEVICE_TABLE(of, tegra_dsi_of_match);
Thierry Redingdec72732013-09-03 08:45:46 +0200989
990struct platform_driver tegra_dsi_driver = {
991 .driver = {
992 .name = "tegra-dsi",
993 .of_match_table = tegra_dsi_of_match,
994 },
995 .probe = tegra_dsi_probe,
996 .remove = tegra_dsi_remove,
997};