blob: bf537c6d413f4cdf27f3d2b277d31f5ebe008c68 [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
58#define CEC_CLOCK_DIV (HSM_CLOCK_FREQ / CEC_CLOCK_FREQ)
59
Eric Anholtc9be8042019-04-01 11:35:58 -070060static int vc4_hdmi_debugfs_regs(struct seq_file *m, void *unused)
Eric Anholtc8b75bc2015-03-02 13:01:12 -080061{
62 struct drm_info_node *node = (struct drm_info_node *)m->private;
Maxime Ripard3408cc22020-09-03 10:01:14 +020063 struct vc4_hdmi *vc4_hdmi = node->info_ent->data;
Eric Anholt30517192019-02-20 13:03:38 -080064 struct drm_printer p = drm_seq_file_printer(m);
Eric Anholtc8b75bc2015-03-02 13:01:12 -080065
Maxime Ripard3408cc22020-09-03 10:01:14 +020066 drm_print_regset32(&p, &vc4_hdmi->hdmi_regset);
67 drm_print_regset32(&p, &vc4_hdmi->hd_regset);
Eric Anholtc8b75bc2015-03-02 13:01:12 -080068
69 return 0;
70}
Eric Anholtc8b75bc2015-03-02 13:01:12 -080071
Maxime Ripard9045e912020-09-03 10:01:24 +020072static void vc4_hdmi_reset(struct vc4_hdmi *vc4_hdmi)
73{
74 HDMI_WRITE(HDMI_M_CTL, VC4_HD_M_SW_RST);
75 udelay(1);
76 HDMI_WRITE(HDMI_M_CTL, 0);
77
78 HDMI_WRITE(HDMI_M_CTL, VC4_HD_M_ENABLE);
79
80 HDMI_WRITE(HDMI_SW_RESET_CONTROL,
81 VC4_HDMI_SW_RESET_HDMI |
82 VC4_HDMI_SW_RESET_FORMAT_DETECT);
83
84 HDMI_WRITE(HDMI_SW_RESET_CONTROL, 0);
85}
86
Eric Anholtc8b75bc2015-03-02 13:01:12 -080087static enum drm_connector_status
88vc4_hdmi_connector_detect(struct drm_connector *connector, bool force)
89{
Maxime Ripard5dfbcae2020-09-03 10:01:17 +020090 struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector);
Eric Anholtc8b75bc2015-03-02 13:01:12 -080091
Maxime Ripardb10db9a2020-09-03 10:01:16 +020092 if (vc4_hdmi->hpd_gpio) {
93 if (gpio_get_value_cansleep(vc4_hdmi->hpd_gpio) ^
94 vc4_hdmi->hpd_active_low)
Eric Anholtc8b75bc2015-03-02 13:01:12 -080095 return connector_status_connected;
Maxime Ripardb10db9a2020-09-03 10:01:16 +020096 cec_phys_addr_invalidate(vc4_hdmi->cec_adap);
Hans Verkuil15b45112017-07-16 12:48:04 +020097 return connector_status_disconnected;
Eric Anholtc8b75bc2015-03-02 13:01:12 -080098 }
99
Maxime Ripardb10db9a2020-09-03 10:01:16 +0200100 if (drm_probe_ddc(vc4_hdmi->ddc))
Eric Anholt9d44abb2016-09-14 19:21:29 +0100101 return connector_status_connected;
102
Maxime Ripard311e3052020-09-03 10:01:23 +0200103 if (HDMI_READ(HDMI_HOTPLUG) & VC4_HDMI_HOTPLUG_CONNECTED)
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800104 return connector_status_connected;
Maxime Ripardb10db9a2020-09-03 10:01:16 +0200105 cec_phys_addr_invalidate(vc4_hdmi->cec_adap);
Hans Verkuil15b45112017-07-16 12:48:04 +0200106 return connector_status_disconnected;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800107}
108
109static void vc4_hdmi_connector_destroy(struct drm_connector *connector)
110{
111 drm_connector_unregister(connector);
112 drm_connector_cleanup(connector);
113}
114
115static int vc4_hdmi_connector_get_modes(struct drm_connector *connector)
116{
Maxime Ripard5dfbcae2020-09-03 10:01:17 +0200117 struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector);
118 struct vc4_hdmi_encoder *vc4_encoder = &vc4_hdmi->encoder;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800119 int ret = 0;
120 struct edid *edid;
121
Maxime Ripard5dfbcae2020-09-03 10:01:17 +0200122 edid = drm_get_edid(connector, vc4_hdmi->ddc);
123 cec_s_phys_addr_from_edid(vc4_hdmi->cec_adap, edid);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800124 if (!edid)
125 return -ENODEV;
126
127 vc4_encoder->hdmi_monitor = drm_detect_hdmi_monitor(edid);
Eric Anholt21317b32016-09-29 15:34:43 -0700128
Daniel Vetterc555f022018-07-09 10:40:06 +0200129 drm_connector_update_edid_property(connector, edid);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800130 ret = drm_add_edid_modes(connector, edid);
Eric Anholt5afe0e62017-08-08 13:56:05 -0700131 kfree(edid);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800132
133 return ret;
134}
135
Maxime Ripard90b2df52019-06-19 12:17:53 +0200136static void vc4_hdmi_connector_reset(struct drm_connector *connector)
137{
138 drm_atomic_helper_connector_reset(connector);
139 drm_atomic_helper_connector_tv_reset(connector);
140}
141
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800142static const struct drm_connector_funcs vc4_hdmi_connector_funcs = {
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800143 .detect = vc4_hdmi_connector_detect,
Eric Anholt682e62c2016-09-28 17:30:25 -0700144 .fill_modes = drm_helper_probe_single_connector_modes,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800145 .destroy = vc4_hdmi_connector_destroy,
Maxime Ripard90b2df52019-06-19 12:17:53 +0200146 .reset = vc4_hdmi_connector_reset,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800147 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
148 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
149};
150
151static const struct drm_connector_helper_funcs vc4_hdmi_connector_helper_funcs = {
152 .get_modes = vc4_hdmi_connector_get_modes,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800153};
154
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200155static int vc4_hdmi_connector_init(struct drm_device *dev,
Maxime Ripardb052e702020-09-03 10:01:13 +0200156 struct vc4_hdmi *vc4_hdmi)
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800157{
Maxime Ripard0532e5e2020-09-03 10:01:21 +0200158 struct drm_connector *connector = &vc4_hdmi->connector;
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200159 struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
Boris Brezillondb999532018-12-06 15:24:39 +0100160 int ret;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800161
Andrzej Pietrasiewicz04a880f2020-01-02 14:22:58 +0100162 drm_connector_init_with_ddc(dev, connector,
163 &vc4_hdmi_connector_funcs,
164 DRM_MODE_CONNECTOR_HDMIA,
Maxime Ripardb052e702020-09-03 10:01:13 +0200165 vc4_hdmi->ddc);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800166 drm_connector_helper_add(connector, &vc4_hdmi_connector_helper_funcs);
167
Boris Brezillondb999532018-12-06 15:24:39 +0100168 /* Create and attach TV margin props to this connector. */
169 ret = drm_mode_create_tv_margin_properties(dev);
170 if (ret)
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200171 return ret;
Boris Brezillondb999532018-12-06 15:24:39 +0100172
173 drm_connector_attach_tv_margin_properties(connector);
174
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800175 connector->polled = (DRM_CONNECTOR_POLL_CONNECT |
176 DRM_CONNECTOR_POLL_DISCONNECT);
177
Mario Kleineracc1be12016-07-19 20:58:58 +0200178 connector->interlace_allowed = 1;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800179 connector->doublescan_allowed = 0;
180
Daniel Vettercde4c442018-07-09 10:40:07 +0200181 drm_connector_attach_encoder(connector, encoder);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800182
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200183 return 0;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800184}
185
Eric Anholt21317b32016-09-29 15:34:43 -0700186static int vc4_hdmi_stop_packet(struct drm_encoder *encoder,
187 enum hdmi_infoframe_type type)
188{
Maxime Ripard5dfbcae2020-09-03 10:01:17 +0200189 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
Eric Anholt21317b32016-09-29 15:34:43 -0700190 u32 packet_id = type - 0x80;
191
Maxime Ripard311e3052020-09-03 10:01:23 +0200192 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
193 HDMI_READ(HDMI_RAM_PACKET_CONFIG) & ~BIT(packet_id));
Eric Anholt21317b32016-09-29 15:34:43 -0700194
Maxime Ripard311e3052020-09-03 10:01:23 +0200195 return wait_for(!(HDMI_READ(HDMI_RAM_PACKET_STATUS) &
Eric Anholt21317b32016-09-29 15:34:43 -0700196 BIT(packet_id)), 100);
197}
198
199static void vc4_hdmi_write_infoframe(struct drm_encoder *encoder,
200 union hdmi_infoframe *frame)
201{
Maxime Ripard5dfbcae2020-09-03 10:01:17 +0200202 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
Eric Anholt21317b32016-09-29 15:34:43 -0700203 u32 packet_id = frame->any.type - 0x80;
Maxime Ripard311e3052020-09-03 10:01:23 +0200204 const struct vc4_hdmi_register *ram_packet_start =
205 &vc4_hdmi->variant->registers[HDMI_RAM_PACKET_START];
206 u32 packet_reg = ram_packet_start->offset + VC4_HDMI_PACKET_STRIDE * packet_id;
207 void __iomem *base = __vc4_hdmi_get_field_base(vc4_hdmi,
208 ram_packet_start->reg);
Eric Anholt21317b32016-09-29 15:34:43 -0700209 uint8_t buffer[VC4_HDMI_PACKET_STRIDE];
210 ssize_t len, i;
211 int ret;
212
Maxime Ripard311e3052020-09-03 10:01:23 +0200213 WARN_ONCE(!(HDMI_READ(HDMI_RAM_PACKET_CONFIG) &
Eric Anholt21317b32016-09-29 15:34:43 -0700214 VC4_HDMI_RAM_PACKET_ENABLE),
215 "Packet RAM has to be on to store the packet.");
216
217 len = hdmi_infoframe_pack(frame, buffer, sizeof(buffer));
218 if (len < 0)
219 return;
220
221 ret = vc4_hdmi_stop_packet(encoder, frame->any.type);
222 if (ret) {
223 DRM_ERROR("Failed to wait for infoframe to go idle: %d\n", ret);
224 return;
225 }
226
227 for (i = 0; i < len; i += 7) {
Maxime Ripard311e3052020-09-03 10:01:23 +0200228 writel(buffer[i + 0] << 0 |
229 buffer[i + 1] << 8 |
230 buffer[i + 2] << 16,
231 base + packet_reg);
Eric Anholt21317b32016-09-29 15:34:43 -0700232 packet_reg += 4;
233
Maxime Ripard311e3052020-09-03 10:01:23 +0200234 writel(buffer[i + 3] << 0 |
235 buffer[i + 4] << 8 |
236 buffer[i + 5] << 16 |
237 buffer[i + 6] << 24,
238 base + packet_reg);
Eric Anholt21317b32016-09-29 15:34:43 -0700239 packet_reg += 4;
240 }
241
Maxime Ripard311e3052020-09-03 10:01:23 +0200242 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
243 HDMI_READ(HDMI_RAM_PACKET_CONFIG) | BIT(packet_id));
244 ret = wait_for((HDMI_READ(HDMI_RAM_PACKET_STATUS) &
Eric Anholt21317b32016-09-29 15:34:43 -0700245 BIT(packet_id)), 100);
246 if (ret)
247 DRM_ERROR("Failed to wait for infoframe to start: %d\n", ret);
248}
249
250static void vc4_hdmi_set_avi_infoframe(struct drm_encoder *encoder)
251{
Maxime Ripard5dfbcae2020-09-03 10:01:17 +0200252 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
Eric Anholt21317b32016-09-29 15:34:43 -0700253 struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
Maxime Ripard0532e5e2020-09-03 10:01:21 +0200254 struct drm_connector *connector = &vc4_hdmi->connector;
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200255 struct drm_connector_state *cstate = connector->state;
Eric Anholt21317b32016-09-29 15:34:43 -0700256 struct drm_crtc *crtc = encoder->crtc;
257 const struct drm_display_mode *mode = &crtc->state->adjusted_mode;
258 union hdmi_infoframe frame;
259 int ret;
260
Ville Syrjälä13d0add2019-01-08 19:28:25 +0200261 ret = drm_hdmi_avi_infoframe_from_display_mode(&frame.avi,
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200262 connector, mode);
Eric Anholt21317b32016-09-29 15:34:43 -0700263 if (ret < 0) {
264 DRM_ERROR("couldn't fill AVI infoframe\n");
265 return;
266 }
267
Ville Syrjälä13d0add2019-01-08 19:28:25 +0200268 drm_hdmi_avi_infoframe_quant_range(&frame.avi,
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200269 connector, mode,
Ville Syrjäläa2ce26f2017-01-11 14:57:23 +0200270 vc4_encoder->limited_rgb_range ?
271 HDMI_QUANTIZATION_RANGE_LIMITED :
Ville Syrjälä1581b2d2019-01-08 19:28:28 +0200272 HDMI_QUANTIZATION_RANGE_FULL);
Eric Anholt21317b32016-09-29 15:34:43 -0700273
Ville Syrjäläcb876372019-10-08 19:48:14 +0300274 drm_hdmi_avi_infoframe_bars(&frame.avi, cstate);
Boris Brezillondb999532018-12-06 15:24:39 +0100275
Eric Anholt21317b32016-09-29 15:34:43 -0700276 vc4_hdmi_write_infoframe(encoder, &frame);
277}
278
279static void vc4_hdmi_set_spd_infoframe(struct drm_encoder *encoder)
280{
281 union hdmi_infoframe frame;
282 int ret;
283
284 ret = hdmi_spd_infoframe_init(&frame.spd, "Broadcom", "Videocore");
285 if (ret < 0) {
286 DRM_ERROR("couldn't fill SPD infoframe\n");
287 return;
288 }
289
290 frame.spd.sdi = HDMI_SPD_SDI_PC;
291
292 vc4_hdmi_write_infoframe(encoder, &frame);
293}
294
Eric Anholtbb7d7852017-02-27 12:28:02 -0800295static void vc4_hdmi_set_audio_infoframe(struct drm_encoder *encoder)
296{
Maxime Ripard5dfbcae2020-09-03 10:01:17 +0200297 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800298 union hdmi_infoframe frame;
299 int ret;
300
301 ret = hdmi_audio_infoframe_init(&frame.audio);
302
303 frame.audio.coding_type = HDMI_AUDIO_CODING_TYPE_STREAM;
304 frame.audio.sample_frequency = HDMI_AUDIO_SAMPLE_FREQUENCY_STREAM;
305 frame.audio.sample_size = HDMI_AUDIO_SAMPLE_SIZE_STREAM;
Maxime Ripard3408cc22020-09-03 10:01:14 +0200306 frame.audio.channels = vc4_hdmi->audio.channels;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800307
308 vc4_hdmi_write_infoframe(encoder, &frame);
309}
310
Eric Anholt21317b32016-09-29 15:34:43 -0700311static void vc4_hdmi_set_infoframes(struct drm_encoder *encoder)
312{
313 vc4_hdmi_set_avi_infoframe(encoder);
314 vc4_hdmi_set_spd_infoframe(encoder);
315}
316
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200317static void vc4_hdmi_encoder_disable(struct drm_encoder *encoder)
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800318{
Maxime Ripard5dfbcae2020-09-03 10:01:17 +0200319 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200320 int ret;
321
Maxime Ripard311e3052020-09-03 10:01:23 +0200322 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG, 0);
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200323
Maxime Ripardc457b8a2020-09-03 10:01:25 +0200324 if (vc4_hdmi->variant->phy_disable)
325 vc4_hdmi->variant->phy_disable(vc4_hdmi);
326
Maxime Ripard311e3052020-09-03 10:01:23 +0200327 HDMI_WRITE(HDMI_VID_CTL,
328 HDMI_READ(HDMI_VID_CTL) & ~VC4_HD_VID_CTL_ENABLE);
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200329
Maxime Ripard3408cc22020-09-03 10:01:14 +0200330 clk_disable_unprepare(vc4_hdmi->pixel_clock);
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200331
Maxime Ripard3408cc22020-09-03 10:01:14 +0200332 ret = pm_runtime_put(&vc4_hdmi->pdev->dev);
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200333 if (ret < 0)
334 DRM_ERROR("Failed to release power domain: %d\n", ret);
335}
336
Maxime Ripard89f31a22020-09-03 10:01:27 +0200337static void vc4_hdmi_csc_setup(struct vc4_hdmi *vc4_hdmi, bool enable)
338{
339 u32 csc_ctl;
340
341 csc_ctl = VC4_SET_FIELD(VC4_HD_CSC_CTL_ORDER_BGR,
342 VC4_HD_CSC_CTL_ORDER);
343
344 if (enable) {
345 /* CEA VICs other than #1 requre limited range RGB
346 * output unless overridden by an AVI infoframe.
347 * Apply a colorspace conversion to squash 0-255 down
348 * to 16-235. The matrix here is:
349 *
350 * [ 0 0 0.8594 16]
351 * [ 0 0.8594 0 16]
352 * [ 0.8594 0 0 16]
353 * [ 0 0 0 1]
354 */
355 csc_ctl |= VC4_HD_CSC_CTL_ENABLE;
356 csc_ctl |= VC4_HD_CSC_CTL_RGB2YCC;
357 csc_ctl |= VC4_SET_FIELD(VC4_HD_CSC_CTL_MODE_CUSTOM,
358 VC4_HD_CSC_CTL_MODE);
359
360 HDMI_WRITE(HDMI_CSC_12_11, (0x000 << 16) | 0x000);
361 HDMI_WRITE(HDMI_CSC_14_13, (0x100 << 16) | 0x6e0);
362 HDMI_WRITE(HDMI_CSC_22_21, (0x6e0 << 16) | 0x000);
363 HDMI_WRITE(HDMI_CSC_24_23, (0x100 << 16) | 0x000);
364 HDMI_WRITE(HDMI_CSC_32_31, (0x000 << 16) | 0x6e0);
365 HDMI_WRITE(HDMI_CSC_34_33, (0x100 << 16) | 0x000);
366 }
367
368 /* The RGB order applies even when CSC is disabled. */
369 HDMI_WRITE(HDMI_CSC_CTL, csc_ctl);
370}
371
Maxime Ripard904f6682020-09-03 10:01:28 +0200372static void vc4_hdmi_set_timings(struct vc4_hdmi *vc4_hdmi,
373 struct drm_display_mode *mode)
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200374{
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800375 bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
376 bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
Eric Anholt682e62c2016-09-28 17:30:25 -0700377 bool interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE;
Eric Anholtdfccd932016-09-29 15:34:44 -0700378 u32 pixel_rep = (mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1;
Eric Anholt682e62c2016-09-28 17:30:25 -0700379 u32 verta = (VC4_SET_FIELD(mode->crtc_vsync_end - mode->crtc_vsync_start,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800380 VC4_HDMI_VERTA_VSP) |
Eric Anholt682e62c2016-09-28 17:30:25 -0700381 VC4_SET_FIELD(mode->crtc_vsync_start - mode->crtc_vdisplay,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800382 VC4_HDMI_VERTA_VFP) |
Eric Anholt682e62c2016-09-28 17:30:25 -0700383 VC4_SET_FIELD(mode->crtc_vdisplay, VC4_HDMI_VERTA_VAL));
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800384 u32 vertb = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) |
Eric Anholt682e62c2016-09-28 17:30:25 -0700385 VC4_SET_FIELD(mode->crtc_vtotal - mode->crtc_vsync_end,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800386 VC4_HDMI_VERTB_VBP));
Eric Anholt682e62c2016-09-28 17:30:25 -0700387 u32 vertb_even = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) |
388 VC4_SET_FIELD(mode->crtc_vtotal -
389 mode->crtc_vsync_end -
390 interlaced,
391 VC4_HDMI_VERTB_VBP));
Maxime Ripard904f6682020-09-03 10:01:28 +0200392
393 HDMI_WRITE(HDMI_HORZA,
394 (vsync_pos ? VC4_HDMI_HORZA_VPOS : 0) |
395 (hsync_pos ? VC4_HDMI_HORZA_HPOS : 0) |
396 VC4_SET_FIELD(mode->hdisplay * pixel_rep,
397 VC4_HDMI_HORZA_HAP));
398
399 HDMI_WRITE(HDMI_HORZB,
400 VC4_SET_FIELD((mode->htotal -
401 mode->hsync_end) * pixel_rep,
402 VC4_HDMI_HORZB_HBP) |
403 VC4_SET_FIELD((mode->hsync_end -
404 mode->hsync_start) * pixel_rep,
405 VC4_HDMI_HORZB_HSP) |
406 VC4_SET_FIELD((mode->hsync_start -
407 mode->hdisplay) * pixel_rep,
408 VC4_HDMI_HORZB_HFP));
409
410 HDMI_WRITE(HDMI_VERTA0, verta);
411 HDMI_WRITE(HDMI_VERTA1, verta);
412
413 HDMI_WRITE(HDMI_VERTB0, vertb_even);
414 HDMI_WRITE(HDMI_VERTB1, vertb);
415
416 HDMI_WRITE(HDMI_VID_CTL,
417 (vsync_pos ? 0 : VC4_HD_VID_CTL_VSYNC_LOW) |
418 (hsync_pos ? 0 : VC4_HD_VID_CTL_HSYNC_LOW));
419}
420
421static void vc4_hdmi_encoder_enable(struct drm_encoder *encoder)
422{
423 struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
424 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
425 struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
426 bool debug_dump_regs = false;
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200427 int ret;
428
Maxime Ripard3408cc22020-09-03 10:01:14 +0200429 ret = pm_runtime_get_sync(&vc4_hdmi->pdev->dev);
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200430 if (ret < 0) {
431 DRM_ERROR("Failed to retain power domain: %d\n", ret);
432 return;
433 }
434
Maxime Ripard3408cc22020-09-03 10:01:14 +0200435 ret = clk_set_rate(vc4_hdmi->pixel_clock,
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200436 mode->clock * 1000 *
437 ((mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1));
438 if (ret) {
439 DRM_ERROR("Failed to set pixel clock rate: %d\n", ret);
440 return;
441 }
442
Maxime Ripard3408cc22020-09-03 10:01:14 +0200443 ret = clk_prepare_enable(vc4_hdmi->pixel_clock);
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200444 if (ret) {
445 DRM_ERROR("Failed to turn on pixel clock: %d\n", ret);
446 return;
447 }
448
Maxime Ripard9045e912020-09-03 10:01:24 +0200449 if (vc4_hdmi->variant->reset)
450 vc4_hdmi->variant->reset(vc4_hdmi);
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200451
Maxime Ripardc457b8a2020-09-03 10:01:25 +0200452 if (vc4_hdmi->variant->phy_init)
453 vc4_hdmi->variant->phy_init(vc4_hdmi, mode);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800454
455 if (debug_dump_regs) {
Maxime Ripard3408cc22020-09-03 10:01:14 +0200456 struct drm_printer p = drm_info_printer(&vc4_hdmi->pdev->dev);
Eric Anholt30517192019-02-20 13:03:38 -0800457
Maxime Ripard3408cc22020-09-03 10:01:14 +0200458 dev_info(&vc4_hdmi->pdev->dev, "HDMI regs before:\n");
459 drm_print_regset32(&p, &vc4_hdmi->hdmi_regset);
460 drm_print_regset32(&p, &vc4_hdmi->hd_regset);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800461 }
462
Maxime Ripard311e3052020-09-03 10:01:23 +0200463 HDMI_WRITE(HDMI_VID_CTL, 0);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800464
Maxime Ripard311e3052020-09-03 10:01:23 +0200465 HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
466 HDMI_READ(HDMI_SCHEDULER_CONTROL) |
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800467 VC4_HDMI_SCHEDULER_CONTROL_MANUAL_FORMAT |
468 VC4_HDMI_SCHEDULER_CONTROL_IGNORE_VSYNC_PREDICTS);
469
Maxime Ripard904f6682020-09-03 10:01:28 +0200470 if (vc4_hdmi->variant->set_timings)
471 vc4_hdmi->variant->set_timings(vc4_hdmi, mode);
Eric Anholt6e1cbba2016-09-16 10:59:45 +0100472
Ville Syrjäläc8127cf02017-01-11 16:18:35 +0200473 if (vc4_encoder->hdmi_monitor &&
Maxime Ripard89f31a22020-09-03 10:01:27 +0200474 drm_default_rgb_quant_range(mode) == HDMI_QUANTIZATION_RANGE_LIMITED) {
475 if (vc4_hdmi->variant->csc_setup)
476 vc4_hdmi->variant->csc_setup(vc4_hdmi, true);
Eric Anholt6e1cbba2016-09-16 10:59:45 +0100477
Eric Anholt21317b32016-09-29 15:34:43 -0700478 vc4_encoder->limited_rgb_range = true;
479 } else {
Maxime Ripard89f31a22020-09-03 10:01:27 +0200480 if (vc4_hdmi->variant->csc_setup)
481 vc4_hdmi->variant->csc_setup(vc4_hdmi, false);
482
Eric Anholt21317b32016-09-29 15:34:43 -0700483 vc4_encoder->limited_rgb_range = false;
Eric Anholt6e1cbba2016-09-16 10:59:45 +0100484 }
485
Maxime Ripard311e3052020-09-03 10:01:23 +0200486 HDMI_WRITE(HDMI_FIFO_CTL, VC4_HDMI_FIFO_CTL_MASTER_SLAVE_N);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800487
488 if (debug_dump_regs) {
Maxime Ripard3408cc22020-09-03 10:01:14 +0200489 struct drm_printer p = drm_info_printer(&vc4_hdmi->pdev->dev);
Eric Anholt30517192019-02-20 13:03:38 -0800490
Maxime Ripard3408cc22020-09-03 10:01:14 +0200491 dev_info(&vc4_hdmi->pdev->dev, "HDMI regs after:\n");
492 drm_print_regset32(&p, &vc4_hdmi->hdmi_regset);
493 drm_print_regset32(&p, &vc4_hdmi->hd_regset);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800494 }
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800495
Maxime Ripard311e3052020-09-03 10:01:23 +0200496 HDMI_WRITE(HDMI_VID_CTL,
497 HDMI_READ(HDMI_VID_CTL) |
498 VC4_HD_VID_CTL_ENABLE |
499 VC4_HD_VID_CTL_UNDERFLOW_ENABLE |
500 VC4_HD_VID_CTL_FRAME_COUNTER_RESET);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800501
502 if (vc4_encoder->hdmi_monitor) {
Maxime Ripard311e3052020-09-03 10:01:23 +0200503 HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
504 HDMI_READ(HDMI_SCHEDULER_CONTROL) |
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800505 VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);
506
Maxime Ripard311e3052020-09-03 10:01:23 +0200507 ret = wait_for(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
Eric Anholt2b29bf12016-09-28 17:21:05 -0700508 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE, 1000);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800509 WARN_ONCE(ret, "Timeout waiting for "
510 "VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
511 } else {
Maxime Ripard311e3052020-09-03 10:01:23 +0200512 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
513 HDMI_READ(HDMI_RAM_PACKET_CONFIG) &
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800514 ~(VC4_HDMI_RAM_PACKET_ENABLE));
Maxime Ripard311e3052020-09-03 10:01:23 +0200515 HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
516 HDMI_READ(HDMI_SCHEDULER_CONTROL) &
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800517 ~VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);
518
Maxime Ripard311e3052020-09-03 10:01:23 +0200519 ret = wait_for(!(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
Eric Anholt2b29bf12016-09-28 17:21:05 -0700520 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE), 1000);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800521 WARN_ONCE(ret, "Timeout waiting for "
522 "!VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
523 }
524
525 if (vc4_encoder->hdmi_monitor) {
526 u32 drift;
527
Maxime Ripard311e3052020-09-03 10:01:23 +0200528 WARN_ON(!(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800529 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE));
Maxime Ripard311e3052020-09-03 10:01:23 +0200530 HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
531 HDMI_READ(HDMI_SCHEDULER_CONTROL) |
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800532 VC4_HDMI_SCHEDULER_CONTROL_VERT_ALWAYS_KEEPOUT);
533
Maxime Ripard311e3052020-09-03 10:01:23 +0200534 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
Eric Anholt21317b32016-09-29 15:34:43 -0700535 VC4_HDMI_RAM_PACKET_ENABLE);
536
537 vc4_hdmi_set_infoframes(encoder);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800538
Maxime Ripard311e3052020-09-03 10:01:23 +0200539 drift = HDMI_READ(HDMI_FIFO_CTL);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800540 drift &= VC4_HDMI_FIFO_VALID_WRITE_MASK;
541
Maxime Ripard311e3052020-09-03 10:01:23 +0200542 HDMI_WRITE(HDMI_FIFO_CTL,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800543 drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
Maxime Ripard311e3052020-09-03 10:01:23 +0200544 HDMI_WRITE(HDMI_FIFO_CTL,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800545 drift | VC4_HDMI_FIFO_CTL_RECENTER);
Stefan Wahrend8eb9de2018-02-24 13:38:14 +0100546 usleep_range(1000, 1100);
Maxime Ripard311e3052020-09-03 10:01:23 +0200547 HDMI_WRITE(HDMI_FIFO_CTL,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800548 drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
Maxime Ripard311e3052020-09-03 10:01:23 +0200549 HDMI_WRITE(HDMI_FIFO_CTL,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800550 drift | VC4_HDMI_FIFO_CTL_RECENTER);
551
Maxime Ripard311e3052020-09-03 10:01:23 +0200552 ret = wait_for(HDMI_READ(HDMI_FIFO_CTL) &
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800553 VC4_HDMI_FIFO_CTL_RECENTER_DONE, 1);
554 WARN_ONCE(ret, "Timeout waiting for "
555 "VC4_HDMI_FIFO_CTL_RECENTER_DONE");
556 }
557}
558
Eric Anholt32e823c2017-09-20 15:59:34 -0700559static enum drm_mode_status
560vc4_hdmi_encoder_mode_valid(struct drm_encoder *crtc,
561 const struct drm_display_mode *mode)
562{
Nicolas Saenz Julienneb1e73962020-03-26 13:20:01 +0100563 /*
564 * As stated in RPi's vc4 firmware "HDMI state machine (HSM) clock must
565 * be faster than pixel clock, infinitesimally faster, tested in
566 * simulation. Otherwise, exact value is unimportant for HDMI
567 * operation." This conflicts with bcm2835's vc4 documentation, which
568 * states HSM's clock has to be at least 108% of the pixel clock.
569 *
570 * Real life tests reveal that vc4's firmware statement holds up, and
571 * users are able to use pixel clocks closer to HSM's, namely for
572 * 1920x1200@60Hz. So it was decided to have leave a 1% margin between
573 * both clocks. Which, for RPi0-3 implies a maximum pixel clock of
574 * 162MHz.
575 *
576 * Additionally, the AXI clock needs to be at least 25% of
577 * pixel clock, but HSM ends up being the limiting factor.
Eric Anholt32e823c2017-09-20 15:59:34 -0700578 */
Nicolas Saenz Julienneb1e73962020-03-26 13:20:01 +0100579 if (mode->clock > HSM_CLOCK_FREQ / (1000 * 101 / 100))
Eric Anholt32e823c2017-09-20 15:59:34 -0700580 return MODE_CLOCK_HIGH;
581
582 return MODE_OK;
583}
584
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800585static const struct drm_encoder_helper_funcs vc4_hdmi_encoder_helper_funcs = {
Eric Anholt32e823c2017-09-20 15:59:34 -0700586 .mode_valid = vc4_hdmi_encoder_mode_valid,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800587 .disable = vc4_hdmi_encoder_disable,
588 .enable = vc4_hdmi_encoder_enable,
589};
590
Eric Anholtbb7d7852017-02-27 12:28:02 -0800591/* HDMI audio codec callbacks */
Maxime Ripard3408cc22020-09-03 10:01:14 +0200592static void vc4_hdmi_audio_set_mai_clock(struct vc4_hdmi *vc4_hdmi)
Eric Anholtbb7d7852017-02-27 12:28:02 -0800593{
Maxime Ripard3408cc22020-09-03 10:01:14 +0200594 u32 hsm_clock = clk_get_rate(vc4_hdmi->hsm_clock);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800595 unsigned long n, m;
596
Maxime Ripard3408cc22020-09-03 10:01:14 +0200597 rational_best_approximation(hsm_clock, vc4_hdmi->audio.samplerate,
Eric Anholtbb7d7852017-02-27 12:28:02 -0800598 VC4_HD_MAI_SMP_N_MASK >>
599 VC4_HD_MAI_SMP_N_SHIFT,
600 (VC4_HD_MAI_SMP_M_MASK >>
601 VC4_HD_MAI_SMP_M_SHIFT) + 1,
602 &n, &m);
603
Maxime Ripard311e3052020-09-03 10:01:23 +0200604 HDMI_WRITE(HDMI_MAI_SMP,
605 VC4_SET_FIELD(n, VC4_HD_MAI_SMP_N) |
606 VC4_SET_FIELD(m - 1, VC4_HD_MAI_SMP_M));
Eric Anholtbb7d7852017-02-27 12:28:02 -0800607}
608
Maxime Ripard3408cc22020-09-03 10:01:14 +0200609static void vc4_hdmi_set_n_cts(struct vc4_hdmi *vc4_hdmi)
Eric Anholtbb7d7852017-02-27 12:28:02 -0800610{
Maxime Ripard3408cc22020-09-03 10:01:14 +0200611 struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800612 struct drm_crtc *crtc = encoder->crtc;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800613 const struct drm_display_mode *mode = &crtc->state->adjusted_mode;
Maxime Ripard3408cc22020-09-03 10:01:14 +0200614 u32 samplerate = vc4_hdmi->audio.samplerate;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800615 u32 n, cts;
616 u64 tmp;
617
618 n = 128 * samplerate / 1000;
619 tmp = (u64)(mode->clock * 1000) * n;
620 do_div(tmp, 128 * samplerate);
621 cts = tmp;
622
Maxime Ripard311e3052020-09-03 10:01:23 +0200623 HDMI_WRITE(HDMI_CRP_CFG,
Eric Anholtbb7d7852017-02-27 12:28:02 -0800624 VC4_HDMI_CRP_CFG_EXTERNAL_CTS_EN |
625 VC4_SET_FIELD(n, VC4_HDMI_CRP_CFG_N));
626
627 /*
628 * We could get slightly more accurate clocks in some cases by
629 * providing a CTS_1 value. The two CTS values are alternated
630 * between based on the period fields
631 */
Maxime Ripard311e3052020-09-03 10:01:23 +0200632 HDMI_WRITE(HDMI_CTS_0, cts);
633 HDMI_WRITE(HDMI_CTS_1, cts);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800634}
635
636static inline struct vc4_hdmi *dai_to_hdmi(struct snd_soc_dai *dai)
637{
638 struct snd_soc_card *card = snd_soc_dai_get_drvdata(dai);
639
640 return snd_soc_card_get_drvdata(card);
641}
642
643static int vc4_hdmi_audio_startup(struct snd_pcm_substream *substream,
644 struct snd_soc_dai *dai)
645{
Maxime Ripard3408cc22020-09-03 10:01:14 +0200646 struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai);
647 struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
Maxime Ripard0532e5e2020-09-03 10:01:21 +0200648 struct drm_connector *connector = &vc4_hdmi->connector;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800649 int ret;
650
Maxime Ripard3408cc22020-09-03 10:01:14 +0200651 if (vc4_hdmi->audio.substream && vc4_hdmi->audio.substream != substream)
Eric Anholtbb7d7852017-02-27 12:28:02 -0800652 return -EINVAL;
653
Maxime Ripard3408cc22020-09-03 10:01:14 +0200654 vc4_hdmi->audio.substream = substream;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800655
656 /*
657 * If the HDMI encoder hasn't probed, or the encoder is
658 * currently in DVI mode, treat the codec dai as missing.
659 */
Maxime Ripard311e3052020-09-03 10:01:23 +0200660 if (!encoder->crtc || !(HDMI_READ(HDMI_RAM_PACKET_CONFIG) &
Eric Anholtbb7d7852017-02-27 12:28:02 -0800661 VC4_HDMI_RAM_PACKET_ENABLE))
662 return -ENODEV;
663
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200664 ret = snd_pcm_hw_constraint_eld(substream->runtime, connector->eld);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800665 if (ret)
666 return ret;
667
668 return 0;
669}
670
671static int vc4_hdmi_audio_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
672{
673 return 0;
674}
675
Maxime Ripard3408cc22020-09-03 10:01:14 +0200676static void vc4_hdmi_audio_reset(struct vc4_hdmi *vc4_hdmi)
Eric Anholtbb7d7852017-02-27 12:28:02 -0800677{
Maxime Ripard3408cc22020-09-03 10:01:14 +0200678 struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
Maxime Ripard3408cc22020-09-03 10:01:14 +0200679 struct device *dev = &vc4_hdmi->pdev->dev;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800680 int ret;
681
682 ret = vc4_hdmi_stop_packet(encoder, HDMI_INFOFRAME_TYPE_AUDIO);
683 if (ret)
684 dev_err(dev, "Failed to stop audio infoframe: %d\n", ret);
685
Maxime Ripard311e3052020-09-03 10:01:23 +0200686 HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_RESET);
687 HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_ERRORF);
688 HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_FLUSH);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800689}
690
691static void vc4_hdmi_audio_shutdown(struct snd_pcm_substream *substream,
692 struct snd_soc_dai *dai)
693{
Maxime Ripard3408cc22020-09-03 10:01:14 +0200694 struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800695
Maxime Ripard3408cc22020-09-03 10:01:14 +0200696 if (substream != vc4_hdmi->audio.substream)
Eric Anholtbb7d7852017-02-27 12:28:02 -0800697 return;
698
Maxime Ripard3408cc22020-09-03 10:01:14 +0200699 vc4_hdmi_audio_reset(vc4_hdmi);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800700
Maxime Ripard3408cc22020-09-03 10:01:14 +0200701 vc4_hdmi->audio.substream = NULL;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800702}
703
704/* HDMI audio codec callbacks */
705static int vc4_hdmi_audio_hw_params(struct snd_pcm_substream *substream,
706 struct snd_pcm_hw_params *params,
707 struct snd_soc_dai *dai)
708{
Maxime Ripard3408cc22020-09-03 10:01:14 +0200709 struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai);
Maxime Ripard3408cc22020-09-03 10:01:14 +0200710 struct device *dev = &vc4_hdmi->pdev->dev;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800711 u32 audio_packet_config, channel_mask;
712 u32 channel_map, i;
713
Maxime Ripard3408cc22020-09-03 10:01:14 +0200714 if (substream != vc4_hdmi->audio.substream)
Eric Anholtbb7d7852017-02-27 12:28:02 -0800715 return -EINVAL;
716
717 dev_dbg(dev, "%s: %u Hz, %d bit, %d channels\n", __func__,
718 params_rate(params), params_width(params),
719 params_channels(params));
720
Maxime Ripard3408cc22020-09-03 10:01:14 +0200721 vc4_hdmi->audio.channels = params_channels(params);
722 vc4_hdmi->audio.samplerate = params_rate(params);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800723
Maxime Ripard311e3052020-09-03 10:01:23 +0200724 HDMI_WRITE(HDMI_MAI_CTL,
725 VC4_HD_MAI_CTL_RESET |
726 VC4_HD_MAI_CTL_FLUSH |
727 VC4_HD_MAI_CTL_DLATE |
728 VC4_HD_MAI_CTL_ERRORE |
729 VC4_HD_MAI_CTL_ERRORF);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800730
Maxime Ripard3408cc22020-09-03 10:01:14 +0200731 vc4_hdmi_audio_set_mai_clock(vc4_hdmi);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800732
733 audio_packet_config =
734 VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_SAMPLE_FLAT |
735 VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_INACTIVE_CHANNELS |
736 VC4_SET_FIELD(0xf, VC4_HDMI_AUDIO_PACKET_B_FRAME_IDENTIFIER);
737
Maxime Ripard3408cc22020-09-03 10:01:14 +0200738 channel_mask = GENMASK(vc4_hdmi->audio.channels - 1, 0);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800739 audio_packet_config |= VC4_SET_FIELD(channel_mask,
740 VC4_HDMI_AUDIO_PACKET_CEA_MASK);
741
742 /* Set the MAI threshold. This logic mimics the firmware's. */
Maxime Ripard3408cc22020-09-03 10:01:14 +0200743 if (vc4_hdmi->audio.samplerate > 96000) {
Maxime Ripard311e3052020-09-03 10:01:23 +0200744 HDMI_WRITE(HDMI_MAI_THR,
745 VC4_SET_FIELD(0x12, VC4_HD_MAI_THR_DREQHIGH) |
746 VC4_SET_FIELD(0x12, VC4_HD_MAI_THR_DREQLOW));
Maxime Ripard3408cc22020-09-03 10:01:14 +0200747 } else if (vc4_hdmi->audio.samplerate > 48000) {
Maxime Ripard311e3052020-09-03 10:01:23 +0200748 HDMI_WRITE(HDMI_MAI_THR,
749 VC4_SET_FIELD(0x14, VC4_HD_MAI_THR_DREQHIGH) |
750 VC4_SET_FIELD(0x12, VC4_HD_MAI_THR_DREQLOW));
Eric Anholtbb7d7852017-02-27 12:28:02 -0800751 } else {
Maxime Ripard311e3052020-09-03 10:01:23 +0200752 HDMI_WRITE(HDMI_MAI_THR,
753 VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_PANICHIGH) |
754 VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_PANICLOW) |
755 VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_DREQHIGH) |
756 VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_DREQLOW));
Eric Anholtbb7d7852017-02-27 12:28:02 -0800757 }
758
Maxime Ripard311e3052020-09-03 10:01:23 +0200759 HDMI_WRITE(HDMI_MAI_CONFIG,
Eric Anholtbb7d7852017-02-27 12:28:02 -0800760 VC4_HDMI_MAI_CONFIG_BIT_REVERSE |
761 VC4_SET_FIELD(channel_mask, VC4_HDMI_MAI_CHANNEL_MASK));
762
763 channel_map = 0;
764 for (i = 0; i < 8; i++) {
765 if (channel_mask & BIT(i))
766 channel_map |= i << (3 * i);
767 }
768
Maxime Ripard311e3052020-09-03 10:01:23 +0200769 HDMI_WRITE(HDMI_MAI_CHANNEL_MAP, channel_map);
770 HDMI_WRITE(HDMI_AUDIO_PACKET_CONFIG, audio_packet_config);
Maxime Ripard3408cc22020-09-03 10:01:14 +0200771 vc4_hdmi_set_n_cts(vc4_hdmi);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800772
773 return 0;
774}
775
776static int vc4_hdmi_audio_trigger(struct snd_pcm_substream *substream, int cmd,
777 struct snd_soc_dai *dai)
778{
Maxime Ripard3408cc22020-09-03 10:01:14 +0200779 struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai);
780 struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800781
782 switch (cmd) {
783 case SNDRV_PCM_TRIGGER_START:
784 vc4_hdmi_set_audio_infoframe(encoder);
Maxime Ripard647b9652020-09-03 10:01:26 +0200785
786 if (vc4_hdmi->variant->phy_rng_enable)
787 vc4_hdmi->variant->phy_rng_enable(vc4_hdmi);
Maxime Ripard311e3052020-09-03 10:01:23 +0200788
789 HDMI_WRITE(HDMI_MAI_CTL,
790 VC4_SET_FIELD(vc4_hdmi->audio.channels,
791 VC4_HD_MAI_CTL_CHNUM) |
792 VC4_HD_MAI_CTL_ENABLE);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800793 break;
794 case SNDRV_PCM_TRIGGER_STOP:
Maxime Ripard311e3052020-09-03 10:01:23 +0200795 HDMI_WRITE(HDMI_MAI_CTL,
796 VC4_HD_MAI_CTL_DLATE |
797 VC4_HD_MAI_CTL_ERRORE |
798 VC4_HD_MAI_CTL_ERRORF);
Maxime Ripard647b9652020-09-03 10:01:26 +0200799
800 if (vc4_hdmi->variant->phy_rng_disable)
801 vc4_hdmi->variant->phy_rng_disable(vc4_hdmi);
802
Eric Anholtbb7d7852017-02-27 12:28:02 -0800803 break;
804 default:
805 break;
806 }
807
808 return 0;
809}
810
811static inline struct vc4_hdmi *
812snd_component_to_hdmi(struct snd_soc_component *component)
813{
814 struct snd_soc_card *card = snd_soc_component_get_drvdata(component);
815
816 return snd_soc_card_get_drvdata(card);
817}
818
819static int vc4_hdmi_audio_eld_ctl_info(struct snd_kcontrol *kcontrol,
820 struct snd_ctl_elem_info *uinfo)
821{
822 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Maxime Ripard3408cc22020-09-03 10:01:14 +0200823 struct vc4_hdmi *vc4_hdmi = snd_component_to_hdmi(component);
Maxime Ripard0532e5e2020-09-03 10:01:21 +0200824 struct drm_connector *connector = &vc4_hdmi->connector;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800825
826 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200827 uinfo->count = sizeof(connector->eld);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800828
829 return 0;
830}
831
832static int vc4_hdmi_audio_eld_ctl_get(struct snd_kcontrol *kcontrol,
833 struct snd_ctl_elem_value *ucontrol)
834{
835 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Maxime Ripard3408cc22020-09-03 10:01:14 +0200836 struct vc4_hdmi *vc4_hdmi = snd_component_to_hdmi(component);
Maxime Ripard0532e5e2020-09-03 10:01:21 +0200837 struct drm_connector *connector = &vc4_hdmi->connector;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800838
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200839 memcpy(ucontrol->value.bytes.data, connector->eld,
840 sizeof(connector->eld));
Eric Anholtbb7d7852017-02-27 12:28:02 -0800841
842 return 0;
843}
844
845static const struct snd_kcontrol_new vc4_hdmi_audio_controls[] = {
846 {
847 .access = SNDRV_CTL_ELEM_ACCESS_READ |
848 SNDRV_CTL_ELEM_ACCESS_VOLATILE,
849 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
850 .name = "ELD",
851 .info = vc4_hdmi_audio_eld_ctl_info,
852 .get = vc4_hdmi_audio_eld_ctl_get,
853 },
854};
855
856static const struct snd_soc_dapm_widget vc4_hdmi_audio_widgets[] = {
857 SND_SOC_DAPM_OUTPUT("TX"),
858};
859
860static const struct snd_soc_dapm_route vc4_hdmi_audio_routes[] = {
861 { "TX", NULL, "Playback" },
862};
863
Kuninori Morimoto635b1c12018-01-29 04:35:04 +0000864static const struct snd_soc_component_driver vc4_hdmi_audio_component_drv = {
865 .controls = vc4_hdmi_audio_controls,
866 .num_controls = ARRAY_SIZE(vc4_hdmi_audio_controls),
867 .dapm_widgets = vc4_hdmi_audio_widgets,
868 .num_dapm_widgets = ARRAY_SIZE(vc4_hdmi_audio_widgets),
869 .dapm_routes = vc4_hdmi_audio_routes,
870 .num_dapm_routes = ARRAY_SIZE(vc4_hdmi_audio_routes),
871 .idle_bias_on = 1,
872 .use_pmdown_time = 1,
873 .endianness = 1,
874 .non_legacy_dai_naming = 1,
Eric Anholtbb7d7852017-02-27 12:28:02 -0800875};
876
877static const struct snd_soc_dai_ops vc4_hdmi_audio_dai_ops = {
878 .startup = vc4_hdmi_audio_startup,
879 .shutdown = vc4_hdmi_audio_shutdown,
880 .hw_params = vc4_hdmi_audio_hw_params,
881 .set_fmt = vc4_hdmi_audio_set_fmt,
882 .trigger = vc4_hdmi_audio_trigger,
883};
884
885static struct snd_soc_dai_driver vc4_hdmi_audio_codec_dai_drv = {
886 .name = "vc4-hdmi-hifi",
887 .playback = {
888 .stream_name = "Playback",
889 .channels_min = 2,
890 .channels_max = 8,
891 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
892 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
893 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
894 SNDRV_PCM_RATE_192000,
895 .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
896 },
897};
898
899static const struct snd_soc_component_driver vc4_hdmi_audio_cpu_dai_comp = {
900 .name = "vc4-hdmi-cpu-dai-component",
901};
902
903static int vc4_hdmi_audio_cpu_dai_probe(struct snd_soc_dai *dai)
904{
Maxime Ripard3408cc22020-09-03 10:01:14 +0200905 struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800906
Maxime Ripard3408cc22020-09-03 10:01:14 +0200907 snd_soc_dai_init_dma_data(dai, &vc4_hdmi->audio.dma_data, NULL);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800908
909 return 0;
910}
911
912static struct snd_soc_dai_driver vc4_hdmi_audio_cpu_dai_drv = {
913 .name = "vc4-hdmi-cpu-dai",
914 .probe = vc4_hdmi_audio_cpu_dai_probe,
915 .playback = {
916 .stream_name = "Playback",
917 .channels_min = 1,
918 .channels_max = 8,
919 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
920 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
921 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
922 SNDRV_PCM_RATE_192000,
923 .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
924 },
925 .ops = &vc4_hdmi_audio_dai_ops,
926};
927
928static const struct snd_dmaengine_pcm_config pcm_conf = {
929 .chan_names[SNDRV_PCM_STREAM_PLAYBACK] = "audio-rx",
930 .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
931};
932
Maxime Ripard3408cc22020-09-03 10:01:14 +0200933static int vc4_hdmi_audio_init(struct vc4_hdmi *vc4_hdmi)
Eric Anholtbb7d7852017-02-27 12:28:02 -0800934{
Maxime Ripard311e3052020-09-03 10:01:23 +0200935 const struct vc4_hdmi_register *mai_data =
936 &vc4_hdmi->variant->registers[HDMI_MAI_DATA];
Maxime Ripard3408cc22020-09-03 10:01:14 +0200937 struct snd_soc_dai_link *dai_link = &vc4_hdmi->audio.link;
938 struct snd_soc_card *card = &vc4_hdmi->audio.card;
939 struct device *dev = &vc4_hdmi->pdev->dev;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800940 const __be32 *addr;
941 int ret;
942
943 if (!of_find_property(dev->of_node, "dmas", NULL)) {
944 dev_warn(dev,
945 "'dmas' DT property is missing, no HDMI audio\n");
946 return 0;
947 }
948
Maxime Ripard311e3052020-09-03 10:01:23 +0200949 if (mai_data->reg != VC4_HD) {
950 WARN_ONCE(true, "MAI isn't in the HD block\n");
951 return -EINVAL;
952 }
953
Eric Anholtbb7d7852017-02-27 12:28:02 -0800954 /*
955 * Get the physical address of VC4_HD_MAI_DATA. We need to retrieve
956 * the bus address specified in the DT, because the physical address
957 * (the one returned by platform_get_resource()) is not appropriate
958 * for DMA transfers.
959 * This VC/MMU should probably be exposed to avoid this kind of hacks.
960 */
961 addr = of_get_address(dev->of_node, 1, NULL, NULL);
Maxime Ripard311e3052020-09-03 10:01:23 +0200962 vc4_hdmi->audio.dma_data.addr = be32_to_cpup(addr) + mai_data->offset;
Maxime Ripard3408cc22020-09-03 10:01:14 +0200963 vc4_hdmi->audio.dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
964 vc4_hdmi->audio.dma_data.maxburst = 2;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800965
966 ret = devm_snd_dmaengine_pcm_register(dev, &pcm_conf, 0);
967 if (ret) {
968 dev_err(dev, "Could not register PCM component: %d\n", ret);
969 return ret;
970 }
971
972 ret = devm_snd_soc_register_component(dev, &vc4_hdmi_audio_cpu_dai_comp,
973 &vc4_hdmi_audio_cpu_dai_drv, 1);
974 if (ret) {
975 dev_err(dev, "Could not register CPU DAI: %d\n", ret);
976 return ret;
977 }
978
Kuninori Morimoto635b1c12018-01-29 04:35:04 +0000979 /* register component and codec dai */
980 ret = devm_snd_soc_register_component(dev, &vc4_hdmi_audio_component_drv,
Eric Anholtbb7d7852017-02-27 12:28:02 -0800981 &vc4_hdmi_audio_codec_dai_drv, 1);
982 if (ret) {
Kuninori Morimoto635b1c12018-01-29 04:35:04 +0000983 dev_err(dev, "Could not register component: %d\n", ret);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800984 return ret;
985 }
986
Maxime Ripard3408cc22020-09-03 10:01:14 +0200987 dai_link->cpus = &vc4_hdmi->audio.cpu;
988 dai_link->codecs = &vc4_hdmi->audio.codec;
989 dai_link->platforms = &vc4_hdmi->audio.platform;
Kuninori Morimoto0467d8e2019-06-06 13:19:19 +0900990
991 dai_link->num_cpus = 1;
992 dai_link->num_codecs = 1;
Kuninori Morimoto8a90efd2019-06-28 10:46:14 +0900993 dai_link->num_platforms = 1;
Kuninori Morimoto0467d8e2019-06-06 13:19:19 +0900994
Eric Anholtbb7d7852017-02-27 12:28:02 -0800995 dai_link->name = "MAI";
996 dai_link->stream_name = "MAI PCM";
Kuninori Morimoto0467d8e2019-06-06 13:19:19 +0900997 dai_link->codecs->dai_name = vc4_hdmi_audio_codec_dai_drv.name;
998 dai_link->cpus->dai_name = dev_name(dev);
999 dai_link->codecs->name = dev_name(dev);
Kuninori Morimoto8a90efd2019-06-28 10:46:14 +09001000 dai_link->platforms->name = dev_name(dev);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001001
1002 card->dai_link = dai_link;
1003 card->num_links = 1;
1004 card->name = "vc4-hdmi";
1005 card->dev = dev;
1006
1007 /*
1008 * Be careful, snd_soc_register_card() calls dev_set_drvdata() and
1009 * stores a pointer to the snd card object in dev->driver_data. This
1010 * means we cannot use it for something else. The hdmi back-pointer is
1011 * now stored in card->drvdata and should be retrieved with
1012 * snd_soc_card_get_drvdata() if needed.
1013 */
Maxime Ripard3408cc22020-09-03 10:01:14 +02001014 snd_soc_card_set_drvdata(card, vc4_hdmi);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001015 ret = devm_snd_soc_register_card(dev, card);
Kuninori Morimoto635b1c12018-01-29 04:35:04 +00001016 if (ret)
Eric Anholtbb7d7852017-02-27 12:28:02 -08001017 dev_err(dev, "Could not register sound card: %d\n", ret);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001018
1019 return ret;
Eric Anholtbb7d7852017-02-27 12:28:02 -08001020
Eric Anholtbb7d7852017-02-27 12:28:02 -08001021}
1022
Hans Verkuil15b45112017-07-16 12:48:04 +02001023#ifdef CONFIG_DRM_VC4_HDMI_CEC
1024static irqreturn_t vc4_cec_irq_handler_thread(int irq, void *priv)
1025{
Maxime Ripard66bf1c32020-09-03 10:01:18 +02001026 struct vc4_hdmi *vc4_hdmi = priv;
Hans Verkuil15b45112017-07-16 12:48:04 +02001027
Maxime Ripard3408cc22020-09-03 10:01:14 +02001028 if (vc4_hdmi->cec_irq_was_rx) {
1029 if (vc4_hdmi->cec_rx_msg.len)
1030 cec_received_msg(vc4_hdmi->cec_adap,
1031 &vc4_hdmi->cec_rx_msg);
1032 } else if (vc4_hdmi->cec_tx_ok) {
1033 cec_transmit_done(vc4_hdmi->cec_adap, CEC_TX_STATUS_OK,
Hans Verkuil15b45112017-07-16 12:48:04 +02001034 0, 0, 0, 0);
1035 } else {
1036 /*
1037 * This CEC implementation makes 1 retry, so if we
1038 * get a NACK, then that means it made 2 attempts.
1039 */
Maxime Ripard3408cc22020-09-03 10:01:14 +02001040 cec_transmit_done(vc4_hdmi->cec_adap, CEC_TX_STATUS_NACK,
Hans Verkuil15b45112017-07-16 12:48:04 +02001041 0, 2, 0, 0);
1042 }
1043 return IRQ_HANDLED;
1044}
1045
Maxime Ripard66bf1c32020-09-03 10:01:18 +02001046static void vc4_cec_read_msg(struct vc4_hdmi *vc4_hdmi, u32 cntrl1)
Hans Verkuil15b45112017-07-16 12:48:04 +02001047{
Maxime Ripard13311452020-09-03 10:01:15 +02001048 struct cec_msg *msg = &vc4_hdmi->cec_rx_msg;
Hans Verkuil15b45112017-07-16 12:48:04 +02001049 unsigned int i;
1050
1051 msg->len = 1 + ((cntrl1 & VC4_HDMI_CEC_REC_WRD_CNT_MASK) >>
1052 VC4_HDMI_CEC_REC_WRD_CNT_SHIFT);
1053 for (i = 0; i < msg->len; i += 4) {
Maxime Ripard311e3052020-09-03 10:01:23 +02001054 u32 val = HDMI_READ(HDMI_CEC_RX_DATA_1 + i);
Hans Verkuil15b45112017-07-16 12:48:04 +02001055
1056 msg->msg[i] = val & 0xff;
1057 msg->msg[i + 1] = (val >> 8) & 0xff;
1058 msg->msg[i + 2] = (val >> 16) & 0xff;
1059 msg->msg[i + 3] = (val >> 24) & 0xff;
1060 }
1061}
1062
1063static irqreturn_t vc4_cec_irq_handler(int irq, void *priv)
1064{
Maxime Ripard66bf1c32020-09-03 10:01:18 +02001065 struct vc4_hdmi *vc4_hdmi = priv;
Maxime Ripard311e3052020-09-03 10:01:23 +02001066 u32 stat = HDMI_READ(HDMI_CEC_CPU_STATUS);
Hans Verkuil15b45112017-07-16 12:48:04 +02001067 u32 cntrl1, cntrl5;
1068
1069 if (!(stat & VC4_HDMI_CPU_CEC))
1070 return IRQ_NONE;
Maxime Ripard3408cc22020-09-03 10:01:14 +02001071 vc4_hdmi->cec_rx_msg.len = 0;
Maxime Ripard311e3052020-09-03 10:01:23 +02001072 cntrl1 = HDMI_READ(HDMI_CEC_CNTRL_1);
1073 cntrl5 = HDMI_READ(HDMI_CEC_CNTRL_5);
Maxime Ripard3408cc22020-09-03 10:01:14 +02001074 vc4_hdmi->cec_irq_was_rx = cntrl5 & VC4_HDMI_CEC_RX_CEC_INT;
1075 if (vc4_hdmi->cec_irq_was_rx) {
Maxime Ripard66bf1c32020-09-03 10:01:18 +02001076 vc4_cec_read_msg(vc4_hdmi, cntrl1);
Hans Verkuil15b45112017-07-16 12:48:04 +02001077 cntrl1 |= VC4_HDMI_CEC_CLEAR_RECEIVE_OFF;
Maxime Ripard311e3052020-09-03 10:01:23 +02001078 HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1);
Hans Verkuil15b45112017-07-16 12:48:04 +02001079 cntrl1 &= ~VC4_HDMI_CEC_CLEAR_RECEIVE_OFF;
1080 } else {
Maxime Ripard3408cc22020-09-03 10:01:14 +02001081 vc4_hdmi->cec_tx_ok = cntrl1 & VC4_HDMI_CEC_TX_STATUS_GOOD;
Hans Verkuil15b45112017-07-16 12:48:04 +02001082 cntrl1 &= ~VC4_HDMI_CEC_START_XMIT_BEGIN;
1083 }
Maxime Ripard311e3052020-09-03 10:01:23 +02001084 HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1);
1085 HDMI_WRITE(HDMI_CEC_CPU_CLEAR, VC4_HDMI_CPU_CEC);
Hans Verkuil15b45112017-07-16 12:48:04 +02001086
1087 return IRQ_WAKE_THREAD;
1088}
1089
1090static int vc4_hdmi_cec_adap_enable(struct cec_adapter *adap, bool enable)
1091{
Maxime Ripard66bf1c32020-09-03 10:01:18 +02001092 struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
Hans Verkuil15b45112017-07-16 12:48:04 +02001093 /* clock period in microseconds */
1094 const u32 usecs = 1000000 / CEC_CLOCK_FREQ;
Maxime Ripard311e3052020-09-03 10:01:23 +02001095 u32 val = HDMI_READ(HDMI_CEC_CNTRL_5);
Hans Verkuil15b45112017-07-16 12:48:04 +02001096
1097 val &= ~(VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET |
1098 VC4_HDMI_CEC_CNT_TO_4700_US_MASK |
1099 VC4_HDMI_CEC_CNT_TO_4500_US_MASK);
1100 val |= ((4700 / usecs) << VC4_HDMI_CEC_CNT_TO_4700_US_SHIFT) |
1101 ((4500 / usecs) << VC4_HDMI_CEC_CNT_TO_4500_US_SHIFT);
1102
1103 if (enable) {
Maxime Ripard311e3052020-09-03 10:01:23 +02001104 HDMI_WRITE(HDMI_CEC_CNTRL_5, val |
Hans Verkuil15b45112017-07-16 12:48:04 +02001105 VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET);
Maxime Ripard311e3052020-09-03 10:01:23 +02001106 HDMI_WRITE(HDMI_CEC_CNTRL_5, val);
1107 HDMI_WRITE(HDMI_CEC_CNTRL_2,
1108 ((1500 / usecs) << VC4_HDMI_CEC_CNT_TO_1500_US_SHIFT) |
1109 ((1300 / usecs) << VC4_HDMI_CEC_CNT_TO_1300_US_SHIFT) |
1110 ((800 / usecs) << VC4_HDMI_CEC_CNT_TO_800_US_SHIFT) |
1111 ((600 / usecs) << VC4_HDMI_CEC_CNT_TO_600_US_SHIFT) |
1112 ((400 / usecs) << VC4_HDMI_CEC_CNT_TO_400_US_SHIFT));
1113 HDMI_WRITE(HDMI_CEC_CNTRL_3,
1114 ((2750 / usecs) << VC4_HDMI_CEC_CNT_TO_2750_US_SHIFT) |
1115 ((2400 / usecs) << VC4_HDMI_CEC_CNT_TO_2400_US_SHIFT) |
1116 ((2050 / usecs) << VC4_HDMI_CEC_CNT_TO_2050_US_SHIFT) |
1117 ((1700 / usecs) << VC4_HDMI_CEC_CNT_TO_1700_US_SHIFT));
1118 HDMI_WRITE(HDMI_CEC_CNTRL_4,
1119 ((4300 / usecs) << VC4_HDMI_CEC_CNT_TO_4300_US_SHIFT) |
1120 ((3900 / usecs) << VC4_HDMI_CEC_CNT_TO_3900_US_SHIFT) |
1121 ((3600 / usecs) << VC4_HDMI_CEC_CNT_TO_3600_US_SHIFT) |
1122 ((3500 / usecs) << VC4_HDMI_CEC_CNT_TO_3500_US_SHIFT));
Hans Verkuil15b45112017-07-16 12:48:04 +02001123
Maxime Ripard311e3052020-09-03 10:01:23 +02001124 HDMI_WRITE(HDMI_CEC_CPU_MASK_CLEAR, VC4_HDMI_CPU_CEC);
Hans Verkuil15b45112017-07-16 12:48:04 +02001125 } else {
Maxime Ripard311e3052020-09-03 10:01:23 +02001126 HDMI_WRITE(HDMI_CEC_CPU_MASK_SET, VC4_HDMI_CPU_CEC);
1127 HDMI_WRITE(HDMI_CEC_CNTRL_5, val |
Hans Verkuil15b45112017-07-16 12:48:04 +02001128 VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET);
1129 }
1130 return 0;
1131}
1132
1133static int vc4_hdmi_cec_adap_log_addr(struct cec_adapter *adap, u8 log_addr)
1134{
Maxime Ripard66bf1c32020-09-03 10:01:18 +02001135 struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
Hans Verkuil15b45112017-07-16 12:48:04 +02001136
Maxime Ripard311e3052020-09-03 10:01:23 +02001137 HDMI_WRITE(HDMI_CEC_CNTRL_1,
1138 (HDMI_READ(HDMI_CEC_CNTRL_1) & ~VC4_HDMI_CEC_ADDR_MASK) |
Hans Verkuil15b45112017-07-16 12:48:04 +02001139 (log_addr & 0xf) << VC4_HDMI_CEC_ADDR_SHIFT);
1140 return 0;
1141}
1142
1143static int vc4_hdmi_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
1144 u32 signal_free_time, struct cec_msg *msg)
1145{
Maxime Ripard66bf1c32020-09-03 10:01:18 +02001146 struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
Hans Verkuil15b45112017-07-16 12:48:04 +02001147 u32 val;
1148 unsigned int i;
1149
1150 for (i = 0; i < msg->len; i += 4)
Maxime Ripard311e3052020-09-03 10:01:23 +02001151 HDMI_WRITE(HDMI_CEC_TX_DATA_1 + i,
Hans Verkuil15b45112017-07-16 12:48:04 +02001152 (msg->msg[i]) |
1153 (msg->msg[i + 1] << 8) |
1154 (msg->msg[i + 2] << 16) |
1155 (msg->msg[i + 3] << 24));
1156
Maxime Ripard311e3052020-09-03 10:01:23 +02001157 val = HDMI_READ(HDMI_CEC_CNTRL_1);
Hans Verkuil15b45112017-07-16 12:48:04 +02001158 val &= ~VC4_HDMI_CEC_START_XMIT_BEGIN;
Maxime Ripard311e3052020-09-03 10:01:23 +02001159 HDMI_WRITE(HDMI_CEC_CNTRL_1, val);
Hans Verkuil15b45112017-07-16 12:48:04 +02001160 val &= ~VC4_HDMI_CEC_MESSAGE_LENGTH_MASK;
1161 val |= (msg->len - 1) << VC4_HDMI_CEC_MESSAGE_LENGTH_SHIFT;
1162 val |= VC4_HDMI_CEC_START_XMIT_BEGIN;
1163
Maxime Ripard311e3052020-09-03 10:01:23 +02001164 HDMI_WRITE(HDMI_CEC_CNTRL_1, val);
Hans Verkuil15b45112017-07-16 12:48:04 +02001165 return 0;
1166}
1167
1168static const struct cec_adap_ops vc4_hdmi_cec_adap_ops = {
1169 .adap_enable = vc4_hdmi_cec_adap_enable,
1170 .adap_log_addr = vc4_hdmi_cec_adap_log_addr,
1171 .adap_transmit = vc4_hdmi_cec_adap_transmit,
1172};
Maxime Ripardc0791e02020-09-03 10:01:31 +02001173
1174static int vc4_hdmi_cec_init(struct vc4_hdmi *vc4_hdmi)
1175{
1176 struct cec_connector_info conn_info;
1177 struct platform_device *pdev = vc4_hdmi->pdev;
1178 u32 value;
1179 int ret;
1180
1181 vc4_hdmi->cec_adap = cec_allocate_adapter(&vc4_hdmi_cec_adap_ops,
1182 vc4_hdmi, "vc4",
1183 CEC_CAP_DEFAULTS |
1184 CEC_CAP_CONNECTOR_INFO, 1);
1185 ret = PTR_ERR_OR_ZERO(vc4_hdmi->cec_adap);
1186 if (ret < 0)
1187 return ret;
1188
1189 cec_fill_conn_info_from_drm(&conn_info, &vc4_hdmi->connector);
1190 cec_s_conn_info(vc4_hdmi->cec_adap, &conn_info);
1191
1192 HDMI_WRITE(HDMI_CEC_CPU_MASK_SET, 0xffffffff);
1193 value = HDMI_READ(HDMI_CEC_CNTRL_1);
1194 value &= ~VC4_HDMI_CEC_DIV_CLK_CNT_MASK;
1195 /*
1196 * Set the logical address to Unregistered and set the clock
1197 * divider: the hsm_clock rate and this divider setting will
1198 * give a 40 kHz CEC clock.
1199 */
1200 value |= VC4_HDMI_CEC_ADDR_MASK |
1201 (4091 << VC4_HDMI_CEC_DIV_CLK_CNT_SHIFT);
1202 HDMI_WRITE(HDMI_CEC_CNTRL_1, value);
1203 ret = devm_request_threaded_irq(&pdev->dev, platform_get_irq(pdev, 0),
1204 vc4_cec_irq_handler,
1205 vc4_cec_irq_handler_thread, 0,
1206 "vc4 hdmi cec", vc4_hdmi);
1207 if (ret)
1208 goto err_delete_cec_adap;
1209
1210 ret = cec_register_adapter(vc4_hdmi->cec_adap, &pdev->dev);
1211 if (ret < 0)
1212 goto err_delete_cec_adap;
1213
1214 return 0;
1215
1216err_delete_cec_adap:
1217 cec_delete_adapter(vc4_hdmi->cec_adap);
1218
1219 return ret;
1220}
1221
1222static void vc4_hdmi_cec_exit(struct vc4_hdmi *vc4_hdmi)
1223{
1224 cec_unregister_adapter(vc4_hdmi->cec_adap);
1225}
1226#else
1227static int vc4_hdmi_cec_init(struct vc4_hdmi *vc4_hdmi)
1228{
1229 return 0;
1230}
1231
1232static void vc4_hdmi_cec_exit(struct vc4_hdmi *vc4_hdmi) {};
1233
Hans Verkuil15b45112017-07-16 12:48:04 +02001234#endif
1235
Maxime Ripard311e3052020-09-03 10:01:23 +02001236static int vc4_hdmi_build_regset(struct vc4_hdmi *vc4_hdmi,
1237 struct debugfs_regset32 *regset,
1238 enum vc4_hdmi_regs reg)
1239{
1240 const struct vc4_hdmi_variant *variant = vc4_hdmi->variant;
1241 struct debugfs_reg32 *regs, *new_regs;
1242 unsigned int count = 0;
1243 unsigned int i;
1244
1245 regs = kcalloc(variant->num_registers, sizeof(*regs),
1246 GFP_KERNEL);
1247 if (!regs)
1248 return -ENOMEM;
1249
1250 for (i = 0; i < variant->num_registers; i++) {
1251 const struct vc4_hdmi_register *field = &variant->registers[i];
1252
1253 if (field->reg != reg)
1254 continue;
1255
1256 regs[count].name = field->name;
1257 regs[count].offset = field->offset;
1258 count++;
1259 }
1260
1261 new_regs = krealloc(regs, count * sizeof(*regs), GFP_KERNEL);
1262 if (!new_regs)
1263 return -ENOMEM;
1264
1265 regset->base = __vc4_hdmi_get_field_base(vc4_hdmi, reg);
1266 regset->regs = new_regs;
1267 regset->nregs = count;
1268
1269 return 0;
1270}
1271
Maxime Ripard33c773e2020-09-03 10:01:22 +02001272static int vc4_hdmi_init_resources(struct vc4_hdmi *vc4_hdmi)
1273{
1274 struct platform_device *pdev = vc4_hdmi->pdev;
1275 struct device *dev = &pdev->dev;
1276 int ret;
1277
1278 vc4_hdmi->hdmicore_regs = vc4_ioremap_regs(pdev, 0);
1279 if (IS_ERR(vc4_hdmi->hdmicore_regs))
1280 return PTR_ERR(vc4_hdmi->hdmicore_regs);
1281
1282 vc4_hdmi->hd_regs = vc4_ioremap_regs(pdev, 1);
1283 if (IS_ERR(vc4_hdmi->hd_regs))
1284 return PTR_ERR(vc4_hdmi->hd_regs);
1285
Maxime Ripard311e3052020-09-03 10:01:23 +02001286 ret = vc4_hdmi_build_regset(vc4_hdmi, &vc4_hdmi->hd_regset, VC4_HD);
1287 if (ret)
1288 return ret;
Maxime Ripard33c773e2020-09-03 10:01:22 +02001289
Maxime Ripard311e3052020-09-03 10:01:23 +02001290 ret = vc4_hdmi_build_regset(vc4_hdmi, &vc4_hdmi->hdmi_regset, VC4_HDMI);
1291 if (ret)
1292 return ret;
Maxime Ripard33c773e2020-09-03 10:01:22 +02001293
1294 vc4_hdmi->pixel_clock = devm_clk_get(dev, "pixel");
1295 if (IS_ERR(vc4_hdmi->pixel_clock)) {
1296 ret = PTR_ERR(vc4_hdmi->pixel_clock);
1297 if (ret != -EPROBE_DEFER)
1298 DRM_ERROR("Failed to get pixel clock\n");
1299 return ret;
1300 }
1301
1302 vc4_hdmi->hsm_clock = devm_clk_get(dev, "hdmi");
1303 if (IS_ERR(vc4_hdmi->hsm_clock)) {
1304 DRM_ERROR("Failed to get HDMI state machine clock\n");
1305 return PTR_ERR(vc4_hdmi->hsm_clock);
1306 }
1307
1308 return 0;
1309}
1310
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001311static int vc4_hdmi_bind(struct device *dev, struct device *master, void *data)
1312{
Maxime Ripard33c773e2020-09-03 10:01:22 +02001313 const struct vc4_hdmi_variant *variant = of_device_get_match_data(dev);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001314 struct platform_device *pdev = to_platform_device(dev);
1315 struct drm_device *drm = dev_get_drvdata(master);
Maxime Ripard3408cc22020-09-03 10:01:14 +02001316 struct vc4_hdmi *vc4_hdmi;
Maxime Ripardc98c85b2020-09-03 10:01:12 +02001317 struct drm_encoder *encoder;
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001318 struct device_node *ddc_node;
1319 u32 value;
1320 int ret;
1321
Maxime Ripard3408cc22020-09-03 10:01:14 +02001322 vc4_hdmi = devm_kzalloc(dev, sizeof(*vc4_hdmi), GFP_KERNEL);
1323 if (!vc4_hdmi)
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001324 return -ENOMEM;
1325
Maxime Ripard47c167b2020-09-03 10:01:19 +02001326 dev_set_drvdata(dev, vc4_hdmi);
Maxime Ripard3408cc22020-09-03 10:01:14 +02001327 encoder = &vc4_hdmi->encoder.base.base;
Maxime Ripard7d732992020-09-03 10:01:29 +02001328 vc4_hdmi->encoder.base.type = variant->encoder_type;
Maxime Ripard3408cc22020-09-03 10:01:14 +02001329 vc4_hdmi->pdev = pdev;
Maxime Ripard33c773e2020-09-03 10:01:22 +02001330 vc4_hdmi->variant = variant;
Maxime Ripardc98c85b2020-09-03 10:01:12 +02001331
Maxime Ripard33c773e2020-09-03 10:01:22 +02001332 ret = variant->init_resources(vc4_hdmi);
1333 if (ret)
James Hilliard8f6f5e02020-05-24 19:28:59 -06001334 return ret;
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001335
Peter Chen027a6972016-07-05 10:04:54 +08001336 ddc_node = of_parse_phandle(dev->of_node, "ddc", 0);
1337 if (!ddc_node) {
1338 DRM_ERROR("Failed to find ddc node in device tree\n");
1339 return -ENODEV;
1340 }
1341
Maxime Ripard3408cc22020-09-03 10:01:14 +02001342 vc4_hdmi->ddc = of_find_i2c_adapter_by_node(ddc_node);
Peter Chen027a6972016-07-05 10:04:54 +08001343 of_node_put(ddc_node);
Maxime Ripard3408cc22020-09-03 10:01:14 +02001344 if (!vc4_hdmi->ddc) {
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001345 DRM_DEBUG("Failed to get ddc i2c adapter by node\n");
1346 return -EPROBE_DEFER;
1347 }
1348
Hans Verkuil10ee2752017-07-16 12:48:03 +02001349 /* This is the rate that is set by the firmware. The number
1350 * needs to be a bit higher than the pixel clock rate
1351 * (generally 148.5Mhz).
1352 */
Maxime Ripard3408cc22020-09-03 10:01:14 +02001353 ret = clk_set_rate(vc4_hdmi->hsm_clock, HSM_CLOCK_FREQ);
Hans Verkuil10ee2752017-07-16 12:48:03 +02001354 if (ret) {
1355 DRM_ERROR("Failed to set HSM clock rate: %d\n", ret);
1356 goto err_put_i2c;
1357 }
1358
Maxime Ripard3408cc22020-09-03 10:01:14 +02001359 ret = clk_prepare_enable(vc4_hdmi->hsm_clock);
Hans Verkuil10ee2752017-07-16 12:48:03 +02001360 if (ret) {
1361 DRM_ERROR("Failed to turn on HDMI state machine clock: %d\n",
1362 ret);
1363 goto err_put_i2c;
1364 }
1365
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001366 /* Only use the GPIO HPD pin if present in the DT, otherwise
1367 * we'll use the HDMI core's register.
1368 */
1369 if (of_find_property(dev->of_node, "hpd-gpios", &value)) {
Eric Anholt0b06e0a2016-02-29 17:53:01 -08001370 enum of_gpio_flags hpd_gpio_flags;
1371
Maxime Ripard3408cc22020-09-03 10:01:14 +02001372 vc4_hdmi->hpd_gpio = of_get_named_gpio_flags(dev->of_node,
1373 "hpd-gpios", 0,
1374 &hpd_gpio_flags);
1375 if (vc4_hdmi->hpd_gpio < 0) {
1376 ret = vc4_hdmi->hpd_gpio;
Hans Verkuil10ee2752017-07-16 12:48:03 +02001377 goto err_unprepare_hsm;
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001378 }
Eric Anholt0b06e0a2016-02-29 17:53:01 -08001379
Maxime Ripard3408cc22020-09-03 10:01:14 +02001380 vc4_hdmi->hpd_active_low = hpd_gpio_flags & OF_GPIO_ACTIVE_LOW;
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001381 }
1382
Boris Brezillon4f6e3d62017-04-11 18:39:25 +02001383 pm_runtime_enable(dev);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001384
Maxime Ripardc98c85b2020-09-03 10:01:12 +02001385 drm_simple_encoder_init(drm, encoder, DRM_MODE_ENCODER_TMDS);
1386 drm_encoder_helper_add(encoder, &vc4_hdmi_encoder_helper_funcs);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001387
Maxime Ripard3408cc22020-09-03 10:01:14 +02001388 ret = vc4_hdmi_connector_init(drm, vc4_hdmi);
Maxime Ripardc98c85b2020-09-03 10:01:12 +02001389 if (ret)
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001390 goto err_destroy_encoder;
Maxime Ripardc98c85b2020-09-03 10:01:12 +02001391
Maxime Ripardc0791e02020-09-03 10:01:31 +02001392 ret = vc4_hdmi_cec_init(vc4_hdmi);
Hans Verkuil15b45112017-07-16 12:48:04 +02001393 if (ret)
Maxime Ripardc0791e02020-09-03 10:01:31 +02001394 goto err_destroy_conn;
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001395
Maxime Ripard3408cc22020-09-03 10:01:14 +02001396 ret = vc4_hdmi_audio_init(vc4_hdmi);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001397 if (ret)
Maxime Ripardc0791e02020-09-03 10:01:31 +02001398 goto err_free_cec;
Eric Anholtbb7d7852017-02-27 12:28:02 -08001399
Maxime Ripardb2405c92020-09-03 10:01:30 +02001400 vc4_debugfs_add_file(drm, variant->debugfs_name,
1401 vc4_hdmi_debugfs_regs,
1402 vc4_hdmi);
Eric Anholtc9be8042019-04-01 11:35:58 -07001403
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001404 return 0;
1405
Maxime Ripardc0791e02020-09-03 10:01:31 +02001406err_free_cec:
1407 vc4_hdmi_cec_exit(vc4_hdmi);
Hans Verkuil15b45112017-07-16 12:48:04 +02001408err_destroy_conn:
Maxime Ripard0532e5e2020-09-03 10:01:21 +02001409 vc4_hdmi_connector_destroy(&vc4_hdmi->connector);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001410err_destroy_encoder:
Maxime Ripardc98c85b2020-09-03 10:01:12 +02001411 drm_encoder_cleanup(encoder);
Hans Verkuil10ee2752017-07-16 12:48:03 +02001412err_unprepare_hsm:
Maxime Ripard3408cc22020-09-03 10:01:14 +02001413 clk_disable_unprepare(vc4_hdmi->hsm_clock);
Boris Brezillon4f6e3d62017-04-11 18:39:25 +02001414 pm_runtime_disable(dev);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001415err_put_i2c:
Maxime Ripard3408cc22020-09-03 10:01:14 +02001416 put_device(&vc4_hdmi->ddc->dev);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001417
1418 return ret;
1419}
1420
1421static void vc4_hdmi_unbind(struct device *dev, struct device *master,
1422 void *data)
1423{
Maxime Ripard47c167b2020-09-03 10:01:19 +02001424 struct vc4_hdmi *vc4_hdmi;
1425
1426 /*
1427 * ASoC makes it a bit hard to retrieve a pointer to the
1428 * vc4_hdmi structure. Registering the card will overwrite our
1429 * device drvdata with a pointer to the snd_soc_card structure,
1430 * which can then be used to retrieve whatever drvdata we want
1431 * to associate.
1432 *
1433 * However, that doesn't fly in the case where we wouldn't
1434 * register an ASoC card (because of an old DT that is missing
1435 * the dmas properties for example), then the card isn't
1436 * registered and the device drvdata wouldn't be set.
1437 *
1438 * We can deal with both cases by making sure a snd_soc_card
1439 * pointer and a vc4_hdmi structure are pointing to the same
1440 * memory address, so we can treat them indistinctly without any
1441 * issue.
1442 */
1443 BUILD_BUG_ON(offsetof(struct vc4_hdmi_audio, card) != 0);
1444 BUILD_BUG_ON(offsetof(struct vc4_hdmi, audio) != 0);
1445 vc4_hdmi = dev_get_drvdata(dev);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001446
Maxime Ripard311e3052020-09-03 10:01:23 +02001447 kfree(vc4_hdmi->hdmi_regset.regs);
1448 kfree(vc4_hdmi->hd_regset.regs);
1449
Maxime Ripardc0791e02020-09-03 10:01:31 +02001450 vc4_hdmi_cec_exit(vc4_hdmi);
Maxime Ripard0532e5e2020-09-03 10:01:21 +02001451 vc4_hdmi_connector_destroy(&vc4_hdmi->connector);
Maxime Ripard3408cc22020-09-03 10:01:14 +02001452 drm_encoder_cleanup(&vc4_hdmi->encoder.base.base);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001453
Maxime Ripard3408cc22020-09-03 10:01:14 +02001454 clk_disable_unprepare(vc4_hdmi->hsm_clock);
Boris Brezillon4f6e3d62017-04-11 18:39:25 +02001455 pm_runtime_disable(dev);
1456
Maxime Ripard3408cc22020-09-03 10:01:14 +02001457 put_device(&vc4_hdmi->ddc->dev);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001458}
1459
1460static const struct component_ops vc4_hdmi_ops = {
1461 .bind = vc4_hdmi_bind,
1462 .unbind = vc4_hdmi_unbind,
1463};
1464
1465static int vc4_hdmi_dev_probe(struct platform_device *pdev)
1466{
1467 return component_add(&pdev->dev, &vc4_hdmi_ops);
1468}
1469
1470static int vc4_hdmi_dev_remove(struct platform_device *pdev)
1471{
1472 component_del(&pdev->dev, &vc4_hdmi_ops);
1473 return 0;
1474}
1475
Maxime Ripard33c773e2020-09-03 10:01:22 +02001476static const struct vc4_hdmi_variant bcm2835_variant = {
Maxime Ripard7d732992020-09-03 10:01:29 +02001477 .encoder_type = VC4_ENCODER_TYPE_HDMI0,
Maxime Ripardb2405c92020-09-03 10:01:30 +02001478 .debugfs_name = "hdmi_regs",
Maxime Ripard311e3052020-09-03 10:01:23 +02001479 .registers = vc4_hdmi_fields,
1480 .num_registers = ARRAY_SIZE(vc4_hdmi_fields),
1481
Maxime Ripard33c773e2020-09-03 10:01:22 +02001482 .init_resources = vc4_hdmi_init_resources,
Maxime Ripard89f31a22020-09-03 10:01:27 +02001483 .csc_setup = vc4_hdmi_csc_setup,
Maxime Ripard9045e912020-09-03 10:01:24 +02001484 .reset = vc4_hdmi_reset,
Maxime Ripard904f6682020-09-03 10:01:28 +02001485 .set_timings = vc4_hdmi_set_timings,
Maxime Ripardc457b8a2020-09-03 10:01:25 +02001486 .phy_init = vc4_hdmi_phy_init,
1487 .phy_disable = vc4_hdmi_phy_disable,
Maxime Ripard647b9652020-09-03 10:01:26 +02001488 .phy_rng_enable = vc4_hdmi_phy_rng_enable,
1489 .phy_rng_disable = vc4_hdmi_phy_rng_disable,
Maxime Ripard33c773e2020-09-03 10:01:22 +02001490};
1491
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001492static const struct of_device_id vc4_hdmi_dt_match[] = {
Maxime Ripard33c773e2020-09-03 10:01:22 +02001493 { .compatible = "brcm,bcm2835-hdmi", .data = &bcm2835_variant },
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001494 {}
1495};
1496
1497struct platform_driver vc4_hdmi_driver = {
1498 .probe = vc4_hdmi_dev_probe,
1499 .remove = vc4_hdmi_dev_remove,
1500 .driver = {
1501 .name = "vc4_hdmi",
1502 .of_match_table = vc4_hdmi_dt_match,
1503 },
1504};