2 * Copyright (C) 2016 BayLibre, SAS
3 * Author: Neil Armstrong <narmstrong@baylibre.com>
4 * Copyright (C) 2015 Amlogic, Inc. All rights reserved.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/component.h>
23 #include <linux/of_graph.h>
24 #include <linux/reset.h>
25 #include <linux/clk.h>
26 #include <linux/regulator/consumer.h>
29 #include <drm/drm_edid.h>
30 #include <drm/drm_crtc_helper.h>
31 #include <drm/drm_atomic_helper.h>
32 #include <drm/bridge/dw_hdmi.h>
34 #include <uapi/linux/media-bus-format.h>
35 #include <uapi/linux/videodev2.h>
37 #include "meson_drv.h"
38 #include "meson_venc.h"
39 #include "meson_vclk.h"
40 #include "meson_dw_hdmi.h"
41 #include "meson_registers.h"
43 #define DRIVER_NAME "meson-dw-hdmi"
44 #define DRIVER_DESC "Amlogic Meson HDMI-TX DRM driver"
49 * HDMI Output is composed of :
51 * - A Synopsys DesignWare HDMI Controller IP
52 * - A TOP control block controlling the Clocks and PHY
53 * - A custom HDMI PHY in order convert video to TMDS signal
57 * ___________________________________
59 * |___________________________________|
61 * | Synopsys HDMI | HDMI PHY |=> TMDS
62 * | Controller |________________|
63 * |___________________________________|<=> DDC
66 * The HDMI TOP block only supports HPD sensing.
67 * The Synopsys HDMI Controller interrupt is routed
68 * through the TOP Block interrupt.
69 * Communication to the TOP Block and the Synopsys
70 * HDMI Controller is done a pair of addr+read/write
72 * The HDMI PHY is configured by registers in the
75 * Pixel data arrives in 4:4:4 format from the VENC
76 * block and the VPU HDMI mux selects either the ENCI
77 * encoder for the 576i or 480i formats or the ENCP
78 * encoder for all the other formats including
79 * interlaced HD formats.
80 * The VENC uses a DVI encoder on top of the ENCI
81 * or ENCP encoders to generate DVI timings for the
84 * GXBB, GXL and GXM embeds the Synopsys DesignWare
85 * HDMI TX IP version 2.01a with HDCP and I2C & S/PDIF
86 * audio source interfaces.
88 * We handle the following features :
90 * - HPD Rise & Fall interrupt
91 * - HDMI Controller Interrupt
92 * - HDMI PHY Init for 480i to 1080p60
93 * - VENC & HDMI Clock setup for 480i to 1080p60
94 * - VENC Mode setup for 480i to 1080p60
98 * - PHY, Clock and Mode setup for 2k && 4k modes
99 * - SDDC Scrambling mode for HDMI 2.0a
104 /* TOP Block Communication Channel */
105 #define HDMITX_TOP_ADDR_REG 0x0
106 #define HDMITX_TOP_DATA_REG 0x4
107 #define HDMITX_TOP_CTRL_REG 0x8
109 /* Controller Communication Channel */
110 #define HDMITX_DWC_ADDR_REG 0x10
111 #define HDMITX_DWC_DATA_REG 0x14
112 #define HDMITX_DWC_CTRL_REG 0x18
115 #define HHI_MEM_PD_REG0 0x100 /* 0x40 */
116 #define HHI_HDMI_CLK_CNTL 0x1cc /* 0x73 */
117 #define HHI_HDMI_PHY_CNTL0 0x3a0 /* 0xe8 */
118 #define HHI_HDMI_PHY_CNTL1 0x3a4 /* 0xe9 */
119 #define HHI_HDMI_PHY_CNTL2 0x3a8 /* 0xea */
120 #define HHI_HDMI_PHY_CNTL3 0x3ac /* 0xeb */
122 static DEFINE_SPINLOCK(reg_lock);
124 enum meson_venc_source {
125 MESON_VENC_SOURCE_NONE = 0,
126 MESON_VENC_SOURCE_ENCI = 1,
127 MESON_VENC_SOURCE_ENCP = 2,
130 struct meson_dw_hdmi {
131 struct drm_encoder encoder;
132 struct dw_hdmi_plat_data dw_plat_data;
133 struct meson_drm *priv;
135 void __iomem *hdmitx;
136 struct reset_control *hdmitx_apb;
137 struct reset_control *hdmitx_ctrl;
138 struct reset_control *hdmitx_phy;
139 struct clk *hdmi_pclk;
140 struct clk *venci_clk;
141 struct regulator *hdmi_supply;
143 struct dw_hdmi *hdmi;
145 #define encoder_to_meson_dw_hdmi(x) \
146 container_of(x, struct meson_dw_hdmi, encoder)
148 static inline int dw_hdmi_is_compatible(struct meson_dw_hdmi *dw_hdmi,
151 return of_device_is_compatible(dw_hdmi->dev->of_node, compat);
154 /* PHY (via TOP bridge) and Controller dedicated register interface */
156 static unsigned int dw_hdmi_top_read(struct meson_dw_hdmi *dw_hdmi,
162 spin_lock_irqsave(®_lock, flags);
164 /* ADDR must be written twice */
165 writel(addr & 0xffff, dw_hdmi->hdmitx + HDMITX_TOP_ADDR_REG);
166 writel(addr & 0xffff, dw_hdmi->hdmitx + HDMITX_TOP_ADDR_REG);
168 /* Read needs a second DATA read */
169 data = readl(dw_hdmi->hdmitx + HDMITX_TOP_DATA_REG);
170 data = readl(dw_hdmi->hdmitx + HDMITX_TOP_DATA_REG);
172 spin_unlock_irqrestore(®_lock, flags);
177 static inline void dw_hdmi_top_write(struct meson_dw_hdmi *dw_hdmi,
178 unsigned int addr, unsigned int data)
182 spin_lock_irqsave(®_lock, flags);
184 /* ADDR must be written twice */
185 writel(addr & 0xffff, dw_hdmi->hdmitx + HDMITX_TOP_ADDR_REG);
186 writel(addr & 0xffff, dw_hdmi->hdmitx + HDMITX_TOP_ADDR_REG);
188 /* Write needs single DATA write */
189 writel(data, dw_hdmi->hdmitx + HDMITX_TOP_DATA_REG);
191 spin_unlock_irqrestore(®_lock, flags);
194 /* Helper to change specific bits in PHY registers */
195 static inline void dw_hdmi_top_write_bits(struct meson_dw_hdmi *dw_hdmi,
200 unsigned int data = dw_hdmi_top_read(dw_hdmi, addr);
205 dw_hdmi_top_write(dw_hdmi, addr, data);
208 static unsigned int dw_hdmi_dwc_read(struct meson_dw_hdmi *dw_hdmi,
214 spin_lock_irqsave(®_lock, flags);
216 /* ADDR must be written twice */
217 writel(addr & 0xffff, dw_hdmi->hdmitx + HDMITX_DWC_ADDR_REG);
218 writel(addr & 0xffff, dw_hdmi->hdmitx + HDMITX_DWC_ADDR_REG);
220 /* Read needs a second DATA read */
221 data = readl(dw_hdmi->hdmitx + HDMITX_DWC_DATA_REG);
222 data = readl(dw_hdmi->hdmitx + HDMITX_DWC_DATA_REG);
224 spin_unlock_irqrestore(®_lock, flags);
229 static inline void dw_hdmi_dwc_write(struct meson_dw_hdmi *dw_hdmi,
230 unsigned int addr, unsigned int data)
234 spin_lock_irqsave(®_lock, flags);
236 /* ADDR must be written twice */
237 writel(addr & 0xffff, dw_hdmi->hdmitx + HDMITX_DWC_ADDR_REG);
238 writel(addr & 0xffff, dw_hdmi->hdmitx + HDMITX_DWC_ADDR_REG);
240 /* Write needs single DATA write */
241 writel(data, dw_hdmi->hdmitx + HDMITX_DWC_DATA_REG);
243 spin_unlock_irqrestore(®_lock, flags);
246 /* Helper to change specific bits in controller registers */
247 static inline void dw_hdmi_dwc_write_bits(struct meson_dw_hdmi *dw_hdmi,
252 unsigned int data = dw_hdmi_dwc_read(dw_hdmi, addr);
257 dw_hdmi_dwc_write(dw_hdmi, addr, data);
262 /* Setup PHY bandwidth modes */
263 static void meson_hdmi_phy_setup_mode(struct meson_dw_hdmi *dw_hdmi,
264 struct drm_display_mode *mode)
266 struct meson_drm *priv = dw_hdmi->priv;
267 unsigned int pixel_clock = mode->clock;
269 if (dw_hdmi_is_compatible(dw_hdmi, "amlogic,meson-gxl-dw-hdmi") ||
270 dw_hdmi_is_compatible(dw_hdmi, "amlogic,meson-gxm-dw-hdmi")) {
271 if (pixel_clock >= 371250) {
272 /* 5.94Gbps, 3.7125Gbps */
273 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL0, 0x333d3282);
274 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL3, 0x2136315b);
275 } else if (pixel_clock >= 297000) {
277 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL0, 0x33303382);
278 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL3, 0x2036315b);
279 } else if (pixel_clock >= 148500) {
281 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL0, 0x33303362);
282 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL3, 0x2016315b);
284 /* 742.5Mbps, and below */
285 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL0, 0x33604142);
286 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL3, 0x0016315b);
288 } else if (dw_hdmi_is_compatible(dw_hdmi,
289 "amlogic,meson-gxbb-dw-hdmi")) {
290 if (pixel_clock >= 371250) {
291 /* 5.94Gbps, 3.7125Gbps */
292 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL0, 0x33353245);
293 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL3, 0x2100115b);
294 } else if (pixel_clock >= 297000) {
296 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL0, 0x33634283);
297 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL3, 0xb000115b);
299 /* 1.485Gbps, and below */
300 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL0, 0x33632122);
301 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL3, 0x2000115b);
306 static inline void meson_dw_hdmi_phy_reset(struct meson_dw_hdmi *dw_hdmi)
308 struct meson_drm *priv = dw_hdmi->priv;
310 /* Enable and software reset */
311 regmap_update_bits(priv->hhi, HHI_HDMI_PHY_CNTL1, 0xf, 0xf);
315 /* Enable and unreset */
316 regmap_update_bits(priv->hhi, HHI_HDMI_PHY_CNTL1, 0xf, 0xe);
321 static void dw_hdmi_set_vclk(struct meson_dw_hdmi *dw_hdmi,
322 struct drm_display_mode *mode)
324 struct meson_drm *priv = dw_hdmi->priv;
325 int vic = drm_match_cea_mode(mode);
326 unsigned int vclk_freq;
327 unsigned int venc_freq;
328 unsigned int hdmi_freq;
330 vclk_freq = mode->clock;
332 if (mode->flags & DRM_MODE_FLAG_DBLCLK)
335 venc_freq = vclk_freq;
336 hdmi_freq = vclk_freq;
338 if (meson_venc_hdmi_venc_repeat(vic))
341 vclk_freq = max(venc_freq, hdmi_freq);
343 if (mode->flags & DRM_MODE_FLAG_DBLCLK)
346 DRM_DEBUG_DRIVER("vclk:%d venc=%d hdmi=%d enci=%d\n",
347 vclk_freq, venc_freq, hdmi_freq,
348 priv->venc.hdmi_use_enci);
350 meson_vclk_setup(priv, MESON_VCLK_TARGET_HDMI, vclk_freq,
351 venc_freq, hdmi_freq, priv->venc.hdmi_use_enci);
354 static int dw_hdmi_phy_init(struct dw_hdmi *hdmi, void *data,
355 struct drm_display_mode *mode)
357 struct meson_dw_hdmi *dw_hdmi = (struct meson_dw_hdmi *)data;
358 struct meson_drm *priv = dw_hdmi->priv;
359 unsigned int wr_clk =
360 readl_relaxed(priv->io_base + _REG(VPU_HDMI_SETTING));
362 DRM_DEBUG_DRIVER("%d:\"%s\"\n", mode->base.id, mode->name);
365 regmap_update_bits(priv->hhi, HHI_HDMI_CLK_CNTL, 0xffff, 0x100);
367 /* Bring HDMITX MEM output of power down */
368 regmap_update_bits(priv->hhi, HHI_MEM_PD_REG0, 0xff << 8, 0);
370 /* Bring out of reset */
371 dw_hdmi_top_write(dw_hdmi, HDMITX_TOP_SW_RESET, 0);
373 /* Enable internal pixclk, tmds_clk, spdif_clk, i2s_clk, cecclk */
374 dw_hdmi_top_write_bits(dw_hdmi, HDMITX_TOP_CLK_CNTL,
376 dw_hdmi_top_write_bits(dw_hdmi, HDMITX_TOP_CLK_CNTL,
379 /* Enable normal output to PHY */
380 dw_hdmi_top_write(dw_hdmi, HDMITX_TOP_BIST_CNTL, BIT(12));
382 /* TMDS pattern setup (TOFIX pattern for 4k2k scrambling) */
383 dw_hdmi_top_write(dw_hdmi, HDMITX_TOP_TMDS_CLK_PTTN_01, 0x001f001f);
384 dw_hdmi_top_write(dw_hdmi, HDMITX_TOP_TMDS_CLK_PTTN_23, 0x001f001f);
386 /* Load TMDS pattern */
387 dw_hdmi_top_write(dw_hdmi, HDMITX_TOP_TMDS_CLK_PTTN_CNTL, 0x1);
389 dw_hdmi_top_write(dw_hdmi, HDMITX_TOP_TMDS_CLK_PTTN_CNTL, 0x2);
391 /* Setup PHY parameters */
392 meson_hdmi_phy_setup_mode(dw_hdmi, mode);
395 regmap_update_bits(priv->hhi, HHI_HDMI_PHY_CNTL1,
396 0xffff << 16, 0x0390 << 16);
399 if (dw_hdmi_is_compatible(dw_hdmi, "amlogic,meson-gxl-dw-hdmi") ||
400 dw_hdmi_is_compatible(dw_hdmi, "amlogic,meson-gxm-dw-hdmi"))
401 regmap_update_bits(priv->hhi, HHI_HDMI_PHY_CNTL1,
404 regmap_update_bits(priv->hhi, HHI_HDMI_PHY_CNTL1,
407 /* Disable clock, fifo, fifo_wr */
408 regmap_update_bits(priv->hhi, HHI_HDMI_PHY_CNTL1, 0xf, 0);
412 /* Reset PHY 3 times in a row */
413 meson_dw_hdmi_phy_reset(dw_hdmi);
414 meson_dw_hdmi_phy_reset(dw_hdmi);
415 meson_dw_hdmi_phy_reset(dw_hdmi);
417 /* Temporary Disable VENC video stream */
418 if (priv->venc.hdmi_use_enci)
419 writel_relaxed(0, priv->io_base + _REG(ENCI_VIDEO_EN));
421 writel_relaxed(0, priv->io_base + _REG(ENCP_VIDEO_EN));
423 /* Temporary Disable HDMI video stream to HDMI-TX */
424 writel_bits_relaxed(0x3, 0,
425 priv->io_base + _REG(VPU_HDMI_SETTING));
426 writel_bits_relaxed(0xf << 8, 0,
427 priv->io_base + _REG(VPU_HDMI_SETTING));
429 /* Re-Enable VENC video stream */
430 if (priv->venc.hdmi_use_enci)
431 writel_relaxed(1, priv->io_base + _REG(ENCI_VIDEO_EN));
433 writel_relaxed(1, priv->io_base + _REG(ENCP_VIDEO_EN));
435 /* Push back HDMI clock settings */
436 writel_bits_relaxed(0xf << 8, wr_clk & (0xf << 8),
437 priv->io_base + _REG(VPU_HDMI_SETTING));
439 /* Enable and Select HDMI video source for HDMI-TX */
440 if (priv->venc.hdmi_use_enci)
441 writel_bits_relaxed(0x3, MESON_VENC_SOURCE_ENCI,
442 priv->io_base + _REG(VPU_HDMI_SETTING));
444 writel_bits_relaxed(0x3, MESON_VENC_SOURCE_ENCP,
445 priv->io_base + _REG(VPU_HDMI_SETTING));
450 static void dw_hdmi_phy_disable(struct dw_hdmi *hdmi,
453 struct meson_dw_hdmi *dw_hdmi = (struct meson_dw_hdmi *)data;
454 struct meson_drm *priv = dw_hdmi->priv;
456 DRM_DEBUG_DRIVER("\n");
458 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL0, 0);
461 static enum drm_connector_status dw_hdmi_read_hpd(struct dw_hdmi *hdmi,
464 struct meson_dw_hdmi *dw_hdmi = (struct meson_dw_hdmi *)data;
466 return !!dw_hdmi_top_read(dw_hdmi, HDMITX_TOP_STAT0) ?
467 connector_status_connected : connector_status_disconnected;
470 static void dw_hdmi_setup_hpd(struct dw_hdmi *hdmi,
473 struct meson_dw_hdmi *dw_hdmi = (struct meson_dw_hdmi *)data;
475 /* Setup HPD Filter */
476 dw_hdmi_top_write(dw_hdmi, HDMITX_TOP_HPD_FILTER,
479 /* Clear interrupts */
480 dw_hdmi_top_write(dw_hdmi, HDMITX_TOP_INTR_STAT_CLR,
481 HDMITX_TOP_INTR_HPD_RISE | HDMITX_TOP_INTR_HPD_FALL);
483 /* Unmask interrupts */
484 dw_hdmi_top_write_bits(dw_hdmi, HDMITX_TOP_INTR_MASKN,
485 HDMITX_TOP_INTR_HPD_RISE | HDMITX_TOP_INTR_HPD_FALL,
486 HDMITX_TOP_INTR_HPD_RISE | HDMITX_TOP_INTR_HPD_FALL);
489 static const struct dw_hdmi_phy_ops meson_dw_hdmi_phy_ops = {
490 .init = dw_hdmi_phy_init,
491 .disable = dw_hdmi_phy_disable,
492 .read_hpd = dw_hdmi_read_hpd,
493 .setup_hpd = dw_hdmi_setup_hpd,
496 static irqreturn_t dw_hdmi_top_irq(int irq, void *dev_id)
498 struct meson_dw_hdmi *dw_hdmi = dev_id;
501 stat = dw_hdmi_top_read(dw_hdmi, HDMITX_TOP_INTR_STAT);
502 dw_hdmi_top_write(dw_hdmi, HDMITX_TOP_INTR_STAT_CLR, stat);
504 /* HPD Events, handle in the threaded interrupt handler */
505 if (stat & (HDMITX_TOP_INTR_HPD_RISE | HDMITX_TOP_INTR_HPD_FALL)) {
506 dw_hdmi->irq_stat = stat;
507 return IRQ_WAKE_THREAD;
510 /* HDMI Controller Interrupt */
514 /* TOFIX Handle HDCP Interrupts */
519 /* Threaded interrupt handler to manage HPD events */
520 static irqreturn_t dw_hdmi_top_thread_irq(int irq, void *dev_id)
522 struct meson_dw_hdmi *dw_hdmi = dev_id;
523 u32 stat = dw_hdmi->irq_stat;
526 if (stat & (HDMITX_TOP_INTR_HPD_RISE | HDMITX_TOP_INTR_HPD_FALL)) {
527 bool hpd_connected = false;
529 if (stat & HDMITX_TOP_INTR_HPD_RISE)
530 hpd_connected = true;
532 dw_hdmi_setup_rx_sense(dw_hdmi->hdmi, hpd_connected,
535 drm_helper_hpd_irq_event(dw_hdmi->encoder.dev);
541 static enum drm_mode_status
542 dw_hdmi_mode_valid(struct drm_connector *connector,
543 const struct drm_display_mode *mode)
545 unsigned int vclk_freq;
546 unsigned int venc_freq;
547 unsigned int hdmi_freq;
548 int vic = drm_match_cea_mode(mode);
550 DRM_DEBUG_DRIVER("Modeline %d:\"%s\" %d %d %d %d %d %d %d %d %d %d 0x%x 0x%x\n",
551 mode->base.id, mode->name, mode->vrefresh, mode->clock,
552 mode->hdisplay, mode->hsync_start,
553 mode->hsync_end, mode->htotal,
554 mode->vdisplay, mode->vsync_start,
555 mode->vsync_end, mode->vtotal, mode->type, mode->flags);
557 /* Check against non-VIC supported modes */
559 if (!meson_venc_hdmi_supported_mode(mode))
561 /* Check against supported VIC modes */
562 } else if (!meson_venc_hdmi_supported_vic(vic))
565 vclk_freq = mode->clock;
567 /* 480i/576i needs global pixel doubling */
568 if (mode->flags & DRM_MODE_FLAG_DBLCLK)
571 venc_freq = vclk_freq;
572 hdmi_freq = vclk_freq;
574 /* VENC double pixels for 1080i and 720p modes */
575 if (meson_venc_hdmi_venc_repeat(vic))
578 vclk_freq = max(venc_freq, hdmi_freq);
580 if (mode->flags & DRM_MODE_FLAG_DBLCLK)
583 dev_dbg(connector->dev->dev, "%s: vclk:%d venc=%d hdmi=%d\n", __func__,
584 vclk_freq, venc_freq, hdmi_freq);
586 /* Finally filter by configurable vclk frequencies */
601 return MODE_CLOCK_RANGE;
606 static void meson_venc_hdmi_encoder_destroy(struct drm_encoder *encoder)
608 drm_encoder_cleanup(encoder);
611 static const struct drm_encoder_funcs meson_venc_hdmi_encoder_funcs = {
612 .destroy = meson_venc_hdmi_encoder_destroy,
615 static int meson_venc_hdmi_encoder_atomic_check(struct drm_encoder *encoder,
616 struct drm_crtc_state *crtc_state,
617 struct drm_connector_state *conn_state)
622 static void meson_venc_hdmi_encoder_disable(struct drm_encoder *encoder)
624 struct meson_dw_hdmi *dw_hdmi = encoder_to_meson_dw_hdmi(encoder);
625 struct meson_drm *priv = dw_hdmi->priv;
627 DRM_DEBUG_DRIVER("\n");
629 writel_bits_relaxed(0x3, 0,
630 priv->io_base + _REG(VPU_HDMI_SETTING));
632 writel_relaxed(0, priv->io_base + _REG(ENCI_VIDEO_EN));
633 writel_relaxed(0, priv->io_base + _REG(ENCP_VIDEO_EN));
636 static void meson_venc_hdmi_encoder_enable(struct drm_encoder *encoder)
638 struct meson_dw_hdmi *dw_hdmi = encoder_to_meson_dw_hdmi(encoder);
639 struct meson_drm *priv = dw_hdmi->priv;
641 DRM_DEBUG_DRIVER("%s\n", priv->venc.hdmi_use_enci ? "VENCI" : "VENCP");
643 if (priv->venc.hdmi_use_enci)
644 writel_relaxed(1, priv->io_base + _REG(ENCI_VIDEO_EN));
646 writel_relaxed(1, priv->io_base + _REG(ENCP_VIDEO_EN));
649 static void meson_venc_hdmi_encoder_mode_set(struct drm_encoder *encoder,
650 struct drm_display_mode *mode,
651 struct drm_display_mode *adjusted_mode)
653 struct meson_dw_hdmi *dw_hdmi = encoder_to_meson_dw_hdmi(encoder);
654 struct meson_drm *priv = dw_hdmi->priv;
655 int vic = drm_match_cea_mode(mode);
657 DRM_DEBUG_DRIVER("%d:\"%s\" vic %d\n",
658 mode->base.id, mode->name, vic);
660 /* VENC + VENC-DVI Mode setup */
661 meson_venc_hdmi_mode_set(priv, vic, mode);
664 dw_hdmi_set_vclk(dw_hdmi, mode);
666 /* Setup YUV444 to HDMI-TX, no 10bit diphering */
667 writel_relaxed(0, priv->io_base + _REG(VPU_HDMI_FMT_CTRL));
670 static const struct drm_encoder_helper_funcs
671 meson_venc_hdmi_encoder_helper_funcs = {
672 .atomic_check = meson_venc_hdmi_encoder_atomic_check,
673 .disable = meson_venc_hdmi_encoder_disable,
674 .enable = meson_venc_hdmi_encoder_enable,
675 .mode_set = meson_venc_hdmi_encoder_mode_set,
680 static int meson_dw_hdmi_reg_read(void *context, unsigned int reg,
681 unsigned int *result)
683 *result = dw_hdmi_dwc_read(context, reg);
689 static int meson_dw_hdmi_reg_write(void *context, unsigned int reg,
692 dw_hdmi_dwc_write(context, reg, val);
697 static const struct regmap_config meson_dw_hdmi_regmap_config = {
700 .reg_read = meson_dw_hdmi_reg_read,
701 .reg_write = meson_dw_hdmi_reg_write,
702 .max_register = 0x10000,
705 static bool meson_hdmi_connector_is_available(struct device *dev)
707 struct device_node *ep, *remote;
709 /* HDMI Connector is on the second port, first endpoint */
710 ep = of_graph_get_endpoint_by_regs(dev->of_node, 1, 0);
714 /* If the endpoint node exists, consider it enabled */
715 remote = of_graph_get_remote_port(ep);
727 static int meson_dw_hdmi_bind(struct device *dev, struct device *master,
730 struct platform_device *pdev = to_platform_device(dev);
731 struct meson_dw_hdmi *meson_dw_hdmi;
732 struct drm_device *drm = data;
733 struct meson_drm *priv = drm->dev_private;
734 struct dw_hdmi_plat_data *dw_plat_data;
735 struct drm_encoder *encoder;
736 struct resource *res;
740 DRM_DEBUG_DRIVER("\n");
742 if (!meson_hdmi_connector_is_available(dev)) {
743 dev_info(drm->dev, "HDMI Output connector not available\n");
747 meson_dw_hdmi = devm_kzalloc(dev, sizeof(*meson_dw_hdmi),
752 meson_dw_hdmi->priv = priv;
753 meson_dw_hdmi->dev = dev;
754 dw_plat_data = &meson_dw_hdmi->dw_plat_data;
755 encoder = &meson_dw_hdmi->encoder;
757 meson_dw_hdmi->hdmi_supply = devm_regulator_get_optional(dev, "hdmi");
758 if (IS_ERR(meson_dw_hdmi->hdmi_supply)) {
759 if (PTR_ERR(meson_dw_hdmi->hdmi_supply) == -EPROBE_DEFER)
760 return -EPROBE_DEFER;
761 meson_dw_hdmi->hdmi_supply = NULL;
763 ret = regulator_enable(meson_dw_hdmi->hdmi_supply);
768 meson_dw_hdmi->hdmitx_apb = devm_reset_control_get_exclusive(dev,
770 if (IS_ERR(meson_dw_hdmi->hdmitx_apb)) {
771 dev_err(dev, "Failed to get hdmitx_apb reset\n");
772 return PTR_ERR(meson_dw_hdmi->hdmitx_apb);
775 meson_dw_hdmi->hdmitx_ctrl = devm_reset_control_get_exclusive(dev,
777 if (IS_ERR(meson_dw_hdmi->hdmitx_ctrl)) {
778 dev_err(dev, "Failed to get hdmitx reset\n");
779 return PTR_ERR(meson_dw_hdmi->hdmitx_ctrl);
782 meson_dw_hdmi->hdmitx_phy = devm_reset_control_get_exclusive(dev,
784 if (IS_ERR(meson_dw_hdmi->hdmitx_phy)) {
785 dev_err(dev, "Failed to get hdmitx_phy reset\n");
786 return PTR_ERR(meson_dw_hdmi->hdmitx_phy);
789 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
790 meson_dw_hdmi->hdmitx = devm_ioremap_resource(dev, res);
791 if (IS_ERR(meson_dw_hdmi->hdmitx))
792 return PTR_ERR(meson_dw_hdmi->hdmitx);
794 meson_dw_hdmi->hdmi_pclk = devm_clk_get(dev, "isfr");
795 if (IS_ERR(meson_dw_hdmi->hdmi_pclk)) {
796 dev_err(dev, "Unable to get HDMI pclk\n");
797 return PTR_ERR(meson_dw_hdmi->hdmi_pclk);
799 clk_prepare_enable(meson_dw_hdmi->hdmi_pclk);
801 meson_dw_hdmi->venci_clk = devm_clk_get(dev, "venci");
802 if (IS_ERR(meson_dw_hdmi->venci_clk)) {
803 dev_err(dev, "Unable to get venci clk\n");
804 return PTR_ERR(meson_dw_hdmi->venci_clk);
806 clk_prepare_enable(meson_dw_hdmi->venci_clk);
808 dw_plat_data->regm = devm_regmap_init(dev, NULL, meson_dw_hdmi,
809 &meson_dw_hdmi_regmap_config);
810 if (IS_ERR(dw_plat_data->regm))
811 return PTR_ERR(dw_plat_data->regm);
813 irq = platform_get_irq(pdev, 0);
815 dev_err(dev, "Failed to get hdmi top irq\n");
819 ret = devm_request_threaded_irq(dev, irq, dw_hdmi_top_irq,
820 dw_hdmi_top_thread_irq, IRQF_SHARED,
821 "dw_hdmi_top_irq", meson_dw_hdmi);
823 dev_err(dev, "Failed to request hdmi top irq\n");
829 drm_encoder_helper_add(encoder, &meson_venc_hdmi_encoder_helper_funcs);
831 ret = drm_encoder_init(drm, encoder, &meson_venc_hdmi_encoder_funcs,
832 DRM_MODE_ENCODER_TMDS, "meson_hdmi");
834 dev_err(priv->dev, "Failed to init HDMI encoder\n");
838 encoder->possible_crtcs = BIT(0);
840 DRM_DEBUG_DRIVER("encoder initialized\n");
843 regmap_update_bits(priv->hhi, HHI_HDMI_CLK_CNTL, 0xffff, 0x100);
845 /* Bring HDMITX MEM output of power down */
846 regmap_update_bits(priv->hhi, HHI_MEM_PD_REG0, 0xff << 8, 0);
848 /* Reset HDMITX APB & TX & PHY */
849 reset_control_reset(meson_dw_hdmi->hdmitx_apb);
850 reset_control_reset(meson_dw_hdmi->hdmitx_ctrl);
851 reset_control_reset(meson_dw_hdmi->hdmitx_phy);
853 /* Enable APB3 fail on error */
854 writel_bits_relaxed(BIT(15), BIT(15),
855 meson_dw_hdmi->hdmitx + HDMITX_TOP_CTRL_REG);
856 writel_bits_relaxed(BIT(15), BIT(15),
857 meson_dw_hdmi->hdmitx + HDMITX_DWC_CTRL_REG);
859 /* Bring out of reset */
860 dw_hdmi_top_write(meson_dw_hdmi, HDMITX_TOP_SW_RESET, 0);
864 dw_hdmi_top_write(meson_dw_hdmi, HDMITX_TOP_CLK_CNTL, 0xff);
866 /* Enable HDMI-TX Interrupt */
867 dw_hdmi_top_write(meson_dw_hdmi, HDMITX_TOP_INTR_STAT_CLR,
868 HDMITX_TOP_INTR_CORE);
870 dw_hdmi_top_write(meson_dw_hdmi, HDMITX_TOP_INTR_MASKN,
871 HDMITX_TOP_INTR_CORE);
873 /* Bridge / Connector */
875 dw_plat_data->mode_valid = dw_hdmi_mode_valid;
876 dw_plat_data->phy_ops = &meson_dw_hdmi_phy_ops;
877 dw_plat_data->phy_name = "meson_dw_hdmi_phy";
878 dw_plat_data->phy_data = meson_dw_hdmi;
879 dw_plat_data->input_bus_format = MEDIA_BUS_FMT_YUV8_1X24;
880 dw_plat_data->input_bus_encoding = V4L2_YCBCR_ENC_709;
882 platform_set_drvdata(pdev, meson_dw_hdmi);
884 meson_dw_hdmi->hdmi = dw_hdmi_bind(pdev, encoder,
885 &meson_dw_hdmi->dw_plat_data);
886 if (IS_ERR(meson_dw_hdmi->hdmi))
887 return PTR_ERR(meson_dw_hdmi->hdmi);
889 DRM_DEBUG_DRIVER("HDMI controller initialized\n");
894 static void meson_dw_hdmi_unbind(struct device *dev, struct device *master,
897 struct meson_dw_hdmi *meson_dw_hdmi = dev_get_drvdata(dev);
899 dw_hdmi_unbind(meson_dw_hdmi->hdmi);
902 static const struct component_ops meson_dw_hdmi_ops = {
903 .bind = meson_dw_hdmi_bind,
904 .unbind = meson_dw_hdmi_unbind,
907 static int meson_dw_hdmi_probe(struct platform_device *pdev)
909 return component_add(&pdev->dev, &meson_dw_hdmi_ops);
912 static int meson_dw_hdmi_remove(struct platform_device *pdev)
914 component_del(&pdev->dev, &meson_dw_hdmi_ops);
919 static const struct of_device_id meson_dw_hdmi_of_table[] = {
920 { .compatible = "amlogic,meson-gxbb-dw-hdmi" },
921 { .compatible = "amlogic,meson-gxl-dw-hdmi" },
922 { .compatible = "amlogic,meson-gxm-dw-hdmi" },
925 MODULE_DEVICE_TABLE(of, meson_dw_hdmi_of_table);
927 static struct platform_driver meson_dw_hdmi_platform_driver = {
928 .probe = meson_dw_hdmi_probe,
929 .remove = meson_dw_hdmi_remove,
932 .of_match_table = meson_dw_hdmi_of_table,
935 module_platform_driver(meson_dw_hdmi_platform_driver);
937 MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
938 MODULE_DESCRIPTION(DRIVER_DESC);
939 MODULE_LICENSE("GPL");