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 unsigned long long
103 vc4_hdmi_encoder_compute_mode_clock(const struct drm_display_mode *mode,
106 static bool vc4_hdmi_mode_needs_scrambling(const struct drm_display_mode *mode,
109 unsigned long long clock = vc4_hdmi_encoder_compute_mode_clock(mode, bpc);
111 return clock > HDMI_14_MAX_TMDS_CLK;
114 static bool vc4_hdmi_is_full_range_rgb(struct vc4_hdmi *vc4_hdmi,
115 const struct drm_display_mode *mode)
117 struct vc4_hdmi_encoder *vc4_encoder = &vc4_hdmi->encoder;
119 return !vc4_encoder->hdmi_monitor ||
120 drm_default_rgb_quant_range(mode) == HDMI_QUANTIZATION_RANGE_FULL;
123 static int vc4_hdmi_debugfs_regs(struct seq_file *m, void *unused)
125 struct drm_info_node *node = (struct drm_info_node *)m->private;
126 struct vc4_hdmi *vc4_hdmi = node->info_ent->data;
127 struct drm_printer p = drm_seq_file_printer(m);
129 drm_print_regset32(&p, &vc4_hdmi->hdmi_regset);
130 drm_print_regset32(&p, &vc4_hdmi->hd_regset);
135 static void vc4_hdmi_reset(struct vc4_hdmi *vc4_hdmi)
139 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
141 HDMI_WRITE(HDMI_M_CTL, VC4_HD_M_SW_RST);
143 HDMI_WRITE(HDMI_M_CTL, 0);
145 HDMI_WRITE(HDMI_M_CTL, VC4_HD_M_ENABLE);
147 HDMI_WRITE(HDMI_SW_RESET_CONTROL,
148 VC4_HDMI_SW_RESET_HDMI |
149 VC4_HDMI_SW_RESET_FORMAT_DETECT);
151 HDMI_WRITE(HDMI_SW_RESET_CONTROL, 0);
153 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
156 static void vc5_hdmi_reset(struct vc4_hdmi *vc4_hdmi)
160 reset_control_reset(vc4_hdmi->reset);
162 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
164 HDMI_WRITE(HDMI_DVP_CTL, 0);
166 HDMI_WRITE(HDMI_CLOCK_STOP,
167 HDMI_READ(HDMI_CLOCK_STOP) | VC4_DVP_HT_CLOCK_STOP_PIXEL);
169 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
172 #ifdef CONFIG_DRM_VC4_HDMI_CEC
173 static void vc4_hdmi_cec_update_clk_div(struct vc4_hdmi *vc4_hdmi)
175 unsigned long cec_rate = clk_get_rate(vc4_hdmi->cec_clock);
180 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
182 value = HDMI_READ(HDMI_CEC_CNTRL_1);
183 value &= ~VC4_HDMI_CEC_DIV_CLK_CNT_MASK;
186 * Set the clock divider: the hsm_clock rate and this divider
187 * setting will give a 40 kHz CEC clock.
189 clk_cnt = cec_rate / CEC_CLOCK_FREQ;
190 value |= clk_cnt << VC4_HDMI_CEC_DIV_CLK_CNT_SHIFT;
191 HDMI_WRITE(HDMI_CEC_CNTRL_1, value);
193 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
196 static void vc4_hdmi_cec_update_clk_div(struct vc4_hdmi *vc4_hdmi) {}
199 static void vc4_hdmi_enable_scrambling(struct drm_encoder *encoder);
201 static enum drm_connector_status
202 vc4_hdmi_connector_detect(struct drm_connector *connector, bool force)
204 struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector);
205 bool connected = false;
207 mutex_lock(&vc4_hdmi->mutex);
209 WARN_ON(pm_runtime_resume_and_get(&vc4_hdmi->pdev->dev));
211 if (vc4_hdmi->hpd_gpio) {
212 if (gpiod_get_value_cansleep(vc4_hdmi->hpd_gpio))
218 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
219 hotplug = HDMI_READ(HDMI_HOTPLUG);
220 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
222 if (hotplug & VC4_HDMI_HOTPLUG_CONNECTED)
227 if (connector->status != connector_status_connected) {
228 struct edid *edid = drm_get_edid(connector, vc4_hdmi->ddc);
231 cec_s_phys_addr_from_edid(vc4_hdmi->cec_adap, edid);
232 vc4_hdmi->encoder.hdmi_monitor = drm_detect_hdmi_monitor(edid);
237 vc4_hdmi_enable_scrambling(&vc4_hdmi->encoder.base.base);
238 pm_runtime_put(&vc4_hdmi->pdev->dev);
239 mutex_unlock(&vc4_hdmi->mutex);
240 return connector_status_connected;
243 cec_phys_addr_invalidate(vc4_hdmi->cec_adap);
244 pm_runtime_put(&vc4_hdmi->pdev->dev);
245 mutex_unlock(&vc4_hdmi->mutex);
246 return connector_status_disconnected;
249 static void vc4_hdmi_connector_destroy(struct drm_connector *connector)
251 drm_connector_unregister(connector);
252 drm_connector_cleanup(connector);
255 static int vc4_hdmi_connector_get_modes(struct drm_connector *connector)
257 struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector);
258 struct vc4_hdmi_encoder *vc4_encoder = &vc4_hdmi->encoder;
262 mutex_lock(&vc4_hdmi->mutex);
264 edid = drm_get_edid(connector, vc4_hdmi->ddc);
265 cec_s_phys_addr_from_edid(vc4_hdmi->cec_adap, edid);
271 vc4_encoder->hdmi_monitor = drm_detect_hdmi_monitor(edid);
273 drm_connector_update_edid_property(connector, edid);
274 ret = drm_add_edid_modes(connector, edid);
277 if (vc4_hdmi->disable_4kp60) {
278 struct drm_device *drm = connector->dev;
279 struct drm_display_mode *mode;
281 list_for_each_entry(mode, &connector->probed_modes, head) {
282 if (vc4_hdmi_mode_needs_scrambling(mode, 8)) {
283 drm_warn_once(drm, "The core clock cannot reach frequencies high enough to support 4k @ 60Hz.");
284 drm_warn_once(drm, "Please change your config.txt file to add hdmi_enable_4kp60.");
290 mutex_unlock(&vc4_hdmi->mutex);
295 static int vc4_hdmi_connector_atomic_check(struct drm_connector *connector,
296 struct drm_atomic_state *state)
298 struct drm_connector_state *old_state =
299 drm_atomic_get_old_connector_state(state, connector);
300 struct drm_connector_state *new_state =
301 drm_atomic_get_new_connector_state(state, connector);
302 struct drm_crtc *crtc = new_state->crtc;
307 if (old_state->colorspace != new_state->colorspace ||
308 !drm_connector_atomic_hdr_metadata_equal(old_state, new_state)) {
309 struct drm_crtc_state *crtc_state;
311 crtc_state = drm_atomic_get_crtc_state(state, crtc);
312 if (IS_ERR(crtc_state))
313 return PTR_ERR(crtc_state);
315 crtc_state->mode_changed = true;
321 static void vc4_hdmi_connector_reset(struct drm_connector *connector)
323 struct vc4_hdmi_connector_state *old_state =
324 conn_state_to_vc4_hdmi_conn_state(connector->state);
325 struct vc4_hdmi_connector_state *new_state =
326 kzalloc(sizeof(*new_state), GFP_KERNEL);
328 if (connector->state)
329 __drm_atomic_helper_connector_destroy_state(connector->state);
332 __drm_atomic_helper_connector_reset(connector, &new_state->base);
337 new_state->base.max_bpc = 8;
338 new_state->base.max_requested_bpc = 8;
339 drm_atomic_helper_connector_tv_reset(connector);
342 static struct drm_connector_state *
343 vc4_hdmi_connector_duplicate_state(struct drm_connector *connector)
345 struct drm_connector_state *conn_state = connector->state;
346 struct vc4_hdmi_connector_state *vc4_state = conn_state_to_vc4_hdmi_conn_state(conn_state);
347 struct vc4_hdmi_connector_state *new_state;
349 new_state = kzalloc(sizeof(*new_state), GFP_KERNEL);
353 new_state->tmds_char_rate = vc4_state->tmds_char_rate;
354 __drm_atomic_helper_connector_duplicate_state(connector, &new_state->base);
356 return &new_state->base;
359 static const struct drm_connector_funcs vc4_hdmi_connector_funcs = {
360 .detect = vc4_hdmi_connector_detect,
361 .fill_modes = drm_helper_probe_single_connector_modes,
362 .destroy = vc4_hdmi_connector_destroy,
363 .reset = vc4_hdmi_connector_reset,
364 .atomic_duplicate_state = vc4_hdmi_connector_duplicate_state,
365 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
368 static const struct drm_connector_helper_funcs vc4_hdmi_connector_helper_funcs = {
369 .get_modes = vc4_hdmi_connector_get_modes,
370 .atomic_check = vc4_hdmi_connector_atomic_check,
373 static int vc4_hdmi_connector_init(struct drm_device *dev,
374 struct vc4_hdmi *vc4_hdmi)
376 struct drm_connector *connector = &vc4_hdmi->connector;
377 struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
380 drm_connector_init_with_ddc(dev, connector,
381 &vc4_hdmi_connector_funcs,
382 DRM_MODE_CONNECTOR_HDMIA,
384 drm_connector_helper_add(connector, &vc4_hdmi_connector_helper_funcs);
387 * Some of the properties below require access to state, like bpc.
388 * Allocate some default initial connector state with our reset helper.
390 if (connector->funcs->reset)
391 connector->funcs->reset(connector);
393 /* Create and attach TV margin props to this connector. */
394 ret = drm_mode_create_tv_margin_properties(dev);
398 ret = drm_mode_create_hdmi_colorspace_property(connector);
402 drm_connector_attach_colorspace_property(connector);
403 drm_connector_attach_tv_margin_properties(connector);
404 drm_connector_attach_max_bpc_property(connector, 8, 12);
406 connector->polled = (DRM_CONNECTOR_POLL_CONNECT |
407 DRM_CONNECTOR_POLL_DISCONNECT);
409 connector->interlace_allowed = 1;
410 connector->doublescan_allowed = 0;
412 if (vc4_hdmi->variant->supports_hdr)
413 drm_connector_attach_hdr_output_metadata_property(connector);
415 drm_connector_attach_encoder(connector, encoder);
420 static int vc4_hdmi_stop_packet(struct drm_encoder *encoder,
421 enum hdmi_infoframe_type type,
424 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
425 u32 packet_id = type - 0x80;
428 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
429 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
430 HDMI_READ(HDMI_RAM_PACKET_CONFIG) & ~BIT(packet_id));
431 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
436 return wait_for(!(HDMI_READ(HDMI_RAM_PACKET_STATUS) &
437 BIT(packet_id)), 100);
440 static void vc4_hdmi_write_infoframe(struct drm_encoder *encoder,
441 union hdmi_infoframe *frame)
443 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
444 u32 packet_id = frame->any.type - 0x80;
445 const struct vc4_hdmi_register *ram_packet_start =
446 &vc4_hdmi->variant->registers[HDMI_RAM_PACKET_START];
447 u32 packet_reg = ram_packet_start->offset + VC4_HDMI_PACKET_STRIDE * packet_id;
448 void __iomem *base = __vc4_hdmi_get_field_base(vc4_hdmi,
449 ram_packet_start->reg);
450 uint8_t buffer[VC4_HDMI_PACKET_STRIDE];
455 WARN_ONCE(!(HDMI_READ(HDMI_RAM_PACKET_CONFIG) &
456 VC4_HDMI_RAM_PACKET_ENABLE),
457 "Packet RAM has to be on to store the packet.");
459 len = hdmi_infoframe_pack(frame, buffer, sizeof(buffer));
463 ret = vc4_hdmi_stop_packet(encoder, frame->any.type, true);
465 DRM_ERROR("Failed to wait for infoframe to go idle: %d\n", ret);
469 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
471 for (i = 0; i < len; i += 7) {
472 writel(buffer[i + 0] << 0 |
478 writel(buffer[i + 3] << 0 |
480 buffer[i + 5] << 16 |
486 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
487 HDMI_READ(HDMI_RAM_PACKET_CONFIG) | BIT(packet_id));
489 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
491 ret = wait_for((HDMI_READ(HDMI_RAM_PACKET_STATUS) &
492 BIT(packet_id)), 100);
494 DRM_ERROR("Failed to wait for infoframe to start: %d\n", ret);
497 static void vc4_hdmi_set_avi_infoframe(struct drm_encoder *encoder)
499 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
500 struct drm_connector *connector = &vc4_hdmi->connector;
501 struct drm_connector_state *cstate = connector->state;
502 const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
503 union hdmi_infoframe frame;
506 lockdep_assert_held(&vc4_hdmi->mutex);
508 ret = drm_hdmi_avi_infoframe_from_display_mode(&frame.avi,
511 DRM_ERROR("couldn't fill AVI infoframe\n");
515 drm_hdmi_avi_infoframe_quant_range(&frame.avi,
517 vc4_hdmi_is_full_range_rgb(vc4_hdmi, mode) ?
518 HDMI_QUANTIZATION_RANGE_FULL :
519 HDMI_QUANTIZATION_RANGE_LIMITED);
520 drm_hdmi_avi_infoframe_colorimetry(&frame.avi, cstate);
521 drm_hdmi_avi_infoframe_bars(&frame.avi, cstate);
523 vc4_hdmi_write_infoframe(encoder, &frame);
526 static void vc4_hdmi_set_spd_infoframe(struct drm_encoder *encoder)
528 union hdmi_infoframe frame;
531 ret = hdmi_spd_infoframe_init(&frame.spd, "Broadcom", "Videocore");
533 DRM_ERROR("couldn't fill SPD infoframe\n");
537 frame.spd.sdi = HDMI_SPD_SDI_PC;
539 vc4_hdmi_write_infoframe(encoder, &frame);
542 static void vc4_hdmi_set_audio_infoframe(struct drm_encoder *encoder)
544 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
545 struct hdmi_audio_infoframe *audio = &vc4_hdmi->audio.infoframe;
546 union hdmi_infoframe frame;
548 memcpy(&frame.audio, audio, sizeof(*audio));
549 vc4_hdmi_write_infoframe(encoder, &frame);
552 static void vc4_hdmi_set_hdr_infoframe(struct drm_encoder *encoder)
554 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
555 struct drm_connector *connector = &vc4_hdmi->connector;
556 struct drm_connector_state *conn_state = connector->state;
557 union hdmi_infoframe frame;
559 lockdep_assert_held(&vc4_hdmi->mutex);
561 if (!vc4_hdmi->variant->supports_hdr)
564 if (!conn_state->hdr_output_metadata)
567 if (drm_hdmi_infoframe_set_hdr_metadata(&frame.drm, conn_state))
570 vc4_hdmi_write_infoframe(encoder, &frame);
573 static void vc4_hdmi_set_infoframes(struct drm_encoder *encoder)
575 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
577 lockdep_assert_held(&vc4_hdmi->mutex);
579 vc4_hdmi_set_avi_infoframe(encoder);
580 vc4_hdmi_set_spd_infoframe(encoder);
582 * If audio was streaming, then we need to reenabled the audio
583 * infoframe here during encoder_enable.
585 if (vc4_hdmi->audio.streaming)
586 vc4_hdmi_set_audio_infoframe(encoder);
588 vc4_hdmi_set_hdr_infoframe(encoder);
591 static bool vc4_hdmi_supports_scrambling(struct drm_encoder *encoder,
592 struct drm_display_mode *mode)
594 struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
595 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
596 struct drm_display_info *display = &vc4_hdmi->connector.display_info;
598 lockdep_assert_held(&vc4_hdmi->mutex);
600 if (!vc4_encoder->hdmi_monitor)
603 if (!display->hdmi.scdc.supported ||
604 !display->hdmi.scdc.scrambling.supported)
610 #define SCRAMBLING_POLLING_DELAY_MS 1000
612 static void vc4_hdmi_enable_scrambling(struct drm_encoder *encoder)
614 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
615 struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
618 lockdep_assert_held(&vc4_hdmi->mutex);
620 if (!vc4_hdmi_supports_scrambling(encoder, mode))
623 if (!vc4_hdmi_mode_needs_scrambling(mode, vc4_hdmi->output_bpc))
626 drm_scdc_set_high_tmds_clock_ratio(vc4_hdmi->ddc, true);
627 drm_scdc_set_scrambling(vc4_hdmi->ddc, true);
629 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
630 HDMI_WRITE(HDMI_SCRAMBLER_CTL, HDMI_READ(HDMI_SCRAMBLER_CTL) |
631 VC5_HDMI_SCRAMBLER_CTL_ENABLE);
632 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
634 vc4_hdmi->scdc_enabled = true;
636 queue_delayed_work(system_wq, &vc4_hdmi->scrambling_work,
637 msecs_to_jiffies(SCRAMBLING_POLLING_DELAY_MS));
640 static void vc4_hdmi_disable_scrambling(struct drm_encoder *encoder)
642 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
645 lockdep_assert_held(&vc4_hdmi->mutex);
647 if (!vc4_hdmi->scdc_enabled)
650 vc4_hdmi->scdc_enabled = false;
652 if (delayed_work_pending(&vc4_hdmi->scrambling_work))
653 cancel_delayed_work_sync(&vc4_hdmi->scrambling_work);
655 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
656 HDMI_WRITE(HDMI_SCRAMBLER_CTL, HDMI_READ(HDMI_SCRAMBLER_CTL) &
657 ~VC5_HDMI_SCRAMBLER_CTL_ENABLE);
658 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
660 drm_scdc_set_scrambling(vc4_hdmi->ddc, false);
661 drm_scdc_set_high_tmds_clock_ratio(vc4_hdmi->ddc, false);
664 static void vc4_hdmi_scrambling_wq(struct work_struct *work)
666 struct vc4_hdmi *vc4_hdmi = container_of(to_delayed_work(work),
670 if (drm_scdc_get_scrambling_status(vc4_hdmi->ddc))
673 drm_scdc_set_high_tmds_clock_ratio(vc4_hdmi->ddc, true);
674 drm_scdc_set_scrambling(vc4_hdmi->ddc, true);
676 queue_delayed_work(system_wq, &vc4_hdmi->scrambling_work,
677 msecs_to_jiffies(SCRAMBLING_POLLING_DELAY_MS));
680 static void vc4_hdmi_encoder_post_crtc_disable(struct drm_encoder *encoder,
681 struct drm_atomic_state *state)
683 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
686 mutex_lock(&vc4_hdmi->mutex);
688 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
690 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG, 0);
692 HDMI_WRITE(HDMI_VID_CTL, HDMI_READ(HDMI_VID_CTL) | VC4_HD_VID_CTL_CLRRGB);
694 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
698 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
699 HDMI_WRITE(HDMI_VID_CTL,
700 HDMI_READ(HDMI_VID_CTL) & ~VC4_HD_VID_CTL_ENABLE);
701 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
703 vc4_hdmi_disable_scrambling(encoder);
705 mutex_unlock(&vc4_hdmi->mutex);
708 static void vc4_hdmi_encoder_post_crtc_powerdown(struct drm_encoder *encoder,
709 struct drm_atomic_state *state)
711 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
715 mutex_lock(&vc4_hdmi->mutex);
717 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
718 HDMI_WRITE(HDMI_VID_CTL,
719 HDMI_READ(HDMI_VID_CTL) | VC4_HD_VID_CTL_BLANKPIX);
720 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
722 if (vc4_hdmi->variant->phy_disable)
723 vc4_hdmi->variant->phy_disable(vc4_hdmi);
725 clk_disable_unprepare(vc4_hdmi->pixel_bvb_clock);
726 clk_disable_unprepare(vc4_hdmi->pixel_clock);
728 ret = pm_runtime_put(&vc4_hdmi->pdev->dev);
730 DRM_ERROR("Failed to release power domain: %d\n", ret);
732 mutex_unlock(&vc4_hdmi->mutex);
735 static void vc4_hdmi_encoder_disable(struct drm_encoder *encoder)
737 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
739 mutex_lock(&vc4_hdmi->mutex);
740 vc4_hdmi->output_enabled = false;
741 mutex_unlock(&vc4_hdmi->mutex);
744 static void vc4_hdmi_csc_setup(struct vc4_hdmi *vc4_hdmi,
745 struct drm_connector_state *state,
746 const struct drm_display_mode *mode)
751 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
753 csc_ctl = VC4_SET_FIELD(VC4_HD_CSC_CTL_ORDER_BGR,
754 VC4_HD_CSC_CTL_ORDER);
756 if (!vc4_hdmi_is_full_range_rgb(vc4_hdmi, mode)) {
757 /* CEA VICs other than #1 requre limited range RGB
758 * output unless overridden by an AVI infoframe.
759 * Apply a colorspace conversion to squash 0-255 down
760 * to 16-235. The matrix here is:
767 csc_ctl |= VC4_HD_CSC_CTL_ENABLE;
768 csc_ctl |= VC4_HD_CSC_CTL_RGB2YCC;
769 csc_ctl |= VC4_SET_FIELD(VC4_HD_CSC_CTL_MODE_CUSTOM,
770 VC4_HD_CSC_CTL_MODE);
772 HDMI_WRITE(HDMI_CSC_12_11, (0x000 << 16) | 0x000);
773 HDMI_WRITE(HDMI_CSC_14_13, (0x100 << 16) | 0x6e0);
774 HDMI_WRITE(HDMI_CSC_22_21, (0x6e0 << 16) | 0x000);
775 HDMI_WRITE(HDMI_CSC_24_23, (0x100 << 16) | 0x000);
776 HDMI_WRITE(HDMI_CSC_32_31, (0x000 << 16) | 0x6e0);
777 HDMI_WRITE(HDMI_CSC_34_33, (0x100 << 16) | 0x000);
780 /* The RGB order applies even when CSC is disabled. */
781 HDMI_WRITE(HDMI_CSC_CTL, csc_ctl);
783 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
787 * If we need to output Full Range RGB, then use the unity matrix
793 * Matrix is signed 2p13 fixed point, with signed 9p6 offsets
795 static const u16 vc5_hdmi_csc_full_rgb_unity[3][4] = {
796 { 0x2000, 0x0000, 0x0000, 0x0000 },
797 { 0x0000, 0x2000, 0x0000, 0x0000 },
798 { 0x0000, 0x0000, 0x2000, 0x0000 },
802 * CEA VICs other than #1 require limited range RGB output unless
803 * overridden by an AVI infoframe. Apply a colorspace conversion to
804 * squash 0-255 down to 16-235. The matrix here is:
810 * Matrix is signed 2p13 fixed point, with signed 9p6 offsets
812 static const u16 vc5_hdmi_csc_full_rgb_to_limited_rgb[3][4] = {
813 { 0x1b80, 0x0000, 0x0000, 0x0400 },
814 { 0x0000, 0x1b80, 0x0000, 0x0400 },
815 { 0x0000, 0x0000, 0x1b80, 0x0400 },
818 static void vc5_hdmi_set_csc_coeffs(struct vc4_hdmi *vc4_hdmi,
819 const u16 coeffs[3][4])
821 lockdep_assert_held(&vc4_hdmi->hw_lock);
823 HDMI_WRITE(HDMI_CSC_12_11, (coeffs[0][1] << 16) | coeffs[0][0]);
824 HDMI_WRITE(HDMI_CSC_14_13, (coeffs[0][3] << 16) | coeffs[0][2]);
825 HDMI_WRITE(HDMI_CSC_22_21, (coeffs[1][1] << 16) | coeffs[1][0]);
826 HDMI_WRITE(HDMI_CSC_24_23, (coeffs[1][3] << 16) | coeffs[1][2]);
827 HDMI_WRITE(HDMI_CSC_32_31, (coeffs[2][1] << 16) | coeffs[2][0]);
828 HDMI_WRITE(HDMI_CSC_34_33, (coeffs[2][3] << 16) | coeffs[2][2]);
831 static void vc5_hdmi_csc_setup(struct vc4_hdmi *vc4_hdmi,
832 struct drm_connector_state *state,
833 const struct drm_display_mode *mode)
836 u32 csc_ctl = VC5_MT_CP_CSC_CTL_ENABLE | VC4_SET_FIELD(VC4_HD_CSC_CTL_MODE_CUSTOM,
837 VC5_MT_CP_CSC_CTL_MODE);
839 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
841 HDMI_WRITE(HDMI_VEC_INTERFACE_XBAR, 0x354021);
843 if (!vc4_hdmi_is_full_range_rgb(vc4_hdmi, mode))
844 vc5_hdmi_set_csc_coeffs(vc4_hdmi, vc5_hdmi_csc_full_rgb_to_limited_rgb);
846 vc5_hdmi_set_csc_coeffs(vc4_hdmi, vc5_hdmi_csc_full_rgb_unity);
848 HDMI_WRITE(HDMI_CSC_CTL, csc_ctl);
850 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
853 static void vc4_hdmi_set_timings(struct vc4_hdmi *vc4_hdmi,
854 struct drm_connector_state *state,
855 struct drm_display_mode *mode)
857 bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
858 bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
859 bool interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE;
860 u32 pixel_rep = (mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1;
861 u32 verta = (VC4_SET_FIELD(mode->crtc_vsync_end - mode->crtc_vsync_start,
862 VC4_HDMI_VERTA_VSP) |
863 VC4_SET_FIELD(mode->crtc_vsync_start - mode->crtc_vdisplay,
864 VC4_HDMI_VERTA_VFP) |
865 VC4_SET_FIELD(mode->crtc_vdisplay, VC4_HDMI_VERTA_VAL));
866 u32 vertb = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) |
867 VC4_SET_FIELD(mode->crtc_vtotal - mode->crtc_vsync_end,
868 VC4_HDMI_VERTB_VBP));
869 u32 vertb_even = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) |
870 VC4_SET_FIELD(mode->crtc_vtotal -
871 mode->crtc_vsync_end -
873 VC4_HDMI_VERTB_VBP));
876 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
878 HDMI_WRITE(HDMI_HORZA,
879 (vsync_pos ? VC4_HDMI_HORZA_VPOS : 0) |
880 (hsync_pos ? VC4_HDMI_HORZA_HPOS : 0) |
881 VC4_SET_FIELD(mode->hdisplay * pixel_rep,
882 VC4_HDMI_HORZA_HAP));
884 HDMI_WRITE(HDMI_HORZB,
885 VC4_SET_FIELD((mode->htotal -
886 mode->hsync_end) * pixel_rep,
887 VC4_HDMI_HORZB_HBP) |
888 VC4_SET_FIELD((mode->hsync_end -
889 mode->hsync_start) * pixel_rep,
890 VC4_HDMI_HORZB_HSP) |
891 VC4_SET_FIELD((mode->hsync_start -
892 mode->hdisplay) * pixel_rep,
893 VC4_HDMI_HORZB_HFP));
895 HDMI_WRITE(HDMI_VERTA0, verta);
896 HDMI_WRITE(HDMI_VERTA1, verta);
898 HDMI_WRITE(HDMI_VERTB0, vertb_even);
899 HDMI_WRITE(HDMI_VERTB1, vertb);
901 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
904 static void vc5_hdmi_set_timings(struct vc4_hdmi *vc4_hdmi,
905 struct drm_connector_state *state,
906 struct drm_display_mode *mode)
908 bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
909 bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
910 bool interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE;
911 u32 pixel_rep = (mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1;
912 u32 verta = (VC4_SET_FIELD(mode->crtc_vsync_end - mode->crtc_vsync_start,
913 VC5_HDMI_VERTA_VSP) |
914 VC4_SET_FIELD(mode->crtc_vsync_start - mode->crtc_vdisplay,
915 VC5_HDMI_VERTA_VFP) |
916 VC4_SET_FIELD(mode->crtc_vdisplay, VC5_HDMI_VERTA_VAL));
917 u32 vertb = (VC4_SET_FIELD(0, VC5_HDMI_VERTB_VSPO) |
918 VC4_SET_FIELD(mode->crtc_vtotal - mode->crtc_vsync_end,
919 VC4_HDMI_VERTB_VBP));
920 u32 vertb_even = (VC4_SET_FIELD(0, VC5_HDMI_VERTB_VSPO) |
921 VC4_SET_FIELD(mode->crtc_vtotal -
922 mode->crtc_vsync_end -
924 VC4_HDMI_VERTB_VBP));
930 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
932 HDMI_WRITE(HDMI_HORZA,
933 (vsync_pos ? VC5_HDMI_HORZA_VPOS : 0) |
934 (hsync_pos ? VC5_HDMI_HORZA_HPOS : 0) |
935 VC4_SET_FIELD(mode->hdisplay * pixel_rep,
936 VC5_HDMI_HORZA_HAP) |
937 VC4_SET_FIELD((mode->hsync_start -
938 mode->hdisplay) * pixel_rep,
939 VC5_HDMI_HORZA_HFP));
941 HDMI_WRITE(HDMI_HORZB,
942 VC4_SET_FIELD((mode->htotal -
943 mode->hsync_end) * pixel_rep,
944 VC5_HDMI_HORZB_HBP) |
945 VC4_SET_FIELD((mode->hsync_end -
946 mode->hsync_start) * pixel_rep,
947 VC5_HDMI_HORZB_HSP));
949 HDMI_WRITE(HDMI_VERTA0, verta);
950 HDMI_WRITE(HDMI_VERTA1, verta);
952 HDMI_WRITE(HDMI_VERTB0, vertb_even);
953 HDMI_WRITE(HDMI_VERTB1, vertb);
955 switch (state->max_bpc) {
971 reg = HDMI_READ(HDMI_DEEP_COLOR_CONFIG_1);
972 reg &= ~(VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE_MASK |
973 VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH_MASK);
974 reg |= VC4_SET_FIELD(2, VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE) |
975 VC4_SET_FIELD(gcp, VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH);
976 HDMI_WRITE(HDMI_DEEP_COLOR_CONFIG_1, reg);
978 reg = HDMI_READ(HDMI_GCP_WORD_1);
979 reg &= ~VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1_MASK;
980 reg |= VC4_SET_FIELD(gcp, VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1);
981 HDMI_WRITE(HDMI_GCP_WORD_1, reg);
983 reg = HDMI_READ(HDMI_GCP_CONFIG);
984 reg &= ~VC5_HDMI_GCP_CONFIG_GCP_ENABLE;
985 reg |= gcp_en ? VC5_HDMI_GCP_CONFIG_GCP_ENABLE : 0;
986 HDMI_WRITE(HDMI_GCP_CONFIG, reg);
988 HDMI_WRITE(HDMI_CLOCK_STOP, 0);
990 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
993 static void vc4_hdmi_recenter_fifo(struct vc4_hdmi *vc4_hdmi)
999 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1001 drift = HDMI_READ(HDMI_FIFO_CTL);
1002 drift &= VC4_HDMI_FIFO_VALID_WRITE_MASK;
1004 HDMI_WRITE(HDMI_FIFO_CTL,
1005 drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
1006 HDMI_WRITE(HDMI_FIFO_CTL,
1007 drift | VC4_HDMI_FIFO_CTL_RECENTER);
1009 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1011 usleep_range(1000, 1100);
1013 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1015 HDMI_WRITE(HDMI_FIFO_CTL,
1016 drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
1017 HDMI_WRITE(HDMI_FIFO_CTL,
1018 drift | VC4_HDMI_FIFO_CTL_RECENTER);
1020 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1022 ret = wait_for(HDMI_READ(HDMI_FIFO_CTL) &
1023 VC4_HDMI_FIFO_CTL_RECENTER_DONE, 1);
1024 WARN_ONCE(ret, "Timeout waiting for "
1025 "VC4_HDMI_FIFO_CTL_RECENTER_DONE");
1028 static void vc4_hdmi_encoder_pre_crtc_configure(struct drm_encoder *encoder,
1029 struct drm_atomic_state *state)
1031 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1032 struct drm_connector *connector = &vc4_hdmi->connector;
1033 struct drm_connector_state *conn_state =
1034 drm_atomic_get_new_connector_state(state, connector);
1035 struct vc4_hdmi_connector_state *vc4_conn_state =
1036 conn_state_to_vc4_hdmi_conn_state(conn_state);
1037 struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
1038 unsigned long tmds_char_rate = vc4_conn_state->tmds_char_rate;
1039 unsigned long bvb_rate, hsm_rate;
1040 unsigned long flags;
1043 mutex_lock(&vc4_hdmi->mutex);
1046 * As stated in RPi's vc4 firmware "HDMI state machine (HSM) clock must
1047 * be faster than pixel clock, infinitesimally faster, tested in
1048 * simulation. Otherwise, exact value is unimportant for HDMI
1049 * operation." This conflicts with bcm2835's vc4 documentation, which
1050 * states HSM's clock has to be at least 108% of the pixel clock.
1052 * Real life tests reveal that vc4's firmware statement holds up, and
1053 * users are able to use pixel clocks closer to HSM's, namely for
1054 * 1920x1200@60Hz. So it was decided to have leave a 1% margin between
1055 * both clocks. Which, for RPi0-3 implies a maximum pixel clock of
1058 * Additionally, the AXI clock needs to be at least 25% of
1059 * pixel clock, but HSM ends up being the limiting factor.
1061 hsm_rate = max_t(unsigned long, 120000000, (tmds_char_rate / 100) * 101);
1062 ret = clk_set_min_rate(vc4_hdmi->hsm_clock, hsm_rate);
1064 DRM_ERROR("Failed to set HSM clock rate: %d\n", ret);
1068 ret = pm_runtime_resume_and_get(&vc4_hdmi->pdev->dev);
1070 DRM_ERROR("Failed to retain power domain: %d\n", ret);
1074 ret = clk_set_rate(vc4_hdmi->pixel_clock, tmds_char_rate);
1076 DRM_ERROR("Failed to set pixel clock rate: %d\n", ret);
1077 goto err_put_runtime_pm;
1080 ret = clk_prepare_enable(vc4_hdmi->pixel_clock);
1082 DRM_ERROR("Failed to turn on pixel clock: %d\n", ret);
1083 goto err_put_runtime_pm;
1087 vc4_hdmi_cec_update_clk_div(vc4_hdmi);
1089 if (tmds_char_rate > 297000000)
1090 bvb_rate = 300000000;
1091 else if (tmds_char_rate > 148500000)
1092 bvb_rate = 150000000;
1094 bvb_rate = 75000000;
1096 ret = clk_set_min_rate(vc4_hdmi->pixel_bvb_clock, bvb_rate);
1098 DRM_ERROR("Failed to set pixel bvb clock rate: %d\n", ret);
1099 goto err_disable_pixel_clock;
1102 ret = clk_prepare_enable(vc4_hdmi->pixel_bvb_clock);
1104 DRM_ERROR("Failed to turn on pixel bvb clock: %d\n", ret);
1105 goto err_disable_pixel_clock;
1108 if (vc4_hdmi->variant->phy_init)
1109 vc4_hdmi->variant->phy_init(vc4_hdmi, vc4_conn_state);
1111 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1113 HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
1114 HDMI_READ(HDMI_SCHEDULER_CONTROL) |
1115 VC4_HDMI_SCHEDULER_CONTROL_MANUAL_FORMAT |
1116 VC4_HDMI_SCHEDULER_CONTROL_IGNORE_VSYNC_PREDICTS);
1118 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1120 if (vc4_hdmi->variant->set_timings)
1121 vc4_hdmi->variant->set_timings(vc4_hdmi, conn_state, mode);
1123 mutex_unlock(&vc4_hdmi->mutex);
1127 err_disable_pixel_clock:
1128 clk_disable_unprepare(vc4_hdmi->pixel_clock);
1130 pm_runtime_put(&vc4_hdmi->pdev->dev);
1132 mutex_unlock(&vc4_hdmi->mutex);
1136 static void vc4_hdmi_encoder_pre_crtc_enable(struct drm_encoder *encoder,
1137 struct drm_atomic_state *state)
1139 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1140 struct drm_connector *connector = &vc4_hdmi->connector;
1141 struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
1142 struct drm_connector_state *conn_state =
1143 drm_atomic_get_new_connector_state(state, connector);
1144 unsigned long flags;
1146 mutex_lock(&vc4_hdmi->mutex);
1148 if (vc4_hdmi->variant->csc_setup)
1149 vc4_hdmi->variant->csc_setup(vc4_hdmi, conn_state, mode);
1151 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1152 HDMI_WRITE(HDMI_FIFO_CTL, VC4_HDMI_FIFO_CTL_MASTER_SLAVE_N);
1153 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1155 mutex_unlock(&vc4_hdmi->mutex);
1158 static void vc4_hdmi_encoder_post_crtc_enable(struct drm_encoder *encoder,
1159 struct drm_atomic_state *state)
1161 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1162 struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
1163 struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
1164 bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
1165 bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
1166 unsigned long flags;
1169 mutex_lock(&vc4_hdmi->mutex);
1171 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1173 HDMI_WRITE(HDMI_VID_CTL,
1174 VC4_HD_VID_CTL_ENABLE |
1175 VC4_HD_VID_CTL_CLRRGB |
1176 VC4_HD_VID_CTL_UNDERFLOW_ENABLE |
1177 VC4_HD_VID_CTL_FRAME_COUNTER_RESET |
1178 (vsync_pos ? 0 : VC4_HD_VID_CTL_VSYNC_LOW) |
1179 (hsync_pos ? 0 : VC4_HD_VID_CTL_HSYNC_LOW));
1181 HDMI_WRITE(HDMI_VID_CTL,
1182 HDMI_READ(HDMI_VID_CTL) & ~VC4_HD_VID_CTL_BLANKPIX);
1184 if (vc4_encoder->hdmi_monitor) {
1185 HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
1186 HDMI_READ(HDMI_SCHEDULER_CONTROL) |
1187 VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);
1189 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1191 ret = wait_for(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
1192 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE, 1000);
1193 WARN_ONCE(ret, "Timeout waiting for "
1194 "VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
1196 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
1197 HDMI_READ(HDMI_RAM_PACKET_CONFIG) &
1198 ~(VC4_HDMI_RAM_PACKET_ENABLE));
1199 HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
1200 HDMI_READ(HDMI_SCHEDULER_CONTROL) &
1201 ~VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);
1203 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1205 ret = wait_for(!(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
1206 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE), 1000);
1207 WARN_ONCE(ret, "Timeout waiting for "
1208 "!VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
1211 if (vc4_encoder->hdmi_monitor) {
1212 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1214 WARN_ON(!(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
1215 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE));
1216 HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
1217 HDMI_READ(HDMI_SCHEDULER_CONTROL) |
1218 VC4_HDMI_SCHEDULER_CONTROL_VERT_ALWAYS_KEEPOUT);
1220 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
1221 VC4_HDMI_RAM_PACKET_ENABLE);
1223 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1225 vc4_hdmi_set_infoframes(encoder);
1228 vc4_hdmi_recenter_fifo(vc4_hdmi);
1229 vc4_hdmi_enable_scrambling(encoder);
1231 mutex_unlock(&vc4_hdmi->mutex);
1234 static void vc4_hdmi_encoder_enable(struct drm_encoder *encoder)
1236 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1238 mutex_lock(&vc4_hdmi->mutex);
1239 vc4_hdmi->output_enabled = true;
1240 mutex_unlock(&vc4_hdmi->mutex);
1243 static void vc4_hdmi_encoder_atomic_mode_set(struct drm_encoder *encoder,
1244 struct drm_crtc_state *crtc_state,
1245 struct drm_connector_state *conn_state)
1247 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1249 mutex_lock(&vc4_hdmi->mutex);
1250 drm_mode_copy(&vc4_hdmi->saved_adjusted_mode,
1251 &crtc_state->adjusted_mode);
1252 vc4_hdmi->output_bpc = conn_state->max_bpc;
1253 mutex_unlock(&vc4_hdmi->mutex);
1256 static enum drm_mode_status
1257 vc4_hdmi_encoder_clock_valid(const struct vc4_hdmi *vc4_hdmi,
1258 unsigned long long clock)
1260 const struct drm_connector *connector = &vc4_hdmi->connector;
1261 const struct drm_display_info *info = &connector->display_info;
1263 if (clock > vc4_hdmi->variant->max_pixel_clock)
1264 return MODE_CLOCK_HIGH;
1266 if (vc4_hdmi->disable_4kp60 && clock > HDMI_14_MAX_TMDS_CLK)
1267 return MODE_CLOCK_HIGH;
1269 if (info->max_tmds_clock && clock > (info->max_tmds_clock * 1000))
1270 return MODE_CLOCK_HIGH;
1275 static unsigned long long
1276 vc4_hdmi_encoder_compute_mode_clock(const struct drm_display_mode *mode,
1279 unsigned long long clock = mode->clock * 1000;
1281 if (mode->flags & DRM_MODE_FLAG_DBLCLK)
1284 clock = clock * bpc;
1291 vc4_hdmi_encoder_compute_clock(const struct vc4_hdmi *vc4_hdmi,
1292 struct vc4_hdmi_connector_state *vc4_state,
1293 const struct drm_display_mode *mode,
1296 unsigned long long clock;
1298 clock = vc4_hdmi_encoder_compute_mode_clock(mode, bpc);
1299 if (vc4_hdmi_encoder_clock_valid(vc4_hdmi, clock) != MODE_OK)
1302 vc4_state->tmds_char_rate = clock;
1307 #define WIFI_2_4GHz_CH1_MIN_FREQ 2400000000ULL
1308 #define WIFI_2_4GHz_CH1_MAX_FREQ 2422000000ULL
1310 static int vc4_hdmi_encoder_atomic_check(struct drm_encoder *encoder,
1311 struct drm_crtc_state *crtc_state,
1312 struct drm_connector_state *conn_state)
1314 struct vc4_hdmi_connector_state *vc4_state = conn_state_to_vc4_hdmi_conn_state(conn_state);
1315 struct drm_display_mode *mode = &crtc_state->adjusted_mode;
1316 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1317 unsigned long long tmds_char_rate = mode->clock * 1000;
1318 unsigned long long tmds_bit_rate;
1321 if (vc4_hdmi->variant->unsupported_odd_h_timings &&
1322 ((mode->hdisplay % 2) || (mode->hsync_start % 2) ||
1323 (mode->hsync_end % 2) || (mode->htotal % 2)))
1327 * The 1440p@60 pixel rate is in the same range than the first
1328 * WiFi channel (between 2.4GHz and 2.422GHz with 22MHz
1329 * bandwidth). Slightly lower the frequency to bring it out of
1332 tmds_bit_rate = tmds_char_rate * 10;
1333 if (vc4_hdmi->disable_wifi_frequencies &&
1334 (tmds_bit_rate >= WIFI_2_4GHz_CH1_MIN_FREQ &&
1335 tmds_bit_rate <= WIFI_2_4GHz_CH1_MAX_FREQ)) {
1336 mode->clock = 238560;
1337 tmds_char_rate = mode->clock * 1000;
1340 ret = vc4_hdmi_encoder_compute_clock(vc4_hdmi, vc4_state, mode,
1341 conn_state->max_bpc);
1348 static enum drm_mode_status
1349 vc4_hdmi_encoder_mode_valid(struct drm_encoder *encoder,
1350 const struct drm_display_mode *mode)
1352 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1354 if (vc4_hdmi->variant->unsupported_odd_h_timings &&
1355 ((mode->hdisplay % 2) || (mode->hsync_start % 2) ||
1356 (mode->hsync_end % 2) || (mode->htotal % 2)))
1357 return MODE_H_ILLEGAL;
1359 return vc4_hdmi_encoder_clock_valid(vc4_hdmi, mode->clock * 1000);
1362 static const struct drm_encoder_helper_funcs vc4_hdmi_encoder_helper_funcs = {
1363 .atomic_check = vc4_hdmi_encoder_atomic_check,
1364 .atomic_mode_set = vc4_hdmi_encoder_atomic_mode_set,
1365 .mode_valid = vc4_hdmi_encoder_mode_valid,
1366 .disable = vc4_hdmi_encoder_disable,
1367 .enable = vc4_hdmi_encoder_enable,
1370 static u32 vc4_hdmi_channel_map(struct vc4_hdmi *vc4_hdmi, u32 channel_mask)
1373 u32 channel_map = 0;
1375 for (i = 0; i < 8; i++) {
1376 if (channel_mask & BIT(i))
1377 channel_map |= i << (3 * i);
1382 static u32 vc5_hdmi_channel_map(struct vc4_hdmi *vc4_hdmi, u32 channel_mask)
1385 u32 channel_map = 0;
1387 for (i = 0; i < 8; i++) {
1388 if (channel_mask & BIT(i))
1389 channel_map |= i << (4 * i);
1394 /* HDMI audio codec callbacks */
1395 static void vc4_hdmi_audio_set_mai_clock(struct vc4_hdmi *vc4_hdmi,
1396 unsigned int samplerate)
1398 u32 hsm_clock = clk_get_rate(vc4_hdmi->audio_clock);
1399 unsigned long flags;
1402 rational_best_approximation(hsm_clock, samplerate,
1403 VC4_HD_MAI_SMP_N_MASK >>
1404 VC4_HD_MAI_SMP_N_SHIFT,
1405 (VC4_HD_MAI_SMP_M_MASK >>
1406 VC4_HD_MAI_SMP_M_SHIFT) + 1,
1409 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1410 HDMI_WRITE(HDMI_MAI_SMP,
1411 VC4_SET_FIELD(n, VC4_HD_MAI_SMP_N) |
1412 VC4_SET_FIELD(m - 1, VC4_HD_MAI_SMP_M));
1413 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1416 static void vc4_hdmi_set_n_cts(struct vc4_hdmi *vc4_hdmi, unsigned int samplerate)
1418 const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
1422 lockdep_assert_held(&vc4_hdmi->mutex);
1423 lockdep_assert_held(&vc4_hdmi->hw_lock);
1425 n = 128 * samplerate / 1000;
1426 tmp = (u64)(mode->clock * 1000) * n;
1427 do_div(tmp, 128 * samplerate);
1430 HDMI_WRITE(HDMI_CRP_CFG,
1431 VC4_HDMI_CRP_CFG_EXTERNAL_CTS_EN |
1432 VC4_SET_FIELD(n, VC4_HDMI_CRP_CFG_N));
1435 * We could get slightly more accurate clocks in some cases by
1436 * providing a CTS_1 value. The two CTS values are alternated
1437 * between based on the period fields
1439 HDMI_WRITE(HDMI_CTS_0, cts);
1440 HDMI_WRITE(HDMI_CTS_1, cts);
1443 static inline struct vc4_hdmi *dai_to_hdmi(struct snd_soc_dai *dai)
1445 struct snd_soc_card *card = snd_soc_dai_get_drvdata(dai);
1447 return snd_soc_card_get_drvdata(card);
1450 static bool vc4_hdmi_audio_can_stream(struct vc4_hdmi *vc4_hdmi)
1452 lockdep_assert_held(&vc4_hdmi->mutex);
1455 * If the controller is disabled, prevent any ALSA output.
1457 if (!vc4_hdmi->output_enabled)
1461 * If the encoder is currently in DVI mode, treat the codec DAI
1464 if (!(HDMI_READ(HDMI_RAM_PACKET_CONFIG) & VC4_HDMI_RAM_PACKET_ENABLE))
1470 static int vc4_hdmi_audio_startup(struct device *dev, void *data)
1472 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
1473 unsigned long flags;
1475 mutex_lock(&vc4_hdmi->mutex);
1477 if (!vc4_hdmi_audio_can_stream(vc4_hdmi)) {
1478 mutex_unlock(&vc4_hdmi->mutex);
1482 vc4_hdmi->audio.streaming = true;
1484 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1485 HDMI_WRITE(HDMI_MAI_CTL,
1486 VC4_HD_MAI_CTL_RESET |
1487 VC4_HD_MAI_CTL_FLUSH |
1488 VC4_HD_MAI_CTL_DLATE |
1489 VC4_HD_MAI_CTL_ERRORE |
1490 VC4_HD_MAI_CTL_ERRORF);
1491 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1493 if (vc4_hdmi->variant->phy_rng_enable)
1494 vc4_hdmi->variant->phy_rng_enable(vc4_hdmi);
1496 mutex_unlock(&vc4_hdmi->mutex);
1501 static void vc4_hdmi_audio_reset(struct vc4_hdmi *vc4_hdmi)
1503 struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
1504 struct device *dev = &vc4_hdmi->pdev->dev;
1505 unsigned long flags;
1508 lockdep_assert_held(&vc4_hdmi->mutex);
1510 vc4_hdmi->audio.streaming = false;
1511 ret = vc4_hdmi_stop_packet(encoder, HDMI_INFOFRAME_TYPE_AUDIO, false);
1513 dev_err(dev, "Failed to stop audio infoframe: %d\n", ret);
1515 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1517 HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_RESET);
1518 HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_ERRORF);
1519 HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_FLUSH);
1521 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1524 static void vc4_hdmi_audio_shutdown(struct device *dev, void *data)
1526 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
1527 unsigned long flags;
1529 mutex_lock(&vc4_hdmi->mutex);
1531 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1533 HDMI_WRITE(HDMI_MAI_CTL,
1534 VC4_HD_MAI_CTL_DLATE |
1535 VC4_HD_MAI_CTL_ERRORE |
1536 VC4_HD_MAI_CTL_ERRORF);
1538 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1540 if (vc4_hdmi->variant->phy_rng_disable)
1541 vc4_hdmi->variant->phy_rng_disable(vc4_hdmi);
1543 vc4_hdmi->audio.streaming = false;
1544 vc4_hdmi_audio_reset(vc4_hdmi);
1546 mutex_unlock(&vc4_hdmi->mutex);
1549 static int sample_rate_to_mai_fmt(int samplerate)
1551 switch (samplerate) {
1553 return VC4_HDMI_MAI_SAMPLE_RATE_8000;
1555 return VC4_HDMI_MAI_SAMPLE_RATE_11025;
1557 return VC4_HDMI_MAI_SAMPLE_RATE_12000;
1559 return VC4_HDMI_MAI_SAMPLE_RATE_16000;
1561 return VC4_HDMI_MAI_SAMPLE_RATE_22050;
1563 return VC4_HDMI_MAI_SAMPLE_RATE_24000;
1565 return VC4_HDMI_MAI_SAMPLE_RATE_32000;
1567 return VC4_HDMI_MAI_SAMPLE_RATE_44100;
1569 return VC4_HDMI_MAI_SAMPLE_RATE_48000;
1571 return VC4_HDMI_MAI_SAMPLE_RATE_64000;
1573 return VC4_HDMI_MAI_SAMPLE_RATE_88200;
1575 return VC4_HDMI_MAI_SAMPLE_RATE_96000;
1577 return VC4_HDMI_MAI_SAMPLE_RATE_128000;
1579 return VC4_HDMI_MAI_SAMPLE_RATE_176400;
1581 return VC4_HDMI_MAI_SAMPLE_RATE_192000;
1583 return VC4_HDMI_MAI_SAMPLE_RATE_NOT_INDICATED;
1587 /* HDMI audio codec callbacks */
1588 static int vc4_hdmi_audio_prepare(struct device *dev, void *data,
1589 struct hdmi_codec_daifmt *daifmt,
1590 struct hdmi_codec_params *params)
1592 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
1593 struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
1594 unsigned int sample_rate = params->sample_rate;
1595 unsigned int channels = params->channels;
1596 unsigned long flags;
1597 u32 audio_packet_config, channel_mask;
1599 u32 mai_audio_format;
1600 u32 mai_sample_rate;
1602 dev_dbg(dev, "%s: %u Hz, %d bit, %d channels\n", __func__,
1603 sample_rate, params->sample_width, channels);
1605 mutex_lock(&vc4_hdmi->mutex);
1607 if (!vc4_hdmi_audio_can_stream(vc4_hdmi)) {
1608 mutex_unlock(&vc4_hdmi->mutex);
1612 vc4_hdmi_audio_set_mai_clock(vc4_hdmi, sample_rate);
1614 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1615 HDMI_WRITE(HDMI_MAI_CTL,
1616 VC4_SET_FIELD(channels, VC4_HD_MAI_CTL_CHNUM) |
1617 VC4_HD_MAI_CTL_WHOLSMP |
1618 VC4_HD_MAI_CTL_CHALIGN |
1619 VC4_HD_MAI_CTL_ENABLE);
1621 mai_sample_rate = sample_rate_to_mai_fmt(sample_rate);
1622 if (params->iec.status[0] & IEC958_AES0_NONAUDIO &&
1623 params->channels == 8)
1624 mai_audio_format = VC4_HDMI_MAI_FORMAT_HBR;
1626 mai_audio_format = VC4_HDMI_MAI_FORMAT_PCM;
1627 HDMI_WRITE(HDMI_MAI_FMT,
1628 VC4_SET_FIELD(mai_sample_rate,
1629 VC4_HDMI_MAI_FORMAT_SAMPLE_RATE) |
1630 VC4_SET_FIELD(mai_audio_format,
1631 VC4_HDMI_MAI_FORMAT_AUDIO_FORMAT));
1633 /* The B frame identifier should match the value used by alsa-lib (8) */
1634 audio_packet_config =
1635 VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_SAMPLE_FLAT |
1636 VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_INACTIVE_CHANNELS |
1637 VC4_SET_FIELD(0x8, VC4_HDMI_AUDIO_PACKET_B_FRAME_IDENTIFIER);
1639 channel_mask = GENMASK(channels - 1, 0);
1640 audio_packet_config |= VC4_SET_FIELD(channel_mask,
1641 VC4_HDMI_AUDIO_PACKET_CEA_MASK);
1643 /* Set the MAI threshold */
1644 HDMI_WRITE(HDMI_MAI_THR,
1645 VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_PANICHIGH) |
1646 VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_PANICLOW) |
1647 VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_DREQHIGH) |
1648 VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_DREQLOW));
1650 HDMI_WRITE(HDMI_MAI_CONFIG,
1651 VC4_HDMI_MAI_CONFIG_BIT_REVERSE |
1652 VC4_HDMI_MAI_CONFIG_FORMAT_REVERSE |
1653 VC4_SET_FIELD(channel_mask, VC4_HDMI_MAI_CHANNEL_MASK));
1655 channel_map = vc4_hdmi->variant->channel_map(vc4_hdmi, channel_mask);
1656 HDMI_WRITE(HDMI_MAI_CHANNEL_MAP, channel_map);
1657 HDMI_WRITE(HDMI_AUDIO_PACKET_CONFIG, audio_packet_config);
1659 vc4_hdmi_set_n_cts(vc4_hdmi, sample_rate);
1661 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1663 memcpy(&vc4_hdmi->audio.infoframe, ¶ms->cea, sizeof(params->cea));
1664 vc4_hdmi_set_audio_infoframe(encoder);
1666 mutex_unlock(&vc4_hdmi->mutex);
1671 static const struct snd_soc_component_driver vc4_hdmi_audio_cpu_dai_comp = {
1672 .name = "vc4-hdmi-cpu-dai-component",
1675 static int vc4_hdmi_audio_cpu_dai_probe(struct snd_soc_dai *dai)
1677 struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai);
1679 snd_soc_dai_init_dma_data(dai, &vc4_hdmi->audio.dma_data, NULL);
1684 static struct snd_soc_dai_driver vc4_hdmi_audio_cpu_dai_drv = {
1685 .name = "vc4-hdmi-cpu-dai",
1686 .probe = vc4_hdmi_audio_cpu_dai_probe,
1688 .stream_name = "Playback",
1691 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
1692 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
1693 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
1694 SNDRV_PCM_RATE_192000,
1695 .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
1699 static const struct snd_dmaengine_pcm_config pcm_conf = {
1700 .chan_names[SNDRV_PCM_STREAM_PLAYBACK] = "audio-rx",
1701 .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
1704 static int vc4_hdmi_audio_get_eld(struct device *dev, void *data,
1705 uint8_t *buf, size_t len)
1707 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
1708 struct drm_connector *connector = &vc4_hdmi->connector;
1710 mutex_lock(&vc4_hdmi->mutex);
1711 memcpy(buf, connector->eld, min(sizeof(connector->eld), len));
1712 mutex_unlock(&vc4_hdmi->mutex);
1717 static const struct hdmi_codec_ops vc4_hdmi_codec_ops = {
1718 .get_eld = vc4_hdmi_audio_get_eld,
1719 .prepare = vc4_hdmi_audio_prepare,
1720 .audio_shutdown = vc4_hdmi_audio_shutdown,
1721 .audio_startup = vc4_hdmi_audio_startup,
1724 static struct hdmi_codec_pdata vc4_hdmi_codec_pdata = {
1725 .ops = &vc4_hdmi_codec_ops,
1726 .max_i2s_channels = 8,
1730 static int vc4_hdmi_audio_init(struct vc4_hdmi *vc4_hdmi)
1732 const struct vc4_hdmi_register *mai_data =
1733 &vc4_hdmi->variant->registers[HDMI_MAI_DATA];
1734 struct snd_soc_dai_link *dai_link = &vc4_hdmi->audio.link;
1735 struct snd_soc_card *card = &vc4_hdmi->audio.card;
1736 struct device *dev = &vc4_hdmi->pdev->dev;
1737 struct platform_device *codec_pdev;
1742 if (!of_find_property(dev->of_node, "dmas", NULL)) {
1744 "'dmas' DT property is missing, no HDMI audio\n");
1748 if (mai_data->reg != VC4_HD) {
1749 WARN_ONCE(true, "MAI isn't in the HD block\n");
1754 * Get the physical address of VC4_HD_MAI_DATA. We need to retrieve
1755 * the bus address specified in the DT, because the physical address
1756 * (the one returned by platform_get_resource()) is not appropriate
1757 * for DMA transfers.
1758 * This VC/MMU should probably be exposed to avoid this kind of hacks.
1760 index = of_property_match_string(dev->of_node, "reg-names", "hd");
1761 /* Before BCM2711, we don't have a named register range */
1765 addr = of_get_address(dev->of_node, index, NULL, NULL);
1767 vc4_hdmi->audio.dma_data.addr = be32_to_cpup(addr) + mai_data->offset;
1768 vc4_hdmi->audio.dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1769 vc4_hdmi->audio.dma_data.maxburst = 2;
1771 ret = devm_snd_dmaengine_pcm_register(dev, &pcm_conf, 0);
1773 dev_err(dev, "Could not register PCM component: %d\n", ret);
1777 ret = devm_snd_soc_register_component(dev, &vc4_hdmi_audio_cpu_dai_comp,
1778 &vc4_hdmi_audio_cpu_dai_drv, 1);
1780 dev_err(dev, "Could not register CPU DAI: %d\n", ret);
1784 codec_pdev = platform_device_register_data(dev, HDMI_CODEC_DRV_NAME,
1785 PLATFORM_DEVID_AUTO,
1786 &vc4_hdmi_codec_pdata,
1787 sizeof(vc4_hdmi_codec_pdata));
1788 if (IS_ERR(codec_pdev)) {
1789 dev_err(dev, "Couldn't register the HDMI codec: %ld\n", PTR_ERR(codec_pdev));
1790 return PTR_ERR(codec_pdev);
1793 dai_link->cpus = &vc4_hdmi->audio.cpu;
1794 dai_link->codecs = &vc4_hdmi->audio.codec;
1795 dai_link->platforms = &vc4_hdmi->audio.platform;
1797 dai_link->num_cpus = 1;
1798 dai_link->num_codecs = 1;
1799 dai_link->num_platforms = 1;
1801 dai_link->name = "MAI";
1802 dai_link->stream_name = "MAI PCM";
1803 dai_link->codecs->dai_name = "i2s-hifi";
1804 dai_link->cpus->dai_name = dev_name(dev);
1805 dai_link->codecs->name = dev_name(&codec_pdev->dev);
1806 dai_link->platforms->name = dev_name(dev);
1808 card->dai_link = dai_link;
1809 card->num_links = 1;
1810 card->name = vc4_hdmi->variant->card_name;
1811 card->driver_name = "vc4-hdmi";
1813 card->owner = THIS_MODULE;
1816 * Be careful, snd_soc_register_card() calls dev_set_drvdata() and
1817 * stores a pointer to the snd card object in dev->driver_data. This
1818 * means we cannot use it for something else. The hdmi back-pointer is
1819 * now stored in card->drvdata and should be retrieved with
1820 * snd_soc_card_get_drvdata() if needed.
1822 snd_soc_card_set_drvdata(card, vc4_hdmi);
1823 ret = devm_snd_soc_register_card(dev, card);
1825 dev_err_probe(dev, ret, "Could not register sound card\n");
1831 static irqreturn_t vc4_hdmi_hpd_irq_thread(int irq, void *priv)
1833 struct vc4_hdmi *vc4_hdmi = priv;
1834 struct drm_connector *connector = &vc4_hdmi->connector;
1835 struct drm_device *dev = connector->dev;
1837 if (dev && dev->registered)
1838 drm_connector_helper_hpd_irq_event(connector);
1843 static int vc4_hdmi_hotplug_init(struct vc4_hdmi *vc4_hdmi)
1845 struct drm_connector *connector = &vc4_hdmi->connector;
1846 struct platform_device *pdev = vc4_hdmi->pdev;
1849 if (vc4_hdmi->variant->external_irq_controller) {
1850 unsigned int hpd_con = platform_get_irq_byname(pdev, "hpd-connected");
1851 unsigned int hpd_rm = platform_get_irq_byname(pdev, "hpd-removed");
1853 ret = request_threaded_irq(hpd_con,
1855 vc4_hdmi_hpd_irq_thread, IRQF_ONESHOT,
1856 "vc4 hdmi hpd connected", vc4_hdmi);
1860 ret = request_threaded_irq(hpd_rm,
1862 vc4_hdmi_hpd_irq_thread, IRQF_ONESHOT,
1863 "vc4 hdmi hpd disconnected", vc4_hdmi);
1865 free_irq(hpd_con, vc4_hdmi);
1869 connector->polled = DRM_CONNECTOR_POLL_HPD;
1875 static void vc4_hdmi_hotplug_exit(struct vc4_hdmi *vc4_hdmi)
1877 struct platform_device *pdev = vc4_hdmi->pdev;
1879 if (vc4_hdmi->variant->external_irq_controller) {
1880 free_irq(platform_get_irq_byname(pdev, "hpd-connected"), vc4_hdmi);
1881 free_irq(platform_get_irq_byname(pdev, "hpd-removed"), vc4_hdmi);
1885 #ifdef CONFIG_DRM_VC4_HDMI_CEC
1886 static irqreturn_t vc4_cec_irq_handler_rx_thread(int irq, void *priv)
1888 struct vc4_hdmi *vc4_hdmi = priv;
1890 if (vc4_hdmi->cec_rx_msg.len)
1891 cec_received_msg(vc4_hdmi->cec_adap,
1892 &vc4_hdmi->cec_rx_msg);
1897 static irqreturn_t vc4_cec_irq_handler_tx_thread(int irq, void *priv)
1899 struct vc4_hdmi *vc4_hdmi = priv;
1901 if (vc4_hdmi->cec_tx_ok) {
1902 cec_transmit_done(vc4_hdmi->cec_adap, CEC_TX_STATUS_OK,
1906 * This CEC implementation makes 1 retry, so if we
1907 * get a NACK, then that means it made 2 attempts.
1909 cec_transmit_done(vc4_hdmi->cec_adap, CEC_TX_STATUS_NACK,
1915 static irqreturn_t vc4_cec_irq_handler_thread(int irq, void *priv)
1917 struct vc4_hdmi *vc4_hdmi = priv;
1920 if (vc4_hdmi->cec_irq_was_rx)
1921 ret = vc4_cec_irq_handler_rx_thread(irq, priv);
1923 ret = vc4_cec_irq_handler_tx_thread(irq, priv);
1928 static void vc4_cec_read_msg(struct vc4_hdmi *vc4_hdmi, u32 cntrl1)
1930 struct drm_device *dev = vc4_hdmi->connector.dev;
1931 struct cec_msg *msg = &vc4_hdmi->cec_rx_msg;
1934 lockdep_assert_held(&vc4_hdmi->hw_lock);
1936 msg->len = 1 + ((cntrl1 & VC4_HDMI_CEC_REC_WRD_CNT_MASK) >>
1937 VC4_HDMI_CEC_REC_WRD_CNT_SHIFT);
1939 if (msg->len > 16) {
1940 drm_err(dev, "Attempting to read too much data (%d)\n", msg->len);
1944 for (i = 0; i < msg->len; i += 4) {
1945 u32 val = HDMI_READ(HDMI_CEC_RX_DATA_1 + (i >> 2));
1947 msg->msg[i] = val & 0xff;
1948 msg->msg[i + 1] = (val >> 8) & 0xff;
1949 msg->msg[i + 2] = (val >> 16) & 0xff;
1950 msg->msg[i + 3] = (val >> 24) & 0xff;
1954 static irqreturn_t vc4_cec_irq_handler_tx_bare_locked(struct vc4_hdmi *vc4_hdmi)
1958 lockdep_assert_held(&vc4_hdmi->hw_lock);
1960 cntrl1 = HDMI_READ(HDMI_CEC_CNTRL_1);
1961 vc4_hdmi->cec_tx_ok = cntrl1 & VC4_HDMI_CEC_TX_STATUS_GOOD;
1962 cntrl1 &= ~VC4_HDMI_CEC_START_XMIT_BEGIN;
1963 HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1);
1965 return IRQ_WAKE_THREAD;
1968 static irqreturn_t vc4_cec_irq_handler_tx_bare(int irq, void *priv)
1970 struct vc4_hdmi *vc4_hdmi = priv;
1973 spin_lock(&vc4_hdmi->hw_lock);
1974 ret = vc4_cec_irq_handler_tx_bare_locked(vc4_hdmi);
1975 spin_unlock(&vc4_hdmi->hw_lock);
1980 static irqreturn_t vc4_cec_irq_handler_rx_bare_locked(struct vc4_hdmi *vc4_hdmi)
1984 lockdep_assert_held(&vc4_hdmi->hw_lock);
1986 vc4_hdmi->cec_rx_msg.len = 0;
1987 cntrl1 = HDMI_READ(HDMI_CEC_CNTRL_1);
1988 vc4_cec_read_msg(vc4_hdmi, cntrl1);
1989 cntrl1 |= VC4_HDMI_CEC_CLEAR_RECEIVE_OFF;
1990 HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1);
1991 cntrl1 &= ~VC4_HDMI_CEC_CLEAR_RECEIVE_OFF;
1993 HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1);
1995 return IRQ_WAKE_THREAD;
1998 static irqreturn_t vc4_cec_irq_handler_rx_bare(int irq, void *priv)
2000 struct vc4_hdmi *vc4_hdmi = priv;
2003 spin_lock(&vc4_hdmi->hw_lock);
2004 ret = vc4_cec_irq_handler_rx_bare_locked(vc4_hdmi);
2005 spin_unlock(&vc4_hdmi->hw_lock);
2010 static irqreturn_t vc4_cec_irq_handler(int irq, void *priv)
2012 struct vc4_hdmi *vc4_hdmi = priv;
2013 u32 stat = HDMI_READ(HDMI_CEC_CPU_STATUS);
2017 if (!(stat & VC4_HDMI_CPU_CEC))
2020 spin_lock(&vc4_hdmi->hw_lock);
2021 cntrl5 = HDMI_READ(HDMI_CEC_CNTRL_5);
2022 vc4_hdmi->cec_irq_was_rx = cntrl5 & VC4_HDMI_CEC_RX_CEC_INT;
2023 if (vc4_hdmi->cec_irq_was_rx)
2024 ret = vc4_cec_irq_handler_rx_bare_locked(vc4_hdmi);
2026 ret = vc4_cec_irq_handler_tx_bare_locked(vc4_hdmi);
2028 HDMI_WRITE(HDMI_CEC_CPU_CLEAR, VC4_HDMI_CPU_CEC);
2029 spin_unlock(&vc4_hdmi->hw_lock);
2034 static int vc4_hdmi_cec_enable(struct cec_adapter *adap)
2036 struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
2037 /* clock period in microseconds */
2038 const u32 usecs = 1000000 / CEC_CLOCK_FREQ;
2039 unsigned long flags;
2044 * NOTE: This function should really take vc4_hdmi->mutex, but doing so
2045 * results in a reentrancy since cec_s_phys_addr_from_edid() called in
2046 * .detect or .get_modes might call .adap_enable, which leads to this
2047 * function being called with that mutex held.
2049 * Concurrency is not an issue for the moment since we don't share any
2050 * state with KMS, so we can ignore the lock for now, but we need to
2051 * keep it in mind if we were to change that assumption.
2054 ret = pm_runtime_resume_and_get(&vc4_hdmi->pdev->dev);
2058 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2060 val = HDMI_READ(HDMI_CEC_CNTRL_5);
2061 val &= ~(VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET |
2062 VC4_HDMI_CEC_CNT_TO_4700_US_MASK |
2063 VC4_HDMI_CEC_CNT_TO_4500_US_MASK);
2064 val |= ((4700 / usecs) << VC4_HDMI_CEC_CNT_TO_4700_US_SHIFT) |
2065 ((4500 / usecs) << VC4_HDMI_CEC_CNT_TO_4500_US_SHIFT);
2067 HDMI_WRITE(HDMI_CEC_CNTRL_5, val |
2068 VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET);
2069 HDMI_WRITE(HDMI_CEC_CNTRL_5, val);
2070 HDMI_WRITE(HDMI_CEC_CNTRL_2,
2071 ((1500 / usecs) << VC4_HDMI_CEC_CNT_TO_1500_US_SHIFT) |
2072 ((1300 / usecs) << VC4_HDMI_CEC_CNT_TO_1300_US_SHIFT) |
2073 ((800 / usecs) << VC4_HDMI_CEC_CNT_TO_800_US_SHIFT) |
2074 ((600 / usecs) << VC4_HDMI_CEC_CNT_TO_600_US_SHIFT) |
2075 ((400 / usecs) << VC4_HDMI_CEC_CNT_TO_400_US_SHIFT));
2076 HDMI_WRITE(HDMI_CEC_CNTRL_3,
2077 ((2750 / usecs) << VC4_HDMI_CEC_CNT_TO_2750_US_SHIFT) |
2078 ((2400 / usecs) << VC4_HDMI_CEC_CNT_TO_2400_US_SHIFT) |
2079 ((2050 / usecs) << VC4_HDMI_CEC_CNT_TO_2050_US_SHIFT) |
2080 ((1700 / usecs) << VC4_HDMI_CEC_CNT_TO_1700_US_SHIFT));
2081 HDMI_WRITE(HDMI_CEC_CNTRL_4,
2082 ((4300 / usecs) << VC4_HDMI_CEC_CNT_TO_4300_US_SHIFT) |
2083 ((3900 / usecs) << VC4_HDMI_CEC_CNT_TO_3900_US_SHIFT) |
2084 ((3600 / usecs) << VC4_HDMI_CEC_CNT_TO_3600_US_SHIFT) |
2085 ((3500 / usecs) << VC4_HDMI_CEC_CNT_TO_3500_US_SHIFT));
2087 if (!vc4_hdmi->variant->external_irq_controller)
2088 HDMI_WRITE(HDMI_CEC_CPU_MASK_CLEAR, VC4_HDMI_CPU_CEC);
2090 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2095 static int vc4_hdmi_cec_disable(struct cec_adapter *adap)
2097 struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
2098 unsigned long flags;
2101 * NOTE: This function should really take vc4_hdmi->mutex, but doing so
2102 * results in a reentrancy since cec_s_phys_addr_from_edid() called in
2103 * .detect or .get_modes might call .adap_enable, which leads to this
2104 * function being called with that mutex held.
2106 * Concurrency is not an issue for the moment since we don't share any
2107 * state with KMS, so we can ignore the lock for now, but we need to
2108 * keep it in mind if we were to change that assumption.
2111 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2113 if (!vc4_hdmi->variant->external_irq_controller)
2114 HDMI_WRITE(HDMI_CEC_CPU_MASK_SET, VC4_HDMI_CPU_CEC);
2116 HDMI_WRITE(HDMI_CEC_CNTRL_5, HDMI_READ(HDMI_CEC_CNTRL_5) |
2117 VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET);
2119 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2121 pm_runtime_put(&vc4_hdmi->pdev->dev);
2126 static int vc4_hdmi_cec_adap_enable(struct cec_adapter *adap, bool enable)
2129 return vc4_hdmi_cec_enable(adap);
2131 return vc4_hdmi_cec_disable(adap);
2134 static int vc4_hdmi_cec_adap_log_addr(struct cec_adapter *adap, u8 log_addr)
2136 struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
2137 unsigned long flags;
2140 * NOTE: This function should really take vc4_hdmi->mutex, but doing so
2141 * results in a reentrancy since cec_s_phys_addr_from_edid() called in
2142 * .detect or .get_modes might call .adap_enable, which leads to this
2143 * function being called with that mutex held.
2145 * Concurrency is not an issue for the moment since we don't share any
2146 * state with KMS, so we can ignore the lock for now, but we need to
2147 * keep it in mind if we were to change that assumption.
2150 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2151 HDMI_WRITE(HDMI_CEC_CNTRL_1,
2152 (HDMI_READ(HDMI_CEC_CNTRL_1) & ~VC4_HDMI_CEC_ADDR_MASK) |
2153 (log_addr & 0xf) << VC4_HDMI_CEC_ADDR_SHIFT);
2154 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2159 static int vc4_hdmi_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
2160 u32 signal_free_time, struct cec_msg *msg)
2162 struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
2163 struct drm_device *dev = vc4_hdmi->connector.dev;
2164 unsigned long flags;
2169 * NOTE: This function should really take vc4_hdmi->mutex, but doing so
2170 * results in a reentrancy since cec_s_phys_addr_from_edid() called in
2171 * .detect or .get_modes might call .adap_enable, which leads to this
2172 * function being called with that mutex held.
2174 * Concurrency is not an issue for the moment since we don't share any
2175 * state with KMS, so we can ignore the lock for now, but we need to
2176 * keep it in mind if we were to change that assumption.
2179 if (msg->len > 16) {
2180 drm_err(dev, "Attempting to transmit too much data (%d)\n", msg->len);
2184 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2186 for (i = 0; i < msg->len; i += 4)
2187 HDMI_WRITE(HDMI_CEC_TX_DATA_1 + (i >> 2),
2189 (msg->msg[i + 1] << 8) |
2190 (msg->msg[i + 2] << 16) |
2191 (msg->msg[i + 3] << 24));
2193 val = HDMI_READ(HDMI_CEC_CNTRL_1);
2194 val &= ~VC4_HDMI_CEC_START_XMIT_BEGIN;
2195 HDMI_WRITE(HDMI_CEC_CNTRL_1, val);
2196 val &= ~VC4_HDMI_CEC_MESSAGE_LENGTH_MASK;
2197 val |= (msg->len - 1) << VC4_HDMI_CEC_MESSAGE_LENGTH_SHIFT;
2198 val |= VC4_HDMI_CEC_START_XMIT_BEGIN;
2200 HDMI_WRITE(HDMI_CEC_CNTRL_1, val);
2202 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2207 static const struct cec_adap_ops vc4_hdmi_cec_adap_ops = {
2208 .adap_enable = vc4_hdmi_cec_adap_enable,
2209 .adap_log_addr = vc4_hdmi_cec_adap_log_addr,
2210 .adap_transmit = vc4_hdmi_cec_adap_transmit,
2213 static int vc4_hdmi_cec_init(struct vc4_hdmi *vc4_hdmi)
2215 struct cec_connector_info conn_info;
2216 struct platform_device *pdev = vc4_hdmi->pdev;
2217 struct device *dev = &pdev->dev;
2218 unsigned long flags;
2222 if (!of_find_property(dev->of_node, "interrupts", NULL)) {
2223 dev_warn(dev, "'interrupts' DT property is missing, no CEC\n");
2227 vc4_hdmi->cec_adap = cec_allocate_adapter(&vc4_hdmi_cec_adap_ops,
2230 CEC_CAP_CONNECTOR_INFO, 1);
2231 ret = PTR_ERR_OR_ZERO(vc4_hdmi->cec_adap);
2235 cec_fill_conn_info_from_drm(&conn_info, &vc4_hdmi->connector);
2236 cec_s_conn_info(vc4_hdmi->cec_adap, &conn_info);
2238 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2239 value = HDMI_READ(HDMI_CEC_CNTRL_1);
2240 /* Set the logical address to Unregistered */
2241 value |= VC4_HDMI_CEC_ADDR_MASK;
2242 HDMI_WRITE(HDMI_CEC_CNTRL_1, value);
2243 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2245 vc4_hdmi_cec_update_clk_div(vc4_hdmi);
2247 if (vc4_hdmi->variant->external_irq_controller) {
2248 ret = request_threaded_irq(platform_get_irq_byname(pdev, "cec-rx"),
2249 vc4_cec_irq_handler_rx_bare,
2250 vc4_cec_irq_handler_rx_thread, 0,
2251 "vc4 hdmi cec rx", vc4_hdmi);
2253 goto err_delete_cec_adap;
2255 ret = request_threaded_irq(platform_get_irq_byname(pdev, "cec-tx"),
2256 vc4_cec_irq_handler_tx_bare,
2257 vc4_cec_irq_handler_tx_thread, 0,
2258 "vc4 hdmi cec tx", vc4_hdmi);
2260 goto err_remove_cec_rx_handler;
2262 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2263 HDMI_WRITE(HDMI_CEC_CPU_MASK_SET, 0xffffffff);
2264 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2266 ret = request_threaded_irq(platform_get_irq(pdev, 0),
2267 vc4_cec_irq_handler,
2268 vc4_cec_irq_handler_thread, 0,
2269 "vc4 hdmi cec", vc4_hdmi);
2271 goto err_delete_cec_adap;
2274 ret = cec_register_adapter(vc4_hdmi->cec_adap, &pdev->dev);
2276 goto err_remove_handlers;
2280 err_remove_handlers:
2281 if (vc4_hdmi->variant->external_irq_controller)
2282 free_irq(platform_get_irq_byname(pdev, "cec-tx"), vc4_hdmi);
2284 free_irq(platform_get_irq(pdev, 0), vc4_hdmi);
2286 err_remove_cec_rx_handler:
2287 if (vc4_hdmi->variant->external_irq_controller)
2288 free_irq(platform_get_irq_byname(pdev, "cec-rx"), vc4_hdmi);
2290 err_delete_cec_adap:
2291 cec_delete_adapter(vc4_hdmi->cec_adap);
2296 static void vc4_hdmi_cec_exit(struct vc4_hdmi *vc4_hdmi)
2298 struct platform_device *pdev = vc4_hdmi->pdev;
2300 if (vc4_hdmi->variant->external_irq_controller) {
2301 free_irq(platform_get_irq_byname(pdev, "cec-rx"), vc4_hdmi);
2302 free_irq(platform_get_irq_byname(pdev, "cec-tx"), vc4_hdmi);
2304 free_irq(platform_get_irq(pdev, 0), vc4_hdmi);
2307 cec_unregister_adapter(vc4_hdmi->cec_adap);
2310 static int vc4_hdmi_cec_init(struct vc4_hdmi *vc4_hdmi)
2315 static void vc4_hdmi_cec_exit(struct vc4_hdmi *vc4_hdmi) {};
2319 static int vc4_hdmi_build_regset(struct vc4_hdmi *vc4_hdmi,
2320 struct debugfs_regset32 *regset,
2321 enum vc4_hdmi_regs reg)
2323 const struct vc4_hdmi_variant *variant = vc4_hdmi->variant;
2324 struct debugfs_reg32 *regs, *new_regs;
2325 unsigned int count = 0;
2328 regs = kcalloc(variant->num_registers, sizeof(*regs),
2333 for (i = 0; i < variant->num_registers; i++) {
2334 const struct vc4_hdmi_register *field = &variant->registers[i];
2336 if (field->reg != reg)
2339 regs[count].name = field->name;
2340 regs[count].offset = field->offset;
2344 new_regs = krealloc(regs, count * sizeof(*regs), GFP_KERNEL);
2348 regset->base = __vc4_hdmi_get_field_base(vc4_hdmi, reg);
2349 regset->regs = new_regs;
2350 regset->nregs = count;
2355 static int vc4_hdmi_init_resources(struct vc4_hdmi *vc4_hdmi)
2357 struct platform_device *pdev = vc4_hdmi->pdev;
2358 struct device *dev = &pdev->dev;
2361 vc4_hdmi->hdmicore_regs = vc4_ioremap_regs(pdev, 0);
2362 if (IS_ERR(vc4_hdmi->hdmicore_regs))
2363 return PTR_ERR(vc4_hdmi->hdmicore_regs);
2365 vc4_hdmi->hd_regs = vc4_ioremap_regs(pdev, 1);
2366 if (IS_ERR(vc4_hdmi->hd_regs))
2367 return PTR_ERR(vc4_hdmi->hd_regs);
2369 ret = vc4_hdmi_build_regset(vc4_hdmi, &vc4_hdmi->hd_regset, VC4_HD);
2373 ret = vc4_hdmi_build_regset(vc4_hdmi, &vc4_hdmi->hdmi_regset, VC4_HDMI);
2377 vc4_hdmi->pixel_clock = devm_clk_get(dev, "pixel");
2378 if (IS_ERR(vc4_hdmi->pixel_clock)) {
2379 ret = PTR_ERR(vc4_hdmi->pixel_clock);
2380 if (ret != -EPROBE_DEFER)
2381 DRM_ERROR("Failed to get pixel clock\n");
2385 vc4_hdmi->hsm_clock = devm_clk_get(dev, "hdmi");
2386 if (IS_ERR(vc4_hdmi->hsm_clock)) {
2387 DRM_ERROR("Failed to get HDMI state machine clock\n");
2388 return PTR_ERR(vc4_hdmi->hsm_clock);
2390 vc4_hdmi->audio_clock = vc4_hdmi->hsm_clock;
2391 vc4_hdmi->cec_clock = vc4_hdmi->hsm_clock;
2396 static int vc5_hdmi_init_resources(struct vc4_hdmi *vc4_hdmi)
2398 struct platform_device *pdev = vc4_hdmi->pdev;
2399 struct device *dev = &pdev->dev;
2400 struct resource *res;
2402 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hdmi");
2406 vc4_hdmi->hdmicore_regs = devm_ioremap(dev, res->start,
2407 resource_size(res));
2408 if (!vc4_hdmi->hdmicore_regs)
2411 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hd");
2415 vc4_hdmi->hd_regs = devm_ioremap(dev, res->start, resource_size(res));
2416 if (!vc4_hdmi->hd_regs)
2419 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cec");
2423 vc4_hdmi->cec_regs = devm_ioremap(dev, res->start, resource_size(res));
2424 if (!vc4_hdmi->cec_regs)
2427 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "csc");
2431 vc4_hdmi->csc_regs = devm_ioremap(dev, res->start, resource_size(res));
2432 if (!vc4_hdmi->csc_regs)
2435 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dvp");
2439 vc4_hdmi->dvp_regs = devm_ioremap(dev, res->start, resource_size(res));
2440 if (!vc4_hdmi->dvp_regs)
2443 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy");
2447 vc4_hdmi->phy_regs = devm_ioremap(dev, res->start, resource_size(res));
2448 if (!vc4_hdmi->phy_regs)
2451 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "packet");
2455 vc4_hdmi->ram_regs = devm_ioremap(dev, res->start, resource_size(res));
2456 if (!vc4_hdmi->ram_regs)
2459 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rm");
2463 vc4_hdmi->rm_regs = devm_ioremap(dev, res->start, resource_size(res));
2464 if (!vc4_hdmi->rm_regs)
2467 vc4_hdmi->hsm_clock = devm_clk_get(dev, "hdmi");
2468 if (IS_ERR(vc4_hdmi->hsm_clock)) {
2469 DRM_ERROR("Failed to get HDMI state machine clock\n");
2470 return PTR_ERR(vc4_hdmi->hsm_clock);
2473 vc4_hdmi->pixel_bvb_clock = devm_clk_get(dev, "bvb");
2474 if (IS_ERR(vc4_hdmi->pixel_bvb_clock)) {
2475 DRM_ERROR("Failed to get pixel bvb clock\n");
2476 return PTR_ERR(vc4_hdmi->pixel_bvb_clock);
2479 vc4_hdmi->audio_clock = devm_clk_get(dev, "audio");
2480 if (IS_ERR(vc4_hdmi->audio_clock)) {
2481 DRM_ERROR("Failed to get audio clock\n");
2482 return PTR_ERR(vc4_hdmi->audio_clock);
2485 vc4_hdmi->cec_clock = devm_clk_get(dev, "cec");
2486 if (IS_ERR(vc4_hdmi->cec_clock)) {
2487 DRM_ERROR("Failed to get CEC clock\n");
2488 return PTR_ERR(vc4_hdmi->cec_clock);
2491 vc4_hdmi->reset = devm_reset_control_get(dev, NULL);
2492 if (IS_ERR(vc4_hdmi->reset)) {
2493 DRM_ERROR("Failed to get HDMI reset line\n");
2494 return PTR_ERR(vc4_hdmi->reset);
2500 static int __maybe_unused vc4_hdmi_runtime_suspend(struct device *dev)
2502 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
2504 clk_disable_unprepare(vc4_hdmi->hsm_clock);
2509 static int vc4_hdmi_runtime_resume(struct device *dev)
2511 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
2514 ret = clk_prepare_enable(vc4_hdmi->hsm_clock);
2521 static int vc4_hdmi_bind(struct device *dev, struct device *master, void *data)
2523 const struct vc4_hdmi_variant *variant = of_device_get_match_data(dev);
2524 struct platform_device *pdev = to_platform_device(dev);
2525 struct drm_device *drm = dev_get_drvdata(master);
2526 struct vc4_hdmi *vc4_hdmi;
2527 struct drm_encoder *encoder;
2528 struct device_node *ddc_node;
2531 vc4_hdmi = devm_kzalloc(dev, sizeof(*vc4_hdmi), GFP_KERNEL);
2534 mutex_init(&vc4_hdmi->mutex);
2535 spin_lock_init(&vc4_hdmi->hw_lock);
2536 INIT_DELAYED_WORK(&vc4_hdmi->scrambling_work, vc4_hdmi_scrambling_wq);
2538 dev_set_drvdata(dev, vc4_hdmi);
2539 encoder = &vc4_hdmi->encoder.base.base;
2540 vc4_hdmi->encoder.base.type = variant->encoder_type;
2541 vc4_hdmi->encoder.base.pre_crtc_configure = vc4_hdmi_encoder_pre_crtc_configure;
2542 vc4_hdmi->encoder.base.pre_crtc_enable = vc4_hdmi_encoder_pre_crtc_enable;
2543 vc4_hdmi->encoder.base.post_crtc_enable = vc4_hdmi_encoder_post_crtc_enable;
2544 vc4_hdmi->encoder.base.post_crtc_disable = vc4_hdmi_encoder_post_crtc_disable;
2545 vc4_hdmi->encoder.base.post_crtc_powerdown = vc4_hdmi_encoder_post_crtc_powerdown;
2546 vc4_hdmi->pdev = pdev;
2547 vc4_hdmi->variant = variant;
2550 * Since we don't know the state of the controller and its
2551 * display (if any), let's assume it's always enabled.
2552 * vc4_hdmi_disable_scrambling() will thus run at boot, make
2553 * sure it's disabled, and avoid any inconsistency.
2555 vc4_hdmi->scdc_enabled = true;
2557 ret = variant->init_resources(vc4_hdmi);
2561 ddc_node = of_parse_phandle(dev->of_node, "ddc", 0);
2563 DRM_ERROR("Failed to find ddc node in device tree\n");
2567 vc4_hdmi->ddc = of_find_i2c_adapter_by_node(ddc_node);
2568 of_node_put(ddc_node);
2569 if (!vc4_hdmi->ddc) {
2570 DRM_DEBUG("Failed to get ddc i2c adapter by node\n");
2571 return -EPROBE_DEFER;
2574 /* Only use the GPIO HPD pin if present in the DT, otherwise
2575 * we'll use the HDMI core's register.
2577 vc4_hdmi->hpd_gpio = devm_gpiod_get_optional(dev, "hpd", GPIOD_IN);
2578 if (IS_ERR(vc4_hdmi->hpd_gpio)) {
2579 ret = PTR_ERR(vc4_hdmi->hpd_gpio);
2583 vc4_hdmi->disable_wifi_frequencies =
2584 of_property_read_bool(dev->of_node, "wifi-2.4ghz-coexistence");
2586 if (variant->max_pixel_clock == 600000000) {
2587 struct vc4_dev *vc4 = to_vc4_dev(drm);
2588 long max_rate = clk_round_rate(vc4->hvs->core_clk, 550000000);
2590 if (max_rate < 550000000)
2591 vc4_hdmi->disable_4kp60 = true;
2595 * If we boot without any cable connected to the HDMI connector,
2596 * the firmware will skip the HSM initialization and leave it
2597 * with a rate of 0, resulting in a bus lockup when we're
2598 * accessing the registers even if it's enabled.
2600 * Let's put a sensible default at runtime_resume so that we
2601 * don't end up in this situation.
2603 ret = clk_set_min_rate(vc4_hdmi->hsm_clock, HSM_MIN_CLOCK_FREQ);
2608 * We need to have the device powered up at this point to call
2609 * our reset hook and for the CEC init.
2611 ret = vc4_hdmi_runtime_resume(dev);
2615 pm_runtime_get_noresume(dev);
2616 pm_runtime_set_active(dev);
2617 pm_runtime_enable(dev);
2619 if (vc4_hdmi->variant->reset)
2620 vc4_hdmi->variant->reset(vc4_hdmi);
2622 if ((of_device_is_compatible(dev->of_node, "brcm,bcm2711-hdmi0") ||
2623 of_device_is_compatible(dev->of_node, "brcm,bcm2711-hdmi1")) &&
2624 HDMI_READ(HDMI_VID_CTL) & VC4_HD_VID_CTL_ENABLE) {
2625 clk_prepare_enable(vc4_hdmi->pixel_clock);
2626 clk_prepare_enable(vc4_hdmi->hsm_clock);
2627 clk_prepare_enable(vc4_hdmi->pixel_bvb_clock);
2630 drm_simple_encoder_init(drm, encoder, DRM_MODE_ENCODER_TMDS);
2631 drm_encoder_helper_add(encoder, &vc4_hdmi_encoder_helper_funcs);
2633 ret = vc4_hdmi_connector_init(drm, vc4_hdmi);
2635 goto err_destroy_encoder;
2637 ret = vc4_hdmi_hotplug_init(vc4_hdmi);
2639 goto err_destroy_conn;
2641 ret = vc4_hdmi_cec_init(vc4_hdmi);
2643 goto err_free_hotplug;
2645 ret = vc4_hdmi_audio_init(vc4_hdmi);
2649 vc4_debugfs_add_file(drm, variant->debugfs_name,
2650 vc4_hdmi_debugfs_regs,
2653 pm_runtime_put_sync(dev);
2658 vc4_hdmi_cec_exit(vc4_hdmi);
2660 vc4_hdmi_hotplug_exit(vc4_hdmi);
2662 vc4_hdmi_connector_destroy(&vc4_hdmi->connector);
2663 err_destroy_encoder:
2664 drm_encoder_cleanup(encoder);
2665 pm_runtime_put_sync(dev);
2666 pm_runtime_disable(dev);
2668 put_device(&vc4_hdmi->ddc->dev);
2673 static void vc4_hdmi_unbind(struct device *dev, struct device *master,
2676 struct vc4_hdmi *vc4_hdmi;
2679 * ASoC makes it a bit hard to retrieve a pointer to the
2680 * vc4_hdmi structure. Registering the card will overwrite our
2681 * device drvdata with a pointer to the snd_soc_card structure,
2682 * which can then be used to retrieve whatever drvdata we want
2685 * However, that doesn't fly in the case where we wouldn't
2686 * register an ASoC card (because of an old DT that is missing
2687 * the dmas properties for example), then the card isn't
2688 * registered and the device drvdata wouldn't be set.
2690 * We can deal with both cases by making sure a snd_soc_card
2691 * pointer and a vc4_hdmi structure are pointing to the same
2692 * memory address, so we can treat them indistinctly without any
2695 BUILD_BUG_ON(offsetof(struct vc4_hdmi_audio, card) != 0);
2696 BUILD_BUG_ON(offsetof(struct vc4_hdmi, audio) != 0);
2697 vc4_hdmi = dev_get_drvdata(dev);
2699 kfree(vc4_hdmi->hdmi_regset.regs);
2700 kfree(vc4_hdmi->hd_regset.regs);
2702 vc4_hdmi_cec_exit(vc4_hdmi);
2703 vc4_hdmi_hotplug_exit(vc4_hdmi);
2704 vc4_hdmi_connector_destroy(&vc4_hdmi->connector);
2705 drm_encoder_cleanup(&vc4_hdmi->encoder.base.base);
2707 pm_runtime_disable(dev);
2709 put_device(&vc4_hdmi->ddc->dev);
2712 static const struct component_ops vc4_hdmi_ops = {
2713 .bind = vc4_hdmi_bind,
2714 .unbind = vc4_hdmi_unbind,
2717 static int vc4_hdmi_dev_probe(struct platform_device *pdev)
2719 return component_add(&pdev->dev, &vc4_hdmi_ops);
2722 static int vc4_hdmi_dev_remove(struct platform_device *pdev)
2724 component_del(&pdev->dev, &vc4_hdmi_ops);
2728 static const struct vc4_hdmi_variant bcm2835_variant = {
2729 .encoder_type = VC4_ENCODER_TYPE_HDMI0,
2730 .debugfs_name = "hdmi_regs",
2731 .card_name = "vc4-hdmi",
2732 .max_pixel_clock = 162000000,
2733 .registers = vc4_hdmi_fields,
2734 .num_registers = ARRAY_SIZE(vc4_hdmi_fields),
2736 .init_resources = vc4_hdmi_init_resources,
2737 .csc_setup = vc4_hdmi_csc_setup,
2738 .reset = vc4_hdmi_reset,
2739 .set_timings = vc4_hdmi_set_timings,
2740 .phy_init = vc4_hdmi_phy_init,
2741 .phy_disable = vc4_hdmi_phy_disable,
2742 .phy_rng_enable = vc4_hdmi_phy_rng_enable,
2743 .phy_rng_disable = vc4_hdmi_phy_rng_disable,
2744 .channel_map = vc4_hdmi_channel_map,
2745 .supports_hdr = false,
2748 static const struct vc4_hdmi_variant bcm2711_hdmi0_variant = {
2749 .encoder_type = VC4_ENCODER_TYPE_HDMI0,
2750 .debugfs_name = "hdmi0_regs",
2751 .card_name = "vc4-hdmi-0",
2752 .max_pixel_clock = 600000000,
2753 .registers = vc5_hdmi_hdmi0_fields,
2754 .num_registers = ARRAY_SIZE(vc5_hdmi_hdmi0_fields),
2755 .phy_lane_mapping = {
2761 .unsupported_odd_h_timings = true,
2762 .external_irq_controller = true,
2764 .init_resources = vc5_hdmi_init_resources,
2765 .csc_setup = vc5_hdmi_csc_setup,
2766 .reset = vc5_hdmi_reset,
2767 .set_timings = vc5_hdmi_set_timings,
2768 .phy_init = vc5_hdmi_phy_init,
2769 .phy_disable = vc5_hdmi_phy_disable,
2770 .phy_rng_enable = vc5_hdmi_phy_rng_enable,
2771 .phy_rng_disable = vc5_hdmi_phy_rng_disable,
2772 .channel_map = vc5_hdmi_channel_map,
2773 .supports_hdr = true,
2776 static const struct vc4_hdmi_variant bcm2711_hdmi1_variant = {
2777 .encoder_type = VC4_ENCODER_TYPE_HDMI1,
2778 .debugfs_name = "hdmi1_regs",
2779 .card_name = "vc4-hdmi-1",
2780 .max_pixel_clock = HDMI_14_MAX_TMDS_CLK,
2781 .registers = vc5_hdmi_hdmi1_fields,
2782 .num_registers = ARRAY_SIZE(vc5_hdmi_hdmi1_fields),
2783 .phy_lane_mapping = {
2789 .unsupported_odd_h_timings = true,
2790 .external_irq_controller = true,
2792 .init_resources = vc5_hdmi_init_resources,
2793 .csc_setup = vc5_hdmi_csc_setup,
2794 .reset = vc5_hdmi_reset,
2795 .set_timings = vc5_hdmi_set_timings,
2796 .phy_init = vc5_hdmi_phy_init,
2797 .phy_disable = vc5_hdmi_phy_disable,
2798 .phy_rng_enable = vc5_hdmi_phy_rng_enable,
2799 .phy_rng_disable = vc5_hdmi_phy_rng_disable,
2800 .channel_map = vc5_hdmi_channel_map,
2801 .supports_hdr = true,
2804 static const struct of_device_id vc4_hdmi_dt_match[] = {
2805 { .compatible = "brcm,bcm2835-hdmi", .data = &bcm2835_variant },
2806 { .compatible = "brcm,bcm2711-hdmi0", .data = &bcm2711_hdmi0_variant },
2807 { .compatible = "brcm,bcm2711-hdmi1", .data = &bcm2711_hdmi1_variant },
2811 static const struct dev_pm_ops vc4_hdmi_pm_ops = {
2812 SET_RUNTIME_PM_OPS(vc4_hdmi_runtime_suspend,
2813 vc4_hdmi_runtime_resume,
2817 struct platform_driver vc4_hdmi_driver = {
2818 .probe = vc4_hdmi_dev_probe,
2819 .remove = vc4_hdmi_dev_remove,
2822 .of_match_table = vc4_hdmi_dt_match,
2823 .pm = &vc4_hdmi_pm_ops,