1e6a3b616STobias Doerffel /*
2e6a3b616STobias Doerffel  * RFKILL support for ath5k
3e6a3b616STobias Doerffel  *
4e6a3b616STobias Doerffel  * Copyright (c) 2009 Tobias Doerffel <tobias.doerffel@gmail.com>
5e6a3b616STobias Doerffel  *
6e6a3b616STobias Doerffel  * All rights reserved.
7e6a3b616STobias Doerffel  *
8e6a3b616STobias Doerffel  * Redistribution and use in source and binary forms, with or without
9e6a3b616STobias Doerffel  * modification, are permitted provided that the following conditions
10e6a3b616STobias Doerffel  * are met:
11e6a3b616STobias Doerffel  * 1. Redistributions of source code must retain the above copyright
12e6a3b616STobias Doerffel  *    notice, this list of conditions and the following disclaimer,
13e6a3b616STobias Doerffel  *    without modification.
14e6a3b616STobias Doerffel  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
15e6a3b616STobias Doerffel  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
16e6a3b616STobias Doerffel  *    redistribution must be conditioned upon including a substantially
17e6a3b616STobias Doerffel  *    similar Disclaimer requirement for further binary redistribution.
18e6a3b616STobias Doerffel  * 3. Neither the names of the above-listed copyright holders nor the names
19e6a3b616STobias Doerffel  *    of any contributors may be used to endorse or promote products derived
20e6a3b616STobias Doerffel  *    from this software without specific prior written permission.
21e6a3b616STobias Doerffel  *
22e6a3b616STobias Doerffel  * NO WARRANTY
23e6a3b616STobias Doerffel  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24e6a3b616STobias Doerffel  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25e6a3b616STobias Doerffel  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
26e6a3b616STobias Doerffel  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
27e6a3b616STobias Doerffel  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
28e6a3b616STobias Doerffel  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29e6a3b616STobias Doerffel  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30e6a3b616STobias Doerffel  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
31e6a3b616STobias Doerffel  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32e6a3b616STobias Doerffel  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
33e6a3b616STobias Doerffel  * THE POSSIBILITY OF SUCH DAMAGES.
34e6a3b616STobias Doerffel  */
35e6a3b616STobias Doerffel 
36e6a3b616STobias Doerffel #include "base.h"
37e6a3b616STobias Doerffel 
38e6a3b616STobias Doerffel 
39e6a3b616STobias Doerffel static inline void ath5k_rfkill_disable(struct ath5k_softc *sc)
40e6a3b616STobias Doerffel {
41e6a3b616STobias Doerffel 	ATH5K_DBG(sc, ATH5K_DEBUG_ANY, "rfkill disable (gpio:%d polarity:%d)\n",
42e6a3b616STobias Doerffel 		sc->rf_kill.gpio, sc->rf_kill.polarity);
43e6a3b616STobias Doerffel 	ath5k_hw_set_gpio_output(sc->ah, sc->rf_kill.gpio);
44e6a3b616STobias Doerffel 	ath5k_hw_set_gpio(sc->ah, sc->rf_kill.gpio, !sc->rf_kill.polarity);
45e6a3b616STobias Doerffel }
46e6a3b616STobias Doerffel 
47e6a3b616STobias Doerffel 
48e6a3b616STobias Doerffel static inline void ath5k_rfkill_enable(struct ath5k_softc *sc)
49e6a3b616STobias Doerffel {
50e6a3b616STobias Doerffel 	ATH5K_DBG(sc, ATH5K_DEBUG_ANY, "rfkill enable (gpio:%d polarity:%d)\n",
51e6a3b616STobias Doerffel 		sc->rf_kill.gpio, sc->rf_kill.polarity);
52e6a3b616STobias Doerffel 	ath5k_hw_set_gpio_output(sc->ah, sc->rf_kill.gpio);
53e6a3b616STobias Doerffel 	ath5k_hw_set_gpio(sc->ah, sc->rf_kill.gpio, sc->rf_kill.polarity);
54e6a3b616STobias Doerffel }
55e6a3b616STobias Doerffel 
56e6a3b616STobias Doerffel static inline void ath5k_rfkill_set_intr(struct ath5k_softc *sc, bool enable)
57e6a3b616STobias Doerffel {
58e6a3b616STobias Doerffel 	struct ath5k_hw *ah = sc->ah;
59e6a3b616STobias Doerffel 	ath5k_hw_set_gpio_input(ah, sc->rf_kill.gpio);
60e6a3b616STobias Doerffel 	ah->ah_gpio[0] = ath5k_hw_get_gpio(ah, sc->rf_kill.gpio);
61e6a3b616STobias Doerffel 	ath5k_hw_set_gpio_intr(ah, sc->rf_kill.gpio, enable ?
62e6a3b616STobias Doerffel 					!!ah->ah_gpio[0] : !ah->ah_gpio[0]);
63e6a3b616STobias Doerffel }
64e6a3b616STobias Doerffel 
65e6a3b616STobias Doerffel static bool
66e6a3b616STobias Doerffel ath5k_is_rfkill_set(struct ath5k_softc *sc)
67e6a3b616STobias Doerffel {
68e6a3b616STobias Doerffel 	/* configuring GPIO for input for some reason disables rfkill */
69e6a3b616STobias Doerffel 	/*ath5k_hw_set_gpio_input(sc->ah, sc->rf_kill.gpio);*/
70e6a3b616STobias Doerffel 	return ath5k_hw_get_gpio(sc->ah, sc->rf_kill.gpio) ==
71e6a3b616STobias Doerffel 							sc->rf_kill.polarity;
72e6a3b616STobias Doerffel }
73e6a3b616STobias Doerffel 
74e6a3b616STobias Doerffel static void
75e6a3b616STobias Doerffel ath5k_tasklet_rfkill_toggle(unsigned long data)
76e6a3b616STobias Doerffel {
77e6a3b616STobias Doerffel 	struct ath5k_softc *sc = (void *)data;
78e6a3b616STobias Doerffel 	bool blocked;
79e6a3b616STobias Doerffel 
80e6a3b616STobias Doerffel 	blocked = ath5k_is_rfkill_set(sc);
81e6a3b616STobias Doerffel 	wiphy_rfkill_set_hw_state(sc->hw->wiphy, blocked);
82e6a3b616STobias Doerffel }
83e6a3b616STobias Doerffel 
84e6a3b616STobias Doerffel 
85e6a3b616STobias Doerffel void
86e6a3b616STobias Doerffel ath5k_rfkill_hw_start(struct ath5k_hw *ah)
87e6a3b616STobias Doerffel {
88e6a3b616STobias Doerffel 	struct ath5k_softc *sc = ah->ah_sc;
89e6a3b616STobias Doerffel 
90e6a3b616STobias Doerffel 	/* read rfkill GPIO configuration from EEPROM header */
91e6a3b616STobias Doerffel 	sc->rf_kill.gpio = ah->ah_capabilities.cap_eeprom.ee_rfkill_pin;
92e6a3b616STobias Doerffel 	sc->rf_kill.polarity = ah->ah_capabilities.cap_eeprom.ee_rfkill_pol;
93e6a3b616STobias Doerffel 
94e6a3b616STobias Doerffel 	tasklet_init(&sc->rf_kill.toggleq, ath5k_tasklet_rfkill_toggle,
95e6a3b616STobias Doerffel 		(unsigned long)sc);
96e6a3b616STobias Doerffel 
97e6a3b616STobias Doerffel 	ath5k_rfkill_disable(sc);
98e6a3b616STobias Doerffel 
99e6a3b616STobias Doerffel 	/* enable interrupt for rfkill switch */
100e6a3b616STobias Doerffel 	if (AR5K_EEPROM_HDR_RFKILL(ah->ah_capabilities.cap_eeprom.ee_header)) {
101e6a3b616STobias Doerffel 		ath5k_rfkill_set_intr(sc, true);
102e6a3b616STobias Doerffel 	}
103e6a3b616STobias Doerffel }
104e6a3b616STobias Doerffel 
105e6a3b616STobias Doerffel 
106e6a3b616STobias Doerffel void
107e6a3b616STobias Doerffel ath5k_rfkill_hw_stop(struct ath5k_hw *ah)
108e6a3b616STobias Doerffel {
109e6a3b616STobias Doerffel 	struct ath5k_softc *sc = ah->ah_sc;
110e6a3b616STobias Doerffel 
111e6a3b616STobias Doerffel 	/* disable interrupt for rfkill switch */
112e6a3b616STobias Doerffel 	if (AR5K_EEPROM_HDR_RFKILL(ah->ah_capabilities.cap_eeprom.ee_header)) {
113e6a3b616STobias Doerffel 		ath5k_rfkill_set_intr(sc, false);
114e6a3b616STobias Doerffel 	}
115e6a3b616STobias Doerffel 
116e6a3b616STobias Doerffel 	tasklet_kill(&sc->rf_kill.toggleq);
117e6a3b616STobias Doerffel 
118e6a3b616STobias Doerffel 	/* enable RFKILL when stopping HW so Wifi LED is turned off */
119e6a3b616STobias Doerffel 	ath5k_rfkill_enable(sc);
120e6a3b616STobias Doerffel }
121e6a3b616STobias Doerffel 
122