blob: ba05aec7ea4b9bde50510a883a8f65f73d21ecbb [file] [log] [blame]
wanzongshunc52d3d62009-06-10 15:49:32 +01001/*
wanzongshun35c92212009-08-21 07:07:46 +01002 * linux/arch/arm/mach-w90x900/gpio.c
wanzongshunc52d3d62009-06-10 15:49:32 +01003 *
wanzongshun35c92212009-08-21 07:07:46 +01004 * Generic nuc900 GPIO handling
wanzongshunc52d3d62009-06-10 15:49:32 +01005 *
6 * Wan ZongShun <mcuos.com@gmail.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13#include <linux/clk.h>
14#include <linux/errno.h>
15#include <linux/interrupt.h>
16#include <linux/irq.h>
17#include <linux/debugfs.h>
18#include <linux/seq_file.h>
19#include <linux/kernel.h>
20#include <linux/list.h>
21#include <linux/module.h>
22#include <linux/io.h>
23#include <linux/gpio.h>
24
25#include <mach/hardware.h>
26
27#define GPIO_BASE (W90X900_VA_GPIO)
28#define GPIO_DIR (0x04)
29#define GPIO_OUT (0x08)
30#define GPIO_IN (0x0C)
31#define GROUPINERV (0x10)
32#define GPIO_GPIO(Nb) (0x00000001 << (Nb))
wanzongshun35c92212009-08-21 07:07:46 +010033#define to_nuc900_gpio_chip(c) container_of(c, struct nuc900_gpio_chip, chip)
wanzongshunc52d3d62009-06-10 15:49:32 +010034
wanzongshun35c92212009-08-21 07:07:46 +010035#define NUC900_GPIO_CHIP(name, base_gpio, nr_gpio) \
wanzongshunc52d3d62009-06-10 15:49:32 +010036 { \
37 .chip = { \
38 .label = name, \
wanzongshun35c92212009-08-21 07:07:46 +010039 .direction_input = nuc900_dir_input, \
40 .direction_output = nuc900_dir_output, \
41 .get = nuc900_gpio_get, \
42 .set = nuc900_gpio_set, \
wanzongshunc52d3d62009-06-10 15:49:32 +010043 .base = base_gpio, \
44 .ngpio = nr_gpio, \
45 } \
46 }
47
wanzongshun35c92212009-08-21 07:07:46 +010048struct nuc900_gpio_chip {
wanzongshunc52d3d62009-06-10 15:49:32 +010049 struct gpio_chip chip;
50 void __iomem *regbase; /* Base of group register*/
51 spinlock_t gpio_lock;
52};
53
wanzongshun35c92212009-08-21 07:07:46 +010054static int nuc900_gpio_get(struct gpio_chip *chip, unsigned offset)
wanzongshunc52d3d62009-06-10 15:49:32 +010055{
wanzongshun35c92212009-08-21 07:07:46 +010056 struct nuc900_gpio_chip *nuc900_gpio = to_nuc900_gpio_chip(chip);
57 void __iomem *pio = nuc900_gpio->regbase + GPIO_IN;
wanzongshunc52d3d62009-06-10 15:49:32 +010058 unsigned int regval;
59
60 regval = __raw_readl(pio);
61 regval &= GPIO_GPIO(offset);
62
63 return (regval != 0);
64}
65
wanzongshun35c92212009-08-21 07:07:46 +010066static void nuc900_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
wanzongshunc52d3d62009-06-10 15:49:32 +010067{
wanzongshun35c92212009-08-21 07:07:46 +010068 struct nuc900_gpio_chip *nuc900_gpio = to_nuc900_gpio_chip(chip);
69 void __iomem *pio = nuc900_gpio->regbase + GPIO_OUT;
wanzongshunc52d3d62009-06-10 15:49:32 +010070 unsigned int regval;
71 unsigned long flags;
72
wanzongshun35c92212009-08-21 07:07:46 +010073 spin_lock_irqsave(&nuc900_gpio->gpio_lock, flags);
wanzongshunc52d3d62009-06-10 15:49:32 +010074
75 regval = __raw_readl(pio);
76
77 if (val)
78 regval |= GPIO_GPIO(offset);
79 else
80 regval &= ~GPIO_GPIO(offset);
81
82 __raw_writel(regval, pio);
83
wanzongshun35c92212009-08-21 07:07:46 +010084 spin_unlock_irqrestore(&nuc900_gpio->gpio_lock, flags);
wanzongshunc52d3d62009-06-10 15:49:32 +010085}
86
wanzongshun35c92212009-08-21 07:07:46 +010087static int nuc900_dir_input(struct gpio_chip *chip, unsigned offset)
wanzongshunc52d3d62009-06-10 15:49:32 +010088{
wanzongshun35c92212009-08-21 07:07:46 +010089 struct nuc900_gpio_chip *nuc900_gpio = to_nuc900_gpio_chip(chip);
90 void __iomem *pio = nuc900_gpio->regbase + GPIO_DIR;
wanzongshunc52d3d62009-06-10 15:49:32 +010091 unsigned int regval;
92 unsigned long flags;
93
wanzongshun35c92212009-08-21 07:07:46 +010094 spin_lock_irqsave(&nuc900_gpio->gpio_lock, flags);
wanzongshunc52d3d62009-06-10 15:49:32 +010095
96 regval = __raw_readl(pio);
97 regval &= ~GPIO_GPIO(offset);
98 __raw_writel(regval, pio);
99
wanzongshun35c92212009-08-21 07:07:46 +0100100 spin_unlock_irqrestore(&nuc900_gpio->gpio_lock, flags);
wanzongshunc52d3d62009-06-10 15:49:32 +0100101
102 return 0;
103}
104
wanzongshun35c92212009-08-21 07:07:46 +0100105static int nuc900_dir_output(struct gpio_chip *chip, unsigned offset, int val)
wanzongshunc52d3d62009-06-10 15:49:32 +0100106{
wanzongshun35c92212009-08-21 07:07:46 +0100107 struct nuc900_gpio_chip *nuc900_gpio = to_nuc900_gpio_chip(chip);
108 void __iomem *outreg = nuc900_gpio->regbase + GPIO_OUT;
109 void __iomem *pio = nuc900_gpio->regbase + GPIO_DIR;
wanzongshunc52d3d62009-06-10 15:49:32 +0100110 unsigned int regval;
111 unsigned long flags;
112
wanzongshun35c92212009-08-21 07:07:46 +0100113 spin_lock_irqsave(&nuc900_gpio->gpio_lock, flags);
wanzongshunc52d3d62009-06-10 15:49:32 +0100114
115 regval = __raw_readl(pio);
116 regval |= GPIO_GPIO(offset);
117 __raw_writel(regval, pio);
118
119 regval = __raw_readl(outreg);
120
121 if (val)
122 regval |= GPIO_GPIO(offset);
123 else
124 regval &= ~GPIO_GPIO(offset);
125
126 __raw_writel(regval, outreg);
127
wanzongshun35c92212009-08-21 07:07:46 +0100128 spin_unlock_irqrestore(&nuc900_gpio->gpio_lock, flags);
wanzongshunc52d3d62009-06-10 15:49:32 +0100129
130 return 0;
131}
132
wanzongshun35c92212009-08-21 07:07:46 +0100133static struct nuc900_gpio_chip nuc900_gpio[] = {
134 NUC900_GPIO_CHIP("GROUPC", 0, 16),
135 NUC900_GPIO_CHIP("GROUPD", 16, 10),
136 NUC900_GPIO_CHIP("GROUPE", 26, 14),
137 NUC900_GPIO_CHIP("GROUPF", 40, 10),
138 NUC900_GPIO_CHIP("GROUPG", 50, 17),
139 NUC900_GPIO_CHIP("GROUPH", 67, 8),
140 NUC900_GPIO_CHIP("GROUPI", 75, 17),
wanzongshunc52d3d62009-06-10 15:49:32 +0100141};
142
wanzongshun35c92212009-08-21 07:07:46 +0100143void __init nuc900_init_gpio(int nr_group)
wanzongshunc52d3d62009-06-10 15:49:32 +0100144{
145 unsigned i;
wanzongshun35c92212009-08-21 07:07:46 +0100146 struct nuc900_gpio_chip *gpio_chip;
wanzongshunc52d3d62009-06-10 15:49:32 +0100147
148 for (i = 0; i < nr_group; i++) {
wanzongshun35c92212009-08-21 07:07:46 +0100149 gpio_chip = &nuc900_gpio[i];
wanzongshunc52d3d62009-06-10 15:49:32 +0100150 spin_lock_init(&gpio_chip->gpio_lock);
151 gpio_chip->regbase = GPIO_BASE + i * GROUPINERV;
152 gpiochip_add(&gpio_chip->chip);
153 }
154}