xref: /linux/drivers/net/wireless/broadcom/brcm80211/brcmsmac/led.c (revision e7d759f31ca295d589f7420719c311870bb3166f)
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 <linux/gpio/driver.h>
6 #include <linux/gpio/machine.h>
7 #include <linux/gpio/consumer.h>
8 
9 #include "mac80211_if.h"
10 #include "pub.h"
11 #include "main.h"
12 #include "led.h"
13 
14 	/* number of leds */
15 #define  BRCMS_LED_NO		4
16 	/* behavior mask */
17 #define  BRCMS_LED_BEH_MASK	0x7f
18 	/* activelow (polarity) bit */
19 #define  BRCMS_LED_AL_MASK	0x80
20 	/* radio enabled */
21 #define  BRCMS_LED_RADIO	3
22 
23 static void brcms_radio_led_ctrl(struct brcms_info *wl, bool state)
24 {
25 	if (!wl->radio_led.gpiod)
26 		return;
27 
28 	if (state)
29 		gpiod_set_value(wl->radio_led.gpiod, 1);
30 	else
31 		gpiod_set_value(wl->radio_led.gpiod, 0);
32 }
33 
34 
35 /* Callback from the LED subsystem. */
36 static void brcms_led_brightness_set(struct led_classdev *led_dev,
37 				   enum led_brightness brightness)
38 {
39 	struct brcms_info *wl = container_of(led_dev,
40 		struct brcms_info, led_dev);
41 	brcms_radio_led_ctrl(wl, brightness);
42 }
43 
44 void brcms_led_unregister(struct brcms_info *wl)
45 {
46 	if (wl->led_dev.dev)
47 		led_classdev_unregister(&wl->led_dev);
48 	if (wl->radio_led.gpiod)
49 		gpiochip_free_own_desc(wl->radio_led.gpiod);
50 }
51 
52 int brcms_led_register(struct brcms_info *wl)
53 {
54 	int i, err;
55 	struct brcms_led *radio_led = &wl->radio_led;
56 	/* get CC core */
57 	struct bcma_drv_cc *cc_drv  = &wl->wlc->hw->d11core->bus->drv_cc;
58 	struct gpio_chip *bcma_gpio = &cc_drv->gpio;
59 	struct ssb_sprom *sprom = &wl->wlc->hw->d11core->bus->sprom;
60 	u8 *leds[] = { &sprom->gpio0,
61 		&sprom->gpio1,
62 		&sprom->gpio2,
63 		&sprom->gpio3 };
64 	int hwnum = -1;
65 	enum gpio_lookup_flags lflags = GPIO_ACTIVE_HIGH;
66 
67 	/* find radio enabled LED */
68 	for (i = 0; i < BRCMS_LED_NO; i++) {
69 		u8 led = *leds[i];
70 		if ((led & BRCMS_LED_BEH_MASK) == BRCMS_LED_RADIO) {
71 			hwnum = i;
72 			if (led & BRCMS_LED_AL_MASK)
73 				lflags = GPIO_ACTIVE_LOW;
74 			break;
75 		}
76 	}
77 
78 	/* No LED, bail out */
79 	if (hwnum == -1)
80 		return -ENODEV;
81 
82 	/* Try to obtain this LED GPIO line */
83 	radio_led->gpiod = gpiochip_request_own_desc(bcma_gpio, hwnum,
84 						     "radio on", lflags,
85 						     GPIOD_OUT_LOW);
86 
87 	if (IS_ERR(radio_led->gpiod)) {
88 		err = PTR_ERR(radio_led->gpiod);
89 		wiphy_err(wl->wiphy, "requesting led GPIO failed (err: %d)\n",
90 			  err);
91 		return err;
92 	}
93 
94 	snprintf(wl->radio_led.name, sizeof(wl->radio_led.name),
95 		 "brcmsmac-%s:radio", wiphy_name(wl->wiphy));
96 
97 	wl->led_dev.name = wl->radio_led.name;
98 	wl->led_dev.default_trigger =
99 		ieee80211_get_radio_led_name(wl->pub->ieee_hw);
100 	wl->led_dev.brightness_set = brcms_led_brightness_set;
101 	err = led_classdev_register(wiphy_dev(wl->wiphy), &wl->led_dev);
102 
103 	if (err) {
104 		wiphy_err(wl->wiphy, "cannot register led device: %s (err: %d)\n",
105 			  wl->radio_led.name, err);
106 		return err;
107 	}
108 
109 	wiphy_info(wl->wiphy, "registered radio enabled led device: %s\n",
110 		   wl->radio_led.name);
111 
112 	return 0;
113 }
114