Initial commit
[kernel/linux-3.0.git] / arch / arm / plat-samsung / pm-gpio.c
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>
17 #include <linux/sysdev.h>
18 #include <linux/init.h>
19 #include <linux/io.h>
20 #include <linux/gpio.h>
21
22 #include <plat/gpio-core.h>
23 #include <plat/pm.h>
24 #include <plat/cpu.h>
25
26 /* PM GPIO helpers */
27
28 #define OFFS_CON        (0x00)
29 #define OFFS_DAT        (0x04)
30 #define OFFS_UP         (0x08)
31
32 static void s3c_gpio_pm_1bit_save(struct s3c_gpio_chip *chip)
33 {
34         chip->pm_save[0] = __raw_readl(chip->base + OFFS_CON);
35         chip->pm_save[1] = __raw_readl(chip->base + OFFS_DAT);
36 }
37
38 static void s3c_gpio_pm_1bit_resume(struct s3c_gpio_chip *chip)
39 {
40         void __iomem *base = chip->base;
41         u32 old_gpcon = __raw_readl(base + OFFS_CON);
42         u32 old_gpdat = __raw_readl(base + OFFS_DAT);
43         u32 gps_gpcon = chip->pm_save[0];
44         u32 gps_gpdat = chip->pm_save[1];
45         u32 gpcon;
46
47         /* GPACON only has one bit per control / data and no PULLUPs.
48          * GPACON[x] = 0 => Output, 1 => SFN */
49
50         /* first set all SFN bits to SFN */
51
52         gpcon = old_gpcon | gps_gpcon;
53         __raw_writel(gpcon, base + OFFS_CON);
54
55         /* now set all the other bits */
56
57         __raw_writel(gps_gpdat, base + OFFS_DAT);
58         __raw_writel(gps_gpcon, base + OFFS_CON);
59
60         S3C_PMDBG("%s: CON %08x => %08x, DAT %08x => %08x\n",
61                   chip->chip.label, old_gpcon, gps_gpcon, old_gpdat, gps_gpdat);
62 }
63
64 struct s3c_gpio_pm s3c_gpio_pm_1bit = {
65         .save   = s3c_gpio_pm_1bit_save,
66         .resume = s3c_gpio_pm_1bit_resume,
67 };
68
69 static void s3c_gpio_pm_2bit_save(struct s3c_gpio_chip *chip)
70 {
71         chip->pm_save[0] = __raw_readl(chip->base + OFFS_CON);
72         chip->pm_save[1] = __raw_readl(chip->base + OFFS_DAT);
73         chip->pm_save[2] = __raw_readl(chip->base + OFFS_UP);
74 }
75
76 /* Test whether the given masked+shifted bits of an GPIO configuration
77  * are one of the SFN (special function) modes. */
78
79 static inline int is_sfn(unsigned long con)
80 {
81         return con >= 2;
82 }
83
84 /* Test if the given masked+shifted GPIO configuration is an input */
85
86 static inline int is_in(unsigned long con)
87 {
88         return con == 0;
89 }
90
91 /* Test if the given masked+shifted GPIO configuration is an output */
92
93 static inline int is_out(unsigned long con)
94 {
95         return con == 1;
96 }
97
98 /**
99  * s3c_gpio_pm_2bit_resume() - restore the given GPIO bank
100  * @chip: The chip information to resume.
101  *
102  * Restore one of the GPIO banks that was saved during suspend. This is
103  * not as simple as once thought, due to the possibility of glitches
104  * from the order that the CON and DAT registers are set in.
105  *
106  * The three states the pin can be are {IN,OUT,SFN} which gives us 9
107  * combinations of changes to check. Three of these, if the pin stays
108  * in the same configuration can be discounted. This leaves us with
109  * the following:
110  *
111  * { IN => OUT }  Change DAT first
112  * { IN => SFN }  Change CON first
113  * { OUT => SFN } Change CON first, so new data will not glitch
114  * { OUT => IN }  Change CON first, so new data will not glitch
115  * { SFN => IN }  Change CON first
116  * { SFN => OUT } Change DAT first, so new data will not glitch [1]
117  *
118  * We do not currently deal with the UP registers as these control
119  * weak resistors, so a small delay in change should not need to bring
120  * these into the calculations.
121  *
122  * [1] this assumes that writing to a pin DAT whilst in SFN will set the
123  *     state for when it is next output.
124  */
125 static void s3c_gpio_pm_2bit_resume(struct s3c_gpio_chip *chip)
126 {
127         void __iomem *base = chip->base;
128         u32 old_gpcon = __raw_readl(base + OFFS_CON);
129         u32 old_gpdat = __raw_readl(base + OFFS_DAT);
130         u32 gps_gpcon = chip->pm_save[0];
131         u32 gps_gpdat = chip->pm_save[1];
132         u32 gpcon, old, new, mask;
133         u32 change_mask = 0x0;
134         int nr;
135
136         /* restore GPIO pull-up settings */
137         __raw_writel(chip->pm_save[2], base + OFFS_UP);
138
139         /* Create a change_mask of all the items that need to have
140          * their CON value changed before their DAT value, so that
141          * we minimise the work between the two settings.
142          */
143
144         for (nr = 0, mask = 0x03; nr < 32; nr += 2, mask <<= 2) {
145                 old = (old_gpcon & mask) >> nr;
146                 new = (gps_gpcon & mask) >> nr;
147
148                 /* If there is no change, then skip */
149
150                 if (old == new)
151                         continue;
152
153                 /* If both are special function, then skip */
154
155                 if (is_sfn(old) && is_sfn(new))
156                         continue;
157
158                 /* Change is IN => OUT, do not change now */
159
160                 if (is_in(old) && is_out(new))
161                         continue;
162
163                 /* Change is SFN => OUT, do not change now */
164
165                 if (is_sfn(old) && is_out(new))
166                         continue;
167
168                 /* We should now be at the case of IN=>SFN,
169                  * OUT=>SFN, OUT=>IN, SFN=>IN. */
170
171                 change_mask |= mask;
172         }
173
174
175         /* Write the new CON settings */
176
177         gpcon = old_gpcon & ~change_mask;
178         gpcon |= gps_gpcon & change_mask;
179
180         __raw_writel(gpcon, base + OFFS_CON);
181
182         /* Now change any items that require DAT,CON */
183
184         __raw_writel(gps_gpdat, base + OFFS_DAT);
185         __raw_writel(gps_gpcon, base + OFFS_CON);
186
187         S3C_PMDBG("%s: CON %08x => %08x, DAT %08x => %08x\n",
188                   chip->chip.label, old_gpcon, gps_gpcon, old_gpdat, gps_gpdat);
189 }
190
191 struct s3c_gpio_pm s3c_gpio_pm_2bit = {
192         .save   = s3c_gpio_pm_2bit_save,
193         .resume = s3c_gpio_pm_2bit_resume,
194 };
195
196 #if defined(CONFIG_ARCH_S3C64XX) || defined(CONFIG_PLAT_S5P)
197 static void s3c_gpio_pm_4bit_save(struct s3c_gpio_chip *chip)
198 {
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);
202
203         if (chip->chip.ngpio > 8)
204                 chip->pm_save[0] = __raw_readl(chip->base - 4);
205 }
206
207 static u32 s3c_gpio_pm_4bit_mask(u32 old_gpcon, u32 gps_gpcon)
208 {
209         u32 old, new, mask;
210         u32 change_mask = 0x0;
211         int nr;
212
213         for (nr = 0, mask = 0x0f; nr < 16; nr += 4, mask <<= 4) {
214                 old = (old_gpcon & mask) >> nr;
215                 new = (gps_gpcon & mask) >> nr;
216
217                 /* If there is no change, then skip */
218
219                 if (old == new)
220                         continue;
221
222                 /* If both are special function, then skip */
223
224                 if (is_sfn(old) && is_sfn(new))
225                         continue;
226
227                 /* Change is IN => OUT, do not change now */
228
229                 if (is_in(old) && is_out(new))
230                         continue;
231
232                 /* Change is SFN => OUT, do not change now */
233
234                 if (is_sfn(old) && is_out(new))
235                         continue;
236
237                 /* We should now be at the case of IN=>SFN,
238                  * OUT=>SFN, OUT=>IN, SFN=>IN. */
239
240                 change_mask |= mask;
241         }
242
243         return change_mask;
244 }
245
246 static void s3c_gpio_pm_4bit_con(struct s3c_gpio_chip *chip, int index)
247 {
248         void __iomem *con = chip->base + (index * 4);
249         u32 old_gpcon = __raw_readl(con);
250         u32 gps_gpcon = chip->pm_save[index + 1];
251         u32 gpcon, mask;
252
253         mask = s3c_gpio_pm_4bit_mask(old_gpcon, gps_gpcon);
254
255         gpcon = old_gpcon & ~mask;
256         gpcon |= gps_gpcon & mask;
257
258         __raw_writel(gpcon, con);
259 }
260
261 static void s3c_gpio_pm_4bit_resume(struct s3c_gpio_chip *chip)
262 {
263         void __iomem *base = chip->base;
264         u32 old_gpcon[2];
265         u32 old_gpdat = __raw_readl(base + OFFS_DAT);
266         u32 gps_gpdat = chip->pm_save[2];
267
268         /* First, modify the CON settings */
269
270         old_gpcon[0] = 0;
271         old_gpcon[1] = __raw_readl(base + OFFS_CON);
272
273         s3c_gpio_pm_4bit_con(chip, 0);
274         if (chip->chip.ngpio > 8) {
275                 old_gpcon[0] = __raw_readl(base - 4);
276                 s3c_gpio_pm_4bit_con(chip, -1);
277         }
278
279         /* Now change the configurations that require DAT,CON */
280
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);
285
286         __raw_writel(chip->pm_save[2], base + OFFS_DAT);
287         __raw_writel(chip->pm_save[3], base + OFFS_UP);
288
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);
295         } else
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);
300 }
301
302 struct s3c_gpio_pm s3c_gpio_pm_4bit = {
303         .save   = s3c_gpio_pm_4bit_save,
304         .resume = s3c_gpio_pm_4bit_resume,
305 };
306 #endif /* CONFIG_ARCH_S3C64XX || CONFIG_PLAT_S5P */
307
308 /**
309  * s3c_pm_save_gpio() - save gpio chip data for suspend
310  * @ourchip: The chip for suspend.
311  */
312 static void s3c_pm_save_gpio(struct s3c_gpio_chip *ourchip)
313 {
314         struct s3c_gpio_pm *pm = ourchip->pm;
315
316         if (pm == NULL || pm->save == NULL)
317                 S3C_PMDBG("%s: no pm for %s\n", __func__, ourchip->chip.label);
318         else
319                 pm->save(ourchip);
320 }
321
322 static int s3c_get_gpio_max_nr (void)
323 {
324         static int gpio_max_nr = 0;
325
326         if (unlikely(!gpio_max_nr)) {
327                 if (soc_is_exynos4210())
328                         gpio_max_nr = EXYNOS4210_GPIO_END;
329                 else if (soc_is_exynos4212() || soc_is_exynos4412())
330                         gpio_max_nr = EXYNOS4212_GPIO_END;
331                 else if (soc_is_exynos5210())
332                         gpio_max_nr = EXYNOS5210_GPIO_END;
333                 else if (soc_is_exynos5250())
334                         gpio_max_nr = EXYNOS5250_GPIO_END;
335                 else
336                         gpio_max_nr = S3C_GPIO_END;
337         }
338
339         return gpio_max_nr;
340 }
341
342 /**
343  * s3c_pm_save_gpios() - Save the state of the GPIO banks.
344  *
345  * For all the GPIO banks, save the state of each one ready for going
346  * into a suspend mode.
347  */
348 void s3c_pm_save_gpios(void)
349 {
350         struct s3c_gpio_chip *ourchip;
351         unsigned int gpio_nr;
352         unsigned int gpio_max_nr;
353
354         gpio_max_nr = s3c_get_gpio_max_nr();
355
356         for (gpio_nr = 0; gpio_nr < gpio_max_nr;) {
357         ourchip = s3c_gpiolib_getchip(gpio_nr);
358                 if (!ourchip) {
359                         gpio_nr++;
360                         continue;
361                 }
362
363                 s3c_pm_save_gpio(ourchip);
364
365                 S3C_PMDBG("%s: save %08x,%08x,%08x,%08x\n",
366                           ourchip->chip.label,
367                           ourchip->pm_save[0],
368                           ourchip->pm_save[1],
369                           ourchip->pm_save[2],
370                           ourchip->pm_save[3]);
371
372                 gpio_nr += ourchip->chip.ngpio;
373                 gpio_nr += CONFIG_S3C_GPIO_SPACE;
374         }
375 }
376
377 /**
378  * s3c_pm_resume_gpio() - restore gpio chip data after suspend
379  * @ourchip: The suspended chip.
380  */
381 static void s3c_pm_resume_gpio(struct s3c_gpio_chip *ourchip)
382 {
383         struct s3c_gpio_pm *pm = ourchip->pm;
384
385         if (pm == NULL || pm->resume == NULL)
386                 S3C_PMDBG("%s: no pm for %s\n", __func__, ourchip->chip.label);
387         else
388                 pm->resume(ourchip);
389 }
390
391 void s3c_pm_restore_gpios(void)
392 {
393         struct s3c_gpio_chip *ourchip;
394         unsigned int gpio_nr;
395         unsigned int gpio_max_nr;
396
397         gpio_max_nr = s3c_get_gpio_max_nr();
398
399         for (gpio_nr = 0; gpio_nr < gpio_max_nr;) {
400                 ourchip = s3c_gpiolib_getchip(gpio_nr);
401                 if (!ourchip) {
402                         gpio_nr++;
403                         continue;
404                 }
405
406                 s3c_pm_resume_gpio(ourchip);
407
408                 gpio_nr += ourchip->chip.ngpio;
409                 gpio_nr += CONFIG_S3C_GPIO_SPACE;
410         }
411 }