Thomas Gleixner | caab277 | 2019-06-03 07:44:50 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 2 | /* |
| 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> |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | /** |
| 10 | * DOC: VC4 Falcon HDMI module |
| 11 | * |
Eric Anholt | f6c0153 | 2017-02-27 12:11:43 -0800 | [diff] [blame] | 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. |
| 15 | * |
| 16 | * HDMI infoframes are kept within a small packet ram, where each |
| 17 | * packet can be individually enabled for including in a frame. |
| 18 | * |
| 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 |
| 23 | * blank regions. |
| 24 | * |
| 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. |
| 29 | * |
| 30 | * The driver does not yet support CEC control, though the HDMI |
| 31 | * encoder block has CEC support. |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 32 | */ |
| 33 | |
Masahiro Yamada | b7e8e25 | 2017-05-18 13:29:38 +0900 | [diff] [blame] | 34 | #include <drm/drm_atomic_helper.h> |
Masahiro Yamada | b7e8e25 | 2017-05-18 13:29:38 +0900 | [diff] [blame] | 35 | #include <drm/drm_edid.h> |
Daniel Vetter | fcd70cd | 2019-01-17 22:03:34 +0100 | [diff] [blame] | 36 | #include <drm/drm_probe_helper.h> |
Thomas Zimmermann | f6ebc1b | 2020-03-05 16:59:46 +0100 | [diff] [blame] | 37 | #include <drm/drm_simple_kms_helper.h> |
Maxime Ripard | c85695a | 2021-05-07 17:05:13 +0200 | [diff] [blame^] | 38 | #include <drm/drm_scdc_helper.h> |
Masahiro Yamada | b7e8e25 | 2017-05-18 13:29:38 +0900 | [diff] [blame] | 39 | #include <linux/clk.h> |
| 40 | #include <linux/component.h> |
| 41 | #include <linux/i2c.h> |
| 42 | #include <linux/of_address.h> |
| 43 | #include <linux/of_gpio.h> |
| 44 | #include <linux/of_platform.h> |
| 45 | #include <linux/pm_runtime.h> |
| 46 | #include <linux/rational.h> |
Maxime Ripard | 8323989 | 2020-09-03 10:01:48 +0200 | [diff] [blame] | 47 | #include <linux/reset.h> |
Masahiro Yamada | b7e8e25 | 2017-05-18 13:29:38 +0900 | [diff] [blame] | 48 | #include <sound/dmaengine_pcm.h> |
| 49 | #include <sound/pcm_drm_eld.h> |
| 50 | #include <sound/pcm_params.h> |
| 51 | #include <sound/soc.h> |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 52 | #include "media/cec.h" |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 53 | #include "vc4_drv.h" |
Maxime Ripard | f73100c | 2020-09-03 10:01:11 +0200 | [diff] [blame] | 54 | #include "vc4_hdmi.h" |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 55 | #include "vc4_hdmi_regs.h" |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 56 | #include "vc4_regs.h" |
| 57 | |
Maxime Ripard | 8323989 | 2020-09-03 10:01:48 +0200 | [diff] [blame] | 58 | #define VC5_HDMI_HORZA_HFP_SHIFT 16 |
| 59 | #define VC5_HDMI_HORZA_HFP_MASK VC4_MASK(28, 16) |
| 60 | #define VC5_HDMI_HORZA_VPOS BIT(15) |
| 61 | #define VC5_HDMI_HORZA_HPOS BIT(14) |
| 62 | #define VC5_HDMI_HORZA_HAP_SHIFT 0 |
| 63 | #define VC5_HDMI_HORZA_HAP_MASK VC4_MASK(13, 0) |
| 64 | |
| 65 | #define VC5_HDMI_HORZB_HBP_SHIFT 16 |
| 66 | #define VC5_HDMI_HORZB_HBP_MASK VC4_MASK(26, 16) |
| 67 | #define VC5_HDMI_HORZB_HSP_SHIFT 0 |
| 68 | #define VC5_HDMI_HORZB_HSP_MASK VC4_MASK(10, 0) |
| 69 | |
| 70 | #define VC5_HDMI_VERTA_VSP_SHIFT 24 |
| 71 | #define VC5_HDMI_VERTA_VSP_MASK VC4_MASK(28, 24) |
| 72 | #define VC5_HDMI_VERTA_VFP_SHIFT 16 |
| 73 | #define VC5_HDMI_VERTA_VFP_MASK VC4_MASK(22, 16) |
| 74 | #define VC5_HDMI_VERTA_VAL_SHIFT 0 |
| 75 | #define VC5_HDMI_VERTA_VAL_MASK VC4_MASK(12, 0) |
| 76 | |
| 77 | #define VC5_HDMI_VERTB_VSPO_SHIFT 16 |
| 78 | #define VC5_HDMI_VERTB_VSPO_MASK VC4_MASK(29, 16) |
| 79 | |
Maxime Ripard | c85695a | 2021-05-07 17:05:13 +0200 | [diff] [blame^] | 80 | #define VC5_HDMI_SCRAMBLER_CTL_ENABLE BIT(0) |
| 81 | |
Maxime Ripard | ba8c0fa | 2020-12-15 16:42:43 +0100 | [diff] [blame] | 82 | #define VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE_SHIFT 8 |
| 83 | #define VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE_MASK VC4_MASK(10, 8) |
| 84 | |
| 85 | #define VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH_SHIFT 0 |
| 86 | #define VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH_MASK VC4_MASK(3, 0) |
| 87 | |
| 88 | #define VC5_HDMI_GCP_CONFIG_GCP_ENABLE BIT(31) |
| 89 | |
| 90 | #define VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1_SHIFT 8 |
| 91 | #define VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1_MASK VC4_MASK(15, 8) |
| 92 | |
Maxime Ripard | 8323989 | 2020-09-03 10:01:48 +0200 | [diff] [blame] | 93 | # define VC4_HD_M_SW_RST BIT(2) |
| 94 | # define VC4_HD_M_ENABLE BIT(0) |
| 95 | |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 96 | #define CEC_CLOCK_FREQ 40000 |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 97 | |
Maxime Ripard | 24169a2 | 2020-12-15 16:42:42 +0100 | [diff] [blame] | 98 | #define HDMI_14_MAX_TMDS_CLK (340 * 1000 * 1000) |
| 99 | |
Maxime Ripard | 86e3a65 | 2021-05-07 17:05:12 +0200 | [diff] [blame] | 100 | static bool vc4_hdmi_mode_needs_scrambling(const struct drm_display_mode *mode) |
| 101 | { |
| 102 | return (mode->clock * 1000) > HDMI_14_MAX_TMDS_CLK; |
| 103 | } |
| 104 | |
Eric Anholt | c9be804 | 2019-04-01 11:35:58 -0700 | [diff] [blame] | 105 | static int vc4_hdmi_debugfs_regs(struct seq_file *m, void *unused) |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 106 | { |
| 107 | struct drm_info_node *node = (struct drm_info_node *)m->private; |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 108 | struct vc4_hdmi *vc4_hdmi = node->info_ent->data; |
Eric Anholt | 3051719 | 2019-02-20 13:03:38 -0800 | [diff] [blame] | 109 | struct drm_printer p = drm_seq_file_printer(m); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 110 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 111 | drm_print_regset32(&p, &vc4_hdmi->hdmi_regset); |
| 112 | drm_print_regset32(&p, &vc4_hdmi->hd_regset); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 113 | |
| 114 | return 0; |
| 115 | } |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 116 | |
Maxime Ripard | 9045e91 | 2020-09-03 10:01:24 +0200 | [diff] [blame] | 117 | static void vc4_hdmi_reset(struct vc4_hdmi *vc4_hdmi) |
| 118 | { |
| 119 | HDMI_WRITE(HDMI_M_CTL, VC4_HD_M_SW_RST); |
| 120 | udelay(1); |
| 121 | HDMI_WRITE(HDMI_M_CTL, 0); |
| 122 | |
| 123 | HDMI_WRITE(HDMI_M_CTL, VC4_HD_M_ENABLE); |
| 124 | |
| 125 | HDMI_WRITE(HDMI_SW_RESET_CONTROL, |
| 126 | VC4_HDMI_SW_RESET_HDMI | |
| 127 | VC4_HDMI_SW_RESET_FORMAT_DETECT); |
| 128 | |
| 129 | HDMI_WRITE(HDMI_SW_RESET_CONTROL, 0); |
| 130 | } |
| 131 | |
Maxime Ripard | 8323989 | 2020-09-03 10:01:48 +0200 | [diff] [blame] | 132 | static void vc5_hdmi_reset(struct vc4_hdmi *vc4_hdmi) |
| 133 | { |
| 134 | reset_control_reset(vc4_hdmi->reset); |
| 135 | |
| 136 | HDMI_WRITE(HDMI_DVP_CTL, 0); |
| 137 | |
| 138 | HDMI_WRITE(HDMI_CLOCK_STOP, |
| 139 | HDMI_READ(HDMI_CLOCK_STOP) | VC4_DVP_HT_CLOCK_STOP_PIXEL); |
| 140 | } |
| 141 | |
Maxime Ripard | 47fa9a8 | 2021-01-11 15:23:01 +0100 | [diff] [blame] | 142 | #ifdef CONFIG_DRM_VC4_HDMI_CEC |
| 143 | static void vc4_hdmi_cec_update_clk_div(struct vc4_hdmi *vc4_hdmi) |
| 144 | { |
| 145 | u16 clk_cnt; |
| 146 | u32 value; |
| 147 | |
| 148 | value = HDMI_READ(HDMI_CEC_CNTRL_1); |
| 149 | value &= ~VC4_HDMI_CEC_DIV_CLK_CNT_MASK; |
| 150 | |
| 151 | /* |
| 152 | * Set the clock divider: the hsm_clock rate and this divider |
| 153 | * setting will give a 40 kHz CEC clock. |
| 154 | */ |
Maxime Ripard | 23b7eb5 | 2021-01-11 15:23:02 +0100 | [diff] [blame] | 155 | clk_cnt = clk_get_rate(vc4_hdmi->cec_clock) / CEC_CLOCK_FREQ; |
Maxime Ripard | 47fa9a8 | 2021-01-11 15:23:01 +0100 | [diff] [blame] | 156 | value |= clk_cnt << VC4_HDMI_CEC_DIV_CLK_CNT_SHIFT; |
| 157 | HDMI_WRITE(HDMI_CEC_CNTRL_1, value); |
| 158 | } |
| 159 | #else |
| 160 | static void vc4_hdmi_cec_update_clk_div(struct vc4_hdmi *vc4_hdmi) {} |
| 161 | #endif |
| 162 | |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 163 | static enum drm_connector_status |
| 164 | vc4_hdmi_connector_detect(struct drm_connector *connector, bool force) |
| 165 | { |
Maxime Ripard | 5dfbcae | 2020-09-03 10:01:17 +0200 | [diff] [blame] | 166 | struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector); |
Dom Cobley | 4d8602b | 2021-01-11 15:22:59 +0100 | [diff] [blame] | 167 | bool connected = false; |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 168 | |
Maxime Ripard | b10db9a | 2020-09-03 10:01:16 +0200 | [diff] [blame] | 169 | if (vc4_hdmi->hpd_gpio) { |
| 170 | if (gpio_get_value_cansleep(vc4_hdmi->hpd_gpio) ^ |
| 171 | vc4_hdmi->hpd_active_low) |
Dom Cobley | 4d8602b | 2021-01-11 15:22:59 +0100 | [diff] [blame] | 172 | connected = true; |
| 173 | } else if (drm_probe_ddc(vc4_hdmi->ddc)) { |
| 174 | connected = true; |
| 175 | } else if (HDMI_READ(HDMI_HOTPLUG) & VC4_HDMI_HOTPLUG_CONNECTED) { |
| 176 | connected = true; |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Dom Cobley | 4d8602b | 2021-01-11 15:22:59 +0100 | [diff] [blame] | 179 | if (connected) { |
| 180 | if (connector->status != connector_status_connected) { |
| 181 | struct edid *edid = drm_get_edid(connector, vc4_hdmi->ddc); |
Eric Anholt | 9d44abb | 2016-09-14 19:21:29 +0100 | [diff] [blame] | 182 | |
Dom Cobley | 4d8602b | 2021-01-11 15:22:59 +0100 | [diff] [blame] | 183 | if (edid) { |
| 184 | cec_s_phys_addr_from_edid(vc4_hdmi->cec_adap, edid); |
| 185 | vc4_hdmi->encoder.hdmi_monitor = drm_detect_hdmi_monitor(edid); |
| 186 | kfree(edid); |
| 187 | } |
| 188 | } |
| 189 | |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 190 | return connector_status_connected; |
Dom Cobley | 4d8602b | 2021-01-11 15:22:59 +0100 | [diff] [blame] | 191 | } |
| 192 | |
Maxime Ripard | b10db9a | 2020-09-03 10:01:16 +0200 | [diff] [blame] | 193 | cec_phys_addr_invalidate(vc4_hdmi->cec_adap); |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 194 | return connector_status_disconnected; |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | static void vc4_hdmi_connector_destroy(struct drm_connector *connector) |
| 198 | { |
| 199 | drm_connector_unregister(connector); |
| 200 | drm_connector_cleanup(connector); |
| 201 | } |
| 202 | |
| 203 | static int vc4_hdmi_connector_get_modes(struct drm_connector *connector) |
| 204 | { |
Maxime Ripard | 5dfbcae | 2020-09-03 10:01:17 +0200 | [diff] [blame] | 205 | struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector); |
| 206 | struct vc4_hdmi_encoder *vc4_encoder = &vc4_hdmi->encoder; |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 207 | int ret = 0; |
| 208 | struct edid *edid; |
| 209 | |
Maxime Ripard | 5dfbcae | 2020-09-03 10:01:17 +0200 | [diff] [blame] | 210 | edid = drm_get_edid(connector, vc4_hdmi->ddc); |
| 211 | cec_s_phys_addr_from_edid(vc4_hdmi->cec_adap, edid); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 212 | if (!edid) |
| 213 | return -ENODEV; |
| 214 | |
| 215 | vc4_encoder->hdmi_monitor = drm_detect_hdmi_monitor(edid); |
Eric Anholt | 21317b3 | 2016-09-29 15:34:43 -0700 | [diff] [blame] | 216 | |
Daniel Vetter | c555f02 | 2018-07-09 10:40:06 +0200 | [diff] [blame] | 217 | drm_connector_update_edid_property(connector, edid); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 218 | ret = drm_add_edid_modes(connector, edid); |
Eric Anholt | 5afe0e6 | 2017-08-08 13:56:05 -0700 | [diff] [blame] | 219 | kfree(edid); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 220 | |
Maxime Ripard | 86e3a65 | 2021-05-07 17:05:12 +0200 | [diff] [blame] | 221 | if (vc4_hdmi->disable_4kp60) { |
| 222 | struct drm_device *drm = connector->dev; |
| 223 | struct drm_display_mode *mode; |
| 224 | |
| 225 | list_for_each_entry(mode, &connector->probed_modes, head) { |
| 226 | if (vc4_hdmi_mode_needs_scrambling(mode)) { |
| 227 | drm_warn_once(drm, "The core clock cannot reach frequencies high enough to support 4k @ 60Hz."); |
| 228 | drm_warn_once(drm, "Please change your config.txt file to add hdmi_enable_4kp60."); |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 233 | return ret; |
| 234 | } |
| 235 | |
Dave Stevenson | bccd5c5 | 2021-04-30 11:44:49 +0200 | [diff] [blame] | 236 | static int vc4_hdmi_connector_atomic_check(struct drm_connector *connector, |
| 237 | struct drm_atomic_state *state) |
| 238 | { |
| 239 | struct drm_connector_state *old_state = |
| 240 | drm_atomic_get_old_connector_state(state, connector); |
| 241 | struct drm_connector_state *new_state = |
| 242 | drm_atomic_get_new_connector_state(state, connector); |
| 243 | struct drm_crtc *crtc = new_state->crtc; |
| 244 | |
| 245 | if (!crtc) |
| 246 | return 0; |
| 247 | |
Maxime Ripard | 76a262d | 2021-04-30 11:44:51 +0200 | [diff] [blame] | 248 | if (old_state->colorspace != new_state->colorspace || |
| 249 | !drm_connector_atomic_hdr_metadata_equal(old_state, new_state)) { |
Dave Stevenson | bccd5c5 | 2021-04-30 11:44:49 +0200 | [diff] [blame] | 250 | struct drm_crtc_state *crtc_state; |
| 251 | |
| 252 | crtc_state = drm_atomic_get_crtc_state(state, crtc); |
| 253 | if (IS_ERR(crtc_state)) |
| 254 | return PTR_ERR(crtc_state); |
| 255 | |
| 256 | crtc_state->mode_changed = true; |
| 257 | } |
| 258 | |
| 259 | return 0; |
| 260 | } |
| 261 | |
Maxime Ripard | 90b2df5 | 2019-06-19 12:17:53 +0200 | [diff] [blame] | 262 | static void vc4_hdmi_connector_reset(struct drm_connector *connector) |
| 263 | { |
Maxime Ripard | fbe7271 | 2020-12-15 16:42:39 +0100 | [diff] [blame] | 264 | struct vc4_hdmi_connector_state *old_state = |
| 265 | conn_state_to_vc4_hdmi_conn_state(connector->state); |
| 266 | struct vc4_hdmi_connector_state *new_state = |
| 267 | kzalloc(sizeof(*new_state), GFP_KERNEL); |
Maxime Ripard | e55a077 | 2020-12-15 16:42:38 +0100 | [diff] [blame] | 268 | |
| 269 | if (connector->state) |
Maxime Ripard | fbe7271 | 2020-12-15 16:42:39 +0100 | [diff] [blame] | 270 | __drm_atomic_helper_connector_destroy_state(connector->state); |
| 271 | |
| 272 | kfree(old_state); |
| 273 | __drm_atomic_helper_connector_reset(connector, &new_state->base); |
| 274 | |
| 275 | if (!new_state) |
| 276 | return; |
| 277 | |
Maxime Ripard | ba8c0fa | 2020-12-15 16:42:43 +0100 | [diff] [blame] | 278 | new_state->base.max_bpc = 8; |
| 279 | new_state->base.max_requested_bpc = 8; |
Maxime Ripard | fbe7271 | 2020-12-15 16:42:39 +0100 | [diff] [blame] | 280 | drm_atomic_helper_connector_tv_reset(connector); |
| 281 | } |
| 282 | |
| 283 | static struct drm_connector_state * |
| 284 | vc4_hdmi_connector_duplicate_state(struct drm_connector *connector) |
| 285 | { |
| 286 | struct drm_connector_state *conn_state = connector->state; |
| 287 | struct vc4_hdmi_connector_state *vc4_state = conn_state_to_vc4_hdmi_conn_state(conn_state); |
| 288 | struct vc4_hdmi_connector_state *new_state; |
| 289 | |
| 290 | new_state = kzalloc(sizeof(*new_state), GFP_KERNEL); |
| 291 | if (!new_state) |
| 292 | return NULL; |
| 293 | |
Maxime Ripard | f623746 | 2020-12-15 16:42:40 +0100 | [diff] [blame] | 294 | new_state->pixel_rate = vc4_state->pixel_rate; |
Maxime Ripard | fbe7271 | 2020-12-15 16:42:39 +0100 | [diff] [blame] | 295 | __drm_atomic_helper_connector_duplicate_state(connector, &new_state->base); |
| 296 | |
| 297 | return &new_state->base; |
Maxime Ripard | 90b2df5 | 2019-06-19 12:17:53 +0200 | [diff] [blame] | 298 | } |
| 299 | |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 300 | static const struct drm_connector_funcs vc4_hdmi_connector_funcs = { |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 301 | .detect = vc4_hdmi_connector_detect, |
Eric Anholt | 682e62c | 2016-09-28 17:30:25 -0700 | [diff] [blame] | 302 | .fill_modes = drm_helper_probe_single_connector_modes, |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 303 | .destroy = vc4_hdmi_connector_destroy, |
Maxime Ripard | 90b2df5 | 2019-06-19 12:17:53 +0200 | [diff] [blame] | 304 | .reset = vc4_hdmi_connector_reset, |
Maxime Ripard | fbe7271 | 2020-12-15 16:42:39 +0100 | [diff] [blame] | 305 | .atomic_duplicate_state = vc4_hdmi_connector_duplicate_state, |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 306 | .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, |
| 307 | }; |
| 308 | |
| 309 | static const struct drm_connector_helper_funcs vc4_hdmi_connector_helper_funcs = { |
| 310 | .get_modes = vc4_hdmi_connector_get_modes, |
Dave Stevenson | bccd5c5 | 2021-04-30 11:44:49 +0200 | [diff] [blame] | 311 | .atomic_check = vc4_hdmi_connector_atomic_check, |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 312 | }; |
| 313 | |
Maxime Ripard | c98c85b | 2020-09-03 10:01:12 +0200 | [diff] [blame] | 314 | static int vc4_hdmi_connector_init(struct drm_device *dev, |
Maxime Ripard | b052e70 | 2020-09-03 10:01:13 +0200 | [diff] [blame] | 315 | struct vc4_hdmi *vc4_hdmi) |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 316 | { |
Maxime Ripard | 0532e5e | 2020-09-03 10:01:21 +0200 | [diff] [blame] | 317 | struct drm_connector *connector = &vc4_hdmi->connector; |
Maxime Ripard | c98c85b | 2020-09-03 10:01:12 +0200 | [diff] [blame] | 318 | struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base; |
Boris Brezillon | db99953 | 2018-12-06 15:24:39 +0100 | [diff] [blame] | 319 | int ret; |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 320 | |
Andrzej Pietrasiewicz | 04a880f | 2020-01-02 14:22:58 +0100 | [diff] [blame] | 321 | drm_connector_init_with_ddc(dev, connector, |
| 322 | &vc4_hdmi_connector_funcs, |
| 323 | DRM_MODE_CONNECTOR_HDMIA, |
Maxime Ripard | b052e70 | 2020-09-03 10:01:13 +0200 | [diff] [blame] | 324 | vc4_hdmi->ddc); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 325 | drm_connector_helper_add(connector, &vc4_hdmi_connector_helper_funcs); |
| 326 | |
Maxime Ripard | ba8c0fa | 2020-12-15 16:42:43 +0100 | [diff] [blame] | 327 | /* |
| 328 | * Some of the properties below require access to state, like bpc. |
| 329 | * Allocate some default initial connector state with our reset helper. |
| 330 | */ |
| 331 | if (connector->funcs->reset) |
| 332 | connector->funcs->reset(connector); |
| 333 | |
Boris Brezillon | db99953 | 2018-12-06 15:24:39 +0100 | [diff] [blame] | 334 | /* Create and attach TV margin props to this connector. */ |
| 335 | ret = drm_mode_create_tv_margin_properties(dev); |
| 336 | if (ret) |
Maxime Ripard | c98c85b | 2020-09-03 10:01:12 +0200 | [diff] [blame] | 337 | return ret; |
Boris Brezillon | db99953 | 2018-12-06 15:24:39 +0100 | [diff] [blame] | 338 | |
Maxime Ripard | 76a262d | 2021-04-30 11:44:51 +0200 | [diff] [blame] | 339 | ret = drm_mode_create_hdmi_colorspace_property(connector); |
| 340 | if (ret) |
| 341 | return ret; |
| 342 | |
| 343 | drm_connector_attach_colorspace_property(connector); |
Boris Brezillon | db99953 | 2018-12-06 15:24:39 +0100 | [diff] [blame] | 344 | drm_connector_attach_tv_margin_properties(connector); |
Maxime Ripard | ba8c0fa | 2020-12-15 16:42:43 +0100 | [diff] [blame] | 345 | drm_connector_attach_max_bpc_property(connector, 8, 12); |
Boris Brezillon | db99953 | 2018-12-06 15:24:39 +0100 | [diff] [blame] | 346 | |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 347 | connector->polled = (DRM_CONNECTOR_POLL_CONNECT | |
| 348 | DRM_CONNECTOR_POLL_DISCONNECT); |
| 349 | |
Mario Kleiner | acc1be1 | 2016-07-19 20:58:58 +0200 | [diff] [blame] | 350 | connector->interlace_allowed = 1; |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 351 | connector->doublescan_allowed = 0; |
| 352 | |
Dave Stevenson | bccd5c5 | 2021-04-30 11:44:49 +0200 | [diff] [blame] | 353 | if (vc4_hdmi->variant->supports_hdr) |
| 354 | drm_connector_attach_hdr_output_metadata_property(connector); |
| 355 | |
Daniel Vetter | cde4c44 | 2018-07-09 10:40:07 +0200 | [diff] [blame] | 356 | drm_connector_attach_encoder(connector, encoder); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 357 | |
Maxime Ripard | c98c85b | 2020-09-03 10:01:12 +0200 | [diff] [blame] | 358 | return 0; |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 359 | } |
| 360 | |
Eric Anholt | 21317b3 | 2016-09-29 15:34:43 -0700 | [diff] [blame] | 361 | static int vc4_hdmi_stop_packet(struct drm_encoder *encoder, |
Maxime Ripard | e2f9b2e | 2020-12-03 08:46:24 +0100 | [diff] [blame] | 362 | enum hdmi_infoframe_type type, |
| 363 | bool poll) |
Eric Anholt | 21317b3 | 2016-09-29 15:34:43 -0700 | [diff] [blame] | 364 | { |
Maxime Ripard | 5dfbcae | 2020-09-03 10:01:17 +0200 | [diff] [blame] | 365 | struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder); |
Eric Anholt | 21317b3 | 2016-09-29 15:34:43 -0700 | [diff] [blame] | 366 | u32 packet_id = type - 0x80; |
| 367 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 368 | HDMI_WRITE(HDMI_RAM_PACKET_CONFIG, |
| 369 | HDMI_READ(HDMI_RAM_PACKET_CONFIG) & ~BIT(packet_id)); |
Eric Anholt | 21317b3 | 2016-09-29 15:34:43 -0700 | [diff] [blame] | 370 | |
Maxime Ripard | e2f9b2e | 2020-12-03 08:46:24 +0100 | [diff] [blame] | 371 | if (!poll) |
| 372 | return 0; |
| 373 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 374 | return wait_for(!(HDMI_READ(HDMI_RAM_PACKET_STATUS) & |
Eric Anholt | 21317b3 | 2016-09-29 15:34:43 -0700 | [diff] [blame] | 375 | BIT(packet_id)), 100); |
| 376 | } |
| 377 | |
| 378 | static void vc4_hdmi_write_infoframe(struct drm_encoder *encoder, |
| 379 | union hdmi_infoframe *frame) |
| 380 | { |
Maxime Ripard | 5dfbcae | 2020-09-03 10:01:17 +0200 | [diff] [blame] | 381 | struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder); |
Eric Anholt | 21317b3 | 2016-09-29 15:34:43 -0700 | [diff] [blame] | 382 | u32 packet_id = frame->any.type - 0x80; |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 383 | const struct vc4_hdmi_register *ram_packet_start = |
| 384 | &vc4_hdmi->variant->registers[HDMI_RAM_PACKET_START]; |
| 385 | u32 packet_reg = ram_packet_start->offset + VC4_HDMI_PACKET_STRIDE * packet_id; |
| 386 | void __iomem *base = __vc4_hdmi_get_field_base(vc4_hdmi, |
| 387 | ram_packet_start->reg); |
Eric Anholt | 21317b3 | 2016-09-29 15:34:43 -0700 | [diff] [blame] | 388 | uint8_t buffer[VC4_HDMI_PACKET_STRIDE]; |
| 389 | ssize_t len, i; |
| 390 | int ret; |
| 391 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 392 | WARN_ONCE(!(HDMI_READ(HDMI_RAM_PACKET_CONFIG) & |
Eric Anholt | 21317b3 | 2016-09-29 15:34:43 -0700 | [diff] [blame] | 393 | VC4_HDMI_RAM_PACKET_ENABLE), |
| 394 | "Packet RAM has to be on to store the packet."); |
| 395 | |
| 396 | len = hdmi_infoframe_pack(frame, buffer, sizeof(buffer)); |
| 397 | if (len < 0) |
| 398 | return; |
| 399 | |
Maxime Ripard | e2f9b2e | 2020-12-03 08:46:24 +0100 | [diff] [blame] | 400 | ret = vc4_hdmi_stop_packet(encoder, frame->any.type, true); |
Eric Anholt | 21317b3 | 2016-09-29 15:34:43 -0700 | [diff] [blame] | 401 | if (ret) { |
| 402 | DRM_ERROR("Failed to wait for infoframe to go idle: %d\n", ret); |
| 403 | return; |
| 404 | } |
| 405 | |
| 406 | for (i = 0; i < len; i += 7) { |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 407 | writel(buffer[i + 0] << 0 | |
| 408 | buffer[i + 1] << 8 | |
| 409 | buffer[i + 2] << 16, |
| 410 | base + packet_reg); |
Eric Anholt | 21317b3 | 2016-09-29 15:34:43 -0700 | [diff] [blame] | 411 | packet_reg += 4; |
| 412 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 413 | writel(buffer[i + 3] << 0 | |
| 414 | buffer[i + 4] << 8 | |
| 415 | buffer[i + 5] << 16 | |
| 416 | buffer[i + 6] << 24, |
| 417 | base + packet_reg); |
Eric Anholt | 21317b3 | 2016-09-29 15:34:43 -0700 | [diff] [blame] | 418 | packet_reg += 4; |
| 419 | } |
| 420 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 421 | HDMI_WRITE(HDMI_RAM_PACKET_CONFIG, |
| 422 | HDMI_READ(HDMI_RAM_PACKET_CONFIG) | BIT(packet_id)); |
| 423 | ret = wait_for((HDMI_READ(HDMI_RAM_PACKET_STATUS) & |
Eric Anholt | 21317b3 | 2016-09-29 15:34:43 -0700 | [diff] [blame] | 424 | BIT(packet_id)), 100); |
| 425 | if (ret) |
| 426 | DRM_ERROR("Failed to wait for infoframe to start: %d\n", ret); |
| 427 | } |
| 428 | |
| 429 | static void vc4_hdmi_set_avi_infoframe(struct drm_encoder *encoder) |
| 430 | { |
Maxime Ripard | 5dfbcae | 2020-09-03 10:01:17 +0200 | [diff] [blame] | 431 | struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder); |
Eric Anholt | 21317b3 | 2016-09-29 15:34:43 -0700 | [diff] [blame] | 432 | struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder); |
Maxime Ripard | 0532e5e | 2020-09-03 10:01:21 +0200 | [diff] [blame] | 433 | struct drm_connector *connector = &vc4_hdmi->connector; |
Maxime Ripard | c98c85b | 2020-09-03 10:01:12 +0200 | [diff] [blame] | 434 | struct drm_connector_state *cstate = connector->state; |
Eric Anholt | 21317b3 | 2016-09-29 15:34:43 -0700 | [diff] [blame] | 435 | struct drm_crtc *crtc = encoder->crtc; |
| 436 | const struct drm_display_mode *mode = &crtc->state->adjusted_mode; |
| 437 | union hdmi_infoframe frame; |
| 438 | int ret; |
| 439 | |
Ville Syrjälä | 13d0add | 2019-01-08 19:28:25 +0200 | [diff] [blame] | 440 | ret = drm_hdmi_avi_infoframe_from_display_mode(&frame.avi, |
Maxime Ripard | c98c85b | 2020-09-03 10:01:12 +0200 | [diff] [blame] | 441 | connector, mode); |
Eric Anholt | 21317b3 | 2016-09-29 15:34:43 -0700 | [diff] [blame] | 442 | if (ret < 0) { |
| 443 | DRM_ERROR("couldn't fill AVI infoframe\n"); |
| 444 | return; |
| 445 | } |
| 446 | |
Ville Syrjälä | 13d0add | 2019-01-08 19:28:25 +0200 | [diff] [blame] | 447 | drm_hdmi_avi_infoframe_quant_range(&frame.avi, |
Maxime Ripard | c98c85b | 2020-09-03 10:01:12 +0200 | [diff] [blame] | 448 | connector, mode, |
Ville Syrjälä | a2ce26f | 2017-01-11 14:57:23 +0200 | [diff] [blame] | 449 | vc4_encoder->limited_rgb_range ? |
| 450 | HDMI_QUANTIZATION_RANGE_LIMITED : |
Ville Syrjälä | 1581b2d | 2019-01-08 19:28:28 +0200 | [diff] [blame] | 451 | HDMI_QUANTIZATION_RANGE_FULL); |
Maxime Ripard | 76a262d | 2021-04-30 11:44:51 +0200 | [diff] [blame] | 452 | drm_hdmi_avi_infoframe_colorspace(&frame.avi, cstate); |
Ville Syrjälä | cb87637 | 2019-10-08 19:48:14 +0300 | [diff] [blame] | 453 | drm_hdmi_avi_infoframe_bars(&frame.avi, cstate); |
Boris Brezillon | db99953 | 2018-12-06 15:24:39 +0100 | [diff] [blame] | 454 | |
Eric Anholt | 21317b3 | 2016-09-29 15:34:43 -0700 | [diff] [blame] | 455 | vc4_hdmi_write_infoframe(encoder, &frame); |
| 456 | } |
| 457 | |
| 458 | static void vc4_hdmi_set_spd_infoframe(struct drm_encoder *encoder) |
| 459 | { |
| 460 | union hdmi_infoframe frame; |
| 461 | int ret; |
| 462 | |
| 463 | ret = hdmi_spd_infoframe_init(&frame.spd, "Broadcom", "Videocore"); |
| 464 | if (ret < 0) { |
| 465 | DRM_ERROR("couldn't fill SPD infoframe\n"); |
| 466 | return; |
| 467 | } |
| 468 | |
| 469 | frame.spd.sdi = HDMI_SPD_SDI_PC; |
| 470 | |
| 471 | vc4_hdmi_write_infoframe(encoder, &frame); |
| 472 | } |
| 473 | |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 474 | static void vc4_hdmi_set_audio_infoframe(struct drm_encoder *encoder) |
| 475 | { |
Maxime Ripard | 5dfbcae | 2020-09-03 10:01:17 +0200 | [diff] [blame] | 476 | struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 477 | union hdmi_infoframe frame; |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 478 | |
Lee Jones | 2bac959 | 2020-11-16 17:41:08 +0000 | [diff] [blame] | 479 | hdmi_audio_infoframe_init(&frame.audio); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 480 | |
| 481 | frame.audio.coding_type = HDMI_AUDIO_CODING_TYPE_STREAM; |
| 482 | frame.audio.sample_frequency = HDMI_AUDIO_SAMPLE_FREQUENCY_STREAM; |
| 483 | frame.audio.sample_size = HDMI_AUDIO_SAMPLE_SIZE_STREAM; |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 484 | frame.audio.channels = vc4_hdmi->audio.channels; |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 485 | |
| 486 | vc4_hdmi_write_infoframe(encoder, &frame); |
| 487 | } |
| 488 | |
Dave Stevenson | bccd5c5 | 2021-04-30 11:44:49 +0200 | [diff] [blame] | 489 | static void vc4_hdmi_set_hdr_infoframe(struct drm_encoder *encoder) |
| 490 | { |
| 491 | struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder); |
| 492 | struct drm_connector *connector = &vc4_hdmi->connector; |
| 493 | struct drm_connector_state *conn_state = connector->state; |
| 494 | union hdmi_infoframe frame; |
| 495 | |
| 496 | if (!vc4_hdmi->variant->supports_hdr) |
| 497 | return; |
| 498 | |
| 499 | if (!conn_state->hdr_output_metadata) |
| 500 | return; |
| 501 | |
| 502 | if (drm_hdmi_infoframe_set_hdr_metadata(&frame.drm, conn_state)) |
| 503 | return; |
| 504 | |
| 505 | vc4_hdmi_write_infoframe(encoder, &frame); |
| 506 | } |
| 507 | |
Eric Anholt | 21317b3 | 2016-09-29 15:34:43 -0700 | [diff] [blame] | 508 | static void vc4_hdmi_set_infoframes(struct drm_encoder *encoder) |
| 509 | { |
Dave Stevenson | 6ac1c75 | 2020-09-03 10:01:38 +0200 | [diff] [blame] | 510 | struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder); |
| 511 | |
Eric Anholt | 21317b3 | 2016-09-29 15:34:43 -0700 | [diff] [blame] | 512 | vc4_hdmi_set_avi_infoframe(encoder); |
| 513 | vc4_hdmi_set_spd_infoframe(encoder); |
Dave Stevenson | 6ac1c75 | 2020-09-03 10:01:38 +0200 | [diff] [blame] | 514 | /* |
| 515 | * If audio was streaming, then we need to reenabled the audio |
| 516 | * infoframe here during encoder_enable. |
| 517 | */ |
| 518 | if (vc4_hdmi->audio.streaming) |
| 519 | vc4_hdmi_set_audio_infoframe(encoder); |
Dave Stevenson | bccd5c5 | 2021-04-30 11:44:49 +0200 | [diff] [blame] | 520 | |
| 521 | vc4_hdmi_set_hdr_infoframe(encoder); |
Eric Anholt | 21317b3 | 2016-09-29 15:34:43 -0700 | [diff] [blame] | 522 | } |
| 523 | |
Maxime Ripard | c85695a | 2021-05-07 17:05:13 +0200 | [diff] [blame^] | 524 | static bool vc4_hdmi_supports_scrambling(struct drm_encoder *encoder, |
| 525 | struct drm_display_mode *mode) |
| 526 | { |
| 527 | struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder); |
| 528 | struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder); |
| 529 | struct drm_display_info *display = &vc4_hdmi->connector.display_info; |
| 530 | |
| 531 | if (!vc4_encoder->hdmi_monitor) |
| 532 | return false; |
| 533 | |
| 534 | if (!display->hdmi.scdc.supported || |
| 535 | !display->hdmi.scdc.scrambling.supported) |
| 536 | return false; |
| 537 | |
| 538 | return true; |
| 539 | } |
| 540 | |
| 541 | static void vc4_hdmi_enable_scrambling(struct drm_encoder *encoder) |
| 542 | { |
| 543 | struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode; |
| 544 | struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder); |
| 545 | |
| 546 | if (!vc4_hdmi_supports_scrambling(encoder, mode)) |
| 547 | return; |
| 548 | |
| 549 | if (!vc4_hdmi_mode_needs_scrambling(mode)) |
| 550 | return; |
| 551 | |
| 552 | drm_scdc_set_high_tmds_clock_ratio(vc4_hdmi->ddc, true); |
| 553 | drm_scdc_set_scrambling(vc4_hdmi->ddc, true); |
| 554 | |
| 555 | HDMI_WRITE(HDMI_SCRAMBLER_CTL, HDMI_READ(HDMI_SCRAMBLER_CTL) | |
| 556 | VC5_HDMI_SCRAMBLER_CTL_ENABLE); |
| 557 | } |
| 558 | |
| 559 | static void vc4_hdmi_disable_scrambling(struct drm_encoder *encoder) |
| 560 | { |
| 561 | struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder); |
| 562 | struct drm_crtc *crtc = encoder->crtc; |
| 563 | |
| 564 | /* |
| 565 | * At boot, encoder->crtc will be NULL. Since we don't know the |
| 566 | * state of the scrambler and in order to avoid any |
| 567 | * inconsistency, let's disable it all the time. |
| 568 | */ |
| 569 | if (crtc && !vc4_hdmi_supports_scrambling(encoder, &crtc->mode)) |
| 570 | return; |
| 571 | |
| 572 | if (crtc && !vc4_hdmi_mode_needs_scrambling(&crtc->mode)) |
| 573 | return; |
| 574 | |
| 575 | HDMI_WRITE(HDMI_SCRAMBLER_CTL, HDMI_READ(HDMI_SCRAMBLER_CTL) & |
| 576 | ~VC5_HDMI_SCRAMBLER_CTL_ENABLE); |
| 577 | |
| 578 | drm_scdc_set_scrambling(vc4_hdmi->ddc, false); |
| 579 | drm_scdc_set_high_tmds_clock_ratio(vc4_hdmi->ddc, false); |
| 580 | } |
| 581 | |
Maxime Ripard | 8d91474 | 2020-12-15 16:42:36 +0100 | [diff] [blame] | 582 | static void vc4_hdmi_encoder_post_crtc_disable(struct drm_encoder *encoder, |
| 583 | struct drm_atomic_state *state) |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 584 | { |
Maxime Ripard | 09c4381 | 2020-09-03 10:01:44 +0200 | [diff] [blame] | 585 | struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder); |
| 586 | |
| 587 | HDMI_WRITE(HDMI_RAM_PACKET_CONFIG, 0); |
Maxime Ripard | 81d8301 | 2020-09-03 10:01:46 +0200 | [diff] [blame] | 588 | |
| 589 | HDMI_WRITE(HDMI_VID_CTL, HDMI_READ(HDMI_VID_CTL) | |
| 590 | VC4_HD_VID_CTL_CLRRGB | VC4_HD_VID_CTL_CLRSYNC); |
| 591 | |
| 592 | HDMI_WRITE(HDMI_VID_CTL, |
| 593 | HDMI_READ(HDMI_VID_CTL) | VC4_HD_VID_CTL_BLANKPIX); |
Maxime Ripard | c85695a | 2021-05-07 17:05:13 +0200 | [diff] [blame^] | 594 | |
| 595 | vc4_hdmi_disable_scrambling(encoder); |
Maxime Ripard | 09c4381 | 2020-09-03 10:01:44 +0200 | [diff] [blame] | 596 | } |
| 597 | |
Maxime Ripard | 8d91474 | 2020-12-15 16:42:36 +0100 | [diff] [blame] | 598 | static void vc4_hdmi_encoder_post_crtc_powerdown(struct drm_encoder *encoder, |
| 599 | struct drm_atomic_state *state) |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 600 | { |
Maxime Ripard | 5dfbcae | 2020-09-03 10:01:17 +0200 | [diff] [blame] | 601 | struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder); |
Boris Brezillon | 4f6e3d6 | 2017-04-11 18:39:25 +0200 | [diff] [blame] | 602 | int ret; |
| 603 | |
Maxime Ripard | c457b8a | 2020-09-03 10:01:25 +0200 | [diff] [blame] | 604 | if (vc4_hdmi->variant->phy_disable) |
| 605 | vc4_hdmi->variant->phy_disable(vc4_hdmi); |
Boris Brezillon | 4f6e3d6 | 2017-04-11 18:39:25 +0200 | [diff] [blame] | 606 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 607 | HDMI_WRITE(HDMI_VID_CTL, |
| 608 | HDMI_READ(HDMI_VID_CTL) & ~VC4_HD_VID_CTL_ENABLE); |
Boris Brezillon | 4f6e3d6 | 2017-04-11 18:39:25 +0200 | [diff] [blame] | 609 | |
Hoegeun Kwon | 3738742 | 2020-09-03 10:01:47 +0200 | [diff] [blame] | 610 | clk_disable_unprepare(vc4_hdmi->pixel_bvb_clock); |
Maxime Ripard | cd4cb49 | 2020-09-03 10:01:35 +0200 | [diff] [blame] | 611 | clk_disable_unprepare(vc4_hdmi->hsm_clock); |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 612 | clk_disable_unprepare(vc4_hdmi->pixel_clock); |
Boris Brezillon | 4f6e3d6 | 2017-04-11 18:39:25 +0200 | [diff] [blame] | 613 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 614 | ret = pm_runtime_put(&vc4_hdmi->pdev->dev); |
Boris Brezillon | 4f6e3d6 | 2017-04-11 18:39:25 +0200 | [diff] [blame] | 615 | if (ret < 0) |
| 616 | DRM_ERROR("Failed to release power domain: %d\n", ret); |
| 617 | } |
| 618 | |
Maxime Ripard | 09c4381 | 2020-09-03 10:01:44 +0200 | [diff] [blame] | 619 | static void vc4_hdmi_encoder_disable(struct drm_encoder *encoder) |
Boris Brezillon | 4f6e3d6 | 2017-04-11 18:39:25 +0200 | [diff] [blame] | 620 | { |
Maxime Ripard | 09c4381 | 2020-09-03 10:01:44 +0200 | [diff] [blame] | 621 | } |
| 622 | |
Maxime Ripard | 89f31a2 | 2020-09-03 10:01:27 +0200 | [diff] [blame] | 623 | static void vc4_hdmi_csc_setup(struct vc4_hdmi *vc4_hdmi, bool enable) |
| 624 | { |
| 625 | u32 csc_ctl; |
| 626 | |
| 627 | csc_ctl = VC4_SET_FIELD(VC4_HD_CSC_CTL_ORDER_BGR, |
| 628 | VC4_HD_CSC_CTL_ORDER); |
| 629 | |
| 630 | if (enable) { |
| 631 | /* CEA VICs other than #1 requre limited range RGB |
| 632 | * output unless overridden by an AVI infoframe. |
| 633 | * Apply a colorspace conversion to squash 0-255 down |
| 634 | * to 16-235. The matrix here is: |
| 635 | * |
| 636 | * [ 0 0 0.8594 16] |
| 637 | * [ 0 0.8594 0 16] |
| 638 | * [ 0.8594 0 0 16] |
| 639 | * [ 0 0 0 1] |
| 640 | */ |
| 641 | csc_ctl |= VC4_HD_CSC_CTL_ENABLE; |
| 642 | csc_ctl |= VC4_HD_CSC_CTL_RGB2YCC; |
| 643 | csc_ctl |= VC4_SET_FIELD(VC4_HD_CSC_CTL_MODE_CUSTOM, |
| 644 | VC4_HD_CSC_CTL_MODE); |
| 645 | |
| 646 | HDMI_WRITE(HDMI_CSC_12_11, (0x000 << 16) | 0x000); |
| 647 | HDMI_WRITE(HDMI_CSC_14_13, (0x100 << 16) | 0x6e0); |
| 648 | HDMI_WRITE(HDMI_CSC_22_21, (0x6e0 << 16) | 0x000); |
| 649 | HDMI_WRITE(HDMI_CSC_24_23, (0x100 << 16) | 0x000); |
| 650 | HDMI_WRITE(HDMI_CSC_32_31, (0x000 << 16) | 0x6e0); |
| 651 | HDMI_WRITE(HDMI_CSC_34_33, (0x100 << 16) | 0x000); |
| 652 | } |
| 653 | |
| 654 | /* The RGB order applies even when CSC is disabled. */ |
| 655 | HDMI_WRITE(HDMI_CSC_CTL, csc_ctl); |
| 656 | } |
| 657 | |
Maxime Ripard | 8323989 | 2020-09-03 10:01:48 +0200 | [diff] [blame] | 658 | static void vc5_hdmi_csc_setup(struct vc4_hdmi *vc4_hdmi, bool enable) |
| 659 | { |
| 660 | u32 csc_ctl; |
| 661 | |
| 662 | csc_ctl = 0x07; /* RGB_CONVERT_MODE = custom matrix, || USE_RGB_TO_YCBCR */ |
| 663 | |
| 664 | if (enable) { |
| 665 | /* CEA VICs other than #1 requre limited range RGB |
| 666 | * output unless overridden by an AVI infoframe. |
| 667 | * Apply a colorspace conversion to squash 0-255 down |
| 668 | * to 16-235. The matrix here is: |
| 669 | * |
| 670 | * [ 0.8594 0 0 16] |
| 671 | * [ 0 0.8594 0 16] |
| 672 | * [ 0 0 0.8594 16] |
| 673 | * [ 0 0 0 1] |
| 674 | * Matrix is signed 2p13 fixed point, with signed 9p6 offsets |
| 675 | */ |
| 676 | HDMI_WRITE(HDMI_CSC_12_11, (0x0000 << 16) | 0x1b80); |
| 677 | HDMI_WRITE(HDMI_CSC_14_13, (0x0400 << 16) | 0x0000); |
| 678 | HDMI_WRITE(HDMI_CSC_22_21, (0x1b80 << 16) | 0x0000); |
| 679 | HDMI_WRITE(HDMI_CSC_24_23, (0x0400 << 16) | 0x0000); |
| 680 | HDMI_WRITE(HDMI_CSC_32_31, (0x0000 << 16) | 0x0000); |
| 681 | HDMI_WRITE(HDMI_CSC_34_33, (0x0400 << 16) | 0x1b80); |
| 682 | } else { |
| 683 | /* Still use the matrix for full range, but make it unity. |
| 684 | * Matrix is signed 2p13 fixed point, with signed 9p6 offsets |
| 685 | */ |
| 686 | HDMI_WRITE(HDMI_CSC_12_11, (0x0000 << 16) | 0x2000); |
| 687 | HDMI_WRITE(HDMI_CSC_14_13, (0x0000 << 16) | 0x0000); |
| 688 | HDMI_WRITE(HDMI_CSC_22_21, (0x2000 << 16) | 0x0000); |
| 689 | HDMI_WRITE(HDMI_CSC_24_23, (0x0000 << 16) | 0x0000); |
| 690 | HDMI_WRITE(HDMI_CSC_32_31, (0x0000 << 16) | 0x0000); |
| 691 | HDMI_WRITE(HDMI_CSC_34_33, (0x0000 << 16) | 0x2000); |
| 692 | } |
| 693 | |
| 694 | HDMI_WRITE(HDMI_CSC_CTL, csc_ctl); |
| 695 | } |
| 696 | |
Maxime Ripard | 904f668 | 2020-09-03 10:01:28 +0200 | [diff] [blame] | 697 | static void vc4_hdmi_set_timings(struct vc4_hdmi *vc4_hdmi, |
Maxime Ripard | ba8c0fa | 2020-12-15 16:42:43 +0100 | [diff] [blame] | 698 | struct drm_connector_state *state, |
Maxime Ripard | 904f668 | 2020-09-03 10:01:28 +0200 | [diff] [blame] | 699 | struct drm_display_mode *mode) |
Boris Brezillon | 4f6e3d6 | 2017-04-11 18:39:25 +0200 | [diff] [blame] | 700 | { |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 701 | bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC; |
| 702 | bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC; |
Eric Anholt | 682e62c | 2016-09-28 17:30:25 -0700 | [diff] [blame] | 703 | bool interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE; |
Eric Anholt | dfccd93 | 2016-09-29 15:34:44 -0700 | [diff] [blame] | 704 | u32 pixel_rep = (mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1; |
Eric Anholt | 682e62c | 2016-09-28 17:30:25 -0700 | [diff] [blame] | 705 | u32 verta = (VC4_SET_FIELD(mode->crtc_vsync_end - mode->crtc_vsync_start, |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 706 | VC4_HDMI_VERTA_VSP) | |
Eric Anholt | 682e62c | 2016-09-28 17:30:25 -0700 | [diff] [blame] | 707 | VC4_SET_FIELD(mode->crtc_vsync_start - mode->crtc_vdisplay, |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 708 | VC4_HDMI_VERTA_VFP) | |
Eric Anholt | 682e62c | 2016-09-28 17:30:25 -0700 | [diff] [blame] | 709 | VC4_SET_FIELD(mode->crtc_vdisplay, VC4_HDMI_VERTA_VAL)); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 710 | u32 vertb = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) | |
Eric Anholt | 682e62c | 2016-09-28 17:30:25 -0700 | [diff] [blame] | 711 | VC4_SET_FIELD(mode->crtc_vtotal - mode->crtc_vsync_end, |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 712 | VC4_HDMI_VERTB_VBP)); |
Eric Anholt | 682e62c | 2016-09-28 17:30:25 -0700 | [diff] [blame] | 713 | u32 vertb_even = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) | |
| 714 | VC4_SET_FIELD(mode->crtc_vtotal - |
| 715 | mode->crtc_vsync_end - |
| 716 | interlaced, |
| 717 | VC4_HDMI_VERTB_VBP)); |
Boris Brezillon | 4f6e3d6 | 2017-04-11 18:39:25 +0200 | [diff] [blame] | 718 | |
Maxime Ripard | 904f668 | 2020-09-03 10:01:28 +0200 | [diff] [blame] | 719 | HDMI_WRITE(HDMI_HORZA, |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 720 | (vsync_pos ? VC4_HDMI_HORZA_VPOS : 0) | |
| 721 | (hsync_pos ? VC4_HDMI_HORZA_HPOS : 0) | |
Eric Anholt | dfccd93 | 2016-09-29 15:34:44 -0700 | [diff] [blame] | 722 | VC4_SET_FIELD(mode->hdisplay * pixel_rep, |
| 723 | VC4_HDMI_HORZA_HAP)); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 724 | |
Maxime Ripard | 904f668 | 2020-09-03 10:01:28 +0200 | [diff] [blame] | 725 | HDMI_WRITE(HDMI_HORZB, |
Eric Anholt | dfccd93 | 2016-09-29 15:34:44 -0700 | [diff] [blame] | 726 | VC4_SET_FIELD((mode->htotal - |
| 727 | mode->hsync_end) * pixel_rep, |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 728 | VC4_HDMI_HORZB_HBP) | |
Eric Anholt | dfccd93 | 2016-09-29 15:34:44 -0700 | [diff] [blame] | 729 | VC4_SET_FIELD((mode->hsync_end - |
| 730 | mode->hsync_start) * pixel_rep, |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 731 | VC4_HDMI_HORZB_HSP) | |
Eric Anholt | dfccd93 | 2016-09-29 15:34:44 -0700 | [diff] [blame] | 732 | VC4_SET_FIELD((mode->hsync_start - |
| 733 | mode->hdisplay) * pixel_rep, |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 734 | VC4_HDMI_HORZB_HFP)); |
| 735 | |
Maxime Ripard | 904f668 | 2020-09-03 10:01:28 +0200 | [diff] [blame] | 736 | HDMI_WRITE(HDMI_VERTA0, verta); |
| 737 | HDMI_WRITE(HDMI_VERTA1, verta); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 738 | |
Maxime Ripard | 904f668 | 2020-09-03 10:01:28 +0200 | [diff] [blame] | 739 | HDMI_WRITE(HDMI_VERTB0, vertb_even); |
| 740 | HDMI_WRITE(HDMI_VERTB1, vertb); |
Maxime Ripard | 904f668 | 2020-09-03 10:01:28 +0200 | [diff] [blame] | 741 | } |
Maxime Ripard | ba8c0fa | 2020-12-15 16:42:43 +0100 | [diff] [blame] | 742 | |
Maxime Ripard | 8323989 | 2020-09-03 10:01:48 +0200 | [diff] [blame] | 743 | static void vc5_hdmi_set_timings(struct vc4_hdmi *vc4_hdmi, |
Maxime Ripard | ba8c0fa | 2020-12-15 16:42:43 +0100 | [diff] [blame] | 744 | struct drm_connector_state *state, |
Maxime Ripard | 8323989 | 2020-09-03 10:01:48 +0200 | [diff] [blame] | 745 | struct drm_display_mode *mode) |
| 746 | { |
| 747 | bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC; |
| 748 | bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC; |
| 749 | bool interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE; |
| 750 | u32 pixel_rep = (mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1; |
| 751 | u32 verta = (VC4_SET_FIELD(mode->crtc_vsync_end - mode->crtc_vsync_start, |
| 752 | VC5_HDMI_VERTA_VSP) | |
| 753 | VC4_SET_FIELD(mode->crtc_vsync_start - mode->crtc_vdisplay, |
| 754 | VC5_HDMI_VERTA_VFP) | |
| 755 | VC4_SET_FIELD(mode->crtc_vdisplay, VC5_HDMI_VERTA_VAL)); |
| 756 | u32 vertb = (VC4_SET_FIELD(0, VC5_HDMI_VERTB_VSPO) | |
| 757 | VC4_SET_FIELD(mode->crtc_vtotal - mode->crtc_vsync_end, |
| 758 | VC4_HDMI_VERTB_VBP)); |
| 759 | u32 vertb_even = (VC4_SET_FIELD(0, VC5_HDMI_VERTB_VSPO) | |
| 760 | VC4_SET_FIELD(mode->crtc_vtotal - |
| 761 | mode->crtc_vsync_end - |
| 762 | interlaced, |
| 763 | VC4_HDMI_VERTB_VBP)); |
Maxime Ripard | ba8c0fa | 2020-12-15 16:42:43 +0100 | [diff] [blame] | 764 | unsigned char gcp; |
| 765 | bool gcp_en; |
| 766 | u32 reg; |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 767 | |
Maxime Ripard | 8323989 | 2020-09-03 10:01:48 +0200 | [diff] [blame] | 768 | HDMI_WRITE(HDMI_VEC_INTERFACE_XBAR, 0x354021); |
| 769 | HDMI_WRITE(HDMI_HORZA, |
| 770 | (vsync_pos ? VC5_HDMI_HORZA_VPOS : 0) | |
| 771 | (hsync_pos ? VC5_HDMI_HORZA_HPOS : 0) | |
| 772 | VC4_SET_FIELD(mode->hdisplay * pixel_rep, |
| 773 | VC5_HDMI_HORZA_HAP) | |
| 774 | VC4_SET_FIELD((mode->hsync_start - |
| 775 | mode->hdisplay) * pixel_rep, |
| 776 | VC5_HDMI_HORZA_HFP)); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 777 | |
Maxime Ripard | 8323989 | 2020-09-03 10:01:48 +0200 | [diff] [blame] | 778 | HDMI_WRITE(HDMI_HORZB, |
| 779 | VC4_SET_FIELD((mode->htotal - |
| 780 | mode->hsync_end) * pixel_rep, |
| 781 | VC5_HDMI_HORZB_HBP) | |
| 782 | VC4_SET_FIELD((mode->hsync_end - |
| 783 | mode->hsync_start) * pixel_rep, |
| 784 | VC5_HDMI_HORZB_HSP)); |
Eric Anholt | 6e1cbba | 2016-09-16 10:59:45 +0100 | [diff] [blame] | 785 | |
Maxime Ripard | 8323989 | 2020-09-03 10:01:48 +0200 | [diff] [blame] | 786 | HDMI_WRITE(HDMI_VERTA0, verta); |
| 787 | HDMI_WRITE(HDMI_VERTA1, verta); |
Eric Anholt | 6e1cbba | 2016-09-16 10:59:45 +0100 | [diff] [blame] | 788 | |
Maxime Ripard | 8323989 | 2020-09-03 10:01:48 +0200 | [diff] [blame] | 789 | HDMI_WRITE(HDMI_VERTB0, vertb_even); |
| 790 | HDMI_WRITE(HDMI_VERTB1, vertb); |
Eric Anholt | 6e1cbba | 2016-09-16 10:59:45 +0100 | [diff] [blame] | 791 | |
Maxime Ripard | ba8c0fa | 2020-12-15 16:42:43 +0100 | [diff] [blame] | 792 | switch (state->max_bpc) { |
| 793 | case 12: |
| 794 | gcp = 6; |
| 795 | gcp_en = true; |
| 796 | break; |
| 797 | case 10: |
| 798 | gcp = 5; |
| 799 | gcp_en = true; |
| 800 | break; |
| 801 | case 8: |
| 802 | default: |
| 803 | gcp = 4; |
| 804 | gcp_en = false; |
| 805 | break; |
| 806 | } |
| 807 | |
| 808 | reg = HDMI_READ(HDMI_DEEP_COLOR_CONFIG_1); |
| 809 | reg &= ~(VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE_MASK | |
| 810 | VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH_MASK); |
| 811 | reg |= VC4_SET_FIELD(2, VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE) | |
| 812 | VC4_SET_FIELD(gcp, VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH); |
| 813 | HDMI_WRITE(HDMI_DEEP_COLOR_CONFIG_1, reg); |
| 814 | |
| 815 | reg = HDMI_READ(HDMI_GCP_WORD_1); |
| 816 | reg &= ~VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1_MASK; |
| 817 | reg |= VC4_SET_FIELD(gcp, VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1); |
| 818 | HDMI_WRITE(HDMI_GCP_WORD_1, reg); |
| 819 | |
| 820 | reg = HDMI_READ(HDMI_GCP_CONFIG); |
| 821 | reg &= ~VC5_HDMI_GCP_CONFIG_GCP_ENABLE; |
| 822 | reg |= gcp_en ? VC5_HDMI_GCP_CONFIG_GCP_ENABLE : 0; |
| 823 | HDMI_WRITE(HDMI_GCP_CONFIG, reg); |
| 824 | |
Maxime Ripard | 8323989 | 2020-09-03 10:01:48 +0200 | [diff] [blame] | 825 | HDMI_WRITE(HDMI_CLOCK_STOP, 0); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 826 | } |
| 827 | |
Maxime Ripard | 691456f | 2020-09-03 10:01:43 +0200 | [diff] [blame] | 828 | static void vc4_hdmi_recenter_fifo(struct vc4_hdmi *vc4_hdmi) |
Eric Anholt | 32e823c | 2017-09-20 15:59:34 -0700 | [diff] [blame] | 829 | { |
Maxime Ripard | 691456f | 2020-09-03 10:01:43 +0200 | [diff] [blame] | 830 | u32 drift; |
| 831 | int ret; |
| 832 | |
| 833 | drift = HDMI_READ(HDMI_FIFO_CTL); |
| 834 | drift &= VC4_HDMI_FIFO_VALID_WRITE_MASK; |
| 835 | |
| 836 | HDMI_WRITE(HDMI_FIFO_CTL, |
| 837 | drift & ~VC4_HDMI_FIFO_CTL_RECENTER); |
| 838 | HDMI_WRITE(HDMI_FIFO_CTL, |
| 839 | drift | VC4_HDMI_FIFO_CTL_RECENTER); |
| 840 | usleep_range(1000, 1100); |
| 841 | HDMI_WRITE(HDMI_FIFO_CTL, |
| 842 | drift & ~VC4_HDMI_FIFO_CTL_RECENTER); |
| 843 | HDMI_WRITE(HDMI_FIFO_CTL, |
| 844 | drift | VC4_HDMI_FIFO_CTL_RECENTER); |
| 845 | |
| 846 | ret = wait_for(HDMI_READ(HDMI_FIFO_CTL) & |
| 847 | VC4_HDMI_FIFO_CTL_RECENTER_DONE, 1); |
| 848 | WARN_ONCE(ret, "Timeout waiting for " |
| 849 | "VC4_HDMI_FIFO_CTL_RECENTER_DONE"); |
| 850 | } |
| 851 | |
Maxime Ripard | f623746 | 2020-12-15 16:42:40 +0100 | [diff] [blame] | 852 | static struct drm_connector_state * |
| 853 | vc4_hdmi_encoder_get_connector_state(struct drm_encoder *encoder, |
| 854 | struct drm_atomic_state *state) |
| 855 | { |
| 856 | struct drm_connector_state *conn_state; |
| 857 | struct drm_connector *connector; |
| 858 | unsigned int i; |
| 859 | |
| 860 | for_each_new_connector_in_state(state, connector, conn_state, i) { |
| 861 | if (conn_state->best_encoder == encoder) |
| 862 | return conn_state; |
| 863 | } |
| 864 | |
| 865 | return NULL; |
| 866 | } |
| 867 | |
Maxime Ripard | 8d91474 | 2020-12-15 16:42:36 +0100 | [diff] [blame] | 868 | static void vc4_hdmi_encoder_pre_crtc_configure(struct drm_encoder *encoder, |
| 869 | struct drm_atomic_state *state) |
Maxime Ripard | 904f668 | 2020-09-03 10:01:28 +0200 | [diff] [blame] | 870 | { |
Maxime Ripard | f623746 | 2020-12-15 16:42:40 +0100 | [diff] [blame] | 871 | struct drm_connector_state *conn_state = |
| 872 | vc4_hdmi_encoder_get_connector_state(encoder, state); |
| 873 | struct vc4_hdmi_connector_state *vc4_conn_state = |
| 874 | conn_state_to_vc4_hdmi_conn_state(conn_state); |
Maxime Ripard | 904f668 | 2020-09-03 10:01:28 +0200 | [diff] [blame] | 875 | struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode; |
| 876 | struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder); |
Maxime Ripard | 7d9061e | 2021-05-07 17:05:11 +0200 | [diff] [blame] | 877 | unsigned long bvb_rate, pixel_rate, hsm_rate; |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 878 | int ret; |
| 879 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 880 | ret = pm_runtime_get_sync(&vc4_hdmi->pdev->dev); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 881 | if (ret < 0) { |
| 882 | DRM_ERROR("Failed to retain power domain: %d\n", ret); |
| 883 | return; |
| 884 | } |
| 885 | |
Maxime Ripard | f623746 | 2020-12-15 16:42:40 +0100 | [diff] [blame] | 886 | pixel_rate = vc4_conn_state->pixel_rate; |
Maxime Ripard | cd4cb49 | 2020-09-03 10:01:35 +0200 | [diff] [blame] | 887 | ret = clk_set_rate(vc4_hdmi->pixel_clock, pixel_rate); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 888 | if (ret) { |
| 889 | DRM_ERROR("Failed to set pixel clock rate: %d\n", ret); |
| 890 | return; |
| 891 | } |
| 892 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 893 | ret = clk_prepare_enable(vc4_hdmi->pixel_clock); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 894 | if (ret) { |
| 895 | DRM_ERROR("Failed to turn on pixel clock: %d\n", ret); |
| 896 | return; |
| 897 | } |
| 898 | |
Nicolas Saenz Julienne | b1e7396 | 2020-03-26 13:20:01 +0100 | [diff] [blame] | 899 | /* |
| 900 | * As stated in RPi's vc4 firmware "HDMI state machine (HSM) clock must |
| 901 | * be faster than pixel clock, infinitesimally faster, tested in |
| 902 | * simulation. Otherwise, exact value is unimportant for HDMI |
| 903 | * operation." This conflicts with bcm2835's vc4 documentation, which |
| 904 | * states HSM's clock has to be at least 108% of the pixel clock. |
| 905 | * |
| 906 | * Real life tests reveal that vc4's firmware statement holds up, and |
| 907 | * users are able to use pixel clocks closer to HSM's, namely for |
| 908 | * 1920x1200@60Hz. So it was decided to have leave a 1% margin between |
| 909 | * both clocks. Which, for RPi0-3 implies a maximum pixel clock of |
| 910 | * 162MHz. |
| 911 | * |
| 912 | * Additionally, the AXI clock needs to be at least 25% of |
| 913 | * pixel clock, but HSM ends up being the limiting factor. |
Eric Anholt | 32e823c | 2017-09-20 15:59:34 -0700 | [diff] [blame] | 914 | */ |
Maxime Ripard | cd4cb49 | 2020-09-03 10:01:35 +0200 | [diff] [blame] | 915 | hsm_rate = max_t(unsigned long, 120000000, (pixel_rate / 100) * 101); |
Maxime Ripard | d5d5ce8 | 2020-09-03 10:01:36 +0200 | [diff] [blame] | 916 | ret = clk_set_min_rate(vc4_hdmi->hsm_clock, hsm_rate); |
Maxime Ripard | cd4cb49 | 2020-09-03 10:01:35 +0200 | [diff] [blame] | 917 | if (ret) { |
| 918 | DRM_ERROR("Failed to set HSM clock rate: %d\n", ret); |
| 919 | return; |
| 920 | } |
| 921 | |
| 922 | ret = clk_prepare_enable(vc4_hdmi->hsm_clock); |
| 923 | if (ret) { |
| 924 | DRM_ERROR("Failed to turn on HSM clock: %d\n", ret); |
| 925 | clk_disable_unprepare(vc4_hdmi->pixel_clock); |
| 926 | return; |
| 927 | } |
| 928 | |
Maxime Ripard | 47fa9a8 | 2021-01-11 15:23:01 +0100 | [diff] [blame] | 929 | vc4_hdmi_cec_update_clk_div(vc4_hdmi); |
| 930 | |
Maxime Ripard | 7d9061e | 2021-05-07 17:05:11 +0200 | [diff] [blame] | 931 | if (pixel_rate > 297000000) |
| 932 | bvb_rate = 300000000; |
| 933 | else if (pixel_rate > 148500000) |
| 934 | bvb_rate = 150000000; |
| 935 | else |
| 936 | bvb_rate = 75000000; |
| 937 | |
| 938 | ret = clk_set_min_rate(vc4_hdmi->pixel_bvb_clock, bvb_rate); |
Hoegeun Kwon | 3738742 | 2020-09-03 10:01:47 +0200 | [diff] [blame] | 939 | if (ret) { |
| 940 | DRM_ERROR("Failed to set pixel bvb clock rate: %d\n", ret); |
| 941 | clk_disable_unprepare(vc4_hdmi->hsm_clock); |
| 942 | clk_disable_unprepare(vc4_hdmi->pixel_clock); |
| 943 | return; |
| 944 | } |
| 945 | |
| 946 | ret = clk_prepare_enable(vc4_hdmi->pixel_bvb_clock); |
| 947 | if (ret) { |
| 948 | DRM_ERROR("Failed to turn on pixel bvb clock: %d\n", ret); |
| 949 | clk_disable_unprepare(vc4_hdmi->hsm_clock); |
| 950 | clk_disable_unprepare(vc4_hdmi->pixel_clock); |
| 951 | return; |
| 952 | } |
| 953 | |
Maxime Ripard | c457b8a | 2020-09-03 10:01:25 +0200 | [diff] [blame] | 954 | if (vc4_hdmi->variant->phy_init) |
Maxime Ripard | d2a7dd0 | 2020-12-15 16:42:41 +0100 | [diff] [blame] | 955 | vc4_hdmi->variant->phy_init(vc4_hdmi, vc4_conn_state); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 956 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 957 | HDMI_WRITE(HDMI_SCHEDULER_CONTROL, |
| 958 | HDMI_READ(HDMI_SCHEDULER_CONTROL) | |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 959 | VC4_HDMI_SCHEDULER_CONTROL_MANUAL_FORMAT | |
| 960 | VC4_HDMI_SCHEDULER_CONTROL_IGNORE_VSYNC_PREDICTS); |
| 961 | |
Maxime Ripard | 904f668 | 2020-09-03 10:01:28 +0200 | [diff] [blame] | 962 | if (vc4_hdmi->variant->set_timings) |
Maxime Ripard | ba8c0fa | 2020-12-15 16:42:43 +0100 | [diff] [blame] | 963 | vc4_hdmi->variant->set_timings(vc4_hdmi, conn_state, mode); |
Maxime Ripard | 09c4381 | 2020-09-03 10:01:44 +0200 | [diff] [blame] | 964 | } |
| 965 | |
Maxime Ripard | 8d91474 | 2020-12-15 16:42:36 +0100 | [diff] [blame] | 966 | static void vc4_hdmi_encoder_pre_crtc_enable(struct drm_encoder *encoder, |
| 967 | struct drm_atomic_state *state) |
Maxime Ripard | 09c4381 | 2020-09-03 10:01:44 +0200 | [diff] [blame] | 968 | { |
| 969 | struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode; |
| 970 | struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder); |
| 971 | struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 972 | |
| 973 | if (vc4_encoder->hdmi_monitor && |
Maxime Ripard | 89f31a2 | 2020-09-03 10:01:27 +0200 | [diff] [blame] | 974 | drm_default_rgb_quant_range(mode) == HDMI_QUANTIZATION_RANGE_LIMITED) { |
| 975 | if (vc4_hdmi->variant->csc_setup) |
| 976 | vc4_hdmi->variant->csc_setup(vc4_hdmi, true); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 977 | |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 978 | vc4_encoder->limited_rgb_range = true; |
| 979 | } else { |
Maxime Ripard | 89f31a2 | 2020-09-03 10:01:27 +0200 | [diff] [blame] | 980 | if (vc4_hdmi->variant->csc_setup) |
| 981 | vc4_hdmi->variant->csc_setup(vc4_hdmi, false); |
| 982 | |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 983 | vc4_encoder->limited_rgb_range = false; |
| 984 | } |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 985 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 986 | HDMI_WRITE(HDMI_FIFO_CTL, VC4_HDMI_FIFO_CTL_MASTER_SLAVE_N); |
Maxime Ripard | 09c4381 | 2020-09-03 10:01:44 +0200 | [diff] [blame] | 987 | } |
| 988 | |
Maxime Ripard | 8d91474 | 2020-12-15 16:42:36 +0100 | [diff] [blame] | 989 | static void vc4_hdmi_encoder_post_crtc_enable(struct drm_encoder *encoder, |
| 990 | struct drm_atomic_state *state) |
Maxime Ripard | 09c4381 | 2020-09-03 10:01:44 +0200 | [diff] [blame] | 991 | { |
Maxime Ripard | 8b3f90e | 2020-09-03 10:01:45 +0200 | [diff] [blame] | 992 | struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode; |
Maxime Ripard | 09c4381 | 2020-09-03 10:01:44 +0200 | [diff] [blame] | 993 | struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder); |
| 994 | struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder); |
Maxime Ripard | 8b3f90e | 2020-09-03 10:01:45 +0200 | [diff] [blame] | 995 | bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC; |
| 996 | bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC; |
Maxime Ripard | 09c4381 | 2020-09-03 10:01:44 +0200 | [diff] [blame] | 997 | int ret; |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 998 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 999 | HDMI_WRITE(HDMI_VID_CTL, |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1000 | VC4_HD_VID_CTL_ENABLE | |
| 1001 | VC4_HD_VID_CTL_UNDERFLOW_ENABLE | |
Maxime Ripard | 8b3f90e | 2020-09-03 10:01:45 +0200 | [diff] [blame] | 1002 | VC4_HD_VID_CTL_FRAME_COUNTER_RESET | |
| 1003 | (vsync_pos ? 0 : VC4_HD_VID_CTL_VSYNC_LOW) | |
| 1004 | (hsync_pos ? 0 : VC4_HD_VID_CTL_HSYNC_LOW)); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 1005 | |
Maxime Ripard | 81d8301 | 2020-09-03 10:01:46 +0200 | [diff] [blame] | 1006 | HDMI_WRITE(HDMI_VID_CTL, |
| 1007 | HDMI_READ(HDMI_VID_CTL) & ~VC4_HD_VID_CTL_BLANKPIX); |
| 1008 | |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 1009 | if (vc4_encoder->hdmi_monitor) { |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1010 | HDMI_WRITE(HDMI_SCHEDULER_CONTROL, |
| 1011 | HDMI_READ(HDMI_SCHEDULER_CONTROL) | |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 1012 | VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI); |
| 1013 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1014 | ret = wait_for(HDMI_READ(HDMI_SCHEDULER_CONTROL) & |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 1015 | VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE, 1000); |
| 1016 | WARN_ONCE(ret, "Timeout waiting for " |
| 1017 | "VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n"); |
| 1018 | } else { |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1019 | HDMI_WRITE(HDMI_RAM_PACKET_CONFIG, |
| 1020 | HDMI_READ(HDMI_RAM_PACKET_CONFIG) & |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 1021 | ~(VC4_HDMI_RAM_PACKET_ENABLE)); |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1022 | HDMI_WRITE(HDMI_SCHEDULER_CONTROL, |
| 1023 | HDMI_READ(HDMI_SCHEDULER_CONTROL) & |
Eric Anholt | 851479a | 2016-02-12 14:15:14 -0800 | [diff] [blame] | 1024 | ~VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI); |
| 1025 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1026 | ret = wait_for(!(HDMI_READ(HDMI_SCHEDULER_CONTROL) & |
Eric Anholt | 851479a | 2016-02-12 14:15:14 -0800 | [diff] [blame] | 1027 | VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE), 1000); |
| 1028 | WARN_ONCE(ret, "Timeout waiting for " |
| 1029 | "!VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n"); |
| 1030 | } |
| 1031 | |
| 1032 | if (vc4_encoder->hdmi_monitor) { |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1033 | WARN_ON(!(HDMI_READ(HDMI_SCHEDULER_CONTROL) & |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 1034 | VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE)); |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1035 | HDMI_WRITE(HDMI_SCHEDULER_CONTROL, |
| 1036 | HDMI_READ(HDMI_SCHEDULER_CONTROL) | |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 1037 | VC4_HDMI_SCHEDULER_CONTROL_VERT_ALWAYS_KEEPOUT); |
| 1038 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1039 | HDMI_WRITE(HDMI_RAM_PACKET_CONFIG, |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 1040 | VC4_HDMI_RAM_PACKET_ENABLE); |
| 1041 | |
Eric Anholt | 0b06e0a | 2016-02-29 17:53:01 -0800 | [diff] [blame] | 1042 | vc4_hdmi_set_infoframes(encoder); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 1043 | } |
Maxime Ripard | 691456f | 2020-09-03 10:01:43 +0200 | [diff] [blame] | 1044 | |
| 1045 | vc4_hdmi_recenter_fifo(vc4_hdmi); |
Maxime Ripard | c85695a | 2021-05-07 17:05:13 +0200 | [diff] [blame^] | 1046 | vc4_hdmi_enable_scrambling(encoder); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 1047 | } |
| 1048 | |
Maxime Ripard | 09c4381 | 2020-09-03 10:01:44 +0200 | [diff] [blame] | 1049 | static void vc4_hdmi_encoder_enable(struct drm_encoder *encoder) |
| 1050 | { |
| 1051 | } |
| 1052 | |
Maxime Ripard | 9fa1d7e | 2020-10-29 14:40:17 +0100 | [diff] [blame] | 1053 | #define WIFI_2_4GHz_CH1_MIN_FREQ 2400000000ULL |
| 1054 | #define WIFI_2_4GHz_CH1_MAX_FREQ 2422000000ULL |
| 1055 | |
Maxime Ripard | 63495f6b | 2020-10-29 13:25:21 +0100 | [diff] [blame] | 1056 | static int vc4_hdmi_encoder_atomic_check(struct drm_encoder *encoder, |
| 1057 | struct drm_crtc_state *crtc_state, |
| 1058 | struct drm_connector_state *conn_state) |
| 1059 | { |
Maxime Ripard | f623746 | 2020-12-15 16:42:40 +0100 | [diff] [blame] | 1060 | struct vc4_hdmi_connector_state *vc4_state = conn_state_to_vc4_hdmi_conn_state(conn_state); |
Maxime Ripard | 63495f6b | 2020-10-29 13:25:21 +0100 | [diff] [blame] | 1061 | struct drm_display_mode *mode = &crtc_state->adjusted_mode; |
| 1062 | struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder); |
| 1063 | unsigned long long pixel_rate = mode->clock * 1000; |
Maxime Ripard | 9fa1d7e | 2020-10-29 14:40:17 +0100 | [diff] [blame] | 1064 | unsigned long long tmds_rate; |
Maxime Ripard | 63495f6b | 2020-10-29 13:25:21 +0100 | [diff] [blame] | 1065 | |
Maxime Ripard | 57fb32e | 2020-10-29 13:25:22 +0100 | [diff] [blame] | 1066 | if (vc4_hdmi->variant->unsupported_odd_h_timings && |
| 1067 | ((mode->hdisplay % 2) || (mode->hsync_start % 2) || |
| 1068 | (mode->hsync_end % 2) || (mode->htotal % 2))) |
| 1069 | return -EINVAL; |
| 1070 | |
Maxime Ripard | 9fa1d7e | 2020-10-29 14:40:17 +0100 | [diff] [blame] | 1071 | /* |
| 1072 | * The 1440p@60 pixel rate is in the same range than the first |
| 1073 | * WiFi channel (between 2.4GHz and 2.422GHz with 22MHz |
| 1074 | * bandwidth). Slightly lower the frequency to bring it out of |
| 1075 | * the WiFi range. |
| 1076 | */ |
| 1077 | tmds_rate = pixel_rate * 10; |
| 1078 | if (vc4_hdmi->disable_wifi_frequencies && |
| 1079 | (tmds_rate >= WIFI_2_4GHz_CH1_MIN_FREQ && |
| 1080 | tmds_rate <= WIFI_2_4GHz_CH1_MAX_FREQ)) { |
| 1081 | mode->clock = 238560; |
| 1082 | pixel_rate = mode->clock * 1000; |
| 1083 | } |
| 1084 | |
Maxime Ripard | ba8c0fa | 2020-12-15 16:42:43 +0100 | [diff] [blame] | 1085 | if (conn_state->max_bpc == 12) { |
| 1086 | pixel_rate = pixel_rate * 150; |
| 1087 | do_div(pixel_rate, 100); |
| 1088 | } else if (conn_state->max_bpc == 10) { |
| 1089 | pixel_rate = pixel_rate * 125; |
| 1090 | do_div(pixel_rate, 100); |
| 1091 | } |
| 1092 | |
Maxime Ripard | 320e84d | 2020-12-15 16:42:37 +0100 | [diff] [blame] | 1093 | if (mode->flags & DRM_MODE_FLAG_DBLCLK) |
| 1094 | pixel_rate = pixel_rate * 2; |
| 1095 | |
Maxime Ripard | 63495f6b | 2020-10-29 13:25:21 +0100 | [diff] [blame] | 1096 | if (pixel_rate > vc4_hdmi->variant->max_pixel_clock) |
| 1097 | return -EINVAL; |
| 1098 | |
Maxime Ripard | 86e3a65 | 2021-05-07 17:05:12 +0200 | [diff] [blame] | 1099 | if (vc4_hdmi->disable_4kp60 && (pixel_rate > HDMI_14_MAX_TMDS_CLK)) |
| 1100 | return -EINVAL; |
| 1101 | |
Maxime Ripard | f623746 | 2020-12-15 16:42:40 +0100 | [diff] [blame] | 1102 | vc4_state->pixel_rate = pixel_rate; |
| 1103 | |
Maxime Ripard | 63495f6b | 2020-10-29 13:25:21 +0100 | [diff] [blame] | 1104 | return 0; |
| 1105 | } |
| 1106 | |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 1107 | static enum drm_mode_status |
Maxime Ripard | 11a1731 | 2020-09-03 10:01:34 +0200 | [diff] [blame] | 1108 | vc4_hdmi_encoder_mode_valid(struct drm_encoder *encoder, |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 1109 | const struct drm_display_mode *mode) |
| 1110 | { |
Maxime Ripard | cd4cb49 | 2020-09-03 10:01:35 +0200 | [diff] [blame] | 1111 | struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder); |
| 1112 | |
Maxime Ripard | 57fb32e | 2020-10-29 13:25:22 +0100 | [diff] [blame] | 1113 | if (vc4_hdmi->variant->unsupported_odd_h_timings && |
| 1114 | ((mode->hdisplay % 2) || (mode->hsync_start % 2) || |
| 1115 | (mode->hsync_end % 2) || (mode->htotal % 2))) |
| 1116 | return MODE_H_ILLEGAL; |
| 1117 | |
Maxime Ripard | cd4cb49 | 2020-09-03 10:01:35 +0200 | [diff] [blame] | 1118 | if ((mode->clock * 1000) > vc4_hdmi->variant->max_pixel_clock) |
Eric Anholt | 32e823c | 2017-09-20 15:59:34 -0700 | [diff] [blame] | 1119 | return MODE_CLOCK_HIGH; |
| 1120 | |
Maxime Ripard | 86e3a65 | 2021-05-07 17:05:12 +0200 | [diff] [blame] | 1121 | if (vc4_hdmi->disable_4kp60 && vc4_hdmi_mode_needs_scrambling(mode)) |
| 1122 | return MODE_CLOCK_HIGH; |
| 1123 | |
Eric Anholt | 32e823c | 2017-09-20 15:59:34 -0700 | [diff] [blame] | 1124 | return MODE_OK; |
| 1125 | } |
| 1126 | |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 1127 | static const struct drm_encoder_helper_funcs vc4_hdmi_encoder_helper_funcs = { |
Maxime Ripard | 63495f6b | 2020-10-29 13:25:21 +0100 | [diff] [blame] | 1128 | .atomic_check = vc4_hdmi_encoder_atomic_check, |
Eric Anholt | 32e823c | 2017-09-20 15:59:34 -0700 | [diff] [blame] | 1129 | .mode_valid = vc4_hdmi_encoder_mode_valid, |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 1130 | .disable = vc4_hdmi_encoder_disable, |
| 1131 | .enable = vc4_hdmi_encoder_enable, |
| 1132 | }; |
| 1133 | |
Dave Stevenson | 632ee3a | 2020-09-03 10:01:40 +0200 | [diff] [blame] | 1134 | static u32 vc4_hdmi_channel_map(struct vc4_hdmi *vc4_hdmi, u32 channel_mask) |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1135 | { |
Dave Stevenson | 632ee3a | 2020-09-03 10:01:40 +0200 | [diff] [blame] | 1136 | int i; |
| 1137 | u32 channel_map = 0; |
| 1138 | |
| 1139 | for (i = 0; i < 8; i++) { |
| 1140 | if (channel_mask & BIT(i)) |
| 1141 | channel_map |= i << (3 * i); |
| 1142 | } |
| 1143 | return channel_map; |
| 1144 | } |
| 1145 | |
Maxime Ripard | 8323989 | 2020-09-03 10:01:48 +0200 | [diff] [blame] | 1146 | static u32 vc5_hdmi_channel_map(struct vc4_hdmi *vc4_hdmi, u32 channel_mask) |
| 1147 | { |
| 1148 | int i; |
| 1149 | u32 channel_map = 0; |
| 1150 | |
| 1151 | for (i = 0; i < 8; i++) { |
| 1152 | if (channel_mask & BIT(i)) |
| 1153 | channel_map |= i << (4 * i); |
| 1154 | } |
| 1155 | return channel_map; |
| 1156 | } |
| 1157 | |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1158 | /* HDMI audio codec callbacks */ |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1159 | static void vc4_hdmi_audio_set_mai_clock(struct vc4_hdmi *vc4_hdmi) |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1160 | { |
Dave Stevenson | 632ee3a | 2020-09-03 10:01:40 +0200 | [diff] [blame] | 1161 | u32 hsm_clock = clk_get_rate(vc4_hdmi->audio_clock); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1162 | unsigned long n, m; |
| 1163 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1164 | rational_best_approximation(hsm_clock, vc4_hdmi->audio.samplerate, |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1165 | VC4_HD_MAI_SMP_N_MASK >> |
| 1166 | VC4_HD_MAI_SMP_N_SHIFT, |
| 1167 | (VC4_HD_MAI_SMP_M_MASK >> |
| 1168 | VC4_HD_MAI_SMP_M_SHIFT) + 1, |
| 1169 | &n, &m); |
| 1170 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1171 | HDMI_WRITE(HDMI_MAI_SMP, |
| 1172 | VC4_SET_FIELD(n, VC4_HD_MAI_SMP_N) | |
| 1173 | VC4_SET_FIELD(m - 1, VC4_HD_MAI_SMP_M)); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1174 | } |
| 1175 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1176 | static void vc4_hdmi_set_n_cts(struct vc4_hdmi *vc4_hdmi) |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1177 | { |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1178 | struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base; |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1179 | struct drm_crtc *crtc = encoder->crtc; |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1180 | const struct drm_display_mode *mode = &crtc->state->adjusted_mode; |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1181 | u32 samplerate = vc4_hdmi->audio.samplerate; |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1182 | u32 n, cts; |
| 1183 | u64 tmp; |
| 1184 | |
| 1185 | n = 128 * samplerate / 1000; |
| 1186 | tmp = (u64)(mode->clock * 1000) * n; |
| 1187 | do_div(tmp, 128 * samplerate); |
| 1188 | cts = tmp; |
| 1189 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1190 | HDMI_WRITE(HDMI_CRP_CFG, |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1191 | VC4_HDMI_CRP_CFG_EXTERNAL_CTS_EN | |
| 1192 | VC4_SET_FIELD(n, VC4_HDMI_CRP_CFG_N)); |
| 1193 | |
| 1194 | /* |
| 1195 | * We could get slightly more accurate clocks in some cases by |
| 1196 | * providing a CTS_1 value. The two CTS values are alternated |
| 1197 | * between based on the period fields |
| 1198 | */ |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1199 | HDMI_WRITE(HDMI_CTS_0, cts); |
| 1200 | HDMI_WRITE(HDMI_CTS_1, cts); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | static inline struct vc4_hdmi *dai_to_hdmi(struct snd_soc_dai *dai) |
| 1204 | { |
| 1205 | struct snd_soc_card *card = snd_soc_dai_get_drvdata(dai); |
| 1206 | |
| 1207 | return snd_soc_card_get_drvdata(card); |
| 1208 | } |
| 1209 | |
| 1210 | static int vc4_hdmi_audio_startup(struct snd_pcm_substream *substream, |
| 1211 | struct snd_soc_dai *dai) |
| 1212 | { |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1213 | struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai); |
| 1214 | struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base; |
Maxime Ripard | 0532e5e | 2020-09-03 10:01:21 +0200 | [diff] [blame] | 1215 | struct drm_connector *connector = &vc4_hdmi->connector; |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1216 | int ret; |
| 1217 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1218 | if (vc4_hdmi->audio.substream && vc4_hdmi->audio.substream != substream) |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1219 | return -EINVAL; |
| 1220 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1221 | vc4_hdmi->audio.substream = substream; |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1222 | |
| 1223 | /* |
| 1224 | * If the HDMI encoder hasn't probed, or the encoder is |
| 1225 | * currently in DVI mode, treat the codec dai as missing. |
| 1226 | */ |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1227 | if (!encoder->crtc || !(HDMI_READ(HDMI_RAM_PACKET_CONFIG) & |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1228 | VC4_HDMI_RAM_PACKET_ENABLE)) |
| 1229 | return -ENODEV; |
| 1230 | |
Maxime Ripard | c98c85b | 2020-09-03 10:01:12 +0200 | [diff] [blame] | 1231 | ret = snd_pcm_hw_constraint_eld(substream->runtime, connector->eld); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1232 | if (ret) |
| 1233 | return ret; |
| 1234 | |
| 1235 | return 0; |
| 1236 | } |
| 1237 | |
| 1238 | static int vc4_hdmi_audio_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) |
| 1239 | { |
| 1240 | return 0; |
| 1241 | } |
| 1242 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1243 | static void vc4_hdmi_audio_reset(struct vc4_hdmi *vc4_hdmi) |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1244 | { |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1245 | struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base; |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1246 | struct device *dev = &vc4_hdmi->pdev->dev; |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1247 | int ret; |
| 1248 | |
Dave Stevenson | 6ac1c75 | 2020-09-03 10:01:38 +0200 | [diff] [blame] | 1249 | vc4_hdmi->audio.streaming = false; |
Maxime Ripard | e2f9b2e | 2020-12-03 08:46:24 +0100 | [diff] [blame] | 1250 | ret = vc4_hdmi_stop_packet(encoder, HDMI_INFOFRAME_TYPE_AUDIO, false); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1251 | if (ret) |
| 1252 | dev_err(dev, "Failed to stop audio infoframe: %d\n", ret); |
| 1253 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1254 | HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_RESET); |
| 1255 | HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_ERRORF); |
| 1256 | HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_FLUSH); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1257 | } |
| 1258 | |
| 1259 | static void vc4_hdmi_audio_shutdown(struct snd_pcm_substream *substream, |
| 1260 | struct snd_soc_dai *dai) |
| 1261 | { |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1262 | struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1263 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1264 | if (substream != vc4_hdmi->audio.substream) |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1265 | return; |
| 1266 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1267 | vc4_hdmi_audio_reset(vc4_hdmi); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1268 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1269 | vc4_hdmi->audio.substream = NULL; |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1270 | } |
| 1271 | |
| 1272 | /* HDMI audio codec callbacks */ |
| 1273 | static int vc4_hdmi_audio_hw_params(struct snd_pcm_substream *substream, |
| 1274 | struct snd_pcm_hw_params *params, |
| 1275 | struct snd_soc_dai *dai) |
| 1276 | { |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1277 | struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai); |
Maxime Ripard | 58d0436 | 2020-10-27 11:15:58 +0100 | [diff] [blame] | 1278 | struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base; |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1279 | struct device *dev = &vc4_hdmi->pdev->dev; |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1280 | u32 audio_packet_config, channel_mask; |
Dave Stevenson | 632ee3a | 2020-09-03 10:01:40 +0200 | [diff] [blame] | 1281 | u32 channel_map; |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1282 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1283 | if (substream != vc4_hdmi->audio.substream) |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1284 | return -EINVAL; |
| 1285 | |
| 1286 | dev_dbg(dev, "%s: %u Hz, %d bit, %d channels\n", __func__, |
| 1287 | params_rate(params), params_width(params), |
| 1288 | params_channels(params)); |
| 1289 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1290 | vc4_hdmi->audio.channels = params_channels(params); |
| 1291 | vc4_hdmi->audio.samplerate = params_rate(params); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1292 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1293 | HDMI_WRITE(HDMI_MAI_CTL, |
| 1294 | VC4_HD_MAI_CTL_RESET | |
| 1295 | VC4_HD_MAI_CTL_FLUSH | |
| 1296 | VC4_HD_MAI_CTL_DLATE | |
| 1297 | VC4_HD_MAI_CTL_ERRORE | |
| 1298 | VC4_HD_MAI_CTL_ERRORF); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1299 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1300 | vc4_hdmi_audio_set_mai_clock(vc4_hdmi); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1301 | |
Dave Stevenson | b9b8bac | 2020-09-03 10:01:39 +0200 | [diff] [blame] | 1302 | /* The B frame identifier should match the value used by alsa-lib (8) */ |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1303 | audio_packet_config = |
| 1304 | VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_SAMPLE_FLAT | |
| 1305 | VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_INACTIVE_CHANNELS | |
Dave Stevenson | b9b8bac | 2020-09-03 10:01:39 +0200 | [diff] [blame] | 1306 | VC4_SET_FIELD(0x8, VC4_HDMI_AUDIO_PACKET_B_FRAME_IDENTIFIER); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1307 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1308 | channel_mask = GENMASK(vc4_hdmi->audio.channels - 1, 0); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1309 | audio_packet_config |= VC4_SET_FIELD(channel_mask, |
| 1310 | VC4_HDMI_AUDIO_PACKET_CEA_MASK); |
| 1311 | |
| 1312 | /* Set the MAI threshold. This logic mimics the firmware's. */ |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1313 | if (vc4_hdmi->audio.samplerate > 96000) { |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1314 | HDMI_WRITE(HDMI_MAI_THR, |
| 1315 | VC4_SET_FIELD(0x12, VC4_HD_MAI_THR_DREQHIGH) | |
| 1316 | VC4_SET_FIELD(0x12, VC4_HD_MAI_THR_DREQLOW)); |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1317 | } else if (vc4_hdmi->audio.samplerate > 48000) { |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1318 | HDMI_WRITE(HDMI_MAI_THR, |
| 1319 | VC4_SET_FIELD(0x14, VC4_HD_MAI_THR_DREQHIGH) | |
| 1320 | VC4_SET_FIELD(0x12, VC4_HD_MAI_THR_DREQLOW)); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1321 | } else { |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1322 | HDMI_WRITE(HDMI_MAI_THR, |
| 1323 | VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_PANICHIGH) | |
| 1324 | VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_PANICLOW) | |
| 1325 | VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_DREQHIGH) | |
| 1326 | VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_DREQLOW)); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1327 | } |
| 1328 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1329 | HDMI_WRITE(HDMI_MAI_CONFIG, |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1330 | VC4_HDMI_MAI_CONFIG_BIT_REVERSE | |
| 1331 | VC4_SET_FIELD(channel_mask, VC4_HDMI_MAI_CHANNEL_MASK)); |
| 1332 | |
Dave Stevenson | 632ee3a | 2020-09-03 10:01:40 +0200 | [diff] [blame] | 1333 | channel_map = vc4_hdmi->variant->channel_map(vc4_hdmi, channel_mask); |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1334 | HDMI_WRITE(HDMI_MAI_CHANNEL_MAP, channel_map); |
| 1335 | HDMI_WRITE(HDMI_AUDIO_PACKET_CONFIG, audio_packet_config); |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1336 | vc4_hdmi_set_n_cts(vc4_hdmi); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1337 | |
Maxime Ripard | 58d0436 | 2020-10-27 11:15:58 +0100 | [diff] [blame] | 1338 | vc4_hdmi_set_audio_infoframe(encoder); |
| 1339 | |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1340 | return 0; |
| 1341 | } |
| 1342 | |
| 1343 | static int vc4_hdmi_audio_trigger(struct snd_pcm_substream *substream, int cmd, |
| 1344 | struct snd_soc_dai *dai) |
| 1345 | { |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1346 | struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1347 | |
| 1348 | switch (cmd) { |
| 1349 | case SNDRV_PCM_TRIGGER_START: |
Dave Stevenson | 6ac1c75 | 2020-09-03 10:01:38 +0200 | [diff] [blame] | 1350 | vc4_hdmi->audio.streaming = true; |
Maxime Ripard | 647b965 | 2020-09-03 10:01:26 +0200 | [diff] [blame] | 1351 | |
| 1352 | if (vc4_hdmi->variant->phy_rng_enable) |
| 1353 | vc4_hdmi->variant->phy_rng_enable(vc4_hdmi); |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1354 | |
| 1355 | HDMI_WRITE(HDMI_MAI_CTL, |
| 1356 | VC4_SET_FIELD(vc4_hdmi->audio.channels, |
| 1357 | VC4_HD_MAI_CTL_CHNUM) | |
| 1358 | VC4_HD_MAI_CTL_ENABLE); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1359 | break; |
| 1360 | case SNDRV_PCM_TRIGGER_STOP: |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1361 | HDMI_WRITE(HDMI_MAI_CTL, |
| 1362 | VC4_HD_MAI_CTL_DLATE | |
| 1363 | VC4_HD_MAI_CTL_ERRORE | |
| 1364 | VC4_HD_MAI_CTL_ERRORF); |
Maxime Ripard | 647b965 | 2020-09-03 10:01:26 +0200 | [diff] [blame] | 1365 | |
| 1366 | if (vc4_hdmi->variant->phy_rng_disable) |
| 1367 | vc4_hdmi->variant->phy_rng_disable(vc4_hdmi); |
| 1368 | |
Dave Stevenson | 6ac1c75 | 2020-09-03 10:01:38 +0200 | [diff] [blame] | 1369 | vc4_hdmi->audio.streaming = false; |
| 1370 | |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1371 | break; |
| 1372 | default: |
| 1373 | break; |
| 1374 | } |
| 1375 | |
| 1376 | return 0; |
| 1377 | } |
| 1378 | |
| 1379 | static inline struct vc4_hdmi * |
| 1380 | snd_component_to_hdmi(struct snd_soc_component *component) |
| 1381 | { |
| 1382 | struct snd_soc_card *card = snd_soc_component_get_drvdata(component); |
| 1383 | |
| 1384 | return snd_soc_card_get_drvdata(card); |
| 1385 | } |
| 1386 | |
| 1387 | static int vc4_hdmi_audio_eld_ctl_info(struct snd_kcontrol *kcontrol, |
| 1388 | struct snd_ctl_elem_info *uinfo) |
| 1389 | { |
| 1390 | struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1391 | struct vc4_hdmi *vc4_hdmi = snd_component_to_hdmi(component); |
Maxime Ripard | 0532e5e | 2020-09-03 10:01:21 +0200 | [diff] [blame] | 1392 | struct drm_connector *connector = &vc4_hdmi->connector; |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1393 | |
| 1394 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES; |
Maxime Ripard | c98c85b | 2020-09-03 10:01:12 +0200 | [diff] [blame] | 1395 | uinfo->count = sizeof(connector->eld); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1396 | |
| 1397 | return 0; |
| 1398 | } |
| 1399 | |
| 1400 | static int vc4_hdmi_audio_eld_ctl_get(struct snd_kcontrol *kcontrol, |
| 1401 | struct snd_ctl_elem_value *ucontrol) |
| 1402 | { |
| 1403 | struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1404 | struct vc4_hdmi *vc4_hdmi = snd_component_to_hdmi(component); |
Maxime Ripard | 0532e5e | 2020-09-03 10:01:21 +0200 | [diff] [blame] | 1405 | struct drm_connector *connector = &vc4_hdmi->connector; |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1406 | |
Maxime Ripard | c98c85b | 2020-09-03 10:01:12 +0200 | [diff] [blame] | 1407 | memcpy(ucontrol->value.bytes.data, connector->eld, |
| 1408 | sizeof(connector->eld)); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1409 | |
| 1410 | return 0; |
| 1411 | } |
| 1412 | |
| 1413 | static const struct snd_kcontrol_new vc4_hdmi_audio_controls[] = { |
| 1414 | { |
| 1415 | .access = SNDRV_CTL_ELEM_ACCESS_READ | |
| 1416 | SNDRV_CTL_ELEM_ACCESS_VOLATILE, |
| 1417 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 1418 | .name = "ELD", |
| 1419 | .info = vc4_hdmi_audio_eld_ctl_info, |
| 1420 | .get = vc4_hdmi_audio_eld_ctl_get, |
| 1421 | }, |
| 1422 | }; |
| 1423 | |
| 1424 | static const struct snd_soc_dapm_widget vc4_hdmi_audio_widgets[] = { |
| 1425 | SND_SOC_DAPM_OUTPUT("TX"), |
| 1426 | }; |
| 1427 | |
| 1428 | static const struct snd_soc_dapm_route vc4_hdmi_audio_routes[] = { |
| 1429 | { "TX", NULL, "Playback" }, |
| 1430 | }; |
| 1431 | |
Kuninori Morimoto | 635b1c1 | 2018-01-29 04:35:04 +0000 | [diff] [blame] | 1432 | static const struct snd_soc_component_driver vc4_hdmi_audio_component_drv = { |
Maxime Ripard | a3a0ded | 2020-07-08 16:45:55 +0200 | [diff] [blame] | 1433 | .name = "vc4-hdmi-codec-dai-component", |
Kuninori Morimoto | 635b1c1 | 2018-01-29 04:35:04 +0000 | [diff] [blame] | 1434 | .controls = vc4_hdmi_audio_controls, |
| 1435 | .num_controls = ARRAY_SIZE(vc4_hdmi_audio_controls), |
| 1436 | .dapm_widgets = vc4_hdmi_audio_widgets, |
| 1437 | .num_dapm_widgets = ARRAY_SIZE(vc4_hdmi_audio_widgets), |
| 1438 | .dapm_routes = vc4_hdmi_audio_routes, |
| 1439 | .num_dapm_routes = ARRAY_SIZE(vc4_hdmi_audio_routes), |
| 1440 | .idle_bias_on = 1, |
| 1441 | .use_pmdown_time = 1, |
| 1442 | .endianness = 1, |
| 1443 | .non_legacy_dai_naming = 1, |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1444 | }; |
| 1445 | |
| 1446 | static const struct snd_soc_dai_ops vc4_hdmi_audio_dai_ops = { |
| 1447 | .startup = vc4_hdmi_audio_startup, |
| 1448 | .shutdown = vc4_hdmi_audio_shutdown, |
| 1449 | .hw_params = vc4_hdmi_audio_hw_params, |
| 1450 | .set_fmt = vc4_hdmi_audio_set_fmt, |
| 1451 | .trigger = vc4_hdmi_audio_trigger, |
| 1452 | }; |
| 1453 | |
| 1454 | static struct snd_soc_dai_driver vc4_hdmi_audio_codec_dai_drv = { |
| 1455 | .name = "vc4-hdmi-hifi", |
| 1456 | .playback = { |
| 1457 | .stream_name = "Playback", |
| 1458 | .channels_min = 2, |
| 1459 | .channels_max = 8, |
| 1460 | .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | |
| 1461 | SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | |
| 1462 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 | |
| 1463 | SNDRV_PCM_RATE_192000, |
| 1464 | .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE, |
| 1465 | }, |
| 1466 | }; |
| 1467 | |
| 1468 | static const struct snd_soc_component_driver vc4_hdmi_audio_cpu_dai_comp = { |
| 1469 | .name = "vc4-hdmi-cpu-dai-component", |
| 1470 | }; |
| 1471 | |
| 1472 | static int vc4_hdmi_audio_cpu_dai_probe(struct snd_soc_dai *dai) |
| 1473 | { |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1474 | struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1475 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1476 | snd_soc_dai_init_dma_data(dai, &vc4_hdmi->audio.dma_data, NULL); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1477 | |
| 1478 | return 0; |
| 1479 | } |
| 1480 | |
| 1481 | static struct snd_soc_dai_driver vc4_hdmi_audio_cpu_dai_drv = { |
| 1482 | .name = "vc4-hdmi-cpu-dai", |
| 1483 | .probe = vc4_hdmi_audio_cpu_dai_probe, |
| 1484 | .playback = { |
| 1485 | .stream_name = "Playback", |
| 1486 | .channels_min = 1, |
| 1487 | .channels_max = 8, |
| 1488 | .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | |
| 1489 | SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | |
| 1490 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 | |
| 1491 | SNDRV_PCM_RATE_192000, |
| 1492 | .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE, |
| 1493 | }, |
| 1494 | .ops = &vc4_hdmi_audio_dai_ops, |
| 1495 | }; |
| 1496 | |
| 1497 | static const struct snd_dmaengine_pcm_config pcm_conf = { |
| 1498 | .chan_names[SNDRV_PCM_STREAM_PLAYBACK] = "audio-rx", |
| 1499 | .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config, |
| 1500 | }; |
| 1501 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1502 | static int vc4_hdmi_audio_init(struct vc4_hdmi *vc4_hdmi) |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1503 | { |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1504 | const struct vc4_hdmi_register *mai_data = |
| 1505 | &vc4_hdmi->variant->registers[HDMI_MAI_DATA]; |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1506 | struct snd_soc_dai_link *dai_link = &vc4_hdmi->audio.link; |
| 1507 | struct snd_soc_card *card = &vc4_hdmi->audio.card; |
| 1508 | struct device *dev = &vc4_hdmi->pdev->dev; |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1509 | const __be32 *addr; |
Dave Stevenson | 094864b | 2020-09-03 10:01:37 +0200 | [diff] [blame] | 1510 | int index; |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1511 | int ret; |
| 1512 | |
| 1513 | if (!of_find_property(dev->of_node, "dmas", NULL)) { |
| 1514 | dev_warn(dev, |
| 1515 | "'dmas' DT property is missing, no HDMI audio\n"); |
| 1516 | return 0; |
| 1517 | } |
| 1518 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1519 | if (mai_data->reg != VC4_HD) { |
| 1520 | WARN_ONCE(true, "MAI isn't in the HD block\n"); |
| 1521 | return -EINVAL; |
| 1522 | } |
| 1523 | |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1524 | /* |
| 1525 | * Get the physical address of VC4_HD_MAI_DATA. We need to retrieve |
| 1526 | * the bus address specified in the DT, because the physical address |
| 1527 | * (the one returned by platform_get_resource()) is not appropriate |
| 1528 | * for DMA transfers. |
| 1529 | * This VC/MMU should probably be exposed to avoid this kind of hacks. |
| 1530 | */ |
Dave Stevenson | 094864b | 2020-09-03 10:01:37 +0200 | [diff] [blame] | 1531 | index = of_property_match_string(dev->of_node, "reg-names", "hd"); |
| 1532 | /* Before BCM2711, we don't have a named register range */ |
| 1533 | if (index < 0) |
| 1534 | index = 1; |
| 1535 | |
| 1536 | addr = of_get_address(dev->of_node, index, NULL, NULL); |
| 1537 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1538 | vc4_hdmi->audio.dma_data.addr = be32_to_cpup(addr) + mai_data->offset; |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1539 | vc4_hdmi->audio.dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; |
| 1540 | vc4_hdmi->audio.dma_data.maxburst = 2; |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1541 | |
| 1542 | ret = devm_snd_dmaengine_pcm_register(dev, &pcm_conf, 0); |
| 1543 | if (ret) { |
| 1544 | dev_err(dev, "Could not register PCM component: %d\n", ret); |
| 1545 | return ret; |
| 1546 | } |
| 1547 | |
| 1548 | ret = devm_snd_soc_register_component(dev, &vc4_hdmi_audio_cpu_dai_comp, |
| 1549 | &vc4_hdmi_audio_cpu_dai_drv, 1); |
| 1550 | if (ret) { |
| 1551 | dev_err(dev, "Could not register CPU DAI: %d\n", ret); |
| 1552 | return ret; |
| 1553 | } |
| 1554 | |
Kuninori Morimoto | 635b1c1 | 2018-01-29 04:35:04 +0000 | [diff] [blame] | 1555 | /* register component and codec dai */ |
| 1556 | ret = devm_snd_soc_register_component(dev, &vc4_hdmi_audio_component_drv, |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1557 | &vc4_hdmi_audio_codec_dai_drv, 1); |
| 1558 | if (ret) { |
Kuninori Morimoto | 635b1c1 | 2018-01-29 04:35:04 +0000 | [diff] [blame] | 1559 | dev_err(dev, "Could not register component: %d\n", ret); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1560 | return ret; |
| 1561 | } |
| 1562 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1563 | dai_link->cpus = &vc4_hdmi->audio.cpu; |
| 1564 | dai_link->codecs = &vc4_hdmi->audio.codec; |
| 1565 | dai_link->platforms = &vc4_hdmi->audio.platform; |
Kuninori Morimoto | 0467d8e | 2019-06-06 13:19:19 +0900 | [diff] [blame] | 1566 | |
| 1567 | dai_link->num_cpus = 1; |
| 1568 | dai_link->num_codecs = 1; |
Kuninori Morimoto | 8a90efd | 2019-06-28 10:46:14 +0900 | [diff] [blame] | 1569 | dai_link->num_platforms = 1; |
Kuninori Morimoto | 0467d8e | 2019-06-06 13:19:19 +0900 | [diff] [blame] | 1570 | |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1571 | dai_link->name = "MAI"; |
| 1572 | dai_link->stream_name = "MAI PCM"; |
Kuninori Morimoto | 0467d8e | 2019-06-06 13:19:19 +0900 | [diff] [blame] | 1573 | dai_link->codecs->dai_name = vc4_hdmi_audio_codec_dai_drv.name; |
| 1574 | dai_link->cpus->dai_name = dev_name(dev); |
| 1575 | dai_link->codecs->name = dev_name(dev); |
Kuninori Morimoto | 8a90efd | 2019-06-28 10:46:14 +0900 | [diff] [blame] | 1576 | dai_link->platforms->name = dev_name(dev); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1577 | |
| 1578 | card->dai_link = dai_link; |
| 1579 | card->num_links = 1; |
Maxime Ripard | 9be43a5 | 2020-09-03 10:01:41 +0200 | [diff] [blame] | 1580 | card->name = vc4_hdmi->variant->card_name; |
Nicolas Saenz Julienne | 33c7453 | 2021-01-15 20:12:09 +0100 | [diff] [blame] | 1581 | card->driver_name = "vc4-hdmi"; |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1582 | card->dev = dev; |
Marek Szyprowski | ec653df | 2020-07-01 09:39:49 +0200 | [diff] [blame] | 1583 | card->owner = THIS_MODULE; |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1584 | |
| 1585 | /* |
| 1586 | * Be careful, snd_soc_register_card() calls dev_set_drvdata() and |
| 1587 | * stores a pointer to the snd card object in dev->driver_data. This |
| 1588 | * means we cannot use it for something else. The hdmi back-pointer is |
| 1589 | * now stored in card->drvdata and should be retrieved with |
| 1590 | * snd_soc_card_get_drvdata() if needed. |
| 1591 | */ |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1592 | snd_soc_card_set_drvdata(card, vc4_hdmi); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1593 | ret = devm_snd_soc_register_card(dev, card); |
Kuninori Morimoto | 635b1c1 | 2018-01-29 04:35:04 +0000 | [diff] [blame] | 1594 | if (ret) |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1595 | dev_err(dev, "Could not register sound card: %d\n", ret); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1596 | |
| 1597 | return ret; |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1598 | |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1599 | } |
| 1600 | |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1601 | #ifdef CONFIG_DRM_VC4_HDMI_CEC |
Maxime Ripard | ed4a6bb | 2021-01-11 15:23:03 +0100 | [diff] [blame] | 1602 | static irqreturn_t vc4_cec_irq_handler_rx_thread(int irq, void *priv) |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1603 | { |
Maxime Ripard | 66bf1c3 | 2020-09-03 10:01:18 +0200 | [diff] [blame] | 1604 | struct vc4_hdmi *vc4_hdmi = priv; |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1605 | |
Maxime Ripard | ed4a6bb | 2021-01-11 15:23:03 +0100 | [diff] [blame] | 1606 | if (vc4_hdmi->cec_rx_msg.len) |
| 1607 | cec_received_msg(vc4_hdmi->cec_adap, |
| 1608 | &vc4_hdmi->cec_rx_msg); |
| 1609 | |
| 1610 | return IRQ_HANDLED; |
| 1611 | } |
| 1612 | |
| 1613 | static irqreturn_t vc4_cec_irq_handler_tx_thread(int irq, void *priv) |
| 1614 | { |
| 1615 | struct vc4_hdmi *vc4_hdmi = priv; |
| 1616 | |
| 1617 | if (vc4_hdmi->cec_tx_ok) { |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1618 | cec_transmit_done(vc4_hdmi->cec_adap, CEC_TX_STATUS_OK, |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1619 | 0, 0, 0, 0); |
| 1620 | } else { |
| 1621 | /* |
| 1622 | * This CEC implementation makes 1 retry, so if we |
| 1623 | * get a NACK, then that means it made 2 attempts. |
| 1624 | */ |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1625 | cec_transmit_done(vc4_hdmi->cec_adap, CEC_TX_STATUS_NACK, |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1626 | 0, 2, 0, 0); |
| 1627 | } |
| 1628 | return IRQ_HANDLED; |
| 1629 | } |
| 1630 | |
Maxime Ripard | ed4a6bb | 2021-01-11 15:23:03 +0100 | [diff] [blame] | 1631 | static irqreturn_t vc4_cec_irq_handler_thread(int irq, void *priv) |
| 1632 | { |
| 1633 | struct vc4_hdmi *vc4_hdmi = priv; |
| 1634 | irqreturn_t ret; |
| 1635 | |
| 1636 | if (vc4_hdmi->cec_irq_was_rx) |
| 1637 | ret = vc4_cec_irq_handler_rx_thread(irq, priv); |
| 1638 | else |
| 1639 | ret = vc4_cec_irq_handler_tx_thread(irq, priv); |
| 1640 | |
| 1641 | return ret; |
| 1642 | } |
| 1643 | |
Maxime Ripard | 66bf1c3 | 2020-09-03 10:01:18 +0200 | [diff] [blame] | 1644 | static void vc4_cec_read_msg(struct vc4_hdmi *vc4_hdmi, u32 cntrl1) |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1645 | { |
Dom Cobley | 4a59ed5 | 2021-01-11 15:22:57 +0100 | [diff] [blame] | 1646 | struct drm_device *dev = vc4_hdmi->connector.dev; |
Maxime Ripard | 1331145 | 2020-09-03 10:01:15 +0200 | [diff] [blame] | 1647 | struct cec_msg *msg = &vc4_hdmi->cec_rx_msg; |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1648 | unsigned int i; |
| 1649 | |
| 1650 | msg->len = 1 + ((cntrl1 & VC4_HDMI_CEC_REC_WRD_CNT_MASK) >> |
| 1651 | VC4_HDMI_CEC_REC_WRD_CNT_SHIFT); |
Dom Cobley | 4a59ed5 | 2021-01-11 15:22:57 +0100 | [diff] [blame] | 1652 | |
| 1653 | if (msg->len > 16) { |
| 1654 | drm_err(dev, "Attempting to read too much data (%d)\n", msg->len); |
| 1655 | return; |
| 1656 | } |
| 1657 | |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1658 | for (i = 0; i < msg->len; i += 4) { |
Dom Cobley | 4a59ed5 | 2021-01-11 15:22:57 +0100 | [diff] [blame] | 1659 | u32 val = HDMI_READ(HDMI_CEC_RX_DATA_1 + (i >> 2)); |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1660 | |
| 1661 | msg->msg[i] = val & 0xff; |
| 1662 | msg->msg[i + 1] = (val >> 8) & 0xff; |
| 1663 | msg->msg[i + 2] = (val >> 16) & 0xff; |
| 1664 | msg->msg[i + 3] = (val >> 24) & 0xff; |
| 1665 | } |
| 1666 | } |
| 1667 | |
Maxime Ripard | ed4a6bb | 2021-01-11 15:23:03 +0100 | [diff] [blame] | 1668 | static irqreturn_t vc4_cec_irq_handler_tx_bare(int irq, void *priv) |
| 1669 | { |
| 1670 | struct vc4_hdmi *vc4_hdmi = priv; |
| 1671 | u32 cntrl1; |
| 1672 | |
| 1673 | cntrl1 = HDMI_READ(HDMI_CEC_CNTRL_1); |
| 1674 | vc4_hdmi->cec_tx_ok = cntrl1 & VC4_HDMI_CEC_TX_STATUS_GOOD; |
| 1675 | cntrl1 &= ~VC4_HDMI_CEC_START_XMIT_BEGIN; |
| 1676 | HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1); |
| 1677 | |
| 1678 | return IRQ_WAKE_THREAD; |
| 1679 | } |
| 1680 | |
| 1681 | static irqreturn_t vc4_cec_irq_handler_rx_bare(int irq, void *priv) |
| 1682 | { |
| 1683 | struct vc4_hdmi *vc4_hdmi = priv; |
| 1684 | u32 cntrl1; |
| 1685 | |
| 1686 | vc4_hdmi->cec_rx_msg.len = 0; |
| 1687 | cntrl1 = HDMI_READ(HDMI_CEC_CNTRL_1); |
| 1688 | vc4_cec_read_msg(vc4_hdmi, cntrl1); |
| 1689 | cntrl1 |= VC4_HDMI_CEC_CLEAR_RECEIVE_OFF; |
| 1690 | HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1); |
| 1691 | cntrl1 &= ~VC4_HDMI_CEC_CLEAR_RECEIVE_OFF; |
| 1692 | |
| 1693 | HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1); |
| 1694 | |
| 1695 | return IRQ_WAKE_THREAD; |
| 1696 | } |
| 1697 | |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1698 | static irqreturn_t vc4_cec_irq_handler(int irq, void *priv) |
| 1699 | { |
Maxime Ripard | 66bf1c3 | 2020-09-03 10:01:18 +0200 | [diff] [blame] | 1700 | struct vc4_hdmi *vc4_hdmi = priv; |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1701 | u32 stat = HDMI_READ(HDMI_CEC_CPU_STATUS); |
Maxime Ripard | ed4a6bb | 2021-01-11 15:23:03 +0100 | [diff] [blame] | 1702 | irqreturn_t ret; |
| 1703 | u32 cntrl5; |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1704 | |
| 1705 | if (!(stat & VC4_HDMI_CPU_CEC)) |
| 1706 | return IRQ_NONE; |
Maxime Ripard | ed4a6bb | 2021-01-11 15:23:03 +0100 | [diff] [blame] | 1707 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1708 | cntrl5 = HDMI_READ(HDMI_CEC_CNTRL_5); |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1709 | vc4_hdmi->cec_irq_was_rx = cntrl5 & VC4_HDMI_CEC_RX_CEC_INT; |
Maxime Ripard | ed4a6bb | 2021-01-11 15:23:03 +0100 | [diff] [blame] | 1710 | if (vc4_hdmi->cec_irq_was_rx) |
| 1711 | ret = vc4_cec_irq_handler_rx_bare(irq, priv); |
| 1712 | else |
| 1713 | ret = vc4_cec_irq_handler_tx_bare(irq, priv); |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1714 | |
Maxime Ripard | ed4a6bb | 2021-01-11 15:23:03 +0100 | [diff] [blame] | 1715 | HDMI_WRITE(HDMI_CEC_CPU_CLEAR, VC4_HDMI_CPU_CEC); |
| 1716 | return ret; |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1717 | } |
| 1718 | |
| 1719 | static int vc4_hdmi_cec_adap_enable(struct cec_adapter *adap, bool enable) |
| 1720 | { |
Maxime Ripard | 66bf1c3 | 2020-09-03 10:01:18 +0200 | [diff] [blame] | 1721 | struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap); |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1722 | /* clock period in microseconds */ |
| 1723 | const u32 usecs = 1000000 / CEC_CLOCK_FREQ; |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1724 | u32 val = HDMI_READ(HDMI_CEC_CNTRL_5); |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1725 | |
| 1726 | val &= ~(VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET | |
| 1727 | VC4_HDMI_CEC_CNT_TO_4700_US_MASK | |
| 1728 | VC4_HDMI_CEC_CNT_TO_4500_US_MASK); |
| 1729 | val |= ((4700 / usecs) << VC4_HDMI_CEC_CNT_TO_4700_US_SHIFT) | |
| 1730 | ((4500 / usecs) << VC4_HDMI_CEC_CNT_TO_4500_US_SHIFT); |
| 1731 | |
| 1732 | if (enable) { |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1733 | HDMI_WRITE(HDMI_CEC_CNTRL_5, val | |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1734 | VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET); |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1735 | HDMI_WRITE(HDMI_CEC_CNTRL_5, val); |
| 1736 | HDMI_WRITE(HDMI_CEC_CNTRL_2, |
| 1737 | ((1500 / usecs) << VC4_HDMI_CEC_CNT_TO_1500_US_SHIFT) | |
| 1738 | ((1300 / usecs) << VC4_HDMI_CEC_CNT_TO_1300_US_SHIFT) | |
| 1739 | ((800 / usecs) << VC4_HDMI_CEC_CNT_TO_800_US_SHIFT) | |
| 1740 | ((600 / usecs) << VC4_HDMI_CEC_CNT_TO_600_US_SHIFT) | |
| 1741 | ((400 / usecs) << VC4_HDMI_CEC_CNT_TO_400_US_SHIFT)); |
| 1742 | HDMI_WRITE(HDMI_CEC_CNTRL_3, |
| 1743 | ((2750 / usecs) << VC4_HDMI_CEC_CNT_TO_2750_US_SHIFT) | |
| 1744 | ((2400 / usecs) << VC4_HDMI_CEC_CNT_TO_2400_US_SHIFT) | |
| 1745 | ((2050 / usecs) << VC4_HDMI_CEC_CNT_TO_2050_US_SHIFT) | |
| 1746 | ((1700 / usecs) << VC4_HDMI_CEC_CNT_TO_1700_US_SHIFT)); |
| 1747 | HDMI_WRITE(HDMI_CEC_CNTRL_4, |
| 1748 | ((4300 / usecs) << VC4_HDMI_CEC_CNT_TO_4300_US_SHIFT) | |
| 1749 | ((3900 / usecs) << VC4_HDMI_CEC_CNT_TO_3900_US_SHIFT) | |
| 1750 | ((3600 / usecs) << VC4_HDMI_CEC_CNT_TO_3600_US_SHIFT) | |
| 1751 | ((3500 / usecs) << VC4_HDMI_CEC_CNT_TO_3500_US_SHIFT)); |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1752 | |
Maxime Ripard | 185e98b | 2021-01-11 15:23:04 +0100 | [diff] [blame] | 1753 | if (!vc4_hdmi->variant->external_irq_controller) |
| 1754 | HDMI_WRITE(HDMI_CEC_CPU_MASK_CLEAR, VC4_HDMI_CPU_CEC); |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1755 | } else { |
Maxime Ripard | 185e98b | 2021-01-11 15:23:04 +0100 | [diff] [blame] | 1756 | if (!vc4_hdmi->variant->external_irq_controller) |
| 1757 | HDMI_WRITE(HDMI_CEC_CPU_MASK_SET, VC4_HDMI_CPU_CEC); |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1758 | HDMI_WRITE(HDMI_CEC_CNTRL_5, val | |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1759 | VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET); |
| 1760 | } |
| 1761 | return 0; |
| 1762 | } |
| 1763 | |
| 1764 | static int vc4_hdmi_cec_adap_log_addr(struct cec_adapter *adap, u8 log_addr) |
| 1765 | { |
Maxime Ripard | 66bf1c3 | 2020-09-03 10:01:18 +0200 | [diff] [blame] | 1766 | struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap); |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1767 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1768 | HDMI_WRITE(HDMI_CEC_CNTRL_1, |
| 1769 | (HDMI_READ(HDMI_CEC_CNTRL_1) & ~VC4_HDMI_CEC_ADDR_MASK) | |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1770 | (log_addr & 0xf) << VC4_HDMI_CEC_ADDR_SHIFT); |
| 1771 | return 0; |
| 1772 | } |
| 1773 | |
| 1774 | static int vc4_hdmi_cec_adap_transmit(struct cec_adapter *adap, u8 attempts, |
| 1775 | u32 signal_free_time, struct cec_msg *msg) |
| 1776 | { |
Maxime Ripard | 66bf1c3 | 2020-09-03 10:01:18 +0200 | [diff] [blame] | 1777 | struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap); |
Dom Cobley | 4a59ed5 | 2021-01-11 15:22:57 +0100 | [diff] [blame] | 1778 | struct drm_device *dev = vc4_hdmi->connector.dev; |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1779 | u32 val; |
| 1780 | unsigned int i; |
| 1781 | |
Dom Cobley | 4a59ed5 | 2021-01-11 15:22:57 +0100 | [diff] [blame] | 1782 | if (msg->len > 16) { |
| 1783 | drm_err(dev, "Attempting to transmit too much data (%d)\n", msg->len); |
| 1784 | return -ENOMEM; |
| 1785 | } |
| 1786 | |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1787 | for (i = 0; i < msg->len; i += 4) |
Dom Cobley | 4a59ed5 | 2021-01-11 15:22:57 +0100 | [diff] [blame] | 1788 | HDMI_WRITE(HDMI_CEC_TX_DATA_1 + (i >> 2), |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1789 | (msg->msg[i]) | |
| 1790 | (msg->msg[i + 1] << 8) | |
| 1791 | (msg->msg[i + 2] << 16) | |
| 1792 | (msg->msg[i + 3] << 24)); |
| 1793 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1794 | val = HDMI_READ(HDMI_CEC_CNTRL_1); |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1795 | val &= ~VC4_HDMI_CEC_START_XMIT_BEGIN; |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1796 | HDMI_WRITE(HDMI_CEC_CNTRL_1, val); |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1797 | val &= ~VC4_HDMI_CEC_MESSAGE_LENGTH_MASK; |
| 1798 | val |= (msg->len - 1) << VC4_HDMI_CEC_MESSAGE_LENGTH_SHIFT; |
| 1799 | val |= VC4_HDMI_CEC_START_XMIT_BEGIN; |
| 1800 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1801 | HDMI_WRITE(HDMI_CEC_CNTRL_1, val); |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1802 | return 0; |
| 1803 | } |
| 1804 | |
| 1805 | static const struct cec_adap_ops vc4_hdmi_cec_adap_ops = { |
| 1806 | .adap_enable = vc4_hdmi_cec_adap_enable, |
| 1807 | .adap_log_addr = vc4_hdmi_cec_adap_log_addr, |
| 1808 | .adap_transmit = vc4_hdmi_cec_adap_transmit, |
| 1809 | }; |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1810 | |
Maxime Ripard | c0791e0 | 2020-09-03 10:01:31 +0200 | [diff] [blame] | 1811 | static int vc4_hdmi_cec_init(struct vc4_hdmi *vc4_hdmi) |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 1812 | { |
Dariusz Marcinkiewicz | 66c2dee | 2019-08-23 13:24:25 +0200 | [diff] [blame] | 1813 | struct cec_connector_info conn_info; |
Maxime Ripard | c0791e0 | 2020-09-03 10:01:31 +0200 | [diff] [blame] | 1814 | struct platform_device *pdev = vc4_hdmi->pdev; |
Maxime Ripard | ae442bf | 2021-01-11 15:23:06 +0100 | [diff] [blame] | 1815 | struct device *dev = &pdev->dev; |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 1816 | u32 value; |
| 1817 | int ret; |
| 1818 | |
Maxime Ripard | ae442bf | 2021-01-11 15:23:06 +0100 | [diff] [blame] | 1819 | if (!of_find_property(dev->of_node, "interrupts", NULL)) { |
| 1820 | dev_warn(dev, "'interrupts' DT property is missing, no CEC\n"); |
| 1821 | return 0; |
| 1822 | } |
| 1823 | |
Maxime Ripard | c0791e0 | 2020-09-03 10:01:31 +0200 | [diff] [blame] | 1824 | vc4_hdmi->cec_adap = cec_allocate_adapter(&vc4_hdmi_cec_adap_ops, |
| 1825 | vc4_hdmi, "vc4", |
| 1826 | CEC_CAP_DEFAULTS | |
| 1827 | CEC_CAP_CONNECTOR_INFO, 1); |
| 1828 | ret = PTR_ERR_OR_ZERO(vc4_hdmi->cec_adap); |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1829 | if (ret < 0) |
Maxime Ripard | c0791e0 | 2020-09-03 10:01:31 +0200 | [diff] [blame] | 1830 | return ret; |
Dariusz Marcinkiewicz | 66c2dee | 2019-08-23 13:24:25 +0200 | [diff] [blame] | 1831 | |
Maxime Ripard | c0791e0 | 2020-09-03 10:01:31 +0200 | [diff] [blame] | 1832 | cec_fill_conn_info_from_drm(&conn_info, &vc4_hdmi->connector); |
| 1833 | cec_s_conn_info(vc4_hdmi->cec_adap, &conn_info); |
Dariusz Marcinkiewicz | 66c2dee | 2019-08-23 13:24:25 +0200 | [diff] [blame] | 1834 | |
Maxime Ripard | c0791e0 | 2020-09-03 10:01:31 +0200 | [diff] [blame] | 1835 | value = HDMI_READ(HDMI_CEC_CNTRL_1); |
Maxime Ripard | 47fa9a8 | 2021-01-11 15:23:01 +0100 | [diff] [blame] | 1836 | /* Set the logical address to Unregistered */ |
| 1837 | value |= VC4_HDMI_CEC_ADDR_MASK; |
Maxime Ripard | c0791e0 | 2020-09-03 10:01:31 +0200 | [diff] [blame] | 1838 | HDMI_WRITE(HDMI_CEC_CNTRL_1, value); |
Maxime Ripard | 47fa9a8 | 2021-01-11 15:23:01 +0100 | [diff] [blame] | 1839 | |
| 1840 | vc4_hdmi_cec_update_clk_div(vc4_hdmi); |
| 1841 | |
Maxime Ripard | 185e98b | 2021-01-11 15:23:04 +0100 | [diff] [blame] | 1842 | if (vc4_hdmi->variant->external_irq_controller) { |
| 1843 | ret = devm_request_threaded_irq(&pdev->dev, |
| 1844 | platform_get_irq_byname(pdev, "cec-rx"), |
| 1845 | vc4_cec_irq_handler_rx_bare, |
| 1846 | vc4_cec_irq_handler_rx_thread, 0, |
| 1847 | "vc4 hdmi cec rx", vc4_hdmi); |
| 1848 | if (ret) |
| 1849 | goto err_delete_cec_adap; |
| 1850 | |
| 1851 | ret = devm_request_threaded_irq(&pdev->dev, |
| 1852 | platform_get_irq_byname(pdev, "cec-tx"), |
| 1853 | vc4_cec_irq_handler_tx_bare, |
| 1854 | vc4_cec_irq_handler_tx_thread, 0, |
| 1855 | "vc4 hdmi cec tx", vc4_hdmi); |
| 1856 | if (ret) |
| 1857 | goto err_delete_cec_adap; |
| 1858 | } else { |
| 1859 | HDMI_WRITE(HDMI_CEC_CPU_MASK_SET, 0xffffffff); |
| 1860 | |
| 1861 | ret = devm_request_threaded_irq(&pdev->dev, platform_get_irq(pdev, 0), |
| 1862 | vc4_cec_irq_handler, |
| 1863 | vc4_cec_irq_handler_thread, 0, |
| 1864 | "vc4 hdmi cec", vc4_hdmi); |
| 1865 | if (ret) |
| 1866 | goto err_delete_cec_adap; |
| 1867 | } |
Maxime Ripard | c0791e0 | 2020-09-03 10:01:31 +0200 | [diff] [blame] | 1868 | |
| 1869 | ret = cec_register_adapter(vc4_hdmi->cec_adap, &pdev->dev); |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1870 | if (ret < 0) |
| 1871 | goto err_delete_cec_adap; |
Eric Anholt | c9be804 | 2019-04-01 11:35:58 -0700 | [diff] [blame] | 1872 | |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 1873 | return 0; |
| 1874 | |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1875 | err_delete_cec_adap: |
Maxime Ripard | c0791e0 | 2020-09-03 10:01:31 +0200 | [diff] [blame] | 1876 | cec_delete_adapter(vc4_hdmi->cec_adap); |
| 1877 | |
| 1878 | return ret; |
| 1879 | } |
| 1880 | |
| 1881 | static void vc4_hdmi_cec_exit(struct vc4_hdmi *vc4_hdmi) |
| 1882 | { |
| 1883 | cec_unregister_adapter(vc4_hdmi->cec_adap); |
| 1884 | } |
| 1885 | #else |
| 1886 | static int vc4_hdmi_cec_init(struct vc4_hdmi *vc4_hdmi) |
| 1887 | { |
| 1888 | return 0; |
| 1889 | } |
| 1890 | |
| 1891 | static void vc4_hdmi_cec_exit(struct vc4_hdmi *vc4_hdmi) {}; |
| 1892 | |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1893 | #endif |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 1894 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1895 | static int vc4_hdmi_build_regset(struct vc4_hdmi *vc4_hdmi, |
| 1896 | struct debugfs_regset32 *regset, |
| 1897 | enum vc4_hdmi_regs reg) |
| 1898 | { |
| 1899 | const struct vc4_hdmi_variant *variant = vc4_hdmi->variant; |
| 1900 | struct debugfs_reg32 *regs, *new_regs; |
| 1901 | unsigned int count = 0; |
| 1902 | unsigned int i; |
| 1903 | |
| 1904 | regs = kcalloc(variant->num_registers, sizeof(*regs), |
| 1905 | GFP_KERNEL); |
| 1906 | if (!regs) |
| 1907 | return -ENOMEM; |
| 1908 | |
| 1909 | for (i = 0; i < variant->num_registers; i++) { |
| 1910 | const struct vc4_hdmi_register *field = &variant->registers[i]; |
| 1911 | |
| 1912 | if (field->reg != reg) |
| 1913 | continue; |
| 1914 | |
| 1915 | regs[count].name = field->name; |
| 1916 | regs[count].offset = field->offset; |
| 1917 | count++; |
| 1918 | } |
| 1919 | |
| 1920 | new_regs = krealloc(regs, count * sizeof(*regs), GFP_KERNEL); |
| 1921 | if (!new_regs) |
| 1922 | return -ENOMEM; |
| 1923 | |
| 1924 | regset->base = __vc4_hdmi_get_field_base(vc4_hdmi, reg); |
| 1925 | regset->regs = new_regs; |
| 1926 | regset->nregs = count; |
| 1927 | |
| 1928 | return 0; |
| 1929 | } |
| 1930 | |
Maxime Ripard | 33c773e | 2020-09-03 10:01:22 +0200 | [diff] [blame] | 1931 | static int vc4_hdmi_init_resources(struct vc4_hdmi *vc4_hdmi) |
| 1932 | { |
| 1933 | struct platform_device *pdev = vc4_hdmi->pdev; |
| 1934 | struct device *dev = &pdev->dev; |
| 1935 | int ret; |
| 1936 | |
| 1937 | vc4_hdmi->hdmicore_regs = vc4_ioremap_regs(pdev, 0); |
| 1938 | if (IS_ERR(vc4_hdmi->hdmicore_regs)) |
| 1939 | return PTR_ERR(vc4_hdmi->hdmicore_regs); |
| 1940 | |
| 1941 | vc4_hdmi->hd_regs = vc4_ioremap_regs(pdev, 1); |
| 1942 | if (IS_ERR(vc4_hdmi->hd_regs)) |
| 1943 | return PTR_ERR(vc4_hdmi->hd_regs); |
| 1944 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1945 | ret = vc4_hdmi_build_regset(vc4_hdmi, &vc4_hdmi->hd_regset, VC4_HD); |
| 1946 | if (ret) |
| 1947 | return ret; |
Maxime Ripard | 33c773e | 2020-09-03 10:01:22 +0200 | [diff] [blame] | 1948 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1949 | ret = vc4_hdmi_build_regset(vc4_hdmi, &vc4_hdmi->hdmi_regset, VC4_HDMI); |
| 1950 | if (ret) |
| 1951 | return ret; |
Maxime Ripard | 33c773e | 2020-09-03 10:01:22 +0200 | [diff] [blame] | 1952 | |
| 1953 | vc4_hdmi->pixel_clock = devm_clk_get(dev, "pixel"); |
| 1954 | if (IS_ERR(vc4_hdmi->pixel_clock)) { |
| 1955 | ret = PTR_ERR(vc4_hdmi->pixel_clock); |
| 1956 | if (ret != -EPROBE_DEFER) |
| 1957 | DRM_ERROR("Failed to get pixel clock\n"); |
| 1958 | return ret; |
| 1959 | } |
| 1960 | |
| 1961 | vc4_hdmi->hsm_clock = devm_clk_get(dev, "hdmi"); |
| 1962 | if (IS_ERR(vc4_hdmi->hsm_clock)) { |
| 1963 | DRM_ERROR("Failed to get HDMI state machine clock\n"); |
| 1964 | return PTR_ERR(vc4_hdmi->hsm_clock); |
| 1965 | } |
Dave Stevenson | 632ee3a | 2020-09-03 10:01:40 +0200 | [diff] [blame] | 1966 | vc4_hdmi->audio_clock = vc4_hdmi->hsm_clock; |
Maxime Ripard | 23b7eb5 | 2021-01-11 15:23:02 +0100 | [diff] [blame] | 1967 | vc4_hdmi->cec_clock = vc4_hdmi->hsm_clock; |
Maxime Ripard | 33c773e | 2020-09-03 10:01:22 +0200 | [diff] [blame] | 1968 | |
| 1969 | return 0; |
| 1970 | } |
| 1971 | |
Maxime Ripard | 8323989 | 2020-09-03 10:01:48 +0200 | [diff] [blame] | 1972 | static int vc5_hdmi_init_resources(struct vc4_hdmi *vc4_hdmi) |
| 1973 | { |
| 1974 | struct platform_device *pdev = vc4_hdmi->pdev; |
| 1975 | struct device *dev = &pdev->dev; |
| 1976 | struct resource *res; |
| 1977 | |
| 1978 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hdmi"); |
| 1979 | if (!res) |
| 1980 | return -ENODEV; |
| 1981 | |
| 1982 | vc4_hdmi->hdmicore_regs = devm_ioremap(dev, res->start, |
| 1983 | resource_size(res)); |
Dan Carpenter | 14929c5 | 2020-09-10 13:08:25 +0300 | [diff] [blame] | 1984 | if (!vc4_hdmi->hdmicore_regs) |
| 1985 | return -ENOMEM; |
Maxime Ripard | 8323989 | 2020-09-03 10:01:48 +0200 | [diff] [blame] | 1986 | |
| 1987 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hd"); |
| 1988 | if (!res) |
| 1989 | return -ENODEV; |
| 1990 | |
| 1991 | vc4_hdmi->hd_regs = devm_ioremap(dev, res->start, resource_size(res)); |
Dan Carpenter | 14929c5 | 2020-09-10 13:08:25 +0300 | [diff] [blame] | 1992 | if (!vc4_hdmi->hd_regs) |
| 1993 | return -ENOMEM; |
Maxime Ripard | 8323989 | 2020-09-03 10:01:48 +0200 | [diff] [blame] | 1994 | |
| 1995 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cec"); |
| 1996 | if (!res) |
| 1997 | return -ENODEV; |
| 1998 | |
| 1999 | vc4_hdmi->cec_regs = devm_ioremap(dev, res->start, resource_size(res)); |
Dan Carpenter | 14929c5 | 2020-09-10 13:08:25 +0300 | [diff] [blame] | 2000 | if (!vc4_hdmi->cec_regs) |
| 2001 | return -ENOMEM; |
Maxime Ripard | 8323989 | 2020-09-03 10:01:48 +0200 | [diff] [blame] | 2002 | |
| 2003 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "csc"); |
| 2004 | if (!res) |
| 2005 | return -ENODEV; |
| 2006 | |
| 2007 | vc4_hdmi->csc_regs = devm_ioremap(dev, res->start, resource_size(res)); |
Dan Carpenter | 14929c5 | 2020-09-10 13:08:25 +0300 | [diff] [blame] | 2008 | if (!vc4_hdmi->csc_regs) |
| 2009 | return -ENOMEM; |
Maxime Ripard | 8323989 | 2020-09-03 10:01:48 +0200 | [diff] [blame] | 2010 | |
| 2011 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dvp"); |
| 2012 | if (!res) |
| 2013 | return -ENODEV; |
| 2014 | |
| 2015 | vc4_hdmi->dvp_regs = devm_ioremap(dev, res->start, resource_size(res)); |
Dan Carpenter | 14929c5 | 2020-09-10 13:08:25 +0300 | [diff] [blame] | 2016 | if (!vc4_hdmi->dvp_regs) |
| 2017 | return -ENOMEM; |
Maxime Ripard | 8323989 | 2020-09-03 10:01:48 +0200 | [diff] [blame] | 2018 | |
| 2019 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy"); |
| 2020 | if (!res) |
| 2021 | return -ENODEV; |
| 2022 | |
| 2023 | vc4_hdmi->phy_regs = devm_ioremap(dev, res->start, resource_size(res)); |
Dan Carpenter | 14929c5 | 2020-09-10 13:08:25 +0300 | [diff] [blame] | 2024 | if (!vc4_hdmi->phy_regs) |
| 2025 | return -ENOMEM; |
Maxime Ripard | 8323989 | 2020-09-03 10:01:48 +0200 | [diff] [blame] | 2026 | |
| 2027 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "packet"); |
| 2028 | if (!res) |
| 2029 | return -ENODEV; |
| 2030 | |
| 2031 | vc4_hdmi->ram_regs = devm_ioremap(dev, res->start, resource_size(res)); |
Dan Carpenter | 14929c5 | 2020-09-10 13:08:25 +0300 | [diff] [blame] | 2032 | if (!vc4_hdmi->ram_regs) |
| 2033 | return -ENOMEM; |
Maxime Ripard | 8323989 | 2020-09-03 10:01:48 +0200 | [diff] [blame] | 2034 | |
| 2035 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rm"); |
| 2036 | if (!res) |
| 2037 | return -ENODEV; |
| 2038 | |
| 2039 | vc4_hdmi->rm_regs = devm_ioremap(dev, res->start, resource_size(res)); |
Dan Carpenter | 14929c5 | 2020-09-10 13:08:25 +0300 | [diff] [blame] | 2040 | if (!vc4_hdmi->rm_regs) |
| 2041 | return -ENOMEM; |
Maxime Ripard | 8323989 | 2020-09-03 10:01:48 +0200 | [diff] [blame] | 2042 | |
| 2043 | vc4_hdmi->hsm_clock = devm_clk_get(dev, "hdmi"); |
| 2044 | if (IS_ERR(vc4_hdmi->hsm_clock)) { |
| 2045 | DRM_ERROR("Failed to get HDMI state machine clock\n"); |
| 2046 | return PTR_ERR(vc4_hdmi->hsm_clock); |
| 2047 | } |
| 2048 | |
| 2049 | vc4_hdmi->pixel_bvb_clock = devm_clk_get(dev, "bvb"); |
| 2050 | if (IS_ERR(vc4_hdmi->pixel_bvb_clock)) { |
| 2051 | DRM_ERROR("Failed to get pixel bvb clock\n"); |
| 2052 | return PTR_ERR(vc4_hdmi->pixel_bvb_clock); |
| 2053 | } |
| 2054 | |
| 2055 | vc4_hdmi->audio_clock = devm_clk_get(dev, "audio"); |
| 2056 | if (IS_ERR(vc4_hdmi->audio_clock)) { |
| 2057 | DRM_ERROR("Failed to get audio clock\n"); |
| 2058 | return PTR_ERR(vc4_hdmi->audio_clock); |
| 2059 | } |
| 2060 | |
Maxime Ripard | 23b7eb5 | 2021-01-11 15:23:02 +0100 | [diff] [blame] | 2061 | vc4_hdmi->cec_clock = devm_clk_get(dev, "cec"); |
| 2062 | if (IS_ERR(vc4_hdmi->cec_clock)) { |
| 2063 | DRM_ERROR("Failed to get CEC clock\n"); |
| 2064 | return PTR_ERR(vc4_hdmi->cec_clock); |
| 2065 | } |
| 2066 | |
Maxime Ripard | 8323989 | 2020-09-03 10:01:48 +0200 | [diff] [blame] | 2067 | vc4_hdmi->reset = devm_reset_control_get(dev, NULL); |
| 2068 | if (IS_ERR(vc4_hdmi->reset)) { |
| 2069 | DRM_ERROR("Failed to get HDMI reset line\n"); |
| 2070 | return PTR_ERR(vc4_hdmi->reset); |
| 2071 | } |
| 2072 | |
| 2073 | return 0; |
| 2074 | } |
| 2075 | |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 2076 | static int vc4_hdmi_bind(struct device *dev, struct device *master, void *data) |
| 2077 | { |
Maxime Ripard | 33c773e | 2020-09-03 10:01:22 +0200 | [diff] [blame] | 2078 | const struct vc4_hdmi_variant *variant = of_device_get_match_data(dev); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 2079 | struct platform_device *pdev = to_platform_device(dev); |
| 2080 | struct drm_device *drm = dev_get_drvdata(master); |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 2081 | struct vc4_hdmi *vc4_hdmi; |
Maxime Ripard | c98c85b | 2020-09-03 10:01:12 +0200 | [diff] [blame] | 2082 | struct drm_encoder *encoder; |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 2083 | struct device_node *ddc_node; |
| 2084 | u32 value; |
| 2085 | int ret; |
| 2086 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 2087 | vc4_hdmi = devm_kzalloc(dev, sizeof(*vc4_hdmi), GFP_KERNEL); |
| 2088 | if (!vc4_hdmi) |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 2089 | return -ENOMEM; |
| 2090 | |
Maxime Ripard | 47c167b | 2020-09-03 10:01:19 +0200 | [diff] [blame] | 2091 | dev_set_drvdata(dev, vc4_hdmi); |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 2092 | encoder = &vc4_hdmi->encoder.base.base; |
Maxime Ripard | 7d73299 | 2020-09-03 10:01:29 +0200 | [diff] [blame] | 2093 | vc4_hdmi->encoder.base.type = variant->encoder_type; |
Maxime Ripard | 09c4381 | 2020-09-03 10:01:44 +0200 | [diff] [blame] | 2094 | vc4_hdmi->encoder.base.pre_crtc_configure = vc4_hdmi_encoder_pre_crtc_configure; |
| 2095 | vc4_hdmi->encoder.base.pre_crtc_enable = vc4_hdmi_encoder_pre_crtc_enable; |
| 2096 | vc4_hdmi->encoder.base.post_crtc_enable = vc4_hdmi_encoder_post_crtc_enable; |
| 2097 | vc4_hdmi->encoder.base.post_crtc_disable = vc4_hdmi_encoder_post_crtc_disable; |
| 2098 | vc4_hdmi->encoder.base.post_crtc_powerdown = vc4_hdmi_encoder_post_crtc_powerdown; |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 2099 | vc4_hdmi->pdev = pdev; |
Maxime Ripard | 33c773e | 2020-09-03 10:01:22 +0200 | [diff] [blame] | 2100 | vc4_hdmi->variant = variant; |
Maxime Ripard | c98c85b | 2020-09-03 10:01:12 +0200 | [diff] [blame] | 2101 | |
Maxime Ripard | 33c773e | 2020-09-03 10:01:22 +0200 | [diff] [blame] | 2102 | ret = variant->init_resources(vc4_hdmi); |
| 2103 | if (ret) |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 2104 | return ret; |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 2105 | |
| 2106 | ddc_node = of_parse_phandle(dev->of_node, "ddc", 0); |
| 2107 | if (!ddc_node) { |
| 2108 | DRM_ERROR("Failed to find ddc node in device tree\n"); |
| 2109 | return -ENODEV; |
| 2110 | } |
| 2111 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 2112 | vc4_hdmi->ddc = of_find_i2c_adapter_by_node(ddc_node); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 2113 | of_node_put(ddc_node); |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 2114 | if (!vc4_hdmi->ddc) { |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 2115 | DRM_DEBUG("Failed to get ddc i2c adapter by node\n"); |
| 2116 | return -EPROBE_DEFER; |
| 2117 | } |
| 2118 | |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 2119 | /* Only use the GPIO HPD pin if present in the DT, otherwise |
| 2120 | * we'll use the HDMI core's register. |
| 2121 | */ |
| 2122 | if (of_find_property(dev->of_node, "hpd-gpios", &value)) { |
| 2123 | enum of_gpio_flags hpd_gpio_flags; |
| 2124 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 2125 | vc4_hdmi->hpd_gpio = of_get_named_gpio_flags(dev->of_node, |
| 2126 | "hpd-gpios", 0, |
| 2127 | &hpd_gpio_flags); |
| 2128 | if (vc4_hdmi->hpd_gpio < 0) { |
| 2129 | ret = vc4_hdmi->hpd_gpio; |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 2130 | goto err_unprepare_hsm; |
| 2131 | } |
| 2132 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 2133 | vc4_hdmi->hpd_active_low = hpd_gpio_flags & OF_GPIO_ACTIVE_LOW; |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 2134 | } |
| 2135 | |
Maxime Ripard | 9fa1d7e | 2020-10-29 14:40:17 +0100 | [diff] [blame] | 2136 | vc4_hdmi->disable_wifi_frequencies = |
| 2137 | of_property_read_bool(dev->of_node, "wifi-2.4ghz-coexistence"); |
| 2138 | |
Maxime Ripard | 86e3a65 | 2021-05-07 17:05:12 +0200 | [diff] [blame] | 2139 | if (variant->max_pixel_clock == 600000000) { |
| 2140 | struct vc4_dev *vc4 = to_vc4_dev(drm); |
| 2141 | long max_rate = clk_round_rate(vc4->hvs->core_clk, 550000000); |
| 2142 | |
| 2143 | if (max_rate < 550000000) |
| 2144 | vc4_hdmi->disable_4kp60 = true; |
| 2145 | } |
| 2146 | |
Dom Cobley | 902dc5c1 | 2021-01-11 15:22:56 +0100 | [diff] [blame] | 2147 | if (vc4_hdmi->variant->reset) |
| 2148 | vc4_hdmi->variant->reset(vc4_hdmi); |
| 2149 | |
Maxime Ripard | 5b00600 | 2021-05-07 17:05:09 +0200 | [diff] [blame] | 2150 | if ((of_device_is_compatible(dev->of_node, "brcm,bcm2711-hdmi0") || |
| 2151 | of_device_is_compatible(dev->of_node, "brcm,bcm2711-hdmi1")) && |
| 2152 | HDMI_READ(HDMI_VID_CTL) & VC4_HD_VID_CTL_ENABLE) { |
| 2153 | clk_prepare_enable(vc4_hdmi->pixel_clock); |
| 2154 | clk_prepare_enable(vc4_hdmi->hsm_clock); |
| 2155 | clk_prepare_enable(vc4_hdmi->pixel_bvb_clock); |
| 2156 | } |
| 2157 | |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 2158 | pm_runtime_enable(dev); |
| 2159 | |
Maxime Ripard | c98c85b | 2020-09-03 10:01:12 +0200 | [diff] [blame] | 2160 | drm_simple_encoder_init(drm, encoder, DRM_MODE_ENCODER_TMDS); |
| 2161 | drm_encoder_helper_add(encoder, &vc4_hdmi_encoder_helper_funcs); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 2162 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 2163 | ret = vc4_hdmi_connector_init(drm, vc4_hdmi); |
Maxime Ripard | c98c85b | 2020-09-03 10:01:12 +0200 | [diff] [blame] | 2164 | if (ret) |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 2165 | goto err_destroy_encoder; |
Maxime Ripard | c98c85b | 2020-09-03 10:01:12 +0200 | [diff] [blame] | 2166 | |
Maxime Ripard | c0791e0 | 2020-09-03 10:01:31 +0200 | [diff] [blame] | 2167 | ret = vc4_hdmi_cec_init(vc4_hdmi); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 2168 | if (ret) |
Maxime Ripard | c0791e0 | 2020-09-03 10:01:31 +0200 | [diff] [blame] | 2169 | goto err_destroy_conn; |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 2170 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 2171 | ret = vc4_hdmi_audio_init(vc4_hdmi); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 2172 | if (ret) |
Maxime Ripard | c0791e0 | 2020-09-03 10:01:31 +0200 | [diff] [blame] | 2173 | goto err_free_cec; |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 2174 | |
Maxime Ripard | b2405c9 | 2020-09-03 10:01:30 +0200 | [diff] [blame] | 2175 | vc4_debugfs_add_file(drm, variant->debugfs_name, |
| 2176 | vc4_hdmi_debugfs_regs, |
| 2177 | vc4_hdmi); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 2178 | |
| 2179 | return 0; |
| 2180 | |
Maxime Ripard | c0791e0 | 2020-09-03 10:01:31 +0200 | [diff] [blame] | 2181 | err_free_cec: |
| 2182 | vc4_hdmi_cec_exit(vc4_hdmi); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 2183 | err_destroy_conn: |
Maxime Ripard | 0532e5e | 2020-09-03 10:01:21 +0200 | [diff] [blame] | 2184 | vc4_hdmi_connector_destroy(&vc4_hdmi->connector); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 2185 | err_destroy_encoder: |
Maxime Ripard | c98c85b | 2020-09-03 10:01:12 +0200 | [diff] [blame] | 2186 | drm_encoder_cleanup(encoder); |
Hans Verkuil | 10ee275 | 2017-07-16 12:48:03 +0200 | [diff] [blame] | 2187 | err_unprepare_hsm: |
Boris Brezillon | 4f6e3d6 | 2017-04-11 18:39:25 +0200 | [diff] [blame] | 2188 | pm_runtime_disable(dev); |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 2189 | put_device(&vc4_hdmi->ddc->dev); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 2190 | |
| 2191 | return ret; |
| 2192 | } |
| 2193 | |
| 2194 | static void vc4_hdmi_unbind(struct device *dev, struct device *master, |
| 2195 | void *data) |
| 2196 | { |
Maxime Ripard | 47c167b | 2020-09-03 10:01:19 +0200 | [diff] [blame] | 2197 | struct vc4_hdmi *vc4_hdmi; |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 2198 | |
Maxime Ripard | 47c167b | 2020-09-03 10:01:19 +0200 | [diff] [blame] | 2199 | /* |
| 2200 | * ASoC makes it a bit hard to retrieve a pointer to the |
| 2201 | * vc4_hdmi structure. Registering the card will overwrite our |
| 2202 | * device drvdata with a pointer to the snd_soc_card structure, |
| 2203 | * which can then be used to retrieve whatever drvdata we want |
| 2204 | * to associate. |
| 2205 | * |
| 2206 | * However, that doesn't fly in the case where we wouldn't |
| 2207 | * register an ASoC card (because of an old DT that is missing |
| 2208 | * the dmas properties for example), then the card isn't |
| 2209 | * registered and the device drvdata wouldn't be set. |
| 2210 | * |
| 2211 | * We can deal with both cases by making sure a snd_soc_card |
| 2212 | * pointer and a vc4_hdmi structure are pointing to the same |
| 2213 | * memory address, so we can treat them indistinctly without any |
| 2214 | * issue. |
| 2215 | */ |
| 2216 | BUILD_BUG_ON(offsetof(struct vc4_hdmi_audio, card) != 0); |
| 2217 | BUILD_BUG_ON(offsetof(struct vc4_hdmi, audio) != 0); |
| 2218 | vc4_hdmi = dev_get_drvdata(dev); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 2219 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 2220 | kfree(vc4_hdmi->hdmi_regset.regs); |
| 2221 | kfree(vc4_hdmi->hd_regset.regs); |
| 2222 | |
Maxime Ripard | c0791e0 | 2020-09-03 10:01:31 +0200 | [diff] [blame] | 2223 | vc4_hdmi_cec_exit(vc4_hdmi); |
Maxime Ripard | 0532e5e | 2020-09-03 10:01:21 +0200 | [diff] [blame] | 2224 | vc4_hdmi_connector_destroy(&vc4_hdmi->connector); |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 2225 | drm_encoder_cleanup(&vc4_hdmi->encoder.base.base); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 2226 | |
Boris Brezillon | 4f6e3d6 | 2017-04-11 18:39:25 +0200 | [diff] [blame] | 2227 | pm_runtime_disable(dev); |
| 2228 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 2229 | put_device(&vc4_hdmi->ddc->dev); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 2230 | } |
| 2231 | |
| 2232 | static const struct component_ops vc4_hdmi_ops = { |
| 2233 | .bind = vc4_hdmi_bind, |
| 2234 | .unbind = vc4_hdmi_unbind, |
| 2235 | }; |
| 2236 | |
| 2237 | static int vc4_hdmi_dev_probe(struct platform_device *pdev) |
| 2238 | { |
| 2239 | return component_add(&pdev->dev, &vc4_hdmi_ops); |
| 2240 | } |
| 2241 | |
| 2242 | static int vc4_hdmi_dev_remove(struct platform_device *pdev) |
| 2243 | { |
| 2244 | component_del(&pdev->dev, &vc4_hdmi_ops); |
| 2245 | return 0; |
| 2246 | } |
| 2247 | |
Maxime Ripard | 33c773e | 2020-09-03 10:01:22 +0200 | [diff] [blame] | 2248 | static const struct vc4_hdmi_variant bcm2835_variant = { |
Maxime Ripard | 7d73299 | 2020-09-03 10:01:29 +0200 | [diff] [blame] | 2249 | .encoder_type = VC4_ENCODER_TYPE_HDMI0, |
Maxime Ripard | b2405c9 | 2020-09-03 10:01:30 +0200 | [diff] [blame] | 2250 | .debugfs_name = "hdmi_regs", |
Maxime Ripard | 9be43a5 | 2020-09-03 10:01:41 +0200 | [diff] [blame] | 2251 | .card_name = "vc4-hdmi", |
Maxime Ripard | cd4cb49 | 2020-09-03 10:01:35 +0200 | [diff] [blame] | 2252 | .max_pixel_clock = 162000000, |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 2253 | .registers = vc4_hdmi_fields, |
| 2254 | .num_registers = ARRAY_SIZE(vc4_hdmi_fields), |
| 2255 | |
Maxime Ripard | 33c773e | 2020-09-03 10:01:22 +0200 | [diff] [blame] | 2256 | .init_resources = vc4_hdmi_init_resources, |
Maxime Ripard | 89f31a2 | 2020-09-03 10:01:27 +0200 | [diff] [blame] | 2257 | .csc_setup = vc4_hdmi_csc_setup, |
Maxime Ripard | 9045e91 | 2020-09-03 10:01:24 +0200 | [diff] [blame] | 2258 | .reset = vc4_hdmi_reset, |
Maxime Ripard | 904f668 | 2020-09-03 10:01:28 +0200 | [diff] [blame] | 2259 | .set_timings = vc4_hdmi_set_timings, |
Maxime Ripard | c457b8a | 2020-09-03 10:01:25 +0200 | [diff] [blame] | 2260 | .phy_init = vc4_hdmi_phy_init, |
| 2261 | .phy_disable = vc4_hdmi_phy_disable, |
Maxime Ripard | 647b965 | 2020-09-03 10:01:26 +0200 | [diff] [blame] | 2262 | .phy_rng_enable = vc4_hdmi_phy_rng_enable, |
| 2263 | .phy_rng_disable = vc4_hdmi_phy_rng_disable, |
Dave Stevenson | 632ee3a | 2020-09-03 10:01:40 +0200 | [diff] [blame] | 2264 | .channel_map = vc4_hdmi_channel_map, |
Dave Stevenson | bccd5c5 | 2021-04-30 11:44:49 +0200 | [diff] [blame] | 2265 | .supports_hdr = false, |
Maxime Ripard | 33c773e | 2020-09-03 10:01:22 +0200 | [diff] [blame] | 2266 | }; |
| 2267 | |
Maxime Ripard | 8323989 | 2020-09-03 10:01:48 +0200 | [diff] [blame] | 2268 | static const struct vc4_hdmi_variant bcm2711_hdmi0_variant = { |
| 2269 | .encoder_type = VC4_ENCODER_TYPE_HDMI0, |
| 2270 | .debugfs_name = "hdmi0_regs", |
| 2271 | .card_name = "vc4-hdmi-0", |
Maxime Ripard | 24169a2 | 2020-12-15 16:42:42 +0100 | [diff] [blame] | 2272 | .max_pixel_clock = HDMI_14_MAX_TMDS_CLK, |
Maxime Ripard | 8323989 | 2020-09-03 10:01:48 +0200 | [diff] [blame] | 2273 | .registers = vc5_hdmi_hdmi0_fields, |
| 2274 | .num_registers = ARRAY_SIZE(vc5_hdmi_hdmi0_fields), |
| 2275 | .phy_lane_mapping = { |
| 2276 | PHY_LANE_0, |
| 2277 | PHY_LANE_1, |
| 2278 | PHY_LANE_2, |
| 2279 | PHY_LANE_CK, |
| 2280 | }, |
Maxime Ripard | 57fb32e | 2020-10-29 13:25:22 +0100 | [diff] [blame] | 2281 | .unsupported_odd_h_timings = true, |
Maxime Ripard | 185e98b | 2021-01-11 15:23:04 +0100 | [diff] [blame] | 2282 | .external_irq_controller = true, |
Maxime Ripard | 8323989 | 2020-09-03 10:01:48 +0200 | [diff] [blame] | 2283 | |
| 2284 | .init_resources = vc5_hdmi_init_resources, |
| 2285 | .csc_setup = vc5_hdmi_csc_setup, |
| 2286 | .reset = vc5_hdmi_reset, |
| 2287 | .set_timings = vc5_hdmi_set_timings, |
| 2288 | .phy_init = vc5_hdmi_phy_init, |
| 2289 | .phy_disable = vc5_hdmi_phy_disable, |
| 2290 | .phy_rng_enable = vc5_hdmi_phy_rng_enable, |
| 2291 | .phy_rng_disable = vc5_hdmi_phy_rng_disable, |
| 2292 | .channel_map = vc5_hdmi_channel_map, |
Dave Stevenson | bccd5c5 | 2021-04-30 11:44:49 +0200 | [diff] [blame] | 2293 | .supports_hdr = true, |
Maxime Ripard | 8323989 | 2020-09-03 10:01:48 +0200 | [diff] [blame] | 2294 | }; |
| 2295 | |
| 2296 | static const struct vc4_hdmi_variant bcm2711_hdmi1_variant = { |
| 2297 | .encoder_type = VC4_ENCODER_TYPE_HDMI1, |
| 2298 | .debugfs_name = "hdmi1_regs", |
| 2299 | .card_name = "vc4-hdmi-1", |
Maxime Ripard | 24169a2 | 2020-12-15 16:42:42 +0100 | [diff] [blame] | 2300 | .max_pixel_clock = HDMI_14_MAX_TMDS_CLK, |
Maxime Ripard | 8323989 | 2020-09-03 10:01:48 +0200 | [diff] [blame] | 2301 | .registers = vc5_hdmi_hdmi1_fields, |
| 2302 | .num_registers = ARRAY_SIZE(vc5_hdmi_hdmi1_fields), |
| 2303 | .phy_lane_mapping = { |
| 2304 | PHY_LANE_1, |
| 2305 | PHY_LANE_0, |
| 2306 | PHY_LANE_CK, |
| 2307 | PHY_LANE_2, |
| 2308 | }, |
Maxime Ripard | 57fb32e | 2020-10-29 13:25:22 +0100 | [diff] [blame] | 2309 | .unsupported_odd_h_timings = true, |
Maxime Ripard | 185e98b | 2021-01-11 15:23:04 +0100 | [diff] [blame] | 2310 | .external_irq_controller = true, |
Maxime Ripard | 8323989 | 2020-09-03 10:01:48 +0200 | [diff] [blame] | 2311 | |
| 2312 | .init_resources = vc5_hdmi_init_resources, |
| 2313 | .csc_setup = vc5_hdmi_csc_setup, |
| 2314 | .reset = vc5_hdmi_reset, |
| 2315 | .set_timings = vc5_hdmi_set_timings, |
| 2316 | .phy_init = vc5_hdmi_phy_init, |
| 2317 | .phy_disable = vc5_hdmi_phy_disable, |
| 2318 | .phy_rng_enable = vc5_hdmi_phy_rng_enable, |
| 2319 | .phy_rng_disable = vc5_hdmi_phy_rng_disable, |
| 2320 | .channel_map = vc5_hdmi_channel_map, |
Dave Stevenson | bccd5c5 | 2021-04-30 11:44:49 +0200 | [diff] [blame] | 2321 | .supports_hdr = true, |
Maxime Ripard | 8323989 | 2020-09-03 10:01:48 +0200 | [diff] [blame] | 2322 | }; |
| 2323 | |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 2324 | static const struct of_device_id vc4_hdmi_dt_match[] = { |
Maxime Ripard | 33c773e | 2020-09-03 10:01:22 +0200 | [diff] [blame] | 2325 | { .compatible = "brcm,bcm2835-hdmi", .data = &bcm2835_variant }, |
Maxime Ripard | 8323989 | 2020-09-03 10:01:48 +0200 | [diff] [blame] | 2326 | { .compatible = "brcm,bcm2711-hdmi0", .data = &bcm2711_hdmi0_variant }, |
| 2327 | { .compatible = "brcm,bcm2711-hdmi1", .data = &bcm2711_hdmi1_variant }, |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 2328 | {} |
| 2329 | }; |
| 2330 | |
| 2331 | struct platform_driver vc4_hdmi_driver = { |
| 2332 | .probe = vc4_hdmi_dev_probe, |
| 2333 | .remove = vc4_hdmi_dev_remove, |
| 2334 | .driver = { |
| 2335 | .name = "vc4_hdmi", |
| 2336 | .of_match_table = vc4_hdmi_dt_match, |
| 2337 | }, |
| 2338 | }; |