1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2021 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_GAMMA_EN 0x0000 20 #define GAMMA_EN BIT(0) 21 #define DISP_GAMMA_CFG 0x0020 22 #define GAMMA_LUT_EN BIT(1) 23 #define GAMMA_DITHERING BIT(2) 24 #define DISP_GAMMA_SIZE 0x0030 25 #define DISP_GAMMA_LUT 0x0700 26 27 #define LUT_10BIT_MASK 0x03ff 28 29 struct mtk_disp_gamma_data { 30 bool has_dither; 31 bool lut_diff; 32 }; 33 34 /* 35 * struct mtk_disp_gamma - DISP_GAMMA driver structure 36 */ 37 struct mtk_disp_gamma { 38 struct clk *clk; 39 void __iomem *regs; 40 struct cmdq_client_reg cmdq_reg; 41 const struct mtk_disp_gamma_data *data; 42 }; 43 44 int mtk_gamma_clk_enable(struct device *dev) 45 { 46 struct mtk_disp_gamma *gamma = dev_get_drvdata(dev); 47 48 return clk_prepare_enable(gamma->clk); 49 } 50 51 void mtk_gamma_clk_disable(struct device *dev) 52 { 53 struct mtk_disp_gamma *gamma = dev_get_drvdata(dev); 54 55 clk_disable_unprepare(gamma->clk); 56 } 57 58 void mtk_gamma_set_common(void __iomem *regs, struct drm_crtc_state *state, bool lut_diff) 59 { 60 unsigned int i, reg; 61 struct drm_color_lut *lut; 62 void __iomem *lut_base; 63 u32 word; 64 u32 diff[3] = {0}; 65 66 if (state->gamma_lut) { 67 reg = readl(regs + DISP_GAMMA_CFG); 68 reg = reg | GAMMA_LUT_EN; 69 writel(reg, regs + DISP_GAMMA_CFG); 70 lut_base = regs + DISP_GAMMA_LUT; 71 lut = (struct drm_color_lut *)state->gamma_lut->data; 72 for (i = 0; i < MTK_LUT_SIZE; i++) { 73 74 if (!lut_diff || (i % 2 == 0)) { 75 word = (((lut[i].red >> 6) & LUT_10BIT_MASK) << 20) + 76 (((lut[i].green >> 6) & LUT_10BIT_MASK) << 10) + 77 ((lut[i].blue >> 6) & LUT_10BIT_MASK); 78 } else { 79 diff[0] = (lut[i].red >> 6) - (lut[i - 1].red >> 6); 80 diff[1] = (lut[i].green >> 6) - (lut[i - 1].green >> 6); 81 diff[2] = (lut[i].blue >> 6) - (lut[i - 1].blue >> 6); 82 83 word = ((diff[0] & LUT_10BIT_MASK) << 20) + 84 ((diff[1] & LUT_10BIT_MASK) << 10) + 85 (diff[2] & LUT_10BIT_MASK); 86 } 87 writel(word, (lut_base + i * 4)); 88 } 89 } 90 } 91 92 void mtk_gamma_set(struct device *dev, struct drm_crtc_state *state) 93 { 94 struct mtk_disp_gamma *gamma = dev_get_drvdata(dev); 95 bool lut_diff = false; 96 97 if (gamma->data) 98 lut_diff = gamma->data->lut_diff; 99 100 mtk_gamma_set_common(gamma->regs, state, lut_diff); 101 } 102 103 void mtk_gamma_config(struct device *dev, unsigned int w, 104 unsigned int h, unsigned int vrefresh, 105 unsigned int bpc, struct cmdq_pkt *cmdq_pkt) 106 { 107 struct mtk_disp_gamma *gamma = dev_get_drvdata(dev); 108 109 mtk_ddp_write(cmdq_pkt, h << 16 | w, &gamma->cmdq_reg, gamma->regs, 110 DISP_GAMMA_SIZE); 111 if (gamma->data && gamma->data->has_dither) 112 mtk_dither_set_common(gamma->regs, &gamma->cmdq_reg, bpc, 113 DISP_GAMMA_CFG, GAMMA_DITHERING, cmdq_pkt); 114 } 115 116 void mtk_gamma_start(struct device *dev) 117 { 118 struct mtk_disp_gamma *gamma = dev_get_drvdata(dev); 119 120 writel(GAMMA_EN, gamma->regs + DISP_GAMMA_EN); 121 } 122 123 void mtk_gamma_stop(struct device *dev) 124 { 125 struct mtk_disp_gamma *gamma = dev_get_drvdata(dev); 126 127 writel_relaxed(0x0, gamma->regs + DISP_GAMMA_EN); 128 } 129 130 static int mtk_disp_gamma_bind(struct device *dev, struct device *master, 131 void *data) 132 { 133 return 0; 134 } 135 136 static void mtk_disp_gamma_unbind(struct device *dev, struct device *master, 137 void *data) 138 { 139 } 140 141 static const struct component_ops mtk_disp_gamma_component_ops = { 142 .bind = mtk_disp_gamma_bind, 143 .unbind = mtk_disp_gamma_unbind, 144 }; 145 146 static int mtk_disp_gamma_probe(struct platform_device *pdev) 147 { 148 struct device *dev = &pdev->dev; 149 struct mtk_disp_gamma *priv; 150 struct resource *res; 151 int ret; 152 153 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 154 if (!priv) 155 return -ENOMEM; 156 157 priv->clk = devm_clk_get(dev, NULL); 158 if (IS_ERR(priv->clk)) { 159 dev_err(dev, "failed to get gamma clk\n"); 160 return PTR_ERR(priv->clk); 161 } 162 163 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 164 priv->regs = devm_ioremap_resource(dev, res); 165 if (IS_ERR(priv->regs)) { 166 dev_err(dev, "failed to ioremap gamma\n"); 167 return PTR_ERR(priv->regs); 168 } 169 170 #if IS_REACHABLE(CONFIG_MTK_CMDQ) 171 ret = cmdq_dev_get_client_reg(dev, &priv->cmdq_reg, 0); 172 if (ret) 173 dev_dbg(dev, "get mediatek,gce-client-reg fail!\n"); 174 #endif 175 176 priv->data = of_device_get_match_data(dev); 177 platform_set_drvdata(pdev, priv); 178 179 ret = component_add(dev, &mtk_disp_gamma_component_ops); 180 if (ret) 181 dev_err(dev, "Failed to add component: %d\n", ret); 182 183 return ret; 184 } 185 186 static int mtk_disp_gamma_remove(struct platform_device *pdev) 187 { 188 component_del(&pdev->dev, &mtk_disp_gamma_component_ops); 189 190 return 0; 191 } 192 193 static const struct mtk_disp_gamma_data mt8173_gamma_driver_data = { 194 .has_dither = true, 195 }; 196 197 static const struct mtk_disp_gamma_data mt8183_gamma_driver_data = { 198 .lut_diff = true, 199 }; 200 201 static const struct of_device_id mtk_disp_gamma_driver_dt_match[] = { 202 { .compatible = "mediatek,mt8173-disp-gamma", 203 .data = &mt8173_gamma_driver_data}, 204 { .compatible = "mediatek,mt8183-disp-gamma", 205 .data = &mt8183_gamma_driver_data}, 206 {}, 207 }; 208 MODULE_DEVICE_TABLE(of, mtk_disp_gamma_driver_dt_match); 209 210 struct platform_driver mtk_disp_gamma_driver = { 211 .probe = mtk_disp_gamma_probe, 212 .remove = mtk_disp_gamma_remove, 213 .driver = { 214 .name = "mediatek-disp-gamma", 215 .owner = THIS_MODULE, 216 .of_match_table = mtk_disp_gamma_driver_dt_match, 217 }, 218 }; 219