1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/drivers/devfreq/governor_passive.c
5 * Copyright (C) 2016 Samsung Electronics
6 * Author: Chanwoo Choi <cw00.choi@samsung.com>
7 * Author: MyungJoo Ham <myungjoo.ham@samsung.com>
10 #include <linux/module.h>
11 #include <linux/device.h>
12 #include <linux/devfreq.h>
15 static int devfreq_passive_get_target_freq(struct devfreq *devfreq,
18 struct devfreq_passive_data *p_data
19 = (struct devfreq_passive_data *)devfreq->data;
20 struct devfreq *parent_devfreq = (struct devfreq *)p_data->parent;
21 unsigned long child_freq = ULONG_MAX;
22 struct dev_pm_opp *opp, *p_opp;
26 * If the devfreq device with passive governor has the specific method
27 * to determine the next frequency, should use the get_target_freq()
28 * of struct devfreq_passive_data.
30 if (p_data->get_target_freq)
31 return p_data->get_target_freq(devfreq, freq);
34 * If the parent and passive devfreq device uses the OPP table,
35 * get the next frequency by using the OPP table.
39 * - parent devfreq device uses the governors except for passive.
40 * - passive devfreq device uses the passive governor.
42 * Each devfreq has the OPP table. After deciding the new frequency
43 * from the governor of parent devfreq device, the passive governor
44 * need to get the index of new frequency on OPP table of parent
45 * device. And then the index is used for getting the suitable
46 * new frequency for passive devfreq device.
48 if (!devfreq->profile || !devfreq->profile->freq_table
49 || devfreq->profile->max_state <= 0)
53 * The passive governor have to get the correct frequency from OPP
54 * list of parent device. Because in this case, *freq is temporary
55 * value which is decided by ondemand governor.
57 if (devfreq->opp_table && parent_devfreq->opp_table) {
58 p_opp = devfreq_recommended_opp(parent_devfreq->dev.parent,
61 return PTR_ERR(p_opp);
63 opp = dev_pm_opp_xlate_required_opp(parent_devfreq->opp_table,
64 devfreq->opp_table, p_opp);
65 dev_pm_opp_put(p_opp);
70 *freq = dev_pm_opp_get_freq(opp);
78 * Get the OPP table's index of decided frequency by governor
81 for (i = 0; i < parent_devfreq->profile->max_state; i++)
82 if (parent_devfreq->profile->freq_table[i] == *freq)
85 if (i == parent_devfreq->profile->max_state)
88 /* Get the suitable frequency by using index of parent device. */
89 if (i < devfreq->profile->max_state) {
90 child_freq = devfreq->profile->freq_table[i];
92 count = devfreq->profile->max_state;
93 child_freq = devfreq->profile->freq_table[count - 1];
96 /* Return the suitable frequency for passive device. */
102 static int devfreq_passive_notifier_call(struct notifier_block *nb,
103 unsigned long event, void *ptr)
105 struct devfreq_passive_data *data
106 = container_of(nb, struct devfreq_passive_data, nb);
107 struct devfreq *devfreq = (struct devfreq *)data->this;
108 struct devfreq *parent = (struct devfreq *)data->parent;
109 struct devfreq_freqs *freqs = (struct devfreq_freqs *)ptr;
110 unsigned long freq = freqs->new;
113 mutex_lock_nested(&devfreq->lock, SINGLE_DEPTH_NESTING);
115 case DEVFREQ_PRECHANGE:
116 if (parent->previous_freq > freq)
117 ret = devfreq_update_target(devfreq, freq);
120 case DEVFREQ_POSTCHANGE:
121 if (parent->previous_freq < freq)
122 ret = devfreq_update_target(devfreq, freq);
125 mutex_unlock(&devfreq->lock);
128 dev_warn(&devfreq->dev,
129 "failed to update devfreq using passive governor\n");
134 static int devfreq_passive_event_handler(struct devfreq *devfreq,
135 unsigned int event, void *data)
137 struct devfreq_passive_data *p_data
138 = (struct devfreq_passive_data *)devfreq->data;
139 struct devfreq *parent = (struct devfreq *)p_data->parent;
140 struct notifier_block *nb = &p_data->nb;
144 return -EPROBE_DEFER;
147 case DEVFREQ_GOV_START:
149 p_data->this = devfreq;
151 nb->notifier_call = devfreq_passive_notifier_call;
152 ret = devfreq_register_notifier(parent, nb,
153 DEVFREQ_TRANSITION_NOTIFIER);
155 case DEVFREQ_GOV_STOP:
156 WARN_ON(devfreq_unregister_notifier(parent, nb,
157 DEVFREQ_TRANSITION_NOTIFIER));
166 static struct devfreq_governor devfreq_passive = {
167 .name = DEVFREQ_GOV_PASSIVE,
168 .flags = DEVFREQ_GOV_FLAG_IMMUTABLE,
169 .get_target_freq = devfreq_passive_get_target_freq,
170 .event_handler = devfreq_passive_event_handler,
173 static int __init devfreq_passive_init(void)
175 return devfreq_add_governor(&devfreq_passive);
177 subsys_initcall(devfreq_passive_init);
179 static void __exit devfreq_passive_exit(void)
183 ret = devfreq_remove_governor(&devfreq_passive);
185 pr_err("%s: failed remove governor %d\n", __func__, ret);
187 module_exit(devfreq_passive_exit);
189 MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
190 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
191 MODULE_DESCRIPTION("DEVFREQ Passive governor");
192 MODULE_LICENSE("GPL v2");