blob: 5d49bf9481624f0d62e1acf11959b9433cead5f4 [file] [log] [blame]
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001/*
2 * Copyright (C) 2015 Broadcom
3 * Copyright (c) 2014 The Linux Foundation. All rights reserved.
4 * Copyright (C) 2013 Red Hat
5 * Author: Rob Clark <robdclark@gmail.com>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published by
9 * the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20/**
21 * DOC: VC4 Falcon HDMI module
22 *
23 * The HDMI core has a state machine and a PHY. Most of the unit
24 * operates off of the HSM clock from CPRMAN. It also internally uses
25 * the PLLH_PIX clock for the PHY.
26 */
27
28#include "drm_atomic_helper.h"
29#include "drm_crtc_helper.h"
30#include "drm_edid.h"
31#include "linux/clk.h"
32#include "linux/component.h"
33#include "linux/i2c.h"
34#include "linux/of_gpio.h"
35#include "linux/of_platform.h"
36#include "vc4_drv.h"
37#include "vc4_regs.h"
38
39/* General HDMI hardware state. */
40struct vc4_hdmi {
41 struct platform_device *pdev;
42
43 struct drm_encoder *encoder;
44 struct drm_connector *connector;
45
46 struct i2c_adapter *ddc;
47 void __iomem *hdmicore_regs;
48 void __iomem *hd_regs;
49 int hpd_gpio;
Eric Anholt0b06e0a2016-02-29 17:53:01 -080050 bool hpd_active_low;
Eric Anholtc8b75bc2015-03-02 13:01:12 -080051
52 struct clk *pixel_clock;
53 struct clk *hsm_clock;
54};
55
56#define HDMI_READ(offset) readl(vc4->hdmi->hdmicore_regs + offset)
57#define HDMI_WRITE(offset, val) writel(val, vc4->hdmi->hdmicore_regs + offset)
58#define HD_READ(offset) readl(vc4->hdmi->hd_regs + offset)
59#define HD_WRITE(offset, val) writel(val, vc4->hdmi->hd_regs + offset)
60
61/* VC4 HDMI encoder KMS struct */
62struct vc4_hdmi_encoder {
63 struct vc4_encoder base;
64 bool hdmi_monitor;
Eric Anholt21317b32016-09-29 15:34:43 -070065 bool limited_rgb_range;
66 bool rgb_range_selectable;
Eric Anholtc8b75bc2015-03-02 13:01:12 -080067};
68
69static inline struct vc4_hdmi_encoder *
70to_vc4_hdmi_encoder(struct drm_encoder *encoder)
71{
72 return container_of(encoder, struct vc4_hdmi_encoder, base.base);
73}
74
75/* VC4 HDMI connector KMS struct */
76struct vc4_hdmi_connector {
77 struct drm_connector base;
78
79 /* Since the connector is attached to just the one encoder,
80 * this is the reference to it so we can do the best_encoder()
81 * hook.
82 */
83 struct drm_encoder *encoder;
84};
85
86static inline struct vc4_hdmi_connector *
87to_vc4_hdmi_connector(struct drm_connector *connector)
88{
89 return container_of(connector, struct vc4_hdmi_connector, base);
90}
91
92#define HDMI_REG(reg) { reg, #reg }
93static const struct {
94 u32 reg;
95 const char *name;
96} hdmi_regs[] = {
97 HDMI_REG(VC4_HDMI_CORE_REV),
98 HDMI_REG(VC4_HDMI_SW_RESET_CONTROL),
99 HDMI_REG(VC4_HDMI_HOTPLUG_INT),
100 HDMI_REG(VC4_HDMI_HOTPLUG),
Eric Anholt936f1a52016-02-12 15:16:56 -0800101 HDMI_REG(VC4_HDMI_RAM_PACKET_CONFIG),
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800102 HDMI_REG(VC4_HDMI_HORZA),
103 HDMI_REG(VC4_HDMI_HORZB),
104 HDMI_REG(VC4_HDMI_FIFO_CTL),
105 HDMI_REG(VC4_HDMI_SCHEDULER_CONTROL),
106 HDMI_REG(VC4_HDMI_VERTA0),
107 HDMI_REG(VC4_HDMI_VERTA1),
108 HDMI_REG(VC4_HDMI_VERTB0),
109 HDMI_REG(VC4_HDMI_VERTB1),
110 HDMI_REG(VC4_HDMI_TX_PHY_RESET_CTL),
111};
112
113static const struct {
114 u32 reg;
115 const char *name;
116} hd_regs[] = {
117 HDMI_REG(VC4_HD_M_CTL),
118 HDMI_REG(VC4_HD_MAI_CTL),
119 HDMI_REG(VC4_HD_VID_CTL),
120 HDMI_REG(VC4_HD_CSC_CTL),
121 HDMI_REG(VC4_HD_FRAME_COUNT),
122};
123
124#ifdef CONFIG_DEBUG_FS
125int vc4_hdmi_debugfs_regs(struct seq_file *m, void *unused)
126{
127 struct drm_info_node *node = (struct drm_info_node *)m->private;
128 struct drm_device *dev = node->minor->dev;
129 struct vc4_dev *vc4 = to_vc4_dev(dev);
130 int i;
131
132 for (i = 0; i < ARRAY_SIZE(hdmi_regs); i++) {
133 seq_printf(m, "%s (0x%04x): 0x%08x\n",
134 hdmi_regs[i].name, hdmi_regs[i].reg,
135 HDMI_READ(hdmi_regs[i].reg));
136 }
137
138 for (i = 0; i < ARRAY_SIZE(hd_regs); i++) {
139 seq_printf(m, "%s (0x%04x): 0x%08x\n",
140 hd_regs[i].name, hd_regs[i].reg,
141 HD_READ(hd_regs[i].reg));
142 }
143
144 return 0;
145}
146#endif /* CONFIG_DEBUG_FS */
147
148static void vc4_hdmi_dump_regs(struct drm_device *dev)
149{
150 struct vc4_dev *vc4 = to_vc4_dev(dev);
151 int i;
152
153 for (i = 0; i < ARRAY_SIZE(hdmi_regs); i++) {
154 DRM_INFO("0x%04x (%s): 0x%08x\n",
155 hdmi_regs[i].reg, hdmi_regs[i].name,
156 HDMI_READ(hdmi_regs[i].reg));
157 }
158 for (i = 0; i < ARRAY_SIZE(hd_regs); i++) {
159 DRM_INFO("0x%04x (%s): 0x%08x\n",
160 hd_regs[i].reg, hd_regs[i].name,
161 HD_READ(hd_regs[i].reg));
162 }
163}
164
165static enum drm_connector_status
166vc4_hdmi_connector_detect(struct drm_connector *connector, bool force)
167{
168 struct drm_device *dev = connector->dev;
169 struct vc4_dev *vc4 = to_vc4_dev(dev);
170
171 if (vc4->hdmi->hpd_gpio) {
Eric Anholt0b06e0a2016-02-29 17:53:01 -0800172 if (gpio_get_value_cansleep(vc4->hdmi->hpd_gpio) ^
173 vc4->hdmi->hpd_active_low)
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800174 return connector_status_connected;
175 else
176 return connector_status_disconnected;
177 }
178
Eric Anholt9d44abb2016-09-14 19:21:29 +0100179 if (drm_probe_ddc(vc4->hdmi->ddc))
180 return connector_status_connected;
181
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800182 if (HDMI_READ(VC4_HDMI_HOTPLUG) & VC4_HDMI_HOTPLUG_CONNECTED)
183 return connector_status_connected;
184 else
185 return connector_status_disconnected;
186}
187
188static void vc4_hdmi_connector_destroy(struct drm_connector *connector)
189{
190 drm_connector_unregister(connector);
191 drm_connector_cleanup(connector);
192}
193
194static int vc4_hdmi_connector_get_modes(struct drm_connector *connector)
195{
196 struct vc4_hdmi_connector *vc4_connector =
197 to_vc4_hdmi_connector(connector);
198 struct drm_encoder *encoder = vc4_connector->encoder;
199 struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
200 struct drm_device *dev = connector->dev;
201 struct vc4_dev *vc4 = to_vc4_dev(dev);
202 int ret = 0;
203 struct edid *edid;
204
205 edid = drm_get_edid(connector, vc4->hdmi->ddc);
206 if (!edid)
207 return -ENODEV;
208
209 vc4_encoder->hdmi_monitor = drm_detect_hdmi_monitor(edid);
Eric Anholt21317b32016-09-29 15:34:43 -0700210
211 if (edid && edid->input & DRM_EDID_INPUT_DIGITAL) {
212 vc4_encoder->rgb_range_selectable =
213 drm_rgb_quant_range_selectable(edid);
214 }
215
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800216 drm_mode_connector_update_edid_property(connector, edid);
217 ret = drm_add_edid_modes(connector, edid);
218
219 return ret;
220}
221
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800222static const struct drm_connector_funcs vc4_hdmi_connector_funcs = {
223 .dpms = drm_atomic_helper_connector_dpms,
224 .detect = vc4_hdmi_connector_detect,
Eric Anholt682e62c2016-09-28 17:30:25 -0700225 .fill_modes = drm_helper_probe_single_connector_modes,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800226 .destroy = vc4_hdmi_connector_destroy,
227 .reset = drm_atomic_helper_connector_reset,
228 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
229 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
230};
231
232static const struct drm_connector_helper_funcs vc4_hdmi_connector_helper_funcs = {
233 .get_modes = vc4_hdmi_connector_get_modes,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800234};
235
236static struct drm_connector *vc4_hdmi_connector_init(struct drm_device *dev,
237 struct drm_encoder *encoder)
238{
239 struct drm_connector *connector = NULL;
240 struct vc4_hdmi_connector *hdmi_connector;
241 int ret = 0;
242
243 hdmi_connector = devm_kzalloc(dev->dev, sizeof(*hdmi_connector),
244 GFP_KERNEL);
245 if (!hdmi_connector) {
246 ret = -ENOMEM;
247 goto fail;
248 }
249 connector = &hdmi_connector->base;
250
251 hdmi_connector->encoder = encoder;
252
253 drm_connector_init(dev, connector, &vc4_hdmi_connector_funcs,
254 DRM_MODE_CONNECTOR_HDMIA);
255 drm_connector_helper_add(connector, &vc4_hdmi_connector_helper_funcs);
256
257 connector->polled = (DRM_CONNECTOR_POLL_CONNECT |
258 DRM_CONNECTOR_POLL_DISCONNECT);
259
Mario Kleineracc1be12016-07-19 20:58:58 +0200260 connector->interlace_allowed = 1;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800261 connector->doublescan_allowed = 0;
262
263 drm_mode_connector_attach_encoder(connector, encoder);
264
265 return connector;
266
267 fail:
268 if (connector)
269 vc4_hdmi_connector_destroy(connector);
270
271 return ERR_PTR(ret);
272}
273
274static void vc4_hdmi_encoder_destroy(struct drm_encoder *encoder)
275{
276 drm_encoder_cleanup(encoder);
277}
278
279static const struct drm_encoder_funcs vc4_hdmi_encoder_funcs = {
280 .destroy = vc4_hdmi_encoder_destroy,
281};
282
Eric Anholt21317b32016-09-29 15:34:43 -0700283static int vc4_hdmi_stop_packet(struct drm_encoder *encoder,
284 enum hdmi_infoframe_type type)
285{
286 struct drm_device *dev = encoder->dev;
287 struct vc4_dev *vc4 = to_vc4_dev(dev);
288 u32 packet_id = type - 0x80;
289
290 HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG,
291 HDMI_READ(VC4_HDMI_RAM_PACKET_CONFIG) & ~BIT(packet_id));
292
293 return wait_for(!(HDMI_READ(VC4_HDMI_RAM_PACKET_STATUS) &
294 BIT(packet_id)), 100);
295}
296
297static void vc4_hdmi_write_infoframe(struct drm_encoder *encoder,
298 union hdmi_infoframe *frame)
299{
300 struct drm_device *dev = encoder->dev;
301 struct vc4_dev *vc4 = to_vc4_dev(dev);
302 u32 packet_id = frame->any.type - 0x80;
303 u32 packet_reg = VC4_HDMI_GCP_0 + VC4_HDMI_PACKET_STRIDE * packet_id;
304 uint8_t buffer[VC4_HDMI_PACKET_STRIDE];
305 ssize_t len, i;
306 int ret;
307
308 WARN_ONCE(!(HDMI_READ(VC4_HDMI_RAM_PACKET_CONFIG) &
309 VC4_HDMI_RAM_PACKET_ENABLE),
310 "Packet RAM has to be on to store the packet.");
311
312 len = hdmi_infoframe_pack(frame, buffer, sizeof(buffer));
313 if (len < 0)
314 return;
315
316 ret = vc4_hdmi_stop_packet(encoder, frame->any.type);
317 if (ret) {
318 DRM_ERROR("Failed to wait for infoframe to go idle: %d\n", ret);
319 return;
320 }
321
322 for (i = 0; i < len; i += 7) {
323 HDMI_WRITE(packet_reg,
324 buffer[i + 0] << 0 |
325 buffer[i + 1] << 8 |
326 buffer[i + 2] << 16);
327 packet_reg += 4;
328
329 HDMI_WRITE(packet_reg,
330 buffer[i + 3] << 0 |
331 buffer[i + 4] << 8 |
332 buffer[i + 5] << 16 |
333 buffer[i + 6] << 24);
334 packet_reg += 4;
335 }
336
337 HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG,
338 HDMI_READ(VC4_HDMI_RAM_PACKET_CONFIG) | BIT(packet_id));
339 ret = wait_for((HDMI_READ(VC4_HDMI_RAM_PACKET_STATUS) &
340 BIT(packet_id)), 100);
341 if (ret)
342 DRM_ERROR("Failed to wait for infoframe to start: %d\n", ret);
343}
344
345static void vc4_hdmi_set_avi_infoframe(struct drm_encoder *encoder)
346{
347 struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
348 struct drm_crtc *crtc = encoder->crtc;
349 const struct drm_display_mode *mode = &crtc->state->adjusted_mode;
350 union hdmi_infoframe frame;
351 int ret;
352
353 ret = drm_hdmi_avi_infoframe_from_display_mode(&frame.avi, mode);
354 if (ret < 0) {
355 DRM_ERROR("couldn't fill AVI infoframe\n");
356 return;
357 }
358
359 if (vc4_encoder->rgb_range_selectable) {
360 if (vc4_encoder->limited_rgb_range) {
361 frame.avi.quantization_range =
362 HDMI_QUANTIZATION_RANGE_LIMITED;
363 } else {
364 frame.avi.quantization_range =
365 HDMI_QUANTIZATION_RANGE_FULL;
366 }
367 }
368
369 vc4_hdmi_write_infoframe(encoder, &frame);
370}
371
372static void vc4_hdmi_set_spd_infoframe(struct drm_encoder *encoder)
373{
374 union hdmi_infoframe frame;
375 int ret;
376
377 ret = hdmi_spd_infoframe_init(&frame.spd, "Broadcom", "Videocore");
378 if (ret < 0) {
379 DRM_ERROR("couldn't fill SPD infoframe\n");
380 return;
381 }
382
383 frame.spd.sdi = HDMI_SPD_SDI_PC;
384
385 vc4_hdmi_write_infoframe(encoder, &frame);
386}
387
388static void vc4_hdmi_set_infoframes(struct drm_encoder *encoder)
389{
390 vc4_hdmi_set_avi_infoframe(encoder);
391 vc4_hdmi_set_spd_infoframe(encoder);
392}
393
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800394static void vc4_hdmi_encoder_mode_set(struct drm_encoder *encoder,
395 struct drm_display_mode *unadjusted_mode,
396 struct drm_display_mode *mode)
397{
Eric Anholt6e1cbba2016-09-16 10:59:45 +0100398 struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800399 struct drm_device *dev = encoder->dev;
400 struct vc4_dev *vc4 = to_vc4_dev(dev);
401 bool debug_dump_regs = false;
402 bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
403 bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
Eric Anholt682e62c2016-09-28 17:30:25 -0700404 bool interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE;
Eric Anholtdfccd932016-09-29 15:34:44 -0700405 u32 pixel_rep = (mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1;
Eric Anholt682e62c2016-09-28 17:30:25 -0700406 u32 verta = (VC4_SET_FIELD(mode->crtc_vsync_end - mode->crtc_vsync_start,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800407 VC4_HDMI_VERTA_VSP) |
Eric Anholt682e62c2016-09-28 17:30:25 -0700408 VC4_SET_FIELD(mode->crtc_vsync_start - mode->crtc_vdisplay,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800409 VC4_HDMI_VERTA_VFP) |
Eric Anholt682e62c2016-09-28 17:30:25 -0700410 VC4_SET_FIELD(mode->crtc_vdisplay, VC4_HDMI_VERTA_VAL));
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800411 u32 vertb = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) |
Eric Anholt682e62c2016-09-28 17:30:25 -0700412 VC4_SET_FIELD(mode->crtc_vtotal - mode->crtc_vsync_end,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800413 VC4_HDMI_VERTB_VBP));
Eric Anholt682e62c2016-09-28 17:30:25 -0700414 u32 vertb_even = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) |
415 VC4_SET_FIELD(mode->crtc_vtotal -
416 mode->crtc_vsync_end -
417 interlaced,
418 VC4_HDMI_VERTB_VBP));
Eric Anholt6e1cbba2016-09-16 10:59:45 +0100419 u32 csc_ctl;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800420
421 if (debug_dump_regs) {
422 DRM_INFO("HDMI regs before:\n");
423 vc4_hdmi_dump_regs(dev);
424 }
425
426 HD_WRITE(VC4_HD_VID_CTL, 0);
427
Eric Anholtdfccd932016-09-29 15:34:44 -0700428 clk_set_rate(vc4->hdmi->pixel_clock, mode->clock * 1000 *
429 ((mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1));
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800430
431 HDMI_WRITE(VC4_HDMI_SCHEDULER_CONTROL,
432 HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) |
433 VC4_HDMI_SCHEDULER_CONTROL_MANUAL_FORMAT |
434 VC4_HDMI_SCHEDULER_CONTROL_IGNORE_VSYNC_PREDICTS);
435
436 HDMI_WRITE(VC4_HDMI_HORZA,
437 (vsync_pos ? VC4_HDMI_HORZA_VPOS : 0) |
438 (hsync_pos ? VC4_HDMI_HORZA_HPOS : 0) |
Eric Anholtdfccd932016-09-29 15:34:44 -0700439 VC4_SET_FIELD(mode->hdisplay * pixel_rep,
440 VC4_HDMI_HORZA_HAP));
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800441
442 HDMI_WRITE(VC4_HDMI_HORZB,
Eric Anholtdfccd932016-09-29 15:34:44 -0700443 VC4_SET_FIELD((mode->htotal -
444 mode->hsync_end) * pixel_rep,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800445 VC4_HDMI_HORZB_HBP) |
Eric Anholtdfccd932016-09-29 15:34:44 -0700446 VC4_SET_FIELD((mode->hsync_end -
447 mode->hsync_start) * pixel_rep,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800448 VC4_HDMI_HORZB_HSP) |
Eric Anholtdfccd932016-09-29 15:34:44 -0700449 VC4_SET_FIELD((mode->hsync_start -
450 mode->hdisplay) * pixel_rep,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800451 VC4_HDMI_HORZB_HFP));
452
453 HDMI_WRITE(VC4_HDMI_VERTA0, verta);
454 HDMI_WRITE(VC4_HDMI_VERTA1, verta);
455
Eric Anholt682e62c2016-09-28 17:30:25 -0700456 HDMI_WRITE(VC4_HDMI_VERTB0, vertb_even);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800457 HDMI_WRITE(VC4_HDMI_VERTB1, vertb);
458
459 HD_WRITE(VC4_HD_VID_CTL,
460 (vsync_pos ? 0 : VC4_HD_VID_CTL_VSYNC_LOW) |
461 (hsync_pos ? 0 : VC4_HD_VID_CTL_HSYNC_LOW));
462
Eric Anholt6e1cbba2016-09-16 10:59:45 +0100463 csc_ctl = VC4_SET_FIELD(VC4_HD_CSC_CTL_ORDER_BGR,
464 VC4_HD_CSC_CTL_ORDER);
465
Ville Syrjäläc8127cf02017-01-11 16:18:35 +0200466 if (vc4_encoder->hdmi_monitor &&
467 drm_default_rgb_quant_range(mode) ==
468 HDMI_QUANTIZATION_RANGE_LIMITED) {
Eric Anholt6e1cbba2016-09-16 10:59:45 +0100469 /* CEA VICs other than #1 requre limited range RGB
Eric Anholt21317b32016-09-29 15:34:43 -0700470 * output unless overridden by an AVI infoframe.
471 * Apply a colorspace conversion to squash 0-255 down
472 * to 16-235. The matrix here is:
Eric Anholt6e1cbba2016-09-16 10:59:45 +0100473 *
474 * [ 0 0 0.8594 16]
475 * [ 0 0.8594 0 16]
476 * [ 0.8594 0 0 16]
477 * [ 0 0 0 1]
478 */
479 csc_ctl |= VC4_HD_CSC_CTL_ENABLE;
480 csc_ctl |= VC4_HD_CSC_CTL_RGB2YCC;
481 csc_ctl |= VC4_SET_FIELD(VC4_HD_CSC_CTL_MODE_CUSTOM,
482 VC4_HD_CSC_CTL_MODE);
483
484 HD_WRITE(VC4_HD_CSC_12_11, (0x000 << 16) | 0x000);
485 HD_WRITE(VC4_HD_CSC_14_13, (0x100 << 16) | 0x6e0);
486 HD_WRITE(VC4_HD_CSC_22_21, (0x6e0 << 16) | 0x000);
487 HD_WRITE(VC4_HD_CSC_24_23, (0x100 << 16) | 0x000);
488 HD_WRITE(VC4_HD_CSC_32_31, (0x000 << 16) | 0x6e0);
489 HD_WRITE(VC4_HD_CSC_34_33, (0x100 << 16) | 0x000);
Eric Anholt21317b32016-09-29 15:34:43 -0700490 vc4_encoder->limited_rgb_range = true;
491 } else {
492 vc4_encoder->limited_rgb_range = false;
Eric Anholt6e1cbba2016-09-16 10:59:45 +0100493 }
494
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800495 /* The RGB order applies even when CSC is disabled. */
Eric Anholt6e1cbba2016-09-16 10:59:45 +0100496 HD_WRITE(VC4_HD_CSC_CTL, csc_ctl);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800497
498 HDMI_WRITE(VC4_HDMI_FIFO_CTL, VC4_HDMI_FIFO_CTL_MASTER_SLAVE_N);
499
500 if (debug_dump_regs) {
501 DRM_INFO("HDMI regs after:\n");
502 vc4_hdmi_dump_regs(dev);
503 }
504}
505
506static void vc4_hdmi_encoder_disable(struct drm_encoder *encoder)
507{
508 struct drm_device *dev = encoder->dev;
509 struct vc4_dev *vc4 = to_vc4_dev(dev);
510
Eric Anholt21317b32016-09-29 15:34:43 -0700511 HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG, 0);
512
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800513 HDMI_WRITE(VC4_HDMI_TX_PHY_RESET_CTL, 0xf << 16);
514 HD_WRITE(VC4_HD_VID_CTL,
515 HD_READ(VC4_HD_VID_CTL) & ~VC4_HD_VID_CTL_ENABLE);
516}
517
518static void vc4_hdmi_encoder_enable(struct drm_encoder *encoder)
519{
520 struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
521 struct drm_device *dev = encoder->dev;
522 struct vc4_dev *vc4 = to_vc4_dev(dev);
523 int ret;
524
525 HDMI_WRITE(VC4_HDMI_TX_PHY_RESET_CTL, 0);
526
527 HD_WRITE(VC4_HD_VID_CTL,
528 HD_READ(VC4_HD_VID_CTL) |
529 VC4_HD_VID_CTL_ENABLE |
530 VC4_HD_VID_CTL_UNDERFLOW_ENABLE |
531 VC4_HD_VID_CTL_FRAME_COUNTER_RESET);
532
533 if (vc4_encoder->hdmi_monitor) {
534 HDMI_WRITE(VC4_HDMI_SCHEDULER_CONTROL,
535 HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) |
536 VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);
537
538 ret = wait_for(HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) &
Eric Anholt2b29bf12016-09-28 17:21:05 -0700539 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE, 1000);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800540 WARN_ONCE(ret, "Timeout waiting for "
541 "VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
542 } else {
543 HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG,
544 HDMI_READ(VC4_HDMI_RAM_PACKET_CONFIG) &
545 ~(VC4_HDMI_RAM_PACKET_ENABLE));
546 HDMI_WRITE(VC4_HDMI_SCHEDULER_CONTROL,
547 HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) &
548 ~VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);
549
550 ret = wait_for(!(HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) &
Eric Anholt2b29bf12016-09-28 17:21:05 -0700551 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE), 1000);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800552 WARN_ONCE(ret, "Timeout waiting for "
553 "!VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
554 }
555
556 if (vc4_encoder->hdmi_monitor) {
557 u32 drift;
558
559 WARN_ON(!(HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) &
560 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE));
561 HDMI_WRITE(VC4_HDMI_SCHEDULER_CONTROL,
562 HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) |
563 VC4_HDMI_SCHEDULER_CONTROL_VERT_ALWAYS_KEEPOUT);
564
Eric Anholt21317b32016-09-29 15:34:43 -0700565 HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG,
566 VC4_HDMI_RAM_PACKET_ENABLE);
567
568 vc4_hdmi_set_infoframes(encoder);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800569
570 drift = HDMI_READ(VC4_HDMI_FIFO_CTL);
571 drift &= VC4_HDMI_FIFO_VALID_WRITE_MASK;
572
573 HDMI_WRITE(VC4_HDMI_FIFO_CTL,
574 drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
575 HDMI_WRITE(VC4_HDMI_FIFO_CTL,
576 drift | VC4_HDMI_FIFO_CTL_RECENTER);
577 udelay(1000);
578 HDMI_WRITE(VC4_HDMI_FIFO_CTL,
579 drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
580 HDMI_WRITE(VC4_HDMI_FIFO_CTL,
581 drift | VC4_HDMI_FIFO_CTL_RECENTER);
582
583 ret = wait_for(HDMI_READ(VC4_HDMI_FIFO_CTL) &
584 VC4_HDMI_FIFO_CTL_RECENTER_DONE, 1);
585 WARN_ONCE(ret, "Timeout waiting for "
586 "VC4_HDMI_FIFO_CTL_RECENTER_DONE");
587 }
588}
589
590static const struct drm_encoder_helper_funcs vc4_hdmi_encoder_helper_funcs = {
591 .mode_set = vc4_hdmi_encoder_mode_set,
592 .disable = vc4_hdmi_encoder_disable,
593 .enable = vc4_hdmi_encoder_enable,
594};
595
596static int vc4_hdmi_bind(struct device *dev, struct device *master, void *data)
597{
598 struct platform_device *pdev = to_platform_device(dev);
599 struct drm_device *drm = dev_get_drvdata(master);
600 struct vc4_dev *vc4 = drm->dev_private;
601 struct vc4_hdmi *hdmi;
602 struct vc4_hdmi_encoder *vc4_hdmi_encoder;
603 struct device_node *ddc_node;
604 u32 value;
605 int ret;
606
607 hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL);
608 if (!hdmi)
609 return -ENOMEM;
610
611 vc4_hdmi_encoder = devm_kzalloc(dev, sizeof(*vc4_hdmi_encoder),
612 GFP_KERNEL);
613 if (!vc4_hdmi_encoder)
614 return -ENOMEM;
615 vc4_hdmi_encoder->base.type = VC4_ENCODER_TYPE_HDMI;
616 hdmi->encoder = &vc4_hdmi_encoder->base.base;
617
618 hdmi->pdev = pdev;
619 hdmi->hdmicore_regs = vc4_ioremap_regs(pdev, 0);
620 if (IS_ERR(hdmi->hdmicore_regs))
621 return PTR_ERR(hdmi->hdmicore_regs);
622
623 hdmi->hd_regs = vc4_ioremap_regs(pdev, 1);
624 if (IS_ERR(hdmi->hd_regs))
625 return PTR_ERR(hdmi->hd_regs);
626
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800627 hdmi->pixel_clock = devm_clk_get(dev, "pixel");
628 if (IS_ERR(hdmi->pixel_clock)) {
629 DRM_ERROR("Failed to get pixel clock\n");
630 return PTR_ERR(hdmi->pixel_clock);
631 }
632 hdmi->hsm_clock = devm_clk_get(dev, "hdmi");
633 if (IS_ERR(hdmi->hsm_clock)) {
634 DRM_ERROR("Failed to get HDMI state machine clock\n");
635 return PTR_ERR(hdmi->hsm_clock);
636 }
637
Peter Chen027a6972016-07-05 10:04:54 +0800638 ddc_node = of_parse_phandle(dev->of_node, "ddc", 0);
639 if (!ddc_node) {
640 DRM_ERROR("Failed to find ddc node in device tree\n");
641 return -ENODEV;
642 }
643
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800644 hdmi->ddc = of_find_i2c_adapter_by_node(ddc_node);
Peter Chen027a6972016-07-05 10:04:54 +0800645 of_node_put(ddc_node);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800646 if (!hdmi->ddc) {
647 DRM_DEBUG("Failed to get ddc i2c adapter by node\n");
648 return -EPROBE_DEFER;
649 }
650
651 /* Enable the clocks at startup. We can't quite recover from
652 * turning off the pixel clock during disable/enables yet, so
653 * it's always running.
654 */
655 ret = clk_prepare_enable(hdmi->pixel_clock);
656 if (ret) {
657 DRM_ERROR("Failed to turn on pixel clock: %d\n", ret);
658 goto err_put_i2c;
659 }
660
Eric Anholt851479a2016-02-12 14:15:14 -0800661 /* This is the rate that is set by the firmware. The number
662 * needs to be a bit higher than the pixel clock rate
663 * (generally 148.5Mhz).
664 */
665 ret = clk_set_rate(hdmi->hsm_clock, 163682864);
666 if (ret) {
667 DRM_ERROR("Failed to set HSM clock rate: %d\n", ret);
668 goto err_unprepare_pix;
669 }
670
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800671 ret = clk_prepare_enable(hdmi->hsm_clock);
672 if (ret) {
673 DRM_ERROR("Failed to turn on HDMI state machine clock: %d\n",
674 ret);
675 goto err_unprepare_pix;
676 }
677
678 /* Only use the GPIO HPD pin if present in the DT, otherwise
679 * we'll use the HDMI core's register.
680 */
681 if (of_find_property(dev->of_node, "hpd-gpios", &value)) {
Eric Anholt0b06e0a2016-02-29 17:53:01 -0800682 enum of_gpio_flags hpd_gpio_flags;
683
684 hdmi->hpd_gpio = of_get_named_gpio_flags(dev->of_node,
685 "hpd-gpios", 0,
686 &hpd_gpio_flags);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800687 if (hdmi->hpd_gpio < 0) {
688 ret = hdmi->hpd_gpio;
689 goto err_unprepare_hsm;
690 }
Eric Anholt0b06e0a2016-02-29 17:53:01 -0800691
692 hdmi->hpd_active_low = hpd_gpio_flags & OF_GPIO_ACTIVE_LOW;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800693 }
694
695 vc4->hdmi = hdmi;
696
697 /* HDMI core must be enabled. */
Eric Anholt851479a2016-02-12 14:15:14 -0800698 if (!(HD_READ(VC4_HD_M_CTL) & VC4_HD_M_ENABLE)) {
699 HD_WRITE(VC4_HD_M_CTL, VC4_HD_M_SW_RST);
700 udelay(1);
701 HD_WRITE(VC4_HD_M_CTL, 0);
702
703 HD_WRITE(VC4_HD_M_CTL, VC4_HD_M_ENABLE);
704
705 HDMI_WRITE(VC4_HDMI_SW_RESET_CONTROL,
706 VC4_HDMI_SW_RESET_HDMI |
707 VC4_HDMI_SW_RESET_FORMAT_DETECT);
708
709 HDMI_WRITE(VC4_HDMI_SW_RESET_CONTROL, 0);
710
711 /* PHY should be in reset, like
712 * vc4_hdmi_encoder_disable() does.
713 */
714 HDMI_WRITE(VC4_HDMI_TX_PHY_RESET_CTL, 0xf << 16);
715 }
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800716
717 drm_encoder_init(drm, hdmi->encoder, &vc4_hdmi_encoder_funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +0200718 DRM_MODE_ENCODER_TMDS, NULL);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800719 drm_encoder_helper_add(hdmi->encoder, &vc4_hdmi_encoder_helper_funcs);
720
721 hdmi->connector = vc4_hdmi_connector_init(drm, hdmi->encoder);
722 if (IS_ERR(hdmi->connector)) {
723 ret = PTR_ERR(hdmi->connector);
724 goto err_destroy_encoder;
725 }
726
727 return 0;
728
729err_destroy_encoder:
730 vc4_hdmi_encoder_destroy(hdmi->encoder);
731err_unprepare_hsm:
732 clk_disable_unprepare(hdmi->hsm_clock);
733err_unprepare_pix:
734 clk_disable_unprepare(hdmi->pixel_clock);
735err_put_i2c:
Eric Anholt58839802016-04-04 14:25:59 -0700736 put_device(&hdmi->ddc->dev);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800737
738 return ret;
739}
740
741static void vc4_hdmi_unbind(struct device *dev, struct device *master,
742 void *data)
743{
744 struct drm_device *drm = dev_get_drvdata(master);
745 struct vc4_dev *vc4 = drm->dev_private;
746 struct vc4_hdmi *hdmi = vc4->hdmi;
747
748 vc4_hdmi_connector_destroy(hdmi->connector);
749 vc4_hdmi_encoder_destroy(hdmi->encoder);
750
751 clk_disable_unprepare(hdmi->pixel_clock);
752 clk_disable_unprepare(hdmi->hsm_clock);
753 put_device(&hdmi->ddc->dev);
754
755 vc4->hdmi = NULL;
756}
757
758static const struct component_ops vc4_hdmi_ops = {
759 .bind = vc4_hdmi_bind,
760 .unbind = vc4_hdmi_unbind,
761};
762
763static int vc4_hdmi_dev_probe(struct platform_device *pdev)
764{
765 return component_add(&pdev->dev, &vc4_hdmi_ops);
766}
767
768static int vc4_hdmi_dev_remove(struct platform_device *pdev)
769{
770 component_del(&pdev->dev, &vc4_hdmi_ops);
771 return 0;
772}
773
774static const struct of_device_id vc4_hdmi_dt_match[] = {
775 { .compatible = "brcm,bcm2835-hdmi" },
776 {}
777};
778
779struct platform_driver vc4_hdmi_driver = {
780 .probe = vc4_hdmi_dev_probe,
781 .remove = vc4_hdmi_dev_remove,
782 .driver = {
783 .name = "vc4_hdmi",
784 .of_match_table = vc4_hdmi_dt_match,
785 },
786};