From: Bartosz Golaszewski <brgl@bgdev.pl>
To: Linus Walleij <linus.walleij@linaro.org>,
Bartosz Golaszewski <brgl@bgdev.pl>
Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org,
Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Subject: [PATCH 1/2] gpio: mmio: use lock guards
Date: Mon, 27 Oct 2025 14:48:02 +0100 [thread overview]
Message-ID: <20251027-gpio-mmio-refactor-v1-1-b0de7cd5a4b9@linaro.org> (raw)
In-Reply-To: <20251027-gpio-mmio-refactor-v1-0-b0de7cd5a4b9@linaro.org>
From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Shrink the code by a couple lines and improve lock management by using
lock guards from cleanup.h.
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
---
drivers/gpio/gpio-mmio.c | 40 ++++++++++++++--------------------------
1 file changed, 14 insertions(+), 26 deletions(-)
diff --git a/drivers/gpio/gpio-mmio.c b/drivers/gpio/gpio-mmio.c
index 7d6dd36cf1aeffeab96704821e0b280727346f6f..95ebbdf04343b81b1b8d836542c324bffb9c40e8 100644
--- a/drivers/gpio/gpio-mmio.c
+++ b/drivers/gpio/gpio-mmio.c
@@ -41,6 +41,7 @@ o ` ~~~~\___/~~~~ ` controller in FPGA is ,.`
*/
#include <linux/bitops.h>
+#include <linux/cleanup.h>
#include <linux/compiler.h>
#include <linux/err.h>
#include <linux/init.h>
@@ -229,9 +230,8 @@ static int bgpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
{
struct gpio_generic_chip *chip = to_gpio_generic_chip(gc);
unsigned long mask = bgpio_line2mask(gc, gpio);
- unsigned long flags;
- raw_spin_lock_irqsave(&chip->lock, flags);
+ guard(raw_spinlock)(&chip->lock);
if (val)
chip->sdata |= mask;
@@ -240,8 +240,6 @@ static int bgpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
chip->write_reg(chip->reg_dat, chip->sdata);
- raw_spin_unlock_irqrestore(&chip->lock, flags);
-
return 0;
}
@@ -262,9 +260,9 @@ static int bgpio_set_with_clear(struct gpio_chip *gc, unsigned int gpio,
static int bgpio_set_set(struct gpio_chip *gc, unsigned int gpio, int val)
{
struct gpio_generic_chip *chip = to_gpio_generic_chip(gc);
- unsigned long mask = bgpio_line2mask(gc, gpio), flags;
+ unsigned long mask = bgpio_line2mask(gc, gpio);
- raw_spin_lock_irqsave(&chip->lock, flags);
+ guard(raw_spinlock)(&chip->lock);
if (val)
chip->sdata |= mask;
@@ -273,8 +271,6 @@ static int bgpio_set_set(struct gpio_chip *gc, unsigned int gpio, int val)
chip->write_reg(chip->reg_set, chip->sdata);
- raw_spin_unlock_irqrestore(&chip->lock, flags);
-
return 0;
}
@@ -303,9 +299,9 @@ static void bgpio_set_multiple_single_reg(struct gpio_chip *gc,
void __iomem *reg)
{
struct gpio_generic_chip *chip = to_gpio_generic_chip(gc);
- unsigned long flags, set_mask, clear_mask;
+ unsigned long set_mask, clear_mask;
- raw_spin_lock_irqsave(&chip->lock, flags);
+ guard(raw_spinlock)(&chip->lock);
bgpio_multiple_get_masks(gc, mask, bits, &set_mask, &clear_mask);
@@ -313,8 +309,6 @@ static void bgpio_set_multiple_single_reg(struct gpio_chip *gc,
chip->sdata &= ~clear_mask;
chip->write_reg(reg, chip->sdata);
-
- raw_spin_unlock_irqrestore(&chip->lock, flags);
}
static int bgpio_set_multiple(struct gpio_chip *gc, unsigned long *mask,
@@ -394,18 +388,15 @@ static int bgpio_simple_dir_out(struct gpio_chip *gc, unsigned int gpio,
static int bgpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
{
struct gpio_generic_chip *chip = to_gpio_generic_chip(gc);
- unsigned long flags;
- raw_spin_lock_irqsave(&chip->lock, flags);
+ scoped_guard(raw_spinlock, &chip->lock) {
+ chip->sdir &= ~bgpio_line2mask(gc, gpio);
- chip->sdir &= ~bgpio_line2mask(gc, gpio);
-
- if (chip->reg_dir_in)
- chip->write_reg(chip->reg_dir_in, ~chip->sdir);
- if (chip->reg_dir_out)
- chip->write_reg(chip->reg_dir_out, chip->sdir);
-
- raw_spin_unlock_irqrestore(&chip->lock, flags);
+ if (chip->reg_dir_in)
+ chip->write_reg(chip->reg_dir_in, ~chip->sdir);
+ if (chip->reg_dir_out)
+ chip->write_reg(chip->reg_dir_out, chip->sdir);
+ }
return bgpio_dir_return(gc, gpio, false);
}
@@ -437,9 +428,8 @@ static int bgpio_get_dir(struct gpio_chip *gc, unsigned int gpio)
static void bgpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
{
struct gpio_generic_chip *chip = to_gpio_generic_chip(gc);
- unsigned long flags;
- raw_spin_lock_irqsave(&chip->lock, flags);
+ guard(raw_spinlock)(&chip->lock);
chip->sdir |= bgpio_line2mask(gc, gpio);
@@ -447,8 +437,6 @@ static void bgpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
chip->write_reg(chip->reg_dir_in, ~chip->sdir);
if (chip->reg_dir_out)
chip->write_reg(chip->reg_dir_out, chip->sdir);
-
- raw_spin_unlock_irqrestore(&chip->lock, flags);
}
static int bgpio_dir_out_dir_first(struct gpio_chip *gc, unsigned int gpio,
--
2.48.1
next prev parent reply other threads:[~2025-10-27 13:48 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-27 13:48 [PATCH 0/2] gpio: mmio: further refactoring Bartosz Golaszewski
2025-10-27 13:48 ` Bartosz Golaszewski [this message]
2025-10-27 22:45 ` [PATCH 1/2] gpio: mmio: use lock guards Linus Walleij
2025-10-27 13:48 ` [PATCH 2/2] gpio: mmio: drop the "bgpio" prefix Bartosz Golaszewski
2025-10-27 22:46 ` Linus Walleij
2025-10-30 9:28 ` [PATCH 0/2] gpio: mmio: further refactoring Bartosz Golaszewski
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251027-gpio-mmio-refactor-v1-1-b0de7cd5a4b9@linaro.org \
--to=brgl@bgdev.pl \
--cc=bartosz.golaszewski@linaro.org \
--cc=linus.walleij@linaro.org \
--cc=linux-gpio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).