xref: /linux/drivers/gpu/drm/msm/dsi/dsi.c (revision 0bb70b82c2f91e4667f3c617505235efd6d77e46)
1a689554bSHai Li /*
2a689554bSHai Li  * Copyright (c) 2015, The Linux Foundation. All rights reserved.
3a689554bSHai Li  *
4a689554bSHai Li  * This program is free software; you can redistribute it and/or modify
5a689554bSHai Li  * it under the terms of the GNU General Public License version 2 and
6a689554bSHai Li  * only version 2 as published by the Free Software Foundation.
7a689554bSHai Li  *
8a689554bSHai Li  * This program is distributed in the hope that it will be useful,
9a689554bSHai Li  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10a689554bSHai Li  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11a689554bSHai Li  * GNU General Public License for more details.
12a689554bSHai Li  */
13a689554bSHai Li 
14a689554bSHai Li #include "dsi.h"
15a689554bSHai Li 
16a689554bSHai Li struct drm_encoder *msm_dsi_get_encoder(struct msm_dsi *msm_dsi)
17a689554bSHai Li {
186f054ec5SArchit Taneja 	if (!msm_dsi || !msm_dsi_device_connected(msm_dsi))
19a689554bSHai Li 		return NULL;
20a689554bSHai Li 
21a9ddac9cSArchit Taneja 	return (msm_dsi->device_flags & MIPI_DSI_MODE_VIDEO) ?
22a689554bSHai Li 		msm_dsi->encoders[MSM_DSI_VIDEO_ENCODER_ID] :
23a689554bSHai Li 		msm_dsi->encoders[MSM_DSI_CMD_ENCODER_ID];
24a689554bSHai Li }
25a689554bSHai Li 
26ec31abf6SHai Li static int dsi_get_phy(struct msm_dsi *msm_dsi)
27ec31abf6SHai Li {
28ec31abf6SHai Li 	struct platform_device *pdev = msm_dsi->pdev;
29ec31abf6SHai Li 	struct platform_device *phy_pdev;
30ec31abf6SHai Li 	struct device_node *phy_node;
31ec31abf6SHai Li 
3269696ea0SArchit Taneja 	phy_node = of_parse_phandle(pdev->dev.of_node, "phys", 0);
33ec31abf6SHai Li 	if (!phy_node) {
34ec31abf6SHai Li 		dev_err(&pdev->dev, "cannot find phy device\n");
35ec31abf6SHai Li 		return -ENXIO;
36ec31abf6SHai Li 	}
37ec31abf6SHai Li 
38ec31abf6SHai Li 	phy_pdev = of_find_device_by_node(phy_node);
39ec31abf6SHai Li 	if (phy_pdev)
40ec31abf6SHai Li 		msm_dsi->phy = platform_get_drvdata(phy_pdev);
41ec31abf6SHai Li 
42ec31abf6SHai Li 	of_node_put(phy_node);
43ec31abf6SHai Li 
44ec31abf6SHai Li 	if (!phy_pdev || !msm_dsi->phy) {
45ec31abf6SHai Li 		dev_err(&pdev->dev, "%s: phy driver is not ready\n", __func__);
46ec31abf6SHai Li 		return -EPROBE_DEFER;
47ec31abf6SHai Li 	}
48ec31abf6SHai Li 
49ec31abf6SHai Li 	msm_dsi->phy_dev = get_device(&phy_pdev->dev);
50ec31abf6SHai Li 
51ec31abf6SHai Li 	return 0;
52ec31abf6SHai Li }
53ec31abf6SHai Li 
54a689554bSHai Li static void dsi_destroy(struct msm_dsi *msm_dsi)
55a689554bSHai Li {
56a689554bSHai Li 	if (!msm_dsi)
57a689554bSHai Li 		return;
58a689554bSHai Li 
59a689554bSHai Li 	msm_dsi_manager_unregister(msm_dsi);
609d32c498SHai Li 
61ec31abf6SHai Li 	if (msm_dsi->phy_dev) {
62ec31abf6SHai Li 		put_device(msm_dsi->phy_dev);
639d32c498SHai Li 		msm_dsi->phy = NULL;
64ec31abf6SHai Li 		msm_dsi->phy_dev = NULL;
659d32c498SHai Li 	}
669d32c498SHai Li 
67a689554bSHai Li 	if (msm_dsi->host) {
68a689554bSHai Li 		msm_dsi_host_destroy(msm_dsi->host);
69a689554bSHai Li 		msm_dsi->host = NULL;
70a689554bSHai Li 	}
71a689554bSHai Li 
72a689554bSHai Li 	platform_set_drvdata(msm_dsi->pdev, NULL);
73a689554bSHai Li }
74a689554bSHai Li 
75a689554bSHai Li static struct msm_dsi *dsi_init(struct platform_device *pdev)
76a689554bSHai Li {
77da882cd1SMarkus Elfring 	struct msm_dsi *msm_dsi;
78a689554bSHai Li 	int ret;
79a689554bSHai Li 
80da882cd1SMarkus Elfring 	if (!pdev)
81da882cd1SMarkus Elfring 		return ERR_PTR(-ENXIO);
82a689554bSHai Li 
83a689554bSHai Li 	msm_dsi = devm_kzalloc(&pdev->dev, sizeof(*msm_dsi), GFP_KERNEL);
84da882cd1SMarkus Elfring 	if (!msm_dsi)
85da882cd1SMarkus Elfring 		return ERR_PTR(-ENOMEM);
86a689554bSHai Li 	DBG("dsi probed=%p", msm_dsi);
87a689554bSHai Li 
88a689554bSHai Li 	msm_dsi->pdev = pdev;
89a689554bSHai Li 	platform_set_drvdata(pdev, msm_dsi);
90a689554bSHai Li 
91a689554bSHai Li 	/* Init dsi host */
92a689554bSHai Li 	ret = msm_dsi_host_init(msm_dsi);
93a689554bSHai Li 	if (ret)
94da882cd1SMarkus Elfring 		goto destroy_dsi;
95a689554bSHai Li 
96ec31abf6SHai Li 	/* GET dsi PHY */
97ec31abf6SHai Li 	ret = dsi_get_phy(msm_dsi);
98ec31abf6SHai Li 	if (ret)
99da882cd1SMarkus Elfring 		goto destroy_dsi;
1009d32c498SHai Li 
101a689554bSHai Li 	/* Register to dsi manager */
102a689554bSHai Li 	ret = msm_dsi_manager_register(msm_dsi);
103a689554bSHai Li 	if (ret)
104da882cd1SMarkus Elfring 		goto destroy_dsi;
105a689554bSHai Li 
106a689554bSHai Li 	return msm_dsi;
107a689554bSHai Li 
108da882cd1SMarkus Elfring destroy_dsi:
109a689554bSHai Li 	dsi_destroy(msm_dsi);
110a689554bSHai Li 	return ERR_PTR(ret);
111a689554bSHai Li }
112a689554bSHai Li 
113a689554bSHai Li static int dsi_bind(struct device *dev, struct device *master, void *data)
114a689554bSHai Li {
115a689554bSHai Li 	struct drm_device *drm = dev_get_drvdata(master);
116a689554bSHai Li 	struct msm_drm_private *priv = drm->dev_private;
117a689554bSHai Li 	struct platform_device *pdev = to_platform_device(dev);
118a689554bSHai Li 	struct msm_dsi *msm_dsi;
119a689554bSHai Li 
120a689554bSHai Li 	DBG("");
121a689554bSHai Li 	msm_dsi = dsi_init(pdev);
122a689554bSHai Li 	if (IS_ERR(msm_dsi))
123a689554bSHai Li 		return PTR_ERR(msm_dsi);
124a689554bSHai Li 
125a689554bSHai Li 	priv->dsi[msm_dsi->id] = msm_dsi;
126a689554bSHai Li 
127a689554bSHai Li 	return 0;
128a689554bSHai Li }
129a689554bSHai Li 
130a689554bSHai Li static void dsi_unbind(struct device *dev, struct device *master,
131a689554bSHai Li 		void *data)
132a689554bSHai Li {
133a689554bSHai Li 	struct drm_device *drm = dev_get_drvdata(master);
134a689554bSHai Li 	struct msm_drm_private *priv = drm->dev_private;
135a689554bSHai Li 	struct msm_dsi *msm_dsi = dev_get_drvdata(dev);
136a689554bSHai Li 	int id = msm_dsi->id;
137a689554bSHai Li 
138a689554bSHai Li 	if (priv->dsi[id]) {
139a689554bSHai Li 		dsi_destroy(msm_dsi);
140a689554bSHai Li 		priv->dsi[id] = NULL;
141a689554bSHai Li 	}
142a689554bSHai Li }
143a689554bSHai Li 
144a689554bSHai Li static const struct component_ops dsi_ops = {
145a689554bSHai Li 	.bind   = dsi_bind,
146a689554bSHai Li 	.unbind = dsi_unbind,
147a689554bSHai Li };
148a689554bSHai Li 
149a689554bSHai Li static int dsi_dev_probe(struct platform_device *pdev)
150a689554bSHai Li {
151a689554bSHai Li 	return component_add(&pdev->dev, &dsi_ops);
152a689554bSHai Li }
153a689554bSHai Li 
154a689554bSHai Li static int dsi_dev_remove(struct platform_device *pdev)
155a689554bSHai Li {
156a689554bSHai Li 	DBG("");
157a689554bSHai Li 	component_del(&pdev->dev, &dsi_ops);
158a689554bSHai Li 	return 0;
159a689554bSHai Li }
160a689554bSHai Li 
161a689554bSHai Li static const struct of_device_id dt_match[] = {
162a689554bSHai Li 	{ .compatible = "qcom,mdss-dsi-ctrl" },
163a689554bSHai Li 	{}
164a689554bSHai Li };
165a689554bSHai Li 
166a689554bSHai Li static struct platform_driver dsi_driver = {
167a689554bSHai Li 	.probe = dsi_dev_probe,
168a689554bSHai Li 	.remove = dsi_dev_remove,
169a689554bSHai Li 	.driver = {
170a689554bSHai Li 		.name = "msm_dsi",
171a689554bSHai Li 		.of_match_table = dt_match,
172a689554bSHai Li 	},
173a689554bSHai Li };
174a689554bSHai Li 
175a689554bSHai Li void __init msm_dsi_register(void)
176a689554bSHai Li {
177a689554bSHai Li 	DBG("");
178ec31abf6SHai Li 	msm_dsi_phy_driver_register();
179a689554bSHai Li 	platform_driver_register(&dsi_driver);
180a689554bSHai Li }
181a689554bSHai Li 
182a689554bSHai Li void __exit msm_dsi_unregister(void)
183a689554bSHai Li {
184a689554bSHai Li 	DBG("");
185ec31abf6SHai Li 	msm_dsi_phy_driver_unregister();
186a689554bSHai Li 	platform_driver_unregister(&dsi_driver);
187a689554bSHai Li }
188a689554bSHai Li 
189a689554bSHai Li int msm_dsi_modeset_init(struct msm_dsi *msm_dsi, struct drm_device *dev,
190a689554bSHai Li 		struct drm_encoder *encoders[MSM_DSI_ENCODER_NUM])
191a689554bSHai Li {
192a689554bSHai Li 	struct msm_drm_private *priv = dev->dev_private;
193c118e290SArchit Taneja 	struct drm_bridge *ext_bridge;
194a689554bSHai Li 	int ret, i;
195a689554bSHai Li 
196a689554bSHai Li 	if (WARN_ON(!encoders[MSM_DSI_VIDEO_ENCODER_ID] ||
197a689554bSHai Li 		!encoders[MSM_DSI_CMD_ENCODER_ID]))
198a689554bSHai Li 		return -EINVAL;
199a689554bSHai Li 
200a689554bSHai Li 	msm_dsi->dev = dev;
201a689554bSHai Li 
202a689554bSHai Li 	ret = msm_dsi_host_modeset_init(msm_dsi->host, dev);
203a689554bSHai Li 	if (ret) {
204a689554bSHai Li 		dev_err(dev->dev, "failed to modeset init host: %d\n", ret);
205a689554bSHai Li 		goto fail;
206a689554bSHai Li 	}
207a689554bSHai Li 
208*0bb70b82SArchit Taneja 	for (i = 0; i < MSM_DSI_ENCODER_NUM; i++)
209*0bb70b82SArchit Taneja 		msm_dsi->encoders[i] = encoders[i];
210*0bb70b82SArchit Taneja 
211a689554bSHai Li 	msm_dsi->bridge = msm_dsi_manager_bridge_init(msm_dsi->id);
212a689554bSHai Li 	if (IS_ERR(msm_dsi->bridge)) {
213a689554bSHai Li 		ret = PTR_ERR(msm_dsi->bridge);
214a689554bSHai Li 		dev_err(dev->dev, "failed to create dsi bridge: %d\n", ret);
215a689554bSHai Li 		msm_dsi->bridge = NULL;
216a689554bSHai Li 		goto fail;
217a689554bSHai Li 	}
218a689554bSHai Li 
219c118e290SArchit Taneja 	/*
220c118e290SArchit Taneja 	 * check if the dsi encoder output is connected to a panel or an
221c118e290SArchit Taneja 	 * external bridge. We create a connector only if we're connected to a
222c118e290SArchit Taneja 	 * drm_panel device. When we're connected to an external bridge, we
223c118e290SArchit Taneja 	 * assume that the drm_bridge driver will create the connector itself.
224c118e290SArchit Taneja 	 */
225c118e290SArchit Taneja 	ext_bridge = msm_dsi_host_get_bridge(msm_dsi->host);
226c118e290SArchit Taneja 
227c118e290SArchit Taneja 	if (ext_bridge)
228c118e290SArchit Taneja 		msm_dsi->connector =
229c118e290SArchit Taneja 			msm_dsi_manager_ext_bridge_init(msm_dsi->id);
230c118e290SArchit Taneja 	else
231c118e290SArchit Taneja 		msm_dsi->connector =
232c118e290SArchit Taneja 			msm_dsi_manager_connector_init(msm_dsi->id);
233c118e290SArchit Taneja 
234a689554bSHai Li 	if (IS_ERR(msm_dsi->connector)) {
235a689554bSHai Li 		ret = PTR_ERR(msm_dsi->connector);
236c118e290SArchit Taneja 		dev_err(dev->dev,
237c118e290SArchit Taneja 			"failed to create dsi connector: %d\n", ret);
238a689554bSHai Li 		msm_dsi->connector = NULL;
239a689554bSHai Li 		goto fail;
240a689554bSHai Li 	}
241a689554bSHai Li 
242a689554bSHai Li 	priv->bridges[priv->num_bridges++]       = msm_dsi->bridge;
243a689554bSHai Li 	priv->connectors[priv->num_connectors++] = msm_dsi->connector;
244a689554bSHai Li 
245a689554bSHai Li 	return 0;
246a689554bSHai Li fail:
247a689554bSHai Li 	if (msm_dsi) {
248a689554bSHai Li 		/* bridge/connector are normally destroyed by drm: */
249a689554bSHai Li 		if (msm_dsi->bridge) {
250a689554bSHai Li 			msm_dsi_manager_bridge_destroy(msm_dsi->bridge);
251a689554bSHai Li 			msm_dsi->bridge = NULL;
252a689554bSHai Li 		}
253c118e290SArchit Taneja 
254c118e290SArchit Taneja 		/* don't destroy connector if we didn't make it */
255c118e290SArchit Taneja 		if (msm_dsi->connector && !msm_dsi->external_bridge)
256a689554bSHai Li 			msm_dsi->connector->funcs->destroy(msm_dsi->connector);
257c118e290SArchit Taneja 
258a689554bSHai Li 		msm_dsi->connector = NULL;
259a689554bSHai Li 	}
260a689554bSHai Li 
261a689554bSHai Li 	return ret;
262a689554bSHai Li }
263a689554bSHai Li 
264