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