blob: 5e2bd843dbe23d87b821b74cf63e8c2e74c80421 [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);
Thierry Redingdec72732013-09-03 08:45:46 +0200647 int err;
648
649 dsi->output.type = TEGRA_OUTPUT_DSI;
650 dsi->output.dev = client->dev;
651 dsi->output.ops = &dsi_ops;
652
653 err = tegra_output_init(tegra->drm, &dsi->output);
654 if (err < 0) {
655 dev_err(client->dev, "output setup failed: %d\n", err);
656 return err;
657 }
658
659 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
660 err = tegra_dsi_debugfs_init(dsi, tegra->drm->primary);
661 if (err < 0)
662 dev_err(dsi->dev, "debugfs setup failed: %d\n", err);
663 }
664
Thierry Redingdec72732013-09-03 08:45:46 +0200665 err = tegra_dsi_pad_calibrate(dsi);
666 if (err < 0) {
667 dev_err(dsi->dev, "MIPI calibration failed: %d\n", err);
668 return err;
669 }
670
Thierry Redingdec72732013-09-03 08:45:46 +0200671 return 0;
672}
673
674static int tegra_dsi_exit(struct host1x_client *client)
675{
676 struct tegra_dsi *dsi = host1x_client_to_dsi(client);
677 int err;
678
679 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
680 err = tegra_dsi_debugfs_exit(dsi);
681 if (err < 0)
682 dev_err(dsi->dev, "debugfs cleanup failed: %d\n", err);
683 }
684
685 err = tegra_output_disable(&dsi->output);
686 if (err < 0) {
687 dev_err(client->dev, "output failed to disable: %d\n", err);
688 return err;
689 }
690
691 err = tegra_output_exit(&dsi->output);
692 if (err < 0) {
693 dev_err(client->dev, "output cleanup failed: %d\n", err);
694 return err;
695 }
696
697 return 0;
698}
699
700static const struct host1x_client_ops dsi_client_ops = {
701 .init = tegra_dsi_init,
702 .exit = tegra_dsi_exit,
703};
704
705static int tegra_dsi_setup_clocks(struct tegra_dsi *dsi)
706{
707 struct clk *parent;
708 int err;
709
710 parent = clk_get_parent(dsi->clk);
711 if (!parent)
712 return -EINVAL;
713
714 err = clk_set_parent(parent, dsi->clk_parent);
715 if (err < 0)
716 return err;
717
718 return 0;
719}
720
Thierry Redingdec72732013-09-03 08:45:46 +0200721static int tegra_dsi_host_attach(struct mipi_dsi_host *host,
722 struct mipi_dsi_device *device)
723{
724 struct tegra_dsi *dsi = host_to_tegra(host);
725 struct tegra_output *output = &dsi->output;
726
727 dsi->format = device->format;
728 dsi->lanes = device->lanes;
729
730 output->panel = of_drm_find_panel(device->dev.of_node);
731 if (output->panel) {
732 if (output->connector.dev)
733 drm_helper_hpd_irq_event(output->connector.dev);
734 }
735
736 return 0;
737}
738
739static int tegra_dsi_host_detach(struct mipi_dsi_host *host,
740 struct mipi_dsi_device *device)
741{
742 struct tegra_dsi *dsi = host_to_tegra(host);
743 struct tegra_output *output = &dsi->output;
744
745 if (output->panel && &device->dev == output->panel->dev) {
746 if (output->connector.dev)
747 drm_helper_hpd_irq_event(output->connector.dev);
748
749 output->panel = NULL;
750 }
751
752 return 0;
753}
754
755static const struct mipi_dsi_host_ops tegra_dsi_host_ops = {
756 .attach = tegra_dsi_host_attach,
757 .detach = tegra_dsi_host_detach,
758};
759
760static int tegra_dsi_probe(struct platform_device *pdev)
761{
762 struct tegra_dsi *dsi;
763 struct resource *regs;
764 int err;
765
766 dsi = devm_kzalloc(&pdev->dev, sizeof(*dsi), GFP_KERNEL);
767 if (!dsi)
768 return -ENOMEM;
769
770 dsi->output.dev = dsi->dev = &pdev->dev;
771
772 err = tegra_output_probe(&dsi->output);
773 if (err < 0)
774 return err;
775
776 /*
777 * Assume these values by default. When a DSI peripheral driver
778 * attaches to the DSI host, the parameters will be taken from
779 * the attached device.
780 */
781 dsi->format = MIPI_DSI_FMT_RGB888;
782 dsi->lanes = 4;
783
784 dsi->rst = devm_reset_control_get(&pdev->dev, "dsi");
785 if (IS_ERR(dsi->rst))
786 return PTR_ERR(dsi->rst);
787
788 dsi->clk = devm_clk_get(&pdev->dev, NULL);
789 if (IS_ERR(dsi->clk)) {
790 dev_err(&pdev->dev, "cannot get DSI clock\n");
791 return PTR_ERR(dsi->clk);
792 }
793
794 err = clk_prepare_enable(dsi->clk);
795 if (err < 0) {
796 dev_err(&pdev->dev, "cannot enable DSI clock\n");
797 return err;
798 }
799
800 dsi->clk_lp = devm_clk_get(&pdev->dev, "lp");
801 if (IS_ERR(dsi->clk_lp)) {
802 dev_err(&pdev->dev, "cannot get low-power clock\n");
803 return PTR_ERR(dsi->clk_lp);
804 }
805
806 err = clk_prepare_enable(dsi->clk_lp);
807 if (err < 0) {
808 dev_err(&pdev->dev, "cannot enable low-power clock\n");
809 return err;
810 }
811
812 dsi->clk_parent = devm_clk_get(&pdev->dev, "parent");
813 if (IS_ERR(dsi->clk_parent)) {
814 dev_err(&pdev->dev, "cannot get parent clock\n");
815 return PTR_ERR(dsi->clk_parent);
816 }
817
818 err = clk_prepare_enable(dsi->clk_parent);
819 if (err < 0) {
820 dev_err(&pdev->dev, "cannot enable parent clock\n");
821 return err;
822 }
823
824 err = tegra_dsi_setup_clocks(dsi);
825 if (err < 0) {
826 dev_err(&pdev->dev, "cannot setup clocks\n");
827 return err;
828 }
829
830 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
831 dsi->regs = devm_ioremap_resource(&pdev->dev, regs);
Wei Yongjun85316ea2013-10-21 11:34:07 +0800832 if (IS_ERR(dsi->regs))
833 return PTR_ERR(dsi->regs);
Thierry Redingdec72732013-09-03 08:45:46 +0200834
Thierry Redingdec72732013-09-03 08:45:46 +0200835 dsi->mipi = tegra_mipi_request(&pdev->dev);
836 if (IS_ERR(dsi->mipi))
837 return PTR_ERR(dsi->mipi);
838
839 dsi->host.ops = &tegra_dsi_host_ops;
840 dsi->host.dev = &pdev->dev;
841
842 err = mipi_dsi_host_register(&dsi->host);
843 if (err < 0) {
844 dev_err(&pdev->dev, "failed to register DSI host: %d\n", err);
845 return err;
846 }
847
848 INIT_LIST_HEAD(&dsi->client.list);
849 dsi->client.ops = &dsi_client_ops;
850 dsi->client.dev = &pdev->dev;
851
852 err = host1x_client_register(&dsi->client);
853 if (err < 0) {
854 dev_err(&pdev->dev, "failed to register host1x client: %d\n",
855 err);
856 return err;
857 }
858
859 platform_set_drvdata(pdev, dsi);
860
861 return 0;
862}
863
864static int tegra_dsi_remove(struct platform_device *pdev)
865{
866 struct tegra_dsi *dsi = platform_get_drvdata(pdev);
867 int err;
868
869 err = host1x_client_unregister(&dsi->client);
870 if (err < 0) {
871 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
872 err);
873 return err;
874 }
875
876 mipi_dsi_host_unregister(&dsi->host);
877 tegra_mipi_free(dsi->mipi);
878
879 clk_disable_unprepare(dsi->clk_parent);
880 clk_disable_unprepare(dsi->clk_lp);
881 clk_disable_unprepare(dsi->clk);
882
883 err = tegra_output_remove(&dsi->output);
884 if (err < 0) {
885 dev_err(&pdev->dev, "failed to remove output: %d\n", err);
886 return err;
887 }
888
889 return 0;
890}
891
892static const struct of_device_id tegra_dsi_of_match[] = {
893 { .compatible = "nvidia,tegra114-dsi", },
894 { },
895};
896
897struct platform_driver tegra_dsi_driver = {
898 .driver = {
899 .name = "tegra-dsi",
900 .of_match_table = tegra_dsi_of_match,
901 },
902 .probe = tegra_dsi_probe,
903 .remove = tegra_dsi_remove,
904};