2 * linux/drivers/devfreq/governor_userspace.c
4 * Copyright (C) 2011 Samsung Electronics
5 * MyungJoo Ham <myungjoo.ham@samsung.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/slab.h>
13 #include <linux/device.h>
14 #include <linux/devfreq.h>
16 #include <linux/mutex.h>
17 #include <linux/module.h>
20 struct userspace_data
{
21 unsigned long user_frequency
;
25 static int devfreq_userspace_func(struct devfreq
*df
, unsigned long *freq
)
27 struct userspace_data
*data
= df
->data
;
30 unsigned long adjusted_freq
= data
->user_frequency
;
32 if (df
->max_freq
&& adjusted_freq
> df
->max_freq
)
33 adjusted_freq
= df
->max_freq
;
35 if (df
->min_freq
&& adjusted_freq
< df
->min_freq
)
36 adjusted_freq
= df
->min_freq
;
38 *freq
= adjusted_freq
;
40 *freq
= df
->previous_freq
; /* No user freq specified yet */
45 static ssize_t
store_freq(struct device
*dev
, struct device_attribute
*attr
,
46 const char *buf
, size_t count
)
48 struct devfreq
*devfreq
= to_devfreq(dev
);
49 struct userspace_data
*data
;
53 mutex_lock(&devfreq
->lock
);
56 sscanf(buf
, "%lu", &wanted
);
57 data
->user_frequency
= wanted
;
59 err
= update_devfreq(devfreq
);
62 mutex_unlock(&devfreq
->lock
);
66 static ssize_t
show_freq(struct device
*dev
, struct device_attribute
*attr
,
69 struct devfreq
*devfreq
= to_devfreq(dev
);
70 struct userspace_data
*data
;
73 mutex_lock(&devfreq
->lock
);
77 err
= sprintf(buf
, "%lu\n", data
->user_frequency
);
79 err
= sprintf(buf
, "undefined\n");
80 mutex_unlock(&devfreq
->lock
);
84 static DEVICE_ATTR(set_freq
, 0644, show_freq
, store_freq
);
85 static struct attribute
*dev_entries
[] = {
86 &dev_attr_set_freq
.attr
,
89 static const struct attribute_group dev_attr_group
= {
90 .name
= DEVFREQ_GOV_USERSPACE
,
94 static int userspace_init(struct devfreq
*devfreq
)
97 struct userspace_data
*data
= kzalloc(sizeof(struct userspace_data
),
105 devfreq
->data
= data
;
107 err
= sysfs_create_group(&devfreq
->dev
.kobj
, &dev_attr_group
);
112 static void userspace_exit(struct devfreq
*devfreq
)
115 * Remove the sysfs entry, unless this is being called after
116 * device_del(), which should have done this already via kobject_del().
118 if (devfreq
->dev
.kobj
.sd
)
119 sysfs_remove_group(&devfreq
->dev
.kobj
, &dev_attr_group
);
121 kfree(devfreq
->data
);
122 devfreq
->data
= NULL
;
125 static int devfreq_userspace_handler(struct devfreq
*devfreq
,
126 unsigned int event
, void *data
)
131 case DEVFREQ_GOV_START
:
132 ret
= userspace_init(devfreq
);
134 case DEVFREQ_GOV_STOP
:
135 userspace_exit(devfreq
);
144 static struct devfreq_governor devfreq_userspace
= {
146 .get_target_freq
= devfreq_userspace_func
,
147 .event_handler
= devfreq_userspace_handler
,
150 static int __init
devfreq_userspace_init(void)
152 return devfreq_add_governor(&devfreq_userspace
);
154 subsys_initcall(devfreq_userspace_init
);
156 static void __exit
devfreq_userspace_exit(void)
160 ret
= devfreq_remove_governor(&devfreq_userspace
);
162 pr_err("%s: failed remove governor %d\n", __func__
, ret
);
166 module_exit(devfreq_userspace_exit
);
167 MODULE_LICENSE("GPL");