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