18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/* Copyright(c) 2009-2010  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 _rtl8821ae_init_led(struct ieee80211_hw *hw,
108c2ecf20Sopenharmony_ci				struct rtl_led *pled,
118c2ecf20Sopenharmony_ci				enum rtl_led_pin ledpin)
128c2ecf20Sopenharmony_ci{
138c2ecf20Sopenharmony_ci	pled->hw = hw;
148c2ecf20Sopenharmony_ci	pled->ledpin = ledpin;
158c2ecf20Sopenharmony_ci	pled->ledon = false;
168c2ecf20Sopenharmony_ci}
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_civoid rtl8821ae_sw_led_on(struct ieee80211_hw *hw, struct rtl_led *pled)
198c2ecf20Sopenharmony_ci{
208c2ecf20Sopenharmony_ci	u8 ledcfg;
218c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_LED, DBG_LOUD,
248c2ecf20Sopenharmony_ci		"LedAddr:%X ledpin=%d\n", REG_LEDCFG2, pled->ledpin);
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci	switch (pled->ledpin) {
278c2ecf20Sopenharmony_ci	case LED_PIN_GPIO0:
288c2ecf20Sopenharmony_ci		break;
298c2ecf20Sopenharmony_ci	case LED_PIN_LED0:
308c2ecf20Sopenharmony_ci		ledcfg = rtl_read_byte(rtlpriv, REG_LEDCFG2);
318c2ecf20Sopenharmony_ci		ledcfg &= ~BIT(6);
328c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv,
338c2ecf20Sopenharmony_ci			       REG_LEDCFG2, (ledcfg & 0xf0) | BIT(5));
348c2ecf20Sopenharmony_ci		break;
358c2ecf20Sopenharmony_ci	case LED_PIN_LED1:
368c2ecf20Sopenharmony_ci		ledcfg = rtl_read_byte(rtlpriv, REG_LEDCFG1);
378c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, REG_LEDCFG1, ledcfg & 0x10);
388c2ecf20Sopenharmony_ci		break;
398c2ecf20Sopenharmony_ci	default:
408c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_ERR, DBG_LOUD,
418c2ecf20Sopenharmony_ci			"switch case %#x not processed\n", pled->ledpin);
428c2ecf20Sopenharmony_ci		break;
438c2ecf20Sopenharmony_ci	}
448c2ecf20Sopenharmony_ci	pled->ledon = true;
458c2ecf20Sopenharmony_ci}
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_civoid rtl8812ae_sw_led_on(struct ieee80211_hw *hw, struct rtl_led *pled)
488c2ecf20Sopenharmony_ci{
498c2ecf20Sopenharmony_ci	u16	ledreg = REG_LEDCFG1;
508c2ecf20Sopenharmony_ci	u8	ledcfg = 0;
518c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	switch (pled->ledpin) {
548c2ecf20Sopenharmony_ci	case LED_PIN_LED0:
558c2ecf20Sopenharmony_ci		ledreg = REG_LEDCFG1;
568c2ecf20Sopenharmony_ci		break;
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	case LED_PIN_LED1:
598c2ecf20Sopenharmony_ci		ledreg = REG_LEDCFG2;
608c2ecf20Sopenharmony_ci		break;
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	case LED_PIN_GPIO0:
638c2ecf20Sopenharmony_ci	default:
648c2ecf20Sopenharmony_ci		break;
658c2ecf20Sopenharmony_ci	}
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_LED, DBG_LOUD,
688c2ecf20Sopenharmony_ci		"In SwLedOn, LedAddr:%X LEDPIN=%d\n",
698c2ecf20Sopenharmony_ci		ledreg, pled->ledpin);
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	ledcfg =  rtl_read_byte(rtlpriv, ledreg);
728c2ecf20Sopenharmony_ci	ledcfg |= BIT(5); /*Set 0x4c[21]*/
738c2ecf20Sopenharmony_ci	ledcfg &= ~(BIT(7) | BIT(6) | BIT(3) | BIT(2) | BIT(1) | BIT(0));
748c2ecf20Sopenharmony_ci		/*Clear 0x4c[23:22] and 0x4c[19:16]*/
758c2ecf20Sopenharmony_ci	rtl_write_byte(rtlpriv, ledreg, ledcfg); /*SW control led0 on.*/
768c2ecf20Sopenharmony_ci	pled->ledon = true;
778c2ecf20Sopenharmony_ci}
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_civoid rtl8821ae_sw_led_off(struct ieee80211_hw *hw, struct rtl_led *pled)
808c2ecf20Sopenharmony_ci{
818c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
828c2ecf20Sopenharmony_ci	u8 ledcfg;
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_LED, DBG_LOUD,
858c2ecf20Sopenharmony_ci		"LedAddr:%X ledpin=%d\n", REG_LEDCFG2, pled->ledpin);
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	ledcfg = rtl_read_byte(rtlpriv, REG_LEDCFG2);
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	switch (pled->ledpin) {
908c2ecf20Sopenharmony_ci	case LED_PIN_GPIO0:
918c2ecf20Sopenharmony_ci		break;
928c2ecf20Sopenharmony_ci	case LED_PIN_LED0:
938c2ecf20Sopenharmony_ci		ledcfg &= 0xf0;
948c2ecf20Sopenharmony_ci		if (rtlpriv->ledctl.led_opendrain) {
958c2ecf20Sopenharmony_ci			ledcfg &= 0x90; /* Set to software control. */
968c2ecf20Sopenharmony_ci			rtl_write_byte(rtlpriv, REG_LEDCFG2, (ledcfg|BIT(3)));
978c2ecf20Sopenharmony_ci			ledcfg = rtl_read_byte(rtlpriv, REG_MAC_PINMUX_CFG);
988c2ecf20Sopenharmony_ci			ledcfg &= 0xFE;
998c2ecf20Sopenharmony_ci			rtl_write_byte(rtlpriv, REG_MAC_PINMUX_CFG, ledcfg);
1008c2ecf20Sopenharmony_ci		} else {
1018c2ecf20Sopenharmony_ci			ledcfg &= ~BIT(6);
1028c2ecf20Sopenharmony_ci			rtl_write_byte(rtlpriv, REG_LEDCFG2,
1038c2ecf20Sopenharmony_ci				       (ledcfg | BIT(3) | BIT(5)));
1048c2ecf20Sopenharmony_ci		}
1058c2ecf20Sopenharmony_ci		break;
1068c2ecf20Sopenharmony_ci	case LED_PIN_LED1:
1078c2ecf20Sopenharmony_ci		ledcfg = rtl_read_byte(rtlpriv, REG_LEDCFG1);
1088c2ecf20Sopenharmony_ci		ledcfg &= 0x10; /* Set to software control. */
1098c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, REG_LEDCFG1, ledcfg|BIT(3));
1108c2ecf20Sopenharmony_ci		break;
1118c2ecf20Sopenharmony_ci	default:
1128c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_ERR, DBG_LOUD,
1138c2ecf20Sopenharmony_ci			"switch case %#x not processed\n", pled->ledpin);
1148c2ecf20Sopenharmony_ci		break;
1158c2ecf20Sopenharmony_ci	}
1168c2ecf20Sopenharmony_ci	pled->ledon = false;
1178c2ecf20Sopenharmony_ci}
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_civoid rtl8812ae_sw_led_off(struct ieee80211_hw *hw, struct rtl_led *pled)
1208c2ecf20Sopenharmony_ci{
1218c2ecf20Sopenharmony_ci	u16 ledreg = REG_LEDCFG1;
1228c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	switch (pled->ledpin) {
1258c2ecf20Sopenharmony_ci	case LED_PIN_LED0:
1268c2ecf20Sopenharmony_ci		ledreg = REG_LEDCFG1;
1278c2ecf20Sopenharmony_ci		break;
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	case LED_PIN_LED1:
1308c2ecf20Sopenharmony_ci		ledreg = REG_LEDCFG2;
1318c2ecf20Sopenharmony_ci		break;
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	case LED_PIN_GPIO0:
1348c2ecf20Sopenharmony_ci	default:
1358c2ecf20Sopenharmony_ci		break;
1368c2ecf20Sopenharmony_ci	}
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_LED, DBG_LOUD,
1398c2ecf20Sopenharmony_ci		"In SwLedOff,LedAddr:%X LEDPIN=%d\n",
1408c2ecf20Sopenharmony_ci		ledreg, pled->ledpin);
1418c2ecf20Sopenharmony_ci	/*Open-drain arrangement for controlling the LED*/
1428c2ecf20Sopenharmony_ci	if (rtlpriv->ledctl.led_opendrain) {
1438c2ecf20Sopenharmony_ci		u8 ledcfg = rtl_read_byte(rtlpriv, ledreg);
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci		ledreg &= 0xd0; /* Set to software control.*/
1468c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, ledreg, (ledcfg | BIT(3)));
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci		/*Open-drain arrangement*/
1498c2ecf20Sopenharmony_ci		ledcfg = rtl_read_byte(rtlpriv, REG_MAC_PINMUX_CFG);
1508c2ecf20Sopenharmony_ci		ledcfg &= 0xFE;/*Set GPIO[8] to input mode*/
1518c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, REG_MAC_PINMUX_CFG, ledcfg);
1528c2ecf20Sopenharmony_ci	} else {
1538c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, ledreg, 0x28);
1548c2ecf20Sopenharmony_ci	}
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	pled->ledon = false;
1578c2ecf20Sopenharmony_ci}
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_civoid rtl8821ae_init_sw_leds(struct ieee80211_hw *hw)
1608c2ecf20Sopenharmony_ci{
1618c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	_rtl8821ae_init_led(hw, &rtlpriv->ledctl.sw_led0, LED_PIN_LED0);
1648c2ecf20Sopenharmony_ci	_rtl8821ae_init_led(hw, &rtlpriv->ledctl.sw_led1, LED_PIN_LED1);
1658c2ecf20Sopenharmony_ci}
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_cistatic void _rtl8821ae_sw_led_control(struct ieee80211_hw *hw,
1688c2ecf20Sopenharmony_ci				      enum led_ctl_mode ledaction)
1698c2ecf20Sopenharmony_ci{
1708c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
1718c2ecf20Sopenharmony_ci	struct rtl_led *pled0 = &rtlpriv->ledctl.sw_led0;
1728c2ecf20Sopenharmony_ci	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	switch (ledaction) {
1758c2ecf20Sopenharmony_ci	case LED_CTL_POWER_ON:
1768c2ecf20Sopenharmony_ci	case LED_CTL_LINK:
1778c2ecf20Sopenharmony_ci	case LED_CTL_NO_LINK:
1788c2ecf20Sopenharmony_ci		if (rtlhal->hw_type == HARDWARE_TYPE_RTL8812AE)
1798c2ecf20Sopenharmony_ci			rtl8812ae_sw_led_on(hw, pled0);
1808c2ecf20Sopenharmony_ci		else
1818c2ecf20Sopenharmony_ci			rtl8821ae_sw_led_on(hw, pled0);
1828c2ecf20Sopenharmony_ci		break;
1838c2ecf20Sopenharmony_ci	case LED_CTL_POWER_OFF:
1848c2ecf20Sopenharmony_ci		if (rtlhal->hw_type == HARDWARE_TYPE_RTL8812AE)
1858c2ecf20Sopenharmony_ci			rtl8812ae_sw_led_off(hw, pled0);
1868c2ecf20Sopenharmony_ci		else
1878c2ecf20Sopenharmony_ci			rtl8821ae_sw_led_off(hw, pled0);
1888c2ecf20Sopenharmony_ci		break;
1898c2ecf20Sopenharmony_ci	default:
1908c2ecf20Sopenharmony_ci		break;
1918c2ecf20Sopenharmony_ci	}
1928c2ecf20Sopenharmony_ci}
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_civoid rtl8821ae_led_control(struct ieee80211_hw *hw,
1958c2ecf20Sopenharmony_ci			   enum led_ctl_mode ledaction)
1968c2ecf20Sopenharmony_ci{
1978c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
1988c2ecf20Sopenharmony_ci	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	if ((ppsc->rfoff_reason > RF_CHANGE_BY_PS) &&
2018c2ecf20Sopenharmony_ci	    (ledaction == LED_CTL_TX ||
2028c2ecf20Sopenharmony_ci	     ledaction == LED_CTL_RX ||
2038c2ecf20Sopenharmony_ci	     ledaction == LED_CTL_SITE_SURVEY ||
2048c2ecf20Sopenharmony_ci	     ledaction == LED_CTL_LINK ||
2058c2ecf20Sopenharmony_ci	     ledaction == LED_CTL_NO_LINK ||
2068c2ecf20Sopenharmony_ci	     ledaction == LED_CTL_START_TO_LINK ||
2078c2ecf20Sopenharmony_ci	     ledaction == LED_CTL_POWER_ON)) {
2088c2ecf20Sopenharmony_ci		return;
2098c2ecf20Sopenharmony_ci	}
2108c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_LED, DBG_LOUD, "ledaction %d,\n",
2118c2ecf20Sopenharmony_ci		ledaction);
2128c2ecf20Sopenharmony_ci	_rtl8821ae_sw_led_control(hw, ledaction);
2138c2ecf20Sopenharmony_ci}
214