blob: 4a78af08df82c6030e4f8ebc9557e39468b50e95 [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>
Thierry Redingef8187d2015-08-07 09:29:54 +020016#include <linux/pm_runtime.h>
Thierry Redingdec72732013-09-03 08:45:46 +020017#include <linux/reset.h>
18
Thierry Reding3b077af2014-03-14 14:07:50 +010019#include <linux/regulator/consumer.h>
20
Thierry Reding4aa3df72014-11-24 16:27:13 +010021#include <drm/drm_atomic_helper.h>
Thierry Redingdec72732013-09-03 08:45:46 +020022#include <drm/drm_mipi_dsi.h>
23#include <drm/drm_panel.h>
24
25#include <video/mipi_display.h>
26
27#include "dc.h"
28#include "drm.h"
29#include "dsi.h"
30#include "mipi-phy.h"
Thierry Reding75af8fa2017-08-15 15:41:12 +020031#include "trace.h"
Thierry Redingdec72732013-09-03 08:45:46 +020032
Thierry Redingebd14af2014-12-08 16:22:28 +010033struct tegra_dsi_state {
34 struct drm_connector_state base;
35
36 struct mipi_dphy_timing timing;
37 unsigned long period;
38
39 unsigned int vrefresh;
40 unsigned int lanes;
41 unsigned long pclk;
42 unsigned long bclk;
43
44 enum tegra_dsi_format format;
45 unsigned int mul;
46 unsigned int div;
47};
48
49static inline struct tegra_dsi_state *
50to_dsi_state(struct drm_connector_state *state)
51{
52 return container_of(state, struct tegra_dsi_state, base);
53}
54
Thierry Redingdec72732013-09-03 08:45:46 +020055struct tegra_dsi {
56 struct host1x_client client;
57 struct tegra_output output;
58 struct device *dev;
59
60 void __iomem *regs;
61
62 struct reset_control *rst;
63 struct clk *clk_parent;
64 struct clk *clk_lp;
65 struct clk *clk;
66
67 struct drm_info_list *debugfs_files;
68 struct drm_minor *minor;
69 struct dentry *debugfs;
70
Thierry Reding17297a22014-03-14 14:13:15 +010071 unsigned long flags;
Thierry Redingdec72732013-09-03 08:45:46 +020072 enum mipi_dsi_pixel_format format;
73 unsigned int lanes;
74
75 struct tegra_mipi_device *mipi;
76 struct mipi_dsi_host host;
Thierry Reding3b077af2014-03-14 14:07:50 +010077
78 struct regulator *vdd;
Thierry Reding976cebc2014-08-06 09:14:28 +020079
80 unsigned int video_fifo_depth;
81 unsigned int host_fifo_depth;
Thierry Redinge94236c2014-10-07 16:10:24 +020082
83 /* for ganged-mode support */
84 struct tegra_dsi *master;
85 struct tegra_dsi *slave;
Thierry Redingdec72732013-09-03 08:45:46 +020086};
87
88static inline struct tegra_dsi *
89host1x_client_to_dsi(struct host1x_client *client)
90{
91 return container_of(client, struct tegra_dsi, client);
92}
93
94static inline struct tegra_dsi *host_to_tegra(struct mipi_dsi_host *host)
95{
96 return container_of(host, struct tegra_dsi, host);
97}
98
99static inline struct tegra_dsi *to_dsi(struct tegra_output *output)
100{
101 return container_of(output, struct tegra_dsi, output);
102}
103
Thierry Redingebd14af2014-12-08 16:22:28 +0100104static struct tegra_dsi_state *tegra_dsi_get_state(struct tegra_dsi *dsi)
105{
106 return to_dsi_state(dsi->output.connector.state);
107}
108
Thierry Reding12831072017-08-15 15:41:07 +0200109static inline u32 tegra_dsi_readl(struct tegra_dsi *dsi, unsigned int offset)
Thierry Redingdec72732013-09-03 08:45:46 +0200110{
Thierry Reding75af8fa2017-08-15 15:41:12 +0200111 u32 value = readl(dsi->regs + (offset << 2));
112
113 trace_dsi_readl(dsi->dev, offset, value);
114
115 return value;
Thierry Redingdec72732013-09-03 08:45:46 +0200116}
117
Thierry Reding9c0b4ca2014-11-24 12:27:59 +0100118static inline void tegra_dsi_writel(struct tegra_dsi *dsi, u32 value,
Thierry Reding12831072017-08-15 15:41:07 +0200119 unsigned int offset)
Thierry Redingdec72732013-09-03 08:45:46 +0200120{
Thierry Reding75af8fa2017-08-15 15:41:12 +0200121 trace_dsi_writel(dsi->dev, offset, value);
Thierry Reding12831072017-08-15 15:41:07 +0200122 writel(value, dsi->regs + (offset << 2));
Thierry Redingdec72732013-09-03 08:45:46 +0200123}
124
Thierry Redinga40051c2017-11-10 12:18:22 +0100125#define DEBUGFS_REG32(_name) { .name = #_name, .offset = _name }
126
127static const struct debugfs_reg32 tegra_dsi_regs[] = {
128 DEBUGFS_REG32(DSI_INCR_SYNCPT),
129 DEBUGFS_REG32(DSI_INCR_SYNCPT_CONTROL),
130 DEBUGFS_REG32(DSI_INCR_SYNCPT_ERROR),
131 DEBUGFS_REG32(DSI_CTXSW),
132 DEBUGFS_REG32(DSI_RD_DATA),
133 DEBUGFS_REG32(DSI_WR_DATA),
134 DEBUGFS_REG32(DSI_POWER_CONTROL),
135 DEBUGFS_REG32(DSI_INT_ENABLE),
136 DEBUGFS_REG32(DSI_INT_STATUS),
137 DEBUGFS_REG32(DSI_INT_MASK),
138 DEBUGFS_REG32(DSI_HOST_CONTROL),
139 DEBUGFS_REG32(DSI_CONTROL),
140 DEBUGFS_REG32(DSI_SOL_DELAY),
141 DEBUGFS_REG32(DSI_MAX_THRESHOLD),
142 DEBUGFS_REG32(DSI_TRIGGER),
143 DEBUGFS_REG32(DSI_TX_CRC),
144 DEBUGFS_REG32(DSI_STATUS),
145 DEBUGFS_REG32(DSI_INIT_SEQ_CONTROL),
146 DEBUGFS_REG32(DSI_INIT_SEQ_DATA_0),
147 DEBUGFS_REG32(DSI_INIT_SEQ_DATA_1),
148 DEBUGFS_REG32(DSI_INIT_SEQ_DATA_2),
149 DEBUGFS_REG32(DSI_INIT_SEQ_DATA_3),
150 DEBUGFS_REG32(DSI_INIT_SEQ_DATA_4),
151 DEBUGFS_REG32(DSI_INIT_SEQ_DATA_5),
152 DEBUGFS_REG32(DSI_INIT_SEQ_DATA_6),
153 DEBUGFS_REG32(DSI_INIT_SEQ_DATA_7),
154 DEBUGFS_REG32(DSI_PKT_SEQ_0_LO),
155 DEBUGFS_REG32(DSI_PKT_SEQ_0_HI),
156 DEBUGFS_REG32(DSI_PKT_SEQ_1_LO),
157 DEBUGFS_REG32(DSI_PKT_SEQ_1_HI),
158 DEBUGFS_REG32(DSI_PKT_SEQ_2_LO),
159 DEBUGFS_REG32(DSI_PKT_SEQ_2_HI),
160 DEBUGFS_REG32(DSI_PKT_SEQ_3_LO),
161 DEBUGFS_REG32(DSI_PKT_SEQ_3_HI),
162 DEBUGFS_REG32(DSI_PKT_SEQ_4_LO),
163 DEBUGFS_REG32(DSI_PKT_SEQ_4_HI),
164 DEBUGFS_REG32(DSI_PKT_SEQ_5_LO),
165 DEBUGFS_REG32(DSI_PKT_SEQ_5_HI),
166 DEBUGFS_REG32(DSI_DCS_CMDS),
167 DEBUGFS_REG32(DSI_PKT_LEN_0_1),
168 DEBUGFS_REG32(DSI_PKT_LEN_2_3),
169 DEBUGFS_REG32(DSI_PKT_LEN_4_5),
170 DEBUGFS_REG32(DSI_PKT_LEN_6_7),
171 DEBUGFS_REG32(DSI_PHY_TIMING_0),
172 DEBUGFS_REG32(DSI_PHY_TIMING_1),
173 DEBUGFS_REG32(DSI_PHY_TIMING_2),
174 DEBUGFS_REG32(DSI_BTA_TIMING),
175 DEBUGFS_REG32(DSI_TIMEOUT_0),
176 DEBUGFS_REG32(DSI_TIMEOUT_1),
177 DEBUGFS_REG32(DSI_TO_TALLY),
178 DEBUGFS_REG32(DSI_PAD_CONTROL_0),
179 DEBUGFS_REG32(DSI_PAD_CONTROL_CD),
180 DEBUGFS_REG32(DSI_PAD_CD_STATUS),
181 DEBUGFS_REG32(DSI_VIDEO_MODE_CONTROL),
182 DEBUGFS_REG32(DSI_PAD_CONTROL_1),
183 DEBUGFS_REG32(DSI_PAD_CONTROL_2),
184 DEBUGFS_REG32(DSI_PAD_CONTROL_3),
185 DEBUGFS_REG32(DSI_PAD_CONTROL_4),
186 DEBUGFS_REG32(DSI_GANGED_MODE_CONTROL),
187 DEBUGFS_REG32(DSI_GANGED_MODE_START),
188 DEBUGFS_REG32(DSI_GANGED_MODE_SIZE),
189 DEBUGFS_REG32(DSI_RAW_DATA_BYTE_COUNT),
190 DEBUGFS_REG32(DSI_ULTRA_LOW_POWER_CONTROL),
191 DEBUGFS_REG32(DSI_INIT_SEQ_DATA_8),
192 DEBUGFS_REG32(DSI_INIT_SEQ_DATA_9),
193 DEBUGFS_REG32(DSI_INIT_SEQ_DATA_10),
194 DEBUGFS_REG32(DSI_INIT_SEQ_DATA_11),
195 DEBUGFS_REG32(DSI_INIT_SEQ_DATA_12),
196 DEBUGFS_REG32(DSI_INIT_SEQ_DATA_13),
197 DEBUGFS_REG32(DSI_INIT_SEQ_DATA_14),
198 DEBUGFS_REG32(DSI_INIT_SEQ_DATA_15),
199};
200
Thierry Redingdec72732013-09-03 08:45:46 +0200201static int tegra_dsi_show_regs(struct seq_file *s, void *data)
202{
203 struct drm_info_node *node = s->private;
204 struct tegra_dsi *dsi = node->info_ent->data;
Thierry Reding171e2e62015-07-29 16:04:44 +0200205 struct drm_crtc *crtc = dsi->output.encoder.crtc;
206 struct drm_device *drm = node->minor->dev;
Thierry Redinga40051c2017-11-10 12:18:22 +0100207 unsigned int i;
Thierry Reding171e2e62015-07-29 16:04:44 +0200208 int err = 0;
209
210 drm_modeset_lock_all(drm);
211
212 if (!crtc || !crtc->state->active) {
213 err = -EBUSY;
214 goto unlock;
215 }
Thierry Redingdec72732013-09-03 08:45:46 +0200216
Thierry Redinga40051c2017-11-10 12:18:22 +0100217 for (i = 0; i < ARRAY_SIZE(tegra_dsi_regs); i++) {
218 unsigned int offset = tegra_dsi_regs[i].offset;
Thierry Redingdec72732013-09-03 08:45:46 +0200219
Thierry Redinga40051c2017-11-10 12:18:22 +0100220 seq_printf(s, "%-32s %#05x %08x\n", tegra_dsi_regs[i].name,
221 offset, tegra_dsi_readl(dsi, offset));
222 }
Thierry Redingdec72732013-09-03 08:45:46 +0200223
Thierry Reding171e2e62015-07-29 16:04:44 +0200224unlock:
225 drm_modeset_unlock_all(drm);
226 return err;
Thierry Redingdec72732013-09-03 08:45:46 +0200227}
228
229static struct drm_info_list debugfs_files[] = {
230 { "regs", tegra_dsi_show_regs, 0, NULL },
231};
232
233static int tegra_dsi_debugfs_init(struct tegra_dsi *dsi,
234 struct drm_minor *minor)
235{
236 const char *name = dev_name(dsi->dev);
237 unsigned int i;
238 int err;
239
240 dsi->debugfs = debugfs_create_dir(name, minor->debugfs_root);
241 if (!dsi->debugfs)
242 return -ENOMEM;
243
244 dsi->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
245 GFP_KERNEL);
246 if (!dsi->debugfs_files) {
247 err = -ENOMEM;
248 goto remove;
249 }
250
251 for (i = 0; i < ARRAY_SIZE(debugfs_files); i++)
252 dsi->debugfs_files[i].data = dsi;
253
254 err = drm_debugfs_create_files(dsi->debugfs_files,
255 ARRAY_SIZE(debugfs_files),
256 dsi->debugfs, minor);
257 if (err < 0)
258 goto free;
259
260 dsi->minor = minor;
261
262 return 0;
263
264free:
265 kfree(dsi->debugfs_files);
266 dsi->debugfs_files = NULL;
267remove:
268 debugfs_remove(dsi->debugfs);
269 dsi->debugfs = NULL;
270
271 return err;
272}
273
Thierry Reding4009c222014-12-19 15:47:30 +0100274static void tegra_dsi_debugfs_exit(struct tegra_dsi *dsi)
Thierry Redingdec72732013-09-03 08:45:46 +0200275{
276 drm_debugfs_remove_files(dsi->debugfs_files, ARRAY_SIZE(debugfs_files),
277 dsi->minor);
278 dsi->minor = NULL;
279
280 kfree(dsi->debugfs_files);
281 dsi->debugfs_files = NULL;
282
283 debugfs_remove(dsi->debugfs);
284 dsi->debugfs = NULL;
Thierry Redingdec72732013-09-03 08:45:46 +0200285}
286
287#define PKT_ID0(id) ((((id) & 0x3f) << 3) | (1 << 9))
288#define PKT_LEN0(len) (((len) & 0x07) << 0)
289#define PKT_ID1(id) ((((id) & 0x3f) << 13) | (1 << 19))
290#define PKT_LEN1(len) (((len) & 0x07) << 10)
291#define PKT_ID2(id) ((((id) & 0x3f) << 23) | (1 << 29))
292#define PKT_LEN2(len) (((len) & 0x07) << 20)
293
294#define PKT_LP (1 << 30)
295#define NUM_PKT_SEQ 12
296
Thierry Reding17297a22014-03-14 14:13:15 +0100297/*
298 * non-burst mode with sync pulses
299 */
300static const u32 pkt_seq_video_non_burst_sync_pulses[NUM_PKT_SEQ] = {
Thierry Redingdec72732013-09-03 08:45:46 +0200301 [ 0] = PKT_ID0(MIPI_DSI_V_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 PKT_LP,
305 [ 1] = 0,
306 [ 2] = PKT_ID0(MIPI_DSI_V_SYNC_END) | PKT_LEN0(0) |
307 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
308 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0) |
309 PKT_LP,
310 [ 3] = 0,
311 [ 4] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
312 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
313 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0) |
314 PKT_LP,
315 [ 5] = 0,
316 [ 6] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
317 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
318 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0),
319 [ 7] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(2) |
320 PKT_ID1(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN1(3) |
321 PKT_ID2(MIPI_DSI_BLANKING_PACKET) | PKT_LEN2(4),
322 [ 8] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
323 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
324 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0) |
325 PKT_LP,
326 [ 9] = 0,
327 [10] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
328 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
329 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0),
330 [11] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(2) |
331 PKT_ID1(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN1(3) |
332 PKT_ID2(MIPI_DSI_BLANKING_PACKET) | PKT_LEN2(4),
333};
334
Thierry Reding17297a22014-03-14 14:13:15 +0100335/*
336 * non-burst mode with sync events
337 */
338static const u32 pkt_seq_video_non_burst_sync_events[NUM_PKT_SEQ] = {
339 [ 0] = PKT_ID0(MIPI_DSI_V_SYNC_START) | PKT_LEN0(0) |
340 PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION) | PKT_LEN1(7) |
341 PKT_LP,
342 [ 1] = 0,
343 [ 2] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
344 PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION) | PKT_LEN1(7) |
345 PKT_LP,
346 [ 3] = 0,
347 [ 4] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
348 PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION) | PKT_LEN1(7) |
349 PKT_LP,
350 [ 5] = 0,
351 [ 6] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
352 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(2) |
353 PKT_ID2(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN2(3),
354 [ 7] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(4),
355 [ 8] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
356 PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION) | PKT_LEN1(7) |
357 PKT_LP,
358 [ 9] = 0,
359 [10] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
360 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(2) |
361 PKT_ID2(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN2(3),
362 [11] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(4),
363};
364
Thierry Reding337b4432014-11-13 15:02:46 +0100365static const u32 pkt_seq_command_mode[NUM_PKT_SEQ] = {
366 [ 0] = 0,
367 [ 1] = 0,
368 [ 2] = 0,
369 [ 3] = 0,
370 [ 4] = 0,
371 [ 5] = 0,
372 [ 6] = PKT_ID0(MIPI_DSI_DCS_LONG_WRITE) | PKT_LEN0(3) | PKT_LP,
373 [ 7] = 0,
374 [ 8] = 0,
375 [ 9] = 0,
376 [10] = PKT_ID0(MIPI_DSI_DCS_LONG_WRITE) | PKT_LEN0(5) | PKT_LP,
377 [11] = 0,
378};
379
Thierry Redingebd14af2014-12-08 16:22:28 +0100380static void tegra_dsi_set_phy_timing(struct tegra_dsi *dsi,
381 unsigned long period,
382 const struct mipi_dphy_timing *timing)
Thierry Redingdec72732013-09-03 08:45:46 +0200383{
Thierry Reding9c0b4ca2014-11-24 12:27:59 +0100384 u32 value;
Thierry Redingdec72732013-09-03 08:45:46 +0200385
Thierry Redingebd14af2014-12-08 16:22:28 +0100386 value = DSI_TIMING_FIELD(timing->hsexit, period, 1) << 24 |
387 DSI_TIMING_FIELD(timing->hstrail, period, 0) << 16 |
388 DSI_TIMING_FIELD(timing->hszero, period, 3) << 8 |
389 DSI_TIMING_FIELD(timing->hsprepare, period, 1);
Thierry Redingdec72732013-09-03 08:45:46 +0200390 tegra_dsi_writel(dsi, value, DSI_PHY_TIMING_0);
391
Thierry Redingebd14af2014-12-08 16:22:28 +0100392 value = DSI_TIMING_FIELD(timing->clktrail, period, 1) << 24 |
393 DSI_TIMING_FIELD(timing->clkpost, period, 1) << 16 |
394 DSI_TIMING_FIELD(timing->clkzero, period, 1) << 8 |
395 DSI_TIMING_FIELD(timing->lpx, period, 1);
Thierry Redingdec72732013-09-03 08:45:46 +0200396 tegra_dsi_writel(dsi, value, DSI_PHY_TIMING_1);
397
Thierry Redingebd14af2014-12-08 16:22:28 +0100398 value = DSI_TIMING_FIELD(timing->clkprepare, period, 1) << 16 |
399 DSI_TIMING_FIELD(timing->clkpre, period, 1) << 8 |
Thierry Redingdec72732013-09-03 08:45:46 +0200400 DSI_TIMING_FIELD(0xff * period, period, 0) << 0;
401 tegra_dsi_writel(dsi, value, DSI_PHY_TIMING_2);
402
Thierry Redingebd14af2014-12-08 16:22:28 +0100403 value = DSI_TIMING_FIELD(timing->taget, period, 1) << 16 |
404 DSI_TIMING_FIELD(timing->tasure, period, 1) << 8 |
405 DSI_TIMING_FIELD(timing->tago, period, 1);
Thierry Redingdec72732013-09-03 08:45:46 +0200406 tegra_dsi_writel(dsi, value, DSI_BTA_TIMING);
407
Sean Paul7e3bc3a2014-10-07 16:04:42 +0200408 if (dsi->slave)
Thierry Redingebd14af2014-12-08 16:22:28 +0100409 tegra_dsi_set_phy_timing(dsi->slave, period, timing);
Thierry Redingdec72732013-09-03 08:45:46 +0200410}
411
412static int tegra_dsi_get_muldiv(enum mipi_dsi_pixel_format format,
413 unsigned int *mulp, unsigned int *divp)
414{
415 switch (format) {
416 case MIPI_DSI_FMT_RGB666_PACKED:
417 case MIPI_DSI_FMT_RGB888:
418 *mulp = 3;
419 *divp = 1;
420 break;
421
422 case MIPI_DSI_FMT_RGB565:
423 *mulp = 2;
424 *divp = 1;
425 break;
426
427 case MIPI_DSI_FMT_RGB666:
428 *mulp = 9;
429 *divp = 4;
430 break;
431
432 default:
433 return -EINVAL;
434 }
435
436 return 0;
437}
438
Thierry Redingf7d68892014-03-13 08:50:39 +0100439static int tegra_dsi_get_format(enum mipi_dsi_pixel_format format,
440 enum tegra_dsi_format *fmt)
441{
442 switch (format) {
443 case MIPI_DSI_FMT_RGB888:
444 *fmt = TEGRA_DSI_FORMAT_24P;
445 break;
446
447 case MIPI_DSI_FMT_RGB666:
448 *fmt = TEGRA_DSI_FORMAT_18NP;
449 break;
450
451 case MIPI_DSI_FMT_RGB666_PACKED:
452 *fmt = TEGRA_DSI_FORMAT_18P;
453 break;
454
455 case MIPI_DSI_FMT_RGB565:
456 *fmt = TEGRA_DSI_FORMAT_16P;
457 break;
458
459 default:
460 return -EINVAL;
461 }
462
463 return 0;
464}
465
Thierry Redinge94236c2014-10-07 16:10:24 +0200466static void tegra_dsi_ganged_enable(struct tegra_dsi *dsi, unsigned int start,
467 unsigned int size)
468{
469 u32 value;
470
471 tegra_dsi_writel(dsi, start, DSI_GANGED_MODE_START);
472 tegra_dsi_writel(dsi, size << 16 | size, DSI_GANGED_MODE_SIZE);
473
474 value = DSI_GANGED_MODE_CONTROL_ENABLE;
475 tegra_dsi_writel(dsi, value, DSI_GANGED_MODE_CONTROL);
476}
477
Thierry Reding563eff12014-11-13 14:44:27 +0100478static void tegra_dsi_enable(struct tegra_dsi *dsi)
Thierry Redingdec72732013-09-03 08:45:46 +0200479{
Thierry Reding563eff12014-11-13 14:44:27 +0100480 u32 value;
Thierry Redingdec72732013-09-03 08:45:46 +0200481
Thierry Reding563eff12014-11-13 14:44:27 +0100482 value = tegra_dsi_readl(dsi, DSI_POWER_CONTROL);
483 value |= DSI_POWER_CONTROL_ENABLE;
484 tegra_dsi_writel(dsi, value, DSI_POWER_CONTROL);
Thierry Redinge94236c2014-10-07 16:10:24 +0200485
486 if (dsi->slave)
487 tegra_dsi_enable(dsi->slave);
488}
489
490static unsigned int tegra_dsi_get_lanes(struct tegra_dsi *dsi)
491{
492 if (dsi->master)
493 return dsi->master->lanes + dsi->lanes;
494
495 if (dsi->slave)
496 return dsi->lanes + dsi->slave->lanes;
497
498 return dsi->lanes;
Thierry Reding563eff12014-11-13 14:44:27 +0100499}
500
Thierry Redingebd14af2014-12-08 16:22:28 +0100501static void tegra_dsi_configure(struct tegra_dsi *dsi, unsigned int pipe,
502 const struct drm_display_mode *mode)
Thierry Reding563eff12014-11-13 14:44:27 +0100503{
504 unsigned int hact, hsw, hbp, hfp, i, mul, div;
Thierry Redingebd14af2014-12-08 16:22:28 +0100505 struct tegra_dsi_state *state;
Thierry Reding563eff12014-11-13 14:44:27 +0100506 const u32 *pkt_seq;
507 u32 value;
Thierry Redingebd14af2014-12-08 16:22:28 +0100508
509 /* XXX: pass in state into this function? */
510 if (dsi->master)
511 state = tegra_dsi_get_state(dsi->master);
512 else
513 state = tegra_dsi_get_state(dsi);
514
515 mul = state->mul;
516 div = state->div;
Thierry Reding334ae6b2014-03-14 14:15:10 +0100517
Thierry Reding17297a22014-03-14 14:13:15 +0100518 if (dsi->flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE) {
519 DRM_DEBUG_KMS("Non-burst video mode with sync pulses\n");
520 pkt_seq = pkt_seq_video_non_burst_sync_pulses;
Thierry Reding337b4432014-11-13 15:02:46 +0100521 } else if (dsi->flags & MIPI_DSI_MODE_VIDEO) {
Thierry Reding17297a22014-03-14 14:13:15 +0100522 DRM_DEBUG_KMS("Non-burst video mode with sync events\n");
523 pkt_seq = pkt_seq_video_non_burst_sync_events;
Thierry Reding337b4432014-11-13 15:02:46 +0100524 } else {
525 DRM_DEBUG_KMS("Command mode\n");
526 pkt_seq = pkt_seq_command_mode;
Thierry Reding17297a22014-03-14 14:13:15 +0100527 }
528
Thierry Redingebd14af2014-12-08 16:22:28 +0100529 value = DSI_CONTROL_CHANNEL(0) |
530 DSI_CONTROL_FORMAT(state->format) |
Thierry Redingdec72732013-09-03 08:45:46 +0200531 DSI_CONTROL_LANES(dsi->lanes - 1) |
Thierry Reding563eff12014-11-13 14:44:27 +0100532 DSI_CONTROL_SOURCE(pipe);
Thierry Redingdec72732013-09-03 08:45:46 +0200533 tegra_dsi_writel(dsi, value, DSI_CONTROL);
534
Thierry Reding976cebc2014-08-06 09:14:28 +0200535 tegra_dsi_writel(dsi, dsi->video_fifo_depth, DSI_MAX_THRESHOLD);
Thierry Redingdec72732013-09-03 08:45:46 +0200536
Thierry Reding563eff12014-11-13 14:44:27 +0100537 value = DSI_HOST_CONTROL_HS;
Thierry Redingdec72732013-09-03 08:45:46 +0200538 tegra_dsi_writel(dsi, value, DSI_HOST_CONTROL);
539
540 value = tegra_dsi_readl(dsi, DSI_CONTROL);
Thierry Reding563eff12014-11-13 14:44:27 +0100541
Alexandre Courbot0c6b1e42014-07-08 21:32:13 +0900542 if (dsi->flags & MIPI_DSI_CLOCK_NON_CONTINUOUS)
543 value |= DSI_CONTROL_HS_CLK_CTRL;
Thierry Reding563eff12014-11-13 14:44:27 +0100544
Thierry Redingdec72732013-09-03 08:45:46 +0200545 value &= ~DSI_CONTROL_TX_TRIG(3);
Thierry Reding337b4432014-11-13 15:02:46 +0100546
547 /* enable DCS commands for command mode */
548 if (dsi->flags & MIPI_DSI_MODE_VIDEO)
549 value &= ~DSI_CONTROL_DCS_ENABLE;
550 else
551 value |= DSI_CONTROL_DCS_ENABLE;
552
Thierry Redingdec72732013-09-03 08:45:46 +0200553 value |= DSI_CONTROL_VIDEO_ENABLE;
554 value &= ~DSI_CONTROL_HOST_ENABLE;
555 tegra_dsi_writel(dsi, value, DSI_CONTROL);
556
Thierry Redingdec72732013-09-03 08:45:46 +0200557 for (i = 0; i < NUM_PKT_SEQ; i++)
558 tegra_dsi_writel(dsi, pkt_seq[i], DSI_PKT_SEQ_0_LO + i);
559
Thierry Reding337b4432014-11-13 15:02:46 +0100560 if (dsi->flags & MIPI_DSI_MODE_VIDEO) {
561 /* horizontal active pixels */
562 hact = mode->hdisplay * mul / div;
Thierry Redingdec72732013-09-03 08:45:46 +0200563
Thierry Reding337b4432014-11-13 15:02:46 +0100564 /* horizontal sync width */
565 hsw = (mode->hsync_end - mode->hsync_start) * mul / div;
Thierry Redingdec72732013-09-03 08:45:46 +0200566
Thierry Reding337b4432014-11-13 15:02:46 +0100567 /* horizontal back porch */
568 hbp = (mode->htotal - mode->hsync_end) * mul / div;
Thierry Redingb8be0bd2015-04-08 16:58:07 +0200569
570 if ((dsi->flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE) == 0)
571 hbp += hsw;
Thierry Redingdec72732013-09-03 08:45:46 +0200572
Thierry Reding337b4432014-11-13 15:02:46 +0100573 /* horizontal front porch */
574 hfp = (mode->hsync_start - mode->hdisplay) * mul / div;
Thierry Redingb8be0bd2015-04-08 16:58:07 +0200575
576 /* subtract packet overhead */
577 hsw -= 10;
578 hbp -= 14;
Thierry Reding337b4432014-11-13 15:02:46 +0100579 hfp -= 8;
Thierry Redingdec72732013-09-03 08:45:46 +0200580
Thierry Reding337b4432014-11-13 15:02:46 +0100581 tegra_dsi_writel(dsi, hsw << 16 | 0, DSI_PKT_LEN_0_1);
582 tegra_dsi_writel(dsi, hact << 16 | hbp, DSI_PKT_LEN_2_3);
583 tegra_dsi_writel(dsi, hfp, DSI_PKT_LEN_4_5);
584 tegra_dsi_writel(dsi, 0x0f0f << 16, DSI_PKT_LEN_6_7);
Thierry Redingdec72732013-09-03 08:45:46 +0200585
Thierry Reding337b4432014-11-13 15:02:46 +0100586 /* set SOL delay (for non-burst mode only) */
587 tegra_dsi_writel(dsi, 8 * mul / div, DSI_SOL_DELAY);
Thierry Redinge94236c2014-10-07 16:10:24 +0200588
589 /* TODO: implement ganged mode */
Thierry Reding337b4432014-11-13 15:02:46 +0100590 } else {
591 u16 bytes;
592
Thierry Redinge94236c2014-10-07 16:10:24 +0200593 if (dsi->master || dsi->slave) {
594 /*
595 * For ganged mode, assume symmetric left-right mode.
596 */
597 bytes = 1 + (mode->hdisplay / 2) * mul / div;
598 } else {
599 /* 1 byte (DCS command) + pixel data */
600 bytes = 1 + mode->hdisplay * mul / div;
601 }
Thierry Reding337b4432014-11-13 15:02:46 +0100602
603 tegra_dsi_writel(dsi, 0, DSI_PKT_LEN_0_1);
604 tegra_dsi_writel(dsi, bytes << 16, DSI_PKT_LEN_2_3);
605 tegra_dsi_writel(dsi, bytes << 16, DSI_PKT_LEN_4_5);
606 tegra_dsi_writel(dsi, 0, DSI_PKT_LEN_6_7);
607
608 value = MIPI_DCS_WRITE_MEMORY_START << 8 |
609 MIPI_DCS_WRITE_MEMORY_CONTINUE;
610 tegra_dsi_writel(dsi, value, DSI_DCS_CMDS);
611
Thierry Redinge94236c2014-10-07 16:10:24 +0200612 /* set SOL delay */
613 if (dsi->master || dsi->slave) {
Thierry Redinge94236c2014-10-07 16:10:24 +0200614 unsigned long delay, bclk, bclk_ganged;
Thierry Redingebd14af2014-12-08 16:22:28 +0100615 unsigned int lanes = state->lanes;
Thierry Redinge94236c2014-10-07 16:10:24 +0200616
617 /* SOL to valid, valid to FIFO and FIFO write delay */
618 delay = 4 + 4 + 2;
619 delay = DIV_ROUND_UP(delay * mul, div * lanes);
620 /* FIFO read delay */
621 delay = delay + 6;
622
623 bclk = DIV_ROUND_UP(mode->htotal * mul, div * lanes);
624 bclk_ganged = DIV_ROUND_UP(bclk * lanes / 2, lanes);
625 value = bclk - bclk_ganged + delay + 20;
626 } else {
627 /* TODO: revisit for non-ganged mode */
628 value = 8 * mul / div;
629 }
Thierry Reding337b4432014-11-13 15:02:46 +0100630
631 tegra_dsi_writel(dsi, value, DSI_SOL_DELAY);
632 }
Thierry Redingdec72732013-09-03 08:45:46 +0200633
Thierry Redinge94236c2014-10-07 16:10:24 +0200634 if (dsi->slave) {
Thierry Redingebd14af2014-12-08 16:22:28 +0100635 tegra_dsi_configure(dsi->slave, pipe, mode);
Thierry Redinge94236c2014-10-07 16:10:24 +0200636
637 /*
638 * TODO: Support modes other than symmetrical left-right
639 * split.
640 */
641 tegra_dsi_ganged_enable(dsi, 0, mode->hdisplay / 2);
642 tegra_dsi_ganged_enable(dsi->slave, mode->hdisplay / 2,
643 mode->hdisplay / 2);
644 }
Thierry Reding563eff12014-11-13 14:44:27 +0100645}
646
Thierry Reding563eff12014-11-13 14:44:27 +0100647static int tegra_dsi_wait_idle(struct tegra_dsi *dsi, unsigned long timeout)
648{
649 u32 value;
650
651 timeout = jiffies + msecs_to_jiffies(timeout);
652
653 while (time_before(jiffies, timeout)) {
654 value = tegra_dsi_readl(dsi, DSI_STATUS);
655 if (value & DSI_STATUS_IDLE)
656 return 0;
657
658 usleep_range(1000, 2000);
659 }
660
661 return -ETIMEDOUT;
662}
663
664static void tegra_dsi_video_disable(struct tegra_dsi *dsi)
665{
666 u32 value;
667
668 value = tegra_dsi_readl(dsi, DSI_CONTROL);
669 value &= ~DSI_CONTROL_VIDEO_ENABLE;
670 tegra_dsi_writel(dsi, value, DSI_CONTROL);
Thierry Redinge94236c2014-10-07 16:10:24 +0200671
672 if (dsi->slave)
673 tegra_dsi_video_disable(dsi->slave);
674}
675
676static void tegra_dsi_ganged_disable(struct tegra_dsi *dsi)
677{
678 tegra_dsi_writel(dsi, 0, DSI_GANGED_MODE_START);
679 tegra_dsi_writel(dsi, 0, DSI_GANGED_MODE_SIZE);
680 tegra_dsi_writel(dsi, 0, DSI_GANGED_MODE_CONTROL);
Thierry Reding563eff12014-11-13 14:44:27 +0100681}
682
Thierry Redingef8187d2015-08-07 09:29:54 +0200683static int tegra_dsi_pad_enable(struct tegra_dsi *dsi)
684{
685 u32 value;
686
687 value = DSI_PAD_CONTROL_VS1_PULLDN(0) | DSI_PAD_CONTROL_VS1_PDIO(0);
688 tegra_dsi_writel(dsi, value, DSI_PAD_CONTROL_0);
689
690 return 0;
691}
692
693static int tegra_dsi_pad_calibrate(struct tegra_dsi *dsi)
694{
695 u32 value;
696
697 /*
698 * XXX Is this still needed? The module reset is deasserted right
699 * before this function is called.
700 */
701 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_0);
702 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_1);
703 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_2);
704 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_3);
705 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_4);
706
707 /* start calibration */
708 tegra_dsi_pad_enable(dsi);
709
710 value = DSI_PAD_SLEW_UP(0x7) | DSI_PAD_SLEW_DN(0x7) |
711 DSI_PAD_LP_UP(0x1) | DSI_PAD_LP_DN(0x1) |
712 DSI_PAD_OUT_CLK(0x0);
713 tegra_dsi_writel(dsi, value, DSI_PAD_CONTROL_2);
714
715 value = DSI_PAD_PREEMP_PD_CLK(0x3) | DSI_PAD_PREEMP_PU_CLK(0x3) |
716 DSI_PAD_PREEMP_PD(0x03) | DSI_PAD_PREEMP_PU(0x3);
717 tegra_dsi_writel(dsi, value, DSI_PAD_CONTROL_3);
718
719 return tegra_mipi_calibrate(dsi->mipi);
720}
721
Thierry Reding5b901e72014-12-02 17:30:23 +0100722static void tegra_dsi_set_timeout(struct tegra_dsi *dsi, unsigned long bclk,
723 unsigned int vrefresh)
724{
725 unsigned int timeout;
726 u32 value;
727
728 /* one frame high-speed transmission timeout */
729 timeout = (bclk / vrefresh) / 512;
730 value = DSI_TIMEOUT_LRX(0x2000) | DSI_TIMEOUT_HTX(timeout);
731 tegra_dsi_writel(dsi, value, DSI_TIMEOUT_0);
732
733 /* 2 ms peripheral timeout for panel */
734 timeout = 2 * bclk / 512 * 1000;
735 value = DSI_TIMEOUT_PR(timeout) | DSI_TIMEOUT_TA(0x2000);
736 tegra_dsi_writel(dsi, value, DSI_TIMEOUT_1);
737
738 value = DSI_TALLY_TA(0) | DSI_TALLY_LRX(0) | DSI_TALLY_HTX(0);
739 tegra_dsi_writel(dsi, value, DSI_TO_TALLY);
740
741 if (dsi->slave)
742 tegra_dsi_set_timeout(dsi->slave, bclk, vrefresh);
743}
744
Thierry Reding563eff12014-11-13 14:44:27 +0100745static void tegra_dsi_disable(struct tegra_dsi *dsi)
746{
747 u32 value;
748
Thierry Redinge94236c2014-10-07 16:10:24 +0200749 if (dsi->slave) {
750 tegra_dsi_ganged_disable(dsi->slave);
751 tegra_dsi_ganged_disable(dsi);
752 }
753
Thierry Reding563eff12014-11-13 14:44:27 +0100754 value = tegra_dsi_readl(dsi, DSI_POWER_CONTROL);
755 value &= ~DSI_POWER_CONTROL_ENABLE;
756 tegra_dsi_writel(dsi, value, DSI_POWER_CONTROL);
757
Thierry Redinge94236c2014-10-07 16:10:24 +0200758 if (dsi->slave)
759 tegra_dsi_disable(dsi->slave);
760
Thierry Reding563eff12014-11-13 14:44:27 +0100761 usleep_range(5000, 10000);
762}
763
Thierry Reding92f0e072014-11-24 16:29:40 +0100764static void tegra_dsi_soft_reset(struct tegra_dsi *dsi)
765{
766 u32 value;
767
768 value = tegra_dsi_readl(dsi, DSI_POWER_CONTROL);
769 value &= ~DSI_POWER_CONTROL_ENABLE;
770 tegra_dsi_writel(dsi, value, DSI_POWER_CONTROL);
771
772 usleep_range(300, 1000);
773
774 value = tegra_dsi_readl(dsi, DSI_POWER_CONTROL);
775 value |= DSI_POWER_CONTROL_ENABLE;
776 tegra_dsi_writel(dsi, value, DSI_POWER_CONTROL);
777
778 usleep_range(300, 1000);
779
780 value = tegra_dsi_readl(dsi, DSI_TRIGGER);
781 if (value)
782 tegra_dsi_writel(dsi, 0, DSI_TRIGGER);
783
784 if (dsi->slave)
785 tegra_dsi_soft_reset(dsi->slave);
786}
787
Thierry Redingebd14af2014-12-08 16:22:28 +0100788static void tegra_dsi_connector_reset(struct drm_connector *connector)
789{
Jon Hunter280dc0e2016-05-18 16:37:36 +0100790 struct tegra_dsi_state *state = kzalloc(sizeof(*state), GFP_KERNEL);
Thierry Redingebd14af2014-12-08 16:22:28 +0100791
Jon Hunter280dc0e2016-05-18 16:37:36 +0100792 if (!state)
793 return;
794
795 if (connector->state) {
796 __drm_atomic_helper_connector_destroy_state(connector->state);
Maarten Lankhorst5459a2a2016-01-04 12:53:17 +0100797 kfree(connector->state);
Maarten Lankhorst5459a2a2016-01-04 12:53:17 +0100798 }
Jon Hunter280dc0e2016-05-18 16:37:36 +0100799
800 __drm_atomic_helper_connector_reset(connector, &state->base);
Thierry Redingebd14af2014-12-08 16:22:28 +0100801}
802
803static struct drm_connector_state *
804tegra_dsi_connector_duplicate_state(struct drm_connector *connector)
805{
806 struct tegra_dsi_state *state = to_dsi_state(connector->state);
807 struct tegra_dsi_state *copy;
808
809 copy = kmemdup(state, sizeof(*state), GFP_KERNEL);
810 if (!copy)
811 return NULL;
812
Jon Hunter280dc0e2016-05-18 16:37:36 +0100813 __drm_atomic_helper_connector_duplicate_state(connector,
814 &copy->base);
815
Thierry Redingebd14af2014-12-08 16:22:28 +0100816 return &copy->base;
817}
818
Thierry Reding5b901e72014-12-02 17:30:23 +0100819static const struct drm_connector_funcs tegra_dsi_connector_funcs = {
Thierry Redingebd14af2014-12-08 16:22:28 +0100820 .reset = tegra_dsi_connector_reset,
Thierry Reding5b901e72014-12-02 17:30:23 +0100821 .detect = tegra_output_connector_detect,
822 .fill_modes = drm_helper_probe_single_connector_modes,
823 .destroy = tegra_output_connector_destroy,
Thierry Redingebd14af2014-12-08 16:22:28 +0100824 .atomic_duplicate_state = tegra_dsi_connector_duplicate_state,
Thierry Reding4aa3df72014-11-24 16:27:13 +0100825 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
Thierry Reding5b901e72014-12-02 17:30:23 +0100826};
827
828static enum drm_mode_status
829tegra_dsi_connector_mode_valid(struct drm_connector *connector,
830 struct drm_display_mode *mode)
Thierry Reding3f6b4062014-11-13 14:50:33 +0100831{
Thierry Reding5b901e72014-12-02 17:30:23 +0100832 return MODE_OK;
Thierry Reding3f6b4062014-11-13 14:50:33 +0100833}
834
Thierry Reding5b901e72014-12-02 17:30:23 +0100835static const struct drm_connector_helper_funcs tegra_dsi_connector_helper_funcs = {
836 .get_modes = tegra_output_connector_get_modes,
837 .mode_valid = tegra_dsi_connector_mode_valid,
Thierry Reding5b901e72014-12-02 17:30:23 +0100838};
839
840static const struct drm_encoder_funcs tegra_dsi_encoder_funcs = {
841 .destroy = tegra_output_encoder_destroy,
842};
843
Thierry Reding87904c32016-08-12 16:00:53 +0200844static void tegra_dsi_unprepare(struct tegra_dsi *dsi)
845{
846 int err;
847
848 if (dsi->slave)
849 tegra_dsi_unprepare(dsi->slave);
850
851 err = tegra_mipi_disable(dsi->mipi);
852 if (err < 0)
853 dev_err(dsi->dev, "failed to disable MIPI calibration: %d\n",
854 err);
855
856 pm_runtime_put(dsi->dev);
857}
858
Thierry Reding5b901e72014-12-02 17:30:23 +0100859static void tegra_dsi_encoder_disable(struct drm_encoder *encoder)
860{
861 struct tegra_output *output = encoder_to_output(encoder);
862 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
863 struct tegra_dsi *dsi = to_dsi(output);
864 u32 value;
865 int err;
866
867 if (output->panel)
868 drm_panel_disable(output->panel);
869
870 tegra_dsi_video_disable(dsi);
871
872 /*
873 * The following accesses registers of the display controller, so make
874 * sure it's only executed when the output is attached to one.
875 */
876 if (dc) {
877 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
878 value &= ~DSI_ENABLE;
879 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
880
881 tegra_dc_commit(dc);
882 }
883
884 err = tegra_dsi_wait_idle(dsi, 100);
885 if (err < 0)
886 dev_dbg(dsi->dev, "failed to idle DSI: %d\n", err);
887
888 tegra_dsi_soft_reset(dsi);
889
890 if (output->panel)
891 drm_panel_unprepare(output->panel);
892
893 tegra_dsi_disable(dsi);
894
Thierry Reding87904c32016-08-12 16:00:53 +0200895 tegra_dsi_unprepare(dsi);
896}
897
898static void tegra_dsi_prepare(struct tegra_dsi *dsi)
899{
900 int err;
901
902 pm_runtime_get_sync(dsi->dev);
903
904 err = tegra_mipi_enable(dsi->mipi);
905 if (err < 0)
906 dev_err(dsi->dev, "failed to enable MIPI calibration: %d\n",
907 err);
908
909 err = tegra_dsi_pad_calibrate(dsi);
910 if (err < 0)
911 dev_err(dsi->dev, "MIPI calibration failed: %d\n", err);
912
913 if (dsi->slave)
914 tegra_dsi_prepare(dsi->slave);
Thierry Reding5b901e72014-12-02 17:30:23 +0100915}
916
Thierry Reding171e2e62015-07-29 16:04:44 +0200917static void tegra_dsi_encoder_enable(struct drm_encoder *encoder)
918{
919 struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
920 struct tegra_output *output = encoder_to_output(encoder);
921 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
922 struct tegra_dsi *dsi = to_dsi(output);
923 struct tegra_dsi_state *state;
924 u32 value;
Thierry Redingef8187d2015-08-07 09:29:54 +0200925
Thierry Reding87904c32016-08-12 16:00:53 +0200926 tegra_dsi_prepare(dsi);
Thierry Reding171e2e62015-07-29 16:04:44 +0200927
928 state = tegra_dsi_get_state(dsi);
929
930 tegra_dsi_set_timeout(dsi, state->bclk, state->vrefresh);
931
932 /*
933 * The D-PHY timing fields are expressed in byte-clock cycles, so
934 * multiply the period by 8.
935 */
936 tegra_dsi_set_phy_timing(dsi, state->period * 8, &state->timing);
937
938 if (output->panel)
939 drm_panel_prepare(output->panel);
940
941 tegra_dsi_configure(dsi, dc->pipe, mode);
942
943 /* enable display controller */
944 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
945 value |= DSI_ENABLE;
946 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
947
948 tegra_dc_commit(dc);
949
950 /* enable DSI controller */
951 tegra_dsi_enable(dsi);
952
953 if (output->panel)
954 drm_panel_enable(output->panel);
Thierry Reding171e2e62015-07-29 16:04:44 +0200955}
956
Thierry Redingebd14af2014-12-08 16:22:28 +0100957static int
958tegra_dsi_encoder_atomic_check(struct drm_encoder *encoder,
959 struct drm_crtc_state *crtc_state,
960 struct drm_connector_state *conn_state)
961{
962 struct tegra_output *output = encoder_to_output(encoder);
963 struct tegra_dsi_state *state = to_dsi_state(conn_state);
964 struct tegra_dc *dc = to_tegra_dc(conn_state->crtc);
965 struct tegra_dsi *dsi = to_dsi(output);
966 unsigned int scdiv;
967 unsigned long plld;
968 int err;
969
970 state->pclk = crtc_state->mode.clock * 1000;
971
972 err = tegra_dsi_get_muldiv(dsi->format, &state->mul, &state->div);
973 if (err < 0)
974 return err;
975
976 state->lanes = tegra_dsi_get_lanes(dsi);
977
978 err = tegra_dsi_get_format(dsi->format, &state->format);
979 if (err < 0)
980 return err;
981
982 state->vrefresh = drm_mode_vrefresh(&crtc_state->mode);
983
984 /* compute byte clock */
985 state->bclk = (state->pclk * state->mul) / (state->div * state->lanes);
986
987 DRM_DEBUG_KMS("mul: %u, div: %u, lanes: %u\n", state->mul, state->div,
988 state->lanes);
989 DRM_DEBUG_KMS("format: %u, vrefresh: %u\n", state->format,
990 state->vrefresh);
991 DRM_DEBUG_KMS("bclk: %lu\n", state->bclk);
992
993 /*
994 * Compute bit clock and round up to the next MHz.
995 */
996 plld = DIV_ROUND_UP(state->bclk * 8, USEC_PER_SEC) * USEC_PER_SEC;
997 state->period = DIV_ROUND_CLOSEST(NSEC_PER_SEC, plld);
998
999 err = mipi_dphy_timing_get_default(&state->timing, state->period);
1000 if (err < 0)
1001 return err;
1002
1003 err = mipi_dphy_timing_validate(&state->timing, state->period);
1004 if (err < 0) {
1005 dev_err(dsi->dev, "failed to validate D-PHY timing: %d\n", err);
1006 return err;
1007 }
1008
1009 /*
1010 * We divide the frequency by two here, but we make up for that by
1011 * setting the shift clock divider (further below) to half of the
1012 * correct value.
1013 */
1014 plld /= 2;
1015
1016 /*
1017 * Derive pixel clock from bit clock using the shift clock divider.
1018 * Note that this is only half of what we would expect, but we need
1019 * that to make up for the fact that we divided the bit clock by a
1020 * factor of two above.
1021 *
1022 * It's not clear exactly why this is necessary, but the display is
1023 * not working properly otherwise. Perhaps the PLLs cannot generate
1024 * frequencies sufficiently high.
1025 */
1026 scdiv = ((8 * state->mul) / (state->div * state->lanes)) - 2;
1027
1028 err = tegra_dc_state_setup_clock(dc, crtc_state, dsi->clk_parent,
1029 plld, scdiv);
1030 if (err < 0) {
1031 dev_err(output->dev, "failed to setup CRTC state: %d\n", err);
1032 return err;
1033 }
1034
1035 return err;
1036}
1037
Thierry Reding5b901e72014-12-02 17:30:23 +01001038static const struct drm_encoder_helper_funcs tegra_dsi_encoder_helper_funcs = {
Thierry Reding5b901e72014-12-02 17:30:23 +01001039 .disable = tegra_dsi_encoder_disable,
Thierry Reding171e2e62015-07-29 16:04:44 +02001040 .enable = tegra_dsi_encoder_enable,
Thierry Redingebd14af2014-12-08 16:22:28 +01001041 .atomic_check = tegra_dsi_encoder_atomic_check,
Thierry Redingdec72732013-09-03 08:45:46 +02001042};
1043
Thierry Redingdec72732013-09-03 08:45:46 +02001044static int tegra_dsi_init(struct host1x_client *client)
1045{
Thierry Reding9910f5c2014-05-22 09:57:15 +02001046 struct drm_device *drm = dev_get_drvdata(client->parent);
Thierry Redingdec72732013-09-03 08:45:46 +02001047 struct tegra_dsi *dsi = host1x_client_to_dsi(client);
Thierry Redingdec72732013-09-03 08:45:46 +02001048 int err;
1049
Thierry Redinge94236c2014-10-07 16:10:24 +02001050 /* Gangsters must not register their own outputs. */
1051 if (!dsi->master) {
Thierry Redinge94236c2014-10-07 16:10:24 +02001052 dsi->output.dev = client->dev;
Thierry Redingdec72732013-09-03 08:45:46 +02001053
Thierry Reding5b901e72014-12-02 17:30:23 +01001054 drm_connector_init(drm, &dsi->output.connector,
1055 &tegra_dsi_connector_funcs,
1056 DRM_MODE_CONNECTOR_DSI);
1057 drm_connector_helper_add(&dsi->output.connector,
1058 &tegra_dsi_connector_helper_funcs);
1059 dsi->output.connector.dpms = DRM_MODE_DPMS_OFF;
1060
Thierry Reding5b901e72014-12-02 17:30:23 +01001061 drm_encoder_init(drm, &dsi->output.encoder,
1062 &tegra_dsi_encoder_funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +02001063 DRM_MODE_ENCODER_DSI, NULL);
Thierry Reding5b901e72014-12-02 17:30:23 +01001064 drm_encoder_helper_add(&dsi->output.encoder,
1065 &tegra_dsi_encoder_helper_funcs);
1066
1067 drm_mode_connector_attach_encoder(&dsi->output.connector,
1068 &dsi->output.encoder);
1069 drm_connector_register(&dsi->output.connector);
1070
Thierry Redingea130b22014-12-19 15:51:35 +01001071 err = tegra_output_init(drm, &dsi->output);
Thierry Redingef8187d2015-08-07 09:29:54 +02001072 if (err < 0)
1073 dev_err(dsi->dev, "failed to initialize output: %d\n",
Thierry Redingea130b22014-12-19 15:51:35 +01001074 err);
Thierry Redingea130b22014-12-19 15:51:35 +01001075
Thierry Reding5b901e72014-12-02 17:30:23 +01001076 dsi->output.encoder.possible_crtcs = 0x3;
Thierry Redingdec72732013-09-03 08:45:46 +02001077 }
1078
1079 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
Thierry Reding9910f5c2014-05-22 09:57:15 +02001080 err = tegra_dsi_debugfs_init(dsi, drm->primary);
Thierry Redingdec72732013-09-03 08:45:46 +02001081 if (err < 0)
1082 dev_err(dsi->dev, "debugfs setup failed: %d\n", err);
1083 }
1084
Thierry Redingdec72732013-09-03 08:45:46 +02001085 return 0;
1086}
1087
1088static int tegra_dsi_exit(struct host1x_client *client)
1089{
1090 struct tegra_dsi *dsi = host1x_client_to_dsi(client);
Thierry Redingdec72732013-09-03 08:45:46 +02001091
Thierry Reding5b901e72014-12-02 17:30:23 +01001092 tegra_output_exit(&dsi->output);
1093
Thierry Reding4009c222014-12-19 15:47:30 +01001094 if (IS_ENABLED(CONFIG_DEBUG_FS))
1095 tegra_dsi_debugfs_exit(dsi);
Thierry Redingdec72732013-09-03 08:45:46 +02001096
Thierry Redingef8187d2015-08-07 09:29:54 +02001097 regulator_disable(dsi->vdd);
Thierry Reding201106d2014-11-24 16:31:48 +01001098
Thierry Redingdec72732013-09-03 08:45:46 +02001099 return 0;
1100}
1101
1102static const struct host1x_client_ops dsi_client_ops = {
1103 .init = tegra_dsi_init,
1104 .exit = tegra_dsi_exit,
1105};
1106
1107static int tegra_dsi_setup_clocks(struct tegra_dsi *dsi)
1108{
1109 struct clk *parent;
1110 int err;
1111
1112 parent = clk_get_parent(dsi->clk);
1113 if (!parent)
1114 return -EINVAL;
1115
1116 err = clk_set_parent(parent, dsi->clk_parent);
1117 if (err < 0)
1118 return err;
1119
1120 return 0;
1121}
1122
Thierry Reding0fffdf62014-11-07 17:25:26 +01001123static const char * const error_report[16] = {
1124 "SoT Error",
1125 "SoT Sync Error",
1126 "EoT Sync Error",
1127 "Escape Mode Entry Command Error",
1128 "Low-Power Transmit Sync Error",
1129 "Peripheral Timeout Error",
1130 "False Control Error",
1131 "Contention Detected",
1132 "ECC Error, single-bit",
1133 "ECC Error, multi-bit",
1134 "Checksum Error",
1135 "DSI Data Type Not Recognized",
1136 "DSI VC ID Invalid",
1137 "Invalid Transmission Length",
1138 "Reserved",
1139 "DSI Protocol Violation",
1140};
1141
1142static ssize_t tegra_dsi_read_response(struct tegra_dsi *dsi,
1143 const struct mipi_dsi_msg *msg,
1144 size_t count)
1145{
1146 u8 *rx = msg->rx_buf;
1147 unsigned int i, j, k;
1148 size_t size = 0;
1149 u16 errors;
1150 u32 value;
1151
1152 /* read and parse packet header */
1153 value = tegra_dsi_readl(dsi, DSI_RD_DATA);
1154
1155 switch (value & 0x3f) {
1156 case MIPI_DSI_RX_ACKNOWLEDGE_AND_ERROR_REPORT:
1157 errors = (value >> 8) & 0xffff;
1158 dev_dbg(dsi->dev, "Acknowledge and error report: %04x\n",
1159 errors);
1160 for (i = 0; i < ARRAY_SIZE(error_report); i++)
1161 if (errors & BIT(i))
1162 dev_dbg(dsi->dev, " %2u: %s\n", i,
1163 error_report[i]);
1164 break;
1165
1166 case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_1BYTE:
1167 rx[0] = (value >> 8) & 0xff;
1168 size = 1;
1169 break;
1170
1171 case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_2BYTE:
1172 rx[0] = (value >> 8) & 0xff;
1173 rx[1] = (value >> 16) & 0xff;
1174 size = 2;
1175 break;
1176
1177 case MIPI_DSI_RX_DCS_LONG_READ_RESPONSE:
1178 size = ((value >> 8) & 0xff00) | ((value >> 8) & 0xff);
1179 break;
1180
1181 case MIPI_DSI_RX_GENERIC_LONG_READ_RESPONSE:
1182 size = ((value >> 8) & 0xff00) | ((value >> 8) & 0xff);
1183 break;
1184
1185 default:
1186 dev_err(dsi->dev, "unhandled response type: %02x\n",
1187 value & 0x3f);
1188 return -EPROTO;
1189 }
1190
1191 size = min(size, msg->rx_len);
1192
1193 if (msg->rx_buf && size > 0) {
1194 for (i = 0, j = 0; i < count - 1; i++, j += 4) {
1195 u8 *rx = msg->rx_buf + j;
1196
1197 value = tegra_dsi_readl(dsi, DSI_RD_DATA);
1198
1199 for (k = 0; k < 4 && (j + k) < msg->rx_len; k++)
1200 rx[j + k] = (value >> (k << 3)) & 0xff;
1201 }
1202 }
1203
1204 return size;
1205}
1206
1207static int tegra_dsi_transmit(struct tegra_dsi *dsi, unsigned long timeout)
1208{
1209 tegra_dsi_writel(dsi, DSI_TRIGGER_HOST, DSI_TRIGGER);
1210
1211 timeout = jiffies + msecs_to_jiffies(timeout);
1212
1213 while (time_before(jiffies, timeout)) {
1214 u32 value = tegra_dsi_readl(dsi, DSI_TRIGGER);
1215 if ((value & DSI_TRIGGER_HOST) == 0)
1216 return 0;
1217
1218 usleep_range(1000, 2000);
1219 }
1220
1221 DRM_DEBUG_KMS("timeout waiting for transmission to complete\n");
1222 return -ETIMEDOUT;
1223}
1224
1225static int tegra_dsi_wait_for_response(struct tegra_dsi *dsi,
1226 unsigned long timeout)
1227{
1228 timeout = jiffies + msecs_to_jiffies(250);
1229
1230 while (time_before(jiffies, timeout)) {
1231 u32 value = tegra_dsi_readl(dsi, DSI_STATUS);
1232 u8 count = value & 0x1f;
1233
1234 if (count > 0)
1235 return count;
1236
1237 usleep_range(1000, 2000);
1238 }
1239
1240 DRM_DEBUG_KMS("peripheral returned no data\n");
1241 return -ETIMEDOUT;
1242}
1243
1244static void tegra_dsi_writesl(struct tegra_dsi *dsi, unsigned long offset,
1245 const void *buffer, size_t size)
1246{
1247 const u8 *buf = buffer;
1248 size_t i, j;
1249 u32 value;
1250
1251 for (j = 0; j < size; j += 4) {
1252 value = 0;
1253
1254 for (i = 0; i < 4 && j + i < size; i++)
1255 value |= buf[j + i] << (i << 3);
1256
1257 tegra_dsi_writel(dsi, value, DSI_WR_DATA);
1258 }
1259}
1260
1261static ssize_t tegra_dsi_host_transfer(struct mipi_dsi_host *host,
1262 const struct mipi_dsi_msg *msg)
1263{
1264 struct tegra_dsi *dsi = host_to_tegra(host);
1265 struct mipi_dsi_packet packet;
1266 const u8 *header;
1267 size_t count;
1268 ssize_t err;
1269 u32 value;
1270
1271 err = mipi_dsi_create_packet(&packet, msg);
1272 if (err < 0)
1273 return err;
1274
1275 header = packet.header;
1276
1277 /* maximum FIFO depth is 1920 words */
1278 if (packet.size > dsi->video_fifo_depth * 4)
1279 return -ENOSPC;
1280
1281 /* reset underflow/overflow flags */
1282 value = tegra_dsi_readl(dsi, DSI_STATUS);
1283 if (value & (DSI_STATUS_UNDERFLOW | DSI_STATUS_OVERFLOW)) {
1284 value = DSI_HOST_CONTROL_FIFO_RESET;
1285 tegra_dsi_writel(dsi, value, DSI_HOST_CONTROL);
1286 usleep_range(10, 20);
1287 }
1288
1289 value = tegra_dsi_readl(dsi, DSI_POWER_CONTROL);
1290 value |= DSI_POWER_CONTROL_ENABLE;
1291 tegra_dsi_writel(dsi, value, DSI_POWER_CONTROL);
1292
1293 usleep_range(5000, 10000);
1294
1295 value = DSI_HOST_CONTROL_CRC_RESET | DSI_HOST_CONTROL_TX_TRIG_HOST |
1296 DSI_HOST_CONTROL_CS | DSI_HOST_CONTROL_ECC;
1297
1298 if ((msg->flags & MIPI_DSI_MSG_USE_LPM) == 0)
1299 value |= DSI_HOST_CONTROL_HS;
1300
1301 /*
1302 * The host FIFO has a maximum of 64 words, so larger transmissions
1303 * need to use the video FIFO.
1304 */
1305 if (packet.size > dsi->host_fifo_depth * 4)
1306 value |= DSI_HOST_CONTROL_FIFO_SEL;
1307
1308 tegra_dsi_writel(dsi, value, DSI_HOST_CONTROL);
1309
1310 /*
1311 * For reads and messages with explicitly requested ACK, generate a
1312 * BTA sequence after the transmission of the packet.
1313 */
1314 if ((msg->flags & MIPI_DSI_MSG_REQ_ACK) ||
1315 (msg->rx_buf && msg->rx_len > 0)) {
1316 value = tegra_dsi_readl(dsi, DSI_HOST_CONTROL);
1317 value |= DSI_HOST_CONTROL_PKT_BTA;
1318 tegra_dsi_writel(dsi, value, DSI_HOST_CONTROL);
1319 }
1320
1321 value = DSI_CONTROL_LANES(0) | DSI_CONTROL_HOST_ENABLE;
1322 tegra_dsi_writel(dsi, value, DSI_CONTROL);
1323
1324 /* write packet header, ECC is generated by hardware */
1325 value = header[2] << 16 | header[1] << 8 | header[0];
1326 tegra_dsi_writel(dsi, value, DSI_WR_DATA);
1327
1328 /* write payload (if any) */
1329 if (packet.payload_length > 0)
1330 tegra_dsi_writesl(dsi, DSI_WR_DATA, packet.payload,
1331 packet.payload_length);
1332
1333 err = tegra_dsi_transmit(dsi, 250);
1334 if (err < 0)
1335 return err;
1336
1337 if ((msg->flags & MIPI_DSI_MSG_REQ_ACK) ||
1338 (msg->rx_buf && msg->rx_len > 0)) {
1339 err = tegra_dsi_wait_for_response(dsi, 250);
1340 if (err < 0)
1341 return err;
1342
1343 count = err;
1344
1345 value = tegra_dsi_readl(dsi, DSI_RD_DATA);
1346 switch (value) {
1347 case 0x84:
1348 /*
1349 dev_dbg(dsi->dev, "ACK\n");
1350 */
1351 break;
1352
1353 case 0x87:
1354 /*
1355 dev_dbg(dsi->dev, "ESCAPE\n");
1356 */
1357 break;
1358
1359 default:
1360 dev_err(dsi->dev, "unknown status: %08x\n", value);
1361 break;
1362 }
1363
1364 if (count > 1) {
1365 err = tegra_dsi_read_response(dsi, msg, count);
1366 if (err < 0)
1367 dev_err(dsi->dev,
1368 "failed to parse response: %zd\n",
1369 err);
1370 else {
1371 /*
1372 * For read commands, return the number of
1373 * bytes returned by the peripheral.
1374 */
1375 count = err;
1376 }
1377 }
1378 } else {
1379 /*
1380 * For write commands, we have transmitted the 4-byte header
1381 * plus the variable-length payload.
1382 */
1383 count = 4 + packet.payload_length;
1384 }
1385
1386 return count;
1387}
1388
Thierry Redinge94236c2014-10-07 16:10:24 +02001389static int tegra_dsi_ganged_setup(struct tegra_dsi *dsi)
1390{
1391 struct clk *parent;
1392 int err;
1393
1394 /* make sure both DSI controllers share the same PLL */
1395 parent = clk_get_parent(dsi->slave->clk);
1396 if (!parent)
1397 return -EINVAL;
1398
1399 err = clk_set_parent(parent, dsi->clk_parent);
1400 if (err < 0)
1401 return err;
1402
1403 return 0;
1404}
1405
Thierry Redingdec72732013-09-03 08:45:46 +02001406static int tegra_dsi_host_attach(struct mipi_dsi_host *host,
1407 struct mipi_dsi_device *device)
1408{
1409 struct tegra_dsi *dsi = host_to_tegra(host);
Thierry Redingdec72732013-09-03 08:45:46 +02001410
Thierry Reding17297a22014-03-14 14:13:15 +01001411 dsi->flags = device->mode_flags;
Thierry Redingdec72732013-09-03 08:45:46 +02001412 dsi->format = device->format;
1413 dsi->lanes = device->lanes;
1414
Thierry Redinge94236c2014-10-07 16:10:24 +02001415 if (dsi->slave) {
1416 int err;
1417
1418 dev_dbg(dsi->dev, "attaching dual-channel device %s\n",
1419 dev_name(&device->dev));
1420
1421 err = tegra_dsi_ganged_setup(dsi);
1422 if (err < 0) {
1423 dev_err(dsi->dev, "failed to set up ganged mode: %d\n",
1424 err);
1425 return err;
1426 }
1427 }
1428
1429 /*
1430 * Slaves don't have a panel associated with them, so they provide
1431 * merely the second channel.
1432 */
1433 if (!dsi->master) {
1434 struct tegra_output *output = &dsi->output;
1435
1436 output->panel = of_drm_find_panel(device->dev.of_node);
1437 if (output->panel && output->connector.dev) {
1438 drm_panel_attach(output->panel, &output->connector);
Thierry Redingdec72732013-09-03 08:45:46 +02001439 drm_helper_hpd_irq_event(output->connector.dev);
Thierry Redinge94236c2014-10-07 16:10:24 +02001440 }
Thierry Redingdec72732013-09-03 08:45:46 +02001441 }
1442
1443 return 0;
1444}
1445
1446static int tegra_dsi_host_detach(struct mipi_dsi_host *host,
1447 struct mipi_dsi_device *device)
1448{
1449 struct tegra_dsi *dsi = host_to_tegra(host);
1450 struct tegra_output *output = &dsi->output;
1451
1452 if (output->panel && &device->dev == output->panel->dev) {
Thierry Redingba3df972014-11-13 14:54:01 +01001453 output->panel = NULL;
1454
Thierry Redingdec72732013-09-03 08:45:46 +02001455 if (output->connector.dev)
1456 drm_helper_hpd_irq_event(output->connector.dev);
Thierry Redingdec72732013-09-03 08:45:46 +02001457 }
1458
1459 return 0;
1460}
1461
1462static const struct mipi_dsi_host_ops tegra_dsi_host_ops = {
1463 .attach = tegra_dsi_host_attach,
1464 .detach = tegra_dsi_host_detach,
Thierry Reding0fffdf62014-11-07 17:25:26 +01001465 .transfer = tegra_dsi_host_transfer,
Thierry Redingdec72732013-09-03 08:45:46 +02001466};
1467
Thierry Redinge94236c2014-10-07 16:10:24 +02001468static int tegra_dsi_ganged_probe(struct tegra_dsi *dsi)
1469{
1470 struct device_node *np;
1471
1472 np = of_parse_phandle(dsi->dev->of_node, "nvidia,ganged-mode", 0);
1473 if (np) {
1474 struct platform_device *gangster = of_find_device_by_node(np);
1475
1476 dsi->slave = platform_get_drvdata(gangster);
1477 of_node_put(np);
1478
1479 if (!dsi->slave)
1480 return -EPROBE_DEFER;
1481
1482 dsi->slave->master = dsi;
1483 }
1484
1485 return 0;
1486}
1487
Thierry Redingdec72732013-09-03 08:45:46 +02001488static int tegra_dsi_probe(struct platform_device *pdev)
1489{
1490 struct tegra_dsi *dsi;
1491 struct resource *regs;
1492 int err;
1493
1494 dsi = devm_kzalloc(&pdev->dev, sizeof(*dsi), GFP_KERNEL);
1495 if (!dsi)
1496 return -ENOMEM;
1497
1498 dsi->output.dev = dsi->dev = &pdev->dev;
Thierry Reding976cebc2014-08-06 09:14:28 +02001499 dsi->video_fifo_depth = 1920;
1500 dsi->host_fifo_depth = 64;
Thierry Redingdec72732013-09-03 08:45:46 +02001501
Thierry Redinge94236c2014-10-07 16:10:24 +02001502 err = tegra_dsi_ganged_probe(dsi);
1503 if (err < 0)
1504 return err;
1505
Thierry Redingdec72732013-09-03 08:45:46 +02001506 err = tegra_output_probe(&dsi->output);
1507 if (err < 0)
1508 return err;
1509
Thierry Redingba3df972014-11-13 14:54:01 +01001510 dsi->output.connector.polled = DRM_CONNECTOR_POLL_HPD;
1511
Thierry Redingdec72732013-09-03 08:45:46 +02001512 /*
1513 * Assume these values by default. When a DSI peripheral driver
1514 * attaches to the DSI host, the parameters will be taken from
1515 * the attached device.
1516 */
Thierry Reding17297a22014-03-14 14:13:15 +01001517 dsi->flags = MIPI_DSI_MODE_VIDEO;
Thierry Redingdec72732013-09-03 08:45:46 +02001518 dsi->format = MIPI_DSI_FMT_RGB888;
1519 dsi->lanes = 4;
1520
Jon Hunter64230aa2016-07-01 14:21:37 +01001521 if (!pdev->dev.pm_domain) {
1522 dsi->rst = devm_reset_control_get(&pdev->dev, "dsi");
1523 if (IS_ERR(dsi->rst))
1524 return PTR_ERR(dsi->rst);
1525 }
Thierry Redingdec72732013-09-03 08:45:46 +02001526
1527 dsi->clk = devm_clk_get(&pdev->dev, NULL);
1528 if (IS_ERR(dsi->clk)) {
1529 dev_err(&pdev->dev, "cannot get DSI clock\n");
Thierry Redingef8187d2015-08-07 09:29:54 +02001530 return PTR_ERR(dsi->clk);
Thierry Redingdec72732013-09-03 08:45:46 +02001531 }
1532
1533 dsi->clk_lp = devm_clk_get(&pdev->dev, "lp");
1534 if (IS_ERR(dsi->clk_lp)) {
1535 dev_err(&pdev->dev, "cannot get low-power clock\n");
Thierry Redingef8187d2015-08-07 09:29:54 +02001536 return PTR_ERR(dsi->clk_lp);
Thierry Redingdec72732013-09-03 08:45:46 +02001537 }
1538
1539 dsi->clk_parent = devm_clk_get(&pdev->dev, "parent");
1540 if (IS_ERR(dsi->clk_parent)) {
1541 dev_err(&pdev->dev, "cannot get parent clock\n");
Thierry Redingef8187d2015-08-07 09:29:54 +02001542 return PTR_ERR(dsi->clk_parent);
Thierry Redingdec72732013-09-03 08:45:46 +02001543 }
1544
Thierry Reding3b077af2014-03-14 14:07:50 +01001545 dsi->vdd = devm_regulator_get(&pdev->dev, "avdd-dsi-csi");
1546 if (IS_ERR(dsi->vdd)) {
1547 dev_err(&pdev->dev, "cannot get VDD supply\n");
Thierry Redingef8187d2015-08-07 09:29:54 +02001548 return PTR_ERR(dsi->vdd);
Thierry Reding3b077af2014-03-14 14:07:50 +01001549 }
1550
Thierry Redingdec72732013-09-03 08:45:46 +02001551 err = tegra_dsi_setup_clocks(dsi);
1552 if (err < 0) {
1553 dev_err(&pdev->dev, "cannot setup clocks\n");
Thierry Redingef8187d2015-08-07 09:29:54 +02001554 return err;
Thierry Redingdec72732013-09-03 08:45:46 +02001555 }
1556
1557 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1558 dsi->regs = devm_ioremap_resource(&pdev->dev, regs);
Thierry Redingef8187d2015-08-07 09:29:54 +02001559 if (IS_ERR(dsi->regs))
1560 return PTR_ERR(dsi->regs);
Thierry Redingdec72732013-09-03 08:45:46 +02001561
Thierry Redingdec72732013-09-03 08:45:46 +02001562 dsi->mipi = tegra_mipi_request(&pdev->dev);
Thierry Redingef8187d2015-08-07 09:29:54 +02001563 if (IS_ERR(dsi->mipi))
1564 return PTR_ERR(dsi->mipi);
Thierry Redingdec72732013-09-03 08:45:46 +02001565
1566 dsi->host.ops = &tegra_dsi_host_ops;
1567 dsi->host.dev = &pdev->dev;
1568
1569 err = mipi_dsi_host_register(&dsi->host);
1570 if (err < 0) {
1571 dev_err(&pdev->dev, "failed to register DSI host: %d\n", err);
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001572 goto mipi_free;
Thierry Redingdec72732013-09-03 08:45:46 +02001573 }
1574
Thierry Redingef8187d2015-08-07 09:29:54 +02001575 platform_set_drvdata(pdev, dsi);
1576 pm_runtime_enable(&pdev->dev);
1577
Thierry Redingdec72732013-09-03 08:45:46 +02001578 INIT_LIST_HEAD(&dsi->client.list);
1579 dsi->client.ops = &dsi_client_ops;
1580 dsi->client.dev = &pdev->dev;
1581
1582 err = host1x_client_register(&dsi->client);
1583 if (err < 0) {
1584 dev_err(&pdev->dev, "failed to register host1x client: %d\n",
1585 err);
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001586 goto unregister;
Thierry Redingdec72732013-09-03 08:45:46 +02001587 }
1588
Thierry Redingdec72732013-09-03 08:45:46 +02001589 return 0;
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001590
1591unregister:
1592 mipi_dsi_host_unregister(&dsi->host);
1593mipi_free:
1594 tegra_mipi_free(dsi->mipi);
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001595 return err;
Thierry Redingdec72732013-09-03 08:45:46 +02001596}
1597
1598static int tegra_dsi_remove(struct platform_device *pdev)
1599{
1600 struct tegra_dsi *dsi = platform_get_drvdata(pdev);
1601 int err;
1602
Thierry Redingef8187d2015-08-07 09:29:54 +02001603 pm_runtime_disable(&pdev->dev);
1604
Thierry Redingdec72732013-09-03 08:45:46 +02001605 err = host1x_client_unregister(&dsi->client);
1606 if (err < 0) {
1607 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
1608 err);
1609 return err;
1610 }
1611
Thierry Reding328ec692014-12-19 15:55:08 +01001612 tegra_output_remove(&dsi->output);
Thierry Reding5b901e72014-12-02 17:30:23 +01001613
Thierry Redingdec72732013-09-03 08:45:46 +02001614 mipi_dsi_host_unregister(&dsi->host);
1615 tegra_mipi_free(dsi->mipi);
1616
Thierry Redingef8187d2015-08-07 09:29:54 +02001617 return 0;
1618}
1619
1620#ifdef CONFIG_PM
1621static int tegra_dsi_suspend(struct device *dev)
1622{
1623 struct tegra_dsi *dsi = dev_get_drvdata(dev);
1624 int err;
1625
Jon Hunter64230aa2016-07-01 14:21:37 +01001626 if (dsi->rst) {
1627 err = reset_control_assert(dsi->rst);
1628 if (err < 0) {
1629 dev_err(dev, "failed to assert reset: %d\n", err);
1630 return err;
1631 }
Thierry Redingef8187d2015-08-07 09:29:54 +02001632 }
1633
1634 usleep_range(1000, 2000);
1635
Thierry Redingdec72732013-09-03 08:45:46 +02001636 clk_disable_unprepare(dsi->clk_lp);
1637 clk_disable_unprepare(dsi->clk);
Thierry Redingef8187d2015-08-07 09:29:54 +02001638
1639 regulator_disable(dsi->vdd);
Thierry Redingdec72732013-09-03 08:45:46 +02001640
Thierry Redingdec72732013-09-03 08:45:46 +02001641 return 0;
1642}
1643
Thierry Redingef8187d2015-08-07 09:29:54 +02001644static int tegra_dsi_resume(struct device *dev)
1645{
1646 struct tegra_dsi *dsi = dev_get_drvdata(dev);
1647 int err;
1648
1649 err = regulator_enable(dsi->vdd);
1650 if (err < 0) {
1651 dev_err(dsi->dev, "failed to enable VDD supply: %d\n", err);
1652 return err;
1653 }
1654
1655 err = clk_prepare_enable(dsi->clk);
1656 if (err < 0) {
1657 dev_err(dev, "cannot enable DSI clock: %d\n", err);
1658 goto disable_vdd;
1659 }
1660
1661 err = clk_prepare_enable(dsi->clk_lp);
1662 if (err < 0) {
1663 dev_err(dev, "cannot enable low-power clock: %d\n", err);
1664 goto disable_clk;
1665 }
1666
1667 usleep_range(1000, 2000);
1668
Jon Hunter64230aa2016-07-01 14:21:37 +01001669 if (dsi->rst) {
1670 err = reset_control_deassert(dsi->rst);
1671 if (err < 0) {
1672 dev_err(dev, "cannot assert reset: %d\n", err);
1673 goto disable_clk_lp;
1674 }
Thierry Redingef8187d2015-08-07 09:29:54 +02001675 }
1676
1677 return 0;
1678
1679disable_clk_lp:
1680 clk_disable_unprepare(dsi->clk_lp);
1681disable_clk:
1682 clk_disable_unprepare(dsi->clk);
1683disable_vdd:
1684 regulator_disable(dsi->vdd);
1685 return err;
1686}
1687#endif
1688
1689static const struct dev_pm_ops tegra_dsi_pm_ops = {
1690 SET_RUNTIME_PM_OPS(tegra_dsi_suspend, tegra_dsi_resume, NULL)
1691};
1692
Thierry Redingdec72732013-09-03 08:45:46 +02001693static const struct of_device_id tegra_dsi_of_match[] = {
Thierry Redingddfb4062015-04-08 16:56:22 +02001694 { .compatible = "nvidia,tegra210-dsi", },
Thierry Redingc06c7932015-04-10 11:35:21 +02001695 { .compatible = "nvidia,tegra132-dsi", },
Thierry Reding7d338582015-04-10 11:35:21 +02001696 { .compatible = "nvidia,tegra124-dsi", },
Thierry Redingdec72732013-09-03 08:45:46 +02001697 { .compatible = "nvidia,tegra114-dsi", },
1698 { },
1699};
Stephen Warrenef707282014-06-18 16:21:55 -06001700MODULE_DEVICE_TABLE(of, tegra_dsi_of_match);
Thierry Redingdec72732013-09-03 08:45:46 +02001701
1702struct platform_driver tegra_dsi_driver = {
1703 .driver = {
1704 .name = "tegra-dsi",
1705 .of_match_table = tegra_dsi_of_match,
Thierry Redingef8187d2015-08-07 09:29:54 +02001706 .pm = &tegra_dsi_pm_ops,
Thierry Redingdec72732013-09-03 08:45:46 +02001707 },
1708 .probe = tegra_dsi_probe,
1709 .remove = tegra_dsi_remove,
1710};