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