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