led.c 3.0 KB

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