Skip to content

Commit 36fb721

Browse files
committed
gpio: exar: switch to using regmap
We can simplify the code in gpio-exar by using regmap. This allows us to drop the mutex (regmap provides its own locking) and we can also reuse regmap's bit operations instead of implementing our own update function. Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com> Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
1 parent 696868d commit 36fb721

File tree

2 files changed

+38
-54
lines changed

2 files changed

+38
-54
lines changed

drivers/gpio/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ config GPIO_EP93XX
255255
config GPIO_EXAR
256256
tristate "Support for GPIO pins on XR17V352/354/358"
257257
depends on SERIAL_8250_EXAR
258+
select REGMAP_MMIO
258259
help
259260
Selecting this option will enable handling of GPIO pins present
260261
on Exar XR17V352/354/358 chips.

drivers/gpio/gpio-exar.c

Lines changed: 37 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <linux/module.h>
1515
#include <linux/pci.h>
1616
#include <linux/platform_device.h>
17+
#include <linux/regmap.h>
1718

1819
#define EXAR_OFFSET_MPIOLVL_LO 0x90
1920
#define EXAR_OFFSET_MPIOSEL_LO 0x93
@@ -26,9 +27,8 @@ static DEFINE_IDA(ida_index);
2627

2728
struct exar_gpio_chip {
2829
struct gpio_chip gpio_chip;
29-
struct mutex lock;
30+
struct regmap *regmap;
3031
int index;
31-
void __iomem *regs;
3232
char name[20];
3333
unsigned int first_pin;
3434
};
@@ -53,51 +53,13 @@ exar_offset_to_bit(struct exar_gpio_chip *exar_gpio, unsigned int offset)
5353
return (offset + exar_gpio->first_pin) % 8;
5454
}
5555

56-
static void exar_update(struct gpio_chip *chip, unsigned int reg, int val,
57-
unsigned int offset)
58-
{
59-
struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
60-
int temp;
61-
62-
mutex_lock(&exar_gpio->lock);
63-
temp = readb(exar_gpio->regs + reg);
64-
temp &= ~BIT(offset);
65-
if (val)
66-
temp |= BIT(offset);
67-
writeb(temp, exar_gpio->regs + reg);
68-
mutex_unlock(&exar_gpio->lock);
69-
}
70-
71-
static int exar_set_direction(struct gpio_chip *chip, int direction,
72-
unsigned int offset)
73-
{
74-
struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
75-
unsigned int addr = exar_offset_to_sel_addr(exar_gpio, offset);
76-
unsigned int bit = exar_offset_to_bit(exar_gpio, offset);
77-
78-
exar_update(chip, addr, direction, bit);
79-
return 0;
80-
}
81-
82-
static int exar_get(struct gpio_chip *chip, unsigned int reg)
83-
{
84-
struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
85-
int value;
86-
87-
mutex_lock(&exar_gpio->lock);
88-
value = readb(exar_gpio->regs + reg);
89-
mutex_unlock(&exar_gpio->lock);
90-
91-
return value;
92-
}
93-
9456
static int exar_get_direction(struct gpio_chip *chip, unsigned int offset)
9557
{
9658
struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
9759
unsigned int addr = exar_offset_to_sel_addr(exar_gpio, offset);
9860
unsigned int bit = exar_offset_to_bit(exar_gpio, offset);
9961

100-
if (exar_get(chip, addr) & BIT(bit))
62+
if (regmap_test_bits(exar_gpio->regmap, addr, BIT(bit)))
10163
return GPIO_LINE_DIRECTION_IN;
10264

10365
return GPIO_LINE_DIRECTION_OUT;
@@ -109,7 +71,7 @@ static int exar_get_value(struct gpio_chip *chip, unsigned int offset)
10971
unsigned int addr = exar_offset_to_lvl_addr(exar_gpio, offset);
11072
unsigned int bit = exar_offset_to_bit(exar_gpio, offset);
11173

112-
return !!(exar_get(chip, addr) & BIT(bit));
74+
return !!(regmap_test_bits(exar_gpio->regmap, addr, BIT(bit)));
11375
}
11476

