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