1 /*
2  * Copyright (C) 2016 Linaro.
3  * Viresh Kumar <viresh.kumar@linaro.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  */
9 
10 #include <linux/err.h>
11 #include <linux/of.h>
12 #include <linux/of_device.h>
13 #include <linux/platform_device.h>
14 
15 #include "cpufreq-dt.h"
16 
17 /*
18  * Machines for which the cpufreq device is *always* created, mostly used for
19  * platforms using "operating-points" (V1) property.
20  */
21 static const struct of_device_id whitelist[] __initconst = {
22 	{ .compatible = "allwinner,sun4i-a10", },
23 	{ .compatible = "allwinner,sun5i-a10s", },
24 	{ .compatible = "allwinner,sun5i-a13", },
25 	{ .compatible = "allwinner,sun5i-r8", },
26 	{ .compatible = "allwinner,sun6i-a31", },
27 	{ .compatible = "allwinner,sun6i-a31s", },
28 	{ .compatible = "allwinner,sun7i-a20", },
29 	{ .compatible = "allwinner,sun8i-a23", },
30 	{ .compatible = "allwinner,sun8i-a83t", },
31 	{ .compatible = "allwinner,sun8i-h3", },
32 
33 	{ .compatible = "apm,xgene-shadowcat", },
34 
35 	{ .compatible = "arm,integrator-ap", },
36 	{ .compatible = "arm,integrator-cp", },
37 
38 	{ .compatible = "hisilicon,hi3660", },
39 
40 	{ .compatible = "fsl,imx27", },
41 	{ .compatible = "fsl,imx51", },
42 	{ .compatible = "fsl,imx53", },
43 	{ .compatible = "fsl,imx7d", },
44 
45 	{ .compatible = "marvell,berlin", },
46 	{ .compatible = "marvell,pxa250", },
47 	{ .compatible = "marvell,pxa270", },
48 
49 	{ .compatible = "samsung,exynos3250", },
50 	{ .compatible = "samsung,exynos4210", },
51 	{ .compatible = "samsung,exynos5250", },
52 #ifndef CONFIG_BL_SWITCHER
53 	{ .compatible = "samsung,exynos5800", },
54 #endif
55 
56 	{ .compatible = "renesas,emev2", },
57 	{ .compatible = "renesas,r7s72100", },
58 	{ .compatible = "renesas,r8a73a4", },
59 	{ .compatible = "renesas,r8a7740", },
60 	{ .compatible = "renesas,r8a7743", },
61 	{ .compatible = "renesas,r8a7745", },
62 	{ .compatible = "renesas,r8a7778", },
63 	{ .compatible = "renesas,r8a7779", },
64 	{ .compatible = "renesas,r8a7790", },
65 	{ .compatible = "renesas,r8a7791", },
66 	{ .compatible = "renesas,r8a7792", },
67 	{ .compatible = "renesas,r8a7793", },
68 	{ .compatible = "renesas,r8a7794", },
69 	{ .compatible = "renesas,r8a7795", },
70 	{ .compatible = "renesas,r8a7796", },
71 	{ .compatible = "renesas,sh73a0", },
72 
73 	{ .compatible = "rockchip,rk2928", },
74 	{ .compatible = "rockchip,rk3036", },
75 	{ .compatible = "rockchip,rk3066a", },
76 	{ .compatible = "rockchip,rk3066b", },
77 	{ .compatible = "rockchip,rk3188", },
78 	{ .compatible = "rockchip,rk3228", },
79 	{ .compatible = "rockchip,rk3288", },
80 	{ .compatible = "rockchip,rk3328", },
81 	{ .compatible = "rockchip,rk3366", },
82 	{ .compatible = "rockchip,rk3368", },
83 	{ .compatible = "rockchip,rk3399", },
84 
85 	{ .compatible = "st-ericsson,u8500", },
86 	{ .compatible = "st-ericsson,u8540", },
87 	{ .compatible = "st-ericsson,u9500", },
88 	{ .compatible = "st-ericsson,u9540", },
89 
90 	{ .compatible = "ti,omap2", },
91 	{ .compatible = "ti,omap3", },
92 	{ .compatible = "ti,omap4", },
93 	{ .compatible = "ti,omap5", },
94 
95 	{ .compatible = "xlnx,zynq-7000", },
96 	{ .compatible = "xlnx,zynqmp", },
97 
98 	{ }
99 };
100 
101 /*
102  * Machines for which the cpufreq device is *not* created, mostly used for
103  * platforms using "operating-points-v2" property.
104  */
105 static const struct of_device_id blacklist[] __initconst = {
106 	{ .compatible = "calxeda,highbank", },
107 	{ .compatible = "calxeda,ecx-2000", },
108 
109 	{ .compatible = "marvell,armadaxp", },
110 
111 	{ .compatible = "mediatek,mt2701", },
112 	{ .compatible = "mediatek,mt2712", },
113 	{ .compatible = "mediatek,mt7622", },
114 	{ .compatible = "mediatek,mt7623", },
115 	{ .compatible = "mediatek,mt817x", },
116 	{ .compatible = "mediatek,mt8173", },
117 	{ .compatible = "mediatek,mt8176", },
118 
119 	{ .compatible = "nvidia,tegra124", },
120 
121 	{ .compatible = "st,stih407", },
122 	{ .compatible = "st,stih410", },
123 
124 	{ .compatible = "sigma,tango4", },
125 
126 	{ .compatible = "ti,am33xx", },
127 	{ .compatible = "ti,am43", },
128 	{ .compatible = "ti,dra7", },
129 
130 	{ }
131 };
132 
133 static bool __init cpu0_node_has_opp_v2_prop(void)
134 {
135 	struct device_node *np = of_cpu_device_node_get(0);
136 	bool ret = false;
137 
138 	if (of_get_property(np, "operating-points-v2", NULL))
139 		ret = true;
140 
141 	of_node_put(np);
142 	return ret;
143 }
144 
145 static int __init cpufreq_dt_platdev_init(void)
146 {
147 	struct device_node *np = of_find_node_by_path("/");
148 	const struct of_device_id *match;
149 	const void *data = NULL;
150 
151 	if (!np)
152 		return -ENODEV;
153 
154 	match = of_match_node(whitelist, np);
155 	if (match) {
156 		data = match->data;
157 		goto create_pdev;
158 	}
159 
160 	if (cpu0_node_has_opp_v2_prop() && !of_match_node(blacklist, np))
161 		goto create_pdev;
162 
163 	of_node_put(np);
164 	return -ENODEV;
165 
166 create_pdev:
167 	of_node_put(np);
168 	return PTR_ERR_OR_ZERO(platform_device_register_data(NULL, "cpufreq-dt",
169 			       -1, data,
170 			       sizeof(struct cpufreq_dt_platform_data)));
171 }
172 device_initcall(cpufreq_dt_platdev_init);
173