wl1251_ps.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * This file is part of wl1251
  3. *
  4. * Copyright (C) 2008 Nokia Corporation
  5. *
  6. * Contact: Kalle Valo <kalle.valo@nokia.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA
  21. *
  22. */
  23. #include "wl1251_reg.h"
  24. #include "wl1251_ps.h"
  25. #include "wl1251_cmd.h"
  26. #include "wl1251_io.h"
  27. #define WL1251_WAKEUP_TIMEOUT 2000
  28. /* Routines to toggle sleep mode while in ELP */
  29. void wl1251_ps_elp_sleep(struct wl1251 *wl)
  30. {
  31. if (wl->elp || !wl->psm)
  32. return;
  33. wl1251_debug(DEBUG_PSM, "chip to elp");
  34. wl1251_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_SLEEP);
  35. wl->elp = true;
  36. }
  37. int wl1251_ps_elp_wakeup(struct wl1251 *wl)
  38. {
  39. unsigned long timeout;
  40. u32 elp_reg;
  41. if (!wl->elp)
  42. return 0;
  43. wl1251_debug(DEBUG_PSM, "waking up chip from elp");
  44. timeout = jiffies + msecs_to_jiffies(WL1251_WAKEUP_TIMEOUT);
  45. wl1251_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_WAKE_UP);
  46. elp_reg = wl1251_read32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
  47. /*
  48. * FIXME: we should wait for irq from chip but, as a temporary
  49. * solution to simplify locking, let's poll instead
  50. */
  51. while (!(elp_reg & ELPCTRL_WLAN_READY)) {
  52. if (time_after(jiffies, timeout)) {
  53. wl1251_error("elp wakeup timeout");
  54. return -ETIMEDOUT;
  55. }
  56. msleep(1);
  57. elp_reg = wl1251_read32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
  58. }
  59. wl1251_debug(DEBUG_PSM, "wakeup time: %u ms",
  60. jiffies_to_msecs(jiffies) -
  61. (jiffies_to_msecs(timeout) - WL1251_WAKEUP_TIMEOUT));
  62. wl->elp = false;
  63. return 0;
  64. }
  65. static int wl1251_ps_set_elp(struct wl1251 *wl, bool enable)
  66. {
  67. int ret;
  68. if (enable) {
  69. wl1251_debug(DEBUG_PSM, "sleep auth psm/elp");
  70. ret = wl1251_acx_sleep_auth(wl, WL1251_PSM_ELP);
  71. if (ret < 0)
  72. return ret;
  73. wl1251_ps_elp_sleep(wl);
  74. } else {
  75. wl1251_debug(DEBUG_PSM, "sleep auth cam");
  76. /*
  77. * When the target is in ELP, we can only
  78. * access the ELP control register. Thus,
  79. * we have to wake the target up before
  80. * changing the power authorization.
  81. */
  82. wl1251_ps_elp_wakeup(wl);
  83. ret = wl1251_acx_sleep_auth(wl, WL1251_PSM_CAM);
  84. if (ret < 0)
  85. return ret;
  86. }
  87. return 0;
  88. }
  89. int wl1251_ps_set_mode(struct wl1251 *wl, enum wl1251_cmd_ps_mode mode)
  90. {
  91. int ret;
  92. switch (mode) {
  93. case STATION_POWER_SAVE_MODE:
  94. wl1251_debug(DEBUG_PSM, "entering psm");
  95. ret = wl1251_acx_wake_up_conditions(wl,
  96. WAKE_UP_EVENT_DTIM_BITMAP,
  97. wl->listen_int);
  98. if (ret < 0)
  99. return ret;
  100. ret = wl1251_cmd_ps_mode(wl, STATION_POWER_SAVE_MODE);
  101. if (ret < 0)
  102. return ret;
  103. ret = wl1251_ps_set_elp(wl, true);
  104. if (ret < 0)
  105. return ret;
  106. wl->psm = 1;
  107. break;
  108. case STATION_ACTIVE_MODE:
  109. default:
  110. wl1251_debug(DEBUG_PSM, "leaving psm");
  111. ret = wl1251_ps_set_elp(wl, false);
  112. if (ret < 0)
  113. return ret;
  114. ret = wl1251_acx_wake_up_conditions(wl,
  115. WAKE_UP_EVENT_DTIM_BITMAP,
  116. wl->listen_int);
  117. if (ret < 0)
  118. return ret;
  119. ret = wl1251_cmd_ps_mode(wl, STATION_ACTIVE_MODE);
  120. if (ret < 0)
  121. return ret;
  122. wl->psm = 0;
  123. break;
  124. }
  125. return ret;
  126. }