18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/* Copyright(c) 2009-2014  Realtek Corporation.*/
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci#include "../wifi.h"
58c2ecf20Sopenharmony_ci#include "../pci.h"
68c2ecf20Sopenharmony_ci#include "reg.h"
78c2ecf20Sopenharmony_ci#include "led.h"
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_cistatic void _rtl8723be_init_led(struct ieee80211_hw *hw,  struct rtl_led *pled,
108c2ecf20Sopenharmony_ci				enum rtl_led_pin ledpin)
118c2ecf20Sopenharmony_ci{
128c2ecf20Sopenharmony_ci	pled->hw = hw;
138c2ecf20Sopenharmony_ci	pled->ledpin = ledpin;
148c2ecf20Sopenharmony_ci	pled->ledon = false;
158c2ecf20Sopenharmony_ci}
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_civoid rtl8723be_sw_led_on(struct ieee80211_hw *hw, struct rtl_led *pled)
188c2ecf20Sopenharmony_ci{
198c2ecf20Sopenharmony_ci	u8 ledcfg;
208c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_LED, DBG_LOUD,
238c2ecf20Sopenharmony_ci		"LedAddr:%X ledpin=%d\n", REG_LEDCFG2, pled->ledpin);
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci	switch (pled->ledpin) {
268c2ecf20Sopenharmony_ci	case LED_PIN_GPIO0:
278c2ecf20Sopenharmony_ci		break;
288c2ecf20Sopenharmony_ci	case LED_PIN_LED0:
298c2ecf20Sopenharmony_ci		ledcfg = rtl_read_byte(rtlpriv, REG_LEDCFG2);
308c2ecf20Sopenharmony_ci		ledcfg &= ~BIT(6);
318c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, REG_LEDCFG2, (ledcfg & 0xf0) | BIT(5));
328c2ecf20Sopenharmony_ci		break;
338c2ecf20Sopenharmony_ci	case LED_PIN_LED1:
348c2ecf20Sopenharmony_ci		ledcfg = rtl_read_byte(rtlpriv, REG_LEDCFG1);
358c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, REG_LEDCFG1, ledcfg & 0x10);
368c2ecf20Sopenharmony_ci		break;
378c2ecf20Sopenharmony_ci	default:
388c2ecf20Sopenharmony_ci		pr_err("switch case %#x not processed\n",
398c2ecf20Sopenharmony_ci		       pled->ledpin);
408c2ecf20Sopenharmony_ci		break;
418c2ecf20Sopenharmony_ci	}
428c2ecf20Sopenharmony_ci	pled->ledon = true;
438c2ecf20Sopenharmony_ci}
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_civoid rtl8723be_sw_led_off(struct ieee80211_hw *hw, struct rtl_led *pled)
468c2ecf20Sopenharmony_ci{
478c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
488c2ecf20Sopenharmony_ci	u8 ledcfg;
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_LED, DBG_LOUD,
518c2ecf20Sopenharmony_ci		"LedAddr:%X ledpin=%d\n", REG_LEDCFG2, pled->ledpin);
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	ledcfg = rtl_read_byte(rtlpriv, REG_LEDCFG2);
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	switch (pled->ledpin) {
568c2ecf20Sopenharmony_ci	case LED_PIN_GPIO0:
578c2ecf20Sopenharmony_ci		break;
588c2ecf20Sopenharmony_ci	case LED_PIN_LED0:
598c2ecf20Sopenharmony_ci		ledcfg &= 0xf0;
608c2ecf20Sopenharmony_ci		if (rtlpriv->ledctl.led_opendrain) {
618c2ecf20Sopenharmony_ci			ledcfg &= 0x90; /* Set to software control. */
628c2ecf20Sopenharmony_ci			rtl_write_byte(rtlpriv, REG_LEDCFG2, (ledcfg|BIT(3)));
638c2ecf20Sopenharmony_ci			ledcfg = rtl_read_byte(rtlpriv, REG_MAC_PINMUX_CFG);
648c2ecf20Sopenharmony_ci			ledcfg &= 0xFE;
658c2ecf20Sopenharmony_ci			rtl_write_byte(rtlpriv, REG_MAC_PINMUX_CFG, ledcfg);
668c2ecf20Sopenharmony_ci		} else {
678c2ecf20Sopenharmony_ci			ledcfg &= ~BIT(6);
688c2ecf20Sopenharmony_ci			rtl_write_byte(rtlpriv, REG_LEDCFG2,
698c2ecf20Sopenharmony_ci				       (ledcfg | BIT(3) | BIT(5)));
708c2ecf20Sopenharmony_ci		}
718c2ecf20Sopenharmony_ci		break;
728c2ecf20Sopenharmony_ci	case LED_PIN_LED1:
738c2ecf20Sopenharmony_ci		ledcfg = rtl_read_byte(rtlpriv, REG_LEDCFG1);
748c2ecf20Sopenharmony_ci		ledcfg &= 0x10; /* Set to software control. */
758c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, REG_LEDCFG1, ledcfg|BIT(3));
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci		break;
788c2ecf20Sopenharmony_ci	default:
798c2ecf20Sopenharmony_ci		pr_err("switch case %#x not processed\n",
808c2ecf20Sopenharmony_ci		       pled->ledpin);
818c2ecf20Sopenharmony_ci		break;
828c2ecf20Sopenharmony_ci	}
838c2ecf20Sopenharmony_ci	pled->ledon = false;
848c2ecf20Sopenharmony_ci}
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_civoid rtl8723be_init_sw_leds(struct ieee80211_hw *hw)
878c2ecf20Sopenharmony_ci{
888c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	_rtl8723be_init_led(hw, &rtlpriv->ledctl.sw_led0, LED_PIN_LED0);
918c2ecf20Sopenharmony_ci	_rtl8723be_init_led(hw, &rtlpriv->ledctl.sw_led1, LED_PIN_LED1);
928c2ecf20Sopenharmony_ci}
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_cistatic void _rtl8723be_sw_led_control(struct ieee80211_hw *hw,
958c2ecf20Sopenharmony_ci				      enum led_ctl_mode ledaction)
968c2ecf20Sopenharmony_ci{
978c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
988c2ecf20Sopenharmony_ci	struct rtl_led *pled0 = &rtlpriv->ledctl.sw_led0;
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	switch (ledaction) {
1018c2ecf20Sopenharmony_ci	case LED_CTL_POWER_ON:
1028c2ecf20Sopenharmony_ci	case LED_CTL_LINK:
1038c2ecf20Sopenharmony_ci	case LED_CTL_NO_LINK:
1048c2ecf20Sopenharmony_ci		rtl8723be_sw_led_on(hw, pled0);
1058c2ecf20Sopenharmony_ci		break;
1068c2ecf20Sopenharmony_ci	case LED_CTL_POWER_OFF:
1078c2ecf20Sopenharmony_ci		rtl8723be_sw_led_off(hw, pled0);
1088c2ecf20Sopenharmony_ci		break;
1098c2ecf20Sopenharmony_ci	default:
1108c2ecf20Sopenharmony_ci		break;
1118c2ecf20Sopenharmony_ci	}
1128c2ecf20Sopenharmony_ci}
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_civoid rtl8723be_led_control(struct ieee80211_hw *hw,
1158c2ecf20Sopenharmony_ci			   enum led_ctl_mode ledaction)
1168c2ecf20Sopenharmony_ci{
1178c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
1188c2ecf20Sopenharmony_ci	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	if ((ppsc->rfoff_reason > RF_CHANGE_BY_PS) &&
1218c2ecf20Sopenharmony_ci	    (ledaction == LED_CTL_TX ||
1228c2ecf20Sopenharmony_ci	     ledaction == LED_CTL_RX ||
1238c2ecf20Sopenharmony_ci	     ledaction == LED_CTL_SITE_SURVEY ||
1248c2ecf20Sopenharmony_ci	     ledaction == LED_CTL_LINK ||
1258c2ecf20Sopenharmony_ci	     ledaction == LED_CTL_NO_LINK ||
1268c2ecf20Sopenharmony_ci	     ledaction == LED_CTL_START_TO_LINK ||
1278c2ecf20Sopenharmony_ci	     ledaction == LED_CTL_POWER_ON)) {
1288c2ecf20Sopenharmony_ci		return;
1298c2ecf20Sopenharmony_ci	}
1308c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_LED, DBG_LOUD, "ledaction %d,\n", ledaction);
1318c2ecf20Sopenharmony_ci	_rtl8723be_sw_led_control(hw, ledaction);
1328c2ecf20Sopenharmony_ci}
133