xref: /openbmc/linux/drivers/net/wireless/ath/ath5k/rfkill.c (revision a6ae0716e5c3b5f9dbe5ed8f473a6c7b89692365)
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;
59*a6ae0716SBob Copeland 	u32 curval;
60*a6ae0716SBob Copeland 
61e6a3b616STobias Doerffel 	ath5k_hw_set_gpio_input(ah, sc->rf_kill.gpio);
62*a6ae0716SBob Copeland 	curval = ath5k_hw_get_gpio(ah, sc->rf_kill.gpio);
63e6a3b616STobias Doerffel 	ath5k_hw_set_gpio_intr(ah, sc->rf_kill.gpio, enable ?
64*a6ae0716SBob Copeland 					!!curval : !curval);
65e6a3b616STobias Doerffel }
66e6a3b616STobias Doerffel 
67e6a3b616STobias Doerffel static bool
68e6a3b616STobias Doerffel ath5k_is_rfkill_set(struct ath5k_softc *sc)
69e6a3b616STobias Doerffel {
70e6a3b616STobias Doerffel 	/* configuring GPIO for input for some reason disables rfkill */
71e6a3b616STobias Doerffel 	/*ath5k_hw_set_gpio_input(sc->ah, sc->rf_kill.gpio);*/
72e6a3b616STobias Doerffel 	return ath5k_hw_get_gpio(sc->ah, sc->rf_kill.gpio) ==
73e6a3b616STobias Doerffel 							sc->rf_kill.polarity;
74e6a3b616STobias Doerffel }
75e6a3b616STobias Doerffel 
76e6a3b616STobias Doerffel static void
77e6a3b616STobias Doerffel ath5k_tasklet_rfkill_toggle(unsigned long data)
78e6a3b616STobias Doerffel {
79e6a3b616STobias Doerffel 	struct ath5k_softc *sc = (void *)data;
80e6a3b616STobias Doerffel 	bool blocked;
81e6a3b616STobias Doerffel 
82e6a3b616STobias Doerffel 	blocked = ath5k_is_rfkill_set(sc);
83e6a3b616STobias Doerffel 	wiphy_rfkill_set_hw_state(sc->hw->wiphy, blocked);
84e6a3b616STobias Doerffel }
85e6a3b616STobias Doerffel 
86e6a3b616STobias Doerffel 
87e6a3b616STobias Doerffel void
88e6a3b616STobias Doerffel ath5k_rfkill_hw_start(struct ath5k_hw *ah)
89e6a3b616STobias Doerffel {
90e6a3b616STobias Doerffel 	struct ath5k_softc *sc = ah->ah_sc;
91e6a3b616STobias Doerffel 
92e6a3b616STobias Doerffel 	/* read rfkill GPIO configuration from EEPROM header */
93e6a3b616STobias Doerffel 	sc->rf_kill.gpio = ah->ah_capabilities.cap_eeprom.ee_rfkill_pin;
94e6a3b616STobias Doerffel 	sc->rf_kill.polarity = ah->ah_capabilities.cap_eeprom.ee_rfkill_pol;
95e6a3b616STobias Doerffel 
96e6a3b616STobias Doerffel 	tasklet_init(&sc->rf_kill.toggleq, ath5k_tasklet_rfkill_toggle,
97e6a3b616STobias Doerffel 		(unsigned long)sc);
98e6a3b616STobias Doerffel 
99e6a3b616STobias Doerffel 	ath5k_rfkill_disable(sc);
100e6a3b616STobias Doerffel 
101e6a3b616STobias Doerffel 	/* enable interrupt for rfkill switch */
102*a6ae0716SBob Copeland 	if (AR5K_EEPROM_HDR_RFKILL(ah->ah_capabilities.cap_eeprom.ee_header))
103e6a3b616STobias Doerffel 		ath5k_rfkill_set_intr(sc, true);
104e6a3b616STobias Doerffel }
105e6a3b616STobias Doerffel 
106e6a3b616STobias Doerffel 
107e6a3b616STobias Doerffel void
108e6a3b616STobias Doerffel ath5k_rfkill_hw_stop(struct ath5k_hw *ah)
109e6a3b616STobias Doerffel {
110e6a3b616STobias Doerffel 	struct ath5k_softc *sc = ah->ah_sc;
111e6a3b616STobias Doerffel 
112e6a3b616STobias Doerffel 	/* disable interrupt for rfkill switch */
113*a6ae0716SBob Copeland 	if (AR5K_EEPROM_HDR_RFKILL(ah->ah_capabilities.cap_eeprom.ee_header))
114e6a3b616STobias Doerffel 		ath5k_rfkill_set_intr(sc, false);
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