mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2024-10-04 08:08:54 +00:00
gpio: pca953x: Simplify code with cleanup helpers
Use macros defined in linux/cleanup.h to automate resource lifetime control in gpio-pca953x. Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com> Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
This commit is contained in:
parent
ec5bde6201
commit
8e471b784a
1 changed files with 29 additions and 48 deletions
|
@ -10,6 +10,7 @@
|
||||||
|
|
||||||
#include <linux/acpi.h>
|
#include <linux/acpi.h>
|
||||||
#include <linux/bitmap.h>
|
#include <linux/bitmap.h>
|
||||||
|
#include <linux/cleanup.h>
|
||||||
#include <linux/gpio/consumer.h>
|
#include <linux/gpio/consumer.h>
|
||||||
#include <linux/gpio/driver.h>
|
#include <linux/gpio/driver.h>
|
||||||
#include <linux/i2c.h>
|
#include <linux/i2c.h>
|
||||||
|
@ -519,12 +520,10 @@ static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
|
||||||
struct pca953x_chip *chip = gpiochip_get_data(gc);
|
struct pca953x_chip *chip = gpiochip_get_data(gc);
|
||||||
u8 dirreg = chip->recalc_addr(chip, chip->regs->direction, off);
|
u8 dirreg = chip->recalc_addr(chip, chip->regs->direction, off);
|
||||||
u8 bit = BIT(off % BANK_SZ);
|
u8 bit = BIT(off % BANK_SZ);
|
||||||
int ret;
|
|
||||||
|
|
||||||
mutex_lock(&chip->i2c_lock);
|
guard(mutex)(&chip->i2c_lock);
|
||||||
ret = regmap_write_bits(chip->regmap, dirreg, bit, bit);
|
|
||||||
mutex_unlock(&chip->i2c_lock);
|
return regmap_write_bits(chip->regmap, dirreg, bit, bit);
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int pca953x_gpio_direction_output(struct gpio_chip *gc,
|
static int pca953x_gpio_direction_output(struct gpio_chip *gc,
|
||||||
|
@ -536,17 +535,15 @@ static int pca953x_gpio_direction_output(struct gpio_chip *gc,
|
||||||
u8 bit = BIT(off % BANK_SZ);
|
u8 bit = BIT(off % BANK_SZ);
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
mutex_lock(&chip->i2c_lock);
|
guard(mutex)(&chip->i2c_lock);
|
||||||
|
|
||||||
/* set output level */
|
/* set output level */
|
||||||
ret = regmap_write_bits(chip->regmap, outreg, bit, val ? bit : 0);
|
ret = regmap_write_bits(chip->regmap, outreg, bit, val ? bit : 0);
|
||||||
if (ret)
|
if (ret)
|
||||||
goto exit;
|
return ret;
|
||||||
|
|
||||||
/* then direction */
|
/* then direction */
|
||||||
ret = regmap_write_bits(chip->regmap, dirreg, bit, 0);
|
return regmap_write_bits(chip->regmap, dirreg, bit, 0);
|
||||||
exit:
|
|
||||||
mutex_unlock(&chip->i2c_lock);
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
|
static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
|
||||||
|
@ -557,9 +554,8 @@ static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
|
||||||
u32 reg_val;
|
u32 reg_val;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
mutex_lock(&chip->i2c_lock);
|
scoped_guard(mutex, &chip->i2c_lock)
|
||||||
ret = regmap_read(chip->regmap, inreg, ®_val);
|
ret = regmap_read(chip->regmap, inreg, ®_val);
|
||||||
mutex_unlock(&chip->i2c_lock);
|
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
|
@ -572,9 +568,9 @@ static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
|
||||||
u8 outreg = chip->recalc_addr(chip, chip->regs->output, off);
|
u8 outreg = chip->recalc_addr(chip, chip->regs->output, off);
|
||||||
u8 bit = BIT(off % BANK_SZ);
|
u8 bit = BIT(off % BANK_SZ);
|
||||||
|
|
||||||
mutex_lock(&chip->i2c_lock);
|
guard(mutex)(&chip->i2c_lock);
|
||||||
|
|
||||||
regmap_write_bits(chip->regmap, outreg, bit, val ? bit : 0);
|
regmap_write_bits(chip->regmap, outreg, bit, val ? bit : 0);
|
||||||
mutex_unlock(&chip->i2c_lock);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int pca953x_gpio_get_direction(struct gpio_chip *gc, unsigned off)
|
static int pca953x_gpio_get_direction(struct gpio_chip *gc, unsigned off)
|
||||||
|
@ -585,9 +581,8 @@ static int pca953x_gpio_get_direction(struct gpio_chip *gc, unsigned off)
|
||||||
u32 reg_val;
|
u32 reg_val;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
mutex_lock(&chip->i2c_lock);
|
scoped_guard(mutex, &chip->i2c_lock)
|
||||||
ret = regmap_read(chip->regmap, dirreg, ®_val);
|
ret = regmap_read(chip->regmap, dirreg, ®_val);
|
||||||
mutex_unlock(&chip->i2c_lock);
|
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
|
@ -604,9 +599,8 @@ static int pca953x_gpio_get_multiple(struct gpio_chip *gc,
|
||||||
DECLARE_BITMAP(reg_val, MAX_LINE);
|
DECLARE_BITMAP(reg_val, MAX_LINE);
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
mutex_lock(&chip->i2c_lock);
|
scoped_guard(mutex, &chip->i2c_lock)
|
||||||
ret = pca953x_read_regs(chip, chip->regs->input, reg_val);
|
ret = pca953x_read_regs(chip, chip->regs->input, reg_val);
|
||||||
mutex_unlock(&chip->i2c_lock);
|
|
||||||
if (ret)
|
if (ret)
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
|
@ -621,16 +615,15 @@ static void pca953x_gpio_set_multiple(struct gpio_chip *gc,
|
||||||
DECLARE_BITMAP(reg_val, MAX_LINE);
|
DECLARE_BITMAP(reg_val, MAX_LINE);
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
mutex_lock(&chip->i2c_lock);
|
guard(mutex)(&chip->i2c_lock);
|
||||||
|
|
||||||
ret = pca953x_read_regs(chip, chip->regs->output, reg_val);
|
ret = pca953x_read_regs(chip, chip->regs->output, reg_val);
|
||||||
if (ret)
|
if (ret)
|
||||||
goto exit;
|
return;
|
||||||
|
|
||||||
bitmap_replace(reg_val, reg_val, bits, mask, gc->ngpio);
|
bitmap_replace(reg_val, reg_val, bits, mask, gc->ngpio);
|
||||||
|
|
||||||
pca953x_write_regs(chip, chip->regs->output, reg_val);
|
pca953x_write_regs(chip, chip->regs->output, reg_val);
|
||||||
exit:
|
|
||||||
mutex_unlock(&chip->i2c_lock);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int pca953x_gpio_set_pull_up_down(struct pca953x_chip *chip,
|
static int pca953x_gpio_set_pull_up_down(struct pca953x_chip *chip,
|
||||||
|
@ -638,7 +631,6 @@ static int pca953x_gpio_set_pull_up_down(struct pca953x_chip *chip,
|
||||||
unsigned long config)
|
unsigned long config)
|
||||||
{
|
{
|
||||||
enum pin_config_param param = pinconf_to_config_param(config);
|
enum pin_config_param param = pinconf_to_config_param(config);
|
||||||
|
|
||||||
u8 pull_en_reg = chip->recalc_addr(chip, PCAL953X_PULL_EN, offset);
|
u8 pull_en_reg = chip->recalc_addr(chip, PCAL953X_PULL_EN, offset);
|
||||||
u8 pull_sel_reg = chip->recalc_addr(chip, PCAL953X_PULL_SEL, offset);
|
u8 pull_sel_reg = chip->recalc_addr(chip, PCAL953X_PULL_SEL, offset);
|
||||||
u8 bit = BIT(offset % BANK_SZ);
|
u8 bit = BIT(offset % BANK_SZ);
|
||||||
|
@ -651,7 +643,7 @@ static int pca953x_gpio_set_pull_up_down(struct pca953x_chip *chip,
|
||||||
if (!(chip->driver_data & PCA_PCAL))
|
if (!(chip->driver_data & PCA_PCAL))
|
||||||
return -ENOTSUPP;
|
return -ENOTSUPP;
|
||||||
|
|
||||||
mutex_lock(&chip->i2c_lock);
|
guard(mutex)(&chip->i2c_lock);
|
||||||
|
|
||||||
/* Configure pull-up/pull-down */
|
/* Configure pull-up/pull-down */
|
||||||
if (param == PIN_CONFIG_BIAS_PULL_UP)
|
if (param == PIN_CONFIG_BIAS_PULL_UP)
|
||||||
|
@ -661,17 +653,13 @@ static int pca953x_gpio_set_pull_up_down(struct pca953x_chip *chip,
|
||||||
else
|
else
|
||||||
ret = 0;
|
ret = 0;
|
||||||
if (ret)
|
if (ret)
|
||||||
goto exit;
|
return ret;
|
||||||
|
|
||||||
/* Disable/Enable pull-up/pull-down */
|
/* Disable/Enable pull-up/pull-down */
|
||||||
if (param == PIN_CONFIG_BIAS_DISABLE)
|
if (param == PIN_CONFIG_BIAS_DISABLE)
|
||||||
ret = regmap_write_bits(chip->regmap, pull_en_reg, bit, 0);
|
return regmap_write_bits(chip->regmap, pull_en_reg, bit, 0);
|
||||||
else
|
else
|
||||||
ret = regmap_write_bits(chip->regmap, pull_en_reg, bit, bit);
|
return regmap_write_bits(chip->regmap, pull_en_reg, bit, bit);
|
||||||
|
|
||||||
exit:
|
|
||||||
mutex_unlock(&chip->i2c_lock);
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int pca953x_gpio_set_config(struct gpio_chip *gc, unsigned int offset,
|
static int pca953x_gpio_set_config(struct gpio_chip *gc, unsigned int offset,
|
||||||
|
@ -900,10 +888,8 @@ static irqreturn_t pca953x_irq_handler(int irq, void *devid)
|
||||||
|
|
||||||
bitmap_zero(pending, MAX_LINE);
|
bitmap_zero(pending, MAX_LINE);
|
||||||
|
|
||||||
mutex_lock(&chip->i2c_lock);
|
scoped_guard(mutex, &chip->i2c_lock)
|
||||||
ret = pca953x_irq_pending(chip, pending);
|
ret = pca953x_irq_pending(chip, pending);
|
||||||
mutex_unlock(&chip->i2c_lock);
|
|
||||||
|
|
||||||
if (ret) {
|
if (ret) {
|
||||||
ret = 0;
|
ret = 0;
|
||||||
|
|
||||||
|
@ -1235,26 +1221,21 @@ static int pca953x_restore_context(struct pca953x_chip *chip)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
mutex_lock(&chip->i2c_lock);
|
guard(mutex)(&chip->i2c_lock);
|
||||||
|
|
||||||
regcache_cache_only(chip->regmap, false);
|
regcache_cache_only(chip->regmap, false);
|
||||||
regcache_mark_dirty(chip->regmap);
|
regcache_mark_dirty(chip->regmap);
|
||||||
ret = pca953x_regcache_sync(chip);
|
ret = pca953x_regcache_sync(chip);
|
||||||
if (ret) {
|
if (ret)
|
||||||
mutex_unlock(&chip->i2c_lock);
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
|
||||||
|
|
||||||
ret = regcache_sync(chip->regmap);
|
return regcache_sync(chip->regmap);
|
||||||
mutex_unlock(&chip->i2c_lock);
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void pca953x_save_context(struct pca953x_chip *chip)
|
static void pca953x_save_context(struct pca953x_chip *chip)
|
||||||
{
|
{
|
||||||
mutex_lock(&chip->i2c_lock);
|
guard(mutex)(&chip->i2c_lock);
|
||||||
regcache_cache_only(chip->regmap, true);
|
regcache_cache_only(chip->regmap, true);
|
||||||
mutex_unlock(&chip->i2c_lock);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int pca953x_suspend(struct device *dev)
|
static int pca953x_suspend(struct device *dev)
|
||||||
|
|
Loading…
Reference in a new issue