diff options
author | WalkerChenL <92851182+WalkerChenL@users.noreply.github.com> | 2022-01-06 15:49:14 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-06 15:49:14 +0300 |
commit | 338d31cdc3f5efeb47e2239b63f9332950b57752 (patch) | |
tree | c3d250c871b9959c5b9aa8aa542b768d1700f267 | |
parent | 9311a08de4961a1bc48681162ed6b1a08109efb0 (diff) | |
parent | 5a113a2dd3d0b8b71ed4b17856ebccf797d7289e (diff) | |
download | linux-338d31cdc3f5efeb47e2239b63f9332950b57752.tar.xz |
Merge pull request #44 from kJugg/visionfive-5.15.y-devel
riscv:driver:drm:starfive:mipi driver
-rw-r--r-- | arch/riscv/boot/dts/starfive/jh7100-common.dtsi | 17 | ||||
-rwxr-xr-x | arch/riscv/boot/dts/starfive/jh7100.dtsi | 15 | ||||
-rw-r--r-- | drivers/gpu/drm/starfive/Kconfig | 9 | ||||
-rw-r--r-- | drivers/gpu/drm/starfive/Makefile | 4 | ||||
-rw-r--r-- | drivers/gpu/drm/starfive/starfive_drm_crtc.c | 57 | ||||
-rw-r--r-- | drivers/gpu/drm/starfive/starfive_drm_drv.c | 27 | ||||
-rw-r--r-- | drivers/gpu/drm/starfive/starfive_drm_drv.h | 2 | ||||
-rw-r--r-- | drivers/gpu/drm/starfive/starfive_drm_dsi.c | 1307 | ||||
-rw-r--r-- | drivers/gpu/drm/starfive/starfive_drm_encoder.c | 16 | ||||
-rw-r--r-- | drivers/gpu/drm/starfive/starfive_drm_seeedpanel.c | 514 | ||||
-rw-r--r-- | drivers/gpu/drm/starfive/starfive_drm_vpp.c | 11 | ||||
-rw-r--r--[-rwxr-xr-x] | drivers/phy/m31/Kconfig | 0 | ||||
-rw-r--r--[-rwxr-xr-x] | drivers/phy/m31/Makefile | 0 | ||||
-rw-r--r--[-rwxr-xr-x] | drivers/phy/m31/phy-m31-dphy-tx0.c | 2 |
14 files changed, 1926 insertions, 55 deletions
diff --git a/arch/riscv/boot/dts/starfive/jh7100-common.dtsi b/arch/riscv/boot/dts/starfive/jh7100-common.dtsi index 4428d24848a1..6e659bd0b351 100644 --- a/arch/riscv/boot/dts/starfive/jh7100-common.dtsi +++ b/arch/riscv/boot/dts/starfive/jh7100-common.dtsi @@ -110,19 +110,31 @@ status = "okay"; ports { + #address-cells = <1>; + #size-cells = <0>; port@0 { + reg = <0>; hdmi_out: endpoint { remote-endpoint = <&tda998x_0_input>; }; }; port@1 { + reg = <1>; + mipi_out: endpoint { + remote-endpoint = <&dsi_out_port>; + }; + }; + + port@2 { + reg = <2>; hdmi_input0: endpoint { remote-endpoint = <&crtc_0_out>; }; }; }; + }; &gmac { @@ -506,11 +518,6 @@ }; #endif - seeed_plane_i2c@45 { - compatible = "seeed_panel"; - reg = <0x45>; - }; - /* TODO: Used for EVB board, should comment here for starlight board, remove it later*/ ov5640: ov5640@3c { compatible = "ovti,ov5640"; diff --git a/arch/riscv/boot/dts/starfive/jh7100.dtsi b/arch/riscv/boot/dts/starfive/jh7100.dtsi index 076623953908..0fa71f92dc37 100755 --- a/arch/riscv/boot/dts/starfive/jh7100.dtsi +++ b/arch/riscv/boot/dts/starfive/jh7100.dtsi @@ -716,12 +716,12 @@ display: display-subsystem { compatible = "starfive,display-subsystem"; dma-coherent; - status = "disabled"; + status = "okay"; }; encoder: display-encoder { compatible = "starfive,display-encoder"; - status = "disabled"; + status = "okay"; }; crtc: crtc@12000000 { @@ -742,8 +742,7 @@ interrupt-names = "lcdc_irq", "vpp1_irq"; #address-cells = <1>; #size-cells = <0>; - status = "disabled"; - + status = "okay"; pp1 { pp-id = <1>; fifo-out; @@ -755,6 +754,14 @@ dst-width = <1920>; dst-height = <1080>; }; + + port: port@0 { + reg = <0>; + crtc_0_out: endpoint { + remote-endpoint = <&hdmi_input0>; + }; + }; + }; mipi_dphy: mipi-dphy@12260000{ diff --git a/drivers/gpu/drm/starfive/Kconfig b/drivers/gpu/drm/starfive/Kconfig index 4d5f1f5972c4..8643dd2690de 100644 --- a/drivers/gpu/drm/starfive/Kconfig +++ b/drivers/gpu/drm/starfive/Kconfig @@ -14,3 +14,12 @@ config DRM_STARFIVE The module will be called starfive-drm This driver provides kernel mode setting and buffer management to userspace. + +config DRM_STARFIVE_MIPI_DSI + bool "Starfive MIPI DSI Select" + select GENERIC_PHY_MIPI_DPHY + help + This selects support for starfive SoC specific extensions + for the Synopsys DesignWare MIPI driver. If you want to + enable MIPI DSI on VIC7100 based SoC, you should + select this option. diff --git a/drivers/gpu/drm/starfive/Makefile b/drivers/gpu/drm/starfive/Makefile index 8ef9e5f469fd..bafa3a5a79da 100644 --- a/drivers/gpu/drm/starfive/Makefile +++ b/drivers/gpu/drm/starfive/Makefile @@ -8,6 +8,8 @@ starfive-drm-y := starfive_drm_drv.o \ starfive_drm_encoder.o \ starfive_drm_plane.o \ starfive_drm_lcdc.o \ - starfive_drm_vpp.o + starfive_drm_vpp.o \ + starfive_drm_dsi.o \ + starfive_drm_seeedpanel.o obj-$(CONFIG_DRM_STARFIVE) += starfive-drm.o diff --git a/drivers/gpu/drm/starfive/starfive_drm_crtc.c b/drivers/gpu/drm/starfive/starfive_drm_crtc.c index 717dae0b52ca..e210a5675bae 100644 --- a/drivers/gpu/drm/starfive/starfive_drm_crtc.c +++ b/drivers/gpu/drm/starfive/starfive_drm_crtc.c @@ -336,27 +336,41 @@ static int starfive_crtc_get_memres(struct platform_device *pdev, struct starfiv static int starfive_crtc_get_clks(struct platform_device *pdev, struct starfive_crtc *sf_crtc) { - struct clk_bulk_data clks[] = { - { .id = "disp_axi" }, - { .id = "vout_src" }, - }; - int ret = devm_clk_bulk_get(&pdev->dev, ARRAY_SIZE(clks), clks); + int ret = 0; + + sf_crtc->clk_disp_axi = devm_clk_get(&pdev->dev, "disp_axi"); + if (IS_ERR(sf_crtc->clk_disp_axi)) { + dev_warn(&pdev->dev, "Can't get disp_axi clock\n"); + return PTR_ERR(sf_crtc->clk_disp_axi); + } + sf_crtc->clk_vout_src = devm_clk_get(&pdev->dev, "vout_src"); + if (IS_ERR(sf_crtc->clk_vout_src)) { + dev_warn(&pdev->dev, "Can't get vout_src clock\n"); + devm_clk_put(&pdev->dev, sf_crtc->clk_disp_axi); + return PTR_ERR(sf_crtc->clk_vout_src); + } + - sf_crtc->clk_disp_axi = clks[0].clk; - sf_crtc->clk_vout_src = clks[1].clk; return ret; } static int starfive_crtc_get_resets(struct platform_device *pdev, struct starfive_crtc *sf_crtc) { - struct reset_control_bulk_data resets[] = { - { .id = "disp_axi" }, - { .id = "vout_src" }, - }; - int ret = devm_reset_control_bulk_get_exclusive(&pdev->dev, ARRAY_SIZE(resets), resets); + int ret = 0; + + sf_crtc->rst_disp_axi = devm_reset_control_get_exclusive(&pdev->dev, "disp_axi"); + if (IS_ERR(sf_crtc->rst_disp_axi)) { + dev_warn(&pdev->dev, "Can't get disp_axi reset_control\n"); + return PTR_ERR(sf_crtc->rst_disp_axi); + } + + sf_crtc->rst_vout_src = devm_reset_control_get_exclusive(&pdev->dev, "vout_src"); + if (IS_ERR(sf_crtc->rst_vout_src)) { + dev_warn(&pdev->dev, "Can't get vout_src reset_control\n"); + reset_control_put(sf_crtc->rst_disp_axi); + return PTR_ERR(sf_crtc->rst_vout_src); + } - sf_crtc->rst_disp_axi = resets[0].rstc; - sf_crtc->rst_vout_src = resets[1].rstc; return ret; } @@ -489,7 +503,14 @@ static int starfive_crtc_bind(struct device *dev, struct device *master, void *d crtcp->is_enabled = false; - /* starfive_set_crtc_possible_masks(drm_dev, crtcp); */ +#ifdef CONFIG_DRM_STARFIVE_MIPI_DSI + dsitx_vout_init(crtcp); + lcdc_dsi_sel(crtcp); +#else + vout_reset(crtcp); +#endif + + /*starfive_set_crtc_possible_masks(drm_dev, crtcp);*/ /* ret = drm_self_refresh_helper_init(crtcp); @@ -507,7 +528,11 @@ static void starfive_crtc_unbind(struct device *dev, struct device *master, void struct platform_device *pdev = to_platform_device(dev); struct starfive_crtc *crtcp = dev_get_drvdata(dev); - drm_crtc_cleanup(&crtcp->crtc); + vout_disable(crtcp);// disable crtc HW + + crtcp->is_enabled = false; + + //drm_crtc_cleanup(&crtcp->crtc); platform_set_drvdata(pdev, NULL); } diff --git a/drivers/gpu/drm/starfive/starfive_drm_drv.c b/drivers/gpu/drm/starfive/starfive_drm_drv.c index 2fa2dff2edbb..92e6e1612b8e 100644 --- a/drivers/gpu/drm/starfive/starfive_drm_drv.c +++ b/drivers/gpu/drm/starfive/starfive_drm_drv.c @@ -98,18 +98,6 @@ static void starfive_drm_match_add(struct device *dev, } } -static void starfive_cleanup(struct drm_device *ddev) -{ - struct starfive_drm_private *private = ddev->dev_private; - - drm_kms_helper_poll_fini(ddev); - drm_atomic_helper_shutdown(ddev); - drm_mode_config_cleanup(ddev); - component_unbind_all(ddev->dev, ddev); - kfree(private); - ddev->dev_private = NULL; -} - static int starfive_drm_bind(struct device *dev) { struct drm_device *drm_dev; @@ -176,9 +164,10 @@ static int starfive_drm_bind(struct device *dev) return 0; err_drm_dev_register: -err_component_bind_all: - starfive_cleanup(drm_dev); + drm_kms_helper_poll_fini(drm_dev); err_drm_vblank_init: +err_component_bind_all: + component_unbind_all(dev, drm_dev); err_free: drm_dev_put(drm_dev); @@ -190,6 +179,12 @@ static void starfive_drm_unbind(struct device *dev) struct drm_device *drm_dev = dev_get_drvdata(dev); drm_dev_unregister(drm_dev); + drm_kms_helper_poll_fini(drm_dev); + drm_atomic_helper_shutdown(drm_dev); + component_unbind_all(dev, drm_dev); + drm_mode_config_cleanup(drm_dev); + + drm_dev_put(drm_dev); } static const struct component_master_ops starfive_drm_ops = { @@ -199,9 +194,7 @@ static const struct component_master_ops starfive_drm_ops = { static struct platform_driver * const starfive_component_drivers[] = { &starfive_crtc_driver, -#ifdef CONFIG_DRM_STARFIVE_MIPI_DSI &starfive_dsi_platform_driver, -#endif &starfive_encoder_driver, }; @@ -255,9 +248,9 @@ static int __init starfive_drm_init(void) static void __exit starfive_drm_exit(void) { + platform_driver_unregister(&starfive_drm_platform_driver); platform_unregister_drivers(starfive_component_drivers, ARRAY_SIZE(starfive_component_drivers)); - platform_driver_unregister(&starfive_drm_platform_driver); } module_init(starfive_drm_init); diff --git a/drivers/gpu/drm/starfive/starfive_drm_drv.h b/drivers/gpu/drm/starfive/starfive_drm_drv.h index 7fde9d846daf..c4026462f553 100644 --- a/drivers/gpu/drm/starfive/starfive_drm_drv.h +++ b/drivers/gpu/drm/starfive/starfive_drm_drv.h @@ -21,5 +21,7 @@ struct starfive_drm_private { extern struct platform_driver starfive_crtc_driver; extern struct platform_driver starfive_encoder_driver; extern struct platform_driver starfive_dsi_platform_driver; +extern int init_seeed_panel(void); +extern void exit_seeed_panel(void); #endif /* _STARFIVE_DRM_DRV_H_ */ diff --git a/drivers/gpu/drm/starfive/starfive_drm_dsi.c b/drivers/gpu/drm/starfive/starfive_drm_dsi.c new file mode 100644 index 000000000000..2ace15113126 --- /dev/null +++ b/drivers/gpu/drm/starfive/starfive_drm_dsi.c @@ -0,0 +1,1307 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright: 2017 Cadence Design Systems, Inc. + * + * Author: Boris Brezillon <boris.brezillon@bootlin.com> + */ + +#include <drm/drm_atomic_helper.h> +#include <drm/drm_bridge.h> +#include <drm/drm_drv.h> +#include <drm/drm_mipi_dsi.h> +#include <drm/drm_panel.h> +#include <drm/drm_probe_helper.h> +#include <video/mipi_display.h> +#include <linux/clk.h> +#include <linux/interrupt.h> +#include <linux/iopoll.h> +#include <linux/module.h> +#include <linux/of_address.h> +#include <linux/of_graph.h> +#include <linux/platform_device.h> +#include <linux/pm_runtime.h> +#include <linux/reset.h> +#include <linux/component.h> +#include <linux/phy/phy.h> +#include <linux/phy/phy-mipi-dphy.h> +#include "starfive_drm_drv.h" + +//sysrst registers +#define SRST_ASSERT0 0x00 +#define SRST_STATUS0 0x04 + +#define IP_CONF 0x0 +#define SP_HS_FIFO_DEPTH(x) (((x) & GENMASK(30, 26)) >> 26) +#define SP_LP_FIFO_DEPTH(x) (((x) & GENMASK(25, 21)) >> 21) +#define VRS_FIFO_DEPTH(x) (((x) & GENMASK(20, 16)) >> 16) +#define DIRCMD_FIFO_DEPTH(x) (((x) & GENMASK(15, 13)) >> 13) +#define SDI_IFACE_32 BIT(12) +#define INTERNAL_DATAPATH_32 (0 << 10) +#define INTERNAL_DATAPATH_16 (1 << 10) +#define INTERNAL_DATAPATH_8 (3 << 10) +#define INTERNAL_DATAPATH_SIZE ((x) & GENMASK(11, 10)) +#define NUM_IFACE(x) ((((x) & GENMASK(9, 8)) >> 8) + 1) +#define MAX_LANE_NB(x) (((x) & GENMASK(7, 6)) >> 6) +#define RX_FIFO_DEPTH(x) ((x) & GENMASK(5, 0)) + +#define MCTL_MAIN_DATA_CTL 0x4 +#define TE_MIPI_POLLING_EN BIT(25) +#define TE_HW_POLLING_EN BIT(24) +#define DISP_EOT_GEN BIT(18) +#define HOST_EOT_GEN BIT(17) +#define DISP_GEN_CHECKSUM BIT(16) +#define DISP_GEN_ECC BIT(15) +#define BTA_EN BIT(14) +#define READ_EN BIT(13) +#define REG_TE_EN BIT(12) +#define IF_TE_EN(x) BIT(8 + (x)) +#define TVG_SEL BIT(6) +#define VID_EN BIT(5) +#define IF_VID_SELECT(x) ((x) << 2) +#define IF_VID_SELECT_MASK GENMASK(3, 2) +#define IF_VID_MODE BIT(1) +#define LINK_EN BIT(0) + +#define MCTL_MAIN_PHY_CTL 0x8 +#define HS_INVERT_DAT(x) BIT(19 + ((x) * 2)) +#define SWAP_PINS_DAT(x) BIT(18 + ((x) * 2)) +#define HS_INVERT_CLK BIT(17) +#define SWAP_PINS_CLK BIT(16) +#define HS_SKEWCAL_EN BIT(15) +#define WAIT_BURST_TIME(x) ((x) << 10) +#define DATA_ULPM_EN(x) BIT(6 + (x)) +#define CLK_ULPM_EN BIT(5) +#define CLK_CONTINUOUS BIT(4) +#define DATA_LANE_EN(x) BIT((x) - 1) + +#define MCTL_MAIN_EN 0xc +#define DATA_FORCE_STOP BIT(17) +#define CLK_FORCE_STOP BIT(16) +#define IF_EN(x) BIT(13 + (x)) +#define DATA_LANE_ULPM_REQ(l) BIT(9 + (l)) +#define CLK_LANE_ULPM_REQ BIT(8) +#define DATA_LANE_START(x) BIT(4 + (x)) +#define CLK_LANE_EN BIT(3) +#define PLL_START BIT(0) + +#define MCTL_DPHY_CFG0 0x10 +#define DPHY_C_RSTB BIT(20) +#define DPHY_D_RSTB(x) GENMASK(15 + (x), 16) +#define DPHY_PLL_PDN BIT(10) +#define DPHY_CMN_PDN BIT(9) +#define DPHY_C_PDN BIT(8) +#define DPHY_D_PDN(x) GENMASK(3 + (x), 4) +#define DPHY_ALL_D_PDN GENMASK(7, 4) +#define DPHY_PLL_PSO BIT(1) +#define DPHY_CMN_PSO BIT(0) + +#define MCTL_DPHY_TIMEOUT1 0x14 +#define HSTX_TIMEOUT(x) ((x) << 4) +#define HSTX_TIMEOUT_MAX GENMASK(17, 0) +#define CLK_DIV(x) (x) +#define CLK_DIV_MAX GENMASK(3, 0) + +#define MCTL_DPHY_TIMEOUT2 0x18 +#define LPRX_TIMEOUT(x) (x) + +#define MCTL_ULPOUT_TIME 0x1c +#define DATA_LANE_ULPOUT_TIME(x) ((x) << 9) +#define CLK_LANE_ULPOUT_TIME(x) (x) + +#define MCTL_3DVIDEO_CTL 0x20 +#define VID_VSYNC_3D_EN BIT(7) +#define VID_VSYNC_3D_LR BIT(5) +#define VID_VSYNC_3D_SECOND_EN BIT(4) +#define VID_VSYNC_3DFORMAT_LINE (0 << 2) +#define VID_VSYNC_3DFORMAT_FRAME (1 << 2) +#define VID_VSYNC_3DFORMAT_PIXEL (2 << 2) +#define VID_VSYNC_3DMODE_OFF 0 +#define VID_VSYNC_3DMODE_PORTRAIT 1 +#define VID_VSYNC_3DMODE_LANDSCAPE 2 + +#define MCTL_MAIN_STS 0x24 +#define MCTL_MAIN_STS_CTL 0x130 +#define MCTL_MAIN_STS_CLR 0x150 +#define MCTL_MAIN_STS_FLAG 0x170 +#define HS_SKEWCAL_DONE BIT(11) +#define IF_UNTERM_PKT_ERR(x) BIT(8 + (x)) +#define LPRX_TIMEOUT_ERR BIT(7) +#define HSTX_TIMEOUT_ERR BIT(6) +#define DATA_LANE_RDY(l) BIT(2 + (l)) +#define CLK_LANE_RDY BIT(1) +#define PLL_LOCKED BIT(0) + +#define MCTL_DPHY_ERR 0x28 +#define MCTL_DPHY_ERR_CTL1 0x148 +#define MCTL_DPHY_ERR_CLR 0x168 +#define MCTL_DPHY_ERR_FLAG 0x188 +#define ERR_CONT_LP(x, l) BIT(18 + ((x) * 4) + (l)) +#define ERR_CONTROL(l) BIT(14 + (l)) +#define ERR_SYNESC(l) BIT(10 + (l)) +#define ERR_ESC(l) BIT(6 + (l)) + +#define MCTL_DPHY_ERR_CTL2 0x14c +#define ERR_CONT_LP_EDGE(x, l) BIT(12 + ((x) * 4) + (l)) +#define ERR_CONTROL_EDGE(l) BIT(8 + (l)) +#define ERR_SYN_ESC_EDGE(l) BIT(4 + (l)) +#define ERR_ESC_EDGE(l) BIT(0 + (l)) + +#define MCTL_LANE_STS 0x2c +#define PPI_C_TX_READY_HS BIT(18) +#define DPHY_PLL_LOCK BIT(17) +#define PPI_D_RX_ULPS_ESC(x) (((x) & GENMASK(15, 12)) >> 12) +#define LANE_STATE_START 0 +#define LANE_STATE_IDLE 1 +#define LANE_STATE_WRITE 2 +#define LANE_STATE_ULPM 3 +#define LANE_STATE_READ 4 +#define DATA_LANE_STATE(l, val) \ + (((val) >> (2 + 2 * (l) + ((l) ? 1 : 0))) & GENMASK((l) ? 1 : 2, 0)) +#define CLK_LANE_STATE_HS 2 +#define CLK_LANE_STATE(val) ((val) & GENMASK(1, 0)) + +#define DSC_MODE_CTL 0x30 +#define DSC_MODE_EN BIT(0) + +#define DSC_CMD_SEND 0x34 +#define DSC_SEND_PPS BIT(0) +#define DSC_EXECUTE_QUEUE BIT(1) + +#define DSC_PPS_WRDAT 0x38 + +#define DSC_MODE_STS 0x3c +#define DSC_PPS_DONE BIT(1) +#define DSC_EXEC_DONE BIT(2) + +#define CMD_MODE_CTL 0x70 +#define IF_LP_EN(x) BIT(9 + (x)) +#define IF_VCHAN_ID(x, c) ((c) << ((x) * 2)) + +#define CMD_MODE_CTL2 0x74 +#define TE_TIMEOUT(x) ((x) << 11) +#define FILL_VALUE(x) ((x) << 3) +#define ARB_IF_WITH_HIGHEST_PRIORITY(x) ((x) << 1) +#define ARB_ROUND_ROBIN_MODE BIT(0) + +#define CMD_MODE_STS 0x78 +#define CMD_MODE_STS_CTL 0x134 +#define CMD_MODE_STS_CLR 0x154 +#define CMD_MODE_STS_FLAG 0x174 +#define ERR_IF_UNDERRUN(x) BIT(4 + (x)) +#define ERR_UNWANTED_READ BIT(3) +#define ERR_TE_MISS BIT(2) +#define ERR_NO_TE BIT(1) +#define CSM_RUNNING BIT(0) + +#define DIRECT_CMD_SEND 0x80 + +#define DIRECT_CMD_MAIN_SETTINGS 0x84 +#define TRIGGER_VAL(x) ((x) << 25) +#define CMD_LP_EN BIT(24) +#define CMD_SIZE(x) ((x) << 16) +#define CMD_VCHAN_ID(x) ((x) << 14) +#define CMD_DATATYPE(x) ((x) << 8) +#define CMD_LONG BIT(3) +#define WRITE_CMD 0 +#define READ_CMD 1 +#define TE_REQ 4 +#define TRIGGER_REQ 5 +#define BTA_REQ 6 + +#define DIRECT_CMD_STS 0x88 +#define DIRECT_CMD_STS_CTL 0x138 +#define DIRECT_CMD_STS_CLR 0x158 +#define DIRECT_CMD_STS_FLAG 0x178 +#define RCVD_ACK_VAL(val) ((val) >> 16) +#define RCVD_TRIGGER_VAL(val) (((val) & GENMASK(14, 11)) >> 11) +#define READ_COMPLETED_WITH_ERR BIT(10) +#define BTA_FINISHED BIT(9) +#define BTA_COMPLETED BIT(8) +#define TE_RCVD BIT(7) +#define TRIGGER_RCVD BIT(6) +#define ACK_WITH_ERR_RCVD BIT(5) +#define ACK_RCVD BIT(4) +#define READ_COMPLETED BIT(3) +#define TRIGGER_COMPLETED BIT(2) +#define WRITE_COMPLETED BIT(1) + +#define SENDING_CMD BIT(0) + +#define DIRECT_CMD_STOP_READ 0x8c + +#define DIRECT_CMD_WRDATA 0x90 + +#define DIRECT_CMD_FIFO_RST 0x94 + +#define DIRECT_CMD_RDDATA 0xa0 + +#define DIRECT_CMD_RD_PROPS 0xa4 +#define RD_DCS BIT(18) +#define RD_VCHAN_ID(val) (((val) >> 16) & GENMASK(1, 0)) +#define RD_SIZE(val) ((val) & GENMASK(15, 0)) + +#define DIRECT_CMD_RD_STS 0xa8 +#define DIRECT_CMD_RD_STS_CTL 0x13c +#define DIRECT_CMD_RD_STS_CLR 0x15c +#define DIRECT_CMD_RD_STS_FLAG 0x17c +#define ERR_EOT_WITH_ERR BIT(8) +#define ERR_MISSING_EOT BIT(7) +#define ERR_WRONG_LENGTH BIT(6) +#define ERR_OVERSIZE BIT(5) +#define ERR_RECEIVE BIT(4) +#define ERR_UNDECODABLE BIT(3) +#define ERR_CHECKSUM BIT(2) +#define ERR_UNCORRECTABLE BIT(1) +#define ERR_FIXED BIT(0) + +#define VID_MAIN_CTL 0xb0 +#define VID_IGNORE_MISS_VSYNC BIT(31) +#define VID_FIELD_SW BIT(28) +#define VID_INTERLACED_EN BIT(27) +#define RECOVERY_MODE(x) ((x) << 25) +#define RECOVERY_MODE_NEXT_HSYNC 0 +#define RECOVERY_MODE_NEXT_STOP_POINT 2 +#define RECOVERY_MODE_NEXT_VSYNC 3 +#define REG_BLKEOL_MODE(x) ((x) << 23) +#define REG_BLKLINE_MODE(x) ((x) << 21) +#define REG_BLK_MODE_NULL_PKT 0 +#define REG_BLK_MODE_BLANKING_PKT 1 +#define REG_BLK_MODE_LP 2 +#define SYNC_PULSE_HORIZONTAL BIT(20) +#define SYNC_PULSE_ACTIVE BIT(19) +#define BURST_MODE BIT(18) +#define VID_PIXEL_MODE_MASK GENMASK(17, 14) +#define VID_PIXEL_MODE_RGB565 (0 << 14) +#define VID_PIXEL_MODE_RGB666_PACKED (1 << 14) +#define VID_PIXEL_MODE_RGB666 (2 << 14) +#define VID_PIXEL_MODE_RGB888 (3 << 14) +#define VID_PIXEL_MODE_RGB101010 (4 << 14) +#define VID_PIXEL_MODE_RGB121212 (5 << 14) +#define VID_PIXEL_MODE_YUV420 (8 << 14) +#define VID_PIXEL_MODE_YUV422_PACKED (9 << 14) +#define VID_PIXEL_MODE_YUV422 (10 << 14) +#define VID_PIXEL_MODE_YUV422_24B (11 << 14) +#define VID_PIXEL_MODE_DSC_COMP (12 << 14) +#define VID_DATATYPE(x) ((x) << 8) +#define VID_VIRTCHAN_ID(iface, x) ((x) << (4 + (iface) * 2)) +#define STOP_MODE(x) ((x) << 2) +#define START_MODE(x) (x) + +#define VID_VSIZE1 0xb4 +#define VFP_LEN(x) ((x) << 12) +#define VBP_LEN(x) ((x) << 6) +#define VSA_LEN(x) (x) + +#define VID_VSIZE2 0xb8 +#define VACT_LEN(x) (x) + +#define VID_HSIZE1 0xc0 +#define HBP_LEN(x) ((x) << 16) +#define HSA_LEN(x) (x) + +#define VID_HSIZE2 0xc4 +#define HFP_LEN(x) ((x) << 16) +#define HACT_LEN(x) (x) + +#define VID_BLKSIZE1 0xcc +#define BLK_EOL_PKT_LEN(x) ((x) << 15) +#define BLK_LINE_EVENT_PKT_LEN(x) (x) + +#define VID_BLKSIZE2 0xd0 +#define BLK_LINE_PULSE_PKT_LEN(x) (x) + +#define VID_PKT_TIME 0xd8 +#define BLK_EOL_DURATION(x) (x) + +#define VID_DPHY_TIME 0xdc +#define REG_WAKEUP_TIME(x) ((x) << 17) +#define REG_LINE_DURATION(x) (x) + +#define VID_ERR_COLOR1 0xe0 +#define COL_GREEN(x) ((x) << 12) +#define COL_RED(x) (x) + +#define VID_ERR_COLOR2 0xe4 +#define PAD_VAL(x) ((x) << 12) +#define COL_BLUE(x) (x) + +#define VID_VPOS 0xe8 +#define LINE_VAL(val) (((val) & GENMASK(14, 2)) >> 2) +#define LINE_POS(val) ((val) & GENMASK(1, 0)) + +#define VID_HPOS 0xec +#define HORIZ_VAL(val) (((val) & GENMASK(17, 3)) >> 3) +#define HORIZ_POS(val) ((val) & GENMASK(2, 0)) + +#define VID_MODE_STS 0xf0 +#define VID_MODE_STS_CTL 0x140 +#define VID_MODE_STS_CLR 0x160 +#define VID_MODE_STS_FLAG 0x180 +#define VSG_RECOVERY BIT(10) +#define ERR_VRS_WRONG_LEN BIT(9) +#define ERR_LONG_READ BIT(8) +#define ERR_LINE_WRITE BIT(7) +#define ERR_BURST_WRITE BIT(6) +#define ERR_SMALL_HEIGHT BIT(5) +#define ERR_SMALL_LEN BIT(4) +#define ERR_MISSING_VSYNC BIT(3) +#define ERR_MISSING_HSYNC BIT(2) +#define ERR_MISSING_DATA BIT(1) +#define VSG_RUNNING BIT(0) + +#define VID_VCA_SETTING1 0xf4 +#define BURST_LP BIT(16) +#define MAX_BURST_LIMIT(x) (x) + +#define VID_VCA_SETTING2 0xf8 +#define MAX_LINE_LIMIT(x) ((x) << 16) +#define EXACT_BURST_LIMIT(x) (x) + +#define TVG_CTL 0xfc +#define TVG_STRIPE_SIZE(x) ((x) << 5) +#define TVG_MODE_MASK GENMASK(4, 3) +#define TVG_MODE_SINGLE_COLOR (0 << 3) +#define TVG_MODE_VSTRIPES (2 << 3) +#define TVG_MODE_HSTRIPES (3 << 3) +#define TVG_STOPMODE_MASK GENMASK(2, 1) +#define TVG_STOPMODE_EOF (0 << 1) +#define TVG_STOPMODE_EOL (1 << 1) +#define TVG_STOPMODE_NOW (2 << 1) +#define TVG_RUN BIT(0) + +#define TVG_IMG_SIZE 0x100 +#define TVG_NBLINES(x) ((x) << 16) +#define TVG_LINE_SIZE(x) (x) + +#define TVG_COLOR1 0x104 +#define TVG_COL1_GREEN(x) ((x) << 12) +#define TVG_COL1_RED(x) (x) + +#define TVG_COLOR1_BIS 0x108 +#define TVG_COL1_BLUE(x) (x) + +#define TVG_COLOR2 0x10c +#define TVG_COL2_GREEN(x) ((x) << 12) +#define TVG_COL2_RED(x) (x) + +#define TVG_COLOR2_BIS 0x110 +#define TVG_COL2_BLUE(x) (x) + +#define TVG_STS 0x114 +#define TVG_STS_CTL 0x144 +#define TVG_STS_CLR 0x164 +#define TVG_STS_FLAG 0x184 +#define TVG_STS_RUNNING BIT(0) + +#define STS_CTL_EDGE(e) ((e) << 16) + +#define DPHY_LANES_MAP 0x198 +#define DAT_REMAP_CFG(b, l) ((l) << ((b) * 8)) + +#define DPI_IRQ_EN 0x1a0 +#define DPI_IRQ_CLR 0x1a4 +#define DPI_IRQ_STS 0x1a8 +#define PIXEL_BUF_OVERFLOW BIT(0) + +#define DPI_CFG 0x1ac +#define DPI_CFG_FIFO_DEPTH(x) ((x) >> 16) +#define DPI_CFG_FIFO_LEVEL(x) ((x) & GENMASK(15, 0)) + +#define TEST_GENERIC 0x1f0 +#define TEST_STATUS(x) ((x) >> 16) +#define TEST_CTRL(x) (x) + +#define ID_REG 0x1fc +#define REV_VENDOR_ID(x) (((x) & GENMASK(31, 20)) >> 20) +#define REV_PRODUCT_ID(x) (((x) & GENMASK(19, 12)) >> 12) +#define REV_HW(x) (((x) & GENMASK(11, 8)) >> 8) +#define REV_MAJOR(x) (((x) & GENMASK(7, 4)) >> 4) +#define REV_MINOR(x) ((x) & GENMASK(3, 0)) + +#define DSI_OUTPUT_PORT 0 +#define DSI_INPUT_PORT(inputid) (1 + (inputid)) + +#define DSI_HBP_FRAME_OVERHEAD 12 +#define DSI_HSA_FRAME_OVERHEAD 14 +#define DSI_HFP_FRAME_OVERHEAD 6 +#define DSI_HSS_VSS_VSE_FRAME_OVERHEAD 4 +#define DSI_BLANKING_FRAME_OVERHEAD 6 +#define DSI_NULL_FRAME_OVERHEAD 6 +#define DSI_EOT_PKT_SIZE 4 + +struct cdns_dsi_output { + struct mipi_dsi_device *dev; + struct drm_panel *panel; + struct drm_bridge *bridge; + union phy_configure_opts phy_opts; +}; + +enum cdns_dsi_input_id { + CDNS_SDI_INPUT, + CDNS_DPI_INPUT, + CDNS_DSC_INPUT, +}; + +struct cdns_dsi_cfg { + unsigned int hfp; + unsigned int hsa; + unsigned int hbp; + unsigned int hact; + unsigned int htotal; +}; + +struct cdns_dsi_input { + enum cdns_dsi_input_id id; + struct drm_bridge bridge; +}; + +struct cdns_dsi { + struct mipi_dsi_host base; + void __iomem *regs; + struct cdns_dsi_input input; + struct cdns_dsi_output output; + unsigned int direct_cmd_fifo_depth; + unsigned int rx_fifo_depth; + struct completion direct_cmd_comp; + struct clk *dsi_p_clk; + struct reset_control *dsi_p_rst; + struct clk *dsi_sys_clk; + bool link_initialized; + struct phy *dphy; +}; + +static inline struct cdns_dsi *input_to_dsi(struct cdns_dsi_input *input) +{ + return container_of(input, struct cdns_dsi, input); +} + +static inline struct cdns_dsi *to_cdns_dsi(struct mipi_dsi_host *host) +{ + return container_of(host, struct cdns_dsi, base); +} + +static inline struct cdns_dsi_input * +bridge_to_cdns_dsi_input(struct drm_bridge *bridge) +{ + return container_of(bridge, struct cdns_dsi_input, bridge); +} + +static unsigned int mode_to_dpi_hfp(const struct drm_display_mode *mode, + bool mode_valid_check) +{ + if (mode_valid_check) + return mode->hsync_start - mode->hdisplay; + + return mode->crtc_hsync_start - mode->crtc_hdisplay; +} + +static unsigned int dpi_to_dsi_timing(unsigned int dpi_timing, + unsigned int dpi_bpp, + unsigned int dsi_pkt_overhead) +{ + unsigned int dsi_timing = DIV_ROUND_UP(dpi_timing * dpi_bpp, 8); + //unsigned int dsi_timing = dpi_timing; + + if (dsi_timing < dsi_pkt_overhead) + dsi_timing = 0; + else + dsi_timing -= dsi_pkt_overhead; + + return dsi_timing; +} + +static int cdns_dsi_mode2cfg(struct cdns_dsi *dsi, + const struct drm_display_mode *mode, + struct cdns_dsi_cfg *dsi_cfg, + bool mode_valid_check) +{ + struct cdns_dsi_output *output = &dsi->output; + unsigned int tmp; + bool sync_pulse = false; + int bpp; + + memset(dsi_cfg, 0, sizeof(*dsi_cfg)); + + if (output->dev->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE) + sync_pulse = true; + bpp = mipi_dsi_pixel_format_to_bpp(output->dev->format); + + if (mode_valid_check) + tmp = mode->htotal - + (sync_pulse ? mode->hsync_end : mode->hsync_start); + else + tmp = mode->crtc_htotal - + (sync_pulse ? + mode->crtc_hsync_end : mode->crtc_hsync_start); + + dsi_cfg->hbp = dpi_to_dsi_timing(tmp, bpp, DSI_HBP_FRAME_OVERHEAD); + + if (sync_pulse) { + if (mode_valid_check) + tmp = mode->hsync_end - mode->hsync_start; + else + tmp = mode->crtc_hsync_end - mode->crtc_hsync_start; + + dsi_cfg->hsa = dpi_to_dsi_timing(tmp, bpp, + DSI_HSA_FRAME_OVERHEAD); + } + + dsi_cfg->hact = dpi_to_dsi_timing(mode_valid_check ? + mode->hdisplay : mode->crtc_hdisplay, + bpp, 0); + dsi_cfg->hfp = dpi_to_dsi_timing(mode_to_dpi_hfp(mode, mode_valid_check), + bpp, DSI_HFP_FRAME_OVERHEAD); + //dpi to dsi transfer can not match , reconfig those parms + if (mode->hdisplay == 800) { + dsi_cfg->hsa = 16; //30-14 + dsi_cfg->hbp = 73; //85-12 + dsi_cfg->hfp = 146; //152-6 + } + + return 0; +} + +static int cdns_dsi_adjust_phy_config(struct cdns_dsi *dsi, + struct cdns_dsi_cfg *dsi_cfg, + struct phy_configure_opts_mipi_dphy *phy_cfg, + const struct drm_display_mode *mode, + bool mode_valid_check) +{ + struct cdns_dsi_output *output = &dsi->output; + unsigned long long dlane_bps; + unsigned long adj_dsi_htotal; + unsigned long dsi_htotal; + unsigned long dpi_htotal; + unsigned long dpi_hz; + unsigned int dsi_hfp_ext; + unsigned int lanes = output->dev->lanes; + + dsi_htotal = dsi_cfg->hbp + DSI_HBP_FRAME_OVERHEAD; + + if (output->dev->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE) + dsi_htotal += dsi_cfg->hsa + DSI_HSA_FRAME_OVERHEAD; + + dsi_htotal += dsi_cfg->hact; + dsi_htotal += dsi_cfg->hfp + DSI_HFP_FRAME_OVERHEAD; + + /* + * Make sure DSI htotal is aligned on a lane boundary when calculating + * the expected data rate. This is done by extending HFP in case of + * misalignment. + */ + adj_dsi_htotal = dsi_htotal; + if (dsi_htotal % lanes) + adj_dsi_htotal += lanes - (dsi_htotal % lanes); + + dpi_hz = (mode_valid_check ? mode->clock : mode->crtc_clock) * 1000; + dlane_bps = (unsigned long long)dpi_hz * adj_dsi_htotal; + + /* data rate in bytes/sec is not an integer, refuse the mode. */ + dpi_htotal = mode_valid_check ? mode->htotal : mode->crtc_htotal; + + if (do_div(dlane_bps, lanes * dpi_htotal)) + return -EINVAL; + + /* data rate was in bytes/sec, convert to bits/sec. */ + phy_cfg->hs_clk_rate = dlane_bps * 8; + + dsi_hfp_ext = adj_dsi_htotal - dsi_htotal; + dsi_cfg->hfp += dsi_hfp_ext; + dsi_cfg->htotal = dsi_htotal + dsi_hfp_ext; + + return 0; +} + +static int cdns_dsi_check_conf(struct cdns_dsi *dsi, + const struct drm_display_mode *mode, + struct cdns_dsi_cfg *dsi_cfg, + bool mode_valid_check) +{ + struct cdns_dsi_output *output = &dsi->output; + struct phy_configure_opts_mipi_dphy *phy_cfg = &output->phy_opts.mipi_dphy; + unsigned long dsi_hss_hsa_hse_hbp; + unsigned int nlanes = output->dev->lanes; + int ret; + + ret = cdns_dsi_mode2cfg(dsi, mode, dsi_cfg, mode_valid_check); + if (ret) + return ret; + + phy_mipi_dphy_get_default_config(mode->crtc_clock * 1000, + mipi_dsi_pixel_format_to_bpp(output->dev->format), + nlanes, phy_cfg); + + ret = cdns_dsi_adjust_phy_config(dsi, dsi_cfg, phy_cfg, mode, mode_valid_check); + if (ret) + return ret; + + ret = phy_validate(dsi->dphy, PHY_MODE_MIPI_DPHY, 0, &output->phy_opts); + if (ret) + return ret; + + dsi_hss_hsa_hse_hbp = dsi_cfg->hbp + DSI_HBP_FRAME_OVERHEAD; + if (output->dev->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE) + dsi_hss_hsa_hse_hbp += dsi_cfg->hsa + DSI_HSA_FRAME_OVERHEAD; + + /* + * Make sure DPI(HFP) > DSI(HSS+HSA+HSE+HBP) to guarantee that the FIFO + * is empty before we start a receiving a new line on the DPI + * interface. + */ + if ((u64)phy_cfg->hs_clk_rate * + mode_to_dpi_hfp(mode, mode_valid_check) * nlanes < + (u64)dsi_hss_hsa_hse_hbp * + (mode_valid_check ? mode->clock : mode->crtc_clock) * 1000) + return -EINVAL; + + return 0; +} + +static int cdns_dsi_bridge_attach(struct drm_bridge *bridge, + enum drm_bridge_attach_flags flags) +{ + struct cdns_dsi_input *input = bridge_to_cdns_dsi_input(bridge); + struct cdns_dsi *dsi = input_to_dsi(input); + struct cdns_dsi_output *output = &dsi->output; + + if (!drm_core_check_feature(bridge->dev, DRIVER_ATOMIC)) { + dev_err(dsi->base.dev, + "cdns-dsi driver is only compatible with DRM devices supporting atomic updates"); + return -EOPNOTSUPP; + } + + return drm_bridge_attach(bridge->encoder, output->bridge, bridge, + flags); +} + +static enum drm_mode_status +cdns_dsi_bridge_mode_valid(struct drm_bridge *bridge, + const struct drm_display_info *info, + const struct drm_display_mode *mode) +{ + struct cdns_dsi_input *input = bridge_to_cdns_dsi_input(bridge); + struct cdns_dsi *dsi = input_to_dsi(input); + struct cdns_dsi_output *output = &dsi->output; + struct cdns_dsi_cfg dsi_cfg; + int bpp, ret; + + /* + * VFP_DSI should be less than VFP_DPI and VFP_DSI should be at + * least 1. + */ + if (mode->vtotal - mode->vsync_end < 2) + return MODE_V_ILLEGAL; + + /* VSA_DSI = VSA_DPI and must be at least 2. */ + if (mode->vsync_end - mode->vsync_start < 2) + return MODE_V_ILLEGAL; + + /* HACT must be 32-bits aligned. */ + bpp = mipi_dsi_pixel_format_to_bpp(output->dev->format); + if ((mode->hdisplay * bpp) % 32) + return MODE_H_ILLEGAL; + + ret = cdns_dsi_check_conf(dsi, mode, &dsi_cfg, true); + if (ret) + return MODE_BAD; + + return MODE_OK; +} + +static void cdns_dsi_bridge_disable(struct drm_bridge *bridge) +{ + struct cdns_dsi_input *input = bridge_to_cdns_dsi_input(bridge); + struct cdns_dsi *dsi = input_to_dsi(input); + u32 val; + + dsi->link_initialized = false; + val = readl(dsi->regs + MCTL_MAIN_DATA_CTL); + val &= ~(IF_VID_SELECT_MASK | IF_VID_MODE | VID_EN | HOST_EOT_GEN | + DISP_EOT_GEN); + writel(val, dsi->regs + MCTL_MAIN_DATA_CTL); + + val = readl(dsi->regs + MCTL_MAIN_EN) & ~IF_EN(input->id); + writel(val, dsi->regs + MCTL_MAIN_EN); + pm_runtime_put(dsi->base.dev); + phy_power_off(dsi->dphy); + phy_exit(dsi->dphy); +} + +static void release_txbyte_rst(void) +{ + void __iomem *regs = ioremap(0x12250000, 0x10000); + + u32 temp = readl(regs + SRST_ASSERT0); + + temp &= ~(0x1 << 18); + temp |= (0x0 & 0x1) << 18; + + writel(temp, regs + SRST_ASSERT0); + + do { + temp = readl(regs + SRST_STATUS0) >> 18; + temp &= 0x1; + } while (temp != 0x1); + //udelay(1); +} + +static void cdns_dsi_hs_init(struct cdns_dsi *dsi) +{ + struct cdns_dsi_output *output = &dsi->output; + u32 dpi_fifo_int = 0; + + /* + * Power all internal DPHY blocks down and maintain their reset line + * asserted before changing the DPHY config. + */ + writel(DPHY_CMN_PSO | DPHY_PLL_PSO | DPHY_ALL_D_PDN | DPHY_C_PDN | + DPHY_CMN_PDN | DPHY_PLL_PDN, + dsi->regs + MCTL_DPHY_CFG0); + + phy_init(dsi->dphy); + phy_set_mode(dsi->dphy, PHY_MODE_MIPI_DPHY); + phy_configure(dsi->dphy, &output->phy_opts); + phy_power_on(dsi->dphy); + release_txbyte_rst(); + + writel(PLL_LOCKED, dsi->regs + MCTL_MAIN_STS_CLR); + writel(DPHY_CMN_PSO | DPHY_ALL_D_PDN | DPHY_C_PDN | DPHY_CMN_PDN, + dsi->regs + MCTL_DPHY_CFG0); + mdelay(100); + /* De-assert data and clock reset lines. */ + writel(DPHY_CMN_PSO | DPHY_ALL_D_PDN | DPHY_C_PDN | DPHY_CMN_PDN | + DPHY_D_RSTB(output->dev->lanes) | DPHY_C_RSTB, + dsi->regs + MCTL_DPHY_CFG0); + + dpi_fifo_int = readl(dsi->regs + DPI_IRQ_CLR); + if (dpi_fifo_int) + writel(1, dsi->regs + DPI_IRQ_CLR); +} + +static void cdns_dsi_init_link(struct cdns_dsi *dsi) +{ + struct cdns_dsi_output *output = &dsi->output; + unsigned long sysclk_period, ulpout; + u32 val; + int i; + + if (dsi->link_initialized) + return; + + val = 0; + for (i = 1; i < output->dev->lanes; i++) + val |= DATA_LANE_EN(i); + + if (!(output->dev->mode_flags & MIPI_DSI_CLOCK_NON_CONTINUOUS)) + val |= CLK_CONTINUOUS; + + writel(val, dsi->regs + MCTL_MAIN_PHY_CTL); + + /* ULPOUT should be set to 1ms and is expressed in sysclk cycles.*/ + sysclk_period = NSEC_PER_SEC / clk_get_rate(dsi->dsi_sys_clk); + ulpout = DIV_ROUND_UP(NSEC_PER_MSEC, sysclk_period); + writel(CLK_LANE_ULPOUT_TIME(ulpout) | DATA_LANE_ULPOUT_TIME(ulpout), + dsi->regs + MCTL_ULPOUT_TIME); + + writel(LINK_EN, dsi->regs + MCTL_MAIN_DATA_CTL); + + val = CLK_LANE_EN | PLL_START; + for (i = 0; i < output->dev->lanes; i++) + val |= DATA_LANE_START(i); + + writel(val, dsi->regs + MCTL_MAIN_EN); + + dsi->link_initialized = true; +} + +static void cdns_dsi_bridge_enable(struct drm_bridge *bridge) +{ + struct cdns_dsi_input *input = bridge_to_cdns_dsi_input(bridge); + struct cdns_dsi *dsi = input_to_dsi(input); + struct cdns_dsi_output *output = &dsi->output; + struct drm_display_mode *mode; + struct phy_configure_opts_mipi_dphy *phy_cfg = &output->phy_opts.mipi_dphy; + unsigned long tx_byte_period; + struct cdns_dsi_cfg dsi_cfg; + u32 tmp, reg_wakeup; + int nlanes; + + if (WARN_ON(pm_runtime_get_sync(dsi->base.dev) < 0)) + return; + + mode = &bridge->encoder->crtc->state->adjusted_mode; + nlanes = output->dev->lanes; + + WARN_ON_ONCE(cdns_dsi_check_conf(dsi, mode, &dsi_cfg, false)); + + cdns_dsi_hs_init(dsi); + cdns_dsi_init_link(dsi); + + writel(HBP_LEN(dsi_cfg.hbp) | HSA_LEN(dsi_cfg.hsa), + dsi->regs + VID_HSIZE1); + writel(HFP_LEN(dsi_cfg.hfp) | HACT_LEN(dsi_cfg.hact), + dsi->regs + VID_HSIZE2); + + writel(VBP_LEN(mode->crtc_vtotal - mode->crtc_vsync_end - 1) | + VFP_LEN(mode->crtc_vsync_start - mode->crtc_vdisplay) | + VSA_LEN(mode->crtc_vsync_end - mode->crtc_vsync_start + 1), + dsi->regs + VID_VSIZE1); + writel(mode->crtc_vdisplay, dsi->regs + VID_VSIZE2); + + tmp = dsi_cfg.htotal - + (dsi_cfg.hsa + DSI_BLANKING_FRAME_OVERHEAD + + DSI_HSA_FRAME_OVERHEAD); + writel(BLK_LINE_PULSE_PKT_LEN(tmp), dsi->regs + VID_BLKSIZE2); + if (output->dev->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE) + writel(MAX_LINE_LIMIT(tmp - DSI_NULL_FRAME_OVERHEAD), + dsi->regs + VID_VCA_SETTING2); + + tmp = dsi_cfg.htotal - + (DSI_HSS_VSS_VSE_FRAME_OVERHEAD + DSI_BLANKING_FRAME_OVERHEAD); + writel(BLK_LINE_EVENT_PKT_LEN(tmp), dsi->regs + VID_BLKSIZE1); + if (!(output->dev->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)) + writel(MAX_LINE_LIMIT(tmp - DSI_NULL_FRAME_OVERHEAD), + dsi->regs + VID_VCA_SETTING2); + + tmp = DIV_ROUND_UP(dsi_cfg.htotal, nlanes) - + DIV_ROUND_UP(dsi_cfg.hsa, nlanes); + + if (!(output->dev->mode_flags & MIPI_DSI_MODE_NO_EOT_PACKET)) + tmp -= DIV_ROUND_UP(DSI_EOT_PKT_SIZE, nlanes); + + tx_byte_period = DIV_ROUND_DOWN_ULL((u64)NSEC_PER_SEC * 8, + phy_cfg->hs_clk_rate); + reg_wakeup = (phy_cfg->hs_prepare + phy_cfg->hs_zero) / tx_byte_period; + writel(REG_WAKEUP_TIME(reg_wakeup) | REG_LINE_DURATION(tmp), + dsi->regs + VID_DPHY_TIME); + + writel(0xafffb, dsi->regs + MCTL_DPHY_TIMEOUT1); + writel(0x3ffff, dsi->regs + MCTL_DPHY_TIMEOUT2); + writel(0x3ab05, dsi->regs + MCTL_ULPOUT_TIME); + + if (output->dev->mode_flags & MIPI_DSI_MODE_VIDEO) { + switch (output->dev->format) { + case MIPI_DSI_FMT_RGB888: + tmp = VID_PIXEL_MODE_RGB888 | + VID_DATATYPE(MIPI_DSI_PACKED_PIXEL_STREAM_24); + break; + + case MIPI_DSI_FMT_RGB666: + tmp = VID_PIXEL_MODE_RGB666 | + VID_DATATYPE(MIPI_DSI_PIXEL_STREAM_3BYTE_18); + break; + + case MIPI_DSI_FMT_RGB666_PACKED: + tmp = VID_PIXEL_MODE_RGB666_PACKED | + VID_DATATYPE(MIPI_DSI_PACKED_PIXEL_STREAM_18); + break; + + case MIPI_DSI_FMT_RGB565: + tmp = VID_PIXEL_MODE_RGB565 | + VID_DATATYPE(MIPI_DSI_PACKED_PIXEL_STREAM_16); + break; + + default: + dev_err(dsi->base.dev, "Unsupported DSI format\n"); + return; + } + + if (output->dev->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE) + tmp |= SYNC_PULSE_ACTIVE | SYNC_PULSE_HORIZONTAL; + + tmp |= REG_BLKLINE_MODE(REG_BLK_MODE_BLANKING_PKT) | + REG_BLKEOL_MODE(REG_BLK_MODE_BLANKING_PKT) | + RECOVERY_MODE(RECOVERY_MODE_NEXT_HSYNC) | + VID_IGNORE_MISS_VSYNC; + + writel(tmp, dsi->regs + VID_MAIN_CTL); + } + + tmp = readl(dsi->regs + MCTL_MAIN_DATA_CTL); + tmp &= ~(IF_VID_SELECT_MASK | HOST_EOT_GEN | IF_VID_MODE); + + if (!(output->dev->mode_flags & MIPI_DSI_MODE_NO_EOT_PACKET)) + tmp |= HOST_EOT_GEN; + + if (output->dev->mode_flags & MIPI_DSI_MODE_VIDEO) + tmp |= IF_VID_MODE | IF_VID_SELECT(input->id) | VID_EN; + + writel(tmp, dsi->regs + MCTL_MAIN_DATA_CTL); + + tmp = readl(dsi->regs + MCTL_MAIN_EN) | IF_EN(input->id); + writel(tmp, dsi->regs + MCTL_MAIN_EN); +} + +static const struct drm_bridge_funcs cdns_dsi_bridge_funcs = { + .attach = cdns_dsi_bridge_attach, + .mode_valid = cdns_dsi_bridge_mode_valid, + .disable = cdns_dsi_bridge_disable, + .enable = cdns_dsi_bridge_enable, +}; + +static int cdns_dsi_attach(struct mipi_dsi_host *host, + struct mipi_dsi_device *dev) +{ + struct cdns_dsi *dsi = to_cdns_dsi(host); + struct cdns_dsi_output *output = &dsi->output; + struct cdns_dsi_input *input = &dsi->input; + struct drm_bridge *bridge; + struct drm_panel *panel; + struct device_node *np; + int ret; + + /* + * We currently do not support connecting several DSI devices to the + * same host. In order to support that we'd need the DRM bridge + * framework to allow dynamic reconfiguration of the bridge chain. + */ + if (output->dev) + return -EBUSY; + + /* We do not support burst mode yet. */ + if (dev->mode_flags & MIPI_DSI_MODE_VIDEO_BURST) + return -EOPNOTSUPP; + + /* + * The host <-> device link might be described using an OF-graph + * representation, in this case we extract the device of_node from + * this representation, otherwise we use dsidev->dev.of_node which + * should have been filled by the core. + */ + np = of_graph_get_remote_node(dsi->base.dev->of_node, DSI_OUTPUT_PORT, + dev->channel); + if (!np) + np = of_node_get(dev->dev.of_node); + + panel = of_drm_find_panel(np); + if (!IS_ERR(panel)) { + bridge = drm_panel_bridge_add_typed(panel, + DRM_MODE_CONNECTOR_DSI); + } else { + bridge = of_drm_find_bridge(dev->dev.of_node); + if (!bridge) + bridge = ERR_PTR(-EINVAL); + } + + of_node_put(np); + + if (IS_ERR(bridge)) { + ret = PTR_ERR(bridge); + dev_err(host->dev, "failed to add DSI device %s (err = %d)", + dev->name, ret); + return ret; + } + + output->dev = dev; + output->bridge = bridge; + output->panel = panel; + + /* + * The DSI output has been properly configured, we can now safely + * register the input to the bridge framework so that it can take place + * in a display pipeline. + */ + drm_bridge_add(&input->bridge); + + return 0; +} + +static int cdns_dsi_detach(struct mipi_dsi_host *host, + struct mipi_dsi_device *dev) +{ + struct cdns_dsi *dsi = to_cdns_dsi(host); + struct cdns_dsi_output *output = &dsi->output; + struct cdns_dsi_input *input = &dsi->input; + + drm_bridge_remove(&input->bridge); + if (output->panel) + drm_panel_bridge_remove(output->bridge); + + return 0; +} + +static irqreturn_t cdns_dsi_interrupt(int irq, void *data) +{ + struct cdns_dsi *dsi = data; + irqreturn_t ret = IRQ_NONE; + u32 flag, ctl; + + flag = readl(dsi->regs + DIRECT_CMD_STS_FLAG); + if (flag) { + ctl = readl(dsi->regs + DIRECT_CMD_STS_CTL); + ctl &= ~flag; + writel(ctl, dsi->regs + DIRECT_CMD_STS_CTL); + complete(&dsi->direct_cmd_comp); + ret = IRQ_HANDLED; + } + + return ret; +} + +static ssize_t cdns_dsi_transfer(struct mipi_dsi_host *host, + const struct mipi_dsi_msg *msg) +{ + struct cdns_dsi *dsi = to_cdns_dsi(host); + u32 cmd, val, wait = WRITE_COMPLETED, ctl = 0; + struct mipi_dsi_packet packet; + int ret, i, tx_len, rx_len; + u32 stat = 0; + int timeout = 100; + int stat_88; + int stat_188; + int stat_88_ack_val; + + ret = pm_runtime_get_sync(host->dev); + if (ret < 0) + return ret; + + cdns_dsi_init_link(dsi); + + ret = mipi_dsi_create_packet(&packet, msg); + if (ret) + goto out; + + tx_len = msg->tx_buf ? msg->tx_len : 0; + rx_len = msg->rx_buf ? msg->rx_len : 0; + + /* For read operations, the maximum TX len is 2. */ + if (rx_len && tx_len > 2) { + ret = -EOPNOTSUPP; + goto out; + } + + /* TX len is limited by the CMD FIFO depth. */ + if (tx_len > dsi->direct_cmd_fifo_depth) { + ret = -EOPNOTSUPP; + goto out; + } + + /* RX len is limited by the RX FIFO depth. */ + if (rx_len > dsi->rx_fifo_depth) { + ret = -EOPNOTSUPP; + goto out; + } + + cmd = CMD_SIZE(tx_len) | CMD_VCHAN_ID(msg->channel) | + CMD_DATATYPE(msg->type); + + if (msg->flags & MIPI_DSI_MSG_USE_LPM) + cmd |= CMD_LP_EN; + + if (mipi_dsi_packet_format_is_long(msg->type)) + cmd |= CMD_LONG; + + if (rx_len) { + cmd |= READ_CMD; + wait = READ_COMPLETED_WITH_ERR | READ_COMPLETED; + ctl = READ_EN | BTA_EN; + } else if (msg->flags & MIPI_DSI_MSG_REQ_ACK) { + cmd |= BTA_REQ; + wait = ACK_WITH_ERR_RCVD | ACK_RCVD; + ctl = BTA_EN; + } + + /* Clear status flags before sending the command. */ + + iowrite32(wait, dsi->regs + DIRECT_CMD_STS_CLR); + iowrite32(wait, dsi->regs + DIRECT_CMD_STS_CTL); + iowrite32(cmd, dsi->regs + DIRECT_CMD_MAIN_SETTINGS); + + for (i = 0; i < tx_len; i += 4) { + const u8 *buf = msg->tx_buf; + int j; + + val = 0; + for (j = 0; j < 4 && j + i < tx_len; j++) + val |= (u32)buf[i + j] << (8 * j); + iowrite32(val, dsi->regs + DIRECT_CMD_WRDATA); + } + iowrite32(0, dsi->regs + DIRECT_CMD_SEND); + + do { + stat = readl(dsi->regs + DIRECT_CMD_STS); + if ((stat & 0x02) == 0x02) + break; + mdelay(10); + } while (--timeout); + if (!timeout) + DRM_DEBUG("timeout!\n"); + + stat_88 = readl(dsi->regs + DIRECT_CMD_STS); + stat_188 = readl(dsi->regs + MCTL_DPHY_ERR_FLAG); + stat_88_ack_val = stat_88 >> 16; + if (stat_188 || stat_88_ack_val) + dev_dbg(host->dev, "stat: [188h] %08x, [88h] %08x\r\n", stat_188, stat_88); + +out: + pm_runtime_put(host->dev); + return ret; +} + +static const struct mipi_dsi_host_ops cdns_dsi_ops = { + .attach = cdns_dsi_attach, + .detach = cdns_dsi_detach, + .transfer = cdns_dsi_transfer, +}; + +static int __maybe_unused cdns_dsi_resume(struct device *dev) +{ + struct cdns_dsi *dsi = dev_get_drvdata(dev); + + reset_control_deassert(dsi->dsi_p_rst); + clk_prepare_enable(dsi->dsi_p_clk); + clk_prepare_enable(dsi->dsi_sys_clk); + + return 0; +} + +static int __maybe_unused cdns_dsi_suspend(struct device *dev) +{ + struct cdns_dsi *dsi = dev_get_drvdata(dev); + + clk_disable_unprepare(dsi->dsi_sys_clk); + clk_disable_unprepare(dsi->dsi_p_clk); + reset_control_assert(dsi->dsi_p_rst); + dsi->link_initialized = false; + return 0; +} + +static UNIVERSAL_DEV_PM_OPS(cdns_dsi_pm_ops, cdns_dsi_suspend, cdns_dsi_resume, + NULL); + +static int cdns_dsi_drm_remove(struct platform_device *pdev) +{ + struct cdns_dsi *dsi = platform_get_drvdata(pdev); + + mipi_dsi_host_unregister(&dsi->base); + pm_runtime_disable(&pdev->dev); + + return 0; +} + +static const struct of_device_id cdns_dsi_of_match[] = { + { .compatible = "cdns,dsi" }, + { }, +}; + +static int starfive_dsi_bind(struct device *dev, struct device *master, void *data) +{ + struct cdns_dsi *dsi; + struct cdns_dsi_input *input; + struct resource *res; + struct platform_device *pdev = to_platform_device(dev); + //struct drm_device *drm_dev = data; + //struct starfive_drm_private *private = drm_dev->dev_private; + int ret; + u32 val; + + dsi = devm_kzalloc(&pdev->dev, sizeof(*dsi), GFP_KERNEL); + if (!dsi) + return -ENOMEM; + + platform_set_drvdata(pdev, dsi); + input = &dsi->input; + + res = platform_get_resource(pdev, IORESOURCE_MEM, 0); + dsi->regs = devm_ioremap_resource(&pdev->dev, res); + if (IS_ERR(dsi->regs)) + return PTR_ERR(dsi->regs); + + dsi->dphy = devm_phy_get(&pdev->dev, "dphy"); + if (IS_ERR(dsi->dphy)) + return PTR_ERR(dsi->dphy); + + val = readl(dsi->regs + ID_REG); + + if (REV_VENDOR_ID(val) != 0xcad) { + dev_err(&pdev->dev, "invalid vendor id\n"); + ret = -EINVAL; + goto err_disable_pclk; + } + + val = readl(dsi->regs + IP_CONF); + dsi->direct_cmd_fifo_depth = 1 << (DIRCMD_FIFO_DEPTH(val) + 2); + dsi->rx_fifo_depth = RX_FIFO_DEPTH(val); + init_completion(&dsi->direct_cmd_comp); + + writel(0, dsi->regs + MCTL_MAIN_DATA_CTL); + writel(0, dsi->regs + MCTL_MAIN_EN); + writel(0, dsi->regs + MCTL_MAIN_PHY_CTL); + + input->id = CDNS_DPI_INPUT; + input->bridge.funcs = &cdns_dsi_bridge_funcs; + input->bridge.of_node = pdev->dev.of_node; + + //drm_bridge_add(&input->bridge); + + /* Mask all interrupts before registering the IRQ handler. */ + writel(0, dsi->regs + MCTL_MAIN_STS_CTL); + writel(0, dsi->regs + MCTL_DPHY_ERR_CTL1); + writel(0, dsi->regs + CMD_MODE_STS_CTL); + writel(0, dsi->regs + DIRECT_CMD_STS_CTL); + writel(0, dsi->regs + DIRECT_CMD_RD_STS_CTL); + writel(0, dsi->regs + VID_MODE_STS_CTL); + writel(0, dsi->regs + TVG_STS_CTL); + writel(0, dsi->regs + DPI_IRQ_EN); + + pm_runtime_enable(&pdev->dev); + dsi->base.dev = &pdev->dev; + dsi->base.ops = &cdns_dsi_ops; + + ret = mipi_dsi_host_register(&dsi->base); + if (ret) + goto err_disable_runtime_pm; + + init_seeed_panel(); + return 0; + +err_disable_runtime_pm: + pm_runtime_disable(&pdev->dev); + +err_disable_pclk: + //clk_disable_unprepare(dsi->dsi_p_clk); + + return ret; +} + +static void starfive_dsi_unbind(struct device *dev, struct device *master, void *data) +{ + struct platform_device *pdev = to_platform_device(dev); + struct cdns_dsi *dsi = platform_get_drvdata(pdev); + + exit_seeed_panel(); + mipi_dsi_host_unregister(&dsi->base); + pm_runtime_disable(dev); +} + +static const struct component_ops starfive_dsi_component_ops = { + .bind = starfive_dsi_bind, + .unbind = starfive_dsi_unbind, +}; + +static int starfive_dsi_probe(struct platform_device *pdev) +{ + return component_add(&pdev->dev, &starfive_dsi_component_ops); +} + +static int starfive_dsi_remove(struct platform_device *pdev) +{ + component_del(&pdev->dev, &starfive_dsi_component_ops); + return 0; +} + +struct platform_driver starfive_dsi_platform_driver = { + .probe = starfive_dsi_probe, + .remove = starfive_dsi_remove, + .driver = { + .name = "cdns-dsi", + .of_match_table = cdns_dsi_of_match, + .pm = &cdns_dsi_pm_ops, + }, +}; + +MODULE_AUTHOR("Boris Brezillon <boris.brezillon@bootlin.com>"); +MODULE_DESCRIPTION("Cadence DSI driver"); +MODULE_LICENSE("GPL"); +MODULE_ALIAS("platform:cdns-dsi"); + diff --git a/drivers/gpu/drm/starfive/starfive_drm_encoder.c b/drivers/gpu/drm/starfive/starfive_drm_encoder.c index 30a37ad8264c..04d00da89f59 100644 --- a/drivers/gpu/drm/starfive/starfive_drm_encoder.c +++ b/drivers/gpu/drm/starfive/starfive_drm_encoder.c @@ -19,7 +19,6 @@ static void starfive_encoder_destroy(struct drm_encoder *encoder) { drm_encoder_cleanup(encoder); - kfree(encoder); } static const struct drm_encoder_funcs starfive_encoder_funcs = { @@ -54,6 +53,12 @@ static int starfive_encoder_bind(struct device *dev, struct device *master, void } } +#ifdef CONFIG_DRM_STARFIVE_MIPI_DSI + encoderp->encoder_type = DRM_MODE_ENCODER_DSI; +#else + encoderp->encoder_type = DRM_MODE_ENCODER_TMDS; +#endif + /* If no CRTCs were found, fall back to our old behaviour */ if (crtcs == 0) { dev_warn(dev, "Falling back to first CRTC\n"); @@ -68,8 +73,13 @@ static int starfive_encoder_bind(struct device *dev, struct device *master, void if (ret) goto err_encoder; +#ifdef CONFIG_DRM_STARFIVE_MIPI_DSI + ret = drm_of_find_panel_or_bridge(dev->of_node, 1, 0, + &tmp_panel, &tmp_bridge); +#else ret = drm_of_find_panel_or_bridge(dev->of_node, 0, 0, - &tmp_panel, &tmp_bridge); + &tmp_panel, &tmp_bridge); +#endif if (ret) { dev_err_probe(dev, ret, "endpoint returns %d\n", ret); goto err_bridge; @@ -96,8 +106,6 @@ err_encoder: static void starfive_encoder_unbind(struct device *dev, struct device *master, void *data) { struct starfive_encoder *encoderp = dev_get_drvdata(dev); - - starfive_encoder_destroy(&encoderp->encoder); } static const struct component_ops starfive_encoder_component_ops = { diff --git a/drivers/gpu/drm/starfive/starfive_drm_seeedpanel.c b/drivers/gpu/drm/starfive/starfive_drm_seeedpanel.c new file mode 100644 index 000000000000..0798e50b3dcf --- /dev/null +++ b/drivers/gpu/drm/starfive/starfive_drm_seeedpanel.c @@ -0,0 +1,514 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2021 StarFive Technology Co., Ltd. + * + * reference to seeed5inch.c + */ +#include <linux/device.h> +#include <linux/delay.h> +#include <linux/i2c.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/of.h> +#include <linux/of_device.h> +#include <linux/of_graph.h> +#include <drm/drm_crtc.h> +#include <drm/drm_device.h> +#include <drm/drm_mipi_dsi.h> +#include <drm/drm_panel.h> +#include "starfive_drm_drv.h" +#define RPI_DSI_DRIVER_NAME "cdns-dri-panel" + +/* I2C registers of the Atmel microcontroller. */ +enum REG_ADDR { + REG_ID = 0x80, + REG_PORTA, /* BIT(2) for horizontal flip, BIT(3) for vertical flip */ + REG_PORTB, + REG_PORTC, + REG_PORTD, + REG_POWERON, + REG_PWM, + REG_DDRA, + REG_DDRB, + REG_DDRC, + REG_DDRD, + REG_TEST, + REG_WR_ADDRL, + REG_WR_ADDRH, + REG_READH, + REG_READL, + REG_WRITEH, + REG_WRITEL, + REG_ID2, +}; + +/* DSI D-PHY Layer Registers */ +#define D0W_DPHYCONTTX 0x0004 +#define CLW_DPHYCONTRX 0x0020 +#define D0W_DPHYCONTRX 0x0024 +#define D1W_DPHYCONTRX 0x0028 +#define COM_DPHYCONTRX 0x0038 +#define CLW_CNTRL 0x0040 +#define D0W_CNTRL 0x0044 +#define D1W_CNTRL 0x0048 +#define DFTMODE_CNTRL 0x0054 + +/* DSI PPI Layer Registers */ +#define PPI_STARTPPI 0x0104 +#define PPI_BUSYPPI 0x0108 +#define PPI_LINEINITCNT 0x0110 +#define PPI_LPTXTIMECNT 0x0114 +#define PPI_CLS_ATMR 0x0140 +#define PPI_D0S_ATMR 0x0144 +#define PPI_D1S_ATMR 0x0148 +#define PPI_D0S_CLRSIPOCOUNT 0x0164 +#define PPI_D1S_CLRSIPOCOUNT 0x0168 +#define CLS_PRE 0x0180 +#define D0S_PRE 0x0184 +#define D1S_PRE 0x0188 +#define CLS_PREP 0x01A0 +#define D0S_PREP 0x01A4 +#define D1S_PREP 0x01A8 +#define CLS_ZERO 0x01C0 +#define D0S_ZERO 0x01C4 +#define D1S_ZERO 0x01C8 +#define PPI_CLRFLG 0x01E0 +#define PPI_CLRSIPO 0x01E4 +#define HSTIMEOUT 0x01F0 +#define HSTIMEOUTENABLE 0x01F4 + +/* DSI Protocol Layer Registers */ +#define DSI_STARTDSI 0x0204 +#define DSI_BUSYDSI 0x0208 +#define DSI_LANEENABLE 0x0210 +#define DSI_LANEENABLE_CLOCK BIT(0) +#define DSI_LANEENABLE_D0 BIT(1) +#define DSI_LANEENABLE_D1 BIT(2) + +#define DSI_LANESTATUS0 0x0214 +#define DSI_LANESTATUS1 0x0218 +#define DSI_INTSTATUS 0x0220 +#define DSI_INTMASK 0x0224 +#define DSI_INTCLR 0x0228 +#define DSI_LPTXTO 0x0230 +#define DSI_MODE 0x0260 +#define DSI_PAYLOAD0 0x0268 +#define DSI_PAYLOAD1 0x026C +#define DSI_SHORTPKTDAT 0x0270 +#define DSI_SHORTPKTREQ 0x0274 +#define DSI_BTASTA 0x0278 +#define DSI_BTACLR 0x027C + +/* DSI General Registers */ +#define DSIERRCNT 0x0300 +#define DSISIGMOD 0x0304 + +/* DSI Application Layer Registers */ +#define APLCTRL 0x0400 +#define APLSTAT 0x0404 +#define APLERR 0x0408 +#define PWRMOD 0x040C +#define RDPKTLN 0x0410 +#define PXLFMT 0x0414 +#define MEMWRCMD 0x0418 + +/* LCDC/DPI Host Registers */ +#define LCDCTRL 0x0420 +#define HSR 0x0424 +#define HDISPR 0x0428 +#define VSR 0x042C +#define VDISPR 0x0430 +#define VFUEN 0x0434 + +/* DBI-B Host Registers */ +#define DBIBCTRL 0x0440 + +/* SPI Master Registers */ +#define SPICMR 0x0450 +#define SPITCR 0x0454 + +/* System Controller Registers */ +#define SYSSTAT 0x0460 +#define SYSCTRL 0x0464 +#define SYSPLL1 0x0468 +#define SYSPLL2 0x046C +#define SYSPLL3 0x0470 +#define SYSPMCTRL 0x047C + +/* GPIO Registers */ +#define GPIOC 0x0480 +#define GPIOO 0x0484 +#define GPIOI 0x0488 + +/* I2C Registers */ +#define I2CCLKCTRL 0x0490 + +/* Chip/Rev Registers */ +#define IDREG 0x04A0 + +/* Debug Registers */ +#define WCMDQUEUE 0x0500 +#define RCMDQUEUE 0x0504 + +struct seeed_panel_dev { + struct i2c_client *client; + struct drm_panel base; + struct mipi_dsi_device *dsi; + + struct device *dev; + int irq; + +}; + +static int seeed_panel_i2c_write(struct i2c_client *client, u8 reg, u8 val) +{ + struct i2c_msg msg; + u8 buf[2]; + int ret; + + buf[0] = reg; + buf[1] = val; + msg.addr = client->addr; + msg.flags = 0; + msg.buf = buf; + msg.len = 2; + + ret = i2c_transfer(client->adapter, &msg, 1); + if (ret >= 0) + return 0; + + return ret; +} + +static int seeed_panel_i2c_read(struct i2c_client *client, u8 reg, u8 *val) +{ + struct i2c_msg msg[2]; + u8 buf[2]; + int ret; + + buf[0] = reg; + msg[0].addr = client->addr; + msg[0].flags = 0; + msg[0].buf = buf; + msg[0].len = 1; + msg[1].addr = client->addr; + msg[1].flags = I2C_M_RD; + msg[1].buf = val; + msg[1].len = 1; + ret = i2c_transfer(client->adapter, msg, 2); + if (ret >= 0) + return 0; + + return ret; +} + +enum dsi_rgb_pattern_t { + RGB_PAT_WHITE, + RGB_PAT_BLACK, + RGB_PAT_RED, + RGB_PAT_GREEN, + RGB_PAT_BLUE, + RGB_PAT_HORIZ_COLORBAR, + RGB_PAT_VERT_COLORBAR, + RGB_PAT_NUM +}; + +static struct seeed_panel_dev *panel_to_seeed(struct drm_panel *panel) +{ + return container_of(panel, struct seeed_panel_dev, base); +} + +static const struct drm_display_mode seeed_panel_modes[] = { + { + .clock = 33000000 / 1000, + .hdisplay = 800, + .hsync_start = 800 + 50, + .hsync_end = 800 + 50 + 20, + .htotal = 800 + 50 + 20 + 10, + .vdisplay = 480, + .vsync_start = 480 + 135, + .vsync_end = 480 + 135 + 5, + .vtotal = 480 + 135 + 5 + 5, + }, +}; + +static int seeed_panel_disable(struct drm_panel *panel) +{ + struct seeed_panel_dev *sp = panel_to_seeed(panel); + + seeed_panel_i2c_write(sp->client, REG_PWM, 0); + seeed_panel_i2c_write(sp->client, REG_POWERON, 0); + udelay(1); + + return 0; +} + +static int seeed_panel_noop(struct drm_panel *panel) +{ + return 0; +} + +static int seeed_dsi_write(struct drm_panel *panel, u16 reg, u32 val) +{ + struct seeed_panel_dev *sp = panel_to_seeed(panel); + + u8 msg[] = { + reg, + reg >> 8, + val, + val >> 8, + val >> 16, + val >> 24, + }; + mipi_dsi_generic_write(sp->dsi, msg, sizeof(msg)); + + return 0; +} + +static int seeed_panel_enable(struct drm_panel *panel) +{ + struct seeed_panel_dev *sp = panel_to_seeed(panel); + int i; + u8 reg_value = 0; + + seeed_panel_i2c_write(sp->client, REG_POWERON, 1); + /* Wait for nPWRDWN to go low to indicate poweron is done. */ + for (i = 0; i < 100; i++) { + seeed_panel_i2c_read(sp->client, REG_PORTB, ®_value); + if (reg_value & 1) + break; + } + + seeed_dsi_write(panel, DSI_LANEENABLE, + DSI_LANEENABLE_CLOCK | + DSI_LANEENABLE_D0); + seeed_dsi_write(panel, PPI_D0S_CLRSIPOCOUNT, 0x05); + seeed_dsi_write(panel, PPI_D1S_CLRSIPOCOUNT, 0x05); + seeed_dsi_write(panel, PPI_D0S_ATMR, 0x00); + seeed_dsi_write(panel, PPI_D1S_ATMR, 0x00); + seeed_dsi_write(panel, PPI_LPTXTIMECNT, 0x03); + seeed_dsi_write(panel, SPICMR, 0x00); + seeed_dsi_write(panel, LCDCTRL, 0x00100150); + seeed_dsi_write(panel, SYSCTRL, 0x040f); + msleep(100); + + seeed_dsi_write(panel, PPI_STARTPPI, 0x01); + seeed_dsi_write(panel, DSI_STARTDSI, 0x01); + msleep(100); + + /* Turn on the backlight. */ + seeed_panel_i2c_write(sp->client, REG_PWM, 255); + + /* Default to the same orientation as the closed source + * firmware used for the panel. Runtime rotation + * configuration will be supported using VC4's plane + * orientation bits. + */ + seeed_panel_i2c_write(sp->client, REG_PORTA, BIT(2)); + + return 0; +} + +static int seeed_panel_get_modes(struct drm_panel *panel, + struct drm_connector *connector) +{ + unsigned int i, num = 0; + static const u32 bus_format = MEDIA_BUS_FMT_RGB888_1X24; + + for (i = 0; i < ARRAY_SIZE(seeed_panel_modes); i++) { + const struct drm_display_mode *m = &seeed_panel_modes[i]; + struct drm_display_mode *mode; + + mode = drm_mode_duplicate(connector->dev, m); + if (!mode) { + dev_err(panel->dev, "failed to add mode %ux%u@%u\n", + m->hdisplay, m->vdisplay, + drm_mode_vrefresh(m)); + continue; + } + + mode->type |= DRM_MODE_TYPE_DRIVER; + + if (i == 0) + mode->type |= DRM_MODE_TYPE_PREFERRED; + + drm_mode_set_name(mode); + drm_mode_probed_add(connector, mode); + num++; + } + + connector->display_info.bpc = 8; + connector->display_info.width_mm = 154; + connector->display_info.height_mm = 86; + drm_display_info_set_bus_formats(&connector->display_info, + &bus_format, 1); + + return num; +} + +static const struct drm_panel_funcs seeed_panel_funcs = { + .disable = seeed_panel_disable, + .unprepare = seeed_panel_noop, + .prepare = seeed_panel_noop, + .enable = seeed_panel_enable, + .get_modes = seeed_panel_get_modes, +}; + +static int seeed_panel_probe(struct i2c_client *client, const struct i2c_device_id *id) +{ + u8 reg_value = 0; + struct seeed_panel_dev *seeed_panel; + struct device_node *endpoint, *dsi_host_node; + struct mipi_dsi_host *host; + struct device *dev = &client->dev; + + struct mipi_dsi_device_info info = { + .type = RPI_DSI_DRIVER_NAME, + .channel = 0, //0, + .node = NULL, + }; + + if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { + dev_warn(&client->dev, + "I2C adapter doesn't support I2C_FUNC_SMBUS_BYTE\n"); + return -EIO; + } + + seeed_panel = devm_kzalloc(&client->dev, sizeof(struct seeed_panel_dev), GFP_KERNEL); + if (!seeed_panel) + return -ENOMEM; + + seeed_panel->client = client; + i2c_set_clientdata(client, seeed_panel); + + seeed_panel_i2c_read(client, REG_ID, ®_value); + switch (reg_value) { + case 0xde: /* ver 1 */ + case 0xc3: /* ver 2 */ + break; + + default: + dev_err(&client->dev, "Unknown Atmel firmware revision: 0x%02x\n", reg_value); + return -ENODEV; + } + + seeed_panel_i2c_write(client, REG_POWERON, 0); + + endpoint = of_graph_get_next_endpoint(dev->of_node, NULL); + if (!endpoint) + return -ENODEV; + + dsi_host_node = of_graph_get_remote_port_parent(endpoint); + if (!dsi_host_node) + goto error; + + host = of_find_mipi_dsi_host_by_node(dsi_host_node); + of_node_put(dsi_host_node); + if (!host) { + of_node_put(endpoint); + return -EPROBE_DEFER; + } + + drm_panel_init(&seeed_panel->base, dev, &seeed_panel_funcs, + DRM_MODE_CONNECTOR_DSI); + + /* This appears last, as it's what will unblock the DSI host + * driver's component bind function. + */ + drm_panel_add(&seeed_panel->base); + + info.node = of_node_get(of_graph_get_remote_port(endpoint)); + if (!info.node) + goto error; + + of_node_put(endpoint); + + seeed_panel->dsi = mipi_dsi_device_register_full(host, &info); + if (IS_ERR(seeed_panel->dsi)) { + dev_err(dev, "DSI device registration failed: %ld\n", + PTR_ERR(seeed_panel->dsi)); + return PTR_ERR(seeed_panel->dsi); + } + + return 0; +error: + of_node_put(endpoint); + return -ENODEV; + +} + +static int seeed_panel_remove(struct i2c_client *client) +{ + struct seeed_panel_dev *seeed_panel = i2c_get_clientdata(client); + + mipi_dsi_detach(seeed_panel->dsi); + + drm_panel_remove(&seeed_panel->base); + + mipi_dsi_device_unregister(seeed_panel->dsi); + // kfree(seeed_panel->dsi); + return 0; +} + +static const struct i2c_device_id seeed_panel_id[] = { + { "seeed_panel", 0 }, + { } +}; + +static const struct of_device_id seeed_panel_dt_ids[] = { + { .compatible = "seeed_panel", }, + { /* sentinel */ } +}; + +static struct i2c_driver seeed_panel_driver = { + .driver = { + .owner = THIS_MODULE, + .name = "seeed_panel", + .of_match_table = seeed_panel_dt_ids, + }, + .probe = seeed_panel_probe, + .remove = seeed_panel_remove, + .id_table = seeed_panel_id, +}; + +static int seeed_dsi_probe(struct mipi_dsi_device *dsi) +{ + int ret; + + dsi->mode_flags = (MIPI_DSI_MODE_VIDEO | + MIPI_DSI_MODE_VIDEO_SYNC_PULSE | + MIPI_DSI_MODE_LPM); + dsi->format = MIPI_DSI_FMT_RGB888; + dsi->lanes = 1; + + ret = mipi_dsi_attach(dsi); + if (ret) + dev_err(&dsi->dev, "failed to attach dsi to host: %d\n", ret); + + return ret; +} + +static struct mipi_dsi_driver seeed_dsi_driver = { + .driver.name = RPI_DSI_DRIVER_NAME, + .probe = seeed_dsi_probe, +}; + +int init_seeed_panel(void) +{ + int err; + + mipi_dsi_driver_register(&seeed_dsi_driver); + err = i2c_add_driver(&seeed_panel_driver); + return err; +} +EXPORT_SYMBOL(init_seeed_panel); + +void exit_seeed_panel(void) +{ + i2c_del_driver(&seeed_panel_driver); + mipi_dsi_driver_unregister(&seeed_dsi_driver); +} +EXPORT_SYMBOL(exit_seeed_panel); + +MODULE_DESCRIPTION("A driver for seeed_panel"); +MODULE_LICENSE("GPL"); diff --git a/drivers/gpu/drm/starfive/starfive_drm_vpp.c b/drivers/gpu/drm/starfive/starfive_drm_vpp.c index 3c732fbcbd14..1c1775ffe6b4 100644 --- a/drivers/gpu/drm/starfive/starfive_drm_vpp.c +++ b/drivers/gpu/drm/starfive/starfive_drm_vpp.c @@ -88,7 +88,8 @@ static void pp_output_cfg(struct starfive_crtc *sf_crtc, int cfg = outSel | progInter << PP_INTERLACE | desformat << PP_DES_FORMAT | ptMode << PP_POINTER_MODE; - int preCfg = sf_fb_vppread32(sf_crtc, ppNum, PP_CTRL1) & 0xffff8f0U; + //int preCfg = sf_fb_vppread32(sf_crtc, ppNum, PP_CTRL1) & 0xffff8f0U; //20211229 disabled + int preCfg = sf_fb_vppread32(sf_crtc, ppNum, PP_CTRL1) & 0xfffff8f0U; //20211229 changed sf_fb_vppwrite32(sf_crtc, ppNum, PP_CTRL1, cfg | preCfg); dev_dbg(sf_crtc->dev, "PP%d outSel: %d, outFormat: 0x%x, Out Interlace: %d, ptMode: %d\n", @@ -297,11 +298,10 @@ static void pp_srcfmt_set(struct starfive_crtc *sf_crtc, int ppNum, struct pp_vi break; case COLOR_YUV420_NV21: pp_srcfmt_cfg(sf_crtc, ppNum, PP_SRC_YUV420I, 0x1, 0, - COLOR_YUV420_NV21 - COLOR_YUV420_NV21, 0x0); - break; + COLOR_YUV420_NV12 - COLOR_YUV420_NV21, 0x0); case COLOR_YUV420_NV12: pp_srcfmt_cfg(sf_crtc, ppNum, PP_SRC_YUV420I, 0x1, 0, - COLOR_YUV420_NV12 - COLOR_YUV420_NV21, 0x0); + COLOR_YUV420_NV12 - COLOR_YUV420_NV12, 0x0); break; case COLOR_RGB888_ARGB: pp_srcfmt_cfg(sf_crtc, ppNum, PP_SRC_GRB888, 0x0, 0x0, @@ -694,10 +694,7 @@ int starfive_pp_update(struct starfive_crtc *sf_crtc) if (sf_crtc->pp[pp_id].inited == 1) { ret = starfive_pp_video_mode_init(sf_crtc, &src, &dst, pp_id); if (!ret) { - if (sf_crtc->ddr_format_change) pp_format_set(sf_crtc, pp_id, &src, &dst); - - if (sf_crtc->dma_addr_change) pp_size_set(sf_crtc, pp_id, &src, &dst); } } diff --git a/drivers/phy/m31/Kconfig b/drivers/phy/m31/Kconfig index e6e457669e70..e6e457669e70 100755..100644 --- a/drivers/phy/m31/Kconfig +++ b/drivers/phy/m31/Kconfig diff --git a/drivers/phy/m31/Makefile b/drivers/phy/m31/Makefile index 7ce3391ac42f..7ce3391ac42f 100755..100644 --- a/drivers/phy/m31/Makefile +++ b/drivers/phy/m31/Makefile diff --git a/drivers/phy/m31/phy-m31-dphy-tx0.c b/drivers/phy/m31/phy-m31-dphy-tx0.c index 89faa77896a5..e82e1da50c66 100755..100644 --- a/drivers/phy/m31/phy-m31-dphy-tx0.c +++ b/drivers/phy/m31/phy-m31-dphy-tx0.c @@ -413,8 +413,8 @@ static void polling_dphy_lock(struct sf_dphy *priv) static int sf_dphy_configure(struct phy *phy, union phy_configure_opts *opts) { struct sf_dphy *dphy = phy_get_drvdata(phy); + uint32_t bit_rate = 800000000/1000000UL;//new mipi panel clock setting - uint32_t bit_rate = 700000000/1000000UL;//(1920 * 1080 * bpp / dlanes * fps / 1000000 + 99) / 100 * 100; dphy_config(dphy, bit_rate); reset_dphy(dphy, 1); |