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