led.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Atheros CARL9170 driver
  3. *
  4. * LED handling
  5. *
  6. * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
  7. * Copyright 2009, 2010, Christian Lamparer <chunkeey@googlemail.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; see the file COPYING. If not, see
  21. * http://www.gnu.org/licenses/.
  22. *
  23. * This file incorporates work covered by the following copyright and
  24. * permission notice:
  25. * Copyright (c) 2007-2008 Atheros Communications, Inc.
  26. *
  27. * Permission to use, copy, modify, and/or distribute this software for any
  28. * purpose with or without fee is hereby granted, provided that the above
  29. * copyright notice and this permission notice appear in all copies.
  30. *
  31. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  32. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  33. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  34. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  35. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  36. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  37. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  38. */
  39. #include "carl9170.h"
  40. #include "cmd.h"
  41. int carl9170_led_set_state(struct ar9170 *ar, const u32 led_state)
  42. {
  43. return carl9170_write_reg(ar, AR9170_GPIO_REG_PORT_DATA, led_state);
  44. }
  45. int carl9170_led_init(struct ar9170 *ar)
  46. {
  47. int err;
  48. /* disable LEDs */
  49. /* GPIO [0/1 mode: output, 2/3: input] */
  50. err = carl9170_write_reg(ar, AR9170_GPIO_REG_PORT_TYPE, 3);
  51. if (err)
  52. goto out;
  53. /* GPIO 0/1 value: off */
  54. err = carl9170_led_set_state(ar, 0);
  55. out:
  56. return err;
  57. }
  58. #ifdef CONFIG_CARL9170_LEDS
  59. static void carl9170_led_update(struct work_struct *work)
  60. {
  61. struct ar9170 *ar = container_of(work, struct ar9170, led_work.work);
  62. int i, tmp = 300, blink_delay = 1000;
  63. u32 led_val = 0;
  64. bool rerun = false;
  65. if (!IS_ACCEPTING_CMD(ar))
  66. return;
  67. mutex_lock(&ar->mutex);
  68. for (i = 0; i < AR9170_NUM_LEDS; i++) {
  69. if (ar->leds[i].registered) {
  70. if (ar->leds[i].last_state ||
  71. ar->leds[i].toggled) {
  72. if (ar->leds[i].toggled)
  73. tmp = 70 + 200 / (ar->leds[i].toggled);
  74. if (tmp < blink_delay)
  75. blink_delay = tmp;
  76. led_val |= 1 << i;
  77. ar->leds[i].toggled = 0;
  78. rerun = true;
  79. }
  80. }
  81. }
  82. carl9170_led_set_state(ar, led_val);
  83. mutex_unlock(&ar->mutex);
  84. if (!rerun)
  85. return;
  86. ieee80211_queue_delayed_work(ar->hw,
  87. &ar->led_work,
  88. msecs_to_jiffies(blink_delay));
  89. }
  90. static void carl9170_led_set_brightness(struct led_classdev *led,
  91. enum led_brightness brightness)
  92. {
  93. struct carl9170_led *arl = container_of(led, struct carl9170_led, l);
  94. struct ar9170 *ar = arl->ar;
  95. if (!arl->registered)
  96. return;
  97. if (arl->last_state != !!brightness) {
  98. arl->toggled++;
  99. arl->last_state = !!brightness;
  100. }
  101. if (likely(IS_ACCEPTING_CMD(ar) && arl->toggled))
  102. ieee80211_queue_delayed_work(ar->hw, &ar->led_work, HZ / 10);
  103. }
  104. static int carl9170_led_register_led(struct ar9170 *ar, int i, char *name,
  105. const char *trigger)
  106. {
  107. int err;
  108. snprintf(ar->leds[i].name, sizeof(ar->leds[i].name),
  109. "carl9170-%s::%s", wiphy_name(ar->hw->wiphy), name);
  110. ar->leds[i].ar = ar;
  111. ar->leds[i].l.name = ar->leds[i].name;
  112. ar->leds[i].l.brightness_set = carl9170_led_set_brightness;
  113. ar->leds[i].l.brightness = 0;
  114. ar->leds[i].l.default_trigger = trigger;
  115. err = led_classdev_register(wiphy_dev(ar->hw->wiphy),
  116. &ar->leds[i].l);
  117. if (err) {
  118. wiphy_err(ar->hw->wiphy, "failed to register %s LED (%d).\n",
  119. ar->leds[i].name, err);
  120. } else {
  121. ar->leds[i].registered = true;
  122. }
  123. return err;
  124. }
  125. void carl9170_led_unregister(struct ar9170 *ar)
  126. {
  127. int i;
  128. for (i = 0; i < AR9170_NUM_LEDS; i++)
  129. if (ar->leds[i].registered) {
  130. led_classdev_unregister(&ar->leds[i].l);
  131. ar->leds[i].registered = false;
  132. ar->leds[i].toggled = 0;
  133. }
  134. cancel_delayed_work_sync(&ar->led_work);
  135. }
  136. int carl9170_led_register(struct ar9170 *ar)
  137. {
  138. int err;
  139. INIT_DELAYED_WORK(&ar->led_work, carl9170_led_update);
  140. err = carl9170_led_register_led(ar, 0, "tx",
  141. ieee80211_get_tx_led_name(ar->hw));
  142. if (err)
  143. goto fail;
  144. if (ar->features & CARL9170_ONE_LED)
  145. return 0;
  146. err = carl9170_led_register_led(ar, 1, "assoc",
  147. ieee80211_get_assoc_led_name(ar->hw));
  148. if (err)
  149. goto fail;
  150. return 0;
  151. fail:
  152. carl9170_led_unregister(ar);
  153. return err;
  154. }
  155. #endif /* CONFIG_CARL9170_LEDS */