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
msm_dsi_get_encoder(struct msm_dsi * msm_dsi)8 struct 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
dsi_get_phy(struct msm_dsi * msm_dsi)16 static 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
dsi_destroy(struct msm_dsi * msm_dsi)49 static 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
dsi_init(struct platform_device * pdev)70 static 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
104 destroy_dsi:
105 dsi_destroy(msm_dsi);
106 return ERR_PTR(ret);
107 }
108
dsi_bind(struct device * dev,struct device * master,void * data)109 static 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
dsi_unbind(struct device * dev,struct device * master,void * data)131 static 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
145 static const struct component_ops dsi_ops = {
146 .bind = dsi_bind,
147 .unbind = dsi_unbind,
148 };
149
dsi_dev_probe(struct platform_device * pdev)150 static int dsi_dev_probe(struct platform_device *pdev)
151 {
152 return component_add(&pdev->dev, &dsi_ops);
153 }
154
dsi_dev_remove(struct platform_device * pdev)155 static int dsi_dev_remove(struct platform_device *pdev)
156 {
157 DBG("");
158 component_del(&pdev->dev, &dsi_ops);
159 return 0;
160 }
161
162 static const struct of_device_id dt_match[] = {
163 { .compatible = "qcom,mdss-dsi-ctrl" },
164 {}
165 };
166
167 static const struct dev_pm_ops dsi_pm_ops = {
168 SET_RUNTIME_PM_OPS(msm_dsi_runtime_suspend, msm_dsi_runtime_resume, NULL)
169 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
170 pm_runtime_force_resume)
171 };
172
173 static struct platform_driver dsi_driver = {
174 .probe = dsi_dev_probe,
175 .remove = dsi_dev_remove,
176 .driver = {
177 .name = "msm_dsi",
178 .of_match_table = dt_match,
179 .pm = &dsi_pm_ops,
180 },
181 };
182
msm_dsi_register(void)183 void __init msm_dsi_register(void)
184 {
185 DBG("");
186 msm_dsi_phy_driver_register();
187 platform_driver_register(&dsi_driver);
188 }
189
msm_dsi_unregister(void)190 void __exit msm_dsi_unregister(void)
191 {
192 DBG("");
193 msm_dsi_phy_driver_unregister();
194 platform_driver_unregister(&dsi_driver);
195 }
196
msm_dsi_modeset_init(struct msm_dsi * msm_dsi,struct drm_device * dev,struct drm_encoder * encoder)197 int msm_dsi_modeset_init(struct msm_dsi *msm_dsi, struct drm_device *dev,
198 struct drm_encoder *encoder)
199 {
200 struct msm_drm_private *priv;
201 struct drm_bridge *ext_bridge;
202 int ret;
203
204 if (WARN_ON(!encoder) || WARN_ON(!msm_dsi) || WARN_ON(!dev))
205 return -EINVAL;
206
207 priv = dev->dev_private;
208
209 if (priv->num_bridges == ARRAY_SIZE(priv->bridges)) {
210 DRM_DEV_ERROR(dev->dev, "too many bridges\n");
211 return -ENOSPC;
212 }
213
214 msm_dsi->dev = dev;
215
216 ret = msm_dsi_host_modeset_init(msm_dsi->host, dev);
217 if (ret) {
218 DRM_DEV_ERROR(dev->dev, "failed to modeset init host: %d\n", ret);
219 goto fail;
220 }
221
222 if (!msm_dsi_manager_validate_current_config(msm_dsi->id)) {
223 ret = -EINVAL;
224 goto fail;
225 }
226
227 msm_dsi->encoder = encoder;
228
229 msm_dsi->bridge = msm_dsi_manager_bridge_init(msm_dsi->id);
230 if (IS_ERR(msm_dsi->bridge)) {
231 ret = PTR_ERR(msm_dsi->bridge);
232 DRM_DEV_ERROR(dev->dev, "failed to create dsi bridge: %d\n", ret);
233 msm_dsi->bridge = NULL;
234 goto fail;
235 }
236
237 /*
238 * check if the dsi encoder output is connected to a panel or an
239 * external bridge. We create a connector only if we're connected to a
240 * drm_panel device. When we're connected to an external bridge, we
241 * assume that the drm_bridge driver will create the connector itself.
242 */
243 ext_bridge = msm_dsi_host_get_bridge(msm_dsi->host);
244
245 if (ext_bridge)
246 msm_dsi->connector =
247 msm_dsi_manager_ext_bridge_init(msm_dsi->id);
248 else
249 msm_dsi->connector =
250 msm_dsi_manager_connector_init(msm_dsi->id);
251
252 if (IS_ERR(msm_dsi->connector)) {
253 ret = PTR_ERR(msm_dsi->connector);
254 DRM_DEV_ERROR(dev->dev,
255 "failed to create dsi connector: %d\n", ret);
256 msm_dsi->connector = NULL;
257 goto fail;
258 }
259
260 msm_dsi_manager_setup_encoder(msm_dsi->id);
261
262 priv->bridges[priv->num_bridges++] = msm_dsi->bridge;
263 priv->connectors[priv->num_connectors++] = msm_dsi->connector;
264
265 return 0;
266 fail:
267 /* bridge/connector are normally destroyed by drm: */
268 if (msm_dsi->bridge) {
269 msm_dsi_manager_bridge_destroy(msm_dsi->bridge);
270 msm_dsi->bridge = NULL;
271 }
272
273 /* don't destroy connector if we didn't make it */
274 if (msm_dsi->connector && !msm_dsi->external_bridge)
275 msm_dsi->connector->funcs->destroy(msm_dsi->connector);
276
277 msm_dsi->connector = NULL;
278
279 return ret;
280 }
281
282