blob: 38c9172cfe52ca35af3ca4506d48d7695f0a6d84 [file] [log] [blame]
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001/*
2 * Copyright (C) 2015 Broadcom
3 * Copyright (c) 2014 The Linux Foundation. All rights reserved.
4 * Copyright (C) 2013 Red Hat
5 * Author: Rob Clark <robdclark@gmail.com>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published by
9 * the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20/**
21 * DOC: VC4 Falcon HDMI module
22 *
Eric Anholtf6c01532017-02-27 12:11:43 -080023 * The HDMI core has a state machine and a PHY. On BCM2835, most of
24 * the unit operates off of the HSM clock from CPRMAN. It also
25 * internally uses the PLLH_PIX clock for the PHY.
26 *
27 * HDMI infoframes are kept within a small packet ram, where each
28 * packet can be individually enabled for including in a frame.
29 *
30 * HDMI audio is implemented entirely within the HDMI IP block. A
31 * register in the HDMI encoder takes SPDIF frames from the DMA engine
32 * and transfers them over an internal MAI (multi-channel audio
33 * interconnect) bus to the encoder side for insertion into the video
34 * blank regions.
35 *
36 * The driver's HDMI encoder does not yet support power management.
37 * The HDMI encoder's power domain and the HSM/pixel clocks are kept
38 * continuously running, and only the HDMI logic and packet ram are
39 * powered off/on at disable/enable time.
40 *
41 * The driver does not yet support CEC control, though the HDMI
42 * encoder block has CEC support.
Eric Anholtc8b75bc2015-03-02 13:01:12 -080043 */
44
Masahiro Yamadab7e8e252017-05-18 13:29:38 +090045#include <drm/drm_atomic_helper.h>
Masahiro Yamadab7e8e252017-05-18 13:29:38 +090046#include <drm/drm_edid.h>
Daniel Vetterfcd70cd2019-01-17 22:03:34 +010047#include <drm/drm_probe_helper.h>
Masahiro Yamadab7e8e252017-05-18 13:29:38 +090048#include <linux/clk.h>
49#include <linux/component.h>
50#include <linux/i2c.h>
51#include <linux/of_address.h>
52#include <linux/of_gpio.h>
53#include <linux/of_platform.h>
54#include <linux/pm_runtime.h>
55#include <linux/rational.h>
56#include <sound/dmaengine_pcm.h>
57#include <sound/pcm_drm_eld.h>
58#include <sound/pcm_params.h>
59#include <sound/soc.h>
Hans Verkuil15b45112017-07-16 12:48:04 +020060#include "media/cec.h"
Eric Anholtc8b75bc2015-03-02 13:01:12 -080061#include "vc4_drv.h"
62#include "vc4_regs.h"
63
Hans Verkuil15b45112017-07-16 12:48:04 +020064#define HSM_CLOCK_FREQ 163682864
65#define CEC_CLOCK_FREQ 40000
66#define CEC_CLOCK_DIV (HSM_CLOCK_FREQ / CEC_CLOCK_FREQ)
67
Eric Anholtbb7d7852017-02-27 12:28:02 -080068/* HDMI audio information */
69struct vc4_hdmi_audio {
70 struct snd_soc_card card;
71 struct snd_soc_dai_link link;
72 int samplerate;
73 int channels;
74 struct snd_dmaengine_dai_dma_data dma_data;
75 struct snd_pcm_substream *substream;
76};
77
Eric Anholtc8b75bc2015-03-02 13:01:12 -080078/* General HDMI hardware state. */
79struct vc4_hdmi {
80 struct platform_device *pdev;
81
82 struct drm_encoder *encoder;
83 struct drm_connector *connector;
84
Eric Anholtbb7d7852017-02-27 12:28:02 -080085 struct vc4_hdmi_audio audio;
86
Eric Anholtc8b75bc2015-03-02 13:01:12 -080087 struct i2c_adapter *ddc;
88 void __iomem *hdmicore_regs;
89 void __iomem *hd_regs;
90 int hpd_gpio;
Eric Anholt0b06e0a2016-02-29 17:53:01 -080091 bool hpd_active_low;
Eric Anholtc8b75bc2015-03-02 13:01:12 -080092
Hans Verkuil15b45112017-07-16 12:48:04 +020093 struct cec_adapter *cec_adap;
94 struct cec_msg cec_rx_msg;
95 bool cec_tx_ok;
96 bool cec_irq_was_rx;
97
Eric Anholtc8b75bc2015-03-02 13:01:12 -080098 struct clk *pixel_clock;
99 struct clk *hsm_clock;
Eric Anholt30517192019-02-20 13:03:38 -0800100
101 struct debugfs_regset32 hdmi_regset;
102 struct debugfs_regset32 hd_regset;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800103};
104
105#define HDMI_READ(offset) readl(vc4->hdmi->hdmicore_regs + offset)
106#define HDMI_WRITE(offset, val) writel(val, vc4->hdmi->hdmicore_regs + offset)
107#define HD_READ(offset) readl(vc4->hdmi->hd_regs + offset)
108#define HD_WRITE(offset, val) writel(val, vc4->hdmi->hd_regs + offset)
109
110/* VC4 HDMI encoder KMS struct */
111struct vc4_hdmi_encoder {
112 struct vc4_encoder base;
113 bool hdmi_monitor;
Eric Anholt21317b32016-09-29 15:34:43 -0700114 bool limited_rgb_range;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800115};
116
117static inline struct vc4_hdmi_encoder *
118to_vc4_hdmi_encoder(struct drm_encoder *encoder)
119{
120 return container_of(encoder, struct vc4_hdmi_encoder, base.base);
121}
122
123/* VC4 HDMI connector KMS struct */
124struct vc4_hdmi_connector {
125 struct drm_connector base;
126
127 /* Since the connector is attached to just the one encoder,
128 * this is the reference to it so we can do the best_encoder()
129 * hook.
130 */
131 struct drm_encoder *encoder;
132};
133
134static inline struct vc4_hdmi_connector *
135to_vc4_hdmi_connector(struct drm_connector *connector)
136{
137 return container_of(connector, struct vc4_hdmi_connector, base);
138}
139
Eric Anholt30517192019-02-20 13:03:38 -0800140static const struct debugfs_reg32 hdmi_regs[] = {
141 VC4_REG32(VC4_HDMI_CORE_REV),
142 VC4_REG32(VC4_HDMI_SW_RESET_CONTROL),
143 VC4_REG32(VC4_HDMI_HOTPLUG_INT),
144 VC4_REG32(VC4_HDMI_HOTPLUG),
145 VC4_REG32(VC4_HDMI_MAI_CHANNEL_MAP),
146 VC4_REG32(VC4_HDMI_MAI_CONFIG),
147 VC4_REG32(VC4_HDMI_MAI_FORMAT),
148 VC4_REG32(VC4_HDMI_AUDIO_PACKET_CONFIG),
149 VC4_REG32(VC4_HDMI_RAM_PACKET_CONFIG),
150 VC4_REG32(VC4_HDMI_HORZA),
151 VC4_REG32(VC4_HDMI_HORZB),
152 VC4_REG32(VC4_HDMI_FIFO_CTL),
153 VC4_REG32(VC4_HDMI_SCHEDULER_CONTROL),
154 VC4_REG32(VC4_HDMI_VERTA0),
155 VC4_REG32(VC4_HDMI_VERTA1),
156 VC4_REG32(VC4_HDMI_VERTB0),
157 VC4_REG32(VC4_HDMI_VERTB1),
158 VC4_REG32(VC4_HDMI_TX_PHY_RESET_CTL),
159 VC4_REG32(VC4_HDMI_TX_PHY_CTL0),
Hans Verkuil15b45112017-07-16 12:48:04 +0200160
Eric Anholt30517192019-02-20 13:03:38 -0800161 VC4_REG32(VC4_HDMI_CEC_CNTRL_1),
162 VC4_REG32(VC4_HDMI_CEC_CNTRL_2),
163 VC4_REG32(VC4_HDMI_CEC_CNTRL_3),
164 VC4_REG32(VC4_HDMI_CEC_CNTRL_4),
165 VC4_REG32(VC4_HDMI_CEC_CNTRL_5),
166 VC4_REG32(VC4_HDMI_CPU_STATUS),
167 VC4_REG32(VC4_HDMI_CPU_MASK_STATUS),
Hans Verkuil15b45112017-07-16 12:48:04 +0200168
Eric Anholt30517192019-02-20 13:03:38 -0800169 VC4_REG32(VC4_HDMI_CEC_RX_DATA_1),
170 VC4_REG32(VC4_HDMI_CEC_RX_DATA_2),
171 VC4_REG32(VC4_HDMI_CEC_RX_DATA_3),
172 VC4_REG32(VC4_HDMI_CEC_RX_DATA_4),
173 VC4_REG32(VC4_HDMI_CEC_TX_DATA_1),
174 VC4_REG32(VC4_HDMI_CEC_TX_DATA_2),
175 VC4_REG32(VC4_HDMI_CEC_TX_DATA_3),
176 VC4_REG32(VC4_HDMI_CEC_TX_DATA_4),
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800177};
178
Eric Anholt30517192019-02-20 13:03:38 -0800179static const struct debugfs_reg32 hd_regs[] = {
180 VC4_REG32(VC4_HD_M_CTL),
181 VC4_REG32(VC4_HD_MAI_CTL),
182 VC4_REG32(VC4_HD_MAI_THR),
183 VC4_REG32(VC4_HD_MAI_FMT),
184 VC4_REG32(VC4_HD_MAI_SMP),
185 VC4_REG32(VC4_HD_VID_CTL),
186 VC4_REG32(VC4_HD_CSC_CTL),
187 VC4_REG32(VC4_HD_FRAME_COUNT),
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800188};
189
190#ifdef CONFIG_DEBUG_FS
191int vc4_hdmi_debugfs_regs(struct seq_file *m, void *unused)
192{
193 struct drm_info_node *node = (struct drm_info_node *)m->private;
194 struct drm_device *dev = node->minor->dev;
195 struct vc4_dev *vc4 = to_vc4_dev(dev);
Eric Anholt30517192019-02-20 13:03:38 -0800196 struct vc4_hdmi *hdmi = vc4->hdmi;
197 struct drm_printer p = drm_seq_file_printer(m);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800198
Eric Anholt30517192019-02-20 13:03:38 -0800199 drm_print_regset32(&p, &hdmi->hdmi_regset);
200 drm_print_regset32(&p, &hdmi->hd_regset);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800201
202 return 0;
203}
204#endif /* CONFIG_DEBUG_FS */
205
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800206static enum drm_connector_status
207vc4_hdmi_connector_detect(struct drm_connector *connector, bool force)
208{
209 struct drm_device *dev = connector->dev;
210 struct vc4_dev *vc4 = to_vc4_dev(dev);
211
212 if (vc4->hdmi->hpd_gpio) {
Eric Anholt0b06e0a2016-02-29 17:53:01 -0800213 if (gpio_get_value_cansleep(vc4->hdmi->hpd_gpio) ^
214 vc4->hdmi->hpd_active_low)
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800215 return connector_status_connected;
Hans Verkuil15b45112017-07-16 12:48:04 +0200216 cec_phys_addr_invalidate(vc4->hdmi->cec_adap);
217 return connector_status_disconnected;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800218 }
219
Eric Anholt9d44abb2016-09-14 19:21:29 +0100220 if (drm_probe_ddc(vc4->hdmi->ddc))
221 return connector_status_connected;
222
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800223 if (HDMI_READ(VC4_HDMI_HOTPLUG) & VC4_HDMI_HOTPLUG_CONNECTED)
224 return connector_status_connected;
Hans Verkuil15b45112017-07-16 12:48:04 +0200225 cec_phys_addr_invalidate(vc4->hdmi->cec_adap);
226 return connector_status_disconnected;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800227}
228
229static void vc4_hdmi_connector_destroy(struct drm_connector *connector)
230{
231 drm_connector_unregister(connector);
232 drm_connector_cleanup(connector);
233}
234
235static int vc4_hdmi_connector_get_modes(struct drm_connector *connector)
236{
237 struct vc4_hdmi_connector *vc4_connector =
238 to_vc4_hdmi_connector(connector);
239 struct drm_encoder *encoder = vc4_connector->encoder;
240 struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
241 struct drm_device *dev = connector->dev;
242 struct vc4_dev *vc4 = to_vc4_dev(dev);
243 int ret = 0;
244 struct edid *edid;
245
246 edid = drm_get_edid(connector, vc4->hdmi->ddc);
Hans Verkuil15b45112017-07-16 12:48:04 +0200247 cec_s_phys_addr_from_edid(vc4->hdmi->cec_adap, edid);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800248 if (!edid)
249 return -ENODEV;
250
251 vc4_encoder->hdmi_monitor = drm_detect_hdmi_monitor(edid);
Eric Anholt21317b32016-09-29 15:34:43 -0700252
Daniel Vetterc555f022018-07-09 10:40:06 +0200253 drm_connector_update_edid_property(connector, edid);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800254 ret = drm_add_edid_modes(connector, edid);
Eric Anholt5afe0e62017-08-08 13:56:05 -0700255 kfree(edid);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800256
257 return ret;
258}
259
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800260static const struct drm_connector_funcs vc4_hdmi_connector_funcs = {
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800261 .detect = vc4_hdmi_connector_detect,
Eric Anholt682e62c2016-09-28 17:30:25 -0700262 .fill_modes = drm_helper_probe_single_connector_modes,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800263 .destroy = vc4_hdmi_connector_destroy,
264 .reset = drm_atomic_helper_connector_reset,
265 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
266 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
267};
268
269static const struct drm_connector_helper_funcs vc4_hdmi_connector_helper_funcs = {
270 .get_modes = vc4_hdmi_connector_get_modes,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800271};
272
273static struct drm_connector *vc4_hdmi_connector_init(struct drm_device *dev,
274 struct drm_encoder *encoder)
275{
Colin Ian King56630772017-09-08 15:05:04 +0100276 struct drm_connector *connector;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800277 struct vc4_hdmi_connector *hdmi_connector;
Boris Brezillondb999532018-12-06 15:24:39 +0100278 int ret;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800279
280 hdmi_connector = devm_kzalloc(dev->dev, sizeof(*hdmi_connector),
281 GFP_KERNEL);
Colin Ian King56630772017-09-08 15:05:04 +0100282 if (!hdmi_connector)
283 return ERR_PTR(-ENOMEM);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800284 connector = &hdmi_connector->base;
285
286 hdmi_connector->encoder = encoder;
287
288 drm_connector_init(dev, connector, &vc4_hdmi_connector_funcs,
289 DRM_MODE_CONNECTOR_HDMIA);
290 drm_connector_helper_add(connector, &vc4_hdmi_connector_helper_funcs);
291
Boris Brezillondb999532018-12-06 15:24:39 +0100292 /* Create and attach TV margin props to this connector. */
293 ret = drm_mode_create_tv_margin_properties(dev);
294 if (ret)
295 return ERR_PTR(ret);
296
297 drm_connector_attach_tv_margin_properties(connector);
298
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800299 connector->polled = (DRM_CONNECTOR_POLL_CONNECT |
300 DRM_CONNECTOR_POLL_DISCONNECT);
301
Mario Kleineracc1be12016-07-19 20:58:58 +0200302 connector->interlace_allowed = 1;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800303 connector->doublescan_allowed = 0;
304
Daniel Vettercde4c442018-07-09 10:40:07 +0200305 drm_connector_attach_encoder(connector, encoder);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800306
307 return connector;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800308}
309
310static void vc4_hdmi_encoder_destroy(struct drm_encoder *encoder)
311{
312 drm_encoder_cleanup(encoder);
313}
314
315static const struct drm_encoder_funcs vc4_hdmi_encoder_funcs = {
316 .destroy = vc4_hdmi_encoder_destroy,
317};
318
Eric Anholt21317b32016-09-29 15:34:43 -0700319static int vc4_hdmi_stop_packet(struct drm_encoder *encoder,
320 enum hdmi_infoframe_type type)
321{
322 struct drm_device *dev = encoder->dev;
323 struct vc4_dev *vc4 = to_vc4_dev(dev);
324 u32 packet_id = type - 0x80;
325
326 HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG,
327 HDMI_READ(VC4_HDMI_RAM_PACKET_CONFIG) & ~BIT(packet_id));
328
329 return wait_for(!(HDMI_READ(VC4_HDMI_RAM_PACKET_STATUS) &
330 BIT(packet_id)), 100);
331}
332
333static void vc4_hdmi_write_infoframe(struct drm_encoder *encoder,
334 union hdmi_infoframe *frame)
335{
336 struct drm_device *dev = encoder->dev;
337 struct vc4_dev *vc4 = to_vc4_dev(dev);
338 u32 packet_id = frame->any.type - 0x80;
Eric Anholtbb7d7852017-02-27 12:28:02 -0800339 u32 packet_reg = VC4_HDMI_RAM_PACKET(packet_id);
Eric Anholt21317b32016-09-29 15:34:43 -0700340 uint8_t buffer[VC4_HDMI_PACKET_STRIDE];
341 ssize_t len, i;
342 int ret;
343
344 WARN_ONCE(!(HDMI_READ(VC4_HDMI_RAM_PACKET_CONFIG) &
345 VC4_HDMI_RAM_PACKET_ENABLE),
346 "Packet RAM has to be on to store the packet.");
347
348 len = hdmi_infoframe_pack(frame, buffer, sizeof(buffer));
349 if (len < 0)
350 return;
351
352 ret = vc4_hdmi_stop_packet(encoder, frame->any.type);
353 if (ret) {
354 DRM_ERROR("Failed to wait for infoframe to go idle: %d\n", ret);
355 return;
356 }
357
358 for (i = 0; i < len; i += 7) {
359 HDMI_WRITE(packet_reg,
360 buffer[i + 0] << 0 |
361 buffer[i + 1] << 8 |
362 buffer[i + 2] << 16);
363 packet_reg += 4;
364
365 HDMI_WRITE(packet_reg,
366 buffer[i + 3] << 0 |
367 buffer[i + 4] << 8 |
368 buffer[i + 5] << 16 |
369 buffer[i + 6] << 24);
370 packet_reg += 4;
371 }
372
373 HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG,
374 HDMI_READ(VC4_HDMI_RAM_PACKET_CONFIG) | BIT(packet_id));
375 ret = wait_for((HDMI_READ(VC4_HDMI_RAM_PACKET_STATUS) &
376 BIT(packet_id)), 100);
377 if (ret)
378 DRM_ERROR("Failed to wait for infoframe to start: %d\n", ret);
379}
380
381static void vc4_hdmi_set_avi_infoframe(struct drm_encoder *encoder)
382{
383 struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
Boris Brezillondb999532018-12-06 15:24:39 +0100384 struct vc4_dev *vc4 = encoder->dev->dev_private;
385 struct vc4_hdmi *hdmi = vc4->hdmi;
386 struct drm_connector_state *cstate = hdmi->connector->state;
Eric Anholt21317b32016-09-29 15:34:43 -0700387 struct drm_crtc *crtc = encoder->crtc;
388 const struct drm_display_mode *mode = &crtc->state->adjusted_mode;
389 union hdmi_infoframe frame;
390 int ret;
391
Ville Syrjälä13d0add2019-01-08 19:28:25 +0200392 ret = drm_hdmi_avi_infoframe_from_display_mode(&frame.avi,
393 hdmi->connector, mode);
Eric Anholt21317b32016-09-29 15:34:43 -0700394 if (ret < 0) {
395 DRM_ERROR("couldn't fill AVI infoframe\n");
396 return;
397 }
398
Ville Syrjälä13d0add2019-01-08 19:28:25 +0200399 drm_hdmi_avi_infoframe_quant_range(&frame.avi,
400 hdmi->connector, mode,
Ville Syrjäläa2ce26f2017-01-11 14:57:23 +0200401 vc4_encoder->limited_rgb_range ?
402 HDMI_QUANTIZATION_RANGE_LIMITED :
Ville Syrjälä1581b2d2019-01-08 19:28:28 +0200403 HDMI_QUANTIZATION_RANGE_FULL);
Eric Anholt21317b32016-09-29 15:34:43 -0700404
Boris Brezillondb999532018-12-06 15:24:39 +0100405 frame.avi.right_bar = cstate->tv.margins.right;
406 frame.avi.left_bar = cstate->tv.margins.left;
407 frame.avi.top_bar = cstate->tv.margins.top;
408 frame.avi.bottom_bar = cstate->tv.margins.bottom;
409
Eric Anholt21317b32016-09-29 15:34:43 -0700410 vc4_hdmi_write_infoframe(encoder, &frame);
411}
412
413static void vc4_hdmi_set_spd_infoframe(struct drm_encoder *encoder)
414{
415 union hdmi_infoframe frame;
416 int ret;
417
418 ret = hdmi_spd_infoframe_init(&frame.spd, "Broadcom", "Videocore");
419 if (ret < 0) {
420 DRM_ERROR("couldn't fill SPD infoframe\n");
421 return;
422 }
423
424 frame.spd.sdi = HDMI_SPD_SDI_PC;
425
426 vc4_hdmi_write_infoframe(encoder, &frame);
427}
428
Eric Anholtbb7d7852017-02-27 12:28:02 -0800429static void vc4_hdmi_set_audio_infoframe(struct drm_encoder *encoder)
430{
431 struct drm_device *drm = encoder->dev;
432 struct vc4_dev *vc4 = drm->dev_private;
433 struct vc4_hdmi *hdmi = vc4->hdmi;
434 union hdmi_infoframe frame;
435 int ret;
436
437 ret = hdmi_audio_infoframe_init(&frame.audio);
438
439 frame.audio.coding_type = HDMI_AUDIO_CODING_TYPE_STREAM;
440 frame.audio.sample_frequency = HDMI_AUDIO_SAMPLE_FREQUENCY_STREAM;
441 frame.audio.sample_size = HDMI_AUDIO_SAMPLE_SIZE_STREAM;
442 frame.audio.channels = hdmi->audio.channels;
443
444 vc4_hdmi_write_infoframe(encoder, &frame);
445}
446
Eric Anholt21317b32016-09-29 15:34:43 -0700447static void vc4_hdmi_set_infoframes(struct drm_encoder *encoder)
448{
449 vc4_hdmi_set_avi_infoframe(encoder);
450 vc4_hdmi_set_spd_infoframe(encoder);
451}
452
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200453static void vc4_hdmi_encoder_disable(struct drm_encoder *encoder)
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800454{
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200455 struct drm_device *dev = encoder->dev;
456 struct vc4_dev *vc4 = to_vc4_dev(dev);
457 struct vc4_hdmi *hdmi = vc4->hdmi;
458 int ret;
459
460 HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG, 0);
461
462 HDMI_WRITE(VC4_HDMI_TX_PHY_RESET_CTL, 0xf << 16);
463 HD_WRITE(VC4_HD_VID_CTL,
464 HD_READ(VC4_HD_VID_CTL) & ~VC4_HD_VID_CTL_ENABLE);
465
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200466 clk_disable_unprepare(hdmi->pixel_clock);
467
468 ret = pm_runtime_put(&hdmi->pdev->dev);
469 if (ret < 0)
470 DRM_ERROR("Failed to release power domain: %d\n", ret);
471}
472
473static void vc4_hdmi_encoder_enable(struct drm_encoder *encoder)
474{
475 struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
Eric Anholt6e1cbba2016-09-16 10:59:45 +0100476 struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800477 struct drm_device *dev = encoder->dev;
478 struct vc4_dev *vc4 = to_vc4_dev(dev);
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200479 struct vc4_hdmi *hdmi = vc4->hdmi;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800480 bool debug_dump_regs = false;
481 bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
482 bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
Eric Anholt682e62c2016-09-28 17:30:25 -0700483 bool interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE;
Eric Anholtdfccd932016-09-29 15:34:44 -0700484 u32 pixel_rep = (mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1;
Eric Anholt682e62c2016-09-28 17:30:25 -0700485 u32 verta = (VC4_SET_FIELD(mode->crtc_vsync_end - mode->crtc_vsync_start,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800486 VC4_HDMI_VERTA_VSP) |
Eric Anholt682e62c2016-09-28 17:30:25 -0700487 VC4_SET_FIELD(mode->crtc_vsync_start - mode->crtc_vdisplay,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800488 VC4_HDMI_VERTA_VFP) |
Eric Anholt682e62c2016-09-28 17:30:25 -0700489 VC4_SET_FIELD(mode->crtc_vdisplay, VC4_HDMI_VERTA_VAL));
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800490 u32 vertb = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) |
Eric Anholt682e62c2016-09-28 17:30:25 -0700491 VC4_SET_FIELD(mode->crtc_vtotal - mode->crtc_vsync_end,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800492 VC4_HDMI_VERTB_VBP));
Eric Anholt682e62c2016-09-28 17:30:25 -0700493 u32 vertb_even = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) |
494 VC4_SET_FIELD(mode->crtc_vtotal -
495 mode->crtc_vsync_end -
496 interlaced,
497 VC4_HDMI_VERTB_VBP));
Eric Anholt6e1cbba2016-09-16 10:59:45 +0100498 u32 csc_ctl;
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200499 int ret;
500
501 ret = pm_runtime_get_sync(&hdmi->pdev->dev);
502 if (ret < 0) {
503 DRM_ERROR("Failed to retain power domain: %d\n", ret);
504 return;
505 }
506
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200507 ret = clk_set_rate(hdmi->pixel_clock,
508 mode->clock * 1000 *
509 ((mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1));
510 if (ret) {
511 DRM_ERROR("Failed to set pixel clock rate: %d\n", ret);
512 return;
513 }
514
515 ret = clk_prepare_enable(hdmi->pixel_clock);
516 if (ret) {
517 DRM_ERROR("Failed to turn on pixel clock: %d\n", ret);
518 return;
519 }
520
Boris Brezillon4f6e3d62017-04-11 18:39:25 +0200521 HDMI_WRITE(VC4_HDMI_SW_RESET_CONTROL,
522 VC4_HDMI_SW_RESET_HDMI |
523 VC4_HDMI_SW_RESET_FORMAT_DETECT);
524
525 HDMI_WRITE(VC4_HDMI_SW_RESET_CONTROL, 0);
526
527 /* PHY should be in reset, like
528 * vc4_hdmi_encoder_disable() does.
529 */
530 HDMI_WRITE(VC4_HDMI_TX_PHY_RESET_CTL, 0xf << 16);
531
532 HDMI_WRITE(VC4_HDMI_TX_PHY_RESET_CTL, 0);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800533
534 if (debug_dump_regs) {
Eric Anholt30517192019-02-20 13:03:38 -0800535 struct drm_printer p = drm_info_printer(&hdmi->pdev->dev);
536
537 dev_info(&hdmi->pdev->dev, "HDMI regs before:\n");
538 drm_print_regset32(&p, &hdmi->hdmi_regset);
539 drm_print_regset32(&p, &hdmi->hd_regset);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800540 }
541
542 HD_WRITE(VC4_HD_VID_CTL, 0);
543
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800544 HDMI_WRITE(VC4_HDMI_SCHEDULER_CONTROL,
545 HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) |
546 VC4_HDMI_SCHEDULER_CONTROL_MANUAL_FORMAT |
547 VC4_HDMI_SCHEDULER_CONTROL_IGNORE_VSYNC_PREDICTS);
548
549 HDMI_WRITE(VC4_HDMI_HORZA,
550 (vsync_pos ? VC4_HDMI_HORZA_VPOS : 0) |
551 (hsync_pos ? VC4_HDMI_HORZA_HPOS : 0) |
Eric Anholtdfccd932016-09-29 15:34:44 -0700552 VC4_SET_FIELD(mode->hdisplay * pixel_rep,
553 VC4_HDMI_HORZA_HAP));
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800554
555 HDMI_WRITE(VC4_HDMI_HORZB,
Eric Anholtdfccd932016-09-29 15:34:44 -0700556 VC4_SET_FIELD((mode->htotal -
557 mode->hsync_end) * pixel_rep,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800558 VC4_HDMI_HORZB_HBP) |
Eric Anholtdfccd932016-09-29 15:34:44 -0700559 VC4_SET_FIELD((mode->hsync_end -
560 mode->hsync_start) * pixel_rep,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800561 VC4_HDMI_HORZB_HSP) |
Eric Anholtdfccd932016-09-29 15:34:44 -0700562 VC4_SET_FIELD((mode->hsync_start -
563 mode->hdisplay) * pixel_rep,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800564 VC4_HDMI_HORZB_HFP));
565
566 HDMI_WRITE(VC4_HDMI_VERTA0, verta);
567 HDMI_WRITE(VC4_HDMI_VERTA1, verta);
568
Eric Anholt682e62c2016-09-28 17:30:25 -0700569 HDMI_WRITE(VC4_HDMI_VERTB0, vertb_even);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800570 HDMI_WRITE(VC4_HDMI_VERTB1, vertb);
571
572 HD_WRITE(VC4_HD_VID_CTL,
573 (vsync_pos ? 0 : VC4_HD_VID_CTL_VSYNC_LOW) |
574 (hsync_pos ? 0 : VC4_HD_VID_CTL_HSYNC_LOW));
575
Eric Anholt6e1cbba2016-09-16 10:59:45 +0100576 csc_ctl = VC4_SET_FIELD(VC4_HD_CSC_CTL_ORDER_BGR,
577 VC4_HD_CSC_CTL_ORDER);
578
Ville Syrjäläc8127cf02017-01-11 16:18:35 +0200579 if (vc4_encoder->hdmi_monitor &&
580 drm_default_rgb_quant_range(mode) ==
581 HDMI_QUANTIZATION_RANGE_LIMITED) {
Eric Anholt6e1cbba2016-09-16 10:59:45 +0100582 /* CEA VICs other than #1 requre limited range RGB
Eric Anholt21317b32016-09-29 15:34:43 -0700583 * output unless overridden by an AVI infoframe.
584 * Apply a colorspace conversion to squash 0-255 down
585 * to 16-235. The matrix here is:
Eric Anholt6e1cbba2016-09-16 10:59:45 +0100586 *
587 * [ 0 0 0.8594 16]
588 * [ 0 0.8594 0 16]
589 * [ 0.8594 0 0 16]
590 * [ 0 0 0 1]
591 */
592 csc_ctl |= VC4_HD_CSC_CTL_ENABLE;
593 csc_ctl |= VC4_HD_CSC_CTL_RGB2YCC;
594 csc_ctl |= VC4_SET_FIELD(VC4_HD_CSC_CTL_MODE_CUSTOM,
595 VC4_HD_CSC_CTL_MODE);
596
597 HD_WRITE(VC4_HD_CSC_12_11, (0x000 << 16) | 0x000);
598 HD_WRITE(VC4_HD_CSC_14_13, (0x100 << 16) | 0x6e0);
599 HD_WRITE(VC4_HD_CSC_22_21, (0x6e0 << 16) | 0x000);
600 HD_WRITE(VC4_HD_CSC_24_23, (0x100 << 16) | 0x000);
601 HD_WRITE(VC4_HD_CSC_32_31, (0x000 << 16) | 0x6e0);
602 HD_WRITE(VC4_HD_CSC_34_33, (0x100 << 16) | 0x000);
Eric Anholt21317b32016-09-29 15:34:43 -0700603 vc4_encoder->limited_rgb_range = true;
604 } else {
605 vc4_encoder->limited_rgb_range = false;
Eric Anholt6e1cbba2016-09-16 10:59:45 +0100606 }
607
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800608 /* The RGB order applies even when CSC is disabled. */
Eric Anholt6e1cbba2016-09-16 10:59:45 +0100609 HD_WRITE(VC4_HD_CSC_CTL, csc_ctl);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800610
611 HDMI_WRITE(VC4_HDMI_FIFO_CTL, VC4_HDMI_FIFO_CTL_MASTER_SLAVE_N);
612
613 if (debug_dump_regs) {
Eric Anholt30517192019-02-20 13:03:38 -0800614 struct drm_printer p = drm_info_printer(&hdmi->pdev->dev);
615
616 dev_info(&hdmi->pdev->dev, "HDMI regs after:\n");
617 drm_print_regset32(&p, &hdmi->hdmi_regset);
618 drm_print_regset32(&p, &hdmi->hd_regset);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800619 }
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800620
621 HD_WRITE(VC4_HD_VID_CTL,
622 HD_READ(VC4_HD_VID_CTL) |
623 VC4_HD_VID_CTL_ENABLE |
624 VC4_HD_VID_CTL_UNDERFLOW_ENABLE |
625 VC4_HD_VID_CTL_FRAME_COUNTER_RESET);
626
627 if (vc4_encoder->hdmi_monitor) {
628 HDMI_WRITE(VC4_HDMI_SCHEDULER_CONTROL,
629 HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) |
630 VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);
631
632 ret = wait_for(HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) &
Eric Anholt2b29bf12016-09-28 17:21:05 -0700633 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE, 1000);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800634 WARN_ONCE(ret, "Timeout waiting for "
635 "VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
636 } else {
637 HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG,
638 HDMI_READ(VC4_HDMI_RAM_PACKET_CONFIG) &
639 ~(VC4_HDMI_RAM_PACKET_ENABLE));
640 HDMI_WRITE(VC4_HDMI_SCHEDULER_CONTROL,
641 HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) &
642 ~VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);
643
644 ret = wait_for(!(HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) &
Eric Anholt2b29bf12016-09-28 17:21:05 -0700645 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE), 1000);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800646 WARN_ONCE(ret, "Timeout waiting for "
647 "!VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
648 }
649
650 if (vc4_encoder->hdmi_monitor) {
651 u32 drift;
652
653 WARN_ON(!(HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) &
654 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE));
655 HDMI_WRITE(VC4_HDMI_SCHEDULER_CONTROL,
656 HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) |
657 VC4_HDMI_SCHEDULER_CONTROL_VERT_ALWAYS_KEEPOUT);
658
Eric Anholt21317b32016-09-29 15:34:43 -0700659 HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG,
660 VC4_HDMI_RAM_PACKET_ENABLE);
661
662 vc4_hdmi_set_infoframes(encoder);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800663
664 drift = HDMI_READ(VC4_HDMI_FIFO_CTL);
665 drift &= VC4_HDMI_FIFO_VALID_WRITE_MASK;
666
667 HDMI_WRITE(VC4_HDMI_FIFO_CTL,
668 drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
669 HDMI_WRITE(VC4_HDMI_FIFO_CTL,
670 drift | VC4_HDMI_FIFO_CTL_RECENTER);
Stefan Wahrend8eb9de2018-02-24 13:38:14 +0100671 usleep_range(1000, 1100);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800672 HDMI_WRITE(VC4_HDMI_FIFO_CTL,
673 drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
674 HDMI_WRITE(VC4_HDMI_FIFO_CTL,
675 drift | VC4_HDMI_FIFO_CTL_RECENTER);
676
677 ret = wait_for(HDMI_READ(VC4_HDMI_FIFO_CTL) &
678 VC4_HDMI_FIFO_CTL_RECENTER_DONE, 1);
679 WARN_ONCE(ret, "Timeout waiting for "
680 "VC4_HDMI_FIFO_CTL_RECENTER_DONE");
681 }
682}
683
Eric Anholt32e823c2017-09-20 15:59:34 -0700684static enum drm_mode_status
685vc4_hdmi_encoder_mode_valid(struct drm_encoder *crtc,
686 const struct drm_display_mode *mode)
687{
688 /* HSM clock must be 108% of the pixel clock. Additionally,
689 * the AXI clock needs to be at least 25% of pixel clock, but
690 * HSM ends up being the limiting factor.
691 */
692 if (mode->clock > HSM_CLOCK_FREQ / (1000 * 108 / 100))
693 return MODE_CLOCK_HIGH;
694
695 return MODE_OK;
696}
697
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800698static const struct drm_encoder_helper_funcs vc4_hdmi_encoder_helper_funcs = {
Eric Anholt32e823c2017-09-20 15:59:34 -0700699 .mode_valid = vc4_hdmi_encoder_mode_valid,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800700 .disable = vc4_hdmi_encoder_disable,
701 .enable = vc4_hdmi_encoder_enable,
702};
703
Eric Anholtbb7d7852017-02-27 12:28:02 -0800704/* HDMI audio codec callbacks */
705static void vc4_hdmi_audio_set_mai_clock(struct vc4_hdmi *hdmi)
706{
707 struct drm_device *drm = hdmi->encoder->dev;
708 struct vc4_dev *vc4 = to_vc4_dev(drm);
709 u32 hsm_clock = clk_get_rate(hdmi->hsm_clock);
710 unsigned long n, m;
711
712 rational_best_approximation(hsm_clock, hdmi->audio.samplerate,
713 VC4_HD_MAI_SMP_N_MASK >>
714 VC4_HD_MAI_SMP_N_SHIFT,
715 (VC4_HD_MAI_SMP_M_MASK >>
716 VC4_HD_MAI_SMP_M_SHIFT) + 1,
717 &n, &m);
718
719 HD_WRITE(VC4_HD_MAI_SMP,
720 VC4_SET_FIELD(n, VC4_HD_MAI_SMP_N) |
721 VC4_SET_FIELD(m - 1, VC4_HD_MAI_SMP_M));
722}
723
724static void vc4_hdmi_set_n_cts(struct vc4_hdmi *hdmi)
725{
726 struct drm_encoder *encoder = hdmi->encoder;
727 struct drm_crtc *crtc = encoder->crtc;
728 struct drm_device *drm = encoder->dev;
729 struct vc4_dev *vc4 = to_vc4_dev(drm);
730 const struct drm_display_mode *mode = &crtc->state->adjusted_mode;
731 u32 samplerate = hdmi->audio.samplerate;
732 u32 n, cts;
733 u64 tmp;
734
735 n = 128 * samplerate / 1000;
736 tmp = (u64)(mode->clock * 1000) * n;
737 do_div(tmp, 128 * samplerate);
738 cts = tmp;
739
740 HDMI_WRITE(VC4_HDMI_CRP_CFG,
741 VC4_HDMI_CRP_CFG_EXTERNAL_CTS_EN |
742 VC4_SET_FIELD(n, VC4_HDMI_CRP_CFG_N));
743
744 /*
745 * We could get slightly more accurate clocks in some cases by
746 * providing a CTS_1 value. The two CTS values are alternated
747 * between based on the period fields
748 */
749 HDMI_WRITE(VC4_HDMI_CTS_0, cts);
750 HDMI_WRITE(VC4_HDMI_CTS_1, cts);
751}
752
753static inline struct vc4_hdmi *dai_to_hdmi(struct snd_soc_dai *dai)
754{
755 struct snd_soc_card *card = snd_soc_dai_get_drvdata(dai);
756
757 return snd_soc_card_get_drvdata(card);
758}
759
760static int vc4_hdmi_audio_startup(struct snd_pcm_substream *substream,
761 struct snd_soc_dai *dai)
762{
763 struct vc4_hdmi *hdmi = dai_to_hdmi(dai);
764 struct drm_encoder *encoder = hdmi->encoder;
765 struct vc4_dev *vc4 = to_vc4_dev(encoder->dev);
766 int ret;
767
768 if (hdmi->audio.substream && hdmi->audio.substream != substream)
769 return -EINVAL;
770
771 hdmi->audio.substream = substream;
772
773 /*
774 * If the HDMI encoder hasn't probed, or the encoder is
775 * currently in DVI mode, treat the codec dai as missing.
776 */
777 if (!encoder->crtc || !(HDMI_READ(VC4_HDMI_RAM_PACKET_CONFIG) &
778 VC4_HDMI_RAM_PACKET_ENABLE))
779 return -ENODEV;
780
781 ret = snd_pcm_hw_constraint_eld(substream->runtime,
782 hdmi->connector->eld);
783 if (ret)
784 return ret;
785
786 return 0;
787}
788
789static int vc4_hdmi_audio_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
790{
791 return 0;
792}
793
794static void vc4_hdmi_audio_reset(struct vc4_hdmi *hdmi)
795{
796 struct drm_encoder *encoder = hdmi->encoder;
797 struct drm_device *drm = encoder->dev;
798 struct device *dev = &hdmi->pdev->dev;
799 struct vc4_dev *vc4 = to_vc4_dev(drm);
800 int ret;
801
802 ret = vc4_hdmi_stop_packet(encoder, HDMI_INFOFRAME_TYPE_AUDIO);
803 if (ret)
804 dev_err(dev, "Failed to stop audio infoframe: %d\n", ret);
805
806 HD_WRITE(VC4_HD_MAI_CTL, VC4_HD_MAI_CTL_RESET);
807 HD_WRITE(VC4_HD_MAI_CTL, VC4_HD_MAI_CTL_ERRORF);
808 HD_WRITE(VC4_HD_MAI_CTL, VC4_HD_MAI_CTL_FLUSH);
809}
810
811static void vc4_hdmi_audio_shutdown(struct snd_pcm_substream *substream,
812 struct snd_soc_dai *dai)
813{
814 struct vc4_hdmi *hdmi = dai_to_hdmi(dai);
815
816 if (substream != hdmi->audio.substream)
817 return;
818
819 vc4_hdmi_audio_reset(hdmi);
820
821 hdmi->audio.substream = NULL;
822}
823
824/* HDMI audio codec callbacks */
825static int vc4_hdmi_audio_hw_params(struct snd_pcm_substream *substream,
826 struct snd_pcm_hw_params *params,
827 struct snd_soc_dai *dai)
828{
829 struct vc4_hdmi *hdmi = dai_to_hdmi(dai);
830 struct drm_encoder *encoder = hdmi->encoder;
831 struct drm_device *drm = encoder->dev;
832 struct device *dev = &hdmi->pdev->dev;
833 struct vc4_dev *vc4 = to_vc4_dev(drm);
834 u32 audio_packet_config, channel_mask;
835 u32 channel_map, i;
836
837 if (substream != hdmi->audio.substream)
838 return -EINVAL;
839
840 dev_dbg(dev, "%s: %u Hz, %d bit, %d channels\n", __func__,
841 params_rate(params), params_width(params),
842 params_channels(params));
843
844 hdmi->audio.channels = params_channels(params);
845 hdmi->audio.samplerate = params_rate(params);
846
847 HD_WRITE(VC4_HD_MAI_CTL,
848 VC4_HD_MAI_CTL_RESET |
849 VC4_HD_MAI_CTL_FLUSH |
850 VC4_HD_MAI_CTL_DLATE |
851 VC4_HD_MAI_CTL_ERRORE |
852 VC4_HD_MAI_CTL_ERRORF);
853
854 vc4_hdmi_audio_set_mai_clock(hdmi);
855
856 audio_packet_config =
857 VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_SAMPLE_FLAT |
858 VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_INACTIVE_CHANNELS |
859 VC4_SET_FIELD(0xf, VC4_HDMI_AUDIO_PACKET_B_FRAME_IDENTIFIER);
860
861 channel_mask = GENMASK(hdmi->audio.channels - 1, 0);
862 audio_packet_config |= VC4_SET_FIELD(channel_mask,
863 VC4_HDMI_AUDIO_PACKET_CEA_MASK);
864
865 /* Set the MAI threshold. This logic mimics the firmware's. */
866 if (hdmi->audio.samplerate > 96000) {
867 HD_WRITE(VC4_HD_MAI_THR,
868 VC4_SET_FIELD(0x12, VC4_HD_MAI_THR_DREQHIGH) |
869 VC4_SET_FIELD(0x12, VC4_HD_MAI_THR_DREQLOW));
870 } else if (hdmi->audio.samplerate > 48000) {
871 HD_WRITE(VC4_HD_MAI_THR,
872 VC4_SET_FIELD(0x14, VC4_HD_MAI_THR_DREQHIGH) |
873 VC4_SET_FIELD(0x12, VC4_HD_MAI_THR_DREQLOW));
874 } else {
875 HD_WRITE(VC4_HD_MAI_THR,
876 VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_PANICHIGH) |
877 VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_PANICLOW) |
878 VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_DREQHIGH) |
879 VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_DREQLOW));
880 }
881
882 HDMI_WRITE(VC4_HDMI_MAI_CONFIG,
883 VC4_HDMI_MAI_CONFIG_BIT_REVERSE |
884 VC4_SET_FIELD(channel_mask, VC4_HDMI_MAI_CHANNEL_MASK));
885
886 channel_map = 0;
887 for (i = 0; i < 8; i++) {
888 if (channel_mask & BIT(i))
889 channel_map |= i << (3 * i);
890 }
891
892 HDMI_WRITE(VC4_HDMI_MAI_CHANNEL_MAP, channel_map);
893 HDMI_WRITE(VC4_HDMI_AUDIO_PACKET_CONFIG, audio_packet_config);
894 vc4_hdmi_set_n_cts(hdmi);
895
896 return 0;
897}
898
899static int vc4_hdmi_audio_trigger(struct snd_pcm_substream *substream, int cmd,
900 struct snd_soc_dai *dai)
901{
902 struct vc4_hdmi *hdmi = dai_to_hdmi(dai);
903 struct drm_encoder *encoder = hdmi->encoder;
904 struct drm_device *drm = encoder->dev;
905 struct vc4_dev *vc4 = to_vc4_dev(drm);
906
907 switch (cmd) {
908 case SNDRV_PCM_TRIGGER_START:
909 vc4_hdmi_set_audio_infoframe(encoder);
910 HDMI_WRITE(VC4_HDMI_TX_PHY_CTL0,
911 HDMI_READ(VC4_HDMI_TX_PHY_CTL0) &
912 ~VC4_HDMI_TX_PHY_RNG_PWRDN);
913 HD_WRITE(VC4_HD_MAI_CTL,
914 VC4_SET_FIELD(hdmi->audio.channels,
915 VC4_HD_MAI_CTL_CHNUM) |
916 VC4_HD_MAI_CTL_ENABLE);
917 break;
918 case SNDRV_PCM_TRIGGER_STOP:
919 HD_WRITE(VC4_HD_MAI_CTL,
920 VC4_HD_MAI_CTL_DLATE |
921 VC4_HD_MAI_CTL_ERRORE |
922 VC4_HD_MAI_CTL_ERRORF);
923 HDMI_WRITE(VC4_HDMI_TX_PHY_CTL0,
924 HDMI_READ(VC4_HDMI_TX_PHY_CTL0) |
925 VC4_HDMI_TX_PHY_RNG_PWRDN);
926 break;
927 default:
928 break;
929 }
930
931 return 0;
932}
933
934static inline struct vc4_hdmi *
935snd_component_to_hdmi(struct snd_soc_component *component)
936{
937 struct snd_soc_card *card = snd_soc_component_get_drvdata(component);
938
939 return snd_soc_card_get_drvdata(card);
940}
941
942static int vc4_hdmi_audio_eld_ctl_info(struct snd_kcontrol *kcontrol,
943 struct snd_ctl_elem_info *uinfo)
944{
945 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
946 struct vc4_hdmi *hdmi = snd_component_to_hdmi(component);
947
948 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
949 uinfo->count = sizeof(hdmi->connector->eld);
950
951 return 0;
952}
953
954static int vc4_hdmi_audio_eld_ctl_get(struct snd_kcontrol *kcontrol,
955 struct snd_ctl_elem_value *ucontrol)
956{
957 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
958 struct vc4_hdmi *hdmi = snd_component_to_hdmi(component);
959
960 memcpy(ucontrol->value.bytes.data, hdmi->connector->eld,
961 sizeof(hdmi->connector->eld));
962
963 return 0;
964}
965
966static const struct snd_kcontrol_new vc4_hdmi_audio_controls[] = {
967 {
968 .access = SNDRV_CTL_ELEM_ACCESS_READ |
969 SNDRV_CTL_ELEM_ACCESS_VOLATILE,
970 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
971 .name = "ELD",
972 .info = vc4_hdmi_audio_eld_ctl_info,
973 .get = vc4_hdmi_audio_eld_ctl_get,
974 },
975};
976
977static const struct snd_soc_dapm_widget vc4_hdmi_audio_widgets[] = {
978 SND_SOC_DAPM_OUTPUT("TX"),
979};
980
981static const struct snd_soc_dapm_route vc4_hdmi_audio_routes[] = {
982 { "TX", NULL, "Playback" },
983};
984
Kuninori Morimoto635b1c12018-01-29 04:35:04 +0000985static const struct snd_soc_component_driver vc4_hdmi_audio_component_drv = {
986 .controls = vc4_hdmi_audio_controls,
987 .num_controls = ARRAY_SIZE(vc4_hdmi_audio_controls),
988 .dapm_widgets = vc4_hdmi_audio_widgets,
989 .num_dapm_widgets = ARRAY_SIZE(vc4_hdmi_audio_widgets),
990 .dapm_routes = vc4_hdmi_audio_routes,
991 .num_dapm_routes = ARRAY_SIZE(vc4_hdmi_audio_routes),
992 .idle_bias_on = 1,
993 .use_pmdown_time = 1,
994 .endianness = 1,
995 .non_legacy_dai_naming = 1,
Eric Anholtbb7d7852017-02-27 12:28:02 -0800996};
997
998static const struct snd_soc_dai_ops vc4_hdmi_audio_dai_ops = {
999 .startup = vc4_hdmi_audio_startup,
1000 .shutdown = vc4_hdmi_audio_shutdown,
1001 .hw_params = vc4_hdmi_audio_hw_params,
1002 .set_fmt = vc4_hdmi_audio_set_fmt,
1003 .trigger = vc4_hdmi_audio_trigger,
1004};
1005
1006static struct snd_soc_dai_driver vc4_hdmi_audio_codec_dai_drv = {
1007 .name = "vc4-hdmi-hifi",
1008 .playback = {
1009 .stream_name = "Playback",
1010 .channels_min = 2,
1011 .channels_max = 8,
1012 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
1013 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
1014 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
1015 SNDRV_PCM_RATE_192000,
1016 .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
1017 },
1018};
1019
1020static const struct snd_soc_component_driver vc4_hdmi_audio_cpu_dai_comp = {
1021 .name = "vc4-hdmi-cpu-dai-component",
1022};
1023
1024static int vc4_hdmi_audio_cpu_dai_probe(struct snd_soc_dai *dai)
1025{
1026 struct vc4_hdmi *hdmi = dai_to_hdmi(dai);
1027
1028 snd_soc_dai_init_dma_data(dai, &hdmi->audio.dma_data, NULL);
1029
1030 return 0;
1031}
1032
1033static struct snd_soc_dai_driver vc4_hdmi_audio_cpu_dai_drv = {
1034 .name = "vc4-hdmi-cpu-dai",
1035 .probe = vc4_hdmi_audio_cpu_dai_probe,
1036 .playback = {
1037 .stream_name = "Playback",
1038 .channels_min = 1,
1039 .channels_max = 8,
1040 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
1041 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
1042 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
1043 SNDRV_PCM_RATE_192000,
1044 .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
1045 },
1046 .ops = &vc4_hdmi_audio_dai_ops,
1047};
1048
1049static const struct snd_dmaengine_pcm_config pcm_conf = {
1050 .chan_names[SNDRV_PCM_STREAM_PLAYBACK] = "audio-rx",
1051 .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
1052};
1053
1054static int vc4_hdmi_audio_init(struct vc4_hdmi *hdmi)
1055{
1056 struct snd_soc_dai_link *dai_link = &hdmi->audio.link;
1057 struct snd_soc_card *card = &hdmi->audio.card;
1058 struct device *dev = &hdmi->pdev->dev;
1059 const __be32 *addr;
1060 int ret;
1061
1062 if (!of_find_property(dev->of_node, "dmas", NULL)) {
1063 dev_warn(dev,
1064 "'dmas' DT property is missing, no HDMI audio\n");
1065 return 0;
1066 }
1067
1068 /*
1069 * Get the physical address of VC4_HD_MAI_DATA. We need to retrieve
1070 * the bus address specified in the DT, because the physical address
1071 * (the one returned by platform_get_resource()) is not appropriate
1072 * for DMA transfers.
1073 * This VC/MMU should probably be exposed to avoid this kind of hacks.
1074 */
1075 addr = of_get_address(dev->of_node, 1, NULL, NULL);
1076 hdmi->audio.dma_data.addr = be32_to_cpup(addr) + VC4_HD_MAI_DATA;
1077 hdmi->audio.dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1078 hdmi->audio.dma_data.maxburst = 2;
1079
1080 ret = devm_snd_dmaengine_pcm_register(dev, &pcm_conf, 0);
1081 if (ret) {
1082 dev_err(dev, "Could not register PCM component: %d\n", ret);
1083 return ret;
1084 }
1085
1086 ret = devm_snd_soc_register_component(dev, &vc4_hdmi_audio_cpu_dai_comp,
1087 &vc4_hdmi_audio_cpu_dai_drv, 1);
1088 if (ret) {
1089 dev_err(dev, "Could not register CPU DAI: %d\n", ret);
1090 return ret;
1091 }
1092
Kuninori Morimoto635b1c12018-01-29 04:35:04 +00001093 /* register component and codec dai */
1094 ret = devm_snd_soc_register_component(dev, &vc4_hdmi_audio_component_drv,
Eric Anholtbb7d7852017-02-27 12:28:02 -08001095 &vc4_hdmi_audio_codec_dai_drv, 1);
1096 if (ret) {
Kuninori Morimoto635b1c12018-01-29 04:35:04 +00001097 dev_err(dev, "Could not register component: %d\n", ret);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001098 return ret;
1099 }
1100
1101 dai_link->name = "MAI";
1102 dai_link->stream_name = "MAI PCM";
1103 dai_link->codec_dai_name = vc4_hdmi_audio_codec_dai_drv.name;
1104 dai_link->cpu_dai_name = dev_name(dev);
1105 dai_link->codec_name = dev_name(dev);
1106 dai_link->platform_name = dev_name(dev);
1107
1108 card->dai_link = dai_link;
1109 card->num_links = 1;
1110 card->name = "vc4-hdmi";
1111 card->dev = dev;
1112
1113 /*
1114 * Be careful, snd_soc_register_card() calls dev_set_drvdata() and
1115 * stores a pointer to the snd card object in dev->driver_data. This
1116 * means we cannot use it for something else. The hdmi back-pointer is
1117 * now stored in card->drvdata and should be retrieved with
1118 * snd_soc_card_get_drvdata() if needed.
1119 */
1120 snd_soc_card_set_drvdata(card, hdmi);
1121 ret = devm_snd_soc_register_card(dev, card);
Kuninori Morimoto635b1c12018-01-29 04:35:04 +00001122 if (ret)
Eric Anholtbb7d7852017-02-27 12:28:02 -08001123 dev_err(dev, "Could not register sound card: %d\n", ret);
Eric Anholtbb7d7852017-02-27 12:28:02 -08001124
1125 return ret;
Eric Anholtbb7d7852017-02-27 12:28:02 -08001126
Eric Anholtbb7d7852017-02-27 12:28:02 -08001127}
1128
Hans Verkuil15b45112017-07-16 12:48:04 +02001129#ifdef CONFIG_DRM_VC4_HDMI_CEC
1130static irqreturn_t vc4_cec_irq_handler_thread(int irq, void *priv)
1131{
1132 struct vc4_dev *vc4 = priv;
1133 struct vc4_hdmi *hdmi = vc4->hdmi;
1134
1135 if (hdmi->cec_irq_was_rx) {
1136 if (hdmi->cec_rx_msg.len)
1137 cec_received_msg(hdmi->cec_adap, &hdmi->cec_rx_msg);
1138 } else if (hdmi->cec_tx_ok) {
1139 cec_transmit_done(hdmi->cec_adap, CEC_TX_STATUS_OK,
1140 0, 0, 0, 0);
1141 } else {
1142 /*
1143 * This CEC implementation makes 1 retry, so if we
1144 * get a NACK, then that means it made 2 attempts.
1145 */
1146 cec_transmit_done(hdmi->cec_adap, CEC_TX_STATUS_NACK,
1147 0, 2, 0, 0);
1148 }
1149 return IRQ_HANDLED;
1150}
1151
1152static void vc4_cec_read_msg(struct vc4_dev *vc4, u32 cntrl1)
1153{
1154 struct cec_msg *msg = &vc4->hdmi->cec_rx_msg;
1155 unsigned int i;
1156
1157 msg->len = 1 + ((cntrl1 & VC4_HDMI_CEC_REC_WRD_CNT_MASK) >>
1158 VC4_HDMI_CEC_REC_WRD_CNT_SHIFT);
1159 for (i = 0; i < msg->len; i += 4) {
1160 u32 val = HDMI_READ(VC4_HDMI_CEC_RX_DATA_1 + i);
1161
1162 msg->msg[i] = val & 0xff;
1163 msg->msg[i + 1] = (val >> 8) & 0xff;
1164 msg->msg[i + 2] = (val >> 16) & 0xff;
1165 msg->msg[i + 3] = (val >> 24) & 0xff;
1166 }
1167}
1168
1169static irqreturn_t vc4_cec_irq_handler(int irq, void *priv)
1170{
1171 struct vc4_dev *vc4 = priv;
1172 struct vc4_hdmi *hdmi = vc4->hdmi;
1173 u32 stat = HDMI_READ(VC4_HDMI_CPU_STATUS);
1174 u32 cntrl1, cntrl5;
1175
1176 if (!(stat & VC4_HDMI_CPU_CEC))
1177 return IRQ_NONE;
1178 hdmi->cec_rx_msg.len = 0;
1179 cntrl1 = HDMI_READ(VC4_HDMI_CEC_CNTRL_1);
1180 cntrl5 = HDMI_READ(VC4_HDMI_CEC_CNTRL_5);
1181 hdmi->cec_irq_was_rx = cntrl5 & VC4_HDMI_CEC_RX_CEC_INT;
1182 if (hdmi->cec_irq_was_rx) {
1183 vc4_cec_read_msg(vc4, cntrl1);
1184 cntrl1 |= VC4_HDMI_CEC_CLEAR_RECEIVE_OFF;
1185 HDMI_WRITE(VC4_HDMI_CEC_CNTRL_1, cntrl1);
1186 cntrl1 &= ~VC4_HDMI_CEC_CLEAR_RECEIVE_OFF;
1187 } else {
1188 hdmi->cec_tx_ok = cntrl1 & VC4_HDMI_CEC_TX_STATUS_GOOD;
1189 cntrl1 &= ~VC4_HDMI_CEC_START_XMIT_BEGIN;
1190 }
1191 HDMI_WRITE(VC4_HDMI_CEC_CNTRL_1, cntrl1);
1192 HDMI_WRITE(VC4_HDMI_CPU_CLEAR, VC4_HDMI_CPU_CEC);
1193
1194 return IRQ_WAKE_THREAD;
1195}
1196
1197static int vc4_hdmi_cec_adap_enable(struct cec_adapter *adap, bool enable)
1198{
1199 struct vc4_dev *vc4 = cec_get_drvdata(adap);
1200 /* clock period in microseconds */
1201 const u32 usecs = 1000000 / CEC_CLOCK_FREQ;
1202 u32 val = HDMI_READ(VC4_HDMI_CEC_CNTRL_5);
1203
1204 val &= ~(VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET |
1205 VC4_HDMI_CEC_CNT_TO_4700_US_MASK |
1206 VC4_HDMI_CEC_CNT_TO_4500_US_MASK);
1207 val |= ((4700 / usecs) << VC4_HDMI_CEC_CNT_TO_4700_US_SHIFT) |
1208 ((4500 / usecs) << VC4_HDMI_CEC_CNT_TO_4500_US_SHIFT);
1209
1210 if (enable) {
1211 HDMI_WRITE(VC4_HDMI_CEC_CNTRL_5, val |
1212 VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET);
1213 HDMI_WRITE(VC4_HDMI_CEC_CNTRL_5, val);
1214 HDMI_WRITE(VC4_HDMI_CEC_CNTRL_2,
1215 ((1500 / usecs) << VC4_HDMI_CEC_CNT_TO_1500_US_SHIFT) |
1216 ((1300 / usecs) << VC4_HDMI_CEC_CNT_TO_1300_US_SHIFT) |
1217 ((800 / usecs) << VC4_HDMI_CEC_CNT_TO_800_US_SHIFT) |
1218 ((600 / usecs) << VC4_HDMI_CEC_CNT_TO_600_US_SHIFT) |
1219 ((400 / usecs) << VC4_HDMI_CEC_CNT_TO_400_US_SHIFT));
1220 HDMI_WRITE(VC4_HDMI_CEC_CNTRL_3,
1221 ((2750 / usecs) << VC4_HDMI_CEC_CNT_TO_2750_US_SHIFT) |
1222 ((2400 / usecs) << VC4_HDMI_CEC_CNT_TO_2400_US_SHIFT) |
1223 ((2050 / usecs) << VC4_HDMI_CEC_CNT_TO_2050_US_SHIFT) |
1224 ((1700 / usecs) << VC4_HDMI_CEC_CNT_TO_1700_US_SHIFT));
1225 HDMI_WRITE(VC4_HDMI_CEC_CNTRL_4,
1226 ((4300 / usecs) << VC4_HDMI_CEC_CNT_TO_4300_US_SHIFT) |
1227 ((3900 / usecs) << VC4_HDMI_CEC_CNT_TO_3900_US_SHIFT) |
1228 ((3600 / usecs) << VC4_HDMI_CEC_CNT_TO_3600_US_SHIFT) |
1229 ((3500 / usecs) << VC4_HDMI_CEC_CNT_TO_3500_US_SHIFT));
1230
1231 HDMI_WRITE(VC4_HDMI_CPU_MASK_CLEAR, VC4_HDMI_CPU_CEC);
1232 } else {
1233 HDMI_WRITE(VC4_HDMI_CPU_MASK_SET, VC4_HDMI_CPU_CEC);
1234 HDMI_WRITE(VC4_HDMI_CEC_CNTRL_5, val |
1235 VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET);
1236 }
1237 return 0;
1238}
1239
1240static int vc4_hdmi_cec_adap_log_addr(struct cec_adapter *adap, u8 log_addr)
1241{
1242 struct vc4_dev *vc4 = cec_get_drvdata(adap);
1243
1244 HDMI_WRITE(VC4_HDMI_CEC_CNTRL_1,
1245 (HDMI_READ(VC4_HDMI_CEC_CNTRL_1) & ~VC4_HDMI_CEC_ADDR_MASK) |
1246 (log_addr & 0xf) << VC4_HDMI_CEC_ADDR_SHIFT);
1247 return 0;
1248}
1249
1250static int vc4_hdmi_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
1251 u32 signal_free_time, struct cec_msg *msg)
1252{
1253 struct vc4_dev *vc4 = cec_get_drvdata(adap);
1254 u32 val;
1255 unsigned int i;
1256
1257 for (i = 0; i < msg->len; i += 4)
1258 HDMI_WRITE(VC4_HDMI_CEC_TX_DATA_1 + i,
1259 (msg->msg[i]) |
1260 (msg->msg[i + 1] << 8) |
1261 (msg->msg[i + 2] << 16) |
1262 (msg->msg[i + 3] << 24));
1263
1264 val = HDMI_READ(VC4_HDMI_CEC_CNTRL_1);
1265 val &= ~VC4_HDMI_CEC_START_XMIT_BEGIN;
1266 HDMI_WRITE(VC4_HDMI_CEC_CNTRL_1, val);
1267 val &= ~VC4_HDMI_CEC_MESSAGE_LENGTH_MASK;
1268 val |= (msg->len - 1) << VC4_HDMI_CEC_MESSAGE_LENGTH_SHIFT;
1269 val |= VC4_HDMI_CEC_START_XMIT_BEGIN;
1270
1271 HDMI_WRITE(VC4_HDMI_CEC_CNTRL_1, val);
1272 return 0;
1273}
1274
1275static const struct cec_adap_ops vc4_hdmi_cec_adap_ops = {
1276 .adap_enable = vc4_hdmi_cec_adap_enable,
1277 .adap_log_addr = vc4_hdmi_cec_adap_log_addr,
1278 .adap_transmit = vc4_hdmi_cec_adap_transmit,
1279};
1280#endif
1281
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001282static int vc4_hdmi_bind(struct device *dev, struct device *master, void *data)
1283{
1284 struct platform_device *pdev = to_platform_device(dev);
1285 struct drm_device *drm = dev_get_drvdata(master);
1286 struct vc4_dev *vc4 = drm->dev_private;
1287 struct vc4_hdmi *hdmi;
1288 struct vc4_hdmi_encoder *vc4_hdmi_encoder;
1289 struct device_node *ddc_node;
1290 u32 value;
1291 int ret;
1292
1293 hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL);
1294 if (!hdmi)
1295 return -ENOMEM;
1296
1297 vc4_hdmi_encoder = devm_kzalloc(dev, sizeof(*vc4_hdmi_encoder),
1298 GFP_KERNEL);
1299 if (!vc4_hdmi_encoder)
1300 return -ENOMEM;
1301 vc4_hdmi_encoder->base.type = VC4_ENCODER_TYPE_HDMI;
1302 hdmi->encoder = &vc4_hdmi_encoder->base.base;
1303
1304 hdmi->pdev = pdev;
1305 hdmi->hdmicore_regs = vc4_ioremap_regs(pdev, 0);
1306 if (IS_ERR(hdmi->hdmicore_regs))
1307 return PTR_ERR(hdmi->hdmicore_regs);
1308
1309 hdmi->hd_regs = vc4_ioremap_regs(pdev, 1);
1310 if (IS_ERR(hdmi->hd_regs))
1311 return PTR_ERR(hdmi->hd_regs);
1312
Eric Anholt30517192019-02-20 13:03:38 -08001313 hdmi->hdmi_regset.base = hdmi->hdmicore_regs;
1314 hdmi->hdmi_regset.regs = hdmi_regs;
1315 hdmi->hdmi_regset.nregs = ARRAY_SIZE(hdmi_regs);
1316 hdmi->hd_regset.base = hdmi->hd_regs;
1317 hdmi->hd_regset.regs = hd_regs;
1318 hdmi->hd_regset.nregs = ARRAY_SIZE(hd_regs);
1319
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001320 hdmi->pixel_clock = devm_clk_get(dev, "pixel");
1321 if (IS_ERR(hdmi->pixel_clock)) {
1322 DRM_ERROR("Failed to get pixel clock\n");
1323 return PTR_ERR(hdmi->pixel_clock);
1324 }
1325 hdmi->hsm_clock = devm_clk_get(dev, "hdmi");
1326 if (IS_ERR(hdmi->hsm_clock)) {
1327 DRM_ERROR("Failed to get HDMI state machine clock\n");
1328 return PTR_ERR(hdmi->hsm_clock);
1329 }
1330
Peter Chen027a6972016-07-05 10:04:54 +08001331 ddc_node = of_parse_phandle(dev->of_node, "ddc", 0);
1332 if (!ddc_node) {
1333 DRM_ERROR("Failed to find ddc node in device tree\n");
1334 return -ENODEV;
1335 }
1336
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001337 hdmi->ddc = of_find_i2c_adapter_by_node(ddc_node);
Peter Chen027a6972016-07-05 10:04:54 +08001338 of_node_put(ddc_node);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001339 if (!hdmi->ddc) {
1340 DRM_DEBUG("Failed to get ddc i2c adapter by node\n");
1341 return -EPROBE_DEFER;
1342 }
1343
Hans Verkuil10ee2752017-07-16 12:48:03 +02001344 /* This is the rate that is set by the firmware. The number
1345 * needs to be a bit higher than the pixel clock rate
1346 * (generally 148.5Mhz).
1347 */
Hans Verkuil15b45112017-07-16 12:48:04 +02001348 ret = clk_set_rate(hdmi->hsm_clock, HSM_CLOCK_FREQ);
Hans Verkuil10ee2752017-07-16 12:48:03 +02001349 if (ret) {
1350 DRM_ERROR("Failed to set HSM clock rate: %d\n", ret);
1351 goto err_put_i2c;
1352 }
1353
1354 ret = clk_prepare_enable(hdmi->hsm_clock);
1355 if (ret) {
1356 DRM_ERROR("Failed to turn on HDMI state machine clock: %d\n",
1357 ret);
1358 goto err_put_i2c;
1359 }
1360
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001361 /* Only use the GPIO HPD pin if present in the DT, otherwise
1362 * we'll use the HDMI core's register.
1363 */
1364 if (of_find_property(dev->of_node, "hpd-gpios", &value)) {
Eric Anholt0b06e0a2016-02-29 17:53:01 -08001365 enum of_gpio_flags hpd_gpio_flags;
1366
1367 hdmi->hpd_gpio = of_get_named_gpio_flags(dev->of_node,
1368 "hpd-gpios", 0,
1369 &hpd_gpio_flags);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001370 if (hdmi->hpd_gpio < 0) {
1371 ret = hdmi->hpd_gpio;
Hans Verkuil10ee2752017-07-16 12:48:03 +02001372 goto err_unprepare_hsm;
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001373 }
Eric Anholt0b06e0a2016-02-29 17:53:01 -08001374
1375 hdmi->hpd_active_low = hpd_gpio_flags & OF_GPIO_ACTIVE_LOW;
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001376 }
1377
1378 vc4->hdmi = hdmi;
1379
Hans Verkuil10ee2752017-07-16 12:48:03 +02001380 /* HDMI core must be enabled. */
1381 if (!(HD_READ(VC4_HD_M_CTL) & VC4_HD_M_ENABLE)) {
1382 HD_WRITE(VC4_HD_M_CTL, VC4_HD_M_SW_RST);
1383 udelay(1);
1384 HD_WRITE(VC4_HD_M_CTL, 0);
1385
1386 HD_WRITE(VC4_HD_M_CTL, VC4_HD_M_ENABLE);
1387 }
Boris Brezillon4f6e3d62017-04-11 18:39:25 +02001388 pm_runtime_enable(dev);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001389
1390 drm_encoder_init(drm, hdmi->encoder, &vc4_hdmi_encoder_funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +02001391 DRM_MODE_ENCODER_TMDS, NULL);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001392 drm_encoder_helper_add(hdmi->encoder, &vc4_hdmi_encoder_helper_funcs);
1393
1394 hdmi->connector = vc4_hdmi_connector_init(drm, hdmi->encoder);
1395 if (IS_ERR(hdmi->connector)) {
1396 ret = PTR_ERR(hdmi->connector);
1397 goto err_destroy_encoder;
1398 }
Hans Verkuil15b45112017-07-16 12:48:04 +02001399#ifdef CONFIG_DRM_VC4_HDMI_CEC
1400 hdmi->cec_adap = cec_allocate_adapter(&vc4_hdmi_cec_adap_ops,
1401 vc4, "vc4",
1402 CEC_CAP_TRANSMIT |
1403 CEC_CAP_LOG_ADDRS |
1404 CEC_CAP_PASSTHROUGH |
1405 CEC_CAP_RC, 1);
1406 ret = PTR_ERR_OR_ZERO(hdmi->cec_adap);
1407 if (ret < 0)
1408 goto err_destroy_conn;
1409 HDMI_WRITE(VC4_HDMI_CPU_MASK_SET, 0xffffffff);
1410 value = HDMI_READ(VC4_HDMI_CEC_CNTRL_1);
1411 value &= ~VC4_HDMI_CEC_DIV_CLK_CNT_MASK;
1412 /*
1413 * Set the logical address to Unregistered and set the clock
1414 * divider: the hsm_clock rate and this divider setting will
1415 * give a 40 kHz CEC clock.
1416 */
1417 value |= VC4_HDMI_CEC_ADDR_MASK |
1418 (4091 << VC4_HDMI_CEC_DIV_CLK_CNT_SHIFT);
1419 HDMI_WRITE(VC4_HDMI_CEC_CNTRL_1, value);
1420 ret = devm_request_threaded_irq(dev, platform_get_irq(pdev, 0),
1421 vc4_cec_irq_handler,
1422 vc4_cec_irq_handler_thread, 0,
1423 "vc4 hdmi cec", vc4);
1424 if (ret)
1425 goto err_delete_cec_adap;
1426 ret = cec_register_adapter(hdmi->cec_adap, dev);
1427 if (ret < 0)
1428 goto err_delete_cec_adap;
1429#endif
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001430
Eric Anholtbb7d7852017-02-27 12:28:02 -08001431 ret = vc4_hdmi_audio_init(hdmi);
1432 if (ret)
1433 goto err_destroy_encoder;
1434
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001435 return 0;
1436
Hans Verkuil15b45112017-07-16 12:48:04 +02001437#ifdef CONFIG_DRM_VC4_HDMI_CEC
1438err_delete_cec_adap:
1439 cec_delete_adapter(hdmi->cec_adap);
1440err_destroy_conn:
1441 vc4_hdmi_connector_destroy(hdmi->connector);
1442#endif
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001443err_destroy_encoder:
1444 vc4_hdmi_encoder_destroy(hdmi->encoder);
Hans Verkuil10ee2752017-07-16 12:48:03 +02001445err_unprepare_hsm:
1446 clk_disable_unprepare(hdmi->hsm_clock);
Boris Brezillon4f6e3d62017-04-11 18:39:25 +02001447 pm_runtime_disable(dev);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001448err_put_i2c:
Eric Anholt58839802016-04-04 14:25:59 -07001449 put_device(&hdmi->ddc->dev);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001450
1451 return ret;
1452}
1453
1454static void vc4_hdmi_unbind(struct device *dev, struct device *master,
1455 void *data)
1456{
1457 struct drm_device *drm = dev_get_drvdata(master);
1458 struct vc4_dev *vc4 = drm->dev_private;
1459 struct vc4_hdmi *hdmi = vc4->hdmi;
1460
Hans Verkuil15b45112017-07-16 12:48:04 +02001461 cec_unregister_adapter(hdmi->cec_adap);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001462 vc4_hdmi_connector_destroy(hdmi->connector);
1463 vc4_hdmi_encoder_destroy(hdmi->encoder);
1464
Hans Verkuil10ee2752017-07-16 12:48:03 +02001465 clk_disable_unprepare(hdmi->hsm_clock);
Boris Brezillon4f6e3d62017-04-11 18:39:25 +02001466 pm_runtime_disable(dev);
1467
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001468 put_device(&hdmi->ddc->dev);
1469
1470 vc4->hdmi = NULL;
1471}
1472
1473static const struct component_ops vc4_hdmi_ops = {
1474 .bind = vc4_hdmi_bind,
1475 .unbind = vc4_hdmi_unbind,
1476};
1477
1478static int vc4_hdmi_dev_probe(struct platform_device *pdev)
1479{
1480 return component_add(&pdev->dev, &vc4_hdmi_ops);
1481}
1482
1483static int vc4_hdmi_dev_remove(struct platform_device *pdev)
1484{
1485 component_del(&pdev->dev, &vc4_hdmi_ops);
1486 return 0;
1487}
1488
1489static const struct of_device_id vc4_hdmi_dt_match[] = {
1490 { .compatible = "brcm,bcm2835-hdmi" },
1491 {}
1492};
1493
1494struct platform_driver vc4_hdmi_driver = {
1495 .probe = vc4_hdmi_dev_probe,
1496 .remove = vc4_hdmi_dev_remove,
1497 .driver = {
1498 .name = "vc4_hdmi",
1499 .of_match_table = vc4_hdmi_dt_match,
1500 },
1501};