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_device.h>
24 #include <linux/of_graph.h>
25 #include <linux/reset.h>
26 #include <linux/clk.h>
27 #include <linux/regulator/consumer.h>
30 #include <drm/drm_atomic_helper.h>
31 #include <drm/drm_edid.h>
32 #include <drm/drm_probe_helper.h>
33 #include <drm/bridge/dw_hdmi.h>
35 #include <uapi/linux/media-bus-format.h>
36 #include <uapi/linux/videodev2.h>
38 #include "meson_drv.h"
39 #include "meson_venc.h"
40 #include "meson_vclk.h"
41 #include "meson_dw_hdmi.h"
42 #include "meson_registers.h"
44 #define DRIVER_NAME "meson-dw-hdmi"
45 #define DRIVER_DESC "Amlogic Meson HDMI-TX DRM driver"
50 * HDMI Output is composed of :
52 * - A Synopsys DesignWare HDMI Controller IP
53 * - A TOP control block controlling the Clocks and PHY
54 * - A custom HDMI PHY in order convert video to TMDS signal
58 * ___________________________________
60 * |___________________________________|
62 * | Synopsys HDMI | HDMI PHY |=> TMDS
63 * | Controller |________________|
64 * |___________________________________|<=> DDC
67 * The HDMI TOP block only supports HPD sensing.
68 * The Synopsys HDMI Controller interrupt is routed
69 * through the TOP Block interrupt.
70 * Communication to the TOP Block and the Synopsys
71 * HDMI Controller is done a pair of addr+read/write
73 * The HDMI PHY is configured by registers in the
76 * Pixel data arrives in 4:4:4 format from the VENC
77 * block and the VPU HDMI mux selects either the ENCI
78 * encoder for the 576i or 480i formats or the ENCP
79 * encoder for all the other formats including
80 * interlaced HD formats.
81 * The VENC uses a DVI encoder on top of the ENCI
82 * or ENCP encoders to generate DVI timings for the
85 * GXBB, GXL and GXM embeds the Synopsys DesignWare
86 * HDMI TX IP version 2.01a with HDCP and I2C & S/PDIF
87 * audio source interfaces.
89 * We handle the following features :
91 * - HPD Rise & Fall interrupt
92 * - HDMI Controller Interrupt
93 * - HDMI PHY Init for 480i to 1080p60
94 * - VENC & HDMI Clock setup for 480i to 1080p60
95 * - VENC Mode setup for 480i to 1080p60
99 * - PHY, Clock and Mode setup for 2k && 4k modes
100 * - SDDC Scrambling mode for HDMI 2.0a
105 /* TOP Block Communication Channel */
106 #define HDMITX_TOP_ADDR_REG 0x0
107 #define HDMITX_TOP_DATA_REG 0x4
108 #define HDMITX_TOP_CTRL_REG 0x8
109 #define HDMITX_TOP_G12A_OFFSET 0x8000
111 /* Controller Communication Channel */
112 #define HDMITX_DWC_ADDR_REG 0x10
113 #define HDMITX_DWC_DATA_REG 0x14
114 #define HDMITX_DWC_CTRL_REG 0x18
117 #define HHI_MEM_PD_REG0 0x100 /* 0x40 */
118 #define HHI_HDMI_CLK_CNTL 0x1cc /* 0x73 */
119 #define HHI_HDMI_PHY_CNTL0 0x3a0 /* 0xe8 */
120 #define HHI_HDMI_PHY_CNTL1 0x3a4 /* 0xe9 */
121 #define HHI_HDMI_PHY_CNTL2 0x3a8 /* 0xea */
122 #define HHI_HDMI_PHY_CNTL3 0x3ac /* 0xeb */
123 #define HHI_HDMI_PHY_CNTL4 0x3b0 /* 0xec */
124 #define HHI_HDMI_PHY_CNTL5 0x3b4 /* 0xed */
126 static DEFINE_SPINLOCK(reg_lock);
128 enum meson_venc_source {
129 MESON_VENC_SOURCE_NONE = 0,
130 MESON_VENC_SOURCE_ENCI = 1,
131 MESON_VENC_SOURCE_ENCP = 2,
134 struct meson_dw_hdmi;
136 struct meson_dw_hdmi_data {
137 unsigned int (*top_read)(struct meson_dw_hdmi *dw_hdmi,
139 void (*top_write)(struct meson_dw_hdmi *dw_hdmi,
140 unsigned int addr, unsigned int data);
141 unsigned int (*dwc_read)(struct meson_dw_hdmi *dw_hdmi,
143 void (*dwc_write)(struct meson_dw_hdmi *dw_hdmi,
144 unsigned int addr, unsigned int data);
147 struct meson_dw_hdmi {
148 struct drm_encoder encoder;
149 struct dw_hdmi_plat_data dw_plat_data;
150 struct meson_drm *priv;
152 void __iomem *hdmitx;
153 const struct meson_dw_hdmi_data *data;
154 struct reset_control *hdmitx_apb;
155 struct reset_control *hdmitx_ctrl;
156 struct reset_control *hdmitx_phy;
157 struct clk *hdmi_pclk;
158 struct clk *venci_clk;
159 struct regulator *hdmi_supply;
161 struct dw_hdmi *hdmi;
163 #define encoder_to_meson_dw_hdmi(x) \
164 container_of(x, struct meson_dw_hdmi, encoder)
166 static inline int dw_hdmi_is_compatible(struct meson_dw_hdmi *dw_hdmi,
169 return of_device_is_compatible(dw_hdmi->dev->of_node, compat);
172 /* PHY (via TOP bridge) and Controller dedicated register interface */
174 static unsigned int dw_hdmi_top_read(struct meson_dw_hdmi *dw_hdmi,
180 spin_lock_irqsave(®_lock, flags);
182 /* ADDR must be written twice */
183 writel(addr & 0xffff, dw_hdmi->hdmitx + HDMITX_TOP_ADDR_REG);
184 writel(addr & 0xffff, dw_hdmi->hdmitx + HDMITX_TOP_ADDR_REG);
186 /* Read needs a second DATA read */
187 data = readl(dw_hdmi->hdmitx + HDMITX_TOP_DATA_REG);
188 data = readl(dw_hdmi->hdmitx + HDMITX_TOP_DATA_REG);
190 spin_unlock_irqrestore(®_lock, flags);
195 static unsigned int dw_hdmi_g12a_top_read(struct meson_dw_hdmi *dw_hdmi,
198 return readl(dw_hdmi->hdmitx + HDMITX_TOP_G12A_OFFSET + (addr << 2));
201 static inline void dw_hdmi_top_write(struct meson_dw_hdmi *dw_hdmi,
202 unsigned int addr, unsigned int data)
206 spin_lock_irqsave(®_lock, flags);
208 /* ADDR must be written twice */
209 writel(addr & 0xffff, dw_hdmi->hdmitx + HDMITX_TOP_ADDR_REG);
210 writel(addr & 0xffff, dw_hdmi->hdmitx + HDMITX_TOP_ADDR_REG);
212 /* Write needs single DATA write */
213 writel(data, dw_hdmi->hdmitx + HDMITX_TOP_DATA_REG);
215 spin_unlock_irqrestore(®_lock, flags);
218 static inline void dw_hdmi_g12a_top_write(struct meson_dw_hdmi *dw_hdmi,
219 unsigned int addr, unsigned int data)
221 writel(data, dw_hdmi->hdmitx + HDMITX_TOP_G12A_OFFSET + (addr << 2));
224 /* Helper to change specific bits in PHY registers */
225 static inline void dw_hdmi_top_write_bits(struct meson_dw_hdmi *dw_hdmi,
230 unsigned int data = dw_hdmi->data->top_read(dw_hdmi, addr);
235 dw_hdmi->data->top_write(dw_hdmi, addr, data);
238 static unsigned int dw_hdmi_dwc_read(struct meson_dw_hdmi *dw_hdmi,
244 spin_lock_irqsave(®_lock, flags);
246 /* ADDR must be written twice */
247 writel(addr & 0xffff, dw_hdmi->hdmitx + HDMITX_DWC_ADDR_REG);
248 writel(addr & 0xffff, dw_hdmi->hdmitx + HDMITX_DWC_ADDR_REG);
250 /* Read needs a second DATA read */
251 data = readl(dw_hdmi->hdmitx + HDMITX_DWC_DATA_REG);
252 data = readl(dw_hdmi->hdmitx + HDMITX_DWC_DATA_REG);
254 spin_unlock_irqrestore(®_lock, flags);
259 static unsigned int dw_hdmi_g12a_dwc_read(struct meson_dw_hdmi *dw_hdmi,
262 return readb(dw_hdmi->hdmitx + addr);
265 static inline void dw_hdmi_dwc_write(struct meson_dw_hdmi *dw_hdmi,
266 unsigned int addr, unsigned int data)
270 spin_lock_irqsave(®_lock, flags);
272 /* ADDR must be written twice */
273 writel(addr & 0xffff, dw_hdmi->hdmitx + HDMITX_DWC_ADDR_REG);
274 writel(addr & 0xffff, dw_hdmi->hdmitx + HDMITX_DWC_ADDR_REG);
276 /* Write needs single DATA write */
277 writel(data, dw_hdmi->hdmitx + HDMITX_DWC_DATA_REG);
279 spin_unlock_irqrestore(®_lock, flags);
282 static inline void dw_hdmi_g12a_dwc_write(struct meson_dw_hdmi *dw_hdmi,
283 unsigned int addr, unsigned int data)
285 writeb(data, dw_hdmi->hdmitx + addr);
288 /* Helper to change specific bits in controller registers */
289 static inline void dw_hdmi_dwc_write_bits(struct meson_dw_hdmi *dw_hdmi,
294 unsigned int data = dw_hdmi->data->dwc_read(dw_hdmi, addr);
299 dw_hdmi->data->dwc_write(dw_hdmi, addr, data);
304 /* Setup PHY bandwidth modes */
305 static void meson_hdmi_phy_setup_mode(struct meson_dw_hdmi *dw_hdmi,
306 struct drm_display_mode *mode)
308 struct meson_drm *priv = dw_hdmi->priv;
309 unsigned int pixel_clock = mode->clock;
311 if (dw_hdmi_is_compatible(dw_hdmi, "amlogic,meson-gxl-dw-hdmi") ||
312 dw_hdmi_is_compatible(dw_hdmi, "amlogic,meson-gxm-dw-hdmi")) {
313 if (pixel_clock >= 371250) {
314 /* 5.94Gbps, 3.7125Gbps */
315 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL0, 0x333d3282);
316 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL3, 0x2136315b);
317 } else if (pixel_clock >= 297000) {
319 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL0, 0x33303382);
320 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL3, 0x2036315b);
321 } else if (pixel_clock >= 148500) {
323 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL0, 0x33303362);
324 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL3, 0x2016315b);
326 /* 742.5Mbps, and below */
327 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL0, 0x33604142);
328 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL3, 0x0016315b);
330 } else if (dw_hdmi_is_compatible(dw_hdmi,
331 "amlogic,meson-gxbb-dw-hdmi")) {
332 if (pixel_clock >= 371250) {
333 /* 5.94Gbps, 3.7125Gbps */
334 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL0, 0x33353245);
335 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL3, 0x2100115b);
336 } else if (pixel_clock >= 297000) {
338 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL0, 0x33634283);
339 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL3, 0xb000115b);
341 /* 1.485Gbps, and below */
342 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL0, 0x33632122);
343 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL3, 0x2000115b);
345 } else if (dw_hdmi_is_compatible(dw_hdmi,
346 "amlogic,meson-g12a-dw-hdmi")) {
347 if (pixel_clock >= 371250) {
348 /* 5.94Gbps, 3.7125Gbps */
349 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL0, 0x37eb65c4);
350 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL3, 0x2ab0ff3b);
351 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL5, 0x0000080b);
352 } else if (pixel_clock >= 297000) {
354 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL0, 0x33eb6262);
355 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL3, 0x2ab0ff3b);
356 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL5, 0x00000003);
358 /* 1.485Gbps, and below */
359 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL0, 0x33eb4242);
360 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL3, 0x2ab0ff3b);
361 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL5, 0x00000003);
366 static inline void meson_dw_hdmi_phy_reset(struct meson_dw_hdmi *dw_hdmi)
368 struct meson_drm *priv = dw_hdmi->priv;
370 /* Enable and software reset */
371 regmap_update_bits(priv->hhi, HHI_HDMI_PHY_CNTL1, 0xf, 0xf);
375 /* Enable and unreset */
376 regmap_update_bits(priv->hhi, HHI_HDMI_PHY_CNTL1, 0xf, 0xe);
381 static void dw_hdmi_set_vclk(struct meson_dw_hdmi *dw_hdmi,
382 struct drm_display_mode *mode)
384 struct meson_drm *priv = dw_hdmi->priv;
385 int vic = drm_match_cea_mode(mode);
386 unsigned int vclk_freq;
387 unsigned int venc_freq;
388 unsigned int hdmi_freq;
390 vclk_freq = mode->clock;
393 meson_vclk_setup(priv, MESON_VCLK_TARGET_DMT, vclk_freq,
394 vclk_freq, vclk_freq, false);
398 if (mode->flags & DRM_MODE_FLAG_DBLCLK)
401 venc_freq = vclk_freq;
402 hdmi_freq = vclk_freq;
404 if (meson_venc_hdmi_venc_repeat(vic))
407 vclk_freq = max(venc_freq, hdmi_freq);
409 if (mode->flags & DRM_MODE_FLAG_DBLCLK)
412 DRM_DEBUG_DRIVER("vclk:%d venc=%d hdmi=%d enci=%d\n",
413 vclk_freq, venc_freq, hdmi_freq,
414 priv->venc.hdmi_use_enci);
416 meson_vclk_setup(priv, MESON_VCLK_TARGET_HDMI, vclk_freq,
417 venc_freq, hdmi_freq, priv->venc.hdmi_use_enci);
420 static int dw_hdmi_phy_init(struct dw_hdmi *hdmi, void *data,
421 struct drm_display_mode *mode)
423 struct meson_dw_hdmi *dw_hdmi = (struct meson_dw_hdmi *)data;
424 struct meson_drm *priv = dw_hdmi->priv;
425 unsigned int wr_clk =
426 readl_relaxed(priv->io_base + _REG(VPU_HDMI_SETTING));
428 DRM_DEBUG_DRIVER("\"%s\" div%d\n", mode->name,
429 mode->clock > 340000 ? 40 : 10);
432 regmap_update_bits(priv->hhi, HHI_HDMI_CLK_CNTL, 0xffff, 0x100);
434 /* Bring HDMITX MEM output of power down */
435 regmap_update_bits(priv->hhi, HHI_MEM_PD_REG0, 0xff << 8, 0);
437 /* Bring out of reset */
438 dw_hdmi->data->top_write(dw_hdmi, HDMITX_TOP_SW_RESET, 0);
440 /* Enable internal pixclk, tmds_clk, spdif_clk, i2s_clk, cecclk */
441 dw_hdmi_top_write_bits(dw_hdmi, HDMITX_TOP_CLK_CNTL,
443 dw_hdmi_top_write_bits(dw_hdmi, HDMITX_TOP_CLK_CNTL,
446 /* Enable normal output to PHY */
447 dw_hdmi->data->top_write(dw_hdmi, HDMITX_TOP_BIST_CNTL, BIT(12));
449 /* TMDS pattern setup (TOFIX Handle the YUV420 case) */
450 if (mode->clock > 340000) {
451 dw_hdmi->data->top_write(dw_hdmi, HDMITX_TOP_TMDS_CLK_PTTN_01,
453 dw_hdmi->data->top_write(dw_hdmi, HDMITX_TOP_TMDS_CLK_PTTN_23,
456 dw_hdmi->data->top_write(dw_hdmi, HDMITX_TOP_TMDS_CLK_PTTN_01,
458 dw_hdmi->data->top_write(dw_hdmi, HDMITX_TOP_TMDS_CLK_PTTN_23,
462 /* Load TMDS pattern */
463 dw_hdmi->data->top_write(dw_hdmi, HDMITX_TOP_TMDS_CLK_PTTN_CNTL, 0x1);
465 dw_hdmi->data->top_write(dw_hdmi, HDMITX_TOP_TMDS_CLK_PTTN_CNTL, 0x2);
467 /* Setup PHY parameters */
468 meson_hdmi_phy_setup_mode(dw_hdmi, mode);
471 regmap_update_bits(priv->hhi, HHI_HDMI_PHY_CNTL1,
472 0xffff << 16, 0x0390 << 16);
475 if (dw_hdmi_is_compatible(dw_hdmi, "amlogic,meson-gxl-dw-hdmi") ||
476 dw_hdmi_is_compatible(dw_hdmi, "amlogic,meson-gxm-dw-hdmi") ||
477 dw_hdmi_is_compatible(dw_hdmi, "amlogic,meson-g12a-dw-hdmi"))
478 regmap_update_bits(priv->hhi, HHI_HDMI_PHY_CNTL1,
481 regmap_update_bits(priv->hhi, HHI_HDMI_PHY_CNTL1,
484 /* Disable clock, fifo, fifo_wr */
485 regmap_update_bits(priv->hhi, HHI_HDMI_PHY_CNTL1, 0xf, 0);
487 dw_hdmi_set_high_tmds_clock_ratio(hdmi);
491 /* Reset PHY 3 times in a row */
492 meson_dw_hdmi_phy_reset(dw_hdmi);
493 meson_dw_hdmi_phy_reset(dw_hdmi);
494 meson_dw_hdmi_phy_reset(dw_hdmi);
496 /* Temporary Disable VENC video stream */
497 if (priv->venc.hdmi_use_enci)
498 writel_relaxed(0, priv->io_base + _REG(ENCI_VIDEO_EN));
500 writel_relaxed(0, priv->io_base + _REG(ENCP_VIDEO_EN));
502 /* Temporary Disable HDMI video stream to HDMI-TX */
503 writel_bits_relaxed(0x3, 0,
504 priv->io_base + _REG(VPU_HDMI_SETTING));
505 writel_bits_relaxed(0xf << 8, 0,
506 priv->io_base + _REG(VPU_HDMI_SETTING));
508 /* Re-Enable VENC video stream */
509 if (priv->venc.hdmi_use_enci)
510 writel_relaxed(1, priv->io_base + _REG(ENCI_VIDEO_EN));
512 writel_relaxed(1, priv->io_base + _REG(ENCP_VIDEO_EN));
514 /* Push back HDMI clock settings */
515 writel_bits_relaxed(0xf << 8, wr_clk & (0xf << 8),
516 priv->io_base + _REG(VPU_HDMI_SETTING));
518 /* Enable and Select HDMI video source for HDMI-TX */
519 if (priv->venc.hdmi_use_enci)
520 writel_bits_relaxed(0x3, MESON_VENC_SOURCE_ENCI,
521 priv->io_base + _REG(VPU_HDMI_SETTING));
523 writel_bits_relaxed(0x3, MESON_VENC_SOURCE_ENCP,
524 priv->io_base + _REG(VPU_HDMI_SETTING));
529 static void dw_hdmi_phy_disable(struct dw_hdmi *hdmi,
532 struct meson_dw_hdmi *dw_hdmi = (struct meson_dw_hdmi *)data;
533 struct meson_drm *priv = dw_hdmi->priv;
535 DRM_DEBUG_DRIVER("\n");
537 regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL0, 0);
540 static enum drm_connector_status dw_hdmi_read_hpd(struct dw_hdmi *hdmi,
543 struct meson_dw_hdmi *dw_hdmi = (struct meson_dw_hdmi *)data;
545 return !!dw_hdmi->data->top_read(dw_hdmi, HDMITX_TOP_STAT0) ?
546 connector_status_connected : connector_status_disconnected;
549 static void dw_hdmi_setup_hpd(struct dw_hdmi *hdmi,
552 struct meson_dw_hdmi *dw_hdmi = (struct meson_dw_hdmi *)data;
554 /* Setup HPD Filter */
555 dw_hdmi->data->top_write(dw_hdmi, HDMITX_TOP_HPD_FILTER,
558 /* Clear interrupts */
559 dw_hdmi->data->top_write(dw_hdmi, HDMITX_TOP_INTR_STAT_CLR,
560 HDMITX_TOP_INTR_HPD_RISE | HDMITX_TOP_INTR_HPD_FALL);
562 /* Unmask interrupts */
563 dw_hdmi_top_write_bits(dw_hdmi, HDMITX_TOP_INTR_MASKN,
564 HDMITX_TOP_INTR_HPD_RISE | HDMITX_TOP_INTR_HPD_FALL,
565 HDMITX_TOP_INTR_HPD_RISE | HDMITX_TOP_INTR_HPD_FALL);
568 static const struct dw_hdmi_phy_ops meson_dw_hdmi_phy_ops = {
569 .init = dw_hdmi_phy_init,
570 .disable = dw_hdmi_phy_disable,
571 .read_hpd = dw_hdmi_read_hpd,
572 .setup_hpd = dw_hdmi_setup_hpd,
575 static irqreturn_t dw_hdmi_top_irq(int irq, void *dev_id)
577 struct meson_dw_hdmi *dw_hdmi = dev_id;
580 stat = dw_hdmi->data->top_read(dw_hdmi, HDMITX_TOP_INTR_STAT);
581 dw_hdmi->data->top_write(dw_hdmi, HDMITX_TOP_INTR_STAT_CLR, stat);
583 /* HPD Events, handle in the threaded interrupt handler */
584 if (stat & (HDMITX_TOP_INTR_HPD_RISE | HDMITX_TOP_INTR_HPD_FALL)) {
585 dw_hdmi->irq_stat = stat;
586 return IRQ_WAKE_THREAD;
589 /* HDMI Controller Interrupt */
593 /* TOFIX Handle HDCP Interrupts */
598 /* Threaded interrupt handler to manage HPD events */
599 static irqreturn_t dw_hdmi_top_thread_irq(int irq, void *dev_id)
601 struct meson_dw_hdmi *dw_hdmi = dev_id;
602 u32 stat = dw_hdmi->irq_stat;
605 if (stat & (HDMITX_TOP_INTR_HPD_RISE | HDMITX_TOP_INTR_HPD_FALL)) {
606 bool hpd_connected = false;
608 if (stat & HDMITX_TOP_INTR_HPD_RISE)
609 hpd_connected = true;
611 dw_hdmi_setup_rx_sense(dw_hdmi->hdmi, hpd_connected,
614 drm_helper_hpd_irq_event(dw_hdmi->encoder.dev);
620 static enum drm_mode_status
621 dw_hdmi_mode_valid(struct drm_connector *connector,
622 const struct drm_display_mode *mode)
624 struct meson_drm *priv = connector->dev->dev_private;
625 unsigned int vclk_freq;
626 unsigned int venc_freq;
627 unsigned int hdmi_freq;
628 int vic = drm_match_cea_mode(mode);
629 enum drm_mode_status status;
631 DRM_DEBUG_DRIVER("Modeline " DRM_MODE_FMT "\n", DRM_MODE_ARG(mode));
633 /* If sink max TMDS clock, we reject the mode */
634 if (mode->clock > connector->display_info.max_tmds_clock)
637 /* Check against non-VIC supported modes */
639 status = meson_venc_hdmi_supported_mode(mode);
640 if (status != MODE_OK)
643 return meson_vclk_dmt_supported_freq(priv, mode->clock);
644 /* Check against supported VIC modes */
645 } else if (!meson_venc_hdmi_supported_vic(vic))
648 vclk_freq = mode->clock;
650 /* 480i/576i needs global pixel doubling */
651 if (mode->flags & DRM_MODE_FLAG_DBLCLK)
654 venc_freq = vclk_freq;
655 hdmi_freq = vclk_freq;
657 /* VENC double pixels for 1080i and 720p modes */
658 if (meson_venc_hdmi_venc_repeat(vic))
661 vclk_freq = max(venc_freq, hdmi_freq);
663 if (mode->flags & DRM_MODE_FLAG_DBLCLK)
666 dev_dbg(connector->dev->dev, "%s: vclk:%d venc=%d hdmi=%d\n", __func__,
667 vclk_freq, venc_freq, hdmi_freq);
669 return meson_vclk_vic_supported_freq(vclk_freq);
674 static void meson_venc_hdmi_encoder_destroy(struct drm_encoder *encoder)
676 drm_encoder_cleanup(encoder);
679 static const struct drm_encoder_funcs meson_venc_hdmi_encoder_funcs = {
680 .destroy = meson_venc_hdmi_encoder_destroy,
683 static int meson_venc_hdmi_encoder_atomic_check(struct drm_encoder *encoder,
684 struct drm_crtc_state *crtc_state,
685 struct drm_connector_state *conn_state)
690 static void meson_venc_hdmi_encoder_disable(struct drm_encoder *encoder)
692 struct meson_dw_hdmi *dw_hdmi = encoder_to_meson_dw_hdmi(encoder);
693 struct meson_drm *priv = dw_hdmi->priv;
695 DRM_DEBUG_DRIVER("\n");
697 writel_bits_relaxed(0x3, 0,
698 priv->io_base + _REG(VPU_HDMI_SETTING));
700 writel_relaxed(0, priv->io_base + _REG(ENCI_VIDEO_EN));
701 writel_relaxed(0, priv->io_base + _REG(ENCP_VIDEO_EN));
704 static void meson_venc_hdmi_encoder_enable(struct drm_encoder *encoder)
706 struct meson_dw_hdmi *dw_hdmi = encoder_to_meson_dw_hdmi(encoder);
707 struct meson_drm *priv = dw_hdmi->priv;
709 DRM_DEBUG_DRIVER("%s\n", priv->venc.hdmi_use_enci ? "VENCI" : "VENCP");
711 if (priv->venc.hdmi_use_enci)
712 writel_relaxed(1, priv->io_base + _REG(ENCI_VIDEO_EN));
714 writel_relaxed(1, priv->io_base + _REG(ENCP_VIDEO_EN));
717 static void meson_venc_hdmi_encoder_mode_set(struct drm_encoder *encoder,
718 struct drm_display_mode *mode,
719 struct drm_display_mode *adjusted_mode)
721 struct meson_dw_hdmi *dw_hdmi = encoder_to_meson_dw_hdmi(encoder);
722 struct meson_drm *priv = dw_hdmi->priv;
723 int vic = drm_match_cea_mode(mode);
725 DRM_DEBUG_DRIVER("\"%s\" vic %d\n", mode->name, vic);
727 /* VENC + VENC-DVI Mode setup */
728 meson_venc_hdmi_mode_set(priv, vic, mode);
731 dw_hdmi_set_vclk(dw_hdmi, mode);
733 /* Setup YUV444 to HDMI-TX, no 10bit diphering */
734 writel_relaxed(0, priv->io_base + _REG(VPU_HDMI_FMT_CTRL));
737 static const struct drm_encoder_helper_funcs
738 meson_venc_hdmi_encoder_helper_funcs = {
739 .atomic_check = meson_venc_hdmi_encoder_atomic_check,
740 .disable = meson_venc_hdmi_encoder_disable,
741 .enable = meson_venc_hdmi_encoder_enable,
742 .mode_set = meson_venc_hdmi_encoder_mode_set,
747 static int meson_dw_hdmi_reg_read(void *context, unsigned int reg,
748 unsigned int *result)
750 struct meson_dw_hdmi *dw_hdmi = context;
752 *result = dw_hdmi->data->dwc_read(dw_hdmi, reg);
758 static int meson_dw_hdmi_reg_write(void *context, unsigned int reg,
761 struct meson_dw_hdmi *dw_hdmi = context;
763 dw_hdmi->data->dwc_write(dw_hdmi, reg, val);
768 static const struct regmap_config meson_dw_hdmi_regmap_config = {
771 .reg_read = meson_dw_hdmi_reg_read,
772 .reg_write = meson_dw_hdmi_reg_write,
773 .max_register = 0x10000,
777 static const struct meson_dw_hdmi_data meson_dw_hdmi_gx_data = {
778 .top_read = dw_hdmi_top_read,
779 .top_write = dw_hdmi_top_write,
780 .dwc_read = dw_hdmi_dwc_read,
781 .dwc_write = dw_hdmi_dwc_write,
784 static const struct meson_dw_hdmi_data meson_dw_hdmi_g12a_data = {
785 .top_read = dw_hdmi_g12a_top_read,
786 .top_write = dw_hdmi_g12a_top_write,
787 .dwc_read = dw_hdmi_g12a_dwc_read,
788 .dwc_write = dw_hdmi_g12a_dwc_write,
791 static bool meson_hdmi_connector_is_available(struct device *dev)
793 struct device_node *ep, *remote;
795 /* HDMI Connector is on the second port, first endpoint */
796 ep = of_graph_get_endpoint_by_regs(dev->of_node, 1, 0);
800 /* If the endpoint node exists, consider it enabled */
801 remote = of_graph_get_remote_port(ep);
813 static int meson_dw_hdmi_bind(struct device *dev, struct device *master,
816 struct platform_device *pdev = to_platform_device(dev);
817 const struct meson_dw_hdmi_data *match;
818 struct meson_dw_hdmi *meson_dw_hdmi;
819 struct drm_device *drm = data;
820 struct meson_drm *priv = drm->dev_private;
821 struct dw_hdmi_plat_data *dw_plat_data;
822 struct drm_encoder *encoder;
823 struct resource *res;
827 DRM_DEBUG_DRIVER("\n");
829 if (!meson_hdmi_connector_is_available(dev)) {
830 dev_info(drm->dev, "HDMI Output connector not available\n");
834 match = of_device_get_match_data(&pdev->dev);
836 dev_err(&pdev->dev, "failed to get match data\n");
840 meson_dw_hdmi = devm_kzalloc(dev, sizeof(*meson_dw_hdmi),
845 meson_dw_hdmi->priv = priv;
846 meson_dw_hdmi->dev = dev;
847 meson_dw_hdmi->data = match;
848 dw_plat_data = &meson_dw_hdmi->dw_plat_data;
849 encoder = &meson_dw_hdmi->encoder;
851 meson_dw_hdmi->hdmi_supply = devm_regulator_get_optional(dev, "hdmi");
852 if (IS_ERR(meson_dw_hdmi->hdmi_supply)) {
853 if (PTR_ERR(meson_dw_hdmi->hdmi_supply) == -EPROBE_DEFER)
854 return -EPROBE_DEFER;
855 meson_dw_hdmi->hdmi_supply = NULL;
857 ret = regulator_enable(meson_dw_hdmi->hdmi_supply);
862 meson_dw_hdmi->hdmitx_apb = devm_reset_control_get_exclusive(dev,
864 if (IS_ERR(meson_dw_hdmi->hdmitx_apb)) {
865 dev_err(dev, "Failed to get hdmitx_apb reset\n");
866 return PTR_ERR(meson_dw_hdmi->hdmitx_apb);
869 meson_dw_hdmi->hdmitx_ctrl = devm_reset_control_get_exclusive(dev,
871 if (IS_ERR(meson_dw_hdmi->hdmitx_ctrl)) {
872 dev_err(dev, "Failed to get hdmitx reset\n");
873 return PTR_ERR(meson_dw_hdmi->hdmitx_ctrl);
876 meson_dw_hdmi->hdmitx_phy = devm_reset_control_get_exclusive(dev,
878 if (IS_ERR(meson_dw_hdmi->hdmitx_phy)) {
879 dev_err(dev, "Failed to get hdmitx_phy reset\n");
880 return PTR_ERR(meson_dw_hdmi->hdmitx_phy);
883 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
884 meson_dw_hdmi->hdmitx = devm_ioremap_resource(dev, res);
885 if (IS_ERR(meson_dw_hdmi->hdmitx))
886 return PTR_ERR(meson_dw_hdmi->hdmitx);
888 meson_dw_hdmi->hdmi_pclk = devm_clk_get(dev, "isfr");
889 if (IS_ERR(meson_dw_hdmi->hdmi_pclk)) {
890 dev_err(dev, "Unable to get HDMI pclk\n");
891 return PTR_ERR(meson_dw_hdmi->hdmi_pclk);
893 clk_prepare_enable(meson_dw_hdmi->hdmi_pclk);
895 meson_dw_hdmi->venci_clk = devm_clk_get(dev, "venci");
896 if (IS_ERR(meson_dw_hdmi->venci_clk)) {
897 dev_err(dev, "Unable to get venci clk\n");
898 return PTR_ERR(meson_dw_hdmi->venci_clk);
900 clk_prepare_enable(meson_dw_hdmi->venci_clk);
902 dw_plat_data->regm = devm_regmap_init(dev, NULL, meson_dw_hdmi,
903 &meson_dw_hdmi_regmap_config);
904 if (IS_ERR(dw_plat_data->regm))
905 return PTR_ERR(dw_plat_data->regm);
907 irq = platform_get_irq(pdev, 0);
909 dev_err(dev, "Failed to get hdmi top irq\n");
913 ret = devm_request_threaded_irq(dev, irq, dw_hdmi_top_irq,
914 dw_hdmi_top_thread_irq, IRQF_SHARED,
915 "dw_hdmi_top_irq", meson_dw_hdmi);
917 dev_err(dev, "Failed to request hdmi top irq\n");
923 drm_encoder_helper_add(encoder, &meson_venc_hdmi_encoder_helper_funcs);
925 ret = drm_encoder_init(drm, encoder, &meson_venc_hdmi_encoder_funcs,
926 DRM_MODE_ENCODER_TMDS, "meson_hdmi");
928 dev_err(priv->dev, "Failed to init HDMI encoder\n");
932 encoder->possible_crtcs = BIT(0);
934 DRM_DEBUG_DRIVER("encoder initialized\n");
937 regmap_update_bits(priv->hhi, HHI_HDMI_CLK_CNTL, 0xffff, 0x100);
939 /* Bring HDMITX MEM output of power down */
940 regmap_update_bits(priv->hhi, HHI_MEM_PD_REG0, 0xff << 8, 0);
942 /* Reset HDMITX APB & TX & PHY */
943 reset_control_reset(meson_dw_hdmi->hdmitx_apb);
944 reset_control_reset(meson_dw_hdmi->hdmitx_ctrl);
945 reset_control_reset(meson_dw_hdmi->hdmitx_phy);
947 /* Enable APB3 fail on error */
948 if (!meson_vpu_is_compatible(priv, "amlogic,meson-g12a-vpu")) {
949 writel_bits_relaxed(BIT(15), BIT(15),
950 meson_dw_hdmi->hdmitx + HDMITX_TOP_CTRL_REG);
951 writel_bits_relaxed(BIT(15), BIT(15),
952 meson_dw_hdmi->hdmitx + HDMITX_DWC_CTRL_REG);
955 /* Bring out of reset */
956 meson_dw_hdmi->data->top_write(meson_dw_hdmi,
957 HDMITX_TOP_SW_RESET, 0);
961 meson_dw_hdmi->data->top_write(meson_dw_hdmi,
962 HDMITX_TOP_CLK_CNTL, 0xff);
964 /* Enable HDMI-TX Interrupt */
965 meson_dw_hdmi->data->top_write(meson_dw_hdmi, HDMITX_TOP_INTR_STAT_CLR,
966 HDMITX_TOP_INTR_CORE);
968 meson_dw_hdmi->data->top_write(meson_dw_hdmi, HDMITX_TOP_INTR_MASKN,
969 HDMITX_TOP_INTR_CORE);
971 /* Bridge / Connector */
973 dw_plat_data->mode_valid = dw_hdmi_mode_valid;
974 dw_plat_data->phy_ops = &meson_dw_hdmi_phy_ops;
975 dw_plat_data->phy_name = "meson_dw_hdmi_phy";
976 dw_plat_data->phy_data = meson_dw_hdmi;
977 dw_plat_data->input_bus_format = MEDIA_BUS_FMT_YUV8_1X24;
978 dw_plat_data->input_bus_encoding = V4L2_YCBCR_ENC_709;
980 platform_set_drvdata(pdev, meson_dw_hdmi);
982 meson_dw_hdmi->hdmi = dw_hdmi_bind(pdev, encoder,
983 &meson_dw_hdmi->dw_plat_data);
984 if (IS_ERR(meson_dw_hdmi->hdmi))
985 return PTR_ERR(meson_dw_hdmi->hdmi);
987 DRM_DEBUG_DRIVER("HDMI controller initialized\n");
992 static void meson_dw_hdmi_unbind(struct device *dev, struct device *master,
995 struct meson_dw_hdmi *meson_dw_hdmi = dev_get_drvdata(dev);
997 dw_hdmi_unbind(meson_dw_hdmi->hdmi);
1000 static const struct component_ops meson_dw_hdmi_ops = {
1001 .bind = meson_dw_hdmi_bind,
1002 .unbind = meson_dw_hdmi_unbind,
1005 static int meson_dw_hdmi_probe(struct platform_device *pdev)
1007 return component_add(&pdev->dev, &meson_dw_hdmi_ops);
1010 static int meson_dw_hdmi_remove(struct platform_device *pdev)
1012 component_del(&pdev->dev, &meson_dw_hdmi_ops);
1017 static const struct of_device_id meson_dw_hdmi_of_table[] = {
1018 { .compatible = "amlogic,meson-gxbb-dw-hdmi",
1019 .data = &meson_dw_hdmi_gx_data },
1020 { .compatible = "amlogic,meson-gxl-dw-hdmi",
1021 .data = &meson_dw_hdmi_gx_data },
1022 { .compatible = "amlogic,meson-gxm-dw-hdmi",
1023 .data = &meson_dw_hdmi_gx_data },
1024 { .compatible = "amlogic,meson-g12a-dw-hdmi",
1025 .data = &meson_dw_hdmi_g12a_data },
1028 MODULE_DEVICE_TABLE(of, meson_dw_hdmi_of_table);
1030 static struct platform_driver meson_dw_hdmi_platform_driver = {
1031 .probe = meson_dw_hdmi_probe,
1032 .remove = meson_dw_hdmi_remove,
1034 .name = DRIVER_NAME,
1035 .of_match_table = meson_dw_hdmi_of_table,
1038 module_platform_driver(meson_dw_hdmi_platform_driver);
1040 MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
1041 MODULE_DESCRIPTION(DRIVER_DESC);
1042 MODULE_LICENSE("GPL");