Blame view

drivers/devfreq/governor_userspace.c 3.27 KB
d2912cb15   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-only
ce26c5bb9   MyungJoo Ham   PM / devfreq: Add...
2
  /*
c701335e7   Chanwoo Choi   PM / devfreq: Fix...
3
   *  linux/drivers/devfreq/governor_userspace.c
ce26c5bb9   MyungJoo Ham   PM / devfreq: Add...
4
5
6
   *
   *  Copyright (C) 2011 Samsung Electronics
   *	MyungJoo Ham <myungjoo.ham@samsung.com>
ce26c5bb9   MyungJoo Ham   PM / devfreq: Add...
7
8
9
10
11
12
13
   */
  
  #include <linux/slab.h>
  #include <linux/device.h>
  #include <linux/devfreq.h>
  #include <linux/pm.h>
  #include <linux/mutex.h>
eff607fdb   Nishanth Menon   PM / devfreq: gov...
14
  #include <linux/module.h>
ce26c5bb9   MyungJoo Ham   PM / devfreq: Add...
15
16
17
18
19
20
21
22
23
24
  #include "governor.h"
  
  struct userspace_data {
  	unsigned long user_frequency;
  	bool valid;
  };
  
  static int devfreq_userspace_func(struct devfreq *df, unsigned long *freq)
  {
  	struct userspace_data *data = df->data;
6ff66e2a0   Matthias Kaehlcke   PM / devfreq: Don...
25
26
27
  	if (data->valid)
  		*freq = data->user_frequency;
  	else
ce26c5bb9   MyungJoo Ham   PM / devfreq: Add...
28
  		*freq = df->previous_freq; /* No user freq specified yet */
6ff66e2a0   Matthias Kaehlcke   PM / devfreq: Don...
29

ce26c5bb9   MyungJoo Ham   PM / devfreq: Add...
30
31
32
33
34
35
36
37
38
39
  	return 0;
  }
  
  static ssize_t store_freq(struct device *dev, struct device_attribute *attr,
  			  const char *buf, size_t count)
  {
  	struct devfreq *devfreq = to_devfreq(dev);
  	struct userspace_data *data;
  	unsigned long wanted;
  	int err = 0;
ce26c5bb9   MyungJoo Ham   PM / devfreq: Add...
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
  	mutex_lock(&devfreq->lock);
  	data = devfreq->data;
  
  	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;
  }
  
  static ssize_t show_freq(struct device *dev, struct device_attribute *attr,
  			 char *buf)
  {
  	struct devfreq *devfreq = to_devfreq(dev);
  	struct userspace_data *data;
  	int err = 0;
  
  	mutex_lock(&devfreq->lock);
  	data = devfreq->data;
  
  	if (data->valid)
  		err = sprintf(buf, "%lu
  ", data->user_frequency);
  	else
  		err = sprintf(buf, "undefined
  ");
  	mutex_unlock(&devfreq->lock);
  	return err;
  }
  
  static DEVICE_ATTR(set_freq, 0644, show_freq, store_freq);
  static struct attribute *dev_entries[] = {
  	&dev_attr_set_freq.attr,
  	NULL,
  };
37d644aa0   Arvind Yadav   PM / devfreq: con...
78
  static const struct attribute_group dev_attr_group = {
aa7c352f9   Chanwoo Choi   PM / devfreq: Def...
79
  	.name	= DEVFREQ_GOV_USERSPACE,
ce26c5bb9   MyungJoo Ham   PM / devfreq: Add...
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
  	.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;
  	devfreq->data = data;
  
  	err = sysfs_create_group(&devfreq->dev.kobj, &dev_attr_group);
  out:
  	return err;
  }
  
  static void userspace_exit(struct devfreq *devfreq)
  {
924b9111a   Chris Diamand   PM / devfreq: Don...
103
104
105
106
107
108
  	/*
  	 * 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);
ce26c5bb9   MyungJoo Ham   PM / devfreq: Add...
109
110
111
  	kfree(devfreq->data);
  	devfreq->data = NULL;
  }
7e6fdd4ba   Rajagopal Venkat   PM / devfreq: Cor...
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
  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;
  }
1b5c1be2c   Nishanth Menon   PM / devfreq: map...
130
  static struct devfreq_governor devfreq_userspace = {
4bb1faacc   pierre Kuo   PM / devfreq: Use...
131
  	.name = DEVFREQ_GOV_USERSPACE,
ce26c5bb9   MyungJoo Ham   PM / devfreq: Add...
132
  	.get_target_freq = devfreq_userspace_func,
7e6fdd4ba   Rajagopal Venkat   PM / devfreq: Cor...
133
  	.event_handler = devfreq_userspace_handler,
ce26c5bb9   MyungJoo Ham   PM / devfreq: Add...
134
  };
83116e66a   Nishanth Menon   PM / devfreq: reg...
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
  
  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
  ", __func__, ret);
  
  	return;
  }
  module_exit(devfreq_userspace_exit);
eff607fdb   Nishanth Menon   PM / devfreq: gov...
154
  MODULE_LICENSE("GPL");