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 
18 #define DISP_AAL_EN				0x0000
19 #define AAL_EN						BIT(0)
20 #define DISP_AAL_SIZE				0x0030
21 
22 
23 struct mtk_disp_aal_data {
24 	bool has_gamma;
25 };
26 
27 /**
28  * struct mtk_disp_aal - DISP_AAL driver structure
29  * @ddp_comp - structure containing type enum and hardware resources
30  * @crtc - associated crtc to report irq events to
31  */
32 struct mtk_disp_aal {
33 	struct clk *clk;
34 	void __iomem *regs;
35 	struct cmdq_client_reg cmdq_reg;
36 	const struct mtk_disp_aal_data *data;
37 };
38 
39 int mtk_aal_clk_enable(struct device *dev)
40 {
41 	struct mtk_disp_aal *aal = dev_get_drvdata(dev);
42 
43 	return clk_prepare_enable(aal->clk);
44 }
45 
46 void mtk_aal_clk_disable(struct device *dev)
47 {
48 	struct mtk_disp_aal *aal = dev_get_drvdata(dev);
49 
50 	clk_disable_unprepare(aal->clk);
51 }
52 
53 void mtk_aal_config(struct device *dev, unsigned int w,
54 			   unsigned int h, unsigned int vrefresh,
55 			   unsigned int bpc, struct cmdq_pkt *cmdq_pkt)
56 {
57 	struct mtk_disp_aal *aal = dev_get_drvdata(dev);
58 
59 	mtk_ddp_write(cmdq_pkt, w << 16 | h, &aal->cmdq_reg, aal->regs, DISP_AAL_SIZE);
60 }
61 
62 void mtk_aal_gamma_set(struct device *dev, struct drm_crtc_state *state)
63 {
64 	struct mtk_disp_aal *aal = dev_get_drvdata(dev);
65 
66 	if (aal->data && aal->data->has_gamma)
67 		mtk_gamma_set_common(aal->regs, state);
68 }
69 
70 void mtk_aal_start(struct device *dev)
71 {
72 	struct mtk_disp_aal *aal = dev_get_drvdata(dev);
73 
74 	writel(AAL_EN, aal->regs + DISP_AAL_EN);
75 }
76 
77 void mtk_aal_stop(struct device *dev)
78 {
79 	struct mtk_disp_aal *aal = dev_get_drvdata(dev);
80 
81 	writel_relaxed(0x0, aal->regs + DISP_AAL_EN);
82 }
83 
84 static int mtk_disp_aal_bind(struct device *dev, struct device *master,
85 			       void *data)
86 {
87 	return 0;
88 }
89 
90 static void mtk_disp_aal_unbind(struct device *dev, struct device *master,
91 				  void *data)
92 {
93 }
94 
95 static const struct component_ops mtk_disp_aal_component_ops = {
96 	.bind	= mtk_disp_aal_bind,
97 	.unbind = mtk_disp_aal_unbind,
98 };
99 
100 static int mtk_disp_aal_probe(struct platform_device *pdev)
101 {
102 	struct device *dev = &pdev->dev;
103 	struct mtk_disp_aal *priv;
104 	struct resource *res;
105 	int ret;
106 
107 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
108 	if (!priv)
109 		return -ENOMEM;
110 
111 	priv->clk = devm_clk_get(dev, NULL);
112 	if (IS_ERR(priv->clk)) {
113 		dev_err(dev, "failed to get aal clk\n");
114 		return PTR_ERR(priv->clk);
115 	}
116 
117 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
118 	priv->regs = devm_ioremap_resource(dev, res);
119 	if (IS_ERR(priv->regs)) {
120 		dev_err(dev, "failed to ioremap aal\n");
121 		return PTR_ERR(priv->regs);
122 	}
123 
124 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
125 	ret = cmdq_dev_get_client_reg(dev, &priv->cmdq_reg, 0);
126 	if (ret)
127 		dev_dbg(dev, "get mediatek,gce-client-reg fail!\n");
128 #endif
129 
130 	priv->data = of_device_get_match_data(dev);
131 	platform_set_drvdata(pdev, priv);
132 
133 	ret = component_add(dev, &mtk_disp_aal_component_ops);
134 	if (ret)
135 		dev_err(dev, "Failed to add component: %d\n", ret);
136 
137 	return ret;
138 }
139 
140 static int mtk_disp_aal_remove(struct platform_device *pdev)
141 {
142 	component_del(&pdev->dev, &mtk_disp_aal_component_ops);
143 
144 	return 0;
145 }
146 
147 static const struct mtk_disp_aal_data mt8173_aal_driver_data = {
148 	.has_gamma = true,
149 };
150 
151 static const struct of_device_id mtk_disp_aal_driver_dt_match[] = {
152 	{ .compatible = "mediatek,mt8173-disp-aal",
153 	  .data = &mt8173_aal_driver_data},
154 	{},
155 };
156 MODULE_DEVICE_TABLE(of, mtk_disp_aal_driver_dt_match);
157 
158 struct platform_driver mtk_disp_aal_driver = {
159 	.probe		= mtk_disp_aal_probe,
160 	.remove		= mtk_disp_aal_remove,
161 	.driver		= {
162 		.name	= "mediatek-disp-aal",
163 		.owner	= THIS_MODULE,
164 		.of_match_table = mtk_disp_aal_driver_dt_match,
165 	},
166 };
167