xref: /openbmc/linux/drivers/soc/samsung/exynos-pmu.c (revision 7f2e85840871f199057e65232ebde846192ed989)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Copyright (c) 2011-2014 Samsung Electronics Co., Ltd.
4 //		http://www.samsung.com/
5 //
6 // EXYNOS - CPU PMU(Power Management Unit) support
7 
8 #include <linux/of.h>
9 #include <linux/of_address.h>
10 #include <linux/of_device.h>
11 #include <linux/mfd/syscon.h>
12 #include <linux/platform_device.h>
13 #include <linux/delay.h>
14 
15 #include <linux/soc/samsung/exynos-regs-pmu.h>
16 #include <linux/soc/samsung/exynos-pmu.h>
17 
18 #include "exynos-pmu.h"
19 
20 struct exynos_pmu_context {
21 	struct device *dev;
22 	const struct exynos_pmu_data *pmu_data;
23 };
24 
25 void __iomem *pmu_base_addr;
26 static struct exynos_pmu_context *pmu_context;
27 
28 void pmu_raw_writel(u32 val, u32 offset)
29 {
30 	writel_relaxed(val, pmu_base_addr + offset);
31 }
32 
33 u32 pmu_raw_readl(u32 offset)
34 {
35 	return readl_relaxed(pmu_base_addr + offset);
36 }
37 
38 void exynos_sys_powerdown_conf(enum sys_powerdown mode)
39 {
40 	unsigned int i;
41 	const struct exynos_pmu_data *pmu_data;
42 
43 	if (!pmu_context || !pmu_context->pmu_data)
44 		return;
45 
46 	pmu_data = pmu_context->pmu_data;
47 
48 	if (pmu_data->powerdown_conf)
49 		pmu_data->powerdown_conf(mode);
50 
51 	if (pmu_data->pmu_config) {
52 		for (i = 0; (pmu_data->pmu_config[i].offset != PMU_TABLE_END); i++)
53 			pmu_raw_writel(pmu_data->pmu_config[i].val[mode],
54 					pmu_data->pmu_config[i].offset);
55 	}
56 
57 	if (pmu_data->powerdown_conf_extra)
58 		pmu_data->powerdown_conf_extra(mode);
59 }
60 
61 /*
62  * Split the data between ARM architectures because it is relatively big
63  * and useless on other arch.
64  */
65 #ifdef CONFIG_EXYNOS_PMU_ARM_DRIVERS
66 #define exynos_pmu_data_arm_ptr(data)	(&data)
67 #else
68 #define exynos_pmu_data_arm_ptr(data)	NULL
69 #endif
70 
71 /*
72  * PMU platform driver and devicetree bindings.
73  */
74 static const struct of_device_id exynos_pmu_of_device_ids[] = {
75 	{
76 		.compatible = "samsung,exynos3250-pmu",
77 		.data = exynos_pmu_data_arm_ptr(exynos3250_pmu_data),
78 	}, {
79 		.compatible = "samsung,exynos4210-pmu",
80 		.data = exynos_pmu_data_arm_ptr(exynos4210_pmu_data),
81 	}, {
82 		.compatible = "samsung,exynos4412-pmu",
83 		.data = exynos_pmu_data_arm_ptr(exynos4412_pmu_data),
84 	}, {
85 		.compatible = "samsung,exynos5250-pmu",
86 		.data = exynos_pmu_data_arm_ptr(exynos5250_pmu_data),
87 	}, {
88 		.compatible = "samsung,exynos5420-pmu",
89 		.data = exynos_pmu_data_arm_ptr(exynos5420_pmu_data),
90 	}, {
91 		.compatible = "samsung,exynos5433-pmu",
92 	},
93 	{ /*sentinel*/ },
94 };
95 
96 struct regmap *exynos_get_pmu_regmap(void)
97 {
98 	struct device_node *np = of_find_matching_node(NULL,
99 						      exynos_pmu_of_device_ids);
100 	if (np)
101 		return syscon_node_to_regmap(np);
102 	return ERR_PTR(-ENODEV);
103 }
104 EXPORT_SYMBOL_GPL(exynos_get_pmu_regmap);
105 
106 static int exynos_pmu_probe(struct platform_device *pdev)
107 {
108 	struct device *dev = &pdev->dev;
109 	struct resource *res;
110 
111 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
112 	pmu_base_addr = devm_ioremap_resource(dev, res);
113 	if (IS_ERR(pmu_base_addr))
114 		return PTR_ERR(pmu_base_addr);
115 
116 	pmu_context = devm_kzalloc(&pdev->dev,
117 			sizeof(struct exynos_pmu_context),
118 			GFP_KERNEL);
119 	if (!pmu_context)
120 		return -ENOMEM;
121 	pmu_context->dev = dev;
122 	pmu_context->pmu_data = of_device_get_match_data(dev);
123 
124 	if (pmu_context->pmu_data && pmu_context->pmu_data->pmu_init)
125 		pmu_context->pmu_data->pmu_init();
126 
127 	platform_set_drvdata(pdev, pmu_context);
128 
129 	dev_dbg(dev, "Exynos PMU Driver probe done\n");
130 	return 0;
131 }
132 
133 static struct platform_driver exynos_pmu_driver = {
134 	.driver  = {
135 		.name   = "exynos-pmu",
136 		.of_match_table = exynos_pmu_of_device_ids,
137 	},
138 	.probe = exynos_pmu_probe,
139 };
140 
141 static int __init exynos_pmu_init(void)
142 {
143 	return platform_driver_register(&exynos_pmu_driver);
144 
145 }
146 postcore_initcall(exynos_pmu_init);
147