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/gpio/consumer.h>
9 #include <linux/of_irq.h>
10 #include <linux/of_platform.h>
11 #include <linux/platform_device.h>
13 #include <drm/drm_bridge_connector.h>
14 #include <drm/drm_of.h>
16 #include <sound/hdmi-codec.h>
19 void msm_hdmi_set_mode(struct hdmi *hdmi, bool power_on)
24 spin_lock_irqsave(&hdmi->reg_lock, flags);
26 ctrl |= HDMI_CTRL_ENABLE;
27 if (!hdmi->hdmi_mode) {
28 ctrl |= HDMI_CTRL_HDMI;
29 hdmi_write(hdmi, REG_HDMI_CTRL, ctrl);
30 ctrl &= ~HDMI_CTRL_HDMI;
32 ctrl |= HDMI_CTRL_HDMI;
35 ctrl = HDMI_CTRL_HDMI;
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);
44 static irqreturn_t msm_hdmi_irq(int irq, void *dev_id)
46 struct hdmi *hdmi = dev_id;
49 msm_hdmi_hpd_irq(hdmi->bridge);
52 msm_hdmi_i2c_irq(hdmi->i2c);
56 msm_hdmi_hdcp_irq(hdmi->hdcp_ctrl);
63 static void msm_hdmi_destroy(struct hdmi *hdmi)
66 * at this point, hpd has been disabled,
67 * after flush workq, it's safe to deinit hdcp
70 destroy_workqueue(hdmi->workq);
71 msm_hdmi_hdcp_destroy(hdmi);
74 msm_hdmi_i2c_destroy(hdmi->i2c);
77 static void msm_hdmi_put_phy(struct hdmi *hdmi)
80 put_device(hdmi->phy_dev);
86 static int msm_hdmi_get_phy(struct hdmi *hdmi)
88 struct platform_device *pdev = hdmi->pdev;
89 struct platform_device *phy_pdev;
90 struct device_node *phy_node;
92 phy_node = of_parse_phandle(pdev->dev.of_node, "phys", 0);
94 DRM_DEV_ERROR(&pdev->dev, "cannot find phy device\n");
98 phy_pdev = of_find_device_by_node(phy_node);
99 of_node_put(phy_node);
102 return dev_err_probe(&pdev->dev, -EPROBE_DEFER, "phy driver is not ready\n");
104 hdmi->phy = platform_get_drvdata(phy_pdev);
106 put_device(&phy_pdev->dev);
107 return dev_err_probe(&pdev->dev, -EPROBE_DEFER, "phy driver is not ready\n");
110 hdmi->phy_dev = &phy_pdev->dev;
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
119 static int msm_hdmi_init(struct hdmi *hdmi)
121 struct platform_device *pdev = hdmi->pdev;
124 hdmi->workq = alloc_ordered_workqueue("msm_hdmi", 0);
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);
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;
147 msm_hdmi_destroy(hdmi);
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!)
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.
160 int msm_hdmi_modeset_init(struct hdmi *hdmi,
161 struct drm_device *dev, struct drm_encoder *encoder)
166 hdmi->encoder = encoder;
168 hdmi_audio_infoframe_init(&hdmi->audio.infoframe);
170 ret = msm_hdmi_bridge_init(hdmi);
172 DRM_DEV_ERROR(dev->dev, "failed to create HDMI bridge: %d\n", ret);
176 if (hdmi->next_bridge) {
177 ret = drm_bridge_attach(hdmi->encoder, hdmi->next_bridge, hdmi->bridge,
178 DRM_BRIDGE_ATTACH_NO_CONNECTOR);
180 DRM_DEV_ERROR(dev->dev, "failed to attach next HDMI bridge: %d\n", ret);
185 hdmi->connector = drm_bridge_connector_init(hdmi->dev, encoder);
186 if (IS_ERR(hdmi->connector)) {
187 ret = PTR_ERR(hdmi->connector);
188 DRM_DEV_ERROR(dev->dev, "failed to create HDMI connector: %d\n", ret);
189 hdmi->connector = NULL;
193 drm_connector_attach_encoder(hdmi->connector, hdmi->encoder);
195 ret = devm_request_irq(dev->dev, hdmi->irq,
196 msm_hdmi_irq, IRQF_TRIGGER_HIGH,
199 DRM_DEV_ERROR(dev->dev, "failed to request IRQ%u: %d\n",
204 ret = msm_hdmi_hpd_enable(hdmi->bridge);
206 DRM_DEV_ERROR(&hdmi->pdev->dev, "failed to enable HPD: %d\n", ret);
213 if (hdmi->connector) {
214 hdmi->connector->funcs->destroy(hdmi->connector);
215 hdmi->connector = NULL;
225 #define HDMI_CFG(item, entry) \
226 .item ## _names = item ##_names_ ## entry, \
227 .item ## _cnt = ARRAY_SIZE(item ## _names_ ## entry)
229 static const char *hpd_reg_names_8960[] = {"core-vdda"};
230 static const char *hpd_clk_names_8960[] = {"core", "master_iface", "slave_iface"};
232 static const struct hdmi_platform_config hdmi_tx_8960_config = {
233 HDMI_CFG(hpd_reg, 8960),
234 HDMI_CFG(hpd_clk, 8960),
237 static const char *pwr_reg_names_8x74[] = {"core-vdda", "core-vcc"};
238 static const char *pwr_clk_names_8x74[] = {"extp", "alt_iface"};
239 static const char *hpd_clk_names_8x74[] = {"iface", "core", "mdp_core"};
240 static unsigned long hpd_clk_freq_8x74[] = {0, 19200000, 0};
242 static const struct hdmi_platform_config hdmi_tx_8974_config = {
243 HDMI_CFG(pwr_reg, 8x74),
244 HDMI_CFG(pwr_clk, 8x74),
245 HDMI_CFG(hpd_clk, 8x74),
246 .hpd_freq = hpd_clk_freq_8x74,
250 * HDMI audio codec callbacks
252 static int msm_hdmi_audio_hw_params(struct device *dev, void *data,
253 struct hdmi_codec_daifmt *daifmt,
254 struct hdmi_codec_params *params)
256 struct hdmi *hdmi = dev_get_drvdata(dev);
258 unsigned int channel_allocation = 0;
260 unsigned int level_shift = 0; /* 0dB */
261 bool down_mix = false;
263 DRM_DEV_DEBUG(dev, "%u Hz, %d bit, %d channels\n", params->sample_rate,
264 params->sample_width, params->cea.channels);
266 switch (params->cea.channels) {
268 /* FR and FL speakers */
269 channel_allocation = 0;
270 chan = MSM_HDMI_AUDIO_CHANNEL_2;
273 /* FC, LFE, FR and FL speakers */
274 channel_allocation = 0x3;
275 chan = MSM_HDMI_AUDIO_CHANNEL_4;
278 /* RR, RL, FC, LFE, FR and FL speakers */
279 channel_allocation = 0x0B;
280 chan = MSM_HDMI_AUDIO_CHANNEL_6;
283 /* FRC, FLC, RR, RL, FC, LFE, FR and FL speakers */
284 channel_allocation = 0x1F;
285 chan = MSM_HDMI_AUDIO_CHANNEL_8;
291 switch (params->sample_rate) {
293 rate = HDMI_SAMPLE_RATE_32KHZ;
296 rate = HDMI_SAMPLE_RATE_44_1KHZ;
299 rate = HDMI_SAMPLE_RATE_48KHZ;
302 rate = HDMI_SAMPLE_RATE_88_2KHZ;
305 rate = HDMI_SAMPLE_RATE_96KHZ;
308 rate = HDMI_SAMPLE_RATE_176_4KHZ;
311 rate = HDMI_SAMPLE_RATE_192KHZ;
314 DRM_DEV_ERROR(dev, "rate[%d] not supported!\n",
315 params->sample_rate);
319 msm_hdmi_audio_set_sample_rate(hdmi, rate);
320 msm_hdmi_audio_info_setup(hdmi, 1, chan, channel_allocation,
321 level_shift, down_mix);
326 static void msm_hdmi_audio_shutdown(struct device *dev, void *data)
328 struct hdmi *hdmi = dev_get_drvdata(dev);
330 msm_hdmi_audio_info_setup(hdmi, 0, 0, 0, 0, 0);
333 static const struct hdmi_codec_ops msm_hdmi_audio_codec_ops = {
334 .hw_params = msm_hdmi_audio_hw_params,
335 .audio_shutdown = msm_hdmi_audio_shutdown,
338 static struct hdmi_codec_pdata codec_data = {
339 .ops = &msm_hdmi_audio_codec_ops,
340 .max_i2s_channels = 8,
344 static int msm_hdmi_register_audio_driver(struct hdmi *hdmi, struct device *dev)
346 hdmi->audio_pdev = platform_device_register_data(dev,
351 return PTR_ERR_OR_ZERO(hdmi->audio_pdev);
354 static int msm_hdmi_bind(struct device *dev, struct device *master, void *data)
356 struct msm_drm_private *priv = dev_get_drvdata(master);
357 struct hdmi *hdmi = dev_get_drvdata(dev);
360 err = msm_hdmi_init(hdmi);
365 err = msm_hdmi_register_audio_driver(hdmi, dev);
367 DRM_ERROR("Failed to attach an audio codec %d\n", err);
368 hdmi->audio_pdev = NULL;
374 static void msm_hdmi_unbind(struct device *dev, struct device *master,
377 struct msm_drm_private *priv = dev_get_drvdata(master);
380 if (priv->hdmi->audio_pdev)
381 platform_device_unregister(priv->hdmi->audio_pdev);
383 if (priv->hdmi->bridge)
384 msm_hdmi_hpd_disable(priv->hdmi);
386 msm_hdmi_destroy(priv->hdmi);
391 static const struct component_ops msm_hdmi_ops = {
392 .bind = msm_hdmi_bind,
393 .unbind = msm_hdmi_unbind,
396 static int msm_hdmi_dev_probe(struct platform_device *pdev)
398 const struct hdmi_platform_config *config;
399 struct device *dev = &pdev->dev;
401 struct resource *res;
404 config = of_device_get_match_data(dev);
408 hdmi = devm_kzalloc(&pdev->dev, sizeof(*hdmi), GFP_KERNEL);
413 hdmi->config = config;
414 spin_lock_init(&hdmi->reg_lock);
416 ret = drm_of_find_panel_or_bridge(pdev->dev.of_node, 1, 0, NULL, &hdmi->next_bridge);
417 if (ret && ret != -ENODEV)
420 hdmi->mmio = msm_ioremap(pdev, "core_physical");
421 if (IS_ERR(hdmi->mmio))
422 return PTR_ERR(hdmi->mmio);
424 /* HDCP needs physical address of hdmi register */
425 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
429 hdmi->mmio_phy_addr = res->start;
431 hdmi->qfprom_mmio = msm_ioremap(pdev, "qfprom_physical");
432 if (IS_ERR(hdmi->qfprom_mmio)) {
433 DRM_DEV_INFO(&pdev->dev, "can't find qfprom resource\n");
434 hdmi->qfprom_mmio = NULL;
437 hdmi->irq = platform_get_irq(pdev, 0);
441 hdmi->hpd_regs = devm_kcalloc(&pdev->dev,
443 sizeof(hdmi->hpd_regs[0]),
448 for (i = 0; i < config->hpd_reg_cnt; i++)
449 hdmi->hpd_regs[i].supply = config->hpd_reg_names[i];
451 ret = devm_regulator_bulk_get(&pdev->dev, config->hpd_reg_cnt, hdmi->hpd_regs);
453 return dev_err_probe(dev, ret, "failed to get hpd regulators\n");
455 hdmi->pwr_regs = devm_kcalloc(&pdev->dev,
457 sizeof(hdmi->pwr_regs[0]),
462 for (i = 0; i < config->pwr_reg_cnt; i++)
463 hdmi->pwr_regs[i].supply = config->pwr_reg_names[i];
465 ret = devm_regulator_bulk_get(&pdev->dev, config->pwr_reg_cnt, hdmi->pwr_regs);
467 return dev_err_probe(dev, ret, "failed to get pwr regulators\n");
469 hdmi->hpd_clks = devm_kcalloc(&pdev->dev,
471 sizeof(hdmi->hpd_clks[0]),
476 for (i = 0; i < config->hpd_clk_cnt; i++) {
479 clk = msm_clk_get(pdev, config->hpd_clk_names[i]);
481 return dev_err_probe(dev, PTR_ERR(clk),
482 "failed to get hpd clk: %s\n",
483 config->hpd_clk_names[i]);
485 hdmi->hpd_clks[i] = clk;
488 hdmi->pwr_clks = devm_kcalloc(&pdev->dev,
490 sizeof(hdmi->pwr_clks[0]),
495 for (i = 0; i < config->pwr_clk_cnt; i++) {
498 clk = msm_clk_get(pdev, config->pwr_clk_names[i]);
500 return dev_err_probe(dev, PTR_ERR(clk),
501 "failed to get pwr clk: %s\n",
502 config->pwr_clk_names[i]);
504 hdmi->pwr_clks[i] = clk;
507 hdmi->hpd_gpiod = devm_gpiod_get_optional(&pdev->dev, "hpd", GPIOD_IN);
508 /* This will catch e.g. -EPROBE_DEFER */
509 if (IS_ERR(hdmi->hpd_gpiod))
510 return dev_err_probe(dev, PTR_ERR(hdmi->hpd_gpiod),
511 "failed to get hpd gpio\n");
513 if (!hdmi->hpd_gpiod)
514 DBG("failed to get HPD gpio");
517 gpiod_set_consumer_name(hdmi->hpd_gpiod, "HDMI_HPD");
519 ret = msm_hdmi_get_phy(hdmi);
521 DRM_DEV_ERROR(&pdev->dev, "failed to get phy\n");
525 ret = devm_pm_runtime_enable(&pdev->dev);
529 platform_set_drvdata(pdev, hdmi);
531 ret = component_add(&pdev->dev, &msm_hdmi_ops);
538 msm_hdmi_put_phy(hdmi);
542 static void msm_hdmi_dev_remove(struct platform_device *pdev)
544 struct hdmi *hdmi = dev_get_drvdata(&pdev->dev);
546 component_del(&pdev->dev, &msm_hdmi_ops);
548 msm_hdmi_put_phy(hdmi);
551 static const struct of_device_id msm_hdmi_dt_match[] = {
552 { .compatible = "qcom,hdmi-tx-8998", .data = &hdmi_tx_8974_config },
553 { .compatible = "qcom,hdmi-tx-8996", .data = &hdmi_tx_8974_config },
554 { .compatible = "qcom,hdmi-tx-8994", .data = &hdmi_tx_8974_config },
555 { .compatible = "qcom,hdmi-tx-8084", .data = &hdmi_tx_8974_config },
556 { .compatible = "qcom,hdmi-tx-8974", .data = &hdmi_tx_8974_config },
557 { .compatible = "qcom,hdmi-tx-8960", .data = &hdmi_tx_8960_config },
558 { .compatible = "qcom,hdmi-tx-8660", .data = &hdmi_tx_8960_config },
562 static struct platform_driver msm_hdmi_driver = {
563 .probe = msm_hdmi_dev_probe,
564 .remove = msm_hdmi_dev_remove,
567 .of_match_table = msm_hdmi_dt_match,
571 void __init msm_hdmi_register(void)
573 msm_hdmi_phy_driver_register();
574 platform_driver_register(&msm_hdmi_driver);
577 void __exit msm_hdmi_unregister(void)
579 platform_driver_unregister(&msm_hdmi_driver);
580 msm_hdmi_phy_driver_unregister();