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 <sound/hdmi-codec.h>
14 void msm_hdmi_set_mode(struct hdmi
*hdmi
, bool power_on
)
19 spin_lock_irqsave(&hdmi
->reg_lock
, flags
);
21 ctrl
|= HDMI_CTRL_ENABLE
;
22 if (!hdmi
->hdmi_mode
) {
23 ctrl
|= HDMI_CTRL_HDMI
;
24 hdmi_write(hdmi
, REG_HDMI_CTRL
, ctrl
);
25 ctrl
&= ~HDMI_CTRL_HDMI
;
27 ctrl
|= HDMI_CTRL_HDMI
;
30 ctrl
= HDMI_CTRL_HDMI
;
33 hdmi_write(hdmi
, REG_HDMI_CTRL
, ctrl
);
34 spin_unlock_irqrestore(&hdmi
->reg_lock
, flags
);
35 DBG("HDMI Core: %s, HDMI_CTRL=0x%08x",
36 power_on
? "Enable" : "Disable", ctrl
);
39 static irqreturn_t
msm_hdmi_irq(int irq
, void *dev_id
)
41 struct hdmi
*hdmi
= dev_id
;
44 msm_hdmi_connector_irq(hdmi
->connector
);
47 msm_hdmi_i2c_irq(hdmi
->i2c
);
51 msm_hdmi_hdcp_irq(hdmi
->hdcp_ctrl
);
58 static void msm_hdmi_destroy(struct hdmi
*hdmi
)
61 * at this point, hpd has been disabled,
62 * after flush workq, it's safe to deinit hdcp
65 flush_workqueue(hdmi
->workq
);
66 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
);
101 DRM_DEV_ERROR(&pdev
->dev
, "phy driver is not ready\n");
102 return -EPROBE_DEFER
;
105 DRM_DEV_ERROR(&pdev
->dev
, "phy driver is not ready\n");
106 put_device(&phy_pdev
->dev
);
107 return -EPROBE_DEFER
;
110 hdmi
->phy_dev
= get_device(&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 struct hdmi
*msm_hdmi_init(struct platform_device
*pdev
)
121 struct hdmi_platform_config
*config
= pdev
->dev
.platform_data
;
122 struct hdmi
*hdmi
= NULL
;
123 struct resource
*res
;
126 hdmi
= devm_kzalloc(&pdev
->dev
, sizeof(*hdmi
), GFP_KERNEL
);
133 hdmi
->config
= config
;
134 spin_lock_init(&hdmi
->reg_lock
);
136 hdmi
->mmio
= msm_ioremap(pdev
, config
->mmio_name
, "HDMI");
137 if (IS_ERR(hdmi
->mmio
)) {
138 ret
= PTR_ERR(hdmi
->mmio
);
142 /* HDCP needs physical address of hdmi register */
143 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
,
145 hdmi
->mmio_phy_addr
= res
->start
;
147 hdmi
->qfprom_mmio
= msm_ioremap(pdev
,
148 config
->qfprom_mmio_name
, "HDMI_QFPROM");
149 if (IS_ERR(hdmi
->qfprom_mmio
)) {
150 DRM_DEV_INFO(&pdev
->dev
, "can't find qfprom resource\n");
151 hdmi
->qfprom_mmio
= NULL
;
154 hdmi
->hpd_regs
= devm_kcalloc(&pdev
->dev
,
156 sizeof(hdmi
->hpd_regs
[0]),
158 if (!hdmi
->hpd_regs
) {
162 for (i
= 0; i
< config
->hpd_reg_cnt
; i
++) {
163 struct regulator
*reg
;
165 reg
= devm_regulator_get(&pdev
->dev
,
166 config
->hpd_reg_names
[i
]);
169 DRM_DEV_ERROR(&pdev
->dev
, "failed to get hpd regulator: %s (%d)\n",
170 config
->hpd_reg_names
[i
], ret
);
174 hdmi
->hpd_regs
[i
] = reg
;
177 hdmi
->pwr_regs
= devm_kcalloc(&pdev
->dev
,
179 sizeof(hdmi
->pwr_regs
[0]),
181 if (!hdmi
->pwr_regs
) {
185 for (i
= 0; i
< config
->pwr_reg_cnt
; i
++) {
186 struct regulator
*reg
;
188 reg
= devm_regulator_get(&pdev
->dev
,
189 config
->pwr_reg_names
[i
]);
192 DRM_DEV_ERROR(&pdev
->dev
, "failed to get pwr regulator: %s (%d)\n",
193 config
->pwr_reg_names
[i
], ret
);
197 hdmi
->pwr_regs
[i
] = reg
;
200 hdmi
->hpd_clks
= devm_kcalloc(&pdev
->dev
,
202 sizeof(hdmi
->hpd_clks
[0]),
204 if (!hdmi
->hpd_clks
) {
208 for (i
= 0; i
< config
->hpd_clk_cnt
; i
++) {
211 clk
= msm_clk_get(pdev
, config
->hpd_clk_names
[i
]);
214 DRM_DEV_ERROR(&pdev
->dev
, "failed to get hpd clk: %s (%d)\n",
215 config
->hpd_clk_names
[i
], ret
);
219 hdmi
->hpd_clks
[i
] = clk
;
222 hdmi
->pwr_clks
= devm_kcalloc(&pdev
->dev
,
224 sizeof(hdmi
->pwr_clks
[0]),
226 if (!hdmi
->pwr_clks
) {
230 for (i
= 0; i
< config
->pwr_clk_cnt
; i
++) {
233 clk
= msm_clk_get(pdev
, config
->pwr_clk_names
[i
]);
236 DRM_DEV_ERROR(&pdev
->dev
, "failed to get pwr clk: %s (%d)\n",
237 config
->pwr_clk_names
[i
], ret
);
241 hdmi
->pwr_clks
[i
] = clk
;
244 pm_runtime_enable(&pdev
->dev
);
246 hdmi
->workq
= alloc_ordered_workqueue("msm_hdmi", 0);
248 hdmi
->i2c
= msm_hdmi_i2c_init(hdmi
);
249 if (IS_ERR(hdmi
->i2c
)) {
250 ret
= PTR_ERR(hdmi
->i2c
);
251 DRM_DEV_ERROR(&pdev
->dev
, "failed to get i2c: %d\n", ret
);
256 ret
= msm_hdmi_get_phy(hdmi
);
258 DRM_DEV_ERROR(&pdev
->dev
, "failed to get phy\n");
262 hdmi
->hdcp_ctrl
= msm_hdmi_hdcp_init(hdmi
);
263 if (IS_ERR(hdmi
->hdcp_ctrl
)) {
264 dev_warn(&pdev
->dev
, "failed to init hdcp: disabled\n");
265 hdmi
->hdcp_ctrl
= NULL
;
272 msm_hdmi_destroy(hdmi
);
277 /* Second part of initialization, the drm/kms level modeset_init,
278 * constructs/initializes mode objects, etc, is called from master
279 * driver (not hdmi sub-device's probe/bind!)
281 * Any resource (regulator/clk/etc) which could be missing at boot
282 * should be handled in msm_hdmi_init() so that failure happens from
283 * hdmi sub-device's probe.
285 int msm_hdmi_modeset_init(struct hdmi
*hdmi
,
286 struct drm_device
*dev
, struct drm_encoder
*encoder
)
288 struct msm_drm_private
*priv
= dev
->dev_private
;
289 struct platform_device
*pdev
= hdmi
->pdev
;
293 hdmi
->encoder
= encoder
;
295 hdmi_audio_infoframe_init(&hdmi
->audio
.infoframe
);
297 hdmi
->bridge
= msm_hdmi_bridge_init(hdmi
);
298 if (IS_ERR(hdmi
->bridge
)) {
299 ret
= PTR_ERR(hdmi
->bridge
);
300 DRM_DEV_ERROR(dev
->dev
, "failed to create HDMI bridge: %d\n", ret
);
305 hdmi
->connector
= msm_hdmi_connector_init(hdmi
);
306 if (IS_ERR(hdmi
->connector
)) {
307 ret
= PTR_ERR(hdmi
->connector
);
308 DRM_DEV_ERROR(dev
->dev
, "failed to create HDMI connector: %d\n", ret
);
309 hdmi
->connector
= NULL
;
313 hdmi
->irq
= irq_of_parse_and_map(pdev
->dev
.of_node
, 0);
316 DRM_DEV_ERROR(dev
->dev
, "failed to get irq: %d\n", ret
);
320 ret
= devm_request_irq(&pdev
->dev
, hdmi
->irq
,
321 msm_hdmi_irq
, IRQF_TRIGGER_HIGH
| IRQF_ONESHOT
,
324 DRM_DEV_ERROR(dev
->dev
, "failed to request IRQ%u: %d\n",
329 ret
= msm_hdmi_hpd_enable(hdmi
->connector
);
331 DRM_DEV_ERROR(&hdmi
->pdev
->dev
, "failed to enable HPD: %d\n", ret
);
335 priv
->bridges
[priv
->num_bridges
++] = hdmi
->bridge
;
336 priv
->connectors
[priv
->num_connectors
++] = hdmi
->connector
;
338 platform_set_drvdata(pdev
, hdmi
);
343 /* bridge is normally destroyed by drm: */
345 msm_hdmi_bridge_destroy(hdmi
->bridge
);
348 if (hdmi
->connector
) {
349 hdmi
->connector
->funcs
->destroy(hdmi
->connector
);
350 hdmi
->connector
= NULL
;
360 #define HDMI_CFG(item, entry) \
361 .item ## _names = item ##_names_ ## entry, \
362 .item ## _cnt = ARRAY_SIZE(item ## _names_ ## entry)
364 static const char *pwr_reg_names_none
[] = {};
365 static const char *hpd_reg_names_none
[] = {};
367 static struct hdmi_platform_config hdmi_tx_8660_config
;
369 static const char *hpd_reg_names_8960
[] = {"core-vdda", "hdmi-mux"};
370 static const char *hpd_clk_names_8960
[] = {"core", "master_iface", "slave_iface"};
372 static struct hdmi_platform_config hdmi_tx_8960_config
= {
373 HDMI_CFG(hpd_reg
, 8960),
374 HDMI_CFG(hpd_clk
, 8960),
377 static const char *pwr_reg_names_8x74
[] = {"core-vdda", "core-vcc"};
378 static const char *hpd_reg_names_8x74
[] = {"hpd-gdsc", "hpd-5v"};
379 static const char *pwr_clk_names_8x74
[] = {"extp", "alt_iface"};
380 static const char *hpd_clk_names_8x74
[] = {"iface", "core", "mdp_core"};
381 static unsigned long hpd_clk_freq_8x74
[] = {0, 19200000, 0};
383 static struct hdmi_platform_config hdmi_tx_8974_config
= {
384 HDMI_CFG(pwr_reg
, 8x74
),
385 HDMI_CFG(hpd_reg
, 8x74
),
386 HDMI_CFG(pwr_clk
, 8x74
),
387 HDMI_CFG(hpd_clk
, 8x74
),
388 .hpd_freq
= hpd_clk_freq_8x74
,
391 static const char *hpd_reg_names_8084
[] = {"hpd-gdsc", "hpd-5v", "hpd-5v-en"};
393 static struct hdmi_platform_config hdmi_tx_8084_config
= {
394 HDMI_CFG(pwr_reg
, 8x74
),
395 HDMI_CFG(hpd_reg
, 8084),
396 HDMI_CFG(pwr_clk
, 8x74
),
397 HDMI_CFG(hpd_clk
, 8x74
),
398 .hpd_freq
= hpd_clk_freq_8x74
,
401 static struct hdmi_platform_config hdmi_tx_8994_config
= {
402 HDMI_CFG(pwr_reg
, 8x74
),
403 HDMI_CFG(hpd_reg
, none
),
404 HDMI_CFG(pwr_clk
, 8x74
),
405 HDMI_CFG(hpd_clk
, 8x74
),
406 .hpd_freq
= hpd_clk_freq_8x74
,
409 static struct hdmi_platform_config hdmi_tx_8996_config
= {
410 HDMI_CFG(pwr_reg
, none
),
411 HDMI_CFG(hpd_reg
, none
),
412 HDMI_CFG(pwr_clk
, 8x74
),
413 HDMI_CFG(hpd_clk
, 8x74
),
414 .hpd_freq
= hpd_clk_freq_8x74
,
417 static const struct {
422 } msm_hdmi_gpio_pdata
[] = {
423 { "qcom,hdmi-tx-ddc-clk", true, 1, "HDMI_DDC_CLK" },
424 { "qcom,hdmi-tx-ddc-data", true, 1, "HDMI_DDC_DATA" },
425 { "qcom,hdmi-tx-hpd", false, 1, "HDMI_HPD" },
426 { "qcom,hdmi-tx-mux-en", true, 1, "HDMI_MUX_EN" },
427 { "qcom,hdmi-tx-mux-sel", true, 0, "HDMI_MUX_SEL" },
428 { "qcom,hdmi-tx-mux-lpm", true, 1, "HDMI_MUX_LPM" },
432 * HDMI audio codec callbacks
434 static int msm_hdmi_audio_hw_params(struct device
*dev
, void *data
,
435 struct hdmi_codec_daifmt
*daifmt
,
436 struct hdmi_codec_params
*params
)
438 struct hdmi
*hdmi
= dev_get_drvdata(dev
);
440 unsigned int channel_allocation
= 0;
442 unsigned int level_shift
= 0; /* 0dB */
443 bool down_mix
= false;
445 DRM_DEV_DEBUG(dev
, "%u Hz, %d bit, %d channels\n", params
->sample_rate
,
446 params
->sample_width
, params
->cea
.channels
);
448 switch (params
->cea
.channels
) {
450 /* FR and FL speakers */
451 channel_allocation
= 0;
452 chan
= MSM_HDMI_AUDIO_CHANNEL_2
;
455 /* FC, LFE, FR and FL speakers */
456 channel_allocation
= 0x3;
457 chan
= MSM_HDMI_AUDIO_CHANNEL_4
;
460 /* RR, RL, FC, LFE, FR and FL speakers */
461 channel_allocation
= 0x0B;
462 chan
= MSM_HDMI_AUDIO_CHANNEL_6
;
465 /* FRC, FLC, RR, RL, FC, LFE, FR and FL speakers */
466 channel_allocation
= 0x1F;
467 chan
= MSM_HDMI_AUDIO_CHANNEL_8
;
473 switch (params
->sample_rate
) {
475 rate
= HDMI_SAMPLE_RATE_32KHZ
;
478 rate
= HDMI_SAMPLE_RATE_44_1KHZ
;
481 rate
= HDMI_SAMPLE_RATE_48KHZ
;
484 rate
= HDMI_SAMPLE_RATE_88_2KHZ
;
487 rate
= HDMI_SAMPLE_RATE_96KHZ
;
490 rate
= HDMI_SAMPLE_RATE_176_4KHZ
;
493 rate
= HDMI_SAMPLE_RATE_192KHZ
;
496 DRM_DEV_ERROR(dev
, "rate[%d] not supported!\n",
497 params
->sample_rate
);
501 msm_hdmi_audio_set_sample_rate(hdmi
, rate
);
502 msm_hdmi_audio_info_setup(hdmi
, 1, chan
, channel_allocation
,
503 level_shift
, down_mix
);
508 static void msm_hdmi_audio_shutdown(struct device
*dev
, void *data
)
510 struct hdmi
*hdmi
= dev_get_drvdata(dev
);
512 msm_hdmi_audio_info_setup(hdmi
, 0, 0, 0, 0, 0);
515 static const struct hdmi_codec_ops msm_hdmi_audio_codec_ops
= {
516 .hw_params
= msm_hdmi_audio_hw_params
,
517 .audio_shutdown
= msm_hdmi_audio_shutdown
,
520 static struct hdmi_codec_pdata codec_data
= {
521 .ops
= &msm_hdmi_audio_codec_ops
,
522 .max_i2s_channels
= 8,
526 static int msm_hdmi_register_audio_driver(struct hdmi
*hdmi
, struct device
*dev
)
528 hdmi
->audio_pdev
= platform_device_register_data(dev
,
533 return PTR_ERR_OR_ZERO(hdmi
->audio_pdev
);
536 static int msm_hdmi_bind(struct device
*dev
, struct device
*master
, void *data
)
538 struct drm_device
*drm
= dev_get_drvdata(master
);
539 struct msm_drm_private
*priv
= drm
->dev_private
;
540 struct hdmi_platform_config
*hdmi_cfg
;
542 struct device_node
*of_node
= dev
->of_node
;
545 hdmi_cfg
= (struct hdmi_platform_config
*)
546 of_device_get_match_data(dev
);
548 DRM_DEV_ERROR(dev
, "unknown hdmi_cfg: %pOFn\n", of_node
);
552 hdmi_cfg
->mmio_name
= "core_physical";
553 hdmi_cfg
->qfprom_mmio_name
= "qfprom_physical";
555 for (i
= 0; i
< HDMI_MAX_NUM_GPIO
; i
++) {
556 const char *name
= msm_hdmi_gpio_pdata
[i
].name
;
557 struct gpio_desc
*gpiod
;
560 * We are fetching the GPIO lines "as is" since the connector
561 * code is enabling and disabling the lines. Until that point
562 * the power-on default value will be kept.
564 gpiod
= devm_gpiod_get_optional(dev
, name
, GPIOD_ASIS
);
565 /* This will catch e.g. -PROBE_DEFER */
567 return PTR_ERR(gpiod
);
569 /* Try a second time, stripping down the name */
573 * Try again after stripping out the "qcom,hdmi-tx"
574 * prefix. This is mainly to match "hpd-gpios" used
575 * in the upstream bindings.
577 if (sscanf(name
, "qcom,hdmi-tx-%s", name3
))
578 gpiod
= devm_gpiod_get_optional(dev
, name3
, GPIOD_ASIS
);
580 return PTR_ERR(gpiod
);
582 DBG("failed to get gpio: %s", name
);
584 hdmi_cfg
->gpios
[i
].gpiod
= gpiod
;
586 gpiod_set_consumer_name(gpiod
, msm_hdmi_gpio_pdata
[i
].label
);
587 hdmi_cfg
->gpios
[i
].output
= msm_hdmi_gpio_pdata
[i
].output
;
588 hdmi_cfg
->gpios
[i
].value
= msm_hdmi_gpio_pdata
[i
].value
;
591 dev
->platform_data
= hdmi_cfg
;
593 hdmi
= msm_hdmi_init(to_platform_device(dev
));
595 return PTR_ERR(hdmi
);
598 err
= msm_hdmi_register_audio_driver(hdmi
, dev
);
600 DRM_ERROR("Failed to attach an audio codec %d\n", err
);
601 hdmi
->audio_pdev
= NULL
;
607 static void msm_hdmi_unbind(struct device
*dev
, struct device
*master
,
610 struct drm_device
*drm
= dev_get_drvdata(master
);
611 struct msm_drm_private
*priv
= drm
->dev_private
;
613 if (priv
->hdmi
->audio_pdev
)
614 platform_device_unregister(priv
->hdmi
->audio_pdev
);
616 msm_hdmi_destroy(priv
->hdmi
);
621 static const struct component_ops msm_hdmi_ops
= {
622 .bind
= msm_hdmi_bind
,
623 .unbind
= msm_hdmi_unbind
,
626 static int msm_hdmi_dev_probe(struct platform_device
*pdev
)
628 return component_add(&pdev
->dev
, &msm_hdmi_ops
);
631 static int msm_hdmi_dev_remove(struct platform_device
*pdev
)
633 component_del(&pdev
->dev
, &msm_hdmi_ops
);
637 static const struct of_device_id msm_hdmi_dt_match
[] = {
638 { .compatible
= "qcom,hdmi-tx-8996", .data
= &hdmi_tx_8996_config
},
639 { .compatible
= "qcom,hdmi-tx-8994", .data
= &hdmi_tx_8994_config
},
640 { .compatible
= "qcom,hdmi-tx-8084", .data
= &hdmi_tx_8084_config
},
641 { .compatible
= "qcom,hdmi-tx-8974", .data
= &hdmi_tx_8974_config
},
642 { .compatible
= "qcom,hdmi-tx-8960", .data
= &hdmi_tx_8960_config
},
643 { .compatible
= "qcom,hdmi-tx-8660", .data
= &hdmi_tx_8660_config
},
647 static struct platform_driver msm_hdmi_driver
= {
648 .probe
= msm_hdmi_dev_probe
,
649 .remove
= msm_hdmi_dev_remove
,
652 .of_match_table
= msm_hdmi_dt_match
,
656 void __init
msm_hdmi_register(void)
658 msm_hdmi_phy_driver_register();
659 platform_driver_register(&msm_hdmi_driver
);
662 void __exit
msm_hdmi_unregister(void)
664 platform_driver_unregister(&msm_hdmi_driver
);
665 msm_hdmi_phy_driver_unregister();