1 // SPDX-License-Identifier: GPL-2.0-only
3 * Linux LED driver for RTL8187
5 * Copyright 2009 Larry Finger <Larry.Finger@lwfinger.net>
7 * Based on the LED handling in the r8187 driver, which is:
8 * Copyright (c) Realtek Semiconductor Corp. All rights reserved.
10 * Thanks to Realtek for their support!
13 #ifdef CONFIG_RTL8187_LEDS
15 #include <net/mac80211.h>
16 #include <linux/usb.h>
17 #include <linux/eeprom_93cx6.h>
22 static void led_turn_on(struct work_struct *work)
24 /* As this routine does read/write operations on the hardware, it must
25 * be run from a work queue.
28 struct rtl8187_priv *priv = container_of(work, struct rtl8187_priv,
30 struct rtl8187_led *led = &priv->led_tx;
32 /* Don't change the LED, when the device is down. */
33 if (!priv->vif || priv->vif->type == NL80211_IFTYPE_UNSPECIFIED)
36 /* Skip if the LED is not registered. */
39 mutex_lock(&priv->conf_mutex);
40 switch (led->ledpin) {
42 rtl818x_iowrite8(priv, &priv->map->GPIO0, 0x01);
43 rtl818x_iowrite8(priv, &priv->map->GP_ENABLE, 0x00);
46 reg = rtl818x_ioread8(priv, &priv->map->PGSELECT) & ~(1 << 4);
47 rtl818x_iowrite8(priv, &priv->map->PGSELECT, reg);
50 reg = rtl818x_ioread8(priv, &priv->map->PGSELECT) & ~(1 << 5);
51 rtl818x_iowrite8(priv, &priv->map->PGSELECT, reg);
57 mutex_unlock(&priv->conf_mutex);
60 static void led_turn_off(struct work_struct *work)
62 /* As this routine does read/write operations on the hardware, it must
63 * be run from a work queue.
66 struct rtl8187_priv *priv = container_of(work, struct rtl8187_priv,
68 struct rtl8187_led *led = &priv->led_tx;
70 /* Don't change the LED, when the device is down. */
71 if (!priv->vif || priv->vif->type == NL80211_IFTYPE_UNSPECIFIED)
74 /* Skip if the LED is not registered. */
77 mutex_lock(&priv->conf_mutex);
78 switch (led->ledpin) {
80 rtl818x_iowrite8(priv, &priv->map->GPIO0, 0x01);
81 rtl818x_iowrite8(priv, &priv->map->GP_ENABLE, 0x01);
84 reg = rtl818x_ioread8(priv, &priv->map->PGSELECT) | (1 << 4);
85 rtl818x_iowrite8(priv, &priv->map->PGSELECT, reg);
88 reg = rtl818x_ioread8(priv, &priv->map->PGSELECT) | (1 << 5);
89 rtl818x_iowrite8(priv, &priv->map->PGSELECT, reg);
95 mutex_unlock(&priv->conf_mutex);
98 /* Callback from the LED subsystem. */
99 static void rtl8187_led_brightness_set(struct led_classdev *led_dev,
100 enum led_brightness brightness)
102 struct rtl8187_led *led = container_of(led_dev, struct rtl8187_led,
104 struct ieee80211_hw *hw = led->dev;
105 struct rtl8187_priv *priv;
106 static bool radio_on;
112 if (brightness == LED_FULL) {
113 ieee80211_queue_delayed_work(hw, &priv->led_on, 0);
115 } else if (radio_on) {
117 cancel_delayed_work(&priv->led_on);
118 ieee80211_queue_delayed_work(hw, &priv->led_off, 0);
120 } else if (radio_on) {
121 if (brightness == LED_OFF) {
122 ieee80211_queue_delayed_work(hw, &priv->led_off, 0);
123 /* The LED is off for 1/20 sec - it just blinks. */
124 ieee80211_queue_delayed_work(hw, &priv->led_on,
127 ieee80211_queue_delayed_work(hw, &priv->led_on, 0);
131 static int rtl8187_register_led(struct ieee80211_hw *dev,
132 struct rtl8187_led *led, const char *name,
133 const char *default_trigger, u8 ledpin,
137 struct rtl8187_priv *priv = dev->priv;
141 if (!default_trigger)
144 led->ledpin = ledpin;
145 led->is_radio = is_radio;
146 strlcpy(led->name, name, sizeof(led->name));
148 led->led_dev.name = led->name;
149 led->led_dev.default_trigger = default_trigger;
150 led->led_dev.brightness_set = rtl8187_led_brightness_set;
152 err = led_classdev_register(&priv->udev->dev, &led->led_dev);
154 printk(KERN_INFO "LEDs: Failed to register %s\n", name);
161 static void rtl8187_unregister_led(struct rtl8187_led *led)
163 struct ieee80211_hw *hw = led->dev;
164 struct rtl8187_priv *priv = hw->priv;
166 led_classdev_unregister(&led->led_dev);
167 flush_delayed_work(&priv->led_off);
171 void rtl8187_leds_init(struct ieee80211_hw *dev, u16 custid)
173 struct rtl8187_priv *priv = dev->priv;
174 char name[RTL8187_LED_MAX_NAME_LEN + 1];
178 /* According to the vendor driver, the LED operation depends on the
179 * customer ID encoded in the EEPROM
181 printk(KERN_INFO "rtl8187: Customer ID is 0x%02X\n", custid);
183 case EEPROM_CID_RSVD0:
184 case EEPROM_CID_RSVD1:
185 case EEPROM_CID_SERCOMM_PS:
187 case EEPROM_CID_DELL:
188 case EEPROM_CID_TOSHIBA:
189 ledpin = LED_PIN_GPIO0;
191 case EEPROM_CID_ALPHA0:
192 ledpin = LED_PIN_LED0;
198 ledpin = LED_PIN_GPIO0;
201 INIT_DELAYED_WORK(&priv->led_on, led_turn_on);
202 INIT_DELAYED_WORK(&priv->led_off, led_turn_off);
204 snprintf(name, sizeof(name),
205 "rtl8187-%s::radio", wiphy_name(dev->wiphy));
206 err = rtl8187_register_led(dev, &priv->led_radio, name,
207 ieee80211_get_radio_led_name(dev), ledpin, true);
211 snprintf(name, sizeof(name),
212 "rtl8187-%s::tx", wiphy_name(dev->wiphy));
213 err = rtl8187_register_led(dev, &priv->led_tx, name,
214 ieee80211_get_tx_led_name(dev), ledpin, false);
218 snprintf(name, sizeof(name),
219 "rtl8187-%s::rx", wiphy_name(dev->wiphy));
220 err = rtl8187_register_led(dev, &priv->led_rx, name,
221 ieee80211_get_rx_led_name(dev), ledpin, false);
225 /* registration of RX LED failed - unregister */
226 rtl8187_unregister_led(&priv->led_tx);
228 rtl8187_unregister_led(&priv->led_radio);
231 void rtl8187_leds_exit(struct ieee80211_hw *dev)
233 struct rtl8187_priv *priv = dev->priv;
235 rtl8187_unregister_led(&priv->led_radio);
236 rtl8187_unregister_led(&priv->led_rx);
237 rtl8187_unregister_led(&priv->led_tx);
238 cancel_delayed_work_sync(&priv->led_off);
239 cancel_delayed_work_sync(&priv->led_on);
241 #endif /* def CONFIG_RTL8187_LEDS */