2 * Atmel PIO2 Port Multiplexer support
4 * Copyright (C) 2004-2006 Atmel Corporation
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include <linux/clk.h>
12 #include <linux/debugfs.h>
14 #include <linux/platform_device.h>
15 #include <linux/irq.h>
20 #include <mach/portmux.h>
24 #define MAX_NR_PIO_DEVICES 8
27 struct gpio_chip chip
;
29 const struct platform_device
*pdev
;
35 static struct pio_device pio_dev
[MAX_NR_PIO_DEVICES
];
37 static struct pio_device
*gpio_to_pio(unsigned int gpio
)
39 struct pio_device
*pio
;
43 if (index
>= MAX_NR_PIO_DEVICES
)
45 pio
= &pio_dev
[index
];
52 /* Pin multiplexing API */
53 static DEFINE_SPINLOCK(pio_lock
);
55 void __init
at32_select_periph(unsigned int port
, u32 pin_mask
,
56 unsigned int periph
, unsigned long flags
)
58 struct pio_device
*pio
;
60 /* assign and verify pio */
61 pio
= gpio_to_pio(port
);
63 printk(KERN_WARNING
"pio: invalid port %u\n", port
);
67 /* Test if any of the requested pins is already muxed */
69 if (unlikely(pio
->pinmux_mask
& pin_mask
)) {
70 printk(KERN_WARNING
"%s: pin(s) busy (requested 0x%x, busy 0x%x)\n",
71 pio
->name
, pin_mask
, pio
->pinmux_mask
& pin_mask
);
72 spin_unlock(&pio_lock
);
76 pio
->pinmux_mask
|= pin_mask
;
79 pio_writel(pio
, PUER
, pin_mask
);
81 /* select either peripheral A or B */
83 pio_writel(pio
, BSR
, pin_mask
);
85 pio_writel(pio
, ASR
, pin_mask
);
87 /* enable peripheral control */
88 pio_writel(pio
, PDR
, pin_mask
);
90 /* Disable pull ups if not requested. */
91 if (!(flags
& AT32_GPIOF_PULLUP
))
92 pio_writel(pio
, PUDR
, pin_mask
);
94 spin_unlock(&pio_lock
);
102 void __init
at32_select_gpio(unsigned int pin
, unsigned long flags
)
104 struct pio_device
*pio
;
105 unsigned int pin_index
= pin
& 0x1f;
106 u32 mask
= 1 << pin_index
;
108 pio
= gpio_to_pio(pin
);
109 if (unlikely(!pio
)) {
110 printk("pio: invalid pin %u\n", pin
);
114 if (unlikely(test_and_set_bit(pin_index
, &pio
->pinmux_mask
))) {
115 printk("%s: pin %u is busy\n", pio
->name
, pin_index
);
119 if (flags
& AT32_GPIOF_OUTPUT
) {
120 if (flags
& AT32_GPIOF_HIGH
)
121 pio_writel(pio
, SODR
, mask
);
123 pio_writel(pio
, CODR
, mask
);
124 if (flags
& AT32_GPIOF_MULTIDRV
)
125 pio_writel(pio
, MDER
, mask
);
127 pio_writel(pio
, MDDR
, mask
);
128 pio_writel(pio
, PUDR
, mask
);
129 pio_writel(pio
, OER
, mask
);
131 if (flags
& AT32_GPIOF_PULLUP
)
132 pio_writel(pio
, PUER
, mask
);
134 pio_writel(pio
, PUDR
, mask
);
135 if (flags
& AT32_GPIOF_DEGLITCH
)
136 pio_writel(pio
, IFER
, mask
);
138 pio_writel(pio
, IFDR
, mask
);
139 pio_writel(pio
, ODR
, mask
);
142 pio_writel(pio
, PER
, mask
);
151 * Undo a previous pin reservation. Will not affect the hardware
154 void at32_deselect_pin(unsigned int pin
)
156 struct pio_device
*pio
;
157 unsigned int pin_index
= pin
& 0x1f;
159 pio
= gpio_to_pio(pin
);
160 if (unlikely(!pio
)) {
161 printk("pio: invalid pin %u\n", pin
);
166 clear_bit(pin_index
, &pio
->pinmux_mask
);
169 /* Reserve a pin, preventing anyone else from changing its configuration. */
170 void __init
at32_reserve_pin(unsigned int port
, u32 pin_mask
)
172 struct pio_device
*pio
;
174 /* assign and verify pio */
175 pio
= gpio_to_pio(port
);
176 if (unlikely(!pio
)) {
177 printk(KERN_WARNING
"pio: invalid port %u\n", port
);
181 /* Test if any of the requested pins is already muxed */
182 spin_lock(&pio_lock
);
183 if (unlikely(pio
->pinmux_mask
& pin_mask
)) {
184 printk(KERN_WARNING
"%s: pin(s) busy (req. 0x%x, busy 0x%x)\n",
185 pio
->name
, pin_mask
, pio
->pinmux_mask
& pin_mask
);
186 spin_unlock(&pio_lock
);
191 pio
->pinmux_mask
|= pin_mask
;
192 spin_unlock(&pio_lock
);
199 /*--------------------------------------------------------------------------*/
203 static int direction_input(struct gpio_chip
*chip
, unsigned offset
)
205 struct pio_device
*pio
= container_of(chip
, struct pio_device
, chip
);
206 u32 mask
= 1 << offset
;
208 if (!(pio_readl(pio
, PSR
) & mask
))
211 pio_writel(pio
, ODR
, mask
);
215 static int gpio_get(struct gpio_chip
*chip
, unsigned offset
)
217 struct pio_device
*pio
= container_of(chip
, struct pio_device
, chip
);
219 return (pio_readl(pio
, PDSR
) >> offset
) & 1;
222 static void gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
);
224 static int direction_output(struct gpio_chip
*chip
, unsigned offset
, int value
)
226 struct pio_device
*pio
= container_of(chip
, struct pio_device
, chip
);
227 u32 mask
= 1 << offset
;
229 if (!(pio_readl(pio
, PSR
) & mask
))
232 gpio_set(chip
, offset
, value
);
233 pio_writel(pio
, OER
, mask
);
237 static void gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
239 struct pio_device
*pio
= container_of(chip
, struct pio_device
, chip
);
240 u32 mask
= 1 << offset
;
243 pio_writel(pio
, SODR
, mask
);
245 pio_writel(pio
, CODR
, mask
);
248 /*--------------------------------------------------------------------------*/
250 /* GPIO IRQ support */
252 static void gpio_irq_mask(struct irq_data
*d
)
254 unsigned gpio
= irq_to_gpio(d
->irq
);
255 struct pio_device
*pio
= &pio_dev
[gpio
>> 5];
257 pio_writel(pio
, IDR
, 1 << (gpio
& 0x1f));
260 static void gpio_irq_unmask(struct irq_data
*d
)
262 unsigned gpio
= irq_to_gpio(d
->irq
);
263 struct pio_device
*pio
= &pio_dev
[gpio
>> 5];
265 pio_writel(pio
, IER
, 1 << (gpio
& 0x1f));
268 static int gpio_irq_type(struct irq_data
*d
, unsigned type
)
270 if (type
!= IRQ_TYPE_EDGE_BOTH
&& type
!= IRQ_TYPE_NONE
)
276 static struct irq_chip gpio_irqchip
= {
278 .irq_mask
= gpio_irq_mask
,
279 .irq_unmask
= gpio_irq_unmask
,
280 .irq_set_type
= gpio_irq_type
,
283 static void gpio_irq_handler(unsigned irq
, struct irq_desc
*desc
)
285 struct pio_device
*pio
= irq_desc_get_chip_data(desc
);
288 gpio_irq
= (unsigned) irq_get_handler_data(irq
);
292 /* ack pending GPIO interrupts */
293 isr
= pio_readl(pio
, ISR
) & pio_readl(pio
, IMR
);
303 generic_handle_irq(i
);
309 gpio_irq_setup(struct pio_device
*pio
, int irq
, int gpio_irq
)
313 irq_set_chip_data(irq
, pio
);
314 irq_set_handler_data(irq
, (void *)gpio_irq
);
316 for (i
= 0; i
< 32; i
++, gpio_irq
++) {
317 irq_set_chip_data(gpio_irq
, pio
);
318 irq_set_chip_and_handler(gpio_irq
, &gpio_irqchip
,
322 irq_set_chained_handler(irq
, gpio_irq_handler
);
325 /*--------------------------------------------------------------------------*/
327 #ifdef CONFIG_DEBUG_FS
329 #include <linux/seq_file.h>
330 #include <linux/export.h>
333 * This shows more info than the generic gpio dump code:
334 * pullups, deglitching, open drain drive.
336 static void pio_bank_show(struct seq_file
*s
, struct gpio_chip
*chip
)
338 struct pio_device
*pio
= container_of(chip
, struct pio_device
, chip
);
339 u32 psr
, osr
, imr
, pdsr
, pusr
, ifsr
, mdsr
;
344 psr
= pio_readl(pio
, PSR
);
345 osr
= pio_readl(pio
, OSR
);
346 imr
= pio_readl(pio
, IMR
);
347 pdsr
= pio_readl(pio
, PDSR
);
348 pusr
= pio_readl(pio
, PUSR
);
349 ifsr
= pio_readl(pio
, IFSR
);
350 mdsr
= pio_readl(pio
, MDSR
);
352 bank
= 'A' + pio
->pdev
->id
;
354 for (i
= 0, mask
= 1; i
< 32; i
++, mask
<<= 1) {
357 label
= gpiochip_is_requested(chip
, i
);
358 if (!label
&& (imr
& mask
))
363 seq_printf(s
, " gpio-%-3d P%c%-2d (%-12s) %s %s %s",
364 chip
->base
+ i
, bank
, i
,
366 (osr
& mask
) ? "out" : "in ",
367 (mask
& pdsr
) ? "hi" : "lo",
368 (mask
& pusr
) ? " " : "up");
370 seq_printf(s
, " deglitch");
371 if ((osr
& mdsr
) & mask
)
372 seq_printf(s
, " open-drain");
374 seq_printf(s
, " irq-%d edge-both",
375 gpio_to_irq(chip
->base
+ i
));
381 #define pio_bank_show NULL
385 /*--------------------------------------------------------------------------*/
387 static int __init
pio_probe(struct platform_device
*pdev
)
389 struct pio_device
*pio
= NULL
;
390 int irq
= platform_get_irq(pdev
, 0);
391 int gpio_irq_base
= GPIO_IRQ_BASE
+ pdev
->id
* 32;
393 BUG_ON(pdev
->id
>= MAX_NR_PIO_DEVICES
);
394 pio
= &pio_dev
[pdev
->id
];
397 pio
->chip
.label
= pio
->name
;
398 pio
->chip
.base
= pdev
->id
* 32;
399 pio
->chip
.ngpio
= 32;
400 pio
->chip
.dev
= &pdev
->dev
;
401 pio
->chip
.owner
= THIS_MODULE
;
403 pio
->chip
.direction_input
= direction_input
;
404 pio
->chip
.get
= gpio_get
;
405 pio
->chip
.direction_output
= direction_output
;
406 pio
->chip
.set
= gpio_set
;
407 pio
->chip
.dbg_show
= pio_bank_show
;
409 gpiochip_add(&pio
->chip
);
411 gpio_irq_setup(pio
, irq
, gpio_irq_base
);
413 platform_set_drvdata(pdev
, pio
);
415 printk(KERN_DEBUG
"%s: base 0x%p, irq %d chains %d..%d\n",
416 pio
->name
, pio
->regs
, irq
, gpio_irq_base
, gpio_irq_base
+ 31);
421 static struct platform_driver pio_driver
= {
427 static int __init
pio_init(void)
429 return platform_driver_probe(&pio_driver
, pio_probe
);
431 postcore_initcall(pio_init
);
433 void __init
at32_init_pio(struct platform_device
*pdev
)
435 struct resource
*regs
;
436 struct pio_device
*pio
;
438 if (pdev
->id
> MAX_NR_PIO_DEVICES
) {
439 dev_err(&pdev
->dev
, "only %d PIO devices supported\n",
444 pio
= &pio_dev
[pdev
->id
];
445 snprintf(pio
->name
, sizeof(pio
->name
), "pio%d", pdev
->id
);
447 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
449 dev_err(&pdev
->dev
, "no mmio resource defined\n");
453 pio
->clk
= clk_get(&pdev
->dev
, "mck");
454 if (IS_ERR(pio
->clk
))
456 * This is a fatal error, but if we continue we might
457 * be so lucky that we manage to initialize the
458 * console and display this message...
460 dev_err(&pdev
->dev
, "no mck clock defined\n");
462 clk_enable(pio
->clk
);
465 pio
->regs
= ioremap(regs
->start
, resource_size(regs
));
467 /* start with irqs disabled and acked */
468 pio_writel(pio
, IDR
, ~0UL);
469 (void) pio_readl(pio
, ISR
);