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