1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2017 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/of_irq.h> 11 #include <linux/platform_device.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 #include "mtk_drm_drv.h" 18 19 #define DISP_COLOR_CFG_MAIN 0x0400 20 #define DISP_COLOR_START_MT2701 0x0f00 21 #define DISP_COLOR_START_MT8167 0x0400 22 #define DISP_COLOR_START_MT8173 0x0c00 23 #define DISP_COLOR_START(comp) ((comp)->data->color_offset) 24 #define DISP_COLOR_WIDTH(comp) (DISP_COLOR_START(comp) + 0x50) 25 #define DISP_COLOR_HEIGHT(comp) (DISP_COLOR_START(comp) + 0x54) 26 27 #define COLOR_BYPASS_ALL BIT(7) 28 #define COLOR_SEQ_SEL BIT(13) 29 30 struct mtk_disp_color_data { 31 unsigned int color_offset; 32 }; 33 34 /* 35 * struct mtk_disp_color - DISP_COLOR driver structure 36 * @crtc: associated crtc to report irq events to 37 * @data: platform colour driver data 38 */ 39 struct mtk_disp_color { 40 struct drm_crtc *crtc; 41 struct clk *clk; 42 void __iomem *regs; 43 struct cmdq_client_reg cmdq_reg; 44 const struct mtk_disp_color_data *data; 45 }; 46 47 int mtk_color_clk_enable(struct device *dev) 48 { 49 struct mtk_disp_color *color = dev_get_drvdata(dev); 50 51 return clk_prepare_enable(color->clk); 52 } 53 54 void mtk_color_clk_disable(struct device *dev) 55 { 56 struct mtk_disp_color *color = dev_get_drvdata(dev); 57 58 clk_disable_unprepare(color->clk); 59 } 60 61 void mtk_color_config(struct device *dev, unsigned int w, 62 unsigned int h, unsigned int vrefresh, 63 unsigned int bpc, struct cmdq_pkt *cmdq_pkt) 64 { 65 struct mtk_disp_color *color = dev_get_drvdata(dev); 66 67 mtk_ddp_write(cmdq_pkt, w, &color->cmdq_reg, color->regs, DISP_COLOR_WIDTH(color)); 68 mtk_ddp_write(cmdq_pkt, h, &color->cmdq_reg, color->regs, DISP_COLOR_HEIGHT(color)); 69 } 70 71 void mtk_color_start(struct device *dev) 72 { 73 struct mtk_disp_color *color = dev_get_drvdata(dev); 74 75 writel(COLOR_BYPASS_ALL | COLOR_SEQ_SEL, 76 color->regs + DISP_COLOR_CFG_MAIN); 77 writel(0x1, color->regs + DISP_COLOR_START(color)); 78 } 79 80 static int mtk_disp_color_bind(struct device *dev, struct device *master, 81 void *data) 82 { 83 return 0; 84 } 85 86 static void mtk_disp_color_unbind(struct device *dev, struct device *master, 87 void *data) 88 { 89 } 90 91 static const struct component_ops mtk_disp_color_component_ops = { 92 .bind = mtk_disp_color_bind, 93 .unbind = mtk_disp_color_unbind, 94 }; 95 96 static int mtk_disp_color_probe(struct platform_device *pdev) 97 { 98 struct device *dev = &pdev->dev; 99 struct mtk_disp_color *priv; 100 struct resource *res; 101 int ret; 102 103 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 104 if (!priv) 105 return -ENOMEM; 106 107 priv->clk = devm_clk_get(dev, NULL); 108 if (IS_ERR(priv->clk)) { 109 dev_err(dev, "failed to get color clk\n"); 110 return PTR_ERR(priv->clk); 111 } 112 113 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 114 priv->regs = devm_ioremap_resource(dev, res); 115 if (IS_ERR(priv->regs)) { 116 dev_err(dev, "failed to ioremap color\n"); 117 return PTR_ERR(priv->regs); 118 } 119 #if IS_REACHABLE(CONFIG_MTK_CMDQ) 120 ret = cmdq_dev_get_client_reg(dev, &priv->cmdq_reg, 0); 121 if (ret) 122 dev_dbg(dev, "get mediatek,gce-client-reg fail!\n"); 123 #endif 124 125 priv->data = of_device_get_match_data(dev); 126 platform_set_drvdata(pdev, priv); 127 128 ret = component_add(dev, &mtk_disp_color_component_ops); 129 if (ret) 130 dev_err(dev, "Failed to add component: %d\n", ret); 131 132 return ret; 133 } 134 135 static int mtk_disp_color_remove(struct platform_device *pdev) 136 { 137 component_del(&pdev->dev, &mtk_disp_color_component_ops); 138 139 return 0; 140 } 141 142 static const struct mtk_disp_color_data mt2701_color_driver_data = { 143 .color_offset = DISP_COLOR_START_MT2701, 144 }; 145 146 static const struct mtk_disp_color_data mt8167_color_driver_data = { 147 .color_offset = DISP_COLOR_START_MT8167, 148 }; 149 150 static const struct mtk_disp_color_data mt8173_color_driver_data = { 151 .color_offset = DISP_COLOR_START_MT8173, 152 }; 153 154 static const struct of_device_id mtk_disp_color_driver_dt_match[] = { 155 { .compatible = "mediatek,mt2701-disp-color", 156 .data = &mt2701_color_driver_data}, 157 { .compatible = "mediatek,mt8167-disp-color", 158 .data = &mt8167_color_driver_data}, 159 { .compatible = "mediatek,mt8173-disp-color", 160 .data = &mt8173_color_driver_data}, 161 {}, 162 }; 163 MODULE_DEVICE_TABLE(of, mtk_disp_color_driver_dt_match); 164 165 struct platform_driver mtk_disp_color_driver = { 166 .probe = mtk_disp_color_probe, 167 .remove = mtk_disp_color_remove, 168 .driver = { 169 .name = "mediatek-disp-color", 170 .owner = THIS_MODULE, 171 .of_match_table = mtk_disp_color_driver_dt_match, 172 }, 173 }; 174