blob: 37d13ccad2e7548720f9b46c9525ced10ab021aa [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
394static ssize_t dw_mipi_dsi_host_transfer(struct mipi_dsi_host *host,
395 const struct mipi_dsi_msg *msg)
396{
397 struct dw_mipi_dsi *dsi = host_to_dsi(host);
Brian Norrisad95dc82018-01-09 12:32:47 -0800398 struct mipi_dsi_packet packet;
Philippe CORNU46fc5152017-07-17 09:40:20 +0200399 int ret;
400
Philippe Cornu4cda6e82018-01-25 11:37:59 +0100401 if (msg->rx_buf || msg->rx_len) {
402 /* TODO dw drv improvements: implement read feature */
403 dev_warn(dsi->dev, "read operations not yet implemented\n");
404 return -EINVAL;
405 }
406
Brian Norrisad95dc82018-01-09 12:32:47 -0800407 ret = mipi_dsi_create_packet(&packet, msg);
408 if (ret) {
409 dev_err(dsi->dev, "failed to create packet: %d\n", ret);
410 return ret;
Philippe CORNU46fc5152017-07-17 09:40:20 +0200411 }
412
Brian Norrisad95dc82018-01-09 12:32:47 -0800413 dw_mipi_message_config(dsi, msg);
414
415 return dw_mipi_dsi_write(dsi, &packet);
Philippe CORNU46fc5152017-07-17 09:40:20 +0200416}
417
418static const struct mipi_dsi_host_ops dw_mipi_dsi_host_ops = {
419 .attach = dw_mipi_dsi_host_attach,
420 .detach = dw_mipi_dsi_host_detach,
421 .transfer = dw_mipi_dsi_host_transfer,
422};
423
424static void dw_mipi_dsi_video_mode_config(struct dw_mipi_dsi *dsi)
425{
426 u32 val;
427
428 /*
429 * TODO dw drv improvements
430 * enabling low power is panel-dependent, we should use the
431 * panel configuration here...
432 */
433 val = ENABLE_LOW_POWER;
434
435 if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_BURST)
436 val |= VID_MODE_TYPE_BURST;
437 else if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
438 val |= VID_MODE_TYPE_NON_BURST_SYNC_PULSES;
439 else
440 val |= VID_MODE_TYPE_NON_BURST_SYNC_EVENTS;
441
442 dsi_write(dsi, DSI_VID_MODE_CFG, val);
443}
444
445static void dw_mipi_dsi_set_mode(struct dw_mipi_dsi *dsi,
446 unsigned long mode_flags)
447{
448 dsi_write(dsi, DSI_PWR_UP, RESET);
449
450 if (mode_flags & MIPI_DSI_MODE_VIDEO) {
451 dsi_write(dsi, DSI_MODE_CFG, ENABLE_VIDEO_MODE);
452 dw_mipi_dsi_video_mode_config(dsi);
453 dsi_write(dsi, DSI_LPCLK_CTRL, PHY_TXREQUESTCLKHS);
454 } else {
455 dsi_write(dsi, DSI_MODE_CFG, ENABLE_CMD_MODE);
456 }
457
458 dsi_write(dsi, DSI_PWR_UP, POWERUP);
459}
460
461static void dw_mipi_dsi_disable(struct dw_mipi_dsi *dsi)
462{
463 dsi_write(dsi, DSI_PWR_UP, RESET);
464 dsi_write(dsi, DSI_PHY_RSTZ, PHY_RSTZ);
465}
466
467static void dw_mipi_dsi_init(struct dw_mipi_dsi *dsi)
468{
469 /*
470 * The maximum permitted escape clock is 20MHz and it is derived from
471 * lanebyteclk, which is running at "lane_mbps / 8". Thus we want:
472 *
473 * (lane_mbps >> 3) / esc_clk_division < 20
474 * which is:
475 * (lane_mbps >> 3) / 20 > esc_clk_division
476 */
477 u32 esc_clk_division = (dsi->lane_mbps >> 3) / 20 + 1;
478
479 dsi_write(dsi, DSI_PWR_UP, RESET);
480
481 /*
482 * TODO dw drv improvements
483 * timeout clock division should be computed with the
484 * high speed transmission counter timeout and byte lane...
485 */
Philippe CORNU1df82a62017-08-01 15:23:07 +0200486 dsi_write(dsi, DSI_CLKMGR_CFG, TO_CLK_DIVISION(10) |
487 TX_ESC_CLK_DIVISION(esc_clk_division));
Philippe CORNU46fc5152017-07-17 09:40:20 +0200488}
489
490static void dw_mipi_dsi_dpi_config(struct dw_mipi_dsi *dsi,
491 struct drm_display_mode *mode)
492{
493 u32 val = 0, color = 0;
494
495 switch (dsi->format) {
496 case MIPI_DSI_FMT_RGB888:
497 color = DPI_COLOR_CODING_24BIT;
498 break;
499 case MIPI_DSI_FMT_RGB666:
Philippe CORNU1df82a62017-08-01 15:23:07 +0200500 color = DPI_COLOR_CODING_18BIT_2 | LOOSELY18_EN;
Philippe CORNU46fc5152017-07-17 09:40:20 +0200501 break;
502 case MIPI_DSI_FMT_RGB666_PACKED:
503 color = DPI_COLOR_CODING_18BIT_1;
504 break;
505 case MIPI_DSI_FMT_RGB565:
506 color = DPI_COLOR_CODING_16BIT_1;
507 break;
508 }
509
510 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
511 val |= VSYNC_ACTIVE_LOW;
512 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
513 val |= HSYNC_ACTIVE_LOW;
514
Philippe CORNU1df82a62017-08-01 15:23:07 +0200515 dsi_write(dsi, DSI_DPI_VCID, DPI_VCID(dsi->channel));
Philippe CORNU46fc5152017-07-17 09:40:20 +0200516 dsi_write(dsi, DSI_DPI_COLOR_CODING, color);
517 dsi_write(dsi, DSI_DPI_CFG_POL, val);
518 /*
519 * TODO dw drv improvements
520 * largest packet sizes during hfp or during vsa/vpb/vfp
521 * should be computed according to byte lane, lane number and only
522 * if sending lp cmds in high speed is enable (PHY_TXREQUESTCLKHS)
523 */
524 dsi_write(dsi, DSI_DPI_LP_CMD_TIM, OUTVACT_LPCMD_TIME(4)
525 | INVACT_LPCMD_TIME(4));
526}
527
528static void dw_mipi_dsi_packet_handler_config(struct dw_mipi_dsi *dsi)
529{
Philippe CORNU1df82a62017-08-01 15:23:07 +0200530 dsi_write(dsi, DSI_PCKHDL_CFG, CRC_RX_EN | ECC_RX_EN | BTA_EN);
Philippe CORNU46fc5152017-07-17 09:40:20 +0200531}
532
533static void dw_mipi_dsi_video_packet_config(struct dw_mipi_dsi *dsi,
534 struct drm_display_mode *mode)
535{
536 /*
537 * TODO dw drv improvements
538 * only burst mode is supported here. For non-burst video modes,
539 * we should compute DSI_VID_PKT_SIZE, DSI_VCCR.NUMC &
540 * DSI_VNPCR.NPSIZE... especially because this driver supports
541 * non-burst video modes, see dw_mipi_dsi_video_mode_config()...
542 */
543 dsi_write(dsi, DSI_VID_PKT_SIZE, VID_PKT_SIZE(mode->hdisplay));
544}
545
546static void dw_mipi_dsi_command_mode_config(struct dw_mipi_dsi *dsi)
547{
548 /*
549 * TODO dw drv improvements
550 * compute high speed transmission counter timeout according
Philippe CORNU1df82a62017-08-01 15:23:07 +0200551 * to the timeout clock division (TO_CLK_DIVISION) and byte lane...
Philippe CORNU46fc5152017-07-17 09:40:20 +0200552 */
553 dsi_write(dsi, DSI_TO_CNT_CFG, HSTX_TO_CNT(1000) | LPRX_TO_CNT(1000));
554 /*
555 * TODO dw drv improvements
556 * the Bus-Turn-Around Timeout Counter should be computed
557 * according to byte lane...
558 */
559 dsi_write(dsi, DSI_BTA_TO_CNT, 0xd00);
560 dsi_write(dsi, DSI_MODE_CFG, ENABLE_CMD_MODE);
561}
562
563/* Get lane byte clock cycles. */
564static u32 dw_mipi_dsi_get_hcomponent_lbcc(struct dw_mipi_dsi *dsi,
565 struct drm_display_mode *mode,
566 u32 hcomponent)
567{
568 u32 frac, lbcc;
569
570 lbcc = hcomponent * dsi->lane_mbps * MSEC_PER_SEC / 8;
571
572 frac = lbcc % mode->clock;
573 lbcc = lbcc / mode->clock;
574 if (frac)
575 lbcc++;
576
577 return lbcc;
578}
579
580static void dw_mipi_dsi_line_timer_config(struct dw_mipi_dsi *dsi,
581 struct drm_display_mode *mode)
582{
583 u32 htotal, hsa, hbp, lbcc;
584
585 htotal = mode->htotal;
586 hsa = mode->hsync_end - mode->hsync_start;
587 hbp = mode->htotal - mode->hsync_end;
588
589 /*
590 * TODO dw drv improvements
591 * computations below may be improved...
592 */
593 lbcc = dw_mipi_dsi_get_hcomponent_lbcc(dsi, mode, htotal);
594 dsi_write(dsi, DSI_VID_HLINE_TIME, lbcc);
595
596 lbcc = dw_mipi_dsi_get_hcomponent_lbcc(dsi, mode, hsa);
597 dsi_write(dsi, DSI_VID_HSA_TIME, lbcc);
598
599 lbcc = dw_mipi_dsi_get_hcomponent_lbcc(dsi, mode, hbp);
600 dsi_write(dsi, DSI_VID_HBP_TIME, lbcc);
601}
602
603static void dw_mipi_dsi_vertical_timing_config(struct dw_mipi_dsi *dsi,
604 struct drm_display_mode *mode)
605{
606 u32 vactive, vsa, vfp, vbp;
607
608 vactive = mode->vdisplay;
609 vsa = mode->vsync_end - mode->vsync_start;
610 vfp = mode->vsync_start - mode->vdisplay;
611 vbp = mode->vtotal - mode->vsync_end;
612
613 dsi_write(dsi, DSI_VID_VACTIVE_LINES, vactive);
614 dsi_write(dsi, DSI_VID_VSA_LINES, vsa);
615 dsi_write(dsi, DSI_VID_VFP_LINES, vfp);
616 dsi_write(dsi, DSI_VID_VBP_LINES, vbp);
617}
618
619static void dw_mipi_dsi_dphy_timing_config(struct dw_mipi_dsi *dsi)
620{
621 /*
622 * TODO dw drv improvements
623 * data & clock lane timers should be computed according to panel
624 * blankings and to the automatic clock lane control mode...
625 * note: DSI_PHY_TMR_CFG.MAX_RD_TIME should be in line with
626 * DSI_CMD_MODE_CFG.MAX_RD_PKT_SIZE_LP (see CMD_MODE_ALL_LP)
627 */
628 dsi_write(dsi, DSI_PHY_TMR_CFG, PHY_HS2LP_TIME(0x40)
629 | PHY_LP2HS_TIME(0x40) | MAX_RD_TIME(10000));
630
631 dsi_write(dsi, DSI_PHY_TMR_LPCLK_CFG, PHY_CLKHS2LP_TIME(0x40)
632 | PHY_CLKLP2HS_TIME(0x40));
633}
634
635static void dw_mipi_dsi_dphy_interface_config(struct dw_mipi_dsi *dsi)
636{
637 /*
638 * TODO dw drv improvements
639 * stop wait time should be the maximum between host dsi
640 * and panel stop wait times
641 */
642 dsi_write(dsi, DSI_PHY_IF_CFG, PHY_STOP_WAIT_TIME(0x20) |
643 N_LANES(dsi->lanes));
644}
645
646static void dw_mipi_dsi_dphy_init(struct dw_mipi_dsi *dsi)
647{
648 /* Clear PHY state */
649 dsi_write(dsi, DSI_PHY_RSTZ, PHY_DISFORCEPLL | PHY_DISABLECLK
650 | PHY_RSTZ | PHY_SHUTDOWNZ);
651 dsi_write(dsi, DSI_PHY_TST_CTRL0, PHY_UNTESTCLR);
652 dsi_write(dsi, DSI_PHY_TST_CTRL0, PHY_TESTCLR);
653 dsi_write(dsi, DSI_PHY_TST_CTRL0, PHY_UNTESTCLR);
654}
655
656static void dw_mipi_dsi_dphy_enable(struct dw_mipi_dsi *dsi)
657{
658 u32 val;
659 int ret;
660
661 dsi_write(dsi, DSI_PHY_RSTZ, PHY_ENFORCEPLL | PHY_ENABLECLK |
662 PHY_UNRSTZ | PHY_UNSHUTDOWNZ);
663
Philippe CORNU1df82a62017-08-01 15:23:07 +0200664 ret = readl_poll_timeout(dsi->base + DSI_PHY_STATUS, val,
665 val & PHY_LOCK, 1000, PHY_STATUS_TIMEOUT_US);
Philippe CORNU46fc5152017-07-17 09:40:20 +0200666 if (ret < 0)
667 DRM_DEBUG_DRIVER("failed to wait phy lock state\n");
668
669 ret = readl_poll_timeout(dsi->base + DSI_PHY_STATUS,
Philippe CORNU1df82a62017-08-01 15:23:07 +0200670 val, val & PHY_STOP_STATE_CLK_LANE, 1000,
Philippe CORNU46fc5152017-07-17 09:40:20 +0200671 PHY_STATUS_TIMEOUT_US);
672 if (ret < 0)
673 DRM_DEBUG_DRIVER("failed to wait phy clk lane stop state\n");
674}
675
676static void dw_mipi_dsi_clear_err(struct dw_mipi_dsi *dsi)
677{
678 dsi_read(dsi, DSI_INT_ST0);
679 dsi_read(dsi, DSI_INT_ST1);
680 dsi_write(dsi, DSI_INT_MSK0, 0);
681 dsi_write(dsi, DSI_INT_MSK1, 0);
682}
683
684static void dw_mipi_dsi_bridge_post_disable(struct drm_bridge *bridge)
685{
686 struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
687
688 /*
689 * Switch to command mode before panel-bridge post_disable &
690 * panel unprepare.
691 * Note: panel-bridge disable & panel disable has been called
692 * before by the drm framework.
693 */
694 dw_mipi_dsi_set_mode(dsi, 0);
695
696 /*
697 * TODO Only way found to call panel-bridge post_disable &
698 * panel unprepare before the dsi "final" disable...
699 * This needs to be fixed in the drm_bridge framework and the API
700 * needs to be updated to manage our own call chains...
701 */
702 dsi->panel_bridge->funcs->post_disable(dsi->panel_bridge);
703
704 dw_mipi_dsi_disable(dsi);
705 clk_disable_unprepare(dsi->pclk);
706 pm_runtime_put(dsi->dev);
707}
708
Brian Norris552de1b2018-01-09 12:33:19 -0800709static void dw_mipi_dsi_bridge_mode_set(struct drm_bridge *bridge,
710 struct drm_display_mode *mode,
711 struct drm_display_mode *adjusted_mode)
Philippe CORNU46fc5152017-07-17 09:40:20 +0200712{
713 struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
714 const struct dw_mipi_dsi_phy_ops *phy_ops = dsi->plat_data->phy_ops;
715 void *priv_data = dsi->plat_data->priv_data;
716 int ret;
717
718 clk_prepare_enable(dsi->pclk);
719
720 ret = phy_ops->get_lane_mbps(priv_data, mode, dsi->mode_flags,
721 dsi->lanes, dsi->format, &dsi->lane_mbps);
722 if (ret)
723 DRM_DEBUG_DRIVER("Phy get_lane_mbps() failed\n");
724
725 pm_runtime_get_sync(dsi->dev);
726 dw_mipi_dsi_init(dsi);
727 dw_mipi_dsi_dpi_config(dsi, mode);
728 dw_mipi_dsi_packet_handler_config(dsi);
729 dw_mipi_dsi_video_mode_config(dsi);
730 dw_mipi_dsi_video_packet_config(dsi, mode);
731 dw_mipi_dsi_command_mode_config(dsi);
732 dw_mipi_dsi_line_timer_config(dsi, mode);
733 dw_mipi_dsi_vertical_timing_config(dsi, mode);
734
735 dw_mipi_dsi_dphy_init(dsi);
736 dw_mipi_dsi_dphy_timing_config(dsi);
737 dw_mipi_dsi_dphy_interface_config(dsi);
738
739 dw_mipi_dsi_clear_err(dsi);
740
741 ret = phy_ops->init(priv_data);
742 if (ret)
743 DRM_DEBUG_DRIVER("Phy init() failed\n");
744
745 dw_mipi_dsi_dphy_enable(dsi);
746
747 dw_mipi_dsi_wait_for_two_frames(mode);
748
749 /* Switch to cmd mode for panel-bridge pre_enable & panel prepare */
750 dw_mipi_dsi_set_mode(dsi, 0);
751}
752
753static void dw_mipi_dsi_bridge_enable(struct drm_bridge *bridge)
754{
755 struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
756
757 /* Switch to video mode for panel-bridge enable & panel enable */
758 dw_mipi_dsi_set_mode(dsi, MIPI_DSI_MODE_VIDEO);
759}
760
761static enum drm_mode_status
762dw_mipi_dsi_bridge_mode_valid(struct drm_bridge *bridge,
763 const struct drm_display_mode *mode)
764{
765 struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
766 const struct dw_mipi_dsi_plat_data *pdata = dsi->plat_data;
767 enum drm_mode_status mode_status = MODE_OK;
768
769 if (pdata->mode_valid)
770 mode_status = pdata->mode_valid(pdata->priv_data, mode);
771
772 return mode_status;
773}
774
775static int dw_mipi_dsi_bridge_attach(struct drm_bridge *bridge)
776{
777 struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
778
779 if (!bridge->encoder) {
780 DRM_ERROR("Parent encoder object not found\n");
781 return -ENODEV;
782 }
783
784 /* Set the encoder type as caller does not know it */
785 bridge->encoder->encoder_type = DRM_MODE_ENCODER_DSI;
786
787 /* Attach the panel-bridge to the dsi bridge */
788 return drm_bridge_attach(bridge->encoder, dsi->panel_bridge, bridge);
789}
790
Bhumika Goyalf4c35e32017-08-08 21:24:10 +0530791static const struct drm_bridge_funcs dw_mipi_dsi_bridge_funcs = {
Philippe CORNU46fc5152017-07-17 09:40:20 +0200792 .mode_set = dw_mipi_dsi_bridge_mode_set,
793 .enable = dw_mipi_dsi_bridge_enable,
794 .post_disable = dw_mipi_dsi_bridge_post_disable,
795 .mode_valid = dw_mipi_dsi_bridge_mode_valid,
796 .attach = dw_mipi_dsi_bridge_attach,
797};
798
799static struct dw_mipi_dsi *
800__dw_mipi_dsi_probe(struct platform_device *pdev,
801 const struct dw_mipi_dsi_plat_data *plat_data)
802{
803 struct device *dev = &pdev->dev;
804 struct reset_control *apb_rst;
805 struct dw_mipi_dsi *dsi;
806 struct resource *res;
807 int ret;
808
809 dsi = devm_kzalloc(dev, sizeof(*dsi), GFP_KERNEL);
810 if (!dsi)
811 return ERR_PTR(-ENOMEM);
812
813 dsi->dev = dev;
814 dsi->plat_data = plat_data;
815
816 if (!plat_data->phy_ops->init || !plat_data->phy_ops->get_lane_mbps) {
817 DRM_ERROR("Phy not properly configured\n");
818 return ERR_PTR(-ENODEV);
819 }
820
821 if (!plat_data->base) {
822 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
823 if (!res)
824 return ERR_PTR(-ENODEV);
825
826 dsi->base = devm_ioremap_resource(dev, res);
827 if (IS_ERR(dsi->base))
828 return ERR_PTR(-ENODEV);
829
830 } else {
831 dsi->base = plat_data->base;
832 }
833
834 dsi->pclk = devm_clk_get(dev, "pclk");
835 if (IS_ERR(dsi->pclk)) {
836 ret = PTR_ERR(dsi->pclk);
837 dev_err(dev, "Unable to get pclk: %d\n", ret);
838 return ERR_PTR(ret);
839 }
840
841 /*
842 * Note that the reset was not defined in the initial device tree, so
843 * we have to be prepared for it not being found.
844 */
Philippe CORNUb1914062017-08-01 15:23:08 +0200845 apb_rst = devm_reset_control_get_optional_exclusive(dev, "apb");
Philippe CORNU46fc5152017-07-17 09:40:20 +0200846 if (IS_ERR(apb_rst)) {
847 ret = PTR_ERR(apb_rst);
Philippe CORNUb1914062017-08-01 15:23:08 +0200848
849 if (ret != -EPROBE_DEFER)
Philippe CORNU46fc5152017-07-17 09:40:20 +0200850 dev_err(dev, "Unable to get reset control: %d\n", ret);
Philippe CORNUb1914062017-08-01 15:23:08 +0200851
852 return ERR_PTR(ret);
Philippe CORNU46fc5152017-07-17 09:40:20 +0200853 }
854
855 if (apb_rst) {
856 ret = clk_prepare_enable(dsi->pclk);
857 if (ret) {
858 dev_err(dev, "%s: Failed to enable pclk\n", __func__);
859 return ERR_PTR(ret);
860 }
861
862 reset_control_assert(apb_rst);
863 usleep_range(10, 20);
864 reset_control_deassert(apb_rst);
865
866 clk_disable_unprepare(dsi->pclk);
867 }
868
869 pm_runtime_enable(dev);
870
871 dsi->dsi_host.ops = &dw_mipi_dsi_host_ops;
872 dsi->dsi_host.dev = dev;
873 ret = mipi_dsi_host_register(&dsi->dsi_host);
874 if (ret) {
875 dev_err(dev, "Failed to register MIPI host: %d\n", ret);
876 return ERR_PTR(ret);
877 }
878
879 dsi->bridge.driver_private = dsi;
880 dsi->bridge.funcs = &dw_mipi_dsi_bridge_funcs;
881#ifdef CONFIG_OF
882 dsi->bridge.of_node = pdev->dev.of_node;
883#endif
884
Philippe CORNU46fc5152017-07-17 09:40:20 +0200885 return dsi;
886}
887
888static void __dw_mipi_dsi_remove(struct dw_mipi_dsi *dsi)
889{
890 pm_runtime_disable(dsi->dev);
891}
892
893/*
894 * Probe/remove API, used from platforms based on the DRM bridge API.
895 */
Brian Norris8242ecb2017-11-27 17:05:38 -0800896struct dw_mipi_dsi *
897dw_mipi_dsi_probe(struct platform_device *pdev,
898 const struct dw_mipi_dsi_plat_data *plat_data)
Philippe CORNU46fc5152017-07-17 09:40:20 +0200899{
Brian Norris8242ecb2017-11-27 17:05:38 -0800900 return __dw_mipi_dsi_probe(pdev, plat_data);
Philippe CORNU46fc5152017-07-17 09:40:20 +0200901}
902EXPORT_SYMBOL_GPL(dw_mipi_dsi_probe);
903
Brian Norris8242ecb2017-11-27 17:05:38 -0800904void dw_mipi_dsi_remove(struct dw_mipi_dsi *dsi)
Philippe CORNU46fc5152017-07-17 09:40:20 +0200905{
Philippe CORNU46fc5152017-07-17 09:40:20 +0200906 mipi_dsi_host_unregister(&dsi->dsi_host);
907
908 __dw_mipi_dsi_remove(dsi);
909}
910EXPORT_SYMBOL_GPL(dw_mipi_dsi_remove);
911
912/*
913 * Bind/unbind API, used from platforms based on the component framework.
914 */
Brian Norris8242ecb2017-11-27 17:05:38 -0800915struct dw_mipi_dsi *
916dw_mipi_dsi_bind(struct platform_device *pdev, struct drm_encoder *encoder,
917 const struct dw_mipi_dsi_plat_data *plat_data)
Philippe CORNU46fc5152017-07-17 09:40:20 +0200918{
919 struct dw_mipi_dsi *dsi;
920 int ret;
921
922 dsi = __dw_mipi_dsi_probe(pdev, plat_data);
923 if (IS_ERR(dsi))
Brian Norris8242ecb2017-11-27 17:05:38 -0800924 return dsi;
Philippe CORNU46fc5152017-07-17 09:40:20 +0200925
926 ret = drm_bridge_attach(encoder, &dsi->bridge, NULL);
927 if (ret) {
Brian Norris8242ecb2017-11-27 17:05:38 -0800928 dw_mipi_dsi_remove(dsi);
Philippe CORNU46fc5152017-07-17 09:40:20 +0200929 DRM_ERROR("Failed to initialize bridge with drm\n");
Brian Norris8242ecb2017-11-27 17:05:38 -0800930 return ERR_PTR(ret);
Philippe CORNU46fc5152017-07-17 09:40:20 +0200931 }
932
Brian Norris8242ecb2017-11-27 17:05:38 -0800933 return dsi;
Philippe CORNU46fc5152017-07-17 09:40:20 +0200934}
935EXPORT_SYMBOL_GPL(dw_mipi_dsi_bind);
936
Brian Norris8242ecb2017-11-27 17:05:38 -0800937void dw_mipi_dsi_unbind(struct dw_mipi_dsi *dsi)
Philippe CORNU46fc5152017-07-17 09:40:20 +0200938{
Philippe CORNU46fc5152017-07-17 09:40:20 +0200939 __dw_mipi_dsi_remove(dsi);
940}
941EXPORT_SYMBOL_GPL(dw_mipi_dsi_unbind);
942
943MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
944MODULE_AUTHOR("Philippe Cornu <philippe.cornu@st.com>");
945MODULE_DESCRIPTION("DW MIPI DSI host controller driver");
946MODULE_LICENSE("GPL");
947MODULE_ALIAS("platform:dw-mipi-dsi");