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