197fb5e8dSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2a689554bSHai Li /*
3a689554bSHai Li * Copyright (c) 2015, The Linux Foundation. All rights reserved.
4a689554bSHai Li */
5a689554bSHai Li
6a689554bSHai Li #include "dsi.h"
7a689554bSHai Li
msm_dsi_is_cmd_mode(struct msm_dsi * msm_dsi)8f518f6c1SDmitry Baryshkov bool msm_dsi_is_cmd_mode(struct msm_dsi *msm_dsi)
9f518f6c1SDmitry Baryshkov {
10f518f6c1SDmitry Baryshkov unsigned long host_flags = msm_dsi_host_get_mode_flags(msm_dsi->host);
11f518f6c1SDmitry Baryshkov
12f518f6c1SDmitry Baryshkov return !(host_flags & MIPI_DSI_MODE_VIDEO);
13f518f6c1SDmitry Baryshkov }
14f518f6c1SDmitry Baryshkov
msm_dsi_get_dsc_config(struct msm_dsi * msm_dsi)154b2b1b36SDmitry Baryshkov struct drm_dsc_config *msm_dsi_get_dsc_config(struct msm_dsi *msm_dsi)
160f40ba48SVinod Koul {
170f40ba48SVinod Koul return msm_dsi_host_get_dsc_config(msm_dsi->host);
180f40ba48SVinod Koul }
190f40ba48SVinod Koul
msm_dsi_wide_bus_enabled(struct msm_dsi * msm_dsi)2097f038dbSJessica Zhang bool msm_dsi_wide_bus_enabled(struct msm_dsi *msm_dsi)
2197f038dbSJessica Zhang {
22efcbd6f9SJessica Zhang return msm_dsi_host_is_wide_bus_enabled(msm_dsi->host);
2397f038dbSJessica Zhang }
2497f038dbSJessica Zhang
dsi_get_phy(struct msm_dsi * msm_dsi)25ec31abf6SHai Li static int dsi_get_phy(struct msm_dsi *msm_dsi)
26ec31abf6SHai Li {
27ec31abf6SHai Li struct platform_device *pdev = msm_dsi->pdev;
28ec31abf6SHai Li struct platform_device *phy_pdev;
29ec31abf6SHai Li struct device_node *phy_node;
30ec31abf6SHai Li
3169696ea0SArchit Taneja phy_node = of_parse_phandle(pdev->dev.of_node, "phys", 0);
32ec31abf6SHai Li if (!phy_node) {
336a41da17SMamta Shukla DRM_DEV_ERROR(&pdev->dev, "cannot find phy device\n");
34ec31abf6SHai Li return -ENXIO;
35ec31abf6SHai Li }
36ec31abf6SHai Li
37ec31abf6SHai Li phy_pdev = of_find_device_by_node(phy_node);
386977cc89SChristophe JAILLET if (phy_pdev) {
39ec31abf6SHai Li msm_dsi->phy = platform_get_drvdata(phy_pdev);
406977cc89SChristophe JAILLET msm_dsi->phy_dev = &phy_pdev->dev;
416977cc89SChristophe JAILLET }
42ec31abf6SHai Li
43ec31abf6SHai Li of_node_put(phy_node);
44ec31abf6SHai Li
45c04c3148SMiaoqian Lin if (!phy_pdev) {
46c04c3148SMiaoqian Lin DRM_DEV_ERROR(&pdev->dev, "%s: phy driver is not ready\n", __func__);
47c04c3148SMiaoqian Lin return -EPROBE_DEFER;
48c04c3148SMiaoqian Lin }
49c04c3148SMiaoqian Lin if (!msm_dsi->phy) {
50c04c3148SMiaoqian Lin put_device(&phy_pdev->dev);
516a41da17SMamta Shukla DRM_DEV_ERROR(&pdev->dev, "%s: phy driver is not ready\n", __func__);
52ec31abf6SHai Li return -EPROBE_DEFER;
53ec31abf6SHai Li }
54ec31abf6SHai Li
55ec31abf6SHai Li return 0;
56ec31abf6SHai Li }
57ec31abf6SHai Li
dsi_destroy(struct msm_dsi * msm_dsi)58a689554bSHai Li static void dsi_destroy(struct msm_dsi *msm_dsi)
59a689554bSHai Li {
60a689554bSHai Li if (!msm_dsi)
61a689554bSHai Li return;
62a689554bSHai Li
63a689554bSHai Li msm_dsi_manager_unregister(msm_dsi);
649d32c498SHai Li
65ec31abf6SHai Li if (msm_dsi->phy_dev) {
66ec31abf6SHai Li put_device(msm_dsi->phy_dev);
679d32c498SHai Li msm_dsi->phy = NULL;
68ec31abf6SHai Li msm_dsi->phy_dev = NULL;
699d32c498SHai Li }
709d32c498SHai Li
71a689554bSHai Li if (msm_dsi->host) {
72a689554bSHai Li msm_dsi_host_destroy(msm_dsi->host);
73a689554bSHai Li msm_dsi->host = NULL;
74a689554bSHai Li }
75a689554bSHai Li
76a689554bSHai Li platform_set_drvdata(msm_dsi->pdev, NULL);
77a689554bSHai Li }
78a689554bSHai Li
dsi_init(struct platform_device * pdev)79a689554bSHai Li static struct msm_dsi *dsi_init(struct platform_device *pdev)
80a689554bSHai Li {
81da882cd1SMarkus Elfring struct msm_dsi *msm_dsi;
82a689554bSHai Li int ret;
83a689554bSHai Li
84da882cd1SMarkus Elfring if (!pdev)
85da882cd1SMarkus Elfring return ERR_PTR(-ENXIO);
86a689554bSHai Li
87a689554bSHai Li msm_dsi = devm_kzalloc(&pdev->dev, sizeof(*msm_dsi), GFP_KERNEL);
88da882cd1SMarkus Elfring if (!msm_dsi)
89da882cd1SMarkus Elfring return ERR_PTR(-ENOMEM);
90a689554bSHai Li DBG("dsi probed=%p", msm_dsi);
91a689554bSHai Li
92aea24171SSean Paul msm_dsi->id = -1;
93a689554bSHai Li msm_dsi->pdev = pdev;
94a689554bSHai Li platform_set_drvdata(pdev, msm_dsi);
95a689554bSHai Li
96a689554bSHai Li /* Init dsi host */
97a689554bSHai Li ret = msm_dsi_host_init(msm_dsi);
98a689554bSHai Li if (ret)
99da882cd1SMarkus Elfring goto destroy_dsi;
100a689554bSHai Li
101ec31abf6SHai Li /* GET dsi PHY */
102ec31abf6SHai Li ret = dsi_get_phy(msm_dsi);
103ec31abf6SHai Li if (ret)
104da882cd1SMarkus Elfring goto destroy_dsi;
1059d32c498SHai Li
106a689554bSHai Li /* Register to dsi manager */
107a689554bSHai Li ret = msm_dsi_manager_register(msm_dsi);
108a689554bSHai Li if (ret)
109da882cd1SMarkus Elfring goto destroy_dsi;
110a689554bSHai Li
111a689554bSHai Li return msm_dsi;
112a689554bSHai Li
113da882cd1SMarkus Elfring destroy_dsi:
114a689554bSHai Li dsi_destroy(msm_dsi);
115a689554bSHai Li return ERR_PTR(ret);
116a689554bSHai Li }
117a689554bSHai Li
dsi_bind(struct device * dev,struct device * master,void * data)118a689554bSHai Li static int dsi_bind(struct device *dev, struct device *master, void *data)
119a689554bSHai Li {
120ec919e6eSAngeloGioacchino Del Regno struct msm_drm_private *priv = dev_get_drvdata(master);
1218f59ee9aSRob Clark struct msm_dsi *msm_dsi = dev_get_drvdata(dev);
122a689554bSHai Li
123c73588c0SDmitry Baryshkov /*
124c73588c0SDmitry Baryshkov * Next bridge doesn't exist for the secondary DSI host in a bonded
125c73588c0SDmitry Baryshkov * pair.
126c73588c0SDmitry Baryshkov */
127c73588c0SDmitry Baryshkov if (!msm_dsi_is_bonded_dsi(msm_dsi) ||
128c73588c0SDmitry Baryshkov msm_dsi_is_master_dsi(msm_dsi)) {
129c73588c0SDmitry Baryshkov struct drm_bridge *ext_bridge;
130c73588c0SDmitry Baryshkov
131c73588c0SDmitry Baryshkov ext_bridge = devm_drm_of_get_bridge(&msm_dsi->pdev->dev,
132c73588c0SDmitry Baryshkov msm_dsi->pdev->dev.of_node, 1, 0);
133c73588c0SDmitry Baryshkov if (IS_ERR(ext_bridge))
134c73588c0SDmitry Baryshkov return PTR_ERR(ext_bridge);
135c73588c0SDmitry Baryshkov
136c73588c0SDmitry Baryshkov msm_dsi->next_bridge = ext_bridge;
137c73588c0SDmitry Baryshkov }
138c73588c0SDmitry Baryshkov
139a689554bSHai Li priv->dsi[msm_dsi->id] = msm_dsi;
140a689554bSHai Li
141a689554bSHai Li return 0;
142a689554bSHai Li }
143a689554bSHai Li
dsi_unbind(struct device * dev,struct device * master,void * data)144a689554bSHai Li static void dsi_unbind(struct device *dev, struct device *master,
145a689554bSHai Li void *data)
146a689554bSHai Li {
147ec919e6eSAngeloGioacchino Del Regno struct msm_drm_private *priv = dev_get_drvdata(master);
148a689554bSHai Li struct msm_dsi *msm_dsi = dev_get_drvdata(dev);
149a689554bSHai Li
1505e05be78SDmitry Baryshkov msm_dsi_tx_buf_free(msm_dsi->host);
1518f59ee9aSRob Clark priv->dsi[msm_dsi->id] = NULL;
152a689554bSHai Li }
153a689554bSHai Li
154a689554bSHai Li static const struct component_ops dsi_ops = {
155a689554bSHai Li .bind = dsi_bind,
156a689554bSHai Li .unbind = dsi_unbind,
157a689554bSHai Li };
158a689554bSHai Li
dsi_dev_attach(struct platform_device * pdev)1598f59ee9aSRob Clark int dsi_dev_attach(struct platform_device *pdev)
160a689554bSHai Li {
161a689554bSHai Li return component_add(&pdev->dev, &dsi_ops);
162a689554bSHai Li }
163a689554bSHai Li
dsi_dev_detach(struct platform_device * pdev)1648f59ee9aSRob Clark void dsi_dev_detach(struct platform_device *pdev)
1658f59ee9aSRob Clark {
1668f59ee9aSRob Clark component_del(&pdev->dev, &dsi_ops);
1678f59ee9aSRob Clark }
1688f59ee9aSRob Clark
dsi_dev_probe(struct platform_device * pdev)1698f59ee9aSRob Clark static int dsi_dev_probe(struct platform_device *pdev)
1708f59ee9aSRob Clark {
1718f59ee9aSRob Clark struct msm_dsi *msm_dsi;
1728f59ee9aSRob Clark
1738f59ee9aSRob Clark DBG("");
1748f59ee9aSRob Clark msm_dsi = dsi_init(pdev);
1758f59ee9aSRob Clark if (IS_ERR(msm_dsi)) {
1768f59ee9aSRob Clark /* Don't fail the bind if the dsi port is not connected */
1778f59ee9aSRob Clark if (PTR_ERR(msm_dsi) == -ENODEV)
1788f59ee9aSRob Clark return 0;
1798f59ee9aSRob Clark else
1808f59ee9aSRob Clark return PTR_ERR(msm_dsi);
1818f59ee9aSRob Clark }
1828f59ee9aSRob Clark
1838f59ee9aSRob Clark return 0;
1848f59ee9aSRob Clark }
1858f59ee9aSRob Clark
dsi_dev_remove(struct platform_device * pdev)18601790d5eSUwe Kleine-König static void dsi_dev_remove(struct platform_device *pdev)
187a689554bSHai Li {
1888f59ee9aSRob Clark struct msm_dsi *msm_dsi = platform_get_drvdata(pdev);
1898f59ee9aSRob Clark
190a689554bSHai Li DBG("");
1918f59ee9aSRob Clark dsi_destroy(msm_dsi);
192a689554bSHai Li }
193a689554bSHai Li
194a689554bSHai Li static const struct of_device_id dt_match[] = {
19550da84c0SKonrad Dybcio { .compatible = "qcom,mdss-dsi-ctrl" },
196da9887adSKonrad Dybcio
197da9887adSKonrad Dybcio /* Deprecated, don't use */
19850da84c0SKonrad Dybcio { .compatible = "qcom,dsi-ctrl-6g-qcm2290" },
199a689554bSHai Li {}
200a689554bSHai Li };
201a689554bSHai Li
202f54ca1a0SArchit Taneja static const struct dev_pm_ops dsi_pm_ops = {
203f54ca1a0SArchit Taneja SET_RUNTIME_PM_OPS(msm_dsi_runtime_suspend, msm_dsi_runtime_resume, NULL)
204ca8199f1SKalyan Thota SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
205ca8199f1SKalyan Thota pm_runtime_force_resume)
206f54ca1a0SArchit Taneja };
207f54ca1a0SArchit Taneja
208a689554bSHai Li static struct platform_driver dsi_driver = {
209a689554bSHai Li .probe = dsi_dev_probe,
21001790d5eSUwe Kleine-König .remove_new = dsi_dev_remove,
211a689554bSHai Li .driver = {
212a689554bSHai Li .name = "msm_dsi",
213a689554bSHai Li .of_match_table = dt_match,
214f54ca1a0SArchit Taneja .pm = &dsi_pm_ops,
215a689554bSHai Li },
216a689554bSHai Li };
217a689554bSHai Li
msm_dsi_register(void)218a689554bSHai Li void __init msm_dsi_register(void)
219a689554bSHai Li {
220a689554bSHai Li DBG("");
221ec31abf6SHai Li msm_dsi_phy_driver_register();
222a689554bSHai Li platform_driver_register(&dsi_driver);
223a689554bSHai Li }
224a689554bSHai Li
msm_dsi_unregister(void)225a689554bSHai Li void __exit msm_dsi_unregister(void)
226a689554bSHai Li {
227a689554bSHai Li DBG("");
228ec31abf6SHai Li msm_dsi_phy_driver_unregister();
229a689554bSHai Li platform_driver_unregister(&dsi_driver);
230a689554bSHai Li }
231a689554bSHai Li
msm_dsi_modeset_init(struct msm_dsi * msm_dsi,struct drm_device * dev,struct drm_encoder * encoder)232a689554bSHai Li int msm_dsi_modeset_init(struct msm_dsi *msm_dsi, struct drm_device *dev,
23397e00119SArchit Taneja struct drm_encoder *encoder)
234a689554bSHai Li {
23597e00119SArchit Taneja int ret;
236a689554bSHai Li
237a689554bSHai Li msm_dsi->dev = dev;
238a689554bSHai Li
239a689554bSHai Li ret = msm_dsi_host_modeset_init(msm_dsi->host, dev);
240a689554bSHai Li if (ret) {
2416a41da17SMamta Shukla DRM_DEV_ERROR(dev->dev, "failed to modeset init host: %d\n", ret);
2425f403fd7SDmitry Baryshkov return ret;
243a689554bSHai Li }
244a689554bSHai Li
24592cb1bedSDmitry Baryshkov if (msm_dsi_is_bonded_dsi(msm_dsi) &&
24692cb1bedSDmitry Baryshkov !msm_dsi_is_master_dsi(msm_dsi)) {
24792cb1bedSDmitry Baryshkov /*
24892cb1bedSDmitry Baryshkov * Do not return an eror here,
24992cb1bedSDmitry Baryshkov * Just skip creating encoder/connector for the slave-DSI.
25092cb1bedSDmitry Baryshkov */
25192cb1bedSDmitry Baryshkov return 0;
252739b4e77SDan Carpenter }
2538b03ad30SChandan Uddaraju
254*87d322e9SDmitry Baryshkov ret = msm_dsi_manager_connector_init(msm_dsi, encoder);
255007ac026SDmitry Baryshkov if (ret) {
2566a41da17SMamta Shukla DRM_DEV_ERROR(dev->dev,
257c118e290SArchit Taneja "failed to create dsi connector: %d\n", ret);
2585f403fd7SDmitry Baryshkov return ret;
259a689554bSHai Li }
260a689554bSHai Li
261a689554bSHai Li return 0;
262a689554bSHai Li }
263a689554bSHai Li
msm_dsi_snapshot(struct msm_disp_state * disp_state,struct msm_dsi * msm_dsi)264eb9d6c7eSDmitry Baryshkov void msm_dsi_snapshot(struct msm_disp_state *disp_state, struct msm_dsi *msm_dsi)
2659d30a4bcSAbhinav Kumar {
266eb9d6c7eSDmitry Baryshkov msm_dsi_host_snapshot(disp_state, msm_dsi->host);
267d91940e2SDmitry Baryshkov msm_dsi_phy_snapshot(disp_state, msm_dsi->phy);
2689d30a4bcSAbhinav Kumar }
26998659487SAbhinav Kumar
270