blob: fc09324a03e03fd8d6ed841f80cd0e019415f59f [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Chanwoo Choi99613312016-03-22 13:44:03 +09002/*
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>
Chanwoo Choi99613312016-03-22 13:44:03 +09008 */
9
10#include <linux/module.h>
11#include <linux/device.h>
12#include <linux/devfreq.h>
13#include "governor.h"
14
15static 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;
Saravana Kannan86ad9a22021-02-04 16:14:24 +080022 struct dev_pm_opp *opp, *p_opp;
23 int i, count;
Chanwoo Choi99613312016-03-22 13:44:03 +090024
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 */
Saravana Kannan86ad9a22021-02-04 16:14:24 +080030 if (p_data->get_target_freq)
31 return p_data->get_target_freq(devfreq, freq);
Chanwoo Choi99613312016-03-22 13:44:03 +090032
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 */
Saravana Kannan86ad9a22021-02-04 16:14:24 +080057 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))
Chanwoo Choi8c37d012021-06-17 15:05:43 +090068 goto no_required_opp;
Saravana Kannan86ad9a22021-02-04 16:14:24 +080069
70 *freq = dev_pm_opp_get_freq(opp);
71 dev_pm_opp_put(opp);
72
73 return 0;
Chanwoo Choi99613312016-03-22 13:44:03 +090074 }
75
Chanwoo Choi8c37d012021-06-17 15:05:43 +090076no_required_opp:
Chanwoo Choi99613312016-03-22 13:44:03 +090077 /*
Saravana Kannan86ad9a22021-02-04 16:14:24 +080078 * Get the OPP table's index of decided frequency by governor
Chanwoo Choi99613312016-03-22 13:44:03 +090079 * of parent device.
80 */
81 for (i = 0; i < parent_devfreq->profile->max_state; i++)
82 if (parent_devfreq->profile->freq_table[i] == *freq)
83 break;
84
Saravana Kannan86ad9a22021-02-04 16:14:24 +080085 if (i == parent_devfreq->profile->max_state)
86 return -EINVAL;
Chanwoo Choi99613312016-03-22 13:44:03 +090087
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];
91 } else {
92 count = devfreq->profile->max_state;
93 child_freq = devfreq->profile->freq_table[count - 1];
94 }
95
96 /* Return the suitable frequency for passive device. */
97 *freq = child_freq;
98
Saravana Kannan86ad9a22021-02-04 16:14:24 +080099 return 0;
Chanwoo Choi99613312016-03-22 13:44:03 +0900100}
101
Chanwoo Choi99613312016-03-22 13:44:03 +0900102static int devfreq_passive_notifier_call(struct notifier_block *nb,
103 unsigned long event, void *ptr)
104{
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;
Chanwoo Choib4365422020-10-07 22:02:39 +0900111 int ret = 0;
Chanwoo Choi99613312016-03-22 13:44:03 +0900112
Chanwoo Choib4365422020-10-07 22:02:39 +0900113 mutex_lock_nested(&devfreq->lock, SINGLE_DEPTH_NESTING);
Chanwoo Choi99613312016-03-22 13:44:03 +0900114 switch (event) {
115 case DEVFREQ_PRECHANGE:
116 if (parent->previous_freq > freq)
Chanwoo Choib4365422020-10-07 22:02:39 +0900117 ret = devfreq_update_target(devfreq, freq);
118
Chanwoo Choi99613312016-03-22 13:44:03 +0900119 break;
120 case DEVFREQ_POSTCHANGE:
121 if (parent->previous_freq < freq)
Chanwoo Choib4365422020-10-07 22:02:39 +0900122 ret = devfreq_update_target(devfreq, freq);
Chanwoo Choi99613312016-03-22 13:44:03 +0900123 break;
124 }
Chanwoo Choib4365422020-10-07 22:02:39 +0900125 mutex_unlock(&devfreq->lock);
126
127 if (ret < 0)
128 dev_warn(&devfreq->dev,
129 "failed to update devfreq using passive governor\n");
Chanwoo Choi99613312016-03-22 13:44:03 +0900130
131 return NOTIFY_DONE;
132}
133
134static int devfreq_passive_event_handler(struct devfreq *devfreq,
135 unsigned int event, void *data)
136{
Chanwoo Choi99613312016-03-22 13:44:03 +0900137 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;
141 int ret = 0;
142
143 if (!parent)
144 return -EPROBE_DEFER;
145
146 switch (event) {
147 case DEVFREQ_GOV_START:
148 if (!p_data->this)
149 p_data->this = devfreq;
150
151 nb->notifier_call = devfreq_passive_notifier_call;
Leonard Crestez0ef7c7c2019-08-08 19:54:08 +0300152 ret = devfreq_register_notifier(parent, nb,
Chanwoo Choi99613312016-03-22 13:44:03 +0900153 DEVFREQ_TRANSITION_NOTIFIER);
154 break;
155 case DEVFREQ_GOV_STOP:
Leonard Crestez0ef7c7c2019-08-08 19:54:08 +0300156 WARN_ON(devfreq_unregister_notifier(parent, nb,
157 DEVFREQ_TRANSITION_NOTIFIER));
Chanwoo Choi99613312016-03-22 13:44:03 +0900158 break;
159 default:
160 break;
161 }
162
163 return ret;
164}
165
166static struct devfreq_governor devfreq_passive = {
Chanwoo Choiaa7c3522017-10-23 10:32:12 +0900167 .name = DEVFREQ_GOV_PASSIVE,
Chanwoo Choi0dd25a02020-10-05 14:48:01 +0900168 .flags = DEVFREQ_GOV_FLAG_IMMUTABLE,
Chanwoo Choi99613312016-03-22 13:44:03 +0900169 .get_target_freq = devfreq_passive_get_target_freq,
170 .event_handler = devfreq_passive_event_handler,
171};
172
173static int __init devfreq_passive_init(void)
174{
175 return devfreq_add_governor(&devfreq_passive);
176}
177subsys_initcall(devfreq_passive_init);
178
179static void __exit devfreq_passive_exit(void)
180{
181 int ret;
182
183 ret = devfreq_remove_governor(&devfreq_passive);
184 if (ret)
185 pr_err("%s: failed remove governor %d\n", __func__, ret);
Chanwoo Choi99613312016-03-22 13:44:03 +0900186}
187module_exit(devfreq_passive_exit);
188
189MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
190MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
191MODULE_DESCRIPTION("DEVFREQ Passive governor");
192MODULE_LICENSE("GPL v2");