1a2443fd1SAndrew Lunn // SPDX-License-Identifier: GPL-2.0+ 2a2443fd1SAndrew Lunn /* Copyright (C) 2016 National Instruments Corp. */ 32e0bc452SZach Brown #include <linux/leds.h> 42e0bc452SZach Brown #include <linux/phy.h> 5d6f8cfa3SGeert Uytterhoeven #include <linux/phy_led_triggers.h> 62e0bc452SZach Brown #include <linux/netdevice.h> 72e0bc452SZach Brown 82e0bc452SZach Brown static struct phy_led_trigger *phy_speed_to_led_trigger(struct phy_device *phy, 92e0bc452SZach Brown unsigned int speed) 102e0bc452SZach Brown { 112e0bc452SZach Brown unsigned int i; 122e0bc452SZach Brown 132e0bc452SZach Brown for (i = 0; i < phy->phy_num_led_triggers; i++) { 142e0bc452SZach Brown if (phy->phy_led_triggers[i].speed == speed) 152e0bc452SZach Brown return &phy->phy_led_triggers[i]; 162e0bc452SZach Brown } 172e0bc452SZach Brown return NULL; 182e0bc452SZach Brown } 192e0bc452SZach Brown 20ff198cdbSMaciej S. Szmigiero static void phy_led_trigger_no_link(struct phy_device *phy) 21ff198cdbSMaciej S. Szmigiero { 22ff198cdbSMaciej S. Szmigiero if (phy->last_triggered) { 23ff198cdbSMaciej S. Szmigiero led_trigger_event(&phy->last_triggered->trigger, LED_OFF); 243928ee64SMaciej S. Szmigiero led_trigger_event(&phy->led_link_trigger->trigger, LED_OFF); 25ff198cdbSMaciej S. Szmigiero phy->last_triggered = NULL; 26ff198cdbSMaciej S. Szmigiero } 27ff198cdbSMaciej S. Szmigiero } 28ff198cdbSMaciej S. Szmigiero 292e0bc452SZach Brown void phy_led_trigger_change_speed(struct phy_device *phy) 302e0bc452SZach Brown { 312e0bc452SZach Brown struct phy_led_trigger *plt; 322e0bc452SZach Brown 332e0bc452SZach Brown if (!phy->link) 34ff198cdbSMaciej S. Szmigiero return phy_led_trigger_no_link(phy); 352e0bc452SZach Brown 362e0bc452SZach Brown if (phy->speed == 0) 372e0bc452SZach Brown return; 382e0bc452SZach Brown 392e0bc452SZach Brown plt = phy_speed_to_led_trigger(phy, phy->speed); 402e0bc452SZach Brown if (!plt) { 412e0bc452SZach Brown netdev_alert(phy->attached_dev, 422e0bc452SZach Brown "No phy led trigger registered for speed(%d)\n", 432e0bc452SZach Brown phy->speed); 44ff198cdbSMaciej S. Szmigiero return phy_led_trigger_no_link(phy); 452e0bc452SZach Brown } 462e0bc452SZach Brown 472e0bc452SZach Brown if (plt != phy->last_triggered) { 483928ee64SMaciej S. Szmigiero if (!phy->last_triggered) 493928ee64SMaciej S. Szmigiero led_trigger_event(&phy->led_link_trigger->trigger, 503928ee64SMaciej S. Szmigiero LED_FULL); 51271da132SJia-Ju Bai else 522e0bc452SZach Brown led_trigger_event(&phy->last_triggered->trigger, LED_OFF); 53271da132SJia-Ju Bai 542e0bc452SZach Brown led_trigger_event(&plt->trigger, LED_FULL); 552e0bc452SZach Brown phy->last_triggered = plt; 562e0bc452SZach Brown } 572e0bc452SZach Brown } 582e0bc452SZach Brown EXPORT_SYMBOL_GPL(phy_led_trigger_change_speed); 592e0bc452SZach Brown 603928ee64SMaciej S. Szmigiero static void phy_led_trigger_format_name(struct phy_device *phy, char *buf, 61457937bdSKyle Roeschley size_t size, const char *suffix) 623928ee64SMaciej S. Szmigiero { 633928ee64SMaciej S. Szmigiero snprintf(buf, size, PHY_ID_FMT ":%s", 643928ee64SMaciej S. Szmigiero phy->mdio.bus->id, phy->mdio.addr, suffix); 653928ee64SMaciej S. Szmigiero } 663928ee64SMaciej S. Szmigiero 672e0bc452SZach Brown static int phy_led_trigger_register(struct phy_device *phy, 682e0bc452SZach Brown struct phy_led_trigger *plt, 69*043d2be2SAndy Shevchenko unsigned int speed, 70*043d2be2SAndy Shevchenko const char *suffix) 712e0bc452SZach Brown { 722e0bc452SZach Brown plt->speed = speed; 73*043d2be2SAndy Shevchenko phy_led_trigger_format_name(phy, plt->name, sizeof(plt->name), suffix); 742e0bc452SZach Brown plt->trigger.name = plt->name; 752e0bc452SZach Brown 762e0bc452SZach Brown return led_trigger_register(&plt->trigger); 772e0bc452SZach Brown } 782e0bc452SZach Brown 792e0bc452SZach Brown static void phy_led_trigger_unregister(struct phy_led_trigger *plt) 802e0bc452SZach Brown { 812e0bc452SZach Brown led_trigger_unregister(&plt->trigger); 822e0bc452SZach Brown } 832e0bc452SZach Brown 842e0bc452SZach Brown int phy_led_triggers_register(struct phy_device *phy) 852e0bc452SZach Brown { 862e0bc452SZach Brown int i, err; 872e0bc452SZach Brown unsigned int speeds[50]; 882e0bc452SZach Brown 892e0bc452SZach Brown phy->phy_num_led_triggers = phy_supported_speeds(phy, speeds, 902e0bc452SZach Brown ARRAY_SIZE(speeds)); 912e0bc452SZach Brown if (!phy->phy_num_led_triggers) 922e0bc452SZach Brown return 0; 932e0bc452SZach Brown 943928ee64SMaciej S. Szmigiero phy->led_link_trigger = devm_kzalloc(&phy->mdio.dev, 953928ee64SMaciej S. Szmigiero sizeof(*phy->led_link_trigger), 963928ee64SMaciej S. Szmigiero GFP_KERNEL); 973928ee64SMaciej S. Szmigiero if (!phy->led_link_trigger) { 983928ee64SMaciej S. Szmigiero err = -ENOMEM; 993928ee64SMaciej S. Szmigiero goto out_clear; 1003928ee64SMaciej S. Szmigiero } 1013928ee64SMaciej S. Szmigiero 102*043d2be2SAndy Shevchenko err = phy_led_trigger_register(phy, phy->led_link_trigger, 0, "link"); 1033928ee64SMaciej S. Szmigiero if (err) 1043928ee64SMaciej S. Szmigiero goto out_free_link; 1053928ee64SMaciej S. Szmigiero 106a86854d0SKees Cook phy->phy_led_triggers = devm_kcalloc(&phy->mdio.dev, 1072e0bc452SZach Brown phy->phy_num_led_triggers, 108a86854d0SKees Cook sizeof(struct phy_led_trigger), 1092e0bc452SZach Brown GFP_KERNEL); 1108a87fca8SGeert Uytterhoeven if (!phy->phy_led_triggers) { 1118a87fca8SGeert Uytterhoeven err = -ENOMEM; 1123928ee64SMaciej S. Szmigiero goto out_unreg_link; 1138a87fca8SGeert Uytterhoeven } 1142e0bc452SZach Brown 1152e0bc452SZach Brown for (i = 0; i < phy->phy_num_led_triggers; i++) { 1162e0bc452SZach Brown err = phy_led_trigger_register(phy, &phy->phy_led_triggers[i], 117*043d2be2SAndy Shevchenko speeds[i], 118*043d2be2SAndy Shevchenko phy_speed_to_str(speeds[i])); 1192e0bc452SZach Brown if (err) 1202e0bc452SZach Brown goto out_unreg; 1212e0bc452SZach Brown } 1222e0bc452SZach Brown 1232e0bc452SZach Brown phy->last_triggered = NULL; 1242e0bc452SZach Brown phy_led_trigger_change_speed(phy); 1252e0bc452SZach Brown 1262e0bc452SZach Brown return 0; 1272e0bc452SZach Brown out_unreg: 1282e0bc452SZach Brown while (i--) 1292e0bc452SZach Brown phy_led_trigger_unregister(&phy->phy_led_triggers[i]); 1302e0bc452SZach Brown devm_kfree(&phy->mdio.dev, phy->phy_led_triggers); 1313928ee64SMaciej S. Szmigiero out_unreg_link: 1323928ee64SMaciej S. Szmigiero phy_led_trigger_unregister(phy->led_link_trigger); 1333928ee64SMaciej S. Szmigiero out_free_link: 1343928ee64SMaciej S. Szmigiero devm_kfree(&phy->mdio.dev, phy->led_link_trigger); 1353928ee64SMaciej S. Szmigiero phy->led_link_trigger = NULL; 1368a87fca8SGeert Uytterhoeven out_clear: 1378a87fca8SGeert Uytterhoeven phy->phy_num_led_triggers = 0; 1382e0bc452SZach Brown return err; 1392e0bc452SZach Brown } 1402e0bc452SZach Brown EXPORT_SYMBOL_GPL(phy_led_triggers_register); 1412e0bc452SZach Brown 1422e0bc452SZach Brown void phy_led_triggers_unregister(struct phy_device *phy) 1432e0bc452SZach Brown { 1442e0bc452SZach Brown int i; 1452e0bc452SZach Brown 1462e0bc452SZach Brown for (i = 0; i < phy->phy_num_led_triggers; i++) 1472e0bc452SZach Brown phy_led_trigger_unregister(&phy->phy_led_triggers[i]); 1483928ee64SMaciej S. Szmigiero 1493928ee64SMaciej S. Szmigiero if (phy->led_link_trigger) 1503928ee64SMaciej S. Szmigiero phy_led_trigger_unregister(phy->led_link_trigger); 1512e0bc452SZach Brown } 1522e0bc452SZach Brown EXPORT_SYMBOL_GPL(phy_led_triggers_unregister); 153