Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 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 | |
| 24 | #include "wl1271_reg.h" |
| 25 | #include "wl1271_ps.h" |
Teemu Paasikivi | 7b048c5 | 2010-02-18 13:25:55 +0200 | [diff] [blame] | 26 | #include "wl1271_io.h" |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 27 | |
| 28 | #define WL1271_WAKEUP_TIMEOUT 500 |
| 29 | |
Juuso Oikarinen | 37b70a8 | 2009-10-08 21:56:21 +0300 | [diff] [blame] | 30 | void wl1271_elp_work(struct work_struct *work) |
| 31 | { |
| 32 | struct delayed_work *dwork; |
| 33 | struct wl1271 *wl; |
| 34 | |
| 35 | dwork = container_of(work, struct delayed_work, work); |
| 36 | wl = container_of(dwork, struct wl1271, elp_work); |
| 37 | |
| 38 | wl1271_debug(DEBUG_PSM, "elp work"); |
| 39 | |
| 40 | mutex_lock(&wl->mutex); |
| 41 | |
Juuso Oikarinen | 71449f8 | 2009-12-11 15:41:07 +0200 | [diff] [blame] | 42 | if (test_bit(WL1271_FLAG_IN_ELP, &wl->flags) || |
Juuso Oikarinen | e197281 | 2010-04-09 11:07:29 +0300 | [diff] [blame] | 43 | (!test_bit(WL1271_FLAG_PSM, &wl->flags) && |
| 44 | !test_bit(WL1271_FLAG_IDLE, &wl->flags))) |
Juuso Oikarinen | 37b70a8 | 2009-10-08 21:56:21 +0300 | [diff] [blame] | 45 | goto out; |
| 46 | |
| 47 | wl1271_debug(DEBUG_PSM, "chip to elp"); |
Juuso Oikarinen | 7462141 | 2009-10-12 15:08:54 +0300 | [diff] [blame] | 48 | wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_SLEEP); |
Juuso Oikarinen | 71449f8 | 2009-12-11 15:41:07 +0200 | [diff] [blame] | 49 | set_bit(WL1271_FLAG_IN_ELP, &wl->flags); |
Juuso Oikarinen | 37b70a8 | 2009-10-08 21:56:21 +0300 | [diff] [blame] | 50 | |
| 51 | out: |
| 52 | mutex_unlock(&wl->mutex); |
| 53 | } |
| 54 | |
| 55 | #define ELP_ENTRY_DELAY 5 |
| 56 | |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 57 | /* Routines to toggle sleep mode while in ELP */ |
| 58 | void wl1271_ps_elp_sleep(struct wl1271 *wl) |
| 59 | { |
Juuso Oikarinen | e197281 | 2010-04-09 11:07:29 +0300 | [diff] [blame] | 60 | if (test_bit(WL1271_FLAG_PSM, &wl->flags) || |
| 61 | test_bit(WL1271_FLAG_IDLE, &wl->flags)) { |
Juuso Oikarinen | 37b70a8 | 2009-10-08 21:56:21 +0300 | [diff] [blame] | 62 | cancel_delayed_work(&wl->elp_work); |
| 63 | ieee80211_queue_delayed_work(wl->hw, &wl->elp_work, |
| 64 | msecs_to_jiffies(ELP_ENTRY_DELAY)); |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
| 68 | int wl1271_ps_elp_wakeup(struct wl1271 *wl, bool chip_awake) |
| 69 | { |
| 70 | DECLARE_COMPLETION_ONSTACK(compl); |
| 71 | unsigned long flags; |
| 72 | int ret; |
| 73 | u32 start_time = jiffies; |
| 74 | bool pending = false; |
| 75 | |
Juuso Oikarinen | 71449f8 | 2009-12-11 15:41:07 +0200 | [diff] [blame] | 76 | if (!test_bit(WL1271_FLAG_IN_ELP, &wl->flags)) |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 77 | return 0; |
| 78 | |
| 79 | wl1271_debug(DEBUG_PSM, "waking up chip from elp"); |
| 80 | |
| 81 | /* |
| 82 | * The spinlock is required here to synchronize both the work and |
| 83 | * the completion variable in one entity. |
| 84 | */ |
| 85 | spin_lock_irqsave(&wl->wl_lock, flags); |
| 86 | if (work_pending(&wl->irq_work) || chip_awake) |
| 87 | pending = true; |
| 88 | else |
| 89 | wl->elp_compl = &compl; |
| 90 | spin_unlock_irqrestore(&wl->wl_lock, flags); |
| 91 | |
Juuso Oikarinen | 7462141 | 2009-10-12 15:08:54 +0300 | [diff] [blame] | 92 | wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_WAKE_UP); |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 93 | |
| 94 | if (!pending) { |
| 95 | ret = wait_for_completion_timeout( |
| 96 | &compl, msecs_to_jiffies(WL1271_WAKEUP_TIMEOUT)); |
| 97 | if (ret == 0) { |
| 98 | wl1271_error("ELP wakeup timeout!"); |
| 99 | ret = -ETIMEDOUT; |
| 100 | goto err; |
| 101 | } else if (ret < 0) { |
| 102 | wl1271_error("ELP wakeup completion error."); |
| 103 | goto err; |
| 104 | } |
| 105 | } |
| 106 | |
Juuso Oikarinen | 71449f8 | 2009-12-11 15:41:07 +0200 | [diff] [blame] | 107 | clear_bit(WL1271_FLAG_IN_ELP, &wl->flags); |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 108 | |
| 109 | wl1271_debug(DEBUG_PSM, "wakeup time: %u ms", |
| 110 | jiffies_to_msecs(jiffies - start_time)); |
| 111 | goto out; |
| 112 | |
| 113 | err: |
| 114 | spin_lock_irqsave(&wl->wl_lock, flags); |
| 115 | wl->elp_compl = NULL; |
| 116 | spin_unlock_irqrestore(&wl->wl_lock, flags); |
| 117 | return ret; |
| 118 | |
| 119 | out: |
| 120 | return 0; |
| 121 | } |
| 122 | |
Juuso Oikarinen | d8c42c0 | 2010-02-18 13:25:36 +0200 | [diff] [blame] | 123 | int wl1271_ps_set_mode(struct wl1271 *wl, enum wl1271_cmd_ps_mode mode, |
| 124 | bool send) |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 125 | { |
| 126 | int ret; |
| 127 | |
| 128 | switch (mode) { |
| 129 | case STATION_POWER_SAVE_MODE: |
| 130 | wl1271_debug(DEBUG_PSM, "entering psm"); |
Juuso Oikarinen | 1922167 | 2009-10-08 21:56:35 +0300 | [diff] [blame] | 131 | |
Juuso Oikarinen | d8c42c0 | 2010-02-18 13:25:36 +0200 | [diff] [blame] | 132 | ret = wl1271_cmd_ps_mode(wl, STATION_POWER_SAVE_MODE, send); |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 133 | if (ret < 0) |
| 134 | return ret; |
| 135 | |
Juuso Oikarinen | 71449f8 | 2009-12-11 15:41:07 +0200 | [diff] [blame] | 136 | set_bit(WL1271_FLAG_PSM, &wl->flags); |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 137 | break; |
| 138 | case STATION_ACTIVE_MODE: |
| 139 | default: |
| 140 | wl1271_debug(DEBUG_PSM, "leaving psm"); |
| 141 | ret = wl1271_ps_elp_wakeup(wl, false); |
| 142 | if (ret < 0) |
| 143 | return ret; |
| 144 | |
Juuso Oikarinen | 11f70f9 | 2009-10-13 12:47:46 +0300 | [diff] [blame] | 145 | /* disable beacon early termination */ |
| 146 | ret = wl1271_acx_bet_enable(wl, false); |
| 147 | if (ret < 0) |
| 148 | return ret; |
| 149 | |
Juuso Oikarinen | 1922167 | 2009-10-08 21:56:35 +0300 | [diff] [blame] | 150 | /* disable beacon filtering */ |
| 151 | ret = wl1271_acx_beacon_filter_opt(wl, false); |
| 152 | if (ret < 0) |
| 153 | return ret; |
| 154 | |
Juuso Oikarinen | d8c42c0 | 2010-02-18 13:25:36 +0200 | [diff] [blame] | 155 | ret = wl1271_cmd_ps_mode(wl, STATION_ACTIVE_MODE, send); |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 156 | if (ret < 0) |
| 157 | return ret; |
| 158 | |
Juuso Oikarinen | 71449f8 | 2009-12-11 15:41:07 +0200 | [diff] [blame] | 159 | clear_bit(WL1271_FLAG_PSM, &wl->flags); |
Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 160 | break; |
| 161 | } |
| 162 | |
| 163 | return ret; |
| 164 | } |
| 165 | |
| 166 | |