blob: 62c262fc2178d4cbf26619a047ccf52652fba2ea [file] [log] [blame]
Chanwoo Choi99613312016-03-22 13:44:03 +09001/*
2 * linux/drivers/devfreq/governor_passive.c
3 *
4 * Copyright (C) 2016 Samsung Electronics
5 * Author: Chanwoo Choi <cw00.choi@samsung.com>
6 * Author: MyungJoo Ham <myungjoo.ham@samsung.com>
7 *
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.
11 */
12
13#include <linux/module.h>
14#include <linux/device.h>
15#include <linux/devfreq.h>
16#include "governor.h"
17
18static int devfreq_passive_get_target_freq(struct devfreq *devfreq,
19 unsigned long *freq)
20{
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;
27
28 /*
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.
32 */
33 if (p_data->get_target_freq) {
34 ret = p_data->get_target_freq(devfreq, freq);
35 goto out;
36 }
37
38 /*
39 * If the parent and passive devfreq device uses the OPP table,
40 * get the next frequency by using the OPP table.
41 */
42
43 /*
44 * - parent devfreq device uses the governors except for passive.
45 * - passive devfreq device uses the passive governor.
46 *
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.
52 */
53 if (!devfreq->profile || !devfreq->profile->freq_table
54 || devfreq->profile->max_state <= 0)
55 return -EINVAL;
56
57 /*
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.
61 */
62 rcu_read_lock();
63 opp = devfreq_recommended_opp(parent_devfreq->dev.parent, freq, 0);
64 rcu_read_unlock();
65 if (IS_ERR(opp)) {
66 ret = PTR_ERR(opp);
67 goto out;
68 }
69
70 /*
71 * Get the OPP table's index of decided freqeuncy by governor
72 * of parent device.
73 */
74 for (i = 0; i < parent_devfreq->profile->max_state; i++)
75 if (parent_devfreq->profile->freq_table[i] == *freq)
76 break;
77
78 if (i == parent_devfreq->profile->max_state) {
79 ret = -EINVAL;
80 goto out;
81 }
82
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];
86 } else {
87 count = devfreq->profile->max_state;
88 child_freq = devfreq->profile->freq_table[count - 1];
89 }
90
91 /* Return the suitable frequency for passive device. */
92 *freq = child_freq;
93
94out:
95 return ret;
96}
97
98static int update_devfreq_passive(struct devfreq *devfreq, unsigned long freq)
99{
100 int ret;
101
102 if (!devfreq->governor)
103 return -EINVAL;
104
105 mutex_lock_nested(&devfreq->lock, SINGLE_DEPTH_NESTING);
106
107 ret = devfreq->governor->get_target_freq(devfreq, &freq);
108 if (ret < 0)
109 goto out;
110
111 ret = devfreq->profile->target(devfreq->dev.parent, &freq, 0);
112 if (ret < 0)
113 goto out;
114
Chanwoo Choife8f92c2017-01-31 15:38:17 +0900115 if (devfreq->profile->freq_table
116 && (devfreq_update_status(devfreq, freq)))
117 dev_err(&devfreq->dev,
118 "Couldn't update frequency transition information.\n");
119
Chanwoo Choi99613312016-03-22 13:44:03 +0900120 devfreq->previous_freq = freq;
121
122out:
123 mutex_unlock(&devfreq->lock);
124
125 return 0;
126}
127
128static int devfreq_passive_notifier_call(struct notifier_block *nb,
129 unsigned long event, void *ptr)
130{
131 struct devfreq_passive_data *data
132 = container_of(nb, struct devfreq_passive_data, nb);
133 struct devfreq *devfreq = (struct devfreq *)data->this;
134 struct devfreq *parent = (struct devfreq *)data->parent;
135 struct devfreq_freqs *freqs = (struct devfreq_freqs *)ptr;
136 unsigned long freq = freqs->new;
137
138 switch (event) {
139 case DEVFREQ_PRECHANGE:
140 if (parent->previous_freq > freq)
141 update_devfreq_passive(devfreq, freq);
142 break;
143 case DEVFREQ_POSTCHANGE:
144 if (parent->previous_freq < freq)
145 update_devfreq_passive(devfreq, freq);
146 break;
147 }
148
149 return NOTIFY_DONE;
150}
151
152static int devfreq_passive_event_handler(struct devfreq *devfreq,
153 unsigned int event, void *data)
154{
Chanwoo Choi99613312016-03-22 13:44:03 +0900155 struct devfreq_passive_data *p_data
156 = (struct devfreq_passive_data *)devfreq->data;
157 struct devfreq *parent = (struct devfreq *)p_data->parent;
158 struct notifier_block *nb = &p_data->nb;
159 int ret = 0;
160
161 if (!parent)
162 return -EPROBE_DEFER;
163
164 switch (event) {
165 case DEVFREQ_GOV_START:
166 if (!p_data->this)
167 p_data->this = devfreq;
168
169 nb->notifier_call = devfreq_passive_notifier_call;
Leonard Crestezd1e9aa32019-08-08 19:54:08 +0300170 ret = devfreq_register_notifier(parent, nb,
Chanwoo Choi99613312016-03-22 13:44:03 +0900171 DEVFREQ_TRANSITION_NOTIFIER);
172 break;
173 case DEVFREQ_GOV_STOP:
Leonard Crestezd1e9aa32019-08-08 19:54:08 +0300174 WARN_ON(devfreq_unregister_notifier(parent, nb,
175 DEVFREQ_TRANSITION_NOTIFIER));
Chanwoo Choi99613312016-03-22 13:44:03 +0900176 break;
177 default:
178 break;
179 }
180
181 return ret;
182}
183
184static struct devfreq_governor devfreq_passive = {
185 .name = "passive",
Chanwoo Choi2294b772017-01-31 15:38:16 +0900186 .immutable = 1,
Chanwoo Choi99613312016-03-22 13:44:03 +0900187 .get_target_freq = devfreq_passive_get_target_freq,
188 .event_handler = devfreq_passive_event_handler,
189};
190
191static int __init devfreq_passive_init(void)
192{
193 return devfreq_add_governor(&devfreq_passive);
194}
195subsys_initcall(devfreq_passive_init);
196
197static void __exit devfreq_passive_exit(void)
198{
199 int ret;
200
201 ret = devfreq_remove_governor(&devfreq_passive);
202 if (ret)
203 pr_err("%s: failed remove governor %d\n", __func__, ret);
Chanwoo Choi99613312016-03-22 13:44:03 +0900204}
205module_exit(devfreq_passive_exit);
206
207MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
208MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
209MODULE_DESCRIPTION("DEVFREQ Passive governor");
210MODULE_LICENSE("GPL v2");