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