Skip to content

Commit 4071437

Browse files
Alban BedelBartosz Golaszewski
authored andcommitted
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>
1 parent 803e822 commit 4071437

1 file changed

Lines changed: 14 additions & 13 deletions

File tree

drivers/gpio/gpio-kempld.c

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717

1818
#define KEMPLD_GPIO_MAX_NUM 16
1919
#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
2222
#define KEMPLD_GPIO_EVT_LVL_EDGE 0x46
2323
#define KEMPLD_GPIO_IEN 0x4A
2424

@@ -32,24 +32,25 @@ struct kempld_gpio_data {
3232
* kempld_get_mutex must be called prior to calling this function.
3333
*/
3434
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)
3636
{
3737
u8 status;
3838

39-
status = kempld_read8(pld, reg);
39+
status = kempld_read8(pld, reg + (bit / 8));
4040
if (val)
4141
status |= KEMPLD_GPIO_MASK(bit);
4242
else
4343
status &= ~KEMPLD_GPIO_MASK(bit);
44-
kempld_write8(pld, reg, status);
44+
kempld_write8(pld, reg + (bit / 8), status);
4545
}
4646

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)
4849
{
4950
u8 status;
5051

5152
kempld_get_mutex(pld);
52-
status = kempld_read8(pld, reg);
53+
status = kempld_read8(pld, reg + (bit / 8));
5354
kempld_release_mutex(pld);
5455

5556
return !!(status & KEMPLD_GPIO_MASK(bit));
@@ -60,7 +61,7 @@ static int kempld_gpio_get(struct gpio_chip *chip, unsigned offset)
6061
struct kempld_gpio_data *gpio = gpiochip_get_data(chip);
6162
struct kempld_device_data *pld = gpio->pld;
6263

63-
return !!kempld_gpio_get_bit(pld, KEMPLD_GPIO_LVL_NUM(offset), offset);
64+
return !!kempld_gpio_get_bit(pld, KEMPLD_GPIO_LVL, offset);
6465
}
6566

6667
static int kempld_gpio_set(struct gpio_chip *chip, unsigned int offset,
@@ -70,7 +71,7 @@ static int kempld_gpio_set(struct gpio_chip *chip, unsigned int offset,
7071
struct kempld_device_data *pld = gpio->pld;
7172

7273
kempld_get_mutex(pld);
73-
kempld_gpio_bitop(pld, KEMPLD_GPIO_LVL_NUM(offset), offset, value);
74+
kempld_gpio_bitop(pld, KEMPLD_GPIO_LVL, offset, value);
7475
kempld_release_mutex(pld);
7576

7677
return 0;
@@ -82,7 +83,7 @@ static int kempld_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
8283
struct kempld_device_data *pld = gpio->pld;
8384

8485
kempld_get_mutex(pld);
85-
kempld_gpio_bitop(pld, KEMPLD_GPIO_DIR_NUM(offset), offset, 0);
86+
kempld_gpio_bitop(pld, KEMPLD_GPIO_DIR, offset, 0);
8687
kempld_release_mutex(pld);
8788

8889
return 0;
@@ -95,8 +96,8 @@ static int kempld_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
9596
struct kempld_device_data *pld = gpio->pld;
9697

9798
kempld_get_mutex(pld);
98-
kempld_gpio_bitop(pld, KEMPLD_GPIO_LVL_NUM(offset), offset, value);
99-
kempld_gpio_bitop(pld, KEMPLD_GPIO_DIR_NUM(offset), offset, 1);
99+
kempld_gpio_bitop(pld, KEMPLD_GPIO_LVL, offset, value);
100+
kempld_gpio_bitop(pld, KEMPLD_GPIO_DIR, offset, 1);
100101
kempld_release_mutex(pld);
101102

102103
return 0;
@@ -107,7 +108,7 @@ static int kempld_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
107108
struct kempld_gpio_data *gpio = gpiochip_get_data(chip);
108109
struct kempld_device_data *pld = gpio->pld;
109110

110-
if (kempld_gpio_get_bit(pld, KEMPLD_GPIO_DIR_NUM(offset), offset))
111+
if (kempld_gpio_get_bit(pld, KEMPLD_GPIO_DIR, offset))
111112
return GPIO_LINE_DIRECTION_OUT;
112113

113114
return GPIO_LINE_DIRECTION_IN;

0 commit comments

Comments
 (0)