xref: /openbmc/linux/drivers/soc/samsung/exynos-pmu.c (revision 25879d7b)
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/core.h>
12 #include <linux/mfd/syscon.h>
13 #include <linux/platform_device.h>
14 #include <linux/delay.h>
15 
16 #include <linux/soc/samsung/exynos-regs-pmu.h>
17 #include <linux/soc/samsung/exynos-pmu.h>
18 
19 #include "exynos-pmu.h"
20 
21 struct exynos_pmu_context {
22 	struct device *dev;
23 	const struct exynos_pmu_data *pmu_data;
24 };
25 
26 void __iomem *pmu_base_addr;
27 static struct exynos_pmu_context *pmu_context;
28 
29 void pmu_raw_writel(u32 val, u32 offset)
30 {
31 	writel_relaxed(val, pmu_base_addr + offset);
32 }
33 
34 u32 pmu_raw_readl(u32 offset)
35 {
36 	return readl_relaxed(pmu_base_addr + offset);
37 }
38 
39 void exynos_sys_powerdown_conf(enum sys_powerdown mode)
40 {
41 	unsigned int i;
42 	const struct exynos_pmu_data *pmu_data;
43 
44 	if (!pmu_context || !pmu_context->pmu_data)
45 		return;
46 
47 	pmu_data = pmu_context->pmu_data;
48 
49 	if (pmu_data->powerdown_conf)
50 		pmu_data->powerdown_conf(mode);
51 
52 	if (pmu_data->pmu_config) {
53 		for (i = 0; (pmu_data->pmu_config[i].offset != PMU_TABLE_END); i++)
54 			pmu_raw_writel(pmu_data->pmu_config[i].val[mode],
55 					pmu_data->pmu_config[i].offset);
56 	}
57 
58 	if (pmu_data->powerdown_conf_extra)
59 		pmu_data->powerdown_conf_extra(mode);
60 }
61 
62 /*
63  * Split the data between ARM architectures because it is relatively big
64  * and useless on other arch.
65  */
66 #ifdef CONFIG_EXYNOS_PMU_ARM_DRIVERS
67 #define exynos_pmu_data_arm_ptr(data)	(&data)
68 #else
69 #define exynos_pmu_data_arm_ptr(data)	NULL
70 #endif
71 
72 /*
73  * PMU platform driver and devicetree bindings.
74  */
75 static const struct of_device_id exynos_pmu_of_device_ids[] = {
76 	{
77 		.compatible = "samsung,exynos3250-pmu",
78 		.data = exynos_pmu_data_arm_ptr(exynos3250_pmu_data),
79 	}, {
80 		.compatible = "samsung,exynos4210-pmu",
81 		.data = exynos_pmu_data_arm_ptr(exynos4210_pmu_data),
82 	}, {
83 		.compatible = "samsung,exynos4412-pmu",
84 		.data = exynos_pmu_data_arm_ptr(exynos4412_pmu_data),
85 	}, {
86 		.compatible = "samsung,exynos5250-pmu",
87 		.data = exynos_pmu_data_arm_ptr(exynos5250_pmu_data),
88 	}, {
89 		.compatible = "samsung,exynos5410-pmu",
90 	}, {
91 		.compatible = "samsung,exynos5420-pmu",
92 		.data = exynos_pmu_data_arm_ptr(exynos5420_pmu_data),
93 	}, {
94 		.compatible = "samsung,exynos5433-pmu",
95 	}, {
96 		.compatible = "samsung,exynos7-pmu",
97 	}, {
98 		.compatible = "samsung,exynos850-pmu",
99 	},
100 	{ /*sentinel*/ },
101 };
102 
103 static const struct mfd_cell exynos_pmu_devs[] = {
104 	{ .name = "exynos-clkout", },
105 };
106 
107 struct regmap *exynos_get_pmu_regmap(void)
108 {
109 	struct device_node *np = of_find_matching_node(NULL,
110 						      exynos_pmu_of_device_ids);
111 	if (np)
112 		return syscon_node_to_regmap(np);
113 	return ERR_PTR(-ENODEV);
114 }
115 EXPORT_SYMBOL_GPL(exynos_get_pmu_regmap);
116 
117 static int exynos_pmu_probe(struct platform_device *pdev)
118 {
119 	struct device *dev = &pdev->dev;
120 	int ret;
121 
122 	pmu_base_addr = devm_platform_ioremap_resource(pdev, 0);
123 	if (IS_ERR(pmu_base_addr))
124 		return PTR_ERR(pmu_base_addr);
125 
126 	pmu_context = devm_kzalloc(&pdev->dev,
127 			sizeof(struct exynos_pmu_context),
128 			GFP_KERNEL);
129 	if (!pmu_context)
130 		return -ENOMEM;
131 	pmu_context->dev = dev;
132 	pmu_context->pmu_data = of_device_get_match_data(dev);
133 
134 	if (pmu_context->pmu_data && pmu_context->pmu_data->pmu_init)
135 		pmu_context->pmu_data->pmu_init();
136 
137 	platform_set_drvdata(pdev, pmu_context);
138 
139 	ret = devm_mfd_add_devices(dev, PLATFORM_DEVID_NONE, exynos_pmu_devs,
140 				   ARRAY_SIZE(exynos_pmu_devs), NULL, 0, NULL);
141 	if (ret)
142 		return ret;
143 
144 	if (devm_of_platform_populate(dev))
145 		dev_err(dev, "Error populating children, reboot and poweroff might not work properly\n");
146 
147 	dev_dbg(dev, "Exynos PMU Driver probe done\n");
148 	return 0;
149 }
150 
151 static struct platform_driver exynos_pmu_driver = {
152 	.driver  = {
153 		.name   = "exynos-pmu",
154 		.of_match_table = exynos_pmu_of_device_ids,
155 	},
156 	.probe = exynos_pmu_probe,
157 };
158 
159 static int __init exynos_pmu_init(void)
160 {
161 	return platform_driver_register(&exynos_pmu_driver);
162 
163 }
164 postcore_initcall(exynos_pmu_init);
165