blob: d6b54b905beeec33eee8a763969ffa9568cfa745 [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;
405 u32 verta = (VC4_SET_FIELD(mode->crtc_vsync_end - mode->crtc_vsync_start,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800406 VC4_HDMI_VERTA_VSP) |
Eric Anholt682e62c2016-09-28 17:30:25 -0700407 VC4_SET_FIELD(mode->crtc_vsync_start - mode->crtc_vdisplay,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800408 VC4_HDMI_VERTA_VFP) |
Eric Anholt682e62c2016-09-28 17:30:25 -0700409 VC4_SET_FIELD(mode->crtc_vdisplay, VC4_HDMI_VERTA_VAL));
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800410 u32 vertb = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) |
Eric Anholt682e62c2016-09-28 17:30:25 -0700411 VC4_SET_FIELD(mode->crtc_vtotal - mode->crtc_vsync_end,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800412 VC4_HDMI_VERTB_VBP));
Eric Anholt682e62c2016-09-28 17:30:25 -0700413 u32 vertb_even = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) |
414 VC4_SET_FIELD(mode->crtc_vtotal -
415 mode->crtc_vsync_end -
416 interlaced,
417 VC4_HDMI_VERTB_VBP));
Eric Anholt6e1cbba2016-09-16 10:59:45 +0100418 u32 csc_ctl;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800419
420 if (debug_dump_regs) {
421 DRM_INFO("HDMI regs before:\n");
422 vc4_hdmi_dump_regs(dev);
423 }
424
425 HD_WRITE(VC4_HD_VID_CTL, 0);
426
427 clk_set_rate(vc4->hdmi->pixel_clock, mode->clock * 1000);
428
429 HDMI_WRITE(VC4_HDMI_SCHEDULER_CONTROL,
430 HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) |
431 VC4_HDMI_SCHEDULER_CONTROL_MANUAL_FORMAT |
432 VC4_HDMI_SCHEDULER_CONTROL_IGNORE_VSYNC_PREDICTS);
433
434 HDMI_WRITE(VC4_HDMI_HORZA,
435 (vsync_pos ? VC4_HDMI_HORZA_VPOS : 0) |
436 (hsync_pos ? VC4_HDMI_HORZA_HPOS : 0) |
437 VC4_SET_FIELD(mode->hdisplay, VC4_HDMI_HORZA_HAP));
438
439 HDMI_WRITE(VC4_HDMI_HORZB,
440 VC4_SET_FIELD(mode->htotal - mode->hsync_end,
441 VC4_HDMI_HORZB_HBP) |
442 VC4_SET_FIELD(mode->hsync_end - mode->hsync_start,
443 VC4_HDMI_HORZB_HSP) |
444 VC4_SET_FIELD(mode->hsync_start - mode->hdisplay,
445 VC4_HDMI_HORZB_HFP));
446
447 HDMI_WRITE(VC4_HDMI_VERTA0, verta);
448 HDMI_WRITE(VC4_HDMI_VERTA1, verta);
449
Eric Anholt682e62c2016-09-28 17:30:25 -0700450 HDMI_WRITE(VC4_HDMI_VERTB0, vertb_even);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800451 HDMI_WRITE(VC4_HDMI_VERTB1, vertb);
452
453 HD_WRITE(VC4_HD_VID_CTL,
454 (vsync_pos ? 0 : VC4_HD_VID_CTL_VSYNC_LOW) |
455 (hsync_pos ? 0 : VC4_HD_VID_CTL_HSYNC_LOW));
456
Eric Anholt6e1cbba2016-09-16 10:59:45 +0100457 csc_ctl = VC4_SET_FIELD(VC4_HD_CSC_CTL_ORDER_BGR,
458 VC4_HD_CSC_CTL_ORDER);
459
460 if (vc4_encoder->hdmi_monitor && drm_match_cea_mode(mode) > 1) {
461 /* CEA VICs other than #1 requre limited range RGB
Eric Anholt21317b32016-09-29 15:34:43 -0700462 * output unless overridden by an AVI infoframe.
463 * Apply a colorspace conversion to squash 0-255 down
464 * to 16-235. The matrix here is:
Eric Anholt6e1cbba2016-09-16 10:59:45 +0100465 *
466 * [ 0 0 0.8594 16]
467 * [ 0 0.8594 0 16]
468 * [ 0.8594 0 0 16]
469 * [ 0 0 0 1]
470 */
471 csc_ctl |= VC4_HD_CSC_CTL_ENABLE;
472 csc_ctl |= VC4_HD_CSC_CTL_RGB2YCC;
473 csc_ctl |= VC4_SET_FIELD(VC4_HD_CSC_CTL_MODE_CUSTOM,
474 VC4_HD_CSC_CTL_MODE);
475
476 HD_WRITE(VC4_HD_CSC_12_11, (0x000 << 16) | 0x000);
477 HD_WRITE(VC4_HD_CSC_14_13, (0x100 << 16) | 0x6e0);
478 HD_WRITE(VC4_HD_CSC_22_21, (0x6e0 << 16) | 0x000);
479 HD_WRITE(VC4_HD_CSC_24_23, (0x100 << 16) | 0x000);
480 HD_WRITE(VC4_HD_CSC_32_31, (0x000 << 16) | 0x6e0);
481 HD_WRITE(VC4_HD_CSC_34_33, (0x100 << 16) | 0x000);
Eric Anholt21317b32016-09-29 15:34:43 -0700482 vc4_encoder->limited_rgb_range = true;
483 } else {
484 vc4_encoder->limited_rgb_range = false;
Eric Anholt6e1cbba2016-09-16 10:59:45 +0100485 }
486
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800487 /* The RGB order applies even when CSC is disabled. */
Eric Anholt6e1cbba2016-09-16 10:59:45 +0100488 HD_WRITE(VC4_HD_CSC_CTL, csc_ctl);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800489
490 HDMI_WRITE(VC4_HDMI_FIFO_CTL, VC4_HDMI_FIFO_CTL_MASTER_SLAVE_N);
491
492 if (debug_dump_regs) {
493 DRM_INFO("HDMI regs after:\n");
494 vc4_hdmi_dump_regs(dev);
495 }
496}
497
498static void vc4_hdmi_encoder_disable(struct drm_encoder *encoder)
499{
500 struct drm_device *dev = encoder->dev;
501 struct vc4_dev *vc4 = to_vc4_dev(dev);
502
Eric Anholt21317b32016-09-29 15:34:43 -0700503 HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG, 0);
504
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800505 HDMI_WRITE(VC4_HDMI_TX_PHY_RESET_CTL, 0xf << 16);
506 HD_WRITE(VC4_HD_VID_CTL,
507 HD_READ(VC4_HD_VID_CTL) & ~VC4_HD_VID_CTL_ENABLE);
508}
509
510static void vc4_hdmi_encoder_enable(struct drm_encoder *encoder)
511{
512 struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
513 struct drm_device *dev = encoder->dev;
514 struct vc4_dev *vc4 = to_vc4_dev(dev);
515 int ret;
516
517 HDMI_WRITE(VC4_HDMI_TX_PHY_RESET_CTL, 0);
518
519 HD_WRITE(VC4_HD_VID_CTL,
520 HD_READ(VC4_HD_VID_CTL) |
521 VC4_HD_VID_CTL_ENABLE |
522 VC4_HD_VID_CTL_UNDERFLOW_ENABLE |
523 VC4_HD_VID_CTL_FRAME_COUNTER_RESET);
524
525 if (vc4_encoder->hdmi_monitor) {
526 HDMI_WRITE(VC4_HDMI_SCHEDULER_CONTROL,
527 HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) |
528 VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);
529
530 ret = wait_for(HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) &
Eric Anholt2b29bf12016-09-28 17:21:05 -0700531 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE, 1000);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800532 WARN_ONCE(ret, "Timeout waiting for "
533 "VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
534 } else {
535 HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG,
536 HDMI_READ(VC4_HDMI_RAM_PACKET_CONFIG) &
537 ~(VC4_HDMI_RAM_PACKET_ENABLE));
538 HDMI_WRITE(VC4_HDMI_SCHEDULER_CONTROL,
539 HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) &
540 ~VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);
541
542 ret = wait_for(!(HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) &
Eric Anholt2b29bf12016-09-28 17:21:05 -0700543 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE), 1000);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800544 WARN_ONCE(ret, "Timeout waiting for "
545 "!VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
546 }
547
548 if (vc4_encoder->hdmi_monitor) {
549 u32 drift;
550
551 WARN_ON(!(HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) &
552 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE));
553 HDMI_WRITE(VC4_HDMI_SCHEDULER_CONTROL,
554 HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) |
555 VC4_HDMI_SCHEDULER_CONTROL_VERT_ALWAYS_KEEPOUT);
556
Eric Anholt21317b32016-09-29 15:34:43 -0700557 HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG,
558 VC4_HDMI_RAM_PACKET_ENABLE);
559
560 vc4_hdmi_set_infoframes(encoder);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800561
562 drift = HDMI_READ(VC4_HDMI_FIFO_CTL);
563 drift &= VC4_HDMI_FIFO_VALID_WRITE_MASK;
564
565 HDMI_WRITE(VC4_HDMI_FIFO_CTL,
566 drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
567 HDMI_WRITE(VC4_HDMI_FIFO_CTL,
568 drift | VC4_HDMI_FIFO_CTL_RECENTER);
569 udelay(1000);
570 HDMI_WRITE(VC4_HDMI_FIFO_CTL,
571 drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
572 HDMI_WRITE(VC4_HDMI_FIFO_CTL,
573 drift | VC4_HDMI_FIFO_CTL_RECENTER);
574
575 ret = wait_for(HDMI_READ(VC4_HDMI_FIFO_CTL) &
576 VC4_HDMI_FIFO_CTL_RECENTER_DONE, 1);
577 WARN_ONCE(ret, "Timeout waiting for "
578 "VC4_HDMI_FIFO_CTL_RECENTER_DONE");
579 }
580}
581
582static const struct drm_encoder_helper_funcs vc4_hdmi_encoder_helper_funcs = {
583 .mode_set = vc4_hdmi_encoder_mode_set,
584 .disable = vc4_hdmi_encoder_disable,
585 .enable = vc4_hdmi_encoder_enable,
586};
587
588static int vc4_hdmi_bind(struct device *dev, struct device *master, void *data)
589{
590 struct platform_device *pdev = to_platform_device(dev);
591 struct drm_device *drm = dev_get_drvdata(master);
592 struct vc4_dev *vc4 = drm->dev_private;
593 struct vc4_hdmi *hdmi;
594 struct vc4_hdmi_encoder *vc4_hdmi_encoder;
595 struct device_node *ddc_node;
596 u32 value;
597 int ret;
598
599 hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL);
600 if (!hdmi)
601 return -ENOMEM;
602
603 vc4_hdmi_encoder = devm_kzalloc(dev, sizeof(*vc4_hdmi_encoder),
604 GFP_KERNEL);
605 if (!vc4_hdmi_encoder)
606 return -ENOMEM;
607 vc4_hdmi_encoder->base.type = VC4_ENCODER_TYPE_HDMI;
608 hdmi->encoder = &vc4_hdmi_encoder->base.base;
609
610 hdmi->pdev = pdev;
611 hdmi->hdmicore_regs = vc4_ioremap_regs(pdev, 0);
612 if (IS_ERR(hdmi->hdmicore_regs))
613 return PTR_ERR(hdmi->hdmicore_regs);
614
615 hdmi->hd_regs = vc4_ioremap_regs(pdev, 1);
616 if (IS_ERR(hdmi->hd_regs))
617 return PTR_ERR(hdmi->hd_regs);
618
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800619 hdmi->pixel_clock = devm_clk_get(dev, "pixel");
620 if (IS_ERR(hdmi->pixel_clock)) {
621 DRM_ERROR("Failed to get pixel clock\n");
622 return PTR_ERR(hdmi->pixel_clock);
623 }
624 hdmi->hsm_clock = devm_clk_get(dev, "hdmi");
625 if (IS_ERR(hdmi->hsm_clock)) {
626 DRM_ERROR("Failed to get HDMI state machine clock\n");
627 return PTR_ERR(hdmi->hsm_clock);
628 }
629
Peter Chen027a6972016-07-05 10:04:54 +0800630 ddc_node = of_parse_phandle(dev->of_node, "ddc", 0);
631 if (!ddc_node) {
632 DRM_ERROR("Failed to find ddc node in device tree\n");
633 return -ENODEV;
634 }
635
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800636 hdmi->ddc = of_find_i2c_adapter_by_node(ddc_node);
Peter Chen027a6972016-07-05 10:04:54 +0800637 of_node_put(ddc_node);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800638 if (!hdmi->ddc) {
639 DRM_DEBUG("Failed to get ddc i2c adapter by node\n");
640 return -EPROBE_DEFER;
641 }
642
643 /* Enable the clocks at startup. We can't quite recover from
644 * turning off the pixel clock during disable/enables yet, so
645 * it's always running.
646 */
647 ret = clk_prepare_enable(hdmi->pixel_clock);
648 if (ret) {
649 DRM_ERROR("Failed to turn on pixel clock: %d\n", ret);
650 goto err_put_i2c;
651 }
652
Eric Anholt851479a2016-02-12 14:15:14 -0800653 /* This is the rate that is set by the firmware. The number
654 * needs to be a bit higher than the pixel clock rate
655 * (generally 148.5Mhz).
656 */
657 ret = clk_set_rate(hdmi->hsm_clock, 163682864);
658 if (ret) {
659 DRM_ERROR("Failed to set HSM clock rate: %d\n", ret);
660 goto err_unprepare_pix;
661 }
662
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800663 ret = clk_prepare_enable(hdmi->hsm_clock);
664 if (ret) {
665 DRM_ERROR("Failed to turn on HDMI state machine clock: %d\n",
666 ret);
667 goto err_unprepare_pix;
668 }
669
670 /* Only use the GPIO HPD pin if present in the DT, otherwise
671 * we'll use the HDMI core's register.
672 */
673 if (of_find_property(dev->of_node, "hpd-gpios", &value)) {
Eric Anholt0b06e0a2016-02-29 17:53:01 -0800674 enum of_gpio_flags hpd_gpio_flags;
675
676 hdmi->hpd_gpio = of_get_named_gpio_flags(dev->of_node,
677 "hpd-gpios", 0,
678 &hpd_gpio_flags);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800679 if (hdmi->hpd_gpio < 0) {
680 ret = hdmi->hpd_gpio;
681 goto err_unprepare_hsm;
682 }
Eric Anholt0b06e0a2016-02-29 17:53:01 -0800683
684 hdmi->hpd_active_low = hpd_gpio_flags & OF_GPIO_ACTIVE_LOW;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800685 }
686
687 vc4->hdmi = hdmi;
688
689 /* HDMI core must be enabled. */
Eric Anholt851479a2016-02-12 14:15:14 -0800690 if (!(HD_READ(VC4_HD_M_CTL) & VC4_HD_M_ENABLE)) {
691 HD_WRITE(VC4_HD_M_CTL, VC4_HD_M_SW_RST);
692 udelay(1);
693 HD_WRITE(VC4_HD_M_CTL, 0);
694
695 HD_WRITE(VC4_HD_M_CTL, VC4_HD_M_ENABLE);
696
697 HDMI_WRITE(VC4_HDMI_SW_RESET_CONTROL,
698 VC4_HDMI_SW_RESET_HDMI |
699 VC4_HDMI_SW_RESET_FORMAT_DETECT);
700
701 HDMI_WRITE(VC4_HDMI_SW_RESET_CONTROL, 0);
702
703 /* PHY should be in reset, like
704 * vc4_hdmi_encoder_disable() does.
705 */
706 HDMI_WRITE(VC4_HDMI_TX_PHY_RESET_CTL, 0xf << 16);
707 }
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800708
709 drm_encoder_init(drm, hdmi->encoder, &vc4_hdmi_encoder_funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +0200710 DRM_MODE_ENCODER_TMDS, NULL);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800711 drm_encoder_helper_add(hdmi->encoder, &vc4_hdmi_encoder_helper_funcs);
712
713 hdmi->connector = vc4_hdmi_connector_init(drm, hdmi->encoder);
714 if (IS_ERR(hdmi->connector)) {
715 ret = PTR_ERR(hdmi->connector);
716 goto err_destroy_encoder;
717 }
718
719 return 0;
720
721err_destroy_encoder:
722 vc4_hdmi_encoder_destroy(hdmi->encoder);
723err_unprepare_hsm:
724 clk_disable_unprepare(hdmi->hsm_clock);
725err_unprepare_pix:
726 clk_disable_unprepare(hdmi->pixel_clock);
727err_put_i2c:
Eric Anholt58839802016-04-04 14:25:59 -0700728 put_device(&hdmi->ddc->dev);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800729
730 return ret;
731}
732
733static void vc4_hdmi_unbind(struct device *dev, struct device *master,
734 void *data)
735{
736 struct drm_device *drm = dev_get_drvdata(master);
737 struct vc4_dev *vc4 = drm->dev_private;
738 struct vc4_hdmi *hdmi = vc4->hdmi;
739
740 vc4_hdmi_connector_destroy(hdmi->connector);
741 vc4_hdmi_encoder_destroy(hdmi->encoder);
742
743 clk_disable_unprepare(hdmi->pixel_clock);
744 clk_disable_unprepare(hdmi->hsm_clock);
745 put_device(&hdmi->ddc->dev);
746
747 vc4->hdmi = NULL;
748}
749
750static const struct component_ops vc4_hdmi_ops = {
751 .bind = vc4_hdmi_bind,
752 .unbind = vc4_hdmi_unbind,
753};
754
755static int vc4_hdmi_dev_probe(struct platform_device *pdev)
756{
757 return component_add(&pdev->dev, &vc4_hdmi_ops);
758}
759
760static int vc4_hdmi_dev_remove(struct platform_device *pdev)
761{
762 component_del(&pdev->dev, &vc4_hdmi_ops);
763 return 0;
764}
765
766static const struct of_device_id vc4_hdmi_dt_match[] = {
767 { .compatible = "brcm,bcm2835-hdmi" },
768 {}
769};
770
771struct platform_driver vc4_hdmi_driver = {
772 .probe = vc4_hdmi_dev_probe,
773 .remove = vc4_hdmi_dev_remove,
774 .driver = {
775 .name = "vc4_hdmi",
776 .of_match_table = vc4_hdmi_dt_match,
777 },
778};