blob: f0b2c736145f3948058e221069da2a53a07a694e [file] [log] [blame]
Philippe CORNU46fc5152017-07-17 09:40:20 +02001/*
2 * Copyright (c) 2016, Fuzhou Rockchip Electronics Co., Ltd
3 * Copyright (C) STMicroelectronics SA 2017
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * Modified by Philippe Cornu <philippe.cornu@st.com>
11 * This generic Synopsys DesignWare MIPI DSI host driver is based on the
12 * Rockchip version from rockchip/dw-mipi-dsi.c with phy & bridge APIs.
13 */
14
15#include <linux/clk.h>
16#include <linux/component.h>
17#include <linux/iopoll.h>
18#include <linux/module.h>
19#include <linux/of_device.h>
20#include <linux/pm_runtime.h>
21#include <linux/reset.h>
22#include <drm/drmP.h>
23#include <drm/drm_atomic_helper.h>
24#include <drm/drm_bridge.h>
25#include <drm/drm_crtc.h>
26#include <drm/drm_crtc_helper.h>
27#include <drm/drm_mipi_dsi.h>
28#include <drm/drm_of.h>
29#include <drm/bridge/dw_mipi_dsi.h>
30#include <video/mipi_display.h>
31
32#define DSI_VERSION 0x00
Philippe CORNU1df82a62017-08-01 15:23:07 +020033
Philippe CORNU46fc5152017-07-17 09:40:20 +020034#define DSI_PWR_UP 0x04
35#define RESET 0
36#define POWERUP BIT(0)
37
38#define DSI_CLKMGR_CFG 0x08
Philippe CORNU1df82a62017-08-01 15:23:07 +020039#define TO_CLK_DIVISION(div) (((div) & 0xff) << 8)
40#define TX_ESC_CLK_DIVISION(div) ((div) & 0xff)
Philippe CORNU46fc5152017-07-17 09:40:20 +020041
42#define DSI_DPI_VCID 0x0c
Philippe CORNU1df82a62017-08-01 15:23:07 +020043#define DPI_VCID(vcid) ((vcid) & 0x3)
Philippe CORNU46fc5152017-07-17 09:40:20 +020044
45#define DSI_DPI_COLOR_CODING 0x10
Philippe CORNU1df82a62017-08-01 15:23:07 +020046#define LOOSELY18_EN BIT(8)
Philippe CORNU46fc5152017-07-17 09:40:20 +020047#define DPI_COLOR_CODING_16BIT_1 0x0
48#define DPI_COLOR_CODING_16BIT_2 0x1
49#define DPI_COLOR_CODING_16BIT_3 0x2
50#define DPI_COLOR_CODING_18BIT_1 0x3
51#define DPI_COLOR_CODING_18BIT_2 0x4
52#define DPI_COLOR_CODING_24BIT 0x5
53
54#define DSI_DPI_CFG_POL 0x14
55#define COLORM_ACTIVE_LOW BIT(4)
56#define SHUTD_ACTIVE_LOW BIT(3)
57#define HSYNC_ACTIVE_LOW BIT(2)
58#define VSYNC_ACTIVE_LOW BIT(1)
59#define DATAEN_ACTIVE_LOW BIT(0)
60
61#define DSI_DPI_LP_CMD_TIM 0x18
62#define OUTVACT_LPCMD_TIME(p) (((p) & 0xff) << 16)
63#define INVACT_LPCMD_TIME(p) ((p) & 0xff)
64
Philippe CORNU1df82a62017-08-01 15:23:07 +020065#define DSI_DBI_VCID 0x1c
Philippe CORNU46fc5152017-07-17 09:40:20 +020066#define DSI_DBI_CFG 0x20
Philippe CORNU1df82a62017-08-01 15:23:07 +020067#define DSI_DBI_PARTITIONING_EN 0x24
Philippe CORNU46fc5152017-07-17 09:40:20 +020068#define DSI_DBI_CMDSIZE 0x28
69
70#define DSI_PCKHDL_CFG 0x2c
Philippe CORNU1df82a62017-08-01 15:23:07 +020071#define CRC_RX_EN BIT(4)
72#define ECC_RX_EN BIT(3)
73#define BTA_EN BIT(2)
74#define EOTP_RX_EN BIT(1)
75#define EOTP_TX_EN BIT(0)
76
77#define DSI_GEN_VCID 0x30
Philippe CORNU46fc5152017-07-17 09:40:20 +020078
79#define DSI_MODE_CFG 0x34
80#define ENABLE_VIDEO_MODE 0
81#define ENABLE_CMD_MODE BIT(0)
82
83#define DSI_VID_MODE_CFG 0x38
Philippe CORNU46fc5152017-07-17 09:40:20 +020084#define ENABLE_LOW_POWER (0x3f << 8)
85#define ENABLE_LOW_POWER_MASK (0x3f << 8)
86#define VID_MODE_TYPE_NON_BURST_SYNC_PULSES 0x0
87#define VID_MODE_TYPE_NON_BURST_SYNC_EVENTS 0x1
88#define VID_MODE_TYPE_BURST 0x2
89#define VID_MODE_TYPE_MASK 0x3
90
91#define DSI_VID_PKT_SIZE 0x3c
Philippe CORNU1df82a62017-08-01 15:23:07 +020092#define VID_PKT_SIZE(p) ((p) & 0x3fff)
93
94#define DSI_VID_NUM_CHUNKS 0x40
95#define VID_NUM_CHUNKS(c) ((c) & 0x1fff)
96
97#define DSI_VID_NULL_SIZE 0x44
98#define VID_NULL_SIZE(b) ((b) & 0x1fff)
Philippe CORNU46fc5152017-07-17 09:40:20 +020099
100#define DSI_VID_HSA_TIME 0x48
101#define DSI_VID_HBP_TIME 0x4c
102#define DSI_VID_HLINE_TIME 0x50
103#define DSI_VID_VSA_LINES 0x54
104#define DSI_VID_VBP_LINES 0x58
105#define DSI_VID_VFP_LINES 0x5c
106#define DSI_VID_VACTIVE_LINES 0x60
Philippe CORNU1df82a62017-08-01 15:23:07 +0200107#define DSI_EDPI_CMD_SIZE 0x64
108
Philippe CORNU46fc5152017-07-17 09:40:20 +0200109#define DSI_CMD_MODE_CFG 0x68
110#define MAX_RD_PKT_SIZE_LP BIT(24)
111#define DCS_LW_TX_LP BIT(19)
112#define DCS_SR_0P_TX_LP BIT(18)
113#define DCS_SW_1P_TX_LP BIT(17)
114#define DCS_SW_0P_TX_LP BIT(16)
115#define GEN_LW_TX_LP BIT(14)
116#define GEN_SR_2P_TX_LP BIT(13)
117#define GEN_SR_1P_TX_LP BIT(12)
118#define GEN_SR_0P_TX_LP BIT(11)
119#define GEN_SW_2P_TX_LP BIT(10)
120#define GEN_SW_1P_TX_LP BIT(9)
121#define GEN_SW_0P_TX_LP BIT(8)
Philippe CORNU1df82a62017-08-01 15:23:07 +0200122#define ACK_RQST_EN BIT(1)
123#define TEAR_FX_EN BIT(0)
Philippe CORNU46fc5152017-07-17 09:40:20 +0200124
125#define CMD_MODE_ALL_LP (MAX_RD_PKT_SIZE_LP | \
126 DCS_LW_TX_LP | \
127 DCS_SR_0P_TX_LP | \
128 DCS_SW_1P_TX_LP | \
129 DCS_SW_0P_TX_LP | \
130 GEN_LW_TX_LP | \
131 GEN_SR_2P_TX_LP | \
132 GEN_SR_1P_TX_LP | \
133 GEN_SR_0P_TX_LP | \
134 GEN_SW_2P_TX_LP | \
135 GEN_SW_1P_TX_LP | \
136 GEN_SW_0P_TX_LP)
137
138#define DSI_GEN_HDR 0x6c
Philippe CORNU46fc5152017-07-17 09:40:20 +0200139#define DSI_GEN_PLD_DATA 0x70
140
141#define DSI_CMD_PKT_STATUS 0x74
Philippe CORNU46fc5152017-07-17 09:40:20 +0200142#define GEN_RD_CMD_BUSY BIT(6)
Philippe CORNU1df82a62017-08-01 15:23:07 +0200143#define GEN_PLD_R_FULL BIT(5)
144#define GEN_PLD_R_EMPTY BIT(4)
145#define GEN_PLD_W_FULL BIT(3)
146#define GEN_PLD_W_EMPTY BIT(2)
147#define GEN_CMD_FULL BIT(1)
148#define GEN_CMD_EMPTY BIT(0)
Philippe CORNU46fc5152017-07-17 09:40:20 +0200149
150#define DSI_TO_CNT_CFG 0x78
151#define HSTX_TO_CNT(p) (((p) & 0xffff) << 16)
152#define LPRX_TO_CNT(p) ((p) & 0xffff)
153
Philippe CORNU1df82a62017-08-01 15:23:07 +0200154#define DSI_HS_RD_TO_CNT 0x7c
155#define DSI_LP_RD_TO_CNT 0x80
156#define DSI_HS_WR_TO_CNT 0x84
157#define DSI_LP_WR_TO_CNT 0x88
Philippe CORNU46fc5152017-07-17 09:40:20 +0200158#define DSI_BTA_TO_CNT 0x8c
Philippe CORNU1df82a62017-08-01 15:23:07 +0200159
Philippe CORNU46fc5152017-07-17 09:40:20 +0200160#define DSI_LPCLK_CTRL 0x94
161#define AUTO_CLKLANE_CTRL BIT(1)
162#define PHY_TXREQUESTCLKHS BIT(0)
163
164#define DSI_PHY_TMR_LPCLK_CFG 0x98
165#define PHY_CLKHS2LP_TIME(lbcc) (((lbcc) & 0x3ff) << 16)
166#define PHY_CLKLP2HS_TIME(lbcc) ((lbcc) & 0x3ff)
167
Philippe CORNU1df82a62017-08-01 15:23:07 +0200168/* TODO Next register is slightly different between 1.30 & 1.31 IP version */
Philippe CORNU46fc5152017-07-17 09:40:20 +0200169#define DSI_PHY_TMR_CFG 0x9c
170#define PHY_HS2LP_TIME(lbcc) (((lbcc) & 0xff) << 24)
171#define PHY_LP2HS_TIME(lbcc) (((lbcc) & 0xff) << 16)
172#define MAX_RD_TIME(lbcc) ((lbcc) & 0x7fff)
173
174#define DSI_PHY_RSTZ 0xa0
175#define PHY_DISFORCEPLL 0
176#define PHY_ENFORCEPLL BIT(3)
177#define PHY_DISABLECLK 0
178#define PHY_ENABLECLK BIT(2)
179#define PHY_RSTZ 0
180#define PHY_UNRSTZ BIT(1)
181#define PHY_SHUTDOWNZ 0
182#define PHY_UNSHUTDOWNZ BIT(0)
183
184#define DSI_PHY_IF_CFG 0xa4
Philippe CORNU46fc5152017-07-17 09:40:20 +0200185#define PHY_STOP_WAIT_TIME(cycle) (((cycle) & 0xff) << 8)
Philippe CORNU1df82a62017-08-01 15:23:07 +0200186#define N_LANES(n) (((n) - 1) & 0x3)
187
188#define DSI_PHY_ULPS_CTRL 0xa8
189#define DSI_PHY_TX_TRIGGERS 0xac
Philippe CORNU46fc5152017-07-17 09:40:20 +0200190
191#define DSI_PHY_STATUS 0xb0
Philippe CORNU1df82a62017-08-01 15:23:07 +0200192#define PHY_STOP_STATE_CLK_LANE BIT(2)
193#define PHY_LOCK BIT(0)
Philippe CORNU46fc5152017-07-17 09:40:20 +0200194
195#define DSI_PHY_TST_CTRL0 0xb4
196#define PHY_TESTCLK BIT(1)
197#define PHY_UNTESTCLK 0
198#define PHY_TESTCLR BIT(0)
199#define PHY_UNTESTCLR 0
200
201#define DSI_PHY_TST_CTRL1 0xb8
202#define PHY_TESTEN BIT(16)
203#define PHY_UNTESTEN 0
204#define PHY_TESTDOUT(n) (((n) & 0xff) << 8)
Philippe CORNU1df82a62017-08-01 15:23:07 +0200205#define PHY_TESTDIN(n) ((n) & 0xff)
Philippe CORNU46fc5152017-07-17 09:40:20 +0200206
207#define DSI_INT_ST0 0xbc
208#define DSI_INT_ST1 0xc0
209#define DSI_INT_MSK0 0xc4
210#define DSI_INT_MSK1 0xc8
Philippe CORNU1df82a62017-08-01 15:23:07 +0200211#define DSI_PHY_TMR_RD_CFG 0xf4
Philippe CORNU46fc5152017-07-17 09:40:20 +0200212
213#define PHY_STATUS_TIMEOUT_US 10000
214#define CMD_PKT_STATUS_TIMEOUT_US 20000
215
216struct dw_mipi_dsi {
217 struct drm_bridge bridge;
218 struct mipi_dsi_host dsi_host;
219 struct drm_bridge *panel_bridge;
Philippe CORNU46fc5152017-07-17 09:40:20 +0200220 struct device *dev;
221 void __iomem *base;
222
223 struct clk *pclk;
224
225 unsigned int lane_mbps; /* per lane */
226 u32 channel;
227 u32 lanes;
228 u32 format;
229 unsigned long mode_flags;
230
231 const struct dw_mipi_dsi_plat_data *plat_data;
232};
233
234/*
235 * The controller should generate 2 frames before
236 * preparing the peripheral.
237 */
238static void dw_mipi_dsi_wait_for_two_frames(struct drm_display_mode *mode)
239{
240 int refresh, two_frames;
241
242 refresh = drm_mode_vrefresh(mode);
243 two_frames = DIV_ROUND_UP(MSEC_PER_SEC, refresh) * 2;
244 msleep(two_frames);
245}
246
247static inline struct dw_mipi_dsi *host_to_dsi(struct mipi_dsi_host *host)
248{
249 return container_of(host, struct dw_mipi_dsi, dsi_host);
250}
251
252static inline struct dw_mipi_dsi *bridge_to_dsi(struct drm_bridge *bridge)
253{
254 return container_of(bridge, struct dw_mipi_dsi, bridge);
255}
256
257static inline void dsi_write(struct dw_mipi_dsi *dsi, u32 reg, u32 val)
258{
259 writel(val, dsi->base + reg);
260}
261
262static inline u32 dsi_read(struct dw_mipi_dsi *dsi, u32 reg)
263{
264 return readl(dsi->base + reg);
265}
266
267static int dw_mipi_dsi_host_attach(struct mipi_dsi_host *host,
268 struct mipi_dsi_device *device)
269{
270 struct dw_mipi_dsi *dsi = host_to_dsi(host);
271 struct drm_bridge *bridge;
272 struct drm_panel *panel;
273 int ret;
274
275 if (device->lanes > dsi->plat_data->max_data_lanes) {
276 dev_err(dsi->dev, "the number of data lanes(%u) is too many\n",
277 device->lanes);
278 return -EINVAL;
279 }
280
281 dsi->lanes = device->lanes;
282 dsi->channel = device->channel;
283 dsi->format = device->format;
284 dsi->mode_flags = device->mode_flags;
285
286 ret = drm_of_find_panel_or_bridge(host->dev->of_node, 1, 0,
287 &panel, &bridge);
288 if (ret)
289 return ret;
290
291 if (panel) {
292 bridge = drm_panel_bridge_add(panel, DRM_MODE_CONNECTOR_DSI);
293 if (IS_ERR(bridge))
294 return PTR_ERR(bridge);
Philippe CORNU46fc5152017-07-17 09:40:20 +0200295 }
296
297 dsi->panel_bridge = bridge;
298
299 drm_bridge_add(&dsi->bridge);
300
301 return 0;
302}
303
304static int dw_mipi_dsi_host_detach(struct mipi_dsi_host *host,
305 struct mipi_dsi_device *device)
306{
307 struct dw_mipi_dsi *dsi = host_to_dsi(host);
308
benjamin.gaignard@linaro.orgd5532f62017-10-02 11:34:48 +0200309 drm_of_panel_bridge_remove(host->dev->of_node, 1, 0);
Philippe CORNU46fc5152017-07-17 09:40:20 +0200310
311 drm_bridge_remove(&dsi->bridge);
312
313 return 0;
314}
315
316static void dw_mipi_message_config(struct dw_mipi_dsi *dsi,
317 const struct mipi_dsi_msg *msg)
318{
319 bool lpm = msg->flags & MIPI_DSI_MSG_USE_LPM;
320 u32 val = 0;
321
322 if (msg->flags & MIPI_DSI_MSG_REQ_ACK)
Philippe CORNU1df82a62017-08-01 15:23:07 +0200323 val |= ACK_RQST_EN;
Philippe CORNU46fc5152017-07-17 09:40:20 +0200324 if (lpm)
325 val |= CMD_MODE_ALL_LP;
326
327 dsi_write(dsi, DSI_LPCLK_CTRL, lpm ? 0 : PHY_TXREQUESTCLKHS);
328 dsi_write(dsi, DSI_CMD_MODE_CFG, val);
329}
330
331static int dw_mipi_dsi_gen_pkt_hdr_write(struct dw_mipi_dsi *dsi, u32 hdr_val)
332{
333 int ret;
334 u32 val, mask;
335
336 ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
337 val, !(val & GEN_CMD_FULL), 1000,
338 CMD_PKT_STATUS_TIMEOUT_US);
339 if (ret < 0) {
340 dev_err(dsi->dev, "failed to get available command FIFO\n");
341 return ret;
342 }
343
344 dsi_write(dsi, DSI_GEN_HDR, hdr_val);
345
346 mask = GEN_CMD_EMPTY | GEN_PLD_W_EMPTY;
347 ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
348 val, (val & mask) == mask,
349 1000, CMD_PKT_STATUS_TIMEOUT_US);
350 if (ret < 0) {
351 dev_err(dsi->dev, "failed to write command FIFO\n");
352 return ret;
353 }
354
355 return 0;
356}
357
Brian Norrisad95dc82018-01-09 12:32:47 -0800358static int dw_mipi_dsi_write(struct dw_mipi_dsi *dsi,
359 const struct mipi_dsi_packet *packet)
Philippe CORNU46fc5152017-07-17 09:40:20 +0200360{
Brian Norrisad95dc82018-01-09 12:32:47 -0800361 const u8 *tx_buf = packet->payload;
362 int len = packet->payload_length, pld_data_bytes = sizeof(u32), ret;
Brian Norrisfd2cb712018-01-09 12:32:48 -0800363 __le32 word;
Philippe CORNU46fc5152017-07-17 09:40:20 +0200364 u32 val;
365
Brian Norrisad95dc82018-01-09 12:32:47 -0800366 while (len) {
Philippe CORNU46fc5152017-07-17 09:40:20 +0200367 if (len < pld_data_bytes) {
Brian Norrisfd2cb712018-01-09 12:32:48 -0800368 word = 0;
369 memcpy(&word, tx_buf, len);
370 dsi_write(dsi, DSI_GEN_PLD_DATA, le32_to_cpu(word));
Philippe CORNU46fc5152017-07-17 09:40:20 +0200371 len = 0;
372 } else {
Brian Norrisfd2cb712018-01-09 12:32:48 -0800373 memcpy(&word, tx_buf, pld_data_bytes);
374 dsi_write(dsi, DSI_GEN_PLD_DATA, le32_to_cpu(word));
Philippe CORNU46fc5152017-07-17 09:40:20 +0200375 tx_buf += pld_data_bytes;
376 len -= pld_data_bytes;
377 }
378
379 ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
380 val, !(val & GEN_PLD_W_FULL), 1000,
381 CMD_PKT_STATUS_TIMEOUT_US);
382 if (ret < 0) {
383 dev_err(dsi->dev,
384 "failed to get available write payload FIFO\n");
385 return ret;
386 }
387 }
388
Brian Norrisfd2cb712018-01-09 12:32:48 -0800389 word = 0;
390 memcpy(&word, packet->header, sizeof(packet->header));
391 return dw_mipi_dsi_gen_pkt_hdr_write(dsi, le32_to_cpu(word));
Philippe CORNU46fc5152017-07-17 09:40:20 +0200392}
393
Philippe Cornua009c532018-02-04 22:31:04 +0100394static int dw_mipi_dsi_read(struct dw_mipi_dsi *dsi,
395 const struct mipi_dsi_msg *msg)
396{
397 int i, j, ret, len = msg->rx_len;
398 u8 *buf = msg->rx_buf;
399 u32 val;
400
401 /* Wait end of the read operation */
402 ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
403 val, !(val & GEN_RD_CMD_BUSY),
404 1000, CMD_PKT_STATUS_TIMEOUT_US);
405 if (ret) {
406 dev_err(dsi->dev, "Timeout during read operation\n");
407 return ret;
408 }
409
410 for (i = 0; i < len; i += 4) {
411 /* Read fifo must not be empty before all bytes are read */
412 ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
413 val, !(val & GEN_PLD_R_EMPTY),
414 1000, CMD_PKT_STATUS_TIMEOUT_US);
415 if (ret) {
416 dev_err(dsi->dev, "Read payload FIFO is empty\n");
417 return ret;
418 }
419
420 val = dsi_read(dsi, DSI_GEN_PLD_DATA);
421 for (j = 0; j < 4 && j + i < len; j++)
422 buf[i + j] = val >> (8 * j);
423 }
424
425 return ret;
426}
427
Philippe CORNU46fc5152017-07-17 09:40:20 +0200428static ssize_t dw_mipi_dsi_host_transfer(struct mipi_dsi_host *host,
429 const struct mipi_dsi_msg *msg)
430{
431 struct dw_mipi_dsi *dsi = host_to_dsi(host);
Brian Norrisad95dc82018-01-09 12:32:47 -0800432 struct mipi_dsi_packet packet;
Philippe Cornua009c532018-02-04 22:31:04 +0100433 int ret, nb_bytes;
Philippe Cornu4cda6e82018-01-25 11:37:59 +0100434
Brian Norrisad95dc82018-01-09 12:32:47 -0800435 ret = mipi_dsi_create_packet(&packet, msg);
436 if (ret) {
437 dev_err(dsi->dev, "failed to create packet: %d\n", ret);
438 return ret;
Philippe CORNU46fc5152017-07-17 09:40:20 +0200439 }
440
Brian Norrisad95dc82018-01-09 12:32:47 -0800441 dw_mipi_message_config(dsi, msg);
442
Philippe Cornu1a1c1302018-01-25 11:38:00 +0100443 ret = dw_mipi_dsi_write(dsi, &packet);
444 if (ret)
445 return ret;
446
Philippe Cornua009c532018-02-04 22:31:04 +0100447 if (msg->rx_buf && msg->rx_len) {
448 ret = dw_mipi_dsi_read(dsi, msg);
449 if (ret)
450 return ret;
451 nb_bytes = msg->rx_len;
452 } else {
453 nb_bytes = packet.size;
454 }
455
456 return nb_bytes;
Philippe CORNU46fc5152017-07-17 09:40:20 +0200457}
458
459static const struct mipi_dsi_host_ops dw_mipi_dsi_host_ops = {
460 .attach = dw_mipi_dsi_host_attach,
461 .detach = dw_mipi_dsi_host_detach,
462 .transfer = dw_mipi_dsi_host_transfer,
463};
464
465static void dw_mipi_dsi_video_mode_config(struct dw_mipi_dsi *dsi)
466{
467 u32 val;
468
469 /*
470 * TODO dw drv improvements
471 * enabling low power is panel-dependent, we should use the
472 * panel configuration here...
473 */
474 val = ENABLE_LOW_POWER;
475
476 if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_BURST)
477 val |= VID_MODE_TYPE_BURST;
478 else if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
479 val |= VID_MODE_TYPE_NON_BURST_SYNC_PULSES;
480 else
481 val |= VID_MODE_TYPE_NON_BURST_SYNC_EVENTS;
482
483 dsi_write(dsi, DSI_VID_MODE_CFG, val);
484}
485
486static void dw_mipi_dsi_set_mode(struct dw_mipi_dsi *dsi,
487 unsigned long mode_flags)
488{
489 dsi_write(dsi, DSI_PWR_UP, RESET);
490
491 if (mode_flags & MIPI_DSI_MODE_VIDEO) {
492 dsi_write(dsi, DSI_MODE_CFG, ENABLE_VIDEO_MODE);
493 dw_mipi_dsi_video_mode_config(dsi);
494 dsi_write(dsi, DSI_LPCLK_CTRL, PHY_TXREQUESTCLKHS);
495 } else {
496 dsi_write(dsi, DSI_MODE_CFG, ENABLE_CMD_MODE);
497 }
498
499 dsi_write(dsi, DSI_PWR_UP, POWERUP);
500}
501
502static void dw_mipi_dsi_disable(struct dw_mipi_dsi *dsi)
503{
504 dsi_write(dsi, DSI_PWR_UP, RESET);
505 dsi_write(dsi, DSI_PHY_RSTZ, PHY_RSTZ);
506}
507
508static void dw_mipi_dsi_init(struct dw_mipi_dsi *dsi)
509{
510 /*
511 * The maximum permitted escape clock is 20MHz and it is derived from
512 * lanebyteclk, which is running at "lane_mbps / 8". Thus we want:
513 *
514 * (lane_mbps >> 3) / esc_clk_division < 20
515 * which is:
516 * (lane_mbps >> 3) / 20 > esc_clk_division
517 */
518 u32 esc_clk_division = (dsi->lane_mbps >> 3) / 20 + 1;
519
520 dsi_write(dsi, DSI_PWR_UP, RESET);
521
522 /*
523 * TODO dw drv improvements
524 * timeout clock division should be computed with the
525 * high speed transmission counter timeout and byte lane...
526 */
Philippe CORNU1df82a62017-08-01 15:23:07 +0200527 dsi_write(dsi, DSI_CLKMGR_CFG, TO_CLK_DIVISION(10) |
528 TX_ESC_CLK_DIVISION(esc_clk_division));
Philippe CORNU46fc5152017-07-17 09:40:20 +0200529}
530
531static void dw_mipi_dsi_dpi_config(struct dw_mipi_dsi *dsi,
532 struct drm_display_mode *mode)
533{
534 u32 val = 0, color = 0;
535
536 switch (dsi->format) {
537 case MIPI_DSI_FMT_RGB888:
538 color = DPI_COLOR_CODING_24BIT;
539 break;
540 case MIPI_DSI_FMT_RGB666:
Philippe CORNU1df82a62017-08-01 15:23:07 +0200541 color = DPI_COLOR_CODING_18BIT_2 | LOOSELY18_EN;
Philippe CORNU46fc5152017-07-17 09:40:20 +0200542 break;
543 case MIPI_DSI_FMT_RGB666_PACKED:
544 color = DPI_COLOR_CODING_18BIT_1;
545 break;
546 case MIPI_DSI_FMT_RGB565:
547 color = DPI_COLOR_CODING_16BIT_1;
548 break;
549 }
550
551 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
552 val |= VSYNC_ACTIVE_LOW;
553 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
554 val |= HSYNC_ACTIVE_LOW;
555
Philippe CORNU1df82a62017-08-01 15:23:07 +0200556 dsi_write(dsi, DSI_DPI_VCID, DPI_VCID(dsi->channel));
Philippe CORNU46fc5152017-07-17 09:40:20 +0200557 dsi_write(dsi, DSI_DPI_COLOR_CODING, color);
558 dsi_write(dsi, DSI_DPI_CFG_POL, val);
559 /*
560 * TODO dw drv improvements
561 * largest packet sizes during hfp or during vsa/vpb/vfp
562 * should be computed according to byte lane, lane number and only
563 * if sending lp cmds in high speed is enable (PHY_TXREQUESTCLKHS)
564 */
565 dsi_write(dsi, DSI_DPI_LP_CMD_TIM, OUTVACT_LPCMD_TIME(4)
566 | INVACT_LPCMD_TIME(4));
567}
568
569static void dw_mipi_dsi_packet_handler_config(struct dw_mipi_dsi *dsi)
570{
Philippe CORNU1df82a62017-08-01 15:23:07 +0200571 dsi_write(dsi, DSI_PCKHDL_CFG, CRC_RX_EN | ECC_RX_EN | BTA_EN);
Philippe CORNU46fc5152017-07-17 09:40:20 +0200572}
573
574static void dw_mipi_dsi_video_packet_config(struct dw_mipi_dsi *dsi,
575 struct drm_display_mode *mode)
576{
577 /*
578 * TODO dw drv improvements
579 * only burst mode is supported here. For non-burst video modes,
580 * we should compute DSI_VID_PKT_SIZE, DSI_VCCR.NUMC &
581 * DSI_VNPCR.NPSIZE... especially because this driver supports
582 * non-burst video modes, see dw_mipi_dsi_video_mode_config()...
583 */
584 dsi_write(dsi, DSI_VID_PKT_SIZE, VID_PKT_SIZE(mode->hdisplay));
585}
586
587static void dw_mipi_dsi_command_mode_config(struct dw_mipi_dsi *dsi)
588{
589 /*
590 * TODO dw drv improvements
591 * compute high speed transmission counter timeout according
Philippe CORNU1df82a62017-08-01 15:23:07 +0200592 * to the timeout clock division (TO_CLK_DIVISION) and byte lane...
Philippe CORNU46fc5152017-07-17 09:40:20 +0200593 */
594 dsi_write(dsi, DSI_TO_CNT_CFG, HSTX_TO_CNT(1000) | LPRX_TO_CNT(1000));
595 /*
596 * TODO dw drv improvements
597 * the Bus-Turn-Around Timeout Counter should be computed
598 * according to byte lane...
599 */
600 dsi_write(dsi, DSI_BTA_TO_CNT, 0xd00);
601 dsi_write(dsi, DSI_MODE_CFG, ENABLE_CMD_MODE);
602}
603
604/* Get lane byte clock cycles. */
605static u32 dw_mipi_dsi_get_hcomponent_lbcc(struct dw_mipi_dsi *dsi,
606 struct drm_display_mode *mode,
607 u32 hcomponent)
608{
609 u32 frac, lbcc;
610
611 lbcc = hcomponent * dsi->lane_mbps * MSEC_PER_SEC / 8;
612
613 frac = lbcc % mode->clock;
614 lbcc = lbcc / mode->clock;
615 if (frac)
616 lbcc++;
617
618 return lbcc;
619}
620
621static void dw_mipi_dsi_line_timer_config(struct dw_mipi_dsi *dsi,
622 struct drm_display_mode *mode)
623{
624 u32 htotal, hsa, hbp, lbcc;
625
626 htotal = mode->htotal;
627 hsa = mode->hsync_end - mode->hsync_start;
628 hbp = mode->htotal - mode->hsync_end;
629
630 /*
631 * TODO dw drv improvements
632 * computations below may be improved...
633 */
634 lbcc = dw_mipi_dsi_get_hcomponent_lbcc(dsi, mode, htotal);
635 dsi_write(dsi, DSI_VID_HLINE_TIME, lbcc);
636
637 lbcc = dw_mipi_dsi_get_hcomponent_lbcc(dsi, mode, hsa);
638 dsi_write(dsi, DSI_VID_HSA_TIME, lbcc);
639
640 lbcc = dw_mipi_dsi_get_hcomponent_lbcc(dsi, mode, hbp);
641 dsi_write(dsi, DSI_VID_HBP_TIME, lbcc);
642}
643
644static void dw_mipi_dsi_vertical_timing_config(struct dw_mipi_dsi *dsi,
645 struct drm_display_mode *mode)
646{
647 u32 vactive, vsa, vfp, vbp;
648
649 vactive = mode->vdisplay;
650 vsa = mode->vsync_end - mode->vsync_start;
651 vfp = mode->vsync_start - mode->vdisplay;
652 vbp = mode->vtotal - mode->vsync_end;
653
654 dsi_write(dsi, DSI_VID_VACTIVE_LINES, vactive);
655 dsi_write(dsi, DSI_VID_VSA_LINES, vsa);
656 dsi_write(dsi, DSI_VID_VFP_LINES, vfp);
657 dsi_write(dsi, DSI_VID_VBP_LINES, vbp);
658}
659
660static void dw_mipi_dsi_dphy_timing_config(struct dw_mipi_dsi *dsi)
661{
662 /*
663 * TODO dw drv improvements
664 * data & clock lane timers should be computed according to panel
665 * blankings and to the automatic clock lane control mode...
666 * note: DSI_PHY_TMR_CFG.MAX_RD_TIME should be in line with
667 * DSI_CMD_MODE_CFG.MAX_RD_PKT_SIZE_LP (see CMD_MODE_ALL_LP)
668 */
669 dsi_write(dsi, DSI_PHY_TMR_CFG, PHY_HS2LP_TIME(0x40)
670 | PHY_LP2HS_TIME(0x40) | MAX_RD_TIME(10000));
671
672 dsi_write(dsi, DSI_PHY_TMR_LPCLK_CFG, PHY_CLKHS2LP_TIME(0x40)
673 | PHY_CLKLP2HS_TIME(0x40));
674}
675
676static void dw_mipi_dsi_dphy_interface_config(struct dw_mipi_dsi *dsi)
677{
678 /*
679 * TODO dw drv improvements
680 * stop wait time should be the maximum between host dsi
681 * and panel stop wait times
682 */
683 dsi_write(dsi, DSI_PHY_IF_CFG, PHY_STOP_WAIT_TIME(0x20) |
684 N_LANES(dsi->lanes));
685}
686
687static void dw_mipi_dsi_dphy_init(struct dw_mipi_dsi *dsi)
688{
689 /* Clear PHY state */
690 dsi_write(dsi, DSI_PHY_RSTZ, PHY_DISFORCEPLL | PHY_DISABLECLK
691 | PHY_RSTZ | PHY_SHUTDOWNZ);
692 dsi_write(dsi, DSI_PHY_TST_CTRL0, PHY_UNTESTCLR);
693 dsi_write(dsi, DSI_PHY_TST_CTRL0, PHY_TESTCLR);
694 dsi_write(dsi, DSI_PHY_TST_CTRL0, PHY_UNTESTCLR);
695}
696
697static void dw_mipi_dsi_dphy_enable(struct dw_mipi_dsi *dsi)
698{
699 u32 val;
700 int ret;
701
702 dsi_write(dsi, DSI_PHY_RSTZ, PHY_ENFORCEPLL | PHY_ENABLECLK |
703 PHY_UNRSTZ | PHY_UNSHUTDOWNZ);
704
Philippe CORNU1df82a62017-08-01 15:23:07 +0200705 ret = readl_poll_timeout(dsi->base + DSI_PHY_STATUS, val,
706 val & PHY_LOCK, 1000, PHY_STATUS_TIMEOUT_US);
Philippe CORNU46fc5152017-07-17 09:40:20 +0200707 if (ret < 0)
708 DRM_DEBUG_DRIVER("failed to wait phy lock state\n");
709
710 ret = readl_poll_timeout(dsi->base + DSI_PHY_STATUS,
Philippe CORNU1df82a62017-08-01 15:23:07 +0200711 val, val & PHY_STOP_STATE_CLK_LANE, 1000,
Philippe CORNU46fc5152017-07-17 09:40:20 +0200712 PHY_STATUS_TIMEOUT_US);
713 if (ret < 0)
714 DRM_DEBUG_DRIVER("failed to wait phy clk lane stop state\n");
715}
716
717static void dw_mipi_dsi_clear_err(struct dw_mipi_dsi *dsi)
718{
719 dsi_read(dsi, DSI_INT_ST0);
720 dsi_read(dsi, DSI_INT_ST1);
721 dsi_write(dsi, DSI_INT_MSK0, 0);
722 dsi_write(dsi, DSI_INT_MSK1, 0);
723}
724
725static void dw_mipi_dsi_bridge_post_disable(struct drm_bridge *bridge)
726{
727 struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
728
729 /*
730 * Switch to command mode before panel-bridge post_disable &
731 * panel unprepare.
732 * Note: panel-bridge disable & panel disable has been called
733 * before by the drm framework.
734 */
735 dw_mipi_dsi_set_mode(dsi, 0);
736
737 /*
738 * TODO Only way found to call panel-bridge post_disable &
739 * panel unprepare before the dsi "final" disable...
740 * This needs to be fixed in the drm_bridge framework and the API
741 * needs to be updated to manage our own call chains...
742 */
743 dsi->panel_bridge->funcs->post_disable(dsi->panel_bridge);
744
745 dw_mipi_dsi_disable(dsi);
746 clk_disable_unprepare(dsi->pclk);
747 pm_runtime_put(dsi->dev);
748}
749
Brian Norris552de1b2018-01-09 12:33:19 -0800750static void dw_mipi_dsi_bridge_mode_set(struct drm_bridge *bridge,
751 struct drm_display_mode *mode,
752 struct drm_display_mode *adjusted_mode)
Philippe CORNU46fc5152017-07-17 09:40:20 +0200753{
754 struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
755 const struct dw_mipi_dsi_phy_ops *phy_ops = dsi->plat_data->phy_ops;
756 void *priv_data = dsi->plat_data->priv_data;
757 int ret;
758
759 clk_prepare_enable(dsi->pclk);
760
761 ret = phy_ops->get_lane_mbps(priv_data, mode, dsi->mode_flags,
762 dsi->lanes, dsi->format, &dsi->lane_mbps);
763 if (ret)
764 DRM_DEBUG_DRIVER("Phy get_lane_mbps() failed\n");
765
766 pm_runtime_get_sync(dsi->dev);
767 dw_mipi_dsi_init(dsi);
768 dw_mipi_dsi_dpi_config(dsi, mode);
769 dw_mipi_dsi_packet_handler_config(dsi);
770 dw_mipi_dsi_video_mode_config(dsi);
771 dw_mipi_dsi_video_packet_config(dsi, mode);
772 dw_mipi_dsi_command_mode_config(dsi);
773 dw_mipi_dsi_line_timer_config(dsi, mode);
774 dw_mipi_dsi_vertical_timing_config(dsi, mode);
775
776 dw_mipi_dsi_dphy_init(dsi);
777 dw_mipi_dsi_dphy_timing_config(dsi);
778 dw_mipi_dsi_dphy_interface_config(dsi);
779
780 dw_mipi_dsi_clear_err(dsi);
781
782 ret = phy_ops->init(priv_data);
783 if (ret)
784 DRM_DEBUG_DRIVER("Phy init() failed\n");
785
786 dw_mipi_dsi_dphy_enable(dsi);
787
788 dw_mipi_dsi_wait_for_two_frames(mode);
789
790 /* Switch to cmd mode for panel-bridge pre_enable & panel prepare */
791 dw_mipi_dsi_set_mode(dsi, 0);
792}
793
794static void dw_mipi_dsi_bridge_enable(struct drm_bridge *bridge)
795{
796 struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
797
798 /* Switch to video mode for panel-bridge enable & panel enable */
799 dw_mipi_dsi_set_mode(dsi, MIPI_DSI_MODE_VIDEO);
800}
801
802static enum drm_mode_status
803dw_mipi_dsi_bridge_mode_valid(struct drm_bridge *bridge,
804 const struct drm_display_mode *mode)
805{
806 struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
807 const struct dw_mipi_dsi_plat_data *pdata = dsi->plat_data;
808 enum drm_mode_status mode_status = MODE_OK;
809
810 if (pdata->mode_valid)
811 mode_status = pdata->mode_valid(pdata->priv_data, mode);
812
813 return mode_status;
814}
815
816static int dw_mipi_dsi_bridge_attach(struct drm_bridge *bridge)
817{
818 struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
819
820 if (!bridge->encoder) {
821 DRM_ERROR("Parent encoder object not found\n");
822 return -ENODEV;
823 }
824
825 /* Set the encoder type as caller does not know it */
826 bridge->encoder->encoder_type = DRM_MODE_ENCODER_DSI;
827
828 /* Attach the panel-bridge to the dsi bridge */
829 return drm_bridge_attach(bridge->encoder, dsi->panel_bridge, bridge);
830}
831
Bhumika Goyalf4c35e32017-08-08 21:24:10 +0530832static const struct drm_bridge_funcs dw_mipi_dsi_bridge_funcs = {
Philippe CORNU46fc5152017-07-17 09:40:20 +0200833 .mode_set = dw_mipi_dsi_bridge_mode_set,
834 .enable = dw_mipi_dsi_bridge_enable,
835 .post_disable = dw_mipi_dsi_bridge_post_disable,
836 .mode_valid = dw_mipi_dsi_bridge_mode_valid,
837 .attach = dw_mipi_dsi_bridge_attach,
838};
839
840static struct dw_mipi_dsi *
841__dw_mipi_dsi_probe(struct platform_device *pdev,
842 const struct dw_mipi_dsi_plat_data *plat_data)
843{
844 struct device *dev = &pdev->dev;
845 struct reset_control *apb_rst;
846 struct dw_mipi_dsi *dsi;
847 struct resource *res;
848 int ret;
849
850 dsi = devm_kzalloc(dev, sizeof(*dsi), GFP_KERNEL);
851 if (!dsi)
852 return ERR_PTR(-ENOMEM);
853
854 dsi->dev = dev;
855 dsi->plat_data = plat_data;
856
857 if (!plat_data->phy_ops->init || !plat_data->phy_ops->get_lane_mbps) {
858 DRM_ERROR("Phy not properly configured\n");
859 return ERR_PTR(-ENODEV);
860 }
861
862 if (!plat_data->base) {
863 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
864 if (!res)
865 return ERR_PTR(-ENODEV);
866
867 dsi->base = devm_ioremap_resource(dev, res);
868 if (IS_ERR(dsi->base))
869 return ERR_PTR(-ENODEV);
870
871 } else {
872 dsi->base = plat_data->base;
873 }
874
875 dsi->pclk = devm_clk_get(dev, "pclk");
876 if (IS_ERR(dsi->pclk)) {
877 ret = PTR_ERR(dsi->pclk);
878 dev_err(dev, "Unable to get pclk: %d\n", ret);
879 return ERR_PTR(ret);
880 }
881
882 /*
883 * Note that the reset was not defined in the initial device tree, so
884 * we have to be prepared for it not being found.
885 */
Philippe CORNUb1914062017-08-01 15:23:08 +0200886 apb_rst = devm_reset_control_get_optional_exclusive(dev, "apb");
Philippe CORNU46fc5152017-07-17 09:40:20 +0200887 if (IS_ERR(apb_rst)) {
888 ret = PTR_ERR(apb_rst);
Philippe CORNUb1914062017-08-01 15:23:08 +0200889
890 if (ret != -EPROBE_DEFER)
Philippe CORNU46fc5152017-07-17 09:40:20 +0200891 dev_err(dev, "Unable to get reset control: %d\n", ret);
Philippe CORNUb1914062017-08-01 15:23:08 +0200892
893 return ERR_PTR(ret);
Philippe CORNU46fc5152017-07-17 09:40:20 +0200894 }
895
896 if (apb_rst) {
897 ret = clk_prepare_enable(dsi->pclk);
898 if (ret) {
899 dev_err(dev, "%s: Failed to enable pclk\n", __func__);
900 return ERR_PTR(ret);
901 }
902
903 reset_control_assert(apb_rst);
904 usleep_range(10, 20);
905 reset_control_deassert(apb_rst);
906
907 clk_disable_unprepare(dsi->pclk);
908 }
909
910 pm_runtime_enable(dev);
911
912 dsi->dsi_host.ops = &dw_mipi_dsi_host_ops;
913 dsi->dsi_host.dev = dev;
914 ret = mipi_dsi_host_register(&dsi->dsi_host);
915 if (ret) {
916 dev_err(dev, "Failed to register MIPI host: %d\n", ret);
917 return ERR_PTR(ret);
918 }
919
920 dsi->bridge.driver_private = dsi;
921 dsi->bridge.funcs = &dw_mipi_dsi_bridge_funcs;
922#ifdef CONFIG_OF
923 dsi->bridge.of_node = pdev->dev.of_node;
924#endif
925
Philippe CORNU46fc5152017-07-17 09:40:20 +0200926 return dsi;
927}
928
929static void __dw_mipi_dsi_remove(struct dw_mipi_dsi *dsi)
930{
931 pm_runtime_disable(dsi->dev);
932}
933
934/*
935 * Probe/remove API, used from platforms based on the DRM bridge API.
936 */
Brian Norris8242ecb2017-11-27 17:05:38 -0800937struct dw_mipi_dsi *
938dw_mipi_dsi_probe(struct platform_device *pdev,
939 const struct dw_mipi_dsi_plat_data *plat_data)
Philippe CORNU46fc5152017-07-17 09:40:20 +0200940{
Brian Norris8242ecb2017-11-27 17:05:38 -0800941 return __dw_mipi_dsi_probe(pdev, plat_data);
Philippe CORNU46fc5152017-07-17 09:40:20 +0200942}
943EXPORT_SYMBOL_GPL(dw_mipi_dsi_probe);
944
Brian Norris8242ecb2017-11-27 17:05:38 -0800945void dw_mipi_dsi_remove(struct dw_mipi_dsi *dsi)
Philippe CORNU46fc5152017-07-17 09:40:20 +0200946{
Philippe CORNU46fc5152017-07-17 09:40:20 +0200947 mipi_dsi_host_unregister(&dsi->dsi_host);
948
949 __dw_mipi_dsi_remove(dsi);
950}
951EXPORT_SYMBOL_GPL(dw_mipi_dsi_remove);
952
953/*
954 * Bind/unbind API, used from platforms based on the component framework.
955 */
Brian Norris8242ecb2017-11-27 17:05:38 -0800956struct dw_mipi_dsi *
957dw_mipi_dsi_bind(struct platform_device *pdev, struct drm_encoder *encoder,
958 const struct dw_mipi_dsi_plat_data *plat_data)
Philippe CORNU46fc5152017-07-17 09:40:20 +0200959{
960 struct dw_mipi_dsi *dsi;
961 int ret;
962
963 dsi = __dw_mipi_dsi_probe(pdev, plat_data);
964 if (IS_ERR(dsi))
Brian Norris8242ecb2017-11-27 17:05:38 -0800965 return dsi;
Philippe CORNU46fc5152017-07-17 09:40:20 +0200966
967 ret = drm_bridge_attach(encoder, &dsi->bridge, NULL);
968 if (ret) {
Brian Norris8242ecb2017-11-27 17:05:38 -0800969 dw_mipi_dsi_remove(dsi);
Philippe CORNU46fc5152017-07-17 09:40:20 +0200970 DRM_ERROR("Failed to initialize bridge with drm\n");
Brian Norris8242ecb2017-11-27 17:05:38 -0800971 return ERR_PTR(ret);
Philippe CORNU46fc5152017-07-17 09:40:20 +0200972 }
973
Brian Norris8242ecb2017-11-27 17:05:38 -0800974 return dsi;
Philippe CORNU46fc5152017-07-17 09:40:20 +0200975}
976EXPORT_SYMBOL_GPL(dw_mipi_dsi_bind);
977
Brian Norris8242ecb2017-11-27 17:05:38 -0800978void dw_mipi_dsi_unbind(struct dw_mipi_dsi *dsi)
Philippe CORNU46fc5152017-07-17 09:40:20 +0200979{
Philippe CORNU46fc5152017-07-17 09:40:20 +0200980 __dw_mipi_dsi_remove(dsi);
981}
982EXPORT_SYMBOL_GPL(dw_mipi_dsi_unbind);
983
984MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
985MODULE_AUTHOR("Philippe Cornu <philippe.cornu@st.com>");
986MODULE_DESCRIPTION("DW MIPI DSI host controller driver");
987MODULE_LICENSE("GPL");
988MODULE_ALIAS("platform:dw-mipi-dsi");