dsi.c 6.2 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4 5 6 7 8 9
/*
 * Copyright (c) 2015, The Linux Foundation. All rights reserved.
 */

#include "dsi.h"

struct drm_encoder *msm_dsi_get_encoder(struct msm_dsi *msm_dsi)
{
10
	if (!msm_dsi || !msm_dsi_device_connected(msm_dsi))
11 12
		return NULL;

13
	return msm_dsi->encoder;
14 15
}

16 17 18 19 20 21 22
bool msm_dsi_is_cmd_mode(struct msm_dsi *msm_dsi)
{
	unsigned long host_flags = msm_dsi_host_get_mode_flags(msm_dsi->host);

	return !(host_flags & MIPI_DSI_MODE_VIDEO);
}

23 24 25 26 27 28
static int dsi_get_phy(struct msm_dsi *msm_dsi)
{
	struct platform_device *pdev = msm_dsi->pdev;
	struct platform_device *phy_pdev;
	struct device_node *phy_node;

29
	phy_node = of_parse_phandle(pdev->dev.of_node, "phys", 0);
30
	if (!phy_node) {
31
		DRM_DEV_ERROR(&pdev->dev, "cannot find phy device\n");
32 33 34 35
		return -ENXIO;
	}

	phy_pdev = of_find_device_by_node(phy_node);
36
	if (phy_pdev) {
37
		msm_dsi->phy = platform_get_drvdata(phy_pdev);
38 39
		msm_dsi->phy_dev = &phy_pdev->dev;
	}
40 41 42 43

	of_node_put(phy_node);

	if (!phy_pdev || !msm_dsi->phy) {
44
		DRM_DEV_ERROR(&pdev->dev, "%s: phy driver is not ready\n", __func__);
45 46 47 48 49 50
		return -EPROBE_DEFER;
	}

	return 0;
}

51 52 53 54 55 56
static void dsi_destroy(struct msm_dsi *msm_dsi)
{
	if (!msm_dsi)
		return;

	msm_dsi_manager_unregister(msm_dsi);
57

58 59
	if (msm_dsi->phy_dev) {
		put_device(msm_dsi->phy_dev);
60
		msm_dsi->phy = NULL;
61
		msm_dsi->phy_dev = NULL;
62 63
	}

64 65 66 67 68 69 70 71 72 73
	if (msm_dsi->host) {
		msm_dsi_host_destroy(msm_dsi->host);
		msm_dsi->host = NULL;
	}

	platform_set_drvdata(msm_dsi->pdev, NULL);
}

static struct msm_dsi *dsi_init(struct platform_device *pdev)
{
74
	struct msm_dsi *msm_dsi;
75 76
	int ret;

77 78
	if (!pdev)
		return ERR_PTR(-ENXIO);
79 80

	msm_dsi = devm_kzalloc(&pdev->dev, sizeof(*msm_dsi), GFP_KERNEL);
81 82
	if (!msm_dsi)
		return ERR_PTR(-ENOMEM);
83 84
	DBG("dsi probed=%p", msm_dsi);

85
	msm_dsi->id = -1;
86 87 88 89 90 91
	msm_dsi->pdev = pdev;
	platform_set_drvdata(pdev, msm_dsi);

	/* Init dsi host */
	ret = msm_dsi_host_init(msm_dsi);
	if (ret)
92
		goto destroy_dsi;
93

94 95 96
	/* GET dsi PHY */
	ret = dsi_get_phy(msm_dsi);
	if (ret)
97
		goto destroy_dsi;
98

99 100 101
	/* Register to dsi manager */
	ret = msm_dsi_manager_register(msm_dsi);
	if (ret)
102
		goto destroy_dsi;
103 104 105

	return msm_dsi;

106
destroy_dsi:
107
	dsi_destroy(msm_dsi);
108 109 110 111 112 113 114 115 116 117 118 119
	return ERR_PTR(ret);
}

static int dsi_bind(struct device *dev, struct device *master, void *data)
{
	struct drm_device *drm = dev_get_drvdata(master);
	struct msm_drm_private *priv = drm->dev_private;
	struct platform_device *pdev = to_platform_device(dev);
	struct msm_dsi *msm_dsi;

	DBG("");
	msm_dsi = dsi_init(pdev);
120 121 122 123 124 125 126
	if (IS_ERR(msm_dsi)) {
		/* Don't fail the bind if the dsi port is not connected */
		if (PTR_ERR(msm_dsi) == -ENODEV)
			return 0;
		else
			return PTR_ERR(msm_dsi);
	}
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168

	priv->dsi[msm_dsi->id] = msm_dsi;

	return 0;
}

static void dsi_unbind(struct device *dev, struct device *master,
		void *data)
{
	struct drm_device *drm = dev_get_drvdata(master);
	struct msm_drm_private *priv = drm->dev_private;
	struct msm_dsi *msm_dsi = dev_get_drvdata(dev);
	int id = msm_dsi->id;

	if (priv->dsi[id]) {
		dsi_destroy(msm_dsi);
		priv->dsi[id] = NULL;
	}
}

