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