blob: 11eb9f55b5d085156ef0eda3195d40ec8319d07e [file] [log] [blame]
Abhijeet Dharmapurikar7e37e6e2012-08-23 18:58:44 -07001/* Copyright (c) 2012, The Linux Foundation. All rights reserved.
Praveen Chidambaram5c8adf22012-02-23 18:44:37 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
13
14#include <linux/kernel.h>
15#include <linux/init.h>
16#include <linux/module.h>
17#include <linux/mutex.h>
18#include <linux/kobject.h>
19#include <linux/cpufreq.h>
20#include <linux/platform_device.h>
21#include <mach/msm_dcvs.h>
22
23struct msm_gov {
24 int cpu;
25 unsigned int cur_freq;
26 unsigned int min_freq;
27 unsigned int max_freq;
28 struct msm_dcvs_freq gov_notifier;
29 struct cpufreq_policy *policy;
30};
31
32static DEFINE_PER_CPU_SHARED_ALIGNED(struct mutex, gov_mutex);
33static DEFINE_PER_CPU_SHARED_ALIGNED(struct msm_gov, msm_gov_info);
34static char core_name[NR_CPUS][10];
35
36static void msm_gov_check_limits(struct cpufreq_policy *policy)
37{
38 struct msm_gov *gov = &per_cpu(msm_gov_info, policy->cpu);
39
40 if (policy->max < gov->cur_freq)
41 __cpufreq_driver_target(policy, policy->max,
42 CPUFREQ_RELATION_H);
43 else if (policy->min > gov->min_freq)
44 __cpufreq_driver_target(policy, policy->min,
45 CPUFREQ_RELATION_L);
46 else
47 __cpufreq_driver_target(policy, gov->cur_freq,
48 CPUFREQ_RELATION_L);
49
50 gov->cur_freq = policy->cur;
51 gov->min_freq = policy->min;
52 gov->max_freq = policy->max;
53}
54
55static int msm_dcvs_freq_set(struct msm_dcvs_freq *self,
56 unsigned int freq)
57{
58 int ret = -EINVAL;
59 struct msm_gov *gov =
60 container_of(self, struct msm_gov, gov_notifier);
61
62 mutex_lock(&per_cpu(gov_mutex, gov->cpu));
63
64 if (freq < gov->min_freq)
65 freq = gov->min_freq;
66 if (freq > gov->max_freq)
67 freq = gov->max_freq;
68
Praveen Chidambaram5c8adf22012-02-23 18:44:37 -070069 mutex_unlock(&per_cpu(gov_mutex, gov->cpu));
70
Abhijeet Dharmapurikar83347e02012-08-26 20:33:43 -070071 ret = cpufreq_driver_target(gov->policy, freq, CPUFREQ_RELATION_L);
72
73 if (!ret) {
74 gov->cur_freq = cpufreq_quick_get(gov->cpu);
75 if (freq != gov->cur_freq)
76 pr_err("cpu %d freq %u gov->cur_freq %u didn't match",
77 gov->cpu, freq, gov->cur_freq);
78 }
79 ret = gov->cur_freq;
Praveen Chidambaram5c8adf22012-02-23 18:44:37 -070080
81 return ret;
82}
83
84static unsigned int msm_dcvs_freq_get(struct msm_dcvs_freq *self)
85{
86 struct msm_gov *gov =
87 container_of(self, struct msm_gov, gov_notifier);
88
Abhijeet Dharmapurikar83347e02012-08-26 20:33:43 -070089 /*
90 * the rw_sem in cpufreq is always held when this is called.
91 * The policy->cur won't be updated in this case - so it is safe to
92 * access policy->cur
93 */
Praveen Chidambaram5c8adf22012-02-23 18:44:37 -070094 return gov->cur_freq;
95}
96
97static int cpufreq_governor_msm(struct cpufreq_policy *policy,
98 unsigned int event)
99{
100 unsigned int cpu = policy->cpu;
101 int ret = 0;
102 int handle = 0;
103 struct msm_gov *gov = &per_cpu(msm_gov_info, policy->cpu);
104 struct msm_dcvs_freq *dcvs_notifier =
105 &(per_cpu(msm_gov_info, cpu).gov_notifier);
106
107 switch (event) {
108 case CPUFREQ_GOV_START:
109 if (!cpu_online(cpu))
110 return -EINVAL;
111 BUG_ON(!policy->cur);
112 mutex_lock(&per_cpu(gov_mutex, cpu));
113 per_cpu(msm_gov_info, cpu).cpu = cpu;
114 gov->policy = policy;
115 dcvs_notifier->core_name = core_name[cpu];
116 dcvs_notifier->set_frequency = msm_dcvs_freq_set;
117 dcvs_notifier->get_frequency = msm_dcvs_freq_get;
118 handle = msm_dcvs_freq_sink_register(dcvs_notifier);
119 BUG_ON(handle < 0);
120 msm_gov_check_limits(policy);
121 mutex_unlock(&per_cpu(gov_mutex, cpu));
122 break;
123
124 case CPUFREQ_GOV_STOP:
Praveen Chidambaram5c8adf22012-02-23 18:44:37 -0700125 msm_dcvs_freq_sink_unregister(dcvs_notifier);
Praveen Chidambaram5c8adf22012-02-23 18:44:37 -0700126 break;
127
128 case CPUFREQ_GOV_LIMITS:
129 mutex_lock(&per_cpu(gov_mutex, cpu));
130 msm_gov_check_limits(policy);
131 mutex_unlock(&per_cpu(gov_mutex, cpu));
132 break;
133 };
134
135 return ret;
136}
137
138struct cpufreq_governor cpufreq_gov_msm = {
139 .name = "msm-dcvs",
140 .governor = cpufreq_governor_msm,
141 .owner = THIS_MODULE,
142};
143
144static int __devinit msm_gov_probe(struct platform_device *pdev)
145{
146 int ret = 0;
147 int cpu;
Praveen Chidambaram5c8adf22012-02-23 18:44:37 -0700148 struct msm_dcvs_core_info *core = NULL;
Abhijeet Dharmapurikarb6c05772012-08-26 18:27:53 -0700149 int sensor = 0;
Praveen Chidambaram5c8adf22012-02-23 18:44:37 -0700150
151 core = pdev->dev.platform_data;
152
153 for_each_possible_cpu(cpu) {
154 mutex_init(&per_cpu(gov_mutex, cpu));
155 snprintf(core_name[cpu], 10, "cpu%d", cpu);
Abhijeet Dharmapurikarb6c05772012-08-26 18:27:53 -0700156 if (cpu < core->num_cores)
157 sensor = core->sensors[cpu];
158 ret = msm_dcvs_register_core(core_name[cpu], core, sensor);
Praveen Chidambaram5c8adf22012-02-23 18:44:37 -0700159 if (ret)
160 pr_err("Unable to register core for %d\n", cpu);
161 }
162
163 return cpufreq_register_governor(&cpufreq_gov_msm);
164}
165
166static int __devexit msm_gov_remove(struct platform_device *pdev)
167{
168 platform_set_drvdata(pdev, NULL);
169 return 0;
170}
171
172static struct platform_driver msm_gov_driver = {
173 .probe = msm_gov_probe,
174 .remove = __devexit_p(msm_gov_remove),
175 .driver = {
176 .name = "msm_dcvs_gov",
177 .owner = THIS_MODULE,
178 },
179};
180
181static int __init cpufreq_gov_msm_init(void)
182{
183 return platform_driver_register(&msm_gov_driver);
184}
185late_initcall(cpufreq_gov_msm_init);