wl1251_ps.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 "reg.h"
  24. #include "wl1251_ps.h"
  25. #include "wl1251_spi.h"
  26. #define WL1251_WAKEUP_TIMEOUT 2000
  27. /* Routines to toggle sleep mode while in ELP */
  28. void wl1251_ps_elp_sleep(struct wl1251 *wl)
  29. {
  30. if (wl->elp || !wl->psm)
  31. return;
  32. wl1251_debug(DEBUG_PSM, "chip to elp");
  33. wl1251_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_SLEEP);
  34. wl->elp = true;
  35. }
  36. int wl1251_ps_elp_wakeup(struct wl1251 *wl)
  37. {
  38. unsigned long timeout;
  39. u32 elp_reg;
  40. if (!wl->elp)
  41. return 0;
  42. wl1251_debug(DEBUG_PSM, "waking up chip from elp");
  43. timeout = jiffies + msecs_to_jiffies(WL1251_WAKEUP_TIMEOUT);
  44. wl1251_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_WAKE_UP);
  45. elp_reg = wl1251_read32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
  46. /*
  47. * FIXME: we should wait for irq from chip but, as a temporary
  48. * solution to simplify locking, let's poll instead
  49. */
  50. while (!(elp_reg & ELPCTRL_WLAN_READY)) {
  51. if (time_after(jiffies, timeout)) {
  52. wl1251_error("elp wakeup timeout");
  53. return -ETIMEDOUT;
  54. }
  55. msleep(1);
  56. elp_reg = wl1251_read32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
  57. }
  58. wl1251_debug(DEBUG_PSM, "wakeup time: %u ms",
  59. jiffies_to_msecs(jiffies) -
  60. (jiffies_to_msecs(timeout) - WL1251_WAKEUP_TIMEOUT));
  61. wl->elp = false;
  62. return 0;
  63. }
  64. static int wl1251_ps_set_elp(struct wl1251 *wl, bool enable)
  65. {
  66. int ret;
  67. if (enable) {
  68. wl1251_debug(DEBUG_PSM, "sleep auth psm/elp");
  69. ret = wl1251_acx_sleep_auth(wl, WL1251_PSM_ELP);
  70. if (ret < 0)
  71. return ret;
  72. wl1251_ps_elp_sleep(wl);
  73. } else {
  74. wl1251_debug(DEBUG_PSM, "sleep auth cam");
  75. /*
  76. * When the target is in ELP, we can only
  77. * access the ELP control register. Thus,
  78. * we have to wake the target up before
  79. * changing the power authorization.
  80. */
  81. wl1251_ps_elp_wakeup(wl);
  82. ret = wl1251_acx_sleep_auth(wl, WL1251_PSM_CAM);
  83. if (ret < 0)
  84. return ret;
  85. }
  86. return 0;
  87. }
  88. int wl1251_ps_set_mode(struct wl1251 *wl, enum wl1251_cmd_ps_mode mode)
  89. {
  90. int ret;
  91. switch (mode) {
  92. case STATION_POWER_SAVE_MODE:
  93. wl1251_debug(DEBUG_PSM, "entering psm");
  94. ret = wl1251_cmd_ps_mode(wl, STATION_POWER_SAVE_MODE);
  95. if (ret < 0)
  96. return ret;
  97. ret = wl1251_ps_set_elp(wl, true);
  98. if (ret < 0)
  99. return ret;
  100. wl->psm = 1;
  101. break;
  102. case STATION_ACTIVE_MODE:
  103. default:
  104. wl1251_debug(DEBUG_PSM, "leaving psm");
  105. ret = wl1251_ps_set_elp(wl, false);
  106. if (ret < 0)
  107. return ret;
  108. ret = wl1251_cmd_ps_mode(wl, STATION_ACTIVE_MODE);
  109. if (ret < 0)
  110. return ret;
  111. wl->psm = 0;
  112. break;
  113. }
  114. return ret;
  115. }