Merge tag 's5pv210-dt' of git://git.kernel.org/pub/scm/linux/kernel/git/kgene/linux...
[deliverable/linux.git] / arch / arm / plat-samsung / pm-gpio.c
CommitLineData
d87964c4
BD
1
2/* linux/arch/arm/plat-s3c/pm-gpio.c
3 *
4 * Copyright 2008 Openmoko, Inc.
5 * Copyright 2008 Simtec Electronics
6 * Ben Dooks <ben@simtec.co.uk>
7 * http://armlinux.simtec.co.uk/
8 *
9 * S3C series GPIO PM code
10 *
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.
14*/
15
16#include <linux/kernel.h>
edbaa603 17#include <linux/device.h>
d87964c4
BD
18#include <linux/init.h>
19#include <linux/io.h>
20#include <linux/gpio.h>
21
785acec3 22#if defined(CONFIG_ARCH_S3C24XX) || defined(CONFIG_ARCH_S3C64XX)
b0161caa 23#include <mach/gpio-samsung.h>
785acec3
SK
24#endif
25
e856bb1f 26#include <plat/gpio-core.h>
d87964c4
BD
27#include <plat/pm.h>
28
29/* PM GPIO helpers */
30
31#define OFFS_CON (0x00)
32#define OFFS_DAT (0x04)
33#define OFFS_UP (0x08)
34
782d8a3c 35static void samsung_gpio_pm_1bit_save(struct samsung_gpio_chip *chip)
d87964c4
BD
36{
37 chip->pm_save[0] = __raw_readl(chip->base + OFFS_CON);
38 chip->pm_save[1] = __raw_readl(chip->base + OFFS_DAT);
39}
40
782d8a3c 41static void samsung_gpio_pm_1bit_resume(struct samsung_gpio_chip *chip)
d87964c4
BD
42{
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];
48 u32 gpcon;
49
50 /* GPACON only has one bit per control / data and no PULLUPs.
51 * GPACON[x] = 0 => Output, 1 => SFN */
52
53 /* first set all SFN bits to SFN */
54
55 gpcon = old_gpcon | gps_gpcon;
56 __raw_writel(gpcon, base + OFFS_CON);
57
58 /* now set all the other bits */
59
60 __raw_writel(gps_gpdat, base + OFFS_DAT);
61 __raw_writel(gps_gpcon, base + OFFS_CON);
62
63 S3C_PMDBG("%s: CON %08x => %08x, DAT %08x => %08x\n",
64 chip->chip.label, old_gpcon, gps_gpcon, old_gpdat, gps_gpdat);
65}
66
782d8a3c
KK
67struct samsung_gpio_pm samsung_gpio_pm_1bit = {
68 .save = samsung_gpio_pm_1bit_save,
69 .resume = samsung_gpio_pm_1bit_resume,
d87964c4
BD
70};
71
782d8a3c 72static void samsung_gpio_pm_2bit_save(struct samsung_gpio_chip *chip)
d87964c4
BD
73{
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);
77}
78
79/* Test whether the given masked+shifted bits of an GPIO configuration
80 * are one of the SFN (special function) modes. */
81
82static inline int is_sfn(unsigned long con)
83{
84 return con >= 2;
85}
86
87/* Test if the given masked+shifted GPIO configuration is an input */
88
89static inline int is_in(unsigned long con)
90{
91 return con == 0;
92}
93
94/* Test if the given masked+shifted GPIO configuration is an output */
95
96static inline int is_out(unsigned long con)
97{
98 return con == 1;
99}
100
101/**
782d8a3c 102 * samsung_gpio_pm_2bit_resume() - restore the given GPIO bank
d87964c4
BD
103 * @chip: The chip information to resume.
104 *
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.
108 *
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
112 * the following:
113 *
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]
120 *
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.
124 *
125 * [1] this assumes that writing to a pin DAT whilst in SFN will set the
126 * state for when it is next output.
127 */
782d8a3c 128static void samsung_gpio_pm_2bit_resume(struct samsung_gpio_chip *chip)
d87964c4
BD
129{
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;
137 int nr;
138
139 /* restore GPIO pull-up settings */
140 __raw_writel(chip->pm_save[2], base + OFFS_UP);
141
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.
145 */
146
147 for (nr = 0, mask = 0x03; nr < 32; nr += 2, mask <<= 2) {
148 old = (old_gpcon & mask) >> nr;
149 new = (gps_gpcon & mask) >> nr;
150
151 /* If there is no change, then skip */
152
153 if (old == new)
154 continue;
155
156 /* If both are special function, then skip */
157
158 if (is_sfn(old) && is_sfn(new))
159 continue;
160
161 /* Change is IN => OUT, do not change now */
162
163 if (is_in(old) && is_out(new))
164 continue;
165
166 /* Change is SFN => OUT, do not change now */
167
168 if (is_sfn(old) && is_out(new))
169 continue;
170
171 /* We should now be at the case of IN=>SFN,
172 * OUT=>SFN, OUT=>IN, SFN=>IN. */
173
174 change_mask |= mask;
175 }
176
177
178 /* Write the new CON settings */
179
180 gpcon = old_gpcon & ~change_mask;
181 gpcon |= gps_gpcon & change_mask;
182
183 __raw_writel(gpcon, base + OFFS_CON);
184
185 /* Now change any items that require DAT,CON */
186
187 __raw_writel(gps_gpdat, base + OFFS_DAT);
188 __raw_writel(gps_gpcon, base + OFFS_CON);
189
190 S3C_PMDBG("%s: CON %08x => %08x, DAT %08x => %08x\n",
191 chip->chip.label, old_gpcon, gps_gpcon, old_gpdat, gps_gpdat);
192}
193
782d8a3c
KK
194struct samsung_gpio_pm samsung_gpio_pm_2bit = {
195 .save = samsung_gpio_pm_2bit_save,
196 .resume = samsung_gpio_pm_2bit_resume,
d87964c4
BD
197};
198
d78c16cc 199#if defined(CONFIG_ARCH_S3C64XX)
782d8a3c 200static void samsung_gpio_pm_4bit_save(struct samsung_gpio_chip *chip)
d87964c4
BD
201{
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);
205
206 if (chip->chip.ngpio > 8)
207 chip->pm_save[0] = __raw_readl(chip->base - 4);
208}
209
782d8a3c 210static u32 samsung_gpio_pm_4bit_mask(u32 old_gpcon, u32 gps_gpcon)
d87964c4
BD
211{
212 u32 old, new, mask;
213 u32 change_mask = 0x0;
214 int nr;
215
216 for (nr = 0, mask = 0x0f; nr < 16; nr += 4, mask <<= 4) {
217 old = (old_gpcon & mask) >> nr;
218 new = (gps_gpcon & mask) >> nr;
219
220 /* If there is no change, then skip */
221
222 if (old == new)
223 continue;
224
225 /* If both are special function, then skip */
226
227 if (is_sfn(old) && is_sfn(new))
228 continue;
229
230 /* Change is IN => OUT, do not change now */
231
232 if (is_in(old) && is_out(new))
233 continue;
234
235 /* Change is SFN => OUT, do not change now */
236
237 if (is_sfn(old) && is_out(new))
238 continue;
239
240 /* We should now be at the case of IN=>SFN,
241 * OUT=>SFN, OUT=>IN, SFN=>IN. */
242
243 change_mask |= mask;
244 }
245
246 return change_mask;
247}
248
782d8a3c 249static void samsung_gpio_pm_4bit_con(struct samsung_gpio_chip *chip, int index)
d87964c4
BD
250{
251 void __iomem *con = chip->base + (index * 4);
252 u32 old_gpcon = __raw_readl(con);
253 u32 gps_gpcon = chip->pm_save[index + 1];
254 u32 gpcon, mask;
255
782d8a3c 256 mask = samsung_gpio_pm_4bit_mask(old_gpcon, gps_gpcon);
d87964c4
BD
257
258 gpcon = old_gpcon & ~mask;
259 gpcon |= gps_gpcon & mask;
260
261 __raw_writel(gpcon, con);
262}
263
782d8a3c 264static void samsung_gpio_pm_4bit_resume(struct samsung_gpio_chip *chip)
d87964c4
BD
265{
266 void __iomem *base = chip->base;
267 u32 old_gpcon[2];
268 u32 old_gpdat = __raw_readl(base + OFFS_DAT);
269 u32 gps_gpdat = chip->pm_save[2];
270
271 /* First, modify the CON settings */
272
273 old_gpcon[0] = 0;
274 old_gpcon[1] = __raw_readl(base + OFFS_CON);
275
782d8a3c 276 samsung_gpio_pm_4bit_con(chip, 0);
d87964c4
BD
277 if (chip->chip.ngpio > 8) {
278 old_gpcon[0] = __raw_readl(base - 4);
782d8a3c 279 samsung_gpio_pm_4bit_con(chip, -1);
d87964c4
BD
280 }
281
282 /* Now change the configurations that require DAT,CON */
283
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);
288
289 __raw_writel(chip->pm_save[2], base + OFFS_DAT);
290 __raw_writel(chip->pm_save[3], base + OFFS_UP);
291
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);
298 } else
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);
303}
304
782d8a3c
KK
305struct samsung_gpio_pm samsung_gpio_pm_4bit = {
306 .save = samsung_gpio_pm_4bit_save,
307 .resume = samsung_gpio_pm_4bit_resume,
d87964c4 308};
d78c16cc 309#endif /* CONFIG_ARCH_S3C64XX */
d87964c4
BD
310
311/**
782d8a3c 312 * samsung_pm_save_gpio() - save gpio chip data for suspend
d87964c4
BD
313 * @ourchip: The chip for suspend.
314 */
782d8a3c 315static void samsung_pm_save_gpio(struct samsung_gpio_chip *ourchip)
d87964c4 316{
782d8a3c 317 struct samsung_gpio_pm *pm = ourchip->pm;
d87964c4
BD
318
319 if (pm == NULL || pm->save == NULL)
320 S3C_PMDBG("%s: no pm for %s\n", __func__, ourchip->chip.label);
321 else
322 pm->save(ourchip);
323}
324
325/**
782d8a3c 326 * samsung_pm_save_gpios() - Save the state of the GPIO banks.
d87964c4
BD
327 *
328 * For all the GPIO banks, save the state of each one ready for going
329 * into a suspend mode.
330 */
782d8a3c 331void samsung_pm_save_gpios(void)
d87964c4 332{
782d8a3c 333 struct samsung_gpio_chip *ourchip;
d87964c4
BD
334 unsigned int gpio_nr;
335
32b6cb38 336 for (gpio_nr = 0; gpio_nr < S3C_GPIO_END;) {
782d8a3c 337 ourchip = samsung_gpiolib_getchip(gpio_nr);
91dc74e4
P
338 if (!ourchip) {
339 gpio_nr++;
d87964c4 340 continue;
91dc74e4 341 }
d87964c4 342
782d8a3c 343 samsung_pm_save_gpio(ourchip);
d87964c4
BD
344
345 S3C_PMDBG("%s: save %08x,%08x,%08x,%08x\n",
346 ourchip->chip.label,
347 ourchip->pm_save[0],
348 ourchip->pm_save[1],
349 ourchip->pm_save[2],
350 ourchip->pm_save[3]);
351
352 gpio_nr += ourchip->chip.ngpio;
353 gpio_nr += CONFIG_S3C_GPIO_SPACE;
354 }
355}
356
357/**
782d8a3c 358 * samsung_pm_resume_gpio() - restore gpio chip data after suspend
d87964c4
BD
359 * @ourchip: The suspended chip.
360 */
782d8a3c 361static void samsung_pm_resume_gpio(struct samsung_gpio_chip *ourchip)
d87964c4 362{
782d8a3c 363 struct samsung_gpio_pm *pm = ourchip->pm;
d87964c4
BD
364
365 if (pm == NULL || pm->resume == NULL)
366 S3C_PMDBG("%s: no pm for %s\n", __func__, ourchip->chip.label);
367 else
368 pm->resume(ourchip);
369}
370
782d8a3c 371void samsung_pm_restore_gpios(void)
d87964c4 372{
782d8a3c 373 struct samsung_gpio_chip *ourchip;
d87964c4
BD
374 unsigned int gpio_nr;
375
32b6cb38 376 for (gpio_nr = 0; gpio_nr < S3C_GPIO_END;) {
782d8a3c 377 ourchip = samsung_gpiolib_getchip(gpio_nr);
91dc74e4
P
378 if (!ourchip) {
379 gpio_nr++;
d87964c4 380 continue;
91dc74e4 381 }
d87964c4 382
782d8a3c 383 samsung_pm_resume_gpio(ourchip);
d87964c4
BD
384
385 gpio_nr += ourchip->chip.ngpio;
386 gpio_nr += CONFIG_S3C_GPIO_SPACE;
387 }
388}
This page took 0.350074 seconds and 5 git commands to generate.