governor_userspace.c 3.5 KB

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