xref: /linux/drivers/net/wireless/realtek/rtlwifi/rtl8192de/led.c (revision e58e871becec2d3b04ed91c0c16fe8deac9c9dfa)
1 /******************************************************************************
2  *
3  * Copyright(c) 2009-2012  Realtek Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * The full GNU General Public License is included in this distribution in the
15  * file called LICENSE.
16  *
17  * Contact Information:
18  * wlanfae <wlanfae@realtek.com>
19  * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
20  * Hsinchu 300, Taiwan.
21  *
22  * Larry Finger <Larry.Finger@lwfinger.net>
23  *
24  *****************************************************************************/
25 
26 #include "../wifi.h"
27 #include "../pci.h"
28 #include "reg.h"
29 #include "led.h"
30 
31 static void _rtl92ce_init_led(struct ieee80211_hw *hw,
32 			      struct rtl_led *pled, enum rtl_led_pin ledpin)
33 {
34 	pled->hw = hw;
35 	pled->ledpin = ledpin;
36 	pled->ledon = false;
37 }
38 
39 void rtl92de_sw_led_on(struct ieee80211_hw *hw, struct rtl_led *pled)
40 {
41 	u8 ledcfg;
42 	struct rtl_priv *rtlpriv = rtl_priv(hw);
43 
44 	RT_TRACE(rtlpriv, COMP_LED, DBG_LOUD, "LedAddr:%X ledpin=%d\n",
45 		 REG_LEDCFG2, pled->ledpin);
46 
47 	switch (pled->ledpin) {
48 	case LED_PIN_GPIO0:
49 		break;
50 	case LED_PIN_LED0:
51 		ledcfg = rtl_read_byte(rtlpriv, REG_LEDCFG2);
52 
53 		if ((rtlpriv->efuse.eeprom_did == 0x8176) ||
54 			(rtlpriv->efuse.eeprom_did == 0x8193))
55 			/* BIT7 of REG_LEDCFG2 should be set to
56 			 * make sure we could emit the led2. */
57 			rtl_write_byte(rtlpriv, REG_LEDCFG2, (ledcfg & 0xf0) |
58 				       BIT(7) | BIT(5) | BIT(6));
59 		else
60 			rtl_write_byte(rtlpriv, REG_LEDCFG2, (ledcfg & 0xf0) |
61 				       BIT(7) | BIT(5));
62 		break;
63 	case LED_PIN_LED1:
64 		ledcfg = rtl_read_byte(rtlpriv, REG_LEDCFG1);
65 
66 		rtl_write_byte(rtlpriv, REG_LEDCFG2, (ledcfg & 0x0f) | BIT(5));
67 		break;
68 	default:
69 		pr_err("switch case %#x not processed\n",
70 		       pled->ledpin);
71 		break;
72 	}
73 	pled->ledon = true;
74 }
75 
76 void rtl92de_sw_led_off(struct ieee80211_hw *hw, struct rtl_led *pled)
77 {
78 	struct rtl_priv *rtlpriv = rtl_priv(hw);
79 	u8 ledcfg;
80 
81 	RT_TRACE(rtlpriv, COMP_LED, DBG_LOUD, "LedAddr:%X ledpin=%d\n",
82 		 REG_LEDCFG2, pled->ledpin);
83 
84 	ledcfg = rtl_read_byte(rtlpriv, REG_LEDCFG2);
85 
86 	switch (pled->ledpin) {
87 	case LED_PIN_GPIO0:
88 		break;
89 	case LED_PIN_LED0:
90 		ledcfg &= 0xf0;
91 		if (rtlpriv->ledctl.led_opendrain)
92 			rtl_write_byte(rtlpriv, REG_LEDCFG2,
93 				       (ledcfg | BIT(1) | BIT(5) | BIT(6)));
94 		else
95 			rtl_write_byte(rtlpriv, REG_LEDCFG2,
96 				       (ledcfg | BIT(3) | BIT(5) | BIT(6)));
97 		break;
98 	case LED_PIN_LED1:
99 		ledcfg &= 0x0f;
100 		rtl_write_byte(rtlpriv, REG_LEDCFG2, (ledcfg | BIT(3)));
101 		break;
102 	default:
103 		pr_err("switch case %#x not processed\n",
104 		       pled->ledpin);
105 		break;
106 	}
107 	pled->ledon = false;
108 }
109 
110 void rtl92de_init_sw_leds(struct ieee80211_hw *hw)
111 {
112 	struct rtl_priv *rtlpriv = rtl_priv(hw);
113 
114 	_rtl92ce_init_led(hw, &rtlpriv->ledctl.sw_led0, LED_PIN_LED0);
115 	_rtl92ce_init_led(hw, &rtlpriv->ledctl.sw_led1, LED_PIN_LED1);
116 }
117 
118 static void _rtl92ce_sw_led_control(struct ieee80211_hw *hw,
119 				    enum led_ctl_mode ledaction)
120 {
121 	struct rtl_priv *rtlpriv = rtl_priv(hw);
122 	struct rtl_led *pled0 = &rtlpriv->ledctl.sw_led0;
123 
124 	switch (ledaction) {
125 	case LED_CTL_POWER_ON:
126 	case LED_CTL_LINK:
127 	case LED_CTL_NO_LINK:
128 		rtl92de_sw_led_on(hw, pled0);
129 		break;
130 	case LED_CTL_POWER_OFF:
131 		rtl92de_sw_led_off(hw, pled0);
132 		break;
133 	default:
134 		break;
135 	}
136 }
137 
138 void rtl92de_led_control(struct ieee80211_hw *hw, enum led_ctl_mode ledaction)
139 {
140 	struct rtl_priv *rtlpriv = rtl_priv(hw);
141 	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
142 
143 	if ((ppsc->rfoff_reason > RF_CHANGE_BY_PS) &&
144 	    (ledaction == LED_CTL_TX ||
145 	     ledaction == LED_CTL_RX ||
146 	     ledaction == LED_CTL_SITE_SURVEY ||
147 	     ledaction == LED_CTL_LINK ||
148 	     ledaction == LED_CTL_NO_LINK ||
149 	     ledaction == LED_CTL_START_TO_LINK ||
150 	     ledaction == LED_CTL_POWER_ON)) {
151 		return;
152 	}
153 	RT_TRACE(rtlpriv, COMP_LED, DBG_LOUD, "ledaction %d,\n", ledaction);
154 
155 	_rtl92ce_sw_led_control(hw, ledaction);
156 }
157