blob: 781340d2774c967719bc80f61741c81cdeb6a974 [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 CORNU1df82a62017-08-01 15:23:07 +0200139/* TODO These 2 defines will be reworked thanks to mipi_dsi_create_packet() */
Philippe CORNU46fc5152017-07-17 09:40:20 +0200140#define GEN_HDATA(data) (((data) & 0xffff) << 8)
Philippe CORNU46fc5152017-07-17 09:40:20 +0200141#define GEN_HTYPE(type) (((type) & 0xff) << 0)
Philippe CORNU46fc5152017-07-17 09:40:20 +0200142
143#define DSI_GEN_PLD_DATA 0x70
144
145#define DSI_CMD_PKT_STATUS 0x74
Philippe CORNU46fc5152017-07-17 09:40:20 +0200146#define GEN_RD_CMD_BUSY BIT(6)
Philippe CORNU1df82a62017-08-01 15:23:07 +0200147#define GEN_PLD_R_FULL BIT(5)
148#define GEN_PLD_R_EMPTY BIT(4)
149#define GEN_PLD_W_FULL BIT(3)
150#define GEN_PLD_W_EMPTY BIT(2)
151#define GEN_CMD_FULL BIT(1)
152#define GEN_CMD_EMPTY BIT(0)
Philippe CORNU46fc5152017-07-17 09:40:20 +0200153
154#define DSI_TO_CNT_CFG 0x78
155#define HSTX_TO_CNT(p) (((p) & 0xffff) << 16)
156#define LPRX_TO_CNT(p) ((p) & 0xffff)
157
Philippe CORNU1df82a62017-08-01 15:23:07 +0200158#define DSI_HS_RD_TO_CNT 0x7c
159#define DSI_LP_RD_TO_CNT 0x80
160#define DSI_HS_WR_TO_CNT 0x84
161#define DSI_LP_WR_TO_CNT 0x88
Philippe CORNU46fc5152017-07-17 09:40:20 +0200162#define DSI_BTA_TO_CNT 0x8c
Philippe CORNU1df82a62017-08-01 15:23:07 +0200163
Philippe CORNU46fc5152017-07-17 09:40:20 +0200164#define DSI_LPCLK_CTRL 0x94
165#define AUTO_CLKLANE_CTRL BIT(1)
166#define PHY_TXREQUESTCLKHS BIT(0)
167
168#define DSI_PHY_TMR_LPCLK_CFG 0x98
169#define PHY_CLKHS2LP_TIME(lbcc) (((lbcc) & 0x3ff) << 16)
170#define PHY_CLKLP2HS_TIME(lbcc) ((lbcc) & 0x3ff)
171
Philippe CORNU1df82a62017-08-01 15:23:07 +0200172/* TODO Next register is slightly different between 1.30 & 1.31 IP version */
Philippe CORNU46fc5152017-07-17 09:40:20 +0200173#define DSI_PHY_TMR_CFG 0x9c
174#define PHY_HS2LP_TIME(lbcc) (((lbcc) & 0xff) << 24)
175#define PHY_LP2HS_TIME(lbcc) (((lbcc) & 0xff) << 16)
176#define MAX_RD_TIME(lbcc) ((lbcc) & 0x7fff)
177
178#define DSI_PHY_RSTZ 0xa0
179#define PHY_DISFORCEPLL 0
180#define PHY_ENFORCEPLL BIT(3)
181#define PHY_DISABLECLK 0
182#define PHY_ENABLECLK BIT(2)
183#define PHY_RSTZ 0
184#define PHY_UNRSTZ BIT(1)
185#define PHY_SHUTDOWNZ 0
186#define PHY_UNSHUTDOWNZ BIT(0)
187
188#define DSI_PHY_IF_CFG 0xa4
Philippe CORNU46fc5152017-07-17 09:40:20 +0200189#define PHY_STOP_WAIT_TIME(cycle) (((cycle) & 0xff) << 8)
Philippe CORNU1df82a62017-08-01 15:23:07 +0200190#define N_LANES(n) (((n) - 1) & 0x3)
191
192#define DSI_PHY_ULPS_CTRL 0xa8
193#define DSI_PHY_TX_TRIGGERS 0xac
Philippe CORNU46fc5152017-07-17 09:40:20 +0200194
195#define DSI_PHY_STATUS 0xb0
Philippe CORNU1df82a62017-08-01 15:23:07 +0200196#define PHY_STOP_STATE_CLK_LANE BIT(2)
197#define PHY_LOCK BIT(0)
Philippe CORNU46fc5152017-07-17 09:40:20 +0200198
199#define DSI_PHY_TST_CTRL0 0xb4
200#define PHY_TESTCLK BIT(1)
201#define PHY_UNTESTCLK 0
202#define PHY_TESTCLR BIT(0)
203#define PHY_UNTESTCLR 0
204
205#define DSI_PHY_TST_CTRL1 0xb8
206#define PHY_TESTEN BIT(16)
207#define PHY_UNTESTEN 0
208#define PHY_TESTDOUT(n) (((n) & 0xff) << 8)
Philippe CORNU1df82a62017-08-01 15:23:07 +0200209#define PHY_TESTDIN(n) ((n) & 0xff)
Philippe CORNU46fc5152017-07-17 09:40:20 +0200210
211#define DSI_INT_ST0 0xbc
212#define DSI_INT_ST1 0xc0
213#define DSI_INT_MSK0 0xc4
214#define DSI_INT_MSK1 0xc8
Philippe CORNU1df82a62017-08-01 15:23:07 +0200215#define DSI_PHY_TMR_RD_CFG 0xf4
Philippe CORNU46fc5152017-07-17 09:40:20 +0200216
217#define PHY_STATUS_TIMEOUT_US 10000
218#define CMD_PKT_STATUS_TIMEOUT_US 20000
219
220struct dw_mipi_dsi {
221 struct drm_bridge bridge;
222 struct mipi_dsi_host dsi_host;
223 struct drm_bridge *panel_bridge;
224 bool is_panel_bridge;
225 struct device *dev;
226 void __iomem *base;
227
228 struct clk *pclk;
229
230 unsigned int lane_mbps; /* per lane */
231 u32 channel;
232 u32 lanes;
233 u32 format;
234 unsigned long mode_flags;
235
236 const struct dw_mipi_dsi_plat_data *plat_data;
237};
238
239/*
240 * The controller should generate 2 frames before
241 * preparing the peripheral.
242 */
243static void dw_mipi_dsi_wait_for_two_frames(struct drm_display_mode *mode)
244{
245 int refresh, two_frames;
246
247 refresh = drm_mode_vrefresh(mode);
248 two_frames = DIV_ROUND_UP(MSEC_PER_SEC, refresh) * 2;
249 msleep(two_frames);
250}
251
252static inline struct dw_mipi_dsi *host_to_dsi(struct mipi_dsi_host *host)
253{
254 return container_of(host, struct dw_mipi_dsi, dsi_host);
255}
256
257static inline struct dw_mipi_dsi *bridge_to_dsi(struct drm_bridge *bridge)
258{
259 return container_of(bridge, struct dw_mipi_dsi, bridge);
260}
261
262static inline void dsi_write(struct dw_mipi_dsi *dsi, u32 reg, u32 val)
263{
264 writel(val, dsi->base + reg);
265}
266
267static inline u32 dsi_read(struct dw_mipi_dsi *dsi, u32 reg)
268{
269 return readl(dsi->base + reg);
270}
271
272static int dw_mipi_dsi_host_attach(struct mipi_dsi_host *host,
273 struct mipi_dsi_device *device)
274{
275 struct dw_mipi_dsi *dsi = host_to_dsi(host);
276 struct drm_bridge *bridge;
277 struct drm_panel *panel;
278 int ret;
279
280 if (device->lanes > dsi->plat_data->max_data_lanes) {
281 dev_err(dsi->dev, "the number of data lanes(%u) is too many\n",
282 device->lanes);
283 return -EINVAL;
284 }
285
286 dsi->lanes = device->lanes;
287 dsi->channel = device->channel;
288 dsi->format = device->format;
289 dsi->mode_flags = device->mode_flags;
290
291 ret = drm_of_find_panel_or_bridge(host->dev->of_node, 1, 0,
292 &panel, &bridge);
293 if (ret)
294 return ret;
295
296 if (panel) {
297 bridge = drm_panel_bridge_add(panel, DRM_MODE_CONNECTOR_DSI);
298 if (IS_ERR(bridge))
299 return PTR_ERR(bridge);
300 dsi->is_panel_bridge = true;
301 }
302
303 dsi->panel_bridge = bridge;
304
305 drm_bridge_add(&dsi->bridge);
306
307 return 0;
308}
309
310static int dw_mipi_dsi_host_detach(struct mipi_dsi_host *host,
311 struct mipi_dsi_device *device)
312{
313 struct dw_mipi_dsi *dsi = host_to_dsi(host);
314
315 if (dsi->is_panel_bridge)
316 drm_panel_bridge_remove(dsi->panel_bridge);
317
318 drm_bridge_remove(&dsi->bridge);
319
320 return 0;
321}
322
323static void dw_mipi_message_config(struct dw_mipi_dsi *dsi,
324 const struct mipi_dsi_msg *msg)
325{
326 bool lpm = msg->flags & MIPI_DSI_MSG_USE_LPM;
327 u32 val = 0;
328
329 if (msg->flags & MIPI_DSI_MSG_REQ_ACK)
Philippe CORNU1df82a62017-08-01 15:23:07 +0200330 val |= ACK_RQST_EN;
Philippe CORNU46fc5152017-07-17 09:40:20 +0200331 if (lpm)
332 val |= CMD_MODE_ALL_LP;
333
334 dsi_write(dsi, DSI_LPCLK_CTRL, lpm ? 0 : PHY_TXREQUESTCLKHS);
335 dsi_write(dsi, DSI_CMD_MODE_CFG, val);
336}
337
338static int dw_mipi_dsi_gen_pkt_hdr_write(struct dw_mipi_dsi *dsi, u32 hdr_val)
339{
340 int ret;
341 u32 val, mask;
342
343 ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
344 val, !(val & GEN_CMD_FULL), 1000,
345 CMD_PKT_STATUS_TIMEOUT_US);
346 if (ret < 0) {
347 dev_err(dsi->dev, "failed to get available command FIFO\n");
348 return ret;
349 }
350
351 dsi_write(dsi, DSI_GEN_HDR, hdr_val);
352
353 mask = GEN_CMD_EMPTY | GEN_PLD_W_EMPTY;
354 ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
355 val, (val & mask) == mask,
356 1000, CMD_PKT_STATUS_TIMEOUT_US);
357 if (ret < 0) {
358 dev_err(dsi->dev, "failed to write command FIFO\n");
359 return ret;
360 }
361
362 return 0;
363}
364
365static int dw_mipi_dsi_dcs_short_write(struct dw_mipi_dsi *dsi,
366 const struct mipi_dsi_msg *msg)
367{
368 const u8 *tx_buf = msg->tx_buf;
369 u16 data = 0;
370 u32 val;
371
372 if (msg->tx_len > 0)
373 data |= tx_buf[0];
374 if (msg->tx_len > 1)
375 data |= tx_buf[1] << 8;
376
377 if (msg->tx_len > 2) {
378 dev_err(dsi->dev, "too long tx buf length %zu for short write\n",
379 msg->tx_len);
380 return -EINVAL;
381 }
382
383 val = GEN_HDATA(data) | GEN_HTYPE(msg->type);
384 return dw_mipi_dsi_gen_pkt_hdr_write(dsi, val);
385}
386
387static int dw_mipi_dsi_dcs_long_write(struct dw_mipi_dsi *dsi,
388 const struct mipi_dsi_msg *msg)
389{
390 const u8 *tx_buf = msg->tx_buf;
391 int len = msg->tx_len, pld_data_bytes = sizeof(u32), ret;
392 u32 hdr_val = GEN_HDATA(msg->tx_len) | GEN_HTYPE(msg->type);
393 u32 remainder;
394 u32 val;
395
396 if (msg->tx_len < 3) {
397 dev_err(dsi->dev, "wrong tx buf length %zu for long write\n",
398 msg->tx_len);
399 return -EINVAL;
400 }
401
402 while (DIV_ROUND_UP(len, pld_data_bytes)) {
403 if (len < pld_data_bytes) {
404 remainder = 0;
405 memcpy(&remainder, tx_buf, len);
406 dsi_write(dsi, DSI_GEN_PLD_DATA, remainder);
407 len = 0;
408 } else {
409 memcpy(&remainder, tx_buf, pld_data_bytes);
410 dsi_write(dsi, DSI_GEN_PLD_DATA, remainder);
411 tx_buf += pld_data_bytes;
412 len -= pld_data_bytes;
413 }
414
415 ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
416 val, !(val & GEN_PLD_W_FULL), 1000,
417 CMD_PKT_STATUS_TIMEOUT_US);
418 if (ret < 0) {
419 dev_err(dsi->dev,
420 "failed to get available write payload FIFO\n");
421 return ret;
422 }
423 }
424
425 return dw_mipi_dsi_gen_pkt_hdr_write(dsi, hdr_val);
426}
427
428static 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);
432 int ret;
433
434 /*
435 * TODO dw drv improvements
436 * use mipi_dsi_create_packet() instead of all following
437 * functions and code (no switch cases, no
438 * dw_mipi_dsi_dcs_short_write(), only the loop in long_write...)
439 * and use packet.header...
440 */
441 dw_mipi_message_config(dsi, msg);
442
443 switch (msg->type) {
444 case MIPI_DSI_DCS_SHORT_WRITE:
445 case MIPI_DSI_DCS_SHORT_WRITE_PARAM:
446 case MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE:
447 ret = dw_mipi_dsi_dcs_short_write(dsi, msg);
448 break;
449 case MIPI_DSI_DCS_LONG_WRITE:
450 ret = dw_mipi_dsi_dcs_long_write(dsi, msg);
451 break;
452 default:
453 dev_err(dsi->dev, "unsupported message type 0x%02x\n",
454 msg->type);
455 ret = -EINVAL;
456 }
457
458 return ret;
459}
460
461static const struct mipi_dsi_host_ops dw_mipi_dsi_host_ops = {
462 .attach = dw_mipi_dsi_host_attach,
463 .detach = dw_mipi_dsi_host_detach,
464 .transfer = dw_mipi_dsi_host_transfer,
465};
466
467static void dw_mipi_dsi_video_mode_config(struct dw_mipi_dsi *dsi)
468{
469 u32 val;
470
471 /*
472 * TODO dw drv improvements
473 * enabling low power is panel-dependent, we should use the
474 * panel configuration here...
475 */
476 val = ENABLE_LOW_POWER;
477
478 if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_BURST)
479 val |= VID_MODE_TYPE_BURST;
480 else if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
481 val |= VID_MODE_TYPE_NON_BURST_SYNC_PULSES;
482 else
483 val |= VID_MODE_TYPE_NON_BURST_SYNC_EVENTS;
484
485 dsi_write(dsi, DSI_VID_MODE_CFG, val);
486}
487
488static void dw_mipi_dsi_set_mode(struct dw_mipi_dsi *dsi,
489 unsigned long mode_flags)
490{
491 dsi_write(dsi, DSI_PWR_UP, RESET);
492
493 if (mode_flags & MIPI_DSI_MODE_VIDEO) {
494 dsi_write(dsi, DSI_MODE_CFG, ENABLE_VIDEO_MODE);
495 dw_mipi_dsi_video_mode_config(dsi);
496 dsi_write(dsi, DSI_LPCLK_CTRL, PHY_TXREQUESTCLKHS);
497 } else {
498 dsi_write(dsi, DSI_MODE_CFG, ENABLE_CMD_MODE);
499 }
500
501 dsi_write(dsi, DSI_PWR_UP, POWERUP);
502}
503
504static void dw_mipi_dsi_disable(struct dw_mipi_dsi *dsi)
505{
506 dsi_write(dsi, DSI_PWR_UP, RESET);
507 dsi_write(dsi, DSI_PHY_RSTZ, PHY_RSTZ);
508}
509
510static void dw_mipi_dsi_init(struct dw_mipi_dsi *dsi)
511{
512 /*
513 * The maximum permitted escape clock is 20MHz and it is derived from
514 * lanebyteclk, which is running at "lane_mbps / 8". Thus we want:
515 *
516 * (lane_mbps >> 3) / esc_clk_division < 20
517 * which is:
518 * (lane_mbps >> 3) / 20 > esc_clk_division
519 */
520 u32 esc_clk_division = (dsi->lane_mbps >> 3) / 20 + 1;
521
522 dsi_write(dsi, DSI_PWR_UP, RESET);
523
524 /*
525 * TODO dw drv improvements
526 * timeout clock division should be computed with the
527 * high speed transmission counter timeout and byte lane...
528 */
Philippe CORNU1df82a62017-08-01 15:23:07 +0200529 dsi_write(dsi, DSI_CLKMGR_CFG, TO_CLK_DIVISION(10) |
530 TX_ESC_CLK_DIVISION(esc_clk_division));
Philippe CORNU46fc5152017-07-17 09:40:20 +0200531}
532
533static void dw_mipi_dsi_dpi_config(struct dw_mipi_dsi *dsi,
534 struct drm_display_mode *mode)
535{
536 u32 val = 0, color = 0;
537
538 switch (dsi->format) {
539 case MIPI_DSI_FMT_RGB888:
540 color = DPI_COLOR_CODING_24BIT;
541 break;
542 case MIPI_DSI_FMT_RGB666:
Philippe CORNU1df82a62017-08-01 15:23:07 +0200543 color = DPI_COLOR_CODING_18BIT_2 | LOOSELY18_EN;
Philippe CORNU46fc5152017-07-17 09:40:20 +0200544 break;
545 case MIPI_DSI_FMT_RGB666_PACKED:
546 color = DPI_COLOR_CODING_18BIT_1;
547 break;
548 case MIPI_DSI_FMT_RGB565:
549 color = DPI_COLOR_CODING_16BIT_1;
550 break;
551 }
552
553 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
554 val |= VSYNC_ACTIVE_LOW;
555 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
556 val |= HSYNC_ACTIVE_LOW;
557
Philippe CORNU1df82a62017-08-01 15:23:07 +0200558 dsi_write(dsi, DSI_DPI_VCID, DPI_VCID(dsi->channel));
Philippe CORNU46fc5152017-07-17 09:40:20 +0200559 dsi_write(dsi, DSI_DPI_COLOR_CODING, color);
560 dsi_write(dsi, DSI_DPI_CFG_POL, val);
561 /*
562 * TODO dw drv improvements
563 * largest packet sizes during hfp or during vsa/vpb/vfp
564 * should be computed according to byte lane, lane number and only
565 * if sending lp cmds in high speed is enable (PHY_TXREQUESTCLKHS)
566 */
567 dsi_write(dsi, DSI_DPI_LP_CMD_TIM, OUTVACT_LPCMD_TIME(4)
568 | INVACT_LPCMD_TIME(4));
569}
570
571static void dw_mipi_dsi_packet_handler_config(struct dw_mipi_dsi *dsi)
572{
Philippe CORNU1df82a62017-08-01 15:23:07 +0200573 dsi_write(dsi, DSI_PCKHDL_CFG, CRC_RX_EN | ECC_RX_EN | BTA_EN);
Philippe CORNU46fc5152017-07-17 09:40:20 +0200574}
575
576static void dw_mipi_dsi_video_packet_config(struct dw_mipi_dsi *dsi,
577 struct drm_display_mode *mode)
578{
579 /*
580 * TODO dw drv improvements
581 * only burst mode is supported here. For non-burst video modes,
582 * we should compute DSI_VID_PKT_SIZE, DSI_VCCR.NUMC &
583 * DSI_VNPCR.NPSIZE... especially because this driver supports
584 * non-burst video modes, see dw_mipi_dsi_video_mode_config()...
585 */
586 dsi_write(dsi, DSI_VID_PKT_SIZE, VID_PKT_SIZE(mode->hdisplay));
587}
588
589static void dw_mipi_dsi_command_mode_config(struct dw_mipi_dsi *dsi)
590{
591 /*
592 * TODO dw drv improvements
593 * compute high speed transmission counter timeout according
Philippe CORNU1df82a62017-08-01 15:23:07 +0200594 * to the timeout clock division (TO_CLK_DIVISION) and byte lane...
Philippe CORNU46fc5152017-07-17 09:40:20 +0200595 */
596 dsi_write(dsi, DSI_TO_CNT_CFG, HSTX_TO_CNT(1000) | LPRX_TO_CNT(1000));
597 /*
598 * TODO dw drv improvements
599 * the Bus-Turn-Around Timeout Counter should be computed
600 * according to byte lane...
601 */
602 dsi_write(dsi, DSI_BTA_TO_CNT, 0xd00);
603 dsi_write(dsi, DSI_MODE_CFG, ENABLE_CMD_MODE);
604}
605
606/* Get lane byte clock cycles. */
607static u32 dw_mipi_dsi_get_hcomponent_lbcc(struct dw_mipi_dsi *dsi,
608 struct drm_display_mode *mode,
609 u32 hcomponent)
610{
611 u32 frac, lbcc;
612
613 lbcc = hcomponent * dsi->lane_mbps * MSEC_PER_SEC / 8;
614
615 frac = lbcc % mode->clock;
616 lbcc = lbcc / mode->clock;
617 if (frac)
618 lbcc++;
619
620 return lbcc;
621}
622
623static void dw_mipi_dsi_line_timer_config(struct dw_mipi_dsi *dsi,
624 struct drm_display_mode *mode)
625{
626 u32 htotal, hsa, hbp, lbcc;
627
628 htotal = mode->htotal;
629 hsa = mode->hsync_end - mode->hsync_start;
630 hbp = mode->htotal - mode->hsync_end;
631
632 /*
633 * TODO dw drv improvements
634 * computations below may be improved...
635 */
636 lbcc = dw_mipi_dsi_get_hcomponent_lbcc(dsi, mode, htotal);
637 dsi_write(dsi, DSI_VID_HLINE_TIME, lbcc);
638
639 lbcc = dw_mipi_dsi_get_hcomponent_lbcc(dsi, mode, hsa);
640 dsi_write(dsi, DSI_VID_HSA_TIME, lbcc);
641
642 lbcc = dw_mipi_dsi_get_hcomponent_lbcc(dsi, mode, hbp);
643 dsi_write(dsi, DSI_VID_HBP_TIME, lbcc);
644}
645
646static void dw_mipi_dsi_vertical_timing_config(struct dw_mipi_dsi *dsi,
647 struct drm_display_mode *mode)
648{
649 u32 vactive, vsa, vfp, vbp;
650
651 vactive = mode->vdisplay;
652 vsa = mode->vsync_end - mode->vsync_start;
653 vfp = mode->vsync_start - mode->vdisplay;
654 vbp = mode->vtotal - mode->vsync_end;
655
656 dsi_write(dsi, DSI_VID_VACTIVE_LINES, vactive);
657 dsi_write(dsi, DSI_VID_VSA_LINES, vsa);
658 dsi_write(dsi, DSI_VID_VFP_LINES, vfp);
659 dsi_write(dsi, DSI_VID_VBP_LINES, vbp);
660}
661
662static void dw_mipi_dsi_dphy_timing_config(struct dw_mipi_dsi *dsi)
663{
664 /*
665 * TODO dw drv improvements
666 * data & clock lane timers should be computed according to panel
667 * blankings and to the automatic clock lane control mode...
668 * note: DSI_PHY_TMR_CFG.MAX_RD_TIME should be in line with
669 * DSI_CMD_MODE_CFG.MAX_RD_PKT_SIZE_LP (see CMD_MODE_ALL_LP)
670 */
671 dsi_write(dsi, DSI_PHY_TMR_CFG, PHY_HS2LP_TIME(0x40)
672 | PHY_LP2HS_TIME(0x40) | MAX_RD_TIME(10000));
673
674 dsi_write(dsi, DSI_PHY_TMR_LPCLK_CFG, PHY_CLKHS2LP_TIME(0x40)
675 | PHY_CLKLP2HS_TIME(0x40));
676}
677
678static void dw_mipi_dsi_dphy_interface_config(struct dw_mipi_dsi *dsi)
679{
680 /*
681 * TODO dw drv improvements
682 * stop wait time should be the maximum between host dsi
683 * and panel stop wait times
684 */
685 dsi_write(dsi, DSI_PHY_IF_CFG, PHY_STOP_WAIT_TIME(0x20) |
686 N_LANES(dsi->lanes));
687}
688
689static void dw_mipi_dsi_dphy_init(struct dw_mipi_dsi *dsi)
690{
691 /* Clear PHY state */
692 dsi_write(dsi, DSI_PHY_RSTZ, PHY_DISFORCEPLL | PHY_DISABLECLK
693 | PHY_RSTZ | PHY_SHUTDOWNZ);
694 dsi_write(dsi, DSI_PHY_TST_CTRL0, PHY_UNTESTCLR);
695 dsi_write(dsi, DSI_PHY_TST_CTRL0, PHY_TESTCLR);
696 dsi_write(dsi, DSI_PHY_TST_CTRL0, PHY_UNTESTCLR);
697}
698
699static void dw_mipi_dsi_dphy_enable(struct dw_mipi_dsi *dsi)
700{
701 u32 val;
702 int ret;
703
704 dsi_write(dsi, DSI_PHY_RSTZ, PHY_ENFORCEPLL | PHY_ENABLECLK |
705 PHY_UNRSTZ | PHY_UNSHUTDOWNZ);
706
Philippe CORNU1df82a62017-08-01 15:23:07 +0200707 ret = readl_poll_timeout(dsi->base + DSI_PHY_STATUS, val,
708 val & PHY_LOCK, 1000, PHY_STATUS_TIMEOUT_US);
Philippe CORNU46fc5152017-07-17 09:40:20 +0200709 if (ret < 0)
710 DRM_DEBUG_DRIVER("failed to wait phy lock state\n");
711
712 ret = readl_poll_timeout(dsi->base + DSI_PHY_STATUS,
Philippe CORNU1df82a62017-08-01 15:23:07 +0200713 val, val & PHY_STOP_STATE_CLK_LANE, 1000,
Philippe CORNU46fc5152017-07-17 09:40:20 +0200714 PHY_STATUS_TIMEOUT_US);
715 if (ret < 0)
716 DRM_DEBUG_DRIVER("failed to wait phy clk lane stop state\n");
717}
718
719static void dw_mipi_dsi_clear_err(struct dw_mipi_dsi *dsi)
720{
721 dsi_read(dsi, DSI_INT_ST0);
722 dsi_read(dsi, DSI_INT_ST1);
723 dsi_write(dsi, DSI_INT_MSK0, 0);
724 dsi_write(dsi, DSI_INT_MSK1, 0);
725}
726
727static void dw_mipi_dsi_bridge_post_disable(struct drm_bridge *bridge)
728{
729 struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
730
731 /*
732 * Switch to command mode before panel-bridge post_disable &
733 * panel unprepare.
734 * Note: panel-bridge disable & panel disable has been called
735 * before by the drm framework.
736 */
737 dw_mipi_dsi_set_mode(dsi, 0);
738
739 /*
740 * TODO Only way found to call panel-bridge post_disable &
741 * panel unprepare before the dsi "final" disable...
742 * This needs to be fixed in the drm_bridge framework and the API
743 * needs to be updated to manage our own call chains...
744 */
745 dsi->panel_bridge->funcs->post_disable(dsi->panel_bridge);
746
747 dw_mipi_dsi_disable(dsi);
748 clk_disable_unprepare(dsi->pclk);
749 pm_runtime_put(dsi->dev);
750}
751
752void dw_mipi_dsi_bridge_mode_set(struct drm_bridge *bridge,
753 struct drm_display_mode *mode,
754 struct drm_display_mode *adjusted_mode)
755{
756 struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
757 const struct dw_mipi_dsi_phy_ops *phy_ops = dsi->plat_data->phy_ops;
758 void *priv_data = dsi->plat_data->priv_data;
759 int ret;
760
761 clk_prepare_enable(dsi->pclk);
762
763 ret = phy_ops->get_lane_mbps(priv_data, mode, dsi->mode_flags,
764 dsi->lanes, dsi->format, &dsi->lane_mbps);
765 if (ret)
766 DRM_DEBUG_DRIVER("Phy get_lane_mbps() failed\n");
767
768 pm_runtime_get_sync(dsi->dev);
769 dw_mipi_dsi_init(dsi);
770 dw_mipi_dsi_dpi_config(dsi, mode);
771 dw_mipi_dsi_packet_handler_config(dsi);
772 dw_mipi_dsi_video_mode_config(dsi);
773 dw_mipi_dsi_video_packet_config(dsi, mode);
774 dw_mipi_dsi_command_mode_config(dsi);
775 dw_mipi_dsi_line_timer_config(dsi, mode);
776 dw_mipi_dsi_vertical_timing_config(dsi, mode);
777
778 dw_mipi_dsi_dphy_init(dsi);
779 dw_mipi_dsi_dphy_timing_config(dsi);
780 dw_mipi_dsi_dphy_interface_config(dsi);
781
782 dw_mipi_dsi_clear_err(dsi);
783
784 ret = phy_ops->init(priv_data);
785 if (ret)
786 DRM_DEBUG_DRIVER("Phy init() failed\n");
787
788 dw_mipi_dsi_dphy_enable(dsi);
789
790 dw_mipi_dsi_wait_for_two_frames(mode);
791
792 /* Switch to cmd mode for panel-bridge pre_enable & panel prepare */
793 dw_mipi_dsi_set_mode(dsi, 0);
794}
795
796static void dw_mipi_dsi_bridge_enable(struct drm_bridge *bridge)
797{
798 struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
799
800 /* Switch to video mode for panel-bridge enable & panel enable */
801 dw_mipi_dsi_set_mode(dsi, MIPI_DSI_MODE_VIDEO);
802}
803
804static enum drm_mode_status
805dw_mipi_dsi_bridge_mode_valid(struct drm_bridge *bridge,
806 const struct drm_display_mode *mode)
807{
808 struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
809 const struct dw_mipi_dsi_plat_data *pdata = dsi->plat_data;
810 enum drm_mode_status mode_status = MODE_OK;
811
812 if (pdata->mode_valid)
813 mode_status = pdata->mode_valid(pdata->priv_data, mode);
814
815 return mode_status;
816}
817
818static int dw_mipi_dsi_bridge_attach(struct drm_bridge *bridge)
819{
820 struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
821
822 if (!bridge->encoder) {
823 DRM_ERROR("Parent encoder object not found\n");
824 return -ENODEV;
825 }
826
827 /* Set the encoder type as caller does not know it */
828 bridge->encoder->encoder_type = DRM_MODE_ENCODER_DSI;
829
830 /* Attach the panel-bridge to the dsi bridge */
831 return drm_bridge_attach(bridge->encoder, dsi->panel_bridge, bridge);
832}
833
Bhumika Goyalf4c35e32017-08-08 21:24:10 +0530834static const struct drm_bridge_funcs dw_mipi_dsi_bridge_funcs = {
Philippe CORNU46fc5152017-07-17 09:40:20 +0200835 .mode_set = dw_mipi_dsi_bridge_mode_set,
836 .enable = dw_mipi_dsi_bridge_enable,
837 .post_disable = dw_mipi_dsi_bridge_post_disable,
838 .mode_valid = dw_mipi_dsi_bridge_mode_valid,
839 .attach = dw_mipi_dsi_bridge_attach,
840};
841
842static struct dw_mipi_dsi *
843__dw_mipi_dsi_probe(struct platform_device *pdev,
844 const struct dw_mipi_dsi_plat_data *plat_data)
845{
846 struct device *dev = &pdev->dev;
847 struct reset_control *apb_rst;
848 struct dw_mipi_dsi *dsi;
849 struct resource *res;
850 int ret;
851
852 dsi = devm_kzalloc(dev, sizeof(*dsi), GFP_KERNEL);
853 if (!dsi)
854 return ERR_PTR(-ENOMEM);
855
856 dsi->dev = dev;
857 dsi->plat_data = plat_data;
858
859 if (!plat_data->phy_ops->init || !plat_data->phy_ops->get_lane_mbps) {
860 DRM_ERROR("Phy not properly configured\n");
861 return ERR_PTR(-ENODEV);
862 }
863
864 if (!plat_data->base) {
865 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
866 if (!res)
867 return ERR_PTR(-ENODEV);
868
869 dsi->base = devm_ioremap_resource(dev, res);
870 if (IS_ERR(dsi->base))
871 return ERR_PTR(-ENODEV);
872
873 } else {
874 dsi->base = plat_data->base;
875 }
876
877 dsi->pclk = devm_clk_get(dev, "pclk");
878 if (IS_ERR(dsi->pclk)) {
879 ret = PTR_ERR(dsi->pclk);
880 dev_err(dev, "Unable to get pclk: %d\n", ret);
881 return ERR_PTR(ret);
882 }
883
884 /*
885 * Note that the reset was not defined in the initial device tree, so
886 * we have to be prepared for it not being found.
887 */
888 apb_rst = devm_reset_control_get(dev, "apb");
889 if (IS_ERR(apb_rst)) {
890 ret = PTR_ERR(apb_rst);
891 if (ret == -ENOENT) {
892 apb_rst = NULL;
893 } else {
894 dev_err(dev, "Unable to get reset control: %d\n", ret);
895 return ERR_PTR(ret);
896 }
897 }
898
899 if (apb_rst) {
900 ret = clk_prepare_enable(dsi->pclk);
901 if (ret) {
902 dev_err(dev, "%s: Failed to enable pclk\n", __func__);
903 return ERR_PTR(ret);
904 }
905
906 reset_control_assert(apb_rst);
907 usleep_range(10, 20);
908 reset_control_deassert(apb_rst);
909
910 clk_disable_unprepare(dsi->pclk);
911 }
912
913 pm_runtime_enable(dev);
914
915 dsi->dsi_host.ops = &dw_mipi_dsi_host_ops;
916 dsi->dsi_host.dev = dev;
917 ret = mipi_dsi_host_register(&dsi->dsi_host);
918 if (ret) {
919 dev_err(dev, "Failed to register MIPI host: %d\n", ret);
920 return ERR_PTR(ret);
921 }
922
923 dsi->bridge.driver_private = dsi;
924 dsi->bridge.funcs = &dw_mipi_dsi_bridge_funcs;
925#ifdef CONFIG_OF
926 dsi->bridge.of_node = pdev->dev.of_node;
927#endif
928
929 dev_set_drvdata(dev, dsi);
930
931 return dsi;
932}
933
934static void __dw_mipi_dsi_remove(struct dw_mipi_dsi *dsi)
935{
936 pm_runtime_disable(dsi->dev);
937}
938
939/*
940 * Probe/remove API, used from platforms based on the DRM bridge API.
941 */
942int dw_mipi_dsi_probe(struct platform_device *pdev,
943 const struct dw_mipi_dsi_plat_data *plat_data)
944{
945 struct dw_mipi_dsi *dsi;
946
947 dsi = __dw_mipi_dsi_probe(pdev, plat_data);
948 if (IS_ERR(dsi))
949 return PTR_ERR(dsi);
950
951 return 0;
952}
953EXPORT_SYMBOL_GPL(dw_mipi_dsi_probe);
954
955void dw_mipi_dsi_remove(struct platform_device *pdev)
956{
957 struct dw_mipi_dsi *dsi = platform_get_drvdata(pdev);
958
959 mipi_dsi_host_unregister(&dsi->dsi_host);
960
961 __dw_mipi_dsi_remove(dsi);
962}
963EXPORT_SYMBOL_GPL(dw_mipi_dsi_remove);
964
965/*
966 * Bind/unbind API, used from platforms based on the component framework.
967 */
968int dw_mipi_dsi_bind(struct platform_device *pdev, struct drm_encoder *encoder,
969 const struct dw_mipi_dsi_plat_data *plat_data)
970{
971 struct dw_mipi_dsi *dsi;
972 int ret;
973
974 dsi = __dw_mipi_dsi_probe(pdev, plat_data);
975 if (IS_ERR(dsi))
976 return PTR_ERR(dsi);
977
978 ret = drm_bridge_attach(encoder, &dsi->bridge, NULL);
979 if (ret) {
980 dw_mipi_dsi_remove(pdev);
981 DRM_ERROR("Failed to initialize bridge with drm\n");
982 return ret;
983 }
984
985 return 0;
986}
987EXPORT_SYMBOL_GPL(dw_mipi_dsi_bind);
988
989void dw_mipi_dsi_unbind(struct device *dev)
990{
991 struct dw_mipi_dsi *dsi = dev_get_drvdata(dev);
992
993 __dw_mipi_dsi_remove(dsi);
994}
995EXPORT_SYMBOL_GPL(dw_mipi_dsi_unbind);
996
997MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
998MODULE_AUTHOR("Philippe Cornu <philippe.cornu@st.com>");
999MODULE_DESCRIPTION("DW MIPI DSI host controller driver");
1000MODULE_LICENSE("GPL");
1001MODULE_ALIAS("platform:dw-mipi-dsi");