Skip to content

Commit b98348b

Browse files
David BrownellLinus Torvalds
authored andcommitted
gpiolib: avr32 at32ap platform support
Teach AVR32 to use the "GPIO Library" when exposing its GPIOs, so that signals on external chips (like GPIO expanders) can easily be used. This mostly reorganizes some existing logic, with two minor changes in behavior: - The PSR registers are used instead of the previous "gpio_mask" values, matching AT91 behavior and removing some duplication between that role and that of "pinmux_mask". - NR_IRQs grew to acommodate a bank of external GPIOs. Eventually this number should probably become a board-specific config option. There's a debugfs dump of status for the built-in GPIOs, showing which pins have deglitching, pullups, or open drain drive enabled, as well as the ID string used when requesting each IRQ. Signed-off-by: David Brownell <dbrownell@users.sourceforge.net> Acked-by: Haavard Skinnemoen <hskinnemoen@atmel.com> Cc: Jean Delvare <khali@linux-fr.org> Cc: Eric Miao <eric.miao@marvell.com> Cc: Sam Ravnborg <sam@ravnborg.org> Cc: Philipp Zabel <philipp.zabel@gmail.com> Cc: Russell King <rmk@arm.linux.org.uk> Cc: Ben Gardner <bgardner@wabtec.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent b72540c commit b98348b

6 files changed

Lines changed: 121 additions & 94 deletions

File tree

‎arch/avr32/Kconfig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ config PLATFORM_AT32AP
8282
select SUBARCH_AVR32B
8383
select MMU
8484
select PERFORMANCE_COUNTERS
85+
select HAVE_GPIO_LIB
8586

8687
#
8788
# CPU types

‎arch/avr32/mach-at32ap/pio.c‎

Lines changed: 92 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
#define MAX_NR_PIO_DEVICES 8
2525

2626
struct pio_device {
27+
struct gpio_chip chip;
2728
void __iomem *regs;
2829
const struct platform_device *pdev;
2930
struct clk *clk;
3031
u32 pinmux_mask;
31-
u32 gpio_mask;
3232
char name[8];
3333
};
3434

@@ -64,7 +64,8 @@ void __init at32_select_periph(unsigned int pin, unsigned int periph,
6464
goto fail;
6565
}
6666

