Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Chanwoo Choi | 9961331 | 2016-03-22 13:44:03 +0900 | [diff] [blame] | 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> |
Chanwoo Choi | 9961331 | 2016-03-22 13:44:03 +0900 | [diff] [blame] | 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 | */ |
Chanwoo Choi | 9961331 | 2016-03-22 13:44:03 +0900 | [diff] [blame] | 59 | opp = devfreq_recommended_opp(parent_devfreq->dev.parent, freq, 0); |
Chanwoo Choi | 9961331 | 2016-03-22 13:44:03 +0900 | [diff] [blame] | 60 | if (IS_ERR(opp)) { |
| 61 | ret = PTR_ERR(opp); |
| 62 | goto out; |
| 63 | } |
| 64 | |
Viresh Kumar | 8a31d9d9 | 2017-01-23 10:11:47 +0530 | [diff] [blame] | 65 | dev_pm_opp_put(opp); |
| 66 | |
Chanwoo Choi | 9961331 | 2016-03-22 13:44:03 +0900 | [diff] [blame] | 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 | |
| 95 | static int update_devfreq_passive(struct devfreq *devfreq, unsigned long freq) |
| 96 | { |
| 97 | int ret; |
| 98 | |
| 99 | if (!devfreq->governor) |
| 100 | return -EINVAL; |
| 101 | |
| 102 | mutex_lock_nested(&devfreq->lock, SINGLE_DEPTH_NESTING); |
| 103 | |
| 104 | ret = devfreq->governor->get_target_freq(devfreq, &freq); |
| 105 | if (ret < 0) |
| 106 | goto out; |
| 107 | |
| 108 | ret = devfreq->profile->target(devfreq->dev.parent, &freq, 0); |
| 109 | if (ret < 0) |
| 110 | goto out; |
| 111 | |
Chanwoo Choi | 30582c2 | 2017-01-31 15:38:17 +0900 | [diff] [blame] | 112 | if (devfreq->profile->freq_table |
| 113 | && (devfreq_update_status(devfreq, freq))) |
| 114 | dev_err(&devfreq->dev, |
| 115 | "Couldn't update frequency transition information.\n"); |
| 116 | |
Chanwoo Choi | 9961331 | 2016-03-22 13:44:03 +0900 | [diff] [blame] | 117 | devfreq->previous_freq = freq; |
| 118 | |
| 119 | out: |
| 120 | mutex_unlock(&devfreq->lock); |
| 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | static int devfreq_passive_notifier_call(struct notifier_block *nb, |
| 126 | unsigned long event, void *ptr) |
| 127 | { |
| 128 | struct devfreq_passive_data *data |
| 129 | = container_of(nb, struct devfreq_passive_data, nb); |
| 130 | struct devfreq *devfreq = (struct devfreq *)data->this; |
| 131 | struct devfreq *parent = (struct devfreq *)data->parent; |
| 132 | struct devfreq_freqs *freqs = (struct devfreq_freqs *)ptr; |
| 133 | unsigned long freq = freqs->new; |
| 134 | |
| 135 | switch (event) { |
| 136 | case DEVFREQ_PRECHANGE: |
| 137 | if (parent->previous_freq > freq) |
| 138 | update_devfreq_passive(devfreq, freq); |
| 139 | break; |
| 140 | case DEVFREQ_POSTCHANGE: |
| 141 | if (parent->previous_freq < freq) |
| 142 | update_devfreq_passive(devfreq, freq); |
| 143 | break; |
| 144 | } |
| 145 | |
| 146 | return NOTIFY_DONE; |
| 147 | } |
| 148 | |
| 149 | static int devfreq_passive_event_handler(struct devfreq *devfreq, |
| 150 | unsigned int event, void *data) |
| 151 | { |
Chanwoo Choi | 9961331 | 2016-03-22 13:44:03 +0900 | [diff] [blame] | 152 | struct devfreq_passive_data *p_data |
| 153 | = (struct devfreq_passive_data *)devfreq->data; |
| 154 | struct devfreq *parent = (struct devfreq *)p_data->parent; |
| 155 | struct notifier_block *nb = &p_data->nb; |
| 156 | int ret = 0; |
| 157 | |
| 158 | if (!parent) |
| 159 | return -EPROBE_DEFER; |
| 160 | |
| 161 | switch (event) { |
| 162 | case DEVFREQ_GOV_START: |
| 163 | if (!p_data->this) |
| 164 | p_data->this = devfreq; |
| 165 | |
| 166 | nb->notifier_call = devfreq_passive_notifier_call; |
Leonard Crestez | 0ef7c7c | 2019-08-08 19:54:08 +0300 | [diff] [blame] | 167 | ret = devfreq_register_notifier(parent, nb, |
Chanwoo Choi | 9961331 | 2016-03-22 13:44:03 +0900 | [diff] [blame] | 168 | DEVFREQ_TRANSITION_NOTIFIER); |
| 169 | break; |
| 170 | case DEVFREQ_GOV_STOP: |
Leonard Crestez | 0ef7c7c | 2019-08-08 19:54:08 +0300 | [diff] [blame] | 171 | WARN_ON(devfreq_unregister_notifier(parent, nb, |
| 172 | DEVFREQ_TRANSITION_NOTIFIER)); |
Chanwoo Choi | 9961331 | 2016-03-22 13:44:03 +0900 | [diff] [blame] | 173 | break; |
| 174 | default: |
| 175 | break; |
| 176 | } |
| 177 | |
| 178 | return ret; |
| 179 | } |
| 180 | |
| 181 | static struct devfreq_governor devfreq_passive = { |
Chanwoo Choi | aa7c352 | 2017-10-23 10:32:12 +0900 | [diff] [blame] | 182 | .name = DEVFREQ_GOV_PASSIVE, |
Chanwoo Choi | bcf23c7 | 2017-01-31 15:38:16 +0900 | [diff] [blame] | 183 | .immutable = 1, |
Chanwoo Choi | 9961331 | 2016-03-22 13:44:03 +0900 | [diff] [blame] | 184 | .get_target_freq = devfreq_passive_get_target_freq, |
| 185 | .event_handler = devfreq_passive_event_handler, |
| 186 | }; |
| 187 | |
| 188 | static int __init devfreq_passive_init(void) |
| 189 | { |
| 190 | return devfreq_add_governor(&devfreq_passive); |
| 191 | } |
| 192 | subsys_initcall(devfreq_passive_init); |
| 193 | |
| 194 | static void __exit devfreq_passive_exit(void) |
| 195 | { |
| 196 | int ret; |
| 197 | |
| 198 | ret = devfreq_remove_governor(&devfreq_passive); |
| 199 | if (ret) |
| 200 | pr_err("%s: failed remove governor %d\n", __func__, ret); |
Chanwoo Choi | 9961331 | 2016-03-22 13:44:03 +0900 | [diff] [blame] | 201 | } |
| 202 | module_exit(devfreq_passive_exit); |
| 203 | |
| 204 | MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>"); |
| 205 | MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>"); |
| 206 | MODULE_DESCRIPTION("DEVFREQ Passive governor"); |
| 207 | MODULE_LICENSE("GPL v2"); |