1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /* Copyright(c) 2018-2019 Realtek Corporation
14 static int rtw_ips_pwr_up(struct rtw_dev *rtwdev)
18 ret = rtw_core_start(rtwdev);
20 rtw_err(rtwdev, "leave idle state failed\n");
22 rtw_set_channel(rtwdev);
23 clear_bit(RTW_FLAG_INACTIVE_PS, rtwdev->flags);
28 int rtw_enter_ips(struct rtw_dev *rtwdev)
30 set_bit(RTW_FLAG_INACTIVE_PS, rtwdev->flags);
32 rtw_coex_ips_notify(rtwdev, COEX_IPS_ENTER);
34 rtw_core_stop(rtwdev);
39 static void rtw_restore_port_cfg_iter(void *data, u8 *mac,
40 struct ieee80211_vif *vif)
42 struct rtw_dev *rtwdev = data;
43 struct rtw_vif *rtwvif = (struct rtw_vif *)vif->drv_priv;
46 rtw_vif_port_config(rtwdev, rtwvif, config);
49 int rtw_leave_ips(struct rtw_dev *rtwdev)
53 ret = rtw_ips_pwr_up(rtwdev);
55 rtw_err(rtwdev, "failed to leave ips state\n");
59 rtw_iterate_vifs_atomic(rtwdev, rtw_restore_port_cfg_iter, rtwdev);
61 rtw_coex_ips_notify(rtwdev, COEX_IPS_LEAVE);
66 void rtw_power_mode_change(struct rtw_dev *rtwdev, bool enter)
68 u8 request, confirm, polling;
73 request = rtw_read8(rtwdev, rtwdev->hci.rpwm_addr);
74 confirm = rtw_read8(rtwdev, rtwdev->hci.cpwm_addr);
76 /* toggle to request power mode, others remain 0 */
77 request ^= request | BIT_RPWM_TOGGLE;
79 request |= POWER_MODE_ACK;
81 request |= POWER_MODE_LCLK;
82 if (rtw_fw_lps_deep_mode == LPS_DEEP_MODE_PG)
83 request |= POWER_MODE_PG;
86 rtw_write8(rtwdev, rtwdev->hci.rpwm_addr, request);
88 /* check confirm power mode has left power save state */
90 for (polling_cnt = 0; polling_cnt < 3; polling_cnt++) {
91 polling = rtw_read8(rtwdev, rtwdev->hci.cpwm_addr);
92 if ((polling ^ confirm) & BIT_RPWM_TOGGLE)
97 /* in case of fw/hw missed the request, retry 3 times */
99 rtw_warn(rtwdev, "failed to leave deep PS, retry=%d\n",
105 /* Hit here means that driver failed to change hardware
106 * power mode to active state after retry 3 times.
107 * If the power state is locked at Deep sleep, most of
108 * the hardware circuits is not working, even register
109 * read/write. It should be treated as fatal error and
110 * requires an entire analysis about the firmware/hardware
112 WARN(1, "Hardware power state locked\n");
115 EXPORT_SYMBOL(rtw_power_mode_change);
117 static void __rtw_leave_lps_deep(struct rtw_dev *rtwdev)
119 rtw_hci_deep_ps(rtwdev, false);
122 static void rtw_fw_leave_lps_state_check(struct rtw_dev *rtwdev)
126 /* Driver needs to wait for firmware to leave LPS state
127 * successfully. Firmware will send null packet to inform AP,
128 * and see if AP sends an ACK back, then firmware will restore
129 * the REG_TCR register.
131 * If driver does not wait for firmware, null packet with
132 * PS bit could be sent due to incorrect REG_TCR setting.
134 * In our test, 100ms should be enough for firmware to finish
135 * the flow. If REG_TCR Register is still incorrect after 100ms,
136 * just modify it directly, and throw a warn message.
138 for (i = 0 ; i < LEAVE_LPS_TRY_CNT; i++) {
139 if (rtw_read32_mask(rtwdev, REG_TCR, BIT_PWRMGT_HWDATA_EN) == 0)
144 rtw_write32_mask(rtwdev, REG_TCR, BIT_PWRMGT_HWDATA_EN, 0);
145 rtw_warn(rtwdev, "firmware failed to restore hardware setting\n");
148 static void rtw_leave_lps_core(struct rtw_dev *rtwdev)
150 struct rtw_lps_conf *conf = &rtwdev->lps_conf;
152 conf->state = RTW_ALL_ON;
153 conf->awake_interval = 1;
157 rtw_fw_set_pwr_mode(rtwdev);
158 rtw_fw_leave_lps_state_check(rtwdev);
160 clear_bit(RTW_FLAG_LEISURE_PS, rtwdev->flags);
162 rtw_coex_lps_notify(rtwdev, COEX_LPS_DISABLE);
165 static void __rtw_enter_lps_deep(struct rtw_dev *rtwdev)
167 if (rtwdev->lps_conf.deep_mode == LPS_DEEP_MODE_NONE)
170 if (!test_bit(RTW_FLAG_LEISURE_PS, rtwdev->flags)) {
171 rtw_dbg(rtwdev, RTW_DBG_PS,
172 "Should enter LPS before entering deep PS\n");
176 if (rtw_fw_lps_deep_mode == LPS_DEEP_MODE_PG)
177 rtw_fw_set_pg_info(rtwdev);
179 rtw_hci_deep_ps(rtwdev, true);
182 static void rtw_enter_lps_core(struct rtw_dev *rtwdev)
184 struct rtw_lps_conf *conf = &rtwdev->lps_conf;
186 conf->state = RTW_RF_OFF;
187 conf->awake_interval = 1;
191 rtw_coex_lps_notify(rtwdev, COEX_LPS_ENABLE);
193 rtw_fw_set_pwr_mode(rtwdev);
194 set_bit(RTW_FLAG_LEISURE_PS, rtwdev->flags);
197 static void __rtw_enter_lps(struct rtw_dev *rtwdev, u8 port_id)
199 struct rtw_lps_conf *conf = &rtwdev->lps_conf;
201 if (test_bit(RTW_FLAG_LEISURE_PS, rtwdev->flags))
204 conf->mode = RTW_MODE_LPS;
205 conf->port_id = port_id;
207 rtw_enter_lps_core(rtwdev);
210 static void __rtw_leave_lps(struct rtw_dev *rtwdev)
212 struct rtw_lps_conf *conf = &rtwdev->lps_conf;
214 if (test_and_clear_bit(RTW_FLAG_LEISURE_PS_DEEP, rtwdev->flags)) {
215 rtw_dbg(rtwdev, RTW_DBG_PS,
216 "Should leave deep PS before leaving LPS\n");
217 __rtw_leave_lps_deep(rtwdev);
220 if (!test_bit(RTW_FLAG_LEISURE_PS, rtwdev->flags))
223 conf->mode = RTW_MODE_ACTIVE;
225 rtw_leave_lps_core(rtwdev);
228 void rtw_enter_lps(struct rtw_dev *rtwdev, u8 port_id)
230 lockdep_assert_held(&rtwdev->mutex);
232 if (rtwdev->coex.stat.wl_force_lps_ctrl)
235 __rtw_enter_lps(rtwdev, port_id);
236 __rtw_enter_lps_deep(rtwdev);
239 void rtw_leave_lps(struct rtw_dev *rtwdev)
241 lockdep_assert_held(&rtwdev->mutex);
243 __rtw_leave_lps_deep(rtwdev);
244 __rtw_leave_lps(rtwdev);
247 void rtw_leave_lps_deep(struct rtw_dev *rtwdev)
249 lockdep_assert_held(&rtwdev->mutex);
251 __rtw_leave_lps_deep(rtwdev);