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