blob: 1e6c0e26d1863b3cf2b735bb639f843c0b1c4714 [file] [log] [blame]
Thomas Gleixnercaab2772019-06-03 07:44:50 +02001// SPDX-License-Identifier: GPL-2.0-only
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002/*
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 Anholtc8b75bc2015-03-02 13:01:12 -08007 */
8
9/**
10 * DOC: VC4 Falcon HDMI module
11 *
Eric Anholtf6c01532017-02-27 12:11:43 -080012 * 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 Anholtc8b75bc2015-03-02 13:01:12 -080032 */
33
Masahiro Yamadab7e8e252017-05-18 13:29:38 +090034#include <drm/drm_atomic_helper.h>
Masahiro Yamadab7e8e252017-05-18 13:29:38 +090035#include <drm/drm_edid.h>
Daniel Vetterfcd70cd2019-01-17 22:03:34 +010036#include <drm/drm_probe_helper.h>
Thomas Zimmermannf6ebc1b2020-03-05 16:59:46 +010037#include <drm/drm_simple_kms_helper.h>
Masahiro Yamadab7e8e252017-05-18 13:29:38 +090038#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 Verkuil15b45112017-07-16 12:48:04 +020050#include "media/cec.h"
Eric Anholtc8b75bc2015-03-02 13:01:12 -080051#include "vc4_drv.h"
Maxime Ripardf73100c2020-09-03 10:01:11 +020052#include "vc4_hdmi.h"
Maxime Ripard311e3052020-09-03 10:01:23 +020053#include "vc4_hdmi_regs.h"
Eric Anholtc8b75bc2015-03-02 13:01:12 -080054#include "vc4_regs.h"
55
Hans Verkuil15b45112017-07-16 12:48:04 +020056#define HSM_CLOCK_FREQ 163682864
57#define CEC_CLOCK_FREQ 40000
Hans Verkuil15b45112017-07-16 12:48:04 +020058
Eric Anholtc9be8042019-04-01 11:35:58 -070059static int vc4_hdmi_debugfs_regs(struct seq_file *m, void *unused)
Eric Anholtc8b75bc2015-03-02 13:01:12 -080060{
61 struct drm_info_node *node = (struct drm_info_node *)m->private;
Maxime Ripard3408cc22020-09-03 10:01:14 +020062 struct vc4_hdmi *vc4_hdmi = node->info_ent->data;
Eric Anholt30517192019-02-20 13:03:38 -080063 struct drm_printer p = drm_seq_file_printer(m);
Eric Anholtc8b75bc2015-03-02 13:01:12 -080064
Maxime Ripard3408cc22020-09-03 10:01:14 +020065 drm_print_regset32(&p, &vc4_hdmi->hdmi_regset);
66 drm_print_regset32(&p, &vc4_hdmi->hd_regset);
Eric Anholtc8b75bc2015-03-02 13:01:12 -080067
68 return 0;
69}
Eric Anholtc8b75bc2015-03-02 13:01:12 -080070
Maxime Ripard9045e912020-09-03 10:01:24 +020071static 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 Anholtc8b75bc2015-03-02 13:01:12 -080086static enum drm_connector_status
87vc4_hdmi_connector_detect(struct drm_connector *connector, bool force)
88{
Maxime Ripard5dfbcae2020-09-03 10:01:17 +020089 struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector);
Eric Anholtc8b75bc2015-03-02 13:01:12 -080090
Maxime Ripardb10db9a2020-09-03 10:01:16 +020091 if (vc4_hdmi->hpd_gpio) {
92 if (gpio_get_value_cansleep(vc4_hdmi->hpd_gpio) ^
93 vc4_hdmi->hpd_active_low)
Eric Anholtc8b75bc2015-03-02 13:01:12 -080094 return connector_status_connected;
Maxime Ripardb10db9a2020-09-03 10:01:16 +020095 cec_phys_addr_invalidate(vc4_hdmi->cec_adap);
Hans Verkuil15b45112017-07-16 12:48:04 +020096 return connector_status_disconnected;
Eric Anholtc8b75bc2015-03-02 13:01:12 -080097 }
98
Maxime Ripardb10db9a2020-09-03 10:01:16 +020099 if (drm_probe_ddc(vc4_hdmi->ddc))
Eric Anholt9d44abb2016-09-14 19:21:29 +0100100 return connector_status_connected;
101
Maxime Ripard311e3052020-09-03 10:01:23 +0200102 if (HDMI_READ(HDMI_HOTPLUG) & VC4_HDMI_HOTPLUG_CONNECTED)
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800103 return connector_status_connected;
Maxime Ripardb10db9a2020-09-03 10:01:16 +0200104 cec_phys_addr_invalidate(vc4_hdmi->cec_adap);
Hans Verkuil15b45112017-07-16 12:48:04 +0200105 return connector_status_disconnected;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800106}
107
108static void vc4_hdmi_connector_destroy(struct drm_connector *connector)
109{
110 drm_connector_unregister(connector);
111 drm_connector_cleanup(connector);
112}
113
114static int vc4_hdmi_connector_get_modes(struct drm_connector *connector)
115{
Maxime Ripard5dfbcae2020-09-03 10:01:17 +0200116 struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector);
117 struct vc4_hdmi_encoder *vc4_encoder = &vc4_hdmi->encoder;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800118 int ret = 0;
119 struct edid *edid;
120
Maxime Ripard5dfbcae2020-09-03 10:01:17 +0200121 edid = drm_get_edid(connector, vc4_hdmi->ddc);
122 cec_s_phys_addr_from_edid(vc4_hdmi->cec_adap, edid);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800123 if (!edid)
124 return -ENODEV;
125
126 vc4_encoder->hdmi_monitor = drm_detect_hdmi_monitor(edid);
Eric Anholt21317b32016-09-29 15:34:43 -0700127
Daniel Vetterc555f022018-07-09 10:40:06 +0200128 drm_connector_update_edid_property(connector, edid);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800129 ret = drm_add_edid_modes(connector, edid);
Eric Anholt5afe0e62017-08-08 13:56:05 -0700130 kfree(edid);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800131
132 return ret;
133}
134
Maxime Ripard90b2df52019-06-19 12:17:53 +0200135static 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 Anholtc8b75bc2015-03-02 13:01:12 -0800141static const struct drm_connector_funcs vc4_hdmi_connector_funcs = {
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800142 .detect = vc4_hdmi_connector_detect,
Eric Anholt682e62c2016-09-28 17:30:25 -0700143 .fill_modes = drm_helper_probe_single_connector_modes,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800144 .destroy = vc4_hdmi_connector_destroy,
Maxime Ripard90b2df52019-06-19 12:17:53 +0200145 .reset = vc4_hdmi_connector_reset,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800146 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
147 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
148};
149
150static const struct drm_connector_helper_funcs vc4_hdmi_connector_helper_funcs = {
151 .get_modes = vc4_hdmi_connector_get_modes,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800152};
153
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200154static int vc4_hdmi_connector_init(struct drm_device *dev,
Maxime Ripardb052e702020-09-03 10:01:13 +0200155 struct vc4_hdmi *vc4_hdmi)
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800156{
Maxime Ripard0532e5e2020-09-03 10:01:21 +0200157 struct drm_connector *connector = &vc4_hdmi->connector;
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200158 struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
Boris Brezillondb999532018-12-06 15:24:39 +0100159 int ret;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800160
Andrzej Pietrasiewicz04a880f2020-01-02 14:22:58 +0100161 drm_connector_init_with_ddc(dev, connector,
162 &vc4_hdmi_connector_funcs,
163 DRM_MODE_CONNECTOR_HDMIA,
Maxime Ripardb052e702020-09-03 10:01:13 +0200164 vc4_hdmi->ddc);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800165 drm_connector_helper_add(connector, &vc4_hdmi_connector_helper_funcs);
166
Boris Brezillondb999532018-12-06 15:24:39 +0100167 /* Create and attach TV margin props to this connector. */
168 ret = drm_mode_create_tv_margin_properties(dev);
169 if (ret)
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200170 return ret;
Boris Brezillondb999532018-12-06 15:24:39 +0100171
172 drm_connector_attach_tv_margin_properties(connector);
173
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800174 connector->polled = (DRM_CONNECTOR_POLL_CONNECT |
175 DRM_CONNECTOR_POLL_DISCONNECT);
176
Mario Kleineracc1be12016-07-19 20:58:58 +0200177 connector->interlace_allowed = 1;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800178 connector->doublescan_allowed = 0;
179
Daniel Vettercde4c442018-07-09 10:40:07 +0200180 drm_connector_attach_encoder(connector, encoder);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800181
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200182 return 0;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800183}
184
Eric Anholt21317b32016-09-29 15:34:43 -0700185static int vc4_hdmi_stop_packet(struct drm_encoder *encoder,
186 enum hdmi_infoframe_type type)
187{
Maxime Ripard5dfbcae2020-09-03 10:01:17 +0200188 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
Eric Anholt21317b32016-09-29 15:34:43 -0700189 u32 packet_id = type - 0x80;
190
Maxime Ripard311e3052020-09-03 10:01:23 +0200191 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
192 HDMI_READ(HDMI_RAM_PACKET_CONFIG) & ~BIT(packet_id));
Eric Anholt21317b32016-09-29 15:34:43 -0700193
Maxime Ripard311e3052020-09-03 10:01:23 +0200194 return wait_for(!(HDMI_READ(HDMI_RAM_PACKET_STATUS) &
Eric Anholt21317b32016-09-29 15:34:43 -0700195 BIT(packet_id)), 100);
196}
197
198static void vc4_hdmi_write_infoframe(struct drm_encoder *encoder,
199 union hdmi_infoframe *frame)
200{
Maxime Ripard5dfbcae2020-09-03 10:01:17 +0200201 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
Eric Anholt21317b32016-09-29 15:34:43 -0700202 u32 packet_id = frame->any.type - 0x80;
Maxime Ripard311e3052020-09-03 10:01:23 +0200203 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 Anholt21317b32016-09-29 15:34:43 -0700208 uint8_t buffer[VC4_HDMI_PACKET_STRIDE];
209 ssize_t len, i;
210 int ret;
211
Maxime Ripard311e3052020-09-03 10:01:23 +0200212 WARN_ONCE(!(HDMI_READ(HDMI_RAM_PACKET_CONFIG) &
Eric Anholt21317b32016-09-29 15:34:43 -0700213 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 Ripard311e3052020-09-03 10:01:23 +0200227 writel(buffer[i + 0] << 0 |
228 buffer[i + 1] << 8 |
229 buffer[i + 2] << 16,
230 base + packet_reg);
Eric Anholt21317b32016-09-29 15:34:43 -0700231 packet_reg += 4;
232
Maxime Ripard311e3052020-09-03 10:01:23 +0200233 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 Anholt21317b32016-09-29 15:34:43 -0700238 packet_reg += 4;
239 }
240
Maxime Ripard311e3052020-09-03 10:01:23 +0200241 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 Anholt21317b32016-09-29 15:34:43 -0700244 BIT(packet_id)), 100);
245 if (ret)
246 DRM_ERROR("Failed to wait for infoframe to start: %d\n", ret);
247}
248
249static void vc4_hdmi_set_avi_infoframe(struct drm_encoder *encoder)
250{
Maxime Ripard5dfbcae2020-09-03 10:01:17 +0200251 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
Eric Anholt21317b32016-09-29 15:34:43 -0700252 struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
Maxime Ripard0532e5e2020-09-03 10:01:21 +0200253 struct drm_connector *connector = &vc4_hdmi->connector;
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200254 struct drm_connector_state *cstate = connector->state;
Eric Anholt21317b32016-09-29 15:34:43 -0700255 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ä13d0add2019-01-08 19:28:25 +0200260 ret = drm_hdmi_avi_infoframe_from_display_mode(&frame.avi,
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200261 connector, mode);
Eric Anholt21317b32016-09-29 15:34:43 -0700262 if (ret < 0) {
263 DRM_ERROR("couldn't fill AVI infoframe\n");
264 return;
265 }
266
Ville Syrjälä13d0add2019-01-08 19:28:25 +0200267 drm_hdmi_avi_infoframe_quant_range(&frame.avi,
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200268 connector, mode,
Ville Syrjäläa2ce26f2017-01-11 14:57:23 +0200269 vc4_encoder->limited_rgb_range ?
270 HDMI_QUANTIZATION_RANGE_LIMITED :
Ville Syrjälä1581b2d2019-01-08 19:28:28 +0200271 HDMI_QUANTIZATION_RANGE_FULL);
Eric Anholt21317b32016-09-29 15:34:43 -0700272
Ville Syrjäläcb876372019-10-08 19:48:14 +0300273 drm_hdmi_avi_infoframe_bars(&frame.avi, cstate);
Boris Brezillondb999532018-12-06 15:24:39 +0100274
Eric Anholt21317b32016-09-29 15:34:43 -0700275 vc4_hdmi_write_infoframe(encoder, &frame);
276}
277
278static 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 Anholtbb7d7852017-02-27 12:28:02 -0800294static void vc4_hdmi_set_audio_infoframe(struct drm_encoder *encoder)
295{
Maxime Ripard5dfbcae2020-09-03 10:01:17 +0200296 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800297 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 Ripard3408cc22020-09-03 10:01:14 +0200305 frame.audio.channels = vc4_hdmi->audio.channels;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800306
307 vc4_hdmi_write_infoframe(encoder, &frame);
308}
309
Eric Anholt21317b32016-09-29 15:34:43 -0700310static 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 Brezillon4f6e3d62017-04-11 18:39:25 +0200316static void vc4_hdmi_encoder_disable(struct drm_encoder *encoder)
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800317{
Maxime Ripard5dfbcae2020-09-03 10:01:17 +0200318 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200319 int ret;
320
Maxime Ripard311e3052020-09-03 10:01:23 +0200321 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG, 0);
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200322
Maxime Ripardc457b8a2020-09-03 10:01:25 +0200323 if (vc4_hdmi->variant->phy_disable)
324 vc4_hdmi->variant->phy_disable(vc4_hdmi);
325
Maxime Ripard311e3052020-09-03 10:01:23 +0200326 HDMI_WRITE(HDMI_VID_CTL,
327 HDMI_READ(HDMI_VID_CTL) & ~VC4_HD_VID_CTL_ENABLE);
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200328
Maxime Ripard3408cc22020-09-03 10:01:14 +0200329 clk_disable_unprepare(vc4_hdmi->pixel_clock);
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200330
Maxime Ripard3408cc22020-09-03 10:01:14 +0200331 ret = pm_runtime_put(&vc4_hdmi->pdev->dev);
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200332 if (ret < 0)
333 DRM_ERROR("Failed to release power domain: %d\n", ret);
334}
335
Maxime Ripard89f31a22020-09-03 10:01:27 +0200336static 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 Ripard904f6682020-09-03 10:01:28 +0200371static void vc4_hdmi_set_timings(struct vc4_hdmi *vc4_hdmi,
372 struct drm_display_mode *mode)
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200373{
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800374 bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
375 bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
Eric Anholt682e62c2016-09-28 17:30:25 -0700376 bool interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE;
Eric Anholtdfccd932016-09-29 15:34:44 -0700377 u32 pixel_rep = (mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1;
Eric Anholt682e62c2016-09-28 17:30:25 -0700378 u32 verta = (VC4_SET_FIELD(mode->crtc_vsync_end - mode->crtc_vsync_start,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800379 VC4_HDMI_VERTA_VSP) |
Eric Anholt682e62c2016-09-28 17:30:25 -0700380 VC4_SET_FIELD(mode->crtc_vsync_start - mode->crtc_vdisplay,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800381 VC4_HDMI_VERTA_VFP) |
Eric Anholt682e62c2016-09-28 17:30:25 -0700382 VC4_SET_FIELD(mode->crtc_vdisplay, VC4_HDMI_VERTA_VAL));
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800383 u32 vertb = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) |
Eric Anholt682e62c2016-09-28 17:30:25 -0700384 VC4_SET_FIELD(mode->crtc_vtotal - mode->crtc_vsync_end,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800385 VC4_HDMI_VERTB_VBP));
Eric Anholt682e62c2016-09-28 17:30:25 -0700386 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 Ripard904f6682020-09-03 10:01:28 +0200391
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
420static 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 Brezillon4f6e3d62017-04-11 18:39:25 +0200426 int ret;
427
Maxime Ripard3408cc22020-09-03 10:01:14 +0200428 ret = pm_runtime_get_sync(&vc4_hdmi->pdev->dev);
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200429 if (ret < 0) {
430 DRM_ERROR("Failed to retain power domain: %d\n", ret);
431 return;
432 }
433
Maxime Ripard3408cc22020-09-03 10:01:14 +0200434 ret = clk_set_rate(vc4_hdmi->pixel_clock,
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200435 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 Ripard3408cc22020-09-03 10:01:14 +0200442 ret = clk_prepare_enable(vc4_hdmi->pixel_clock);
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200443 if (ret) {
444 DRM_ERROR("Failed to turn on pixel clock: %d\n", ret);
445 return;
446 }
447
Maxime Ripard9045e912020-09-03 10:01:24 +0200448 if (vc4_hdmi->variant->reset)
449 vc4_hdmi->variant->reset(vc4_hdmi);
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200450
Maxime Ripardc457b8a2020-09-03 10:01:25 +0200451 if (vc4_hdmi->variant->phy_init)
452 vc4_hdmi->variant->phy_init(vc4_hdmi, mode);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800453
454 if (debug_dump_regs) {
Maxime Ripard3408cc22020-09-03 10:01:14 +0200455 struct drm_printer p = drm_info_printer(&vc4_hdmi->pdev->dev);
Eric Anholt30517192019-02-20 13:03:38 -0800456
Maxime Ripard3408cc22020-09-03 10:01:14 +0200457 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 Anholtc8b75bc2015-03-02 13:01:12 -0800460 }
461
Maxime Ripard311e3052020-09-03 10:01:23 +0200462 HDMI_WRITE(HDMI_VID_CTL, 0);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800463
Maxime Ripard311e3052020-09-03 10:01:23 +0200464 HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
465 HDMI_READ(HDMI_SCHEDULER_CONTROL) |
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800466 VC4_HDMI_SCHEDULER_CONTROL_MANUAL_FORMAT |
467 VC4_HDMI_SCHEDULER_CONTROL_IGNORE_VSYNC_PREDICTS);
468
Maxime Ripard904f6682020-09-03 10:01:28 +0200469 if (vc4_hdmi->variant->set_timings)
470 vc4_hdmi->variant->set_timings(vc4_hdmi, mode);
Eric Anholt6e1cbba2016-09-16 10:59:45 +0100471
Ville Syrjäläc8127cf02017-01-11 16:18:35 +0200472 if (vc4_encoder->hdmi_monitor &&
Maxime Ripard89f31a22020-09-03 10:01:27 +0200473 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 Anholt6e1cbba2016-09-16 10:59:45 +0100476
Eric Anholt21317b32016-09-29 15:34:43 -0700477 vc4_encoder->limited_rgb_range = true;
478 } else {
Maxime Ripard89f31a22020-09-03 10:01:27 +0200479 if (vc4_hdmi->variant->csc_setup)
480 vc4_hdmi->variant->csc_setup(vc4_hdmi, false);
481
Eric Anholt21317b32016-09-29 15:34:43 -0700482 vc4_encoder->limited_rgb_range = false;
Eric Anholt6e1cbba2016-09-16 10:59:45 +0100483 }
484
Maxime Ripard311e3052020-09-03 10:01:23 +0200485 HDMI_WRITE(HDMI_FIFO_CTL, VC4_HDMI_FIFO_CTL_MASTER_SLAVE_N);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800486
487 if (debug_dump_regs) {
Maxime Ripard3408cc22020-09-03 10:01:14 +0200488 struct drm_printer p = drm_info_printer(&vc4_hdmi->pdev->dev);
Eric Anholt30517192019-02-20 13:03:38 -0800489
Maxime Ripard3408cc22020-09-03 10:01:14 +0200490 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 Anholtc8b75bc2015-03-02 13:01:12 -0800493 }
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800494
Maxime Ripard311e3052020-09-03 10:01:23 +0200495 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 Anholtc8b75bc2015-03-02 13:01:12 -0800500
501 if (vc4_encoder->hdmi_monitor) {
Maxime Ripard311e3052020-09-03 10:01:23 +0200502 HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
503 HDMI_READ(HDMI_SCHEDULER_CONTROL) |
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800504 VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);
505
Maxime Ripard311e3052020-09-03 10:01:23 +0200506 ret = wait_for(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
Eric Anholt2b29bf12016-09-28 17:21:05 -0700507 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE, 1000);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800508 WARN_ONCE(ret, "Timeout waiting for "
509 "VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
510 } else {
Maxime Ripard311e3052020-09-03 10:01:23 +0200511 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
512 HDMI_READ(HDMI_RAM_PACKET_CONFIG) &
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800513 ~(VC4_HDMI_RAM_PACKET_ENABLE));
Maxime Ripard311e3052020-09-03 10:01:23 +0200514 HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
515 HDMI_READ(HDMI_SCHEDULER_CONTROL) &
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800516 ~VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);
517
Maxime Ripard311e3052020-09-03 10:01:23 +0200518 ret = wait_for(!(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
Eric Anholt2b29bf12016-09-28 17:21:05 -0700519 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE), 1000);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800520 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 Ripard311e3052020-09-03 10:01:23 +0200527 WARN_ON(!(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800528 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE));
Maxime Ripard311e3052020-09-03 10:01:23 +0200529 HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
530 HDMI_READ(HDMI_SCHEDULER_CONTROL) |
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800531 VC4_HDMI_SCHEDULER_CONTROL_VERT_ALWAYS_KEEPOUT);
532
Maxime Ripard311e3052020-09-03 10:01:23 +0200533 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
Eric Anholt21317b32016-09-29 15:34:43 -0700534 VC4_HDMI_RAM_PACKET_ENABLE);
535
536 vc4_hdmi_set_infoframes(encoder);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800537
Maxime Ripard311e3052020-09-03 10:01:23 +0200538 drift = HDMI_READ(HDMI_FIFO_CTL);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800539 drift &= VC4_HDMI_FIFO_VALID_WRITE_MASK;
540
Maxime Ripard311e3052020-09-03 10:01:23 +0200541 HDMI_WRITE(HDMI_FIFO_CTL,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800542 drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
Maxime Ripard311e3052020-09-03 10:01:23 +0200543 HDMI_WRITE(HDMI_FIFO_CTL,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800544 drift | VC4_HDMI_FIFO_CTL_RECENTER);
Stefan Wahrend8eb9de2018-02-24 13:38:14 +0100545 usleep_range(1000, 1100);
Maxime Ripard311e3052020-09-03 10:01:23 +0200546 HDMI_WRITE(HDMI_FIFO_CTL,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800547 drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
Maxime Ripard311e3052020-09-03 10:01:23 +0200548 HDMI_WRITE(HDMI_FIFO_CTL,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800549 drift | VC4_HDMI_FIFO_CTL_RECENTER);
550
Maxime Ripard311e3052020-09-03 10:01:23 +0200551 ret = wait_for(HDMI_READ(HDMI_FIFO_CTL) &
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800552 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 Anholt32e823c2017-09-20 15:59:34 -0700558static enum drm_mode_status
Maxime Ripard11a17312020-09-03 10:01:34 +0200559vc4_hdmi_encoder_mode_valid(struct drm_encoder *encoder,
Eric Anholt32e823c2017-09-20 15:59:34 -0700560 const struct drm_display_mode *mode)
561{
Nicolas Saenz Julienneb1e73962020-03-26 13:20:01 +0100562 /*
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 Anholt32e823c2017-09-20 15:59:34 -0700577 */
Nicolas Saenz Julienneb1e73962020-03-26 13:20:01 +0100578 if (mode->clock > HSM_CLOCK_FREQ / (1000 * 101 / 100))
Eric Anholt32e823c2017-09-20 15:59:34 -0700579 return MODE_CLOCK_HIGH;
580
581 return MODE_OK;
582}
583
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800584static const struct drm_encoder_helper_funcs vc4_hdmi_encoder_helper_funcs = {
Eric Anholt32e823c2017-09-20 15:59:34 -0700585 .mode_valid = vc4_hdmi_encoder_mode_valid,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800586 .disable = vc4_hdmi_encoder_disable,
587 .enable = vc4_hdmi_encoder_enable,
588};
589
Eric Anholtbb7d7852017-02-27 12:28:02 -0800590/* HDMI audio codec callbacks */
Maxime Ripard3408cc22020-09-03 10:01:14 +0200591static void vc4_hdmi_audio_set_mai_clock(struct vc4_hdmi *vc4_hdmi)
Eric Anholtbb7d7852017-02-27 12:28:02 -0800592{
Maxime Ripard3408cc22020-09-03 10:01:14 +0200593 u32 hsm_clock = clk_get_rate(vc4_hdmi->hsm_clock);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800594 unsigned long n, m;
595
Maxime Ripard3408cc22020-09-03 10:01:14 +0200596 rational_best_approximation(hsm_clock, vc4_hdmi->audio.samplerate,
Eric Anholtbb7d7852017-02-27 12:28:02 -0800597 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 Ripard311e3052020-09-03 10:01:23 +0200603 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 Anholtbb7d7852017-02-27 12:28:02 -0800606}
607
Maxime Ripard3408cc22020-09-03 10:01:14 +0200608static void vc4_hdmi_set_n_cts(struct vc4_hdmi *vc4_hdmi)
Eric Anholtbb7d7852017-02-27 12:28:02 -0800609{
Maxime Ripard3408cc22020-09-03 10:01:14 +0200610 struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800611 struct drm_crtc *crtc = encoder->crtc;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800612 const struct drm_display_mode *mode = &crtc->state->adjusted_mode;
Maxime Ripard3408cc22020-09-03 10:01:14 +0200613 u32 samplerate = vc4_hdmi->audio.samplerate;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800614 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 Ripard311e3052020-09-03 10:01:23 +0200622 HDMI_WRITE(HDMI_CRP_CFG,
Eric Anholtbb7d7852017-02-27 12:28:02 -0800623 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 Ripard311e3052020-09-03 10:01:23 +0200631 HDMI_WRITE(HDMI_CTS_0, cts);
632 HDMI_WRITE(HDMI_CTS_1, cts);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800633}
634
635static 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
642static int vc4_hdmi_audio_startup(struct snd_pcm_substream *substream,
643 struct snd_soc_dai *dai)
644{
Maxime Ripard3408cc22020-09-03 10:01:14 +0200645 struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai);
646 struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
Maxime Ripard0532e5e2020-09-03 10:01:21 +0200647 struct drm_connector *connector = &vc4_hdmi->connector;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800648 int ret;
649
Maxime Ripard3408cc22020-09-03 10:01:14 +0200650 if (vc4_hdmi->audio.substream && vc4_hdmi->audio.substream != substream)
Eric Anholtbb7d7852017-02-27 12:28:02 -0800651 return -EINVAL;
652
Maxime Ripard3408cc22020-09-03 10:01:14 +0200653 vc4_hdmi->audio.substream = substream;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800654
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 Ripard311e3052020-09-03 10:01:23 +0200659 if (!encoder->crtc || !(HDMI_READ(HDMI_RAM_PACKET_CONFIG) &
Eric Anholtbb7d7852017-02-27 12:28:02 -0800660 VC4_HDMI_RAM_PACKET_ENABLE))
661 return -ENODEV;
662
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200663 ret = snd_pcm_hw_constraint_eld(substream->runtime, connector->eld);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800664 if (ret)
665 return ret;
666
667 return 0;
668}
669
670static int vc4_hdmi_audio_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
671{
672 return 0;
673}
674
Maxime Ripard3408cc22020-09-03 10:01:14 +0200675static void vc4_hdmi_audio_reset(struct vc4_hdmi *vc4_hdmi)
Eric Anholtbb7d7852017-02-27 12:28:02 -0800676{
Maxime Ripard3408cc22020-09-03 10:01:14 +0200677 struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
Maxime Ripard3408cc22020-09-03 10:01:14 +0200678 struct device *dev = &vc4_hdmi->pdev->dev;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800679 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 Ripard311e3052020-09-03 10:01:23 +0200685 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 Anholtbb7d7852017-02-27 12:28:02 -0800688}
689
690static void vc4_hdmi_audio_shutdown(struct snd_pcm_substream *substream,
691 struct snd_soc_dai *dai)
692{
Maxime Ripard3408cc22020-09-03 10:01:14 +0200693 struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800694
Maxime Ripard3408cc22020-09-03 10:01:14 +0200695 if (substream != vc4_hdmi->audio.substream)
Eric Anholtbb7d7852017-02-27 12:28:02 -0800696 return;
697
Maxime Ripard3408cc22020-09-03 10:01:14 +0200698 vc4_hdmi_audio_reset(vc4_hdmi);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800699
Maxime Ripard3408cc22020-09-03 10:01:14 +0200700 vc4_hdmi->audio.substream = NULL;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800701}
702
703/* HDMI audio codec callbacks */
704static 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 Ripard3408cc22020-09-03 10:01:14 +0200708 struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai);
Maxime Ripard3408cc22020-09-03 10:01:14 +0200709 struct device *dev = &vc4_hdmi->pdev->dev;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800710 u32 audio_packet_config, channel_mask;
711 u32 channel_map, i;
712
Maxime Ripard3408cc22020-09-03 10:01:14 +0200713 if (substream != vc4_hdmi->audio.substream)
Eric Anholtbb7d7852017-02-27 12:28:02 -0800714 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 Ripard3408cc22020-09-03 10:01:14 +0200720 vc4_hdmi->audio.channels = params_channels(params);
721 vc4_hdmi->audio.samplerate = params_rate(params);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800722
Maxime Ripard311e3052020-09-03 10:01:23 +0200723 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 Anholtbb7d7852017-02-27 12:28:02 -0800729
Maxime Ripard3408cc22020-09-03 10:01:14 +0200730 vc4_hdmi_audio_set_mai_clock(vc4_hdmi);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800731
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 Ripard3408cc22020-09-03 10:01:14 +0200737 channel_mask = GENMASK(vc4_hdmi->audio.channels - 1, 0);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800738 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 Ripard3408cc22020-09-03 10:01:14 +0200742 if (vc4_hdmi->audio.samplerate > 96000) {
Maxime Ripard311e3052020-09-03 10:01:23 +0200743 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 Ripard3408cc22020-09-03 10:01:14 +0200746 } else if (vc4_hdmi->audio.samplerate > 48000) {
Maxime Ripard311e3052020-09-03 10:01:23 +0200747 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 Anholtbb7d7852017-02-27 12:28:02 -0800750 } else {
Maxime Ripard311e3052020-09-03 10:01:23 +0200751 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 Anholtbb7d7852017-02-27 12:28:02 -0800756 }
757
Maxime Ripard311e3052020-09-03 10:01:23 +0200758 HDMI_WRITE(HDMI_MAI_CONFIG,
Eric Anholtbb7d7852017-02-27 12:28:02 -0800759 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 Ripard311e3052020-09-03 10:01:23 +0200768 HDMI_WRITE(HDMI_MAI_CHANNEL_MAP, channel_map);
769 HDMI_WRITE(HDMI_AUDIO_PACKET_CONFIG, audio_packet_config);
Maxime Ripard3408cc22020-09-03 10:01:14 +0200770 vc4_hdmi_set_n_cts(vc4_hdmi);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800771
772 return 0;
773}
774
775static int vc4_hdmi_audio_trigger(struct snd_pcm_substream *substream, int cmd,
776 struct snd_soc_dai *dai)
777{
Maxime Ripard3408cc22020-09-03 10:01:14 +0200778 struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai);
779 struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800780
781 switch (cmd) {
782 case SNDRV_PCM_TRIGGER_START:
783 vc4_hdmi_set_audio_infoframe(encoder);
Maxime Ripard647b9652020-09-03 10:01:26 +0200784
785 if (vc4_hdmi->variant->phy_rng_enable)
786 vc4_hdmi->variant->phy_rng_enable(vc4_hdmi);
Maxime Ripard311e3052020-09-03 10:01:23 +0200787
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 Anholtbb7d7852017-02-27 12:28:02 -0800792 break;
793 case SNDRV_PCM_TRIGGER_STOP:
Maxime Ripard311e3052020-09-03 10:01:23 +0200794 HDMI_WRITE(HDMI_MAI_CTL,
795 VC4_HD_MAI_CTL_DLATE |
796 VC4_HD_MAI_CTL_ERRORE |
797 VC4_HD_MAI_CTL_ERRORF);
Maxime Ripard647b9652020-09-03 10:01:26 +0200798
799 if (vc4_hdmi->variant->phy_rng_disable)
800 vc4_hdmi->variant->phy_rng_disable(vc4_hdmi);
801
Eric Anholtbb7d7852017-02-27 12:28:02 -0800802 break;
803 default:
804 break;
805 }
806
807 return 0;
808}
809
810static inline struct vc4_hdmi *
811snd_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
818static 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 Ripard3408cc22020-09-03 10:01:14 +0200822 struct vc4_hdmi *vc4_hdmi = snd_component_to_hdmi(component);
Maxime Ripard0532e5e2020-09-03 10:01:21 +0200823 struct drm_connector *connector = &vc4_hdmi->connector;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800824
825 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200826 uinfo->count = sizeof(connector->eld);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800827
828 return 0;
829}
830
831static 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 Ripard3408cc22020-09-03 10:01:14 +0200835 struct vc4_hdmi *vc4_hdmi = snd_component_to_hdmi(component);
Maxime Ripard0532e5e2020-09-03 10:01:21 +0200836 struct drm_connector *connector = &vc4_hdmi->connector;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800837
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200838 memcpy(ucontrol->value.bytes.data, connector->eld,
839 sizeof(connector->eld));
Eric Anholtbb7d7852017-02-27 12:28:02 -0800840
841 return 0;
842}
843
844static 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
855static const struct snd_soc_dapm_widget vc4_hdmi_audio_widgets[] = {
856 SND_SOC_DAPM_OUTPUT("TX"),
857};
858
859static const struct snd_soc_dapm_route vc4_hdmi_audio_routes[] = {
860 { "TX", NULL, "Playback" },
861};
862
Kuninori Morimoto635b1c12018-01-29 04:35:04 +0000863static 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 Anholtbb7d7852017-02-27 12:28:02 -0800874};
875
876static 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
884static 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
898static const struct snd_soc_component_driver vc4_hdmi_audio_cpu_dai_comp = {
899 .name = "vc4-hdmi-cpu-dai-component",
900};
901
902static int vc4_hdmi_audio_cpu_dai_probe(struct snd_soc_dai *dai)
903{
Maxime Ripard3408cc22020-09-03 10:01:14 +0200904 struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800905
Maxime Ripard3408cc22020-09-03 10:01:14 +0200906 snd_soc_dai_init_dma_data(dai, &vc4_hdmi->audio.dma_data, NULL);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800907
908 return 0;
909}
910
911static 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
927static 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 Ripard3408cc22020-09-03 10:01:14 +0200932static int vc4_hdmi_audio_init(struct vc4_hdmi *vc4_hdmi)
Eric Anholtbb7d7852017-02-27 12:28:02 -0800933{
Maxime Ripard311e3052020-09-03 10:01:23 +0200934 const struct vc4_hdmi_register *mai_data =
935 &vc4_hdmi->variant->registers[HDMI_MAI_DATA];
Maxime Ripard3408cc22020-09-03 10:01:14 +0200936 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 Anholtbb7d7852017-02-27 12:28:02 -0800939 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 Ripard311e3052020-09-03 10:01:23 +0200948 if (mai_data->reg != VC4_HD) {
949 WARN_ONCE(true, "MAI isn't in the HD block\n");
950 return -EINVAL;
951 }
952
Eric Anholtbb7d7852017-02-27 12:28:02 -0800953 /*
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 Ripard311e3052020-09-03 10:01:23 +0200961 vc4_hdmi->audio.dma_data.addr = be32_to_cpup(addr) + mai_data->offset;
Maxime Ripard3408cc22020-09-03 10:01:14 +0200962 vc4_hdmi->audio.dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
963 vc4_hdmi->audio.dma_data.maxburst = 2;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800964
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 Morimoto635b1c12018-01-29 04:35:04 +0000978 /* register component and codec dai */
979 ret = devm_snd_soc_register_component(dev, &vc4_hdmi_audio_component_drv,
Eric Anholtbb7d7852017-02-27 12:28:02 -0800980 &vc4_hdmi_audio_codec_dai_drv, 1);
981 if (ret) {
Kuninori Morimoto635b1c12018-01-29 04:35:04 +0000982 dev_err(dev, "Could not register component: %d\n", ret);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800983 return ret;
984 }
985
Maxime Ripard3408cc22020-09-03 10:01:14 +0200986 dai_link->cpus = &vc4_hdmi->audio.cpu;
987 dai_link->codecs = &vc4_hdmi->audio.codec;
988 dai_link->platforms = &vc4_hdmi->audio.platform;
Kuninori Morimoto0467d8e2019-06-06 13:19:19 +0900989
990 dai_link->num_cpus = 1;
991 dai_link->num_codecs = 1;
Kuninori Morimoto8a90efd2019-06-28 10:46:14 +0900992 dai_link->num_platforms = 1;
Kuninori Morimoto0467d8e2019-06-06 13:19:19 +0900993
Eric Anholtbb7d7852017-02-27 12:28:02 -0800994 dai_link->name = "MAI";
995 dai_link->stream_name = "MAI PCM";
Kuninori Morimoto0467d8e2019-06-06 13:19:19 +0900996 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 Morimoto8a90efd2019-06-28 10:46:14 +0900999 dai_link->platforms->name = dev_name(dev);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001000
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 Ripard3408cc22020-09-03 10:01:14 +02001013 snd_soc_card_set_drvdata(card, vc4_hdmi);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001014 ret = devm_snd_soc_register_card(dev, card);
Kuninori Morimoto635b1c12018-01-29 04:35:04 +00001015 if (ret)
Eric Anholtbb7d7852017-02-27 12:28:02 -08001016 dev_err(dev, "Could not register sound card: %d\n", ret);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001017
1018 return ret;
Eric Anholtbb7d7852017-02-27 12:28:02 -08001019
Eric Anholtbb7d7852017-02-27 12:28:02 -08001020}
1021
Hans Verkuil15b45112017-07-16 12:48:04 +02001022#ifdef CONFIG_DRM_VC4_HDMI_CEC
1023static irqreturn_t vc4_cec_irq_handler_thread(int irq, void *priv)
1024{
Maxime Ripard66bf1c32020-09-03 10:01:18 +02001025 struct vc4_hdmi *vc4_hdmi = priv;
Hans Verkuil15b45112017-07-16 12:48:04 +02001026
Maxime Ripard3408cc22020-09-03 10:01:14 +02001027 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 Verkuil15b45112017-07-16 12:48:04 +02001033 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 Ripard3408cc22020-09-03 10:01:14 +02001039 cec_transmit_done(vc4_hdmi->cec_adap, CEC_TX_STATUS_NACK,
Hans Verkuil15b45112017-07-16 12:48:04 +02001040 0, 2, 0, 0);
1041 }
1042 return IRQ_HANDLED;
1043}
1044
Maxime Ripard66bf1c32020-09-03 10:01:18 +02001045static void vc4_cec_read_msg(struct vc4_hdmi *vc4_hdmi, u32 cntrl1)
Hans Verkuil15b45112017-07-16 12:48:04 +02001046{
Maxime Ripard13311452020-09-03 10:01:15 +02001047 struct cec_msg *msg = &vc4_hdmi->cec_rx_msg;
Hans Verkuil15b45112017-07-16 12:48:04 +02001048 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 Ripard311e3052020-09-03 10:01:23 +02001053 u32 val = HDMI_READ(HDMI_CEC_RX_DATA_1 + i);
Hans Verkuil15b45112017-07-16 12:48:04 +02001054
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
1062static irqreturn_t vc4_cec_irq_handler(int irq, void *priv)
1063{
Maxime Ripard66bf1c32020-09-03 10:01:18 +02001064 struct vc4_hdmi *vc4_hdmi = priv;
Maxime Ripard311e3052020-09-03 10:01:23 +02001065 u32 stat = HDMI_READ(HDMI_CEC_CPU_STATUS);
Hans Verkuil15b45112017-07-16 12:48:04 +02001066 u32 cntrl1, cntrl5;
1067
1068 if (!(stat & VC4_HDMI_CPU_CEC))
1069 return IRQ_NONE;
Maxime Ripard3408cc22020-09-03 10:01:14 +02001070 vc4_hdmi->cec_rx_msg.len = 0;
Maxime Ripard311e3052020-09-03 10:01:23 +02001071 cntrl1 = HDMI_READ(HDMI_CEC_CNTRL_1);
1072 cntrl5 = HDMI_READ(HDMI_CEC_CNTRL_5);
Maxime Ripard3408cc22020-09-03 10:01:14 +02001073 vc4_hdmi->cec_irq_was_rx = cntrl5 & VC4_HDMI_CEC_RX_CEC_INT;
1074 if (vc4_hdmi->cec_irq_was_rx) {
Maxime Ripard66bf1c32020-09-03 10:01:18 +02001075 vc4_cec_read_msg(vc4_hdmi, cntrl1);
Hans Verkuil15b45112017-07-16 12:48:04 +02001076 cntrl1 |= VC4_HDMI_CEC_CLEAR_RECEIVE_OFF;
Maxime Ripard311e3052020-09-03 10:01:23 +02001077 HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1);
Hans Verkuil15b45112017-07-16 12:48:04 +02001078 cntrl1 &= ~VC4_HDMI_CEC_CLEAR_RECEIVE_OFF;
1079 } else {
Maxime Ripard3408cc22020-09-03 10:01:14 +02001080 vc4_hdmi->cec_tx_ok = cntrl1 & VC4_HDMI_CEC_TX_STATUS_GOOD;
Hans Verkuil15b45112017-07-16 12:48:04 +02001081 cntrl1 &= ~VC4_HDMI_CEC_START_XMIT_BEGIN;
1082 }
Maxime Ripard311e3052020-09-03 10:01:23 +02001083 HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1);
1084 HDMI_WRITE(HDMI_CEC_CPU_CLEAR, VC4_HDMI_CPU_CEC);
Hans Verkuil15b45112017-07-16 12:48:04 +02001085
1086 return IRQ_WAKE_THREAD;
1087}
1088
1089static int vc4_hdmi_cec_adap_enable(struct cec_adapter *adap, bool enable)
1090{
Maxime Ripard66bf1c32020-09-03 10:01:18 +02001091 struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
Hans Verkuil15b45112017-07-16 12:48:04 +02001092 /* clock period in microseconds */
1093 const u32 usecs = 1000000 / CEC_CLOCK_FREQ;
Maxime Ripard311e3052020-09-03 10:01:23 +02001094 u32 val = HDMI_READ(HDMI_CEC_CNTRL_5);
Hans Verkuil15b45112017-07-16 12:48:04 +02001095
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 Ripard311e3052020-09-03 10:01:23 +02001103 HDMI_WRITE(HDMI_CEC_CNTRL_5, val |
Hans Verkuil15b45112017-07-16 12:48:04 +02001104 VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET);
Maxime Ripard311e3052020-09-03 10:01:23 +02001105 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 Verkuil15b45112017-07-16 12:48:04 +02001122
Maxime Ripard311e3052020-09-03 10:01:23 +02001123 HDMI_WRITE(HDMI_CEC_CPU_MASK_CLEAR, VC4_HDMI_CPU_CEC);
Hans Verkuil15b45112017-07-16 12:48:04 +02001124 } else {
Maxime Ripard311e3052020-09-03 10:01:23 +02001125 HDMI_WRITE(HDMI_CEC_CPU_MASK_SET, VC4_HDMI_CPU_CEC);
1126 HDMI_WRITE(HDMI_CEC_CNTRL_5, val |
Hans Verkuil15b45112017-07-16 12:48:04 +02001127 VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET);
1128 }
1129 return 0;
1130}
1131
1132static int vc4_hdmi_cec_adap_log_addr(struct cec_adapter *adap, u8 log_addr)
1133{
Maxime Ripard66bf1c32020-09-03 10:01:18 +02001134 struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
Hans Verkuil15b45112017-07-16 12:48:04 +02001135
Maxime Ripard311e3052020-09-03 10:01:23 +02001136 HDMI_WRITE(HDMI_CEC_CNTRL_1,
1137 (HDMI_READ(HDMI_CEC_CNTRL_1) & ~VC4_HDMI_CEC_ADDR_MASK) |
Hans Verkuil15b45112017-07-16 12:48:04 +02001138 (log_addr & 0xf) << VC4_HDMI_CEC_ADDR_SHIFT);
1139 return 0;
1140}
1141
1142static int vc4_hdmi_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
1143 u32 signal_free_time, struct cec_msg *msg)
1144{
Maxime Ripard66bf1c32020-09-03 10:01:18 +02001145 struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
Hans Verkuil15b45112017-07-16 12:48:04 +02001146 u32 val;
1147 unsigned int i;
1148
1149 for (i = 0; i < msg->len; i += 4)
Maxime Ripard311e3052020-09-03 10:01:23 +02001150 HDMI_WRITE(HDMI_CEC_TX_DATA_1 + i,
Hans Verkuil15b45112017-07-16 12:48:04 +02001151 (msg->msg[i]) |
1152 (msg->msg[i + 1] << 8) |
1153 (msg->msg[i + 2] << 16) |
1154 (msg->msg[i + 3] << 24));
1155
Maxime Ripard311e3052020-09-03 10:01:23 +02001156 val = HDMI_READ(HDMI_CEC_CNTRL_1);
Hans Verkuil15b45112017-07-16 12:48:04 +02001157 val &= ~VC4_HDMI_CEC_START_XMIT_BEGIN;
Maxime Ripard311e3052020-09-03 10:01:23 +02001158 HDMI_WRITE(HDMI_CEC_CNTRL_1, val);
Hans Verkuil15b45112017-07-16 12:48:04 +02001159 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 Ripard311e3052020-09-03 10:01:23 +02001163 HDMI_WRITE(HDMI_CEC_CNTRL_1, val);
Hans Verkuil15b45112017-07-16 12:48:04 +02001164 return 0;
1165}
1166
1167static 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 Ripardc0791e02020-09-03 10:01:31 +02001172
1173static 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 Ripard234f4212020-09-03 10:01:32 +02001180 if (!vc4_hdmi->variant->cec_available)
1181 return 0;
1182
Maxime Ripardc0791e02020-09-03 10:01:31 +02001183 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
1218err_delete_cec_adap:
1219 cec_delete_adapter(vc4_hdmi->cec_adap);
1220
1221 return ret;
1222}
1223
1224static void vc4_hdmi_cec_exit(struct vc4_hdmi *vc4_hdmi)
1225{
1226 cec_unregister_adapter(vc4_hdmi->cec_adap);
1227}
1228#else
1229static int vc4_hdmi_cec_init(struct vc4_hdmi *vc4_hdmi)
1230{
1231 return 0;
1232}
1233
1234static void vc4_hdmi_cec_exit(struct vc4_hdmi *vc4_hdmi) {};
1235
Hans Verkuil15b45112017-07-16 12:48:04 +02001236#endif
1237
Maxime Ripard311e3052020-09-03 10:01:23 +02001238static 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 Ripard33c773e2020-09-03 10:01:22 +02001274static 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 Ripard311e3052020-09-03 10:01:23 +02001288 ret = vc4_hdmi_build_regset(vc4_hdmi, &vc4_hdmi->hd_regset, VC4_HD);
1289 if (ret)
1290 return ret;
Maxime Ripard33c773e2020-09-03 10:01:22 +02001291
Maxime Ripard311e3052020-09-03 10:01:23 +02001292 ret = vc4_hdmi_build_regset(vc4_hdmi, &vc4_hdmi->hdmi_regset, VC4_HDMI);
1293 if (ret)
1294 return ret;
Maxime Ripard33c773e2020-09-03 10:01:22 +02001295
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 Anholtc8b75bc2015-03-02 13:01:12 -08001313static int vc4_hdmi_bind(struct device *dev, struct device *master, void *data)
1314{
Maxime Ripard33c773e2020-09-03 10:01:22 +02001315 const struct vc4_hdmi_variant *variant = of_device_get_match_data(dev);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001316 struct platform_device *pdev = to_platform_device(dev);
1317 struct drm_device *drm = dev_get_drvdata(master);
Maxime Ripard3408cc22020-09-03 10:01:14 +02001318 struct vc4_hdmi *vc4_hdmi;
Maxime Ripardc98c85b2020-09-03 10:01:12 +02001319 struct drm_encoder *encoder;
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001320 struct device_node *ddc_node;
1321 u32 value;
1322 int ret;
1323
Maxime Ripard3408cc22020-09-03 10:01:14 +02001324 vc4_hdmi = devm_kzalloc(dev, sizeof(*vc4_hdmi), GFP_KERNEL);
1325 if (!vc4_hdmi)
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001326 return -ENOMEM;
1327
Maxime Ripard47c167b2020-09-03 10:01:19 +02001328 dev_set_drvdata(dev, vc4_hdmi);
Maxime Ripard3408cc22020-09-03 10:01:14 +02001329 encoder = &vc4_hdmi->encoder.base.base;
Maxime Ripard7d732992020-09-03 10:01:29 +02001330 vc4_hdmi->encoder.base.type = variant->encoder_type;
Maxime Ripard3408cc22020-09-03 10:01:14 +02001331 vc4_hdmi->pdev = pdev;
Maxime Ripard33c773e2020-09-03 10:01:22 +02001332 vc4_hdmi->variant = variant;
Maxime Ripardc98c85b2020-09-03 10:01:12 +02001333
Maxime Ripard33c773e2020-09-03 10:01:22 +02001334 ret = variant->init_resources(vc4_hdmi);
1335 if (ret)
James Hilliard8f6f5e02020-05-24 19:28:59 -06001336 return ret;
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001337
Peter Chen027a6972016-07-05 10:04:54 +08001338 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 Ripard3408cc22020-09-03 10:01:14 +02001344 vc4_hdmi->ddc = of_find_i2c_adapter_by_node(ddc_node);
Peter Chen027a6972016-07-05 10:04:54 +08001345 of_node_put(ddc_node);
Maxime Ripard3408cc22020-09-03 10:01:14 +02001346 if (!vc4_hdmi->ddc) {
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001347 DRM_DEBUG("Failed to get ddc i2c adapter by node\n");
1348 return -EPROBE_DEFER;
1349 }
1350
Hans Verkuil10ee2752017-07-16 12:48:03 +02001351 /* 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 Ripard3408cc22020-09-03 10:01:14 +02001355 ret = clk_set_rate(vc4_hdmi->hsm_clock, HSM_CLOCK_FREQ);
Hans Verkuil10ee2752017-07-16 12:48:03 +02001356 if (ret) {
1357 DRM_ERROR("Failed to set HSM clock rate: %d\n", ret);
1358 goto err_put_i2c;
1359 }
1360
Maxime Ripard3408cc22020-09-03 10:01:14 +02001361 ret = clk_prepare_enable(vc4_hdmi->hsm_clock);
Hans Verkuil10ee2752017-07-16 12:48:03 +02001362 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 Anholtc8b75bc2015-03-02 13:01:12 -08001368 /* 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 Anholt0b06e0a2016-02-29 17:53:01 -08001372 enum of_gpio_flags hpd_gpio_flags;
1373
Maxime Ripard3408cc22020-09-03 10:01:14 +02001374 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 Verkuil10ee2752017-07-16 12:48:03 +02001379 goto err_unprepare_hsm;
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001380 }
Eric Anholt0b06e0a2016-02-29 17:53:01 -08001381
Maxime Ripard3408cc22020-09-03 10:01:14 +02001382 vc4_hdmi->hpd_active_low = hpd_gpio_flags & OF_GPIO_ACTIVE_LOW;
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001383 }
1384
Boris Brezillon4f6e3d62017-04-11 18:39:25 +02001385 pm_runtime_enable(dev);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001386
Maxime Ripardc98c85b2020-09-03 10:01:12 +02001387 drm_simple_encoder_init(drm, encoder, DRM_MODE_ENCODER_TMDS);
1388 drm_encoder_helper_add(encoder, &vc4_hdmi_encoder_helper_funcs);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001389
Maxime Ripard3408cc22020-09-03 10:01:14 +02001390 ret = vc4_hdmi_connector_init(drm, vc4_hdmi);
Maxime Ripardc98c85b2020-09-03 10:01:12 +02001391 if (ret)
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001392 goto err_destroy_encoder;
Maxime Ripardc98c85b2020-09-03 10:01:12 +02001393
Maxime Ripardc0791e02020-09-03 10:01:31 +02001394 ret = vc4_hdmi_cec_init(vc4_hdmi);
Hans Verkuil15b45112017-07-16 12:48:04 +02001395 if (ret)
Maxime Ripardc0791e02020-09-03 10:01:31 +02001396 goto err_destroy_conn;
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001397
Maxime Ripard3408cc22020-09-03 10:01:14 +02001398 ret = vc4_hdmi_audio_init(vc4_hdmi);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001399 if (ret)
Maxime Ripardc0791e02020-09-03 10:01:31 +02001400 goto err_free_cec;
Eric Anholtbb7d7852017-02-27 12:28:02 -08001401
Maxime Ripardb2405c92020-09-03 10:01:30 +02001402 vc4_debugfs_add_file(drm, variant->debugfs_name,
1403 vc4_hdmi_debugfs_regs,
1404 vc4_hdmi);
Eric Anholtc9be8042019-04-01 11:35:58 -07001405
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001406 return 0;
1407
Maxime Ripardc0791e02020-09-03 10:01:31 +02001408err_free_cec:
1409 vc4_hdmi_cec_exit(vc4_hdmi);
Hans Verkuil15b45112017-07-16 12:48:04 +02001410err_destroy_conn:
Maxime Ripard0532e5e2020-09-03 10:01:21 +02001411 vc4_hdmi_connector_destroy(&vc4_hdmi->connector);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001412err_destroy_encoder:
Maxime Ripardc98c85b2020-09-03 10:01:12 +02001413 drm_encoder_cleanup(encoder);
Hans Verkuil10ee2752017-07-16 12:48:03 +02001414err_unprepare_hsm:
Maxime Ripard3408cc22020-09-03 10:01:14 +02001415 clk_disable_unprepare(vc4_hdmi->hsm_clock);
Boris Brezillon4f6e3d62017-04-11 18:39:25 +02001416 pm_runtime_disable(dev);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001417err_put_i2c:
Maxime Ripard3408cc22020-09-03 10:01:14 +02001418 put_device(&vc4_hdmi->ddc->dev);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001419
1420 return ret;
1421}
1422
1423static void vc4_hdmi_unbind(struct device *dev, struct device *master,
1424 void *data)
1425{
Maxime Ripard47c167b2020-09-03 10:01:19 +02001426 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 Anholtc8b75bc2015-03-02 13:01:12 -08001448
Maxime Ripard311e3052020-09-03 10:01:23 +02001449 kfree(vc4_hdmi->hdmi_regset.regs);
1450 kfree(vc4_hdmi->hd_regset.regs);
1451
Maxime Ripardc0791e02020-09-03 10:01:31 +02001452 vc4_hdmi_cec_exit(vc4_hdmi);
Maxime Ripard0532e5e2020-09-03 10:01:21 +02001453 vc4_hdmi_connector_destroy(&vc4_hdmi->connector);
Maxime Ripard3408cc22020-09-03 10:01:14 +02001454 drm_encoder_cleanup(&vc4_hdmi->encoder.base.base);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001455
Maxime Ripard3408cc22020-09-03 10:01:14 +02001456 clk_disable_unprepare(vc4_hdmi->hsm_clock);
Boris Brezillon4f6e3d62017-04-11 18:39:25 +02001457 pm_runtime_disable(dev);
1458
Maxime Ripard3408cc22020-09-03 10:01:14 +02001459 put_device(&vc4_hdmi->ddc->dev);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001460}
1461
1462static const struct component_ops vc4_hdmi_ops = {
1463 .bind = vc4_hdmi_bind,
1464 .unbind = vc4_hdmi_unbind,
1465};
1466
1467static int vc4_hdmi_dev_probe(struct platform_device *pdev)
1468{
1469 return component_add(&pdev->dev, &vc4_hdmi_ops);
1470}
1471
1472static int vc4_hdmi_dev_remove(struct platform_device *pdev)
1473{
1474 component_del(&pdev->dev, &vc4_hdmi_ops);
1475 return 0;
1476}
1477
Maxime Ripard33c773e2020-09-03 10:01:22 +02001478static const struct vc4_hdmi_variant bcm2835_variant = {
Maxime Ripard7d732992020-09-03 10:01:29 +02001479 .encoder_type = VC4_ENCODER_TYPE_HDMI0,
Maxime Ripardb2405c92020-09-03 10:01:30 +02001480 .debugfs_name = "hdmi_regs",
Maxime Ripard234f4212020-09-03 10:01:32 +02001481 .cec_available = true,
Maxime Ripard311e3052020-09-03 10:01:23 +02001482 .registers = vc4_hdmi_fields,
1483 .num_registers = ARRAY_SIZE(vc4_hdmi_fields),
1484
Maxime Ripard33c773e2020-09-03 10:01:22 +02001485 .init_resources = vc4_hdmi_init_resources,
Maxime Ripard89f31a22020-09-03 10:01:27 +02001486 .csc_setup = vc4_hdmi_csc_setup,
Maxime Ripard9045e912020-09-03 10:01:24 +02001487 .reset = vc4_hdmi_reset,
Maxime Ripard904f6682020-09-03 10:01:28 +02001488 .set_timings = vc4_hdmi_set_timings,
Maxime Ripardc457b8a2020-09-03 10:01:25 +02001489 .phy_init = vc4_hdmi_phy_init,
1490 .phy_disable = vc4_hdmi_phy_disable,
Maxime Ripard647b9652020-09-03 10:01:26 +02001491 .phy_rng_enable = vc4_hdmi_phy_rng_enable,
1492 .phy_rng_disable = vc4_hdmi_phy_rng_disable,
Maxime Ripard33c773e2020-09-03 10:01:22 +02001493};
1494
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001495static const struct of_device_id vc4_hdmi_dt_match[] = {
Maxime Ripard33c773e2020-09-03 10:01:22 +02001496 { .compatible = "brcm,bcm2835-hdmi", .data = &bcm2835_variant },
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001497 {}
1498};
1499
1500struct 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};