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 static void _rtl92ce_init_led(struct ieee80211_hw *hw,
10 			      struct rtl_led *pled, enum rtl_led_pin ledpin)
11 {
12 	pled->hw = hw;
13 	pled->ledpin = ledpin;
14 	pled->ledon = false;
15 }
16 
17 void rtl92ce_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 	RT_TRACE(rtlpriv, COMP_LED, DBG_LOUD, "LedAddr:%X ledpin=%d\n",
23 		 REG_LEDCFG2, pled->ledpin);
24 
25 	ledcfg = rtl_read_byte(rtlpriv, REG_LEDCFG2);
26 
27 	switch (pled->ledpin) {
28 	case LED_PIN_GPIO0:
29 		break;
30 	case LED_PIN_LED0:
31 		rtl_write_byte(rtlpriv,
32 			       REG_LEDCFG2, (ledcfg & 0xf0) | BIT(5) | BIT(6));
33 		break;
34 	case LED_PIN_LED1:
35 		rtl_write_byte(rtlpriv, REG_LEDCFG2, (ledcfg & 0x0f) | BIT(5));
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 rtl92ce_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 	RT_TRACE(rtlpriv, COMP_LED, DBG_LOUD, "LedAddr:%X ledpin=%d\n",
51 		 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 			rtl_write_byte(rtlpriv, REG_LEDCFG2,
62 				       (ledcfg | BIT(1) | BIT(5) | BIT(6)));
63 		else
64 			rtl_write_byte(rtlpriv, REG_LEDCFG2,
65 				       (ledcfg | BIT(3) | BIT(5) | BIT(6)));
66 		break;
67 	case LED_PIN_LED1:
68 		ledcfg &= 0x0f;
69 		rtl_write_byte(rtlpriv, REG_LEDCFG2, (ledcfg | BIT(3)));
70 		break;
71 	default:
72 		pr_info("switch case %#x not processed\n", pled->ledpin);
73 		break;
74 	}
75 	pled->ledon = false;
76 }
77 
78 void rtl92ce_init_sw_leds(struct ieee80211_hw *hw)
79 {
80 	struct rtl_priv *rtlpriv = rtl_priv(hw);
81 
82 	_rtl92ce_init_led(hw, &rtlpriv->ledctl.sw_led0, LED_PIN_LED0);
83 	_rtl92ce_init_led(hw, &rtlpriv->ledctl.sw_led1, LED_PIN_LED1);
84 }
85 
86 static void _rtl92ce_sw_led_control(struct ieee80211_hw *hw,
87 				    enum led_ctl_mode ledaction)
88 {
89 	struct rtl_priv *rtlpriv = rtl_priv(hw);
90 	struct rtl_led *pled0 = &rtlpriv->ledctl.sw_led0;
91 
92 	switch (ledaction) {
93 	case LED_CTL_POWER_ON:
94 	case LED_CTL_LINK:
95 	case LED_CTL_NO_LINK:
96 		rtl92ce_sw_led_on(hw, pled0);
97 		break;
98 	case LED_CTL_POWER_OFF:
99 		rtl92ce_sw_led_off(hw, pled0);
100 		break;
101 	default:
102 		break;
103 	}
104 }
105 
106 void rtl92ce_led_control(struct ieee80211_hw *hw,
107 			enum led_ctl_mode ledaction)
108 {
109 	struct rtl_priv *rtlpriv = rtl_priv(hw);
110 	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
111 
112 	if ((ppsc->rfoff_reason > RF_CHANGE_BY_PS) &&
113 	    (ledaction == LED_CTL_TX ||
114 	     ledaction == LED_CTL_RX ||
115 	     ledaction == LED_CTL_SITE_SURVEY ||
116 	     ledaction == LED_CTL_LINK ||
117 	     ledaction == LED_CTL_NO_LINK ||
118 	     ledaction == LED_CTL_START_TO_LINK ||
119 	     ledaction == LED_CTL_POWER_ON)) {
120 		return;
121 	}
122 	RT_TRACE(rtlpriv, COMP_LED, DBG_LOUD, "ledaction %d\n",
123 		 ledaction);
124 	_rtl92ce_sw_led_control(hw, ledaction);
125 }
126