Skip to content

Commit 4c2bab5

Browse files
kmaincentPaolo Abeni
authored andcommitted
net: pse-pd: tps23881: Use helpers to calculate bit offset for a channel
This driver frequently follows a pattern where two registers are read or written in a single operation, followed by calculating the bit offset for a specific channel. Introduce helpers to streamline this process and reduce code redundancy, making the codebase cleaner and more maintainable. Acked-by: Oleksij Rempel <[email protected]> Signed-off-by: Kory Maincent <[email protected]> Signed-off-by: Paolo Abeni <[email protected]>
1 parent 0b56751 commit 4c2bab5

File tree

1 file changed

+69
-38
lines changed

1 file changed

+69
-38
lines changed

drivers/net/pse-pd/tps23881.c

Lines changed: 69 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,55 @@ static struct tps23881_priv *to_tps23881_priv(struct pse_controller_dev *pcdev)
5353
return container_of(pcdev, struct tps23881_priv, pcdev);
5454
}
5555

56+
/*
57+
* Helper to extract a value from a u16 register value, which is made of two
58+
* u8 registers. The function calculates the bit offset based on the channel
59+
* and extracts the relevant bits using a provided field mask.
60+
*
61+
* @param reg_val: The u16 register value (composed of two u8 registers).
62+
* @param chan: The channel number (0-7).
63+
* @param field_offset: The base bit offset to apply (e.g., 0 or 4).
64+
* @param field_mask: The mask to apply to extract the required bits.
65+
* @return: The extracted value for the specific channel.
66+
*/
67+
static u16 tps23881_calc_val(u16 reg_val, u8 chan, u8 field_offset,
68+
u16 field_mask)
69+
{
70+
if (chan >= 4)
71+
reg_val >>= 8;
72+
73+
return (reg_val >> field_offset) & field_mask;
74+
}
75+
76+
/*
77+
* Helper to combine individual channel values into a u16 register value.
78+
* The function sets the value for a specific channel in the appropriate
79+
* position.
80+
*
81+
* @param reg_val: The current u16 register value.
82+
* @param chan: The channel number (0-7).
83+
* @param field_offset: The base bit offset to apply (e.g., 0 or 4).
84+
* @param field_mask: The mask to apply for the field (e.g., 0x0F).
85+
* @param field_val: The value to set for the specific channel (masked by
86+
* field_mask).
87+
* @return: The updated u16 register value with the channel value set.
88+
*/
89+
static u16 tps23881_set_val(u16 reg_val, u8 chan, u8 field_offset,
90+
u16 field_mask, u16 field_val)
91+
{
92+
field_val &= field_mask;
93+
94+
if (chan < 4) {
95+
reg_val &= ~(field_mask << field_offset);
96+
reg_val |= (field_val << field_offset);
97+
} else {
98+
reg_val &= ~(field_mask << (field_offset + 8));
99+
reg_val |= (field_val << (field_offset + 8));
100+
}
101+
102+
return reg_val;
103+
}
104+
56105
static int tps23881_pi_enable(struct pse_controller_dev *pcdev, int id)
57106
{
58107
struct tps23881_priv *priv = to_tps23881_priv(pcdev);
@@ -64,17 +113,12 @@ static int tps23881_pi_enable(struct pse_controller_dev *pcdev, int id)
64113
return -ERANGE;
65114

66115
chan = priv->port[id].chan[0];
67-
if (chan < 4)
68-
val = BIT(chan);
69-
else
70-
val = BIT(chan + 4);
116+
val = tps23881_set_val(0, chan, 0, BIT(chan % 4), BIT(chan % 4));
71117

72118
if (priv->port[id].is_4p) {
73119
chan = priv->port[id].chan[1];
74-
if (chan < 4)
75-
val |= BIT(chan);
76-
else
77-
val |= BIT(chan + 4);
120+
val = tps23881_set_val(val, chan, 0, BIT(chan % 4),
121+
BIT(chan % 4));
78122
}
79123

80124
return i2c_smbus_write_word_data(client, TPS23881_REG_PW_EN, val);
@@ -91,17 +135,12 @@ static int tps23881_pi_disable(struct pse_controller_dev *pcdev, int id)
91135
return -ERANGE;
92136

93137
chan = priv->port[id].chan[0];
94-
if (chan < 4)
95-
val = BIT(chan + 4);
96-
else
97-
val = BIT(chan + 8);
138+
val = tps23881_set_val(0, chan, 4, BIT(chan % 4), BIT(chan % 4));
98139

99140
if (priv->port[id].is_4p) {
100141
chan = priv->port[id].chan[1];
101-
if (chan < 4)
102-
val |= BIT(chan + 4);
103-
else
104-
val |= BIT(chan + 8);
142+
val = tps23881_set_val(val, chan, 4, BIT(chan % 4),
143+
BIT(chan % 4));
105144
}
106145

107146
return i2c_smbus_write_word_data(client, TPS23881_REG_PW_EN, val);
@@ -113,24 +152,21 @@ static int tps23881_pi_is_enabled(struct pse_controller_dev *pcdev, int id)
113152
struct i2c_client *client = priv->client;
114153
bool enabled;
115154
u8 chan;
155+
u16 val;
116156
int ret;
117157

118158
ret = i2c_smbus_read_word_data(client, TPS23881_REG_PW_STATUS);
119159
if (ret < 0)
120160
return ret;
121161

122162
chan = priv->port[id].chan[0];
123-
if (chan < 4)
124-
enabled = ret & BIT(chan);
125-
else
126-
enabled = ret & BIT(chan + 4);
163+
val = tps23881_calc_val(ret, chan, 0, BIT(chan % 4));
164+
enabled = !!(val);
127165

128166
if (priv->port[id].is_4p) {
129167
chan = priv->port[id].chan[1];
130-
if (chan < 4)
131-
enabled &= !!(ret & BIT(chan));
132-
else
133-
enabled &= !!(ret & BIT(chan + 4));
168+
val = tps23881_calc_val(ret, chan, 0, BIT(chan % 4));
169+
enabled &= !!(val);
134170
}
135171

136172
/* Return enabled status only if both channel are on this state */
@@ -146,30 +182,25 @@ static int tps23881_ethtool_get_status(struct pse_controller_dev *pcdev,
146182
struct i2c_client *client = priv->client;
147183
bool enabled, delivering;
148184
u8 chan;
185+
u16 val;
149186
int ret;
150187

151188
ret = i2c_smbus_read_word_data(client, TPS23881_REG_PW_STATUS);
152189
if (ret < 0)
153190
return ret;
154191

155192
chan = priv->port[id].chan[0];
156-
if (chan < 4) {
157-
enabled = ret & BIT(chan);
158-
delivering = ret & BIT(chan + 4);
159-
} else {
160-
enabled = ret & BIT(chan + 4);
161-
delivering = ret & BIT(chan + 8);
162-
}
193+
val = tps23881_calc_val(ret, chan, 0, BIT(chan % 4));
194+
enabled = !!(val);
195+
val = tps23881_calc_val(ret, chan, 4, BIT(chan % 4));
196+
delivering = !!(val);
163197

164198
if (priv->port[id].is_4p) {
165199
chan = priv->port[id].chan[1];
166-
if (chan < 4) {
167-
enabled &= !!(ret & BIT(chan));
168-
delivering &= !!(ret & BIT(chan + 4));
169-
} else {
170-
enabled &= !!(ret & BIT(chan + 4));
171-
delivering &= !!(ret & BIT(chan + 8));
172-
}
200+
val = tps23881_calc_val(ret, chan, 0, BIT(chan % 4));
201+
enabled &= !!(val);
202+
val = tps23881_calc_val(ret, chan, 4, BIT(chan % 4));
203+
delivering &= !!(val);
173204
}
174205

175206
/* Return delivering status only if both channel are on this state */

0 commit comments

Comments
 (0)