xref: /linux/drivers/gpu/drm/mediatek/mtk_disp_color.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
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_crtc.h"
14 #include "mtk_ddp_comp.h"
15 #include "mtk_disp_drv.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 		return dev_err_probe(dev, PTR_ERR(priv->clk),
109 				     "failed to get color clk\n");
110 
111 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
112 	priv->regs = devm_ioremap_resource(dev, res);
113 	if (IS_ERR(priv->regs))
114 		return dev_err_probe(dev, PTR_ERR(priv->regs),
115 				     "failed to ioremap color\n");
116 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
117 	ret = cmdq_dev_get_client_reg(dev, &priv->cmdq_reg, 0);
118 	if (ret)
119 		dev_dbg(dev, "get mediatek,gce-client-reg fail!\n");
120 #endif
121 
122 	priv->data = of_device_get_match_data(dev);
123 	platform_set_drvdata(pdev, priv);
124 
125 	ret = component_add(dev, &mtk_disp_color_component_ops);
126 	if (ret)
127 		return dev_err_probe(dev, ret, "Failed to add component\n");
128 
129 	return 0;
130 }
131 
mtk_disp_color_remove(struct platform_device * pdev)132 static void mtk_disp_color_remove(struct platform_device *pdev)
133 {
134 	component_del(&pdev->dev, &mtk_disp_color_component_ops);
135 }
136 
137 static const struct mtk_disp_color_data mt2701_color_driver_data = {
138 	.color_offset = DISP_COLOR_START_MT2701,
139 };
140 
141 static const struct mtk_disp_color_data mt8167_color_driver_data = {
142 	.color_offset = DISP_COLOR_START_MT8167,
143 };
144 
145 static const struct mtk_disp_color_data mt8173_color_driver_data = {
146 	.color_offset = DISP_COLOR_START_MT8173,
147 };
148 
149 static const struct of_device_id mtk_disp_color_driver_dt_match[] = {
150 	{ .compatible = "mediatek,mt2701-disp-color",
151 	  .data = &mt2701_color_driver_data},
152 	{ .compatible = "mediatek,mt8167-disp-color",
153 	  .data = &mt8167_color_driver_data},
154 	{ .compatible = "mediatek,mt8173-disp-color",
155 	  .data = &mt8173_color_driver_data},
156 	{},
157 };
158 MODULE_DEVICE_TABLE(of, mtk_disp_color_driver_dt_match);
159 
160 struct platform_driver mtk_disp_color_driver = {
161 	.probe		= mtk_disp_color_probe,
162 	.remove_new	= mtk_disp_color_remove,
163 	.driver		= {
164 		.name	= "mediatek-disp-color",
165 		.of_match_table = mtk_disp_color_driver_dt_match,
166 	},
167 };
168