blob: 00b307120cdd3fce3927bcc061bae856b0684985 [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 Redingebd14af2014-12-08 16:22:28 +010031struct tegra_dsi_state {
32 struct drm_connector_state base;
33
34 struct mipi_dphy_timing timing;
35 unsigned long period;
36
37 unsigned int vrefresh;
38 unsigned int lanes;
39 unsigned long pclk;
40 unsigned long bclk;
41
42 enum tegra_dsi_format format;
43 unsigned int mul;
44 unsigned int div;
45};
46
47static inline struct tegra_dsi_state *
48to_dsi_state(struct drm_connector_state *state)
49{
50 return container_of(state, struct tegra_dsi_state, base);
51}
52
Thierry Redingdec72732013-09-03 08:45:46 +020053struct tegra_dsi {
54 struct host1x_client client;
55 struct tegra_output output;
56 struct device *dev;
57
58 void __iomem *regs;
59
60 struct reset_control *rst;
61 struct clk *clk_parent;
62 struct clk *clk_lp;
63 struct clk *clk;
64
65 struct drm_info_list *debugfs_files;
66 struct drm_minor *minor;
67 struct dentry *debugfs;
68
Thierry Reding17297a22014-03-14 14:13:15 +010069 unsigned long flags;
Thierry Redingdec72732013-09-03 08:45:46 +020070 enum mipi_dsi_pixel_format format;
71 unsigned int lanes;
72
73 struct tegra_mipi_device *mipi;
74 struct mipi_dsi_host host;
Thierry Reding3b077af2014-03-14 14:07:50 +010075
76 struct regulator *vdd;
Thierry Reding976cebc2014-08-06 09:14:28 +020077
78 unsigned int video_fifo_depth;
79 unsigned int host_fifo_depth;
Thierry Redinge94236c2014-10-07 16:10:24 +020080
81 /* for ganged-mode support */
82 struct tegra_dsi *master;
83 struct tegra_dsi *slave;
Thierry Redingdec72732013-09-03 08:45:46 +020084};
85
86static inline struct tegra_dsi *
87host1x_client_to_dsi(struct host1x_client *client)
88{
89 return container_of(client, struct tegra_dsi, client);
90}
91
92static inline struct tegra_dsi *host_to_tegra(struct mipi_dsi_host *host)
93{
94 return container_of(host, struct tegra_dsi, host);
95}
96
97static inline struct tegra_dsi *to_dsi(struct tegra_output *output)
98{
99 return container_of(output, struct tegra_dsi, output);
100}
101
Thierry Redingebd14af2014-12-08 16:22:28 +0100102static struct tegra_dsi_state *tegra_dsi_get_state(struct tegra_dsi *dsi)
103{
104 return to_dsi_state(dsi->output.connector.state);
105}
106
Thierry Reding9c0b4ca2014-11-24 12:27:59 +0100107static inline u32 tegra_dsi_readl(struct tegra_dsi *dsi, unsigned long reg)
Thierry Redingdec72732013-09-03 08:45:46 +0200108{
109 return readl(dsi->regs + (reg << 2));
110}
111
Thierry Reding9c0b4ca2014-11-24 12:27:59 +0100112static inline void tegra_dsi_writel(struct tegra_dsi *dsi, u32 value,
Thierry Redingdec72732013-09-03 08:45:46 +0200113 unsigned long reg)
114{
115 writel(value, dsi->regs + (reg << 2));
116}
117
118static int tegra_dsi_show_regs(struct seq_file *s, void *data)
119{
120 struct drm_info_node *node = s->private;
121 struct tegra_dsi *dsi = node->info_ent->data;
122
123#define DUMP_REG(name) \
Thierry Reding9c0b4ca2014-11-24 12:27:59 +0100124 seq_printf(s, "%-32s %#05x %08x\n", #name, name, \
Thierry Redingdec72732013-09-03 08:45:46 +0200125 tegra_dsi_readl(dsi, name))
126
127 DUMP_REG(DSI_INCR_SYNCPT);
128 DUMP_REG(DSI_INCR_SYNCPT_CONTROL);
129 DUMP_REG(DSI_INCR_SYNCPT_ERROR);
130 DUMP_REG(DSI_CTXSW);
131 DUMP_REG(DSI_RD_DATA);
132 DUMP_REG(DSI_WR_DATA);
133 DUMP_REG(DSI_POWER_CONTROL);
134 DUMP_REG(DSI_INT_ENABLE);
135 DUMP_REG(DSI_INT_STATUS);
136 DUMP_REG(DSI_INT_MASK);
137 DUMP_REG(DSI_HOST_CONTROL);
138 DUMP_REG(DSI_CONTROL);
139 DUMP_REG(DSI_SOL_DELAY);
140 DUMP_REG(DSI_MAX_THRESHOLD);
141 DUMP_REG(DSI_TRIGGER);
142 DUMP_REG(DSI_TX_CRC);
143 DUMP_REG(DSI_STATUS);
144
145 DUMP_REG(DSI_INIT_SEQ_CONTROL);
146 DUMP_REG(DSI_INIT_SEQ_DATA_0);
147 DUMP_REG(DSI_INIT_SEQ_DATA_1);
148 DUMP_REG(DSI_INIT_SEQ_DATA_2);
149 DUMP_REG(DSI_INIT_SEQ_DATA_3);
150 DUMP_REG(DSI_INIT_SEQ_DATA_4);
151 DUMP_REG(DSI_INIT_SEQ_DATA_5);
152 DUMP_REG(DSI_INIT_SEQ_DATA_6);
153 DUMP_REG(DSI_INIT_SEQ_DATA_7);
154
155 DUMP_REG(DSI_PKT_SEQ_0_LO);
156 DUMP_REG(DSI_PKT_SEQ_0_HI);
157 DUMP_REG(DSI_PKT_SEQ_1_LO);
158 DUMP_REG(DSI_PKT_SEQ_1_HI);
159 DUMP_REG(DSI_PKT_SEQ_2_LO);
160 DUMP_REG(DSI_PKT_SEQ_2_HI);
161 DUMP_REG(DSI_PKT_SEQ_3_LO);
162 DUMP_REG(DSI_PKT_SEQ_3_HI);
163 DUMP_REG(DSI_PKT_SEQ_4_LO);
164 DUMP_REG(DSI_PKT_SEQ_4_HI);
165 DUMP_REG(DSI_PKT_SEQ_5_LO);
166 DUMP_REG(DSI_PKT_SEQ_5_HI);
167
168 DUMP_REG(DSI_DCS_CMDS);
169
170 DUMP_REG(DSI_PKT_LEN_0_1);
171 DUMP_REG(DSI_PKT_LEN_2_3);
172 DUMP_REG(DSI_PKT_LEN_4_5);
173 DUMP_REG(DSI_PKT_LEN_6_7);
174
175 DUMP_REG(DSI_PHY_TIMING_0);
176 DUMP_REG(DSI_PHY_TIMING_1);
177 DUMP_REG(DSI_PHY_TIMING_2);
178 DUMP_REG(DSI_BTA_TIMING);
179
180 DUMP_REG(DSI_TIMEOUT_0);
181 DUMP_REG(DSI_TIMEOUT_1);
182 DUMP_REG(DSI_TO_TALLY);
183
184 DUMP_REG(DSI_PAD_CONTROL_0);
185 DUMP_REG(DSI_PAD_CONTROL_CD);
186 DUMP_REG(DSI_PAD_CD_STATUS);
187 DUMP_REG(DSI_VIDEO_MODE_CONTROL);
188 DUMP_REG(DSI_PAD_CONTROL_1);
189 DUMP_REG(DSI_PAD_CONTROL_2);
190 DUMP_REG(DSI_PAD_CONTROL_3);
191 DUMP_REG(DSI_PAD_CONTROL_4);
192
193 DUMP_REG(DSI_GANGED_MODE_CONTROL);
194 DUMP_REG(DSI_GANGED_MODE_START);
195 DUMP_REG(DSI_GANGED_MODE_SIZE);
196
197 DUMP_REG(DSI_RAW_DATA_BYTE_COUNT);
198 DUMP_REG(DSI_ULTRA_LOW_POWER_CONTROL);
199
200 DUMP_REG(DSI_INIT_SEQ_DATA_8);
201 DUMP_REG(DSI_INIT_SEQ_DATA_9);
202 DUMP_REG(DSI_INIT_SEQ_DATA_10);
203 DUMP_REG(DSI_INIT_SEQ_DATA_11);
204 DUMP_REG(DSI_INIT_SEQ_DATA_12);
205 DUMP_REG(DSI_INIT_SEQ_DATA_13);
206 DUMP_REG(DSI_INIT_SEQ_DATA_14);
207 DUMP_REG(DSI_INIT_SEQ_DATA_15);
208
209#undef DUMP_REG
210
211 return 0;
212}
213
214static struct drm_info_list debugfs_files[] = {
215 { "regs", tegra_dsi_show_regs, 0, NULL },
216};
217
218static int tegra_dsi_debugfs_init(struct tegra_dsi *dsi,
219 struct drm_minor *minor)
220{
221 const char *name = dev_name(dsi->dev);
222 unsigned int i;
223 int err;
224
225 dsi->debugfs = debugfs_create_dir(name, minor->debugfs_root);
226 if (!dsi->debugfs)
227 return -ENOMEM;
228
229 dsi->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
230 GFP_KERNEL);
231 if (!dsi->debugfs_files) {
232 err = -ENOMEM;
233 goto remove;
234 }
235
236 for (i = 0; i < ARRAY_SIZE(debugfs_files); i++)
237 dsi->debugfs_files[i].data = dsi;
238
239 err = drm_debugfs_create_files(dsi->debugfs_files,
240 ARRAY_SIZE(debugfs_files),
241 dsi->debugfs, minor);
242 if (err < 0)
243 goto free;
244
245 dsi->minor = minor;
246
247 return 0;
248
249free:
250 kfree(dsi->debugfs_files);
251 dsi->debugfs_files = NULL;
252remove:
253 debugfs_remove(dsi->debugfs);
254 dsi->debugfs = NULL;
255
256 return err;
257}
258
Thierry Reding4009c222014-12-19 15:47:30 +0100259static void tegra_dsi_debugfs_exit(struct tegra_dsi *dsi)
Thierry Redingdec72732013-09-03 08:45:46 +0200260{
261 drm_debugfs_remove_files(dsi->debugfs_files, ARRAY_SIZE(debugfs_files),
262 dsi->minor);
263 dsi->minor = NULL;
264
265 kfree(dsi->debugfs_files);
266 dsi->debugfs_files = NULL;
267
268 debugfs_remove(dsi->debugfs);
269 dsi->debugfs = NULL;
Thierry Redingdec72732013-09-03 08:45:46 +0200270}
271
272#define PKT_ID0(id) ((((id) & 0x3f) << 3) | (1 << 9))
273#define PKT_LEN0(len) (((len) & 0x07) << 0)
274#define PKT_ID1(id) ((((id) & 0x3f) << 13) | (1 << 19))
275#define PKT_LEN1(len) (((len) & 0x07) << 10)
276#define PKT_ID2(id) ((((id) & 0x3f) << 23) | (1 << 29))
277#define PKT_LEN2(len) (((len) & 0x07) << 20)
278
279#define PKT_LP (1 << 30)
280#define NUM_PKT_SEQ 12
281
Thierry Reding17297a22014-03-14 14:13:15 +0100282/*
283 * non-burst mode with sync pulses
284 */
285static const u32 pkt_seq_video_non_burst_sync_pulses[NUM_PKT_SEQ] = {
Thierry Redingdec72732013-09-03 08:45:46 +0200286 [ 0] = PKT_ID0(MIPI_DSI_V_SYNC_START) | PKT_LEN0(0) |
287 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
288 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0) |
289 PKT_LP,
290 [ 1] = 0,
291 [ 2] = PKT_ID0(MIPI_DSI_V_SYNC_END) | PKT_LEN0(0) |
292 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
293 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0) |
294 PKT_LP,
295 [ 3] = 0,
296 [ 4] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
297 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
298 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0) |
299 PKT_LP,
300 [ 5] = 0,
301 [ 6] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
302 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
303 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0),
304 [ 7] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(2) |
305 PKT_ID1(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN1(3) |
306 PKT_ID2(MIPI_DSI_BLANKING_PACKET) | PKT_LEN2(4),
307 [ 8] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
308 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
309 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0) |
310 PKT_LP,
311 [ 9] = 0,
312 [10] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
313 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
314 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0),
315 [11] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(2) |
316 PKT_ID1(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN1(3) |
317 PKT_ID2(MIPI_DSI_BLANKING_PACKET) | PKT_LEN2(4),
318};
319
Thierry Reding17297a22014-03-14 14:13:15 +0100320/*
321 * non-burst mode with sync events
322 */
323static const u32 pkt_seq_video_non_burst_sync_events[NUM_PKT_SEQ] = {
324 [ 0] = PKT_ID0(MIPI_DSI_V_SYNC_START) | PKT_LEN0(0) |
325 PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION) | PKT_LEN1(7) |
326 PKT_LP,
327 [ 1] = 0,
328 [ 2] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
329 PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION) | PKT_LEN1(7) |
330 PKT_LP,
331 [ 3] = 0,
332 [ 4] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
333 PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION) | PKT_LEN1(7) |
334 PKT_LP,
335 [ 5] = 0,
336 [ 6] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
337 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(2) |
338 PKT_ID2(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN2(3),
339 [ 7] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(4),
340 [ 8] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
341 PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION) | PKT_LEN1(7) |
342 PKT_LP,
343 [ 9] = 0,
344 [10] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
345 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(2) |
346 PKT_ID2(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN2(3),
347 [11] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(4),
348};
349
Thierry Reding337b4432014-11-13 15:02:46 +0100350static const u32 pkt_seq_command_mode[NUM_PKT_SEQ] = {
351 [ 0] = 0,
352 [ 1] = 0,
353 [ 2] = 0,
354 [ 3] = 0,
355 [ 4] = 0,
356 [ 5] = 0,
357 [ 6] = PKT_ID0(MIPI_DSI_DCS_LONG_WRITE) | PKT_LEN0(3) | PKT_LP,
358 [ 7] = 0,
359 [ 8] = 0,
360 [ 9] = 0,
361 [10] = PKT_ID0(MIPI_DSI_DCS_LONG_WRITE) | PKT_LEN0(5) | PKT_LP,
362 [11] = 0,
363};
364
Thierry Redingebd14af2014-12-08 16:22:28 +0100365static void tegra_dsi_set_phy_timing(struct tegra_dsi *dsi,
366 unsigned long period,
367 const struct mipi_dphy_timing *timing)
Thierry Redingdec72732013-09-03 08:45:46 +0200368{
Thierry Reding9c0b4ca2014-11-24 12:27:59 +0100369 u32 value;
Thierry Redingdec72732013-09-03 08:45:46 +0200370
Thierry Redingebd14af2014-12-08 16:22:28 +0100371 value = DSI_TIMING_FIELD(timing->hsexit, period, 1) << 24 |
372 DSI_TIMING_FIELD(timing->hstrail, period, 0) << 16 |
373 DSI_TIMING_FIELD(timing->hszero, period, 3) << 8 |
374 DSI_TIMING_FIELD(timing->hsprepare, period, 1);
Thierry Redingdec72732013-09-03 08:45:46 +0200375 tegra_dsi_writel(dsi, value, DSI_PHY_TIMING_0);
376
Thierry Redingebd14af2014-12-08 16:22:28 +0100377 value = DSI_TIMING_FIELD(timing->clktrail, period, 1) << 24 |
378 DSI_TIMING_FIELD(timing->clkpost, period, 1) << 16 |
379 DSI_TIMING_FIELD(timing->clkzero, period, 1) << 8 |
380 DSI_TIMING_FIELD(timing->lpx, period, 1);
Thierry Redingdec72732013-09-03 08:45:46 +0200381 tegra_dsi_writel(dsi, value, DSI_PHY_TIMING_1);
382
Thierry Redingebd14af2014-12-08 16:22:28 +0100383 value = DSI_TIMING_FIELD(timing->clkprepare, period, 1) << 16 |
384 DSI_TIMING_FIELD(timing->clkpre, period, 1) << 8 |
Thierry Redingdec72732013-09-03 08:45:46 +0200385 DSI_TIMING_FIELD(0xff * period, period, 0) << 0;
386 tegra_dsi_writel(dsi, value, DSI_PHY_TIMING_2);
387
Thierry Redingebd14af2014-12-08 16:22:28 +0100388 value = DSI_TIMING_FIELD(timing->taget, period, 1) << 16 |
389 DSI_TIMING_FIELD(timing->tasure, period, 1) << 8 |
390 DSI_TIMING_FIELD(timing->tago, period, 1);
Thierry Redingdec72732013-09-03 08:45:46 +0200391 tegra_dsi_writel(dsi, value, DSI_BTA_TIMING);
392
Sean Paul7e3bc3a2014-10-07 16:04:42 +0200393 if (dsi->slave)
Thierry Redingebd14af2014-12-08 16:22:28 +0100394 tegra_dsi_set_phy_timing(dsi->slave, period, timing);
Thierry Redingdec72732013-09-03 08:45:46 +0200395}
396
397static int tegra_dsi_get_muldiv(enum mipi_dsi_pixel_format format,
398 unsigned int *mulp, unsigned int *divp)
399{
400 switch (format) {
401 case MIPI_DSI_FMT_RGB666_PACKED:
402 case MIPI_DSI_FMT_RGB888:
403 *mulp = 3;
404 *divp = 1;
405 break;
406
407 case MIPI_DSI_FMT_RGB565:
408 *mulp = 2;
409 *divp = 1;
410 break;
411
412 case MIPI_DSI_FMT_RGB666:
413 *mulp = 9;
414 *divp = 4;
415 break;
416
417 default:
418 return -EINVAL;
419 }
420
421 return 0;
422}
423
Thierry Redingf7d68892014-03-13 08:50:39 +0100424static int tegra_dsi_get_format(enum mipi_dsi_pixel_format format,
425 enum tegra_dsi_format *fmt)
426{
427 switch (format) {
428 case MIPI_DSI_FMT_RGB888:
429 *fmt = TEGRA_DSI_FORMAT_24P;
430 break;
431
432 case MIPI_DSI_FMT_RGB666:
433 *fmt = TEGRA_DSI_FORMAT_18NP;
434 break;
435
436 case MIPI_DSI_FMT_RGB666_PACKED:
437 *fmt = TEGRA_DSI_FORMAT_18P;
438 break;
439
440 case MIPI_DSI_FMT_RGB565:
441 *fmt = TEGRA_DSI_FORMAT_16P;
442 break;
443
444 default:
445 return -EINVAL;
446 }
447
448 return 0;
449}
450
Thierry Redinge94236c2014-10-07 16:10:24 +0200451static void tegra_dsi_ganged_enable(struct tegra_dsi *dsi, unsigned int start,
452 unsigned int size)
453{
454 u32 value;
455
456 tegra_dsi_writel(dsi, start, DSI_GANGED_MODE_START);
457 tegra_dsi_writel(dsi, size << 16 | size, DSI_GANGED_MODE_SIZE);
458
459 value = DSI_GANGED_MODE_CONTROL_ENABLE;
460 tegra_dsi_writel(dsi, value, DSI_GANGED_MODE_CONTROL);
461}
462
Thierry Reding563eff12014-11-13 14:44:27 +0100463static void tegra_dsi_enable(struct tegra_dsi *dsi)
Thierry Redingdec72732013-09-03 08:45:46 +0200464{
Thierry Reding563eff12014-11-13 14:44:27 +0100465 u32 value;
Thierry Redingdec72732013-09-03 08:45:46 +0200466
Thierry Reding563eff12014-11-13 14:44:27 +0100467 value = tegra_dsi_readl(dsi, DSI_POWER_CONTROL);
468 value |= DSI_POWER_CONTROL_ENABLE;
469 tegra_dsi_writel(dsi, value, DSI_POWER_CONTROL);
Thierry Redinge94236c2014-10-07 16:10:24 +0200470
471 if (dsi->slave)
472 tegra_dsi_enable(dsi->slave);
473}
474
475static unsigned int tegra_dsi_get_lanes(struct tegra_dsi *dsi)
476{
477 if (dsi->master)
478 return dsi->master->lanes + dsi->lanes;
479
480 if (dsi->slave)
481 return dsi->lanes + dsi->slave->lanes;
482
483 return dsi->lanes;
Thierry Reding563eff12014-11-13 14:44:27 +0100484}
485
Thierry Redingebd14af2014-12-08 16:22:28 +0100486static void tegra_dsi_configure(struct tegra_dsi *dsi, unsigned int pipe,
487 const struct drm_display_mode *mode)
Thierry Reding563eff12014-11-13 14:44:27 +0100488{
489 unsigned int hact, hsw, hbp, hfp, i, mul, div;
Thierry Redingebd14af2014-12-08 16:22:28 +0100490 struct tegra_dsi_state *state;
Thierry Reding563eff12014-11-13 14:44:27 +0100491 const u32 *pkt_seq;
492 u32 value;
Thierry Redingebd14af2014-12-08 16:22:28 +0100493
494 /* XXX: pass in state into this function? */
495 if (dsi->master)
496 state = tegra_dsi_get_state(dsi->master);
497 else
498 state = tegra_dsi_get_state(dsi);
499
500 mul = state->mul;
501 div = state->div;
Thierry Reding334ae6b2014-03-14 14:15:10 +0100502
Thierry Reding17297a22014-03-14 14:13:15 +0100503 if (dsi->flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE) {
504 DRM_DEBUG_KMS("Non-burst video mode with sync pulses\n");
505 pkt_seq = pkt_seq_video_non_burst_sync_pulses;
Thierry Reding337b4432014-11-13 15:02:46 +0100506 } else if (dsi->flags & MIPI_DSI_MODE_VIDEO) {
Thierry Reding17297a22014-03-14 14:13:15 +0100507 DRM_DEBUG_KMS("Non-burst video mode with sync events\n");
508 pkt_seq = pkt_seq_video_non_burst_sync_events;
Thierry Reding337b4432014-11-13 15:02:46 +0100509 } else {
510 DRM_DEBUG_KMS("Command mode\n");
511 pkt_seq = pkt_seq_command_mode;
Thierry Reding17297a22014-03-14 14:13:15 +0100512 }
513
Thierry Redingebd14af2014-12-08 16:22:28 +0100514 value = DSI_CONTROL_CHANNEL(0) |
515 DSI_CONTROL_FORMAT(state->format) |
Thierry Redingdec72732013-09-03 08:45:46 +0200516 DSI_CONTROL_LANES(dsi->lanes - 1) |
Thierry Reding563eff12014-11-13 14:44:27 +0100517 DSI_CONTROL_SOURCE(pipe);
Thierry Redingdec72732013-09-03 08:45:46 +0200518 tegra_dsi_writel(dsi, value, DSI_CONTROL);
519
Thierry Reding976cebc2014-08-06 09:14:28 +0200520 tegra_dsi_writel(dsi, dsi->video_fifo_depth, DSI_MAX_THRESHOLD);
Thierry Redingdec72732013-09-03 08:45:46 +0200521
Thierry Reding563eff12014-11-13 14:44:27 +0100522 value = DSI_HOST_CONTROL_HS;
Thierry Redingdec72732013-09-03 08:45:46 +0200523 tegra_dsi_writel(dsi, value, DSI_HOST_CONTROL);
524
525 value = tegra_dsi_readl(dsi, DSI_CONTROL);
Thierry Reding563eff12014-11-13 14:44:27 +0100526
Alexandre Courbot0c6b1e42014-07-08 21:32:13 +0900527 if (dsi->flags & MIPI_DSI_CLOCK_NON_CONTINUOUS)
528 value |= DSI_CONTROL_HS_CLK_CTRL;
Thierry Reding563eff12014-11-13 14:44:27 +0100529
Thierry Redingdec72732013-09-03 08:45:46 +0200530 value &= ~DSI_CONTROL_TX_TRIG(3);
Thierry Reding337b4432014-11-13 15:02:46 +0100531
532 /* enable DCS commands for command mode */
533 if (dsi->flags & MIPI_DSI_MODE_VIDEO)
534 value &= ~DSI_CONTROL_DCS_ENABLE;
535 else
536 value |= DSI_CONTROL_DCS_ENABLE;
537
Thierry Redingdec72732013-09-03 08:45:46 +0200538 value |= DSI_CONTROL_VIDEO_ENABLE;
539 value &= ~DSI_CONTROL_HOST_ENABLE;
540 tegra_dsi_writel(dsi, value, DSI_CONTROL);
541
Thierry Redingdec72732013-09-03 08:45:46 +0200542 for (i = 0; i < NUM_PKT_SEQ; i++)
543 tegra_dsi_writel(dsi, pkt_seq[i], DSI_PKT_SEQ_0_LO + i);
544
Thierry Reding337b4432014-11-13 15:02:46 +0100545 if (dsi->flags & MIPI_DSI_MODE_VIDEO) {
546 /* horizontal active pixels */
547 hact = mode->hdisplay * mul / div;
Thierry Redingdec72732013-09-03 08:45:46 +0200548
Thierry Reding337b4432014-11-13 15:02:46 +0100549 /* horizontal sync width */
550 hsw = (mode->hsync_end - mode->hsync_start) * mul / div;
551 hsw -= 10;
Thierry Redingdec72732013-09-03 08:45:46 +0200552
Thierry Reding337b4432014-11-13 15:02:46 +0100553 /* horizontal back porch */
554 hbp = (mode->htotal - mode->hsync_end) * mul / div;
555 hbp -= 14;
Thierry Redingdec72732013-09-03 08:45:46 +0200556
Thierry Reding337b4432014-11-13 15:02:46 +0100557 /* horizontal front porch */
558 hfp = (mode->hsync_start - mode->hdisplay) * mul / div;
559 hfp -= 8;
Thierry Redingdec72732013-09-03 08:45:46 +0200560
Thierry Reding337b4432014-11-13 15:02:46 +0100561 tegra_dsi_writel(dsi, hsw << 16 | 0, DSI_PKT_LEN_0_1);
562 tegra_dsi_writel(dsi, hact << 16 | hbp, DSI_PKT_LEN_2_3);
563 tegra_dsi_writel(dsi, hfp, DSI_PKT_LEN_4_5);
564 tegra_dsi_writel(dsi, 0x0f0f << 16, DSI_PKT_LEN_6_7);
Thierry Redingdec72732013-09-03 08:45:46 +0200565
Thierry Reding337b4432014-11-13 15:02:46 +0100566 /* set SOL delay (for non-burst mode only) */
567 tegra_dsi_writel(dsi, 8 * mul / div, DSI_SOL_DELAY);
Thierry Redinge94236c2014-10-07 16:10:24 +0200568
569 /* TODO: implement ganged mode */
Thierry Reding337b4432014-11-13 15:02:46 +0100570 } else {
571 u16 bytes;
572
Thierry Redinge94236c2014-10-07 16:10:24 +0200573 if (dsi->master || dsi->slave) {
574 /*
575 * For ganged mode, assume symmetric left-right mode.
576 */
577 bytes = 1 + (mode->hdisplay / 2) * mul / div;
578 } else {
579 /* 1 byte (DCS command) + pixel data */
580 bytes = 1 + mode->hdisplay * mul / div;
581 }
Thierry Reding337b4432014-11-13 15:02:46 +0100582
583 tegra_dsi_writel(dsi, 0, DSI_PKT_LEN_0_1);
584 tegra_dsi_writel(dsi, bytes << 16, DSI_PKT_LEN_2_3);
585 tegra_dsi_writel(dsi, bytes << 16, DSI_PKT_LEN_4_5);
586 tegra_dsi_writel(dsi, 0, DSI_PKT_LEN_6_7);
587
588 value = MIPI_DCS_WRITE_MEMORY_START << 8 |
589 MIPI_DCS_WRITE_MEMORY_CONTINUE;
590 tegra_dsi_writel(dsi, value, DSI_DCS_CMDS);
591
Thierry Redinge94236c2014-10-07 16:10:24 +0200592 /* set SOL delay */
593 if (dsi->master || dsi->slave) {
Thierry Redinge94236c2014-10-07 16:10:24 +0200594 unsigned long delay, bclk, bclk_ganged;
Thierry Redingebd14af2014-12-08 16:22:28 +0100595 unsigned int lanes = state->lanes;
Thierry Redinge94236c2014-10-07 16:10:24 +0200596
597 /* SOL to valid, valid to FIFO and FIFO write delay */
598 delay = 4 + 4 + 2;
599 delay = DIV_ROUND_UP(delay * mul, div * lanes);
600 /* FIFO read delay */
601 delay = delay + 6;
602
603 bclk = DIV_ROUND_UP(mode->htotal * mul, div * lanes);
604 bclk_ganged = DIV_ROUND_UP(bclk * lanes / 2, lanes);
605 value = bclk - bclk_ganged + delay + 20;
606 } else {
607 /* TODO: revisit for non-ganged mode */
608 value = 8 * mul / div;
609 }
Thierry Reding337b4432014-11-13 15:02:46 +0100610
611 tegra_dsi_writel(dsi, value, DSI_SOL_DELAY);
612 }
Thierry Redingdec72732013-09-03 08:45:46 +0200613
Thierry Redinge94236c2014-10-07 16:10:24 +0200614 if (dsi->slave) {
Thierry Redingebd14af2014-12-08 16:22:28 +0100615 tegra_dsi_configure(dsi->slave, pipe, mode);
Thierry Redinge94236c2014-10-07 16:10:24 +0200616
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 }
Thierry Reding563eff12014-11-13 14:44:27 +0100625}
626
Thierry Reding563eff12014-11-13 14:44:27 +0100627static int tegra_dsi_wait_idle(struct tegra_dsi *dsi, unsigned long timeout)
628{
629 u32 value;
630
631 timeout = jiffies + msecs_to_jiffies(timeout);
632
633 while (time_before(jiffies, timeout)) {
634 value = tegra_dsi_readl(dsi, DSI_STATUS);
635 if (value & DSI_STATUS_IDLE)
636 return 0;
637
638 usleep_range(1000, 2000);
639 }
640
641 return -ETIMEDOUT;
642}
643
644static void tegra_dsi_video_disable(struct tegra_dsi *dsi)
645{
646 u32 value;
647
648 value = tegra_dsi_readl(dsi, DSI_CONTROL);
649 value &= ~DSI_CONTROL_VIDEO_ENABLE;
650 tegra_dsi_writel(dsi, value, DSI_CONTROL);
Thierry Redinge94236c2014-10-07 16:10:24 +0200651
652 if (dsi->slave)
653 tegra_dsi_video_disable(dsi->slave);
654}
655
656static void tegra_dsi_ganged_disable(struct tegra_dsi *dsi)
657{
658 tegra_dsi_writel(dsi, 0, DSI_GANGED_MODE_START);
659 tegra_dsi_writel(dsi, 0, DSI_GANGED_MODE_SIZE);
660 tegra_dsi_writel(dsi, 0, DSI_GANGED_MODE_CONTROL);
Thierry Reding563eff12014-11-13 14:44:27 +0100661}
662
Thierry Reding5b901e72014-12-02 17:30:23 +0100663static void tegra_dsi_set_timeout(struct tegra_dsi *dsi, unsigned long bclk,
664 unsigned int vrefresh)
665{
666 unsigned int timeout;
667 u32 value;
668
669 /* one frame high-speed transmission timeout */
670 timeout = (bclk / vrefresh) / 512;
671 value = DSI_TIMEOUT_LRX(0x2000) | DSI_TIMEOUT_HTX(timeout);
672 tegra_dsi_writel(dsi, value, DSI_TIMEOUT_0);
673
674 /* 2 ms peripheral timeout for panel */
675 timeout = 2 * bclk / 512 * 1000;
676 value = DSI_TIMEOUT_PR(timeout) | DSI_TIMEOUT_TA(0x2000);
677 tegra_dsi_writel(dsi, value, DSI_TIMEOUT_1);
678
679 value = DSI_TALLY_TA(0) | DSI_TALLY_LRX(0) | DSI_TALLY_HTX(0);
680 tegra_dsi_writel(dsi, value, DSI_TO_TALLY);
681
682 if (dsi->slave)
683 tegra_dsi_set_timeout(dsi->slave, bclk, vrefresh);
684}
685
Thierry Reding563eff12014-11-13 14:44:27 +0100686static void tegra_dsi_disable(struct tegra_dsi *dsi)
687{
688 u32 value;
689
Thierry Redinge94236c2014-10-07 16:10:24 +0200690 if (dsi->slave) {
691 tegra_dsi_ganged_disable(dsi->slave);
692 tegra_dsi_ganged_disable(dsi);
693 }
694
Thierry Reding563eff12014-11-13 14:44:27 +0100695 value = tegra_dsi_readl(dsi, DSI_POWER_CONTROL);
696 value &= ~DSI_POWER_CONTROL_ENABLE;
697 tegra_dsi_writel(dsi, value, DSI_POWER_CONTROL);
698
Thierry Redinge94236c2014-10-07 16:10:24 +0200699 if (dsi->slave)
700 tegra_dsi_disable(dsi->slave);
701
Thierry Reding563eff12014-11-13 14:44:27 +0100702 usleep_range(5000, 10000);
703}
704
Thierry Reding92f0e072014-11-24 16:29:40 +0100705static void tegra_dsi_soft_reset(struct tegra_dsi *dsi)
706{
707 u32 value;
708
709 value = tegra_dsi_readl(dsi, DSI_POWER_CONTROL);
710 value &= ~DSI_POWER_CONTROL_ENABLE;
711 tegra_dsi_writel(dsi, value, DSI_POWER_CONTROL);
712
713 usleep_range(300, 1000);
714
715 value = tegra_dsi_readl(dsi, DSI_POWER_CONTROL);
716 value |= DSI_POWER_CONTROL_ENABLE;
717 tegra_dsi_writel(dsi, value, DSI_POWER_CONTROL);
718
719 usleep_range(300, 1000);
720
721 value = tegra_dsi_readl(dsi, DSI_TRIGGER);
722 if (value)
723 tegra_dsi_writel(dsi, 0, DSI_TRIGGER);
724
725 if (dsi->slave)
726 tegra_dsi_soft_reset(dsi->slave);
727}
728
Thierry Reding5b901e72014-12-02 17:30:23 +0100729static void tegra_dsi_connector_dpms(struct drm_connector *connector, int mode)
Thierry Redingdec72732013-09-03 08:45:46 +0200730{
Thierry Redingdec72732013-09-03 08:45:46 +0200731}
732
Thierry Redingebd14af2014-12-08 16:22:28 +0100733static void tegra_dsi_connector_reset(struct drm_connector *connector)
734{
735 struct tegra_dsi_state *state;
736
737 kfree(connector->state);
738 connector->state = NULL;
739
740 state = kzalloc(sizeof(*state), GFP_KERNEL);
741 if (state)
742 connector->state = &state->base;
743}
744
745static struct drm_connector_state *
746tegra_dsi_connector_duplicate_state(struct drm_connector *connector)
747{
748 struct tegra_dsi_state *state = to_dsi_state(connector->state);
749 struct tegra_dsi_state *copy;
750
751 copy = kmemdup(state, sizeof(*state), GFP_KERNEL);
752 if (!copy)
753 return NULL;
754
755 return &copy->base;
756}
757
Thierry Reding5b901e72014-12-02 17:30:23 +0100758static const struct drm_connector_funcs tegra_dsi_connector_funcs = {
759 .dpms = tegra_dsi_connector_dpms,
Thierry Redingebd14af2014-12-08 16:22:28 +0100760 .reset = tegra_dsi_connector_reset,
Thierry Reding5b901e72014-12-02 17:30:23 +0100761 .detect = tegra_output_connector_detect,
762 .fill_modes = drm_helper_probe_single_connector_modes,
763 .destroy = tegra_output_connector_destroy,
Thierry Redingebd14af2014-12-08 16:22:28 +0100764 .atomic_duplicate_state = tegra_dsi_connector_duplicate_state,
Thierry Reding4aa3df72014-11-24 16:27:13 +0100765 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
Thierry Reding5b901e72014-12-02 17:30:23 +0100766};
767
768static enum drm_mode_status
769tegra_dsi_connector_mode_valid(struct drm_connector *connector,
770 struct drm_display_mode *mode)
Thierry Reding3f6b4062014-11-13 14:50:33 +0100771{
Thierry Reding5b901e72014-12-02 17:30:23 +0100772 return MODE_OK;
Thierry Reding3f6b4062014-11-13 14:50:33 +0100773}
774
Thierry Reding5b901e72014-12-02 17:30:23 +0100775static const struct drm_connector_helper_funcs tegra_dsi_connector_helper_funcs = {
776 .get_modes = tegra_output_connector_get_modes,
777 .mode_valid = tegra_dsi_connector_mode_valid,
778 .best_encoder = tegra_output_connector_best_encoder,
779};
780
781static const struct drm_encoder_funcs tegra_dsi_encoder_funcs = {
782 .destroy = tegra_output_encoder_destroy,
783};
784
785static void tegra_dsi_encoder_dpms(struct drm_encoder *encoder, int mode)
Thierry Redingdec72732013-09-03 08:45:46 +0200786{
Thierry Reding5b901e72014-12-02 17:30:23 +0100787}
788
789static bool tegra_dsi_encoder_mode_fixup(struct drm_encoder *encoder,
790 const struct drm_display_mode *mode,
791 struct drm_display_mode *adjusted)
792{
793 struct tegra_output *output = encoder_to_output(encoder);
794 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
795 unsigned int mul, div, scdiv, vrefresh, lanes;
Thierry Redingdec72732013-09-03 08:45:46 +0200796 struct tegra_dsi *dsi = to_dsi(output);
Thierry Redingebd14af2014-12-08 16:22:28 +0100797 struct mipi_dphy_timing timing;
Thierry Reding5b901e72014-12-02 17:30:23 +0100798 unsigned long pclk, bclk, plld;
Thierry Redingebd14af2014-12-08 16:22:28 +0100799 unsigned long period;
Thierry Redingdec72732013-09-03 08:45:46 +0200800 int err;
801
Thierry Redinge94236c2014-10-07 16:10:24 +0200802 lanes = tegra_dsi_get_lanes(dsi);
Thierry Reding5b901e72014-12-02 17:30:23 +0100803 pclk = mode->clock * 1000;
Thierry Redinge94236c2014-10-07 16:10:24 +0200804
Thierry Redingdec72732013-09-03 08:45:46 +0200805 err = tegra_dsi_get_muldiv(dsi->format, &mul, &div);
806 if (err < 0)
807 return err;
808
Thierry Redinge94236c2014-10-07 16:10:24 +0200809 DRM_DEBUG_KMS("mul: %u, div: %u, lanes: %u\n", mul, div, lanes);
Thierry Redingdec72732013-09-03 08:45:46 +0200810 vrefresh = drm_mode_vrefresh(mode);
Thierry Reding91eded92014-03-26 13:32:21 +0100811 DRM_DEBUG_KMS("vrefresh: %u\n", vrefresh);
Thierry Redingdec72732013-09-03 08:45:46 +0200812
Thierry Reding91eded92014-03-26 13:32:21 +0100813 /* compute byte clock */
Thierry Redinge94236c2014-10-07 16:10:24 +0200814 bclk = (pclk * mul) / (div * lanes);
Thierry Reding91eded92014-03-26 13:32:21 +0100815
816 /*
817 * Compute bit clock and round up to the next MHz.
818 */
Thierry Reding030611e2014-11-13 14:32:06 +0100819 plld = DIV_ROUND_UP(bclk * 8, USEC_PER_SEC) * USEC_PER_SEC;
Thierry Redingebd14af2014-12-08 16:22:28 +0100820 period = DIV_ROUND_CLOSEST(NSEC_PER_SEC, plld);
Thierry Reding91eded92014-03-26 13:32:21 +0100821
822 /*
823 * We divide the frequency by two here, but we make up for that by
824 * setting the shift clock divider (further below) to half of the
825 * correct value.
826 */
827 plld /= 2;
Thierry Redingdec72732013-09-03 08:45:46 +0200828
Thierry Redingdec72732013-09-03 08:45:46 +0200829 /*
Thierry Reding91eded92014-03-26 13:32:21 +0100830 * Derive pixel clock from bit clock using the shift clock divider.
831 * Note that this is only half of what we would expect, but we need
832 * that to make up for the fact that we divided the bit clock by a
833 * factor of two above.
834 *
835 * It's not clear exactly why this is necessary, but the display is
836 * not working properly otherwise. Perhaps the PLLs cannot generate
837 * frequencies sufficiently high.
838 */
Thierry Reding5b901e72014-12-02 17:30:23 +0100839 scdiv = ((8 * mul) / (div * lanes)) - 2;
Thierry Reding91eded92014-03-26 13:32:21 +0100840
Thierry Reding5b901e72014-12-02 17:30:23 +0100841 err = tegra_dc_setup_clock(dc, dsi->clk_parent, plld, scdiv);
842 if (err < 0) {
843 dev_err(output->dev, "failed to setup DC clock: %d\n", err);
844 return false;
845 }
846
847 err = clk_set_rate(dsi->clk_parent, plld);
848 if (err < 0) {
849 dev_err(dsi->dev, "failed to set clock rate to %lu Hz\n",
850 plld);
851 return false;
852 }
853
Thierry Reding3f6b4062014-11-13 14:50:33 +0100854 tegra_dsi_set_timeout(dsi, bclk, vrefresh);
Thierry Redingdec72732013-09-03 08:45:46 +0200855
Thierry Redingebd14af2014-12-08 16:22:28 +0100856 err = mipi_dphy_timing_get_default(&timing, period);
857 if (err < 0)
858 return err;
859
860 err = mipi_dphy_timing_validate(&timing, period);
Thierry Reding5b901e72014-12-02 17:30:23 +0100861 if (err < 0) {
Thierry Redingebd14af2014-12-08 16:22:28 +0100862 dev_err(dsi->dev, "failed to validate D-PHY timing: %d\n", err);
863 return err;
Thierry Reding5b901e72014-12-02 17:30:23 +0100864 }
Sean Paul7e3bc3a2014-10-07 16:04:42 +0200865
Thierry Redingebd14af2014-12-08 16:22:28 +0100866 /*
867 * The D-PHY timing fields are expressed in byte-clock cycles, so
868 * multiply the period by 8.
869 */
870 tegra_dsi_set_phy_timing(dsi, period * 8, &timing);
871
Thierry Reding5b901e72014-12-02 17:30:23 +0100872 return true;
Thierry Redingdec72732013-09-03 08:45:46 +0200873}
874
Thierry Reding5b901e72014-12-02 17:30:23 +0100875static void tegra_dsi_encoder_prepare(struct drm_encoder *encoder)
Thierry Redingdec72732013-09-03 08:45:46 +0200876{
Thierry Redingdec72732013-09-03 08:45:46 +0200877}
878
Thierry Reding5b901e72014-12-02 17:30:23 +0100879static void tegra_dsi_encoder_commit(struct drm_encoder *encoder)
880{
881}
882
883static void tegra_dsi_encoder_mode_set(struct drm_encoder *encoder,
884 struct drm_display_mode *mode,
885 struct drm_display_mode *adjusted)
886{
887 struct tegra_output *output = encoder_to_output(encoder);
888 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
889 struct tegra_dsi *dsi = to_dsi(output);
Thierry Redingebd14af2014-12-08 16:22:28 +0100890 struct tegra_dsi_state *state;
Thierry Reding5b901e72014-12-02 17:30:23 +0100891 u32 value;
Thierry Reding5b901e72014-12-02 17:30:23 +0100892
Thierry Redingebd14af2014-12-08 16:22:28 +0100893 state = tegra_dsi_get_state(dsi);
Thierry Reding5b901e72014-12-02 17:30:23 +0100894
Thierry Redingebd14af2014-12-08 16:22:28 +0100895 tegra_dsi_set_timeout(dsi, state->bclk, state->vrefresh);
896
897 /*
898 * The D-PHY timing fields are expressed in byte-clock cycles, so
899 * multiply the period by 8.
900 */
901 tegra_dsi_set_phy_timing(dsi, state->period * 8, &state->timing);
Thierry Reding5b901e72014-12-02 17:30:23 +0100902
903 if (output->panel)
904 drm_panel_prepare(output->panel);
905
Thierry Redingebd14af2014-12-08 16:22:28 +0100906 tegra_dsi_configure(dsi, dc->pipe, mode);
907
Thierry Reding5b901e72014-12-02 17:30:23 +0100908 /* enable display controller */
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 value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
914 value &= ~DISP_CTRL_MODE_MASK;
915 value |= DISP_CTRL_MODE_C_DISPLAY;
916 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
917
918 value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
919 value |= PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
920 PW4_ENABLE | PM0_ENABLE | PM1_ENABLE;
921 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
922
923 tegra_dc_commit(dc);
924
925 /* enable DSI controller */
926 tegra_dsi_enable(dsi);
927
928 if (output->panel)
929 drm_panel_enable(output->panel);
930
931 return;
932}
933
934static void tegra_dsi_encoder_disable(struct drm_encoder *encoder)
935{
936 struct tegra_output *output = encoder_to_output(encoder);
937 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
938 struct tegra_dsi *dsi = to_dsi(output);
939 u32 value;
940 int err;
941
942 if (output->panel)
943 drm_panel_disable(output->panel);
944
945 tegra_dsi_video_disable(dsi);
946
947 /*
948 * The following accesses registers of the display controller, so make
949 * sure it's only executed when the output is attached to one.
950 */
951 if (dc) {
952 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
953 value &= ~DSI_ENABLE;
954 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
955
956 tegra_dc_commit(dc);
957 }
958
959 err = tegra_dsi_wait_idle(dsi, 100);
960 if (err < 0)
961 dev_dbg(dsi->dev, "failed to idle DSI: %d\n", err);
962
963 tegra_dsi_soft_reset(dsi);
964
965 if (output->panel)
966 drm_panel_unprepare(output->panel);
967
968 tegra_dsi_disable(dsi);
969
970 return;
971}
972
Thierry Redingebd14af2014-12-08 16:22:28 +0100973static int
974tegra_dsi_encoder_atomic_check(struct drm_encoder *encoder,
975 struct drm_crtc_state *crtc_state,
976 struct drm_connector_state *conn_state)
977{
978 struct tegra_output *output = encoder_to_output(encoder);
979 struct tegra_dsi_state *state = to_dsi_state(conn_state);
980 struct tegra_dc *dc = to_tegra_dc(conn_state->crtc);
981 struct tegra_dsi *dsi = to_dsi(output);
982 unsigned int scdiv;
983 unsigned long plld;
984 int err;
985
986 state->pclk = crtc_state->mode.clock * 1000;
987
988 err = tegra_dsi_get_muldiv(dsi->format, &state->mul, &state->div);
989 if (err < 0)
990 return err;
991
992 state->lanes = tegra_dsi_get_lanes(dsi);
993
994 err = tegra_dsi_get_format(dsi->format, &state->format);
995 if (err < 0)
996 return err;
997
998 state->vrefresh = drm_mode_vrefresh(&crtc_state->mode);
999
1000 /* compute byte clock */
1001 state->bclk = (state->pclk * state->mul) / (state->div * state->lanes);
1002
1003 DRM_DEBUG_KMS("mul: %u, div: %u, lanes: %u\n", state->mul, state->div,
1004 state->lanes);
1005 DRM_DEBUG_KMS("format: %u, vrefresh: %u\n", state->format,
1006 state->vrefresh);
1007 DRM_DEBUG_KMS("bclk: %lu\n", state->bclk);
1008
1009 /*
1010 * Compute bit clock and round up to the next MHz.
1011 */
1012 plld = DIV_ROUND_UP(state->bclk * 8, USEC_PER_SEC) * USEC_PER_SEC;
1013 state->period = DIV_ROUND_CLOSEST(NSEC_PER_SEC, plld);
1014
1015 err = mipi_dphy_timing_get_default(&state->timing, state->period);
1016 if (err < 0)
1017 return err;
1018
1019 err = mipi_dphy_timing_validate(&state->timing, state->period);
1020 if (err < 0) {
1021 dev_err(dsi->dev, "failed to validate D-PHY timing: %d\n", err);
1022 return err;
1023 }
1024
1025 /*
1026 * We divide the frequency by two here, but we make up for that by
1027 * setting the shift clock divider (further below) to half of the
1028 * correct value.
1029 */
1030 plld /= 2;
1031
1032 /*
1033 * Derive pixel clock from bit clock using the shift clock divider.
1034 * Note that this is only half of what we would expect, but we need
1035 * that to make up for the fact that we divided the bit clock by a
1036 * factor of two above.
1037 *
1038 * It's not clear exactly why this is necessary, but the display is
1039 * not working properly otherwise. Perhaps the PLLs cannot generate
1040 * frequencies sufficiently high.
1041 */
1042 scdiv = ((8 * state->mul) / (state->div * state->lanes)) - 2;
1043
1044 err = tegra_dc_state_setup_clock(dc, crtc_state, dsi->clk_parent,
1045 plld, scdiv);
1046 if (err < 0) {
1047 dev_err(output->dev, "failed to setup CRTC state: %d\n", err);
1048 return err;
1049 }
1050
1051 return err;
1052}
1053
Thierry Reding5b901e72014-12-02 17:30:23 +01001054static const struct drm_encoder_helper_funcs tegra_dsi_encoder_helper_funcs = {
1055 .dpms = tegra_dsi_encoder_dpms,
1056 .mode_fixup = tegra_dsi_encoder_mode_fixup,
1057 .prepare = tegra_dsi_encoder_prepare,
1058 .commit = tegra_dsi_encoder_commit,
1059 .mode_set = tegra_dsi_encoder_mode_set,
1060 .disable = tegra_dsi_encoder_disable,
Thierry Redingebd14af2014-12-08 16:22:28 +01001061 .atomic_check = tegra_dsi_encoder_atomic_check,
Thierry Redingdec72732013-09-03 08:45:46 +02001062};
1063
1064static int tegra_dsi_pad_enable(struct tegra_dsi *dsi)
1065{
Thierry Reding9c0b4ca2014-11-24 12:27:59 +01001066 u32 value;
Thierry Redingdec72732013-09-03 08:45:46 +02001067
1068 value = DSI_PAD_CONTROL_VS1_PULLDN(0) | DSI_PAD_CONTROL_VS1_PDIO(0);
1069 tegra_dsi_writel(dsi, value, DSI_PAD_CONTROL_0);
1070
1071 return 0;
1072}
1073
1074static int tegra_dsi_pad_calibrate(struct tegra_dsi *dsi)
1075{
Thierry Reding183ef282014-11-13 14:27:29 +01001076 u32 value;
Thierry Redingdec72732013-09-03 08:45:46 +02001077
1078 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_0);
1079 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_1);
1080 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_2);
1081 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_3);
1082 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_4);
1083
1084 /* start calibration */
1085 tegra_dsi_pad_enable(dsi);
1086
1087 value = DSI_PAD_SLEW_UP(0x7) | DSI_PAD_SLEW_DN(0x7) |
1088 DSI_PAD_LP_UP(0x1) | DSI_PAD_LP_DN(0x1) |
1089 DSI_PAD_OUT_CLK(0x0);
1090 tegra_dsi_writel(dsi, value, DSI_PAD_CONTROL_2);
1091
1092 return tegra_mipi_calibrate(dsi->mipi);
1093}
1094
1095static int tegra_dsi_init(struct host1x_client *client)
1096{
Thierry Reding9910f5c2014-05-22 09:57:15 +02001097 struct drm_device *drm = dev_get_drvdata(client->parent);
Thierry Redingdec72732013-09-03 08:45:46 +02001098 struct tegra_dsi *dsi = host1x_client_to_dsi(client);
Thierry Redingdec72732013-09-03 08:45:46 +02001099 int err;
1100
Thierry Reding201106d2014-11-24 16:31:48 +01001101 reset_control_deassert(dsi->rst);
1102
1103 err = tegra_dsi_pad_calibrate(dsi);
1104 if (err < 0) {
1105 dev_err(dsi->dev, "MIPI calibration failed: %d\n", err);
1106 goto reset;
1107 }
1108
Thierry Redinge94236c2014-10-07 16:10:24 +02001109 /* Gangsters must not register their own outputs. */
1110 if (!dsi->master) {
Thierry Redinge94236c2014-10-07 16:10:24 +02001111 dsi->output.dev = client->dev;
Thierry Redingdec72732013-09-03 08:45:46 +02001112
Thierry Reding5b901e72014-12-02 17:30:23 +01001113 drm_connector_init(drm, &dsi->output.connector,
1114 &tegra_dsi_connector_funcs,
1115 DRM_MODE_CONNECTOR_DSI);
1116 drm_connector_helper_add(&dsi->output.connector,
1117 &tegra_dsi_connector_helper_funcs);
1118 dsi->output.connector.dpms = DRM_MODE_DPMS_OFF;
1119
Thierry Reding5b901e72014-12-02 17:30:23 +01001120 drm_encoder_init(drm, &dsi->output.encoder,
1121 &tegra_dsi_encoder_funcs,
1122 DRM_MODE_ENCODER_DSI);
1123 drm_encoder_helper_add(&dsi->output.encoder,
1124 &tegra_dsi_encoder_helper_funcs);
1125
1126 drm_mode_connector_attach_encoder(&dsi->output.connector,
1127 &dsi->output.encoder);
1128 drm_connector_register(&dsi->output.connector);
1129
Thierry Redingea130b22014-12-19 15:51:35 +01001130 err = tegra_output_init(drm, &dsi->output);
1131 if (err < 0) {
1132 dev_err(client->dev,
1133 "failed to initialize output: %d\n",
1134 err);
1135 goto reset;
1136 }
1137
Thierry Reding5b901e72014-12-02 17:30:23 +01001138 dsi->output.encoder.possible_crtcs = 0x3;
Thierry Redingdec72732013-09-03 08:45:46 +02001139 }
1140
1141 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
Thierry Reding9910f5c2014-05-22 09:57:15 +02001142 err = tegra_dsi_debugfs_init(dsi, drm->primary);
Thierry Redingdec72732013-09-03 08:45:46 +02001143 if (err < 0)
1144 dev_err(dsi->dev, "debugfs setup failed: %d\n", err);
1145 }
1146
Thierry Redingdec72732013-09-03 08:45:46 +02001147 return 0;
Thierry Reding201106d2014-11-24 16:31:48 +01001148
1149reset:
1150 reset_control_assert(dsi->rst);
1151 return err;
Thierry Redingdec72732013-09-03 08:45:46 +02001152}
1153
1154static int tegra_dsi_exit(struct host1x_client *client)
1155{
1156 struct tegra_dsi *dsi = host1x_client_to_dsi(client);
Thierry Redingdec72732013-09-03 08:45:46 +02001157
Thierry Reding5b901e72014-12-02 17:30:23 +01001158 tegra_output_exit(&dsi->output);
1159
Thierry Reding4009c222014-12-19 15:47:30 +01001160 if (IS_ENABLED(CONFIG_DEBUG_FS))
1161 tegra_dsi_debugfs_exit(dsi);
Thierry Redingdec72732013-09-03 08:45:46 +02001162
Thierry Reding201106d2014-11-24 16:31:48 +01001163 reset_control_assert(dsi->rst);
1164
Thierry Redingdec72732013-09-03 08:45:46 +02001165 return 0;
1166}
1167
1168static const struct host1x_client_ops dsi_client_ops = {
1169 .init = tegra_dsi_init,
1170 .exit = tegra_dsi_exit,
1171};
1172
1173static int tegra_dsi_setup_clocks(struct tegra_dsi *dsi)
1174{
1175 struct clk *parent;
1176 int err;
1177
1178 parent = clk_get_parent(dsi->clk);
1179 if (!parent)
1180 return -EINVAL;
1181
1182 err = clk_set_parent(parent, dsi->clk_parent);
1183 if (err < 0)
1184 return err;
1185
1186 return 0;
1187}
1188
Thierry Reding0fffdf62014-11-07 17:25:26 +01001189static const char * const error_report[16] = {
1190 "SoT Error",
1191 "SoT Sync Error",
1192 "EoT Sync Error",
1193 "Escape Mode Entry Command Error",
1194 "Low-Power Transmit Sync Error",
1195 "Peripheral Timeout Error",
1196 "False Control Error",
1197 "Contention Detected",
1198 "ECC Error, single-bit",
1199 "ECC Error, multi-bit",
1200 "Checksum Error",
1201 "DSI Data Type Not Recognized",
1202 "DSI VC ID Invalid",
1203 "Invalid Transmission Length",
1204 "Reserved",
1205 "DSI Protocol Violation",
1206};
1207
1208static ssize_t tegra_dsi_read_response(struct tegra_dsi *dsi,
1209 const struct mipi_dsi_msg *msg,
1210 size_t count)
1211{
1212 u8 *rx = msg->rx_buf;
1213 unsigned int i, j, k;
1214 size_t size = 0;
1215 u16 errors;
1216 u32 value;
1217
1218 /* read and parse packet header */
1219 value = tegra_dsi_readl(dsi, DSI_RD_DATA);
1220
1221 switch (value & 0x3f) {
1222 case MIPI_DSI_RX_ACKNOWLEDGE_AND_ERROR_REPORT:
1223 errors = (value >> 8) & 0xffff;
1224 dev_dbg(dsi->dev, "Acknowledge and error report: %04x\n",
1225 errors);
1226 for (i = 0; i < ARRAY_SIZE(error_report); i++)
1227 if (errors & BIT(i))
1228 dev_dbg(dsi->dev, " %2u: %s\n", i,
1229 error_report[i]);
1230 break;
1231
1232 case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_1BYTE:
1233 rx[0] = (value >> 8) & 0xff;
1234 size = 1;
1235 break;
1236
1237 case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_2BYTE:
1238 rx[0] = (value >> 8) & 0xff;
1239 rx[1] = (value >> 16) & 0xff;
1240 size = 2;
1241 break;
1242
1243 case MIPI_DSI_RX_DCS_LONG_READ_RESPONSE:
1244 size = ((value >> 8) & 0xff00) | ((value >> 8) & 0xff);
1245 break;
1246
1247 case MIPI_DSI_RX_GENERIC_LONG_READ_RESPONSE:
1248 size = ((value >> 8) & 0xff00) | ((value >> 8) & 0xff);
1249 break;
1250
1251 default:
1252 dev_err(dsi->dev, "unhandled response type: %02x\n",
1253 value & 0x3f);
1254 return -EPROTO;
1255 }
1256
1257 size = min(size, msg->rx_len);
1258
1259 if (msg->rx_buf && size > 0) {
1260 for (i = 0, j = 0; i < count - 1; i++, j += 4) {
1261 u8 *rx = msg->rx_buf + j;
1262
1263 value = tegra_dsi_readl(dsi, DSI_RD_DATA);
1264
1265 for (k = 0; k < 4 && (j + k) < msg->rx_len; k++)
1266 rx[j + k] = (value >> (k << 3)) & 0xff;
1267 }
1268 }
1269
1270 return size;
1271}
1272
1273static int tegra_dsi_transmit(struct tegra_dsi *dsi, unsigned long timeout)
1274{
1275 tegra_dsi_writel(dsi, DSI_TRIGGER_HOST, DSI_TRIGGER);
1276
1277 timeout = jiffies + msecs_to_jiffies(timeout);
1278
1279 while (time_before(jiffies, timeout)) {
1280 u32 value = tegra_dsi_readl(dsi, DSI_TRIGGER);
1281 if ((value & DSI_TRIGGER_HOST) == 0)
1282 return 0;
1283
1284 usleep_range(1000, 2000);
1285 }
1286
1287 DRM_DEBUG_KMS("timeout waiting for transmission to complete\n");
1288 return -ETIMEDOUT;
1289}
1290
1291static int tegra_dsi_wait_for_response(struct tegra_dsi *dsi,
1292 unsigned long timeout)
1293{
1294 timeout = jiffies + msecs_to_jiffies(250);
1295
1296 while (time_before(jiffies, timeout)) {
1297 u32 value = tegra_dsi_readl(dsi, DSI_STATUS);
1298 u8 count = value & 0x1f;
1299
1300 if (count > 0)
1301 return count;
1302
1303 usleep_range(1000, 2000);
1304 }
1305
1306 DRM_DEBUG_KMS("peripheral returned no data\n");
1307 return -ETIMEDOUT;
1308}
1309
1310static void tegra_dsi_writesl(struct tegra_dsi *dsi, unsigned long offset,
1311 const void *buffer, size_t size)
1312{
1313 const u8 *buf = buffer;
1314 size_t i, j;
1315 u32 value;
1316
1317 for (j = 0; j < size; j += 4) {
1318 value = 0;
1319
1320 for (i = 0; i < 4 && j + i < size; i++)
1321 value |= buf[j + i] << (i << 3);
1322
1323 tegra_dsi_writel(dsi, value, DSI_WR_DATA);
1324 }
1325}
1326
1327static ssize_t tegra_dsi_host_transfer(struct mipi_dsi_host *host,
1328 const struct mipi_dsi_msg *msg)
1329{
1330 struct tegra_dsi *dsi = host_to_tegra(host);
1331 struct mipi_dsi_packet packet;
1332 const u8 *header;
1333 size_t count;
1334 ssize_t err;
1335 u32 value;
1336
1337 err = mipi_dsi_create_packet(&packet, msg);
1338 if (err < 0)
1339 return err;
1340
1341 header = packet.header;
1342
1343 /* maximum FIFO depth is 1920 words */
1344 if (packet.size > dsi->video_fifo_depth * 4)
1345 return -ENOSPC;
1346
1347 /* reset underflow/overflow flags */
1348 value = tegra_dsi_readl(dsi, DSI_STATUS);
1349 if (value & (DSI_STATUS_UNDERFLOW | DSI_STATUS_OVERFLOW)) {
1350 value = DSI_HOST_CONTROL_FIFO_RESET;
1351 tegra_dsi_writel(dsi, value, DSI_HOST_CONTROL);
1352 usleep_range(10, 20);
1353 }
1354
1355 value = tegra_dsi_readl(dsi, DSI_POWER_CONTROL);
1356 value |= DSI_POWER_CONTROL_ENABLE;
1357 tegra_dsi_writel(dsi, value, DSI_POWER_CONTROL);
1358
1359 usleep_range(5000, 10000);
1360
1361 value = DSI_HOST_CONTROL_CRC_RESET | DSI_HOST_CONTROL_TX_TRIG_HOST |
1362 DSI_HOST_CONTROL_CS | DSI_HOST_CONTROL_ECC;
1363
1364 if ((msg->flags & MIPI_DSI_MSG_USE_LPM) == 0)
1365 value |= DSI_HOST_CONTROL_HS;
1366
1367 /*
1368 * The host FIFO has a maximum of 64 words, so larger transmissions
1369 * need to use the video FIFO.
1370 */
1371 if (packet.size > dsi->host_fifo_depth * 4)
1372 value |= DSI_HOST_CONTROL_FIFO_SEL;
1373
1374 tegra_dsi_writel(dsi, value, DSI_HOST_CONTROL);
1375
1376 /*
1377 * For reads and messages with explicitly requested ACK, generate a
1378 * BTA sequence after the transmission of the packet.
1379 */
1380 if ((msg->flags & MIPI_DSI_MSG_REQ_ACK) ||
1381 (msg->rx_buf && msg->rx_len > 0)) {
1382 value = tegra_dsi_readl(dsi, DSI_HOST_CONTROL);
1383 value |= DSI_HOST_CONTROL_PKT_BTA;
1384 tegra_dsi_writel(dsi, value, DSI_HOST_CONTROL);
1385 }
1386
1387 value = DSI_CONTROL_LANES(0) | DSI_CONTROL_HOST_ENABLE;
1388 tegra_dsi_writel(dsi, value, DSI_CONTROL);
1389
1390 /* write packet header, ECC is generated by hardware */
1391 value = header[2] << 16 | header[1] << 8 | header[0];
1392 tegra_dsi_writel(dsi, value, DSI_WR_DATA);
1393
1394 /* write payload (if any) */
1395 if (packet.payload_length > 0)
1396 tegra_dsi_writesl(dsi, DSI_WR_DATA, packet.payload,
1397 packet.payload_length);
1398
1399 err = tegra_dsi_transmit(dsi, 250);
1400 if (err < 0)
1401 return err;
1402
1403 if ((msg->flags & MIPI_DSI_MSG_REQ_ACK) ||
1404 (msg->rx_buf && msg->rx_len > 0)) {
1405 err = tegra_dsi_wait_for_response(dsi, 250);
1406 if (err < 0)
1407 return err;
1408
1409 count = err;
1410
1411 value = tegra_dsi_readl(dsi, DSI_RD_DATA);
1412 switch (value) {
1413 case 0x84:
1414 /*
1415 dev_dbg(dsi->dev, "ACK\n");
1416 */
1417 break;
1418
1419 case 0x87:
1420 /*
1421 dev_dbg(dsi->dev, "ESCAPE\n");
1422 */
1423 break;
1424
1425 default:
1426 dev_err(dsi->dev, "unknown status: %08x\n", value);
1427 break;
1428 }
1429
1430 if (count > 1) {
1431 err = tegra_dsi_read_response(dsi, msg, count);
1432 if (err < 0)
1433 dev_err(dsi->dev,
1434 "failed to parse response: %zd\n",
1435 err);
1436 else {
1437 /*
1438 * For read commands, return the number of
1439 * bytes returned by the peripheral.
1440 */
1441 count = err;
1442 }
1443 }
1444 } else {
1445 /*
1446 * For write commands, we have transmitted the 4-byte header
1447 * plus the variable-length payload.
1448 */
1449 count = 4 + packet.payload_length;
1450 }
1451
1452 return count;
1453}
1454
Thierry Redinge94236c2014-10-07 16:10:24 +02001455static int tegra_dsi_ganged_setup(struct tegra_dsi *dsi)
1456{
1457 struct clk *parent;
1458 int err;
1459
1460 /* make sure both DSI controllers share the same PLL */
1461 parent = clk_get_parent(dsi->slave->clk);
1462 if (!parent)
1463 return -EINVAL;
1464
1465 err = clk_set_parent(parent, dsi->clk_parent);
1466 if (err < 0)
1467 return err;
1468
1469 return 0;
1470}
1471
Thierry Redingdec72732013-09-03 08:45:46 +02001472static int tegra_dsi_host_attach(struct mipi_dsi_host *host,
1473 struct mipi_dsi_device *device)
1474{
1475 struct tegra_dsi *dsi = host_to_tegra(host);
Thierry Redingdec72732013-09-03 08:45:46 +02001476
Thierry Reding17297a22014-03-14 14:13:15 +01001477 dsi->flags = device->mode_flags;
Thierry Redingdec72732013-09-03 08:45:46 +02001478 dsi->format = device->format;
1479 dsi->lanes = device->lanes;
1480
Thierry Redinge94236c2014-10-07 16:10:24 +02001481 if (dsi->slave) {
1482 int err;
1483
1484 dev_dbg(dsi->dev, "attaching dual-channel device %s\n",
1485 dev_name(&device->dev));
1486
1487 err = tegra_dsi_ganged_setup(dsi);
1488 if (err < 0) {
1489 dev_err(dsi->dev, "failed to set up ganged mode: %d\n",
1490 err);
1491 return err;
1492 }
1493 }
1494
1495 /*
1496 * Slaves don't have a panel associated with them, so they provide
1497 * merely the second channel.
1498 */
1499 if (!dsi->master) {
1500 struct tegra_output *output = &dsi->output;
1501
1502 output->panel = of_drm_find_panel(device->dev.of_node);
1503 if (output->panel && output->connector.dev) {
1504 drm_panel_attach(output->panel, &output->connector);
Thierry Redingdec72732013-09-03 08:45:46 +02001505 drm_helper_hpd_irq_event(output->connector.dev);
Thierry Redinge94236c2014-10-07 16:10:24 +02001506 }
Thierry Redingdec72732013-09-03 08:45:46 +02001507 }
1508
1509 return 0;
1510}
1511
1512static int tegra_dsi_host_detach(struct mipi_dsi_host *host,
1513 struct mipi_dsi_device *device)
1514{
1515 struct tegra_dsi *dsi = host_to_tegra(host);
1516 struct tegra_output *output = &dsi->output;
1517
1518 if (output->panel && &device->dev == output->panel->dev) {
Thierry Redingba3df972014-11-13 14:54:01 +01001519 output->panel = NULL;
1520
Thierry Redingdec72732013-09-03 08:45:46 +02001521 if (output->connector.dev)
1522 drm_helper_hpd_irq_event(output->connector.dev);
Thierry Redingdec72732013-09-03 08:45:46 +02001523 }
1524
1525 return 0;
1526}
1527
1528static const struct mipi_dsi_host_ops tegra_dsi_host_ops = {
1529 .attach = tegra_dsi_host_attach,
1530 .detach = tegra_dsi_host_detach,
Thierry Reding0fffdf62014-11-07 17:25:26 +01001531 .transfer = tegra_dsi_host_transfer,
Thierry Redingdec72732013-09-03 08:45:46 +02001532};
1533
Thierry Redinge94236c2014-10-07 16:10:24 +02001534static int tegra_dsi_ganged_probe(struct tegra_dsi *dsi)
1535{
1536 struct device_node *np;
1537
1538 np = of_parse_phandle(dsi->dev->of_node, "nvidia,ganged-mode", 0);
1539 if (np) {
1540 struct platform_device *gangster = of_find_device_by_node(np);
1541
1542 dsi->slave = platform_get_drvdata(gangster);
1543 of_node_put(np);
1544
1545 if (!dsi->slave)
1546 return -EPROBE_DEFER;
1547
1548 dsi->slave->master = dsi;
1549 }
1550
1551 return 0;
1552}
1553
Thierry Redingdec72732013-09-03 08:45:46 +02001554static int tegra_dsi_probe(struct platform_device *pdev)
1555{
1556 struct tegra_dsi *dsi;
1557 struct resource *regs;
1558 int err;
1559
1560 dsi = devm_kzalloc(&pdev->dev, sizeof(*dsi), GFP_KERNEL);
1561 if (!dsi)
1562 return -ENOMEM;
1563
1564 dsi->output.dev = dsi->dev = &pdev->dev;
Thierry Reding976cebc2014-08-06 09:14:28 +02001565 dsi->video_fifo_depth = 1920;
1566 dsi->host_fifo_depth = 64;
Thierry Redingdec72732013-09-03 08:45:46 +02001567
Thierry Redinge94236c2014-10-07 16:10:24 +02001568 err = tegra_dsi_ganged_probe(dsi);
1569 if (err < 0)
1570 return err;
1571
Thierry Redingdec72732013-09-03 08:45:46 +02001572 err = tegra_output_probe(&dsi->output);
1573 if (err < 0)
1574 return err;
1575
Thierry Redingba3df972014-11-13 14:54:01 +01001576 dsi->output.connector.polled = DRM_CONNECTOR_POLL_HPD;
1577
Thierry Redingdec72732013-09-03 08:45:46 +02001578 /*
1579 * Assume these values by default. When a DSI peripheral driver
1580 * attaches to the DSI host, the parameters will be taken from
1581 * the attached device.
1582 */
Thierry Reding17297a22014-03-14 14:13:15 +01001583 dsi->flags = MIPI_DSI_MODE_VIDEO;
Thierry Redingdec72732013-09-03 08:45:46 +02001584 dsi->format = MIPI_DSI_FMT_RGB888;
1585 dsi->lanes = 4;
1586
1587 dsi->rst = devm_reset_control_get(&pdev->dev, "dsi");
1588 if (IS_ERR(dsi->rst))
1589 return PTR_ERR(dsi->rst);
1590
1591 dsi->clk = devm_clk_get(&pdev->dev, NULL);
1592 if (IS_ERR(dsi->clk)) {
1593 dev_err(&pdev->dev, "cannot get DSI clock\n");
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001594 err = PTR_ERR(dsi->clk);
1595 goto reset;
Thierry Redingdec72732013-09-03 08:45:46 +02001596 }
1597
1598 err = clk_prepare_enable(dsi->clk);
1599 if (err < 0) {
1600 dev_err(&pdev->dev, "cannot enable DSI clock\n");
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001601 goto reset;
Thierry Redingdec72732013-09-03 08:45:46 +02001602 }
1603
1604 dsi->clk_lp = devm_clk_get(&pdev->dev, "lp");
1605 if (IS_ERR(dsi->clk_lp)) {
1606 dev_err(&pdev->dev, "cannot get low-power clock\n");
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001607 err = PTR_ERR(dsi->clk_lp);
1608 goto disable_clk;
Thierry Redingdec72732013-09-03 08:45:46 +02001609 }
1610
1611 err = clk_prepare_enable(dsi->clk_lp);
1612 if (err < 0) {
1613 dev_err(&pdev->dev, "cannot enable low-power clock\n");
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001614 goto disable_clk;
Thierry Redingdec72732013-09-03 08:45:46 +02001615 }
1616
1617 dsi->clk_parent = devm_clk_get(&pdev->dev, "parent");
1618 if (IS_ERR(dsi->clk_parent)) {
1619 dev_err(&pdev->dev, "cannot get parent clock\n");
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001620 err = PTR_ERR(dsi->clk_parent);
1621 goto disable_clk_lp;
Thierry Redingdec72732013-09-03 08:45:46 +02001622 }
1623
Thierry Reding3b077af2014-03-14 14:07:50 +01001624 dsi->vdd = devm_regulator_get(&pdev->dev, "avdd-dsi-csi");
1625 if (IS_ERR(dsi->vdd)) {
1626 dev_err(&pdev->dev, "cannot get VDD supply\n");
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001627 err = PTR_ERR(dsi->vdd);
1628 goto disable_clk_lp;
Thierry Reding3b077af2014-03-14 14:07:50 +01001629 }
1630
1631 err = regulator_enable(dsi->vdd);
1632 if (err < 0) {
1633 dev_err(&pdev->dev, "cannot enable VDD supply\n");
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001634 goto disable_clk_lp;
Thierry Reding3b077af2014-03-14 14:07:50 +01001635 }
1636
Thierry Redingdec72732013-09-03 08:45:46 +02001637 err = tegra_dsi_setup_clocks(dsi);
1638 if (err < 0) {
1639 dev_err(&pdev->dev, "cannot setup clocks\n");
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001640 goto disable_vdd;
Thierry Redingdec72732013-09-03 08:45:46 +02001641 }
1642
1643 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1644 dsi->regs = devm_ioremap_resource(&pdev->dev, regs);
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001645 if (IS_ERR(dsi->regs)) {
1646 err = PTR_ERR(dsi->regs);
1647 goto disable_vdd;
1648 }
Thierry Redingdec72732013-09-03 08:45:46 +02001649
Thierry Redingdec72732013-09-03 08:45:46 +02001650 dsi->mipi = tegra_mipi_request(&pdev->dev);
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001651 if (IS_ERR(dsi->mipi)) {
1652 err = PTR_ERR(dsi->mipi);
1653 goto disable_vdd;
1654 }
Thierry Redingdec72732013-09-03 08:45:46 +02001655
1656 dsi->host.ops = &tegra_dsi_host_ops;
1657 dsi->host.dev = &pdev->dev;
1658
1659 err = mipi_dsi_host_register(&dsi->host);
1660 if (err < 0) {
1661 dev_err(&pdev->dev, "failed to register DSI host: %d\n", err);
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001662 goto mipi_free;
Thierry Redingdec72732013-09-03 08:45:46 +02001663 }
1664
1665 INIT_LIST_HEAD(&dsi->client.list);
1666 dsi->client.ops = &dsi_client_ops;
1667 dsi->client.dev = &pdev->dev;
1668
1669 err = host1x_client_register(&dsi->client);
1670 if (err < 0) {
1671 dev_err(&pdev->dev, "failed to register host1x client: %d\n",
1672 err);
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001673 goto unregister;
Thierry Redingdec72732013-09-03 08:45:46 +02001674 }
1675
1676 platform_set_drvdata(pdev, dsi);
1677
1678 return 0;
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001679
1680unregister:
1681 mipi_dsi_host_unregister(&dsi->host);
1682mipi_free:
1683 tegra_mipi_free(dsi->mipi);
1684disable_vdd:
1685 regulator_disable(dsi->vdd);
1686disable_clk_lp:
1687 clk_disable_unprepare(dsi->clk_lp);
1688disable_clk:
1689 clk_disable_unprepare(dsi->clk);
1690reset:
1691 reset_control_assert(dsi->rst);
1692 return err;
Thierry Redingdec72732013-09-03 08:45:46 +02001693}
1694
1695static int tegra_dsi_remove(struct platform_device *pdev)
1696{
1697 struct tegra_dsi *dsi = platform_get_drvdata(pdev);
1698 int err;
1699
1700 err = host1x_client_unregister(&dsi->client);
1701 if (err < 0) {
1702 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
1703 err);
1704 return err;
1705 }
1706
Thierry Reding328ec692014-12-19 15:55:08 +01001707 tegra_output_remove(&dsi->output);
Thierry Reding5b901e72014-12-02 17:30:23 +01001708
Thierry Redingdec72732013-09-03 08:45:46 +02001709 mipi_dsi_host_unregister(&dsi->host);
1710 tegra_mipi_free(dsi->mipi);
1711
Thierry Reding3b077af2014-03-14 14:07:50 +01001712 regulator_disable(dsi->vdd);
Thierry Redingdec72732013-09-03 08:45:46 +02001713 clk_disable_unprepare(dsi->clk_lp);
1714 clk_disable_unprepare(dsi->clk);
Thierry Redingcb825d82014-03-14 14:25:43 +01001715 reset_control_assert(dsi->rst);
Thierry Redingdec72732013-09-03 08:45:46 +02001716
Thierry Redingdec72732013-09-03 08:45:46 +02001717 return 0;
1718}
1719
1720static const struct of_device_id tegra_dsi_of_match[] = {
1721 { .compatible = "nvidia,tegra114-dsi", },
1722 { },
1723};
Stephen Warrenef707282014-06-18 16:21:55 -06001724MODULE_DEVICE_TABLE(of, tegra_dsi_of_match);
Thierry Redingdec72732013-09-03 08:45:46 +02001725
1726struct platform_driver tegra_dsi_driver = {
1727 .driver = {
1728 .name = "tegra-dsi",
1729 .of_match_table = tegra_dsi_of_match,
1730 },
1731 .probe = tegra_dsi_probe,
1732 .remove = tegra_dsi_remove,
1733};