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