]>
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; | |
86ad9a24 SK |
22 | struct dev_pm_opp *opp, *p_opp; |
23 | int i, count; | |
99613311 CC |
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 | */ | |
86ad9a24 SK |
30 | if (p_data->get_target_freq) |
31 | return p_data->get_target_freq(devfreq, freq); | |
99613311 CC |
32 | |
33 | /* | |
34 | * If the parent and passive devfreq device uses the OPP table, | |
35 | * get the next frequency by using the OPP table. | |
36 | */ | |
37 | ||
38 | /* | |
39 | * - parent devfreq device uses the governors except for passive. | |
40 | * - passive devfreq device uses the passive governor. | |
41 | * | |
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. | |
47 | */ | |
48 | if (!devfreq->profile || !devfreq->profile->freq_table | |
49 | || devfreq->profile->max_state <= 0) | |
50 | return -EINVAL; | |
51 | ||
52 | /* | |
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. | |
56 | */ | |
86ad9a24 SK |
57 | if (devfreq->opp_table && parent_devfreq->opp_table) { |
58 | p_opp = devfreq_recommended_opp(parent_devfreq->dev.parent, | |
59 | freq, 0); | |
60 | if (IS_ERR(p_opp)) | |
61 | return PTR_ERR(p_opp); | |
62 | ||
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); | |
66 | ||
67 | if (IS_ERR(opp)) | |
68 | return PTR_ERR(opp); | |
99613311 | 69 | |
86ad9a24 SK |
70 | *freq = dev_pm_opp_get_freq(opp); |
71 | dev_pm_opp_put(opp); | |
72 | ||
73 | return 0; | |
74 | } | |
8a31d9d9 | 75 | |
99613311 | 76 | /* |
86ad9a24 | 77 | * Get the OPP table's index of decided frequency by governor |
99613311 CC |
78 | * of parent device. |
79 | */ | |
80 | for (i = 0; i < parent_devfreq->profile->max_state; i++) | |
81 | if (parent_devfreq->profile->freq_table[i] == *freq) | |
82 | break; | |
83 | ||
86ad9a24 SK |
84 | if (i == parent_devfreq->profile->max_state) |
85 | return -EINVAL; | |
99613311 CC |
86 | |
87 | /* Get the suitable frequency by using index of parent device. */ | |
88 | if (i < devfreq->profile->max_state) { | |
89 | child_freq = devfreq->profile->freq_table[i]; | |
90 | } else { | |
91 | count = devfreq->profile->max_state; | |
92 | child_freq = devfreq->profile->freq_table[count - 1]; | |
93 | } | |
94 | ||
95 | /* Return the suitable frequency for passive device. */ | |
96 | *freq = child_freq; | |
97 | ||
86ad9a24 | 98 | return 0; |
99613311 CC |
99 | } |
100 | ||
99613311 CC |
101 | static int devfreq_passive_notifier_call(struct notifier_block *nb, |
102 | unsigned long event, void *ptr) | |
103 | { | |
104 | struct devfreq_passive_data *data | |
105 | = container_of(nb, struct devfreq_passive_data, nb); | |
106 | struct devfreq *devfreq = (struct devfreq *)data->this; | |
107 | struct devfreq *parent = (struct devfreq *)data->parent; | |
108 | struct devfreq_freqs *freqs = (struct devfreq_freqs *)ptr; | |
109 | unsigned long freq = freqs->new; | |
b4365423 | 110 | int ret = 0; |
99613311 | 111 | |
b4365423 | 112 | mutex_lock_nested(&devfreq->lock, SINGLE_DEPTH_NESTING); |
99613311 CC |
113 | switch (event) { |
114 | case DEVFREQ_PRECHANGE: | |
115 | if (parent->previous_freq > freq) | |
b4365423 CC |
116 | ret = devfreq_update_target(devfreq, freq); |
117 | ||
99613311 CC |
118 | break; |
119 | case DEVFREQ_POSTCHANGE: | |
120 | if (parent->previous_freq < freq) | |
b4365423 | 121 | ret = devfreq_update_target(devfreq, freq); |
99613311 CC |
122 | break; |
123 | } | |
b4365423 CC |
124 | mutex_unlock(&devfreq->lock); |
125 | ||
126 | if (ret < 0) | |
127 | dev_warn(&devfreq->dev, | |
128 | "failed to update devfreq using passive governor\n"); | |
99613311 CC |
129 | |
130 | return NOTIFY_DONE; | |
131 | } | |
132 | ||
133 | static int devfreq_passive_event_handler(struct devfreq *devfreq, | |
134 | unsigned int event, void *data) | |
135 | { | |
99613311 CC |
136 | struct devfreq_passive_data *p_data |
137 | = (struct devfreq_passive_data *)devfreq->data; | |
138 | struct devfreq *parent = (struct devfreq *)p_data->parent; | |
139 | struct notifier_block *nb = &p_data->nb; | |
140 | int ret = 0; | |
141 | ||
142 | if (!parent) | |
143 | return -EPROBE_DEFER; | |
144 | ||
145 | switch (event) { | |
146 | case DEVFREQ_GOV_START: | |
147 | if (!p_data->this) | |
148 | p_data->this = devfreq; | |
149 | ||
150 | nb->notifier_call = devfreq_passive_notifier_call; | |
0ef7c7cc | 151 | ret = devfreq_register_notifier(parent, nb, |
99613311 CC |
152 | DEVFREQ_TRANSITION_NOTIFIER); |
153 | break; | |
154 | case DEVFREQ_GOV_STOP: | |
0ef7c7cc LC |
155 | WARN_ON(devfreq_unregister_notifier(parent, nb, |
156 | DEVFREQ_TRANSITION_NOTIFIER)); | |
99613311 CC |
157 | break; |
158 | default: | |
159 | break; | |
160 | } | |
161 | ||
162 | return ret; | |
163 | } | |
164 | ||
165 | static struct devfreq_governor devfreq_passive = { | |
aa7c352f | 166 | .name = DEVFREQ_GOV_PASSIVE, |
0dd25a0d | 167 | .flags = DEVFREQ_GOV_FLAG_IMMUTABLE, |
99613311 CC |
168 | .get_target_freq = devfreq_passive_get_target_freq, |
169 | .event_handler = devfreq_passive_event_handler, | |
170 | }; | |
171 | ||
172 | static int __init devfreq_passive_init(void) | |
173 | { | |
174 | return devfreq_add_governor(&devfreq_passive); | |
175 | } | |
176 | subsys_initcall(devfreq_passive_init); | |
177 | ||
178 | static void __exit devfreq_passive_exit(void) | |
179 | { | |
180 | int ret; | |
181 | ||
182 | ret = devfreq_remove_governor(&devfreq_passive); | |
183 | if (ret) | |
184 | pr_err("%s: failed remove governor %d\n", __func__, ret); | |
99613311 CC |
185 | } |
186 | module_exit(devfreq_passive_exit); | |
187 | ||
188 | MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>"); | |
189 | MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>"); | |
190 | MODULE_DESCRIPTION("DEVFREQ Passive governor"); | |
191 | MODULE_LICENSE("GPL v2"); |