xref: /openbmc/linux/drivers/soc/mediatek/mtk-mmsys.c (revision 249592bf)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2014 MediaTek Inc.
4  * Author: James Liao <jamesjj.liao@mediatek.com>
5  */
6 
7 #include <linux/device.h>
8 #include <linux/io.h>
9 #include <linux/of_device.h>
10 #include <linux/platform_device.h>
11 #include <linux/soc/mediatek/mtk-mmsys.h>
12 
13 #include "mtk-mmsys.h"
14 #include "mt8167-mmsys.h"
15 #include "mt8183-mmsys.h"
16 
17 static const struct mtk_mmsys_driver_data mt2701_mmsys_driver_data = {
18 	.clk_driver = "clk-mt2701-mm",
19 	.routes = mmsys_default_routing_table,
20 	.num_routes = ARRAY_SIZE(mmsys_default_routing_table),
21 };
22 
23 static const struct mtk_mmsys_driver_data mt2712_mmsys_driver_data = {
24 	.clk_driver = "clk-mt2712-mm",
25 	.routes = mmsys_default_routing_table,
26 	.num_routes = ARRAY_SIZE(mmsys_default_routing_table),
27 };
28 
29 static const struct mtk_mmsys_driver_data mt6779_mmsys_driver_data = {
30 	.clk_driver = "clk-mt6779-mm",
31 };
32 
33 static const struct mtk_mmsys_driver_data mt6797_mmsys_driver_data = {
34 	.clk_driver = "clk-mt6797-mm",
35 };
36 
37 static const struct mtk_mmsys_driver_data mt8167_mmsys_driver_data = {
38 	.clk_driver = "clk-mt8167-mm",
39 	.routes = mt8167_mmsys_routing_table,
40 	.num_routes = ARRAY_SIZE(mt8167_mmsys_routing_table),
41 };
42 
43 static const struct mtk_mmsys_driver_data mt8173_mmsys_driver_data = {
44 	.clk_driver = "clk-mt8173-mm",
45 	.routes = mmsys_default_routing_table,
46 	.num_routes = ARRAY_SIZE(mmsys_default_routing_table),
47 };
48 
49 static const struct mtk_mmsys_driver_data mt8183_mmsys_driver_data = {
50 	.clk_driver = "clk-mt8183-mm",
51 	.routes = mmsys_mt8183_routing_table,
52 	.num_routes = ARRAY_SIZE(mmsys_mt8183_routing_table),
53 };
54 
55 struct mtk_mmsys {
56 	void __iomem *regs;
57 	const struct mtk_mmsys_driver_data *data;
58 };
59 
60 void mtk_mmsys_ddp_connect(struct device *dev,
61 			   enum mtk_ddp_comp_id cur,
62 			   enum mtk_ddp_comp_id next)
63 {
64 	struct mtk_mmsys *mmsys = dev_get_drvdata(dev);
65 	const struct mtk_mmsys_routes *routes = mmsys->data->routes;
66 	u32 reg;
67 	int i;
68 
69 	for (i = 0; i < mmsys->data->num_routes; i++)
70 		if (cur == routes[i].from_comp && next == routes[i].to_comp) {
71 			reg = readl_relaxed(mmsys->regs + routes[i].addr) | routes[i].val;
72 			writel_relaxed(reg, mmsys->regs + routes[i].addr);
73 		}
74 }
75 EXPORT_SYMBOL_GPL(mtk_mmsys_ddp_connect);
76 
77 void mtk_mmsys_ddp_disconnect(struct device *dev,
78 			      enum mtk_ddp_comp_id cur,
79 			      enum mtk_ddp_comp_id next)
80 {
81 	struct mtk_mmsys *mmsys = dev_get_drvdata(dev);
82 	const struct mtk_mmsys_routes *routes = mmsys->data->routes;
83 	u32 reg;
84 	int i;
85 
86 	for (i = 0; i < mmsys->data->num_routes; i++)
87 		if (cur == routes[i].from_comp && next == routes[i].to_comp) {
88 			reg = readl_relaxed(mmsys->regs + routes[i].addr) & ~routes[i].val;
89 			writel_relaxed(reg, mmsys->regs + routes[i].addr);
90 		}
91 }
92 EXPORT_SYMBOL_GPL(mtk_mmsys_ddp_disconnect);
93 
94 static int mtk_mmsys_probe(struct platform_device *pdev)
95 {
96 	struct device *dev = &pdev->dev;
97 	struct platform_device *clks;
98 	struct platform_device *drm;
99 	struct mtk_mmsys *mmsys;
100 	int ret;
101 
102 	mmsys = devm_kzalloc(dev, sizeof(*mmsys), GFP_KERNEL);
103 	if (!mmsys)
104 		return -ENOMEM;
105 
106 	mmsys->regs = devm_platform_ioremap_resource(pdev, 0);
107 	if (IS_ERR(mmsys->regs)) {
108 		ret = PTR_ERR(mmsys->regs);
109 		dev_err(dev, "Failed to ioremap mmsys registers: %d\n", ret);
110 		return ret;
111 	}
112 
113 	mmsys->data = of_device_get_match_data(&pdev->dev);
114 	platform_set_drvdata(pdev, mmsys);
115 
116 	clks = platform_device_register_data(&pdev->dev, mmsys->data->clk_driver,
117 					     PLATFORM_DEVID_AUTO, NULL, 0);
118 	if (IS_ERR(clks))
119 		return PTR_ERR(clks);
120 
121 	drm = platform_device_register_data(&pdev->dev, "mediatek-drm",
122 					    PLATFORM_DEVID_AUTO, NULL, 0);
123 	if (IS_ERR(drm)) {
124 		platform_device_unregister(clks);
125 		return PTR_ERR(drm);
126 	}
127 
128 	return 0;
129 }
130 
131 static const struct of_device_id of_match_mtk_mmsys[] = {
132 	{
133 		.compatible = "mediatek,mt2701-mmsys",
134 		.data = &mt2701_mmsys_driver_data,
135 	},
136 	{
137 		.compatible = "mediatek,mt2712-mmsys",
138 		.data = &mt2712_mmsys_driver_data,
139 	},
140 	{
141 		.compatible = "mediatek,mt6779-mmsys",
142 		.data = &mt6779_mmsys_driver_data,
143 	},
144 	{
145 		.compatible = "mediatek,mt6797-mmsys",
146 		.data = &mt6797_mmsys_driver_data,
147 	},
148 	{
149 		.compatible = "mediatek,mt8167-mmsys",
150 		.data = &mt8167_mmsys_driver_data,
151 	},
152 	{
153 		.compatible = "mediatek,mt8173-mmsys",
154 		.data = &mt8173_mmsys_driver_data,
155 	},
156 	{
157 		.compatible = "mediatek,mt8183-mmsys",
158 		.data = &mt8183_mmsys_driver_data,
159 	},
160 	{ }
161 };
162 
163 static struct platform_driver mtk_mmsys_drv = {
164 	.driver = {
165 		.name = "mtk-mmsys",
166 		.of_match_table = of_match_mtk_mmsys,
167 	},
168 	.probe = mtk_mmsys_probe,
169 };
170 
171 builtin_platform_driver(mtk_mmsys_drv);
172