2 * arch/arm/mach-ns9xxx/gpio.c
4 * Copyright (C) 2006 by Digi International Inc.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published by
9 * the Free Software Foundation.
11 #include <linux/compiler.h>
12 #include <linux/init.h>
13 #include <linux/spinlock.h>
14 #include <linux/module.h>
16 #include <asm/arch-ns9xxx/gpio.h>
17 #include <asm/arch-ns9xxx/processor.h>
18 #include <asm/arch-ns9xxx/regs-bbu.h>
21 #include <asm/types.h>
22 #include <asm/bitops.h>
24 #if defined(CONFIG_PROCESSOR_NS9360)
26 #elif defined(CONFIG_PROCESSOR_NS9750)
30 /* protects BBU_GCONFx and BBU_GCTRLx */
31 static spinlock_t gpio_lock
= __SPIN_LOCK_UNLOCKED(gpio_lock
);
33 /* only access gpiores with atomic ops */
34 <<<<<<< HEAD
:arch
/arm
/mach
-ns9xxx
/gpio
.c
35 static DECLARE_BITMAP(gpiores
, GPIO_MAX
);
37 static DECLARE_BITMAP(gpiores
, GPIO_MAX
+ 1);
38 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/arm
/mach
-ns9xxx
/gpio
.c
40 static inline int ns9xxx_valid_gpio(unsigned gpio
)
42 #if defined(CONFIG_PROCESSOR_NS9360)
43 if (processor_is_ns9360())
47 #if defined(CONFIG_PROCESSOR_NS9750)
48 if (processor_is_ns9750())
55 static inline void __iomem
*ns9xxx_gpio_get_gconfaddr(unsigned gpio
)
58 return BBU_GCONFb1(gpio
/ 8);
61 * this could be optimised away on
62 * ns9750 only builds, but it isn't ...
64 return BBU_GCONFb2((gpio
- 56) / 8);
67 static inline void __iomem
*ns9xxx_gpio_get_gctrladdr(unsigned gpio
)
74 /* this could be optimised away on ns9750 only builds */
78 static inline void __iomem
*ns9xxx_gpio_get_gstataddr(unsigned gpio
)
85 /* this could be optimised away on ns9750 only builds */
89 int gpio_request(unsigned gpio
, const char *label
)
91 if (likely(ns9xxx_valid_gpio(gpio
)))
92 return test_and_set_bit(gpio
, gpiores
) ? -EBUSY
: 0;
96 EXPORT_SYMBOL(gpio_request
);
98 void gpio_free(unsigned gpio
)
100 clear_bit(gpio
, gpiores
);
103 EXPORT_SYMBOL(gpio_free
);
106 * each gpio can serve for 4 different purposes [0..3]. These are called
107 * "functions" and passed in the parameter func. Functions 0-2 are always some
108 * special things, function 3 is GPIO. If func == 3 dir specifies input or
109 * output, and with inv you can enable an inverter (independent of func).
111 static int __ns9xxx_gpio_configure(unsigned gpio
, int dir
, int inv
, int func
)
113 void __iomem
*conf
= ns9xxx_gpio_get_gconfaddr(gpio
);
117 spin_lock_irqsave(&gpio_lock
, flags
);
119 confval
= __raw_readl(conf
);
120 REGSETIM_IDX(confval
, BBU_GCONFx
, DIR, gpio
& 7, dir
);
121 REGSETIM_IDX(confval
, BBU_GCONFx
, INV
, gpio
& 7, inv
);
122 REGSETIM_IDX(confval
, BBU_GCONFx
, FUNC
, gpio
& 7, func
);
123 __raw_writel(confval
, conf
);
125 spin_unlock_irqrestore(&gpio_lock
, flags
);
130 int ns9xxx_gpio_configure(unsigned gpio
, int inv
, int func
)
132 if (likely(ns9xxx_valid_gpio(gpio
))) {
134 printk(KERN_WARNING
"use gpio_direction_input "
135 "or gpio_direction_output\n");
138 return __ns9xxx_gpio_configure(gpio
, 0, inv
, func
);
142 EXPORT_SYMBOL(ns9xxx_gpio_configure
);
144 int gpio_direction_input(unsigned gpio
)
146 if (likely(ns9xxx_valid_gpio(gpio
))) {
147 return __ns9xxx_gpio_configure(gpio
, 0, 0, 3);
151 EXPORT_SYMBOL(gpio_direction_input
);
153 int gpio_direction_output(unsigned gpio
, int value
)
155 if (likely(ns9xxx_valid_gpio(gpio
))) {
156 gpio_set_value(gpio
, value
);
158 return __ns9xxx_gpio_configure(gpio
, 1, 0, 3);
162 EXPORT_SYMBOL(gpio_direction_output
);
164 int gpio_get_value(unsigned gpio
)
166 void __iomem
*stat
= ns9xxx_gpio_get_gstataddr(gpio
);
169 ret
= 1 & (__raw_readl(stat
) >> (gpio
& 31));
173 EXPORT_SYMBOL(gpio_get_value
);
175 void gpio_set_value(unsigned gpio
, int value
)
177 void __iomem
*ctrl
= ns9xxx_gpio_get_gctrladdr(gpio
);
181 spin_lock_irqsave(&gpio_lock
, flags
);
183 ctrlval
= __raw_readl(ctrl
);
186 ctrlval
|= 1 << (gpio
& 31);
188 ctrlval
&= ~(1 << (gpio
& 31));
190 __raw_writel(ctrlval
, ctrl
);
192 spin_unlock_irqrestore(&gpio_lock
, flags
);
194 EXPORT_SYMBOL(gpio_set_value
);