1*d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2d3466830SKalle Valo /* 3d3466830SKalle Valo * Common code for mac80211 Prism54 drivers 4d3466830SKalle Valo * 5d3466830SKalle Valo * Copyright (c) 2006, Michael Wu <flamingice@sourmilk.net> 6d3466830SKalle Valo * Copyright (c) 2007-2009, Christian Lamparter <chunkeey@web.de> 7d3466830SKalle Valo * Copyright 2008, Johannes Berg <johannes@sipsolutions.net> 8d3466830SKalle Valo * 9d3466830SKalle Valo * Based on: 10d3466830SKalle Valo * - the islsm (softmac prism54) driver, which is: 11d3466830SKalle Valo * Copyright 2004-2006 Jean-Baptiste Note <jbnote@gmail.com>, et al. 12d3466830SKalle Valo * - stlc45xx driver 13d3466830SKalle Valo * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies). 14d3466830SKalle Valo */ 15d3466830SKalle Valo 16d3466830SKalle Valo #include <linux/firmware.h> 17d3466830SKalle Valo #include <linux/etherdevice.h> 18d3466830SKalle Valo 19d3466830SKalle Valo #include <net/mac80211.h> 20d3466830SKalle Valo #ifdef CONFIG_P54_LEDS 21d3466830SKalle Valo #include <linux/leds.h> 22d3466830SKalle Valo #endif /* CONFIG_P54_LEDS */ 23d3466830SKalle Valo 24d3466830SKalle Valo #include "p54.h" 25d3466830SKalle Valo #include "lmac.h" 26d3466830SKalle Valo 27d3466830SKalle Valo static void p54_update_leds(struct work_struct *work) 28d3466830SKalle Valo { 29d3466830SKalle Valo struct p54_common *priv = container_of(work, struct p54_common, 30d3466830SKalle Valo led_work.work); 31d3466830SKalle Valo int err, i, tmp, blink_delay = 400; 32d3466830SKalle Valo bool rerun = false; 33d3466830SKalle Valo 34d3466830SKalle Valo /* Don't toggle the LED, when the device is down. */ 35d3466830SKalle Valo if (priv->mode == NL80211_IFTYPE_UNSPECIFIED) 36d3466830SKalle Valo return ; 37d3466830SKalle Valo 38d3466830SKalle Valo for (i = 0; i < ARRAY_SIZE(priv->leds); i++) 39d3466830SKalle Valo if (priv->leds[i].toggled) { 40d3466830SKalle Valo priv->softled_state |= BIT(i); 41d3466830SKalle Valo 42d3466830SKalle Valo tmp = 70 + 200 / (priv->leds[i].toggled); 43d3466830SKalle Valo if (tmp < blink_delay) 44d3466830SKalle Valo blink_delay = tmp; 45d3466830SKalle Valo 46d3466830SKalle Valo if (priv->leds[i].led_dev.brightness == LED_OFF) 47d3466830SKalle Valo rerun = true; 48d3466830SKalle Valo 49d3466830SKalle Valo priv->leds[i].toggled = 50d3466830SKalle Valo !!priv->leds[i].led_dev.brightness; 51d3466830SKalle Valo } else 52d3466830SKalle Valo priv->softled_state &= ~BIT(i); 53d3466830SKalle Valo 54d3466830SKalle Valo err = p54_set_leds(priv); 55d3466830SKalle Valo if (err && net_ratelimit()) 56d3466830SKalle Valo wiphy_err(priv->hw->wiphy, 57d3466830SKalle Valo "failed to update LEDs (%d).\n", err); 58d3466830SKalle Valo 59d3466830SKalle Valo if (rerun) 60d3466830SKalle Valo ieee80211_queue_delayed_work(priv->hw, &priv->led_work, 61d3466830SKalle Valo msecs_to_jiffies(blink_delay)); 62d3466830SKalle Valo } 63d3466830SKalle Valo 64d3466830SKalle Valo static void p54_led_brightness_set(struct led_classdev *led_dev, 65d3466830SKalle Valo enum led_brightness brightness) 66d3466830SKalle Valo { 67d3466830SKalle Valo struct p54_led_dev *led = container_of(led_dev, struct p54_led_dev, 68d3466830SKalle Valo led_dev); 69d3466830SKalle Valo struct ieee80211_hw *dev = led->hw_dev; 70d3466830SKalle Valo struct p54_common *priv = dev->priv; 71d3466830SKalle Valo 72d3466830SKalle Valo if (priv->mode == NL80211_IFTYPE_UNSPECIFIED) 73d3466830SKalle Valo return ; 74d3466830SKalle Valo 75d3466830SKalle Valo if ((brightness) && (led->registered)) { 76d3466830SKalle Valo led->toggled++; 77d3466830SKalle Valo ieee80211_queue_delayed_work(priv->hw, &priv->led_work, HZ/10); 78d3466830SKalle Valo } 79d3466830SKalle Valo } 80d3466830SKalle Valo 81d3466830SKalle Valo static int p54_register_led(struct p54_common *priv, 82d3466830SKalle Valo unsigned int led_index, 83d3466830SKalle Valo char *name, const char *trigger) 84d3466830SKalle Valo { 85d3466830SKalle Valo struct p54_led_dev *led = &priv->leds[led_index]; 86d3466830SKalle Valo int err; 87d3466830SKalle Valo 88d3466830SKalle Valo if (led->registered) 89d3466830SKalle Valo return -EEXIST; 90d3466830SKalle Valo 91d3466830SKalle Valo snprintf(led->name, sizeof(led->name), "p54-%s::%s", 92d3466830SKalle Valo wiphy_name(priv->hw->wiphy), name); 93d3466830SKalle Valo led->hw_dev = priv->hw; 94d3466830SKalle Valo led->index = led_index; 95d3466830SKalle Valo led->led_dev.name = led->name; 96d3466830SKalle Valo led->led_dev.default_trigger = trigger; 97d3466830SKalle Valo led->led_dev.brightness_set = p54_led_brightness_set; 98d3466830SKalle Valo 99d3466830SKalle Valo err = led_classdev_register(wiphy_dev(priv->hw->wiphy), &led->led_dev); 100d3466830SKalle Valo if (err) 101d3466830SKalle Valo wiphy_err(priv->hw->wiphy, 102d3466830SKalle Valo "Failed to register %s LED.\n", name); 103d3466830SKalle Valo else 104d3466830SKalle Valo led->registered = 1; 105d3466830SKalle Valo 106d3466830SKalle Valo return err; 107d3466830SKalle Valo } 108d3466830SKalle Valo 109d3466830SKalle Valo int p54_init_leds(struct p54_common *priv) 110d3466830SKalle Valo { 111d3466830SKalle Valo int err; 112d3466830SKalle Valo 113d3466830SKalle Valo /* 114d3466830SKalle Valo * TODO: 115d3466830SKalle Valo * Figure out if the EEPROM contains some hints about the number 116d3466830SKalle Valo * of available/programmable LEDs of the device. 117d3466830SKalle Valo */ 118d3466830SKalle Valo 119d3466830SKalle Valo INIT_DELAYED_WORK(&priv->led_work, p54_update_leds); 120d3466830SKalle Valo 121d3466830SKalle Valo err = p54_register_led(priv, 0, "assoc", 122d3466830SKalle Valo ieee80211_get_assoc_led_name(priv->hw)); 123d3466830SKalle Valo if (err) 124d3466830SKalle Valo return err; 125d3466830SKalle Valo 126d3466830SKalle Valo err = p54_register_led(priv, 1, "tx", 127d3466830SKalle Valo ieee80211_get_tx_led_name(priv->hw)); 128d3466830SKalle Valo if (err) 129d3466830SKalle Valo return err; 130d3466830SKalle Valo 131d3466830SKalle Valo err = p54_register_led(priv, 2, "rx", 132d3466830SKalle Valo ieee80211_get_rx_led_name(priv->hw)); 133d3466830SKalle Valo if (err) 134d3466830SKalle Valo return err; 135d3466830SKalle Valo 136d3466830SKalle Valo err = p54_register_led(priv, 3, "radio", 137d3466830SKalle Valo ieee80211_get_radio_led_name(priv->hw)); 138d3466830SKalle Valo if (err) 139d3466830SKalle Valo return err; 140d3466830SKalle Valo 141d3466830SKalle Valo err = p54_set_leds(priv); 142d3466830SKalle Valo return err; 143d3466830SKalle Valo } 144d3466830SKalle Valo 145d3466830SKalle Valo void p54_unregister_leds(struct p54_common *priv) 146d3466830SKalle Valo { 147d3466830SKalle Valo int i; 148d3466830SKalle Valo 149d3466830SKalle Valo for (i = 0; i < ARRAY_SIZE(priv->leds); i++) { 150d3466830SKalle Valo if (priv->leds[i].registered) { 151d3466830SKalle Valo priv->leds[i].registered = false; 152d3466830SKalle Valo priv->leds[i].toggled = 0; 153d3466830SKalle Valo led_classdev_unregister(&priv->leds[i].led_dev); 154d3466830SKalle Valo } 155d3466830SKalle Valo } 156d3466830SKalle Valo 157d3466830SKalle Valo cancel_delayed_work_sync(&priv->led_work); 158d3466830SKalle Valo } 159