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 rtl92de_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 		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 
23 		if ((rtlpriv->efuse.eeprom_did == 0x8176) ||
24 			(rtlpriv->efuse.eeprom_did == 0x8193))
25 			/* BIT7 of REG_LEDCFG2 should be set to
26 			 * make sure we could emit the led2. */
27 			rtl_write_byte(rtlpriv, REG_LEDCFG2, (ledcfg & 0xf0) |
28 				       BIT(7) | BIT(5) | BIT(6));
29 		else
30 			rtl_write_byte(rtlpriv, REG_LEDCFG2, (ledcfg & 0xf0) |
31 				       BIT(7) | BIT(5));
32 		break;
33 	case LED_PIN_LED1:
34 		ledcfg = rtl_read_byte(rtlpriv, REG_LEDCFG1);
35 
36 		rtl_write_byte(rtlpriv, REG_LEDCFG2, (ledcfg & 0x0f) | BIT(5));
37 		break;
38 	default:
39 		pr_err("switch case %#x not processed\n", pin);
40 		break;
41 	}
42 }
43 
44 void rtl92de_sw_led_off(struct ieee80211_hw *hw, enum rtl_led_pin pin)
45 {
46 	struct rtl_priv *rtlpriv = rtl_priv(hw);
47 	u8 ledcfg;
48 
49 	rtl_dbg(rtlpriv, COMP_LED, DBG_LOUD, "LedAddr:%X ledpin=%d\n",
50 		REG_LEDCFG2, pin);
51 
52 	ledcfg = rtl_read_byte(rtlpriv, REG_LEDCFG2);
53 
54 	switch (pin) {
55 	case LED_PIN_GPIO0:
56 		break;
57 	case LED_PIN_LED0:
58 		ledcfg &= 0xf0;
59 		if (rtlpriv->ledctl.led_opendrain)
60 			rtl_write_byte(rtlpriv, REG_LEDCFG2,
61 				       (ledcfg | BIT(1) | BIT(5) | BIT(6)));
62 		else
63 			rtl_write_byte(rtlpriv, REG_LEDCFG2,
64 				       (ledcfg | BIT(3) | BIT(5) | BIT(6)));
65 		break;
66 	case LED_PIN_LED1:
67 		ledcfg &= 0x0f;
68 		rtl_write_byte(rtlpriv, REG_LEDCFG2, (ledcfg | BIT(3)));
69 		break;
70 	default:
71 		pr_err("switch case %#x not processed\n", pin);
72 		break;
73 	}
74 }
75 
76 static void _rtl92ce_sw_led_control(struct ieee80211_hw *hw,
77 				    enum led_ctl_mode ledaction)
78 {
79 	struct rtl_priv *rtlpriv = rtl_priv(hw);
80 	enum rtl_led_pin pin0 = rtlpriv->ledctl.sw_led0;
81 
82 	switch (ledaction) {
83 	case LED_CTL_POWER_ON:
84 	case LED_CTL_LINK:
85 	case LED_CTL_NO_LINK:
86 		rtl92de_sw_led_on(hw, pin0);
87 		break;
88 	case LED_CTL_POWER_OFF:
89 		rtl92de_sw_led_off(hw, pin0);
90 		break;
91 	default:
92 		break;
93 	}
94 }
95 
96 void rtl92de_led_control(struct ieee80211_hw *hw, enum led_ctl_mode ledaction)
97 {
98 	struct rtl_priv *rtlpriv = rtl_priv(hw);
99 	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
100 
101 	if ((ppsc->rfoff_reason > RF_CHANGE_BY_PS) &&
102 	    (ledaction == LED_CTL_TX ||
103 	     ledaction == LED_CTL_RX ||
104 	     ledaction == LED_CTL_SITE_SURVEY ||
105 	     ledaction == LED_CTL_LINK ||
106 	     ledaction == LED_CTL_NO_LINK ||
107 	     ledaction == LED_CTL_START_TO_LINK ||
108 	     ledaction == LED_CTL_POWER_ON)) {
109 		return;
110 	}
111 	rtl_dbg(rtlpriv, COMP_LED, DBG_LOUD, "ledaction %d,\n", ledaction);
112 
113 	_rtl92ce_sw_led_control(hw, ledaction);
114 }
115