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