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> |
Masahiro Yamada | b7e8e25 | 2017-05-18 13:29:38 +0900 | [diff] [blame] | 38 | #include <linux/clk.h> |
| 39 | #include <linux/component.h> |
| 40 | #include <linux/i2c.h> |
| 41 | #include <linux/of_address.h> |
| 42 | #include <linux/of_gpio.h> |
| 43 | #include <linux/of_platform.h> |
| 44 | #include <linux/pm_runtime.h> |
| 45 | #include <linux/rational.h> |
| 46 | #include <sound/dmaengine_pcm.h> |
| 47 | #include <sound/pcm_drm_eld.h> |
| 48 | #include <sound/pcm_params.h> |
| 49 | #include <sound/soc.h> |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 50 | #include "media/cec.h" |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 51 | #include "vc4_drv.h" |
Maxime Ripard | f73100c | 2020-09-03 10:01:11 +0200 | [diff] [blame] | 52 | #include "vc4_hdmi.h" |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 53 | #include "vc4_hdmi_regs.h" |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 54 | #include "vc4_regs.h" |
| 55 | |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 56 | #define HSM_CLOCK_FREQ 163682864 |
| 57 | #define CEC_CLOCK_FREQ 40000 |
| 58 | #define CEC_CLOCK_DIV (HSM_CLOCK_FREQ / CEC_CLOCK_FREQ) |
| 59 | |
Eric Anholt | c9be804 | 2019-04-01 11:35:58 -0700 | [diff] [blame] | 60 | static int vc4_hdmi_debugfs_regs(struct seq_file *m, void *unused) |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 61 | { |
| 62 | struct drm_info_node *node = (struct drm_info_node *)m->private; |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 63 | struct vc4_hdmi *vc4_hdmi = node->info_ent->data; |
Eric Anholt | 3051719 | 2019-02-20 13:03:38 -0800 | [diff] [blame] | 64 | struct drm_printer p = drm_seq_file_printer(m); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 65 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 66 | drm_print_regset32(&p, &vc4_hdmi->hdmi_regset); |
| 67 | drm_print_regset32(&p, &vc4_hdmi->hd_regset); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 68 | |
| 69 | return 0; |
| 70 | } |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 71 | |
Maxime Ripard | 9045e91 | 2020-09-03 10:01:24 +0200 | [diff] [blame] | 72 | static void vc4_hdmi_reset(struct vc4_hdmi *vc4_hdmi) |
| 73 | { |
| 74 | HDMI_WRITE(HDMI_M_CTL, VC4_HD_M_SW_RST); |
| 75 | udelay(1); |
| 76 | HDMI_WRITE(HDMI_M_CTL, 0); |
| 77 | |
| 78 | HDMI_WRITE(HDMI_M_CTL, VC4_HD_M_ENABLE); |
| 79 | |
| 80 | HDMI_WRITE(HDMI_SW_RESET_CONTROL, |
| 81 | VC4_HDMI_SW_RESET_HDMI | |
| 82 | VC4_HDMI_SW_RESET_FORMAT_DETECT); |
| 83 | |
| 84 | HDMI_WRITE(HDMI_SW_RESET_CONTROL, 0); |
| 85 | } |
| 86 | |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 87 | static enum drm_connector_status |
| 88 | vc4_hdmi_connector_detect(struct drm_connector *connector, bool force) |
| 89 | { |
Maxime Ripard | 5dfbcae | 2020-09-03 10:01:17 +0200 | [diff] [blame] | 90 | struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 91 | |
Maxime Ripard | b10db9a | 2020-09-03 10:01:16 +0200 | [diff] [blame] | 92 | if (vc4_hdmi->hpd_gpio) { |
| 93 | if (gpio_get_value_cansleep(vc4_hdmi->hpd_gpio) ^ |
| 94 | vc4_hdmi->hpd_active_low) |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 95 | return connector_status_connected; |
Maxime Ripard | b10db9a | 2020-09-03 10:01:16 +0200 | [diff] [blame] | 96 | cec_phys_addr_invalidate(vc4_hdmi->cec_adap); |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 97 | return connector_status_disconnected; |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Maxime Ripard | b10db9a | 2020-09-03 10:01:16 +0200 | [diff] [blame] | 100 | if (drm_probe_ddc(vc4_hdmi->ddc)) |
Eric Anholt | 9d44abb | 2016-09-14 19:21:29 +0100 | [diff] [blame] | 101 | return connector_status_connected; |
| 102 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 103 | if (HDMI_READ(HDMI_HOTPLUG) & VC4_HDMI_HOTPLUG_CONNECTED) |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 104 | return connector_status_connected; |
Maxime Ripard | b10db9a | 2020-09-03 10:01:16 +0200 | [diff] [blame] | 105 | cec_phys_addr_invalidate(vc4_hdmi->cec_adap); |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 106 | return connector_status_disconnected; |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | static void vc4_hdmi_connector_destroy(struct drm_connector *connector) |
| 110 | { |
| 111 | drm_connector_unregister(connector); |
| 112 | drm_connector_cleanup(connector); |
| 113 | } |
| 114 | |
| 115 | static int vc4_hdmi_connector_get_modes(struct drm_connector *connector) |
| 116 | { |
Maxime Ripard | 5dfbcae | 2020-09-03 10:01:17 +0200 | [diff] [blame] | 117 | struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector); |
| 118 | struct vc4_hdmi_encoder *vc4_encoder = &vc4_hdmi->encoder; |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 119 | int ret = 0; |
| 120 | struct edid *edid; |
| 121 | |
Maxime Ripard | 5dfbcae | 2020-09-03 10:01:17 +0200 | [diff] [blame] | 122 | edid = drm_get_edid(connector, vc4_hdmi->ddc); |
| 123 | cec_s_phys_addr_from_edid(vc4_hdmi->cec_adap, edid); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 124 | if (!edid) |
| 125 | return -ENODEV; |
| 126 | |
| 127 | vc4_encoder->hdmi_monitor = drm_detect_hdmi_monitor(edid); |
Eric Anholt | 21317b3 | 2016-09-29 15:34:43 -0700 | [diff] [blame] | 128 | |
Daniel Vetter | c555f02 | 2018-07-09 10:40:06 +0200 | [diff] [blame] | 129 | drm_connector_update_edid_property(connector, edid); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 130 | ret = drm_add_edid_modes(connector, edid); |
Eric Anholt | 5afe0e6 | 2017-08-08 13:56:05 -0700 | [diff] [blame] | 131 | kfree(edid); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 132 | |
| 133 | return ret; |
| 134 | } |
| 135 | |
Maxime Ripard | 90b2df5 | 2019-06-19 12:17:53 +0200 | [diff] [blame] | 136 | static void vc4_hdmi_connector_reset(struct drm_connector *connector) |
| 137 | { |
| 138 | drm_atomic_helper_connector_reset(connector); |
| 139 | drm_atomic_helper_connector_tv_reset(connector); |
| 140 | } |
| 141 | |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 142 | static const struct drm_connector_funcs vc4_hdmi_connector_funcs = { |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 143 | .detect = vc4_hdmi_connector_detect, |
Eric Anholt | 682e62c | 2016-09-28 17:30:25 -0700 | [diff] [blame] | 144 | .fill_modes = drm_helper_probe_single_connector_modes, |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 145 | .destroy = vc4_hdmi_connector_destroy, |
Maxime Ripard | 90b2df5 | 2019-06-19 12:17:53 +0200 | [diff] [blame] | 146 | .reset = vc4_hdmi_connector_reset, |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 147 | .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, |
| 148 | .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, |
| 149 | }; |
| 150 | |
| 151 | static const struct drm_connector_helper_funcs vc4_hdmi_connector_helper_funcs = { |
| 152 | .get_modes = vc4_hdmi_connector_get_modes, |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 153 | }; |
| 154 | |
Maxime Ripard | c98c85b | 2020-09-03 10:01:12 +0200 | [diff] [blame] | 155 | static int vc4_hdmi_connector_init(struct drm_device *dev, |
Maxime Ripard | b052e70 | 2020-09-03 10:01:13 +0200 | [diff] [blame] | 156 | struct vc4_hdmi *vc4_hdmi) |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 157 | { |
Maxime Ripard | 0532e5e | 2020-09-03 10:01:21 +0200 | [diff] [blame] | 158 | struct drm_connector *connector = &vc4_hdmi->connector; |
Maxime Ripard | c98c85b | 2020-09-03 10:01:12 +0200 | [diff] [blame] | 159 | struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base; |
Boris Brezillon | db99953 | 2018-12-06 15:24:39 +0100 | [diff] [blame] | 160 | int ret; |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 161 | |
Andrzej Pietrasiewicz | 04a880f | 2020-01-02 14:22:58 +0100 | [diff] [blame] | 162 | drm_connector_init_with_ddc(dev, connector, |
| 163 | &vc4_hdmi_connector_funcs, |
| 164 | DRM_MODE_CONNECTOR_HDMIA, |
Maxime Ripard | b052e70 | 2020-09-03 10:01:13 +0200 | [diff] [blame] | 165 | vc4_hdmi->ddc); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 166 | drm_connector_helper_add(connector, &vc4_hdmi_connector_helper_funcs); |
| 167 | |
Boris Brezillon | db99953 | 2018-12-06 15:24:39 +0100 | [diff] [blame] | 168 | /* Create and attach TV margin props to this connector. */ |
| 169 | ret = drm_mode_create_tv_margin_properties(dev); |
| 170 | if (ret) |
Maxime Ripard | c98c85b | 2020-09-03 10:01:12 +0200 | [diff] [blame] | 171 | return ret; |
Boris Brezillon | db99953 | 2018-12-06 15:24:39 +0100 | [diff] [blame] | 172 | |
| 173 | drm_connector_attach_tv_margin_properties(connector); |
| 174 | |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 175 | connector->polled = (DRM_CONNECTOR_POLL_CONNECT | |
| 176 | DRM_CONNECTOR_POLL_DISCONNECT); |
| 177 | |
Mario Kleiner | acc1be1 | 2016-07-19 20:58:58 +0200 | [diff] [blame] | 178 | connector->interlace_allowed = 1; |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 179 | connector->doublescan_allowed = 0; |
| 180 | |
Daniel Vetter | cde4c44 | 2018-07-09 10:40:07 +0200 | [diff] [blame] | 181 | drm_connector_attach_encoder(connector, encoder); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 182 | |
Maxime Ripard | c98c85b | 2020-09-03 10:01:12 +0200 | [diff] [blame] | 183 | return 0; |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 184 | } |
| 185 | |
Eric Anholt | 21317b3 | 2016-09-29 15:34:43 -0700 | [diff] [blame] | 186 | static int vc4_hdmi_stop_packet(struct drm_encoder *encoder, |
| 187 | enum hdmi_infoframe_type type) |
| 188 | { |
Maxime Ripard | 5dfbcae | 2020-09-03 10:01:17 +0200 | [diff] [blame] | 189 | struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder); |
Eric Anholt | 21317b3 | 2016-09-29 15:34:43 -0700 | [diff] [blame] | 190 | u32 packet_id = type - 0x80; |
| 191 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 192 | HDMI_WRITE(HDMI_RAM_PACKET_CONFIG, |
| 193 | HDMI_READ(HDMI_RAM_PACKET_CONFIG) & ~BIT(packet_id)); |
Eric Anholt | 21317b3 | 2016-09-29 15:34:43 -0700 | [diff] [blame] | 194 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 195 | return wait_for(!(HDMI_READ(HDMI_RAM_PACKET_STATUS) & |
Eric Anholt | 21317b3 | 2016-09-29 15:34:43 -0700 | [diff] [blame] | 196 | BIT(packet_id)), 100); |
| 197 | } |
| 198 | |
| 199 | static void vc4_hdmi_write_infoframe(struct drm_encoder *encoder, |
| 200 | union hdmi_infoframe *frame) |
| 201 | { |
Maxime Ripard | 5dfbcae | 2020-09-03 10:01:17 +0200 | [diff] [blame] | 202 | struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder); |
Eric Anholt | 21317b3 | 2016-09-29 15:34:43 -0700 | [diff] [blame] | 203 | u32 packet_id = frame->any.type - 0x80; |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 204 | const struct vc4_hdmi_register *ram_packet_start = |
| 205 | &vc4_hdmi->variant->registers[HDMI_RAM_PACKET_START]; |
| 206 | u32 packet_reg = ram_packet_start->offset + VC4_HDMI_PACKET_STRIDE * packet_id; |
| 207 | void __iomem *base = __vc4_hdmi_get_field_base(vc4_hdmi, |
| 208 | ram_packet_start->reg); |
Eric Anholt | 21317b3 | 2016-09-29 15:34:43 -0700 | [diff] [blame] | 209 | uint8_t buffer[VC4_HDMI_PACKET_STRIDE]; |
| 210 | ssize_t len, i; |
| 211 | int ret; |
| 212 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 213 | WARN_ONCE(!(HDMI_READ(HDMI_RAM_PACKET_CONFIG) & |
Eric Anholt | 21317b3 | 2016-09-29 15:34:43 -0700 | [diff] [blame] | 214 | VC4_HDMI_RAM_PACKET_ENABLE), |
| 215 | "Packet RAM has to be on to store the packet."); |
| 216 | |
| 217 | len = hdmi_infoframe_pack(frame, buffer, sizeof(buffer)); |
| 218 | if (len < 0) |
| 219 | return; |
| 220 | |
| 221 | ret = vc4_hdmi_stop_packet(encoder, frame->any.type); |
| 222 | if (ret) { |
| 223 | DRM_ERROR("Failed to wait for infoframe to go idle: %d\n", ret); |
| 224 | return; |
| 225 | } |
| 226 | |
| 227 | for (i = 0; i < len; i += 7) { |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 228 | writel(buffer[i + 0] << 0 | |
| 229 | buffer[i + 1] << 8 | |
| 230 | buffer[i + 2] << 16, |
| 231 | base + packet_reg); |
Eric Anholt | 21317b3 | 2016-09-29 15:34:43 -0700 | [diff] [blame] | 232 | packet_reg += 4; |
| 233 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 234 | writel(buffer[i + 3] << 0 | |
| 235 | buffer[i + 4] << 8 | |
| 236 | buffer[i + 5] << 16 | |
| 237 | buffer[i + 6] << 24, |
| 238 | base + packet_reg); |
Eric Anholt | 21317b3 | 2016-09-29 15:34:43 -0700 | [diff] [blame] | 239 | packet_reg += 4; |
| 240 | } |
| 241 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 242 | HDMI_WRITE(HDMI_RAM_PACKET_CONFIG, |
| 243 | HDMI_READ(HDMI_RAM_PACKET_CONFIG) | BIT(packet_id)); |
| 244 | ret = wait_for((HDMI_READ(HDMI_RAM_PACKET_STATUS) & |
Eric Anholt | 21317b3 | 2016-09-29 15:34:43 -0700 | [diff] [blame] | 245 | BIT(packet_id)), 100); |
| 246 | if (ret) |
| 247 | DRM_ERROR("Failed to wait for infoframe to start: %d\n", ret); |
| 248 | } |
| 249 | |
| 250 | static void vc4_hdmi_set_avi_infoframe(struct drm_encoder *encoder) |
| 251 | { |
Maxime Ripard | 5dfbcae | 2020-09-03 10:01:17 +0200 | [diff] [blame] | 252 | struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder); |
Eric Anholt | 21317b3 | 2016-09-29 15:34:43 -0700 | [diff] [blame] | 253 | struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder); |
Maxime Ripard | 0532e5e | 2020-09-03 10:01:21 +0200 | [diff] [blame] | 254 | struct drm_connector *connector = &vc4_hdmi->connector; |
Maxime Ripard | c98c85b | 2020-09-03 10:01:12 +0200 | [diff] [blame] | 255 | struct drm_connector_state *cstate = connector->state; |
Eric Anholt | 21317b3 | 2016-09-29 15:34:43 -0700 | [diff] [blame] | 256 | struct drm_crtc *crtc = encoder->crtc; |
| 257 | const struct drm_display_mode *mode = &crtc->state->adjusted_mode; |
| 258 | union hdmi_infoframe frame; |
| 259 | int ret; |
| 260 | |
Ville Syrjälä | 13d0add | 2019-01-08 19:28:25 +0200 | [diff] [blame] | 261 | ret = drm_hdmi_avi_infoframe_from_display_mode(&frame.avi, |
Maxime Ripard | c98c85b | 2020-09-03 10:01:12 +0200 | [diff] [blame] | 262 | connector, mode); |
Eric Anholt | 21317b3 | 2016-09-29 15:34:43 -0700 | [diff] [blame] | 263 | if (ret < 0) { |
| 264 | DRM_ERROR("couldn't fill AVI infoframe\n"); |
| 265 | return; |
| 266 | } |
| 267 | |
Ville Syrjälä | 13d0add | 2019-01-08 19:28:25 +0200 | [diff] [blame] | 268 | drm_hdmi_avi_infoframe_quant_range(&frame.avi, |
Maxime Ripard | c98c85b | 2020-09-03 10:01:12 +0200 | [diff] [blame] | 269 | connector, mode, |
Ville Syrjälä | a2ce26f | 2017-01-11 14:57:23 +0200 | [diff] [blame] | 270 | vc4_encoder->limited_rgb_range ? |
| 271 | HDMI_QUANTIZATION_RANGE_LIMITED : |
Ville Syrjälä | 1581b2d | 2019-01-08 19:28:28 +0200 | [diff] [blame] | 272 | HDMI_QUANTIZATION_RANGE_FULL); |
Eric Anholt | 21317b3 | 2016-09-29 15:34:43 -0700 | [diff] [blame] | 273 | |
Ville Syrjälä | cb87637 | 2019-10-08 19:48:14 +0300 | [diff] [blame] | 274 | drm_hdmi_avi_infoframe_bars(&frame.avi, cstate); |
Boris Brezillon | db99953 | 2018-12-06 15:24:39 +0100 | [diff] [blame] | 275 | |
Eric Anholt | 21317b3 | 2016-09-29 15:34:43 -0700 | [diff] [blame] | 276 | vc4_hdmi_write_infoframe(encoder, &frame); |
| 277 | } |
| 278 | |
| 279 | static void vc4_hdmi_set_spd_infoframe(struct drm_encoder *encoder) |
| 280 | { |
| 281 | union hdmi_infoframe frame; |
| 282 | int ret; |
| 283 | |
| 284 | ret = hdmi_spd_infoframe_init(&frame.spd, "Broadcom", "Videocore"); |
| 285 | if (ret < 0) { |
| 286 | DRM_ERROR("couldn't fill SPD infoframe\n"); |
| 287 | return; |
| 288 | } |
| 289 | |
| 290 | frame.spd.sdi = HDMI_SPD_SDI_PC; |
| 291 | |
| 292 | vc4_hdmi_write_infoframe(encoder, &frame); |
| 293 | } |
| 294 | |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 295 | static void vc4_hdmi_set_audio_infoframe(struct drm_encoder *encoder) |
| 296 | { |
Maxime Ripard | 5dfbcae | 2020-09-03 10:01:17 +0200 | [diff] [blame] | 297 | struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 298 | union hdmi_infoframe frame; |
| 299 | int ret; |
| 300 | |
| 301 | ret = hdmi_audio_infoframe_init(&frame.audio); |
| 302 | |
| 303 | frame.audio.coding_type = HDMI_AUDIO_CODING_TYPE_STREAM; |
| 304 | frame.audio.sample_frequency = HDMI_AUDIO_SAMPLE_FREQUENCY_STREAM; |
| 305 | frame.audio.sample_size = HDMI_AUDIO_SAMPLE_SIZE_STREAM; |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 306 | frame.audio.channels = vc4_hdmi->audio.channels; |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 307 | |
| 308 | vc4_hdmi_write_infoframe(encoder, &frame); |
| 309 | } |
| 310 | |
Eric Anholt | 21317b3 | 2016-09-29 15:34:43 -0700 | [diff] [blame] | 311 | static void vc4_hdmi_set_infoframes(struct drm_encoder *encoder) |
| 312 | { |
| 313 | vc4_hdmi_set_avi_infoframe(encoder); |
| 314 | vc4_hdmi_set_spd_infoframe(encoder); |
| 315 | } |
| 316 | |
Boris Brezillon | 4f6e3d6 | 2017-04-11 18:39:25 +0200 | [diff] [blame] | 317 | static void vc4_hdmi_encoder_disable(struct drm_encoder *encoder) |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 318 | { |
Maxime Ripard | 5dfbcae | 2020-09-03 10:01:17 +0200 | [diff] [blame] | 319 | struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder); |
Boris Brezillon | 4f6e3d6 | 2017-04-11 18:39:25 +0200 | [diff] [blame] | 320 | int ret; |
| 321 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 322 | HDMI_WRITE(HDMI_RAM_PACKET_CONFIG, 0); |
Boris Brezillon | 4f6e3d6 | 2017-04-11 18:39:25 +0200 | [diff] [blame] | 323 | |
Maxime Ripard | c457b8a | 2020-09-03 10:01:25 +0200 | [diff] [blame] | 324 | if (vc4_hdmi->variant->phy_disable) |
| 325 | vc4_hdmi->variant->phy_disable(vc4_hdmi); |
| 326 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 327 | HDMI_WRITE(HDMI_VID_CTL, |
| 328 | HDMI_READ(HDMI_VID_CTL) & ~VC4_HD_VID_CTL_ENABLE); |
Boris Brezillon | 4f6e3d6 | 2017-04-11 18:39:25 +0200 | [diff] [blame] | 329 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 330 | clk_disable_unprepare(vc4_hdmi->pixel_clock); |
Boris Brezillon | 4f6e3d6 | 2017-04-11 18:39:25 +0200 | [diff] [blame] | 331 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 332 | ret = pm_runtime_put(&vc4_hdmi->pdev->dev); |
Boris Brezillon | 4f6e3d6 | 2017-04-11 18:39:25 +0200 | [diff] [blame] | 333 | if (ret < 0) |
| 334 | DRM_ERROR("Failed to release power domain: %d\n", ret); |
| 335 | } |
| 336 | |
Maxime Ripard | 89f31a2 | 2020-09-03 10:01:27 +0200 | [diff] [blame] | 337 | static void vc4_hdmi_csc_setup(struct vc4_hdmi *vc4_hdmi, bool enable) |
| 338 | { |
| 339 | u32 csc_ctl; |
| 340 | |
| 341 | csc_ctl = VC4_SET_FIELD(VC4_HD_CSC_CTL_ORDER_BGR, |
| 342 | VC4_HD_CSC_CTL_ORDER); |
| 343 | |
| 344 | if (enable) { |
| 345 | /* CEA VICs other than #1 requre limited range RGB |
| 346 | * output unless overridden by an AVI infoframe. |
| 347 | * Apply a colorspace conversion to squash 0-255 down |
| 348 | * to 16-235. The matrix here is: |
| 349 | * |
| 350 | * [ 0 0 0.8594 16] |
| 351 | * [ 0 0.8594 0 16] |
| 352 | * [ 0.8594 0 0 16] |
| 353 | * [ 0 0 0 1] |
| 354 | */ |
| 355 | csc_ctl |= VC4_HD_CSC_CTL_ENABLE; |
| 356 | csc_ctl |= VC4_HD_CSC_CTL_RGB2YCC; |
| 357 | csc_ctl |= VC4_SET_FIELD(VC4_HD_CSC_CTL_MODE_CUSTOM, |
| 358 | VC4_HD_CSC_CTL_MODE); |
| 359 | |
| 360 | HDMI_WRITE(HDMI_CSC_12_11, (0x000 << 16) | 0x000); |
| 361 | HDMI_WRITE(HDMI_CSC_14_13, (0x100 << 16) | 0x6e0); |
| 362 | HDMI_WRITE(HDMI_CSC_22_21, (0x6e0 << 16) | 0x000); |
| 363 | HDMI_WRITE(HDMI_CSC_24_23, (0x100 << 16) | 0x000); |
| 364 | HDMI_WRITE(HDMI_CSC_32_31, (0x000 << 16) | 0x6e0); |
| 365 | HDMI_WRITE(HDMI_CSC_34_33, (0x100 << 16) | 0x000); |
| 366 | } |
| 367 | |
| 368 | /* The RGB order applies even when CSC is disabled. */ |
| 369 | HDMI_WRITE(HDMI_CSC_CTL, csc_ctl); |
| 370 | } |
| 371 | |
Maxime Ripard | 904f668 | 2020-09-03 10:01:28 +0200 | [diff] [blame] | 372 | static void vc4_hdmi_set_timings(struct vc4_hdmi *vc4_hdmi, |
| 373 | struct drm_display_mode *mode) |
Boris Brezillon | 4f6e3d6 | 2017-04-11 18:39:25 +0200 | [diff] [blame] | 374 | { |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 375 | bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC; |
| 376 | bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC; |
Eric Anholt | 682e62c | 2016-09-28 17:30:25 -0700 | [diff] [blame] | 377 | bool interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE; |
Eric Anholt | dfccd93 | 2016-09-29 15:34:44 -0700 | [diff] [blame] | 378 | u32 pixel_rep = (mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1; |
Eric Anholt | 682e62c | 2016-09-28 17:30:25 -0700 | [diff] [blame] | 379 | 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] | 380 | VC4_HDMI_VERTA_VSP) | |
Eric Anholt | 682e62c | 2016-09-28 17:30:25 -0700 | [diff] [blame] | 381 | VC4_SET_FIELD(mode->crtc_vsync_start - mode->crtc_vdisplay, |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 382 | VC4_HDMI_VERTA_VFP) | |
Eric Anholt | 682e62c | 2016-09-28 17:30:25 -0700 | [diff] [blame] | 383 | VC4_SET_FIELD(mode->crtc_vdisplay, VC4_HDMI_VERTA_VAL)); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 384 | u32 vertb = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) | |
Eric Anholt | 682e62c | 2016-09-28 17:30:25 -0700 | [diff] [blame] | 385 | VC4_SET_FIELD(mode->crtc_vtotal - mode->crtc_vsync_end, |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 386 | VC4_HDMI_VERTB_VBP)); |
Eric Anholt | 682e62c | 2016-09-28 17:30:25 -0700 | [diff] [blame] | 387 | u32 vertb_even = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) | |
| 388 | VC4_SET_FIELD(mode->crtc_vtotal - |
| 389 | mode->crtc_vsync_end - |
| 390 | interlaced, |
| 391 | VC4_HDMI_VERTB_VBP)); |
Maxime Ripard | 904f668 | 2020-09-03 10:01:28 +0200 | [diff] [blame] | 392 | |
| 393 | HDMI_WRITE(HDMI_HORZA, |
| 394 | (vsync_pos ? VC4_HDMI_HORZA_VPOS : 0) | |
| 395 | (hsync_pos ? VC4_HDMI_HORZA_HPOS : 0) | |
| 396 | VC4_SET_FIELD(mode->hdisplay * pixel_rep, |
| 397 | VC4_HDMI_HORZA_HAP)); |
| 398 | |
| 399 | HDMI_WRITE(HDMI_HORZB, |
| 400 | VC4_SET_FIELD((mode->htotal - |
| 401 | mode->hsync_end) * pixel_rep, |
| 402 | VC4_HDMI_HORZB_HBP) | |
| 403 | VC4_SET_FIELD((mode->hsync_end - |
| 404 | mode->hsync_start) * pixel_rep, |
| 405 | VC4_HDMI_HORZB_HSP) | |
| 406 | VC4_SET_FIELD((mode->hsync_start - |
| 407 | mode->hdisplay) * pixel_rep, |
| 408 | VC4_HDMI_HORZB_HFP)); |
| 409 | |
| 410 | HDMI_WRITE(HDMI_VERTA0, verta); |
| 411 | HDMI_WRITE(HDMI_VERTA1, verta); |
| 412 | |
| 413 | HDMI_WRITE(HDMI_VERTB0, vertb_even); |
| 414 | HDMI_WRITE(HDMI_VERTB1, vertb); |
| 415 | |
| 416 | HDMI_WRITE(HDMI_VID_CTL, |
| 417 | (vsync_pos ? 0 : VC4_HD_VID_CTL_VSYNC_LOW) | |
| 418 | (hsync_pos ? 0 : VC4_HD_VID_CTL_HSYNC_LOW)); |
| 419 | } |
| 420 | |
| 421 | static void vc4_hdmi_encoder_enable(struct drm_encoder *encoder) |
| 422 | { |
| 423 | struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode; |
| 424 | struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder); |
| 425 | struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder); |
| 426 | bool debug_dump_regs = false; |
Boris Brezillon | 4f6e3d6 | 2017-04-11 18:39:25 +0200 | [diff] [blame] | 427 | int ret; |
| 428 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 429 | ret = pm_runtime_get_sync(&vc4_hdmi->pdev->dev); |
Boris Brezillon | 4f6e3d6 | 2017-04-11 18:39:25 +0200 | [diff] [blame] | 430 | if (ret < 0) { |
| 431 | DRM_ERROR("Failed to retain power domain: %d\n", ret); |
| 432 | return; |
| 433 | } |
| 434 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 435 | ret = clk_set_rate(vc4_hdmi->pixel_clock, |
Boris Brezillon | 4f6e3d6 | 2017-04-11 18:39:25 +0200 | [diff] [blame] | 436 | mode->clock * 1000 * |
| 437 | ((mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1)); |
| 438 | if (ret) { |
| 439 | DRM_ERROR("Failed to set pixel clock rate: %d\n", ret); |
| 440 | return; |
| 441 | } |
| 442 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 443 | ret = clk_prepare_enable(vc4_hdmi->pixel_clock); |
Boris Brezillon | 4f6e3d6 | 2017-04-11 18:39:25 +0200 | [diff] [blame] | 444 | if (ret) { |
| 445 | DRM_ERROR("Failed to turn on pixel clock: %d\n", ret); |
| 446 | return; |
| 447 | } |
| 448 | |
Maxime Ripard | 9045e91 | 2020-09-03 10:01:24 +0200 | [diff] [blame] | 449 | if (vc4_hdmi->variant->reset) |
| 450 | vc4_hdmi->variant->reset(vc4_hdmi); |
Boris Brezillon | 4f6e3d6 | 2017-04-11 18:39:25 +0200 | [diff] [blame] | 451 | |
Maxime Ripard | c457b8a | 2020-09-03 10:01:25 +0200 | [diff] [blame] | 452 | if (vc4_hdmi->variant->phy_init) |
| 453 | vc4_hdmi->variant->phy_init(vc4_hdmi, mode); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 454 | |
| 455 | if (debug_dump_regs) { |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 456 | struct drm_printer p = drm_info_printer(&vc4_hdmi->pdev->dev); |
Eric Anholt | 3051719 | 2019-02-20 13:03:38 -0800 | [diff] [blame] | 457 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 458 | dev_info(&vc4_hdmi->pdev->dev, "HDMI regs before:\n"); |
| 459 | drm_print_regset32(&p, &vc4_hdmi->hdmi_regset); |
| 460 | drm_print_regset32(&p, &vc4_hdmi->hd_regset); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 461 | } |
| 462 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 463 | HDMI_WRITE(HDMI_VID_CTL, 0); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 464 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 465 | HDMI_WRITE(HDMI_SCHEDULER_CONTROL, |
| 466 | HDMI_READ(HDMI_SCHEDULER_CONTROL) | |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 467 | VC4_HDMI_SCHEDULER_CONTROL_MANUAL_FORMAT | |
| 468 | VC4_HDMI_SCHEDULER_CONTROL_IGNORE_VSYNC_PREDICTS); |
| 469 | |
Maxime Ripard | 904f668 | 2020-09-03 10:01:28 +0200 | [diff] [blame] | 470 | if (vc4_hdmi->variant->set_timings) |
| 471 | vc4_hdmi->variant->set_timings(vc4_hdmi, mode); |
Eric Anholt | 6e1cbba | 2016-09-16 10:59:45 +0100 | [diff] [blame] | 472 | |
Ville Syrjälä | c8127cf0 | 2017-01-11 16:18:35 +0200 | [diff] [blame] | 473 | if (vc4_encoder->hdmi_monitor && |
Maxime Ripard | 89f31a2 | 2020-09-03 10:01:27 +0200 | [diff] [blame] | 474 | drm_default_rgb_quant_range(mode) == HDMI_QUANTIZATION_RANGE_LIMITED) { |
| 475 | if (vc4_hdmi->variant->csc_setup) |
| 476 | vc4_hdmi->variant->csc_setup(vc4_hdmi, true); |
Eric Anholt | 6e1cbba | 2016-09-16 10:59:45 +0100 | [diff] [blame] | 477 | |
Eric Anholt | 21317b3 | 2016-09-29 15:34:43 -0700 | [diff] [blame] | 478 | vc4_encoder->limited_rgb_range = true; |
| 479 | } else { |
Maxime Ripard | 89f31a2 | 2020-09-03 10:01:27 +0200 | [diff] [blame] | 480 | if (vc4_hdmi->variant->csc_setup) |
| 481 | vc4_hdmi->variant->csc_setup(vc4_hdmi, false); |
| 482 | |
Eric Anholt | 21317b3 | 2016-09-29 15:34:43 -0700 | [diff] [blame] | 483 | vc4_encoder->limited_rgb_range = false; |
Eric Anholt | 6e1cbba | 2016-09-16 10:59:45 +0100 | [diff] [blame] | 484 | } |
| 485 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 486 | HDMI_WRITE(HDMI_FIFO_CTL, VC4_HDMI_FIFO_CTL_MASTER_SLAVE_N); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 487 | |
| 488 | if (debug_dump_regs) { |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 489 | struct drm_printer p = drm_info_printer(&vc4_hdmi->pdev->dev); |
Eric Anholt | 3051719 | 2019-02-20 13:03:38 -0800 | [diff] [blame] | 490 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 491 | dev_info(&vc4_hdmi->pdev->dev, "HDMI regs after:\n"); |
| 492 | drm_print_regset32(&p, &vc4_hdmi->hdmi_regset); |
| 493 | drm_print_regset32(&p, &vc4_hdmi->hd_regset); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 494 | } |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 495 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 496 | HDMI_WRITE(HDMI_VID_CTL, |
| 497 | HDMI_READ(HDMI_VID_CTL) | |
| 498 | VC4_HD_VID_CTL_ENABLE | |
| 499 | VC4_HD_VID_CTL_UNDERFLOW_ENABLE | |
| 500 | VC4_HD_VID_CTL_FRAME_COUNTER_RESET); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 501 | |
| 502 | if (vc4_encoder->hdmi_monitor) { |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 503 | HDMI_WRITE(HDMI_SCHEDULER_CONTROL, |
| 504 | HDMI_READ(HDMI_SCHEDULER_CONTROL) | |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 505 | VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI); |
| 506 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 507 | ret = wait_for(HDMI_READ(HDMI_SCHEDULER_CONTROL) & |
Eric Anholt | 2b29bf1 | 2016-09-28 17:21:05 -0700 | [diff] [blame] | 508 | VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE, 1000); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 509 | WARN_ONCE(ret, "Timeout waiting for " |
| 510 | "VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n"); |
| 511 | } else { |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 512 | HDMI_WRITE(HDMI_RAM_PACKET_CONFIG, |
| 513 | HDMI_READ(HDMI_RAM_PACKET_CONFIG) & |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 514 | ~(VC4_HDMI_RAM_PACKET_ENABLE)); |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 515 | HDMI_WRITE(HDMI_SCHEDULER_CONTROL, |
| 516 | HDMI_READ(HDMI_SCHEDULER_CONTROL) & |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 517 | ~VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI); |
| 518 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 519 | ret = wait_for(!(HDMI_READ(HDMI_SCHEDULER_CONTROL) & |
Eric Anholt | 2b29bf1 | 2016-09-28 17:21:05 -0700 | [diff] [blame] | 520 | VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE), 1000); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 521 | WARN_ONCE(ret, "Timeout waiting for " |
| 522 | "!VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n"); |
| 523 | } |
| 524 | |
| 525 | if (vc4_encoder->hdmi_monitor) { |
| 526 | u32 drift; |
| 527 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 528 | WARN_ON(!(HDMI_READ(HDMI_SCHEDULER_CONTROL) & |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 529 | VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE)); |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 530 | HDMI_WRITE(HDMI_SCHEDULER_CONTROL, |
| 531 | HDMI_READ(HDMI_SCHEDULER_CONTROL) | |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 532 | VC4_HDMI_SCHEDULER_CONTROL_VERT_ALWAYS_KEEPOUT); |
| 533 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 534 | HDMI_WRITE(HDMI_RAM_PACKET_CONFIG, |
Eric Anholt | 21317b3 | 2016-09-29 15:34:43 -0700 | [diff] [blame] | 535 | VC4_HDMI_RAM_PACKET_ENABLE); |
| 536 | |
| 537 | vc4_hdmi_set_infoframes(encoder); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 538 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 539 | drift = HDMI_READ(HDMI_FIFO_CTL); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 540 | drift &= VC4_HDMI_FIFO_VALID_WRITE_MASK; |
| 541 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 542 | HDMI_WRITE(HDMI_FIFO_CTL, |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 543 | drift & ~VC4_HDMI_FIFO_CTL_RECENTER); |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 544 | HDMI_WRITE(HDMI_FIFO_CTL, |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 545 | drift | VC4_HDMI_FIFO_CTL_RECENTER); |
Stefan Wahren | d8eb9de | 2018-02-24 13:38:14 +0100 | [diff] [blame] | 546 | usleep_range(1000, 1100); |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 547 | HDMI_WRITE(HDMI_FIFO_CTL, |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 548 | drift & ~VC4_HDMI_FIFO_CTL_RECENTER); |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 549 | HDMI_WRITE(HDMI_FIFO_CTL, |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 550 | drift | VC4_HDMI_FIFO_CTL_RECENTER); |
| 551 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 552 | ret = wait_for(HDMI_READ(HDMI_FIFO_CTL) & |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 553 | VC4_HDMI_FIFO_CTL_RECENTER_DONE, 1); |
| 554 | WARN_ONCE(ret, "Timeout waiting for " |
| 555 | "VC4_HDMI_FIFO_CTL_RECENTER_DONE"); |
| 556 | } |
| 557 | } |
| 558 | |
Eric Anholt | 32e823c | 2017-09-20 15:59:34 -0700 | [diff] [blame] | 559 | static enum drm_mode_status |
| 560 | vc4_hdmi_encoder_mode_valid(struct drm_encoder *crtc, |
| 561 | const struct drm_display_mode *mode) |
| 562 | { |
Nicolas Saenz Julienne | b1e7396 | 2020-03-26 13:20:01 +0100 | [diff] [blame] | 563 | /* |
| 564 | * As stated in RPi's vc4 firmware "HDMI state machine (HSM) clock must |
| 565 | * be faster than pixel clock, infinitesimally faster, tested in |
| 566 | * simulation. Otherwise, exact value is unimportant for HDMI |
| 567 | * operation." This conflicts with bcm2835's vc4 documentation, which |
| 568 | * states HSM's clock has to be at least 108% of the pixel clock. |
| 569 | * |
| 570 | * Real life tests reveal that vc4's firmware statement holds up, and |
| 571 | * users are able to use pixel clocks closer to HSM's, namely for |
| 572 | * 1920x1200@60Hz. So it was decided to have leave a 1% margin between |
| 573 | * both clocks. Which, for RPi0-3 implies a maximum pixel clock of |
| 574 | * 162MHz. |
| 575 | * |
| 576 | * Additionally, the AXI clock needs to be at least 25% of |
| 577 | * pixel clock, but HSM ends up being the limiting factor. |
Eric Anholt | 32e823c | 2017-09-20 15:59:34 -0700 | [diff] [blame] | 578 | */ |
Nicolas Saenz Julienne | b1e7396 | 2020-03-26 13:20:01 +0100 | [diff] [blame] | 579 | if (mode->clock > HSM_CLOCK_FREQ / (1000 * 101 / 100)) |
Eric Anholt | 32e823c | 2017-09-20 15:59:34 -0700 | [diff] [blame] | 580 | return MODE_CLOCK_HIGH; |
| 581 | |
| 582 | return MODE_OK; |
| 583 | } |
| 584 | |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 585 | static const struct drm_encoder_helper_funcs vc4_hdmi_encoder_helper_funcs = { |
Eric Anholt | 32e823c | 2017-09-20 15:59:34 -0700 | [diff] [blame] | 586 | .mode_valid = vc4_hdmi_encoder_mode_valid, |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 587 | .disable = vc4_hdmi_encoder_disable, |
| 588 | .enable = vc4_hdmi_encoder_enable, |
| 589 | }; |
| 590 | |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 591 | /* HDMI audio codec callbacks */ |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 592 | static void vc4_hdmi_audio_set_mai_clock(struct vc4_hdmi *vc4_hdmi) |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 593 | { |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 594 | u32 hsm_clock = clk_get_rate(vc4_hdmi->hsm_clock); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 595 | unsigned long n, m; |
| 596 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 597 | rational_best_approximation(hsm_clock, vc4_hdmi->audio.samplerate, |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 598 | VC4_HD_MAI_SMP_N_MASK >> |
| 599 | VC4_HD_MAI_SMP_N_SHIFT, |
| 600 | (VC4_HD_MAI_SMP_M_MASK >> |
| 601 | VC4_HD_MAI_SMP_M_SHIFT) + 1, |
| 602 | &n, &m); |
| 603 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 604 | HDMI_WRITE(HDMI_MAI_SMP, |
| 605 | VC4_SET_FIELD(n, VC4_HD_MAI_SMP_N) | |
| 606 | VC4_SET_FIELD(m - 1, VC4_HD_MAI_SMP_M)); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 607 | } |
| 608 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 609 | static void vc4_hdmi_set_n_cts(struct vc4_hdmi *vc4_hdmi) |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 610 | { |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 611 | struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base; |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 612 | struct drm_crtc *crtc = encoder->crtc; |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 613 | const struct drm_display_mode *mode = &crtc->state->adjusted_mode; |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 614 | u32 samplerate = vc4_hdmi->audio.samplerate; |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 615 | u32 n, cts; |
| 616 | u64 tmp; |
| 617 | |
| 618 | n = 128 * samplerate / 1000; |
| 619 | tmp = (u64)(mode->clock * 1000) * n; |
| 620 | do_div(tmp, 128 * samplerate); |
| 621 | cts = tmp; |
| 622 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 623 | HDMI_WRITE(HDMI_CRP_CFG, |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 624 | VC4_HDMI_CRP_CFG_EXTERNAL_CTS_EN | |
| 625 | VC4_SET_FIELD(n, VC4_HDMI_CRP_CFG_N)); |
| 626 | |
| 627 | /* |
| 628 | * We could get slightly more accurate clocks in some cases by |
| 629 | * providing a CTS_1 value. The two CTS values are alternated |
| 630 | * between based on the period fields |
| 631 | */ |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 632 | HDMI_WRITE(HDMI_CTS_0, cts); |
| 633 | HDMI_WRITE(HDMI_CTS_1, cts); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | static inline struct vc4_hdmi *dai_to_hdmi(struct snd_soc_dai *dai) |
| 637 | { |
| 638 | struct snd_soc_card *card = snd_soc_dai_get_drvdata(dai); |
| 639 | |
| 640 | return snd_soc_card_get_drvdata(card); |
| 641 | } |
| 642 | |
| 643 | static int vc4_hdmi_audio_startup(struct snd_pcm_substream *substream, |
| 644 | struct snd_soc_dai *dai) |
| 645 | { |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 646 | struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai); |
| 647 | struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base; |
Maxime Ripard | 0532e5e | 2020-09-03 10:01:21 +0200 | [diff] [blame] | 648 | struct drm_connector *connector = &vc4_hdmi->connector; |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 649 | int ret; |
| 650 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 651 | if (vc4_hdmi->audio.substream && vc4_hdmi->audio.substream != substream) |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 652 | return -EINVAL; |
| 653 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 654 | vc4_hdmi->audio.substream = substream; |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 655 | |
| 656 | /* |
| 657 | * If the HDMI encoder hasn't probed, or the encoder is |
| 658 | * currently in DVI mode, treat the codec dai as missing. |
| 659 | */ |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 660 | if (!encoder->crtc || !(HDMI_READ(HDMI_RAM_PACKET_CONFIG) & |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 661 | VC4_HDMI_RAM_PACKET_ENABLE)) |
| 662 | return -ENODEV; |
| 663 | |
Maxime Ripard | c98c85b | 2020-09-03 10:01:12 +0200 | [diff] [blame] | 664 | ret = snd_pcm_hw_constraint_eld(substream->runtime, connector->eld); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 665 | if (ret) |
| 666 | return ret; |
| 667 | |
| 668 | return 0; |
| 669 | } |
| 670 | |
| 671 | static int vc4_hdmi_audio_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) |
| 672 | { |
| 673 | return 0; |
| 674 | } |
| 675 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 676 | static void vc4_hdmi_audio_reset(struct vc4_hdmi *vc4_hdmi) |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 677 | { |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 678 | struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base; |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 679 | struct device *dev = &vc4_hdmi->pdev->dev; |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 680 | int ret; |
| 681 | |
| 682 | ret = vc4_hdmi_stop_packet(encoder, HDMI_INFOFRAME_TYPE_AUDIO); |
| 683 | if (ret) |
| 684 | dev_err(dev, "Failed to stop audio infoframe: %d\n", ret); |
| 685 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 686 | HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_RESET); |
| 687 | HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_ERRORF); |
| 688 | HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_FLUSH); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | static void vc4_hdmi_audio_shutdown(struct snd_pcm_substream *substream, |
| 692 | struct snd_soc_dai *dai) |
| 693 | { |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 694 | struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 695 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 696 | if (substream != vc4_hdmi->audio.substream) |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 697 | return; |
| 698 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 699 | vc4_hdmi_audio_reset(vc4_hdmi); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 700 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 701 | vc4_hdmi->audio.substream = NULL; |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | /* HDMI audio codec callbacks */ |
| 705 | static int vc4_hdmi_audio_hw_params(struct snd_pcm_substream *substream, |
| 706 | struct snd_pcm_hw_params *params, |
| 707 | struct snd_soc_dai *dai) |
| 708 | { |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 709 | struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai); |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 710 | struct device *dev = &vc4_hdmi->pdev->dev; |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 711 | u32 audio_packet_config, channel_mask; |
| 712 | u32 channel_map, i; |
| 713 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 714 | if (substream != vc4_hdmi->audio.substream) |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 715 | return -EINVAL; |
| 716 | |
| 717 | dev_dbg(dev, "%s: %u Hz, %d bit, %d channels\n", __func__, |
| 718 | params_rate(params), params_width(params), |
| 719 | params_channels(params)); |
| 720 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 721 | vc4_hdmi->audio.channels = params_channels(params); |
| 722 | vc4_hdmi->audio.samplerate = params_rate(params); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 723 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 724 | HDMI_WRITE(HDMI_MAI_CTL, |
| 725 | VC4_HD_MAI_CTL_RESET | |
| 726 | VC4_HD_MAI_CTL_FLUSH | |
| 727 | VC4_HD_MAI_CTL_DLATE | |
| 728 | VC4_HD_MAI_CTL_ERRORE | |
| 729 | VC4_HD_MAI_CTL_ERRORF); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 730 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 731 | vc4_hdmi_audio_set_mai_clock(vc4_hdmi); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 732 | |
| 733 | audio_packet_config = |
| 734 | VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_SAMPLE_FLAT | |
| 735 | VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_INACTIVE_CHANNELS | |
| 736 | VC4_SET_FIELD(0xf, VC4_HDMI_AUDIO_PACKET_B_FRAME_IDENTIFIER); |
| 737 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 738 | channel_mask = GENMASK(vc4_hdmi->audio.channels - 1, 0); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 739 | audio_packet_config |= VC4_SET_FIELD(channel_mask, |
| 740 | VC4_HDMI_AUDIO_PACKET_CEA_MASK); |
| 741 | |
| 742 | /* Set the MAI threshold. This logic mimics the firmware's. */ |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 743 | if (vc4_hdmi->audio.samplerate > 96000) { |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 744 | HDMI_WRITE(HDMI_MAI_THR, |
| 745 | VC4_SET_FIELD(0x12, VC4_HD_MAI_THR_DREQHIGH) | |
| 746 | VC4_SET_FIELD(0x12, VC4_HD_MAI_THR_DREQLOW)); |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 747 | } else if (vc4_hdmi->audio.samplerate > 48000) { |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 748 | HDMI_WRITE(HDMI_MAI_THR, |
| 749 | VC4_SET_FIELD(0x14, VC4_HD_MAI_THR_DREQHIGH) | |
| 750 | VC4_SET_FIELD(0x12, VC4_HD_MAI_THR_DREQLOW)); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 751 | } else { |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 752 | HDMI_WRITE(HDMI_MAI_THR, |
| 753 | VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_PANICHIGH) | |
| 754 | VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_PANICLOW) | |
| 755 | VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_DREQHIGH) | |
| 756 | VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_DREQLOW)); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 757 | } |
| 758 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 759 | HDMI_WRITE(HDMI_MAI_CONFIG, |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 760 | VC4_HDMI_MAI_CONFIG_BIT_REVERSE | |
| 761 | VC4_SET_FIELD(channel_mask, VC4_HDMI_MAI_CHANNEL_MASK)); |
| 762 | |
| 763 | channel_map = 0; |
| 764 | for (i = 0; i < 8; i++) { |
| 765 | if (channel_mask & BIT(i)) |
| 766 | channel_map |= i << (3 * i); |
| 767 | } |
| 768 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 769 | HDMI_WRITE(HDMI_MAI_CHANNEL_MAP, channel_map); |
| 770 | HDMI_WRITE(HDMI_AUDIO_PACKET_CONFIG, audio_packet_config); |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 771 | vc4_hdmi_set_n_cts(vc4_hdmi); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 772 | |
| 773 | return 0; |
| 774 | } |
| 775 | |
| 776 | static int vc4_hdmi_audio_trigger(struct snd_pcm_substream *substream, int cmd, |
| 777 | struct snd_soc_dai *dai) |
| 778 | { |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 779 | struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai); |
| 780 | struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base; |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 781 | |
| 782 | switch (cmd) { |
| 783 | case SNDRV_PCM_TRIGGER_START: |
| 784 | vc4_hdmi_set_audio_infoframe(encoder); |
Maxime Ripard | 647b965 | 2020-09-03 10:01:26 +0200 | [diff] [blame] | 785 | |
| 786 | if (vc4_hdmi->variant->phy_rng_enable) |
| 787 | vc4_hdmi->variant->phy_rng_enable(vc4_hdmi); |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 788 | |
| 789 | HDMI_WRITE(HDMI_MAI_CTL, |
| 790 | VC4_SET_FIELD(vc4_hdmi->audio.channels, |
| 791 | VC4_HD_MAI_CTL_CHNUM) | |
| 792 | VC4_HD_MAI_CTL_ENABLE); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 793 | break; |
| 794 | case SNDRV_PCM_TRIGGER_STOP: |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 795 | HDMI_WRITE(HDMI_MAI_CTL, |
| 796 | VC4_HD_MAI_CTL_DLATE | |
| 797 | VC4_HD_MAI_CTL_ERRORE | |
| 798 | VC4_HD_MAI_CTL_ERRORF); |
Maxime Ripard | 647b965 | 2020-09-03 10:01:26 +0200 | [diff] [blame] | 799 | |
| 800 | if (vc4_hdmi->variant->phy_rng_disable) |
| 801 | vc4_hdmi->variant->phy_rng_disable(vc4_hdmi); |
| 802 | |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 803 | break; |
| 804 | default: |
| 805 | break; |
| 806 | } |
| 807 | |
| 808 | return 0; |
| 809 | } |
| 810 | |
| 811 | static inline struct vc4_hdmi * |
| 812 | snd_component_to_hdmi(struct snd_soc_component *component) |
| 813 | { |
| 814 | struct snd_soc_card *card = snd_soc_component_get_drvdata(component); |
| 815 | |
| 816 | return snd_soc_card_get_drvdata(card); |
| 817 | } |
| 818 | |
| 819 | static int vc4_hdmi_audio_eld_ctl_info(struct snd_kcontrol *kcontrol, |
| 820 | struct snd_ctl_elem_info *uinfo) |
| 821 | { |
| 822 | struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 823 | struct vc4_hdmi *vc4_hdmi = snd_component_to_hdmi(component); |
Maxime Ripard | 0532e5e | 2020-09-03 10:01:21 +0200 | [diff] [blame] | 824 | struct drm_connector *connector = &vc4_hdmi->connector; |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 825 | |
| 826 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES; |
Maxime Ripard | c98c85b | 2020-09-03 10:01:12 +0200 | [diff] [blame] | 827 | uinfo->count = sizeof(connector->eld); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 828 | |
| 829 | return 0; |
| 830 | } |
| 831 | |
| 832 | static int vc4_hdmi_audio_eld_ctl_get(struct snd_kcontrol *kcontrol, |
| 833 | struct snd_ctl_elem_value *ucontrol) |
| 834 | { |
| 835 | struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 836 | struct vc4_hdmi *vc4_hdmi = snd_component_to_hdmi(component); |
Maxime Ripard | 0532e5e | 2020-09-03 10:01:21 +0200 | [diff] [blame] | 837 | struct drm_connector *connector = &vc4_hdmi->connector; |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 838 | |
Maxime Ripard | c98c85b | 2020-09-03 10:01:12 +0200 | [diff] [blame] | 839 | memcpy(ucontrol->value.bytes.data, connector->eld, |
| 840 | sizeof(connector->eld)); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 841 | |
| 842 | return 0; |
| 843 | } |
| 844 | |
| 845 | static const struct snd_kcontrol_new vc4_hdmi_audio_controls[] = { |
| 846 | { |
| 847 | .access = SNDRV_CTL_ELEM_ACCESS_READ | |
| 848 | SNDRV_CTL_ELEM_ACCESS_VOLATILE, |
| 849 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 850 | .name = "ELD", |
| 851 | .info = vc4_hdmi_audio_eld_ctl_info, |
| 852 | .get = vc4_hdmi_audio_eld_ctl_get, |
| 853 | }, |
| 854 | }; |
| 855 | |
| 856 | static const struct snd_soc_dapm_widget vc4_hdmi_audio_widgets[] = { |
| 857 | SND_SOC_DAPM_OUTPUT("TX"), |
| 858 | }; |
| 859 | |
| 860 | static const struct snd_soc_dapm_route vc4_hdmi_audio_routes[] = { |
| 861 | { "TX", NULL, "Playback" }, |
| 862 | }; |
| 863 | |
Kuninori Morimoto | 635b1c1 | 2018-01-29 04:35:04 +0000 | [diff] [blame] | 864 | static const struct snd_soc_component_driver vc4_hdmi_audio_component_drv = { |
| 865 | .controls = vc4_hdmi_audio_controls, |
| 866 | .num_controls = ARRAY_SIZE(vc4_hdmi_audio_controls), |
| 867 | .dapm_widgets = vc4_hdmi_audio_widgets, |
| 868 | .num_dapm_widgets = ARRAY_SIZE(vc4_hdmi_audio_widgets), |
| 869 | .dapm_routes = vc4_hdmi_audio_routes, |
| 870 | .num_dapm_routes = ARRAY_SIZE(vc4_hdmi_audio_routes), |
| 871 | .idle_bias_on = 1, |
| 872 | .use_pmdown_time = 1, |
| 873 | .endianness = 1, |
| 874 | .non_legacy_dai_naming = 1, |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 875 | }; |
| 876 | |
| 877 | static const struct snd_soc_dai_ops vc4_hdmi_audio_dai_ops = { |
| 878 | .startup = vc4_hdmi_audio_startup, |
| 879 | .shutdown = vc4_hdmi_audio_shutdown, |
| 880 | .hw_params = vc4_hdmi_audio_hw_params, |
| 881 | .set_fmt = vc4_hdmi_audio_set_fmt, |
| 882 | .trigger = vc4_hdmi_audio_trigger, |
| 883 | }; |
| 884 | |
| 885 | static struct snd_soc_dai_driver vc4_hdmi_audio_codec_dai_drv = { |
| 886 | .name = "vc4-hdmi-hifi", |
| 887 | .playback = { |
| 888 | .stream_name = "Playback", |
| 889 | .channels_min = 2, |
| 890 | .channels_max = 8, |
| 891 | .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | |
| 892 | SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | |
| 893 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 | |
| 894 | SNDRV_PCM_RATE_192000, |
| 895 | .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE, |
| 896 | }, |
| 897 | }; |
| 898 | |
| 899 | static const struct snd_soc_component_driver vc4_hdmi_audio_cpu_dai_comp = { |
| 900 | .name = "vc4-hdmi-cpu-dai-component", |
| 901 | }; |
| 902 | |
| 903 | static int vc4_hdmi_audio_cpu_dai_probe(struct snd_soc_dai *dai) |
| 904 | { |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 905 | struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 906 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 907 | 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] | 908 | |
| 909 | return 0; |
| 910 | } |
| 911 | |
| 912 | static struct snd_soc_dai_driver vc4_hdmi_audio_cpu_dai_drv = { |
| 913 | .name = "vc4-hdmi-cpu-dai", |
| 914 | .probe = vc4_hdmi_audio_cpu_dai_probe, |
| 915 | .playback = { |
| 916 | .stream_name = "Playback", |
| 917 | .channels_min = 1, |
| 918 | .channels_max = 8, |
| 919 | .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | |
| 920 | SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | |
| 921 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 | |
| 922 | SNDRV_PCM_RATE_192000, |
| 923 | .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE, |
| 924 | }, |
| 925 | .ops = &vc4_hdmi_audio_dai_ops, |
| 926 | }; |
| 927 | |
| 928 | static const struct snd_dmaengine_pcm_config pcm_conf = { |
| 929 | .chan_names[SNDRV_PCM_STREAM_PLAYBACK] = "audio-rx", |
| 930 | .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config, |
| 931 | }; |
| 932 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 933 | static int vc4_hdmi_audio_init(struct vc4_hdmi *vc4_hdmi) |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 934 | { |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 935 | const struct vc4_hdmi_register *mai_data = |
| 936 | &vc4_hdmi->variant->registers[HDMI_MAI_DATA]; |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 937 | struct snd_soc_dai_link *dai_link = &vc4_hdmi->audio.link; |
| 938 | struct snd_soc_card *card = &vc4_hdmi->audio.card; |
| 939 | struct device *dev = &vc4_hdmi->pdev->dev; |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 940 | const __be32 *addr; |
| 941 | int ret; |
| 942 | |
| 943 | if (!of_find_property(dev->of_node, "dmas", NULL)) { |
| 944 | dev_warn(dev, |
| 945 | "'dmas' DT property is missing, no HDMI audio\n"); |
| 946 | return 0; |
| 947 | } |
| 948 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 949 | if (mai_data->reg != VC4_HD) { |
| 950 | WARN_ONCE(true, "MAI isn't in the HD block\n"); |
| 951 | return -EINVAL; |
| 952 | } |
| 953 | |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 954 | /* |
| 955 | * Get the physical address of VC4_HD_MAI_DATA. We need to retrieve |
| 956 | * the bus address specified in the DT, because the physical address |
| 957 | * (the one returned by platform_get_resource()) is not appropriate |
| 958 | * for DMA transfers. |
| 959 | * This VC/MMU should probably be exposed to avoid this kind of hacks. |
| 960 | */ |
| 961 | addr = of_get_address(dev->of_node, 1, NULL, NULL); |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 962 | 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] | 963 | vc4_hdmi->audio.dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; |
| 964 | vc4_hdmi->audio.dma_data.maxburst = 2; |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 965 | |
| 966 | ret = devm_snd_dmaengine_pcm_register(dev, &pcm_conf, 0); |
| 967 | if (ret) { |
| 968 | dev_err(dev, "Could not register PCM component: %d\n", ret); |
| 969 | return ret; |
| 970 | } |
| 971 | |
| 972 | ret = devm_snd_soc_register_component(dev, &vc4_hdmi_audio_cpu_dai_comp, |
| 973 | &vc4_hdmi_audio_cpu_dai_drv, 1); |
| 974 | if (ret) { |
| 975 | dev_err(dev, "Could not register CPU DAI: %d\n", ret); |
| 976 | return ret; |
| 977 | } |
| 978 | |
Kuninori Morimoto | 635b1c1 | 2018-01-29 04:35:04 +0000 | [diff] [blame] | 979 | /* register component and codec dai */ |
| 980 | ret = devm_snd_soc_register_component(dev, &vc4_hdmi_audio_component_drv, |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 981 | &vc4_hdmi_audio_codec_dai_drv, 1); |
| 982 | if (ret) { |
Kuninori Morimoto | 635b1c1 | 2018-01-29 04:35:04 +0000 | [diff] [blame] | 983 | dev_err(dev, "Could not register component: %d\n", ret); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 984 | return ret; |
| 985 | } |
| 986 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 987 | dai_link->cpus = &vc4_hdmi->audio.cpu; |
| 988 | dai_link->codecs = &vc4_hdmi->audio.codec; |
| 989 | dai_link->platforms = &vc4_hdmi->audio.platform; |
Kuninori Morimoto | 0467d8e | 2019-06-06 13:19:19 +0900 | [diff] [blame] | 990 | |
| 991 | dai_link->num_cpus = 1; |
| 992 | dai_link->num_codecs = 1; |
Kuninori Morimoto | 8a90efd | 2019-06-28 10:46:14 +0900 | [diff] [blame] | 993 | dai_link->num_platforms = 1; |
Kuninori Morimoto | 0467d8e | 2019-06-06 13:19:19 +0900 | [diff] [blame] | 994 | |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 995 | dai_link->name = "MAI"; |
| 996 | dai_link->stream_name = "MAI PCM"; |
Kuninori Morimoto | 0467d8e | 2019-06-06 13:19:19 +0900 | [diff] [blame] | 997 | dai_link->codecs->dai_name = vc4_hdmi_audio_codec_dai_drv.name; |
| 998 | dai_link->cpus->dai_name = dev_name(dev); |
| 999 | dai_link->codecs->name = dev_name(dev); |
Kuninori Morimoto | 8a90efd | 2019-06-28 10:46:14 +0900 | [diff] [blame] | 1000 | dai_link->platforms->name = dev_name(dev); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1001 | |
| 1002 | card->dai_link = dai_link; |
| 1003 | card->num_links = 1; |
| 1004 | card->name = "vc4-hdmi"; |
| 1005 | card->dev = dev; |
| 1006 | |
| 1007 | /* |
| 1008 | * Be careful, snd_soc_register_card() calls dev_set_drvdata() and |
| 1009 | * stores a pointer to the snd card object in dev->driver_data. This |
| 1010 | * means we cannot use it for something else. The hdmi back-pointer is |
| 1011 | * now stored in card->drvdata and should be retrieved with |
| 1012 | * snd_soc_card_get_drvdata() if needed. |
| 1013 | */ |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1014 | snd_soc_card_set_drvdata(card, vc4_hdmi); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1015 | ret = devm_snd_soc_register_card(dev, card); |
Kuninori Morimoto | 635b1c1 | 2018-01-29 04:35:04 +0000 | [diff] [blame] | 1016 | if (ret) |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1017 | dev_err(dev, "Could not register sound card: %d\n", ret); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1018 | |
| 1019 | return ret; |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1020 | |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1021 | } |
| 1022 | |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1023 | #ifdef CONFIG_DRM_VC4_HDMI_CEC |
| 1024 | static irqreturn_t vc4_cec_irq_handler_thread(int irq, void *priv) |
| 1025 | { |
Maxime Ripard | 66bf1c3 | 2020-09-03 10:01:18 +0200 | [diff] [blame] | 1026 | struct vc4_hdmi *vc4_hdmi = priv; |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1027 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1028 | if (vc4_hdmi->cec_irq_was_rx) { |
| 1029 | if (vc4_hdmi->cec_rx_msg.len) |
| 1030 | cec_received_msg(vc4_hdmi->cec_adap, |
| 1031 | &vc4_hdmi->cec_rx_msg); |
| 1032 | } else if (vc4_hdmi->cec_tx_ok) { |
| 1033 | cec_transmit_done(vc4_hdmi->cec_adap, CEC_TX_STATUS_OK, |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1034 | 0, 0, 0, 0); |
| 1035 | } else { |
| 1036 | /* |
| 1037 | * This CEC implementation makes 1 retry, so if we |
| 1038 | * get a NACK, then that means it made 2 attempts. |
| 1039 | */ |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1040 | cec_transmit_done(vc4_hdmi->cec_adap, CEC_TX_STATUS_NACK, |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1041 | 0, 2, 0, 0); |
| 1042 | } |
| 1043 | return IRQ_HANDLED; |
| 1044 | } |
| 1045 | |
Maxime Ripard | 66bf1c3 | 2020-09-03 10:01:18 +0200 | [diff] [blame] | 1046 | 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] | 1047 | { |
Maxime Ripard | 1331145 | 2020-09-03 10:01:15 +0200 | [diff] [blame] | 1048 | struct cec_msg *msg = &vc4_hdmi->cec_rx_msg; |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1049 | unsigned int i; |
| 1050 | |
| 1051 | msg->len = 1 + ((cntrl1 & VC4_HDMI_CEC_REC_WRD_CNT_MASK) >> |
| 1052 | VC4_HDMI_CEC_REC_WRD_CNT_SHIFT); |
| 1053 | for (i = 0; i < msg->len; i += 4) { |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1054 | u32 val = HDMI_READ(HDMI_CEC_RX_DATA_1 + i); |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1055 | |
| 1056 | msg->msg[i] = val & 0xff; |
| 1057 | msg->msg[i + 1] = (val >> 8) & 0xff; |
| 1058 | msg->msg[i + 2] = (val >> 16) & 0xff; |
| 1059 | msg->msg[i + 3] = (val >> 24) & 0xff; |
| 1060 | } |
| 1061 | } |
| 1062 | |
| 1063 | static irqreturn_t vc4_cec_irq_handler(int irq, void *priv) |
| 1064 | { |
Maxime Ripard | 66bf1c3 | 2020-09-03 10:01:18 +0200 | [diff] [blame] | 1065 | struct vc4_hdmi *vc4_hdmi = priv; |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1066 | u32 stat = HDMI_READ(HDMI_CEC_CPU_STATUS); |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1067 | u32 cntrl1, cntrl5; |
| 1068 | |
| 1069 | if (!(stat & VC4_HDMI_CPU_CEC)) |
| 1070 | return IRQ_NONE; |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1071 | vc4_hdmi->cec_rx_msg.len = 0; |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1072 | cntrl1 = HDMI_READ(HDMI_CEC_CNTRL_1); |
| 1073 | cntrl5 = HDMI_READ(HDMI_CEC_CNTRL_5); |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1074 | vc4_hdmi->cec_irq_was_rx = cntrl5 & VC4_HDMI_CEC_RX_CEC_INT; |
| 1075 | if (vc4_hdmi->cec_irq_was_rx) { |
Maxime Ripard | 66bf1c3 | 2020-09-03 10:01:18 +0200 | [diff] [blame] | 1076 | vc4_cec_read_msg(vc4_hdmi, cntrl1); |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1077 | cntrl1 |= VC4_HDMI_CEC_CLEAR_RECEIVE_OFF; |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1078 | HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1); |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1079 | cntrl1 &= ~VC4_HDMI_CEC_CLEAR_RECEIVE_OFF; |
| 1080 | } else { |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1081 | vc4_hdmi->cec_tx_ok = cntrl1 & VC4_HDMI_CEC_TX_STATUS_GOOD; |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1082 | cntrl1 &= ~VC4_HDMI_CEC_START_XMIT_BEGIN; |
| 1083 | } |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1084 | HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1); |
| 1085 | HDMI_WRITE(HDMI_CEC_CPU_CLEAR, VC4_HDMI_CPU_CEC); |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1086 | |
| 1087 | return IRQ_WAKE_THREAD; |
| 1088 | } |
| 1089 | |
| 1090 | static int vc4_hdmi_cec_adap_enable(struct cec_adapter *adap, bool enable) |
| 1091 | { |
Maxime Ripard | 66bf1c3 | 2020-09-03 10:01:18 +0200 | [diff] [blame] | 1092 | struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap); |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1093 | /* clock period in microseconds */ |
| 1094 | const u32 usecs = 1000000 / CEC_CLOCK_FREQ; |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1095 | u32 val = HDMI_READ(HDMI_CEC_CNTRL_5); |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1096 | |
| 1097 | val &= ~(VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET | |
| 1098 | VC4_HDMI_CEC_CNT_TO_4700_US_MASK | |
| 1099 | VC4_HDMI_CEC_CNT_TO_4500_US_MASK); |
| 1100 | val |= ((4700 / usecs) << VC4_HDMI_CEC_CNT_TO_4700_US_SHIFT) | |
| 1101 | ((4500 / usecs) << VC4_HDMI_CEC_CNT_TO_4500_US_SHIFT); |
| 1102 | |
| 1103 | if (enable) { |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1104 | HDMI_WRITE(HDMI_CEC_CNTRL_5, val | |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1105 | VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET); |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1106 | HDMI_WRITE(HDMI_CEC_CNTRL_5, val); |
| 1107 | HDMI_WRITE(HDMI_CEC_CNTRL_2, |
| 1108 | ((1500 / usecs) << VC4_HDMI_CEC_CNT_TO_1500_US_SHIFT) | |
| 1109 | ((1300 / usecs) << VC4_HDMI_CEC_CNT_TO_1300_US_SHIFT) | |
| 1110 | ((800 / usecs) << VC4_HDMI_CEC_CNT_TO_800_US_SHIFT) | |
| 1111 | ((600 / usecs) << VC4_HDMI_CEC_CNT_TO_600_US_SHIFT) | |
| 1112 | ((400 / usecs) << VC4_HDMI_CEC_CNT_TO_400_US_SHIFT)); |
| 1113 | HDMI_WRITE(HDMI_CEC_CNTRL_3, |
| 1114 | ((2750 / usecs) << VC4_HDMI_CEC_CNT_TO_2750_US_SHIFT) | |
| 1115 | ((2400 / usecs) << VC4_HDMI_CEC_CNT_TO_2400_US_SHIFT) | |
| 1116 | ((2050 / usecs) << VC4_HDMI_CEC_CNT_TO_2050_US_SHIFT) | |
| 1117 | ((1700 / usecs) << VC4_HDMI_CEC_CNT_TO_1700_US_SHIFT)); |
| 1118 | HDMI_WRITE(HDMI_CEC_CNTRL_4, |
| 1119 | ((4300 / usecs) << VC4_HDMI_CEC_CNT_TO_4300_US_SHIFT) | |
| 1120 | ((3900 / usecs) << VC4_HDMI_CEC_CNT_TO_3900_US_SHIFT) | |
| 1121 | ((3600 / usecs) << VC4_HDMI_CEC_CNT_TO_3600_US_SHIFT) | |
| 1122 | ((3500 / usecs) << VC4_HDMI_CEC_CNT_TO_3500_US_SHIFT)); |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1123 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1124 | HDMI_WRITE(HDMI_CEC_CPU_MASK_CLEAR, VC4_HDMI_CPU_CEC); |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1125 | } else { |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1126 | HDMI_WRITE(HDMI_CEC_CPU_MASK_SET, VC4_HDMI_CPU_CEC); |
| 1127 | HDMI_WRITE(HDMI_CEC_CNTRL_5, val | |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1128 | VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET); |
| 1129 | } |
| 1130 | return 0; |
| 1131 | } |
| 1132 | |
| 1133 | static int vc4_hdmi_cec_adap_log_addr(struct cec_adapter *adap, u8 log_addr) |
| 1134 | { |
Maxime Ripard | 66bf1c3 | 2020-09-03 10:01:18 +0200 | [diff] [blame] | 1135 | struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap); |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1136 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1137 | HDMI_WRITE(HDMI_CEC_CNTRL_1, |
| 1138 | (HDMI_READ(HDMI_CEC_CNTRL_1) & ~VC4_HDMI_CEC_ADDR_MASK) | |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1139 | (log_addr & 0xf) << VC4_HDMI_CEC_ADDR_SHIFT); |
| 1140 | return 0; |
| 1141 | } |
| 1142 | |
| 1143 | static int vc4_hdmi_cec_adap_transmit(struct cec_adapter *adap, u8 attempts, |
| 1144 | u32 signal_free_time, struct cec_msg *msg) |
| 1145 | { |
Maxime Ripard | 66bf1c3 | 2020-09-03 10:01:18 +0200 | [diff] [blame] | 1146 | struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap); |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1147 | u32 val; |
| 1148 | unsigned int i; |
| 1149 | |
| 1150 | for (i = 0; i < msg->len; i += 4) |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1151 | HDMI_WRITE(HDMI_CEC_TX_DATA_1 + i, |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1152 | (msg->msg[i]) | |
| 1153 | (msg->msg[i + 1] << 8) | |
| 1154 | (msg->msg[i + 2] << 16) | |
| 1155 | (msg->msg[i + 3] << 24)); |
| 1156 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1157 | val = HDMI_READ(HDMI_CEC_CNTRL_1); |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1158 | val &= ~VC4_HDMI_CEC_START_XMIT_BEGIN; |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1159 | HDMI_WRITE(HDMI_CEC_CNTRL_1, val); |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1160 | val &= ~VC4_HDMI_CEC_MESSAGE_LENGTH_MASK; |
| 1161 | val |= (msg->len - 1) << VC4_HDMI_CEC_MESSAGE_LENGTH_SHIFT; |
| 1162 | val |= VC4_HDMI_CEC_START_XMIT_BEGIN; |
| 1163 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1164 | HDMI_WRITE(HDMI_CEC_CNTRL_1, val); |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1165 | return 0; |
| 1166 | } |
| 1167 | |
| 1168 | static const struct cec_adap_ops vc4_hdmi_cec_adap_ops = { |
| 1169 | .adap_enable = vc4_hdmi_cec_adap_enable, |
| 1170 | .adap_log_addr = vc4_hdmi_cec_adap_log_addr, |
| 1171 | .adap_transmit = vc4_hdmi_cec_adap_transmit, |
| 1172 | }; |
Maxime Ripard | c0791e0 | 2020-09-03 10:01:31 +0200 | [diff] [blame^] | 1173 | |
| 1174 | static int vc4_hdmi_cec_init(struct vc4_hdmi *vc4_hdmi) |
| 1175 | { |
| 1176 | struct cec_connector_info conn_info; |
| 1177 | struct platform_device *pdev = vc4_hdmi->pdev; |
| 1178 | u32 value; |
| 1179 | int ret; |
| 1180 | |
| 1181 | vc4_hdmi->cec_adap = cec_allocate_adapter(&vc4_hdmi_cec_adap_ops, |
| 1182 | vc4_hdmi, "vc4", |
| 1183 | CEC_CAP_DEFAULTS | |
| 1184 | CEC_CAP_CONNECTOR_INFO, 1); |
| 1185 | ret = PTR_ERR_OR_ZERO(vc4_hdmi->cec_adap); |
| 1186 | if (ret < 0) |
| 1187 | return ret; |
| 1188 | |
| 1189 | cec_fill_conn_info_from_drm(&conn_info, &vc4_hdmi->connector); |
| 1190 | cec_s_conn_info(vc4_hdmi->cec_adap, &conn_info); |
| 1191 | |
| 1192 | HDMI_WRITE(HDMI_CEC_CPU_MASK_SET, 0xffffffff); |
| 1193 | value = HDMI_READ(HDMI_CEC_CNTRL_1); |
| 1194 | value &= ~VC4_HDMI_CEC_DIV_CLK_CNT_MASK; |
| 1195 | /* |
| 1196 | * Set the logical address to Unregistered and set the clock |
| 1197 | * divider: the hsm_clock rate and this divider setting will |
| 1198 | * give a 40 kHz CEC clock. |
| 1199 | */ |
| 1200 | value |= VC4_HDMI_CEC_ADDR_MASK | |
| 1201 | (4091 << VC4_HDMI_CEC_DIV_CLK_CNT_SHIFT); |
| 1202 | HDMI_WRITE(HDMI_CEC_CNTRL_1, value); |
| 1203 | ret = devm_request_threaded_irq(&pdev->dev, platform_get_irq(pdev, 0), |
| 1204 | vc4_cec_irq_handler, |
| 1205 | vc4_cec_irq_handler_thread, 0, |
| 1206 | "vc4 hdmi cec", vc4_hdmi); |
| 1207 | if (ret) |
| 1208 | goto err_delete_cec_adap; |
| 1209 | |
| 1210 | ret = cec_register_adapter(vc4_hdmi->cec_adap, &pdev->dev); |
| 1211 | if (ret < 0) |
| 1212 | goto err_delete_cec_adap; |
| 1213 | |
| 1214 | return 0; |
| 1215 | |
| 1216 | err_delete_cec_adap: |
| 1217 | cec_delete_adapter(vc4_hdmi->cec_adap); |
| 1218 | |
| 1219 | return ret; |
| 1220 | } |
| 1221 | |
| 1222 | static void vc4_hdmi_cec_exit(struct vc4_hdmi *vc4_hdmi) |
| 1223 | { |
| 1224 | cec_unregister_adapter(vc4_hdmi->cec_adap); |
| 1225 | } |
| 1226 | #else |
| 1227 | static int vc4_hdmi_cec_init(struct vc4_hdmi *vc4_hdmi) |
| 1228 | { |
| 1229 | return 0; |
| 1230 | } |
| 1231 | |
| 1232 | static void vc4_hdmi_cec_exit(struct vc4_hdmi *vc4_hdmi) {}; |
| 1233 | |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1234 | #endif |
| 1235 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1236 | static int vc4_hdmi_build_regset(struct vc4_hdmi *vc4_hdmi, |
| 1237 | struct debugfs_regset32 *regset, |
| 1238 | enum vc4_hdmi_regs reg) |
| 1239 | { |
| 1240 | const struct vc4_hdmi_variant *variant = vc4_hdmi->variant; |
| 1241 | struct debugfs_reg32 *regs, *new_regs; |
| 1242 | unsigned int count = 0; |
| 1243 | unsigned int i; |
| 1244 | |
| 1245 | regs = kcalloc(variant->num_registers, sizeof(*regs), |
| 1246 | GFP_KERNEL); |
| 1247 | if (!regs) |
| 1248 | return -ENOMEM; |
| 1249 | |
| 1250 | for (i = 0; i < variant->num_registers; i++) { |
| 1251 | const struct vc4_hdmi_register *field = &variant->registers[i]; |
| 1252 | |
| 1253 | if (field->reg != reg) |
| 1254 | continue; |
| 1255 | |
| 1256 | regs[count].name = field->name; |
| 1257 | regs[count].offset = field->offset; |
| 1258 | count++; |
| 1259 | } |
| 1260 | |
| 1261 | new_regs = krealloc(regs, count * sizeof(*regs), GFP_KERNEL); |
| 1262 | if (!new_regs) |
| 1263 | return -ENOMEM; |
| 1264 | |
| 1265 | regset->base = __vc4_hdmi_get_field_base(vc4_hdmi, reg); |
| 1266 | regset->regs = new_regs; |
| 1267 | regset->nregs = count; |
| 1268 | |
| 1269 | return 0; |
| 1270 | } |
| 1271 | |
Maxime Ripard | 33c773e | 2020-09-03 10:01:22 +0200 | [diff] [blame] | 1272 | static int vc4_hdmi_init_resources(struct vc4_hdmi *vc4_hdmi) |
| 1273 | { |
| 1274 | struct platform_device *pdev = vc4_hdmi->pdev; |
| 1275 | struct device *dev = &pdev->dev; |
| 1276 | int ret; |
| 1277 | |
| 1278 | vc4_hdmi->hdmicore_regs = vc4_ioremap_regs(pdev, 0); |
| 1279 | if (IS_ERR(vc4_hdmi->hdmicore_regs)) |
| 1280 | return PTR_ERR(vc4_hdmi->hdmicore_regs); |
| 1281 | |
| 1282 | vc4_hdmi->hd_regs = vc4_ioremap_regs(pdev, 1); |
| 1283 | if (IS_ERR(vc4_hdmi->hd_regs)) |
| 1284 | return PTR_ERR(vc4_hdmi->hd_regs); |
| 1285 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1286 | ret = vc4_hdmi_build_regset(vc4_hdmi, &vc4_hdmi->hd_regset, VC4_HD); |
| 1287 | if (ret) |
| 1288 | return ret; |
Maxime Ripard | 33c773e | 2020-09-03 10:01:22 +0200 | [diff] [blame] | 1289 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1290 | ret = vc4_hdmi_build_regset(vc4_hdmi, &vc4_hdmi->hdmi_regset, VC4_HDMI); |
| 1291 | if (ret) |
| 1292 | return ret; |
Maxime Ripard | 33c773e | 2020-09-03 10:01:22 +0200 | [diff] [blame] | 1293 | |
| 1294 | vc4_hdmi->pixel_clock = devm_clk_get(dev, "pixel"); |
| 1295 | if (IS_ERR(vc4_hdmi->pixel_clock)) { |
| 1296 | ret = PTR_ERR(vc4_hdmi->pixel_clock); |
| 1297 | if (ret != -EPROBE_DEFER) |
| 1298 | DRM_ERROR("Failed to get pixel clock\n"); |
| 1299 | return ret; |
| 1300 | } |
| 1301 | |
| 1302 | vc4_hdmi->hsm_clock = devm_clk_get(dev, "hdmi"); |
| 1303 | if (IS_ERR(vc4_hdmi->hsm_clock)) { |
| 1304 | DRM_ERROR("Failed to get HDMI state machine clock\n"); |
| 1305 | return PTR_ERR(vc4_hdmi->hsm_clock); |
| 1306 | } |
| 1307 | |
| 1308 | return 0; |
| 1309 | } |
| 1310 | |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 1311 | static int vc4_hdmi_bind(struct device *dev, struct device *master, void *data) |
| 1312 | { |
Maxime Ripard | 33c773e | 2020-09-03 10:01:22 +0200 | [diff] [blame] | 1313 | const struct vc4_hdmi_variant *variant = of_device_get_match_data(dev); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 1314 | struct platform_device *pdev = to_platform_device(dev); |
| 1315 | struct drm_device *drm = dev_get_drvdata(master); |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1316 | struct vc4_hdmi *vc4_hdmi; |
Maxime Ripard | c98c85b | 2020-09-03 10:01:12 +0200 | [diff] [blame] | 1317 | struct drm_encoder *encoder; |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 1318 | struct device_node *ddc_node; |
| 1319 | u32 value; |
| 1320 | int ret; |
| 1321 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1322 | vc4_hdmi = devm_kzalloc(dev, sizeof(*vc4_hdmi), GFP_KERNEL); |
| 1323 | if (!vc4_hdmi) |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 1324 | return -ENOMEM; |
| 1325 | |
Maxime Ripard | 47c167b | 2020-09-03 10:01:19 +0200 | [diff] [blame] | 1326 | dev_set_drvdata(dev, vc4_hdmi); |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1327 | encoder = &vc4_hdmi->encoder.base.base; |
Maxime Ripard | 7d73299 | 2020-09-03 10:01:29 +0200 | [diff] [blame] | 1328 | vc4_hdmi->encoder.base.type = variant->encoder_type; |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1329 | vc4_hdmi->pdev = pdev; |
Maxime Ripard | 33c773e | 2020-09-03 10:01:22 +0200 | [diff] [blame] | 1330 | vc4_hdmi->variant = variant; |
Maxime Ripard | c98c85b | 2020-09-03 10:01:12 +0200 | [diff] [blame] | 1331 | |
Maxime Ripard | 33c773e | 2020-09-03 10:01:22 +0200 | [diff] [blame] | 1332 | ret = variant->init_resources(vc4_hdmi); |
| 1333 | if (ret) |
James Hilliard | 8f6f5e0 | 2020-05-24 19:28:59 -0600 | [diff] [blame] | 1334 | return ret; |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 1335 | |
Peter Chen | 027a697 | 2016-07-05 10:04:54 +0800 | [diff] [blame] | 1336 | ddc_node = of_parse_phandle(dev->of_node, "ddc", 0); |
| 1337 | if (!ddc_node) { |
| 1338 | DRM_ERROR("Failed to find ddc node in device tree\n"); |
| 1339 | return -ENODEV; |
| 1340 | } |
| 1341 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1342 | vc4_hdmi->ddc = of_find_i2c_adapter_by_node(ddc_node); |
Peter Chen | 027a697 | 2016-07-05 10:04:54 +0800 | [diff] [blame] | 1343 | of_node_put(ddc_node); |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1344 | if (!vc4_hdmi->ddc) { |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 1345 | DRM_DEBUG("Failed to get ddc i2c adapter by node\n"); |
| 1346 | return -EPROBE_DEFER; |
| 1347 | } |
| 1348 | |
Hans Verkuil | 10ee275 | 2017-07-16 12:48:03 +0200 | [diff] [blame] | 1349 | /* This is the rate that is set by the firmware. The number |
| 1350 | * needs to be a bit higher than the pixel clock rate |
| 1351 | * (generally 148.5Mhz). |
| 1352 | */ |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1353 | ret = clk_set_rate(vc4_hdmi->hsm_clock, HSM_CLOCK_FREQ); |
Hans Verkuil | 10ee275 | 2017-07-16 12:48:03 +0200 | [diff] [blame] | 1354 | if (ret) { |
| 1355 | DRM_ERROR("Failed to set HSM clock rate: %d\n", ret); |
| 1356 | goto err_put_i2c; |
| 1357 | } |
| 1358 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1359 | ret = clk_prepare_enable(vc4_hdmi->hsm_clock); |
Hans Verkuil | 10ee275 | 2017-07-16 12:48:03 +0200 | [diff] [blame] | 1360 | if (ret) { |
| 1361 | DRM_ERROR("Failed to turn on HDMI state machine clock: %d\n", |
| 1362 | ret); |
| 1363 | goto err_put_i2c; |
| 1364 | } |
| 1365 | |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 1366 | /* Only use the GPIO HPD pin if present in the DT, otherwise |
| 1367 | * we'll use the HDMI core's register. |
| 1368 | */ |
| 1369 | if (of_find_property(dev->of_node, "hpd-gpios", &value)) { |
Eric Anholt | 0b06e0a | 2016-02-29 17:53:01 -0800 | [diff] [blame] | 1370 | enum of_gpio_flags hpd_gpio_flags; |
| 1371 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1372 | vc4_hdmi->hpd_gpio = of_get_named_gpio_flags(dev->of_node, |
| 1373 | "hpd-gpios", 0, |
| 1374 | &hpd_gpio_flags); |
| 1375 | if (vc4_hdmi->hpd_gpio < 0) { |
| 1376 | ret = vc4_hdmi->hpd_gpio; |
Hans Verkuil | 10ee275 | 2017-07-16 12:48:03 +0200 | [diff] [blame] | 1377 | goto err_unprepare_hsm; |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 1378 | } |
Eric Anholt | 0b06e0a | 2016-02-29 17:53:01 -0800 | [diff] [blame] | 1379 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1380 | vc4_hdmi->hpd_active_low = hpd_gpio_flags & OF_GPIO_ACTIVE_LOW; |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 1381 | } |
| 1382 | |
Boris Brezillon | 4f6e3d6 | 2017-04-11 18:39:25 +0200 | [diff] [blame] | 1383 | pm_runtime_enable(dev); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 1384 | |
Maxime Ripard | c98c85b | 2020-09-03 10:01:12 +0200 | [diff] [blame] | 1385 | drm_simple_encoder_init(drm, encoder, DRM_MODE_ENCODER_TMDS); |
| 1386 | drm_encoder_helper_add(encoder, &vc4_hdmi_encoder_helper_funcs); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 1387 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1388 | ret = vc4_hdmi_connector_init(drm, vc4_hdmi); |
Maxime Ripard | c98c85b | 2020-09-03 10:01:12 +0200 | [diff] [blame] | 1389 | if (ret) |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 1390 | goto err_destroy_encoder; |
Maxime Ripard | c98c85b | 2020-09-03 10:01:12 +0200 | [diff] [blame] | 1391 | |
Maxime Ripard | c0791e0 | 2020-09-03 10:01:31 +0200 | [diff] [blame^] | 1392 | ret = vc4_hdmi_cec_init(vc4_hdmi); |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1393 | if (ret) |
Maxime Ripard | c0791e0 | 2020-09-03 10:01:31 +0200 | [diff] [blame^] | 1394 | goto err_destroy_conn; |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 1395 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1396 | ret = vc4_hdmi_audio_init(vc4_hdmi); |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1397 | if (ret) |
Maxime Ripard | c0791e0 | 2020-09-03 10:01:31 +0200 | [diff] [blame^] | 1398 | goto err_free_cec; |
Eric Anholt | bb7d785 | 2017-02-27 12:28:02 -0800 | [diff] [blame] | 1399 | |
Maxime Ripard | b2405c9 | 2020-09-03 10:01:30 +0200 | [diff] [blame] | 1400 | vc4_debugfs_add_file(drm, variant->debugfs_name, |
| 1401 | vc4_hdmi_debugfs_regs, |
| 1402 | vc4_hdmi); |
Eric Anholt | c9be804 | 2019-04-01 11:35:58 -0700 | [diff] [blame] | 1403 | |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 1404 | return 0; |
| 1405 | |
Maxime Ripard | c0791e0 | 2020-09-03 10:01:31 +0200 | [diff] [blame^] | 1406 | err_free_cec: |
| 1407 | vc4_hdmi_cec_exit(vc4_hdmi); |
Hans Verkuil | 15b4511 | 2017-07-16 12:48:04 +0200 | [diff] [blame] | 1408 | err_destroy_conn: |
Maxime Ripard | 0532e5e | 2020-09-03 10:01:21 +0200 | [diff] [blame] | 1409 | vc4_hdmi_connector_destroy(&vc4_hdmi->connector); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 1410 | err_destroy_encoder: |
Maxime Ripard | c98c85b | 2020-09-03 10:01:12 +0200 | [diff] [blame] | 1411 | drm_encoder_cleanup(encoder); |
Hans Verkuil | 10ee275 | 2017-07-16 12:48:03 +0200 | [diff] [blame] | 1412 | err_unprepare_hsm: |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1413 | clk_disable_unprepare(vc4_hdmi->hsm_clock); |
Boris Brezillon | 4f6e3d6 | 2017-04-11 18:39:25 +0200 | [diff] [blame] | 1414 | pm_runtime_disable(dev); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 1415 | err_put_i2c: |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1416 | put_device(&vc4_hdmi->ddc->dev); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 1417 | |
| 1418 | return ret; |
| 1419 | } |
| 1420 | |
| 1421 | static void vc4_hdmi_unbind(struct device *dev, struct device *master, |
| 1422 | void *data) |
| 1423 | { |
Maxime Ripard | 47c167b | 2020-09-03 10:01:19 +0200 | [diff] [blame] | 1424 | struct vc4_hdmi *vc4_hdmi; |
| 1425 | |
| 1426 | /* |
| 1427 | * ASoC makes it a bit hard to retrieve a pointer to the |
| 1428 | * vc4_hdmi structure. Registering the card will overwrite our |
| 1429 | * device drvdata with a pointer to the snd_soc_card structure, |
| 1430 | * which can then be used to retrieve whatever drvdata we want |
| 1431 | * to associate. |
| 1432 | * |
| 1433 | * However, that doesn't fly in the case where we wouldn't |
| 1434 | * register an ASoC card (because of an old DT that is missing |
| 1435 | * the dmas properties for example), then the card isn't |
| 1436 | * registered and the device drvdata wouldn't be set. |
| 1437 | * |
| 1438 | * We can deal with both cases by making sure a snd_soc_card |
| 1439 | * pointer and a vc4_hdmi structure are pointing to the same |
| 1440 | * memory address, so we can treat them indistinctly without any |
| 1441 | * issue. |
| 1442 | */ |
| 1443 | BUILD_BUG_ON(offsetof(struct vc4_hdmi_audio, card) != 0); |
| 1444 | BUILD_BUG_ON(offsetof(struct vc4_hdmi, audio) != 0); |
| 1445 | vc4_hdmi = dev_get_drvdata(dev); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 1446 | |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1447 | kfree(vc4_hdmi->hdmi_regset.regs); |
| 1448 | kfree(vc4_hdmi->hd_regset.regs); |
| 1449 | |
Maxime Ripard | c0791e0 | 2020-09-03 10:01:31 +0200 | [diff] [blame^] | 1450 | vc4_hdmi_cec_exit(vc4_hdmi); |
Maxime Ripard | 0532e5e | 2020-09-03 10:01:21 +0200 | [diff] [blame] | 1451 | vc4_hdmi_connector_destroy(&vc4_hdmi->connector); |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1452 | drm_encoder_cleanup(&vc4_hdmi->encoder.base.base); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 1453 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1454 | clk_disable_unprepare(vc4_hdmi->hsm_clock); |
Boris Brezillon | 4f6e3d6 | 2017-04-11 18:39:25 +0200 | [diff] [blame] | 1455 | pm_runtime_disable(dev); |
| 1456 | |
Maxime Ripard | 3408cc2 | 2020-09-03 10:01:14 +0200 | [diff] [blame] | 1457 | put_device(&vc4_hdmi->ddc->dev); |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 1458 | } |
| 1459 | |
| 1460 | static const struct component_ops vc4_hdmi_ops = { |
| 1461 | .bind = vc4_hdmi_bind, |
| 1462 | .unbind = vc4_hdmi_unbind, |
| 1463 | }; |
| 1464 | |
| 1465 | static int vc4_hdmi_dev_probe(struct platform_device *pdev) |
| 1466 | { |
| 1467 | return component_add(&pdev->dev, &vc4_hdmi_ops); |
| 1468 | } |
| 1469 | |
| 1470 | static int vc4_hdmi_dev_remove(struct platform_device *pdev) |
| 1471 | { |
| 1472 | component_del(&pdev->dev, &vc4_hdmi_ops); |
| 1473 | return 0; |
| 1474 | } |
| 1475 | |
Maxime Ripard | 33c773e | 2020-09-03 10:01:22 +0200 | [diff] [blame] | 1476 | static const struct vc4_hdmi_variant bcm2835_variant = { |
Maxime Ripard | 7d73299 | 2020-09-03 10:01:29 +0200 | [diff] [blame] | 1477 | .encoder_type = VC4_ENCODER_TYPE_HDMI0, |
Maxime Ripard | b2405c9 | 2020-09-03 10:01:30 +0200 | [diff] [blame] | 1478 | .debugfs_name = "hdmi_regs", |
Maxime Ripard | 311e305 | 2020-09-03 10:01:23 +0200 | [diff] [blame] | 1479 | .registers = vc4_hdmi_fields, |
| 1480 | .num_registers = ARRAY_SIZE(vc4_hdmi_fields), |
| 1481 | |
Maxime Ripard | 33c773e | 2020-09-03 10:01:22 +0200 | [diff] [blame] | 1482 | .init_resources = vc4_hdmi_init_resources, |
Maxime Ripard | 89f31a2 | 2020-09-03 10:01:27 +0200 | [diff] [blame] | 1483 | .csc_setup = vc4_hdmi_csc_setup, |
Maxime Ripard | 9045e91 | 2020-09-03 10:01:24 +0200 | [diff] [blame] | 1484 | .reset = vc4_hdmi_reset, |
Maxime Ripard | 904f668 | 2020-09-03 10:01:28 +0200 | [diff] [blame] | 1485 | .set_timings = vc4_hdmi_set_timings, |
Maxime Ripard | c457b8a | 2020-09-03 10:01:25 +0200 | [diff] [blame] | 1486 | .phy_init = vc4_hdmi_phy_init, |
| 1487 | .phy_disable = vc4_hdmi_phy_disable, |
Maxime Ripard | 647b965 | 2020-09-03 10:01:26 +0200 | [diff] [blame] | 1488 | .phy_rng_enable = vc4_hdmi_phy_rng_enable, |
| 1489 | .phy_rng_disable = vc4_hdmi_phy_rng_disable, |
Maxime Ripard | 33c773e | 2020-09-03 10:01:22 +0200 | [diff] [blame] | 1490 | }; |
| 1491 | |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 1492 | static const struct of_device_id vc4_hdmi_dt_match[] = { |
Maxime Ripard | 33c773e | 2020-09-03 10:01:22 +0200 | [diff] [blame] | 1493 | { .compatible = "brcm,bcm2835-hdmi", .data = &bcm2835_variant }, |
Eric Anholt | c8b75bc | 2015-03-02 13:01:12 -0800 | [diff] [blame] | 1494 | {} |
| 1495 | }; |
| 1496 | |
| 1497 | struct platform_driver vc4_hdmi_driver = { |
| 1498 | .probe = vc4_hdmi_dev_probe, |
| 1499 | .remove = vc4_hdmi_dev_remove, |
| 1500 | .driver = { |
| 1501 | .name = "vc4_hdmi", |
| 1502 | .of_match_table = vc4_hdmi_dt_match, |
| 1503 | }, |
| 1504 | }; |