1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
299613311SChanwoo Choi /*
399613311SChanwoo Choi * linux/drivers/devfreq/governor_passive.c
499613311SChanwoo Choi *
599613311SChanwoo Choi * Copyright (C) 2016 Samsung Electronics
699613311SChanwoo Choi * Author: Chanwoo Choi <cw00.choi@samsung.com>
799613311SChanwoo Choi * Author: MyungJoo Ham <myungjoo.ham@samsung.com>
899613311SChanwoo Choi */
999613311SChanwoo Choi
1099613311SChanwoo Choi #include <linux/module.h>
11a03dacb0SSaravana Kannan #include <linux/cpu.h>
12a03dacb0SSaravana Kannan #include <linux/cpufreq.h>
13a03dacb0SSaravana Kannan #include <linux/cpumask.h>
14a03dacb0SSaravana Kannan #include <linux/slab.h>
1599613311SChanwoo Choi #include <linux/device.h>
1699613311SChanwoo Choi #include <linux/devfreq.h>
1720e6c3ccSYicong Yang #include <linux/units.h>
1899613311SChanwoo Choi #include "governor.h"
1999613311SChanwoo Choi
2026984d9dSChanwoo Choi static struct devfreq_cpu_data *
get_parent_cpu_data(struct devfreq_passive_data * p_data,struct cpufreq_policy * policy)2126984d9dSChanwoo Choi get_parent_cpu_data(struct devfreq_passive_data *p_data,
2226984d9dSChanwoo Choi struct cpufreq_policy *policy)
2326984d9dSChanwoo Choi {
2426984d9dSChanwoo Choi struct devfreq_cpu_data *parent_cpu_data;
2526984d9dSChanwoo Choi
2626984d9dSChanwoo Choi if (!p_data || !policy)
2726984d9dSChanwoo Choi return NULL;
2826984d9dSChanwoo Choi
2926984d9dSChanwoo Choi list_for_each_entry(parent_cpu_data, &p_data->cpu_data_list, node)
3026984d9dSChanwoo Choi if (parent_cpu_data->first_cpu == cpumask_first(policy->related_cpus))
3126984d9dSChanwoo Choi return parent_cpu_data;
3226984d9dSChanwoo Choi
3326984d9dSChanwoo Choi return NULL;
3426984d9dSChanwoo Choi }
3526984d9dSChanwoo Choi
delete_parent_cpu_data(struct devfreq_passive_data * p_data)360cca7e8dSChristian 'Ansuel' Marangi static void delete_parent_cpu_data(struct devfreq_passive_data *p_data)
370cca7e8dSChristian 'Ansuel' Marangi {
380cca7e8dSChristian 'Ansuel' Marangi struct devfreq_cpu_data *parent_cpu_data, *tmp;
390cca7e8dSChristian 'Ansuel' Marangi
400cca7e8dSChristian 'Ansuel' Marangi list_for_each_entry_safe(parent_cpu_data, tmp, &p_data->cpu_data_list, node) {
410cca7e8dSChristian 'Ansuel' Marangi list_del(&parent_cpu_data->node);
420cca7e8dSChristian 'Ansuel' Marangi
430cca7e8dSChristian 'Ansuel' Marangi if (parent_cpu_data->opp_table)
440cca7e8dSChristian 'Ansuel' Marangi dev_pm_opp_put_opp_table(parent_cpu_data->opp_table);
450cca7e8dSChristian 'Ansuel' Marangi
460cca7e8dSChristian 'Ansuel' Marangi kfree(parent_cpu_data);
470cca7e8dSChristian 'Ansuel' Marangi }
480cca7e8dSChristian 'Ansuel' Marangi }
490cca7e8dSChristian 'Ansuel' Marangi
get_target_freq_by_required_opp(struct device * p_dev,struct opp_table * p_opp_table,struct opp_table * opp_table,unsigned long * freq)50a03dacb0SSaravana Kannan static unsigned long get_target_freq_by_required_opp(struct device *p_dev,
51a03dacb0SSaravana Kannan struct opp_table *p_opp_table,
52a03dacb0SSaravana Kannan struct opp_table *opp_table,
53a03dacb0SSaravana Kannan unsigned long *freq)
54a03dacb0SSaravana Kannan {
55a03dacb0SSaravana Kannan struct dev_pm_opp *opp = NULL, *p_opp = NULL;
56a03dacb0SSaravana Kannan unsigned long target_freq;
57a03dacb0SSaravana Kannan
58a03dacb0SSaravana Kannan if (!p_dev || !p_opp_table || !opp_table || !freq)
59a03dacb0SSaravana Kannan return 0;
60a03dacb0SSaravana Kannan
61a03dacb0SSaravana Kannan p_opp = devfreq_recommended_opp(p_dev, freq, 0);
62a03dacb0SSaravana Kannan if (IS_ERR(p_opp))
63a03dacb0SSaravana Kannan return 0;
64a03dacb0SSaravana Kannan
65a03dacb0SSaravana Kannan opp = dev_pm_opp_xlate_required_opp(p_opp_table, opp_table, p_opp);
66a03dacb0SSaravana Kannan dev_pm_opp_put(p_opp);
67a03dacb0SSaravana Kannan
68a03dacb0SSaravana Kannan if (IS_ERR(opp))
69a03dacb0SSaravana Kannan return 0;
70a03dacb0SSaravana Kannan
71a03dacb0SSaravana Kannan target_freq = dev_pm_opp_get_freq(opp);
72a03dacb0SSaravana Kannan dev_pm_opp_put(opp);
73a03dacb0SSaravana Kannan
74a03dacb0SSaravana Kannan return target_freq;
75a03dacb0SSaravana Kannan }
76a03dacb0SSaravana Kannan
get_target_freq_with_cpufreq(struct devfreq * devfreq,unsigned long * target_freq)77a03dacb0SSaravana Kannan static int get_target_freq_with_cpufreq(struct devfreq *devfreq,
78a03dacb0SSaravana Kannan unsigned long *target_freq)
79a03dacb0SSaravana Kannan {
80a03dacb0SSaravana Kannan struct devfreq_passive_data *p_data =
81a03dacb0SSaravana Kannan (struct devfreq_passive_data *)devfreq->data;
82a03dacb0SSaravana Kannan struct devfreq_cpu_data *parent_cpu_data;
8326984d9dSChanwoo Choi struct cpufreq_policy *policy;
84a03dacb0SSaravana Kannan unsigned long cpu, cpu_cur, cpu_min, cpu_max, cpu_percent;
85a03dacb0SSaravana Kannan unsigned long dev_min, dev_max;
86a03dacb0SSaravana Kannan unsigned long freq = 0;
8726984d9dSChanwoo Choi int ret = 0;
88a03dacb0SSaravana Kannan
89a03dacb0SSaravana Kannan for_each_online_cpu(cpu) {
9026984d9dSChanwoo Choi policy = cpufreq_cpu_get(cpu);
9126984d9dSChanwoo Choi if (!policy) {
9226984d9dSChanwoo Choi ret = -EINVAL;
93a03dacb0SSaravana Kannan continue;
9426984d9dSChanwoo Choi }
9526984d9dSChanwoo Choi
9626984d9dSChanwoo Choi parent_cpu_data = get_parent_cpu_data(p_data, policy);
9726984d9dSChanwoo Choi if (!parent_cpu_data) {
9826984d9dSChanwoo Choi cpufreq_cpu_put(policy);
9926984d9dSChanwoo Choi continue;
10026984d9dSChanwoo Choi }
101a03dacb0SSaravana Kannan
102a03dacb0SSaravana Kannan /* Get target freq via required opps */
103a03dacb0SSaravana Kannan cpu_cur = parent_cpu_data->cur_freq * HZ_PER_KHZ;
104a03dacb0SSaravana Kannan freq = get_target_freq_by_required_opp(parent_cpu_data->dev,
105a03dacb0SSaravana Kannan parent_cpu_data->opp_table,
106a03dacb0SSaravana Kannan devfreq->opp_table, &cpu_cur);
107a03dacb0SSaravana Kannan if (freq) {
108a03dacb0SSaravana Kannan *target_freq = max(freq, *target_freq);
10926984d9dSChanwoo Choi cpufreq_cpu_put(policy);
110a03dacb0SSaravana Kannan continue;
111a03dacb0SSaravana Kannan }
112a03dacb0SSaravana Kannan
113a03dacb0SSaravana Kannan /* Use interpolation if required opps is not available */
114a03dacb0SSaravana Kannan devfreq_get_freq_range(devfreq, &dev_min, &dev_max);
115a03dacb0SSaravana Kannan
116a03dacb0SSaravana Kannan cpu_min = parent_cpu_data->min_freq;
117a03dacb0SSaravana Kannan cpu_max = parent_cpu_data->max_freq;
118a03dacb0SSaravana Kannan cpu_cur = parent_cpu_data->cur_freq;
119a03dacb0SSaravana Kannan
120a03dacb0SSaravana Kannan cpu_percent = ((cpu_cur - cpu_min) * 100) / (cpu_max - cpu_min);
121a03dacb0SSaravana Kannan freq = dev_min + mult_frac(dev_max - dev_min, cpu_percent, 100);
122a03dacb0SSaravana Kannan
123a03dacb0SSaravana Kannan *target_freq = max(freq, *target_freq);
12426984d9dSChanwoo Choi cpufreq_cpu_put(policy);
125a03dacb0SSaravana Kannan }
126a03dacb0SSaravana Kannan
12726984d9dSChanwoo Choi return ret;
128a03dacb0SSaravana Kannan }
129a03dacb0SSaravana Kannan
get_target_freq_with_devfreq(struct devfreq * devfreq,unsigned long * freq)130a03dacb0SSaravana Kannan static int get_target_freq_with_devfreq(struct devfreq *devfreq,
13199613311SChanwoo Choi unsigned long *freq)
13299613311SChanwoo Choi {
13399613311SChanwoo Choi struct devfreq_passive_data *p_data
13499613311SChanwoo Choi = (struct devfreq_passive_data *)devfreq->data;
13599613311SChanwoo Choi struct devfreq *parent_devfreq = (struct devfreq *)p_data->parent;
13699613311SChanwoo Choi unsigned long child_freq = ULONG_MAX;
13786ad9a24SSaravana Kannan int i, count;
13899613311SChanwoo Choi
13905723e71SChanwoo Choi /* Get target freq via required opps */
14005723e71SChanwoo Choi child_freq = get_target_freq_by_required_opp(parent_devfreq->dev.parent,
14105723e71SChanwoo Choi parent_devfreq->opp_table,
14205723e71SChanwoo Choi devfreq->opp_table, freq);
14305723e71SChanwoo Choi if (child_freq)
14405723e71SChanwoo Choi goto out;
14599613311SChanwoo Choi
14605723e71SChanwoo Choi /* Use interpolation if required opps is not available */
147*b5d281f6SChristian Marangi for (i = 0; i < parent_devfreq->max_state; i++)
148*b5d281f6SChristian Marangi if (parent_devfreq->freq_table[i] == *freq)
14999613311SChanwoo Choi break;
15099613311SChanwoo Choi
151*b5d281f6SChristian Marangi if (i == parent_devfreq->max_state)
15286ad9a24SSaravana Kannan return -EINVAL;
15399613311SChanwoo Choi
154*b5d281f6SChristian Marangi if (i < devfreq->max_state) {
155*b5d281f6SChristian Marangi child_freq = devfreq->freq_table[i];
15699613311SChanwoo Choi } else {
157*b5d281f6SChristian Marangi count = devfreq->max_state;
158*b5d281f6SChristian Marangi child_freq = devfreq->freq_table[count - 1];
15999613311SChanwoo Choi }
16099613311SChanwoo Choi
16105723e71SChanwoo Choi out:
16299613311SChanwoo Choi *freq = child_freq;
16399613311SChanwoo Choi
16486ad9a24SSaravana Kannan return 0;
16599613311SChanwoo Choi }
16699613311SChanwoo Choi
devfreq_passive_get_target_freq(struct devfreq * devfreq,unsigned long * freq)167a03dacb0SSaravana Kannan static int devfreq_passive_get_target_freq(struct devfreq *devfreq,
168a03dacb0SSaravana Kannan unsigned long *freq)
169a03dacb0SSaravana Kannan {
170a03dacb0SSaravana Kannan struct devfreq_passive_data *p_data =
171a03dacb0SSaravana Kannan (struct devfreq_passive_data *)devfreq->data;
172a03dacb0SSaravana Kannan int ret;
173a03dacb0SSaravana Kannan
174a03dacb0SSaravana Kannan if (!p_data)
175a03dacb0SSaravana Kannan return -EINVAL;
176a03dacb0SSaravana Kannan
177a03dacb0SSaravana Kannan /*
178a03dacb0SSaravana Kannan * If the devfreq device with passive governor has the specific method
179a03dacb0SSaravana Kannan * to determine the next frequency, should use the get_target_freq()
180a03dacb0SSaravana Kannan * of struct devfreq_passive_data.
181a03dacb0SSaravana Kannan */
182a03dacb0SSaravana Kannan if (p_data->get_target_freq)
183a03dacb0SSaravana Kannan return p_data->get_target_freq(devfreq, freq);
184a03dacb0SSaravana Kannan
185a03dacb0SSaravana Kannan switch (p_data->parent_type) {
186a03dacb0SSaravana Kannan case DEVFREQ_PARENT_DEV:
187a03dacb0SSaravana Kannan ret = get_target_freq_with_devfreq(devfreq, freq);
188a03dacb0SSaravana Kannan break;
189a03dacb0SSaravana Kannan case CPUFREQ_PARENT_DEV:
190a03dacb0SSaravana Kannan ret = get_target_freq_with_cpufreq(devfreq, freq);
191a03dacb0SSaravana Kannan break;
192a03dacb0SSaravana Kannan default:
193a03dacb0SSaravana Kannan ret = -EINVAL;
194a03dacb0SSaravana Kannan dev_err(&devfreq->dev, "Invalid parent type\n");
195a03dacb0SSaravana Kannan break;
196a03dacb0SSaravana Kannan }
197a03dacb0SSaravana Kannan
198a03dacb0SSaravana Kannan return ret;
199a03dacb0SSaravana Kannan }
200a03dacb0SSaravana Kannan
cpufreq_passive_notifier_call(struct notifier_block * nb,unsigned long event,void * ptr)201a03dacb0SSaravana Kannan static int cpufreq_passive_notifier_call(struct notifier_block *nb,
202a03dacb0SSaravana Kannan unsigned long event, void *ptr)
203a03dacb0SSaravana Kannan {
204a03dacb0SSaravana Kannan struct devfreq_passive_data *p_data =
205a03dacb0SSaravana Kannan container_of(nb, struct devfreq_passive_data, nb);
206a03dacb0SSaravana Kannan struct devfreq *devfreq = (struct devfreq *)p_data->this;
207a03dacb0SSaravana Kannan struct devfreq_cpu_data *parent_cpu_data;
208a03dacb0SSaravana Kannan struct cpufreq_freqs *freqs = ptr;
209a03dacb0SSaravana Kannan unsigned int cur_freq;
210a03dacb0SSaravana Kannan int ret;
211a03dacb0SSaravana Kannan
21226984d9dSChanwoo Choi if (event != CPUFREQ_POSTCHANGE || !freqs)
213a03dacb0SSaravana Kannan return 0;
214a03dacb0SSaravana Kannan
21526984d9dSChanwoo Choi parent_cpu_data = get_parent_cpu_data(p_data, freqs->policy);
21626984d9dSChanwoo Choi if (!parent_cpu_data || parent_cpu_data->cur_freq == freqs->new)
217a03dacb0SSaravana Kannan return 0;
218a03dacb0SSaravana Kannan
219a03dacb0SSaravana Kannan cur_freq = parent_cpu_data->cur_freq;
220a03dacb0SSaravana Kannan parent_cpu_data->cur_freq = freqs->new;
221a03dacb0SSaravana Kannan
222a03dacb0SSaravana Kannan mutex_lock(&devfreq->lock);
223a03dacb0SSaravana Kannan ret = devfreq_update_target(devfreq, freqs->new);
224a03dacb0SSaravana Kannan mutex_unlock(&devfreq->lock);
225a03dacb0SSaravana Kannan if (ret) {
226a03dacb0SSaravana Kannan parent_cpu_data->cur_freq = cur_freq;
227a03dacb0SSaravana Kannan dev_err(&devfreq->dev, "failed to update the frequency.\n");
228a03dacb0SSaravana Kannan return ret;
229a03dacb0SSaravana Kannan }
230a03dacb0SSaravana Kannan
231a03dacb0SSaravana Kannan return 0;
232a03dacb0SSaravana Kannan }
233a03dacb0SSaravana Kannan
cpufreq_passive_unregister_notifier(struct devfreq * devfreq)234a03dacb0SSaravana Kannan static int cpufreq_passive_unregister_notifier(struct devfreq *devfreq)
235a03dacb0SSaravana Kannan {
236a03dacb0SSaravana Kannan struct devfreq_passive_data *p_data
237a03dacb0SSaravana Kannan = (struct devfreq_passive_data *)devfreq->data;
2380cca7e8dSChristian 'Ansuel' Marangi int ret;
239a03dacb0SSaravana Kannan
240a03dacb0SSaravana Kannan if (p_data->nb.notifier_call) {
241a03dacb0SSaravana Kannan ret = cpufreq_unregister_notifier(&p_data->nb,
242a03dacb0SSaravana Kannan CPUFREQ_TRANSITION_NOTIFIER);
243a03dacb0SSaravana Kannan if (ret < 0)
244a03dacb0SSaravana Kannan return ret;
245a03dacb0SSaravana Kannan }
246a03dacb0SSaravana Kannan
2470cca7e8dSChristian 'Ansuel' Marangi delete_parent_cpu_data(p_data);
248a03dacb0SSaravana Kannan
2490cca7e8dSChristian 'Ansuel' Marangi return 0;
250a03dacb0SSaravana Kannan }
251a03dacb0SSaravana Kannan
cpufreq_passive_register_notifier(struct devfreq * devfreq)252a03dacb0SSaravana Kannan static int cpufreq_passive_register_notifier(struct devfreq *devfreq)
253a03dacb0SSaravana Kannan {
254a03dacb0SSaravana Kannan struct devfreq_passive_data *p_data
255a03dacb0SSaravana Kannan = (struct devfreq_passive_data *)devfreq->data;
256a03dacb0SSaravana Kannan struct device *dev = devfreq->dev.parent;
257a03dacb0SSaravana Kannan struct opp_table *opp_table = NULL;
258a03dacb0SSaravana Kannan struct devfreq_cpu_data *parent_cpu_data;
259a03dacb0SSaravana Kannan struct cpufreq_policy *policy;
260a03dacb0SSaravana Kannan struct device *cpu_dev;
261a03dacb0SSaravana Kannan unsigned int cpu;
262a03dacb0SSaravana Kannan int ret;
263a03dacb0SSaravana Kannan
26426984d9dSChanwoo Choi p_data->cpu_data_list
26526984d9dSChanwoo Choi = (struct list_head)LIST_HEAD_INIT(p_data->cpu_data_list);
26626984d9dSChanwoo Choi
267a03dacb0SSaravana Kannan p_data->nb.notifier_call = cpufreq_passive_notifier_call;
268a03dacb0SSaravana Kannan ret = cpufreq_register_notifier(&p_data->nb, CPUFREQ_TRANSITION_NOTIFIER);
269a03dacb0SSaravana Kannan if (ret) {
270a03dacb0SSaravana Kannan dev_err(dev, "failed to register cpufreq notifier\n");
271a03dacb0SSaravana Kannan p_data->nb.notifier_call = NULL;
272a03dacb0SSaravana Kannan goto err;
273a03dacb0SSaravana Kannan }
274a03dacb0SSaravana Kannan
275a03dacb0SSaravana Kannan for_each_possible_cpu(cpu) {
276a03dacb0SSaravana Kannan policy = cpufreq_cpu_get(cpu);
277a03dacb0SSaravana Kannan if (!policy) {
278a03dacb0SSaravana Kannan ret = -EPROBE_DEFER;
279a03dacb0SSaravana Kannan goto err;
280a03dacb0SSaravana Kannan }
281a03dacb0SSaravana Kannan
28226984d9dSChanwoo Choi parent_cpu_data = get_parent_cpu_data(p_data, policy);
28326984d9dSChanwoo Choi if (parent_cpu_data) {
28426984d9dSChanwoo Choi cpufreq_cpu_put(policy);
28526984d9dSChanwoo Choi continue;
28626984d9dSChanwoo Choi }
28726984d9dSChanwoo Choi
288a03dacb0SSaravana Kannan parent_cpu_data = kzalloc(sizeof(*parent_cpu_data),
289a03dacb0SSaravana Kannan GFP_KERNEL);
290a03dacb0SSaravana Kannan if (!parent_cpu_data) {
291a03dacb0SSaravana Kannan ret = -ENOMEM;
292a03dacb0SSaravana Kannan goto err_put_policy;
293a03dacb0SSaravana Kannan }
294a03dacb0SSaravana Kannan
295a03dacb0SSaravana Kannan cpu_dev = get_cpu_device(cpu);
296a03dacb0SSaravana Kannan if (!cpu_dev) {
297a03dacb0SSaravana Kannan dev_err(dev, "failed to get cpu device\n");
298a03dacb0SSaravana Kannan ret = -ENODEV;
299a03dacb0SSaravana Kannan goto err_free_cpu_data;
300a03dacb0SSaravana Kannan }
301a03dacb0SSaravana Kannan
302a03dacb0SSaravana Kannan opp_table = dev_pm_opp_get_opp_table(cpu_dev);
303a03dacb0SSaravana Kannan if (IS_ERR(opp_table)) {
304a03dacb0SSaravana Kannan dev_err(dev, "failed to get opp_table of cpu%d\n", cpu);
305a03dacb0SSaravana Kannan ret = PTR_ERR(opp_table);
306a03dacb0SSaravana Kannan goto err_free_cpu_data;
307a03dacb0SSaravana Kannan }
308a03dacb0SSaravana Kannan
309a03dacb0SSaravana Kannan parent_cpu_data->dev = cpu_dev;
310a03dacb0SSaravana Kannan parent_cpu_data->opp_table = opp_table;
311a03dacb0SSaravana Kannan parent_cpu_data->first_cpu = cpumask_first(policy->related_cpus);
312a03dacb0SSaravana Kannan parent_cpu_data->cur_freq = policy->cur;
313a03dacb0SSaravana Kannan parent_cpu_data->min_freq = policy->cpuinfo.min_freq;
314a03dacb0SSaravana Kannan parent_cpu_data->max_freq = policy->cpuinfo.max_freq;
315a03dacb0SSaravana Kannan
31626984d9dSChanwoo Choi list_add_tail(&parent_cpu_data->node, &p_data->cpu_data_list);
317a03dacb0SSaravana Kannan cpufreq_cpu_put(policy);
318a03dacb0SSaravana Kannan }
319a03dacb0SSaravana Kannan
320a03dacb0SSaravana Kannan mutex_lock(&devfreq->lock);
321a03dacb0SSaravana Kannan ret = devfreq_update_target(devfreq, 0L);
322a03dacb0SSaravana Kannan mutex_unlock(&devfreq->lock);
323a03dacb0SSaravana Kannan if (ret)
324a03dacb0SSaravana Kannan dev_err(dev, "failed to update the frequency\n");
325a03dacb0SSaravana Kannan
326a03dacb0SSaravana Kannan return ret;
327a03dacb0SSaravana Kannan
328a03dacb0SSaravana Kannan err_free_cpu_data:
329a03dacb0SSaravana Kannan kfree(parent_cpu_data);
330a03dacb0SSaravana Kannan err_put_policy:
331a03dacb0SSaravana Kannan cpufreq_cpu_put(policy);
332a03dacb0SSaravana Kannan err:
333a03dacb0SSaravana Kannan
334a03dacb0SSaravana Kannan return ret;
335a03dacb0SSaravana Kannan }
336a03dacb0SSaravana Kannan
devfreq_passive_notifier_call(struct notifier_block * nb,unsigned long event,void * ptr)33799613311SChanwoo Choi static int devfreq_passive_notifier_call(struct notifier_block *nb,
33899613311SChanwoo Choi unsigned long event, void *ptr)
33999613311SChanwoo Choi {
34099613311SChanwoo Choi struct devfreq_passive_data *data
34199613311SChanwoo Choi = container_of(nb, struct devfreq_passive_data, nb);
34299613311SChanwoo Choi struct devfreq *devfreq = (struct devfreq *)data->this;
34399613311SChanwoo Choi struct devfreq *parent = (struct devfreq *)data->parent;
34499613311SChanwoo Choi struct devfreq_freqs *freqs = (struct devfreq_freqs *)ptr;
34599613311SChanwoo Choi unsigned long freq = freqs->new;
346b4365423SChanwoo Choi int ret = 0;
34799613311SChanwoo Choi
348b4365423SChanwoo Choi mutex_lock_nested(&devfreq->lock, SINGLE_DEPTH_NESTING);
34999613311SChanwoo Choi switch (event) {
35099613311SChanwoo Choi case DEVFREQ_PRECHANGE:
35199613311SChanwoo Choi if (parent->previous_freq > freq)
352b4365423SChanwoo Choi ret = devfreq_update_target(devfreq, freq);
353b4365423SChanwoo Choi
35499613311SChanwoo Choi break;
35599613311SChanwoo Choi case DEVFREQ_POSTCHANGE:
35699613311SChanwoo Choi if (parent->previous_freq < freq)
357b4365423SChanwoo Choi ret = devfreq_update_target(devfreq, freq);
35899613311SChanwoo Choi break;
35999613311SChanwoo Choi }
360b4365423SChanwoo Choi mutex_unlock(&devfreq->lock);
361b4365423SChanwoo Choi
362b4365423SChanwoo Choi if (ret < 0)
363b4365423SChanwoo Choi dev_warn(&devfreq->dev,
364b4365423SChanwoo Choi "failed to update devfreq using passive governor\n");
36599613311SChanwoo Choi
36699613311SChanwoo Choi return NOTIFY_DONE;
36799613311SChanwoo Choi }
36899613311SChanwoo Choi
devfreq_passive_unregister_notifier(struct devfreq * devfreq)369a03dacb0SSaravana Kannan static int devfreq_passive_unregister_notifier(struct devfreq *devfreq)
37099613311SChanwoo Choi {
37199613311SChanwoo Choi struct devfreq_passive_data *p_data
37299613311SChanwoo Choi = (struct devfreq_passive_data *)devfreq->data;
37399613311SChanwoo Choi struct devfreq *parent = (struct devfreq *)p_data->parent;
37499613311SChanwoo Choi struct notifier_block *nb = &p_data->nb;
375a03dacb0SSaravana Kannan
376a03dacb0SSaravana Kannan return devfreq_unregister_notifier(parent, nb, DEVFREQ_TRANSITION_NOTIFIER);
377a03dacb0SSaravana Kannan }
378a03dacb0SSaravana Kannan
devfreq_passive_register_notifier(struct devfreq * devfreq)379a03dacb0SSaravana Kannan static int devfreq_passive_register_notifier(struct devfreq *devfreq)
380a03dacb0SSaravana Kannan {
381a03dacb0SSaravana Kannan struct devfreq_passive_data *p_data
382a03dacb0SSaravana Kannan = (struct devfreq_passive_data *)devfreq->data;
383a03dacb0SSaravana Kannan struct devfreq *parent = (struct devfreq *)p_data->parent;
384a03dacb0SSaravana Kannan struct notifier_block *nb = &p_data->nb;
38599613311SChanwoo Choi
38699613311SChanwoo Choi if (!parent)
38799613311SChanwoo Choi return -EPROBE_DEFER;
38899613311SChanwoo Choi
389a03dacb0SSaravana Kannan nb->notifier_call = devfreq_passive_notifier_call;
390a03dacb0SSaravana Kannan return devfreq_register_notifier(parent, nb, DEVFREQ_TRANSITION_NOTIFIER);
391a03dacb0SSaravana Kannan }
392a03dacb0SSaravana Kannan
devfreq_passive_event_handler(struct devfreq * devfreq,unsigned int event,void * data)393a03dacb0SSaravana Kannan static int devfreq_passive_event_handler(struct devfreq *devfreq,
394a03dacb0SSaravana Kannan unsigned int event, void *data)
395a03dacb0SSaravana Kannan {
396a03dacb0SSaravana Kannan struct devfreq_passive_data *p_data
397a03dacb0SSaravana Kannan = (struct devfreq_passive_data *)devfreq->data;
39842d2607dSChanwoo Choi int ret = 0;
399a03dacb0SSaravana Kannan
400a03dacb0SSaravana Kannan if (!p_data)
401a03dacb0SSaravana Kannan return -EINVAL;
402a03dacb0SSaravana Kannan
40399613311SChanwoo Choi p_data->this = devfreq;
40499613311SChanwoo Choi
405a03dacb0SSaravana Kannan switch (event) {
406a03dacb0SSaravana Kannan case DEVFREQ_GOV_START:
407a03dacb0SSaravana Kannan if (p_data->parent_type == DEVFREQ_PARENT_DEV)
408a03dacb0SSaravana Kannan ret = devfreq_passive_register_notifier(devfreq);
409a03dacb0SSaravana Kannan else if (p_data->parent_type == CPUFREQ_PARENT_DEV)
410a03dacb0SSaravana Kannan ret = cpufreq_passive_register_notifier(devfreq);
41199613311SChanwoo Choi break;
41299613311SChanwoo Choi case DEVFREQ_GOV_STOP:
413a03dacb0SSaravana Kannan if (p_data->parent_type == DEVFREQ_PARENT_DEV)
414a03dacb0SSaravana Kannan WARN_ON(devfreq_passive_unregister_notifier(devfreq));
415a03dacb0SSaravana Kannan else if (p_data->parent_type == CPUFREQ_PARENT_DEV)
416a03dacb0SSaravana Kannan WARN_ON(cpufreq_passive_unregister_notifier(devfreq));
41799613311SChanwoo Choi break;
41899613311SChanwoo Choi default:
41999613311SChanwoo Choi break;
42099613311SChanwoo Choi }
42199613311SChanwoo Choi
42299613311SChanwoo Choi return ret;
42399613311SChanwoo Choi }
42499613311SChanwoo Choi
42599613311SChanwoo Choi static struct devfreq_governor devfreq_passive = {
426aa7c352fSChanwoo Choi .name = DEVFREQ_GOV_PASSIVE,
4270dd25a0dSChanwoo Choi .flags = DEVFREQ_GOV_FLAG_IMMUTABLE,
42899613311SChanwoo Choi .get_target_freq = devfreq_passive_get_target_freq,
42999613311SChanwoo Choi .event_handler = devfreq_passive_event_handler,
43099613311SChanwoo Choi };
43199613311SChanwoo Choi
devfreq_passive_init(void)43299613311SChanwoo Choi static int __init devfreq_passive_init(void)
43399613311SChanwoo Choi {
43499613311SChanwoo Choi return devfreq_add_governor(&devfreq_passive);
43599613311SChanwoo Choi }
43699613311SChanwoo Choi subsys_initcall(devfreq_passive_init);
43799613311SChanwoo Choi
devfreq_passive_exit(void)43899613311SChanwoo Choi static void __exit devfreq_passive_exit(void)
43999613311SChanwoo Choi {
44099613311SChanwoo Choi int ret;
44199613311SChanwoo Choi
44299613311SChanwoo Choi ret = devfreq_remove_governor(&devfreq_passive);
44399613311SChanwoo Choi if (ret)
44499613311SChanwoo Choi pr_err("%s: failed remove governor %d\n", __func__, ret);
44599613311SChanwoo Choi }
44699613311SChanwoo Choi module_exit(devfreq_passive_exit);
44799613311SChanwoo Choi
44899613311SChanwoo Choi MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
44999613311SChanwoo Choi MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
45099613311SChanwoo Choi MODULE_DESCRIPTION("DEVFREQ Passive governor");
45199613311SChanwoo Choi MODULE_LICENSE("GPL v2");
452