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