blob: e0dc823c622a345c1cb89d4e5ea31dc9a340cc71 [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"
Eric Anholtc8b75bc2015-03-02 13:01:12 -080053#include "vc4_regs.h"
54
Hans Verkuil15b45112017-07-16 12:48:04 +020055#define HSM_CLOCK_FREQ 163682864
56#define CEC_CLOCK_FREQ 40000
57#define CEC_CLOCK_DIV (HSM_CLOCK_FREQ / CEC_CLOCK_FREQ)
58
Eric Anholt30517192019-02-20 13:03:38 -080059static const struct debugfs_reg32 hdmi_regs[] = {
60 VC4_REG32(VC4_HDMI_CORE_REV),
61 VC4_REG32(VC4_HDMI_SW_RESET_CONTROL),
62 VC4_REG32(VC4_HDMI_HOTPLUG_INT),
63 VC4_REG32(VC4_HDMI_HOTPLUG),
64 VC4_REG32(VC4_HDMI_MAI_CHANNEL_MAP),
65 VC4_REG32(VC4_HDMI_MAI_CONFIG),
66 VC4_REG32(VC4_HDMI_MAI_FORMAT),
67 VC4_REG32(VC4_HDMI_AUDIO_PACKET_CONFIG),
68 VC4_REG32(VC4_HDMI_RAM_PACKET_CONFIG),
69 VC4_REG32(VC4_HDMI_HORZA),
70 VC4_REG32(VC4_HDMI_HORZB),
71 VC4_REG32(VC4_HDMI_FIFO_CTL),
72 VC4_REG32(VC4_HDMI_SCHEDULER_CONTROL),
73 VC4_REG32(VC4_HDMI_VERTA0),
74 VC4_REG32(VC4_HDMI_VERTA1),
75 VC4_REG32(VC4_HDMI_VERTB0),
76 VC4_REG32(VC4_HDMI_VERTB1),
77 VC4_REG32(VC4_HDMI_TX_PHY_RESET_CTL),
78 VC4_REG32(VC4_HDMI_TX_PHY_CTL0),
Hans Verkuil15b45112017-07-16 12:48:04 +020079
Eric Anholt30517192019-02-20 13:03:38 -080080 VC4_REG32(VC4_HDMI_CEC_CNTRL_1),
81 VC4_REG32(VC4_HDMI_CEC_CNTRL_2),
82 VC4_REG32(VC4_HDMI_CEC_CNTRL_3),
83 VC4_REG32(VC4_HDMI_CEC_CNTRL_4),
84 VC4_REG32(VC4_HDMI_CEC_CNTRL_5),
85 VC4_REG32(VC4_HDMI_CPU_STATUS),
86 VC4_REG32(VC4_HDMI_CPU_MASK_STATUS),
Hans Verkuil15b45112017-07-16 12:48:04 +020087
Eric Anholt30517192019-02-20 13:03:38 -080088 VC4_REG32(VC4_HDMI_CEC_RX_DATA_1),
89 VC4_REG32(VC4_HDMI_CEC_RX_DATA_2),
90 VC4_REG32(VC4_HDMI_CEC_RX_DATA_3),
91 VC4_REG32(VC4_HDMI_CEC_RX_DATA_4),
92 VC4_REG32(VC4_HDMI_CEC_TX_DATA_1),
93 VC4_REG32(VC4_HDMI_CEC_TX_DATA_2),
94 VC4_REG32(VC4_HDMI_CEC_TX_DATA_3),
95 VC4_REG32(VC4_HDMI_CEC_TX_DATA_4),
Eric Anholtc8b75bc2015-03-02 13:01:12 -080096};
97
Eric Anholt30517192019-02-20 13:03:38 -080098static const struct debugfs_reg32 hd_regs[] = {
99 VC4_REG32(VC4_HD_M_CTL),
100 VC4_REG32(VC4_HD_MAI_CTL),
101 VC4_REG32(VC4_HD_MAI_THR),
102 VC4_REG32(VC4_HD_MAI_FMT),
103 VC4_REG32(VC4_HD_MAI_SMP),
104 VC4_REG32(VC4_HD_VID_CTL),
105 VC4_REG32(VC4_HD_CSC_CTL),
106 VC4_REG32(VC4_HD_FRAME_COUNT),
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800107};
108
Eric Anholtc9be8042019-04-01 11:35:58 -0700109static int vc4_hdmi_debugfs_regs(struct seq_file *m, void *unused)
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800110{
111 struct drm_info_node *node = (struct drm_info_node *)m->private;
Maxime Ripard3408cc22020-09-03 10:01:14 +0200112 struct vc4_hdmi *vc4_hdmi = node->info_ent->data;
Eric Anholt30517192019-02-20 13:03:38 -0800113 struct drm_printer p = drm_seq_file_printer(m);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800114
Maxime Ripard3408cc22020-09-03 10:01:14 +0200115 drm_print_regset32(&p, &vc4_hdmi->hdmi_regset);
116 drm_print_regset32(&p, &vc4_hdmi->hd_regset);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800117
118 return 0;
119}
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800120
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800121static enum drm_connector_status
122vc4_hdmi_connector_detect(struct drm_connector *connector, bool force)
123{
Maxime Ripard5dfbcae2020-09-03 10:01:17 +0200124 struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800125
Maxime Ripardb10db9a2020-09-03 10:01:16 +0200126 if (vc4_hdmi->hpd_gpio) {
127 if (gpio_get_value_cansleep(vc4_hdmi->hpd_gpio) ^
128 vc4_hdmi->hpd_active_low)
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800129 return connector_status_connected;
Maxime Ripardb10db9a2020-09-03 10:01:16 +0200130 cec_phys_addr_invalidate(vc4_hdmi->cec_adap);
Hans Verkuil15b45112017-07-16 12:48:04 +0200131 return connector_status_disconnected;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800132 }
133
Maxime Ripardb10db9a2020-09-03 10:01:16 +0200134 if (drm_probe_ddc(vc4_hdmi->ddc))
Eric Anholt9d44abb2016-09-14 19:21:29 +0100135 return connector_status_connected;
136
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800137 if (HDMI_READ(VC4_HDMI_HOTPLUG) & VC4_HDMI_HOTPLUG_CONNECTED)
138 return connector_status_connected;
Maxime Ripardb10db9a2020-09-03 10:01:16 +0200139 cec_phys_addr_invalidate(vc4_hdmi->cec_adap);
Hans Verkuil15b45112017-07-16 12:48:04 +0200140 return connector_status_disconnected;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800141}
142
143static void vc4_hdmi_connector_destroy(struct drm_connector *connector)
144{
145 drm_connector_unregister(connector);
146 drm_connector_cleanup(connector);
147}
148
149static int vc4_hdmi_connector_get_modes(struct drm_connector *connector)
150{
Maxime Ripard5dfbcae2020-09-03 10:01:17 +0200151 struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector);
152 struct vc4_hdmi_encoder *vc4_encoder = &vc4_hdmi->encoder;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800153 int ret = 0;
154 struct edid *edid;
155
Maxime Ripard5dfbcae2020-09-03 10:01:17 +0200156 edid = drm_get_edid(connector, vc4_hdmi->ddc);
157 cec_s_phys_addr_from_edid(vc4_hdmi->cec_adap, edid);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800158 if (!edid)
159 return -ENODEV;
160
161 vc4_encoder->hdmi_monitor = drm_detect_hdmi_monitor(edid);
Eric Anholt21317b32016-09-29 15:34:43 -0700162
Daniel Vetterc555f022018-07-09 10:40:06 +0200163 drm_connector_update_edid_property(connector, edid);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800164 ret = drm_add_edid_modes(connector, edid);
Eric Anholt5afe0e62017-08-08 13:56:05 -0700165 kfree(edid);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800166
167 return ret;
168}
169
Maxime Ripard90b2df52019-06-19 12:17:53 +0200170static void vc4_hdmi_connector_reset(struct drm_connector *connector)
171{
172 drm_atomic_helper_connector_reset(connector);
173 drm_atomic_helper_connector_tv_reset(connector);
174}
175
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800176static const struct drm_connector_funcs vc4_hdmi_connector_funcs = {
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800177 .detect = vc4_hdmi_connector_detect,
Eric Anholt682e62c2016-09-28 17:30:25 -0700178 .fill_modes = drm_helper_probe_single_connector_modes,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800179 .destroy = vc4_hdmi_connector_destroy,
Maxime Ripard90b2df52019-06-19 12:17:53 +0200180 .reset = vc4_hdmi_connector_reset,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800181 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
182 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
183};
184
185static const struct drm_connector_helper_funcs vc4_hdmi_connector_helper_funcs = {
186 .get_modes = vc4_hdmi_connector_get_modes,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800187};
188
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200189static int vc4_hdmi_connector_init(struct drm_device *dev,
Maxime Ripardb052e702020-09-03 10:01:13 +0200190 struct vc4_hdmi *vc4_hdmi)
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800191{
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200192 struct vc4_hdmi_connector *hdmi_connector = &vc4_hdmi->connector;
193 struct drm_connector *connector = &hdmi_connector->base;
194 struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
Boris Brezillondb999532018-12-06 15:24:39 +0100195 int ret;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800196
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800197 hdmi_connector->encoder = encoder;
198
Andrzej Pietrasiewicz04a880f2020-01-02 14:22:58 +0100199 drm_connector_init_with_ddc(dev, connector,
200 &vc4_hdmi_connector_funcs,
201 DRM_MODE_CONNECTOR_HDMIA,
Maxime Ripardb052e702020-09-03 10:01:13 +0200202 vc4_hdmi->ddc);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800203 drm_connector_helper_add(connector, &vc4_hdmi_connector_helper_funcs);
204
Boris Brezillondb999532018-12-06 15:24:39 +0100205 /* Create and attach TV margin props to this connector. */
206 ret = drm_mode_create_tv_margin_properties(dev);
207 if (ret)
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200208 return ret;
Boris Brezillondb999532018-12-06 15:24:39 +0100209
210 drm_connector_attach_tv_margin_properties(connector);
211
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800212 connector->polled = (DRM_CONNECTOR_POLL_CONNECT |
213 DRM_CONNECTOR_POLL_DISCONNECT);
214
Mario Kleineracc1be12016-07-19 20:58:58 +0200215 connector->interlace_allowed = 1;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800216 connector->doublescan_allowed = 0;
217
Daniel Vettercde4c442018-07-09 10:40:07 +0200218 drm_connector_attach_encoder(connector, encoder);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800219
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200220 return 0;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800221}
222
Eric Anholt21317b32016-09-29 15:34:43 -0700223static int vc4_hdmi_stop_packet(struct drm_encoder *encoder,
224 enum hdmi_infoframe_type type)
225{
Maxime Ripard5dfbcae2020-09-03 10:01:17 +0200226 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
Eric Anholt21317b32016-09-29 15:34:43 -0700227 u32 packet_id = type - 0x80;
228
229 HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG,
230 HDMI_READ(VC4_HDMI_RAM_PACKET_CONFIG) & ~BIT(packet_id));
231
232 return wait_for(!(HDMI_READ(VC4_HDMI_RAM_PACKET_STATUS) &
233 BIT(packet_id)), 100);
234}
235
236static void vc4_hdmi_write_infoframe(struct drm_encoder *encoder,
237 union hdmi_infoframe *frame)
238{
Maxime Ripard5dfbcae2020-09-03 10:01:17 +0200239 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
Eric Anholt21317b32016-09-29 15:34:43 -0700240 u32 packet_id = frame->any.type - 0x80;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800241 u32 packet_reg = VC4_HDMI_RAM_PACKET(packet_id);
Eric Anholt21317b32016-09-29 15:34:43 -0700242 uint8_t buffer[VC4_HDMI_PACKET_STRIDE];
243 ssize_t len, i;
244 int ret;
245
246 WARN_ONCE(!(HDMI_READ(VC4_HDMI_RAM_PACKET_CONFIG) &
247 VC4_HDMI_RAM_PACKET_ENABLE),
248 "Packet RAM has to be on to store the packet.");
249
250 len = hdmi_infoframe_pack(frame, buffer, sizeof(buffer));
251 if (len < 0)
252 return;
253
254 ret = vc4_hdmi_stop_packet(encoder, frame->any.type);
255 if (ret) {
256 DRM_ERROR("Failed to wait for infoframe to go idle: %d\n", ret);
257 return;
258 }
259
260 for (i = 0; i < len; i += 7) {
261 HDMI_WRITE(packet_reg,
262 buffer[i + 0] << 0 |
263 buffer[i + 1] << 8 |
264 buffer[i + 2] << 16);
265 packet_reg += 4;
266
267 HDMI_WRITE(packet_reg,
268 buffer[i + 3] << 0 |
269 buffer[i + 4] << 8 |
270 buffer[i + 5] << 16 |
271 buffer[i + 6] << 24);
272 packet_reg += 4;
273 }
274
275 HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG,
276 HDMI_READ(VC4_HDMI_RAM_PACKET_CONFIG) | BIT(packet_id));
277 ret = wait_for((HDMI_READ(VC4_HDMI_RAM_PACKET_STATUS) &
278 BIT(packet_id)), 100);
279 if (ret)
280 DRM_ERROR("Failed to wait for infoframe to start: %d\n", ret);
281}
282
283static void vc4_hdmi_set_avi_infoframe(struct drm_encoder *encoder)
284{
Maxime Ripard5dfbcae2020-09-03 10:01:17 +0200285 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
Eric Anholt21317b32016-09-29 15:34:43 -0700286 struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
Maxime Ripard3408cc22020-09-03 10:01:14 +0200287 struct drm_connector *connector = &vc4_hdmi->connector.base;
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200288 struct drm_connector_state *cstate = connector->state;
Eric Anholt21317b32016-09-29 15:34:43 -0700289 struct drm_crtc *crtc = encoder->crtc;
290 const struct drm_display_mode *mode = &crtc->state->adjusted_mode;
291 union hdmi_infoframe frame;
292 int ret;
293
Ville Syrjälä13d0add2019-01-08 19:28:25 +0200294 ret = drm_hdmi_avi_infoframe_from_display_mode(&frame.avi,
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200295 connector, mode);
Eric Anholt21317b32016-09-29 15:34:43 -0700296 if (ret < 0) {
297 DRM_ERROR("couldn't fill AVI infoframe\n");
298 return;
299 }
300
Ville Syrjälä13d0add2019-01-08 19:28:25 +0200301 drm_hdmi_avi_infoframe_quant_range(&frame.avi,
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200302 connector, mode,
Ville Syrjäläa2ce26f2017-01-11 14:57:23 +0200303 vc4_encoder->limited_rgb_range ?
304 HDMI_QUANTIZATION_RANGE_LIMITED :
Ville Syrjälä1581b2d2019-01-08 19:28:28 +0200305 HDMI_QUANTIZATION_RANGE_FULL);
Eric Anholt21317b32016-09-29 15:34:43 -0700306
Ville Syrjäläcb876372019-10-08 19:48:14 +0300307 drm_hdmi_avi_infoframe_bars(&frame.avi, cstate);
Boris Brezillondb999532018-12-06 15:24:39 +0100308
Eric Anholt21317b32016-09-29 15:34:43 -0700309 vc4_hdmi_write_infoframe(encoder, &frame);
310}
311
312static void vc4_hdmi_set_spd_infoframe(struct drm_encoder *encoder)
313{
314 union hdmi_infoframe frame;
315 int ret;
316
317 ret = hdmi_spd_infoframe_init(&frame.spd, "Broadcom", "Videocore");
318 if (ret < 0) {
319 DRM_ERROR("couldn't fill SPD infoframe\n");
320 return;
321 }
322
323 frame.spd.sdi = HDMI_SPD_SDI_PC;
324
325 vc4_hdmi_write_infoframe(encoder, &frame);
326}
327
Eric Anholtbb7d7852017-02-27 12:28:02 -0800328static void vc4_hdmi_set_audio_infoframe(struct drm_encoder *encoder)
329{
Maxime Ripard5dfbcae2020-09-03 10:01:17 +0200330 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800331 union hdmi_infoframe frame;
332 int ret;
333
334 ret = hdmi_audio_infoframe_init(&frame.audio);
335
336 frame.audio.coding_type = HDMI_AUDIO_CODING_TYPE_STREAM;
337 frame.audio.sample_frequency = HDMI_AUDIO_SAMPLE_FREQUENCY_STREAM;
338 frame.audio.sample_size = HDMI_AUDIO_SAMPLE_SIZE_STREAM;
Maxime Ripard3408cc22020-09-03 10:01:14 +0200339 frame.audio.channels = vc4_hdmi->audio.channels;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800340
341 vc4_hdmi_write_infoframe(encoder, &frame);
342}
343
Eric Anholt21317b32016-09-29 15:34:43 -0700344static void vc4_hdmi_set_infoframes(struct drm_encoder *encoder)
345{
346 vc4_hdmi_set_avi_infoframe(encoder);
347 vc4_hdmi_set_spd_infoframe(encoder);
348}
349
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200350static void vc4_hdmi_encoder_disable(struct drm_encoder *encoder)
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800351{
Maxime Ripard5dfbcae2020-09-03 10:01:17 +0200352 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200353 int ret;
354
355 HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG, 0);
356
357 HDMI_WRITE(VC4_HDMI_TX_PHY_RESET_CTL, 0xf << 16);
358 HD_WRITE(VC4_HD_VID_CTL,
359 HD_READ(VC4_HD_VID_CTL) & ~VC4_HD_VID_CTL_ENABLE);
360
Maxime Ripard3408cc22020-09-03 10:01:14 +0200361 clk_disable_unprepare(vc4_hdmi->pixel_clock);
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200362
Maxime Ripard3408cc22020-09-03 10:01:14 +0200363 ret = pm_runtime_put(&vc4_hdmi->pdev->dev);
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200364 if (ret < 0)
365 DRM_ERROR("Failed to release power domain: %d\n", ret);
366}
367
368static void vc4_hdmi_encoder_enable(struct drm_encoder *encoder)
369{
370 struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
Maxime Ripard5dfbcae2020-09-03 10:01:17 +0200371 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
372 struct vc4_hdmi_encoder *vc4_encoder = &vc4_hdmi->encoder;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800373 bool debug_dump_regs = false;
374 bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
375 bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
Eric Anholt682e62c2016-09-28 17:30:25 -0700376 bool interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE;
Eric Anholtdfccd932016-09-29 15:34:44 -0700377 u32 pixel_rep = (mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1;
Eric Anholt682e62c2016-09-28 17:30:25 -0700378 u32 verta = (VC4_SET_FIELD(mode->crtc_vsync_end - mode->crtc_vsync_start,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800379 VC4_HDMI_VERTA_VSP) |
Eric Anholt682e62c2016-09-28 17:30:25 -0700380 VC4_SET_FIELD(mode->crtc_vsync_start - mode->crtc_vdisplay,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800381 VC4_HDMI_VERTA_VFP) |
Eric Anholt682e62c2016-09-28 17:30:25 -0700382 VC4_SET_FIELD(mode->crtc_vdisplay, VC4_HDMI_VERTA_VAL));
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800383 u32 vertb = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) |
Eric Anholt682e62c2016-09-28 17:30:25 -0700384 VC4_SET_FIELD(mode->crtc_vtotal - mode->crtc_vsync_end,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800385 VC4_HDMI_VERTB_VBP));
Eric Anholt682e62c2016-09-28 17:30:25 -0700386 u32 vertb_even = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) |
387 VC4_SET_FIELD(mode->crtc_vtotal -
388 mode->crtc_vsync_end -
389 interlaced,
390 VC4_HDMI_VERTB_VBP));
Eric Anholt6e1cbba2016-09-16 10:59:45 +0100391 u32 csc_ctl;
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200392 int ret;
393
Maxime Ripard3408cc22020-09-03 10:01:14 +0200394 ret = pm_runtime_get_sync(&vc4_hdmi->pdev->dev);
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200395 if (ret < 0) {
396 DRM_ERROR("Failed to retain power domain: %d\n", ret);
397 return;
398 }
399
Maxime Ripard3408cc22020-09-03 10:01:14 +0200400 ret = clk_set_rate(vc4_hdmi->pixel_clock,
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200401 mode->clock * 1000 *
402 ((mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1));
403 if (ret) {
404 DRM_ERROR("Failed to set pixel clock rate: %d\n", ret);
405 return;
406 }
407
Maxime Ripard3408cc22020-09-03 10:01:14 +0200408 ret = clk_prepare_enable(vc4_hdmi->pixel_clock);
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200409 if (ret) {
410 DRM_ERROR("Failed to turn on pixel clock: %d\n", ret);
411 return;
412 }
413
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200414 HDMI_WRITE(VC4_HDMI_SW_RESET_CONTROL,
415 VC4_HDMI_SW_RESET_HDMI |
416 VC4_HDMI_SW_RESET_FORMAT_DETECT);
417
418 HDMI_WRITE(VC4_HDMI_SW_RESET_CONTROL, 0);
419
420 /* PHY should be in reset, like
421 * vc4_hdmi_encoder_disable() does.
422 */
423 HDMI_WRITE(VC4_HDMI_TX_PHY_RESET_CTL, 0xf << 16);
424
425 HDMI_WRITE(VC4_HDMI_TX_PHY_RESET_CTL, 0);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800426
427 if (debug_dump_regs) {
Maxime Ripard3408cc22020-09-03 10:01:14 +0200428 struct drm_printer p = drm_info_printer(&vc4_hdmi->pdev->dev);
Eric Anholt30517192019-02-20 13:03:38 -0800429
Maxime Ripard3408cc22020-09-03 10:01:14 +0200430 dev_info(&vc4_hdmi->pdev->dev, "HDMI regs before:\n");
431 drm_print_regset32(&p, &vc4_hdmi->hdmi_regset);
432 drm_print_regset32(&p, &vc4_hdmi->hd_regset);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800433 }
434
435 HD_WRITE(VC4_HD_VID_CTL, 0);
436
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800437 HDMI_WRITE(VC4_HDMI_SCHEDULER_CONTROL,
438 HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) |
439 VC4_HDMI_SCHEDULER_CONTROL_MANUAL_FORMAT |
440 VC4_HDMI_SCHEDULER_CONTROL_IGNORE_VSYNC_PREDICTS);
441
442 HDMI_WRITE(VC4_HDMI_HORZA,
443 (vsync_pos ? VC4_HDMI_HORZA_VPOS : 0) |
444 (hsync_pos ? VC4_HDMI_HORZA_HPOS : 0) |
Eric Anholtdfccd932016-09-29 15:34:44 -0700445 VC4_SET_FIELD(mode->hdisplay * pixel_rep,
446 VC4_HDMI_HORZA_HAP));
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800447
448 HDMI_WRITE(VC4_HDMI_HORZB,
Eric Anholtdfccd932016-09-29 15:34:44 -0700449 VC4_SET_FIELD((mode->htotal -
450 mode->hsync_end) * pixel_rep,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800451 VC4_HDMI_HORZB_HBP) |
Eric Anholtdfccd932016-09-29 15:34:44 -0700452 VC4_SET_FIELD((mode->hsync_end -
453 mode->hsync_start) * pixel_rep,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800454 VC4_HDMI_HORZB_HSP) |
Eric Anholtdfccd932016-09-29 15:34:44 -0700455 VC4_SET_FIELD((mode->hsync_start -
456 mode->hdisplay) * pixel_rep,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800457 VC4_HDMI_HORZB_HFP));
458
459 HDMI_WRITE(VC4_HDMI_VERTA0, verta);
460 HDMI_WRITE(VC4_HDMI_VERTA1, verta);
461
Eric Anholt682e62c2016-09-28 17:30:25 -0700462 HDMI_WRITE(VC4_HDMI_VERTB0, vertb_even);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800463 HDMI_WRITE(VC4_HDMI_VERTB1, vertb);
464
465 HD_WRITE(VC4_HD_VID_CTL,
466 (vsync_pos ? 0 : VC4_HD_VID_CTL_VSYNC_LOW) |
467 (hsync_pos ? 0 : VC4_HD_VID_CTL_HSYNC_LOW));
468
Eric Anholt6e1cbba2016-09-16 10:59:45 +0100469 csc_ctl = VC4_SET_FIELD(VC4_HD_CSC_CTL_ORDER_BGR,
470 VC4_HD_CSC_CTL_ORDER);
471
Ville Syrjäläc8127cf02017-01-11 16:18:35 +0200472 if (vc4_encoder->hdmi_monitor &&
473 drm_default_rgb_quant_range(mode) ==
474 HDMI_QUANTIZATION_RANGE_LIMITED) {
Eric Anholt6e1cbba2016-09-16 10:59:45 +0100475 /* CEA VICs other than #1 requre limited range RGB
Eric Anholt21317b32016-09-29 15:34:43 -0700476 * output unless overridden by an AVI infoframe.
477 * Apply a colorspace conversion to squash 0-255 down
478 * to 16-235. The matrix here is:
Eric Anholt6e1cbba2016-09-16 10:59:45 +0100479 *
480 * [ 0 0 0.8594 16]
481 * [ 0 0.8594 0 16]
482 * [ 0.8594 0 0 16]
483 * [ 0 0 0 1]
484 */
485 csc_ctl |= VC4_HD_CSC_CTL_ENABLE;
486 csc_ctl |= VC4_HD_CSC_CTL_RGB2YCC;
487 csc_ctl |= VC4_SET_FIELD(VC4_HD_CSC_CTL_MODE_CUSTOM,
488 VC4_HD_CSC_CTL_MODE);
489
490 HD_WRITE(VC4_HD_CSC_12_11, (0x000 << 16) | 0x000);
491 HD_WRITE(VC4_HD_CSC_14_13, (0x100 << 16) | 0x6e0);
492 HD_WRITE(VC4_HD_CSC_22_21, (0x6e0 << 16) | 0x000);
493 HD_WRITE(VC4_HD_CSC_24_23, (0x100 << 16) | 0x000);
494 HD_WRITE(VC4_HD_CSC_32_31, (0x000 << 16) | 0x6e0);
495 HD_WRITE(VC4_HD_CSC_34_33, (0x100 << 16) | 0x000);
Eric Anholt21317b32016-09-29 15:34:43 -0700496 vc4_encoder->limited_rgb_range = true;
497 } else {
498 vc4_encoder->limited_rgb_range = false;
Eric Anholt6e1cbba2016-09-16 10:59:45 +0100499 }
500
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800501 /* The RGB order applies even when CSC is disabled. */
Eric Anholt6e1cbba2016-09-16 10:59:45 +0100502 HD_WRITE(VC4_HD_CSC_CTL, csc_ctl);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800503
504 HDMI_WRITE(VC4_HDMI_FIFO_CTL, VC4_HDMI_FIFO_CTL_MASTER_SLAVE_N);
505
506 if (debug_dump_regs) {
Maxime Ripard3408cc22020-09-03 10:01:14 +0200507 struct drm_printer p = drm_info_printer(&vc4_hdmi->pdev->dev);
Eric Anholt30517192019-02-20 13:03:38 -0800508
Maxime Ripard3408cc22020-09-03 10:01:14 +0200509 dev_info(&vc4_hdmi->pdev->dev, "HDMI regs after:\n");
510 drm_print_regset32(&p, &vc4_hdmi->hdmi_regset);
511 drm_print_regset32(&p, &vc4_hdmi->hd_regset);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800512 }
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800513
514 HD_WRITE(VC4_HD_VID_CTL,
515 HD_READ(VC4_HD_VID_CTL) |
516 VC4_HD_VID_CTL_ENABLE |
517 VC4_HD_VID_CTL_UNDERFLOW_ENABLE |
518 VC4_HD_VID_CTL_FRAME_COUNTER_RESET);
519
520 if (vc4_encoder->hdmi_monitor) {
521 HDMI_WRITE(VC4_HDMI_SCHEDULER_CONTROL,
522 HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) |
523 VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);
524
525 ret = wait_for(HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) &
Eric Anholt2b29bf12016-09-28 17:21:05 -0700526 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE, 1000);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800527 WARN_ONCE(ret, "Timeout waiting for "
528 "VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
529 } else {
530 HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG,
531 HDMI_READ(VC4_HDMI_RAM_PACKET_CONFIG) &
532 ~(VC4_HDMI_RAM_PACKET_ENABLE));
533 HDMI_WRITE(VC4_HDMI_SCHEDULER_CONTROL,
534 HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) &
535 ~VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);
536
537 ret = wait_for(!(HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) &
Eric Anholt2b29bf12016-09-28 17:21:05 -0700538 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE), 1000);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800539 WARN_ONCE(ret, "Timeout waiting for "
540 "!VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
541 }
542
543 if (vc4_encoder->hdmi_monitor) {
544 u32 drift;
545
546 WARN_ON(!(HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) &
547 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE));
548 HDMI_WRITE(VC4_HDMI_SCHEDULER_CONTROL,
549 HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) |
550 VC4_HDMI_SCHEDULER_CONTROL_VERT_ALWAYS_KEEPOUT);
551
Eric Anholt21317b32016-09-29 15:34:43 -0700552 HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG,
553 VC4_HDMI_RAM_PACKET_ENABLE);
554
555 vc4_hdmi_set_infoframes(encoder);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800556
557 drift = HDMI_READ(VC4_HDMI_FIFO_CTL);
558 drift &= VC4_HDMI_FIFO_VALID_WRITE_MASK;
559
560 HDMI_WRITE(VC4_HDMI_FIFO_CTL,
561 drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
562 HDMI_WRITE(VC4_HDMI_FIFO_CTL,
563 drift | VC4_HDMI_FIFO_CTL_RECENTER);
Stefan Wahrend8eb9de2018-02-24 13:38:14 +0100564 usleep_range(1000, 1100);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800565 HDMI_WRITE(VC4_HDMI_FIFO_CTL,
566 drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
567 HDMI_WRITE(VC4_HDMI_FIFO_CTL,
568 drift | VC4_HDMI_FIFO_CTL_RECENTER);
569
570 ret = wait_for(HDMI_READ(VC4_HDMI_FIFO_CTL) &
571 VC4_HDMI_FIFO_CTL_RECENTER_DONE, 1);
572 WARN_ONCE(ret, "Timeout waiting for "
573 "VC4_HDMI_FIFO_CTL_RECENTER_DONE");
574 }
575}
576
Eric Anholt32e823c2017-09-20 15:59:34 -0700577static enum drm_mode_status
578vc4_hdmi_encoder_mode_valid(struct drm_encoder *crtc,
579 const struct drm_display_mode *mode)
580{
Nicolas Saenz Julienneb1e73962020-03-26 13:20:01 +0100581 /*
582 * As stated in RPi's vc4 firmware "HDMI state machine (HSM) clock must
583 * be faster than pixel clock, infinitesimally faster, tested in
584 * simulation. Otherwise, exact value is unimportant for HDMI
585 * operation." This conflicts with bcm2835's vc4 documentation, which
586 * states HSM's clock has to be at least 108% of the pixel clock.
587 *
588 * Real life tests reveal that vc4's firmware statement holds up, and
589 * users are able to use pixel clocks closer to HSM's, namely for
590 * 1920x1200@60Hz. So it was decided to have leave a 1% margin between
591 * both clocks. Which, for RPi0-3 implies a maximum pixel clock of
592 * 162MHz.
593 *
594 * Additionally, the AXI clock needs to be at least 25% of
595 * pixel clock, but HSM ends up being the limiting factor.
Eric Anholt32e823c2017-09-20 15:59:34 -0700596 */
Nicolas Saenz Julienneb1e73962020-03-26 13:20:01 +0100597 if (mode->clock > HSM_CLOCK_FREQ / (1000 * 101 / 100))
Eric Anholt32e823c2017-09-20 15:59:34 -0700598 return MODE_CLOCK_HIGH;
599
600 return MODE_OK;
601}
602
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800603static const struct drm_encoder_helper_funcs vc4_hdmi_encoder_helper_funcs = {
Eric Anholt32e823c2017-09-20 15:59:34 -0700604 .mode_valid = vc4_hdmi_encoder_mode_valid,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800605 .disable = vc4_hdmi_encoder_disable,
606 .enable = vc4_hdmi_encoder_enable,
607};
608
Eric Anholtbb7d7852017-02-27 12:28:02 -0800609/* HDMI audio codec callbacks */
Maxime Ripard3408cc22020-09-03 10:01:14 +0200610static void vc4_hdmi_audio_set_mai_clock(struct vc4_hdmi *vc4_hdmi)
Eric Anholtbb7d7852017-02-27 12:28:02 -0800611{
Maxime Ripard3408cc22020-09-03 10:01:14 +0200612 u32 hsm_clock = clk_get_rate(vc4_hdmi->hsm_clock);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800613 unsigned long n, m;
614
Maxime Ripard3408cc22020-09-03 10:01:14 +0200615 rational_best_approximation(hsm_clock, vc4_hdmi->audio.samplerate,
Eric Anholtbb7d7852017-02-27 12:28:02 -0800616 VC4_HD_MAI_SMP_N_MASK >>
617 VC4_HD_MAI_SMP_N_SHIFT,
618 (VC4_HD_MAI_SMP_M_MASK >>
619 VC4_HD_MAI_SMP_M_SHIFT) + 1,
620 &n, &m);
621
622 HD_WRITE(VC4_HD_MAI_SMP,
623 VC4_SET_FIELD(n, VC4_HD_MAI_SMP_N) |
624 VC4_SET_FIELD(m - 1, VC4_HD_MAI_SMP_M));
625}
626
Maxime Ripard3408cc22020-09-03 10:01:14 +0200627static void vc4_hdmi_set_n_cts(struct vc4_hdmi *vc4_hdmi)
Eric Anholtbb7d7852017-02-27 12:28:02 -0800628{
Maxime Ripard3408cc22020-09-03 10:01:14 +0200629 struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800630 struct drm_crtc *crtc = encoder->crtc;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800631 const struct drm_display_mode *mode = &crtc->state->adjusted_mode;
Maxime Ripard3408cc22020-09-03 10:01:14 +0200632 u32 samplerate = vc4_hdmi->audio.samplerate;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800633 u32 n, cts;
634 u64 tmp;
635
636 n = 128 * samplerate / 1000;
637 tmp = (u64)(mode->clock * 1000) * n;
638 do_div(tmp, 128 * samplerate);
639 cts = tmp;
640
641 HDMI_WRITE(VC4_HDMI_CRP_CFG,
642 VC4_HDMI_CRP_CFG_EXTERNAL_CTS_EN |
643 VC4_SET_FIELD(n, VC4_HDMI_CRP_CFG_N));
644
645 /*
646 * We could get slightly more accurate clocks in some cases by
647 * providing a CTS_1 value. The two CTS values are alternated
648 * between based on the period fields
649 */
650 HDMI_WRITE(VC4_HDMI_CTS_0, cts);
651 HDMI_WRITE(VC4_HDMI_CTS_1, cts);
652}
653
654static inline struct vc4_hdmi *dai_to_hdmi(struct snd_soc_dai *dai)
655{
656 struct snd_soc_card *card = snd_soc_dai_get_drvdata(dai);
657
658 return snd_soc_card_get_drvdata(card);
659}
660
661static int vc4_hdmi_audio_startup(struct snd_pcm_substream *substream,
662 struct snd_soc_dai *dai)
663{
Maxime Ripard3408cc22020-09-03 10:01:14 +0200664 struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai);
665 struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
666 struct drm_connector *connector = &vc4_hdmi->connector.base;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800667 int ret;
668
Maxime Ripard3408cc22020-09-03 10:01:14 +0200669 if (vc4_hdmi->audio.substream && vc4_hdmi->audio.substream != substream)
Eric Anholtbb7d7852017-02-27 12:28:02 -0800670 return -EINVAL;
671
Maxime Ripard3408cc22020-09-03 10:01:14 +0200672 vc4_hdmi->audio.substream = substream;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800673
674 /*
675 * If the HDMI encoder hasn't probed, or the encoder is
676 * currently in DVI mode, treat the codec dai as missing.
677 */
678 if (!encoder->crtc || !(HDMI_READ(VC4_HDMI_RAM_PACKET_CONFIG) &
679 VC4_HDMI_RAM_PACKET_ENABLE))
680 return -ENODEV;
681
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200682 ret = snd_pcm_hw_constraint_eld(substream->runtime, connector->eld);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800683 if (ret)
684 return ret;
685
686 return 0;
687}
688
689static int vc4_hdmi_audio_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
690{
691 return 0;
692}
693
Maxime Ripard3408cc22020-09-03 10:01:14 +0200694static void vc4_hdmi_audio_reset(struct vc4_hdmi *vc4_hdmi)
Eric Anholtbb7d7852017-02-27 12:28:02 -0800695{
Maxime Ripard3408cc22020-09-03 10:01:14 +0200696 struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
Maxime Ripard3408cc22020-09-03 10:01:14 +0200697 struct device *dev = &vc4_hdmi->pdev->dev;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800698 int ret;
699
700 ret = vc4_hdmi_stop_packet(encoder, HDMI_INFOFRAME_TYPE_AUDIO);
701 if (ret)
702 dev_err(dev, "Failed to stop audio infoframe: %d\n", ret);
703
704 HD_WRITE(VC4_HD_MAI_CTL, VC4_HD_MAI_CTL_RESET);
705 HD_WRITE(VC4_HD_MAI_CTL, VC4_HD_MAI_CTL_ERRORF);
706 HD_WRITE(VC4_HD_MAI_CTL, VC4_HD_MAI_CTL_FLUSH);
707}
708
709static void vc4_hdmi_audio_shutdown(struct snd_pcm_substream *substream,
710 struct snd_soc_dai *dai)
711{
Maxime Ripard3408cc22020-09-03 10:01:14 +0200712 struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800713
Maxime Ripard3408cc22020-09-03 10:01:14 +0200714 if (substream != vc4_hdmi->audio.substream)
Eric Anholtbb7d7852017-02-27 12:28:02 -0800715 return;
716
Maxime Ripard3408cc22020-09-03 10:01:14 +0200717 vc4_hdmi_audio_reset(vc4_hdmi);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800718
Maxime Ripard3408cc22020-09-03 10:01:14 +0200719 vc4_hdmi->audio.substream = NULL;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800720}
721
722/* HDMI audio codec callbacks */
723static int vc4_hdmi_audio_hw_params(struct snd_pcm_substream *substream,
724 struct snd_pcm_hw_params *params,
725 struct snd_soc_dai *dai)
726{
Maxime Ripard3408cc22020-09-03 10:01:14 +0200727 struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai);
Maxime Ripard3408cc22020-09-03 10:01:14 +0200728 struct device *dev = &vc4_hdmi->pdev->dev;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800729 u32 audio_packet_config, channel_mask;
730 u32 channel_map, i;
731
Maxime Ripard3408cc22020-09-03 10:01:14 +0200732 if (substream != vc4_hdmi->audio.substream)
Eric Anholtbb7d7852017-02-27 12:28:02 -0800733 return -EINVAL;
734
735 dev_dbg(dev, "%s: %u Hz, %d bit, %d channels\n", __func__,
736 params_rate(params), params_width(params),
737 params_channels(params));
738
Maxime Ripard3408cc22020-09-03 10:01:14 +0200739 vc4_hdmi->audio.channels = params_channels(params);
740 vc4_hdmi->audio.samplerate = params_rate(params);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800741
742 HD_WRITE(VC4_HD_MAI_CTL,
743 VC4_HD_MAI_CTL_RESET |
744 VC4_HD_MAI_CTL_FLUSH |
745 VC4_HD_MAI_CTL_DLATE |
746 VC4_HD_MAI_CTL_ERRORE |
747 VC4_HD_MAI_CTL_ERRORF);
748
Maxime Ripard3408cc22020-09-03 10:01:14 +0200749 vc4_hdmi_audio_set_mai_clock(vc4_hdmi);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800750
751 audio_packet_config =
752 VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_SAMPLE_FLAT |
753 VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_INACTIVE_CHANNELS |
754 VC4_SET_FIELD(0xf, VC4_HDMI_AUDIO_PACKET_B_FRAME_IDENTIFIER);
755
Maxime Ripard3408cc22020-09-03 10:01:14 +0200756 channel_mask = GENMASK(vc4_hdmi->audio.channels - 1, 0);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800757 audio_packet_config |= VC4_SET_FIELD(channel_mask,
758 VC4_HDMI_AUDIO_PACKET_CEA_MASK);
759
760 /* Set the MAI threshold. This logic mimics the firmware's. */
Maxime Ripard3408cc22020-09-03 10:01:14 +0200761 if (vc4_hdmi->audio.samplerate > 96000) {
Eric Anholtbb7d7852017-02-27 12:28:02 -0800762 HD_WRITE(VC4_HD_MAI_THR,
763 VC4_SET_FIELD(0x12, VC4_HD_MAI_THR_DREQHIGH) |
764 VC4_SET_FIELD(0x12, VC4_HD_MAI_THR_DREQLOW));
Maxime Ripard3408cc22020-09-03 10:01:14 +0200765 } else if (vc4_hdmi->audio.samplerate > 48000) {
Eric Anholtbb7d7852017-02-27 12:28:02 -0800766 HD_WRITE(VC4_HD_MAI_THR,
767 VC4_SET_FIELD(0x14, VC4_HD_MAI_THR_DREQHIGH) |
768 VC4_SET_FIELD(0x12, VC4_HD_MAI_THR_DREQLOW));
769 } else {
770 HD_WRITE(VC4_HD_MAI_THR,
771 VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_PANICHIGH) |
772 VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_PANICLOW) |
773 VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_DREQHIGH) |
774 VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_DREQLOW));
775 }
776
777 HDMI_WRITE(VC4_HDMI_MAI_CONFIG,
778 VC4_HDMI_MAI_CONFIG_BIT_REVERSE |
779 VC4_SET_FIELD(channel_mask, VC4_HDMI_MAI_CHANNEL_MASK));
780
781 channel_map = 0;
782 for (i = 0; i < 8; i++) {
783 if (channel_mask & BIT(i))
784 channel_map |= i << (3 * i);
785 }
786
787 HDMI_WRITE(VC4_HDMI_MAI_CHANNEL_MAP, channel_map);
788 HDMI_WRITE(VC4_HDMI_AUDIO_PACKET_CONFIG, audio_packet_config);
Maxime Ripard3408cc22020-09-03 10:01:14 +0200789 vc4_hdmi_set_n_cts(vc4_hdmi);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800790
791 return 0;
792}
793
794static int vc4_hdmi_audio_trigger(struct snd_pcm_substream *substream, int cmd,
795 struct snd_soc_dai *dai)
796{
Maxime Ripard3408cc22020-09-03 10:01:14 +0200797 struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai);
798 struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800799
800 switch (cmd) {
801 case SNDRV_PCM_TRIGGER_START:
802 vc4_hdmi_set_audio_infoframe(encoder);
803 HDMI_WRITE(VC4_HDMI_TX_PHY_CTL0,
804 HDMI_READ(VC4_HDMI_TX_PHY_CTL0) &
805 ~VC4_HDMI_TX_PHY_RNG_PWRDN);
806 HD_WRITE(VC4_HD_MAI_CTL,
Maxime Ripard3408cc22020-09-03 10:01:14 +0200807 VC4_SET_FIELD(vc4_hdmi->audio.channels,
Eric Anholtbb7d7852017-02-27 12:28:02 -0800808 VC4_HD_MAI_CTL_CHNUM) |
809 VC4_HD_MAI_CTL_ENABLE);
810 break;
811 case SNDRV_PCM_TRIGGER_STOP:
812 HD_WRITE(VC4_HD_MAI_CTL,
813 VC4_HD_MAI_CTL_DLATE |
814 VC4_HD_MAI_CTL_ERRORE |
815 VC4_HD_MAI_CTL_ERRORF);
816 HDMI_WRITE(VC4_HDMI_TX_PHY_CTL0,
817 HDMI_READ(VC4_HDMI_TX_PHY_CTL0) |
818 VC4_HDMI_TX_PHY_RNG_PWRDN);
819 break;
820 default:
821 break;
822 }
823
824 return 0;
825}
826
827static inline struct vc4_hdmi *
828snd_component_to_hdmi(struct snd_soc_component *component)
829{
830 struct snd_soc_card *card = snd_soc_component_get_drvdata(component);
831
832 return snd_soc_card_get_drvdata(card);
833}
834
835static int vc4_hdmi_audio_eld_ctl_info(struct snd_kcontrol *kcontrol,
836 struct snd_ctl_elem_info *uinfo)
837{
838 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Maxime Ripard3408cc22020-09-03 10:01:14 +0200839 struct vc4_hdmi *vc4_hdmi = snd_component_to_hdmi(component);
840 struct drm_connector *connector = &vc4_hdmi->connector.base;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800841
842 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200843 uinfo->count = sizeof(connector->eld);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800844
845 return 0;
846}
847
848static int vc4_hdmi_audio_eld_ctl_get(struct snd_kcontrol *kcontrol,
849 struct snd_ctl_elem_value *ucontrol)
850{
851 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Maxime Ripard3408cc22020-09-03 10:01:14 +0200852 struct vc4_hdmi *vc4_hdmi = snd_component_to_hdmi(component);
853 struct drm_connector *connector = &vc4_hdmi->connector.base;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800854
Maxime Ripardc98c85b2020-09-03 10:01:12 +0200855 memcpy(ucontrol->value.bytes.data, connector->eld,
856 sizeof(connector->eld));
Eric Anholtbb7d7852017-02-27 12:28:02 -0800857
858 return 0;
859}
860
861static const struct snd_kcontrol_new vc4_hdmi_audio_controls[] = {
862 {
863 .access = SNDRV_CTL_ELEM_ACCESS_READ |
864 SNDRV_CTL_ELEM_ACCESS_VOLATILE,
865 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
866 .name = "ELD",
867 .info = vc4_hdmi_audio_eld_ctl_info,
868 .get = vc4_hdmi_audio_eld_ctl_get,
869 },
870};
871
872static const struct snd_soc_dapm_widget vc4_hdmi_audio_widgets[] = {
873 SND_SOC_DAPM_OUTPUT("TX"),
874};
875
876static const struct snd_soc_dapm_route vc4_hdmi_audio_routes[] = {
877 { "TX", NULL, "Playback" },
878};
879
Kuninori Morimoto635b1c12018-01-29 04:35:04 +0000880static const struct snd_soc_component_driver vc4_hdmi_audio_component_drv = {
881 .controls = vc4_hdmi_audio_controls,
882 .num_controls = ARRAY_SIZE(vc4_hdmi_audio_controls),
883 .dapm_widgets = vc4_hdmi_audio_widgets,
884 .num_dapm_widgets = ARRAY_SIZE(vc4_hdmi_audio_widgets),
885 .dapm_routes = vc4_hdmi_audio_routes,
886 .num_dapm_routes = ARRAY_SIZE(vc4_hdmi_audio_routes),
887 .idle_bias_on = 1,
888 .use_pmdown_time = 1,
889 .endianness = 1,
890 .non_legacy_dai_naming = 1,
Eric Anholtbb7d7852017-02-27 12:28:02 -0800891};
892
893static const struct snd_soc_dai_ops vc4_hdmi_audio_dai_ops = {
894 .startup = vc4_hdmi_audio_startup,
895 .shutdown = vc4_hdmi_audio_shutdown,
896 .hw_params = vc4_hdmi_audio_hw_params,
897 .set_fmt = vc4_hdmi_audio_set_fmt,
898 .trigger = vc4_hdmi_audio_trigger,
899};
900
901static struct snd_soc_dai_driver vc4_hdmi_audio_codec_dai_drv = {
902 .name = "vc4-hdmi-hifi",
903 .playback = {
904 .stream_name = "Playback",
905 .channels_min = 2,
906 .channels_max = 8,
907 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
908 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
909 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
910 SNDRV_PCM_RATE_192000,
911 .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
912 },
913};
914
915static const struct snd_soc_component_driver vc4_hdmi_audio_cpu_dai_comp = {
916 .name = "vc4-hdmi-cpu-dai-component",
917};
918
919static int vc4_hdmi_audio_cpu_dai_probe(struct snd_soc_dai *dai)
920{
Maxime Ripard3408cc22020-09-03 10:01:14 +0200921 struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800922
Maxime Ripard3408cc22020-09-03 10:01:14 +0200923 snd_soc_dai_init_dma_data(dai, &vc4_hdmi->audio.dma_data, NULL);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800924
925 return 0;
926}
927
928static struct snd_soc_dai_driver vc4_hdmi_audio_cpu_dai_drv = {
929 .name = "vc4-hdmi-cpu-dai",
930 .probe = vc4_hdmi_audio_cpu_dai_probe,
931 .playback = {
932 .stream_name = "Playback",
933 .channels_min = 1,
934 .channels_max = 8,
935 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
936 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
937 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
938 SNDRV_PCM_RATE_192000,
939 .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
940 },
941 .ops = &vc4_hdmi_audio_dai_ops,
942};
943
944static const struct snd_dmaengine_pcm_config pcm_conf = {
945 .chan_names[SNDRV_PCM_STREAM_PLAYBACK] = "audio-rx",
946 .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
947};
948
Maxime Ripard3408cc22020-09-03 10:01:14 +0200949static int vc4_hdmi_audio_init(struct vc4_hdmi *vc4_hdmi)
Eric Anholtbb7d7852017-02-27 12:28:02 -0800950{
Maxime Ripard3408cc22020-09-03 10:01:14 +0200951 struct snd_soc_dai_link *dai_link = &vc4_hdmi->audio.link;
952 struct snd_soc_card *card = &vc4_hdmi->audio.card;
953 struct device *dev = &vc4_hdmi->pdev->dev;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800954 const __be32 *addr;
955 int ret;
956
957 if (!of_find_property(dev->of_node, "dmas", NULL)) {
958 dev_warn(dev,
959 "'dmas' DT property is missing, no HDMI audio\n");
960 return 0;
961 }
962
963 /*
964 * Get the physical address of VC4_HD_MAI_DATA. We need to retrieve
965 * the bus address specified in the DT, because the physical address
966 * (the one returned by platform_get_resource()) is not appropriate
967 * for DMA transfers.
968 * This VC/MMU should probably be exposed to avoid this kind of hacks.
969 */
970 addr = of_get_address(dev->of_node, 1, NULL, NULL);
Maxime Ripard3408cc22020-09-03 10:01:14 +0200971 vc4_hdmi->audio.dma_data.addr = be32_to_cpup(addr) + VC4_HD_MAI_DATA;
972 vc4_hdmi->audio.dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
973 vc4_hdmi->audio.dma_data.maxburst = 2;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800974
975 ret = devm_snd_dmaengine_pcm_register(dev, &pcm_conf, 0);
976 if (ret) {
977 dev_err(dev, "Could not register PCM component: %d\n", ret);
978 return ret;
979 }
980
981 ret = devm_snd_soc_register_component(dev, &vc4_hdmi_audio_cpu_dai_comp,
982 &vc4_hdmi_audio_cpu_dai_drv, 1);
983 if (ret) {
984 dev_err(dev, "Could not register CPU DAI: %d\n", ret);
985 return ret;
986 }
987
Kuninori Morimoto635b1c12018-01-29 04:35:04 +0000988 /* register component and codec dai */
989 ret = devm_snd_soc_register_component(dev, &vc4_hdmi_audio_component_drv,
Eric Anholtbb7d7852017-02-27 12:28:02 -0800990 &vc4_hdmi_audio_codec_dai_drv, 1);
991 if (ret) {
Kuninori Morimoto635b1c12018-01-29 04:35:04 +0000992 dev_err(dev, "Could not register component: %d\n", ret);
Eric Anholtbb7d7852017-02-27 12:28:02 -0800993 return ret;
994 }
995
Maxime Ripard3408cc22020-09-03 10:01:14 +0200996 dai_link->cpus = &vc4_hdmi->audio.cpu;
997 dai_link->codecs = &vc4_hdmi->audio.codec;
998 dai_link->platforms = &vc4_hdmi->audio.platform;
Kuninori Morimoto0467d8e2019-06-06 13:19:19 +0900999
1000 dai_link->num_cpus = 1;
1001 dai_link->num_codecs = 1;
Kuninori Morimoto8a90efd2019-06-28 10:46:14 +09001002 dai_link->num_platforms = 1;
Kuninori Morimoto0467d8e2019-06-06 13:19:19 +09001003
Eric Anholtbb7d7852017-02-27 12:28:02 -08001004 dai_link->name = "MAI";
1005 dai_link->stream_name = "MAI PCM";
Kuninori Morimoto0467d8e2019-06-06 13:19:19 +09001006 dai_link->codecs->dai_name = vc4_hdmi_audio_codec_dai_drv.name;
1007 dai_link->cpus->dai_name = dev_name(dev);
1008 dai_link->codecs->name = dev_name(dev);
Kuninori Morimoto8a90efd2019-06-28 10:46:14 +09001009 dai_link->platforms->name = dev_name(dev);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001010
1011 card->dai_link = dai_link;
1012 card->num_links = 1;
1013 card->name = "vc4-hdmi";
1014 card->dev = dev;
1015
1016 /*
1017 * Be careful, snd_soc_register_card() calls dev_set_drvdata() and
1018 * stores a pointer to the snd card object in dev->driver_data. This
1019 * means we cannot use it for something else. The hdmi back-pointer is
1020 * now stored in card->drvdata and should be retrieved with
1021 * snd_soc_card_get_drvdata() if needed.
1022 */
Maxime Ripard3408cc22020-09-03 10:01:14 +02001023 snd_soc_card_set_drvdata(card, vc4_hdmi);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001024 ret = devm_snd_soc_register_card(dev, card);
Kuninori Morimoto635b1c12018-01-29 04:35:04 +00001025 if (ret)
Eric Anholtbb7d7852017-02-27 12:28:02 -08001026 dev_err(dev, "Could not register sound card: %d\n", ret);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001027
1028 return ret;
Eric Anholtbb7d7852017-02-27 12:28:02 -08001029
Eric Anholtbb7d7852017-02-27 12:28:02 -08001030}
1031
Hans Verkuil15b45112017-07-16 12:48:04 +02001032#ifdef CONFIG_DRM_VC4_HDMI_CEC
1033static irqreturn_t vc4_cec_irq_handler_thread(int irq, void *priv)
1034{
Maxime Ripard66bf1c32020-09-03 10:01:18 +02001035 struct vc4_hdmi *vc4_hdmi = priv;
Hans Verkuil15b45112017-07-16 12:48:04 +02001036
Maxime Ripard3408cc22020-09-03 10:01:14 +02001037 if (vc4_hdmi->cec_irq_was_rx) {
1038 if (vc4_hdmi->cec_rx_msg.len)
1039 cec_received_msg(vc4_hdmi->cec_adap,
1040 &vc4_hdmi->cec_rx_msg);
1041 } else if (vc4_hdmi->cec_tx_ok) {
1042 cec_transmit_done(vc4_hdmi->cec_adap, CEC_TX_STATUS_OK,
Hans Verkuil15b45112017-07-16 12:48:04 +02001043 0, 0, 0, 0);
1044 } else {
1045 /*
1046 * This CEC implementation makes 1 retry, so if we
1047 * get a NACK, then that means it made 2 attempts.
1048 */
Maxime Ripard3408cc22020-09-03 10:01:14 +02001049 cec_transmit_done(vc4_hdmi->cec_adap, CEC_TX_STATUS_NACK,
Hans Verkuil15b45112017-07-16 12:48:04 +02001050 0, 2, 0, 0);
1051 }
1052 return IRQ_HANDLED;
1053}
1054
Maxime Ripard66bf1c32020-09-03 10:01:18 +02001055static void vc4_cec_read_msg(struct vc4_hdmi *vc4_hdmi, u32 cntrl1)
Hans Verkuil15b45112017-07-16 12:48:04 +02001056{
Maxime Ripard13311452020-09-03 10:01:15 +02001057 struct cec_msg *msg = &vc4_hdmi->cec_rx_msg;
Hans Verkuil15b45112017-07-16 12:48:04 +02001058 unsigned int i;
1059
1060 msg->len = 1 + ((cntrl1 & VC4_HDMI_CEC_REC_WRD_CNT_MASK) >>
1061 VC4_HDMI_CEC_REC_WRD_CNT_SHIFT);
1062 for (i = 0; i < msg->len; i += 4) {
1063 u32 val = HDMI_READ(VC4_HDMI_CEC_RX_DATA_1 + i);
1064
1065 msg->msg[i] = val & 0xff;
1066 msg->msg[i + 1] = (val >> 8) & 0xff;
1067 msg->msg[i + 2] = (val >> 16) & 0xff;
1068 msg->msg[i + 3] = (val >> 24) & 0xff;
1069 }
1070}
1071
1072static irqreturn_t vc4_cec_irq_handler(int irq, void *priv)
1073{
Maxime Ripard66bf1c32020-09-03 10:01:18 +02001074 struct vc4_hdmi *vc4_hdmi = priv;
Hans Verkuil15b45112017-07-16 12:48:04 +02001075 u32 stat = HDMI_READ(VC4_HDMI_CPU_STATUS);
1076 u32 cntrl1, cntrl5;
1077
1078 if (!(stat & VC4_HDMI_CPU_CEC))
1079 return IRQ_NONE;
Maxime Ripard3408cc22020-09-03 10:01:14 +02001080 vc4_hdmi->cec_rx_msg.len = 0;
Hans Verkuil15b45112017-07-16 12:48:04 +02001081 cntrl1 = HDMI_READ(VC4_HDMI_CEC_CNTRL_1);
1082 cntrl5 = HDMI_READ(VC4_HDMI_CEC_CNTRL_5);
Maxime Ripard3408cc22020-09-03 10:01:14 +02001083 vc4_hdmi->cec_irq_was_rx = cntrl5 & VC4_HDMI_CEC_RX_CEC_INT;
1084 if (vc4_hdmi->cec_irq_was_rx) {
Maxime Ripard66bf1c32020-09-03 10:01:18 +02001085 vc4_cec_read_msg(vc4_hdmi, cntrl1);
Hans Verkuil15b45112017-07-16 12:48:04 +02001086 cntrl1 |= VC4_HDMI_CEC_CLEAR_RECEIVE_OFF;
1087 HDMI_WRITE(VC4_HDMI_CEC_CNTRL_1, cntrl1);
1088 cntrl1 &= ~VC4_HDMI_CEC_CLEAR_RECEIVE_OFF;
1089 } else {
Maxime Ripard3408cc22020-09-03 10:01:14 +02001090 vc4_hdmi->cec_tx_ok = cntrl1 & VC4_HDMI_CEC_TX_STATUS_GOOD;
Hans Verkuil15b45112017-07-16 12:48:04 +02001091 cntrl1 &= ~VC4_HDMI_CEC_START_XMIT_BEGIN;
1092 }
1093 HDMI_WRITE(VC4_HDMI_CEC_CNTRL_1, cntrl1);
1094 HDMI_WRITE(VC4_HDMI_CPU_CLEAR, VC4_HDMI_CPU_CEC);
1095
1096 return IRQ_WAKE_THREAD;
1097}
1098
1099static int vc4_hdmi_cec_adap_enable(struct cec_adapter *adap, bool enable)
1100{
Maxime Ripard66bf1c32020-09-03 10:01:18 +02001101 struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
Hans Verkuil15b45112017-07-16 12:48:04 +02001102 /* clock period in microseconds */
1103 const u32 usecs = 1000000 / CEC_CLOCK_FREQ;
1104 u32 val = HDMI_READ(VC4_HDMI_CEC_CNTRL_5);
1105
1106 val &= ~(VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET |
1107 VC4_HDMI_CEC_CNT_TO_4700_US_MASK |
1108 VC4_HDMI_CEC_CNT_TO_4500_US_MASK);
1109 val |= ((4700 / usecs) << VC4_HDMI_CEC_CNT_TO_4700_US_SHIFT) |
1110 ((4500 / usecs) << VC4_HDMI_CEC_CNT_TO_4500_US_SHIFT);
1111
1112 if (enable) {
1113 HDMI_WRITE(VC4_HDMI_CEC_CNTRL_5, val |
1114 VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET);
1115 HDMI_WRITE(VC4_HDMI_CEC_CNTRL_5, val);
1116 HDMI_WRITE(VC4_HDMI_CEC_CNTRL_2,
1117 ((1500 / usecs) << VC4_HDMI_CEC_CNT_TO_1500_US_SHIFT) |
1118 ((1300 / usecs) << VC4_HDMI_CEC_CNT_TO_1300_US_SHIFT) |
1119 ((800 / usecs) << VC4_HDMI_CEC_CNT_TO_800_US_SHIFT) |
1120 ((600 / usecs) << VC4_HDMI_CEC_CNT_TO_600_US_SHIFT) |
1121 ((400 / usecs) << VC4_HDMI_CEC_CNT_TO_400_US_SHIFT));
1122 HDMI_WRITE(VC4_HDMI_CEC_CNTRL_3,
1123 ((2750 / usecs) << VC4_HDMI_CEC_CNT_TO_2750_US_SHIFT) |
1124 ((2400 / usecs) << VC4_HDMI_CEC_CNT_TO_2400_US_SHIFT) |
1125 ((2050 / usecs) << VC4_HDMI_CEC_CNT_TO_2050_US_SHIFT) |
1126 ((1700 / usecs) << VC4_HDMI_CEC_CNT_TO_1700_US_SHIFT));
1127 HDMI_WRITE(VC4_HDMI_CEC_CNTRL_4,
1128 ((4300 / usecs) << VC4_HDMI_CEC_CNT_TO_4300_US_SHIFT) |
1129 ((3900 / usecs) << VC4_HDMI_CEC_CNT_TO_3900_US_SHIFT) |
1130 ((3600 / usecs) << VC4_HDMI_CEC_CNT_TO_3600_US_SHIFT) |
1131 ((3500 / usecs) << VC4_HDMI_CEC_CNT_TO_3500_US_SHIFT));
1132
1133 HDMI_WRITE(VC4_HDMI_CPU_MASK_CLEAR, VC4_HDMI_CPU_CEC);
1134 } else {
1135 HDMI_WRITE(VC4_HDMI_CPU_MASK_SET, VC4_HDMI_CPU_CEC);
1136 HDMI_WRITE(VC4_HDMI_CEC_CNTRL_5, val |
1137 VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET);
1138 }
1139 return 0;
1140}
1141
1142static int vc4_hdmi_cec_adap_log_addr(struct cec_adapter *adap, u8 log_addr)
1143{
Maxime Ripard66bf1c32020-09-03 10:01:18 +02001144 struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
Hans Verkuil15b45112017-07-16 12:48:04 +02001145
1146 HDMI_WRITE(VC4_HDMI_CEC_CNTRL_1,
1147 (HDMI_READ(VC4_HDMI_CEC_CNTRL_1) & ~VC4_HDMI_CEC_ADDR_MASK) |
1148 (log_addr & 0xf) << VC4_HDMI_CEC_ADDR_SHIFT);
1149 return 0;
1150}
1151
1152static int vc4_hdmi_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
1153 u32 signal_free_time, struct cec_msg *msg)
1154{
Maxime Ripard66bf1c32020-09-03 10:01:18 +02001155 struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
Hans Verkuil15b45112017-07-16 12:48:04 +02001156 u32 val;
1157 unsigned int i;
1158
1159 for (i = 0; i < msg->len; i += 4)
1160 HDMI_WRITE(VC4_HDMI_CEC_TX_DATA_1 + i,
1161 (msg->msg[i]) |
1162 (msg->msg[i + 1] << 8) |
1163 (msg->msg[i + 2] << 16) |
1164 (msg->msg[i + 3] << 24));
1165
1166 val = HDMI_READ(VC4_HDMI_CEC_CNTRL_1);
1167 val &= ~VC4_HDMI_CEC_START_XMIT_BEGIN;
1168 HDMI_WRITE(VC4_HDMI_CEC_CNTRL_1, val);
1169 val &= ~VC4_HDMI_CEC_MESSAGE_LENGTH_MASK;
1170 val |= (msg->len - 1) << VC4_HDMI_CEC_MESSAGE_LENGTH_SHIFT;
1171 val |= VC4_HDMI_CEC_START_XMIT_BEGIN;
1172
1173 HDMI_WRITE(VC4_HDMI_CEC_CNTRL_1, val);
1174 return 0;
1175}
1176
1177static const struct cec_adap_ops vc4_hdmi_cec_adap_ops = {
1178 .adap_enable = vc4_hdmi_cec_adap_enable,
1179 .adap_log_addr = vc4_hdmi_cec_adap_log_addr,
1180 .adap_transmit = vc4_hdmi_cec_adap_transmit,
1181};
1182#endif
1183
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001184static int vc4_hdmi_bind(struct device *dev, struct device *master, void *data)
1185{
Dariusz Marcinkiewicz66c2dee2019-08-23 13:24:25 +02001186#ifdef CONFIG_DRM_VC4_HDMI_CEC
1187 struct cec_connector_info conn_info;
1188#endif
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001189 struct platform_device *pdev = to_platform_device(dev);
1190 struct drm_device *drm = dev_get_drvdata(master);
1191 struct vc4_dev *vc4 = drm->dev_private;
Maxime Ripard3408cc22020-09-03 10:01:14 +02001192 struct vc4_hdmi *vc4_hdmi;
Maxime Ripardc98c85b2020-09-03 10:01:12 +02001193 struct drm_encoder *encoder;
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001194 struct device_node *ddc_node;
1195 u32 value;
1196 int ret;
1197
Maxime Ripard3408cc22020-09-03 10:01:14 +02001198 vc4_hdmi = devm_kzalloc(dev, sizeof(*vc4_hdmi), GFP_KERNEL);
1199 if (!vc4_hdmi)
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001200 return -ENOMEM;
1201
Maxime Ripard47c167b2020-09-03 10:01:19 +02001202 dev_set_drvdata(dev, vc4_hdmi);
Maxime Ripard3408cc22020-09-03 10:01:14 +02001203 encoder = &vc4_hdmi->encoder.base.base;
1204 vc4_hdmi->encoder.base.type = VC4_ENCODER_TYPE_HDMI0;
1205 vc4_hdmi->pdev = pdev;
Maxime Ripardc98c85b2020-09-03 10:01:12 +02001206
Maxime Ripard3408cc22020-09-03 10:01:14 +02001207 vc4_hdmi->hdmicore_regs = vc4_ioremap_regs(pdev, 0);
1208 if (IS_ERR(vc4_hdmi->hdmicore_regs))
1209 return PTR_ERR(vc4_hdmi->hdmicore_regs);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001210
Maxime Ripard3408cc22020-09-03 10:01:14 +02001211 vc4_hdmi->hd_regs = vc4_ioremap_regs(pdev, 1);
1212 if (IS_ERR(vc4_hdmi->hd_regs))
1213 return PTR_ERR(vc4_hdmi->hd_regs);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001214
Maxime Ripard3408cc22020-09-03 10:01:14 +02001215 vc4_hdmi->hdmi_regset.base = vc4_hdmi->hdmicore_regs;
1216 vc4_hdmi->hdmi_regset.regs = hdmi_regs;
1217 vc4_hdmi->hdmi_regset.nregs = ARRAY_SIZE(hdmi_regs);
1218 vc4_hdmi->hd_regset.base = vc4_hdmi->hd_regs;
1219 vc4_hdmi->hd_regset.regs = hd_regs;
1220 vc4_hdmi->hd_regset.nregs = ARRAY_SIZE(hd_regs);
Eric Anholt30517192019-02-20 13:03:38 -08001221
Maxime Ripard3408cc22020-09-03 10:01:14 +02001222 vc4_hdmi->pixel_clock = devm_clk_get(dev, "pixel");
1223 if (IS_ERR(vc4_hdmi->pixel_clock)) {
1224 ret = PTR_ERR(vc4_hdmi->pixel_clock);
James Hilliard8f6f5e02020-05-24 19:28:59 -06001225 if (ret != -EPROBE_DEFER)
1226 DRM_ERROR("Failed to get pixel clock\n");
1227 return ret;
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001228 }
Maxime Ripard3408cc22020-09-03 10:01:14 +02001229 vc4_hdmi->hsm_clock = devm_clk_get(dev, "hdmi");
1230 if (IS_ERR(vc4_hdmi->hsm_clock)) {
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001231 DRM_ERROR("Failed to get HDMI state machine clock\n");
Maxime Ripard3408cc22020-09-03 10:01:14 +02001232 return PTR_ERR(vc4_hdmi->hsm_clock);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001233 }
1234
Peter Chen027a6972016-07-05 10:04:54 +08001235 ddc_node = of_parse_phandle(dev->of_node, "ddc", 0);
1236 if (!ddc_node) {
1237 DRM_ERROR("Failed to find ddc node in device tree\n");
1238 return -ENODEV;
1239 }
1240
Maxime Ripard3408cc22020-09-03 10:01:14 +02001241 vc4_hdmi->ddc = of_find_i2c_adapter_by_node(ddc_node);
Peter Chen027a6972016-07-05 10:04:54 +08001242 of_node_put(ddc_node);
Maxime Ripard3408cc22020-09-03 10:01:14 +02001243 if (!vc4_hdmi->ddc) {
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001244 DRM_DEBUG("Failed to get ddc i2c adapter by node\n");
1245 return -EPROBE_DEFER;
1246 }
1247
Hans Verkuil10ee2752017-07-16 12:48:03 +02001248 /* This is the rate that is set by the firmware. The number
1249 * needs to be a bit higher than the pixel clock rate
1250 * (generally 148.5Mhz).
1251 */
Maxime Ripard3408cc22020-09-03 10:01:14 +02001252 ret = clk_set_rate(vc4_hdmi->hsm_clock, HSM_CLOCK_FREQ);
Hans Verkuil10ee2752017-07-16 12:48:03 +02001253 if (ret) {
1254 DRM_ERROR("Failed to set HSM clock rate: %d\n", ret);
1255 goto err_put_i2c;
1256 }
1257
Maxime Ripard3408cc22020-09-03 10:01:14 +02001258 ret = clk_prepare_enable(vc4_hdmi->hsm_clock);
Hans Verkuil10ee2752017-07-16 12:48:03 +02001259 if (ret) {
1260 DRM_ERROR("Failed to turn on HDMI state machine clock: %d\n",
1261 ret);
1262 goto err_put_i2c;
1263 }
1264
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001265 /* Only use the GPIO HPD pin if present in the DT, otherwise
1266 * we'll use the HDMI core's register.
1267 */
1268 if (of_find_property(dev->of_node, "hpd-gpios", &value)) {
Eric Anholt0b06e0a2016-02-29 17:53:01 -08001269 enum of_gpio_flags hpd_gpio_flags;
1270
Maxime Ripard3408cc22020-09-03 10:01:14 +02001271 vc4_hdmi->hpd_gpio = of_get_named_gpio_flags(dev->of_node,
1272 "hpd-gpios", 0,
1273 &hpd_gpio_flags);
1274 if (vc4_hdmi->hpd_gpio < 0) {
1275 ret = vc4_hdmi->hpd_gpio;
Hans Verkuil10ee2752017-07-16 12:48:03 +02001276 goto err_unprepare_hsm;
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001277 }
Eric Anholt0b06e0a2016-02-29 17:53:01 -08001278
Maxime Ripard3408cc22020-09-03 10:01:14 +02001279 vc4_hdmi->hpd_active_low = hpd_gpio_flags & OF_GPIO_ACTIVE_LOW;
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001280 }
1281
Maxime Ripard3408cc22020-09-03 10:01:14 +02001282 vc4->hdmi = vc4_hdmi;
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001283
Hans Verkuil10ee2752017-07-16 12:48:03 +02001284 /* HDMI core must be enabled. */
1285 if (!(HD_READ(VC4_HD_M_CTL) & VC4_HD_M_ENABLE)) {
1286 HD_WRITE(VC4_HD_M_CTL, VC4_HD_M_SW_RST);
1287 udelay(1);
1288 HD_WRITE(VC4_HD_M_CTL, 0);
1289
1290 HD_WRITE(VC4_HD_M_CTL, VC4_HD_M_ENABLE);
1291 }
Boris Brezillon4f6e3d62017-04-11 18:39:25 +02001292 pm_runtime_enable(dev);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001293
Maxime Ripardc98c85b2020-09-03 10:01:12 +02001294 drm_simple_encoder_init(drm, encoder, DRM_MODE_ENCODER_TMDS);
1295 drm_encoder_helper_add(encoder, &vc4_hdmi_encoder_helper_funcs);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001296
Maxime Ripard3408cc22020-09-03 10:01:14 +02001297 ret = vc4_hdmi_connector_init(drm, vc4_hdmi);
Maxime Ripardc98c85b2020-09-03 10:01:12 +02001298 if (ret)
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001299 goto err_destroy_encoder;
Maxime Ripardc98c85b2020-09-03 10:01:12 +02001300
Hans Verkuil15b45112017-07-16 12:48:04 +02001301#ifdef CONFIG_DRM_VC4_HDMI_CEC
Maxime Ripard3408cc22020-09-03 10:01:14 +02001302 vc4_hdmi->cec_adap = cec_allocate_adapter(&vc4_hdmi_cec_adap_ops,
Maxime Ripard66bf1c32020-09-03 10:01:18 +02001303 vc4_hdmi, "vc4",
Maxime Ripard3408cc22020-09-03 10:01:14 +02001304 CEC_CAP_DEFAULTS |
1305 CEC_CAP_CONNECTOR_INFO, 1);
1306 ret = PTR_ERR_OR_ZERO(vc4_hdmi->cec_adap);
Hans Verkuil15b45112017-07-16 12:48:04 +02001307 if (ret < 0)
1308 goto err_destroy_conn;
Dariusz Marcinkiewicz66c2dee2019-08-23 13:24:25 +02001309
Maxime Ripard3408cc22020-09-03 10:01:14 +02001310 cec_fill_conn_info_from_drm(&conn_info, &vc4_hdmi->connector.base);
1311 cec_s_conn_info(vc4_hdmi->cec_adap, &conn_info);
Dariusz Marcinkiewicz66c2dee2019-08-23 13:24:25 +02001312
Hans Verkuil15b45112017-07-16 12:48:04 +02001313 HDMI_WRITE(VC4_HDMI_CPU_MASK_SET, 0xffffffff);
1314 value = HDMI_READ(VC4_HDMI_CEC_CNTRL_1);
1315 value &= ~VC4_HDMI_CEC_DIV_CLK_CNT_MASK;
1316 /*
1317 * Set the logical address to Unregistered and set the clock
1318 * divider: the hsm_clock rate and this divider setting will
1319 * give a 40 kHz CEC clock.
1320 */
1321 value |= VC4_HDMI_CEC_ADDR_MASK |
1322 (4091 << VC4_HDMI_CEC_DIV_CLK_CNT_SHIFT);
1323 HDMI_WRITE(VC4_HDMI_CEC_CNTRL_1, value);
1324 ret = devm_request_threaded_irq(dev, platform_get_irq(pdev, 0),
1325 vc4_cec_irq_handler,
1326 vc4_cec_irq_handler_thread, 0,
Maxime Ripard66bf1c32020-09-03 10:01:18 +02001327 "vc4 hdmi cec", vc4_hdmi);
Hans Verkuil15b45112017-07-16 12:48:04 +02001328 if (ret)
1329 goto err_delete_cec_adap;
Maxime Ripard3408cc22020-09-03 10:01:14 +02001330 ret = cec_register_adapter(vc4_hdmi->cec_adap, dev);
Hans Verkuil15b45112017-07-16 12:48:04 +02001331 if (ret < 0)
1332 goto err_delete_cec_adap;
1333#endif
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001334
Maxime Ripard3408cc22020-09-03 10:01:14 +02001335 ret = vc4_hdmi_audio_init(vc4_hdmi);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001336 if (ret)
1337 goto err_destroy_encoder;
1338
Maxime Ripard3408cc22020-09-03 10:01:14 +02001339 vc4_debugfs_add_file(drm, "hdmi_regs", vc4_hdmi_debugfs_regs, vc4_hdmi);
Eric Anholtc9be8042019-04-01 11:35:58 -07001340
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001341 return 0;
1342
Hans Verkuil15b45112017-07-16 12:48:04 +02001343#ifdef CONFIG_DRM_VC4_HDMI_CEC
1344err_delete_cec_adap:
Maxime Ripard3408cc22020-09-03 10:01:14 +02001345 cec_delete_adapter(vc4_hdmi->cec_adap);
Hans Verkuil15b45112017-07-16 12:48:04 +02001346err_destroy_conn:
Maxime Ripard3408cc22020-09-03 10:01:14 +02001347 vc4_hdmi_connector_destroy(&vc4_hdmi->connector.base);
Hans Verkuil15b45112017-07-16 12:48:04 +02001348#endif
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001349err_destroy_encoder:
Maxime Ripardc98c85b2020-09-03 10:01:12 +02001350 drm_encoder_cleanup(encoder);
Hans Verkuil10ee2752017-07-16 12:48:03 +02001351err_unprepare_hsm:
Maxime Ripard3408cc22020-09-03 10:01:14 +02001352 clk_disable_unprepare(vc4_hdmi->hsm_clock);
Boris Brezillon4f6e3d62017-04-11 18:39:25 +02001353 pm_runtime_disable(dev);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001354err_put_i2c:
Maxime Ripard3408cc22020-09-03 10:01:14 +02001355 put_device(&vc4_hdmi->ddc->dev);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001356
1357 return ret;
1358}
1359
1360static void vc4_hdmi_unbind(struct device *dev, struct device *master,
1361 void *data)
1362{
1363 struct drm_device *drm = dev_get_drvdata(master);
1364 struct vc4_dev *vc4 = drm->dev_private;
Maxime Ripard47c167b2020-09-03 10:01:19 +02001365 struct vc4_hdmi *vc4_hdmi;
1366
1367 /*
1368 * ASoC makes it a bit hard to retrieve a pointer to the
1369 * vc4_hdmi structure. Registering the card will overwrite our
1370 * device drvdata with a pointer to the snd_soc_card structure,
1371 * which can then be used to retrieve whatever drvdata we want
1372 * to associate.
1373 *
1374 * However, that doesn't fly in the case where we wouldn't
1375 * register an ASoC card (because of an old DT that is missing
1376 * the dmas properties for example), then the card isn't
1377 * registered and the device drvdata wouldn't be set.
1378 *
1379 * We can deal with both cases by making sure a snd_soc_card
1380 * pointer and a vc4_hdmi structure are pointing to the same
1381 * memory address, so we can treat them indistinctly without any
1382 * issue.
1383 */
1384 BUILD_BUG_ON(offsetof(struct vc4_hdmi_audio, card) != 0);
1385 BUILD_BUG_ON(offsetof(struct vc4_hdmi, audio) != 0);
1386 vc4_hdmi = dev_get_drvdata(dev);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001387
Maxime Ripard3408cc22020-09-03 10:01:14 +02001388 cec_unregister_adapter(vc4_hdmi->cec_adap);
1389 vc4_hdmi_connector_destroy(&vc4_hdmi->connector.base);
1390 drm_encoder_cleanup(&vc4_hdmi->encoder.base.base);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001391
Maxime Ripard3408cc22020-09-03 10:01:14 +02001392 clk_disable_unprepare(vc4_hdmi->hsm_clock);
Boris Brezillon4f6e3d62017-04-11 18:39:25 +02001393 pm_runtime_disable(dev);
1394
Maxime Ripard3408cc22020-09-03 10:01:14 +02001395 put_device(&vc4_hdmi->ddc->dev);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001396
1397 vc4->hdmi = NULL;
1398}
1399
1400static const struct component_ops vc4_hdmi_ops = {
1401 .bind = vc4_hdmi_bind,
1402 .unbind = vc4_hdmi_unbind,
1403};
1404
1405static int vc4_hdmi_dev_probe(struct platform_device *pdev)
1406{
1407 return component_add(&pdev->dev, &vc4_hdmi_ops);
1408}
1409
1410static int vc4_hdmi_dev_remove(struct platform_device *pdev)
1411{
1412 component_del(&pdev->dev, &vc4_hdmi_ops);
1413 return 0;
1414}
1415
1416static const struct of_device_id vc4_hdmi_dt_match[] = {
1417 { .compatible = "brcm,bcm2835-hdmi" },
1418 {}
1419};
1420
1421struct platform_driver vc4_hdmi_driver = {
1422 .probe = vc4_hdmi_dev_probe,
1423 .remove = vc4_hdmi_dev_remove,
1424 .driver = {
1425 .name = "vc4_hdmi",
1426 .of_match_table = vc4_hdmi_dt_match,
1427 },
1428};