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