1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2014 The Linux Foundation. All rights reserved. 4 * Copyright (C) 2013 Red Hat 5 * Author: Rob Clark <robdclark@gmail.com> 6 */ 7 8 #include <linux/gpio/consumer.h> 9 #include <linux/of_irq.h> 10 #include <linux/of_platform.h> 11 #include <linux/platform_device.h> 12 13 #include <drm/drm_bridge_connector.h> 14 #include <drm/drm_of.h> 15 #include <drm/display/drm_hdmi_state_helper.h> 16 17 #include "hdmi.h" 18 19 void msm_hdmi_set_mode(struct hdmi *hdmi, bool power_on) 20 { 21 uint32_t ctrl = 0; 22 unsigned long flags; 23 24 spin_lock_irqsave(&hdmi->reg_lock, flags); 25 if (power_on) { 26 ctrl |= HDMI_CTRL_ENABLE; 27 if (!hdmi->connector->display_info.is_hdmi) { 28 ctrl |= HDMI_CTRL_HDMI; 29 hdmi_write(hdmi, REG_HDMI_CTRL, ctrl); 30 ctrl &= ~HDMI_CTRL_HDMI; 31 } else { 32 ctrl |= HDMI_CTRL_HDMI; 33 } 34 } else { 35 ctrl = HDMI_CTRL_HDMI; 36 } 37 38 hdmi_write(hdmi, REG_HDMI_CTRL, ctrl); 39 spin_unlock_irqrestore(&hdmi->reg_lock, flags); 40 DBG("HDMI Core: %s, HDMI_CTRL=0x%08x", 41 power_on ? "Enable" : "Disable", ctrl); 42 } 43 44 static irqreturn_t msm_hdmi_irq(int irq, void *dev_id) 45 { 46 struct hdmi *hdmi = dev_id; 47 48 /* Process HPD: */ 49 msm_hdmi_hpd_irq(hdmi->bridge); 50 51 /* Process DDC: */ 52 msm_hdmi_i2c_irq(hdmi->i2c); 53 54 /* Process HDCP: */ 55 if (hdmi->hdcp_ctrl) 56 msm_hdmi_hdcp_irq(hdmi->hdcp_ctrl); 57 58 /* TODO audio.. */ 59 60 return IRQ_HANDLED; 61 } 62 63 static void msm_hdmi_destroy(struct hdmi *hdmi) 64 { 65 /* 66 * at this point, hpd has been disabled, 67 * after flush workq, it's safe to deinit hdcp 68 */ 69 if (hdmi->workq) 70 destroy_workqueue(hdmi->workq); 71 msm_hdmi_hdcp_destroy(hdmi); 72 73 if (hdmi->i2c) 74 msm_hdmi_i2c_destroy(hdmi->i2c); 75 } 76 77 static void msm_hdmi_put_phy(struct hdmi *hdmi) 78 { 79 if (hdmi->phy_dev) { 80 put_device(hdmi->phy_dev); 81 hdmi->phy = NULL; 82 hdmi->phy_dev = NULL; 83 } 84 } 85 86 static int msm_hdmi_get_phy(struct hdmi *hdmi) 87 { 88 struct platform_device *pdev = hdmi->pdev; 89 struct platform_device *phy_pdev; 90 struct device_node *phy_node; 91 92 phy_node = of_parse_phandle(pdev->dev.of_node, "phys", 0); 93 if (!phy_node) { 94 DRM_DEV_ERROR(&pdev->dev, "cannot find phy device\n"); 95 return -ENXIO; 96 } 97 98 phy_pdev = of_find_device_by_node(phy_node); 99 of_node_put(phy_node); 100 101 if (!phy_pdev) 102 return dev_err_probe(&pdev->dev, -EPROBE_DEFER, "phy driver is not ready\n"); 103 104 hdmi->phy = platform_get_drvdata(phy_pdev); 105 if (!hdmi->phy) { 106 put_device(&phy_pdev->dev); 107 return dev_err_probe(&pdev->dev, -EPROBE_DEFER, "phy driver is not ready\n"); 108 } 109 110 hdmi->phy_dev = &phy_pdev->dev; 111 112 return 0; 113 } 114 115 /* construct hdmi at bind/probe time, grab all the resources. If 116 * we are to EPROBE_DEFER we want to do it here, rather than later 117 * at modeset_init() time 118 */ 119 static int msm_hdmi_init(struct hdmi *hdmi) 120 { 121 struct platform_device *pdev = hdmi->pdev; 122 int ret; 123 124 hdmi->workq = alloc_ordered_workqueue("msm_hdmi", 0); 125 if (!hdmi->workq) { 126 ret = -ENOMEM; 127 goto fail; 128 } 129 130 hdmi->i2c = msm_hdmi_i2c_init(hdmi); 131 if (IS_ERR(hdmi->i2c)) { 132 ret = PTR_ERR(hdmi->i2c); 133 DRM_DEV_ERROR(&pdev->dev, "failed to get i2c: %d\n", ret); 134 hdmi->i2c = NULL; 135 goto fail; 136 } 137 138 hdmi->hdcp_ctrl = msm_hdmi_hdcp_init(hdmi); 139 if (IS_ERR(hdmi->hdcp_ctrl)) { 140 dev_warn(&pdev->dev, "failed to init hdcp: disabled\n"); 141 hdmi->hdcp_ctrl = NULL; 142 } 143 144 return 0; 145 146 fail: 147 msm_hdmi_destroy(hdmi); 148 149 return ret; 150 } 151 152 /* Second part of initialization, the drm/kms level modeset_init, 153 * constructs/initializes mode objects, etc, is called from master 154 * driver (not hdmi sub-device's probe/bind!) 155 * 156 * Any resource (regulator/clk/etc) which could be missing at boot 157 * should be handled in msm_hdmi_init() so that failure happens from 158 * hdmi sub-device's probe. 159 */ 160 int msm_hdmi_modeset_init(struct hdmi *hdmi, 161 struct drm_device *dev, struct drm_encoder *encoder) 162 { 163 int ret; 164 165 hdmi->dev = dev; 166 hdmi->encoder = encoder; 167 168 ret = msm_hdmi_bridge_init(hdmi); 169 if (ret) { 170 DRM_DEV_ERROR(dev->dev, "failed to create HDMI bridge: %d\n", ret); 171 goto fail; 172 } 173 174 if (hdmi->next_bridge) { 175 ret = drm_bridge_attach(hdmi->encoder, hdmi->next_bridge, hdmi->bridge, 176 DRM_BRIDGE_ATTACH_NO_CONNECTOR); 177 if (ret) { 178 DRM_DEV_ERROR(dev->dev, "failed to attach next HDMI bridge: %d\n", ret); 179 goto fail; 180 } 181 } 182 183 hdmi->connector = drm_bridge_connector_init(hdmi->dev, encoder); 184 if (IS_ERR(hdmi->connector)) { 185 ret = PTR_ERR(hdmi->connector); 186 DRM_DEV_ERROR(dev->dev, "failed to create HDMI connector: %d\n", ret); 187 hdmi->connector = NULL; 188 goto fail; 189 } 190 191 drm_connector_attach_encoder(hdmi->connector, hdmi->encoder); 192 193 ret = devm_request_irq(dev->dev, hdmi->irq, 194 msm_hdmi_irq, IRQF_TRIGGER_HIGH, 195 "hdmi_isr", hdmi); 196 if (ret < 0) { 197 DRM_DEV_ERROR(dev->dev, "failed to request IRQ%u: %d\n", 198 hdmi->irq, ret); 199 goto fail; 200 } 201 202 ret = msm_hdmi_hpd_enable(hdmi->bridge); 203 if (ret < 0) { 204 DRM_DEV_ERROR(&hdmi->pdev->dev, "failed to enable HPD: %d\n", ret); 205 goto fail; 206 } 207 208 return 0; 209 210 fail: 211 if (hdmi->connector) { 212 hdmi->connector->funcs->destroy(hdmi->connector); 213 hdmi->connector = NULL; 214 } 215 216 return ret; 217 } 218 219 /* 220 * The hdmi device: 221 */ 222 223 #define HDMI_CFG(item, entry) \ 224 .item ## _names = item ##_names_ ## entry, \ 225 .item ## _cnt = ARRAY_SIZE(item ## _names_ ## entry) 226 227 static const char *hpd_reg_names_8960[] = {"core-vdda"}; 228 static const char *hpd_clk_names_8960[] = {"core", "master_iface", "slave_iface"}; 229 230 static const struct hdmi_platform_config hdmi_tx_8960_config = { 231 HDMI_CFG(hpd_reg, 8960), 232 HDMI_CFG(hpd_clk, 8960), 233 }; 234 235 static const char *pwr_reg_names_8x74[] = {"core-vdda", "core-vcc"}; 236 static const char *pwr_clk_names_8x74[] = {"extp", "alt_iface"}; 237 static const char *hpd_clk_names_8x74[] = {"iface", "core", "mdp_core"}; 238 static unsigned long hpd_clk_freq_8x74[] = {0, 19200000, 0}; 239 240 static const struct hdmi_platform_config hdmi_tx_8974_config = { 241 HDMI_CFG(pwr_reg, 8x74), 242 HDMI_CFG(pwr_clk, 8x74), 243 HDMI_CFG(hpd_clk, 8x74), 244 .hpd_freq = hpd_clk_freq_8x74, 245 }; 246 247 static int msm_hdmi_bind(struct device *dev, struct device *master, void *data) 248 { 249 struct msm_drm_private *priv = dev_get_drvdata(master); 250 struct hdmi *hdmi = dev_get_drvdata(dev); 251 int err; 252 253 err = msm_hdmi_init(hdmi); 254 if (err) 255 return err; 256 priv->hdmi = hdmi; 257 258 return 0; 259 } 260 261 static void msm_hdmi_unbind(struct device *dev, struct device *master, 262 void *data) 263 { 264 struct msm_drm_private *priv = dev_get_drvdata(master); 265 266 if (priv->hdmi) { 267 if (priv->hdmi->bridge) 268 msm_hdmi_hpd_disable(priv->hdmi); 269 270 msm_hdmi_destroy(priv->hdmi); 271 priv->hdmi = NULL; 272 } 273 } 274 275 static const struct component_ops msm_hdmi_ops = { 276 .bind = msm_hdmi_bind, 277 .unbind = msm_hdmi_unbind, 278 }; 279 280 static int msm_hdmi_dev_probe(struct platform_device *pdev) 281 { 282 const struct hdmi_platform_config *config; 283 struct device *dev = &pdev->dev; 284 struct hdmi *hdmi; 285 struct resource *res; 286 int i, ret; 287 288 config = of_device_get_match_data(dev); 289 if (!config) 290 return -EINVAL; 291 292 hdmi = devm_kzalloc(&pdev->dev, sizeof(*hdmi), GFP_KERNEL); 293 if (!hdmi) 294 return -ENOMEM; 295 296 hdmi->pdev = pdev; 297 hdmi->config = config; 298 spin_lock_init(&hdmi->reg_lock); 299 300 ret = drm_of_find_panel_or_bridge(pdev->dev.of_node, 1, 0, NULL, &hdmi->next_bridge); 301 if (ret && ret != -ENODEV) 302 return ret; 303 304 hdmi->mmio = msm_ioremap(pdev, "core_physical"); 305 if (IS_ERR(hdmi->mmio)) 306 return PTR_ERR(hdmi->mmio); 307 308 /* HDCP needs physical address of hdmi register */ 309 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, 310 "core_physical"); 311 if (!res) 312 return -EINVAL; 313 hdmi->mmio_phy_addr = res->start; 314 315 hdmi->qfprom_mmio = msm_ioremap(pdev, "qfprom_physical"); 316 if (IS_ERR(hdmi->qfprom_mmio)) { 317 DRM_DEV_INFO(&pdev->dev, "can't find qfprom resource\n"); 318 hdmi->qfprom_mmio = NULL; 319 } 320 321 hdmi->irq = platform_get_irq(pdev, 0); 322 if (hdmi->irq < 0) 323 return hdmi->irq; 324 325 hdmi->hpd_regs = devm_kcalloc(&pdev->dev, 326 config->hpd_reg_cnt, 327 sizeof(hdmi->hpd_regs[0]), 328 GFP_KERNEL); 329 if (!hdmi->hpd_regs) 330 return -ENOMEM; 331 332 for (i = 0; i < config->hpd_reg_cnt; i++) 333 hdmi->hpd_regs[i].supply = config->hpd_reg_names[i]; 334 335 ret = devm_regulator_bulk_get(&pdev->dev, config->hpd_reg_cnt, hdmi->hpd_regs); 336 if (ret) 337 return dev_err_probe(dev, ret, "failed to get hpd regulators\n"); 338 339 hdmi->pwr_regs = devm_kcalloc(&pdev->dev, 340 config->pwr_reg_cnt, 341 sizeof(hdmi->pwr_regs[0]), 342 GFP_KERNEL); 343 if (!hdmi->pwr_regs) 344 return -ENOMEM; 345 346 for (i = 0; i < config->pwr_reg_cnt; i++) 347 hdmi->pwr_regs[i].supply = config->pwr_reg_names[i]; 348 349 ret = devm_regulator_bulk_get(&pdev->dev, config->pwr_reg_cnt, hdmi->pwr_regs); 350 if (ret) 351 return dev_err_probe(dev, ret, "failed to get pwr regulators\n"); 352 353 hdmi->hpd_clks = devm_kcalloc(&pdev->dev, 354 config->hpd_clk_cnt, 355 sizeof(hdmi->hpd_clks[0]), 356 GFP_KERNEL); 357 if (!hdmi->hpd_clks) 358 return -ENOMEM; 359 360 for (i = 0; i < config->hpd_clk_cnt; i++) { 361 struct clk *clk; 362 363 clk = msm_clk_get(pdev, config->hpd_clk_names[i]); 364 if (IS_ERR(clk)) 365 return dev_err_probe(dev, PTR_ERR(clk), 366 "failed to get hpd clk: %s\n", 367 config->hpd_clk_names[i]); 368 369 hdmi->hpd_clks[i] = clk; 370 } 371 372 hdmi->pwr_clks = devm_kcalloc(&pdev->dev, 373 config->pwr_clk_cnt, 374 sizeof(hdmi->pwr_clks[0]), 375 GFP_KERNEL); 376 if (!hdmi->pwr_clks) 377 return -ENOMEM; 378 379 for (i = 0; i < config->pwr_clk_cnt; i++) { 380 struct clk *clk; 381 382 clk = msm_clk_get(pdev, config->pwr_clk_names[i]); 383 if (IS_ERR(clk)) 384 return dev_err_probe(dev, PTR_ERR(clk), 385 "failed to get pwr clk: %s\n", 386 config->pwr_clk_names[i]); 387 388 hdmi->pwr_clks[i] = clk; 389 } 390 391 hdmi->hpd_gpiod = devm_gpiod_get_optional(&pdev->dev, "hpd", GPIOD_IN); 392 /* This will catch e.g. -EPROBE_DEFER */ 393 if (IS_ERR(hdmi->hpd_gpiod)) 394 return dev_err_probe(dev, PTR_ERR(hdmi->hpd_gpiod), 395 "failed to get hpd gpio\n"); 396 397 if (!hdmi->hpd_gpiod) 398 DBG("failed to get HPD gpio"); 399 400 if (hdmi->hpd_gpiod) 401 gpiod_set_consumer_name(hdmi->hpd_gpiod, "HDMI_HPD"); 402 403 ret = msm_hdmi_get_phy(hdmi); 404 if (ret) { 405 DRM_DEV_ERROR(&pdev->dev, "failed to get phy\n"); 406 return ret; 407 } 408 409 ret = devm_pm_runtime_enable(&pdev->dev); 410 if (ret) 411 goto err_put_phy; 412 413 platform_set_drvdata(pdev, hdmi); 414 415 ret = component_add(&pdev->dev, &msm_hdmi_ops); 416 if (ret) 417 goto err_put_phy; 418 419 return 0; 420 421 err_put_phy: 422 msm_hdmi_put_phy(hdmi); 423 return ret; 424 } 425 426 static void msm_hdmi_dev_remove(struct platform_device *pdev) 427 { 428 struct hdmi *hdmi = dev_get_drvdata(&pdev->dev); 429 430 component_del(&pdev->dev, &msm_hdmi_ops); 431 432 msm_hdmi_put_phy(hdmi); 433 } 434 435 static const struct of_device_id msm_hdmi_dt_match[] = { 436 { .compatible = "qcom,hdmi-tx-8998", .data = &hdmi_tx_8974_config }, 437 { .compatible = "qcom,hdmi-tx-8996", .data = &hdmi_tx_8974_config }, 438 { .compatible = "qcom,hdmi-tx-8994", .data = &hdmi_tx_8974_config }, 439 { .compatible = "qcom,hdmi-tx-8084", .data = &hdmi_tx_8974_config }, 440 { .compatible = "qcom,hdmi-tx-8974", .data = &hdmi_tx_8974_config }, 441 { .compatible = "qcom,hdmi-tx-8960", .data = &hdmi_tx_8960_config }, 442 { .compatible = "qcom,hdmi-tx-8660", .data = &hdmi_tx_8960_config }, 443 {} 444 }; 445 446 static struct platform_driver msm_hdmi_driver = { 447 .probe = msm_hdmi_dev_probe, 448 .remove = msm_hdmi_dev_remove, 449 .driver = { 450 .name = "hdmi_msm", 451 .of_match_table = msm_hdmi_dt_match, 452 }, 453 }; 454 455 void __init msm_hdmi_register(void) 456 { 457 msm_hdmi_phy_driver_register(); 458 platform_driver_register(&msm_hdmi_driver); 459 } 460 461 void __exit msm_hdmi_unregister(void) 462 { 463 platform_driver_unregister(&msm_hdmi_driver); 464 msm_hdmi_phy_driver_unregister(); 465 } 466