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.

gpio: kempld: Simplify the bit level register accesses

The hardware uses 8 bit registers but supports configurations with up
to 16 GPIO, so all GPIO registers come in pairs. Most accesses to
single bits is done using the kempld_gpio_bitop() and
kempld_gpio_get_bit() functions, which take a register index and bit
offset as parameter. These functions apply a modulo on the bit offset
but leave the register index as is, so callers have to use an
additional macro to fix the register index before the call.

Simplify things by also handling the register index offsetting in the
bitop functions.

Signed-off-by: Alban Bedel <alban.bedel@lht.dlh.de>
Link: https://patch.msgid.link/20260311143120.2179347-2-alban.bedel@lht.dlh.de
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>

authored by

Alban Bedel and committed by
Bartosz Golaszewski
4071437c 803e822b

+14 -13
+14 -13
drivers/gpio/gpio-kempld.c
··· 17 17 18 18 #define KEMPLD_GPIO_MAX_NUM 16 19 19 #define KEMPLD_GPIO_MASK(x) (BIT((x) % 8)) 20 - #define KEMPLD_GPIO_DIR_NUM(x) (0x40 + (x) / 8) 21 - #define KEMPLD_GPIO_LVL_NUM(x) (0x42 + (x) / 8) 20 + #define KEMPLD_GPIO_DIR 0x40 21 + #define KEMPLD_GPIO_LVL 0x42 22 22 #define KEMPLD_GPIO_EVT_LVL_EDGE 0x46 23 23 #define KEMPLD_GPIO_IEN 0x4A 24 24 ··· 32 32 * kempld_get_mutex must be called prior to calling this function. 33 33 */ 34 34 static void kempld_gpio_bitop(struct kempld_device_data *pld, 35 - u8 reg, u8 bit, u8 val) 35 + u8 reg, unsigned int bit, bool val) 36 36 { 37 37 u8 status; 38 38 39 - status = kempld_read8(pld, reg); 39 + status = kempld_read8(pld, reg + (bit / 8)); 40 40 if (val) 41 41 status |= KEMPLD_GPIO_MASK(bit); 42 42 else 43 43 status &= ~KEMPLD_GPIO_MASK(bit); 44 - kempld_write8(pld, reg, status); 44 + kempld_write8(pld, reg + (bit / 8), status); 45 45 } 46 46 47 - static int kempld_gpio_get_bit(struct kempld_device_data *pld, u8 reg, u8 bit) 47 + static int kempld_gpio_get_bit(struct kempld_device_data *pld, 48 + u8 reg, unsigned int bit) 48 49 { 49 50 u8 status; 50 51 51 52 kempld_get_mutex(pld); 52 - status = kempld_read8(pld, reg); 53 + status = kempld_read8(pld, reg + (bit / 8)); 53 54 kempld_release_mutex(pld); 54 55 55 56 return !!(status & KEMPLD_GPIO_MASK(bit)); ··· 61 60 struct kempld_gpio_data *gpio = gpiochip_get_data(chip); 62 61 struct kempld_device_data *pld = gpio->pld; 63 62 64 - return !!kempld_gpio_get_bit(pld, KEMPLD_GPIO_LVL_NUM(offset), offset); 63 + return !!kempld_gpio_get_bit(pld, KEMPLD_GPIO_LVL, offset); 65 64 } 66 65 67 66 static int kempld_gpio_set(struct gpio_chip *chip, unsigned int offset, ··· 71 70 struct kempld_device_data *pld = gpio->pld; 72 71 73 72 kempld_get_mutex(pld); 74 - kempld_gpio_bitop(pld, KEMPLD_GPIO_LVL_NUM(offset), offset, value); 73 + kempld_gpio_bitop(pld, KEMPLD_GPIO_LVL, offset, value); 75 74 kempld_release_mutex(pld); 76 75 77 76 return 0; ··· 83 82 struct kempld_device_data *pld = gpio->pld; 84 83 85 84 kempld_get_mutex(pld); 86 - kempld_gpio_bitop(pld, KEMPLD_GPIO_DIR_NUM(offset), offset, 0); 85 + kempld_gpio_bitop(pld, KEMPLD_GPIO_DIR, offset, 0); 87 86 kempld_release_mutex(pld); 88 87 89 88 return 0; ··· 96 95 struct kempld_device_data *pld = gpio->pld; 97 96 98 97 kempld_get_mutex(pld); 99 - kempld_gpio_bitop(pld, KEMPLD_GPIO_LVL_NUM(offset), offset, value); 100 - kempld_gpio_bitop(pld, KEMPLD_GPIO_DIR_NUM(offset), offset, 1); 98 + kempld_gpio_bitop(pld, KEMPLD_GPIO_LVL, offset, value); 99 + kempld_gpio_bitop(pld, KEMPLD_GPIO_DIR, offset, 1); 101 100 kempld_release_mutex(pld); 102 101 103 102 return 0; ··· 108 107 struct kempld_gpio_data *gpio = gpiochip_get_data(chip); 109 108 struct kempld_device_data *pld = gpio->pld; 110 109 111 - if (kempld_gpio_get_bit(pld, KEMPLD_GPIO_DIR_NUM(offset), offset)) 110 + if (kempld_gpio_get_bit(pld, KEMPLD_GPIO_DIR, offset)) 112 111 return GPIO_LINE_DIRECTION_OUT; 113 112 114 113 return GPIO_LINE_DIRECTION_IN;