blob: 5e479647559f4116c86f22f7f9ca326ddbe01d77 [file] [log] [blame]
Thomas Gleixnercaab2772019-06-03 07:44:50 +02001// SPDX-License-Identifier: GPL-2.0-only
Eric Anholtc8b75bc2015-03-02 13:01:12 -08002/*
3 * Copyright (C) 2015 Broadcom
4 * Copyright (c) 2014 The Linux Foundation. All rights reserved.
5 * Copyright (C) 2013 Red Hat
6 * Author: Rob Clark <robdclark@gmail.com>
Eric Anholtc8b75bc2015-03-02 13:01:12 -08007 */
8
9/**
10 * DOC: VC4 Falcon HDMI module
11 *
Eric Anholtf6c01532017-02-27 12:11:43 -080012 * The HDMI core has a state machine and a PHY. On BCM2835, most of
13 * the unit operates off of the HSM clock from CPRMAN. It also
14 * internally uses the PLLH_PIX clock for the PHY.
15 *
16 * HDMI infoframes are kept within a small packet ram, where each
17 * packet can be individually enabled for including in a frame.
18 *
19 * HDMI audio is implemented entirely within the HDMI IP block. A
20 * register in the HDMI encoder takes SPDIF frames from the DMA engine
21 * and transfers them over an internal MAI (multi-channel audio
22 * interconnect) bus to the encoder side for insertion into the video
23 * blank regions.
24 *
25 * The driver's HDMI encoder does not yet support power management.
26 * The HDMI encoder's power domain and the HSM/pixel clocks are kept
27 * continuously running, and only the HDMI logic and packet ram are
28 * powered off/on at disable/enable time.
29 *
30 * The driver does not yet support CEC control, though the HDMI
31 * encoder block has CEC support.
Eric Anholtc8b75bc2015-03-02 13:01:12 -080032 */
33
Masahiro Yamadab7e8e252017-05-18 13:29:38 +090034#include <drm/drm_atomic_helper.h>
Masahiro Yamadab7e8e252017-05-18 13:29:38 +090035#include <drm/drm_edid.h>
Daniel Vetterfcd70cd2019-01-17 22:03:34 +010036#include <drm/drm_probe_helper.h>
Thomas Zimmermannf6ebc1b2020-03-05 16:59:46 +010037#include <drm/drm_simple_kms_helper.h>
Masahiro Yamadab7e8e252017-05-18 13:29:38 +090038#include <linux/clk.h>
39#include <linux/component.h>
40#include <linux/i2c.h>
41#include <linux/of_address.h>
42#include <linux/of_gpio.h>
43#include <linux/of_platform.h>
44#include <linux/pm_runtime.h>
45#include <linux/rational.h>
46#include <sound/dmaengine_pcm.h>
47#include <sound/pcm_drm_eld.h>
48#include <sound/pcm_params.h>
49#include <sound/soc.h>
Hans Verkuil15b45112017-07-16 12:48:04 +020050#include "media/cec.h"
Eric Anholtc8b75bc2015-03-02 13:01:12 -080051#include "vc4_drv.h"
Maxime Ripardf73100c2020-09-03 10:01:11 +020052#include "vc4_hdmi.h"
Maxime Ripard311e3052020-09-03 10:01:23 +020053#include "vc4_hdmi_regs.h"
Eric Anholtc8b75bc2015-03-02 13:01:12 -080054#include "vc4_regs.h"
55
Hans Verkuil15b45112017-07-16 12:48:04 +020056#define CEC_CLOCK_FREQ 40000
Hans Verkuil15b45112017-07-16 12:48:04 +020057
Eric Anholtc9be8042019-04-01 11:35:58 -070058static int vc4_hdmi_debugfs_regs(struct seq_file *m, void *unused)
Eric Anholtc8b75bc2015-03-02 13:01:12 -080059{
60 struct drm_info_node *node = (struct drm_info_node *)m->private;
Maxime Ripard3408cc22020-09-03 10:01:14 +020061 struct vc4_hdmi *vc4_hdmi = node->info_ent->data;
Eric Anholt30517192019-02-20 13:03:38 -080062 struct drm_printer p = drm_seq_file_printer(m);
Eric Anholtc8b75bc2015-03-02 13:01:12 -080063
Maxime Ripard3408cc22020-09-03 10:01:14 +020064 drm_print_regset32(&p, &vc4_hdmi->hdmi_regset);
65 drm_print_regset32(&p, &vc4_hdmi->hd_regset);
Eric Anholtc8b75bc2015-03-02 13:01:12 -080066
67 return 0;
68}
Eric Anholtc8b75bc2015-03-02 13:01:12 -080069
Maxime Ripard9045e912020-09-03 10:01:24 +020070static void vc4_hdmi_reset(struct vc4_hdmi *vc4_hdmi)
71{
72 HDMI_WRITE(HDMI_M_CTL, VC4_HD_M_SW_RST);
73 udelay(1);
74 HDMI_WRITE(HDMI_M_CTL, 0);
75
76 HDMI_WRITE(HDMI_M_CTL, VC4_HD_M_ENABLE);
77
78 HDMI_WRITE(HDMI_SW_RESET_CONTROL,
79 VC4_HDMI_SW_RESET_HDMI |
80 VC4_HDMI_SW_RESET_FORMAT_DETECT);
81
82 HDMI_WRITE(HDMI_SW_RESET_CONTROL, 0);
83}
84
Eric Anholtc8b75bc2015-03-02 13:01:12 -080085static enum drm_connector_status
86vc4_hdmi_connector_detect(struct drm_connector *connector, bool force)
87{
Maxime Ripard5dfbcae2020-09-03 10:01:17 +020088 struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector);
Eric Anholtc8b75bc2015-03-02 13:01:12 -080089
Maxime Ripardb10db9a2020-09-03 10:01:16 +020090 if (vc4_hdmi->hpd_gpio) {
91 if (gpio_get_value_cansleep(vc4_hdmi->hpd_gpio) ^
92 vc4_hdmi->hpd_active_low)
Eric Anholtc8b75bc2015-03-02 13:01:12 -080093 return connector_status_connected;
Maxime Ripardb10db9a2020-09-03 10:01:16 +020094 cec_phys_addr_invalidate(vc4_hdmi->cec_adap);
Hans Verkuil15b45112017-07-16 12:48:04 +020095 return connector_status_disconnected;
Eric Anholtc8b75bc2015-03-02 13:01:12 -080096 }
97
Maxime Ripardb10db9a2020-09-03 10:01:16 +020098 if (drm_probe_ddc(vc4_hdmi->ddc))
Eric Anholt9d44abb2016-09-14 19:21:29 +010099 return connector_status_connected;
100
Maxime Ripard311e3052020-09-03 10:01:23 +0200101 if (HDMI_READ(HDMI_HOTPLUG) & VC4_HDMI_HOTPLUG_CONNECTED)
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800102 return connector_status_connected;
Maxime Ripardb10db9a2020-09-03 10:01:16 +0200103 cec_phys_addr_invalidate(vc4_hdmi->cec_adap);
Hans Verkuil15b45112017-07-16 12:48:04 +0200104 return connector_status_disconnected;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800105}
106
107static void vc4_hdmi_connector_destroy(struct drm_connector *connector)
108{
109 drm_connector_unregister(connector);
110 drm_connector_cleanup(connector);
111}
112
113static int vc4_hdmi_connector_get_modes(struct drm_connector *connector)
114{
Maxime Ripard5dfbcae2020-09-03 10:01:17 +0200115 struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector);
116 struct vc4_hdmi_encoder *vc4_encoder = &vc4_hdmi->encoder;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800117 int ret = 0;
118 struct edid *edid;
119
Maxime Ripard5dfbcae2020-09-03 10:01:17 +0200120 edid = drm_get_edid(connector, vc4_hdmi->ddc);
121 cec_s_phys_addr_from_edid(vc4_hdmi->cec_adap, edid);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800122 if (!edid)
123 return -ENODEV;
124
125 vc4_encoder->hdmi_monitor = drm_detect_hdmi_monitor(edid);
Eric Anholt21317b32016-09-29 15:34:43 -0700126
Daniel Vetterc555f022018-07-09 10:40:06 +0200127 drm_connector_update_edid_property(connector, edid);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800128 ret = drm_add_edid_modes(connector, edid);
Eric Anholt5afe0e62017-08-08 13:56:05 -0700129 kfree(edid);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800130
131 return ret;
132}
133
Maxime Ripard90b2df52019-06-19 12:17:53 +0200134static void vc4_hdmi_connector_reset(struct drm_connector *connector)
135{
136 drm_atomic_helper_connector_reset(connector);
137 drm_atomic_helper_connector_tv_reset(connector);
138}
139
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800140static const struct drm_connector_funcs vc4_hdmi_connector_funcs = {
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800141 .detect = vc4_hdmi_connector_detect,
Eric Anholt682e62c2016-09-28 17:30:25 -0700142 .fill_modes = drm_helper_probe_single_connector_modes,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800143 .destroy = vc4_hdmi_connector_destroy,
Maxime Ripard90b2df52019-06-19 12:17:53 +0200144 .reset = vc4_hdmi_connector_reset,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800145 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
146 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
147};
148
149static const struct drm_connector_helper_funcs vc4_hdmi_connector_helper_funcs = {
150 .get_modes = vc4_hdmi_connector_get_modes,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800151};
152
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200153static int vc4_hdmi_connector_init(struct drm_device *dev,
Maxime Ripardb052e702020-09-03 10:01:13 +0200154 struct vc4_hdmi *vc4_hdmi)
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800155{
Maxime Ripard0532e5e2020-09-03 10:01:21 +0200156 struct drm_connector *connector = &vc4_hdmi->connector;
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200157 struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
Boris Brezillondb999532018-12-06 15:24:39 +0100158 int ret;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800159
Andrzej Pietrasiewicz04a880f2020-01-02 14:22:58 +0100160 drm_connector_init_with_ddc(dev, connector,
161 &vc4_hdmi_connector_funcs,
162 DRM_MODE_CONNECTOR_HDMIA,
Maxime Ripardb052e702020-09-03 10:01:13 +0200163 vc4_hdmi->ddc);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800164 drm_connector_helper_add(connector, &vc4_hdmi_connector_helper_funcs);
165
Boris Brezillondb999532018-12-06 15:24:39 +0100166 /* Create and attach TV margin props to this connector. */
167 ret = drm_mode_create_tv_margin_properties(dev);
168 if (ret)
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200169 return ret;
Boris Brezillondb999532018-12-06 15:24:39 +0100170
171 drm_connector_attach_tv_margin_properties(connector);
172
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800173 connector->polled = (DRM_CONNECTOR_POLL_CONNECT |
174 DRM_CONNECTOR_POLL_DISCONNECT);
175
Mario Kleineracc1be12016-07-19 20:58:58 +0200176 connector->interlace_allowed = 1;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800177 connector->doublescan_allowed = 0;
178
Daniel Vettercde4c442018-07-09 10:40:07 +0200179 drm_connector_attach_encoder(connector, encoder);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800180
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200181 return 0;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800182}
183
Eric Anholt21317b32016-09-29 15:34:43 -0700184static int vc4_hdmi_stop_packet(struct drm_encoder *encoder,
185 enum hdmi_infoframe_type type)
186{
Maxime Ripard5dfbcae2020-09-03 10:01:17 +0200187 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
Eric Anholt21317b32016-09-29 15:34:43 -0700188 u32 packet_id = type - 0x80;
189
Maxime Ripard311e3052020-09-03 10:01:23 +0200190 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
191 HDMI_READ(HDMI_RAM_PACKET_CONFIG) & ~BIT(packet_id));
Eric Anholt21317b32016-09-29 15:34:43 -0700192
Maxime Ripard311e3052020-09-03 10:01:23 +0200193 return wait_for(!(HDMI_READ(HDMI_RAM_PACKET_STATUS) &
Eric Anholt21317b32016-09-29 15:34:43 -0700194 BIT(packet_id)), 100);
195}
196
197static void vc4_hdmi_write_infoframe(struct drm_encoder *encoder,
198 union hdmi_infoframe *frame)
199{
Maxime Ripard5dfbcae2020-09-03 10:01:17 +0200200 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
Eric Anholt21317b32016-09-29 15:34:43 -0700201 u32 packet_id = frame->any.type - 0x80;
Maxime Ripard311e3052020-09-03 10:01:23 +0200202 const struct vc4_hdmi_register *ram_packet_start =
203 &vc4_hdmi->variant->registers[HDMI_RAM_PACKET_START];
204 u32 packet_reg = ram_packet_start->offset + VC4_HDMI_PACKET_STRIDE * packet_id;
205 void __iomem *base = __vc4_hdmi_get_field_base(vc4_hdmi,
206 ram_packet_start->reg);
Eric Anholt21317b32016-09-29 15:34:43 -0700207 uint8_t buffer[VC4_HDMI_PACKET_STRIDE];
208 ssize_t len, i;
209 int ret;
210
Maxime Ripard311e3052020-09-03 10:01:23 +0200211 WARN_ONCE(!(HDMI_READ(HDMI_RAM_PACKET_CONFIG) &
Eric Anholt21317b32016-09-29 15:34:43 -0700212 VC4_HDMI_RAM_PACKET_ENABLE),
213 "Packet RAM has to be on to store the packet.");
214
215 len = hdmi_infoframe_pack(frame, buffer, sizeof(buffer));
216 if (len < 0)
217 return;
218
219 ret = vc4_hdmi_stop_packet(encoder, frame->any.type);
220 if (ret) {
221 DRM_ERROR("Failed to wait for infoframe to go idle: %d\n", ret);
222 return;
223 }
224
225 for (i = 0; i < len; i += 7) {
Maxime Ripard311e3052020-09-03 10:01:23 +0200226 writel(buffer[i + 0] << 0 |
227 buffer[i + 1] << 8 |
228 buffer[i + 2] << 16,
229 base + packet_reg);
Eric Anholt21317b32016-09-29 15:34:43 -0700230 packet_reg += 4;
231
Maxime Ripard311e3052020-09-03 10:01:23 +0200232 writel(buffer[i + 3] << 0 |
233 buffer[i + 4] << 8 |
234 buffer[i + 5] << 16 |
235 buffer[i + 6] << 24,
236 base + packet_reg);
Eric Anholt21317b32016-09-29 15:34:43 -0700237 packet_reg += 4;
238 }
239
Maxime Ripard311e3052020-09-03 10:01:23 +0200240 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
241 HDMI_READ(HDMI_RAM_PACKET_CONFIG) | BIT(packet_id));
242 ret = wait_for((HDMI_READ(HDMI_RAM_PACKET_STATUS) &
Eric Anholt21317b32016-09-29 15:34:43 -0700243 BIT(packet_id)), 100);
244 if (ret)
245 DRM_ERROR("Failed to wait for infoframe to start: %d\n", ret);
246}
247
248static void vc4_hdmi_set_avi_infoframe(struct drm_encoder *encoder)
249{
Maxime Ripard5dfbcae2020-09-03 10:01:17 +0200250 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
Eric Anholt21317b32016-09-29 15:34:43 -0700251 struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
Maxime Ripard0532e5e2020-09-03 10:01:21 +0200252 struct drm_connector *connector = &vc4_hdmi->connector;
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200253 struct drm_connector_state *cstate = connector->state;
Eric Anholt21317b32016-09-29 15:34:43 -0700254 struct drm_crtc *crtc = encoder->crtc;
255 const struct drm_display_mode *mode = &crtc->state->adjusted_mode;
256 union hdmi_infoframe frame;
257 int ret;
258
Ville Syrjälä13d0add2019-01-08 19:28:25 +0200259 ret = drm_hdmi_avi_infoframe_from_display_mode(&frame.avi,
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200260 connector, mode);
Eric Anholt21317b32016-09-29 15:34:43 -0700261 if (ret < 0) {
262 DRM_ERROR("couldn't fill AVI infoframe\n");
263 return;
264 }
265
Ville Syrjälä13d0add2019-01-08 19:28:25 +0200266 drm_hdmi_avi_infoframe_quant_range(&frame.avi,
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200267 connector, mode,
Ville Syrjäläa2ce26f2017-01-11 14:57:23 +0200268 vc4_encoder->limited_rgb_range ?
269 HDMI_QUANTIZATION_RANGE_LIMITED :
Ville Syrjälä1581b2d2019-01-08 19:28:28 +0200270 HDMI_QUANTIZATION_RANGE_FULL);
Eric Anholt21317b32016-09-29 15:34:43 -0700271
Ville Syrjäläcb876372019-10-08 19:48:14 +0300272 drm_hdmi_avi_infoframe_bars(&frame.avi, cstate);
Boris Brezillondb999532018-12-06 15:24:39 +0100273
Eric Anholt21317b32016-09-29 15:34:43 -0700274 vc4_hdmi_write_infoframe(encoder, &frame);
275}
276
277static void vc4_hdmi_set_spd_infoframe(struct drm_encoder *encoder)
278{
279 union hdmi_infoframe frame;
280 int ret;
281
282 ret = hdmi_spd_infoframe_init(&frame.spd, "Broadcom", "Videocore");
283 if (ret < 0) {
284 DRM_ERROR("couldn't fill SPD infoframe\n");
285 return;
286 }
287
288 frame.spd.sdi = HDMI_SPD_SDI_PC;
289
290 vc4_hdmi_write_infoframe(encoder, &frame);
291}
292
Eric Anholtbb7d7852017-02-27 12:28:02 -0800293static void vc4_hdmi_set_audio_infoframe(struct drm_encoder *encoder)
294{
Maxime Ripard5dfbcae2020-09-03 10:01:17 +0200295 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800296 union hdmi_infoframe frame;
297 int ret;
298
299 ret = hdmi_audio_infoframe_init(&frame.audio);
300
301 frame.audio.coding_type = HDMI_AUDIO_CODING_TYPE_STREAM;
302 frame.audio.sample_frequency = HDMI_AUDIO_SAMPLE_FREQUENCY_STREAM;
303 frame.audio.sample_size = HDMI_AUDIO_SAMPLE_SIZE_STREAM;
Maxime Ripard3408cc22020-09-03 10:01:14 +0200304 frame.audio.channels = vc4_hdmi->audio.channels;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800305
306 vc4_hdmi_write_infoframe(encoder, &frame);
307}
308
Eric Anholt21317b32016-09-29 15:34:43 -0700309static void vc4_hdmi_set_infoframes(struct drm_encoder *encoder)
310{
Dave Stevenson6ac1c752020-09-03 10:01:38 +0200311 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
312
Eric Anholt21317b32016-09-29 15:34:43 -0700313 vc4_hdmi_set_avi_infoframe(encoder);
314 vc4_hdmi_set_spd_infoframe(encoder);
Dave Stevenson6ac1c752020-09-03 10:01:38 +0200315 /*
316 * If audio was streaming, then we need to reenabled the audio
317 * infoframe here during encoder_enable.
318 */
319 if (vc4_hdmi->audio.streaming)
320 vc4_hdmi_set_audio_infoframe(encoder);
Eric Anholt21317b32016-09-29 15:34:43 -0700321}
322
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200323static void vc4_hdmi_encoder_disable(struct drm_encoder *encoder)
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800324{
Maxime Ripard5dfbcae2020-09-03 10:01:17 +0200325 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200326 int ret;
327
Maxime Ripard311e3052020-09-03 10:01:23 +0200328 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG, 0);
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200329
Maxime Ripardc457b8a2020-09-03 10:01:25 +0200330 if (vc4_hdmi->variant->phy_disable)
331 vc4_hdmi->variant->phy_disable(vc4_hdmi);
332
Maxime Ripard311e3052020-09-03 10:01:23 +0200333 HDMI_WRITE(HDMI_VID_CTL,
334 HDMI_READ(HDMI_VID_CTL) & ~VC4_HD_VID_CTL_ENABLE);
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200335
Maxime Ripardcd4cb492020-09-03 10:01:35 +0200336 clk_disable_unprepare(vc4_hdmi->hsm_clock);
Maxime Ripard3408cc22020-09-03 10:01:14 +0200337 clk_disable_unprepare(vc4_hdmi->pixel_clock);
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200338
Maxime Ripard3408cc22020-09-03 10:01:14 +0200339 ret = pm_runtime_put(&vc4_hdmi->pdev->dev);
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200340 if (ret < 0)
341 DRM_ERROR("Failed to release power domain: %d\n", ret);
342}
343
Maxime Ripard89f31a22020-09-03 10:01:27 +0200344static void vc4_hdmi_csc_setup(struct vc4_hdmi *vc4_hdmi, bool enable)
345{
346 u32 csc_ctl;
347
348 csc_ctl = VC4_SET_FIELD(VC4_HD_CSC_CTL_ORDER_BGR,
349 VC4_HD_CSC_CTL_ORDER);
350
351 if (enable) {
352 /* CEA VICs other than #1 requre limited range RGB
353 * output unless overridden by an AVI infoframe.
354 * Apply a colorspace conversion to squash 0-255 down
355 * to 16-235. The matrix here is:
356 *
357 * [ 0 0 0.8594 16]
358 * [ 0 0.8594 0 16]
359 * [ 0.8594 0 0 16]
360 * [ 0 0 0 1]
361 */
362 csc_ctl |= VC4_HD_CSC_CTL_ENABLE;
363 csc_ctl |= VC4_HD_CSC_CTL_RGB2YCC;
364 csc_ctl |= VC4_SET_FIELD(VC4_HD_CSC_CTL_MODE_CUSTOM,
365 VC4_HD_CSC_CTL_MODE);
366
367 HDMI_WRITE(HDMI_CSC_12_11, (0x000 << 16) | 0x000);
368 HDMI_WRITE(HDMI_CSC_14_13, (0x100 << 16) | 0x6e0);
369 HDMI_WRITE(HDMI_CSC_22_21, (0x6e0 << 16) | 0x000);
370 HDMI_WRITE(HDMI_CSC_24_23, (0x100 << 16) | 0x000);
371 HDMI_WRITE(HDMI_CSC_32_31, (0x000 << 16) | 0x6e0);
372 HDMI_WRITE(HDMI_CSC_34_33, (0x100 << 16) | 0x000);
373 }
374
375 /* The RGB order applies even when CSC is disabled. */
376 HDMI_WRITE(HDMI_CSC_CTL, csc_ctl);
377}
378
Maxime Ripard904f6682020-09-03 10:01:28 +0200379static void vc4_hdmi_set_timings(struct vc4_hdmi *vc4_hdmi,
380 struct drm_display_mode *mode)
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200381{
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800382 bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
383 bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
Eric Anholt682e62c2016-09-28 17:30:25 -0700384 bool interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE;
Eric Anholtdfccd932016-09-29 15:34:44 -0700385 u32 pixel_rep = (mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1;
Eric Anholt682e62c2016-09-28 17:30:25 -0700386 u32 verta = (VC4_SET_FIELD(mode->crtc_vsync_end - mode->crtc_vsync_start,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800387 VC4_HDMI_VERTA_VSP) |
Eric Anholt682e62c2016-09-28 17:30:25 -0700388 VC4_SET_FIELD(mode->crtc_vsync_start - mode->crtc_vdisplay,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800389 VC4_HDMI_VERTA_VFP) |
Eric Anholt682e62c2016-09-28 17:30:25 -0700390 VC4_SET_FIELD(mode->crtc_vdisplay, VC4_HDMI_VERTA_VAL));
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800391 u32 vertb = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) |
Eric Anholt682e62c2016-09-28 17:30:25 -0700392 VC4_SET_FIELD(mode->crtc_vtotal - mode->crtc_vsync_end,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800393 VC4_HDMI_VERTB_VBP));
Eric Anholt682e62c2016-09-28 17:30:25 -0700394 u32 vertb_even = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) |
395 VC4_SET_FIELD(mode->crtc_vtotal -
396 mode->crtc_vsync_end -
397 interlaced,
398 VC4_HDMI_VERTB_VBP));
Maxime Ripard904f6682020-09-03 10:01:28 +0200399
400 HDMI_WRITE(HDMI_HORZA,
401 (vsync_pos ? VC4_HDMI_HORZA_VPOS : 0) |
402 (hsync_pos ? VC4_HDMI_HORZA_HPOS : 0) |
403 VC4_SET_FIELD(mode->hdisplay * pixel_rep,
404 VC4_HDMI_HORZA_HAP));
405
406 HDMI_WRITE(HDMI_HORZB,
407 VC4_SET_FIELD((mode->htotal -
408 mode->hsync_end) * pixel_rep,
409 VC4_HDMI_HORZB_HBP) |
410 VC4_SET_FIELD((mode->hsync_end -
411 mode->hsync_start) * pixel_rep,
412 VC4_HDMI_HORZB_HSP) |
413 VC4_SET_FIELD((mode->hsync_start -
414 mode->hdisplay) * pixel_rep,
415 VC4_HDMI_HORZB_HFP));
416
417 HDMI_WRITE(HDMI_VERTA0, verta);
418 HDMI_WRITE(HDMI_VERTA1, verta);
419
420 HDMI_WRITE(HDMI_VERTB0, vertb_even);
421 HDMI_WRITE(HDMI_VERTB1, vertb);
422
423 HDMI_WRITE(HDMI_VID_CTL,
424 (vsync_pos ? 0 : VC4_HD_VID_CTL_VSYNC_LOW) |
425 (hsync_pos ? 0 : VC4_HD_VID_CTL_HSYNC_LOW));
426}
427
428static void vc4_hdmi_encoder_enable(struct drm_encoder *encoder)
429{
430 struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
431 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
432 struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
433 bool debug_dump_regs = false;
Maxime Ripardcd4cb492020-09-03 10:01:35 +0200434 unsigned long pixel_rate, hsm_rate;
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200435 int ret;
436
Maxime Ripard3408cc22020-09-03 10:01:14 +0200437 ret = pm_runtime_get_sync(&vc4_hdmi->pdev->dev);
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200438 if (ret < 0) {
439 DRM_ERROR("Failed to retain power domain: %d\n", ret);
440 return;
441 }
442
Maxime Ripardcd4cb492020-09-03 10:01:35 +0200443 pixel_rate = mode->clock * 1000 * ((mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1);
444 ret = clk_set_rate(vc4_hdmi->pixel_clock, pixel_rate);
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200445 if (ret) {
446 DRM_ERROR("Failed to set pixel clock rate: %d\n", ret);
447 return;
448 }
449
Maxime Ripard3408cc22020-09-03 10:01:14 +0200450 ret = clk_prepare_enable(vc4_hdmi->pixel_clock);
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200451 if (ret) {
452 DRM_ERROR("Failed to turn on pixel clock: %d\n", ret);
453 return;
454 }
455
Maxime Ripardcd4cb492020-09-03 10:01:35 +0200456 /*
457 * As stated in RPi's vc4 firmware "HDMI state machine (HSM) clock must
458 * be faster than pixel clock, infinitesimally faster, tested in
459 * simulation. Otherwise, exact value is unimportant for HDMI
460 * operation." This conflicts with bcm2835's vc4 documentation, which
461 * states HSM's clock has to be at least 108% of the pixel clock.
462 *
463 * Real life tests reveal that vc4's firmware statement holds up, and
464 * users are able to use pixel clocks closer to HSM's, namely for
465 * 1920x1200@60Hz. So it was decided to have leave a 1% margin between
466 * both clocks. Which, for RPi0-3 implies a maximum pixel clock of
467 * 162MHz.
468 *
469 * Additionally, the AXI clock needs to be at least 25% of
470 * pixel clock, but HSM ends up being the limiting factor.
471 */
472 hsm_rate = max_t(unsigned long, 120000000, (pixel_rate / 100) * 101);
Maxime Ripardd5d5ce82020-09-03 10:01:36 +0200473 ret = clk_set_min_rate(vc4_hdmi->hsm_clock, hsm_rate);
Maxime Ripardcd4cb492020-09-03 10:01:35 +0200474 if (ret) {
475 DRM_ERROR("Failed to set HSM clock rate: %d\n", ret);
476 return;
477 }
478
479 ret = clk_prepare_enable(vc4_hdmi->hsm_clock);
480 if (ret) {
481 DRM_ERROR("Failed to turn on HSM clock: %d\n", ret);
482 clk_disable_unprepare(vc4_hdmi->pixel_clock);
483 return;
484 }
485
Maxime Ripard9045e912020-09-03 10:01:24 +0200486 if (vc4_hdmi->variant->reset)
487 vc4_hdmi->variant->reset(vc4_hdmi);
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200488
Maxime Ripardc457b8a2020-09-03 10:01:25 +0200489 if (vc4_hdmi->variant->phy_init)
490 vc4_hdmi->variant->phy_init(vc4_hdmi, mode);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800491
492 if (debug_dump_regs) {
Maxime Ripard3408cc22020-09-03 10:01:14 +0200493 struct drm_printer p = drm_info_printer(&vc4_hdmi->pdev->dev);
Eric Anholt30517192019-02-20 13:03:38 -0800494
Maxime Ripard3408cc22020-09-03 10:01:14 +0200495 dev_info(&vc4_hdmi->pdev->dev, "HDMI regs before:\n");
496 drm_print_regset32(&p, &vc4_hdmi->hdmi_regset);
497 drm_print_regset32(&p, &vc4_hdmi->hd_regset);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800498 }
499
Maxime Ripard311e3052020-09-03 10:01:23 +0200500 HDMI_WRITE(HDMI_VID_CTL, 0);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800501
Maxime Ripard311e3052020-09-03 10:01:23 +0200502 HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
503 HDMI_READ(HDMI_SCHEDULER_CONTROL) |
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800504 VC4_HDMI_SCHEDULER_CONTROL_MANUAL_FORMAT |
505 VC4_HDMI_SCHEDULER_CONTROL_IGNORE_VSYNC_PREDICTS);
506
Maxime Ripard904f6682020-09-03 10:01:28 +0200507 if (vc4_hdmi->variant->set_timings)
508 vc4_hdmi->variant->set_timings(vc4_hdmi, mode);
Eric Anholt6e1cbba2016-09-16 10:59:45 +0100509
Ville Syrjäläc8127cf02017-01-11 16:18:35 +0200510 if (vc4_encoder->hdmi_monitor &&
Maxime Ripard89f31a22020-09-03 10:01:27 +0200511 drm_default_rgb_quant_range(mode) == HDMI_QUANTIZATION_RANGE_LIMITED) {
512 if (vc4_hdmi->variant->csc_setup)
513 vc4_hdmi->variant->csc_setup(vc4_hdmi, true);
Eric Anholt6e1cbba2016-09-16 10:59:45 +0100514
Eric Anholt21317b32016-09-29 15:34:43 -0700515 vc4_encoder->limited_rgb_range = true;
516 } else {
Maxime Ripard89f31a22020-09-03 10:01:27 +0200517 if (vc4_hdmi->variant->csc_setup)
518 vc4_hdmi->variant->csc_setup(vc4_hdmi, false);
519
Eric Anholt21317b32016-09-29 15:34:43 -0700520 vc4_encoder->limited_rgb_range = false;
Eric Anholt6e1cbba2016-09-16 10:59:45 +0100521 }
522
Maxime Ripard311e3052020-09-03 10:01:23 +0200523 HDMI_WRITE(HDMI_FIFO_CTL, VC4_HDMI_FIFO_CTL_MASTER_SLAVE_N);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800524
525 if (debug_dump_regs) {
Maxime Ripard3408cc22020-09-03 10:01:14 +0200526 struct drm_printer p = drm_info_printer(&vc4_hdmi->pdev->dev);
Eric Anholt30517192019-02-20 13:03:38 -0800527
Maxime Ripard3408cc22020-09-03 10:01:14 +0200528 dev_info(&vc4_hdmi->pdev->dev, "HDMI regs after:\n");
529 drm_print_regset32(&p, &vc4_hdmi->hdmi_regset);
530 drm_print_regset32(&p, &vc4_hdmi->hd_regset);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800531 }
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800532
Maxime Ripard311e3052020-09-03 10:01:23 +0200533 HDMI_WRITE(HDMI_VID_CTL,
534 HDMI_READ(HDMI_VID_CTL) |
535 VC4_HD_VID_CTL_ENABLE |
536 VC4_HD_VID_CTL_UNDERFLOW_ENABLE |
537 VC4_HD_VID_CTL_FRAME_COUNTER_RESET);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800538
539 if (vc4_encoder->hdmi_monitor) {
Maxime Ripard311e3052020-09-03 10:01:23 +0200540 HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
541 HDMI_READ(HDMI_SCHEDULER_CONTROL) |
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800542 VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);
543
Maxime Ripard311e3052020-09-03 10:01:23 +0200544 ret = wait_for(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
Eric Anholt2b29bf12016-09-28 17:21:05 -0700545 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE, 1000);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800546 WARN_ONCE(ret, "Timeout waiting for "
547 "VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
548 } else {
Maxime Ripard311e3052020-09-03 10:01:23 +0200549 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
550 HDMI_READ(HDMI_RAM_PACKET_CONFIG) &
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800551 ~(VC4_HDMI_RAM_PACKET_ENABLE));
Maxime Ripard311e3052020-09-03 10:01:23 +0200552 HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
553 HDMI_READ(HDMI_SCHEDULER_CONTROL) &
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800554 ~VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);
555
Maxime Ripard311e3052020-09-03 10:01:23 +0200556 ret = wait_for(!(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
Eric Anholt2b29bf12016-09-28 17:21:05 -0700557 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE), 1000);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800558 WARN_ONCE(ret, "Timeout waiting for "
559 "!VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
560 }
561
562 if (vc4_encoder->hdmi_monitor) {
563 u32 drift;
564
Maxime Ripard311e3052020-09-03 10:01:23 +0200565 WARN_ON(!(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800566 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE));
Maxime Ripard311e3052020-09-03 10:01:23 +0200567 HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
568 HDMI_READ(HDMI_SCHEDULER_CONTROL) |
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800569 VC4_HDMI_SCHEDULER_CONTROL_VERT_ALWAYS_KEEPOUT);
570
Maxime Ripard311e3052020-09-03 10:01:23 +0200571 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
Eric Anholt21317b32016-09-29 15:34:43 -0700572 VC4_HDMI_RAM_PACKET_ENABLE);
573
574 vc4_hdmi_set_infoframes(encoder);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800575
Maxime Ripard311e3052020-09-03 10:01:23 +0200576 drift = HDMI_READ(HDMI_FIFO_CTL);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800577 drift &= VC4_HDMI_FIFO_VALID_WRITE_MASK;
578
Maxime Ripard311e3052020-09-03 10:01:23 +0200579 HDMI_WRITE(HDMI_FIFO_CTL,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800580 drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
Maxime Ripard311e3052020-09-03 10:01:23 +0200581 HDMI_WRITE(HDMI_FIFO_CTL,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800582 drift | VC4_HDMI_FIFO_CTL_RECENTER);
Stefan Wahrend8eb9de2018-02-24 13:38:14 +0100583 usleep_range(1000, 1100);
Maxime Ripard311e3052020-09-03 10:01:23 +0200584 HDMI_WRITE(HDMI_FIFO_CTL,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800585 drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
Maxime Ripard311e3052020-09-03 10:01:23 +0200586 HDMI_WRITE(HDMI_FIFO_CTL,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800587 drift | VC4_HDMI_FIFO_CTL_RECENTER);
588
Maxime Ripard311e3052020-09-03 10:01:23 +0200589 ret = wait_for(HDMI_READ(HDMI_FIFO_CTL) &
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800590 VC4_HDMI_FIFO_CTL_RECENTER_DONE, 1);
591 WARN_ONCE(ret, "Timeout waiting for "
592 "VC4_HDMI_FIFO_CTL_RECENTER_DONE");
593 }
594}
595
Eric Anholt32e823c2017-09-20 15:59:34 -0700596static enum drm_mode_status
Maxime Ripard11a17312020-09-03 10:01:34 +0200597vc4_hdmi_encoder_mode_valid(struct drm_encoder *encoder,
Eric Anholt32e823c2017-09-20 15:59:34 -0700598 const struct drm_display_mode *mode)
599{
Maxime Ripardcd4cb492020-09-03 10:01:35 +0200600 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
601
602 if ((mode->clock * 1000) > vc4_hdmi->variant->max_pixel_clock)
Eric Anholt32e823c2017-09-20 15:59:34 -0700603 return MODE_CLOCK_HIGH;
604
605 return MODE_OK;
606}
607
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800608static const struct drm_encoder_helper_funcs vc4_hdmi_encoder_helper_funcs = {
Eric Anholt32e823c2017-09-20 15:59:34 -0700609 .mode_valid = vc4_hdmi_encoder_mode_valid,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800610 .disable = vc4_hdmi_encoder_disable,
611 .enable = vc4_hdmi_encoder_enable,
612};
613
Dave Stevenson632ee3a2020-09-03 10:01:40 +0200614static u32 vc4_hdmi_channel_map(struct vc4_hdmi *vc4_hdmi, u32 channel_mask)
615{
616 int i;
617 u32 channel_map = 0;
618
619 for (i = 0; i < 8; i++) {
620 if (channel_mask & BIT(i))
621 channel_map |= i << (3 * i);
622 }
623 return channel_map;
624}
625
Eric Anholtbb7d7852017-02-27 12:28:02 -0800626/* HDMI audio codec callbacks */
Maxime Ripard3408cc22020-09-03 10:01:14 +0200627static void vc4_hdmi_audio_set_mai_clock(struct vc4_hdmi *vc4_hdmi)
Eric Anholtbb7d7852017-02-27 12:28:02 -0800628{
Dave Stevenson632ee3a2020-09-03 10:01:40 +0200629 u32 hsm_clock = clk_get_rate(vc4_hdmi->audio_clock);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800630 unsigned long n, m;
631
Maxime Ripard3408cc22020-09-03 10:01:14 +0200632 rational_best_approximation(hsm_clock, vc4_hdmi->audio.samplerate,
Eric Anholtbb7d7852017-02-27 12:28:02 -0800633 VC4_HD_MAI_SMP_N_MASK >>
634 VC4_HD_MAI_SMP_N_SHIFT,
635 (VC4_HD_MAI_SMP_M_MASK >>
636 VC4_HD_MAI_SMP_M_SHIFT) + 1,
637 &n, &m);
638
Maxime Ripard311e3052020-09-03 10:01:23 +0200639 HDMI_WRITE(HDMI_MAI_SMP,
640 VC4_SET_FIELD(n, VC4_HD_MAI_SMP_N) |
641 VC4_SET_FIELD(m - 1, VC4_HD_MAI_SMP_M));
Eric Anholtbb7d7852017-02-27 12:28:02 -0800642}
643
Maxime Ripard3408cc22020-09-03 10:01:14 +0200644static void vc4_hdmi_set_n_cts(struct vc4_hdmi *vc4_hdmi)
Eric Anholtbb7d7852017-02-27 12:28:02 -0800645{
Maxime Ripard3408cc22020-09-03 10:01:14 +0200646 struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800647 struct drm_crtc *crtc = encoder->crtc;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800648 const struct drm_display_mode *mode = &crtc->state->adjusted_mode;
Maxime Ripard3408cc22020-09-03 10:01:14 +0200649 u32 samplerate = vc4_hdmi->audio.samplerate;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800650 u32 n, cts;
651 u64 tmp;
652
653 n = 128 * samplerate / 1000;
654 tmp = (u64)(mode->clock * 1000) * n;
655 do_div(tmp, 128 * samplerate);
656 cts = tmp;
657
Maxime Ripard311e3052020-09-03 10:01:23 +0200658 HDMI_WRITE(HDMI_CRP_CFG,
Eric Anholtbb7d7852017-02-27 12:28:02 -0800659 VC4_HDMI_CRP_CFG_EXTERNAL_CTS_EN |
660 VC4_SET_FIELD(n, VC4_HDMI_CRP_CFG_N));
661
662 /*
663 * We could get slightly more accurate clocks in some cases by
664 * providing a CTS_1 value. The two CTS values are alternated
665 * between based on the period fields
666 */
Maxime Ripard311e3052020-09-03 10:01:23 +0200667 HDMI_WRITE(HDMI_CTS_0, cts);
668 HDMI_WRITE(HDMI_CTS_1, cts);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800669}
670
671static inline struct vc4_hdmi *dai_to_hdmi(struct snd_soc_dai *dai)
672{
673 struct snd_soc_card *card = snd_soc_dai_get_drvdata(dai);
674
675 return snd_soc_card_get_drvdata(card);
676}
677
678static int vc4_hdmi_audio_startup(struct snd_pcm_substream *substream,
679 struct snd_soc_dai *dai)
680{
Maxime Ripard3408cc22020-09-03 10:01:14 +0200681 struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai);
682 struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
Maxime Ripard0532e5e2020-09-03 10:01:21 +0200683 struct drm_connector *connector = &vc4_hdmi->connector;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800684 int ret;
685
Maxime Ripard3408cc22020-09-03 10:01:14 +0200686 if (vc4_hdmi->audio.substream && vc4_hdmi->audio.substream != substream)
Eric Anholtbb7d7852017-02-27 12:28:02 -0800687 return -EINVAL;
688
Maxime Ripard3408cc22020-09-03 10:01:14 +0200689 vc4_hdmi->audio.substream = substream;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800690
691 /*
692 * If the HDMI encoder hasn't probed, or the encoder is
693 * currently in DVI mode, treat the codec dai as missing.
694 */
Maxime Ripard311e3052020-09-03 10:01:23 +0200695 if (!encoder->crtc || !(HDMI_READ(HDMI_RAM_PACKET_CONFIG) &
Eric Anholtbb7d7852017-02-27 12:28:02 -0800696 VC4_HDMI_RAM_PACKET_ENABLE))
697 return -ENODEV;
698
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200699 ret = snd_pcm_hw_constraint_eld(substream->runtime, connector->eld);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800700 if (ret)
701 return ret;
702
703 return 0;
704}
705
706static int vc4_hdmi_audio_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
707{
708 return 0;
709}
710
Maxime Ripard3408cc22020-09-03 10:01:14 +0200711static void vc4_hdmi_audio_reset(struct vc4_hdmi *vc4_hdmi)
Eric Anholtbb7d7852017-02-27 12:28:02 -0800712{
Maxime Ripard3408cc22020-09-03 10:01:14 +0200713 struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
Maxime Ripard3408cc22020-09-03 10:01:14 +0200714 struct device *dev = &vc4_hdmi->pdev->dev;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800715 int ret;
716
Dave Stevenson6ac1c752020-09-03 10:01:38 +0200717 vc4_hdmi->audio.streaming = false;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800718 ret = vc4_hdmi_stop_packet(encoder, HDMI_INFOFRAME_TYPE_AUDIO);
719 if (ret)
720 dev_err(dev, "Failed to stop audio infoframe: %d\n", ret);
721
Maxime Ripard311e3052020-09-03 10:01:23 +0200722 HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_RESET);
723 HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_ERRORF);
724 HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_FLUSH);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800725}
726
727static void vc4_hdmi_audio_shutdown(struct snd_pcm_substream *substream,
728 struct snd_soc_dai *dai)
729{
Maxime Ripard3408cc22020-09-03 10:01:14 +0200730 struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800731
Maxime Ripard3408cc22020-09-03 10:01:14 +0200732 if (substream != vc4_hdmi->audio.substream)
Eric Anholtbb7d7852017-02-27 12:28:02 -0800733 return;
734
Maxime Ripard3408cc22020-09-03 10:01:14 +0200735 vc4_hdmi_audio_reset(vc4_hdmi);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800736
Maxime Ripard3408cc22020-09-03 10:01:14 +0200737 vc4_hdmi->audio.substream = NULL;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800738}
739
740/* HDMI audio codec callbacks */
741static int vc4_hdmi_audio_hw_params(struct snd_pcm_substream *substream,
742 struct snd_pcm_hw_params *params,
743 struct snd_soc_dai *dai)
744{
Maxime Ripard3408cc22020-09-03 10:01:14 +0200745 struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai);
Maxime Ripard3408cc22020-09-03 10:01:14 +0200746 struct device *dev = &vc4_hdmi->pdev->dev;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800747 u32 audio_packet_config, channel_mask;
Dave Stevenson632ee3a2020-09-03 10:01:40 +0200748 u32 channel_map;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800749
Maxime Ripard3408cc22020-09-03 10:01:14 +0200750 if (substream != vc4_hdmi->audio.substream)
Eric Anholtbb7d7852017-02-27 12:28:02 -0800751 return -EINVAL;
752
753 dev_dbg(dev, "%s: %u Hz, %d bit, %d channels\n", __func__,
754 params_rate(params), params_width(params),
755 params_channels(params));
756
Maxime Ripard3408cc22020-09-03 10:01:14 +0200757 vc4_hdmi->audio.channels = params_channels(params);
758 vc4_hdmi->audio.samplerate = params_rate(params);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800759
Maxime Ripard311e3052020-09-03 10:01:23 +0200760 HDMI_WRITE(HDMI_MAI_CTL,
761 VC4_HD_MAI_CTL_RESET |
762 VC4_HD_MAI_CTL_FLUSH |
763 VC4_HD_MAI_CTL_DLATE |
764 VC4_HD_MAI_CTL_ERRORE |
765 VC4_HD_MAI_CTL_ERRORF);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800766
Maxime Ripard3408cc22020-09-03 10:01:14 +0200767 vc4_hdmi_audio_set_mai_clock(vc4_hdmi);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800768
Dave Stevensonb9b8bac2020-09-03 10:01:39 +0200769 /* The B frame identifier should match the value used by alsa-lib (8) */
Eric Anholtbb7d7852017-02-27 12:28:02 -0800770 audio_packet_config =
771 VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_SAMPLE_FLAT |
772 VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_INACTIVE_CHANNELS |
Dave Stevensonb9b8bac2020-09-03 10:01:39 +0200773 VC4_SET_FIELD(0x8, VC4_HDMI_AUDIO_PACKET_B_FRAME_IDENTIFIER);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800774
Maxime Ripard3408cc22020-09-03 10:01:14 +0200775 channel_mask = GENMASK(vc4_hdmi->audio.channels - 1, 0);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800776 audio_packet_config |= VC4_SET_FIELD(channel_mask,
777 VC4_HDMI_AUDIO_PACKET_CEA_MASK);
778
779 /* Set the MAI threshold. This logic mimics the firmware's. */
Maxime Ripard3408cc22020-09-03 10:01:14 +0200780 if (vc4_hdmi->audio.samplerate > 96000) {
Maxime Ripard311e3052020-09-03 10:01:23 +0200781 HDMI_WRITE(HDMI_MAI_THR,
782 VC4_SET_FIELD(0x12, VC4_HD_MAI_THR_DREQHIGH) |
783 VC4_SET_FIELD(0x12, VC4_HD_MAI_THR_DREQLOW));
Maxime Ripard3408cc22020-09-03 10:01:14 +0200784 } else if (vc4_hdmi->audio.samplerate > 48000) {
Maxime Ripard311e3052020-09-03 10:01:23 +0200785 HDMI_WRITE(HDMI_MAI_THR,
786 VC4_SET_FIELD(0x14, VC4_HD_MAI_THR_DREQHIGH) |
787 VC4_SET_FIELD(0x12, VC4_HD_MAI_THR_DREQLOW));
Eric Anholtbb7d7852017-02-27 12:28:02 -0800788 } else {
Maxime Ripard311e3052020-09-03 10:01:23 +0200789 HDMI_WRITE(HDMI_MAI_THR,
790 VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_PANICHIGH) |
791 VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_PANICLOW) |
792 VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_DREQHIGH) |
793 VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_DREQLOW));
Eric Anholtbb7d7852017-02-27 12:28:02 -0800794 }
795
Maxime Ripard311e3052020-09-03 10:01:23 +0200796 HDMI_WRITE(HDMI_MAI_CONFIG,
Eric Anholtbb7d7852017-02-27 12:28:02 -0800797 VC4_HDMI_MAI_CONFIG_BIT_REVERSE |
798 VC4_SET_FIELD(channel_mask, VC4_HDMI_MAI_CHANNEL_MASK));
799
Dave Stevenson632ee3a2020-09-03 10:01:40 +0200800 channel_map = vc4_hdmi->variant->channel_map(vc4_hdmi, channel_mask);
Maxime Ripard311e3052020-09-03 10:01:23 +0200801 HDMI_WRITE(HDMI_MAI_CHANNEL_MAP, channel_map);
802 HDMI_WRITE(HDMI_AUDIO_PACKET_CONFIG, audio_packet_config);
Maxime Ripard3408cc22020-09-03 10:01:14 +0200803 vc4_hdmi_set_n_cts(vc4_hdmi);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800804
805 return 0;
806}
807
808static int vc4_hdmi_audio_trigger(struct snd_pcm_substream *substream, int cmd,
809 struct snd_soc_dai *dai)
810{
Maxime Ripard3408cc22020-09-03 10:01:14 +0200811 struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai);
812 struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800813
814 switch (cmd) {
815 case SNDRV_PCM_TRIGGER_START:
816 vc4_hdmi_set_audio_infoframe(encoder);
Dave Stevenson6ac1c752020-09-03 10:01:38 +0200817 vc4_hdmi->audio.streaming = true;
Maxime Ripard647b9652020-09-03 10:01:26 +0200818
819 if (vc4_hdmi->variant->phy_rng_enable)
820 vc4_hdmi->variant->phy_rng_enable(vc4_hdmi);
Maxime Ripard311e3052020-09-03 10:01:23 +0200821
822 HDMI_WRITE(HDMI_MAI_CTL,
823 VC4_SET_FIELD(vc4_hdmi->audio.channels,
824 VC4_HD_MAI_CTL_CHNUM) |
825 VC4_HD_MAI_CTL_ENABLE);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800826 break;
827 case SNDRV_PCM_TRIGGER_STOP:
Maxime Ripard311e3052020-09-03 10:01:23 +0200828 HDMI_WRITE(HDMI_MAI_CTL,
829 VC4_HD_MAI_CTL_DLATE |
830 VC4_HD_MAI_CTL_ERRORE |
831 VC4_HD_MAI_CTL_ERRORF);
Maxime Ripard647b9652020-09-03 10:01:26 +0200832
833 if (vc4_hdmi->variant->phy_rng_disable)
834 vc4_hdmi->variant->phy_rng_disable(vc4_hdmi);
835
Dave Stevenson6ac1c752020-09-03 10:01:38 +0200836 vc4_hdmi->audio.streaming = false;
837
Eric Anholtbb7d7852017-02-27 12:28:02 -0800838 break;
839 default:
840 break;
841 }
842
843 return 0;
844}
845
846static inline struct vc4_hdmi *
847snd_component_to_hdmi(struct snd_soc_component *component)
848{
849 struct snd_soc_card *card = snd_soc_component_get_drvdata(component);
850
851 return snd_soc_card_get_drvdata(card);
852}
853
854static int vc4_hdmi_audio_eld_ctl_info(struct snd_kcontrol *kcontrol,
855 struct snd_ctl_elem_info *uinfo)
856{
857 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Maxime Ripard3408cc22020-09-03 10:01:14 +0200858 struct vc4_hdmi *vc4_hdmi = snd_component_to_hdmi(component);
Maxime Ripard0532e5e2020-09-03 10:01:21 +0200859 struct drm_connector *connector = &vc4_hdmi->connector;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800860
861 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200862 uinfo->count = sizeof(connector->eld);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800863
864 return 0;
865}
866
867static int vc4_hdmi_audio_eld_ctl_get(struct snd_kcontrol *kcontrol,
868 struct snd_ctl_elem_value *ucontrol)
869{
870 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Maxime Ripard3408cc22020-09-03 10:01:14 +0200871 struct vc4_hdmi *vc4_hdmi = snd_component_to_hdmi(component);
Maxime Ripard0532e5e2020-09-03 10:01:21 +0200872 struct drm_connector *connector = &vc4_hdmi->connector;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800873
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200874 memcpy(ucontrol->value.bytes.data, connector->eld,
875 sizeof(connector->eld));
Eric Anholtbb7d7852017-02-27 12:28:02 -0800876
877 return 0;
878}
879
880static const struct snd_kcontrol_new vc4_hdmi_audio_controls[] = {
881 {
882 .access = SNDRV_CTL_ELEM_ACCESS_READ |
883 SNDRV_CTL_ELEM_ACCESS_VOLATILE,
884 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
885 .name = "ELD",
886 .info = vc4_hdmi_audio_eld_ctl_info,
887 .get = vc4_hdmi_audio_eld_ctl_get,
888 },
889};
890
891static const struct snd_soc_dapm_widget vc4_hdmi_audio_widgets[] = {
892 SND_SOC_DAPM_OUTPUT("TX"),
893};
894
895static const struct snd_soc_dapm_route vc4_hdmi_audio_routes[] = {
896 { "TX", NULL, "Playback" },
897};
898
Kuninori Morimoto635b1c12018-01-29 04:35:04 +0000899static const struct snd_soc_component_driver vc4_hdmi_audio_component_drv = {
900 .controls = vc4_hdmi_audio_controls,
901 .num_controls = ARRAY_SIZE(vc4_hdmi_audio_controls),
902 .dapm_widgets = vc4_hdmi_audio_widgets,
903 .num_dapm_widgets = ARRAY_SIZE(vc4_hdmi_audio_widgets),
904 .dapm_routes = vc4_hdmi_audio_routes,
905 .num_dapm_routes = ARRAY_SIZE(vc4_hdmi_audio_routes),
906 .idle_bias_on = 1,
907 .use_pmdown_time = 1,
908 .endianness = 1,
909 .non_legacy_dai_naming = 1,
Eric Anholtbb7d7852017-02-27 12:28:02 -0800910};
911
912static const struct snd_soc_dai_ops vc4_hdmi_audio_dai_ops = {
913 .startup = vc4_hdmi_audio_startup,
914 .shutdown = vc4_hdmi_audio_shutdown,
915 .hw_params = vc4_hdmi_audio_hw_params,
916 .set_fmt = vc4_hdmi_audio_set_fmt,
917 .trigger = vc4_hdmi_audio_trigger,
918};
919
920static struct snd_soc_dai_driver vc4_hdmi_audio_codec_dai_drv = {
921 .name = "vc4-hdmi-hifi",
922 .playback = {
923 .stream_name = "Playback",
924 .channels_min = 2,
925 .channels_max = 8,
926 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
927 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
928 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
929 SNDRV_PCM_RATE_192000,
930 .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
931 },
932};
933
934static const struct snd_soc_component_driver vc4_hdmi_audio_cpu_dai_comp = {
935 .name = "vc4-hdmi-cpu-dai-component",
936};
937
938static int vc4_hdmi_audio_cpu_dai_probe(struct snd_soc_dai *dai)
939{
Maxime Ripard3408cc22020-09-03 10:01:14 +0200940 struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800941
Maxime Ripard3408cc22020-09-03 10:01:14 +0200942 snd_soc_dai_init_dma_data(dai, &vc4_hdmi->audio.dma_data, NULL);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800943
944 return 0;
945}
946
947static struct snd_soc_dai_driver vc4_hdmi_audio_cpu_dai_drv = {
948 .name = "vc4-hdmi-cpu-dai",
949 .probe = vc4_hdmi_audio_cpu_dai_probe,
950 .playback = {
951 .stream_name = "Playback",
952 .channels_min = 1,
953 .channels_max = 8,
954 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
955 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
956 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
957 SNDRV_PCM_RATE_192000,
958 .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
959 },
960 .ops = &vc4_hdmi_audio_dai_ops,
961};
962
963static const struct snd_dmaengine_pcm_config pcm_conf = {
964 .chan_names[SNDRV_PCM_STREAM_PLAYBACK] = "audio-rx",
965 .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
966};
967
Maxime Ripard3408cc22020-09-03 10:01:14 +0200968static int vc4_hdmi_audio_init(struct vc4_hdmi *vc4_hdmi)
Eric Anholtbb7d7852017-02-27 12:28:02 -0800969{
Maxime Ripard311e3052020-09-03 10:01:23 +0200970 const struct vc4_hdmi_register *mai_data =
971 &vc4_hdmi->variant->registers[HDMI_MAI_DATA];
Maxime Ripard3408cc22020-09-03 10:01:14 +0200972 struct snd_soc_dai_link *dai_link = &vc4_hdmi->audio.link;
973 struct snd_soc_card *card = &vc4_hdmi->audio.card;
974 struct device *dev = &vc4_hdmi->pdev->dev;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800975 const __be32 *addr;
Dave Stevenson094864b2020-09-03 10:01:37 +0200976 int index;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800977 int ret;
978
979 if (!of_find_property(dev->of_node, "dmas", NULL)) {
980 dev_warn(dev,
981 "'dmas' DT property is missing, no HDMI audio\n");
982 return 0;
983 }
984
Maxime Ripard311e3052020-09-03 10:01:23 +0200985 if (mai_data->reg != VC4_HD) {
986 WARN_ONCE(true, "MAI isn't in the HD block\n");
987 return -EINVAL;
988 }
989
Eric Anholtbb7d7852017-02-27 12:28:02 -0800990 /*
991 * Get the physical address of VC4_HD_MAI_DATA. We need to retrieve
992 * the bus address specified in the DT, because the physical address
993 * (the one returned by platform_get_resource()) is not appropriate
994 * for DMA transfers.
995 * This VC/MMU should probably be exposed to avoid this kind of hacks.
996 */
Dave Stevenson094864b2020-09-03 10:01:37 +0200997 index = of_property_match_string(dev->of_node, "reg-names", "hd");
998 /* Before BCM2711, we don't have a named register range */
999 if (index < 0)
1000 index = 1;
1001
1002 addr = of_get_address(dev->of_node, index, NULL, NULL);
1003
Maxime Ripard311e3052020-09-03 10:01:23 +02001004 vc4_hdmi->audio.dma_data.addr = be32_to_cpup(addr) + mai_data->offset;
Maxime Ripard3408cc22020-09-03 10:01:14 +02001005 vc4_hdmi->audio.dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1006 vc4_hdmi->audio.dma_data.maxburst = 2;
Eric Anholtbb7d7852017-02-27 12:28:02 -08001007
1008 ret = devm_snd_dmaengine_pcm_register(dev, &pcm_conf, 0);
1009 if (ret) {
1010 dev_err(dev, "Could not register PCM component: %d\n", ret);
1011 return ret;
1012 }
1013
1014 ret = devm_snd_soc_register_component(dev, &vc4_hdmi_audio_cpu_dai_comp,
1015 &vc4_hdmi_audio_cpu_dai_drv, 1);
1016 if (ret) {
1017 dev_err(dev, "Could not register CPU DAI: %d\n", ret);
1018 return ret;
1019 }
1020
Kuninori Morimoto635b1c12018-01-29 04:35:04 +00001021 /* register component and codec dai */
1022 ret = devm_snd_soc_register_component(dev, &vc4_hdmi_audio_component_drv,
Eric Anholtbb7d7852017-02-27 12:28:02 -08001023 &vc4_hdmi_audio_codec_dai_drv, 1);
1024 if (ret) {
Kuninori Morimoto635b1c12018-01-29 04:35:04 +00001025 dev_err(dev, "Could not register component: %d\n", ret);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001026 return ret;
1027 }
1028
Maxime Ripard3408cc22020-09-03 10:01:14 +02001029 dai_link->cpus = &vc4_hdmi->audio.cpu;
1030 dai_link->codecs = &vc4_hdmi->audio.codec;
1031 dai_link->platforms = &vc4_hdmi->audio.platform;
Kuninori Morimoto0467d8e2019-06-06 13:19:19 +09001032
1033 dai_link->num_cpus = 1;
1034 dai_link->num_codecs = 1;
Kuninori Morimoto8a90efd2019-06-28 10:46:14 +09001035 dai_link->num_platforms = 1;
Kuninori Morimoto0467d8e2019-06-06 13:19:19 +09001036
Eric Anholtbb7d7852017-02-27 12:28:02 -08001037 dai_link->name = "MAI";
1038 dai_link->stream_name = "MAI PCM";
Kuninori Morimoto0467d8e2019-06-06 13:19:19 +09001039 dai_link->codecs->dai_name = vc4_hdmi_audio_codec_dai_drv.name;
1040 dai_link->cpus->dai_name = dev_name(dev);
1041 dai_link->codecs->name = dev_name(dev);
Kuninori Morimoto8a90efd2019-06-28 10:46:14 +09001042 dai_link->platforms->name = dev_name(dev);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001043
1044 card->dai_link = dai_link;
1045 card->num_links = 1;
Maxime Ripard9be43a52020-09-03 10:01:41 +02001046 card->name = vc4_hdmi->variant->card_name;
Eric Anholtbb7d7852017-02-27 12:28:02 -08001047 card->dev = dev;
1048
1049 /*
1050 * Be careful, snd_soc_register_card() calls dev_set_drvdata() and
1051 * stores a pointer to the snd card object in dev->driver_data. This
1052 * means we cannot use it for something else. The hdmi back-pointer is
1053 * now stored in card->drvdata and should be retrieved with
1054 * snd_soc_card_get_drvdata() if needed.
1055 */
Maxime Ripard3408cc22020-09-03 10:01:14 +02001056 snd_soc_card_set_drvdata(card, vc4_hdmi);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001057 ret = devm_snd_soc_register_card(dev, card);
Kuninori Morimoto635b1c12018-01-29 04:35:04 +00001058 if (ret)
Eric Anholtbb7d7852017-02-27 12:28:02 -08001059 dev_err(dev, "Could not register sound card: %d\n", ret);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001060
1061 return ret;
Eric Anholtbb7d7852017-02-27 12:28:02 -08001062
Eric Anholtbb7d7852017-02-27 12:28:02 -08001063}
1064
Hans Verkuil15b45112017-07-16 12:48:04 +02001065#ifdef CONFIG_DRM_VC4_HDMI_CEC
1066static irqreturn_t vc4_cec_irq_handler_thread(int irq, void *priv)
1067{
Maxime Ripard66bf1c32020-09-03 10:01:18 +02001068 struct vc4_hdmi *vc4_hdmi = priv;
Hans Verkuil15b45112017-07-16 12:48:04 +02001069
Maxime Ripard3408cc22020-09-03 10:01:14 +02001070 if (vc4_hdmi->cec_irq_was_rx) {
1071 if (vc4_hdmi->cec_rx_msg.len)
1072 cec_received_msg(vc4_hdmi->cec_adap,
1073 &vc4_hdmi->cec_rx_msg);
1074 } else if (vc4_hdmi->cec_tx_ok) {
1075 cec_transmit_done(vc4_hdmi->cec_adap, CEC_TX_STATUS_OK,
Hans Verkuil15b45112017-07-16 12:48:04 +02001076 0, 0, 0, 0);
1077 } else {
1078 /*
1079 * This CEC implementation makes 1 retry, so if we
1080 * get a NACK, then that means it made 2 attempts.
1081 */
Maxime Ripard3408cc22020-09-03 10:01:14 +02001082 cec_transmit_done(vc4_hdmi->cec_adap, CEC_TX_STATUS_NACK,
Hans Verkuil15b45112017-07-16 12:48:04 +02001083 0, 2, 0, 0);
1084 }
1085 return IRQ_HANDLED;
1086}
1087
Maxime Ripard66bf1c32020-09-03 10:01:18 +02001088static void vc4_cec_read_msg(struct vc4_hdmi *vc4_hdmi, u32 cntrl1)
Hans Verkuil15b45112017-07-16 12:48:04 +02001089{
Maxime Ripard13311452020-09-03 10:01:15 +02001090 struct cec_msg *msg = &vc4_hdmi->cec_rx_msg;
Hans Verkuil15b45112017-07-16 12:48:04 +02001091 unsigned int i;
1092
1093 msg->len = 1 + ((cntrl1 & VC4_HDMI_CEC_REC_WRD_CNT_MASK) >>
1094 VC4_HDMI_CEC_REC_WRD_CNT_SHIFT);
1095 for (i = 0; i < msg->len; i += 4) {
Maxime Ripard311e3052020-09-03 10:01:23 +02001096 u32 val = HDMI_READ(HDMI_CEC_RX_DATA_1 + i);
Hans Verkuil15b45112017-07-16 12:48:04 +02001097
1098 msg->msg[i] = val & 0xff;
1099 msg->msg[i + 1] = (val >> 8) & 0xff;
1100 msg->msg[i + 2] = (val >> 16) & 0xff;
1101 msg->msg[i + 3] = (val >> 24) & 0xff;
1102 }
1103}
1104
1105static irqreturn_t vc4_cec_irq_handler(int irq, void *priv)
1106{
Maxime Ripard66bf1c32020-09-03 10:01:18 +02001107 struct vc4_hdmi *vc4_hdmi = priv;
Maxime Ripard311e3052020-09-03 10:01:23 +02001108 u32 stat = HDMI_READ(HDMI_CEC_CPU_STATUS);
Hans Verkuil15b45112017-07-16 12:48:04 +02001109 u32 cntrl1, cntrl5;
1110
1111 if (!(stat & VC4_HDMI_CPU_CEC))
1112 return IRQ_NONE;
Maxime Ripard3408cc22020-09-03 10:01:14 +02001113 vc4_hdmi->cec_rx_msg.len = 0;
Maxime Ripard311e3052020-09-03 10:01:23 +02001114 cntrl1 = HDMI_READ(HDMI_CEC_CNTRL_1);
1115 cntrl5 = HDMI_READ(HDMI_CEC_CNTRL_5);
Maxime Ripard3408cc22020-09-03 10:01:14 +02001116 vc4_hdmi->cec_irq_was_rx = cntrl5 & VC4_HDMI_CEC_RX_CEC_INT;
1117 if (vc4_hdmi->cec_irq_was_rx) {
Maxime Ripard66bf1c32020-09-03 10:01:18 +02001118 vc4_cec_read_msg(vc4_hdmi, cntrl1);
Hans Verkuil15b45112017-07-16 12:48:04 +02001119 cntrl1 |= VC4_HDMI_CEC_CLEAR_RECEIVE_OFF;
Maxime Ripard311e3052020-09-03 10:01:23 +02001120 HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1);
Hans Verkuil15b45112017-07-16 12:48:04 +02001121 cntrl1 &= ~VC4_HDMI_CEC_CLEAR_RECEIVE_OFF;
1122 } else {
Maxime Ripard3408cc22020-09-03 10:01:14 +02001123 vc4_hdmi->cec_tx_ok = cntrl1 & VC4_HDMI_CEC_TX_STATUS_GOOD;
Hans Verkuil15b45112017-07-16 12:48:04 +02001124 cntrl1 &= ~VC4_HDMI_CEC_START_XMIT_BEGIN;
1125 }
Maxime Ripard311e3052020-09-03 10:01:23 +02001126 HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1);
1127 HDMI_WRITE(HDMI_CEC_CPU_CLEAR, VC4_HDMI_CPU_CEC);
Hans Verkuil15b45112017-07-16 12:48:04 +02001128
1129 return IRQ_WAKE_THREAD;
1130}
1131
1132static int vc4_hdmi_cec_adap_enable(struct cec_adapter *adap, bool enable)
1133{
Maxime Ripard66bf1c32020-09-03 10:01:18 +02001134 struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
Hans Verkuil15b45112017-07-16 12:48:04 +02001135 /* clock period in microseconds */
1136 const u32 usecs = 1000000 / CEC_CLOCK_FREQ;
Maxime Ripard311e3052020-09-03 10:01:23 +02001137 u32 val = HDMI_READ(HDMI_CEC_CNTRL_5);
Hans Verkuil15b45112017-07-16 12:48:04 +02001138
1139 val &= ~(VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET |
1140 VC4_HDMI_CEC_CNT_TO_4700_US_MASK |
1141 VC4_HDMI_CEC_CNT_TO_4500_US_MASK);
1142 val |= ((4700 / usecs) << VC4_HDMI_CEC_CNT_TO_4700_US_SHIFT) |
1143 ((4500 / usecs) << VC4_HDMI_CEC_CNT_TO_4500_US_SHIFT);
1144
1145 if (enable) {
Maxime Ripard311e3052020-09-03 10:01:23 +02001146 HDMI_WRITE(HDMI_CEC_CNTRL_5, val |
Hans Verkuil15b45112017-07-16 12:48:04 +02001147 VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET);
Maxime Ripard311e3052020-09-03 10:01:23 +02001148 HDMI_WRITE(HDMI_CEC_CNTRL_5, val);
1149 HDMI_WRITE(HDMI_CEC_CNTRL_2,
1150 ((1500 / usecs) << VC4_HDMI_CEC_CNT_TO_1500_US_SHIFT) |
1151 ((1300 / usecs) << VC4_HDMI_CEC_CNT_TO_1300_US_SHIFT) |
1152 ((800 / usecs) << VC4_HDMI_CEC_CNT_TO_800_US_SHIFT) |
1153 ((600 / usecs) << VC4_HDMI_CEC_CNT_TO_600_US_SHIFT) |
1154 ((400 / usecs) << VC4_HDMI_CEC_CNT_TO_400_US_SHIFT));
1155 HDMI_WRITE(HDMI_CEC_CNTRL_3,
1156 ((2750 / usecs) << VC4_HDMI_CEC_CNT_TO_2750_US_SHIFT) |
1157 ((2400 / usecs) << VC4_HDMI_CEC_CNT_TO_2400_US_SHIFT) |
1158 ((2050 / usecs) << VC4_HDMI_CEC_CNT_TO_2050_US_SHIFT) |
1159 ((1700 / usecs) << VC4_HDMI_CEC_CNT_TO_1700_US_SHIFT));
1160 HDMI_WRITE(HDMI_CEC_CNTRL_4,
1161 ((4300 / usecs) << VC4_HDMI_CEC_CNT_TO_4300_US_SHIFT) |
1162 ((3900 / usecs) << VC4_HDMI_CEC_CNT_TO_3900_US_SHIFT) |
1163 ((3600 / usecs) << VC4_HDMI_CEC_CNT_TO_3600_US_SHIFT) |
1164 ((3500 / usecs) << VC4_HDMI_CEC_CNT_TO_3500_US_SHIFT));
Hans Verkuil15b45112017-07-16 12:48:04 +02001165
Maxime Ripard311e3052020-09-03 10:01:23 +02001166 HDMI_WRITE(HDMI_CEC_CPU_MASK_CLEAR, VC4_HDMI_CPU_CEC);
Hans Verkuil15b45112017-07-16 12:48:04 +02001167 } else {
Maxime Ripard311e3052020-09-03 10:01:23 +02001168 HDMI_WRITE(HDMI_CEC_CPU_MASK_SET, VC4_HDMI_CPU_CEC);
1169 HDMI_WRITE(HDMI_CEC_CNTRL_5, val |
Hans Verkuil15b45112017-07-16 12:48:04 +02001170 VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET);
1171 }
1172 return 0;
1173}
1174
1175static int vc4_hdmi_cec_adap_log_addr(struct cec_adapter *adap, u8 log_addr)
1176{
Maxime Ripard66bf1c32020-09-03 10:01:18 +02001177 struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
Hans Verkuil15b45112017-07-16 12:48:04 +02001178
Maxime Ripard311e3052020-09-03 10:01:23 +02001179 HDMI_WRITE(HDMI_CEC_CNTRL_1,
1180 (HDMI_READ(HDMI_CEC_CNTRL_1) & ~VC4_HDMI_CEC_ADDR_MASK) |
Hans Verkuil15b45112017-07-16 12:48:04 +02001181 (log_addr & 0xf) << VC4_HDMI_CEC_ADDR_SHIFT);
1182 return 0;
1183}
1184
1185static int vc4_hdmi_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
1186 u32 signal_free_time, struct cec_msg *msg)
1187{
Maxime Ripard66bf1c32020-09-03 10:01:18 +02001188 struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
Hans Verkuil15b45112017-07-16 12:48:04 +02001189 u32 val;
1190 unsigned int i;
1191
1192 for (i = 0; i < msg->len; i += 4)
Maxime Ripard311e3052020-09-03 10:01:23 +02001193 HDMI_WRITE(HDMI_CEC_TX_DATA_1 + i,
Hans Verkuil15b45112017-07-16 12:48:04 +02001194 (msg->msg[i]) |
1195 (msg->msg[i + 1] << 8) |
1196 (msg->msg[i + 2] << 16) |
1197 (msg->msg[i + 3] << 24));
1198
Maxime Ripard311e3052020-09-03 10:01:23 +02001199 val = HDMI_READ(HDMI_CEC_CNTRL_1);
Hans Verkuil15b45112017-07-16 12:48:04 +02001200 val &= ~VC4_HDMI_CEC_START_XMIT_BEGIN;
Maxime Ripard311e3052020-09-03 10:01:23 +02001201 HDMI_WRITE(HDMI_CEC_CNTRL_1, val);
Hans Verkuil15b45112017-07-16 12:48:04 +02001202 val &= ~VC4_HDMI_CEC_MESSAGE_LENGTH_MASK;
1203 val |= (msg->len - 1) << VC4_HDMI_CEC_MESSAGE_LENGTH_SHIFT;
1204 val |= VC4_HDMI_CEC_START_XMIT_BEGIN;
1205
Maxime Ripard311e3052020-09-03 10:01:23 +02001206 HDMI_WRITE(HDMI_CEC_CNTRL_1, val);
Hans Verkuil15b45112017-07-16 12:48:04 +02001207 return 0;
1208}
1209
1210static const struct cec_adap_ops vc4_hdmi_cec_adap_ops = {
1211 .adap_enable = vc4_hdmi_cec_adap_enable,
1212 .adap_log_addr = vc4_hdmi_cec_adap_log_addr,
1213 .adap_transmit = vc4_hdmi_cec_adap_transmit,
1214};
Maxime Ripardc0791e02020-09-03 10:01:31 +02001215
1216static int vc4_hdmi_cec_init(struct vc4_hdmi *vc4_hdmi)
1217{
1218 struct cec_connector_info conn_info;
1219 struct platform_device *pdev = vc4_hdmi->pdev;
1220 u32 value;
1221 int ret;
1222
Maxime Ripard234f4212020-09-03 10:01:32 +02001223 if (!vc4_hdmi->variant->cec_available)
1224 return 0;
1225
Maxime Ripardc0791e02020-09-03 10:01:31 +02001226 vc4_hdmi->cec_adap = cec_allocate_adapter(&vc4_hdmi_cec_adap_ops,
1227 vc4_hdmi, "vc4",
1228 CEC_CAP_DEFAULTS |
1229 CEC_CAP_CONNECTOR_INFO, 1);
1230 ret = PTR_ERR_OR_ZERO(vc4_hdmi->cec_adap);
1231 if (ret < 0)
1232 return ret;
1233
1234 cec_fill_conn_info_from_drm(&conn_info, &vc4_hdmi->connector);
1235 cec_s_conn_info(vc4_hdmi->cec_adap, &conn_info);
1236
1237 HDMI_WRITE(HDMI_CEC_CPU_MASK_SET, 0xffffffff);
1238 value = HDMI_READ(HDMI_CEC_CNTRL_1);
1239 value &= ~VC4_HDMI_CEC_DIV_CLK_CNT_MASK;
1240 /*
1241 * Set the logical address to Unregistered and set the clock
1242 * divider: the hsm_clock rate and this divider setting will
1243 * give a 40 kHz CEC clock.
1244 */
1245 value |= VC4_HDMI_CEC_ADDR_MASK |
1246 (4091 << VC4_HDMI_CEC_DIV_CLK_CNT_SHIFT);
1247 HDMI_WRITE(HDMI_CEC_CNTRL_1, value);
1248 ret = devm_request_threaded_irq(&pdev->dev, platform_get_irq(pdev, 0),
1249 vc4_cec_irq_handler,
1250 vc4_cec_irq_handler_thread, 0,
1251 "vc4 hdmi cec", vc4_hdmi);
1252 if (ret)
1253 goto err_delete_cec_adap;
1254
1255 ret = cec_register_adapter(vc4_hdmi->cec_adap, &pdev->dev);
1256 if (ret < 0)
1257 goto err_delete_cec_adap;
1258
1259 return 0;
1260
1261err_delete_cec_adap:
1262 cec_delete_adapter(vc4_hdmi->cec_adap);
1263
1264 return ret;
1265}
1266
1267static void vc4_hdmi_cec_exit(struct vc4_hdmi *vc4_hdmi)
1268{
1269 cec_unregister_adapter(vc4_hdmi->cec_adap);
1270}
1271#else
1272static int vc4_hdmi_cec_init(struct vc4_hdmi *vc4_hdmi)
1273{
1274 return 0;
1275}
1276
1277static void vc4_hdmi_cec_exit(struct vc4_hdmi *vc4_hdmi) {};
1278
Hans Verkuil15b45112017-07-16 12:48:04 +02001279#endif
1280
Maxime Ripard311e3052020-09-03 10:01:23 +02001281static int vc4_hdmi_build_regset(struct vc4_hdmi *vc4_hdmi,
1282 struct debugfs_regset32 *regset,
1283 enum vc4_hdmi_regs reg)
1284{
1285 const struct vc4_hdmi_variant *variant = vc4_hdmi->variant;
1286 struct debugfs_reg32 *regs, *new_regs;
1287 unsigned int count = 0;
1288 unsigned int i;
1289
1290 regs = kcalloc(variant->num_registers, sizeof(*regs),
1291 GFP_KERNEL);
1292 if (!regs)
1293 return -ENOMEM;
1294
1295 for (i = 0; i < variant->num_registers; i++) {
1296 const struct vc4_hdmi_register *field = &variant->registers[i];
1297
1298 if (field->reg != reg)
1299 continue;
1300
1301 regs[count].name = field->name;
1302 regs[count].offset = field->offset;
1303 count++;
1304 }
1305
1306 new_regs = krealloc(regs, count * sizeof(*regs), GFP_KERNEL);
1307 if (!new_regs)
1308 return -ENOMEM;
1309
1310 regset->base = __vc4_hdmi_get_field_base(vc4_hdmi, reg);
1311 regset->regs = new_regs;
1312 regset->nregs = count;
1313
1314 return 0;
1315}
1316
Maxime Ripard33c773e2020-09-03 10:01:22 +02001317static int vc4_hdmi_init_resources(struct vc4_hdmi *vc4_hdmi)
1318{
1319 struct platform_device *pdev = vc4_hdmi->pdev;
1320 struct device *dev = &pdev->dev;
1321 int ret;
1322
1323 vc4_hdmi->hdmicore_regs = vc4_ioremap_regs(pdev, 0);
1324 if (IS_ERR(vc4_hdmi->hdmicore_regs))
1325 return PTR_ERR(vc4_hdmi->hdmicore_regs);
1326
1327 vc4_hdmi->hd_regs = vc4_ioremap_regs(pdev, 1);
1328 if (IS_ERR(vc4_hdmi->hd_regs))
1329 return PTR_ERR(vc4_hdmi->hd_regs);
1330
Maxime Ripard311e3052020-09-03 10:01:23 +02001331 ret = vc4_hdmi_build_regset(vc4_hdmi, &vc4_hdmi->hd_regset, VC4_HD);
1332 if (ret)
1333 return ret;
Maxime Ripard33c773e2020-09-03 10:01:22 +02001334
Maxime Ripard311e3052020-09-03 10:01:23 +02001335 ret = vc4_hdmi_build_regset(vc4_hdmi, &vc4_hdmi->hdmi_regset, VC4_HDMI);
1336 if (ret)
1337 return ret;
Maxime Ripard33c773e2020-09-03 10:01:22 +02001338
1339 vc4_hdmi->pixel_clock = devm_clk_get(dev, "pixel");
1340 if (IS_ERR(vc4_hdmi->pixel_clock)) {
1341 ret = PTR_ERR(vc4_hdmi->pixel_clock);
1342 if (ret != -EPROBE_DEFER)
1343 DRM_ERROR("Failed to get pixel clock\n");
1344 return ret;
1345 }
1346
1347 vc4_hdmi->hsm_clock = devm_clk_get(dev, "hdmi");
1348 if (IS_ERR(vc4_hdmi->hsm_clock)) {
1349 DRM_ERROR("Failed to get HDMI state machine clock\n");
1350 return PTR_ERR(vc4_hdmi->hsm_clock);
1351 }
Dave Stevenson632ee3a2020-09-03 10:01:40 +02001352 vc4_hdmi->audio_clock = vc4_hdmi->hsm_clock;
Maxime Ripard33c773e2020-09-03 10:01:22 +02001353
1354 return 0;
1355}
1356
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001357static int vc4_hdmi_bind(struct device *dev, struct device *master, void *data)
1358{
Maxime Ripard33c773e2020-09-03 10:01:22 +02001359 const struct vc4_hdmi_variant *variant = of_device_get_match_data(dev);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001360 struct platform_device *pdev = to_platform_device(dev);
1361 struct drm_device *drm = dev_get_drvdata(master);
Maxime Ripard3408cc22020-09-03 10:01:14 +02001362 struct vc4_hdmi *vc4_hdmi;
Maxime Ripardc98c85b2020-09-03 10:01:12 +02001363 struct drm_encoder *encoder;
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001364 struct device_node *ddc_node;
1365 u32 value;
1366 int ret;
1367
Maxime Ripard3408cc22020-09-03 10:01:14 +02001368 vc4_hdmi = devm_kzalloc(dev, sizeof(*vc4_hdmi), GFP_KERNEL);
1369 if (!vc4_hdmi)
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001370 return -ENOMEM;
1371
Maxime Ripard47c167b2020-09-03 10:01:19 +02001372 dev_set_drvdata(dev, vc4_hdmi);
Maxime Ripard3408cc22020-09-03 10:01:14 +02001373 encoder = &vc4_hdmi->encoder.base.base;
Maxime Ripard7d732992020-09-03 10:01:29 +02001374 vc4_hdmi->encoder.base.type = variant->encoder_type;
Maxime Ripard3408cc22020-09-03 10:01:14 +02001375 vc4_hdmi->pdev = pdev;
Maxime Ripard33c773e2020-09-03 10:01:22 +02001376 vc4_hdmi->variant = variant;
Maxime Ripardc98c85b2020-09-03 10:01:12 +02001377
Maxime Ripard33c773e2020-09-03 10:01:22 +02001378 ret = variant->init_resources(vc4_hdmi);
1379 if (ret)
James Hilliard8f6f5e02020-05-24 19:28:59 -06001380 return ret;
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001381
Peter Chen027a6972016-07-05 10:04:54 +08001382 ddc_node = of_parse_phandle(dev->of_node, "ddc", 0);
1383 if (!ddc_node) {
1384 DRM_ERROR("Failed to find ddc node in device tree\n");
1385 return -ENODEV;
1386 }
1387
Maxime Ripard3408cc22020-09-03 10:01:14 +02001388 vc4_hdmi->ddc = of_find_i2c_adapter_by_node(ddc_node);
Peter Chen027a6972016-07-05 10:04:54 +08001389 of_node_put(ddc_node);
Maxime Ripard3408cc22020-09-03 10:01:14 +02001390 if (!vc4_hdmi->ddc) {
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001391 DRM_DEBUG("Failed to get ddc i2c adapter by node\n");
1392 return -EPROBE_DEFER;
1393 }
1394
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001395 /* Only use the GPIO HPD pin if present in the DT, otherwise
1396 * we'll use the HDMI core's register.
1397 */
1398 if (of_find_property(dev->of_node, "hpd-gpios", &value)) {
Eric Anholt0b06e0a2016-02-29 17:53:01 -08001399 enum of_gpio_flags hpd_gpio_flags;
1400
Maxime Ripard3408cc22020-09-03 10:01:14 +02001401 vc4_hdmi->hpd_gpio = of_get_named_gpio_flags(dev->of_node,
1402 "hpd-gpios", 0,
1403 &hpd_gpio_flags);
1404 if (vc4_hdmi->hpd_gpio < 0) {
1405 ret = vc4_hdmi->hpd_gpio;
Hans Verkuil10ee2752017-07-16 12:48:03 +02001406 goto err_unprepare_hsm;
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001407 }
Eric Anholt0b06e0a2016-02-29 17:53:01 -08001408
Maxime Ripard3408cc22020-09-03 10:01:14 +02001409 vc4_hdmi->hpd_active_low = hpd_gpio_flags & OF_GPIO_ACTIVE_LOW;
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001410 }
1411
Boris Brezillon4f6e3d62017-04-11 18:39:25 +02001412 pm_runtime_enable(dev);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001413
Maxime Ripardc98c85b2020-09-03 10:01:12 +02001414 drm_simple_encoder_init(drm, encoder, DRM_MODE_ENCODER_TMDS);
1415 drm_encoder_helper_add(encoder, &vc4_hdmi_encoder_helper_funcs);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001416
Maxime Ripard3408cc22020-09-03 10:01:14 +02001417 ret = vc4_hdmi_connector_init(drm, vc4_hdmi);
Maxime Ripardc98c85b2020-09-03 10:01:12 +02001418 if (ret)
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001419 goto err_destroy_encoder;
Maxime Ripardc98c85b2020-09-03 10:01:12 +02001420
Maxime Ripardc0791e02020-09-03 10:01:31 +02001421 ret = vc4_hdmi_cec_init(vc4_hdmi);
Hans Verkuil15b45112017-07-16 12:48:04 +02001422 if (ret)
Maxime Ripardc0791e02020-09-03 10:01:31 +02001423 goto err_destroy_conn;
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001424
Maxime Ripard3408cc22020-09-03 10:01:14 +02001425 ret = vc4_hdmi_audio_init(vc4_hdmi);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001426 if (ret)
Maxime Ripardc0791e02020-09-03 10:01:31 +02001427 goto err_free_cec;
Eric Anholtbb7d7852017-02-27 12:28:02 -08001428
Maxime Ripardb2405c92020-09-03 10:01:30 +02001429 vc4_debugfs_add_file(drm, variant->debugfs_name,
1430 vc4_hdmi_debugfs_regs,
1431 vc4_hdmi);
Eric Anholtc9be8042019-04-01 11:35:58 -07001432
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001433 return 0;
1434
Maxime Ripardc0791e02020-09-03 10:01:31 +02001435err_free_cec:
1436 vc4_hdmi_cec_exit(vc4_hdmi);
Hans Verkuil15b45112017-07-16 12:48:04 +02001437err_destroy_conn:
Maxime Ripard0532e5e2020-09-03 10:01:21 +02001438 vc4_hdmi_connector_destroy(&vc4_hdmi->connector);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001439err_destroy_encoder:
Maxime Ripardc98c85b2020-09-03 10:01:12 +02001440 drm_encoder_cleanup(encoder);
Hans Verkuil10ee2752017-07-16 12:48:03 +02001441err_unprepare_hsm:
Boris Brezillon4f6e3d62017-04-11 18:39:25 +02001442 pm_runtime_disable(dev);
Maxime Ripard3408cc22020-09-03 10:01:14 +02001443 put_device(&vc4_hdmi->ddc->dev);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001444
1445 return ret;
1446}
1447
1448static void vc4_hdmi_unbind(struct device *dev, struct device *master,
1449 void *data)
1450{
Maxime Ripard47c167b2020-09-03 10:01:19 +02001451 struct vc4_hdmi *vc4_hdmi;
1452
1453 /*
1454 * ASoC makes it a bit hard to retrieve a pointer to the
1455 * vc4_hdmi structure. Registering the card will overwrite our
1456 * device drvdata with a pointer to the snd_soc_card structure,
1457 * which can then be used to retrieve whatever drvdata we want
1458 * to associate.
1459 *
1460 * However, that doesn't fly in the case where we wouldn't
1461 * register an ASoC card (because of an old DT that is missing
1462 * the dmas properties for example), then the card isn't
1463 * registered and the device drvdata wouldn't be set.
1464 *
1465 * We can deal with both cases by making sure a snd_soc_card
1466 * pointer and a vc4_hdmi structure are pointing to the same
1467 * memory address, so we can treat them indistinctly without any
1468 * issue.
1469 */
1470 BUILD_BUG_ON(offsetof(struct vc4_hdmi_audio, card) != 0);
1471 BUILD_BUG_ON(offsetof(struct vc4_hdmi, audio) != 0);
1472 vc4_hdmi = dev_get_drvdata(dev);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001473
Maxime Ripard311e3052020-09-03 10:01:23 +02001474 kfree(vc4_hdmi->hdmi_regset.regs);
1475 kfree(vc4_hdmi->hd_regset.regs);
1476
Maxime Ripardc0791e02020-09-03 10:01:31 +02001477 vc4_hdmi_cec_exit(vc4_hdmi);
Maxime Ripard0532e5e2020-09-03 10:01:21 +02001478 vc4_hdmi_connector_destroy(&vc4_hdmi->connector);
Maxime Ripard3408cc22020-09-03 10:01:14 +02001479 drm_encoder_cleanup(&vc4_hdmi->encoder.base.base);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001480
Boris Brezillon4f6e3d62017-04-11 18:39:25 +02001481 pm_runtime_disable(dev);
1482
Maxime Ripard3408cc22020-09-03 10:01:14 +02001483 put_device(&vc4_hdmi->ddc->dev);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001484}
1485
1486static const struct component_ops vc4_hdmi_ops = {
1487 .bind = vc4_hdmi_bind,
1488 .unbind = vc4_hdmi_unbind,
1489};
1490
1491static int vc4_hdmi_dev_probe(struct platform_device *pdev)
1492{
1493 return component_add(&pdev->dev, &vc4_hdmi_ops);
1494}
1495
1496static int vc4_hdmi_dev_remove(struct platform_device *pdev)
1497{
1498 component_del(&pdev->dev, &vc4_hdmi_ops);
1499 return 0;
1500}
1501
Maxime Ripard33c773e2020-09-03 10:01:22 +02001502static const struct vc4_hdmi_variant bcm2835_variant = {
Maxime Ripard7d732992020-09-03 10:01:29 +02001503 .encoder_type = VC4_ENCODER_TYPE_HDMI0,
Maxime Ripardb2405c92020-09-03 10:01:30 +02001504 .debugfs_name = "hdmi_regs",
Maxime Ripard9be43a52020-09-03 10:01:41 +02001505 .card_name = "vc4-hdmi",
Maxime Ripardcd4cb492020-09-03 10:01:35 +02001506 .max_pixel_clock = 162000000,
Maxime Ripard234f4212020-09-03 10:01:32 +02001507 .cec_available = true,
Maxime Ripard311e3052020-09-03 10:01:23 +02001508 .registers = vc4_hdmi_fields,
1509 .num_registers = ARRAY_SIZE(vc4_hdmi_fields),
1510
Maxime Ripard33c773e2020-09-03 10:01:22 +02001511 .init_resources = vc4_hdmi_init_resources,
Maxime Ripard89f31a22020-09-03 10:01:27 +02001512 .csc_setup = vc4_hdmi_csc_setup,
Maxime Ripard9045e912020-09-03 10:01:24 +02001513 .reset = vc4_hdmi_reset,
Maxime Ripard904f6682020-09-03 10:01:28 +02001514 .set_timings = vc4_hdmi_set_timings,
Maxime Ripardc457b8a2020-09-03 10:01:25 +02001515 .phy_init = vc4_hdmi_phy_init,
1516 .phy_disable = vc4_hdmi_phy_disable,
Maxime Ripard647b9652020-09-03 10:01:26 +02001517 .phy_rng_enable = vc4_hdmi_phy_rng_enable,
1518 .phy_rng_disable = vc4_hdmi_phy_rng_disable,
Dave Stevenson632ee3a2020-09-03 10:01:40 +02001519 .channel_map = vc4_hdmi_channel_map,
Maxime Ripard33c773e2020-09-03 10:01:22 +02001520};
1521
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001522static const struct of_device_id vc4_hdmi_dt_match[] = {
Maxime Ripard33c773e2020-09-03 10:01:22 +02001523 { .compatible = "brcm,bcm2835-hdmi", .data = &bcm2835_variant },
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001524 {}
1525};
1526
1527struct platform_driver vc4_hdmi_driver = {
1528 .probe = vc4_hdmi_dev_probe,
1529 .remove = vc4_hdmi_dev_remove,
1530 .driver = {
1531 .name = "vc4_hdmi",
1532 .of_match_table = vc4_hdmi_dt_match,
1533 },
1534};