1 From 17784801d888238571a0c4101b9ac4401fffeaa0 Mon Sep 17 00:00:00 2001
2 From: Marek Vasut <marex@denx.de>
3 Date: Tue, 25 Jun 2024 22:42:17 +0200
4 Subject: [PATCH] net: phy: realtek: Add support for PHY LEDs on RTL8211F
6 Realtek RTL8211F Ethernet PHY supports 3 LED pins which are used to
7 indicate link status and activity. Add minimal LED controller driver
8 supporting the most common uses with the 'netdev' trigger.
10 Signed-off-by: Marek Vasut <marex@denx.de>
11 Signed-off-by: David S. Miller <davem@davemloft.net>
13 drivers/net/phy/realtek.c | 106 ++++++++++++++++++++++++++++++++++++++
14 1 file changed, 106 insertions(+)
16 --- a/drivers/net/phy/realtek.c
17 +++ b/drivers/net/phy/realtek.c
19 #define RTL8211F_PHYCR2 0x19
20 #define RTL8211F_INSR 0x1d
22 +#define RTL8211F_LEDCR 0x10
23 +#define RTL8211F_LEDCR_MODE BIT(15)
24 +#define RTL8211F_LEDCR_ACT_TXRX BIT(4)
25 +#define RTL8211F_LEDCR_LINK_1000 BIT(3)
26 +#define RTL8211F_LEDCR_LINK_100 BIT(1)
27 +#define RTL8211F_LEDCR_LINK_10 BIT(0)
28 +#define RTL8211F_LEDCR_MASK GENMASK(4, 0)
29 +#define RTL8211F_LEDCR_SHIFT 5
31 #define RTL8211F_TX_DELAY BIT(8)
32 #define RTL8211F_RX_DELAY BIT(3)
35 #define RTL_8221B_VN_CG 0x001cc84a
36 #define RTL_8251B 0x001cc862
38 +#define RTL8211F_LED_COUNT 3
40 MODULE_DESCRIPTION("Realtek PHY driver");
41 MODULE_AUTHOR("Johnson Leung");
42 MODULE_LICENSE("GPL");
43 @@ -476,6 +487,98 @@ static int rtl821x_resume(struct phy_dev
47 +static int rtl8211f_led_hw_is_supported(struct phy_device *phydev, u8 index,
48 + unsigned long rules)
50 + const unsigned long mask = BIT(TRIGGER_NETDEV_LINK_10) |
51 + BIT(TRIGGER_NETDEV_LINK_100) |
52 + BIT(TRIGGER_NETDEV_LINK_1000) |
53 + BIT(TRIGGER_NETDEV_RX) |
54 + BIT(TRIGGER_NETDEV_TX);
56 + /* The RTL8211F PHY supports these LED settings on up to three LEDs:
57 + * - Link: Configurable subset of 10/100/1000 link rates
58 + * - Active: Blink on activity, RX or TX is not differentiated
59 + * The Active option has two modes, A and B:
60 + * - A: Link and Active indication at configurable, but matching,
61 + * subset of 10/100/1000 link rates
62 + * - B: Link indication at configurable subset of 10/100/1000 link
63 + * rates and Active indication always at all three 10+100+1000
65 + * This code currently uses mode B only.
68 + if (index >= RTL8211F_LED_COUNT)
71 + /* Filter out any other unsupported triggers. */
75 + /* RX and TX are not differentiated, either both are set or not set. */
76 + if (!(rules & BIT(TRIGGER_NETDEV_RX)) ^ !(rules & BIT(TRIGGER_NETDEV_TX)))
82 +static int rtl8211f_led_hw_control_get(struct phy_device *phydev, u8 index,
83 + unsigned long *rules)
87 + val = phy_read_paged(phydev, 0xd04, RTL8211F_LEDCR);
91 + val >>= RTL8211F_LEDCR_SHIFT * index;
92 + val &= RTL8211F_LEDCR_MASK;
94 + if (val & RTL8211F_LEDCR_LINK_10)
95 + set_bit(TRIGGER_NETDEV_LINK_10, rules);
97 + if (val & RTL8211F_LEDCR_LINK_100)
98 + set_bit(TRIGGER_NETDEV_LINK_100, rules);
100 + if (val & RTL8211F_LEDCR_LINK_1000)
101 + set_bit(TRIGGER_NETDEV_LINK_1000, rules);
103 + if (val & RTL8211F_LEDCR_ACT_TXRX) {
104 + set_bit(TRIGGER_NETDEV_RX, rules);
105 + set_bit(TRIGGER_NETDEV_TX, rules);
111 +static int rtl8211f_led_hw_control_set(struct phy_device *phydev, u8 index,
112 + unsigned long rules)
114 + const u16 mask = RTL8211F_LEDCR_MASK << (RTL8211F_LEDCR_SHIFT * index);
115 + u16 reg = RTL8211F_LEDCR_MODE; /* Mode B */
117 + if (index >= RTL8211F_LED_COUNT)
120 + if (test_bit(TRIGGER_NETDEV_LINK_10, &rules))
121 + reg |= RTL8211F_LEDCR_LINK_10;
123 + if (test_bit(TRIGGER_NETDEV_LINK_100, &rules))
124 + reg |= RTL8211F_LEDCR_LINK_100;
126 + if (test_bit(TRIGGER_NETDEV_LINK_1000, &rules))
127 + reg |= RTL8211F_LEDCR_LINK_1000;
129 + if (test_bit(TRIGGER_NETDEV_RX, &rules) ||
130 + test_bit(TRIGGER_NETDEV_TX, &rules)) {
131 + reg |= RTL8211F_LEDCR_ACT_TXRX;
134 + reg <<= RTL8211F_LEDCR_SHIFT * index;
136 + return phy_modify_paged(phydev, 0xd04, RTL8211F_LEDCR, mask, reg);
139 static int rtl8211e_config_init(struct phy_device *phydev)
141 int ret = 0, oldpage;
142 @@ -1192,6 +1295,9 @@ static struct phy_driver realtek_drvs[]
143 .read_page = rtl821x_read_page,
144 .write_page = rtl821x_write_page,
145 .flags = PHY_ALWAYS_CALL_SUSPEND,
146 + .led_hw_is_supported = rtl8211f_led_hw_is_supported,
147 + .led_hw_control_get = rtl8211f_led_hw_control_get,
148 + .led_hw_control_set = rtl8211f_led_hw_control_set,
150 PHY_ID_MATCH_EXACT(RTL_8211FVD_PHYID),
151 .name = "RTL8211F-VD Gigabit Ethernet",