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/display/drm_hdmi_helper.h>
35 #include <drm/display/drm_scdc_helper.h>
36 #include <drm/drm_atomic_helper.h>
37 #include <drm/drm_drv.h>
38 #include <drm/drm_probe_helper.h>
39 #include <drm/drm_simple_kms_helper.h>
40 #include <linux/clk.h>
41 #include <linux/component.h>
42 #include <linux/gpio/consumer.h>
43 #include <linux/i2c.h>
44 #include <linux/of_address.h>
45 #include <linux/of_gpio.h>
46 #include <linux/of_platform.h>
47 #include <linux/pm_runtime.h>
48 #include <linux/rational.h>
49 #include <linux/reset.h>
50 #include <sound/dmaengine_pcm.h>
51 #include <sound/hdmi-codec.h>
52 #include <sound/pcm_drm_eld.h>
53 #include <sound/pcm_params.h>
54 #include <sound/soc.h>
55 #include "media/cec.h"
58 #include "vc4_hdmi_regs.h"
61 #define VC5_HDMI_HORZA_HFP_SHIFT 16
62 #define VC5_HDMI_HORZA_HFP_MASK VC4_MASK(28, 16)
63 #define VC5_HDMI_HORZA_VPOS BIT(15)
64 #define VC5_HDMI_HORZA_HPOS BIT(14)
65 #define VC5_HDMI_HORZA_HAP_SHIFT 0
66 #define VC5_HDMI_HORZA_HAP_MASK VC4_MASK(13, 0)
68 #define VC5_HDMI_HORZB_HBP_SHIFT 16
69 #define VC5_HDMI_HORZB_HBP_MASK VC4_MASK(26, 16)
70 #define VC5_HDMI_HORZB_HSP_SHIFT 0
71 #define VC5_HDMI_HORZB_HSP_MASK VC4_MASK(10, 0)
73 #define VC5_HDMI_VERTA_VSP_SHIFT 24
74 #define VC5_HDMI_VERTA_VSP_MASK VC4_MASK(28, 24)
75 #define VC5_HDMI_VERTA_VFP_SHIFT 16
76 #define VC5_HDMI_VERTA_VFP_MASK VC4_MASK(22, 16)
77 #define VC5_HDMI_VERTA_VAL_SHIFT 0
78 #define VC5_HDMI_VERTA_VAL_MASK VC4_MASK(12, 0)
80 #define VC5_HDMI_VERTB_VSPO_SHIFT 16
81 #define VC5_HDMI_VERTB_VSPO_MASK VC4_MASK(29, 16)
83 #define VC4_HDMI_MISC_CONTROL_PIXEL_REP_SHIFT 0
84 #define VC4_HDMI_MISC_CONTROL_PIXEL_REP_MASK VC4_MASK(3, 0)
85 #define VC5_HDMI_MISC_CONTROL_PIXEL_REP_SHIFT 0
86 #define VC5_HDMI_MISC_CONTROL_PIXEL_REP_MASK VC4_MASK(3, 0)
88 #define VC5_HDMI_SCRAMBLER_CTL_ENABLE BIT(0)
90 #define VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE_SHIFT 8
91 #define VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE_MASK VC4_MASK(10, 8)
93 #define VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH_SHIFT 0
94 #define VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH_MASK VC4_MASK(3, 0)
96 #define VC5_HDMI_GCP_CONFIG_GCP_ENABLE BIT(31)
98 #define VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1_SHIFT 8
99 #define VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1_MASK VC4_MASK(15, 8)
101 # define VC4_HD_M_SW_RST BIT(2)
102 # define VC4_HD_M_ENABLE BIT(0)
104 #define HSM_MIN_CLOCK_FREQ 120000000
105 #define CEC_CLOCK_FREQ 40000
107 #define HDMI_14_MAX_TMDS_CLK (340 * 1000 * 1000)
109 static const char * const output_format_str[] = {
110 [VC4_HDMI_OUTPUT_RGB] = "RGB",
111 [VC4_HDMI_OUTPUT_YUV420] = "YUV 4:2:0",
112 [VC4_HDMI_OUTPUT_YUV422] = "YUV 4:2:2",
113 [VC4_HDMI_OUTPUT_YUV444] = "YUV 4:4:4",
116 static const char *vc4_hdmi_output_fmt_str(enum vc4_hdmi_output_format fmt)
118 if (fmt >= ARRAY_SIZE(output_format_str))
121 return output_format_str[fmt];
124 static unsigned long long
125 vc4_hdmi_encoder_compute_mode_clock(const struct drm_display_mode *mode,
126 unsigned int bpc, enum vc4_hdmi_output_format fmt);
128 static bool vc4_hdmi_mode_needs_scrambling(const struct drm_display_mode *mode,
130 enum vc4_hdmi_output_format fmt)
132 unsigned long long clock = vc4_hdmi_encoder_compute_mode_clock(mode, bpc, fmt);
134 return clock > HDMI_14_MAX_TMDS_CLK;
137 static bool vc4_hdmi_is_full_range_rgb(struct vc4_hdmi *vc4_hdmi,
138 const struct drm_display_mode *mode)
140 struct drm_display_info *display = &vc4_hdmi->connector.display_info;
142 return !display->is_hdmi ||
143 drm_default_rgb_quant_range(mode) == HDMI_QUANTIZATION_RANGE_FULL;
146 static int vc4_hdmi_debugfs_regs(struct seq_file *m, void *unused)
148 struct drm_info_node *node = (struct drm_info_node *)m->private;
149 struct vc4_hdmi *vc4_hdmi = node->info_ent->data;
150 struct drm_device *drm = vc4_hdmi->connector.dev;
151 struct drm_printer p = drm_seq_file_printer(m);
154 if (!drm_dev_enter(drm, &idx))
157 drm_print_regset32(&p, &vc4_hdmi->hdmi_regset);
158 drm_print_regset32(&p, &vc4_hdmi->hd_regset);
159 drm_print_regset32(&p, &vc4_hdmi->cec_regset);
160 drm_print_regset32(&p, &vc4_hdmi->csc_regset);
161 drm_print_regset32(&p, &vc4_hdmi->dvp_regset);
162 drm_print_regset32(&p, &vc4_hdmi->phy_regset);
163 drm_print_regset32(&p, &vc4_hdmi->ram_regset);
164 drm_print_regset32(&p, &vc4_hdmi->rm_regset);
171 static void vc4_hdmi_reset(struct vc4_hdmi *vc4_hdmi)
173 struct drm_device *drm = vc4_hdmi->connector.dev;
178 * We can be called by our bind callback, when the
179 * connector->dev pointer might not be initialised yet.
181 if (drm && !drm_dev_enter(drm, &idx))
184 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
186 HDMI_WRITE(HDMI_M_CTL, VC4_HD_M_SW_RST);
188 HDMI_WRITE(HDMI_M_CTL, 0);
190 HDMI_WRITE(HDMI_M_CTL, VC4_HD_M_ENABLE);
192 HDMI_WRITE(HDMI_SW_RESET_CONTROL,
193 VC4_HDMI_SW_RESET_HDMI |
194 VC4_HDMI_SW_RESET_FORMAT_DETECT);
196 HDMI_WRITE(HDMI_SW_RESET_CONTROL, 0);
198 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
204 static void vc5_hdmi_reset(struct vc4_hdmi *vc4_hdmi)
206 struct drm_device *drm = vc4_hdmi->connector.dev;
211 * We can be called by our bind callback, when the
212 * connector->dev pointer might not be initialised yet.
214 if (drm && !drm_dev_enter(drm, &idx))
217 reset_control_reset(vc4_hdmi->reset);
219 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
221 HDMI_WRITE(HDMI_DVP_CTL, 0);
223 HDMI_WRITE(HDMI_CLOCK_STOP,
224 HDMI_READ(HDMI_CLOCK_STOP) | VC4_DVP_HT_CLOCK_STOP_PIXEL);
226 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
232 #ifdef CONFIG_DRM_VC4_HDMI_CEC
233 static void vc4_hdmi_cec_update_clk_div(struct vc4_hdmi *vc4_hdmi)
235 struct drm_device *drm = vc4_hdmi->connector.dev;
236 unsigned long cec_rate;
243 * This function is called by our runtime_resume implementation
244 * and thus at bind time, when we haven't registered our
245 * connector yet and thus don't have a pointer to the DRM
248 if (drm && !drm_dev_enter(drm, &idx))
251 cec_rate = clk_get_rate(vc4_hdmi->cec_clock);
253 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
255 value = HDMI_READ(HDMI_CEC_CNTRL_1);
256 value &= ~VC4_HDMI_CEC_DIV_CLK_CNT_MASK;
259 * Set the clock divider: the hsm_clock rate and this divider
260 * setting will give a 40 kHz CEC clock.
262 clk_cnt = cec_rate / CEC_CLOCK_FREQ;
263 value |= clk_cnt << VC4_HDMI_CEC_DIV_CLK_CNT_SHIFT;
264 HDMI_WRITE(HDMI_CEC_CNTRL_1, value);
266 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
272 static void vc4_hdmi_cec_update_clk_div(struct vc4_hdmi *vc4_hdmi) {}
275 static void vc4_hdmi_enable_scrambling(struct drm_encoder *encoder);
277 static enum drm_connector_status
278 vc4_hdmi_connector_detect(struct drm_connector *connector, bool force)
280 struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector);
281 bool connected = false;
283 mutex_lock(&vc4_hdmi->mutex);
285 WARN_ON(pm_runtime_resume_and_get(&vc4_hdmi->pdev->dev));
287 if (vc4_hdmi->hpd_gpio) {
288 if (gpiod_get_value_cansleep(vc4_hdmi->hpd_gpio))
291 if (vc4_hdmi->variant->hp_detect &&
292 vc4_hdmi->variant->hp_detect(vc4_hdmi))
297 if (connector->status != connector_status_connected) {
298 struct edid *edid = drm_get_edid(connector, vc4_hdmi->ddc);
301 cec_s_phys_addr_from_edid(vc4_hdmi->cec_adap, edid);
306 vc4_hdmi_enable_scrambling(&vc4_hdmi->encoder.base);
307 pm_runtime_put(&vc4_hdmi->pdev->dev);
308 mutex_unlock(&vc4_hdmi->mutex);
309 return connector_status_connected;
312 cec_phys_addr_invalidate(vc4_hdmi->cec_adap);
313 pm_runtime_put(&vc4_hdmi->pdev->dev);
314 mutex_unlock(&vc4_hdmi->mutex);
315 return connector_status_disconnected;
318 static int vc4_hdmi_connector_get_modes(struct drm_connector *connector)
320 struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector);
324 mutex_lock(&vc4_hdmi->mutex);
326 edid = drm_get_edid(connector, vc4_hdmi->ddc);
327 cec_s_phys_addr_from_edid(vc4_hdmi->cec_adap, edid);
333 drm_connector_update_edid_property(connector, edid);
334 ret = drm_add_edid_modes(connector, edid);
337 if (vc4_hdmi->disable_4kp60) {
338 struct drm_device *drm = connector->dev;
339 struct drm_display_mode *mode;
341 list_for_each_entry(mode, &connector->probed_modes, head) {
342 if (vc4_hdmi_mode_needs_scrambling(mode, 8, VC4_HDMI_OUTPUT_RGB)) {
343 drm_warn_once(drm, "The core clock cannot reach frequencies high enough to support 4k @ 60Hz.");
344 drm_warn_once(drm, "Please change your config.txt file to add hdmi_enable_4kp60.");
350 mutex_unlock(&vc4_hdmi->mutex);
355 static int vc4_hdmi_connector_atomic_check(struct drm_connector *connector,
356 struct drm_atomic_state *state)
358 struct drm_connector_state *old_state =
359 drm_atomic_get_old_connector_state(state, connector);
360 struct drm_connector_state *new_state =
361 drm_atomic_get_new_connector_state(state, connector);
362 struct drm_crtc *crtc = new_state->crtc;
367 if (old_state->colorspace != new_state->colorspace ||
368 !drm_connector_atomic_hdr_metadata_equal(old_state, new_state)) {
369 struct drm_crtc_state *crtc_state;
371 crtc_state = drm_atomic_get_crtc_state(state, crtc);
372 if (IS_ERR(crtc_state))
373 return PTR_ERR(crtc_state);
375 crtc_state->mode_changed = true;
381 static void vc4_hdmi_connector_reset(struct drm_connector *connector)
383 struct vc4_hdmi_connector_state *old_state =
384 conn_state_to_vc4_hdmi_conn_state(connector->state);
385 struct vc4_hdmi_connector_state *new_state =
386 kzalloc(sizeof(*new_state), GFP_KERNEL);
388 if (connector->state)
389 __drm_atomic_helper_connector_destroy_state(connector->state);
392 __drm_atomic_helper_connector_reset(connector, &new_state->base);
397 new_state->base.max_bpc = 8;
398 new_state->base.max_requested_bpc = 8;
399 new_state->output_format = VC4_HDMI_OUTPUT_RGB;
400 drm_atomic_helper_connector_tv_reset(connector);
403 static struct drm_connector_state *
404 vc4_hdmi_connector_duplicate_state(struct drm_connector *connector)
406 struct drm_connector_state *conn_state = connector->state;
407 struct vc4_hdmi_connector_state *vc4_state = conn_state_to_vc4_hdmi_conn_state(conn_state);
408 struct vc4_hdmi_connector_state *new_state;
410 new_state = kzalloc(sizeof(*new_state), GFP_KERNEL);
414 new_state->tmds_char_rate = vc4_state->tmds_char_rate;
415 new_state->output_bpc = vc4_state->output_bpc;
416 new_state->output_format = vc4_state->output_format;
417 __drm_atomic_helper_connector_duplicate_state(connector, &new_state->base);
419 return &new_state->base;
422 static const struct drm_connector_funcs vc4_hdmi_connector_funcs = {
423 .detect = vc4_hdmi_connector_detect,
424 .fill_modes = drm_helper_probe_single_connector_modes,
425 .reset = vc4_hdmi_connector_reset,
426 .atomic_duplicate_state = vc4_hdmi_connector_duplicate_state,
427 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
430 static const struct drm_connector_helper_funcs vc4_hdmi_connector_helper_funcs = {
431 .get_modes = vc4_hdmi_connector_get_modes,
432 .atomic_check = vc4_hdmi_connector_atomic_check,
435 static int vc4_hdmi_connector_init(struct drm_device *dev,
436 struct vc4_hdmi *vc4_hdmi)
438 struct drm_connector *connector = &vc4_hdmi->connector;
439 struct drm_encoder *encoder = &vc4_hdmi->encoder.base;
442 ret = drmm_connector_init(dev, connector,
443 &vc4_hdmi_connector_funcs,
444 DRM_MODE_CONNECTOR_HDMIA,
449 drm_connector_helper_add(connector, &vc4_hdmi_connector_helper_funcs);
452 * Some of the properties below require access to state, like bpc.
453 * Allocate some default initial connector state with our reset helper.
455 if (connector->funcs->reset)
456 connector->funcs->reset(connector);
458 /* Create and attach TV margin props to this connector. */
459 ret = drm_mode_create_tv_margin_properties(dev);
463 ret = drm_mode_create_hdmi_colorspace_property(connector);
467 drm_connector_attach_colorspace_property(connector);
468 drm_connector_attach_tv_margin_properties(connector);
469 drm_connector_attach_max_bpc_property(connector, 8, 12);
471 connector->polled = (DRM_CONNECTOR_POLL_CONNECT |
472 DRM_CONNECTOR_POLL_DISCONNECT);
474 connector->interlace_allowed = 1;
475 connector->doublescan_allowed = 0;
476 connector->stereo_allowed = 1;
478 if (vc4_hdmi->variant->supports_hdr)
479 drm_connector_attach_hdr_output_metadata_property(connector);
481 drm_connector_attach_encoder(connector, encoder);
486 static int vc4_hdmi_stop_packet(struct drm_encoder *encoder,
487 enum hdmi_infoframe_type type,
490 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
491 struct drm_device *drm = vc4_hdmi->connector.dev;
492 u32 packet_id = type - 0x80;
497 if (!drm_dev_enter(drm, &idx))
500 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
501 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
502 HDMI_READ(HDMI_RAM_PACKET_CONFIG) & ~BIT(packet_id));
503 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
506 ret = wait_for(!(HDMI_READ(HDMI_RAM_PACKET_STATUS) &
507 BIT(packet_id)), 100);
514 static void vc4_hdmi_write_infoframe(struct drm_encoder *encoder,
515 union hdmi_infoframe *frame)
517 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
518 struct drm_device *drm = vc4_hdmi->connector.dev;
519 u32 packet_id = frame->any.type - 0x80;
520 const struct vc4_hdmi_register *ram_packet_start =
521 &vc4_hdmi->variant->registers[HDMI_RAM_PACKET_START];
522 u32 packet_reg = ram_packet_start->offset + VC4_HDMI_PACKET_STRIDE * packet_id;
523 u32 packet_reg_next = ram_packet_start->offset +
524 VC4_HDMI_PACKET_STRIDE * (packet_id + 1);
525 void __iomem *base = __vc4_hdmi_get_field_base(vc4_hdmi,
526 ram_packet_start->reg);
527 uint8_t buffer[VC4_HDMI_PACKET_STRIDE] = {};
533 if (!drm_dev_enter(drm, &idx))
536 WARN_ONCE(!(HDMI_READ(HDMI_RAM_PACKET_CONFIG) &
537 VC4_HDMI_RAM_PACKET_ENABLE),
538 "Packet RAM has to be on to store the packet.");
540 len = hdmi_infoframe_pack(frame, buffer, sizeof(buffer));
544 ret = vc4_hdmi_stop_packet(encoder, frame->any.type, true);
546 DRM_ERROR("Failed to wait for infoframe to go idle: %d\n", ret);
550 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
552 for (i = 0; i < len; i += 7) {
553 writel(buffer[i + 0] << 0 |
559 writel(buffer[i + 3] << 0 |
561 buffer[i + 5] << 16 |
568 * clear remainder of packet ram as it's included in the
569 * infoframe and triggers a checksum error on hdmi analyser
571 for (; packet_reg < packet_reg_next; packet_reg += 4)
572 writel(0, base + packet_reg);
574 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
575 HDMI_READ(HDMI_RAM_PACKET_CONFIG) | BIT(packet_id));
577 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
579 ret = wait_for((HDMI_READ(HDMI_RAM_PACKET_STATUS) &
580 BIT(packet_id)), 100);
582 DRM_ERROR("Failed to wait for infoframe to start: %d\n", ret);
588 static void vc4_hdmi_avi_infoframe_colorspace(struct hdmi_avi_infoframe *frame,
589 enum vc4_hdmi_output_format fmt)
592 case VC4_HDMI_OUTPUT_RGB:
593 frame->colorspace = HDMI_COLORSPACE_RGB;
596 case VC4_HDMI_OUTPUT_YUV420:
597 frame->colorspace = HDMI_COLORSPACE_YUV420;
600 case VC4_HDMI_OUTPUT_YUV422:
601 frame->colorspace = HDMI_COLORSPACE_YUV422;
604 case VC4_HDMI_OUTPUT_YUV444:
605 frame->colorspace = HDMI_COLORSPACE_YUV444;
613 static void vc4_hdmi_set_avi_infoframe(struct drm_encoder *encoder)
615 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
616 struct drm_connector *connector = &vc4_hdmi->connector;
617 struct drm_connector_state *cstate = connector->state;
618 struct vc4_hdmi_connector_state *vc4_state =
619 conn_state_to_vc4_hdmi_conn_state(cstate);
620 const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
621 union hdmi_infoframe frame;
624 lockdep_assert_held(&vc4_hdmi->mutex);
626 ret = drm_hdmi_avi_infoframe_from_display_mode(&frame.avi,
629 DRM_ERROR("couldn't fill AVI infoframe\n");
633 drm_hdmi_avi_infoframe_quant_range(&frame.avi,
635 vc4_hdmi_is_full_range_rgb(vc4_hdmi, mode) ?
636 HDMI_QUANTIZATION_RANGE_FULL :
637 HDMI_QUANTIZATION_RANGE_LIMITED);
638 drm_hdmi_avi_infoframe_colorimetry(&frame.avi, cstate);
639 vc4_hdmi_avi_infoframe_colorspace(&frame.avi, vc4_state->output_format);
640 drm_hdmi_avi_infoframe_bars(&frame.avi, cstate);
642 vc4_hdmi_write_infoframe(encoder, &frame);
645 static void vc4_hdmi_set_spd_infoframe(struct drm_encoder *encoder)
647 union hdmi_infoframe frame;
650 ret = hdmi_spd_infoframe_init(&frame.spd, "Broadcom", "Videocore");
652 DRM_ERROR("couldn't fill SPD infoframe\n");
656 frame.spd.sdi = HDMI_SPD_SDI_PC;
658 vc4_hdmi_write_infoframe(encoder, &frame);
661 static void vc4_hdmi_set_audio_infoframe(struct drm_encoder *encoder)
663 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
664 struct hdmi_audio_infoframe *audio = &vc4_hdmi->audio.infoframe;
665 union hdmi_infoframe frame;
667 memcpy(&frame.audio, audio, sizeof(*audio));
669 if (vc4_hdmi->packet_ram_enabled)
670 vc4_hdmi_write_infoframe(encoder, &frame);
673 static void vc4_hdmi_set_hdr_infoframe(struct drm_encoder *encoder)
675 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
676 struct drm_connector *connector = &vc4_hdmi->connector;
677 struct drm_connector_state *conn_state = connector->state;
678 union hdmi_infoframe frame;
680 lockdep_assert_held(&vc4_hdmi->mutex);
682 if (!vc4_hdmi->variant->supports_hdr)
685 if (!conn_state->hdr_output_metadata)
688 if (drm_hdmi_infoframe_set_hdr_metadata(&frame.drm, conn_state))
691 vc4_hdmi_write_infoframe(encoder, &frame);
694 static void vc4_hdmi_set_infoframes(struct drm_encoder *encoder)
696 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
698 lockdep_assert_held(&vc4_hdmi->mutex);
700 vc4_hdmi_set_avi_infoframe(encoder);
701 vc4_hdmi_set_spd_infoframe(encoder);
703 * If audio was streaming, then we need to reenabled the audio
704 * infoframe here during encoder_enable.
706 if (vc4_hdmi->audio.streaming)
707 vc4_hdmi_set_audio_infoframe(encoder);
709 vc4_hdmi_set_hdr_infoframe(encoder);
712 static bool vc4_hdmi_supports_scrambling(struct drm_encoder *encoder,
713 struct drm_display_mode *mode)
715 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
716 struct drm_display_info *display = &vc4_hdmi->connector.display_info;
718 lockdep_assert_held(&vc4_hdmi->mutex);
720 if (!display->is_hdmi)
723 if (!display->hdmi.scdc.supported ||
724 !display->hdmi.scdc.scrambling.supported)
730 #define SCRAMBLING_POLLING_DELAY_MS 1000
732 static void vc4_hdmi_enable_scrambling(struct drm_encoder *encoder)
734 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
735 struct drm_device *drm = vc4_hdmi->connector.dev;
736 struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
740 lockdep_assert_held(&vc4_hdmi->mutex);
742 if (!vc4_hdmi_supports_scrambling(encoder, mode))
745 if (!vc4_hdmi_mode_needs_scrambling(mode,
746 vc4_hdmi->output_bpc,
747 vc4_hdmi->output_format))
750 if (!drm_dev_enter(drm, &idx))
753 drm_scdc_set_high_tmds_clock_ratio(vc4_hdmi->ddc, true);
754 drm_scdc_set_scrambling(vc4_hdmi->ddc, true);
756 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
757 HDMI_WRITE(HDMI_SCRAMBLER_CTL, HDMI_READ(HDMI_SCRAMBLER_CTL) |
758 VC5_HDMI_SCRAMBLER_CTL_ENABLE);
759 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
763 vc4_hdmi->scdc_enabled = true;
765 queue_delayed_work(system_wq, &vc4_hdmi->scrambling_work,
766 msecs_to_jiffies(SCRAMBLING_POLLING_DELAY_MS));
769 static void vc4_hdmi_disable_scrambling(struct drm_encoder *encoder)
771 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
772 struct drm_device *drm = vc4_hdmi->connector.dev;
776 lockdep_assert_held(&vc4_hdmi->mutex);
778 if (!vc4_hdmi->scdc_enabled)
781 vc4_hdmi->scdc_enabled = false;
783 if (delayed_work_pending(&vc4_hdmi->scrambling_work))
784 cancel_delayed_work_sync(&vc4_hdmi->scrambling_work);
786 if (!drm_dev_enter(drm, &idx))
789 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
790 HDMI_WRITE(HDMI_SCRAMBLER_CTL, HDMI_READ(HDMI_SCRAMBLER_CTL) &
791 ~VC5_HDMI_SCRAMBLER_CTL_ENABLE);
792 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
794 drm_scdc_set_scrambling(vc4_hdmi->ddc, false);
795 drm_scdc_set_high_tmds_clock_ratio(vc4_hdmi->ddc, false);
800 static void vc4_hdmi_scrambling_wq(struct work_struct *work)
802 struct vc4_hdmi *vc4_hdmi = container_of(to_delayed_work(work),
806 if (drm_scdc_get_scrambling_status(vc4_hdmi->ddc))
809 drm_scdc_set_high_tmds_clock_ratio(vc4_hdmi->ddc, true);
810 drm_scdc_set_scrambling(vc4_hdmi->ddc, true);
812 queue_delayed_work(system_wq, &vc4_hdmi->scrambling_work,
813 msecs_to_jiffies(SCRAMBLING_POLLING_DELAY_MS));
816 static void vc4_hdmi_encoder_post_crtc_disable(struct drm_encoder *encoder,
817 struct drm_atomic_state *state)
819 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
820 struct drm_device *drm = vc4_hdmi->connector.dev;
824 mutex_lock(&vc4_hdmi->mutex);
826 vc4_hdmi->packet_ram_enabled = false;
828 if (!drm_dev_enter(drm, &idx))
831 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
833 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG, 0);
835 HDMI_WRITE(HDMI_VID_CTL, HDMI_READ(HDMI_VID_CTL) | VC4_HD_VID_CTL_CLRRGB);
837 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
841 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
842 HDMI_WRITE(HDMI_VID_CTL,
843 HDMI_READ(HDMI_VID_CTL) & ~VC4_HD_VID_CTL_ENABLE);
844 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
846 vc4_hdmi_disable_scrambling(encoder);
851 mutex_unlock(&vc4_hdmi->mutex);
854 static void vc4_hdmi_encoder_post_crtc_powerdown(struct drm_encoder *encoder,
855 struct drm_atomic_state *state)
857 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
858 struct drm_device *drm = vc4_hdmi->connector.dev;
863 mutex_lock(&vc4_hdmi->mutex);
865 if (!drm_dev_enter(drm, &idx))
868 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
869 HDMI_WRITE(HDMI_VID_CTL,
870 HDMI_READ(HDMI_VID_CTL) | VC4_HD_VID_CTL_BLANKPIX);
871 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
873 if (vc4_hdmi->variant->phy_disable)
874 vc4_hdmi->variant->phy_disable(vc4_hdmi);
876 clk_disable_unprepare(vc4_hdmi->pixel_bvb_clock);
877 clk_disable_unprepare(vc4_hdmi->pixel_clock);
879 ret = pm_runtime_put(&vc4_hdmi->pdev->dev);
881 DRM_ERROR("Failed to release power domain: %d\n", ret);
886 mutex_unlock(&vc4_hdmi->mutex);
889 static void vc4_hdmi_csc_setup(struct vc4_hdmi *vc4_hdmi,
890 struct drm_connector_state *state,
891 const struct drm_display_mode *mode)
893 struct drm_device *drm = vc4_hdmi->connector.dev;
898 if (!drm_dev_enter(drm, &idx))
901 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
903 csc_ctl = VC4_SET_FIELD(VC4_HD_CSC_CTL_ORDER_BGR,
904 VC4_HD_CSC_CTL_ORDER);
906 if (!vc4_hdmi_is_full_range_rgb(vc4_hdmi, mode)) {
907 /* CEA VICs other than #1 requre limited range RGB
908 * output unless overridden by an AVI infoframe.
909 * Apply a colorspace conversion to squash 0-255 down
910 * to 16-235. The matrix here is:
917 csc_ctl |= VC4_HD_CSC_CTL_ENABLE;
918 csc_ctl |= VC4_HD_CSC_CTL_RGB2YCC;
919 csc_ctl |= VC4_SET_FIELD(VC4_HD_CSC_CTL_MODE_CUSTOM,
920 VC4_HD_CSC_CTL_MODE);
922 HDMI_WRITE(HDMI_CSC_12_11, (0x000 << 16) | 0x000);
923 HDMI_WRITE(HDMI_CSC_14_13, (0x100 << 16) | 0x6e0);
924 HDMI_WRITE(HDMI_CSC_22_21, (0x6e0 << 16) | 0x000);
925 HDMI_WRITE(HDMI_CSC_24_23, (0x100 << 16) | 0x000);
926 HDMI_WRITE(HDMI_CSC_32_31, (0x000 << 16) | 0x6e0);
927 HDMI_WRITE(HDMI_CSC_34_33, (0x100 << 16) | 0x000);
930 /* The RGB order applies even when CSC is disabled. */
931 HDMI_WRITE(HDMI_CSC_CTL, csc_ctl);
933 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
939 * If we need to output Full Range RGB, then use the unity matrix
945 * Matrix is signed 2p13 fixed point, with signed 9p6 offsets
947 static const u16 vc5_hdmi_csc_full_rgb_unity[3][4] = {
948 { 0x2000, 0x0000, 0x0000, 0x0000 },
949 { 0x0000, 0x2000, 0x0000, 0x0000 },
950 { 0x0000, 0x0000, 0x2000, 0x0000 },
954 * CEA VICs other than #1 require limited range RGB output unless
955 * overridden by an AVI infoframe. Apply a colorspace conversion to
956 * squash 0-255 down to 16-235. The matrix here is:
962 * Matrix is signed 2p13 fixed point, with signed 9p6 offsets
964 static const u16 vc5_hdmi_csc_full_rgb_to_limited_rgb[3][4] = {
965 { 0x1b80, 0x0000, 0x0000, 0x0400 },
966 { 0x0000, 0x1b80, 0x0000, 0x0400 },
967 { 0x0000, 0x0000, 0x1b80, 0x0400 },
971 * Conversion between Full Range RGB and Full Range YUV422 using the
975 * [ 0.181906 0.611804 0.061758 16 ]
976 * [ -0.100268 -0.337232 0.437500 128 ]
977 * [ 0.437500 -0.397386 -0.040114 128 ]
979 * Matrix is signed 2p13 fixed point, with signed 9p6 offsets
981 static const u16 vc5_hdmi_csc_full_rgb_to_limited_yuv422_bt709[3][4] = {
982 { 0x05d2, 0x1394, 0x01fa, 0x0400 },
983 { 0xfccc, 0xf536, 0x0e00, 0x2000 },
984 { 0x0e00, 0xf34a, 0xfeb8, 0x2000 },
988 * Conversion between Full Range RGB and Full Range YUV444 using the
991 * [ -0.100268 -0.337232 0.437500 128 ]
992 * [ 0.437500 -0.397386 -0.040114 128 ]
993 * [ 0.181906 0.611804 0.061758 16 ]
995 * Matrix is signed 2p13 fixed point, with signed 9p6 offsets
997 static const u16 vc5_hdmi_csc_full_rgb_to_limited_yuv444_bt709[3][4] = {
998 { 0xfccc, 0xf536, 0x0e00, 0x2000 },
999 { 0x0e00, 0xf34a, 0xfeb8, 0x2000 },
1000 { 0x05d2, 0x1394, 0x01fa, 0x0400 },
1003 static void vc5_hdmi_set_csc_coeffs(struct vc4_hdmi *vc4_hdmi,
1004 const u16 coeffs[3][4])
1006 lockdep_assert_held(&vc4_hdmi->hw_lock);
1008 HDMI_WRITE(HDMI_CSC_12_11, (coeffs[0][1] << 16) | coeffs[0][0]);
1009 HDMI_WRITE(HDMI_CSC_14_13, (coeffs[0][3] << 16) | coeffs[0][2]);
1010 HDMI_WRITE(HDMI_CSC_22_21, (coeffs[1][1] << 16) | coeffs[1][0]);
1011 HDMI_WRITE(HDMI_CSC_24_23, (coeffs[1][3] << 16) | coeffs[1][2]);
1012 HDMI_WRITE(HDMI_CSC_32_31, (coeffs[2][1] << 16) | coeffs[2][0]);
1013 HDMI_WRITE(HDMI_CSC_34_33, (coeffs[2][3] << 16) | coeffs[2][2]);
1016 static void vc5_hdmi_csc_setup(struct vc4_hdmi *vc4_hdmi,
1017 struct drm_connector_state *state,
1018 const struct drm_display_mode *mode)
1020 struct drm_device *drm = vc4_hdmi->connector.dev;
1021 struct vc4_hdmi_connector_state *vc4_state =
1022 conn_state_to_vc4_hdmi_conn_state(state);
1023 unsigned long flags;
1025 u32 if_xbar = 0x543210;
1026 u32 csc_chan_ctl = 0;
1027 u32 csc_ctl = VC5_MT_CP_CSC_CTL_ENABLE | VC4_SET_FIELD(VC4_HD_CSC_CTL_MODE_CUSTOM,
1028 VC5_MT_CP_CSC_CTL_MODE);
1031 if (!drm_dev_enter(drm, &idx))
1034 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1036 switch (vc4_state->output_format) {
1037 case VC4_HDMI_OUTPUT_YUV444:
1038 vc5_hdmi_set_csc_coeffs(vc4_hdmi, vc5_hdmi_csc_full_rgb_to_limited_yuv444_bt709);
1041 case VC4_HDMI_OUTPUT_YUV422:
1042 csc_ctl |= VC4_SET_FIELD(VC5_MT_CP_CSC_CTL_FILTER_MODE_444_TO_422_STANDARD,
1043 VC5_MT_CP_CSC_CTL_FILTER_MODE_444_TO_422) |
1044 VC5_MT_CP_CSC_CTL_USE_444_TO_422 |
1045 VC5_MT_CP_CSC_CTL_USE_RNG_SUPPRESSION;
1047 csc_chan_ctl |= VC4_SET_FIELD(VC5_MT_CP_CHANNEL_CTL_OUTPUT_REMAP_LEGACY_STYLE,
1048 VC5_MT_CP_CHANNEL_CTL_OUTPUT_REMAP);
1050 if_cfg |= VC4_SET_FIELD(VC5_DVP_HT_VEC_INTERFACE_CFG_SEL_422_FORMAT_422_LEGACY,
1051 VC5_DVP_HT_VEC_INTERFACE_CFG_SEL_422);
1053 vc5_hdmi_set_csc_coeffs(vc4_hdmi, vc5_hdmi_csc_full_rgb_to_limited_yuv422_bt709);
1056 case VC4_HDMI_OUTPUT_RGB:
1059 if (!vc4_hdmi_is_full_range_rgb(vc4_hdmi, mode))
1060 vc5_hdmi_set_csc_coeffs(vc4_hdmi, vc5_hdmi_csc_full_rgb_to_limited_rgb);
1062 vc5_hdmi_set_csc_coeffs(vc4_hdmi, vc5_hdmi_csc_full_rgb_unity);
1069 HDMI_WRITE(HDMI_VEC_INTERFACE_CFG, if_cfg);
1070 HDMI_WRITE(HDMI_VEC_INTERFACE_XBAR, if_xbar);
1071 HDMI_WRITE(HDMI_CSC_CHANNEL_CTL, csc_chan_ctl);
1072 HDMI_WRITE(HDMI_CSC_CTL, csc_ctl);
1074 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1079 static void vc4_hdmi_set_timings(struct vc4_hdmi *vc4_hdmi,
1080 struct drm_connector_state *state,
1081 struct drm_display_mode *mode)
1083 struct drm_device *drm = vc4_hdmi->connector.dev;
1084 bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
1085 bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
1086 bool interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE;
1087 u32 pixel_rep = (mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1;
1088 u32 verta = (VC4_SET_FIELD(mode->crtc_vsync_end - mode->crtc_vsync_start,
1089 VC4_HDMI_VERTA_VSP) |
1090 VC4_SET_FIELD(mode->crtc_vsync_start - mode->crtc_vdisplay,
1091 VC4_HDMI_VERTA_VFP) |
1092 VC4_SET_FIELD(mode->crtc_vdisplay, VC4_HDMI_VERTA_VAL));
1093 u32 vertb = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) |
1094 VC4_SET_FIELD(mode->crtc_vtotal - mode->crtc_vsync_end +
1096 VC4_HDMI_VERTB_VBP));
1097 u32 vertb_even = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) |
1098 VC4_SET_FIELD(mode->crtc_vtotal -
1099 mode->crtc_vsync_end,
1100 VC4_HDMI_VERTB_VBP));
1101 unsigned long flags;
1105 if (!drm_dev_enter(drm, &idx))
1108 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1110 HDMI_WRITE(HDMI_HORZA,
1111 (vsync_pos ? VC4_HDMI_HORZA_VPOS : 0) |
1112 (hsync_pos ? VC4_HDMI_HORZA_HPOS : 0) |
1113 VC4_SET_FIELD(mode->hdisplay * pixel_rep,
1114 VC4_HDMI_HORZA_HAP));
1116 HDMI_WRITE(HDMI_HORZB,
1117 VC4_SET_FIELD((mode->htotal -
1118 mode->hsync_end) * pixel_rep,
1119 VC4_HDMI_HORZB_HBP) |
1120 VC4_SET_FIELD((mode->hsync_end -
1121 mode->hsync_start) * pixel_rep,
1122 VC4_HDMI_HORZB_HSP) |
1123 VC4_SET_FIELD((mode->hsync_start -
1124 mode->hdisplay) * pixel_rep,
1125 VC4_HDMI_HORZB_HFP));
1127 HDMI_WRITE(HDMI_VERTA0, verta);
1128 HDMI_WRITE(HDMI_VERTA1, verta);
1130 HDMI_WRITE(HDMI_VERTB0, vertb_even);
1131 HDMI_WRITE(HDMI_VERTB1, vertb);
1133 reg = HDMI_READ(HDMI_MISC_CONTROL);
1134 reg &= ~VC4_HDMI_MISC_CONTROL_PIXEL_REP_MASK;
1135 reg |= VC4_SET_FIELD(pixel_rep - 1, VC4_HDMI_MISC_CONTROL_PIXEL_REP);
1136 HDMI_WRITE(HDMI_MISC_CONTROL, reg);
1138 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1143 static void vc5_hdmi_set_timings(struct vc4_hdmi *vc4_hdmi,
1144 struct drm_connector_state *state,
1145 struct drm_display_mode *mode)
1147 struct drm_device *drm = vc4_hdmi->connector.dev;
1148 const struct vc4_hdmi_connector_state *vc4_state =
1149 conn_state_to_vc4_hdmi_conn_state(state);
1150 bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
1151 bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
1152 bool interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE;
1153 u32 pixel_rep = (mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1;
1154 u32 verta = (VC4_SET_FIELD(mode->crtc_vsync_end - mode->crtc_vsync_start,
1155 VC5_HDMI_VERTA_VSP) |
1156 VC4_SET_FIELD(mode->crtc_vsync_start - mode->crtc_vdisplay,
1157 VC5_HDMI_VERTA_VFP) |
1158 VC4_SET_FIELD(mode->crtc_vdisplay, VC5_HDMI_VERTA_VAL));
1159 u32 vertb = (VC4_SET_FIELD(mode->htotal >> (2 - pixel_rep),
1160 VC5_HDMI_VERTB_VSPO) |
1161 VC4_SET_FIELD(mode->crtc_vtotal - mode->crtc_vsync_end,
1162 VC4_HDMI_VERTB_VBP));
1163 u32 vertb_even = (VC4_SET_FIELD(0, VC5_HDMI_VERTB_VSPO) |
1164 VC4_SET_FIELD(mode->crtc_vtotal -
1165 mode->crtc_vsync_end - interlaced,
1166 VC4_HDMI_VERTB_VBP));
1167 unsigned long flags;
1173 if (!drm_dev_enter(drm, &idx))
1176 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1178 HDMI_WRITE(HDMI_HORZA,
1179 (vsync_pos ? VC5_HDMI_HORZA_VPOS : 0) |
1180 (hsync_pos ? VC5_HDMI_HORZA_HPOS : 0) |
1181 VC4_SET_FIELD(mode->hdisplay * pixel_rep,
1182 VC5_HDMI_HORZA_HAP) |
1183 VC4_SET_FIELD((mode->hsync_start -
1184 mode->hdisplay) * pixel_rep,
1185 VC5_HDMI_HORZA_HFP));
1187 HDMI_WRITE(HDMI_HORZB,
1188 VC4_SET_FIELD((mode->htotal -
1189 mode->hsync_end) * pixel_rep,
1190 VC5_HDMI_HORZB_HBP) |
1191 VC4_SET_FIELD((mode->hsync_end -
1192 mode->hsync_start) * pixel_rep,
1193 VC5_HDMI_HORZB_HSP));
1195 HDMI_WRITE(HDMI_VERTA0, verta);
1196 HDMI_WRITE(HDMI_VERTA1, verta);
1198 HDMI_WRITE(HDMI_VERTB0, vertb_even);
1199 HDMI_WRITE(HDMI_VERTB1, vertb);
1201 switch (vc4_state->output_bpc) {
1218 * YCC422 is always 36-bit and not considered deep colour so
1219 * doesn't signal in GCP.
1221 if (vc4_state->output_format == VC4_HDMI_OUTPUT_YUV422) {
1226 reg = HDMI_READ(HDMI_DEEP_COLOR_CONFIG_1);
1227 reg &= ~(VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE_MASK |
1228 VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH_MASK);
1229 reg |= VC4_SET_FIELD(2, VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE) |
1230 VC4_SET_FIELD(gcp, VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH);
1231 HDMI_WRITE(HDMI_DEEP_COLOR_CONFIG_1, reg);
1233 reg = HDMI_READ(HDMI_GCP_WORD_1);
1234 reg &= ~VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1_MASK;
1235 reg |= VC4_SET_FIELD(gcp, VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1);
1236 HDMI_WRITE(HDMI_GCP_WORD_1, reg);
1238 reg = HDMI_READ(HDMI_GCP_CONFIG);
1239 reg &= ~VC5_HDMI_GCP_CONFIG_GCP_ENABLE;
1240 reg |= gcp_en ? VC5_HDMI_GCP_CONFIG_GCP_ENABLE : 0;
1241 HDMI_WRITE(HDMI_GCP_CONFIG, reg);
1243 reg = HDMI_READ(HDMI_MISC_CONTROL);
1244 reg &= ~VC5_HDMI_MISC_CONTROL_PIXEL_REP_MASK;
1245 reg |= VC4_SET_FIELD(pixel_rep - 1, VC5_HDMI_MISC_CONTROL_PIXEL_REP);
1246 HDMI_WRITE(HDMI_MISC_CONTROL, reg);
1248 HDMI_WRITE(HDMI_CLOCK_STOP, 0);
1250 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1255 static void vc4_hdmi_recenter_fifo(struct vc4_hdmi *vc4_hdmi)
1257 struct drm_device *drm = vc4_hdmi->connector.dev;
1258 unsigned long flags;
1263 if (!drm_dev_enter(drm, &idx))
1266 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1268 drift = HDMI_READ(HDMI_FIFO_CTL);
1269 drift &= VC4_HDMI_FIFO_VALID_WRITE_MASK;
1271 HDMI_WRITE(HDMI_FIFO_CTL,
1272 drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
1273 HDMI_WRITE(HDMI_FIFO_CTL,
1274 drift | VC4_HDMI_FIFO_CTL_RECENTER);
1276 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1278 usleep_range(1000, 1100);
1280 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1282 HDMI_WRITE(HDMI_FIFO_CTL,
1283 drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
1284 HDMI_WRITE(HDMI_FIFO_CTL,
1285 drift | VC4_HDMI_FIFO_CTL_RECENTER);
1287 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1289 ret = wait_for(HDMI_READ(HDMI_FIFO_CTL) &
1290 VC4_HDMI_FIFO_CTL_RECENTER_DONE, 1);
1291 WARN_ONCE(ret, "Timeout waiting for "
1292 "VC4_HDMI_FIFO_CTL_RECENTER_DONE");
1297 static void vc4_hdmi_encoder_pre_crtc_configure(struct drm_encoder *encoder,
1298 struct drm_atomic_state *state)
1300 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1301 struct drm_device *drm = vc4_hdmi->connector.dev;
1302 struct drm_connector *connector = &vc4_hdmi->connector;
1303 struct drm_connector_state *conn_state =
1304 drm_atomic_get_new_connector_state(state, connector);
1305 struct vc4_hdmi_connector_state *vc4_conn_state =
1306 conn_state_to_vc4_hdmi_conn_state(conn_state);
1307 struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
1308 unsigned long tmds_char_rate = vc4_conn_state->tmds_char_rate;
1309 unsigned long bvb_rate, hsm_rate;
1310 unsigned long flags;
1314 mutex_lock(&vc4_hdmi->mutex);
1316 if (!drm_dev_enter(drm, &idx))
1320 * As stated in RPi's vc4 firmware "HDMI state machine (HSM) clock must
1321 * be faster than pixel clock, infinitesimally faster, tested in
1322 * simulation. Otherwise, exact value is unimportant for HDMI
1323 * operation." This conflicts with bcm2835's vc4 documentation, which
1324 * states HSM's clock has to be at least 108% of the pixel clock.
1326 * Real life tests reveal that vc4's firmware statement holds up, and
1327 * users are able to use pixel clocks closer to HSM's, namely for
1328 * 1920x1200@60Hz. So it was decided to have leave a 1% margin between
1329 * both clocks. Which, for RPi0-3 implies a maximum pixel clock of
1332 * Additionally, the AXI clock needs to be at least 25% of
1333 * pixel clock, but HSM ends up being the limiting factor.
1335 hsm_rate = max_t(unsigned long, 120000000, (tmds_char_rate / 100) * 101);
1336 ret = clk_set_min_rate(vc4_hdmi->hsm_clock, hsm_rate);
1338 DRM_ERROR("Failed to set HSM clock rate: %d\n", ret);
1342 ret = pm_runtime_resume_and_get(&vc4_hdmi->pdev->dev);
1344 DRM_ERROR("Failed to retain power domain: %d\n", ret);
1348 ret = clk_set_rate(vc4_hdmi->pixel_clock, tmds_char_rate);
1350 DRM_ERROR("Failed to set pixel clock rate: %d\n", ret);
1351 goto err_put_runtime_pm;
1354 ret = clk_prepare_enable(vc4_hdmi->pixel_clock);
1356 DRM_ERROR("Failed to turn on pixel clock: %d\n", ret);
1357 goto err_put_runtime_pm;
1361 vc4_hdmi_cec_update_clk_div(vc4_hdmi);
1363 if (tmds_char_rate > 297000000)
1364 bvb_rate = 300000000;
1365 else if (tmds_char_rate > 148500000)
1366 bvb_rate = 150000000;
1368 bvb_rate = 75000000;
1370 ret = clk_set_min_rate(vc4_hdmi->pixel_bvb_clock, bvb_rate);
1372 DRM_ERROR("Failed to set pixel bvb clock rate: %d\n", ret);
1373 goto err_disable_pixel_clock;
1376 ret = clk_prepare_enable(vc4_hdmi->pixel_bvb_clock);
1378 DRM_ERROR("Failed to turn on pixel bvb clock: %d\n", ret);
1379 goto err_disable_pixel_clock;
1382 if (vc4_hdmi->variant->phy_init)
1383 vc4_hdmi->variant->phy_init(vc4_hdmi, vc4_conn_state);
1385 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1387 HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
1388 HDMI_READ(HDMI_SCHEDULER_CONTROL) |
1389 VC4_HDMI_SCHEDULER_CONTROL_MANUAL_FORMAT |
1390 VC4_HDMI_SCHEDULER_CONTROL_IGNORE_VSYNC_PREDICTS);
1392 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1394 if (vc4_hdmi->variant->set_timings)
1395 vc4_hdmi->variant->set_timings(vc4_hdmi, conn_state, mode);
1399 mutex_unlock(&vc4_hdmi->mutex);
1403 err_disable_pixel_clock:
1404 clk_disable_unprepare(vc4_hdmi->pixel_clock);
1406 pm_runtime_put(&vc4_hdmi->pdev->dev);
1410 mutex_unlock(&vc4_hdmi->mutex);
1414 static void vc4_hdmi_encoder_pre_crtc_enable(struct drm_encoder *encoder,
1415 struct drm_atomic_state *state)
1417 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1418 struct drm_device *drm = vc4_hdmi->connector.dev;
1419 struct drm_connector *connector = &vc4_hdmi->connector;
1420 struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
1421 struct drm_connector_state *conn_state =
1422 drm_atomic_get_new_connector_state(state, connector);
1423 unsigned long flags;
1426 mutex_lock(&vc4_hdmi->mutex);
1428 if (!drm_dev_enter(drm, &idx))
1431 if (vc4_hdmi->variant->csc_setup)
1432 vc4_hdmi->variant->csc_setup(vc4_hdmi, conn_state, mode);
1434 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1435 HDMI_WRITE(HDMI_FIFO_CTL, VC4_HDMI_FIFO_CTL_MASTER_SLAVE_N);
1436 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1440 mutex_unlock(&vc4_hdmi->mutex);
1443 static void vc4_hdmi_encoder_post_crtc_enable(struct drm_encoder *encoder,
1444 struct drm_atomic_state *state)
1446 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1447 struct drm_device *drm = vc4_hdmi->connector.dev;
1448 struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
1449 struct drm_display_info *display = &vc4_hdmi->connector.display_info;
1450 bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
1451 bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
1452 unsigned long flags;
1456 mutex_lock(&vc4_hdmi->mutex);
1458 if (!drm_dev_enter(drm, &idx))
1461 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1463 HDMI_WRITE(HDMI_VID_CTL,
1464 VC4_HD_VID_CTL_ENABLE |
1465 VC4_HD_VID_CTL_CLRRGB |
1466 VC4_HD_VID_CTL_UNDERFLOW_ENABLE |
1467 VC4_HD_VID_CTL_FRAME_COUNTER_RESET |
1468 (vsync_pos ? 0 : VC4_HD_VID_CTL_VSYNC_LOW) |
1469 (hsync_pos ? 0 : VC4_HD_VID_CTL_HSYNC_LOW));
1471 HDMI_WRITE(HDMI_VID_CTL,
1472 HDMI_READ(HDMI_VID_CTL) & ~VC4_HD_VID_CTL_BLANKPIX);
1474 if (display->is_hdmi) {
1475 HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
1476 HDMI_READ(HDMI_SCHEDULER_CONTROL) |
1477 VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);
1479 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1481 ret = wait_for(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
1482 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE, 1000);
1483 WARN_ONCE(ret, "Timeout waiting for "
1484 "VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
1486 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
1487 HDMI_READ(HDMI_RAM_PACKET_CONFIG) &
1488 ~(VC4_HDMI_RAM_PACKET_ENABLE));
1489 HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
1490 HDMI_READ(HDMI_SCHEDULER_CONTROL) &
1491 ~VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);
1493 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1495 ret = wait_for(!(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
1496 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE), 1000);
1497 WARN_ONCE(ret, "Timeout waiting for "
1498 "!VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
1501 if (display->is_hdmi) {
1502 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1504 WARN_ON(!(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
1505 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE));
1507 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
1508 VC4_HDMI_RAM_PACKET_ENABLE);
1510 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1511 vc4_hdmi->packet_ram_enabled = true;
1513 vc4_hdmi_set_infoframes(encoder);
1516 vc4_hdmi_recenter_fifo(vc4_hdmi);
1517 vc4_hdmi_enable_scrambling(encoder);
1520 mutex_unlock(&vc4_hdmi->mutex);
1523 static void vc4_hdmi_encoder_atomic_mode_set(struct drm_encoder *encoder,
1524 struct drm_crtc_state *crtc_state,
1525 struct drm_connector_state *conn_state)
1527 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1528 struct vc4_hdmi_connector_state *vc4_state =
1529 conn_state_to_vc4_hdmi_conn_state(conn_state);
1531 mutex_lock(&vc4_hdmi->mutex);
1532 drm_mode_copy(&vc4_hdmi->saved_adjusted_mode,
1533 &crtc_state->adjusted_mode);
1534 vc4_hdmi->output_bpc = vc4_state->output_bpc;
1535 vc4_hdmi->output_format = vc4_state->output_format;
1536 mutex_unlock(&vc4_hdmi->mutex);
1540 vc4_hdmi_sink_supports_format_bpc(const struct vc4_hdmi *vc4_hdmi,
1541 const struct drm_display_info *info,
1542 const struct drm_display_mode *mode,
1543 unsigned int format, unsigned int bpc)
1545 struct drm_device *dev = vc4_hdmi->connector.dev;
1546 u8 vic = drm_match_cea_mode(mode);
1548 if (vic == 1 && bpc != 8) {
1549 drm_dbg(dev, "VIC1 requires a bpc of 8, got %u\n", bpc);
1553 if (!info->is_hdmi &&
1554 (format != VC4_HDMI_OUTPUT_RGB || bpc != 8)) {
1555 drm_dbg(dev, "DVI Monitors require an RGB output at 8 bpc\n");
1560 case VC4_HDMI_OUTPUT_RGB:
1561 drm_dbg(dev, "RGB Format, checking the constraints.\n");
1563 if (!(info->color_formats & DRM_COLOR_FORMAT_RGB444))
1566 if (bpc == 10 && !(info->edid_hdmi_rgb444_dc_modes & DRM_EDID_HDMI_DC_30)) {
1567 drm_dbg(dev, "10 BPC but sink doesn't support Deep Color 30.\n");
1571 if (bpc == 12 && !(info->edid_hdmi_rgb444_dc_modes & DRM_EDID_HDMI_DC_36)) {
1572 drm_dbg(dev, "12 BPC but sink doesn't support Deep Color 36.\n");
1576 drm_dbg(dev, "RGB format supported in that configuration.\n");
1580 case VC4_HDMI_OUTPUT_YUV422:
1581 drm_dbg(dev, "YUV422 format, checking the constraints.\n");
1583 if (!(info->color_formats & DRM_COLOR_FORMAT_YCBCR422)) {
1584 drm_dbg(dev, "Sink doesn't support YUV422.\n");
1589 drm_dbg(dev, "YUV422 only supports 12 bpc.\n");
1593 drm_dbg(dev, "YUV422 format supported in that configuration.\n");
1597 case VC4_HDMI_OUTPUT_YUV444:
1598 drm_dbg(dev, "YUV444 format, checking the constraints.\n");
1600 if (!(info->color_formats & DRM_COLOR_FORMAT_YCBCR444)) {
1601 drm_dbg(dev, "Sink doesn't support YUV444.\n");
1605 if (bpc == 10 && !(info->edid_hdmi_ycbcr444_dc_modes & DRM_EDID_HDMI_DC_30)) {
1606 drm_dbg(dev, "10 BPC but sink doesn't support Deep Color 30.\n");
1610 if (bpc == 12 && !(info->edid_hdmi_ycbcr444_dc_modes & DRM_EDID_HDMI_DC_36)) {
1611 drm_dbg(dev, "12 BPC but sink doesn't support Deep Color 36.\n");
1615 drm_dbg(dev, "YUV444 format supported in that configuration.\n");
1623 static enum drm_mode_status
1624 vc4_hdmi_encoder_clock_valid(const struct vc4_hdmi *vc4_hdmi,
1625 unsigned long long clock)
1627 const struct drm_connector *connector = &vc4_hdmi->connector;
1628 const struct drm_display_info *info = &connector->display_info;
1630 if (clock > vc4_hdmi->variant->max_pixel_clock)
1631 return MODE_CLOCK_HIGH;
1633 if (vc4_hdmi->disable_4kp60 && clock > HDMI_14_MAX_TMDS_CLK)
1634 return MODE_CLOCK_HIGH;
1636 if (info->max_tmds_clock && clock > (info->max_tmds_clock * 1000))
1637 return MODE_CLOCK_HIGH;
1642 static unsigned long long
1643 vc4_hdmi_encoder_compute_mode_clock(const struct drm_display_mode *mode,
1645 enum vc4_hdmi_output_format fmt)
1647 unsigned long long clock = mode->clock * 1000ULL;
1649 if (mode->flags & DRM_MODE_FLAG_DBLCLK)
1652 if (fmt == VC4_HDMI_OUTPUT_YUV422)
1655 clock = clock * bpc;
1662 vc4_hdmi_encoder_compute_clock(const struct vc4_hdmi *vc4_hdmi,
1663 struct vc4_hdmi_connector_state *vc4_state,
1664 const struct drm_display_mode *mode,
1665 unsigned int bpc, unsigned int fmt)
1667 unsigned long long clock;
1669 clock = vc4_hdmi_encoder_compute_mode_clock(mode, bpc, fmt);
1670 if (vc4_hdmi_encoder_clock_valid(vc4_hdmi, clock) != MODE_OK)
1673 vc4_state->tmds_char_rate = clock;
1679 vc4_hdmi_encoder_compute_format(const struct vc4_hdmi *vc4_hdmi,
1680 struct vc4_hdmi_connector_state *vc4_state,
1681 const struct drm_display_mode *mode,
1684 struct drm_device *dev = vc4_hdmi->connector.dev;
1685 const struct drm_connector *connector = &vc4_hdmi->connector;
1686 const struct drm_display_info *info = &connector->display_info;
1687 unsigned int format;
1689 drm_dbg(dev, "Trying with an RGB output\n");
1691 format = VC4_HDMI_OUTPUT_RGB;
1692 if (vc4_hdmi_sink_supports_format_bpc(vc4_hdmi, info, mode, format, bpc)) {
1695 ret = vc4_hdmi_encoder_compute_clock(vc4_hdmi, vc4_state,
1698 vc4_state->output_format = format;
1703 drm_dbg(dev, "Failed, Trying with an YUV422 output\n");
1705 format = VC4_HDMI_OUTPUT_YUV422;
1706 if (vc4_hdmi_sink_supports_format_bpc(vc4_hdmi, info, mode, format, bpc)) {
1709 ret = vc4_hdmi_encoder_compute_clock(vc4_hdmi, vc4_state,
1712 vc4_state->output_format = format;
1717 drm_dbg(dev, "Failed. No Format Supported for that bpc count.\n");
1723 vc4_hdmi_encoder_compute_config(const struct vc4_hdmi *vc4_hdmi,
1724 struct vc4_hdmi_connector_state *vc4_state,
1725 const struct drm_display_mode *mode)
1727 struct drm_device *dev = vc4_hdmi->connector.dev;
1728 struct drm_connector_state *conn_state = &vc4_state->base;
1729 unsigned int max_bpc = clamp_t(unsigned int, conn_state->max_bpc, 8, 12);
1733 for (bpc = max_bpc; bpc >= 8; bpc -= 2) {
1734 drm_dbg(dev, "Trying with a %d bpc output\n", bpc);
1736 ret = vc4_hdmi_encoder_compute_format(vc4_hdmi, vc4_state,
1741 vc4_state->output_bpc = bpc;
1744 "Mode %ux%u @ %uHz: Found configuration: bpc: %u, fmt: %s, clock: %llu\n",
1745 mode->hdisplay, mode->vdisplay, drm_mode_vrefresh(mode),
1746 vc4_state->output_bpc,
1747 vc4_hdmi_output_fmt_str(vc4_state->output_format),
1748 vc4_state->tmds_char_rate);
1756 #define WIFI_2_4GHz_CH1_MIN_FREQ 2400000000ULL
1757 #define WIFI_2_4GHz_CH1_MAX_FREQ 2422000000ULL
1759 static int vc4_hdmi_encoder_atomic_check(struct drm_encoder *encoder,
1760 struct drm_crtc_state *crtc_state,
1761 struct drm_connector_state *conn_state)
1763 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1764 struct drm_connector *connector = &vc4_hdmi->connector;
1765 struct drm_connector_state *old_conn_state =
1766 drm_atomic_get_old_connector_state(conn_state->state, connector);
1767 struct vc4_hdmi_connector_state *old_vc4_state =
1768 conn_state_to_vc4_hdmi_conn_state(old_conn_state);
1769 struct vc4_hdmi_connector_state *vc4_state = conn_state_to_vc4_hdmi_conn_state(conn_state);
1770 struct drm_display_mode *mode = &crtc_state->adjusted_mode;
1771 unsigned long long tmds_char_rate = mode->clock * 1000;
1772 unsigned long long tmds_bit_rate;
1775 if (vc4_hdmi->variant->unsupported_odd_h_timings) {
1776 if (mode->flags & DRM_MODE_FLAG_DBLCLK) {
1777 /* Only try to fixup DBLCLK modes to get 480i and 576i
1779 * A generic solution for all modes with odd horizontal
1780 * timing values seems impossible based on trying to
1781 * solve it for 1366x768 monitors.
1783 if ((mode->hsync_start - mode->hdisplay) & 1)
1784 mode->hsync_start--;
1785 if ((mode->hsync_end - mode->hsync_start) & 1)
1789 /* Now check whether we still have odd values remaining */
1790 if ((mode->hdisplay % 2) || (mode->hsync_start % 2) ||
1791 (mode->hsync_end % 2) || (mode->htotal % 2))
1796 * The 1440p@60 pixel rate is in the same range than the first
1797 * WiFi channel (between 2.4GHz and 2.422GHz with 22MHz
1798 * bandwidth). Slightly lower the frequency to bring it out of
1801 tmds_bit_rate = tmds_char_rate * 10;
1802 if (vc4_hdmi->disable_wifi_frequencies &&
1803 (tmds_bit_rate >= WIFI_2_4GHz_CH1_MIN_FREQ &&
1804 tmds_bit_rate <= WIFI_2_4GHz_CH1_MAX_FREQ)) {
1805 mode->clock = 238560;
1806 tmds_char_rate = mode->clock * 1000;
1809 ret = vc4_hdmi_encoder_compute_config(vc4_hdmi, vc4_state, mode);
1813 /* vc4_hdmi_encoder_compute_config may have changed output_bpc and/or output_format */
1814 if (vc4_state->output_bpc != old_vc4_state->output_bpc ||
1815 vc4_state->output_format != old_vc4_state->output_format)
1816 crtc_state->mode_changed = true;
1821 static enum drm_mode_status
1822 vc4_hdmi_encoder_mode_valid(struct drm_encoder *encoder,
1823 const struct drm_display_mode *mode)
1825 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1827 if (vc4_hdmi->variant->unsupported_odd_h_timings &&
1828 !(mode->flags & DRM_MODE_FLAG_DBLCLK) &&
1829 ((mode->hdisplay % 2) || (mode->hsync_start % 2) ||
1830 (mode->hsync_end % 2) || (mode->htotal % 2)))
1831 return MODE_H_ILLEGAL;
1833 return vc4_hdmi_encoder_clock_valid(vc4_hdmi, mode->clock * 1000);
1836 static const struct drm_encoder_helper_funcs vc4_hdmi_encoder_helper_funcs = {
1837 .atomic_check = vc4_hdmi_encoder_atomic_check,
1838 .atomic_mode_set = vc4_hdmi_encoder_atomic_mode_set,
1839 .mode_valid = vc4_hdmi_encoder_mode_valid,
1842 static u32 vc4_hdmi_channel_map(struct vc4_hdmi *vc4_hdmi, u32 channel_mask)
1845 u32 channel_map = 0;
1847 for (i = 0; i < 8; i++) {
1848 if (channel_mask & BIT(i))
1849 channel_map |= i << (3 * i);
1854 static u32 vc5_hdmi_channel_map(struct vc4_hdmi *vc4_hdmi, u32 channel_mask)
1857 u32 channel_map = 0;
1859 for (i = 0; i < 8; i++) {
1860 if (channel_mask & BIT(i))
1861 channel_map |= i << (4 * i);
1866 static bool vc5_hdmi_hp_detect(struct vc4_hdmi *vc4_hdmi)
1868 struct drm_device *drm = vc4_hdmi->connector.dev;
1869 unsigned long flags;
1873 if (!drm_dev_enter(drm, &idx))
1876 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1877 hotplug = HDMI_READ(HDMI_HOTPLUG);
1878 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1882 return !!(hotplug & VC4_HDMI_HOTPLUG_CONNECTED);
1885 /* HDMI audio codec callbacks */
1886 static void vc4_hdmi_audio_set_mai_clock(struct vc4_hdmi *vc4_hdmi,
1887 unsigned int samplerate)
1889 struct drm_device *drm = vc4_hdmi->connector.dev;
1891 unsigned long flags;
1895 if (!drm_dev_enter(drm, &idx))
1898 hsm_clock = clk_get_rate(vc4_hdmi->audio_clock);
1899 rational_best_approximation(hsm_clock, samplerate,
1900 VC4_HD_MAI_SMP_N_MASK >>
1901 VC4_HD_MAI_SMP_N_SHIFT,
1902 (VC4_HD_MAI_SMP_M_MASK >>
1903 VC4_HD_MAI_SMP_M_SHIFT) + 1,
1906 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1907 HDMI_WRITE(HDMI_MAI_SMP,
1908 VC4_SET_FIELD(n, VC4_HD_MAI_SMP_N) |
1909 VC4_SET_FIELD(m - 1, VC4_HD_MAI_SMP_M));
1910 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1915 static void vc4_hdmi_set_n_cts(struct vc4_hdmi *vc4_hdmi, unsigned int samplerate)
1917 const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
1921 lockdep_assert_held(&vc4_hdmi->mutex);
1922 lockdep_assert_held(&vc4_hdmi->hw_lock);
1924 n = 128 * samplerate / 1000;
1925 tmp = (u64)(mode->clock * 1000) * n;
1926 do_div(tmp, 128 * samplerate);
1929 HDMI_WRITE(HDMI_CRP_CFG,
1930 VC4_HDMI_CRP_CFG_EXTERNAL_CTS_EN |
1931 VC4_SET_FIELD(n, VC4_HDMI_CRP_CFG_N));
1934 * We could get slightly more accurate clocks in some cases by
1935 * providing a CTS_1 value. The two CTS values are alternated
1936 * between based on the period fields
1938 HDMI_WRITE(HDMI_CTS_0, cts);
1939 HDMI_WRITE(HDMI_CTS_1, cts);
1942 static inline struct vc4_hdmi *dai_to_hdmi(struct snd_soc_dai *dai)
1944 struct snd_soc_card *card = snd_soc_dai_get_drvdata(dai);
1946 return snd_soc_card_get_drvdata(card);
1949 static bool vc4_hdmi_audio_can_stream(struct vc4_hdmi *vc4_hdmi)
1951 struct drm_display_info *display = &vc4_hdmi->connector.display_info;
1953 lockdep_assert_held(&vc4_hdmi->mutex);
1956 * If the encoder is currently in DVI mode, treat the codec DAI
1959 if (!display->is_hdmi)
1965 static int vc4_hdmi_audio_startup(struct device *dev, void *data)
1967 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
1968 struct drm_device *drm = vc4_hdmi->connector.dev;
1969 unsigned long flags;
1973 mutex_lock(&vc4_hdmi->mutex);
1975 if (!drm_dev_enter(drm, &idx)) {
1980 if (!vc4_hdmi_audio_can_stream(vc4_hdmi)) {
1985 vc4_hdmi->audio.streaming = true;
1987 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1988 HDMI_WRITE(HDMI_MAI_CTL,
1989 VC4_HD_MAI_CTL_RESET |
1990 VC4_HD_MAI_CTL_FLUSH |
1991 VC4_HD_MAI_CTL_DLATE |
1992 VC4_HD_MAI_CTL_ERRORE |
1993 VC4_HD_MAI_CTL_ERRORF);
1994 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1996 if (vc4_hdmi->variant->phy_rng_enable)
1997 vc4_hdmi->variant->phy_rng_enable(vc4_hdmi);
2002 mutex_unlock(&vc4_hdmi->mutex);
2007 static void vc4_hdmi_audio_reset(struct vc4_hdmi *vc4_hdmi)
2009 struct drm_encoder *encoder = &vc4_hdmi->encoder.base;
2010 struct device *dev = &vc4_hdmi->pdev->dev;
2011 unsigned long flags;
2014 lockdep_assert_held(&vc4_hdmi->mutex);
2016 vc4_hdmi->audio.streaming = false;
2017 ret = vc4_hdmi_stop_packet(encoder, HDMI_INFOFRAME_TYPE_AUDIO, false);
2019 dev_err(dev, "Failed to stop audio infoframe: %d\n", ret);
2021 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2023 HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_RESET);
2024 HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_ERRORF);
2025 HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_FLUSH);
2027 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2030 static void vc4_hdmi_audio_shutdown(struct device *dev, void *data)
2032 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
2033 struct drm_device *drm = vc4_hdmi->connector.dev;
2034 unsigned long flags;
2037 mutex_lock(&vc4_hdmi->mutex);
2039 if (!drm_dev_enter(drm, &idx))
2042 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2044 HDMI_WRITE(HDMI_MAI_CTL,
2045 VC4_HD_MAI_CTL_DLATE |
2046 VC4_HD_MAI_CTL_ERRORE |
2047 VC4_HD_MAI_CTL_ERRORF);
2049 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2051 if (vc4_hdmi->variant->phy_rng_disable)
2052 vc4_hdmi->variant->phy_rng_disable(vc4_hdmi);
2054 vc4_hdmi->audio.streaming = false;
2055 vc4_hdmi_audio_reset(vc4_hdmi);
2060 mutex_unlock(&vc4_hdmi->mutex);
2063 static int sample_rate_to_mai_fmt(int samplerate)
2065 switch (samplerate) {
2067 return VC4_HDMI_MAI_SAMPLE_RATE_8000;
2069 return VC4_HDMI_MAI_SAMPLE_RATE_11025;
2071 return VC4_HDMI_MAI_SAMPLE_RATE_12000;
2073 return VC4_HDMI_MAI_SAMPLE_RATE_16000;
2075 return VC4_HDMI_MAI_SAMPLE_RATE_22050;
2077 return VC4_HDMI_MAI_SAMPLE_RATE_24000;
2079 return VC4_HDMI_MAI_SAMPLE_RATE_32000;
2081 return VC4_HDMI_MAI_SAMPLE_RATE_44100;
2083 return VC4_HDMI_MAI_SAMPLE_RATE_48000;
2085 return VC4_HDMI_MAI_SAMPLE_RATE_64000;
2087 return VC4_HDMI_MAI_SAMPLE_RATE_88200;
2089 return VC4_HDMI_MAI_SAMPLE_RATE_96000;
2091 return VC4_HDMI_MAI_SAMPLE_RATE_128000;
2093 return VC4_HDMI_MAI_SAMPLE_RATE_176400;
2095 return VC4_HDMI_MAI_SAMPLE_RATE_192000;
2097 return VC4_HDMI_MAI_SAMPLE_RATE_NOT_INDICATED;
2101 /* HDMI audio codec callbacks */
2102 static int vc4_hdmi_audio_prepare(struct device *dev, void *data,
2103 struct hdmi_codec_daifmt *daifmt,
2104 struct hdmi_codec_params *params)
2106 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
2107 struct drm_device *drm = vc4_hdmi->connector.dev;
2108 struct drm_encoder *encoder = &vc4_hdmi->encoder.base;
2109 unsigned int sample_rate = params->sample_rate;
2110 unsigned int channels = params->channels;
2111 unsigned long flags;
2112 u32 audio_packet_config, channel_mask;
2114 u32 mai_audio_format;
2115 u32 mai_sample_rate;
2119 dev_dbg(dev, "%s: %u Hz, %d bit, %d channels\n", __func__,
2120 sample_rate, params->sample_width, channels);
2122 mutex_lock(&vc4_hdmi->mutex);
2124 if (!drm_dev_enter(drm, &idx)) {
2129 if (!vc4_hdmi_audio_can_stream(vc4_hdmi)) {
2134 vc4_hdmi_audio_set_mai_clock(vc4_hdmi, sample_rate);
2136 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2137 HDMI_WRITE(HDMI_MAI_CTL,
2138 VC4_SET_FIELD(channels, VC4_HD_MAI_CTL_CHNUM) |
2139 VC4_HD_MAI_CTL_WHOLSMP |
2140 VC4_HD_MAI_CTL_CHALIGN |
2141 VC4_HD_MAI_CTL_ENABLE);
2143 mai_sample_rate = sample_rate_to_mai_fmt(sample_rate);
2144 if (params->iec.status[0] & IEC958_AES0_NONAUDIO &&
2145 params->channels == 8)
2146 mai_audio_format = VC4_HDMI_MAI_FORMAT_HBR;
2148 mai_audio_format = VC4_HDMI_MAI_FORMAT_PCM;
2149 HDMI_WRITE(HDMI_MAI_FMT,
2150 VC4_SET_FIELD(mai_sample_rate,
2151 VC4_HDMI_MAI_FORMAT_SAMPLE_RATE) |
2152 VC4_SET_FIELD(mai_audio_format,
2153 VC4_HDMI_MAI_FORMAT_AUDIO_FORMAT));
2155 /* The B frame identifier should match the value used by alsa-lib (8) */
2156 audio_packet_config =
2157 VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_SAMPLE_FLAT |
2158 VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_INACTIVE_CHANNELS |
2159 VC4_SET_FIELD(0x8, VC4_HDMI_AUDIO_PACKET_B_FRAME_IDENTIFIER);
2161 channel_mask = GENMASK(channels - 1, 0);
2162 audio_packet_config |= VC4_SET_FIELD(channel_mask,
2163 VC4_HDMI_AUDIO_PACKET_CEA_MASK);
2165 /* Set the MAI threshold */
2166 HDMI_WRITE(HDMI_MAI_THR,
2167 VC4_SET_FIELD(0x08, VC4_HD_MAI_THR_PANICHIGH) |
2168 VC4_SET_FIELD(0x08, VC4_HD_MAI_THR_PANICLOW) |
2169 VC4_SET_FIELD(0x06, VC4_HD_MAI_THR_DREQHIGH) |
2170 VC4_SET_FIELD(0x08, VC4_HD_MAI_THR_DREQLOW));
2172 HDMI_WRITE(HDMI_MAI_CONFIG,
2173 VC4_HDMI_MAI_CONFIG_BIT_REVERSE |
2174 VC4_HDMI_MAI_CONFIG_FORMAT_REVERSE |
2175 VC4_SET_FIELD(channel_mask, VC4_HDMI_MAI_CHANNEL_MASK));
2177 channel_map = vc4_hdmi->variant->channel_map(vc4_hdmi, channel_mask);
2178 HDMI_WRITE(HDMI_MAI_CHANNEL_MAP, channel_map);
2179 HDMI_WRITE(HDMI_AUDIO_PACKET_CONFIG, audio_packet_config);
2181 vc4_hdmi_set_n_cts(vc4_hdmi, sample_rate);
2183 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2185 memcpy(&vc4_hdmi->audio.infoframe, ¶ms->cea, sizeof(params->cea));
2186 vc4_hdmi_set_audio_infoframe(encoder);
2191 mutex_unlock(&vc4_hdmi->mutex);
2196 static const struct snd_soc_component_driver vc4_hdmi_audio_cpu_dai_comp = {
2197 .name = "vc4-hdmi-cpu-dai-component",
2200 static int vc4_hdmi_audio_cpu_dai_probe(struct snd_soc_dai *dai)
2202 struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai);
2204 snd_soc_dai_init_dma_data(dai, &vc4_hdmi->audio.dma_data, NULL);
2209 static struct snd_soc_dai_driver vc4_hdmi_audio_cpu_dai_drv = {
2210 .name = "vc4-hdmi-cpu-dai",
2211 .probe = vc4_hdmi_audio_cpu_dai_probe,
2213 .stream_name = "Playback",
2216 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
2217 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
2218 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
2219 SNDRV_PCM_RATE_192000,
2220 .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
2224 static const struct snd_dmaengine_pcm_config pcm_conf = {
2225 .chan_names[SNDRV_PCM_STREAM_PLAYBACK] = "audio-rx",
2226 .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
2229 static int vc4_hdmi_audio_get_eld(struct device *dev, void *data,
2230 uint8_t *buf, size_t len)
2232 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
2233 struct drm_connector *connector = &vc4_hdmi->connector;
2235 mutex_lock(&vc4_hdmi->mutex);
2236 memcpy(buf, connector->eld, min(sizeof(connector->eld), len));
2237 mutex_unlock(&vc4_hdmi->mutex);
2242 static const struct hdmi_codec_ops vc4_hdmi_codec_ops = {
2243 .get_eld = vc4_hdmi_audio_get_eld,
2244 .prepare = vc4_hdmi_audio_prepare,
2245 .audio_shutdown = vc4_hdmi_audio_shutdown,
2246 .audio_startup = vc4_hdmi_audio_startup,
2249 static struct hdmi_codec_pdata vc4_hdmi_codec_pdata = {
2250 .ops = &vc4_hdmi_codec_ops,
2251 .max_i2s_channels = 8,
2255 static void vc4_hdmi_audio_codec_release(void *ptr)
2257 struct vc4_hdmi *vc4_hdmi = ptr;
2259 platform_device_unregister(vc4_hdmi->audio.codec_pdev);
2260 vc4_hdmi->audio.codec_pdev = NULL;
2263 static int vc4_hdmi_audio_init(struct vc4_hdmi *vc4_hdmi)
2265 const struct vc4_hdmi_register *mai_data =
2266 &vc4_hdmi->variant->registers[HDMI_MAI_DATA];
2267 struct snd_soc_dai_link *dai_link = &vc4_hdmi->audio.link;
2268 struct snd_soc_card *card = &vc4_hdmi->audio.card;
2269 struct device *dev = &vc4_hdmi->pdev->dev;
2270 struct platform_device *codec_pdev;
2276 * ASoC makes it a bit hard to retrieve a pointer to the
2277 * vc4_hdmi structure. Registering the card will overwrite our
2278 * device drvdata with a pointer to the snd_soc_card structure,
2279 * which can then be used to retrieve whatever drvdata we want
2282 * However, that doesn't fly in the case where we wouldn't
2283 * register an ASoC card (because of an old DT that is missing
2284 * the dmas properties for example), then the card isn't
2285 * registered and the device drvdata wouldn't be set.
2287 * We can deal with both cases by making sure a snd_soc_card
2288 * pointer and a vc4_hdmi structure are pointing to the same
2289 * memory address, so we can treat them indistinctly without any
2292 BUILD_BUG_ON(offsetof(struct vc4_hdmi_audio, card) != 0);
2293 BUILD_BUG_ON(offsetof(struct vc4_hdmi, audio) != 0);
2295 if (!of_find_property(dev->of_node, "dmas", &len) || !len) {
2297 "'dmas' DT property is missing or empty, no HDMI audio\n");
2301 if (mai_data->reg != VC4_HD) {
2302 WARN_ONCE(true, "MAI isn't in the HD block\n");
2307 * Get the physical address of VC4_HD_MAI_DATA. We need to retrieve
2308 * the bus address specified in the DT, because the physical address
2309 * (the one returned by platform_get_resource()) is not appropriate
2310 * for DMA transfers.
2311 * This VC/MMU should probably be exposed to avoid this kind of hacks.
2313 index = of_property_match_string(dev->of_node, "reg-names", "hd");
2314 /* Before BCM2711, we don't have a named register range */
2318 addr = of_get_address(dev->of_node, index, NULL, NULL);
2320 vc4_hdmi->audio.dma_data.addr = be32_to_cpup(addr) + mai_data->offset;
2321 vc4_hdmi->audio.dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
2322 vc4_hdmi->audio.dma_data.maxburst = 2;
2325 * NOTE: Strictly speaking, we should probably use a DRM-managed
2326 * registration there to avoid removing all the audio components
2327 * by the time the driver doesn't have any user anymore.
2329 * However, the ASoC core uses a number of devm_kzalloc calls
2330 * when registering, even when using non-device-managed
2331 * functions (such as in snd_soc_register_component()).
2333 * If we call snd_soc_unregister_component() in a DRM-managed
2334 * action, the device-managed actions have already been executed
2335 * and thus we would access memory that has been freed.
2337 * Using device-managed hooks here probably leaves us open to a
2338 * bunch of issues if userspace still has a handle on the ALSA
2339 * device when the device is removed. However, this is mitigated
2340 * by the use of drm_dev_enter()/drm_dev_exit() in the audio
2341 * path to prevent the access to the device resources if it
2342 * isn't there anymore.
2344 * Then, the vc4_hdmi structure is DRM-managed and thus only
2345 * freed whenever the last user has closed the DRM device file.
2346 * It should thus outlive ALSA in most situations.
2348 ret = devm_snd_dmaengine_pcm_register(dev, &pcm_conf, 0);
2350 dev_err(dev, "Could not register PCM component: %d\n", ret);
2354 ret = devm_snd_soc_register_component(dev, &vc4_hdmi_audio_cpu_dai_comp,
2355 &vc4_hdmi_audio_cpu_dai_drv, 1);
2357 dev_err(dev, "Could not register CPU DAI: %d\n", ret);
2361 codec_pdev = platform_device_register_data(dev, HDMI_CODEC_DRV_NAME,
2362 PLATFORM_DEVID_AUTO,
2363 &vc4_hdmi_codec_pdata,
2364 sizeof(vc4_hdmi_codec_pdata));
2365 if (IS_ERR(codec_pdev)) {
2366 dev_err(dev, "Couldn't register the HDMI codec: %ld\n", PTR_ERR(codec_pdev));
2367 return PTR_ERR(codec_pdev);
2369 vc4_hdmi->audio.codec_pdev = codec_pdev;
2371 ret = devm_add_action_or_reset(dev, vc4_hdmi_audio_codec_release, vc4_hdmi);
2375 dai_link->cpus = &vc4_hdmi->audio.cpu;
2376 dai_link->codecs = &vc4_hdmi->audio.codec;
2377 dai_link->platforms = &vc4_hdmi->audio.platform;
2379 dai_link->num_cpus = 1;
2380 dai_link->num_codecs = 1;
2381 dai_link->num_platforms = 1;
2383 dai_link->name = "MAI";
2384 dai_link->stream_name = "MAI PCM";
2385 dai_link->codecs->dai_name = "i2s-hifi";
2386 dai_link->cpus->dai_name = dev_name(dev);
2387 dai_link->codecs->name = dev_name(&codec_pdev->dev);
2388 dai_link->platforms->name = dev_name(dev);
2390 card->dai_link = dai_link;
2391 card->num_links = 1;
2392 card->name = vc4_hdmi->variant->card_name;
2393 card->driver_name = "vc4-hdmi";
2395 card->owner = THIS_MODULE;
2398 * Be careful, snd_soc_register_card() calls dev_set_drvdata() and
2399 * stores a pointer to the snd card object in dev->driver_data. This
2400 * means we cannot use it for something else. The hdmi back-pointer is
2401 * now stored in card->drvdata and should be retrieved with
2402 * snd_soc_card_get_drvdata() if needed.
2404 snd_soc_card_set_drvdata(card, vc4_hdmi);
2405 ret = devm_snd_soc_register_card(dev, card);
2407 dev_err_probe(dev, ret, "Could not register sound card\n");
2413 static irqreturn_t vc4_hdmi_hpd_irq_thread(int irq, void *priv)
2415 struct vc4_hdmi *vc4_hdmi = priv;
2416 struct drm_connector *connector = &vc4_hdmi->connector;
2417 struct drm_device *dev = connector->dev;
2419 if (dev && dev->registered)
2420 drm_connector_helper_hpd_irq_event(connector);
2425 static int vc4_hdmi_hotplug_init(struct vc4_hdmi *vc4_hdmi)
2427 struct drm_connector *connector = &vc4_hdmi->connector;
2428 struct platform_device *pdev = vc4_hdmi->pdev;
2431 if (vc4_hdmi->variant->external_irq_controller) {
2432 unsigned int hpd_con = platform_get_irq_byname(pdev, "hpd-connected");
2433 unsigned int hpd_rm = platform_get_irq_byname(pdev, "hpd-removed");
2435 ret = devm_request_threaded_irq(&pdev->dev, hpd_con,
2437 vc4_hdmi_hpd_irq_thread, IRQF_ONESHOT,
2438 "vc4 hdmi hpd connected", vc4_hdmi);
2442 ret = devm_request_threaded_irq(&pdev->dev, hpd_rm,
2444 vc4_hdmi_hpd_irq_thread, IRQF_ONESHOT,
2445 "vc4 hdmi hpd disconnected", vc4_hdmi);
2449 connector->polled = DRM_CONNECTOR_POLL_HPD;
2455 #ifdef CONFIG_DRM_VC4_HDMI_CEC
2456 static irqreturn_t vc4_cec_irq_handler_rx_thread(int irq, void *priv)
2458 struct vc4_hdmi *vc4_hdmi = priv;
2460 if (vc4_hdmi->cec_rx_msg.len)
2461 cec_received_msg(vc4_hdmi->cec_adap,
2462 &vc4_hdmi->cec_rx_msg);
2467 static irqreturn_t vc4_cec_irq_handler_tx_thread(int irq, void *priv)
2469 struct vc4_hdmi *vc4_hdmi = priv;
2471 if (vc4_hdmi->cec_tx_ok) {
2472 cec_transmit_done(vc4_hdmi->cec_adap, CEC_TX_STATUS_OK,
2476 * This CEC implementation makes 1 retry, so if we
2477 * get a NACK, then that means it made 2 attempts.
2479 cec_transmit_done(vc4_hdmi->cec_adap, CEC_TX_STATUS_NACK,
2485 static irqreturn_t vc4_cec_irq_handler_thread(int irq, void *priv)
2487 struct vc4_hdmi *vc4_hdmi = priv;
2490 if (vc4_hdmi->cec_irq_was_rx)
2491 ret = vc4_cec_irq_handler_rx_thread(irq, priv);
2493 ret = vc4_cec_irq_handler_tx_thread(irq, priv);
2498 static void vc4_cec_read_msg(struct vc4_hdmi *vc4_hdmi, u32 cntrl1)
2500 struct drm_device *dev = vc4_hdmi->connector.dev;
2501 struct cec_msg *msg = &vc4_hdmi->cec_rx_msg;
2504 lockdep_assert_held(&vc4_hdmi->hw_lock);
2506 msg->len = 1 + ((cntrl1 & VC4_HDMI_CEC_REC_WRD_CNT_MASK) >>
2507 VC4_HDMI_CEC_REC_WRD_CNT_SHIFT);
2509 if (msg->len > 16) {
2510 drm_err(dev, "Attempting to read too much data (%d)\n", msg->len);
2514 for (i = 0; i < msg->len; i += 4) {
2515 u32 val = HDMI_READ(HDMI_CEC_RX_DATA_1 + (i >> 2));
2517 msg->msg[i] = val & 0xff;
2518 msg->msg[i + 1] = (val >> 8) & 0xff;
2519 msg->msg[i + 2] = (val >> 16) & 0xff;
2520 msg->msg[i + 3] = (val >> 24) & 0xff;
2524 static irqreturn_t vc4_cec_irq_handler_tx_bare_locked(struct vc4_hdmi *vc4_hdmi)
2529 * We don't need to protect the register access using
2530 * drm_dev_enter() there because the interrupt handler lifetime
2531 * is tied to the device itself, and not to the DRM device.
2533 * So when the device will be gone, one of the first thing we
2534 * will be doing will be to unregister the interrupt handler,
2535 * and then unregister the DRM device. drm_dev_enter() would
2536 * thus always succeed if we are here.
2539 lockdep_assert_held(&vc4_hdmi->hw_lock);
2541 cntrl1 = HDMI_READ(HDMI_CEC_CNTRL_1);
2542 vc4_hdmi->cec_tx_ok = cntrl1 & VC4_HDMI_CEC_TX_STATUS_GOOD;
2543 cntrl1 &= ~VC4_HDMI_CEC_START_XMIT_BEGIN;
2544 HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1);
2546 return IRQ_WAKE_THREAD;
2549 static irqreturn_t vc4_cec_irq_handler_tx_bare(int irq, void *priv)
2551 struct vc4_hdmi *vc4_hdmi = priv;
2554 spin_lock(&vc4_hdmi->hw_lock);
2555 ret = vc4_cec_irq_handler_tx_bare_locked(vc4_hdmi);
2556 spin_unlock(&vc4_hdmi->hw_lock);
2561 static irqreturn_t vc4_cec_irq_handler_rx_bare_locked(struct vc4_hdmi *vc4_hdmi)
2565 lockdep_assert_held(&vc4_hdmi->hw_lock);
2568 * We don't need to protect the register access using
2569 * drm_dev_enter() there because the interrupt handler lifetime
2570 * is tied to the device itself, and not to the DRM device.
2572 * So when the device will be gone, one of the first thing we
2573 * will be doing will be to unregister the interrupt handler,
2574 * and then unregister the DRM device. drm_dev_enter() would
2575 * thus always succeed if we are here.
2578 vc4_hdmi->cec_rx_msg.len = 0;
2579 cntrl1 = HDMI_READ(HDMI_CEC_CNTRL_1);
2580 vc4_cec_read_msg(vc4_hdmi, cntrl1);
2581 cntrl1 |= VC4_HDMI_CEC_CLEAR_RECEIVE_OFF;
2582 HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1);
2583 cntrl1 &= ~VC4_HDMI_CEC_CLEAR_RECEIVE_OFF;
2585 HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1);
2587 return IRQ_WAKE_THREAD;
2590 static irqreturn_t vc4_cec_irq_handler_rx_bare(int irq, void *priv)
2592 struct vc4_hdmi *vc4_hdmi = priv;
2595 spin_lock(&vc4_hdmi->hw_lock);
2596 ret = vc4_cec_irq_handler_rx_bare_locked(vc4_hdmi);
2597 spin_unlock(&vc4_hdmi->hw_lock);
2602 static irqreturn_t vc4_cec_irq_handler(int irq, void *priv)
2604 struct vc4_hdmi *vc4_hdmi = priv;
2605 u32 stat = HDMI_READ(HDMI_CEC_CPU_STATUS);
2610 * We don't need to protect the register access using
2611 * drm_dev_enter() there because the interrupt handler lifetime
2612 * is tied to the device itself, and not to the DRM device.
2614 * So when the device will be gone, one of the first thing we
2615 * will be doing will be to unregister the interrupt handler,
2616 * and then unregister the DRM device. drm_dev_enter() would
2617 * thus always succeed if we are here.
2620 if (!(stat & VC4_HDMI_CPU_CEC))
2623 spin_lock(&vc4_hdmi->hw_lock);
2624 cntrl5 = HDMI_READ(HDMI_CEC_CNTRL_5);
2625 vc4_hdmi->cec_irq_was_rx = cntrl5 & VC4_HDMI_CEC_RX_CEC_INT;
2626 if (vc4_hdmi->cec_irq_was_rx)
2627 ret = vc4_cec_irq_handler_rx_bare_locked(vc4_hdmi);
2629 ret = vc4_cec_irq_handler_tx_bare_locked(vc4_hdmi);
2631 HDMI_WRITE(HDMI_CEC_CPU_CLEAR, VC4_HDMI_CPU_CEC);
2632 spin_unlock(&vc4_hdmi->hw_lock);
2637 static int vc4_hdmi_cec_enable(struct cec_adapter *adap)
2639 struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
2640 struct drm_device *drm = vc4_hdmi->connector.dev;
2641 /* clock period in microseconds */
2642 const u32 usecs = 1000000 / CEC_CLOCK_FREQ;
2643 unsigned long flags;
2649 * NOTE: This function should really take vc4_hdmi->mutex, but doing so
2650 * results in a reentrancy since cec_s_phys_addr_from_edid() called in
2651 * .detect or .get_modes might call .adap_enable, which leads to this
2652 * function being called with that mutex held.
2654 * Concurrency is not an issue for the moment since we don't share any
2655 * state with KMS, so we can ignore the lock for now, but we need to
2656 * keep it in mind if we were to change that assumption.
2659 if (!drm_dev_enter(drm, &idx))
2661 * We can't return an error code, because the CEC
2662 * framework will emit WARN_ON messages at unbind
2667 ret = pm_runtime_resume_and_get(&vc4_hdmi->pdev->dev);
2673 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2675 val = HDMI_READ(HDMI_CEC_CNTRL_5);
2676 val &= ~(VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET |
2677 VC4_HDMI_CEC_CNT_TO_4700_US_MASK |
2678 VC4_HDMI_CEC_CNT_TO_4500_US_MASK);
2679 val |= ((4700 / usecs) << VC4_HDMI_CEC_CNT_TO_4700_US_SHIFT) |
2680 ((4500 / usecs) << VC4_HDMI_CEC_CNT_TO_4500_US_SHIFT);
2682 HDMI_WRITE(HDMI_CEC_CNTRL_5, val |
2683 VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET);
2684 HDMI_WRITE(HDMI_CEC_CNTRL_5, val);
2685 HDMI_WRITE(HDMI_CEC_CNTRL_2,
2686 ((1500 / usecs) << VC4_HDMI_CEC_CNT_TO_1500_US_SHIFT) |
2687 ((1300 / usecs) << VC4_HDMI_CEC_CNT_TO_1300_US_SHIFT) |
2688 ((800 / usecs) << VC4_HDMI_CEC_CNT_TO_800_US_SHIFT) |
2689 ((600 / usecs) << VC4_HDMI_CEC_CNT_TO_600_US_SHIFT) |
2690 ((400 / usecs) << VC4_HDMI_CEC_CNT_TO_400_US_SHIFT));
2691 HDMI_WRITE(HDMI_CEC_CNTRL_3,
2692 ((2750 / usecs) << VC4_HDMI_CEC_CNT_TO_2750_US_SHIFT) |
2693 ((2400 / usecs) << VC4_HDMI_CEC_CNT_TO_2400_US_SHIFT) |
2694 ((2050 / usecs) << VC4_HDMI_CEC_CNT_TO_2050_US_SHIFT) |
2695 ((1700 / usecs) << VC4_HDMI_CEC_CNT_TO_1700_US_SHIFT));
2696 HDMI_WRITE(HDMI_CEC_CNTRL_4,
2697 ((4300 / usecs) << VC4_HDMI_CEC_CNT_TO_4300_US_SHIFT) |
2698 ((3900 / usecs) << VC4_HDMI_CEC_CNT_TO_3900_US_SHIFT) |
2699 ((3600 / usecs) << VC4_HDMI_CEC_CNT_TO_3600_US_SHIFT) |
2700 ((3500 / usecs) << VC4_HDMI_CEC_CNT_TO_3500_US_SHIFT));
2702 if (!vc4_hdmi->variant->external_irq_controller)
2703 HDMI_WRITE(HDMI_CEC_CPU_MASK_CLEAR, VC4_HDMI_CPU_CEC);
2705 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2712 static int vc4_hdmi_cec_disable(struct cec_adapter *adap)
2714 struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
2715 struct drm_device *drm = vc4_hdmi->connector.dev;
2716 unsigned long flags;
2719 if (!drm_dev_enter(drm, &idx))
2721 * We can't return an error code, because the CEC
2722 * framework will emit WARN_ON messages at unbind
2728 * NOTE: This function should really take vc4_hdmi->mutex, but doing so
2729 * results in a reentrancy since cec_s_phys_addr_from_edid() called in
2730 * .detect or .get_modes might call .adap_enable, which leads to this
2731 * function being called with that mutex held.
2733 * Concurrency is not an issue for the moment since we don't share any
2734 * state with KMS, so we can ignore the lock for now, but we need to
2735 * keep it in mind if we were to change that assumption.
2738 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2740 if (!vc4_hdmi->variant->external_irq_controller)
2741 HDMI_WRITE(HDMI_CEC_CPU_MASK_SET, VC4_HDMI_CPU_CEC);
2743 HDMI_WRITE(HDMI_CEC_CNTRL_5, HDMI_READ(HDMI_CEC_CNTRL_5) |
2744 VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET);
2746 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2748 pm_runtime_put(&vc4_hdmi->pdev->dev);
2755 static int vc4_hdmi_cec_adap_enable(struct cec_adapter *adap, bool enable)
2758 return vc4_hdmi_cec_enable(adap);
2760 return vc4_hdmi_cec_disable(adap);
2763 static int vc4_hdmi_cec_adap_log_addr(struct cec_adapter *adap, u8 log_addr)
2765 struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
2766 struct drm_device *drm = vc4_hdmi->connector.dev;
2767 unsigned long flags;
2771 * NOTE: This function should really take vc4_hdmi->mutex, but doing so
2772 * results in a reentrancy since cec_s_phys_addr_from_edid() called in
2773 * .detect or .get_modes might call .adap_enable, which leads to this
2774 * function being called with that mutex held.
2776 * Concurrency is not an issue for the moment since we don't share any
2777 * state with KMS, so we can ignore the lock for now, but we need to
2778 * keep it in mind if we were to change that assumption.
2781 if (!drm_dev_enter(drm, &idx))
2783 * We can't return an error code, because the CEC
2784 * framework will emit WARN_ON messages at unbind
2789 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2790 HDMI_WRITE(HDMI_CEC_CNTRL_1,
2791 (HDMI_READ(HDMI_CEC_CNTRL_1) & ~VC4_HDMI_CEC_ADDR_MASK) |
2792 (log_addr & 0xf) << VC4_HDMI_CEC_ADDR_SHIFT);
2793 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2800 static int vc4_hdmi_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
2801 u32 signal_free_time, struct cec_msg *msg)
2803 struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
2804 struct drm_device *dev = vc4_hdmi->connector.dev;
2805 unsigned long flags;
2811 * NOTE: This function should really take vc4_hdmi->mutex, but doing so
2812 * results in a reentrancy since cec_s_phys_addr_from_edid() called in
2813 * .detect or .get_modes might call .adap_enable, which leads to this
2814 * function being called with that mutex held.
2816 * Concurrency is not an issue for the moment since we don't share any
2817 * state with KMS, so we can ignore the lock for now, but we need to
2818 * keep it in mind if we were to change that assumption.
2821 if (!drm_dev_enter(dev, &idx))
2824 if (msg->len > 16) {
2825 drm_err(dev, "Attempting to transmit too much data (%d)\n", msg->len);
2830 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2832 for (i = 0; i < msg->len; i += 4)
2833 HDMI_WRITE(HDMI_CEC_TX_DATA_1 + (i >> 2),
2835 (msg->msg[i + 1] << 8) |
2836 (msg->msg[i + 2] << 16) |
2837 (msg->msg[i + 3] << 24));
2839 val = HDMI_READ(HDMI_CEC_CNTRL_1);
2840 val &= ~VC4_HDMI_CEC_START_XMIT_BEGIN;
2841 HDMI_WRITE(HDMI_CEC_CNTRL_1, val);
2842 val &= ~VC4_HDMI_CEC_MESSAGE_LENGTH_MASK;
2843 val |= (msg->len - 1) << VC4_HDMI_CEC_MESSAGE_LENGTH_SHIFT;
2844 val |= VC4_HDMI_CEC_START_XMIT_BEGIN;
2846 HDMI_WRITE(HDMI_CEC_CNTRL_1, val);
2848 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2855 static const struct cec_adap_ops vc4_hdmi_cec_adap_ops = {
2856 .adap_enable = vc4_hdmi_cec_adap_enable,
2857 .adap_log_addr = vc4_hdmi_cec_adap_log_addr,
2858 .adap_transmit = vc4_hdmi_cec_adap_transmit,
2861 static void vc4_hdmi_cec_release(void *ptr)
2863 struct vc4_hdmi *vc4_hdmi = ptr;
2865 cec_unregister_adapter(vc4_hdmi->cec_adap);
2866 vc4_hdmi->cec_adap = NULL;
2869 static int vc4_hdmi_cec_init(struct vc4_hdmi *vc4_hdmi)
2871 struct cec_connector_info conn_info;
2872 struct platform_device *pdev = vc4_hdmi->pdev;
2873 struct device *dev = &pdev->dev;
2876 if (!of_find_property(dev->of_node, "interrupts", NULL)) {
2877 dev_warn(dev, "'interrupts' DT property is missing, no CEC\n");
2881 vc4_hdmi->cec_adap = cec_allocate_adapter(&vc4_hdmi_cec_adap_ops,
2884 CEC_CAP_CONNECTOR_INFO, 1);
2885 ret = PTR_ERR_OR_ZERO(vc4_hdmi->cec_adap);
2889 cec_fill_conn_info_from_drm(&conn_info, &vc4_hdmi->connector);
2890 cec_s_conn_info(vc4_hdmi->cec_adap, &conn_info);
2892 if (vc4_hdmi->variant->external_irq_controller) {
2893 ret = devm_request_threaded_irq(dev, platform_get_irq_byname(pdev, "cec-rx"),
2894 vc4_cec_irq_handler_rx_bare,
2895 vc4_cec_irq_handler_rx_thread, 0,
2896 "vc4 hdmi cec rx", vc4_hdmi);
2898 goto err_delete_cec_adap;
2900 ret = devm_request_threaded_irq(dev, platform_get_irq_byname(pdev, "cec-tx"),
2901 vc4_cec_irq_handler_tx_bare,
2902 vc4_cec_irq_handler_tx_thread, 0,
2903 "vc4 hdmi cec tx", vc4_hdmi);
2905 goto err_delete_cec_adap;
2907 ret = devm_request_threaded_irq(dev, platform_get_irq(pdev, 0),
2908 vc4_cec_irq_handler,
2909 vc4_cec_irq_handler_thread, 0,
2910 "vc4 hdmi cec", vc4_hdmi);
2912 goto err_delete_cec_adap;
2915 ret = cec_register_adapter(vc4_hdmi->cec_adap, &pdev->dev);
2917 goto err_delete_cec_adap;
2920 * NOTE: Strictly speaking, we should probably use a DRM-managed
2921 * registration there to avoid removing the CEC adapter by the
2922 * time the DRM driver doesn't have any user anymore.
2924 * However, the CEC framework already cleans up the CEC adapter
2925 * only when the last user has closed its file descriptor, so we
2926 * don't need to handle it in DRM.
2928 * By the time the device-managed hook is executed, we will give
2929 * up our reference to the CEC adapter and therefore don't
2930 * really care when it's actually freed.
2932 * There's still a problematic sequence: if we unregister our
2933 * CEC adapter, but the userspace keeps a handle on the CEC
2934 * adapter but not the DRM device for some reason. In such a
2935 * case, our vc4_hdmi structure will be freed, but the
2936 * cec_adapter structure will have a dangling pointer to what
2937 * used to be our HDMI controller. If we get a CEC call at that
2938 * moment, we could end up with a use-after-free. Fortunately,
2939 * the CEC framework already handles this too, by calling
2940 * cec_is_registered() in cec_ioctl() and cec_poll().
2942 ret = devm_add_action_or_reset(dev, vc4_hdmi_cec_release, vc4_hdmi);
2948 err_delete_cec_adap:
2949 cec_delete_adapter(vc4_hdmi->cec_adap);
2954 static int vc4_hdmi_cec_init(struct vc4_hdmi *vc4_hdmi)
2960 static void vc4_hdmi_free_regset(struct drm_device *drm, void *ptr)
2962 struct debugfs_reg32 *regs = ptr;
2967 static int vc4_hdmi_build_regset(struct drm_device *drm,
2968 struct vc4_hdmi *vc4_hdmi,
2969 struct debugfs_regset32 *regset,
2970 enum vc4_hdmi_regs reg)
2972 const struct vc4_hdmi_variant *variant = vc4_hdmi->variant;
2973 struct debugfs_reg32 *regs, *new_regs;
2974 unsigned int count = 0;
2978 regs = kcalloc(variant->num_registers, sizeof(*regs),
2983 for (i = 0; i < variant->num_registers; i++) {
2984 const struct vc4_hdmi_register *field = &variant->registers[i];
2986 if (field->reg != reg)
2989 regs[count].name = field->name;
2990 regs[count].offset = field->offset;
2994 new_regs = krealloc(regs, count * sizeof(*regs), GFP_KERNEL);
2998 regset->base = __vc4_hdmi_get_field_base(vc4_hdmi, reg);
2999 regset->regs = new_regs;
3000 regset->nregs = count;
3002 ret = drmm_add_action_or_reset(drm, vc4_hdmi_free_regset, new_regs);
3009 static int vc4_hdmi_init_resources(struct drm_device *drm,
3010 struct vc4_hdmi *vc4_hdmi)
3012 struct platform_device *pdev = vc4_hdmi->pdev;
3013 struct device *dev = &pdev->dev;
3016 vc4_hdmi->hdmicore_regs = vc4_ioremap_regs(pdev, 0);
3017 if (IS_ERR(vc4_hdmi->hdmicore_regs))
3018 return PTR_ERR(vc4_hdmi->hdmicore_regs);
3020 vc4_hdmi->hd_regs = vc4_ioremap_regs(pdev, 1);
3021 if (IS_ERR(vc4_hdmi->hd_regs))
3022 return PTR_ERR(vc4_hdmi->hd_regs);
3024 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->hd_regset, VC4_HD);
3028 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->hdmi_regset, VC4_HDMI);
3032 vc4_hdmi->pixel_clock = devm_clk_get(dev, "pixel");
3033 if (IS_ERR(vc4_hdmi->pixel_clock)) {
3034 ret = PTR_ERR(vc4_hdmi->pixel_clock);
3035 if (ret != -EPROBE_DEFER)
3036 DRM_ERROR("Failed to get pixel clock\n");
3040 vc4_hdmi->hsm_clock = devm_clk_get(dev, "hdmi");
3041 if (IS_ERR(vc4_hdmi->hsm_clock)) {
3042 DRM_ERROR("Failed to get HDMI state machine clock\n");
3043 return PTR_ERR(vc4_hdmi->hsm_clock);
3045 vc4_hdmi->audio_clock = vc4_hdmi->hsm_clock;
3046 vc4_hdmi->cec_clock = vc4_hdmi->hsm_clock;
3051 static int vc5_hdmi_init_resources(struct drm_device *drm,
3052 struct vc4_hdmi *vc4_hdmi)
3054 struct platform_device *pdev = vc4_hdmi->pdev;
3055 struct device *dev = &pdev->dev;
3056 struct resource *res;
3059 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hdmi");
3063 vc4_hdmi->hdmicore_regs = devm_ioremap(dev, res->start,
3064 resource_size(res));
3065 if (!vc4_hdmi->hdmicore_regs)
3068 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hd");
3072 vc4_hdmi->hd_regs = devm_ioremap(dev, res->start, resource_size(res));
3073 if (!vc4_hdmi->hd_regs)
3076 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cec");
3080 vc4_hdmi->cec_regs = devm_ioremap(dev, res->start, resource_size(res));
3081 if (!vc4_hdmi->cec_regs)
3084 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "csc");
3088 vc4_hdmi->csc_regs = devm_ioremap(dev, res->start, resource_size(res));
3089 if (!vc4_hdmi->csc_regs)
3092 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dvp");
3096 vc4_hdmi->dvp_regs = devm_ioremap(dev, res->start, resource_size(res));
3097 if (!vc4_hdmi->dvp_regs)
3100 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy");
3104 vc4_hdmi->phy_regs = devm_ioremap(dev, res->start, resource_size(res));
3105 if (!vc4_hdmi->phy_regs)
3108 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "packet");
3112 vc4_hdmi->ram_regs = devm_ioremap(dev, res->start, resource_size(res));
3113 if (!vc4_hdmi->ram_regs)
3116 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rm");
3120 vc4_hdmi->rm_regs = devm_ioremap(dev, res->start, resource_size(res));
3121 if (!vc4_hdmi->rm_regs)
3124 vc4_hdmi->hsm_clock = devm_clk_get(dev, "hdmi");
3125 if (IS_ERR(vc4_hdmi->hsm_clock)) {
3126 DRM_ERROR("Failed to get HDMI state machine clock\n");
3127 return PTR_ERR(vc4_hdmi->hsm_clock);
3130 vc4_hdmi->pixel_bvb_clock = devm_clk_get(dev, "bvb");
3131 if (IS_ERR(vc4_hdmi->pixel_bvb_clock)) {
3132 DRM_ERROR("Failed to get pixel bvb clock\n");
3133 return PTR_ERR(vc4_hdmi->pixel_bvb_clock);
3136 vc4_hdmi->audio_clock = devm_clk_get(dev, "audio");
3137 if (IS_ERR(vc4_hdmi->audio_clock)) {
3138 DRM_ERROR("Failed to get audio clock\n");
3139 return PTR_ERR(vc4_hdmi->audio_clock);
3142 vc4_hdmi->cec_clock = devm_clk_get(dev, "cec");
3143 if (IS_ERR(vc4_hdmi->cec_clock)) {
3144 DRM_ERROR("Failed to get CEC clock\n");
3145 return PTR_ERR(vc4_hdmi->cec_clock);
3148 vc4_hdmi->reset = devm_reset_control_get(dev, NULL);
3149 if (IS_ERR(vc4_hdmi->reset)) {
3150 DRM_ERROR("Failed to get HDMI reset line\n");
3151 return PTR_ERR(vc4_hdmi->reset);
3154 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->hdmi_regset, VC4_HDMI);
3158 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->hd_regset, VC4_HD);
3162 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->cec_regset, VC5_CEC);
3166 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->csc_regset, VC5_CSC);
3170 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->dvp_regset, VC5_DVP);
3174 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->phy_regset, VC5_PHY);
3178 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->ram_regset, VC5_RAM);
3182 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->rm_regset, VC5_RM);
3189 static int vc4_hdmi_runtime_suspend(struct device *dev)
3191 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
3193 clk_disable_unprepare(vc4_hdmi->hsm_clock);
3198 static int vc4_hdmi_runtime_resume(struct device *dev)
3200 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
3201 unsigned long __maybe_unused flags;
3202 u32 __maybe_unused value;
3205 ret = clk_prepare_enable(vc4_hdmi->hsm_clock);
3209 if (vc4_hdmi->variant->reset)
3210 vc4_hdmi->variant->reset(vc4_hdmi);
3212 #ifdef CONFIG_DRM_VC4_HDMI_CEC
3213 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
3214 value = HDMI_READ(HDMI_CEC_CNTRL_1);
3215 /* Set the logical address to Unregistered */
3216 value |= VC4_HDMI_CEC_ADDR_MASK;
3217 HDMI_WRITE(HDMI_CEC_CNTRL_1, value);
3218 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
3220 vc4_hdmi_cec_update_clk_div(vc4_hdmi);
3222 if (!vc4_hdmi->variant->external_irq_controller) {
3223 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
3224 HDMI_WRITE(HDMI_CEC_CPU_MASK_SET, 0xffffffff);
3225 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
3232 static void vc4_hdmi_put_ddc_device(void *ptr)
3234 struct vc4_hdmi *vc4_hdmi = ptr;
3236 put_device(&vc4_hdmi->ddc->dev);
3239 static int vc4_hdmi_bind(struct device *dev, struct device *master, void *data)
3241 const struct vc4_hdmi_variant *variant = of_device_get_match_data(dev);
3242 struct platform_device *pdev = to_platform_device(dev);
3243 struct drm_device *drm = dev_get_drvdata(master);
3244 struct vc4_hdmi *vc4_hdmi;
3245 struct drm_encoder *encoder;
3246 struct device_node *ddc_node;
3249 vc4_hdmi = drmm_kzalloc(drm, sizeof(*vc4_hdmi), GFP_KERNEL);
3253 mutex_init(&vc4_hdmi->mutex);
3254 spin_lock_init(&vc4_hdmi->hw_lock);
3255 INIT_DELAYED_WORK(&vc4_hdmi->scrambling_work, vc4_hdmi_scrambling_wq);
3257 dev_set_drvdata(dev, vc4_hdmi);
3258 encoder = &vc4_hdmi->encoder.base;
3259 vc4_hdmi->encoder.type = variant->encoder_type;
3260 vc4_hdmi->encoder.pre_crtc_configure = vc4_hdmi_encoder_pre_crtc_configure;
3261 vc4_hdmi->encoder.pre_crtc_enable = vc4_hdmi_encoder_pre_crtc_enable;
3262 vc4_hdmi->encoder.post_crtc_enable = vc4_hdmi_encoder_post_crtc_enable;
3263 vc4_hdmi->encoder.post_crtc_disable = vc4_hdmi_encoder_post_crtc_disable;
3264 vc4_hdmi->encoder.post_crtc_powerdown = vc4_hdmi_encoder_post_crtc_powerdown;
3265 vc4_hdmi->pdev = pdev;
3266 vc4_hdmi->variant = variant;
3269 * Since we don't know the state of the controller and its
3270 * display (if any), let's assume it's always enabled.
3271 * vc4_hdmi_disable_scrambling() will thus run at boot, make
3272 * sure it's disabled, and avoid any inconsistency.
3274 if (variant->max_pixel_clock > HDMI_14_MAX_TMDS_CLK)
3275 vc4_hdmi->scdc_enabled = true;
3277 ret = variant->init_resources(drm, vc4_hdmi);
3281 ddc_node = of_parse_phandle(dev->of_node, "ddc", 0);
3283 DRM_ERROR("Failed to find ddc node in device tree\n");
3287 vc4_hdmi->ddc = of_find_i2c_adapter_by_node(ddc_node);
3288 of_node_put(ddc_node);
3289 if (!vc4_hdmi->ddc) {
3290 DRM_DEBUG("Failed to get ddc i2c adapter by node\n");
3291 return -EPROBE_DEFER;
3294 ret = devm_add_action_or_reset(dev, vc4_hdmi_put_ddc_device, vc4_hdmi);
3298 /* Only use the GPIO HPD pin if present in the DT, otherwise
3299 * we'll use the HDMI core's register.
3301 vc4_hdmi->hpd_gpio = devm_gpiod_get_optional(dev, "hpd", GPIOD_IN);
3302 if (IS_ERR(vc4_hdmi->hpd_gpio)) {
3303 return PTR_ERR(vc4_hdmi->hpd_gpio);
3306 vc4_hdmi->disable_wifi_frequencies =
3307 of_property_read_bool(dev->of_node, "wifi-2.4ghz-coexistence");
3309 if (variant->max_pixel_clock == 600000000) {
3310 struct vc4_dev *vc4 = to_vc4_dev(drm);
3311 long max_rate = clk_round_rate(vc4->hvs->core_clk, 550000000);
3313 if (max_rate < 550000000)
3314 vc4_hdmi->disable_4kp60 = true;
3317 ret = devm_pm_runtime_enable(dev);
3322 * We need to have the device powered up at this point to call
3323 * our reset hook and for the CEC init.
3325 ret = pm_runtime_resume_and_get(dev);
3329 if ((of_device_is_compatible(dev->of_node, "brcm,bcm2711-hdmi0") ||
3330 of_device_is_compatible(dev->of_node, "brcm,bcm2711-hdmi1")) &&
3331 HDMI_READ(HDMI_VID_CTL) & VC4_HD_VID_CTL_ENABLE) {
3332 clk_prepare_enable(vc4_hdmi->pixel_clock);
3333 clk_prepare_enable(vc4_hdmi->hsm_clock);
3334 clk_prepare_enable(vc4_hdmi->pixel_bvb_clock);
3337 ret = drmm_encoder_init(drm, encoder,
3339 DRM_MODE_ENCODER_TMDS,
3342 goto err_put_runtime_pm;
3344 drm_encoder_helper_add(encoder, &vc4_hdmi_encoder_helper_funcs);
3346 ret = vc4_hdmi_connector_init(drm, vc4_hdmi);
3348 goto err_put_runtime_pm;
3350 ret = vc4_hdmi_hotplug_init(vc4_hdmi);
3352 goto err_put_runtime_pm;
3354 ret = vc4_hdmi_cec_init(vc4_hdmi);
3356 goto err_put_runtime_pm;
3358 ret = vc4_hdmi_audio_init(vc4_hdmi);
3360 goto err_put_runtime_pm;
3362 vc4_debugfs_add_file(drm, variant->debugfs_name,
3363 vc4_hdmi_debugfs_regs,
3366 pm_runtime_put_sync(dev);
3371 pm_runtime_put_sync(dev);
3376 static const struct component_ops vc4_hdmi_ops = {
3377 .bind = vc4_hdmi_bind,
3380 static int vc4_hdmi_dev_probe(struct platform_device *pdev)
3382 return component_add(&pdev->dev, &vc4_hdmi_ops);
3385 static int vc4_hdmi_dev_remove(struct platform_device *pdev)
3387 component_del(&pdev->dev, &vc4_hdmi_ops);
3391 static const struct vc4_hdmi_variant bcm2835_variant = {
3392 .encoder_type = VC4_ENCODER_TYPE_HDMI0,
3393 .debugfs_name = "hdmi_regs",
3394 .card_name = "vc4-hdmi",
3395 .max_pixel_clock = 162000000,
3396 .registers = vc4_hdmi_fields,
3397 .num_registers = ARRAY_SIZE(vc4_hdmi_fields),
3399 .init_resources = vc4_hdmi_init_resources,
3400 .csc_setup = vc4_hdmi_csc_setup,
3401 .reset = vc4_hdmi_reset,
3402 .set_timings = vc4_hdmi_set_timings,
3403 .phy_init = vc4_hdmi_phy_init,
3404 .phy_disable = vc4_hdmi_phy_disable,
3405 .phy_rng_enable = vc4_hdmi_phy_rng_enable,
3406 .phy_rng_disable = vc4_hdmi_phy_rng_disable,
3407 .channel_map = vc4_hdmi_channel_map,
3408 .supports_hdr = false,
3411 static const struct vc4_hdmi_variant bcm2711_hdmi0_variant = {
3412 .encoder_type = VC4_ENCODER_TYPE_HDMI0,
3413 .debugfs_name = "hdmi0_regs",
3414 .card_name = "vc4-hdmi-0",
3415 .max_pixel_clock = 600000000,
3416 .registers = vc5_hdmi_hdmi0_fields,
3417 .num_registers = ARRAY_SIZE(vc5_hdmi_hdmi0_fields),
3418 .phy_lane_mapping = {
3424 .unsupported_odd_h_timings = true,
3425 .external_irq_controller = true,
3427 .init_resources = vc5_hdmi_init_resources,
3428 .csc_setup = vc5_hdmi_csc_setup,
3429 .reset = vc5_hdmi_reset,
3430 .set_timings = vc5_hdmi_set_timings,
3431 .phy_init = vc5_hdmi_phy_init,
3432 .phy_disable = vc5_hdmi_phy_disable,
3433 .phy_rng_enable = vc5_hdmi_phy_rng_enable,
3434 .phy_rng_disable = vc5_hdmi_phy_rng_disable,
3435 .channel_map = vc5_hdmi_channel_map,
3436 .supports_hdr = true,
3437 .hp_detect = vc5_hdmi_hp_detect,
3440 static const struct vc4_hdmi_variant bcm2711_hdmi1_variant = {
3441 .encoder_type = VC4_ENCODER_TYPE_HDMI1,
3442 .debugfs_name = "hdmi1_regs",
3443 .card_name = "vc4-hdmi-1",
3444 .max_pixel_clock = HDMI_14_MAX_TMDS_CLK,
3445 .registers = vc5_hdmi_hdmi1_fields,
3446 .num_registers = ARRAY_SIZE(vc5_hdmi_hdmi1_fields),
3447 .phy_lane_mapping = {
3453 .unsupported_odd_h_timings = true,
3454 .external_irq_controller = true,
3456 .init_resources = vc5_hdmi_init_resources,
3457 .csc_setup = vc5_hdmi_csc_setup,
3458 .reset = vc5_hdmi_reset,
3459 .set_timings = vc5_hdmi_set_timings,
3460 .phy_init = vc5_hdmi_phy_init,
3461 .phy_disable = vc5_hdmi_phy_disable,
3462 .phy_rng_enable = vc5_hdmi_phy_rng_enable,
3463 .phy_rng_disable = vc5_hdmi_phy_rng_disable,
3464 .channel_map = vc5_hdmi_channel_map,
3465 .supports_hdr = true,
3466 .hp_detect = vc5_hdmi_hp_detect,
3469 static const struct of_device_id vc4_hdmi_dt_match[] = {
3470 { .compatible = "brcm,bcm2835-hdmi", .data = &bcm2835_variant },
3471 { .compatible = "brcm,bcm2711-hdmi0", .data = &bcm2711_hdmi0_variant },
3472 { .compatible = "brcm,bcm2711-hdmi1", .data = &bcm2711_hdmi1_variant },
3476 static const struct dev_pm_ops vc4_hdmi_pm_ops = {
3477 SET_RUNTIME_PM_OPS(vc4_hdmi_runtime_suspend,
3478 vc4_hdmi_runtime_resume,
3482 struct platform_driver vc4_hdmi_driver = {
3483 .probe = vc4_hdmi_dev_probe,
3484 .remove = vc4_hdmi_dev_remove,
3487 .of_match_table = vc4_hdmi_dt_match,
3488 .pm = &vc4_hdmi_pm_ops,