1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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>
7 */
8
9 /**
10 * DOC: VC4 Falcon HDMI module
11 *
12 * 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.
32 */
33
34 #include <drm/display/drm_hdmi_helper.h>
35 #include <drm/display/drm_scdc_helper.h>
36 #include <drm/drm_atomic_helper.h>
37 #include <drm/drm_drv.h>
38 #include <drm/drm_probe_helper.h>
39 #include <drm/drm_simple_kms_helper.h>
40 #include <linux/clk.h>
41 #include <linux/component.h>
42 #include <linux/gpio/consumer.h>
43 #include <linux/i2c.h>
44 #include <linux/of.h>
45 #include <linux/of_address.h>
46 #include <linux/pm_runtime.h>
47 #include <linux/rational.h>
48 #include <linux/reset.h>
49 #include <sound/dmaengine_pcm.h>
50 #include <sound/hdmi-codec.h>
51 #include <sound/pcm_drm_eld.h>
52 #include <sound/pcm_params.h>
53 #include <sound/soc.h>
54 #include "media/cec.h"
55 #include "vc4_drv.h"
56 #include "vc4_hdmi.h"
57 #include "vc4_hdmi_regs.h"
58 #include "vc4_regs.h"
59
60 #define VC5_HDMI_HORZA_HFP_SHIFT 16
61 #define VC5_HDMI_HORZA_HFP_MASK VC4_MASK(28, 16)
62 #define VC5_HDMI_HORZA_VPOS BIT(15)
63 #define VC5_HDMI_HORZA_HPOS BIT(14)
64 #define VC5_HDMI_HORZA_HAP_SHIFT 0
65 #define VC5_HDMI_HORZA_HAP_MASK VC4_MASK(13, 0)
66
67 #define VC5_HDMI_HORZB_HBP_SHIFT 16
68 #define VC5_HDMI_HORZB_HBP_MASK VC4_MASK(26, 16)
69 #define VC5_HDMI_HORZB_HSP_SHIFT 0
70 #define VC5_HDMI_HORZB_HSP_MASK VC4_MASK(10, 0)
71
72 #define VC5_HDMI_VERTA_VSP_SHIFT 24
73 #define VC5_HDMI_VERTA_VSP_MASK VC4_MASK(28, 24)
74 #define VC5_HDMI_VERTA_VFP_SHIFT 16
75 #define VC5_HDMI_VERTA_VFP_MASK VC4_MASK(22, 16)
76 #define VC5_HDMI_VERTA_VAL_SHIFT 0
77 #define VC5_HDMI_VERTA_VAL_MASK VC4_MASK(12, 0)
78
79 #define VC5_HDMI_VERTB_VSPO_SHIFT 16
80 #define VC5_HDMI_VERTB_VSPO_MASK VC4_MASK(29, 16)
81
82 #define VC4_HDMI_MISC_CONTROL_PIXEL_REP_SHIFT 0
83 #define VC4_HDMI_MISC_CONTROL_PIXEL_REP_MASK VC4_MASK(3, 0)
84 #define VC5_HDMI_MISC_CONTROL_PIXEL_REP_SHIFT 0
85 #define VC5_HDMI_MISC_CONTROL_PIXEL_REP_MASK VC4_MASK(3, 0)
86
87 #define VC5_HDMI_SCRAMBLER_CTL_ENABLE BIT(0)
88
89 #define VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE_SHIFT 8
90 #define VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE_MASK VC4_MASK(10, 8)
91
92 #define VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH_SHIFT 0
93 #define VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH_MASK VC4_MASK(3, 0)
94
95 #define VC5_HDMI_GCP_CONFIG_GCP_ENABLE BIT(31)
96
97 #define VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1_SHIFT 8
98 #define VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1_MASK VC4_MASK(15, 8)
99
100 #define VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_0_MASK VC4_MASK(7, 0)
101 #define VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_0_SET_AVMUTE BIT(0)
102 #define VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_0_CLEAR_AVMUTE BIT(4)
103
104 # define VC4_HD_M_SW_RST BIT(2)
105 # define VC4_HD_M_ENABLE BIT(0)
106
107 #define HSM_MIN_CLOCK_FREQ 120000000
108 #define CEC_CLOCK_FREQ 40000
109
110 #define HDMI_14_MAX_TMDS_CLK (340 * 1000 * 1000)
111
112 static const char * const output_format_str[] = {
113 [VC4_HDMI_OUTPUT_RGB] = "RGB",
114 [VC4_HDMI_OUTPUT_YUV420] = "YUV 4:2:0",
115 [VC4_HDMI_OUTPUT_YUV422] = "YUV 4:2:2",
116 [VC4_HDMI_OUTPUT_YUV444] = "YUV 4:4:4",
117 };
118
vc4_hdmi_output_fmt_str(enum vc4_hdmi_output_format fmt)119 static const char *vc4_hdmi_output_fmt_str(enum vc4_hdmi_output_format fmt)
120 {
121 if (fmt >= ARRAY_SIZE(output_format_str))
122 return "invalid";
123
124 return output_format_str[fmt];
125 }
126
127 static unsigned long long
128 vc4_hdmi_encoder_compute_mode_clock(const struct drm_display_mode *mode,
129 unsigned int bpc, enum vc4_hdmi_output_format fmt);
130
vc4_hdmi_supports_scrambling(struct vc4_hdmi * vc4_hdmi)131 static bool vc4_hdmi_supports_scrambling(struct vc4_hdmi *vc4_hdmi)
132 {
133 struct drm_display_info *display = &vc4_hdmi->connector.display_info;
134
135 lockdep_assert_held(&vc4_hdmi->mutex);
136
137 if (!display->is_hdmi)
138 return false;
139
140 if (!display->hdmi.scdc.supported ||
141 !display->hdmi.scdc.scrambling.supported)
142 return false;
143
144 return true;
145 }
146
vc4_hdmi_mode_needs_scrambling(const struct drm_display_mode * mode,unsigned int bpc,enum vc4_hdmi_output_format fmt)147 static bool vc4_hdmi_mode_needs_scrambling(const struct drm_display_mode *mode,
148 unsigned int bpc,
149 enum vc4_hdmi_output_format fmt)
150 {
151 unsigned long long clock = vc4_hdmi_encoder_compute_mode_clock(mode, bpc, fmt);
152
153 return clock > HDMI_14_MAX_TMDS_CLK;
154 }
155
vc4_hdmi_is_full_range(struct vc4_hdmi * vc4_hdmi,struct vc4_hdmi_connector_state * vc4_state)156 static bool vc4_hdmi_is_full_range(struct vc4_hdmi *vc4_hdmi,
157 struct vc4_hdmi_connector_state *vc4_state)
158 {
159 const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
160 struct drm_display_info *display = &vc4_hdmi->connector.display_info;
161
162 if (vc4_state->broadcast_rgb == VC4_HDMI_BROADCAST_RGB_LIMITED)
163 return false;
164 else if (vc4_state->broadcast_rgb == VC4_HDMI_BROADCAST_RGB_FULL)
165 return true;
166
167 return !display->is_hdmi ||
168 drm_default_rgb_quant_range(mode) == HDMI_QUANTIZATION_RANGE_FULL;
169 }
170
vc4_hdmi_debugfs_regs(struct seq_file * m,void * unused)171 static int vc4_hdmi_debugfs_regs(struct seq_file *m, void *unused)
172 {
173 struct drm_debugfs_entry *entry = m->private;
174 struct vc4_hdmi *vc4_hdmi = entry->file.data;
175 struct drm_device *drm = vc4_hdmi->connector.dev;
176 struct drm_printer p = drm_seq_file_printer(m);
177 int idx;
178
179 if (!drm_dev_enter(drm, &idx))
180 return -ENODEV;
181
182 WARN_ON(pm_runtime_resume_and_get(&vc4_hdmi->pdev->dev));
183
184 drm_print_regset32(&p, &vc4_hdmi->hdmi_regset);
185 drm_print_regset32(&p, &vc4_hdmi->hd_regset);
186 drm_print_regset32(&p, &vc4_hdmi->cec_regset);
187 drm_print_regset32(&p, &vc4_hdmi->csc_regset);
188 drm_print_regset32(&p, &vc4_hdmi->dvp_regset);
189 drm_print_regset32(&p, &vc4_hdmi->phy_regset);
190 drm_print_regset32(&p, &vc4_hdmi->ram_regset);
191 drm_print_regset32(&p, &vc4_hdmi->rm_regset);
192
193 pm_runtime_put(&vc4_hdmi->pdev->dev);
194
195 drm_dev_exit(idx);
196
197 return 0;
198 }
199
vc4_hdmi_reset(struct vc4_hdmi * vc4_hdmi)200 static void vc4_hdmi_reset(struct vc4_hdmi *vc4_hdmi)
201 {
202 struct drm_device *drm = vc4_hdmi->connector.dev;
203 unsigned long flags;
204 int idx;
205
206 /*
207 * We can be called by our bind callback, when the
208 * connector->dev pointer might not be initialised yet.
209 */
210 if (drm && !drm_dev_enter(drm, &idx))
211 return;
212
213 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
214
215 HDMI_WRITE(HDMI_M_CTL, VC4_HD_M_SW_RST);
216 udelay(1);
217 HDMI_WRITE(HDMI_M_CTL, 0);
218
219 HDMI_WRITE(HDMI_M_CTL, VC4_HD_M_ENABLE);
220
221 HDMI_WRITE(HDMI_SW_RESET_CONTROL,
222 VC4_HDMI_SW_RESET_HDMI |
223 VC4_HDMI_SW_RESET_FORMAT_DETECT);
224
225 HDMI_WRITE(HDMI_SW_RESET_CONTROL, 0);
226
227 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
228
229 if (drm)
230 drm_dev_exit(idx);
231 }
232
vc5_hdmi_reset(struct vc4_hdmi * vc4_hdmi)233 static void vc5_hdmi_reset(struct vc4_hdmi *vc4_hdmi)
234 {
235 struct drm_device *drm = vc4_hdmi->connector.dev;
236 unsigned long flags;
237 int idx;
238
239 /*
240 * We can be called by our bind callback, when the
241 * connector->dev pointer might not be initialised yet.
242 */
243 if (drm && !drm_dev_enter(drm, &idx))
244 return;
245
246 reset_control_reset(vc4_hdmi->reset);
247
248 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
249
250 HDMI_WRITE(HDMI_DVP_CTL, 0);
251
252 HDMI_WRITE(HDMI_CLOCK_STOP,
253 HDMI_READ(HDMI_CLOCK_STOP) | VC4_DVP_HT_CLOCK_STOP_PIXEL);
254
255 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
256
257 if (drm)
258 drm_dev_exit(idx);
259 }
260
261 #ifdef CONFIG_DRM_VC4_HDMI_CEC
vc4_hdmi_cec_update_clk_div(struct vc4_hdmi * vc4_hdmi)262 static void vc4_hdmi_cec_update_clk_div(struct vc4_hdmi *vc4_hdmi)
263 {
264 struct drm_device *drm = vc4_hdmi->connector.dev;
265 unsigned long cec_rate;
266 unsigned long flags;
267 u16 clk_cnt;
268 u32 value;
269 int idx;
270
271 /*
272 * This function is called by our runtime_resume implementation
273 * and thus at bind time, when we haven't registered our
274 * connector yet and thus don't have a pointer to the DRM
275 * device.
276 */
277 if (drm && !drm_dev_enter(drm, &idx))
278 return;
279
280 cec_rate = clk_get_rate(vc4_hdmi->cec_clock);
281
282 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
283
284 value = HDMI_READ(HDMI_CEC_CNTRL_1);
285 value &= ~VC4_HDMI_CEC_DIV_CLK_CNT_MASK;
286
287 /*
288 * Set the clock divider: the hsm_clock rate and this divider
289 * setting will give a 40 kHz CEC clock.
290 */
291 clk_cnt = cec_rate / CEC_CLOCK_FREQ;
292 value |= clk_cnt << VC4_HDMI_CEC_DIV_CLK_CNT_SHIFT;
293 HDMI_WRITE(HDMI_CEC_CNTRL_1, value);
294
295 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
296
297 if (drm)
298 drm_dev_exit(idx);
299 }
300 #else
vc4_hdmi_cec_update_clk_div(struct vc4_hdmi * vc4_hdmi)301 static void vc4_hdmi_cec_update_clk_div(struct vc4_hdmi *vc4_hdmi) {}
302 #endif
303
reset_pipe(struct drm_crtc * crtc,struct drm_modeset_acquire_ctx * ctx)304 static int reset_pipe(struct drm_crtc *crtc,
305 struct drm_modeset_acquire_ctx *ctx)
306 {
307 struct drm_atomic_state *state;
308 struct drm_crtc_state *crtc_state;
309 int ret;
310
311 state = drm_atomic_state_alloc(crtc->dev);
312 if (!state)
313 return -ENOMEM;
314
315 state->acquire_ctx = ctx;
316
317 crtc_state = drm_atomic_get_crtc_state(state, crtc);
318 if (IS_ERR(crtc_state)) {
319 ret = PTR_ERR(crtc_state);
320 goto out;
321 }
322
323 crtc_state->connectors_changed = true;
324
325 ret = drm_atomic_commit(state);
326 out:
327 drm_atomic_state_put(state);
328
329 return ret;
330 }
331
vc4_hdmi_reset_link(struct drm_connector * connector,struct drm_modeset_acquire_ctx * ctx)332 static int vc4_hdmi_reset_link(struct drm_connector *connector,
333 struct drm_modeset_acquire_ctx *ctx)
334 {
335 struct drm_device *drm;
336 struct vc4_hdmi *vc4_hdmi;
337 struct drm_connector_state *conn_state;
338 struct drm_crtc_state *crtc_state;
339 struct drm_crtc *crtc;
340 bool scrambling_needed;
341 u8 config;
342 int ret;
343
344 if (!connector)
345 return 0;
346
347 drm = connector->dev;
348 ret = drm_modeset_lock(&drm->mode_config.connection_mutex, ctx);
349 if (ret)
350 return ret;
351
352 conn_state = connector->state;
353 crtc = conn_state->crtc;
354 if (!crtc)
355 return 0;
356
357 ret = drm_modeset_lock(&crtc->mutex, ctx);
358 if (ret)
359 return ret;
360
361 crtc_state = crtc->state;
362 if (!crtc_state->active)
363 return 0;
364
365 vc4_hdmi = connector_to_vc4_hdmi(connector);
366 mutex_lock(&vc4_hdmi->mutex);
367
368 if (!vc4_hdmi_supports_scrambling(vc4_hdmi)) {
369 mutex_unlock(&vc4_hdmi->mutex);
370 return 0;
371 }
372
373 scrambling_needed = vc4_hdmi_mode_needs_scrambling(&vc4_hdmi->saved_adjusted_mode,
374 vc4_hdmi->output_bpc,
375 vc4_hdmi->output_format);
376 if (!scrambling_needed) {
377 mutex_unlock(&vc4_hdmi->mutex);
378 return 0;
379 }
380
381 if (conn_state->commit &&
382 !try_wait_for_completion(&conn_state->commit->hw_done)) {
383 mutex_unlock(&vc4_hdmi->mutex);
384 return 0;
385 }
386
387 ret = drm_scdc_readb(connector->ddc, SCDC_TMDS_CONFIG, &config);
388 if (ret < 0) {
389 drm_err(drm, "Failed to read TMDS config: %d\n", ret);
390 mutex_unlock(&vc4_hdmi->mutex);
391 return 0;
392 }
393
394 if (!!(config & SCDC_SCRAMBLING_ENABLE) == scrambling_needed) {
395 mutex_unlock(&vc4_hdmi->mutex);
396 return 0;
397 }
398
399 mutex_unlock(&vc4_hdmi->mutex);
400
401 /*
402 * HDMI 2.0 says that one should not send scrambled data
403 * prior to configuring the sink scrambling, and that
404 * TMDS clock/data transmission should be suspended when
405 * changing the TMDS clock rate in the sink. So let's
406 * just do a full modeset here, even though some sinks
407 * would be perfectly happy if were to just reconfigure
408 * the SCDC settings on the fly.
409 */
410 return reset_pipe(crtc, ctx);
411 }
412
vc4_hdmi_handle_hotplug(struct vc4_hdmi * vc4_hdmi,struct drm_modeset_acquire_ctx * ctx,enum drm_connector_status status)413 static void vc4_hdmi_handle_hotplug(struct vc4_hdmi *vc4_hdmi,
414 struct drm_modeset_acquire_ctx *ctx,
415 enum drm_connector_status status)
416 {
417 struct drm_connector *connector = &vc4_hdmi->connector;
418 struct edid *edid;
419 int ret;
420
421 /*
422 * NOTE: This function should really be called with
423 * vc4_hdmi->mutex held, but doing so results in reentrancy
424 * issues since cec_s_phys_addr_from_edid might call
425 * .adap_enable, which leads to that funtion being called with
426 * our mutex held.
427 *
428 * A similar situation occurs with vc4_hdmi_reset_link() that
429 * will call into our KMS hooks if the scrambling was enabled.
430 *
431 * Concurrency isn't an issue at the moment since we don't share
432 * any state with any of the other frameworks so we can ignore
433 * the lock for now.
434 */
435
436 if (status == connector_status_disconnected) {
437 cec_phys_addr_invalidate(vc4_hdmi->cec_adap);
438 return;
439 }
440
441 edid = drm_get_edid(connector, vc4_hdmi->ddc);
442 if (!edid)
443 return;
444
445 cec_s_phys_addr_from_edid(vc4_hdmi->cec_adap, edid);
446 kfree(edid);
447
448 for (;;) {
449 ret = vc4_hdmi_reset_link(connector, ctx);
450 if (ret == -EDEADLK) {
451 drm_modeset_backoff(ctx);
452 continue;
453 }
454
455 break;
456 }
457 }
458
vc4_hdmi_connector_detect_ctx(struct drm_connector * connector,struct drm_modeset_acquire_ctx * ctx,bool force)459 static int vc4_hdmi_connector_detect_ctx(struct drm_connector *connector,
460 struct drm_modeset_acquire_ctx *ctx,
461 bool force)
462 {
463 struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector);
464 enum drm_connector_status status = connector_status_disconnected;
465 int ret;
466
467 /*
468 * NOTE: This function should really take vc4_hdmi->mutex, but
469 * doing so results in reentrancy issues since
470 * vc4_hdmi_handle_hotplug() can call into other functions that
471 * would take the mutex while it's held here.
472 *
473 * Concurrency isn't an issue at the moment since we don't share
474 * any state with any of the other frameworks so we can ignore
475 * the lock for now.
476 */
477
478 ret = pm_runtime_resume_and_get(&vc4_hdmi->pdev->dev);
479 if (ret) {
480 drm_err_once(connector->dev, "Failed to retain HDMI power domain: %d\n",
481 ret);
482 return connector_status_unknown;
483 }
484
485 if (vc4_hdmi->hpd_gpio) {
486 if (gpiod_get_value_cansleep(vc4_hdmi->hpd_gpio))
487 status = connector_status_connected;
488 } else {
489 if (vc4_hdmi->variant->hp_detect &&
490 vc4_hdmi->variant->hp_detect(vc4_hdmi))
491 status = connector_status_connected;
492 }
493
494 vc4_hdmi_handle_hotplug(vc4_hdmi, ctx, status);
495 pm_runtime_put(&vc4_hdmi->pdev->dev);
496
497 return status;
498 }
499
vc4_hdmi_connector_get_modes(struct drm_connector * connector)500 static int vc4_hdmi_connector_get_modes(struct drm_connector *connector)
501 {
502 struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector);
503 struct vc4_dev *vc4 = to_vc4_dev(connector->dev);
504 int ret = 0;
505 struct edid *edid;
506
507 /*
508 * NOTE: This function should really take vc4_hdmi->mutex, but
509 * doing so results in reentrancy issues since
510 * cec_s_phys_addr_from_edid might call .adap_enable, which
511 * leads to that funtion being called with our mutex held.
512 *
513 * Concurrency isn't an issue at the moment since we don't share
514 * any state with any of the other frameworks so we can ignore
515 * the lock for now.
516 */
517
518 edid = drm_get_edid(connector, vc4_hdmi->ddc);
519 cec_s_phys_addr_from_edid(vc4_hdmi->cec_adap, edid);
520 if (!edid)
521 return 0;
522
523 drm_connector_update_edid_property(connector, edid);
524 ret = drm_add_edid_modes(connector, edid);
525 kfree(edid);
526
527 if (!vc4->hvs->vc5_hdmi_enable_hdmi_20) {
528 struct drm_device *drm = connector->dev;
529 const struct drm_display_mode *mode;
530
531 list_for_each_entry(mode, &connector->probed_modes, head) {
532 if (vc4_hdmi_mode_needs_scrambling(mode, 8, VC4_HDMI_OUTPUT_RGB)) {
533 drm_warn_once(drm, "The core clock cannot reach frequencies high enough to support 4k @ 60Hz.");
534 drm_warn_once(drm, "Please change your config.txt file to add hdmi_enable_4kp60.");
535 }
536 }
537 }
538
539 return ret;
540 }
541
vc4_hdmi_connector_atomic_check(struct drm_connector * connector,struct drm_atomic_state * state)542 static int vc4_hdmi_connector_atomic_check(struct drm_connector *connector,
543 struct drm_atomic_state *state)
544 {
545 struct drm_connector_state *old_state =
546 drm_atomic_get_old_connector_state(state, connector);
547 struct vc4_hdmi_connector_state *old_vc4_state =
548 conn_state_to_vc4_hdmi_conn_state(old_state);
549 struct drm_connector_state *new_state =
550 drm_atomic_get_new_connector_state(state, connector);
551 struct vc4_hdmi_connector_state *new_vc4_state =
552 conn_state_to_vc4_hdmi_conn_state(new_state);
553 struct drm_crtc *crtc = new_state->crtc;
554
555 if (!crtc)
556 return 0;
557
558 if (old_state->tv.margins.left != new_state->tv.margins.left ||
559 old_state->tv.margins.right != new_state->tv.margins.right ||
560 old_state->tv.margins.top != new_state->tv.margins.top ||
561 old_state->tv.margins.bottom != new_state->tv.margins.bottom) {
562 struct drm_crtc_state *crtc_state;
563 int ret;
564
565 crtc_state = drm_atomic_get_crtc_state(state, crtc);
566 if (IS_ERR(crtc_state))
567 return PTR_ERR(crtc_state);
568
569 /*
570 * Strictly speaking, we should be calling
571 * drm_atomic_helper_check_planes() after our call to
572 * drm_atomic_add_affected_planes(). However, the
573 * connector atomic_check is called as part of
574 * drm_atomic_helper_check_modeset() that already
575 * happens before a call to
576 * drm_atomic_helper_check_planes() in
577 * drm_atomic_helper_check().
578 */
579 ret = drm_atomic_add_affected_planes(state, crtc);
580 if (ret)
581 return ret;
582 }
583
584 if (old_state->colorspace != new_state->colorspace ||
585 old_vc4_state->broadcast_rgb != new_vc4_state->broadcast_rgb ||
586 !drm_connector_atomic_hdr_metadata_equal(old_state, new_state)) {
587 struct drm_crtc_state *crtc_state;
588
589 crtc_state = drm_atomic_get_crtc_state(state, crtc);
590 if (IS_ERR(crtc_state))
591 return PTR_ERR(crtc_state);
592
593 crtc_state->mode_changed = true;
594 }
595
596 return 0;
597 }
598
vc4_hdmi_connector_get_property(struct drm_connector * connector,const struct drm_connector_state * state,struct drm_property * property,uint64_t * val)599 static int vc4_hdmi_connector_get_property(struct drm_connector *connector,
600 const struct drm_connector_state *state,
601 struct drm_property *property,
602 uint64_t *val)
603 {
604 struct drm_device *drm = connector->dev;
605 struct vc4_hdmi *vc4_hdmi =
606 connector_to_vc4_hdmi(connector);
607 const struct vc4_hdmi_connector_state *vc4_conn_state =
608 conn_state_to_vc4_hdmi_conn_state(state);
609
610 if (property == vc4_hdmi->broadcast_rgb_property) {
611 *val = vc4_conn_state->broadcast_rgb;
612 } else {
613 drm_dbg(drm, "Unknown property [PROP:%d:%s]\n",
614 property->base.id, property->name);
615 return -EINVAL;
616 }
617
618 return 0;
619 }
620
vc4_hdmi_connector_set_property(struct drm_connector * connector,struct drm_connector_state * state,struct drm_property * property,uint64_t val)621 static int vc4_hdmi_connector_set_property(struct drm_connector *connector,
622 struct drm_connector_state *state,
623 struct drm_property *property,
624 uint64_t val)
625 {
626 struct drm_device *drm = connector->dev;
627 struct vc4_hdmi *vc4_hdmi =
628 connector_to_vc4_hdmi(connector);
629 struct vc4_hdmi_connector_state *vc4_conn_state =
630 conn_state_to_vc4_hdmi_conn_state(state);
631
632 if (property == vc4_hdmi->broadcast_rgb_property) {
633 vc4_conn_state->broadcast_rgb = val;
634 return 0;
635 }
636
637 drm_dbg(drm, "Unknown property [PROP:%d:%s]\n",
638 property->base.id, property->name);
639 return -EINVAL;
640 }
641
vc4_hdmi_connector_reset(struct drm_connector * connector)642 static void vc4_hdmi_connector_reset(struct drm_connector *connector)
643 {
644 struct vc4_hdmi_connector_state *old_state =
645 conn_state_to_vc4_hdmi_conn_state(connector->state);
646 struct vc4_hdmi_connector_state *new_state =
647 kzalloc(sizeof(*new_state), GFP_KERNEL);
648
649 if (connector->state)
650 __drm_atomic_helper_connector_destroy_state(connector->state);
651
652 kfree(old_state);
653 __drm_atomic_helper_connector_reset(connector, &new_state->base);
654
655 if (!new_state)
656 return;
657
658 new_state->base.max_bpc = 8;
659 new_state->base.max_requested_bpc = 8;
660 new_state->output_format = VC4_HDMI_OUTPUT_RGB;
661 new_state->broadcast_rgb = VC4_HDMI_BROADCAST_RGB_AUTO;
662 drm_atomic_helper_connector_tv_margins_reset(connector);
663 }
664
665 static struct drm_connector_state *
vc4_hdmi_connector_duplicate_state(struct drm_connector * connector)666 vc4_hdmi_connector_duplicate_state(struct drm_connector *connector)
667 {
668 struct drm_connector_state *conn_state = connector->state;
669 struct vc4_hdmi_connector_state *vc4_state = conn_state_to_vc4_hdmi_conn_state(conn_state);
670 struct vc4_hdmi_connector_state *new_state;
671
672 new_state = kzalloc(sizeof(*new_state), GFP_KERNEL);
673 if (!new_state)
674 return NULL;
675
676 new_state->tmds_char_rate = vc4_state->tmds_char_rate;
677 new_state->output_bpc = vc4_state->output_bpc;
678 new_state->output_format = vc4_state->output_format;
679 new_state->broadcast_rgb = vc4_state->broadcast_rgb;
680 __drm_atomic_helper_connector_duplicate_state(connector, &new_state->base);
681
682 return &new_state->base;
683 }
684
685 static const struct drm_connector_funcs vc4_hdmi_connector_funcs = {
686 .fill_modes = drm_helper_probe_single_connector_modes,
687 .reset = vc4_hdmi_connector_reset,
688 .atomic_duplicate_state = vc4_hdmi_connector_duplicate_state,
689 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
690 .atomic_get_property = vc4_hdmi_connector_get_property,
691 .atomic_set_property = vc4_hdmi_connector_set_property,
692 };
693
694 static const struct drm_connector_helper_funcs vc4_hdmi_connector_helper_funcs = {
695 .detect_ctx = vc4_hdmi_connector_detect_ctx,
696 .get_modes = vc4_hdmi_connector_get_modes,
697 .atomic_check = vc4_hdmi_connector_atomic_check,
698 };
699
700 static const struct drm_prop_enum_list broadcast_rgb_names[] = {
701 { VC4_HDMI_BROADCAST_RGB_AUTO, "Automatic" },
702 { VC4_HDMI_BROADCAST_RGB_FULL, "Full" },
703 { VC4_HDMI_BROADCAST_RGB_LIMITED, "Limited 16:235" },
704 };
705
706 static void
vc4_hdmi_attach_broadcast_rgb_property(struct drm_device * dev,struct vc4_hdmi * vc4_hdmi)707 vc4_hdmi_attach_broadcast_rgb_property(struct drm_device *dev,
708 struct vc4_hdmi *vc4_hdmi)
709 {
710 struct drm_property *prop = vc4_hdmi->broadcast_rgb_property;
711
712 if (!prop) {
713 prop = drm_property_create_enum(dev, DRM_MODE_PROP_ENUM,
714 "Broadcast RGB",
715 broadcast_rgb_names,
716 ARRAY_SIZE(broadcast_rgb_names));
717 if (!prop)
718 return;
719
720 vc4_hdmi->broadcast_rgb_property = prop;
721 }
722
723 drm_object_attach_property(&vc4_hdmi->connector.base, prop,
724 VC4_HDMI_BROADCAST_RGB_AUTO);
725 }
726
vc4_hdmi_connector_init(struct drm_device * dev,struct vc4_hdmi * vc4_hdmi)727 static int vc4_hdmi_connector_init(struct drm_device *dev,
728 struct vc4_hdmi *vc4_hdmi)
729 {
730 struct drm_connector *connector = &vc4_hdmi->connector;
731 struct drm_encoder *encoder = &vc4_hdmi->encoder.base;
732 int ret;
733
734 ret = drmm_connector_init(dev, connector,
735 &vc4_hdmi_connector_funcs,
736 DRM_MODE_CONNECTOR_HDMIA,
737 vc4_hdmi->ddc);
738 if (ret)
739 return ret;
740
741 drm_connector_helper_add(connector, &vc4_hdmi_connector_helper_funcs);
742
743 /*
744 * Some of the properties below require access to state, like bpc.
745 * Allocate some default initial connector state with our reset helper.
746 */
747 if (connector->funcs->reset)
748 connector->funcs->reset(connector);
749
750 /* Create and attach TV margin props to this connector. */
751 ret = drm_mode_create_tv_margin_properties(dev);
752 if (ret)
753 return ret;
754
755 ret = drm_mode_create_hdmi_colorspace_property(connector, 0);
756 if (ret)
757 return ret;
758
759 drm_connector_attach_colorspace_property(connector);
760 drm_connector_attach_tv_margin_properties(connector);
761 drm_connector_attach_max_bpc_property(connector, 8, 12);
762
763 connector->polled = (DRM_CONNECTOR_POLL_CONNECT |
764 DRM_CONNECTOR_POLL_DISCONNECT);
765
766 connector->interlace_allowed = 1;
767 connector->doublescan_allowed = 0;
768 connector->stereo_allowed = 1;
769
770 if (vc4_hdmi->variant->supports_hdr)
771 drm_connector_attach_hdr_output_metadata_property(connector);
772
773 vc4_hdmi_attach_broadcast_rgb_property(dev, vc4_hdmi);
774
775 drm_connector_attach_encoder(connector, encoder);
776
777 return 0;
778 }
779
vc4_hdmi_stop_packet(struct drm_encoder * encoder,enum hdmi_infoframe_type type,bool poll)780 static int vc4_hdmi_stop_packet(struct drm_encoder *encoder,
781 enum hdmi_infoframe_type type,
782 bool poll)
783 {
784 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
785 struct drm_device *drm = vc4_hdmi->connector.dev;
786 u32 packet_id = type - 0x80;
787 unsigned long flags;
788 int ret = 0;
789 int idx;
790
791 if (!drm_dev_enter(drm, &idx))
792 return -ENODEV;
793
794 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
795 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
796 HDMI_READ(HDMI_RAM_PACKET_CONFIG) & ~BIT(packet_id));
797 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
798
799 if (poll) {
800 ret = wait_for(!(HDMI_READ(HDMI_RAM_PACKET_STATUS) &
801 BIT(packet_id)), 100);
802 }
803
804 drm_dev_exit(idx);
805 return ret;
806 }
807
vc4_hdmi_write_infoframe(struct drm_encoder * encoder,union hdmi_infoframe * frame)808 static void vc4_hdmi_write_infoframe(struct drm_encoder *encoder,
809 union hdmi_infoframe *frame)
810 {
811 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
812 struct drm_device *drm = vc4_hdmi->connector.dev;
813 u32 packet_id = frame->any.type - 0x80;
814 const struct vc4_hdmi_register *ram_packet_start =
815 &vc4_hdmi->variant->registers[HDMI_RAM_PACKET_START];
816 u32 packet_reg = ram_packet_start->offset + VC4_HDMI_PACKET_STRIDE * packet_id;
817 u32 packet_reg_next = ram_packet_start->offset +
818 VC4_HDMI_PACKET_STRIDE * (packet_id + 1);
819 void __iomem *base = __vc4_hdmi_get_field_base(vc4_hdmi,
820 ram_packet_start->reg);
821 uint8_t buffer[VC4_HDMI_PACKET_STRIDE] = {};
822 unsigned long flags;
823 ssize_t len, i;
824 int ret;
825 int idx;
826
827 if (!drm_dev_enter(drm, &idx))
828 return;
829
830 WARN_ONCE(!(HDMI_READ(HDMI_RAM_PACKET_CONFIG) &
831 VC4_HDMI_RAM_PACKET_ENABLE),
832 "Packet RAM has to be on to store the packet.");
833
834 len = hdmi_infoframe_pack(frame, buffer, sizeof(buffer));
835 if (len < 0)
836 goto out;
837
838 ret = vc4_hdmi_stop_packet(encoder, frame->any.type, true);
839 if (ret) {
840 DRM_ERROR("Failed to wait for infoframe to go idle: %d\n", ret);
841 goto out;
842 }
843
844 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
845
846 for (i = 0; i < len; i += 7) {
847 writel(buffer[i + 0] << 0 |
848 buffer[i + 1] << 8 |
849 buffer[i + 2] << 16,
850 base + packet_reg);
851 packet_reg += 4;
852
853 writel(buffer[i + 3] << 0 |
854 buffer[i + 4] << 8 |
855 buffer[i + 5] << 16 |
856 buffer[i + 6] << 24,
857 base + packet_reg);
858 packet_reg += 4;
859 }
860
861 /*
862 * clear remainder of packet ram as it's included in the
863 * infoframe and triggers a checksum error on hdmi analyser
864 */
865 for (; packet_reg < packet_reg_next; packet_reg += 4)
866 writel(0, base + packet_reg);
867
868 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
869 HDMI_READ(HDMI_RAM_PACKET_CONFIG) | BIT(packet_id));
870
871 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
872
873 ret = wait_for((HDMI_READ(HDMI_RAM_PACKET_STATUS) &
874 BIT(packet_id)), 100);
875 if (ret)
876 DRM_ERROR("Failed to wait for infoframe to start: %d\n", ret);
877
878 out:
879 drm_dev_exit(idx);
880 }
881
vc4_hdmi_avi_infoframe_colorspace(struct hdmi_avi_infoframe * frame,enum vc4_hdmi_output_format fmt)882 static void vc4_hdmi_avi_infoframe_colorspace(struct hdmi_avi_infoframe *frame,
883 enum vc4_hdmi_output_format fmt)
884 {
885 switch (fmt) {
886 case VC4_HDMI_OUTPUT_RGB:
887 frame->colorspace = HDMI_COLORSPACE_RGB;
888 break;
889
890 case VC4_HDMI_OUTPUT_YUV420:
891 frame->colorspace = HDMI_COLORSPACE_YUV420;
892 break;
893
894 case VC4_HDMI_OUTPUT_YUV422:
895 frame->colorspace = HDMI_COLORSPACE_YUV422;
896 break;
897
898 case VC4_HDMI_OUTPUT_YUV444:
899 frame->colorspace = HDMI_COLORSPACE_YUV444;
900 break;
901
902 default:
903 break;
904 }
905 }
906
vc4_hdmi_set_avi_infoframe(struct drm_encoder * encoder)907 static void vc4_hdmi_set_avi_infoframe(struct drm_encoder *encoder)
908 {
909 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
910 struct drm_connector *connector = &vc4_hdmi->connector;
911 struct drm_connector_state *cstate = connector->state;
912 struct vc4_hdmi_connector_state *vc4_state =
913 conn_state_to_vc4_hdmi_conn_state(cstate);
914 const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
915 union hdmi_infoframe frame;
916 int ret;
917
918 lockdep_assert_held(&vc4_hdmi->mutex);
919
920 ret = drm_hdmi_avi_infoframe_from_display_mode(&frame.avi,
921 connector, mode);
922 if (ret < 0) {
923 DRM_ERROR("couldn't fill AVI infoframe\n");
924 return;
925 }
926
927 drm_hdmi_avi_infoframe_quant_range(&frame.avi,
928 connector, mode,
929 vc4_hdmi_is_full_range(vc4_hdmi, vc4_state) ?
930 HDMI_QUANTIZATION_RANGE_FULL :
931 HDMI_QUANTIZATION_RANGE_LIMITED);
932 drm_hdmi_avi_infoframe_colorimetry(&frame.avi, cstate);
933 vc4_hdmi_avi_infoframe_colorspace(&frame.avi, vc4_state->output_format);
934 drm_hdmi_avi_infoframe_bars(&frame.avi, cstate);
935
936 vc4_hdmi_write_infoframe(encoder, &frame);
937 }
938
vc4_hdmi_set_spd_infoframe(struct drm_encoder * encoder)939 static void vc4_hdmi_set_spd_infoframe(struct drm_encoder *encoder)
940 {
941 union hdmi_infoframe frame;
942 int ret;
943
944 ret = hdmi_spd_infoframe_init(&frame.spd, "Broadcom", "Videocore");
945 if (ret < 0) {
946 DRM_ERROR("couldn't fill SPD infoframe\n");
947 return;
948 }
949
950 frame.spd.sdi = HDMI_SPD_SDI_PC;
951
952 vc4_hdmi_write_infoframe(encoder, &frame);
953 }
954
vc4_hdmi_set_audio_infoframe(struct drm_encoder * encoder)955 static void vc4_hdmi_set_audio_infoframe(struct drm_encoder *encoder)
956 {
957 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
958 struct hdmi_audio_infoframe *audio = &vc4_hdmi->audio.infoframe;
959 union hdmi_infoframe frame;
960
961 memcpy(&frame.audio, audio, sizeof(*audio));
962
963 if (vc4_hdmi->packet_ram_enabled)
964 vc4_hdmi_write_infoframe(encoder, &frame);
965 }
966
vc4_hdmi_set_hdr_infoframe(struct drm_encoder * encoder)967 static void vc4_hdmi_set_hdr_infoframe(struct drm_encoder *encoder)
968 {
969 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
970 struct drm_connector *connector = &vc4_hdmi->connector;
971 struct drm_connector_state *conn_state = connector->state;
972 union hdmi_infoframe frame;
973
974 lockdep_assert_held(&vc4_hdmi->mutex);
975
976 if (!vc4_hdmi->variant->supports_hdr)
977 return;
978
979 if (!conn_state->hdr_output_metadata)
980 return;
981
982 if (drm_hdmi_infoframe_set_hdr_metadata(&frame.drm, conn_state))
983 return;
984
985 vc4_hdmi_write_infoframe(encoder, &frame);
986 }
987
vc4_hdmi_set_infoframes(struct drm_encoder * encoder)988 static void vc4_hdmi_set_infoframes(struct drm_encoder *encoder)
989 {
990 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
991
992 lockdep_assert_held(&vc4_hdmi->mutex);
993
994 vc4_hdmi_set_avi_infoframe(encoder);
995 vc4_hdmi_set_spd_infoframe(encoder);
996 /*
997 * If audio was streaming, then we need to reenabled the audio
998 * infoframe here during encoder_enable.
999 */
1000 if (vc4_hdmi->audio.streaming)
1001 vc4_hdmi_set_audio_infoframe(encoder);
1002
1003 vc4_hdmi_set_hdr_infoframe(encoder);
1004 }
1005
1006 #define SCRAMBLING_POLLING_DELAY_MS 1000
1007
vc4_hdmi_enable_scrambling(struct drm_encoder * encoder)1008 static void vc4_hdmi_enable_scrambling(struct drm_encoder *encoder)
1009 {
1010 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1011 struct drm_connector *connector = &vc4_hdmi->connector;
1012 struct drm_device *drm = connector->dev;
1013 const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
1014 unsigned long flags;
1015 int idx;
1016
1017 lockdep_assert_held(&vc4_hdmi->mutex);
1018
1019 if (!vc4_hdmi_supports_scrambling(vc4_hdmi))
1020 return;
1021
1022 if (!vc4_hdmi_mode_needs_scrambling(mode,
1023 vc4_hdmi->output_bpc,
1024 vc4_hdmi->output_format))
1025 return;
1026
1027 if (!drm_dev_enter(drm, &idx))
1028 return;
1029
1030 drm_scdc_set_high_tmds_clock_ratio(connector, true);
1031 drm_scdc_set_scrambling(connector, true);
1032
1033 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1034 HDMI_WRITE(HDMI_SCRAMBLER_CTL, HDMI_READ(HDMI_SCRAMBLER_CTL) |
1035 VC5_HDMI_SCRAMBLER_CTL_ENABLE);
1036 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1037
1038 drm_dev_exit(idx);
1039
1040 vc4_hdmi->scdc_enabled = true;
1041
1042 queue_delayed_work(system_wq, &vc4_hdmi->scrambling_work,
1043 msecs_to_jiffies(SCRAMBLING_POLLING_DELAY_MS));
1044 }
1045
vc4_hdmi_disable_scrambling(struct drm_encoder * encoder)1046 static void vc4_hdmi_disable_scrambling(struct drm_encoder *encoder)
1047 {
1048 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1049 struct drm_connector *connector = &vc4_hdmi->connector;
1050 struct drm_device *drm = connector->dev;
1051 unsigned long flags;
1052 int idx;
1053
1054 lockdep_assert_held(&vc4_hdmi->mutex);
1055
1056 if (!vc4_hdmi->scdc_enabled)
1057 return;
1058
1059 vc4_hdmi->scdc_enabled = false;
1060
1061 if (delayed_work_pending(&vc4_hdmi->scrambling_work))
1062 cancel_delayed_work_sync(&vc4_hdmi->scrambling_work);
1063
1064 if (!drm_dev_enter(drm, &idx))
1065 return;
1066
1067 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1068 HDMI_WRITE(HDMI_SCRAMBLER_CTL, HDMI_READ(HDMI_SCRAMBLER_CTL) &
1069 ~VC5_HDMI_SCRAMBLER_CTL_ENABLE);
1070 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1071
1072 drm_scdc_set_scrambling(connector, false);
1073 drm_scdc_set_high_tmds_clock_ratio(connector, false);
1074
1075 drm_dev_exit(idx);
1076 }
1077
vc4_hdmi_scrambling_wq(struct work_struct * work)1078 static void vc4_hdmi_scrambling_wq(struct work_struct *work)
1079 {
1080 struct vc4_hdmi *vc4_hdmi = container_of(to_delayed_work(work),
1081 struct vc4_hdmi,
1082 scrambling_work);
1083 struct drm_connector *connector = &vc4_hdmi->connector;
1084
1085 if (drm_scdc_get_scrambling_status(connector))
1086 return;
1087
1088 drm_scdc_set_high_tmds_clock_ratio(connector, true);
1089 drm_scdc_set_scrambling(connector, true);
1090
1091 queue_delayed_work(system_wq, &vc4_hdmi->scrambling_work,
1092 msecs_to_jiffies(SCRAMBLING_POLLING_DELAY_MS));
1093 }
1094
vc4_hdmi_encoder_post_crtc_disable(struct drm_encoder * encoder,struct drm_atomic_state * state)1095 static void vc4_hdmi_encoder_post_crtc_disable(struct drm_encoder *encoder,
1096 struct drm_atomic_state *state)
1097 {
1098 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1099 struct drm_device *drm = vc4_hdmi->connector.dev;
1100 unsigned long flags;
1101 int idx;
1102
1103 mutex_lock(&vc4_hdmi->mutex);
1104
1105 vc4_hdmi->packet_ram_enabled = false;
1106
1107 if (!drm_dev_enter(drm, &idx))
1108 goto out;
1109
1110 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1111
1112 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG, 0);
1113
1114 HDMI_WRITE(HDMI_VID_CTL, HDMI_READ(HDMI_VID_CTL) | VC4_HD_VID_CTL_CLRRGB);
1115
1116 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1117
1118 mdelay(1);
1119
1120 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1121 HDMI_WRITE(HDMI_VID_CTL,
1122 HDMI_READ(HDMI_VID_CTL) & ~VC4_HD_VID_CTL_ENABLE);
1123 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1124
1125 vc4_hdmi_disable_scrambling(encoder);
1126
1127 drm_dev_exit(idx);
1128
1129 out:
1130 mutex_unlock(&vc4_hdmi->mutex);
1131 }
1132
vc4_hdmi_encoder_post_crtc_powerdown(struct drm_encoder * encoder,struct drm_atomic_state * state)1133 static void vc4_hdmi_encoder_post_crtc_powerdown(struct drm_encoder *encoder,
1134 struct drm_atomic_state *state)
1135 {
1136 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1137 struct drm_device *drm = vc4_hdmi->connector.dev;
1138 unsigned long flags;
1139 int ret;
1140 int idx;
1141
1142 mutex_lock(&vc4_hdmi->mutex);
1143
1144 if (!drm_dev_enter(drm, &idx))
1145 goto out;
1146
1147 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1148 HDMI_WRITE(HDMI_VID_CTL,
1149 HDMI_READ(HDMI_VID_CTL) | VC4_HD_VID_CTL_BLANKPIX);
1150 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1151
1152 if (vc4_hdmi->variant->phy_disable)
1153 vc4_hdmi->variant->phy_disable(vc4_hdmi);
1154
1155 clk_disable_unprepare(vc4_hdmi->pixel_bvb_clock);
1156 clk_disable_unprepare(vc4_hdmi->pixel_clock);
1157
1158 ret = pm_runtime_put(&vc4_hdmi->pdev->dev);
1159 if (ret < 0)
1160 DRM_ERROR("Failed to release power domain: %d\n", ret);
1161
1162 drm_dev_exit(idx);
1163
1164 out:
1165 mutex_unlock(&vc4_hdmi->mutex);
1166 }
1167
vc4_hdmi_csc_setup(struct vc4_hdmi * vc4_hdmi,struct drm_connector_state * state,const struct drm_display_mode * mode)1168 static void vc4_hdmi_csc_setup(struct vc4_hdmi *vc4_hdmi,
1169 struct drm_connector_state *state,
1170 const struct drm_display_mode *mode)
1171 {
1172 struct vc4_hdmi_connector_state *vc4_state =
1173 conn_state_to_vc4_hdmi_conn_state(state);
1174 struct drm_device *drm = vc4_hdmi->connector.dev;
1175 unsigned long flags;
1176 u32 csc_ctl;
1177 int idx;
1178
1179 if (!drm_dev_enter(drm, &idx))
1180 return;
1181
1182 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1183
1184 csc_ctl = VC4_SET_FIELD(VC4_HD_CSC_CTL_ORDER_BGR,
1185 VC4_HD_CSC_CTL_ORDER);
1186
1187 if (!vc4_hdmi_is_full_range(vc4_hdmi, vc4_state)) {
1188 /* CEA VICs other than #1 requre limited range RGB
1189 * output unless overridden by an AVI infoframe.
1190 * Apply a colorspace conversion to squash 0-255 down
1191 * to 16-235. The matrix here is:
1192 *
1193 * [ 0 0 0.8594 16]
1194 * [ 0 0.8594 0 16]
1195 * [ 0.8594 0 0 16]
1196 * [ 0 0 0 1]
1197 */
1198 csc_ctl |= VC4_HD_CSC_CTL_ENABLE;
1199 csc_ctl |= VC4_HD_CSC_CTL_RGB2YCC;
1200 csc_ctl |= VC4_SET_FIELD(VC4_HD_CSC_CTL_MODE_CUSTOM,
1201 VC4_HD_CSC_CTL_MODE);
1202
1203 HDMI_WRITE(HDMI_CSC_12_11, (0x000 << 16) | 0x000);
1204 HDMI_WRITE(HDMI_CSC_14_13, (0x100 << 16) | 0x6e0);
1205 HDMI_WRITE(HDMI_CSC_22_21, (0x6e0 << 16) | 0x000);
1206 HDMI_WRITE(HDMI_CSC_24_23, (0x100 << 16) | 0x000);
1207 HDMI_WRITE(HDMI_CSC_32_31, (0x000 << 16) | 0x6e0);
1208 HDMI_WRITE(HDMI_CSC_34_33, (0x100 << 16) | 0x000);
1209 }
1210
1211 /* The RGB order applies even when CSC is disabled. */
1212 HDMI_WRITE(HDMI_CSC_CTL, csc_ctl);
1213
1214 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1215
1216 drm_dev_exit(idx);
1217 }
1218
1219 /*
1220 * Matrices for (internal) RGB to RGB output.
1221 *
1222 * Matrices are signed 2p13 fixed point, with signed 9p6 offsets
1223 */
1224 static const u16 vc5_hdmi_csc_full_rgb_to_rgb[2][3][4] = {
1225 {
1226 /*
1227 * Full range - unity
1228 *
1229 * [ 1 0 0 0]
1230 * [ 0 1 0 0]
1231 * [ 0 0 1 0]
1232 */
1233 { 0x2000, 0x0000, 0x0000, 0x0000 },
1234 { 0x0000, 0x2000, 0x0000, 0x0000 },
1235 { 0x0000, 0x0000, 0x2000, 0x0000 },
1236 },
1237 {
1238 /*
1239 * Limited range
1240 *
1241 * CEA VICs other than #1 require limited range RGB
1242 * output unless overridden by an AVI infoframe. Apply a
1243 * colorspace conversion to squash 0-255 down to 16-235.
1244 * The matrix here is:
1245 *
1246 * [ 0.8594 0 0 16]
1247 * [ 0 0.8594 0 16]
1248 * [ 0 0 0.8594 16]
1249 */
1250 { 0x1b80, 0x0000, 0x0000, 0x0400 },
1251 { 0x0000, 0x1b80, 0x0000, 0x0400 },
1252 { 0x0000, 0x0000, 0x1b80, 0x0400 },
1253 },
1254 };
1255
1256 /*
1257 * Conversion between Full Range RGB and YUV using the BT.601 Colorspace
1258 *
1259 * Matrices are signed 2p13 fixed point, with signed 9p6 offsets
1260 */
1261 static const u16 vc5_hdmi_csc_full_rgb_to_yuv_bt601[2][3][4] = {
1262 {
1263 /*
1264 * Full Range
1265 *
1266 * [ 0.299000 0.587000 0.114000 0 ]
1267 * [ -0.168736 -0.331264 0.500000 128 ]
1268 * [ 0.500000 -0.418688 -0.081312 128 ]
1269 */
1270 { 0x0991, 0x12c9, 0x03a6, 0x0000 },
1271 { 0xfa9b, 0xf567, 0x1000, 0x2000 },
1272 { 0x1000, 0xf29b, 0xfd67, 0x2000 },
1273 },
1274 {
1275 /* Limited Range
1276 *
1277 * [ 0.255785 0.502160 0.097523 16 ]
1278 * [ -0.147644 -0.289856 0.437500 128 ]
1279 * [ 0.437500 -0.366352 -0.071148 128 ]
1280 */
1281 { 0x082f, 0x1012, 0x031f, 0x0400 },
1282 { 0xfb48, 0xf6ba, 0x0e00, 0x2000 },
1283 { 0x0e00, 0xf448, 0xfdba, 0x2000 },
1284 },
1285 };
1286
1287 /*
1288 * Conversion between Full Range RGB and YUV using the BT.709 Colorspace
1289 *
1290 * Matrices are signed 2p13 fixed point, with signed 9p6 offsets
1291 */
1292 static const u16 vc5_hdmi_csc_full_rgb_to_yuv_bt709[2][3][4] = {
1293 {
1294 /*
1295 * Full Range
1296 *
1297 * [ 0.212600 0.715200 0.072200 0 ]
1298 * [ -0.114572 -0.385428 0.500000 128 ]
1299 * [ 0.500000 -0.454153 -0.045847 128 ]
1300 */
1301 { 0x06ce, 0x16e3, 0x024f, 0x0000 },
1302 { 0xfc56, 0xf3ac, 0x1000, 0x2000 },
1303 { 0x1000, 0xf179, 0xfe89, 0x2000 },
1304 },
1305 {
1306 /*
1307 * Limited Range
1308 *
1309 * [ 0.181906 0.611804 0.061758 16 ]
1310 * [ -0.100268 -0.337232 0.437500 128 ]
1311 * [ 0.437500 -0.397386 -0.040114 128 ]
1312 */
1313 { 0x05d2, 0x1394, 0x01fa, 0x0400 },
1314 { 0xfccc, 0xf536, 0x0e00, 0x2000 },
1315 { 0x0e00, 0xf34a, 0xfeb8, 0x2000 },
1316 },
1317 };
1318
1319 /*
1320 * Conversion between Full Range RGB and YUV using the BT.2020 Colorspace
1321 *
1322 * Matrices are signed 2p13 fixed point, with signed 9p6 offsets
1323 */
1324 static const u16 vc5_hdmi_csc_full_rgb_to_yuv_bt2020[2][3][4] = {
1325 {
1326 /*
1327 * Full Range
1328 *
1329 * [ 0.262700 0.678000 0.059300 0 ]
1330 * [ -0.139630 -0.360370 0.500000 128 ]
1331 * [ 0.500000 -0.459786 -0.040214 128 ]
1332 */
1333 { 0x0868, 0x15b2, 0x01e6, 0x0000 },
1334 { 0xfb89, 0xf479, 0x1000, 0x2000 },
1335 { 0x1000, 0xf14a, 0xfeb8, 0x2000 },
1336 },
1337 {
1338 /* Limited Range
1339 *
1340 * [ 0.224732 0.580008 0.050729 16 ]
1341 * [ -0.122176 -0.315324 0.437500 128 ]
1342 * [ 0.437500 -0.402312 -0.035188 128 ]
1343 */
1344 { 0x082f, 0x1012, 0x031f, 0x0400 },
1345 { 0xfb48, 0xf6ba, 0x0e00, 0x2000 },
1346 { 0x0e00, 0xf448, 0xfdba, 0x2000 },
1347 },
1348 };
1349
vc5_hdmi_set_csc_coeffs(struct vc4_hdmi * vc4_hdmi,const u16 coeffs[3][4])1350 static void vc5_hdmi_set_csc_coeffs(struct vc4_hdmi *vc4_hdmi,
1351 const u16 coeffs[3][4])
1352 {
1353 lockdep_assert_held(&vc4_hdmi->hw_lock);
1354
1355 HDMI_WRITE(HDMI_CSC_12_11, (coeffs[0][1] << 16) | coeffs[0][0]);
1356 HDMI_WRITE(HDMI_CSC_14_13, (coeffs[0][3] << 16) | coeffs[0][2]);
1357 HDMI_WRITE(HDMI_CSC_22_21, (coeffs[1][1] << 16) | coeffs[1][0]);
1358 HDMI_WRITE(HDMI_CSC_24_23, (coeffs[1][3] << 16) | coeffs[1][2]);
1359 HDMI_WRITE(HDMI_CSC_32_31, (coeffs[2][1] << 16) | coeffs[2][0]);
1360 HDMI_WRITE(HDMI_CSC_34_33, (coeffs[2][3] << 16) | coeffs[2][2]);
1361 }
1362
vc5_hdmi_set_csc_coeffs_swap(struct vc4_hdmi * vc4_hdmi,const u16 coeffs[3][4])1363 static void vc5_hdmi_set_csc_coeffs_swap(struct vc4_hdmi *vc4_hdmi,
1364 const u16 coeffs[3][4])
1365 {
1366 lockdep_assert_held(&vc4_hdmi->hw_lock);
1367
1368 /* YUV444 needs the CSC matrices using the channels in a different order */
1369 HDMI_WRITE(HDMI_CSC_12_11, (coeffs[1][1] << 16) | coeffs[1][0]);
1370 HDMI_WRITE(HDMI_CSC_14_13, (coeffs[1][3] << 16) | coeffs[1][2]);
1371 HDMI_WRITE(HDMI_CSC_22_21, (coeffs[2][1] << 16) | coeffs[2][0]);
1372 HDMI_WRITE(HDMI_CSC_24_23, (coeffs[2][3] << 16) | coeffs[2][2]);
1373 HDMI_WRITE(HDMI_CSC_32_31, (coeffs[0][1] << 16) | coeffs[0][0]);
1374 HDMI_WRITE(HDMI_CSC_34_33, (coeffs[0][3] << 16) | coeffs[0][2]);
1375 }
1376
1377 static const u16
vc5_hdmi_find_yuv_csc_coeffs(struct vc4_hdmi * vc4_hdmi,u32 colorspace,bool limited)1378 (*vc5_hdmi_find_yuv_csc_coeffs(struct vc4_hdmi *vc4_hdmi, u32 colorspace, bool limited))[4]
1379 {
1380 switch (colorspace) {
1381 case DRM_MODE_COLORIMETRY_SMPTE_170M_YCC:
1382 case DRM_MODE_COLORIMETRY_XVYCC_601:
1383 case DRM_MODE_COLORIMETRY_SYCC_601:
1384 case DRM_MODE_COLORIMETRY_OPYCC_601:
1385 case DRM_MODE_COLORIMETRY_BT601_YCC:
1386 return vc5_hdmi_csc_full_rgb_to_yuv_bt601[limited];
1387
1388 default:
1389 case DRM_MODE_COLORIMETRY_NO_DATA:
1390 case DRM_MODE_COLORIMETRY_BT709_YCC:
1391 case DRM_MODE_COLORIMETRY_XVYCC_709:
1392 case DRM_MODE_COLORIMETRY_RGB_WIDE_FIXED:
1393 case DRM_MODE_COLORIMETRY_RGB_WIDE_FLOAT:
1394 return vc5_hdmi_csc_full_rgb_to_yuv_bt709[limited];
1395
1396 case DRM_MODE_COLORIMETRY_BT2020_CYCC:
1397 case DRM_MODE_COLORIMETRY_BT2020_YCC:
1398 case DRM_MODE_COLORIMETRY_BT2020_RGB:
1399 case DRM_MODE_COLORIMETRY_DCI_P3_RGB_D65:
1400 case DRM_MODE_COLORIMETRY_DCI_P3_RGB_THEATER:
1401 return vc5_hdmi_csc_full_rgb_to_yuv_bt2020[limited];
1402 }
1403 }
1404
vc5_hdmi_csc_setup(struct vc4_hdmi * vc4_hdmi,struct drm_connector_state * state,const struct drm_display_mode * mode)1405 static void vc5_hdmi_csc_setup(struct vc4_hdmi *vc4_hdmi,
1406 struct drm_connector_state *state,
1407 const struct drm_display_mode *mode)
1408 {
1409 struct drm_device *drm = vc4_hdmi->connector.dev;
1410 struct vc4_hdmi_connector_state *vc4_state =
1411 conn_state_to_vc4_hdmi_conn_state(state);
1412 unsigned int lim_range = vc4_hdmi_is_full_range(vc4_hdmi, vc4_state) ? 0 : 1;
1413 unsigned long flags;
1414 const u16 (*csc)[4];
1415 u32 if_cfg = 0;
1416 u32 if_xbar = 0x543210;
1417 u32 csc_chan_ctl = 0;
1418 u32 csc_ctl = VC5_MT_CP_CSC_CTL_ENABLE | VC4_SET_FIELD(VC4_HD_CSC_CTL_MODE_CUSTOM,
1419 VC5_MT_CP_CSC_CTL_MODE);
1420 int idx;
1421
1422 if (!drm_dev_enter(drm, &idx))
1423 return;
1424
1425 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1426
1427 switch (vc4_state->output_format) {
1428 case VC4_HDMI_OUTPUT_YUV444:
1429 csc = vc5_hdmi_find_yuv_csc_coeffs(vc4_hdmi, state->colorspace, !!lim_range);
1430
1431 vc5_hdmi_set_csc_coeffs_swap(vc4_hdmi, csc);
1432 break;
1433
1434 case VC4_HDMI_OUTPUT_YUV422:
1435 csc = vc5_hdmi_find_yuv_csc_coeffs(vc4_hdmi, state->colorspace, !!lim_range);
1436
1437 csc_ctl |= VC4_SET_FIELD(VC5_MT_CP_CSC_CTL_FILTER_MODE_444_TO_422_STANDARD,
1438 VC5_MT_CP_CSC_CTL_FILTER_MODE_444_TO_422) |
1439 VC5_MT_CP_CSC_CTL_USE_444_TO_422 |
1440 VC5_MT_CP_CSC_CTL_USE_RNG_SUPPRESSION;
1441
1442 csc_chan_ctl |= VC4_SET_FIELD(VC5_MT_CP_CHANNEL_CTL_OUTPUT_REMAP_LEGACY_STYLE,
1443 VC5_MT_CP_CHANNEL_CTL_OUTPUT_REMAP);
1444
1445 if_cfg |= VC4_SET_FIELD(VC5_DVP_HT_VEC_INTERFACE_CFG_SEL_422_FORMAT_422_LEGACY,
1446 VC5_DVP_HT_VEC_INTERFACE_CFG_SEL_422);
1447
1448 vc5_hdmi_set_csc_coeffs(vc4_hdmi, csc);
1449 break;
1450
1451 case VC4_HDMI_OUTPUT_RGB:
1452 if_xbar = 0x354021;
1453
1454 vc5_hdmi_set_csc_coeffs(vc4_hdmi, vc5_hdmi_csc_full_rgb_to_rgb[lim_range]);
1455 break;
1456
1457 default:
1458 break;
1459 }
1460
1461 HDMI_WRITE(HDMI_VEC_INTERFACE_CFG, if_cfg);
1462 HDMI_WRITE(HDMI_VEC_INTERFACE_XBAR, if_xbar);
1463 HDMI_WRITE(HDMI_CSC_CHANNEL_CTL, csc_chan_ctl);
1464 HDMI_WRITE(HDMI_CSC_CTL, csc_ctl);
1465
1466 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1467
1468 drm_dev_exit(idx);
1469 }
1470
vc4_hdmi_set_timings(struct vc4_hdmi * vc4_hdmi,struct drm_connector_state * state,const struct drm_display_mode * mode)1471 static void vc4_hdmi_set_timings(struct vc4_hdmi *vc4_hdmi,
1472 struct drm_connector_state *state,
1473 const struct drm_display_mode *mode)
1474 {
1475 struct drm_device *drm = vc4_hdmi->connector.dev;
1476 bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
1477 bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
1478 bool interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE;
1479 u32 pixel_rep = (mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1;
1480 u32 verta = (VC4_SET_FIELD(mode->crtc_vsync_end - mode->crtc_vsync_start,
1481 VC4_HDMI_VERTA_VSP) |
1482 VC4_SET_FIELD(mode->crtc_vsync_start - mode->crtc_vdisplay,
1483 VC4_HDMI_VERTA_VFP) |
1484 VC4_SET_FIELD(mode->crtc_vdisplay, VC4_HDMI_VERTA_VAL));
1485 u32 vertb = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) |
1486 VC4_SET_FIELD(mode->crtc_vtotal - mode->crtc_vsync_end +
1487 interlaced,
1488 VC4_HDMI_VERTB_VBP));
1489 u32 vertb_even = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) |
1490 VC4_SET_FIELD(mode->crtc_vtotal -
1491 mode->crtc_vsync_end,
1492 VC4_HDMI_VERTB_VBP));
1493 unsigned long flags;
1494 u32 reg;
1495 int idx;
1496
1497 if (!drm_dev_enter(drm, &idx))
1498 return;
1499
1500 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1501
1502 HDMI_WRITE(HDMI_HORZA,
1503 (vsync_pos ? VC4_HDMI_HORZA_VPOS : 0) |
1504 (hsync_pos ? VC4_HDMI_HORZA_HPOS : 0) |
1505 VC4_SET_FIELD(mode->hdisplay * pixel_rep,
1506 VC4_HDMI_HORZA_HAP));
1507
1508 HDMI_WRITE(HDMI_HORZB,
1509 VC4_SET_FIELD((mode->htotal -
1510 mode->hsync_end) * pixel_rep,
1511 VC4_HDMI_HORZB_HBP) |
1512 VC4_SET_FIELD((mode->hsync_end -
1513 mode->hsync_start) * pixel_rep,
1514 VC4_HDMI_HORZB_HSP) |
1515 VC4_SET_FIELD((mode->hsync_start -
1516 mode->hdisplay) * pixel_rep,
1517 VC4_HDMI_HORZB_HFP));
1518
1519 HDMI_WRITE(HDMI_VERTA0, verta);
1520 HDMI_WRITE(HDMI_VERTA1, verta);
1521
1522 HDMI_WRITE(HDMI_VERTB0, vertb_even);
1523 HDMI_WRITE(HDMI_VERTB1, vertb);
1524
1525 reg = HDMI_READ(HDMI_MISC_CONTROL);
1526 reg &= ~VC4_HDMI_MISC_CONTROL_PIXEL_REP_MASK;
1527 reg |= VC4_SET_FIELD(pixel_rep - 1, VC4_HDMI_MISC_CONTROL_PIXEL_REP);
1528 HDMI_WRITE(HDMI_MISC_CONTROL, reg);
1529
1530 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1531
1532 drm_dev_exit(idx);
1533 }
1534
vc5_hdmi_set_timings(struct vc4_hdmi * vc4_hdmi,struct drm_connector_state * state,const struct drm_display_mode * mode)1535 static void vc5_hdmi_set_timings(struct vc4_hdmi *vc4_hdmi,
1536 struct drm_connector_state *state,
1537 const struct drm_display_mode *mode)
1538 {
1539 struct drm_device *drm = vc4_hdmi->connector.dev;
1540 const struct vc4_hdmi_connector_state *vc4_state =
1541 conn_state_to_vc4_hdmi_conn_state(state);
1542 bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
1543 bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
1544 bool interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE;
1545 u32 pixel_rep = (mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1;
1546 u32 verta = (VC4_SET_FIELD(mode->crtc_vsync_end - mode->crtc_vsync_start,
1547 VC5_HDMI_VERTA_VSP) |
1548 VC4_SET_FIELD(mode->crtc_vsync_start - mode->crtc_vdisplay,
1549 VC5_HDMI_VERTA_VFP) |
1550 VC4_SET_FIELD(mode->crtc_vdisplay, VC5_HDMI_VERTA_VAL));
1551 u32 vertb = (VC4_SET_FIELD(mode->htotal >> (2 - pixel_rep),
1552 VC5_HDMI_VERTB_VSPO) |
1553 VC4_SET_FIELD(mode->crtc_vtotal - mode->crtc_vsync_end +
1554 interlaced,
1555 VC4_HDMI_VERTB_VBP));
1556 u32 vertb_even = (VC4_SET_FIELD(0, VC5_HDMI_VERTB_VSPO) |
1557 VC4_SET_FIELD(mode->crtc_vtotal -
1558 mode->crtc_vsync_end,
1559 VC4_HDMI_VERTB_VBP));
1560 unsigned long flags;
1561 unsigned char gcp;
1562 u32 reg;
1563 int idx;
1564
1565 if (!drm_dev_enter(drm, &idx))
1566 return;
1567
1568 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1569
1570 HDMI_WRITE(HDMI_HORZA,
1571 (vsync_pos ? VC5_HDMI_HORZA_VPOS : 0) |
1572 (hsync_pos ? VC5_HDMI_HORZA_HPOS : 0) |
1573 VC4_SET_FIELD(mode->hdisplay * pixel_rep,
1574 VC5_HDMI_HORZA_HAP) |
1575 VC4_SET_FIELD((mode->hsync_start -
1576 mode->hdisplay) * pixel_rep,
1577 VC5_HDMI_HORZA_HFP));
1578
1579 HDMI_WRITE(HDMI_HORZB,
1580 VC4_SET_FIELD((mode->htotal -
1581 mode->hsync_end) * pixel_rep,
1582 VC5_HDMI_HORZB_HBP) |
1583 VC4_SET_FIELD((mode->hsync_end -
1584 mode->hsync_start) * pixel_rep,
1585 VC5_HDMI_HORZB_HSP));
1586
1587 HDMI_WRITE(HDMI_VERTA0, verta);
1588 HDMI_WRITE(HDMI_VERTA1, verta);
1589
1590 HDMI_WRITE(HDMI_VERTB0, vertb_even);
1591 HDMI_WRITE(HDMI_VERTB1, vertb);
1592
1593 switch (vc4_state->output_bpc) {
1594 case 12:
1595 gcp = 6;
1596 break;
1597 case 10:
1598 gcp = 5;
1599 break;
1600 case 8:
1601 default:
1602 gcp = 0;
1603 break;
1604 }
1605
1606 /*
1607 * YCC422 is always 36-bit and not considered deep colour so
1608 * doesn't signal in GCP.
1609 */
1610 if (vc4_state->output_format == VC4_HDMI_OUTPUT_YUV422) {
1611 gcp = 0;
1612 }
1613
1614 reg = HDMI_READ(HDMI_DEEP_COLOR_CONFIG_1);
1615 reg &= ~(VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE_MASK |
1616 VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH_MASK);
1617 reg |= VC4_SET_FIELD(2, VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE) |
1618 VC4_SET_FIELD(gcp, VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH);
1619 HDMI_WRITE(HDMI_DEEP_COLOR_CONFIG_1, reg);
1620
1621 reg = HDMI_READ(HDMI_GCP_WORD_1);
1622 reg &= ~VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1_MASK;
1623 reg |= VC4_SET_FIELD(gcp, VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1);
1624 reg &= ~VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_0_MASK;
1625 reg |= VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_0_CLEAR_AVMUTE;
1626 HDMI_WRITE(HDMI_GCP_WORD_1, reg);
1627
1628 reg = HDMI_READ(HDMI_GCP_CONFIG);
1629 reg |= VC5_HDMI_GCP_CONFIG_GCP_ENABLE;
1630 HDMI_WRITE(HDMI_GCP_CONFIG, reg);
1631
1632 reg = HDMI_READ(HDMI_MISC_CONTROL);
1633 reg &= ~VC5_HDMI_MISC_CONTROL_PIXEL_REP_MASK;
1634 reg |= VC4_SET_FIELD(pixel_rep - 1, VC5_HDMI_MISC_CONTROL_PIXEL_REP);
1635 HDMI_WRITE(HDMI_MISC_CONTROL, reg);
1636
1637 HDMI_WRITE(HDMI_CLOCK_STOP, 0);
1638
1639 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1640
1641 drm_dev_exit(idx);
1642 }
1643
vc4_hdmi_recenter_fifo(struct vc4_hdmi * vc4_hdmi)1644 static void vc4_hdmi_recenter_fifo(struct vc4_hdmi *vc4_hdmi)
1645 {
1646 struct drm_device *drm = vc4_hdmi->connector.dev;
1647 unsigned long flags;
1648 u32 drift;
1649 int ret;
1650 int idx;
1651
1652 if (!drm_dev_enter(drm, &idx))
1653 return;
1654
1655 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1656
1657 drift = HDMI_READ(HDMI_FIFO_CTL);
1658 drift &= VC4_HDMI_FIFO_VALID_WRITE_MASK;
1659
1660 HDMI_WRITE(HDMI_FIFO_CTL,
1661 drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
1662 HDMI_WRITE(HDMI_FIFO_CTL,
1663 drift | VC4_HDMI_FIFO_CTL_RECENTER);
1664
1665 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1666
1667 usleep_range(1000, 1100);
1668
1669 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1670
1671 HDMI_WRITE(HDMI_FIFO_CTL,
1672 drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
1673 HDMI_WRITE(HDMI_FIFO_CTL,
1674 drift | VC4_HDMI_FIFO_CTL_RECENTER);
1675
1676 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1677
1678 ret = wait_for(HDMI_READ(HDMI_FIFO_CTL) &
1679 VC4_HDMI_FIFO_CTL_RECENTER_DONE, 1);
1680 WARN_ONCE(ret, "Timeout waiting for "
1681 "VC4_HDMI_FIFO_CTL_RECENTER_DONE");
1682
1683 drm_dev_exit(idx);
1684 }
1685
vc4_hdmi_encoder_pre_crtc_configure(struct drm_encoder * encoder,struct drm_atomic_state * state)1686 static void vc4_hdmi_encoder_pre_crtc_configure(struct drm_encoder *encoder,
1687 struct drm_atomic_state *state)
1688 {
1689 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1690 struct drm_device *drm = vc4_hdmi->connector.dev;
1691 struct drm_connector *connector = &vc4_hdmi->connector;
1692 struct drm_connector_state *conn_state =
1693 drm_atomic_get_new_connector_state(state, connector);
1694 struct vc4_hdmi_connector_state *vc4_conn_state =
1695 conn_state_to_vc4_hdmi_conn_state(conn_state);
1696 const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
1697 unsigned long tmds_char_rate = vc4_conn_state->tmds_char_rate;
1698 unsigned long bvb_rate, hsm_rate;
1699 unsigned long flags;
1700 int ret;
1701 int idx;
1702
1703 mutex_lock(&vc4_hdmi->mutex);
1704
1705 if (!drm_dev_enter(drm, &idx))
1706 goto out;
1707
1708 ret = pm_runtime_resume_and_get(&vc4_hdmi->pdev->dev);
1709 if (ret < 0) {
1710 DRM_ERROR("Failed to retain power domain: %d\n", ret);
1711 goto err_dev_exit;
1712 }
1713
1714 /*
1715 * As stated in RPi's vc4 firmware "HDMI state machine (HSM) clock must
1716 * be faster than pixel clock, infinitesimally faster, tested in
1717 * simulation. Otherwise, exact value is unimportant for HDMI
1718 * operation." This conflicts with bcm2835's vc4 documentation, which
1719 * states HSM's clock has to be at least 108% of the pixel clock.
1720 *
1721 * Real life tests reveal that vc4's firmware statement holds up, and
1722 * users are able to use pixel clocks closer to HSM's, namely for
1723 * 1920x1200@60Hz. So it was decided to have leave a 1% margin between
1724 * both clocks. Which, for RPi0-3 implies a maximum pixel clock of
1725 * 162MHz.
1726 *
1727 * Additionally, the AXI clock needs to be at least 25% of
1728 * pixel clock, but HSM ends up being the limiting factor.
1729 */
1730 hsm_rate = max_t(unsigned long,
1731 HSM_MIN_CLOCK_FREQ,
1732 (tmds_char_rate / 100) * 101);
1733 ret = clk_set_min_rate(vc4_hdmi->hsm_clock, hsm_rate);
1734 if (ret) {
1735 DRM_ERROR("Failed to set HSM clock rate: %d\n", ret);
1736 goto err_put_runtime_pm;
1737 }
1738
1739 ret = clk_set_rate(vc4_hdmi->pixel_clock, tmds_char_rate);
1740 if (ret) {
1741 DRM_ERROR("Failed to set pixel clock rate: %d\n", ret);
1742 goto err_put_runtime_pm;
1743 }
1744
1745 ret = clk_prepare_enable(vc4_hdmi->pixel_clock);
1746 if (ret) {
1747 DRM_ERROR("Failed to turn on pixel clock: %d\n", ret);
1748 goto err_put_runtime_pm;
1749 }
1750
1751
1752 vc4_hdmi_cec_update_clk_div(vc4_hdmi);
1753
1754 if (tmds_char_rate > 297000000)
1755 bvb_rate = 300000000;
1756 else if (tmds_char_rate > 148500000)
1757 bvb_rate = 150000000;
1758 else
1759 bvb_rate = 75000000;
1760
1761 ret = clk_set_min_rate(vc4_hdmi->pixel_bvb_clock, bvb_rate);
1762 if (ret) {
1763 DRM_ERROR("Failed to set pixel bvb clock rate: %d\n", ret);
1764 goto err_disable_pixel_clock;
1765 }
1766
1767 ret = clk_prepare_enable(vc4_hdmi->pixel_bvb_clock);
1768 if (ret) {
1769 DRM_ERROR("Failed to turn on pixel bvb clock: %d\n", ret);
1770 goto err_disable_pixel_clock;
1771 }
1772
1773 if (vc4_hdmi->variant->phy_init)
1774 vc4_hdmi->variant->phy_init(vc4_hdmi, vc4_conn_state);
1775
1776 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1777
1778 HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
1779 HDMI_READ(HDMI_SCHEDULER_CONTROL) |
1780 VC4_HDMI_SCHEDULER_CONTROL_MANUAL_FORMAT |
1781 VC4_HDMI_SCHEDULER_CONTROL_IGNORE_VSYNC_PREDICTS);
1782
1783 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1784
1785 if (vc4_hdmi->variant->set_timings)
1786 vc4_hdmi->variant->set_timings(vc4_hdmi, conn_state, mode);
1787
1788 drm_dev_exit(idx);
1789
1790 mutex_unlock(&vc4_hdmi->mutex);
1791
1792 return;
1793
1794 err_disable_pixel_clock:
1795 clk_disable_unprepare(vc4_hdmi->pixel_clock);
1796 err_put_runtime_pm:
1797 pm_runtime_put(&vc4_hdmi->pdev->dev);
1798 err_dev_exit:
1799 drm_dev_exit(idx);
1800 out:
1801 mutex_unlock(&vc4_hdmi->mutex);
1802 return;
1803 }
1804
vc4_hdmi_encoder_pre_crtc_enable(struct drm_encoder * encoder,struct drm_atomic_state * state)1805 static void vc4_hdmi_encoder_pre_crtc_enable(struct drm_encoder *encoder,
1806 struct drm_atomic_state *state)
1807 {
1808 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1809 struct drm_device *drm = vc4_hdmi->connector.dev;
1810 struct drm_connector *connector = &vc4_hdmi->connector;
1811 const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
1812 struct drm_connector_state *conn_state =
1813 drm_atomic_get_new_connector_state(state, connector);
1814 unsigned long flags;
1815 int idx;
1816
1817 mutex_lock(&vc4_hdmi->mutex);
1818
1819 if (!drm_dev_enter(drm, &idx))
1820 goto out;
1821
1822 if (vc4_hdmi->variant->csc_setup)
1823 vc4_hdmi->variant->csc_setup(vc4_hdmi, conn_state, mode);
1824
1825 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1826 HDMI_WRITE(HDMI_FIFO_CTL, VC4_HDMI_FIFO_CTL_MASTER_SLAVE_N);
1827 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1828
1829 drm_dev_exit(idx);
1830
1831 out:
1832 mutex_unlock(&vc4_hdmi->mutex);
1833 }
1834
vc4_hdmi_encoder_post_crtc_enable(struct drm_encoder * encoder,struct drm_atomic_state * state)1835 static void vc4_hdmi_encoder_post_crtc_enable(struct drm_encoder *encoder,
1836 struct drm_atomic_state *state)
1837 {
1838 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1839 struct drm_device *drm = vc4_hdmi->connector.dev;
1840 const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
1841 struct drm_display_info *display = &vc4_hdmi->connector.display_info;
1842 bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
1843 bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
1844 unsigned long flags;
1845 int ret;
1846 int idx;
1847
1848 mutex_lock(&vc4_hdmi->mutex);
1849
1850 if (!drm_dev_enter(drm, &idx))
1851 goto out;
1852
1853 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1854
1855 HDMI_WRITE(HDMI_VID_CTL,
1856 VC4_HD_VID_CTL_ENABLE |
1857 VC4_HD_VID_CTL_CLRRGB |
1858 VC4_HD_VID_CTL_UNDERFLOW_ENABLE |
1859 VC4_HD_VID_CTL_FRAME_COUNTER_RESET |
1860 (vsync_pos ? 0 : VC4_HD_VID_CTL_VSYNC_LOW) |
1861 (hsync_pos ? 0 : VC4_HD_VID_CTL_HSYNC_LOW));
1862
1863 HDMI_WRITE(HDMI_VID_CTL,
1864 HDMI_READ(HDMI_VID_CTL) & ~VC4_HD_VID_CTL_BLANKPIX);
1865
1866 if (display->is_hdmi) {
1867 HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
1868 HDMI_READ(HDMI_SCHEDULER_CONTROL) |
1869 VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);
1870
1871 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1872
1873 ret = wait_for(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
1874 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE, 1000);
1875 WARN_ONCE(ret, "Timeout waiting for "
1876 "VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
1877 } else {
1878 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
1879 HDMI_READ(HDMI_RAM_PACKET_CONFIG) &
1880 ~(VC4_HDMI_RAM_PACKET_ENABLE));
1881 HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
1882 HDMI_READ(HDMI_SCHEDULER_CONTROL) &
1883 ~VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);
1884
1885 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1886
1887 ret = wait_for(!(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
1888 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE), 1000);
1889 WARN_ONCE(ret, "Timeout waiting for "
1890 "!VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
1891 }
1892
1893 if (display->is_hdmi) {
1894 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
1895
1896 WARN_ON(!(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
1897 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE));
1898
1899 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
1900 VC4_HDMI_RAM_PACKET_ENABLE);
1901
1902 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
1903 vc4_hdmi->packet_ram_enabled = true;
1904
1905 vc4_hdmi_set_infoframes(encoder);
1906 }
1907
1908 vc4_hdmi_recenter_fifo(vc4_hdmi);
1909 vc4_hdmi_enable_scrambling(encoder);
1910
1911 drm_dev_exit(idx);
1912
1913 out:
1914 mutex_unlock(&vc4_hdmi->mutex);
1915 }
1916
vc4_hdmi_encoder_atomic_mode_set(struct drm_encoder * encoder,struct drm_crtc_state * crtc_state,struct drm_connector_state * conn_state)1917 static void vc4_hdmi_encoder_atomic_mode_set(struct drm_encoder *encoder,
1918 struct drm_crtc_state *crtc_state,
1919 struct drm_connector_state *conn_state)
1920 {
1921 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
1922 struct vc4_hdmi_connector_state *vc4_state =
1923 conn_state_to_vc4_hdmi_conn_state(conn_state);
1924
1925 mutex_lock(&vc4_hdmi->mutex);
1926 drm_mode_copy(&vc4_hdmi->saved_adjusted_mode,
1927 &crtc_state->adjusted_mode);
1928 vc4_hdmi->output_bpc = vc4_state->output_bpc;
1929 vc4_hdmi->output_format = vc4_state->output_format;
1930 mutex_unlock(&vc4_hdmi->mutex);
1931 }
1932
1933 static bool
vc4_hdmi_sink_supports_format_bpc(const struct vc4_hdmi * vc4_hdmi,const struct drm_display_info * info,const struct drm_display_mode * mode,unsigned int format,unsigned int bpc)1934 vc4_hdmi_sink_supports_format_bpc(const struct vc4_hdmi *vc4_hdmi,
1935 const struct drm_display_info *info,
1936 const struct drm_display_mode *mode,
1937 unsigned int format, unsigned int bpc)
1938 {
1939 struct drm_device *dev = vc4_hdmi->connector.dev;
1940 u8 vic = drm_match_cea_mode(mode);
1941
1942 if (vic == 1 && bpc != 8) {
1943 drm_dbg(dev, "VIC1 requires a bpc of 8, got %u\n", bpc);
1944 return false;
1945 }
1946
1947 if (!info->is_hdmi &&
1948 (format != VC4_HDMI_OUTPUT_RGB || bpc != 8)) {
1949 drm_dbg(dev, "DVI Monitors require an RGB output at 8 bpc\n");
1950 return false;
1951 }
1952
1953 switch (format) {
1954 case VC4_HDMI_OUTPUT_RGB:
1955 drm_dbg(dev, "RGB Format, checking the constraints.\n");
1956
1957 if (!(info->color_formats & DRM_COLOR_FORMAT_RGB444))
1958 return false;
1959
1960 if (bpc == 10 && !(info->edid_hdmi_rgb444_dc_modes & DRM_EDID_HDMI_DC_30)) {
1961 drm_dbg(dev, "10 BPC but sink doesn't support Deep Color 30.\n");
1962 return false;
1963 }
1964
1965 if (bpc == 12 && !(info->edid_hdmi_rgb444_dc_modes & DRM_EDID_HDMI_DC_36)) {
1966 drm_dbg(dev, "12 BPC but sink doesn't support Deep Color 36.\n");
1967 return false;
1968 }
1969
1970 drm_dbg(dev, "RGB format supported in that configuration.\n");
1971
1972 return true;
1973
1974 case VC4_HDMI_OUTPUT_YUV422:
1975 drm_dbg(dev, "YUV422 format, checking the constraints.\n");
1976
1977 if (!(info->color_formats & DRM_COLOR_FORMAT_YCBCR422)) {
1978 drm_dbg(dev, "Sink doesn't support YUV422.\n");
1979 return false;
1980 }
1981
1982 if (bpc != 12) {
1983 drm_dbg(dev, "YUV422 only supports 12 bpc.\n");
1984 return false;
1985 }
1986
1987 drm_dbg(dev, "YUV422 format supported in that configuration.\n");
1988
1989 return true;
1990
1991 case VC4_HDMI_OUTPUT_YUV444:
1992 drm_dbg(dev, "YUV444 format, checking the constraints.\n");
1993
1994 if (!(info->color_formats & DRM_COLOR_FORMAT_YCBCR444)) {
1995 drm_dbg(dev, "Sink doesn't support YUV444.\n");
1996 return false;
1997 }
1998
1999 if (bpc == 10 && !(info->edid_hdmi_ycbcr444_dc_modes & DRM_EDID_HDMI_DC_30)) {
2000 drm_dbg(dev, "10 BPC but sink doesn't support Deep Color 30.\n");
2001 return false;
2002 }
2003
2004 if (bpc == 12 && !(info->edid_hdmi_ycbcr444_dc_modes & DRM_EDID_HDMI_DC_36)) {
2005 drm_dbg(dev, "12 BPC but sink doesn't support Deep Color 36.\n");
2006 return false;
2007 }
2008
2009 drm_dbg(dev, "YUV444 format supported in that configuration.\n");
2010
2011 return true;
2012 }
2013
2014 return false;
2015 }
2016
2017 static enum drm_mode_status
vc4_hdmi_encoder_clock_valid(const struct vc4_hdmi * vc4_hdmi,const struct drm_display_mode * mode,unsigned long long clock)2018 vc4_hdmi_encoder_clock_valid(const struct vc4_hdmi *vc4_hdmi,
2019 const struct drm_display_mode *mode,
2020 unsigned long long clock)
2021 {
2022 const struct drm_connector *connector = &vc4_hdmi->connector;
2023 const struct drm_display_info *info = &connector->display_info;
2024 struct vc4_dev *vc4 = to_vc4_dev(connector->dev);
2025
2026 if (clock > vc4_hdmi->variant->max_pixel_clock)
2027 return MODE_CLOCK_HIGH;
2028
2029 if (!vc4->hvs->vc5_hdmi_enable_hdmi_20 && clock > HDMI_14_MAX_TMDS_CLK)
2030 return MODE_CLOCK_HIGH;
2031
2032 /* 4096x2160@60 is not reliable without overclocking core */
2033 if (!vc4->hvs->vc5_hdmi_enable_4096by2160 &&
2034 mode->hdisplay > 3840 && mode->vdisplay >= 2160 &&
2035 drm_mode_vrefresh(mode) >= 50)
2036 return MODE_CLOCK_HIGH;
2037
2038 if (info->max_tmds_clock && clock > (info->max_tmds_clock * 1000))
2039 return MODE_CLOCK_HIGH;
2040
2041 return MODE_OK;
2042 }
2043
2044 static unsigned long long
vc4_hdmi_encoder_compute_mode_clock(const struct drm_display_mode * mode,unsigned int bpc,enum vc4_hdmi_output_format fmt)2045 vc4_hdmi_encoder_compute_mode_clock(const struct drm_display_mode *mode,
2046 unsigned int bpc,
2047 enum vc4_hdmi_output_format fmt)
2048 {
2049 unsigned long long clock = mode->clock * 1000ULL;
2050
2051 if (mode->flags & DRM_MODE_FLAG_DBLCLK)
2052 clock = clock * 2;
2053
2054 if (fmt == VC4_HDMI_OUTPUT_YUV422)
2055 bpc = 8;
2056
2057 clock = clock * bpc;
2058 do_div(clock, 8);
2059
2060 return clock;
2061 }
2062
2063 static int
vc4_hdmi_encoder_compute_clock(const struct vc4_hdmi * vc4_hdmi,struct vc4_hdmi_connector_state * vc4_state,const struct drm_display_mode * mode,unsigned int bpc,unsigned int fmt)2064 vc4_hdmi_encoder_compute_clock(const struct vc4_hdmi *vc4_hdmi,
2065 struct vc4_hdmi_connector_state *vc4_state,
2066 const struct drm_display_mode *mode,
2067 unsigned int bpc, unsigned int fmt)
2068 {
2069 unsigned long long clock;
2070
2071 clock = vc4_hdmi_encoder_compute_mode_clock(mode, bpc, fmt);
2072 if (vc4_hdmi_encoder_clock_valid(vc4_hdmi, mode, clock) != MODE_OK)
2073 return -EINVAL;
2074
2075 vc4_state->tmds_char_rate = clock;
2076
2077 return 0;
2078 }
2079
2080 static int
vc4_hdmi_encoder_compute_format(const struct vc4_hdmi * vc4_hdmi,struct vc4_hdmi_connector_state * vc4_state,const struct drm_display_mode * mode,unsigned int bpc)2081 vc4_hdmi_encoder_compute_format(const struct vc4_hdmi *vc4_hdmi,
2082 struct vc4_hdmi_connector_state *vc4_state,
2083 const struct drm_display_mode *mode,
2084 unsigned int bpc)
2085 {
2086 struct drm_device *dev = vc4_hdmi->connector.dev;
2087 const struct drm_connector *connector = &vc4_hdmi->connector;
2088 const struct drm_display_info *info = &connector->display_info;
2089 unsigned int format;
2090
2091 drm_dbg(dev, "Trying with an RGB output\n");
2092
2093 format = VC4_HDMI_OUTPUT_RGB;
2094 if (vc4_hdmi_sink_supports_format_bpc(vc4_hdmi, info, mode, format, bpc)) {
2095 int ret;
2096
2097 ret = vc4_hdmi_encoder_compute_clock(vc4_hdmi, vc4_state,
2098 mode, bpc, format);
2099 if (!ret) {
2100 vc4_state->output_format = format;
2101 return 0;
2102 }
2103 }
2104
2105 drm_dbg(dev, "Failed, Trying with an YUV422 output\n");
2106
2107 format = VC4_HDMI_OUTPUT_YUV422;
2108 if (vc4_hdmi_sink_supports_format_bpc(vc4_hdmi, info, mode, format, bpc)) {
2109 int ret;
2110
2111 ret = vc4_hdmi_encoder_compute_clock(vc4_hdmi, vc4_state,
2112 mode, bpc, format);
2113 if (!ret) {
2114 vc4_state->output_format = format;
2115 return 0;
2116 }
2117 }
2118
2119 drm_dbg(dev, "Failed. No Format Supported for that bpc count.\n");
2120
2121 return -EINVAL;
2122 }
2123
2124 static int
vc4_hdmi_encoder_compute_config(const struct vc4_hdmi * vc4_hdmi,struct vc4_hdmi_connector_state * vc4_state,const struct drm_display_mode * mode)2125 vc4_hdmi_encoder_compute_config(const struct vc4_hdmi *vc4_hdmi,
2126 struct vc4_hdmi_connector_state *vc4_state,
2127 const struct drm_display_mode *mode)
2128 {
2129 struct drm_device *dev = vc4_hdmi->connector.dev;
2130 struct drm_connector_state *conn_state = &vc4_state->base;
2131 unsigned int max_bpc = clamp_t(unsigned int, conn_state->max_bpc, 8, 12);
2132 unsigned int bpc;
2133 int ret;
2134
2135 for (bpc = max_bpc; bpc >= 8; bpc -= 2) {
2136 drm_dbg(dev, "Trying with a %d bpc output\n", bpc);
2137
2138 ret = vc4_hdmi_encoder_compute_format(vc4_hdmi, vc4_state,
2139 mode, bpc);
2140 if (ret)
2141 continue;
2142
2143 vc4_state->output_bpc = bpc;
2144
2145 drm_dbg(dev,
2146 "Mode %ux%u @ %uHz: Found configuration: bpc: %u, fmt: %s, clock: %llu\n",
2147 mode->hdisplay, mode->vdisplay, drm_mode_vrefresh(mode),
2148 vc4_state->output_bpc,
2149 vc4_hdmi_output_fmt_str(vc4_state->output_format),
2150 vc4_state->tmds_char_rate);
2151
2152 break;
2153 }
2154
2155 return ret;
2156 }
2157
2158 #define WIFI_2_4GHz_CH1_MIN_FREQ 2400000000ULL
2159 #define WIFI_2_4GHz_CH1_MAX_FREQ 2422000000ULL
2160
vc4_hdmi_encoder_atomic_check(struct drm_encoder * encoder,struct drm_crtc_state * crtc_state,struct drm_connector_state * conn_state)2161 static int vc4_hdmi_encoder_atomic_check(struct drm_encoder *encoder,
2162 struct drm_crtc_state *crtc_state,
2163 struct drm_connector_state *conn_state)
2164 {
2165 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
2166 struct drm_connector *connector = &vc4_hdmi->connector;
2167 struct drm_connector_state *old_conn_state =
2168 drm_atomic_get_old_connector_state(conn_state->state, connector);
2169 struct vc4_hdmi_connector_state *old_vc4_state =
2170 conn_state_to_vc4_hdmi_conn_state(old_conn_state);
2171 struct vc4_hdmi_connector_state *vc4_state = conn_state_to_vc4_hdmi_conn_state(conn_state);
2172 struct drm_display_mode *mode = &crtc_state->adjusted_mode;
2173 unsigned long long tmds_char_rate = mode->clock * 1000;
2174 unsigned long long tmds_bit_rate;
2175 int ret;
2176
2177 if (vc4_hdmi->variant->unsupported_odd_h_timings) {
2178 if (mode->flags & DRM_MODE_FLAG_DBLCLK) {
2179 /* Only try to fixup DBLCLK modes to get 480i and 576i
2180 * working.
2181 * A generic solution for all modes with odd horizontal
2182 * timing values seems impossible based on trying to
2183 * solve it for 1366x768 monitors.
2184 */
2185 if ((mode->hsync_start - mode->hdisplay) & 1)
2186 mode->hsync_start--;
2187 if ((mode->hsync_end - mode->hsync_start) & 1)
2188 mode->hsync_end--;
2189 }
2190
2191 /* Now check whether we still have odd values remaining */
2192 if ((mode->hdisplay % 2) || (mode->hsync_start % 2) ||
2193 (mode->hsync_end % 2) || (mode->htotal % 2))
2194 return -EINVAL;
2195 }
2196
2197 /*
2198 * The 1440p@60 pixel rate is in the same range than the first
2199 * WiFi channel (between 2.4GHz and 2.422GHz with 22MHz
2200 * bandwidth). Slightly lower the frequency to bring it out of
2201 * the WiFi range.
2202 */
2203 tmds_bit_rate = tmds_char_rate * 10;
2204 if (vc4_hdmi->disable_wifi_frequencies &&
2205 (tmds_bit_rate >= WIFI_2_4GHz_CH1_MIN_FREQ &&
2206 tmds_bit_rate <= WIFI_2_4GHz_CH1_MAX_FREQ)) {
2207 mode->clock = 238560;
2208 tmds_char_rate = mode->clock * 1000;
2209 }
2210
2211 ret = vc4_hdmi_encoder_compute_config(vc4_hdmi, vc4_state, mode);
2212 if (ret)
2213 return ret;
2214
2215 /* vc4_hdmi_encoder_compute_config may have changed output_bpc and/or output_format */
2216 if (vc4_state->output_bpc != old_vc4_state->output_bpc ||
2217 vc4_state->output_format != old_vc4_state->output_format)
2218 crtc_state->mode_changed = true;
2219
2220 return 0;
2221 }
2222
2223 static enum drm_mode_status
vc4_hdmi_encoder_mode_valid(struct drm_encoder * encoder,const struct drm_display_mode * mode)2224 vc4_hdmi_encoder_mode_valid(struct drm_encoder *encoder,
2225 const struct drm_display_mode *mode)
2226 {
2227 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
2228
2229 if (vc4_hdmi->variant->unsupported_odd_h_timings &&
2230 !(mode->flags & DRM_MODE_FLAG_DBLCLK) &&
2231 ((mode->hdisplay % 2) || (mode->hsync_start % 2) ||
2232 (mode->hsync_end % 2) || (mode->htotal % 2)))
2233 return MODE_H_ILLEGAL;
2234
2235 return vc4_hdmi_encoder_clock_valid(vc4_hdmi, mode, mode->clock * 1000);
2236 }
2237
2238 static const struct drm_encoder_helper_funcs vc4_hdmi_encoder_helper_funcs = {
2239 .atomic_check = vc4_hdmi_encoder_atomic_check,
2240 .atomic_mode_set = vc4_hdmi_encoder_atomic_mode_set,
2241 .mode_valid = vc4_hdmi_encoder_mode_valid,
2242 };
2243
vc4_hdmi_late_register(struct drm_encoder * encoder)2244 static int vc4_hdmi_late_register(struct drm_encoder *encoder)
2245 {
2246 struct drm_device *drm = encoder->dev;
2247 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
2248 const struct vc4_hdmi_variant *variant = vc4_hdmi->variant;
2249
2250 drm_debugfs_add_file(drm, variant->debugfs_name,
2251 vc4_hdmi_debugfs_regs, vc4_hdmi);
2252
2253 return 0;
2254 }
2255
2256 static const struct drm_encoder_funcs vc4_hdmi_encoder_funcs = {
2257 .late_register = vc4_hdmi_late_register,
2258 };
2259
vc4_hdmi_channel_map(struct vc4_hdmi * vc4_hdmi,u32 channel_mask)2260 static u32 vc4_hdmi_channel_map(struct vc4_hdmi *vc4_hdmi, u32 channel_mask)
2261 {
2262 int i;
2263 u32 channel_map = 0;
2264
2265 for (i = 0; i < 8; i++) {
2266 if (channel_mask & BIT(i))
2267 channel_map |= i << (3 * i);
2268 }
2269 return channel_map;
2270 }
2271
vc5_hdmi_channel_map(struct vc4_hdmi * vc4_hdmi,u32 channel_mask)2272 static u32 vc5_hdmi_channel_map(struct vc4_hdmi *vc4_hdmi, u32 channel_mask)
2273 {
2274 int i;
2275 u32 channel_map = 0;
2276
2277 for (i = 0; i < 8; i++) {
2278 if (channel_mask & BIT(i))
2279 channel_map |= i << (4 * i);
2280 }
2281 return channel_map;
2282 }
2283
vc5_hdmi_hp_detect(struct vc4_hdmi * vc4_hdmi)2284 static bool vc5_hdmi_hp_detect(struct vc4_hdmi *vc4_hdmi)
2285 {
2286 struct drm_device *drm = vc4_hdmi->connector.dev;
2287 unsigned long flags;
2288 u32 hotplug;
2289 int idx;
2290
2291 if (!drm_dev_enter(drm, &idx))
2292 return false;
2293
2294 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2295 hotplug = HDMI_READ(HDMI_HOTPLUG);
2296 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2297
2298 drm_dev_exit(idx);
2299
2300 return !!(hotplug & VC4_HDMI_HOTPLUG_CONNECTED);
2301 }
2302
2303 /* HDMI audio codec callbacks */
vc4_hdmi_audio_set_mai_clock(struct vc4_hdmi * vc4_hdmi,unsigned int samplerate)2304 static void vc4_hdmi_audio_set_mai_clock(struct vc4_hdmi *vc4_hdmi,
2305 unsigned int samplerate)
2306 {
2307 struct drm_device *drm = vc4_hdmi->connector.dev;
2308 u32 hsm_clock;
2309 unsigned long flags;
2310 unsigned long n, m;
2311 int idx;
2312
2313 if (!drm_dev_enter(drm, &idx))
2314 return;
2315
2316 hsm_clock = clk_get_rate(vc4_hdmi->audio_clock);
2317 rational_best_approximation(hsm_clock, samplerate,
2318 VC4_HD_MAI_SMP_N_MASK >>
2319 VC4_HD_MAI_SMP_N_SHIFT,
2320 (VC4_HD_MAI_SMP_M_MASK >>
2321 VC4_HD_MAI_SMP_M_SHIFT) + 1,
2322 &n, &m);
2323
2324 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2325 HDMI_WRITE(HDMI_MAI_SMP,
2326 VC4_SET_FIELD(n, VC4_HD_MAI_SMP_N) |
2327 VC4_SET_FIELD(m - 1, VC4_HD_MAI_SMP_M));
2328 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2329
2330 drm_dev_exit(idx);
2331 }
2332
vc4_hdmi_set_n_cts(struct vc4_hdmi * vc4_hdmi,unsigned int samplerate)2333 static void vc4_hdmi_set_n_cts(struct vc4_hdmi *vc4_hdmi, unsigned int samplerate)
2334 {
2335 const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
2336 u32 n, cts;
2337 u64 tmp;
2338
2339 lockdep_assert_held(&vc4_hdmi->mutex);
2340 lockdep_assert_held(&vc4_hdmi->hw_lock);
2341
2342 n = 128 * samplerate / 1000;
2343 tmp = (u64)(mode->clock * 1000) * n;
2344 do_div(tmp, 128 * samplerate);
2345 cts = tmp;
2346
2347 HDMI_WRITE(HDMI_CRP_CFG,
2348 VC4_HDMI_CRP_CFG_EXTERNAL_CTS_EN |
2349 VC4_SET_FIELD(n, VC4_HDMI_CRP_CFG_N));
2350
2351 /*
2352 * We could get slightly more accurate clocks in some cases by
2353 * providing a CTS_1 value. The two CTS values are alternated
2354 * between based on the period fields
2355 */
2356 HDMI_WRITE(HDMI_CTS_0, cts);
2357 HDMI_WRITE(HDMI_CTS_1, cts);
2358 }
2359
dai_to_hdmi(struct snd_soc_dai * dai)2360 static inline struct vc4_hdmi *dai_to_hdmi(struct snd_soc_dai *dai)
2361 {
2362 struct snd_soc_card *card = snd_soc_dai_get_drvdata(dai);
2363
2364 return snd_soc_card_get_drvdata(card);
2365 }
2366
vc4_hdmi_audio_can_stream(struct vc4_hdmi * vc4_hdmi)2367 static bool vc4_hdmi_audio_can_stream(struct vc4_hdmi *vc4_hdmi)
2368 {
2369 struct drm_display_info *display = &vc4_hdmi->connector.display_info;
2370
2371 lockdep_assert_held(&vc4_hdmi->mutex);
2372
2373 /*
2374 * If the encoder is currently in DVI mode, treat the codec DAI
2375 * as missing.
2376 */
2377 if (!display->is_hdmi)
2378 return false;
2379
2380 return true;
2381 }
2382
vc4_hdmi_audio_startup(struct device * dev,void * data)2383 static int vc4_hdmi_audio_startup(struct device *dev, void *data)
2384 {
2385 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
2386 struct drm_device *drm = vc4_hdmi->connector.dev;
2387 unsigned long flags;
2388 int ret = 0;
2389 int idx;
2390
2391 mutex_lock(&vc4_hdmi->mutex);
2392
2393 if (!drm_dev_enter(drm, &idx)) {
2394 ret = -ENODEV;
2395 goto out;
2396 }
2397
2398 if (!vc4_hdmi_audio_can_stream(vc4_hdmi)) {
2399 ret = -ENOTSUPP;
2400 goto out_dev_exit;
2401 }
2402
2403 vc4_hdmi->audio.streaming = true;
2404
2405 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2406 HDMI_WRITE(HDMI_MAI_CTL,
2407 VC4_HD_MAI_CTL_RESET |
2408 VC4_HD_MAI_CTL_FLUSH |
2409 VC4_HD_MAI_CTL_DLATE |
2410 VC4_HD_MAI_CTL_ERRORE |
2411 VC4_HD_MAI_CTL_ERRORF);
2412 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2413
2414 if (vc4_hdmi->variant->phy_rng_enable)
2415 vc4_hdmi->variant->phy_rng_enable(vc4_hdmi);
2416
2417 out_dev_exit:
2418 drm_dev_exit(idx);
2419 out:
2420 mutex_unlock(&vc4_hdmi->mutex);
2421
2422 return ret;
2423 }
2424
vc4_hdmi_audio_reset(struct vc4_hdmi * vc4_hdmi)2425 static void vc4_hdmi_audio_reset(struct vc4_hdmi *vc4_hdmi)
2426 {
2427 struct drm_encoder *encoder = &vc4_hdmi->encoder.base;
2428 struct device *dev = &vc4_hdmi->pdev->dev;
2429 unsigned long flags;
2430 int ret;
2431
2432 lockdep_assert_held(&vc4_hdmi->mutex);
2433
2434 vc4_hdmi->audio.streaming = false;
2435 ret = vc4_hdmi_stop_packet(encoder, HDMI_INFOFRAME_TYPE_AUDIO, false);
2436 if (ret)
2437 dev_err(dev, "Failed to stop audio infoframe: %d\n", ret);
2438
2439 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2440
2441 HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_RESET);
2442 HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_ERRORF);
2443 HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_FLUSH);
2444
2445 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2446 }
2447
vc4_hdmi_audio_shutdown(struct device * dev,void * data)2448 static void vc4_hdmi_audio_shutdown(struct device *dev, void *data)
2449 {
2450 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
2451 struct drm_device *drm = vc4_hdmi->connector.dev;
2452 unsigned long flags;
2453 int idx;
2454
2455 mutex_lock(&vc4_hdmi->mutex);
2456
2457 if (!drm_dev_enter(drm, &idx))
2458 goto out;
2459
2460 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2461
2462 HDMI_WRITE(HDMI_MAI_CTL,
2463 VC4_HD_MAI_CTL_DLATE |
2464 VC4_HD_MAI_CTL_ERRORE |
2465 VC4_HD_MAI_CTL_ERRORF);
2466
2467 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2468
2469 if (vc4_hdmi->variant->phy_rng_disable)
2470 vc4_hdmi->variant->phy_rng_disable(vc4_hdmi);
2471
2472 vc4_hdmi->audio.streaming = false;
2473 vc4_hdmi_audio_reset(vc4_hdmi);
2474
2475 drm_dev_exit(idx);
2476
2477 out:
2478 mutex_unlock(&vc4_hdmi->mutex);
2479 }
2480
sample_rate_to_mai_fmt(int samplerate)2481 static int sample_rate_to_mai_fmt(int samplerate)
2482 {
2483 switch (samplerate) {
2484 case 8000:
2485 return VC4_HDMI_MAI_SAMPLE_RATE_8000;
2486 case 11025:
2487 return VC4_HDMI_MAI_SAMPLE_RATE_11025;
2488 case 12000:
2489 return VC4_HDMI_MAI_SAMPLE_RATE_12000;
2490 case 16000:
2491 return VC4_HDMI_MAI_SAMPLE_RATE_16000;
2492 case 22050:
2493 return VC4_HDMI_MAI_SAMPLE_RATE_22050;
2494 case 24000:
2495 return VC4_HDMI_MAI_SAMPLE_RATE_24000;
2496 case 32000:
2497 return VC4_HDMI_MAI_SAMPLE_RATE_32000;
2498 case 44100:
2499 return VC4_HDMI_MAI_SAMPLE_RATE_44100;
2500 case 48000:
2501 return VC4_HDMI_MAI_SAMPLE_RATE_48000;
2502 case 64000:
2503 return VC4_HDMI_MAI_SAMPLE_RATE_64000;
2504 case 88200:
2505 return VC4_HDMI_MAI_SAMPLE_RATE_88200;
2506 case 96000:
2507 return VC4_HDMI_MAI_SAMPLE_RATE_96000;
2508 case 128000:
2509 return VC4_HDMI_MAI_SAMPLE_RATE_128000;
2510 case 176400:
2511 return VC4_HDMI_MAI_SAMPLE_RATE_176400;
2512 case 192000:
2513 return VC4_HDMI_MAI_SAMPLE_RATE_192000;
2514 default:
2515 return VC4_HDMI_MAI_SAMPLE_RATE_NOT_INDICATED;
2516 }
2517 }
2518
2519 /* HDMI audio codec callbacks */
vc4_hdmi_audio_prepare(struct device * dev,void * data,struct hdmi_codec_daifmt * daifmt,struct hdmi_codec_params * params)2520 static int vc4_hdmi_audio_prepare(struct device *dev, void *data,
2521 struct hdmi_codec_daifmt *daifmt,
2522 struct hdmi_codec_params *params)
2523 {
2524 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
2525 struct drm_device *drm = vc4_hdmi->connector.dev;
2526 struct drm_encoder *encoder = &vc4_hdmi->encoder.base;
2527 unsigned int sample_rate = params->sample_rate;
2528 unsigned int channels = params->channels;
2529 unsigned long flags;
2530 u32 audio_packet_config, channel_mask;
2531 u32 channel_map;
2532 u32 mai_audio_format;
2533 u32 mai_sample_rate;
2534 int ret = 0;
2535 int idx;
2536
2537 dev_dbg(dev, "%s: %u Hz, %d bit, %d channels\n", __func__,
2538 sample_rate, params->sample_width, channels);
2539
2540 mutex_lock(&vc4_hdmi->mutex);
2541
2542 if (!drm_dev_enter(drm, &idx)) {
2543 ret = -ENODEV;
2544 goto out;
2545 }
2546
2547 if (!vc4_hdmi_audio_can_stream(vc4_hdmi)) {
2548 ret = -EINVAL;
2549 goto out_dev_exit;
2550 }
2551
2552 vc4_hdmi_audio_set_mai_clock(vc4_hdmi, sample_rate);
2553
2554 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
2555 HDMI_WRITE(HDMI_MAI_CTL,
2556 VC4_SET_FIELD(channels, VC4_HD_MAI_CTL_CHNUM) |
2557 VC4_HD_MAI_CTL_WHOLSMP |
2558 VC4_HD_MAI_CTL_CHALIGN |
2559 VC4_HD_MAI_CTL_ENABLE);
2560
2561 mai_sample_rate = sample_rate_to_mai_fmt(sample_rate);
2562 if (params->iec.status[0] & IEC958_AES0_NONAUDIO &&
2563 params->channels == 8)
2564 mai_audio_format = VC4_HDMI_MAI_FORMAT_HBR;
2565 else
2566 mai_audio_format = VC4_HDMI_MAI_FORMAT_PCM;
2567 HDMI_WRITE(HDMI_MAI_FMT,
2568 VC4_SET_FIELD(mai_sample_rate,
2569 VC4_HDMI_MAI_FORMAT_SAMPLE_RATE) |
2570 VC4_SET_FIELD(mai_audio_format,
2571 VC4_HDMI_MAI_FORMAT_AUDIO_FORMAT));
2572
2573 /* The B frame identifier should match the value used by alsa-lib (8) */
2574 audio_packet_config =
2575 VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_SAMPLE_FLAT |
2576 VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_INACTIVE_CHANNELS |
2577 VC4_SET_FIELD(0x8, VC4_HDMI_AUDIO_PACKET_B_FRAME_IDENTIFIER);
2578
2579 channel_mask = GENMASK(channels - 1, 0);
2580 audio_packet_config |= VC4_SET_FIELD(channel_mask,
2581 VC4_HDMI_AUDIO_PACKET_CEA_MASK);
2582
2583 /* Set the MAI threshold */
2584 HDMI_WRITE(HDMI_MAI_THR,
2585 VC4_SET_FIELD(0x08, VC4_HD_MAI_THR_PANICHIGH) |
2586 VC4_SET_FIELD(0x08, VC4_HD_MAI_THR_PANICLOW) |
2587 VC4_SET_FIELD(0x06, VC4_HD_MAI_THR_DREQHIGH) |
2588 VC4_SET_FIELD(0x08, VC4_HD_MAI_THR_DREQLOW));
2589
2590 HDMI_WRITE(HDMI_MAI_CONFIG,
2591 VC4_HDMI_MAI_CONFIG_BIT_REVERSE |
2592 VC4_HDMI_MAI_CONFIG_FORMAT_REVERSE |
2593 VC4_SET_FIELD(channel_mask, VC4_HDMI_MAI_CHANNEL_MASK));
2594
2595 channel_map = vc4_hdmi->variant->channel_map(vc4_hdmi, channel_mask);
2596 HDMI_WRITE(HDMI_MAI_CHANNEL_MAP, channel_map);
2597 HDMI_WRITE(HDMI_AUDIO_PACKET_CONFIG, audio_packet_config);
2598
2599 vc4_hdmi_set_n_cts(vc4_hdmi, sample_rate);
2600
2601 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
2602
2603 memcpy(&vc4_hdmi->audio.infoframe, ¶ms->cea, sizeof(params->cea));
2604 vc4_hdmi_set_audio_infoframe(encoder);
2605
2606 out_dev_exit:
2607 drm_dev_exit(idx);
2608 out:
2609 mutex_unlock(&vc4_hdmi->mutex);
2610
2611 return ret;
2612 }
2613
2614 static const struct snd_soc_component_driver vc4_hdmi_audio_cpu_dai_comp = {
2615 .name = "vc4-hdmi-cpu-dai-component",
2616 .legacy_dai_naming = 1,
2617 };
2618
vc4_hdmi_audio_cpu_dai_probe(struct snd_soc_dai * dai)2619 static int vc4_hdmi_audio_cpu_dai_probe(struct snd_soc_dai *dai)
2620 {
2621 struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai);
2622
2623 snd_soc_dai_init_dma_data(dai, &vc4_hdmi->audio.dma_data, NULL);
2624
2625 return 0;
2626 }
2627
2628 static const struct snd_soc_dai_ops vc4_snd_dai_ops = {
2629 .probe = vc4_hdmi_audio_cpu_dai_probe,
2630 };
2631
2632 static struct snd_soc_dai_driver vc4_hdmi_audio_cpu_dai_drv = {
2633 .name = "vc4-hdmi-cpu-dai",
2634 .ops = &vc4_snd_dai_ops,
2635 .playback = {
2636 .stream_name = "Playback",
2637 .channels_min = 1,
2638 .channels_max = 8,
2639 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
2640 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
2641 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
2642 SNDRV_PCM_RATE_192000,
2643 .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
2644 },
2645 };
2646
2647 static const struct snd_dmaengine_pcm_config pcm_conf = {
2648 .chan_names[SNDRV_PCM_STREAM_PLAYBACK] = "audio-rx",
2649 .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
2650 };
2651
vc4_hdmi_audio_get_eld(struct device * dev,void * data,uint8_t * buf,size_t len)2652 static int vc4_hdmi_audio_get_eld(struct device *dev, void *data,
2653 uint8_t *buf, size_t len)
2654 {
2655 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
2656 struct drm_connector *connector = &vc4_hdmi->connector;
2657
2658 mutex_lock(&vc4_hdmi->mutex);
2659 memcpy(buf, connector->eld, min(sizeof(connector->eld), len));
2660 mutex_unlock(&vc4_hdmi->mutex);
2661
2662 return 0;
2663 }
2664
2665 static const struct hdmi_codec_ops vc4_hdmi_codec_ops = {
2666 .get_eld = vc4_hdmi_audio_get_eld,
2667 .prepare = vc4_hdmi_audio_prepare,
2668 .audio_shutdown = vc4_hdmi_audio_shutdown,
2669 .audio_startup = vc4_hdmi_audio_startup,
2670 };
2671
2672 static struct hdmi_codec_pdata vc4_hdmi_codec_pdata = {
2673 .ops = &vc4_hdmi_codec_ops,
2674 .max_i2s_channels = 8,
2675 .i2s = 1,
2676 };
2677
vc4_hdmi_audio_codec_release(void * ptr)2678 static void vc4_hdmi_audio_codec_release(void *ptr)
2679 {
2680 struct vc4_hdmi *vc4_hdmi = ptr;
2681
2682 platform_device_unregister(vc4_hdmi->audio.codec_pdev);
2683 vc4_hdmi->audio.codec_pdev = NULL;
2684 }
2685
vc4_hdmi_audio_init(struct vc4_hdmi * vc4_hdmi)2686 static int vc4_hdmi_audio_init(struct vc4_hdmi *vc4_hdmi)
2687 {
2688 const struct vc4_hdmi_register *mai_data =
2689 &vc4_hdmi->variant->registers[HDMI_MAI_DATA];
2690 struct snd_soc_dai_link *dai_link = &vc4_hdmi->audio.link;
2691 struct snd_soc_card *card = &vc4_hdmi->audio.card;
2692 struct device *dev = &vc4_hdmi->pdev->dev;
2693 struct platform_device *codec_pdev;
2694 const __be32 *addr;
2695 int index, len;
2696 int ret;
2697
2698 /*
2699 * ASoC makes it a bit hard to retrieve a pointer to the
2700 * vc4_hdmi structure. Registering the card will overwrite our
2701 * device drvdata with a pointer to the snd_soc_card structure,
2702 * which can then be used to retrieve whatever drvdata we want
2703 * to associate.
2704 *
2705 * However, that doesn't fly in the case where we wouldn't
2706 * register an ASoC card (because of an old DT that is missing
2707 * the dmas properties for example), then the card isn't
2708 * registered and the device drvdata wouldn't be set.
2709 *
2710 * We can deal with both cases by making sure a snd_soc_card
2711 * pointer and a vc4_hdmi structure are pointing to the same
2712 * memory address, so we can treat them indistinctly without any
2713 * issue.
2714 */
2715 BUILD_BUG_ON(offsetof(struct vc4_hdmi_audio, card) != 0);
2716 BUILD_BUG_ON(offsetof(struct vc4_hdmi, audio) != 0);
2717
2718 if (!of_find_property(dev->of_node, "dmas", &len) || !len) {
2719 dev_warn(dev,
2720 "'dmas' DT property is missing or empty, no HDMI audio\n");
2721 return 0;
2722 }
2723
2724 if (mai_data->reg != VC4_HD) {
2725 WARN_ONCE(true, "MAI isn't in the HD block\n");
2726 return -EINVAL;
2727 }
2728
2729 /*
2730 * Get the physical address of VC4_HD_MAI_DATA. We need to retrieve
2731 * the bus address specified in the DT, because the physical address
2732 * (the one returned by platform_get_resource()) is not appropriate
2733 * for DMA transfers.
2734 * This VC/MMU should probably be exposed to avoid this kind of hacks.
2735 */
2736 index = of_property_match_string(dev->of_node, "reg-names", "hd");
2737 /* Before BCM2711, we don't have a named register range */
2738 if (index < 0)
2739 index = 1;
2740
2741 addr = of_get_address(dev->of_node, index, NULL, NULL);
2742 if (!addr)
2743 return -EINVAL;
2744
2745 vc4_hdmi->audio.dma_data.addr = be32_to_cpup(addr) + mai_data->offset;
2746 vc4_hdmi->audio.dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
2747 vc4_hdmi->audio.dma_data.maxburst = 2;
2748
2749 /*
2750 * NOTE: Strictly speaking, we should probably use a DRM-managed
2751 * registration there to avoid removing all the audio components
2752 * by the time the driver doesn't have any user anymore.
2753 *
2754 * However, the ASoC core uses a number of devm_kzalloc calls
2755 * when registering, even when using non-device-managed
2756 * functions (such as in snd_soc_register_component()).
2757 *
2758 * If we call snd_soc_unregister_component() in a DRM-managed
2759 * action, the device-managed actions have already been executed
2760 * and thus we would access memory that has been freed.
2761 *
2762 * Using device-managed hooks here probably leaves us open to a
2763 * bunch of issues if userspace still has a handle on the ALSA
2764 * device when the device is removed. However, this is mitigated
2765 * by the use of drm_dev_enter()/drm_dev_exit() in the audio
2766 * path to prevent the access to the device resources if it
2767 * isn't there anymore.
2768 *
2769 * Then, the vc4_hdmi structure is DRM-managed and thus only
2770 * freed whenever the last user has closed the DRM device file.
2771 * It should thus outlive ALSA in most situations.
2772 */
2773 ret = devm_snd_dmaengine_pcm_register(dev, &pcm_conf, 0);
2774 if (ret) {
2775 dev_err(dev, "Could not register PCM component: %d\n", ret);
2776 return ret;
2777 }
2778
2779 ret = devm_snd_soc_register_component(dev, &vc4_hdmi_audio_cpu_dai_comp,
2780 &vc4_hdmi_audio_cpu_dai_drv, 1);
2781 if (ret) {
2782 dev_err(dev, "Could not register CPU DAI: %d\n", ret);
2783 return ret;
2784 }
2785
2786 codec_pdev = platform_device_register_data(dev, HDMI_CODEC_DRV_NAME,
2787 PLATFORM_DEVID_AUTO,
2788 &vc4_hdmi_codec_pdata,
2789 sizeof(vc4_hdmi_codec_pdata));
2790 if (IS_ERR(codec_pdev)) {
2791 dev_err(dev, "Couldn't register the HDMI codec: %ld\n", PTR_ERR(codec_pdev));
2792 return PTR_ERR(codec_pdev);
2793 }
2794 vc4_hdmi->audio.codec_pdev = codec_pdev;
2795
2796 ret = devm_add_action_or_reset(dev, vc4_hdmi_audio_codec_release, vc4_hdmi);
2797 if (ret)
2798 return ret;
2799
2800 dai_link->cpus = &vc4_hdmi->audio.cpu;
2801 dai_link->codecs = &vc4_hdmi->audio.codec;
2802 dai_link->platforms = &vc4_hdmi->audio.platform;
2803
2804 dai_link->num_cpus = 1;
2805 dai_link->num_codecs = 1;
2806 dai_link->num_platforms = 1;
2807
2808 dai_link->name = "MAI";
2809 dai_link->stream_name = "MAI PCM";
2810 dai_link->codecs->dai_name = "i2s-hifi";
2811 dai_link->cpus->dai_name = dev_name(dev);
2812 dai_link->codecs->name = dev_name(&codec_pdev->dev);
2813 dai_link->platforms->name = dev_name(dev);
2814
2815 card->dai_link = dai_link;
2816 card->num_links = 1;
2817 card->name = vc4_hdmi->variant->card_name;
2818 card->driver_name = "vc4-hdmi";
2819 card->dev = dev;
2820 card->owner = THIS_MODULE;
2821
2822 /*
2823 * Be careful, snd_soc_register_card() calls dev_set_drvdata() and
2824 * stores a pointer to the snd card object in dev->driver_data. This
2825 * means we cannot use it for something else. The hdmi back-pointer is
2826 * now stored in card->drvdata and should be retrieved with
2827 * snd_soc_card_get_drvdata() if needed.
2828 */
2829 snd_soc_card_set_drvdata(card, vc4_hdmi);
2830 ret = devm_snd_soc_register_card(dev, card);
2831 if (ret)
2832 dev_err_probe(dev, ret, "Could not register sound card\n");
2833
2834 return ret;
2835
2836 }
2837
vc4_hdmi_hpd_irq_thread(int irq,void * priv)2838 static irqreturn_t vc4_hdmi_hpd_irq_thread(int irq, void *priv)
2839 {
2840 struct vc4_hdmi *vc4_hdmi = priv;
2841 struct drm_connector *connector = &vc4_hdmi->connector;
2842 struct drm_device *dev = connector->dev;
2843
2844 if (dev && dev->registered)
2845 drm_connector_helper_hpd_irq_event(connector);
2846
2847 return IRQ_HANDLED;
2848 }
2849
vc4_hdmi_hotplug_init(struct vc4_hdmi * vc4_hdmi)2850 static int vc4_hdmi_hotplug_init(struct vc4_hdmi *vc4_hdmi)
2851 {
2852 struct drm_connector *connector = &vc4_hdmi->connector;
2853 struct platform_device *pdev = vc4_hdmi->pdev;
2854 int ret;
2855
2856 if (vc4_hdmi->variant->external_irq_controller) {
2857 unsigned int hpd_con = platform_get_irq_byname(pdev, "hpd-connected");
2858 unsigned int hpd_rm = platform_get_irq_byname(pdev, "hpd-removed");
2859
2860 ret = devm_request_threaded_irq(&pdev->dev, hpd_con,
2861 NULL,
2862 vc4_hdmi_hpd_irq_thread, IRQF_ONESHOT,
2863 "vc4 hdmi hpd connected", vc4_hdmi);
2864 if (ret)
2865 return ret;
2866
2867 ret = devm_request_threaded_irq(&pdev->dev, hpd_rm,
2868 NULL,
2869 vc4_hdmi_hpd_irq_thread, IRQF_ONESHOT,
2870 "vc4 hdmi hpd disconnected", vc4_hdmi);
2871 if (ret)
2872 return ret;
2873
2874 connector->polled = DRM_CONNECTOR_POLL_HPD;
2875 }
2876
2877 return 0;
2878 }
2879
2880 #ifdef CONFIG_DRM_VC4_HDMI_CEC
vc4_cec_irq_handler_rx_thread(int irq,void * priv)2881 static irqreturn_t vc4_cec_irq_handler_rx_thread(int irq, void *priv)
2882 {
2883 struct vc4_hdmi *vc4_hdmi = priv;
2884
2885 if (vc4_hdmi->cec_rx_msg.len)
2886 cec_received_msg(vc4_hdmi->cec_adap,
2887 &vc4_hdmi->cec_rx_msg);
2888
2889 return IRQ_HANDLED;
2890 }
2891
vc4_cec_irq_handler_tx_thread(int irq,void * priv)2892 static irqreturn_t vc4_cec_irq_handler_tx_thread(int irq, void *priv)
2893 {
2894 struct vc4_hdmi *vc4_hdmi = priv;
2895
2896 if (vc4_hdmi->cec_tx_ok) {
2897 cec_transmit_done(vc4_hdmi->cec_adap, CEC_TX_STATUS_OK,
2898 0, 0, 0, 0);
2899 } else {
2900 /*
2901 * This CEC implementation makes 1 retry, so if we
2902 * get a NACK, then that means it made 2 attempts.
2903 */
2904 cec_transmit_done(vc4_hdmi->cec_adap, CEC_TX_STATUS_NACK,
2905 0, 2, 0, 0);
2906 }
2907 return IRQ_HANDLED;
2908 }
2909
vc4_cec_irq_handler_thread(int irq,void * priv)2910 static irqreturn_t vc4_cec_irq_handler_thread(int irq, void *priv)
2911 {
2912 struct vc4_hdmi *vc4_hdmi = priv;
2913 irqreturn_t ret;
2914
2915 if (vc4_hdmi->cec_irq_was_rx)
2916 ret = vc4_cec_irq_handler_rx_thread(irq, priv);
2917 else
2918 ret = vc4_cec_irq_handler_tx_thread(irq, priv);
2919
2920 return ret;
2921 }
2922
vc4_cec_read_msg(struct vc4_hdmi * vc4_hdmi,u32 cntrl1)2923 static void vc4_cec_read_msg(struct vc4_hdmi *vc4_hdmi, u32 cntrl1)
2924 {
2925 struct drm_device *dev = vc4_hdmi->connector.dev;
2926 struct cec_msg *msg = &vc4_hdmi->cec_rx_msg;
2927 unsigned int i;
2928
2929 lockdep_assert_held(&vc4_hdmi->hw_lock);
2930
2931 msg->len = 1 + ((cntrl1 & VC4_HDMI_CEC_REC_WRD_CNT_MASK) >>
2932 VC4_HDMI_CEC_REC_WRD_CNT_SHIFT);
2933
2934 if (msg->len > 16) {
2935 drm_err(dev, "Attempting to read too much data (%d)\n", msg->len);
2936 return;
2937 }
2938
2939 for (i = 0; i < msg->len; i += 4) {
2940 u32 val = HDMI_READ(HDMI_CEC_RX_DATA_1 + (i >> 2));
2941
2942 msg->msg[i] = val & 0xff;
2943 msg->msg[i + 1] = (val >> 8) & 0xff;
2944 msg->msg[i + 2] = (val >> 16) & 0xff;
2945 msg->msg[i + 3] = (val >> 24) & 0xff;
2946 }
2947 }
2948
vc4_cec_irq_handler_tx_bare_locked(struct vc4_hdmi * vc4_hdmi)2949 static irqreturn_t vc4_cec_irq_handler_tx_bare_locked(struct vc4_hdmi *vc4_hdmi)
2950 {
2951 u32 cntrl1;
2952
2953 /*
2954 * We don't need to protect the register access using
2955 * drm_dev_enter() there because the interrupt handler lifetime
2956 * is tied to the device itself, and not to the DRM device.
2957 *
2958 * So when the device will be gone, one of the first thing we
2959 * will be doing will be to unregister the interrupt handler,
2960 * and then unregister the DRM device. drm_dev_enter() would
2961 * thus always succeed if we are here.
2962 */
2963
2964 lockdep_assert_held(&vc4_hdmi->hw_lock);
2965
2966 cntrl1 = HDMI_READ(HDMI_CEC_CNTRL_1);
2967 vc4_hdmi->cec_tx_ok = cntrl1 & VC4_HDMI_CEC_TX_STATUS_GOOD;
2968 cntrl1 &= ~VC4_HDMI_CEC_START_XMIT_BEGIN;
2969 HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1);
2970
2971 return IRQ_WAKE_THREAD;
2972 }
2973
vc4_cec_irq_handler_tx_bare(int irq,void * priv)2974 static irqreturn_t vc4_cec_irq_handler_tx_bare(int irq, void *priv)
2975 {
2976 struct vc4_hdmi *vc4_hdmi = priv;
2977 irqreturn_t ret;
2978
2979 spin_lock(&vc4_hdmi->hw_lock);
2980 ret = vc4_cec_irq_handler_tx_bare_locked(vc4_hdmi);
2981 spin_unlock(&vc4_hdmi->hw_lock);
2982
2983 return ret;
2984 }
2985
vc4_cec_irq_handler_rx_bare_locked(struct vc4_hdmi * vc4_hdmi)2986 static irqreturn_t vc4_cec_irq_handler_rx_bare_locked(struct vc4_hdmi *vc4_hdmi)
2987 {
2988 u32 cntrl1;
2989
2990 lockdep_assert_held(&vc4_hdmi->hw_lock);
2991
2992 /*
2993 * We don't need to protect the register access using
2994 * drm_dev_enter() there because the interrupt handler lifetime
2995 * is tied to the device itself, and not to the DRM device.
2996 *
2997 * So when the device will be gone, one of the first thing we
2998 * will be doing will be to unregister the interrupt handler,
2999 * and then unregister the DRM device. drm_dev_enter() would
3000 * thus always succeed if we are here.
3001 */
3002
3003 vc4_hdmi->cec_rx_msg.len = 0;
3004 cntrl1 = HDMI_READ(HDMI_CEC_CNTRL_1);
3005 vc4_cec_read_msg(vc4_hdmi, cntrl1);
3006 cntrl1 |= VC4_HDMI_CEC_CLEAR_RECEIVE_OFF;
3007 HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1);
3008 cntrl1 &= ~VC4_HDMI_CEC_CLEAR_RECEIVE_OFF;
3009
3010 HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1);
3011
3012 return IRQ_WAKE_THREAD;
3013 }
3014
vc4_cec_irq_handler_rx_bare(int irq,void * priv)3015 static irqreturn_t vc4_cec_irq_handler_rx_bare(int irq, void *priv)
3016 {
3017 struct vc4_hdmi *vc4_hdmi = priv;
3018 irqreturn_t ret;
3019
3020 spin_lock(&vc4_hdmi->hw_lock);
3021 ret = vc4_cec_irq_handler_rx_bare_locked(vc4_hdmi);
3022 spin_unlock(&vc4_hdmi->hw_lock);
3023
3024 return ret;
3025 }
3026
vc4_cec_irq_handler(int irq,void * priv)3027 static irqreturn_t vc4_cec_irq_handler(int irq, void *priv)
3028 {
3029 struct vc4_hdmi *vc4_hdmi = priv;
3030 u32 stat = HDMI_READ(HDMI_CEC_CPU_STATUS);
3031 irqreturn_t ret;
3032 u32 cntrl5;
3033
3034 /*
3035 * We don't need to protect the register access using
3036 * drm_dev_enter() there because the interrupt handler lifetime
3037 * is tied to the device itself, and not to the DRM device.
3038 *
3039 * So when the device will be gone, one of the first thing we
3040 * will be doing will be to unregister the interrupt handler,
3041 * and then unregister the DRM device. drm_dev_enter() would
3042 * thus always succeed if we are here.
3043 */
3044
3045 if (!(stat & VC4_HDMI_CPU_CEC))
3046 return IRQ_NONE;
3047
3048 spin_lock(&vc4_hdmi->hw_lock);
3049 cntrl5 = HDMI_READ(HDMI_CEC_CNTRL_5);
3050 vc4_hdmi->cec_irq_was_rx = cntrl5 & VC4_HDMI_CEC_RX_CEC_INT;
3051 if (vc4_hdmi->cec_irq_was_rx)
3052 ret = vc4_cec_irq_handler_rx_bare_locked(vc4_hdmi);
3053 else
3054 ret = vc4_cec_irq_handler_tx_bare_locked(vc4_hdmi);
3055
3056 HDMI_WRITE(HDMI_CEC_CPU_CLEAR, VC4_HDMI_CPU_CEC);
3057 spin_unlock(&vc4_hdmi->hw_lock);
3058
3059 return ret;
3060 }
3061
vc4_hdmi_cec_enable(struct cec_adapter * adap)3062 static int vc4_hdmi_cec_enable(struct cec_adapter *adap)
3063 {
3064 struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
3065 struct drm_device *drm = vc4_hdmi->connector.dev;
3066 /* clock period in microseconds */
3067 const u32 usecs = 1000000 / CEC_CLOCK_FREQ;
3068 unsigned long flags;
3069 u32 val;
3070 int ret;
3071 int idx;
3072
3073 if (!drm_dev_enter(drm, &idx))
3074 /*
3075 * We can't return an error code, because the CEC
3076 * framework will emit WARN_ON messages at unbind
3077 * otherwise.
3078 */
3079 return 0;
3080
3081 ret = pm_runtime_resume_and_get(&vc4_hdmi->pdev->dev);
3082 if (ret) {
3083 drm_dev_exit(idx);
3084 return ret;
3085 }
3086
3087 mutex_lock(&vc4_hdmi->mutex);
3088
3089 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
3090
3091 val = HDMI_READ(HDMI_CEC_CNTRL_5);
3092 val &= ~(VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET |
3093 VC4_HDMI_CEC_CNT_TO_4700_US_MASK |
3094 VC4_HDMI_CEC_CNT_TO_4500_US_MASK);
3095 val |= ((4700 / usecs) << VC4_HDMI_CEC_CNT_TO_4700_US_SHIFT) |
3096 ((4500 / usecs) << VC4_HDMI_CEC_CNT_TO_4500_US_SHIFT);
3097
3098 HDMI_WRITE(HDMI_CEC_CNTRL_5, val |
3099 VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET);
3100 HDMI_WRITE(HDMI_CEC_CNTRL_5, val);
3101 HDMI_WRITE(HDMI_CEC_CNTRL_2,
3102 ((1500 / usecs) << VC4_HDMI_CEC_CNT_TO_1500_US_SHIFT) |
3103 ((1300 / usecs) << VC4_HDMI_CEC_CNT_TO_1300_US_SHIFT) |
3104 ((800 / usecs) << VC4_HDMI_CEC_CNT_TO_800_US_SHIFT) |
3105 ((600 / usecs) << VC4_HDMI_CEC_CNT_TO_600_US_SHIFT) |
3106 ((400 / usecs) << VC4_HDMI_CEC_CNT_TO_400_US_SHIFT));
3107 HDMI_WRITE(HDMI_CEC_CNTRL_3,
3108 ((2750 / usecs) << VC4_HDMI_CEC_CNT_TO_2750_US_SHIFT) |
3109 ((2400 / usecs) << VC4_HDMI_CEC_CNT_TO_2400_US_SHIFT) |
3110 ((2050 / usecs) << VC4_HDMI_CEC_CNT_TO_2050_US_SHIFT) |
3111 ((1700 / usecs) << VC4_HDMI_CEC_CNT_TO_1700_US_SHIFT));
3112 HDMI_WRITE(HDMI_CEC_CNTRL_4,
3113 ((4300 / usecs) << VC4_HDMI_CEC_CNT_TO_4300_US_SHIFT) |
3114 ((3900 / usecs) << VC4_HDMI_CEC_CNT_TO_3900_US_SHIFT) |
3115 ((3600 / usecs) << VC4_HDMI_CEC_CNT_TO_3600_US_SHIFT) |
3116 ((3500 / usecs) << VC4_HDMI_CEC_CNT_TO_3500_US_SHIFT));
3117
3118 if (!vc4_hdmi->variant->external_irq_controller)
3119 HDMI_WRITE(HDMI_CEC_CPU_MASK_CLEAR, VC4_HDMI_CPU_CEC);
3120
3121 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
3122
3123 mutex_unlock(&vc4_hdmi->mutex);
3124 drm_dev_exit(idx);
3125
3126 return 0;
3127 }
3128
vc4_hdmi_cec_disable(struct cec_adapter * adap)3129 static int vc4_hdmi_cec_disable(struct cec_adapter *adap)
3130 {
3131 struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
3132 struct drm_device *drm = vc4_hdmi->connector.dev;
3133 unsigned long flags;
3134 int idx;
3135
3136 if (!drm_dev_enter(drm, &idx))
3137 /*
3138 * We can't return an error code, because the CEC
3139 * framework will emit WARN_ON messages at unbind
3140 * otherwise.
3141 */
3142 return 0;
3143
3144 mutex_lock(&vc4_hdmi->mutex);
3145
3146 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
3147
3148 if (!vc4_hdmi->variant->external_irq_controller)
3149 HDMI_WRITE(HDMI_CEC_CPU_MASK_SET, VC4_HDMI_CPU_CEC);
3150
3151 HDMI_WRITE(HDMI_CEC_CNTRL_5, HDMI_READ(HDMI_CEC_CNTRL_5) |
3152 VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET);
3153
3154 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
3155
3156 mutex_unlock(&vc4_hdmi->mutex);
3157
3158 pm_runtime_put(&vc4_hdmi->pdev->dev);
3159
3160 drm_dev_exit(idx);
3161
3162 return 0;
3163 }
3164
vc4_hdmi_cec_adap_enable(struct cec_adapter * adap,bool enable)3165 static int vc4_hdmi_cec_adap_enable(struct cec_adapter *adap, bool enable)
3166 {
3167 if (enable)
3168 return vc4_hdmi_cec_enable(adap);
3169 else
3170 return vc4_hdmi_cec_disable(adap);
3171 }
3172
vc4_hdmi_cec_adap_log_addr(struct cec_adapter * adap,u8 log_addr)3173 static int vc4_hdmi_cec_adap_log_addr(struct cec_adapter *adap, u8 log_addr)
3174 {
3175 struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
3176 struct drm_device *drm = vc4_hdmi->connector.dev;
3177 unsigned long flags;
3178 int idx;
3179
3180 if (!drm_dev_enter(drm, &idx))
3181 /*
3182 * We can't return an error code, because the CEC
3183 * framework will emit WARN_ON messages at unbind
3184 * otherwise.
3185 */
3186 return 0;
3187
3188 mutex_lock(&vc4_hdmi->mutex);
3189 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
3190 HDMI_WRITE(HDMI_CEC_CNTRL_1,
3191 (HDMI_READ(HDMI_CEC_CNTRL_1) & ~VC4_HDMI_CEC_ADDR_MASK) |
3192 (log_addr & 0xf) << VC4_HDMI_CEC_ADDR_SHIFT);
3193 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
3194 mutex_unlock(&vc4_hdmi->mutex);
3195
3196 drm_dev_exit(idx);
3197
3198 return 0;
3199 }
3200
vc4_hdmi_cec_adap_transmit(struct cec_adapter * adap,u8 attempts,u32 signal_free_time,struct cec_msg * msg)3201 static int vc4_hdmi_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
3202 u32 signal_free_time, struct cec_msg *msg)
3203 {
3204 struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
3205 struct drm_device *dev = vc4_hdmi->connector.dev;
3206 unsigned long flags;
3207 u32 val;
3208 unsigned int i;
3209 int idx;
3210
3211 if (!drm_dev_enter(dev, &idx))
3212 return -ENODEV;
3213
3214 if (msg->len > 16) {
3215 drm_err(dev, "Attempting to transmit too much data (%d)\n", msg->len);
3216 drm_dev_exit(idx);
3217 return -ENOMEM;
3218 }
3219
3220 mutex_lock(&vc4_hdmi->mutex);
3221
3222 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
3223
3224 for (i = 0; i < msg->len; i += 4)
3225 HDMI_WRITE(HDMI_CEC_TX_DATA_1 + (i >> 2),
3226 (msg->msg[i]) |
3227 (msg->msg[i + 1] << 8) |
3228 (msg->msg[i + 2] << 16) |
3229 (msg->msg[i + 3] << 24));
3230
3231 val = HDMI_READ(HDMI_CEC_CNTRL_1);
3232 val &= ~VC4_HDMI_CEC_START_XMIT_BEGIN;
3233 HDMI_WRITE(HDMI_CEC_CNTRL_1, val);
3234 val &= ~VC4_HDMI_CEC_MESSAGE_LENGTH_MASK;
3235 val |= (msg->len - 1) << VC4_HDMI_CEC_MESSAGE_LENGTH_SHIFT;
3236 val |= VC4_HDMI_CEC_START_XMIT_BEGIN;
3237
3238 HDMI_WRITE(HDMI_CEC_CNTRL_1, val);
3239
3240 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
3241 mutex_unlock(&vc4_hdmi->mutex);
3242 drm_dev_exit(idx);
3243
3244 return 0;
3245 }
3246
3247 static const struct cec_adap_ops vc4_hdmi_cec_adap_ops = {
3248 .adap_enable = vc4_hdmi_cec_adap_enable,
3249 .adap_log_addr = vc4_hdmi_cec_adap_log_addr,
3250 .adap_transmit = vc4_hdmi_cec_adap_transmit,
3251 };
3252
vc4_hdmi_cec_release(void * ptr)3253 static void vc4_hdmi_cec_release(void *ptr)
3254 {
3255 struct vc4_hdmi *vc4_hdmi = ptr;
3256
3257 cec_unregister_adapter(vc4_hdmi->cec_adap);
3258 vc4_hdmi->cec_adap = NULL;
3259 }
3260
vc4_hdmi_cec_init(struct vc4_hdmi * vc4_hdmi)3261 static int vc4_hdmi_cec_init(struct vc4_hdmi *vc4_hdmi)
3262 {
3263 struct cec_connector_info conn_info;
3264 struct platform_device *pdev = vc4_hdmi->pdev;
3265 struct device *dev = &pdev->dev;
3266 int ret;
3267
3268 if (!of_property_present(dev->of_node, "interrupts")) {
3269 dev_warn(dev, "'interrupts' DT property is missing, no CEC\n");
3270 return 0;
3271 }
3272
3273 vc4_hdmi->cec_adap = cec_allocate_adapter(&vc4_hdmi_cec_adap_ops,
3274 vc4_hdmi,
3275 vc4_hdmi->variant->card_name,
3276 CEC_CAP_DEFAULTS |
3277 CEC_CAP_CONNECTOR_INFO, 1);
3278 ret = PTR_ERR_OR_ZERO(vc4_hdmi->cec_adap);
3279 if (ret < 0)
3280 return ret;
3281
3282 cec_fill_conn_info_from_drm(&conn_info, &vc4_hdmi->connector);
3283 cec_s_conn_info(vc4_hdmi->cec_adap, &conn_info);
3284
3285 if (vc4_hdmi->variant->external_irq_controller) {
3286 ret = devm_request_threaded_irq(dev, platform_get_irq_byname(pdev, "cec-rx"),
3287 vc4_cec_irq_handler_rx_bare,
3288 vc4_cec_irq_handler_rx_thread, 0,
3289 "vc4 hdmi cec rx", vc4_hdmi);
3290 if (ret)
3291 goto err_delete_cec_adap;
3292
3293 ret = devm_request_threaded_irq(dev, platform_get_irq_byname(pdev, "cec-tx"),
3294 vc4_cec_irq_handler_tx_bare,
3295 vc4_cec_irq_handler_tx_thread, 0,
3296 "vc4 hdmi cec tx", vc4_hdmi);
3297 if (ret)
3298 goto err_delete_cec_adap;
3299 } else {
3300 ret = devm_request_threaded_irq(dev, platform_get_irq(pdev, 0),
3301 vc4_cec_irq_handler,
3302 vc4_cec_irq_handler_thread, 0,
3303 "vc4 hdmi cec", vc4_hdmi);
3304 if (ret)
3305 goto err_delete_cec_adap;
3306 }
3307
3308 ret = cec_register_adapter(vc4_hdmi->cec_adap, &pdev->dev);
3309 if (ret < 0)
3310 goto err_delete_cec_adap;
3311
3312 /*
3313 * NOTE: Strictly speaking, we should probably use a DRM-managed
3314 * registration there to avoid removing the CEC adapter by the
3315 * time the DRM driver doesn't have any user anymore.
3316 *
3317 * However, the CEC framework already cleans up the CEC adapter
3318 * only when the last user has closed its file descriptor, so we
3319 * don't need to handle it in DRM.
3320 *
3321 * By the time the device-managed hook is executed, we will give
3322 * up our reference to the CEC adapter and therefore don't
3323 * really care when it's actually freed.
3324 *
3325 * There's still a problematic sequence: if we unregister our
3326 * CEC adapter, but the userspace keeps a handle on the CEC
3327 * adapter but not the DRM device for some reason. In such a
3328 * case, our vc4_hdmi structure will be freed, but the
3329 * cec_adapter structure will have a dangling pointer to what
3330 * used to be our HDMI controller. If we get a CEC call at that
3331 * moment, we could end up with a use-after-free. Fortunately,
3332 * the CEC framework already handles this too, by calling
3333 * cec_is_registered() in cec_ioctl() and cec_poll().
3334 */
3335 ret = devm_add_action_or_reset(dev, vc4_hdmi_cec_release, vc4_hdmi);
3336 if (ret)
3337 return ret;
3338
3339 return 0;
3340
3341 err_delete_cec_adap:
3342 cec_delete_adapter(vc4_hdmi->cec_adap);
3343
3344 return ret;
3345 }
3346 #else
vc4_hdmi_cec_init(struct vc4_hdmi * vc4_hdmi)3347 static int vc4_hdmi_cec_init(struct vc4_hdmi *vc4_hdmi)
3348 {
3349 return 0;
3350 }
3351 #endif
3352
vc4_hdmi_free_regset(struct drm_device * drm,void * ptr)3353 static void vc4_hdmi_free_regset(struct drm_device *drm, void *ptr)
3354 {
3355 struct debugfs_reg32 *regs = ptr;
3356
3357 kfree(regs);
3358 }
3359
vc4_hdmi_build_regset(struct drm_device * drm,struct vc4_hdmi * vc4_hdmi,struct debugfs_regset32 * regset,enum vc4_hdmi_regs reg)3360 static int vc4_hdmi_build_regset(struct drm_device *drm,
3361 struct vc4_hdmi *vc4_hdmi,
3362 struct debugfs_regset32 *regset,
3363 enum vc4_hdmi_regs reg)
3364 {
3365 const struct vc4_hdmi_variant *variant = vc4_hdmi->variant;
3366 struct debugfs_reg32 *regs, *new_regs;
3367 unsigned int count = 0;
3368 unsigned int i;
3369 int ret;
3370
3371 regs = kcalloc(variant->num_registers, sizeof(*regs),
3372 GFP_KERNEL);
3373 if (!regs)
3374 return -ENOMEM;
3375
3376 for (i = 0; i < variant->num_registers; i++) {
3377 const struct vc4_hdmi_register *field = &variant->registers[i];
3378
3379 if (field->reg != reg)
3380 continue;
3381
3382 regs[count].name = field->name;
3383 regs[count].offset = field->offset;
3384 count++;
3385 }
3386
3387 new_regs = krealloc(regs, count * sizeof(*regs), GFP_KERNEL);
3388 if (!new_regs)
3389 return -ENOMEM;
3390
3391 regset->base = __vc4_hdmi_get_field_base(vc4_hdmi, reg);
3392 regset->regs = new_regs;
3393 regset->nregs = count;
3394
3395 ret = drmm_add_action_or_reset(drm, vc4_hdmi_free_regset, new_regs);
3396 if (ret)
3397 return ret;
3398
3399 return 0;
3400 }
3401
vc4_hdmi_init_resources(struct drm_device * drm,struct vc4_hdmi * vc4_hdmi)3402 static int vc4_hdmi_init_resources(struct drm_device *drm,
3403 struct vc4_hdmi *vc4_hdmi)
3404 {
3405 struct platform_device *pdev = vc4_hdmi->pdev;
3406 struct device *dev = &pdev->dev;
3407 int ret;
3408
3409 vc4_hdmi->hdmicore_regs = vc4_ioremap_regs(pdev, 0);
3410 if (IS_ERR(vc4_hdmi->hdmicore_regs))
3411 return PTR_ERR(vc4_hdmi->hdmicore_regs);
3412
3413 vc4_hdmi->hd_regs = vc4_ioremap_regs(pdev, 1);
3414 if (IS_ERR(vc4_hdmi->hd_regs))
3415 return PTR_ERR(vc4_hdmi->hd_regs);
3416
3417 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->hd_regset, VC4_HD);
3418 if (ret)
3419 return ret;
3420
3421 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->hdmi_regset, VC4_HDMI);
3422 if (ret)
3423 return ret;
3424
3425 vc4_hdmi->pixel_clock = devm_clk_get(dev, "pixel");
3426 if (IS_ERR(vc4_hdmi->pixel_clock)) {
3427 ret = PTR_ERR(vc4_hdmi->pixel_clock);
3428 if (ret != -EPROBE_DEFER)
3429 DRM_ERROR("Failed to get pixel clock\n");
3430 return ret;
3431 }
3432
3433 vc4_hdmi->hsm_clock = devm_clk_get(dev, "hdmi");
3434 if (IS_ERR(vc4_hdmi->hsm_clock)) {
3435 DRM_ERROR("Failed to get HDMI state machine clock\n");
3436 return PTR_ERR(vc4_hdmi->hsm_clock);
3437 }
3438 vc4_hdmi->audio_clock = vc4_hdmi->hsm_clock;
3439 vc4_hdmi->cec_clock = vc4_hdmi->hsm_clock;
3440
3441 return 0;
3442 }
3443
vc5_hdmi_init_resources(struct drm_device * drm,struct vc4_hdmi * vc4_hdmi)3444 static int vc5_hdmi_init_resources(struct drm_device *drm,
3445 struct vc4_hdmi *vc4_hdmi)
3446 {
3447 struct platform_device *pdev = vc4_hdmi->pdev;
3448 struct device *dev = &pdev->dev;
3449 struct resource *res;
3450 int ret;
3451
3452 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hdmi");
3453 if (!res)
3454 return -ENODEV;
3455
3456 vc4_hdmi->hdmicore_regs = devm_ioremap(dev, res->start,
3457 resource_size(res));
3458 if (!vc4_hdmi->hdmicore_regs)
3459 return -ENOMEM;
3460
3461 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hd");
3462 if (!res)
3463 return -ENODEV;
3464
3465 vc4_hdmi->hd_regs = devm_ioremap(dev, res->start, resource_size(res));
3466 if (!vc4_hdmi->hd_regs)
3467 return -ENOMEM;
3468
3469 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cec");
3470 if (!res)
3471 return -ENODEV;
3472
3473 vc4_hdmi->cec_regs = devm_ioremap(dev, res->start, resource_size(res));
3474 if (!vc4_hdmi->cec_regs)
3475 return -ENOMEM;
3476
3477 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "csc");
3478 if (!res)
3479 return -ENODEV;
3480
3481 vc4_hdmi->csc_regs = devm_ioremap(dev, res->start, resource_size(res));
3482 if (!vc4_hdmi->csc_regs)
3483 return -ENOMEM;
3484
3485 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dvp");
3486 if (!res)
3487 return -ENODEV;
3488
3489 vc4_hdmi->dvp_regs = devm_ioremap(dev, res->start, resource_size(res));
3490 if (!vc4_hdmi->dvp_regs)
3491 return -ENOMEM;
3492
3493 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy");
3494 if (!res)
3495 return -ENODEV;
3496
3497 vc4_hdmi->phy_regs = devm_ioremap(dev, res->start, resource_size(res));
3498 if (!vc4_hdmi->phy_regs)
3499 return -ENOMEM;
3500
3501 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "packet");
3502 if (!res)
3503 return -ENODEV;
3504
3505 vc4_hdmi->ram_regs = devm_ioremap(dev, res->start, resource_size(res));
3506 if (!vc4_hdmi->ram_regs)
3507 return -ENOMEM;
3508
3509 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rm");
3510 if (!res)
3511 return -ENODEV;
3512
3513 vc4_hdmi->rm_regs = devm_ioremap(dev, res->start, resource_size(res));
3514 if (!vc4_hdmi->rm_regs)
3515 return -ENOMEM;
3516
3517 vc4_hdmi->hsm_clock = devm_clk_get(dev, "hdmi");
3518 if (IS_ERR(vc4_hdmi->hsm_clock)) {
3519 DRM_ERROR("Failed to get HDMI state machine clock\n");
3520 return PTR_ERR(vc4_hdmi->hsm_clock);
3521 }
3522
3523 vc4_hdmi->pixel_bvb_clock = devm_clk_get(dev, "bvb");
3524 if (IS_ERR(vc4_hdmi->pixel_bvb_clock)) {
3525 DRM_ERROR("Failed to get pixel bvb clock\n");
3526 return PTR_ERR(vc4_hdmi->pixel_bvb_clock);
3527 }
3528
3529 vc4_hdmi->audio_clock = devm_clk_get(dev, "audio");
3530 if (IS_ERR(vc4_hdmi->audio_clock)) {
3531 DRM_ERROR("Failed to get audio clock\n");
3532 return PTR_ERR(vc4_hdmi->audio_clock);
3533 }
3534
3535 vc4_hdmi->cec_clock = devm_clk_get(dev, "cec");
3536 if (IS_ERR(vc4_hdmi->cec_clock)) {
3537 DRM_ERROR("Failed to get CEC clock\n");
3538 return PTR_ERR(vc4_hdmi->cec_clock);
3539 }
3540
3541 vc4_hdmi->reset = devm_reset_control_get(dev, NULL);
3542 if (IS_ERR(vc4_hdmi->reset)) {
3543 DRM_ERROR("Failed to get HDMI reset line\n");
3544 return PTR_ERR(vc4_hdmi->reset);
3545 }
3546
3547 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->hdmi_regset, VC4_HDMI);
3548 if (ret)
3549 return ret;
3550
3551 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->hd_regset, VC4_HD);
3552 if (ret)
3553 return ret;
3554
3555 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->cec_regset, VC5_CEC);
3556 if (ret)
3557 return ret;
3558
3559 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->csc_regset, VC5_CSC);
3560 if (ret)
3561 return ret;
3562
3563 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->dvp_regset, VC5_DVP);
3564 if (ret)
3565 return ret;
3566
3567 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->phy_regset, VC5_PHY);
3568 if (ret)
3569 return ret;
3570
3571 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->ram_regset, VC5_RAM);
3572 if (ret)
3573 return ret;
3574
3575 ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->rm_regset, VC5_RM);
3576 if (ret)
3577 return ret;
3578
3579 return 0;
3580 }
3581
vc4_hdmi_runtime_suspend(struct device * dev)3582 static int vc4_hdmi_runtime_suspend(struct device *dev)
3583 {
3584 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
3585
3586 clk_disable_unprepare(vc4_hdmi->hsm_clock);
3587
3588 return 0;
3589 }
3590
vc4_hdmi_runtime_resume(struct device * dev)3591 static int vc4_hdmi_runtime_resume(struct device *dev)
3592 {
3593 struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
3594 unsigned long __maybe_unused flags;
3595 u32 __maybe_unused value;
3596 unsigned long rate;
3597 int ret;
3598
3599 ret = clk_prepare_enable(vc4_hdmi->hsm_clock);
3600 if (ret)
3601 return ret;
3602
3603 /*
3604 * Whenever the RaspberryPi boots without an HDMI monitor
3605 * plugged in, the firmware won't have initialized the HSM clock
3606 * rate and it will be reported as 0.
3607 *
3608 * If we try to access a register of the controller in such a
3609 * case, it will lead to a silent CPU stall. Let's make sure we
3610 * prevent such a case.
3611 */
3612 rate = clk_get_rate(vc4_hdmi->hsm_clock);
3613 if (!rate) {
3614 ret = -EINVAL;
3615 goto err_disable_clk;
3616 }
3617
3618 if (vc4_hdmi->variant->reset)
3619 vc4_hdmi->variant->reset(vc4_hdmi);
3620
3621 #ifdef CONFIG_DRM_VC4_HDMI_CEC
3622 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
3623 value = HDMI_READ(HDMI_CEC_CNTRL_1);
3624 /* Set the logical address to Unregistered */
3625 value |= VC4_HDMI_CEC_ADDR_MASK;
3626 HDMI_WRITE(HDMI_CEC_CNTRL_1, value);
3627 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
3628
3629 vc4_hdmi_cec_update_clk_div(vc4_hdmi);
3630
3631 if (!vc4_hdmi->variant->external_irq_controller) {
3632 spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
3633 HDMI_WRITE(HDMI_CEC_CPU_MASK_SET, 0xffffffff);
3634 spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
3635 }
3636 #endif
3637
3638 return 0;
3639
3640 err_disable_clk:
3641 clk_disable_unprepare(vc4_hdmi->hsm_clock);
3642 return ret;
3643 }
3644
vc4_hdmi_put_ddc_device(void * ptr)3645 static void vc4_hdmi_put_ddc_device(void *ptr)
3646 {
3647 struct vc4_hdmi *vc4_hdmi = ptr;
3648
3649 put_device(&vc4_hdmi->ddc->dev);
3650 }
3651
vc4_hdmi_bind(struct device * dev,struct device * master,void * data)3652 static int vc4_hdmi_bind(struct device *dev, struct device *master, void *data)
3653 {
3654 const struct vc4_hdmi_variant *variant = of_device_get_match_data(dev);
3655 struct platform_device *pdev = to_platform_device(dev);
3656 struct drm_device *drm = dev_get_drvdata(master);
3657 struct vc4_hdmi *vc4_hdmi;
3658 struct drm_encoder *encoder;
3659 struct device_node *ddc_node;
3660 int ret;
3661
3662 vc4_hdmi = drmm_kzalloc(drm, sizeof(*vc4_hdmi), GFP_KERNEL);
3663 if (!vc4_hdmi)
3664 return -ENOMEM;
3665
3666 ret = drmm_mutex_init(drm, &vc4_hdmi->mutex);
3667 if (ret)
3668 return ret;
3669
3670 spin_lock_init(&vc4_hdmi->hw_lock);
3671 INIT_DELAYED_WORK(&vc4_hdmi->scrambling_work, vc4_hdmi_scrambling_wq);
3672
3673 dev_set_drvdata(dev, vc4_hdmi);
3674 encoder = &vc4_hdmi->encoder.base;
3675 vc4_hdmi->encoder.type = variant->encoder_type;
3676 vc4_hdmi->encoder.pre_crtc_configure = vc4_hdmi_encoder_pre_crtc_configure;
3677 vc4_hdmi->encoder.pre_crtc_enable = vc4_hdmi_encoder_pre_crtc_enable;
3678 vc4_hdmi->encoder.post_crtc_enable = vc4_hdmi_encoder_post_crtc_enable;
3679 vc4_hdmi->encoder.post_crtc_disable = vc4_hdmi_encoder_post_crtc_disable;
3680 vc4_hdmi->encoder.post_crtc_powerdown = vc4_hdmi_encoder_post_crtc_powerdown;
3681 vc4_hdmi->pdev = pdev;
3682 vc4_hdmi->variant = variant;
3683
3684 /*
3685 * Since we don't know the state of the controller and its
3686 * display (if any), let's assume it's always enabled.
3687 * vc4_hdmi_disable_scrambling() will thus run at boot, make
3688 * sure it's disabled, and avoid any inconsistency.
3689 */
3690 if (variant->max_pixel_clock > HDMI_14_MAX_TMDS_CLK)
3691 vc4_hdmi->scdc_enabled = true;
3692
3693 ret = variant->init_resources(drm, vc4_hdmi);
3694 if (ret)
3695 return ret;
3696
3697 ddc_node = of_parse_phandle(dev->of_node, "ddc", 0);
3698 if (!ddc_node) {
3699 DRM_ERROR("Failed to find ddc node in device tree\n");
3700 return -ENODEV;
3701 }
3702
3703 vc4_hdmi->ddc = of_find_i2c_adapter_by_node(ddc_node);
3704 of_node_put(ddc_node);
3705 if (!vc4_hdmi->ddc) {
3706 DRM_DEBUG("Failed to get ddc i2c adapter by node\n");
3707 return -EPROBE_DEFER;
3708 }
3709
3710 ret = devm_add_action_or_reset(dev, vc4_hdmi_put_ddc_device, vc4_hdmi);
3711 if (ret)
3712 return ret;
3713
3714 /* Only use the GPIO HPD pin if present in the DT, otherwise
3715 * we'll use the HDMI core's register.
3716 */
3717 vc4_hdmi->hpd_gpio = devm_gpiod_get_optional(dev, "hpd", GPIOD_IN);
3718 if (IS_ERR(vc4_hdmi->hpd_gpio)) {
3719 return PTR_ERR(vc4_hdmi->hpd_gpio);
3720 }
3721
3722 vc4_hdmi->disable_wifi_frequencies =
3723 of_property_read_bool(dev->of_node, "wifi-2.4ghz-coexistence");
3724
3725 ret = devm_pm_runtime_enable(dev);
3726 if (ret)
3727 return ret;
3728
3729 /*
3730 * We need to have the device powered up at this point to call
3731 * our reset hook and for the CEC init.
3732 */
3733 ret = pm_runtime_resume_and_get(dev);
3734 if (ret)
3735 return ret;
3736
3737 if ((of_device_is_compatible(dev->of_node, "brcm,bcm2711-hdmi0") ||
3738 of_device_is_compatible(dev->of_node, "brcm,bcm2711-hdmi1")) &&
3739 HDMI_READ(HDMI_VID_CTL) & VC4_HD_VID_CTL_ENABLE) {
3740 clk_prepare_enable(vc4_hdmi->pixel_clock);
3741 clk_prepare_enable(vc4_hdmi->hsm_clock);
3742 clk_prepare_enable(vc4_hdmi->pixel_bvb_clock);
3743 }
3744
3745 ret = drmm_encoder_init(drm, encoder,
3746 &vc4_hdmi_encoder_funcs,
3747 DRM_MODE_ENCODER_TMDS,
3748 NULL);
3749 if (ret)
3750 goto err_put_runtime_pm;
3751
3752 drm_encoder_helper_add(encoder, &vc4_hdmi_encoder_helper_funcs);
3753
3754 ret = vc4_hdmi_connector_init(drm, vc4_hdmi);
3755 if (ret)
3756 goto err_put_runtime_pm;
3757
3758 ret = vc4_hdmi_hotplug_init(vc4_hdmi);
3759 if (ret)
3760 goto err_put_runtime_pm;
3761
3762 ret = vc4_hdmi_cec_init(vc4_hdmi);
3763 if (ret)
3764 goto err_put_runtime_pm;
3765
3766 ret = vc4_hdmi_audio_init(vc4_hdmi);
3767 if (ret)
3768 goto err_put_runtime_pm;
3769
3770 pm_runtime_put_sync(dev);
3771
3772 return 0;
3773
3774 err_put_runtime_pm:
3775 pm_runtime_put_sync(dev);
3776
3777 return ret;
3778 }
3779
3780 static const struct component_ops vc4_hdmi_ops = {
3781 .bind = vc4_hdmi_bind,
3782 };
3783
vc4_hdmi_dev_probe(struct platform_device * pdev)3784 static int vc4_hdmi_dev_probe(struct platform_device *pdev)
3785 {
3786 return component_add(&pdev->dev, &vc4_hdmi_ops);
3787 }
3788
vc4_hdmi_dev_remove(struct platform_device * pdev)3789 static void vc4_hdmi_dev_remove(struct platform_device *pdev)
3790 {
3791 component_del(&pdev->dev, &vc4_hdmi_ops);
3792 }
3793
3794 static const struct vc4_hdmi_variant bcm2835_variant = {
3795 .encoder_type = VC4_ENCODER_TYPE_HDMI0,
3796 .debugfs_name = "hdmi_regs",
3797 .card_name = "vc4-hdmi",
3798 .max_pixel_clock = 162000000,
3799 .registers = vc4_hdmi_fields,
3800 .num_registers = ARRAY_SIZE(vc4_hdmi_fields),
3801
3802 .init_resources = vc4_hdmi_init_resources,
3803 .csc_setup = vc4_hdmi_csc_setup,
3804 .reset = vc4_hdmi_reset,
3805 .set_timings = vc4_hdmi_set_timings,
3806 .phy_init = vc4_hdmi_phy_init,
3807 .phy_disable = vc4_hdmi_phy_disable,
3808 .phy_rng_enable = vc4_hdmi_phy_rng_enable,
3809 .phy_rng_disable = vc4_hdmi_phy_rng_disable,
3810 .channel_map = vc4_hdmi_channel_map,
3811 .supports_hdr = false,
3812 };
3813
3814 static const struct vc4_hdmi_variant bcm2711_hdmi0_variant = {
3815 .encoder_type = VC4_ENCODER_TYPE_HDMI0,
3816 .debugfs_name = "hdmi0_regs",
3817 .card_name = "vc4-hdmi-0",
3818 .max_pixel_clock = 600000000,
3819 .registers = vc5_hdmi_hdmi0_fields,
3820 .num_registers = ARRAY_SIZE(vc5_hdmi_hdmi0_fields),
3821 .phy_lane_mapping = {
3822 PHY_LANE_0,
3823 PHY_LANE_1,
3824 PHY_LANE_2,
3825 PHY_LANE_CK,
3826 },
3827 .unsupported_odd_h_timings = true,
3828 .external_irq_controller = true,
3829
3830 .init_resources = vc5_hdmi_init_resources,
3831 .csc_setup = vc5_hdmi_csc_setup,
3832 .reset = vc5_hdmi_reset,
3833 .set_timings = vc5_hdmi_set_timings,
3834 .phy_init = vc5_hdmi_phy_init,
3835 .phy_disable = vc5_hdmi_phy_disable,
3836 .phy_rng_enable = vc5_hdmi_phy_rng_enable,
3837 .phy_rng_disable = vc5_hdmi_phy_rng_disable,
3838 .channel_map = vc5_hdmi_channel_map,
3839 .supports_hdr = true,
3840 .hp_detect = vc5_hdmi_hp_detect,
3841 };
3842
3843 static const struct vc4_hdmi_variant bcm2711_hdmi1_variant = {
3844 .encoder_type = VC4_ENCODER_TYPE_HDMI1,
3845 .debugfs_name = "hdmi1_regs",
3846 .card_name = "vc4-hdmi-1",
3847 .max_pixel_clock = HDMI_14_MAX_TMDS_CLK,
3848 .registers = vc5_hdmi_hdmi1_fields,
3849 .num_registers = ARRAY_SIZE(vc5_hdmi_hdmi1_fields),
3850 .phy_lane_mapping = {
3851 PHY_LANE_1,
3852 PHY_LANE_0,
3853 PHY_LANE_CK,
3854 PHY_LANE_2,
3855 },
3856 .unsupported_odd_h_timings = true,
3857 .external_irq_controller = true,
3858
3859 .init_resources = vc5_hdmi_init_resources,
3860 .csc_setup = vc5_hdmi_csc_setup,
3861 .reset = vc5_hdmi_reset,
3862 .set_timings = vc5_hdmi_set_timings,
3863 .phy_init = vc5_hdmi_phy_init,
3864 .phy_disable = vc5_hdmi_phy_disable,
3865 .phy_rng_enable = vc5_hdmi_phy_rng_enable,
3866 .phy_rng_disable = vc5_hdmi_phy_rng_disable,
3867 .channel_map = vc5_hdmi_channel_map,
3868 .supports_hdr = true,
3869 .hp_detect = vc5_hdmi_hp_detect,
3870 };
3871
3872 static const struct of_device_id vc4_hdmi_dt_match[] = {
3873 { .compatible = "brcm,bcm2835-hdmi", .data = &bcm2835_variant },
3874 { .compatible = "brcm,bcm2711-hdmi0", .data = &bcm2711_hdmi0_variant },
3875 { .compatible = "brcm,bcm2711-hdmi1", .data = &bcm2711_hdmi1_variant },
3876 {}
3877 };
3878
3879 static const struct dev_pm_ops vc4_hdmi_pm_ops = {
3880 SET_RUNTIME_PM_OPS(vc4_hdmi_runtime_suspend,
3881 vc4_hdmi_runtime_resume,
3882 NULL)
3883 };
3884
3885 struct platform_driver vc4_hdmi_driver = {
3886 .probe = vc4_hdmi_dev_probe,
3887 .remove_new = vc4_hdmi_dev_remove,
3888 .driver = {
3889 .name = "vc4_hdmi",
3890 .of_match_table = vc4_hdmi_dt_match,
3891 .pm = &vc4_hdmi_pm_ops,
3892 },
3893 };
3894