static const struct component_ops dsi_ops = {
	.bind   = dsi_bind,
	.unbind = dsi_unbind,
};

static int dsi_dev_probe(struct platform_device *pdev)
{
	return component_add(&pdev->dev, &dsi_ops);
}

static int dsi_dev_remove(struct platform_device *pdev)
{
	DBG("");
	component_del(&pdev->dev, &dsi_ops);
	return 0;
}

static const struct of_device_id dt_match[] = {
	{ .compatible = "qcom,mdss-dsi-ctrl" },
	{}
};

169 170
static const struct dev_pm_ops dsi_pm_ops = {
	SET_RUNTIME_PM_OPS(msm_dsi_runtime_suspend, msm_dsi_runtime_resume, NULL)
171 172
	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
				pm_runtime_force_resume)
173 174
};

175 176 177 178 179 180
static struct platform_driver dsi_driver = {
	.probe = dsi_dev_probe,
	.remove = dsi_dev_remove,
	.driver = {
		.name = "msm_dsi",
		.of_match_table = dt_match,
181
		.pm = &dsi_pm_ops,
182 183 184 185 186 187
	},
};

void __init msm_dsi_register(void)
{
	DBG("");
188
	msm_dsi_phy_driver_register();
189 190 191 192 193 194
	platform_driver_register(&dsi_driver);
}

void __exit msm_dsi_unregister(void)
{
	DBG("");
195
	msm_dsi_phy_driver_unregister();
196 197 198 199
	platform_driver_unregister(&dsi_driver);
}

int msm_dsi_modeset_init(struct msm_dsi *msm_dsi, struct drm_device *dev,
200
			 struct drm_encoder *encoder)
201
{
202
	struct msm_drm_private *priv;
203
	struct drm_bridge *ext_bridge;
204
	int ret;
205

206
	if (WARN_ON(!encoder) || WARN_ON(!msm_dsi) || WARN_ON(!dev))
207 208
		return -EINVAL;

209
	priv = dev->dev_private;
210 211 212 213
	msm_dsi->dev = dev;

	ret = msm_dsi_host_modeset_init(msm_dsi->host, dev);
	if (ret) {
214
		DRM_DEV_ERROR(dev->dev, "failed to modeset init host: %d\n", ret);
215 216 217
		goto fail;
	}

218 219 220
	if (!msm_dsi_manager_validate_current_config(msm_dsi->id))
		goto fail;

221
	msm_dsi->encoder = encoder;
222

223 224 225
	msm_dsi->bridge = msm_dsi_manager_bridge_init(msm_dsi->id);
	if (IS_ERR(msm_dsi->bridge)) {
		ret = PTR_ERR(msm_dsi->bridge);
226
		DRM_DEV_ERROR(dev->dev, "failed to create dsi bridge: %d\n", ret);
227 228 229 230
		msm_dsi->bridge = NULL;
		goto fail;
	}

231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
	/*
	 * check if the dsi encoder output is connected to a panel or an
	 * external bridge. We create a connector only if we're connected to a
	 * drm_panel device. When we're connected to an external bridge, we
	 * assume that the drm_bridge driver will create the connector itself.
	 */
	ext_bridge = msm_dsi_host_get_bridge(msm_dsi->host);

	if (ext_bridge)
		msm_dsi->connector =
			msm_dsi_manager_ext_bridge_init(msm_dsi->id);
	else
		msm_dsi->connector =
			msm_dsi_manager_connector_init(msm_dsi->id);

246 247
	if (IS_ERR(msm_dsi->connector)) {
		ret = PTR_ERR(msm_dsi->connector);
248
		DRM_DEV_ERROR(dev->dev,
249
			"failed to create dsi connector: %d\n", ret);
250 251 252 253 254 255 256 257 258
		msm_dsi->connector = NULL;
		goto fail;
	}

	priv->bridges[priv->num_bridges++]       = msm_dsi->bridge;
	priv->connectors[priv->num_connectors++] = msm_dsi->connector;

	return 0;
fail:
259 260 261 262 263
	/* bridge/connector are normally destroyed by drm: */
	if (msm_dsi->bridge) {
		msm_dsi_manager_bridge_destroy(msm_dsi->bridge);
		msm_dsi->bridge = NULL;
	}
264

265 266 267
	/* don't destroy connector if we didn't make it */
	if (msm_dsi->connector && !msm_dsi->external_bridge)
		msm_dsi->connector->funcs->destroy(msm_dsi->connector);
268

269
	msm_dsi->connector = NULL;
270 271 272 273

	return ret;
}

274
void msm_dsi_snapshot(struct msm_disp_state *disp_state, struct msm_dsi *msm_dsi)
275
{
276
	msm_dsi_host_snapshot(disp_state, msm_dsi->host);
277
	msm_dsi_phy_snapshot(disp_state, msm_dsi->phy);
278
}
279