governor_userspace.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * linux/drivers/devfreq/governor_simpleondemand.c
  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. #include <linux/slab.h>
  12. #include <linux/device.h>
  13. #include <linux/devfreq.h>
  14. #include <linux/pm.h>
  15. #include <linux/mutex.h>
  16. #include <linux/module.h>
  17. #include "governor.h"
  18. struct userspace_data {
  19. unsigned long user_frequency;
  20. bool valid;
  21. };
  22. static int devfreq_userspace_func(struct devfreq *df, unsigned long *freq)
  23. {
  24. struct userspace_data *data = df->data;
  25. if (data->valid) {
  26. unsigned long adjusted_freq = data->user_frequency;
  27. if (df->max_freq && adjusted_freq > df->max_freq)
  28. adjusted_freq = df->max_freq;
  29. if (df->min_freq && adjusted_freq < df->min_freq)
  30. adjusted_freq = df->min_freq;
  31. *freq = adjusted_freq;
  32. } else {
  33. *freq = df->previous_freq; /* No user freq specified yet */
  34. }
  35. return 0;
  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. mutex_lock(&devfreq->lock);
  45. data = devfreq->data;
  46. sscanf(buf, "%lu", &wanted);
  47. data->user_frequency = wanted;
  48. data->valid = true;
  49. err = update_devfreq(devfreq);
  50. if (err == 0)
  51. err = count;
  52. mutex_unlock(&devfreq->lock);
  53. return err;
  54. }
  55. static ssize_t show_freq(struct device *dev, struct device_attribute *attr,
  56. char *buf)
  57. {
  58. struct devfreq *devfreq = to_devfreq(dev);
  59. struct userspace_data *data;
  60. int err = 0;
  61. mutex_lock(&devfreq->lock);
  62. data = devfreq->data;
  63. if (data->valid)
  64. err = sprintf(buf, "%lu\n", data->user_frequency);
  65. else
  66. err = sprintf(buf, "undefined\n");
  67. mutex_unlock(&devfreq->lock);
  68. return err;
  69. }
  70. static DEVICE_ATTR(set_freq, 0644, show_freq, store_freq);
  71. static struct attribute *dev_entries[] = {
  72. &dev_attr_set_freq.attr,
  73. NULL,
  74. };
  75. static struct attribute_group dev_attr_group = {
  76. .name = "userspace",
  77. .attrs = dev_entries,
  78. };
  79. static int userspace_init(struct devfreq *devfreq)
  80. {
  81. int err = 0;
  82. struct userspace_data *data = kzalloc(sizeof(struct userspace_data),
  83. GFP_KERNEL);
  84. if (!data) {
  85. err = -ENOMEM;
  86. goto out;
  87. }
  88. data->valid = false;
  89. devfreq->data = data;
  90. err = sysfs_create_group(&devfreq->dev.kobj, &dev_attr_group);
  91. out:
  92. return err;
  93. }
  94. static void userspace_exit(struct devfreq *devfreq)
  95. {
  96. sysfs_remove_group(&devfreq->dev.kobj, &dev_attr_group);
  97. kfree(devfreq->data);
  98. devfreq->data = NULL;
  99. }
  100. static int devfreq_userspace_handler(struct devfreq *devfreq,
  101. unsigned int event, void *data)
  102. {
  103. int ret = 0;
  104. switch (event) {
  105. case DEVFREQ_GOV_START:
  106. ret = userspace_init(devfreq);
  107. break;
  108. case DEVFREQ_GOV_STOP:
  109. userspace_exit(devfreq);
  110. break;
  111. default:
  112. break;
  113. }
  114. return ret;
  115. }
  116. static struct devfreq_governor devfreq_userspace = {
  117. .name = "userspace",
  118. .get_target_freq = devfreq_userspace_func,
  119. .event_handler = devfreq_userspace_handler,
  120. };
  121. static int __init devfreq_userspace_init(void)
  122. {
  123. return devfreq_add_governor(&devfreq_userspace);
  124. }
  125. subsys_initcall(devfreq_userspace_init);
  126. static void __exit devfreq_userspace_exit(void)
  127. {
  128. int ret;
  129. ret = devfreq_remove_governor(&devfreq_userspace);
  130. if (ret)
  131. pr_err("%s: failed remove governor %d\n", __func__, ret);
  132. return;
  133. }
  134. module_exit(devfreq_userspace_exit);
  135. MODULE_LICENSE("GPL");