blob: f67c6f69fa281c380f0850cd37b887349f188040 [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>
49#include <sound/pcm_drm_eld.h>
50#include <sound/pcm_params.h>
51#include <sound/soc.h>
Hans Verkuil15b45112017-07-16 12:48:04 +020052#include "media/cec.h"
Eric Anholtc8b75bc2015-03-02 13:01:12 -080053#include "vc4_drv.h"
Maxime Ripardf73100c2020-09-03 10:01:11 +020054#include "vc4_hdmi.h"
Maxime Ripard311e3052020-09-03 10:01:23 +020055#include "vc4_hdmi_regs.h"
Eric Anholtc8b75bc2015-03-02 13:01:12 -080056#include "vc4_regs.h"
57
Maxime Ripard83239892020-09-03 10:01:48 +020058#define VC5_HDMI_HORZA_HFP_SHIFT 16
59#define VC5_HDMI_HORZA_HFP_MASK VC4_MASK(28, 16)
60#define VC5_HDMI_HORZA_VPOS BIT(15)
61#define VC5_HDMI_HORZA_HPOS BIT(14)
62#define VC5_HDMI_HORZA_HAP_SHIFT 0
63#define VC5_HDMI_HORZA_HAP_MASK VC4_MASK(13, 0)
64
65#define VC5_HDMI_HORZB_HBP_SHIFT 16
66#define VC5_HDMI_HORZB_HBP_MASK VC4_MASK(26, 16)
67#define VC5_HDMI_HORZB_HSP_SHIFT 0
68#define VC5_HDMI_HORZB_HSP_MASK VC4_MASK(10, 0)
69
70#define VC5_HDMI_VERTA_VSP_SHIFT 24
71#define VC5_HDMI_VERTA_VSP_MASK VC4_MASK(28, 24)
72#define VC5_HDMI_VERTA_VFP_SHIFT 16
73#define VC5_HDMI_VERTA_VFP_MASK VC4_MASK(22, 16)
74#define VC5_HDMI_VERTA_VAL_SHIFT 0
75#define VC5_HDMI_VERTA_VAL_MASK VC4_MASK(12, 0)
76
77#define VC5_HDMI_VERTB_VSPO_SHIFT 16
78#define VC5_HDMI_VERTB_VSPO_MASK VC4_MASK(29, 16)
79
Maxime Ripardc85695a2021-05-07 17:05:13 +020080#define VC5_HDMI_SCRAMBLER_CTL_ENABLE BIT(0)
81
Maxime Ripardba8c0fa2020-12-15 16:42:43 +010082#define VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE_SHIFT 8
83#define VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE_MASK VC4_MASK(10, 8)
84
85#define VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH_SHIFT 0
86#define VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH_MASK VC4_MASK(3, 0)
87
88#define VC5_HDMI_GCP_CONFIG_GCP_ENABLE BIT(31)
89
90#define VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1_SHIFT 8
91#define VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1_MASK VC4_MASK(15, 8)
92
Maxime Ripard83239892020-09-03 10:01:48 +020093# define VC4_HD_M_SW_RST BIT(2)
94# define VC4_HD_M_ENABLE BIT(0)
95
Hans Verkuil15b45112017-07-16 12:48:04 +020096#define CEC_CLOCK_FREQ 40000
Eric Anholtc8b75bc2015-03-02 13:01:12 -080097
Maxime Ripard24169a22020-12-15 16:42:42 +010098#define HDMI_14_MAX_TMDS_CLK (340 * 1000 * 1000)
99
Maxime Ripard86e3a652021-05-07 17:05:12 +0200100static bool vc4_hdmi_mode_needs_scrambling(const struct drm_display_mode *mode)
101{
102 return (mode->clock * 1000) > HDMI_14_MAX_TMDS_CLK;
103}
104
Eric Anholtc9be8042019-04-01 11:35:58 -0700105static int vc4_hdmi_debugfs_regs(struct seq_file *m, void *unused)
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800106{
107 struct drm_info_node *node = (struct drm_info_node *)m->private;
Maxime Ripard3408cc22020-09-03 10:01:14 +0200108 struct vc4_hdmi *vc4_hdmi = node->info_ent->data;
Eric Anholt30517192019-02-20 13:03:38 -0800109 struct drm_printer p = drm_seq_file_printer(m);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800110
Maxime Ripard3408cc22020-09-03 10:01:14 +0200111 drm_print_regset32(&p, &vc4_hdmi->hdmi_regset);
112 drm_print_regset32(&p, &vc4_hdmi->hd_regset);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800113
114 return 0;
115}
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800116
Maxime Ripard9045e912020-09-03 10:01:24 +0200117static void vc4_hdmi_reset(struct vc4_hdmi *vc4_hdmi)
118{
119 HDMI_WRITE(HDMI_M_CTL, VC4_HD_M_SW_RST);
120 udelay(1);
121 HDMI_WRITE(HDMI_M_CTL, 0);
122
123 HDMI_WRITE(HDMI_M_CTL, VC4_HD_M_ENABLE);
124
125 HDMI_WRITE(HDMI_SW_RESET_CONTROL,
126 VC4_HDMI_SW_RESET_HDMI |
127 VC4_HDMI_SW_RESET_FORMAT_DETECT);
128
129 HDMI_WRITE(HDMI_SW_RESET_CONTROL, 0);
130}
131
Maxime Ripard83239892020-09-03 10:01:48 +0200132static void vc5_hdmi_reset(struct vc4_hdmi *vc4_hdmi)
133{
134 reset_control_reset(vc4_hdmi->reset);
135
136 HDMI_WRITE(HDMI_DVP_CTL, 0);
137
138 HDMI_WRITE(HDMI_CLOCK_STOP,
139 HDMI_READ(HDMI_CLOCK_STOP) | VC4_DVP_HT_CLOCK_STOP_PIXEL);
140}
141
Maxime Ripard47fa9a82021-01-11 15:23:01 +0100142#ifdef CONFIG_DRM_VC4_HDMI_CEC
143static void vc4_hdmi_cec_update_clk_div(struct vc4_hdmi *vc4_hdmi)
144{
145 u16 clk_cnt;
146 u32 value;
147
148 value = HDMI_READ(HDMI_CEC_CNTRL_1);
149 value &= ~VC4_HDMI_CEC_DIV_CLK_CNT_MASK;
150
151 /*
152 * Set the clock divider: the hsm_clock rate and this divider
153 * setting will give a 40 kHz CEC clock.
154 */
Maxime Ripard23b7eb52021-01-11 15:23:02 +0100155 clk_cnt = clk_get_rate(vc4_hdmi->cec_clock) / CEC_CLOCK_FREQ;
Maxime Ripard47fa9a82021-01-11 15:23:01 +0100156 value |= clk_cnt << VC4_HDMI_CEC_DIV_CLK_CNT_SHIFT;
157 HDMI_WRITE(HDMI_CEC_CNTRL_1, value);
158}
159#else
160static void vc4_hdmi_cec_update_clk_div(struct vc4_hdmi *vc4_hdmi) {}
161#endif
162
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800163static enum drm_connector_status
164vc4_hdmi_connector_detect(struct drm_connector *connector, bool force)
165{
Maxime Ripard5dfbcae2020-09-03 10:01:17 +0200166 struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector);
Dom Cobley4d8602b2021-01-11 15:22:59 +0100167 bool connected = false;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800168
Maxime Ripardb10db9a2020-09-03 10:01:16 +0200169 if (vc4_hdmi->hpd_gpio) {
170 if (gpio_get_value_cansleep(vc4_hdmi->hpd_gpio) ^
171 vc4_hdmi->hpd_active_low)
Dom Cobley4d8602b2021-01-11 15:22:59 +0100172 connected = true;
173 } else if (drm_probe_ddc(vc4_hdmi->ddc)) {
174 connected = true;
175 } else if (HDMI_READ(HDMI_HOTPLUG) & VC4_HDMI_HOTPLUG_CONNECTED) {
176 connected = true;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800177 }
178
Dom Cobley4d8602b2021-01-11 15:22:59 +0100179 if (connected) {
180 if (connector->status != connector_status_connected) {
181 struct edid *edid = drm_get_edid(connector, vc4_hdmi->ddc);
Eric Anholt9d44abb2016-09-14 19:21:29 +0100182
Dom Cobley4d8602b2021-01-11 15:22:59 +0100183 if (edid) {
184 cec_s_phys_addr_from_edid(vc4_hdmi->cec_adap, edid);
185 vc4_hdmi->encoder.hdmi_monitor = drm_detect_hdmi_monitor(edid);
186 kfree(edid);
187 }
188 }
189
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800190 return connector_status_connected;
Dom Cobley4d8602b2021-01-11 15:22:59 +0100191 }
192
Maxime Ripardb10db9a2020-09-03 10:01:16 +0200193 cec_phys_addr_invalidate(vc4_hdmi->cec_adap);
Hans Verkuil15b45112017-07-16 12:48:04 +0200194 return connector_status_disconnected;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800195}
196
197static void vc4_hdmi_connector_destroy(struct drm_connector *connector)
198{
199 drm_connector_unregister(connector);
200 drm_connector_cleanup(connector);
201}
202
203static int vc4_hdmi_connector_get_modes(struct drm_connector *connector)
204{
Maxime Ripard5dfbcae2020-09-03 10:01:17 +0200205 struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector);
206 struct vc4_hdmi_encoder *vc4_encoder = &vc4_hdmi->encoder;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800207 int ret = 0;
208 struct edid *edid;
209
Maxime Ripard5dfbcae2020-09-03 10:01:17 +0200210 edid = drm_get_edid(connector, vc4_hdmi->ddc);
211 cec_s_phys_addr_from_edid(vc4_hdmi->cec_adap, edid);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800212 if (!edid)
213 return -ENODEV;
214
215 vc4_encoder->hdmi_monitor = drm_detect_hdmi_monitor(edid);
Eric Anholt21317b32016-09-29 15:34:43 -0700216
Daniel Vetterc555f022018-07-09 10:40:06 +0200217 drm_connector_update_edid_property(connector, edid);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800218 ret = drm_add_edid_modes(connector, edid);
Eric Anholt5afe0e62017-08-08 13:56:05 -0700219 kfree(edid);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800220
Maxime Ripard86e3a652021-05-07 17:05:12 +0200221 if (vc4_hdmi->disable_4kp60) {
222 struct drm_device *drm = connector->dev;
223 struct drm_display_mode *mode;
224
225 list_for_each_entry(mode, &connector->probed_modes, head) {
226 if (vc4_hdmi_mode_needs_scrambling(mode)) {
227 drm_warn_once(drm, "The core clock cannot reach frequencies high enough to support 4k @ 60Hz.");
228 drm_warn_once(drm, "Please change your config.txt file to add hdmi_enable_4kp60.");
229 }
230 }
231 }
232
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800233 return ret;
234}
235
Dave Stevensonbccd5c52021-04-30 11:44:49 +0200236static int vc4_hdmi_connector_atomic_check(struct drm_connector *connector,
237 struct drm_atomic_state *state)
238{
239 struct drm_connector_state *old_state =
240 drm_atomic_get_old_connector_state(state, connector);
241 struct drm_connector_state *new_state =
242 drm_atomic_get_new_connector_state(state, connector);
243 struct drm_crtc *crtc = new_state->crtc;
244
245 if (!crtc)
246 return 0;
247
Maxime Ripard76a262d2021-04-30 11:44:51 +0200248 if (old_state->colorspace != new_state->colorspace ||
249 !drm_connector_atomic_hdr_metadata_equal(old_state, new_state)) {
Dave Stevensonbccd5c52021-04-30 11:44:49 +0200250 struct drm_crtc_state *crtc_state;
251
252 crtc_state = drm_atomic_get_crtc_state(state, crtc);
253 if (IS_ERR(crtc_state))
254 return PTR_ERR(crtc_state);
255
256 crtc_state->mode_changed = true;
257 }
258
259 return 0;
260}
261
Maxime Ripard90b2df52019-06-19 12:17:53 +0200262static void vc4_hdmi_connector_reset(struct drm_connector *connector)
263{
Maxime Ripardfbe72712020-12-15 16:42:39 +0100264 struct vc4_hdmi_connector_state *old_state =
265 conn_state_to_vc4_hdmi_conn_state(connector->state);
266 struct vc4_hdmi_connector_state *new_state =
267 kzalloc(sizeof(*new_state), GFP_KERNEL);
Maxime Riparde55a0772020-12-15 16:42:38 +0100268
269 if (connector->state)
Maxime Ripardfbe72712020-12-15 16:42:39 +0100270 __drm_atomic_helper_connector_destroy_state(connector->state);
271
272 kfree(old_state);
273 __drm_atomic_helper_connector_reset(connector, &new_state->base);
274
275 if (!new_state)
276 return;
277
Maxime Ripardba8c0fa2020-12-15 16:42:43 +0100278 new_state->base.max_bpc = 8;
279 new_state->base.max_requested_bpc = 8;
Maxime Ripardfbe72712020-12-15 16:42:39 +0100280 drm_atomic_helper_connector_tv_reset(connector);
281}
282
283static struct drm_connector_state *
284vc4_hdmi_connector_duplicate_state(struct drm_connector *connector)
285{
286 struct drm_connector_state *conn_state = connector->state;
287 struct vc4_hdmi_connector_state *vc4_state = conn_state_to_vc4_hdmi_conn_state(conn_state);
288 struct vc4_hdmi_connector_state *new_state;
289
290 new_state = kzalloc(sizeof(*new_state), GFP_KERNEL);
291 if (!new_state)
292 return NULL;
293
Maxime Ripardf6237462020-12-15 16:42:40 +0100294 new_state->pixel_rate = vc4_state->pixel_rate;
Maxime Ripardfbe72712020-12-15 16:42:39 +0100295 __drm_atomic_helper_connector_duplicate_state(connector, &new_state->base);
296
297 return &new_state->base;
Maxime Ripard90b2df52019-06-19 12:17:53 +0200298}
299
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800300static const struct drm_connector_funcs vc4_hdmi_connector_funcs = {
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800301 .detect = vc4_hdmi_connector_detect,
Eric Anholt682e62c2016-09-28 17:30:25 -0700302 .fill_modes = drm_helper_probe_single_connector_modes,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800303 .destroy = vc4_hdmi_connector_destroy,
Maxime Ripard90b2df52019-06-19 12:17:53 +0200304 .reset = vc4_hdmi_connector_reset,
Maxime Ripardfbe72712020-12-15 16:42:39 +0100305 .atomic_duplicate_state = vc4_hdmi_connector_duplicate_state,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800306 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
307};
308
309static const struct drm_connector_helper_funcs vc4_hdmi_connector_helper_funcs = {
310 .get_modes = vc4_hdmi_connector_get_modes,
Dave Stevensonbccd5c52021-04-30 11:44:49 +0200311 .atomic_check = vc4_hdmi_connector_atomic_check,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800312};
313
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200314static int vc4_hdmi_connector_init(struct drm_device *dev,
Maxime Ripardb052e702020-09-03 10:01:13 +0200315 struct vc4_hdmi *vc4_hdmi)
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800316{
Maxime Ripard0532e5e2020-09-03 10:01:21 +0200317 struct drm_connector *connector = &vc4_hdmi->connector;
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200318 struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
Boris Brezillondb999532018-12-06 15:24:39 +0100319 int ret;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800320
Andrzej Pietrasiewicz04a880f2020-01-02 14:22:58 +0100321 drm_connector_init_with_ddc(dev, connector,
322 &vc4_hdmi_connector_funcs,
323 DRM_MODE_CONNECTOR_HDMIA,
Maxime Ripardb052e702020-09-03 10:01:13 +0200324 vc4_hdmi->ddc);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800325 drm_connector_helper_add(connector, &vc4_hdmi_connector_helper_funcs);
326
Maxime Ripardba8c0fa2020-12-15 16:42:43 +0100327 /*
328 * Some of the properties below require access to state, like bpc.
329 * Allocate some default initial connector state with our reset helper.
330 */
331 if (connector->funcs->reset)
332 connector->funcs->reset(connector);
333
Boris Brezillondb999532018-12-06 15:24:39 +0100334 /* Create and attach TV margin props to this connector. */
335 ret = drm_mode_create_tv_margin_properties(dev);
336 if (ret)
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200337 return ret;
Boris Brezillondb999532018-12-06 15:24:39 +0100338
Maxime Ripard76a262d2021-04-30 11:44:51 +0200339 ret = drm_mode_create_hdmi_colorspace_property(connector);
340 if (ret)
341 return ret;
342
343 drm_connector_attach_colorspace_property(connector);
Boris Brezillondb999532018-12-06 15:24:39 +0100344 drm_connector_attach_tv_margin_properties(connector);
Maxime Ripardba8c0fa2020-12-15 16:42:43 +0100345 drm_connector_attach_max_bpc_property(connector, 8, 12);
Boris Brezillondb999532018-12-06 15:24:39 +0100346
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800347 connector->polled = (DRM_CONNECTOR_POLL_CONNECT |
348 DRM_CONNECTOR_POLL_DISCONNECT);
349
Mario Kleineracc1be12016-07-19 20:58:58 +0200350 connector->interlace_allowed = 1;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800351 connector->doublescan_allowed = 0;
352
Dave Stevensonbccd5c52021-04-30 11:44:49 +0200353 if (vc4_hdmi->variant->supports_hdr)
354 drm_connector_attach_hdr_output_metadata_property(connector);
355
Daniel Vettercde4c442018-07-09 10:40:07 +0200356 drm_connector_attach_encoder(connector, encoder);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800357
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200358 return 0;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800359}
360
Eric Anholt21317b32016-09-29 15:34:43 -0700361static int vc4_hdmi_stop_packet(struct drm_encoder *encoder,
Maxime Riparde2f9b2e2020-12-03 08:46:24 +0100362 enum hdmi_infoframe_type type,
363 bool poll)
Eric Anholt21317b32016-09-29 15:34:43 -0700364{
Maxime Ripard5dfbcae2020-09-03 10:01:17 +0200365 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
Eric Anholt21317b32016-09-29 15:34:43 -0700366 u32 packet_id = type - 0x80;
367
Maxime Ripard311e3052020-09-03 10:01:23 +0200368 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
369 HDMI_READ(HDMI_RAM_PACKET_CONFIG) & ~BIT(packet_id));
Eric Anholt21317b32016-09-29 15:34:43 -0700370
Maxime Riparde2f9b2e2020-12-03 08:46:24 +0100371 if (!poll)
372 return 0;
373
Maxime Ripard311e3052020-09-03 10:01:23 +0200374 return wait_for(!(HDMI_READ(HDMI_RAM_PACKET_STATUS) &
Eric Anholt21317b32016-09-29 15:34:43 -0700375 BIT(packet_id)), 100);
376}
377
378static void vc4_hdmi_write_infoframe(struct drm_encoder *encoder,
379 union hdmi_infoframe *frame)
380{
Maxime Ripard5dfbcae2020-09-03 10:01:17 +0200381 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
Eric Anholt21317b32016-09-29 15:34:43 -0700382 u32 packet_id = frame->any.type - 0x80;
Maxime Ripard311e3052020-09-03 10:01:23 +0200383 const struct vc4_hdmi_register *ram_packet_start =
384 &vc4_hdmi->variant->registers[HDMI_RAM_PACKET_START];
385 u32 packet_reg = ram_packet_start->offset + VC4_HDMI_PACKET_STRIDE * packet_id;
386 void __iomem *base = __vc4_hdmi_get_field_base(vc4_hdmi,
387 ram_packet_start->reg);
Eric Anholt21317b32016-09-29 15:34:43 -0700388 uint8_t buffer[VC4_HDMI_PACKET_STRIDE];
389 ssize_t len, i;
390 int ret;
391
Maxime Ripard311e3052020-09-03 10:01:23 +0200392 WARN_ONCE(!(HDMI_READ(HDMI_RAM_PACKET_CONFIG) &
Eric Anholt21317b32016-09-29 15:34:43 -0700393 VC4_HDMI_RAM_PACKET_ENABLE),
394 "Packet RAM has to be on to store the packet.");
395
396 len = hdmi_infoframe_pack(frame, buffer, sizeof(buffer));
397 if (len < 0)
398 return;
399
Maxime Riparde2f9b2e2020-12-03 08:46:24 +0100400 ret = vc4_hdmi_stop_packet(encoder, frame->any.type, true);
Eric Anholt21317b32016-09-29 15:34:43 -0700401 if (ret) {
402 DRM_ERROR("Failed to wait for infoframe to go idle: %d\n", ret);
403 return;
404 }
405
406 for (i = 0; i < len; i += 7) {
Maxime Ripard311e3052020-09-03 10:01:23 +0200407 writel(buffer[i + 0] << 0 |
408 buffer[i + 1] << 8 |
409 buffer[i + 2] << 16,
410 base + packet_reg);
Eric Anholt21317b32016-09-29 15:34:43 -0700411 packet_reg += 4;
412
Maxime Ripard311e3052020-09-03 10:01:23 +0200413 writel(buffer[i + 3] << 0 |
414 buffer[i + 4] << 8 |
415 buffer[i + 5] << 16 |
416 buffer[i + 6] << 24,
417 base + packet_reg);
Eric Anholt21317b32016-09-29 15:34:43 -0700418 packet_reg += 4;
419 }
420
Maxime Ripard311e3052020-09-03 10:01:23 +0200421 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
422 HDMI_READ(HDMI_RAM_PACKET_CONFIG) | BIT(packet_id));
423 ret = wait_for((HDMI_READ(HDMI_RAM_PACKET_STATUS) &
Eric Anholt21317b32016-09-29 15:34:43 -0700424 BIT(packet_id)), 100);
425 if (ret)
426 DRM_ERROR("Failed to wait for infoframe to start: %d\n", ret);
427}
428
429static void vc4_hdmi_set_avi_infoframe(struct drm_encoder *encoder)
430{
Maxime Ripard5dfbcae2020-09-03 10:01:17 +0200431 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
Eric Anholt21317b32016-09-29 15:34:43 -0700432 struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
Maxime Ripard0532e5e2020-09-03 10:01:21 +0200433 struct drm_connector *connector = &vc4_hdmi->connector;
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200434 struct drm_connector_state *cstate = connector->state;
Eric Anholt21317b32016-09-29 15:34:43 -0700435 struct drm_crtc *crtc = encoder->crtc;
436 const struct drm_display_mode *mode = &crtc->state->adjusted_mode;
437 union hdmi_infoframe frame;
438 int ret;
439
Ville Syrjälä13d0add2019-01-08 19:28:25 +0200440 ret = drm_hdmi_avi_infoframe_from_display_mode(&frame.avi,
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200441 connector, mode);
Eric Anholt21317b32016-09-29 15:34:43 -0700442 if (ret < 0) {
443 DRM_ERROR("couldn't fill AVI infoframe\n");
444 return;
445 }
446
Ville Syrjälä13d0add2019-01-08 19:28:25 +0200447 drm_hdmi_avi_infoframe_quant_range(&frame.avi,
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200448 connector, mode,
Ville Syrjäläa2ce26f2017-01-11 14:57:23 +0200449 vc4_encoder->limited_rgb_range ?
450 HDMI_QUANTIZATION_RANGE_LIMITED :
Ville Syrjälä1581b2d2019-01-08 19:28:28 +0200451 HDMI_QUANTIZATION_RANGE_FULL);
Maxime Ripard76a262d2021-04-30 11:44:51 +0200452 drm_hdmi_avi_infoframe_colorspace(&frame.avi, cstate);
Ville Syrjäläcb876372019-10-08 19:48:14 +0300453 drm_hdmi_avi_infoframe_bars(&frame.avi, cstate);
Boris Brezillondb999532018-12-06 15:24:39 +0100454
Eric Anholt21317b32016-09-29 15:34:43 -0700455 vc4_hdmi_write_infoframe(encoder, &frame);
456}
457
458static void vc4_hdmi_set_spd_infoframe(struct drm_encoder *encoder)
459{
460 union hdmi_infoframe frame;
461 int ret;
462
463 ret = hdmi_spd_infoframe_init(&frame.spd, "Broadcom", "Videocore");
464 if (ret < 0) {
465 DRM_ERROR("couldn't fill SPD infoframe\n");
466 return;
467 }
468
469 frame.spd.sdi = HDMI_SPD_SDI_PC;
470
471 vc4_hdmi_write_infoframe(encoder, &frame);
472}
473
Eric Anholtbb7d7852017-02-27 12:28:02 -0800474static void vc4_hdmi_set_audio_infoframe(struct drm_encoder *encoder)
475{
Maxime Ripard5dfbcae2020-09-03 10:01:17 +0200476 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800477 union hdmi_infoframe frame;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800478
Lee Jones2bac9592020-11-16 17:41:08 +0000479 hdmi_audio_infoframe_init(&frame.audio);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800480
481 frame.audio.coding_type = HDMI_AUDIO_CODING_TYPE_STREAM;
482 frame.audio.sample_frequency = HDMI_AUDIO_SAMPLE_FREQUENCY_STREAM;
483 frame.audio.sample_size = HDMI_AUDIO_SAMPLE_SIZE_STREAM;
Maxime Ripard3408cc22020-09-03 10:01:14 +0200484 frame.audio.channels = vc4_hdmi->audio.channels;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800485
486 vc4_hdmi_write_infoframe(encoder, &frame);
487}
488
Dave Stevensonbccd5c52021-04-30 11:44:49 +0200489static void vc4_hdmi_set_hdr_infoframe(struct drm_encoder *encoder)
490{
491 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
492 struct drm_connector *connector = &vc4_hdmi->connector;
493 struct drm_connector_state *conn_state = connector->state;
494 union hdmi_infoframe frame;
495
496 if (!vc4_hdmi->variant->supports_hdr)
497 return;
498
499 if (!conn_state->hdr_output_metadata)
500 return;
501
502 if (drm_hdmi_infoframe_set_hdr_metadata(&frame.drm, conn_state))
503 return;
504
505 vc4_hdmi_write_infoframe(encoder, &frame);
506}
507
Eric Anholt21317b32016-09-29 15:34:43 -0700508static void vc4_hdmi_set_infoframes(struct drm_encoder *encoder)
509{
Dave Stevenson6ac1c752020-09-03 10:01:38 +0200510 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
511
Eric Anholt21317b32016-09-29 15:34:43 -0700512 vc4_hdmi_set_avi_infoframe(encoder);
513 vc4_hdmi_set_spd_infoframe(encoder);
Dave Stevenson6ac1c752020-09-03 10:01:38 +0200514 /*
515 * If audio was streaming, then we need to reenabled the audio
516 * infoframe here during encoder_enable.
517 */
518 if (vc4_hdmi->audio.streaming)
519 vc4_hdmi_set_audio_infoframe(encoder);
Dave Stevensonbccd5c52021-04-30 11:44:49 +0200520
521 vc4_hdmi_set_hdr_infoframe(encoder);
Eric Anholt21317b32016-09-29 15:34:43 -0700522}
523
Maxime Ripardc85695a2021-05-07 17:05:13 +0200524static bool vc4_hdmi_supports_scrambling(struct drm_encoder *encoder,
525 struct drm_display_mode *mode)
526{
527 struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
528 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
529 struct drm_display_info *display = &vc4_hdmi->connector.display_info;
530
531 if (!vc4_encoder->hdmi_monitor)
532 return false;
533
534 if (!display->hdmi.scdc.supported ||
535 !display->hdmi.scdc.scrambling.supported)
536 return false;
537
538 return true;
539}
540
541static void vc4_hdmi_enable_scrambling(struct drm_encoder *encoder)
542{
543 struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
544 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
545
546 if (!vc4_hdmi_supports_scrambling(encoder, mode))
547 return;
548
549 if (!vc4_hdmi_mode_needs_scrambling(mode))
550 return;
551
552 drm_scdc_set_high_tmds_clock_ratio(vc4_hdmi->ddc, true);
553 drm_scdc_set_scrambling(vc4_hdmi->ddc, true);
554
555 HDMI_WRITE(HDMI_SCRAMBLER_CTL, HDMI_READ(HDMI_SCRAMBLER_CTL) |
556 VC5_HDMI_SCRAMBLER_CTL_ENABLE);
557}
558
559static void vc4_hdmi_disable_scrambling(struct drm_encoder *encoder)
560{
561 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
562 struct drm_crtc *crtc = encoder->crtc;
563
564 /*
565 * At boot, encoder->crtc will be NULL. Since we don't know the
566 * state of the scrambler and in order to avoid any
567 * inconsistency, let's disable it all the time.
568 */
569 if (crtc && !vc4_hdmi_supports_scrambling(encoder, &crtc->mode))
570 return;
571
572 if (crtc && !vc4_hdmi_mode_needs_scrambling(&crtc->mode))
573 return;
574
575 HDMI_WRITE(HDMI_SCRAMBLER_CTL, HDMI_READ(HDMI_SCRAMBLER_CTL) &
576 ~VC5_HDMI_SCRAMBLER_CTL_ENABLE);
577
578 drm_scdc_set_scrambling(vc4_hdmi->ddc, false);
579 drm_scdc_set_high_tmds_clock_ratio(vc4_hdmi->ddc, false);
580}
581
Maxime Ripard8d914742020-12-15 16:42:36 +0100582static void vc4_hdmi_encoder_post_crtc_disable(struct drm_encoder *encoder,
583 struct drm_atomic_state *state)
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800584{
Maxime Ripard09c43812020-09-03 10:01:44 +0200585 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
586
587 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG, 0);
Maxime Ripard81d83012020-09-03 10:01:46 +0200588
589 HDMI_WRITE(HDMI_VID_CTL, HDMI_READ(HDMI_VID_CTL) |
590 VC4_HD_VID_CTL_CLRRGB | VC4_HD_VID_CTL_CLRSYNC);
591
592 HDMI_WRITE(HDMI_VID_CTL,
593 HDMI_READ(HDMI_VID_CTL) | VC4_HD_VID_CTL_BLANKPIX);
Maxime Ripardc85695a2021-05-07 17:05:13 +0200594
595 vc4_hdmi_disable_scrambling(encoder);
Maxime Ripard09c43812020-09-03 10:01:44 +0200596}
597
Maxime Ripard8d914742020-12-15 16:42:36 +0100598static void vc4_hdmi_encoder_post_crtc_powerdown(struct drm_encoder *encoder,
599 struct drm_atomic_state *state)
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800600{
Maxime Ripard5dfbcae2020-09-03 10:01:17 +0200601 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200602 int ret;
603
Maxime Ripardc457b8a2020-09-03 10:01:25 +0200604 if (vc4_hdmi->variant->phy_disable)
605 vc4_hdmi->variant->phy_disable(vc4_hdmi);
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200606
Maxime Ripard311e3052020-09-03 10:01:23 +0200607 HDMI_WRITE(HDMI_VID_CTL,
608 HDMI_READ(HDMI_VID_CTL) & ~VC4_HD_VID_CTL_ENABLE);
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200609
Hoegeun Kwon37387422020-09-03 10:01:47 +0200610 clk_disable_unprepare(vc4_hdmi->pixel_bvb_clock);
Maxime Ripardcd4cb492020-09-03 10:01:35 +0200611 clk_disable_unprepare(vc4_hdmi->hsm_clock);
Maxime Ripard3408cc22020-09-03 10:01:14 +0200612 clk_disable_unprepare(vc4_hdmi->pixel_clock);
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200613
Maxime Ripard3408cc22020-09-03 10:01:14 +0200614 ret = pm_runtime_put(&vc4_hdmi->pdev->dev);
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200615 if (ret < 0)
616 DRM_ERROR("Failed to release power domain: %d\n", ret);
617}
618
Maxime Ripard09c43812020-09-03 10:01:44 +0200619static void vc4_hdmi_encoder_disable(struct drm_encoder *encoder)
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200620{
Maxime Ripard09c43812020-09-03 10:01:44 +0200621}
622
Maxime Ripard89f31a22020-09-03 10:01:27 +0200623static void vc4_hdmi_csc_setup(struct vc4_hdmi *vc4_hdmi, bool enable)
624{
625 u32 csc_ctl;
626
627 csc_ctl = VC4_SET_FIELD(VC4_HD_CSC_CTL_ORDER_BGR,
628 VC4_HD_CSC_CTL_ORDER);
629
630 if (enable) {
631 /* CEA VICs other than #1 requre limited range RGB
632 * output unless overridden by an AVI infoframe.
633 * Apply a colorspace conversion to squash 0-255 down
634 * to 16-235. The matrix here is:
635 *
636 * [ 0 0 0.8594 16]
637 * [ 0 0.8594 0 16]
638 * [ 0.8594 0 0 16]
639 * [ 0 0 0 1]
640 */
641 csc_ctl |= VC4_HD_CSC_CTL_ENABLE;
642 csc_ctl |= VC4_HD_CSC_CTL_RGB2YCC;
643 csc_ctl |= VC4_SET_FIELD(VC4_HD_CSC_CTL_MODE_CUSTOM,
644 VC4_HD_CSC_CTL_MODE);
645
646 HDMI_WRITE(HDMI_CSC_12_11, (0x000 << 16) | 0x000);
647 HDMI_WRITE(HDMI_CSC_14_13, (0x100 << 16) | 0x6e0);
648 HDMI_WRITE(HDMI_CSC_22_21, (0x6e0 << 16) | 0x000);
649 HDMI_WRITE(HDMI_CSC_24_23, (0x100 << 16) | 0x000);
650 HDMI_WRITE(HDMI_CSC_32_31, (0x000 << 16) | 0x6e0);
651 HDMI_WRITE(HDMI_CSC_34_33, (0x100 << 16) | 0x000);
652 }
653
654 /* The RGB order applies even when CSC is disabled. */
655 HDMI_WRITE(HDMI_CSC_CTL, csc_ctl);
656}
657
Maxime Ripard83239892020-09-03 10:01:48 +0200658static void vc5_hdmi_csc_setup(struct vc4_hdmi *vc4_hdmi, bool enable)
659{
660 u32 csc_ctl;
661
662 csc_ctl = 0x07; /* RGB_CONVERT_MODE = custom matrix, || USE_RGB_TO_YCBCR */
663
664 if (enable) {
665 /* CEA VICs other than #1 requre limited range RGB
666 * output unless overridden by an AVI infoframe.
667 * Apply a colorspace conversion to squash 0-255 down
668 * to 16-235. The matrix here is:
669 *
670 * [ 0.8594 0 0 16]
671 * [ 0 0.8594 0 16]
672 * [ 0 0 0.8594 16]
673 * [ 0 0 0 1]
674 * Matrix is signed 2p13 fixed point, with signed 9p6 offsets
675 */
676 HDMI_WRITE(HDMI_CSC_12_11, (0x0000 << 16) | 0x1b80);
677 HDMI_WRITE(HDMI_CSC_14_13, (0x0400 << 16) | 0x0000);
678 HDMI_WRITE(HDMI_CSC_22_21, (0x1b80 << 16) | 0x0000);
679 HDMI_WRITE(HDMI_CSC_24_23, (0x0400 << 16) | 0x0000);
680 HDMI_WRITE(HDMI_CSC_32_31, (0x0000 << 16) | 0x0000);
681 HDMI_WRITE(HDMI_CSC_34_33, (0x0400 << 16) | 0x1b80);
682 } else {
683 /* Still use the matrix for full range, but make it unity.
684 * Matrix is signed 2p13 fixed point, with signed 9p6 offsets
685 */
686 HDMI_WRITE(HDMI_CSC_12_11, (0x0000 << 16) | 0x2000);
687 HDMI_WRITE(HDMI_CSC_14_13, (0x0000 << 16) | 0x0000);
688 HDMI_WRITE(HDMI_CSC_22_21, (0x2000 << 16) | 0x0000);
689 HDMI_WRITE(HDMI_CSC_24_23, (0x0000 << 16) | 0x0000);
690 HDMI_WRITE(HDMI_CSC_32_31, (0x0000 << 16) | 0x0000);
691 HDMI_WRITE(HDMI_CSC_34_33, (0x0000 << 16) | 0x2000);
692 }
693
694 HDMI_WRITE(HDMI_CSC_CTL, csc_ctl);
695}
696
Maxime Ripard904f6682020-09-03 10:01:28 +0200697static void vc4_hdmi_set_timings(struct vc4_hdmi *vc4_hdmi,
Maxime Ripardba8c0fa2020-12-15 16:42:43 +0100698 struct drm_connector_state *state,
Maxime Ripard904f6682020-09-03 10:01:28 +0200699 struct drm_display_mode *mode)
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200700{
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800701 bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
702 bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
Eric Anholt682e62c2016-09-28 17:30:25 -0700703 bool interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE;
Eric Anholtdfccd932016-09-29 15:34:44 -0700704 u32 pixel_rep = (mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1;
Eric Anholt682e62c2016-09-28 17:30:25 -0700705 u32 verta = (VC4_SET_FIELD(mode->crtc_vsync_end - mode->crtc_vsync_start,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800706 VC4_HDMI_VERTA_VSP) |
Eric Anholt682e62c2016-09-28 17:30:25 -0700707 VC4_SET_FIELD(mode->crtc_vsync_start - mode->crtc_vdisplay,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800708 VC4_HDMI_VERTA_VFP) |
Eric Anholt682e62c2016-09-28 17:30:25 -0700709 VC4_SET_FIELD(mode->crtc_vdisplay, VC4_HDMI_VERTA_VAL));
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800710 u32 vertb = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) |
Eric Anholt682e62c2016-09-28 17:30:25 -0700711 VC4_SET_FIELD(mode->crtc_vtotal - mode->crtc_vsync_end,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800712 VC4_HDMI_VERTB_VBP));
Eric Anholt682e62c2016-09-28 17:30:25 -0700713 u32 vertb_even = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) |
714 VC4_SET_FIELD(mode->crtc_vtotal -
715 mode->crtc_vsync_end -
716 interlaced,
717 VC4_HDMI_VERTB_VBP));
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200718
Maxime Ripard904f6682020-09-03 10:01:28 +0200719 HDMI_WRITE(HDMI_HORZA,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800720 (vsync_pos ? VC4_HDMI_HORZA_VPOS : 0) |
721 (hsync_pos ? VC4_HDMI_HORZA_HPOS : 0) |
Eric Anholtdfccd932016-09-29 15:34:44 -0700722 VC4_SET_FIELD(mode->hdisplay * pixel_rep,
723 VC4_HDMI_HORZA_HAP));
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800724
Maxime Ripard904f6682020-09-03 10:01:28 +0200725 HDMI_WRITE(HDMI_HORZB,
Eric Anholtdfccd932016-09-29 15:34:44 -0700726 VC4_SET_FIELD((mode->htotal -
727 mode->hsync_end) * pixel_rep,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800728 VC4_HDMI_HORZB_HBP) |
Eric Anholtdfccd932016-09-29 15:34:44 -0700729 VC4_SET_FIELD((mode->hsync_end -
730 mode->hsync_start) * pixel_rep,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800731 VC4_HDMI_HORZB_HSP) |
Eric Anholtdfccd932016-09-29 15:34:44 -0700732 VC4_SET_FIELD((mode->hsync_start -
733 mode->hdisplay) * pixel_rep,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800734 VC4_HDMI_HORZB_HFP));
735
Maxime Ripard904f6682020-09-03 10:01:28 +0200736 HDMI_WRITE(HDMI_VERTA0, verta);
737 HDMI_WRITE(HDMI_VERTA1, verta);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800738
Maxime Ripard904f6682020-09-03 10:01:28 +0200739 HDMI_WRITE(HDMI_VERTB0, vertb_even);
740 HDMI_WRITE(HDMI_VERTB1, vertb);
Maxime Ripard904f6682020-09-03 10:01:28 +0200741}
Maxime Ripardba8c0fa2020-12-15 16:42:43 +0100742
Maxime Ripard83239892020-09-03 10:01:48 +0200743static void vc5_hdmi_set_timings(struct vc4_hdmi *vc4_hdmi,
Maxime Ripardba8c0fa2020-12-15 16:42:43 +0100744 struct drm_connector_state *state,
Maxime Ripard83239892020-09-03 10:01:48 +0200745 struct drm_display_mode *mode)
746{
747 bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
748 bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
749 bool interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE;
750 u32 pixel_rep = (mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1;
751 u32 verta = (VC4_SET_FIELD(mode->crtc_vsync_end - mode->crtc_vsync_start,
752 VC5_HDMI_VERTA_VSP) |
753 VC4_SET_FIELD(mode->crtc_vsync_start - mode->crtc_vdisplay,
754 VC5_HDMI_VERTA_VFP) |
755 VC4_SET_FIELD(mode->crtc_vdisplay, VC5_HDMI_VERTA_VAL));
756 u32 vertb = (VC4_SET_FIELD(0, VC5_HDMI_VERTB_VSPO) |
757 VC4_SET_FIELD(mode->crtc_vtotal - mode->crtc_vsync_end,
758 VC4_HDMI_VERTB_VBP));
759 u32 vertb_even = (VC4_SET_FIELD(0, VC5_HDMI_VERTB_VSPO) |
760 VC4_SET_FIELD(mode->crtc_vtotal -
761 mode->crtc_vsync_end -
762 interlaced,
763 VC4_HDMI_VERTB_VBP));
Maxime Ripardba8c0fa2020-12-15 16:42:43 +0100764 unsigned char gcp;
765 bool gcp_en;
766 u32 reg;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800767
Maxime Ripard83239892020-09-03 10:01:48 +0200768 HDMI_WRITE(HDMI_VEC_INTERFACE_XBAR, 0x354021);
769 HDMI_WRITE(HDMI_HORZA,
770 (vsync_pos ? VC5_HDMI_HORZA_VPOS : 0) |
771 (hsync_pos ? VC5_HDMI_HORZA_HPOS : 0) |
772 VC4_SET_FIELD(mode->hdisplay * pixel_rep,
773 VC5_HDMI_HORZA_HAP) |
774 VC4_SET_FIELD((mode->hsync_start -
775 mode->hdisplay) * pixel_rep,
776 VC5_HDMI_HORZA_HFP));
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800777
Maxime Ripard83239892020-09-03 10:01:48 +0200778 HDMI_WRITE(HDMI_HORZB,
779 VC4_SET_FIELD((mode->htotal -
780 mode->hsync_end) * pixel_rep,
781 VC5_HDMI_HORZB_HBP) |
782 VC4_SET_FIELD((mode->hsync_end -
783 mode->hsync_start) * pixel_rep,
784 VC5_HDMI_HORZB_HSP));
Eric Anholt6e1cbba2016-09-16 10:59:45 +0100785
Maxime Ripard83239892020-09-03 10:01:48 +0200786 HDMI_WRITE(HDMI_VERTA0, verta);
787 HDMI_WRITE(HDMI_VERTA1, verta);
Eric Anholt6e1cbba2016-09-16 10:59:45 +0100788
Maxime Ripard83239892020-09-03 10:01:48 +0200789 HDMI_WRITE(HDMI_VERTB0, vertb_even);
790 HDMI_WRITE(HDMI_VERTB1, vertb);
Eric Anholt6e1cbba2016-09-16 10:59:45 +0100791
Maxime Ripardba8c0fa2020-12-15 16:42:43 +0100792 switch (state->max_bpc) {
793 case 12:
794 gcp = 6;
795 gcp_en = true;
796 break;
797 case 10:
798 gcp = 5;
799 gcp_en = true;
800 break;
801 case 8:
802 default:
803 gcp = 4;
804 gcp_en = false;
805 break;
806 }
807
808 reg = HDMI_READ(HDMI_DEEP_COLOR_CONFIG_1);
809 reg &= ~(VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE_MASK |
810 VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH_MASK);
811 reg |= VC4_SET_FIELD(2, VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE) |
812 VC4_SET_FIELD(gcp, VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH);
813 HDMI_WRITE(HDMI_DEEP_COLOR_CONFIG_1, reg);
814
815 reg = HDMI_READ(HDMI_GCP_WORD_1);
816 reg &= ~VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1_MASK;
817 reg |= VC4_SET_FIELD(gcp, VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1);
818 HDMI_WRITE(HDMI_GCP_WORD_1, reg);
819
820 reg = HDMI_READ(HDMI_GCP_CONFIG);
821 reg &= ~VC5_HDMI_GCP_CONFIG_GCP_ENABLE;
822 reg |= gcp_en ? VC5_HDMI_GCP_CONFIG_GCP_ENABLE : 0;
823 HDMI_WRITE(HDMI_GCP_CONFIG, reg);
824
Maxime Ripard83239892020-09-03 10:01:48 +0200825 HDMI_WRITE(HDMI_CLOCK_STOP, 0);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800826}
827
Maxime Ripard691456f2020-09-03 10:01:43 +0200828static void vc4_hdmi_recenter_fifo(struct vc4_hdmi *vc4_hdmi)
Eric Anholt32e823c2017-09-20 15:59:34 -0700829{
Maxime Ripard691456f2020-09-03 10:01:43 +0200830 u32 drift;
831 int ret;
832
833 drift = HDMI_READ(HDMI_FIFO_CTL);
834 drift &= VC4_HDMI_FIFO_VALID_WRITE_MASK;
835
836 HDMI_WRITE(HDMI_FIFO_CTL,
837 drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
838 HDMI_WRITE(HDMI_FIFO_CTL,
839 drift | VC4_HDMI_FIFO_CTL_RECENTER);
840 usleep_range(1000, 1100);
841 HDMI_WRITE(HDMI_FIFO_CTL,
842 drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
843 HDMI_WRITE(HDMI_FIFO_CTL,
844 drift | VC4_HDMI_FIFO_CTL_RECENTER);
845
846 ret = wait_for(HDMI_READ(HDMI_FIFO_CTL) &
847 VC4_HDMI_FIFO_CTL_RECENTER_DONE, 1);
848 WARN_ONCE(ret, "Timeout waiting for "
849 "VC4_HDMI_FIFO_CTL_RECENTER_DONE");
850}
851
Maxime Ripardf6237462020-12-15 16:42:40 +0100852static struct drm_connector_state *
853vc4_hdmi_encoder_get_connector_state(struct drm_encoder *encoder,
854 struct drm_atomic_state *state)
855{
856 struct drm_connector_state *conn_state;
857 struct drm_connector *connector;
858 unsigned int i;
859
860 for_each_new_connector_in_state(state, connector, conn_state, i) {
861 if (conn_state->best_encoder == encoder)
862 return conn_state;
863 }
864
865 return NULL;
866}
867
Maxime Ripard8d914742020-12-15 16:42:36 +0100868static void vc4_hdmi_encoder_pre_crtc_configure(struct drm_encoder *encoder,
869 struct drm_atomic_state *state)
Maxime Ripard904f6682020-09-03 10:01:28 +0200870{
Maxime Ripardf6237462020-12-15 16:42:40 +0100871 struct drm_connector_state *conn_state =
872 vc4_hdmi_encoder_get_connector_state(encoder, state);
873 struct vc4_hdmi_connector_state *vc4_conn_state =
874 conn_state_to_vc4_hdmi_conn_state(conn_state);
Maxime Ripard904f6682020-09-03 10:01:28 +0200875 struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
876 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
Maxime Ripard7d9061e2021-05-07 17:05:11 +0200877 unsigned long bvb_rate, pixel_rate, hsm_rate;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800878 int ret;
879
Maxime Ripard3408cc22020-09-03 10:01:14 +0200880 ret = pm_runtime_get_sync(&vc4_hdmi->pdev->dev);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800881 if (ret < 0) {
882 DRM_ERROR("Failed to retain power domain: %d\n", ret);
883 return;
884 }
885
Maxime Ripardf6237462020-12-15 16:42:40 +0100886 pixel_rate = vc4_conn_state->pixel_rate;
Maxime Ripardcd4cb492020-09-03 10:01:35 +0200887 ret = clk_set_rate(vc4_hdmi->pixel_clock, pixel_rate);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800888 if (ret) {
889 DRM_ERROR("Failed to set pixel clock rate: %d\n", ret);
890 return;
891 }
892
Maxime Ripard3408cc22020-09-03 10:01:14 +0200893 ret = clk_prepare_enable(vc4_hdmi->pixel_clock);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800894 if (ret) {
895 DRM_ERROR("Failed to turn on pixel clock: %d\n", ret);
896 return;
897 }
898
Nicolas Saenz Julienneb1e73962020-03-26 13:20:01 +0100899 /*
900 * As stated in RPi's vc4 firmware "HDMI state machine (HSM) clock must
901 * be faster than pixel clock, infinitesimally faster, tested in
902 * simulation. Otherwise, exact value is unimportant for HDMI
903 * operation." This conflicts with bcm2835's vc4 documentation, which
904 * states HSM's clock has to be at least 108% of the pixel clock.
905 *
906 * Real life tests reveal that vc4's firmware statement holds up, and
907 * users are able to use pixel clocks closer to HSM's, namely for
908 * 1920x1200@60Hz. So it was decided to have leave a 1% margin between
909 * both clocks. Which, for RPi0-3 implies a maximum pixel clock of
910 * 162MHz.
911 *
912 * Additionally, the AXI clock needs to be at least 25% of
913 * pixel clock, but HSM ends up being the limiting factor.
Eric Anholt32e823c2017-09-20 15:59:34 -0700914 */
Maxime Ripardcd4cb492020-09-03 10:01:35 +0200915 hsm_rate = max_t(unsigned long, 120000000, (pixel_rate / 100) * 101);
Maxime Ripardd5d5ce82020-09-03 10:01:36 +0200916 ret = clk_set_min_rate(vc4_hdmi->hsm_clock, hsm_rate);
Maxime Ripardcd4cb492020-09-03 10:01:35 +0200917 if (ret) {
918 DRM_ERROR("Failed to set HSM clock rate: %d\n", ret);
919 return;
920 }
921
922 ret = clk_prepare_enable(vc4_hdmi->hsm_clock);
923 if (ret) {
924 DRM_ERROR("Failed to turn on HSM clock: %d\n", ret);
925 clk_disable_unprepare(vc4_hdmi->pixel_clock);
926 return;
927 }
928
Maxime Ripard47fa9a82021-01-11 15:23:01 +0100929 vc4_hdmi_cec_update_clk_div(vc4_hdmi);
930
Maxime Ripard7d9061e2021-05-07 17:05:11 +0200931 if (pixel_rate > 297000000)
932 bvb_rate = 300000000;
933 else if (pixel_rate > 148500000)
934 bvb_rate = 150000000;
935 else
936 bvb_rate = 75000000;
937
938 ret = clk_set_min_rate(vc4_hdmi->pixel_bvb_clock, bvb_rate);
Hoegeun Kwon37387422020-09-03 10:01:47 +0200939 if (ret) {
940 DRM_ERROR("Failed to set pixel bvb clock rate: %d\n", ret);
941 clk_disable_unprepare(vc4_hdmi->hsm_clock);
942 clk_disable_unprepare(vc4_hdmi->pixel_clock);
943 return;
944 }
945
946 ret = clk_prepare_enable(vc4_hdmi->pixel_bvb_clock);
947 if (ret) {
948 DRM_ERROR("Failed to turn on pixel bvb clock: %d\n", ret);
949 clk_disable_unprepare(vc4_hdmi->hsm_clock);
950 clk_disable_unprepare(vc4_hdmi->pixel_clock);
951 return;
952 }
953
Maxime Ripardc457b8a2020-09-03 10:01:25 +0200954 if (vc4_hdmi->variant->phy_init)
Maxime Ripardd2a7dd02020-12-15 16:42:41 +0100955 vc4_hdmi->variant->phy_init(vc4_hdmi, vc4_conn_state);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800956
Maxime Ripard311e3052020-09-03 10:01:23 +0200957 HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
958 HDMI_READ(HDMI_SCHEDULER_CONTROL) |
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800959 VC4_HDMI_SCHEDULER_CONTROL_MANUAL_FORMAT |
960 VC4_HDMI_SCHEDULER_CONTROL_IGNORE_VSYNC_PREDICTS);
961
Maxime Ripard904f6682020-09-03 10:01:28 +0200962 if (vc4_hdmi->variant->set_timings)
Maxime Ripardba8c0fa2020-12-15 16:42:43 +0100963 vc4_hdmi->variant->set_timings(vc4_hdmi, conn_state, mode);
Maxime Ripard09c43812020-09-03 10:01:44 +0200964}
965
Maxime Ripard8d914742020-12-15 16:42:36 +0100966static void vc4_hdmi_encoder_pre_crtc_enable(struct drm_encoder *encoder,
967 struct drm_atomic_state *state)
Maxime Ripard09c43812020-09-03 10:01:44 +0200968{
969 struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
970 struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
971 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800972
973 if (vc4_encoder->hdmi_monitor &&
Maxime Ripard89f31a22020-09-03 10:01:27 +0200974 drm_default_rgb_quant_range(mode) == HDMI_QUANTIZATION_RANGE_LIMITED) {
975 if (vc4_hdmi->variant->csc_setup)
976 vc4_hdmi->variant->csc_setup(vc4_hdmi, true);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800977
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800978 vc4_encoder->limited_rgb_range = true;
979 } else {
Maxime Ripard89f31a22020-09-03 10:01:27 +0200980 if (vc4_hdmi->variant->csc_setup)
981 vc4_hdmi->variant->csc_setup(vc4_hdmi, false);
982
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800983 vc4_encoder->limited_rgb_range = false;
984 }
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800985
Maxime Ripard311e3052020-09-03 10:01:23 +0200986 HDMI_WRITE(HDMI_FIFO_CTL, VC4_HDMI_FIFO_CTL_MASTER_SLAVE_N);
Maxime Ripard09c43812020-09-03 10:01:44 +0200987}
988
Maxime Ripard8d914742020-12-15 16:42:36 +0100989static void vc4_hdmi_encoder_post_crtc_enable(struct drm_encoder *encoder,
990 struct drm_atomic_state *state)
Maxime Ripard09c43812020-09-03 10:01:44 +0200991{
Maxime Ripard8b3f90e2020-09-03 10:01:45 +0200992 struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
Maxime Ripard09c43812020-09-03 10:01:44 +0200993 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
994 struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
Maxime Ripard8b3f90e2020-09-03 10:01:45 +0200995 bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
996 bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
Maxime Ripard09c43812020-09-03 10:01:44 +0200997 int ret;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800998
Maxime Ripard311e3052020-09-03 10:01:23 +0200999 HDMI_WRITE(HDMI_VID_CTL,
Maxime Ripard311e3052020-09-03 10:01:23 +02001000 VC4_HD_VID_CTL_ENABLE |
1001 VC4_HD_VID_CTL_UNDERFLOW_ENABLE |
Maxime Ripard8b3f90e2020-09-03 10:01:45 +02001002 VC4_HD_VID_CTL_FRAME_COUNTER_RESET |
1003 (vsync_pos ? 0 : VC4_HD_VID_CTL_VSYNC_LOW) |
1004 (hsync_pos ? 0 : VC4_HD_VID_CTL_HSYNC_LOW));
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001005
Maxime Ripard81d83012020-09-03 10:01:46 +02001006 HDMI_WRITE(HDMI_VID_CTL,
1007 HDMI_READ(HDMI_VID_CTL) & ~VC4_HD_VID_CTL_BLANKPIX);
1008
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001009 if (vc4_encoder->hdmi_monitor) {
Maxime Ripard311e3052020-09-03 10:01:23 +02001010 HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
1011 HDMI_READ(HDMI_SCHEDULER_CONTROL) |
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001012 VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);
1013
Maxime Ripard311e3052020-09-03 10:01:23 +02001014 ret = wait_for(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001015 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE, 1000);
1016 WARN_ONCE(ret, "Timeout waiting for "
1017 "VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
1018 } else {
Maxime Ripard311e3052020-09-03 10:01:23 +02001019 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
1020 HDMI_READ(HDMI_RAM_PACKET_CONFIG) &
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001021 ~(VC4_HDMI_RAM_PACKET_ENABLE));
Maxime Ripard311e3052020-09-03 10:01:23 +02001022 HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
1023 HDMI_READ(HDMI_SCHEDULER_CONTROL) &
Eric Anholt851479a2016-02-12 14:15:14 -08001024 ~VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);
1025
Maxime Ripard311e3052020-09-03 10:01:23 +02001026 ret = wait_for(!(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
Eric Anholt851479a2016-02-12 14:15:14 -08001027 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE), 1000);
1028 WARN_ONCE(ret, "Timeout waiting for "
1029 "!VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
1030 }
1031
1032 if (vc4_encoder->hdmi_monitor) {
Maxime Ripard311e3052020-09-03 10:01:23 +02001033 WARN_ON(!(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001034 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE));
Maxime Ripard311e3052020-09-03 10:01:23 +02001035 HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
1036 HDMI_READ(HDMI_SCHEDULER_CONTROL) |
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001037 VC4_HDMI_SCHEDULER_CONTROL_VERT_ALWAYS_KEEPOUT);
1038
Maxime Ripard311e3052020-09-03 10:01:23 +02001039 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001040 VC4_HDMI_RAM_PACKET_ENABLE);
1041
Eric Anholt0b06e0a2016-02-29 17:53:01 -08001042 vc4_hdmi_set_infoframes(encoder);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001043 }
Maxime Ripard691456f2020-09-03 10:01:43 +02001044
1045 vc4_hdmi_recenter_fifo(vc4_hdmi);
Maxime Ripardc85695a2021-05-07 17:05:13 +02001046 vc4_hdmi_enable_scrambling(encoder);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001047}
1048
Maxime Ripard09c43812020-09-03 10:01:44 +02001049static void vc4_hdmi_encoder_enable(struct drm_encoder *encoder)
1050{
1051}
1052
Maxime Ripard9fa1d7e2020-10-29 14:40:17 +01001053#define WIFI_2_4GHz_CH1_MIN_FREQ 2400000000ULL
1054#define WIFI_2_4GHz_CH1_MAX_FREQ 2422000000ULL
1055
Maxime Ripard63495f6b2020-10-29 13:25:21 +01001056static int vc4_hdmi_encoder_atomic_check(struct drm_encoder *encoder,
1057 struct drm_crtc_state *crtc_state,
1058 struct drm_connector_state *conn_state)
1059{
Maxime Ripardf6237462020-12-15 16:42:40 +01001060 struct vc4_hdmi_connector_state *vc4_state = conn_state_to_vc4_hdmi_conn_state(conn_state);
Maxime Ripard63495f6b2020-10-29 13:25:21 +01001061 struct drm_display_mode *mode = &crtc_state->adjusted_mode;
1062 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1063 unsigned long long pixel_rate = mode->clock * 1000;
Maxime Ripard9fa1d7e2020-10-29 14:40:17 +01001064 unsigned long long tmds_rate;
Maxime Ripard63495f6b2020-10-29 13:25:21 +01001065
Maxime Ripard57fb32e2020-10-29 13:25:22 +01001066 if (vc4_hdmi->variant->unsupported_odd_h_timings &&
1067 ((mode->hdisplay % 2) || (mode->hsync_start % 2) ||
1068 (mode->hsync_end % 2) || (mode->htotal % 2)))
1069 return -EINVAL;
1070
Maxime Ripard9fa1d7e2020-10-29 14:40:17 +01001071 /*
1072 * The 1440p@60 pixel rate is in the same range than the first
1073 * WiFi channel (between 2.4GHz and 2.422GHz with 22MHz
1074 * bandwidth). Slightly lower the frequency to bring it out of
1075 * the WiFi range.
1076 */
1077 tmds_rate = pixel_rate * 10;
1078 if (vc4_hdmi->disable_wifi_frequencies &&
1079 (tmds_rate >= WIFI_2_4GHz_CH1_MIN_FREQ &&
1080 tmds_rate <= WIFI_2_4GHz_CH1_MAX_FREQ)) {
1081 mode->clock = 238560;
1082 pixel_rate = mode->clock * 1000;
1083 }
1084
Maxime Ripardba8c0fa2020-12-15 16:42:43 +01001085 if (conn_state->max_bpc == 12) {
1086 pixel_rate = pixel_rate * 150;
1087 do_div(pixel_rate, 100);
1088 } else if (conn_state->max_bpc == 10) {
1089 pixel_rate = pixel_rate * 125;
1090 do_div(pixel_rate, 100);
1091 }
1092
Maxime Ripard320e84d2020-12-15 16:42:37 +01001093 if (mode->flags & DRM_MODE_FLAG_DBLCLK)
1094 pixel_rate = pixel_rate * 2;
1095
Maxime Ripard63495f6b2020-10-29 13:25:21 +01001096 if (pixel_rate > vc4_hdmi->variant->max_pixel_clock)
1097 return -EINVAL;
1098
Maxime Ripard86e3a652021-05-07 17:05:12 +02001099 if (vc4_hdmi->disable_4kp60 && (pixel_rate > HDMI_14_MAX_TMDS_CLK))
1100 return -EINVAL;
1101
Maxime Ripardf6237462020-12-15 16:42:40 +01001102 vc4_state->pixel_rate = pixel_rate;
1103
Maxime Ripard63495f6b2020-10-29 13:25:21 +01001104 return 0;
1105}
1106
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001107static enum drm_mode_status
Maxime Ripard11a17312020-09-03 10:01:34 +02001108vc4_hdmi_encoder_mode_valid(struct drm_encoder *encoder,
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001109 const struct drm_display_mode *mode)
1110{
Maxime Ripardcd4cb492020-09-03 10:01:35 +02001111 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1112
Maxime Ripard57fb32e2020-10-29 13:25:22 +01001113 if (vc4_hdmi->variant->unsupported_odd_h_timings &&
1114 ((mode->hdisplay % 2) || (mode->hsync_start % 2) ||
1115 (mode->hsync_end % 2) || (mode->htotal % 2)))
1116 return MODE_H_ILLEGAL;
1117
Maxime Ripardcd4cb492020-09-03 10:01:35 +02001118 if ((mode->clock * 1000) > vc4_hdmi->variant->max_pixel_clock)
Eric Anholt32e823c2017-09-20 15:59:34 -07001119 return MODE_CLOCK_HIGH;
1120
Maxime Ripard86e3a652021-05-07 17:05:12 +02001121 if (vc4_hdmi->disable_4kp60 && vc4_hdmi_mode_needs_scrambling(mode))
1122 return MODE_CLOCK_HIGH;
1123
Eric Anholt32e823c2017-09-20 15:59:34 -07001124 return MODE_OK;
1125}
1126
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001127static const struct drm_encoder_helper_funcs vc4_hdmi_encoder_helper_funcs = {
Maxime Ripard63495f6b2020-10-29 13:25:21 +01001128 .atomic_check = vc4_hdmi_encoder_atomic_check,
Eric Anholt32e823c2017-09-20 15:59:34 -07001129 .mode_valid = vc4_hdmi_encoder_mode_valid,
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001130 .disable = vc4_hdmi_encoder_disable,
1131 .enable = vc4_hdmi_encoder_enable,
1132};
1133
Dave Stevenson632ee3a2020-09-03 10:01:40 +02001134static u32 vc4_hdmi_channel_map(struct vc4_hdmi *vc4_hdmi, u32 channel_mask)
Eric Anholtbb7d7852017-02-27 12:28:02 -08001135{
Dave Stevenson632ee3a2020-09-03 10:01:40 +02001136 int i;
1137 u32 channel_map = 0;
1138
1139 for (i = 0; i < 8; i++) {
1140 if (channel_mask & BIT(i))
1141 channel_map |= i << (3 * i);
1142 }
1143 return channel_map;
1144}
1145
Maxime Ripard83239892020-09-03 10:01:48 +02001146static u32 vc5_hdmi_channel_map(struct vc4_hdmi *vc4_hdmi, u32 channel_mask)
1147{
1148 int i;
1149 u32 channel_map = 0;
1150
1151 for (i = 0; i < 8; i++) {
1152 if (channel_mask & BIT(i))
1153 channel_map |= i << (4 * i);
1154 }
1155 return channel_map;
1156}
1157
Eric Anholtbb7d7852017-02-27 12:28:02 -08001158/* HDMI audio codec callbacks */
Maxime Ripard3408cc22020-09-03 10:01:14 +02001159static void vc4_hdmi_audio_set_mai_clock(struct vc4_hdmi *vc4_hdmi)
Eric Anholtbb7d7852017-02-27 12:28:02 -08001160{
Dave Stevenson632ee3a2020-09-03 10:01:40 +02001161 u32 hsm_clock = clk_get_rate(vc4_hdmi->audio_clock);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001162 unsigned long n, m;
1163
Maxime Ripard3408cc22020-09-03 10:01:14 +02001164 rational_best_approximation(hsm_clock, vc4_hdmi->audio.samplerate,
Eric Anholtbb7d7852017-02-27 12:28:02 -08001165 VC4_HD_MAI_SMP_N_MASK >>
1166 VC4_HD_MAI_SMP_N_SHIFT,
1167 (VC4_HD_MAI_SMP_M_MASK >>
1168 VC4_HD_MAI_SMP_M_SHIFT) + 1,
1169 &n, &m);
1170
Maxime Ripard311e3052020-09-03 10:01:23 +02001171 HDMI_WRITE(HDMI_MAI_SMP,
1172 VC4_SET_FIELD(n, VC4_HD_MAI_SMP_N) |
1173 VC4_SET_FIELD(m - 1, VC4_HD_MAI_SMP_M));
Eric Anholtbb7d7852017-02-27 12:28:02 -08001174}
1175
Maxime Ripard3408cc22020-09-03 10:01:14 +02001176static void vc4_hdmi_set_n_cts(struct vc4_hdmi *vc4_hdmi)
Eric Anholtbb7d7852017-02-27 12:28:02 -08001177{
Maxime Ripard3408cc22020-09-03 10:01:14 +02001178 struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
Eric Anholtbb7d7852017-02-27 12:28:02 -08001179 struct drm_crtc *crtc = encoder->crtc;
Eric Anholtbb7d7852017-02-27 12:28:02 -08001180 const struct drm_display_mode *mode = &crtc->state->adjusted_mode;
Maxime Ripard3408cc22020-09-03 10:01:14 +02001181 u32 samplerate = vc4_hdmi->audio.samplerate;
Eric Anholtbb7d7852017-02-27 12:28:02 -08001182 u32 n, cts;
1183 u64 tmp;
1184
1185 n = 128 * samplerate / 1000;
1186 tmp = (u64)(mode->clock * 1000) * n;
1187 do_div(tmp, 128 * samplerate);
1188 cts = tmp;
1189
Maxime Ripard311e3052020-09-03 10:01:23 +02001190 HDMI_WRITE(HDMI_CRP_CFG,
Eric Anholtbb7d7852017-02-27 12:28:02 -08001191 VC4_HDMI_CRP_CFG_EXTERNAL_CTS_EN |
1192 VC4_SET_FIELD(n, VC4_HDMI_CRP_CFG_N));
1193
1194 /*
1195 * We could get slightly more accurate clocks in some cases by
1196 * providing a CTS_1 value. The two CTS values are alternated
1197 * between based on the period fields
1198 */
Maxime Ripard311e3052020-09-03 10:01:23 +02001199 HDMI_WRITE(HDMI_CTS_0, cts);
1200 HDMI_WRITE(HDMI_CTS_1, cts);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001201}
1202
1203static inline struct vc4_hdmi *dai_to_hdmi(struct snd_soc_dai *dai)
1204{
1205 struct snd_soc_card *card = snd_soc_dai_get_drvdata(dai);
1206
1207 return snd_soc_card_get_drvdata(card);
1208}
1209
1210static int vc4_hdmi_audio_startup(struct snd_pcm_substream *substream,
1211 struct snd_soc_dai *dai)
1212{
Maxime Ripard3408cc22020-09-03 10:01:14 +02001213 struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai);
1214 struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
Maxime Ripard0532e5e2020-09-03 10:01:21 +02001215 struct drm_connector *connector = &vc4_hdmi->connector;
Eric Anholtbb7d7852017-02-27 12:28:02 -08001216 int ret;
1217
Maxime Ripard3408cc22020-09-03 10:01:14 +02001218 if (vc4_hdmi->audio.substream && vc4_hdmi->audio.substream != substream)
Eric Anholtbb7d7852017-02-27 12:28:02 -08001219 return -EINVAL;
1220
Maxime Ripard3408cc22020-09-03 10:01:14 +02001221 vc4_hdmi->audio.substream = substream;
Eric Anholtbb7d7852017-02-27 12:28:02 -08001222
1223 /*
1224 * If the HDMI encoder hasn't probed, or the encoder is
1225 * currently in DVI mode, treat the codec dai as missing.
1226 */
Maxime Ripard311e3052020-09-03 10:01:23 +02001227 if (!encoder->crtc || !(HDMI_READ(HDMI_RAM_PACKET_CONFIG) &
Eric Anholtbb7d7852017-02-27 12:28:02 -08001228 VC4_HDMI_RAM_PACKET_ENABLE))
1229 return -ENODEV;
1230
Maxime Ripardc98c85b2020-09-03 10:01:12 +02001231 ret = snd_pcm_hw_constraint_eld(substream->runtime, connector->eld);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001232 if (ret)
1233 return ret;
1234
1235 return 0;
1236}
1237
1238static int vc4_hdmi_audio_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
1239{
1240 return 0;
1241}
1242
Maxime Ripard3408cc22020-09-03 10:01:14 +02001243static void vc4_hdmi_audio_reset(struct vc4_hdmi *vc4_hdmi)
Eric Anholtbb7d7852017-02-27 12:28:02 -08001244{
Maxime Ripard3408cc22020-09-03 10:01:14 +02001245 struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
Maxime Ripard3408cc22020-09-03 10:01:14 +02001246 struct device *dev = &vc4_hdmi->pdev->dev;
Eric Anholtbb7d7852017-02-27 12:28:02 -08001247 int ret;
1248
Dave Stevenson6ac1c752020-09-03 10:01:38 +02001249 vc4_hdmi->audio.streaming = false;
Maxime Riparde2f9b2e2020-12-03 08:46:24 +01001250 ret = vc4_hdmi_stop_packet(encoder, HDMI_INFOFRAME_TYPE_AUDIO, false);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001251 if (ret)
1252 dev_err(dev, "Failed to stop audio infoframe: %d\n", ret);
1253
Maxime Ripard311e3052020-09-03 10:01:23 +02001254 HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_RESET);
1255 HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_ERRORF);
1256 HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_FLUSH);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001257}
1258
1259static void vc4_hdmi_audio_shutdown(struct snd_pcm_substream *substream,
1260 struct snd_soc_dai *dai)
1261{
Maxime Ripard3408cc22020-09-03 10:01:14 +02001262 struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001263
Maxime Ripard3408cc22020-09-03 10:01:14 +02001264 if (substream != vc4_hdmi->audio.substream)
Eric Anholtbb7d7852017-02-27 12:28:02 -08001265 return;
1266
Maxime Ripard3408cc22020-09-03 10:01:14 +02001267 vc4_hdmi_audio_reset(vc4_hdmi);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001268
Maxime Ripard3408cc22020-09-03 10:01:14 +02001269 vc4_hdmi->audio.substream = NULL;
Eric Anholtbb7d7852017-02-27 12:28:02 -08001270}
1271
1272/* HDMI audio codec callbacks */
1273static int vc4_hdmi_audio_hw_params(struct snd_pcm_substream *substream,
1274 struct snd_pcm_hw_params *params,
1275 struct snd_soc_dai *dai)
1276{
Maxime Ripard3408cc22020-09-03 10:01:14 +02001277 struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai);
Maxime Ripard58d04362020-10-27 11:15:58 +01001278 struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
Maxime Ripard3408cc22020-09-03 10:01:14 +02001279 struct device *dev = &vc4_hdmi->pdev->dev;
Eric Anholtbb7d7852017-02-27 12:28:02 -08001280 u32 audio_packet_config, channel_mask;
Dave Stevenson632ee3a2020-09-03 10:01:40 +02001281 u32 channel_map;
Eric Anholtbb7d7852017-02-27 12:28:02 -08001282
Maxime Ripard3408cc22020-09-03 10:01:14 +02001283 if (substream != vc4_hdmi->audio.substream)
Eric Anholtbb7d7852017-02-27 12:28:02 -08001284 return -EINVAL;
1285
1286 dev_dbg(dev, "%s: %u Hz, %d bit, %d channels\n", __func__,
1287 params_rate(params), params_width(params),
1288 params_channels(params));
1289
Maxime Ripard3408cc22020-09-03 10:01:14 +02001290 vc4_hdmi->audio.channels = params_channels(params);
1291 vc4_hdmi->audio.samplerate = params_rate(params);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001292
Maxime Ripard311e3052020-09-03 10:01:23 +02001293 HDMI_WRITE(HDMI_MAI_CTL,
1294 VC4_HD_MAI_CTL_RESET |
1295 VC4_HD_MAI_CTL_FLUSH |
1296 VC4_HD_MAI_CTL_DLATE |
1297 VC4_HD_MAI_CTL_ERRORE |
1298 VC4_HD_MAI_CTL_ERRORF);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001299
Maxime Ripard3408cc22020-09-03 10:01:14 +02001300 vc4_hdmi_audio_set_mai_clock(vc4_hdmi);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001301
Dave Stevensonb9b8bac2020-09-03 10:01:39 +02001302 /* The B frame identifier should match the value used by alsa-lib (8) */
Eric Anholtbb7d7852017-02-27 12:28:02 -08001303 audio_packet_config =
1304 VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_SAMPLE_FLAT |
1305 VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_INACTIVE_CHANNELS |
Dave Stevensonb9b8bac2020-09-03 10:01:39 +02001306 VC4_SET_FIELD(0x8, VC4_HDMI_AUDIO_PACKET_B_FRAME_IDENTIFIER);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001307
Maxime Ripard3408cc22020-09-03 10:01:14 +02001308 channel_mask = GENMASK(vc4_hdmi->audio.channels - 1, 0);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001309 audio_packet_config |= VC4_SET_FIELD(channel_mask,
1310 VC4_HDMI_AUDIO_PACKET_CEA_MASK);
1311
1312 /* Set the MAI threshold. This logic mimics the firmware's. */
Maxime Ripard3408cc22020-09-03 10:01:14 +02001313 if (vc4_hdmi->audio.samplerate > 96000) {
Maxime Ripard311e3052020-09-03 10:01:23 +02001314 HDMI_WRITE(HDMI_MAI_THR,
1315 VC4_SET_FIELD(0x12, VC4_HD_MAI_THR_DREQHIGH) |
1316 VC4_SET_FIELD(0x12, VC4_HD_MAI_THR_DREQLOW));
Maxime Ripard3408cc22020-09-03 10:01:14 +02001317 } else if (vc4_hdmi->audio.samplerate > 48000) {
Maxime Ripard311e3052020-09-03 10:01:23 +02001318 HDMI_WRITE(HDMI_MAI_THR,
1319 VC4_SET_FIELD(0x14, VC4_HD_MAI_THR_DREQHIGH) |
1320 VC4_SET_FIELD(0x12, VC4_HD_MAI_THR_DREQLOW));
Eric Anholtbb7d7852017-02-27 12:28:02 -08001321 } else {
Maxime Ripard311e3052020-09-03 10:01:23 +02001322 HDMI_WRITE(HDMI_MAI_THR,
1323 VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_PANICHIGH) |
1324 VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_PANICLOW) |
1325 VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_DREQHIGH) |
1326 VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_DREQLOW));
Eric Anholtbb7d7852017-02-27 12:28:02 -08001327 }
1328
Maxime Ripard311e3052020-09-03 10:01:23 +02001329 HDMI_WRITE(HDMI_MAI_CONFIG,
Eric Anholtbb7d7852017-02-27 12:28:02 -08001330 VC4_HDMI_MAI_CONFIG_BIT_REVERSE |
1331 VC4_SET_FIELD(channel_mask, VC4_HDMI_MAI_CHANNEL_MASK));
1332
Dave Stevenson632ee3a2020-09-03 10:01:40 +02001333 channel_map = vc4_hdmi->variant->channel_map(vc4_hdmi, channel_mask);
Maxime Ripard311e3052020-09-03 10:01:23 +02001334 HDMI_WRITE(HDMI_MAI_CHANNEL_MAP, channel_map);
1335 HDMI_WRITE(HDMI_AUDIO_PACKET_CONFIG, audio_packet_config);
Maxime Ripard3408cc22020-09-03 10:01:14 +02001336 vc4_hdmi_set_n_cts(vc4_hdmi);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001337
Maxime Ripard58d04362020-10-27 11:15:58 +01001338 vc4_hdmi_set_audio_infoframe(encoder);
1339
Eric Anholtbb7d7852017-02-27 12:28:02 -08001340 return 0;
1341}
1342
1343static int vc4_hdmi_audio_trigger(struct snd_pcm_substream *substream, int cmd,
1344 struct snd_soc_dai *dai)
1345{
Maxime Ripard3408cc22020-09-03 10:01:14 +02001346 struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001347
1348 switch (cmd) {
1349 case SNDRV_PCM_TRIGGER_START:
Dave Stevenson6ac1c752020-09-03 10:01:38 +02001350 vc4_hdmi->audio.streaming = true;
Maxime Ripard647b9652020-09-03 10:01:26 +02001351
1352 if (vc4_hdmi->variant->phy_rng_enable)
1353 vc4_hdmi->variant->phy_rng_enable(vc4_hdmi);
Maxime Ripard311e3052020-09-03 10:01:23 +02001354
1355 HDMI_WRITE(HDMI_MAI_CTL,
1356 VC4_SET_FIELD(vc4_hdmi->audio.channels,
1357 VC4_HD_MAI_CTL_CHNUM) |
1358 VC4_HD_MAI_CTL_ENABLE);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001359 break;
1360 case SNDRV_PCM_TRIGGER_STOP:
Maxime Ripard311e3052020-09-03 10:01:23 +02001361 HDMI_WRITE(HDMI_MAI_CTL,
1362 VC4_HD_MAI_CTL_DLATE |
1363 VC4_HD_MAI_CTL_ERRORE |
1364 VC4_HD_MAI_CTL_ERRORF);
Maxime Ripard647b9652020-09-03 10:01:26 +02001365
1366 if (vc4_hdmi->variant->phy_rng_disable)
1367 vc4_hdmi->variant->phy_rng_disable(vc4_hdmi);
1368
Dave Stevenson6ac1c752020-09-03 10:01:38 +02001369 vc4_hdmi->audio.streaming = false;
1370
Eric Anholtbb7d7852017-02-27 12:28:02 -08001371 break;
1372 default:
1373 break;
1374 }
1375
1376 return 0;
1377}
1378
1379static inline struct vc4_hdmi *
1380snd_component_to_hdmi(struct snd_soc_component *component)
1381{
1382 struct snd_soc_card *card = snd_soc_component_get_drvdata(component);
1383
1384 return snd_soc_card_get_drvdata(card);
1385}
1386
1387static int vc4_hdmi_audio_eld_ctl_info(struct snd_kcontrol *kcontrol,
1388 struct snd_ctl_elem_info *uinfo)
1389{
1390 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Maxime Ripard3408cc22020-09-03 10:01:14 +02001391 struct vc4_hdmi *vc4_hdmi = snd_component_to_hdmi(component);
Maxime Ripard0532e5e2020-09-03 10:01:21 +02001392 struct drm_connector *connector = &vc4_hdmi->connector;
Eric Anholtbb7d7852017-02-27 12:28:02 -08001393
1394 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
Maxime Ripardc98c85b2020-09-03 10:01:12 +02001395 uinfo->count = sizeof(connector->eld);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001396
1397 return 0;
1398}
1399
1400static int vc4_hdmi_audio_eld_ctl_get(struct snd_kcontrol *kcontrol,
1401 struct snd_ctl_elem_value *ucontrol)
1402{
1403 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Maxime Ripard3408cc22020-09-03 10:01:14 +02001404 struct vc4_hdmi *vc4_hdmi = snd_component_to_hdmi(component);
Maxime Ripard0532e5e2020-09-03 10:01:21 +02001405 struct drm_connector *connector = &vc4_hdmi->connector;
Eric Anholtbb7d7852017-02-27 12:28:02 -08001406
Maxime Ripardc98c85b2020-09-03 10:01:12 +02001407 memcpy(ucontrol->value.bytes.data, connector->eld,
1408 sizeof(connector->eld));
Eric Anholtbb7d7852017-02-27 12:28:02 -08001409
1410 return 0;
1411}
1412
1413static const struct snd_kcontrol_new vc4_hdmi_audio_controls[] = {
1414 {
1415 .access = SNDRV_CTL_ELEM_ACCESS_READ |
1416 SNDRV_CTL_ELEM_ACCESS_VOLATILE,
1417 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1418 .name = "ELD",
1419 .info = vc4_hdmi_audio_eld_ctl_info,
1420 .get = vc4_hdmi_audio_eld_ctl_get,
1421 },
1422};
1423
1424static const struct snd_soc_dapm_widget vc4_hdmi_audio_widgets[] = {
1425 SND_SOC_DAPM_OUTPUT("TX"),
1426};
1427
1428static const struct snd_soc_dapm_route vc4_hdmi_audio_routes[] = {
1429 { "TX", NULL, "Playback" },
1430};
1431
Kuninori Morimoto635b1c12018-01-29 04:35:04 +00001432static const struct snd_soc_component_driver vc4_hdmi_audio_component_drv = {
Maxime Riparda3a0ded2020-07-08 16:45:55 +02001433 .name = "vc4-hdmi-codec-dai-component",
Kuninori Morimoto635b1c12018-01-29 04:35:04 +00001434 .controls = vc4_hdmi_audio_controls,
1435 .num_controls = ARRAY_SIZE(vc4_hdmi_audio_controls),
1436 .dapm_widgets = vc4_hdmi_audio_widgets,
1437 .num_dapm_widgets = ARRAY_SIZE(vc4_hdmi_audio_widgets),
1438 .dapm_routes = vc4_hdmi_audio_routes,
1439 .num_dapm_routes = ARRAY_SIZE(vc4_hdmi_audio_routes),
1440 .idle_bias_on = 1,
1441 .use_pmdown_time = 1,
1442 .endianness = 1,
1443 .non_legacy_dai_naming = 1,
Eric Anholtbb7d7852017-02-27 12:28:02 -08001444};
1445
1446static const struct snd_soc_dai_ops vc4_hdmi_audio_dai_ops = {
1447 .startup = vc4_hdmi_audio_startup,
1448 .shutdown = vc4_hdmi_audio_shutdown,
1449 .hw_params = vc4_hdmi_audio_hw_params,
1450 .set_fmt = vc4_hdmi_audio_set_fmt,
1451 .trigger = vc4_hdmi_audio_trigger,
1452};
1453
1454static struct snd_soc_dai_driver vc4_hdmi_audio_codec_dai_drv = {
1455 .name = "vc4-hdmi-hifi",
1456 .playback = {
1457 .stream_name = "Playback",
1458 .channels_min = 2,
1459 .channels_max = 8,
1460 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
1461 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
1462 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
1463 SNDRV_PCM_RATE_192000,
1464 .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
1465 },
1466};
1467
1468static const struct snd_soc_component_driver vc4_hdmi_audio_cpu_dai_comp = {
1469 .name = "vc4-hdmi-cpu-dai-component",
1470};
1471
1472static int vc4_hdmi_audio_cpu_dai_probe(struct snd_soc_dai *dai)
1473{
Maxime Ripard3408cc22020-09-03 10:01:14 +02001474 struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001475
Maxime Ripard3408cc22020-09-03 10:01:14 +02001476 snd_soc_dai_init_dma_data(dai, &vc4_hdmi->audio.dma_data, NULL);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001477
1478 return 0;
1479}
1480
1481static struct snd_soc_dai_driver vc4_hdmi_audio_cpu_dai_drv = {
1482 .name = "vc4-hdmi-cpu-dai",
1483 .probe = vc4_hdmi_audio_cpu_dai_probe,
1484 .playback = {
1485 .stream_name = "Playback",
1486 .channels_min = 1,
1487 .channels_max = 8,
1488 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
1489 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
1490 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
1491 SNDRV_PCM_RATE_192000,
1492 .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
1493 },
1494 .ops = &vc4_hdmi_audio_dai_ops,
1495};
1496
1497static const struct snd_dmaengine_pcm_config pcm_conf = {
1498 .chan_names[SNDRV_PCM_STREAM_PLAYBACK] = "audio-rx",
1499 .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
1500};
1501
Maxime Ripard3408cc22020-09-03 10:01:14 +02001502static int vc4_hdmi_audio_init(struct vc4_hdmi *vc4_hdmi)
Eric Anholtbb7d7852017-02-27 12:28:02 -08001503{
Maxime Ripard311e3052020-09-03 10:01:23 +02001504 const struct vc4_hdmi_register *mai_data =
1505 &vc4_hdmi->variant->registers[HDMI_MAI_DATA];
Maxime Ripard3408cc22020-09-03 10:01:14 +02001506 struct snd_soc_dai_link *dai_link = &vc4_hdmi->audio.link;
1507 struct snd_soc_card *card = &vc4_hdmi->audio.card;
1508 struct device *dev = &vc4_hdmi->pdev->dev;
Eric Anholtbb7d7852017-02-27 12:28:02 -08001509 const __be32 *addr;
Dave Stevenson094864b2020-09-03 10:01:37 +02001510 int index;
Eric Anholtbb7d7852017-02-27 12:28:02 -08001511 int ret;
1512
1513 if (!of_find_property(dev->of_node, "dmas", NULL)) {
1514 dev_warn(dev,
1515 "'dmas' DT property is missing, no HDMI audio\n");
1516 return 0;
1517 }
1518
Maxime Ripard311e3052020-09-03 10:01:23 +02001519 if (mai_data->reg != VC4_HD) {
1520 WARN_ONCE(true, "MAI isn't in the HD block\n");
1521 return -EINVAL;
1522 }
1523
Eric Anholtbb7d7852017-02-27 12:28:02 -08001524 /*
1525 * Get the physical address of VC4_HD_MAI_DATA. We need to retrieve
1526 * the bus address specified in the DT, because the physical address
1527 * (the one returned by platform_get_resource()) is not appropriate
1528 * for DMA transfers.
1529 * This VC/MMU should probably be exposed to avoid this kind of hacks.
1530 */
Dave Stevenson094864b2020-09-03 10:01:37 +02001531 index = of_property_match_string(dev->of_node, "reg-names", "hd");
1532 /* Before BCM2711, we don't have a named register range */
1533 if (index < 0)
1534 index = 1;
1535
1536 addr = of_get_address(dev->of_node, index, NULL, NULL);
1537
Maxime Ripard311e3052020-09-03 10:01:23 +02001538 vc4_hdmi->audio.dma_data.addr = be32_to_cpup(addr) + mai_data->offset;
Maxime Ripard3408cc22020-09-03 10:01:14 +02001539 vc4_hdmi->audio.dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1540 vc4_hdmi->audio.dma_data.maxburst = 2;
Eric Anholtbb7d7852017-02-27 12:28:02 -08001541
1542 ret = devm_snd_dmaengine_pcm_register(dev, &pcm_conf, 0);
1543 if (ret) {
1544 dev_err(dev, "Could not register PCM component: %d\n", ret);
1545 return ret;
1546 }
1547
1548 ret = devm_snd_soc_register_component(dev, &vc4_hdmi_audio_cpu_dai_comp,
1549 &vc4_hdmi_audio_cpu_dai_drv, 1);
1550 if (ret) {
1551 dev_err(dev, "Could not register CPU DAI: %d\n", ret);
1552 return ret;
1553 }
1554
Kuninori Morimoto635b1c12018-01-29 04:35:04 +00001555 /* register component and codec dai */
1556 ret = devm_snd_soc_register_component(dev, &vc4_hdmi_audio_component_drv,
Eric Anholtbb7d7852017-02-27 12:28:02 -08001557 &vc4_hdmi_audio_codec_dai_drv, 1);
1558 if (ret) {
Kuninori Morimoto635b1c12018-01-29 04:35:04 +00001559 dev_err(dev, "Could not register component: %d\n", ret);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001560 return ret;
1561 }
1562
Maxime Ripard3408cc22020-09-03 10:01:14 +02001563 dai_link->cpus = &vc4_hdmi->audio.cpu;
1564 dai_link->codecs = &vc4_hdmi->audio.codec;
1565 dai_link->platforms = &vc4_hdmi->audio.platform;
Kuninori Morimoto0467d8e2019-06-06 13:19:19 +09001566
1567 dai_link->num_cpus = 1;
1568 dai_link->num_codecs = 1;
Kuninori Morimoto8a90efd2019-06-28 10:46:14 +09001569 dai_link->num_platforms = 1;
Kuninori Morimoto0467d8e2019-06-06 13:19:19 +09001570
Eric Anholtbb7d7852017-02-27 12:28:02 -08001571 dai_link->name = "MAI";
1572 dai_link->stream_name = "MAI PCM";
Kuninori Morimoto0467d8e2019-06-06 13:19:19 +09001573 dai_link->codecs->dai_name = vc4_hdmi_audio_codec_dai_drv.name;
1574 dai_link->cpus->dai_name = dev_name(dev);
1575 dai_link->codecs->name = dev_name(dev);
Kuninori Morimoto8a90efd2019-06-28 10:46:14 +09001576 dai_link->platforms->name = dev_name(dev);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001577
1578 card->dai_link = dai_link;
1579 card->num_links = 1;
Maxime Ripard9be43a52020-09-03 10:01:41 +02001580 card->name = vc4_hdmi->variant->card_name;
Nicolas Saenz Julienne33c74532021-01-15 20:12:09 +01001581 card->driver_name = "vc4-hdmi";
Eric Anholtbb7d7852017-02-27 12:28:02 -08001582 card->dev = dev;
Marek Szyprowskiec653df2020-07-01 09:39:49 +02001583 card->owner = THIS_MODULE;
Eric Anholtbb7d7852017-02-27 12:28:02 -08001584
1585 /*
1586 * Be careful, snd_soc_register_card() calls dev_set_drvdata() and
1587 * stores a pointer to the snd card object in dev->driver_data. This
1588 * means we cannot use it for something else. The hdmi back-pointer is
1589 * now stored in card->drvdata and should be retrieved with
1590 * snd_soc_card_get_drvdata() if needed.
1591 */
Maxime Ripard3408cc22020-09-03 10:01:14 +02001592 snd_soc_card_set_drvdata(card, vc4_hdmi);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001593 ret = devm_snd_soc_register_card(dev, card);
Kuninori Morimoto635b1c12018-01-29 04:35:04 +00001594 if (ret)
Eric Anholtbb7d7852017-02-27 12:28:02 -08001595 dev_err(dev, "Could not register sound card: %d\n", ret);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001596
1597 return ret;
Eric Anholtbb7d7852017-02-27 12:28:02 -08001598
Eric Anholtbb7d7852017-02-27 12:28:02 -08001599}
1600
Hans Verkuil15b45112017-07-16 12:48:04 +02001601#ifdef CONFIG_DRM_VC4_HDMI_CEC
Maxime Riparded4a6bb2021-01-11 15:23:03 +01001602static irqreturn_t vc4_cec_irq_handler_rx_thread(int irq, void *priv)
Hans Verkuil15b45112017-07-16 12:48:04 +02001603{
Maxime Ripard66bf1c32020-09-03 10:01:18 +02001604 struct vc4_hdmi *vc4_hdmi = priv;
Hans Verkuil15b45112017-07-16 12:48:04 +02001605
Maxime Riparded4a6bb2021-01-11 15:23:03 +01001606 if (vc4_hdmi->cec_rx_msg.len)
1607 cec_received_msg(vc4_hdmi->cec_adap,
1608 &vc4_hdmi->cec_rx_msg);
1609
1610 return IRQ_HANDLED;
1611}
1612
1613static irqreturn_t vc4_cec_irq_handler_tx_thread(int irq, void *priv)
1614{
1615 struct vc4_hdmi *vc4_hdmi = priv;
1616
1617 if (vc4_hdmi->cec_tx_ok) {
Maxime Ripard3408cc22020-09-03 10:01:14 +02001618 cec_transmit_done(vc4_hdmi->cec_adap, CEC_TX_STATUS_OK,
Hans Verkuil15b45112017-07-16 12:48:04 +02001619 0, 0, 0, 0);
1620 } else {
1621 /*
1622 * This CEC implementation makes 1 retry, so if we
1623 * get a NACK, then that means it made 2 attempts.
1624 */
Maxime Ripard3408cc22020-09-03 10:01:14 +02001625 cec_transmit_done(vc4_hdmi->cec_adap, CEC_TX_STATUS_NACK,
Hans Verkuil15b45112017-07-16 12:48:04 +02001626 0, 2, 0, 0);
1627 }
1628 return IRQ_HANDLED;
1629}
1630
Maxime Riparded4a6bb2021-01-11 15:23:03 +01001631static irqreturn_t vc4_cec_irq_handler_thread(int irq, void *priv)
1632{
1633 struct vc4_hdmi *vc4_hdmi = priv;
1634 irqreturn_t ret;
1635
1636 if (vc4_hdmi->cec_irq_was_rx)
1637 ret = vc4_cec_irq_handler_rx_thread(irq, priv);
1638 else
1639 ret = vc4_cec_irq_handler_tx_thread(irq, priv);
1640
1641 return ret;
1642}
1643
Maxime Ripard66bf1c32020-09-03 10:01:18 +02001644static void vc4_cec_read_msg(struct vc4_hdmi *vc4_hdmi, u32 cntrl1)
Hans Verkuil15b45112017-07-16 12:48:04 +02001645{
Dom Cobley4a59ed52021-01-11 15:22:57 +01001646 struct drm_device *dev = vc4_hdmi->connector.dev;
Maxime Ripard13311452020-09-03 10:01:15 +02001647 struct cec_msg *msg = &vc4_hdmi->cec_rx_msg;
Hans Verkuil15b45112017-07-16 12:48:04 +02001648 unsigned int i;
1649
1650 msg->len = 1 + ((cntrl1 & VC4_HDMI_CEC_REC_WRD_CNT_MASK) >>
1651 VC4_HDMI_CEC_REC_WRD_CNT_SHIFT);
Dom Cobley4a59ed52021-01-11 15:22:57 +01001652
1653 if (msg->len > 16) {
1654 drm_err(dev, "Attempting to read too much data (%d)\n", msg->len);
1655 return;
1656 }
1657
Hans Verkuil15b45112017-07-16 12:48:04 +02001658 for (i = 0; i < msg->len; i += 4) {
Dom Cobley4a59ed52021-01-11 15:22:57 +01001659 u32 val = HDMI_READ(HDMI_CEC_RX_DATA_1 + (i >> 2));
Hans Verkuil15b45112017-07-16 12:48:04 +02001660
1661 msg->msg[i] = val & 0xff;
1662 msg->msg[i + 1] = (val >> 8) & 0xff;
1663 msg->msg[i + 2] = (val >> 16) & 0xff;
1664 msg->msg[i + 3] = (val >> 24) & 0xff;
1665 }
1666}
1667
Maxime Riparded4a6bb2021-01-11 15:23:03 +01001668static irqreturn_t vc4_cec_irq_handler_tx_bare(int irq, void *priv)
1669{
1670 struct vc4_hdmi *vc4_hdmi = priv;
1671 u32 cntrl1;
1672
1673 cntrl1 = HDMI_READ(HDMI_CEC_CNTRL_1);
1674 vc4_hdmi->cec_tx_ok = cntrl1 & VC4_HDMI_CEC_TX_STATUS_GOOD;
1675 cntrl1 &= ~VC4_HDMI_CEC_START_XMIT_BEGIN;
1676 HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1);
1677
1678 return IRQ_WAKE_THREAD;
1679}
1680
1681static irqreturn_t vc4_cec_irq_handler_rx_bare(int irq, void *priv)
1682{
1683 struct vc4_hdmi *vc4_hdmi = priv;
1684 u32 cntrl1;
1685
1686 vc4_hdmi->cec_rx_msg.len = 0;
1687 cntrl1 = HDMI_READ(HDMI_CEC_CNTRL_1);
1688 vc4_cec_read_msg(vc4_hdmi, cntrl1);
1689 cntrl1 |= VC4_HDMI_CEC_CLEAR_RECEIVE_OFF;
1690 HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1);
1691 cntrl1 &= ~VC4_HDMI_CEC_CLEAR_RECEIVE_OFF;
1692
1693 HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1);
1694
1695 return IRQ_WAKE_THREAD;
1696}
1697
Hans Verkuil15b45112017-07-16 12:48:04 +02001698static irqreturn_t vc4_cec_irq_handler(int irq, void *priv)
1699{
Maxime Ripard66bf1c32020-09-03 10:01:18 +02001700 struct vc4_hdmi *vc4_hdmi = priv;
Maxime Ripard311e3052020-09-03 10:01:23 +02001701 u32 stat = HDMI_READ(HDMI_CEC_CPU_STATUS);
Maxime Riparded4a6bb2021-01-11 15:23:03 +01001702 irqreturn_t ret;
1703 u32 cntrl5;
Hans Verkuil15b45112017-07-16 12:48:04 +02001704
1705 if (!(stat & VC4_HDMI_CPU_CEC))
1706 return IRQ_NONE;
Maxime Riparded4a6bb2021-01-11 15:23:03 +01001707
Maxime Ripard311e3052020-09-03 10:01:23 +02001708 cntrl5 = HDMI_READ(HDMI_CEC_CNTRL_5);
Maxime Ripard3408cc22020-09-03 10:01:14 +02001709 vc4_hdmi->cec_irq_was_rx = cntrl5 & VC4_HDMI_CEC_RX_CEC_INT;
Maxime Riparded4a6bb2021-01-11 15:23:03 +01001710 if (vc4_hdmi->cec_irq_was_rx)
1711 ret = vc4_cec_irq_handler_rx_bare(irq, priv);
1712 else
1713 ret = vc4_cec_irq_handler_tx_bare(irq, priv);
Hans Verkuil15b45112017-07-16 12:48:04 +02001714
Maxime Riparded4a6bb2021-01-11 15:23:03 +01001715 HDMI_WRITE(HDMI_CEC_CPU_CLEAR, VC4_HDMI_CPU_CEC);
1716 return ret;
Hans Verkuil15b45112017-07-16 12:48:04 +02001717}
1718
1719static int vc4_hdmi_cec_adap_enable(struct cec_adapter *adap, bool enable)
1720{
Maxime Ripard66bf1c32020-09-03 10:01:18 +02001721 struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
Hans Verkuil15b45112017-07-16 12:48:04 +02001722 /* clock period in microseconds */
1723 const u32 usecs = 1000000 / CEC_CLOCK_FREQ;
Maxime Ripard311e3052020-09-03 10:01:23 +02001724 u32 val = HDMI_READ(HDMI_CEC_CNTRL_5);
Hans Verkuil15b45112017-07-16 12:48:04 +02001725
1726 val &= ~(VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET |
1727 VC4_HDMI_CEC_CNT_TO_4700_US_MASK |
1728 VC4_HDMI_CEC_CNT_TO_4500_US_MASK);
1729 val |= ((4700 / usecs) << VC4_HDMI_CEC_CNT_TO_4700_US_SHIFT) |
1730 ((4500 / usecs) << VC4_HDMI_CEC_CNT_TO_4500_US_SHIFT);
1731
1732 if (enable) {
Maxime Ripard311e3052020-09-03 10:01:23 +02001733 HDMI_WRITE(HDMI_CEC_CNTRL_5, val |
Hans Verkuil15b45112017-07-16 12:48:04 +02001734 VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET);
Maxime Ripard311e3052020-09-03 10:01:23 +02001735 HDMI_WRITE(HDMI_CEC_CNTRL_5, val);
1736 HDMI_WRITE(HDMI_CEC_CNTRL_2,
1737 ((1500 / usecs) << VC4_HDMI_CEC_CNT_TO_1500_US_SHIFT) |
1738 ((1300 / usecs) << VC4_HDMI_CEC_CNT_TO_1300_US_SHIFT) |
1739 ((800 / usecs) << VC4_HDMI_CEC_CNT_TO_800_US_SHIFT) |
1740 ((600 / usecs) << VC4_HDMI_CEC_CNT_TO_600_US_SHIFT) |
1741 ((400 / usecs) << VC4_HDMI_CEC_CNT_TO_400_US_SHIFT));
1742 HDMI_WRITE(HDMI_CEC_CNTRL_3,
1743 ((2750 / usecs) << VC4_HDMI_CEC_CNT_TO_2750_US_SHIFT) |
1744 ((2400 / usecs) << VC4_HDMI_CEC_CNT_TO_2400_US_SHIFT) |
1745 ((2050 / usecs) << VC4_HDMI_CEC_CNT_TO_2050_US_SHIFT) |
1746 ((1700 / usecs) << VC4_HDMI_CEC_CNT_TO_1700_US_SHIFT));
1747 HDMI_WRITE(HDMI_CEC_CNTRL_4,
1748 ((4300 / usecs) << VC4_HDMI_CEC_CNT_TO_4300_US_SHIFT) |
1749 ((3900 / usecs) << VC4_HDMI_CEC_CNT_TO_3900_US_SHIFT) |
1750 ((3600 / usecs) << VC4_HDMI_CEC_CNT_TO_3600_US_SHIFT) |
1751 ((3500 / usecs) << VC4_HDMI_CEC_CNT_TO_3500_US_SHIFT));
Hans Verkuil15b45112017-07-16 12:48:04 +02001752
Maxime Ripard185e98b2021-01-11 15:23:04 +01001753 if (!vc4_hdmi->variant->external_irq_controller)
1754 HDMI_WRITE(HDMI_CEC_CPU_MASK_CLEAR, VC4_HDMI_CPU_CEC);
Hans Verkuil15b45112017-07-16 12:48:04 +02001755 } else {
Maxime Ripard185e98b2021-01-11 15:23:04 +01001756 if (!vc4_hdmi->variant->external_irq_controller)
1757 HDMI_WRITE(HDMI_CEC_CPU_MASK_SET, VC4_HDMI_CPU_CEC);
Maxime Ripard311e3052020-09-03 10:01:23 +02001758 HDMI_WRITE(HDMI_CEC_CNTRL_5, val |
Hans Verkuil15b45112017-07-16 12:48:04 +02001759 VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET);
1760 }
1761 return 0;
1762}
1763
1764static int vc4_hdmi_cec_adap_log_addr(struct cec_adapter *adap, u8 log_addr)
1765{
Maxime Ripard66bf1c32020-09-03 10:01:18 +02001766 struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
Hans Verkuil15b45112017-07-16 12:48:04 +02001767
Maxime Ripard311e3052020-09-03 10:01:23 +02001768 HDMI_WRITE(HDMI_CEC_CNTRL_1,
1769 (HDMI_READ(HDMI_CEC_CNTRL_1) & ~VC4_HDMI_CEC_ADDR_MASK) |
Hans Verkuil15b45112017-07-16 12:48:04 +02001770 (log_addr & 0xf) << VC4_HDMI_CEC_ADDR_SHIFT);
1771 return 0;
1772}
1773
1774static int vc4_hdmi_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
1775 u32 signal_free_time, struct cec_msg *msg)
1776{
Maxime Ripard66bf1c32020-09-03 10:01:18 +02001777 struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
Dom Cobley4a59ed52021-01-11 15:22:57 +01001778 struct drm_device *dev = vc4_hdmi->connector.dev;
Hans Verkuil15b45112017-07-16 12:48:04 +02001779 u32 val;
1780 unsigned int i;
1781
Dom Cobley4a59ed52021-01-11 15:22:57 +01001782 if (msg->len > 16) {
1783 drm_err(dev, "Attempting to transmit too much data (%d)\n", msg->len);
1784 return -ENOMEM;
1785 }
1786
Hans Verkuil15b45112017-07-16 12:48:04 +02001787 for (i = 0; i < msg->len; i += 4)
Dom Cobley4a59ed52021-01-11 15:22:57 +01001788 HDMI_WRITE(HDMI_CEC_TX_DATA_1 + (i >> 2),
Hans Verkuil15b45112017-07-16 12:48:04 +02001789 (msg->msg[i]) |
1790 (msg->msg[i + 1] << 8) |
1791 (msg->msg[i + 2] << 16) |
1792 (msg->msg[i + 3] << 24));
1793
Maxime Ripard311e3052020-09-03 10:01:23 +02001794 val = HDMI_READ(HDMI_CEC_CNTRL_1);
Hans Verkuil15b45112017-07-16 12:48:04 +02001795 val &= ~VC4_HDMI_CEC_START_XMIT_BEGIN;
Maxime Ripard311e3052020-09-03 10:01:23 +02001796 HDMI_WRITE(HDMI_CEC_CNTRL_1, val);
Hans Verkuil15b45112017-07-16 12:48:04 +02001797 val &= ~VC4_HDMI_CEC_MESSAGE_LENGTH_MASK;
1798 val |= (msg->len - 1) << VC4_HDMI_CEC_MESSAGE_LENGTH_SHIFT;
1799 val |= VC4_HDMI_CEC_START_XMIT_BEGIN;
1800
Maxime Ripard311e3052020-09-03 10:01:23 +02001801 HDMI_WRITE(HDMI_CEC_CNTRL_1, val);
Hans Verkuil15b45112017-07-16 12:48:04 +02001802 return 0;
1803}
1804
1805static const struct cec_adap_ops vc4_hdmi_cec_adap_ops = {
1806 .adap_enable = vc4_hdmi_cec_adap_enable,
1807 .adap_log_addr = vc4_hdmi_cec_adap_log_addr,
1808 .adap_transmit = vc4_hdmi_cec_adap_transmit,
1809};
Hans Verkuil15b45112017-07-16 12:48:04 +02001810
Maxime Ripardc0791e02020-09-03 10:01:31 +02001811static int vc4_hdmi_cec_init(struct vc4_hdmi *vc4_hdmi)
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001812{
Dariusz Marcinkiewicz66c2dee2019-08-23 13:24:25 +02001813 struct cec_connector_info conn_info;
Maxime Ripardc0791e02020-09-03 10:01:31 +02001814 struct platform_device *pdev = vc4_hdmi->pdev;
Maxime Ripardae442bf2021-01-11 15:23:06 +01001815 struct device *dev = &pdev->dev;
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001816 u32 value;
1817 int ret;
1818
Maxime Ripardae442bf2021-01-11 15:23:06 +01001819 if (!of_find_property(dev->of_node, "interrupts", NULL)) {
1820 dev_warn(dev, "'interrupts' DT property is missing, no CEC\n");
1821 return 0;
1822 }
1823
Maxime Ripardc0791e02020-09-03 10:01:31 +02001824 vc4_hdmi->cec_adap = cec_allocate_adapter(&vc4_hdmi_cec_adap_ops,
1825 vc4_hdmi, "vc4",
1826 CEC_CAP_DEFAULTS |
1827 CEC_CAP_CONNECTOR_INFO, 1);
1828 ret = PTR_ERR_OR_ZERO(vc4_hdmi->cec_adap);
Hans Verkuil15b45112017-07-16 12:48:04 +02001829 if (ret < 0)
Maxime Ripardc0791e02020-09-03 10:01:31 +02001830 return ret;
Dariusz Marcinkiewicz66c2dee2019-08-23 13:24:25 +02001831
Maxime Ripardc0791e02020-09-03 10:01:31 +02001832 cec_fill_conn_info_from_drm(&conn_info, &vc4_hdmi->connector);
1833 cec_s_conn_info(vc4_hdmi->cec_adap, &conn_info);
Dariusz Marcinkiewicz66c2dee2019-08-23 13:24:25 +02001834
Maxime Ripardc0791e02020-09-03 10:01:31 +02001835 value = HDMI_READ(HDMI_CEC_CNTRL_1);
Maxime Ripard47fa9a82021-01-11 15:23:01 +01001836 /* Set the logical address to Unregistered */
1837 value |= VC4_HDMI_CEC_ADDR_MASK;
Maxime Ripardc0791e02020-09-03 10:01:31 +02001838 HDMI_WRITE(HDMI_CEC_CNTRL_1, value);
Maxime Ripard47fa9a82021-01-11 15:23:01 +01001839
1840 vc4_hdmi_cec_update_clk_div(vc4_hdmi);
1841
Maxime Ripard185e98b2021-01-11 15:23:04 +01001842 if (vc4_hdmi->variant->external_irq_controller) {
1843 ret = devm_request_threaded_irq(&pdev->dev,
1844 platform_get_irq_byname(pdev, "cec-rx"),
1845 vc4_cec_irq_handler_rx_bare,
1846 vc4_cec_irq_handler_rx_thread, 0,
1847 "vc4 hdmi cec rx", vc4_hdmi);
1848 if (ret)
1849 goto err_delete_cec_adap;
1850
1851 ret = devm_request_threaded_irq(&pdev->dev,
1852 platform_get_irq_byname(pdev, "cec-tx"),
1853 vc4_cec_irq_handler_tx_bare,
1854 vc4_cec_irq_handler_tx_thread, 0,
1855 "vc4 hdmi cec tx", vc4_hdmi);
1856 if (ret)
1857 goto err_delete_cec_adap;
1858 } else {
1859 HDMI_WRITE(HDMI_CEC_CPU_MASK_SET, 0xffffffff);
1860
1861 ret = devm_request_threaded_irq(&pdev->dev, platform_get_irq(pdev, 0),
1862 vc4_cec_irq_handler,
1863 vc4_cec_irq_handler_thread, 0,
1864 "vc4 hdmi cec", vc4_hdmi);
1865 if (ret)
1866 goto err_delete_cec_adap;
1867 }
Maxime Ripardc0791e02020-09-03 10:01:31 +02001868
1869 ret = cec_register_adapter(vc4_hdmi->cec_adap, &pdev->dev);
Hans Verkuil15b45112017-07-16 12:48:04 +02001870 if (ret < 0)
1871 goto err_delete_cec_adap;
Eric Anholtc9be8042019-04-01 11:35:58 -07001872
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001873 return 0;
1874
Hans Verkuil15b45112017-07-16 12:48:04 +02001875err_delete_cec_adap:
Maxime Ripardc0791e02020-09-03 10:01:31 +02001876 cec_delete_adapter(vc4_hdmi->cec_adap);
1877
1878 return ret;
1879}
1880
1881static void vc4_hdmi_cec_exit(struct vc4_hdmi *vc4_hdmi)
1882{
1883 cec_unregister_adapter(vc4_hdmi->cec_adap);
1884}
1885#else
1886static int vc4_hdmi_cec_init(struct vc4_hdmi *vc4_hdmi)
1887{
1888 return 0;
1889}
1890
1891static void vc4_hdmi_cec_exit(struct vc4_hdmi *vc4_hdmi) {};
1892
Hans Verkuil15b45112017-07-16 12:48:04 +02001893#endif
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001894
Maxime Ripard311e3052020-09-03 10:01:23 +02001895static int vc4_hdmi_build_regset(struct vc4_hdmi *vc4_hdmi,
1896 struct debugfs_regset32 *regset,
1897 enum vc4_hdmi_regs reg)
1898{
1899 const struct vc4_hdmi_variant *variant = vc4_hdmi->variant;
1900 struct debugfs_reg32 *regs, *new_regs;
1901 unsigned int count = 0;
1902 unsigned int i;
1903
1904 regs = kcalloc(variant->num_registers, sizeof(*regs),
1905 GFP_KERNEL);
1906 if (!regs)
1907 return -ENOMEM;
1908
1909 for (i = 0; i < variant->num_registers; i++) {
1910 const struct vc4_hdmi_register *field = &variant->registers[i];
1911
1912 if (field->reg != reg)
1913 continue;
1914
1915 regs[count].name = field->name;
1916 regs[count].offset = field->offset;
1917 count++;
1918 }
1919
1920 new_regs = krealloc(regs, count * sizeof(*regs), GFP_KERNEL);
1921 if (!new_regs)
1922 return -ENOMEM;
1923
1924 regset->base = __vc4_hdmi_get_field_base(vc4_hdmi, reg);
1925 regset->regs = new_regs;
1926 regset->nregs = count;
1927
1928 return 0;
1929}
1930
Maxime Ripard33c773e2020-09-03 10:01:22 +02001931static int vc4_hdmi_init_resources(struct vc4_hdmi *vc4_hdmi)
1932{
1933 struct platform_device *pdev = vc4_hdmi->pdev;
1934 struct device *dev = &pdev->dev;
1935 int ret;
1936
1937 vc4_hdmi->hdmicore_regs = vc4_ioremap_regs(pdev, 0);
1938 if (IS_ERR(vc4_hdmi->hdmicore_regs))
1939 return PTR_ERR(vc4_hdmi->hdmicore_regs);
1940
1941 vc4_hdmi->hd_regs = vc4_ioremap_regs(pdev, 1);
1942 if (IS_ERR(vc4_hdmi->hd_regs))
1943 return PTR_ERR(vc4_hdmi->hd_regs);
1944
Maxime Ripard311e3052020-09-03 10:01:23 +02001945 ret = vc4_hdmi_build_regset(vc4_hdmi, &vc4_hdmi->hd_regset, VC4_HD);
1946 if (ret)
1947 return ret;
Maxime Ripard33c773e2020-09-03 10:01:22 +02001948
Maxime Ripard311e3052020-09-03 10:01:23 +02001949 ret = vc4_hdmi_build_regset(vc4_hdmi, &vc4_hdmi->hdmi_regset, VC4_HDMI);
1950 if (ret)
1951 return ret;
Maxime Ripard33c773e2020-09-03 10:01:22 +02001952
1953 vc4_hdmi->pixel_clock = devm_clk_get(dev, "pixel");
1954 if (IS_ERR(vc4_hdmi->pixel_clock)) {
1955 ret = PTR_ERR(vc4_hdmi->pixel_clock);
1956 if (ret != -EPROBE_DEFER)
1957 DRM_ERROR("Failed to get pixel clock\n");
1958 return ret;
1959 }
1960
1961 vc4_hdmi->hsm_clock = devm_clk_get(dev, "hdmi");
1962 if (IS_ERR(vc4_hdmi->hsm_clock)) {
1963 DRM_ERROR("Failed to get HDMI state machine clock\n");
1964 return PTR_ERR(vc4_hdmi->hsm_clock);
1965 }
Dave Stevenson632ee3a2020-09-03 10:01:40 +02001966 vc4_hdmi->audio_clock = vc4_hdmi->hsm_clock;
Maxime Ripard23b7eb52021-01-11 15:23:02 +01001967 vc4_hdmi->cec_clock = vc4_hdmi->hsm_clock;
Maxime Ripard33c773e2020-09-03 10:01:22 +02001968
1969 return 0;
1970}
1971
Maxime Ripard83239892020-09-03 10:01:48 +02001972static int vc5_hdmi_init_resources(struct vc4_hdmi *vc4_hdmi)
1973{
1974 struct platform_device *pdev = vc4_hdmi->pdev;
1975 struct device *dev = &pdev->dev;
1976 struct resource *res;
1977
1978 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hdmi");
1979 if (!res)
1980 return -ENODEV;
1981
1982 vc4_hdmi->hdmicore_regs = devm_ioremap(dev, res->start,
1983 resource_size(res));
Dan Carpenter14929c52020-09-10 13:08:25 +03001984 if (!vc4_hdmi->hdmicore_regs)
1985 return -ENOMEM;
Maxime Ripard83239892020-09-03 10:01:48 +02001986
1987 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hd");
1988 if (!res)
1989 return -ENODEV;
1990
1991 vc4_hdmi->hd_regs = devm_ioremap(dev, res->start, resource_size(res));
Dan Carpenter14929c52020-09-10 13:08:25 +03001992 if (!vc4_hdmi->hd_regs)
1993 return -ENOMEM;
Maxime Ripard83239892020-09-03 10:01:48 +02001994
1995 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cec");
1996 if (!res)
1997 return -ENODEV;
1998
1999 vc4_hdmi->cec_regs = devm_ioremap(dev, res->start, resource_size(res));
Dan Carpenter14929c52020-09-10 13:08:25 +03002000 if (!vc4_hdmi->cec_regs)
2001 return -ENOMEM;
Maxime Ripard83239892020-09-03 10:01:48 +02002002
2003 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "csc");
2004 if (!res)
2005 return -ENODEV;
2006
2007 vc4_hdmi->csc_regs = devm_ioremap(dev, res->start, resource_size(res));
Dan Carpenter14929c52020-09-10 13:08:25 +03002008 if (!vc4_hdmi->csc_regs)
2009 return -ENOMEM;
Maxime Ripard83239892020-09-03 10:01:48 +02002010
2011 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dvp");
2012 if (!res)
2013 return -ENODEV;
2014
2015 vc4_hdmi->dvp_regs = devm_ioremap(dev, res->start, resource_size(res));
Dan Carpenter14929c52020-09-10 13:08:25 +03002016 if (!vc4_hdmi->dvp_regs)
2017 return -ENOMEM;
Maxime Ripard83239892020-09-03 10:01:48 +02002018
2019 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy");
2020 if (!res)
2021 return -ENODEV;
2022
2023 vc4_hdmi->phy_regs = devm_ioremap(dev, res->start, resource_size(res));
Dan Carpenter14929c52020-09-10 13:08:25 +03002024 if (!vc4_hdmi->phy_regs)
2025 return -ENOMEM;
Maxime Ripard83239892020-09-03 10:01:48 +02002026
2027 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "packet");
2028 if (!res)
2029 return -ENODEV;
2030
2031 vc4_hdmi->ram_regs = devm_ioremap(dev, res->start, resource_size(res));
Dan Carpenter14929c52020-09-10 13:08:25 +03002032 if (!vc4_hdmi->ram_regs)
2033 return -ENOMEM;
Maxime Ripard83239892020-09-03 10:01:48 +02002034
2035 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rm");
2036 if (!res)
2037 return -ENODEV;
2038
2039 vc4_hdmi->rm_regs = devm_ioremap(dev, res->start, resource_size(res));
Dan Carpenter14929c52020-09-10 13:08:25 +03002040 if (!vc4_hdmi->rm_regs)
2041 return -ENOMEM;
Maxime Ripard83239892020-09-03 10:01:48 +02002042
2043 vc4_hdmi->hsm_clock = devm_clk_get(dev, "hdmi");
2044 if (IS_ERR(vc4_hdmi->hsm_clock)) {
2045 DRM_ERROR("Failed to get HDMI state machine clock\n");
2046 return PTR_ERR(vc4_hdmi->hsm_clock);
2047 }
2048
2049 vc4_hdmi->pixel_bvb_clock = devm_clk_get(dev, "bvb");
2050 if (IS_ERR(vc4_hdmi->pixel_bvb_clock)) {
2051 DRM_ERROR("Failed to get pixel bvb clock\n");
2052 return PTR_ERR(vc4_hdmi->pixel_bvb_clock);
2053 }
2054
2055 vc4_hdmi->audio_clock = devm_clk_get(dev, "audio");
2056 if (IS_ERR(vc4_hdmi->audio_clock)) {
2057 DRM_ERROR("Failed to get audio clock\n");
2058 return PTR_ERR(vc4_hdmi->audio_clock);
2059 }
2060
Maxime Ripard23b7eb52021-01-11 15:23:02 +01002061 vc4_hdmi->cec_clock = devm_clk_get(dev, "cec");
2062 if (IS_ERR(vc4_hdmi->cec_clock)) {
2063 DRM_ERROR("Failed to get CEC clock\n");
2064 return PTR_ERR(vc4_hdmi->cec_clock);
2065 }
2066
Maxime Ripard83239892020-09-03 10:01:48 +02002067 vc4_hdmi->reset = devm_reset_control_get(dev, NULL);
2068 if (IS_ERR(vc4_hdmi->reset)) {
2069 DRM_ERROR("Failed to get HDMI reset line\n");
2070 return PTR_ERR(vc4_hdmi->reset);
2071 }
2072
2073 return 0;
2074}
2075
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002076static int vc4_hdmi_bind(struct device *dev, struct device *master, void *data)
2077{
Maxime Ripard33c773e2020-09-03 10:01:22 +02002078 const struct vc4_hdmi_variant *variant = of_device_get_match_data(dev);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002079 struct platform_device *pdev = to_platform_device(dev);
2080 struct drm_device *drm = dev_get_drvdata(master);
Maxime Ripard3408cc22020-09-03 10:01:14 +02002081 struct vc4_hdmi *vc4_hdmi;
Maxime Ripardc98c85b2020-09-03 10:01:12 +02002082 struct drm_encoder *encoder;
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002083 struct device_node *ddc_node;
2084 u32 value;
2085 int ret;
2086
Maxime Ripard3408cc22020-09-03 10:01:14 +02002087 vc4_hdmi = devm_kzalloc(dev, sizeof(*vc4_hdmi), GFP_KERNEL);
2088 if (!vc4_hdmi)
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002089 return -ENOMEM;
2090
Maxime Ripard47c167b2020-09-03 10:01:19 +02002091 dev_set_drvdata(dev, vc4_hdmi);
Maxime Ripard3408cc22020-09-03 10:01:14 +02002092 encoder = &vc4_hdmi->encoder.base.base;
Maxime Ripard7d732992020-09-03 10:01:29 +02002093 vc4_hdmi->encoder.base.type = variant->encoder_type;
Maxime Ripard09c43812020-09-03 10:01:44 +02002094 vc4_hdmi->encoder.base.pre_crtc_configure = vc4_hdmi_encoder_pre_crtc_configure;
2095 vc4_hdmi->encoder.base.pre_crtc_enable = vc4_hdmi_encoder_pre_crtc_enable;
2096 vc4_hdmi->encoder.base.post_crtc_enable = vc4_hdmi_encoder_post_crtc_enable;
2097 vc4_hdmi->encoder.base.post_crtc_disable = vc4_hdmi_encoder_post_crtc_disable;
2098 vc4_hdmi->encoder.base.post_crtc_powerdown = vc4_hdmi_encoder_post_crtc_powerdown;
Maxime Ripard3408cc22020-09-03 10:01:14 +02002099 vc4_hdmi->pdev = pdev;
Maxime Ripard33c773e2020-09-03 10:01:22 +02002100 vc4_hdmi->variant = variant;
Maxime Ripardc98c85b2020-09-03 10:01:12 +02002101
Maxime Ripard33c773e2020-09-03 10:01:22 +02002102 ret = variant->init_resources(vc4_hdmi);
2103 if (ret)
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002104 return ret;
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002105
2106 ddc_node = of_parse_phandle(dev->of_node, "ddc", 0);
2107 if (!ddc_node) {
2108 DRM_ERROR("Failed to find ddc node in device tree\n");
2109 return -ENODEV;
2110 }
2111
Maxime Ripard3408cc22020-09-03 10:01:14 +02002112 vc4_hdmi->ddc = of_find_i2c_adapter_by_node(ddc_node);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002113 of_node_put(ddc_node);
Maxime Ripard3408cc22020-09-03 10:01:14 +02002114 if (!vc4_hdmi->ddc) {
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002115 DRM_DEBUG("Failed to get ddc i2c adapter by node\n");
2116 return -EPROBE_DEFER;
2117 }
2118
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002119 /* Only use the GPIO HPD pin if present in the DT, otherwise
2120 * we'll use the HDMI core's register.
2121 */
2122 if (of_find_property(dev->of_node, "hpd-gpios", &value)) {
2123 enum of_gpio_flags hpd_gpio_flags;
2124
Maxime Ripard3408cc22020-09-03 10:01:14 +02002125 vc4_hdmi->hpd_gpio = of_get_named_gpio_flags(dev->of_node,
2126 "hpd-gpios", 0,
2127 &hpd_gpio_flags);
2128 if (vc4_hdmi->hpd_gpio < 0) {
2129 ret = vc4_hdmi->hpd_gpio;
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002130 goto err_unprepare_hsm;
2131 }
2132
Maxime Ripard3408cc22020-09-03 10:01:14 +02002133 vc4_hdmi->hpd_active_low = hpd_gpio_flags & OF_GPIO_ACTIVE_LOW;
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002134 }
2135
Maxime Ripard9fa1d7e2020-10-29 14:40:17 +01002136 vc4_hdmi->disable_wifi_frequencies =
2137 of_property_read_bool(dev->of_node, "wifi-2.4ghz-coexistence");
2138
Maxime Ripard86e3a652021-05-07 17:05:12 +02002139 if (variant->max_pixel_clock == 600000000) {
2140 struct vc4_dev *vc4 = to_vc4_dev(drm);
2141 long max_rate = clk_round_rate(vc4->hvs->core_clk, 550000000);
2142
2143 if (max_rate < 550000000)
2144 vc4_hdmi->disable_4kp60 = true;
2145 }
2146
Dom Cobley902dc5c12021-01-11 15:22:56 +01002147 if (vc4_hdmi->variant->reset)
2148 vc4_hdmi->variant->reset(vc4_hdmi);
2149
Maxime Ripard5b006002021-05-07 17:05:09 +02002150 if ((of_device_is_compatible(dev->of_node, "brcm,bcm2711-hdmi0") ||
2151 of_device_is_compatible(dev->of_node, "brcm,bcm2711-hdmi1")) &&
2152 HDMI_READ(HDMI_VID_CTL) & VC4_HD_VID_CTL_ENABLE) {
2153 clk_prepare_enable(vc4_hdmi->pixel_clock);
2154 clk_prepare_enable(vc4_hdmi->hsm_clock);
2155 clk_prepare_enable(vc4_hdmi->pixel_bvb_clock);
2156 }
2157
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002158 pm_runtime_enable(dev);
2159
Maxime Ripardc98c85b2020-09-03 10:01:12 +02002160 drm_simple_encoder_init(drm, encoder, DRM_MODE_ENCODER_TMDS);
2161 drm_encoder_helper_add(encoder, &vc4_hdmi_encoder_helper_funcs);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002162
Maxime Ripard3408cc22020-09-03 10:01:14 +02002163 ret = vc4_hdmi_connector_init(drm, vc4_hdmi);
Maxime Ripardc98c85b2020-09-03 10:01:12 +02002164 if (ret)
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002165 goto err_destroy_encoder;
Maxime Ripardc98c85b2020-09-03 10:01:12 +02002166
Maxime Ripardc0791e02020-09-03 10:01:31 +02002167 ret = vc4_hdmi_cec_init(vc4_hdmi);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002168 if (ret)
Maxime Ripardc0791e02020-09-03 10:01:31 +02002169 goto err_destroy_conn;
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002170
Maxime Ripard3408cc22020-09-03 10:01:14 +02002171 ret = vc4_hdmi_audio_init(vc4_hdmi);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002172 if (ret)
Maxime Ripardc0791e02020-09-03 10:01:31 +02002173 goto err_free_cec;
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002174
Maxime Ripardb2405c92020-09-03 10:01:30 +02002175 vc4_debugfs_add_file(drm, variant->debugfs_name,
2176 vc4_hdmi_debugfs_regs,
2177 vc4_hdmi);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002178
2179 return 0;
2180
Maxime Ripardc0791e02020-09-03 10:01:31 +02002181err_free_cec:
2182 vc4_hdmi_cec_exit(vc4_hdmi);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002183err_destroy_conn:
Maxime Ripard0532e5e2020-09-03 10:01:21 +02002184 vc4_hdmi_connector_destroy(&vc4_hdmi->connector);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002185err_destroy_encoder:
Maxime Ripardc98c85b2020-09-03 10:01:12 +02002186 drm_encoder_cleanup(encoder);
Hans Verkuil10ee2752017-07-16 12:48:03 +02002187err_unprepare_hsm:
Boris Brezillon4f6e3d62017-04-11 18:39:25 +02002188 pm_runtime_disable(dev);
Maxime Ripard3408cc22020-09-03 10:01:14 +02002189 put_device(&vc4_hdmi->ddc->dev);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002190
2191 return ret;
2192}
2193
2194static void vc4_hdmi_unbind(struct device *dev, struct device *master,
2195 void *data)
2196{
Maxime Ripard47c167b2020-09-03 10:01:19 +02002197 struct vc4_hdmi *vc4_hdmi;
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002198
Maxime Ripard47c167b2020-09-03 10:01:19 +02002199 /*
2200 * ASoC makes it a bit hard to retrieve a pointer to the
2201 * vc4_hdmi structure. Registering the card will overwrite our
2202 * device drvdata with a pointer to the snd_soc_card structure,
2203 * which can then be used to retrieve whatever drvdata we want
2204 * to associate.
2205 *
2206 * However, that doesn't fly in the case where we wouldn't
2207 * register an ASoC card (because of an old DT that is missing
2208 * the dmas properties for example), then the card isn't
2209 * registered and the device drvdata wouldn't be set.
2210 *
2211 * We can deal with both cases by making sure a snd_soc_card
2212 * pointer and a vc4_hdmi structure are pointing to the same
2213 * memory address, so we can treat them indistinctly without any
2214 * issue.
2215 */
2216 BUILD_BUG_ON(offsetof(struct vc4_hdmi_audio, card) != 0);
2217 BUILD_BUG_ON(offsetof(struct vc4_hdmi, audio) != 0);
2218 vc4_hdmi = dev_get_drvdata(dev);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002219
Maxime Ripard311e3052020-09-03 10:01:23 +02002220 kfree(vc4_hdmi->hdmi_regset.regs);
2221 kfree(vc4_hdmi->hd_regset.regs);
2222
Maxime Ripardc0791e02020-09-03 10:01:31 +02002223 vc4_hdmi_cec_exit(vc4_hdmi);
Maxime Ripard0532e5e2020-09-03 10:01:21 +02002224 vc4_hdmi_connector_destroy(&vc4_hdmi->connector);
Maxime Ripard3408cc22020-09-03 10:01:14 +02002225 drm_encoder_cleanup(&vc4_hdmi->encoder.base.base);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002226
Boris Brezillon4f6e3d62017-04-11 18:39:25 +02002227 pm_runtime_disable(dev);
2228
Maxime Ripard3408cc22020-09-03 10:01:14 +02002229 put_device(&vc4_hdmi->ddc->dev);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002230}
2231
2232static const struct component_ops vc4_hdmi_ops = {
2233 .bind = vc4_hdmi_bind,
2234 .unbind = vc4_hdmi_unbind,
2235};
2236
2237static int vc4_hdmi_dev_probe(struct platform_device *pdev)
2238{
2239 return component_add(&pdev->dev, &vc4_hdmi_ops);
2240}
2241
2242static int vc4_hdmi_dev_remove(struct platform_device *pdev)
2243{
2244 component_del(&pdev->dev, &vc4_hdmi_ops);
2245 return 0;
2246}
2247
Maxime Ripard33c773e2020-09-03 10:01:22 +02002248static const struct vc4_hdmi_variant bcm2835_variant = {
Maxime Ripard7d732992020-09-03 10:01:29 +02002249 .encoder_type = VC4_ENCODER_TYPE_HDMI0,
Maxime Ripardb2405c92020-09-03 10:01:30 +02002250 .debugfs_name = "hdmi_regs",
Maxime Ripard9be43a52020-09-03 10:01:41 +02002251 .card_name = "vc4-hdmi",
Maxime Ripardcd4cb492020-09-03 10:01:35 +02002252 .max_pixel_clock = 162000000,
Maxime Ripard311e3052020-09-03 10:01:23 +02002253 .registers = vc4_hdmi_fields,
2254 .num_registers = ARRAY_SIZE(vc4_hdmi_fields),
2255
Maxime Ripard33c773e2020-09-03 10:01:22 +02002256 .init_resources = vc4_hdmi_init_resources,
Maxime Ripard89f31a22020-09-03 10:01:27 +02002257 .csc_setup = vc4_hdmi_csc_setup,
Maxime Ripard9045e912020-09-03 10:01:24 +02002258 .reset = vc4_hdmi_reset,
Maxime Ripard904f6682020-09-03 10:01:28 +02002259 .set_timings = vc4_hdmi_set_timings,
Maxime Ripardc457b8a2020-09-03 10:01:25 +02002260 .phy_init = vc4_hdmi_phy_init,
2261 .phy_disable = vc4_hdmi_phy_disable,
Maxime Ripard647b9652020-09-03 10:01:26 +02002262 .phy_rng_enable = vc4_hdmi_phy_rng_enable,
2263 .phy_rng_disable = vc4_hdmi_phy_rng_disable,
Dave Stevenson632ee3a2020-09-03 10:01:40 +02002264 .channel_map = vc4_hdmi_channel_map,
Dave Stevensonbccd5c52021-04-30 11:44:49 +02002265 .supports_hdr = false,
Maxime Ripard33c773e2020-09-03 10:01:22 +02002266};
2267
Maxime Ripard83239892020-09-03 10:01:48 +02002268static const struct vc4_hdmi_variant bcm2711_hdmi0_variant = {
2269 .encoder_type = VC4_ENCODER_TYPE_HDMI0,
2270 .debugfs_name = "hdmi0_regs",
2271 .card_name = "vc4-hdmi-0",
Maxime Ripard24169a22020-12-15 16:42:42 +01002272 .max_pixel_clock = HDMI_14_MAX_TMDS_CLK,
Maxime Ripard83239892020-09-03 10:01:48 +02002273 .registers = vc5_hdmi_hdmi0_fields,
2274 .num_registers = ARRAY_SIZE(vc5_hdmi_hdmi0_fields),
2275 .phy_lane_mapping = {
2276 PHY_LANE_0,
2277 PHY_LANE_1,
2278 PHY_LANE_2,
2279 PHY_LANE_CK,
2280 },
Maxime Ripard57fb32e2020-10-29 13:25:22 +01002281 .unsupported_odd_h_timings = true,
Maxime Ripard185e98b2021-01-11 15:23:04 +01002282 .external_irq_controller = true,
Maxime Ripard83239892020-09-03 10:01:48 +02002283
2284 .init_resources = vc5_hdmi_init_resources,
2285 .csc_setup = vc5_hdmi_csc_setup,
2286 .reset = vc5_hdmi_reset,
2287 .set_timings = vc5_hdmi_set_timings,
2288 .phy_init = vc5_hdmi_phy_init,
2289 .phy_disable = vc5_hdmi_phy_disable,
2290 .phy_rng_enable = vc5_hdmi_phy_rng_enable,
2291 .phy_rng_disable = vc5_hdmi_phy_rng_disable,
2292 .channel_map = vc5_hdmi_channel_map,
Dave Stevensonbccd5c52021-04-30 11:44:49 +02002293 .supports_hdr = true,
Maxime Ripard83239892020-09-03 10:01:48 +02002294};
2295
2296static const struct vc4_hdmi_variant bcm2711_hdmi1_variant = {
2297 .encoder_type = VC4_ENCODER_TYPE_HDMI1,
2298 .debugfs_name = "hdmi1_regs",
2299 .card_name = "vc4-hdmi-1",
Maxime Ripard24169a22020-12-15 16:42:42 +01002300 .max_pixel_clock = HDMI_14_MAX_TMDS_CLK,
Maxime Ripard83239892020-09-03 10:01:48 +02002301 .registers = vc5_hdmi_hdmi1_fields,
2302 .num_registers = ARRAY_SIZE(vc5_hdmi_hdmi1_fields),
2303 .phy_lane_mapping = {
2304 PHY_LANE_1,
2305 PHY_LANE_0,
2306 PHY_LANE_CK,
2307 PHY_LANE_2,
2308 },
Maxime Ripard57fb32e2020-10-29 13:25:22 +01002309 .unsupported_odd_h_timings = true,
Maxime Ripard185e98b2021-01-11 15:23:04 +01002310 .external_irq_controller = true,
Maxime Ripard83239892020-09-03 10:01:48 +02002311
2312 .init_resources = vc5_hdmi_init_resources,
2313 .csc_setup = vc5_hdmi_csc_setup,
2314 .reset = vc5_hdmi_reset,
2315 .set_timings = vc5_hdmi_set_timings,
2316 .phy_init = vc5_hdmi_phy_init,
2317 .phy_disable = vc5_hdmi_phy_disable,
2318 .phy_rng_enable = vc5_hdmi_phy_rng_enable,
2319 .phy_rng_disable = vc5_hdmi_phy_rng_disable,
2320 .channel_map = vc5_hdmi_channel_map,
Dave Stevensonbccd5c52021-04-30 11:44:49 +02002321 .supports_hdr = true,
Maxime Ripard83239892020-09-03 10:01:48 +02002322};
2323
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002324static const struct of_device_id vc4_hdmi_dt_match[] = {
Maxime Ripard33c773e2020-09-03 10:01:22 +02002325 { .compatible = "brcm,bcm2835-hdmi", .data = &bcm2835_variant },
Maxime Ripard83239892020-09-03 10:01:48 +02002326 { .compatible = "brcm,bcm2711-hdmi0", .data = &bcm2711_hdmi0_variant },
2327 { .compatible = "brcm,bcm2711-hdmi1", .data = &bcm2711_hdmi1_variant },
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002328 {}
2329};
2330
2331struct platform_driver vc4_hdmi_driver = {
2332 .probe = vc4_hdmi_dev_probe,
2333 .remove = vc4_hdmi_dev_remove,
2334 .driver = {
2335 .name = "vc4_hdmi",
2336 .of_match_table = vc4_hdmi_dt_match,
2337 },
2338};