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 
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  * @ddp_comp: structure containing type enum and hardware resources
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 	return 0;
138 }
139 
140 static const struct mtk_disp_color_data mt2701_color_driver_data = {
141 	.color_offset = DISP_COLOR_START_MT2701,
142 };
143 
144 static const struct mtk_disp_color_data mt8167_color_driver_data = {
145 	.color_offset = DISP_COLOR_START_MT8167,
146 };
147 
148 static const struct mtk_disp_color_data mt8173_color_driver_data = {
149 	.color_offset = DISP_COLOR_START_MT8173,
150 };
151 
152 static const struct of_device_id mtk_disp_color_driver_dt_match[] = {
153 	{ .compatible = "mediatek,mt2701-disp-color",
154 	  .data = &mt2701_color_driver_data},
155 	{ .compatible = "mediatek,mt8167-disp-color",
156 	  .data = &mt8167_color_driver_data},
157 	{ .compatible = "mediatek,mt8173-disp-color",
158 	  .data = &mt8173_color_driver_data},
159 	{},
160 };
161 MODULE_DEVICE_TABLE(of, mtk_disp_color_driver_dt_match);
162 
163 struct platform_driver mtk_disp_color_driver = {
164 	.probe		= mtk_disp_color_probe,
165 	.remove		= mtk_disp_color_remove,
166 	.driver		= {
167 		.name	= "mediatek-disp-color",
168 		.owner	= THIS_MODULE,
169 		.of_match_table = mtk_disp_color_driver_dt_match,
170 	},
171 };
172