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