1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2014 The Linux Foundation. All rights reserved.
4 * Copyright (C) 2013 Red Hat
8 #include <linux/of_irq.h>
9 #include <linux/of_gpio.h>
11 #include <drm/drm_bridge_connector.h>
13 #include <sound/hdmi-codec.h>
16 void msm_hdmi_set_mode(struct hdmi *hdmi, bool power_on)
21 spin_lock_irqsave(&hdmi->reg_lock, flags);
23 ctrl |= HDMI_CTRL_ENABLE;
24 if (!hdmi->hdmi_mode) {
25 ctrl |= HDMI_CTRL_HDMI;
26 hdmi_write(hdmi, REG_HDMI_CTRL, ctrl);
27 ctrl &= ~HDMI_CTRL_HDMI;
29 ctrl |= HDMI_CTRL_HDMI;
32 ctrl = HDMI_CTRL_HDMI;
35 hdmi_write(hdmi, REG_HDMI_CTRL, ctrl);
36 spin_unlock_irqrestore(&hdmi->reg_lock, flags);
37 DBG("HDMI Core: %s, HDMI_CTRL=0x%08x",
38 power_on ? "Enable" : "Disable", ctrl);
41 static irqreturn_t msm_hdmi_irq(int irq, void *dev_id)
43 struct hdmi *hdmi = dev_id;
46 msm_hdmi_hpd_irq(hdmi->bridge);
49 msm_hdmi_i2c_irq(hdmi->i2c);
53 msm_hdmi_hdcp_irq(hdmi->hdcp_ctrl);
60 static void msm_hdmi_destroy(struct hdmi *hdmi)
63 * at this point, hpd has been disabled,
64 * after flush workq, it's safe to deinit hdcp
67 destroy_workqueue(hdmi->workq);
68 msm_hdmi_hdcp_destroy(hdmi);
71 put_device(hdmi->phy_dev);
77 msm_hdmi_i2c_destroy(hdmi->i2c);
79 platform_set_drvdata(hdmi->pdev, NULL);
82 static int msm_hdmi_get_phy(struct hdmi *hdmi)
84 struct platform_device *pdev = hdmi->pdev;
85 struct platform_device *phy_pdev;
86 struct device_node *phy_node;
88 phy_node = of_parse_phandle(pdev->dev.of_node, "phys", 0);
90 DRM_DEV_ERROR(&pdev->dev, "cannot find phy device\n");
94 phy_pdev = of_find_device_by_node(phy_node);
96 hdmi->phy = platform_get_drvdata(phy_pdev);
98 of_node_put(phy_node);
100 if (!phy_pdev || !hdmi->phy) {
101 DRM_DEV_ERROR(&pdev->dev, "phy driver is not ready\n");
102 return -EPROBE_DEFER;
105 hdmi->phy_dev = get_device(&phy_pdev->dev);
110 /* construct hdmi at bind/probe time, grab all the resources. If
111 * we are to EPROBE_DEFER we want to do it here, rather than later
112 * at modeset_init() time
114 static struct hdmi *msm_hdmi_init(struct platform_device *pdev)
116 struct hdmi_platform_config *config = pdev->dev.platform_data;
117 struct hdmi *hdmi = NULL;
118 struct resource *res;
121 hdmi = devm_kzalloc(&pdev->dev, sizeof(*hdmi), GFP_KERNEL);
128 hdmi->config = config;
129 spin_lock_init(&hdmi->reg_lock);
131 hdmi->mmio = msm_ioremap(pdev, config->mmio_name, "HDMI");
132 if (IS_ERR(hdmi->mmio)) {
133 ret = PTR_ERR(hdmi->mmio);
137 /* HDCP needs physical address of hdmi register */
138 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
140 hdmi->mmio_phy_addr = res->start;
142 hdmi->qfprom_mmio = msm_ioremap(pdev,
143 config->qfprom_mmio_name, "HDMI_QFPROM");
144 if (IS_ERR(hdmi->qfprom_mmio)) {
145 DRM_DEV_INFO(&pdev->dev, "can't find qfprom resource\n");
146 hdmi->qfprom_mmio = NULL;
149 hdmi->hpd_regs = devm_kcalloc(&pdev->dev,
151 sizeof(hdmi->hpd_regs[0]),
153 if (!hdmi->hpd_regs) {
157 for (i = 0; i < config->hpd_reg_cnt; i++)
158 hdmi->hpd_regs[i].supply = config->hpd_reg_names[i];
160 ret = devm_regulator_bulk_get(&pdev->dev, config->hpd_reg_cnt, hdmi->hpd_regs);
162 DRM_DEV_ERROR(&pdev->dev, "failed to get hpd regulator: %d\n", ret);
166 hdmi->pwr_regs = devm_kcalloc(&pdev->dev,
168 sizeof(hdmi->pwr_regs[0]),
170 if (!hdmi->pwr_regs) {
175 ret = devm_regulator_bulk_get(&pdev->dev, config->pwr_reg_cnt, hdmi->pwr_regs);
177 DRM_DEV_ERROR(&pdev->dev, "failed to get pwr regulator: %d\n", ret);
181 hdmi->hpd_clks = devm_kcalloc(&pdev->dev,
183 sizeof(hdmi->hpd_clks[0]),
185 if (!hdmi->hpd_clks) {
189 for (i = 0; i < config->hpd_clk_cnt; i++) {
192 clk = msm_clk_get(pdev, config->hpd_clk_names[i]);
195 DRM_DEV_ERROR(&pdev->dev, "failed to get hpd clk: %s (%d)\n",
196 config->hpd_clk_names[i], ret);
200 hdmi->hpd_clks[i] = clk;
203 hdmi->pwr_clks = devm_kcalloc(&pdev->dev,
205 sizeof(hdmi->pwr_clks[0]),
207 if (!hdmi->pwr_clks) {
211 for (i = 0; i < config->pwr_clk_cnt; i++) {
214 clk = msm_clk_get(pdev, config->pwr_clk_names[i]);
217 DRM_DEV_ERROR(&pdev->dev, "failed to get pwr clk: %s (%d)\n",
218 config->pwr_clk_names[i], ret);
222 hdmi->pwr_clks[i] = clk;
225 pm_runtime_enable(&pdev->dev);
227 hdmi->workq = alloc_ordered_workqueue("msm_hdmi", 0);
229 hdmi->i2c = msm_hdmi_i2c_init(hdmi);
230 if (IS_ERR(hdmi->i2c)) {
231 ret = PTR_ERR(hdmi->i2c);
232 DRM_DEV_ERROR(&pdev->dev, "failed to get i2c: %d\n", ret);
237 ret = msm_hdmi_get_phy(hdmi);
239 DRM_DEV_ERROR(&pdev->dev, "failed to get phy\n");
243 hdmi->hdcp_ctrl = msm_hdmi_hdcp_init(hdmi);
244 if (IS_ERR(hdmi->hdcp_ctrl)) {
245 dev_warn(&pdev->dev, "failed to init hdcp: disabled\n");
246 hdmi->hdcp_ctrl = NULL;
253 msm_hdmi_destroy(hdmi);
258 /* Second part of initialization, the drm/kms level modeset_init,
259 * constructs/initializes mode objects, etc, is called from master
260 * driver (not hdmi sub-device's probe/bind!)
262 * Any resource (regulator/clk/etc) which could be missing at boot
263 * should be handled in msm_hdmi_init() so that failure happens from
264 * hdmi sub-device's probe.
266 int msm_hdmi_modeset_init(struct hdmi *hdmi,
267 struct drm_device *dev, struct drm_encoder *encoder)
269 struct msm_drm_private *priv = dev->dev_private;
270 struct platform_device *pdev = hdmi->pdev;
274 hdmi->encoder = encoder;
276 hdmi_audio_infoframe_init(&hdmi->audio.infoframe);
278 hdmi->bridge = msm_hdmi_bridge_init(hdmi);
279 if (IS_ERR(hdmi->bridge)) {
280 ret = PTR_ERR(hdmi->bridge);
281 DRM_DEV_ERROR(dev->dev, "failed to create HDMI bridge: %d\n", ret);
286 hdmi->connector = drm_bridge_connector_init(hdmi->dev, encoder);
287 if (IS_ERR(hdmi->connector)) {
288 ret = PTR_ERR(hdmi->connector);
289 DRM_DEV_ERROR(dev->dev, "failed to create HDMI connector: %d\n", ret);
290 hdmi->connector = NULL;
294 drm_connector_attach_encoder(hdmi->connector, hdmi->encoder);
296 hdmi->irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
299 DRM_DEV_ERROR(dev->dev, "failed to get irq: %d\n", ret);
303 ret = devm_request_irq(&pdev->dev, hdmi->irq,
304 msm_hdmi_irq, IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
307 DRM_DEV_ERROR(dev->dev, "failed to request IRQ%u: %d\n",
312 drm_bridge_connector_enable_hpd(hdmi->connector);
314 ret = msm_hdmi_hpd_enable(hdmi->bridge);
316 DRM_DEV_ERROR(&hdmi->pdev->dev, "failed to enable HPD: %d\n", ret);
320 priv->bridges[priv->num_bridges++] = hdmi->bridge;
321 priv->connectors[priv->num_connectors++] = hdmi->connector;
323 platform_set_drvdata(pdev, hdmi);
328 /* bridge is normally destroyed by drm: */
330 msm_hdmi_bridge_destroy(hdmi->bridge);
333 if (hdmi->connector) {
334 hdmi->connector->funcs->destroy(hdmi->connector);
335 hdmi->connector = NULL;
345 #define HDMI_CFG(item, entry) \
346 .item ## _names = item ##_names_ ## entry, \
347 .item ## _cnt = ARRAY_SIZE(item ## _names_ ## entry)
349 static const char *pwr_reg_names_none[] = {};
350 static const char *hpd_reg_names_none[] = {};
352 static struct hdmi_platform_config hdmi_tx_8660_config;
354 static const char *hpd_reg_names_8960[] = {"core-vdda", "hdmi-mux"};
355 static const char *hpd_clk_names_8960[] = {"core", "master_iface", "slave_iface"};
357 static struct hdmi_platform_config hdmi_tx_8960_config = {
358 HDMI_CFG(hpd_reg, 8960),
359 HDMI_CFG(hpd_clk, 8960),
362 static const char *pwr_reg_names_8x74[] = {"core-vdda", "core-vcc"};
363 static const char *hpd_reg_names_8x74[] = {"hpd-gdsc", "hpd-5v"};
364 static const char *pwr_clk_names_8x74[] = {"extp", "alt_iface"};
365 static const char *hpd_clk_names_8x74[] = {"iface", "core", "mdp_core"};
366 static unsigned long hpd_clk_freq_8x74[] = {0, 19200000, 0};
368 static struct hdmi_platform_config hdmi_tx_8974_config = {
369 HDMI_CFG(pwr_reg, 8x74),
370 HDMI_CFG(hpd_reg, 8x74),
371 HDMI_CFG(pwr_clk, 8x74),
372 HDMI_CFG(hpd_clk, 8x74),
373 .hpd_freq = hpd_clk_freq_8x74,
376 static const char *hpd_reg_names_8084[] = {"hpd-gdsc", "hpd-5v", "hpd-5v-en"};
378 static struct hdmi_platform_config hdmi_tx_8084_config = {
379 HDMI_CFG(pwr_reg, 8x74),
380 HDMI_CFG(hpd_reg, 8084),
381 HDMI_CFG(pwr_clk, 8x74),
382 HDMI_CFG(hpd_clk, 8x74),
383 .hpd_freq = hpd_clk_freq_8x74,
386 static struct hdmi_platform_config hdmi_tx_8994_config = {
387 HDMI_CFG(pwr_reg, 8x74),
388 HDMI_CFG(hpd_reg, none),
389 HDMI_CFG(pwr_clk, 8x74),
390 HDMI_CFG(hpd_clk, 8x74),
391 .hpd_freq = hpd_clk_freq_8x74,
394 static struct hdmi_platform_config hdmi_tx_8996_config = {
395 HDMI_CFG(pwr_reg, none),
396 HDMI_CFG(hpd_reg, none),
397 HDMI_CFG(pwr_clk, 8x74),
398 HDMI_CFG(hpd_clk, 8x74),
399 .hpd_freq = hpd_clk_freq_8x74,
402 static const struct {
407 } msm_hdmi_gpio_pdata[] = {
408 { "qcom,hdmi-tx-ddc-clk", true, 1, "HDMI_DDC_CLK" },
409 { "qcom,hdmi-tx-ddc-data", true, 1, "HDMI_DDC_DATA" },
410 { "qcom,hdmi-tx-hpd", false, 1, "HDMI_HPD" },
411 { "qcom,hdmi-tx-mux-en", true, 1, "HDMI_MUX_EN" },
412 { "qcom,hdmi-tx-mux-sel", true, 0, "HDMI_MUX_SEL" },
413 { "qcom,hdmi-tx-mux-lpm", true, 1, "HDMI_MUX_LPM" },
417 * HDMI audio codec callbacks
419 static int msm_hdmi_audio_hw_params(struct device *dev, void *data,
420 struct hdmi_codec_daifmt *daifmt,
421 struct hdmi_codec_params *params)
423 struct hdmi *hdmi = dev_get_drvdata(dev);
425 unsigned int channel_allocation = 0;
427 unsigned int level_shift = 0; /* 0dB */
428 bool down_mix = false;
430 DRM_DEV_DEBUG(dev, "%u Hz, %d bit, %d channels\n", params->sample_rate,
431 params->sample_width, params->cea.channels);
433 switch (params->cea.channels) {
435 /* FR and FL speakers */
436 channel_allocation = 0;
437 chan = MSM_HDMI_AUDIO_CHANNEL_2;
440 /* FC, LFE, FR and FL speakers */
441 channel_allocation = 0x3;
442 chan = MSM_HDMI_AUDIO_CHANNEL_4;
445 /* RR, RL, FC, LFE, FR and FL speakers */
446 channel_allocation = 0x0B;
447 chan = MSM_HDMI_AUDIO_CHANNEL_6;
450 /* FRC, FLC, RR, RL, FC, LFE, FR and FL speakers */
451 channel_allocation = 0x1F;
452 chan = MSM_HDMI_AUDIO_CHANNEL_8;
458 switch (params->sample_rate) {
460 rate = HDMI_SAMPLE_RATE_32KHZ;
463 rate = HDMI_SAMPLE_RATE_44_1KHZ;
466 rate = HDMI_SAMPLE_RATE_48KHZ;
469 rate = HDMI_SAMPLE_RATE_88_2KHZ;
472 rate = HDMI_SAMPLE_RATE_96KHZ;
475 rate = HDMI_SAMPLE_RATE_176_4KHZ;
478 rate = HDMI_SAMPLE_RATE_192KHZ;
481 DRM_DEV_ERROR(dev, "rate[%d] not supported!\n",
482 params->sample_rate);
486 msm_hdmi_audio_set_sample_rate(hdmi, rate);
487 msm_hdmi_audio_info_setup(hdmi, 1, chan, channel_allocation,
488 level_shift, down_mix);
493 static void msm_hdmi_audio_shutdown(struct device *dev, void *data)
495 struct hdmi *hdmi = dev_get_drvdata(dev);
497 msm_hdmi_audio_info_setup(hdmi, 0, 0, 0, 0, 0);
500 static const struct hdmi_codec_ops msm_hdmi_audio_codec_ops = {
501 .hw_params = msm_hdmi_audio_hw_params,
502 .audio_shutdown = msm_hdmi_audio_shutdown,
505 static struct hdmi_codec_pdata codec_data = {
506 .ops = &msm_hdmi_audio_codec_ops,
507 .max_i2s_channels = 8,
511 static int msm_hdmi_register_audio_driver(struct hdmi *hdmi, struct device *dev)
513 hdmi->audio_pdev = platform_device_register_data(dev,
518 return PTR_ERR_OR_ZERO(hdmi->audio_pdev);
521 static int msm_hdmi_bind(struct device *dev, struct device *master, void *data)
523 struct msm_drm_private *priv = dev_get_drvdata(master);
524 struct hdmi_platform_config *hdmi_cfg;
526 struct device_node *of_node = dev->of_node;
529 hdmi_cfg = (struct hdmi_platform_config *)
530 of_device_get_match_data(dev);
532 DRM_DEV_ERROR(dev, "unknown hdmi_cfg: %pOFn\n", of_node);
536 hdmi_cfg->mmio_name = "core_physical";
537 hdmi_cfg->qfprom_mmio_name = "qfprom_physical";
539 for (i = 0; i < HDMI_MAX_NUM_GPIO; i++) {
540 const char *name = msm_hdmi_gpio_pdata[i].name;
541 struct gpio_desc *gpiod;
544 * We are fetching the GPIO lines "as is" since the connector
545 * code is enabling and disabling the lines. Until that point
546 * the power-on default value will be kept.
548 gpiod = devm_gpiod_get_optional(dev, name, GPIOD_ASIS);
549 /* This will catch e.g. -PROBE_DEFER */
551 return PTR_ERR(gpiod);
553 /* Try a second time, stripping down the name */
557 * Try again after stripping out the "qcom,hdmi-tx"
558 * prefix. This is mainly to match "hpd-gpios" used
559 * in the upstream bindings.
561 if (sscanf(name, "qcom,hdmi-tx-%s", name3))
562 gpiod = devm_gpiod_get_optional(dev, name3, GPIOD_ASIS);
564 return PTR_ERR(gpiod);
566 DBG("failed to get gpio: %s", name);
568 hdmi_cfg->gpios[i].gpiod = gpiod;
570 gpiod_set_consumer_name(gpiod, msm_hdmi_gpio_pdata[i].label);
571 hdmi_cfg->gpios[i].output = msm_hdmi_gpio_pdata[i].output;
572 hdmi_cfg->gpios[i].value = msm_hdmi_gpio_pdata[i].value;
575 dev->platform_data = hdmi_cfg;
577 hdmi = msm_hdmi_init(to_platform_device(dev));
579 return PTR_ERR(hdmi);
582 err = msm_hdmi_register_audio_driver(hdmi, dev);
584 DRM_ERROR("Failed to attach an audio codec %d\n", err);
585 hdmi->audio_pdev = NULL;
591 static void msm_hdmi_unbind(struct device *dev, struct device *master,
594 struct msm_drm_private *priv = dev_get_drvdata(master);
597 if (priv->hdmi->audio_pdev)
598 platform_device_unregister(priv->hdmi->audio_pdev);
600 msm_hdmi_destroy(priv->hdmi);
605 static const struct component_ops msm_hdmi_ops = {
606 .bind = msm_hdmi_bind,
607 .unbind = msm_hdmi_unbind,
610 static int msm_hdmi_dev_probe(struct platform_device *pdev)
612 return component_add(&pdev->dev, &msm_hdmi_ops);
615 static int msm_hdmi_dev_remove(struct platform_device *pdev)
617 component_del(&pdev->dev, &msm_hdmi_ops);
621 static const struct of_device_id msm_hdmi_dt_match[] = {
622 { .compatible = "qcom,hdmi-tx-8996", .data = &hdmi_tx_8996_config },
623 { .compatible = "qcom,hdmi-tx-8994", .data = &hdmi_tx_8994_config },
624 { .compatible = "qcom,hdmi-tx-8084", .data = &hdmi_tx_8084_config },
625 { .compatible = "qcom,hdmi-tx-8974", .data = &hdmi_tx_8974_config },
626 { .compatible = "qcom,hdmi-tx-8960", .data = &hdmi_tx_8960_config },
627 { .compatible = "qcom,hdmi-tx-8660", .data = &hdmi_tx_8660_config },
631 static struct platform_driver msm_hdmi_driver = {
632 .probe = msm_hdmi_dev_probe,
633 .remove = msm_hdmi_dev_remove,
636 .of_match_table = msm_hdmi_dt_match,
640 void __init msm_hdmi_register(void)
642 msm_hdmi_phy_driver_register();
643 platform_driver_register(&msm_hdmi_driver);
646 void __exit msm_hdmi_unregister(void)
648 platform_driver_unregister(&msm_hdmi_driver);
649 msm_hdmi_phy_driver_unregister();