18c2ecf20Sopenharmony_ci/******************************************************************************
28c2ecf20Sopenharmony_ci *
38c2ecf20Sopenharmony_ci * This file is provided under a dual BSD/GPLv2 license.  When using or
48c2ecf20Sopenharmony_ci * redistributing this file, you may do so under either license.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * GPL LICENSE SUMMARY
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
98c2ecf20Sopenharmony_ci * Copyright(c) 2017        Intel Deutschland GmbH
108c2ecf20Sopenharmony_ci * Copyright(c) 2018 - 2019 Intel Corporation
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or modify
138c2ecf20Sopenharmony_ci * it under the terms of version 2 of the GNU General Public License as
148c2ecf20Sopenharmony_ci * published by the Free Software Foundation.
158c2ecf20Sopenharmony_ci *
168c2ecf20Sopenharmony_ci * This program is distributed in the hope that it will be useful, but
178c2ecf20Sopenharmony_ci * WITHOUT ANY WARRANTY; without even the implied warranty of
188c2ecf20Sopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
198c2ecf20Sopenharmony_ci * General Public License for more details.
208c2ecf20Sopenharmony_ci *
218c2ecf20Sopenharmony_ci * The full GNU General Public License is included in this distribution
228c2ecf20Sopenharmony_ci * in the file called COPYING.
238c2ecf20Sopenharmony_ci *
248c2ecf20Sopenharmony_ci * Contact Information:
258c2ecf20Sopenharmony_ci *  Intel Linux Wireless <linuxwifi@intel.com>
268c2ecf20Sopenharmony_ci * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
278c2ecf20Sopenharmony_ci *
288c2ecf20Sopenharmony_ci * BSD LICENSE
298c2ecf20Sopenharmony_ci *
308c2ecf20Sopenharmony_ci * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
318c2ecf20Sopenharmony_ci * Copyright(c) 2017        Intel Deutschland GmbH
328c2ecf20Sopenharmony_ci * Copyright(c) 2018 - 2019 Intel Corporation
338c2ecf20Sopenharmony_ci * All rights reserved.
348c2ecf20Sopenharmony_ci *
358c2ecf20Sopenharmony_ci * Redistribution and use in source and binary forms, with or without
368c2ecf20Sopenharmony_ci * modification, are permitted provided that the following conditions
378c2ecf20Sopenharmony_ci * are met:
388c2ecf20Sopenharmony_ci *
398c2ecf20Sopenharmony_ci *  * Redistributions of source code must retain the above copyright
408c2ecf20Sopenharmony_ci *    notice, this list of conditions and the following disclaimer.
418c2ecf20Sopenharmony_ci *  * Redistributions in binary form must reproduce the above copyright
428c2ecf20Sopenharmony_ci *    notice, this list of conditions and the following disclaimer in
438c2ecf20Sopenharmony_ci *    the documentation and/or other materials provided with the
448c2ecf20Sopenharmony_ci *    distribution.
458c2ecf20Sopenharmony_ci *  * Neither the name Intel Corporation nor the names of its
468c2ecf20Sopenharmony_ci *    contributors may be used to endorse or promote products derived
478c2ecf20Sopenharmony_ci *    from this software without specific prior written permission.
488c2ecf20Sopenharmony_ci *
498c2ecf20Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
508c2ecf20Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
518c2ecf20Sopenharmony_ci * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
528c2ecf20Sopenharmony_ci * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
538c2ecf20Sopenharmony_ci * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
548c2ecf20Sopenharmony_ci * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
558c2ecf20Sopenharmony_ci * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
568c2ecf20Sopenharmony_ci * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
578c2ecf20Sopenharmony_ci * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
588c2ecf20Sopenharmony_ci * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
598c2ecf20Sopenharmony_ci * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
608c2ecf20Sopenharmony_ci *
618c2ecf20Sopenharmony_ci *****************************************************************************/
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci#include <linux/leds.h>
648c2ecf20Sopenharmony_ci#include "iwl-io.h"
658c2ecf20Sopenharmony_ci#include "iwl-csr.h"
668c2ecf20Sopenharmony_ci#include "mvm.h"
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_cistatic void iwl_mvm_send_led_fw_cmd(struct iwl_mvm *mvm, bool on)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	struct iwl_led_cmd led_cmd = {
718c2ecf20Sopenharmony_ci		.status = cpu_to_le32(on),
728c2ecf20Sopenharmony_ci	};
738c2ecf20Sopenharmony_ci	struct iwl_host_cmd cmd = {
748c2ecf20Sopenharmony_ci		.id = WIDE_ID(LONG_GROUP, LEDS_CMD),
758c2ecf20Sopenharmony_ci		.len = { sizeof(led_cmd), },
768c2ecf20Sopenharmony_ci		.data = { &led_cmd, },
778c2ecf20Sopenharmony_ci		.flags = CMD_ASYNC,
788c2ecf20Sopenharmony_ci	};
798c2ecf20Sopenharmony_ci	int err;
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	if (!iwl_mvm_firmware_running(mvm))
828c2ecf20Sopenharmony_ci		return;
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	err = iwl_mvm_send_cmd(mvm, &cmd);
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	if (err)
878c2ecf20Sopenharmony_ci		IWL_WARN(mvm, "LED command failed: %d\n", err);
888c2ecf20Sopenharmony_ci}
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_cistatic void iwl_mvm_led_set(struct iwl_mvm *mvm, bool on)
918c2ecf20Sopenharmony_ci{
928c2ecf20Sopenharmony_ci	if (fw_has_capa(&mvm->fw->ucode_capa,
938c2ecf20Sopenharmony_ci			IWL_UCODE_TLV_CAPA_LED_CMD_SUPPORT)) {
948c2ecf20Sopenharmony_ci		iwl_mvm_send_led_fw_cmd(mvm, on);
958c2ecf20Sopenharmony_ci		return;
968c2ecf20Sopenharmony_ci	}
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	iwl_write32(mvm->trans, CSR_LED_REG,
998c2ecf20Sopenharmony_ci		    on ? CSR_LED_REG_TURN_ON : CSR_LED_REG_TURN_OFF);
1008c2ecf20Sopenharmony_ci}
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_cistatic void iwl_led_brightness_set(struct led_classdev *led_cdev,
1038c2ecf20Sopenharmony_ci				   enum led_brightness brightness)
1048c2ecf20Sopenharmony_ci{
1058c2ecf20Sopenharmony_ci	struct iwl_mvm *mvm = container_of(led_cdev, struct iwl_mvm, led);
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	iwl_mvm_led_set(mvm, brightness > 0);
1088c2ecf20Sopenharmony_ci}
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ciint iwl_mvm_leds_init(struct iwl_mvm *mvm)
1118c2ecf20Sopenharmony_ci{
1128c2ecf20Sopenharmony_ci	int mode = iwlwifi_mod_params.led_mode;
1138c2ecf20Sopenharmony_ci	int ret;
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	switch (mode) {
1168c2ecf20Sopenharmony_ci	case IWL_LED_BLINK:
1178c2ecf20Sopenharmony_ci		IWL_ERR(mvm, "Blink led mode not supported, used default\n");
1188c2ecf20Sopenharmony_ci		/* fall through */
1198c2ecf20Sopenharmony_ci	case IWL_LED_DEFAULT:
1208c2ecf20Sopenharmony_ci	case IWL_LED_RF_STATE:
1218c2ecf20Sopenharmony_ci		mode = IWL_LED_RF_STATE;
1228c2ecf20Sopenharmony_ci		break;
1238c2ecf20Sopenharmony_ci	case IWL_LED_DISABLE:
1248c2ecf20Sopenharmony_ci		IWL_INFO(mvm, "Led disabled\n");
1258c2ecf20Sopenharmony_ci		return 0;
1268c2ecf20Sopenharmony_ci	default:
1278c2ecf20Sopenharmony_ci		return -EINVAL;
1288c2ecf20Sopenharmony_ci	}
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	mvm->led.name = kasprintf(GFP_KERNEL, "%s-led",
1318c2ecf20Sopenharmony_ci				   wiphy_name(mvm->hw->wiphy));
1328c2ecf20Sopenharmony_ci	if (!mvm->led.name)
1338c2ecf20Sopenharmony_ci		return -ENOMEM;
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	mvm->led.brightness_set = iwl_led_brightness_set;
1368c2ecf20Sopenharmony_ci	mvm->led.max_brightness = 1;
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	if (mode == IWL_LED_RF_STATE)
1398c2ecf20Sopenharmony_ci		mvm->led.default_trigger =
1408c2ecf20Sopenharmony_ci			ieee80211_get_radio_led_name(mvm->hw);
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	ret = led_classdev_register(mvm->trans->dev, &mvm->led);
1438c2ecf20Sopenharmony_ci	if (ret) {
1448c2ecf20Sopenharmony_ci		kfree(mvm->led.name);
1458c2ecf20Sopenharmony_ci		IWL_INFO(mvm, "Failed to enable led\n");
1468c2ecf20Sopenharmony_ci		return ret;
1478c2ecf20Sopenharmony_ci	}
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	mvm->init_status |= IWL_MVM_INIT_STATUS_LEDS_INIT_COMPLETE;
1508c2ecf20Sopenharmony_ci	return 0;
1518c2ecf20Sopenharmony_ci}
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_civoid iwl_mvm_leds_sync(struct iwl_mvm *mvm)
1548c2ecf20Sopenharmony_ci{
1558c2ecf20Sopenharmony_ci	if (!(mvm->init_status & IWL_MVM_INIT_STATUS_LEDS_INIT_COMPLETE))
1568c2ecf20Sopenharmony_ci		return;
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	/*
1598c2ecf20Sopenharmony_ci	 * if we control through the register, we're doing it
1608c2ecf20Sopenharmony_ci	 * even when the firmware isn't up, so no need to sync
1618c2ecf20Sopenharmony_ci	 */
1628c2ecf20Sopenharmony_ci	if (mvm->trans->trans_cfg->device_family < IWL_DEVICE_FAMILY_8000)
1638c2ecf20Sopenharmony_ci		return;
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	iwl_mvm_led_set(mvm, mvm->led.brightness > 0);
1668c2ecf20Sopenharmony_ci}
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_civoid iwl_mvm_leds_exit(struct iwl_mvm *mvm)
1698c2ecf20Sopenharmony_ci{
1708c2ecf20Sopenharmony_ci	if (!(mvm->init_status & IWL_MVM_INIT_STATUS_LEDS_INIT_COMPLETE))
1718c2ecf20Sopenharmony_ci		return;
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	led_classdev_unregister(&mvm->led);
1748c2ecf20Sopenharmony_ci	kfree(mvm->led.name);
1758c2ecf20Sopenharmony_ci	mvm->init_status &= ~IWL_MVM_INIT_STATUS_LEDS_INIT_COMPLETE;
1768c2ecf20Sopenharmony_ci}
177