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