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 static void samsung_gpio_pm_4bit_save(struct samsung_gpio_chip
*chip
)
202 chip
->pm_save
[1] = __raw_readl(chip
->base
+ OFFS_CON
);
203 chip
->pm_save
[2] = __raw_readl(chip
->base
+ OFFS_DAT
);
204 chip
->pm_save
[3] = __raw_readl(chip
->base
+ OFFS_UP
);
206 if (chip
->chip
.ngpio
> 8)
207 chip
->pm_save
[0] = __raw_readl(chip
->base
- 4);
210 static u32
samsung_gpio_pm_4bit_mask(u32 old_gpcon
, u32 gps_gpcon
)
213 u32 change_mask
= 0x0;
216 for (nr
= 0, mask
= 0x0f; nr
< 16; nr
+= 4, mask
<<= 4) {
217 old
= (old_gpcon
& mask
) >> nr
;
218 new = (gps_gpcon
& mask
) >> nr
;
220 /* If there is no change, then skip */
225 /* If both are special function, then skip */
227 if (is_sfn(old
) && is_sfn(new))
230 /* Change is IN => OUT, do not change now */
232 if (is_in(old
) && is_out(new))
235 /* Change is SFN => OUT, do not change now */
237 if (is_sfn(old
) && is_out(new))
240 /* We should now be at the case of IN=>SFN,
241 * OUT=>SFN, OUT=>IN, SFN=>IN. */
249 static void samsung_gpio_pm_4bit_con(struct samsung_gpio_chip
*chip
, int index
)
251 void __iomem
*con
= chip
->base
+ (index
* 4);
252 u32 old_gpcon
= __raw_readl(con
);
253 u32 gps_gpcon
= chip
->pm_save
[index
+ 1];
256 mask
= samsung_gpio_pm_4bit_mask(old_gpcon
, gps_gpcon
);
258 gpcon
= old_gpcon
& ~mask
;
259 gpcon
|= gps_gpcon
& mask
;
261 __raw_writel(gpcon
, con
);
264 static void samsung_gpio_pm_4bit_resume(struct samsung_gpio_chip
*chip
)
266 void __iomem
*base
= chip
->base
;
268 u32 old_gpdat
= __raw_readl(base
+ OFFS_DAT
);
269 u32 gps_gpdat
= chip
->pm_save
[2];
271 /* First, modify the CON settings */
274 old_gpcon
[1] = __raw_readl(base
+ OFFS_CON
);
276 samsung_gpio_pm_4bit_con(chip
, 0);
277 if (chip
->chip
.ngpio
> 8) {
278 old_gpcon
[0] = __raw_readl(base
- 4);
279 samsung_gpio_pm_4bit_con(chip
, -1);
282 /* Now change the configurations that require DAT,CON */
284 __raw_writel(chip
->pm_save
[2], base
+ OFFS_DAT
);
285 __raw_writel(chip
->pm_save
[1], base
+ OFFS_CON
);
286 if (chip
->chip
.ngpio
> 8)
287 __raw_writel(chip
->pm_save
[0], base
- 4);
289 __raw_writel(chip
->pm_save
[2], base
+ OFFS_DAT
);
290 __raw_writel(chip
->pm_save
[3], base
+ OFFS_UP
);
292 if (chip
->chip
.ngpio
> 8) {
293 S3C_PMDBG("%s: CON4 %08x,%08x => %08x,%08x, DAT %08x => %08x\n",
294 chip
->chip
.label
, old_gpcon
[0], old_gpcon
[1],
295 __raw_readl(base
- 4),
296 __raw_readl(base
+ OFFS_CON
),
297 old_gpdat
, gps_gpdat
);
299 S3C_PMDBG("%s: CON4 %08x => %08x, DAT %08x => %08x\n",
300 chip
->chip
.label
, old_gpcon
[1],
301 __raw_readl(base
+ OFFS_CON
),
302 old_gpdat
, gps_gpdat
);
305 struct samsung_gpio_pm samsung_gpio_pm_4bit
= {
306 .save
= samsung_gpio_pm_4bit_save
,
307 .resume
= samsung_gpio_pm_4bit_resume
,
309 #endif /* CONFIG_ARCH_S3C64XX || CONFIG_PLAT_S5P */
312 * samsung_pm_save_gpio() - save gpio chip data for suspend
313 * @ourchip: The chip for suspend.
315 static void samsung_pm_save_gpio(struct samsung_gpio_chip
*ourchip
)
317 struct samsung_gpio_pm
*pm
= ourchip
->pm
;
319 if (pm
== NULL
|| pm
->save
== NULL
)
320 S3C_PMDBG("%s: no pm for %s\n", __func__
, ourchip
->chip
.label
);
326 * samsung_pm_save_gpios() - Save the state of the GPIO banks.
328 * For all the GPIO banks, save the state of each one ready for going
329 * into a suspend mode.
331 void samsung_pm_save_gpios(void)
333 struct samsung_gpio_chip
*ourchip
;
334 unsigned int gpio_nr
;
336 for (gpio_nr
= 0; gpio_nr
< S3C_GPIO_END
;) {
337 ourchip
= samsung_gpiolib_getchip(gpio_nr
);
343 samsung_pm_save_gpio(ourchip
);
345 S3C_PMDBG("%s: save %08x,%08x,%08x,%08x\n",
350 ourchip
->pm_save
[3]);
352 gpio_nr
+= ourchip
->chip
.ngpio
;
353 gpio_nr
+= CONFIG_S3C_GPIO_SPACE
;
358 * samsung_pm_resume_gpio() - restore gpio chip data after suspend
359 * @ourchip: The suspended chip.
361 static void samsung_pm_resume_gpio(struct samsung_gpio_chip
*ourchip
)
363 struct samsung_gpio_pm
*pm
= ourchip
->pm
;
365 if (pm
== NULL
|| pm
->resume
== NULL
)
366 S3C_PMDBG("%s: no pm for %s\n", __func__
, ourchip
->chip
.label
);
371 void samsung_pm_restore_gpios(void)
373 struct samsung_gpio_chip
*ourchip
;
374 unsigned int gpio_nr
;
376 for (gpio_nr
= 0; gpio_nr
< S3C_GPIO_END
;) {
377 ourchip
= samsung_gpiolib_getchip(gpio_nr
);
383 samsung_pm_resume_gpio(ourchip
);
385 gpio_nr
+= ourchip
->chip
.ngpio
;
386 gpio_nr
+= CONFIG_S3C_GPIO_SPACE
;