dt-bindings: mtd: ingenic: Use standard ecc-engine property
[linux/fpc-iii.git] / drivers / net / phy / phy_led_triggers.c
blobb86a4b2116f8156af6c32f82a6e8df9bfaa441d0
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>
8 static struct phy_led_trigger *phy_speed_to_led_trigger(struct phy_device *phy,
9 unsigned int speed)
11 unsigned int i;
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];
17 return NULL;
20 static void phy_led_trigger_no_link(struct phy_device *phy)
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;
29 void phy_led_trigger_change_speed(struct phy_device *phy)
31 struct phy_led_trigger *plt;
33 if (!phy->link)
34 return phy_led_trigger_no_link(phy);
36 if (phy->speed == 0)
37 return;
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);
47 if (plt != phy->last_triggered) {
48 if (!phy->last_triggered)
49 led_trigger_event(&phy->led_link_trigger->trigger,
50 LED_FULL);
52 led_trigger_event(&phy->last_triggered->trigger, LED_OFF);
53 led_trigger_event(&plt->trigger, LED_FULL);
54 phy->last_triggered = plt;
57 EXPORT_SYMBOL_GPL(phy_led_trigger_change_speed);
59 static void phy_led_trigger_format_name(struct phy_device *phy, char *buf,
60 size_t size, const char *suffix)
62 snprintf(buf, size, PHY_ID_FMT ":%s",
63 phy->mdio.bus->id, phy->mdio.addr, suffix);
66 static int phy_led_trigger_register(struct phy_device *phy,
67 struct phy_led_trigger *plt,
68 unsigned int speed)
70 plt->speed = speed;
71 phy_led_trigger_format_name(phy, plt->name, sizeof(plt->name),
72 phy_speed_to_str(speed));
73 plt->trigger.name = plt->name;
75 return led_trigger_register(&plt->trigger);
78 static void phy_led_trigger_unregister(struct phy_led_trigger *plt)
80 led_trigger_unregister(&plt->trigger);
83 int phy_led_triggers_register(struct phy_device *phy)
85 int i, err;
86 unsigned int speeds[50];
88 phy->phy_num_led_triggers = phy_supported_speeds(phy, speeds,
89 ARRAY_SIZE(speeds));
90 if (!phy->phy_num_led_triggers)
91 return 0;
93 phy->led_link_trigger = devm_kzalloc(&phy->mdio.dev,
94 sizeof(*phy->led_link_trigger),
95 GFP_KERNEL);
96 if (!phy->led_link_trigger) {
97 err = -ENOMEM;
98 goto out_clear;
101 phy_led_trigger_format_name(phy, phy->led_link_trigger->name,
102 sizeof(phy->led_link_trigger->name),
103 "link");
104 phy->led_link_trigger->trigger.name = phy->led_link_trigger->name;
106 err = led_trigger_register(&phy->led_link_trigger->trigger);
107 if (err)
108 goto out_free_link;
110 phy->phy_led_triggers = devm_kcalloc(&phy->mdio.dev,
111 phy->phy_num_led_triggers,
112 sizeof(struct phy_led_trigger),
113 GFP_KERNEL);
114 if (!phy->phy_led_triggers) {
115 err = -ENOMEM;
116 goto out_unreg_link;
119 for (i = 0; i < phy->phy_num_led_triggers; i++) {
120 err = phy_led_trigger_register(phy, &phy->phy_led_triggers[i],
121 speeds[i]);
122 if (err)
123 goto out_unreg;
126 phy->last_triggered = NULL;
127 phy_led_trigger_change_speed(phy);
129 return 0;
130 out_unreg:
131 while (i--)
132 phy_led_trigger_unregister(&phy->phy_led_triggers[i]);
133 devm_kfree(&phy->mdio.dev, phy->phy_led_triggers);
134 out_unreg_link:
135 phy_led_trigger_unregister(phy->led_link_trigger);
136 out_free_link:
137 devm_kfree(&phy->mdio.dev, phy->led_link_trigger);
138 phy->led_link_trigger = NULL;
139 out_clear:
140 phy->phy_num_led_triggers = 0;
141 return err;
143 EXPORT_SYMBOL_GPL(phy_led_triggers_register);
145 void phy_led_triggers_unregister(struct phy_device *phy)
147 int i;
149 for (i = 0; i < phy->phy_num_led_triggers; i++)
150 phy_led_trigger_unregister(&phy->phy_led_triggers[i]);
152 if (phy->led_link_trigger)
153 phy_led_trigger_unregister(phy->led_link_trigger);
155 EXPORT_SYMBOL_GPL(phy_led_triggers_unregister);