blob: 19ab89df24c1f438a8cddddf519fa007c32b47a8 [file] [log] [blame]
Ben Dooks21b23662008-10-31 16:14:34 +00001/* linux/arch/arm/plat-s3c/gpio-config.c
2 *
3 * Copyright 2008 Openmoko, Inc.
Ben Dooks97a33992010-05-06 10:27:16 +09004 * Copyright 2008-2010 Simtec Electronics
Ben Dooks21b23662008-10-31 16:14:34 +00005 * Ben Dooks <ben@simtec.co.uk>
6 * http://armlinux.simtec.co.uk/
7 *
8 * S3C series GPIO configuration core
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13*/
14
15#include <linux/kernel.h>
Ben Dooksdb756392009-03-10 23:21:48 +000016#include <linux/module.h>
Ben Dooks21b23662008-10-31 16:14:34 +000017#include <linux/gpio.h>
18#include <linux/io.h>
19
Ben Dookse856bb12010-01-19 17:14:46 +090020#include <plat/gpio-core.h>
Ben Dooks21b23662008-10-31 16:14:34 +000021#include <plat/gpio-cfg.h>
22#include <plat/gpio-cfg-helpers.h>
23
24int s3c_gpio_cfgpin(unsigned int pin, unsigned int config)
25{
26 struct s3c_gpio_chip *chip = s3c_gpiolib_getchip(pin);
27 unsigned long flags;
28 int offset;
29 int ret;
30
31 if (!chip)
32 return -EINVAL;
33
34 offset = pin - chip->chip.base;
35
36 local_irq_save(flags);
37 ret = s3c_gpio_do_setcfg(chip, offset, config);
38 local_irq_restore(flags);
39
40 return ret;
41}
Ben Dooksdb756392009-03-10 23:21:48 +000042EXPORT_SYMBOL(s3c_gpio_cfgpin);
Ben Dooks21b23662008-10-31 16:14:34 +000043
44int s3c_gpio_setpull(unsigned int pin, s3c_gpio_pull_t pull)
45{
46 struct s3c_gpio_chip *chip = s3c_gpiolib_getchip(pin);
47 unsigned long flags;
48 int offset, ret;
49
50 if (!chip)
51 return -EINVAL;
52
53 offset = pin - chip->chip.base;
54
55 local_irq_save(flags);
56 ret = s3c_gpio_do_setpull(chip, offset, pull);
57 local_irq_restore(flags);
58
59 return ret;
60}
Ben Dooksdb756392009-03-10 23:21:48 +000061EXPORT_SYMBOL(s3c_gpio_setpull);
Ben Dooks21b23662008-10-31 16:14:34 +000062
63#ifdef CONFIG_S3C_GPIO_CFG_S3C24XX
Ben Dooks9bbb8512010-04-30 19:30:35 +090064int s3c_gpio_setcfg_s3c24xx_a(struct s3c_gpio_chip *chip,
65 unsigned int off, unsigned int cfg)
Ben Dooks21b23662008-10-31 16:14:34 +000066{
67 void __iomem *reg = chip->base;
68 unsigned int shift = off;
69 u32 con;
70
71 if (s3c_gpio_is_cfg_special(cfg)) {
72 cfg &= 0xf;
73
74 /* Map output to 0, and SFN2 to 1 */
75 cfg -= 1;
76 if (cfg > 1)
77 return -EINVAL;
78
79 cfg <<= shift;
80 }
81
82 con = __raw_readl(reg);
83 con &= ~(0x1 << shift);
84 con |= cfg;
85 __raw_writel(con, reg);
86
87 return 0;
88}
89
Ben Dooks97a33992010-05-06 10:27:16 +090090unsigned s3c_gpio_getcfg_s3c24xx_a(struct s3c_gpio_chip *chip,
91 unsigned int off)
92{
93 u32 con;
94
95 con = __raw_readl(chip->base);
96 con >>= off;
97 con &= 1;
98 con++;
99
100 return S3C_GPIO_SFN(con);
101}
102
Ben Dooks21b23662008-10-31 16:14:34 +0000103int s3c_gpio_setcfg_s3c24xx(struct s3c_gpio_chip *chip,
104 unsigned int off, unsigned int cfg)
105{
106 void __iomem *reg = chip->base;
107 unsigned int shift = off * 2;
108 u32 con;
109
110 if (s3c_gpio_is_cfg_special(cfg)) {
111 cfg &= 0xf;
112 if (cfg > 3)
113 return -EINVAL;
114
115 cfg <<= shift;
116 }
117
118 con = __raw_readl(reg);
119 con &= ~(0x3 << shift);
120 con |= cfg;
121 __raw_writel(con, reg);
122
123 return 0;
124}
Ben Dooks97a33992010-05-06 10:27:16 +0900125
126unsigned int s3c_gpio_getcfg_s3c24xx(struct s3c_gpio_chip *chip,
127 unsigned int off)
128{
129 u32 con;
130
131 con = __raw_readl(chip->base);
132 con >>= off * 2;
133 con &= 3;
134
135 /* this conversion works for IN and OUT as well as special mode */
136 return S3C_GPIO_SPECIAL(con);
137}
Ben Dooks21b23662008-10-31 16:14:34 +0000138#endif
139
140#ifdef CONFIG_S3C_GPIO_CFG_S3C64XX
141int s3c_gpio_setcfg_s3c64xx_4bit(struct s3c_gpio_chip *chip,
142 unsigned int off, unsigned int cfg)
143{
144 void __iomem *reg = chip->base;
145 unsigned int shift = (off & 7) * 4;
146 u32 con;
147
Marek Szyprowski49fb88a2009-06-18 13:30:16 +0200148 if (off < 8 && chip->chip.ngpio > 8)
Ben Dooks21b23662008-10-31 16:14:34 +0000149 reg -= 4;
150
151 if (s3c_gpio_is_cfg_special(cfg)) {
152 cfg &= 0xf;
153 cfg <<= shift;
154 }
155
156 con = __raw_readl(reg);
157 con &= ~(0xf << shift);
158 con |= cfg;
159 __raw_writel(con, reg);
160
161 return 0;
162}
Ben Dooks97a33992010-05-06 10:27:16 +0900163
164unsigned s3c_gpio_getcfg_s3c64xx_4bit(struct s3c_gpio_chip *chip,
165 unsigned int off)
166{
167 void __iomem *reg = chip->base;
168 unsigned int shift = (off & 7) * 4;
169 u32 con;
170
171 if (off < 8 && chip->chip.ngpio > 8)
172 reg -= 4;
173
174 con = __raw_readl(reg);
175 con >>= shift;
176 con &= 0xf;
177
178 /* this conversion works for IN and OUT as well as special mode */
179 return S3C_GPIO_SPECIAL(con);
180}
181
Ben Dooks21b23662008-10-31 16:14:34 +0000182#endif /* CONFIG_S3C_GPIO_CFG_S3C64XX */
183
184#ifdef CONFIG_S3C_GPIO_PULL_UPDOWN
185int s3c_gpio_setpull_updown(struct s3c_gpio_chip *chip,
186 unsigned int off, s3c_gpio_pull_t pull)
187{
188 void __iomem *reg = chip->base + 0x08;
189 int shift = off * 2;
190 u32 pup;
191
192 pup = __raw_readl(reg);
193 pup &= ~(3 << shift);
194 pup |= pull << shift;
195 __raw_writel(pup, reg);
196
197 return 0;
198}
199
200s3c_gpio_pull_t s3c_gpio_getpull_updown(struct s3c_gpio_chip *chip,
201 unsigned int off)
202{
203 void __iomem *reg = chip->base + 0x08;
204 int shift = off * 2;
205 u32 pup = __raw_readl(reg);
206
207 pup >>= shift;
208 pup &= 0x3;
209 return (__force s3c_gpio_pull_t)pup;
210}
211#endif
Ben Dooks1ec72692010-05-03 14:39:45 +0900212
213#ifdef CONFIG_S3C_GPIO_PULL_UP
214int s3c_gpio_setpull_1up(struct s3c_gpio_chip *chip,
215 unsigned int off, s3c_gpio_pull_t pull)
216{
217 void __iomem *reg = chip->base + 0x08;
218 u32 pup = __raw_readl(reg);
219
220 pup = __raw_readl(reg);
221
222 if (pup == S3C_GPIO_PULL_UP)
223 pup &= ~(1 << off);
224 else if (pup == S3C_GPIO_PULL_NONE)
225 pup |= (1 << off);
226 else
227 return -EINVAL;
228
229 __raw_writel(pup, reg);
230 return 0;
231}
232
233s3c_gpio_pull_t s3c_gpio_getpull_1up(struct s3c_gpio_chip *chip,
234 unsigned int off)
235{
236 void __iomem *reg = chip->base + 0x08;
237 u32 pup = __raw_readl(reg);
238
239 pup &= (1 << off);
240 return pup ? S3C_GPIO_PULL_NONE : S3C_GPIO_PULL_UP;
241}
242#endif /* CONFIG_S3C_GPIO_PULL_UP */
243