blob: 4d2ed966f9e3248a074ab25dcdfb6156e9c93200 [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;
Thierry Redingdec72732013-09-03 08:45:46 +020068
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 Reding12831072017-08-15 15:41:07 +0200107static inline u32 tegra_dsi_readl(struct tegra_dsi *dsi, unsigned int offset)
Thierry Redingdec72732013-09-03 08:45:46 +0200108{
Thierry Reding75af8fa2017-08-15 15:41:12 +0200109 u32 value = readl(dsi->regs + (offset << 2));
110
111 trace_dsi_readl(dsi->dev, offset, value);
112
113 return value;
Thierry Redingdec72732013-09-03 08:45:46 +0200114}
115
Thierry Reding9c0b4ca2014-11-24 12:27:59 +0100116static inline void tegra_dsi_writel(struct tegra_dsi *dsi, u32 value,
Thierry Reding12831072017-08-15 15:41:07 +0200117 unsigned int offset)
Thierry Redingdec72732013-09-03 08:45:46 +0200118{
Thierry Reding75af8fa2017-08-15 15:41:12 +0200119 trace_dsi_writel(dsi->dev, offset, value);
Thierry Reding12831072017-08-15 15:41:07 +0200120 writel(value, dsi->regs + (offset << 2));
Thierry Redingdec72732013-09-03 08:45:46 +0200121}
122
Thierry Redinga40051c2017-11-10 12:18:22 +0100123#define DEBUGFS_REG32(_name) { .name = #_name, .offset = _name }
124
125static const struct debugfs_reg32 tegra_dsi_regs[] = {
126 DEBUGFS_REG32(DSI_INCR_SYNCPT),
127 DEBUGFS_REG32(DSI_INCR_SYNCPT_CONTROL),
128 DEBUGFS_REG32(DSI_INCR_SYNCPT_ERROR),
129 DEBUGFS_REG32(DSI_CTXSW),
130 DEBUGFS_REG32(DSI_RD_DATA),
131 DEBUGFS_REG32(DSI_WR_DATA),
132 DEBUGFS_REG32(DSI_POWER_CONTROL),
133 DEBUGFS_REG32(DSI_INT_ENABLE),
134 DEBUGFS_REG32(DSI_INT_STATUS),
135 DEBUGFS_REG32(DSI_INT_MASK),
136 DEBUGFS_REG32(DSI_HOST_CONTROL),
137 DEBUGFS_REG32(DSI_CONTROL),
138 DEBUGFS_REG32(DSI_SOL_DELAY),
139 DEBUGFS_REG32(DSI_MAX_THRESHOLD),
140 DEBUGFS_REG32(DSI_TRIGGER),
141 DEBUGFS_REG32(DSI_TX_CRC),
142 DEBUGFS_REG32(DSI_STATUS),
143 DEBUGFS_REG32(DSI_INIT_SEQ_CONTROL),
144 DEBUGFS_REG32(DSI_INIT_SEQ_DATA_0),
145 DEBUGFS_REG32(DSI_INIT_SEQ_DATA_1),
146 DEBUGFS_REG32(DSI_INIT_SEQ_DATA_2),
147 DEBUGFS_REG32(DSI_INIT_SEQ_DATA_3),
148 DEBUGFS_REG32(DSI_INIT_SEQ_DATA_4),
149 DEBUGFS_REG32(DSI_INIT_SEQ_DATA_5),
150 DEBUGFS_REG32(DSI_INIT_SEQ_DATA_6),
151 DEBUGFS_REG32(DSI_INIT_SEQ_DATA_7),
152 DEBUGFS_REG32(DSI_PKT_SEQ_0_LO),
153 DEBUGFS_REG32(DSI_PKT_SEQ_0_HI),
154 DEBUGFS_REG32(DSI_PKT_SEQ_1_LO),
155 DEBUGFS_REG32(DSI_PKT_SEQ_1_HI),
156 DEBUGFS_REG32(DSI_PKT_SEQ_2_LO),
157 DEBUGFS_REG32(DSI_PKT_SEQ_2_HI),
158 DEBUGFS_REG32(DSI_PKT_SEQ_3_LO),
159 DEBUGFS_REG32(DSI_PKT_SEQ_3_HI),
160 DEBUGFS_REG32(DSI_PKT_SEQ_4_LO),
161 DEBUGFS_REG32(DSI_PKT_SEQ_4_HI),
162 DEBUGFS_REG32(DSI_PKT_SEQ_5_LO),
163 DEBUGFS_REG32(DSI_PKT_SEQ_5_HI),
164 DEBUGFS_REG32(DSI_DCS_CMDS),
165 DEBUGFS_REG32(DSI_PKT_LEN_0_1),
166 DEBUGFS_REG32(DSI_PKT_LEN_2_3),
167 DEBUGFS_REG32(DSI_PKT_LEN_4_5),
168 DEBUGFS_REG32(DSI_PKT_LEN_6_7),
169 DEBUGFS_REG32(DSI_PHY_TIMING_0),
170 DEBUGFS_REG32(DSI_PHY_TIMING_1),
171 DEBUGFS_REG32(DSI_PHY_TIMING_2),
172 DEBUGFS_REG32(DSI_BTA_TIMING),
173 DEBUGFS_REG32(DSI_TIMEOUT_0),
174 DEBUGFS_REG32(DSI_TIMEOUT_1),
175 DEBUGFS_REG32(DSI_TO_TALLY),
176 DEBUGFS_REG32(DSI_PAD_CONTROL_0),
177 DEBUGFS_REG32(DSI_PAD_CONTROL_CD),
178 DEBUGFS_REG32(DSI_PAD_CD_STATUS),
179 DEBUGFS_REG32(DSI_VIDEO_MODE_CONTROL),
180 DEBUGFS_REG32(DSI_PAD_CONTROL_1),
181 DEBUGFS_REG32(DSI_PAD_CONTROL_2),
182 DEBUGFS_REG32(DSI_PAD_CONTROL_3),
183 DEBUGFS_REG32(DSI_PAD_CONTROL_4),
184 DEBUGFS_REG32(DSI_GANGED_MODE_CONTROL),
185 DEBUGFS_REG32(DSI_GANGED_MODE_START),
186 DEBUGFS_REG32(DSI_GANGED_MODE_SIZE),
187 DEBUGFS_REG32(DSI_RAW_DATA_BYTE_COUNT),
188 DEBUGFS_REG32(DSI_ULTRA_LOW_POWER_CONTROL),
189 DEBUGFS_REG32(DSI_INIT_SEQ_DATA_8),
190 DEBUGFS_REG32(DSI_INIT_SEQ_DATA_9),
191 DEBUGFS_REG32(DSI_INIT_SEQ_DATA_10),
192 DEBUGFS_REG32(DSI_INIT_SEQ_DATA_11),
193 DEBUGFS_REG32(DSI_INIT_SEQ_DATA_12),
194 DEBUGFS_REG32(DSI_INIT_SEQ_DATA_13),
195 DEBUGFS_REG32(DSI_INIT_SEQ_DATA_14),
196 DEBUGFS_REG32(DSI_INIT_SEQ_DATA_15),
197};
198
Thierry Redingdec72732013-09-03 08:45:46 +0200199static int tegra_dsi_show_regs(struct seq_file *s, void *data)
200{
201 struct drm_info_node *node = s->private;
202 struct tegra_dsi *dsi = node->info_ent->data;
Thierry Reding171e2e62015-07-29 16:04:44 +0200203 struct drm_crtc *crtc = dsi->output.encoder.crtc;
204 struct drm_device *drm = node->minor->dev;
Thierry Redinga40051c2017-11-10 12:18:22 +0100205 unsigned int i;
Thierry Reding171e2e62015-07-29 16:04:44 +0200206 int err = 0;
207
208 drm_modeset_lock_all(drm);
209
210 if (!crtc || !crtc->state->active) {
211 err = -EBUSY;
212 goto unlock;
213 }
Thierry Redingdec72732013-09-03 08:45:46 +0200214
Thierry Redinga40051c2017-11-10 12:18:22 +0100215 for (i = 0; i < ARRAY_SIZE(tegra_dsi_regs); i++) {
216 unsigned int offset = tegra_dsi_regs[i].offset;
Thierry Redingdec72732013-09-03 08:45:46 +0200217
Thierry Redinga40051c2017-11-10 12:18:22 +0100218 seq_printf(s, "%-32s %#05x %08x\n", tegra_dsi_regs[i].name,
219 offset, tegra_dsi_readl(dsi, offset));
220 }
Thierry Redingdec72732013-09-03 08:45:46 +0200221
Thierry Reding171e2e62015-07-29 16:04:44 +0200222unlock:
223 drm_modeset_unlock_all(drm);
224 return err;
Thierry Redingdec72732013-09-03 08:45:46 +0200225}
226
227static struct drm_info_list debugfs_files[] = {
228 { "regs", tegra_dsi_show_regs, 0, NULL },
229};
230
Thierry Redinga813d702017-11-08 13:12:44 +0100231static int tegra_dsi_late_register(struct drm_connector *connector)
Thierry Redingdec72732013-09-03 08:45:46 +0200232{
Thierry Redinga813d702017-11-08 13:12:44 +0100233 struct tegra_output *output = connector_to_output(connector);
234 unsigned int i, count = ARRAY_SIZE(debugfs_files);
235 struct drm_minor *minor = connector->dev->primary;
236 struct dentry *root = connector->debugfs_entry;
237 struct tegra_dsi *dsi = to_dsi(output);
Thierry Redingdec72732013-09-03 08:45:46 +0200238 int err;
239
Thierry Redingdec72732013-09-03 08:45:46 +0200240 dsi->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
241 GFP_KERNEL);
Thierry Redinga813d702017-11-08 13:12:44 +0100242 if (!dsi->debugfs_files)
243 return -ENOMEM;
Thierry Redingdec72732013-09-03 08:45:46 +0200244
Thierry Redinga813d702017-11-08 13:12:44 +0100245 for (i = 0; i < count; i++)
Thierry Redingdec72732013-09-03 08:45:46 +0200246 dsi->debugfs_files[i].data = dsi;
247
Thierry Redinga813d702017-11-08 13:12:44 +0100248 err = drm_debugfs_create_files(dsi->debugfs_files, count, root, minor);
Thierry Redingdec72732013-09-03 08:45:46 +0200249 if (err < 0)
250 goto free;
251
Thierry Redingdec72732013-09-03 08:45:46 +0200252 return 0;
253
254free:
255 kfree(dsi->debugfs_files);
256 dsi->debugfs_files = NULL;
Thierry Redingdec72732013-09-03 08:45:46 +0200257
258 return err;
259}
260
Thierry Redinga813d702017-11-08 13:12:44 +0100261static void tegra_dsi_early_unregister(struct drm_connector *connector)
Thierry Redingdec72732013-09-03 08:45:46 +0200262{
Thierry Redinga813d702017-11-08 13:12:44 +0100263 struct tegra_output *output = connector_to_output(connector);
264 unsigned int count = ARRAY_SIZE(debugfs_files);
265 struct tegra_dsi *dsi = to_dsi(output);
Thierry Redingdec72732013-09-03 08:45:46 +0200266
Thierry Redinga813d702017-11-08 13:12:44 +0100267 drm_debugfs_remove_files(dsi->debugfs_files, count,
268 connector->dev->primary);
Thierry Redingdec72732013-09-03 08:45:46 +0200269 kfree(dsi->debugfs_files);
270 dsi->debugfs_files = NULL;
Thierry Redingdec72732013-09-03 08:45:46 +0200271}
272
273#define PKT_ID0(id) ((((id) & 0x3f) << 3) | (1 << 9))
274#define PKT_LEN0(len) (((len) & 0x07) << 0)
275#define PKT_ID1(id) ((((id) & 0x3f) << 13) | (1 << 19))
276#define PKT_LEN1(len) (((len) & 0x07) << 10)
277#define PKT_ID2(id) ((((id) & 0x3f) << 23) | (1 << 29))
278#define PKT_LEN2(len) (((len) & 0x07) << 20)
279
280#define PKT_LP (1 << 30)
281#define NUM_PKT_SEQ 12
282
Thierry Reding17297a22014-03-14 14:13:15 +0100283/*
284 * non-burst mode with sync pulses
285 */
286static const u32 pkt_seq_video_non_burst_sync_pulses[NUM_PKT_SEQ] = {
Thierry Redingdec72732013-09-03 08:45:46 +0200287 [ 0] = PKT_ID0(MIPI_DSI_V_SYNC_START) | PKT_LEN0(0) |
288 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
289 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0) |
290 PKT_LP,
291 [ 1] = 0,
292 [ 2] = PKT_ID0(MIPI_DSI_V_SYNC_END) | PKT_LEN0(0) |
293 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
294 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0) |
295 PKT_LP,
296 [ 3] = 0,
297 [ 4] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
298 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
299 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0) |
300 PKT_LP,
301 [ 5] = 0,
302 [ 6] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
303 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
304 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0),
305 [ 7] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(2) |
306 PKT_ID1(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN1(3) |
307 PKT_ID2(MIPI_DSI_BLANKING_PACKET) | PKT_LEN2(4),
308 [ 8] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
309 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
310 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0) |
311 PKT_LP,
312 [ 9] = 0,
313 [10] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
314 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
315 PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0),
316 [11] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(2) |
317 PKT_ID1(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN1(3) |
318 PKT_ID2(MIPI_DSI_BLANKING_PACKET) | PKT_LEN2(4),
319};
320
Thierry Reding17297a22014-03-14 14:13:15 +0100321/*
322 * non-burst mode with sync events
323 */
324static const u32 pkt_seq_video_non_burst_sync_events[NUM_PKT_SEQ] = {
325 [ 0] = PKT_ID0(MIPI_DSI_V_SYNC_START) | PKT_LEN0(0) |
326 PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION) | PKT_LEN1(7) |
327 PKT_LP,
328 [ 1] = 0,
329 [ 2] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
330 PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION) | PKT_LEN1(7) |
331 PKT_LP,
332 [ 3] = 0,
333 [ 4] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
334 PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION) | PKT_LEN1(7) |
335 PKT_LP,
336 [ 5] = 0,
337 [ 6] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
338 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(2) |
339 PKT_ID2(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN2(3),
340 [ 7] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(4),
341 [ 8] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
342 PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION) | PKT_LEN1(7) |
343 PKT_LP,
344 [ 9] = 0,
345 [10] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
346 PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(2) |
347 PKT_ID2(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN2(3),
348 [11] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(4),
349};
350
Thierry Reding337b4432014-11-13 15:02:46 +0100351static const u32 pkt_seq_command_mode[NUM_PKT_SEQ] = {
352 [ 0] = 0,
353 [ 1] = 0,
354 [ 2] = 0,
355 [ 3] = 0,
356 [ 4] = 0,
357 [ 5] = 0,
358 [ 6] = PKT_ID0(MIPI_DSI_DCS_LONG_WRITE) | PKT_LEN0(3) | PKT_LP,
359 [ 7] = 0,
360 [ 8] = 0,
361 [ 9] = 0,
362 [10] = PKT_ID0(MIPI_DSI_DCS_LONG_WRITE) | PKT_LEN0(5) | PKT_LP,
363 [11] = 0,
364};
365
Thierry Redingebd14af2014-12-08 16:22:28 +0100366static void tegra_dsi_set_phy_timing(struct tegra_dsi *dsi,
367 unsigned long period,
368 const struct mipi_dphy_timing *timing)
Thierry Redingdec72732013-09-03 08:45:46 +0200369{
Thierry Reding9c0b4ca2014-11-24 12:27:59 +0100370 u32 value;
Thierry Redingdec72732013-09-03 08:45:46 +0200371
Thierry Redingebd14af2014-12-08 16:22:28 +0100372 value = DSI_TIMING_FIELD(timing->hsexit, period, 1) << 24 |
373 DSI_TIMING_FIELD(timing->hstrail, period, 0) << 16 |
374 DSI_TIMING_FIELD(timing->hszero, period, 3) << 8 |
375 DSI_TIMING_FIELD(timing->hsprepare, period, 1);
Thierry Redingdec72732013-09-03 08:45:46 +0200376 tegra_dsi_writel(dsi, value, DSI_PHY_TIMING_0);
377
Thierry Redingebd14af2014-12-08 16:22:28 +0100378 value = DSI_TIMING_FIELD(timing->clktrail, period, 1) << 24 |
379 DSI_TIMING_FIELD(timing->clkpost, period, 1) << 16 |
380 DSI_TIMING_FIELD(timing->clkzero, period, 1) << 8 |
381 DSI_TIMING_FIELD(timing->lpx, period, 1);
Thierry Redingdec72732013-09-03 08:45:46 +0200382 tegra_dsi_writel(dsi, value, DSI_PHY_TIMING_1);
383
Thierry Redingebd14af2014-12-08 16:22:28 +0100384 value = DSI_TIMING_FIELD(timing->clkprepare, period, 1) << 16 |
385 DSI_TIMING_FIELD(timing->clkpre, period, 1) << 8 |
Thierry Redingdec72732013-09-03 08:45:46 +0200386 DSI_TIMING_FIELD(0xff * period, period, 0) << 0;
387 tegra_dsi_writel(dsi, value, DSI_PHY_TIMING_2);
388
Thierry Redingebd14af2014-12-08 16:22:28 +0100389 value = DSI_TIMING_FIELD(timing->taget, period, 1) << 16 |
390 DSI_TIMING_FIELD(timing->tasure, period, 1) << 8 |
391 DSI_TIMING_FIELD(timing->tago, period, 1);
Thierry Redingdec72732013-09-03 08:45:46 +0200392 tegra_dsi_writel(dsi, value, DSI_BTA_TIMING);
393
Sean Paul7e3bc3a2014-10-07 16:04:42 +0200394 if (dsi->slave)
Thierry Redingebd14af2014-12-08 16:22:28 +0100395 tegra_dsi_set_phy_timing(dsi->slave, period, timing);
Thierry Redingdec72732013-09-03 08:45:46 +0200396}
397
398static int tegra_dsi_get_muldiv(enum mipi_dsi_pixel_format format,
399 unsigned int *mulp, unsigned int *divp)
400{
401 switch (format) {
402 case MIPI_DSI_FMT_RGB666_PACKED:
403 case MIPI_DSI_FMT_RGB888:
404 *mulp = 3;
405 *divp = 1;
406 break;
407
408 case MIPI_DSI_FMT_RGB565:
409 *mulp = 2;
410 *divp = 1;
411 break;
412
413 case MIPI_DSI_FMT_RGB666:
414 *mulp = 9;
415 *divp = 4;
416 break;
417
418 default:
419 return -EINVAL;
420 }
421
422 return 0;
423}
424
Thierry Redingf7d68892014-03-13 08:50:39 +0100425static int tegra_dsi_get_format(enum mipi_dsi_pixel_format format,
426 enum tegra_dsi_format *fmt)
427{
428 switch (format) {
429 case MIPI_DSI_FMT_RGB888:
430 *fmt = TEGRA_DSI_FORMAT_24P;
431 break;
432
433 case MIPI_DSI_FMT_RGB666:
434 *fmt = TEGRA_DSI_FORMAT_18NP;
435 break;
436
437 case MIPI_DSI_FMT_RGB666_PACKED:
438 *fmt = TEGRA_DSI_FORMAT_18P;
439 break;
440
441 case MIPI_DSI_FMT_RGB565:
442 *fmt = TEGRA_DSI_FORMAT_16P;
443 break;
444
445 default:
446 return -EINVAL;
447 }
448
449 return 0;
450}
451
Thierry Redinge94236c2014-10-07 16:10:24 +0200452static void tegra_dsi_ganged_enable(struct tegra_dsi *dsi, unsigned int start,
453 unsigned int size)
454{
455 u32 value;
456
457 tegra_dsi_writel(dsi, start, DSI_GANGED_MODE_START);
458 tegra_dsi_writel(dsi, size << 16 | size, DSI_GANGED_MODE_SIZE);
459
460 value = DSI_GANGED_MODE_CONTROL_ENABLE;
461 tegra_dsi_writel(dsi, value, DSI_GANGED_MODE_CONTROL);
462}
463
Thierry Reding563eff12014-11-13 14:44:27 +0100464static void tegra_dsi_enable(struct tegra_dsi *dsi)
Thierry Redingdec72732013-09-03 08:45:46 +0200465{
Thierry Reding563eff12014-11-13 14:44:27 +0100466 u32 value;
Thierry Redingdec72732013-09-03 08:45:46 +0200467
Thierry Reding563eff12014-11-13 14:44:27 +0100468 value = tegra_dsi_readl(dsi, DSI_POWER_CONTROL);
469 value |= DSI_POWER_CONTROL_ENABLE;
470 tegra_dsi_writel(dsi, value, DSI_POWER_CONTROL);
Thierry Redinge94236c2014-10-07 16:10:24 +0200471
472 if (dsi->slave)
473 tegra_dsi_enable(dsi->slave);
474}
475
476static unsigned int tegra_dsi_get_lanes(struct tegra_dsi *dsi)
477{
478 if (dsi->master)
479 return dsi->master->lanes + dsi->lanes;
480
481 if (dsi->slave)
482 return dsi->lanes + dsi->slave->lanes;
483
484 return dsi->lanes;
Thierry Reding563eff12014-11-13 14:44:27 +0100485}
486
Thierry Redingebd14af2014-12-08 16:22:28 +0100487static void tegra_dsi_configure(struct tegra_dsi *dsi, unsigned int pipe,
488 const struct drm_display_mode *mode)
Thierry Reding563eff12014-11-13 14:44:27 +0100489{
490 unsigned int hact, hsw, hbp, hfp, i, mul, div;
Thierry Redingebd14af2014-12-08 16:22:28 +0100491 struct tegra_dsi_state *state;
Thierry Reding563eff12014-11-13 14:44:27 +0100492 const u32 *pkt_seq;
493 u32 value;
Thierry Redingebd14af2014-12-08 16:22:28 +0100494
495 /* XXX: pass in state into this function? */
496 if (dsi->master)
497 state = tegra_dsi_get_state(dsi->master);
498 else
499 state = tegra_dsi_get_state(dsi);
500
501 mul = state->mul;
502 div = state->div;
Thierry Reding334ae6b2014-03-14 14:15:10 +0100503
Thierry Reding17297a22014-03-14 14:13:15 +0100504 if (dsi->flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE) {
505 DRM_DEBUG_KMS("Non-burst video mode with sync pulses\n");
506 pkt_seq = pkt_seq_video_non_burst_sync_pulses;
Thierry Reding337b4432014-11-13 15:02:46 +0100507 } else if (dsi->flags & MIPI_DSI_MODE_VIDEO) {
Thierry Reding17297a22014-03-14 14:13:15 +0100508 DRM_DEBUG_KMS("Non-burst video mode with sync events\n");
509 pkt_seq = pkt_seq_video_non_burst_sync_events;
Thierry Reding337b4432014-11-13 15:02:46 +0100510 } else {
511 DRM_DEBUG_KMS("Command mode\n");
512 pkt_seq = pkt_seq_command_mode;
Thierry Reding17297a22014-03-14 14:13:15 +0100513 }
514
Thierry Redingebd14af2014-12-08 16:22:28 +0100515 value = DSI_CONTROL_CHANNEL(0) |
516 DSI_CONTROL_FORMAT(state->format) |
Thierry Redingdec72732013-09-03 08:45:46 +0200517 DSI_CONTROL_LANES(dsi->lanes - 1) |
Thierry Reding563eff12014-11-13 14:44:27 +0100518 DSI_CONTROL_SOURCE(pipe);
Thierry Redingdec72732013-09-03 08:45:46 +0200519 tegra_dsi_writel(dsi, value, DSI_CONTROL);
520
Thierry Reding976cebc2014-08-06 09:14:28 +0200521 tegra_dsi_writel(dsi, dsi->video_fifo_depth, DSI_MAX_THRESHOLD);
Thierry Redingdec72732013-09-03 08:45:46 +0200522
Thierry Reding563eff12014-11-13 14:44:27 +0100523 value = DSI_HOST_CONTROL_HS;
Thierry Redingdec72732013-09-03 08:45:46 +0200524 tegra_dsi_writel(dsi, value, DSI_HOST_CONTROL);
525
526 value = tegra_dsi_readl(dsi, DSI_CONTROL);
Thierry Reding563eff12014-11-13 14:44:27 +0100527
Alexandre Courbot0c6b1e42014-07-08 21:32:13 +0900528 if (dsi->flags & MIPI_DSI_CLOCK_NON_CONTINUOUS)
529 value |= DSI_CONTROL_HS_CLK_CTRL;
Thierry Reding563eff12014-11-13 14:44:27 +0100530
Thierry Redingdec72732013-09-03 08:45:46 +0200531 value &= ~DSI_CONTROL_TX_TRIG(3);
Thierry Reding337b4432014-11-13 15:02:46 +0100532
533 /* enable DCS commands for command mode */
534 if (dsi->flags & MIPI_DSI_MODE_VIDEO)
535 value &= ~DSI_CONTROL_DCS_ENABLE;
536 else
537 value |= DSI_CONTROL_DCS_ENABLE;
538
Thierry Redingdec72732013-09-03 08:45:46 +0200539 value |= DSI_CONTROL_VIDEO_ENABLE;
540 value &= ~DSI_CONTROL_HOST_ENABLE;
541 tegra_dsi_writel(dsi, value, DSI_CONTROL);
542
Thierry Redingdec72732013-09-03 08:45:46 +0200543 for (i = 0; i < NUM_PKT_SEQ; i++)
544 tegra_dsi_writel(dsi, pkt_seq[i], DSI_PKT_SEQ_0_LO + i);
545
Thierry Reding337b4432014-11-13 15:02:46 +0100546 if (dsi->flags & MIPI_DSI_MODE_VIDEO) {
547 /* horizontal active pixels */
548 hact = mode->hdisplay * mul / div;
Thierry Redingdec72732013-09-03 08:45:46 +0200549
Thierry Reding337b4432014-11-13 15:02:46 +0100550 /* horizontal sync width */
551 hsw = (mode->hsync_end - mode->hsync_start) * mul / div;
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;
Thierry Redingb8be0bd2015-04-08 16:58:07 +0200555
556 if ((dsi->flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE) == 0)
557 hbp += hsw;
Thierry Redingdec72732013-09-03 08:45:46 +0200558
Thierry Reding337b4432014-11-13 15:02:46 +0100559 /* horizontal front porch */
560 hfp = (mode->hsync_start - mode->hdisplay) * mul / div;
Thierry Redingb8be0bd2015-04-08 16:58:07 +0200561
562 /* subtract packet overhead */
563 hsw -= 10;
564 hbp -= 14;
Thierry Reding337b4432014-11-13 15:02:46 +0100565 hfp -= 8;
Thierry Redingdec72732013-09-03 08:45:46 +0200566
Thierry Reding337b4432014-11-13 15:02:46 +0100567 tegra_dsi_writel(dsi, hsw << 16 | 0, DSI_PKT_LEN_0_1);
568 tegra_dsi_writel(dsi, hact << 16 | hbp, DSI_PKT_LEN_2_3);
569 tegra_dsi_writel(dsi, hfp, DSI_PKT_LEN_4_5);
570 tegra_dsi_writel(dsi, 0x0f0f << 16, DSI_PKT_LEN_6_7);
Thierry Redingdec72732013-09-03 08:45:46 +0200571
Thierry Reding337b4432014-11-13 15:02:46 +0100572 /* set SOL delay (for non-burst mode only) */
573 tegra_dsi_writel(dsi, 8 * mul / div, DSI_SOL_DELAY);
Thierry Redinge94236c2014-10-07 16:10:24 +0200574
575 /* TODO: implement ganged mode */
Thierry Reding337b4432014-11-13 15:02:46 +0100576 } else {
577 u16 bytes;
578
Thierry Redinge94236c2014-10-07 16:10:24 +0200579 if (dsi->master || dsi->slave) {
580 /*
581 * For ganged mode, assume symmetric left-right mode.
582 */
583 bytes = 1 + (mode->hdisplay / 2) * mul / div;
584 } else {
585 /* 1 byte (DCS command) + pixel data */
586 bytes = 1 + mode->hdisplay * mul / div;
587 }
Thierry Reding337b4432014-11-13 15:02:46 +0100588
589 tegra_dsi_writel(dsi, 0, DSI_PKT_LEN_0_1);
590 tegra_dsi_writel(dsi, bytes << 16, DSI_PKT_LEN_2_3);
591 tegra_dsi_writel(dsi, bytes << 16, DSI_PKT_LEN_4_5);
592 tegra_dsi_writel(dsi, 0, DSI_PKT_LEN_6_7);
593
594 value = MIPI_DCS_WRITE_MEMORY_START << 8 |
595 MIPI_DCS_WRITE_MEMORY_CONTINUE;
596 tegra_dsi_writel(dsi, value, DSI_DCS_CMDS);
597
Thierry Redinge94236c2014-10-07 16:10:24 +0200598 /* set SOL delay */
599 if (dsi->master || dsi->slave) {
Thierry Redinge94236c2014-10-07 16:10:24 +0200600 unsigned long delay, bclk, bclk_ganged;
Thierry Redingebd14af2014-12-08 16:22:28 +0100601 unsigned int lanes = state->lanes;
Thierry Redinge94236c2014-10-07 16:10:24 +0200602
603 /* SOL to valid, valid to FIFO and FIFO write delay */
604 delay = 4 + 4 + 2;
605 delay = DIV_ROUND_UP(delay * mul, div * lanes);
606 /* FIFO read delay */
607 delay = delay + 6;
608
609 bclk = DIV_ROUND_UP(mode->htotal * mul, div * lanes);
610 bclk_ganged = DIV_ROUND_UP(bclk * lanes / 2, lanes);
611 value = bclk - bclk_ganged + delay + 20;
612 } else {
613 /* TODO: revisit for non-ganged mode */
614 value = 8 * mul / div;
615 }
Thierry Reding337b4432014-11-13 15:02:46 +0100616
617 tegra_dsi_writel(dsi, value, DSI_SOL_DELAY);
618 }
Thierry Redingdec72732013-09-03 08:45:46 +0200619
Thierry Redinge94236c2014-10-07 16:10:24 +0200620 if (dsi->slave) {
Thierry Redingebd14af2014-12-08 16:22:28 +0100621 tegra_dsi_configure(dsi->slave, pipe, mode);
Thierry Redinge94236c2014-10-07 16:10:24 +0200622
623 /*
624 * TODO: Support modes other than symmetrical left-right
625 * split.
626 */
627 tegra_dsi_ganged_enable(dsi, 0, mode->hdisplay / 2);
628 tegra_dsi_ganged_enable(dsi->slave, mode->hdisplay / 2,
629 mode->hdisplay / 2);
630 }
Thierry Reding563eff12014-11-13 14:44:27 +0100631}
632
Thierry Reding563eff12014-11-13 14:44:27 +0100633static int tegra_dsi_wait_idle(struct tegra_dsi *dsi, unsigned long timeout)
634{
635 u32 value;
636
637 timeout = jiffies + msecs_to_jiffies(timeout);
638
639 while (time_before(jiffies, timeout)) {
640 value = tegra_dsi_readl(dsi, DSI_STATUS);
641 if (value & DSI_STATUS_IDLE)
642 return 0;
643
644 usleep_range(1000, 2000);
645 }
646
647 return -ETIMEDOUT;
648}
649
650static void tegra_dsi_video_disable(struct tegra_dsi *dsi)
651{
652 u32 value;
653
654 value = tegra_dsi_readl(dsi, DSI_CONTROL);
655 value &= ~DSI_CONTROL_VIDEO_ENABLE;
656 tegra_dsi_writel(dsi, value, DSI_CONTROL);
Thierry Redinge94236c2014-10-07 16:10:24 +0200657
658 if (dsi->slave)
659 tegra_dsi_video_disable(dsi->slave);
660}
661
662static void tegra_dsi_ganged_disable(struct tegra_dsi *dsi)
663{
664 tegra_dsi_writel(dsi, 0, DSI_GANGED_MODE_START);
665 tegra_dsi_writel(dsi, 0, DSI_GANGED_MODE_SIZE);
666 tegra_dsi_writel(dsi, 0, DSI_GANGED_MODE_CONTROL);
Thierry Reding563eff12014-11-13 14:44:27 +0100667}
668
Thierry Redingef8187d2015-08-07 09:29:54 +0200669static int tegra_dsi_pad_enable(struct tegra_dsi *dsi)
670{
671 u32 value;
672
673 value = DSI_PAD_CONTROL_VS1_PULLDN(0) | DSI_PAD_CONTROL_VS1_PDIO(0);
674 tegra_dsi_writel(dsi, value, DSI_PAD_CONTROL_0);
675
676 return 0;
677}
678
679static int tegra_dsi_pad_calibrate(struct tegra_dsi *dsi)
680{
681 u32 value;
682
683 /*
684 * XXX Is this still needed? The module reset is deasserted right
685 * before this function is called.
686 */
687 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_0);
688 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_1);
689 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_2);
690 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_3);
691 tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_4);
692
693 /* start calibration */
694 tegra_dsi_pad_enable(dsi);
695
696 value = DSI_PAD_SLEW_UP(0x7) | DSI_PAD_SLEW_DN(0x7) |
697 DSI_PAD_LP_UP(0x1) | DSI_PAD_LP_DN(0x1) |
698 DSI_PAD_OUT_CLK(0x0);
699 tegra_dsi_writel(dsi, value, DSI_PAD_CONTROL_2);
700
701 value = DSI_PAD_PREEMP_PD_CLK(0x3) | DSI_PAD_PREEMP_PU_CLK(0x3) |
702 DSI_PAD_PREEMP_PD(0x03) | DSI_PAD_PREEMP_PU(0x3);
703 tegra_dsi_writel(dsi, value, DSI_PAD_CONTROL_3);
704
705 return tegra_mipi_calibrate(dsi->mipi);
706}
707
Thierry Reding5b901e72014-12-02 17:30:23 +0100708static void tegra_dsi_set_timeout(struct tegra_dsi *dsi, unsigned long bclk,
709 unsigned int vrefresh)
710{
711 unsigned int timeout;
712 u32 value;
713
714 /* one frame high-speed transmission timeout */
715 timeout = (bclk / vrefresh) / 512;
716 value = DSI_TIMEOUT_LRX(0x2000) | DSI_TIMEOUT_HTX(timeout);
717 tegra_dsi_writel(dsi, value, DSI_TIMEOUT_0);
718
719 /* 2 ms peripheral timeout for panel */
720 timeout = 2 * bclk / 512 * 1000;
721 value = DSI_TIMEOUT_PR(timeout) | DSI_TIMEOUT_TA(0x2000);
722 tegra_dsi_writel(dsi, value, DSI_TIMEOUT_1);
723
724 value = DSI_TALLY_TA(0) | DSI_TALLY_LRX(0) | DSI_TALLY_HTX(0);
725 tegra_dsi_writel(dsi, value, DSI_TO_TALLY);
726
727 if (dsi->slave)
728 tegra_dsi_set_timeout(dsi->slave, bclk, vrefresh);
729}
730
Thierry Reding563eff12014-11-13 14:44:27 +0100731static void tegra_dsi_disable(struct tegra_dsi *dsi)
732{
733 u32 value;
734
Thierry Redinge94236c2014-10-07 16:10:24 +0200735 if (dsi->slave) {
736 tegra_dsi_ganged_disable(dsi->slave);
737 tegra_dsi_ganged_disable(dsi);
738 }
739
Thierry Reding563eff12014-11-13 14:44:27 +0100740 value = tegra_dsi_readl(dsi, DSI_POWER_CONTROL);
741 value &= ~DSI_POWER_CONTROL_ENABLE;
742 tegra_dsi_writel(dsi, value, DSI_POWER_CONTROL);
743
Thierry Redinge94236c2014-10-07 16:10:24 +0200744 if (dsi->slave)
745 tegra_dsi_disable(dsi->slave);
746
Thierry Reding563eff12014-11-13 14:44:27 +0100747 usleep_range(5000, 10000);
748}
749
Thierry Reding92f0e072014-11-24 16:29:40 +0100750static void tegra_dsi_soft_reset(struct tegra_dsi *dsi)
751{
752 u32 value;
753
754 value = tegra_dsi_readl(dsi, DSI_POWER_CONTROL);
755 value &= ~DSI_POWER_CONTROL_ENABLE;
756 tegra_dsi_writel(dsi, value, DSI_POWER_CONTROL);
757
758 usleep_range(300, 1000);
759
760 value = tegra_dsi_readl(dsi, DSI_POWER_CONTROL);
761 value |= DSI_POWER_CONTROL_ENABLE;
762 tegra_dsi_writel(dsi, value, DSI_POWER_CONTROL);
763
764 usleep_range(300, 1000);
765
766 value = tegra_dsi_readl(dsi, DSI_TRIGGER);
767 if (value)
768 tegra_dsi_writel(dsi, 0, DSI_TRIGGER);
769
770 if (dsi->slave)
771 tegra_dsi_soft_reset(dsi->slave);
772}
773
Thierry Redingebd14af2014-12-08 16:22:28 +0100774static void tegra_dsi_connector_reset(struct drm_connector *connector)
775{
Jon Hunter280dc0e2016-05-18 16:37:36 +0100776 struct tegra_dsi_state *state = kzalloc(sizeof(*state), GFP_KERNEL);
Thierry Redingebd14af2014-12-08 16:22:28 +0100777
Jon Hunter280dc0e2016-05-18 16:37:36 +0100778 if (!state)
779 return;
780
781 if (connector->state) {
782 __drm_atomic_helper_connector_destroy_state(connector->state);
Maarten Lankhorst5459a2a2016-01-04 12:53:17 +0100783 kfree(connector->state);
Maarten Lankhorst5459a2a2016-01-04 12:53:17 +0100784 }
Jon Hunter280dc0e2016-05-18 16:37:36 +0100785
786 __drm_atomic_helper_connector_reset(connector, &state->base);
Thierry Redingebd14af2014-12-08 16:22:28 +0100787}
788
789static struct drm_connector_state *
790tegra_dsi_connector_duplicate_state(struct drm_connector *connector)
791{
792 struct tegra_dsi_state *state = to_dsi_state(connector->state);
793 struct tegra_dsi_state *copy;
794
795 copy = kmemdup(state, sizeof(*state), GFP_KERNEL);
796 if (!copy)
797 return NULL;
798
Jon Hunter280dc0e2016-05-18 16:37:36 +0100799 __drm_atomic_helper_connector_duplicate_state(connector,
800 &copy->base);
801
Thierry Redingebd14af2014-12-08 16:22:28 +0100802 return &copy->base;
803}
804
Thierry Reding5b901e72014-12-02 17:30:23 +0100805static const struct drm_connector_funcs tegra_dsi_connector_funcs = {
Thierry Redingebd14af2014-12-08 16:22:28 +0100806 .reset = tegra_dsi_connector_reset,
Thierry Reding5b901e72014-12-02 17:30:23 +0100807 .detect = tegra_output_connector_detect,
808 .fill_modes = drm_helper_probe_single_connector_modes,
809 .destroy = tegra_output_connector_destroy,
Thierry Redingebd14af2014-12-08 16:22:28 +0100810 .atomic_duplicate_state = tegra_dsi_connector_duplicate_state,
Thierry Reding4aa3df72014-11-24 16:27:13 +0100811 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
Thierry Redinga813d702017-11-08 13:12:44 +0100812 .late_register = tegra_dsi_late_register,
813 .early_unregister = tegra_dsi_early_unregister,
Thierry Reding5b901e72014-12-02 17:30:23 +0100814};
815
816static enum drm_mode_status
817tegra_dsi_connector_mode_valid(struct drm_connector *connector,
818 struct drm_display_mode *mode)
Thierry Reding3f6b4062014-11-13 14:50:33 +0100819{
Thierry Reding5b901e72014-12-02 17:30:23 +0100820 return MODE_OK;
Thierry Reding3f6b4062014-11-13 14:50:33 +0100821}
822
Thierry Reding5b901e72014-12-02 17:30:23 +0100823static const struct drm_connector_helper_funcs tegra_dsi_connector_helper_funcs = {
824 .get_modes = tegra_output_connector_get_modes,
825 .mode_valid = tegra_dsi_connector_mode_valid,
Thierry Reding5b901e72014-12-02 17:30:23 +0100826};
827
828static const struct drm_encoder_funcs tegra_dsi_encoder_funcs = {
829 .destroy = tegra_output_encoder_destroy,
830};
831
Thierry Reding87904c32016-08-12 16:00:53 +0200832static void tegra_dsi_unprepare(struct tegra_dsi *dsi)
833{
834 int err;
835
836 if (dsi->slave)
837 tegra_dsi_unprepare(dsi->slave);
838
839 err = tegra_mipi_disable(dsi->mipi);
840 if (err < 0)
841 dev_err(dsi->dev, "failed to disable MIPI calibration: %d\n",
842 err);
843
844 pm_runtime_put(dsi->dev);
845}
846
Thierry Reding5b901e72014-12-02 17:30:23 +0100847static void tegra_dsi_encoder_disable(struct drm_encoder *encoder)
848{
849 struct tegra_output *output = encoder_to_output(encoder);
850 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
851 struct tegra_dsi *dsi = to_dsi(output);
852 u32 value;
853 int err;
854
855 if (output->panel)
856 drm_panel_disable(output->panel);
857
858 tegra_dsi_video_disable(dsi);
859
860 /*
861 * The following accesses registers of the display controller, so make
862 * sure it's only executed when the output is attached to one.
863 */
864 if (dc) {
865 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
866 value &= ~DSI_ENABLE;
867 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
868
869 tegra_dc_commit(dc);
870 }
871
872 err = tegra_dsi_wait_idle(dsi, 100);
873 if (err < 0)
874 dev_dbg(dsi->dev, "failed to idle DSI: %d\n", err);
875
876 tegra_dsi_soft_reset(dsi);
877
878 if (output->panel)
879 drm_panel_unprepare(output->panel);
880
881 tegra_dsi_disable(dsi);
882
Thierry Reding87904c32016-08-12 16:00:53 +0200883 tegra_dsi_unprepare(dsi);
884}
885
886static void tegra_dsi_prepare(struct tegra_dsi *dsi)
887{
888 int err;
889
890 pm_runtime_get_sync(dsi->dev);
891
892 err = tegra_mipi_enable(dsi->mipi);
893 if (err < 0)
894 dev_err(dsi->dev, "failed to enable MIPI calibration: %d\n",
895 err);
896
897 err = tegra_dsi_pad_calibrate(dsi);
898 if (err < 0)
899 dev_err(dsi->dev, "MIPI calibration failed: %d\n", err);
900
901 if (dsi->slave)
902 tegra_dsi_prepare(dsi->slave);
Thierry Reding5b901e72014-12-02 17:30:23 +0100903}
904
Thierry Reding171e2e62015-07-29 16:04:44 +0200905static void tegra_dsi_encoder_enable(struct drm_encoder *encoder)
906{
907 struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
908 struct tegra_output *output = encoder_to_output(encoder);
909 struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
910 struct tegra_dsi *dsi = to_dsi(output);
911 struct tegra_dsi_state *state;
912 u32 value;
Thierry Redingef8187d2015-08-07 09:29:54 +0200913
Thierry Reding87904c32016-08-12 16:00:53 +0200914 tegra_dsi_prepare(dsi);
Thierry Reding171e2e62015-07-29 16:04:44 +0200915
916 state = tegra_dsi_get_state(dsi);
917
918 tegra_dsi_set_timeout(dsi, state->bclk, state->vrefresh);
919
920 /*
921 * The D-PHY timing fields are expressed in byte-clock cycles, so
922 * multiply the period by 8.
923 */
924 tegra_dsi_set_phy_timing(dsi, state->period * 8, &state->timing);
925
926 if (output->panel)
927 drm_panel_prepare(output->panel);
928
929 tegra_dsi_configure(dsi, dc->pipe, mode);
930
931 /* enable display controller */
932 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
933 value |= DSI_ENABLE;
934 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
935
936 tegra_dc_commit(dc);
937
938 /* enable DSI controller */
939 tegra_dsi_enable(dsi);
940
941 if (output->panel)
942 drm_panel_enable(output->panel);
Thierry Reding171e2e62015-07-29 16:04:44 +0200943}
944
Thierry Redingebd14af2014-12-08 16:22:28 +0100945static int
946tegra_dsi_encoder_atomic_check(struct drm_encoder *encoder,
947 struct drm_crtc_state *crtc_state,
948 struct drm_connector_state *conn_state)
949{
950 struct tegra_output *output = encoder_to_output(encoder);
951 struct tegra_dsi_state *state = to_dsi_state(conn_state);
952 struct tegra_dc *dc = to_tegra_dc(conn_state->crtc);
953 struct tegra_dsi *dsi = to_dsi(output);
954 unsigned int scdiv;
955 unsigned long plld;
956 int err;
957
958 state->pclk = crtc_state->mode.clock * 1000;
959
960 err = tegra_dsi_get_muldiv(dsi->format, &state->mul, &state->div);
961 if (err < 0)
962 return err;
963
964 state->lanes = tegra_dsi_get_lanes(dsi);
965
966 err = tegra_dsi_get_format(dsi->format, &state->format);
967 if (err < 0)
968 return err;
969
970 state->vrefresh = drm_mode_vrefresh(&crtc_state->mode);
971
972 /* compute byte clock */
973 state->bclk = (state->pclk * state->mul) / (state->div * state->lanes);
974
975 DRM_DEBUG_KMS("mul: %u, div: %u, lanes: %u\n", state->mul, state->div,
976 state->lanes);
977 DRM_DEBUG_KMS("format: %u, vrefresh: %u\n", state->format,
978 state->vrefresh);
979 DRM_DEBUG_KMS("bclk: %lu\n", state->bclk);
980
981 /*
982 * Compute bit clock and round up to the next MHz.
983 */
984 plld = DIV_ROUND_UP(state->bclk * 8, USEC_PER_SEC) * USEC_PER_SEC;
985 state->period = DIV_ROUND_CLOSEST(NSEC_PER_SEC, plld);
986
987 err = mipi_dphy_timing_get_default(&state->timing, state->period);
988 if (err < 0)
989 return err;
990
991 err = mipi_dphy_timing_validate(&state->timing, state->period);
992 if (err < 0) {
993 dev_err(dsi->dev, "failed to validate D-PHY timing: %d\n", err);
994 return err;
995 }
996
997 /*
998 * We divide the frequency by two here, but we make up for that by
999 * setting the shift clock divider (further below) to half of the
1000 * correct value.
1001 */
1002 plld /= 2;
1003
1004 /*
1005 * Derive pixel clock from bit clock using the shift clock divider.
1006 * Note that this is only half of what we would expect, but we need
1007 * that to make up for the fact that we divided the bit clock by a
1008 * factor of two above.
1009 *
1010 * It's not clear exactly why this is necessary, but the display is
1011 * not working properly otherwise. Perhaps the PLLs cannot generate
1012 * frequencies sufficiently high.
1013 */
1014 scdiv = ((8 * state->mul) / (state->div * state->lanes)) - 2;
1015
1016 err = tegra_dc_state_setup_clock(dc, crtc_state, dsi->clk_parent,
1017 plld, scdiv);
1018 if (err < 0) {
1019 dev_err(output->dev, "failed to setup CRTC state: %d\n", err);
1020 return err;
1021 }
1022
1023 return err;
1024}
1025
Thierry Reding5b901e72014-12-02 17:30:23 +01001026static const struct drm_encoder_helper_funcs tegra_dsi_encoder_helper_funcs = {
Thierry Reding5b901e72014-12-02 17:30:23 +01001027 .disable = tegra_dsi_encoder_disable,
Thierry Reding171e2e62015-07-29 16:04:44 +02001028 .enable = tegra_dsi_encoder_enable,
Thierry Redingebd14af2014-12-08 16:22:28 +01001029 .atomic_check = tegra_dsi_encoder_atomic_check,
Thierry Redingdec72732013-09-03 08:45:46 +02001030};
1031
Thierry Redingdec72732013-09-03 08:45:46 +02001032static int tegra_dsi_init(struct host1x_client *client)
1033{
Thierry Reding9910f5c2014-05-22 09:57:15 +02001034 struct drm_device *drm = dev_get_drvdata(client->parent);
Thierry Redingdec72732013-09-03 08:45:46 +02001035 struct tegra_dsi *dsi = host1x_client_to_dsi(client);
Thierry Redingdec72732013-09-03 08:45:46 +02001036 int err;
1037
Thierry Redinge94236c2014-10-07 16:10:24 +02001038 /* Gangsters must not register their own outputs. */
1039 if (!dsi->master) {
Thierry Redinge94236c2014-10-07 16:10:24 +02001040 dsi->output.dev = client->dev;
Thierry Redingdec72732013-09-03 08:45:46 +02001041
Thierry Reding5b901e72014-12-02 17:30:23 +01001042 drm_connector_init(drm, &dsi->output.connector,
1043 &tegra_dsi_connector_funcs,
1044 DRM_MODE_CONNECTOR_DSI);
1045 drm_connector_helper_add(&dsi->output.connector,
1046 &tegra_dsi_connector_helper_funcs);
1047 dsi->output.connector.dpms = DRM_MODE_DPMS_OFF;
1048
Thierry Reding5b901e72014-12-02 17:30:23 +01001049 drm_encoder_init(drm, &dsi->output.encoder,
1050 &tegra_dsi_encoder_funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +02001051 DRM_MODE_ENCODER_DSI, NULL);
Thierry Reding5b901e72014-12-02 17:30:23 +01001052 drm_encoder_helper_add(&dsi->output.encoder,
1053 &tegra_dsi_encoder_helper_funcs);
1054
1055 drm_mode_connector_attach_encoder(&dsi->output.connector,
1056 &dsi->output.encoder);
1057 drm_connector_register(&dsi->output.connector);
1058
Thierry Redingea130b22014-12-19 15:51:35 +01001059 err = tegra_output_init(drm, &dsi->output);
Thierry Redingef8187d2015-08-07 09:29:54 +02001060 if (err < 0)
1061 dev_err(dsi->dev, "failed to initialize output: %d\n",
Thierry Redingea130b22014-12-19 15:51:35 +01001062 err);
Thierry Redingea130b22014-12-19 15:51:35 +01001063
Thierry Reding5b901e72014-12-02 17:30:23 +01001064 dsi->output.encoder.possible_crtcs = 0x3;
Thierry Redingdec72732013-09-03 08:45:46 +02001065 }
1066
Thierry Redingdec72732013-09-03 08:45:46 +02001067 return 0;
1068}
1069
1070static int tegra_dsi_exit(struct host1x_client *client)
1071{
1072 struct tegra_dsi *dsi = host1x_client_to_dsi(client);
Thierry Redingdec72732013-09-03 08:45:46 +02001073
Thierry Reding5b901e72014-12-02 17:30:23 +01001074 tegra_output_exit(&dsi->output);
Thierry Redingef8187d2015-08-07 09:29:54 +02001075 regulator_disable(dsi->vdd);
Thierry Reding201106d2014-11-24 16:31:48 +01001076
Thierry Redingdec72732013-09-03 08:45:46 +02001077 return 0;
1078}
1079
1080static const struct host1x_client_ops dsi_client_ops = {
1081 .init = tegra_dsi_init,
1082 .exit = tegra_dsi_exit,
1083};
1084
1085static int tegra_dsi_setup_clocks(struct tegra_dsi *dsi)
1086{
1087 struct clk *parent;
1088 int err;
1089
1090 parent = clk_get_parent(dsi->clk);
1091 if (!parent)
1092 return -EINVAL;
1093
1094 err = clk_set_parent(parent, dsi->clk_parent);
1095 if (err < 0)
1096 return err;
1097
1098 return 0;
1099}
1100
Thierry Reding0fffdf62014-11-07 17:25:26 +01001101static const char * const error_report[16] = {
1102 "SoT Error",
1103 "SoT Sync Error",
1104 "EoT Sync Error",
1105 "Escape Mode Entry Command Error",
1106 "Low-Power Transmit Sync Error",
1107 "Peripheral Timeout Error",
1108 "False Control Error",
1109 "Contention Detected",
1110 "ECC Error, single-bit",
1111 "ECC Error, multi-bit",
1112 "Checksum Error",
1113 "DSI Data Type Not Recognized",
1114 "DSI VC ID Invalid",
1115 "Invalid Transmission Length",
1116 "Reserved",
1117 "DSI Protocol Violation",
1118};
1119
1120static ssize_t tegra_dsi_read_response(struct tegra_dsi *dsi,
1121 const struct mipi_dsi_msg *msg,
1122 size_t count)
1123{
1124 u8 *rx = msg->rx_buf;
1125 unsigned int i, j, k;
1126 size_t size = 0;
1127 u16 errors;
1128 u32 value;
1129
1130 /* read and parse packet header */
1131 value = tegra_dsi_readl(dsi, DSI_RD_DATA);
1132
1133 switch (value & 0x3f) {
1134 case MIPI_DSI_RX_ACKNOWLEDGE_AND_ERROR_REPORT:
1135 errors = (value >> 8) & 0xffff;
1136 dev_dbg(dsi->dev, "Acknowledge and error report: %04x\n",
1137 errors);
1138 for (i = 0; i < ARRAY_SIZE(error_report); i++)
1139 if (errors & BIT(i))
1140 dev_dbg(dsi->dev, " %2u: %s\n", i,
1141 error_report[i]);
1142 break;
1143
1144 case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_1BYTE:
1145 rx[0] = (value >> 8) & 0xff;
1146 size = 1;
1147 break;
1148
1149 case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_2BYTE:
1150 rx[0] = (value >> 8) & 0xff;
1151 rx[1] = (value >> 16) & 0xff;
1152 size = 2;
1153 break;
1154
1155 case MIPI_DSI_RX_DCS_LONG_READ_RESPONSE:
1156 size = ((value >> 8) & 0xff00) | ((value >> 8) & 0xff);
1157 break;
1158
1159 case MIPI_DSI_RX_GENERIC_LONG_READ_RESPONSE:
1160 size = ((value >> 8) & 0xff00) | ((value >> 8) & 0xff);
1161 break;
1162
1163 default:
1164 dev_err(dsi->dev, "unhandled response type: %02x\n",
1165 value & 0x3f);
1166 return -EPROTO;
1167 }
1168
1169 size = min(size, msg->rx_len);
1170
1171 if (msg->rx_buf && size > 0) {
1172 for (i = 0, j = 0; i < count - 1; i++, j += 4) {
1173 u8 *rx = msg->rx_buf + j;
1174
1175 value = tegra_dsi_readl(dsi, DSI_RD_DATA);
1176
1177 for (k = 0; k < 4 && (j + k) < msg->rx_len; k++)
1178 rx[j + k] = (value >> (k << 3)) & 0xff;
1179 }
1180 }
1181
1182 return size;
1183}
1184
1185static int tegra_dsi_transmit(struct tegra_dsi *dsi, unsigned long timeout)
1186{
1187 tegra_dsi_writel(dsi, DSI_TRIGGER_HOST, DSI_TRIGGER);
1188
1189 timeout = jiffies + msecs_to_jiffies(timeout);
1190
1191 while (time_before(jiffies, timeout)) {
1192 u32 value = tegra_dsi_readl(dsi, DSI_TRIGGER);
1193 if ((value & DSI_TRIGGER_HOST) == 0)
1194 return 0;
1195
1196 usleep_range(1000, 2000);
1197 }
1198
1199 DRM_DEBUG_KMS("timeout waiting for transmission to complete\n");
1200 return -ETIMEDOUT;
1201}
1202
1203static int tegra_dsi_wait_for_response(struct tegra_dsi *dsi,
1204 unsigned long timeout)
1205{
1206 timeout = jiffies + msecs_to_jiffies(250);
1207
1208 while (time_before(jiffies, timeout)) {
1209 u32 value = tegra_dsi_readl(dsi, DSI_STATUS);
1210 u8 count = value & 0x1f;
1211
1212 if (count > 0)
1213 return count;
1214
1215 usleep_range(1000, 2000);
1216 }
1217
1218 DRM_DEBUG_KMS("peripheral returned no data\n");
1219 return -ETIMEDOUT;
1220}
1221
1222static void tegra_dsi_writesl(struct tegra_dsi *dsi, unsigned long offset,
1223 const void *buffer, size_t size)
1224{
1225 const u8 *buf = buffer;
1226 size_t i, j;
1227 u32 value;
1228
1229 for (j = 0; j < size; j += 4) {
1230 value = 0;
1231
1232 for (i = 0; i < 4 && j + i < size; i++)
1233 value |= buf[j + i] << (i << 3);
1234
1235 tegra_dsi_writel(dsi, value, DSI_WR_DATA);
1236 }
1237}
1238
1239static ssize_t tegra_dsi_host_transfer(struct mipi_dsi_host *host,
1240 const struct mipi_dsi_msg *msg)
1241{
1242 struct tegra_dsi *dsi = host_to_tegra(host);
1243 struct mipi_dsi_packet packet;
1244 const u8 *header;
1245 size_t count;
1246 ssize_t err;
1247 u32 value;
1248
1249 err = mipi_dsi_create_packet(&packet, msg);
1250 if (err < 0)
1251 return err;
1252
1253 header = packet.header;
1254
1255 /* maximum FIFO depth is 1920 words */
1256 if (packet.size > dsi->video_fifo_depth * 4)
1257 return -ENOSPC;
1258
1259 /* reset underflow/overflow flags */
1260 value = tegra_dsi_readl(dsi, DSI_STATUS);
1261 if (value & (DSI_STATUS_UNDERFLOW | DSI_STATUS_OVERFLOW)) {
1262 value = DSI_HOST_CONTROL_FIFO_RESET;
1263 tegra_dsi_writel(dsi, value, DSI_HOST_CONTROL);
1264 usleep_range(10, 20);
1265 }
1266
1267 value = tegra_dsi_readl(dsi, DSI_POWER_CONTROL);
1268 value |= DSI_POWER_CONTROL_ENABLE;
1269 tegra_dsi_writel(dsi, value, DSI_POWER_CONTROL);
1270
1271 usleep_range(5000, 10000);
1272
1273 value = DSI_HOST_CONTROL_CRC_RESET | DSI_HOST_CONTROL_TX_TRIG_HOST |
1274 DSI_HOST_CONTROL_CS | DSI_HOST_CONTROL_ECC;
1275
1276 if ((msg->flags & MIPI_DSI_MSG_USE_LPM) == 0)
1277 value |= DSI_HOST_CONTROL_HS;
1278
1279 /*
1280 * The host FIFO has a maximum of 64 words, so larger transmissions
1281 * need to use the video FIFO.
1282 */
1283 if (packet.size > dsi->host_fifo_depth * 4)
1284 value |= DSI_HOST_CONTROL_FIFO_SEL;
1285
1286 tegra_dsi_writel(dsi, value, DSI_HOST_CONTROL);
1287
1288 /*
1289 * For reads and messages with explicitly requested ACK, generate a
1290 * BTA sequence after the transmission of the packet.
1291 */
1292 if ((msg->flags & MIPI_DSI_MSG_REQ_ACK) ||
1293 (msg->rx_buf && msg->rx_len > 0)) {
1294 value = tegra_dsi_readl(dsi, DSI_HOST_CONTROL);
1295 value |= DSI_HOST_CONTROL_PKT_BTA;
1296 tegra_dsi_writel(dsi, value, DSI_HOST_CONTROL);
1297 }
1298
1299 value = DSI_CONTROL_LANES(0) | DSI_CONTROL_HOST_ENABLE;
1300 tegra_dsi_writel(dsi, value, DSI_CONTROL);
1301
1302 /* write packet header, ECC is generated by hardware */
1303 value = header[2] << 16 | header[1] << 8 | header[0];
1304 tegra_dsi_writel(dsi, value, DSI_WR_DATA);
1305
1306 /* write payload (if any) */
1307 if (packet.payload_length > 0)
1308 tegra_dsi_writesl(dsi, DSI_WR_DATA, packet.payload,
1309 packet.payload_length);
1310
1311 err = tegra_dsi_transmit(dsi, 250);
1312 if (err < 0)
1313 return err;
1314
1315 if ((msg->flags & MIPI_DSI_MSG_REQ_ACK) ||
1316 (msg->rx_buf && msg->rx_len > 0)) {
1317 err = tegra_dsi_wait_for_response(dsi, 250);
1318 if (err < 0)
1319 return err;
1320
1321 count = err;
1322
1323 value = tegra_dsi_readl(dsi, DSI_RD_DATA);
1324 switch (value) {
1325 case 0x84:
1326 /*
1327 dev_dbg(dsi->dev, "ACK\n");
1328 */
1329 break;
1330
1331 case 0x87:
1332 /*
1333 dev_dbg(dsi->dev, "ESCAPE\n");
1334 */
1335 break;
1336
1337 default:
1338 dev_err(dsi->dev, "unknown status: %08x\n", value);
1339 break;
1340 }
1341
1342 if (count > 1) {
1343 err = tegra_dsi_read_response(dsi, msg, count);
1344 if (err < 0)
1345 dev_err(dsi->dev,
1346 "failed to parse response: %zd\n",
1347 err);
1348 else {
1349 /*
1350 * For read commands, return the number of
1351 * bytes returned by the peripheral.
1352 */
1353 count = err;
1354 }
1355 }
1356 } else {
1357 /*
1358 * For write commands, we have transmitted the 4-byte header
1359 * plus the variable-length payload.
1360 */
1361 count = 4 + packet.payload_length;
1362 }
1363
1364 return count;
1365}
1366
Thierry Redinge94236c2014-10-07 16:10:24 +02001367static int tegra_dsi_ganged_setup(struct tegra_dsi *dsi)
1368{
1369 struct clk *parent;
1370 int err;
1371
1372 /* make sure both DSI controllers share the same PLL */
1373 parent = clk_get_parent(dsi->slave->clk);
1374 if (!parent)
1375 return -EINVAL;
1376
1377 err = clk_set_parent(parent, dsi->clk_parent);
1378 if (err < 0)
1379 return err;
1380
1381 return 0;
1382}
1383
Thierry Redingdec72732013-09-03 08:45:46 +02001384static int tegra_dsi_host_attach(struct mipi_dsi_host *host,
1385 struct mipi_dsi_device *device)
1386{
1387 struct tegra_dsi *dsi = host_to_tegra(host);
Thierry Redingdec72732013-09-03 08:45:46 +02001388
Thierry Reding17297a22014-03-14 14:13:15 +01001389 dsi->flags = device->mode_flags;
Thierry Redingdec72732013-09-03 08:45:46 +02001390 dsi->format = device->format;
1391 dsi->lanes = device->lanes;
1392
Thierry Redinge94236c2014-10-07 16:10:24 +02001393 if (dsi->slave) {
1394 int err;
1395
1396 dev_dbg(dsi->dev, "attaching dual-channel device %s\n",
1397 dev_name(&device->dev));
1398
1399 err = tegra_dsi_ganged_setup(dsi);
1400 if (err < 0) {
1401 dev_err(dsi->dev, "failed to set up ganged mode: %d\n",
1402 err);
1403 return err;
1404 }
1405 }
1406
1407 /*
1408 * Slaves don't have a panel associated with them, so they provide
1409 * merely the second channel.
1410 */
1411 if (!dsi->master) {
1412 struct tegra_output *output = &dsi->output;
1413
1414 output->panel = of_drm_find_panel(device->dev.of_node);
1415 if (output->panel && output->connector.dev) {
1416 drm_panel_attach(output->panel, &output->connector);
Thierry Redingdec72732013-09-03 08:45:46 +02001417 drm_helper_hpd_irq_event(output->connector.dev);
Thierry Redinge94236c2014-10-07 16:10:24 +02001418 }
Thierry Redingdec72732013-09-03 08:45:46 +02001419 }
1420
1421 return 0;
1422}
1423
1424static int tegra_dsi_host_detach(struct mipi_dsi_host *host,
1425 struct mipi_dsi_device *device)
1426{
1427 struct tegra_dsi *dsi = host_to_tegra(host);
1428 struct tegra_output *output = &dsi->output;
1429
1430 if (output->panel && &device->dev == output->panel->dev) {
Thierry Redingba3df972014-11-13 14:54:01 +01001431 output->panel = NULL;
1432
Thierry Redingdec72732013-09-03 08:45:46 +02001433 if (output->connector.dev)
1434 drm_helper_hpd_irq_event(output->connector.dev);
Thierry Redingdec72732013-09-03 08:45:46 +02001435 }
1436
1437 return 0;
1438}
1439
1440static const struct mipi_dsi_host_ops tegra_dsi_host_ops = {
1441 .attach = tegra_dsi_host_attach,
1442 .detach = tegra_dsi_host_detach,
Thierry Reding0fffdf62014-11-07 17:25:26 +01001443 .transfer = tegra_dsi_host_transfer,
Thierry Redingdec72732013-09-03 08:45:46 +02001444};
1445
Thierry Redinge94236c2014-10-07 16:10:24 +02001446static int tegra_dsi_ganged_probe(struct tegra_dsi *dsi)
1447{
1448 struct device_node *np;
1449
1450 np = of_parse_phandle(dsi->dev->of_node, "nvidia,ganged-mode", 0);
1451 if (np) {
1452 struct platform_device *gangster = of_find_device_by_node(np);
1453
1454 dsi->slave = platform_get_drvdata(gangster);
1455 of_node_put(np);
1456
1457 if (!dsi->slave)
1458 return -EPROBE_DEFER;
1459
1460 dsi->slave->master = dsi;
1461 }
1462
1463 return 0;
1464}
1465
Thierry Redingdec72732013-09-03 08:45:46 +02001466static int tegra_dsi_probe(struct platform_device *pdev)
1467{
1468 struct tegra_dsi *dsi;
1469 struct resource *regs;
1470 int err;
1471
1472 dsi = devm_kzalloc(&pdev->dev, sizeof(*dsi), GFP_KERNEL);
1473 if (!dsi)
1474 return -ENOMEM;
1475
1476 dsi->output.dev = dsi->dev = &pdev->dev;
Thierry Reding976cebc2014-08-06 09:14:28 +02001477 dsi->video_fifo_depth = 1920;
1478 dsi->host_fifo_depth = 64;
Thierry Redingdec72732013-09-03 08:45:46 +02001479
Thierry Redinge94236c2014-10-07 16:10:24 +02001480 err = tegra_dsi_ganged_probe(dsi);
1481 if (err < 0)
1482 return err;
1483
Thierry Redingdec72732013-09-03 08:45:46 +02001484 err = tegra_output_probe(&dsi->output);
1485 if (err < 0)
1486 return err;
1487
Thierry Redingba3df972014-11-13 14:54:01 +01001488 dsi->output.connector.polled = DRM_CONNECTOR_POLL_HPD;
1489
Thierry Redingdec72732013-09-03 08:45:46 +02001490 /*
1491 * Assume these values by default. When a DSI peripheral driver
1492 * attaches to the DSI host, the parameters will be taken from
1493 * the attached device.
1494 */
Thierry Reding17297a22014-03-14 14:13:15 +01001495 dsi->flags = MIPI_DSI_MODE_VIDEO;
Thierry Redingdec72732013-09-03 08:45:46 +02001496 dsi->format = MIPI_DSI_FMT_RGB888;
1497 dsi->lanes = 4;
1498
Jon Hunter64230aa2016-07-01 14:21:37 +01001499 if (!pdev->dev.pm_domain) {
1500 dsi->rst = devm_reset_control_get(&pdev->dev, "dsi");
1501 if (IS_ERR(dsi->rst))
1502 return PTR_ERR(dsi->rst);
1503 }
Thierry Redingdec72732013-09-03 08:45:46 +02001504
1505 dsi->clk = devm_clk_get(&pdev->dev, NULL);
1506 if (IS_ERR(dsi->clk)) {
1507 dev_err(&pdev->dev, "cannot get DSI clock\n");
Thierry Redingef8187d2015-08-07 09:29:54 +02001508 return PTR_ERR(dsi->clk);
Thierry Redingdec72732013-09-03 08:45:46 +02001509 }
1510
1511 dsi->clk_lp = devm_clk_get(&pdev->dev, "lp");
1512 if (IS_ERR(dsi->clk_lp)) {
1513 dev_err(&pdev->dev, "cannot get low-power clock\n");
Thierry Redingef8187d2015-08-07 09:29:54 +02001514 return PTR_ERR(dsi->clk_lp);
Thierry Redingdec72732013-09-03 08:45:46 +02001515 }
1516
1517 dsi->clk_parent = devm_clk_get(&pdev->dev, "parent");
1518 if (IS_ERR(dsi->clk_parent)) {
1519 dev_err(&pdev->dev, "cannot get parent clock\n");
Thierry Redingef8187d2015-08-07 09:29:54 +02001520 return PTR_ERR(dsi->clk_parent);
Thierry Redingdec72732013-09-03 08:45:46 +02001521 }
1522
Thierry Reding3b077af2014-03-14 14:07:50 +01001523 dsi->vdd = devm_regulator_get(&pdev->dev, "avdd-dsi-csi");
1524 if (IS_ERR(dsi->vdd)) {
1525 dev_err(&pdev->dev, "cannot get VDD supply\n");
Thierry Redingef8187d2015-08-07 09:29:54 +02001526 return PTR_ERR(dsi->vdd);
Thierry Reding3b077af2014-03-14 14:07:50 +01001527 }
1528
Thierry Redingdec72732013-09-03 08:45:46 +02001529 err = tegra_dsi_setup_clocks(dsi);
1530 if (err < 0) {
1531 dev_err(&pdev->dev, "cannot setup clocks\n");
Thierry Redingef8187d2015-08-07 09:29:54 +02001532 return err;
Thierry Redingdec72732013-09-03 08:45:46 +02001533 }
1534
1535 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1536 dsi->regs = devm_ioremap_resource(&pdev->dev, regs);
Thierry Redingef8187d2015-08-07 09:29:54 +02001537 if (IS_ERR(dsi->regs))
1538 return PTR_ERR(dsi->regs);
Thierry Redingdec72732013-09-03 08:45:46 +02001539
Thierry Redingdec72732013-09-03 08:45:46 +02001540 dsi->mipi = tegra_mipi_request(&pdev->dev);
Thierry Redingef8187d2015-08-07 09:29:54 +02001541 if (IS_ERR(dsi->mipi))
1542 return PTR_ERR(dsi->mipi);
Thierry Redingdec72732013-09-03 08:45:46 +02001543
1544 dsi->host.ops = &tegra_dsi_host_ops;
1545 dsi->host.dev = &pdev->dev;
1546
1547 err = mipi_dsi_host_register(&dsi->host);
1548 if (err < 0) {
1549 dev_err(&pdev->dev, "failed to register DSI host: %d\n", err);
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001550 goto mipi_free;
Thierry Redingdec72732013-09-03 08:45:46 +02001551 }
1552
Thierry Redingef8187d2015-08-07 09:29:54 +02001553 platform_set_drvdata(pdev, dsi);
1554 pm_runtime_enable(&pdev->dev);
1555
Thierry Redingdec72732013-09-03 08:45:46 +02001556 INIT_LIST_HEAD(&dsi->client.list);
1557 dsi->client.ops = &dsi_client_ops;
1558 dsi->client.dev = &pdev->dev;
1559
1560 err = host1x_client_register(&dsi->client);
1561 if (err < 0) {
1562 dev_err(&pdev->dev, "failed to register host1x client: %d\n",
1563 err);
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001564 goto unregister;
Thierry Redingdec72732013-09-03 08:45:46 +02001565 }
1566
Thierry Redingdec72732013-09-03 08:45:46 +02001567 return 0;
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001568
1569unregister:
1570 mipi_dsi_host_unregister(&dsi->host);
1571mipi_free:
1572 tegra_mipi_free(dsi->mipi);
Thierry Redingd2d0a9d2014-11-13 14:58:27 +01001573 return err;
Thierry Redingdec72732013-09-03 08:45:46 +02001574}
1575
1576static int tegra_dsi_remove(struct platform_device *pdev)
1577{
1578 struct tegra_dsi *dsi = platform_get_drvdata(pdev);
1579 int err;
1580
Thierry Redingef8187d2015-08-07 09:29:54 +02001581 pm_runtime_disable(&pdev->dev);
1582
Thierry Redingdec72732013-09-03 08:45:46 +02001583 err = host1x_client_unregister(&dsi->client);
1584 if (err < 0) {
1585 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
1586 err);
1587 return err;
1588 }
1589
Thierry Reding328ec692014-12-19 15:55:08 +01001590 tegra_output_remove(&dsi->output);
Thierry Reding5b901e72014-12-02 17:30:23 +01001591
Thierry Redingdec72732013-09-03 08:45:46 +02001592 mipi_dsi_host_unregister(&dsi->host);
1593 tegra_mipi_free(dsi->mipi);
1594
Thierry Redingef8187d2015-08-07 09:29:54 +02001595 return 0;
1596}
1597
1598#ifdef CONFIG_PM
1599static int tegra_dsi_suspend(struct device *dev)
1600{
1601 struct tegra_dsi *dsi = dev_get_drvdata(dev);
1602 int err;
1603
Jon Hunter64230aa2016-07-01 14:21:37 +01001604 if (dsi->rst) {
1605 err = reset_control_assert(dsi->rst);
1606 if (err < 0) {
1607 dev_err(dev, "failed to assert reset: %d\n", err);
1608 return err;
1609 }
Thierry Redingef8187d2015-08-07 09:29:54 +02001610 }
1611
1612 usleep_range(1000, 2000);
1613
Thierry Redingdec72732013-09-03 08:45:46 +02001614 clk_disable_unprepare(dsi->clk_lp);
1615 clk_disable_unprepare(dsi->clk);
Thierry Redingef8187d2015-08-07 09:29:54 +02001616
1617 regulator_disable(dsi->vdd);
Thierry Redingdec72732013-09-03 08:45:46 +02001618
Thierry Redingdec72732013-09-03 08:45:46 +02001619 return 0;
1620}
1621
Thierry Redingef8187d2015-08-07 09:29:54 +02001622static int tegra_dsi_resume(struct device *dev)
1623{
1624 struct tegra_dsi *dsi = dev_get_drvdata(dev);
1625 int err;
1626
1627 err = regulator_enable(dsi->vdd);
1628 if (err < 0) {
1629 dev_err(dsi->dev, "failed to enable VDD supply: %d\n", err);
1630 return err;
1631 }
1632
1633 err = clk_prepare_enable(dsi->clk);
1634 if (err < 0) {
1635 dev_err(dev, "cannot enable DSI clock: %d\n", err);
1636 goto disable_vdd;
1637 }
1638
1639 err = clk_prepare_enable(dsi->clk_lp);
1640 if (err < 0) {
1641 dev_err(dev, "cannot enable low-power clock: %d\n", err);
1642 goto disable_clk;
1643 }
1644
1645 usleep_range(1000, 2000);
1646
Jon Hunter64230aa2016-07-01 14:21:37 +01001647 if (dsi->rst) {
1648 err = reset_control_deassert(dsi->rst);
1649 if (err < 0) {
1650 dev_err(dev, "cannot assert reset: %d\n", err);
1651 goto disable_clk_lp;
1652 }
Thierry Redingef8187d2015-08-07 09:29:54 +02001653 }
1654
1655 return 0;
1656
1657disable_clk_lp:
1658 clk_disable_unprepare(dsi->clk_lp);
1659disable_clk:
1660 clk_disable_unprepare(dsi->clk);
1661disable_vdd:
1662 regulator_disable(dsi->vdd);
1663 return err;
1664}
1665#endif
1666
1667static const struct dev_pm_ops tegra_dsi_pm_ops = {
1668 SET_RUNTIME_PM_OPS(tegra_dsi_suspend, tegra_dsi_resume, NULL)
1669};
1670
Thierry Redingdec72732013-09-03 08:45:46 +02001671static const struct of_device_id tegra_dsi_of_match[] = {
Thierry Redingddfb4062015-04-08 16:56:22 +02001672 { .compatible = "nvidia,tegra210-dsi", },
Thierry Redingc06c7932015-04-10 11:35:21 +02001673 { .compatible = "nvidia,tegra132-dsi", },
Thierry Reding7d338582015-04-10 11:35:21 +02001674 { .compatible = "nvidia,tegra124-dsi", },
Thierry Redingdec72732013-09-03 08:45:46 +02001675 { .compatible = "nvidia,tegra114-dsi", },
1676 { },
1677};
Stephen Warrenef707282014-06-18 16:21:55 -06001678MODULE_DEVICE_TABLE(of, tegra_dsi_of_match);
Thierry Redingdec72732013-09-03 08:45:46 +02001679
1680struct platform_driver tegra_dsi_driver = {
1681 .driver = {
1682 .name = "tegra-dsi",
1683 .of_match_table = tegra_dsi_of_match,
Thierry Redingef8187d2015-08-07 09:29:54 +02001684 .pm = &tegra_dsi_pm_ops,
Thierry Redingdec72732013-09-03 08:45:46 +02001685 },
1686 .probe = tegra_dsi_probe,
1687 .remove = tegra_dsi_remove,
1688};