xref: /linux/drivers/gpu/drm/msm/dsi/dsi.c (revision 89755ee1d59311855b4afcf35aebddcb653b3cd5)
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 void 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 
172 static const struct of_device_id dt_match[] = {
173 	{ .compatible = "qcom,mdss-dsi-ctrl" },
174 
175 	/* Deprecated, don't use */
176 	{ .compatible = "qcom,dsi-ctrl-6g-qcm2290" },
177 	{}
178 };
179 
180 static const struct dev_pm_ops dsi_pm_ops = {
181 	SET_RUNTIME_PM_OPS(msm_dsi_runtime_suspend, msm_dsi_runtime_resume, NULL)
182 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
183 				pm_runtime_force_resume)
184 };
185 
186 static struct platform_driver dsi_driver = {
187 	.probe = dsi_dev_probe,
188 	.remove_new = dsi_dev_remove,
189 	.driver = {
190 		.name = "msm_dsi",
191 		.of_match_table = dt_match,
192 		.pm = &dsi_pm_ops,
193 	},
194 };
195 
196 void __init msm_dsi_register(void)
197 {
198 	DBG("");
199 	msm_dsi_phy_driver_register();
200 	platform_driver_register(&dsi_driver);
201 }
202 
203 void __exit msm_dsi_unregister(void)
204 {
205 	DBG("");
206 	msm_dsi_phy_driver_unregister();
207 	platform_driver_unregister(&dsi_driver);
208 }
209 
210 int msm_dsi_modeset_init(struct msm_dsi *msm_dsi, struct drm_device *dev,
211 			 struct drm_encoder *encoder)
212 {
213 	struct msm_drm_private *priv = dev->dev_private;
214 	int ret;
215 
216 	if (priv->num_bridges == ARRAY_SIZE(priv->bridges)) {
217 		DRM_DEV_ERROR(dev->dev, "too many bridges\n");
218 		return -ENOSPC;
219 	}
220 
221 	msm_dsi->dev = dev;
222 
223 	ret = msm_dsi_host_modeset_init(msm_dsi->host, dev);
224 	if (ret) {
225 		DRM_DEV_ERROR(dev->dev, "failed to modeset init host: %d\n", ret);
226 		goto fail;
227 	}
228 
229 	if (msm_dsi_is_bonded_dsi(msm_dsi) &&
230 	    !msm_dsi_is_master_dsi(msm_dsi)) {
231 		/*
232 		 * Do not return an eror here,
233 		 * Just skip creating encoder/connector for the slave-DSI.
234 		 */
235 		return 0;
236 	}
237 
238 	msm_dsi->encoder = encoder;
239 
240 	msm_dsi->bridge = msm_dsi_manager_bridge_init(msm_dsi->id);
241 	if (IS_ERR(msm_dsi->bridge)) {
242 		ret = PTR_ERR(msm_dsi->bridge);
243 		DRM_DEV_ERROR(dev->dev, "failed to create dsi bridge: %d\n", ret);
244 		msm_dsi->bridge = NULL;
245 		goto fail;
246 	}
247 
248 	ret = msm_dsi_manager_ext_bridge_init(msm_dsi->id);
249 	if (ret) {
250 		DRM_DEV_ERROR(dev->dev,
251 			"failed to create dsi connector: %d\n", ret);
252 		goto fail;
253 	}
254 
255 	priv->bridges[priv->num_bridges++]       = msm_dsi->bridge;
256 
257 	return 0;
258 fail:
259 	/* bridge/connector are normally destroyed by drm: */
260 	if (msm_dsi->bridge) {
261 		msm_dsi_manager_bridge_destroy(msm_dsi->bridge);
262 		msm_dsi->bridge = NULL;
263 	}
264 
265 	return ret;
266 }
267 
268 void msm_dsi_snapshot(struct msm_disp_state *disp_state, struct msm_dsi *msm_dsi)
269 {
270 	msm_dsi_host_snapshot(disp_state, msm_dsi->host);
271 	msm_dsi_phy_snapshot(disp_state, msm_dsi->phy);
272 }
273 
274