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, 692e0bc452SZach Brown unsigned int speed) 702e0bc452SZach Brown { 712e0bc452SZach Brown plt->speed = speed; 723928ee64SMaciej S. Szmigiero phy_led_trigger_format_name(phy, plt->name, sizeof(plt->name), 73457937bdSKyle Roeschley phy_speed_to_str(speed)); 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 1023928ee64SMaciej S. Szmigiero phy_led_trigger_format_name(phy, phy->led_link_trigger->name, 1033928ee64SMaciej S. Szmigiero sizeof(phy->led_link_trigger->name), 1043928ee64SMaciej S. Szmigiero "link"); 1053928ee64SMaciej S. Szmigiero phy->led_link_trigger->trigger.name = phy->led_link_trigger->name; 1063928ee64SMaciej S. Szmigiero 1073928ee64SMaciej S. Szmigiero err = led_trigger_register(&phy->led_link_trigger->trigger); 1083928ee64SMaciej S. Szmigiero if (err) 1093928ee64SMaciej S. Szmigiero goto out_free_link; 1103928ee64SMaciej S. Szmigiero 111a86854d0SKees Cook phy->phy_led_triggers = devm_kcalloc(&phy->mdio.dev, 1122e0bc452SZach Brown phy->phy_num_led_triggers, 113a86854d0SKees Cook sizeof(struct phy_led_trigger), 1142e0bc452SZach Brown GFP_KERNEL); 1158a87fca8SGeert Uytterhoeven if (!phy->phy_led_triggers) { 1168a87fca8SGeert Uytterhoeven err = -ENOMEM; 1173928ee64SMaciej S. Szmigiero goto out_unreg_link; 1188a87fca8SGeert Uytterhoeven } 1192e0bc452SZach Brown 1202e0bc452SZach Brown for (i = 0; i < phy->phy_num_led_triggers; i++) { 1212e0bc452SZach Brown err = phy_led_trigger_register(phy, &phy->phy_led_triggers[i], 1222e0bc452SZach Brown speeds[i]); 1232e0bc452SZach Brown if (err) 1242e0bc452SZach Brown goto out_unreg; 1252e0bc452SZach Brown } 1262e0bc452SZach Brown 1272e0bc452SZach Brown phy->last_triggered = NULL; 1282e0bc452SZach Brown phy_led_trigger_change_speed(phy); 1292e0bc452SZach Brown 1302e0bc452SZach Brown return 0; 1312e0bc452SZach Brown out_unreg: 1322e0bc452SZach Brown while (i--) 1332e0bc452SZach Brown phy_led_trigger_unregister(&phy->phy_led_triggers[i]); 1342e0bc452SZach Brown devm_kfree(&phy->mdio.dev, phy->phy_led_triggers); 1353928ee64SMaciej S. Szmigiero out_unreg_link: 1363928ee64SMaciej S. Szmigiero phy_led_trigger_unregister(phy->led_link_trigger); 1373928ee64SMaciej S. Szmigiero out_free_link: 1383928ee64SMaciej S. Szmigiero devm_kfree(&phy->mdio.dev, phy->led_link_trigger); 1393928ee64SMaciej S. Szmigiero phy->led_link_trigger = NULL; 1408a87fca8SGeert Uytterhoeven out_clear: 1418a87fca8SGeert Uytterhoeven phy->phy_num_led_triggers = 0; 1422e0bc452SZach Brown return err; 1432e0bc452SZach Brown } 1442e0bc452SZach Brown EXPORT_SYMBOL_GPL(phy_led_triggers_register); 1452e0bc452SZach Brown 1462e0bc452SZach Brown void phy_led_triggers_unregister(struct phy_device *phy) 1472e0bc452SZach Brown { 1482e0bc452SZach Brown int i; 1492e0bc452SZach Brown 1502e0bc452SZach Brown for (i = 0; i < phy->phy_num_led_triggers; i++) 1512e0bc452SZach Brown phy_led_trigger_unregister(&phy->phy_led_triggers[i]); 1523928ee64SMaciej S. Szmigiero 1533928ee64SMaciej S. Szmigiero if (phy->led_link_trigger) 1543928ee64SMaciej S. Szmigiero phy_led_trigger_unregister(phy->led_link_trigger); 1552e0bc452SZach Brown } 1562e0bc452SZach Brown EXPORT_SYMBOL_GPL(phy_led_triggers_unregister); 157