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>
28 #include <drm/drm_edid.h>
29 #include <drm/drm_crtc_helper.h>
30 #include <drm/drm_atomic_helper.h>
31 #include <drm/bridge/dw_hdmi.h>
33 #include <uapi/linux/media-bus-format.h>
34 #include <uapi/linux/videodev2.h>
36 #include "meson_drv.h"
37 #include "meson_venc.h"
38 #include "meson_vclk.h"
39 #include "meson_dw_hdmi.h"
40 #include "meson_registers.h"
42 #define DRIVER_NAME "meson-dw-hdmi"
43 #define DRIVER_DESC "Amlogic Meson HDMI-TX DRM driver"
48 * HDMI Output is composed of :
50 * - A Synopsys DesignWare HDMI Controller IP
51 * - A TOP control block controlling the Clocks and PHY
52 * - A custom HDMI PHY in order convert video to TMDS signal
56 * ___________________________________
58 * |___________________________________|
60 * | Synopsys HDMI | HDMI PHY |=> TMDS
61 * | Controller |________________|
62 * |___________________________________|<=> DDC
65 * The HDMI TOP block only supports HPD sensing.
66 * The Synopsys HDMI Controller interrupt is routed
67 * through the TOP Block interrupt.
68 * Communication to the TOP Block and the Synopsys
69 * HDMI Controller is done a pair of addr+read/write
71 * The HDMI PHY is configured by registers in the
74 * Pixel data arrives in 4:4:4 format from the VENC
75 * block and the VPU HDMI mux selects either the ENCI
76 * encoder for the 576i or 480i formats or the ENCP
77 * encoder for all the other formats including
78 * interlaced HD formats.
79 * The VENC uses a DVI encoder on top of the ENCI
80 * or ENCP encoders to generate DVI timings for the
83 * GXBB, GXL and GXM embeds the Synopsys DesignWare
84 * HDMI TX IP version 2.01a with HDCP and I2C & S/PDIF
85 * audio source interfaces.
87 * We handle the following features :
89 * - HPD Rise & Fall interrupt
90 * - HDMI Controller Interrupt
91 * - HDMI PHY Init for 480i to 1080p60
92 * - VENC & HDMI Clock setup for 480i to 1080p60
93 * - VENC Mode setup for 480i to 1080p60
97 * - PHY, Clock and Mode setup for 2k && 4k modes
98 * - SDDC Scrambling mode for HDMI 2.0a
103 /* TOP Block Communication Channel */
104 #define HDMITX_TOP_ADDR_REG 0x0
105 #define HDMITX_TOP_DATA_REG 0x4
106 #define HDMITX_TOP_CTRL_REG 0x8
108 /* Controller Communication Channel */
109 #define HDMITX_DWC_ADDR_REG 0x10
110 #define HDMITX_DWC_DATA_REG 0x14
111 #define HDMITX_DWC_CTRL_REG 0x18
114 #define HHI_MEM_PD_REG0 0x100 /* 0x40 */
115 #define HHI_HDMI_CLK_CNTL 0x1cc /* 0x73 */
116 #define HHI_HDMI_PHY_CNTL0 0x3a0 /* 0xe8 */
117 #define HHI_HDMI_PHY_CNTL1 0x3a4 /* 0xe9 */
118 #define HHI_HDMI_PHY_CNTL2 0x3a8 /* 0xea */
119 #define HHI_HDMI_PHY_CNTL3 0x3ac /* 0xeb */
121 static DEFINE_SPINLOCK(reg_lock);
123 enum meson_venc_source {
124 MESON_VENC_SOURCE_NONE = 0,
125 MESON_VENC_SOURCE_ENCI = 1,
126 MESON_VENC_SOURCE_ENCP = 2,
129 struct meson_dw_hdmi {
130 struct drm_encoder encoder;
131 struct dw_hdmi_plat_data dw_plat_data;
132 struct meson_drm *priv;
134 void __iomem *hdmitx;
135 struct reset_control *hdmitx_apb;
136 struct reset_control *hdmitx_ctrl;
137 struct reset_control *hdmitx_phy;
138 struct clk *hdmi_pclk;
139 struct clk *venci_clk;
142 #define encoder_to_meson_dw_hdmi(x) \
143 container_of(x, struct meson_dw_hdmi, encoder)
145 static inline int dw_hdmi_is_compatible(struct meson_dw_hdmi *dw_hdmi,
148 return of_device_is_compatible(dw_hdmi->dev->of_node, compat);
151 /* PHY (via TOP bridge) and Controller dedicated register interface */
153 static unsigned int dw_hdmi_top_read(struct meson_dw_hdmi *dw_hdmi,
159 spin_lock_irqsave(®_lock, flags);
161 /* ADDR must be written twice */
162 writel(addr & 0xffff, dw_hdmi->hdmitx + HDMITX_TOP_ADDR_REG);
163 writel(addr & 0xffff, dw_hdmi->hdmitx + HDMITX_TOP_ADDR_REG);
165 /* Read needs a second DATA read */
166 data = readl(dw_hdmi->hdmitx + HDMITX_TOP_DATA_REG);
167 data = readl(dw_hdmi->hdmitx + HDMITX_TOP_DATA_REG);
169 spin_unlock_irqrestore(®_lock, flags);
174 static inline void dw_hdmi_top_write(struct meson_dw_hdmi *dw_hdmi,
175 unsigned int addr, unsigned int data)
179 spin_lock_irqsave(®_lock, flags);
181 /* ADDR must be written twice */
182 writel(addr & 0xffff, dw_hdmi->hdmitx + HDMITX_TOP_ADDR_REG);
183 writel(addr & 0xffff, dw_hdmi->hdmitx + HDMITX_TOP_ADDR_REG);
185 /* Write needs single DATA write */
186 writel(data, dw_hdmi->hdmitx + HDMITX_TOP_DATA_REG);
188 spin_unlock_irqrestore(®_lock, flags);
191 /* Helper to change specific bits in PHY registers */
192 static inline void dw_hdmi_top_write_bits(struct meson_dw_hdmi *dw_hdmi,
197 unsigned int data = dw_hdmi_top_read(dw_hdmi, addr);
202 dw_hdmi_top_write(dw_hdmi, addr, data);
205 static unsigned int dw_hdmi_dwc_read(struct meson_dw_hdmi *dw_hdmi,
211 spin_lock_irqsave(®_lock, flags);
213 /* ADDR must be written twice */
214 writel(addr & 0xffff, dw_hdmi->hdmitx + HDMITX_DWC_ADDR_REG);
215 writel(addr & 0xffff, dw_hdmi->hdmitx + HDMITX_DWC_ADDR_REG);
217 /* Read needs a second DATA read */
218 data = readl(dw_hdmi->hdmitx + HDMITX_DWC_DATA_REG);
219 data = readl(dw_hdmi->hdmitx + HDMITX_DWC_DATA_REG);
221 spin_unlock_irqrestore(®_lock, flags);
226 static inline void dw_hdmi_dwc_write(struct meson_dw_hdmi *dw_hdmi,
227 unsigned int addr, unsigned int data)
231 spin_lock_irqsave(®_lock, flags);
233 /* ADDR must be written twice */
234 writel(addr & 0xffff, dw_hdmi->hdmitx + HDMITX_DWC_ADDR_REG);
235 writel(addr & 0xffff, dw_hdmi->hdmitx + HDMITX_DWC_ADDR_REG);
237 /* Write needs single DATA write */
238 writel(data, dw_hdmi->hdmitx + HDMITX_DWC_DATA_REG);
240 spin_unlock_irqrestore(®_lock, flags);
243 /* Helper to change specific bits in controller registers */
244 static inline void dw_hdmi_dwc_write_bits(struct meson_dw_hdmi *dw_hdmi,
249 unsigned int data = dw_hdmi_dwc_read(dw_hdmi, addr);
254 dw_hdmi_dwc_write(dw_hdmi, addr, data);
259 /* Setup PHY bandwidth modes */
260 static void meson_hdmi_phy_setup_mode(struct meson_dw_hdmi *dw_hdmi,
261 struct drm_display_mode *mode)
263 struct meson_drm *priv = dw_hdmi->priv;
264 unsigned int pixel_clock = mode->clock;
266 if (dw_hdmi_is_compatible(dw_hdmi, "amlogic,meson-gxl-dw-hdmi") ||
267 dw_hdmi_is_compatible(dw_hdmi, "amlogic,meson-gxm-dw-hdmi")) {
268 if (pixel_clock >= 371250) {
269 /* 5.94Gbps, 3.7125Gbps */
270 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL0, 0x333d3282);
271 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL3, 0x2136315b);
272 } else if (pixel_clock >= 297000) {
274 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL0, 0x33303382);
275 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL3, 0x2036315b);
276 } else if (pixel_clock >= 148500) {
278 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL0, 0x33303362);
279 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL3, 0x2016315b);
281 /* 742.5Mbps, and below */
282 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL0, 0x33604142);
283 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL3, 0x0016315b);
285 } else if (dw_hdmi_is_compatible(dw_hdmi,
286 "amlogic,meson-gxbb-dw-hdmi")) {
287 if (pixel_clock >= 371250) {
288 /* 5.94Gbps, 3.7125Gbps */
289 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL0, 0x33353245);
290 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL3, 0x2100115b);
291 } else if (pixel_clock >= 297000) {
293 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL0, 0x33634283);
294 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL3, 0xb000115b);
296 /* 1.485Gbps, and below */
297 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL0, 0x33632122);
298 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL3, 0x2000115b);
303 static inline void dw_hdmi_phy_reset(struct meson_dw_hdmi *dw_hdmi)
305 struct meson_drm *priv = dw_hdmi->priv;
307 /* Enable and software reset */
308 regmap_update_bits(priv->hhi, HHI_HDMI_PHY_CNTL1, 0xf, 0xf);
312 /* Enable and unreset */
313 regmap_update_bits(priv->hhi, HHI_HDMI_PHY_CNTL1, 0xf, 0xe);
318 static void dw_hdmi_set_vclk(struct meson_dw_hdmi *dw_hdmi,
319 struct drm_display_mode *mode)
321 struct meson_drm *priv = dw_hdmi->priv;
322 int vic = drm_match_cea_mode(mode);
323 unsigned int vclk_freq;
324 unsigned int venc_freq;
325 unsigned int hdmi_freq;
327 vclk_freq = mode->clock;
329 if (mode->flags & DRM_MODE_FLAG_DBLCLK)
332 venc_freq = vclk_freq;
333 hdmi_freq = vclk_freq;
335 if (meson_venc_hdmi_venc_repeat(vic))
338 vclk_freq = max(venc_freq, hdmi_freq);
340 if (mode->flags & DRM_MODE_FLAG_DBLCLK)
343 DRM_DEBUG_DRIVER("vclk:%d venc=%d hdmi=%d enci=%d\n",
344 vclk_freq, venc_freq, hdmi_freq,
345 priv->venc.hdmi_use_enci);
347 meson_vclk_setup(priv, MESON_VCLK_TARGET_HDMI, vclk_freq,
348 venc_freq, hdmi_freq, priv->venc.hdmi_use_enci);
351 static int dw_hdmi_phy_init(struct dw_hdmi *hdmi, void *data,
352 struct drm_display_mode *mode)
354 struct meson_dw_hdmi *dw_hdmi = (struct meson_dw_hdmi *)data;
355 struct meson_drm *priv = dw_hdmi->priv;
356 unsigned int wr_clk =
357 readl_relaxed(priv->io_base + _REG(VPU_HDMI_SETTING));
359 DRM_DEBUG_DRIVER("%d:\"%s\"\n", mode->base.id, mode->name);
362 regmap_update_bits(priv->hhi, HHI_HDMI_CLK_CNTL, 0xffff, 0x100);
364 /* Bring HDMITX MEM output of power down */
365 regmap_update_bits(priv->hhi, HHI_MEM_PD_REG0, 0xff << 8, 0);
367 /* Bring out of reset */
368 dw_hdmi_top_write(dw_hdmi, HDMITX_TOP_SW_RESET, 0);
370 /* Enable internal pixclk, tmds_clk, spdif_clk, i2s_clk, cecclk */
371 dw_hdmi_top_write_bits(dw_hdmi, HDMITX_TOP_CLK_CNTL,
373 dw_hdmi_top_write_bits(dw_hdmi, HDMITX_TOP_CLK_CNTL,
376 /* Enable normal output to PHY */
377 dw_hdmi_top_write(dw_hdmi, HDMITX_TOP_BIST_CNTL, BIT(12));
379 /* TMDS pattern setup (TOFIX pattern for 4k2k scrambling) */
380 dw_hdmi_top_write(dw_hdmi, HDMITX_TOP_TMDS_CLK_PTTN_01, 0x001f001f);
381 dw_hdmi_top_write(dw_hdmi, HDMITX_TOP_TMDS_CLK_PTTN_23, 0x001f001f);
383 /* Load TMDS pattern */
384 dw_hdmi_top_write(dw_hdmi, HDMITX_TOP_TMDS_CLK_PTTN_CNTL, 0x1);
386 dw_hdmi_top_write(dw_hdmi, HDMITX_TOP_TMDS_CLK_PTTN_CNTL, 0x2);
388 /* Setup PHY parameters */
389 meson_hdmi_phy_setup_mode(dw_hdmi, mode);
392 regmap_update_bits(priv->hhi, HHI_HDMI_PHY_CNTL1,
393 0xffff << 16, 0x0390 << 16);
396 if (dw_hdmi_is_compatible(dw_hdmi, "amlogic,meson-gxl-dw-hdmi") ||
397 dw_hdmi_is_compatible(dw_hdmi, "amlogic,meson-gxm-dw-hdmi"))
398 regmap_update_bits(priv->hhi, HHI_HDMI_PHY_CNTL1,
401 regmap_update_bits(priv->hhi, HHI_HDMI_PHY_CNTL1,
404 /* Disable clock, fifo, fifo_wr */
405 regmap_update_bits(priv->hhi, HHI_HDMI_PHY_CNTL1, 0xf, 0);
409 /* Reset PHY 3 times in a row */
410 dw_hdmi_phy_reset(dw_hdmi);
411 dw_hdmi_phy_reset(dw_hdmi);
412 dw_hdmi_phy_reset(dw_hdmi);
414 /* Temporary Disable VENC video stream */
415 if (priv->venc.hdmi_use_enci)
416 writel_relaxed(0, priv->io_base + _REG(ENCI_VIDEO_EN));
418 writel_relaxed(0, priv->io_base + _REG(ENCP_VIDEO_EN));
420 /* Temporary Disable HDMI video stream to HDMI-TX */
421 writel_bits_relaxed(0x3, 0,
422 priv->io_base + _REG(VPU_HDMI_SETTING));
423 writel_bits_relaxed(0xf << 8, 0,
424 priv->io_base + _REG(VPU_HDMI_SETTING));
426 /* Re-Enable VENC video stream */
427 if (priv->venc.hdmi_use_enci)
428 writel_relaxed(1, priv->io_base + _REG(ENCI_VIDEO_EN));
430 writel_relaxed(1, priv->io_base + _REG(ENCP_VIDEO_EN));
432 /* Push back HDMI clock settings */
433 writel_bits_relaxed(0xf << 8, wr_clk & (0xf << 8),
434 priv->io_base + _REG(VPU_HDMI_SETTING));
436 /* Enable and Select HDMI video source for HDMI-TX */
437 if (priv->venc.hdmi_use_enci)
438 writel_bits_relaxed(0x3, MESON_VENC_SOURCE_ENCI,
439 priv->io_base + _REG(VPU_HDMI_SETTING));
441 writel_bits_relaxed(0x3, MESON_VENC_SOURCE_ENCP,
442 priv->io_base + _REG(VPU_HDMI_SETTING));
447 static void dw_hdmi_phy_disable(struct dw_hdmi *hdmi,
450 struct meson_dw_hdmi *dw_hdmi = (struct meson_dw_hdmi *)data;
451 struct meson_drm *priv = dw_hdmi->priv;
453 DRM_DEBUG_DRIVER("\n");
455 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL0, 0);
458 static enum drm_connector_status dw_hdmi_read_hpd(struct dw_hdmi *hdmi,
461 struct meson_dw_hdmi *dw_hdmi = (struct meson_dw_hdmi *)data;
463 return !!dw_hdmi_top_read(dw_hdmi, HDMITX_TOP_STAT0) ?
464 connector_status_connected : connector_status_disconnected;
467 static void dw_hdmi_setup_hpd(struct dw_hdmi *hdmi,
470 struct meson_dw_hdmi *dw_hdmi = (struct meson_dw_hdmi *)data;
472 /* Setup HPD Filter */
473 dw_hdmi_top_write(dw_hdmi, HDMITX_TOP_HPD_FILTER,
476 /* Clear interrupts */
477 dw_hdmi_top_write(dw_hdmi, HDMITX_TOP_INTR_STAT_CLR,
478 HDMITX_TOP_INTR_HPD_RISE | HDMITX_TOP_INTR_HPD_FALL);
480 /* Unmask interrupts */
481 dw_hdmi_top_write_bits(dw_hdmi, HDMITX_TOP_INTR_MASKN,
482 HDMITX_TOP_INTR_HPD_RISE | HDMITX_TOP_INTR_HPD_FALL,
483 HDMITX_TOP_INTR_HPD_RISE | HDMITX_TOP_INTR_HPD_FALL);
486 static const struct dw_hdmi_phy_ops meson_dw_hdmi_phy_ops = {
487 .init = dw_hdmi_phy_init,
488 .disable = dw_hdmi_phy_disable,
489 .read_hpd = dw_hdmi_read_hpd,
490 .setup_hpd = dw_hdmi_setup_hpd,
493 static irqreturn_t dw_hdmi_top_irq(int irq, void *dev_id)
495 struct meson_dw_hdmi *dw_hdmi = dev_id;
498 stat = dw_hdmi_top_read(dw_hdmi, HDMITX_TOP_INTR_STAT);
499 dw_hdmi_top_write(dw_hdmi, HDMITX_TOP_INTR_STAT_CLR, stat);
501 /* HPD Events, handle in the threaded interrupt handler */
502 if (stat & (HDMITX_TOP_INTR_HPD_RISE | HDMITX_TOP_INTR_HPD_FALL)) {
503 dw_hdmi->irq_stat = stat;
504 return IRQ_WAKE_THREAD;
507 /* HDMI Controller Interrupt */
511 /* TOFIX Handle HDCP Interrupts */
516 /* Threaded interrupt handler to manage HPD events */
517 static irqreturn_t dw_hdmi_top_thread_irq(int irq, void *dev_id)
519 struct meson_dw_hdmi *dw_hdmi = dev_id;
520 u32 stat = dw_hdmi->irq_stat;
523 if (stat & (HDMITX_TOP_INTR_HPD_RISE | HDMITX_TOP_INTR_HPD_FALL)) {
524 bool hpd_connected = false;
526 if (stat & HDMITX_TOP_INTR_HPD_RISE)
527 hpd_connected = true;
529 dw_hdmi_setup_rx_sense(dw_hdmi->dev, hpd_connected,
532 drm_helper_hpd_irq_event(dw_hdmi->encoder.dev);
538 /* TOFIX Enable support for non-vic modes */
539 static enum drm_mode_status
540 dw_hdmi_mode_valid(struct drm_connector *connector,
541 const struct drm_display_mode *mode)
543 unsigned int vclk_freq;
544 unsigned int venc_freq;
545 unsigned int hdmi_freq;
546 int vic = drm_match_cea_mode(mode);
548 DRM_DEBUG_DRIVER("Modeline %d:\"%s\" %d %d %d %d %d %d %d %d %d %d 0x%x 0x%x\n",
549 mode->base.id, mode->name, mode->vrefresh, mode->clock,
550 mode->hdisplay, mode->hsync_start,
551 mode->hsync_end, mode->htotal,
552 mode->vdisplay, mode->vsync_start,
553 mode->vsync_end, mode->vtotal, mode->type, mode->flags);
555 /* For now, only accept VIC modes */
559 /* For now, filter by supported VIC modes */
560 if (!meson_venc_hdmi_supported_vic(vic))
563 vclk_freq = mode->clock;
565 /* 480i/576i needs global pixel doubling */
566 if (mode->flags & DRM_MODE_FLAG_DBLCLK)
569 venc_freq = vclk_freq;
570 hdmi_freq = vclk_freq;
572 /* VENC double pixels for 1080i and 720p modes */
573 if (meson_venc_hdmi_venc_repeat(vic))
576 vclk_freq = max(venc_freq, hdmi_freq);
578 if (mode->flags & DRM_MODE_FLAG_DBLCLK)
581 dev_dbg(connector->dev->dev, "%s: vclk:%d venc=%d hdmi=%d\n", __func__,
582 vclk_freq, venc_freq, hdmi_freq);
584 /* Finally filter by configurable vclk frequencies */
594 return MODE_CLOCK_RANGE;
599 static void meson_venc_hdmi_encoder_destroy(struct drm_encoder *encoder)
601 drm_encoder_cleanup(encoder);
604 static const struct drm_encoder_funcs meson_venc_hdmi_encoder_funcs = {
605 .destroy = meson_venc_hdmi_encoder_destroy,
608 static int meson_venc_hdmi_encoder_atomic_check(struct drm_encoder *encoder,
609 struct drm_crtc_state *crtc_state,
610 struct drm_connector_state *conn_state)
615 static void meson_venc_hdmi_encoder_disable(struct drm_encoder *encoder)
617 struct meson_dw_hdmi *dw_hdmi = encoder_to_meson_dw_hdmi(encoder);
618 struct meson_drm *priv = dw_hdmi->priv;
620 DRM_DEBUG_DRIVER("\n");
622 writel_bits_relaxed(0x3, 0,
623 priv->io_base + _REG(VPU_HDMI_SETTING));
625 writel_relaxed(0, priv->io_base + _REG(ENCI_VIDEO_EN));
626 writel_relaxed(0, priv->io_base + _REG(ENCP_VIDEO_EN));
629 static void meson_venc_hdmi_encoder_enable(struct drm_encoder *encoder)
631 struct meson_dw_hdmi *dw_hdmi = encoder_to_meson_dw_hdmi(encoder);
632 struct meson_drm *priv = dw_hdmi->priv;
634 DRM_DEBUG_DRIVER("%s\n", priv->venc.hdmi_use_enci ? "VENCI" : "VENCP");
636 if (priv->venc.hdmi_use_enci)
637 writel_relaxed(1, priv->io_base + _REG(ENCI_VIDEO_EN));
639 writel_relaxed(1, priv->io_base + _REG(ENCP_VIDEO_EN));
642 static void meson_venc_hdmi_encoder_mode_set(struct drm_encoder *encoder,
643 struct drm_display_mode *mode,
644 struct drm_display_mode *adjusted_mode)
646 struct meson_dw_hdmi *dw_hdmi = encoder_to_meson_dw_hdmi(encoder);
647 struct meson_drm *priv = dw_hdmi->priv;
648 int vic = drm_match_cea_mode(mode);
650 DRM_DEBUG_DRIVER("%d:\"%s\" vic %d\n",
651 mode->base.id, mode->name, vic);
653 /* Should have been filtered */
657 /* VENC + VENC-DVI Mode setup */
658 meson_venc_hdmi_mode_set(priv, vic, mode);
661 dw_hdmi_set_vclk(dw_hdmi, mode);
663 /* Setup YUV444 to HDMI-TX, no 10bit diphering */
664 writel_relaxed(0, priv->io_base + _REG(VPU_HDMI_FMT_CTRL));
667 static const struct drm_encoder_helper_funcs
668 meson_venc_hdmi_encoder_helper_funcs = {
669 .atomic_check = meson_venc_hdmi_encoder_atomic_check,
670 .disable = meson_venc_hdmi_encoder_disable,
671 .enable = meson_venc_hdmi_encoder_enable,
672 .mode_set = meson_venc_hdmi_encoder_mode_set,
677 static int meson_dw_hdmi_reg_read(void *context, unsigned int reg,
678 unsigned int *result)
680 *result = dw_hdmi_dwc_read(context, reg);
686 static int meson_dw_hdmi_reg_write(void *context, unsigned int reg,
689 dw_hdmi_dwc_write(context, reg, val);
694 static const struct regmap_config meson_dw_hdmi_regmap_config = {
697 .reg_read = meson_dw_hdmi_reg_read,
698 .reg_write = meson_dw_hdmi_reg_write,
699 .max_register = 0x10000,
703 static bool meson_hdmi_connector_is_available(struct device *dev)
705 struct device_node *ep, *remote;
707 /* HDMI Connector is on the second port, first endpoint */
708 ep = of_graph_get_endpoint_by_regs(dev->of_node, 1, 0);
712 /* If the endpoint node exists, consider it enabled */
713 remote = of_graph_get_remote_port(ep);
725 static int meson_dw_hdmi_bind(struct device *dev, struct device *master,
728 struct platform_device *pdev = to_platform_device(dev);
729 struct meson_dw_hdmi *meson_dw_hdmi;
730 struct drm_device *drm = data;
731 struct meson_drm *priv = drm->dev_private;
732 struct dw_hdmi_plat_data *dw_plat_data;
733 struct drm_encoder *encoder;
734 struct resource *res;
738 DRM_DEBUG_DRIVER("\n");
740 if (!meson_hdmi_connector_is_available(dev)) {
741 dev_info(drm->dev, "HDMI Output connector not available\n");
745 meson_dw_hdmi = devm_kzalloc(dev, sizeof(*meson_dw_hdmi),
750 meson_dw_hdmi->priv = priv;
751 meson_dw_hdmi->dev = dev;
752 dw_plat_data = &meson_dw_hdmi->dw_plat_data;
753 encoder = &meson_dw_hdmi->encoder;
755 meson_dw_hdmi->hdmitx_apb = devm_reset_control_get_exclusive(dev,
757 if (IS_ERR(meson_dw_hdmi->hdmitx_apb)) {
758 dev_err(dev, "Failed to get hdmitx_apb reset\n");
759 return PTR_ERR(meson_dw_hdmi->hdmitx_apb);
762 meson_dw_hdmi->hdmitx_ctrl = devm_reset_control_get_exclusive(dev,
764 if (IS_ERR(meson_dw_hdmi->hdmitx_ctrl)) {
765 dev_err(dev, "Failed to get hdmitx reset\n");
766 return PTR_ERR(meson_dw_hdmi->hdmitx_ctrl);
769 meson_dw_hdmi->hdmitx_phy = devm_reset_control_get_exclusive(dev,
771 if (IS_ERR(meson_dw_hdmi->hdmitx_phy)) {
772 dev_err(dev, "Failed to get hdmitx_phy reset\n");
773 return PTR_ERR(meson_dw_hdmi->hdmitx_phy);
776 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
777 meson_dw_hdmi->hdmitx = devm_ioremap_resource(dev, res);
778 if (IS_ERR(meson_dw_hdmi->hdmitx))
779 return PTR_ERR(meson_dw_hdmi->hdmitx);
781 meson_dw_hdmi->hdmi_pclk = devm_clk_get(dev, "isfr");
782 if (IS_ERR(meson_dw_hdmi->hdmi_pclk)) {
783 dev_err(dev, "Unable to get HDMI pclk\n");
784 return PTR_ERR(meson_dw_hdmi->hdmi_pclk);
786 clk_prepare_enable(meson_dw_hdmi->hdmi_pclk);
788 meson_dw_hdmi->venci_clk = devm_clk_get(dev, "venci");
789 if (IS_ERR(meson_dw_hdmi->venci_clk)) {
790 dev_err(dev, "Unable to get venci clk\n");
791 return PTR_ERR(meson_dw_hdmi->venci_clk);
793 clk_prepare_enable(meson_dw_hdmi->venci_clk);
795 dw_plat_data->regm = devm_regmap_init(dev, NULL, meson_dw_hdmi,
796 &meson_dw_hdmi_regmap_config);
797 if (IS_ERR(dw_plat_data->regm))
798 return PTR_ERR(dw_plat_data->regm);
800 irq = platform_get_irq(pdev, 0);
802 dev_err(dev, "Failed to get hdmi top irq\n");
806 ret = devm_request_threaded_irq(dev, irq, dw_hdmi_top_irq,
807 dw_hdmi_top_thread_irq, IRQF_SHARED,
808 "dw_hdmi_top_irq", meson_dw_hdmi);
810 dev_err(dev, "Failed to request hdmi top irq\n");
816 drm_encoder_helper_add(encoder, &meson_venc_hdmi_encoder_helper_funcs);
818 ret = drm_encoder_init(drm, encoder, &meson_venc_hdmi_encoder_funcs,
819 DRM_MODE_ENCODER_TMDS, "meson_hdmi");
821 dev_err(priv->dev, "Failed to init HDMI encoder\n");
825 encoder->possible_crtcs = BIT(0);
827 DRM_DEBUG_DRIVER("encoder initialized\n");
830 regmap_update_bits(priv->hhi, HHI_HDMI_CLK_CNTL, 0xffff, 0x100);
832 /* Bring HDMITX MEM output of power down */
833 regmap_update_bits(priv->hhi, HHI_MEM_PD_REG0, 0xff << 8, 0);
835 /* Reset HDMITX APB & TX & PHY */
836 reset_control_reset(meson_dw_hdmi->hdmitx_apb);
837 reset_control_reset(meson_dw_hdmi->hdmitx_ctrl);
838 reset_control_reset(meson_dw_hdmi->hdmitx_phy);
840 /* Enable APB3 fail on error */
841 writel_bits_relaxed(BIT(15), BIT(15),
842 meson_dw_hdmi->hdmitx + HDMITX_TOP_CTRL_REG);
843 writel_bits_relaxed(BIT(15), BIT(15),
844 meson_dw_hdmi->hdmitx + HDMITX_DWC_CTRL_REG);
846 /* Bring out of reset */
847 dw_hdmi_top_write(meson_dw_hdmi, HDMITX_TOP_SW_RESET, 0);
851 dw_hdmi_top_write(meson_dw_hdmi, HDMITX_TOP_CLK_CNTL, 0xff);
853 /* Enable HDMI-TX Interrupt */
854 dw_hdmi_top_write(meson_dw_hdmi, HDMITX_TOP_INTR_STAT_CLR,
855 HDMITX_TOP_INTR_CORE);
857 dw_hdmi_top_write(meson_dw_hdmi, HDMITX_TOP_INTR_MASKN,
858 HDMITX_TOP_INTR_CORE);
860 /* Bridge / Connector */
862 dw_plat_data->mode_valid = dw_hdmi_mode_valid;
863 dw_plat_data->phy_ops = &meson_dw_hdmi_phy_ops;
864 dw_plat_data->phy_name = "meson_dw_hdmi_phy";
865 dw_plat_data->phy_data = meson_dw_hdmi;
866 dw_plat_data->input_bus_format = MEDIA_BUS_FMT_YUV8_1X24;
867 dw_plat_data->input_bus_encoding = V4L2_YCBCR_ENC_709;
869 ret = dw_hdmi_bind(pdev, encoder, &meson_dw_hdmi->dw_plat_data);
873 DRM_DEBUG_DRIVER("HDMI controller initialized\n");
878 static void meson_dw_hdmi_unbind(struct device *dev, struct device *master,
884 static const struct component_ops meson_dw_hdmi_ops = {
885 .bind = meson_dw_hdmi_bind,
886 .unbind = meson_dw_hdmi_unbind,
889 static int meson_dw_hdmi_probe(struct platform_device *pdev)
891 return component_add(&pdev->dev, &meson_dw_hdmi_ops);
894 static int meson_dw_hdmi_remove(struct platform_device *pdev)
896 component_del(&pdev->dev, &meson_dw_hdmi_ops);
901 static const struct of_device_id meson_dw_hdmi_of_table[] = {
902 { .compatible = "amlogic,meson-gxbb-dw-hdmi" },
903 { .compatible = "amlogic,meson-gxl-dw-hdmi" },
904 { .compatible = "amlogic,meson-gxm-dw-hdmi" },
907 MODULE_DEVICE_TABLE(of, meson_dw_hdmi_of_table);
909 static struct platform_driver meson_dw_hdmi_platform_driver = {
910 .probe = meson_dw_hdmi_probe,
911 .remove = meson_dw_hdmi_remove,
914 .of_match_table = meson_dw_hdmi_of_table,
917 module_platform_driver(meson_dw_hdmi_platform_driver);
919 MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
920 MODULE_DESCRIPTION(DRIVER_DESC);
921 MODULE_LICENSE("GPL");