xref: /linux/drivers/gpu/drm/msm/dsi/dsi.c (revision c04c3148ca12227d92f91b355b4538cc333c9922)
197fb5e8dSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2a689554bSHai Li /*
3a689554bSHai Li  * Copyright (c) 2015, The Linux Foundation. All rights reserved.
4a689554bSHai Li  */
5a689554bSHai Li 
6a689554bSHai Li #include "dsi.h"
7a689554bSHai Li 
8a689554bSHai Li struct drm_encoder *msm_dsi_get_encoder(struct msm_dsi *msm_dsi)
9a689554bSHai Li {
106f054ec5SArchit Taneja 	if (!msm_dsi || !msm_dsi_device_connected(msm_dsi))
11a689554bSHai Li 		return NULL;
12a689554bSHai Li 
1397e00119SArchit Taneja 	return msm_dsi->encoder;
14a689554bSHai Li }
15a689554bSHai Li 
16f518f6c1SDmitry Baryshkov bool msm_dsi_is_cmd_mode(struct msm_dsi *msm_dsi)
17f518f6c1SDmitry Baryshkov {
18f518f6c1SDmitry Baryshkov 	unsigned long host_flags = msm_dsi_host_get_mode_flags(msm_dsi->host);
19f518f6c1SDmitry Baryshkov 
20f518f6c1SDmitry Baryshkov 	return !(host_flags & MIPI_DSI_MODE_VIDEO);
21f518f6c1SDmitry Baryshkov }
22f518f6c1SDmitry Baryshkov 
23ec31abf6SHai Li static int dsi_get_phy(struct msm_dsi *msm_dsi)
24ec31abf6SHai Li {
25ec31abf6SHai Li 	struct platform_device *pdev = msm_dsi->pdev;
26ec31abf6SHai Li 	struct platform_device *phy_pdev;
27ec31abf6SHai Li 	struct device_node *phy_node;
28ec31abf6SHai Li 
2969696ea0SArchit Taneja 	phy_node = of_parse_phandle(pdev->dev.of_node, "phys", 0);
30ec31abf6SHai Li 	if (!phy_node) {
316a41da17SMamta Shukla 		DRM_DEV_ERROR(&pdev->dev, "cannot find phy device\n");
32ec31abf6SHai Li 		return -ENXIO;
33ec31abf6SHai Li 	}
34ec31abf6SHai Li 
35ec31abf6SHai Li 	phy_pdev = of_find_device_by_node(phy_node);
366977cc89SChristophe JAILLET 	if (phy_pdev) {
37ec31abf6SHai Li 		msm_dsi->phy = platform_get_drvdata(phy_pdev);
386977cc89SChristophe JAILLET 		msm_dsi->phy_dev = &phy_pdev->dev;
396977cc89SChristophe JAILLET 	}
40ec31abf6SHai Li 
41ec31abf6SHai Li 	of_node_put(phy_node);
42ec31abf6SHai Li 
43*c04c3148SMiaoqian Lin 	if (!phy_pdev) {
44*c04c3148SMiaoqian Lin 		DRM_DEV_ERROR(&pdev->dev, "%s: phy driver is not ready\n", __func__);
45*c04c3148SMiaoqian Lin 		return -EPROBE_DEFER;
46*c04c3148SMiaoqian Lin 	}
47*c04c3148SMiaoqian Lin 	if (!msm_dsi->phy) {
48*c04c3148SMiaoqian Lin 		put_device(&phy_pdev->dev);
496a41da17SMamta Shukla 		DRM_DEV_ERROR(&pdev->dev, "%s: phy driver is not ready\n", __func__);
50ec31abf6SHai Li 		return -EPROBE_DEFER;
51ec31abf6SHai Li 	}
52ec31abf6SHai Li 
53ec31abf6SHai Li 	return 0;
54ec31abf6SHai Li }
55ec31abf6SHai Li 
56a689554bSHai Li static void dsi_destroy(struct msm_dsi *msm_dsi)
57a689554bSHai Li {
58a689554bSHai Li 	if (!msm_dsi)
59a689554bSHai Li 		return;
60a689554bSHai Li 
61a689554bSHai Li 	msm_dsi_manager_unregister(msm_dsi);
629d32c498SHai Li 
63ec31abf6SHai Li 	if (msm_dsi->phy_dev) {
64ec31abf6SHai Li 		put_device(msm_dsi->phy_dev);
659d32c498SHai Li 		msm_dsi->phy = NULL;
66ec31abf6SHai Li 		msm_dsi->phy_dev = NULL;
679d32c498SHai Li 	}
689d32c498SHai Li 
69a689554bSHai Li 	if (msm_dsi->host) {
70a689554bSHai Li 		msm_dsi_host_destroy(msm_dsi->host);
71a689554bSHai Li 		msm_dsi->host = NULL;
72a689554bSHai Li 	}
73a689554bSHai Li 
74a689554bSHai Li 	platform_set_drvdata(msm_dsi->pdev, NULL);
75a689554bSHai Li }
76a689554bSHai Li 
77a689554bSHai Li static struct msm_dsi *dsi_init(struct platform_device *pdev)
78a689554bSHai Li {
79da882cd1SMarkus Elfring 	struct msm_dsi *msm_dsi;
80a689554bSHai Li 	int ret;
81a689554bSHai Li 
82da882cd1SMarkus Elfring 	if (!pdev)
83da882cd1SMarkus Elfring 		return ERR_PTR(-ENXIO);
84a689554bSHai Li 
85a689554bSHai Li 	msm_dsi = devm_kzalloc(&pdev->dev, sizeof(*msm_dsi), GFP_KERNEL);
86da882cd1SMarkus Elfring 	if (!msm_dsi)
87da882cd1SMarkus Elfring 		return ERR_PTR(-ENOMEM);
88a689554bSHai Li 	DBG("dsi probed=%p", msm_dsi);
89a689554bSHai Li 
90aea24171SSean Paul 	msm_dsi->id = -1;
91a689554bSHai Li 	msm_dsi->pdev = pdev;
92a689554bSHai Li 	platform_set_drvdata(pdev, msm_dsi);
93a689554bSHai Li 
94a689554bSHai Li 	/* Init dsi host */
95a689554bSHai Li 	ret = msm_dsi_host_init(msm_dsi);
96a689554bSHai Li 	if (ret)
97da882cd1SMarkus Elfring 		goto destroy_dsi;
98a689554bSHai Li 
99ec31abf6SHai Li 	/* GET dsi PHY */
100ec31abf6SHai Li 	ret = dsi_get_phy(msm_dsi);
101ec31abf6SHai Li 	if (ret)
102da882cd1SMarkus Elfring 		goto destroy_dsi;
1039d32c498SHai Li 
104a689554bSHai Li 	/* Register to dsi manager */
105a689554bSHai Li 	ret = msm_dsi_manager_register(msm_dsi);
106a689554bSHai Li 	if (ret)
107da882cd1SMarkus Elfring 		goto destroy_dsi;
108a689554bSHai Li 
109a689554bSHai Li 	return msm_dsi;
110a689554bSHai Li 
111da882cd1SMarkus Elfring destroy_dsi:
112a689554bSHai Li 	dsi_destroy(msm_dsi);
113a689554bSHai Li 	return ERR_PTR(ret);
114a689554bSHai Li }
115a689554bSHai Li 
116a689554bSHai Li static int dsi_bind(struct device *dev, struct device *master, void *data)
117a689554bSHai Li {
118ec919e6eSAngeloGioacchino Del Regno 	struct msm_drm_private *priv = dev_get_drvdata(master);
1198f59ee9aSRob Clark 	struct msm_dsi *msm_dsi = dev_get_drvdata(dev);
120a689554bSHai Li 
121a689554bSHai Li 	priv->dsi[msm_dsi->id] = msm_dsi;
122a689554bSHai Li 
123a689554bSHai Li 	return 0;
124a689554bSHai Li }
125a689554bSHai Li 
126a689554bSHai Li static void dsi_unbind(struct device *dev, struct device *master,
127a689554bSHai Li 		void *data)
128a689554bSHai Li {
129ec919e6eSAngeloGioacchino Del Regno 	struct msm_drm_private *priv = dev_get_drvdata(master);
130a689554bSHai Li 	struct msm_dsi *msm_dsi = dev_get_drvdata(dev);
131a689554bSHai Li 
1328f59ee9aSRob Clark 	priv->dsi[msm_dsi->id] = NULL;
133a689554bSHai Li }
134a689554bSHai Li 
135a689554bSHai Li static const struct component_ops dsi_ops = {
136a689554bSHai Li 	.bind   = dsi_bind,
137a689554bSHai Li 	.unbind = dsi_unbind,
138a689554bSHai Li };
139a689554bSHai Li 
1408f59ee9aSRob Clark int dsi_dev_attach(struct platform_device *pdev)
141a689554bSHai Li {
142a689554bSHai Li 	return component_add(&pdev->dev, &dsi_ops);
143a689554bSHai Li }
144a689554bSHai Li 
1458f59ee9aSRob Clark void dsi_dev_detach(struct platform_device *pdev)
1468f59ee9aSRob Clark {
1478f59ee9aSRob Clark 	component_del(&pdev->dev, &dsi_ops);
1488f59ee9aSRob Clark }
1498f59ee9aSRob Clark 
1508f59ee9aSRob Clark static int dsi_dev_probe(struct platform_device *pdev)
1518f59ee9aSRob Clark {
1528f59ee9aSRob Clark 	struct msm_dsi *msm_dsi;
1538f59ee9aSRob Clark 
1548f59ee9aSRob Clark 	DBG("");
1558f59ee9aSRob Clark 	msm_dsi = dsi_init(pdev);
1568f59ee9aSRob Clark 	if (IS_ERR(msm_dsi)) {
1578f59ee9aSRob Clark 		/* Don't fail the bind if the dsi port is not connected */
1588f59ee9aSRob Clark 		if (PTR_ERR(msm_dsi) == -ENODEV)
1598f59ee9aSRob Clark 			return 0;
1608f59ee9aSRob Clark 		else
1618f59ee9aSRob Clark 			return PTR_ERR(msm_dsi);
1628f59ee9aSRob Clark 	}
1638f59ee9aSRob Clark 
1648f59ee9aSRob Clark 	return 0;
1658f59ee9aSRob Clark }
1668f59ee9aSRob Clark 
167a689554bSHai Li static int dsi_dev_remove(struct platform_device *pdev)
168a689554bSHai Li {
1698f59ee9aSRob Clark 	struct msm_dsi *msm_dsi = platform_get_drvdata(pdev);
1708f59ee9aSRob Clark 
171a689554bSHai Li 	DBG("");
1728f59ee9aSRob Clark 	dsi_destroy(msm_dsi);
1738f59ee9aSRob Clark 
174a689554bSHai Li 	return 0;
175a689554bSHai Li }
176a689554bSHai Li 
177a689554bSHai Li static const struct of_device_id dt_match[] = {
178a689554bSHai Li 	{ .compatible = "qcom,mdss-dsi-ctrl" },
179a689554bSHai Li 	{}
180a689554bSHai Li };
181a689554bSHai Li 
182f54ca1a0SArchit Taneja static const struct dev_pm_ops dsi_pm_ops = {
183f54ca1a0SArchit Taneja 	SET_RUNTIME_PM_OPS(msm_dsi_runtime_suspend, msm_dsi_runtime_resume, NULL)
184ca8199f1SKalyan Thota 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
185ca8199f1SKalyan Thota 				pm_runtime_force_resume)
186f54ca1a0SArchit Taneja };
187f54ca1a0SArchit Taneja 
188a689554bSHai Li static struct platform_driver dsi_driver = {
189a689554bSHai Li 	.probe = dsi_dev_probe,
190a689554bSHai Li 	.remove = dsi_dev_remove,
191a689554bSHai Li 	.driver = {
192a689554bSHai Li 		.name = "msm_dsi",
193a689554bSHai Li 		.of_match_table = dt_match,
194f54ca1a0SArchit Taneja 		.pm = &dsi_pm_ops,
195a689554bSHai Li 	},
196a689554bSHai Li };
197a689554bSHai Li 
198a689554bSHai Li void __init msm_dsi_register(void)
199a689554bSHai Li {
200a689554bSHai Li 	DBG("");
201ec31abf6SHai Li 	msm_dsi_phy_driver_register();
202a689554bSHai Li 	platform_driver_register(&dsi_driver);
203a689554bSHai Li }
204a689554bSHai Li 
205a689554bSHai Li void __exit msm_dsi_unregister(void)
206a689554bSHai Li {
207a689554bSHai Li 	DBG("");
208ec31abf6SHai Li 	msm_dsi_phy_driver_unregister();
209a689554bSHai Li 	platform_driver_unregister(&dsi_driver);
210a689554bSHai Li }
211a689554bSHai Li 
212a689554bSHai Li int msm_dsi_modeset_init(struct msm_dsi *msm_dsi, struct drm_device *dev,
21397e00119SArchit Taneja 			 struct drm_encoder *encoder)
214a689554bSHai Li {
21552749d60SGustavo A. R. Silva 	struct msm_drm_private *priv;
216c118e290SArchit Taneja 	struct drm_bridge *ext_bridge;
21797e00119SArchit Taneja 	int ret;
218a689554bSHai Li 
2193f0689e6SLloyd Atkinson 	if (WARN_ON(!encoder) || WARN_ON(!msm_dsi) || WARN_ON(!dev))
220a689554bSHai Li 		return -EINVAL;
221a689554bSHai Li 
22252749d60SGustavo A. R. Silva 	priv = dev->dev_private;
223a689554bSHai Li 	msm_dsi->dev = dev;
224a689554bSHai Li 
225a689554bSHai Li 	ret = msm_dsi_host_modeset_init(msm_dsi->host, dev);
226a689554bSHai Li 	if (ret) {
2276a41da17SMamta Shukla 		DRM_DEV_ERROR(dev->dev, "failed to modeset init host: %d\n", ret);
228a689554bSHai Li 		goto fail;
229a689554bSHai Li 	}
230a689554bSHai Li 
23192cb1bedSDmitry Baryshkov 	if (msm_dsi_is_bonded_dsi(msm_dsi) &&
23292cb1bedSDmitry Baryshkov 	    !msm_dsi_is_master_dsi(msm_dsi)) {
23392cb1bedSDmitry Baryshkov 		/*
23492cb1bedSDmitry Baryshkov 		 * Do not return an eror here,
23592cb1bedSDmitry Baryshkov 		 * Just skip creating encoder/connector for the slave-DSI.
23692cb1bedSDmitry Baryshkov 		 */
23792cb1bedSDmitry Baryshkov 		return 0;
238739b4e77SDan Carpenter 	}
2398b03ad30SChandan Uddaraju 
24097e00119SArchit Taneja 	msm_dsi->encoder = encoder;
2410bb70b82SArchit Taneja 
242a689554bSHai Li 	msm_dsi->bridge = msm_dsi_manager_bridge_init(msm_dsi->id);
243a689554bSHai Li 	if (IS_ERR(msm_dsi->bridge)) {
244a689554bSHai Li 		ret = PTR_ERR(msm_dsi->bridge);
2456a41da17SMamta Shukla 		DRM_DEV_ERROR(dev->dev, "failed to create dsi bridge: %d\n", ret);
246a689554bSHai Li 		msm_dsi->bridge = NULL;
247a689554bSHai Li 		goto fail;
248a689554bSHai Li 	}
249a689554bSHai Li 
250c118e290SArchit Taneja 	/*
251c118e290SArchit Taneja 	 * check if the dsi encoder output is connected to a panel or an
252c118e290SArchit Taneja 	 * external bridge. We create a connector only if we're connected to a
253c118e290SArchit Taneja 	 * drm_panel device. When we're connected to an external bridge, we
254c118e290SArchit Taneja 	 * assume that the drm_bridge driver will create the connector itself.
255c118e290SArchit Taneja 	 */
256c118e290SArchit Taneja 	ext_bridge = msm_dsi_host_get_bridge(msm_dsi->host);
257c118e290SArchit Taneja 
258c118e290SArchit Taneja 	if (ext_bridge)
259c118e290SArchit Taneja 		msm_dsi->connector =
260c118e290SArchit Taneja 			msm_dsi_manager_ext_bridge_init(msm_dsi->id);
261c118e290SArchit Taneja 	else
262c118e290SArchit Taneja 		msm_dsi->connector =
263c118e290SArchit Taneja 			msm_dsi_manager_connector_init(msm_dsi->id);
264c118e290SArchit Taneja 
265a689554bSHai Li 	if (IS_ERR(msm_dsi->connector)) {
266a689554bSHai Li 		ret = PTR_ERR(msm_dsi->connector);
2676a41da17SMamta Shukla 		DRM_DEV_ERROR(dev->dev,
268c118e290SArchit Taneja 			"failed to create dsi connector: %d\n", ret);
269a689554bSHai Li 		msm_dsi->connector = NULL;
270a689554bSHai Li 		goto fail;
271a689554bSHai Li 	}
272a689554bSHai Li 
273a689554bSHai Li 	priv->bridges[priv->num_bridges++]       = msm_dsi->bridge;
274a689554bSHai Li 	priv->connectors[priv->num_connectors++] = msm_dsi->connector;
275a689554bSHai Li 
276a689554bSHai Li 	return 0;
277a689554bSHai Li fail:
278a689554bSHai Li 	/* bridge/connector are normally destroyed by drm: */
279a689554bSHai Li 	if (msm_dsi->bridge) {
280a689554bSHai Li 		msm_dsi_manager_bridge_destroy(msm_dsi->bridge);
281a689554bSHai Li 		msm_dsi->bridge = NULL;
282a689554bSHai Li 	}
283c118e290SArchit Taneja 
284c118e290SArchit Taneja 	/* don't destroy connector if we didn't make it */
285c118e290SArchit Taneja 	if (msm_dsi->connector && !msm_dsi->external_bridge)
286a689554bSHai Li 		msm_dsi->connector->funcs->destroy(msm_dsi->connector);
287c118e290SArchit Taneja 
288a689554bSHai Li 	msm_dsi->connector = NULL;
289a689554bSHai Li 
290a689554bSHai Li 	return ret;
291a689554bSHai Li }
292a689554bSHai Li 
293eb9d6c7eSDmitry Baryshkov void msm_dsi_snapshot(struct msm_disp_state *disp_state, struct msm_dsi *msm_dsi)
2949d30a4bcSAbhinav Kumar {
295eb9d6c7eSDmitry Baryshkov 	msm_dsi_host_snapshot(disp_state, msm_dsi->host);
296d91940e2SDmitry Baryshkov 	msm_dsi_phy_snapshot(disp_state, msm_dsi->phy);
2979d30a4bcSAbhinav Kumar }
29898659487SAbhinav Kumar 
299