2 * Copyright (c) 2015, The Linux Foundation. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
17 struct msm_dsi_manager {
18 struct msm_dsi *dsi[DSI_MAX];
25 static struct msm_dsi_manager msm_dsim_glb;
27 #define IS_DUAL_PANEL() (msm_dsim_glb.is_dual_panel)
28 #define IS_SYNC_NEEDED() (msm_dsim_glb.is_sync_needed)
29 #define IS_MASTER_PANEL(id) (msm_dsim_glb.master_panel_id == id)
31 static inline struct msm_dsi *dsi_mgr_get_dsi(int id)
33 return msm_dsim_glb.dsi[id];
36 static inline struct msm_dsi *dsi_mgr_get_other_dsi(int id)
38 return msm_dsim_glb.dsi[(id + 1) % DSI_MAX];
41 static int dsi_mgr_parse_dual_panel(struct device_node *np, int id)
43 struct msm_dsi_manager *msm_dsim = &msm_dsim_glb;
45 /* We assume 2 dsi nodes have the same information of dual-panel and
46 * sync-mode, and only one node specifies master in case of dual mode.
48 if (!msm_dsim->is_dual_panel)
49 msm_dsim->is_dual_panel = of_property_read_bool(
50 np, "qcom,dual-panel-mode");
52 if (msm_dsim->is_dual_panel) {
53 if (of_property_read_bool(np, "qcom,master-panel"))
54 msm_dsim->master_panel_id = id;
55 if (!msm_dsim->is_sync_needed)
56 msm_dsim->is_sync_needed = of_property_read_bool(
57 np, "qcom,sync-dual-panel");
63 static int dsi_mgr_host_register(int id)
65 struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
66 struct msm_dsi *other_dsi = dsi_mgr_get_other_dsi(id);
67 struct msm_dsi *clk_master_dsi = dsi_mgr_get_dsi(DSI_CLOCK_MASTER);
68 struct msm_dsi_pll *src_pll;
71 if (!IS_DUAL_PANEL()) {
72 ret = msm_dsi_host_register(msm_dsi->host, true);
76 src_pll = msm_dsi_phy_get_pll(msm_dsi->phy);
77 ret = msm_dsi_host_set_src_pll(msm_dsi->host, src_pll);
78 } else if (!other_dsi) {
81 struct msm_dsi *mdsi = IS_MASTER_PANEL(id) ?
83 struct msm_dsi *sdsi = IS_MASTER_PANEL(id) ?
85 /* Register slave host first, so that slave DSI device
86 * has a chance to probe, and do not block the master
88 * Also, do not check defer for the slave host,
89 * because only master DSI device adds the panel to global
90 * panel list. The panel's device is the master DSI device.
92 ret = msm_dsi_host_register(sdsi->host, false);
95 ret = msm_dsi_host_register(mdsi->host, true);
99 /* PLL0 is to drive both 2 DSI link clocks in Dual DSI mode. */
100 src_pll = msm_dsi_phy_get_pll(clk_master_dsi->phy);
101 ret = msm_dsi_host_set_src_pll(msm_dsi->host, src_pll);
104 ret = msm_dsi_host_set_src_pll(other_dsi->host, src_pll);
110 struct dsi_connector {
111 struct drm_connector base;
116 struct drm_bridge base;
120 #define to_dsi_connector(x) container_of(x, struct dsi_connector, base)
121 #define to_dsi_bridge(x) container_of(x, struct dsi_bridge, base)
123 static inline int dsi_mgr_connector_get_id(struct drm_connector *connector)
125 struct dsi_connector *dsi_connector = to_dsi_connector(connector);
126 return dsi_connector->id;
129 static int dsi_mgr_bridge_get_id(struct drm_bridge *bridge)
131 struct dsi_bridge *dsi_bridge = to_dsi_bridge(bridge);
132 return dsi_bridge->id;
135 static enum drm_connector_status dsi_mgr_connector_detect(
136 struct drm_connector *connector, bool force)
138 int id = dsi_mgr_connector_get_id(connector);
139 struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
140 struct msm_dsi *other_dsi = dsi_mgr_get_other_dsi(id);
141 struct msm_drm_private *priv = connector->dev->dev_private;
142 struct msm_kms *kms = priv->kms;
145 if (!msm_dsi->panel) {
146 msm_dsi->panel = msm_dsi_host_get_panel(msm_dsi->host,
147 &msm_dsi->panel_flags);
149 /* There is only 1 panel in the global panel list
150 * for dual panel mode. Therefore slave dsi should get
151 * the drm_panel instance from master dsi, and
152 * keep using the panel flags got from the current DSI link.
154 if (!msm_dsi->panel && IS_DUAL_PANEL() &&
155 !IS_MASTER_PANEL(id) && other_dsi)
156 msm_dsi->panel = msm_dsi_host_get_panel(
157 other_dsi->host, NULL);
159 if (msm_dsi->panel && IS_DUAL_PANEL())
160 drm_object_attach_property(&connector->base,
161 connector->dev->mode_config.tile_property, 0);
163 /* Set split display info to kms once dual panel is connected
166 if (msm_dsi->panel && IS_DUAL_PANEL() &&
167 other_dsi && other_dsi->panel) {
168 bool cmd_mode = !(msm_dsi->panel_flags &
169 MIPI_DSI_MODE_VIDEO);
170 struct drm_encoder *encoder = msm_dsi_get_encoder(
171 dsi_mgr_get_dsi(DSI_ENCODER_MASTER));
172 struct drm_encoder *slave_enc = msm_dsi_get_encoder(
173 dsi_mgr_get_dsi(DSI_ENCODER_SLAVE));
175 if (kms->funcs->set_split_display)
176 kms->funcs->set_split_display(kms, encoder,
177 slave_enc, cmd_mode);
179 pr_err("mdp does not support dual panel\n");
183 return msm_dsi->panel ? connector_status_connected :
184 connector_status_disconnected;
187 static void dsi_mgr_connector_destroy(struct drm_connector *connector)
190 drm_connector_unregister(connector);
191 drm_connector_cleanup(connector);
194 static void dsi_dual_connector_fix_modes(struct drm_connector *connector)
196 struct drm_display_mode *mode, *m;
198 /* Only support left-right mode */
199 list_for_each_entry_safe(mode, m, &connector->probed_modes, head) {
201 mode->hdisplay >>= 1;
202 mode->hsync_start >>= 1;
203 mode->hsync_end >>= 1;
205 drm_mode_set_name(mode);
209 static int dsi_dual_connector_tile_init(
210 struct drm_connector *connector, int id)
212 struct drm_display_mode *mode;
213 /* Fake topology id */
214 char topo_id[8] = {'M', 'S', 'M', 'D', 'U', 'D', 'S', 'I'};
216 if (connector->tile_group) {
217 DBG("Tile property has been initialized");
221 /* Use the first mode only for now */
222 mode = list_first_entry(&connector->probed_modes,
223 struct drm_display_mode,
228 connector->tile_group = drm_mode_get_tile_group(
229 connector->dev, topo_id);
230 if (!connector->tile_group)
231 connector->tile_group = drm_mode_create_tile_group(
232 connector->dev, topo_id);
233 if (!connector->tile_group) {
234 pr_err("%s: failed to create tile group\n", __func__);
238 connector->has_tile = true;
239 connector->tile_is_single_monitor = true;
241 /* mode has been fixed */
242 connector->tile_h_size = mode->hdisplay;
243 connector->tile_v_size = mode->vdisplay;
245 /* Only support left-right mode */
246 connector->num_h_tile = 2;
247 connector->num_v_tile = 1;
249 connector->tile_v_loc = 0;
250 connector->tile_h_loc = (id == DSI_RIGHT) ? 1 : 0;
255 static int dsi_mgr_connector_get_modes(struct drm_connector *connector)
257 int id = dsi_mgr_connector_get_id(connector);
258 struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
259 struct drm_panel *panel = msm_dsi->panel;
265 /* Since we have 2 connectors, but only 1 drm_panel in dual DSI mode,
266 * panel should not attach to any connector.
267 * Only temporarily attach panel to the current connector here,
268 * to let panel set mode to this connector.
270 drm_panel_attach(panel, connector);
271 num = drm_panel_get_modes(panel);
272 drm_panel_detach(panel);
276 if (IS_DUAL_PANEL()) {
277 /* report half resolution to user */
278 dsi_dual_connector_fix_modes(connector);
279 ret = dsi_dual_connector_tile_init(connector, id);
282 ret = drm_mode_connector_set_tile_property(connector);
284 pr_err("%s: set tile property failed, %d\n",
293 static int dsi_mgr_connector_mode_valid(struct drm_connector *connector,
294 struct drm_display_mode *mode)
296 int id = dsi_mgr_connector_get_id(connector);
297 struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
298 struct drm_encoder *encoder = msm_dsi_get_encoder(msm_dsi);
299 struct msm_drm_private *priv = connector->dev->dev_private;
300 struct msm_kms *kms = priv->kms;
301 long actual, requested;
304 requested = 1000 * mode->clock;
305 actual = kms->funcs->round_pixclk(kms, requested, encoder);
307 DBG("requested=%ld, actual=%ld", requested, actual);
308 if (actual != requested)
309 return MODE_CLOCK_RANGE;
314 static struct drm_encoder *
315 dsi_mgr_connector_best_encoder(struct drm_connector *connector)
317 int id = dsi_mgr_connector_get_id(connector);
318 struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
321 return msm_dsi_get_encoder(msm_dsi);
324 static void dsi_mgr_bridge_pre_enable(struct drm_bridge *bridge)
326 int id = dsi_mgr_bridge_get_id(bridge);
327 struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
328 struct msm_dsi *msm_dsi1 = dsi_mgr_get_dsi(DSI_1);
329 struct mipi_dsi_host *host = msm_dsi->host;
330 struct drm_panel *panel = msm_dsi->panel;
331 bool is_dual_panel = IS_DUAL_PANEL();
335 if (!panel || (is_dual_panel && (DSI_1 == id)))
338 ret = msm_dsi_host_power_on(host);
340 pr_err("%s: power on host %d failed, %d\n", __func__, id, ret);
344 if (is_dual_panel && msm_dsi1) {
345 ret = msm_dsi_host_power_on(msm_dsi1->host);
347 pr_err("%s: power on host1 failed, %d\n",
353 /* Always call panel functions once, because even for dual panels,
354 * there is only one drm_panel instance.
356 ret = drm_panel_prepare(panel);
358 pr_err("%s: prepare panel %d failed, %d\n", __func__, id, ret);
359 goto panel_prep_fail;
362 ret = msm_dsi_host_enable(host);
364 pr_err("%s: enable host %d failed, %d\n", __func__, id, ret);
368 if (is_dual_panel && msm_dsi1) {
369 ret = msm_dsi_host_enable(msm_dsi1->host);
371 pr_err("%s: enable host1 failed, %d\n", __func__, ret);
376 ret = drm_panel_enable(panel);
378 pr_err("%s: enable panel %d failed, %d\n", __func__, id, ret);
385 if (is_dual_panel && msm_dsi1)
386 msm_dsi_host_disable(msm_dsi1->host);
388 msm_dsi_host_disable(host);
390 drm_panel_unprepare(panel);
392 if (is_dual_panel && msm_dsi1)
393 msm_dsi_host_power_off(msm_dsi1->host);
395 msm_dsi_host_power_off(host);
400 static void dsi_mgr_bridge_enable(struct drm_bridge *bridge)
405 static void dsi_mgr_bridge_disable(struct drm_bridge *bridge)
410 static void dsi_mgr_bridge_post_disable(struct drm_bridge *bridge)
412 int id = dsi_mgr_bridge_get_id(bridge);
413 struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
414 struct msm_dsi *msm_dsi1 = dsi_mgr_get_dsi(DSI_1);
415 struct mipi_dsi_host *host = msm_dsi->host;
416 struct drm_panel *panel = msm_dsi->panel;
417 bool is_dual_panel = IS_DUAL_PANEL();
422 if (!panel || (is_dual_panel && (DSI_1 == id)))
425 ret = drm_panel_disable(panel);
427 pr_err("%s: Panel %d OFF failed, %d\n", __func__, id, ret);
429 ret = msm_dsi_host_disable(host);
431 pr_err("%s: host %d disable failed, %d\n", __func__, id, ret);
433 if (is_dual_panel && msm_dsi1) {
434 ret = msm_dsi_host_disable(msm_dsi1->host);
436 pr_err("%s: host1 disable failed, %d\n", __func__, ret);
439 ret = drm_panel_unprepare(panel);
441 pr_err("%s: Panel %d unprepare failed,%d\n", __func__, id, ret);
443 ret = msm_dsi_host_power_off(host);
445 pr_err("%s: host %d power off failed,%d\n", __func__, id, ret);
447 if (is_dual_panel && msm_dsi1) {
448 ret = msm_dsi_host_power_off(msm_dsi1->host);
450 pr_err("%s: host1 power off failed, %d\n",
455 static void dsi_mgr_bridge_mode_set(struct drm_bridge *bridge,
456 struct drm_display_mode *mode,
457 struct drm_display_mode *adjusted_mode)
459 int id = dsi_mgr_bridge_get_id(bridge);
460 struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
461 struct msm_dsi *other_dsi = dsi_mgr_get_other_dsi(id);
462 struct mipi_dsi_host *host = msm_dsi->host;
463 bool is_dual_panel = IS_DUAL_PANEL();
465 DBG("set mode: %d:\"%s\" %d %d %d %d %d %d %d %d %d %d 0x%x 0x%x",
466 mode->base.id, mode->name,
467 mode->vrefresh, mode->clock,
468 mode->hdisplay, mode->hsync_start,
469 mode->hsync_end, mode->htotal,
470 mode->vdisplay, mode->vsync_start,
471 mode->vsync_end, mode->vtotal,
472 mode->type, mode->flags);
474 if (is_dual_panel && (DSI_1 == id))
477 msm_dsi_host_set_display_mode(host, adjusted_mode);
478 if (is_dual_panel && other_dsi)
479 msm_dsi_host_set_display_mode(other_dsi->host, adjusted_mode);
482 static const struct drm_connector_funcs dsi_mgr_connector_funcs = {
483 .dpms = drm_atomic_helper_connector_dpms,
484 .detect = dsi_mgr_connector_detect,
485 .fill_modes = drm_helper_probe_single_connector_modes,
486 .destroy = dsi_mgr_connector_destroy,
487 .reset = drm_atomic_helper_connector_reset,
488 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
489 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
492 static const struct drm_connector_helper_funcs dsi_mgr_conn_helper_funcs = {
493 .get_modes = dsi_mgr_connector_get_modes,
494 .mode_valid = dsi_mgr_connector_mode_valid,
495 .best_encoder = dsi_mgr_connector_best_encoder,
498 static const struct drm_bridge_funcs dsi_mgr_bridge_funcs = {
499 .pre_enable = dsi_mgr_bridge_pre_enable,
500 .enable = dsi_mgr_bridge_enable,
501 .disable = dsi_mgr_bridge_disable,
502 .post_disable = dsi_mgr_bridge_post_disable,
503 .mode_set = dsi_mgr_bridge_mode_set,
506 /* initialize connector */
507 struct drm_connector *msm_dsi_manager_connector_init(u8 id)
509 struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
510 struct drm_connector *connector = NULL;
511 struct dsi_connector *dsi_connector;
514 dsi_connector = devm_kzalloc(msm_dsi->dev->dev,
515 sizeof(*dsi_connector), GFP_KERNEL);
516 if (!dsi_connector) {
521 dsi_connector->id = id;
523 connector = &dsi_connector->base;
525 ret = drm_connector_init(msm_dsi->dev, connector,
526 &dsi_mgr_connector_funcs, DRM_MODE_CONNECTOR_DSI);
530 drm_connector_helper_add(connector, &dsi_mgr_conn_helper_funcs);
532 /* Enable HPD to let hpd event is handled
533 * when panel is attached to the host.
535 connector->polled = DRM_CONNECTOR_POLL_HPD;
537 /* Display driver doesn't support interlace now. */
538 connector->interlace_allowed = 0;
539 connector->doublescan_allowed = 0;
541 ret = drm_connector_register(connector);
545 for (i = 0; i < MSM_DSI_ENCODER_NUM; i++)
546 drm_mode_connector_attach_encoder(connector,
547 msm_dsi->encoders[i]);
553 dsi_mgr_connector_destroy(connector);
558 /* initialize bridge */
559 struct drm_bridge *msm_dsi_manager_bridge_init(u8 id)
561 struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
562 struct drm_bridge *bridge = NULL;
563 struct dsi_bridge *dsi_bridge;
566 dsi_bridge = devm_kzalloc(msm_dsi->dev->dev,
567 sizeof(*dsi_bridge), GFP_KERNEL);
575 bridge = &dsi_bridge->base;
576 bridge->funcs = &dsi_mgr_bridge_funcs;
578 ret = drm_bridge_attach(msm_dsi->dev, bridge);
586 msm_dsi_manager_bridge_destroy(bridge);
591 void msm_dsi_manager_bridge_destroy(struct drm_bridge *bridge)
595 int msm_dsi_manager_phy_enable(int id,
596 const unsigned long bit_rate, const unsigned long esc_rate,
597 u32 *clk_pre, u32 *clk_post)
599 struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
600 struct msm_dsi_phy *phy = msm_dsi->phy;
603 ret = msm_dsi_phy_enable(phy, IS_DUAL_PANEL(), bit_rate, esc_rate);
607 msm_dsi->phy_enabled = true;
608 msm_dsi_phy_get_clk_pre_post(phy, clk_pre, clk_post);
613 void msm_dsi_manager_phy_disable(int id)
615 struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
616 struct msm_dsi *mdsi = dsi_mgr_get_dsi(DSI_CLOCK_MASTER);
617 struct msm_dsi *sdsi = dsi_mgr_get_dsi(DSI_CLOCK_SLAVE);
618 struct msm_dsi_phy *phy = msm_dsi->phy;
621 * In dual-dsi configuration, the phy should be disabled for the
622 * first controller only when the second controller is disabled.
624 msm_dsi->phy_enabled = false;
625 if (IS_DUAL_PANEL() && mdsi && sdsi) {
626 if (!mdsi->phy_enabled && !sdsi->phy_enabled) {
627 msm_dsi_phy_disable(sdsi->phy);
628 msm_dsi_phy_disable(mdsi->phy);
631 msm_dsi_phy_disable(phy);
635 int msm_dsi_manager_cmd_xfer(int id, const struct mipi_dsi_msg *msg)
637 struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
638 struct msm_dsi *msm_dsi0 = dsi_mgr_get_dsi(DSI_0);
639 struct mipi_dsi_host *host = msm_dsi->host;
640 bool is_read = (msg->rx_buf && msg->rx_len);
641 bool need_sync = (IS_SYNC_NEEDED() && !is_read);
644 if (!msg->tx_buf || !msg->tx_len)
647 /* In dual master case, panel requires the same commands sent to
648 * both DSI links. Host issues the command trigger to both links
649 * when DSI_1 calls the cmd transfer function, no matter it happens
650 * before or after DSI_0 cmd transfer.
652 if (need_sync && (id == DSI_0))
653 return is_read ? msg->rx_len : msg->tx_len;
655 if (need_sync && msm_dsi0) {
656 ret = msm_dsi_host_xfer_prepare(msm_dsi0->host, msg);
658 pr_err("%s: failed to prepare non-trigger host, %d\n",
663 ret = msm_dsi_host_xfer_prepare(host, msg);
665 pr_err("%s: failed to prepare host, %d\n", __func__, ret);
669 ret = is_read ? msm_dsi_host_cmd_rx(host, msg) :
670 msm_dsi_host_cmd_tx(host, msg);
672 msm_dsi_host_xfer_restore(host, msg);
675 if (need_sync && msm_dsi0)
676 msm_dsi_host_xfer_restore(msm_dsi0->host, msg);
681 bool msm_dsi_manager_cmd_xfer_trigger(int id, u32 iova, u32 len)
683 struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
684 struct msm_dsi *msm_dsi0 = dsi_mgr_get_dsi(DSI_0);
685 struct mipi_dsi_host *host = msm_dsi->host;
687 if (IS_SYNC_NEEDED() && (id == DSI_0))
690 if (IS_SYNC_NEEDED() && msm_dsi0)
691 msm_dsi_host_cmd_xfer_commit(msm_dsi0->host, iova, len);
693 msm_dsi_host_cmd_xfer_commit(host, iova, len);
698 int msm_dsi_manager_register(struct msm_dsi *msm_dsi)
700 struct msm_dsi_manager *msm_dsim = &msm_dsim_glb;
701 int id = msm_dsi->id;
705 pr_err("%s: invalid id %d\n", __func__, id);
709 if (msm_dsim->dsi[id]) {
710 pr_err("%s: dsi%d already registered\n", __func__, id);
714 msm_dsim->dsi[id] = msm_dsi;
716 ret = dsi_mgr_parse_dual_panel(msm_dsi->pdev->dev.of_node, id);
718 pr_err("%s: failed to parse dual panel info\n", __func__);
722 ret = dsi_mgr_host_register(id);
724 pr_err("%s: failed to register mipi dsi host for DSI %d\n",
732 msm_dsim->dsi[id] = NULL;
736 void msm_dsi_manager_unregister(struct msm_dsi *msm_dsi)
738 struct msm_dsi_manager *msm_dsim = &msm_dsim_glb;
741 msm_dsi_host_unregister(msm_dsi->host);
742 msm_dsim->dsi[msm_dsi->id] = NULL;