wl1271_ps.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * This file is part of wl1271
  3. *
  4. * Copyright (C) 2008-2009 Nokia Corporation
  5. *
  6. * Contact: Luciano Coelho <luciano.coelho@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 "wl1271_reg.h"
  24. #include "wl1271_ps.h"
  25. #include "wl1271_spi.h"
  26. #define WL1271_WAKEUP_TIMEOUT 500
  27. /* Routines to toggle sleep mode while in ELP */
  28. void wl1271_ps_elp_sleep(struct wl1271 *wl)
  29. {
  30. /*
  31. * FIXME: due to a problem in the firmware (causing a firmware
  32. * crash), ELP entry is prevented below. Remove the "true" to
  33. * re-enable ELP entry.
  34. */
  35. if (true || wl->elp || !wl->psm)
  36. return;
  37. /*
  38. * Go to ELP unless there is work already pending - pending work
  39. * will immediately wakeup the chipset anyway.
  40. */
  41. if (!work_pending(&wl->irq_work) && !work_pending(&wl->tx_work)) {
  42. wl1271_debug(DEBUG_PSM, "chip to elp");
  43. wl1271_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_SLEEP);
  44. wl->elp = true;
  45. }
  46. }
  47. int wl1271_ps_elp_wakeup(struct wl1271 *wl, bool chip_awake)
  48. {
  49. DECLARE_COMPLETION_ONSTACK(compl);
  50. unsigned long flags;
  51. int ret;
  52. u32 start_time = jiffies;
  53. bool pending = false;
  54. if (!wl->elp)
  55. return 0;
  56. wl1271_debug(DEBUG_PSM, "waking up chip from elp");
  57. /*
  58. * The spinlock is required here to synchronize both the work and
  59. * the completion variable in one entity.
  60. */
  61. spin_lock_irqsave(&wl->wl_lock, flags);
  62. if (work_pending(&wl->irq_work) || chip_awake)
  63. pending = true;
  64. else
  65. wl->elp_compl = &compl;
  66. spin_unlock_irqrestore(&wl->wl_lock, flags);
  67. wl1271_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_WAKE_UP);
  68. if (!pending) {
  69. ret = wait_for_completion_timeout(
  70. &compl, msecs_to_jiffies(WL1271_WAKEUP_TIMEOUT));
  71. if (ret == 0) {
  72. wl1271_error("ELP wakeup timeout!");
  73. ret = -ETIMEDOUT;
  74. goto err;
  75. } else if (ret < 0) {
  76. wl1271_error("ELP wakeup completion error.");
  77. goto err;
  78. }
  79. }
  80. wl->elp = false;
  81. wl1271_debug(DEBUG_PSM, "wakeup time: %u ms",
  82. jiffies_to_msecs(jiffies - start_time));
  83. goto out;
  84. err:
  85. spin_lock_irqsave(&wl->wl_lock, flags);
  86. wl->elp_compl = NULL;
  87. spin_unlock_irqrestore(&wl->wl_lock, flags);
  88. return ret;
  89. out:
  90. return 0;
  91. }
  92. int wl1271_ps_set_mode(struct wl1271 *wl, enum wl1271_cmd_ps_mode mode)
  93. {
  94. int ret;
  95. switch (mode) {
  96. case STATION_POWER_SAVE_MODE:
  97. wl1271_debug(DEBUG_PSM, "entering psm");
  98. ret = wl1271_cmd_ps_mode(wl, STATION_POWER_SAVE_MODE);
  99. if (ret < 0)
  100. return ret;
  101. wl1271_ps_elp_sleep(wl);
  102. if (ret < 0)
  103. return ret;
  104. wl->psm = 1;
  105. break;
  106. case STATION_ACTIVE_MODE:
  107. default:
  108. wl1271_debug(DEBUG_PSM, "leaving psm");
  109. ret = wl1271_ps_elp_wakeup(wl, false);
  110. if (ret < 0)
  111. return ret;
  112. ret = wl1271_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. }