1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (c) 2016, Fuzhou Rockchip Electronics Co., Ltd
4 * Copyright (C) STMicroelectronics SA 2017
5 *
6 * Modified by Philippe Cornu <philippe.cornu@st.com>
7 * This generic Synopsys DesignWare MIPI DSI host driver is based on the
8 * Rockchip version from rockchip/dw-mipi-dsi.c with phy & bridge APIs.
9 */
10
11 #include <linux/clk.h>
12 #include <linux/component.h>
13 #include <linux/debugfs.h>
14 #include <linux/iopoll.h>
15 #include <linux/module.h>
16 #include <linux/of_device.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/reset.h>
19
20 #include <video/mipi_display.h>
21
22 #include <drm/bridge/dw_mipi_dsi.h>
23 #include <drm/drm_atomic_helper.h>
24 #include <drm/drm_bridge.h>
25 #include <drm/drm_crtc.h>
26 #include <drm/drm_mipi_dsi.h>
27 #include <drm/drm_modes.h>
28 #include <drm/drm_of.h>
29 #include <drm/drm_panel.h>
30 #include <drm/drm_probe_helper.h>
31 #include <drm/drm_print.h>
32
33 #define HWVER_131 0x31333100 /* IP version 1.31 */
34
35 #define DSI_VERSION 0x00
36 #define VERSION GENMASK(31, 8)
37
38 #define DSI_PWR_UP 0x04
39 #define RESET 0
40 #define POWERUP BIT(0)
41
42 #define DSI_CLKMGR_CFG 0x08
43 #define TO_CLK_DIVISION(div) (((div) & 0xff) << 8)
44 #define TX_ESC_CLK_DIVISION(div) ((div) & 0xff)
45
46 #define DSI_DPI_VCID 0x0c
47 #define DPI_VCID(vcid) ((vcid) & 0x3)
48
49 #define DSI_DPI_COLOR_CODING 0x10
50 #define LOOSELY18_EN BIT(8)
51 #define DPI_COLOR_CODING_16BIT_1 0x0
52 #define DPI_COLOR_CODING_16BIT_2 0x1
53 #define DPI_COLOR_CODING_16BIT_3 0x2
54 #define DPI_COLOR_CODING_18BIT_1 0x3
55 #define DPI_COLOR_CODING_18BIT_2 0x4
56 #define DPI_COLOR_CODING_24BIT 0x5
57
58 #define DSI_DPI_CFG_POL 0x14
59 #define COLORM_ACTIVE_LOW BIT(4)
60 #define SHUTD_ACTIVE_LOW BIT(3)
61 #define HSYNC_ACTIVE_LOW BIT(2)
62 #define VSYNC_ACTIVE_LOW BIT(1)
63 #define DATAEN_ACTIVE_LOW BIT(0)
64
65 #define DSI_DPI_LP_CMD_TIM 0x18
66 #define OUTVACT_LPCMD_TIME(p) (((p) & 0xff) << 16)
67 #define INVACT_LPCMD_TIME(p) ((p) & 0xff)
68
69 #define DSI_DBI_VCID 0x1c
70 #define DSI_DBI_CFG 0x20
71 #define DSI_DBI_PARTITIONING_EN 0x24
72 #define DSI_DBI_CMDSIZE 0x28
73
74 #define DSI_PCKHDL_CFG 0x2c
75 #define CRC_RX_EN BIT(4)
76 #define ECC_RX_EN BIT(3)
77 #define BTA_EN BIT(2)
78 #define EOTP_RX_EN BIT(1)
79 #define EOTP_TX_EN BIT(0)
80
81 #define DSI_GEN_VCID 0x30
82
83 #define DSI_MODE_CFG 0x34
84 #define ENABLE_VIDEO_MODE 0
85 #define ENABLE_CMD_MODE BIT(0)
86
87 #define DSI_VID_MODE_CFG 0x38
88 #define LP_HFP_EN BIT(13)
89 #define LP_HBP_EN BIT(12)
90 #define LP_VACT_EN BIT(11)
91 #define LP_VFP_EN BIT(10)
92 #define LP_VBP_EN BIT(9)
93 #define LP_VSA_EN BIT(8)
94 #define VID_MODE_TYPE_NON_BURST_SYNC_PULSES 0x0
95 #define VID_MODE_TYPE_NON_BURST_SYNC_EVENTS 0x1
96 #define VID_MODE_TYPE_BURST 0x2
97 #define VID_MODE_TYPE_MASK 0x3
98 #define ENABLE_LOW_POWER_CMD BIT(15)
99 #define VID_MODE_VPG_ENABLE BIT(16)
100 #define VID_MODE_VPG_MODE BIT(20)
101 #define VID_MODE_VPG_HORIZONTAL BIT(24)
102
103 #define DSI_VID_PKT_SIZE 0x3c
104 #define VID_PKT_SIZE(p) ((p) & 0x3fff)
105
106 #define DSI_VID_NUM_CHUNKS 0x40
107 #define VID_NUM_CHUNKS(c) ((c) & 0x1fff)
108
109 #define DSI_VID_NULL_SIZE 0x44
110 #define VID_NULL_SIZE(b) ((b) & 0x1fff)
111
112 #define DSI_VID_HSA_TIME 0x48
113 #define DSI_VID_HBP_TIME 0x4c
114 #define DSI_VID_HLINE_TIME 0x50
115 #define DSI_VID_VSA_LINES 0x54
116 #define DSI_VID_VBP_LINES 0x58
117 #define DSI_VID_VFP_LINES 0x5c
118 #define DSI_VID_VACTIVE_LINES 0x60
119 #define DSI_EDPI_CMD_SIZE 0x64
120
121 #define DSI_CMD_MODE_CFG 0x68
122 #define MAX_RD_PKT_SIZE_LP BIT(24)
123 #define DCS_LW_TX_LP BIT(19)
124 #define DCS_SR_0P_TX_LP BIT(18)
125 #define DCS_SW_1P_TX_LP BIT(17)
126 #define DCS_SW_0P_TX_LP BIT(16)
127 #define GEN_LW_TX_LP BIT(14)
128 #define GEN_SR_2P_TX_LP BIT(13)
129 #define GEN_SR_1P_TX_LP BIT(12)
130 #define GEN_SR_0P_TX_LP BIT(11)
131 #define GEN_SW_2P_TX_LP BIT(10)
132 #define GEN_SW_1P_TX_LP BIT(9)
133 #define GEN_SW_0P_TX_LP BIT(8)
134 #define ACK_RQST_EN BIT(1)
135 #define TEAR_FX_EN BIT(0)
136
137 #define CMD_MODE_ALL_LP (MAX_RD_PKT_SIZE_LP | \
138 DCS_LW_TX_LP | \
139 DCS_SR_0P_TX_LP | \
140 DCS_SW_1P_TX_LP | \
141 DCS_SW_0P_TX_LP | \
142 GEN_LW_TX_LP | \
143 GEN_SR_2P_TX_LP | \
144 GEN_SR_1P_TX_LP | \
145 GEN_SR_0P_TX_LP | \
146 GEN_SW_2P_TX_LP | \
147 GEN_SW_1P_TX_LP | \
148 GEN_SW_0P_TX_LP)
149
150 #define DSI_GEN_HDR 0x6c
151 #define DSI_GEN_PLD_DATA 0x70
152
153 #define DSI_CMD_PKT_STATUS 0x74
154 #define GEN_RD_CMD_BUSY BIT(6)
155 #define GEN_PLD_R_FULL BIT(5)
156 #define GEN_PLD_R_EMPTY BIT(4)
157 #define GEN_PLD_W_FULL BIT(3)
158 #define GEN_PLD_W_EMPTY BIT(2)
159 #define GEN_CMD_FULL BIT(1)
160 #define GEN_CMD_EMPTY BIT(0)
161
162 #define DSI_TO_CNT_CFG 0x78
163 #define HSTX_TO_CNT(p) (((p) & 0xffff) << 16)
164 #define LPRX_TO_CNT(p) ((p) & 0xffff)
165
166 #define DSI_HS_RD_TO_CNT 0x7c
167 #define DSI_LP_RD_TO_CNT 0x80
168 #define DSI_HS_WR_TO_CNT 0x84
169 #define DSI_LP_WR_TO_CNT 0x88
170 #define DSI_BTA_TO_CNT 0x8c
171
172 #define DSI_LPCLK_CTRL 0x94
173 #define AUTO_CLKLANE_CTRL BIT(1)
174 #define PHY_TXREQUESTCLKHS BIT(0)
175
176 #define DSI_PHY_TMR_LPCLK_CFG 0x98
177 #define PHY_CLKHS2LP_TIME(lbcc) (((lbcc) & 0x3ff) << 16)
178 #define PHY_CLKLP2HS_TIME(lbcc) ((lbcc) & 0x3ff)
179
180 #define DSI_PHY_TMR_CFG 0x9c
181 #define PHY_HS2LP_TIME(lbcc) (((lbcc) & 0xff) << 24)
182 #define PHY_LP2HS_TIME(lbcc) (((lbcc) & 0xff) << 16)
183 #define MAX_RD_TIME(lbcc) ((lbcc) & 0x7fff)
184 #define PHY_HS2LP_TIME_V131(lbcc) (((lbcc) & 0x3ff) << 16)
185 #define PHY_LP2HS_TIME_V131(lbcc) ((lbcc) & 0x3ff)
186
187 #define DSI_PHY_RSTZ 0xa0
188 #define PHY_DISFORCEPLL 0
189 #define PHY_ENFORCEPLL BIT(3)
190 #define PHY_DISABLECLK 0
191 #define PHY_ENABLECLK BIT(2)
192 #define PHY_RSTZ 0
193 #define PHY_UNRSTZ BIT(1)
194 #define PHY_SHUTDOWNZ 0
195 #define PHY_UNSHUTDOWNZ BIT(0)
196
197 #define DSI_PHY_IF_CFG 0xa4
198 #define PHY_STOP_WAIT_TIME(cycle) (((cycle) & 0xff) << 8)
199 #define N_LANES(n) (((n) - 1) & 0x3)
200
201 #define DSI_PHY_ULPS_CTRL 0xa8
202 #define DSI_PHY_TX_TRIGGERS 0xac
203
204 #define DSI_PHY_STATUS 0xb0
205 #define PHY_STOP_STATE_CLK_LANE BIT(2)
206 #define PHY_LOCK BIT(0)
207
208 #define DSI_PHY_TST_CTRL0 0xb4
209 #define PHY_TESTCLK BIT(1)
210 #define PHY_UNTESTCLK 0
211 #define PHY_TESTCLR BIT(0)
212 #define PHY_UNTESTCLR 0
213
214 #define DSI_PHY_TST_CTRL1 0xb8
215 #define PHY_TESTEN BIT(16)
216 #define PHY_UNTESTEN 0
217 #define PHY_TESTDOUT(n) (((n) & 0xff) << 8)
218 #define PHY_TESTDIN(n) ((n) & 0xff)
219
220 #define DSI_INT_ST0 0xbc
221 #define DSI_INT_ST1 0xc0
222 #define DSI_INT_MSK0 0xc4
223 #define DSI_INT_MSK1 0xc8
224
225 #define DSI_PHY_TMR_RD_CFG 0xf4
226 #define MAX_RD_TIME_V131(lbcc) ((lbcc) & 0x7fff)
227
228 #define PHY_STATUS_TIMEOUT_US 10000
229 #define CMD_PKT_STATUS_TIMEOUT_US 20000
230
231 #ifdef CONFIG_DEBUG_FS
232 #define VPG_DEFS(name, dsi) \
233 ((void __force *)&((*dsi).vpg_defs.name))
234
235 #define REGISTER(name, mask, dsi) \
236 { #name, VPG_DEFS(name, dsi), mask, dsi }
237
238 struct debugfs_entries {
239 const char *name;
240 bool *reg;
241 u32 mask;
242 struct dw_mipi_dsi *dsi;
243 };
244 #endif /* CONFIG_DEBUG_FS */
245
246 struct dw_mipi_dsi {
247 struct drm_bridge bridge;
248 struct drm_connector connector;
249 struct drm_encoder *encoder;
250 struct mipi_dsi_host dsi_host;
251 struct drm_panel *panel;
252 struct drm_bridge *next_bridge;
253 struct device *dev;
254 void __iomem *base;
255
256 struct reset_control *apb_rst;
257
258 unsigned int lane_mbps; /* per lane */
259 u32 channel;
260 u32 lanes;
261 u32 format;
262 struct drm_display_mode mode;
263 unsigned long mode_flags;
264
265 #ifdef CONFIG_DEBUG_FS
266 struct dentry *debugfs;
267 struct debugfs_entries *debugfs_vpg;
268 struct {
269 bool vpg;
270 bool vpg_horizontal;
271 bool vpg_ber_pattern;
272 } vpg_defs;
273 #endif /* CONFIG_DEBUG_FS */
274
275 struct dw_mipi_dsi *master; /* dual-dsi master ptr */
276 struct dw_mipi_dsi *slave; /* dual-dsi slave ptr */
277
278 const struct dw_mipi_dsi_plat_data *plat_data;
279 };
280
281 /*
282 * Check if either a link to a master or slave is present
283 */
dw_mipi_is_dual_mode(struct dw_mipi_dsi * dsi)284 static inline bool dw_mipi_is_dual_mode(struct dw_mipi_dsi *dsi)
285 {
286 return dsi->slave || dsi->master;
287 }
288
289 /*
290 * The controller should generate 2 frames before
291 * preparing the peripheral.
292 */
dw_mipi_dsi_wait_for_two_frames(const struct drm_display_mode * mode)293 static void dw_mipi_dsi_wait_for_two_frames(const struct drm_display_mode *mode)
294 {
295 int refresh, two_frames;
296
297 refresh = drm_mode_vrefresh(mode);
298 two_frames = DIV_ROUND_UP(MSEC_PER_SEC, refresh) * 2;
299 msleep(two_frames);
300 }
301
host_to_dsi(struct mipi_dsi_host * host)302 static inline struct dw_mipi_dsi *host_to_dsi(struct mipi_dsi_host *host)
303 {
304 return container_of(host, struct dw_mipi_dsi, dsi_host);
305 }
306
bridge_to_dsi(struct drm_bridge * bridge)307 static inline struct dw_mipi_dsi *bridge_to_dsi(struct drm_bridge *bridge)
308 {
309 return container_of(bridge, struct dw_mipi_dsi, bridge);
310 }
311
con_to_dsi(struct drm_connector * con)312 static inline struct dw_mipi_dsi *con_to_dsi(struct drm_connector *con)
313 {
314 return container_of(con, struct dw_mipi_dsi, connector);
315 }
316
dsi_write(struct dw_mipi_dsi * dsi,u32 reg,u32 val)317 static inline void dsi_write(struct dw_mipi_dsi *dsi, u32 reg, u32 val)
318 {
319 writel(val, dsi->base + reg);
320 }
321
dsi_read(struct dw_mipi_dsi * dsi,u32 reg)322 static inline u32 dsi_read(struct dw_mipi_dsi *dsi, u32 reg)
323 {
324 return readl(dsi->base + reg);
325 }
326
dw_mipi_dsi_host_attach(struct mipi_dsi_host * host,struct mipi_dsi_device * device)327 static int dw_mipi_dsi_host_attach(struct mipi_dsi_host *host,
328 struct mipi_dsi_device *device)
329 {
330 struct dw_mipi_dsi *dsi = host_to_dsi(host);
331 const struct dw_mipi_dsi_plat_data *pdata = dsi->plat_data;
332 int max_data_lanes = dsi->plat_data->max_data_lanes;
333 int ret;
334
335 dsi->lanes = (device->lanes > max_data_lanes) ? device->lanes / 2 : device->lanes;
336 dsi->channel = device->channel;
337 dsi->format = device->format;
338 dsi->mode_flags = device->mode_flags;
339
340 ret = drm_of_find_panel_or_bridge(host->dev->of_node, 1, -1,
341 &dsi->panel, &dsi->next_bridge);
342 if (ret) {
343 DRM_DEV_ERROR(dsi->dev, "Failed to find panel or bridge: %d\n", ret);
344 return ret;
345 }
346
347 drm_bridge_add(&dsi->bridge);
348
349 if (pdata->host_ops && pdata->host_ops->attach) {
350 ret = pdata->host_ops->attach(pdata->priv_data, device);
351 if (ret < 0)
352 return ret;
353 }
354
355 return 0;
356 }
357
dw_mipi_dsi_host_detach(struct mipi_dsi_host * host,struct mipi_dsi_device * device)358 static int dw_mipi_dsi_host_detach(struct mipi_dsi_host *host,
359 struct mipi_dsi_device *device)
360 {
361 struct dw_mipi_dsi *dsi = host_to_dsi(host);
362 const struct dw_mipi_dsi_plat_data *pdata = dsi->plat_data;
363 int ret;
364
365 if (pdata->host_ops && pdata->host_ops->detach) {
366 ret = pdata->host_ops->detach(pdata->priv_data, device);
367 if (ret < 0)
368 return ret;
369 }
370
371 drm_of_panel_bridge_remove(host->dev->of_node, 1, 0);
372
373 drm_bridge_remove(&dsi->bridge);
374
375 return 0;
376 }
377
dw_mipi_message_config(struct dw_mipi_dsi * dsi,const struct mipi_dsi_msg * msg)378 static void dw_mipi_message_config(struct dw_mipi_dsi *dsi,
379 const struct mipi_dsi_msg *msg)
380 {
381 bool lpm = msg->flags & MIPI_DSI_MSG_USE_LPM;
382 u32 val = 0;
383 u32 ctrl = 0;
384
385 /*
386 * TODO dw drv improvements
387 * largest packet sizes during hfp or during vsa/vpb/vfp
388 * should be computed according to byte lane, lane number and only
389 * if sending lp cmds in high speed is enable (PHY_TXREQUESTCLKHS)
390 */
391 dsi_write(dsi, DSI_DPI_LP_CMD_TIM, OUTVACT_LPCMD_TIME(16)
392 | INVACT_LPCMD_TIME(4));
393
394 if (msg->flags & MIPI_DSI_MSG_REQ_ACK)
395 val |= ACK_RQST_EN;
396 if (lpm)
397 val |= CMD_MODE_ALL_LP;
398
399 dsi_write(dsi, DSI_CMD_MODE_CFG, val);
400
401 val = dsi_read(dsi, DSI_VID_MODE_CFG);
402 ctrl = dsi_read(dsi, DSI_LPCLK_CTRL);
403 if (lpm) {
404 val |= ENABLE_LOW_POWER_CMD;
405 ctrl &= ~PHY_TXREQUESTCLKHS;
406 } else {
407 val &= ~ENABLE_LOW_POWER_CMD;
408 ctrl |= PHY_TXREQUESTCLKHS;
409 }
410
411 dsi_write(dsi, DSI_VID_MODE_CFG, val);
412 dsi_write(dsi, DSI_LPCLK_CTRL, ctrl);
413 }
414
dw_mipi_dsi_gen_pkt_hdr_write(struct dw_mipi_dsi * dsi,u32 hdr_val)415 static int dw_mipi_dsi_gen_pkt_hdr_write(struct dw_mipi_dsi *dsi, u32 hdr_val)
416 {
417 int ret;
418 u32 val, mask;
419
420 ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
421 val, !(val & GEN_CMD_FULL), 1000,
422 CMD_PKT_STATUS_TIMEOUT_US);
423 if (ret) {
424 dev_err(dsi->dev, "failed to get available command FIFO\n");
425 return ret;
426 }
427
428 dsi_write(dsi, DSI_GEN_HDR, hdr_val);
429
430 mask = GEN_CMD_EMPTY | GEN_PLD_W_EMPTY;
431 ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
432 val, (val & mask) == mask,
433 1000, CMD_PKT_STATUS_TIMEOUT_US);
434 if (ret) {
435 dev_err(dsi->dev, "failed to write command FIFO\n");
436 return ret;
437 }
438
439 return 0;
440 }
441
dw_mipi_dsi_write(struct dw_mipi_dsi * dsi,const struct mipi_dsi_packet * packet)442 static int dw_mipi_dsi_write(struct dw_mipi_dsi *dsi,
443 const struct mipi_dsi_packet *packet)
444 {
445 const u8 *tx_buf = packet->payload;
446 int len = packet->payload_length, pld_data_bytes = sizeof(u32), ret;
447 __le32 word;
448 u32 val;
449
450 while (len) {
451 if (len < pld_data_bytes) {
452 word = 0;
453 memcpy(&word, tx_buf, len);
454 dsi_write(dsi, DSI_GEN_PLD_DATA, le32_to_cpu(word));
455 len = 0;
456 } else {
457 memcpy(&word, tx_buf, pld_data_bytes);
458 dsi_write(dsi, DSI_GEN_PLD_DATA, le32_to_cpu(word));
459 tx_buf += pld_data_bytes;
460 len -= pld_data_bytes;
461 }
462
463 ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
464 val, !(val & GEN_PLD_W_FULL), 1000,
465 CMD_PKT_STATUS_TIMEOUT_US);
466 if (ret) {
467 dev_err(dsi->dev,
468 "failed to get available write payload FIFO\n");
469 return ret;
470 }
471 }
472
473 word = 0;
474 memcpy(&word, packet->header, sizeof(packet->header));
475 return dw_mipi_dsi_gen_pkt_hdr_write(dsi, le32_to_cpu(word));
476 }
477
dw_mipi_dsi_read(struct dw_mipi_dsi * dsi,const struct mipi_dsi_msg * msg)478 static int dw_mipi_dsi_read(struct dw_mipi_dsi *dsi,
479 const struct mipi_dsi_msg *msg)
480 {
481 int i, j, ret, len = msg->rx_len;
482 u8 *buf = msg->rx_buf;
483 u32 val;
484
485 /* Wait end of the read operation */
486 ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
487 val, !(val & GEN_RD_CMD_BUSY),
488 1000, CMD_PKT_STATUS_TIMEOUT_US);
489 if (ret) {
490 dev_err(dsi->dev, "Timeout during read operation\n");
491 return ret;
492 }
493
494 for (i = 0; i < len; i += 4) {
495 /* Read fifo must not be empty before all bytes are read */
496 ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
497 val, !(val & GEN_PLD_R_EMPTY),
498 1000, CMD_PKT_STATUS_TIMEOUT_US);
499 if (ret) {
500 dev_err(dsi->dev, "Read payload FIFO is empty\n");
501 return ret;
502 }
503
504 val = dsi_read(dsi, DSI_GEN_PLD_DATA);
505 for (j = 0; j < 4 && j + i < len; j++)
506 buf[i + j] = val >> (8 * j);
507 }
508
509 return ret;
510 }
511
dw_mipi_dsi_host_transfer(struct mipi_dsi_host * host,const struct mipi_dsi_msg * msg)512 static ssize_t dw_mipi_dsi_host_transfer(struct mipi_dsi_host *host,
513 const struct mipi_dsi_msg *msg)
514 {
515 struct dw_mipi_dsi *dsi = host_to_dsi(host);
516 struct mipi_dsi_packet packet;
517 int ret, nb_bytes;
518
519 ret = mipi_dsi_create_packet(&packet, msg);
520 if (ret) {
521 dev_err(dsi->dev, "failed to create packet: %d\n", ret);
522 return ret;
523 }
524
525 dw_mipi_message_config(dsi, msg);
526 if (dsi->slave)
527 dw_mipi_message_config(dsi->slave, msg);
528
529 ret = dw_mipi_dsi_write(dsi, &packet);
530 if (ret)
531 return ret;
532 if (dsi->slave) {
533 ret = dw_mipi_dsi_write(dsi->slave, &packet);
534 if (ret)
535 return ret;
536 }
537
538 if (msg->rx_buf && msg->rx_len) {
539 ret = dw_mipi_dsi_read(dsi, msg);
540 if (ret)
541 return ret;
542 nb_bytes = msg->rx_len;
543 } else {
544 nb_bytes = packet.size;
545 }
546
547 return nb_bytes;
548 }
549
550 static const struct mipi_dsi_host_ops dw_mipi_dsi_host_ops = {
551 .attach = dw_mipi_dsi_host_attach,
552 .detach = dw_mipi_dsi_host_detach,
553 .transfer = dw_mipi_dsi_host_transfer,
554 };
555
dw_mipi_dsi_video_mode_config(struct dw_mipi_dsi * dsi)556 static void dw_mipi_dsi_video_mode_config(struct dw_mipi_dsi *dsi)
557 {
558 u32 val = LP_VSA_EN | LP_VBP_EN | LP_VFP_EN |
559 LP_VACT_EN | LP_HBP_EN | LP_HFP_EN;
560
561 if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_HFP)
562 val &= ~LP_HFP_EN;
563
564 if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_HBP)
565 val &= ~LP_HBP_EN;
566
567 if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_BURST)
568 val |= VID_MODE_TYPE_BURST;
569 else if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
570 val |= VID_MODE_TYPE_NON_BURST_SYNC_PULSES;
571 else
572 val |= VID_MODE_TYPE_NON_BURST_SYNC_EVENTS;
573
574 #ifdef CONFIG_DEBUG_FS
575 if (dsi->vpg_defs.vpg) {
576 val |= VID_MODE_VPG_ENABLE;
577 val |= dsi->vpg_defs.vpg_horizontal ?
578 VID_MODE_VPG_HORIZONTAL : 0;
579 val |= dsi->vpg_defs.vpg_ber_pattern ? VID_MODE_VPG_MODE : 0;
580 }
581 #endif /* CONFIG_DEBUG_FS */
582
583 dsi_write(dsi, DSI_VID_MODE_CFG, val);
584 }
585
dw_mipi_dsi_set_mode(struct dw_mipi_dsi * dsi,unsigned long mode_flags)586 static void dw_mipi_dsi_set_mode(struct dw_mipi_dsi *dsi,
587 unsigned long mode_flags)
588 {
589 dsi_write(dsi, DSI_PWR_UP, RESET);
590
591 if (mode_flags & MIPI_DSI_MODE_VIDEO) {
592 dsi_write(dsi, DSI_MODE_CFG, ENABLE_VIDEO_MODE);
593 dw_mipi_dsi_video_mode_config(dsi);
594 } else {
595 dsi_write(dsi, DSI_MODE_CFG, ENABLE_CMD_MODE);
596 }
597
598 dsi_write(dsi, DSI_PWR_UP, POWERUP);
599 }
600
dw_mipi_dsi_disable(struct dw_mipi_dsi * dsi)601 static void dw_mipi_dsi_disable(struct dw_mipi_dsi *dsi)
602 {
603 dsi_write(dsi, DSI_LPCLK_CTRL, 0);
604 dsi_write(dsi, DSI_EDPI_CMD_SIZE, 0);
605 dw_mipi_dsi_set_mode(dsi, 0);
606 if (dsi->slave)
607 dw_mipi_dsi_disable(dsi->slave);
608 }
609
dw_mipi_dsi_init(struct dw_mipi_dsi * dsi)610 static void dw_mipi_dsi_init(struct dw_mipi_dsi *dsi)
611 {
612 const struct dw_mipi_dsi_phy_ops *phy_ops = dsi->plat_data->phy_ops;
613 unsigned int esc_rate; /* in MHz */
614 u32 esc_clk_division;
615 int ret;
616
617 /*
618 * The maximum permitted escape clock is 20MHz and it is derived from
619 * lanebyteclk, which is running at "lane_mbps / 8".
620 */
621 if (phy_ops->get_esc_clk_rate) {
622 ret = phy_ops->get_esc_clk_rate(dsi->plat_data->priv_data,
623 &esc_rate);
624 if (ret)
625 DRM_DEBUG_DRIVER("Phy get_esc_clk_rate() failed\n");
626 } else
627 esc_rate = 20; /* Default to 20MHz */
628
629 /*
630 * We want :
631 * (lane_mbps >> 3) / esc_clk_division < X
632 * which is:
633 * (lane_mbps >> 3) / X > esc_clk_division
634 */
635 esc_clk_division = (dsi->lane_mbps >> 3) / esc_rate + 1;
636
637 dsi_write(dsi, DSI_PWR_UP, RESET);
638
639 /*
640 * TODO dw drv improvements
641 * timeout clock division should be computed with the
642 * high speed transmission counter timeout and byte lane...
643 */
644 dsi_write(dsi, DSI_CLKMGR_CFG, TO_CLK_DIVISION(10) |
645 TX_ESC_CLK_DIVISION(esc_clk_division));
646 }
647
dw_mipi_dsi_dpi_config(struct dw_mipi_dsi * dsi,const struct drm_display_mode * mode)648 static void dw_mipi_dsi_dpi_config(struct dw_mipi_dsi *dsi,
649 const struct drm_display_mode *mode)
650 {
651 u32 val = 0, color = 0;
652
653 switch (dsi->format) {
654 case MIPI_DSI_FMT_RGB888:
655 color = DPI_COLOR_CODING_24BIT;
656 break;
657 case MIPI_DSI_FMT_RGB666:
658 color = DPI_COLOR_CODING_18BIT_2 | LOOSELY18_EN;
659 break;
660 case MIPI_DSI_FMT_RGB666_PACKED:
661 color = DPI_COLOR_CODING_18BIT_1;
662 break;
663 case MIPI_DSI_FMT_RGB565:
664 color = DPI_COLOR_CODING_16BIT_1;
665 break;
666 }
667
668 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
669 val |= VSYNC_ACTIVE_LOW;
670 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
671 val |= HSYNC_ACTIVE_LOW;
672
673 dsi_write(dsi, DSI_DPI_VCID, DPI_VCID(dsi->channel));
674 dsi_write(dsi, DSI_DPI_COLOR_CODING, color);
675 dsi_write(dsi, DSI_DPI_CFG_POL, val);
676 }
677
dw_mipi_dsi_packet_handler_config(struct dw_mipi_dsi * dsi)678 static void dw_mipi_dsi_packet_handler_config(struct dw_mipi_dsi *dsi)
679 {
680 dsi_write(dsi, DSI_PCKHDL_CFG, CRC_RX_EN | ECC_RX_EN | BTA_EN);
681 }
682
dw_mipi_dsi_video_packet_config(struct dw_mipi_dsi * dsi,const struct drm_display_mode * mode)683 static void dw_mipi_dsi_video_packet_config(struct dw_mipi_dsi *dsi,
684 const struct drm_display_mode *mode)
685 {
686 /*
687 * TODO dw drv improvements
688 * only burst mode is supported here. For non-burst video modes,
689 * we should compute DSI_VID_PKT_SIZE, DSI_VCCR.NUMC &
690 * DSI_VNPCR.NPSIZE... especially because this driver supports
691 * non-burst video modes, see dw_mipi_dsi_video_mode_config()...
692 */
693
694 dsi_write(dsi, DSI_VID_PKT_SIZE,
695 dw_mipi_is_dual_mode(dsi) ?
696 VID_PKT_SIZE(mode->hdisplay / 2) :
697 VID_PKT_SIZE(mode->hdisplay));
698 }
699
dw_mipi_dsi_command_mode_config(struct dw_mipi_dsi * dsi)700 static void dw_mipi_dsi_command_mode_config(struct dw_mipi_dsi *dsi)
701 {
702 /*
703 * TODO dw drv improvements
704 * compute high speed transmission counter timeout according
705 * to the timeout clock division (TO_CLK_DIVISION) and byte lane...
706 */
707 dsi_write(dsi, DSI_TO_CNT_CFG, HSTX_TO_CNT(1000) | LPRX_TO_CNT(1000));
708 /*
709 * TODO dw drv improvements
710 * the Bus-Turn-Around Timeout Counter should be computed
711 * according to byte lane...
712 */
713 dsi_write(dsi, DSI_BTA_TO_CNT, 0xd00);
714 dsi_write(dsi, DSI_MODE_CFG, ENABLE_CMD_MODE);
715 }
716
717 /* Get lane byte clock cycles. */
dw_mipi_dsi_get_hcomponent_lbcc(struct dw_mipi_dsi * dsi,const struct drm_display_mode * mode,u32 hcomponent)718 static u32 dw_mipi_dsi_get_hcomponent_lbcc(struct dw_mipi_dsi *dsi,
719 const struct drm_display_mode *mode,
720 u32 hcomponent)
721 {
722 u32 lbcc;
723
724 lbcc = hcomponent * dsi->lane_mbps * MSEC_PER_SEC / 8;
725
726 if (mode->clock == 0) {
727 DRM_ERROR("dsi mode clock is 0!\n");
728 return 0;
729 }
730
731 return DIV_ROUND_CLOSEST_ULL(lbcc, mode->clock);
732 }
733
dw_mipi_dsi_line_timer_config(struct dw_mipi_dsi * dsi,const struct drm_display_mode * mode)734 static void dw_mipi_dsi_line_timer_config(struct dw_mipi_dsi *dsi,
735 const struct drm_display_mode *mode)
736 {
737 u32 htotal, hsa, hbp, lbcc;
738
739 htotal = mode->htotal;
740 hsa = mode->hsync_end - mode->hsync_start;
741 hbp = mode->htotal - mode->hsync_end;
742
743 /*
744 * TODO dw drv improvements
745 * computations below may be improved...
746 */
747 lbcc = dw_mipi_dsi_get_hcomponent_lbcc(dsi, mode, htotal);
748 dsi_write(dsi, DSI_VID_HLINE_TIME, lbcc);
749
750 lbcc = dw_mipi_dsi_get_hcomponent_lbcc(dsi, mode, hsa);
751 dsi_write(dsi, DSI_VID_HSA_TIME, lbcc);
752
753 lbcc = dw_mipi_dsi_get_hcomponent_lbcc(dsi, mode, hbp);
754 dsi_write(dsi, DSI_VID_HBP_TIME, lbcc);
755 }
756
dw_mipi_dsi_vertical_timing_config(struct dw_mipi_dsi * dsi,const struct drm_display_mode * mode)757 static void dw_mipi_dsi_vertical_timing_config(struct dw_mipi_dsi *dsi,
758 const struct drm_display_mode *mode)
759 {
760 u32 vactive, vsa, vfp, vbp;
761
762 vactive = mode->vdisplay;
763 vsa = mode->vsync_end - mode->vsync_start;
764 vfp = mode->vsync_start - mode->vdisplay;
765 vbp = mode->vtotal - mode->vsync_end;
766
767 dsi_write(dsi, DSI_VID_VACTIVE_LINES, vactive);
768 dsi_write(dsi, DSI_VID_VSA_LINES, vsa);
769 dsi_write(dsi, DSI_VID_VFP_LINES, vfp);
770 dsi_write(dsi, DSI_VID_VBP_LINES, vbp);
771 }
772
dw_mipi_dsi_dphy_timing_config(struct dw_mipi_dsi * dsi)773 static void dw_mipi_dsi_dphy_timing_config(struct dw_mipi_dsi *dsi)
774 {
775 const struct dw_mipi_dsi_phy_ops *phy_ops = dsi->plat_data->phy_ops;
776 struct dw_mipi_dsi_dphy_timing timing;
777 u32 hw_version;
778 int ret;
779
780 ret = phy_ops->get_timing(dsi->plat_data->priv_data,
781 dsi->lane_mbps, &timing);
782 if (ret)
783 DRM_DEV_ERROR(dsi->dev, "Retrieving phy timings failed\n");
784
785 /*
786 * TODO dw drv improvements
787 * data & clock lane timers should be computed according to panel
788 * blankings and to the automatic clock lane control mode...
789 * note: DSI_PHY_TMR_CFG.MAX_RD_TIME should be in line with
790 * DSI_CMD_MODE_CFG.MAX_RD_PKT_SIZE_LP (see CMD_MODE_ALL_LP)
791 */
792
793 hw_version = dsi_read(dsi, DSI_VERSION) & VERSION;
794
795 if (hw_version >= HWVER_131) {
796 dsi_write(dsi, DSI_PHY_TMR_CFG,
797 PHY_HS2LP_TIME_V131(timing.data_hs2lp) |
798 PHY_LP2HS_TIME_V131(timing.data_lp2hs));
799 dsi_write(dsi, DSI_PHY_TMR_RD_CFG, MAX_RD_TIME_V131(10000));
800 } else {
801 dsi_write(dsi, DSI_PHY_TMR_CFG,
802 PHY_HS2LP_TIME(timing.data_hs2lp) |
803 PHY_LP2HS_TIME(timing.data_lp2hs) |
804 MAX_RD_TIME(10000));
805 }
806
807 dsi_write(dsi, DSI_PHY_TMR_LPCLK_CFG,
808 PHY_CLKHS2LP_TIME(timing.clk_hs2lp) |
809 PHY_CLKLP2HS_TIME(timing.clk_lp2hs));
810 }
811
dw_mipi_dsi_dphy_interface_config(struct dw_mipi_dsi * dsi)812 static void dw_mipi_dsi_dphy_interface_config(struct dw_mipi_dsi *dsi)
813 {
814 /*
815 * TODO dw drv improvements
816 * stop wait time should be the maximum between host dsi
817 * and panel stop wait times
818 */
819 dsi_write(dsi, DSI_PHY_IF_CFG, PHY_STOP_WAIT_TIME(0x20) |
820 N_LANES(dsi->lanes));
821 }
822
dw_mipi_dsi_dphy_init(struct dw_mipi_dsi * dsi)823 static void dw_mipi_dsi_dphy_init(struct dw_mipi_dsi *dsi)
824 {
825 /* Clear PHY state */
826 dsi_write(dsi, DSI_PHY_RSTZ, PHY_DISFORCEPLL | PHY_DISABLECLK
827 | PHY_RSTZ | PHY_SHUTDOWNZ);
828 dsi_write(dsi, DSI_PHY_TST_CTRL0, PHY_UNTESTCLR);
829 dsi_write(dsi, DSI_PHY_TST_CTRL0, PHY_TESTCLR);
830 dsi_write(dsi, DSI_PHY_TST_CTRL0, PHY_UNTESTCLR);
831 }
832
dw_mipi_dsi_dphy_enable(struct dw_mipi_dsi * dsi)833 static void dw_mipi_dsi_dphy_enable(struct dw_mipi_dsi *dsi)
834 {
835 u32 val;
836 int ret;
837
838 dsi_write(dsi, DSI_PHY_RSTZ, PHY_ENFORCEPLL | PHY_ENABLECLK |
839 PHY_UNRSTZ | PHY_UNSHUTDOWNZ);
840
841 ret = readl_poll_timeout(dsi->base + DSI_PHY_STATUS, val,
842 val & PHY_LOCK, 1000, PHY_STATUS_TIMEOUT_US);
843 if (ret)
844 DRM_ERROR("failed to wait phy lock state\n");
845
846 ret = readl_poll_timeout(dsi->base + DSI_PHY_STATUS,
847 val, val & PHY_STOP_STATE_CLK_LANE, 1000,
848 PHY_STATUS_TIMEOUT_US);
849 if (ret)
850 DRM_ERROR("failed to wait phy clk lane stop state\n");
851 }
852
dw_mipi_dsi_clear_err(struct dw_mipi_dsi * dsi)853 static void dw_mipi_dsi_clear_err(struct dw_mipi_dsi *dsi)
854 {
855 dsi_read(dsi, DSI_INT_ST0);
856 dsi_read(dsi, DSI_INT_ST1);
857 dsi_write(dsi, DSI_INT_MSK0, 0);
858 dsi_write(dsi, DSI_INT_MSK1, 0);
859 }
860
dw_mipi_dsi_post_disable(struct dw_mipi_dsi * dsi)861 static void dw_mipi_dsi_post_disable(struct dw_mipi_dsi *dsi)
862 {
863 const struct dw_mipi_dsi_phy_ops *phy_ops = dsi->plat_data->phy_ops;
864
865 if (phy_ops->power_off)
866 phy_ops->power_off(dsi->plat_data->priv_data);
867
868 dsi_write(dsi, DSI_PWR_UP, RESET);
869 dsi_write(dsi, DSI_PHY_RSTZ, PHY_RSTZ);
870 pm_runtime_put(dsi->dev);
871
872 if (dsi->slave)
873 dw_mipi_dsi_post_disable(dsi->slave);
874 }
875
dw_mipi_dsi_bridge_post_disable(struct drm_bridge * bridge)876 static void dw_mipi_dsi_bridge_post_disable(struct drm_bridge *bridge)
877 {
878 struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
879
880 if (dsi->panel)
881 drm_panel_unprepare(dsi->panel);
882
883 dw_mipi_dsi_post_disable(dsi);
884 }
885
dw_mipi_dsi_bridge_disable(struct drm_bridge * bridge)886 static void dw_mipi_dsi_bridge_disable(struct drm_bridge *bridge)
887 {
888 struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
889
890 if (dsi->panel)
891 drm_panel_disable(dsi->panel);
892
893 dw_mipi_dsi_disable(dsi);
894 }
895
dw_mipi_dsi_get_lanes(struct dw_mipi_dsi * dsi)896 static unsigned int dw_mipi_dsi_get_lanes(struct dw_mipi_dsi *dsi)
897 {
898 /* this instance is the slave, so add the master's lanes */
899 if (dsi->master)
900 return dsi->master->lanes + dsi->lanes;
901
902 /* this instance is the master, so add the slave's lanes */
903 if (dsi->slave)
904 return dsi->lanes + dsi->slave->lanes;
905
906 /* single-dsi, so no other instance to consider */
907 return dsi->lanes;
908 }
909
dw_mipi_dsi_bridge_mode_set(struct drm_bridge * bridge,const struct drm_display_mode * mode,const struct drm_display_mode * adjusted_mode)910 static void dw_mipi_dsi_bridge_mode_set(struct drm_bridge *bridge,
911 const struct drm_display_mode *mode,
912 const struct drm_display_mode *adjusted_mode)
913 {
914 struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
915
916 drm_mode_copy(&dsi->mode, adjusted_mode);
917
918 if (dsi->slave)
919 drm_mode_copy(&dsi->slave->mode, adjusted_mode);
920 }
921
dw_mipi_dsi_pre_enable(struct dw_mipi_dsi * dsi)922 static void dw_mipi_dsi_pre_enable(struct dw_mipi_dsi *dsi)
923 {
924 const struct dw_mipi_dsi_phy_ops *phy_ops = dsi->plat_data->phy_ops;
925 void *priv_data = dsi->plat_data->priv_data;
926 const struct drm_display_mode *adjusted_mode = &dsi->mode;
927 int ret;
928 u32 lanes = dw_mipi_dsi_get_lanes(dsi);
929
930 if (dsi->apb_rst) {
931 reset_control_assert(dsi->apb_rst);
932 usleep_range(10, 20);
933 reset_control_deassert(dsi->apb_rst);
934 }
935
936 ret = phy_ops->get_lane_mbps(priv_data, adjusted_mode, dsi->mode_flags,
937 lanes, dsi->format, &dsi->lane_mbps);
938 if (ret)
939 DRM_DEBUG_DRIVER("Phy get_lane_mbps() failed\n");
940
941 pm_runtime_get_sync(dsi->dev);
942 dw_mipi_dsi_init(dsi);
943 dw_mipi_dsi_dpi_config(dsi, adjusted_mode);
944 dw_mipi_dsi_packet_handler_config(dsi);
945 dw_mipi_dsi_video_mode_config(dsi);
946 dw_mipi_dsi_video_packet_config(dsi, adjusted_mode);
947 dw_mipi_dsi_command_mode_config(dsi);
948 dw_mipi_dsi_line_timer_config(dsi, adjusted_mode);
949 dw_mipi_dsi_vertical_timing_config(dsi, adjusted_mode);
950
951 dw_mipi_dsi_dphy_init(dsi);
952 dw_mipi_dsi_dphy_timing_config(dsi);
953 dw_mipi_dsi_dphy_interface_config(dsi);
954
955 dw_mipi_dsi_clear_err(dsi);
956
957 ret = phy_ops->init(priv_data);
958 if (ret)
959 DRM_DEBUG_DRIVER("Phy init() failed\n");
960
961 if (phy_ops->power_on)
962 phy_ops->power_on(dsi->plat_data->priv_data);
963
964 dw_mipi_dsi_dphy_enable(dsi);
965
966 dw_mipi_dsi_wait_for_two_frames(adjusted_mode);
967
968 /* Switch to cmd mode for panel-bridge pre_enable & panel prepare */
969 dw_mipi_dsi_set_mode(dsi, 0);
970
971 if (dsi->slave)
972 dw_mipi_dsi_pre_enable(dsi->slave);
973 }
974
dw_mipi_dsi_bridge_pre_enable(struct drm_bridge * bridge)975 static void dw_mipi_dsi_bridge_pre_enable(struct drm_bridge *bridge)
976 {
977 struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
978
979 dw_mipi_dsi_pre_enable(dsi);
980
981 if (dsi->panel)
982 drm_panel_prepare(dsi->panel);
983 }
984
dw_mipi_dsi_enable(struct dw_mipi_dsi * dsi)985 static void dw_mipi_dsi_enable(struct dw_mipi_dsi *dsi)
986 {
987 u32 val;
988
989 val = PHY_TXREQUESTCLKHS;
990 if (dsi->mode_flags & MIPI_DSI_CLOCK_NON_CONTINUOUS)
991 val |= AUTO_CLKLANE_CTRL;
992
993 dsi_write(dsi, DSI_LPCLK_CTRL, val);
994
995 if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO) {
996 dw_mipi_dsi_set_mode(dsi, MIPI_DSI_MODE_VIDEO);
997 } else {
998 dsi_write(dsi, DSI_EDPI_CMD_SIZE, dsi->mode.hdisplay);
999 dw_mipi_dsi_set_mode(dsi, 0);
1000 }
1001
1002 if (dsi->slave)
1003 dw_mipi_dsi_enable(dsi->slave);
1004 }
1005
dw_mipi_dsi_bridge_enable(struct drm_bridge * bridge)1006 static void dw_mipi_dsi_bridge_enable(struct drm_bridge *bridge)
1007 {
1008 struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
1009
1010 dw_mipi_dsi_enable(dsi);
1011
1012 if (dsi->panel)
1013 drm_panel_enable(dsi->panel);
1014
1015 DRM_DEV_INFO(dsi->dev, "final DSI-Link bandwidth: %u x %d Mbps\n",
1016 dsi->lane_mbps, dsi->slave ? dsi->lanes * 2 : dsi->lanes);
1017 }
1018
1019 static enum drm_mode_status
dw_mipi_dsi_bridge_mode_valid(struct drm_bridge * bridge,const struct drm_display_info * info,const struct drm_display_mode * mode)1020 dw_mipi_dsi_bridge_mode_valid(struct drm_bridge *bridge,
1021 const struct drm_display_info *info,
1022 const struct drm_display_mode *mode)
1023 {
1024 struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
1025 const struct dw_mipi_dsi_plat_data *pdata = dsi->plat_data;
1026 enum drm_mode_status mode_status = MODE_OK;
1027
1028 if (pdata->mode_valid)
1029 mode_status = pdata->mode_valid(pdata->priv_data, mode);
1030
1031 return mode_status;
1032 }
1033
dw_mipi_dsi_bridge_attach(struct drm_bridge * bridge,enum drm_bridge_attach_flags flags)1034 static int dw_mipi_dsi_bridge_attach(struct drm_bridge *bridge,
1035 enum drm_bridge_attach_flags flags)
1036 {
1037 struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
1038
1039 if (!bridge->encoder) {
1040 DRM_ERROR("Parent encoder object not found\n");
1041 return -ENODEV;
1042 }
1043
1044 /* Set the encoder type as caller does not know it */
1045 bridge->encoder->encoder_type = DRM_MODE_ENCODER_DSI;
1046
1047 /* Attach the next-bridge to the dsi bridge */
1048 if (dsi->next_bridge)
1049 return drm_bridge_attach(bridge->encoder, dsi->next_bridge,
1050 bridge, flags);
1051
1052 return 0;
1053 }
1054
1055 static const struct drm_bridge_funcs dw_mipi_dsi_bridge_funcs = {
1056 .mode_set = dw_mipi_dsi_bridge_mode_set,
1057 .pre_enable = dw_mipi_dsi_bridge_pre_enable,
1058 .enable = dw_mipi_dsi_bridge_enable,
1059 .post_disable = dw_mipi_dsi_bridge_post_disable,
1060 .disable = dw_mipi_dsi_bridge_disable,
1061 .mode_valid = dw_mipi_dsi_bridge_mode_valid,
1062 .attach = dw_mipi_dsi_bridge_attach,
1063 };
1064
1065 #ifdef CONFIG_DEBUG_FS
1066
dw_mipi_dsi_debugfs_write(void * data,u64 val)1067 static int dw_mipi_dsi_debugfs_write(void *data, u64 val)
1068 {
1069 struct debugfs_entries *vpg = data;
1070 struct dw_mipi_dsi *dsi;
1071 u32 mode_cfg;
1072
1073 if (!vpg)
1074 return -ENODEV;
1075
1076 dsi = vpg->dsi;
1077
1078 *vpg->reg = (bool)val;
1079
1080 mode_cfg = dsi_read(dsi, DSI_VID_MODE_CFG);
1081
1082 if (*vpg->reg)
1083 mode_cfg |= vpg->mask;
1084 else
1085 mode_cfg &= ~vpg->mask;
1086
1087 dsi_write(dsi, DSI_VID_MODE_CFG, mode_cfg);
1088
1089 return 0;
1090 }
1091
dw_mipi_dsi_debugfs_show(void * data,u64 * val)1092 static int dw_mipi_dsi_debugfs_show(void *data, u64 *val)
1093 {
1094 struct debugfs_entries *vpg = data;
1095
1096 if (!vpg)
1097 return -ENODEV;
1098
1099 *val = *vpg->reg;
1100
1101 return 0;
1102 }
1103
1104 DEFINE_DEBUGFS_ATTRIBUTE(fops_x32, dw_mipi_dsi_debugfs_show,
1105 dw_mipi_dsi_debugfs_write, "%llu\n");
1106
debugfs_create_files(void * data)1107 static void debugfs_create_files(void *data)
1108 {
1109 struct dw_mipi_dsi *dsi = data;
1110 struct debugfs_entries debugfs[] = {
1111 REGISTER(vpg, VID_MODE_VPG_ENABLE, dsi),
1112 REGISTER(vpg_horizontal, VID_MODE_VPG_HORIZONTAL, dsi),
1113 REGISTER(vpg_ber_pattern, VID_MODE_VPG_MODE, dsi),
1114 };
1115 int i;
1116
1117 dsi->debugfs_vpg = kmemdup(debugfs, sizeof(debugfs), GFP_KERNEL);
1118 if (!dsi->debugfs_vpg)
1119 return;
1120
1121 for (i = 0; i < ARRAY_SIZE(debugfs); i++)
1122 debugfs_create_file(dsi->debugfs_vpg[i].name, 0644,
1123 dsi->debugfs, &dsi->debugfs_vpg[i],
1124 &fops_x32);
1125 }
1126
dw_mipi_dsi_debugfs_init(struct dw_mipi_dsi * dsi)1127 static void dw_mipi_dsi_debugfs_init(struct dw_mipi_dsi *dsi)
1128 {
1129 dsi->debugfs = debugfs_create_dir(dev_name(dsi->dev), NULL);
1130 if (IS_ERR(dsi->debugfs)) {
1131 dev_err(dsi->dev, "failed to create debugfs root\n");
1132 return;
1133 }
1134
1135 debugfs_create_files(dsi);
1136 }
1137
dw_mipi_dsi_debugfs_remove(struct dw_mipi_dsi * dsi)1138 static void dw_mipi_dsi_debugfs_remove(struct dw_mipi_dsi *dsi)
1139 {
1140 debugfs_remove_recursive(dsi->debugfs);
1141 kfree(dsi->debugfs_vpg);
1142 }
1143
1144 #else
1145
dw_mipi_dsi_debugfs_init(struct dw_mipi_dsi * dsi)1146 static void dw_mipi_dsi_debugfs_init(struct dw_mipi_dsi *dsi) { }
dw_mipi_dsi_debugfs_remove(struct dw_mipi_dsi * dsi)1147 static void dw_mipi_dsi_debugfs_remove(struct dw_mipi_dsi *dsi) { }
1148
1149 #endif /* CONFIG_DEBUG_FS */
1150
1151 static struct dw_mipi_dsi *
__dw_mipi_dsi_probe(struct platform_device * pdev,const struct dw_mipi_dsi_plat_data * plat_data)1152 __dw_mipi_dsi_probe(struct platform_device *pdev,
1153 const struct dw_mipi_dsi_plat_data *plat_data)
1154 {
1155 struct device *dev = &pdev->dev;
1156 struct dw_mipi_dsi *dsi;
1157 int ret;
1158
1159 dsi = devm_kzalloc(dev, sizeof(*dsi), GFP_KERNEL);
1160 if (!dsi)
1161 return ERR_PTR(-ENOMEM);
1162
1163 dsi->dev = dev;
1164 dsi->plat_data = plat_data;
1165
1166 if (!plat_data->phy_ops->init || !plat_data->phy_ops->get_lane_mbps ||
1167 !plat_data->phy_ops->get_timing) {
1168 DRM_ERROR("Phy not properly configured\n");
1169 return ERR_PTR(-ENODEV);
1170 }
1171
1172 if (!plat_data->base) {
1173 dsi->base = devm_platform_ioremap_resource(pdev, 0);
1174 if (IS_ERR(dsi->base))
1175 return ERR_PTR(-ENODEV);
1176
1177 } else {
1178 dsi->base = plat_data->base;
1179 }
1180
1181 /*
1182 * Note that the reset was not defined in the initial device tree, so
1183 * we have to be prepared for it not being found.
1184 */
1185 dsi->apb_rst = devm_reset_control_get_optional_exclusive(dev, "apb");
1186 if (IS_ERR(dsi->apb_rst)) {
1187 ret = PTR_ERR(dsi->apb_rst);
1188
1189 if (ret != -EPROBE_DEFER)
1190 dev_err(dev, "Unable to get reset control: %d\n", ret);
1191
1192 return ERR_PTR(ret);
1193 }
1194
1195 dw_mipi_dsi_debugfs_init(dsi);
1196 pm_runtime_enable(dev);
1197
1198 dsi->dsi_host.ops = &dw_mipi_dsi_host_ops;
1199 dsi->dsi_host.dev = dev;
1200 ret = mipi_dsi_host_register(&dsi->dsi_host);
1201 if (ret) {
1202 dev_err(dev, "Failed to register MIPI host: %d\n", ret);
1203 pm_runtime_disable(dev);
1204 dw_mipi_dsi_debugfs_remove(dsi);
1205 return ERR_PTR(ret);
1206 }
1207
1208 dsi->bridge.driver_private = dsi;
1209 dsi->bridge.funcs = &dw_mipi_dsi_bridge_funcs;
1210 #ifdef CONFIG_OF
1211 dsi->bridge.of_node = pdev->dev.of_node;
1212 #endif
1213
1214 return dsi;
1215 }
1216
__dw_mipi_dsi_remove(struct dw_mipi_dsi * dsi)1217 static void __dw_mipi_dsi_remove(struct dw_mipi_dsi *dsi)
1218 {
1219 mipi_dsi_host_unregister(&dsi->dsi_host);
1220
1221 pm_runtime_disable(dsi->dev);
1222 dw_mipi_dsi_debugfs_remove(dsi);
1223 }
1224
dw_mipi_dsi_set_slave(struct dw_mipi_dsi * dsi,struct dw_mipi_dsi * slave)1225 void dw_mipi_dsi_set_slave(struct dw_mipi_dsi *dsi, struct dw_mipi_dsi *slave)
1226 {
1227 /* introduce controllers to each other */
1228 dsi->slave = slave;
1229 dsi->slave->master = dsi;
1230
1231 /* migrate settings for already attached displays */
1232 dsi->slave->lanes = dsi->lanes;
1233 dsi->slave->channel = dsi->channel;
1234 dsi->slave->format = dsi->format;
1235 dsi->slave->mode_flags = dsi->mode_flags;
1236 }
1237 EXPORT_SYMBOL_GPL(dw_mipi_dsi_set_slave);
1238
1239 /*
1240 * Probe/remove API, used from platforms based on the DRM bridge API.
1241 */
1242 struct dw_mipi_dsi *
dw_mipi_dsi_probe(struct platform_device * pdev,const struct dw_mipi_dsi_plat_data * plat_data)1243 dw_mipi_dsi_probe(struct platform_device *pdev,
1244 const struct dw_mipi_dsi_plat_data *plat_data)
1245 {
1246 return __dw_mipi_dsi_probe(pdev, plat_data);
1247 }
1248 EXPORT_SYMBOL_GPL(dw_mipi_dsi_probe);
1249
dw_mipi_dsi_remove(struct dw_mipi_dsi * dsi)1250 void dw_mipi_dsi_remove(struct dw_mipi_dsi *dsi)
1251 {
1252 __dw_mipi_dsi_remove(dsi);
1253 }
1254 EXPORT_SYMBOL_GPL(dw_mipi_dsi_remove);
1255
dw_mipi_dsi_connector_get_modes(struct drm_connector * connector)1256 static int dw_mipi_dsi_connector_get_modes(struct drm_connector *connector)
1257 {
1258 struct dw_mipi_dsi *dsi = con_to_dsi(connector);
1259
1260 if (dsi->next_bridge && (dsi->next_bridge->ops & DRM_BRIDGE_OP_MODES))
1261 return drm_bridge_get_modes(dsi->next_bridge, connector);
1262
1263 if (dsi->panel)
1264 return drm_panel_get_modes(dsi->panel, connector);
1265
1266 return -EINVAL;
1267 }
1268
1269 static struct drm_connector_helper_funcs dw_mipi_dsi_connector_helper_funcs = {
1270 .get_modes = dw_mipi_dsi_connector_get_modes,
1271 };
1272
1273 static enum drm_connector_status
dw_mipi_dsi_connector_detect(struct drm_connector * connector,bool force)1274 dw_mipi_dsi_connector_detect(struct drm_connector *connector, bool force)
1275 {
1276 struct dw_mipi_dsi *dsi = con_to_dsi(connector);
1277
1278 if (dsi->next_bridge && (dsi->next_bridge->ops & DRM_BRIDGE_OP_DETECT))
1279 return drm_bridge_detect(dsi->next_bridge);
1280
1281 return connector_status_connected;
1282 }
1283
dw_mipi_dsi_drm_connector_destroy(struct drm_connector * connector)1284 static void dw_mipi_dsi_drm_connector_destroy(struct drm_connector *connector)
1285 {
1286 drm_connector_unregister(connector);
1287 drm_connector_cleanup(connector);
1288 }
1289
1290 static const struct drm_connector_funcs dw_mipi_dsi_atomic_connector_funcs = {
1291 .fill_modes = drm_helper_probe_single_connector_modes,
1292 .detect = dw_mipi_dsi_connector_detect,
1293 .destroy = dw_mipi_dsi_drm_connector_destroy,
1294 .reset = drm_atomic_helper_connector_reset,
1295 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1296 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1297 };
1298
dw_mipi_dsi_connector_init(struct dw_mipi_dsi * dsi)1299 static int dw_mipi_dsi_connector_init(struct dw_mipi_dsi *dsi)
1300 {
1301 struct drm_encoder *encoder = dsi->encoder;
1302 struct drm_connector *connector = &dsi->connector;
1303 struct drm_device *drm_dev = dsi->bridge.dev;
1304 struct device *dev = dsi->dev;
1305 int ret;
1306
1307 ret = drm_connector_init(drm_dev, connector,
1308 &dw_mipi_dsi_atomic_connector_funcs,
1309 DRM_MODE_CONNECTOR_DSI);
1310 if (ret) {
1311 DRM_DEV_ERROR(dev, "Failed to initialize connector\n");
1312 return ret;
1313 }
1314
1315 drm_connector_helper_add(connector,
1316 &dw_mipi_dsi_connector_helper_funcs);
1317 ret = drm_connector_attach_encoder(connector, encoder);
1318 if (ret < 0) {
1319 DRM_DEV_ERROR(dev, "Failed to attach encoder: %d\n", ret);
1320 goto connector_cleanup;
1321 }
1322
1323 return 0;
1324
1325 connector_cleanup:
1326 connector->funcs->destroy(connector);
1327
1328 return ret;
1329 }
1330
1331 /*
1332 * Bind/unbind API, used from platforms based on the component framework.
1333 */
dw_mipi_dsi_bind(struct dw_mipi_dsi * dsi,struct drm_encoder * encoder)1334 int dw_mipi_dsi_bind(struct dw_mipi_dsi *dsi, struct drm_encoder *encoder)
1335 {
1336 int ret;
1337
1338 dsi->encoder = encoder;
1339
1340 ret = drm_bridge_attach(encoder, &dsi->bridge, NULL, 0);
1341 if (ret) {
1342 DRM_ERROR("Failed to initialize bridge with drm\n");
1343 return ret;
1344 }
1345
1346 return ret;
1347 }
1348 EXPORT_SYMBOL_GPL(dw_mipi_dsi_bind);
1349
dw_mipi_dsi_unbind(struct dw_mipi_dsi * dsi)1350 void dw_mipi_dsi_unbind(struct dw_mipi_dsi *dsi)
1351 {
1352 }
1353 EXPORT_SYMBOL_GPL(dw_mipi_dsi_unbind);
1354
dw_mipi_dsi_get_connector(struct dw_mipi_dsi * dsi)1355 struct drm_connector *dw_mipi_dsi_get_connector(struct dw_mipi_dsi *dsi)
1356 {
1357 struct drm_connector *connector = NULL;
1358 enum drm_bridge_attach_flags flags = 0;
1359 int ret;
1360
1361 if (dsi->next_bridge) {
1362 enum drm_bridge_attach_flags flags;
1363 struct list_head *connector_list =
1364 &dsi->next_bridge->dev->mode_config.connector_list;
1365
1366 flags = dsi->next_bridge->ops & DRM_BRIDGE_OP_MODES ?
1367 DRM_BRIDGE_ATTACH_NO_CONNECTOR : 0;
1368 if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR))
1369 list_for_each_entry(connector, connector_list, head)
1370 if (drm_connector_has_possible_encoder(connector,
1371 dsi->encoder))
1372 break;
1373 }
1374
1375 if (dsi->panel || (dsi->next_bridge && (flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR))) {
1376 ret = dw_mipi_dsi_connector_init(dsi);
1377 if (ret)
1378 return ERR_PTR(ret);
1379
1380 connector = &dsi->connector;
1381 }
1382
1383 return connector;
1384 }
1385 EXPORT_SYMBOL_GPL(dw_mipi_dsi_get_connector);
1386
1387 MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
1388 MODULE_AUTHOR("Philippe Cornu <philippe.cornu@st.com>");
1389 MODULE_DESCRIPTION("DW MIPI DSI host controller driver");
1390 MODULE_LICENSE("GPL");
1391 MODULE_ALIAS("platform:dw-mipi-dsi");
1392