led.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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].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. queue_delayed_work(ar->hw->workqueue, &ar->led_work,
  81. msecs_to_jiffies(blink_delay));
  82. }
  83. static void ar9170_led_brightness_set(struct led_classdev *led,
  84. enum led_brightness brightness)
  85. {
  86. struct ar9170_led *arl = container_of(led, struct ar9170_led, l);
  87. struct ar9170 *ar = arl->ar;
  88. arl->toggled++;
  89. if (likely(IS_ACCEPTING_CMD(ar) && brightness))
  90. queue_delayed_work(ar->hw->workqueue, &ar->led_work, HZ/10);
  91. }
  92. static int ar9170_register_led(struct ar9170 *ar, int i, char *name,
  93. char *trigger)
  94. {
  95. int err;
  96. snprintf(ar->leds[i].name, sizeof(ar->leds[i].name),
  97. "ar9170-%s::%s", wiphy_name(ar->hw->wiphy), name);
  98. ar->leds[i].ar = ar;
  99. ar->leds[i].l.name = ar->leds[i].name;
  100. ar->leds[i].l.brightness_set = ar9170_led_brightness_set;
  101. ar->leds[i].l.brightness = 0;
  102. ar->leds[i].l.default_trigger = trigger;
  103. err = led_classdev_register(wiphy_dev(ar->hw->wiphy),
  104. &ar->leds[i].l);
  105. if (err)
  106. printk(KERN_ERR "%s: failed to register %s LED (%d).\n",
  107. wiphy_name(ar->hw->wiphy), ar->leds[i].name, err);
  108. else
  109. ar->leds[i].registered = true;
  110. return err;
  111. }
  112. void ar9170_unregister_leds(struct ar9170 *ar)
  113. {
  114. int i;
  115. cancel_delayed_work_sync(&ar->led_work);
  116. for (i = 0; i < AR9170_NUM_LEDS; i++)
  117. if (ar->leds[i].registered) {
  118. led_classdev_unregister(&ar->leds[i].l);
  119. ar->leds[i].registered = false;
  120. }
  121. }
  122. int ar9170_register_leds(struct ar9170 *ar)
  123. {
  124. int err;
  125. INIT_DELAYED_WORK(&ar->led_work, ar9170_update_leds);
  126. err = ar9170_register_led(ar, 0, "tx",
  127. ieee80211_get_tx_led_name(ar->hw));
  128. if (err)
  129. goto fail;
  130. err = ar9170_register_led(ar, 1, "assoc",
  131. ieee80211_get_assoc_led_name(ar->hw));
  132. if (err)
  133. goto fail;
  134. return 0;
  135. fail:
  136. ar9170_unregister_leds(ar);
  137. return err;
  138. }
  139. #endif /* CONFIG_AR9170_LEDS */