blob: 55d1a00dbd28dfeccf7744b183f1498f1ab11862 [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>
Linus Walleijc788aab2015-12-08 11:24:57 +010023#include <linux/gpio/driver.h>
wanzongshunc52d3d62009-06-10 15:49:32 +010024
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))
wanzongshunc52d3d62009-06-10 15:49:32 +010033
wanzongshun35c92212009-08-21 07:07:46 +010034#define NUC900_GPIO_CHIP(name, base_gpio, nr_gpio) \
wanzongshunc52d3d62009-06-10 15:49:32 +010035 { \
36 .chip = { \
37 .label = name, \
wanzongshun35c92212009-08-21 07:07:46 +010038 .direction_input = nuc900_dir_input, \
39 .direction_output = nuc900_dir_output, \
40 .get = nuc900_gpio_get, \
41 .set = nuc900_gpio_set, \
wanzongshunc52d3d62009-06-10 15:49:32 +010042 .base = base_gpio, \
43 .ngpio = nr_gpio, \
44 } \
45 }
46
wanzongshun35c92212009-08-21 07:07:46 +010047struct nuc900_gpio_chip {
wanzongshunc52d3d62009-06-10 15:49:32 +010048 struct gpio_chip chip;
49 void __iomem *regbase; /* Base of group register*/
50 spinlock_t gpio_lock;
51};
52
wanzongshun35c92212009-08-21 07:07:46 +010053static int nuc900_gpio_get(struct gpio_chip *chip, unsigned offset)
wanzongshunc52d3d62009-06-10 15:49:32 +010054{
Linus Walleijc788aab2015-12-08 11:24:57 +010055 struct nuc900_gpio_chip *nuc900_gpio = gpiochip_get_data(chip);
wanzongshun35c92212009-08-21 07:07:46 +010056 void __iomem *pio = nuc900_gpio->regbase + GPIO_IN;
wanzongshunc52d3d62009-06-10 15:49:32 +010057 unsigned int regval;
58
59 regval = __raw_readl(pio);
60 regval &= GPIO_GPIO(offset);
61
62 return (regval != 0);
63}
64
wanzongshun35c92212009-08-21 07:07:46 +010065static void nuc900_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
wanzongshunc52d3d62009-06-10 15:49:32 +010066{
Linus Walleijc788aab2015-12-08 11:24:57 +010067 struct nuc900_gpio_chip *nuc900_gpio = gpiochip_get_data(chip);
wanzongshun35c92212009-08-21 07:07:46 +010068 void __iomem *pio = nuc900_gpio->regbase + GPIO_OUT;
wanzongshunc52d3d62009-06-10 15:49:32 +010069 unsigned int regval;
70 unsigned long flags;
71
wanzongshun35c92212009-08-21 07:07:46 +010072 spin_lock_irqsave(&nuc900_gpio->gpio_lock, flags);
wanzongshunc52d3d62009-06-10 15:49:32 +010073
74 regval = __raw_readl(pio);
75
76 if (val)
77 regval |= GPIO_GPIO(offset);
78 else
79 regval &= ~GPIO_GPIO(offset);
80
81 __raw_writel(regval, pio);
82
wanzongshun35c92212009-08-21 07:07:46 +010083 spin_unlock_irqrestore(&nuc900_gpio->gpio_lock, flags);
wanzongshunc52d3d62009-06-10 15:49:32 +010084}
85
wanzongshun35c92212009-08-21 07:07:46 +010086static int nuc900_dir_input(struct gpio_chip *chip, unsigned offset)
wanzongshunc52d3d62009-06-10 15:49:32 +010087{
Linus Walleijc788aab2015-12-08 11:24:57 +010088 struct nuc900_gpio_chip *nuc900_gpio = gpiochip_get_data(chip);
wanzongshun35c92212009-08-21 07:07:46 +010089 void __iomem *pio = nuc900_gpio->regbase + GPIO_DIR;
wanzongshunc52d3d62009-06-10 15:49:32 +010090 unsigned int regval;
91 unsigned long flags;
92
wanzongshun35c92212009-08-21 07:07:46 +010093 spin_lock_irqsave(&nuc900_gpio->gpio_lock, flags);
wanzongshunc52d3d62009-06-10 15:49:32 +010094
95 regval = __raw_readl(pio);
96 regval &= ~GPIO_GPIO(offset);
97 __raw_writel(regval, pio);
98
wanzongshun35c92212009-08-21 07:07:46 +010099 spin_unlock_irqrestore(&nuc900_gpio->gpio_lock, flags);
wanzongshunc52d3d62009-06-10 15:49:32 +0100100
101 return 0;
102}
103
wanzongshun35c92212009-08-21 07:07:46 +0100104static int nuc900_dir_output(struct gpio_chip *chip, unsigned offset, int val)
wanzongshunc52d3d62009-06-10 15:49:32 +0100105{
Linus Walleijc788aab2015-12-08 11:24:57 +0100106 struct nuc900_gpio_chip *nuc900_gpio = gpiochip_get_data(chip);
wanzongshun35c92212009-08-21 07:07:46 +0100107 void __iomem *outreg = nuc900_gpio->regbase + GPIO_OUT;
108 void __iomem *pio = nuc900_gpio->regbase + GPIO_DIR;
wanzongshunc52d3d62009-06-10 15:49:32 +0100109 unsigned int regval;
110 unsigned long flags;
111
wanzongshun35c92212009-08-21 07:07:46 +0100112 spin_lock_irqsave(&nuc900_gpio->gpio_lock, flags);
wanzongshunc52d3d62009-06-10 15:49:32 +0100113
114 regval = __raw_readl(pio);
115 regval |= GPIO_GPIO(offset);
116 __raw_writel(regval, pio);
117
118 regval = __raw_readl(outreg);
119
120 if (val)
121 regval |= GPIO_GPIO(offset);
122 else
123 regval &= ~GPIO_GPIO(offset);
124
125 __raw_writel(regval, outreg);
126
wanzongshun35c92212009-08-21 07:07:46 +0100127 spin_unlock_irqrestore(&nuc900_gpio->gpio_lock, flags);
wanzongshunc52d3d62009-06-10 15:49:32 +0100128
129 return 0;
130}
131
wanzongshun35c92212009-08-21 07:07:46 +0100132static struct nuc900_gpio_chip nuc900_gpio[] = {
133 NUC900_GPIO_CHIP("GROUPC", 0, 16),
134 NUC900_GPIO_CHIP("GROUPD", 16, 10),
135 NUC900_GPIO_CHIP("GROUPE", 26, 14),
136 NUC900_GPIO_CHIP("GROUPF", 40, 10),
137 NUC900_GPIO_CHIP("GROUPG", 50, 17),
138 NUC900_GPIO_CHIP("GROUPH", 67, 8),
139 NUC900_GPIO_CHIP("GROUPI", 75, 17),
wanzongshunc52d3d62009-06-10 15:49:32 +0100140};
141
wanzongshun35c92212009-08-21 07:07:46 +0100142void __init nuc900_init_gpio(int nr_group)
wanzongshunc52d3d62009-06-10 15:49:32 +0100143{
144 unsigned i;
wanzongshun35c92212009-08-21 07:07:46 +0100145 struct nuc900_gpio_chip *gpio_chip;
wanzongshunc52d3d62009-06-10 15:49:32 +0100146
147 for (i = 0; i < nr_group; i++) {
wanzongshun35c92212009-08-21 07:07:46 +0100148 gpio_chip = &nuc900_gpio[i];
wanzongshunc52d3d62009-06-10 15:49:32 +0100149 spin_lock_init(&gpio_chip->gpio_lock);
150 gpio_chip->regbase = GPIO_BASE + i * GROUPINERV;
Linus Walleijc788aab2015-12-08 11:24:57 +0100151 gpiochip_add_data(&gpio_chip->chip, gpio_chip);
wanzongshunc52d3d62009-06-10 15:49:32 +0100152 }
153}