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 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 hdmi_irq(int irq, void *dev_id)
52 struct hdmi *hdmi = dev_id;
55 hdmi_connector_irq(hdmi->connector);
58 hdmi_i2c_irq(hdmi->i2c);
62 hdmi_hdcp_irq(hdmi->hdcp_ctrl);
69 static void hdmi_destroy(struct hdmi *hdmi)
71 struct hdmi_phy *phy = hdmi->phy;
74 * at this point, hpd has been disabled,
75 * after flush workq, it's safe to deinit hdcp
78 flush_workqueue(hdmi->workq);
79 destroy_workqueue(hdmi->workq);
81 hdmi_hdcp_destroy(hdmi);
83 phy->funcs->destroy(phy);
86 hdmi_i2c_destroy(hdmi->i2c);
88 platform_set_drvdata(hdmi->pdev, NULL);
91 /* construct hdmi at bind/probe time, grab all the resources. If
92 * we are to EPROBE_DEFER we want to do it here, rather than later
93 * at modeset_init() time
95 static struct hdmi *hdmi_init(struct platform_device *pdev)
97 struct hdmi_platform_config *config = pdev->dev.platform_data;
98 struct hdmi *hdmi = NULL;
102 hdmi = devm_kzalloc(&pdev->dev, sizeof(*hdmi), GFP_KERNEL);
109 hdmi->config = config;
110 spin_lock_init(&hdmi->reg_lock);
112 /* not sure about which phy maps to which msm.. probably I miss some */
113 if (config->phy_init) {
114 hdmi->phy = config->phy_init(hdmi);
116 if (IS_ERR(hdmi->phy)) {
117 ret = PTR_ERR(hdmi->phy);
118 dev_err(&pdev->dev, "failed to load phy: %d\n", ret);
124 hdmi->mmio = msm_ioremap(pdev, config->mmio_name, "HDMI");
125 if (IS_ERR(hdmi->mmio)) {
126 ret = PTR_ERR(hdmi->mmio);
130 /* HDCP needs physical address of hdmi register */
131 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
133 hdmi->mmio_phy_addr = res->start;
135 hdmi->qfprom_mmio = msm_ioremap(pdev,
136 config->qfprom_mmio_name, "HDMI_QFPROM");
137 if (IS_ERR(hdmi->qfprom_mmio)) {
138 dev_info(&pdev->dev, "can't find qfprom resource\n");
139 hdmi->qfprom_mmio = NULL;
142 hdmi->hpd_regs = devm_kzalloc(&pdev->dev, sizeof(hdmi->hpd_regs[0]) *
143 config->hpd_reg_cnt, GFP_KERNEL);
144 if (!hdmi->hpd_regs) {
148 for (i = 0; i < config->hpd_reg_cnt; i++) {
149 struct regulator *reg;
151 reg = devm_regulator_get(&pdev->dev,
152 config->hpd_reg_names[i]);
155 dev_err(&pdev->dev, "failed to get hpd regulator: %s (%d)\n",
156 config->hpd_reg_names[i], ret);
160 hdmi->hpd_regs[i] = reg;
163 hdmi->pwr_regs = devm_kzalloc(&pdev->dev, sizeof(hdmi->pwr_regs[0]) *
164 config->pwr_reg_cnt, GFP_KERNEL);
165 if (!hdmi->pwr_regs) {
169 for (i = 0; i < config->pwr_reg_cnt; i++) {
170 struct regulator *reg;
172 reg = devm_regulator_get(&pdev->dev,
173 config->pwr_reg_names[i]);
176 dev_err(&pdev->dev, "failed to get pwr regulator: %s (%d)\n",
177 config->pwr_reg_names[i], ret);
181 hdmi->pwr_regs[i] = reg;
184 hdmi->hpd_clks = devm_kzalloc(&pdev->dev, sizeof(hdmi->hpd_clks[0]) *
185 config->hpd_clk_cnt, GFP_KERNEL);
186 if (!hdmi->hpd_clks) {
190 for (i = 0; i < config->hpd_clk_cnt; i++) {
193 clk = devm_clk_get(&pdev->dev, config->hpd_clk_names[i]);
196 dev_err(&pdev->dev, "failed to get hpd clk: %s (%d)\n",
197 config->hpd_clk_names[i], ret);
201 hdmi->hpd_clks[i] = clk;
204 hdmi->pwr_clks = devm_kzalloc(&pdev->dev, sizeof(hdmi->pwr_clks[0]) *
205 config->pwr_clk_cnt, GFP_KERNEL);
206 if (!hdmi->pwr_clks) {
210 for (i = 0; i < config->pwr_clk_cnt; i++) {
213 clk = devm_clk_get(&pdev->dev, config->pwr_clk_names[i]);
216 dev_err(&pdev->dev, "failed to get pwr clk: %s (%d)\n",
217 config->pwr_clk_names[i], ret);
221 hdmi->pwr_clks[i] = clk;
224 hdmi->workq = alloc_ordered_workqueue("msm_hdmi", 0);
226 hdmi->i2c = hdmi_i2c_init(hdmi);
227 if (IS_ERR(hdmi->i2c)) {
228 ret = PTR_ERR(hdmi->i2c);
229 dev_err(&pdev->dev, "failed to get i2c: %d\n", ret);
234 hdmi->hdcp_ctrl = hdmi_hdcp_init(hdmi);
235 if (IS_ERR(hdmi->hdcp_ctrl)) {
236 dev_warn(&pdev->dev, "failed to init hdcp: disabled\n");
237 hdmi->hdcp_ctrl = NULL;
249 /* Second part of initialization, the drm/kms level modeset_init,
250 * constructs/initializes mode objects, etc, is called from master
251 * driver (not hdmi sub-device's probe/bind!)
253 * Any resource (regulator/clk/etc) which could be missing at boot
254 * should be handled in hdmi_init() so that failure happens from
255 * hdmi sub-device's probe.
257 int hdmi_modeset_init(struct hdmi *hdmi,
258 struct drm_device *dev, struct drm_encoder *encoder)
260 struct msm_drm_private *priv = dev->dev_private;
261 struct platform_device *pdev = hdmi->pdev;
265 hdmi->encoder = encoder;
267 hdmi_audio_infoframe_init(&hdmi->audio.infoframe);
269 hdmi->bridge = hdmi_bridge_init(hdmi);
270 if (IS_ERR(hdmi->bridge)) {
271 ret = PTR_ERR(hdmi->bridge);
272 dev_err(dev->dev, "failed to create HDMI bridge: %d\n", ret);
277 hdmi->connector = hdmi_connector_init(hdmi);
278 if (IS_ERR(hdmi->connector)) {
279 ret = PTR_ERR(hdmi->connector);
280 dev_err(dev->dev, "failed to create HDMI connector: %d\n", ret);
281 hdmi->connector = NULL;
285 hdmi->irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
288 dev_err(dev->dev, "failed to get irq: %d\n", ret);
292 ret = devm_request_irq(&pdev->dev, hdmi->irq,
293 hdmi_irq, IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
296 dev_err(dev->dev, "failed to request IRQ%u: %d\n",
301 encoder->bridge = hdmi->bridge;
303 priv->bridges[priv->num_bridges++] = hdmi->bridge;
304 priv->connectors[priv->num_connectors++] = hdmi->connector;
306 platform_set_drvdata(pdev, hdmi);
311 /* bridge is normally destroyed by drm: */
313 hdmi_bridge_destroy(hdmi->bridge);
316 if (hdmi->connector) {
317 hdmi->connector->funcs->destroy(hdmi->connector);
318 hdmi->connector = NULL;
328 #include <linux/of_gpio.h>
330 #define HDMI_CFG(item, entry) \
331 .item ## _names = item ##_names_ ## entry, \
332 .item ## _cnt = ARRAY_SIZE(item ## _names_ ## entry)
334 static const char *pwr_reg_names_none[] = {};
335 static const char *hpd_reg_names_none[] = {};
337 static struct hdmi_platform_config hdmi_tx_8660_config = {
338 .phy_init = hdmi_phy_8x60_init,
341 static const char *hpd_reg_names_8960[] = {"core-vdda", "hdmi-mux"};
342 static const char *hpd_clk_names_8960[] = {"core_clk", "master_iface_clk", "slave_iface_clk"};
344 static struct hdmi_platform_config hdmi_tx_8960_config = {
345 .phy_init = hdmi_phy_8960_init,
346 HDMI_CFG(hpd_reg, 8960),
347 HDMI_CFG(hpd_clk, 8960),
350 static const char *pwr_reg_names_8x74[] = {"core-vdda", "core-vcc"};
351 static const char *hpd_reg_names_8x74[] = {"hpd-gdsc", "hpd-5v"};
352 static const char *pwr_clk_names_8x74[] = {"extp_clk", "alt_iface_clk"};
353 static const char *hpd_clk_names_8x74[] = {"iface_clk", "core_clk", "mdp_core_clk"};
354 static unsigned long hpd_clk_freq_8x74[] = {0, 19200000, 0};
356 static struct hdmi_platform_config hdmi_tx_8974_config = {
357 .phy_init = hdmi_phy_8x74_init,
358 HDMI_CFG(pwr_reg, 8x74),
359 HDMI_CFG(hpd_reg, 8x74),
360 HDMI_CFG(pwr_clk, 8x74),
361 HDMI_CFG(hpd_clk, 8x74),
362 .hpd_freq = hpd_clk_freq_8x74,
365 static const char *hpd_reg_names_8084[] = {"hpd-gdsc", "hpd-5v", "hpd-5v-en"};
367 static struct hdmi_platform_config hdmi_tx_8084_config = {
368 .phy_init = hdmi_phy_8x74_init,
369 HDMI_CFG(pwr_reg, 8x74),
370 HDMI_CFG(hpd_reg, 8084),
371 HDMI_CFG(pwr_clk, 8x74),
372 HDMI_CFG(hpd_clk, 8x74),
373 .hpd_freq = hpd_clk_freq_8x74,
376 static struct hdmi_platform_config hdmi_tx_8994_config = {
377 .phy_init = NULL, /* nothing to do for this HDMI PHY 20nm */
378 HDMI_CFG(pwr_reg, 8x74),
379 HDMI_CFG(hpd_reg, none),
380 HDMI_CFG(pwr_clk, 8x74),
381 HDMI_CFG(hpd_clk, 8x74),
382 .hpd_freq = hpd_clk_freq_8x74,
385 static struct hdmi_platform_config hdmi_tx_8996_config = {
387 HDMI_CFG(pwr_reg, none),
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 const struct of_device_id dt_match[] = {
395 { .compatible = "qcom,hdmi-tx-8996", .data = &hdmi_tx_8996_config },
396 { .compatible = "qcom,hdmi-tx-8994", .data = &hdmi_tx_8994_config },
397 { .compatible = "qcom,hdmi-tx-8084", .data = &hdmi_tx_8084_config },
398 { .compatible = "qcom,hdmi-tx-8974", .data = &hdmi_tx_8974_config },
399 { .compatible = "qcom,hdmi-tx-8960", .data = &hdmi_tx_8960_config },
400 { .compatible = "qcom,hdmi-tx-8660", .data = &hdmi_tx_8660_config },
405 static int get_gpio(struct device *dev, struct device_node *of_node, const char *name)
407 int gpio = of_get_named_gpio(of_node, name, 0);
410 snprintf(name2, sizeof(name2), "%s-gpio", name);
411 gpio = of_get_named_gpio(of_node, name2, 0);
413 DBG("failed to get gpio: %s (%d)", name, gpio);
422 * HDMI audio codec callbacks
424 static int msm_hdmi_audio_hw_params(struct device *dev,
425 struct hdmi_codec_daifmt *daifmt,
426 struct hdmi_codec_params *params)
428 struct hdmi *hdmi = dev_get_drvdata(dev);
430 unsigned int channel_allocation = 0;
432 unsigned int level_shift = 0; /* 0dB */
433 bool down_mix = false;
435 dev_dbg(dev, "%u Hz, %d bit, %d channels\n", params->sample_rate,
436 params->sample_width, params->cea.channels);
438 switch (params->cea.channels) {
440 /* FR and FL speakers */
441 channel_allocation = 0;
442 chan = MSM_HDMI_AUDIO_CHANNEL_2;
445 /* FC, LFE, FR and FL speakers */
446 channel_allocation = 0x3;
447 chan = MSM_HDMI_AUDIO_CHANNEL_4;
450 /* RR, RL, FC, LFE, FR and FL speakers */
451 channel_allocation = 0x0B;
452 chan = MSM_HDMI_AUDIO_CHANNEL_6;
455 /* FRC, FLC, RR, RL, FC, LFE, FR and FL speakers */
456 channel_allocation = 0x1F;
457 chan = MSM_HDMI_AUDIO_CHANNEL_8;
463 switch (params->sample_rate) {
465 rate = HDMI_SAMPLE_RATE_32KHZ;
468 rate = HDMI_SAMPLE_RATE_44_1KHZ;
471 rate = HDMI_SAMPLE_RATE_48KHZ;
474 rate = HDMI_SAMPLE_RATE_88_2KHZ;
477 rate = HDMI_SAMPLE_RATE_96KHZ;
480 rate = HDMI_SAMPLE_RATE_176_4KHZ;
483 rate = HDMI_SAMPLE_RATE_192KHZ;
486 dev_err(dev, "rate[%d] not supported!\n",
487 params->sample_rate);
491 hdmi_audio_set_sample_rate(hdmi, rate);
492 hdmi_audio_info_setup(hdmi, 1, chan, channel_allocation,
493 level_shift, down_mix);
498 static void msm_hdmi_audio_shutdown(struct device *dev)
500 struct hdmi *hdmi = dev_get_drvdata(dev);
502 hdmi_audio_info_setup(hdmi, 0, 0, 0, 0, 0);
505 static const struct hdmi_codec_ops msm_hdmi_audio_codec_ops = {
506 .hw_params = msm_hdmi_audio_hw_params,
507 .audio_shutdown = msm_hdmi_audio_shutdown,
510 static struct hdmi_codec_pdata codec_data = {
511 .ops = &msm_hdmi_audio_codec_ops,
512 .max_i2s_channels = 8,
516 static int msm_hdmi_register_audio_driver(struct hdmi *hdmi, struct device *dev)
518 hdmi->audio_pdev = platform_device_register_data(dev,
523 if (IS_ERR(hdmi->audio_pdev))
524 return PTR_ERR(hdmi->audio_pdev);
529 static int hdmi_bind(struct device *dev, struct device *master, void *data)
531 struct drm_device *drm = dev_get_drvdata(master);
532 struct msm_drm_private *priv = drm->dev_private;
533 static struct hdmi_platform_config *hdmi_cfg;
536 struct device_node *of_node = dev->of_node;
537 const struct of_device_id *match;
540 match = of_match_node(dt_match, of_node);
541 if (match && match->data) {
542 hdmi_cfg = (struct hdmi_platform_config *)match->data;
543 DBG("hdmi phy: %s", match->compatible);
545 dev_err(dev, "unknown phy: %s\n", of_node->name);
549 hdmi_cfg->mmio_name = "core_physical";
550 hdmi_cfg->qfprom_mmio_name = "qfprom_physical";
551 hdmi_cfg->ddc_clk_gpio = get_gpio(dev, of_node, "qcom,hdmi-tx-ddc-clk");
552 hdmi_cfg->ddc_data_gpio = get_gpio(dev, of_node, "qcom,hdmi-tx-ddc-data");
553 hdmi_cfg->hpd_gpio = get_gpio(dev, of_node, "qcom,hdmi-tx-hpd");
554 hdmi_cfg->mux_en_gpio = get_gpio(dev, of_node, "qcom,hdmi-tx-mux-en");
555 hdmi_cfg->mux_sel_gpio = get_gpio(dev, of_node, "qcom,hdmi-tx-mux-sel");
556 hdmi_cfg->mux_lpm_gpio = get_gpio(dev, of_node, "qcom,hdmi-tx-mux-lpm");
559 static struct hdmi_platform_config config = {};
560 static const char *hpd_clk_names[] = {
561 "core_clk", "master_iface_clk", "slave_iface_clk",
563 if (cpu_is_apq8064()) {
564 static const char *hpd_reg_names[] = {"8921_hdmi_mvs"};
565 config.phy_init = hdmi_phy_8960_init;
566 config.hpd_reg_names = hpd_reg_names;
567 config.hpd_reg_cnt = ARRAY_SIZE(hpd_reg_names);
568 config.hpd_clk_names = hpd_clk_names;
569 config.hpd_clk_cnt = ARRAY_SIZE(hpd_clk_names);
570 config.ddc_clk_gpio = 70;
571 config.ddc_data_gpio = 71;
572 config.hpd_gpio = 72;
573 config.mux_en_gpio = -1;
574 config.mux_sel_gpio = -1;
575 } else if (cpu_is_msm8960() || cpu_is_msm8960ab()) {
576 static const char *hpd_reg_names[] = {"8921_hdmi_mvs"};
577 config.phy_init = hdmi_phy_8960_init;
578 config.hpd_reg_names = hpd_reg_names;
579 config.hpd_reg_cnt = ARRAY_SIZE(hpd_reg_names);
580 config.hpd_clk_names = hpd_clk_names;
581 config.hpd_clk_cnt = ARRAY_SIZE(hpd_clk_names);
582 config.ddc_clk_gpio = 100;
583 config.ddc_data_gpio = 101;
584 config.hpd_gpio = 102;
585 config.mux_en_gpio = -1;
586 config.mux_sel_gpio = -1;
587 } else if (cpu_is_msm8x60()) {
588 static const char *hpd_reg_names[] = {
589 "8901_hdmi_mvs", "8901_mpp0"
591 config.phy_init = hdmi_phy_8x60_init;
592 config.hpd_reg_names = hpd_reg_names;
593 config.hpd_reg_cnt = ARRAY_SIZE(hpd_reg_names);
594 config.hpd_clk_names = hpd_clk_names;
595 config.hpd_clk_cnt = ARRAY_SIZE(hpd_clk_names);
596 config.ddc_clk_gpio = 170;
597 config.ddc_data_gpio = 171;
598 config.hpd_gpio = 172;
599 config.mux_en_gpio = -1;
600 config.mux_sel_gpio = -1;
602 config.mmio_name = "hdmi_msm_hdmi_addr";
603 config.qfprom_mmio_name = "hdmi_msm_qfprom_addr";
607 dev->platform_data = hdmi_cfg;
609 hdmi = hdmi_init(to_platform_device(dev));
611 return PTR_ERR(hdmi);
614 err = msm_hdmi_register_audio_driver(hdmi, dev);
616 DRM_ERROR("Failed to attach an audio codec %d\n", err);
617 hdmi->audio_pdev = NULL;
623 static void hdmi_unbind(struct device *dev, struct device *master,
626 struct drm_device *drm = dev_get_drvdata(master);
627 struct msm_drm_private *priv = drm->dev_private;
629 hdmi_destroy(priv->hdmi);
630 if (priv->hdmi->audio_pdev)
631 platform_device_unregister(priv->hdmi->audio_pdev);
637 static const struct component_ops hdmi_ops = {
639 .unbind = hdmi_unbind,
642 static int hdmi_dev_probe(struct platform_device *pdev)
644 return component_add(&pdev->dev, &hdmi_ops);
647 static int hdmi_dev_remove(struct platform_device *pdev)
649 component_del(&pdev->dev, &hdmi_ops);
653 static struct platform_driver hdmi_driver = {
654 .probe = hdmi_dev_probe,
655 .remove = hdmi_dev_remove,
658 .of_match_table = dt_match,
662 void __init hdmi_register(void)
664 platform_driver_register(&hdmi_driver);
667 void __exit hdmi_unregister(void)
669 platform_driver_unregister(&hdmi_driver);