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