led.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include <net/mac80211.h>
  2. #include <linux/bcma/bcma_driver_chipcommon.h>
  3. #include <linux/gpio.h>
  4. #include "mac80211_if.h"
  5. #include "pub.h"
  6. #include "main.h"
  7. #include "led.h"
  8. /* number of leds */
  9. #define BRCMS_LED_NO 4
  10. /* behavior mask */
  11. #define BRCMS_LED_BEH_MASK 0x7f
  12. /* activelow (polarity) bit */
  13. #define BRCMS_LED_AL_MASK 0x80
  14. /* radio enabled */
  15. #define BRCMS_LED_RADIO 3
  16. static void brcms_radio_led_ctrl(struct brcms_info *wl, bool state)
  17. {
  18. if (wl->radio_led.gpio == -1)
  19. return;
  20. if (wl->radio_led.active_low)
  21. state = !state;
  22. if (state)
  23. gpio_set_value(wl->radio_led.gpio, 1);
  24. else
  25. gpio_set_value(wl->radio_led.gpio, 0);
  26. }
  27. /* Callback from the LED subsystem. */
  28. static void brcms_led_brightness_set(struct led_classdev *led_dev,
  29. enum led_brightness brightness)
  30. {
  31. struct brcms_info *wl = container_of(led_dev,
  32. struct brcms_info, led_dev);
  33. brcms_radio_led_ctrl(wl, brightness);
  34. }
  35. void brcms_led_unregister(struct brcms_info *wl)
  36. {
  37. if (wl->led_dev.dev)
  38. led_classdev_unregister(&wl->led_dev);
  39. if (wl->radio_led.gpio != -1)
  40. gpio_free(wl->radio_led.gpio);
  41. }
  42. int brcms_led_register(struct brcms_info *wl)
  43. {
  44. int i, err;
  45. struct brcms_led *radio_led = &wl->radio_led;
  46. /* get CC core */
  47. struct bcma_drv_cc *cc_drv = &wl->wlc->hw->d11core->bus->drv_cc;
  48. struct gpio_chip *bcma_gpio = &cc_drv->gpio;
  49. struct ssb_sprom *sprom = &wl->wlc->hw->d11core->bus->sprom;
  50. u8 *leds[] = { &sprom->gpio0,
  51. &sprom->gpio1,
  52. &sprom->gpio2,
  53. &sprom->gpio3 };
  54. unsigned gpio = -1;
  55. bool active_low = false;
  56. /* none by default */
  57. radio_led->gpio = -1;
  58. radio_led->active_low = false;
  59. if (!bcma_gpio || !gpio_is_valid(bcma_gpio->base))
  60. return -ENODEV;
  61. /* find radio enabled LED */
  62. for (i = 0; i < BRCMS_LED_NO; i++) {
  63. u8 led = *leds[i];
  64. if ((led & BRCMS_LED_BEH_MASK) == BRCMS_LED_RADIO) {
  65. gpio = bcma_gpio->base + i;
  66. if (led & BRCMS_LED_AL_MASK)
  67. active_low = true;
  68. break;
  69. }
  70. }
  71. if (gpio == -1 || !gpio_is_valid(gpio))
  72. return -ENODEV;
  73. /* request and configure LED gpio */
  74. err = gpio_request_one(gpio,
  75. active_low ? GPIOF_OUT_INIT_HIGH
  76. : GPIOF_OUT_INIT_LOW,
  77. "radio on");
  78. if (err) {
  79. wiphy_err(wl->wiphy, "requesting led gpio %d failed (err: %d)\n",
  80. gpio, err);
  81. return err;
  82. }
  83. err = gpio_direction_output(gpio, 1);
  84. if (err) {
  85. wiphy_err(wl->wiphy, "cannot set led gpio %d to output (err: %d)\n",
  86. gpio, err);
  87. return err;
  88. }
  89. snprintf(wl->radio_led.name, sizeof(wl->radio_led.name),
  90. "brcmsmac-%s:radio", wiphy_name(wl->wiphy));
  91. wl->led_dev.name = wl->radio_led.name;
  92. wl->led_dev.default_trigger =
  93. ieee80211_get_radio_led_name(wl->pub->ieee_hw);
  94. wl->led_dev.brightness_set = brcms_led_brightness_set;
  95. err = led_classdev_register(wiphy_dev(wl->wiphy), &wl->led_dev);
  96. if (err) {
  97. wiphy_err(wl->wiphy, "cannot register led device: %s (err: %d)\n",
  98. wl->radio_led.name, err);
  99. return err;
  100. }
  101. wiphy_info(wl->wiphy, "registered radio enabled led device: %s gpio: %d\n",
  102. wl->radio_led.name,
  103. gpio);
  104. radio_led->gpio = gpio;
  105. radio_led->active_low = active_low;
  106. return 0;
  107. }