2 * Copyright (c) 2014 The Linux Foundation. All rights reserved.
3 * Copyright (C) 2013 Red Hat
4 * Author: Rob Clark <robdclark@gmail.com>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <linux/of_irq.h>
20 #include <linux/of_gpio.h>
22 #include <sound/hdmi-codec.h>
25 void msm_hdmi_set_mode(struct hdmi *hdmi, bool power_on)
30 spin_lock_irqsave(&hdmi->reg_lock, flags);
32 ctrl |= HDMI_CTRL_ENABLE;
33 if (!hdmi->hdmi_mode) {
34 ctrl |= HDMI_CTRL_HDMI;
35 hdmi_write(hdmi, REG_HDMI_CTRL, ctrl);
36 ctrl &= ~HDMI_CTRL_HDMI;
38 ctrl |= HDMI_CTRL_HDMI;
41 ctrl = HDMI_CTRL_HDMI;
44 hdmi_write(hdmi, REG_HDMI_CTRL, ctrl);
45 spin_unlock_irqrestore(&hdmi->reg_lock, flags);
46 DBG("HDMI Core: %s, HDMI_CTRL=0x%08x",
47 power_on ? "Enable" : "Disable", ctrl);
50 static irqreturn_t msm_hdmi_irq(int irq, void *dev_id)
52 struct hdmi *hdmi = dev_id;
55 msm_hdmi_connector_irq(hdmi->connector);
58 msm_hdmi_i2c_irq(hdmi->i2c);
62 msm_hdmi_hdcp_irq(hdmi->hdcp_ctrl);
69 static void msm_hdmi_destroy(struct hdmi *hdmi)
72 * at this point, hpd has been disabled,
73 * after flush workq, it's safe to deinit hdcp
76 flush_workqueue(hdmi->workq);
77 destroy_workqueue(hdmi->workq);
79 msm_hdmi_hdcp_destroy(hdmi);
82 put_device(hdmi->phy_dev);
88 msm_hdmi_i2c_destroy(hdmi->i2c);
90 platform_set_drvdata(hdmi->pdev, NULL);
93 static int msm_hdmi_get_phy(struct hdmi *hdmi)
95 struct platform_device *pdev = hdmi->pdev;
96 struct platform_device *phy_pdev;
97 struct device_node *phy_node;
99 phy_node = of_parse_phandle(pdev->dev.of_node, "phys", 0);
101 dev_err(&pdev->dev, "cannot find phy device\n");
105 phy_pdev = of_find_device_by_node(phy_node);
107 hdmi->phy = platform_get_drvdata(phy_pdev);
109 of_node_put(phy_node);
111 if (!phy_pdev || !hdmi->phy) {
112 dev_err(&pdev->dev, "phy driver is not ready\n");
113 return -EPROBE_DEFER;
116 hdmi->phy_dev = get_device(&phy_pdev->dev);
121 /* construct hdmi at bind/probe time, grab all the resources. If
122 * we are to EPROBE_DEFER we want to do it here, rather than later
123 * at modeset_init() time
125 static struct hdmi *msm_hdmi_init(struct platform_device *pdev)
127 struct hdmi_platform_config *config = pdev->dev.platform_data;
128 struct hdmi *hdmi = NULL;
129 struct resource *res;
132 hdmi = devm_kzalloc(&pdev->dev, sizeof(*hdmi), GFP_KERNEL);
139 hdmi->config = config;
140 spin_lock_init(&hdmi->reg_lock);
142 hdmi->mmio = msm_ioremap(pdev, config->mmio_name, "HDMI");
143 if (IS_ERR(hdmi->mmio)) {
144 ret = PTR_ERR(hdmi->mmio);
148 /* HDCP needs physical address of hdmi register */
149 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
155 hdmi->mmio_phy_addr = res->start;
157 hdmi->qfprom_mmio = msm_ioremap(pdev,
158 config->qfprom_mmio_name, "HDMI_QFPROM");
159 if (IS_ERR(hdmi->qfprom_mmio)) {
160 dev_info(&pdev->dev, "can't find qfprom resource\n");
161 hdmi->qfprom_mmio = NULL;
164 hdmi->hpd_regs = devm_kcalloc(&pdev->dev,
166 sizeof(hdmi->hpd_regs[0]),
168 if (!hdmi->hpd_regs) {
172 for (i = 0; i < config->hpd_reg_cnt; i++) {
173 struct regulator *reg;
175 reg = devm_regulator_get(&pdev->dev,
176 config->hpd_reg_names[i]);
179 dev_err(&pdev->dev, "failed to get hpd regulator: %s (%d)\n",
180 config->hpd_reg_names[i], ret);
184 hdmi->hpd_regs[i] = reg;
187 hdmi->pwr_regs = devm_kcalloc(&pdev->dev,
189 sizeof(hdmi->pwr_regs[0]),
191 if (!hdmi->pwr_regs) {
195 for (i = 0; i < config->pwr_reg_cnt; i++) {
196 struct regulator *reg;
198 reg = devm_regulator_get(&pdev->dev,
199 config->pwr_reg_names[i]);
202 dev_err(&pdev->dev, "failed to get pwr regulator: %s (%d)\n",
203 config->pwr_reg_names[i], ret);
207 hdmi->pwr_regs[i] = reg;
210 hdmi->hpd_clks = devm_kcalloc(&pdev->dev,
212 sizeof(hdmi->hpd_clks[0]),
214 if (!hdmi->hpd_clks) {
218 for (i = 0; i < config->hpd_clk_cnt; i++) {
221 clk = msm_clk_get(pdev, config->hpd_clk_names[i]);
224 dev_err(&pdev->dev, "failed to get hpd clk: %s (%d)\n",
225 config->hpd_clk_names[i], ret);
229 hdmi->hpd_clks[i] = clk;
232 hdmi->pwr_clks = devm_kcalloc(&pdev->dev,
234 sizeof(hdmi->pwr_clks[0]),
236 if (!hdmi->pwr_clks) {
240 for (i = 0; i < config->pwr_clk_cnt; i++) {
243 clk = msm_clk_get(pdev, config->pwr_clk_names[i]);
246 dev_err(&pdev->dev, "failed to get pwr clk: %s (%d)\n",
247 config->pwr_clk_names[i], ret);
251 hdmi->pwr_clks[i] = clk;
254 pm_runtime_enable(&pdev->dev);
256 hdmi->workq = alloc_ordered_workqueue("msm_hdmi", 0);
258 hdmi->i2c = msm_hdmi_i2c_init(hdmi);
259 if (IS_ERR(hdmi->i2c)) {
260 ret = PTR_ERR(hdmi->i2c);
261 dev_err(&pdev->dev, "failed to get i2c: %d\n", ret);
266 ret = msm_hdmi_get_phy(hdmi);
268 dev_err(&pdev->dev, "failed to get phy\n");
272 hdmi->hdcp_ctrl = msm_hdmi_hdcp_init(hdmi);
273 if (IS_ERR(hdmi->hdcp_ctrl)) {
274 dev_warn(&pdev->dev, "failed to init hdcp: disabled\n");
275 hdmi->hdcp_ctrl = NULL;
282 msm_hdmi_destroy(hdmi);
287 /* Second part of initialization, the drm/kms level modeset_init,
288 * constructs/initializes mode objects, etc, is called from master
289 * driver (not hdmi sub-device's probe/bind!)
291 * Any resource (regulator/clk/etc) which could be missing at boot
292 * should be handled in msm_hdmi_init() so that failure happens from
293 * hdmi sub-device's probe.
295 int msm_hdmi_modeset_init(struct hdmi *hdmi,
296 struct drm_device *dev, struct drm_encoder *encoder)
298 struct msm_drm_private *priv = dev->dev_private;
299 struct platform_device *pdev = hdmi->pdev;
302 if (priv->num_bridges == ARRAY_SIZE(priv->bridges)) {
303 DRM_DEV_ERROR(dev->dev, "too many bridges\n");
308 hdmi->encoder = encoder;
310 hdmi_audio_infoframe_init(&hdmi->audio.infoframe);
312 hdmi->bridge = msm_hdmi_bridge_init(hdmi);
313 if (IS_ERR(hdmi->bridge)) {
314 ret = PTR_ERR(hdmi->bridge);
315 dev_err(dev->dev, "failed to create HDMI bridge: %d\n", ret);
320 hdmi->connector = msm_hdmi_connector_init(hdmi);
321 if (IS_ERR(hdmi->connector)) {
322 ret = PTR_ERR(hdmi->connector);
323 dev_err(dev->dev, "failed to create HDMI connector: %d\n", ret);
324 hdmi->connector = NULL;
328 hdmi->irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
331 dev_err(dev->dev, "failed to get irq: %d\n", ret);
335 ret = devm_request_irq(&pdev->dev, hdmi->irq,
336 msm_hdmi_irq, IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
339 dev_err(dev->dev, "failed to request IRQ%u: %d\n",
344 ret = msm_hdmi_hpd_enable(hdmi->connector);
346 DRM_DEV_ERROR(&hdmi->pdev->dev, "failed to enable HPD: %d\n", ret);
350 encoder->bridge = hdmi->bridge;
352 priv->bridges[priv->num_bridges++] = hdmi->bridge;
353 priv->connectors[priv->num_connectors++] = hdmi->connector;
355 platform_set_drvdata(pdev, hdmi);
360 /* bridge is normally destroyed by drm: */
362 msm_hdmi_bridge_destroy(hdmi->bridge);
365 if (hdmi->connector) {
366 hdmi->connector->funcs->destroy(hdmi->connector);
367 hdmi->connector = NULL;
377 #define HDMI_CFG(item, entry) \
378 .item ## _names = item ##_names_ ## entry, \
379 .item ## _cnt = ARRAY_SIZE(item ## _names_ ## entry)
381 static const char *pwr_reg_names_none[] = {};
382 static const char *hpd_reg_names_none[] = {};
384 static struct hdmi_platform_config hdmi_tx_8660_config;
386 static const char *hpd_reg_names_8960[] = {"core-vdda", "hdmi-mux"};
387 static const char *hpd_clk_names_8960[] = {"core", "master_iface", "slave_iface"};
389 static struct hdmi_platform_config hdmi_tx_8960_config = {
390 HDMI_CFG(hpd_reg, 8960),
391 HDMI_CFG(hpd_clk, 8960),
394 static const char *pwr_reg_names_8x74[] = {"core-vdda", "core-vcc"};
395 static const char *hpd_reg_names_8x74[] = {"hpd-gdsc", "hpd-5v"};
396 static const char *pwr_clk_names_8x74[] = {"extp", "alt_iface"};
397 static const char *hpd_clk_names_8x74[] = {"iface", "core", "mdp_core"};
398 static unsigned long hpd_clk_freq_8x74[] = {0, 19200000, 0};
400 static struct hdmi_platform_config hdmi_tx_8974_config = {
401 HDMI_CFG(pwr_reg, 8x74),
402 HDMI_CFG(hpd_reg, 8x74),
403 HDMI_CFG(pwr_clk, 8x74),
404 HDMI_CFG(hpd_clk, 8x74),
405 .hpd_freq = hpd_clk_freq_8x74,
408 static const char *hpd_reg_names_8084[] = {"hpd-gdsc", "hpd-5v", "hpd-5v-en"};
410 static struct hdmi_platform_config hdmi_tx_8084_config = {
411 HDMI_CFG(pwr_reg, 8x74),
412 HDMI_CFG(hpd_reg, 8084),
413 HDMI_CFG(pwr_clk, 8x74),
414 HDMI_CFG(hpd_clk, 8x74),
415 .hpd_freq = hpd_clk_freq_8x74,
418 static struct hdmi_platform_config hdmi_tx_8994_config = {
419 HDMI_CFG(pwr_reg, 8x74),
420 HDMI_CFG(hpd_reg, none),
421 HDMI_CFG(pwr_clk, 8x74),
422 HDMI_CFG(hpd_clk, 8x74),
423 .hpd_freq = hpd_clk_freq_8x74,
426 static struct hdmi_platform_config hdmi_tx_8996_config = {
427 HDMI_CFG(pwr_reg, none),
428 HDMI_CFG(hpd_reg, none),
429 HDMI_CFG(pwr_clk, 8x74),
430 HDMI_CFG(hpd_clk, 8x74),
431 .hpd_freq = hpd_clk_freq_8x74,
434 static const struct {
439 } msm_hdmi_gpio_pdata[] = {
440 { "qcom,hdmi-tx-ddc-clk", true, 1, "HDMI_DDC_CLK" },
441 { "qcom,hdmi-tx-ddc-data", true, 1, "HDMI_DDC_DATA" },
442 { "qcom,hdmi-tx-hpd", false, 1, "HDMI_HPD" },
443 { "qcom,hdmi-tx-mux-en", true, 1, "HDMI_MUX_EN" },
444 { "qcom,hdmi-tx-mux-sel", true, 0, "HDMI_MUX_SEL" },
445 { "qcom,hdmi-tx-mux-lpm", true, 1, "HDMI_MUX_LPM" },
448 static int msm_hdmi_get_gpio(struct device_node *of_node, const char *name)
452 /* try with the gpio names as in the table (downstream bindings) */
453 gpio = of_get_named_gpio(of_node, name, 0);
457 /* try with the gpio names as in the upstream bindings */
458 snprintf(name2, sizeof(name2), "%s-gpios", name);
459 gpio = of_get_named_gpio(of_node, name2, 0);
464 * try again after stripping out the "qcom,hdmi-tx"
465 * prefix. This is mainly to match "hpd-gpios" used
466 * in the upstream bindings
468 if (sscanf(name2, "qcom,hdmi-tx-%s", name3))
469 gpio = of_get_named_gpio(of_node, name3, 0);
473 DBG("failed to get gpio: %s (%d)", name, gpio);
481 * HDMI audio codec callbacks
483 static int msm_hdmi_audio_hw_params(struct device *dev, void *data,
484 struct hdmi_codec_daifmt *daifmt,
485 struct hdmi_codec_params *params)
487 struct hdmi *hdmi = dev_get_drvdata(dev);
489 unsigned int channel_allocation = 0;
491 unsigned int level_shift = 0; /* 0dB */
492 bool down_mix = false;
494 dev_dbg(dev, "%u Hz, %d bit, %d channels\n", params->sample_rate,
495 params->sample_width, params->cea.channels);
497 switch (params->cea.channels) {
499 /* FR and FL speakers */
500 channel_allocation = 0;
501 chan = MSM_HDMI_AUDIO_CHANNEL_2;
504 /* FC, LFE, FR and FL speakers */
505 channel_allocation = 0x3;
506 chan = MSM_HDMI_AUDIO_CHANNEL_4;
509 /* RR, RL, FC, LFE, FR and FL speakers */
510 channel_allocation = 0x0B;
511 chan = MSM_HDMI_AUDIO_CHANNEL_6;
514 /* FRC, FLC, RR, RL, FC, LFE, FR and FL speakers */
515 channel_allocation = 0x1F;
516 chan = MSM_HDMI_AUDIO_CHANNEL_8;
522 switch (params->sample_rate) {
524 rate = HDMI_SAMPLE_RATE_32KHZ;
527 rate = HDMI_SAMPLE_RATE_44_1KHZ;
530 rate = HDMI_SAMPLE_RATE_48KHZ;
533 rate = HDMI_SAMPLE_RATE_88_2KHZ;
536 rate = HDMI_SAMPLE_RATE_96KHZ;
539 rate = HDMI_SAMPLE_RATE_176_4KHZ;
542 rate = HDMI_SAMPLE_RATE_192KHZ;
545 dev_err(dev, "rate[%d] not supported!\n",
546 params->sample_rate);
550 msm_hdmi_audio_set_sample_rate(hdmi, rate);
551 msm_hdmi_audio_info_setup(hdmi, 1, chan, channel_allocation,
552 level_shift, down_mix);
557 static void msm_hdmi_audio_shutdown(struct device *dev, void *data)
559 struct hdmi *hdmi = dev_get_drvdata(dev);
561 msm_hdmi_audio_info_setup(hdmi, 0, 0, 0, 0, 0);
564 static const struct hdmi_codec_ops msm_hdmi_audio_codec_ops = {
565 .hw_params = msm_hdmi_audio_hw_params,
566 .audio_shutdown = msm_hdmi_audio_shutdown,
569 static struct hdmi_codec_pdata codec_data = {
570 .ops = &msm_hdmi_audio_codec_ops,
571 .max_i2s_channels = 8,
575 static int msm_hdmi_register_audio_driver(struct hdmi *hdmi, struct device *dev)
577 hdmi->audio_pdev = platform_device_register_data(dev,
582 return PTR_ERR_OR_ZERO(hdmi->audio_pdev);
585 static int msm_hdmi_bind(struct device *dev, struct device *master, void *data)
587 struct drm_device *drm = dev_get_drvdata(master);
588 struct msm_drm_private *priv = drm->dev_private;
589 static struct hdmi_platform_config *hdmi_cfg;
591 struct device_node *of_node = dev->of_node;
594 hdmi_cfg = (struct hdmi_platform_config *)
595 of_device_get_match_data(dev);
597 dev_err(dev, "unknown hdmi_cfg: %s\n", of_node->name);
601 hdmi_cfg->mmio_name = "core_physical";
602 hdmi_cfg->qfprom_mmio_name = "qfprom_physical";
604 for (i = 0; i < HDMI_MAX_NUM_GPIO; i++) {
605 hdmi_cfg->gpios[i].num = msm_hdmi_get_gpio(of_node,
606 msm_hdmi_gpio_pdata[i].name);
607 hdmi_cfg->gpios[i].output = msm_hdmi_gpio_pdata[i].output;
608 hdmi_cfg->gpios[i].value = msm_hdmi_gpio_pdata[i].value;
609 hdmi_cfg->gpios[i].label = msm_hdmi_gpio_pdata[i].label;
612 dev->platform_data = hdmi_cfg;
614 hdmi = msm_hdmi_init(to_platform_device(dev));
616 return PTR_ERR(hdmi);
619 err = msm_hdmi_register_audio_driver(hdmi, dev);
621 DRM_ERROR("Failed to attach an audio codec %d\n", err);
622 hdmi->audio_pdev = NULL;
628 static void msm_hdmi_unbind(struct device *dev, struct device *master,
631 struct drm_device *drm = dev_get_drvdata(master);
632 struct msm_drm_private *priv = drm->dev_private;
634 if (priv->hdmi->audio_pdev)
635 platform_device_unregister(priv->hdmi->audio_pdev);
637 msm_hdmi_destroy(priv->hdmi);
642 static const struct component_ops msm_hdmi_ops = {
643 .bind = msm_hdmi_bind,
644 .unbind = msm_hdmi_unbind,
647 static int msm_hdmi_dev_probe(struct platform_device *pdev)
649 return component_add(&pdev->dev, &msm_hdmi_ops);
652 static int msm_hdmi_dev_remove(struct platform_device *pdev)
654 component_del(&pdev->dev, &msm_hdmi_ops);
658 static const struct of_device_id msm_hdmi_dt_match[] = {
659 { .compatible = "qcom,hdmi-tx-8996", .data = &hdmi_tx_8996_config },
660 { .compatible = "qcom,hdmi-tx-8994", .data = &hdmi_tx_8994_config },
661 { .compatible = "qcom,hdmi-tx-8084", .data = &hdmi_tx_8084_config },
662 { .compatible = "qcom,hdmi-tx-8974", .data = &hdmi_tx_8974_config },
663 { .compatible = "qcom,hdmi-tx-8960", .data = &hdmi_tx_8960_config },
664 { .compatible = "qcom,hdmi-tx-8660", .data = &hdmi_tx_8660_config },
668 static struct platform_driver msm_hdmi_driver = {
669 .probe = msm_hdmi_dev_probe,
670 .remove = msm_hdmi_dev_remove,
673 .of_match_table = msm_hdmi_dt_match,
677 void __init msm_hdmi_register(void)
679 msm_hdmi_phy_driver_register();
680 platform_driver_register(&msm_hdmi_driver);
683 void __exit msm_hdmi_unregister(void)
685 platform_driver_unregister(&msm_hdmi_driver);
686 msm_hdmi_phy_driver_unregister();