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 #include <plat/gpio-core.h>
27 #define OFFS_CON (0x00)
28 #define OFFS_DAT (0x04)
29 #define OFFS_UP (0x08)
31 static void samsung_gpio_pm_1bit_save(struct samsung_gpio_chip
*chip
)
33 chip
->pm_save
[0] = __raw_readl(chip
->base
+ OFFS_CON
);
34 chip
->pm_save
[1] = __raw_readl(chip
->base
+ OFFS_DAT
);
37 static void samsung_gpio_pm_1bit_resume(struct samsung_gpio_chip
*chip
)
39 void __iomem
*base
= chip
->base
;
40 u32 old_gpcon
= __raw_readl(base
+ OFFS_CON
);
41 u32 old_gpdat
= __raw_readl(base
+ OFFS_DAT
);
42 u32 gps_gpcon
= chip
->pm_save
[0];
43 u32 gps_gpdat
= chip
->pm_save
[1];
46 /* GPACON only has one bit per control / data and no PULLUPs.
47 * GPACON[x] = 0 => Output, 1 => SFN */
49 /* first set all SFN bits to SFN */
51 gpcon
= old_gpcon
| gps_gpcon
;
52 __raw_writel(gpcon
, base
+ OFFS_CON
);
54 /* now set all the other bits */
56 __raw_writel(gps_gpdat
, base
+ OFFS_DAT
);
57 __raw_writel(gps_gpcon
, base
+ OFFS_CON
);
59 S3C_PMDBG("%s: CON %08x => %08x, DAT %08x => %08x\n",
60 chip
->chip
.label
, old_gpcon
, gps_gpcon
, old_gpdat
, gps_gpdat
);
63 struct samsung_gpio_pm samsung_gpio_pm_1bit
= {
64 .save
= samsung_gpio_pm_1bit_save
,
65 .resume
= samsung_gpio_pm_1bit_resume
,
68 static void samsung_gpio_pm_2bit_save(struct samsung_gpio_chip
*chip
)
70 chip
->pm_save
[0] = __raw_readl(chip
->base
+ OFFS_CON
);
71 chip
->pm_save
[1] = __raw_readl(chip
->base
+ OFFS_DAT
);
72 chip
->pm_save
[2] = __raw_readl(chip
->base
+ OFFS_UP
);
75 /* Test whether the given masked+shifted bits of an GPIO configuration
76 * are one of the SFN (special function) modes. */
78 static inline int is_sfn(unsigned long con
)
83 /* Test if the given masked+shifted GPIO configuration is an input */
85 static inline int is_in(unsigned long con
)
90 /* Test if the given masked+shifted GPIO configuration is an output */
92 static inline int is_out(unsigned long con
)
98 * samsung_gpio_pm_2bit_resume() - restore the given GPIO bank
99 * @chip: The chip information to resume.
101 * Restore one of the GPIO banks that was saved during suspend. This is
102 * not as simple as once thought, due to the possibility of glitches
103 * from the order that the CON and DAT registers are set in.
105 * The three states the pin can be are {IN,OUT,SFN} which gives us 9
106 * combinations of changes to check. Three of these, if the pin stays
107 * in the same configuration can be discounted. This leaves us with
110 * { IN => OUT } Change DAT first
111 * { IN => SFN } Change CON first
112 * { OUT => SFN } Change CON first, so new data will not glitch
113 * { OUT => IN } Change CON first, so new data will not glitch
114 * { SFN => IN } Change CON first
115 * { SFN => OUT } Change DAT first, so new data will not glitch [1]
117 * We do not currently deal with the UP registers as these control
118 * weak resistors, so a small delay in change should not need to bring
119 * these into the calculations.
121 * [1] this assumes that writing to a pin DAT whilst in SFN will set the
122 * state for when it is next output.
124 static void samsung_gpio_pm_2bit_resume(struct samsung_gpio_chip
*chip
)
126 void __iomem
*base
= chip
->base
;
127 u32 old_gpcon
= __raw_readl(base
+ OFFS_CON
);
128 u32 old_gpdat
= __raw_readl(base
+ OFFS_DAT
);
129 u32 gps_gpcon
= chip
->pm_save
[0];
130 u32 gps_gpdat
= chip
->pm_save
[1];
131 u32 gpcon
, old
, new, mask
;
132 u32 change_mask
= 0x0;
135 /* restore GPIO pull-up settings */
136 __raw_writel(chip
->pm_save
[2], base
+ OFFS_UP
);
138 /* Create a change_mask of all the items that need to have
139 * their CON value changed before their DAT value, so that
140 * we minimise the work between the two settings.
143 for (nr
= 0, mask
= 0x03; nr
< 32; nr
+= 2, mask
<<= 2) {
144 old
= (old_gpcon
& mask
) >> nr
;
145 new = (gps_gpcon
& mask
) >> nr
;
147 /* If there is no change, then skip */
152 /* If both are special function, then skip */
154 if (is_sfn(old
) && is_sfn(new))
157 /* Change is IN => OUT, do not change now */
159 if (is_in(old
) && is_out(new))
162 /* Change is SFN => OUT, do not change now */
164 if (is_sfn(old
) && is_out(new))
167 /* We should now be at the case of IN=>SFN,
168 * OUT=>SFN, OUT=>IN, SFN=>IN. */
174 /* Write the new CON settings */
176 gpcon
= old_gpcon
& ~change_mask
;
177 gpcon
|= gps_gpcon
& change_mask
;
179 __raw_writel(gpcon
, base
+ OFFS_CON
);
181 /* Now change any items that require DAT,CON */
183 __raw_writel(gps_gpdat
, base
+ OFFS_DAT
);
184 __raw_writel(gps_gpcon
, base
+ OFFS_CON
);
186 S3C_PMDBG("%s: CON %08x => %08x, DAT %08x => %08x\n",
187 chip
->chip
.label
, old_gpcon
, gps_gpcon
, old_gpdat
, gps_gpdat
);
190 struct samsung_gpio_pm samsung_gpio_pm_2bit
= {
191 .save
= samsung_gpio_pm_2bit_save
,
192 .resume
= samsung_gpio_pm_2bit_resume
,
195 #if defined(CONFIG_ARCH_S3C64XX) || defined(CONFIG_PLAT_S5P) \
196 || defined(CONFIG_ARCH_EXYNOS)
197 static void samsung_gpio_pm_4bit_save(struct samsung_gpio_chip
*chip
)
199 chip
->pm_save
[1] = __raw_readl(chip
->base
+ OFFS_CON
);
200 chip
->pm_save
[2] = __raw_readl(chip
->base
+ OFFS_DAT
);
201 chip
->pm_save
[3] = __raw_readl(chip
->base
+ OFFS_UP
);
203 if (chip
->chip
.ngpio
> 8)
204 chip
->pm_save
[0] = __raw_readl(chip
->base
- 4);
207 static u32
samsung_gpio_pm_4bit_mask(u32 old_gpcon
, u32 gps_gpcon
)
210 u32 change_mask
= 0x0;
213 for (nr
= 0, mask
= 0x0f; nr
< 16; nr
+= 4, mask
<<= 4) {
214 old
= (old_gpcon
& mask
) >> nr
;
215 new = (gps_gpcon
& mask
) >> nr
;
217 /* If there is no change, then skip */
222 /* If both are special function, then skip */
224 if (is_sfn(old
) && is_sfn(new))
227 /* Change is IN => OUT, do not change now */
229 if (is_in(old
) && is_out(new))
232 /* Change is SFN => OUT, do not change now */
234 if (is_sfn(old
) && is_out(new))
237 /* We should now be at the case of IN=>SFN,
238 * OUT=>SFN, OUT=>IN, SFN=>IN. */
246 static void samsung_gpio_pm_4bit_con(struct samsung_gpio_chip
*chip
, int index
)
248 void __iomem
*con
= chip
->base
+ (index
* 4);
249 u32 old_gpcon
= __raw_readl(con
);
250 u32 gps_gpcon
= chip
->pm_save
[index
+ 1];
253 mask
= samsung_gpio_pm_4bit_mask(old_gpcon
, gps_gpcon
);
255 gpcon
= old_gpcon
& ~mask
;
256 gpcon
|= gps_gpcon
& mask
;
258 __raw_writel(gpcon
, con
);
261 static void samsung_gpio_pm_4bit_resume(struct samsung_gpio_chip
*chip
)
263 void __iomem
*base
= chip
->base
;
265 u32 old_gpdat
= __raw_readl(base
+ OFFS_DAT
);
266 u32 gps_gpdat
= chip
->pm_save
[2];
268 /* First, modify the CON settings */
271 old_gpcon
[1] = __raw_readl(base
+ OFFS_CON
);
273 samsung_gpio_pm_4bit_con(chip
, 0);
274 if (chip
->chip
.ngpio
> 8) {
275 old_gpcon
[0] = __raw_readl(base
- 4);
276 samsung_gpio_pm_4bit_con(chip
, -1);
279 /* Now change the configurations that require DAT,CON */
281 __raw_writel(chip
->pm_save
[2], base
+ OFFS_DAT
);
282 __raw_writel(chip
->pm_save
[1], base
+ OFFS_CON
);
283 if (chip
->chip
.ngpio
> 8)
284 __raw_writel(chip
->pm_save
[0], base
- 4);
286 __raw_writel(chip
->pm_save
[2], base
+ OFFS_DAT
);
287 __raw_writel(chip
->pm_save
[3], base
+ OFFS_UP
);
289 if (chip
->chip
.ngpio
> 8) {
290 S3C_PMDBG("%s: CON4 %08x,%08x => %08x,%08x, DAT %08x => %08x\n",
291 chip
->chip
.label
, old_gpcon
[0], old_gpcon
[1],
292 __raw_readl(base
- 4),
293 __raw_readl(base
+ OFFS_CON
),
294 old_gpdat
, gps_gpdat
);
296 S3C_PMDBG("%s: CON4 %08x => %08x, DAT %08x => %08x\n",
297 chip
->chip
.label
, old_gpcon
[1],
298 __raw_readl(base
+ OFFS_CON
),
299 old_gpdat
, gps_gpdat
);
302 struct samsung_gpio_pm samsung_gpio_pm_4bit
= {
303 .save
= samsung_gpio_pm_4bit_save
,
304 .resume
= samsung_gpio_pm_4bit_resume
,
306 #endif /* CONFIG_ARCH_S3C64XX || CONFIG_PLAT_S5P || CONFIG_ARCH_EXYNOS */
309 * samsung_pm_save_gpio() - save gpio chip data for suspend
310 * @ourchip: The chip for suspend.
312 static void samsung_pm_save_gpio(struct samsung_gpio_chip
*ourchip
)
314 struct samsung_gpio_pm
*pm
= ourchip
->pm
;
316 if (pm
== NULL
|| pm
->save
== NULL
)
317 S3C_PMDBG("%s: no pm for %s\n", __func__
, ourchip
->chip
.label
);
323 * samsung_pm_save_gpios() - Save the state of the GPIO banks.
325 * For all the GPIO banks, save the state of each one ready for going
326 * into a suspend mode.
328 void samsung_pm_save_gpios(void)
330 struct samsung_gpio_chip
*ourchip
;
331 unsigned int gpio_nr
;
333 for (gpio_nr
= 0; gpio_nr
< S3C_GPIO_END
;) {
334 ourchip
= samsung_gpiolib_getchip(gpio_nr
);
340 samsung_pm_save_gpio(ourchip
);
342 S3C_PMDBG("%s: save %08x,%08x,%08x,%08x\n",
347 ourchip
->pm_save
[3]);
349 gpio_nr
+= ourchip
->chip
.ngpio
;
350 gpio_nr
+= CONFIG_S3C_GPIO_SPACE
;
355 * samsung_pm_resume_gpio() - restore gpio chip data after suspend
356 * @ourchip: The suspended chip.
358 static void samsung_pm_resume_gpio(struct samsung_gpio_chip
*ourchip
)
360 struct samsung_gpio_pm
*pm
= ourchip
->pm
;
362 if (pm
== NULL
|| pm
->resume
== NULL
)
363 S3C_PMDBG("%s: no pm for %s\n", __func__
, ourchip
->chip
.label
);
368 void samsung_pm_restore_gpios(void)
370 struct samsung_gpio_chip
*ourchip
;
371 unsigned int gpio_nr
;
373 for (gpio_nr
= 0; gpio_nr
< S3C_GPIO_END
;) {
374 ourchip
= samsung_gpiolib_getchip(gpio_nr
);
380 samsung_pm_resume_gpio(ourchip
);
382 gpio_nr
+= ourchip
->chip
.ngpio
;
383 gpio_nr
+= CONFIG_S3C_GPIO_SPACE
;