blob: 71c1b1a64ccf3da2b3d9bef9bb5823cfbe0bfd0d [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
17#include <drm/drm_mipi_dsi.h>
18#include <drm/drm_panel.h>
19
20#include <video/mipi_display.h>
21
22#include "dc.h"
23#include "drm.h"
24#include "dsi.h"
25#include "mipi-phy.h"
26
27#define DSI_VIDEO_FIFO_DEPTH (1920 / 4)
28#define DSI_HOST_FIFO_DEPTH 64
29
30struct 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
46 enum mipi_dsi_pixel_format format;
47 unsigned int lanes;
48
49 struct tegra_mipi_device *mipi;
50 struct mipi_dsi_host host;
51};
52
53static inline struct tegra_dsi *
54host1x_client_to_dsi(struct host1x_client *client)
55{
56 return container_of(client, struct tegra_dsi, client);
57}
58
59static inline struct tegra_dsi *host_to_tegra(struct mipi_dsi_host *host)
60{
61 return container_of(host, struct tegra_dsi, host);
62}
63
64static inline struct tegra_dsi *to_dsi(struct tegra_output *output)
65{
66 return container_of(output, struct tegra_dsi, output);
67}
68
69static inline unsigned long tegra_dsi_readl(struct tegra_dsi *dsi,
70 unsigned long reg)
71{
72 return readl(dsi->regs + (reg << 2));
73}
74
75static inline void tegra_dsi_writel(struct tegra_dsi *dsi, unsigned long value,
76 unsigned long reg)
77{
78 writel(value, dsi->regs + (reg << 2));
79}
80
81static int tegra_dsi_show_regs(struct seq_file *s, void *data)
82{
83 struct drm_info_node *node = s->private;
84 struct tegra_dsi *dsi = node->info_ent->data;
85
86#define DUMP_REG(name) \
87 seq_printf(s, "%-32s %#05x %08lx\n", #name, name, \
88 tegra_dsi_readl(dsi, name))
89
90 DUMP_REG(DSI_INCR_SYNCPT);
91 DUMP_REG(DSI_INCR_SYNCPT_CONTROL);
92 DUMP_REG(DSI_INCR_SYNCPT_ERROR);
93 DUMP_REG(DSI_CTXSW);
94 DUMP_REG(DSI_RD_DATA);
95 DUMP_REG(DSI_WR_DATA);
96 DUMP_REG(DSI_POWER_CONTROL);
97 DUMP_REG(DSI_INT_ENABLE);
98 DUMP_REG(DSI_INT_STATUS);
99 DUMP_REG(DSI_INT_MASK);
100 DUMP_REG(DSI_HOST_CONTROL);
101 DUMP_REG(DSI_CONTROL);
102 DUMP_REG(DSI_SOL_DELAY);
103 DUMP_REG(DSI_MAX_THRESHOLD);
104 DUMP_REG(DSI_TRIGGER);
105 DUMP_REG(DSI_TX_CRC);
106 DUMP_REG(DSI_STATUS);
107
108 DUMP_REG(DSI_INIT_SEQ_CONTROL);
109 DUMP_REG(DSI_INIT_SEQ_DATA_0);
110 DUMP_REG(DSI_INIT_SEQ_DATA_1);
111 DUMP_REG(DSI_INIT_SEQ_DATA_2);
112 DUMP_REG(DSI_INIT_SEQ_DATA_3);
113 DUMP_REG(DSI_INIT_SEQ_DATA_4);
114 DUMP_REG(DSI_INIT_SEQ_DATA_5);
115 DUMP_REG(DSI_INIT_SEQ_DATA_6);
116 DUMP_REG(DSI_INIT_SEQ_DATA_7);
117
118 DUMP_REG(DSI_PKT_SEQ_0_LO);
119 DUMP_REG(DSI_PKT_SEQ_0_HI);
120 DUMP_REG(DSI_PKT_SEQ_1_LO);
121 DUMP_REG(DSI_PKT_SEQ_1_HI);
122 DUMP_REG(DSI_PKT_SEQ_2_LO);
123 DUMP_REG(DSI_PKT_SEQ_2_HI);
124 DUMP_REG(DSI_PKT_SEQ_3_LO);
125 DUMP_REG(DSI_PKT_SEQ_3_HI);
126 DUMP_REG(DSI_PKT_SEQ_4_LO);
127 DUMP_REG(DSI_PKT_SEQ_4_HI);
128 DUMP_REG(DSI_PKT_SEQ_5_LO);
129 DUMP_REG(DSI_PKT_SEQ_5_HI);
130
131 DUMP_REG(DSI_DCS_CMDS);
132
133 DUMP_REG(DSI_PKT_LEN_0_1);
134 DUMP_REG(DSI_PKT_LEN_2_3);
135 DUMP_REG(DSI_PKT_LEN_4_5);
136 DUMP_REG(DSI_PKT_LEN_6_7);
137
138 DUMP_REG(DSI_PHY_TIMING_0);
139 DUMP_REG(DSI_PHY_TIMING_1);
140 DUMP_REG(DSI_PHY_TIMING_2);
141 DUMP_REG(DSI_BTA_TIMING);
142
143 DUMP_REG(DSI_TIMEOUT_0);
144 DUMP_REG(DSI_TIMEOUT_1);
145 DUMP_REG(DSI_TO_TALLY);
146
147 DUMP_REG(DSI_PAD_CONTROL_0);
148 DUMP_REG(DSI_PAD_CONTROL_CD);
149 DUMP_REG(DSI_PAD_CD_STATUS);
150 DUMP_REG(DSI_VIDEO_MODE_CONTROL);
151 DUMP_REG(DSI_PAD_CONTROL_1);
152 DUMP_REG(DSI_PAD_CONTROL_2);
153 DUMP_REG(DSI_PAD_CONTROL_3);
154 DUMP_REG(DSI_PAD_CONTROL_4);
155
156 DUMP_REG(DSI_GANGED_MODE_CONTROL);
157 DUMP_REG(DSI_GANGED_MODE_START);
158 DUMP_REG(DSI_GANGED_MODE_SIZE);
159
160 DUMP_REG(DSI_RAW_DATA_BYTE_COUNT);
161 DUMP_REG(DSI_ULTRA_LOW_POWER_CONTROL);
162
163 DUMP_REG(DSI_INIT_SEQ_DATA_8);
164 DUMP_REG(DSI_INIT_SEQ_DATA_9);
165 DUMP_REG(DSI_INIT_SEQ_DATA_10);
166 DUMP_REG(DSI_INIT_SEQ_DATA_11);
167 DUMP_REG(DSI_INIT_SEQ_DATA_12);
168 DUMP_REG(DSI_INIT_SEQ_DATA_13);
169 DUMP_REG(DSI_INIT_SEQ_DATA_14);
170 DUMP_REG(DSI_INIT_SEQ_DATA_15);
171
172#undef DUMP_REG
173
174 return 0;
175}
176
177static struct drm_info_list debugfs_files[] = {
178 { "regs", tegra_dsi_show_regs, 0, NULL },
179};
180
181static int tegra_dsi_debugfs_init(struct tegra_dsi *dsi,
182 struct drm_minor *minor)
183{
184 const char *name = dev_name(dsi->dev);
185 unsigned int i;
186 int err;
187
188 dsi->debugfs = debugfs_create_dir(name, minor->debugfs_root);
189 if (!dsi->debugfs)
190 return -ENOMEM;
191
192 dsi->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
193 GFP_KERNEL);
194 if (!dsi->debugfs_files) {
195 err = -ENOMEM;
196 goto remove;
197 }
198
199 for (i = 0; i < ARRAY_SIZE(debugfs_files); i++)
200 dsi->debugfs_files[i].data = dsi;
201
202 err = drm_debugfs_create_files(dsi->debugfs_files,
203 ARRAY_SIZE(debugfs_files),
204 dsi->debugfs, minor);
205 if (err < 0)
206 goto free;
207
208 dsi->minor = minor;
209
210 return 0;
211
212free:
213 kfree(dsi->debugfs_files);
214 dsi->debugfs_files = NULL;
215remove:
216 debugfs_remove(dsi->debugfs);
217 dsi->debugfs = NULL;
218
219 return err;
220}
221
222static int tegra_dsi_debugfs_exit(struct tegra_dsi *dsi)
223{
224 drm_debugfs_remove_files(dsi->debugfs_files, ARRAY_SIZE(debugfs_files),
225 dsi->minor);
226 dsi->minor = NULL;
227
228 kfree(dsi->debugfs_files);
229 dsi->debugfs_files = NULL;
230
231 debugfs_remove(dsi->debugfs);
232 dsi->debugfs = NULL;
233
234 return 0;
235}
236
237#define PKT_ID0(id) ((((id) & 0x3f) << 3) | (1 << 9))
238#define PKT_LEN0(len) (((len) & 0x07) << 0)
239#define PKT_ID1(id) ((((id) & 0x3f) << 13) | (1 << 19))
240#define PKT_LEN1(len) (((len) & 0x07) << 10)
241#define PKT_ID2(id) ((((id) & 0x3f) << 23) | (1 << 29))
242#define PKT_LEN2(len) (((len) & 0x07) << 20)
243
244#define PKT_LP (1 << 30)
245#define NUM_PKT_SEQ 12
246
247/* non-burst mode with sync-end */
248static const u32 pkt_seq_vnb_syne[NUM_PKT_SEQ] = {
249 [ 0] = PKT_ID0(MIPI_DSI_V_SYNC_START) | PKT_LEN0(0) |
250 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
251 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0) |
252 PKT_LP,
253 [ 1] = 0,
254 [ 2] = PKT_ID0(MIPI_DSI_V_SYNC_END) | PKT_LEN0(0) |
255 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
256 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0) |
257 PKT_LP,
258 [ 3] = 0,
259 [ 4] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
260 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
261 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0) |
262 PKT_LP,
263 [ 5] = 0,
264 [ 6] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
265 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
266 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0),
267 [ 7] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(2) |
268 PKT_ID1(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN1(3) |
269 PKT_ID2(MIPI_DSI_BLANKING_PACKET) | PKT_LEN2(4),
270 [ 8] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
271 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
272 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0) |
273 PKT_LP,
274 [ 9] = 0,
275 [10] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
276 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
277 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0),
278 [11] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(2) |
279 PKT_ID1(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN1(3) |
280 PKT_ID2(MIPI_DSI_BLANKING_PACKET) | PKT_LEN2(4),
281};
282
283static int tegra_dsi_set_phy_timing(struct tegra_dsi *dsi)
284{
285 struct mipi_dphy_timing timing;
286 unsigned long value, period;
287 long rate;
288 int err;
289
290 rate = clk_get_rate(dsi->clk);
291 if (rate < 0)
292 return rate;
293
294 period = DIV_ROUND_CLOSEST(1000000000UL, rate * 2);
295
296 err = mipi_dphy_timing_get_default(&timing, period);
297 if (err < 0)
298 return err;
299
300 err = mipi_dphy_timing_validate(&timing, period);
301 if (err < 0) {
302 dev_err(dsi->dev, "failed to validate D-PHY timing: %d\n", err);
303 return err;
304 }
305
306 /*
307 * The D-PHY timing fields below are expressed in byte-clock cycles,
308 * so multiply the period by 8.
309 */
310 period *= 8;
311
312 value = DSI_TIMING_FIELD(timing.hsexit, period, 1) << 24 |
313 DSI_TIMING_FIELD(timing.hstrail, period, 0) << 16 |
314 DSI_TIMING_FIELD(timing.hszero, period, 3) << 8 |
315 DSI_TIMING_FIELD(timing.hsprepare, period, 1);
316 tegra_dsi_writel(dsi, value, DSI_PHY_TIMING_0);
317
318 value = DSI_TIMING_FIELD(timing.clktrail, period, 1) << 24 |
319 DSI_TIMING_FIELD(timing.clkpost, period, 1) << 16 |
320 DSI_TIMING_FIELD(timing.clkzero, period, 1) << 8 |
321 DSI_TIMING_FIELD(timing.lpx, period, 1);
322 tegra_dsi_writel(dsi, value, DSI_PHY_TIMING_1);
323
324 value = DSI_TIMING_FIELD(timing.clkprepare, period, 1) << 16 |
325 DSI_TIMING_FIELD(timing.clkpre, period, 1) << 8 |
326 DSI_TIMING_FIELD(0xff * period, period, 0) << 0;
327 tegra_dsi_writel(dsi, value, DSI_PHY_TIMING_2);
328
329 value = DSI_TIMING_FIELD(timing.taget, period, 1) << 16 |
330 DSI_TIMING_FIELD(timing.tasure, period, 1) << 8 |
331 DSI_TIMING_FIELD(timing.tago, period, 1);
332 tegra_dsi_writel(dsi, value, DSI_BTA_TIMING);
333
334 return 0;
335}
336
337static int tegra_dsi_get_muldiv(enum mipi_dsi_pixel_format format,
338 unsigned int *mulp, unsigned int *divp)
339{
340 switch (format) {
341 case MIPI_DSI_FMT_RGB666_PACKED:
342 case MIPI_DSI_FMT_RGB888:
343 *mulp = 3;
344 *divp = 1;
345 break;
346
347 case MIPI_DSI_FMT_RGB565:
348 *mulp = 2;
349 *divp = 1;
350 break;
351
352 case MIPI_DSI_FMT_RGB666:
353 *mulp = 9;
354 *divp = 4;
355 break;
356
357 default:
358 return -EINVAL;
359 }
360
361 return 0;
362}
363
Thierry Redingf7d68892014-03-13 08:50:39 +0100364static int tegra_dsi_get_format(enum mipi_dsi_pixel_format format,
365 enum tegra_dsi_format *fmt)
366{
367 switch (format) {
368 case MIPI_DSI_FMT_RGB888:
369 *fmt = TEGRA_DSI_FORMAT_24P;
370 break;
371
372 case MIPI_DSI_FMT_RGB666:
373 *fmt = TEGRA_DSI_FORMAT_18NP;
374 break;
375
376 case MIPI_DSI_FMT_RGB666_PACKED:
377 *fmt = TEGRA_DSI_FORMAT_18P;
378 break;
379
380 case MIPI_DSI_FMT_RGB565:
381 *fmt = TEGRA_DSI_FORMAT_16P;
382 break;
383
384 default:
385 return -EINVAL;
386 }
387
388 return 0;
389}
390
Thierry Redingdec72732013-09-03 08:45:46 +0200391static int tegra_output_dsi_enable(struct tegra_output *output)
392{
393 struct tegra_dc *dc = to_tegra_dc(output->encoder.crtc);
394 struct drm_display_mode *mode = &dc->base.mode;
395 unsigned int hact, hsw, hbp, hfp, i, mul, div;
396 struct tegra_dsi *dsi = to_dsi(output);
397 /* FIXME: don't hardcode this */
398 const u32 *pkt_seq = pkt_seq_vnb_syne;
Thierry Redingf7d68892014-03-13 08:50:39 +0100399 enum tegra_dsi_format format;
Thierry Redingdec72732013-09-03 08:45:46 +0200400 unsigned long value;
401 int err;
402
403 err = tegra_dsi_get_muldiv(dsi->format, &mul, &div);
404 if (err < 0)
405 return err;
406
Thierry Redingf7d68892014-03-13 08:50:39 +0100407 err = tegra_dsi_get_format(dsi->format, &format);
408 if (err < 0)
409 return err;
410
Thierry Redingdec72732013-09-03 08:45:46 +0200411 err = clk_enable(dsi->clk);
412 if (err < 0)
413 return err;
414
415 reset_control_deassert(dsi->rst);
416
Thierry Redingf7d68892014-03-13 08:50:39 +0100417 value = DSI_CONTROL_CHANNEL(0) | DSI_CONTROL_FORMAT(format) |
Thierry Redingdec72732013-09-03 08:45:46 +0200418 DSI_CONTROL_LANES(dsi->lanes - 1) |
419 DSI_CONTROL_SOURCE(dc->pipe);
420 tegra_dsi_writel(dsi, value, DSI_CONTROL);
421
422 tegra_dsi_writel(dsi, DSI_VIDEO_FIFO_DEPTH, DSI_MAX_THRESHOLD);
423
424 value = DSI_HOST_CONTROL_HS | DSI_HOST_CONTROL_CS |
425 DSI_HOST_CONTROL_ECC;
426 tegra_dsi_writel(dsi, value, DSI_HOST_CONTROL);
427
428 value = tegra_dsi_readl(dsi, DSI_CONTROL);
429 value |= DSI_CONTROL_HS_CLK_CTRL;
430 value &= ~DSI_CONTROL_TX_TRIG(3);
431 value &= ~DSI_CONTROL_DCS_ENABLE;
432 value |= DSI_CONTROL_VIDEO_ENABLE;
433 value &= ~DSI_CONTROL_HOST_ENABLE;
434 tegra_dsi_writel(dsi, value, DSI_CONTROL);
435
436 err = tegra_dsi_set_phy_timing(dsi);
437 if (err < 0)
438 return err;
439
440 for (i = 0; i < NUM_PKT_SEQ; i++)
441 tegra_dsi_writel(dsi, pkt_seq[i], DSI_PKT_SEQ_0_LO + i);
442
443 /* horizontal active pixels */
444 hact = mode->hdisplay * mul / div;
445
446 /* horizontal sync width */
447 hsw = (mode->hsync_end - mode->hsync_start) * mul / div;
448 hsw -= 10;
449
450 /* horizontal back porch */
451 hbp = (mode->htotal - mode->hsync_end) * mul / div;
452 hbp -= 14;
453
454 /* horizontal front porch */
455 hfp = (mode->hsync_start - mode->hdisplay) * mul / div;
456 hfp -= 8;
457
458 tegra_dsi_writel(dsi, hsw << 16 | 0, DSI_PKT_LEN_0_1);
459 tegra_dsi_writel(dsi, hact << 16 | hbp, DSI_PKT_LEN_2_3);
460 tegra_dsi_writel(dsi, hfp, DSI_PKT_LEN_4_5);
461 tegra_dsi_writel(dsi, 0x0f0f << 16, DSI_PKT_LEN_6_7);
462
463 /* set SOL delay */
464 tegra_dsi_writel(dsi, 8 * mul / div, DSI_SOL_DELAY);
465
466 /* enable display controller */
467 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
468 value |= DSI_ENABLE;
469 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
470
Thierry Redingdec72732013-09-03 08:45:46 +0200471 value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
472 value &= ~DISP_CTRL_MODE_MASK;
473 value |= DISP_CTRL_MODE_C_DISPLAY;
474 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
475
Thierry Reding72d30282013-12-12 11:06:55 +0100476 value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
477 value |= PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
478 PW4_ENABLE | PM0_ENABLE | PM1_ENABLE;
479 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
480
Thierry Redingdec72732013-09-03 08:45:46 +0200481 tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
482 tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
483
484 /* enable DSI controller */
485 value = tegra_dsi_readl(dsi, DSI_POWER_CONTROL);
486 value |= DSI_POWER_CONTROL_ENABLE;
487 tegra_dsi_writel(dsi, value, DSI_POWER_CONTROL);
488
489 return 0;
490}
491
492static int tegra_output_dsi_disable(struct tegra_output *output)
493{
494 struct tegra_dc *dc = to_tegra_dc(output->encoder.crtc);
495 struct tegra_dsi *dsi = to_dsi(output);
496 unsigned long value;
497
498 /* disable DSI controller */
499 value = tegra_dsi_readl(dsi, DSI_POWER_CONTROL);
500 value &= DSI_POWER_CONTROL_ENABLE;
501 tegra_dsi_writel(dsi, value, DSI_POWER_CONTROL);
502
503 /*
Thierry Reding72d30282013-12-12 11:06:55 +0100504 * The following accesses registers of the display controller, so make
505 * sure it's only executed when the output is attached to one.
Thierry Redingdec72732013-09-03 08:45:46 +0200506 */
507 if (dc) {
Thierry Reding72d30282013-12-12 11:06:55 +0100508 value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
509 value &= ~(PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
510 PW4_ENABLE | PM0_ENABLE | PM1_ENABLE);
511 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
512
Thierry Redingdec72732013-09-03 08:45:46 +0200513 value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
514 value &= ~DISP_CTRL_MODE_MASK;
515 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
516
517 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
518 value &= ~DSI_ENABLE;
519 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
Thierry Reding72d30282013-12-12 11:06:55 +0100520
521 tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
522 tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
Thierry Redingdec72732013-09-03 08:45:46 +0200523 }
524
525 clk_disable(dsi->clk);
526
527 return 0;
528}
529
530static int tegra_output_dsi_setup_clock(struct tegra_output *output,
531 struct clk *clk, unsigned long pclk)
532{
533 struct tegra_dc *dc = to_tegra_dc(output->encoder.crtc);
534 struct drm_display_mode *mode = &dc->base.mode;
535 unsigned int timeout, mul, div, vrefresh;
536 struct tegra_dsi *dsi = to_dsi(output);
537 unsigned long bclk, plld, value;
538 struct clk *base;
539 int err;
540
541 err = tegra_dsi_get_muldiv(dsi->format, &mul, &div);
542 if (err < 0)
543 return err;
544
545 vrefresh = drm_mode_vrefresh(mode);
546
547 pclk = mode->htotal * mode->vtotal * vrefresh;
548 bclk = (pclk * mul) / (div * dsi->lanes);
549 plld = DIV_ROUND_UP(bclk * 8, 1000000);
550 pclk = (plld * 1000000) / 2;
551
552 err = clk_set_parent(clk, dsi->clk_parent);
553 if (err < 0) {
554 dev_err(dsi->dev, "failed to set parent clock: %d\n", err);
555 return err;
556 }
557
558 base = clk_get_parent(dsi->clk_parent);
559
560 /*
561 * This assumes that the parent clock is pll_d_out0 or pll_d2_out
562 * respectively, each of which divides the base pll_d by 2.
563 */
564 err = clk_set_rate(base, pclk * 2);
565 if (err < 0) {
566 dev_err(dsi->dev, "failed to set base clock rate to %lu Hz\n",
567 pclk * 2);
568 return err;
569 }
570
571 /*
572 * XXX: Move the below somewhere else so that we don't need to have
573 * access to the vrefresh in this function?
574 */
575
576 /* one frame high-speed transmission timeout */
577 timeout = (bclk / vrefresh) / 512;
578 value = DSI_TIMEOUT_LRX(0x2000) | DSI_TIMEOUT_HTX(timeout);
579 tegra_dsi_writel(dsi, value, DSI_TIMEOUT_0);
580
581 /* 2 ms peripheral timeout for panel */
582 timeout = 2 * bclk / 512 * 1000;
583 value = DSI_TIMEOUT_PR(timeout) | DSI_TIMEOUT_TA(0x2000);
584 tegra_dsi_writel(dsi, value, DSI_TIMEOUT_1);
585
586 value = DSI_TALLY_TA(0) | DSI_TALLY_LRX(0) | DSI_TALLY_HTX(0);
587 tegra_dsi_writel(dsi, value, DSI_TO_TALLY);
588
589 return 0;
590}
591
592static int tegra_output_dsi_check_mode(struct tegra_output *output,
593 struct drm_display_mode *mode,
594 enum drm_mode_status *status)
595{
596 /*
597 * FIXME: For now, always assume that the mode is okay.
598 */
599
600 *status = MODE_OK;
601
602 return 0;
603}
604
605static const struct tegra_output_ops dsi_ops = {
606 .enable = tegra_output_dsi_enable,
607 .disable = tegra_output_dsi_disable,
608 .setup_clock = tegra_output_dsi_setup_clock,
609 .check_mode = tegra_output_dsi_check_mode,
610};
611
612static int tegra_dsi_pad_enable(struct tegra_dsi *dsi)
613{
614 unsigned long value;
615
616 value = DSI_PAD_CONTROL_VS1_PULLDN(0) | DSI_PAD_CONTROL_VS1_PDIO(0);
617 tegra_dsi_writel(dsi, value, DSI_PAD_CONTROL_0);
618
619 return 0;
620}
621
622static int tegra_dsi_pad_calibrate(struct tegra_dsi *dsi)
623{
624 unsigned long value;
625
626 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_0);
627 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_1);
628 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_2);
629 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_3);
630 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_4);
631
632 /* start calibration */
633 tegra_dsi_pad_enable(dsi);
634
635 value = DSI_PAD_SLEW_UP(0x7) | DSI_PAD_SLEW_DN(0x7) |
636 DSI_PAD_LP_UP(0x1) | DSI_PAD_LP_DN(0x1) |
637 DSI_PAD_OUT_CLK(0x0);
638 tegra_dsi_writel(dsi, value, DSI_PAD_CONTROL_2);
639
640 return tegra_mipi_calibrate(dsi->mipi);
641}
642
643static int tegra_dsi_init(struct host1x_client *client)
644{
645 struct tegra_drm *tegra = dev_get_drvdata(client->parent);
646 struct tegra_dsi *dsi = host1x_client_to_dsi(client);
647 unsigned long value, i;
648 int err;
649
650 dsi->output.type = TEGRA_OUTPUT_DSI;
651 dsi->output.dev = client->dev;
652 dsi->output.ops = &dsi_ops;
653
654 err = tegra_output_init(tegra->drm, &dsi->output);
655 if (err < 0) {
656 dev_err(client->dev, "output setup failed: %d\n", err);
657 return err;
658 }
659
660 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
661 err = tegra_dsi_debugfs_init(dsi, tegra->drm->primary);
662 if (err < 0)
663 dev_err(dsi->dev, "debugfs setup failed: %d\n", err);
664 }
665
666 /*
667 * enable high-speed mode, checksum generation, ECC generation and
668 * disable raw mode
669 */
670 value = tegra_dsi_readl(dsi, DSI_HOST_CONTROL);
671 value |= DSI_HOST_CONTROL_ECC | DSI_HOST_CONTROL_CS |
672 DSI_HOST_CONTROL_HS;
673 value &= ~DSI_HOST_CONTROL_RAW;
674 tegra_dsi_writel(dsi, value, DSI_HOST_CONTROL);
675
676 tegra_dsi_writel(dsi, 0, DSI_SOL_DELAY);
677 tegra_dsi_writel(dsi, 0, DSI_MAX_THRESHOLD);
678
679 tegra_dsi_writel(dsi, 0, DSI_INIT_SEQ_CONTROL);
680
681 for (i = 0; i < 8; i++) {
682 tegra_dsi_writel(dsi, 0, DSI_INIT_SEQ_DATA_0 + i);
683 tegra_dsi_writel(dsi, 0, DSI_INIT_SEQ_DATA_8 + i);
684 }
685
686 for (i = 0; i < 12; i++)
687 tegra_dsi_writel(dsi, 0, DSI_PKT_SEQ_0_LO + i);
688
689 tegra_dsi_writel(dsi, 0, DSI_DCS_CMDS);
690
691 err = tegra_dsi_pad_calibrate(dsi);
692 if (err < 0) {
693 dev_err(dsi->dev, "MIPI calibration failed: %d\n", err);
694 return err;
695 }
696
697 tegra_dsi_writel(dsi, DSI_POWER_CONTROL_ENABLE, DSI_POWER_CONTROL);
698 usleep_range(300, 1000);
699
700 return 0;
701}
702
703static int tegra_dsi_exit(struct host1x_client *client)
704{
705 struct tegra_dsi *dsi = host1x_client_to_dsi(client);
706 int err;
707
708 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
709 err = tegra_dsi_debugfs_exit(dsi);
710 if (err < 0)
711 dev_err(dsi->dev, "debugfs cleanup failed: %d\n", err);
712 }
713
714 err = tegra_output_disable(&dsi->output);
715 if (err < 0) {
716 dev_err(client->dev, "output failed to disable: %d\n", err);
717 return err;
718 }
719
720 err = tegra_output_exit(&dsi->output);
721 if (err < 0) {
722 dev_err(client->dev, "output cleanup failed: %d\n", err);
723 return err;
724 }
725
726 return 0;
727}
728
729static const struct host1x_client_ops dsi_client_ops = {
730 .init = tegra_dsi_init,
731 .exit = tegra_dsi_exit,
732};
733
734static int tegra_dsi_setup_clocks(struct tegra_dsi *dsi)
735{
736 struct clk *parent;
737 int err;
738
739 parent = clk_get_parent(dsi->clk);
740 if (!parent)
741 return -EINVAL;
742
743 err = clk_set_parent(parent, dsi->clk_parent);
744 if (err < 0)
745 return err;
746
747 return 0;
748}
749
750static void tegra_dsi_initialize(struct tegra_dsi *dsi)
751{
752 unsigned int i;
753
754 tegra_dsi_writel(dsi, 0, DSI_POWER_CONTROL);
755
756 tegra_dsi_writel(dsi, 0, DSI_INT_ENABLE);
757 tegra_dsi_writel(dsi, 0, DSI_INT_STATUS);
758 tegra_dsi_writel(dsi, 0, DSI_INT_MASK);
759
760 tegra_dsi_writel(dsi, 0, DSI_HOST_CONTROL);
761 tegra_dsi_writel(dsi, 0, DSI_CONTROL);
762
763 tegra_dsi_writel(dsi, 0, DSI_SOL_DELAY);
764 tegra_dsi_writel(dsi, 0, DSI_MAX_THRESHOLD);
765
766 tegra_dsi_writel(dsi, 0, DSI_INIT_SEQ_CONTROL);
767
768 for (i = 0; i < 8; i++) {
769 tegra_dsi_writel(dsi, 0, DSI_INIT_SEQ_DATA_0 + i);
770 tegra_dsi_writel(dsi, 0, DSI_INIT_SEQ_DATA_8 + i);
771 }
772
773 for (i = 0; i < 12; i++)
774 tegra_dsi_writel(dsi, 0, DSI_PKT_SEQ_0_LO + i);
775
776 tegra_dsi_writel(dsi, 0, DSI_DCS_CMDS);
777
778 for (i = 0; i < 4; i++)
779 tegra_dsi_writel(dsi, 0, DSI_PKT_LEN_0_1 + i);
780
781 tegra_dsi_writel(dsi, 0x00000000, DSI_PHY_TIMING_0);
782 tegra_dsi_writel(dsi, 0x00000000, DSI_PHY_TIMING_1);
783 tegra_dsi_writel(dsi, 0x000000ff, DSI_PHY_TIMING_2);
784 tegra_dsi_writel(dsi, 0x00000000, DSI_BTA_TIMING);
785
786 tegra_dsi_writel(dsi, 0, DSI_TIMEOUT_0);
787 tegra_dsi_writel(dsi, 0, DSI_TIMEOUT_1);
788 tegra_dsi_writel(dsi, 0, DSI_TO_TALLY);
789
790 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_0);
791 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_CD);
792 tegra_dsi_writel(dsi, 0, DSI_PAD_CD_STATUS);
793 tegra_dsi_writel(dsi, 0, DSI_VIDEO_MODE_CONTROL);
794 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_1);
795 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_2);
796 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_3);
797 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_4);
798
799 tegra_dsi_writel(dsi, 0, DSI_GANGED_MODE_CONTROL);
800 tegra_dsi_writel(dsi, 0, DSI_GANGED_MODE_START);
801 tegra_dsi_writel(dsi, 0, DSI_GANGED_MODE_SIZE);
802}
803
804static int tegra_dsi_host_attach(struct mipi_dsi_host *host,
805 struct mipi_dsi_device *device)
806{
807 struct tegra_dsi *dsi = host_to_tegra(host);
808 struct tegra_output *output = &dsi->output;
809
810 dsi->format = device->format;
811 dsi->lanes = device->lanes;
812
813 output->panel = of_drm_find_panel(device->dev.of_node);
814 if (output->panel) {
815 if (output->connector.dev)
816 drm_helper_hpd_irq_event(output->connector.dev);
817 }
818
819 return 0;
820}
821
822static int tegra_dsi_host_detach(struct mipi_dsi_host *host,
823 struct mipi_dsi_device *device)
824{
825 struct tegra_dsi *dsi = host_to_tegra(host);
826 struct tegra_output *output = &dsi->output;
827
828 if (output->panel && &device->dev == output->panel->dev) {
829 if (output->connector.dev)
830 drm_helper_hpd_irq_event(output->connector.dev);
831
832 output->panel = NULL;
833 }
834
835 return 0;
836}
837
838static const struct mipi_dsi_host_ops tegra_dsi_host_ops = {
839 .attach = tegra_dsi_host_attach,
840 .detach = tegra_dsi_host_detach,
841};
842
843static int tegra_dsi_probe(struct platform_device *pdev)
844{
845 struct tegra_dsi *dsi;
846 struct resource *regs;
847 int err;
848
849 dsi = devm_kzalloc(&pdev->dev, sizeof(*dsi), GFP_KERNEL);
850 if (!dsi)
851 return -ENOMEM;
852
853 dsi->output.dev = dsi->dev = &pdev->dev;
854
855 err = tegra_output_probe(&dsi->output);
856 if (err < 0)
857 return err;
858
859 /*
860 * Assume these values by default. When a DSI peripheral driver
861 * attaches to the DSI host, the parameters will be taken from
862 * the attached device.
863 */
864 dsi->format = MIPI_DSI_FMT_RGB888;
865 dsi->lanes = 4;
866
867 dsi->rst = devm_reset_control_get(&pdev->dev, "dsi");
868 if (IS_ERR(dsi->rst))
869 return PTR_ERR(dsi->rst);
870
871 dsi->clk = devm_clk_get(&pdev->dev, NULL);
872 if (IS_ERR(dsi->clk)) {
873 dev_err(&pdev->dev, "cannot get DSI clock\n");
874 return PTR_ERR(dsi->clk);
875 }
876
877 err = clk_prepare_enable(dsi->clk);
878 if (err < 0) {
879 dev_err(&pdev->dev, "cannot enable DSI clock\n");
880 return err;
881 }
882
883 dsi->clk_lp = devm_clk_get(&pdev->dev, "lp");
884 if (IS_ERR(dsi->clk_lp)) {
885 dev_err(&pdev->dev, "cannot get low-power clock\n");
886 return PTR_ERR(dsi->clk_lp);
887 }
888
889 err = clk_prepare_enable(dsi->clk_lp);
890 if (err < 0) {
891 dev_err(&pdev->dev, "cannot enable low-power clock\n");
892 return err;
893 }
894
895 dsi->clk_parent = devm_clk_get(&pdev->dev, "parent");
896 if (IS_ERR(dsi->clk_parent)) {
897 dev_err(&pdev->dev, "cannot get parent clock\n");
898 return PTR_ERR(dsi->clk_parent);
899 }
900
901 err = clk_prepare_enable(dsi->clk_parent);
902 if (err < 0) {
903 dev_err(&pdev->dev, "cannot enable parent clock\n");
904 return err;
905 }
906
907 err = tegra_dsi_setup_clocks(dsi);
908 if (err < 0) {
909 dev_err(&pdev->dev, "cannot setup clocks\n");
910 return err;
911 }
912
913 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
914 dsi->regs = devm_ioremap_resource(&pdev->dev, regs);
Wei Yongjun85316ea2013-10-21 11:34:07 +0800915 if (IS_ERR(dsi->regs))
916 return PTR_ERR(dsi->regs);
Thierry Redingdec72732013-09-03 08:45:46 +0200917
918 tegra_dsi_initialize(dsi);
919
920 dsi->mipi = tegra_mipi_request(&pdev->dev);
921 if (IS_ERR(dsi->mipi))
922 return PTR_ERR(dsi->mipi);
923
924 dsi->host.ops = &tegra_dsi_host_ops;
925 dsi->host.dev = &pdev->dev;
926
927 err = mipi_dsi_host_register(&dsi->host);
928 if (err < 0) {
929 dev_err(&pdev->dev, "failed to register DSI host: %d\n", err);
930 return err;
931 }
932
933 INIT_LIST_HEAD(&dsi->client.list);
934 dsi->client.ops = &dsi_client_ops;
935 dsi->client.dev = &pdev->dev;
936
937 err = host1x_client_register(&dsi->client);
938 if (err < 0) {
939 dev_err(&pdev->dev, "failed to register host1x client: %d\n",
940 err);
941 return err;
942 }
943
944 platform_set_drvdata(pdev, dsi);
945
946 return 0;
947}
948
949static int tegra_dsi_remove(struct platform_device *pdev)
950{
951 struct tegra_dsi *dsi = platform_get_drvdata(pdev);
952 int err;
953
954 err = host1x_client_unregister(&dsi->client);
955 if (err < 0) {
956 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
957 err);
958 return err;
959 }
960
961 mipi_dsi_host_unregister(&dsi->host);
962 tegra_mipi_free(dsi->mipi);
963
964 clk_disable_unprepare(dsi->clk_parent);
965 clk_disable_unprepare(dsi->clk_lp);
966 clk_disable_unprepare(dsi->clk);
967
968 err = tegra_output_remove(&dsi->output);
969 if (err < 0) {
970 dev_err(&pdev->dev, "failed to remove output: %d\n", err);
971 return err;
972 }
973
974 return 0;
975}
976
977static const struct of_device_id tegra_dsi_of_match[] = {
978 { .compatible = "nvidia,tegra114-dsi", },
979 { },
980};
981
982struct platform_driver tegra_dsi_driver = {
983 .driver = {
984 .name = "tegra-dsi",
985 .of_match_table = tegra_dsi_of_match,
986 },
987 .probe = tegra_dsi_probe,
988 .remove = tegra_dsi_remove,
989};