Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

usb: typec: tcpci: use GENMASK() for TCPC_CC_STATUS_CC[12]

The existing code here, particularly in maxim_contaminant.c, is
arguably quite hard to read due to the open-coded masking and shifting
spanning multiple lines.

Use GENMASK() and FIELD_GET() instead, which arguably make the code
much easier to follow.

While at it, use the symbolic name TCPC_CC_STATE_SRC_OPEN for one
instance of open-coded 0x0.

Signed-off-by: André Draszik <andre.draszik@linaro.org>
Reviewed-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
Link: https://lore.kernel.org/r/20240710-tcpc-cleanup-v1-3-0ec1f41f4263@linaro.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

André Draszik and committed by
Greg Kroah-Hartman
f523aa6d 0943ce2b

+12 -18
+3 -5
drivers/usb/typec/tcpm/maxim_contaminant.c
··· 5 5 * USB-C module to reduce wakeups due to contaminants. 6 6 */ 7 7 8 + #include <linux/bitfield.h> 8 9 #include <linux/device.h> 9 10 #include <linux/irqreturn.h> 10 11 #include <linux/module.h> ··· 49 48 #define STATUS_CHECK(reg, mask, val) (((reg) & (mask)) == (val)) 50 49 51 50 #define IS_CC_OPEN(cc_status) \ 52 - (STATUS_CHECK((cc_status), TCPC_CC_STATUS_CC1_MASK << TCPC_CC_STATUS_CC1_SHIFT, \ 53 - TCPC_CC_STATE_SRC_OPEN) && STATUS_CHECK((cc_status), \ 54 - TCPC_CC_STATUS_CC2_MASK << \ 55 - TCPC_CC_STATUS_CC2_SHIFT, \ 56 - TCPC_CC_STATE_SRC_OPEN)) 51 + (FIELD_GET(TCPC_CC_STATUS_CC1, cc_status) == TCPC_CC_STATE_SRC_OPEN \ 52 + && FIELD_GET(TCPC_CC_STATUS_CC2, cc_status) == TCPC_CC_STATE_SRC_OPEN) 57 53 58 54 static int max_contaminant_adc_to_mv(struct max_tcpci_chip *chip, enum fladc_select channel, 59 55 bool ua_src, u8 fladc)
+3 -4
drivers/usb/typec/tcpm/tcpci.c
··· 5 5 * USB Type-C Port Controller Interface. 6 6 */ 7 7 8 + #include <linux/bitfield.h> 8 9 #include <linux/delay.h> 9 10 #include <linux/kernel.h> 10 11 #include <linux/module.h> ··· 242 241 if (ret < 0) 243 242 return ret; 244 243 245 - *cc1 = tcpci_to_typec_cc((reg >> TCPC_CC_STATUS_CC1_SHIFT) & 246 - TCPC_CC_STATUS_CC1_MASK, 244 + *cc1 = tcpci_to_typec_cc(FIELD_GET(TCPC_CC_STATUS_CC1, reg), 247 245 reg & TCPC_CC_STATUS_TERM || 248 246 tcpc_presenting_rd(role_control, CC1)); 249 - *cc2 = tcpci_to_typec_cc((reg >> TCPC_CC_STATUS_CC2_SHIFT) & 250 - TCPC_CC_STATUS_CC2_MASK, 247 + *cc2 = tcpci_to_typec_cc(FIELD_GET(TCPC_CC_STATUS_CC2, reg), 251 248 reg & TCPC_CC_STATUS_TERM || 252 249 tcpc_presenting_rd(role_control, CC2)); 253 250
+3 -4
drivers/usb/typec/tcpm/tcpci_rt1711h.c
··· 5 5 * Richtek RT1711H Type-C Chip Driver 6 6 */ 7 7 8 + #include <linux/bitfield.h> 8 9 #include <linux/bits.h> 9 10 #include <linux/kernel.h> 10 11 #include <linux/mod_devicetable.h> ··· 196 195 if (ret < 0) 197 196 return ret; 198 197 199 - cc1 = tcpci_to_typec_cc((status >> TCPC_CC_STATUS_CC1_SHIFT) & 200 - TCPC_CC_STATUS_CC1_MASK, 198 + cc1 = tcpci_to_typec_cc(FIELD_GET(TCPC_CC_STATUS_CC1, status), 201 199 status & TCPC_CC_STATUS_TERM || 202 200 tcpc_presenting_rd(role, CC1)); 203 - cc2 = tcpci_to_typec_cc((status >> TCPC_CC_STATUS_CC2_SHIFT) & 204 - TCPC_CC_STATUS_CC2_MASK, 201 + cc2 = tcpci_to_typec_cc(FIELD_GET(TCPC_CC_STATUS_CC2, status), 205 202 status & TCPC_CC_STATUS_TERM || 206 203 tcpc_presenting_rd(role, CC2)); 207 204
+3 -5
include/linux/usb/tcpci.h
··· 92 92 #define TCPC_CC_STATUS_TERM BIT(4) 93 93 #define TCPC_CC_STATUS_TERM_RP 0 94 94 #define TCPC_CC_STATUS_TERM_RD 1 95 + #define TCPC_CC_STATUS_CC2 GENMASK(3, 2) 96 + #define TCPC_CC_STATUS_CC1 GENMASK(1, 0) 95 97 #define TCPC_CC_STATE_SRC_OPEN 0 96 - #define TCPC_CC_STATUS_CC2_SHIFT 2 97 - #define TCPC_CC_STATUS_CC2_MASK 0x3 98 - #define TCPC_CC_STATUS_CC1_SHIFT 0 99 - #define TCPC_CC_STATUS_CC1_MASK 0x3 100 98 101 99 #define TCPC_POWER_STATUS 0x1e 102 100 #define TCPC_POWER_STATUS_DBG_ACC_CON BIT(7) ··· 254 256 if (sink) 255 257 return TYPEC_CC_RP_3_0; 256 258 fallthrough; 257 - case 0x0: 259 + case TCPC_CC_STATE_SRC_OPEN: 258 260 default: 259 261 return TYPEC_CC_OPEN; 260 262 }