2019-06-04 11:11:33 +03:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2011-10-02 02:19:34 +04:00
|
|
|
/*
|
2017-01-16 15:26:04 +03:00
|
|
|
* linux/drivers/devfreq/governor_userspace.c
|
2011-10-02 02:19:34 +04:00
|
|
|
*
|
|
|
|
* Copyright (C) 2011 Samsung Electronics
|
|
|
|
* MyungJoo Ham <myungjoo.ham@samsung.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/device.h>
|
|
|
|
#include <linux/devfreq.h>
|
|
|
|
#include <linux/pm.h>
|
|
|
|
#include <linux/mutex.h>
|
2012-10-30 00:01:46 +04:00
|
|
|
#include <linux/module.h>
|
2011-10-02 02:19:34 +04:00
|
|
|
#include "governor.h"
|
|
|
|
|
|
|
|
struct userspace_data {
|
|
|
|
unsigned long user_frequency;
|
|
|
|
bool valid;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int devfreq_userspace_func(struct devfreq *df, unsigned long *freq)
|
|
|
|
{
|
2022-10-25 10:21:09 +03:00
|
|
|
struct userspace_data *data = df->governor_data;
|
2011-10-02 02:19:34 +04:00
|
|
|
|
2018-08-03 23:05:10 +03:00
|
|
|
if (data->valid)
|
|
|
|
*freq = data->user_frequency;
|
|
|
|
else
|
2011-10-02 02:19:34 +04:00
|
|
|
*freq = df->previous_freq; /* No user freq specified yet */
|
2018-08-03 23:05:10 +03:00
|
|
|
|
2011-10-02 02:19:34 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-05-24 05:11:58 +03:00
|
|
|
static ssize_t set_freq_store(struct device *dev, struct device_attribute *attr,
|
|
|
|
const char *buf, size_t count)
|
2011-10-02 02:19:34 +04:00
|
|
|
{
|
|
|
|
struct devfreq *devfreq = to_devfreq(dev);
|
|
|
|
struct userspace_data *data;
|
|
|
|
unsigned long wanted;
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
mutex_lock(&devfreq->lock);
|
2022-10-25 10:21:09 +03:00
|
|
|
data = devfreq->governor_data;
|
2011-10-02 02:19:34 +04:00
|
|
|
|
|
|
|
sscanf(buf, "%lu", &wanted);
|
|
|
|
data->user_frequency = wanted;
|
|
|
|
data->valid = true;
|
|
|
|
err = update_devfreq(devfreq);
|
|
|
|
if (err == 0)
|
|
|
|
err = count;
|
|
|
|
mutex_unlock(&devfreq->lock);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2021-05-24 05:11:58 +03:00
|
|
|
static ssize_t set_freq_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
2011-10-02 02:19:34 +04:00
|
|
|
{
|
|
|
|
struct devfreq *devfreq = to_devfreq(dev);
|
|
|
|
struct userspace_data *data;
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
mutex_lock(&devfreq->lock);
|
2022-10-25 10:21:09 +03:00
|
|
|
data = devfreq->governor_data;
|
2011-10-02 02:19:34 +04:00
|
|
|
|
|
|
|
if (data->valid)
|
|
|
|
err = sprintf(buf, "%lu\n", data->user_frequency);
|
|
|
|
else
|
|
|
|
err = sprintf(buf, "undefined\n");
|
|
|
|
mutex_unlock(&devfreq->lock);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2021-05-24 05:11:58 +03:00
|
|
|
static DEVICE_ATTR_RW(set_freq);
|
2011-10-02 02:19:34 +04:00
|
|
|
static struct attribute *dev_entries[] = {
|
|
|
|
&dev_attr_set_freq.attr,
|
|
|
|
NULL,
|
|
|
|
};
|
2017-07-03 13:10:04 +03:00
|
|
|
static const struct attribute_group dev_attr_group = {
|
2017-10-23 04:32:12 +03:00
|
|
|
.name = DEVFREQ_GOV_USERSPACE,
|
2011-10-02 02:19:34 +04:00
|
|
|
.attrs = dev_entries,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int userspace_init(struct devfreq *devfreq)
|
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
struct userspace_data *data = kzalloc(sizeof(struct userspace_data),
|
|
|
|
GFP_KERNEL);
|
|
|
|
|
|
|
|
if (!data) {
|
|
|
|
err = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
data->valid = false;
|
2022-10-25 10:21:09 +03:00
|
|
|
devfreq->governor_data = data;
|
2011-10-02 02:19:34 +04:00
|
|
|
|
|
|
|
err = sysfs_create_group(&devfreq->dev.kobj, &dev_attr_group);
|
|
|
|
out:
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void userspace_exit(struct devfreq *devfreq)
|
|
|
|
{
|
2017-01-12 17:57:41 +03:00
|
|
|
/*
|
|
|
|
* Remove the sysfs entry, unless this is being called after
|
|
|
|
* device_del(), which should have done this already via kobject_del().
|
|
|
|
*/
|
|
|
|
if (devfreq->dev.kobj.sd)
|
|
|
|
sysfs_remove_group(&devfreq->dev.kobj, &dev_attr_group);
|
|
|
|
|
2022-10-25 10:21:09 +03:00
|
|
|
kfree(devfreq->governor_data);
|
|
|
|
devfreq->governor_data = NULL;
|
2011-10-02 02:19:34 +04:00
|
|
|
}
|
|
|
|
|
2012-10-26 03:50:09 +04:00
|
|
|
static int devfreq_userspace_handler(struct devfreq *devfreq,
|
|
|
|
unsigned int event, void *data)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
switch (event) {
|
|
|
|
case DEVFREQ_GOV_START:
|
|
|
|
ret = userspace_init(devfreq);
|
|
|
|
break;
|
|
|
|
case DEVFREQ_GOV_STOP:
|
|
|
|
userspace_exit(devfreq);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-10-30 00:01:45 +04:00
|
|
|
static struct devfreq_governor devfreq_userspace = {
|
2020-01-23 18:59:12 +03:00
|
|
|
.name = DEVFREQ_GOV_USERSPACE,
|
2011-10-02 02:19:34 +04:00
|
|
|
.get_target_freq = devfreq_userspace_func,
|
2012-10-26 03:50:09 +04:00
|
|
|
.event_handler = devfreq_userspace_handler,
|
2011-10-02 02:19:34 +04:00
|
|
|
};
|
2012-10-30 00:01:44 +04:00
|
|
|
|
|
|
|
static int __init devfreq_userspace_init(void)
|
|
|
|
{
|
|
|
|
return devfreq_add_governor(&devfreq_userspace);
|
|
|
|
}
|
|
|
|
subsys_initcall(devfreq_userspace_init);
|
|
|
|
|
|
|
|
static void __exit devfreq_userspace_exit(void)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = devfreq_remove_governor(&devfreq_userspace);
|
|
|
|
if (ret)
|
|
|
|
pr_err("%s: failed remove governor %d\n", __func__, ret);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
module_exit(devfreq_userspace_exit);
|
2012-10-30 00:01:46 +04:00
|
|
|
MODULE_LICENSE("GPL");
|