12e0bc452SZach Brown /* Copyright (C) 2016 National Instruments Corp. 22e0bc452SZach Brown * 32e0bc452SZach Brown * This program is free software; you can redistribute it and/or modify 42e0bc452SZach Brown * it under the terms of the GNU General Public License as published by 52e0bc452SZach Brown * the Free Software Foundation; either version 2 of the License, or 62e0bc452SZach Brown * (at your option) any later version. 72e0bc452SZach Brown * 82e0bc452SZach Brown * This program is distributed in the hope that it will be useful, 92e0bc452SZach Brown * but WITHOUT ANY WARRANTY; without even the implied warranty of 102e0bc452SZach Brown * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 112e0bc452SZach Brown * GNU General Public License for more details. 122e0bc452SZach Brown */ 132e0bc452SZach Brown #include <linux/leds.h> 142e0bc452SZach Brown #include <linux/phy.h> 15d6f8cfa3SGeert Uytterhoeven #include <linux/phy_led_triggers.h> 162e0bc452SZach Brown #include <linux/netdevice.h> 172e0bc452SZach Brown 182e0bc452SZach Brown static struct phy_led_trigger *phy_speed_to_led_trigger(struct phy_device *phy, 192e0bc452SZach Brown unsigned int speed) 202e0bc452SZach Brown { 212e0bc452SZach Brown unsigned int i; 222e0bc452SZach Brown 232e0bc452SZach Brown for (i = 0; i < phy->phy_num_led_triggers; i++) { 242e0bc452SZach Brown if (phy->phy_led_triggers[i].speed == speed) 252e0bc452SZach Brown return &phy->phy_led_triggers[i]; 262e0bc452SZach Brown } 272e0bc452SZach Brown return NULL; 282e0bc452SZach Brown } 292e0bc452SZach Brown 30ff198cdbSMaciej S. Szmigiero static void phy_led_trigger_no_link(struct phy_device *phy) 31ff198cdbSMaciej S. Szmigiero { 32ff198cdbSMaciej S. Szmigiero if (phy->last_triggered) { 33ff198cdbSMaciej S. Szmigiero led_trigger_event(&phy->last_triggered->trigger, LED_OFF); 343928ee64SMaciej S. Szmigiero led_trigger_event(&phy->led_link_trigger->trigger, LED_OFF); 35ff198cdbSMaciej S. Szmigiero phy->last_triggered = NULL; 36ff198cdbSMaciej S. Szmigiero } 37ff198cdbSMaciej S. Szmigiero } 38ff198cdbSMaciej S. Szmigiero 392e0bc452SZach Brown void phy_led_trigger_change_speed(struct phy_device *phy) 402e0bc452SZach Brown { 412e0bc452SZach Brown struct phy_led_trigger *plt; 422e0bc452SZach Brown 432e0bc452SZach Brown if (!phy->link) 44ff198cdbSMaciej S. Szmigiero return phy_led_trigger_no_link(phy); 452e0bc452SZach Brown 462e0bc452SZach Brown if (phy->speed == 0) 472e0bc452SZach Brown return; 482e0bc452SZach Brown 492e0bc452SZach Brown plt = phy_speed_to_led_trigger(phy, phy->speed); 502e0bc452SZach Brown if (!plt) { 512e0bc452SZach Brown netdev_alert(phy->attached_dev, 522e0bc452SZach Brown "No phy led trigger registered for speed(%d)\n", 532e0bc452SZach Brown phy->speed); 54ff198cdbSMaciej S. Szmigiero return phy_led_trigger_no_link(phy); 552e0bc452SZach Brown } 562e0bc452SZach Brown 572e0bc452SZach Brown if (plt != phy->last_triggered) { 583928ee64SMaciej S. Szmigiero if (!phy->last_triggered) 593928ee64SMaciej S. Szmigiero led_trigger_event(&phy->led_link_trigger->trigger, 603928ee64SMaciej S. Szmigiero LED_FULL); 613928ee64SMaciej S. Szmigiero 622e0bc452SZach Brown led_trigger_event(&phy->last_triggered->trigger, LED_OFF); 632e0bc452SZach Brown led_trigger_event(&plt->trigger, LED_FULL); 642e0bc452SZach Brown phy->last_triggered = plt; 652e0bc452SZach Brown } 662e0bc452SZach Brown } 672e0bc452SZach Brown EXPORT_SYMBOL_GPL(phy_led_trigger_change_speed); 682e0bc452SZach Brown 693928ee64SMaciej S. Szmigiero static void phy_led_trigger_format_name(struct phy_device *phy, char *buf, 70*457937bdSKyle Roeschley size_t size, const char *suffix) 713928ee64SMaciej S. Szmigiero { 723928ee64SMaciej S. Szmigiero snprintf(buf, size, PHY_ID_FMT ":%s", 733928ee64SMaciej S. Szmigiero phy->mdio.bus->id, phy->mdio.addr, suffix); 743928ee64SMaciej S. Szmigiero } 753928ee64SMaciej S. Szmigiero 762e0bc452SZach Brown static int phy_led_trigger_register(struct phy_device *phy, 772e0bc452SZach Brown struct phy_led_trigger *plt, 782e0bc452SZach Brown unsigned int speed) 792e0bc452SZach Brown { 802e0bc452SZach Brown plt->speed = speed; 813928ee64SMaciej S. Szmigiero phy_led_trigger_format_name(phy, plt->name, sizeof(plt->name), 82*457937bdSKyle Roeschley phy_speed_to_str(speed)); 832e0bc452SZach Brown plt->trigger.name = plt->name; 842e0bc452SZach Brown 852e0bc452SZach Brown return led_trigger_register(&plt->trigger); 862e0bc452SZach Brown } 872e0bc452SZach Brown 882e0bc452SZach Brown static void phy_led_trigger_unregister(struct phy_led_trigger *plt) 892e0bc452SZach Brown { 902e0bc452SZach Brown led_trigger_unregister(&plt->trigger); 912e0bc452SZach Brown } 922e0bc452SZach Brown 932e0bc452SZach Brown int phy_led_triggers_register(struct phy_device *phy) 942e0bc452SZach Brown { 952e0bc452SZach Brown int i, err; 962e0bc452SZach Brown unsigned int speeds[50]; 972e0bc452SZach Brown 982e0bc452SZach Brown phy->phy_num_led_triggers = phy_supported_speeds(phy, speeds, 992e0bc452SZach Brown ARRAY_SIZE(speeds)); 1002e0bc452SZach Brown if (!phy->phy_num_led_triggers) 1012e0bc452SZach Brown return 0; 1022e0bc452SZach Brown 1033928ee64SMaciej S. Szmigiero phy->led_link_trigger = devm_kzalloc(&phy->mdio.dev, 1043928ee64SMaciej S. Szmigiero sizeof(*phy->led_link_trigger), 1053928ee64SMaciej S. Szmigiero GFP_KERNEL); 1063928ee64SMaciej S. Szmigiero if (!phy->led_link_trigger) { 1073928ee64SMaciej S. Szmigiero err = -ENOMEM; 1083928ee64SMaciej S. Szmigiero goto out_clear; 1093928ee64SMaciej S. Szmigiero } 1103928ee64SMaciej S. Szmigiero 1113928ee64SMaciej S. Szmigiero phy_led_trigger_format_name(phy, phy->led_link_trigger->name, 1123928ee64SMaciej S. Szmigiero sizeof(phy->led_link_trigger->name), 1133928ee64SMaciej S. Szmigiero "link"); 1143928ee64SMaciej S. Szmigiero phy->led_link_trigger->trigger.name = phy->led_link_trigger->name; 1153928ee64SMaciej S. Szmigiero 1163928ee64SMaciej S. Szmigiero err = led_trigger_register(&phy->led_link_trigger->trigger); 1173928ee64SMaciej S. Szmigiero if (err) 1183928ee64SMaciej S. Szmigiero goto out_free_link; 1193928ee64SMaciej S. Szmigiero 120a86854d0SKees Cook phy->phy_led_triggers = devm_kcalloc(&phy->mdio.dev, 1212e0bc452SZach Brown phy->phy_num_led_triggers, 122a86854d0SKees Cook sizeof(struct phy_led_trigger), 1232e0bc452SZach Brown GFP_KERNEL); 1248a87fca8SGeert Uytterhoeven if (!phy->phy_led_triggers) { 1258a87fca8SGeert Uytterhoeven err = -ENOMEM; 1263928ee64SMaciej S. Szmigiero goto out_unreg_link; 1278a87fca8SGeert Uytterhoeven } 1282e0bc452SZach Brown 1292e0bc452SZach Brown for (i = 0; i < phy->phy_num_led_triggers; i++) { 1302e0bc452SZach Brown err = phy_led_trigger_register(phy, &phy->phy_led_triggers[i], 1312e0bc452SZach Brown speeds[i]); 1322e0bc452SZach Brown if (err) 1332e0bc452SZach Brown goto out_unreg; 1342e0bc452SZach Brown } 1352e0bc452SZach Brown 1362e0bc452SZach Brown phy->last_triggered = NULL; 1372e0bc452SZach Brown phy_led_trigger_change_speed(phy); 1382e0bc452SZach Brown 1392e0bc452SZach Brown return 0; 1402e0bc452SZach Brown out_unreg: 1412e0bc452SZach Brown while (i--) 1422e0bc452SZach Brown phy_led_trigger_unregister(&phy->phy_led_triggers[i]); 1432e0bc452SZach Brown devm_kfree(&phy->mdio.dev, phy->phy_led_triggers); 1443928ee64SMaciej S. Szmigiero out_unreg_link: 1453928ee64SMaciej S. Szmigiero phy_led_trigger_unregister(phy->led_link_trigger); 1463928ee64SMaciej S. Szmigiero out_free_link: 1473928ee64SMaciej S. Szmigiero devm_kfree(&phy->mdio.dev, phy->led_link_trigger); 1483928ee64SMaciej S. Szmigiero phy->led_link_trigger = NULL; 1498a87fca8SGeert Uytterhoeven out_clear: 1508a87fca8SGeert Uytterhoeven phy->phy_num_led_triggers = 0; 1512e0bc452SZach Brown return err; 1522e0bc452SZach Brown } 1532e0bc452SZach Brown EXPORT_SYMBOL_GPL(phy_led_triggers_register); 1542e0bc452SZach Brown 1552e0bc452SZach Brown void phy_led_triggers_unregister(struct phy_device *phy) 1562e0bc452SZach Brown { 1572e0bc452SZach Brown int i; 1582e0bc452SZach Brown 1592e0bc452SZach Brown for (i = 0; i < phy->phy_num_led_triggers; i++) 1602e0bc452SZach Brown phy_led_trigger_unregister(&phy->phy_led_triggers[i]); 1613928ee64SMaciej S. Szmigiero 1623928ee64SMaciej S. Szmigiero if (phy->led_link_trigger) 1633928ee64SMaciej S. Szmigiero phy_led_trigger_unregister(phy->led_link_trigger); 1642e0bc452SZach Brown } 1652e0bc452SZach Brown EXPORT_SYMBOL_GPL(phy_led_triggers_unregister); 166