gpio: exar: unduplicate address and offset computation
Provide and use helpers for calculating the register address and bit offset instead of hand coding it in every function. Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com> Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
This commit is contained in:
parent
0c2c7e1323
commit
696868d0a7
1 changed files with 28 additions and 12 deletions
|
@ -33,6 +33,26 @@ struct exar_gpio_chip {
|
||||||
unsigned int first_pin;
|
unsigned int first_pin;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static unsigned int
|
||||||
|
exar_offset_to_sel_addr(struct exar_gpio_chip *exar_gpio, unsigned int offset)
|
||||||
|
{
|
||||||
|
return (offset + exar_gpio->first_pin) / 8 ? EXAR_OFFSET_MPIOSEL_HI
|
||||||
|
: EXAR_OFFSET_MPIOSEL_LO;
|
||||||
|
}
|
||||||
|
|
||||||
|
static unsigned int
|
||||||
|
exar_offset_to_lvl_addr(struct exar_gpio_chip *exar_gpio, unsigned int offset)
|
||||||
|
{
|
||||||
|
return (offset + exar_gpio->first_pin) / 8 ? EXAR_OFFSET_MPIOLVL_HI
|
||||||
|
: EXAR_OFFSET_MPIOLVL_LO;
|
||||||
|
}
|
||||||
|
|
||||||
|
static unsigned int
|
||||||
|
exar_offset_to_bit(struct exar_gpio_chip *exar_gpio, unsigned int offset)
|
||||||
|
{
|
||||||
|
return (offset + exar_gpio->first_pin) % 8;
|
||||||
|
}
|
||||||
|
|
||||||
static void exar_update(struct gpio_chip *chip, unsigned int reg, int val,
|
static void exar_update(struct gpio_chip *chip, unsigned int reg, int val,
|
||||||
unsigned int offset)
|
unsigned int offset)
|
||||||
{
|
{
|
||||||
|
@ -52,9 +72,8 @@ static int exar_set_direction(struct gpio_chip *chip, int direction,
|
||||||
unsigned int offset)
|
unsigned int offset)
|
||||||
{
|
{
|
||||||
struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
|
struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
|
||||||
unsigned int addr = (offset + exar_gpio->first_pin) / 8 ?
|
unsigned int addr = exar_offset_to_sel_addr(exar_gpio, offset);
|
||||||
EXAR_OFFSET_MPIOSEL_HI : EXAR_OFFSET_MPIOSEL_LO;
|
unsigned int bit = exar_offset_to_bit(exar_gpio, offset);
|
||||||
unsigned int bit = (offset + exar_gpio->first_pin) % 8;
|
|
||||||
|
|
||||||
exar_update(chip, addr, direction, bit);
|
exar_update(chip, addr, direction, bit);
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -75,9 +94,8 @@ static int exar_get(struct gpio_chip *chip, unsigned int reg)
|
||||||
static int exar_get_direction(struct gpio_chip *chip, unsigned int offset)
|
static int exar_get_direction(struct gpio_chip *chip, unsigned int offset)
|
||||||
{
|
{
|
||||||
struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
|
struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
|
||||||
unsigned int addr = (offset + exar_gpio->first_pin) / 8 ?
|
unsigned int addr = exar_offset_to_sel_addr(exar_gpio, offset);
|
||||||
EXAR_OFFSET_MPIOSEL_HI : EXAR_OFFSET_MPIOSEL_LO;
|
unsigned int bit = exar_offset_to_bit(exar_gpio, offset);
|
||||||
unsigned int bit = (offset + exar_gpio->first_pin) % 8;
|
|
||||||
|
|
||||||
if (exar_get(chip, addr) & BIT(bit))
|
if (exar_get(chip, addr) & BIT(bit))
|
||||||
return GPIO_LINE_DIRECTION_IN;
|
return GPIO_LINE_DIRECTION_IN;
|
||||||
|
@ -88,9 +106,8 @@ static int exar_get_direction(struct gpio_chip *chip, unsigned int offset)
|
||||||
static int exar_get_value(struct gpio_chip *chip, unsigned int offset)
|
static int exar_get_value(struct gpio_chip *chip, unsigned int offset)
|
||||||
{
|
{
|
||||||
struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
|
struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
|
||||||
unsigned int addr = (offset + exar_gpio->first_pin) / 8 ?
|
unsigned int addr = exar_offset_to_lvl_addr(exar_gpio, offset);
|
||||||
EXAR_OFFSET_MPIOLVL_HI : EXAR_OFFSET_MPIOLVL_LO;
|
unsigned int bit = exar_offset_to_bit(exar_gpio, offset);
|
||||||
unsigned int bit = (offset + exar_gpio->first_pin) % 8;
|
|
||||||
|
|
||||||
return !!(exar_get(chip, addr) & BIT(bit));
|
return !!(exar_get(chip, addr) & BIT(bit));
|
||||||
}
|
}
|
||||||
|
@ -99,9 +116,8 @@ static void exar_set_value(struct gpio_chip *chip, unsigned int offset,
|
||||||
int value)
|
int value)
|
||||||
{
|
{
|
||||||
struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
|
struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
|
||||||
unsigned int addr = (offset + exar_gpio->first_pin) / 8 ?
|
unsigned int addr = exar_offset_to_lvl_addr(exar_gpio, offset);
|
||||||
EXAR_OFFSET_MPIOLVL_HI : EXAR_OFFSET_MPIOLVL_LO;
|
unsigned int bit = exar_offset_to_bit(exar_gpio, offset);
|
||||||
unsigned int bit = (offset + exar_gpio->first_pin) % 8;
|
|
||||||
|
|
||||||
exar_update(chip, addr, value, bit);
|
exar_update(chip, addr, value, bit);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue