1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright (C) 2024 Intel Corporation
4  */
5 #include <linux/leds.h>
6 #include <net/mac80211.h>
7 
8 #include "fw/api/led.h"
9 #include "mld.h"
10 #include "led.h"
11 #include "hcmd.h"
12 
iwl_mld_send_led_fw_cmd(struct iwl_mld * mld,bool on)13 static void iwl_mld_send_led_fw_cmd(struct iwl_mld *mld, bool on)
14 {
15 	struct iwl_led_cmd led_cmd = {
16 		.status = cpu_to_le32(on),
17 	};
18 	int err;
19 
20 	if (WARN_ON(!mld->fw_status.running))
21 		return;
22 
23 	err = iwl_mld_send_cmd_with_flags_pdu(mld, WIDE_ID(LONG_GROUP,
24 							   LEDS_CMD),
25 					      CMD_ASYNC, &led_cmd);
26 
27 	if (err)
28 		IWL_WARN(mld, "LED command failed: %d\n", err);
29 }
30 
iwl_led_brightness_set(struct led_classdev * led_cdev,enum led_brightness brightness)31 static void iwl_led_brightness_set(struct led_classdev *led_cdev,
32 				   enum led_brightness brightness)
33 {
34 	struct iwl_mld *mld = container_of(led_cdev, struct iwl_mld, led);
35 
36 	if (!mld->fw_status.running)
37 		return;
38 
39 	iwl_mld_send_led_fw_cmd(mld, brightness > 0);
40 }
41 
iwl_mld_leds_init(struct iwl_mld * mld)42 int iwl_mld_leds_init(struct iwl_mld *mld)
43 {
44 	int mode = iwlwifi_mod_params.led_mode;
45 	int ret;
46 
47 	switch (mode) {
48 	case IWL_LED_BLINK:
49 		IWL_ERR(mld, "Blink led mode not supported, used default\n");
50 		fallthrough;
51 	case IWL_LED_DEFAULT:
52 	case IWL_LED_RF_STATE:
53 		mode = IWL_LED_RF_STATE;
54 		break;
55 	case IWL_LED_DISABLE:
56 		IWL_INFO(mld, "Led disabled\n");
57 		return 0;
58 	default:
59 		return -EINVAL;
60 	}
61 
62 	mld->led.name = kasprintf(GFP_KERNEL, "%s-led",
63 				  wiphy_name(mld->hw->wiphy));
64 	if (!mld->led.name)
65 		return -ENOMEM;
66 
67 	mld->led.brightness_set = iwl_led_brightness_set;
68 	mld->led.max_brightness = 1;
69 
70 	if (mode == IWL_LED_RF_STATE)
71 		mld->led.default_trigger =
72 			ieee80211_get_radio_led_name(mld->hw);
73 
74 	ret = led_classdev_register(mld->trans->dev, &mld->led);
75 	if (ret) {
76 		kfree(mld->led.name);
77 		mld->led.name = NULL;
78 		IWL_INFO(mld, "Failed to enable led\n");
79 	}
80 
81 	return ret;
82 }
83 
iwl_mld_led_config_fw(struct iwl_mld * mld)84 void iwl_mld_led_config_fw(struct iwl_mld *mld)
85 {
86 	if (!mld->led.name)
87 		return;
88 
89 	iwl_mld_send_led_fw_cmd(mld, mld->led.brightness > 0);
90 }
91 
iwl_mld_leds_exit(struct iwl_mld * mld)92 void iwl_mld_leds_exit(struct iwl_mld *mld)
93 {
94 	if (!mld->led.name)
95 		return;
96 
97 	led_classdev_unregister(&mld->led);
98 	kfree(mld->led.name);
99 	mld->led.name = NULL;
100 }
101