1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2014 The Linux Foundation. All rights reserved.
4 * Copyright (C) 2013 Red Hat
5 * Author: Rob Clark <robdclark@gmail.com>
8 #include <linux/of_irq.h>
9 #include <linux/of_gpio.h>
11 #include <drm/drm_bridge_connector.h>
12 #include <drm/drm_of.h>
14 #include <sound/hdmi-codec.h>
17 void msm_hdmi_set_mode(struct hdmi *hdmi, bool power_on)
22 spin_lock_irqsave(&hdmi->reg_lock, flags);
24 ctrl |= HDMI_CTRL_ENABLE;
25 if (!hdmi->hdmi_mode) {
26 ctrl |= HDMI_CTRL_HDMI;
27 hdmi_write(hdmi, REG_HDMI_CTRL, ctrl);
28 ctrl &= ~HDMI_CTRL_HDMI;
30 ctrl |= HDMI_CTRL_HDMI;
33 ctrl = HDMI_CTRL_HDMI;
36 hdmi_write(hdmi, REG_HDMI_CTRL, ctrl);
37 spin_unlock_irqrestore(&hdmi->reg_lock, flags);
38 DBG("HDMI Core: %s, HDMI_CTRL=0x%08x",
39 power_on ? "Enable" : "Disable", ctrl);
42 static irqreturn_t msm_hdmi_irq(int irq, void *dev_id)
44 struct hdmi *hdmi = dev_id;
47 msm_hdmi_hpd_irq(hdmi->bridge);
50 msm_hdmi_i2c_irq(hdmi->i2c);
54 msm_hdmi_hdcp_irq(hdmi->hdcp_ctrl);
61 static void msm_hdmi_destroy(struct hdmi *hdmi)
64 * at this point, hpd has been disabled,
65 * after flush workq, it's safe to deinit hdcp
68 destroy_workqueue(hdmi->workq);
69 msm_hdmi_hdcp_destroy(hdmi);
72 put_device(hdmi->phy_dev);
78 msm_hdmi_i2c_destroy(hdmi->i2c);
80 platform_set_drvdata(hdmi->pdev, NULL);
83 static int msm_hdmi_get_phy(struct hdmi *hdmi)
85 struct platform_device *pdev = hdmi->pdev;
86 struct platform_device *phy_pdev;
87 struct device_node *phy_node;
89 phy_node = of_parse_phandle(pdev->dev.of_node, "phys", 0);
91 DRM_DEV_ERROR(&pdev->dev, "cannot find phy device\n");
95 phy_pdev = of_find_device_by_node(phy_node);
97 hdmi->phy = platform_get_drvdata(phy_pdev);
99 of_node_put(phy_node);
102 DRM_DEV_ERROR(&pdev->dev, "phy driver is not ready\n");
103 return -EPROBE_DEFER;
106 DRM_DEV_ERROR(&pdev->dev, "phy driver is not ready\n");
107 put_device(&phy_pdev->dev);
108 return -EPROBE_DEFER;
111 hdmi->phy_dev = get_device(&phy_pdev->dev);
116 /* construct hdmi at bind/probe time, grab all the resources. If
117 * we are to EPROBE_DEFER we want to do it here, rather than later
118 * at modeset_init() time
120 static struct hdmi *msm_hdmi_init(struct platform_device *pdev)
122 struct hdmi_platform_config *config = pdev->dev.platform_data;
123 struct hdmi *hdmi = NULL;
124 struct resource *res;
127 hdmi = devm_kzalloc(&pdev->dev, sizeof(*hdmi), GFP_KERNEL);
134 hdmi->config = config;
135 spin_lock_init(&hdmi->reg_lock);
137 ret = drm_of_find_panel_or_bridge(pdev->dev.of_node, 1, 0, NULL, &hdmi->next_bridge);
138 if (ret && ret != -ENODEV)
141 hdmi->mmio = msm_ioremap(pdev, config->mmio_name);
142 if (IS_ERR(hdmi->mmio)) {
143 ret = PTR_ERR(hdmi->mmio);
147 /* HDCP needs physical address of hdmi register */
148 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
154 hdmi->mmio_phy_addr = res->start;
156 hdmi->qfprom_mmio = msm_ioremap(pdev, config->qfprom_mmio_name);
157 if (IS_ERR(hdmi->qfprom_mmio)) {
158 DRM_DEV_INFO(&pdev->dev, "can't find qfprom resource\n");
159 hdmi->qfprom_mmio = NULL;
162 hdmi->hpd_regs = devm_kcalloc(&pdev->dev,
164 sizeof(hdmi->hpd_regs[0]),
166 if (!hdmi->hpd_regs) {
170 for (i = 0; i < config->hpd_reg_cnt; i++)
171 hdmi->hpd_regs[i].supply = config->hpd_reg_names[i];
173 ret = devm_regulator_bulk_get(&pdev->dev, config->hpd_reg_cnt, hdmi->hpd_regs);
175 DRM_DEV_ERROR(&pdev->dev, "failed to get hpd regulator: %d\n", ret);
179 hdmi->pwr_regs = devm_kcalloc(&pdev->dev,
181 sizeof(hdmi->pwr_regs[0]),
183 if (!hdmi->pwr_regs) {
188 for (i = 0; i < config->pwr_reg_cnt; i++)
189 hdmi->pwr_regs[i].supply = config->pwr_reg_names[i];
191 ret = devm_regulator_bulk_get(&pdev->dev, config->pwr_reg_cnt, hdmi->pwr_regs);
193 DRM_DEV_ERROR(&pdev->dev, "failed to get pwr regulator: %d\n", ret);
197 hdmi->hpd_clks = devm_kcalloc(&pdev->dev,
199 sizeof(hdmi->hpd_clks[0]),
201 if (!hdmi->hpd_clks) {
205 for (i = 0; i < config->hpd_clk_cnt; i++) {
208 clk = msm_clk_get(pdev, config->hpd_clk_names[i]);
211 DRM_DEV_ERROR(&pdev->dev, "failed to get hpd clk: %s (%d)\n",
212 config->hpd_clk_names[i], ret);
216 hdmi->hpd_clks[i] = clk;
219 hdmi->pwr_clks = devm_kcalloc(&pdev->dev,
221 sizeof(hdmi->pwr_clks[0]),
223 if (!hdmi->pwr_clks) {
227 for (i = 0; i < config->pwr_clk_cnt; i++) {
230 clk = msm_clk_get(pdev, config->pwr_clk_names[i]);
233 DRM_DEV_ERROR(&pdev->dev, "failed to get pwr clk: %s (%d)\n",
234 config->pwr_clk_names[i], ret);
238 hdmi->pwr_clks[i] = clk;
241 hdmi->hpd_gpiod = devm_gpiod_get_optional(&pdev->dev, "hpd", GPIOD_IN);
242 /* This will catch e.g. -EPROBE_DEFER */
243 if (IS_ERR(hdmi->hpd_gpiod)) {
244 ret = PTR_ERR(hdmi->hpd_gpiod);
245 DRM_DEV_ERROR(&pdev->dev, "failed to get hpd gpio: (%d)\n", ret);
249 if (!hdmi->hpd_gpiod)
250 DBG("failed to get HPD gpio");
253 gpiod_set_consumer_name(hdmi->hpd_gpiod, "HDMI_HPD");
255 pm_runtime_enable(&pdev->dev);
257 hdmi->workq = alloc_ordered_workqueue("msm_hdmi", 0);
259 hdmi->i2c = msm_hdmi_i2c_init(hdmi);
260 if (IS_ERR(hdmi->i2c)) {
261 ret = PTR_ERR(hdmi->i2c);
262 DRM_DEV_ERROR(&pdev->dev, "failed to get i2c: %d\n", ret);
267 ret = msm_hdmi_get_phy(hdmi);
269 DRM_DEV_ERROR(&pdev->dev, "failed to get phy\n");
273 hdmi->hdcp_ctrl = msm_hdmi_hdcp_init(hdmi);
274 if (IS_ERR(hdmi->hdcp_ctrl)) {
275 dev_warn(&pdev->dev, "failed to init hdcp: disabled\n");
276 hdmi->hdcp_ctrl = NULL;
283 msm_hdmi_destroy(hdmi);
288 /* Second part of initialization, the drm/kms level modeset_init,
289 * constructs/initializes mode objects, etc, is called from master
290 * driver (not hdmi sub-device's probe/bind!)
292 * Any resource (regulator/clk/etc) which could be missing at boot
293 * should be handled in msm_hdmi_init() so that failure happens from
294 * hdmi sub-device's probe.
296 int msm_hdmi_modeset_init(struct hdmi *hdmi,
297 struct drm_device *dev, struct drm_encoder *encoder)
299 struct msm_drm_private *priv = dev->dev_private;
300 struct platform_device *pdev = hdmi->pdev;
303 if (priv->num_bridges == ARRAY_SIZE(priv->bridges)) {
304 DRM_DEV_ERROR(dev->dev, "too many bridges\n");
309 hdmi->encoder = encoder;
311 hdmi_audio_infoframe_init(&hdmi->audio.infoframe);
313 hdmi->bridge = msm_hdmi_bridge_init(hdmi);
314 if (IS_ERR(hdmi->bridge)) {
315 ret = PTR_ERR(hdmi->bridge);
316 DRM_DEV_ERROR(dev->dev, "failed to create HDMI bridge: %d\n", ret);
321 if (hdmi->next_bridge) {
322 ret = drm_bridge_attach(hdmi->encoder, hdmi->next_bridge, hdmi->bridge,
323 DRM_BRIDGE_ATTACH_NO_CONNECTOR);
325 DRM_DEV_ERROR(dev->dev, "failed to attach next HDMI bridge: %d\n", ret);
330 hdmi->connector = drm_bridge_connector_init(hdmi->dev, encoder);
331 if (IS_ERR(hdmi->connector)) {
332 ret = PTR_ERR(hdmi->connector);
333 DRM_DEV_ERROR(dev->dev, "failed to create HDMI connector: %d\n", ret);
334 hdmi->connector = NULL;
338 drm_connector_attach_encoder(hdmi->connector, hdmi->encoder);
340 hdmi->irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
343 DRM_DEV_ERROR(dev->dev, "failed to get irq\n");
347 ret = devm_request_irq(dev->dev, hdmi->irq,
348 msm_hdmi_irq, IRQF_TRIGGER_HIGH,
351 DRM_DEV_ERROR(dev->dev, "failed to request IRQ%u: %d\n",
356 drm_bridge_connector_enable_hpd(hdmi->connector);
358 ret = msm_hdmi_hpd_enable(hdmi->bridge);
360 DRM_DEV_ERROR(&hdmi->pdev->dev, "failed to enable HPD: %d\n", ret);
364 priv->bridges[priv->num_bridges++] = hdmi->bridge;
366 platform_set_drvdata(pdev, hdmi);
371 /* bridge is normally destroyed by drm: */
373 msm_hdmi_bridge_destroy(hdmi->bridge);
376 if (hdmi->connector) {
377 hdmi->connector->funcs->destroy(hdmi->connector);
378 hdmi->connector = NULL;
388 #define HDMI_CFG(item, entry) \
389 .item ## _names = item ##_names_ ## entry, \
390 .item ## _cnt = ARRAY_SIZE(item ## _names_ ## entry)
392 static const char *hpd_reg_names_8960[] = {"core-vdda"};
393 static const char *hpd_clk_names_8960[] = {"core", "master_iface", "slave_iface"};
395 static struct hdmi_platform_config hdmi_tx_8960_config = {
396 HDMI_CFG(hpd_reg, 8960),
397 HDMI_CFG(hpd_clk, 8960),
400 static const char *pwr_reg_names_8x74[] = {"core-vdda", "core-vcc"};
401 static const char *pwr_clk_names_8x74[] = {"extp", "alt_iface"};
402 static const char *hpd_clk_names_8x74[] = {"iface", "core", "mdp_core"};
403 static unsigned long hpd_clk_freq_8x74[] = {0, 19200000, 0};
405 static struct hdmi_platform_config hdmi_tx_8974_config = {
406 HDMI_CFG(pwr_reg, 8x74),
407 HDMI_CFG(pwr_clk, 8x74),
408 HDMI_CFG(hpd_clk, 8x74),
409 .hpd_freq = hpd_clk_freq_8x74,
413 * HDMI audio codec callbacks
415 static int msm_hdmi_audio_hw_params(struct device *dev, void *data,
416 struct hdmi_codec_daifmt *daifmt,
417 struct hdmi_codec_params *params)
419 struct hdmi *hdmi = dev_get_drvdata(dev);
421 unsigned int channel_allocation = 0;
423 unsigned int level_shift = 0; /* 0dB */
424 bool down_mix = false;
426 DRM_DEV_DEBUG(dev, "%u Hz, %d bit, %d channels\n", params->sample_rate,
427 params->sample_width, params->cea.channels);
429 switch (params->cea.channels) {
431 /* FR and FL speakers */
432 channel_allocation = 0;
433 chan = MSM_HDMI_AUDIO_CHANNEL_2;
436 /* FC, LFE, FR and FL speakers */
437 channel_allocation = 0x3;
438 chan = MSM_HDMI_AUDIO_CHANNEL_4;
441 /* RR, RL, FC, LFE, FR and FL speakers */
442 channel_allocation = 0x0B;
443 chan = MSM_HDMI_AUDIO_CHANNEL_6;
446 /* FRC, FLC, RR, RL, FC, LFE, FR and FL speakers */
447 channel_allocation = 0x1F;
448 chan = MSM_HDMI_AUDIO_CHANNEL_8;
454 switch (params->sample_rate) {
456 rate = HDMI_SAMPLE_RATE_32KHZ;
459 rate = HDMI_SAMPLE_RATE_44_1KHZ;
462 rate = HDMI_SAMPLE_RATE_48KHZ;
465 rate = HDMI_SAMPLE_RATE_88_2KHZ;
468 rate = HDMI_SAMPLE_RATE_96KHZ;
471 rate = HDMI_SAMPLE_RATE_176_4KHZ;
474 rate = HDMI_SAMPLE_RATE_192KHZ;
477 DRM_DEV_ERROR(dev, "rate[%d] not supported!\n",
478 params->sample_rate);
482 msm_hdmi_audio_set_sample_rate(hdmi, rate);
483 msm_hdmi_audio_info_setup(hdmi, 1, chan, channel_allocation,
484 level_shift, down_mix);
489 static void msm_hdmi_audio_shutdown(struct device *dev, void *data)
491 struct hdmi *hdmi = dev_get_drvdata(dev);
493 msm_hdmi_audio_info_setup(hdmi, 0, 0, 0, 0, 0);
496 static const struct hdmi_codec_ops msm_hdmi_audio_codec_ops = {
497 .hw_params = msm_hdmi_audio_hw_params,
498 .audio_shutdown = msm_hdmi_audio_shutdown,
501 static struct hdmi_codec_pdata codec_data = {
502 .ops = &msm_hdmi_audio_codec_ops,
503 .max_i2s_channels = 8,
507 static int msm_hdmi_register_audio_driver(struct hdmi *hdmi, struct device *dev)
509 hdmi->audio_pdev = platform_device_register_data(dev,
514 return PTR_ERR_OR_ZERO(hdmi->audio_pdev);
517 static int msm_hdmi_bind(struct device *dev, struct device *master, void *data)
519 struct msm_drm_private *priv = dev_get_drvdata(master);
520 struct hdmi_platform_config *hdmi_cfg;
522 struct device_node *of_node = dev->of_node;
525 hdmi_cfg = (struct hdmi_platform_config *)
526 of_device_get_match_data(dev);
528 DRM_DEV_ERROR(dev, "unknown hdmi_cfg: %pOFn\n", of_node);
532 hdmi_cfg->mmio_name = "core_physical";
533 hdmi_cfg->qfprom_mmio_name = "qfprom_physical";
535 dev->platform_data = hdmi_cfg;
537 hdmi = msm_hdmi_init(to_platform_device(dev));
539 return PTR_ERR(hdmi);
542 err = msm_hdmi_register_audio_driver(hdmi, dev);
544 DRM_ERROR("Failed to attach an audio codec %d\n", err);
545 hdmi->audio_pdev = NULL;
551 static void msm_hdmi_unbind(struct device *dev, struct device *master,
554 struct msm_drm_private *priv = dev_get_drvdata(master);
557 if (priv->hdmi->audio_pdev)
558 platform_device_unregister(priv->hdmi->audio_pdev);
560 msm_hdmi_destroy(priv->hdmi);
565 static const struct component_ops msm_hdmi_ops = {
566 .bind = msm_hdmi_bind,
567 .unbind = msm_hdmi_unbind,
570 static int msm_hdmi_dev_probe(struct platform_device *pdev)
572 return component_add(&pdev->dev, &msm_hdmi_ops);
575 static int msm_hdmi_dev_remove(struct platform_device *pdev)
577 component_del(&pdev->dev, &msm_hdmi_ops);
581 static const struct of_device_id msm_hdmi_dt_match[] = {
582 { .compatible = "qcom,hdmi-tx-8996", .data = &hdmi_tx_8974_config },
583 { .compatible = "qcom,hdmi-tx-8994", .data = &hdmi_tx_8974_config },
584 { .compatible = "qcom,hdmi-tx-8084", .data = &hdmi_tx_8974_config },
585 { .compatible = "qcom,hdmi-tx-8974", .data = &hdmi_tx_8974_config },
586 { .compatible = "qcom,hdmi-tx-8960", .data = &hdmi_tx_8960_config },
587 { .compatible = "qcom,hdmi-tx-8660", .data = &hdmi_tx_8960_config },
591 static struct platform_driver msm_hdmi_driver = {
592 .probe = msm_hdmi_dev_probe,
593 .remove = msm_hdmi_dev_remove,
596 .of_match_table = msm_hdmi_dt_match,
600 void __init msm_hdmi_register(void)
602 msm_hdmi_phy_driver_register();
603 platform_driver_register(&msm_hdmi_driver);
606 void __exit msm_hdmi_unregister(void)
608 platform_driver_unregister(&msm_hdmi_driver);
609 msm_hdmi_phy_driver_unregister();