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 
9 static void _rtl8723be_init_led(struct ieee80211_hw *hw,  struct rtl_led *pled,
10 				enum rtl_led_pin ledpin)
11 {
12 	pled->hw = hw;
13 	pled->ledpin = ledpin;
14 	pled->ledon = false;
15 }
16 
17 void rtl8723be_sw_led_on(struct ieee80211_hw *hw, struct rtl_led *pled)
18 {
19 	u8 ledcfg;
20 	struct rtl_priv *rtlpriv = rtl_priv(hw);
21 
22 	rtl_dbg(rtlpriv, COMP_LED, DBG_LOUD,
23 		"LedAddr:%X ledpin=%d\n", REG_LEDCFG2, pled->ledpin);
24 
25 	switch (pled->ledpin) {
26 	case LED_PIN_GPIO0:
27 		break;
28 	case LED_PIN_LED0:
29 		ledcfg = rtl_read_byte(rtlpriv, REG_LEDCFG2);
30 		ledcfg &= ~BIT(6);
31 		rtl_write_byte(rtlpriv, REG_LEDCFG2, (ledcfg & 0xf0) | BIT(5));
32 		break;
33 	case LED_PIN_LED1:
34 		ledcfg = rtl_read_byte(rtlpriv, REG_LEDCFG1);
35 		rtl_write_byte(rtlpriv, REG_LEDCFG1, ledcfg & 0x10);
36 		break;
37 	default:
38 		pr_err("switch case %#x not processed\n",
39 		       pled->ledpin);
40 		break;
41 	}
42 	pled->ledon = true;
43 }
44 
45 void rtl8723be_sw_led_off(struct ieee80211_hw *hw, struct rtl_led *pled)
46 {
47 	struct rtl_priv *rtlpriv = rtl_priv(hw);
48 	u8 ledcfg;
49 
50 	rtl_dbg(rtlpriv, COMP_LED, DBG_LOUD,
51 		"LedAddr:%X ledpin=%d\n", REG_LEDCFG2, pled->ledpin);
52 
53 	ledcfg = rtl_read_byte(rtlpriv, REG_LEDCFG2);
54 
55 	switch (pled->ledpin) {
56 	case LED_PIN_GPIO0:
57 		break;
58 	case LED_PIN_LED0:
59 		ledcfg &= 0xf0;
60 		if (rtlpriv->ledctl.led_opendrain) {
61 			ledcfg &= 0x90; /* Set to software control. */
62 			rtl_write_byte(rtlpriv, REG_LEDCFG2, (ledcfg|BIT(3)));
63 			ledcfg = rtl_read_byte(rtlpriv, REG_MAC_PINMUX_CFG);
64 			ledcfg &= 0xFE;
65 			rtl_write_byte(rtlpriv, REG_MAC_PINMUX_CFG, ledcfg);
66 		} else {
67 			ledcfg &= ~BIT(6);
68 			rtl_write_byte(rtlpriv, REG_LEDCFG2,
69 				       (ledcfg | BIT(3) | BIT(5)));
70 		}
71 		break;
72 	case LED_PIN_LED1:
73 		ledcfg = rtl_read_byte(rtlpriv, REG_LEDCFG1);
74 		ledcfg &= 0x10; /* Set to software control. */
75 		rtl_write_byte(rtlpriv, REG_LEDCFG1, ledcfg|BIT(3));
76 
77 		break;
78 	default:
79 		pr_err("switch case %#x not processed\n",
80 		       pled->ledpin);
81 		break;
82 	}
83 	pled->ledon = false;
84 }
85 
86 void rtl8723be_init_sw_leds(struct ieee80211_hw *hw)
87 {
88 	struct rtl_priv *rtlpriv = rtl_priv(hw);
89 
90 	_rtl8723be_init_led(hw, &rtlpriv->ledctl.sw_led0, LED_PIN_LED0);
91 	_rtl8723be_init_led(hw, &rtlpriv->ledctl.sw_led1, LED_PIN_LED1);
92 }
93 
94 static void _rtl8723be_sw_led_control(struct ieee80211_hw *hw,
95 				      enum led_ctl_mode ledaction)
96 {
97 	struct rtl_priv *rtlpriv = rtl_priv(hw);
98 	struct rtl_led *pled0 = &rtlpriv->ledctl.sw_led0;
99 
100 	switch (ledaction) {
101 	case LED_CTL_POWER_ON:
102 	case LED_CTL_LINK:
103 	case LED_CTL_NO_LINK:
104 		rtl8723be_sw_led_on(hw, pled0);
105 		break;
106 	case LED_CTL_POWER_OFF:
107 		rtl8723be_sw_led_off(hw, pled0);
108 		break;
109 	default:
110 		break;
111 	}
112 }
113 
114 void rtl8723be_led_control(struct ieee80211_hw *hw,
115 			   enum led_ctl_mode ledaction)
116 {
117 	struct rtl_priv *rtlpriv = rtl_priv(hw);
118 	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
119 
120 	if ((ppsc->rfoff_reason > RF_CHANGE_BY_PS) &&
121 	    (ledaction == LED_CTL_TX ||
122 	     ledaction == LED_CTL_RX ||
123 	     ledaction == LED_CTL_SITE_SURVEY ||
124 	     ledaction == LED_CTL_LINK ||
125 	     ledaction == LED_CTL_NO_LINK ||
126 	     ledaction == LED_CTL_START_TO_LINK ||
127 	     ledaction == LED_CTL_POWER_ON)) {
128 		return;
129 	}
130 	rtl_dbg(rtlpriv, COMP_LED, DBG_LOUD, "ledaction %d,\n", ledaction);
131 	_rtl8723be_sw_led_control(hw, ledaction);
132 }
133