12c8ae66b52eb97dd9d294a4b5c9a15935411976
[openwrt/staging/wigyori.git] /
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
5
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.
9
10 Signed-off-by: Marek Vasut <marex@denx.de>
11 Signed-off-by: David S. Miller <davem@davemloft.net>
12 ---
13 drivers/net/phy/realtek.c | 106 ++++++++++++++++++++++++++++++++++++++
14 1 file changed, 106 insertions(+)
15
16 --- a/drivers/net/phy/realtek.c
17 +++ b/drivers/net/phy/realtek.c
18 @@ -32,6 +32,15 @@
19 #define RTL8211F_PHYCR2 0x19
20 #define RTL8211F_INSR 0x1d
21
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
30 +
31 #define RTL8211F_TX_DELAY BIT(8)
32 #define RTL8211F_RX_DELAY BIT(3)
33
34 @@ -87,6 +96,8 @@
35 #define RTL_8221B_VN_CG 0x001cc84a
36 #define RTL_8251B 0x001cc862
37
38 +#define RTL8211F_LED_COUNT 3
39 +
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
44 return 0;
45 }
46
47 +static int rtl8211f_led_hw_is_supported(struct phy_device *phydev, u8 index,
48 + unsigned long rules)
49 +{
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);
55 +
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
64 + * link rates.
65 + * This code currently uses mode B only.
66 + */
67 +
68 + if (index >= RTL8211F_LED_COUNT)
69 + return -EINVAL;
70 +
71 + /* Filter out any other unsupported triggers. */
72 + if (rules & ~mask)
73 + return -EOPNOTSUPP;
74 +
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)))
77 + return -EOPNOTSUPP;
78 +
79 + return 0;
80 +}
81 +
82 +static int rtl8211f_led_hw_control_get(struct phy_device *phydev, u8 index,
83 + unsigned long *rules)
84 +{
85 + int val;
86 +
87 + val = phy_read_paged(phydev, 0xd04, RTL8211F_LEDCR);
88 + if (val < 0)
89 + return val;
90 +
91 + val >>= RTL8211F_LEDCR_SHIFT * index;
92 + val &= RTL8211F_LEDCR_MASK;
93 +
94 + if (val & RTL8211F_LEDCR_LINK_10)
95 + set_bit(TRIGGER_NETDEV_LINK_10, rules);
96 +
97 + if (val & RTL8211F_LEDCR_LINK_100)
98 + set_bit(TRIGGER_NETDEV_LINK_100, rules);
99 +
100 + if (val & RTL8211F_LEDCR_LINK_1000)
101 + set_bit(TRIGGER_NETDEV_LINK_1000, rules);
102 +
103 + if (val & RTL8211F_LEDCR_ACT_TXRX) {
104 + set_bit(TRIGGER_NETDEV_RX, rules);
105 + set_bit(TRIGGER_NETDEV_TX, rules);
106 + }
107 +
108 + return 0;
109 +}
110 +
111 +static int rtl8211f_led_hw_control_set(struct phy_device *phydev, u8 index,
112 + unsigned long rules)
113 +{
114 + const u16 mask = RTL8211F_LEDCR_MASK << (RTL8211F_LEDCR_SHIFT * index);
115 + u16 reg = RTL8211F_LEDCR_MODE; /* Mode B */
116 +
117 + if (index >= RTL8211F_LED_COUNT)
118 + return -EINVAL;
119 +
120 + if (test_bit(TRIGGER_NETDEV_LINK_10, &rules))
121 + reg |= RTL8211F_LEDCR_LINK_10;
122 +
123 + if (test_bit(TRIGGER_NETDEV_LINK_100, &rules))
124 + reg |= RTL8211F_LEDCR_LINK_100;
125 +
126 + if (test_bit(TRIGGER_NETDEV_LINK_1000, &rules))
127 + reg |= RTL8211F_LEDCR_LINK_1000;
128 +
129 + if (test_bit(TRIGGER_NETDEV_RX, &rules) ||
130 + test_bit(TRIGGER_NETDEV_TX, &rules)) {
131 + reg |= RTL8211F_LEDCR_ACT_TXRX;
132 + }
133 +
134 + reg <<= RTL8211F_LEDCR_SHIFT * index;
135 +
136 + return phy_modify_paged(phydev, 0xd04, RTL8211F_LEDCR, mask, reg);
137 +}
138 +
139 static int rtl8211e_config_init(struct phy_device *phydev)
140 {
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,
149 }, {
150 PHY_ID_MATCH_EXACT(RTL_8211FVD_PHYID),
151 .name = "RTL8211F-VD Gigabit Ethernet",