0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/of_irq.h>
0009 #include <linux/of_gpio.h>
0010
0011 #include <drm/drm_bridge_connector.h>
0012 #include <drm/drm_of.h>
0013
0014 #include <sound/hdmi-codec.h>
0015 #include "hdmi.h"
0016
0017 void msm_hdmi_set_mode(struct hdmi *hdmi, bool power_on)
0018 {
0019 uint32_t ctrl = 0;
0020 unsigned long flags;
0021
0022 spin_lock_irqsave(&hdmi->reg_lock, flags);
0023 if (power_on) {
0024 ctrl |= HDMI_CTRL_ENABLE;
0025 if (!hdmi->hdmi_mode) {
0026 ctrl |= HDMI_CTRL_HDMI;
0027 hdmi_write(hdmi, REG_HDMI_CTRL, ctrl);
0028 ctrl &= ~HDMI_CTRL_HDMI;
0029 } else {
0030 ctrl |= HDMI_CTRL_HDMI;
0031 }
0032 } else {
0033 ctrl = HDMI_CTRL_HDMI;
0034 }
0035
0036 hdmi_write(hdmi, REG_HDMI_CTRL, ctrl);
0037 spin_unlock_irqrestore(&hdmi->reg_lock, flags);
0038 DBG("HDMI Core: %s, HDMI_CTRL=0x%08x",
0039 power_on ? "Enable" : "Disable", ctrl);
0040 }
0041
0042 static irqreturn_t msm_hdmi_irq(int irq, void *dev_id)
0043 {
0044 struct hdmi *hdmi = dev_id;
0045
0046
0047 msm_hdmi_hpd_irq(hdmi->bridge);
0048
0049
0050 msm_hdmi_i2c_irq(hdmi->i2c);
0051
0052
0053 if (hdmi->hdcp_ctrl)
0054 msm_hdmi_hdcp_irq(hdmi->hdcp_ctrl);
0055
0056
0057
0058 return IRQ_HANDLED;
0059 }
0060
0061 static void msm_hdmi_destroy(struct hdmi *hdmi)
0062 {
0063
0064
0065
0066
0067 if (hdmi->workq)
0068 destroy_workqueue(hdmi->workq);
0069 msm_hdmi_hdcp_destroy(hdmi);
0070
0071 if (hdmi->phy_dev) {
0072 put_device(hdmi->phy_dev);
0073 hdmi->phy = NULL;
0074 hdmi->phy_dev = NULL;
0075 }
0076
0077 if (hdmi->i2c)
0078 msm_hdmi_i2c_destroy(hdmi->i2c);
0079
0080 platform_set_drvdata(hdmi->pdev, NULL);
0081 }
0082
0083 static int msm_hdmi_get_phy(struct hdmi *hdmi)
0084 {
0085 struct platform_device *pdev = hdmi->pdev;
0086 struct platform_device *phy_pdev;
0087 struct device_node *phy_node;
0088
0089 phy_node = of_parse_phandle(pdev->dev.of_node, "phys", 0);
0090 if (!phy_node) {
0091 DRM_DEV_ERROR(&pdev->dev, "cannot find phy device\n");
0092 return -ENXIO;
0093 }
0094
0095 phy_pdev = of_find_device_by_node(phy_node);
0096 if (phy_pdev)
0097 hdmi->phy = platform_get_drvdata(phy_pdev);
0098
0099 of_node_put(phy_node);
0100
0101 if (!phy_pdev) {
0102 DRM_DEV_ERROR(&pdev->dev, "phy driver is not ready\n");
0103 return -EPROBE_DEFER;
0104 }
0105 if (!hdmi->phy) {
0106 DRM_DEV_ERROR(&pdev->dev, "phy driver is not ready\n");
0107 put_device(&phy_pdev->dev);
0108 return -EPROBE_DEFER;
0109 }
0110
0111 hdmi->phy_dev = get_device(&phy_pdev->dev);
0112
0113 return 0;
0114 }
0115
0116
0117
0118
0119
0120 static struct hdmi *msm_hdmi_init(struct platform_device *pdev)
0121 {
0122 struct hdmi_platform_config *config = pdev->dev.platform_data;
0123 struct hdmi *hdmi = NULL;
0124 struct resource *res;
0125 int i, ret;
0126
0127 hdmi = devm_kzalloc(&pdev->dev, sizeof(*hdmi), GFP_KERNEL);
0128 if (!hdmi) {
0129 ret = -ENOMEM;
0130 goto fail;
0131 }
0132
0133 hdmi->pdev = pdev;
0134 hdmi->config = config;
0135 spin_lock_init(&hdmi->reg_lock);
0136
0137 ret = drm_of_find_panel_or_bridge(pdev->dev.of_node, 1, 0, NULL, &hdmi->next_bridge);
0138 if (ret && ret != -ENODEV)
0139 goto fail;
0140
0141 hdmi->mmio = msm_ioremap(pdev, config->mmio_name);
0142 if (IS_ERR(hdmi->mmio)) {
0143 ret = PTR_ERR(hdmi->mmio);
0144 goto fail;
0145 }
0146
0147
0148 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
0149 config->mmio_name);
0150 if (!res) {
0151 ret = -EINVAL;
0152 goto fail;
0153 }
0154 hdmi->mmio_phy_addr = res->start;
0155
0156 hdmi->qfprom_mmio = msm_ioremap(pdev, config->qfprom_mmio_name);
0157 if (IS_ERR(hdmi->qfprom_mmio)) {
0158 DRM_DEV_INFO(&pdev->dev, "can't find qfprom resource\n");
0159 hdmi->qfprom_mmio = NULL;
0160 }
0161
0162 hdmi->hpd_regs = devm_kcalloc(&pdev->dev,
0163 config->hpd_reg_cnt,
0164 sizeof(hdmi->hpd_regs[0]),
0165 GFP_KERNEL);
0166 if (!hdmi->hpd_regs) {
0167 ret = -ENOMEM;
0168 goto fail;
0169 }
0170 for (i = 0; i < config->hpd_reg_cnt; i++)
0171 hdmi->hpd_regs[i].supply = config->hpd_reg_names[i];
0172
0173 ret = devm_regulator_bulk_get(&pdev->dev, config->hpd_reg_cnt, hdmi->hpd_regs);
0174 if (ret) {
0175 DRM_DEV_ERROR(&pdev->dev, "failed to get hpd regulator: %d\n", ret);
0176 goto fail;
0177 }
0178
0179 hdmi->pwr_regs = devm_kcalloc(&pdev->dev,
0180 config->pwr_reg_cnt,
0181 sizeof(hdmi->pwr_regs[0]),
0182 GFP_KERNEL);
0183 if (!hdmi->pwr_regs) {
0184 ret = -ENOMEM;
0185 goto fail;
0186 }
0187
0188 for (i = 0; i < config->pwr_reg_cnt; i++)
0189 hdmi->pwr_regs[i].supply = config->pwr_reg_names[i];
0190
0191 ret = devm_regulator_bulk_get(&pdev->dev, config->pwr_reg_cnt, hdmi->pwr_regs);
0192 if (ret) {
0193 DRM_DEV_ERROR(&pdev->dev, "failed to get pwr regulator: %d\n", ret);
0194 goto fail;
0195 }
0196
0197 hdmi->hpd_clks = devm_kcalloc(&pdev->dev,
0198 config->hpd_clk_cnt,
0199 sizeof(hdmi->hpd_clks[0]),
0200 GFP_KERNEL);
0201 if (!hdmi->hpd_clks) {
0202 ret = -ENOMEM;
0203 goto fail;
0204 }
0205 for (i = 0; i < config->hpd_clk_cnt; i++) {
0206 struct clk *clk;
0207
0208 clk = msm_clk_get(pdev, config->hpd_clk_names[i]);
0209 if (IS_ERR(clk)) {
0210 ret = PTR_ERR(clk);
0211 DRM_DEV_ERROR(&pdev->dev, "failed to get hpd clk: %s (%d)\n",
0212 config->hpd_clk_names[i], ret);
0213 goto fail;
0214 }
0215
0216 hdmi->hpd_clks[i] = clk;
0217 }
0218
0219 hdmi->pwr_clks = devm_kcalloc(&pdev->dev,
0220 config->pwr_clk_cnt,
0221 sizeof(hdmi->pwr_clks[0]),
0222 GFP_KERNEL);
0223 if (!hdmi->pwr_clks) {
0224 ret = -ENOMEM;
0225 goto fail;
0226 }
0227 for (i = 0; i < config->pwr_clk_cnt; i++) {
0228 struct clk *clk;
0229
0230 clk = msm_clk_get(pdev, config->pwr_clk_names[i]);
0231 if (IS_ERR(clk)) {
0232 ret = PTR_ERR(clk);
0233 DRM_DEV_ERROR(&pdev->dev, "failed to get pwr clk: %s (%d)\n",
0234 config->pwr_clk_names[i], ret);
0235 goto fail;
0236 }
0237
0238 hdmi->pwr_clks[i] = clk;
0239 }
0240
0241 hdmi->hpd_gpiod = devm_gpiod_get_optional(&pdev->dev, "hpd", GPIOD_IN);
0242
0243 if (IS_ERR(hdmi->hpd_gpiod)) {
0244 ret = PTR_ERR(hdmi->hpd_gpiod);
0245 DRM_DEV_ERROR(&pdev->dev, "failed to get hpd gpio: (%d)\n", ret);
0246 goto fail;
0247 }
0248
0249 if (!hdmi->hpd_gpiod)
0250 DBG("failed to get HPD gpio");
0251
0252 if (hdmi->hpd_gpiod)
0253 gpiod_set_consumer_name(hdmi->hpd_gpiod, "HDMI_HPD");
0254
0255 pm_runtime_enable(&pdev->dev);
0256
0257 hdmi->workq = alloc_ordered_workqueue("msm_hdmi", 0);
0258
0259 hdmi->i2c = msm_hdmi_i2c_init(hdmi);
0260 if (IS_ERR(hdmi->i2c)) {
0261 ret = PTR_ERR(hdmi->i2c);
0262 DRM_DEV_ERROR(&pdev->dev, "failed to get i2c: %d\n", ret);
0263 hdmi->i2c = NULL;
0264 goto fail;
0265 }
0266
0267 ret = msm_hdmi_get_phy(hdmi);
0268 if (ret) {
0269 DRM_DEV_ERROR(&pdev->dev, "failed to get phy\n");
0270 goto fail;
0271 }
0272
0273 hdmi->hdcp_ctrl = msm_hdmi_hdcp_init(hdmi);
0274 if (IS_ERR(hdmi->hdcp_ctrl)) {
0275 dev_warn(&pdev->dev, "failed to init hdcp: disabled\n");
0276 hdmi->hdcp_ctrl = NULL;
0277 }
0278
0279 return hdmi;
0280
0281 fail:
0282 if (hdmi)
0283 msm_hdmi_destroy(hdmi);
0284
0285 return ERR_PTR(ret);
0286 }
0287
0288
0289
0290
0291
0292
0293
0294
0295
0296 int msm_hdmi_modeset_init(struct hdmi *hdmi,
0297 struct drm_device *dev, struct drm_encoder *encoder)
0298 {
0299 struct msm_drm_private *priv = dev->dev_private;
0300 struct platform_device *pdev = hdmi->pdev;
0301 int ret;
0302
0303 hdmi->dev = dev;
0304 hdmi->encoder = encoder;
0305
0306 hdmi_audio_infoframe_init(&hdmi->audio.infoframe);
0307
0308 hdmi->bridge = msm_hdmi_bridge_init(hdmi);
0309 if (IS_ERR(hdmi->bridge)) {
0310 ret = PTR_ERR(hdmi->bridge);
0311 DRM_DEV_ERROR(dev->dev, "failed to create HDMI bridge: %d\n", ret);
0312 hdmi->bridge = NULL;
0313 goto fail;
0314 }
0315
0316 if (hdmi->next_bridge) {
0317 ret = drm_bridge_attach(hdmi->encoder, hdmi->next_bridge, hdmi->bridge,
0318 DRM_BRIDGE_ATTACH_NO_CONNECTOR);
0319 if (ret) {
0320 DRM_DEV_ERROR(dev->dev, "failed to attach next HDMI bridge: %d\n", ret);
0321 goto fail;
0322 }
0323 }
0324
0325 hdmi->connector = drm_bridge_connector_init(hdmi->dev, encoder);
0326 if (IS_ERR(hdmi->connector)) {
0327 ret = PTR_ERR(hdmi->connector);
0328 DRM_DEV_ERROR(dev->dev, "failed to create HDMI connector: %d\n", ret);
0329 hdmi->connector = NULL;
0330 goto fail;
0331 }
0332
0333 drm_connector_attach_encoder(hdmi->connector, hdmi->encoder);
0334
0335 hdmi->irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
0336 if (!hdmi->irq) {
0337 ret = -EINVAL;
0338 DRM_DEV_ERROR(dev->dev, "failed to get irq\n");
0339 goto fail;
0340 }
0341
0342 ret = devm_request_irq(&pdev->dev, hdmi->irq,
0343 msm_hdmi_irq, IRQF_TRIGGER_HIGH,
0344 "hdmi_isr", hdmi);
0345 if (ret < 0) {
0346 DRM_DEV_ERROR(dev->dev, "failed to request IRQ%u: %d\n",
0347 hdmi->irq, ret);
0348 goto fail;
0349 }
0350
0351 drm_bridge_connector_enable_hpd(hdmi->connector);
0352
0353 ret = msm_hdmi_hpd_enable(hdmi->bridge);
0354 if (ret < 0) {
0355 DRM_DEV_ERROR(&hdmi->pdev->dev, "failed to enable HPD: %d\n", ret);
0356 goto fail;
0357 }
0358
0359 priv->bridges[priv->num_bridges++] = hdmi->bridge;
0360
0361 platform_set_drvdata(pdev, hdmi);
0362
0363 return 0;
0364
0365 fail:
0366
0367 if (hdmi->bridge) {
0368 msm_hdmi_bridge_destroy(hdmi->bridge);
0369 hdmi->bridge = NULL;
0370 }
0371 if (hdmi->connector) {
0372 hdmi->connector->funcs->destroy(hdmi->connector);
0373 hdmi->connector = NULL;
0374 }
0375
0376 return ret;
0377 }
0378
0379
0380
0381
0382
0383 #define HDMI_CFG(item, entry) \
0384 .item ## _names = item ##_names_ ## entry, \
0385 .item ## _cnt = ARRAY_SIZE(item ## _names_ ## entry)
0386
0387 static const char *hpd_reg_names_8960[] = {"core-vdda"};
0388 static const char *hpd_clk_names_8960[] = {"core", "master_iface", "slave_iface"};
0389
0390 static struct hdmi_platform_config hdmi_tx_8960_config = {
0391 HDMI_CFG(hpd_reg, 8960),
0392 HDMI_CFG(hpd_clk, 8960),
0393 };
0394
0395 static const char *pwr_reg_names_8x74[] = {"core-vdda", "core-vcc"};
0396 static const char *pwr_clk_names_8x74[] = {"extp", "alt_iface"};
0397 static const char *hpd_clk_names_8x74[] = {"iface", "core", "mdp_core"};
0398 static unsigned long hpd_clk_freq_8x74[] = {0, 19200000, 0};
0399
0400 static struct hdmi_platform_config hdmi_tx_8974_config = {
0401 HDMI_CFG(pwr_reg, 8x74),
0402 HDMI_CFG(pwr_clk, 8x74),
0403 HDMI_CFG(hpd_clk, 8x74),
0404 .hpd_freq = hpd_clk_freq_8x74,
0405 };
0406
0407
0408
0409
0410 static int msm_hdmi_audio_hw_params(struct device *dev, void *data,
0411 struct hdmi_codec_daifmt *daifmt,
0412 struct hdmi_codec_params *params)
0413 {
0414 struct hdmi *hdmi = dev_get_drvdata(dev);
0415 unsigned int chan;
0416 unsigned int channel_allocation = 0;
0417 unsigned int rate;
0418 unsigned int level_shift = 0;
0419 bool down_mix = false;
0420
0421 DRM_DEV_DEBUG(dev, "%u Hz, %d bit, %d channels\n", params->sample_rate,
0422 params->sample_width, params->cea.channels);
0423
0424 switch (params->cea.channels) {
0425 case 2:
0426
0427 channel_allocation = 0;
0428 chan = MSM_HDMI_AUDIO_CHANNEL_2;
0429 break;
0430 case 4:
0431
0432 channel_allocation = 0x3;
0433 chan = MSM_HDMI_AUDIO_CHANNEL_4;
0434 break;
0435 case 6:
0436
0437 channel_allocation = 0x0B;
0438 chan = MSM_HDMI_AUDIO_CHANNEL_6;
0439 break;
0440 case 8:
0441
0442 channel_allocation = 0x1F;
0443 chan = MSM_HDMI_AUDIO_CHANNEL_8;
0444 break;
0445 default:
0446 return -EINVAL;
0447 }
0448
0449 switch (params->sample_rate) {
0450 case 32000:
0451 rate = HDMI_SAMPLE_RATE_32KHZ;
0452 break;
0453 case 44100:
0454 rate = HDMI_SAMPLE_RATE_44_1KHZ;
0455 break;
0456 case 48000:
0457 rate = HDMI_SAMPLE_RATE_48KHZ;
0458 break;
0459 case 88200:
0460 rate = HDMI_SAMPLE_RATE_88_2KHZ;
0461 break;
0462 case 96000:
0463 rate = HDMI_SAMPLE_RATE_96KHZ;
0464 break;
0465 case 176400:
0466 rate = HDMI_SAMPLE_RATE_176_4KHZ;
0467 break;
0468 case 192000:
0469 rate = HDMI_SAMPLE_RATE_192KHZ;
0470 break;
0471 default:
0472 DRM_DEV_ERROR(dev, "rate[%d] not supported!\n",
0473 params->sample_rate);
0474 return -EINVAL;
0475 }
0476
0477 msm_hdmi_audio_set_sample_rate(hdmi, rate);
0478 msm_hdmi_audio_info_setup(hdmi, 1, chan, channel_allocation,
0479 level_shift, down_mix);
0480
0481 return 0;
0482 }
0483
0484 static void msm_hdmi_audio_shutdown(struct device *dev, void *data)
0485 {
0486 struct hdmi *hdmi = dev_get_drvdata(dev);
0487
0488 msm_hdmi_audio_info_setup(hdmi, 0, 0, 0, 0, 0);
0489 }
0490
0491 static const struct hdmi_codec_ops msm_hdmi_audio_codec_ops = {
0492 .hw_params = msm_hdmi_audio_hw_params,
0493 .audio_shutdown = msm_hdmi_audio_shutdown,
0494 };
0495
0496 static struct hdmi_codec_pdata codec_data = {
0497 .ops = &msm_hdmi_audio_codec_ops,
0498 .max_i2s_channels = 8,
0499 .i2s = 1,
0500 };
0501
0502 static int msm_hdmi_register_audio_driver(struct hdmi *hdmi, struct device *dev)
0503 {
0504 hdmi->audio_pdev = platform_device_register_data(dev,
0505 HDMI_CODEC_DRV_NAME,
0506 PLATFORM_DEVID_AUTO,
0507 &codec_data,
0508 sizeof(codec_data));
0509 return PTR_ERR_OR_ZERO(hdmi->audio_pdev);
0510 }
0511
0512 static int msm_hdmi_bind(struct device *dev, struct device *master, void *data)
0513 {
0514 struct msm_drm_private *priv = dev_get_drvdata(master);
0515 struct hdmi_platform_config *hdmi_cfg;
0516 struct hdmi *hdmi;
0517 struct device_node *of_node = dev->of_node;
0518 int err;
0519
0520 hdmi_cfg = (struct hdmi_platform_config *)
0521 of_device_get_match_data(dev);
0522 if (!hdmi_cfg) {
0523 DRM_DEV_ERROR(dev, "unknown hdmi_cfg: %pOFn\n", of_node);
0524 return -ENXIO;
0525 }
0526
0527 hdmi_cfg->mmio_name = "core_physical";
0528 hdmi_cfg->qfprom_mmio_name = "qfprom_physical";
0529
0530 dev->platform_data = hdmi_cfg;
0531
0532 hdmi = msm_hdmi_init(to_platform_device(dev));
0533 if (IS_ERR(hdmi))
0534 return PTR_ERR(hdmi);
0535 priv->hdmi = hdmi;
0536
0537 err = msm_hdmi_register_audio_driver(hdmi, dev);
0538 if (err) {
0539 DRM_ERROR("Failed to attach an audio codec %d\n", err);
0540 hdmi->audio_pdev = NULL;
0541 }
0542
0543 return 0;
0544 }
0545
0546 static void msm_hdmi_unbind(struct device *dev, struct device *master,
0547 void *data)
0548 {
0549 struct msm_drm_private *priv = dev_get_drvdata(master);
0550
0551 if (priv->hdmi) {
0552 if (priv->hdmi->audio_pdev)
0553 platform_device_unregister(priv->hdmi->audio_pdev);
0554
0555 msm_hdmi_destroy(priv->hdmi);
0556 priv->hdmi = NULL;
0557 }
0558 }
0559
0560 static const struct component_ops msm_hdmi_ops = {
0561 .bind = msm_hdmi_bind,
0562 .unbind = msm_hdmi_unbind,
0563 };
0564
0565 static int msm_hdmi_dev_probe(struct platform_device *pdev)
0566 {
0567 return component_add(&pdev->dev, &msm_hdmi_ops);
0568 }
0569
0570 static int msm_hdmi_dev_remove(struct platform_device *pdev)
0571 {
0572 component_del(&pdev->dev, &msm_hdmi_ops);
0573 return 0;
0574 }
0575
0576 static const struct of_device_id msm_hdmi_dt_match[] = {
0577 { .compatible = "qcom,hdmi-tx-8996", .data = &hdmi_tx_8974_config },
0578 { .compatible = "qcom,hdmi-tx-8994", .data = &hdmi_tx_8974_config },
0579 { .compatible = "qcom,hdmi-tx-8084", .data = &hdmi_tx_8974_config },
0580 { .compatible = "qcom,hdmi-tx-8974", .data = &hdmi_tx_8974_config },
0581 { .compatible = "qcom,hdmi-tx-8960", .data = &hdmi_tx_8960_config },
0582 { .compatible = "qcom,hdmi-tx-8660", .data = &hdmi_tx_8960_config },
0583 {}
0584 };
0585
0586 static struct platform_driver msm_hdmi_driver = {
0587 .probe = msm_hdmi_dev_probe,
0588 .remove = msm_hdmi_dev_remove,
0589 .driver = {
0590 .name = "hdmi_msm",
0591 .of_match_table = msm_hdmi_dt_match,
0592 },
0593 };
0594
0595 void __init msm_hdmi_register(void)
0596 {
0597 msm_hdmi_phy_driver_register();
0598 platform_driver_register(&msm_hdmi_driver);
0599 }
0600
0601 void __exit msm_hdmi_unregister(void)
0602 {
0603 platform_driver_unregister(&msm_hdmi_driver);
0604 msm_hdmi_phy_driver_unregister();
0605 }