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