2 * linux/drivers/devfreq/governor_passive.c
4 * Copyright (C) 2016 Samsung Electronics
5 * Author: Chanwoo Choi <cw00.choi@samsung.com>
6 * Author: MyungJoo Ham <myungjoo.ham@samsung.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/module.h>
14 #include <linux/device.h>
15 #include <linux/devfreq.h>
18 static int devfreq_passive_get_target_freq(struct devfreq
*devfreq
,
21 struct devfreq_passive_data
*p_data
22 = (struct devfreq_passive_data
*)devfreq
->data
;
23 struct devfreq
*parent_devfreq
= (struct devfreq
*)p_data
->parent
;
24 unsigned long child_freq
= ULONG_MAX
;
25 struct dev_pm_opp
*opp
;
26 int i
, count
, ret
= 0;
29 * If the devfreq device with passive governor has the specific method
30 * to determine the next frequency, should use the get_target_freq()
31 * of struct devfreq_passive_data.
33 if (p_data
->get_target_freq
) {
34 ret
= p_data
->get_target_freq(devfreq
, freq
);
39 * If the parent and passive devfreq device uses the OPP table,
40 * get the next frequency by using the OPP table.
44 * - parent devfreq device uses the governors except for passive.
45 * - passive devfreq device uses the passive governor.
47 * Each devfreq has the OPP table. After deciding the new frequency
48 * from the governor of parent devfreq device, the passive governor
49 * need to get the index of new frequency on OPP table of parent
50 * device. And then the index is used for getting the suitable
51 * new frequency for passive devfreq device.
53 if (!devfreq
->profile
|| !devfreq
->profile
->freq_table
54 || devfreq
->profile
->max_state
<= 0)
58 * The passive governor have to get the correct frequency from OPP
59 * list of parent device. Because in this case, *freq is temporary
60 * value which is decided by ondemand governor.
63 opp
= devfreq_recommended_opp(parent_devfreq
->dev
.parent
, freq
, 0);
71 * Get the OPP table's index of decided freqeuncy by governor
74 for (i
= 0; i
< parent_devfreq
->profile
->max_state
; i
++)
75 if (parent_devfreq
->profile
->freq_table
[i
] == *freq
)
78 if (i
== parent_devfreq
->profile
->max_state
) {
83 /* Get the suitable frequency by using index of parent device. */
84 if (i
< devfreq
->profile
->max_state
) {
85 child_freq
= devfreq
->profile
->freq_table
[i
];
87 count
= devfreq
->profile
->max_state
;
88 child_freq
= devfreq
->profile
->freq_table
[count
- 1];
91 /* Return the suitable frequency for passive device. */
98 static int update_devfreq_passive(struct devfreq
*devfreq
, unsigned long freq
)
102 if (!devfreq
->governor
)
105 mutex_lock_nested(&devfreq
->lock
, SINGLE_DEPTH_NESTING
);
107 ret
= devfreq
->governor
->get_target_freq(devfreq
, &freq
);
111 ret
= devfreq
->profile
->target(devfreq
->dev
.parent
, &freq
, 0);
115 devfreq
->previous_freq
= freq
;
118 mutex_unlock(&devfreq
->lock
);
123 static int devfreq_passive_notifier_call(struct notifier_block
*nb
,
124 unsigned long event
, void *ptr
)
126 struct devfreq_passive_data
*data
127 = container_of(nb
, struct devfreq_passive_data
, nb
);
128 struct devfreq
*devfreq
= (struct devfreq
*)data
->this;
129 struct devfreq
*parent
= (struct devfreq
*)data
->parent
;
130 struct devfreq_freqs
*freqs
= (struct devfreq_freqs
*)ptr
;
131 unsigned long freq
= freqs
->new;
134 case DEVFREQ_PRECHANGE
:
135 if (parent
->previous_freq
> freq
)
136 update_devfreq_passive(devfreq
, freq
);
138 case DEVFREQ_POSTCHANGE
:
139 if (parent
->previous_freq
< freq
)
140 update_devfreq_passive(devfreq
, freq
);
147 static int devfreq_passive_event_handler(struct devfreq
*devfreq
,
148 unsigned int event
, void *data
)
150 struct device
*dev
= devfreq
->dev
.parent
;
151 struct devfreq_passive_data
*p_data
152 = (struct devfreq_passive_data
*)devfreq
->data
;
153 struct devfreq
*parent
= (struct devfreq
*)p_data
->parent
;
154 struct notifier_block
*nb
= &p_data
->nb
;
158 return -EPROBE_DEFER
;
161 case DEVFREQ_GOV_START
:
163 p_data
->this = devfreq
;
165 nb
->notifier_call
= devfreq_passive_notifier_call
;
166 ret
= devm_devfreq_register_notifier(dev
, parent
, nb
,
167 DEVFREQ_TRANSITION_NOTIFIER
);
169 case DEVFREQ_GOV_STOP
:
170 devm_devfreq_unregister_notifier(dev
, parent
, nb
,
171 DEVFREQ_TRANSITION_NOTIFIER
);
180 static struct devfreq_governor devfreq_passive
= {
182 .get_target_freq
= devfreq_passive_get_target_freq
,
183 .event_handler
= devfreq_passive_event_handler
,
186 static int __init
devfreq_passive_init(void)
188 return devfreq_add_governor(&devfreq_passive
);
190 subsys_initcall(devfreq_passive_init
);
192 static void __exit
devfreq_passive_exit(void)
196 ret
= devfreq_remove_governor(&devfreq_passive
);
198 pr_err("%s: failed remove governor %d\n", __func__
, ret
);
200 module_exit(devfreq_passive_exit
);
202 MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
203 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
204 MODULE_DESCRIPTION("DEVFREQ Passive governor");
205 MODULE_LICENSE("GPL v2");