forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-09 b22da3d8526a935aa31e086e63f60ff3246cb61c
kernel/drivers/gpu/drm/bridge/synopsys/dw-mipi-dsi.c
....@@ -10,20 +10,25 @@
1010
1111 #include <linux/clk.h>
1212 #include <linux/component.h>
13
+#include <linux/debugfs.h>
1314 #include <linux/iopoll.h>
1415 #include <linux/module.h>
1516 #include <linux/of_device.h>
1617 #include <linux/pm_runtime.h>
1718 #include <linux/reset.h>
18
-#include <drm/drmP.h>
19
+
20
+#include <video/mipi_display.h>
21
+
22
+#include <drm/bridge/dw_mipi_dsi.h>
1923 #include <drm/drm_atomic_helper.h>
2024 #include <drm/drm_bridge.h>
2125 #include <drm/drm_crtc.h>
22
-#include <drm/drm_crtc_helper.h>
2326 #include <drm/drm_mipi_dsi.h>
27
+#include <drm/drm_modes.h>
2428 #include <drm/drm_of.h>
25
-#include <drm/bridge/dw_mipi_dsi.h>
26
-#include <video/mipi_display.h>
29
+#include <drm/drm_panel.h>
30
+#include <drm/drm_probe_helper.h>
31
+#include <drm/drm_print.h>
2732
2833 #define HWVER_131 0x31333100 /* IP version 1.31 */
2934
....@@ -80,12 +85,20 @@
8085 #define ENABLE_CMD_MODE BIT(0)
8186
8287 #define DSI_VID_MODE_CFG 0x38
83
-#define ENABLE_LOW_POWER (0x3f << 8)
84
-#define ENABLE_LOW_POWER_MASK (0x3f << 8)
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)
8594 #define VID_MODE_TYPE_NON_BURST_SYNC_PULSES 0x0
8695 #define VID_MODE_TYPE_NON_BURST_SYNC_EVENTS 0x1
8796 #define VID_MODE_TYPE_BURST 0x2
8897 #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)
89102
90103 #define DSI_VID_PKT_SIZE 0x3c
91104 #define VID_PKT_SIZE(p) ((p) & 0x3fff)
....@@ -215,29 +228,69 @@
215228 #define PHY_STATUS_TIMEOUT_US 10000
216229 #define CMD_PKT_STATUS_TIMEOUT_US 20000
217230
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
+
218246 struct dw_mipi_dsi {
219247 struct drm_bridge bridge;
248
+ struct drm_connector connector;
249
+ struct drm_encoder *encoder;
220250 struct mipi_dsi_host dsi_host;
221
- struct drm_bridge *panel_bridge;
251
+ struct drm_panel *panel;
252
+ struct drm_bridge *next_bridge;
222253 struct device *dev;
223254 void __iomem *base;
224255
225
- struct clk *pclk;
256
+ struct reset_control *apb_rst;
226257
227258 unsigned int lane_mbps; /* per lane */
228259 u32 channel;
229260 u32 lanes;
230261 u32 format;
262
+ struct drm_display_mode mode;
231263 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 */
232277
233278 const struct dw_mipi_dsi_plat_data *plat_data;
234279 };
235280
236281 /*
282
+ * Check if either a link to a master or slave is present
283
+ */
284
+static inline bool dw_mipi_is_dual_mode(struct dw_mipi_dsi *dsi)
285
+{
286
+ return dsi->slave || dsi->master;
287
+}
288
+
289
+/*
237290 * The controller should generate 2 frames before
238291 * preparing the peripheral.
239292 */
240
-static void dw_mipi_dsi_wait_for_two_frames(struct drm_display_mode *mode)
293
+static void dw_mipi_dsi_wait_for_two_frames(const struct drm_display_mode *mode)
241294 {
242295 int refresh, two_frames;
243296
....@@ -256,6 +309,11 @@
256309 return container_of(bridge, struct dw_mipi_dsi, bridge);
257310 }
258311
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
+
259317 static inline void dsi_write(struct dw_mipi_dsi *dsi, u32 reg, u32 val)
260318 {
261319 writel(val, dsi->base + reg);
....@@ -270,35 +328,29 @@
270328 struct mipi_dsi_device *device)
271329 {
272330 struct dw_mipi_dsi *dsi = host_to_dsi(host);
273
- struct drm_bridge *bridge;
274
- struct drm_panel *panel;
331
+ const struct dw_mipi_dsi_plat_data *pdata = dsi->plat_data;
332
+ int max_data_lanes = dsi->plat_data->max_data_lanes;
275333 int ret;
276334
277
- if (device->lanes > dsi->plat_data->max_data_lanes) {
278
- dev_err(dsi->dev, "the number of data lanes(%u) is too many\n",
279
- device->lanes);
280
- return -EINVAL;
281
- }
282
-
283
- dsi->lanes = device->lanes;
335
+ dsi->lanes = (device->lanes > max_data_lanes) ? device->lanes / 2 : device->lanes;
284336 dsi->channel = device->channel;
285337 dsi->format = device->format;
286338 dsi->mode_flags = device->mode_flags;
287339
288
- ret = drm_of_find_panel_or_bridge(host->dev->of_node, 1, 0,
289
- &panel, &bridge);
290
- if (ret)
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);
291344 return ret;
292
-
293
- if (panel) {
294
- bridge = drm_panel_bridge_add(panel, DRM_MODE_CONNECTOR_DSI);
295
- if (IS_ERR(bridge))
296
- return PTR_ERR(bridge);
297345 }
298346
299
- dsi->panel_bridge = bridge;
300
-
301347 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
+ }
302354
303355 return 0;
304356 }
....@@ -307,6 +359,14 @@
307359 struct mipi_dsi_device *device)
308360 {
309361 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
+ }
310370
311371 drm_of_panel_bridge_remove(host->dev->of_node, 1, 0);
312372
....@@ -320,6 +380,16 @@
320380 {
321381 bool lpm = msg->flags & MIPI_DSI_MSG_USE_LPM;
322382 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));
323393
324394 if (msg->flags & MIPI_DSI_MSG_REQ_ACK)
325395 val |= ACK_RQST_EN;
....@@ -327,6 +397,19 @@
327397 val |= CMD_MODE_ALL_LP;
328398
329399 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);
330413 }
331414
332415 static int dw_mipi_dsi_gen_pkt_hdr_write(struct dw_mipi_dsi *dsi, u32 hdr_val)
....@@ -440,10 +523,17 @@
440523 }
441524
442525 dw_mipi_message_config(dsi, msg);
526
+ if (dsi->slave)
527
+ dw_mipi_message_config(dsi->slave, msg);
443528
444529 ret = dw_mipi_dsi_write(dsi, &packet);
445530 if (ret)
446531 return ret;
532
+ if (dsi->slave) {
533
+ ret = dw_mipi_dsi_write(dsi->slave, &packet);
534
+ if (ret)
535
+ return ret;
536
+ }
447537
448538 if (msg->rx_buf && msg->rx_len) {
449539 ret = dw_mipi_dsi_read(dsi, msg);
....@@ -465,14 +555,14 @@
465555
466556 static void dw_mipi_dsi_video_mode_config(struct dw_mipi_dsi *dsi)
467557 {
468
- u32 val;
558
+ u32 val = LP_VSA_EN | LP_VBP_EN | LP_VFP_EN |
559
+ LP_VACT_EN | LP_HBP_EN | LP_HFP_EN;
469560
470
- /*
471
- * TODO dw drv improvements
472
- * enabling low power is panel-dependent, we should use the
473
- * panel configuration here...
474
- */
475
- val = ENABLE_LOW_POWER;
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;
476566
477567 if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_BURST)
478568 val |= VID_MODE_TYPE_BURST;
....@@ -481,14 +571,21 @@
481571 else
482572 val |= VID_MODE_TYPE_NON_BURST_SYNC_EVENTS;
483573
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
+
484583 dsi_write(dsi, DSI_VID_MODE_CFG, val);
485584 }
486585
487586 static void dw_mipi_dsi_set_mode(struct dw_mipi_dsi *dsi,
488587 unsigned long mode_flags)
489588 {
490
- u32 val;
491
-
492589 dsi_write(dsi, DSI_PWR_UP, RESET);
493590
494591 if (mode_flags & MIPI_DSI_MODE_VIDEO) {
....@@ -498,31 +595,44 @@
498595 dsi_write(dsi, DSI_MODE_CFG, ENABLE_CMD_MODE);
499596 }
500597
501
- val = PHY_TXREQUESTCLKHS;
502
- if (dsi->mode_flags & MIPI_DSI_CLOCK_NON_CONTINUOUS)
503
- val |= AUTO_CLKLANE_CTRL;
504
- dsi_write(dsi, DSI_LPCLK_CTRL, val);
505
-
506598 dsi_write(dsi, DSI_PWR_UP, POWERUP);
507599 }
508600
509601 static void dw_mipi_dsi_disable(struct dw_mipi_dsi *dsi)
510602 {
511
- dsi_write(dsi, DSI_PWR_UP, RESET);
512
- dsi_write(dsi, DSI_PHY_RSTZ, PHY_RSTZ);
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);
513608 }
514609
515610 static void dw_mipi_dsi_init(struct dw_mipi_dsi *dsi)
516611 {
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
+
517617 /*
518618 * The maximum permitted escape clock is 20MHz and it is derived from
519
- * lanebyteclk, which is running at "lane_mbps / 8". Thus we want:
520
- *
521
- * (lane_mbps >> 3) / esc_clk_division < 20
522
- * which is:
523
- * (lane_mbps >> 3) / 20 > esc_clk_division
619
+ * lanebyteclk, which is running at "lane_mbps / 8".
524620 */
525
- u32 esc_clk_division = (dsi->lane_mbps >> 3) / 20 + 1;
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;
526636
527637 dsi_write(dsi, DSI_PWR_UP, RESET);
528638
....@@ -536,7 +646,7 @@
536646 }
537647
538648 static void dw_mipi_dsi_dpi_config(struct dw_mipi_dsi *dsi,
539
- struct drm_display_mode *mode)
649
+ const struct drm_display_mode *mode)
540650 {
541651 u32 val = 0, color = 0;
542652
....@@ -563,14 +673,6 @@
563673 dsi_write(dsi, DSI_DPI_VCID, DPI_VCID(dsi->channel));
564674 dsi_write(dsi, DSI_DPI_COLOR_CODING, color);
565675 dsi_write(dsi, DSI_DPI_CFG_POL, val);
566
- /*
567
- * TODO dw drv improvements
568
- * largest packet sizes during hfp or during vsa/vpb/vfp
569
- * should be computed according to byte lane, lane number and only
570
- * if sending lp cmds in high speed is enable (PHY_TXREQUESTCLKHS)
571
- */
572
- dsi_write(dsi, DSI_DPI_LP_CMD_TIM, OUTVACT_LPCMD_TIME(4)
573
- | INVACT_LPCMD_TIME(4));
574676 }
575677
576678 static void dw_mipi_dsi_packet_handler_config(struct dw_mipi_dsi *dsi)
....@@ -579,7 +681,7 @@
579681 }
580682
581683 static void dw_mipi_dsi_video_packet_config(struct dw_mipi_dsi *dsi,
582
- struct drm_display_mode *mode)
684
+ const struct drm_display_mode *mode)
583685 {
584686 /*
585687 * TODO dw drv improvements
....@@ -588,7 +690,11 @@
588690 * DSI_VNPCR.NPSIZE... especially because this driver supports
589691 * non-burst video modes, see dw_mipi_dsi_video_mode_config()...
590692 */
591
- dsi_write(dsi, DSI_VID_PKT_SIZE, VID_PKT_SIZE(mode->hdisplay));
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));
592698 }
593699
594700 static void dw_mipi_dsi_command_mode_config(struct dw_mipi_dsi *dsi)
....@@ -610,23 +716,23 @@
610716
611717 /* Get lane byte clock cycles. */
612718 static u32 dw_mipi_dsi_get_hcomponent_lbcc(struct dw_mipi_dsi *dsi,
613
- struct drm_display_mode *mode,
719
+ const struct drm_display_mode *mode,
614720 u32 hcomponent)
615721 {
616
- u32 frac, lbcc;
722
+ u32 lbcc;
617723
618724 lbcc = hcomponent * dsi->lane_mbps * MSEC_PER_SEC / 8;
619725
620
- frac = lbcc % mode->clock;
621
- lbcc = lbcc / mode->clock;
622
- if (frac)
623
- lbcc++;
726
+ if (mode->clock == 0) {
727
+ DRM_ERROR("dsi mode clock is 0!\n");
728
+ return 0;
729
+ }
624730
625
- return lbcc;
731
+ return DIV_ROUND_CLOSEST_ULL(lbcc, mode->clock);
626732 }
627733
628734 static void dw_mipi_dsi_line_timer_config(struct dw_mipi_dsi *dsi,
629
- struct drm_display_mode *mode)
735
+ const struct drm_display_mode *mode)
630736 {
631737 u32 htotal, hsa, hbp, lbcc;
632738
....@@ -649,7 +755,7 @@
649755 }
650756
651757 static void dw_mipi_dsi_vertical_timing_config(struct dw_mipi_dsi *dsi,
652
- struct drm_display_mode *mode)
758
+ const struct drm_display_mode *mode)
653759 {
654760 u32 vactive, vsa, vfp, vbp;
655761
....@@ -666,7 +772,15 @@
666772
667773 static void dw_mipi_dsi_dphy_timing_config(struct dw_mipi_dsi *dsi)
668774 {
775
+ const struct dw_mipi_dsi_phy_ops *phy_ops = dsi->plat_data->phy_ops;
776
+ struct dw_mipi_dsi_dphy_timing timing;
669777 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");
670784
671785 /*
672786 * TODO dw drv improvements
....@@ -679,16 +793,20 @@
679793 hw_version = dsi_read(dsi, DSI_VERSION) & VERSION;
680794
681795 if (hw_version >= HWVER_131) {
682
- dsi_write(dsi, DSI_PHY_TMR_CFG, PHY_HS2LP_TIME_V131(0x40) |
683
- PHY_LP2HS_TIME_V131(0x40));
796
+ dsi_write(dsi, DSI_PHY_TMR_CFG,
797
+ PHY_HS2LP_TIME_V131(timing.data_hs2lp) |
798
+ PHY_LP2HS_TIME_V131(timing.data_lp2hs));
684799 dsi_write(dsi, DSI_PHY_TMR_RD_CFG, MAX_RD_TIME_V131(10000));
685800 } else {
686
- dsi_write(dsi, DSI_PHY_TMR_CFG, PHY_HS2LP_TIME(0x40) |
687
- PHY_LP2HS_TIME(0x40) | MAX_RD_TIME(10000));
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));
688805 }
689806
690
- dsi_write(dsi, DSI_PHY_TMR_LPCLK_CFG, PHY_CLKHS2LP_TIME(0x40)
691
- | PHY_CLKLP2HS_TIME(0x40));
807
+ dsi_write(dsi, DSI_PHY_TMR_LPCLK_CFG,
808
+ PHY_CLKHS2LP_TIME(timing.clk_hs2lp) |
809
+ PHY_CLKLP2HS_TIME(timing.clk_lp2hs));
692810 }
693811
694812 static void dw_mipi_dsi_dphy_interface_config(struct dw_mipi_dsi *dsi)
....@@ -723,13 +841,13 @@
723841 ret = readl_poll_timeout(dsi->base + DSI_PHY_STATUS, val,
724842 val & PHY_LOCK, 1000, PHY_STATUS_TIMEOUT_US);
725843 if (ret)
726
- DRM_DEBUG_DRIVER("failed to wait phy lock state\n");
844
+ DRM_ERROR("failed to wait phy lock state\n");
727845
728846 ret = readl_poll_timeout(dsi->base + DSI_PHY_STATUS,
729847 val, val & PHY_STOP_STATE_CLK_LANE, 1000,
730848 PHY_STATUS_TIMEOUT_US);
731849 if (ret)
732
- DRM_DEBUG_DRIVER("failed to wait phy clk lane stop state\n");
850
+ DRM_ERROR("failed to wait phy clk lane stop state\n");
733851 }
734852
735853 static void dw_mipi_dsi_clear_err(struct dw_mipi_dsi *dsi)
....@@ -740,44 +858,83 @@
740858 dsi_write(dsi, DSI_INT_MSK1, 0);
741859 }
742860
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
+
743876 static void dw_mipi_dsi_bridge_post_disable(struct drm_bridge *bridge)
744877 {
745878 struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
746879
747
- /*
748
- * Switch to command mode before panel-bridge post_disable &
749
- * panel unprepare.
750
- * Note: panel-bridge disable & panel disable has been called
751
- * before by the drm framework.
752
- */
753
- dw_mipi_dsi_set_mode(dsi, 0);
880
+ if (dsi->panel)
881
+ drm_panel_unprepare(dsi->panel);
754882
755
- /*
756
- * TODO Only way found to call panel-bridge post_disable &
757
- * panel unprepare before the dsi "final" disable...
758
- * This needs to be fixed in the drm_bridge framework and the API
759
- * needs to be updated to manage our own call chains...
760
- */
761
- dsi->panel_bridge->funcs->post_disable(dsi->panel_bridge);
883
+ dw_mipi_dsi_post_disable(dsi);
884
+}
885
+
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);
762892
763893 dw_mipi_dsi_disable(dsi);
764
- clk_disable_unprepare(dsi->pclk);
765
- pm_runtime_put(dsi->dev);
894
+}
895
+
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;
766908 }
767909
768910 static void dw_mipi_dsi_bridge_mode_set(struct drm_bridge *bridge,
769
- struct drm_display_mode *mode,
770
- struct drm_display_mode *adjusted_mode)
911
+ const struct drm_display_mode *mode,
912
+ const struct drm_display_mode *adjusted_mode)
771913 {
772914 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
+
922
+static void dw_mipi_dsi_pre_enable(struct dw_mipi_dsi *dsi)
923
+{
773924 const struct dw_mipi_dsi_phy_ops *phy_ops = dsi->plat_data->phy_ops;
774925 void *priv_data = dsi->plat_data->priv_data;
926
+ const struct drm_display_mode *adjusted_mode = &dsi->mode;
775927 int ret;
928
+ u32 lanes = dw_mipi_dsi_get_lanes(dsi);
776929
777
- clk_prepare_enable(dsi->pclk);
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
+ }
778935
779936 ret = phy_ops->get_lane_mbps(priv_data, adjusted_mode, dsi->mode_flags,
780
- dsi->lanes, dsi->format, &dsi->lane_mbps);
937
+ lanes, dsi->format, &dsi->lane_mbps);
781938 if (ret)
782939 DRM_DEBUG_DRIVER("Phy get_lane_mbps() failed\n");
783940
....@@ -801,24 +958,67 @@
801958 if (ret)
802959 DRM_DEBUG_DRIVER("Phy init() failed\n");
803960
961
+ if (phy_ops->power_on)
962
+ phy_ops->power_on(dsi->plat_data->priv_data);
963
+
804964 dw_mipi_dsi_dphy_enable(dsi);
805965
806966 dw_mipi_dsi_wait_for_two_frames(adjusted_mode);
807967
808968 /* Switch to cmd mode for panel-bridge pre_enable & panel prepare */
809969 dw_mipi_dsi_set_mode(dsi, 0);
970
+
971
+ if (dsi->slave)
972
+ dw_mipi_dsi_pre_enable(dsi->slave);
973
+}
974
+
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
+
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);
8101004 }
8111005
8121006 static void dw_mipi_dsi_bridge_enable(struct drm_bridge *bridge)
8131007 {
8141008 struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
8151009
816
- /* Switch to video mode for panel-bridge enable & panel enable */
817
- dw_mipi_dsi_set_mode(dsi, MIPI_DSI_MODE_VIDEO);
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);
8181017 }
8191018
8201019 static enum drm_mode_status
8211020 dw_mipi_dsi_bridge_mode_valid(struct drm_bridge *bridge,
1021
+ const struct drm_display_info *info,
8221022 const struct drm_display_mode *mode)
8231023 {
8241024 struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
....@@ -831,7 +1031,8 @@
8311031 return mode_status;
8321032 }
8331033
834
-static int dw_mipi_dsi_bridge_attach(struct drm_bridge *bridge)
1034
+static int dw_mipi_dsi_bridge_attach(struct drm_bridge *bridge,
1035
+ enum drm_bridge_attach_flags flags)
8351036 {
8361037 struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
8371038
....@@ -843,26 +1044,116 @@
8431044 /* Set the encoder type as caller does not know it */
8441045 bridge->encoder->encoder_type = DRM_MODE_ENCODER_DSI;
8451046
846
- /* Attach the panel-bridge to the dsi bridge */
847
- return drm_bridge_attach(bridge->encoder, dsi->panel_bridge, bridge);
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;
8481053 }
8491054
8501055 static const struct drm_bridge_funcs dw_mipi_dsi_bridge_funcs = {
8511056 .mode_set = dw_mipi_dsi_bridge_mode_set,
1057
+ .pre_enable = dw_mipi_dsi_bridge_pre_enable,
8521058 .enable = dw_mipi_dsi_bridge_enable,
8531059 .post_disable = dw_mipi_dsi_bridge_post_disable,
1060
+ .disable = dw_mipi_dsi_bridge_disable,
8541061 .mode_valid = dw_mipi_dsi_bridge_mode_valid,
8551062 .attach = dw_mipi_dsi_bridge_attach,
8561063 };
1064
+
1065
+#ifdef CONFIG_DEBUG_FS
1066
+
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
+
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
+
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
+
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
+
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
+
1146
+static void dw_mipi_dsi_debugfs_init(struct dw_mipi_dsi *dsi) { }
1147
+static void dw_mipi_dsi_debugfs_remove(struct dw_mipi_dsi *dsi) { }
1148
+
1149
+#endif /* CONFIG_DEBUG_FS */
8571150
8581151 static struct dw_mipi_dsi *
8591152 __dw_mipi_dsi_probe(struct platform_device *pdev,
8601153 const struct dw_mipi_dsi_plat_data *plat_data)
8611154 {
8621155 struct device *dev = &pdev->dev;
863
- struct reset_control *apb_rst;
8641156 struct dw_mipi_dsi *dsi;
865
- struct resource *res;
8661157 int ret;
8671158
8681159 dsi = devm_kzalloc(dev, sizeof(*dsi), GFP_KERNEL);
....@@ -872,17 +1163,14 @@
8721163 dsi->dev = dev;
8731164 dsi->plat_data = plat_data;
8741165
875
- if (!plat_data->phy_ops->init || !plat_data->phy_ops->get_lane_mbps) {
1166
+ if (!plat_data->phy_ops->init || !plat_data->phy_ops->get_lane_mbps ||
1167
+ !plat_data->phy_ops->get_timing) {
8761168 DRM_ERROR("Phy not properly configured\n");
8771169 return ERR_PTR(-ENODEV);
8781170 }
8791171
8801172 if (!plat_data->base) {
881
- res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
882
- if (!res)
883
- return ERR_PTR(-ENODEV);
884
-
885
- dsi->base = devm_ioremap_resource(dev, res);
1173
+ dsi->base = devm_platform_ioremap_resource(pdev, 0);
8861174 if (IS_ERR(dsi->base))
8871175 return ERR_PTR(-ENODEV);
8881176
....@@ -890,20 +1178,13 @@
8901178 dsi->base = plat_data->base;
8911179 }
8921180
893
- dsi->pclk = devm_clk_get(dev, "pclk");
894
- if (IS_ERR(dsi->pclk)) {
895
- ret = PTR_ERR(dsi->pclk);
896
- dev_err(dev, "Unable to get pclk: %d\n", ret);
897
- return ERR_PTR(ret);
898
- }
899
-
9001181 /*
9011182 * Note that the reset was not defined in the initial device tree, so
9021183 * we have to be prepared for it not being found.
9031184 */
904
- apb_rst = devm_reset_control_get_optional_exclusive(dev, "apb");
905
- if (IS_ERR(apb_rst)) {
906
- ret = PTR_ERR(apb_rst);
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);
9071188
9081189 if (ret != -EPROBE_DEFER)
9091190 dev_err(dev, "Unable to get reset control: %d\n", ret);
....@@ -911,20 +1192,7 @@
9111192 return ERR_PTR(ret);
9121193 }
9131194
914
- if (apb_rst) {
915
- ret = clk_prepare_enable(dsi->pclk);
916
- if (ret) {
917
- dev_err(dev, "%s: Failed to enable pclk\n", __func__);
918
- return ERR_PTR(ret);
919
- }
920
-
921
- reset_control_assert(apb_rst);
922
- usleep_range(10, 20);
923
- reset_control_deassert(apb_rst);
924
-
925
- clk_disable_unprepare(dsi->pclk);
926
- }
927
-
1195
+ dw_mipi_dsi_debugfs_init(dsi);
9281196 pm_runtime_enable(dev);
9291197
9301198 dsi->dsi_host.ops = &dw_mipi_dsi_host_ops;
....@@ -932,6 +1200,8 @@
9321200 ret = mipi_dsi_host_register(&dsi->dsi_host);
9331201 if (ret) {
9341202 dev_err(dev, "Failed to register MIPI host: %d\n", ret);
1203
+ pm_runtime_disable(dev);
1204
+ dw_mipi_dsi_debugfs_remove(dsi);
9351205 return ERR_PTR(ret);
9361206 }
9371207
....@@ -946,8 +1216,25 @@
9461216
9471217 static void __dw_mipi_dsi_remove(struct dw_mipi_dsi *dsi)
9481218 {
1219
+ mipi_dsi_host_unregister(&dsi->dsi_host);
1220
+
9491221 pm_runtime_disable(dsi->dev);
1222
+ dw_mipi_dsi_debugfs_remove(dsi);
9501223 }
1224
+
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);
9511238
9521239 /*
9531240 * Probe/remove API, used from platforms based on the DRM bridge API.
....@@ -962,43 +1249,141 @@
9621249
9631250 void dw_mipi_dsi_remove(struct dw_mipi_dsi *dsi)
9641251 {
965
- mipi_dsi_host_unregister(&dsi->dsi_host);
966
-
9671252 __dw_mipi_dsi_remove(dsi);
9681253 }
9691254 EXPORT_SYMBOL_GPL(dw_mipi_dsi_remove);
9701255
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
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
+
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
+
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
+
9711331 /*
9721332 * Bind/unbind API, used from platforms based on the component framework.
9731333 */
974
-struct dw_mipi_dsi *
975
-dw_mipi_dsi_bind(struct platform_device *pdev, struct drm_encoder *encoder,
976
- const struct dw_mipi_dsi_plat_data *plat_data)
1334
+int dw_mipi_dsi_bind(struct dw_mipi_dsi *dsi, struct drm_encoder *encoder)
9771335 {
978
- struct dw_mipi_dsi *dsi;
9791336 int ret;
9801337
981
- dsi = __dw_mipi_dsi_probe(pdev, plat_data);
982
- if (IS_ERR(dsi))
983
- return dsi;
1338
+ dsi->encoder = encoder;
9841339
985
- ret = drm_bridge_attach(encoder, &dsi->bridge, NULL);
1340
+ ret = drm_bridge_attach(encoder, &dsi->bridge, NULL, 0);
9861341 if (ret) {
987
- dw_mipi_dsi_remove(dsi);
9881342 DRM_ERROR("Failed to initialize bridge with drm\n");
989
- return ERR_PTR(ret);
1343
+ return ret;
9901344 }
9911345
992
- return dsi;
1346
+ return ret;
9931347 }
9941348 EXPORT_SYMBOL_GPL(dw_mipi_dsi_bind);
9951349
9961350 void dw_mipi_dsi_unbind(struct dw_mipi_dsi *dsi)
9971351 {
998
- __dw_mipi_dsi_remove(dsi);
9991352 }
10001353 EXPORT_SYMBOL_GPL(dw_mipi_dsi_unbind);
10011354
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
+
10021387 MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
10031388 MODULE_AUTHOR("Philippe Cornu <philippe.cornu@st.com>");
10041389 MODULE_DESCRIPTION("DW MIPI DSI host controller driver");