67-
if (unlikely(test_and_set_bit(pin_index, &pio->pinmux_mask))) {
67+
if (unlikely(test_and_set_bit(pin_index, &pio->pinmux_mask)
68+
|| gpiochip_is_requested(&pio->chip, pin_index))) {
6869
printk("%s: pin %u is busy\n", pio->name, pin_index);
6970
goto fail;
7071
}
@@ -79,9 +80,6 @@ void __init at32_select_periph(unsigned int pin, unsigned int periph,
7980
if (!(flags & AT32_GPIOF_PULLUP))
8081
pio_writel(pio, PUDR, mask);
8182

82-
/* gpio_request NOT allowed */
83-
set_bit(pin_index, &pio->gpio_mask);
84-
8583
return;
8684

8785
fail:
@@ -130,9 +128,6 @@ void __init at32_select_gpio(unsigned int pin, unsigned long flags)
130128

131129
pio_writel(pio, PER, mask);
132130

133-
/* gpio_request now allowed */
134-
clear_bit(pin_index, &pio->gpio_mask);
135-
136131
return;
137132

138133
fail:
@@ -166,96 +161,50 @@ void __init at32_reserve_pin(unsigned int pin)
166161

167162
/* GPIO API */
168163

169-
int gpio_request(unsigned int gpio, const char *label)
164+
static int direction_input(struct gpio_chip *chip, unsigned offset)
170165
{
171-
struct pio_device *pio;
172-
unsigned int pin;
173-
174-
pio = gpio_to_pio(gpio);
175-
if (!pio)
176-
return -ENODEV;
166+
struct pio_device *pio = container_of(chip, struct pio_device, chip);
167+
u32 mask = 1 << offset;
177168

178-
pin = gpio & 0x1f;
179-
if (test_and_set_bit(pin, &pio->gpio_mask))
180-
return -EBUSY;
169+
if (!(pio_readl(pio, PSR) & mask))
170+
return -EINVAL;
181171

172+
pio_writel(pio, ODR, mask);
182173
return 0;
183174
}
184-
EXPORT_SYMBOL(gpio_request);
185175

186-
void gpio_free(unsigned int gpio)
176+
static int gpio_get(struct gpio_chip *chip, unsigned offset)
187177
{
188-
struct pio_device *pio;
189-
unsigned int pin;
178+
struct pio_device *pio = container_of(chip, struct pio_device, chip);
190179

191-
pio = gpio_to_pio(gpio);
192-
if (!pio) {
193-
printk(KERN_ERR
194-
"gpio: attempted to free invalid pin %u\n", gpio);
195-
return;
196-
}
197-
198-
pin = gpio & 0x1f;
199-
if (!test_and_clear_bit(pin, &pio->gpio_mask))
200-
printk(KERN_ERR "gpio: freeing free or non-gpio pin %s-%u\n",
201-
pio->name, pin);
180+
return (pio_readl(pio, PDSR) >> offset) & 1;
202181
}
203-
EXPORT_SYMBOL(gpio_free);
204182

205-
int gpio_direction_input(unsigned int gpio)
206-
{
207-
struct pio_device *pio;
208-
unsigned int pin;
209-
210-
pio = gpio_to_pio(gpio);
211-
if (!pio)
212-
return -ENODEV;
213-
214-
pin = gpio & 0x1f;
215-
pio_writel(pio, ODR, 1 << pin);
216-
217-
return 0;
218-
}
219-
EXPORT_SYMBOL(gpio_direction_input);
183+
static void gpio_set(struct gpio_chip *chip, unsigned offset, int value);
220184

221-
int gpio_direction_output(unsigned int gpio, int value)
185+
static int direction_output(struct gpio_chip *chip, unsigned offset, int value)
222186
{
223-
struct pio_device *pio;
224-
unsigned int pin;
225-
226-
pio = gpio_to_pio(gpio);
227-
if (!pio)
228-
return -ENODEV;
187+
struct pio_device *pio = container_of(chip, struct pio_device, chip);
188+
u32 mask = 1 << offset;
229189

230-
gpio_set_value(gpio, value);
231-
232-
pin = gpio & 0x1f;
233-
pio_writel(pio, OER, 1 << pin);
190+
if (!(pio_readl(pio, PSR) & mask))
191+
return -EINVAL;
234192

193+
gpio_set(chip, offset, value);
194+
pio_writel(pio, OER, mask);
235195
return 0;
236196
}
237-
EXPORT_SYMBOL(gpio_direction_output);
238197

239-
int gpio_get_value(unsigned int gpio)
198+
static void gpio_set(struct gpio_chip *chip, unsigned offset, int value)
240199
{
241-
struct pio_device *pio = &pio_dev[gpio >> 5];
200+
struct pio_device *pio = container_of(chip, struct pio_device, chip);
201+
u32 mask = 1 << offset;
242202

243-
return (pio_readl(pio, PDSR) >> (gpio & 0x1f)) & 1;
244-
}
245-
EXPORT_SYMBOL(gpio_get_value);
246-
247-
void gpio_set_value(unsigned int gpio, int value)
248-
{
249-
struct pio_device *pio = &pio_dev[gpio >> 5];
250-
u32 mask;
251-
252-
mask = 1 << (gpio & 0x1f);
253203
if (value)
254204
pio_writel(pio, SODR, mask);
255205
else
256206
pio_writel(pio, CODR, mask);
257207
}
258-
EXPORT_SYMBOL(gpio_set_value);
259208

260209
/*--------------------------------------------------------------------------*/
261210

@@ -337,6 +286,63 @@ gpio_irq_setup(struct pio_device *pio, int irq, int gpio_irq)
337286
set_irq_chained_handler(irq, gpio_irq_handler);
338287
}
339288

289+
/*--------------------------------------------------------------------------*/
290+
291+
#ifdef CONFIG_DEBUG_FS
292+
293+
#include <linux/seq_file.h>
294+
295+
/*
296+
* This shows more info than the generic gpio dump code:
297+
* pullups, deglitching, open drain drive.
298+
*/
299+
static void pio_bank_show(struct seq_file *s, struct gpio_chip *chip)
300+
{
301+
struct pio_device *pio = container_of(chip, struct pio_device, chip);
302+
u32 psr, osr, imr, pdsr, pusr, ifsr, mdsr;
303+
unsigned i;
304+
u32 mask;
305+
char bank;
306+
307+
psr = pio_readl(pio, PSR);
308+
osr = pio_readl(pio, OSR);
309+
imr = pio_readl(pio, IMR);
310+
pdsr = pio_readl(pio, PDSR);
311+
pusr = pio_readl(pio, PUSR);
312+
ifsr = pio_readl(pio, IFSR);
313+
mdsr = pio_readl(pio, MDSR);
314+
315+
bank = 'A' + pio->pdev->id;
316+
317+
for (i = 0, mask = 1; i < 32; i++, mask <<= 1) {
318+
const char *label;
319+
320+
label = gpiochip_is_requested(chip, i);
321+
if (!label)
322+
continue;
323+
324+
seq_printf(s, " gpio-%-3d P%c%-2d (%-12s) %s %s %s",
325+
chip->base + i, bank, i,
326+
label,
327+
(osr & mask) ? "out" : "in ",
328+
(mask & pdsr) ? "hi" : "lo",
329+
(mask & pusr) ? " " : "up");
330+
if (ifsr & mask)
331+
seq_printf(s, " deglitch");
332+
if ((osr & mdsr) & mask)
333+
seq_printf(s, " open-drain");
334+
if (imr & mask)
335+
seq_printf(s, " irq-%d edge-both",
336+
gpio_to_irq(chip->base + i));
337+
seq_printf(s, "\n");
338+
}
339+
}
340+
341+
#else
342+
#define pio_bank_show NULL
343+
#endif
344+
345+
340346
/*--------------------------------------------------------------------------*/
341347

342348
static int __init pio_probe(struct platform_device *pdev)
@@ -349,6 +355,18 @@ static int __init pio_probe(struct platform_device *pdev)
349355
pio = &pio_dev[pdev->id];
350356
BUG_ON(!pio->regs);
351357

358+
pio->chip.label = pio->name;
359+
pio->chip.base = pdev->id * 32;
360+
pio->chip.ngpio = 32;
361+
362+
pio->chip.direction_input = direction_input;
363+
pio->chip.get = gpio_get;
364+
pio->chip.direction_output = direction_output;
365+
pio->chip.set = gpio_set;
366+
pio->chip.dbg_show = pio_bank_show;
367+
368+
gpiochip_add(&pio->chip);
369+
352370
gpio_irq_setup(pio, irq, gpio_irq_base);
353371

354372
platform_set_drvdata(pdev, pio);
@@ -406,12 +424,6 @@ void __init at32_init_pio(struct platform_device *pdev)
406424
pio->pdev = pdev;
407425
pio->regs = ioremap(regs->start, regs->end - regs->start + 1);
408426

409-
/*
410-
* request_gpio() is only valid for pins that have been
411-
* explicitly configured as GPIO and not previously requested
412-
*/
413-
pio->gpio_mask = ~0UL;
414-
415427
/* start with irqs disabled and acked */
416428
pio_writel(pio, IDR, ~0UL);
417429
(void) pio_readl(pio, ISR);

‎arch/avr32/mach-at32ap/pio.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#define PIO_OSR 0x0018
2020
#define PIO_IFER 0x0020
2121
#define PIO_IFDR 0x0024
22-
#define PIO_ISFR 0x0028
22+
#define PIO_IFSR 0x0028
2323
#define PIO_SODR 0x0030
2424
#define PIO_CODR 0x0034
2525
#define PIO_ODSR 0x0038

‎include/asm-avr32/arch-at32ap/at32ap700x.h‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
#define GPIO_PERIPH_A 0
1414
#define GPIO_PERIPH_B 1
1515

16-
#define NR_GPIO_CONTROLLERS 4
17-
1816
/*
1917
* Pin numbers identifying specific GPIO pins on the chip. They can
2018
* also be converted to IRQ numbers by passing them through

‎include/asm-avr32/arch-at32ap/gpio.h‎

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,36 @@
55
#include <asm/irq.h>
66

77

8-
/* Arch-neutral GPIO API */
9-
int __must_check gpio_request(unsigned int gpio, const char *label);
10-
void gpio_free(unsigned int gpio);
8+
/* Some GPIO chips can manage IRQs; some can't. The exact numbers can
9+
* be changed if needed, but for the moment they're not configurable.
10+
*/
11+
#define ARCH_NR_GPIOS (NR_GPIO_IRQS + 2 * 32)
1112

12-
int gpio_direction_input(unsigned int gpio);
13-
int gpio_direction_output(unsigned int gpio, int value);
14-
int gpio_get_value(unsigned int gpio);
15-
void gpio_set_value(unsigned int gpio, int value);
1613

17-
#include <asm-generic/gpio.h> /* cansleep wrappers */
14+
/* Arch-neutral GPIO API, supporting both "native" and external GPIOs. */
15+
#include <asm-generic/gpio.h>
16+
17+
static inline int gpio_get_value(unsigned int gpio)
18+
{
19+
return __gpio_get_value(gpio);
20+
}
21+
22+
static inline void gpio_set_value(unsigned int gpio, int value)
23+
{
24+
__gpio_set_value(gpio, value);
25+
}
26+
27+
static inline int gpio_cansleep(unsigned int gpio)
28+
{
29+
return __gpio_cansleep(gpio);
30+
}
31+
1832

1933
static inline int gpio_to_irq(unsigned int gpio)
2034
{
21-
return gpio + GPIO_IRQ_BASE;
35+
if (gpio < NR_GPIO_IRQS)
36+
return gpio + GPIO_IRQ_BASE;
37+
return -EINVAL;
2238
}
2339

2440
static inline int irq_to_gpio(unsigned int irq)

‎include/asm-avr32/arch-at32ap/irq.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33

44
#define EIM_IRQ_BASE NR_INTERNAL_IRQS
55
#define NR_EIM_IRQS 32
6-
76
#define AT32_EXTINT(n) (EIM_IRQ_BASE + (n))
87

98
#define GPIO_IRQ_BASE (EIM_IRQ_BASE + NR_EIM_IRQS)
10-
#define NR_GPIO_IRQS (5 * 32)
9+
#define NR_GPIO_CTLR (5 /*internal*/ + 1 /*external*/)
10+
#define NR_GPIO_IRQS (NR_GPIO_CTLR * 32)
1111

1212
#define NR_IRQS (GPIO_IRQ_BASE + NR_GPIO_IRQS)
1313

0 commit comments

Comments
 (0)