blob: d350a435ddad553d962c589b474e97d9f15299e9 [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (c) 2014 MediaTek Inc.
4 */
5
6
7#include <linux/cpuidle.h>
8#include <linux/kernel.h>
9#include <linux/module.h>
10#include <linux/of.h>
11#include <linux/slab.h>
12#include <linux/cpu_pm.h>
13#include <linux/platform_device.h>
14#include <linux/of_platform.h>
15#include <linux/smp.h>
16
17#include <asm/cpuidle.h>
18#include <asm/suspend.h>
19#include "dt_idle_states.h"
20
21#define MTK_CPUIDLE_PREPARE (0)
22#define MTK_CPUIDLE_RESUME (1)
23
24typedef int (*mtk_pwr_fn)(int type,
25 struct cpuidle_driver *drv,
26 int index);
27
28static mtk_pwr_fn mtk_pwr_conservation;
29
30static __always_inline int __mtk_lp_enter(int index)
31{
32 return CPU_PM_CPU_IDLE_ENTER(arm_cpuidle_suspend, index);
33}
34
35static int mtk_idle_state_enter(struct cpuidle_device *dev,
36 struct cpuidle_driver *drv,
37 int idx)
38{
39 int ret;
40
41 if (mtk_pwr_conservation) {
42 ret = mtk_pwr_conservation(MTK_CPUIDLE_PREPARE, drv, idx);
43 idx = ret ? 0 : idx;
44 ret = __mtk_lp_enter(idx);
45 mtk_pwr_conservation(MTK_CPUIDLE_RESUME, drv, idx);
46 } else
47 ret = CPU_PM_CPU_IDLE_ENTER(arm_cpuidle_suspend, 0);
48
49 return ret;
50}
51
52static int mtk_regular_idle_state_enter(struct cpuidle_device *dev,
53 struct cpuidle_driver *drv,
54 int idx)
55{
56 return CPU_PM_CPU_IDLE_ENTER(arm_cpuidle_suspend, idx);
57}
58
59static struct cpuidle_driver mtk_cpuidle_driver __initdata = {
60 .name = "mtk_cpuidle",
61 .owner = THIS_MODULE,
62 .states[0] = {
63 .enter = mtk_regular_idle_state_enter,
64 .exit_latency = 1,
65 .target_residency = 1,
66 .power_usage = UINT_MAX,
67 .name = "wfi",
68 .desc = "wfi",
69 },
70 .state_count = 1
71};
72
73static const struct of_device_id mtk_idle_state_match[] __initconst = {
74 { .compatible = "mediatek,idle-state",
75 .data = mtk_idle_state_enter },
76 { .compatible = "arm,idle-state",
77 .data = mtk_regular_idle_state_enter },
78 { },
79};
80
81static int mtk_cpuidle_pm_drv_probe(struct platform_device *pdev)
82{
83 if (IS_ERR_OR_NULL(pdev->dev.platform_data))
84 goto mtk_probe_fail;
85
86 mtk_pwr_conservation = *((mtk_pwr_fn *)pdev->dev.platform_data);
87 return 0;
88
89mtk_probe_fail:
90 return -ENODATA;
91}
92
93int mtk_cpuidle_pm_drv_remove(struct platform_device *pdev)
94{
95 mtk_pwr_conservation = NULL;
96 return 0;
97}
98
99static struct platform_driver mtk_cpuidle_pm_driver = {
100 .driver = {
101 .name = "mtk_cpuidle_pm",
102 .owner = THIS_MODULE,
103 },
104 .probe = mtk_cpuidle_pm_drv_probe,
105 .remove = mtk_cpuidle_pm_drv_remove,
106};
107
108static int __init mtk_cpuidle_driver_init(void)
109{
110 int cpu, ret;
111 struct cpuidle_driver *drv;
112 struct cpuidle_device *dev;
113
114 platform_driver_register(&mtk_cpuidle_pm_driver);
115
116 for_each_possible_cpu(cpu) {
117 drv = kmemdup(&mtk_cpuidle_driver, sizeof(*drv), GFP_KERNEL);
118
119 if (!drv) {
120 ret = -ENOMEM;
121 goto out_fail;
122 }
123
124 drv->cpumask = (struct cpumask *)cpumask_of(cpu);
125
126#ifdef CONFIG_DT_IDLE_STATES
127 ret = dt_init_idle_driver(drv, mtk_idle_state_match, 1);
128
129 if (ret <= 0) {
130 ret = ret ? : -ENODEV;
131 goto out_kfree_drv;
132 }
133#endif
134
135 ret = arm_cpuidle_init(cpu);
136
137 if (ret) {
138 pr_err("CPU %d failed to init idle CPU ops\n", cpu);
139 goto out_kfree_drv;
140 }
141
142 ret = cpuidle_register(drv, NULL);
143
144 if (ret)
145 goto out_kfree_drv;
146 }
147
148 return 0;
149
150out_kfree_drv:
151 kfree(drv);
152out_fail:
153 while (--cpu >= 0) {
154 dev = per_cpu(cpuidle_devices, cpu);
155 drv = cpuidle_get_cpu_driver(dev);
156 cpuidle_unregister(drv);
157 kfree(drv);
158 }
159
160 return ret;
161}
162device_initcall_sync(mtk_cpuidle_driver_init);
163