governor_userspace.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 "governor.h"
  17. struct userspace_data {
  18. unsigned long user_frequency;
  19. bool valid;
  20. };
  21. static int devfreq_userspace_func(struct devfreq *df, unsigned long *freq)
  22. {
  23. struct userspace_data *data = df->data;
  24. if (!data->valid)
  25. *freq = df->previous_freq; /* No user freq specified yet */
  26. else
  27. *freq = data->user_frequency;
  28. return 0;
  29. }
  30. static ssize_t store_freq(struct device *dev, struct device_attribute *attr,
  31. const char *buf, size_t count)
  32. {
  33. struct devfreq *devfreq = to_devfreq(dev);
  34. struct userspace_data *data;
  35. unsigned long wanted;
  36. int err = 0;
  37. mutex_lock(&devfreq->lock);
  38. data = devfreq->data;
  39. sscanf(buf, "%lu", &wanted);
  40. data->user_frequency = wanted;
  41. data->valid = true;
  42. err = update_devfreq(devfreq);
  43. if (err == 0)
  44. err = count;
  45. mutex_unlock(&devfreq->lock);
  46. return err;
  47. }
  48. static ssize_t show_freq(struct device *dev, struct device_attribute *attr,
  49. char *buf)
  50. {
  51. struct devfreq *devfreq = to_devfreq(dev);
  52. struct userspace_data *data;
  53. int err = 0;
  54. mutex_lock(&devfreq->lock);
  55. data = devfreq->data;
  56. if (data->valid)
  57. err = sprintf(buf, "%lu\n", data->user_frequency);
  58. else
  59. err = sprintf(buf, "undefined\n");
  60. mutex_unlock(&devfreq->lock);
  61. return err;
  62. }
  63. static DEVICE_ATTR(set_freq, 0644, show_freq, store_freq);
  64. static struct attribute *dev_entries[] = {
  65. &dev_attr_set_freq.attr,
  66. NULL,
  67. };
  68. static struct attribute_group dev_attr_group = {
  69. .name = "userspace",
  70. .attrs = dev_entries,
  71. };
  72. static int userspace_init(struct devfreq *devfreq)
  73. {
  74. int err = 0;
  75. struct userspace_data *data = kzalloc(sizeof(struct userspace_data),
  76. GFP_KERNEL);
  77. if (!data) {
  78. err = -ENOMEM;
  79. goto out;
  80. }
  81. data->valid = false;
  82. devfreq->data = data;
  83. err = sysfs_create_group(&devfreq->dev.kobj, &dev_attr_group);
  84. out:
  85. return err;
  86. }
  87. static void userspace_exit(struct devfreq *devfreq)
  88. {
  89. sysfs_remove_group(&devfreq->dev.kobj, &dev_attr_group);
  90. kfree(devfreq->data);
  91. devfreq->data = NULL;
  92. }
  93. const struct devfreq_governor devfreq_userspace = {
  94. .name = "userspace",
  95. .get_target_freq = devfreq_userspace_func,
  96. .init = userspace_init,
  97. .exit = userspace_exit,
  98. .no_central_polling = true,
  99. };