2 /* linux/arch/arm/plat-s3c/pm-gpio.c
4 * Copyright 2008 Openmoko, Inc.
5 * Copyright 2008 Simtec Electronics
6 * Ben Dooks <ben@simtec.co.uk>
7 * http://armlinux.simtec.co.uk/
9 * S3C series GPIO PM code
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
16 #include <linux/kernel.h>
17 #include <linux/device.h>
18 #include <linux/init.h>
20 #include <linux/gpio.h>
22 #if defined(CONFIG_ARCH_S3C24XX) || defined(CONFIG_ARCH_S3C64XX)
23 #include <mach/gpio-samsung.h>
26 #include <plat/gpio-core.h>
31 #define OFFS_CON (0x00)
32 #define OFFS_DAT (0x04)
33 #define OFFS_UP (0x08)
35 static void samsung_gpio_pm_1bit_save(struct samsung_gpio_chip
*chip
)
37 chip
->pm_save
[0] = __raw_readl(chip
->base
+ OFFS_CON
);
38 chip
->pm_save
[1] = __raw_readl(chip
->base
+ OFFS_DAT
);
41 static void samsung_gpio_pm_1bit_resume(struct samsung_gpio_chip
*chip
)
43 void __iomem
*base
= chip
->base
;
44 u32 old_gpcon
= __raw_readl(base
+ OFFS_CON
);
45 u32 old_gpdat
= __raw_readl(base
+ OFFS_DAT
);
46 u32 gps_gpcon
= chip
->pm_save
[0];
47 u32 gps_gpdat
= chip
->pm_save
[1];
50 /* GPACON only has one bit per control / data and no PULLUPs.
51 * GPACON[x] = 0 => Output, 1 => SFN */
53 /* first set all SFN bits to SFN */
55 gpcon
= old_gpcon
| gps_gpcon
;
56 __raw_writel(gpcon
, base
+ OFFS_CON
);
58 /* now set all the other bits */
60 __raw_writel(gps_gpdat
, base
+ OFFS_DAT
);
61 __raw_writel(gps_gpcon
, base
+ OFFS_CON
);
63 S3C_PMDBG("%s: CON %08x => %08x, DAT %08x => %08x\n",
64 chip
->chip
.label
, old_gpcon
, gps_gpcon
, old_gpdat
, gps_gpdat
);
67 struct samsung_gpio_pm samsung_gpio_pm_1bit
= {
68 .save
= samsung_gpio_pm_1bit_save
,
69 .resume
= samsung_gpio_pm_1bit_resume
,
72 static void samsung_gpio_pm_2bit_save(struct samsung_gpio_chip
*chip
)
74 chip
->pm_save
[0] = __raw_readl(chip
->base
+ OFFS_CON
);
75 chip
->pm_save
[1] = __raw_readl(chip
->base
+ OFFS_DAT
);
76 chip
->pm_save
[2] = __raw_readl(chip
->base
+ OFFS_UP
);
79 /* Test whether the given masked+shifted bits of an GPIO configuration
80 * are one of the SFN (special function) modes. */
82 static inline int is_sfn(unsigned long con
)
87 /* Test if the given masked+shifted GPIO configuration is an input */
89 static inline int is_in(unsigned long con
)
94 /* Test if the given masked+shifted GPIO configuration is an output */
96 static inline int is_out(unsigned long con
)
102 * samsung_gpio_pm_2bit_resume() - restore the given GPIO bank
103 * @chip: The chip information to resume.
105 * Restore one of the GPIO banks that was saved during suspend. This is
106 * not as simple as once thought, due to the possibility of glitches
107 * from the order that the CON and DAT registers are set in.
109 * The three states the pin can be are {IN,OUT,SFN} which gives us 9
110 * combinations of changes to check. Three of these, if the pin stays
111 * in the same configuration can be discounted. This leaves us with
114 * { IN => OUT } Change DAT first
115 * { IN => SFN } Change CON first
116 * { OUT => SFN } Change CON first, so new data will not glitch
117 * { OUT => IN } Change CON first, so new data will not glitch
118 * { SFN => IN } Change CON first
119 * { SFN => OUT } Change DAT first, so new data will not glitch [1]
121 * We do not currently deal with the UP registers as these control
122 * weak resistors, so a small delay in change should not need to bring
123 * these into the calculations.
125 * [1] this assumes that writing to a pin DAT whilst in SFN will set the
126 * state for when it is next output.
128 static void samsung_gpio_pm_2bit_resume(struct samsung_gpio_chip
*chip
)
130 void __iomem
*base
= chip
->base
;
131 u32 old_gpcon
= __raw_readl(base
+ OFFS_CON
);
132 u32 old_gpdat
= __raw_readl(base
+ OFFS_DAT
);
133 u32 gps_gpcon
= chip
->pm_save
[0];
134 u32 gps_gpdat
= chip
->pm_save
[1];
135 u32 gpcon
, old
, new, mask
;
136 u32 change_mask
= 0x0;
139 /* restore GPIO pull-up settings */
140 __raw_writel(chip
->pm_save
[2], base
+ OFFS_UP
);
142 /* Create a change_mask of all the items that need to have
143 * their CON value changed before their DAT value, so that
144 * we minimise the work between the two settings.
147 for (nr
= 0, mask
= 0x03; nr
< 32; nr
+= 2, mask
<<= 2) {
148 old
= (old_gpcon
& mask
) >> nr
;
149 new = (gps_gpcon
& mask
) >> nr
;
151 /* If there is no change, then skip */
156 /* If both are special function, then skip */
158 if (is_sfn(old
) && is_sfn(new))
161 /* Change is IN => OUT, do not change now */
163 if (is_in(old
) && is_out(new))
166 /* Change is SFN => OUT, do not change now */
168 if (is_sfn(old
) && is_out(new))
171 /* We should now be at the case of IN=>SFN,
172 * OUT=>SFN, OUT=>IN, SFN=>IN. */
178 /* Write the new CON settings */
180 gpcon
= old_gpcon
& ~change_mask
;
181 gpcon
|= gps_gpcon
& change_mask
;
183 __raw_writel(gpcon
, base
+ OFFS_CON
);
185 /* Now change any items that require DAT,CON */
187 __raw_writel(gps_gpdat
, base
+ OFFS_DAT
);
188 __raw_writel(gps_gpcon
, base
+ OFFS_CON
);
190 S3C_PMDBG("%s: CON %08x => %08x, DAT %08x => %08x\n",
191 chip
->chip
.label
, old_gpcon
, gps_gpcon
, old_gpdat
, gps_gpdat
);
194 struct samsung_gpio_pm samsung_gpio_pm_2bit
= {
195 .save
= samsung_gpio_pm_2bit_save
,
196 .resume
= samsung_gpio_pm_2bit_resume
,
199 #if defined(CONFIG_ARCH_S3C64XX) || defined(CONFIG_PLAT_S5P) \
200 || defined(CONFIG_ARCH_EXYNOS)
201 static void samsung_gpio_pm_4bit_save(struct samsung_gpio_chip
*chip
)
203 chip
->pm_save
[1] = __raw_readl(chip
->base
+ OFFS_CON
);
204 chip
->pm_save
[2] = __raw_readl(chip
->base
+ OFFS_DAT
);
205 chip
->pm_save
[3] = __raw_readl(chip
->base
+ OFFS_UP
);
207 if (chip
->chip
.ngpio
> 8)
208 chip
->pm_save
[0] = __raw_readl(chip
->base
- 4);
211 static u32
samsung_gpio_pm_4bit_mask(u32 old_gpcon
, u32 gps_gpcon
)
214 u32 change_mask
= 0x0;
217 for (nr
= 0, mask
= 0x0f; nr
< 16; nr
+= 4, mask
<<= 4) {
218 old
= (old_gpcon
& mask
) >> nr
;
219 new = (gps_gpcon
& mask
) >> nr
;
221 /* If there is no change, then skip */
226 /* If both are special function, then skip */
228 if (is_sfn(old
) && is_sfn(new))
231 /* Change is IN => OUT, do not change now */
233 if (is_in(old
) && is_out(new))
236 /* Change is SFN => OUT, do not change now */
238 if (is_sfn(old
) && is_out(new))
241 /* We should now be at the case of IN=>SFN,
242 * OUT=>SFN, OUT=>IN, SFN=>IN. */
250 static void samsung_gpio_pm_4bit_con(struct samsung_gpio_chip
*chip
, int index
)
252 void __iomem
*con
= chip
->base
+ (index
* 4);
253 u32 old_gpcon
= __raw_readl(con
);
254 u32 gps_gpcon
= chip
->pm_save
[index
+ 1];
257 mask
= samsung_gpio_pm_4bit_mask(old_gpcon
, gps_gpcon
);
259 gpcon
= old_gpcon
& ~mask
;
260 gpcon
|= gps_gpcon
& mask
;
262 __raw_writel(gpcon
, con
);
265 static void samsung_gpio_pm_4bit_resume(struct samsung_gpio_chip
*chip
)
267 void __iomem
*base
= chip
->base
;
269 u32 old_gpdat
= __raw_readl(base
+ OFFS_DAT
);
270 u32 gps_gpdat
= chip
->pm_save
[2];
272 /* First, modify the CON settings */
275 old_gpcon
[1] = __raw_readl(base
+ OFFS_CON
);
277 samsung_gpio_pm_4bit_con(chip
, 0);
278 if (chip
->chip
.ngpio
> 8) {
279 old_gpcon
[0] = __raw_readl(base
- 4);
280 samsung_gpio_pm_4bit_con(chip
, -1);
283 /* Now change the configurations that require DAT,CON */
285 __raw_writel(chip
->pm_save
[2], base
+ OFFS_DAT
);
286 __raw_writel(chip
->pm_save
[1], base
+ OFFS_CON
);
287 if (chip
->chip
.ngpio
> 8)
288 __raw_writel(chip
->pm_save
[0], base
- 4);
290 __raw_writel(chip
->pm_save
[2], base
+ OFFS_DAT
);
291 __raw_writel(chip
->pm_save
[3], base
+ OFFS_UP
);
293 if (chip
->chip
.ngpio
> 8) {
294 S3C_PMDBG("%s: CON4 %08x,%08x => %08x,%08x, DAT %08x => %08x\n",
295 chip
->chip
.label
, old_gpcon
[0], old_gpcon
[1],
296 __raw_readl(base
- 4),
297 __raw_readl(base
+ OFFS_CON
),
298 old_gpdat
, gps_gpdat
);
300 S3C_PMDBG("%s: CON4 %08x => %08x, DAT %08x => %08x\n",
301 chip
->chip
.label
, old_gpcon
[1],
302 __raw_readl(base
+ OFFS_CON
),
303 old_gpdat
, gps_gpdat
);
306 struct samsung_gpio_pm samsung_gpio_pm_4bit
= {
307 .save
= samsung_gpio_pm_4bit_save
,
308 .resume
= samsung_gpio_pm_4bit_resume
,
310 #endif /* CONFIG_ARCH_S3C64XX || CONFIG_PLAT_S5P || CONFIG_ARCH_EXYNOS */
313 * samsung_pm_save_gpio() - save gpio chip data for suspend
314 * @ourchip: The chip for suspend.
316 static void samsung_pm_save_gpio(struct samsung_gpio_chip
*ourchip
)
318 struct samsung_gpio_pm
*pm
= ourchip
->pm
;
320 if (pm
== NULL
|| pm
->save
== NULL
)
321 S3C_PMDBG("%s: no pm for %s\n", __func__
, ourchip
->chip
.label
);
327 * samsung_pm_save_gpios() - Save the state of the GPIO banks.
329 * For all the GPIO banks, save the state of each one ready for going
330 * into a suspend mode.
332 void samsung_pm_save_gpios(void)
334 struct samsung_gpio_chip
*ourchip
;
335 unsigned int gpio_nr
;
337 for (gpio_nr
= 0; gpio_nr
< S3C_GPIO_END
;) {
338 ourchip
= samsung_gpiolib_getchip(gpio_nr
);
344 samsung_pm_save_gpio(ourchip
);
346 S3C_PMDBG("%s: save %08x,%08x,%08x,%08x\n",
351 ourchip
->pm_save
[3]);
353 gpio_nr
+= ourchip
->chip
.ngpio
;
354 gpio_nr
+= CONFIG_S3C_GPIO_SPACE
;
359 * samsung_pm_resume_gpio() - restore gpio chip data after suspend
360 * @ourchip: The suspended chip.
362 static void samsung_pm_resume_gpio(struct samsung_gpio_chip
*ourchip
)
364 struct samsung_gpio_pm
*pm
= ourchip
->pm
;
366 if (pm
== NULL
|| pm
->resume
== NULL
)
367 S3C_PMDBG("%s: no pm for %s\n", __func__
, ourchip
->chip
.label
);
372 void samsung_pm_restore_gpios(void)
374 struct samsung_gpio_chip
*ourchip
;
375 unsigned int gpio_nr
;
377 for (gpio_nr
= 0; gpio_nr
< S3C_GPIO_END
;) {
378 ourchip
= samsung_gpiolib_getchip(gpio_nr
);
384 samsung_pm_resume_gpio(ourchip
);
386 gpio_nr
+= ourchip
->chip
.ngpio
;
387 gpio_nr
+= CONFIG_S3C_GPIO_SPACE
;