summaryrefslogtreecommitdiff
path: root/drivers/net/phy/aquantia/aquantia_leds.c
blob: 0516ac02c3f81e7be3b8cb621d238d3794f11463 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// SPDX-License-Identifier: GPL-2.0
/* LED driver for Aquantia PHY
 *
 * Author: Daniel Golle <daniel@makrotopia.org>
 */

#include <linux/phy.h>

#include "aquantia.h"

int aqr_phy_led_brightness_set(struct phy_device *phydev,
			       u8 index, enum led_brightness value)
{
	if (index >= AQR_MAX_LEDS)
		return -EINVAL;

	return phy_modify_mmd(phydev, MDIO_MMD_VEND1, AQR_LED_PROV(index),
			      VEND1_GLOBAL_LED_PROV_LINK_MASK |
			      VEND1_GLOBAL_LED_PROV_FORCE_ON |
			      VEND1_GLOBAL_LED_PROV_RX_ACT |
			      VEND1_GLOBAL_LED_PROV_TX_ACT,
			      value ? VEND1_GLOBAL_LED_PROV_FORCE_ON : 0);
}

static const unsigned long supported_triggers = (BIT(TRIGGER_NETDEV_LINK) |
						 BIT(TRIGGER_NETDEV_LINK_100) |
						 BIT(TRIGGER_NETDEV_LINK_1000) |
						 BIT(TRIGGER_NETDEV_LINK_2500) |
						 BIT(TRIGGER_NETDEV_LINK_5000) |
						 BIT(TRIGGER_NETDEV_LINK_10000)  |
						 BIT(TRIGGER_NETDEV_RX) |
						 BIT(TRIGGER_NETDEV_TX));

int aqr_phy_led_hw_is_supported(struct phy_device *phydev, u8 index,
				unsigned long rules)
{
	if (index >= AQR_MAX_LEDS)
		return -EINVAL;

	/* All combinations of the supported triggers are allowed */
	if (rules & ~supported_triggers)
		return -EOPNOTSUPP;

	return 0;
}

int aqr_phy_led_hw_control_get(struct phy_device *phydev, u8 index,
			       unsigned long *rules)
{
	int val;

	if (index >= AQR_MAX_LEDS)
		return -EINVAL;

	val = phy_read_mmd(phydev, MDIO_MMD_VEND1, AQR_LED_PROV(index));
	if (val < 0)
		return val;

	*rules = 0;
	if (val & VEND1_GLOBAL_LED_PROV_LINK100)
		*rules |= BIT(TRIGGER_NETDEV_LINK_100);

	if (val & VEND1_GLOBAL_LED_PROV_LINK1000)
		*rules |= BIT(TRIGGER_NETDEV_LINK_1000);

	if (val & VEND1_GLOBAL_LED_PROV_LINK2500)
		*rules |= BIT(TRIGGER_NETDEV_LINK_2500);

	if (val & VEND1_GLOBAL_LED_PROV_LINK5000)
		*rules |= BIT(TRIGGER_NETDEV_LINK_5000);

	if (val & VEND1_GLOBAL_LED_PROV_LINK10000)
		*rules |= BIT(TRIGGER_NETDEV_LINK_10000);

	if (val & VEND1_GLOBAL_LED_PROV_RX_ACT)
		*rules |= BIT(TRIGGER_NETDEV_RX);

	if (val & VEND1_GLOBAL_LED_PROV_TX_ACT)
		*rules |= BIT(TRIGGER_NETDEV_TX);

	return 0;
}

int aqr_phy_led_hw_control_set(struct phy_device *phydev, u8 index,
			       unsigned long rules)
{
	u16 val = 0;

	if (index >= AQR_MAX_LEDS)
		return -EINVAL;

	if (rules & (BIT(TRIGGER_NETDEV_LINK_100) | BIT(TRIGGER_NETDEV_LINK)))
		val |= VEND1_GLOBAL_LED_PROV_LINK100;

	if (rules & (BIT(TRIGGER_NETDEV_LINK_1000) | BIT(TRIGGER_NETDEV_LINK)))
		val |= VEND1_GLOBAL_LED_PROV_LINK1000;

	if (rules & (BIT(TRIGGER_NETDEV_LINK_2500) | BIT(TRIGGER_NETDEV_LINK)))
		val |= VEND1_GLOBAL_LED_PROV_LINK2500;

	if (rules & (BIT(TRIGGER_NETDEV_LINK_5000) | BIT(TRIGGER_NETDEV_LINK)))
		val |= VEND1_GLOBAL_LED_PROV_LINK5000;

	if (rules & (BIT(TRIGGER_NETDEV_LINK_10000) | BIT(TRIGGER_NETDEV_LINK)))
		val |= VEND1_GLOBAL_LED_PROV_LINK10000;

	if (rules & BIT(TRIGGER_NETDEV_RX))
		val |= VEND1_GLOBAL_LED_PROV_RX_ACT;

	if (rules & BIT(TRIGGER_NETDEV_TX))
		val |= VEND1_GLOBAL_LED_PROV_TX_ACT;

	return phy_modify_mmd(phydev, MDIO_MMD_VEND1, AQR_LED_PROV(index),
			      VEND1_GLOBAL_LED_PROV_LINK_MASK |
			      VEND1_GLOBAL_LED_PROV_FORCE_ON |
			      VEND1_GLOBAL_LED_PROV_RX_ACT |
			      VEND1_GLOBAL_LED_PROV_TX_ACT, val);
}

int aqr_phy_led_active_low_set(struct phy_device *phydev, int index, bool enable)
{
	return phy_modify_mmd(phydev, MDIO_MMD_VEND1, AQR_LED_DRIVE(index),
			      VEND1_GLOBAL_LED_DRIVE_VDD, enable);
}

int aqr_phy_led_polarity_set(struct phy_device *phydev, int index, unsigned long modes)
{
	struct aqr107_priv *priv = phydev->priv;
	bool active_low = false;
	u32 mode;

	if (index >= AQR_MAX_LEDS)
		return -EINVAL;

	for_each_set_bit(mode, &modes, __PHY_LED_MODES_NUM) {
		switch (mode) {
		case PHY_LED_ACTIVE_LOW:
			active_low = true;
			break;
		default:
			return -EINVAL;
		}
	}

	/* Save LED driver vdd state to restore on SW reset */
	if (active_low)
		priv->leds_active_low |= BIT(index);

	return aqr_phy_led_active_low_set(phydev, index, active_low);
}