xref: /openbmc/linux/drivers/gpu/drm/msm/dsi/dsi.c (revision 99a15348)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2015, The Linux Foundation. All rights reserved.
4  */
5 
6 #include "dsi.h"
7 #include "dsi_cfg.h"
8 
9 struct drm_encoder *msm_dsi_get_encoder(struct msm_dsi *msm_dsi)
10 {
11 	if (!msm_dsi || !msm_dsi_device_connected(msm_dsi))
12 		return NULL;
13 
14 	return msm_dsi->encoder;
15 }
16 
17 bool msm_dsi_is_cmd_mode(struct msm_dsi *msm_dsi)
18 {
19 	unsigned long host_flags = msm_dsi_host_get_mode_flags(msm_dsi->host);
20 
21 	return !(host_flags & MIPI_DSI_MODE_VIDEO);
22 }
23 
24 static int dsi_get_phy(struct msm_dsi *msm_dsi)
25 {
26 	struct platform_device *pdev = msm_dsi->pdev;
27 	struct platform_device *phy_pdev;
28 	struct device_node *phy_node;
29 
30 	phy_node = of_parse_phandle(pdev->dev.of_node, "phys", 0);
31 	if (!phy_node) {
32 		DRM_DEV_ERROR(&pdev->dev, "cannot find phy device\n");
33 		return -ENXIO;
34 	}
35 
36 	phy_pdev = of_find_device_by_node(phy_node);
37 	if (phy_pdev) {
38 		msm_dsi->phy = platform_get_drvdata(phy_pdev);
39 		msm_dsi->phy_dev = &phy_pdev->dev;
40 	}
41 
42 	of_node_put(phy_node);
43 
44 	if (!phy_pdev) {
45 		DRM_DEV_ERROR(&pdev->dev, "%s: phy driver is not ready\n", __func__);
46 		return -EPROBE_DEFER;
47 	}
48 	if (!msm_dsi->phy) {
49 		put_device(&phy_pdev->dev);
50 		DRM_DEV_ERROR(&pdev->dev, "%s: phy driver is not ready\n", __func__);
51 		return -EPROBE_DEFER;
52 	}
53 
54 	return 0;
55 }
56 
57 static void dsi_destroy(struct msm_dsi *msm_dsi)
58 {
59 	if (!msm_dsi)
60 		return;
61 
62 	msm_dsi_manager_unregister(msm_dsi);
63 
64 	if (msm_dsi->phy_dev) {
65 		put_device(msm_dsi->phy_dev);
66 		msm_dsi->phy = NULL;
67 		msm_dsi->phy_dev = NULL;
68 	}
69 
70 	if (msm_dsi->host) {
71 		msm_dsi_host_destroy(msm_dsi->host);
72 		msm_dsi->host = NULL;
73 	}
74 
75 	platform_set_drvdata(msm_dsi->pdev, NULL);
76 }
77 
78 static struct msm_dsi *dsi_init(struct platform_device *pdev)
79 {
80 	struct msm_dsi *msm_dsi;
81 	int ret;
82 
83 	if (!pdev)
84 		return ERR_PTR(-ENXIO);
85 
86 	msm_dsi = devm_kzalloc(&pdev->dev, sizeof(*msm_dsi), GFP_KERNEL);
87 	if (!msm_dsi)
88 		return ERR_PTR(-ENOMEM);
89 	DBG("dsi probed=%p", msm_dsi);
90 
91 	msm_dsi->id = -1;
92 	msm_dsi->pdev = pdev;
93 	platform_set_drvdata(pdev, msm_dsi);
94 
95 	/* Init dsi host */
96 	ret = msm_dsi_host_init(msm_dsi);
97 	if (ret)
98 		goto destroy_dsi;
99 
100 	/* GET dsi PHY */
101 	ret = dsi_get_phy(msm_dsi);
102 	if (ret)
103 		goto destroy_dsi;
104 
105 	/* Register to dsi manager */
106 	ret = msm_dsi_manager_register(msm_dsi);
107 	if (ret)
108 		goto destroy_dsi;
109 
110 	return msm_dsi;
111 
112 destroy_dsi:
113 	dsi_destroy(msm_dsi);
114 	return ERR_PTR(ret);
115 }
116 
117 static int dsi_bind(struct device *dev, struct device *master, void *data)
118 {
119 	struct msm_drm_private *priv = dev_get_drvdata(master);
120 	struct msm_dsi *msm_dsi = dev_get_drvdata(dev);
121 
122 	priv->dsi[msm_dsi->id] = msm_dsi;
123 
124 	return 0;
125 }
126 
127 static void dsi_unbind(struct device *dev, struct device *master,
128 		void *data)
129 {
130 	struct msm_drm_private *priv = dev_get_drvdata(master);
131 	struct msm_dsi *msm_dsi = dev_get_drvdata(dev);
132 
133 	priv->dsi[msm_dsi->id] = NULL;
134 }
135 
136 static const struct component_ops dsi_ops = {
137 	.bind   = dsi_bind,
138 	.unbind = dsi_unbind,
139 };
140 
141 int dsi_dev_attach(struct platform_device *pdev)
142 {
143 	return component_add(&pdev->dev, &dsi_ops);
144 }
145 
146 void dsi_dev_detach(struct platform_device *pdev)
147 {
148 	component_del(&pdev->dev, &dsi_ops);
149 }
150 
151 static int dsi_dev_probe(struct platform_device *pdev)
152 {
153 	struct msm_dsi *msm_dsi;
154 
155 	DBG("");
156 	msm_dsi = dsi_init(pdev);
157 	if (IS_ERR(msm_dsi)) {
158 		/* Don't fail the bind if the dsi port is not connected */
159 		if (PTR_ERR(msm_dsi) == -ENODEV)
160 			return 0;
161 		else
162 			return PTR_ERR(msm_dsi);
163 	}
164 
165 	return 0;
166 }
167 
168 static int dsi_dev_remove(struct platform_device *pdev)
169 {
170 	struct msm_dsi *msm_dsi = platform_get_drvdata(pdev);
171 
172 	DBG("");
173 	dsi_destroy(msm_dsi);
174 
175 	return 0;
176 }
177 
178 static const struct of_device_id dt_match[] = {
179 	{ .compatible = "qcom,mdss-dsi-ctrl", .data = NULL /* autodetect cfg */ },
180 	{ .compatible = "qcom,dsi-ctrl-6g-qcm2290", .data = &qcm2290_dsi_cfg_handler },
181 	{}
182 };
183 
184 static const struct dev_pm_ops dsi_pm_ops = {
185 	SET_RUNTIME_PM_OPS(msm_dsi_runtime_suspend, msm_dsi_runtime_resume, NULL)
186 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
187 				pm_runtime_force_resume)
188 };
189 
190 static struct platform_driver dsi_driver = {
191 	.probe = dsi_dev_probe,
192 	.remove = dsi_dev_remove,
193 	.driver = {
194 		.name = "msm_dsi",
195 		.of_match_table = dt_match,
196 		.pm = &dsi_pm_ops,
197 	},
198 };
199 
200 void __init msm_dsi_register(void)
201 {
202 	DBG("");
203 	msm_dsi_phy_driver_register();
204 	platform_driver_register(&dsi_driver);
205 }
206 
207 void __exit msm_dsi_unregister(void)
208 {
209 	DBG("");
210 	msm_dsi_phy_driver_unregister();
211 	platform_driver_unregister(&dsi_driver);
212 }
213 
214 int msm_dsi_modeset_init(struct msm_dsi *msm_dsi, struct drm_device *dev,
215 			 struct drm_encoder *encoder)
216 {
217 	struct msm_drm_private *priv;
218 	struct drm_bridge *ext_bridge;
219 	int ret;
220 
221 	if (WARN_ON(!encoder) || WARN_ON(!msm_dsi) || WARN_ON(!dev))
222 		return -EINVAL;
223 
224 	priv = dev->dev_private;
225 	msm_dsi->dev = dev;
226 
227 	ret = msm_dsi_host_modeset_init(msm_dsi->host, dev);
228 	if (ret) {
229 		DRM_DEV_ERROR(dev->dev, "failed to modeset init host: %d\n", ret);
230 		goto fail;
231 	}
232 
233 	if (msm_dsi_is_bonded_dsi(msm_dsi) &&
234 	    !msm_dsi_is_master_dsi(msm_dsi)) {
235 		/*
236 		 * Do not return an eror here,
237 		 * Just skip creating encoder/connector for the slave-DSI.
238 		 */
239 		return 0;
240 	}
241 
242 	msm_dsi->encoder = encoder;
243 
244 	msm_dsi->bridge = msm_dsi_manager_bridge_init(msm_dsi->id);
245 	if (IS_ERR(msm_dsi->bridge)) {
246 		ret = PTR_ERR(msm_dsi->bridge);
247 		DRM_DEV_ERROR(dev->dev, "failed to create dsi bridge: %d\n", ret);
248 		msm_dsi->bridge = NULL;
249 		goto fail;
250 	}
251 
252 	/*
253 	 * check if the dsi encoder output is connected to a panel or an
254 	 * external bridge. We create a connector only if we're connected to a
255 	 * drm_panel device. When we're connected to an external bridge, we
256 	 * assume that the drm_bridge driver will create the connector itself.
257 	 */
258 	ext_bridge = msm_dsi_host_get_bridge(msm_dsi->host);
259 
260 	if (ext_bridge)
261 		msm_dsi->connector =
262 			msm_dsi_manager_ext_bridge_init(msm_dsi->id);
263 	else
264 		msm_dsi->connector =
265 			msm_dsi_manager_connector_init(msm_dsi->id);
266 
267 	if (IS_ERR(msm_dsi->connector)) {
268 		ret = PTR_ERR(msm_dsi->connector);
269 		DRM_DEV_ERROR(dev->dev,
270 			"failed to create dsi connector: %d\n", ret);
271 		msm_dsi->connector = NULL;
272 		goto fail;
273 	}
274 
275 	priv->bridges[priv->num_bridges++]       = msm_dsi->bridge;
276 	priv->connectors[priv->num_connectors++] = msm_dsi->connector;
277 
278 	return 0;
279 fail:
280 	/* bridge/connector are normally destroyed by drm: */
281 	if (msm_dsi->bridge) {
282 		msm_dsi_manager_bridge_destroy(msm_dsi->bridge);
283 		msm_dsi->bridge = NULL;
284 	}
285 
286 	/* don't destroy connector if we didn't make it */
287 	if (msm_dsi->connector && !msm_dsi->external_bridge)
288 		msm_dsi->connector->funcs->destroy(msm_dsi->connector);
289 
290 	msm_dsi->connector = NULL;
291 
292 	return ret;
293 }
294 
295 void msm_dsi_snapshot(struct msm_disp_state *disp_state, struct msm_dsi *msm_dsi)
296 {
297 	msm_dsi_host_snapshot(disp_state, msm_dsi->host);
298 	msm_dsi_phy_snapshot(disp_state, msm_dsi->phy);
299 }
300 
301