blob: 46fcf62658cf57ad6aba288d1fb8ea20450c3364 [file] [log] [blame]
Thierry Redingdec72732013-09-03 08:45:46 +02001/*
2 * Copyright (C) 2013 NVIDIA Corporation
3 *
Thierry Reding9a2ac2d2014-02-11 15:52:01 +01004 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
Thierry Redingdec72732013-09-03 08:45:46 +02007 */
8
9#include <linux/clk.h>
10#include <linux/debugfs.h>
11#include <linux/host1x.h>
12#include <linux/module.h>
13#include <linux/of.h>
14#include <linux/platform_device.h>
15#include <linux/reset.h>
16
Thierry Reding3b077af2014-03-14 14:07:50 +010017#include <linux/regulator/consumer.h>
18
Thierry Redingdec72732013-09-03 08:45:46 +020019#include <drm/drm_mipi_dsi.h>
20#include <drm/drm_panel.h>
21
22#include <video/mipi_display.h>
23
24#include "dc.h"
25#include "drm.h"
26#include "dsi.h"
27#include "mipi-phy.h"
28
Thierry Redingdec72732013-09-03 08:45:46 +020029struct tegra_dsi {
30 struct host1x_client client;
31 struct tegra_output output;
32 struct device *dev;
33
34 void __iomem *regs;
35
36 struct reset_control *rst;
37 struct clk *clk_parent;
38 struct clk *clk_lp;
39 struct clk *clk;
40
41 struct drm_info_list *debugfs_files;
42 struct drm_minor *minor;
43 struct dentry *debugfs;
44
Thierry Reding17297a22014-03-14 14:13:15 +010045 unsigned long flags;
Thierry Redingdec72732013-09-03 08:45:46 +020046 enum mipi_dsi_pixel_format format;
47 unsigned int lanes;
48
49 struct tegra_mipi_device *mipi;
50 struct mipi_dsi_host host;
Thierry Reding3b077af2014-03-14 14:07:50 +010051
52 struct regulator *vdd;
Thierry Reding334ae6b2014-03-14 14:15:10 +010053 bool enabled;
Thierry Reding976cebc2014-08-06 09:14:28 +020054
55 unsigned int video_fifo_depth;
56 unsigned int host_fifo_depth;
Thierry Redingdec72732013-09-03 08:45:46 +020057};
58
59static inline struct tegra_dsi *
60host1x_client_to_dsi(struct host1x_client *client)
61{
62 return container_of(client, struct tegra_dsi, client);
63}
64
65static inline struct tegra_dsi *host_to_tegra(struct mipi_dsi_host *host)
66{
67 return container_of(host, struct tegra_dsi, host);
68}
69
70static inline struct tegra_dsi *to_dsi(struct tegra_output *output)
71{
72 return container_of(output, struct tegra_dsi, output);
73}
74
75static inline unsigned long tegra_dsi_readl(struct tegra_dsi *dsi,
76 unsigned long reg)
77{
78 return readl(dsi->regs + (reg << 2));
79}
80
81static inline void tegra_dsi_writel(struct tegra_dsi *dsi, unsigned long value,
82 unsigned long reg)
83{
84 writel(value, dsi->regs + (reg << 2));
85}
86
87static int tegra_dsi_show_regs(struct seq_file *s, void *data)
88{
89 struct drm_info_node *node = s->private;
90 struct tegra_dsi *dsi = node->info_ent->data;
91
92#define DUMP_REG(name) \
93 seq_printf(s, "%-32s %#05x %08lx\n", #name, name, \
94 tegra_dsi_readl(dsi, name))
95
96 DUMP_REG(DSI_INCR_SYNCPT);
97 DUMP_REG(DSI_INCR_SYNCPT_CONTROL);
98 DUMP_REG(DSI_INCR_SYNCPT_ERROR);
99 DUMP_REG(DSI_CTXSW);
100 DUMP_REG(DSI_RD_DATA);
101 DUMP_REG(DSI_WR_DATA);
102 DUMP_REG(DSI_POWER_CONTROL);
103 DUMP_REG(DSI_INT_ENABLE);
104 DUMP_REG(DSI_INT_STATUS);
105 DUMP_REG(DSI_INT_MASK);
106 DUMP_REG(DSI_HOST_CONTROL);
107 DUMP_REG(DSI_CONTROL);
108 DUMP_REG(DSI_SOL_DELAY);
109 DUMP_REG(DSI_MAX_THRESHOLD);
110 DUMP_REG(DSI_TRIGGER);
111 DUMP_REG(DSI_TX_CRC);
112 DUMP_REG(DSI_STATUS);
113
114 DUMP_REG(DSI_INIT_SEQ_CONTROL);
115 DUMP_REG(DSI_INIT_SEQ_DATA_0);
116 DUMP_REG(DSI_INIT_SEQ_DATA_1);
117 DUMP_REG(DSI_INIT_SEQ_DATA_2);
118 DUMP_REG(DSI_INIT_SEQ_DATA_3);
119 DUMP_REG(DSI_INIT_SEQ_DATA_4);
120 DUMP_REG(DSI_INIT_SEQ_DATA_5);
121 DUMP_REG(DSI_INIT_SEQ_DATA_6);
122 DUMP_REG(DSI_INIT_SEQ_DATA_7);
123
124 DUMP_REG(DSI_PKT_SEQ_0_LO);
125 DUMP_REG(DSI_PKT_SEQ_0_HI);
126 DUMP_REG(DSI_PKT_SEQ_1_LO);
127 DUMP_REG(DSI_PKT_SEQ_1_HI);
128 DUMP_REG(DSI_PKT_SEQ_2_LO);
129 DUMP_REG(DSI_PKT_SEQ_2_HI);
130 DUMP_REG(DSI_PKT_SEQ_3_LO);
131 DUMP_REG(DSI_PKT_SEQ_3_HI);
132 DUMP_REG(DSI_PKT_SEQ_4_LO);
133 DUMP_REG(DSI_PKT_SEQ_4_HI);
134 DUMP_REG(DSI_PKT_SEQ_5_LO);
135 DUMP_REG(DSI_PKT_SEQ_5_HI);
136
137 DUMP_REG(DSI_DCS_CMDS);
138
139 DUMP_REG(DSI_PKT_LEN_0_1);
140 DUMP_REG(DSI_PKT_LEN_2_3);
141 DUMP_REG(DSI_PKT_LEN_4_5);
142 DUMP_REG(DSI_PKT_LEN_6_7);
143
144 DUMP_REG(DSI_PHY_TIMING_0);
145 DUMP_REG(DSI_PHY_TIMING_1);
146 DUMP_REG(DSI_PHY_TIMING_2);
147 DUMP_REG(DSI_BTA_TIMING);
148
149 DUMP_REG(DSI_TIMEOUT_0);
150 DUMP_REG(DSI_TIMEOUT_1);
151 DUMP_REG(DSI_TO_TALLY);
152
153 DUMP_REG(DSI_PAD_CONTROL_0);
154 DUMP_REG(DSI_PAD_CONTROL_CD);
155 DUMP_REG(DSI_PAD_CD_STATUS);
156 DUMP_REG(DSI_VIDEO_MODE_CONTROL);
157 DUMP_REG(DSI_PAD_CONTROL_1);
158 DUMP_REG(DSI_PAD_CONTROL_2);
159 DUMP_REG(DSI_PAD_CONTROL_3);
160 DUMP_REG(DSI_PAD_CONTROL_4);
161
162 DUMP_REG(DSI_GANGED_MODE_CONTROL);
163 DUMP_REG(DSI_GANGED_MODE_START);
164 DUMP_REG(DSI_GANGED_MODE_SIZE);
165
166 DUMP_REG(DSI_RAW_DATA_BYTE_COUNT);
167 DUMP_REG(DSI_ULTRA_LOW_POWER_CONTROL);
168
169 DUMP_REG(DSI_INIT_SEQ_DATA_8);
170 DUMP_REG(DSI_INIT_SEQ_DATA_9);
171 DUMP_REG(DSI_INIT_SEQ_DATA_10);
172 DUMP_REG(DSI_INIT_SEQ_DATA_11);
173 DUMP_REG(DSI_INIT_SEQ_DATA_12);
174 DUMP_REG(DSI_INIT_SEQ_DATA_13);
175 DUMP_REG(DSI_INIT_SEQ_DATA_14);
176 DUMP_REG(DSI_INIT_SEQ_DATA_15);
177
178#undef DUMP_REG
179
180 return 0;
181}
182
183static struct drm_info_list debugfs_files[] = {
184 { "regs", tegra_dsi_show_regs, 0, NULL },
185};
186
187static int tegra_dsi_debugfs_init(struct tegra_dsi *dsi,
188 struct drm_minor *minor)
189{
190 const char *name = dev_name(dsi->dev);
191 unsigned int i;
192 int err;
193
194 dsi->debugfs = debugfs_create_dir(name, minor->debugfs_root);
195 if (!dsi->debugfs)
196 return -ENOMEM;
197
198 dsi->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
199 GFP_KERNEL);
200 if (!dsi->debugfs_files) {
201 err = -ENOMEM;
202 goto remove;
203 }
204
205 for (i = 0; i < ARRAY_SIZE(debugfs_files); i++)
206 dsi->debugfs_files[i].data = dsi;
207
208 err = drm_debugfs_create_files(dsi->debugfs_files,
209 ARRAY_SIZE(debugfs_files),
210 dsi->debugfs, minor);
211 if (err < 0)
212 goto free;
213
214 dsi->minor = minor;
215
216 return 0;
217
218free:
219 kfree(dsi->debugfs_files);
220 dsi->debugfs_files = NULL;
221remove:
222 debugfs_remove(dsi->debugfs);
223 dsi->debugfs = NULL;
224
225 return err;
226}
227
228static int tegra_dsi_debugfs_exit(struct tegra_dsi *dsi)
229{
230 drm_debugfs_remove_files(dsi->debugfs_files, ARRAY_SIZE(debugfs_files),
231 dsi->minor);
232 dsi->minor = NULL;
233
234 kfree(dsi->debugfs_files);
235 dsi->debugfs_files = NULL;
236
237 debugfs_remove(dsi->debugfs);
238 dsi->debugfs = NULL;
239
240 return 0;
241}
242
243#define PKT_ID0(id) ((((id) & 0x3f) << 3) | (1 << 9))
244#define PKT_LEN0(len) (((len) & 0x07) << 0)
245#define PKT_ID1(id) ((((id) & 0x3f) << 13) | (1 << 19))
246#define PKT_LEN1(len) (((len) & 0x07) << 10)
247#define PKT_ID2(id) ((((id) & 0x3f) << 23) | (1 << 29))
248#define PKT_LEN2(len) (((len) & 0x07) << 20)
249
250#define PKT_LP (1 << 30)
251#define NUM_PKT_SEQ 12
252
Thierry Reding17297a22014-03-14 14:13:15 +0100253/*
254 * non-burst mode with sync pulses
255 */
256static const u32 pkt_seq_video_non_burst_sync_pulses[NUM_PKT_SEQ] = {
Thierry Redingdec72732013-09-03 08:45:46 +0200257 [ 0] = PKT_ID0(MIPI_DSI_V_SYNC_START) | PKT_LEN0(0) |
258 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
259 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0) |
260 PKT_LP,
261 [ 1] = 0,
262 [ 2] = PKT_ID0(MIPI_DSI_V_SYNC_END) | PKT_LEN0(0) |
263 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
264 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0) |
265 PKT_LP,
266 [ 3] = 0,
267 [ 4] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
268 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
269 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0) |
270 PKT_LP,
271 [ 5] = 0,
272 [ 6] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
273 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
274 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0),
275 [ 7] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(2) |
276 PKT_ID1(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN1(3) |
277 PKT_ID2(MIPI_DSI_BLANKING_PACKET) | PKT_LEN2(4),
278 [ 8] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
279 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
280 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0) |
281 PKT_LP,
282 [ 9] = 0,
283 [10] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
284 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
285 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0),
286 [11] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(2) |
287 PKT_ID1(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN1(3) |
288 PKT_ID2(MIPI_DSI_BLANKING_PACKET) | PKT_LEN2(4),
289};
290
Thierry Reding17297a22014-03-14 14:13:15 +0100291/*
292 * non-burst mode with sync events
293 */
294static const u32 pkt_seq_video_non_burst_sync_events[NUM_PKT_SEQ] = {
295 [ 0] = PKT_ID0(MIPI_DSI_V_SYNC_START) | PKT_LEN0(0) |
296 PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION) | PKT_LEN1(7) |
297 PKT_LP,
298 [ 1] = 0,
299 [ 2] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
300 PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION) | PKT_LEN1(7) |
301 PKT_LP,
302 [ 3] = 0,
303 [ 4] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
304 PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION) | PKT_LEN1(7) |
305 PKT_LP,
306 [ 5] = 0,
307 [ 6] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
308 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(2) |
309 PKT_ID2(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN2(3),
310 [ 7] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(4),
311 [ 8] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
312 PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION) | PKT_LEN1(7) |
313 PKT_LP,
314 [ 9] = 0,
315 [10] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
316 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(2) |
317 PKT_ID2(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN2(3),
318 [11] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(4),
319};
320
Thierry Reding337b4432014-11-13 15:02:46 +0100321static const u32 pkt_seq_command_mode[NUM_PKT_SEQ] = {
322 [ 0] = 0,
323 [ 1] = 0,
324 [ 2] = 0,
325 [ 3] = 0,
326 [ 4] = 0,
327 [ 5] = 0,
328 [ 6] = PKT_ID0(MIPI_DSI_DCS_LONG_WRITE) | PKT_LEN0(3) | PKT_LP,
329 [ 7] = 0,
330 [ 8] = 0,
331 [ 9] = 0,
332 [10] = PKT_ID0(MIPI_DSI_DCS_LONG_WRITE) | PKT_LEN0(5) | PKT_LP,
333 [11] = 0,
334};
335
Thierry Redingdec72732013-09-03 08:45:46 +0200336static int tegra_dsi_set_phy_timing(struct tegra_dsi *dsi)
337{
338 struct mipi_dphy_timing timing;
339 unsigned long value, period;
340 long rate;
341 int err;
342
343 rate = clk_get_rate(dsi->clk);
344 if (rate < 0)
345 return rate;
346
347 period = DIV_ROUND_CLOSEST(1000000000UL, rate * 2);
348
349 err = mipi_dphy_timing_get_default(&timing, period);
350 if (err < 0)
351 return err;
352
353 err = mipi_dphy_timing_validate(&timing, period);
354 if (err < 0) {
355 dev_err(dsi->dev, "failed to validate D-PHY timing: %d\n", err);
356 return err;
357 }
358
359 /*
360 * The D-PHY timing fields below are expressed in byte-clock cycles,
361 * so multiply the period by 8.
362 */
363 period *= 8;
364
365 value = DSI_TIMING_FIELD(timing.hsexit, period, 1) << 24 |
366 DSI_TIMING_FIELD(timing.hstrail, period, 0) << 16 |
367 DSI_TIMING_FIELD(timing.hszero, period, 3) << 8 |
368 DSI_TIMING_FIELD(timing.hsprepare, period, 1);
369 tegra_dsi_writel(dsi, value, DSI_PHY_TIMING_0);
370
371 value = DSI_TIMING_FIELD(timing.clktrail, period, 1) << 24 |
372 DSI_TIMING_FIELD(timing.clkpost, period, 1) << 16 |
373 DSI_TIMING_FIELD(timing.clkzero, period, 1) << 8 |
374 DSI_TIMING_FIELD(timing.lpx, period, 1);
375 tegra_dsi_writel(dsi, value, DSI_PHY_TIMING_1);
376
377 value = DSI_TIMING_FIELD(timing.clkprepare, period, 1) << 16 |
378 DSI_TIMING_FIELD(timing.clkpre, period, 1) << 8 |
379 DSI_TIMING_FIELD(0xff * period, period, 0) << 0;
380 tegra_dsi_writel(dsi, value, DSI_PHY_TIMING_2);
381
382 value = DSI_TIMING_FIELD(timing.taget, period, 1) << 16 |
383 DSI_TIMING_FIELD(timing.tasure, period, 1) << 8 |
384 DSI_TIMING_FIELD(timing.tago, period, 1);
385 tegra_dsi_writel(dsi, value, DSI_BTA_TIMING);
386
387 return 0;
388}
389
390static int tegra_dsi_get_muldiv(enum mipi_dsi_pixel_format format,
391 unsigned int *mulp, unsigned int *divp)
392{
393 switch (format) {
394 case MIPI_DSI_FMT_RGB666_PACKED:
395 case MIPI_DSI_FMT_RGB888:
396 *mulp = 3;
397 *divp = 1;
398 break;
399
400 case MIPI_DSI_FMT_RGB565:
401 *mulp = 2;
402 *divp = 1;
403 break;
404
405 case MIPI_DSI_FMT_RGB666:
406 *mulp = 9;
407 *divp = 4;
408 break;
409
410 default:
411 return -EINVAL;
412 }
413
414 return 0;
415}
416
Thierry Redingf7d68892014-03-13 08:50:39 +0100417static int tegra_dsi_get_format(enum mipi_dsi_pixel_format format,
418 enum tegra_dsi_format *fmt)
419{
420 switch (format) {
421 case MIPI_DSI_FMT_RGB888:
422 *fmt = TEGRA_DSI_FORMAT_24P;
423 break;
424
425 case MIPI_DSI_FMT_RGB666:
426 *fmt = TEGRA_DSI_FORMAT_18NP;
427 break;
428
429 case MIPI_DSI_FMT_RGB666_PACKED:
430 *fmt = TEGRA_DSI_FORMAT_18P;
431 break;
432
433 case MIPI_DSI_FMT_RGB565:
434 *fmt = TEGRA_DSI_FORMAT_16P;
435 break;
436
437 default:
438 return -EINVAL;
439 }
440
441 return 0;
442}
443
Thierry Reding563eff12014-11-13 14:44:27 +0100444static void tegra_dsi_enable(struct tegra_dsi *dsi)
Thierry Redingdec72732013-09-03 08:45:46 +0200445{
Thierry Reding563eff12014-11-13 14:44:27 +0100446 u32 value;
Thierry Redingdec72732013-09-03 08:45:46 +0200447
Thierry Reding563eff12014-11-13 14:44:27 +0100448 value = tegra_dsi_readl(dsi, DSI_POWER_CONTROL);
449 value |= DSI_POWER_CONTROL_ENABLE;
450 tegra_dsi_writel(dsi, value, DSI_POWER_CONTROL);
451}
452
453static int tegra_dsi_configure(struct tegra_dsi *dsi, unsigned int pipe,
454 const struct drm_display_mode *mode)
455{
456 unsigned int hact, hsw, hbp, hfp, i, mul, div;
457 enum tegra_dsi_format format;
458 const u32 *pkt_seq;
459 u32 value;
460 int err;
Thierry Reding334ae6b2014-03-14 14:15:10 +0100461
Thierry Reding17297a22014-03-14 14:13:15 +0100462 if (dsi->flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE) {
463 DRM_DEBUG_KMS("Non-burst video mode with sync pulses\n");
464 pkt_seq = pkt_seq_video_non_burst_sync_pulses;
Thierry Reding337b4432014-11-13 15:02:46 +0100465 } else if (dsi->flags & MIPI_DSI_MODE_VIDEO) {
Thierry Reding17297a22014-03-14 14:13:15 +0100466 DRM_DEBUG_KMS("Non-burst video mode with sync events\n");
467 pkt_seq = pkt_seq_video_non_burst_sync_events;
Thierry Reding337b4432014-11-13 15:02:46 +0100468 } else {
469 DRM_DEBUG_KMS("Command mode\n");
470 pkt_seq = pkt_seq_command_mode;
Thierry Reding17297a22014-03-14 14:13:15 +0100471 }
472
Thierry Redingdec72732013-09-03 08:45:46 +0200473 err = tegra_dsi_get_muldiv(dsi->format, &mul, &div);
474 if (err < 0)
475 return err;
476
Thierry Redingf7d68892014-03-13 08:50:39 +0100477 err = tegra_dsi_get_format(dsi->format, &format);
478 if (err < 0)
479 return err;
480
Thierry Redingf7d68892014-03-13 08:50:39 +0100481 value = DSI_CONTROL_CHANNEL(0) | DSI_CONTROL_FORMAT(format) |
Thierry Redingdec72732013-09-03 08:45:46 +0200482 DSI_CONTROL_LANES(dsi->lanes - 1) |
Thierry Reding563eff12014-11-13 14:44:27 +0100483 DSI_CONTROL_SOURCE(pipe);
Thierry Redingdec72732013-09-03 08:45:46 +0200484 tegra_dsi_writel(dsi, value, DSI_CONTROL);
485
Thierry Reding976cebc2014-08-06 09:14:28 +0200486 tegra_dsi_writel(dsi, dsi->video_fifo_depth, DSI_MAX_THRESHOLD);
Thierry Redingdec72732013-09-03 08:45:46 +0200487
Thierry Reding563eff12014-11-13 14:44:27 +0100488 value = DSI_HOST_CONTROL_HS;
Thierry Redingdec72732013-09-03 08:45:46 +0200489 tegra_dsi_writel(dsi, value, DSI_HOST_CONTROL);
490
491 value = tegra_dsi_readl(dsi, DSI_CONTROL);
Thierry Reding563eff12014-11-13 14:44:27 +0100492
Alexandre Courbot0c6b1e42014-07-08 21:32:13 +0900493 if (dsi->flags & MIPI_DSI_CLOCK_NON_CONTINUOUS)
494 value |= DSI_CONTROL_HS_CLK_CTRL;
Thierry Reding563eff12014-11-13 14:44:27 +0100495
Thierry Redingdec72732013-09-03 08:45:46 +0200496 value &= ~DSI_CONTROL_TX_TRIG(3);
Thierry Reding337b4432014-11-13 15:02:46 +0100497
498 /* enable DCS commands for command mode */
499 if (dsi->flags & MIPI_DSI_MODE_VIDEO)
500 value &= ~DSI_CONTROL_DCS_ENABLE;
501 else
502 value |= DSI_CONTROL_DCS_ENABLE;
503
Thierry Redingdec72732013-09-03 08:45:46 +0200504 value |= DSI_CONTROL_VIDEO_ENABLE;
505 value &= ~DSI_CONTROL_HOST_ENABLE;
506 tegra_dsi_writel(dsi, value, DSI_CONTROL);
507
508 err = tegra_dsi_set_phy_timing(dsi);
509 if (err < 0)
510 return err;
511
512 for (i = 0; i < NUM_PKT_SEQ; i++)
513 tegra_dsi_writel(dsi, pkt_seq[i], DSI_PKT_SEQ_0_LO + i);
514
Thierry Reding337b4432014-11-13 15:02:46 +0100515 if (dsi->flags & MIPI_DSI_MODE_VIDEO) {
516 /* horizontal active pixels */
517 hact = mode->hdisplay * mul / div;
Thierry Redingdec72732013-09-03 08:45:46 +0200518
Thierry Reding337b4432014-11-13 15:02:46 +0100519 /* horizontal sync width */
520 hsw = (mode->hsync_end - mode->hsync_start) * mul / div;
521 hsw -= 10;
Thierry Redingdec72732013-09-03 08:45:46 +0200522
Thierry Reding337b4432014-11-13 15:02:46 +0100523 /* horizontal back porch */
524 hbp = (mode->htotal - mode->hsync_end) * mul / div;
525 hbp -= 14;
Thierry Redingdec72732013-09-03 08:45:46 +0200526
Thierry Reding337b4432014-11-13 15:02:46 +0100527 /* horizontal front porch */
528 hfp = (mode->hsync_start - mode->hdisplay) * mul / div;
529 hfp -= 8;
Thierry Redingdec72732013-09-03 08:45:46 +0200530
Thierry Reding337b4432014-11-13 15:02:46 +0100531 tegra_dsi_writel(dsi, hsw << 16 | 0, DSI_PKT_LEN_0_1);
532 tegra_dsi_writel(dsi, hact << 16 | hbp, DSI_PKT_LEN_2_3);
533 tegra_dsi_writel(dsi, hfp, DSI_PKT_LEN_4_5);
534 tegra_dsi_writel(dsi, 0x0f0f << 16, DSI_PKT_LEN_6_7);
Thierry Redingdec72732013-09-03 08:45:46 +0200535
Thierry Reding337b4432014-11-13 15:02:46 +0100536 /* set SOL delay (for non-burst mode only) */
537 tegra_dsi_writel(dsi, 8 * mul / div, DSI_SOL_DELAY);
538 } else {
539 u16 bytes;
540
541 /* 1 byte (DCS command) + pixel data */
542 bytes = 1 + mode->hdisplay * mul / div;
543
544 tegra_dsi_writel(dsi, 0, DSI_PKT_LEN_0_1);
545 tegra_dsi_writel(dsi, bytes << 16, DSI_PKT_LEN_2_3);
546 tegra_dsi_writel(dsi, bytes << 16, DSI_PKT_LEN_4_5);
547 tegra_dsi_writel(dsi, 0, DSI_PKT_LEN_6_7);
548
549 value = MIPI_DCS_WRITE_MEMORY_START << 8 |
550 MIPI_DCS_WRITE_MEMORY_CONTINUE;
551 tegra_dsi_writel(dsi, value, DSI_DCS_CMDS);
552
553 value = 8 * mul / div;
554
555 tegra_dsi_writel(dsi, value, DSI_SOL_DELAY);
556 }
Thierry Redingdec72732013-09-03 08:45:46 +0200557
Thierry Reding563eff12014-11-13 14:44:27 +0100558 return 0;
559}
560
561static int tegra_output_dsi_enable(struct tegra_output *output)
562{
563 struct tegra_dc *dc = to_tegra_dc(output->encoder.crtc);
564 const struct drm_display_mode *mode = &dc->base.mode;
565 struct tegra_dsi *dsi = to_dsi(output);
566 u32 value;
567 int err;
568
569 if (dsi->enabled)
570 return 0;
571
572 err = tegra_dsi_configure(dsi, dc->pipe, mode);
573 if (err < 0)
574 return err;
575
Thierry Redingdec72732013-09-03 08:45:46 +0200576 /* enable display controller */
577 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
578 value |= DSI_ENABLE;
579 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
580
Thierry Redingdec72732013-09-03 08:45:46 +0200581 value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
582 value &= ~DISP_CTRL_MODE_MASK;
583 value |= DISP_CTRL_MODE_C_DISPLAY;
584 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
585
Thierry Reding72d30282013-12-12 11:06:55 +0100586 value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
587 value |= PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
588 PW4_ENABLE | PM0_ENABLE | PM1_ENABLE;
589 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
590
Thierry Redingdec72732013-09-03 08:45:46 +0200591 tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
592 tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
593
594 /* enable DSI controller */
Thierry Reding563eff12014-11-13 14:44:27 +0100595 tegra_dsi_enable(dsi);
Thierry Redingdec72732013-09-03 08:45:46 +0200596
Thierry Reding334ae6b2014-03-14 14:15:10 +0100597 dsi->enabled = true;
598
Thierry Redingdec72732013-09-03 08:45:46 +0200599 return 0;
600}
601
Thierry Reding563eff12014-11-13 14:44:27 +0100602static int tegra_dsi_wait_idle(struct tegra_dsi *dsi, unsigned long timeout)
603{
604 u32 value;
605
606 timeout = jiffies + msecs_to_jiffies(timeout);
607
608 while (time_before(jiffies, timeout)) {
609 value = tegra_dsi_readl(dsi, DSI_STATUS);
610 if (value & DSI_STATUS_IDLE)
611 return 0;
612
613 usleep_range(1000, 2000);
614 }
615
616 return -ETIMEDOUT;
617}
618
619static void tegra_dsi_video_disable(struct tegra_dsi *dsi)
620{
621 u32 value;
622
623 value = tegra_dsi_readl(dsi, DSI_CONTROL);
624 value &= ~DSI_CONTROL_VIDEO_ENABLE;
625 tegra_dsi_writel(dsi, value, DSI_CONTROL);
626}
627
628static void tegra_dsi_disable(struct tegra_dsi *dsi)
629{
630 u32 value;
631
632 value = tegra_dsi_readl(dsi, DSI_POWER_CONTROL);
633 value &= ~DSI_POWER_CONTROL_ENABLE;
634 tegra_dsi_writel(dsi, value, DSI_POWER_CONTROL);
635
636 usleep_range(5000, 10000);
637}
638
Thierry Redingdec72732013-09-03 08:45:46 +0200639static int tegra_output_dsi_disable(struct tegra_output *output)
640{
641 struct tegra_dc *dc = to_tegra_dc(output->encoder.crtc);
642 struct tegra_dsi *dsi = to_dsi(output);
643 unsigned long value;
Thierry Reding563eff12014-11-13 14:44:27 +0100644 int err;
Thierry Redingdec72732013-09-03 08:45:46 +0200645
Thierry Reding334ae6b2014-03-14 14:15:10 +0100646 if (!dsi->enabled)
647 return 0;
648
Thierry Reding563eff12014-11-13 14:44:27 +0100649 tegra_dsi_video_disable(dsi);
Thierry Redingdec72732013-09-03 08:45:46 +0200650
651 /*
Thierry Reding72d30282013-12-12 11:06:55 +0100652 * The following accesses registers of the display controller, so make
653 * sure it's only executed when the output is attached to one.
Thierry Redingdec72732013-09-03 08:45:46 +0200654 */
655 if (dc) {
Thierry Reding72d30282013-12-12 11:06:55 +0100656 value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
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);
660
Thierry Redingdec72732013-09-03 08:45:46 +0200661 value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
662 value &= ~DISP_CTRL_MODE_MASK;
663 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
664
665 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
666 value &= ~DSI_ENABLE;
667 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
Thierry Reding72d30282013-12-12 11:06:55 +0100668
669 tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
670 tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
Thierry Redingdec72732013-09-03 08:45:46 +0200671 }
672
Thierry Reding563eff12014-11-13 14:44:27 +0100673 err = tegra_dsi_wait_idle(dsi, 100);
674 if (err < 0)
675 dev_dbg(dsi->dev, "failed to idle DSI: %d\n", err);
676
677 tegra_dsi_disable(dsi);
678
Thierry Reding334ae6b2014-03-14 14:15:10 +0100679 dsi->enabled = false;
680
Thierry Redingdec72732013-09-03 08:45:46 +0200681 return 0;
682}
683
Thierry Reding3f6b4062014-11-13 14:50:33 +0100684static void tegra_dsi_set_timeout(struct tegra_dsi *dsi, unsigned long bclk,
685 unsigned int vrefresh)
686{
687 unsigned int timeout;
688 u32 value;
689
690 /* one frame high-speed transmission timeout */
691 timeout = (bclk / vrefresh) / 512;
692 value = DSI_TIMEOUT_LRX(0x2000) | DSI_TIMEOUT_HTX(timeout);
693 tegra_dsi_writel(dsi, value, DSI_TIMEOUT_0);
694
695 /* 2 ms peripheral timeout for panel */
696 timeout = 2 * bclk / 512 * 1000;
697 value = DSI_TIMEOUT_PR(timeout) | DSI_TIMEOUT_TA(0x2000);
698 tegra_dsi_writel(dsi, value, DSI_TIMEOUT_1);
699
700 value = DSI_TALLY_TA(0) | DSI_TALLY_LRX(0) | DSI_TALLY_HTX(0);
701 tegra_dsi_writel(dsi, value, DSI_TO_TALLY);
702}
703
Thierry Redingdec72732013-09-03 08:45:46 +0200704static int tegra_output_dsi_setup_clock(struct tegra_output *output,
Thierry Reding91eded92014-03-26 13:32:21 +0100705 struct clk *clk, unsigned long pclk,
706 unsigned int *divp)
Thierry Redingdec72732013-09-03 08:45:46 +0200707{
708 struct tegra_dc *dc = to_tegra_dc(output->encoder.crtc);
709 struct drm_display_mode *mode = &dc->base.mode;
Thierry Redingdec72732013-09-03 08:45:46 +0200710 struct tegra_dsi *dsi = to_dsi(output);
Thierry Reding3f6b4062014-11-13 14:50:33 +0100711 unsigned int mul, div, vrefresh;
712 unsigned long bclk, plld;
Thierry Redingdec72732013-09-03 08:45:46 +0200713 int err;
714
715 err = tegra_dsi_get_muldiv(dsi->format, &mul, &div);
716 if (err < 0)
717 return err;
718
Thierry Reding91eded92014-03-26 13:32:21 +0100719 DRM_DEBUG_KMS("mul: %u, div: %u, lanes: %u\n", mul, div, dsi->lanes);
Thierry Redingdec72732013-09-03 08:45:46 +0200720 vrefresh = drm_mode_vrefresh(mode);
Thierry Reding91eded92014-03-26 13:32:21 +0100721 DRM_DEBUG_KMS("vrefresh: %u\n", vrefresh);
Thierry Redingdec72732013-09-03 08:45:46 +0200722
Thierry Reding91eded92014-03-26 13:32:21 +0100723 /* compute byte clock */
Thierry Redingdec72732013-09-03 08:45:46 +0200724 bclk = (pclk * mul) / (div * dsi->lanes);
Thierry Reding91eded92014-03-26 13:32:21 +0100725
726 /*
727 * Compute bit clock and round up to the next MHz.
728 */
729 plld = DIV_ROUND_UP(bclk * 8, 1000000) * 1000000;
730
731 /*
732 * We divide the frequency by two here, but we make up for that by
733 * setting the shift clock divider (further below) to half of the
734 * correct value.
735 */
736 plld /= 2;
Thierry Redingdec72732013-09-03 08:45:46 +0200737
738 err = clk_set_parent(clk, dsi->clk_parent);
739 if (err < 0) {
740 dev_err(dsi->dev, "failed to set parent clock: %d\n", err);
741 return err;
742 }
743
Thierry Reding91eded92014-03-26 13:32:21 +0100744 err = clk_set_rate(dsi->clk_parent, plld);
Thierry Redingdec72732013-09-03 08:45:46 +0200745 if (err < 0) {
746 dev_err(dsi->dev, "failed to set base clock rate to %lu Hz\n",
Thierry Reding91eded92014-03-26 13:32:21 +0100747 plld);
Thierry Redingdec72732013-09-03 08:45:46 +0200748 return err;
749 }
750
751 /*
Thierry Reding91eded92014-03-26 13:32:21 +0100752 * Derive pixel clock from bit clock using the shift clock divider.
753 * Note that this is only half of what we would expect, but we need
754 * that to make up for the fact that we divided the bit clock by a
755 * factor of two above.
756 *
757 * It's not clear exactly why this is necessary, but the display is
758 * not working properly otherwise. Perhaps the PLLs cannot generate
759 * frequencies sufficiently high.
760 */
761 *divp = ((8 * mul) / (div * dsi->lanes)) - 2;
762
763 /*
Thierry Redingdec72732013-09-03 08:45:46 +0200764 * XXX: Move the below somewhere else so that we don't need to have
765 * access to the vrefresh in this function?
766 */
Thierry Reding3f6b4062014-11-13 14:50:33 +0100767 tegra_dsi_set_timeout(dsi, bclk, vrefresh);
Thierry Redingdec72732013-09-03 08:45:46 +0200768
769 return 0;
770}
771
772static int tegra_output_dsi_check_mode(struct tegra_output *output,
773 struct drm_display_mode *mode,
774 enum drm_mode_status *status)
775{
776 /*
777 * FIXME: For now, always assume that the mode is okay.
778 */
779
780 *status = MODE_OK;
781
782 return 0;
783}
784
785static const struct tegra_output_ops dsi_ops = {
786 .enable = tegra_output_dsi_enable,
787 .disable = tegra_output_dsi_disable,
788 .setup_clock = tegra_output_dsi_setup_clock,
789 .check_mode = tegra_output_dsi_check_mode,
790};
791
792static int tegra_dsi_pad_enable(struct tegra_dsi *dsi)
793{
794 unsigned long value;
795
796 value = DSI_PAD_CONTROL_VS1_PULLDN(0) | DSI_PAD_CONTROL_VS1_PDIO(0);
797 tegra_dsi_writel(dsi, value, DSI_PAD_CONTROL_0);
798
799 return 0;
800}
801
802static int tegra_dsi_pad_calibrate(struct tegra_dsi *dsi)
803{
Thierry Reding183ef282014-11-13 14:27:29 +0100804 u32 value;
Thierry Redingdec72732013-09-03 08:45:46 +0200805
806 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_0);
807 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_1);
808 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_2);
809 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_3);
810 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_4);
811
812 /* start calibration */
813 tegra_dsi_pad_enable(dsi);
814
815 value = DSI_PAD_SLEW_UP(0x7) | DSI_PAD_SLEW_DN(0x7) |
816 DSI_PAD_LP_UP(0x1) | DSI_PAD_LP_DN(0x1) |
817 DSI_PAD_OUT_CLK(0x0);
818 tegra_dsi_writel(dsi, value, DSI_PAD_CONTROL_2);
819
820 return tegra_mipi_calibrate(dsi->mipi);
821}
822
823static int tegra_dsi_init(struct host1x_client *client)
824{
Thierry Reding9910f5c2014-05-22 09:57:15 +0200825 struct drm_device *drm = dev_get_drvdata(client->parent);
Thierry Redingdec72732013-09-03 08:45:46 +0200826 struct tegra_dsi *dsi = host1x_client_to_dsi(client);
Thierry Redingdec72732013-09-03 08:45:46 +0200827 int err;
828
829 dsi->output.type = TEGRA_OUTPUT_DSI;
830 dsi->output.dev = client->dev;
831 dsi->output.ops = &dsi_ops;
832
Thierry Reding9910f5c2014-05-22 09:57:15 +0200833 err = tegra_output_init(drm, &dsi->output);
Thierry Redingdec72732013-09-03 08:45:46 +0200834 if (err < 0) {
835 dev_err(client->dev, "output setup failed: %d\n", err);
836 return err;
837 }
838
839 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
Thierry Reding9910f5c2014-05-22 09:57:15 +0200840 err = tegra_dsi_debugfs_init(dsi, drm->primary);
Thierry Redingdec72732013-09-03 08:45:46 +0200841 if (err < 0)
842 dev_err(dsi->dev, "debugfs setup failed: %d\n", err);
843 }
844
Thierry Redingdec72732013-09-03 08:45:46 +0200845 return 0;
846}
847
848static int tegra_dsi_exit(struct host1x_client *client)
849{
850 struct tegra_dsi *dsi = host1x_client_to_dsi(client);
851 int err;
852
853 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
854 err = tegra_dsi_debugfs_exit(dsi);
855 if (err < 0)
856 dev_err(dsi->dev, "debugfs cleanup failed: %d\n", err);
857 }
858
859 err = tegra_output_disable(&dsi->output);
860 if (err < 0) {
861 dev_err(client->dev, "output failed to disable: %d\n", err);
862 return err;
863 }
864
865 err = tegra_output_exit(&dsi->output);
866 if (err < 0) {
867 dev_err(client->dev, "output cleanup failed: %d\n", err);
868 return err;
869 }
870
871 return 0;
872}
873
874static const struct host1x_client_ops dsi_client_ops = {
875 .init = tegra_dsi_init,
876 .exit = tegra_dsi_exit,
877};
878
879static int tegra_dsi_setup_clocks(struct tegra_dsi *dsi)
880{
881 struct clk *parent;
882 int err;
883
884 parent = clk_get_parent(dsi->clk);
885 if (!parent)
886 return -EINVAL;
887
888 err = clk_set_parent(parent, dsi->clk_parent);
889 if (err < 0)
890 return err;
891
892 return 0;
893}
894
Thierry Redingdec72732013-09-03 08:45:46 +0200895static int tegra_dsi_host_attach(struct mipi_dsi_host *host,
896 struct mipi_dsi_device *device)
897{
898 struct tegra_dsi *dsi = host_to_tegra(host);
899 struct tegra_output *output = &dsi->output;
900
Thierry Reding17297a22014-03-14 14:13:15 +0100901 dsi->flags = device->mode_flags;
Thierry Redingdec72732013-09-03 08:45:46 +0200902 dsi->format = device->format;
903 dsi->lanes = device->lanes;
904
905 output->panel = of_drm_find_panel(device->dev.of_node);
906 if (output->panel) {
907 if (output->connector.dev)
908 drm_helper_hpd_irq_event(output->connector.dev);
909 }
910
911 return 0;
912}
913
914static int tegra_dsi_host_detach(struct mipi_dsi_host *host,
915 struct mipi_dsi_device *device)
916{
917 struct tegra_dsi *dsi = host_to_tegra(host);
918 struct tegra_output *output = &dsi->output;
919
920 if (output->panel && &device->dev == output->panel->dev) {
Thierry Redingba3df972014-11-13 14:54:01 +0100921 output->panel = NULL;
922
Thierry Redingdec72732013-09-03 08:45:46 +0200923 if (output->connector.dev)
924 drm_helper_hpd_irq_event(output->connector.dev);
Thierry Redingdec72732013-09-03 08:45:46 +0200925 }
926
927 return 0;
928}
929
930static const struct mipi_dsi_host_ops tegra_dsi_host_ops = {
931 .attach = tegra_dsi_host_attach,
932 .detach = tegra_dsi_host_detach,
933};
934
935static int tegra_dsi_probe(struct platform_device *pdev)
936{
937 struct tegra_dsi *dsi;
938 struct resource *regs;
939 int err;
940
941 dsi = devm_kzalloc(&pdev->dev, sizeof(*dsi), GFP_KERNEL);
942 if (!dsi)
943 return -ENOMEM;
944
945 dsi->output.dev = dsi->dev = &pdev->dev;
Thierry Reding976cebc2014-08-06 09:14:28 +0200946 dsi->video_fifo_depth = 1920;
947 dsi->host_fifo_depth = 64;
Thierry Redingdec72732013-09-03 08:45:46 +0200948
949 err = tegra_output_probe(&dsi->output);
950 if (err < 0)
951 return err;
952
Thierry Redingba3df972014-11-13 14:54:01 +0100953 dsi->output.connector.polled = DRM_CONNECTOR_POLL_HPD;
954
Thierry Redingdec72732013-09-03 08:45:46 +0200955 /*
956 * Assume these values by default. When a DSI peripheral driver
957 * attaches to the DSI host, the parameters will be taken from
958 * the attached device.
959 */
Thierry Reding17297a22014-03-14 14:13:15 +0100960 dsi->flags = MIPI_DSI_MODE_VIDEO;
Thierry Redingdec72732013-09-03 08:45:46 +0200961 dsi->format = MIPI_DSI_FMT_RGB888;
962 dsi->lanes = 4;
963
964 dsi->rst = devm_reset_control_get(&pdev->dev, "dsi");
965 if (IS_ERR(dsi->rst))
966 return PTR_ERR(dsi->rst);
967
Thierry Reding183ef282014-11-13 14:27:29 +0100968 err = reset_control_deassert(dsi->rst);
969 if (err < 0) {
970 dev_err(&pdev->dev, "failed to bring DSI out of reset: %d\n",
971 err);
972 return err;
973 }
974
Thierry Redingdec72732013-09-03 08:45:46 +0200975 dsi->clk = devm_clk_get(&pdev->dev, NULL);
976 if (IS_ERR(dsi->clk)) {
977 dev_err(&pdev->dev, "cannot get DSI clock\n");
Thierry Redingd2d0a9d2014-11-13 14:58:27 +0100978 err = PTR_ERR(dsi->clk);
979 goto reset;
Thierry Redingdec72732013-09-03 08:45:46 +0200980 }
981
982 err = clk_prepare_enable(dsi->clk);
983 if (err < 0) {
984 dev_err(&pdev->dev, "cannot enable DSI clock\n");
Thierry Redingd2d0a9d2014-11-13 14:58:27 +0100985 goto reset;
Thierry Redingdec72732013-09-03 08:45:46 +0200986 }
987
988 dsi->clk_lp = devm_clk_get(&pdev->dev, "lp");
989 if (IS_ERR(dsi->clk_lp)) {
990 dev_err(&pdev->dev, "cannot get low-power clock\n");
Thierry Redingd2d0a9d2014-11-13 14:58:27 +0100991 err = PTR_ERR(dsi->clk_lp);
992 goto disable_clk;
Thierry Redingdec72732013-09-03 08:45:46 +0200993 }
994
995 err = clk_prepare_enable(dsi->clk_lp);
996 if (err < 0) {
997 dev_err(&pdev->dev, "cannot enable low-power clock\n");
Thierry Redingd2d0a9d2014-11-13 14:58:27 +0100998 goto disable_clk;
Thierry Redingdec72732013-09-03 08:45:46 +0200999 }
1000
1001 dsi->clk_parent = devm_clk_get(&pdev->dev, "parent");
1002 if (IS_ERR(dsi->clk_parent)) {
1003 dev_err(&pdev->dev, "cannot get parent clock\n");
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001004 err = PTR_ERR(dsi->clk_parent);
1005 goto disable_clk_lp;
Thierry Redingdec72732013-09-03 08:45:46 +02001006 }
1007
Thierry Reding3b077af2014-03-14 14:07:50 +01001008 dsi->vdd = devm_regulator_get(&pdev->dev, "avdd-dsi-csi");
1009 if (IS_ERR(dsi->vdd)) {
1010 dev_err(&pdev->dev, "cannot get VDD supply\n");
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001011 err = PTR_ERR(dsi->vdd);
1012 goto disable_clk_lp;
Thierry Reding3b077af2014-03-14 14:07:50 +01001013 }
1014
1015 err = regulator_enable(dsi->vdd);
1016 if (err < 0) {
1017 dev_err(&pdev->dev, "cannot enable VDD supply\n");
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001018 goto disable_clk_lp;
Thierry Reding3b077af2014-03-14 14:07:50 +01001019 }
1020
Thierry Redingdec72732013-09-03 08:45:46 +02001021 err = tegra_dsi_setup_clocks(dsi);
1022 if (err < 0) {
1023 dev_err(&pdev->dev, "cannot setup clocks\n");
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001024 goto disable_vdd;
Thierry Redingdec72732013-09-03 08:45:46 +02001025 }
1026
1027 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1028 dsi->regs = devm_ioremap_resource(&pdev->dev, regs);
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001029 if (IS_ERR(dsi->regs)) {
1030 err = PTR_ERR(dsi->regs);
1031 goto disable_vdd;
1032 }
Thierry Redingdec72732013-09-03 08:45:46 +02001033
Thierry Redingdec72732013-09-03 08:45:46 +02001034 dsi->mipi = tegra_mipi_request(&pdev->dev);
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001035 if (IS_ERR(dsi->mipi)) {
1036 err = PTR_ERR(dsi->mipi);
1037 goto disable_vdd;
1038 }
Thierry Redingdec72732013-09-03 08:45:46 +02001039
Thierry Reding183ef282014-11-13 14:27:29 +01001040 err = tegra_dsi_pad_calibrate(dsi);
1041 if (err < 0) {
1042 dev_err(dsi->dev, "MIPI calibration failed: %d\n", err);
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001043 goto mipi_free;
Thierry Reding183ef282014-11-13 14:27:29 +01001044 }
1045
Thierry Redingdec72732013-09-03 08:45:46 +02001046 dsi->host.ops = &tegra_dsi_host_ops;
1047 dsi->host.dev = &pdev->dev;
1048
1049 err = mipi_dsi_host_register(&dsi->host);
1050 if (err < 0) {
1051 dev_err(&pdev->dev, "failed to register DSI host: %d\n", err);
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001052 goto mipi_free;
Thierry Redingdec72732013-09-03 08:45:46 +02001053 }
1054
1055 INIT_LIST_HEAD(&dsi->client.list);
1056 dsi->client.ops = &dsi_client_ops;
1057 dsi->client.dev = &pdev->dev;
1058
1059 err = host1x_client_register(&dsi->client);
1060 if (err < 0) {
1061 dev_err(&pdev->dev, "failed to register host1x client: %d\n",
1062 err);
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001063 goto unregister;
Thierry Redingdec72732013-09-03 08:45:46 +02001064 }
1065
1066 platform_set_drvdata(pdev, dsi);
1067
1068 return 0;
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001069
1070unregister:
1071 mipi_dsi_host_unregister(&dsi->host);
1072mipi_free:
1073 tegra_mipi_free(dsi->mipi);
1074disable_vdd:
1075 regulator_disable(dsi->vdd);
1076disable_clk_lp:
1077 clk_disable_unprepare(dsi->clk_lp);
1078disable_clk:
1079 clk_disable_unprepare(dsi->clk);
1080reset:
1081 reset_control_assert(dsi->rst);
1082 return err;
Thierry Redingdec72732013-09-03 08:45:46 +02001083}
1084
1085static int tegra_dsi_remove(struct platform_device *pdev)
1086{
1087 struct tegra_dsi *dsi = platform_get_drvdata(pdev);
1088 int err;
1089
1090 err = host1x_client_unregister(&dsi->client);
1091 if (err < 0) {
1092 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
1093 err);
1094 return err;
1095 }
1096
1097 mipi_dsi_host_unregister(&dsi->host);
1098 tegra_mipi_free(dsi->mipi);
1099
Thierry Reding3b077af2014-03-14 14:07:50 +01001100 regulator_disable(dsi->vdd);
Thierry Redingdec72732013-09-03 08:45:46 +02001101 clk_disable_unprepare(dsi->clk_lp);
1102 clk_disable_unprepare(dsi->clk);
Thierry Redingcb825d82014-03-14 14:25:43 +01001103 reset_control_assert(dsi->rst);
Thierry Redingdec72732013-09-03 08:45:46 +02001104
1105 err = tegra_output_remove(&dsi->output);
1106 if (err < 0) {
1107 dev_err(&pdev->dev, "failed to remove output: %d\n", err);
1108 return err;
1109 }
1110
1111 return 0;
1112}
1113
1114static const struct of_device_id tegra_dsi_of_match[] = {
1115 { .compatible = "nvidia,tegra114-dsi", },
1116 { },
1117};
Stephen Warrenef707282014-06-18 16:21:55 -06001118MODULE_DEVICE_TABLE(of, tegra_dsi_of_match);
Thierry Redingdec72732013-09-03 08:45:46 +02001119
1120struct platform_driver tegra_dsi_driver = {
1121 .driver = {
1122 .name = "tegra-dsi",
1123 .of_match_table = tegra_dsi_of_match,
1124 },
1125 .probe = tegra_dsi_probe,
1126 .remove = tegra_dsi_remove,
1127};