led.c 4.7 KB

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