1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2023 MediaTek Inc. 4 */ 5 6 #include <linux/clk.h> 7 #include <linux/component.h> 8 #include <linux/module.h> 9 #include <linux/of_device.h> 10 #include <linux/platform_device.h> 11 #include <linux/pm_runtime.h> 12 #include <linux/soc/mediatek/mtk-cmdq.h> 13 14 #include "mtk_disp_drv.h" 15 #include "mtk_drm_crtc.h" 16 #include "mtk_drm_ddp_comp.h" 17 18 #define PADDING_CONTROL_REG 0x00 19 #define PADDING_BYPASS BIT(0) 20 #define PADDING_ENABLE BIT(1) 21 #define PADDING_PIC_SIZE_REG 0x04 22 #define PADDING_H_REG 0x08 /* horizontal */ 23 #define PADDING_V_REG 0x0c /* vertical */ 24 #define PADDING_COLOR_REG 0x10 25 26 /** 27 * struct mtk_padding - Basic information of the Padding 28 * @clk: Clock of the module 29 * @reg: Virtual address of the Padding for CPU to access 30 * @cmdq_reg: CMDQ setting of the Padding 31 * 32 * Every Padding should have different clock source, register base, and 33 * CMDQ settings, we stored these differences all together. 34 */ 35 struct mtk_padding { 36 struct clk *clk; 37 void __iomem *reg; 38 struct cmdq_client_reg cmdq_reg; 39 }; 40 41 int mtk_padding_clk_enable(struct device *dev) 42 { 43 struct mtk_padding *padding = dev_get_drvdata(dev); 44 45 return clk_prepare_enable(padding->clk); 46 } 47 48 void mtk_padding_clk_disable(struct device *dev) 49 { 50 struct mtk_padding *padding = dev_get_drvdata(dev); 51 52 clk_disable_unprepare(padding->clk); 53 } 54 55 void mtk_padding_start(struct device *dev) 56 { 57 struct mtk_padding *padding = dev_get_drvdata(dev); 58 59 writel(PADDING_ENABLE | PADDING_BYPASS, 60 padding->reg + PADDING_CONTROL_REG); 61 62 /* 63 * Notice that even the padding is in bypass mode, 64 * all the settings must be cleared to 0 or 65 * undefined behaviors could happen 66 */ 67 writel(0, padding->reg + PADDING_PIC_SIZE_REG); 68 writel(0, padding->reg + PADDING_H_REG); 69 writel(0, padding->reg + PADDING_V_REG); 70 writel(0, padding->reg + PADDING_COLOR_REG); 71 } 72 73 void mtk_padding_stop(struct device *dev) 74 { 75 struct mtk_padding *padding = dev_get_drvdata(dev); 76 77 writel(0, padding->reg + PADDING_CONTROL_REG); 78 } 79 80 static int mtk_padding_bind(struct device *dev, struct device *master, void *data) 81 { 82 return 0; 83 } 84 85 static void mtk_padding_unbind(struct device *dev, struct device *master, void *data) 86 { 87 } 88 89 static const struct component_ops mtk_padding_component_ops = { 90 .bind = mtk_padding_bind, 91 .unbind = mtk_padding_unbind, 92 }; 93 94 static int mtk_padding_probe(struct platform_device *pdev) 95 { 96 struct device *dev = &pdev->dev; 97 struct mtk_padding *priv; 98 struct resource *res; 99 int ret; 100 101 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 102 if (!priv) 103 return -ENOMEM; 104 105 priv->clk = devm_clk_get(dev, NULL); 106 if (IS_ERR(priv->clk)) { 107 dev_err(dev, "failed to get clk\n"); 108 return PTR_ERR(priv->clk); 109 } 110 111 priv->reg = devm_platform_get_and_ioremap_resource(pdev, 0, &res); 112 if (IS_ERR(priv->reg)) { 113 dev_err(dev, "failed to do ioremap\n"); 114 return PTR_ERR(priv->reg); 115 } 116 117 #if IS_REACHABLE(CONFIG_MTK_CMDQ) 118 ret = cmdq_dev_get_client_reg(dev, &priv->cmdq_reg, 0); 119 if (ret) { 120 dev_err(dev, "failed to get gce client reg\n"); 121 return ret; 122 } 123 #endif 124 125 platform_set_drvdata(pdev, priv); 126 127 ret = devm_pm_runtime_enable(dev); 128 if (ret) 129 return ret; 130 131 ret = component_add(dev, &mtk_padding_component_ops); 132 if (ret) { 133 pm_runtime_disable(dev); 134 return dev_err_probe(dev, ret, "failed to add component\n"); 135 } 136 137 return 0; 138 } 139 140 static int mtk_padding_remove(struct platform_device *pdev) 141 { 142 component_del(&pdev->dev, &mtk_padding_component_ops); 143 return 0; 144 } 145 146 static const struct of_device_id mtk_padding_driver_dt_match[] = { 147 { .compatible = "mediatek,mt8188-disp-padding" }, 148 { /* sentinel */ } 149 }; 150 MODULE_DEVICE_TABLE(of, mtk_padding_driver_dt_match); 151 152 struct platform_driver mtk_padding_driver = { 153 .probe = mtk_padding_probe, 154 .remove = mtk_padding_remove, 155 .driver = { 156 .name = "mediatek-disp-padding", 157 .owner = THIS_MODULE, 158 .of_match_table = mtk_padding_driver_dt_match, 159 }, 160 }; 161