2 * Copyright (C) 2013 NVIDIA Corporation
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
23 #include <linux/clk.h>
24 #include <linux/clk/tegra.h>
25 #include <linux/debugfs.h>
26 #include <linux/host1x.h>
27 #include <linux/module.h>
29 #include <linux/platform_device.h>
34 #define DSI_INCR_SYNCPT 0x00
35 #define DSI_INCR_SYNCPT_CONTROL 0x01
36 #define DSI_INCR_SYNCPT_ERROR 0x02
37 #define DSI_CTXSW 0x08
38 #define DSI_RD_DATA 0x09
39 #define DSI_WR_DATA 0x0a
40 #define DSI_POWER_CONTROL 0x0b
41 #define DSI_POWER_CONTROL_ENABLE (1 << 0)
42 #define DSI_INT_ENABLE 0x0c
43 #define DSI_INT_STATUS 0x0d
44 #define DSI_INT_MASK 0x0e
45 #define DSI_HOST_CONTROL 0x0f
46 #define DSI_HOST_CONTROL_RAW (1 << 6)
47 #define DSI_HOST_CONTROL_HS (1 << 5)
48 #define DSI_HOST_CONTROL_CS (1 << 1)
49 #define DSI_HOST_CONTROL_ECC (1 << 0)
50 #define DSI_CONTROL 0x10
51 #define DSI_CONTROL_HS_CLK_CTRL (1 << 20)
52 #define DSI_CONTROL_CHANNEL(c) (((c) & 0x3) << 16)
53 #define DSI_CONTROL_FORMAT(f) (((f) & 0x3) << 12)
54 #define DSI_CONTROL_TX_TRIG(x) (((x) & 0x3) << 8)
55 #define DSI_CONTROL_LANES(n) (((n) & 0x3) << 4)
56 #define DSI_CONTROL_DCS_ENABLE (1 << 3)
57 #define DSI_CONTROL_SOURCE(s) (((s) & 0x1) << 2)
58 #define DSI_CONTROL_VIDEO_ENABLE (1 << 1)
59 #define DSI_CONTROL_HOST_ENABLE (1 << 0)
60 #define DSI_SOL_DELAY 0x11
61 #define DSI_MAX_THRESHOLD 0x12
62 #define DSI_TRIGGER 0x13
63 #define DSI_TX_CRC 0x14
64 #define DSI_STATUS 0x15
65 #define DSI_INIT_SEQ_CONTROL 0x1a
66 #define DSI_INIT_SEQ_DATA_0 0x1b
67 #define DSI_INIT_SEQ_DATA_1 0x1c
68 #define DSI_INIT_SEQ_DATA_2 0x1d
69 #define DSI_INIT_SEQ_DATA_3 0x1e
70 #define DSI_INIT_SEQ_DATA_4 0x1f
71 #define DSI_INIT_SEQ_DATA_5 0x20
72 #define DSI_INIT_SEQ_DATA_6 0x21
73 #define DSI_INIT_SEQ_DATA_7 0x22
74 #define DSI_PKT_SEQ_0_LO 0x23
75 #define DSI_PKT_SEQ_0_HI 0x24
76 #define DSI_PKT_SEQ_1_LO 0x25
77 #define DSI_PKT_SEQ_1_HI 0x26
78 #define DSI_PKT_SEQ_2_LO 0x27
79 #define DSI_PKT_SEQ_2_HI 0x28
80 #define DSI_PKT_SEQ_3_LO 0x29
81 #define DSI_PKT_SEQ_3_HI 0x2a
82 #define DSI_PKT_SEQ_4_LO 0x2b
83 #define DSI_PKT_SEQ_4_HI 0x2c
84 #define DSI_PKT_SEQ_5_LO 0x2d
85 #define DSI_PKT_SEQ_5_HI 0x2e
86 #define DSI_DCS_CMDS 0x33
87 #define DSI_PKT_LEN_0_1 0x34
88 #define DSI_PKT_LEN_2_3 0x35
89 #define DSI_PKT_LEN_4_5 0x36
90 #define DSI_PKT_LEN_6_7 0x37
91 #define DSI_PHY_TIMING_0 0x3c
92 #define DSI_PHY_TIMING_1 0x3d
93 #define DSI_PHY_TIMING_2 0x3e
94 #define DSI_BTA_TIMING 0x3f
96 #define DSI_TIMING_FIELD(value, period, hwinc) \
97 ((DIV_ROUND_CLOSEST(value, period) - (hwinc)) & 0xff)
99 #define DSI_TIMEOUT_0 0x44
100 #define DSI_TIMEOUT_LRX(x) (((x) & 0xffff) << 16)
101 #define DSI_TIMEOUT_HTX(x) (((x) & 0xffff) << 0)
102 #define DSI_TIMEOUT_1 0x45
103 #define DSI_TIMEOUT_PR(x) (((x) & 0xffff) << 16)
104 #define DSI_TIMEOUT_TA(x) (((x) & 0xffff) << 0)
105 #define DSI_TO_TALLY 0x46
106 #define DSI_TALLY_TA(x) (((x) & 0xff) << 16)
107 #define DSI_TALLY_LRX(x) (((x) & 0xff) << 8)
108 #define DSI_TALLY_HTX(x) (((x) & 0xff) << 0)
109 #define DSI_PAD_CONTROL_0 0x4b
110 #define DSI_PAD_CONTROL_VS1_PDIO(x) (((x) & 0xf) << 0)
111 #define DSI_PAD_CONTROL_VS1_PDIO_CLK (1 << 8)
112 #define DSI_PAD_CONTROL_VS1_PULLDN(x) (((x) & 0xf) << 16)
113 #define DSI_PAD_CONTROL_VS1_PULLDN_CLK (1 << 24)
114 #define DSI_PAD_CONTROL_CD 0x4c
115 #define DSI_PAD_CD_STATUS 0x4d
116 #define DSI_VIDEO_MODE_CONTROL 0x4e
117 #define DSI_PAD_CONTROL_1 0x4f
118 #define DSI_PAD_CONTROL_2 0x50
119 #define DSI_PAD_OUT_CLK(x) (((x) & 0x7) << 0)
120 #define DSI_PAD_LP_DN(x) (((x) & 0x7) << 4)
121 #define DSI_PAD_LP_UP(x) (((x) & 0x7) << 8)
122 #define DSI_PAD_SLEW_DN(x) (((x) & 0x7) << 12)
123 #define DSI_PAD_SLEW_UP(x) (((x) & 0x7) << 16)
124 #define DSI_PAD_CONTROL_3 0x51
125 #define DSI_PAD_CONTROL_4 0x52
126 #define DSI_GANGED_MODE_CONTROL 0x53
127 #define DSI_GANGED_MODE_START 0x54
128 #define DSI_GANGED_MODE_SIZE 0x55
129 #define DSI_RAW_DATA_BYTE_COUNT 0x56
130 #define DSI_ULTRA_LOW_POWER_CONTROL 0x57
131 #define DSI_INIT_SEQ_DATA_8 0x58
132 #define DSI_INIT_SEQ_DATA_9 0x59
133 #define DSI_INIT_SEQ_DATA_10 0x5a
134 #define DSI_INIT_SEQ_DATA_11 0x5b
135 #define DSI_INIT_SEQ_DATA_12 0x5c
136 #define DSI_INIT_SEQ_DATA_13 0x5d
137 #define DSI_INIT_SEQ_DATA_14 0x5e
138 #define DSI_INIT_SEQ_DATA_15 0x5f
148 * D-PHY timing parameters
150 * A detailed description of these parameters can be found in the MIPI
151 * Alliance Specification for D-PHY, Section 5.9 "Global Operation Timing
154 * All parameters are specified in nanoseconds.
156 struct dsi_phy_timing {
157 unsigned int clkmiss;
158 unsigned int clkpost;
160 unsigned int clkprepare;
161 unsigned int clksettle;
162 unsigned int clktermen;
163 unsigned int clktrail;
164 unsigned int clkzero;
165 unsigned int dtermen;
168 unsigned int hsprepare;
170 unsigned int hssettle;
172 unsigned int hstrail;
182 * Default D-PHY timings based on MIPI D-PHY specification. Derived from
183 * the valid ranges specified in Section 5.9 of the D-PHY specification
184 * with minor adjustments.
186 static int dsi_phy_timing_get_default(struct dsi_phy_timing *timing,
187 unsigned long period)
190 timing->clkpost = 70 + 52 * period;
192 timing->clkprepare = 65;
193 timing->clksettle = 95;
194 timing->clktermen = 0;
195 timing->clktrail = 80;
196 timing->clkzero = 260;
199 timing->hsexit = 120;
200 timing->hsprepare = 65 + 5 * period;
201 timing->hszero = 145 + 5 * period;
202 timing->hssettle = 85 + 6 * period;
204 timing->hstrail = max(8 * period, 60 + 4 * period);
205 timing->init = 100000;
207 timing->taget = 5 * timing->lpx;
208 timing->tago = 4 * timing->lpx;
209 timing->tasure = 2 * timing->lpx;
210 timing->wakeup = 1000000;
216 * Validate D-PHY timing according to MIPI Alliance Specification for D-PHY,
217 * Section 5.9 "Global Operation Timing Parameters".
219 static int dsi_phy_timing_validate(struct dsi_phy_timing *timing,
220 unsigned long period)
222 if (timing->clkmiss > 60)
225 if (timing->clkpost < (60 + 52 * period))
228 if (timing->clkpre < 8)
231 if (timing->clkprepare < 38 || timing->clkprepare > 95)
234 if (timing->clksettle < 95 || timing->clksettle > 300)
237 if (timing->clktermen > 38)
240 if (timing->clktrail < 60)
243 if (timing->clkprepare + timing->clkzero < 300)
246 if (timing->dtermen > 35 + 4 * period)
249 if (timing->eot > 105 + 12 * period)
252 if (timing->hsexit < 100)
255 if (timing->hsprepare < 40 + 4 * period ||
256 timing->hsprepare > 85 + 6 * period)
259 if (timing->hsprepare + timing->hszero < 145 + 10 * period)
262 if ((timing->hssettle < 85 + 6 * period) ||
263 (timing->hssettle > 145 + 10 * period))
266 if (timing->hsskip < 40 || timing->hsskip > 55 + 4 * period)
269 if (timing->hstrail < max(8 * period, 60 + 4 * period))
272 if (timing->init < 100000)
275 if (timing->lpx < 50)
278 if (timing->taget != 5 * timing->lpx)
281 if (timing->tago != 4 * timing->lpx)
284 if (timing->tasure < timing->lpx || timing->tasure > 2 * timing->lpx)
287 if (timing->wakeup < 1000000)
294 struct host1x_client client;
295 struct tegra_output output;
300 struct clk *clk_parent;
303 struct drm_info_list *debugfs_files;
304 struct drm_minor *minor;
305 struct dentry *debugfs;
307 enum dsi_format format;
311 static inline struct tegra_dsi *
312 host1x_client_to_dsi(struct host1x_client *client)
314 return container_of(client, struct tegra_dsi, client);
317 static inline struct tegra_dsi *to_dsi(struct tegra_output *output)
319 return container_of(output, struct tegra_dsi, output);
322 static inline unsigned long tegra_dsi_readl(struct tegra_dsi *dsi,
325 return readl(dsi->regs + (reg << 2));
328 static inline void tegra_dsi_writel(struct tegra_dsi *dsi, unsigned long value,
331 writel(value, dsi->regs + (reg << 2));
334 static int tegra_dsi_show_regs(struct seq_file *s, void *data)
336 struct drm_info_node *node = s->private;
337 struct tegra_dsi *dsi = node->info_ent->data;
339 #define DUMP_REG(name) \
340 seq_printf(s, "%-32s %#05x %08lx\n", #name, name, \
341 tegra_dsi_readl(dsi, name))
343 DUMP_REG(DSI_INCR_SYNCPT);
344 DUMP_REG(DSI_INCR_SYNCPT_CONTROL);
345 DUMP_REG(DSI_INCR_SYNCPT_ERROR);
347 DUMP_REG(DSI_RD_DATA);
348 DUMP_REG(DSI_WR_DATA);
349 DUMP_REG(DSI_POWER_CONTROL);
350 DUMP_REG(DSI_INT_ENABLE);
351 DUMP_REG(DSI_INT_STATUS);
352 DUMP_REG(DSI_INT_MASK);
353 DUMP_REG(DSI_HOST_CONTROL);
354 DUMP_REG(DSI_CONTROL);
355 DUMP_REG(DSI_SOL_DELAY);
356 DUMP_REG(DSI_MAX_THRESHOLD);
357 DUMP_REG(DSI_TRIGGER);
358 DUMP_REG(DSI_TX_CRC);
359 DUMP_REG(DSI_STATUS);
361 DUMP_REG(DSI_INIT_SEQ_CONTROL);
362 DUMP_REG(DSI_INIT_SEQ_DATA_0);
363 DUMP_REG(DSI_INIT_SEQ_DATA_1);
364 DUMP_REG(DSI_INIT_SEQ_DATA_2);
365 DUMP_REG(DSI_INIT_SEQ_DATA_3);
366 DUMP_REG(DSI_INIT_SEQ_DATA_4);
367 DUMP_REG(DSI_INIT_SEQ_DATA_5);
368 DUMP_REG(DSI_INIT_SEQ_DATA_6);
369 DUMP_REG(DSI_INIT_SEQ_DATA_7);
371 DUMP_REG(DSI_PKT_SEQ_0_LO);
372 DUMP_REG(DSI_PKT_SEQ_0_HI);
373 DUMP_REG(DSI_PKT_SEQ_1_LO);
374 DUMP_REG(DSI_PKT_SEQ_1_HI);
375 DUMP_REG(DSI_PKT_SEQ_2_LO);
376 DUMP_REG(DSI_PKT_SEQ_2_HI);
377 DUMP_REG(DSI_PKT_SEQ_3_LO);
378 DUMP_REG(DSI_PKT_SEQ_3_HI);
379 DUMP_REG(DSI_PKT_SEQ_4_LO);
380 DUMP_REG(DSI_PKT_SEQ_4_HI);
381 DUMP_REG(DSI_PKT_SEQ_5_LO);
382 DUMP_REG(DSI_PKT_SEQ_5_HI);
384 DUMP_REG(DSI_DCS_CMDS);
386 DUMP_REG(DSI_PKT_LEN_0_1);
387 DUMP_REG(DSI_PKT_LEN_2_3);
388 DUMP_REG(DSI_PKT_LEN_4_5);
389 DUMP_REG(DSI_PKT_LEN_6_7);
391 DUMP_REG(DSI_PHY_TIMING_0);
392 DUMP_REG(DSI_PHY_TIMING_1);
393 DUMP_REG(DSI_PHY_TIMING_2);
394 DUMP_REG(DSI_BTA_TIMING);
396 DUMP_REG(DSI_TIMEOUT_0);
397 DUMP_REG(DSI_TIMEOUT_1);
398 DUMP_REG(DSI_TO_TALLY);
400 DUMP_REG(DSI_PAD_CONTROL_0);
401 DUMP_REG(DSI_PAD_CONTROL_CD);
402 DUMP_REG(DSI_PAD_CD_STATUS);
403 DUMP_REG(DSI_VIDEO_MODE_CONTROL);
404 DUMP_REG(DSI_PAD_CONTROL_1);
405 DUMP_REG(DSI_PAD_CONTROL_2);
406 DUMP_REG(DSI_PAD_CONTROL_3);
407 DUMP_REG(DSI_PAD_CONTROL_4);
409 DUMP_REG(DSI_GANGED_MODE_CONTROL);
410 DUMP_REG(DSI_GANGED_MODE_START);
411 DUMP_REG(DSI_GANGED_MODE_SIZE);
413 DUMP_REG(DSI_RAW_DATA_BYTE_COUNT);
414 DUMP_REG(DSI_ULTRA_LOW_POWER_CONTROL);
416 DUMP_REG(DSI_INIT_SEQ_DATA_8);
417 DUMP_REG(DSI_INIT_SEQ_DATA_9);
418 DUMP_REG(DSI_INIT_SEQ_DATA_10);
419 DUMP_REG(DSI_INIT_SEQ_DATA_11);
420 DUMP_REG(DSI_INIT_SEQ_DATA_12);
421 DUMP_REG(DSI_INIT_SEQ_DATA_13);
422 DUMP_REG(DSI_INIT_SEQ_DATA_14);
423 DUMP_REG(DSI_INIT_SEQ_DATA_15);
430 static struct drm_info_list debugfs_files[] = {
431 { "regs", tegra_dsi_show_regs, 0, NULL },
434 static int tegra_dsi_debugfs_init(struct tegra_dsi *dsi,
435 struct drm_minor *minor)
437 const char *name = dev_name(dsi->dev);
441 dsi->debugfs = debugfs_create_dir(name, minor->debugfs_root);
445 dsi->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
447 if (!dsi->debugfs_files) {
452 for (i = 0; i < ARRAY_SIZE(debugfs_files); i++)
453 dsi->debugfs_files[i].data = dsi;
455 err = drm_debugfs_create_files(dsi->debugfs_files,
456 ARRAY_SIZE(debugfs_files),
457 dsi->debugfs, minor);
466 kfree(dsi->debugfs_files);
467 dsi->debugfs_files = NULL;
469 debugfs_remove(dsi->debugfs);
475 static int tegra_dsi_debugfs_exit(struct tegra_dsi *dsi)
477 drm_debugfs_remove_files(dsi->debugfs_files, ARRAY_SIZE(debugfs_files),
481 kfree(dsi->debugfs_files);
482 dsi->debugfs_files = NULL;
484 debugfs_remove(dsi->debugfs);
504 CMD_RGB_16BPP = 0x0E,
505 CMD_RGB_18BPP = 0x1E,
506 CMD_RGB_18BPPNP = 0x2E,
507 CMD_RGB_24BPP = 0x3E,
510 #define PKT_ID0(id) ((((id) & 0x3f) << 3) | (1 << 9))
511 #define PKT_LEN0(len) (((len) & 0x7) << 0)
512 #define PKT_ID1(id) ((((id) & 0x3f) << 13) | (1 << 19))
513 #define PKT_LEN1(len) (((len) & 0x7) << 10)
514 #define PKT_ID2(id) ((((id) & 0x3f) << 23) | (1 << 29))
515 #define PKT_LEN2(len) (((len) & 0x7) << 20)
516 #define PKT_ID3(id) ((((id) & 0x3f) << 3) | (1 << 9))
517 #define PKT_LEN3(len) (((len) & 0x7) << 0)
518 #define PKT_ID4(id) ((((id) & 0x3f) << 13) | (1 << 19))
519 #define PKT_LEN4(len) (((len) & 0x7) << 10)
520 #define PKT_ID5(id) ((((id) & 0x3f) << 23) | (1 << 29))
521 #define PKT_LEN5(len) (((len) & 0x7) << 20)
522 #define PKT_LP (1 << 30)
523 #define NUM_PKT_SEQ 12
525 /* non-burst mode with sync-end */
526 static const u32 pkt_seq_vnb_syne[NUM_PKT_SEQ] = {
527 PKT_ID0(CMD_VS) | PKT_LEN0(0) | PKT_ID1(CMD_BLNK) | PKT_LEN1(1) |
528 PKT_ID2(CMD_HE) | PKT_LEN2(0) | PKT_LP,
530 PKT_ID0(CMD_VE) | PKT_LEN0(0) | PKT_ID1(CMD_BLNK) | PKT_LEN1(1) |
531 PKT_ID2(CMD_HE) | PKT_LEN2(0) | PKT_LP,
533 PKT_ID0(CMD_HS) | PKT_LEN0(0) | PKT_ID1(CMD_BLNK) | PKT_LEN1(1) |
534 PKT_ID2(CMD_HE) | PKT_LEN2(0) | PKT_LP,
536 PKT_ID0(CMD_HS) | PKT_LEN0(0) | PKT_ID1(CMD_BLNK) | PKT_LEN1(1) |
537 PKT_ID2(CMD_HE) | PKT_LEN2(0),
538 PKT_ID3(CMD_BLNK) | PKT_LEN3(2) | PKT_ID4(CMD_RGB_24BPP) | PKT_LEN4(3) |
539 PKT_ID5(CMD_BLNK) | PKT_LEN5(4),
540 PKT_ID0(CMD_HS) | PKT_LEN0(0) | PKT_ID1(CMD_BLNK) | PKT_LEN1(1) |
541 PKT_ID2(CMD_HE) | PKT_LEN2(0) | PKT_LP,
543 PKT_ID0(CMD_HS) | PKT_LEN0(0) | PKT_ID1(CMD_BLNK) | PKT_LEN1(1) |
544 PKT_ID2(CMD_HE) | PKT_LEN2(0),
545 PKT_ID3(CMD_BLNK) | PKT_LEN3(2) | PKT_ID4(CMD_RGB_24BPP) | PKT_LEN4(3) |
546 PKT_ID5(CMD_BLNK) | PKT_LEN5(4),
549 static int tegra_dsi_set_phy_timing(struct tegra_dsi *dsi)
551 struct dsi_phy_timing timing;
552 unsigned long value, period;
556 rate = clk_get_rate(dsi->clk);
560 period = DIV_ROUND_CLOSEST(1000000000UL, rate * 2);
562 err = dsi_phy_timing_get_default(&timing, period);
566 err = dsi_phy_timing_validate(&timing, period);
568 dev_err(dsi->dev, "failed to validate D-PHY timing: %d\n", err);
573 * The D-PHY timing fields below are expressed in byte-clock cycles,
574 * so multiply the period by 8.
578 value = DSI_TIMING_FIELD(timing.hsexit, period, 1) << 24 |
579 DSI_TIMING_FIELD(timing.hstrail, period, 0) << 16 |
580 DSI_TIMING_FIELD(timing.hszero, period, 3) << 8 |
581 DSI_TIMING_FIELD(timing.hsprepare, period, 1);
582 tegra_dsi_writel(dsi, value, DSI_PHY_TIMING_0);
584 value = DSI_TIMING_FIELD(timing.clktrail, period, 1) << 24 |
585 DSI_TIMING_FIELD(timing.clkpost, period, 1) << 16 |
586 DSI_TIMING_FIELD(timing.clkzero, period, 1) << 8 |
587 DSI_TIMING_FIELD(timing.lpx, period, 1);
588 tegra_dsi_writel(dsi, value, DSI_PHY_TIMING_1);
590 value = DSI_TIMING_FIELD(timing.clkprepare, period, 1) << 16 |
591 DSI_TIMING_FIELD(timing.clkpre, period, 1) << 8 |
592 DSI_TIMING_FIELD(0xff * period, period, 0) << 0;
593 tegra_dsi_writel(dsi, value, DSI_PHY_TIMING_2);
595 value = DSI_TIMING_FIELD(timing.taget, period, 1) << 16 |
596 DSI_TIMING_FIELD(timing.tasure, period, 1) << 8 |
597 DSI_TIMING_FIELD(timing.tago, period, 1);
598 tegra_dsi_writel(dsi, value, DSI_BTA_TIMING);
603 static int tegra_dsi_get_muldiv(enum dsi_format format, unsigned int *mulp,
612 case DSI_FORMAT_18NP:
630 static int tegra_output_dsi_enable(struct tegra_output *output)
632 struct tegra_dc *dc = to_tegra_dc(output->encoder.crtc);
633 struct drm_display_mode *mode = &dc->base.mode;
634 unsigned int hact, hsw, hbp, hfp, i, mul, div;
635 struct tegra_dsi *dsi = to_dsi(output);
636 /* FIXME: don't hardcode this */
637 const u32 *pkt_seq = pkt_seq_vnb_syne;
641 err = tegra_dsi_get_muldiv(dsi->format, &mul, &div);
645 err = clk_enable(dsi->clk);
649 tegra_periph_reset_deassert(dsi->clk);
651 value = DSI_CONTROL_CHANNEL(0) | DSI_CONTROL_FORMAT(dsi->format) |
652 DSI_CONTROL_LANES(dsi->lanes - 1) | DSI_CONTROL_SOURCE(0);
653 tegra_dsi_writel(dsi, value, DSI_CONTROL);
655 tegra_dc_writel(dc, DSI_ENABLE, DC_DISP_DISP_WIN_OPTIONS);
657 value = PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
658 PW4_ENABLE | PM0_ENABLE | PM1_ENABLE;
659 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
661 tegra_dc_writel(dc, DISP_CTRL_MODE_C_DISPLAY, DC_CMD_DISPLAY_COMMAND);
662 tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
663 tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
665 tegra_dsi_writel(dsi, 0x000001e0, DSI_MAX_THRESHOLD);
667 value = DSI_HOST_CONTROL_HS | DSI_HOST_CONTROL_CS |
668 DSI_HOST_CONTROL_ECC;
669 tegra_dsi_writel(dsi, value, DSI_HOST_CONTROL);
671 value = tegra_dsi_readl(dsi, DSI_CONTROL);
672 value |= DSI_CONTROL_HS_CLK_CTRL;
673 value &= ~DSI_CONTROL_TX_TRIG(3);
674 value &= ~DSI_CONTROL_DCS_ENABLE;
675 value |= DSI_CONTROL_VIDEO_ENABLE;
676 value &= ~DSI_CONTROL_HOST_ENABLE;
677 tegra_dsi_writel(dsi, value, DSI_CONTROL);
679 err = tegra_dsi_set_phy_timing(dsi);
683 for (i = 0; i < NUM_PKT_SEQ; i++)
684 tegra_dsi_writel(dsi, pkt_seq[i], DSI_PKT_SEQ_0_LO + i);
686 /* horizontal active pixels */
687 hact = mode->hdisplay * mul / div;
689 /* horizontal sync width */
690 hsw = (mode->hsync_end - mode->hsync_start) * mul / div;
693 /* horizontal back porch */
694 hbp = (mode->htotal - mode->hsync_end) * mul / div;
697 /* horizontal front porch */
698 hfp = (mode->hsync_start - mode->hdisplay) * mul / div;
701 tegra_dsi_writel(dsi, hsw << 16 | 0, DSI_PKT_LEN_0_1);
702 tegra_dsi_writel(dsi, hact << 16 | hbp, DSI_PKT_LEN_2_3);
703 tegra_dsi_writel(dsi, hfp, DSI_PKT_LEN_4_5);
704 tegra_dsi_writel(dsi, 0x0f0f << 16, DSI_PKT_LEN_6_7);
707 tegra_dsi_writel(dsi, 8 * mul / div, DSI_SOL_DELAY);
712 static int tegra_output_dsi_disable(struct tegra_output *output)
714 struct tegra_dsi *dsi = to_dsi(output);
716 clk_disable(dsi->clk);
721 static int tegra_output_dsi_setup_clock(struct tegra_output *output,
722 struct clk *clk, unsigned long pclk)
724 struct tegra_dc *dc = to_tegra_dc(output->encoder.crtc);
725 struct drm_display_mode *mode = &dc->base.mode;
726 struct tegra_dsi *dsi = to_dsi(output);
727 unsigned long bclk, plld, value;
728 unsigned int timeout, mul, div;
732 err = tegra_dsi_get_muldiv(dsi->format, &mul, &div);
736 pclk = mode->htotal * mode->vtotal * mode->vrefresh;
737 bclk = (pclk * mul) / (div * dsi->lanes);
738 plld = DIV_ROUND_UP(bclk * 8, 1000000);
739 pclk = (plld * 1000000) / 2;
741 err = clk_set_parent(clk, dsi->clk_parent);
743 dev_err(dsi->dev, "failed to set parent clock: %d\n", err);
747 base = clk_get_parent(dsi->clk_parent);
750 * This assumes that the parent clock is pll_d_out0 or pll_d2_out
751 * respectively, each of which divides the base pll_d by 2.
753 err = clk_set_rate(base, pclk * 2);
755 dev_err(dsi->dev, "failed to set base clock rate to %lu Hz\n",
760 /* one frame high-speed transmission timeout */
761 timeout = (bclk / mode->vrefresh) / 512;
762 value = DSI_TIMEOUT_LRX(0x2000) | DSI_TIMEOUT_HTX(timeout);
763 tegra_dsi_writel(dsi, value, DSI_TIMEOUT_0);
765 /* 2 ms peripheral timeout for panel */
766 timeout = 2 * bclk / 512 * 1000;
767 value = DSI_TIMEOUT_PR(timeout) | DSI_TIMEOUT_TA(0x2000);
768 tegra_dsi_writel(dsi, value, DSI_TIMEOUT_1);
770 value = DSI_TALLY_TA(0) | DSI_TALLY_LRX(0) | DSI_TALLY_HTX(0);
771 tegra_dsi_writel(dsi, value, DSI_TO_TALLY);
776 static int tegra_output_dsi_check_mode(struct tegra_output *output,
777 struct drm_display_mode *mode,
778 enum drm_mode_status *status)
781 * FIXME: For now, always assume that the mode is okay.
789 static const struct tegra_output_ops dsi_ops = {
790 .enable = tegra_output_dsi_enable,
791 .disable = tegra_output_dsi_disable,
792 .setup_clock = tegra_output_dsi_setup_clock,
793 .check_mode = tegra_output_dsi_check_mode,
796 static int tegra_dsi_pad_enable(struct tegra_dsi *dsi)
800 value = DSI_PAD_CONTROL_VS1_PULLDN(0) | DSI_PAD_CONTROL_VS1_PDIO(0);
801 tegra_dsi_writel(dsi, value, DSI_PAD_CONTROL_0);
806 static int tegra_dsi_init(struct host1x_client *client)
808 struct tegra_drm *tegra = dev_get_drvdata(client->parent);
809 struct tegra_dsi *dsi = host1x_client_to_dsi(client);
810 unsigned long value, i;
813 dsi->output.type = TEGRA_OUTPUT_DSI;
814 dsi->output.dev = client->dev;
815 dsi->output.ops = &dsi_ops;
817 err = tegra_output_init(tegra->drm, &dsi->output);
819 dev_err(client->dev, "output setup failed: %d\n", err);
823 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
824 err = tegra_dsi_debugfs_init(dsi, tegra->drm->primary);
826 dev_err(dsi->dev, "debugfs setup failed: %d\n", err);
830 * enable high-speed mode, checksum generation, ECC generation and
833 value = tegra_dsi_readl(dsi, DSI_HOST_CONTROL);
834 value |= DSI_HOST_CONTROL_ECC | DSI_HOST_CONTROL_CS |
836 value &= ~DSI_HOST_CONTROL_RAW;
837 tegra_dsi_writel(dsi, value, DSI_HOST_CONTROL);
839 tegra_dsi_writel(dsi, 0, DSI_SOL_DELAY);
840 tegra_dsi_writel(dsi, 0, DSI_MAX_THRESHOLD);
842 tegra_dsi_writel(dsi, 0, DSI_INIT_SEQ_CONTROL);
844 for (i = 0; i < 8; i++) {
845 tegra_dsi_writel(dsi, 0, DSI_INIT_SEQ_DATA_0 + i);
846 tegra_dsi_writel(dsi, 0, DSI_INIT_SEQ_DATA_8 + i);
849 for (i = 0; i < 12; i++)
850 tegra_dsi_writel(dsi, 0, DSI_PKT_SEQ_0_LO + i);
852 tegra_dsi_writel(dsi, 0, DSI_DCS_CMDS);
854 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_0);
855 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_1);
856 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_2);
857 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_3);
858 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_4);
860 /* start calibration */
861 tegra_dsi_pad_enable(dsi);
863 value = DSI_PAD_SLEW_UP(0x7) | DSI_PAD_SLEW_DN(0x7) |
864 DSI_PAD_LP_UP(0x1) | DSI_PAD_LP_DN(0x1) |
865 DSI_PAD_OUT_CLK(0x0);
866 tegra_dsi_writel(dsi, value, DSI_PAD_CONTROL_2);
868 err = tegra_mipi_calibrate(dsi->dev);
870 dev_err(dsi->dev, "MIPI calibration failed: %d\n", err);
874 tegra_dsi_writel(dsi, DSI_POWER_CONTROL_ENABLE, DSI_POWER_CONTROL);
875 usleep_range(300, 1000);
880 static int tegra_dsi_exit(struct host1x_client *client)
882 struct tegra_dsi *dsi = host1x_client_to_dsi(client);
885 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
886 err = tegra_dsi_debugfs_exit(dsi);
888 dev_err(dsi->dev, "debugfs cleanup failed: %d\n", err);
891 err = tegra_output_disable(&dsi->output);
893 dev_err(client->dev, "output failed to disable: %d\n", err);
897 err = tegra_output_exit(&dsi->output);
899 dev_err(client->dev, "output cleanup failed: %d\n", err);
906 static const struct host1x_client_ops dsi_client_ops = {
907 .init = tegra_dsi_init,
908 .exit = tegra_dsi_exit,
911 static int tegra_dsi_setup_clocks(struct tegra_dsi *dsi)
916 parent = clk_get_parent(dsi->clk);
920 err = clk_set_parent(parent, dsi->clk_parent);
927 static int tegra_dsi_probe(struct platform_device *pdev)
929 struct tegra_dsi *dsi;
930 struct resource *regs;
933 dsi = devm_kzalloc(&pdev->dev, sizeof(*dsi), GFP_KERNEL);
937 dsi->output.dev = dsi->dev = &pdev->dev;
939 err = tegra_output_probe(&dsi->output);
944 * FIXME: Don't hardcode these. Perhaps they should be queried from
945 * the panel or from the DSI interface's DT node.
947 dsi->format = DSI_FORMAT_24P;
950 dsi->clk = devm_clk_get(&pdev->dev, NULL);
951 if (IS_ERR(dsi->clk))
952 return PTR_ERR(dsi->clk);;
954 err = clk_prepare_enable(dsi->clk);
958 dsi->clk_parent = devm_clk_get(&pdev->dev, "parent");
959 if (IS_ERR(dsi->clk_parent))
960 return PTR_ERR(dsi->clk_parent);
962 err = clk_prepare_enable(dsi->clk_parent);
966 err = tegra_dsi_setup_clocks(dsi);
970 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
971 dsi->regs = devm_ioremap_resource(&pdev->dev, regs);
973 return -EADDRNOTAVAIL;
975 INIT_LIST_HEAD(&dsi->client.list);
976 dsi->client.ops = &dsi_client_ops;
977 dsi->client.dev = &pdev->dev;
979 err = host1x_client_register(&dsi->client);
981 dev_err(&pdev->dev, "failed to register host1x client: %d\n",
986 platform_set_drvdata(pdev, dsi);
991 static int tegra_dsi_remove(struct platform_device *pdev)
993 struct tegra_dsi *dsi = platform_get_drvdata(pdev);
996 err = host1x_client_unregister(&dsi->client);
998 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
1003 clk_disable_unprepare(dsi->clk_parent);
1004 clk_disable_unprepare(dsi->clk);
1006 err = tegra_output_remove(&dsi->output);
1008 dev_err(&pdev->dev, "failed to remove output: %d\n", err);
1015 static const struct of_device_id tegra_dsi_of_match[] = {
1016 { .compatible = "nvidia,tegra114-dsi", },
1020 struct platform_driver tegra_dsi_driver = {
1022 .name = "tegra-dsi",
1023 .of_match_table = tegra_dsi_of_match,
1025 .probe = tegra_dsi_probe,
1026 .remove = tegra_dsi_remove,