1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * This file is part of wl1251
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2008 Nokia Corporation
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #include "reg.h"
9*4882a593Smuzhiyun #include "ps.h"
10*4882a593Smuzhiyun #include "cmd.h"
11*4882a593Smuzhiyun #include "io.h"
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun /* in ms */
14*4882a593Smuzhiyun #define WL1251_WAKEUP_TIMEOUT 100
15*4882a593Smuzhiyun
wl1251_elp_work(struct work_struct * work)16*4882a593Smuzhiyun void wl1251_elp_work(struct work_struct *work)
17*4882a593Smuzhiyun {
18*4882a593Smuzhiyun struct delayed_work *dwork;
19*4882a593Smuzhiyun struct wl1251 *wl;
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun dwork = to_delayed_work(work);
22*4882a593Smuzhiyun wl = container_of(dwork, struct wl1251, elp_work);
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun wl1251_debug(DEBUG_PSM, "elp work");
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun mutex_lock(&wl->mutex);
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun if (wl->elp || wl->station_mode == STATION_ACTIVE_MODE)
29*4882a593Smuzhiyun goto out;
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun wl1251_debug(DEBUG_PSM, "chip to elp");
32*4882a593Smuzhiyun wl1251_write_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_SLEEP);
33*4882a593Smuzhiyun wl->elp = true;
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun out:
36*4882a593Smuzhiyun mutex_unlock(&wl->mutex);
37*4882a593Smuzhiyun }
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun #define ELP_ENTRY_DELAY 5
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun /* Routines to toggle sleep mode while in ELP */
wl1251_ps_elp_sleep(struct wl1251 * wl)42*4882a593Smuzhiyun void wl1251_ps_elp_sleep(struct wl1251 *wl)
43*4882a593Smuzhiyun {
44*4882a593Smuzhiyun unsigned long delay;
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun if (wl->station_mode != STATION_ACTIVE_MODE) {
47*4882a593Smuzhiyun delay = msecs_to_jiffies(ELP_ENTRY_DELAY);
48*4882a593Smuzhiyun ieee80211_queue_delayed_work(wl->hw, &wl->elp_work, delay);
49*4882a593Smuzhiyun }
50*4882a593Smuzhiyun }
51*4882a593Smuzhiyun
wl1251_ps_elp_wakeup(struct wl1251 * wl)52*4882a593Smuzhiyun int wl1251_ps_elp_wakeup(struct wl1251 *wl)
53*4882a593Smuzhiyun {
54*4882a593Smuzhiyun unsigned long timeout, start;
55*4882a593Smuzhiyun u32 elp_reg;
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun cancel_delayed_work(&wl->elp_work);
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun if (!wl->elp)
60*4882a593Smuzhiyun return 0;
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun wl1251_debug(DEBUG_PSM, "waking up chip from elp");
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun start = jiffies;
65*4882a593Smuzhiyun timeout = jiffies + msecs_to_jiffies(WL1251_WAKEUP_TIMEOUT);
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun wl1251_write_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_WAKE_UP);
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun elp_reg = wl1251_read_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun /*
72*4882a593Smuzhiyun * FIXME: we should wait for irq from chip but, as a temporary
73*4882a593Smuzhiyun * solution to simplify locking, let's poll instead
74*4882a593Smuzhiyun */
75*4882a593Smuzhiyun while (!(elp_reg & ELPCTRL_WLAN_READY)) {
76*4882a593Smuzhiyun if (time_after(jiffies, timeout)) {
77*4882a593Smuzhiyun wl1251_error("elp wakeup timeout");
78*4882a593Smuzhiyun return -ETIMEDOUT;
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun msleep(1);
81*4882a593Smuzhiyun elp_reg = wl1251_read_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
82*4882a593Smuzhiyun }
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun wl1251_debug(DEBUG_PSM, "wakeup time: %u ms",
85*4882a593Smuzhiyun jiffies_to_msecs(jiffies - start));
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun wl->elp = false;
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun return 0;
90*4882a593Smuzhiyun }
91*4882a593Smuzhiyun
wl1251_ps_set_mode(struct wl1251 * wl,enum wl1251_station_mode mode)92*4882a593Smuzhiyun int wl1251_ps_set_mode(struct wl1251 *wl, enum wl1251_station_mode mode)
93*4882a593Smuzhiyun {
94*4882a593Smuzhiyun int ret;
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun switch (mode) {
97*4882a593Smuzhiyun case STATION_POWER_SAVE_MODE:
98*4882a593Smuzhiyun wl1251_debug(DEBUG_PSM, "entering psm");
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun /* enable beacon filtering */
101*4882a593Smuzhiyun ret = wl1251_acx_beacon_filter_opt(wl, true);
102*4882a593Smuzhiyun if (ret < 0)
103*4882a593Smuzhiyun return ret;
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun ret = wl1251_acx_wake_up_conditions(wl,
106*4882a593Smuzhiyun WAKE_UP_EVENT_DTIM_BITMAP,
107*4882a593Smuzhiyun wl->listen_int);
108*4882a593Smuzhiyun if (ret < 0)
109*4882a593Smuzhiyun return ret;
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun ret = wl1251_acx_bet_enable(wl, WL1251_ACX_BET_ENABLE,
112*4882a593Smuzhiyun WL1251_DEFAULT_BET_CONSECUTIVE);
113*4882a593Smuzhiyun if (ret < 0)
114*4882a593Smuzhiyun return ret;
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun ret = wl1251_cmd_ps_mode(wl, CHIP_POWER_SAVE_MODE);
117*4882a593Smuzhiyun if (ret < 0)
118*4882a593Smuzhiyun return ret;
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun ret = wl1251_acx_sleep_auth(wl, WL1251_PSM_ELP);
121*4882a593Smuzhiyun if (ret < 0)
122*4882a593Smuzhiyun return ret;
123*4882a593Smuzhiyun break;
124*4882a593Smuzhiyun case STATION_IDLE:
125*4882a593Smuzhiyun wl1251_debug(DEBUG_PSM, "entering idle");
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun ret = wl1251_acx_sleep_auth(wl, WL1251_PSM_ELP);
128*4882a593Smuzhiyun if (ret < 0)
129*4882a593Smuzhiyun return ret;
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun ret = wl1251_cmd_template_set(wl, CMD_DISCONNECT, NULL, 0);
132*4882a593Smuzhiyun if (ret < 0)
133*4882a593Smuzhiyun return ret;
134*4882a593Smuzhiyun break;
135*4882a593Smuzhiyun case STATION_ACTIVE_MODE:
136*4882a593Smuzhiyun default:
137*4882a593Smuzhiyun wl1251_debug(DEBUG_PSM, "leaving psm");
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun ret = wl1251_acx_sleep_auth(wl, WL1251_PSM_CAM);
140*4882a593Smuzhiyun if (ret < 0)
141*4882a593Smuzhiyun return ret;
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun /* disable BET */
144*4882a593Smuzhiyun ret = wl1251_acx_bet_enable(wl, WL1251_ACX_BET_DISABLE,
145*4882a593Smuzhiyun WL1251_DEFAULT_BET_CONSECUTIVE);
146*4882a593Smuzhiyun if (ret < 0)
147*4882a593Smuzhiyun return ret;
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun /* disable beacon filtering */
150*4882a593Smuzhiyun ret = wl1251_acx_beacon_filter_opt(wl, false);
151*4882a593Smuzhiyun if (ret < 0)
152*4882a593Smuzhiyun return ret;
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun ret = wl1251_acx_wake_up_conditions(wl,
155*4882a593Smuzhiyun WAKE_UP_EVENT_DTIM_BITMAP,
156*4882a593Smuzhiyun wl->listen_int);
157*4882a593Smuzhiyun if (ret < 0)
158*4882a593Smuzhiyun return ret;
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun ret = wl1251_cmd_ps_mode(wl, CHIP_ACTIVE_MODE);
161*4882a593Smuzhiyun if (ret < 0)
162*4882a593Smuzhiyun return ret;
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun break;
165*4882a593Smuzhiyun }
166*4882a593Smuzhiyun wl->station_mode = mode;
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun return ret;
169*4882a593Smuzhiyun }
170*4882a593Smuzhiyun
171