1 // SPDX-License-Identifier: GPL-2.0+
2 /* Copyright (C) 2016 National Instruments Corp. */
3 #include <linux/leds.h>
4 #include <linux/phy.h>
5 #include <linux/phy_led_triggers.h>
6 #include <linux/netdevice.h>
7 
8 static struct phy_led_trigger *phy_speed_to_led_trigger(struct phy_device *phy,
9 							unsigned int speed)
10 {
11 	unsigned int i;
12 
13 	for (i = 0; i < phy->phy_num_led_triggers; i++) {
14 		if (phy->phy_led_triggers[i].speed == speed)
15 			return &phy->phy_led_triggers[i];
16 	}
17 	return NULL;
18 }
19 
20 static void phy_led_trigger_no_link(struct phy_device *phy)
21 {
22 	if (phy->last_triggered) {
23 		led_trigger_event(&phy->last_triggered->trigger, LED_OFF);
24 		led_trigger_event(&phy->led_link_trigger->trigger, LED_OFF);
25 		phy->last_triggered = NULL;
26 	}
27 }
28 
29 void phy_led_trigger_change_speed(struct phy_device *phy)
30 {
31 	struct phy_led_trigger *plt;
32 
33 	if (!phy->link)
34 		return phy_led_trigger_no_link(phy);
35 
36 	if (phy->speed == 0)
37 		return;
38 
39 	plt = phy_speed_to_led_trigger(phy, phy->speed);
40 	if (!plt) {
41 		netdev_alert(phy->attached_dev,
42 			     "No phy led trigger registered for speed(%d)\n",
43 			     phy->speed);
44 		return phy_led_trigger_no_link(phy);
45 	}
46 
47 	if (plt != phy->last_triggered) {
48 		if (!phy->last_triggered)
49 			led_trigger_event(&phy->led_link_trigger->trigger,
50 					  LED_FULL);
51 		else
52 			led_trigger_event(&phy->last_triggered->trigger, LED_OFF);
53 
54 		led_trigger_event(&plt->trigger, LED_FULL);
55 		phy->last_triggered = plt;
56 	}
57 }
58 EXPORT_SYMBOL_GPL(phy_led_trigger_change_speed);
59 
60 static void phy_led_trigger_format_name(struct phy_device *phy, char *buf,
61 					size_t size, const char *suffix)
62 {
63 	snprintf(buf, size, PHY_ID_FMT ":%s",
64 		 phy->mdio.bus->id, phy->mdio.addr, suffix);
65 }
66 
67 static int phy_led_trigger_register(struct phy_device *phy,
68 				    struct phy_led_trigger *plt,
69 				    unsigned int speed)
70 {
71 	plt->speed = speed;
72 	phy_led_trigger_format_name(phy, plt->name, sizeof(plt->name),
73 				    phy_speed_to_str(speed));
74 	plt->trigger.name = plt->name;
75 
76 	return led_trigger_register(&plt->trigger);
77 }
78 
79 static void phy_led_trigger_unregister(struct phy_led_trigger *plt)
80 {
81 	led_trigger_unregister(&plt->trigger);
82 }
83 
84 int phy_led_triggers_register(struct phy_device *phy)
85 {
86 	int i, err;
87 	unsigned int speeds[50];
88 
89 	phy->phy_num_led_triggers = phy_supported_speeds(phy, speeds,
90 							 ARRAY_SIZE(speeds));
91 	if (!phy->phy_num_led_triggers)
92 		return 0;
93 
94 	phy->led_link_trigger = devm_kzalloc(&phy->mdio.dev,
95 					     sizeof(*phy->led_link_trigger),
96 					     GFP_KERNEL);
97 	if (!phy->led_link_trigger) {
98 		err = -ENOMEM;
99 		goto out_clear;
100 	}
101 
102 	phy_led_trigger_format_name(phy, phy->led_link_trigger->name,
103 				    sizeof(phy->led_link_trigger->name),
104 				    "link");
105 	phy->led_link_trigger->trigger.name = phy->led_link_trigger->name;
106 
107 	err = led_trigger_register(&phy->led_link_trigger->trigger);
108 	if (err)
109 		goto out_free_link;
110 
111 	phy->phy_led_triggers = devm_kcalloc(&phy->mdio.dev,
112 					    phy->phy_num_led_triggers,
113 					    sizeof(struct phy_led_trigger),
114 					    GFP_KERNEL);
115 	if (!phy->phy_led_triggers) {
116 		err = -ENOMEM;
117 		goto out_unreg_link;
118 	}
119 
120 	for (i = 0; i < phy->phy_num_led_triggers; i++) {
121 		err = phy_led_trigger_register(phy, &phy->phy_led_triggers[i],
122 					       speeds[i]);
123 		if (err)
124 			goto out_unreg;
125 	}
126 
127 	phy->last_triggered = NULL;
128 	phy_led_trigger_change_speed(phy);
129 
130 	return 0;
131 out_unreg:
132 	while (i--)
133 		phy_led_trigger_unregister(&phy->phy_led_triggers[i]);
134 	devm_kfree(&phy->mdio.dev, phy->phy_led_triggers);
135 out_unreg_link:
136 	phy_led_trigger_unregister(phy->led_link_trigger);
137 out_free_link:
138 	devm_kfree(&phy->mdio.dev, phy->led_link_trigger);
139 	phy->led_link_trigger = NULL;
140 out_clear:
141 	phy->phy_num_led_triggers = 0;
142 	return err;
143 }
144 EXPORT_SYMBOL_GPL(phy_led_triggers_register);
145 
146 void phy_led_triggers_unregister(struct phy_device *phy)
147 {
148 	int i;
149 
150 	for (i = 0; i < phy->phy_num_led_triggers; i++)
151 		phy_led_trigger_unregister(&phy->phy_led_triggers[i]);
152 
153 	if (phy->led_link_trigger)
154 		phy_led_trigger_unregister(phy->led_link_trigger);
155 }
156 EXPORT_SYMBOL_GPL(phy_led_triggers_unregister);
157