ps.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * This file is part of wl12xx
  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 "ps.h"
  25. #include "spi.h"
  26. #define WL12XX_WAKEUP_TIMEOUT 2000
  27. /* Routines to toggle sleep mode while in ELP */
  28. void wl12xx_ps_elp_sleep(struct wl12xx *wl)
  29. {
  30. if (wl->elp || !wl->psm)
  31. return;
  32. wl12xx_debug(DEBUG_PSM, "chip to elp");
  33. wl12xx_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_SLEEP);
  34. wl->elp = true;
  35. }
  36. int wl12xx_ps_elp_wakeup(struct wl12xx *wl)
  37. {
  38. unsigned long timeout;
  39. u32 elp_reg;
  40. if (!wl->elp)
  41. return 0;
  42. wl12xx_debug(DEBUG_PSM, "waking up chip from elp");
  43. timeout = jiffies + msecs_to_jiffies(WL12XX_WAKEUP_TIMEOUT);
  44. wl12xx_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_WAKE_UP);
  45. elp_reg = wl12xx_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. wl12xx_error("elp wakeup timeout");
  53. return -ETIMEDOUT;
  54. }
  55. msleep(1);
  56. elp_reg = wl12xx_read32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
  57. }
  58. wl12xx_debug(DEBUG_PSM, "wakeup time: %u ms",
  59. jiffies_to_msecs(jiffies) -
  60. (jiffies_to_msecs(timeout) - WL12XX_WAKEUP_TIMEOUT));
  61. wl->elp = false;
  62. return 0;
  63. }
  64. static int wl12xx_ps_set_elp(struct wl12xx *wl, bool enable)
  65. {
  66. int ret;
  67. if (enable) {
  68. wl12xx_debug(DEBUG_PSM, "sleep auth psm/elp");
  69. /*
  70. * FIXME: we should PSM_ELP, but because of firmware wakeup
  71. * problems let's use only PSM_PS
  72. */
  73. ret = wl12xx_acx_sleep_auth(wl, WL12XX_PSM_PS);
  74. if (ret < 0)
  75. return ret;
  76. wl12xx_ps_elp_sleep(wl);
  77. } else {
  78. wl12xx_debug(DEBUG_PSM, "sleep auth cam");
  79. /*
  80. * When the target is in ELP, we can only
  81. * access the ELP control register. Thus,
  82. * we have to wake the target up before
  83. * changing the power authorization.
  84. */
  85. wl12xx_ps_elp_wakeup(wl);
  86. ret = wl12xx_acx_sleep_auth(wl, WL12XX_PSM_CAM);
  87. if (ret < 0)
  88. return ret;
  89. }
  90. return 0;
  91. }
  92. int wl12xx_ps_set_mode(struct wl12xx *wl, enum acx_ps_mode mode)
  93. {
  94. int ret;
  95. switch (mode) {
  96. case STATION_POWER_SAVE_MODE:
  97. wl12xx_debug(DEBUG_PSM, "entering psm");
  98. ret = wl12xx_cmd_ps_mode(wl, STATION_POWER_SAVE_MODE);
  99. if (ret < 0)
  100. return ret;
  101. ret = wl12xx_ps_set_elp(wl, true);
  102. if (ret < 0)
  103. return ret;
  104. wl->psm = 1;
  105. break;
  106. case STATION_ACTIVE_MODE:
  107. default:
  108. wl12xx_debug(DEBUG_PSM, "leaving psm");
  109. ret = wl12xx_ps_set_elp(wl, false);
  110. if (ret < 0)
  111. return ret;
  112. ret = wl12xx_cmd_ps_mode(wl, STATION_ACTIVE_MODE);
  113. if (ret < 0)
  114. return ret;
  115. wl->psm = 0;
  116. break;
  117. }
  118. return ret;
  119. }