^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_userspace.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2011 Samsung Electronics
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * MyungJoo Ham <myungjoo.ham@samsung.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/devfreq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/pm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include "governor.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) struct userspace_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) unsigned long user_frequency;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) bool valid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static int devfreq_userspace_func(struct devfreq *df, unsigned long *freq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct userspace_data *data = df->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) if (data->valid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) *freq = data->user_frequency;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) *freq = df->previous_freq; /* No user freq specified yet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static ssize_t store_freq(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct devfreq *devfreq = to_devfreq(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct userspace_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) unsigned long wanted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) mutex_lock(&devfreq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) data = devfreq->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) sscanf(buf, "%lu", &wanted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) data->user_frequency = wanted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) data->valid = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) err = update_devfreq(devfreq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) if (err == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) err = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) mutex_unlock(&devfreq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) return err;
^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) static ssize_t show_freq(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) struct devfreq *devfreq = to_devfreq(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) struct userspace_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) mutex_lock(&devfreq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) data = devfreq->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (data->valid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) err = sprintf(buf, "%lu\n", data->user_frequency);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) err = sprintf(buf, "undefined\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) mutex_unlock(&devfreq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) static DEVICE_ATTR(set_freq, 0644, show_freq, store_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) static struct attribute *dev_entries[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) &dev_attr_set_freq.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) static const struct attribute_group dev_attr_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) .name = DEVFREQ_GOV_USERSPACE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) .attrs = dev_entries,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) static int userspace_init(struct devfreq *devfreq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) struct userspace_data *data = kzalloc(sizeof(struct userspace_data),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (!data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) data->valid = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) devfreq->data = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) err = sysfs_create_group(&devfreq->dev.kobj, &dev_attr_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) static void userspace_exit(struct devfreq *devfreq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * Remove the sysfs entry, unless this is being called after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * device_del(), which should have done this already via kobject_del().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (devfreq->dev.kobj.sd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) sysfs_remove_group(&devfreq->dev.kobj, &dev_attr_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) kfree(devfreq->data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) devfreq->data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) static int devfreq_userspace_handler(struct devfreq *devfreq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) unsigned int event, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) switch (event) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) case DEVFREQ_GOV_START:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) ret = userspace_init(devfreq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) case DEVFREQ_GOV_STOP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) userspace_exit(devfreq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) static struct devfreq_governor devfreq_userspace = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) .name = DEVFREQ_GOV_USERSPACE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) .get_target_freq = devfreq_userspace_func,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) .event_handler = devfreq_userspace_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) static int __init devfreq_userspace_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) return devfreq_add_governor(&devfreq_userspace);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) subsys_initcall(devfreq_userspace_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) static void __exit devfreq_userspace_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) ret = devfreq_remove_governor(&devfreq_userspace);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) pr_err("%s: failed remove governor %d\n", __func__, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) module_exit(devfreq_userspace_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) MODULE_LICENSE("GPL");