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