]>
Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
99613311 CC |
2 | /* |
3 | * linux/drivers/devfreq/governor_passive.c | |
4 | * | |
5 | * Copyright (C) 2016 Samsung Electronics | |
6 | * Author: Chanwoo Choi <cw00.choi@samsung.com> | |
7 | * Author: MyungJoo Ham <myungjoo.ham@samsung.com> | |
99613311 CC |
8 | */ |
9 | ||
10 | #include <linux/module.h> | |
11 | #include <linux/device.h> | |
12 | #include <linux/devfreq.h> | |
13 | #include "governor.h" | |
14 | ||
15 | static int devfreq_passive_get_target_freq(struct devfreq *devfreq, | |
16 | unsigned long *freq) | |
17 | { | |
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; | |
23 | int i, count, ret = 0; | |
24 | ||
25 | /* | |
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. | |
29 | */ | |
30 | if (p_data->get_target_freq) { | |
31 | ret = p_data->get_target_freq(devfreq, freq); | |
32 | goto out; | |
33 | } | |
34 | ||
35 | /* | |
36 | * If the parent and passive devfreq device uses the OPP table, | |
37 | * get the next frequency by using the OPP table. | |
38 | */ | |
39 | ||
40 | /* | |
41 | * - parent devfreq device uses the governors except for passive. | |
42 | * - passive devfreq device uses the passive governor. | |
43 | * | |
44 | * Each devfreq has the OPP table. After deciding the new frequency | |
45 | * from the governor of parent devfreq device, the passive governor | |
46 | * need to get the index of new frequency on OPP table of parent | |
47 | * device. And then the index is used for getting the suitable | |
48 | * new frequency for passive devfreq device. | |
49 | */ | |
50 | if (!devfreq->profile || !devfreq->profile->freq_table | |
51 | || devfreq->profile->max_state <= 0) | |
52 | return -EINVAL; | |
53 | ||
54 | /* | |
55 | * The passive governor have to get the correct frequency from OPP | |
56 | * list of parent device. Because in this case, *freq is temporary | |
57 | * value which is decided by ondemand governor. | |
58 | */ | |
99613311 | 59 | opp = devfreq_recommended_opp(parent_devfreq->dev.parent, freq, 0); |
99613311 CC |
60 | if (IS_ERR(opp)) { |
61 | ret = PTR_ERR(opp); | |
62 | goto out; | |
63 | } | |
64 | ||
8a31d9d9 VK |
65 | dev_pm_opp_put(opp); |
66 | ||
99613311 CC |
67 | /* |
68 | * Get the OPP table's index of decided freqeuncy by governor | |
69 | * of parent device. | |
70 | */ | |
71 | for (i = 0; i < parent_devfreq->profile->max_state; i++) | |
72 | if (parent_devfreq->profile->freq_table[i] == *freq) | |
73 | break; | |
74 | ||
75 | if (i == parent_devfreq->profile->max_state) { | |
76 | ret = -EINVAL; | |
77 | goto out; | |
78 | } | |
79 | ||
80 | /* Get the suitable frequency by using index of parent device. */ | |
81 | if (i < devfreq->profile->max_state) { | |
82 | child_freq = devfreq->profile->freq_table[i]; | |
83 | } else { | |
84 | count = devfreq->profile->max_state; | |
85 | child_freq = devfreq->profile->freq_table[count - 1]; | |
86 | } | |
87 | ||
88 | /* Return the suitable frequency for passive device. */ | |
89 | *freq = child_freq; | |
90 | ||
91 | out: | |
92 | return ret; | |
93 | } | |
94 | ||
99613311 CC |
95 | static int devfreq_passive_notifier_call(struct notifier_block *nb, |
96 | unsigned long event, void *ptr) | |
97 | { | |
98 | struct devfreq_passive_data *data | |
99 | = container_of(nb, struct devfreq_passive_data, nb); | |
100 | struct devfreq *devfreq = (struct devfreq *)data->this; | |
101 | struct devfreq *parent = (struct devfreq *)data->parent; | |
102 | struct devfreq_freqs *freqs = (struct devfreq_freqs *)ptr; | |
103 | unsigned long freq = freqs->new; | |
b4365423 | 104 | int ret = 0; |
99613311 | 105 | |
b4365423 | 106 | mutex_lock_nested(&devfreq->lock, SINGLE_DEPTH_NESTING); |
99613311 CC |
107 | switch (event) { |
108 | case DEVFREQ_PRECHANGE: | |
109 | if (parent->previous_freq > freq) | |
b4365423 CC |
110 | ret = devfreq_update_target(devfreq, freq); |
111 | ||
99613311 CC |
112 | break; |
113 | case DEVFREQ_POSTCHANGE: | |
114 | if (parent->previous_freq < freq) | |
b4365423 | 115 | ret = devfreq_update_target(devfreq, freq); |
99613311 CC |
116 | break; |
117 | } | |
b4365423 CC |
118 | mutex_unlock(&devfreq->lock); |
119 | ||
120 | if (ret < 0) | |
121 | dev_warn(&devfreq->dev, | |
122 | "failed to update devfreq using passive governor\n"); | |
99613311 CC |
123 | |
124 | return NOTIFY_DONE; | |
125 | } | |
126 | ||
127 | static int devfreq_passive_event_handler(struct devfreq *devfreq, | |
128 | unsigned int event, void *data) | |
129 | { | |
99613311 CC |
130 | struct devfreq_passive_data *p_data |
131 | = (struct devfreq_passive_data *)devfreq->data; | |
132 | struct devfreq *parent = (struct devfreq *)p_data->parent; | |
133 | struct notifier_block *nb = &p_data->nb; | |
134 | int ret = 0; | |
135 | ||
136 | if (!parent) | |
137 | return -EPROBE_DEFER; | |
138 | ||
139 | switch (event) { | |
140 | case DEVFREQ_GOV_START: | |
141 | if (!p_data->this) | |
142 | p_data->this = devfreq; | |
143 | ||
144 | nb->notifier_call = devfreq_passive_notifier_call; | |
0ef7c7cc | 145 | ret = devfreq_register_notifier(parent, nb, |
99613311 CC |
146 | DEVFREQ_TRANSITION_NOTIFIER); |
147 | break; | |
148 | case DEVFREQ_GOV_STOP: | |
0ef7c7cc LC |
149 | WARN_ON(devfreq_unregister_notifier(parent, nb, |
150 | DEVFREQ_TRANSITION_NOTIFIER)); | |
99613311 CC |
151 | break; |
152 | default: | |
153 | break; | |
154 | } | |
155 | ||
156 | return ret; | |
157 | } | |
158 | ||
159 | static struct devfreq_governor devfreq_passive = { | |
aa7c352f | 160 | .name = DEVFREQ_GOV_PASSIVE, |
bcf23c79 | 161 | .immutable = 1, |
99613311 CC |
162 | .get_target_freq = devfreq_passive_get_target_freq, |
163 | .event_handler = devfreq_passive_event_handler, | |
164 | }; | |
165 | ||
166 | static int __init devfreq_passive_init(void) | |
167 | { | |
168 | return devfreq_add_governor(&devfreq_passive); | |
169 | } | |
170 | subsys_initcall(devfreq_passive_init); | |
171 | ||
172 | static void __exit devfreq_passive_exit(void) | |
173 | { | |
174 | int ret; | |
175 | ||
176 | ret = devfreq_remove_governor(&devfreq_passive); | |
177 | if (ret) | |
178 | pr_err("%s: failed remove governor %d\n", __func__, ret); | |
99613311 CC |
179 | } |
180 | module_exit(devfreq_passive_exit); | |
181 | ||
182 | MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>"); | |
183 | MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>"); | |
184 | MODULE_DESCRIPTION("DEVFREQ Passive governor"); | |
185 | MODULE_LICENSE("GPL v2"); |