1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2015 Broadcom
4 * Copyright (c) 2014 The Linux Foundation. All rights reserved.
5 * Copyright (C) 2013 Red Hat
6 * Author: Rob Clark <robdclark@gmail.com>
10 * DOC: VC4 Falcon HDMI module
12 * The HDMI core has a state machine and a PHY. On BCM2835, most of
13 * the unit operates off of the HSM clock from CPRMAN. It also
14 * internally uses the PLLH_PIX clock for the PHY.
16 * HDMI infoframes are kept within a small packet ram, where each
17 * packet can be individually enabled for including in a frame.
19 * HDMI audio is implemented entirely within the HDMI IP block. A
20 * register in the HDMI encoder takes SPDIF frames from the DMA engine
21 * and transfers them over an internal MAI (multi-channel audio
22 * interconnect) bus to the encoder side for insertion into the video
25 * The driver's HDMI encoder does not yet support power management.
26 * The HDMI encoder's power domain and the HSM/pixel clocks are kept
27 * continuously running, and only the HDMI logic and packet ram are
28 * powered off/on at disable/enable time.
30 * The driver does not yet support CEC control, though the HDMI
31 * encoder block has CEC support.
34 #include <drm/drm_atomic_helper.h>
35 #include <drm/drm_edid.h>
36 #include <drm/drm_probe_helper.h>
37 #include <drm/drm_simple_kms_helper.h>
38 #include <drm/drm_scdc_helper.h>
39 #include <linux/clk.h>
40 #include <linux/component.h>
41 #include <linux/i2c.h>
42 #include <linux/of_address.h>
43 #include <linux/of_gpio.h>
44 #include <linux/of_platform.h>
45 #include <linux/pm_runtime.h>
46 #include <linux/rational.h>
47 #include <linux/reset.h>
48 #include <sound/dmaengine_pcm.h>
49 #include <sound/hdmi-codec.h>
50 #include <sound/pcm_drm_eld.h>
51 #include <sound/pcm_params.h>
52 #include <sound/soc.h>
53 #include "media/cec.h"
56 #include "vc4_hdmi_regs.h"
59 #define VC5_HDMI_HORZA_HFP_SHIFT 16
60 #define VC5_HDMI_HORZA_HFP_MASK VC4_MASK(28, 16)
61 #define VC5_HDMI_HORZA_VPOS BIT(15)
62 #define VC5_HDMI_HORZA_HPOS BIT(14)
63 #define VC5_HDMI_HORZA_HAP_SHIFT 0
64 #define VC5_HDMI_HORZA_HAP_MASK VC4_MASK(13, 0)
66 #define VC5_HDMI_HORZB_HBP_SHIFT 16
67 #define VC5_HDMI_HORZB_HBP_MASK VC4_MASK(26, 16)
68 #define VC5_HDMI_HORZB_HSP_SHIFT 0
69 #define VC5_HDMI_HORZB_HSP_MASK VC4_MASK(10, 0)
71 #define VC5_HDMI_VERTA_VSP_SHIFT 24
72 #define VC5_HDMI_VERTA_VSP_MASK VC4_MASK(28, 24)
73 #define VC5_HDMI_VERTA_VFP_SHIFT 16
74 #define VC5_HDMI_VERTA_VFP_MASK VC4_MASK(22, 16)
75 #define VC5_HDMI_VERTA_VAL_SHIFT 0
76 #define VC5_HDMI_VERTA_VAL_MASK VC4_MASK(12, 0)
78 #define VC5_HDMI_VERTB_VSPO_SHIFT 16
79 #define VC5_HDMI_VERTB_VSPO_MASK VC4_MASK(29, 16)
81 #define VC5_HDMI_SCRAMBLER_CTL_ENABLE BIT(0)
83 #define VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE_SHIFT 8
84 #define VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE_MASK VC4_MASK(10, 8)
86 #define VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH_SHIFT 0
87 #define VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH_MASK VC4_MASK(3, 0)
89 #define VC5_HDMI_GCP_CONFIG_GCP_ENABLE BIT(31)
91 #define VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1_SHIFT 8
92 #define VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1_MASK VC4_MASK(15, 8)
94 # define VC4_HD_M_SW_RST BIT(2)
95 # define VC4_HD_M_ENABLE BIT(0)
97 #define HSM_MIN_CLOCK_FREQ 120000000
98 #define CEC_CLOCK_FREQ 40000
100 #define HDMI_14_MAX_TMDS_CLK (340 * 1000 * 1000)
102 static bool vc4_hdmi_mode_needs_scrambling(const struct drm_display_mode *mode)
104 return (mode->clock * 1000) > HDMI_14_MAX_TMDS_CLK;
107 static bool vc4_hdmi_is_full_range_rgb(struct vc4_hdmi *vc4_hdmi,
108 const struct drm_display_mode *mode)
110 struct vc4_hdmi_encoder *vc4_encoder = &vc4_hdmi->encoder;
112 return !vc4_encoder->hdmi_monitor ||
113 drm_default_rgb_quant_range(mode) == HDMI_QUANTIZATION_RANGE_FULL;
116 static int vc4_hdmi_debugfs_regs(struct seq_file *m, void *unused)
118 struct drm_info_node *node = (struct drm_info_node *)m->private;
119 struct vc4_hdmi *vc4_hdmi = node->info_ent->data;
120 struct drm_printer p = drm_seq_file_printer(m);
122 drm_print_regset32(&p, &vc4_hdmi->hdmi_regset);
123 drm_print_regset32(&p, &vc4_hdmi->hd_regset);
128 static void vc4_hdmi_reset(struct vc4_hdmi *vc4_hdmi)
132 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
134 HDMI_WRITE(HDMI_M_CTL, VC4_HD_M_SW_RST);
136 HDMI_WRITE(HDMI_M_CTL, 0);
138 HDMI_WRITE(HDMI_M_CTL, VC4_HD_M_ENABLE);
140 HDMI_WRITE(HDMI_SW_RESET_CONTROL,
141 VC4_HDMI_SW_RESET_HDMI |
142 VC4_HDMI_SW_RESET_FORMAT_DETECT);
144 HDMI_WRITE(HDMI_SW_RESET_CONTROL, 0);
146 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
149 static void vc5_hdmi_reset(struct vc4_hdmi *vc4_hdmi)
153 reset_control_reset(vc4_hdmi->reset);
155 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
157 HDMI_WRITE(HDMI_DVP_CTL, 0);
159 HDMI_WRITE(HDMI_CLOCK_STOP,
160 HDMI_READ(HDMI_CLOCK_STOP) | VC4_DVP_HT_CLOCK_STOP_PIXEL);
162 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
165 #ifdef CONFIG_DRM_VC4_HDMI_CEC
166 static void vc4_hdmi_cec_update_clk_div(struct vc4_hdmi *vc4_hdmi)
168 unsigned long cec_rate = clk_get_rate(vc4_hdmi->cec_clock);
173 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
175 value = HDMI_READ(HDMI_CEC_CNTRL_1);
176 value &= ~VC4_HDMI_CEC_DIV_CLK_CNT_MASK;
179 * Set the clock divider: the hsm_clock rate and this divider
180 * setting will give a 40 kHz CEC clock.
182 clk_cnt = cec_rate / CEC_CLOCK_FREQ;
183 value |= clk_cnt << VC4_HDMI_CEC_DIV_CLK_CNT_SHIFT;
184 HDMI_WRITE(HDMI_CEC_CNTRL_1, value);
186 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
189 static void vc4_hdmi_cec_update_clk_div(struct vc4_hdmi *vc4_hdmi) {}
192 static void vc4_hdmi_enable_scrambling(struct drm_encoder *encoder);
194 static enum drm_connector_status
195 vc4_hdmi_connector_detect(struct drm_connector *connector, bool force)
197 struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector);
198 bool connected = false;
200 mutex_lock(&vc4_hdmi->mutex);
202 WARN_ON(pm_runtime_resume_and_get(&vc4_hdmi->pdev->dev));
204 if (vc4_hdmi->hpd_gpio) {
205 if (gpiod_get_value_cansleep(vc4_hdmi->hpd_gpio))
208 if (vc4_hdmi->variant->hp_detect &&
209 vc4_hdmi->variant->hp_detect(vc4_hdmi))
214 if (connector->status != connector_status_connected) {
215 struct edid *edid = drm_get_edid(connector, vc4_hdmi->ddc);
218 cec_s_phys_addr_from_edid(vc4_hdmi->cec_adap, edid);
219 vc4_hdmi->encoder.hdmi_monitor = drm_detect_hdmi_monitor(edid);
224 vc4_hdmi_enable_scrambling(&vc4_hdmi->encoder.base.base);
225 pm_runtime_put(&vc4_hdmi->pdev->dev);
226 mutex_unlock(&vc4_hdmi->mutex);
227 return connector_status_connected;
230 cec_phys_addr_invalidate(vc4_hdmi->cec_adap);
231 pm_runtime_put(&vc4_hdmi->pdev->dev);
232 mutex_unlock(&vc4_hdmi->mutex);
233 return connector_status_disconnected;
236 static void vc4_hdmi_connector_destroy(struct drm_connector *connector)
238 drm_connector_unregister(connector);
239 drm_connector_cleanup(connector);
242 static int vc4_hdmi_connector_get_modes(struct drm_connector *connector)
244 struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector);
245 struct vc4_hdmi_encoder *vc4_encoder = &vc4_hdmi->encoder;
249 mutex_lock(&vc4_hdmi->mutex);
251 edid = drm_get_edid(connector, vc4_hdmi->ddc);
252 cec_s_phys_addr_from_edid(vc4_hdmi->cec_adap, edid);
258 vc4_encoder->hdmi_monitor = drm_detect_hdmi_monitor(edid);
260 drm_connector_update_edid_property(connector, edid);
261 ret = drm_add_edid_modes(connector, edid);
264 if (vc4_hdmi->disable_4kp60) {
265 struct drm_device *drm = connector->dev;
266 struct drm_display_mode *mode;
268 list_for_each_entry(mode, &connector->probed_modes, head) {
269 if (vc4_hdmi_mode_needs_scrambling(mode)) {
270 drm_warn_once(drm, "The core clock cannot reach frequencies high enough to support 4k @ 60Hz.");
271 drm_warn_once(drm, "Please change your config.txt file to add hdmi_enable_4kp60.");
277 mutex_unlock(&vc4_hdmi->mutex);
282 static int vc4_hdmi_connector_atomic_check(struct drm_connector *connector,
283 struct drm_atomic_state *state)
285 struct drm_connector_state *old_state =
286 drm_atomic_get_old_connector_state(state, connector);
287 struct drm_connector_state *new_state =
288 drm_atomic_get_new_connector_state(state, connector);
289 struct drm_crtc *crtc = new_state->crtc;
294 if (old_state->colorspace != new_state->colorspace ||
295 !drm_connector_atomic_hdr_metadata_equal(old_state, new_state)) {
296 struct drm_crtc_state *crtc_state;
298 crtc_state = drm_atomic_get_crtc_state(state, crtc);
299 if (IS_ERR(crtc_state))
300 return PTR_ERR(crtc_state);
302 crtc_state->mode_changed = true;
308 static void vc4_hdmi_connector_reset(struct drm_connector *connector)
310 struct vc4_hdmi_connector_state *old_state =
311 conn_state_to_vc4_hdmi_conn_state(connector->state);
312 struct vc4_hdmi_connector_state *new_state =
313 kzalloc(sizeof(*new_state), GFP_KERNEL);
315 if (connector->state)
316 __drm_atomic_helper_connector_destroy_state(connector->state);
319 __drm_atomic_helper_connector_reset(connector, &new_state->base);
324 new_state->base.max_bpc = 8;
325 new_state->base.max_requested_bpc = 8;
326 drm_atomic_helper_connector_tv_reset(connector);
329 static struct drm_connector_state *
330 vc4_hdmi_connector_duplicate_state(struct drm_connector *connector)
332 struct drm_connector_state *conn_state = connector->state;
333 struct vc4_hdmi_connector_state *vc4_state = conn_state_to_vc4_hdmi_conn_state(conn_state);
334 struct vc4_hdmi_connector_state *new_state;
336 new_state = kzalloc(sizeof(*new_state), GFP_KERNEL);
340 new_state->pixel_rate = vc4_state->pixel_rate;
341 __drm_atomic_helper_connector_duplicate_state(connector, &new_state->base);
343 return &new_state->base;
346 static const struct drm_connector_funcs vc4_hdmi_connector_funcs = {
347 .detect = vc4_hdmi_connector_detect,
348 .fill_modes = drm_helper_probe_single_connector_modes,
349 .destroy = vc4_hdmi_connector_destroy,
350 .reset = vc4_hdmi_connector_reset,
351 .atomic_duplicate_state = vc4_hdmi_connector_duplicate_state,
352 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
355 static const struct drm_connector_helper_funcs vc4_hdmi_connector_helper_funcs = {
356 .get_modes = vc4_hdmi_connector_get_modes,
357 .atomic_check = vc4_hdmi_connector_atomic_check,
360 static int vc4_hdmi_connector_init(struct drm_device *dev,
361 struct vc4_hdmi *vc4_hdmi)
363 struct drm_connector *connector = &vc4_hdmi->connector;
364 struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
367 drm_connector_init_with_ddc(dev, connector,
368 &vc4_hdmi_connector_funcs,
369 DRM_MODE_CONNECTOR_HDMIA,
371 drm_connector_helper_add(connector, &vc4_hdmi_connector_helper_funcs);
374 * Some of the properties below require access to state, like bpc.
375 * Allocate some default initial connector state with our reset helper.
377 if (connector->funcs->reset)
378 connector->funcs->reset(connector);
380 /* Create and attach TV margin props to this connector. */
381 ret = drm_mode_create_tv_margin_properties(dev);
385 ret = drm_mode_create_hdmi_colorspace_property(connector);
389 drm_connector_attach_colorspace_property(connector);
390 drm_connector_attach_tv_margin_properties(connector);
391 drm_connector_attach_max_bpc_property(connector, 8, 12);
393 connector->polled = (DRM_CONNECTOR_POLL_CONNECT |
394 DRM_CONNECTOR_POLL_DISCONNECT);
396 connector->interlace_allowed = 1;
397 connector->doublescan_allowed = 0;
399 if (vc4_hdmi->variant->supports_hdr)
400 drm_connector_attach_hdr_output_metadata_property(connector);
402 drm_connector_attach_encoder(connector, encoder);
407 static int vc4_hdmi_stop_packet(struct drm_encoder *encoder,
408 enum hdmi_infoframe_type type,
411 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
412 u32 packet_id = type - 0x80;
415 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
416 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
417 HDMI_READ(HDMI_RAM_PACKET_CONFIG) & ~BIT(packet_id));
418 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
423 return wait_for(!(HDMI_READ(HDMI_RAM_PACKET_STATUS) &
424 BIT(packet_id)), 100);
427 static void vc4_hdmi_write_infoframe(struct drm_encoder *encoder,
428 union hdmi_infoframe *frame)
430 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
431 u32 packet_id = frame->any.type - 0x80;
432 const struct vc4_hdmi_register *ram_packet_start =
433 &vc4_hdmi->variant->registers[HDMI_RAM_PACKET_START];
434 u32 packet_reg = ram_packet_start->offset + VC4_HDMI_PACKET_STRIDE * packet_id;
435 void __iomem *base = __vc4_hdmi_get_field_base(vc4_hdmi,
436 ram_packet_start->reg);
437 uint8_t buffer[VC4_HDMI_PACKET_STRIDE];
442 WARN_ONCE(!(HDMI_READ(HDMI_RAM_PACKET_CONFIG) &
443 VC4_HDMI_RAM_PACKET_ENABLE),
444 "Packet RAM has to be on to store the packet.");
446 len = hdmi_infoframe_pack(frame, buffer, sizeof(buffer));
450 ret = vc4_hdmi_stop_packet(encoder, frame->any.type, true);
452 DRM_ERROR("Failed to wait for infoframe to go idle: %d\n", ret);
456 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
458 for (i = 0; i < len; i += 7) {
459 writel(buffer[i + 0] << 0 |
465 writel(buffer[i + 3] << 0 |
467 buffer[i + 5] << 16 |
473 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
474 HDMI_READ(HDMI_RAM_PACKET_CONFIG) | BIT(packet_id));
476 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
478 ret = wait_for((HDMI_READ(HDMI_RAM_PACKET_STATUS) &
479 BIT(packet_id)), 100);
481 DRM_ERROR("Failed to wait for infoframe to start: %d\n", ret);
484 static void vc4_hdmi_set_avi_infoframe(struct drm_encoder *encoder)
486 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
487 struct drm_connector *connector = &vc4_hdmi->connector;
488 struct drm_connector_state *cstate = connector->state;
489 const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
490 union hdmi_infoframe frame;
493 lockdep_assert_held(&vc4_hdmi->mutex);
495 ret = drm_hdmi_avi_infoframe_from_display_mode(&frame.avi,
498 DRM_ERROR("couldn't fill AVI infoframe\n");
502 drm_hdmi_avi_infoframe_quant_range(&frame.avi,
504 vc4_hdmi_is_full_range_rgb(vc4_hdmi, mode) ?
505 HDMI_QUANTIZATION_RANGE_FULL :
506 HDMI_QUANTIZATION_RANGE_LIMITED);
507 drm_hdmi_avi_infoframe_colorimetry(&frame.avi, cstate);
508 drm_hdmi_avi_infoframe_bars(&frame.avi, cstate);
510 vc4_hdmi_write_infoframe(encoder, &frame);
513 static void vc4_hdmi_set_spd_infoframe(struct drm_encoder *encoder)
515 union hdmi_infoframe frame;
518 ret = hdmi_spd_infoframe_init(&frame.spd, "Broadcom", "Videocore");
520 DRM_ERROR("couldn't fill SPD infoframe\n");
524 frame.spd.sdi = HDMI_SPD_SDI_PC;
526 vc4_hdmi_write_infoframe(encoder, &frame);
529 static void vc4_hdmi_set_audio_infoframe(struct drm_encoder *encoder)
531 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
532 struct hdmi_audio_infoframe *audio = &vc4_hdmi->audio.infoframe;
533 union hdmi_infoframe frame;
535 memcpy(&frame.audio, audio, sizeof(*audio));
536 vc4_hdmi_write_infoframe(encoder, &frame);
539 static void vc4_hdmi_set_hdr_infoframe(struct drm_encoder *encoder)
541 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
542 struct drm_connector *connector = &vc4_hdmi->connector;
543 struct drm_connector_state *conn_state = connector->state;
544 union hdmi_infoframe frame;
546 lockdep_assert_held(&vc4_hdmi->mutex);
548 if (!vc4_hdmi->variant->supports_hdr)
551 if (!conn_state->hdr_output_metadata)
554 if (drm_hdmi_infoframe_set_hdr_metadata(&frame.drm, conn_state))
557 vc4_hdmi_write_infoframe(encoder, &frame);
560 static void vc4_hdmi_set_infoframes(struct drm_encoder *encoder)
562 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
564 lockdep_assert_held(&vc4_hdmi->mutex);
566 vc4_hdmi_set_avi_infoframe(encoder);
567 vc4_hdmi_set_spd_infoframe(encoder);
569 * If audio was streaming, then we need to reenabled the audio
570 * infoframe here during encoder_enable.
572 if (vc4_hdmi->audio.streaming)
573 vc4_hdmi_set_audio_infoframe(encoder);
575 vc4_hdmi_set_hdr_infoframe(encoder);
578 static bool vc4_hdmi_supports_scrambling(struct drm_encoder *encoder,
579 struct drm_display_mode *mode)
581 struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
582 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
583 struct drm_display_info *display = &vc4_hdmi->connector.display_info;
585 lockdep_assert_held(&vc4_hdmi->mutex);
587 if (!vc4_encoder->hdmi_monitor)
590 if (!display->hdmi.scdc.supported ||
591 !display->hdmi.scdc.scrambling.supported)
597 #define SCRAMBLING_POLLING_DELAY_MS 1000
599 static void vc4_hdmi_enable_scrambling(struct drm_encoder *encoder)
601 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
602 struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
605 lockdep_assert_held(&vc4_hdmi->mutex);
607 if (!vc4_hdmi_supports_scrambling(encoder, mode))
610 if (!vc4_hdmi_mode_needs_scrambling(mode))
613 drm_scdc_set_high_tmds_clock_ratio(vc4_hdmi->ddc, true);
614 drm_scdc_set_scrambling(vc4_hdmi->ddc, true);
616 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
617 HDMI_WRITE(HDMI_SCRAMBLER_CTL, HDMI_READ(HDMI_SCRAMBLER_CTL) |
618 VC5_HDMI_SCRAMBLER_CTL_ENABLE);
619 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
621 vc4_hdmi->scdc_enabled = true;
623 queue_delayed_work(system_wq, &vc4_hdmi->scrambling_work,
624 msecs_to_jiffies(SCRAMBLING_POLLING_DELAY_MS));
627 static void vc4_hdmi_disable_scrambling(struct drm_encoder *encoder)
629 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
632 lockdep_assert_held(&vc4_hdmi->mutex);
634 if (!vc4_hdmi->scdc_enabled)
637 vc4_hdmi->scdc_enabled = false;
639 if (delayed_work_pending(&vc4_hdmi->scrambling_work))
640 cancel_delayed_work_sync(&vc4_hdmi->scrambling_work);
642 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
643 HDMI_WRITE(HDMI_SCRAMBLER_CTL, HDMI_READ(HDMI_SCRAMBLER_CTL) &
644 ~VC5_HDMI_SCRAMBLER_CTL_ENABLE);
645 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
647 drm_scdc_set_scrambling(vc4_hdmi->ddc, false);
648 drm_scdc_set_high_tmds_clock_ratio(vc4_hdmi->ddc, false);
651 static void vc4_hdmi_scrambling_wq(struct work_struct *work)
653 struct vc4_hdmi *vc4_hdmi = container_of(to_delayed_work(work),
657 if (drm_scdc_get_scrambling_status(vc4_hdmi->ddc))
660 drm_scdc_set_high_tmds_clock_ratio(vc4_hdmi->ddc, true);
661 drm_scdc_set_scrambling(vc4_hdmi->ddc, true);
663 queue_delayed_work(system_wq, &vc4_hdmi->scrambling_work,
664 msecs_to_jiffies(SCRAMBLING_POLLING_DELAY_MS));
667 static void vc4_hdmi_encoder_post_crtc_disable(struct drm_encoder *encoder,
668 struct drm_atomic_state *state)
670 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
673 mutex_lock(&vc4_hdmi->mutex);
675 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
677 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG, 0);
679 HDMI_WRITE(HDMI_VID_CTL, HDMI_READ(HDMI_VID_CTL) | VC4_HD_VID_CTL_CLRRGB);
681 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
685 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
686 HDMI_WRITE(HDMI_VID_CTL,
687 HDMI_READ(HDMI_VID_CTL) & ~VC4_HD_VID_CTL_ENABLE);
688 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
690 vc4_hdmi_disable_scrambling(encoder);
692 mutex_unlock(&vc4_hdmi->mutex);
695 static void vc4_hdmi_encoder_post_crtc_powerdown(struct drm_encoder *encoder,
696 struct drm_atomic_state *state)
698 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
702 mutex_lock(&vc4_hdmi->mutex);
704 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
705 HDMI_WRITE(HDMI_VID_CTL,
706 HDMI_READ(HDMI_VID_CTL) | VC4_HD_VID_CTL_BLANKPIX);
707 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
709 if (vc4_hdmi->variant->phy_disable)
710 vc4_hdmi->variant->phy_disable(vc4_hdmi);
712 clk_disable_unprepare(vc4_hdmi->pixel_bvb_clock);
713 clk_disable_unprepare(vc4_hdmi->pixel_clock);
715 ret = pm_runtime_put(&vc4_hdmi->pdev->dev);
717 DRM_ERROR("Failed to release power domain: %d\n", ret);
719 mutex_unlock(&vc4_hdmi->mutex);
722 static void vc4_hdmi_encoder_disable(struct drm_encoder *encoder)
724 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
726 mutex_lock(&vc4_hdmi->mutex);
727 vc4_hdmi->output_enabled = false;
728 mutex_unlock(&vc4_hdmi->mutex);
731 static void vc4_hdmi_csc_setup(struct vc4_hdmi *vc4_hdmi,
732 struct drm_connector_state *state,
733 const struct drm_display_mode *mode)
738 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
740 csc_ctl = VC4_SET_FIELD(VC4_HD_CSC_CTL_ORDER_BGR,
741 VC4_HD_CSC_CTL_ORDER);
743 if (!vc4_hdmi_is_full_range_rgb(vc4_hdmi, mode)) {
744 /* CEA VICs other than #1 requre limited range RGB
745 * output unless overridden by an AVI infoframe.
746 * Apply a colorspace conversion to squash 0-255 down
747 * to 16-235. The matrix here is:
754 csc_ctl |= VC4_HD_CSC_CTL_ENABLE;
755 csc_ctl |= VC4_HD_CSC_CTL_RGB2YCC;
756 csc_ctl |= VC4_SET_FIELD(VC4_HD_CSC_CTL_MODE_CUSTOM,
757 VC4_HD_CSC_CTL_MODE);
759 HDMI_WRITE(HDMI_CSC_12_11, (0x000 << 16) | 0x000);
760 HDMI_WRITE(HDMI_CSC_14_13, (0x100 << 16) | 0x6e0);
761 HDMI_WRITE(HDMI_CSC_22_21, (0x6e0 << 16) | 0x000);
762 HDMI_WRITE(HDMI_CSC_24_23, (0x100 << 16) | 0x000);
763 HDMI_WRITE(HDMI_CSC_32_31, (0x000 << 16) | 0x6e0);
764 HDMI_WRITE(HDMI_CSC_34_33, (0x100 << 16) | 0x000);
767 /* The RGB order applies even when CSC is disabled. */
768 HDMI_WRITE(HDMI_CSC_CTL, csc_ctl);
770 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
774 * If we need to output Full Range RGB, then use the unity matrix
780 * Matrix is signed 2p13 fixed point, with signed 9p6 offsets
782 static const u16 vc5_hdmi_csc_full_rgb_unity[3][4] = {
783 { 0x2000, 0x0000, 0x0000, 0x0000 },
784 { 0x0000, 0x2000, 0x0000, 0x0000 },
785 { 0x0000, 0x0000, 0x2000, 0x0000 },
789 * CEA VICs other than #1 require limited range RGB output unless
790 * overridden by an AVI infoframe. Apply a colorspace conversion to
791 * squash 0-255 down to 16-235. The matrix here is:
797 * Matrix is signed 2p13 fixed point, with signed 9p6 offsets
799 static const u16 vc5_hdmi_csc_full_rgb_to_limited_rgb[3][4] = {
800 { 0x1b80, 0x0000, 0x0000, 0x0400 },
801 { 0x0000, 0x1b80, 0x0000, 0x0400 },
802 { 0x0000, 0x0000, 0x1b80, 0x0400 },
805 static void vc5_hdmi_set_csc_coeffs(struct vc4_hdmi *vc4_hdmi,
806 const u16 coeffs[3][4])
808 lockdep_assert_held(&vc4_hdmi->hw_lock);
810 HDMI_WRITE(HDMI_CSC_12_11, (coeffs[0][1] << 16) | coeffs[0][0]);
811 HDMI_WRITE(HDMI_CSC_14_13, (coeffs[0][3] << 16) | coeffs[0][2]);
812 HDMI_WRITE(HDMI_CSC_22_21, (coeffs[1][1] << 16) | coeffs[1][0]);
813 HDMI_WRITE(HDMI_CSC_24_23, (coeffs[1][3] << 16) | coeffs[1][2]);
814 HDMI_WRITE(HDMI_CSC_32_31, (coeffs[2][1] << 16) | coeffs[2][0]);
815 HDMI_WRITE(HDMI_CSC_34_33, (coeffs[2][3] << 16) | coeffs[2][2]);
818 static void vc5_hdmi_csc_setup(struct vc4_hdmi *vc4_hdmi,
819 struct drm_connector_state *state,
820 const struct drm_display_mode *mode)
823 u32 csc_ctl = VC5_MT_CP_CSC_CTL_ENABLE | VC4_SET_FIELD(VC4_HD_CSC_CTL_MODE_CUSTOM,
824 VC5_MT_CP_CSC_CTL_MODE);
826 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
828 HDMI_WRITE(HDMI_VEC_INTERFACE_XBAR, 0x354021);
830 if (!vc4_hdmi_is_full_range_rgb(vc4_hdmi, mode))
831 vc5_hdmi_set_csc_coeffs(vc4_hdmi, vc5_hdmi_csc_full_rgb_to_limited_rgb);
833 vc5_hdmi_set_csc_coeffs(vc4_hdmi, vc5_hdmi_csc_full_rgb_unity);
835 HDMI_WRITE(HDMI_CSC_CTL, csc_ctl);
837 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
840 static void vc4_hdmi_set_timings(struct vc4_hdmi *vc4_hdmi,
841 struct drm_connector_state *state,
842 struct drm_display_mode *mode)
844 bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
845 bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
846 bool interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE;
847 u32 pixel_rep = (mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1;
848 u32 verta = (VC4_SET_FIELD(mode->crtc_vsync_end - mode->crtc_vsync_start,
849 VC4_HDMI_VERTA_VSP) |
850 VC4_SET_FIELD(mode->crtc_vsync_start - mode->crtc_vdisplay,
851 VC4_HDMI_VERTA_VFP) |
852 VC4_SET_FIELD(mode->crtc_vdisplay, VC4_HDMI_VERTA_VAL));
853 u32 vertb = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) |
854 VC4_SET_FIELD(mode->crtc_vtotal - mode->crtc_vsync_end,
855 VC4_HDMI_VERTB_VBP));
856 u32 vertb_even = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) |
857 VC4_SET_FIELD(mode->crtc_vtotal -
858 mode->crtc_vsync_end -
860 VC4_HDMI_VERTB_VBP));
863 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
865 HDMI_WRITE(HDMI_HORZA,
866 (vsync_pos ? VC4_HDMI_HORZA_VPOS : 0) |
867 (hsync_pos ? VC4_HDMI_HORZA_HPOS : 0) |
868 VC4_SET_FIELD(mode->hdisplay * pixel_rep,
869 VC4_HDMI_HORZA_HAP));
871 HDMI_WRITE(HDMI_HORZB,
872 VC4_SET_FIELD((mode->htotal -
873 mode->hsync_end) * pixel_rep,
874 VC4_HDMI_HORZB_HBP) |
875 VC4_SET_FIELD((mode->hsync_end -
876 mode->hsync_start) * pixel_rep,
877 VC4_HDMI_HORZB_HSP) |
878 VC4_SET_FIELD((mode->hsync_start -
879 mode->hdisplay) * pixel_rep,
880 VC4_HDMI_HORZB_HFP));
882 HDMI_WRITE(HDMI_VERTA0, verta);
883 HDMI_WRITE(HDMI_VERTA1, verta);
885 HDMI_WRITE(HDMI_VERTB0, vertb_even);
886 HDMI_WRITE(HDMI_VERTB1, vertb);
888 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
891 static void vc5_hdmi_set_timings(struct vc4_hdmi *vc4_hdmi,
892 struct drm_connector_state *state,
893 struct drm_display_mode *mode)
895 bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
896 bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
897 bool interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE;
898 u32 pixel_rep = (mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1;
899 u32 verta = (VC4_SET_FIELD(mode->crtc_vsync_end - mode->crtc_vsync_start,
900 VC5_HDMI_VERTA_VSP) |
901 VC4_SET_FIELD(mode->crtc_vsync_start - mode->crtc_vdisplay,
902 VC5_HDMI_VERTA_VFP) |
903 VC4_SET_FIELD(mode->crtc_vdisplay, VC5_HDMI_VERTA_VAL));
904 u32 vertb = (VC4_SET_FIELD(0, VC5_HDMI_VERTB_VSPO) |
905 VC4_SET_FIELD(mode->crtc_vtotal - mode->crtc_vsync_end,
906 VC4_HDMI_VERTB_VBP));
907 u32 vertb_even = (VC4_SET_FIELD(0, VC5_HDMI_VERTB_VSPO) |
908 VC4_SET_FIELD(mode->crtc_vtotal -
909 mode->crtc_vsync_end -
911 VC4_HDMI_VERTB_VBP));
917 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
919 HDMI_WRITE(HDMI_HORZA,
920 (vsync_pos ? VC5_HDMI_HORZA_VPOS : 0) |
921 (hsync_pos ? VC5_HDMI_HORZA_HPOS : 0) |
922 VC4_SET_FIELD(mode->hdisplay * pixel_rep,
923 VC5_HDMI_HORZA_HAP) |
924 VC4_SET_FIELD((mode->hsync_start -
925 mode->hdisplay) * pixel_rep,
926 VC5_HDMI_HORZA_HFP));
928 HDMI_WRITE(HDMI_HORZB,
929 VC4_SET_FIELD((mode->htotal -
930 mode->hsync_end) * pixel_rep,
931 VC5_HDMI_HORZB_HBP) |
932 VC4_SET_FIELD((mode->hsync_end -
933 mode->hsync_start) * pixel_rep,
934 VC5_HDMI_HORZB_HSP));
936 HDMI_WRITE(HDMI_VERTA0, verta);
937 HDMI_WRITE(HDMI_VERTA1, verta);
939 HDMI_WRITE(HDMI_VERTB0, vertb_even);
940 HDMI_WRITE(HDMI_VERTB1, vertb);
942 switch (state->max_bpc) {
958 reg = HDMI_READ(HDMI_DEEP_COLOR_CONFIG_1);
959 reg &= ~(VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE_MASK |
960 VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH_MASK);
961 reg |= VC4_SET_FIELD(2, VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE) |
962 VC4_SET_FIELD(gcp, VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH);
963 HDMI_WRITE(HDMI_DEEP_COLOR_CONFIG_1, reg);
965 reg = HDMI_READ(HDMI_GCP_WORD_1);
966 reg &= ~VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1_MASK;
967 reg |= VC4_SET_FIELD(gcp, VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1);
968 HDMI_WRITE(HDMI_GCP_WORD_1, reg);
970 reg = HDMI_READ(HDMI_GCP_CONFIG);
971 reg &= ~VC5_HDMI_GCP_CONFIG_GCP_ENABLE;
972 reg |= gcp_en ? VC5_HDMI_GCP_CONFIG_GCP_ENABLE : 0;
973 HDMI_WRITE(HDMI_GCP_CONFIG, reg);
975 HDMI_WRITE(HDMI_CLOCK_STOP, 0);
977 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
980 static void vc4_hdmi_recenter_fifo(struct vc4_hdmi *vc4_hdmi)
986 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
988 drift = HDMI_READ(HDMI_FIFO_CTL);
989 drift &= VC4_HDMI_FIFO_VALID_WRITE_MASK;
991 HDMI_WRITE(HDMI_FIFO_CTL,
992 drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
993 HDMI_WRITE(HDMI_FIFO_CTL,
994 drift | VC4_HDMI_FIFO_CTL_RECENTER);
996 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
998 usleep_range(1000, 1100);
1000 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1002 HDMI_WRITE(HDMI_FIFO_CTL,
1003 drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
1004 HDMI_WRITE(HDMI_FIFO_CTL,
1005 drift | VC4_HDMI_FIFO_CTL_RECENTER);
1007 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1009 ret = wait_for(HDMI_READ(HDMI_FIFO_CTL) &
1010 VC4_HDMI_FIFO_CTL_RECENTER_DONE, 1);
1011 WARN_ONCE(ret, "Timeout waiting for "
1012 "VC4_HDMI_FIFO_CTL_RECENTER_DONE");
1015 static void vc4_hdmi_encoder_pre_crtc_configure(struct drm_encoder *encoder,
1016 struct drm_atomic_state *state)
1018 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1019 struct drm_connector *connector = &vc4_hdmi->connector;
1020 struct drm_connector_state *conn_state =
1021 drm_atomic_get_new_connector_state(state, connector);
1022 struct vc4_hdmi_connector_state *vc4_conn_state =
1023 conn_state_to_vc4_hdmi_conn_state(conn_state);
1024 struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
1025 unsigned long pixel_rate = vc4_conn_state->pixel_rate;
1026 unsigned long bvb_rate, hsm_rate;
1027 unsigned long flags;
1030 mutex_lock(&vc4_hdmi->mutex);
1033 * As stated in RPi's vc4 firmware "HDMI state machine (HSM) clock must
1034 * be faster than pixel clock, infinitesimally faster, tested in
1035 * simulation. Otherwise, exact value is unimportant for HDMI
1036 * operation." This conflicts with bcm2835's vc4 documentation, which
1037 * states HSM's clock has to be at least 108% of the pixel clock.
1039 * Real life tests reveal that vc4's firmware statement holds up, and
1040 * users are able to use pixel clocks closer to HSM's, namely for
1041 * 1920x1200@60Hz. So it was decided to have leave a 1% margin between
1042 * both clocks. Which, for RPi0-3 implies a maximum pixel clock of
1045 * Additionally, the AXI clock needs to be at least 25% of
1046 * pixel clock, but HSM ends up being the limiting factor.
1048 hsm_rate = max_t(unsigned long, 120000000, (pixel_rate / 100) * 101);
1049 ret = clk_set_min_rate(vc4_hdmi->hsm_clock, hsm_rate);
1051 DRM_ERROR("Failed to set HSM clock rate: %d\n", ret);
1055 ret = pm_runtime_resume_and_get(&vc4_hdmi->pdev->dev);
1057 DRM_ERROR("Failed to retain power domain: %d\n", ret);
1061 ret = clk_set_rate(vc4_hdmi->pixel_clock, pixel_rate);
1063 DRM_ERROR("Failed to set pixel clock rate: %d\n", ret);
1064 goto err_put_runtime_pm;
1067 ret = clk_prepare_enable(vc4_hdmi->pixel_clock);
1069 DRM_ERROR("Failed to turn on pixel clock: %d\n", ret);
1070 goto err_put_runtime_pm;
1074 vc4_hdmi_cec_update_clk_div(vc4_hdmi);
1076 if (pixel_rate > 297000000)
1077 bvb_rate = 300000000;
1078 else if (pixel_rate > 148500000)
1079 bvb_rate = 150000000;
1081 bvb_rate = 75000000;
1083 ret = clk_set_min_rate(vc4_hdmi->pixel_bvb_clock, bvb_rate);
1085 DRM_ERROR("Failed to set pixel bvb clock rate: %d\n", ret);
1086 goto err_disable_pixel_clock;
1089 ret = clk_prepare_enable(vc4_hdmi->pixel_bvb_clock);
1091 DRM_ERROR("Failed to turn on pixel bvb clock: %d\n", ret);
1092 goto err_disable_pixel_clock;
1095 if (vc4_hdmi->variant->phy_init)
1096 vc4_hdmi->variant->phy_init(vc4_hdmi, vc4_conn_state);
1098 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1100 HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
1101 HDMI_READ(HDMI_SCHEDULER_CONTROL) |
1102 VC4_HDMI_SCHEDULER_CONTROL_MANUAL_FORMAT |
1103 VC4_HDMI_SCHEDULER_CONTROL_IGNORE_VSYNC_PREDICTS);
1105 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1107 if (vc4_hdmi->variant->set_timings)
1108 vc4_hdmi->variant->set_timings(vc4_hdmi, conn_state, mode);
1110 mutex_unlock(&vc4_hdmi->mutex);
1114 err_disable_pixel_clock:
1115 clk_disable_unprepare(vc4_hdmi->pixel_clock);
1117 pm_runtime_put(&vc4_hdmi->pdev->dev);
1119 mutex_unlock(&vc4_hdmi->mutex);
1123 static void vc4_hdmi_encoder_pre_crtc_enable(struct drm_encoder *encoder,
1124 struct drm_atomic_state *state)
1126 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1127 struct drm_connector *connector = &vc4_hdmi->connector;
1128 struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
1129 struct drm_connector_state *conn_state =
1130 drm_atomic_get_new_connector_state(state, connector);
1131 unsigned long flags;
1133 mutex_lock(&vc4_hdmi->mutex);
1135 if (vc4_hdmi->variant->csc_setup)
1136 vc4_hdmi->variant->csc_setup(vc4_hdmi, conn_state, mode);
1138 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1139 HDMI_WRITE(HDMI_FIFO_CTL, VC4_HDMI_FIFO_CTL_MASTER_SLAVE_N);
1140 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1142 mutex_unlock(&vc4_hdmi->mutex);
1145 static void vc4_hdmi_encoder_post_crtc_enable(struct drm_encoder *encoder,
1146 struct drm_atomic_state *state)
1148 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1149 struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
1150 struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
1151 bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
1152 bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
1153 unsigned long flags;
1156 mutex_lock(&vc4_hdmi->mutex);
1158 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1160 HDMI_WRITE(HDMI_VID_CTL,
1161 VC4_HD_VID_CTL_ENABLE |
1162 VC4_HD_VID_CTL_CLRRGB |
1163 VC4_HD_VID_CTL_UNDERFLOW_ENABLE |
1164 VC4_HD_VID_CTL_FRAME_COUNTER_RESET |
1165 (vsync_pos ? 0 : VC4_HD_VID_CTL_VSYNC_LOW) |
1166 (hsync_pos ? 0 : VC4_HD_VID_CTL_HSYNC_LOW));
1168 HDMI_WRITE(HDMI_VID_CTL,
1169 HDMI_READ(HDMI_VID_CTL) & ~VC4_HD_VID_CTL_BLANKPIX);
1171 if (vc4_encoder->hdmi_monitor) {
1172 HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
1173 HDMI_READ(HDMI_SCHEDULER_CONTROL) |
1174 VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);
1176 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1178 ret = wait_for(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
1179 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE, 1000);
1180 WARN_ONCE(ret, "Timeout waiting for "
1181 "VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
1183 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
1184 HDMI_READ(HDMI_RAM_PACKET_CONFIG) &
1185 ~(VC4_HDMI_RAM_PACKET_ENABLE));
1186 HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
1187 HDMI_READ(HDMI_SCHEDULER_CONTROL) &
1188 ~VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);
1190 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1192 ret = wait_for(!(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
1193 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE), 1000);
1194 WARN_ONCE(ret, "Timeout waiting for "
1195 "!VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
1198 if (vc4_encoder->hdmi_monitor) {
1199 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1201 WARN_ON(!(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
1202 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE));
1203 HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
1204 HDMI_READ(HDMI_SCHEDULER_CONTROL) |
1205 VC4_HDMI_SCHEDULER_CONTROL_VERT_ALWAYS_KEEPOUT);
1207 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
1208 VC4_HDMI_RAM_PACKET_ENABLE);
1210 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1212 vc4_hdmi_set_infoframes(encoder);
1215 vc4_hdmi_recenter_fifo(vc4_hdmi);
1216 vc4_hdmi_enable_scrambling(encoder);
1218 mutex_unlock(&vc4_hdmi->mutex);
1221 static void vc4_hdmi_encoder_enable(struct drm_encoder *encoder)
1223 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1225 mutex_lock(&vc4_hdmi->mutex);
1226 vc4_hdmi->output_enabled = true;
1227 mutex_unlock(&vc4_hdmi->mutex);
1230 static void vc4_hdmi_encoder_atomic_mode_set(struct drm_encoder *encoder,
1231 struct drm_crtc_state *crtc_state,
1232 struct drm_connector_state *conn_state)
1234 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1236 mutex_lock(&vc4_hdmi->mutex);
1237 drm_mode_copy(&vc4_hdmi->saved_adjusted_mode,
1238 &crtc_state->adjusted_mode);
1239 mutex_unlock(&vc4_hdmi->mutex);
1242 #define WIFI_2_4GHz_CH1_MIN_FREQ 2400000000ULL
1243 #define WIFI_2_4GHz_CH1_MAX_FREQ 2422000000ULL
1245 static int vc4_hdmi_encoder_atomic_check(struct drm_encoder *encoder,
1246 struct drm_crtc_state *crtc_state,
1247 struct drm_connector_state *conn_state)
1249 struct vc4_hdmi_connector_state *vc4_state = conn_state_to_vc4_hdmi_conn_state(conn_state);
1250 struct drm_display_mode *mode = &crtc_state->adjusted_mode;
1251 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1252 unsigned long long pixel_rate = mode->clock * 1000;
1253 unsigned long long tmds_rate;
1255 if (vc4_hdmi->variant->unsupported_odd_h_timings &&
1256 !(mode->flags & DRM_MODE_FLAG_DBLCLK) &&
1257 ((mode->hdisplay % 2) || (mode->hsync_start % 2) ||
1258 (mode->hsync_end % 2) || (mode->htotal % 2)))
1262 * The 1440p@60 pixel rate is in the same range than the first
1263 * WiFi channel (between 2.4GHz and 2.422GHz with 22MHz
1264 * bandwidth). Slightly lower the frequency to bring it out of
1267 tmds_rate = pixel_rate * 10;
1268 if (vc4_hdmi->disable_wifi_frequencies &&
1269 (tmds_rate >= WIFI_2_4GHz_CH1_MIN_FREQ &&
1270 tmds_rate <= WIFI_2_4GHz_CH1_MAX_FREQ)) {
1271 mode->clock = 238560;
1272 pixel_rate = mode->clock * 1000;
1275 if (conn_state->max_bpc == 12) {
1276 pixel_rate = pixel_rate * 150;
1277 do_div(pixel_rate, 100);
1278 } else if (conn_state->max_bpc == 10) {
1279 pixel_rate = pixel_rate * 125;
1280 do_div(pixel_rate, 100);
1283 if (mode->flags & DRM_MODE_FLAG_DBLCLK)
1284 pixel_rate = pixel_rate * 2;
1286 if (pixel_rate > vc4_hdmi->variant->max_pixel_clock)
1289 if (vc4_hdmi->disable_4kp60 && (pixel_rate > HDMI_14_MAX_TMDS_CLK))
1292 vc4_state->pixel_rate = pixel_rate;
1297 static enum drm_mode_status
1298 vc4_hdmi_encoder_mode_valid(struct drm_encoder *encoder,
1299 const struct drm_display_mode *mode)
1301 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1303 if (vc4_hdmi->variant->unsupported_odd_h_timings &&
1304 !(mode->flags & DRM_MODE_FLAG_DBLCLK) &&
1305 ((mode->hdisplay % 2) || (mode->hsync_start % 2) ||
1306 (mode->hsync_end % 2) || (mode->htotal % 2)))
1307 return MODE_H_ILLEGAL;
1309 if ((mode->clock * 1000) > vc4_hdmi->variant->max_pixel_clock)
1310 return MODE_CLOCK_HIGH;
1312 if (vc4_hdmi->disable_4kp60 && vc4_hdmi_mode_needs_scrambling(mode))
1313 return MODE_CLOCK_HIGH;
1318 static const struct drm_encoder_helper_funcs vc4_hdmi_encoder_helper_funcs = {
1319 .atomic_check = vc4_hdmi_encoder_atomic_check,
1320 .atomic_mode_set = vc4_hdmi_encoder_atomic_mode_set,
1321 .mode_valid = vc4_hdmi_encoder_mode_valid,
1322 .disable = vc4_hdmi_encoder_disable,
1323 .enable = vc4_hdmi_encoder_enable,
1326 static u32 vc4_hdmi_channel_map(struct vc4_hdmi *vc4_hdmi, u32 channel_mask)
1329 u32 channel_map = 0;
1331 for (i = 0; i < 8; i++) {
1332 if (channel_mask & BIT(i))
1333 channel_map |= i << (3 * i);
1338 static u32 vc5_hdmi_channel_map(struct vc4_hdmi *vc4_hdmi, u32 channel_mask)
1341 u32 channel_map = 0;
1343 for (i = 0; i < 8; i++) {
1344 if (channel_mask & BIT(i))
1345 channel_map |= i << (4 * i);
1350 static bool vc5_hdmi_hp_detect(struct vc4_hdmi *vc4_hdmi)
1352 unsigned long flags;
1355 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1356 hotplug = HDMI_READ(HDMI_HOTPLUG);
1357 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1359 return !!(hotplug & VC4_HDMI_HOTPLUG_CONNECTED);
1362 /* HDMI audio codec callbacks */
1363 static void vc4_hdmi_audio_set_mai_clock(struct vc4_hdmi *vc4_hdmi,
1364 unsigned int samplerate)
1366 u32 hsm_clock = clk_get_rate(vc4_hdmi->audio_clock);
1367 unsigned long flags;
1370 rational_best_approximation(hsm_clock, samplerate,
1371 VC4_HD_MAI_SMP_N_MASK >>
1372 VC4_HD_MAI_SMP_N_SHIFT,
1373 (VC4_HD_MAI_SMP_M_MASK >>
1374 VC4_HD_MAI_SMP_M_SHIFT) + 1,
1377 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1378 HDMI_WRITE(HDMI_MAI_SMP,
1379 VC4_SET_FIELD(n, VC4_HD_MAI_SMP_N) |
1380 VC4_SET_FIELD(m - 1, VC4_HD_MAI_SMP_M));
1381 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1384 static void vc4_hdmi_set_n_cts(struct vc4_hdmi *vc4_hdmi, unsigned int samplerate)
1386 const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
1390 lockdep_assert_held(&vc4_hdmi->mutex);
1391 lockdep_assert_held(&vc4_hdmi->hw_lock);
1393 n = 128 * samplerate / 1000;
1394 tmp = (u64)(mode->clock * 1000) * n;
1395 do_div(tmp, 128 * samplerate);
1398 HDMI_WRITE(HDMI_CRP_CFG,
1399 VC4_HDMI_CRP_CFG_EXTERNAL_CTS_EN |
1400 VC4_SET_FIELD(n, VC4_HDMI_CRP_CFG_N));
1403 * We could get slightly more accurate clocks in some cases by
1404 * providing a CTS_1 value. The two CTS values are alternated
1405 * between based on the period fields
1407 HDMI_WRITE(HDMI_CTS_0, cts);
1408 HDMI_WRITE(HDMI_CTS_1, cts);
1411 static inline struct vc4_hdmi *dai_to_hdmi(struct snd_soc_dai *dai)
1413 struct snd_soc_card *card = snd_soc_dai_get_drvdata(dai);
1415 return snd_soc_card_get_drvdata(card);
1418 static bool vc4_hdmi_audio_can_stream(struct vc4_hdmi *vc4_hdmi)
1420 lockdep_assert_held(&vc4_hdmi->mutex);
1423 * If the controller is disabled, prevent any ALSA output.
1425 if (!vc4_hdmi->output_enabled)
1429 * If the encoder is currently in DVI mode, treat the codec DAI
1432 if (!(HDMI_READ(HDMI_RAM_PACKET_CONFIG) & VC4_HDMI_RAM_PACKET_ENABLE))
1438 static int vc4_hdmi_audio_startup(struct device *dev, void *data)
1440 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
1441 unsigned long flags;
1443 mutex_lock(&vc4_hdmi->mutex);
1445 if (!vc4_hdmi_audio_can_stream(vc4_hdmi)) {
1446 mutex_unlock(&vc4_hdmi->mutex);
1450 vc4_hdmi->audio.streaming = true;
1452 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1453 HDMI_WRITE(HDMI_MAI_CTL,
1454 VC4_HD_MAI_CTL_RESET |
1455 VC4_HD_MAI_CTL_FLUSH |
1456 VC4_HD_MAI_CTL_DLATE |
1457 VC4_HD_MAI_CTL_ERRORE |
1458 VC4_HD_MAI_CTL_ERRORF);
1459 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1461 if (vc4_hdmi->variant->phy_rng_enable)
1462 vc4_hdmi->variant->phy_rng_enable(vc4_hdmi);
1464 mutex_unlock(&vc4_hdmi->mutex);
1469 static void vc4_hdmi_audio_reset(struct vc4_hdmi *vc4_hdmi)
1471 struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
1472 struct device *dev = &vc4_hdmi->pdev->dev;
1473 unsigned long flags;
1476 lockdep_assert_held(&vc4_hdmi->mutex);
1478 vc4_hdmi->audio.streaming = false;
1479 ret = vc4_hdmi_stop_packet(encoder, HDMI_INFOFRAME_TYPE_AUDIO, false);
1481 dev_err(dev, "Failed to stop audio infoframe: %d\n", ret);
1483 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1485 HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_RESET);
1486 HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_ERRORF);
1487 HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_FLUSH);
1489 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1492 static void vc4_hdmi_audio_shutdown(struct device *dev, void *data)
1494 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
1495 unsigned long flags;
1497 mutex_lock(&vc4_hdmi->mutex);
1499 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1501 HDMI_WRITE(HDMI_MAI_CTL,
1502 VC4_HD_MAI_CTL_DLATE |
1503 VC4_HD_MAI_CTL_ERRORE |
1504 VC4_HD_MAI_CTL_ERRORF);
1506 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1508 if (vc4_hdmi->variant->phy_rng_disable)
1509 vc4_hdmi->variant->phy_rng_disable(vc4_hdmi);
1511 vc4_hdmi->audio.streaming = false;
1512 vc4_hdmi_audio_reset(vc4_hdmi);
1514 mutex_unlock(&vc4_hdmi->mutex);
1517 static int sample_rate_to_mai_fmt(int samplerate)
1519 switch (samplerate) {
1521 return VC4_HDMI_MAI_SAMPLE_RATE_8000;
1523 return VC4_HDMI_MAI_SAMPLE_RATE_11025;
1525 return VC4_HDMI_MAI_SAMPLE_RATE_12000;
1527 return VC4_HDMI_MAI_SAMPLE_RATE_16000;
1529 return VC4_HDMI_MAI_SAMPLE_RATE_22050;
1531 return VC4_HDMI_MAI_SAMPLE_RATE_24000;
1533 return VC4_HDMI_MAI_SAMPLE_RATE_32000;
1535 return VC4_HDMI_MAI_SAMPLE_RATE_44100;
1537 return VC4_HDMI_MAI_SAMPLE_RATE_48000;
1539 return VC4_HDMI_MAI_SAMPLE_RATE_64000;
1541 return VC4_HDMI_MAI_SAMPLE_RATE_88200;
1543 return VC4_HDMI_MAI_SAMPLE_RATE_96000;
1545 return VC4_HDMI_MAI_SAMPLE_RATE_128000;
1547 return VC4_HDMI_MAI_SAMPLE_RATE_176400;
1549 return VC4_HDMI_MAI_SAMPLE_RATE_192000;
1551 return VC4_HDMI_MAI_SAMPLE_RATE_NOT_INDICATED;
1555 /* HDMI audio codec callbacks */
1556 static int vc4_hdmi_audio_prepare(struct device *dev, void *data,
1557 struct hdmi_codec_daifmt *daifmt,
1558 struct hdmi_codec_params *params)
1560 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
1561 struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
1562 unsigned int sample_rate = params->sample_rate;
1563 unsigned int channels = params->channels;
1564 unsigned long flags;
1565 u32 audio_packet_config, channel_mask;
1567 u32 mai_audio_format;
1568 u32 mai_sample_rate;
1570 dev_dbg(dev, "%s: %u Hz, %d bit, %d channels\n", __func__,
1571 sample_rate, params->sample_width, channels);
1573 mutex_lock(&vc4_hdmi->mutex);
1575 if (!vc4_hdmi_audio_can_stream(vc4_hdmi)) {
1576 mutex_unlock(&vc4_hdmi->mutex);
1580 vc4_hdmi_audio_set_mai_clock(vc4_hdmi, sample_rate);
1582 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1583 HDMI_WRITE(HDMI_MAI_CTL,
1584 VC4_SET_FIELD(channels, VC4_HD_MAI_CTL_CHNUM) |
1585 VC4_HD_MAI_CTL_WHOLSMP |
1586 VC4_HD_MAI_CTL_CHALIGN |
1587 VC4_HD_MAI_CTL_ENABLE);
1589 mai_sample_rate = sample_rate_to_mai_fmt(sample_rate);
1590 if (params->iec.status[0] & IEC958_AES0_NONAUDIO &&
1591 params->channels == 8)
1592 mai_audio_format = VC4_HDMI_MAI_FORMAT_HBR;
1594 mai_audio_format = VC4_HDMI_MAI_FORMAT_PCM;
1595 HDMI_WRITE(HDMI_MAI_FMT,
1596 VC4_SET_FIELD(mai_sample_rate,
1597 VC4_HDMI_MAI_FORMAT_SAMPLE_RATE) |
1598 VC4_SET_FIELD(mai_audio_format,
1599 VC4_HDMI_MAI_FORMAT_AUDIO_FORMAT));
1601 /* The B frame identifier should match the value used by alsa-lib (8) */
1602 audio_packet_config =
1603 VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_SAMPLE_FLAT |
1604 VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_INACTIVE_CHANNELS |
1605 VC4_SET_FIELD(0x8, VC4_HDMI_AUDIO_PACKET_B_FRAME_IDENTIFIER);
1607 channel_mask = GENMASK(channels - 1, 0);
1608 audio_packet_config |= VC4_SET_FIELD(channel_mask,
1609 VC4_HDMI_AUDIO_PACKET_CEA_MASK);
1611 /* Set the MAI threshold */
1612 HDMI_WRITE(HDMI_MAI_THR,
1613 VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_PANICHIGH) |
1614 VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_PANICLOW) |
1615 VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_DREQHIGH) |
1616 VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_DREQLOW));
1618 HDMI_WRITE(HDMI_MAI_CONFIG,
1619 VC4_HDMI_MAI_CONFIG_BIT_REVERSE |
1620 VC4_HDMI_MAI_CONFIG_FORMAT_REVERSE |
1621 VC4_SET_FIELD(channel_mask, VC4_HDMI_MAI_CHANNEL_MASK));
1623 channel_map = vc4_hdmi->variant->channel_map(vc4_hdmi, channel_mask);
1624 HDMI_WRITE(HDMI_MAI_CHANNEL_MAP, channel_map);
1625 HDMI_WRITE(HDMI_AUDIO_PACKET_CONFIG, audio_packet_config);
1627 vc4_hdmi_set_n_cts(vc4_hdmi, sample_rate);
1629 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1631 memcpy(&vc4_hdmi->audio.infoframe, ¶ms->cea, sizeof(params->cea));
1632 vc4_hdmi_set_audio_infoframe(encoder);
1634 mutex_unlock(&vc4_hdmi->mutex);
1639 static const struct snd_soc_component_driver vc4_hdmi_audio_cpu_dai_comp = {
1640 .name = "vc4-hdmi-cpu-dai-component",
1643 static int vc4_hdmi_audio_cpu_dai_probe(struct snd_soc_dai *dai)
1645 struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai);
1647 snd_soc_dai_init_dma_data(dai, &vc4_hdmi->audio.dma_data, NULL);
1652 static struct snd_soc_dai_driver vc4_hdmi_audio_cpu_dai_drv = {
1653 .name = "vc4-hdmi-cpu-dai",
1654 .probe = vc4_hdmi_audio_cpu_dai_probe,
1656 .stream_name = "Playback",
1659 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
1660 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
1661 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
1662 SNDRV_PCM_RATE_192000,
1663 .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
1667 static const struct snd_dmaengine_pcm_config pcm_conf = {
1668 .chan_names[SNDRV_PCM_STREAM_PLAYBACK] = "audio-rx",
1669 .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
1672 static int vc4_hdmi_audio_get_eld(struct device *dev, void *data,
1673 uint8_t *buf, size_t len)
1675 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
1676 struct drm_connector *connector = &vc4_hdmi->connector;
1678 mutex_lock(&vc4_hdmi->mutex);
1679 memcpy(buf, connector->eld, min(sizeof(connector->eld), len));
1680 mutex_unlock(&vc4_hdmi->mutex);
1685 static const struct hdmi_codec_ops vc4_hdmi_codec_ops = {
1686 .get_eld = vc4_hdmi_audio_get_eld,
1687 .prepare = vc4_hdmi_audio_prepare,
1688 .audio_shutdown = vc4_hdmi_audio_shutdown,
1689 .audio_startup = vc4_hdmi_audio_startup,
1692 static struct hdmi_codec_pdata vc4_hdmi_codec_pdata = {
1693 .ops = &vc4_hdmi_codec_ops,
1694 .max_i2s_channels = 8,
1698 static int vc4_hdmi_audio_init(struct vc4_hdmi *vc4_hdmi)
1700 const struct vc4_hdmi_register *mai_data =
1701 &vc4_hdmi->variant->registers[HDMI_MAI_DATA];
1702 struct snd_soc_dai_link *dai_link = &vc4_hdmi->audio.link;
1703 struct snd_soc_card *card = &vc4_hdmi->audio.card;
1704 struct device *dev = &vc4_hdmi->pdev->dev;
1705 struct platform_device *codec_pdev;
1710 if (!of_find_property(dev->of_node, "dmas", NULL)) {
1712 "'dmas' DT property is missing, no HDMI audio\n");
1716 if (mai_data->reg != VC4_HD) {
1717 WARN_ONCE(true, "MAI isn't in the HD block\n");
1722 * Get the physical address of VC4_HD_MAI_DATA. We need to retrieve
1723 * the bus address specified in the DT, because the physical address
1724 * (the one returned by platform_get_resource()) is not appropriate
1725 * for DMA transfers.
1726 * This VC/MMU should probably be exposed to avoid this kind of hacks.
1728 index = of_property_match_string(dev->of_node, "reg-names", "hd");
1729 /* Before BCM2711, we don't have a named register range */
1733 addr = of_get_address(dev->of_node, index, NULL, NULL);
1735 vc4_hdmi->audio.dma_data.addr = be32_to_cpup(addr) + mai_data->offset;
1736 vc4_hdmi->audio.dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1737 vc4_hdmi->audio.dma_data.maxburst = 2;
1739 ret = devm_snd_dmaengine_pcm_register(dev, &pcm_conf, 0);
1741 dev_err(dev, "Could not register PCM component: %d\n", ret);
1745 ret = devm_snd_soc_register_component(dev, &vc4_hdmi_audio_cpu_dai_comp,
1746 &vc4_hdmi_audio_cpu_dai_drv, 1);
1748 dev_err(dev, "Could not register CPU DAI: %d\n", ret);
1752 codec_pdev = platform_device_register_data(dev, HDMI_CODEC_DRV_NAME,
1753 PLATFORM_DEVID_AUTO,
1754 &vc4_hdmi_codec_pdata,
1755 sizeof(vc4_hdmi_codec_pdata));
1756 if (IS_ERR(codec_pdev)) {
1757 dev_err(dev, "Couldn't register the HDMI codec: %ld\n", PTR_ERR(codec_pdev));
1758 return PTR_ERR(codec_pdev);
1760 vc4_hdmi->audio.codec_pdev = codec_pdev;
1762 dai_link->cpus = &vc4_hdmi->audio.cpu;
1763 dai_link->codecs = &vc4_hdmi->audio.codec;
1764 dai_link->platforms = &vc4_hdmi->audio.platform;
1766 dai_link->num_cpus = 1;
1767 dai_link->num_codecs = 1;
1768 dai_link->num_platforms = 1;
1770 dai_link->name = "MAI";
1771 dai_link->stream_name = "MAI PCM";
1772 dai_link->codecs->dai_name = "i2s-hifi";
1773 dai_link->cpus->dai_name = dev_name(dev);
1774 dai_link->codecs->name = dev_name(&codec_pdev->dev);
1775 dai_link->platforms->name = dev_name(dev);
1777 card->dai_link = dai_link;
1778 card->num_links = 1;
1779 card->name = vc4_hdmi->variant->card_name;
1780 card->driver_name = "vc4-hdmi";
1782 card->owner = THIS_MODULE;
1785 * Be careful, snd_soc_register_card() calls dev_set_drvdata() and
1786 * stores a pointer to the snd card object in dev->driver_data. This
1787 * means we cannot use it for something else. The hdmi back-pointer is
1788 * now stored in card->drvdata and should be retrieved with
1789 * snd_soc_card_get_drvdata() if needed.
1791 snd_soc_card_set_drvdata(card, vc4_hdmi);
1792 ret = devm_snd_soc_register_card(dev, card);
1794 dev_err_probe(dev, ret, "Could not register sound card\n");
1800 static void vc4_hdmi_audio_exit(struct vc4_hdmi *vc4_hdmi)
1802 platform_device_unregister(vc4_hdmi->audio.codec_pdev);
1803 vc4_hdmi->audio.codec_pdev = NULL;
1806 static irqreturn_t vc4_hdmi_hpd_irq_thread(int irq, void *priv)
1808 struct vc4_hdmi *vc4_hdmi = priv;
1809 struct drm_connector *connector = &vc4_hdmi->connector;
1810 struct drm_device *dev = connector->dev;
1812 if (dev && dev->registered)
1813 drm_connector_helper_hpd_irq_event(connector);
1818 static int vc4_hdmi_hotplug_init(struct vc4_hdmi *vc4_hdmi)
1820 struct drm_connector *connector = &vc4_hdmi->connector;
1821 struct platform_device *pdev = vc4_hdmi->pdev;
1824 if (vc4_hdmi->variant->external_irq_controller) {
1825 unsigned int hpd_con = platform_get_irq_byname(pdev, "hpd-connected");
1826 unsigned int hpd_rm = platform_get_irq_byname(pdev, "hpd-removed");
1828 ret = request_threaded_irq(hpd_con,
1830 vc4_hdmi_hpd_irq_thread, IRQF_ONESHOT,
1831 "vc4 hdmi hpd connected", vc4_hdmi);
1835 ret = request_threaded_irq(hpd_rm,
1837 vc4_hdmi_hpd_irq_thread, IRQF_ONESHOT,
1838 "vc4 hdmi hpd disconnected", vc4_hdmi);
1840 free_irq(hpd_con, vc4_hdmi);
1844 connector->polled = DRM_CONNECTOR_POLL_HPD;
1850 static void vc4_hdmi_hotplug_exit(struct vc4_hdmi *vc4_hdmi)
1852 struct platform_device *pdev = vc4_hdmi->pdev;
1854 if (vc4_hdmi->variant->external_irq_controller) {
1855 free_irq(platform_get_irq_byname(pdev, "hpd-connected"), vc4_hdmi);
1856 free_irq(platform_get_irq_byname(pdev, "hpd-removed"), vc4_hdmi);
1860 #ifdef CONFIG_DRM_VC4_HDMI_CEC
1861 static irqreturn_t vc4_cec_irq_handler_rx_thread(int irq, void *priv)
1863 struct vc4_hdmi *vc4_hdmi = priv;
1865 if (vc4_hdmi->cec_rx_msg.len)
1866 cec_received_msg(vc4_hdmi->cec_adap,
1867 &vc4_hdmi->cec_rx_msg);
1872 static irqreturn_t vc4_cec_irq_handler_tx_thread(int irq, void *priv)
1874 struct vc4_hdmi *vc4_hdmi = priv;
1876 if (vc4_hdmi->cec_tx_ok) {
1877 cec_transmit_done(vc4_hdmi->cec_adap, CEC_TX_STATUS_OK,
1881 * This CEC implementation makes 1 retry, so if we
1882 * get a NACK, then that means it made 2 attempts.
1884 cec_transmit_done(vc4_hdmi->cec_adap, CEC_TX_STATUS_NACK,
1890 static irqreturn_t vc4_cec_irq_handler_thread(int irq, void *priv)
1892 struct vc4_hdmi *vc4_hdmi = priv;
1895 if (vc4_hdmi->cec_irq_was_rx)
1896 ret = vc4_cec_irq_handler_rx_thread(irq, priv);
1898 ret = vc4_cec_irq_handler_tx_thread(irq, priv);
1903 static void vc4_cec_read_msg(struct vc4_hdmi *vc4_hdmi, u32 cntrl1)
1905 struct drm_device *dev = vc4_hdmi->connector.dev;
1906 struct cec_msg *msg = &vc4_hdmi->cec_rx_msg;
1909 lockdep_assert_held(&vc4_hdmi->hw_lock);
1911 msg->len = 1 + ((cntrl1 & VC4_HDMI_CEC_REC_WRD_CNT_MASK) >>
1912 VC4_HDMI_CEC_REC_WRD_CNT_SHIFT);
1914 if (msg->len > 16) {
1915 drm_err(dev, "Attempting to read too much data (%d)\n", msg->len);
1919 for (i = 0; i < msg->len; i += 4) {
1920 u32 val = HDMI_READ(HDMI_CEC_RX_DATA_1 + (i >> 2));
1922 msg->msg[i] = val & 0xff;
1923 msg->msg[i + 1] = (val >> 8) & 0xff;
1924 msg->msg[i + 2] = (val >> 16) & 0xff;
1925 msg->msg[i + 3] = (val >> 24) & 0xff;
1929 static irqreturn_t vc4_cec_irq_handler_tx_bare_locked(struct vc4_hdmi *vc4_hdmi)
1933 lockdep_assert_held(&vc4_hdmi->hw_lock);
1935 cntrl1 = HDMI_READ(HDMI_CEC_CNTRL_1);
1936 vc4_hdmi->cec_tx_ok = cntrl1 & VC4_HDMI_CEC_TX_STATUS_GOOD;
1937 cntrl1 &= ~VC4_HDMI_CEC_START_XMIT_BEGIN;
1938 HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1);
1940 return IRQ_WAKE_THREAD;
1943 static irqreturn_t vc4_cec_irq_handler_tx_bare(int irq, void *priv)
1945 struct vc4_hdmi *vc4_hdmi = priv;
1948 spin_lock(&vc4_hdmi->hw_lock);
1949 ret = vc4_cec_irq_handler_tx_bare_locked(vc4_hdmi);
1950 spin_unlock(&vc4_hdmi->hw_lock);
1955 static irqreturn_t vc4_cec_irq_handler_rx_bare_locked(struct vc4_hdmi *vc4_hdmi)
1959 lockdep_assert_held(&vc4_hdmi->hw_lock);
1961 vc4_hdmi->cec_rx_msg.len = 0;
1962 cntrl1 = HDMI_READ(HDMI_CEC_CNTRL_1);
1963 vc4_cec_read_msg(vc4_hdmi, cntrl1);
1964 cntrl1 |= VC4_HDMI_CEC_CLEAR_RECEIVE_OFF;
1965 HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1);
1966 cntrl1 &= ~VC4_HDMI_CEC_CLEAR_RECEIVE_OFF;
1968 HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1);
1970 return IRQ_WAKE_THREAD;
1973 static irqreturn_t vc4_cec_irq_handler_rx_bare(int irq, void *priv)
1975 struct vc4_hdmi *vc4_hdmi = priv;
1978 spin_lock(&vc4_hdmi->hw_lock);
1979 ret = vc4_cec_irq_handler_rx_bare_locked(vc4_hdmi);
1980 spin_unlock(&vc4_hdmi->hw_lock);
1985 static irqreturn_t vc4_cec_irq_handler(int irq, void *priv)
1987 struct vc4_hdmi *vc4_hdmi = priv;
1988 u32 stat = HDMI_READ(HDMI_CEC_CPU_STATUS);
1992 if (!(stat & VC4_HDMI_CPU_CEC))
1995 spin_lock(&vc4_hdmi->hw_lock);
1996 cntrl5 = HDMI_READ(HDMI_CEC_CNTRL_5);
1997 vc4_hdmi->cec_irq_was_rx = cntrl5 & VC4_HDMI_CEC_RX_CEC_INT;
1998 if (vc4_hdmi->cec_irq_was_rx)
1999 ret = vc4_cec_irq_handler_rx_bare_locked(vc4_hdmi);
2001 ret = vc4_cec_irq_handler_tx_bare_locked(vc4_hdmi);
2003 HDMI_WRITE(HDMI_CEC_CPU_CLEAR, VC4_HDMI_CPU_CEC);
2004 spin_unlock(&vc4_hdmi->hw_lock);
2009 static int vc4_hdmi_cec_enable(struct cec_adapter *adap)
2011 struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
2012 /* clock period in microseconds */
2013 const u32 usecs = 1000000 / CEC_CLOCK_FREQ;
2014 unsigned long flags;
2019 * NOTE: This function should really take vc4_hdmi->mutex, but doing so
2020 * results in a reentrancy since cec_s_phys_addr_from_edid() called in
2021 * .detect or .get_modes might call .adap_enable, which leads to this
2022 * function being called with that mutex held.
2024 * Concurrency is not an issue for the moment since we don't share any
2025 * state with KMS, so we can ignore the lock for now, but we need to
2026 * keep it in mind if we were to change that assumption.
2029 ret = pm_runtime_resume_and_get(&vc4_hdmi->pdev->dev);
2033 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2035 val = HDMI_READ(HDMI_CEC_CNTRL_5);
2036 val &= ~(VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET |
2037 VC4_HDMI_CEC_CNT_TO_4700_US_MASK |
2038 VC4_HDMI_CEC_CNT_TO_4500_US_MASK);
2039 val |= ((4700 / usecs) << VC4_HDMI_CEC_CNT_TO_4700_US_SHIFT) |
2040 ((4500 / usecs) << VC4_HDMI_CEC_CNT_TO_4500_US_SHIFT);
2042 HDMI_WRITE(HDMI_CEC_CNTRL_5, val |
2043 VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET);
2044 HDMI_WRITE(HDMI_CEC_CNTRL_5, val);
2045 HDMI_WRITE(HDMI_CEC_CNTRL_2,
2046 ((1500 / usecs) << VC4_HDMI_CEC_CNT_TO_1500_US_SHIFT) |
2047 ((1300 / usecs) << VC4_HDMI_CEC_CNT_TO_1300_US_SHIFT) |
2048 ((800 / usecs) << VC4_HDMI_CEC_CNT_TO_800_US_SHIFT) |
2049 ((600 / usecs) << VC4_HDMI_CEC_CNT_TO_600_US_SHIFT) |
2050 ((400 / usecs) << VC4_HDMI_CEC_CNT_TO_400_US_SHIFT));
2051 HDMI_WRITE(HDMI_CEC_CNTRL_3,
2052 ((2750 / usecs) << VC4_HDMI_CEC_CNT_TO_2750_US_SHIFT) |
2053 ((2400 / usecs) << VC4_HDMI_CEC_CNT_TO_2400_US_SHIFT) |
2054 ((2050 / usecs) << VC4_HDMI_CEC_CNT_TO_2050_US_SHIFT) |
2055 ((1700 / usecs) << VC4_HDMI_CEC_CNT_TO_1700_US_SHIFT));
2056 HDMI_WRITE(HDMI_CEC_CNTRL_4,
2057 ((4300 / usecs) << VC4_HDMI_CEC_CNT_TO_4300_US_SHIFT) |
2058 ((3900 / usecs) << VC4_HDMI_CEC_CNT_TO_3900_US_SHIFT) |
2059 ((3600 / usecs) << VC4_HDMI_CEC_CNT_TO_3600_US_SHIFT) |
2060 ((3500 / usecs) << VC4_HDMI_CEC_CNT_TO_3500_US_SHIFT));
2062 if (!vc4_hdmi->variant->external_irq_controller)
2063 HDMI_WRITE(HDMI_CEC_CPU_MASK_CLEAR, VC4_HDMI_CPU_CEC);
2065 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2070 static int vc4_hdmi_cec_disable(struct cec_adapter *adap)
2072 struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
2073 unsigned long flags;
2076 * NOTE: This function should really take vc4_hdmi->mutex, but doing so
2077 * results in a reentrancy since cec_s_phys_addr_from_edid() called in
2078 * .detect or .get_modes might call .adap_enable, which leads to this
2079 * function being called with that mutex held.
2081 * Concurrency is not an issue for the moment since we don't share any
2082 * state with KMS, so we can ignore the lock for now, but we need to
2083 * keep it in mind if we were to change that assumption.
2086 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2088 if (!vc4_hdmi->variant->external_irq_controller)
2089 HDMI_WRITE(HDMI_CEC_CPU_MASK_SET, VC4_HDMI_CPU_CEC);
2091 HDMI_WRITE(HDMI_CEC_CNTRL_5, HDMI_READ(HDMI_CEC_CNTRL_5) |
2092 VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET);
2094 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2096 pm_runtime_put(&vc4_hdmi->pdev->dev);
2101 static int vc4_hdmi_cec_adap_enable(struct cec_adapter *adap, bool enable)
2104 return vc4_hdmi_cec_enable(adap);
2106 return vc4_hdmi_cec_disable(adap);
2109 static int vc4_hdmi_cec_adap_log_addr(struct cec_adapter *adap, u8 log_addr)
2111 struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
2112 unsigned long flags;
2115 * NOTE: This function should really take vc4_hdmi->mutex, but doing so
2116 * results in a reentrancy since cec_s_phys_addr_from_edid() called in
2117 * .detect or .get_modes might call .adap_enable, which leads to this
2118 * function being called with that mutex held.
2120 * Concurrency is not an issue for the moment since we don't share any
2121 * state with KMS, so we can ignore the lock for now, but we need to
2122 * keep it in mind if we were to change that assumption.
2125 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2126 HDMI_WRITE(HDMI_CEC_CNTRL_1,
2127 (HDMI_READ(HDMI_CEC_CNTRL_1) & ~VC4_HDMI_CEC_ADDR_MASK) |
2128 (log_addr & 0xf) << VC4_HDMI_CEC_ADDR_SHIFT);
2129 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2134 static int vc4_hdmi_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
2135 u32 signal_free_time, struct cec_msg *msg)
2137 struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
2138 struct drm_device *dev = vc4_hdmi->connector.dev;
2139 unsigned long flags;
2144 * NOTE: This function should really take vc4_hdmi->mutex, but doing so
2145 * results in a reentrancy since cec_s_phys_addr_from_edid() called in
2146 * .detect or .get_modes might call .adap_enable, which leads to this
2147 * function being called with that mutex held.
2149 * Concurrency is not an issue for the moment since we don't share any
2150 * state with KMS, so we can ignore the lock for now, but we need to
2151 * keep it in mind if we were to change that assumption.
2154 if (msg->len > 16) {
2155 drm_err(dev, "Attempting to transmit too much data (%d)\n", msg->len);
2159 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2161 for (i = 0; i < msg->len; i += 4)
2162 HDMI_WRITE(HDMI_CEC_TX_DATA_1 + (i >> 2),
2164 (msg->msg[i + 1] << 8) |
2165 (msg->msg[i + 2] << 16) |
2166 (msg->msg[i + 3] << 24));
2168 val = HDMI_READ(HDMI_CEC_CNTRL_1);
2169 val &= ~VC4_HDMI_CEC_START_XMIT_BEGIN;
2170 HDMI_WRITE(HDMI_CEC_CNTRL_1, val);
2171 val &= ~VC4_HDMI_CEC_MESSAGE_LENGTH_MASK;
2172 val |= (msg->len - 1) << VC4_HDMI_CEC_MESSAGE_LENGTH_SHIFT;
2173 val |= VC4_HDMI_CEC_START_XMIT_BEGIN;
2175 HDMI_WRITE(HDMI_CEC_CNTRL_1, val);
2177 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2182 static const struct cec_adap_ops vc4_hdmi_cec_adap_ops = {
2183 .adap_enable = vc4_hdmi_cec_adap_enable,
2184 .adap_log_addr = vc4_hdmi_cec_adap_log_addr,
2185 .adap_transmit = vc4_hdmi_cec_adap_transmit,
2188 static int vc4_hdmi_cec_init(struct vc4_hdmi *vc4_hdmi)
2190 struct cec_connector_info conn_info;
2191 struct platform_device *pdev = vc4_hdmi->pdev;
2192 struct device *dev = &pdev->dev;
2193 unsigned long flags;
2197 if (!of_find_property(dev->of_node, "interrupts", NULL)) {
2198 dev_warn(dev, "'interrupts' DT property is missing, no CEC\n");
2202 vc4_hdmi->cec_adap = cec_allocate_adapter(&vc4_hdmi_cec_adap_ops,
2205 CEC_CAP_CONNECTOR_INFO, 1);
2206 ret = PTR_ERR_OR_ZERO(vc4_hdmi->cec_adap);
2210 cec_fill_conn_info_from_drm(&conn_info, &vc4_hdmi->connector);
2211 cec_s_conn_info(vc4_hdmi->cec_adap, &conn_info);
2213 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2214 value = HDMI_READ(HDMI_CEC_CNTRL_1);
2215 /* Set the logical address to Unregistered */
2216 value |= VC4_HDMI_CEC_ADDR_MASK;
2217 HDMI_WRITE(HDMI_CEC_CNTRL_1, value);
2218 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2220 vc4_hdmi_cec_update_clk_div(vc4_hdmi);
2222 if (vc4_hdmi->variant->external_irq_controller) {
2223 ret = request_threaded_irq(platform_get_irq_byname(pdev, "cec-rx"),
2224 vc4_cec_irq_handler_rx_bare,
2225 vc4_cec_irq_handler_rx_thread, 0,
2226 "vc4 hdmi cec rx", vc4_hdmi);
2228 goto err_delete_cec_adap;
2230 ret = request_threaded_irq(platform_get_irq_byname(pdev, "cec-tx"),
2231 vc4_cec_irq_handler_tx_bare,
2232 vc4_cec_irq_handler_tx_thread, 0,
2233 "vc4 hdmi cec tx", vc4_hdmi);
2235 goto err_remove_cec_rx_handler;
2237 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2238 HDMI_WRITE(HDMI_CEC_CPU_MASK_SET, 0xffffffff);
2239 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2241 ret = request_threaded_irq(platform_get_irq(pdev, 0),
2242 vc4_cec_irq_handler,
2243 vc4_cec_irq_handler_thread, 0,
2244 "vc4 hdmi cec", vc4_hdmi);
2246 goto err_delete_cec_adap;
2249 ret = cec_register_adapter(vc4_hdmi->cec_adap, &pdev->dev);
2251 goto err_remove_handlers;
2255 err_remove_handlers:
2256 if (vc4_hdmi->variant->external_irq_controller)
2257 free_irq(platform_get_irq_byname(pdev, "cec-tx"), vc4_hdmi);
2259 free_irq(platform_get_irq(pdev, 0), vc4_hdmi);
2261 err_remove_cec_rx_handler:
2262 if (vc4_hdmi->variant->external_irq_controller)
2263 free_irq(platform_get_irq_byname(pdev, "cec-rx"), vc4_hdmi);
2265 err_delete_cec_adap:
2266 cec_delete_adapter(vc4_hdmi->cec_adap);
2271 static void vc4_hdmi_cec_exit(struct vc4_hdmi *vc4_hdmi)
2273 struct platform_device *pdev = vc4_hdmi->pdev;
2275 if (vc4_hdmi->variant->external_irq_controller) {
2276 free_irq(platform_get_irq_byname(pdev, "cec-rx"), vc4_hdmi);
2277 free_irq(platform_get_irq_byname(pdev, "cec-tx"), vc4_hdmi);
2279 free_irq(platform_get_irq(pdev, 0), vc4_hdmi);
2282 cec_unregister_adapter(vc4_hdmi->cec_adap);
2285 static int vc4_hdmi_cec_init(struct vc4_hdmi *vc4_hdmi)
2290 static void vc4_hdmi_cec_exit(struct vc4_hdmi *vc4_hdmi) {};
2294 static int vc4_hdmi_build_regset(struct vc4_hdmi *vc4_hdmi,
2295 struct debugfs_regset32 *regset,
2296 enum vc4_hdmi_regs reg)
2298 const struct vc4_hdmi_variant *variant = vc4_hdmi->variant;
2299 struct debugfs_reg32 *regs, *new_regs;
2300 unsigned int count = 0;
2303 regs = kcalloc(variant->num_registers, sizeof(*regs),
2308 for (i = 0; i < variant->num_registers; i++) {
2309 const struct vc4_hdmi_register *field = &variant->registers[i];
2311 if (field->reg != reg)
2314 regs[count].name = field->name;
2315 regs[count].offset = field->offset;
2319 new_regs = krealloc(regs, count * sizeof(*regs), GFP_KERNEL);
2323 regset->base = __vc4_hdmi_get_field_base(vc4_hdmi, reg);
2324 regset->regs = new_regs;
2325 regset->nregs = count;
2330 static int vc4_hdmi_init_resources(struct vc4_hdmi *vc4_hdmi)
2332 struct platform_device *pdev = vc4_hdmi->pdev;
2333 struct device *dev = &pdev->dev;
2336 vc4_hdmi->hdmicore_regs = vc4_ioremap_regs(pdev, 0);
2337 if (IS_ERR(vc4_hdmi->hdmicore_regs))
2338 return PTR_ERR(vc4_hdmi->hdmicore_regs);
2340 vc4_hdmi->hd_regs = vc4_ioremap_regs(pdev, 1);
2341 if (IS_ERR(vc4_hdmi->hd_regs))
2342 return PTR_ERR(vc4_hdmi->hd_regs);
2344 ret = vc4_hdmi_build_regset(vc4_hdmi, &vc4_hdmi->hd_regset, VC4_HD);
2348 ret = vc4_hdmi_build_regset(vc4_hdmi, &vc4_hdmi->hdmi_regset, VC4_HDMI);
2352 vc4_hdmi->pixel_clock = devm_clk_get(dev, "pixel");
2353 if (IS_ERR(vc4_hdmi->pixel_clock)) {
2354 ret = PTR_ERR(vc4_hdmi->pixel_clock);
2355 if (ret != -EPROBE_DEFER)
2356 DRM_ERROR("Failed to get pixel clock\n");
2360 vc4_hdmi->hsm_clock = devm_clk_get(dev, "hdmi");
2361 if (IS_ERR(vc4_hdmi->hsm_clock)) {
2362 DRM_ERROR("Failed to get HDMI state machine clock\n");
2363 return PTR_ERR(vc4_hdmi->hsm_clock);
2365 vc4_hdmi->audio_clock = vc4_hdmi->hsm_clock;
2366 vc4_hdmi->cec_clock = vc4_hdmi->hsm_clock;
2371 static int vc5_hdmi_init_resources(struct vc4_hdmi *vc4_hdmi)
2373 struct platform_device *pdev = vc4_hdmi->pdev;
2374 struct device *dev = &pdev->dev;
2375 struct resource *res;
2377 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hdmi");
2381 vc4_hdmi->hdmicore_regs = devm_ioremap(dev, res->start,
2382 resource_size(res));
2383 if (!vc4_hdmi->hdmicore_regs)
2386 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hd");
2390 vc4_hdmi->hd_regs = devm_ioremap(dev, res->start, resource_size(res));
2391 if (!vc4_hdmi->hd_regs)
2394 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cec");
2398 vc4_hdmi->cec_regs = devm_ioremap(dev, res->start, resource_size(res));
2399 if (!vc4_hdmi->cec_regs)
2402 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "csc");
2406 vc4_hdmi->csc_regs = devm_ioremap(dev, res->start, resource_size(res));
2407 if (!vc4_hdmi->csc_regs)
2410 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dvp");
2414 vc4_hdmi->dvp_regs = devm_ioremap(dev, res->start, resource_size(res));
2415 if (!vc4_hdmi->dvp_regs)
2418 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy");
2422 vc4_hdmi->phy_regs = devm_ioremap(dev, res->start, resource_size(res));
2423 if (!vc4_hdmi->phy_regs)
2426 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "packet");
2430 vc4_hdmi->ram_regs = devm_ioremap(dev, res->start, resource_size(res));
2431 if (!vc4_hdmi->ram_regs)
2434 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rm");
2438 vc4_hdmi->rm_regs = devm_ioremap(dev, res->start, resource_size(res));
2439 if (!vc4_hdmi->rm_regs)
2442 vc4_hdmi->hsm_clock = devm_clk_get(dev, "hdmi");
2443 if (IS_ERR(vc4_hdmi->hsm_clock)) {
2444 DRM_ERROR("Failed to get HDMI state machine clock\n");
2445 return PTR_ERR(vc4_hdmi->hsm_clock);
2448 vc4_hdmi->pixel_bvb_clock = devm_clk_get(dev, "bvb");
2449 if (IS_ERR(vc4_hdmi->pixel_bvb_clock)) {
2450 DRM_ERROR("Failed to get pixel bvb clock\n");
2451 return PTR_ERR(vc4_hdmi->pixel_bvb_clock);
2454 vc4_hdmi->audio_clock = devm_clk_get(dev, "audio");
2455 if (IS_ERR(vc4_hdmi->audio_clock)) {
2456 DRM_ERROR("Failed to get audio clock\n");
2457 return PTR_ERR(vc4_hdmi->audio_clock);
2460 vc4_hdmi->cec_clock = devm_clk_get(dev, "cec");
2461 if (IS_ERR(vc4_hdmi->cec_clock)) {
2462 DRM_ERROR("Failed to get CEC clock\n");
2463 return PTR_ERR(vc4_hdmi->cec_clock);
2466 vc4_hdmi->reset = devm_reset_control_get(dev, NULL);
2467 if (IS_ERR(vc4_hdmi->reset)) {
2468 DRM_ERROR("Failed to get HDMI reset line\n");
2469 return PTR_ERR(vc4_hdmi->reset);
2475 static int __maybe_unused vc4_hdmi_runtime_suspend(struct device *dev)
2477 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
2479 clk_disable_unprepare(vc4_hdmi->hsm_clock);
2484 static int vc4_hdmi_runtime_resume(struct device *dev)
2486 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
2489 ret = clk_prepare_enable(vc4_hdmi->hsm_clock);
2496 static int vc4_hdmi_bind(struct device *dev, struct device *master, void *data)
2498 const struct vc4_hdmi_variant *variant = of_device_get_match_data(dev);
2499 struct platform_device *pdev = to_platform_device(dev);
2500 struct drm_device *drm = dev_get_drvdata(master);
2501 struct vc4_hdmi *vc4_hdmi;
2502 struct drm_encoder *encoder;
2503 struct device_node *ddc_node;
2506 vc4_hdmi = devm_kzalloc(dev, sizeof(*vc4_hdmi), GFP_KERNEL);
2509 mutex_init(&vc4_hdmi->mutex);
2510 spin_lock_init(&vc4_hdmi->hw_lock);
2511 INIT_DELAYED_WORK(&vc4_hdmi->scrambling_work, vc4_hdmi_scrambling_wq);
2513 dev_set_drvdata(dev, vc4_hdmi);
2514 encoder = &vc4_hdmi->encoder.base.base;
2515 vc4_hdmi->encoder.base.type = variant->encoder_type;
2516 vc4_hdmi->encoder.base.pre_crtc_configure = vc4_hdmi_encoder_pre_crtc_configure;
2517 vc4_hdmi->encoder.base.pre_crtc_enable = vc4_hdmi_encoder_pre_crtc_enable;
2518 vc4_hdmi->encoder.base.post_crtc_enable = vc4_hdmi_encoder_post_crtc_enable;
2519 vc4_hdmi->encoder.base.post_crtc_disable = vc4_hdmi_encoder_post_crtc_disable;
2520 vc4_hdmi->encoder.base.post_crtc_powerdown = vc4_hdmi_encoder_post_crtc_powerdown;
2521 vc4_hdmi->pdev = pdev;
2522 vc4_hdmi->variant = variant;
2525 * Since we don't know the state of the controller and its
2526 * display (if any), let's assume it's always enabled.
2527 * vc4_hdmi_disable_scrambling() will thus run at boot, make
2528 * sure it's disabled, and avoid any inconsistency.
2530 if (variant->max_pixel_clock > HDMI_14_MAX_TMDS_CLK)
2531 vc4_hdmi->scdc_enabled = true;
2533 ret = variant->init_resources(vc4_hdmi);
2537 ddc_node = of_parse_phandle(dev->of_node, "ddc", 0);
2539 DRM_ERROR("Failed to find ddc node in device tree\n");
2543 vc4_hdmi->ddc = of_find_i2c_adapter_by_node(ddc_node);
2544 of_node_put(ddc_node);
2545 if (!vc4_hdmi->ddc) {
2546 DRM_DEBUG("Failed to get ddc i2c adapter by node\n");
2547 return -EPROBE_DEFER;
2550 /* Only use the GPIO HPD pin if present in the DT, otherwise
2551 * we'll use the HDMI core's register.
2553 vc4_hdmi->hpd_gpio = devm_gpiod_get_optional(dev, "hpd", GPIOD_IN);
2554 if (IS_ERR(vc4_hdmi->hpd_gpio)) {
2555 ret = PTR_ERR(vc4_hdmi->hpd_gpio);
2559 vc4_hdmi->disable_wifi_frequencies =
2560 of_property_read_bool(dev->of_node, "wifi-2.4ghz-coexistence");
2562 if (variant->max_pixel_clock == 600000000) {
2563 struct vc4_dev *vc4 = to_vc4_dev(drm);
2564 long max_rate = clk_round_rate(vc4->hvs->core_clk, 550000000);
2566 if (max_rate < 550000000)
2567 vc4_hdmi->disable_4kp60 = true;
2571 * If we boot without any cable connected to the HDMI connector,
2572 * the firmware will skip the HSM initialization and leave it
2573 * with a rate of 0, resulting in a bus lockup when we're
2574 * accessing the registers even if it's enabled.
2576 * Let's put a sensible default at runtime_resume so that we
2577 * don't end up in this situation.
2579 ret = clk_set_min_rate(vc4_hdmi->hsm_clock, HSM_MIN_CLOCK_FREQ);
2584 * We need to have the device powered up at this point to call
2585 * our reset hook and for the CEC init.
2587 ret = vc4_hdmi_runtime_resume(dev);
2591 pm_runtime_get_noresume(dev);
2592 pm_runtime_set_active(dev);
2593 pm_runtime_enable(dev);
2595 if (vc4_hdmi->variant->reset)
2596 vc4_hdmi->variant->reset(vc4_hdmi);
2598 if ((of_device_is_compatible(dev->of_node, "brcm,bcm2711-hdmi0") ||
2599 of_device_is_compatible(dev->of_node, "brcm,bcm2711-hdmi1")) &&
2600 HDMI_READ(HDMI_VID_CTL) & VC4_HD_VID_CTL_ENABLE) {
2601 clk_prepare_enable(vc4_hdmi->pixel_clock);
2602 clk_prepare_enable(vc4_hdmi->hsm_clock);
2603 clk_prepare_enable(vc4_hdmi->pixel_bvb_clock);
2606 drm_simple_encoder_init(drm, encoder, DRM_MODE_ENCODER_TMDS);
2607 drm_encoder_helper_add(encoder, &vc4_hdmi_encoder_helper_funcs);
2609 ret = vc4_hdmi_connector_init(drm, vc4_hdmi);
2611 goto err_destroy_encoder;
2613 ret = vc4_hdmi_hotplug_init(vc4_hdmi);
2615 goto err_destroy_conn;
2617 ret = vc4_hdmi_cec_init(vc4_hdmi);
2619 goto err_free_hotplug;
2621 ret = vc4_hdmi_audio_init(vc4_hdmi);
2625 vc4_debugfs_add_file(drm, variant->debugfs_name,
2626 vc4_hdmi_debugfs_regs,
2629 pm_runtime_put_sync(dev);
2634 vc4_hdmi_cec_exit(vc4_hdmi);
2636 vc4_hdmi_hotplug_exit(vc4_hdmi);
2638 vc4_hdmi_connector_destroy(&vc4_hdmi->connector);
2639 err_destroy_encoder:
2640 drm_encoder_cleanup(encoder);
2641 pm_runtime_put_sync(dev);
2642 pm_runtime_disable(dev);
2644 put_device(&vc4_hdmi->ddc->dev);
2649 static void vc4_hdmi_unbind(struct device *dev, struct device *master,
2652 struct vc4_hdmi *vc4_hdmi;
2655 * ASoC makes it a bit hard to retrieve a pointer to the
2656 * vc4_hdmi structure. Registering the card will overwrite our
2657 * device drvdata with a pointer to the snd_soc_card structure,
2658 * which can then be used to retrieve whatever drvdata we want
2661 * However, that doesn't fly in the case where we wouldn't
2662 * register an ASoC card (because of an old DT that is missing
2663 * the dmas properties for example), then the card isn't
2664 * registered and the device drvdata wouldn't be set.
2666 * We can deal with both cases by making sure a snd_soc_card
2667 * pointer and a vc4_hdmi structure are pointing to the same
2668 * memory address, so we can treat them indistinctly without any
2671 BUILD_BUG_ON(offsetof(struct vc4_hdmi_audio, card) != 0);
2672 BUILD_BUG_ON(offsetof(struct vc4_hdmi, audio) != 0);
2673 vc4_hdmi = dev_get_drvdata(dev);
2675 kfree(vc4_hdmi->hdmi_regset.regs);
2676 kfree(vc4_hdmi->hd_regset.regs);
2678 vc4_hdmi_audio_exit(vc4_hdmi);
2679 vc4_hdmi_cec_exit(vc4_hdmi);
2680 vc4_hdmi_hotplug_exit(vc4_hdmi);
2681 vc4_hdmi_connector_destroy(&vc4_hdmi->connector);
2682 drm_encoder_cleanup(&vc4_hdmi->encoder.base.base);
2684 pm_runtime_disable(dev);
2686 put_device(&vc4_hdmi->ddc->dev);
2689 static const struct component_ops vc4_hdmi_ops = {
2690 .bind = vc4_hdmi_bind,
2691 .unbind = vc4_hdmi_unbind,
2694 static int vc4_hdmi_dev_probe(struct platform_device *pdev)
2696 return component_add(&pdev->dev, &vc4_hdmi_ops);
2699 static int vc4_hdmi_dev_remove(struct platform_device *pdev)
2701 component_del(&pdev->dev, &vc4_hdmi_ops);
2705 static const struct vc4_hdmi_variant bcm2835_variant = {
2706 .encoder_type = VC4_ENCODER_TYPE_HDMI0,
2707 .debugfs_name = "hdmi_regs",
2708 .card_name = "vc4-hdmi",
2709 .max_pixel_clock = 162000000,
2710 .registers = vc4_hdmi_fields,
2711 .num_registers = ARRAY_SIZE(vc4_hdmi_fields),
2713 .init_resources = vc4_hdmi_init_resources,
2714 .csc_setup = vc4_hdmi_csc_setup,
2715 .reset = vc4_hdmi_reset,
2716 .set_timings = vc4_hdmi_set_timings,
2717 .phy_init = vc4_hdmi_phy_init,
2718 .phy_disable = vc4_hdmi_phy_disable,
2719 .phy_rng_enable = vc4_hdmi_phy_rng_enable,
2720 .phy_rng_disable = vc4_hdmi_phy_rng_disable,
2721 .channel_map = vc4_hdmi_channel_map,
2722 .supports_hdr = false,
2725 static const struct vc4_hdmi_variant bcm2711_hdmi0_variant = {
2726 .encoder_type = VC4_ENCODER_TYPE_HDMI0,
2727 .debugfs_name = "hdmi0_regs",
2728 .card_name = "vc4-hdmi-0",
2729 .max_pixel_clock = 600000000,
2730 .registers = vc5_hdmi_hdmi0_fields,
2731 .num_registers = ARRAY_SIZE(vc5_hdmi_hdmi0_fields),
2732 .phy_lane_mapping = {
2738 .unsupported_odd_h_timings = true,
2739 .external_irq_controller = true,
2741 .init_resources = vc5_hdmi_init_resources,
2742 .csc_setup = vc5_hdmi_csc_setup,
2743 .reset = vc5_hdmi_reset,
2744 .set_timings = vc5_hdmi_set_timings,
2745 .phy_init = vc5_hdmi_phy_init,
2746 .phy_disable = vc5_hdmi_phy_disable,
2747 .phy_rng_enable = vc5_hdmi_phy_rng_enable,
2748 .phy_rng_disable = vc5_hdmi_phy_rng_disable,
2749 .channel_map = vc5_hdmi_channel_map,
2750 .supports_hdr = true,
2751 .hp_detect = vc5_hdmi_hp_detect,
2754 static const struct vc4_hdmi_variant bcm2711_hdmi1_variant = {
2755 .encoder_type = VC4_ENCODER_TYPE_HDMI1,
2756 .debugfs_name = "hdmi1_regs",
2757 .card_name = "vc4-hdmi-1",
2758 .max_pixel_clock = HDMI_14_MAX_TMDS_CLK,
2759 .registers = vc5_hdmi_hdmi1_fields,
2760 .num_registers = ARRAY_SIZE(vc5_hdmi_hdmi1_fields),
2761 .phy_lane_mapping = {
2767 .unsupported_odd_h_timings = true,
2768 .external_irq_controller = true,
2770 .init_resources = vc5_hdmi_init_resources,
2771 .csc_setup = vc5_hdmi_csc_setup,
2772 .reset = vc5_hdmi_reset,
2773 .set_timings = vc5_hdmi_set_timings,
2774 .phy_init = vc5_hdmi_phy_init,
2775 .phy_disable = vc5_hdmi_phy_disable,
2776 .phy_rng_enable = vc5_hdmi_phy_rng_enable,
2777 .phy_rng_disable = vc5_hdmi_phy_rng_disable,
2778 .channel_map = vc5_hdmi_channel_map,
2779 .supports_hdr = true,
2780 .hp_detect = vc5_hdmi_hp_detect,
2783 static const struct of_device_id vc4_hdmi_dt_match[] = {
2784 { .compatible = "brcm,bcm2835-hdmi", .data = &bcm2835_variant },
2785 { .compatible = "brcm,bcm2711-hdmi0", .data = &bcm2711_hdmi0_variant },
2786 { .compatible = "brcm,bcm2711-hdmi1", .data = &bcm2711_hdmi1_variant },
2790 static const struct dev_pm_ops vc4_hdmi_pm_ops = {
2791 SET_RUNTIME_PM_OPS(vc4_hdmi_runtime_suspend,
2792 vc4_hdmi_runtime_resume,
2796 struct platform_driver vc4_hdmi_driver = {
2797 .probe = vc4_hdmi_dev_probe,
2798 .remove = vc4_hdmi_dev_remove,
2801 .of_match_table = vc4_hdmi_dt_match,
2802 .pm = &vc4_hdmi_pm_ops,