Linux-libre 3.16.85-gnu
[librecmc/linux-libre.git] / drivers / staging / rtl8821ae / rtl8821ae / led.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2009-2010  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  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17  *
18  * The full GNU General Public License is included in this distribution in the
19  * file called LICENSE.
20  *
21  * Contact Information:
22  * wlanfae <wlanfae@realtek.com>
23  * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
24  * Hsinchu 300, Taiwan.
25  *
26  * Larry Finger <Larry.Finger@lwfinger.net>
27  *
28  *****************************************************************************/
29
30 #include "../wifi.h"
31 #include "../pci.h"
32 #include "reg.h"
33
34 static void _rtl8821ae_init_led(struct ieee80211_hw *hw,
35                                                                  struct rtl_led *pled,
36                                                                  enum rtl_led_pin ledpin)
37 {
38         pled->hw = hw;
39         pled->ledpin = ledpin;
40         pled->b_ledon = false;
41 }
42
43 void rtl8821ae_sw_led_on(struct ieee80211_hw *hw, struct rtl_led *pled)
44 {
45         u8 ledcfg;
46         struct rtl_priv *rtlpriv = rtl_priv(hw);
47
48         RT_TRACE(COMP_LED, DBG_LOUD,
49                  ("LedAddr:%X ledpin=%d\n", REG_LEDCFG2, pled->ledpin));
50
51         switch (pled->ledpin) {
52         case LED_PIN_GPIO0:
53                 break;
54         case LED_PIN_LED0:
55                 ledcfg = rtl_read_byte(rtlpriv, REG_LEDCFG2);
56                 ledcfg &= ~BIT(6);
57                 rtl_write_byte(rtlpriv,
58                                REG_LEDCFG2, (ledcfg & 0xf0) | BIT(5));
59                 break;
60         case LED_PIN_LED1:
61                 ledcfg = rtl_read_byte(rtlpriv, REG_LEDCFG1);
62                 rtl_write_byte(rtlpriv, REG_LEDCFG1, ledcfg & 0x10);
63                 break;
64         default:
65                 RT_TRACE(COMP_ERR, DBG_EMERG,
66                          ("switch case not process \n"));
67                 break;
68         }
69         pled->b_ledon = true;
70 }
71
72 void rtl8812ae_sw_led_on(struct ieee80211_hw *hw, struct rtl_led *pled)
73 {
74         u16     ledreg = REG_LEDCFG1;
75         u8      ledcfg = 0;
76         struct rtl_priv *rtlpriv = rtl_priv(hw);
77
78         switch (pled->ledpin) {
79                 case LED_PIN_LED0:
80                         ledreg = REG_LEDCFG1;
81                         break;
82
83                 case LED_PIN_LED1:
84                         ledreg = REG_LEDCFG2;
85                         break;
86
87                 case LED_PIN_GPIO0:
88                 default:
89                         break;
90         }
91
92         RT_TRACE(COMP_LED, DBG_LOUD, ("In SwLedOn, LedAddr:%X LEDPIN=%d \n", ledreg, pled->ledpin));
93
94         ledcfg =  rtl_read_byte(rtlpriv, ledreg);
95         ledcfg |= BIT(5); /*Set 0x4c[21]*/
96         ledcfg &= ~(BIT(7) | BIT(6) | BIT(3) |BIT(2) | BIT(1) |BIT(0));
97                 /*Clear 0x4c[23:22] and 0x4c[19:16]*/
98         rtl_write_byte(rtlpriv, ledreg, ledcfg); /*SW control led0 on.*/
99         pled->b_ledon = true;
100 }
101
102 void rtl8821ae_sw_led_off(struct ieee80211_hw *hw, struct rtl_led *pled)
103 {
104         struct rtl_priv *rtlpriv = rtl_priv(hw);
105         struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
106         u8 ledcfg;
107
108         RT_TRACE(COMP_LED, DBG_LOUD,
109                  ("LedAddr:%X ledpin=%d\n", REG_LEDCFG2, pled->ledpin));
110
111         ledcfg = rtl_read_byte(rtlpriv, REG_LEDCFG2);
112
113         switch (pled->ledpin) {
114         case LED_PIN_GPIO0:
115                 break;
116         case LED_PIN_LED0:
117                 ledcfg &= 0xf0;
118                 if (pcipriv->ledctl.bled_opendrain == true) {
119                         ledcfg &= 0x90; /* Set to software control. */
120                         rtl_write_byte(rtlpriv, REG_LEDCFG2, (ledcfg|BIT(3)));
121                         ledcfg = rtl_read_byte(rtlpriv, REG_MAC_PINMUX_CFG);
122                         ledcfg &= 0xFE;
123                         rtl_write_byte(rtlpriv, REG_MAC_PINMUX_CFG, ledcfg);
124                 }
125                 else {
126                         ledcfg &= ~BIT(6);
127                         rtl_write_byte(rtlpriv, REG_LEDCFG2,
128                                         (ledcfg | BIT(3) | BIT(5)));
129                 }
130                 break;
131         case LED_PIN_LED1:
132                 ledcfg = rtl_read_byte(rtlpriv, REG_LEDCFG1);
133                 ledcfg &= 0x10; /* Set to software control. */
134                 rtl_write_byte(rtlpriv, REG_LEDCFG1, ledcfg|BIT(3));
135
136                 break;
137         default:
138                 RT_TRACE(COMP_ERR, DBG_EMERG,
139                          ("switch case not process \n"));
140                 break;
141         }
142         pled->b_ledon = false;
143 }
144
145 void rtl8812ae_sw_led_off(struct ieee80211_hw *hw, struct rtl_led *pled){
146         u16 ledreg = REG_LEDCFG1;
147         struct rtl_priv *rtlpriv = rtl_priv(hw);
148         struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
149
150         switch(pled->ledpin)
151         {
152                 case LED_PIN_LED0:
153                         ledreg = REG_LEDCFG1;
154                         break;
155
156                 case LED_PIN_LED1:
157                         ledreg = REG_LEDCFG2;
158                         break;
159
160                 case LED_PIN_GPIO0:
161                 default:
162                         break;
163         }
164
165         RT_TRACE(COMP_LED,DBG_LOUD,("In SwLedOff,LedAddr:%X LEDPIN=%d\n", ledreg, pled->ledpin));
166
167         if(pcipriv->ledctl.bled_opendrain == true) /*Open-drain arrangement for controlling the LED*/
168         {
169                 u8 ledcfg = rtl_read_byte(rtlpriv,  ledreg);
170
171                 ledreg &= 0xd0; /* Set to software control.*/
172                 rtl_write_byte(rtlpriv, ledreg, (ledcfg | BIT(3)));
173
174                 /*Open-drain arrangement*/
175                 ledcfg = rtl_read_byte(rtlpriv, REG_MAC_PINMUX_CFG);
176                 ledcfg &= 0xFE;/*Set GPIO[8] to input mode*/
177                 rtl_write_byte(rtlpriv, REG_MAC_PINMUX_CFG, ledcfg);
178         }
179         else
180         {
181                 rtl_write_byte(rtlpriv, ledreg, 0x28);
182         }
183
184         pled->b_ledon = false;
185 }
186
187 void rtl8821ae_init_sw_leds(struct ieee80211_hw *hw)
188 {
189         struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
190         _rtl8821ae_init_led(hw, &(pcipriv->ledctl.sw_led0), LED_PIN_LED0);
191         _rtl8821ae_init_led(hw, &(pcipriv->ledctl.sw_led1), LED_PIN_LED1);
192 }
193
194 static void _rtl8821ae_sw_led_control(struct ieee80211_hw *hw,
195                                     enum led_ctl_mode ledaction)
196 {
197         struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
198         struct rtl_led *pLed0 = &(pcipriv->ledctl.sw_led0);
199         struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
200         switch (ledaction) {
201         case LED_CTL_POWER_ON:
202         case LED_CTL_LINK:
203         case LED_CTL_NO_LINK:
204                 if (rtlhal->hw_type == HARDWARE_TYPE_RTL8812AE)
205                         rtl8812ae_sw_led_on(hw, pLed0);
206                 else
207                         rtl8821ae_sw_led_on(hw, pLed0);
208                 break;
209         case LED_CTL_POWER_OFF:
210                 if (rtlhal->hw_type == HARDWARE_TYPE_RTL8812AE)\
211                         rtl8812ae_sw_led_off(hw, pLed0);
212                 else
213                         rtl8821ae_sw_led_off(hw, pLed0);
214                 break;
215         default:
216                 break;
217         }
218 }
219
220 void rtl8821ae_led_control(struct ieee80211_hw *hw,
221                         enum led_ctl_mode ledaction)
222 {
223         struct rtl_priv *rtlpriv = rtl_priv(hw);
224         struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
225
226         if ((ppsc->rfoff_reason > RF_CHANGE_BY_PS) &&
227             (ledaction == LED_CTL_TX ||
228              ledaction == LED_CTL_RX ||
229              ledaction == LED_CTL_SITE_SURVEY ||
230              ledaction == LED_CTL_LINK ||
231              ledaction == LED_CTL_NO_LINK ||
232              ledaction == LED_CTL_START_TO_LINK ||
233              ledaction == LED_CTL_POWER_ON)) {
234                 return;
235         }
236         RT_TRACE(COMP_LED, DBG_LOUD, ("ledaction %d, \n",
237                                 ledaction));
238         _rtl8821ae_sw_led_control(hw, ledaction);
239 }