blob: f1280ed8ed6dcb6a7b48fd5418d3c116e3bb7794 [file] [log] [blame]
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07001/*
2 * Atmel PIO2 Port Multiplexer support
3 *
4 * Copyright (C) 2004-2006 Atmel Corporation
5 *
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.
9 */
10
11#include <linux/clk.h>
12#include <linux/debugfs.h>
13#include <linux/fs.h>
14#include <linux/platform_device.h>
15
16#include <asm/io.h>
17
18#include <asm/arch/portmux.h>
19
20#include "pio.h"
21
22#define MAX_NR_PIO_DEVICES 8
23
24struct pio_device {
25 void __iomem *regs;
26 const struct platform_device *pdev;
27 struct clk *clk;
Haavard Skinnemoenc3e2a792006-12-04 13:46:52 +010028 u32 pinmux_mask;
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070029 char name[32];
30};
31
32static struct pio_device pio_dev[MAX_NR_PIO_DEVICES];
33
Haavard Skinnemoenc3e2a792006-12-04 13:46:52 +010034static struct pio_device *gpio_to_pio(unsigned int gpio)
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070035{
36 struct pio_device *pio;
Haavard Skinnemoenc3e2a792006-12-04 13:46:52 +010037 unsigned int index;
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070038
Haavard Skinnemoenc3e2a792006-12-04 13:46:52 +010039 index = gpio >> 5;
40 if (index >= MAX_NR_PIO_DEVICES)
41 return NULL;
42 pio = &pio_dev[index];
43 if (!pio->regs)
44 return NULL;
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070045
Haavard Skinnemoenc3e2a792006-12-04 13:46:52 +010046 return pio;
47}
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070048
Haavard Skinnemoenc3e2a792006-12-04 13:46:52 +010049/* Pin multiplexing API */
50
51void __init at32_select_periph(unsigned int pin, unsigned int periph,
52 unsigned long flags)
53{
54 struct pio_device *pio;
55 unsigned int pin_index = pin & 0x1f;
56 u32 mask = 1 << pin_index;
57
58 pio = gpio_to_pio(pin);
59 if (unlikely(!pio)) {
60 printk("pio: invalid pin %u\n", pin);
61 goto fail;
62 }
63
64 if (unlikely(test_and_set_bit(pin_index, &pio->pinmux_mask))) {
65 printk("%s: pin %u is busy\n", pio->name, pin_index);
66 goto fail;
67 }
68
69 pio_writel(pio, PUER, mask);
70 if (periph)
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070071 pio_writel(pio, BSR, mask);
72 else
73 pio_writel(pio, ASR, mask);
Haavard Skinnemoenc3e2a792006-12-04 13:46:52 +010074
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070075 pio_writel(pio, PDR, mask);
Haavard Skinnemoenc3e2a792006-12-04 13:46:52 +010076 if (!(flags & AT32_GPIOF_PULLUP))
77 pio_writel(pio, PUDR, mask);
78
79 return;
80
81fail:
82 dump_stack();
83}
84
85void __init at32_select_gpio(unsigned int pin, unsigned long flags)
86{
87 struct pio_device *pio;
88 unsigned int pin_index = pin & 0x1f;
89 u32 mask = 1 << pin_index;
90
91 pio = gpio_to_pio(pin);
92 if (unlikely(!pio)) {
93 printk("pio: invalid pin %u\n", pin);
94 goto fail;
95 }
96
97 if (unlikely(test_and_set_bit(pin_index, &pio->pinmux_mask))) {
98 printk("%s: pin %u is busy\n", pio->name, pin_index);
99 goto fail;
100 }
101
102 pio_writel(pio, PUER, mask);
103 if (flags & AT32_GPIOF_HIGH)
104 pio_writel(pio, SODR, mask);
105 else
106 pio_writel(pio, CODR, mask);
107 if (flags & AT32_GPIOF_OUTPUT)
108 pio_writel(pio, OER, mask);
109 else
110 pio_writel(pio, ODR, mask);
111
112 pio_writel(pio, PER, mask);
113 if (!(flags & AT32_GPIOF_PULLUP))
114 pio_writel(pio, PUDR, mask);
115
116 return;
117
118fail:
119 dump_stack();
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700120}
121
122static int __init pio_probe(struct platform_device *pdev)
123{
124 struct pio_device *pio = NULL;
125
126 BUG_ON(pdev->id >= MAX_NR_PIO_DEVICES);
127 pio = &pio_dev[pdev->id];
128 BUG_ON(!pio->regs);
129
130 /* TODO: Interrupts */
131
132 platform_set_drvdata(pdev, pio);
133
134 printk(KERN_INFO "%s: Atmel Port Multiplexer at 0x%p (irq %d)\n",
135 pio->name, pio->regs, platform_get_irq(pdev, 0));
136
137 return 0;
138}
139
140static struct platform_driver pio_driver = {
141 .probe = pio_probe,
142 .driver = {
143 .name = "pio",
144 },
145};
146
147static int __init pio_init(void)
148{
149 return platform_driver_register(&pio_driver);
150}
151subsys_initcall(pio_init);
152
153void __init at32_init_pio(struct platform_device *pdev)
154{
155 struct resource *regs;
156 struct pio_device *pio;
157
158 if (pdev->id > MAX_NR_PIO_DEVICES) {
159 dev_err(&pdev->dev, "only %d PIO devices supported\n",
160 MAX_NR_PIO_DEVICES);
161 return;
162 }
163
164 pio = &pio_dev[pdev->id];
165 snprintf(pio->name, sizeof(pio->name), "pio%d", pdev->id);
166
167 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
168 if (!regs) {
169 dev_err(&pdev->dev, "no mmio resource defined\n");
170 return;
171 }
172
173 pio->clk = clk_get(&pdev->dev, "mck");
174 if (IS_ERR(pio->clk))
175 /*
176 * This is a fatal error, but if we continue we might
177 * be so lucky that we manage to initialize the
178 * console and display this message...
179 */
180 dev_err(&pdev->dev, "no mck clock defined\n");
181 else
182 clk_enable(pio->clk);
183
184 pio->pdev = pdev;
185 pio->regs = ioremap(regs->start, regs->end - regs->start + 1);
186
187 pio_writel(pio, ODR, ~0UL);
188 pio_writel(pio, PER, ~0UL);
189}