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