18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/* Copyright(c) 2009-2013  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 _rtl88ee_init_led(struct ieee80211_hw *hw,
108c2ecf20Sopenharmony_ci			      struct rtl_led *pled, 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 rtl88ee_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		rtl_write_byte(rtlpriv,
318c2ecf20Sopenharmony_ci			       REG_LEDCFG2, (ledcfg & 0xf0) | BIT(5) | BIT(6));
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		rtl_dbg(rtlpriv, COMP_ERR, DBG_LOUD,
398c2ecf20Sopenharmony_ci			"switch case %#x not processed\n", pled->ledpin);
408c2ecf20Sopenharmony_ci		break;
418c2ecf20Sopenharmony_ci	}
428c2ecf20Sopenharmony_ci	pled->ledon = true;
438c2ecf20Sopenharmony_ci}
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_civoid rtl88ee_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	switch (pled->ledpin) {
548c2ecf20Sopenharmony_ci	case LED_PIN_GPIO0:
558c2ecf20Sopenharmony_ci		break;
568c2ecf20Sopenharmony_ci	case LED_PIN_LED0:
578c2ecf20Sopenharmony_ci		ledcfg = rtl_read_byte(rtlpriv, REG_LEDCFG2);
588c2ecf20Sopenharmony_ci		ledcfg &= 0xf0;
598c2ecf20Sopenharmony_ci		if (rtlpriv->ledctl.led_opendrain) {
608c2ecf20Sopenharmony_ci			rtl_write_byte(rtlpriv, REG_LEDCFG2,
618c2ecf20Sopenharmony_ci				       (ledcfg | BIT(3) | BIT(5) | BIT(6)));
628c2ecf20Sopenharmony_ci			ledcfg = rtl_read_byte(rtlpriv, REG_MAC_PINMUX_CFG);
638c2ecf20Sopenharmony_ci			rtl_write_byte(rtlpriv, REG_MAC_PINMUX_CFG,
648c2ecf20Sopenharmony_ci				       (ledcfg & 0xFE));
658c2ecf20Sopenharmony_ci		} else
668c2ecf20Sopenharmony_ci			rtl_write_byte(rtlpriv, REG_LEDCFG2,
678c2ecf20Sopenharmony_ci				       (ledcfg | BIT(3) | BIT(5) | BIT(6)));
688c2ecf20Sopenharmony_ci		break;
698c2ecf20Sopenharmony_ci	case LED_PIN_LED1:
708c2ecf20Sopenharmony_ci		ledcfg = rtl_read_byte(rtlpriv, REG_LEDCFG1);
718c2ecf20Sopenharmony_ci		ledcfg &= 0x10;
728c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, REG_LEDCFG1, (ledcfg | BIT(3)));
738c2ecf20Sopenharmony_ci		break;
748c2ecf20Sopenharmony_ci	default:
758c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_ERR, DBG_LOUD,
768c2ecf20Sopenharmony_ci			"switch case %#x not processed\n", pled->ledpin);
778c2ecf20Sopenharmony_ci		break;
788c2ecf20Sopenharmony_ci	}
798c2ecf20Sopenharmony_ci	pled->ledon = false;
808c2ecf20Sopenharmony_ci}
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_civoid rtl88ee_init_sw_leds(struct ieee80211_hw *hw)
838c2ecf20Sopenharmony_ci{
848c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	_rtl88ee_init_led(hw, &rtlpriv->ledctl.sw_led0, LED_PIN_LED0);
878c2ecf20Sopenharmony_ci	_rtl88ee_init_led(hw, &rtlpriv->ledctl.sw_led1, LED_PIN_LED1);
888c2ecf20Sopenharmony_ci}
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_cistatic void _rtl88ee_sw_led_control(struct ieee80211_hw *hw,
918c2ecf20Sopenharmony_ci				    enum led_ctl_mode ledaction)
928c2ecf20Sopenharmony_ci{
938c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
948c2ecf20Sopenharmony_ci	struct rtl_led *pled0 = &rtlpriv->ledctl.sw_led0;
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	switch (ledaction) {
978c2ecf20Sopenharmony_ci	case LED_CTL_POWER_ON:
988c2ecf20Sopenharmony_ci	case LED_CTL_LINK:
998c2ecf20Sopenharmony_ci	case LED_CTL_NO_LINK:
1008c2ecf20Sopenharmony_ci		rtl88ee_sw_led_on(hw, pled0);
1018c2ecf20Sopenharmony_ci		break;
1028c2ecf20Sopenharmony_ci	case LED_CTL_POWER_OFF:
1038c2ecf20Sopenharmony_ci		rtl88ee_sw_led_off(hw, pled0);
1048c2ecf20Sopenharmony_ci		break;
1058c2ecf20Sopenharmony_ci	default:
1068c2ecf20Sopenharmony_ci		break;
1078c2ecf20Sopenharmony_ci	}
1088c2ecf20Sopenharmony_ci}
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_civoid rtl88ee_led_control(struct ieee80211_hw *hw,
1118c2ecf20Sopenharmony_ci			enum led_ctl_mode ledaction)
1128c2ecf20Sopenharmony_ci{
1138c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
1148c2ecf20Sopenharmony_ci	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	if ((ppsc->rfoff_reason > RF_CHANGE_BY_PS) &&
1178c2ecf20Sopenharmony_ci	    (ledaction == LED_CTL_TX ||
1188c2ecf20Sopenharmony_ci	     ledaction == LED_CTL_RX ||
1198c2ecf20Sopenharmony_ci	     ledaction == LED_CTL_SITE_SURVEY ||
1208c2ecf20Sopenharmony_ci	     ledaction == LED_CTL_LINK ||
1218c2ecf20Sopenharmony_ci	     ledaction == LED_CTL_NO_LINK ||
1228c2ecf20Sopenharmony_ci	     ledaction == LED_CTL_START_TO_LINK ||
1238c2ecf20Sopenharmony_ci	     ledaction == LED_CTL_POWER_ON)) {
1248c2ecf20Sopenharmony_ci		return;
1258c2ecf20Sopenharmony_ci	}
1268c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_LED, DBG_TRACE, "ledaction %d,\n",
1278c2ecf20Sopenharmony_ci		ledaction);
1288c2ecf20Sopenharmony_ci	_rtl88ee_sw_led_control(hw, ledaction);
1298c2ecf20Sopenharmony_ci}
130