cpuidle.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. #include <asm/hwblk.h>
  21. static unsigned long cpuidle_mode[] = {
  22. SUSP_SH_SLEEP, /* regular sleep mode */
  23. SUSP_SH_SLEEP | SUSP_SH_SF, /* sleep mode + self refresh */
  24. SUSP_SH_STANDBY | SUSP_SH_SF, /* software standby mode + self refresh */
  25. };
  26. static int cpuidle_sleep_enter(struct cpuidle_device *dev,
  27. struct cpuidle_driver *drv,
  28. int index)
  29. {
  30. unsigned long allowed_mode = arch_hwblk_sleep_mode();
  31. ktime_t before, after;
  32. int requested_state = index;
  33. int allowed_state;
  34. int k;
  35. /* convert allowed mode to allowed state */
  36. for (k = ARRAY_SIZE(cpuidle_mode) - 1; k > 0; k--)
  37. if (cpuidle_mode[k] == allowed_mode)
  38. break;
  39. allowed_state = k;
  40. /* take the following into account for sleep mode selection:
  41. * - allowed_state: best mode allowed by hardware (clock deps)
  42. * - requested_state: best mode allowed by software (latencies)
  43. */
  44. k = min_t(int, allowed_state, requested_state);
  45. before = ktime_get();
  46. sh_mobile_call_standby(cpuidle_mode[k]);
  47. after = ktime_get();
  48. dev->last_residency = (int)ktime_to_ns(ktime_sub(after, before)) >> 10;
  49. return k;
  50. }
  51. static struct cpuidle_device cpuidle_dev;
  52. static struct cpuidle_driver cpuidle_driver = {
  53. .name = "sh_idle",
  54. .owner = THIS_MODULE,
  55. };
  56. void sh_mobile_setup_cpuidle(void)
  57. {
  58. struct cpuidle_device *dev = &cpuidle_dev;
  59. struct cpuidle_driver *drv = &cpuidle_driver;
  60. struct cpuidle_state *state;
  61. int i;
  62. for (i = 0; i < CPUIDLE_STATE_MAX; i++) {
  63. drv->states[i].name[0] = '\0';
  64. drv->states[i].desc[0] = '\0';
  65. }
  66. i = CPUIDLE_DRIVER_STATE_START;
  67. state = &drv->states[i++];
  68. snprintf(state->name, CPUIDLE_NAME_LEN, "C1");
  69. strncpy(state->desc, "SuperH Sleep Mode", CPUIDLE_DESC_LEN);
  70. state->exit_latency = 1;
  71. state->target_residency = 1 * 2;
  72. state->power_usage = 3;
  73. state->flags = 0;
  74. state->flags |= CPUIDLE_FLAG_TIME_VALID;
  75. state->enter = cpuidle_sleep_enter;
  76. drv->safe_state_index = i-1;
  77. if (sh_mobile_sleep_supported & SUSP_SH_SF) {
  78. state = &drv->states[i++];
  79. snprintf(state->name, CPUIDLE_NAME_LEN, "C2");
  80. strncpy(state->desc, "SuperH Sleep Mode [SF]",
  81. CPUIDLE_DESC_LEN);
  82. state->exit_latency = 100;
  83. state->target_residency = 1 * 2;
  84. state->power_usage = 1;
  85. state->flags = 0;
  86. state->flags |= CPUIDLE_FLAG_TIME_VALID;
  87. state->enter = cpuidle_sleep_enter;
  88. }
  89. if (sh_mobile_sleep_supported & SUSP_SH_STANDBY) {
  90. state = &drv->states[i++];
  91. snprintf(state->name, CPUIDLE_NAME_LEN, "C3");
  92. strncpy(state->desc, "SuperH Mobile Standby Mode [SF]",
  93. CPUIDLE_DESC_LEN);
  94. state->exit_latency = 2300;
  95. state->target_residency = 1 * 2;
  96. state->power_usage = 1;
  97. state->flags = 0;
  98. state->flags |= CPUIDLE_FLAG_TIME_VALID;
  99. state->enter = cpuidle_sleep_enter;
  100. }
  101. drv->state_count = i;
  102. dev->state_count = i;
  103. cpuidle_register_driver(&cpuidle_driver);
  104. cpuidle_register_device(dev);
  105. }