1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/drivers/devfreq/governor_userspace.c
5 * Copyright (C) 2011 Samsung Electronics
6 * MyungJoo Ham <myungjoo.ham@samsung.com>
9 #include <linux/slab.h>
10 #include <linux/device.h>
11 #include <linux/devfreq.h>
13 #include <linux/mutex.h>
14 #include <linux/module.h>
17 struct userspace_data {
18 unsigned long user_frequency;
22 static int devfreq_userspace_func(struct devfreq *df, unsigned long *freq)
24 struct userspace_data *data = df->data;
27 *freq = data->user_frequency;
29 *freq = df->previous_freq; /* No user freq specified yet */
34 static ssize_t set_freq_store(struct device *dev, struct device_attribute *attr,
35 const char *buf, size_t count)
37 struct devfreq *devfreq = to_devfreq(dev);
38 struct userspace_data *data;
42 mutex_lock(&devfreq->lock);
45 sscanf(buf, "%lu", &wanted);
46 data->user_frequency = wanted;
48 err = update_devfreq(devfreq);
51 mutex_unlock(&devfreq->lock);
55 static ssize_t set_freq_show(struct device *dev,
56 struct device_attribute *attr, char *buf)
58 struct devfreq *devfreq = to_devfreq(dev);
59 struct userspace_data *data;
62 mutex_lock(&devfreq->lock);
66 err = sprintf(buf, "%lu\n", data->user_frequency);
68 err = sprintf(buf, "undefined\n");
69 mutex_unlock(&devfreq->lock);
73 static DEVICE_ATTR_RW(set_freq);
74 static struct attribute *dev_entries[] = {
75 &dev_attr_set_freq.attr,
78 static const struct attribute_group dev_attr_group = {
79 .name = DEVFREQ_GOV_USERSPACE,
83 static int userspace_init(struct devfreq *devfreq)
86 struct userspace_data *data = kzalloc(sizeof(struct userspace_data),
96 err = sysfs_create_group(&devfreq->dev.kobj, &dev_attr_group);
101 static void userspace_exit(struct devfreq *devfreq)
104 * Remove the sysfs entry, unless this is being called after
105 * device_del(), which should have done this already via kobject_del().
107 if (devfreq->dev.kobj.sd)
108 sysfs_remove_group(&devfreq->dev.kobj, &dev_attr_group);
110 kfree(devfreq->data);
111 devfreq->data = NULL;
114 static int devfreq_userspace_handler(struct devfreq *devfreq,
115 unsigned int event, void *data)
120 case DEVFREQ_GOV_START:
121 ret = userspace_init(devfreq);
123 case DEVFREQ_GOV_STOP:
124 userspace_exit(devfreq);
133 static struct devfreq_governor devfreq_userspace = {
134 .name = DEVFREQ_GOV_USERSPACE,
135 .get_target_freq = devfreq_userspace_func,
136 .event_handler = devfreq_userspace_handler,
139 static int __init devfreq_userspace_init(void)
141 return devfreq_add_governor(&devfreq_userspace);
143 subsys_initcall(devfreq_userspace_init);
145 static void __exit devfreq_userspace_exit(void)
149 ret = devfreq_remove_governor(&devfreq_userspace);
151 pr_err("%s: failed remove governor %d\n", __func__, ret);
155 module_exit(devfreq_userspace_exit);
156 MODULE_LICENSE("GPL");