1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2009-2014  Realtek Corporation.*/
3 
4 #include "../wifi.h"
5 #include "../pci.h"
6 #include "reg.h"
7 #include "led.h"
8 
rtl8723be_sw_led_on(struct ieee80211_hw * hw,enum rtl_led_pin pin)9 void rtl8723be_sw_led_on(struct ieee80211_hw *hw, enum rtl_led_pin pin)
10 {
11 	u8 ledcfg;
12 	struct rtl_priv *rtlpriv = rtl_priv(hw);
13 
14 	rtl_dbg(rtlpriv, COMP_LED, DBG_LOUD,
15 		"LedAddr:%X ledpin=%d\n", REG_LEDCFG2, pin);
16 
17 	switch (pin) {
18 	case LED_PIN_GPIO0:
19 		break;
20 	case LED_PIN_LED0:
21 		ledcfg = rtl_read_byte(rtlpriv, REG_LEDCFG2);
22 		ledcfg &= ~BIT(6);
23 		rtl_write_byte(rtlpriv, REG_LEDCFG2, (ledcfg & 0xf0) | BIT(5));
24 		break;
25 	case LED_PIN_LED1:
26 		ledcfg = rtl_read_byte(rtlpriv, REG_LEDCFG1);
27 		rtl_write_byte(rtlpriv, REG_LEDCFG1, ledcfg & 0x10);
28 		break;
29 	default:
30 		pr_err("switch case %#x not processed\n", pin);
31 		break;
32 	}
33 }
34 
rtl8723be_sw_led_off(struct ieee80211_hw * hw,enum rtl_led_pin pin)35 void rtl8723be_sw_led_off(struct ieee80211_hw *hw, enum rtl_led_pin pin)
36 {
37 	struct rtl_priv *rtlpriv = rtl_priv(hw);
38 	u8 ledcfg;
39 
40 	rtl_dbg(rtlpriv, COMP_LED, DBG_LOUD,
41 		"LedAddr:%X ledpin=%d\n", REG_LEDCFG2, pin);
42 
43 	ledcfg = rtl_read_byte(rtlpriv, REG_LEDCFG2);
44 
45 	switch (pin) {
46 	case LED_PIN_GPIO0:
47 		break;
48 	case LED_PIN_LED0:
49 		ledcfg &= 0xf0;
50 		if (rtlpriv->ledctl.led_opendrain) {
51 			ledcfg &= 0x90; /* Set to software control. */
52 			rtl_write_byte(rtlpriv, REG_LEDCFG2, (ledcfg|BIT(3)));
53 			ledcfg = rtl_read_byte(rtlpriv, REG_MAC_PINMUX_CFG);
54 			ledcfg &= 0xFE;
55 			rtl_write_byte(rtlpriv, REG_MAC_PINMUX_CFG, ledcfg);
56 		} else {
57 			ledcfg &= ~BIT(6);
58 			rtl_write_byte(rtlpriv, REG_LEDCFG2,
59 				       (ledcfg | BIT(3) | BIT(5)));
60 		}
61 		break;
62 	case LED_PIN_LED1:
63 		ledcfg = rtl_read_byte(rtlpriv, REG_LEDCFG1);
64 		ledcfg &= 0x10; /* Set to software control. */
65 		rtl_write_byte(rtlpriv, REG_LEDCFG1, ledcfg|BIT(3));
66 
67 		break;
68 	default:
69 		pr_err("switch case %#x not processed\n", pin);
70 		break;
71 	}
72 }
73 
_rtl8723be_sw_led_control(struct ieee80211_hw * hw,enum led_ctl_mode ledaction)74 static void _rtl8723be_sw_led_control(struct ieee80211_hw *hw,
75 				      enum led_ctl_mode ledaction)
76 {
77 	struct rtl_priv *rtlpriv = rtl_priv(hw);
78 	enum rtl_led_pin pin0 = rtlpriv->ledctl.sw_led0;
79 
80 	switch (ledaction) {
81 	case LED_CTL_POWER_ON:
82 	case LED_CTL_LINK:
83 	case LED_CTL_NO_LINK:
84 		rtl8723be_sw_led_on(hw, pin0);
85 		break;
86 	case LED_CTL_POWER_OFF:
87 		rtl8723be_sw_led_off(hw, pin0);
88 		break;
89 	default:
90 		break;
91 	}
92 }
93 
rtl8723be_led_control(struct ieee80211_hw * hw,enum led_ctl_mode ledaction)94 void rtl8723be_led_control(struct ieee80211_hw *hw,
95 			   enum led_ctl_mode ledaction)
96 {
97 	struct rtl_priv *rtlpriv = rtl_priv(hw);
98 	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
99 
100 	if ((ppsc->rfoff_reason > RF_CHANGE_BY_PS) &&
101 	    (ledaction == LED_CTL_TX ||
102 	     ledaction == LED_CTL_RX ||
103 	     ledaction == LED_CTL_SITE_SURVEY ||
104 	     ledaction == LED_CTL_LINK ||
105 	     ledaction == LED_CTL_NO_LINK ||
106 	     ledaction == LED_CTL_START_TO_LINK ||
107 	     ledaction == LED_CTL_POWER_ON)) {
108 		return;
109 	}
110 	rtl_dbg(rtlpriv, COMP_LED, DBG_LOUD, "ledaction %d,\n", ledaction);
111 	_rtl8723be_sw_led_control(hw, ledaction);
112 }
113