1e705c121SKalle Valo /****************************************************************************** 2e705c121SKalle Valo * 3e705c121SKalle Valo * This file is provided under a dual BSD/GPLv2 license. When using or 4e705c121SKalle Valo * redistributing this file, you may do so under either license. 5e705c121SKalle Valo * 6e705c121SKalle Valo * GPL LICENSE SUMMARY 7e705c121SKalle Valo * 8e705c121SKalle Valo * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved. 9*7089ae63SJohannes Berg * Copyright(c) 2017 Intel Deutschland GmbH 10e705c121SKalle Valo * 11e705c121SKalle Valo * This program is free software; you can redistribute it and/or modify 12e705c121SKalle Valo * it under the terms of version 2 of the GNU General Public License as 13e705c121SKalle Valo * published by the Free Software Foundation. 14e705c121SKalle Valo * 15e705c121SKalle Valo * This program is distributed in the hope that it will be useful, but 16e705c121SKalle Valo * WITHOUT ANY WARRANTY; without even the implied warranty of 17e705c121SKalle Valo * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 18e705c121SKalle Valo * General Public License for more details. 19e705c121SKalle Valo * 20e705c121SKalle Valo * You should have received a copy of the GNU General Public License 21e705c121SKalle Valo * along with this program; if not, write to the Free Software 22e705c121SKalle Valo * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, 23e705c121SKalle Valo * USA 24e705c121SKalle Valo * 25e705c121SKalle Valo * The full GNU General Public License is included in this distribution 26e705c121SKalle Valo * in the file called COPYING. 27e705c121SKalle Valo * 28e705c121SKalle Valo * Contact Information: 29d01c5366SEmmanuel Grumbach * Intel Linux Wireless <linuxwifi@intel.com> 30e705c121SKalle Valo * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497 31e705c121SKalle Valo * 32e705c121SKalle Valo * BSD LICENSE 33e705c121SKalle Valo * 34e705c121SKalle Valo * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved. 35*7089ae63SJohannes Berg * Copyright(c) 2017 Intel Deutschland GmbH 36e705c121SKalle Valo * All rights reserved. 37e705c121SKalle Valo * 38e705c121SKalle Valo * Redistribution and use in source and binary forms, with or without 39e705c121SKalle Valo * modification, are permitted provided that the following conditions 40e705c121SKalle Valo * are met: 41e705c121SKalle Valo * 42e705c121SKalle Valo * * Redistributions of source code must retain the above copyright 43e705c121SKalle Valo * notice, this list of conditions and the following disclaimer. 44e705c121SKalle Valo * * Redistributions in binary form must reproduce the above copyright 45e705c121SKalle Valo * notice, this list of conditions and the following disclaimer in 46e705c121SKalle Valo * the documentation and/or other materials provided with the 47e705c121SKalle Valo * distribution. 48e705c121SKalle Valo * * Neither the name Intel Corporation nor the names of its 49e705c121SKalle Valo * contributors may be used to endorse or promote products derived 50e705c121SKalle Valo * from this software without specific prior written permission. 51e705c121SKalle Valo * 52e705c121SKalle Valo * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 53e705c121SKalle Valo * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 54e705c121SKalle Valo * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 55e705c121SKalle Valo * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 56e705c121SKalle Valo * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 57e705c121SKalle Valo * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 58e705c121SKalle Valo * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 59e705c121SKalle Valo * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 60e705c121SKalle Valo * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 61e705c121SKalle Valo * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 62e705c121SKalle Valo * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 63e705c121SKalle Valo * 64e705c121SKalle Valo *****************************************************************************/ 65e705c121SKalle Valo 66e705c121SKalle Valo #include <linux/leds.h> 67e705c121SKalle Valo #include "iwl-io.h" 68e705c121SKalle Valo #include "iwl-csr.h" 69e705c121SKalle Valo #include "mvm.h" 70e705c121SKalle Valo 71*7089ae63SJohannes Berg static void iwl_mvm_send_led_fw_cmd(struct iwl_mvm *mvm, bool on) 72e705c121SKalle Valo { 73*7089ae63SJohannes Berg struct iwl_led_cmd led_cmd = { 74*7089ae63SJohannes Berg .status = cpu_to_le32(on), 75*7089ae63SJohannes Berg }; 76*7089ae63SJohannes Berg struct iwl_host_cmd cmd = { 77*7089ae63SJohannes Berg .id = WIDE_ID(LONG_GROUP, LEDS_CMD), 78*7089ae63SJohannes Berg .len = { sizeof(led_cmd), }, 79*7089ae63SJohannes Berg .data = { &led_cmd, }, 80*7089ae63SJohannes Berg .flags = CMD_ASYNC, 81*7089ae63SJohannes Berg }; 82*7089ae63SJohannes Berg int err; 83*7089ae63SJohannes Berg 84*7089ae63SJohannes Berg if (!iwl_mvm_firmware_running(mvm)) 85*7089ae63SJohannes Berg return; 86*7089ae63SJohannes Berg 87*7089ae63SJohannes Berg err = iwl_mvm_send_cmd(mvm, &cmd); 88*7089ae63SJohannes Berg 89*7089ae63SJohannes Berg if (err) 90*7089ae63SJohannes Berg IWL_WARN(mvm, "LED command failed: %d\n", err); 91e705c121SKalle Valo } 92e705c121SKalle Valo 93*7089ae63SJohannes Berg static void iwl_mvm_led_set(struct iwl_mvm *mvm, bool on) 94e705c121SKalle Valo { 95*7089ae63SJohannes Berg if (mvm->cfg->device_family >= IWL_DEVICE_FAMILY_8000) { 96*7089ae63SJohannes Berg iwl_mvm_send_led_fw_cmd(mvm, on); 97*7089ae63SJohannes Berg return; 98*7089ae63SJohannes Berg } 99*7089ae63SJohannes Berg 100*7089ae63SJohannes Berg iwl_write32(mvm->trans, CSR_LED_REG, 101*7089ae63SJohannes Berg on ? CSR_LED_REG_TURN_ON : CSR_LED_REG_TURN_OFF); 102e705c121SKalle Valo } 103e705c121SKalle Valo 104e705c121SKalle Valo static void iwl_led_brightness_set(struct led_classdev *led_cdev, 105e705c121SKalle Valo enum led_brightness brightness) 106e705c121SKalle Valo { 107e705c121SKalle Valo struct iwl_mvm *mvm = container_of(led_cdev, struct iwl_mvm, led); 108*7089ae63SJohannes Berg 109*7089ae63SJohannes Berg iwl_mvm_led_set(mvm, brightness > 0); 110e705c121SKalle Valo } 111e705c121SKalle Valo 112e705c121SKalle Valo int iwl_mvm_leds_init(struct iwl_mvm *mvm) 113e705c121SKalle Valo { 114e705c121SKalle Valo int mode = iwlwifi_mod_params.led_mode; 115e705c121SKalle Valo int ret; 116e705c121SKalle Valo 117e705c121SKalle Valo switch (mode) { 118e705c121SKalle Valo case IWL_LED_BLINK: 119e705c121SKalle Valo IWL_ERR(mvm, "Blink led mode not supported, used default\n"); 120e705c121SKalle Valo case IWL_LED_DEFAULT: 121e705c121SKalle Valo case IWL_LED_RF_STATE: 122e705c121SKalle Valo mode = IWL_LED_RF_STATE; 123e705c121SKalle Valo break; 124e705c121SKalle Valo case IWL_LED_DISABLE: 125e705c121SKalle Valo IWL_INFO(mvm, "Led disabled\n"); 126e705c121SKalle Valo return 0; 127e705c121SKalle Valo default: 128e705c121SKalle Valo return -EINVAL; 129e705c121SKalle Valo } 130e705c121SKalle Valo 131e705c121SKalle Valo mvm->led.name = kasprintf(GFP_KERNEL, "%s-led", 132e705c121SKalle Valo wiphy_name(mvm->hw->wiphy)); 133e705c121SKalle Valo mvm->led.brightness_set = iwl_led_brightness_set; 134e705c121SKalle Valo mvm->led.max_brightness = 1; 135e705c121SKalle Valo 136e705c121SKalle Valo if (mode == IWL_LED_RF_STATE) 137e705c121SKalle Valo mvm->led.default_trigger = 138e705c121SKalle Valo ieee80211_get_radio_led_name(mvm->hw); 139e705c121SKalle Valo 140e705c121SKalle Valo ret = led_classdev_register(mvm->trans->dev, &mvm->led); 141e705c121SKalle Valo if (ret) { 142e705c121SKalle Valo kfree(mvm->led.name); 143e705c121SKalle Valo IWL_INFO(mvm, "Failed to enable led\n"); 144e705c121SKalle Valo return ret; 145e705c121SKalle Valo } 146e705c121SKalle Valo 147de8ba41bSLiad Kaufman mvm->init_status |= IWL_MVM_INIT_STATUS_LEDS_INIT_COMPLETE; 148e705c121SKalle Valo return 0; 149e705c121SKalle Valo } 150e705c121SKalle Valo 151*7089ae63SJohannes Berg void iwl_mvm_leds_sync(struct iwl_mvm *mvm) 152*7089ae63SJohannes Berg { 153*7089ae63SJohannes Berg if (!(mvm->init_status & IWL_MVM_INIT_STATUS_LEDS_INIT_COMPLETE)) 154*7089ae63SJohannes Berg return; 155*7089ae63SJohannes Berg 156*7089ae63SJohannes Berg /* 157*7089ae63SJohannes Berg * if we control through the register, we're doing it 158*7089ae63SJohannes Berg * even when the firmware isn't up, so no need to sync 159*7089ae63SJohannes Berg */ 160*7089ae63SJohannes Berg if (mvm->cfg->device_family < IWL_DEVICE_FAMILY_8000) 161*7089ae63SJohannes Berg return; 162*7089ae63SJohannes Berg 163*7089ae63SJohannes Berg iwl_mvm_led_set(mvm, mvm->led.brightness > 0); 164*7089ae63SJohannes Berg } 165*7089ae63SJohannes Berg 166e705c121SKalle Valo void iwl_mvm_leds_exit(struct iwl_mvm *mvm) 167e705c121SKalle Valo { 1684aa234eeSJohannes Berg if (!(mvm->init_status & IWL_MVM_INIT_STATUS_LEDS_INIT_COMPLETE)) 169e705c121SKalle Valo return; 170e705c121SKalle Valo 171e705c121SKalle Valo led_classdev_unregister(&mvm->led); 172e705c121SKalle Valo kfree(mvm->led.name); 173de8ba41bSLiad Kaufman mvm->init_status &= ~IWL_MVM_INIT_STATUS_LEDS_INIT_COMPLETE; 174e705c121SKalle Valo } 175