Skip to content

Commit b9a557d

Browse files
author
Bartosz Golaszewski
committed
gpio: bt8xx: use lock guards
Reduce the code complexity by using automatic lock guards with the spinlock. Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Bartosz Golaszewski <[email protected]>
1 parent c948fee commit b9a557d

File tree

1 file changed

+14
-29
lines changed

1 file changed

+14
-29
lines changed

drivers/gpio/gpio-bt8xx.c

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
3232
*/
3333

34+
#include <linux/cleanup.h>
3435
#include <linux/module.h>
3536
#include <linux/pci.h>
3637
#include <linux/spinlock.h>
@@ -69,10 +70,9 @@ MODULE_PARM_DESC(gpiobase, "The GPIO number base. -1 means dynamic, which is the
6970
static int bt8xxgpio_gpio_direction_input(struct gpio_chip *gpio, unsigned nr)
7071
{
7172
struct bt8xxgpio *bg = gpiochip_get_data(gpio);
72-
unsigned long flags;
7373
u32 outen, data;
7474

75-
spin_lock_irqsave(&bg->lock, flags);
75+
guard(spinlock_irqsave)(&bg->lock);
7676

7777
data = bgread(BT848_GPIO_DATA);
7878
data &= ~(1 << nr);
@@ -82,20 +82,17 @@ static int bt8xxgpio_gpio_direction_input(struct gpio_chip *gpio, unsigned nr)
8282
outen &= ~(1 << nr);
8383
bgwrite(outen, BT848_GPIO_OUT_EN);
8484

85-
spin_unlock_irqrestore(&bg->lock, flags);
86-
8785
return 0;
8886
}
8987

9088
static int bt8xxgpio_gpio_get(struct gpio_chip *gpio, unsigned nr)
9189
{
9290
struct bt8xxgpio *bg = gpiochip_get_data(gpio);
93-
unsigned long flags;
9491
u32 val;
9592

96-
spin_lock_irqsave(&bg->lock, flags);
93+
guard(spinlock_irqsave)(&bg->lock);
94+
9795
val = bgread(BT848_GPIO_DATA);
98-
spin_unlock_irqrestore(&bg->lock, flags);
9996

10097
return !!(val & (1 << nr));
10198
}
@@ -104,10 +101,9 @@ static int bt8xxgpio_gpio_direction_output(struct gpio_chip *gpio,
104101
unsigned nr, int val)
105102
{
106103
struct bt8xxgpio *bg = gpiochip_get_data(gpio);
107-
unsigned long flags;
108104
u32 outen, data;
109105

110-
spin_lock_irqsave(&bg->lock, flags);
106+
guard(spinlock_irqsave)(&bg->lock);
111107

112108
outen = bgread(BT848_GPIO_OUT_EN);
113109
outen |= (1 << nr);
@@ -120,28 +116,23 @@ static int bt8xxgpio_gpio_direction_output(struct gpio_chip *gpio,
120116
data &= ~(1 << nr);
121117
bgwrite(data, BT848_GPIO_DATA);
122118

123-
spin_unlock_irqrestore(&bg->lock, flags);
124-
125119
return 0;
126120
}
127121

128122
static void bt8xxgpio_gpio_set(struct gpio_chip *gpio,
129123
unsigned nr, int val)
130124
{
131125
struct bt8xxgpio *bg = gpiochip_get_data(gpio);
132-
unsigned long flags;
133126
u32 data;
134127

135-
spin_lock_irqsave(&bg->lock, flags);
128+
guard(spinlock_irqsave)(&bg->lock);
136129

137130
data = bgread(BT848_GPIO_DATA);
138131
if (val)
139132
data |= (1 << nr);
140133
else
141134
data &= ~(1 << nr);
142135
bgwrite(data, BT848_GPIO_DATA);
143-
144-
spin_unlock_irqrestore(&bg->lock, flags);
145136
}
146137

147138
static void bt8xxgpio_gpio_setup(struct bt8xxgpio *bg)
@@ -236,18 +227,15 @@ static void bt8xxgpio_remove(struct pci_dev *pdev)
236227
static int bt8xxgpio_suspend(struct pci_dev *pdev, pm_message_t state)
237228
{
238229
struct bt8xxgpio *bg = pci_get_drvdata(pdev);
239-
unsigned long flags;
240-
241-
spin_lock_irqsave(&bg->lock, flags);
242230

243-
bg->saved_outen = bgread(BT848_GPIO_OUT_EN);
244-
bg->saved_data = bgread(BT848_GPIO_DATA);
231+
scoped_guard(spinlock_irqsave, &bg->lock) {
232+
bg->saved_outen = bgread(BT848_GPIO_OUT_EN);
233+
bg->saved_data = bgread(BT848_GPIO_DATA);
245234

246-
bgwrite(0, BT848_INT_MASK);
247-
bgwrite(~0x0, BT848_INT_STAT);
248-
bgwrite(0x0, BT848_GPIO_OUT_EN);
249-
250-
spin_unlock_irqrestore(&bg->lock, flags);
235+
bgwrite(0, BT848_INT_MASK);
236+
bgwrite(~0x0, BT848_INT_STAT);
237+
bgwrite(0x0, BT848_GPIO_OUT_EN);
238+
}
251239

252240
pci_save_state(pdev);
253241
pci_disable_device(pdev);
@@ -259,7 +247,6 @@ static int bt8xxgpio_suspend(struct pci_dev *pdev, pm_message_t state)
259247
static int bt8xxgpio_resume(struct pci_dev *pdev)
260248
{
261249
struct bt8xxgpio *bg = pci_get_drvdata(pdev);
262-
unsigned long flags;
263250
int err;
264251

265252
pci_set_power_state(pdev, PCI_D0);
@@ -268,7 +255,7 @@ static int bt8xxgpio_resume(struct pci_dev *pdev)
268255
return err;
269256
pci_restore_state(pdev);
270257

271-
spin_lock_irqsave(&bg->lock, flags);
258+
guard(spinlock_irqsave)(&bg->lock);
272259

273260
bgwrite(0, BT848_INT_MASK);
274261
bgwrite(0, BT848_GPIO_DMA_CTL);
@@ -277,8 +264,6 @@ static int bt8xxgpio_resume(struct pci_dev *pdev)
277264
bgwrite(bg->saved_data & bg->saved_outen,
278265
BT848_GPIO_DATA);
279266

280-
spin_unlock_irqrestore(&bg->lock, flags);
281-
282267
return 0;
283268
}
284269
#else

0 commit comments

Comments
 (0)