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