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