1a689554bSHai Li /* 2a689554bSHai Li * Copyright (c) 2015, The Linux Foundation. All rights reserved. 3a689554bSHai Li * 4a689554bSHai Li * This program is free software; you can redistribute it and/or modify 5a689554bSHai Li * it under the terms of the GNU General Public License version 2 and 6a689554bSHai Li * only version 2 as published by the Free Software Foundation. 7a689554bSHai Li * 8a689554bSHai Li * This program is distributed in the hope that it will be useful, 9a689554bSHai Li * but WITHOUT ANY WARRANTY; without even the implied warranty of 10a689554bSHai Li * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11a689554bSHai Li * GNU General Public License for more details. 12a689554bSHai Li */ 13a689554bSHai Li 14a689554bSHai Li #include "dsi.h" 15a689554bSHai Li 16a689554bSHai Li struct drm_encoder *msm_dsi_get_encoder(struct msm_dsi *msm_dsi) 17a689554bSHai Li { 18a689554bSHai Li if (!msm_dsi || !msm_dsi->panel) 19a689554bSHai Li return NULL; 20a689554bSHai Li 21a689554bSHai Li return (msm_dsi->panel_flags & MIPI_DSI_MODE_VIDEO) ? 22a689554bSHai Li msm_dsi->encoders[MSM_DSI_VIDEO_ENCODER_ID] : 23a689554bSHai Li msm_dsi->encoders[MSM_DSI_CMD_ENCODER_ID]; 24a689554bSHai Li } 25a689554bSHai Li 26a689554bSHai Li static void dsi_destroy(struct msm_dsi *msm_dsi) 27a689554bSHai Li { 28a689554bSHai Li if (!msm_dsi) 29a689554bSHai Li return; 30a689554bSHai Li 31a689554bSHai Li msm_dsi_manager_unregister(msm_dsi); 32a689554bSHai Li if (msm_dsi->host) { 33a689554bSHai Li msm_dsi_host_destroy(msm_dsi->host); 34a689554bSHai Li msm_dsi->host = NULL; 35a689554bSHai Li } 36a689554bSHai Li 37a689554bSHai Li platform_set_drvdata(msm_dsi->pdev, NULL); 38a689554bSHai Li } 39a689554bSHai Li 40a689554bSHai Li static struct msm_dsi *dsi_init(struct platform_device *pdev) 41a689554bSHai Li { 42a689554bSHai Li struct msm_dsi *msm_dsi = NULL; 43a689554bSHai Li int ret; 44a689554bSHai Li 45a689554bSHai Li if (!pdev) { 46a689554bSHai Li dev_err(&pdev->dev, "no dsi device\n"); 47a689554bSHai Li ret = -ENXIO; 48a689554bSHai Li goto fail; 49a689554bSHai Li } 50a689554bSHai Li 51a689554bSHai Li msm_dsi = devm_kzalloc(&pdev->dev, sizeof(*msm_dsi), GFP_KERNEL); 52a689554bSHai Li if (!msm_dsi) { 53a689554bSHai Li ret = -ENOMEM; 54a689554bSHai Li goto fail; 55a689554bSHai Li } 56a689554bSHai Li DBG("dsi probed=%p", msm_dsi); 57a689554bSHai Li 58a689554bSHai Li msm_dsi->pdev = pdev; 59a689554bSHai Li platform_set_drvdata(pdev, msm_dsi); 60a689554bSHai Li 61a689554bSHai Li /* Init dsi host */ 62a689554bSHai Li ret = msm_dsi_host_init(msm_dsi); 63a689554bSHai Li if (ret) 64a689554bSHai Li goto fail; 65a689554bSHai Li 66a689554bSHai Li /* Register to dsi manager */ 67a689554bSHai Li ret = msm_dsi_manager_register(msm_dsi); 68a689554bSHai Li if (ret) 69a689554bSHai Li goto fail; 70a689554bSHai Li 71a689554bSHai Li return msm_dsi; 72a689554bSHai Li 73a689554bSHai Li fail: 74a689554bSHai Li if (msm_dsi) 75a689554bSHai Li dsi_destroy(msm_dsi); 76a689554bSHai Li 77a689554bSHai Li return ERR_PTR(ret); 78a689554bSHai Li } 79a689554bSHai Li 80a689554bSHai Li static int dsi_bind(struct device *dev, struct device *master, void *data) 81a689554bSHai Li { 82a689554bSHai Li struct drm_device *drm = dev_get_drvdata(master); 83a689554bSHai Li struct msm_drm_private *priv = drm->dev_private; 84a689554bSHai Li struct platform_device *pdev = to_platform_device(dev); 85a689554bSHai Li struct msm_dsi *msm_dsi; 86a689554bSHai Li 87a689554bSHai Li DBG(""); 88a689554bSHai Li msm_dsi = dsi_init(pdev); 89a689554bSHai Li if (IS_ERR(msm_dsi)) 90a689554bSHai Li return PTR_ERR(msm_dsi); 91a689554bSHai Li 92a689554bSHai Li priv->dsi[msm_dsi->id] = msm_dsi; 93a689554bSHai Li 94a689554bSHai Li return 0; 95a689554bSHai Li } 96a689554bSHai Li 97a689554bSHai Li static void dsi_unbind(struct device *dev, struct device *master, 98a689554bSHai Li void *data) 99a689554bSHai Li { 100a689554bSHai Li struct drm_device *drm = dev_get_drvdata(master); 101a689554bSHai Li struct msm_drm_private *priv = drm->dev_private; 102a689554bSHai Li struct msm_dsi *msm_dsi = dev_get_drvdata(dev); 103a689554bSHai Li int id = msm_dsi->id; 104a689554bSHai Li 105a689554bSHai Li if (priv->dsi[id]) { 106a689554bSHai Li dsi_destroy(msm_dsi); 107a689554bSHai Li priv->dsi[id] = NULL; 108a689554bSHai Li } 109a689554bSHai Li } 110a689554bSHai Li 111a689554bSHai Li static const struct component_ops dsi_ops = { 112a689554bSHai Li .bind = dsi_bind, 113a689554bSHai Li .unbind = dsi_unbind, 114a689554bSHai Li }; 115a689554bSHai Li 116a689554bSHai Li static int dsi_dev_probe(struct platform_device *pdev) 117a689554bSHai Li { 118a689554bSHai Li return component_add(&pdev->dev, &dsi_ops); 119a689554bSHai Li } 120a689554bSHai Li 121a689554bSHai Li static int dsi_dev_remove(struct platform_device *pdev) 122a689554bSHai Li { 123a689554bSHai Li DBG(""); 124a689554bSHai Li component_del(&pdev->dev, &dsi_ops); 125a689554bSHai Li return 0; 126a689554bSHai Li } 127a689554bSHai Li 128a689554bSHai Li static const struct of_device_id dt_match[] = { 129a689554bSHai Li { .compatible = "qcom,mdss-dsi-ctrl" }, 130a689554bSHai Li {} 131a689554bSHai Li }; 132a689554bSHai Li 133a689554bSHai Li static struct platform_driver dsi_driver = { 134a689554bSHai Li .probe = dsi_dev_probe, 135a689554bSHai Li .remove = dsi_dev_remove, 136a689554bSHai Li .driver = { 137a689554bSHai Li .name = "msm_dsi", 138a689554bSHai Li .of_match_table = dt_match, 139a689554bSHai Li }, 140a689554bSHai Li }; 141a689554bSHai Li 142a689554bSHai Li void __init msm_dsi_register(void) 143a689554bSHai Li { 144a689554bSHai Li DBG(""); 145a689554bSHai Li platform_driver_register(&dsi_driver); 146a689554bSHai Li } 147a689554bSHai Li 148a689554bSHai Li void __exit msm_dsi_unregister(void) 149a689554bSHai Li { 150a689554bSHai Li DBG(""); 151a689554bSHai Li platform_driver_unregister(&dsi_driver); 152a689554bSHai Li } 153a689554bSHai Li 154a689554bSHai Li int msm_dsi_modeset_init(struct msm_dsi *msm_dsi, struct drm_device *dev, 155a689554bSHai Li struct drm_encoder *encoders[MSM_DSI_ENCODER_NUM]) 156a689554bSHai Li { 157a689554bSHai Li struct msm_drm_private *priv = dev->dev_private; 158a689554bSHai Li int ret, i; 159a689554bSHai Li 160a689554bSHai Li if (WARN_ON(!encoders[MSM_DSI_VIDEO_ENCODER_ID] || 161a689554bSHai Li !encoders[MSM_DSI_CMD_ENCODER_ID])) 162a689554bSHai Li return -EINVAL; 163a689554bSHai Li 164a689554bSHai Li msm_dsi->dev = dev; 165a689554bSHai Li 166a689554bSHai Li ret = msm_dsi_host_modeset_init(msm_dsi->host, dev); 167a689554bSHai Li if (ret) { 168a689554bSHai Li dev_err(dev->dev, "failed to modeset init host: %d\n", ret); 169a689554bSHai Li goto fail; 170a689554bSHai Li } 171a689554bSHai Li 172a689554bSHai Li msm_dsi->bridge = msm_dsi_manager_bridge_init(msm_dsi->id); 173a689554bSHai Li if (IS_ERR(msm_dsi->bridge)) { 174a689554bSHai Li ret = PTR_ERR(msm_dsi->bridge); 175a689554bSHai Li dev_err(dev->dev, "failed to create dsi bridge: %d\n", ret); 176a689554bSHai Li msm_dsi->bridge = NULL; 177a689554bSHai Li goto fail; 178a689554bSHai Li } 179a689554bSHai Li 180*6f6b2879SHai Li for (i = 0; i < MSM_DSI_ENCODER_NUM; i++) { 181*6f6b2879SHai Li encoders[i]->bridge = msm_dsi->bridge; 182*6f6b2879SHai Li msm_dsi->encoders[i] = encoders[i]; 183*6f6b2879SHai Li } 184*6f6b2879SHai Li 185a689554bSHai Li msm_dsi->connector = msm_dsi_manager_connector_init(msm_dsi->id); 186a689554bSHai Li if (IS_ERR(msm_dsi->connector)) { 187a689554bSHai Li ret = PTR_ERR(msm_dsi->connector); 188a689554bSHai Li dev_err(dev->dev, "failed to create dsi connector: %d\n", ret); 189a689554bSHai Li msm_dsi->connector = NULL; 190a689554bSHai Li goto fail; 191a689554bSHai Li } 192a689554bSHai Li 193a689554bSHai Li priv->bridges[priv->num_bridges++] = msm_dsi->bridge; 194a689554bSHai Li priv->connectors[priv->num_connectors++] = msm_dsi->connector; 195a689554bSHai Li 196a689554bSHai Li return 0; 197a689554bSHai Li fail: 198a689554bSHai Li if (msm_dsi) { 199a689554bSHai Li /* bridge/connector are normally destroyed by drm: */ 200a689554bSHai Li if (msm_dsi->bridge) { 201a689554bSHai Li msm_dsi_manager_bridge_destroy(msm_dsi->bridge); 202a689554bSHai Li msm_dsi->bridge = NULL; 203a689554bSHai Li } 204a689554bSHai Li if (msm_dsi->connector) { 205a689554bSHai Li msm_dsi->connector->funcs->destroy(msm_dsi->connector); 206a689554bSHai Li msm_dsi->connector = NULL; 207a689554bSHai Li } 208a689554bSHai Li } 209a689554bSHai Li 210a689554bSHai Li return ret; 211a689554bSHai Li } 212a689554bSHai Li 213