blob: a3813a6137fb045808a8a02c23c76c21097e1402 [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>
Maxime Ripardc85695a2021-05-07 17:05:13 +020038#include <drm/drm_scdc_helper.h>
Masahiro Yamadab7e8e252017-05-18 13:29:38 +090039#include <linux/clk.h>
40#include <linux/component.h>
41#include <linux/i2c.h>
42#include <linux/of_address.h>
43#include <linux/of_gpio.h>
44#include <linux/of_platform.h>
45#include <linux/pm_runtime.h>
46#include <linux/rational.h>
Maxime Ripard83239892020-09-03 10:01:48 +020047#include <linux/reset.h>
Masahiro Yamadab7e8e252017-05-18 13:29:38 +090048#include <sound/dmaengine_pcm.h>
Maxime Ripard91e99e12021-05-25 15:23:52 +020049#include <sound/hdmi-codec.h>
Masahiro Yamadab7e8e252017-05-18 13:29:38 +090050#include <sound/pcm_drm_eld.h>
51#include <sound/pcm_params.h>
52#include <sound/soc.h>
Hans Verkuil15b45112017-07-16 12:48:04 +020053#include "media/cec.h"
Eric Anholtc8b75bc2015-03-02 13:01:12 -080054#include "vc4_drv.h"
Maxime Ripardf73100c2020-09-03 10:01:11 +020055#include "vc4_hdmi.h"
Maxime Ripard311e3052020-09-03 10:01:23 +020056#include "vc4_hdmi_regs.h"
Eric Anholtc8b75bc2015-03-02 13:01:12 -080057#include "vc4_regs.h"
58
Maxime Ripard83239892020-09-03 10:01:48 +020059#define VC5_HDMI_HORZA_HFP_SHIFT 16
60#define VC5_HDMI_HORZA_HFP_MASK VC4_MASK(28, 16)
61#define VC5_HDMI_HORZA_VPOS BIT(15)
62#define VC5_HDMI_HORZA_HPOS BIT(14)
63#define VC5_HDMI_HORZA_HAP_SHIFT 0
64#define VC5_HDMI_HORZA_HAP_MASK VC4_MASK(13, 0)
65
66#define VC5_HDMI_HORZB_HBP_SHIFT 16
67#define VC5_HDMI_HORZB_HBP_MASK VC4_MASK(26, 16)
68#define VC5_HDMI_HORZB_HSP_SHIFT 0
69#define VC5_HDMI_HORZB_HSP_MASK VC4_MASK(10, 0)
70
71#define VC5_HDMI_VERTA_VSP_SHIFT 24
72#define VC5_HDMI_VERTA_VSP_MASK VC4_MASK(28, 24)
73#define VC5_HDMI_VERTA_VFP_SHIFT 16
74#define VC5_HDMI_VERTA_VFP_MASK VC4_MASK(22, 16)
75#define VC5_HDMI_VERTA_VAL_SHIFT 0
76#define VC5_HDMI_VERTA_VAL_MASK VC4_MASK(12, 0)
77
78#define VC5_HDMI_VERTB_VSPO_SHIFT 16
79#define VC5_HDMI_VERTB_VSPO_MASK VC4_MASK(29, 16)
80
Maxime Ripardc85695a2021-05-07 17:05:13 +020081#define VC5_HDMI_SCRAMBLER_CTL_ENABLE BIT(0)
82
Maxime Ripardba8c0fa2020-12-15 16:42:43 +010083#define VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE_SHIFT 8
84#define VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE_MASK VC4_MASK(10, 8)
85
86#define VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH_SHIFT 0
87#define VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH_MASK VC4_MASK(3, 0)
88
89#define VC5_HDMI_GCP_CONFIG_GCP_ENABLE BIT(31)
90
91#define VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1_SHIFT 8
92#define VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1_MASK VC4_MASK(15, 8)
93
Maxime Ripard83239892020-09-03 10:01:48 +020094# define VC4_HD_M_SW_RST BIT(2)
95# define VC4_HD_M_ENABLE BIT(0)
96
Maxime Ripard3e85b812021-09-22 14:54:17 +020097#define HSM_MIN_CLOCK_FREQ 120000000
Hans Verkuil15b45112017-07-16 12:48:04 +020098#define CEC_CLOCK_FREQ 40000
Eric Anholtc8b75bc2015-03-02 13:01:12 -080099
Maxime Ripard24169a22020-12-15 16:42:42 +0100100#define HDMI_14_MAX_TMDS_CLK (340 * 1000 * 1000)
101
Maxime Ripard86e3a652021-05-07 17:05:12 +0200102static bool vc4_hdmi_mode_needs_scrambling(const struct drm_display_mode *mode)
103{
104 return (mode->clock * 1000) > HDMI_14_MAX_TMDS_CLK;
105}
106
Eric Anholtc9be8042019-04-01 11:35:58 -0700107static int vc4_hdmi_debugfs_regs(struct seq_file *m, void *unused)
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800108{
109 struct drm_info_node *node = (struct drm_info_node *)m->private;
Maxime Ripard3408cc22020-09-03 10:01:14 +0200110 struct vc4_hdmi *vc4_hdmi = node->info_ent->data;
Eric Anholt30517192019-02-20 13:03:38 -0800111 struct drm_printer p = drm_seq_file_printer(m);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800112
Maxime Ripard3408cc22020-09-03 10:01:14 +0200113 drm_print_regset32(&p, &vc4_hdmi->hdmi_regset);
114 drm_print_regset32(&p, &vc4_hdmi->hd_regset);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800115
116 return 0;
117}
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800118
Maxime Ripard9045e912020-09-03 10:01:24 +0200119static void vc4_hdmi_reset(struct vc4_hdmi *vc4_hdmi)
120{
121 HDMI_WRITE(HDMI_M_CTL, VC4_HD_M_SW_RST);
122 udelay(1);
123 HDMI_WRITE(HDMI_M_CTL, 0);
124
125 HDMI_WRITE(HDMI_M_CTL, VC4_HD_M_ENABLE);
126
127 HDMI_WRITE(HDMI_SW_RESET_CONTROL,
128 VC4_HDMI_SW_RESET_HDMI |
129 VC4_HDMI_SW_RESET_FORMAT_DETECT);
130
131 HDMI_WRITE(HDMI_SW_RESET_CONTROL, 0);
132}
133
Maxime Ripard83239892020-09-03 10:01:48 +0200134static void vc5_hdmi_reset(struct vc4_hdmi *vc4_hdmi)
135{
136 reset_control_reset(vc4_hdmi->reset);
137
138 HDMI_WRITE(HDMI_DVP_CTL, 0);
139
140 HDMI_WRITE(HDMI_CLOCK_STOP,
141 HDMI_READ(HDMI_CLOCK_STOP) | VC4_DVP_HT_CLOCK_STOP_PIXEL);
142}
143
Maxime Ripard47fa9a82021-01-11 15:23:01 +0100144#ifdef CONFIG_DRM_VC4_HDMI_CEC
145static void vc4_hdmi_cec_update_clk_div(struct vc4_hdmi *vc4_hdmi)
146{
147 u16 clk_cnt;
148 u32 value;
149
150 value = HDMI_READ(HDMI_CEC_CNTRL_1);
151 value &= ~VC4_HDMI_CEC_DIV_CLK_CNT_MASK;
152
153 /*
154 * Set the clock divider: the hsm_clock rate and this divider
155 * setting will give a 40 kHz CEC clock.
156 */
Maxime Ripard23b7eb52021-01-11 15:23:02 +0100157 clk_cnt = clk_get_rate(vc4_hdmi->cec_clock) / CEC_CLOCK_FREQ;
Maxime Ripard47fa9a82021-01-11 15:23:01 +0100158 value |= clk_cnt << VC4_HDMI_CEC_DIV_CLK_CNT_SHIFT;
159 HDMI_WRITE(HDMI_CEC_CNTRL_1, value);
160}
161#else
162static void vc4_hdmi_cec_update_clk_div(struct vc4_hdmi *vc4_hdmi) {}
163#endif
164
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800165static enum drm_connector_status
166vc4_hdmi_connector_detect(struct drm_connector *connector, bool force)
167{
Maxime Ripard5dfbcae2020-09-03 10:01:17 +0200168 struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector);
Dom Cobley4d8602b2021-01-11 15:22:59 +0100169 bool connected = false;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800170
Maxime Ripard68002342021-05-24 15:18:52 +0200171 if (vc4_hdmi->hpd_gpio &&
172 gpiod_get_value_cansleep(vc4_hdmi->hpd_gpio)) {
173 connected = true;
Dom Cobley4d8602b2021-01-11 15:22:59 +0100174 } else if (drm_probe_ddc(vc4_hdmi->ddc)) {
175 connected = true;
176 } else if (HDMI_READ(HDMI_HOTPLUG) & VC4_HDMI_HOTPLUG_CONNECTED) {
177 connected = true;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800178 }
179
Dom Cobley4d8602b2021-01-11 15:22:59 +0100180 if (connected) {
181 if (connector->status != connector_status_connected) {
182 struct edid *edid = drm_get_edid(connector, vc4_hdmi->ddc);
Eric Anholt9d44abb2016-09-14 19:21:29 +0100183
Dom Cobley4d8602b2021-01-11 15:22:59 +0100184 if (edid) {
185 cec_s_phys_addr_from_edid(vc4_hdmi->cec_adap, edid);
186 vc4_hdmi->encoder.hdmi_monitor = drm_detect_hdmi_monitor(edid);
187 kfree(edid);
188 }
189 }
190
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800191 return connector_status_connected;
Dom Cobley4d8602b2021-01-11 15:22:59 +0100192 }
193
Maxime Ripardb10db9a2020-09-03 10:01:16 +0200194 cec_phys_addr_invalidate(vc4_hdmi->cec_adap);
Hans Verkuil15b45112017-07-16 12:48:04 +0200195 return connector_status_disconnected;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800196}
197
198static void vc4_hdmi_connector_destroy(struct drm_connector *connector)
199{
200 drm_connector_unregister(connector);
201 drm_connector_cleanup(connector);
202}
203
204static int vc4_hdmi_connector_get_modes(struct drm_connector *connector)
205{
Maxime Ripard5dfbcae2020-09-03 10:01:17 +0200206 struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector);
207 struct vc4_hdmi_encoder *vc4_encoder = &vc4_hdmi->encoder;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800208 int ret = 0;
209 struct edid *edid;
210
Maxime Ripard5dfbcae2020-09-03 10:01:17 +0200211 edid = drm_get_edid(connector, vc4_hdmi->ddc);
212 cec_s_phys_addr_from_edid(vc4_hdmi->cec_adap, edid);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800213 if (!edid)
214 return -ENODEV;
215
216 vc4_encoder->hdmi_monitor = drm_detect_hdmi_monitor(edid);
Eric Anholt21317b32016-09-29 15:34:43 -0700217
Daniel Vetterc555f022018-07-09 10:40:06 +0200218 drm_connector_update_edid_property(connector, edid);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800219 ret = drm_add_edid_modes(connector, edid);
Eric Anholt5afe0e62017-08-08 13:56:05 -0700220 kfree(edid);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800221
Maxime Ripard86e3a652021-05-07 17:05:12 +0200222 if (vc4_hdmi->disable_4kp60) {
223 struct drm_device *drm = connector->dev;
224 struct drm_display_mode *mode;
225
226 list_for_each_entry(mode, &connector->probed_modes, head) {
227 if (vc4_hdmi_mode_needs_scrambling(mode)) {
228 drm_warn_once(drm, "The core clock cannot reach frequencies high enough to support 4k @ 60Hz.");
229 drm_warn_once(drm, "Please change your config.txt file to add hdmi_enable_4kp60.");
230 }
231 }
232 }
233
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800234 return ret;
235}
236
Dave Stevensonbccd5c52021-04-30 11:44:49 +0200237static int vc4_hdmi_connector_atomic_check(struct drm_connector *connector,
238 struct drm_atomic_state *state)
239{
240 struct drm_connector_state *old_state =
241 drm_atomic_get_old_connector_state(state, connector);
242 struct drm_connector_state *new_state =
243 drm_atomic_get_new_connector_state(state, connector);
244 struct drm_crtc *crtc = new_state->crtc;
245
246 if (!crtc)
247 return 0;
248
Maxime Ripard76a262d2021-04-30 11:44:51 +0200249 if (old_state->colorspace != new_state->colorspace ||
250 !drm_connector_atomic_hdr_metadata_equal(old_state, new_state)) {
Dave Stevensonbccd5c52021-04-30 11:44:49 +0200251 struct drm_crtc_state *crtc_state;
252
253 crtc_state = drm_atomic_get_crtc_state(state, crtc);
254 if (IS_ERR(crtc_state))
255 return PTR_ERR(crtc_state);
256
257 crtc_state->mode_changed = true;
258 }
259
260 return 0;
261}
262
Maxime Ripard90b2df52019-06-19 12:17:53 +0200263static void vc4_hdmi_connector_reset(struct drm_connector *connector)
264{
Maxime Ripardfbe72712020-12-15 16:42:39 +0100265 struct vc4_hdmi_connector_state *old_state =
266 conn_state_to_vc4_hdmi_conn_state(connector->state);
267 struct vc4_hdmi_connector_state *new_state =
268 kzalloc(sizeof(*new_state), GFP_KERNEL);
Maxime Riparde55a0772020-12-15 16:42:38 +0100269
270 if (connector->state)
Maxime Ripardfbe72712020-12-15 16:42:39 +0100271 __drm_atomic_helper_connector_destroy_state(connector->state);
272
273 kfree(old_state);
274 __drm_atomic_helper_connector_reset(connector, &new_state->base);
275
276 if (!new_state)
277 return;
278
Maxime Ripardba8c0fa2020-12-15 16:42:43 +0100279 new_state->base.max_bpc = 8;
280 new_state->base.max_requested_bpc = 8;
Maxime Ripardfbe72712020-12-15 16:42:39 +0100281 drm_atomic_helper_connector_tv_reset(connector);
282}
283
284static struct drm_connector_state *
285vc4_hdmi_connector_duplicate_state(struct drm_connector *connector)
286{
287 struct drm_connector_state *conn_state = connector->state;
288 struct vc4_hdmi_connector_state *vc4_state = conn_state_to_vc4_hdmi_conn_state(conn_state);
289 struct vc4_hdmi_connector_state *new_state;
290
291 new_state = kzalloc(sizeof(*new_state), GFP_KERNEL);
292 if (!new_state)
293 return NULL;
294
Maxime Ripardf6237462020-12-15 16:42:40 +0100295 new_state->pixel_rate = vc4_state->pixel_rate;
Maxime Ripardfbe72712020-12-15 16:42:39 +0100296 __drm_atomic_helper_connector_duplicate_state(connector, &new_state->base);
297
298 return &new_state->base;
Maxime Ripard90b2df52019-06-19 12:17:53 +0200299}
300
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800301static const struct drm_connector_funcs vc4_hdmi_connector_funcs = {
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800302 .detect = vc4_hdmi_connector_detect,
Eric Anholt682e62c2016-09-28 17:30:25 -0700303 .fill_modes = drm_helper_probe_single_connector_modes,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800304 .destroy = vc4_hdmi_connector_destroy,
Maxime Ripard90b2df52019-06-19 12:17:53 +0200305 .reset = vc4_hdmi_connector_reset,
Maxime Ripardfbe72712020-12-15 16:42:39 +0100306 .atomic_duplicate_state = vc4_hdmi_connector_duplicate_state,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800307 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
308};
309
310static const struct drm_connector_helper_funcs vc4_hdmi_connector_helper_funcs = {
311 .get_modes = vc4_hdmi_connector_get_modes,
Dave Stevensonbccd5c52021-04-30 11:44:49 +0200312 .atomic_check = vc4_hdmi_connector_atomic_check,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800313};
314
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200315static int vc4_hdmi_connector_init(struct drm_device *dev,
Maxime Ripardb052e702020-09-03 10:01:13 +0200316 struct vc4_hdmi *vc4_hdmi)
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800317{
Maxime Ripard0532e5e2020-09-03 10:01:21 +0200318 struct drm_connector *connector = &vc4_hdmi->connector;
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200319 struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
Boris Brezillondb999532018-12-06 15:24:39 +0100320 int ret;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800321
Andrzej Pietrasiewicz04a880f2020-01-02 14:22:58 +0100322 drm_connector_init_with_ddc(dev, connector,
323 &vc4_hdmi_connector_funcs,
324 DRM_MODE_CONNECTOR_HDMIA,
Maxime Ripardb052e702020-09-03 10:01:13 +0200325 vc4_hdmi->ddc);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800326 drm_connector_helper_add(connector, &vc4_hdmi_connector_helper_funcs);
327
Maxime Ripardba8c0fa2020-12-15 16:42:43 +0100328 /*
329 * Some of the properties below require access to state, like bpc.
330 * Allocate some default initial connector state with our reset helper.
331 */
332 if (connector->funcs->reset)
333 connector->funcs->reset(connector);
334
Boris Brezillondb999532018-12-06 15:24:39 +0100335 /* Create and attach TV margin props to this connector. */
336 ret = drm_mode_create_tv_margin_properties(dev);
337 if (ret)
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200338 return ret;
Boris Brezillondb999532018-12-06 15:24:39 +0100339
Maxime Ripard76a262d2021-04-30 11:44:51 +0200340 ret = drm_mode_create_hdmi_colorspace_property(connector);
341 if (ret)
342 return ret;
343
344 drm_connector_attach_colorspace_property(connector);
Boris Brezillondb999532018-12-06 15:24:39 +0100345 drm_connector_attach_tv_margin_properties(connector);
Maxime Ripardba8c0fa2020-12-15 16:42:43 +0100346 drm_connector_attach_max_bpc_property(connector, 8, 12);
Boris Brezillondb999532018-12-06 15:24:39 +0100347
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800348 connector->polled = (DRM_CONNECTOR_POLL_CONNECT |
349 DRM_CONNECTOR_POLL_DISCONNECT);
350
Mario Kleineracc1be12016-07-19 20:58:58 +0200351 connector->interlace_allowed = 1;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800352 connector->doublescan_allowed = 0;
353
Dave Stevensonbccd5c52021-04-30 11:44:49 +0200354 if (vc4_hdmi->variant->supports_hdr)
355 drm_connector_attach_hdr_output_metadata_property(connector);
356
Daniel Vettercde4c442018-07-09 10:40:07 +0200357 drm_connector_attach_encoder(connector, encoder);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800358
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200359 return 0;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800360}
361
Eric Anholt21317b32016-09-29 15:34:43 -0700362static int vc4_hdmi_stop_packet(struct drm_encoder *encoder,
Maxime Riparde2f9b2e2020-12-03 08:46:24 +0100363 enum hdmi_infoframe_type type,
364 bool poll)
Eric Anholt21317b32016-09-29 15:34:43 -0700365{
Maxime Ripard5dfbcae2020-09-03 10:01:17 +0200366 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
Eric Anholt21317b32016-09-29 15:34:43 -0700367 u32 packet_id = type - 0x80;
368
Maxime Ripard311e3052020-09-03 10:01:23 +0200369 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
370 HDMI_READ(HDMI_RAM_PACKET_CONFIG) & ~BIT(packet_id));
Eric Anholt21317b32016-09-29 15:34:43 -0700371
Maxime Riparde2f9b2e2020-12-03 08:46:24 +0100372 if (!poll)
373 return 0;
374
Maxime Ripard311e3052020-09-03 10:01:23 +0200375 return wait_for(!(HDMI_READ(HDMI_RAM_PACKET_STATUS) &
Eric Anholt21317b32016-09-29 15:34:43 -0700376 BIT(packet_id)), 100);
377}
378
379static void vc4_hdmi_write_infoframe(struct drm_encoder *encoder,
380 union hdmi_infoframe *frame)
381{
Maxime Ripard5dfbcae2020-09-03 10:01:17 +0200382 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
Eric Anholt21317b32016-09-29 15:34:43 -0700383 u32 packet_id = frame->any.type - 0x80;
Maxime Ripard311e3052020-09-03 10:01:23 +0200384 const struct vc4_hdmi_register *ram_packet_start =
385 &vc4_hdmi->variant->registers[HDMI_RAM_PACKET_START];
386 u32 packet_reg = ram_packet_start->offset + VC4_HDMI_PACKET_STRIDE * packet_id;
387 void __iomem *base = __vc4_hdmi_get_field_base(vc4_hdmi,
388 ram_packet_start->reg);
Eric Anholt21317b32016-09-29 15:34:43 -0700389 uint8_t buffer[VC4_HDMI_PACKET_STRIDE];
390 ssize_t len, i;
391 int ret;
392
Maxime Ripard311e3052020-09-03 10:01:23 +0200393 WARN_ONCE(!(HDMI_READ(HDMI_RAM_PACKET_CONFIG) &
Eric Anholt21317b32016-09-29 15:34:43 -0700394 VC4_HDMI_RAM_PACKET_ENABLE),
395 "Packet RAM has to be on to store the packet.");
396
397 len = hdmi_infoframe_pack(frame, buffer, sizeof(buffer));
398 if (len < 0)
399 return;
400
Maxime Riparde2f9b2e2020-12-03 08:46:24 +0100401 ret = vc4_hdmi_stop_packet(encoder, frame->any.type, true);
Eric Anholt21317b32016-09-29 15:34:43 -0700402 if (ret) {
403 DRM_ERROR("Failed to wait for infoframe to go idle: %d\n", ret);
404 return;
405 }
406
407 for (i = 0; i < len; i += 7) {
Maxime Ripard311e3052020-09-03 10:01:23 +0200408 writel(buffer[i + 0] << 0 |
409 buffer[i + 1] << 8 |
410 buffer[i + 2] << 16,
411 base + packet_reg);
Eric Anholt21317b32016-09-29 15:34:43 -0700412 packet_reg += 4;
413
Maxime Ripard311e3052020-09-03 10:01:23 +0200414 writel(buffer[i + 3] << 0 |
415 buffer[i + 4] << 8 |
416 buffer[i + 5] << 16 |
417 buffer[i + 6] << 24,
418 base + packet_reg);
Eric Anholt21317b32016-09-29 15:34:43 -0700419 packet_reg += 4;
420 }
421
Maxime Ripard311e3052020-09-03 10:01:23 +0200422 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
423 HDMI_READ(HDMI_RAM_PACKET_CONFIG) | BIT(packet_id));
424 ret = wait_for((HDMI_READ(HDMI_RAM_PACKET_STATUS) &
Eric Anholt21317b32016-09-29 15:34:43 -0700425 BIT(packet_id)), 100);
426 if (ret)
427 DRM_ERROR("Failed to wait for infoframe to start: %d\n", ret);
428}
429
430static void vc4_hdmi_set_avi_infoframe(struct drm_encoder *encoder)
431{
Maxime Ripard5dfbcae2020-09-03 10:01:17 +0200432 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
Eric Anholt21317b32016-09-29 15:34:43 -0700433 struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
Maxime Ripard0532e5e2020-09-03 10:01:21 +0200434 struct drm_connector *connector = &vc4_hdmi->connector;
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200435 struct drm_connector_state *cstate = connector->state;
Linus Torvalds31ad37b2021-09-19 10:11:53 -0700436 struct drm_crtc *crtc = encoder->crtc;
Eric Anholt21317b32016-09-29 15:34:43 -0700437 const struct drm_display_mode *mode = &crtc->state->adjusted_mode;
438 union hdmi_infoframe frame;
439 int ret;
440
Ville Syrjälä13d0add2019-01-08 19:28:25 +0200441 ret = drm_hdmi_avi_infoframe_from_display_mode(&frame.avi,
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200442 connector, mode);
Eric Anholt21317b32016-09-29 15:34:43 -0700443 if (ret < 0) {
444 DRM_ERROR("couldn't fill AVI infoframe\n");
445 return;
446 }
447
Ville Syrjälä13d0add2019-01-08 19:28:25 +0200448 drm_hdmi_avi_infoframe_quant_range(&frame.avi,
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200449 connector, mode,
Ville Syrjäläa2ce26f2017-01-11 14:57:23 +0200450 vc4_encoder->limited_rgb_range ?
451 HDMI_QUANTIZATION_RANGE_LIMITED :
Ville Syrjälä1581b2d2019-01-08 19:28:28 +0200452 HDMI_QUANTIZATION_RANGE_FULL);
Maxime Ripard76a262d2021-04-30 11:44:51 +0200453 drm_hdmi_avi_infoframe_colorspace(&frame.avi, cstate);
Ville Syrjäläcb876372019-10-08 19:48:14 +0300454 drm_hdmi_avi_infoframe_bars(&frame.avi, cstate);
Boris Brezillondb999532018-12-06 15:24:39 +0100455
Eric Anholt21317b32016-09-29 15:34:43 -0700456 vc4_hdmi_write_infoframe(encoder, &frame);
457}
458
459static void vc4_hdmi_set_spd_infoframe(struct drm_encoder *encoder)
460{
461 union hdmi_infoframe frame;
462 int ret;
463
464 ret = hdmi_spd_infoframe_init(&frame.spd, "Broadcom", "Videocore");
465 if (ret < 0) {
466 DRM_ERROR("couldn't fill SPD infoframe\n");
467 return;
468 }
469
470 frame.spd.sdi = HDMI_SPD_SDI_PC;
471
472 vc4_hdmi_write_infoframe(encoder, &frame);
473}
474
Eric Anholtbb7d7852017-02-27 12:28:02 -0800475static void vc4_hdmi_set_audio_infoframe(struct drm_encoder *encoder)
476{
Maxime Ripard5dfbcae2020-09-03 10:01:17 +0200477 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
Maxime Ripard91e99e12021-05-25 15:23:52 +0200478 struct hdmi_audio_infoframe *audio = &vc4_hdmi->audio.infoframe;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800479 union hdmi_infoframe frame;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800480
Maxime Ripard91e99e12021-05-25 15:23:52 +0200481 memcpy(&frame.audio, audio, sizeof(*audio));
Eric Anholtbb7d7852017-02-27 12:28:02 -0800482 vc4_hdmi_write_infoframe(encoder, &frame);
483}
484
Dave Stevensonbccd5c52021-04-30 11:44:49 +0200485static void vc4_hdmi_set_hdr_infoframe(struct drm_encoder *encoder)
486{
487 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
488 struct drm_connector *connector = &vc4_hdmi->connector;
489 struct drm_connector_state *conn_state = connector->state;
490 union hdmi_infoframe frame;
491
492 if (!vc4_hdmi->variant->supports_hdr)
493 return;
494
495 if (!conn_state->hdr_output_metadata)
496 return;
497
498 if (drm_hdmi_infoframe_set_hdr_metadata(&frame.drm, conn_state))
499 return;
500
501 vc4_hdmi_write_infoframe(encoder, &frame);
502}
503
Eric Anholt21317b32016-09-29 15:34:43 -0700504static void vc4_hdmi_set_infoframes(struct drm_encoder *encoder)
505{
Dave Stevenson6ac1c752020-09-03 10:01:38 +0200506 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
507
Eric Anholt21317b32016-09-29 15:34:43 -0700508 vc4_hdmi_set_avi_infoframe(encoder);
509 vc4_hdmi_set_spd_infoframe(encoder);
Dave Stevenson6ac1c752020-09-03 10:01:38 +0200510 /*
511 * If audio was streaming, then we need to reenabled the audio
512 * infoframe here during encoder_enable.
513 */
514 if (vc4_hdmi->audio.streaming)
515 vc4_hdmi_set_audio_infoframe(encoder);
Dave Stevensonbccd5c52021-04-30 11:44:49 +0200516
517 vc4_hdmi_set_hdr_infoframe(encoder);
Eric Anholt21317b32016-09-29 15:34:43 -0700518}
519
Maxime Ripardc85695a2021-05-07 17:05:13 +0200520static bool vc4_hdmi_supports_scrambling(struct drm_encoder *encoder,
521 struct drm_display_mode *mode)
522{
523 struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
524 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
525 struct drm_display_info *display = &vc4_hdmi->connector.display_info;
526
527 if (!vc4_encoder->hdmi_monitor)
528 return false;
529
530 if (!display->hdmi.scdc.supported ||
531 !display->hdmi.scdc.scrambling.supported)
532 return false;
533
534 return true;
535}
536
Maxime Ripard257d36d2021-05-07 17:05:14 +0200537#define SCRAMBLING_POLLING_DELAY_MS 1000
538
Maxime Ripardc85695a2021-05-07 17:05:13 +0200539static void vc4_hdmi_enable_scrambling(struct drm_encoder *encoder)
540{
Linus Torvalds31ad37b2021-09-19 10:11:53 -0700541 struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
Maxime Ripardc85695a2021-05-07 17:05:13 +0200542 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
543
544 if (!vc4_hdmi_supports_scrambling(encoder, mode))
545 return;
546
547 if (!vc4_hdmi_mode_needs_scrambling(mode))
548 return;
549
550 drm_scdc_set_high_tmds_clock_ratio(vc4_hdmi->ddc, true);
551 drm_scdc_set_scrambling(vc4_hdmi->ddc, true);
552
553 HDMI_WRITE(HDMI_SCRAMBLER_CTL, HDMI_READ(HDMI_SCRAMBLER_CTL) |
554 VC5_HDMI_SCRAMBLER_CTL_ENABLE);
Maxime Ripard257d36d2021-05-07 17:05:14 +0200555
556 queue_delayed_work(system_wq, &vc4_hdmi->scrambling_work,
557 msecs_to_jiffies(SCRAMBLING_POLLING_DELAY_MS));
Maxime Ripardc85695a2021-05-07 17:05:13 +0200558}
559
560static void vc4_hdmi_disable_scrambling(struct drm_encoder *encoder)
561{
562 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
Linus Torvalds31ad37b2021-09-19 10:11:53 -0700563 struct drm_crtc *crtc = encoder->crtc;
Maxime Ripardc85695a2021-05-07 17:05:13 +0200564
565 /*
Linus Torvalds31ad37b2021-09-19 10:11:53 -0700566 * At boot, encoder->crtc will be NULL. Since we don't know the
Maxime Ripardc85695a2021-05-07 17:05:13 +0200567 * state of the scrambler and in order to avoid any
568 * inconsistency, let's disable it all the time.
569 */
Linus Torvalds31ad37b2021-09-19 10:11:53 -0700570 if (crtc && !vc4_hdmi_supports_scrambling(encoder, &crtc->mode))
Maxime Ripardc85695a2021-05-07 17:05:13 +0200571 return;
572
Linus Torvalds31ad37b2021-09-19 10:11:53 -0700573 if (crtc && !vc4_hdmi_mode_needs_scrambling(&crtc->mode))
Maxime Ripardc85695a2021-05-07 17:05:13 +0200574 return;
575
Maxime Ripard257d36d2021-05-07 17:05:14 +0200576 if (delayed_work_pending(&vc4_hdmi->scrambling_work))
577 cancel_delayed_work_sync(&vc4_hdmi->scrambling_work);
578
Maxime Ripardc85695a2021-05-07 17:05:13 +0200579 HDMI_WRITE(HDMI_SCRAMBLER_CTL, HDMI_READ(HDMI_SCRAMBLER_CTL) &
580 ~VC5_HDMI_SCRAMBLER_CTL_ENABLE);
581
582 drm_scdc_set_scrambling(vc4_hdmi->ddc, false);
583 drm_scdc_set_high_tmds_clock_ratio(vc4_hdmi->ddc, false);
584}
585
Maxime Ripard257d36d2021-05-07 17:05:14 +0200586static void vc4_hdmi_scrambling_wq(struct work_struct *work)
587{
588 struct vc4_hdmi *vc4_hdmi = container_of(to_delayed_work(work),
589 struct vc4_hdmi,
590 scrambling_work);
591
592 if (drm_scdc_get_scrambling_status(vc4_hdmi->ddc))
593 return;
594
595 drm_scdc_set_high_tmds_clock_ratio(vc4_hdmi->ddc, true);
596 drm_scdc_set_scrambling(vc4_hdmi->ddc, true);
597
598 queue_delayed_work(system_wq, &vc4_hdmi->scrambling_work,
599 msecs_to_jiffies(SCRAMBLING_POLLING_DELAY_MS));
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800600}
601
Maxime Ripard8d914742020-12-15 16:42:36 +0100602static void vc4_hdmi_encoder_post_crtc_disable(struct drm_encoder *encoder,
603 struct drm_atomic_state *state)
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800604{
Maxime Ripard09c43812020-09-03 10:01:44 +0200605 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
606
607 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG, 0);
Maxime Ripard81d83012020-09-03 10:01:46 +0200608
Tim Gover0b066a62021-06-28 15:05:33 +0200609 HDMI_WRITE(HDMI_VID_CTL, HDMI_READ(HDMI_VID_CTL) | VC4_HD_VID_CTL_CLRRGB);
610
611 mdelay(1);
Maxime Ripard81d83012020-09-03 10:01:46 +0200612
613 HDMI_WRITE(HDMI_VID_CTL,
Tim Gover0b066a62021-06-28 15:05:33 +0200614 HDMI_READ(HDMI_VID_CTL) & ~VC4_HD_VID_CTL_ENABLE);
Maxime Ripardc85695a2021-05-07 17:05:13 +0200615 vc4_hdmi_disable_scrambling(encoder);
Maxime Ripard09c43812020-09-03 10:01:44 +0200616}
617
Maxime Ripard8d914742020-12-15 16:42:36 +0100618static void vc4_hdmi_encoder_post_crtc_powerdown(struct drm_encoder *encoder,
619 struct drm_atomic_state *state)
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800620{
Maxime Ripard5dfbcae2020-09-03 10:01:17 +0200621 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200622 int ret;
623
Tim Gover0b066a62021-06-28 15:05:33 +0200624 HDMI_WRITE(HDMI_VID_CTL,
625 HDMI_READ(HDMI_VID_CTL) | VC4_HD_VID_CTL_BLANKPIX);
626
Maxime Ripardc457b8a2020-09-03 10:01:25 +0200627 if (vc4_hdmi->variant->phy_disable)
628 vc4_hdmi->variant->phy_disable(vc4_hdmi);
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200629
Hoegeun Kwon37387422020-09-03 10:01:47 +0200630 clk_disable_unprepare(vc4_hdmi->pixel_bvb_clock);
Linus Torvaldsb1044a92021-09-19 10:06:46 -0700631 clk_disable_unprepare(vc4_hdmi->hsm_clock);
Maxime Ripard3408cc22020-09-03 10:01:14 +0200632 clk_disable_unprepare(vc4_hdmi->pixel_clock);
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200633
Maxime Ripard3408cc22020-09-03 10:01:14 +0200634 ret = pm_runtime_put(&vc4_hdmi->pdev->dev);
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200635 if (ret < 0)
636 DRM_ERROR("Failed to release power domain: %d\n", ret);
637}
638
Maxime Ripard09c43812020-09-03 10:01:44 +0200639static void vc4_hdmi_encoder_disable(struct drm_encoder *encoder)
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200640{
Maxime Ripard09c43812020-09-03 10:01:44 +0200641}
642
Maxime Ripard89f31a22020-09-03 10:01:27 +0200643static void vc4_hdmi_csc_setup(struct vc4_hdmi *vc4_hdmi, bool enable)
644{
645 u32 csc_ctl;
646
647 csc_ctl = VC4_SET_FIELD(VC4_HD_CSC_CTL_ORDER_BGR,
648 VC4_HD_CSC_CTL_ORDER);
649
650 if (enable) {
651 /* CEA VICs other than #1 requre limited range RGB
652 * output unless overridden by an AVI infoframe.
653 * Apply a colorspace conversion to squash 0-255 down
654 * to 16-235. The matrix here is:
655 *
656 * [ 0 0 0.8594 16]
657 * [ 0 0.8594 0 16]
658 * [ 0.8594 0 0 16]
659 * [ 0 0 0 1]
660 */
661 csc_ctl |= VC4_HD_CSC_CTL_ENABLE;
662 csc_ctl |= VC4_HD_CSC_CTL_RGB2YCC;
663 csc_ctl |= VC4_SET_FIELD(VC4_HD_CSC_CTL_MODE_CUSTOM,
664 VC4_HD_CSC_CTL_MODE);
665
666 HDMI_WRITE(HDMI_CSC_12_11, (0x000 << 16) | 0x000);
667 HDMI_WRITE(HDMI_CSC_14_13, (0x100 << 16) | 0x6e0);
668 HDMI_WRITE(HDMI_CSC_22_21, (0x6e0 << 16) | 0x000);
669 HDMI_WRITE(HDMI_CSC_24_23, (0x100 << 16) | 0x000);
670 HDMI_WRITE(HDMI_CSC_32_31, (0x000 << 16) | 0x6e0);
671 HDMI_WRITE(HDMI_CSC_34_33, (0x100 << 16) | 0x000);
672 }
673
674 /* The RGB order applies even when CSC is disabled. */
675 HDMI_WRITE(HDMI_CSC_CTL, csc_ctl);
676}
677
Maxime Ripard83239892020-09-03 10:01:48 +0200678static void vc5_hdmi_csc_setup(struct vc4_hdmi *vc4_hdmi, bool enable)
679{
680 u32 csc_ctl;
681
682 csc_ctl = 0x07; /* RGB_CONVERT_MODE = custom matrix, || USE_RGB_TO_YCBCR */
683
684 if (enable) {
685 /* CEA VICs other than #1 requre limited range RGB
686 * output unless overridden by an AVI infoframe.
687 * Apply a colorspace conversion to squash 0-255 down
688 * to 16-235. The matrix here is:
689 *
690 * [ 0.8594 0 0 16]
691 * [ 0 0.8594 0 16]
692 * [ 0 0 0.8594 16]
693 * [ 0 0 0 1]
694 * Matrix is signed 2p13 fixed point, with signed 9p6 offsets
695 */
696 HDMI_WRITE(HDMI_CSC_12_11, (0x0000 << 16) | 0x1b80);
697 HDMI_WRITE(HDMI_CSC_14_13, (0x0400 << 16) | 0x0000);
698 HDMI_WRITE(HDMI_CSC_22_21, (0x1b80 << 16) | 0x0000);
699 HDMI_WRITE(HDMI_CSC_24_23, (0x0400 << 16) | 0x0000);
700 HDMI_WRITE(HDMI_CSC_32_31, (0x0000 << 16) | 0x0000);
701 HDMI_WRITE(HDMI_CSC_34_33, (0x0400 << 16) | 0x1b80);
702 } else {
703 /* Still use the matrix for full range, but make it unity.
704 * Matrix is signed 2p13 fixed point, with signed 9p6 offsets
705 */
706 HDMI_WRITE(HDMI_CSC_12_11, (0x0000 << 16) | 0x2000);
707 HDMI_WRITE(HDMI_CSC_14_13, (0x0000 << 16) | 0x0000);
708 HDMI_WRITE(HDMI_CSC_22_21, (0x2000 << 16) | 0x0000);
709 HDMI_WRITE(HDMI_CSC_24_23, (0x0000 << 16) | 0x0000);
710 HDMI_WRITE(HDMI_CSC_32_31, (0x0000 << 16) | 0x0000);
711 HDMI_WRITE(HDMI_CSC_34_33, (0x0000 << 16) | 0x2000);
712 }
713
714 HDMI_WRITE(HDMI_CSC_CTL, csc_ctl);
715}
716
Maxime Ripard904f6682020-09-03 10:01:28 +0200717static void vc4_hdmi_set_timings(struct vc4_hdmi *vc4_hdmi,
Maxime Ripardba8c0fa2020-12-15 16:42:43 +0100718 struct drm_connector_state *state,
Maxime Ripard904f6682020-09-03 10:01:28 +0200719 struct drm_display_mode *mode)
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200720{
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800721 bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
722 bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
Eric Anholt682e62c2016-09-28 17:30:25 -0700723 bool interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE;
Eric Anholtdfccd932016-09-29 15:34:44 -0700724 u32 pixel_rep = (mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1;
Eric Anholt682e62c2016-09-28 17:30:25 -0700725 u32 verta = (VC4_SET_FIELD(mode->crtc_vsync_end - mode->crtc_vsync_start,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800726 VC4_HDMI_VERTA_VSP) |
Eric Anholt682e62c2016-09-28 17:30:25 -0700727 VC4_SET_FIELD(mode->crtc_vsync_start - mode->crtc_vdisplay,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800728 VC4_HDMI_VERTA_VFP) |
Eric Anholt682e62c2016-09-28 17:30:25 -0700729 VC4_SET_FIELD(mode->crtc_vdisplay, VC4_HDMI_VERTA_VAL));
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800730 u32 vertb = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) |
Eric Anholt682e62c2016-09-28 17:30:25 -0700731 VC4_SET_FIELD(mode->crtc_vtotal - mode->crtc_vsync_end,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800732 VC4_HDMI_VERTB_VBP));
Eric Anholt682e62c2016-09-28 17:30:25 -0700733 u32 vertb_even = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) |
734 VC4_SET_FIELD(mode->crtc_vtotal -
735 mode->crtc_vsync_end -
736 interlaced,
737 VC4_HDMI_VERTB_VBP));
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200738
Maxime Ripard904f6682020-09-03 10:01:28 +0200739 HDMI_WRITE(HDMI_HORZA,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800740 (vsync_pos ? VC4_HDMI_HORZA_VPOS : 0) |
741 (hsync_pos ? VC4_HDMI_HORZA_HPOS : 0) |
Eric Anholtdfccd932016-09-29 15:34:44 -0700742 VC4_SET_FIELD(mode->hdisplay * pixel_rep,
743 VC4_HDMI_HORZA_HAP));
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800744
Maxime Ripard904f6682020-09-03 10:01:28 +0200745 HDMI_WRITE(HDMI_HORZB,
Eric Anholtdfccd932016-09-29 15:34:44 -0700746 VC4_SET_FIELD((mode->htotal -
747 mode->hsync_end) * pixel_rep,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800748 VC4_HDMI_HORZB_HBP) |
Eric Anholtdfccd932016-09-29 15:34:44 -0700749 VC4_SET_FIELD((mode->hsync_end -
750 mode->hsync_start) * pixel_rep,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800751 VC4_HDMI_HORZB_HSP) |
Eric Anholtdfccd932016-09-29 15:34:44 -0700752 VC4_SET_FIELD((mode->hsync_start -
753 mode->hdisplay) * pixel_rep,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800754 VC4_HDMI_HORZB_HFP));
755
Maxime Ripard904f6682020-09-03 10:01:28 +0200756 HDMI_WRITE(HDMI_VERTA0, verta);
757 HDMI_WRITE(HDMI_VERTA1, verta);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800758
Maxime Ripard904f6682020-09-03 10:01:28 +0200759 HDMI_WRITE(HDMI_VERTB0, vertb_even);
760 HDMI_WRITE(HDMI_VERTB1, vertb);
Maxime Ripard904f6682020-09-03 10:01:28 +0200761}
Maxime Ripardba8c0fa2020-12-15 16:42:43 +0100762
Maxime Ripard83239892020-09-03 10:01:48 +0200763static void vc5_hdmi_set_timings(struct vc4_hdmi *vc4_hdmi,
Maxime Ripardba8c0fa2020-12-15 16:42:43 +0100764 struct drm_connector_state *state,
Maxime Ripard83239892020-09-03 10:01:48 +0200765 struct drm_display_mode *mode)
766{
767 bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
768 bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
769 bool interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE;
770 u32 pixel_rep = (mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1;
771 u32 verta = (VC4_SET_FIELD(mode->crtc_vsync_end - mode->crtc_vsync_start,
772 VC5_HDMI_VERTA_VSP) |
773 VC4_SET_FIELD(mode->crtc_vsync_start - mode->crtc_vdisplay,
774 VC5_HDMI_VERTA_VFP) |
775 VC4_SET_FIELD(mode->crtc_vdisplay, VC5_HDMI_VERTA_VAL));
776 u32 vertb = (VC4_SET_FIELD(0, VC5_HDMI_VERTB_VSPO) |
777 VC4_SET_FIELD(mode->crtc_vtotal - mode->crtc_vsync_end,
778 VC4_HDMI_VERTB_VBP));
779 u32 vertb_even = (VC4_SET_FIELD(0, VC5_HDMI_VERTB_VSPO) |
780 VC4_SET_FIELD(mode->crtc_vtotal -
781 mode->crtc_vsync_end -
782 interlaced,
783 VC4_HDMI_VERTB_VBP));
Maxime Ripardba8c0fa2020-12-15 16:42:43 +0100784 unsigned char gcp;
785 bool gcp_en;
786 u32 reg;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800787
Maxime Ripard83239892020-09-03 10:01:48 +0200788 HDMI_WRITE(HDMI_VEC_INTERFACE_XBAR, 0x354021);
789 HDMI_WRITE(HDMI_HORZA,
790 (vsync_pos ? VC5_HDMI_HORZA_VPOS : 0) |
791 (hsync_pos ? VC5_HDMI_HORZA_HPOS : 0) |
792 VC4_SET_FIELD(mode->hdisplay * pixel_rep,
793 VC5_HDMI_HORZA_HAP) |
794 VC4_SET_FIELD((mode->hsync_start -
795 mode->hdisplay) * pixel_rep,
796 VC5_HDMI_HORZA_HFP));
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800797
Maxime Ripard83239892020-09-03 10:01:48 +0200798 HDMI_WRITE(HDMI_HORZB,
799 VC4_SET_FIELD((mode->htotal -
800 mode->hsync_end) * pixel_rep,
801 VC5_HDMI_HORZB_HBP) |
802 VC4_SET_FIELD((mode->hsync_end -
803 mode->hsync_start) * pixel_rep,
804 VC5_HDMI_HORZB_HSP));
Eric Anholt6e1cbba2016-09-16 10:59:45 +0100805
Maxime Ripard83239892020-09-03 10:01:48 +0200806 HDMI_WRITE(HDMI_VERTA0, verta);
807 HDMI_WRITE(HDMI_VERTA1, verta);
Eric Anholt6e1cbba2016-09-16 10:59:45 +0100808
Maxime Ripard83239892020-09-03 10:01:48 +0200809 HDMI_WRITE(HDMI_VERTB0, vertb_even);
810 HDMI_WRITE(HDMI_VERTB1, vertb);
Eric Anholt6e1cbba2016-09-16 10:59:45 +0100811
Maxime Ripardba8c0fa2020-12-15 16:42:43 +0100812 switch (state->max_bpc) {
813 case 12:
814 gcp = 6;
815 gcp_en = true;
816 break;
817 case 10:
818 gcp = 5;
819 gcp_en = true;
820 break;
821 case 8:
822 default:
823 gcp = 4;
824 gcp_en = false;
825 break;
826 }
827
828 reg = HDMI_READ(HDMI_DEEP_COLOR_CONFIG_1);
829 reg &= ~(VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE_MASK |
830 VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH_MASK);
831 reg |= VC4_SET_FIELD(2, VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE) |
832 VC4_SET_FIELD(gcp, VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH);
833 HDMI_WRITE(HDMI_DEEP_COLOR_CONFIG_1, reg);
834
835 reg = HDMI_READ(HDMI_GCP_WORD_1);
836 reg &= ~VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1_MASK;
837 reg |= VC4_SET_FIELD(gcp, VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1);
838 HDMI_WRITE(HDMI_GCP_WORD_1, reg);
839
840 reg = HDMI_READ(HDMI_GCP_CONFIG);
841 reg &= ~VC5_HDMI_GCP_CONFIG_GCP_ENABLE;
842 reg |= gcp_en ? VC5_HDMI_GCP_CONFIG_GCP_ENABLE : 0;
843 HDMI_WRITE(HDMI_GCP_CONFIG, reg);
844
Maxime Ripard83239892020-09-03 10:01:48 +0200845 HDMI_WRITE(HDMI_CLOCK_STOP, 0);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800846}
847
Maxime Ripard691456f2020-09-03 10:01:43 +0200848static void vc4_hdmi_recenter_fifo(struct vc4_hdmi *vc4_hdmi)
Eric Anholt32e823c2017-09-20 15:59:34 -0700849{
Maxime Ripard691456f2020-09-03 10:01:43 +0200850 u32 drift;
851 int ret;
852
853 drift = HDMI_READ(HDMI_FIFO_CTL);
854 drift &= VC4_HDMI_FIFO_VALID_WRITE_MASK;
855
856 HDMI_WRITE(HDMI_FIFO_CTL,
857 drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
858 HDMI_WRITE(HDMI_FIFO_CTL,
859 drift | VC4_HDMI_FIFO_CTL_RECENTER);
860 usleep_range(1000, 1100);
861 HDMI_WRITE(HDMI_FIFO_CTL,
862 drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
863 HDMI_WRITE(HDMI_FIFO_CTL,
864 drift | VC4_HDMI_FIFO_CTL_RECENTER);
865
866 ret = wait_for(HDMI_READ(HDMI_FIFO_CTL) &
867 VC4_HDMI_FIFO_CTL_RECENTER_DONE, 1);
868 WARN_ONCE(ret, "Timeout waiting for "
869 "VC4_HDMI_FIFO_CTL_RECENTER_DONE");
870}
871
Maxime Ripardf6237462020-12-15 16:42:40 +0100872static struct drm_connector_state *
873vc4_hdmi_encoder_get_connector_state(struct drm_encoder *encoder,
874 struct drm_atomic_state *state)
875{
876 struct drm_connector_state *conn_state;
877 struct drm_connector *connector;
878 unsigned int i;
879
880 for_each_new_connector_in_state(state, connector, conn_state, i) {
881 if (conn_state->best_encoder == encoder)
882 return conn_state;
883 }
884
885 return NULL;
886}
887
Maxime Ripard8d914742020-12-15 16:42:36 +0100888static void vc4_hdmi_encoder_pre_crtc_configure(struct drm_encoder *encoder,
889 struct drm_atomic_state *state)
Maxime Ripard904f6682020-09-03 10:01:28 +0200890{
Maxime Ripardf6237462020-12-15 16:42:40 +0100891 struct drm_connector_state *conn_state =
892 vc4_hdmi_encoder_get_connector_state(encoder, state);
893 struct vc4_hdmi_connector_state *vc4_conn_state =
894 conn_state_to_vc4_hdmi_conn_state(conn_state);
Linus Torvalds31ad37b2021-09-19 10:11:53 -0700895 struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
Maxime Ripard904f6682020-09-03 10:01:28 +0200896 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
Maxime Ripard7d9061e2021-05-07 17:05:11 +0200897 unsigned long bvb_rate, pixel_rate, hsm_rate;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800898 int ret;
899
Zou Wei5e4322a2021-05-24 15:20:54 +0800900 ret = pm_runtime_resume_and_get(&vc4_hdmi->pdev->dev);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800901 if (ret < 0) {
902 DRM_ERROR("Failed to retain power domain: %d\n", ret);
903 return;
904 }
905
Maxime Ripardf6237462020-12-15 16:42:40 +0100906 pixel_rate = vc4_conn_state->pixel_rate;
Maxime Ripardcd4cb492020-09-03 10:01:35 +0200907 ret = clk_set_rate(vc4_hdmi->pixel_clock, pixel_rate);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800908 if (ret) {
909 DRM_ERROR("Failed to set pixel clock rate: %d\n", ret);
910 return;
911 }
912
Maxime Ripard3408cc22020-09-03 10:01:14 +0200913 ret = clk_prepare_enable(vc4_hdmi->pixel_clock);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800914 if (ret) {
915 DRM_ERROR("Failed to turn on pixel clock: %d\n", ret);
916 return;
917 }
918
Nicolas Saenz Julienneb1e73962020-03-26 13:20:01 +0100919 /*
920 * As stated in RPi's vc4 firmware "HDMI state machine (HSM) clock must
921 * be faster than pixel clock, infinitesimally faster, tested in
922 * simulation. Otherwise, exact value is unimportant for HDMI
923 * operation." This conflicts with bcm2835's vc4 documentation, which
924 * states HSM's clock has to be at least 108% of the pixel clock.
925 *
926 * Real life tests reveal that vc4's firmware statement holds up, and
927 * users are able to use pixel clocks closer to HSM's, namely for
928 * 1920x1200@60Hz. So it was decided to have leave a 1% margin between
929 * both clocks. Which, for RPi0-3 implies a maximum pixel clock of
930 * 162MHz.
931 *
932 * Additionally, the AXI clock needs to be at least 25% of
933 * pixel clock, but HSM ends up being the limiting factor.
Eric Anholt32e823c2017-09-20 15:59:34 -0700934 */
Maxime Ripardcd4cb492020-09-03 10:01:35 +0200935 hsm_rate = max_t(unsigned long, 120000000, (pixel_rate / 100) * 101);
Maxime Ripardd5d5ce82020-09-03 10:01:36 +0200936 ret = clk_set_min_rate(vc4_hdmi->hsm_clock, hsm_rate);
Maxime Ripardcd4cb492020-09-03 10:01:35 +0200937 if (ret) {
938 DRM_ERROR("Failed to set HSM clock rate: %d\n", ret);
939 return;
940 }
941
Linus Torvaldsb1044a92021-09-19 10:06:46 -0700942 ret = clk_prepare_enable(vc4_hdmi->hsm_clock);
943 if (ret) {
944 DRM_ERROR("Failed to turn on HSM clock: %d\n", ret);
945 clk_disable_unprepare(vc4_hdmi->pixel_clock);
946 return;
947 }
948
Maxime Ripard47fa9a82021-01-11 15:23:01 +0100949 vc4_hdmi_cec_update_clk_div(vc4_hdmi);
950
Maxime Ripard7d9061e2021-05-07 17:05:11 +0200951 if (pixel_rate > 297000000)
952 bvb_rate = 300000000;
953 else if (pixel_rate > 148500000)
954 bvb_rate = 150000000;
955 else
956 bvb_rate = 75000000;
957
958 ret = clk_set_min_rate(vc4_hdmi->pixel_bvb_clock, bvb_rate);
Hoegeun Kwon37387422020-09-03 10:01:47 +0200959 if (ret) {
960 DRM_ERROR("Failed to set pixel bvb clock rate: %d\n", ret);
Linus Torvaldsb1044a92021-09-19 10:06:46 -0700961 clk_disable_unprepare(vc4_hdmi->hsm_clock);
Hoegeun Kwon37387422020-09-03 10:01:47 +0200962 clk_disable_unprepare(vc4_hdmi->pixel_clock);
963 return;
964 }
965
966 ret = clk_prepare_enable(vc4_hdmi->pixel_bvb_clock);
967 if (ret) {
968 DRM_ERROR("Failed to turn on pixel bvb clock: %d\n", ret);
Linus Torvaldsb1044a92021-09-19 10:06:46 -0700969 clk_disable_unprepare(vc4_hdmi->hsm_clock);
Hoegeun Kwon37387422020-09-03 10:01:47 +0200970 clk_disable_unprepare(vc4_hdmi->pixel_clock);
971 return;
972 }
973
Maxime Ripardc457b8a2020-09-03 10:01:25 +0200974 if (vc4_hdmi->variant->phy_init)
Maxime Ripardd2a7dd02020-12-15 16:42:41 +0100975 vc4_hdmi->variant->phy_init(vc4_hdmi, vc4_conn_state);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800976
Maxime Ripard311e3052020-09-03 10:01:23 +0200977 HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
978 HDMI_READ(HDMI_SCHEDULER_CONTROL) |
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800979 VC4_HDMI_SCHEDULER_CONTROL_MANUAL_FORMAT |
980 VC4_HDMI_SCHEDULER_CONTROL_IGNORE_VSYNC_PREDICTS);
981
Maxime Ripard904f6682020-09-03 10:01:28 +0200982 if (vc4_hdmi->variant->set_timings)
Maxime Ripardba8c0fa2020-12-15 16:42:43 +0100983 vc4_hdmi->variant->set_timings(vc4_hdmi, conn_state, mode);
Maxime Ripard09c43812020-09-03 10:01:44 +0200984}
985
Maxime Ripard8d914742020-12-15 16:42:36 +0100986static void vc4_hdmi_encoder_pre_crtc_enable(struct drm_encoder *encoder,
987 struct drm_atomic_state *state)
Maxime Ripard09c43812020-09-03 10:01:44 +0200988{
Linus Torvalds31ad37b2021-09-19 10:11:53 -0700989 struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
Maxime Ripard09c43812020-09-03 10:01:44 +0200990 struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
991 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800992
993 if (vc4_encoder->hdmi_monitor &&
Maxime Ripard89f31a22020-09-03 10:01:27 +0200994 drm_default_rgb_quant_range(mode) == HDMI_QUANTIZATION_RANGE_LIMITED) {
995 if (vc4_hdmi->variant->csc_setup)
996 vc4_hdmi->variant->csc_setup(vc4_hdmi, true);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800997
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800998 vc4_encoder->limited_rgb_range = true;
999 } else {
Maxime Ripard89f31a22020-09-03 10:01:27 +02001000 if (vc4_hdmi->variant->csc_setup)
1001 vc4_hdmi->variant->csc_setup(vc4_hdmi, false);
1002
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001003 vc4_encoder->limited_rgb_range = false;
1004 }
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001005
Maxime Ripard311e3052020-09-03 10:01:23 +02001006 HDMI_WRITE(HDMI_FIFO_CTL, VC4_HDMI_FIFO_CTL_MASTER_SLAVE_N);
Maxime Ripard09c43812020-09-03 10:01:44 +02001007}
1008
Maxime Ripard8d914742020-12-15 16:42:36 +01001009static void vc4_hdmi_encoder_post_crtc_enable(struct drm_encoder *encoder,
1010 struct drm_atomic_state *state)
Maxime Ripard09c43812020-09-03 10:01:44 +02001011{
Linus Torvalds31ad37b2021-09-19 10:11:53 -07001012 struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
Maxime Ripard09c43812020-09-03 10:01:44 +02001013 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1014 struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
Maxime Ripard8b3f90e2020-09-03 10:01:45 +02001015 bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
1016 bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
Maxime Ripard09c43812020-09-03 10:01:44 +02001017 int ret;
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001018
Maxime Ripard311e3052020-09-03 10:01:23 +02001019 HDMI_WRITE(HDMI_VID_CTL,
Maxime Ripard311e3052020-09-03 10:01:23 +02001020 VC4_HD_VID_CTL_ENABLE |
Tim Gover0b066a62021-06-28 15:05:33 +02001021 VC4_HD_VID_CTL_CLRRGB |
Maxime Ripard311e3052020-09-03 10:01:23 +02001022 VC4_HD_VID_CTL_UNDERFLOW_ENABLE |
Maxime Ripard8b3f90e2020-09-03 10:01:45 +02001023 VC4_HD_VID_CTL_FRAME_COUNTER_RESET |
1024 (vsync_pos ? 0 : VC4_HD_VID_CTL_VSYNC_LOW) |
1025 (hsync_pos ? 0 : VC4_HD_VID_CTL_HSYNC_LOW));
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001026
Maxime Ripard81d83012020-09-03 10:01:46 +02001027 HDMI_WRITE(HDMI_VID_CTL,
1028 HDMI_READ(HDMI_VID_CTL) & ~VC4_HD_VID_CTL_BLANKPIX);
1029
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001030 if (vc4_encoder->hdmi_monitor) {
Maxime Ripard311e3052020-09-03 10:01:23 +02001031 HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
1032 HDMI_READ(HDMI_SCHEDULER_CONTROL) |
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001033 VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);
1034
Maxime Ripard311e3052020-09-03 10:01:23 +02001035 ret = wait_for(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001036 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE, 1000);
1037 WARN_ONCE(ret, "Timeout waiting for "
1038 "VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
1039 } else {
Maxime Ripard311e3052020-09-03 10:01:23 +02001040 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
1041 HDMI_READ(HDMI_RAM_PACKET_CONFIG) &
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001042 ~(VC4_HDMI_RAM_PACKET_ENABLE));
Maxime Ripard311e3052020-09-03 10:01:23 +02001043 HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
1044 HDMI_READ(HDMI_SCHEDULER_CONTROL) &
Eric Anholt851479a2016-02-12 14:15:14 -08001045 ~VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);
1046
Maxime Ripard311e3052020-09-03 10:01:23 +02001047 ret = wait_for(!(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
Eric Anholt851479a2016-02-12 14:15:14 -08001048 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE), 1000);
1049 WARN_ONCE(ret, "Timeout waiting for "
1050 "!VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
1051 }
1052
1053 if (vc4_encoder->hdmi_monitor) {
Maxime Ripard311e3052020-09-03 10:01:23 +02001054 WARN_ON(!(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001055 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE));
Maxime Ripard311e3052020-09-03 10:01:23 +02001056 HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
1057 HDMI_READ(HDMI_SCHEDULER_CONTROL) |
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001058 VC4_HDMI_SCHEDULER_CONTROL_VERT_ALWAYS_KEEPOUT);
1059
Maxime Ripard311e3052020-09-03 10:01:23 +02001060 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001061 VC4_HDMI_RAM_PACKET_ENABLE);
1062
Eric Anholt0b06e0a2016-02-29 17:53:01 -08001063 vc4_hdmi_set_infoframes(encoder);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001064 }
Maxime Ripard691456f2020-09-03 10:01:43 +02001065
1066 vc4_hdmi_recenter_fifo(vc4_hdmi);
Maxime Ripardc85695a2021-05-07 17:05:13 +02001067 vc4_hdmi_enable_scrambling(encoder);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001068}
1069
Maxime Ripard09c43812020-09-03 10:01:44 +02001070static void vc4_hdmi_encoder_enable(struct drm_encoder *encoder)
1071{
1072}
1073
Maxime Ripard9fa1d7e2020-10-29 14:40:17 +01001074#define WIFI_2_4GHz_CH1_MIN_FREQ 2400000000ULL
1075#define WIFI_2_4GHz_CH1_MAX_FREQ 2422000000ULL
1076
Maxime Ripard63495f6b2020-10-29 13:25:21 +01001077static int vc4_hdmi_encoder_atomic_check(struct drm_encoder *encoder,
1078 struct drm_crtc_state *crtc_state,
1079 struct drm_connector_state *conn_state)
1080{
Maxime Ripardf6237462020-12-15 16:42:40 +01001081 struct vc4_hdmi_connector_state *vc4_state = conn_state_to_vc4_hdmi_conn_state(conn_state);
Maxime Ripard63495f6b2020-10-29 13:25:21 +01001082 struct drm_display_mode *mode = &crtc_state->adjusted_mode;
1083 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1084 unsigned long long pixel_rate = mode->clock * 1000;
Maxime Ripard9fa1d7e2020-10-29 14:40:17 +01001085 unsigned long long tmds_rate;
Maxime Ripard63495f6b2020-10-29 13:25:21 +01001086
Maxime Ripard57fb32e2020-10-29 13:25:22 +01001087 if (vc4_hdmi->variant->unsupported_odd_h_timings &&
1088 ((mode->hdisplay % 2) || (mode->hsync_start % 2) ||
1089 (mode->hsync_end % 2) || (mode->htotal % 2)))
1090 return -EINVAL;
1091
Maxime Ripard9fa1d7e2020-10-29 14:40:17 +01001092 /*
1093 * The 1440p@60 pixel rate is in the same range than the first
1094 * WiFi channel (between 2.4GHz and 2.422GHz with 22MHz
1095 * bandwidth). Slightly lower the frequency to bring it out of
1096 * the WiFi range.
1097 */
1098 tmds_rate = pixel_rate * 10;
1099 if (vc4_hdmi->disable_wifi_frequencies &&
1100 (tmds_rate >= WIFI_2_4GHz_CH1_MIN_FREQ &&
1101 tmds_rate <= WIFI_2_4GHz_CH1_MAX_FREQ)) {
1102 mode->clock = 238560;
1103 pixel_rate = mode->clock * 1000;
1104 }
1105
Maxime Ripardba8c0fa2020-12-15 16:42:43 +01001106 if (conn_state->max_bpc == 12) {
1107 pixel_rate = pixel_rate * 150;
1108 do_div(pixel_rate, 100);
1109 } else if (conn_state->max_bpc == 10) {
1110 pixel_rate = pixel_rate * 125;
1111 do_div(pixel_rate, 100);
1112 }
1113
Maxime Ripard320e84d2020-12-15 16:42:37 +01001114 if (mode->flags & DRM_MODE_FLAG_DBLCLK)
1115 pixel_rate = pixel_rate * 2;
1116
Maxime Ripard63495f6b2020-10-29 13:25:21 +01001117 if (pixel_rate > vc4_hdmi->variant->max_pixel_clock)
1118 return -EINVAL;
1119
Maxime Ripard86e3a652021-05-07 17:05:12 +02001120 if (vc4_hdmi->disable_4kp60 && (pixel_rate > HDMI_14_MAX_TMDS_CLK))
1121 return -EINVAL;
1122
Maxime Ripardf6237462020-12-15 16:42:40 +01001123 vc4_state->pixel_rate = pixel_rate;
1124
Maxime Ripard63495f6b2020-10-29 13:25:21 +01001125 return 0;
1126}
1127
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001128static enum drm_mode_status
Maxime Ripard11a17312020-09-03 10:01:34 +02001129vc4_hdmi_encoder_mode_valid(struct drm_encoder *encoder,
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001130 const struct drm_display_mode *mode)
1131{
Maxime Ripardcd4cb492020-09-03 10:01:35 +02001132 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1133
Maxime Ripard57fb32e2020-10-29 13:25:22 +01001134 if (vc4_hdmi->variant->unsupported_odd_h_timings &&
1135 ((mode->hdisplay % 2) || (mode->hsync_start % 2) ||
1136 (mode->hsync_end % 2) || (mode->htotal % 2)))
1137 return MODE_H_ILLEGAL;
1138
Maxime Ripardcd4cb492020-09-03 10:01:35 +02001139 if ((mode->clock * 1000) > vc4_hdmi->variant->max_pixel_clock)
Eric Anholt32e823c2017-09-20 15:59:34 -07001140 return MODE_CLOCK_HIGH;
1141
Maxime Ripard86e3a652021-05-07 17:05:12 +02001142 if (vc4_hdmi->disable_4kp60 && vc4_hdmi_mode_needs_scrambling(mode))
1143 return MODE_CLOCK_HIGH;
1144
Eric Anholt32e823c2017-09-20 15:59:34 -07001145 return MODE_OK;
1146}
1147
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001148static const struct drm_encoder_helper_funcs vc4_hdmi_encoder_helper_funcs = {
Maxime Ripard63495f6b2020-10-29 13:25:21 +01001149 .atomic_check = vc4_hdmi_encoder_atomic_check,
Eric Anholt32e823c2017-09-20 15:59:34 -07001150 .mode_valid = vc4_hdmi_encoder_mode_valid,
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001151 .disable = vc4_hdmi_encoder_disable,
1152 .enable = vc4_hdmi_encoder_enable,
1153};
1154
Dave Stevenson632ee3a2020-09-03 10:01:40 +02001155static u32 vc4_hdmi_channel_map(struct vc4_hdmi *vc4_hdmi, u32 channel_mask)
Eric Anholtbb7d7852017-02-27 12:28:02 -08001156{
Dave Stevenson632ee3a2020-09-03 10:01:40 +02001157 int i;
1158 u32 channel_map = 0;
1159
1160 for (i = 0; i < 8; i++) {
1161 if (channel_mask & BIT(i))
1162 channel_map |= i << (3 * i);
1163 }
1164 return channel_map;
1165}
1166
Maxime Ripard83239892020-09-03 10:01:48 +02001167static u32 vc5_hdmi_channel_map(struct vc4_hdmi *vc4_hdmi, u32 channel_mask)
1168{
1169 int i;
1170 u32 channel_map = 0;
1171
1172 for (i = 0; i < 8; i++) {
1173 if (channel_mask & BIT(i))
1174 channel_map |= i << (4 * i);
1175 }
1176 return channel_map;
1177}
1178
Eric Anholtbb7d7852017-02-27 12:28:02 -08001179/* HDMI audio codec callbacks */
Maxime Ripardf1437782021-07-07 11:36:31 +02001180static void vc4_hdmi_audio_set_mai_clock(struct vc4_hdmi *vc4_hdmi,
1181 unsigned int samplerate)
Eric Anholtbb7d7852017-02-27 12:28:02 -08001182{
Dave Stevenson632ee3a2020-09-03 10:01:40 +02001183 u32 hsm_clock = clk_get_rate(vc4_hdmi->audio_clock);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001184 unsigned long n, m;
1185
Maxime Ripardf1437782021-07-07 11:36:31 +02001186 rational_best_approximation(hsm_clock, samplerate,
Eric Anholtbb7d7852017-02-27 12:28:02 -08001187 VC4_HD_MAI_SMP_N_MASK >>
1188 VC4_HD_MAI_SMP_N_SHIFT,
1189 (VC4_HD_MAI_SMP_M_MASK >>
1190 VC4_HD_MAI_SMP_M_SHIFT) + 1,
1191 &n, &m);
1192
Maxime Ripard311e3052020-09-03 10:01:23 +02001193 HDMI_WRITE(HDMI_MAI_SMP,
1194 VC4_SET_FIELD(n, VC4_HD_MAI_SMP_N) |
1195 VC4_SET_FIELD(m - 1, VC4_HD_MAI_SMP_M));
Eric Anholtbb7d7852017-02-27 12:28:02 -08001196}
1197
Maxime Ripardf1437782021-07-07 11:36:31 +02001198static void vc4_hdmi_set_n_cts(struct vc4_hdmi *vc4_hdmi, unsigned int samplerate)
Eric Anholtbb7d7852017-02-27 12:28:02 -08001199{
Linus Torvalds31ad37b2021-09-19 10:11:53 -07001200 struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
1201 struct drm_crtc *crtc = encoder->crtc;
Eric Anholtbb7d7852017-02-27 12:28:02 -08001202 const struct drm_display_mode *mode = &crtc->state->adjusted_mode;
Eric Anholtbb7d7852017-02-27 12:28:02 -08001203 u32 n, cts;
1204 u64 tmp;
1205
1206 n = 128 * samplerate / 1000;
1207 tmp = (u64)(mode->clock * 1000) * n;
1208 do_div(tmp, 128 * samplerate);
1209 cts = tmp;
1210
Maxime Ripard311e3052020-09-03 10:01:23 +02001211 HDMI_WRITE(HDMI_CRP_CFG,
Eric Anholtbb7d7852017-02-27 12:28:02 -08001212 VC4_HDMI_CRP_CFG_EXTERNAL_CTS_EN |
1213 VC4_SET_FIELD(n, VC4_HDMI_CRP_CFG_N));
1214
1215 /*
1216 * We could get slightly more accurate clocks in some cases by
1217 * providing a CTS_1 value. The two CTS values are alternated
1218 * between based on the period fields
1219 */
Maxime Ripard311e3052020-09-03 10:01:23 +02001220 HDMI_WRITE(HDMI_CTS_0, cts);
1221 HDMI_WRITE(HDMI_CTS_1, cts);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001222}
1223
1224static inline struct vc4_hdmi *dai_to_hdmi(struct snd_soc_dai *dai)
1225{
1226 struct snd_soc_card *card = snd_soc_dai_get_drvdata(dai);
1227
1228 return snd_soc_card_get_drvdata(card);
1229}
1230
Maxime Ripard91e99e12021-05-25 15:23:52 +02001231static int vc4_hdmi_audio_startup(struct device *dev, void *data)
Eric Anholtbb7d7852017-02-27 12:28:02 -08001232{
Maxime Ripard91e99e12021-05-25 15:23:52 +02001233 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
Linus Torvalds31ad37b2021-09-19 10:11:53 -07001234 struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
Eric Anholtbb7d7852017-02-27 12:28:02 -08001235
1236 /*
1237 * If the HDMI encoder hasn't probed, or the encoder is
1238 * currently in DVI mode, treat the codec dai as missing.
1239 */
Linus Torvalds31ad37b2021-09-19 10:11:53 -07001240 if (!encoder->crtc || !(HDMI_READ(HDMI_RAM_PACKET_CONFIG) &
Eric Anholtbb7d7852017-02-27 12:28:02 -08001241 VC4_HDMI_RAM_PACKET_ENABLE))
1242 return -ENODEV;
1243
Maxime Ripard91e99e12021-05-25 15:23:52 +02001244 vc4_hdmi->audio.streaming = true;
Eric Anholtbb7d7852017-02-27 12:28:02 -08001245
Maxime Ripard91e99e12021-05-25 15:23:52 +02001246 HDMI_WRITE(HDMI_MAI_CTL,
1247 VC4_HD_MAI_CTL_RESET |
1248 VC4_HD_MAI_CTL_FLUSH |
1249 VC4_HD_MAI_CTL_DLATE |
1250 VC4_HD_MAI_CTL_ERRORE |
1251 VC4_HD_MAI_CTL_ERRORF);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001252
Maxime Ripard91e99e12021-05-25 15:23:52 +02001253 if (vc4_hdmi->variant->phy_rng_enable)
1254 vc4_hdmi->variant->phy_rng_enable(vc4_hdmi);
1255
Eric Anholtbb7d7852017-02-27 12:28:02 -08001256 return 0;
1257}
1258
Maxime Ripard3408cc22020-09-03 10:01:14 +02001259static void vc4_hdmi_audio_reset(struct vc4_hdmi *vc4_hdmi)
Eric Anholtbb7d7852017-02-27 12:28:02 -08001260{
Maxime Ripard3408cc22020-09-03 10:01:14 +02001261 struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
Maxime Ripard3408cc22020-09-03 10:01:14 +02001262 struct device *dev = &vc4_hdmi->pdev->dev;
Eric Anholtbb7d7852017-02-27 12:28:02 -08001263 int ret;
1264
Dave Stevenson6ac1c752020-09-03 10:01:38 +02001265 vc4_hdmi->audio.streaming = false;
Maxime Riparde2f9b2e2020-12-03 08:46:24 +01001266 ret = vc4_hdmi_stop_packet(encoder, HDMI_INFOFRAME_TYPE_AUDIO, false);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001267 if (ret)
1268 dev_err(dev, "Failed to stop audio infoframe: %d\n", ret);
1269
Maxime Ripard311e3052020-09-03 10:01:23 +02001270 HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_RESET);
1271 HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_ERRORF);
1272 HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_FLUSH);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001273}
1274
Maxime Ripard91e99e12021-05-25 15:23:52 +02001275static void vc4_hdmi_audio_shutdown(struct device *dev, void *data)
Eric Anholtbb7d7852017-02-27 12:28:02 -08001276{
Maxime Ripard91e99e12021-05-25 15:23:52 +02001277 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001278
Maxime Ripard311e3052020-09-03 10:01:23 +02001279 HDMI_WRITE(HDMI_MAI_CTL,
Maxime Ripard311e3052020-09-03 10:01:23 +02001280 VC4_HD_MAI_CTL_DLATE |
1281 VC4_HD_MAI_CTL_ERRORE |
1282 VC4_HD_MAI_CTL_ERRORF);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001283
Maxime Ripard91e99e12021-05-25 15:23:52 +02001284 if (vc4_hdmi->variant->phy_rng_disable)
1285 vc4_hdmi->variant->phy_rng_disable(vc4_hdmi);
1286
1287 vc4_hdmi->audio.streaming = false;
Eric Anholtbb7d7852017-02-27 12:28:02 -08001288 vc4_hdmi_audio_reset(vc4_hdmi);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001289}
1290
Dom Cobley82bd6072021-05-25 15:23:49 +02001291static int sample_rate_to_mai_fmt(int samplerate)
1292{
1293 switch (samplerate) {
1294 case 8000:
1295 return VC4_HDMI_MAI_SAMPLE_RATE_8000;
1296 case 11025:
1297 return VC4_HDMI_MAI_SAMPLE_RATE_11025;
1298 case 12000:
1299 return VC4_HDMI_MAI_SAMPLE_RATE_12000;
1300 case 16000:
1301 return VC4_HDMI_MAI_SAMPLE_RATE_16000;
1302 case 22050:
1303 return VC4_HDMI_MAI_SAMPLE_RATE_22050;
1304 case 24000:
1305 return VC4_HDMI_MAI_SAMPLE_RATE_24000;
1306 case 32000:
1307 return VC4_HDMI_MAI_SAMPLE_RATE_32000;
1308 case 44100:
1309 return VC4_HDMI_MAI_SAMPLE_RATE_44100;
1310 case 48000:
1311 return VC4_HDMI_MAI_SAMPLE_RATE_48000;
1312 case 64000:
1313 return VC4_HDMI_MAI_SAMPLE_RATE_64000;
1314 case 88200:
1315 return VC4_HDMI_MAI_SAMPLE_RATE_88200;
1316 case 96000:
1317 return VC4_HDMI_MAI_SAMPLE_RATE_96000;
1318 case 128000:
1319 return VC4_HDMI_MAI_SAMPLE_RATE_128000;
1320 case 176400:
1321 return VC4_HDMI_MAI_SAMPLE_RATE_176400;
1322 case 192000:
1323 return VC4_HDMI_MAI_SAMPLE_RATE_192000;
1324 default:
1325 return VC4_HDMI_MAI_SAMPLE_RATE_NOT_INDICATED;
1326 }
1327}
1328
Eric Anholtbb7d7852017-02-27 12:28:02 -08001329/* HDMI audio codec callbacks */
Maxime Ripard91e99e12021-05-25 15:23:52 +02001330static int vc4_hdmi_audio_prepare(struct device *dev, void *data,
1331 struct hdmi_codec_daifmt *daifmt,
1332 struct hdmi_codec_params *params)
Eric Anholtbb7d7852017-02-27 12:28:02 -08001333{
Maxime Ripard91e99e12021-05-25 15:23:52 +02001334 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001335 struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
Maxime Ripardf1437782021-07-07 11:36:31 +02001336 unsigned int sample_rate = params->sample_rate;
1337 unsigned int channels = params->channels;
Eric Anholtbb7d7852017-02-27 12:28:02 -08001338 u32 audio_packet_config, channel_mask;
1339 u32 channel_map;
Dom Cobley82bd6072021-05-25 15:23:49 +02001340 u32 mai_audio_format;
1341 u32 mai_sample_rate;
Eric Anholtbb7d7852017-02-27 12:28:02 -08001342
Eric Anholtbb7d7852017-02-27 12:28:02 -08001343 dev_dbg(dev, "%s: %u Hz, %d bit, %d channels\n", __func__,
Maxime Ripardf1437782021-07-07 11:36:31 +02001344 sample_rate, params->sample_width, channels);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001345
1346 HDMI_WRITE(HDMI_MAI_CTL,
Maxime Ripardf1437782021-07-07 11:36:31 +02001347 VC4_SET_FIELD(channels, VC4_HD_MAI_CTL_CHNUM) |
Maxime Ripard91e99e12021-05-25 15:23:52 +02001348 VC4_HD_MAI_CTL_WHOLSMP |
1349 VC4_HD_MAI_CTL_CHALIGN |
1350 VC4_HD_MAI_CTL_ENABLE);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001351
Maxime Ripardf1437782021-07-07 11:36:31 +02001352 vc4_hdmi_audio_set_mai_clock(vc4_hdmi, sample_rate);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001353
Maxime Ripardf1437782021-07-07 11:36:31 +02001354 mai_sample_rate = sample_rate_to_mai_fmt(sample_rate);
Maxime Ripard91e99e12021-05-25 15:23:52 +02001355 if (params->iec.status[0] & IEC958_AES0_NONAUDIO &&
1356 params->channels == 8)
1357 mai_audio_format = VC4_HDMI_MAI_FORMAT_HBR;
1358 else
1359 mai_audio_format = VC4_HDMI_MAI_FORMAT_PCM;
Dom Cobley82bd6072021-05-25 15:23:49 +02001360 HDMI_WRITE(HDMI_MAI_FMT,
1361 VC4_SET_FIELD(mai_sample_rate,
1362 VC4_HDMI_MAI_FORMAT_SAMPLE_RATE) |
1363 VC4_SET_FIELD(mai_audio_format,
1364 VC4_HDMI_MAI_FORMAT_AUDIO_FORMAT));
1365
Dave Stevensonb9b8bac2020-09-03 10:01:39 +02001366 /* The B frame identifier should match the value used by alsa-lib (8) */
Eric Anholtbb7d7852017-02-27 12:28:02 -08001367 audio_packet_config =
1368 VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_SAMPLE_FLAT |
1369 VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_INACTIVE_CHANNELS |
Dave Stevensonb9b8bac2020-09-03 10:01:39 +02001370 VC4_SET_FIELD(0x8, VC4_HDMI_AUDIO_PACKET_B_FRAME_IDENTIFIER);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001371
Maxime Ripardf1437782021-07-07 11:36:31 +02001372 channel_mask = GENMASK(channels - 1, 0);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001373 audio_packet_config |= VC4_SET_FIELD(channel_mask,
1374 VC4_HDMI_AUDIO_PACKET_CEA_MASK);
1375
Dom Cobley84341112021-05-25 15:23:51 +02001376 /* Set the MAI threshold */
1377 HDMI_WRITE(HDMI_MAI_THR,
1378 VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_PANICHIGH) |
1379 VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_PANICLOW) |
1380 VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_DREQHIGH) |
1381 VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_DREQLOW));
Eric Anholtbb7d7852017-02-27 12:28:02 -08001382
Maxime Ripard311e3052020-09-03 10:01:23 +02001383 HDMI_WRITE(HDMI_MAI_CONFIG,
Eric Anholtbb7d7852017-02-27 12:28:02 -08001384 VC4_HDMI_MAI_CONFIG_BIT_REVERSE |
Dom Cobley9a8fd2772021-05-25 15:23:50 +02001385 VC4_HDMI_MAI_CONFIG_FORMAT_REVERSE |
Eric Anholtbb7d7852017-02-27 12:28:02 -08001386 VC4_SET_FIELD(channel_mask, VC4_HDMI_MAI_CHANNEL_MASK));
1387
Dave Stevenson632ee3a2020-09-03 10:01:40 +02001388 channel_map = vc4_hdmi->variant->channel_map(vc4_hdmi, channel_mask);
Maxime Ripard311e3052020-09-03 10:01:23 +02001389 HDMI_WRITE(HDMI_MAI_CHANNEL_MAP, channel_map);
1390 HDMI_WRITE(HDMI_AUDIO_PACKET_CONFIG, audio_packet_config);
Maxime Ripardf1437782021-07-07 11:36:31 +02001391 vc4_hdmi_set_n_cts(vc4_hdmi, sample_rate);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001392
Maxime Ripard91e99e12021-05-25 15:23:52 +02001393 memcpy(&vc4_hdmi->audio.infoframe, &params->cea, sizeof(params->cea));
Maxime Ripard58d04362020-10-27 11:15:58 +01001394 vc4_hdmi_set_audio_infoframe(encoder);
1395
Eric Anholtbb7d7852017-02-27 12:28:02 -08001396 return 0;
1397}
1398
Eric Anholtbb7d7852017-02-27 12:28:02 -08001399static const struct snd_soc_dapm_widget vc4_hdmi_audio_widgets[] = {
1400 SND_SOC_DAPM_OUTPUT("TX"),
1401};
1402
1403static const struct snd_soc_dapm_route vc4_hdmi_audio_routes[] = {
1404 { "TX", NULL, "Playback" },
1405};
1406
Eric Anholtbb7d7852017-02-27 12:28:02 -08001407static const struct snd_soc_component_driver vc4_hdmi_audio_cpu_dai_comp = {
1408 .name = "vc4-hdmi-cpu-dai-component",
1409};
1410
1411static int vc4_hdmi_audio_cpu_dai_probe(struct snd_soc_dai *dai)
1412{
Maxime Ripard3408cc22020-09-03 10:01:14 +02001413 struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001414
Maxime Ripard3408cc22020-09-03 10:01:14 +02001415 snd_soc_dai_init_dma_data(dai, &vc4_hdmi->audio.dma_data, NULL);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001416
1417 return 0;
1418}
1419
1420static struct snd_soc_dai_driver vc4_hdmi_audio_cpu_dai_drv = {
1421 .name = "vc4-hdmi-cpu-dai",
1422 .probe = vc4_hdmi_audio_cpu_dai_probe,
1423 .playback = {
1424 .stream_name = "Playback",
1425 .channels_min = 1,
1426 .channels_max = 8,
1427 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
1428 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
1429 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
1430 SNDRV_PCM_RATE_192000,
1431 .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
1432 },
Eric Anholtbb7d7852017-02-27 12:28:02 -08001433};
1434
1435static const struct snd_dmaengine_pcm_config pcm_conf = {
1436 .chan_names[SNDRV_PCM_STREAM_PLAYBACK] = "audio-rx",
1437 .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
1438};
1439
Maxime Ripard91e99e12021-05-25 15:23:52 +02001440static int vc4_hdmi_audio_get_eld(struct device *dev, void *data,
1441 uint8_t *buf, size_t len)
1442{
1443 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
1444 struct drm_connector *connector = &vc4_hdmi->connector;
1445
1446 memcpy(buf, connector->eld, min(sizeof(connector->eld), len));
1447
1448 return 0;
1449}
1450
1451static const struct hdmi_codec_ops vc4_hdmi_codec_ops = {
1452 .get_eld = vc4_hdmi_audio_get_eld,
1453 .prepare = vc4_hdmi_audio_prepare,
1454 .audio_shutdown = vc4_hdmi_audio_shutdown,
1455 .audio_startup = vc4_hdmi_audio_startup,
1456};
1457
Jiapeng Chong17d3d3a2021-07-30 18:26:34 +08001458static struct hdmi_codec_pdata vc4_hdmi_codec_pdata = {
Maxime Ripard91e99e12021-05-25 15:23:52 +02001459 .ops = &vc4_hdmi_codec_ops,
1460 .max_i2s_channels = 8,
1461 .i2s = 1,
1462};
1463
Maxime Ripard3408cc22020-09-03 10:01:14 +02001464static int vc4_hdmi_audio_init(struct vc4_hdmi *vc4_hdmi)
Eric Anholtbb7d7852017-02-27 12:28:02 -08001465{
Maxime Ripard311e3052020-09-03 10:01:23 +02001466 const struct vc4_hdmi_register *mai_data =
1467 &vc4_hdmi->variant->registers[HDMI_MAI_DATA];
Maxime Ripard3408cc22020-09-03 10:01:14 +02001468 struct snd_soc_dai_link *dai_link = &vc4_hdmi->audio.link;
1469 struct snd_soc_card *card = &vc4_hdmi->audio.card;
1470 struct device *dev = &vc4_hdmi->pdev->dev;
Maxime Ripard91e99e12021-05-25 15:23:52 +02001471 struct platform_device *codec_pdev;
Eric Anholtbb7d7852017-02-27 12:28:02 -08001472 const __be32 *addr;
Dave Stevenson094864b2020-09-03 10:01:37 +02001473 int index;
Eric Anholtbb7d7852017-02-27 12:28:02 -08001474 int ret;
1475
1476 if (!of_find_property(dev->of_node, "dmas", NULL)) {
1477 dev_warn(dev,
1478 "'dmas' DT property is missing, no HDMI audio\n");
1479 return 0;
1480 }
1481
Maxime Ripard311e3052020-09-03 10:01:23 +02001482 if (mai_data->reg != VC4_HD) {
1483 WARN_ONCE(true, "MAI isn't in the HD block\n");
1484 return -EINVAL;
1485 }
1486
Eric Anholtbb7d7852017-02-27 12:28:02 -08001487 /*
1488 * Get the physical address of VC4_HD_MAI_DATA. We need to retrieve
1489 * the bus address specified in the DT, because the physical address
1490 * (the one returned by platform_get_resource()) is not appropriate
1491 * for DMA transfers.
1492 * This VC/MMU should probably be exposed to avoid this kind of hacks.
1493 */
Dave Stevenson094864b2020-09-03 10:01:37 +02001494 index = of_property_match_string(dev->of_node, "reg-names", "hd");
1495 /* Before BCM2711, we don't have a named register range */
1496 if (index < 0)
1497 index = 1;
1498
1499 addr = of_get_address(dev->of_node, index, NULL, NULL);
1500
Maxime Ripard311e3052020-09-03 10:01:23 +02001501 vc4_hdmi->audio.dma_data.addr = be32_to_cpup(addr) + mai_data->offset;
Maxime Ripard3408cc22020-09-03 10:01:14 +02001502 vc4_hdmi->audio.dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1503 vc4_hdmi->audio.dma_data.maxburst = 2;
Eric Anholtbb7d7852017-02-27 12:28:02 -08001504
1505 ret = devm_snd_dmaengine_pcm_register(dev, &pcm_conf, 0);
1506 if (ret) {
1507 dev_err(dev, "Could not register PCM component: %d\n", ret);
1508 return ret;
1509 }
1510
1511 ret = devm_snd_soc_register_component(dev, &vc4_hdmi_audio_cpu_dai_comp,
1512 &vc4_hdmi_audio_cpu_dai_drv, 1);
1513 if (ret) {
1514 dev_err(dev, "Could not register CPU DAI: %d\n", ret);
1515 return ret;
1516 }
1517
Maxime Ripard91e99e12021-05-25 15:23:52 +02001518 codec_pdev = platform_device_register_data(dev, HDMI_CODEC_DRV_NAME,
1519 PLATFORM_DEVID_AUTO,
1520 &vc4_hdmi_codec_pdata,
1521 sizeof(vc4_hdmi_codec_pdata));
1522 if (IS_ERR(codec_pdev)) {
1523 dev_err(dev, "Couldn't register the HDMI codec: %ld\n", PTR_ERR(codec_pdev));
1524 return PTR_ERR(codec_pdev);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001525 }
1526
Maxime Ripard3408cc22020-09-03 10:01:14 +02001527 dai_link->cpus = &vc4_hdmi->audio.cpu;
1528 dai_link->codecs = &vc4_hdmi->audio.codec;
1529 dai_link->platforms = &vc4_hdmi->audio.platform;
Kuninori Morimoto0467d8e2019-06-06 13:19:19 +09001530
1531 dai_link->num_cpus = 1;
1532 dai_link->num_codecs = 1;
Kuninori Morimoto8a90efd2019-06-28 10:46:14 +09001533 dai_link->num_platforms = 1;
Kuninori Morimoto0467d8e2019-06-06 13:19:19 +09001534
Eric Anholtbb7d7852017-02-27 12:28:02 -08001535 dai_link->name = "MAI";
1536 dai_link->stream_name = "MAI PCM";
Maxime Ripard91e99e12021-05-25 15:23:52 +02001537 dai_link->codecs->dai_name = "i2s-hifi";
Kuninori Morimoto0467d8e2019-06-06 13:19:19 +09001538 dai_link->cpus->dai_name = dev_name(dev);
Maxime Ripard91e99e12021-05-25 15:23:52 +02001539 dai_link->codecs->name = dev_name(&codec_pdev->dev);
Kuninori Morimoto8a90efd2019-06-28 10:46:14 +09001540 dai_link->platforms->name = dev_name(dev);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001541
1542 card->dai_link = dai_link;
1543 card->num_links = 1;
Maxime Ripard9be43a52020-09-03 10:01:41 +02001544 card->name = vc4_hdmi->variant->card_name;
Nicolas Saenz Julienne33c74532021-01-15 20:12:09 +01001545 card->driver_name = "vc4-hdmi";
Eric Anholtbb7d7852017-02-27 12:28:02 -08001546 card->dev = dev;
Marek Szyprowskiec653df2020-07-01 09:39:49 +02001547 card->owner = THIS_MODULE;
Eric Anholtbb7d7852017-02-27 12:28:02 -08001548
1549 /*
1550 * Be careful, snd_soc_register_card() calls dev_set_drvdata() and
1551 * stores a pointer to the snd card object in dev->driver_data. This
1552 * means we cannot use it for something else. The hdmi back-pointer is
1553 * now stored in card->drvdata and should be retrieved with
1554 * snd_soc_card_get_drvdata() if needed.
1555 */
Maxime Ripard3408cc22020-09-03 10:01:14 +02001556 snd_soc_card_set_drvdata(card, vc4_hdmi);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001557 ret = devm_snd_soc_register_card(dev, card);
Kuninori Morimoto635b1c12018-01-29 04:35:04 +00001558 if (ret)
Nicolas Saenz Julienne9d9fb752021-06-29 14:17:23 +02001559 dev_err_probe(dev, ret, "Could not register sound card\n");
Eric Anholtbb7d7852017-02-27 12:28:02 -08001560
1561 return ret;
Eric Anholtbb7d7852017-02-27 12:28:02 -08001562
Eric Anholtbb7d7852017-02-27 12:28:02 -08001563}
1564
Maxime Ripardf4790082021-05-24 15:20:18 +02001565static irqreturn_t vc4_hdmi_hpd_irq_thread(int irq, void *priv)
1566{
1567 struct vc4_hdmi *vc4_hdmi = priv;
Maxime Riparddaf4e7d2021-09-14 12:17:24 +02001568 struct drm_connector *connector = &vc4_hdmi->connector;
1569 struct drm_device *dev = connector->dev;
Maxime Ripardf4790082021-05-24 15:20:18 +02001570
Maxime Ripard44fe9f902021-07-07 11:51:12 +02001571 if (dev && dev->registered)
Maxime Riparddaf4e7d2021-09-14 12:17:24 +02001572 drm_connector_helper_hpd_irq_event(connector);
Maxime Ripardf4790082021-05-24 15:20:18 +02001573
1574 return IRQ_HANDLED;
1575}
1576
1577static int vc4_hdmi_hotplug_init(struct vc4_hdmi *vc4_hdmi)
1578{
1579 struct drm_connector *connector = &vc4_hdmi->connector;
1580 struct platform_device *pdev = vc4_hdmi->pdev;
Maxime Ripardf4790082021-05-24 15:20:18 +02001581 int ret;
1582
1583 if (vc4_hdmi->variant->external_irq_controller) {
Maxime Ripard776efe82021-07-07 11:51:11 +02001584 unsigned int hpd_con = platform_get_irq_byname(pdev, "hpd-connected");
1585 unsigned int hpd_rm = platform_get_irq_byname(pdev, "hpd-removed");
1586
1587 ret = request_threaded_irq(hpd_con,
1588 NULL,
1589 vc4_hdmi_hpd_irq_thread, IRQF_ONESHOT,
1590 "vc4 hdmi hpd connected", vc4_hdmi);
Maxime Ripardf4790082021-05-24 15:20:18 +02001591 if (ret)
1592 return ret;
1593
Maxime Ripard776efe82021-07-07 11:51:11 +02001594 ret = request_threaded_irq(hpd_rm,
1595 NULL,
1596 vc4_hdmi_hpd_irq_thread, IRQF_ONESHOT,
1597 "vc4 hdmi hpd disconnected", vc4_hdmi);
1598 if (ret) {
1599 free_irq(hpd_con, vc4_hdmi);
Maxime Ripardf4790082021-05-24 15:20:18 +02001600 return ret;
Maxime Ripard776efe82021-07-07 11:51:11 +02001601 }
Maxime Ripardf4790082021-05-24 15:20:18 +02001602
1603 connector->polled = DRM_CONNECTOR_POLL_HPD;
1604 }
1605
1606 return 0;
1607}
1608
Maxime Ripard776efe82021-07-07 11:51:11 +02001609static void vc4_hdmi_hotplug_exit(struct vc4_hdmi *vc4_hdmi)
1610{
1611 struct platform_device *pdev = vc4_hdmi->pdev;
1612
1613 if (vc4_hdmi->variant->external_irq_controller) {
1614 free_irq(platform_get_irq_byname(pdev, "hpd-connected"), vc4_hdmi);
1615 free_irq(platform_get_irq_byname(pdev, "hpd-removed"), vc4_hdmi);
1616 }
1617}
1618
Hans Verkuil15b45112017-07-16 12:48:04 +02001619#ifdef CONFIG_DRM_VC4_HDMI_CEC
Maxime Riparded4a6bb2021-01-11 15:23:03 +01001620static irqreturn_t vc4_cec_irq_handler_rx_thread(int irq, void *priv)
Hans Verkuil15b45112017-07-16 12:48:04 +02001621{
Maxime Ripard66bf1c32020-09-03 10:01:18 +02001622 struct vc4_hdmi *vc4_hdmi = priv;
Hans Verkuil15b45112017-07-16 12:48:04 +02001623
Maxime Riparded4a6bb2021-01-11 15:23:03 +01001624 if (vc4_hdmi->cec_rx_msg.len)
1625 cec_received_msg(vc4_hdmi->cec_adap,
1626 &vc4_hdmi->cec_rx_msg);
1627
1628 return IRQ_HANDLED;
1629}
1630
1631static irqreturn_t vc4_cec_irq_handler_tx_thread(int irq, void *priv)
1632{
1633 struct vc4_hdmi *vc4_hdmi = priv;
1634
1635 if (vc4_hdmi->cec_tx_ok) {
Maxime Ripard3408cc22020-09-03 10:01:14 +02001636 cec_transmit_done(vc4_hdmi->cec_adap, CEC_TX_STATUS_OK,
Hans Verkuil15b45112017-07-16 12:48:04 +02001637 0, 0, 0, 0);
1638 } else {
1639 /*
1640 * This CEC implementation makes 1 retry, so if we
1641 * get a NACK, then that means it made 2 attempts.
1642 */
Maxime Ripard3408cc22020-09-03 10:01:14 +02001643 cec_transmit_done(vc4_hdmi->cec_adap, CEC_TX_STATUS_NACK,
Hans Verkuil15b45112017-07-16 12:48:04 +02001644 0, 2, 0, 0);
1645 }
1646 return IRQ_HANDLED;
1647}
1648
Maxime Riparded4a6bb2021-01-11 15:23:03 +01001649static irqreturn_t vc4_cec_irq_handler_thread(int irq, void *priv)
1650{
1651 struct vc4_hdmi *vc4_hdmi = priv;
1652 irqreturn_t ret;
1653
1654 if (vc4_hdmi->cec_irq_was_rx)
1655 ret = vc4_cec_irq_handler_rx_thread(irq, priv);
1656 else
1657 ret = vc4_cec_irq_handler_tx_thread(irq, priv);
1658
1659 return ret;
1660}
1661
Maxime Ripard66bf1c32020-09-03 10:01:18 +02001662static void vc4_cec_read_msg(struct vc4_hdmi *vc4_hdmi, u32 cntrl1)
Hans Verkuil15b45112017-07-16 12:48:04 +02001663{
Dom Cobley4a59ed52021-01-11 15:22:57 +01001664 struct drm_device *dev = vc4_hdmi->connector.dev;
Maxime Ripard13311452020-09-03 10:01:15 +02001665 struct cec_msg *msg = &vc4_hdmi->cec_rx_msg;
Hans Verkuil15b45112017-07-16 12:48:04 +02001666 unsigned int i;
1667
1668 msg->len = 1 + ((cntrl1 & VC4_HDMI_CEC_REC_WRD_CNT_MASK) >>
1669 VC4_HDMI_CEC_REC_WRD_CNT_SHIFT);
Dom Cobley4a59ed52021-01-11 15:22:57 +01001670
1671 if (msg->len > 16) {
1672 drm_err(dev, "Attempting to read too much data (%d)\n", msg->len);
1673 return;
1674 }
1675
Hans Verkuil15b45112017-07-16 12:48:04 +02001676 for (i = 0; i < msg->len; i += 4) {
Dom Cobley4a59ed52021-01-11 15:22:57 +01001677 u32 val = HDMI_READ(HDMI_CEC_RX_DATA_1 + (i >> 2));
Hans Verkuil15b45112017-07-16 12:48:04 +02001678
1679 msg->msg[i] = val & 0xff;
1680 msg->msg[i + 1] = (val >> 8) & 0xff;
1681 msg->msg[i + 2] = (val >> 16) & 0xff;
1682 msg->msg[i + 3] = (val >> 24) & 0xff;
1683 }
1684}
1685
Maxime Riparded4a6bb2021-01-11 15:23:03 +01001686static irqreturn_t vc4_cec_irq_handler_tx_bare(int irq, void *priv)
1687{
1688 struct vc4_hdmi *vc4_hdmi = priv;
1689 u32 cntrl1;
1690
1691 cntrl1 = HDMI_READ(HDMI_CEC_CNTRL_1);
1692 vc4_hdmi->cec_tx_ok = cntrl1 & VC4_HDMI_CEC_TX_STATUS_GOOD;
1693 cntrl1 &= ~VC4_HDMI_CEC_START_XMIT_BEGIN;
1694 HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1);
1695
1696 return IRQ_WAKE_THREAD;
1697}
1698
1699static irqreturn_t vc4_cec_irq_handler_rx_bare(int irq, void *priv)
1700{
1701 struct vc4_hdmi *vc4_hdmi = priv;
1702 u32 cntrl1;
1703
1704 vc4_hdmi->cec_rx_msg.len = 0;
1705 cntrl1 = HDMI_READ(HDMI_CEC_CNTRL_1);
1706 vc4_cec_read_msg(vc4_hdmi, cntrl1);
1707 cntrl1 |= VC4_HDMI_CEC_CLEAR_RECEIVE_OFF;
1708 HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1);
1709 cntrl1 &= ~VC4_HDMI_CEC_CLEAR_RECEIVE_OFF;
1710
1711 HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1);
1712
1713 return IRQ_WAKE_THREAD;
1714}
1715
Hans Verkuil15b45112017-07-16 12:48:04 +02001716static irqreturn_t vc4_cec_irq_handler(int irq, void *priv)
1717{
Maxime Ripard66bf1c32020-09-03 10:01:18 +02001718 struct vc4_hdmi *vc4_hdmi = priv;
Maxime Ripard311e3052020-09-03 10:01:23 +02001719 u32 stat = HDMI_READ(HDMI_CEC_CPU_STATUS);
Maxime Riparded4a6bb2021-01-11 15:23:03 +01001720 irqreturn_t ret;
1721 u32 cntrl5;
Hans Verkuil15b45112017-07-16 12:48:04 +02001722
1723 if (!(stat & VC4_HDMI_CPU_CEC))
1724 return IRQ_NONE;
Maxime Riparded4a6bb2021-01-11 15:23:03 +01001725
Maxime Ripard311e3052020-09-03 10:01:23 +02001726 cntrl5 = HDMI_READ(HDMI_CEC_CNTRL_5);
Maxime Ripard3408cc22020-09-03 10:01:14 +02001727 vc4_hdmi->cec_irq_was_rx = cntrl5 & VC4_HDMI_CEC_RX_CEC_INT;
Maxime Riparded4a6bb2021-01-11 15:23:03 +01001728 if (vc4_hdmi->cec_irq_was_rx)
1729 ret = vc4_cec_irq_handler_rx_bare(irq, priv);
1730 else
1731 ret = vc4_cec_irq_handler_tx_bare(irq, priv);
Hans Verkuil15b45112017-07-16 12:48:04 +02001732
Maxime Riparded4a6bb2021-01-11 15:23:03 +01001733 HDMI_WRITE(HDMI_CEC_CPU_CLEAR, VC4_HDMI_CPU_CEC);
1734 return ret;
Hans Verkuil15b45112017-07-16 12:48:04 +02001735}
1736
1737static int vc4_hdmi_cec_adap_enable(struct cec_adapter *adap, bool enable)
1738{
Maxime Ripard66bf1c32020-09-03 10:01:18 +02001739 struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
Hans Verkuil15b45112017-07-16 12:48:04 +02001740 /* clock period in microseconds */
1741 const u32 usecs = 1000000 / CEC_CLOCK_FREQ;
Maxime Ripard311e3052020-09-03 10:01:23 +02001742 u32 val = HDMI_READ(HDMI_CEC_CNTRL_5);
Hans Verkuil15b45112017-07-16 12:48:04 +02001743
1744 val &= ~(VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET |
1745 VC4_HDMI_CEC_CNT_TO_4700_US_MASK |
1746 VC4_HDMI_CEC_CNT_TO_4500_US_MASK);
1747 val |= ((4700 / usecs) << VC4_HDMI_CEC_CNT_TO_4700_US_SHIFT) |
1748 ((4500 / usecs) << VC4_HDMI_CEC_CNT_TO_4500_US_SHIFT);
1749
1750 if (enable) {
Maxime Ripard311e3052020-09-03 10:01:23 +02001751 HDMI_WRITE(HDMI_CEC_CNTRL_5, val |
Hans Verkuil15b45112017-07-16 12:48:04 +02001752 VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET);
Maxime Ripard311e3052020-09-03 10:01:23 +02001753 HDMI_WRITE(HDMI_CEC_CNTRL_5, val);
1754 HDMI_WRITE(HDMI_CEC_CNTRL_2,
1755 ((1500 / usecs) << VC4_HDMI_CEC_CNT_TO_1500_US_SHIFT) |
1756 ((1300 / usecs) << VC4_HDMI_CEC_CNT_TO_1300_US_SHIFT) |
1757 ((800 / usecs) << VC4_HDMI_CEC_CNT_TO_800_US_SHIFT) |
1758 ((600 / usecs) << VC4_HDMI_CEC_CNT_TO_600_US_SHIFT) |
1759 ((400 / usecs) << VC4_HDMI_CEC_CNT_TO_400_US_SHIFT));
1760 HDMI_WRITE(HDMI_CEC_CNTRL_3,
1761 ((2750 / usecs) << VC4_HDMI_CEC_CNT_TO_2750_US_SHIFT) |
1762 ((2400 / usecs) << VC4_HDMI_CEC_CNT_TO_2400_US_SHIFT) |
1763 ((2050 / usecs) << VC4_HDMI_CEC_CNT_TO_2050_US_SHIFT) |
1764 ((1700 / usecs) << VC4_HDMI_CEC_CNT_TO_1700_US_SHIFT));
1765 HDMI_WRITE(HDMI_CEC_CNTRL_4,
1766 ((4300 / usecs) << VC4_HDMI_CEC_CNT_TO_4300_US_SHIFT) |
1767 ((3900 / usecs) << VC4_HDMI_CEC_CNT_TO_3900_US_SHIFT) |
1768 ((3600 / usecs) << VC4_HDMI_CEC_CNT_TO_3600_US_SHIFT) |
1769 ((3500 / usecs) << VC4_HDMI_CEC_CNT_TO_3500_US_SHIFT));
Hans Verkuil15b45112017-07-16 12:48:04 +02001770
Maxime Ripard185e98b2021-01-11 15:23:04 +01001771 if (!vc4_hdmi->variant->external_irq_controller)
1772 HDMI_WRITE(HDMI_CEC_CPU_MASK_CLEAR, VC4_HDMI_CPU_CEC);
Hans Verkuil15b45112017-07-16 12:48:04 +02001773 } else {
Maxime Ripard185e98b2021-01-11 15:23:04 +01001774 if (!vc4_hdmi->variant->external_irq_controller)
1775 HDMI_WRITE(HDMI_CEC_CPU_MASK_SET, VC4_HDMI_CPU_CEC);
Maxime Ripard311e3052020-09-03 10:01:23 +02001776 HDMI_WRITE(HDMI_CEC_CNTRL_5, val |
Hans Verkuil15b45112017-07-16 12:48:04 +02001777 VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET);
1778 }
1779 return 0;
1780}
1781
1782static int vc4_hdmi_cec_adap_log_addr(struct cec_adapter *adap, u8 log_addr)
1783{
Maxime Ripard66bf1c32020-09-03 10:01:18 +02001784 struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
Hans Verkuil15b45112017-07-16 12:48:04 +02001785
Maxime Ripard311e3052020-09-03 10:01:23 +02001786 HDMI_WRITE(HDMI_CEC_CNTRL_1,
1787 (HDMI_READ(HDMI_CEC_CNTRL_1) & ~VC4_HDMI_CEC_ADDR_MASK) |
Hans Verkuil15b45112017-07-16 12:48:04 +02001788 (log_addr & 0xf) << VC4_HDMI_CEC_ADDR_SHIFT);
1789 return 0;
1790}
1791
1792static int vc4_hdmi_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
1793 u32 signal_free_time, struct cec_msg *msg)
1794{
Maxime Ripard66bf1c32020-09-03 10:01:18 +02001795 struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
Dom Cobley4a59ed52021-01-11 15:22:57 +01001796 struct drm_device *dev = vc4_hdmi->connector.dev;
Hans Verkuil15b45112017-07-16 12:48:04 +02001797 u32 val;
1798 unsigned int i;
1799
Dom Cobley4a59ed52021-01-11 15:22:57 +01001800 if (msg->len > 16) {
1801 drm_err(dev, "Attempting to transmit too much data (%d)\n", msg->len);
1802 return -ENOMEM;
1803 }
1804
Hans Verkuil15b45112017-07-16 12:48:04 +02001805 for (i = 0; i < msg->len; i += 4)
Dom Cobley4a59ed52021-01-11 15:22:57 +01001806 HDMI_WRITE(HDMI_CEC_TX_DATA_1 + (i >> 2),
Hans Verkuil15b45112017-07-16 12:48:04 +02001807 (msg->msg[i]) |
1808 (msg->msg[i + 1] << 8) |
1809 (msg->msg[i + 2] << 16) |
1810 (msg->msg[i + 3] << 24));
1811
Maxime Ripard311e3052020-09-03 10:01:23 +02001812 val = HDMI_READ(HDMI_CEC_CNTRL_1);
Hans Verkuil15b45112017-07-16 12:48:04 +02001813 val &= ~VC4_HDMI_CEC_START_XMIT_BEGIN;
Maxime Ripard311e3052020-09-03 10:01:23 +02001814 HDMI_WRITE(HDMI_CEC_CNTRL_1, val);
Hans Verkuil15b45112017-07-16 12:48:04 +02001815 val &= ~VC4_HDMI_CEC_MESSAGE_LENGTH_MASK;
1816 val |= (msg->len - 1) << VC4_HDMI_CEC_MESSAGE_LENGTH_SHIFT;
1817 val |= VC4_HDMI_CEC_START_XMIT_BEGIN;
1818
Maxime Ripard311e3052020-09-03 10:01:23 +02001819 HDMI_WRITE(HDMI_CEC_CNTRL_1, val);
Hans Verkuil15b45112017-07-16 12:48:04 +02001820 return 0;
1821}
1822
1823static const struct cec_adap_ops vc4_hdmi_cec_adap_ops = {
1824 .adap_enable = vc4_hdmi_cec_adap_enable,
1825 .adap_log_addr = vc4_hdmi_cec_adap_log_addr,
1826 .adap_transmit = vc4_hdmi_cec_adap_transmit,
1827};
Hans Verkuil15b45112017-07-16 12:48:04 +02001828
Maxime Ripardc0791e02020-09-03 10:01:31 +02001829static int vc4_hdmi_cec_init(struct vc4_hdmi *vc4_hdmi)
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001830{
Dariusz Marcinkiewicz66c2dee2019-08-23 13:24:25 +02001831 struct cec_connector_info conn_info;
Maxime Ripardc0791e02020-09-03 10:01:31 +02001832 struct platform_device *pdev = vc4_hdmi->pdev;
Maxime Ripardae442bf2021-01-11 15:23:06 +01001833 struct device *dev = &pdev->dev;
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001834 u32 value;
1835 int ret;
1836
Maxime Ripardae442bf2021-01-11 15:23:06 +01001837 if (!of_find_property(dev->of_node, "interrupts", NULL)) {
1838 dev_warn(dev, "'interrupts' DT property is missing, no CEC\n");
1839 return 0;
1840 }
1841
Maxime Ripardc0791e02020-09-03 10:01:31 +02001842 vc4_hdmi->cec_adap = cec_allocate_adapter(&vc4_hdmi_cec_adap_ops,
1843 vc4_hdmi, "vc4",
1844 CEC_CAP_DEFAULTS |
1845 CEC_CAP_CONNECTOR_INFO, 1);
1846 ret = PTR_ERR_OR_ZERO(vc4_hdmi->cec_adap);
Hans Verkuil15b45112017-07-16 12:48:04 +02001847 if (ret < 0)
Maxime Ripardc0791e02020-09-03 10:01:31 +02001848 return ret;
Dariusz Marcinkiewicz66c2dee2019-08-23 13:24:25 +02001849
Maxime Ripardc0791e02020-09-03 10:01:31 +02001850 cec_fill_conn_info_from_drm(&conn_info, &vc4_hdmi->connector);
1851 cec_s_conn_info(vc4_hdmi->cec_adap, &conn_info);
Dariusz Marcinkiewicz66c2dee2019-08-23 13:24:25 +02001852
Maxime Ripardc0791e02020-09-03 10:01:31 +02001853 value = HDMI_READ(HDMI_CEC_CNTRL_1);
Maxime Ripard47fa9a82021-01-11 15:23:01 +01001854 /* Set the logical address to Unregistered */
1855 value |= VC4_HDMI_CEC_ADDR_MASK;
Maxime Ripardc0791e02020-09-03 10:01:31 +02001856 HDMI_WRITE(HDMI_CEC_CNTRL_1, value);
Maxime Ripard47fa9a82021-01-11 15:23:01 +01001857
1858 vc4_hdmi_cec_update_clk_div(vc4_hdmi);
1859
Maxime Ripard185e98b2021-01-11 15:23:04 +01001860 if (vc4_hdmi->variant->external_irq_controller) {
Maxime Ripard32a19de2021-07-07 11:51:10 +02001861 ret = request_threaded_irq(platform_get_irq_byname(pdev, "cec-rx"),
1862 vc4_cec_irq_handler_rx_bare,
1863 vc4_cec_irq_handler_rx_thread, 0,
1864 "vc4 hdmi cec rx", vc4_hdmi);
Maxime Ripard185e98b2021-01-11 15:23:04 +01001865 if (ret)
1866 goto err_delete_cec_adap;
1867
Maxime Ripard32a19de2021-07-07 11:51:10 +02001868 ret = request_threaded_irq(platform_get_irq_byname(pdev, "cec-tx"),
1869 vc4_cec_irq_handler_tx_bare,
1870 vc4_cec_irq_handler_tx_thread, 0,
1871 "vc4 hdmi cec tx", vc4_hdmi);
Maxime Ripard185e98b2021-01-11 15:23:04 +01001872 if (ret)
Maxime Ripard32a19de2021-07-07 11:51:10 +02001873 goto err_remove_cec_rx_handler;
Maxime Ripard185e98b2021-01-11 15:23:04 +01001874 } else {
1875 HDMI_WRITE(HDMI_CEC_CPU_MASK_SET, 0xffffffff);
1876
Maxime Ripard32a19de2021-07-07 11:51:10 +02001877 ret = request_threaded_irq(platform_get_irq(pdev, 0),
1878 vc4_cec_irq_handler,
1879 vc4_cec_irq_handler_thread, 0,
1880 "vc4 hdmi cec", vc4_hdmi);
Maxime Ripard185e98b2021-01-11 15:23:04 +01001881 if (ret)
1882 goto err_delete_cec_adap;
1883 }
Maxime Ripardc0791e02020-09-03 10:01:31 +02001884
1885 ret = cec_register_adapter(vc4_hdmi->cec_adap, &pdev->dev);
Hans Verkuil15b45112017-07-16 12:48:04 +02001886 if (ret < 0)
Maxime Ripard32a19de2021-07-07 11:51:10 +02001887 goto err_remove_handlers;
Eric Anholtc9be8042019-04-01 11:35:58 -07001888
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001889 return 0;
1890
Maxime Ripard32a19de2021-07-07 11:51:10 +02001891err_remove_handlers:
1892 if (vc4_hdmi->variant->external_irq_controller)
1893 free_irq(platform_get_irq_byname(pdev, "cec-tx"), vc4_hdmi);
1894 else
1895 free_irq(platform_get_irq(pdev, 0), vc4_hdmi);
1896
1897err_remove_cec_rx_handler:
1898 if (vc4_hdmi->variant->external_irq_controller)
1899 free_irq(platform_get_irq_byname(pdev, "cec-rx"), vc4_hdmi);
1900
Hans Verkuil15b45112017-07-16 12:48:04 +02001901err_delete_cec_adap:
Maxime Ripardc0791e02020-09-03 10:01:31 +02001902 cec_delete_adapter(vc4_hdmi->cec_adap);
1903
1904 return ret;
1905}
1906
1907static void vc4_hdmi_cec_exit(struct vc4_hdmi *vc4_hdmi)
1908{
Maxime Ripard32a19de2021-07-07 11:51:10 +02001909 struct platform_device *pdev = vc4_hdmi->pdev;
1910
1911 if (vc4_hdmi->variant->external_irq_controller) {
1912 free_irq(platform_get_irq_byname(pdev, "cec-rx"), vc4_hdmi);
1913 free_irq(platform_get_irq_byname(pdev, "cec-tx"), vc4_hdmi);
1914 } else {
1915 free_irq(platform_get_irq(pdev, 0), vc4_hdmi);
1916 }
1917
Maxime Ripardc0791e02020-09-03 10:01:31 +02001918 cec_unregister_adapter(vc4_hdmi->cec_adap);
1919}
1920#else
1921static int vc4_hdmi_cec_init(struct vc4_hdmi *vc4_hdmi)
1922{
1923 return 0;
1924}
1925
1926static void vc4_hdmi_cec_exit(struct vc4_hdmi *vc4_hdmi) {};
1927
Hans Verkuil15b45112017-07-16 12:48:04 +02001928#endif
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001929
Maxime Ripard311e3052020-09-03 10:01:23 +02001930static int vc4_hdmi_build_regset(struct vc4_hdmi *vc4_hdmi,
1931 struct debugfs_regset32 *regset,
1932 enum vc4_hdmi_regs reg)
1933{
1934 const struct vc4_hdmi_variant *variant = vc4_hdmi->variant;
1935 struct debugfs_reg32 *regs, *new_regs;
1936 unsigned int count = 0;
1937 unsigned int i;
1938
1939 regs = kcalloc(variant->num_registers, sizeof(*regs),
1940 GFP_KERNEL);
1941 if (!regs)
1942 return -ENOMEM;
1943
1944 for (i = 0; i < variant->num_registers; i++) {
1945 const struct vc4_hdmi_register *field = &variant->registers[i];
1946
1947 if (field->reg != reg)
1948 continue;
1949
1950 regs[count].name = field->name;
1951 regs[count].offset = field->offset;
1952 count++;
1953 }
1954
1955 new_regs = krealloc(regs, count * sizeof(*regs), GFP_KERNEL);
1956 if (!new_regs)
1957 return -ENOMEM;
1958
1959 regset->base = __vc4_hdmi_get_field_base(vc4_hdmi, reg);
1960 regset->regs = new_regs;
1961 regset->nregs = count;
1962
1963 return 0;
1964}
1965
Maxime Ripard33c773e2020-09-03 10:01:22 +02001966static int vc4_hdmi_init_resources(struct vc4_hdmi *vc4_hdmi)
1967{
1968 struct platform_device *pdev = vc4_hdmi->pdev;
1969 struct device *dev = &pdev->dev;
1970 int ret;
1971
1972 vc4_hdmi->hdmicore_regs = vc4_ioremap_regs(pdev, 0);
1973 if (IS_ERR(vc4_hdmi->hdmicore_regs))
1974 return PTR_ERR(vc4_hdmi->hdmicore_regs);
1975
1976 vc4_hdmi->hd_regs = vc4_ioremap_regs(pdev, 1);
1977 if (IS_ERR(vc4_hdmi->hd_regs))
1978 return PTR_ERR(vc4_hdmi->hd_regs);
1979
Maxime Ripard311e3052020-09-03 10:01:23 +02001980 ret = vc4_hdmi_build_regset(vc4_hdmi, &vc4_hdmi->hd_regset, VC4_HD);
1981 if (ret)
1982 return ret;
Maxime Ripard33c773e2020-09-03 10:01:22 +02001983
Maxime Ripard311e3052020-09-03 10:01:23 +02001984 ret = vc4_hdmi_build_regset(vc4_hdmi, &vc4_hdmi->hdmi_regset, VC4_HDMI);
1985 if (ret)
1986 return ret;
Maxime Ripard33c773e2020-09-03 10:01:22 +02001987
1988 vc4_hdmi->pixel_clock = devm_clk_get(dev, "pixel");
1989 if (IS_ERR(vc4_hdmi->pixel_clock)) {
1990 ret = PTR_ERR(vc4_hdmi->pixel_clock);
1991 if (ret != -EPROBE_DEFER)
1992 DRM_ERROR("Failed to get pixel clock\n");
1993 return ret;
1994 }
1995
1996 vc4_hdmi->hsm_clock = devm_clk_get(dev, "hdmi");
1997 if (IS_ERR(vc4_hdmi->hsm_clock)) {
1998 DRM_ERROR("Failed to get HDMI state machine clock\n");
1999 return PTR_ERR(vc4_hdmi->hsm_clock);
2000 }
Dave Stevenson632ee3a2020-09-03 10:01:40 +02002001 vc4_hdmi->audio_clock = vc4_hdmi->hsm_clock;
Maxime Ripard23b7eb52021-01-11 15:23:02 +01002002 vc4_hdmi->cec_clock = vc4_hdmi->hsm_clock;
Maxime Ripard33c773e2020-09-03 10:01:22 +02002003
2004 return 0;
2005}
2006
Maxime Ripard83239892020-09-03 10:01:48 +02002007static int vc5_hdmi_init_resources(struct vc4_hdmi *vc4_hdmi)
2008{
2009 struct platform_device *pdev = vc4_hdmi->pdev;
2010 struct device *dev = &pdev->dev;
2011 struct resource *res;
2012
2013 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hdmi");
2014 if (!res)
2015 return -ENODEV;
2016
2017 vc4_hdmi->hdmicore_regs = devm_ioremap(dev, res->start,
2018 resource_size(res));
Dan Carpenter14929c52020-09-10 13:08:25 +03002019 if (!vc4_hdmi->hdmicore_regs)
2020 return -ENOMEM;
Maxime Ripard83239892020-09-03 10:01:48 +02002021
2022 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hd");
2023 if (!res)
2024 return -ENODEV;
2025
2026 vc4_hdmi->hd_regs = devm_ioremap(dev, res->start, resource_size(res));
Dan Carpenter14929c52020-09-10 13:08:25 +03002027 if (!vc4_hdmi->hd_regs)
2028 return -ENOMEM;
Maxime Ripard83239892020-09-03 10:01:48 +02002029
2030 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cec");
2031 if (!res)
2032 return -ENODEV;
2033
2034 vc4_hdmi->cec_regs = devm_ioremap(dev, res->start, resource_size(res));
Dan Carpenter14929c52020-09-10 13:08:25 +03002035 if (!vc4_hdmi->cec_regs)
2036 return -ENOMEM;
Maxime Ripard83239892020-09-03 10:01:48 +02002037
2038 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "csc");
2039 if (!res)
2040 return -ENODEV;
2041
2042 vc4_hdmi->csc_regs = devm_ioremap(dev, res->start, resource_size(res));
Dan Carpenter14929c52020-09-10 13:08:25 +03002043 if (!vc4_hdmi->csc_regs)
2044 return -ENOMEM;
Maxime Ripard83239892020-09-03 10:01:48 +02002045
2046 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dvp");
2047 if (!res)
2048 return -ENODEV;
2049
2050 vc4_hdmi->dvp_regs = devm_ioremap(dev, res->start, resource_size(res));
Dan Carpenter14929c52020-09-10 13:08:25 +03002051 if (!vc4_hdmi->dvp_regs)
2052 return -ENOMEM;
Maxime Ripard83239892020-09-03 10:01:48 +02002053
2054 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy");
2055 if (!res)
2056 return -ENODEV;
2057
2058 vc4_hdmi->phy_regs = devm_ioremap(dev, res->start, resource_size(res));
Dan Carpenter14929c52020-09-10 13:08:25 +03002059 if (!vc4_hdmi->phy_regs)
2060 return -ENOMEM;
Maxime Ripard83239892020-09-03 10:01:48 +02002061
2062 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "packet");
2063 if (!res)
2064 return -ENODEV;
2065
2066 vc4_hdmi->ram_regs = devm_ioremap(dev, res->start, resource_size(res));
Dan Carpenter14929c52020-09-10 13:08:25 +03002067 if (!vc4_hdmi->ram_regs)
2068 return -ENOMEM;
Maxime Ripard83239892020-09-03 10:01:48 +02002069
2070 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rm");
2071 if (!res)
2072 return -ENODEV;
2073
2074 vc4_hdmi->rm_regs = devm_ioremap(dev, res->start, resource_size(res));
Dan Carpenter14929c52020-09-10 13:08:25 +03002075 if (!vc4_hdmi->rm_regs)
2076 return -ENOMEM;
Maxime Ripard83239892020-09-03 10:01:48 +02002077
2078 vc4_hdmi->hsm_clock = devm_clk_get(dev, "hdmi");
2079 if (IS_ERR(vc4_hdmi->hsm_clock)) {
2080 DRM_ERROR("Failed to get HDMI state machine clock\n");
2081 return PTR_ERR(vc4_hdmi->hsm_clock);
2082 }
2083
2084 vc4_hdmi->pixel_bvb_clock = devm_clk_get(dev, "bvb");
2085 if (IS_ERR(vc4_hdmi->pixel_bvb_clock)) {
2086 DRM_ERROR("Failed to get pixel bvb clock\n");
2087 return PTR_ERR(vc4_hdmi->pixel_bvb_clock);
2088 }
2089
2090 vc4_hdmi->audio_clock = devm_clk_get(dev, "audio");
2091 if (IS_ERR(vc4_hdmi->audio_clock)) {
2092 DRM_ERROR("Failed to get audio clock\n");
2093 return PTR_ERR(vc4_hdmi->audio_clock);
2094 }
2095
Maxime Ripard23b7eb52021-01-11 15:23:02 +01002096 vc4_hdmi->cec_clock = devm_clk_get(dev, "cec");
2097 if (IS_ERR(vc4_hdmi->cec_clock)) {
2098 DRM_ERROR("Failed to get CEC clock\n");
2099 return PTR_ERR(vc4_hdmi->cec_clock);
2100 }
2101
Maxime Ripard83239892020-09-03 10:01:48 +02002102 vc4_hdmi->reset = devm_reset_control_get(dev, NULL);
2103 if (IS_ERR(vc4_hdmi->reset)) {
2104 DRM_ERROR("Failed to get HDMI reset line\n");
2105 return PTR_ERR(vc4_hdmi->reset);
2106 }
2107
2108 return 0;
2109}
2110
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002111static int vc4_hdmi_bind(struct device *dev, struct device *master, void *data)
2112{
Maxime Ripard33c773e2020-09-03 10:01:22 +02002113 const struct vc4_hdmi_variant *variant = of_device_get_match_data(dev);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002114 struct platform_device *pdev = to_platform_device(dev);
2115 struct drm_device *drm = dev_get_drvdata(master);
Maxime Ripard3408cc22020-09-03 10:01:14 +02002116 struct vc4_hdmi *vc4_hdmi;
Maxime Ripardc98c85b2020-09-03 10:01:12 +02002117 struct drm_encoder *encoder;
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002118 struct device_node *ddc_node;
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002119 int ret;
2120
Maxime Ripard3408cc22020-09-03 10:01:14 +02002121 vc4_hdmi = devm_kzalloc(dev, sizeof(*vc4_hdmi), GFP_KERNEL);
2122 if (!vc4_hdmi)
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002123 return -ENOMEM;
Maxime Ripard257d36d2021-05-07 17:05:14 +02002124 INIT_DELAYED_WORK(&vc4_hdmi->scrambling_work, vc4_hdmi_scrambling_wq);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002125
Maxime Ripard47c167b2020-09-03 10:01:19 +02002126 dev_set_drvdata(dev, vc4_hdmi);
Maxime Ripard3408cc22020-09-03 10:01:14 +02002127 encoder = &vc4_hdmi->encoder.base.base;
Maxime Ripard7d732992020-09-03 10:01:29 +02002128 vc4_hdmi->encoder.base.type = variant->encoder_type;
Maxime Ripard09c43812020-09-03 10:01:44 +02002129 vc4_hdmi->encoder.base.pre_crtc_configure = vc4_hdmi_encoder_pre_crtc_configure;
2130 vc4_hdmi->encoder.base.pre_crtc_enable = vc4_hdmi_encoder_pre_crtc_enable;
2131 vc4_hdmi->encoder.base.post_crtc_enable = vc4_hdmi_encoder_post_crtc_enable;
2132 vc4_hdmi->encoder.base.post_crtc_disable = vc4_hdmi_encoder_post_crtc_disable;
2133 vc4_hdmi->encoder.base.post_crtc_powerdown = vc4_hdmi_encoder_post_crtc_powerdown;
Maxime Ripard3408cc22020-09-03 10:01:14 +02002134 vc4_hdmi->pdev = pdev;
Maxime Ripard33c773e2020-09-03 10:01:22 +02002135 vc4_hdmi->variant = variant;
Maxime Ripardc98c85b2020-09-03 10:01:12 +02002136
Maxime Ripard33c773e2020-09-03 10:01:22 +02002137 ret = variant->init_resources(vc4_hdmi);
2138 if (ret)
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002139 return ret;
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002140
2141 ddc_node = of_parse_phandle(dev->of_node, "ddc", 0);
2142 if (!ddc_node) {
2143 DRM_ERROR("Failed to find ddc node in device tree\n");
2144 return -ENODEV;
2145 }
2146
Maxime Ripard3408cc22020-09-03 10:01:14 +02002147 vc4_hdmi->ddc = of_find_i2c_adapter_by_node(ddc_node);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002148 of_node_put(ddc_node);
Maxime Ripard3408cc22020-09-03 10:01:14 +02002149 if (!vc4_hdmi->ddc) {
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002150 DRM_DEBUG("Failed to get ddc i2c adapter by node\n");
2151 return -EPROBE_DEFER;
2152 }
2153
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002154 /* Only use the GPIO HPD pin if present in the DT, otherwise
2155 * we'll use the HDMI core's register.
2156 */
Maxime Ripard68002342021-05-24 15:18:52 +02002157 vc4_hdmi->hpd_gpio = devm_gpiod_get_optional(dev, "hpd", GPIOD_IN);
2158 if (IS_ERR(vc4_hdmi->hpd_gpio)) {
2159 ret = PTR_ERR(vc4_hdmi->hpd_gpio);
2160 goto err_put_ddc;
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002161 }
2162
Maxime Ripard9fa1d7e2020-10-29 14:40:17 +01002163 vc4_hdmi->disable_wifi_frequencies =
2164 of_property_read_bool(dev->of_node, "wifi-2.4ghz-coexistence");
2165
Maxime Ripard86e3a652021-05-07 17:05:12 +02002166 if (variant->max_pixel_clock == 600000000) {
2167 struct vc4_dev *vc4 = to_vc4_dev(drm);
2168 long max_rate = clk_round_rate(vc4->hvs->core_clk, 550000000);
2169
2170 if (max_rate < 550000000)
2171 vc4_hdmi->disable_4kp60 = true;
2172 }
2173
Maxime Ripard3e85b812021-09-22 14:54:17 +02002174 /*
2175 * If we boot without any cable connected to the HDMI connector,
2176 * the firmware will skip the HSM initialization and leave it
2177 * with a rate of 0, resulting in a bus lockup when we're
2178 * accessing the registers even if it's enabled.
2179 *
2180 * Let's put a sensible default at runtime_resume so that we
2181 * don't end up in this situation.
2182 */
2183 ret = clk_set_min_rate(vc4_hdmi->hsm_clock, HSM_MIN_CLOCK_FREQ);
2184 if (ret)
2185 goto err_put_ddc;
2186
Dom Cobley902dc5c12021-01-11 15:22:56 +01002187 if (vc4_hdmi->variant->reset)
2188 vc4_hdmi->variant->reset(vc4_hdmi);
2189
Maxime Ripard5b006002021-05-07 17:05:09 +02002190 if ((of_device_is_compatible(dev->of_node, "brcm,bcm2711-hdmi0") ||
2191 of_device_is_compatible(dev->of_node, "brcm,bcm2711-hdmi1")) &&
2192 HDMI_READ(HDMI_VID_CTL) & VC4_HD_VID_CTL_ENABLE) {
2193 clk_prepare_enable(vc4_hdmi->pixel_clock);
2194 clk_prepare_enable(vc4_hdmi->hsm_clock);
2195 clk_prepare_enable(vc4_hdmi->pixel_bvb_clock);
2196 }
2197
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002198 pm_runtime_enable(dev);
2199
Maxime Ripardc98c85b2020-09-03 10:01:12 +02002200 drm_simple_encoder_init(drm, encoder, DRM_MODE_ENCODER_TMDS);
2201 drm_encoder_helper_add(encoder, &vc4_hdmi_encoder_helper_funcs);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002202
Maxime Ripard3408cc22020-09-03 10:01:14 +02002203 ret = vc4_hdmi_connector_init(drm, vc4_hdmi);
Maxime Ripardc98c85b2020-09-03 10:01:12 +02002204 if (ret)
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002205 goto err_destroy_encoder;
Maxime Ripardc98c85b2020-09-03 10:01:12 +02002206
Maxime Ripardf4790082021-05-24 15:20:18 +02002207 ret = vc4_hdmi_hotplug_init(vc4_hdmi);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002208 if (ret)
Maxime Ripardc0791e02020-09-03 10:01:31 +02002209 goto err_destroy_conn;
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002210
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002211 ret = vc4_hdmi_cec_init(vc4_hdmi);
2212 if (ret)
Maxime Ripard776efe82021-07-07 11:51:11 +02002213 goto err_free_hotplug;
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002214
Maxime Ripard3408cc22020-09-03 10:01:14 +02002215 ret = vc4_hdmi_audio_init(vc4_hdmi);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002216 if (ret)
Maxime Ripardc0791e02020-09-03 10:01:31 +02002217 goto err_free_cec;
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002218
Maxime Ripardb2405c92020-09-03 10:01:30 +02002219 vc4_debugfs_add_file(drm, variant->debugfs_name,
2220 vc4_hdmi_debugfs_regs,
2221 vc4_hdmi);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002222
2223 return 0;
2224
Maxime Ripardc0791e02020-09-03 10:01:31 +02002225err_free_cec:
2226 vc4_hdmi_cec_exit(vc4_hdmi);
Maxime Ripard776efe82021-07-07 11:51:11 +02002227err_free_hotplug:
2228 vc4_hdmi_hotplug_exit(vc4_hdmi);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002229err_destroy_conn:
Maxime Ripard0532e5e2020-09-03 10:01:21 +02002230 vc4_hdmi_connector_destroy(&vc4_hdmi->connector);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002231err_destroy_encoder:
Maxime Ripardc98c85b2020-09-03 10:01:12 +02002232 drm_encoder_cleanup(encoder);
Boris Brezillon4f6e3d62017-04-11 18:39:25 +02002233 pm_runtime_disable(dev);
Maxime Riparde075a782021-05-24 15:18:51 +02002234err_put_ddc:
Maxime Ripard3408cc22020-09-03 10:01:14 +02002235 put_device(&vc4_hdmi->ddc->dev);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002236
2237 return ret;
2238}
2239
2240static void vc4_hdmi_unbind(struct device *dev, struct device *master,
2241 void *data)
2242{
Maxime Ripard47c167b2020-09-03 10:01:19 +02002243 struct vc4_hdmi *vc4_hdmi;
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002244
Maxime Ripard47c167b2020-09-03 10:01:19 +02002245 /*
2246 * ASoC makes it a bit hard to retrieve a pointer to the
2247 * vc4_hdmi structure. Registering the card will overwrite our
2248 * device drvdata with a pointer to the snd_soc_card structure,
2249 * which can then be used to retrieve whatever drvdata we want
2250 * to associate.
2251 *
2252 * However, that doesn't fly in the case where we wouldn't
2253 * register an ASoC card (because of an old DT that is missing
2254 * the dmas properties for example), then the card isn't
2255 * registered and the device drvdata wouldn't be set.
2256 *
2257 * We can deal with both cases by making sure a snd_soc_card
2258 * pointer and a vc4_hdmi structure are pointing to the same
2259 * memory address, so we can treat them indistinctly without any
2260 * issue.
2261 */
2262 BUILD_BUG_ON(offsetof(struct vc4_hdmi_audio, card) != 0);
2263 BUILD_BUG_ON(offsetof(struct vc4_hdmi, audio) != 0);
2264 vc4_hdmi = dev_get_drvdata(dev);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002265
Maxime Ripard311e3052020-09-03 10:01:23 +02002266 kfree(vc4_hdmi->hdmi_regset.regs);
2267 kfree(vc4_hdmi->hd_regset.regs);
2268
Maxime Ripardc0791e02020-09-03 10:01:31 +02002269 vc4_hdmi_cec_exit(vc4_hdmi);
Maxime Ripard776efe82021-07-07 11:51:11 +02002270 vc4_hdmi_hotplug_exit(vc4_hdmi);
Maxime Ripard0532e5e2020-09-03 10:01:21 +02002271 vc4_hdmi_connector_destroy(&vc4_hdmi->connector);
Maxime Ripard3408cc22020-09-03 10:01:14 +02002272 drm_encoder_cleanup(&vc4_hdmi->encoder.base.base);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002273
Boris Brezillon4f6e3d62017-04-11 18:39:25 +02002274 pm_runtime_disable(dev);
2275
Maxime Ripard3408cc22020-09-03 10:01:14 +02002276 put_device(&vc4_hdmi->ddc->dev);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002277}
2278
2279static const struct component_ops vc4_hdmi_ops = {
2280 .bind = vc4_hdmi_bind,
2281 .unbind = vc4_hdmi_unbind,
2282};
2283
2284static int vc4_hdmi_dev_probe(struct platform_device *pdev)
2285{
2286 return component_add(&pdev->dev, &vc4_hdmi_ops);
2287}
2288
2289static int vc4_hdmi_dev_remove(struct platform_device *pdev)
2290{
2291 component_del(&pdev->dev, &vc4_hdmi_ops);
2292 return 0;
2293}
2294
Maxime Ripard33c773e2020-09-03 10:01:22 +02002295static const struct vc4_hdmi_variant bcm2835_variant = {
Maxime Ripard7d732992020-09-03 10:01:29 +02002296 .encoder_type = VC4_ENCODER_TYPE_HDMI0,
Maxime Ripardb2405c92020-09-03 10:01:30 +02002297 .debugfs_name = "hdmi_regs",
Maxime Ripard9be43a52020-09-03 10:01:41 +02002298 .card_name = "vc4-hdmi",
Maxime Ripardcd4cb492020-09-03 10:01:35 +02002299 .max_pixel_clock = 162000000,
Maxime Ripard311e3052020-09-03 10:01:23 +02002300 .registers = vc4_hdmi_fields,
2301 .num_registers = ARRAY_SIZE(vc4_hdmi_fields),
2302
Maxime Ripard33c773e2020-09-03 10:01:22 +02002303 .init_resources = vc4_hdmi_init_resources,
Maxime Ripard89f31a22020-09-03 10:01:27 +02002304 .csc_setup = vc4_hdmi_csc_setup,
Maxime Ripard9045e912020-09-03 10:01:24 +02002305 .reset = vc4_hdmi_reset,
Maxime Ripard904f6682020-09-03 10:01:28 +02002306 .set_timings = vc4_hdmi_set_timings,
Maxime Ripardc457b8a2020-09-03 10:01:25 +02002307 .phy_init = vc4_hdmi_phy_init,
2308 .phy_disable = vc4_hdmi_phy_disable,
Maxime Ripard647b9652020-09-03 10:01:26 +02002309 .phy_rng_enable = vc4_hdmi_phy_rng_enable,
2310 .phy_rng_disable = vc4_hdmi_phy_rng_disable,
Dave Stevenson632ee3a2020-09-03 10:01:40 +02002311 .channel_map = vc4_hdmi_channel_map,
Dave Stevensonbccd5c52021-04-30 11:44:49 +02002312 .supports_hdr = false,
Maxime Ripard33c773e2020-09-03 10:01:22 +02002313};
2314
Maxime Ripard83239892020-09-03 10:01:48 +02002315static const struct vc4_hdmi_variant bcm2711_hdmi0_variant = {
2316 .encoder_type = VC4_ENCODER_TYPE_HDMI0,
2317 .debugfs_name = "hdmi0_regs",
2318 .card_name = "vc4-hdmi-0",
Maxime Ripard24169a22020-12-15 16:42:42 +01002319 .max_pixel_clock = HDMI_14_MAX_TMDS_CLK,
Maxime Ripard83239892020-09-03 10:01:48 +02002320 .registers = vc5_hdmi_hdmi0_fields,
2321 .num_registers = ARRAY_SIZE(vc5_hdmi_hdmi0_fields),
2322 .phy_lane_mapping = {
2323 PHY_LANE_0,
2324 PHY_LANE_1,
2325 PHY_LANE_2,
2326 PHY_LANE_CK,
2327 },
Maxime Ripard57fb32e2020-10-29 13:25:22 +01002328 .unsupported_odd_h_timings = true,
Maxime Ripard185e98b2021-01-11 15:23:04 +01002329 .external_irq_controller = true,
Maxime Ripard83239892020-09-03 10:01:48 +02002330
2331 .init_resources = vc5_hdmi_init_resources,
2332 .csc_setup = vc5_hdmi_csc_setup,
2333 .reset = vc5_hdmi_reset,
2334 .set_timings = vc5_hdmi_set_timings,
2335 .phy_init = vc5_hdmi_phy_init,
2336 .phy_disable = vc5_hdmi_phy_disable,
2337 .phy_rng_enable = vc5_hdmi_phy_rng_enable,
2338 .phy_rng_disable = vc5_hdmi_phy_rng_disable,
2339 .channel_map = vc5_hdmi_channel_map,
Dave Stevensonbccd5c52021-04-30 11:44:49 +02002340 .supports_hdr = true,
Maxime Ripard83239892020-09-03 10:01:48 +02002341};
2342
2343static const struct vc4_hdmi_variant bcm2711_hdmi1_variant = {
2344 .encoder_type = VC4_ENCODER_TYPE_HDMI1,
2345 .debugfs_name = "hdmi1_regs",
2346 .card_name = "vc4-hdmi-1",
Maxime Ripard24169a22020-12-15 16:42:42 +01002347 .max_pixel_clock = HDMI_14_MAX_TMDS_CLK,
Maxime Ripard83239892020-09-03 10:01:48 +02002348 .registers = vc5_hdmi_hdmi1_fields,
2349 .num_registers = ARRAY_SIZE(vc5_hdmi_hdmi1_fields),
2350 .phy_lane_mapping = {
2351 PHY_LANE_1,
2352 PHY_LANE_0,
2353 PHY_LANE_CK,
2354 PHY_LANE_2,
2355 },
Maxime Ripard57fb32e2020-10-29 13:25:22 +01002356 .unsupported_odd_h_timings = true,
Maxime Ripard185e98b2021-01-11 15:23:04 +01002357 .external_irq_controller = true,
Maxime Ripard83239892020-09-03 10:01:48 +02002358
2359 .init_resources = vc5_hdmi_init_resources,
2360 .csc_setup = vc5_hdmi_csc_setup,
2361 .reset = vc5_hdmi_reset,
2362 .set_timings = vc5_hdmi_set_timings,
2363 .phy_init = vc5_hdmi_phy_init,
2364 .phy_disable = vc5_hdmi_phy_disable,
2365 .phy_rng_enable = vc5_hdmi_phy_rng_enable,
2366 .phy_rng_disable = vc5_hdmi_phy_rng_disable,
2367 .channel_map = vc5_hdmi_channel_map,
Dave Stevensonbccd5c52021-04-30 11:44:49 +02002368 .supports_hdr = true,
Maxime Ripard83239892020-09-03 10:01:48 +02002369};
2370
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002371static const struct of_device_id vc4_hdmi_dt_match[] = {
Maxime Ripard33c773e2020-09-03 10:01:22 +02002372 { .compatible = "brcm,bcm2835-hdmi", .data = &bcm2835_variant },
Maxime Ripard83239892020-09-03 10:01:48 +02002373 { .compatible = "brcm,bcm2711-hdmi0", .data = &bcm2711_hdmi0_variant },
2374 { .compatible = "brcm,bcm2711-hdmi1", .data = &bcm2711_hdmi1_variant },
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002375 {}
2376};
2377
2378struct platform_driver vc4_hdmi_driver = {
2379 .probe = vc4_hdmi_dev_probe,
2380 .remove = vc4_hdmi_dev_remove,
2381 .driver = {
2382 .name = "vc4_hdmi",
2383 .of_match_table = vc4_hdmi_dt_match,
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002384 },
2385};