xref: /linux/drivers/gpu/drm/msm/dsi/dsi.c (revision a9ddac9c5765712fa7eace55feeaf7c4ac75e32b)
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 {
18a689554bSHai Li 	if (!msm_dsi || !msm_dsi->panel)
19a689554bSHai Li 		return NULL;
20a689554bSHai Li 
21*a9ddac9cSArchit 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 
32ec31abf6SHai Li 	phy_node = of_parse_phandle(pdev->dev.of_node, "qcom,dsi-phy", 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;
193a689554bSHai Li 	int ret, i;
194a689554bSHai Li 
195a689554bSHai Li 	if (WARN_ON(!encoders[MSM_DSI_VIDEO_ENCODER_ID] ||
196a689554bSHai Li 		!encoders[MSM_DSI_CMD_ENCODER_ID]))
197a689554bSHai Li 		return -EINVAL;
198a689554bSHai Li 
199a689554bSHai Li 	msm_dsi->dev = dev;
200a689554bSHai Li 
201a689554bSHai Li 	ret = msm_dsi_host_modeset_init(msm_dsi->host, dev);
202a689554bSHai Li 	if (ret) {
203a689554bSHai Li 		dev_err(dev->dev, "failed to modeset init host: %d\n", ret);
204a689554bSHai Li 		goto fail;
205a689554bSHai Li 	}
206a689554bSHai Li 
207a689554bSHai Li 	msm_dsi->bridge = msm_dsi_manager_bridge_init(msm_dsi->id);
208a689554bSHai Li 	if (IS_ERR(msm_dsi->bridge)) {
209a689554bSHai Li 		ret = PTR_ERR(msm_dsi->bridge);
210a689554bSHai Li 		dev_err(dev->dev, "failed to create dsi bridge: %d\n", ret);
211a689554bSHai Li 		msm_dsi->bridge = NULL;
212a689554bSHai Li 		goto fail;
213a689554bSHai Li 	}
214a689554bSHai Li 
2156f6b2879SHai Li 	for (i = 0; i < MSM_DSI_ENCODER_NUM; i++) {
2166f6b2879SHai Li 		encoders[i]->bridge = msm_dsi->bridge;
2176f6b2879SHai Li 		msm_dsi->encoders[i] = encoders[i];
2186f6b2879SHai Li 	}
2196f6b2879SHai Li 
220a689554bSHai Li 	msm_dsi->connector = msm_dsi_manager_connector_init(msm_dsi->id);
221a689554bSHai Li 	if (IS_ERR(msm_dsi->connector)) {
222a689554bSHai Li 		ret = PTR_ERR(msm_dsi->connector);
223a689554bSHai Li 		dev_err(dev->dev, "failed to create dsi connector: %d\n", ret);
224a689554bSHai Li 		msm_dsi->connector = NULL;
225a689554bSHai Li 		goto fail;
226a689554bSHai Li 	}
227a689554bSHai Li 
228a689554bSHai Li 	priv->bridges[priv->num_bridges++]       = msm_dsi->bridge;
229a689554bSHai Li 	priv->connectors[priv->num_connectors++] = msm_dsi->connector;
230a689554bSHai Li 
231a689554bSHai Li 	return 0;
232a689554bSHai Li fail:
233a689554bSHai Li 	if (msm_dsi) {
234a689554bSHai Li 		/* bridge/connector are normally destroyed by drm: */
235a689554bSHai Li 		if (msm_dsi->bridge) {
236a689554bSHai Li 			msm_dsi_manager_bridge_destroy(msm_dsi->bridge);
237a689554bSHai Li 			msm_dsi->bridge = NULL;
238a689554bSHai Li 		}
239a689554bSHai Li 		if (msm_dsi->connector) {
240a689554bSHai Li 			msm_dsi->connector->funcs->destroy(msm_dsi->connector);
241a689554bSHai Li 			msm_dsi->connector = NULL;
242a689554bSHai Li 		}
243a689554bSHai Li 	}
244a689554bSHai Li 
245a689554bSHai Li 	return ret;
246a689554bSHai Li }
247a689554bSHai Li 
248