gpio.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright (c) 2004-2008 Reyk Floeter <reyk@openbsd.org>
  3. * Copyright (c) 2006-2008 Nick Kossifidis <mickflemm@gmail.com>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. *
  17. */
  18. /****************\
  19. GPIO Functions
  20. \****************/
  21. #include "ath5k.h"
  22. #include "reg.h"
  23. #include "debug.h"
  24. /**
  25. * DOC: GPIO/LED functions
  26. *
  27. * Here we control the 6 bidirectional GPIO pins provided by the hw.
  28. * We can set a GPIO pin to be an input or an output pin on GPIO control
  29. * register and then read or set its status from GPIO data input/output
  30. * registers.
  31. *
  32. * We also control the two LED pins provided by the hw, LED_0 is our
  33. * "power" LED and LED_1 is our "network activity" LED but many scenarios
  34. * are available from hw. Vendors might also provide LEDs connected to the
  35. * GPIO pins, we handle them through the LED subsystem on led.c
  36. */
  37. /**
  38. * ath5k_hw_set_ledstate() - Set led state
  39. * @ah: The &struct ath5k_hw
  40. * @state: One of AR5K_LED_*
  41. *
  42. * Used to set the LED blinking state. This only
  43. * works for the LED connected to the LED_0, LED_1 pins,
  44. * not the GPIO based.
  45. */
  46. void
  47. ath5k_hw_set_ledstate(struct ath5k_hw *ah, unsigned int state)
  48. {
  49. u32 led;
  50. /*5210 has different led mode handling*/
  51. u32 led_5210;
  52. /*Reset led status*/
  53. if (ah->ah_version != AR5K_AR5210)
  54. AR5K_REG_DISABLE_BITS(ah, AR5K_PCICFG,
  55. AR5K_PCICFG_LEDMODE | AR5K_PCICFG_LED);
  56. else
  57. AR5K_REG_DISABLE_BITS(ah, AR5K_PCICFG, AR5K_PCICFG_LED);
  58. /*
  59. * Some blinking values, define at your wish
  60. */
  61. switch (state) {
  62. case AR5K_LED_SCAN:
  63. case AR5K_LED_AUTH:
  64. led = AR5K_PCICFG_LEDMODE_PROP | AR5K_PCICFG_LED_PEND;
  65. led_5210 = AR5K_PCICFG_LED_PEND | AR5K_PCICFG_LED_BCTL;
  66. break;
  67. case AR5K_LED_INIT:
  68. led = AR5K_PCICFG_LEDMODE_PROP | AR5K_PCICFG_LED_NONE;
  69. led_5210 = AR5K_PCICFG_LED_PEND;
  70. break;
  71. case AR5K_LED_ASSOC:
  72. case AR5K_LED_RUN:
  73. led = AR5K_PCICFG_LEDMODE_PROP | AR5K_PCICFG_LED_ASSOC;
  74. led_5210 = AR5K_PCICFG_LED_ASSOC;
  75. break;
  76. default:
  77. led = AR5K_PCICFG_LEDMODE_PROM | AR5K_PCICFG_LED_NONE;
  78. led_5210 = AR5K_PCICFG_LED_PEND;
  79. break;
  80. }
  81. /*Write new status to the register*/
  82. if (ah->ah_version != AR5K_AR5210)
  83. AR5K_REG_ENABLE_BITS(ah, AR5K_PCICFG, led);
  84. else
  85. AR5K_REG_ENABLE_BITS(ah, AR5K_PCICFG, led_5210);
  86. }
  87. /**
  88. * ath5k_hw_set_gpio_input() - Set GPIO inputs
  89. * @ah: The &struct ath5k_hw
  90. * @gpio: GPIO pin to set as input
  91. */
  92. int
  93. ath5k_hw_set_gpio_input(struct ath5k_hw *ah, u32 gpio)
  94. {
  95. if (gpio >= AR5K_NUM_GPIO)
  96. return -EINVAL;
  97. ath5k_hw_reg_write(ah,
  98. (ath5k_hw_reg_read(ah, AR5K_GPIOCR) & ~AR5K_GPIOCR_OUT(gpio))
  99. | AR5K_GPIOCR_IN(gpio), AR5K_GPIOCR);
  100. return 0;
  101. }
  102. /**
  103. * ath5k_hw_set_gpio_output() - Set GPIO outputs
  104. * @ah: The &struct ath5k_hw
  105. * @gpio: The GPIO pin to set as output
  106. */
  107. int
  108. ath5k_hw_set_gpio_output(struct ath5k_hw *ah, u32 gpio)
  109. {
  110. if (gpio >= AR5K_NUM_GPIO)
  111. return -EINVAL;
  112. ath5k_hw_reg_write(ah,
  113. (ath5k_hw_reg_read(ah, AR5K_GPIOCR) & ~AR5K_GPIOCR_OUT(gpio))
  114. | AR5K_GPIOCR_OUT(gpio), AR5K_GPIOCR);
  115. return 0;
  116. }
  117. /**
  118. * ath5k_hw_get_gpio() - Get GPIO state
  119. * @ah: The &struct ath5k_hw
  120. * @gpio: The GPIO pin to read
  121. */
  122. u32
  123. ath5k_hw_get_gpio(struct ath5k_hw *ah, u32 gpio)
  124. {
  125. if (gpio >= AR5K_NUM_GPIO)
  126. return 0xffffffff;
  127. /* GPIO input magic */
  128. return ((ath5k_hw_reg_read(ah, AR5K_GPIODI) & AR5K_GPIODI_M) >> gpio) &
  129. 0x1;
  130. }
  131. /**
  132. * ath5k_hw_set_gpio() - Set GPIO state
  133. * @ah: The &struct ath5k_hw
  134. * @gpio: The GPIO pin to set
  135. * @val: Value to set (boolean)
  136. */
  137. int
  138. ath5k_hw_set_gpio(struct ath5k_hw *ah, u32 gpio, u32 val)
  139. {
  140. u32 data;
  141. if (gpio >= AR5K_NUM_GPIO)
  142. return -EINVAL;
  143. /* GPIO output magic */
  144. data = ath5k_hw_reg_read(ah, AR5K_GPIODO);
  145. data &= ~(1 << gpio);
  146. data |= (val & 1) << gpio;
  147. ath5k_hw_reg_write(ah, data, AR5K_GPIODO);
  148. return 0;
  149. }
  150. /**
  151. * ath5k_hw_set_gpio_intr() - Initialize the GPIO interrupt (RFKill switch)
  152. * @ah: The &struct ath5k_hw
  153. * @gpio: The GPIO pin to use
  154. * @interrupt_level: True to generate interrupt on active pin (high)
  155. *
  156. * This function is used to set up the GPIO interrupt for the hw RFKill switch.
  157. * That switch is connected to a GPIO pin and it's number is stored on EEPROM.
  158. * It can either open or close the circuit to indicate that we should disable
  159. * RF/Wireless to save power (we also get that from EEPROM).
  160. */
  161. void
  162. ath5k_hw_set_gpio_intr(struct ath5k_hw *ah, unsigned int gpio,
  163. u32 interrupt_level)
  164. {
  165. u32 data;
  166. if (gpio >= AR5K_NUM_GPIO)
  167. return;
  168. /*
  169. * Set the GPIO interrupt
  170. */
  171. data = (ath5k_hw_reg_read(ah, AR5K_GPIOCR) &
  172. ~(AR5K_GPIOCR_INT_SEL(gpio) | AR5K_GPIOCR_INT_SELH |
  173. AR5K_GPIOCR_INT_ENA | AR5K_GPIOCR_OUT(gpio))) |
  174. (AR5K_GPIOCR_INT_SEL(gpio) | AR5K_GPIOCR_INT_ENA);
  175. ath5k_hw_reg_write(ah, interrupt_level ? data :
  176. (data | AR5K_GPIOCR_INT_SELH), AR5K_GPIOCR);
  177. ah->ah_imr |= AR5K_IMR_GPIO;
  178. /* Enable GPIO interrupts */
  179. AR5K_REG_ENABLE_BITS(ah, AR5K_PIMR, AR5K_IMR_GPIO);
  180. }