cpuidle.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * arch/sh/kernel/cpu/shmobile/cpuidle.c
  3. *
  4. * Cpuidle support code for SuperH Mobile
  5. *
  6. * Copyright (C) 2009 Magnus Damm
  7. *
  8. * This file is subject to the terms and conditions of the GNU General Public
  9. * License. See the file "COPYING" in the main directory of this archive
  10. * for more details.
  11. */
  12. #include <linux/init.h>
  13. #include <linux/kernel.h>
  14. #include <linux/io.h>
  15. #include <linux/suspend.h>
  16. #include <linux/cpuidle.h>
  17. #include <linux/export.h>
  18. #include <asm/suspend.h>
  19. #include <asm/uaccess.h>
  20. static unsigned long cpuidle_mode[] = {
  21. SUSP_SH_SLEEP, /* regular sleep mode */
  22. SUSP_SH_SLEEP | SUSP_SH_SF, /* sleep mode + self refresh */
  23. SUSP_SH_STANDBY | SUSP_SH_SF, /* software standby mode + self refresh */
  24. };
  25. static int cpuidle_sleep_enter(struct cpuidle_device *dev,
  26. struct cpuidle_driver *drv,
  27. int index)
  28. {
  29. unsigned long allowed_mode = SUSP_SH_SLEEP;
  30. ktime_t before, after;
  31. int requested_state = index;
  32. int allowed_state;
  33. int k;
  34. /* convert allowed mode to allowed state */
  35. for (k = ARRAY_SIZE(cpuidle_mode) - 1; k > 0; k--)
  36. if (cpuidle_mode[k] == allowed_mode)
  37. break;
  38. allowed_state = k;
  39. /* take the following into account for sleep mode selection:
  40. * - allowed_state: best mode allowed by hardware (clock deps)
  41. * - requested_state: best mode allowed by software (latencies)
  42. */
  43. k = min_t(int, allowed_state, requested_state);
  44. before = ktime_get();
  45. sh_mobile_call_standby(cpuidle_mode[k]);
  46. after = ktime_get();
  47. dev->last_residency = (int)ktime_to_ns(ktime_sub(after, before)) >> 10;
  48. return k;
  49. }
  50. static struct cpuidle_device cpuidle_dev;
  51. static struct cpuidle_driver cpuidle_driver = {
  52. .name = "sh_idle",
  53. .owner = THIS_MODULE,
  54. };
  55. void sh_mobile_setup_cpuidle(void)
  56. {
  57. struct cpuidle_device *dev = &cpuidle_dev;
  58. struct cpuidle_driver *drv = &cpuidle_driver;
  59. struct cpuidle_state *state;
  60. int i;
  61. for (i = 0; i < CPUIDLE_STATE_MAX; i++) {
  62. drv->states[i].name[0] = '\0';
  63. drv->states[i].desc[0] = '\0';
  64. }
  65. i = CPUIDLE_DRIVER_STATE_START;
  66. state = &drv->states[i++];
  67. snprintf(state->name, CPUIDLE_NAME_LEN, "C1");
  68. strncpy(state->desc, "SuperH Sleep Mode", CPUIDLE_DESC_LEN);
  69. state->exit_latency = 1;
  70. state->target_residency = 1 * 2;
  71. state->power_usage = 3;
  72. state->flags = 0;
  73. state->flags |= CPUIDLE_FLAG_TIME_VALID;
  74. state->enter = cpuidle_sleep_enter;
  75. drv->safe_state_index = i-1;
  76. if (sh_mobile_sleep_supported & SUSP_SH_SF) {
  77. state = &drv->states[i++];
  78. snprintf(state->name, CPUIDLE_NAME_LEN, "C2");
  79. strncpy(state->desc, "SuperH Sleep Mode [SF]",
  80. CPUIDLE_DESC_LEN);
  81. state->exit_latency = 100;
  82. state->target_residency = 1 * 2;
  83. state->power_usage = 1;
  84. state->flags = 0;
  85. state->flags |= CPUIDLE_FLAG_TIME_VALID;
  86. state->enter = cpuidle_sleep_enter;
  87. }
  88. if (sh_mobile_sleep_supported & SUSP_SH_STANDBY) {
  89. state = &drv->states[i++];
  90. snprintf(state->name, CPUIDLE_NAME_LEN, "C3");
  91. strncpy(state->desc, "SuperH Mobile Standby Mode [SF]",
  92. CPUIDLE_DESC_LEN);
  93. state->exit_latency = 2300;
  94. state->target_residency = 1 * 2;
  95. state->power_usage = 1;
  96. state->flags = 0;
  97. state->flags |= CPUIDLE_FLAG_TIME_VALID;
  98. state->enter = cpuidle_sleep_enter;
  99. }
  100. drv->state_count = i;
  101. dev->state_count = i;
  102. cpuidle_register_driver(&cpuidle_driver);
  103. cpuidle_register_device(dev);
  104. }