11577
static void exar_set_value(struct gpio_chip *chip, unsigned int offset,
@@ -119,21 +81,42 @@ static void exar_set_value(struct gpio_chip *chip, unsigned int offset,
11981
unsigned int addr = exar_offset_to_lvl_addr(exar_gpio, offset);
12082
unsigned int bit = exar_offset_to_bit(exar_gpio, offset);
12183

122-
exar_update(chip, addr, value, bit);
84+
if (value)
85+
regmap_set_bits(exar_gpio->regmap, addr, BIT(bit));
86+
else
87+
regmap_clear_bits(exar_gpio->regmap, addr, BIT(bit));
12388
}
12489

12590
static int exar_direction_output(struct gpio_chip *chip, unsigned int offset,
12691
int value)
12792
{
93+
struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
94+
unsigned int addr = exar_offset_to_sel_addr(exar_gpio, offset);
95+
unsigned int bit = exar_offset_to_bit(exar_gpio, offset);
96+
12897
exar_set_value(chip, offset, value);
129-
return exar_set_direction(chip, 0, offset);
98+
regmap_clear_bits(exar_gpio->regmap, addr, BIT(bit));
99+
100+
return 0;
130101
}
131102

132103
static int exar_direction_input(struct gpio_chip *chip, unsigned int offset)
133104
{
134-
return exar_set_direction(chip, 1, offset);
105+
struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
106+
unsigned int addr = exar_offset_to_sel_addr(exar_gpio, offset);
107+
unsigned int bit = exar_offset_to_bit(exar_gpio, offset);
108+
109+
regmap_set_bits(exar_gpio->regmap, addr, BIT(bit));
110+
111+
return 0;
135112
}
136113

114+
static const struct regmap_config exar_regmap_config = {
115+
.name = "exar-gpio",
116+
.reg_bits = 16,
117+
.val_bits = 8,
118+
};
119+
137120
static int gpio_exar_probe(struct platform_device *pdev)
138121
{
139122
struct device *dev = &pdev->dev;
@@ -163,13 +146,17 @@ static int gpio_exar_probe(struct platform_device *pdev)
163146
if (!exar_gpio)
164147
return -ENOMEM;
165148

166-
mutex_init(&exar_gpio->lock);
149+
/*
150+
* We don't need to check the return values of mmio regmap operations (unless
151+
* the regmap has a clock attached which is not the case here).
152+
*/
153+
exar_gpio->regmap = devm_regmap_init_mmio(dev, p, &exar_regmap_config);
154+
if (IS_ERR(exar_gpio->regmap))
155+
return PTR_ERR(exar_gpio->regmap);
167156

168157
index = ida_alloc(&ida_index, GFP_KERNEL);
169-
if (index < 0) {
170-
ret = index;
171-
goto err_mutex_destroy;
172-
}
158+
if (index < 0)
159+
return index;
173160

174161
sprintf(exar_gpio->name, "exar_gpio%d", index);
175162
exar_gpio->gpio_chip.label = exar_gpio->name;
@@ -181,7 +168,6 @@ static int gpio_exar_probe(struct platform_device *pdev)
181168
exar_gpio->gpio_chip.set = exar_set_value;
182169
exar_gpio->gpio_chip.base = -1;
183170
exar_gpio->gpio_chip.ngpio = ngpios;
184-
exar_gpio->regs = p;
185171
exar_gpio->index = index;
186172
exar_gpio->first_pin = first_pin;
187173

@@ -195,8 +181,6 @@ static int gpio_exar_probe(struct platform_device *pdev)
195181

196182
err_destroy:
197183
ida_free(&ida_index, index);
198-
err_mutex_destroy:
199-
mutex_destroy(&exar_gpio->lock);
200184
return ret;
201185
}
202186

@@ -205,7 +189,6 @@ static int gpio_exar_remove(struct platform_device *pdev)
205189
struct exar_gpio_chip *exar_gpio = platform_get_drvdata(pdev);
206190

207191
ida_free(&ida_index, exar_gpio->index);
208-
mutex_destroy(&exar_gpio->lock);
209192

210193
return 0;
211194
}

0 commit comments

Comments
 (0)