blob: 50684a4aa4f0ecc2490ad33c020b467901991b6c [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
684static int tegra_output_dsi_setup_clock(struct tegra_output *output,
Thierry Reding91eded92014-03-26 13:32:21 +0100685 struct clk *clk, unsigned long pclk,
686 unsigned int *divp)
Thierry Redingdec72732013-09-03 08:45:46 +0200687{
688 struct tegra_dc *dc = to_tegra_dc(output->encoder.crtc);
689 struct drm_display_mode *mode = &dc->base.mode;
690 unsigned int timeout, mul, div, vrefresh;
691 struct tegra_dsi *dsi = to_dsi(output);
692 unsigned long bclk, plld, value;
Thierry Redingdec72732013-09-03 08:45:46 +0200693 int err;
694
695 err = tegra_dsi_get_muldiv(dsi->format, &mul, &div);
696 if (err < 0)
697 return err;
698
Thierry Reding91eded92014-03-26 13:32:21 +0100699 DRM_DEBUG_KMS("mul: %u, div: %u, lanes: %u\n", mul, div, dsi->lanes);
Thierry Redingdec72732013-09-03 08:45:46 +0200700 vrefresh = drm_mode_vrefresh(mode);
Thierry Reding91eded92014-03-26 13:32:21 +0100701 DRM_DEBUG_KMS("vrefresh: %u\n", vrefresh);
Thierry Redingdec72732013-09-03 08:45:46 +0200702
Thierry Reding91eded92014-03-26 13:32:21 +0100703 /* compute byte clock */
Thierry Redingdec72732013-09-03 08:45:46 +0200704 bclk = (pclk * mul) / (div * dsi->lanes);
Thierry Reding91eded92014-03-26 13:32:21 +0100705
706 /*
707 * Compute bit clock and round up to the next MHz.
708 */
709 plld = DIV_ROUND_UP(bclk * 8, 1000000) * 1000000;
710
711 /*
712 * We divide the frequency by two here, but we make up for that by
713 * setting the shift clock divider (further below) to half of the
714 * correct value.
715 */
716 plld /= 2;
Thierry Redingdec72732013-09-03 08:45:46 +0200717
718 err = clk_set_parent(clk, dsi->clk_parent);
719 if (err < 0) {
720 dev_err(dsi->dev, "failed to set parent clock: %d\n", err);
721 return err;
722 }
723
Thierry Reding91eded92014-03-26 13:32:21 +0100724 err = clk_set_rate(dsi->clk_parent, plld);
Thierry Redingdec72732013-09-03 08:45:46 +0200725 if (err < 0) {
726 dev_err(dsi->dev, "failed to set base clock rate to %lu Hz\n",
Thierry Reding91eded92014-03-26 13:32:21 +0100727 plld);
Thierry Redingdec72732013-09-03 08:45:46 +0200728 return err;
729 }
730
731 /*
Thierry Reding91eded92014-03-26 13:32:21 +0100732 * Derive pixel clock from bit clock using the shift clock divider.
733 * Note that this is only half of what we would expect, but we need
734 * that to make up for the fact that we divided the bit clock by a
735 * factor of two above.
736 *
737 * It's not clear exactly why this is necessary, but the display is
738 * not working properly otherwise. Perhaps the PLLs cannot generate
739 * frequencies sufficiently high.
740 */
741 *divp = ((8 * mul) / (div * dsi->lanes)) - 2;
742
743 /*
Thierry Redingdec72732013-09-03 08:45:46 +0200744 * XXX: Move the below somewhere else so that we don't need to have
745 * access to the vrefresh in this function?
746 */
747
748 /* one frame high-speed transmission timeout */
749 timeout = (bclk / vrefresh) / 512;
750 value = DSI_TIMEOUT_LRX(0x2000) | DSI_TIMEOUT_HTX(timeout);
751 tegra_dsi_writel(dsi, value, DSI_TIMEOUT_0);
752
753 /* 2 ms peripheral timeout for panel */
754 timeout = 2 * bclk / 512 * 1000;
755 value = DSI_TIMEOUT_PR(timeout) | DSI_TIMEOUT_TA(0x2000);
756 tegra_dsi_writel(dsi, value, DSI_TIMEOUT_1);
757
758 value = DSI_TALLY_TA(0) | DSI_TALLY_LRX(0) | DSI_TALLY_HTX(0);
759 tegra_dsi_writel(dsi, value, DSI_TO_TALLY);
760
761 return 0;
762}
763
764static int tegra_output_dsi_check_mode(struct tegra_output *output,
765 struct drm_display_mode *mode,
766 enum drm_mode_status *status)
767{
768 /*
769 * FIXME: For now, always assume that the mode is okay.
770 */
771
772 *status = MODE_OK;
773
774 return 0;
775}
776
777static const struct tegra_output_ops dsi_ops = {
778 .enable = tegra_output_dsi_enable,
779 .disable = tegra_output_dsi_disable,
780 .setup_clock = tegra_output_dsi_setup_clock,
781 .check_mode = tegra_output_dsi_check_mode,
782};
783
784static int tegra_dsi_pad_enable(struct tegra_dsi *dsi)
785{
786 unsigned long value;
787
788 value = DSI_PAD_CONTROL_VS1_PULLDN(0) | DSI_PAD_CONTROL_VS1_PDIO(0);
789 tegra_dsi_writel(dsi, value, DSI_PAD_CONTROL_0);
790
791 return 0;
792}
793
794static int tegra_dsi_pad_calibrate(struct tegra_dsi *dsi)
795{
Thierry Reding183ef282014-11-13 14:27:29 +0100796 u32 value;
Thierry Redingdec72732013-09-03 08:45:46 +0200797
798 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_0);
799 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_1);
800 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_2);
801 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_3);
802 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_4);
803
804 /* start calibration */
805 tegra_dsi_pad_enable(dsi);
806
807 value = DSI_PAD_SLEW_UP(0x7) | DSI_PAD_SLEW_DN(0x7) |
808 DSI_PAD_LP_UP(0x1) | DSI_PAD_LP_DN(0x1) |
809 DSI_PAD_OUT_CLK(0x0);
810 tegra_dsi_writel(dsi, value, DSI_PAD_CONTROL_2);
811
812 return tegra_mipi_calibrate(dsi->mipi);
813}
814
815static int tegra_dsi_init(struct host1x_client *client)
816{
Thierry Reding9910f5c2014-05-22 09:57:15 +0200817 struct drm_device *drm = dev_get_drvdata(client->parent);
Thierry Redingdec72732013-09-03 08:45:46 +0200818 struct tegra_dsi *dsi = host1x_client_to_dsi(client);
Thierry Redingdec72732013-09-03 08:45:46 +0200819 int err;
820
821 dsi->output.type = TEGRA_OUTPUT_DSI;
822 dsi->output.dev = client->dev;
823 dsi->output.ops = &dsi_ops;
824
Thierry Reding9910f5c2014-05-22 09:57:15 +0200825 err = tegra_output_init(drm, &dsi->output);
Thierry Redingdec72732013-09-03 08:45:46 +0200826 if (err < 0) {
827 dev_err(client->dev, "output setup failed: %d\n", err);
828 return err;
829 }
830
831 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
Thierry Reding9910f5c2014-05-22 09:57:15 +0200832 err = tegra_dsi_debugfs_init(dsi, drm->primary);
Thierry Redingdec72732013-09-03 08:45:46 +0200833 if (err < 0)
834 dev_err(dsi->dev, "debugfs setup failed: %d\n", err);
835 }
836
Thierry Redingdec72732013-09-03 08:45:46 +0200837 return 0;
838}
839
840static int tegra_dsi_exit(struct host1x_client *client)
841{
842 struct tegra_dsi *dsi = host1x_client_to_dsi(client);
843 int err;
844
845 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
846 err = tegra_dsi_debugfs_exit(dsi);
847 if (err < 0)
848 dev_err(dsi->dev, "debugfs cleanup failed: %d\n", err);
849 }
850
851 err = tegra_output_disable(&dsi->output);
852 if (err < 0) {
853 dev_err(client->dev, "output failed to disable: %d\n", err);
854 return err;
855 }
856
857 err = tegra_output_exit(&dsi->output);
858 if (err < 0) {
859 dev_err(client->dev, "output cleanup failed: %d\n", err);
860 return err;
861 }
862
863 return 0;
864}
865
866static const struct host1x_client_ops dsi_client_ops = {
867 .init = tegra_dsi_init,
868 .exit = tegra_dsi_exit,
869};
870
871static int tegra_dsi_setup_clocks(struct tegra_dsi *dsi)
872{
873 struct clk *parent;
874 int err;
875
876 parent = clk_get_parent(dsi->clk);
877 if (!parent)
878 return -EINVAL;
879
880 err = clk_set_parent(parent, dsi->clk_parent);
881 if (err < 0)
882 return err;
883
884 return 0;
885}
886
Thierry Redingdec72732013-09-03 08:45:46 +0200887static int tegra_dsi_host_attach(struct mipi_dsi_host *host,
888 struct mipi_dsi_device *device)
889{
890 struct tegra_dsi *dsi = host_to_tegra(host);
891 struct tegra_output *output = &dsi->output;
892
Thierry Reding17297a22014-03-14 14:13:15 +0100893 dsi->flags = device->mode_flags;
Thierry Redingdec72732013-09-03 08:45:46 +0200894 dsi->format = device->format;
895 dsi->lanes = device->lanes;
896
897 output->panel = of_drm_find_panel(device->dev.of_node);
898 if (output->panel) {
899 if (output->connector.dev)
900 drm_helper_hpd_irq_event(output->connector.dev);
901 }
902
903 return 0;
904}
905
906static int tegra_dsi_host_detach(struct mipi_dsi_host *host,
907 struct mipi_dsi_device *device)
908{
909 struct tegra_dsi *dsi = host_to_tegra(host);
910 struct tegra_output *output = &dsi->output;
911
912 if (output->panel && &device->dev == output->panel->dev) {
Thierry Redingba3df972014-11-13 14:54:01 +0100913 output->panel = NULL;
914
Thierry Redingdec72732013-09-03 08:45:46 +0200915 if (output->connector.dev)
916 drm_helper_hpd_irq_event(output->connector.dev);
Thierry Redingdec72732013-09-03 08:45:46 +0200917 }
918
919 return 0;
920}
921
922static const struct mipi_dsi_host_ops tegra_dsi_host_ops = {
923 .attach = tegra_dsi_host_attach,
924 .detach = tegra_dsi_host_detach,
925};
926
927static int tegra_dsi_probe(struct platform_device *pdev)
928{
929 struct tegra_dsi *dsi;
930 struct resource *regs;
931 int err;
932
933 dsi = devm_kzalloc(&pdev->dev, sizeof(*dsi), GFP_KERNEL);
934 if (!dsi)
935 return -ENOMEM;
936
937 dsi->output.dev = dsi->dev = &pdev->dev;
Thierry Reding976cebc2014-08-06 09:14:28 +0200938 dsi->video_fifo_depth = 1920;
939 dsi->host_fifo_depth = 64;
Thierry Redingdec72732013-09-03 08:45:46 +0200940
941 err = tegra_output_probe(&dsi->output);
942 if (err < 0)
943 return err;
944
Thierry Redingba3df972014-11-13 14:54:01 +0100945 dsi->output.connector.polled = DRM_CONNECTOR_POLL_HPD;
946
Thierry Redingdec72732013-09-03 08:45:46 +0200947 /*
948 * Assume these values by default. When a DSI peripheral driver
949 * attaches to the DSI host, the parameters will be taken from
950 * the attached device.
951 */
Thierry Reding17297a22014-03-14 14:13:15 +0100952 dsi->flags = MIPI_DSI_MODE_VIDEO;
Thierry Redingdec72732013-09-03 08:45:46 +0200953 dsi->format = MIPI_DSI_FMT_RGB888;
954 dsi->lanes = 4;
955
956 dsi->rst = devm_reset_control_get(&pdev->dev, "dsi");
957 if (IS_ERR(dsi->rst))
958 return PTR_ERR(dsi->rst);
959
Thierry Reding183ef282014-11-13 14:27:29 +0100960 err = reset_control_deassert(dsi->rst);
961 if (err < 0) {
962 dev_err(&pdev->dev, "failed to bring DSI out of reset: %d\n",
963 err);
964 return err;
965 }
966
Thierry Redingdec72732013-09-03 08:45:46 +0200967 dsi->clk = devm_clk_get(&pdev->dev, NULL);
968 if (IS_ERR(dsi->clk)) {
969 dev_err(&pdev->dev, "cannot get DSI clock\n");
Thierry Redingd2d0a9d2014-11-13 14:58:27 +0100970 err = PTR_ERR(dsi->clk);
971 goto reset;
Thierry Redingdec72732013-09-03 08:45:46 +0200972 }
973
974 err = clk_prepare_enable(dsi->clk);
975 if (err < 0) {
976 dev_err(&pdev->dev, "cannot enable DSI clock\n");
Thierry Redingd2d0a9d2014-11-13 14:58:27 +0100977 goto reset;
Thierry Redingdec72732013-09-03 08:45:46 +0200978 }
979
980 dsi->clk_lp = devm_clk_get(&pdev->dev, "lp");
981 if (IS_ERR(dsi->clk_lp)) {
982 dev_err(&pdev->dev, "cannot get low-power clock\n");
Thierry Redingd2d0a9d2014-11-13 14:58:27 +0100983 err = PTR_ERR(dsi->clk_lp);
984 goto disable_clk;
Thierry Redingdec72732013-09-03 08:45:46 +0200985 }
986
987 err = clk_prepare_enable(dsi->clk_lp);
988 if (err < 0) {
989 dev_err(&pdev->dev, "cannot enable low-power clock\n");
Thierry Redingd2d0a9d2014-11-13 14:58:27 +0100990 goto disable_clk;
Thierry Redingdec72732013-09-03 08:45:46 +0200991 }
992
993 dsi->clk_parent = devm_clk_get(&pdev->dev, "parent");
994 if (IS_ERR(dsi->clk_parent)) {
995 dev_err(&pdev->dev, "cannot get parent clock\n");
Thierry Redingd2d0a9d2014-11-13 14:58:27 +0100996 err = PTR_ERR(dsi->clk_parent);
997 goto disable_clk_lp;
Thierry Redingdec72732013-09-03 08:45:46 +0200998 }
999
Thierry Reding3b077af2014-03-14 14:07:50 +01001000 dsi->vdd = devm_regulator_get(&pdev->dev, "avdd-dsi-csi");
1001 if (IS_ERR(dsi->vdd)) {
1002 dev_err(&pdev->dev, "cannot get VDD supply\n");
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001003 err = PTR_ERR(dsi->vdd);
1004 goto disable_clk_lp;
Thierry Reding3b077af2014-03-14 14:07:50 +01001005 }
1006
1007 err = regulator_enable(dsi->vdd);
1008 if (err < 0) {
1009 dev_err(&pdev->dev, "cannot enable VDD supply\n");
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001010 goto disable_clk_lp;
Thierry Reding3b077af2014-03-14 14:07:50 +01001011 }
1012
Thierry Redingdec72732013-09-03 08:45:46 +02001013 err = tegra_dsi_setup_clocks(dsi);
1014 if (err < 0) {
1015 dev_err(&pdev->dev, "cannot setup clocks\n");
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001016 goto disable_vdd;
Thierry Redingdec72732013-09-03 08:45:46 +02001017 }
1018
1019 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1020 dsi->regs = devm_ioremap_resource(&pdev->dev, regs);
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001021 if (IS_ERR(dsi->regs)) {
1022 err = PTR_ERR(dsi->regs);
1023 goto disable_vdd;
1024 }
Thierry Redingdec72732013-09-03 08:45:46 +02001025
Thierry Redingdec72732013-09-03 08:45:46 +02001026 dsi->mipi = tegra_mipi_request(&pdev->dev);
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001027 if (IS_ERR(dsi->mipi)) {
1028 err = PTR_ERR(dsi->mipi);
1029 goto disable_vdd;
1030 }
Thierry Redingdec72732013-09-03 08:45:46 +02001031
Thierry Reding183ef282014-11-13 14:27:29 +01001032 err = tegra_dsi_pad_calibrate(dsi);
1033 if (err < 0) {
1034 dev_err(dsi->dev, "MIPI calibration failed: %d\n", err);
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001035 goto mipi_free;
Thierry Reding183ef282014-11-13 14:27:29 +01001036 }
1037
Thierry Redingdec72732013-09-03 08:45:46 +02001038 dsi->host.ops = &tegra_dsi_host_ops;
1039 dsi->host.dev = &pdev->dev;
1040
1041 err = mipi_dsi_host_register(&dsi->host);
1042 if (err < 0) {
1043 dev_err(&pdev->dev, "failed to register DSI host: %d\n", err);
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001044 goto mipi_free;
Thierry Redingdec72732013-09-03 08:45:46 +02001045 }
1046
1047 INIT_LIST_HEAD(&dsi->client.list);
1048 dsi->client.ops = &dsi_client_ops;
1049 dsi->client.dev = &pdev->dev;
1050
1051 err = host1x_client_register(&dsi->client);
1052 if (err < 0) {
1053 dev_err(&pdev->dev, "failed to register host1x client: %d\n",
1054 err);
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001055 goto unregister;
Thierry Redingdec72732013-09-03 08:45:46 +02001056 }
1057
1058 platform_set_drvdata(pdev, dsi);
1059
1060 return 0;
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001061
1062unregister:
1063 mipi_dsi_host_unregister(&dsi->host);
1064mipi_free:
1065 tegra_mipi_free(dsi->mipi);
1066disable_vdd:
1067 regulator_disable(dsi->vdd);
1068disable_clk_lp:
1069 clk_disable_unprepare(dsi->clk_lp);
1070disable_clk:
1071 clk_disable_unprepare(dsi->clk);
1072reset:
1073 reset_control_assert(dsi->rst);
1074 return err;
Thierry Redingdec72732013-09-03 08:45:46 +02001075}
1076
1077static int tegra_dsi_remove(struct platform_device *pdev)
1078{
1079 struct tegra_dsi *dsi = platform_get_drvdata(pdev);
1080 int err;
1081
1082 err = host1x_client_unregister(&dsi->client);
1083 if (err < 0) {
1084 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
1085 err);
1086 return err;
1087 }
1088
1089 mipi_dsi_host_unregister(&dsi->host);
1090 tegra_mipi_free(dsi->mipi);
1091
Thierry Reding3b077af2014-03-14 14:07:50 +01001092 regulator_disable(dsi->vdd);
Thierry Redingdec72732013-09-03 08:45:46 +02001093 clk_disable_unprepare(dsi->clk_lp);
1094 clk_disable_unprepare(dsi->clk);
Thierry Redingcb825d82014-03-14 14:25:43 +01001095 reset_control_assert(dsi->rst);
Thierry Redingdec72732013-09-03 08:45:46 +02001096
1097 err = tegra_output_remove(&dsi->output);
1098 if (err < 0) {
1099 dev_err(&pdev->dev, "failed to remove output: %d\n", err);
1100 return err;
1101 }
1102
1103 return 0;
1104}
1105
1106static const struct of_device_id tegra_dsi_of_match[] = {
1107 { .compatible = "nvidia,tegra114-dsi", },
1108 { },
1109};
Stephen Warrenef707282014-06-18 16:21:55 -06001110MODULE_DEVICE_TABLE(of, tegra_dsi_of_match);
Thierry Redingdec72732013-09-03 08:45:46 +02001111
1112struct platform_driver tegra_dsi_driver = {
1113 .driver = {
1114 .name = "tegra-dsi",
1115 .of_match_table = tegra_dsi_of_match,
1116 },
1117 .probe = tegra_dsi_probe,
1118 .remove = tegra_dsi_remove,
1119};