@@ -53,6 +53,55 @@ static struct tps23881_priv *to_tps23881_priv(struct pse_controller_dev *pcdev)
53
53
return container_of (pcdev , struct tps23881_priv , pcdev );
54
54
}
55
55
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
+
56
105
static int tps23881_pi_enable (struct pse_controller_dev * pcdev , int id )
57
106
{
58
107
struct tps23881_priv * priv = to_tps23881_priv (pcdev );
@@ -64,17 +113,12 @@ static int tps23881_pi_enable(struct pse_controller_dev *pcdev, int id)
64
113
return - ERANGE ;
65
114
66
115
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 ));
71
117
72
118
if (priv -> port [id ].is_4p ) {
73
119
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 ));
78
122
}
79
123
80
124
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)
91
135
return - ERANGE ;
92
136
93
137
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 ));
98
139
99
140
if (priv -> port [id ].is_4p ) {
100
141
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 ));
105
144
}
106
145
107
146
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)
113
152
struct i2c_client * client = priv -> client ;
114
153
bool enabled ;
115
154
u8 chan ;
155
+ u16 val ;
116
156
int ret ;
117
157
118
158
ret = i2c_smbus_read_word_data (client , TPS23881_REG_PW_STATUS );
119
159
if (ret < 0 )
120
160
return ret ;
121
161
122
162
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 );
127
165
128
166
if (priv -> port [id ].is_4p ) {
129
167
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 );
134
170
}
135
171
136
172
/* 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,
146
182
struct i2c_client * client = priv -> client ;
147
183
bool enabled , delivering ;
148
184
u8 chan ;
185
+ u16 val ;
149
186
int ret ;
150
187
151
188
ret = i2c_smbus_read_word_data (client , TPS23881_REG_PW_STATUS );
152
189
if (ret < 0 )
153
190
return ret ;
154
191
155
192
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 );
163
197
164
198
if (priv -> port [id ].is_4p ) {
165
199
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 );
173
204
}
174
205
175
206
/* Return delivering status only if both channel are on this state */
0 commit comments