xref: /linux/drivers/gpu/drm/msm/dsi/dsi.c (revision 8ccd54fe45713cd458015b5b08d6098545e70543)
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 	priv->dsi[msm_dsi->id] = NULL;
130 }
131 
132 static const struct component_ops dsi_ops = {
133 	.bind   = dsi_bind,
134 	.unbind = dsi_unbind,
135 };
136 
137 int dsi_dev_attach(struct platform_device *pdev)
138 {
139 	return component_add(&pdev->dev, &dsi_ops);
140 }
141 
142 void dsi_dev_detach(struct platform_device *pdev)
143 {
144 	component_del(&pdev->dev, &dsi_ops);
145 }
146 
147 static int dsi_dev_probe(struct platform_device *pdev)
148 {
149 	struct msm_dsi *msm_dsi;
150 
151 	DBG("");
152 	msm_dsi = dsi_init(pdev);
153 	if (IS_ERR(msm_dsi)) {
154 		/* Don't fail the bind if the dsi port is not connected */
155 		if (PTR_ERR(msm_dsi) == -ENODEV)
156 			return 0;
157 		else
158 			return PTR_ERR(msm_dsi);
159 	}
160 
161 	return 0;
162 }
163 
164 static int dsi_dev_remove(struct platform_device *pdev)
165 {
166 	struct msm_dsi *msm_dsi = platform_get_drvdata(pdev);
167 
168 	DBG("");
169 	dsi_destroy(msm_dsi);
170 
171 	return 0;
172 }
173 
174 static const struct of_device_id dt_match[] = {
175 	{ .compatible = "qcom,mdss-dsi-ctrl" },
176 
177 	/* Deprecated, don't use */
178 	{ .compatible = "qcom,dsi-ctrl-6g-qcm2290" },
179 	{}
180 };
181 
182 static const struct dev_pm_ops dsi_pm_ops = {
183 	SET_RUNTIME_PM_OPS(msm_dsi_runtime_suspend, msm_dsi_runtime_resume, NULL)
184 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
185 				pm_runtime_force_resume)
186 };
187 
188 static struct platform_driver dsi_driver = {
189 	.probe = dsi_dev_probe,
190 	.remove = dsi_dev_remove,
191 	.driver = {
192 		.name = "msm_dsi",
193 		.of_match_table = dt_match,
194 		.pm = &dsi_pm_ops,
195 	},
196 };
197 
198 void __init msm_dsi_register(void)
199 {
200 	DBG("");
201 	msm_dsi_phy_driver_register();
202 	platform_driver_register(&dsi_driver);
203 }
204 
205 void __exit msm_dsi_unregister(void)
206 {
207 	DBG("");
208 	msm_dsi_phy_driver_unregister();
209 	platform_driver_unregister(&dsi_driver);
210 }
211 
212 int msm_dsi_modeset_init(struct msm_dsi *msm_dsi, struct drm_device *dev,
213 			 struct drm_encoder *encoder)
214 {
215 	struct msm_drm_private *priv = dev->dev_private;
216 	int ret;
217 
218 	if (priv->num_bridges == ARRAY_SIZE(priv->bridges)) {
219 		DRM_DEV_ERROR(dev->dev, "too many bridges\n");
220 		return -ENOSPC;
221 	}
222 
223 	msm_dsi->dev = dev;
224 
225 	ret = msm_dsi_host_modeset_init(msm_dsi->host, dev);
226 	if (ret) {
227 		DRM_DEV_ERROR(dev->dev, "failed to modeset init host: %d\n", ret);
228 		goto fail;
229 	}
230 
231 	if (msm_dsi_is_bonded_dsi(msm_dsi) &&
232 	    !msm_dsi_is_master_dsi(msm_dsi)) {
233 		/*
234 		 * Do not return an eror here,
235 		 * Just skip creating encoder/connector for the slave-DSI.
236 		 */
237 		return 0;
238 	}
239 
240 	msm_dsi->encoder = encoder;
241 
242 	msm_dsi->bridge = msm_dsi_manager_bridge_init(msm_dsi->id);
243 	if (IS_ERR(msm_dsi->bridge)) {
244 		ret = PTR_ERR(msm_dsi->bridge);
245 		DRM_DEV_ERROR(dev->dev, "failed to create dsi bridge: %d\n", ret);
246 		msm_dsi->bridge = NULL;
247 		goto fail;
248 	}
249 
250 	ret = msm_dsi_manager_ext_bridge_init(msm_dsi->id);
251 	if (ret) {
252 		DRM_DEV_ERROR(dev->dev,
253 			"failed to create dsi connector: %d\n", ret);
254 		goto fail;
255 	}
256 
257 	priv->bridges[priv->num_bridges++]       = msm_dsi->bridge;
258 
259 	return 0;
260 fail:
261 	/* bridge/connector are normally destroyed by drm: */
262 	if (msm_dsi->bridge) {
263 		msm_dsi_manager_bridge_destroy(msm_dsi->bridge);
264 		msm_dsi->bridge = NULL;
265 	}
266 
267 	return ret;
268 }
269 
270 void msm_dsi_snapshot(struct msm_disp_state *disp_state, struct msm_dsi *msm_dsi)
271 {
272 	msm_dsi_host_snapshot(disp_state, msm_dsi->host);
273 	msm_dsi_phy_snapshot(disp_state, msm_dsi->phy);
274 }
275 
276