blob: 234cd1c3079ff4d701ea477551a4c02d94e26286 [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>
Thierry Redinge94236c2014-10-07 16:10:24 +020014#include <linux/of_platform.h>
Thierry Redingdec72732013-09-03 08:45:46 +020015#include <linux/platform_device.h>
16#include <linux/reset.h>
17
Thierry Reding3b077af2014-03-14 14:07:50 +010018#include <linux/regulator/consumer.h>
19
Thierry Reding4aa3df72014-11-24 16:27:13 +010020#include <drm/drm_atomic_helper.h>
Thierry Redingdec72732013-09-03 08:45:46 +020021#include <drm/drm_mipi_dsi.h>
22#include <drm/drm_panel.h>
23
24#include <video/mipi_display.h>
25
26#include "dc.h"
27#include "drm.h"
28#include "dsi.h"
29#include "mipi-phy.h"
30
Thierry Redingdec72732013-09-03 08:45:46 +020031struct tegra_dsi {
32 struct host1x_client client;
33 struct tegra_output output;
34 struct device *dev;
35
36 void __iomem *regs;
37
38 struct reset_control *rst;
39 struct clk *clk_parent;
40 struct clk *clk_lp;
41 struct clk *clk;
42
43 struct drm_info_list *debugfs_files;
44 struct drm_minor *minor;
45 struct dentry *debugfs;
46
Thierry Reding17297a22014-03-14 14:13:15 +010047 unsigned long flags;
Thierry Redingdec72732013-09-03 08:45:46 +020048 enum mipi_dsi_pixel_format format;
49 unsigned int lanes;
50
51 struct tegra_mipi_device *mipi;
52 struct mipi_dsi_host host;
Thierry Reding3b077af2014-03-14 14:07:50 +010053
54 struct regulator *vdd;
Thierry Reding976cebc2014-08-06 09:14:28 +020055
56 unsigned int video_fifo_depth;
57 unsigned int host_fifo_depth;
Thierry Redinge94236c2014-10-07 16:10:24 +020058
59 /* for ganged-mode support */
60 struct tegra_dsi *master;
61 struct tegra_dsi *slave;
Thierry Redingdec72732013-09-03 08:45:46 +020062};
63
64static inline struct tegra_dsi *
65host1x_client_to_dsi(struct host1x_client *client)
66{
67 return container_of(client, struct tegra_dsi, client);
68}
69
70static inline struct tegra_dsi *host_to_tegra(struct mipi_dsi_host *host)
71{
72 return container_of(host, struct tegra_dsi, host);
73}
74
75static inline struct tegra_dsi *to_dsi(struct tegra_output *output)
76{
77 return container_of(output, struct tegra_dsi, output);
78}
79
Thierry Reding9c0b4ca2014-11-24 12:27:59 +010080static inline u32 tegra_dsi_readl(struct tegra_dsi *dsi, unsigned long reg)
Thierry Redingdec72732013-09-03 08:45:46 +020081{
82 return readl(dsi->regs + (reg << 2));
83}
84
Thierry Reding9c0b4ca2014-11-24 12:27:59 +010085static inline void tegra_dsi_writel(struct tegra_dsi *dsi, u32 value,
Thierry Redingdec72732013-09-03 08:45:46 +020086 unsigned long reg)
87{
88 writel(value, dsi->regs + (reg << 2));
89}
90
91static int tegra_dsi_show_regs(struct seq_file *s, void *data)
92{
93 struct drm_info_node *node = s->private;
94 struct tegra_dsi *dsi = node->info_ent->data;
95
96#define DUMP_REG(name) \
Thierry Reding9c0b4ca2014-11-24 12:27:59 +010097 seq_printf(s, "%-32s %#05x %08x\n", #name, name, \
Thierry Redingdec72732013-09-03 08:45:46 +020098 tegra_dsi_readl(dsi, name))
99
100 DUMP_REG(DSI_INCR_SYNCPT);
101 DUMP_REG(DSI_INCR_SYNCPT_CONTROL);
102 DUMP_REG(DSI_INCR_SYNCPT_ERROR);
103 DUMP_REG(DSI_CTXSW);
104 DUMP_REG(DSI_RD_DATA);
105 DUMP_REG(DSI_WR_DATA);
106 DUMP_REG(DSI_POWER_CONTROL);
107 DUMP_REG(DSI_INT_ENABLE);
108 DUMP_REG(DSI_INT_STATUS);
109 DUMP_REG(DSI_INT_MASK);
110 DUMP_REG(DSI_HOST_CONTROL);
111 DUMP_REG(DSI_CONTROL);
112 DUMP_REG(DSI_SOL_DELAY);
113 DUMP_REG(DSI_MAX_THRESHOLD);
114 DUMP_REG(DSI_TRIGGER);
115 DUMP_REG(DSI_TX_CRC);
116 DUMP_REG(DSI_STATUS);
117
118 DUMP_REG(DSI_INIT_SEQ_CONTROL);
119 DUMP_REG(DSI_INIT_SEQ_DATA_0);
120 DUMP_REG(DSI_INIT_SEQ_DATA_1);
121 DUMP_REG(DSI_INIT_SEQ_DATA_2);
122 DUMP_REG(DSI_INIT_SEQ_DATA_3);
123 DUMP_REG(DSI_INIT_SEQ_DATA_4);
124 DUMP_REG(DSI_INIT_SEQ_DATA_5);
125 DUMP_REG(DSI_INIT_SEQ_DATA_6);
126 DUMP_REG(DSI_INIT_SEQ_DATA_7);
127
128 DUMP_REG(DSI_PKT_SEQ_0_LO);
129 DUMP_REG(DSI_PKT_SEQ_0_HI);
130 DUMP_REG(DSI_PKT_SEQ_1_LO);
131 DUMP_REG(DSI_PKT_SEQ_1_HI);
132 DUMP_REG(DSI_PKT_SEQ_2_LO);
133 DUMP_REG(DSI_PKT_SEQ_2_HI);
134 DUMP_REG(DSI_PKT_SEQ_3_LO);
135 DUMP_REG(DSI_PKT_SEQ_3_HI);
136 DUMP_REG(DSI_PKT_SEQ_4_LO);
137 DUMP_REG(DSI_PKT_SEQ_4_HI);
138 DUMP_REG(DSI_PKT_SEQ_5_LO);
139 DUMP_REG(DSI_PKT_SEQ_5_HI);
140
141 DUMP_REG(DSI_DCS_CMDS);
142
143 DUMP_REG(DSI_PKT_LEN_0_1);
144 DUMP_REG(DSI_PKT_LEN_2_3);
145 DUMP_REG(DSI_PKT_LEN_4_5);
146 DUMP_REG(DSI_PKT_LEN_6_7);
147
148 DUMP_REG(DSI_PHY_TIMING_0);
149 DUMP_REG(DSI_PHY_TIMING_1);
150 DUMP_REG(DSI_PHY_TIMING_2);
151 DUMP_REG(DSI_BTA_TIMING);
152
153 DUMP_REG(DSI_TIMEOUT_0);
154 DUMP_REG(DSI_TIMEOUT_1);
155 DUMP_REG(DSI_TO_TALLY);
156
157 DUMP_REG(DSI_PAD_CONTROL_0);
158 DUMP_REG(DSI_PAD_CONTROL_CD);
159 DUMP_REG(DSI_PAD_CD_STATUS);
160 DUMP_REG(DSI_VIDEO_MODE_CONTROL);
161 DUMP_REG(DSI_PAD_CONTROL_1);
162 DUMP_REG(DSI_PAD_CONTROL_2);
163 DUMP_REG(DSI_PAD_CONTROL_3);
164 DUMP_REG(DSI_PAD_CONTROL_4);
165
166 DUMP_REG(DSI_GANGED_MODE_CONTROL);
167 DUMP_REG(DSI_GANGED_MODE_START);
168 DUMP_REG(DSI_GANGED_MODE_SIZE);
169
170 DUMP_REG(DSI_RAW_DATA_BYTE_COUNT);
171 DUMP_REG(DSI_ULTRA_LOW_POWER_CONTROL);
172
173 DUMP_REG(DSI_INIT_SEQ_DATA_8);
174 DUMP_REG(DSI_INIT_SEQ_DATA_9);
175 DUMP_REG(DSI_INIT_SEQ_DATA_10);
176 DUMP_REG(DSI_INIT_SEQ_DATA_11);
177 DUMP_REG(DSI_INIT_SEQ_DATA_12);
178 DUMP_REG(DSI_INIT_SEQ_DATA_13);
179 DUMP_REG(DSI_INIT_SEQ_DATA_14);
180 DUMP_REG(DSI_INIT_SEQ_DATA_15);
181
182#undef DUMP_REG
183
184 return 0;
185}
186
187static struct drm_info_list debugfs_files[] = {
188 { "regs", tegra_dsi_show_regs, 0, NULL },
189};
190
191static int tegra_dsi_debugfs_init(struct tegra_dsi *dsi,
192 struct drm_minor *minor)
193{
194 const char *name = dev_name(dsi->dev);
195 unsigned int i;
196 int err;
197
198 dsi->debugfs = debugfs_create_dir(name, minor->debugfs_root);
199 if (!dsi->debugfs)
200 return -ENOMEM;
201
202 dsi->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
203 GFP_KERNEL);
204 if (!dsi->debugfs_files) {
205 err = -ENOMEM;
206 goto remove;
207 }
208
209 for (i = 0; i < ARRAY_SIZE(debugfs_files); i++)
210 dsi->debugfs_files[i].data = dsi;
211
212 err = drm_debugfs_create_files(dsi->debugfs_files,
213 ARRAY_SIZE(debugfs_files),
214 dsi->debugfs, minor);
215 if (err < 0)
216 goto free;
217
218 dsi->minor = minor;
219
220 return 0;
221
222free:
223 kfree(dsi->debugfs_files);
224 dsi->debugfs_files = NULL;
225remove:
226 debugfs_remove(dsi->debugfs);
227 dsi->debugfs = NULL;
228
229 return err;
230}
231
Thierry Reding4009c222014-12-19 15:47:30 +0100232static void tegra_dsi_debugfs_exit(struct tegra_dsi *dsi)
Thierry Redingdec72732013-09-03 08:45:46 +0200233{
234 drm_debugfs_remove_files(dsi->debugfs_files, ARRAY_SIZE(debugfs_files),
235 dsi->minor);
236 dsi->minor = NULL;
237
238 kfree(dsi->debugfs_files);
239 dsi->debugfs_files = NULL;
240
241 debugfs_remove(dsi->debugfs);
242 dsi->debugfs = NULL;
Thierry Redingdec72732013-09-03 08:45:46 +0200243}
244
245#define PKT_ID0(id) ((((id) & 0x3f) << 3) | (1 << 9))
246#define PKT_LEN0(len) (((len) & 0x07) << 0)
247#define PKT_ID1(id) ((((id) & 0x3f) << 13) | (1 << 19))
248#define PKT_LEN1(len) (((len) & 0x07) << 10)
249#define PKT_ID2(id) ((((id) & 0x3f) << 23) | (1 << 29))
250#define PKT_LEN2(len) (((len) & 0x07) << 20)
251
252#define PKT_LP (1 << 30)
253#define NUM_PKT_SEQ 12
254
Thierry Reding17297a22014-03-14 14:13:15 +0100255/*
256 * non-burst mode with sync pulses
257 */
258static const u32 pkt_seq_video_non_burst_sync_pulses[NUM_PKT_SEQ] = {
Thierry Redingdec72732013-09-03 08:45:46 +0200259 [ 0] = PKT_ID0(MIPI_DSI_V_SYNC_START) | PKT_LEN0(0) |
260 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
261 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0) |
262 PKT_LP,
263 [ 1] = 0,
264 [ 2] = PKT_ID0(MIPI_DSI_V_SYNC_END) | PKT_LEN0(0) |
265 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
266 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0) |
267 PKT_LP,
268 [ 3] = 0,
269 [ 4] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
270 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
271 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0) |
272 PKT_LP,
273 [ 5] = 0,
274 [ 6] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
275 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
276 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0),
277 [ 7] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(2) |
278 PKT_ID1(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN1(3) |
279 PKT_ID2(MIPI_DSI_BLANKING_PACKET) | PKT_LEN2(4),
280 [ 8] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
281 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
282 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0) |
283 PKT_LP,
284 [ 9] = 0,
285 [10] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
286 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
287 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0),
288 [11] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(2) |
289 PKT_ID1(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN1(3) |
290 PKT_ID2(MIPI_DSI_BLANKING_PACKET) | PKT_LEN2(4),
291};
292
Thierry Reding17297a22014-03-14 14:13:15 +0100293/*
294 * non-burst mode with sync events
295 */
296static const u32 pkt_seq_video_non_burst_sync_events[NUM_PKT_SEQ] = {
297 [ 0] = PKT_ID0(MIPI_DSI_V_SYNC_START) | PKT_LEN0(0) |
298 PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION) | PKT_LEN1(7) |
299 PKT_LP,
300 [ 1] = 0,
301 [ 2] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
302 PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION) | PKT_LEN1(7) |
303 PKT_LP,
304 [ 3] = 0,
305 [ 4] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
306 PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION) | PKT_LEN1(7) |
307 PKT_LP,
308 [ 5] = 0,
309 [ 6] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
310 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(2) |
311 PKT_ID2(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN2(3),
312 [ 7] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(4),
313 [ 8] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
314 PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION) | PKT_LEN1(7) |
315 PKT_LP,
316 [ 9] = 0,
317 [10] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
318 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(2) |
319 PKT_ID2(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN2(3),
320 [11] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(4),
321};
322
Thierry Reding337b4432014-11-13 15:02:46 +0100323static const u32 pkt_seq_command_mode[NUM_PKT_SEQ] = {
324 [ 0] = 0,
325 [ 1] = 0,
326 [ 2] = 0,
327 [ 3] = 0,
328 [ 4] = 0,
329 [ 5] = 0,
330 [ 6] = PKT_ID0(MIPI_DSI_DCS_LONG_WRITE) | PKT_LEN0(3) | PKT_LP,
331 [ 7] = 0,
332 [ 8] = 0,
333 [ 9] = 0,
334 [10] = PKT_ID0(MIPI_DSI_DCS_LONG_WRITE) | PKT_LEN0(5) | PKT_LP,
335 [11] = 0,
336};
337
Thierry Redingdec72732013-09-03 08:45:46 +0200338static int tegra_dsi_set_phy_timing(struct tegra_dsi *dsi)
339{
340 struct mipi_dphy_timing timing;
Thierry Reding9c0b4ca2014-11-24 12:27:59 +0100341 unsigned long period;
342 u32 value;
Thierry Redingdec72732013-09-03 08:45:46 +0200343 long rate;
344 int err;
345
346 rate = clk_get_rate(dsi->clk);
347 if (rate < 0)
348 return rate;
349
Thierry Reding369bc652014-11-07 17:17:41 +0100350 period = DIV_ROUND_CLOSEST(NSEC_PER_SEC, rate * 2);
Thierry Redingdec72732013-09-03 08:45:46 +0200351
352 err = mipi_dphy_timing_get_default(&timing, period);
353 if (err < 0)
354 return err;
355
356 err = mipi_dphy_timing_validate(&timing, period);
357 if (err < 0) {
358 dev_err(dsi->dev, "failed to validate D-PHY timing: %d\n", err);
359 return err;
360 }
361
362 /*
363 * The D-PHY timing fields below are expressed in byte-clock cycles,
364 * so multiply the period by 8.
365 */
366 period *= 8;
367
368 value = DSI_TIMING_FIELD(timing.hsexit, period, 1) << 24 |
369 DSI_TIMING_FIELD(timing.hstrail, period, 0) << 16 |
370 DSI_TIMING_FIELD(timing.hszero, period, 3) << 8 |
371 DSI_TIMING_FIELD(timing.hsprepare, period, 1);
372 tegra_dsi_writel(dsi, value, DSI_PHY_TIMING_0);
373
374 value = DSI_TIMING_FIELD(timing.clktrail, period, 1) << 24 |
375 DSI_TIMING_FIELD(timing.clkpost, period, 1) << 16 |
376 DSI_TIMING_FIELD(timing.clkzero, period, 1) << 8 |
377 DSI_TIMING_FIELD(timing.lpx, period, 1);
378 tegra_dsi_writel(dsi, value, DSI_PHY_TIMING_1);
379
380 value = DSI_TIMING_FIELD(timing.clkprepare, period, 1) << 16 |
381 DSI_TIMING_FIELD(timing.clkpre, period, 1) << 8 |
382 DSI_TIMING_FIELD(0xff * period, period, 0) << 0;
383 tegra_dsi_writel(dsi, value, DSI_PHY_TIMING_2);
384
385 value = DSI_TIMING_FIELD(timing.taget, period, 1) << 16 |
386 DSI_TIMING_FIELD(timing.tasure, period, 1) << 8 |
387 DSI_TIMING_FIELD(timing.tago, period, 1);
388 tegra_dsi_writel(dsi, value, DSI_BTA_TIMING);
389
Sean Paul7e3bc3a2014-10-07 16:04:42 +0200390 if (dsi->slave)
391 return tegra_dsi_set_phy_timing(dsi->slave);
392
Thierry Redingdec72732013-09-03 08:45:46 +0200393 return 0;
394}
395
396static int tegra_dsi_get_muldiv(enum mipi_dsi_pixel_format format,
397 unsigned int *mulp, unsigned int *divp)
398{
399 switch (format) {
400 case MIPI_DSI_FMT_RGB666_PACKED:
401 case MIPI_DSI_FMT_RGB888:
402 *mulp = 3;
403 *divp = 1;
404 break;
405
406 case MIPI_DSI_FMT_RGB565:
407 *mulp = 2;
408 *divp = 1;
409 break;
410
411 case MIPI_DSI_FMT_RGB666:
412 *mulp = 9;
413 *divp = 4;
414 break;
415
416 default:
417 return -EINVAL;
418 }
419
420 return 0;
421}
422
Thierry Redingf7d68892014-03-13 08:50:39 +0100423static int tegra_dsi_get_format(enum mipi_dsi_pixel_format format,
424 enum tegra_dsi_format *fmt)
425{
426 switch (format) {
427 case MIPI_DSI_FMT_RGB888:
428 *fmt = TEGRA_DSI_FORMAT_24P;
429 break;
430
431 case MIPI_DSI_FMT_RGB666:
432 *fmt = TEGRA_DSI_FORMAT_18NP;
433 break;
434
435 case MIPI_DSI_FMT_RGB666_PACKED:
436 *fmt = TEGRA_DSI_FORMAT_18P;
437 break;
438
439 case MIPI_DSI_FMT_RGB565:
440 *fmt = TEGRA_DSI_FORMAT_16P;
441 break;
442
443 default:
444 return -EINVAL;
445 }
446
447 return 0;
448}
449
Thierry Redinge94236c2014-10-07 16:10:24 +0200450static void tegra_dsi_ganged_enable(struct tegra_dsi *dsi, unsigned int start,
451 unsigned int size)
452{
453 u32 value;
454
455 tegra_dsi_writel(dsi, start, DSI_GANGED_MODE_START);
456 tegra_dsi_writel(dsi, size << 16 | size, DSI_GANGED_MODE_SIZE);
457
458 value = DSI_GANGED_MODE_CONTROL_ENABLE;
459 tegra_dsi_writel(dsi, value, DSI_GANGED_MODE_CONTROL);
460}
461
Thierry Reding563eff12014-11-13 14:44:27 +0100462static void tegra_dsi_enable(struct tegra_dsi *dsi)
Thierry Redingdec72732013-09-03 08:45:46 +0200463{
Thierry Reding563eff12014-11-13 14:44:27 +0100464 u32 value;
Thierry Redingdec72732013-09-03 08:45:46 +0200465
Thierry Reding563eff12014-11-13 14:44:27 +0100466 value = tegra_dsi_readl(dsi, DSI_POWER_CONTROL);
467 value |= DSI_POWER_CONTROL_ENABLE;
468 tegra_dsi_writel(dsi, value, DSI_POWER_CONTROL);
Thierry Redinge94236c2014-10-07 16:10:24 +0200469
470 if (dsi->slave)
471 tegra_dsi_enable(dsi->slave);
472}
473
474static unsigned int tegra_dsi_get_lanes(struct tegra_dsi *dsi)
475{
476 if (dsi->master)
477 return dsi->master->lanes + dsi->lanes;
478
479 if (dsi->slave)
480 return dsi->lanes + dsi->slave->lanes;
481
482 return dsi->lanes;
Thierry Reding563eff12014-11-13 14:44:27 +0100483}
484
485static int tegra_dsi_configure(struct tegra_dsi *dsi, unsigned int pipe,
486 const struct drm_display_mode *mode)
487{
488 unsigned int hact, hsw, hbp, hfp, i, mul, div;
489 enum tegra_dsi_format format;
490 const u32 *pkt_seq;
491 u32 value;
492 int err;
Thierry Reding334ae6b2014-03-14 14:15:10 +0100493
Thierry Reding17297a22014-03-14 14:13:15 +0100494 if (dsi->flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE) {
495 DRM_DEBUG_KMS("Non-burst video mode with sync pulses\n");
496 pkt_seq = pkt_seq_video_non_burst_sync_pulses;
Thierry Reding337b4432014-11-13 15:02:46 +0100497 } else if (dsi->flags & MIPI_DSI_MODE_VIDEO) {
Thierry Reding17297a22014-03-14 14:13:15 +0100498 DRM_DEBUG_KMS("Non-burst video mode with sync events\n");
499 pkt_seq = pkt_seq_video_non_burst_sync_events;
Thierry Reding337b4432014-11-13 15:02:46 +0100500 } else {
501 DRM_DEBUG_KMS("Command mode\n");
502 pkt_seq = pkt_seq_command_mode;
Thierry Reding17297a22014-03-14 14:13:15 +0100503 }
504
Thierry Redingdec72732013-09-03 08:45:46 +0200505 err = tegra_dsi_get_muldiv(dsi->format, &mul, &div);
506 if (err < 0)
507 return err;
508
Thierry Redingf7d68892014-03-13 08:50:39 +0100509 err = tegra_dsi_get_format(dsi->format, &format);
510 if (err < 0)
511 return err;
512
Thierry Redingf7d68892014-03-13 08:50:39 +0100513 value = DSI_CONTROL_CHANNEL(0) | DSI_CONTROL_FORMAT(format) |
Thierry Redingdec72732013-09-03 08:45:46 +0200514 DSI_CONTROL_LANES(dsi->lanes - 1) |
Thierry Reding563eff12014-11-13 14:44:27 +0100515 DSI_CONTROL_SOURCE(pipe);
Thierry Redingdec72732013-09-03 08:45:46 +0200516 tegra_dsi_writel(dsi, value, DSI_CONTROL);
517
Thierry Reding976cebc2014-08-06 09:14:28 +0200518 tegra_dsi_writel(dsi, dsi->video_fifo_depth, DSI_MAX_THRESHOLD);
Thierry Redingdec72732013-09-03 08:45:46 +0200519
Thierry Reding563eff12014-11-13 14:44:27 +0100520 value = DSI_HOST_CONTROL_HS;
Thierry Redingdec72732013-09-03 08:45:46 +0200521 tegra_dsi_writel(dsi, value, DSI_HOST_CONTROL);
522
523 value = tegra_dsi_readl(dsi, DSI_CONTROL);
Thierry Reding563eff12014-11-13 14:44:27 +0100524
Alexandre Courbot0c6b1e42014-07-08 21:32:13 +0900525 if (dsi->flags & MIPI_DSI_CLOCK_NON_CONTINUOUS)
526 value |= DSI_CONTROL_HS_CLK_CTRL;
Thierry Reding563eff12014-11-13 14:44:27 +0100527
Thierry Redingdec72732013-09-03 08:45:46 +0200528 value &= ~DSI_CONTROL_TX_TRIG(3);
Thierry Reding337b4432014-11-13 15:02:46 +0100529
530 /* enable DCS commands for command mode */
531 if (dsi->flags & MIPI_DSI_MODE_VIDEO)
532 value &= ~DSI_CONTROL_DCS_ENABLE;
533 else
534 value |= DSI_CONTROL_DCS_ENABLE;
535
Thierry Redingdec72732013-09-03 08:45:46 +0200536 value |= DSI_CONTROL_VIDEO_ENABLE;
537 value &= ~DSI_CONTROL_HOST_ENABLE;
538 tegra_dsi_writel(dsi, value, DSI_CONTROL);
539
Thierry Redingdec72732013-09-03 08:45:46 +0200540 for (i = 0; i < NUM_PKT_SEQ; i++)
541 tegra_dsi_writel(dsi, pkt_seq[i], DSI_PKT_SEQ_0_LO + i);
542
Thierry Reding337b4432014-11-13 15:02:46 +0100543 if (dsi->flags & MIPI_DSI_MODE_VIDEO) {
544 /* horizontal active pixels */
545 hact = mode->hdisplay * mul / div;
Thierry Redingdec72732013-09-03 08:45:46 +0200546
Thierry Reding337b4432014-11-13 15:02:46 +0100547 /* horizontal sync width */
548 hsw = (mode->hsync_end - mode->hsync_start) * mul / div;
549 hsw -= 10;
Thierry Redingdec72732013-09-03 08:45:46 +0200550
Thierry Reding337b4432014-11-13 15:02:46 +0100551 /* horizontal back porch */
552 hbp = (mode->htotal - mode->hsync_end) * mul / div;
553 hbp -= 14;
Thierry Redingdec72732013-09-03 08:45:46 +0200554
Thierry Reding337b4432014-11-13 15:02:46 +0100555 /* horizontal front porch */
556 hfp = (mode->hsync_start - mode->hdisplay) * mul / div;
557 hfp -= 8;
Thierry Redingdec72732013-09-03 08:45:46 +0200558
Thierry Reding337b4432014-11-13 15:02:46 +0100559 tegra_dsi_writel(dsi, hsw << 16 | 0, DSI_PKT_LEN_0_1);
560 tegra_dsi_writel(dsi, hact << 16 | hbp, DSI_PKT_LEN_2_3);
561 tegra_dsi_writel(dsi, hfp, DSI_PKT_LEN_4_5);
562 tegra_dsi_writel(dsi, 0x0f0f << 16, DSI_PKT_LEN_6_7);
Thierry Redingdec72732013-09-03 08:45:46 +0200563
Thierry Reding337b4432014-11-13 15:02:46 +0100564 /* set SOL delay (for non-burst mode only) */
565 tegra_dsi_writel(dsi, 8 * mul / div, DSI_SOL_DELAY);
Thierry Redinge94236c2014-10-07 16:10:24 +0200566
567 /* TODO: implement ganged mode */
Thierry Reding337b4432014-11-13 15:02:46 +0100568 } else {
569 u16 bytes;
570
Thierry Redinge94236c2014-10-07 16:10:24 +0200571 if (dsi->master || dsi->slave) {
572 /*
573 * For ganged mode, assume symmetric left-right mode.
574 */
575 bytes = 1 + (mode->hdisplay / 2) * mul / div;
576 } else {
577 /* 1 byte (DCS command) + pixel data */
578 bytes = 1 + mode->hdisplay * mul / div;
579 }
Thierry Reding337b4432014-11-13 15:02:46 +0100580
581 tegra_dsi_writel(dsi, 0, DSI_PKT_LEN_0_1);
582 tegra_dsi_writel(dsi, bytes << 16, DSI_PKT_LEN_2_3);
583 tegra_dsi_writel(dsi, bytes << 16, DSI_PKT_LEN_4_5);
584 tegra_dsi_writel(dsi, 0, DSI_PKT_LEN_6_7);
585
586 value = MIPI_DCS_WRITE_MEMORY_START << 8 |
587 MIPI_DCS_WRITE_MEMORY_CONTINUE;
588 tegra_dsi_writel(dsi, value, DSI_DCS_CMDS);
589
Thierry Redinge94236c2014-10-07 16:10:24 +0200590 /* set SOL delay */
591 if (dsi->master || dsi->slave) {
592 unsigned int lanes = tegra_dsi_get_lanes(dsi);
593 unsigned long delay, bclk, bclk_ganged;
594
595 /* SOL to valid, valid to FIFO and FIFO write delay */
596 delay = 4 + 4 + 2;
597 delay = DIV_ROUND_UP(delay * mul, div * lanes);
598 /* FIFO read delay */
599 delay = delay + 6;
600
601 bclk = DIV_ROUND_UP(mode->htotal * mul, div * lanes);
602 bclk_ganged = DIV_ROUND_UP(bclk * lanes / 2, lanes);
603 value = bclk - bclk_ganged + delay + 20;
604 } else {
605 /* TODO: revisit for non-ganged mode */
606 value = 8 * mul / div;
607 }
Thierry Reding337b4432014-11-13 15:02:46 +0100608
609 tegra_dsi_writel(dsi, value, DSI_SOL_DELAY);
610 }
Thierry Redingdec72732013-09-03 08:45:46 +0200611
Thierry Redinge94236c2014-10-07 16:10:24 +0200612 if (dsi->slave) {
613 err = tegra_dsi_configure(dsi->slave, pipe, mode);
614 if (err < 0)
615 return err;
616
617 /*
618 * TODO: Support modes other than symmetrical left-right
619 * split.
620 */
621 tegra_dsi_ganged_enable(dsi, 0, mode->hdisplay / 2);
622 tegra_dsi_ganged_enable(dsi->slave, mode->hdisplay / 2,
623 mode->hdisplay / 2);
624 }
625
Thierry Reding563eff12014-11-13 14:44:27 +0100626 return 0;
627}
628
Thierry Reding563eff12014-11-13 14:44:27 +0100629static int tegra_dsi_wait_idle(struct tegra_dsi *dsi, unsigned long timeout)
630{
631 u32 value;
632
633 timeout = jiffies + msecs_to_jiffies(timeout);
634
635 while (time_before(jiffies, timeout)) {
636 value = tegra_dsi_readl(dsi, DSI_STATUS);
637 if (value & DSI_STATUS_IDLE)
638 return 0;
639
640 usleep_range(1000, 2000);
641 }
642
643 return -ETIMEDOUT;
644}
645
646static void tegra_dsi_video_disable(struct tegra_dsi *dsi)
647{
648 u32 value;
649
650 value = tegra_dsi_readl(dsi, DSI_CONTROL);
651 value &= ~DSI_CONTROL_VIDEO_ENABLE;
652 tegra_dsi_writel(dsi, value, DSI_CONTROL);
Thierry Redinge94236c2014-10-07 16:10:24 +0200653
654 if (dsi->slave)
655 tegra_dsi_video_disable(dsi->slave);
656}
657
658static void tegra_dsi_ganged_disable(struct tegra_dsi *dsi)
659{
660 tegra_dsi_writel(dsi, 0, DSI_GANGED_MODE_START);
661 tegra_dsi_writel(dsi, 0, DSI_GANGED_MODE_SIZE);
662 tegra_dsi_writel(dsi, 0, DSI_GANGED_MODE_CONTROL);
Thierry Reding563eff12014-11-13 14:44:27 +0100663}
664
Thierry Reding5b901e72014-12-02 17:30:23 +0100665static void tegra_dsi_set_timeout(struct tegra_dsi *dsi, unsigned long bclk,
666 unsigned int vrefresh)
667{
668 unsigned int timeout;
669 u32 value;
670
671 /* one frame high-speed transmission timeout */
672 timeout = (bclk / vrefresh) / 512;
673 value = DSI_TIMEOUT_LRX(0x2000) | DSI_TIMEOUT_HTX(timeout);
674 tegra_dsi_writel(dsi, value, DSI_TIMEOUT_0);
675
676 /* 2 ms peripheral timeout for panel */
677 timeout = 2 * bclk / 512 * 1000;
678 value = DSI_TIMEOUT_PR(timeout) | DSI_TIMEOUT_TA(0x2000);
679 tegra_dsi_writel(dsi, value, DSI_TIMEOUT_1);
680
681 value = DSI_TALLY_TA(0) | DSI_TALLY_LRX(0) | DSI_TALLY_HTX(0);
682 tegra_dsi_writel(dsi, value, DSI_TO_TALLY);
683
684 if (dsi->slave)
685 tegra_dsi_set_timeout(dsi->slave, bclk, vrefresh);
686}
687
Thierry Reding563eff12014-11-13 14:44:27 +0100688static void tegra_dsi_disable(struct tegra_dsi *dsi)
689{
690 u32 value;
691
Thierry Redinge94236c2014-10-07 16:10:24 +0200692 if (dsi->slave) {
693 tegra_dsi_ganged_disable(dsi->slave);
694 tegra_dsi_ganged_disable(dsi);
695 }
696
Thierry Reding563eff12014-11-13 14:44:27 +0100697 value = tegra_dsi_readl(dsi, DSI_POWER_CONTROL);
698 value &= ~DSI_POWER_CONTROL_ENABLE;
699 tegra_dsi_writel(dsi, value, DSI_POWER_CONTROL);
700
Thierry Redinge94236c2014-10-07 16:10:24 +0200701 if (dsi->slave)
702 tegra_dsi_disable(dsi->slave);
703
Thierry Reding563eff12014-11-13 14:44:27 +0100704 usleep_range(5000, 10000);
705}
706
Thierry Reding92f0e072014-11-24 16:29:40 +0100707static void tegra_dsi_soft_reset(struct tegra_dsi *dsi)
708{
709 u32 value;
710
711 value = tegra_dsi_readl(dsi, DSI_POWER_CONTROL);
712 value &= ~DSI_POWER_CONTROL_ENABLE;
713 tegra_dsi_writel(dsi, value, DSI_POWER_CONTROL);
714
715 usleep_range(300, 1000);
716
717 value = tegra_dsi_readl(dsi, DSI_POWER_CONTROL);
718 value |= DSI_POWER_CONTROL_ENABLE;
719 tegra_dsi_writel(dsi, value, DSI_POWER_CONTROL);
720
721 usleep_range(300, 1000);
722
723 value = tegra_dsi_readl(dsi, DSI_TRIGGER);
724 if (value)
725 tegra_dsi_writel(dsi, 0, DSI_TRIGGER);
726
727 if (dsi->slave)
728 tegra_dsi_soft_reset(dsi->slave);
729}
730
Thierry Reding5b901e72014-12-02 17:30:23 +0100731static void tegra_dsi_connector_dpms(struct drm_connector *connector, int mode)
Thierry Redingdec72732013-09-03 08:45:46 +0200732{
Thierry Redingdec72732013-09-03 08:45:46 +0200733}
734
Thierry Reding5b901e72014-12-02 17:30:23 +0100735static const struct drm_connector_funcs tegra_dsi_connector_funcs = {
736 .dpms = tegra_dsi_connector_dpms,
737 .detect = tegra_output_connector_detect,
738 .fill_modes = drm_helper_probe_single_connector_modes,
739 .destroy = tegra_output_connector_destroy,
Thierry Reding4aa3df72014-11-24 16:27:13 +0100740 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
Thierry Reding5b901e72014-12-02 17:30:23 +0100741};
742
743static enum drm_mode_status
744tegra_dsi_connector_mode_valid(struct drm_connector *connector,
745 struct drm_display_mode *mode)
Thierry Reding3f6b4062014-11-13 14:50:33 +0100746{
Thierry Reding5b901e72014-12-02 17:30:23 +0100747 return MODE_OK;
Thierry Reding3f6b4062014-11-13 14:50:33 +0100748}
749
Thierry Reding5b901e72014-12-02 17:30:23 +0100750static const struct drm_connector_helper_funcs tegra_dsi_connector_helper_funcs = {
751 .get_modes = tegra_output_connector_get_modes,
752 .mode_valid = tegra_dsi_connector_mode_valid,
753 .best_encoder = tegra_output_connector_best_encoder,
754};
755
756static const struct drm_encoder_funcs tegra_dsi_encoder_funcs = {
757 .destroy = tegra_output_encoder_destroy,
758};
759
760static void tegra_dsi_encoder_dpms(struct drm_encoder *encoder, int mode)
Thierry Redingdec72732013-09-03 08:45:46 +0200761{
Thierry Reding5b901e72014-12-02 17:30:23 +0100762}
763
764static bool tegra_dsi_encoder_mode_fixup(struct drm_encoder *encoder,
765 const struct drm_display_mode *mode,
766 struct drm_display_mode *adjusted)
767{
768 struct tegra_output *output = encoder_to_output(encoder);
769 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
770 unsigned int mul, div, scdiv, vrefresh, lanes;
Thierry Redingdec72732013-09-03 08:45:46 +0200771 struct tegra_dsi *dsi = to_dsi(output);
Thierry Reding5b901e72014-12-02 17:30:23 +0100772 unsigned long pclk, bclk, plld;
Thierry Redingdec72732013-09-03 08:45:46 +0200773 int err;
774
Thierry Redinge94236c2014-10-07 16:10:24 +0200775 lanes = tegra_dsi_get_lanes(dsi);
Thierry Reding5b901e72014-12-02 17:30:23 +0100776 pclk = mode->clock * 1000;
Thierry Redinge94236c2014-10-07 16:10:24 +0200777
Thierry Redingdec72732013-09-03 08:45:46 +0200778 err = tegra_dsi_get_muldiv(dsi->format, &mul, &div);
779 if (err < 0)
780 return err;
781
Thierry Redinge94236c2014-10-07 16:10:24 +0200782 DRM_DEBUG_KMS("mul: %u, div: %u, lanes: %u\n", mul, div, lanes);
Thierry Redingdec72732013-09-03 08:45:46 +0200783 vrefresh = drm_mode_vrefresh(mode);
Thierry Reding91eded92014-03-26 13:32:21 +0100784 DRM_DEBUG_KMS("vrefresh: %u\n", vrefresh);
Thierry Redingdec72732013-09-03 08:45:46 +0200785
Thierry Reding91eded92014-03-26 13:32:21 +0100786 /* compute byte clock */
Thierry Redinge94236c2014-10-07 16:10:24 +0200787 bclk = (pclk * mul) / (div * lanes);
Thierry Reding91eded92014-03-26 13:32:21 +0100788
789 /*
790 * Compute bit clock and round up to the next MHz.
791 */
Thierry Reding030611e2014-11-13 14:32:06 +0100792 plld = DIV_ROUND_UP(bclk * 8, USEC_PER_SEC) * USEC_PER_SEC;
Thierry Reding91eded92014-03-26 13:32:21 +0100793
794 /*
795 * We divide the frequency by two here, but we make up for that by
796 * setting the shift clock divider (further below) to half of the
797 * correct value.
798 */
799 plld /= 2;
Thierry Redingdec72732013-09-03 08:45:46 +0200800
Thierry Redingdec72732013-09-03 08:45:46 +0200801 /*
Thierry Reding91eded92014-03-26 13:32:21 +0100802 * Derive pixel clock from bit clock using the shift clock divider.
803 * Note that this is only half of what we would expect, but we need
804 * that to make up for the fact that we divided the bit clock by a
805 * factor of two above.
806 *
807 * It's not clear exactly why this is necessary, but the display is
808 * not working properly otherwise. Perhaps the PLLs cannot generate
809 * frequencies sufficiently high.
810 */
Thierry Reding5b901e72014-12-02 17:30:23 +0100811 scdiv = ((8 * mul) / (div * lanes)) - 2;
Thierry Reding91eded92014-03-26 13:32:21 +0100812
Thierry Reding5b901e72014-12-02 17:30:23 +0100813 err = tegra_dc_setup_clock(dc, dsi->clk_parent, plld, scdiv);
814 if (err < 0) {
815 dev_err(output->dev, "failed to setup DC clock: %d\n", err);
816 return false;
817 }
818
819 err = clk_set_rate(dsi->clk_parent, plld);
820 if (err < 0) {
821 dev_err(dsi->dev, "failed to set clock rate to %lu Hz\n",
822 plld);
823 return false;
824 }
825
Thierry Reding3f6b4062014-11-13 14:50:33 +0100826 tegra_dsi_set_timeout(dsi, bclk, vrefresh);
Thierry Redingdec72732013-09-03 08:45:46 +0200827
Sean Paul7e3bc3a2014-10-07 16:04:42 +0200828 err = tegra_dsi_set_phy_timing(dsi);
Thierry Reding5b901e72014-12-02 17:30:23 +0100829 if (err < 0) {
830 dev_err(dsi->dev, "failed to setup D-PHY timing: %d\n", err);
831 return false;
832 }
Sean Paul7e3bc3a2014-10-07 16:04:42 +0200833
Thierry Reding5b901e72014-12-02 17:30:23 +0100834 return true;
Thierry Redingdec72732013-09-03 08:45:46 +0200835}
836
Thierry Reding5b901e72014-12-02 17:30:23 +0100837static void tegra_dsi_encoder_prepare(struct drm_encoder *encoder)
Thierry Redingdec72732013-09-03 08:45:46 +0200838{
Thierry Redingdec72732013-09-03 08:45:46 +0200839}
840
Thierry Reding5b901e72014-12-02 17:30:23 +0100841static void tegra_dsi_encoder_commit(struct drm_encoder *encoder)
842{
843}
844
845static void tegra_dsi_encoder_mode_set(struct drm_encoder *encoder,
846 struct drm_display_mode *mode,
847 struct drm_display_mode *adjusted)
848{
849 struct tegra_output *output = encoder_to_output(encoder);
850 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
851 struct tegra_dsi *dsi = to_dsi(output);
852 u32 value;
853 int err;
854
855
856 err = tegra_dsi_configure(dsi, dc->pipe, mode);
857 if (err < 0) {
858 dev_err(dsi->dev, "failed to configure DSI: %d\n", err);
859 return;
860 }
861
862 if (output->panel)
863 drm_panel_prepare(output->panel);
864
865 /* enable display controller */
866 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
867 value |= DSI_ENABLE;
868 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
869
870 value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
871 value &= ~DISP_CTRL_MODE_MASK;
872 value |= DISP_CTRL_MODE_C_DISPLAY;
873 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
874
875 value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
876 value |= PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
877 PW4_ENABLE | PM0_ENABLE | PM1_ENABLE;
878 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
879
880 tegra_dc_commit(dc);
881
882 /* enable DSI controller */
883 tegra_dsi_enable(dsi);
884
885 if (output->panel)
886 drm_panel_enable(output->panel);
887
888 return;
889}
890
891static void tegra_dsi_encoder_disable(struct drm_encoder *encoder)
892{
893 struct tegra_output *output = encoder_to_output(encoder);
894 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
895 struct tegra_dsi *dsi = to_dsi(output);
896 u32 value;
897 int err;
898
899 if (output->panel)
900 drm_panel_disable(output->panel);
901
902 tegra_dsi_video_disable(dsi);
903
904 /*
905 * The following accesses registers of the display controller, so make
906 * sure it's only executed when the output is attached to one.
907 */
908 if (dc) {
909 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
910 value &= ~DSI_ENABLE;
911 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
912
913 tegra_dc_commit(dc);
914 }
915
916 err = tegra_dsi_wait_idle(dsi, 100);
917 if (err < 0)
918 dev_dbg(dsi->dev, "failed to idle DSI: %d\n", err);
919
920 tegra_dsi_soft_reset(dsi);
921
922 if (output->panel)
923 drm_panel_unprepare(output->panel);
924
925 tegra_dsi_disable(dsi);
926
927 return;
928}
929
930static const struct drm_encoder_helper_funcs tegra_dsi_encoder_helper_funcs = {
931 .dpms = tegra_dsi_encoder_dpms,
932 .mode_fixup = tegra_dsi_encoder_mode_fixup,
933 .prepare = tegra_dsi_encoder_prepare,
934 .commit = tegra_dsi_encoder_commit,
935 .mode_set = tegra_dsi_encoder_mode_set,
936 .disable = tegra_dsi_encoder_disable,
Thierry Redingdec72732013-09-03 08:45:46 +0200937};
938
939static int tegra_dsi_pad_enable(struct tegra_dsi *dsi)
940{
Thierry Reding9c0b4ca2014-11-24 12:27:59 +0100941 u32 value;
Thierry Redingdec72732013-09-03 08:45:46 +0200942
943 value = DSI_PAD_CONTROL_VS1_PULLDN(0) | DSI_PAD_CONTROL_VS1_PDIO(0);
944 tegra_dsi_writel(dsi, value, DSI_PAD_CONTROL_0);
945
946 return 0;
947}
948
949static int tegra_dsi_pad_calibrate(struct tegra_dsi *dsi)
950{
Thierry Reding183ef282014-11-13 14:27:29 +0100951 u32 value;
Thierry Redingdec72732013-09-03 08:45:46 +0200952
953 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_0);
954 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_1);
955 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_2);
956 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_3);
957 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_4);
958
959 /* start calibration */
960 tegra_dsi_pad_enable(dsi);
961
962 value = DSI_PAD_SLEW_UP(0x7) | DSI_PAD_SLEW_DN(0x7) |
963 DSI_PAD_LP_UP(0x1) | DSI_PAD_LP_DN(0x1) |
964 DSI_PAD_OUT_CLK(0x0);
965 tegra_dsi_writel(dsi, value, DSI_PAD_CONTROL_2);
966
967 return tegra_mipi_calibrate(dsi->mipi);
968}
969
970static int tegra_dsi_init(struct host1x_client *client)
971{
Thierry Reding9910f5c2014-05-22 09:57:15 +0200972 struct drm_device *drm = dev_get_drvdata(client->parent);
Thierry Redingdec72732013-09-03 08:45:46 +0200973 struct tegra_dsi *dsi = host1x_client_to_dsi(client);
Thierry Redingdec72732013-09-03 08:45:46 +0200974 int err;
975
Thierry Reding201106d2014-11-24 16:31:48 +0100976 reset_control_deassert(dsi->rst);
977
978 err = tegra_dsi_pad_calibrate(dsi);
979 if (err < 0) {
980 dev_err(dsi->dev, "MIPI calibration failed: %d\n", err);
981 goto reset;
982 }
983
Thierry Redinge94236c2014-10-07 16:10:24 +0200984 /* Gangsters must not register their own outputs. */
985 if (!dsi->master) {
Thierry Redinge94236c2014-10-07 16:10:24 +0200986 dsi->output.dev = client->dev;
Thierry Redingdec72732013-09-03 08:45:46 +0200987
Thierry Reding5b901e72014-12-02 17:30:23 +0100988 drm_connector_init(drm, &dsi->output.connector,
989 &tegra_dsi_connector_funcs,
990 DRM_MODE_CONNECTOR_DSI);
991 drm_connector_helper_add(&dsi->output.connector,
992 &tegra_dsi_connector_helper_funcs);
993 dsi->output.connector.dpms = DRM_MODE_DPMS_OFF;
994
Thierry Reding5b901e72014-12-02 17:30:23 +0100995 drm_encoder_init(drm, &dsi->output.encoder,
996 &tegra_dsi_encoder_funcs,
997 DRM_MODE_ENCODER_DSI);
998 drm_encoder_helper_add(&dsi->output.encoder,
999 &tegra_dsi_encoder_helper_funcs);
1000
1001 drm_mode_connector_attach_encoder(&dsi->output.connector,
1002 &dsi->output.encoder);
1003 drm_connector_register(&dsi->output.connector);
1004
Thierry Redingea130b22014-12-19 15:51:35 +01001005 err = tegra_output_init(drm, &dsi->output);
1006 if (err < 0) {
1007 dev_err(client->dev,
1008 "failed to initialize output: %d\n",
1009 err);
1010 goto reset;
1011 }
1012
Thierry Reding5b901e72014-12-02 17:30:23 +01001013 dsi->output.encoder.possible_crtcs = 0x3;
Thierry Redingdec72732013-09-03 08:45:46 +02001014 }
1015
1016 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
Thierry Reding9910f5c2014-05-22 09:57:15 +02001017 err = tegra_dsi_debugfs_init(dsi, drm->primary);
Thierry Redingdec72732013-09-03 08:45:46 +02001018 if (err < 0)
1019 dev_err(dsi->dev, "debugfs setup failed: %d\n", err);
1020 }
1021
Thierry Redingdec72732013-09-03 08:45:46 +02001022 return 0;
Thierry Reding201106d2014-11-24 16:31:48 +01001023
1024reset:
1025 reset_control_assert(dsi->rst);
1026 return err;
Thierry Redingdec72732013-09-03 08:45:46 +02001027}
1028
1029static int tegra_dsi_exit(struct host1x_client *client)
1030{
1031 struct tegra_dsi *dsi = host1x_client_to_dsi(client);
Thierry Redingdec72732013-09-03 08:45:46 +02001032
Thierry Reding5b901e72014-12-02 17:30:23 +01001033 tegra_output_exit(&dsi->output);
1034
Thierry Reding4009c222014-12-19 15:47:30 +01001035 if (IS_ENABLED(CONFIG_DEBUG_FS))
1036 tegra_dsi_debugfs_exit(dsi);
Thierry Redingdec72732013-09-03 08:45:46 +02001037
Thierry Reding201106d2014-11-24 16:31:48 +01001038 reset_control_assert(dsi->rst);
1039
Thierry Redingdec72732013-09-03 08:45:46 +02001040 return 0;
1041}
1042
1043static const struct host1x_client_ops dsi_client_ops = {
1044 .init = tegra_dsi_init,
1045 .exit = tegra_dsi_exit,
1046};
1047
1048static int tegra_dsi_setup_clocks(struct tegra_dsi *dsi)
1049{
1050 struct clk *parent;
1051 int err;
1052
1053 parent = clk_get_parent(dsi->clk);
1054 if (!parent)
1055 return -EINVAL;
1056
1057 err = clk_set_parent(parent, dsi->clk_parent);
1058 if (err < 0)
1059 return err;
1060
1061 return 0;
1062}
1063
Thierry Reding0fffdf62014-11-07 17:25:26 +01001064static const char * const error_report[16] = {
1065 "SoT Error",
1066 "SoT Sync Error",
1067 "EoT Sync Error",
1068 "Escape Mode Entry Command Error",
1069 "Low-Power Transmit Sync Error",
1070 "Peripheral Timeout Error",
1071 "False Control Error",
1072 "Contention Detected",
1073 "ECC Error, single-bit",
1074 "ECC Error, multi-bit",
1075 "Checksum Error",
1076 "DSI Data Type Not Recognized",
1077 "DSI VC ID Invalid",
1078 "Invalid Transmission Length",
1079 "Reserved",
1080 "DSI Protocol Violation",
1081};
1082
1083static ssize_t tegra_dsi_read_response(struct tegra_dsi *dsi,
1084 const struct mipi_dsi_msg *msg,
1085 size_t count)
1086{
1087 u8 *rx = msg->rx_buf;
1088 unsigned int i, j, k;
1089 size_t size = 0;
1090 u16 errors;
1091 u32 value;
1092
1093 /* read and parse packet header */
1094 value = tegra_dsi_readl(dsi, DSI_RD_DATA);
1095
1096 switch (value & 0x3f) {
1097 case MIPI_DSI_RX_ACKNOWLEDGE_AND_ERROR_REPORT:
1098 errors = (value >> 8) & 0xffff;
1099 dev_dbg(dsi->dev, "Acknowledge and error report: %04x\n",
1100 errors);
1101 for (i = 0; i < ARRAY_SIZE(error_report); i++)
1102 if (errors & BIT(i))
1103 dev_dbg(dsi->dev, " %2u: %s\n", i,
1104 error_report[i]);
1105 break;
1106
1107 case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_1BYTE:
1108 rx[0] = (value >> 8) & 0xff;
1109 size = 1;
1110 break;
1111
1112 case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_2BYTE:
1113 rx[0] = (value >> 8) & 0xff;
1114 rx[1] = (value >> 16) & 0xff;
1115 size = 2;
1116 break;
1117
1118 case MIPI_DSI_RX_DCS_LONG_READ_RESPONSE:
1119 size = ((value >> 8) & 0xff00) | ((value >> 8) & 0xff);
1120 break;
1121
1122 case MIPI_DSI_RX_GENERIC_LONG_READ_RESPONSE:
1123 size = ((value >> 8) & 0xff00) | ((value >> 8) & 0xff);
1124 break;
1125
1126 default:
1127 dev_err(dsi->dev, "unhandled response type: %02x\n",
1128 value & 0x3f);
1129 return -EPROTO;
1130 }
1131
1132 size = min(size, msg->rx_len);
1133
1134 if (msg->rx_buf && size > 0) {
1135 for (i = 0, j = 0; i < count - 1; i++, j += 4) {
1136 u8 *rx = msg->rx_buf + j;
1137
1138 value = tegra_dsi_readl(dsi, DSI_RD_DATA);
1139
1140 for (k = 0; k < 4 && (j + k) < msg->rx_len; k++)
1141 rx[j + k] = (value >> (k << 3)) & 0xff;
1142 }
1143 }
1144
1145 return size;
1146}
1147
1148static int tegra_dsi_transmit(struct tegra_dsi *dsi, unsigned long timeout)
1149{
1150 tegra_dsi_writel(dsi, DSI_TRIGGER_HOST, DSI_TRIGGER);
1151
1152 timeout = jiffies + msecs_to_jiffies(timeout);
1153
1154 while (time_before(jiffies, timeout)) {
1155 u32 value = tegra_dsi_readl(dsi, DSI_TRIGGER);
1156 if ((value & DSI_TRIGGER_HOST) == 0)
1157 return 0;
1158
1159 usleep_range(1000, 2000);
1160 }
1161
1162 DRM_DEBUG_KMS("timeout waiting for transmission to complete\n");
1163 return -ETIMEDOUT;
1164}
1165
1166static int tegra_dsi_wait_for_response(struct tegra_dsi *dsi,
1167 unsigned long timeout)
1168{
1169 timeout = jiffies + msecs_to_jiffies(250);
1170
1171 while (time_before(jiffies, timeout)) {
1172 u32 value = tegra_dsi_readl(dsi, DSI_STATUS);
1173 u8 count = value & 0x1f;
1174
1175 if (count > 0)
1176 return count;
1177
1178 usleep_range(1000, 2000);
1179 }
1180
1181 DRM_DEBUG_KMS("peripheral returned no data\n");
1182 return -ETIMEDOUT;
1183}
1184
1185static void tegra_dsi_writesl(struct tegra_dsi *dsi, unsigned long offset,
1186 const void *buffer, size_t size)
1187{
1188 const u8 *buf = buffer;
1189 size_t i, j;
1190 u32 value;
1191
1192 for (j = 0; j < size; j += 4) {
1193 value = 0;
1194
1195 for (i = 0; i < 4 && j + i < size; i++)
1196 value |= buf[j + i] << (i << 3);
1197
1198 tegra_dsi_writel(dsi, value, DSI_WR_DATA);
1199 }
1200}
1201
1202static ssize_t tegra_dsi_host_transfer(struct mipi_dsi_host *host,
1203 const struct mipi_dsi_msg *msg)
1204{
1205 struct tegra_dsi *dsi = host_to_tegra(host);
1206 struct mipi_dsi_packet packet;
1207 const u8 *header;
1208 size_t count;
1209 ssize_t err;
1210 u32 value;
1211
1212 err = mipi_dsi_create_packet(&packet, msg);
1213 if (err < 0)
1214 return err;
1215
1216 header = packet.header;
1217
1218 /* maximum FIFO depth is 1920 words */
1219 if (packet.size > dsi->video_fifo_depth * 4)
1220 return -ENOSPC;
1221
1222 /* reset underflow/overflow flags */
1223 value = tegra_dsi_readl(dsi, DSI_STATUS);
1224 if (value & (DSI_STATUS_UNDERFLOW | DSI_STATUS_OVERFLOW)) {
1225 value = DSI_HOST_CONTROL_FIFO_RESET;
1226 tegra_dsi_writel(dsi, value, DSI_HOST_CONTROL);
1227 usleep_range(10, 20);
1228 }
1229
1230 value = tegra_dsi_readl(dsi, DSI_POWER_CONTROL);
1231 value |= DSI_POWER_CONTROL_ENABLE;
1232 tegra_dsi_writel(dsi, value, DSI_POWER_CONTROL);
1233
1234 usleep_range(5000, 10000);
1235
1236 value = DSI_HOST_CONTROL_CRC_RESET | DSI_HOST_CONTROL_TX_TRIG_HOST |
1237 DSI_HOST_CONTROL_CS | DSI_HOST_CONTROL_ECC;
1238
1239 if ((msg->flags & MIPI_DSI_MSG_USE_LPM) == 0)
1240 value |= DSI_HOST_CONTROL_HS;
1241
1242 /*
1243 * The host FIFO has a maximum of 64 words, so larger transmissions
1244 * need to use the video FIFO.
1245 */
1246 if (packet.size > dsi->host_fifo_depth * 4)
1247 value |= DSI_HOST_CONTROL_FIFO_SEL;
1248
1249 tegra_dsi_writel(dsi, value, DSI_HOST_CONTROL);
1250
1251 /*
1252 * For reads and messages with explicitly requested ACK, generate a
1253 * BTA sequence after the transmission of the packet.
1254 */
1255 if ((msg->flags & MIPI_DSI_MSG_REQ_ACK) ||
1256 (msg->rx_buf && msg->rx_len > 0)) {
1257 value = tegra_dsi_readl(dsi, DSI_HOST_CONTROL);
1258 value |= DSI_HOST_CONTROL_PKT_BTA;
1259 tegra_dsi_writel(dsi, value, DSI_HOST_CONTROL);
1260 }
1261
1262 value = DSI_CONTROL_LANES(0) | DSI_CONTROL_HOST_ENABLE;
1263 tegra_dsi_writel(dsi, value, DSI_CONTROL);
1264
1265 /* write packet header, ECC is generated by hardware */
1266 value = header[2] << 16 | header[1] << 8 | header[0];
1267 tegra_dsi_writel(dsi, value, DSI_WR_DATA);
1268
1269 /* write payload (if any) */
1270 if (packet.payload_length > 0)
1271 tegra_dsi_writesl(dsi, DSI_WR_DATA, packet.payload,
1272 packet.payload_length);
1273
1274 err = tegra_dsi_transmit(dsi, 250);
1275 if (err < 0)
1276 return err;
1277
1278 if ((msg->flags & MIPI_DSI_MSG_REQ_ACK) ||
1279 (msg->rx_buf && msg->rx_len > 0)) {
1280 err = tegra_dsi_wait_for_response(dsi, 250);
1281 if (err < 0)
1282 return err;
1283
1284 count = err;
1285
1286 value = tegra_dsi_readl(dsi, DSI_RD_DATA);
1287 switch (value) {
1288 case 0x84:
1289 /*
1290 dev_dbg(dsi->dev, "ACK\n");
1291 */
1292 break;
1293
1294 case 0x87:
1295 /*
1296 dev_dbg(dsi->dev, "ESCAPE\n");
1297 */
1298 break;
1299
1300 default:
1301 dev_err(dsi->dev, "unknown status: %08x\n", value);
1302 break;
1303 }
1304
1305 if (count > 1) {
1306 err = tegra_dsi_read_response(dsi, msg, count);
1307 if (err < 0)
1308 dev_err(dsi->dev,
1309 "failed to parse response: %zd\n",
1310 err);
1311 else {
1312 /*
1313 * For read commands, return the number of
1314 * bytes returned by the peripheral.
1315 */
1316 count = err;
1317 }
1318 }
1319 } else {
1320 /*
1321 * For write commands, we have transmitted the 4-byte header
1322 * plus the variable-length payload.
1323 */
1324 count = 4 + packet.payload_length;
1325 }
1326
1327 return count;
1328}
1329
Thierry Redinge94236c2014-10-07 16:10:24 +02001330static int tegra_dsi_ganged_setup(struct tegra_dsi *dsi)
1331{
1332 struct clk *parent;
1333 int err;
1334
1335 /* make sure both DSI controllers share the same PLL */
1336 parent = clk_get_parent(dsi->slave->clk);
1337 if (!parent)
1338 return -EINVAL;
1339
1340 err = clk_set_parent(parent, dsi->clk_parent);
1341 if (err < 0)
1342 return err;
1343
1344 return 0;
1345}
1346
Thierry Redingdec72732013-09-03 08:45:46 +02001347static int tegra_dsi_host_attach(struct mipi_dsi_host *host,
1348 struct mipi_dsi_device *device)
1349{
1350 struct tegra_dsi *dsi = host_to_tegra(host);
Thierry Redingdec72732013-09-03 08:45:46 +02001351
Thierry Reding17297a22014-03-14 14:13:15 +01001352 dsi->flags = device->mode_flags;
Thierry Redingdec72732013-09-03 08:45:46 +02001353 dsi->format = device->format;
1354 dsi->lanes = device->lanes;
1355
Thierry Redinge94236c2014-10-07 16:10:24 +02001356 if (dsi->slave) {
1357 int err;
1358
1359 dev_dbg(dsi->dev, "attaching dual-channel device %s\n",
1360 dev_name(&device->dev));
1361
1362 err = tegra_dsi_ganged_setup(dsi);
1363 if (err < 0) {
1364 dev_err(dsi->dev, "failed to set up ganged mode: %d\n",
1365 err);
1366 return err;
1367 }
1368 }
1369
1370 /*
1371 * Slaves don't have a panel associated with them, so they provide
1372 * merely the second channel.
1373 */
1374 if (!dsi->master) {
1375 struct tegra_output *output = &dsi->output;
1376
1377 output->panel = of_drm_find_panel(device->dev.of_node);
1378 if (output->panel && output->connector.dev) {
1379 drm_panel_attach(output->panel, &output->connector);
Thierry Redingdec72732013-09-03 08:45:46 +02001380 drm_helper_hpd_irq_event(output->connector.dev);
Thierry Redinge94236c2014-10-07 16:10:24 +02001381 }
Thierry Redingdec72732013-09-03 08:45:46 +02001382 }
1383
1384 return 0;
1385}
1386
1387static int tegra_dsi_host_detach(struct mipi_dsi_host *host,
1388 struct mipi_dsi_device *device)
1389{
1390 struct tegra_dsi *dsi = host_to_tegra(host);
1391 struct tegra_output *output = &dsi->output;
1392
1393 if (output->panel && &device->dev == output->panel->dev) {
Thierry Redingba3df972014-11-13 14:54:01 +01001394 output->panel = NULL;
1395
Thierry Redingdec72732013-09-03 08:45:46 +02001396 if (output->connector.dev)
1397 drm_helper_hpd_irq_event(output->connector.dev);
Thierry Redingdec72732013-09-03 08:45:46 +02001398 }
1399
1400 return 0;
1401}
1402
1403static const struct mipi_dsi_host_ops tegra_dsi_host_ops = {
1404 .attach = tegra_dsi_host_attach,
1405 .detach = tegra_dsi_host_detach,
Thierry Reding0fffdf62014-11-07 17:25:26 +01001406 .transfer = tegra_dsi_host_transfer,
Thierry Redingdec72732013-09-03 08:45:46 +02001407};
1408
Thierry Redinge94236c2014-10-07 16:10:24 +02001409static int tegra_dsi_ganged_probe(struct tegra_dsi *dsi)
1410{
1411 struct device_node *np;
1412
1413 np = of_parse_phandle(dsi->dev->of_node, "nvidia,ganged-mode", 0);
1414 if (np) {
1415 struct platform_device *gangster = of_find_device_by_node(np);
1416
1417 dsi->slave = platform_get_drvdata(gangster);
1418 of_node_put(np);
1419
1420 if (!dsi->slave)
1421 return -EPROBE_DEFER;
1422
1423 dsi->slave->master = dsi;
1424 }
1425
1426 return 0;
1427}
1428
Thierry Redingdec72732013-09-03 08:45:46 +02001429static int tegra_dsi_probe(struct platform_device *pdev)
1430{
1431 struct tegra_dsi *dsi;
1432 struct resource *regs;
1433 int err;
1434
1435 dsi = devm_kzalloc(&pdev->dev, sizeof(*dsi), GFP_KERNEL);
1436 if (!dsi)
1437 return -ENOMEM;
1438
1439 dsi->output.dev = dsi->dev = &pdev->dev;
Thierry Reding976cebc2014-08-06 09:14:28 +02001440 dsi->video_fifo_depth = 1920;
1441 dsi->host_fifo_depth = 64;
Thierry Redingdec72732013-09-03 08:45:46 +02001442
Thierry Redinge94236c2014-10-07 16:10:24 +02001443 err = tegra_dsi_ganged_probe(dsi);
1444 if (err < 0)
1445 return err;
1446
Thierry Redingdec72732013-09-03 08:45:46 +02001447 err = tegra_output_probe(&dsi->output);
1448 if (err < 0)
1449 return err;
1450
Thierry Redingba3df972014-11-13 14:54:01 +01001451 dsi->output.connector.polled = DRM_CONNECTOR_POLL_HPD;
1452
Thierry Redingdec72732013-09-03 08:45:46 +02001453 /*
1454 * Assume these values by default. When a DSI peripheral driver
1455 * attaches to the DSI host, the parameters will be taken from
1456 * the attached device.
1457 */
Thierry Reding17297a22014-03-14 14:13:15 +01001458 dsi->flags = MIPI_DSI_MODE_VIDEO;
Thierry Redingdec72732013-09-03 08:45:46 +02001459 dsi->format = MIPI_DSI_FMT_RGB888;
1460 dsi->lanes = 4;
1461
1462 dsi->rst = devm_reset_control_get(&pdev->dev, "dsi");
1463 if (IS_ERR(dsi->rst))
1464 return PTR_ERR(dsi->rst);
1465
1466 dsi->clk = devm_clk_get(&pdev->dev, NULL);
1467 if (IS_ERR(dsi->clk)) {
1468 dev_err(&pdev->dev, "cannot get DSI clock\n");
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001469 err = PTR_ERR(dsi->clk);
1470 goto reset;
Thierry Redingdec72732013-09-03 08:45:46 +02001471 }
1472
1473 err = clk_prepare_enable(dsi->clk);
1474 if (err < 0) {
1475 dev_err(&pdev->dev, "cannot enable DSI clock\n");
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001476 goto reset;
Thierry Redingdec72732013-09-03 08:45:46 +02001477 }
1478
1479 dsi->clk_lp = devm_clk_get(&pdev->dev, "lp");
1480 if (IS_ERR(dsi->clk_lp)) {
1481 dev_err(&pdev->dev, "cannot get low-power clock\n");
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001482 err = PTR_ERR(dsi->clk_lp);
1483 goto disable_clk;
Thierry Redingdec72732013-09-03 08:45:46 +02001484 }
1485
1486 err = clk_prepare_enable(dsi->clk_lp);
1487 if (err < 0) {
1488 dev_err(&pdev->dev, "cannot enable low-power clock\n");
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001489 goto disable_clk;
Thierry Redingdec72732013-09-03 08:45:46 +02001490 }
1491
1492 dsi->clk_parent = devm_clk_get(&pdev->dev, "parent");
1493 if (IS_ERR(dsi->clk_parent)) {
1494 dev_err(&pdev->dev, "cannot get parent clock\n");
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001495 err = PTR_ERR(dsi->clk_parent);
1496 goto disable_clk_lp;
Thierry Redingdec72732013-09-03 08:45:46 +02001497 }
1498
Thierry Reding3b077af2014-03-14 14:07:50 +01001499 dsi->vdd = devm_regulator_get(&pdev->dev, "avdd-dsi-csi");
1500 if (IS_ERR(dsi->vdd)) {
1501 dev_err(&pdev->dev, "cannot get VDD supply\n");
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001502 err = PTR_ERR(dsi->vdd);
1503 goto disable_clk_lp;
Thierry Reding3b077af2014-03-14 14:07:50 +01001504 }
1505
1506 err = regulator_enable(dsi->vdd);
1507 if (err < 0) {
1508 dev_err(&pdev->dev, "cannot enable VDD supply\n");
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001509 goto disable_clk_lp;
Thierry Reding3b077af2014-03-14 14:07:50 +01001510 }
1511
Thierry Redingdec72732013-09-03 08:45:46 +02001512 err = tegra_dsi_setup_clocks(dsi);
1513 if (err < 0) {
1514 dev_err(&pdev->dev, "cannot setup clocks\n");
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001515 goto disable_vdd;
Thierry Redingdec72732013-09-03 08:45:46 +02001516 }
1517
1518 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1519 dsi->regs = devm_ioremap_resource(&pdev->dev, regs);
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001520 if (IS_ERR(dsi->regs)) {
1521 err = PTR_ERR(dsi->regs);
1522 goto disable_vdd;
1523 }
Thierry Redingdec72732013-09-03 08:45:46 +02001524
Thierry Redingdec72732013-09-03 08:45:46 +02001525 dsi->mipi = tegra_mipi_request(&pdev->dev);
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001526 if (IS_ERR(dsi->mipi)) {
1527 err = PTR_ERR(dsi->mipi);
1528 goto disable_vdd;
1529 }
Thierry Redingdec72732013-09-03 08:45:46 +02001530
1531 dsi->host.ops = &tegra_dsi_host_ops;
1532 dsi->host.dev = &pdev->dev;
1533
1534 err = mipi_dsi_host_register(&dsi->host);
1535 if (err < 0) {
1536 dev_err(&pdev->dev, "failed to register DSI host: %d\n", err);
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001537 goto mipi_free;
Thierry Redingdec72732013-09-03 08:45:46 +02001538 }
1539
1540 INIT_LIST_HEAD(&dsi->client.list);
1541 dsi->client.ops = &dsi_client_ops;
1542 dsi->client.dev = &pdev->dev;
1543
1544 err = host1x_client_register(&dsi->client);
1545 if (err < 0) {
1546 dev_err(&pdev->dev, "failed to register host1x client: %d\n",
1547 err);
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001548 goto unregister;
Thierry Redingdec72732013-09-03 08:45:46 +02001549 }
1550
1551 platform_set_drvdata(pdev, dsi);
1552
1553 return 0;
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001554
1555unregister:
1556 mipi_dsi_host_unregister(&dsi->host);
1557mipi_free:
1558 tegra_mipi_free(dsi->mipi);
1559disable_vdd:
1560 regulator_disable(dsi->vdd);
1561disable_clk_lp:
1562 clk_disable_unprepare(dsi->clk_lp);
1563disable_clk:
1564 clk_disable_unprepare(dsi->clk);
1565reset:
1566 reset_control_assert(dsi->rst);
1567 return err;
Thierry Redingdec72732013-09-03 08:45:46 +02001568}
1569
1570static int tegra_dsi_remove(struct platform_device *pdev)
1571{
1572 struct tegra_dsi *dsi = platform_get_drvdata(pdev);
1573 int err;
1574
1575 err = host1x_client_unregister(&dsi->client);
1576 if (err < 0) {
1577 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
1578 err);
1579 return err;
1580 }
1581
Thierry Reding328ec692014-12-19 15:55:08 +01001582 tegra_output_remove(&dsi->output);
Thierry Reding5b901e72014-12-02 17:30:23 +01001583
Thierry Redingdec72732013-09-03 08:45:46 +02001584 mipi_dsi_host_unregister(&dsi->host);
1585 tegra_mipi_free(dsi->mipi);
1586
Thierry Reding3b077af2014-03-14 14:07:50 +01001587 regulator_disable(dsi->vdd);
Thierry Redingdec72732013-09-03 08:45:46 +02001588 clk_disable_unprepare(dsi->clk_lp);
1589 clk_disable_unprepare(dsi->clk);
Thierry Redingcb825d82014-03-14 14:25:43 +01001590 reset_control_assert(dsi->rst);
Thierry Redingdec72732013-09-03 08:45:46 +02001591
Thierry Redingdec72732013-09-03 08:45:46 +02001592 return 0;
1593}
1594
1595static const struct of_device_id tegra_dsi_of_match[] = {
1596 { .compatible = "nvidia,tegra114-dsi", },
1597 { },
1598};
Stephen Warrenef707282014-06-18 16:21:55 -06001599MODULE_DEVICE_TABLE(of, tegra_dsi_of_match);
Thierry Redingdec72732013-09-03 08:45:46 +02001600
1601struct platform_driver tegra_dsi_driver = {
1602 .driver = {
1603 .name = "tegra-dsi",
1604 .of_match_table = tegra_dsi_of_match,
1605 },
1606 .probe = tegra_dsi_probe,
1607 .remove = tegra_dsi_remove,
1608};